blob: fef7e67f71016b67967d052cc42e6ed5b129f733 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/skbuff.h>
6#include <linux/net.h>
7#include <linux/if.h>
Jan Engelhardt2e3075a2008-01-14 23:40:34 -08008#include <linux/in.h>
9#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/wait.h>
11#include <linux/list.h>
David Howells607ca462012-10-13 10:46:48 +010012#include <uapi/linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#ifdef CONFIG_NETFILTER
Florian Westphalf615df72011-01-18 15:52:14 +010014static inline int NF_DROP_GETERR(int verdict)
15{
16 return -(verdict >> NF_VERDICT_QBITS);
17}
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Patrick McHardyb8beedd2008-03-25 20:09:33 -070019static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
20 const union nf_inet_addr *a2)
21{
22 return a1->all[0] == a2->all[0] &&
23 a1->all[1] == a2->all[1] &&
24 a1->all[2] == a2->all[2] &&
25 a1->all[3] == a2->all[3];
26}
27
Denys Fedoryshchenkoefdedd52012-05-17 23:08:57 +030028static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
29 union nf_inet_addr *result,
30 const union nf_inet_addr *mask)
31{
32 result->all[0] = a1->all[0] & mask->all[0];
33 result->all[1] = a1->all[1] & mask->all[1];
34 result->all[2] = a1->all[2] & mask->all[2];
35 result->all[3] = a1->all[3] & mask->all[3];
36}
37
Joe Perchesa0f4ecf2013-09-26 14:48:15 -070038int netfilter_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Largest hook number + 1 */
41#define NF_MAX_HOOKS 8
42
43struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Patrick McHardy795aa6e2013-10-10 09:21:55 +020045struct nf_hook_ops;
46typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
Herbert Xu3db05fe2007-10-15 00:53:15 -070047 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 const struct net_device *in,
49 const struct net_device *out,
50 int (*okfn)(struct sk_buff *));
51
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080052struct nf_hook_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct list_head list;
54
55 /* User fills in from here down. */
56 nf_hookfn *hook;
57 struct module *owner;
Jan Engelhardt76108ce2008-10-08 11:35:00 +020058 u_int8_t pf;
59 unsigned int hooknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* Hooks are ordered in ascending priority. */
61 int priority;
62};
63
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080064struct nf_sockopt_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct list_head list;
66
Jan Engelhardt76108ce2008-10-08 11:35:00 +020067 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
70 int set_optmin;
71 int set_optmax;
72 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010073#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080074 int (*compat_set)(struct sock *sk, int optval,
75 void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int get_optmin;
78 int get_optmax;
79 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010080#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080081 int (*compat_get)(struct sock *sk, int optval,
82 void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010083#endif
Neil Horman16fcec32007-09-11 11:28:26 +020084 /* Use the module struct to lock set/get code in place */
85 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Function to register/unregister hook points. */
89int nf_register_hook(struct nf_hook_ops *reg);
90void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -070091int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
92void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* Functions to register get/setsockopt ranges (non-inclusive). You
95 need to check permissions yourself! */
96int nf_register_sockopt(struct nf_sockopt_ops *reg);
97void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
98
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020099extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000101#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100102#include <linux/static_key.h>
103extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000104static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
105{
106 if (__builtin_constant_p(pf) &&
107 __builtin_constant_p(hook))
Ingo Molnarc5905af2012-02-24 08:31:31 +0100108 return static_key_false(&nf_hooks_needed[pf][hook]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000109
110 return !list_empty(&nf_hooks[pf][hook]);
111}
112#else
113static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
114{
115 return !list_empty(&nf_hooks[pf][hook]);
116}
117#endif
118
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200119int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800120 struct net_device *indev, struct net_device *outdev,
121 int (*okfn)(struct sk_buff *), int thresh);
122
123/**
124 * nf_hook_thresh - call a netfilter hook
125 *
126 * Returns 1 if the hook has allowed the packet to pass. The function
127 * okfn must be invoked by the caller in this case. Any other return
128 * value indicates the packet has been consumed by the hook.
129 */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200130static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700131 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800132 struct net_device *indev,
133 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200134 int (*okfn)(struct sk_buff *), int thresh)
Patrick McHardy16a66772006-01-06 23:01:48 -0800135{
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000136 if (nf_hooks_active(pf, hook))
137 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
138 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800139}
140
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200141static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800142 struct net_device *indev, struct net_device *outdev,
143 int (*okfn)(struct sk_buff *))
144{
Jan Engelhardt23f37332009-06-05 17:31:46 +0200145 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
Patrick McHardy16a66772006-01-06 23:01:48 -0800146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Activate hook; either okfn or kfree_skb called, unless a hook
149 returns NF_STOLEN (in which case, it's up to the hook to deal with
150 the consequences).
151
152 Returns -ERRNO if packet dropped. Zero means queued, stolen or
153 accepted.
154*/
155
156/* RR:
157 > I don't want nf_hook to return anything because people might forget
158 > about async and trust the return value to mean "packet was ok".
159
160 AK:
161 Just document it clearly, then you can expect some sense from kernel
162 coders :)
163*/
164
Jan Engelhardt22490652009-06-13 04:13:26 +0200165static inline int
166NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
167 struct net_device *in, struct net_device *out,
168 int (*okfn)(struct sk_buff *), int thresh)
169{
170 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
171 if (ret == 1)
172 ret = okfn(skb);
173 return ret;
174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jan Engelhardt22490652009-06-13 04:13:26 +0200176static inline int
177NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
178 struct net_device *in, struct net_device *out,
179 int (*okfn)(struct sk_buff *), bool cond)
180{
Patrick McHardy4bac6b12010-02-19 08:03:28 +0100181 int ret;
182
183 if (!cond ||
Eric Parisac5aa2e2010-11-12 08:26:06 +0100184 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
Jan Engelhardt22490652009-06-13 04:13:26 +0200185 ret = okfn(skb);
186 return ret;
187}
Patrick McHardy16a66772006-01-06 23:01:48 -0800188
Jan Engelhardt22490652009-06-13 04:13:26 +0200189static inline int
190NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
191 struct net_device *in, struct net_device *out,
192 int (*okfn)(struct sk_buff *))
193{
194 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/* Call setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200198int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
David S. Millerb7058842009-09-30 16:12:20 -0700199 unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200200int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100202#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200203int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
David S. Millerb7058842009-09-30 16:12:20 -0700204 char __user *opt, unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200205int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800206 char __user *opt, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100207#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800208
Harald Welte089af262005-08-09 19:37:23 -0700209/* Call this before modifying an existing packet: ensures it is
210 modifiable and linear to the point you care about (writable_len).
211 Returns true or false. */
Joe Perchesa0f4ecf2013-09-26 14:48:15 -0700212int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700213
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800214struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800215struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800216
Patrick McHardybce80322006-04-06 14:18:09 -0700217struct nf_afinfo {
218 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800219 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700220 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100221 __sum16 (*checksum_partial)(struct sk_buff *skb,
222 unsigned int hook,
223 unsigned int dataoff,
224 unsigned int len,
225 u_int8_t protocol);
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200226 int (*route)(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200227 struct flowi *fl, bool strict);
Patrick McHardybce80322006-04-06 14:18:09 -0700228 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800229 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700230 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800231 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700232 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700233};
234
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100235extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800236static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700237{
238 return rcu_dereference(nf_afinfo[family]);
239}
Harald Welte2cc7d572005-08-09 19:42:34 -0700240
Al Virob51655b2006-11-14 21:40:42 -0800241static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700242nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
243 u_int8_t protocol, unsigned short family)
244{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800245 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800246 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700247
248 rcu_read_lock();
249 afinfo = nf_get_afinfo(family);
250 if (afinfo)
251 csum = afinfo->checksum(skb, hook, dataoff, protocol);
252 rcu_read_unlock();
253 return csum;
254}
255
Patrick McHardyd63a6502008-03-20 15:15:53 +0100256static inline __sum16
257nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
258 unsigned int dataoff, unsigned int len,
259 u_int8_t protocol, unsigned short family)
260{
261 const struct nf_afinfo *afinfo;
262 __sum16 csum = 0;
263
264 rcu_read_lock();
265 afinfo = nf_get_afinfo(family);
266 if (afinfo)
267 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
268 protocol);
269 rcu_read_unlock();
270 return csum;
271}
272
Joe Perchesa0f4ecf2013-09-26 14:48:15 -0700273int nf_register_afinfo(const struct nf_afinfo *afinfo);
274void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700275
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800276#include <net/flow.h>
Patrick McHardyc7232c92012-08-26 19:14:06 +0200277extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800278
279static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200280nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800281{
Patrick McHardy051578c2007-12-17 22:42:51 -0800282#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800283 void (*decodefn)(struct sk_buff *, struct flowi *);
284
Patrick McHardyc7232c92012-08-26 19:14:06 +0200285 rcu_read_lock();
286 decodefn = rcu_dereference(nf_nat_decode_session_hook);
287 if (decodefn)
288 decodefn(skb, fl);
289 rcu_read_unlock();
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800290#endif
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#else /* !CONFIG_NETFILTER */
294#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800295#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200296static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700297 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800298 struct net_device *indev,
299 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200300 int (*okfn)(struct sk_buff *), int thresh)
David S. Millerf53b61d2006-01-07 12:50:27 -0800301{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700302 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800303}
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200304static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800305 struct net_device *indev, struct net_device *outdev,
306 int (*okfn)(struct sk_buff *))
307{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800308 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800309}
David S. Millerf53b61d2006-01-07 12:50:27 -0800310struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800311static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200312nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
313{
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif /*CONFIG_NETFILTER*/
316
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700317#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Patrick McHardy312a0c162013-07-28 22:54:08 +0200318extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
Joe Perchesa0f4ecf2013-09-26 14:48:15 -0700319void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100320extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200321
322struct nf_conn;
Patrick McHardy41d73ec2013-08-27 08:50:12 +0200323enum ip_conntrack_info;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200324struct nlattr;
325
326struct nfq_ct_hook {
327 size_t (*build_size)(const struct nf_conn *ct);
328 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
329 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
Pablo Neira Ayusobd077932013-08-07 18:13:20 +0200330 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
331 u32 portid, u32 report);
Patrick McHardy41d73ec2013-08-27 08:50:12 +0200332 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
333 enum ip_conntrack_info ctinfo, s32 off);
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200334};
335extern struct nfq_ct_hook __rcu *nfq_ct_hook;
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700336#else
337static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
338#endif
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif /*__LINUX_NETFILTER_H*/