Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __LINUX_NETFILTER_H |
2 | #define __LINUX_NETFILTER_H | |
3 | ||
4 | #ifdef __KERNEL__ | |
5 | #include <linux/init.h> | |
1da177e4 LT |
6 | #include <linux/skbuff.h> |
7 | #include <linux/net.h> | |
8 | #include <linux/if.h> | |
2e3075a2 JE |
9 | #include <linux/in.h> |
10 | #include <linux/in6.h> | |
1da177e4 LT |
11 | #include <linux/wait.h> |
12 | #include <linux/list.h> | |
13 | #endif | |
c8942f1f | 14 | #include <linux/types.h> |
1da177e4 | 15 | #include <linux/compiler.h> |
bee95250 | 16 | #include <linux/sysctl.h> |
1da177e4 LT |
17 | |
18 | /* Responses from hook functions. */ | |
19 | #define NF_DROP 0 | |
20 | #define NF_ACCEPT 1 | |
21 | #define NF_STOLEN 2 | |
22 | #define NF_QUEUE 3 | |
23 | #define NF_REPEAT 4 | |
24 | #define NF_STOP 5 | |
25 | #define NF_MAX_VERDICT NF_STOP | |
26 | ||
0ab43f84 | 27 | /* we overload the higher bits for encoding auxiliary data such as the queue |
f615df76 FW |
28 | * number or errno values. Not nice, but better than additional function |
29 | * arguments. */ | |
30 | #define NF_VERDICT_MASK 0x000000ff | |
31 | ||
32 | /* extra verdict flags have mask 0x0000ff00 */ | |
94b27cc3 | 33 | #define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000 |
0ab43f84 | 34 | |
f615df76 | 35 | /* queue number (NF_QUEUE) or errno (NF_DROP) */ |
0ab43f84 HW |
36 | #define NF_VERDICT_QMASK 0xffff0000 |
37 | #define NF_VERDICT_QBITS 16 | |
38 | ||
f615df76 | 39 | #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE) |
0ab43f84 | 40 | |
f615df76 | 41 | #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP) |
da683650 | 42 | |
6869c4d8 HW |
43 | /* only for userspace compatibility */ |
44 | #ifndef __KERNEL__ | |
1da177e4 LT |
45 | /* Generic cache responses from hook functions. |
46 | <= 0x2000 is used for protocol-flags. */ | |
47 | #define NFC_UNKNOWN 0x4000 | |
48 | #define NFC_ALTERED 0x8000 | |
f615df76 FW |
49 | |
50 | /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */ | |
51 | #define NF_VERDICT_BITS 16 | |
6869c4d8 | 52 | #endif |
1da177e4 | 53 | |
6e23ae2a PM |
54 | enum nf_inet_hooks { |
55 | NF_INET_PRE_ROUTING, | |
56 | NF_INET_LOCAL_IN, | |
57 | NF_INET_FORWARD, | |
58 | NF_INET_LOCAL_OUT, | |
59 | NF_INET_POST_ROUTING, | |
60 | NF_INET_NUMHOOKS | |
61 | }; | |
62 | ||
7e9c6eeb JE |
63 | enum { |
64 | NFPROTO_UNSPEC = 0, | |
65 | NFPROTO_IPV4 = 2, | |
66 | NFPROTO_ARP = 3, | |
67 | NFPROTO_BRIDGE = 7, | |
68 | NFPROTO_IPV6 = 10, | |
69 | NFPROTO_DECNET = 12, | |
70 | NFPROTO_NUMPROTO, | |
71 | }; | |
72 | ||
643a2c15 | 73 | union nf_inet_addr { |
7b33ed22 | 74 | __u32 all[4]; |
643a2c15 JE |
75 | __be32 ip; |
76 | __be32 ip6[4]; | |
2e3075a2 JE |
77 | struct in_addr in; |
78 | struct in6_addr in6; | |
643a2c15 JE |
79 | }; |
80 | ||
1da177e4 | 81 | #ifdef __KERNEL__ |
1da177e4 | 82 | #ifdef CONFIG_NETFILTER |
f615df76 FW |
83 | static inline int NF_DROP_GETERR(int verdict) |
84 | { | |
85 | return -(verdict >> NF_VERDICT_QBITS); | |
86 | } | |
1da177e4 | 87 | |
b8beedd2 PM |
88 | static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, |
89 | const union nf_inet_addr *a2) | |
90 | { | |
91 | return a1->all[0] == a2->all[0] && | |
92 | a1->all[1] == a2->all[1] && | |
93 | a1->all[2] == a2->all[2] && | |
94 | a1->all[3] == a2->all[3]; | |
95 | } | |
96 | ||
efdedd54 DF |
97 | static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, |
98 | union nf_inet_addr *result, | |
99 | const union nf_inet_addr *mask) | |
100 | { | |
101 | result->all[0] = a1->all[0] & mask->all[0]; | |
102 | result->all[1] = a1->all[1] & mask->all[1]; | |
103 | result->all[2] = a1->all[2] & mask->all[2]; | |
104 | result->all[3] = a1->all[3] & mask->all[3]; | |
105 | } | |
106 | ||
1da177e4 LT |
107 | extern void netfilter_init(void); |
108 | ||
109 | /* Largest hook number + 1 */ | |
110 | #define NF_MAX_HOOKS 8 | |
111 | ||
112 | struct sk_buff; | |
1da177e4 LT |
113 | |
114 | typedef unsigned int nf_hookfn(unsigned int hooknum, | |
3db05fea | 115 | struct sk_buff *skb, |
1da177e4 LT |
116 | const struct net_device *in, |
117 | const struct net_device *out, | |
118 | int (*okfn)(struct sk_buff *)); | |
119 | ||
d94d9fee | 120 | struct nf_hook_ops { |
1da177e4 LT |
121 | struct list_head list; |
122 | ||
123 | /* User fills in from here down. */ | |
124 | nf_hookfn *hook; | |
125 | struct module *owner; | |
76108cea JE |
126 | u_int8_t pf; |
127 | unsigned int hooknum; | |
1da177e4 LT |
128 | /* Hooks are ordered in ascending priority. */ |
129 | int priority; | |
130 | }; | |
131 | ||
d94d9fee | 132 | struct nf_sockopt_ops { |
1da177e4 LT |
133 | struct list_head list; |
134 | ||
76108cea | 135 | u_int8_t pf; |
1da177e4 LT |
136 | |
137 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ | |
138 | int set_optmin; | |
139 | int set_optmax; | |
140 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); | |
c30f540b | 141 | #ifdef CONFIG_COMPAT |
3fdadf7d DM |
142 | int (*compat_set)(struct sock *sk, int optval, |
143 | void __user *user, unsigned int len); | |
c30f540b | 144 | #endif |
1da177e4 LT |
145 | int get_optmin; |
146 | int get_optmax; | |
147 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); | |
c30f540b | 148 | #ifdef CONFIG_COMPAT |
3fdadf7d DM |
149 | int (*compat_get)(struct sock *sk, int optval, |
150 | void __user *user, int *len); | |
c30f540b | 151 | #endif |
16fcec35 NH |
152 | /* Use the module struct to lock set/get code in place */ |
153 | struct module *owner; | |
1da177e4 LT |
154 | }; |
155 | ||
1da177e4 LT |
156 | /* Function to register/unregister hook points. */ |
157 | int nf_register_hook(struct nf_hook_ops *reg); | |
158 | void nf_unregister_hook(struct nf_hook_ops *reg); | |
972d1cb1 PM |
159 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n); |
160 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n); | |
1da177e4 LT |
161 | |
162 | /* Functions to register get/setsockopt ranges (non-inclusive). You | |
163 | need to check permissions yourself! */ | |
164 | int nf_register_sockopt(struct nf_sockopt_ops *reg); | |
165 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); | |
166 | ||
7e9c6eeb | 167 | extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
1da177e4 | 168 | |
a2d7ec58 | 169 | #if defined(CONFIG_JUMP_LABEL) |
c5905afb IM |
170 | #include <linux/static_key.h> |
171 | extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; | |
a2d7ec58 ED |
172 | static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook) |
173 | { | |
174 | if (__builtin_constant_p(pf) && | |
175 | __builtin_constant_p(hook)) | |
c5905afb | 176 | return static_key_false(&nf_hooks_needed[pf][hook]); |
a2d7ec58 ED |
177 | |
178 | return !list_empty(&nf_hooks[pf][hook]); | |
179 | } | |
180 | #else | |
181 | static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook) | |
182 | { | |
183 | return !list_empty(&nf_hooks[pf][hook]); | |
184 | } | |
185 | #endif | |
186 | ||
76108cea | 187 | int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
16a6677f PM |
188 | struct net_device *indev, struct net_device *outdev, |
189 | int (*okfn)(struct sk_buff *), int thresh); | |
190 | ||
191 | /** | |
192 | * nf_hook_thresh - call a netfilter hook | |
193 | * | |
194 | * Returns 1 if the hook has allowed the packet to pass. The function | |
195 | * okfn must be invoked by the caller in this case. Any other return | |
196 | * value indicates the packet has been consumed by the hook. | |
197 | */ | |
76108cea | 198 | static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
3db05fea | 199 | struct sk_buff *skb, |
16a6677f PM |
200 | struct net_device *indev, |
201 | struct net_device *outdev, | |
23f3733d | 202 | int (*okfn)(struct sk_buff *), int thresh) |
16a6677f | 203 | { |
a2d7ec58 ED |
204 | if (nf_hooks_active(pf, hook)) |
205 | return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh); | |
206 | return 1; | |
16a6677f PM |
207 | } |
208 | ||
76108cea | 209 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
16a6677f PM |
210 | struct net_device *indev, struct net_device *outdev, |
211 | int (*okfn)(struct sk_buff *)) | |
212 | { | |
23f3733d | 213 | return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN); |
16a6677f | 214 | } |
1da177e4 LT |
215 | |
216 | /* Activate hook; either okfn or kfree_skb called, unless a hook | |
217 | returns NF_STOLEN (in which case, it's up to the hook to deal with | |
218 | the consequences). | |
219 | ||
220 | Returns -ERRNO if packet dropped. Zero means queued, stolen or | |
221 | accepted. | |
222 | */ | |
223 | ||
224 | /* RR: | |
225 | > I don't want nf_hook to return anything because people might forget | |
226 | > about async and trust the return value to mean "packet was ok". | |
227 | ||
228 | AK: | |
229 | Just document it clearly, then you can expect some sense from kernel | |
230 | coders :) | |
231 | */ | |
232 | ||
2249065f JE |
233 | static inline int |
234 | NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb, | |
235 | struct net_device *in, struct net_device *out, | |
236 | int (*okfn)(struct sk_buff *), int thresh) | |
237 | { | |
238 | int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh); | |
239 | if (ret == 1) | |
240 | ret = okfn(skb); | |
241 | return ret; | |
242 | } | |
48d5cad8 | 243 | |
2249065f JE |
244 | static inline int |
245 | NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb, | |
246 | struct net_device *in, struct net_device *out, | |
247 | int (*okfn)(struct sk_buff *), bool cond) | |
248 | { | |
4bac6b18 PM |
249 | int ret; |
250 | ||
251 | if (!cond || | |
ac5aa2e3 | 252 | ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1)) |
2249065f JE |
253 | ret = okfn(skb); |
254 | return ret; | |
255 | } | |
1da177e4 | 256 | |
2249065f JE |
257 | static inline int |
258 | NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb, | |
259 | struct net_device *in, struct net_device *out, | |
260 | int (*okfn)(struct sk_buff *)) | |
261 | { | |
262 | return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN); | |
263 | } | |
1da177e4 LT |
264 | |
265 | /* Call setsockopt() */ | |
76108cea | 266 | int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
b7058842 | 267 | unsigned int len); |
76108cea | 268 | int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
1da177e4 | 269 | int *len); |
c30f540b | 270 | #ifdef CONFIG_COMPAT |
76108cea | 271 | int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, |
b7058842 | 272 | char __user *opt, unsigned int len); |
76108cea | 273 | int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, |
3fdadf7d | 274 | char __user *opt, int *len); |
c30f540b | 275 | #endif |
3fdadf7d | 276 | |
089af26c HW |
277 | /* Call this before modifying an existing packet: ensures it is |
278 | modifiable and linear to the point you care about (writable_len). | |
279 | Returns true or false. */ | |
37d41879 | 280 | extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
089af26c | 281 | |
1841a4c7 | 282 | struct flowi; |
02f014d8 | 283 | struct nf_queue_entry; |
c01cd429 | 284 | |
bce8032e PM |
285 | struct nf_afinfo { |
286 | unsigned short family; | |
b51655b9 | 287 | __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
422c346f | 288 | unsigned int dataoff, u_int8_t protocol); |
d63a6507 PM |
289 | __sum16 (*checksum_partial)(struct sk_buff *skb, |
290 | unsigned int hook, | |
291 | unsigned int dataoff, | |
292 | unsigned int len, | |
293 | u_int8_t protocol); | |
31ad3dd6 | 294 | int (*route)(struct net *net, struct dst_entry **dst, |
0fae2e77 | 295 | struct flowi *fl, bool strict); |
bce8032e | 296 | void (*saveroute)(const struct sk_buff *skb, |
02f014d8 | 297 | struct nf_queue_entry *entry); |
3db05fea | 298 | int (*reroute)(struct sk_buff *skb, |
02f014d8 | 299 | const struct nf_queue_entry *entry); |
bce8032e | 300 | int route_key_size; |
2cc7d573 HW |
301 | }; |
302 | ||
0e60ebe0 | 303 | extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO]; |
1e796fda | 304 | static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family) |
bce8032e PM |
305 | { |
306 | return rcu_dereference(nf_afinfo[family]); | |
307 | } | |
2cc7d573 | 308 | |
b51655b9 | 309 | static inline __sum16 |
422c346f PM |
310 | nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, |
311 | u_int8_t protocol, unsigned short family) | |
312 | { | |
1e796fda | 313 | const struct nf_afinfo *afinfo; |
b51655b9 | 314 | __sum16 csum = 0; |
422c346f PM |
315 | |
316 | rcu_read_lock(); | |
317 | afinfo = nf_get_afinfo(family); | |
318 | if (afinfo) | |
319 | csum = afinfo->checksum(skb, hook, dataoff, protocol); | |
320 | rcu_read_unlock(); | |
321 | return csum; | |
322 | } | |
323 | ||
d63a6507 PM |
324 | static inline __sum16 |
325 | nf_checksum_partial(struct sk_buff *skb, unsigned int hook, | |
326 | unsigned int dataoff, unsigned int len, | |
327 | u_int8_t protocol, unsigned short family) | |
328 | { | |
329 | const struct nf_afinfo *afinfo; | |
330 | __sum16 csum = 0; | |
331 | ||
332 | rcu_read_lock(); | |
333 | afinfo = nf_get_afinfo(family); | |
334 | if (afinfo) | |
335 | csum = afinfo->checksum_partial(skb, hook, dataoff, len, | |
336 | protocol); | |
337 | rcu_read_unlock(); | |
338 | return csum; | |
339 | } | |
340 | ||
1e796fda PM |
341 | extern int nf_register_afinfo(const struct nf_afinfo *afinfo); |
342 | extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo); | |
bce8032e | 343 | |
eb9c7ebe | 344 | #include <net/flow.h> |
c7232c99 | 345 | extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
eb9c7ebe PM |
346 | |
347 | static inline void | |
76108cea | 348 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
eb9c7ebe | 349 | { |
051578cc | 350 | #ifdef CONFIG_NF_NAT_NEEDED |
eb9c7ebe PM |
351 | void (*decodefn)(struct sk_buff *, struct flowi *); |
352 | ||
c7232c99 PM |
353 | rcu_read_lock(); |
354 | decodefn = rcu_dereference(nf_nat_decode_session_hook); | |
355 | if (decodefn) | |
356 | decodefn(skb, fl); | |
357 | rcu_read_unlock(); | |
eb9c7ebe PM |
358 | #endif |
359 | } | |
360 | ||
608c8e4f HW |
361 | #ifdef CONFIG_PROC_FS |
362 | #include <linux/proc_fs.h> | |
363 | extern struct proc_dir_entry *proc_net_netfilter; | |
364 | #endif | |
365 | ||
1da177e4 LT |
366 | #else /* !CONFIG_NETFILTER */ |
367 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) | |
48d5cad8 | 368 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) |
76108cea | 369 | static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
3db05fea | 370 | struct sk_buff *skb, |
f53b61d8 DM |
371 | struct net_device *indev, |
372 | struct net_device *outdev, | |
23f3733d | 373 | int (*okfn)(struct sk_buff *), int thresh) |
f53b61d8 | 374 | { |
3db05fea | 375 | return okfn(skb); |
f53b61d8 | 376 | } |
76108cea | 377 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
f53b61d8 DM |
378 | struct net_device *indev, struct net_device *outdev, |
379 | int (*okfn)(struct sk_buff *)) | |
380 | { | |
9c92d348 | 381 | return 1; |
f53b61d8 | 382 | } |
f53b61d8 | 383 | struct flowi; |
eb9c7ebe | 384 | static inline void |
76108cea JE |
385 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
386 | { | |
387 | } | |
1da177e4 LT |
388 | #endif /*CONFIG_NETFILTER*/ |
389 | ||
5f79e0f9 | 390 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
0e60ebe0 | 391 | extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu; |
5f79e0f9 | 392 | extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); |
0e60ebe0 | 393 | extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu; |
9cb01766 PNA |
394 | |
395 | struct nf_conn; | |
396 | struct nlattr; | |
397 | ||
398 | struct nfq_ct_hook { | |
399 | size_t (*build_size)(const struct nf_conn *ct); | |
400 | int (*build)(struct sk_buff *skb, struct nf_conn *ct); | |
401 | int (*parse)(const struct nlattr *attr, struct nf_conn *ct); | |
d584a61a PNA |
402 | }; |
403 | extern struct nfq_ct_hook __rcu *nfq_ct_hook; | |
404 | ||
405 | struct nfq_ct_nat_hook { | |
8c88f87c PNA |
406 | void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, |
407 | u32 ctinfo, int off); | |
9cb01766 | 408 | }; |
d584a61a | 409 | extern struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook; |
5f79e0f9 YK |
410 | #else |
411 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} | |
412 | #endif | |
413 | ||
1da177e4 LT |
414 | #endif /*__KERNEL__*/ |
415 | #endif /*__LINUX_NETFILTER_H*/ |