]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __LINUX_NETFILTER_H |
2 | #define __LINUX_NETFILTER_H | |
3 | ||
4 | #ifdef __KERNEL__ | |
5 | #include <linux/init.h> | |
6 | #include <linux/types.h> | |
7 | #include <linux/skbuff.h> | |
8 | #include <linux/net.h> | |
9 | #include <linux/if.h> | |
10 | #include <linux/wait.h> | |
11 | #include <linux/list.h> | |
12 | #endif | |
13 | #include <linux/compiler.h> | |
14 | ||
15 | /* Responses from hook functions. */ | |
16 | #define NF_DROP 0 | |
17 | #define NF_ACCEPT 1 | |
18 | #define NF_STOLEN 2 | |
19 | #define NF_QUEUE 3 | |
20 | #define NF_REPEAT 4 | |
21 | #define NF_STOP 5 | |
22 | #define NF_MAX_VERDICT NF_STOP | |
23 | ||
0ab43f84 HW |
24 | /* we overload the higher bits for encoding auxiliary data such as the queue |
25 | * number. Not nice, but better than additional function arguments. */ | |
26 | #define NF_VERDICT_MASK 0x0000ffff | |
27 | #define NF_VERDICT_BITS 16 | |
28 | ||
29 | #define NF_VERDICT_QMASK 0xffff0000 | |
30 | #define NF_VERDICT_QBITS 16 | |
31 | ||
b766b305 | 32 | #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE) |
0ab43f84 | 33 | |
6869c4d8 HW |
34 | /* only for userspace compatibility */ |
35 | #ifndef __KERNEL__ | |
1da177e4 LT |
36 | /* Generic cache responses from hook functions. |
37 | <= 0x2000 is used for protocol-flags. */ | |
38 | #define NFC_UNKNOWN 0x4000 | |
39 | #define NFC_ALTERED 0x8000 | |
6869c4d8 | 40 | #endif |
1da177e4 | 41 | |
6e23ae2a PM |
42 | enum nf_inet_hooks { |
43 | NF_INET_PRE_ROUTING, | |
44 | NF_INET_LOCAL_IN, | |
45 | NF_INET_FORWARD, | |
46 | NF_INET_LOCAL_OUT, | |
47 | NF_INET_POST_ROUTING, | |
48 | NF_INET_NUMHOOKS | |
49 | }; | |
50 | ||
1da177e4 | 51 | #ifdef __KERNEL__ |
1da177e4 LT |
52 | #ifdef CONFIG_NETFILTER |
53 | ||
54 | extern void netfilter_init(void); | |
55 | ||
56 | /* Largest hook number + 1 */ | |
57 | #define NF_MAX_HOOKS 8 | |
58 | ||
59 | struct sk_buff; | |
60 | struct net_device; | |
61 | ||
62 | typedef unsigned int nf_hookfn(unsigned int hooknum, | |
3db05fea | 63 | struct sk_buff *skb, |
1da177e4 LT |
64 | const struct net_device *in, |
65 | const struct net_device *out, | |
66 | int (*okfn)(struct sk_buff *)); | |
67 | ||
68 | struct nf_hook_ops | |
69 | { | |
70 | struct list_head list; | |
71 | ||
72 | /* User fills in from here down. */ | |
73 | nf_hookfn *hook; | |
74 | struct module *owner; | |
75 | int pf; | |
76 | int hooknum; | |
77 | /* Hooks are ordered in ascending priority. */ | |
78 | int priority; | |
79 | }; | |
80 | ||
81 | struct nf_sockopt_ops | |
82 | { | |
83 | struct list_head list; | |
84 | ||
85 | int pf; | |
86 | ||
87 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ | |
88 | int set_optmin; | |
89 | int set_optmax; | |
90 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); | |
3fdadf7d DM |
91 | int (*compat_set)(struct sock *sk, int optval, |
92 | void __user *user, unsigned int len); | |
1da177e4 LT |
93 | |
94 | int get_optmin; | |
95 | int get_optmax; | |
96 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); | |
3fdadf7d DM |
97 | int (*compat_get)(struct sock *sk, int optval, |
98 | void __user *user, int *len); | |
1da177e4 | 99 | |
16fcec35 NH |
100 | /* Use the module struct to lock set/get code in place */ |
101 | struct module *owner; | |
1da177e4 LT |
102 | }; |
103 | ||
104 | /* Each queued (to userspace) skbuff has one of these. */ | |
105 | struct nf_info | |
106 | { | |
107 | /* The ops struct which sent us to userspace. */ | |
108 | struct nf_hook_ops *elem; | |
109 | ||
110 | /* If we're sent to userspace, this keeps housekeeping info */ | |
111 | int pf; | |
112 | unsigned int hook; | |
113 | struct net_device *indev, *outdev; | |
114 | int (*okfn)(struct sk_buff *); | |
115 | }; | |
116 | ||
117 | /* Function to register/unregister hook points. */ | |
118 | int nf_register_hook(struct nf_hook_ops *reg); | |
119 | void nf_unregister_hook(struct nf_hook_ops *reg); | |
972d1cb1 PM |
120 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n); |
121 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n); | |
1da177e4 LT |
122 | |
123 | /* Functions to register get/setsockopt ranges (non-inclusive). You | |
124 | need to check permissions yourself! */ | |
125 | int nf_register_sockopt(struct nf_sockopt_ops *reg); | |
126 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); | |
127 | ||
d62f9ed4 PM |
128 | #ifdef CONFIG_SYSCTL |
129 | /* Sysctl registration */ | |
130 | struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path, | |
131 | struct ctl_table *table); | |
132 | void nf_unregister_sysctl_table(struct ctl_table_header *header, | |
133 | struct ctl_table *table); | |
134 | extern struct ctl_table nf_net_netfilter_sysctl_path[]; | |
135 | extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[]; | |
136 | #endif /* CONFIG_SYSCTL */ | |
137 | ||
1da177e4 LT |
138 | extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS]; |
139 | ||
608c8e4f HW |
140 | /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will |
141 | * disappear once iptables is replaced with pkttables. Please DO NOT use them | |
142 | * for any new code! */ | |
143 | #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */ | |
144 | #define NF_LOG_TCPOPT 0x02 /* Log TCP options */ | |
145 | #define NF_LOG_IPOPT 0x04 /* Log IP options */ | |
146 | #define NF_LOG_UID 0x08 /* Log UID owning local socket */ | |
147 | #define NF_LOG_MASK 0x0f | |
148 | ||
149 | #define NF_LOG_TYPE_LOG 0x01 | |
150 | #define NF_LOG_TYPE_ULOG 0x02 | |
151 | ||
152 | struct nf_loginfo { | |
153 | u_int8_t type; | |
154 | union { | |
155 | struct { | |
156 | u_int32_t copy_len; | |
157 | u_int16_t group; | |
158 | u_int16_t qthreshold; | |
159 | } ulog; | |
160 | struct { | |
161 | u_int8_t level; | |
162 | u_int8_t logflags; | |
163 | } log; | |
164 | } u; | |
165 | }; | |
166 | ||
167 | typedef void nf_logfn(unsigned int pf, | |
168 | unsigned int hooknum, | |
1da177e4 LT |
169 | const struct sk_buff *skb, |
170 | const struct net_device *in, | |
171 | const struct net_device *out, | |
608c8e4f | 172 | const struct nf_loginfo *li, |
1da177e4 LT |
173 | const char *prefix); |
174 | ||
608c8e4f HW |
175 | struct nf_logger { |
176 | struct module *me; | |
177 | nf_logfn *logfn; | |
178 | char *name; | |
179 | }; | |
180 | ||
1da177e4 | 181 | /* Function to register/unregister log function. */ |
608c8e4f | 182 | int nf_log_register(int pf, struct nf_logger *logger); |
e92ad99c | 183 | void nf_log_unregister(struct nf_logger *logger); |
9dc6aa5f | 184 | void nf_log_unregister_pf(int pf); |
1da177e4 LT |
185 | |
186 | /* Calls the registered backend logging function */ | |
187 | void nf_log_packet(int pf, | |
188 | unsigned int hooknum, | |
189 | const struct sk_buff *skb, | |
190 | const struct net_device *in, | |
191 | const struct net_device *out, | |
608c8e4f | 192 | struct nf_loginfo *li, |
1da177e4 | 193 | const char *fmt, ...); |
16a6677f | 194 | |
3db05fea | 195 | int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb, |
16a6677f PM |
196 | struct net_device *indev, struct net_device *outdev, |
197 | int (*okfn)(struct sk_buff *), int thresh); | |
198 | ||
199 | /** | |
200 | * nf_hook_thresh - call a netfilter hook | |
201 | * | |
202 | * Returns 1 if the hook has allowed the packet to pass. The function | |
203 | * okfn must be invoked by the caller in this case. Any other return | |
204 | * value indicates the packet has been consumed by the hook. | |
205 | */ | |
206 | static inline int nf_hook_thresh(int pf, unsigned int hook, | |
3db05fea | 207 | struct sk_buff *skb, |
16a6677f PM |
208 | struct net_device *indev, |
209 | struct net_device *outdev, | |
48d5cad8 PM |
210 | int (*okfn)(struct sk_buff *), int thresh, |
211 | int cond) | |
16a6677f | 212 | { |
48d5cad8 PM |
213 | if (!cond) |
214 | return 1; | |
16a6677f PM |
215 | #ifndef CONFIG_NETFILTER_DEBUG |
216 | if (list_empty(&nf_hooks[pf][hook])) | |
217 | return 1; | |
218 | #endif | |
3db05fea | 219 | return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh); |
16a6677f PM |
220 | } |
221 | ||
3db05fea | 222 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb, |
16a6677f PM |
223 | struct net_device *indev, struct net_device *outdev, |
224 | int (*okfn)(struct sk_buff *)) | |
225 | { | |
3db05fea | 226 | return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1); |
16a6677f | 227 | } |
1da177e4 LT |
228 | |
229 | /* Activate hook; either okfn or kfree_skb called, unless a hook | |
230 | returns NF_STOLEN (in which case, it's up to the hook to deal with | |
231 | the consequences). | |
232 | ||
233 | Returns -ERRNO if packet dropped. Zero means queued, stolen or | |
234 | accepted. | |
235 | */ | |
236 | ||
237 | /* RR: | |
238 | > I don't want nf_hook to return anything because people might forget | |
239 | > about async and trust the return value to mean "packet was ok". | |
240 | ||
241 | AK: | |
242 | Just document it clearly, then you can expect some sense from kernel | |
243 | coders :) | |
244 | */ | |
245 | ||
246 | /* This is gross, but inline doesn't cut it for avoiding the function | |
247 | call in fast path: gcc doesn't inline (needs value tracking?). --RR */ | |
16a6677f PM |
248 | |
249 | /* HX: It's slightly less gross now. */ | |
250 | ||
1da177e4 LT |
251 | #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ |
252 | ({int __ret; \ | |
3db05fea | 253 | if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\ |
48d5cad8 PM |
254 | __ret = (okfn)(skb); \ |
255 | __ret;}) | |
256 | ||
257 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \ | |
258 | ({int __ret; \ | |
3db05fea | 259 | if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\ |
1da177e4 LT |
260 | __ret = (okfn)(skb); \ |
261 | __ret;}) | |
1da177e4 | 262 | |
16a6677f PM |
263 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ |
264 | NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN) | |
1da177e4 LT |
265 | |
266 | /* Call setsockopt() */ | |
267 | int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, | |
268 | int len); | |
269 | int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt, | |
270 | int *len); | |
271 | ||
3fdadf7d DM |
272 | int compat_nf_setsockopt(struct sock *sk, int pf, int optval, |
273 | char __user *opt, int len); | |
274 | int compat_nf_getsockopt(struct sock *sk, int pf, int optval, | |
275 | char __user *opt, int *len); | |
276 | ||
1da177e4 | 277 | /* Packet queuing */ |
bbd86b9f HW |
278 | struct nf_queue_handler { |
279 | int (*outfn)(struct sk_buff *skb, struct nf_info *info, | |
280 | unsigned int queuenum, void *data); | |
281 | void *data; | |
282 | char *name; | |
283 | }; | |
1da177e4 | 284 | extern int nf_register_queue_handler(int pf, |
bbd86b9f | 285 | struct nf_queue_handler *qh); |
ce7663d8 YK |
286 | extern int nf_unregister_queue_handler(int pf, |
287 | struct nf_queue_handler *qh); | |
bbd86b9f | 288 | extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh); |
1da177e4 LT |
289 | extern void nf_reinject(struct sk_buff *skb, |
290 | struct nf_info *info, | |
291 | unsigned int verdict); | |
292 | ||
1da177e4 LT |
293 | /* FIXME: Before cache is ever used, this must be implemented for real. */ |
294 | extern void nf_invalidate_cache(int pf); | |
295 | ||
089af26c HW |
296 | /* Call this before modifying an existing packet: ensures it is |
297 | modifiable and linear to the point you care about (writable_len). | |
298 | Returns true or false. */ | |
37d41879 | 299 | extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
089af26c | 300 | |
bce8032e PM |
301 | struct nf_afinfo { |
302 | unsigned short family; | |
b51655b9 | 303 | __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
422c346f | 304 | unsigned int dataoff, u_int8_t protocol); |
bce8032e PM |
305 | void (*saveroute)(const struct sk_buff *skb, |
306 | struct nf_info *info); | |
3db05fea | 307 | int (*reroute)(struct sk_buff *skb, |
bce8032e PM |
308 | const struct nf_info *info); |
309 | int route_key_size; | |
2cc7d573 HW |
310 | }; |
311 | ||
bce8032e PM |
312 | extern struct nf_afinfo *nf_afinfo[]; |
313 | static inline struct nf_afinfo *nf_get_afinfo(unsigned short family) | |
314 | { | |
315 | return rcu_dereference(nf_afinfo[family]); | |
316 | } | |
2cc7d573 | 317 | |
b51655b9 | 318 | static inline __sum16 |
422c346f PM |
319 | nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, |
320 | u_int8_t protocol, unsigned short family) | |
321 | { | |
322 | struct nf_afinfo *afinfo; | |
b51655b9 | 323 | __sum16 csum = 0; |
422c346f PM |
324 | |
325 | rcu_read_lock(); | |
326 | afinfo = nf_get_afinfo(family); | |
327 | if (afinfo) | |
328 | csum = afinfo->checksum(skb, hook, dataoff, protocol); | |
329 | rcu_read_unlock(); | |
330 | return csum; | |
331 | } | |
332 | ||
bce8032e PM |
333 | extern int nf_register_afinfo(struct nf_afinfo *afinfo); |
334 | extern void nf_unregister_afinfo(struct nf_afinfo *afinfo); | |
335 | ||
336 | #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info)) | |
2cc7d573 | 337 | |
eb9c7ebe PM |
338 | #include <net/flow.h> |
339 | extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); | |
340 | ||
341 | static inline void | |
342 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) | |
343 | { | |
5b1158e9 | 344 | #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED) |
eb9c7ebe PM |
345 | void (*decodefn)(struct sk_buff *, struct flowi *); |
346 | ||
347 | if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL) | |
348 | decodefn(skb, fl); | |
349 | #endif | |
350 | } | |
351 | ||
608c8e4f HW |
352 | #ifdef CONFIG_PROC_FS |
353 | #include <linux/proc_fs.h> | |
354 | extern struct proc_dir_entry *proc_net_netfilter; | |
355 | #endif | |
356 | ||
1da177e4 LT |
357 | #else /* !CONFIG_NETFILTER */ |
358 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) | |
48d5cad8 | 359 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) |
f53b61d8 | 360 | static inline int nf_hook_thresh(int pf, unsigned int hook, |
3db05fea | 361 | struct sk_buff *skb, |
f53b61d8 DM |
362 | struct net_device *indev, |
363 | struct net_device *outdev, | |
48d5cad8 PM |
364 | int (*okfn)(struct sk_buff *), int thresh, |
365 | int cond) | |
f53b61d8 | 366 | { |
3db05fea | 367 | return okfn(skb); |
f53b61d8 | 368 | } |
3db05fea | 369 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb, |
f53b61d8 DM |
370 | struct net_device *indev, struct net_device *outdev, |
371 | int (*okfn)(struct sk_buff *)) | |
372 | { | |
9c92d348 | 373 | return 1; |
f53b61d8 | 374 | } |
f53b61d8 | 375 | struct flowi; |
eb9c7ebe PM |
376 | static inline void |
377 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {} | |
1da177e4 LT |
378 | #endif /*CONFIG_NETFILTER*/ |
379 | ||
5f79e0f9 YK |
380 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
381 | extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); | |
382 | extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); | |
de6e05c4 | 383 | extern void (*nf_ct_destroy)(struct nf_conntrack *); |
5f79e0f9 YK |
384 | #else |
385 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} | |
386 | #endif | |
387 | ||
1da177e4 LT |
388 | #endif /*__KERNEL__*/ |
389 | #endif /*__LINUX_NETFILTER_H*/ |