blob: de70f7b45b682126284a4162535659bfde8e5be1 [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
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +000038extern int 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
45typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070046 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 const struct net_device *in,
48 const struct net_device *out,
49 int (*okfn)(struct sk_buff *));
50
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080051struct nf_hook_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct list_head list;
53
54 /* User fills in from here down. */
55 nf_hookfn *hook;
56 struct module *owner;
Jan Engelhardt76108ce2008-10-08 11:35:00 +020057 u_int8_t pf;
58 unsigned int hooknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* Hooks are ordered in ascending priority. */
60 int priority;
61};
62
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080063struct nf_sockopt_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct list_head list;
65
Jan Engelhardt76108ce2008-10-08 11:35:00 +020066 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
69 int set_optmin;
70 int set_optmax;
71 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010072#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080073 int (*compat_set)(struct sock *sk, int optval,
74 void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int get_optmin;
77 int get_optmax;
78 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010079#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080080 int (*compat_get)(struct sock *sk, int optval,
81 void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +010082#endif
Neil Horman16fcec32007-09-11 11:28:26 +020083 /* Use the module struct to lock set/get code in place */
84 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Function to register/unregister hook points. */
88int nf_register_hook(struct nf_hook_ops *reg);
89void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -070090int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
91void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* Functions to register get/setsockopt ranges (non-inclusive). You
94 need to check permissions yourself! */
95int nf_register_sockopt(struct nf_sockopt_ops *reg);
96void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
97
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020098extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000100#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +0100101#include <linux/static_key.h>
102extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000103static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
104{
105 if (__builtin_constant_p(pf) &&
106 __builtin_constant_p(hook))
Ingo Molnarc5905af2012-02-24 08:31:31 +0100107 return static_key_false(&nf_hooks_needed[pf][hook]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000108
109 return !list_empty(&nf_hooks[pf][hook]);
110}
111#else
112static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
113{
114 return !list_empty(&nf_hooks[pf][hook]);
115}
116#endif
117
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200118int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800119 struct net_device *indev, struct net_device *outdev,
120 int (*okfn)(struct sk_buff *), int thresh);
121
122/**
123 * nf_hook_thresh - call a netfilter hook
124 *
125 * Returns 1 if the hook has allowed the packet to pass. The function
126 * okfn must be invoked by the caller in this case. Any other return
127 * value indicates the packet has been consumed by the hook.
128 */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200129static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700130 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800131 struct net_device *indev,
132 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200133 int (*okfn)(struct sk_buff *), int thresh)
Patrick McHardy16a66772006-01-06 23:01:48 -0800134{
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000135 if (nf_hooks_active(pf, hook))
136 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
137 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800138}
139
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200140static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800141 struct net_device *indev, struct net_device *outdev,
142 int (*okfn)(struct sk_buff *))
143{
Jan Engelhardt23f37332009-06-05 17:31:46 +0200144 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
Patrick McHardy16a66772006-01-06 23:01:48 -0800145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* Activate hook; either okfn or kfree_skb called, unless a hook
148 returns NF_STOLEN (in which case, it's up to the hook to deal with
149 the consequences).
150
151 Returns -ERRNO if packet dropped. Zero means queued, stolen or
152 accepted.
153*/
154
155/* RR:
156 > I don't want nf_hook to return anything because people might forget
157 > about async and trust the return value to mean "packet was ok".
158
159 AK:
160 Just document it clearly, then you can expect some sense from kernel
161 coders :)
162*/
163
Jan Engelhardt22490652009-06-13 04:13:26 +0200164static inline int
165NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
166 struct net_device *in, struct net_device *out,
167 int (*okfn)(struct sk_buff *), int thresh)
168{
169 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
170 if (ret == 1)
171 ret = okfn(skb);
172 return ret;
173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jan Engelhardt22490652009-06-13 04:13:26 +0200175static inline int
176NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
177 struct net_device *in, struct net_device *out,
178 int (*okfn)(struct sk_buff *), bool cond)
179{
Patrick McHardy4bac6b12010-02-19 08:03:28 +0100180 int ret;
181
182 if (!cond ||
Eric Parisac5aa2e2010-11-12 08:26:06 +0100183 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
Jan Engelhardt22490652009-06-13 04:13:26 +0200184 ret = okfn(skb);
185 return ret;
186}
Patrick McHardy16a66772006-01-06 23:01:48 -0800187
Jan Engelhardt22490652009-06-13 04:13:26 +0200188static inline int
189NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
190 struct net_device *in, struct net_device *out,
191 int (*okfn)(struct sk_buff *))
192{
193 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/* Call setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200197int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
David S. Millerb7058842009-09-30 16:12:20 -0700198 unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200199int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100201#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200202int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
David S. Millerb7058842009-09-30 16:12:20 -0700203 char __user *opt, unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200204int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800205 char __user *opt, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100206#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800207
Harald Welte089af262005-08-09 19:37:23 -0700208/* Call this before modifying an existing packet: ensures it is
209 modifiable and linear to the point you care about (writable_len).
210 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700211extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700212
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800213struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800214struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800215
Patrick McHardybce80322006-04-06 14:18:09 -0700216struct nf_afinfo {
217 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800218 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700219 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100220 __sum16 (*checksum_partial)(struct sk_buff *skb,
221 unsigned int hook,
222 unsigned int dataoff,
223 unsigned int len,
224 u_int8_t protocol);
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200225 int (*route)(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200226 struct flowi *fl, bool strict);
Patrick McHardybce80322006-04-06 14:18:09 -0700227 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800228 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700229 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800230 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700231 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700232};
233
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100234extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800235static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700236{
237 return rcu_dereference(nf_afinfo[family]);
238}
Harald Welte2cc7d572005-08-09 19:42:34 -0700239
Al Virob51655b2006-11-14 21:40:42 -0800240static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700241nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
242 u_int8_t protocol, unsigned short family)
243{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800244 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800245 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700246
247 rcu_read_lock();
248 afinfo = nf_get_afinfo(family);
249 if (afinfo)
250 csum = afinfo->checksum(skb, hook, dataoff, protocol);
251 rcu_read_unlock();
252 return csum;
253}
254
Patrick McHardyd63a6502008-03-20 15:15:53 +0100255static inline __sum16
256nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
257 unsigned int dataoff, unsigned int len,
258 u_int8_t protocol, unsigned short family)
259{
260 const struct nf_afinfo *afinfo;
261 __sum16 csum = 0;
262
263 rcu_read_lock();
264 afinfo = nf_get_afinfo(family);
265 if (afinfo)
266 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
267 protocol);
268 rcu_read_unlock();
269 return csum;
270}
271
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800272extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
273extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700274
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800275#include <net/flow.h>
Patrick McHardyc7232c92012-08-26 19:14:06 +0200276extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800277
278static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200279nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800280{
Patrick McHardy051578c2007-12-17 22:42:51 -0800281#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800282 void (*decodefn)(struct sk_buff *, struct flowi *);
283
Patrick McHardyc7232c92012-08-26 19:14:06 +0200284 rcu_read_lock();
285 decodefn = rcu_dereference(nf_nat_decode_session_hook);
286 if (decodefn)
287 decodefn(skb, fl);
288 rcu_read_unlock();
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800289#endif
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#else /* !CONFIG_NETFILTER */
293#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800294#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200295static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700296 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800297 struct net_device *indev,
298 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200299 int (*okfn)(struct sk_buff *), int thresh)
David S. Millerf53b61d2006-01-07 12:50:27 -0800300{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700301 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800302}
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200303static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800304 struct net_device *indev, struct net_device *outdev,
305 int (*okfn)(struct sk_buff *))
306{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800307 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800308}
David S. Millerf53b61d2006-01-07 12:50:27 -0800309struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800310static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200311nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
312{
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif /*CONFIG_NETFILTER*/
315
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700316#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100317extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700318extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100319extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200320
321struct nf_conn;
322struct nlattr;
323
324struct nfq_ct_hook {
325 size_t (*build_size)(const struct nf_conn *ct);
326 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
327 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200328};
329extern struct nfq_ct_hook __rcu *nfq_ct_hook;
330
331struct nfq_ct_nat_hook {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200332 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
333 u32 ctinfo, int off);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200334};
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200335extern struct nfq_ct_nat_hook __rcu *nfq_ct_nat_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*/