]>
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 LT |
41 | |
42 | #ifdef __KERNEL__ | |
43 | #include <linux/config.h> | |
44 | #ifdef CONFIG_NETFILTER | |
45 | ||
46 | extern void netfilter_init(void); | |
47 | ||
48 | /* Largest hook number + 1 */ | |
49 | #define NF_MAX_HOOKS 8 | |
50 | ||
51 | struct sk_buff; | |
52 | struct net_device; | |
53 | ||
54 | typedef unsigned int nf_hookfn(unsigned int hooknum, | |
55 | struct sk_buff **skb, | |
56 | const struct net_device *in, | |
57 | const struct net_device *out, | |
58 | int (*okfn)(struct sk_buff *)); | |
59 | ||
60 | struct nf_hook_ops | |
61 | { | |
62 | struct list_head list; | |
63 | ||
64 | /* User fills in from here down. */ | |
65 | nf_hookfn *hook; | |
66 | struct module *owner; | |
67 | int pf; | |
68 | int hooknum; | |
69 | /* Hooks are ordered in ascending priority. */ | |
70 | int priority; | |
71 | }; | |
72 | ||
73 | struct nf_sockopt_ops | |
74 | { | |
75 | struct list_head list; | |
76 | ||
77 | int pf; | |
78 | ||
79 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ | |
80 | int set_optmin; | |
81 | int set_optmax; | |
82 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); | |
3fdadf7d DM |
83 | int (*compat_set)(struct sock *sk, int optval, |
84 | void __user *user, unsigned int len); | |
1da177e4 LT |
85 | |
86 | int get_optmin; | |
87 | int get_optmax; | |
88 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); | |
3fdadf7d DM |
89 | int (*compat_get)(struct sock *sk, int optval, |
90 | void __user *user, int *len); | |
1da177e4 LT |
91 | |
92 | /* Number of users inside set() or get(). */ | |
93 | unsigned int use; | |
94 | struct task_struct *cleanup_task; | |
95 | }; | |
96 | ||
97 | /* Each queued (to userspace) skbuff has one of these. */ | |
98 | struct nf_info | |
99 | { | |
100 | /* The ops struct which sent us to userspace. */ | |
101 | struct nf_hook_ops *elem; | |
102 | ||
103 | /* If we're sent to userspace, this keeps housekeeping info */ | |
104 | int pf; | |
105 | unsigned int hook; | |
106 | struct net_device *indev, *outdev; | |
107 | int (*okfn)(struct sk_buff *); | |
108 | }; | |
109 | ||
110 | /* Function to register/unregister hook points. */ | |
111 | int nf_register_hook(struct nf_hook_ops *reg); | |
112 | void nf_unregister_hook(struct nf_hook_ops *reg); | |
113 | ||
114 | /* Functions to register get/setsockopt ranges (non-inclusive). You | |
115 | need to check permissions yourself! */ | |
116 | int nf_register_sockopt(struct nf_sockopt_ops *reg); | |
117 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); | |
118 | ||
119 | extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS]; | |
120 | ||
608c8e4f HW |
121 | /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will |
122 | * disappear once iptables is replaced with pkttables. Please DO NOT use them | |
123 | * for any new code! */ | |
124 | #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */ | |
125 | #define NF_LOG_TCPOPT 0x02 /* Log TCP options */ | |
126 | #define NF_LOG_IPOPT 0x04 /* Log IP options */ | |
127 | #define NF_LOG_UID 0x08 /* Log UID owning local socket */ | |
128 | #define NF_LOG_MASK 0x0f | |
129 | ||
130 | #define NF_LOG_TYPE_LOG 0x01 | |
131 | #define NF_LOG_TYPE_ULOG 0x02 | |
132 | ||
133 | struct nf_loginfo { | |
134 | u_int8_t type; | |
135 | union { | |
136 | struct { | |
137 | u_int32_t copy_len; | |
138 | u_int16_t group; | |
139 | u_int16_t qthreshold; | |
140 | } ulog; | |
141 | struct { | |
142 | u_int8_t level; | |
143 | u_int8_t logflags; | |
144 | } log; | |
145 | } u; | |
146 | }; | |
147 | ||
148 | typedef void nf_logfn(unsigned int pf, | |
149 | unsigned int hooknum, | |
1da177e4 LT |
150 | const struct sk_buff *skb, |
151 | const struct net_device *in, | |
152 | const struct net_device *out, | |
608c8e4f | 153 | const struct nf_loginfo *li, |
1da177e4 LT |
154 | const char *prefix); |
155 | ||
608c8e4f HW |
156 | struct nf_logger { |
157 | struct module *me; | |
158 | nf_logfn *logfn; | |
159 | char *name; | |
160 | }; | |
161 | ||
1da177e4 | 162 | /* Function to register/unregister log function. */ |
608c8e4f | 163 | int nf_log_register(int pf, struct nf_logger *logger); |
8a61fadb | 164 | int nf_log_unregister_pf(int pf); |
608c8e4f | 165 | void nf_log_unregister_logger(struct nf_logger *logger); |
1da177e4 LT |
166 | |
167 | /* Calls the registered backend logging function */ | |
168 | void nf_log_packet(int pf, | |
169 | unsigned int hooknum, | |
170 | const struct sk_buff *skb, | |
171 | const struct net_device *in, | |
172 | const struct net_device *out, | |
608c8e4f | 173 | struct nf_loginfo *li, |
1da177e4 | 174 | const char *fmt, ...); |
16a6677f PM |
175 | |
176 | int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb, | |
177 | struct net_device *indev, struct net_device *outdev, | |
178 | int (*okfn)(struct sk_buff *), int thresh); | |
179 | ||
180 | /** | |
181 | * nf_hook_thresh - call a netfilter hook | |
182 | * | |
183 | * Returns 1 if the hook has allowed the packet to pass. The function | |
184 | * okfn must be invoked by the caller in this case. Any other return | |
185 | * value indicates the packet has been consumed by the hook. | |
186 | */ | |
187 | static inline int nf_hook_thresh(int pf, unsigned int hook, | |
188 | struct sk_buff **pskb, | |
189 | struct net_device *indev, | |
190 | struct net_device *outdev, | |
48d5cad8 PM |
191 | int (*okfn)(struct sk_buff *), int thresh, |
192 | int cond) | |
16a6677f | 193 | { |
48d5cad8 PM |
194 | if (!cond) |
195 | return 1; | |
16a6677f PM |
196 | #ifndef CONFIG_NETFILTER_DEBUG |
197 | if (list_empty(&nf_hooks[pf][hook])) | |
198 | return 1; | |
199 | #endif | |
200 | return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh); | |
201 | } | |
202 | ||
203 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, | |
204 | struct net_device *indev, struct net_device *outdev, | |
205 | int (*okfn)(struct sk_buff *)) | |
206 | { | |
48d5cad8 | 207 | return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1); |
16a6677f | 208 | } |
1da177e4 LT |
209 | |
210 | /* Activate hook; either okfn or kfree_skb called, unless a hook | |
211 | returns NF_STOLEN (in which case, it's up to the hook to deal with | |
212 | the consequences). | |
213 | ||
214 | Returns -ERRNO if packet dropped. Zero means queued, stolen or | |
215 | accepted. | |
216 | */ | |
217 | ||
218 | /* RR: | |
219 | > I don't want nf_hook to return anything because people might forget | |
220 | > about async and trust the return value to mean "packet was ok". | |
221 | ||
222 | AK: | |
223 | Just document it clearly, then you can expect some sense from kernel | |
224 | coders :) | |
225 | */ | |
226 | ||
227 | /* This is gross, but inline doesn't cut it for avoiding the function | |
228 | call in fast path: gcc doesn't inline (needs value tracking?). --RR */ | |
16a6677f PM |
229 | |
230 | /* HX: It's slightly less gross now. */ | |
231 | ||
1da177e4 LT |
232 | #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ |
233 | ({int __ret; \ | |
48d5cad8 PM |
234 | if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\ |
235 | __ret = (okfn)(skb); \ | |
236 | __ret;}) | |
237 | ||
238 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \ | |
239 | ({int __ret; \ | |
240 | if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\ | |
1da177e4 LT |
241 | __ret = (okfn)(skb); \ |
242 | __ret;}) | |
1da177e4 | 243 | |
16a6677f PM |
244 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ |
245 | NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN) | |
1da177e4 LT |
246 | |
247 | /* Call setsockopt() */ | |
248 | int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, | |
249 | int len); | |
250 | int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt, | |
251 | int *len); | |
252 | ||
3fdadf7d DM |
253 | int compat_nf_setsockopt(struct sock *sk, int pf, int optval, |
254 | char __user *opt, int len); | |
255 | int compat_nf_getsockopt(struct sock *sk, int pf, int optval, | |
256 | char __user *opt, int *len); | |
257 | ||
1da177e4 | 258 | /* Packet queuing */ |
bbd86b9f HW |
259 | struct nf_queue_handler { |
260 | int (*outfn)(struct sk_buff *skb, struct nf_info *info, | |
261 | unsigned int queuenum, void *data); | |
262 | void *data; | |
263 | char *name; | |
264 | }; | |
1da177e4 | 265 | extern int nf_register_queue_handler(int pf, |
bbd86b9f | 266 | struct nf_queue_handler *qh); |
1da177e4 | 267 | extern int nf_unregister_queue_handler(int pf); |
bbd86b9f | 268 | extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh); |
1da177e4 LT |
269 | extern void nf_reinject(struct sk_buff *skb, |
270 | struct nf_info *info, | |
271 | unsigned int verdict); | |
272 | ||
273 | extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); | |
274 | extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); | |
275 | ||
276 | /* FIXME: Before cache is ever used, this must be implemented for real. */ | |
277 | extern void nf_invalidate_cache(int pf); | |
278 | ||
089af26c HW |
279 | /* Call this before modifying an existing packet: ensures it is |
280 | modifiable and linear to the point you care about (writable_len). | |
281 | Returns true or false. */ | |
282 | extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len); | |
283 | ||
2cc7d573 HW |
284 | struct nf_queue_rerouter { |
285 | void (*save)(const struct sk_buff *skb, struct nf_info *info); | |
286 | int (*reroute)(struct sk_buff **skb, const struct nf_info *info); | |
287 | int rer_size; | |
288 | }; | |
289 | ||
290 | #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info)) | |
291 | ||
292 | extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer); | |
293 | extern int nf_unregister_queue_rerouter(int pf); | |
294 | ||
eb9c7ebe PM |
295 | #include <net/flow.h> |
296 | extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); | |
297 | ||
298 | static inline void | |
299 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) | |
300 | { | |
301 | #ifdef CONFIG_IP_NF_NAT_NEEDED | |
302 | void (*decodefn)(struct sk_buff *, struct flowi *); | |
303 | ||
304 | if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL) | |
305 | decodefn(skb, fl); | |
306 | #endif | |
307 | } | |
308 | ||
608c8e4f HW |
309 | #ifdef CONFIG_PROC_FS |
310 | #include <linux/proc_fs.h> | |
311 | extern struct proc_dir_entry *proc_net_netfilter; | |
312 | #endif | |
313 | ||
1da177e4 LT |
314 | #else /* !CONFIG_NETFILTER */ |
315 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) | |
48d5cad8 | 316 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) |
f53b61d8 DM |
317 | static inline int nf_hook_thresh(int pf, unsigned int hook, |
318 | struct sk_buff **pskb, | |
319 | struct net_device *indev, | |
320 | struct net_device *outdev, | |
48d5cad8 PM |
321 | int (*okfn)(struct sk_buff *), int thresh, |
322 | int cond) | |
f53b61d8 DM |
323 | { |
324 | return okfn(*pskb); | |
325 | } | |
326 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, | |
327 | struct net_device *indev, struct net_device *outdev, | |
328 | int (*okfn)(struct sk_buff *)) | |
329 | { | |
9c92d348 | 330 | return 1; |
f53b61d8 | 331 | } |
1da177e4 | 332 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} |
f53b61d8 | 333 | struct flowi; |
eb9c7ebe PM |
334 | static inline void |
335 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {} | |
1da177e4 LT |
336 | #endif /*CONFIG_NETFILTER*/ |
337 | ||
338 | #endif /*__KERNEL__*/ | |
339 | #endif /*__LINUX_NETFILTER_H*/ |