1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Header for use in defining a given L4 protocol for connection tracking.
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalized L3 protocol dependent part.
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
11 #ifndef _NF_CONNTRACK_L4PROTO_H
12 #define _NF_CONNTRACK_L4PROTO_H
13 #include <linux/netlink.h>
14 #include <net/netlink.h>
15 #include <net/netfilter/nf_conntrack.h>
16 #include <net/netns/generic.h>
20 struct nf_conntrack_l4proto {
21 /* L4 Protocol number. */
24 /* Resolve clashes on insertion races. */
27 /* protoinfo nlattr size, closes a hole */
30 /* called by gc worker if table is full */
31 bool (*can_early_drop)(const struct nf_conn *ct);
33 /* convert protoinfo to nfnetink attributes */
34 int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
35 struct nf_conn *ct, bool destroy);
37 /* convert nfnetlink attributes to protoinfo */
38 int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
40 int (*tuple_to_nlattr)(struct sk_buff *skb,
41 const struct nf_conntrack_tuple *t);
42 /* Calculate tuple nlattr size */
43 unsigned int (*nlattr_tuple_size)(void);
44 int (*nlattr_to_tuple)(struct nlattr *tb[],
45 struct nf_conntrack_tuple *t,
47 const struct nla_policy *nla_policy;
50 int (*nlattr_to_obj)(struct nlattr *tb[],
51 struct net *net, void *data);
52 int (*obj_to_nlattr)(struct sk_buff *skb, const void *data);
56 const struct nla_policy *nla_policy;
58 #ifdef CONFIG_NF_CONNTRACK_PROCFS
59 /* Print out the private part of the conntrack. */
60 void (*print_conntrack)(struct seq_file *s, struct nf_conn *);
64 bool icmp_pkt_to_tuple(const struct sk_buff *skb,
67 struct nf_conntrack_tuple *tuple);
69 bool icmpv6_pkt_to_tuple(const struct sk_buff *skb,
72 struct nf_conntrack_tuple *tuple);
74 bool nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple *tuple,
75 const struct nf_conntrack_tuple *orig);
76 bool nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple *tuple,
77 const struct nf_conntrack_tuple *orig);
79 int nf_conntrack_inet_error(struct nf_conn *tmpl, struct sk_buff *skb,
81 const struct nf_hook_state *state,
83 union nf_inet_addr *outer_daddr);
85 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
88 const struct nf_hook_state *state);
90 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
93 const struct nf_hook_state *state);
95 int nf_conntrack_icmp_packet(struct nf_conn *ct,
97 enum ip_conntrack_info ctinfo,
98 const struct nf_hook_state *state);
100 int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
102 enum ip_conntrack_info ctinfo,
103 const struct nf_hook_state *state);
105 int nf_conntrack_udp_packet(struct nf_conn *ct,
107 unsigned int dataoff,
108 enum ip_conntrack_info ctinfo,
109 const struct nf_hook_state *state);
110 int nf_conntrack_udplite_packet(struct nf_conn *ct,
112 unsigned int dataoff,
113 enum ip_conntrack_info ctinfo,
114 const struct nf_hook_state *state);
115 int nf_conntrack_tcp_packet(struct nf_conn *ct,
117 unsigned int dataoff,
118 enum ip_conntrack_info ctinfo,
119 const struct nf_hook_state *state);
120 int nf_conntrack_dccp_packet(struct nf_conn *ct,
122 unsigned int dataoff,
123 enum ip_conntrack_info ctinfo,
124 const struct nf_hook_state *state);
125 int nf_conntrack_sctp_packet(struct nf_conn *ct,
127 unsigned int dataoff,
128 enum ip_conntrack_info ctinfo,
129 const struct nf_hook_state *state);
130 int nf_conntrack_gre_packet(struct nf_conn *ct,
132 unsigned int dataoff,
133 enum ip_conntrack_info ctinfo,
134 const struct nf_hook_state *state);
136 void nf_conntrack_generic_init_net(struct net *net);
137 void nf_conntrack_tcp_init_net(struct net *net);
138 void nf_conntrack_udp_init_net(struct net *net);
139 void nf_conntrack_gre_init_net(struct net *net);
140 void nf_conntrack_dccp_init_net(struct net *net);
141 void nf_conntrack_sctp_init_net(struct net *net);
142 void nf_conntrack_icmp_init_net(struct net *net);
143 void nf_conntrack_icmpv6_init_net(struct net *net);
145 /* Existing built-in generic protocol */
146 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
148 #define MAX_NF_CT_PROTO IPPROTO_UDPLITE
150 const struct nf_conntrack_l4proto *nf_ct_l4proto_find(u8 l4proto);
152 /* Generic netlink helpers */
153 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
154 const struct nf_conntrack_tuple *tuple);
155 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
156 struct nf_conntrack_tuple *t,
158 unsigned int nf_ct_port_nlattr_tuple_size(void);
159 extern const struct nla_policy nf_ct_port_nla_policy[];
162 __printf(3, 4) __cold
163 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
164 const struct nf_conn *ct,
165 const char *fmt, ...);
166 __printf(5, 6) __cold
167 void nf_l4proto_log_invalid(const struct sk_buff *skb,
170 const char *fmt, ...);
172 static inline __printf(5, 6) __cold
173 void nf_l4proto_log_invalid(const struct sk_buff *skb, struct net *net,
174 u16 pf, u8 protonum, const char *fmt, ...) {}
175 static inline __printf(3, 4) __cold
176 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
177 const struct nf_conn *ct,
178 const char *fmt, ...) { }
179 #endif /* CONFIG_SYSCTL */
181 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
182 static inline struct nf_generic_net *nf_generic_pernet(struct net *net)
184 return &net->ct.nf_ct_proto.generic;
187 static inline struct nf_tcp_net *nf_tcp_pernet(struct net *net)
189 return &net->ct.nf_ct_proto.tcp;
192 static inline struct nf_udp_net *nf_udp_pernet(struct net *net)
194 return &net->ct.nf_ct_proto.udp;
197 static inline struct nf_icmp_net *nf_icmp_pernet(struct net *net)
199 return &net->ct.nf_ct_proto.icmp;
202 static inline struct nf_icmp_net *nf_icmpv6_pernet(struct net *net)
204 return &net->ct.nf_ct_proto.icmpv6;
207 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */
208 static inline void nf_ct_set_tcp_be_liberal(struct nf_conn *ct)
210 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
211 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
214 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */
215 static inline bool nf_conntrack_tcp_established(const struct nf_conn *ct)
217 return ct->proto.tcp.state == TCP_CONNTRACK_ESTABLISHED &&
218 test_bit(IPS_ASSURED_BIT, &ct->status);
222 #ifdef CONFIG_NF_CT_PROTO_DCCP
223 static inline struct nf_dccp_net *nf_dccp_pernet(struct net *net)
225 return &net->ct.nf_ct_proto.dccp;
229 #ifdef CONFIG_NF_CT_PROTO_SCTP
230 static inline struct nf_sctp_net *nf_sctp_pernet(struct net *net)
232 return &net->ct.nf_ct_proto.sctp;
236 #ifdef CONFIG_NF_CT_PROTO_GRE
237 static inline struct nf_gre_net *nf_gre_pernet(struct net *net)
239 return &net->ct.nf_ct_proto.gre;
243 #endif /*_NF_CONNTRACK_PROTOCOL_H*/