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