]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __LINUX_NETFILTER_H |
2 | #define __LINUX_NETFILTER_H | |
3 | ||
1da177e4 | 4 | #include <linux/init.h> |
1da177e4 LT |
5 | #include <linux/skbuff.h> |
6 | #include <linux/net.h> | |
7 | #include <linux/if.h> | |
2e3075a2 JE |
8 | #include <linux/in.h> |
9 | #include <linux/in6.h> | |
1da177e4 LT |
10 | #include <linux/wait.h> |
11 | #include <linux/list.h> | |
d1c85c2e | 12 | #include <linux/static_key.h> |
a263653e | 13 | #include <linux/netfilter_defs.h> |
085db2c0 EB |
14 | #include <linux/netdevice.h> |
15 | #include <net/net_namespace.h> | |
a263653e | 16 | |
1da177e4 | 17 | #ifdef CONFIG_NETFILTER |
f615df76 FW |
18 | static inline int NF_DROP_GETERR(int verdict) |
19 | { | |
20 | return -(verdict >> NF_VERDICT_QBITS); | |
21 | } | |
1da177e4 | 22 | |
b8beedd2 PM |
23 | static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, |
24 | const union nf_inet_addr *a2) | |
25 | { | |
26 | return a1->all[0] == a2->all[0] && | |
27 | a1->all[1] == a2->all[1] && | |
28 | a1->all[2] == a2->all[2] && | |
29 | a1->all[3] == a2->all[3]; | |
30 | } | |
31 | ||
efdedd54 DF |
32 | static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, |
33 | union nf_inet_addr *result, | |
34 | const union nf_inet_addr *mask) | |
35 | { | |
36 | result->all[0] = a1->all[0] & mask->all[0]; | |
37 | result->all[1] = a1->all[1] & mask->all[1]; | |
38 | result->all[2] = a1->all[2] & mask->all[2]; | |
39 | result->all[3] = a1->all[3] & mask->all[3]; | |
40 | } | |
41 | ||
a0f4ecf3 | 42 | int netfilter_init(void); |
1da177e4 | 43 | |
1da177e4 | 44 | struct sk_buff; |
1da177e4 | 45 | |
795aa6ef | 46 | struct nf_hook_ops; |
cfdfab31 | 47 | |
1c984f8a DM |
48 | struct sock; |
49 | ||
cfdfab31 DM |
50 | struct nf_hook_state { |
51 | unsigned int hook; | |
52 | int thresh; | |
53 | u_int8_t pf; | |
54 | struct net_device *in; | |
55 | struct net_device *out; | |
1c984f8a | 56 | struct sock *sk; |
b11b1f65 | 57 | struct net *net; |
e3b37f11 | 58 | struct nf_hook_entry __rcu *hook_entries; |
0c4b51f0 | 59 | int (*okfn)(struct net *, struct sock *, struct sk_buff *); |
cfdfab31 DM |
60 | }; |
61 | ||
e3b37f11 AC |
62 | typedef unsigned int nf_hookfn(void *priv, |
63 | struct sk_buff *skb, | |
64 | const struct nf_hook_state *state); | |
65 | struct nf_hook_ops { | |
66 | struct list_head list; | |
67 | ||
68 | /* User fills in from here down. */ | |
69 | nf_hookfn *hook; | |
70 | struct net_device *dev; | |
71 | void *priv; | |
72 | u_int8_t pf; | |
73 | unsigned int hooknum; | |
74 | /* Hooks are ordered in ascending priority. */ | |
75 | int priority; | |
76 | }; | |
77 | ||
78 | struct nf_hook_entry { | |
79 | struct nf_hook_entry __rcu *next; | |
80 | struct nf_hook_ops ops; | |
81 | const struct nf_hook_ops *orig_ops; | |
82 | }; | |
83 | ||
107a9f4d | 84 | static inline void nf_hook_state_init(struct nf_hook_state *p, |
e3b37f11 | 85 | struct nf_hook_entry *hook_entry, |
107a9f4d DM |
86 | unsigned int hook, |
87 | int thresh, u_int8_t pf, | |
88 | struct net_device *indev, | |
89 | struct net_device *outdev, | |
1c984f8a | 90 | struct sock *sk, |
b11b1f65 | 91 | struct net *net, |
0c4b51f0 | 92 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
107a9f4d DM |
93 | { |
94 | p->hook = hook; | |
95 | p->thresh = thresh; | |
96 | p->pf = pf; | |
97 | p->in = indev; | |
98 | p->out = outdev; | |
1c984f8a | 99 | p->sk = sk; |
b11b1f65 | 100 | p->net = net; |
e3b37f11 | 101 | RCU_INIT_POINTER(p->hook_entries, hook_entry); |
107a9f4d DM |
102 | p->okfn = okfn; |
103 | } | |
104 | ||
1da177e4 | 105 | |
1da177e4 | 106 | |
d94d9fee | 107 | struct nf_sockopt_ops { |
1da177e4 LT |
108 | struct list_head list; |
109 | ||
76108cea | 110 | u_int8_t pf; |
1da177e4 LT |
111 | |
112 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ | |
113 | int set_optmin; | |
114 | int set_optmax; | |
115 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); | |
c30f540b | 116 | #ifdef CONFIG_COMPAT |
3fdadf7d DM |
117 | int (*compat_set)(struct sock *sk, int optval, |
118 | void __user *user, unsigned int len); | |
c30f540b | 119 | #endif |
1da177e4 LT |
120 | int get_optmin; |
121 | int get_optmax; | |
122 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); | |
c30f540b | 123 | #ifdef CONFIG_COMPAT |
3fdadf7d DM |
124 | int (*compat_get)(struct sock *sk, int optval, |
125 | void __user *user, int *len); | |
c30f540b | 126 | #endif |
16fcec35 NH |
127 | /* Use the module struct to lock set/get code in place */ |
128 | struct module *owner; | |
1da177e4 LT |
129 | }; |
130 | ||
1da177e4 | 131 | /* Function to register/unregister hook points. */ |
085db2c0 EB |
132 | int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops); |
133 | void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops); | |
134 | int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, | |
135 | unsigned int n); | |
136 | void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, | |
137 | unsigned int n); | |
138 | ||
1da177e4 LT |
139 | int nf_register_hook(struct nf_hook_ops *reg); |
140 | void nf_unregister_hook(struct nf_hook_ops *reg); | |
972d1cb1 PM |
141 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n); |
142 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n); | |
e8bffe0c MB |
143 | int _nf_register_hooks(struct nf_hook_ops *reg, unsigned int n); |
144 | void _nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n); | |
1da177e4 LT |
145 | |
146 | /* Functions to register get/setsockopt ranges (non-inclusive). You | |
147 | need to check permissions yourself! */ | |
148 | int nf_register_sockopt(struct nf_sockopt_ops *reg); | |
149 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); | |
150 | ||
d1c85c2e | 151 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 152 | extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
a2d7ec58 ED |
153 | #endif |
154 | ||
cfdfab31 | 155 | int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state); |
16a6677f PM |
156 | |
157 | /** | |
158 | * nf_hook_thresh - call a netfilter hook | |
b8d0aad0 | 159 | * |
16a6677f PM |
160 | * Returns 1 if the hook has allowed the packet to pass. The function |
161 | * okfn must be invoked by the caller in this case. Any other return | |
162 | * value indicates the packet has been consumed by the hook. | |
163 | */ | |
76108cea | 164 | static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
7a773504 | 165 | struct net *net, |
7026b1dd | 166 | struct sock *sk, |
3db05fea | 167 | struct sk_buff *skb, |
16a6677f PM |
168 | struct net_device *indev, |
169 | struct net_device *outdev, | |
0c4b51f0 | 170 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
7026b1dd | 171 | int thresh) |
16a6677f | 172 | { |
e3b37f11 AC |
173 | struct nf_hook_entry *hook_head; |
174 | int ret = 1; | |
af4610c3 FW |
175 | |
176 | #ifdef HAVE_JUMP_LABEL | |
177 | if (__builtin_constant_p(pf) && | |
178 | __builtin_constant_p(hook) && | |
179 | !static_key_false(&nf_hooks_needed[pf][hook])) | |
180 | return 1; | |
181 | #endif | |
182 | ||
e3b37f11 AC |
183 | rcu_read_lock(); |
184 | hook_head = rcu_dereference(net->nf.hooks[pf][hook]); | |
185 | if (hook_head) { | |
107a9f4d | 186 | struct nf_hook_state state; |
cfdfab31 | 187 | |
e3b37f11 | 188 | nf_hook_state_init(&state, hook_head, hook, thresh, |
b11b1f65 | 189 | pf, indev, outdev, sk, net, okfn); |
fe72926b FW |
190 | |
191 | ret = nf_hook_slow(skb, &state); | |
cfdfab31 | 192 | } |
e3b37f11 AC |
193 | rcu_read_unlock(); |
194 | ||
195 | return ret; | |
16a6677f PM |
196 | } |
197 | ||
29a26a56 EB |
198 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, |
199 | struct sock *sk, struct sk_buff *skb, | |
200 | struct net_device *indev, struct net_device *outdev, | |
0c4b51f0 | 201 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
16a6677f | 202 | { |
7a773504 | 203 | return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN); |
16a6677f | 204 | } |
1da177e4 LT |
205 | |
206 | /* Activate hook; either okfn or kfree_skb called, unless a hook | |
207 | returns NF_STOLEN (in which case, it's up to the hook to deal with | |
208 | the consequences). | |
209 | ||
210 | Returns -ERRNO if packet dropped. Zero means queued, stolen or | |
211 | accepted. | |
212 | */ | |
213 | ||
214 | /* RR: | |
215 | > I don't want nf_hook to return anything because people might forget | |
216 | > about async and trust the return value to mean "packet was ok". | |
217 | ||
218 | AK: | |
219 | Just document it clearly, then you can expect some sense from kernel | |
220 | coders :) | |
221 | */ | |
222 | ||
2249065f | 223 | static inline int |
29a26a56 | 224 | NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
7026b1dd DM |
225 | struct sk_buff *skb, struct net_device *in, |
226 | struct net_device *out, | |
0c4b51f0 EB |
227 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
228 | int thresh) | |
2249065f | 229 | { |
7a773504 | 230 | int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh); |
2249065f | 231 | if (ret == 1) |
0c4b51f0 | 232 | ret = okfn(net, sk, skb); |
2249065f JE |
233 | return ret; |
234 | } | |
48d5cad8 | 235 | |
2249065f | 236 | static inline int |
29a26a56 | 237 | NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
7026b1dd | 238 | struct sk_buff *skb, struct net_device *in, struct net_device *out, |
0c4b51f0 EB |
239 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
240 | bool cond) | |
2249065f | 241 | { |
4bac6b18 PM |
242 | int ret; |
243 | ||
244 | if (!cond || | |
7a773504 | 245 | ((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1)) |
0c4b51f0 | 246 | ret = okfn(net, sk, skb); |
2249065f JE |
247 | return ret; |
248 | } | |
1da177e4 | 249 | |
2249065f | 250 | static inline int |
29a26a56 | 251 | NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, |
2249065f | 252 | struct net_device *in, struct net_device *out, |
0c4b51f0 | 253 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
2249065f | 254 | { |
29a26a56 | 255 | return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN); |
2249065f | 256 | } |
1da177e4 LT |
257 | |
258 | /* Call setsockopt() */ | |
76108cea | 259 | int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
b7058842 | 260 | unsigned int len); |
76108cea | 261 | int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
1da177e4 | 262 | int *len); |
c30f540b | 263 | #ifdef CONFIG_COMPAT |
76108cea | 264 | int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, |
b7058842 | 265 | char __user *opt, unsigned int len); |
76108cea | 266 | int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, |
3fdadf7d | 267 | char __user *opt, int *len); |
c30f540b | 268 | #endif |
3fdadf7d | 269 | |
089af26c HW |
270 | /* Call this before modifying an existing packet: ensures it is |
271 | modifiable and linear to the point you care about (writable_len). | |
272 | Returns true or false. */ | |
a0f4ecf3 | 273 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
089af26c | 274 | |
1841a4c7 | 275 | struct flowi; |
02f014d8 | 276 | struct nf_queue_entry; |
c01cd429 | 277 | |
bce8032e PM |
278 | struct nf_afinfo { |
279 | unsigned short family; | |
b51655b9 | 280 | __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
422c346f | 281 | unsigned int dataoff, u_int8_t protocol); |
d63a6507 PM |
282 | __sum16 (*checksum_partial)(struct sk_buff *skb, |
283 | unsigned int hook, | |
284 | unsigned int dataoff, | |
285 | unsigned int len, | |
286 | u_int8_t protocol); | |
31ad3dd6 | 287 | int (*route)(struct net *net, struct dst_entry **dst, |
0fae2e77 | 288 | struct flowi *fl, bool strict); |
bce8032e | 289 | void (*saveroute)(const struct sk_buff *skb, |
02f014d8 | 290 | struct nf_queue_entry *entry); |
d815d90b | 291 | int (*reroute)(struct net *net, struct sk_buff *skb, |
02f014d8 | 292 | const struct nf_queue_entry *entry); |
bce8032e | 293 | int route_key_size; |
2cc7d573 HW |
294 | }; |
295 | ||
0e60ebe0 | 296 | extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO]; |
1e796fda | 297 | static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family) |
bce8032e PM |
298 | { |
299 | return rcu_dereference(nf_afinfo[family]); | |
300 | } | |
2cc7d573 | 301 | |
b51655b9 | 302 | static inline __sum16 |
422c346f PM |
303 | nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, |
304 | u_int8_t protocol, unsigned short family) | |
305 | { | |
1e796fda | 306 | const struct nf_afinfo *afinfo; |
b51655b9 | 307 | __sum16 csum = 0; |
422c346f PM |
308 | |
309 | rcu_read_lock(); | |
310 | afinfo = nf_get_afinfo(family); | |
311 | if (afinfo) | |
312 | csum = afinfo->checksum(skb, hook, dataoff, protocol); | |
313 | rcu_read_unlock(); | |
314 | return csum; | |
315 | } | |
316 | ||
d63a6507 PM |
317 | static inline __sum16 |
318 | nf_checksum_partial(struct sk_buff *skb, unsigned int hook, | |
319 | unsigned int dataoff, unsigned int len, | |
320 | u_int8_t protocol, unsigned short family) | |
321 | { | |
322 | const struct nf_afinfo *afinfo; | |
323 | __sum16 csum = 0; | |
324 | ||
325 | rcu_read_lock(); | |
326 | afinfo = nf_get_afinfo(family); | |
327 | if (afinfo) | |
328 | csum = afinfo->checksum_partial(skb, hook, dataoff, len, | |
329 | protocol); | |
330 | rcu_read_unlock(); | |
331 | return csum; | |
332 | } | |
333 | ||
a0f4ecf3 JP |
334 | int nf_register_afinfo(const struct nf_afinfo *afinfo); |
335 | void nf_unregister_afinfo(const struct nf_afinfo *afinfo); | |
bce8032e | 336 | |
eb9c7ebe | 337 | #include <net/flow.h> |
c7232c99 | 338 | extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
eb9c7ebe PM |
339 | |
340 | static inline void | |
76108cea | 341 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
eb9c7ebe | 342 | { |
051578cc | 343 | #ifdef CONFIG_NF_NAT_NEEDED |
eb9c7ebe PM |
344 | void (*decodefn)(struct sk_buff *, struct flowi *); |
345 | ||
c7232c99 PM |
346 | rcu_read_lock(); |
347 | decodefn = rcu_dereference(nf_nat_decode_session_hook); | |
348 | if (decodefn) | |
349 | decodefn(skb, fl); | |
350 | rcu_read_unlock(); | |
eb9c7ebe PM |
351 | #endif |
352 | } | |
353 | ||
1da177e4 | 354 | #else /* !CONFIG_NETFILTER */ |
008027c3 AB |
355 | static inline int |
356 | NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, | |
357 | struct sk_buff *skb, struct net_device *in, struct net_device *out, | |
358 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), | |
359 | bool cond) | |
360 | { | |
361 | return okfn(net, sk, skb); | |
362 | } | |
363 | ||
364 | static inline int | |
365 | NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, | |
366 | struct sk_buff *skb, struct net_device *in, struct net_device *out, | |
367 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) | |
368 | { | |
369 | return okfn(net, sk, skb); | |
370 | } | |
371 | ||
29a26a56 EB |
372 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, |
373 | struct sock *sk, struct sk_buff *skb, | |
374 | struct net_device *indev, struct net_device *outdev, | |
0c4b51f0 | 375 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
f53b61d8 | 376 | { |
9c92d348 | 377 | return 1; |
f53b61d8 | 378 | } |
f53b61d8 | 379 | struct flowi; |
eb9c7ebe | 380 | static inline void |
76108cea JE |
381 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
382 | { | |
383 | } | |
1da177e4 LT |
384 | #endif /*CONFIG_NETFILTER*/ |
385 | ||
5f79e0f9 | 386 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
62da9865 DB |
387 | #include <linux/netfilter/nf_conntrack_zones_common.h> |
388 | ||
312a0c16 | 389 | extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu; |
a0f4ecf3 | 390 | void nf_ct_attach(struct sk_buff *, const struct sk_buff *); |
0e60ebe0 | 391 | extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu; |
b7bd1809 PNA |
392 | #else |
393 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} | |
394 | #endif | |
9cb01766 PNA |
395 | |
396 | struct nf_conn; | |
41d73ec0 | 397 | enum ip_conntrack_info; |
9cb01766 PNA |
398 | struct nlattr; |
399 | ||
a4b4766c | 400 | struct nfnl_ct_hook { |
224a0597 | 401 | struct nf_conn *(*get_ct)(const struct sk_buff *skb, |
b7bd1809 | 402 | enum ip_conntrack_info *ctinfo); |
9cb01766 | 403 | size_t (*build_size)(const struct nf_conn *ct); |
b7bd1809 PNA |
404 | int (*build)(struct sk_buff *skb, struct nf_conn *ct, |
405 | enum ip_conntrack_info ctinfo, | |
406 | u_int16_t ct_attr, u_int16_t ct_info_attr); | |
9cb01766 | 407 | int (*parse)(const struct nlattr *attr, struct nf_conn *ct); |
bd077937 PNA |
408 | int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, |
409 | u32 portid, u32 report); | |
8c88f87c | 410 | void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, |
41d73ec0 | 411 | enum ip_conntrack_info ctinfo, s32 off); |
9cb01766 | 412 | }; |
a4b4766c | 413 | extern struct nfnl_ct_hook __rcu *nfnl_ct_hook; |
5f79e0f9 | 414 | |
e7c8899f FW |
415 | /** |
416 | * nf_skb_duplicated - TEE target has sent a packet | |
417 | * | |
418 | * When a xtables target sends a packet, the OUTPUT and POSTROUTING | |
419 | * hooks are traversed again, i.e. nft and xtables are invoked recursively. | |
420 | * | |
421 | * This is used by xtables TEE target to prevent the duplicated skb from | |
422 | * being duplicated again. | |
423 | */ | |
424 | DECLARE_PER_CPU(bool, nf_skb_duplicated); | |
425 | ||
1da177e4 | 426 | #endif /*__LINUX_NETFILTER_H*/ |