blob: 707bb2e53c4ecf10dba2d358d2f7678d999778ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
Harald Welte0ab43f82005-08-09 19:43:44 -070024/* 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
Harald Welteb766b302005-08-12 11:36:44 -070032#define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
Harald Welte0ab43f82005-08-09 19:43:44 -070033
Harald Welte6869c4d2005-08-09 19:24:19 -070034/* only for userspace compatibility */
35#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Generic cache responses from hook functions.
37 <= 0x2000 is used for protocol-flags. */
38#define NFC_UNKNOWN 0x4000
39#define NFC_ALTERED 0x8000
Harald Welte6869c4d2005-08-09 19:24:19 -070040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#ifdef CONFIG_NETFILTER
44
45extern void netfilter_init(void);
46
47/* Largest hook number + 1 */
48#define NF_MAX_HOOKS 8
49
50struct sk_buff;
51struct net_device;
52
53typedef unsigned int nf_hookfn(unsigned int hooknum,
54 struct sk_buff **skb,
55 const struct net_device *in,
56 const struct net_device *out,
57 int (*okfn)(struct sk_buff *));
58
59struct nf_hook_ops
60{
61 struct list_head list;
62
63 /* User fills in from here down. */
64 nf_hookfn *hook;
65 struct module *owner;
66 int pf;
67 int hooknum;
68 /* Hooks are ordered in ascending priority. */
69 int priority;
70};
71
72struct nf_sockopt_ops
73{
74 struct list_head list;
75
76 int pf;
77
78 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
79 int set_optmin;
80 int set_optmax;
81 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080082 int (*compat_set)(struct sock *sk, int optval,
83 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 int get_optmin;
86 int get_optmax;
87 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080088 int (*compat_get)(struct sock *sk, int optval,
89 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* Number of users inside set() or get(). */
92 unsigned int use;
93 struct task_struct *cleanup_task;
94};
95
96/* Each queued (to userspace) skbuff has one of these. */
97struct nf_info
98{
99 /* The ops struct which sent us to userspace. */
100 struct nf_hook_ops *elem;
101
102 /* If we're sent to userspace, this keeps housekeeping info */
103 int pf;
104 unsigned int hook;
105 struct net_device *indev, *outdev;
106 int (*okfn)(struct sk_buff *);
107};
108
109/* Function to register/unregister hook points. */
110int nf_register_hook(struct nf_hook_ops *reg);
111void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700112int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
113void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/* Functions to register get/setsockopt ranges (non-inclusive). You
116 need to check permissions yourself! */
117int nf_register_sockopt(struct nf_sockopt_ops *reg);
118void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
119
120extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
121
Harald Welte608c8e42005-08-09 19:58:27 -0700122/* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
123 * disappear once iptables is replaced with pkttables. Please DO NOT use them
124 * for any new code! */
125#define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
126#define NF_LOG_TCPOPT 0x02 /* Log TCP options */
127#define NF_LOG_IPOPT 0x04 /* Log IP options */
128#define NF_LOG_UID 0x08 /* Log UID owning local socket */
129#define NF_LOG_MASK 0x0f
130
131#define NF_LOG_TYPE_LOG 0x01
132#define NF_LOG_TYPE_ULOG 0x02
133
134struct nf_loginfo {
135 u_int8_t type;
136 union {
137 struct {
138 u_int32_t copy_len;
139 u_int16_t group;
140 u_int16_t qthreshold;
141 } ulog;
142 struct {
143 u_int8_t level;
144 u_int8_t logflags;
145 } log;
146 } u;
147};
148
149typedef void nf_logfn(unsigned int pf,
150 unsigned int hooknum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 const struct sk_buff *skb,
152 const struct net_device *in,
153 const struct net_device *out,
Harald Welte608c8e42005-08-09 19:58:27 -0700154 const struct nf_loginfo *li,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 const char *prefix);
156
Harald Welte608c8e42005-08-09 19:58:27 -0700157struct nf_logger {
158 struct module *me;
159 nf_logfn *logfn;
160 char *name;
161};
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* Function to register/unregister log function. */
Harald Welte608c8e42005-08-09 19:58:27 -0700164int nf_log_register(int pf, struct nf_logger *logger);
Harald Welte8a61fad2005-08-09 20:23:53 -0700165int nf_log_unregister_pf(int pf);
Harald Welte608c8e42005-08-09 19:58:27 -0700166void nf_log_unregister_logger(struct nf_logger *logger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* Calls the registered backend logging function */
169void nf_log_packet(int pf,
170 unsigned int hooknum,
171 const struct sk_buff *skb,
172 const struct net_device *in,
173 const struct net_device *out,
Harald Welte608c8e42005-08-09 19:58:27 -0700174 struct nf_loginfo *li,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 const char *fmt, ...);
Patrick McHardy16a66772006-01-06 23:01:48 -0800176
177int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
178 struct net_device *indev, struct net_device *outdev,
179 int (*okfn)(struct sk_buff *), int thresh);
180
181/**
182 * nf_hook_thresh - call a netfilter hook
183 *
184 * Returns 1 if the hook has allowed the packet to pass. The function
185 * okfn must be invoked by the caller in this case. Any other return
186 * value indicates the packet has been consumed by the hook.
187 */
188static inline int nf_hook_thresh(int pf, unsigned int hook,
189 struct sk_buff **pskb,
190 struct net_device *indev,
191 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800192 int (*okfn)(struct sk_buff *), int thresh,
193 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800194{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800195 if (!cond)
196 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800197#ifndef CONFIG_NETFILTER_DEBUG
198 if (list_empty(&nf_hooks[pf][hook]))
199 return 1;
200#endif
201 return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
202}
203
204static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
205 struct net_device *indev, struct net_device *outdev,
206 int (*okfn)(struct sk_buff *))
207{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800208 return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* Activate hook; either okfn or kfree_skb called, unless a hook
212 returns NF_STOLEN (in which case, it's up to the hook to deal with
213 the consequences).
214
215 Returns -ERRNO if packet dropped. Zero means queued, stolen or
216 accepted.
217*/
218
219/* RR:
220 > I don't want nf_hook to return anything because people might forget
221 > about async and trust the return value to mean "packet was ok".
222
223 AK:
224 Just document it clearly, then you can expect some sense from kernel
225 coders :)
226*/
227
228/* This is gross, but inline doesn't cut it for avoiding the function
229 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Patrick McHardy16a66772006-01-06 23:01:48 -0800231/* HX: It's slightly less gross now. */
232
233#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
234({int __ret; \
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800235if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
236 __ret = (okfn)(skb); \
237__ret;})
238
239#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
240({int __ret; \
241if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800242 __ret = (okfn)(skb); \
243__ret;})
244
245#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
246 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248/* Call setsockopt() */
249int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
250 int len);
251int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
252 int *len);
253
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800254int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
255 char __user *opt, int len);
256int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
257 char __user *opt, int *len);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* Packet queuing */
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700260struct nf_queue_handler {
261 int (*outfn)(struct sk_buff *skb, struct nf_info *info,
262 unsigned int queuenum, void *data);
263 void *data;
264 char *name;
265};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266extern int nf_register_queue_handler(int pf,
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700267 struct nf_queue_handler *qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268extern int nf_unregister_queue_handler(int pf);
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700269extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270extern void nf_reinject(struct sk_buff *skb,
271 struct nf_info *info,
272 unsigned int verdict);
273
274extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
275extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
276
277/* FIXME: Before cache is ever used, this must be implemented for real. */
278extern void nf_invalidate_cache(int pf);
279
Harald Welte089af262005-08-09 19:37:23 -0700280/* Call this before modifying an existing packet: ensures it is
281 modifiable and linear to the point you care about (writable_len).
282 Returns true or false. */
283extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
284
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700285extern u_int16_t nf_csum_update(u_int32_t oldval, u_int32_t newval,
286 u_int32_t csum);
287extern u_int16_t nf_proto_csum_update(struct sk_buff *skb,
288 u_int32_t oldval, u_int32_t newval,
289 u_int16_t csum, int pseudohdr);
290
Patrick McHardybce80322006-04-06 14:18:09 -0700291struct nf_afinfo {
292 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800293 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700294 unsigned int dataoff, u_int8_t protocol);
Patrick McHardybce80322006-04-06 14:18:09 -0700295 void (*saveroute)(const struct sk_buff *skb,
296 struct nf_info *info);
297 int (*reroute)(struct sk_buff **skb,
298 const struct nf_info *info);
299 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700300};
301
Patrick McHardybce80322006-04-06 14:18:09 -0700302extern struct nf_afinfo *nf_afinfo[];
303static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
304{
305 return rcu_dereference(nf_afinfo[family]);
306}
Harald Welte2cc7d572005-08-09 19:42:34 -0700307
Al Virob51655b2006-11-14 21:40:42 -0800308static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700309nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
310 u_int8_t protocol, unsigned short family)
311{
312 struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800313 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700314
315 rcu_read_lock();
316 afinfo = nf_get_afinfo(family);
317 if (afinfo)
318 csum = afinfo->checksum(skb, hook, dataoff, protocol);
319 rcu_read_unlock();
320 return csum;
321}
322
Patrick McHardybce80322006-04-06 14:18:09 -0700323extern int nf_register_afinfo(struct nf_afinfo *afinfo);
324extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
325
326#define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
Harald Welte2cc7d572005-08-09 19:42:34 -0700327
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800328#include <net/flow.h>
329extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
330
331static inline void
332nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
333{
334#ifdef CONFIG_IP_NF_NAT_NEEDED
335 void (*decodefn)(struct sk_buff *, struct flowi *);
336
337 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
338 decodefn(skb, fl);
339#endif
340}
341
Harald Welte608c8e42005-08-09 19:58:27 -0700342#ifdef CONFIG_PROC_FS
343#include <linux/proc_fs.h>
344extern struct proc_dir_entry *proc_net_netfilter;
345#endif
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#else /* !CONFIG_NETFILTER */
348#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800349#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800350static inline int nf_hook_thresh(int pf, unsigned int hook,
351 struct sk_buff **pskb,
352 struct net_device *indev,
353 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800354 int (*okfn)(struct sk_buff *), int thresh,
355 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800356{
357 return okfn(*pskb);
358}
359static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
360 struct net_device *indev, struct net_device *outdev,
361 int (*okfn)(struct sk_buff *))
362{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800363 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
David S. Millerf53b61d2006-01-07 12:50:27 -0800366struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800367static inline void
368nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif /*CONFIG_NETFILTER*/
370
371#endif /*__KERNEL__*/
372#endif /*__LINUX_NETFILTER_H*/