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