xref: /linux/include/net/netfilter/nf_conntrack_l4proto.h (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Header for use in defining a given L4 protocol for connection tracking.
4  *
5  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6  *	- generalized L3 protocol dependent part.
7  *
8  * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
9  */
10 
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>
17 
18 struct seq_file;
19 
20 struct nf_conntrack_l4proto {
21 	/* L4 Protocol number. */
22 	u_int8_t l4proto;
23 
24 	/* Resolve clashes on insertion races. */
25 	bool allow_clash;
26 
27 	/* protoinfo nlattr size, closes a hole */
28 	u16 nlattr_size;
29 
30 	/* called by gc worker if table is full */
31 	bool (*can_early_drop)(const struct nf_conn *ct);
32 
33 	/* convert protoinfo to nfnetlink attributes */
34 	int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
35 			 struct nf_conn *ct, bool destroy);
36 
37 	/* convert nfnetlink attributes to protoinfo */
38 	int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
39 
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,
46 			       u_int32_t flags);
47 	const struct nla_policy *nla_policy;
48 
49 	struct {
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);
53 
54 		u16 obj_size;
55 		u16 nlattr_max;
56 		const struct nla_policy *nla_policy;
57 	} ctnl_timeout;
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 *);
61 #endif
62 };
63 
64 bool icmp_pkt_to_tuple(const struct sk_buff *skb,
65 		       unsigned int dataoff,
66 		       struct net *net,
67 		       struct nf_conntrack_tuple *tuple);
68 
69 bool icmpv6_pkt_to_tuple(const struct sk_buff *skb,
70 			 unsigned int dataoff,
71 			 struct net *net,
72 			 struct nf_conntrack_tuple *tuple);
73 
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);
78 
79 int nf_conntrack_inet_error(struct nf_conn *tmpl, struct sk_buff *skb,
80 			    unsigned int dataoff,
81 			    const struct nf_hook_state *state,
82 			    u8 l4proto,
83 			    union nf_inet_addr *outer_daddr);
84 
85 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
86 			      struct sk_buff *skb,
87 			      unsigned int dataoff,
88 			      const struct nf_hook_state *state);
89 
90 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
91 			      struct sk_buff *skb,
92 			      unsigned int dataoff,
93 			      const struct nf_hook_state *state);
94 
95 int nf_conntrack_icmp_packet(struct nf_conn *ct,
96 			     struct sk_buff *skb,
97 			     enum ip_conntrack_info ctinfo,
98 			     const struct nf_hook_state *state);
99 
100 int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
101 			       struct sk_buff *skb,
102 			       enum ip_conntrack_info ctinfo,
103 			       const struct nf_hook_state *state);
104 
105 int nf_conntrack_udp_packet(struct nf_conn *ct,
106 			    struct sk_buff *skb,
107 			    unsigned int dataoff,
108 			    enum ip_conntrack_info ctinfo,
109 			    const struct nf_hook_state *state);
110 int nf_conntrack_tcp_packet(struct nf_conn *ct,
111 			    struct sk_buff *skb,
112 			    unsigned int dataoff,
113 			    enum ip_conntrack_info ctinfo,
114 			    const struct nf_hook_state *state);
115 int nf_conntrack_sctp_packet(struct nf_conn *ct,
116 			     struct sk_buff *skb,
117 			     unsigned int dataoff,
118 			     enum ip_conntrack_info ctinfo,
119 			     const struct nf_hook_state *state);
120 int nf_conntrack_gre_packet(struct nf_conn *ct,
121 			    struct sk_buff *skb,
122 			    unsigned int dataoff,
123 			    enum ip_conntrack_info ctinfo,
124 			    const struct nf_hook_state *state);
125 
126 void nf_conntrack_generic_init_net(struct net *net);
127 void nf_conntrack_tcp_init_net(struct net *net);
128 void nf_conntrack_udp_init_net(struct net *net);
129 void nf_conntrack_gre_init_net(struct net *net);
130 void nf_conntrack_sctp_init_net(struct net *net);
131 void nf_conntrack_icmp_init_net(struct net *net);
132 void nf_conntrack_icmpv6_init_net(struct net *net);
133 
134 /* Existing built-in generic protocol */
135 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
136 
137 const struct nf_conntrack_l4proto *nf_ct_l4proto_find(u8 l4proto);
138 
139 /* Generic netlink helpers */
140 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
141 			       const struct nf_conntrack_tuple *tuple);
142 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
143 			       struct nf_conntrack_tuple *t,
144 			       u_int32_t flags);
145 unsigned int nf_ct_port_nlattr_tuple_size(void);
146 extern const struct nla_policy nf_ct_port_nla_policy[];
147 
148 #ifdef CONFIG_SYSCTL
149 __printf(4, 5) __cold
150 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
151 			       const struct nf_conn *ct,
152 			       const struct nf_hook_state *state,
153 			       const char *fmt, ...);
154 __printf(4, 5) __cold
155 void nf_l4proto_log_invalid(const struct sk_buff *skb,
156 			    const struct nf_hook_state *state,
157 			    u8 protonum,
158 			    const char *fmt, ...);
159 #else
160 static inline __printf(4, 5) __cold
161 void nf_l4proto_log_invalid(const struct sk_buff *skb,
162 			    const struct nf_hook_state *state,
163 			    u8 protonum,
164 			    const char *fmt, ...) {}
165 static inline __printf(4, 5) __cold
166 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
167 			       const struct nf_conn *ct,
168 			       const struct nf_hook_state *state,
169 			       const char *fmt, ...) { }
170 #endif /* CONFIG_SYSCTL */
171 
172 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
173 static inline struct nf_generic_net *nf_generic_pernet(struct net *net)
174 {
175 	return &net->ct.nf_ct_proto.generic;
176 }
177 
178 static inline struct nf_tcp_net *nf_tcp_pernet(struct net *net)
179 {
180 	return &net->ct.nf_ct_proto.tcp;
181 }
182 
183 static inline struct nf_udp_net *nf_udp_pernet(struct net *net)
184 {
185 	return &net->ct.nf_ct_proto.udp;
186 }
187 
188 static inline struct nf_icmp_net *nf_icmp_pernet(struct net *net)
189 {
190 	return &net->ct.nf_ct_proto.icmp;
191 }
192 
193 static inline struct nf_icmp_net *nf_icmpv6_pernet(struct net *net)
194 {
195 	return &net->ct.nf_ct_proto.icmpv6;
196 }
197 
198 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */
199 static inline void nf_ct_set_tcp_be_liberal(struct nf_conn *ct)
200 {
201 	ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
202 	ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
203 }
204 
205 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */
206 static inline bool nf_conntrack_tcp_established(const struct nf_conn *ct)
207 {
208 	return ct->proto.tcp.state == TCP_CONNTRACK_ESTABLISHED &&
209 	       test_bit(IPS_ASSURED_BIT, &ct->status);
210 }
211 #endif
212 
213 #ifdef CONFIG_NF_CT_PROTO_SCTP
214 static inline struct nf_sctp_net *nf_sctp_pernet(struct net *net)
215 {
216 	return &net->ct.nf_ct_proto.sctp;
217 }
218 #endif
219 
220 #ifdef CONFIG_NF_CT_PROTO_GRE
221 static inline struct nf_gre_net *nf_gre_pernet(struct net *net)
222 {
223 	return &net->ct.nf_ct_proto.gre;
224 }
225 #endif
226 
227 #endif /*_NF_CONNTRACK_PROTOCOL_H*/
228