]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/netfilter.h
x86 platform drivers: add POWER_SUPPLY to selected drivers for Dell
[mirror_ubuntu-artful-kernel.git] / include / linux / netfilter.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
4#ifdef __KERNEL__
5#include <linux/init.h>
1da177e4
LT
6#include <linux/skbuff.h>
7#include <linux/net.h>
8#include <linux/if.h>
2e3075a2
JE
9#include <linux/in.h>
10#include <linux/in6.h>
1da177e4
LT
11#include <linux/wait.h>
12#include <linux/list.h>
13#endif
c8942f1f 14#include <linux/types.h>
1da177e4 15#include <linux/compiler.h>
bee95250 16#include <linux/sysctl.h>
1da177e4
LT
17
18/* Responses from hook functions. */
19#define NF_DROP 0
20#define NF_ACCEPT 1
21#define NF_STOLEN 2
22#define NF_QUEUE 3
23#define NF_REPEAT 4
24#define NF_STOP 5
25#define NF_MAX_VERDICT NF_STOP
26
0ab43f84 27/* we overload the higher bits for encoding auxiliary data such as the queue
f615df76
FW
28 * number or errno values. Not nice, but better than additional function
29 * arguments. */
30#define NF_VERDICT_MASK 0x000000ff
31
32/* extra verdict flags have mask 0x0000ff00 */
94b27cc3 33#define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
0ab43f84 34
f615df76 35/* queue number (NF_QUEUE) or errno (NF_DROP) */
0ab43f84
HW
36#define NF_VERDICT_QMASK 0xffff0000
37#define NF_VERDICT_QBITS 16
38
f615df76 39#define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
0ab43f84 40
f615df76 41#define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
da683650 42
6869c4d8
HW
43/* only for userspace compatibility */
44#ifndef __KERNEL__
1da177e4
LT
45/* Generic cache responses from hook functions.
46 <= 0x2000 is used for protocol-flags. */
47#define NFC_UNKNOWN 0x4000
48#define NFC_ALTERED 0x8000
f615df76
FW
49
50/* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
51#define NF_VERDICT_BITS 16
6869c4d8 52#endif
1da177e4 53
6e23ae2a
PM
54enum nf_inet_hooks {
55 NF_INET_PRE_ROUTING,
56 NF_INET_LOCAL_IN,
57 NF_INET_FORWARD,
58 NF_INET_LOCAL_OUT,
59 NF_INET_POST_ROUTING,
60 NF_INET_NUMHOOKS
61};
62
7e9c6eeb
JE
63enum {
64 NFPROTO_UNSPEC = 0,
65 NFPROTO_IPV4 = 2,
66 NFPROTO_ARP = 3,
67 NFPROTO_BRIDGE = 7,
68 NFPROTO_IPV6 = 10,
69 NFPROTO_DECNET = 12,
70 NFPROTO_NUMPROTO,
71};
72
643a2c15 73union nf_inet_addr {
7b33ed22 74 __u32 all[4];
643a2c15
JE
75 __be32 ip;
76 __be32 ip6[4];
2e3075a2
JE
77 struct in_addr in;
78 struct in6_addr in6;
643a2c15
JE
79};
80
1da177e4 81#ifdef __KERNEL__
1da177e4 82#ifdef CONFIG_NETFILTER
f615df76
FW
83static inline int NF_DROP_GETERR(int verdict)
84{
85 return -(verdict >> NF_VERDICT_QBITS);
86}
1da177e4 87
b8beedd2
PM
88static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
89 const union nf_inet_addr *a2)
90{
91 return a1->all[0] == a2->all[0] &&
92 a1->all[1] == a2->all[1] &&
93 a1->all[2] == a2->all[2] &&
94 a1->all[3] == a2->all[3];
95}
96
1da177e4
LT
97extern void netfilter_init(void);
98
99/* Largest hook number + 1 */
100#define NF_MAX_HOOKS 8
101
102struct sk_buff;
1da177e4
LT
103
104typedef unsigned int nf_hookfn(unsigned int hooknum,
3db05fea 105 struct sk_buff *skb,
1da177e4
LT
106 const struct net_device *in,
107 const struct net_device *out,
108 int (*okfn)(struct sk_buff *));
109
d94d9fee 110struct nf_hook_ops {
1da177e4
LT
111 struct list_head list;
112
113 /* User fills in from here down. */
114 nf_hookfn *hook;
115 struct module *owner;
76108cea
JE
116 u_int8_t pf;
117 unsigned int hooknum;
1da177e4
LT
118 /* Hooks are ordered in ascending priority. */
119 int priority;
120};
121
d94d9fee 122struct nf_sockopt_ops {
1da177e4
LT
123 struct list_head list;
124
76108cea 125 u_int8_t pf;
1da177e4
LT
126
127 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
128 int set_optmin;
129 int set_optmax;
130 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 131#ifdef CONFIG_COMPAT
3fdadf7d
DM
132 int (*compat_set)(struct sock *sk, int optval,
133 void __user *user, unsigned int len);
c30f540b 134#endif
1da177e4
LT
135 int get_optmin;
136 int get_optmax;
137 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 138#ifdef CONFIG_COMPAT
3fdadf7d
DM
139 int (*compat_get)(struct sock *sk, int optval,
140 void __user *user, int *len);
c30f540b 141#endif
16fcec35
NH
142 /* Use the module struct to lock set/get code in place */
143 struct module *owner;
1da177e4
LT
144};
145
1da177e4
LT
146/* Function to register/unregister hook points. */
147int nf_register_hook(struct nf_hook_ops *reg);
148void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
149int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
150void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
151
152/* Functions to register get/setsockopt ranges (non-inclusive). You
153 need to check permissions yourself! */
154int nf_register_sockopt(struct nf_sockopt_ops *reg);
155void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
156
d62f9ed4
PM
157#ifdef CONFIG_SYSCTL
158/* Sysctl registration */
b3fd3ffe
PE
159extern struct ctl_path nf_net_netfilter_sysctl_path[];
160extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
d62f9ed4
PM
161#endif /* CONFIG_SYSCTL */
162
7e9c6eeb 163extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
1da177e4 164
76108cea 165int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
166 struct net_device *indev, struct net_device *outdev,
167 int (*okfn)(struct sk_buff *), int thresh);
168
169/**
170 * nf_hook_thresh - call a netfilter hook
171 *
172 * Returns 1 if the hook has allowed the packet to pass. The function
173 * okfn must be invoked by the caller in this case. Any other return
174 * value indicates the packet has been consumed by the hook.
175 */
76108cea 176static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea 177 struct sk_buff *skb,
16a6677f
PM
178 struct net_device *indev,
179 struct net_device *outdev,
23f3733d 180 int (*okfn)(struct sk_buff *), int thresh)
16a6677f
PM
181{
182#ifndef CONFIG_NETFILTER_DEBUG
183 if (list_empty(&nf_hooks[pf][hook]))
184 return 1;
185#endif
3db05fea 186 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
16a6677f
PM
187}
188
76108cea 189static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
190 struct net_device *indev, struct net_device *outdev,
191 int (*okfn)(struct sk_buff *))
192{
23f3733d 193 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
16a6677f 194}
1da177e4
LT
195
196/* Activate hook; either okfn or kfree_skb called, unless a hook
197 returns NF_STOLEN (in which case, it's up to the hook to deal with
198 the consequences).
199
200 Returns -ERRNO if packet dropped. Zero means queued, stolen or
201 accepted.
202*/
203
204/* RR:
205 > I don't want nf_hook to return anything because people might forget
206 > about async and trust the return value to mean "packet was ok".
207
208 AK:
209 Just document it clearly, then you can expect some sense from kernel
210 coders :)
211*/
212
2249065f
JE
213static inline int
214NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
215 struct net_device *in, struct net_device *out,
216 int (*okfn)(struct sk_buff *), int thresh)
217{
218 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
219 if (ret == 1)
220 ret = okfn(skb);
221 return ret;
222}
48d5cad8 223
2249065f
JE
224static inline int
225NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
226 struct net_device *in, struct net_device *out,
227 int (*okfn)(struct sk_buff *), bool cond)
228{
4bac6b18
PM
229 int ret;
230
231 if (!cond ||
ac5aa2e3 232 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
2249065f
JE
233 ret = okfn(skb);
234 return ret;
235}
1da177e4 236
2249065f
JE
237static inline int
238NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
239 struct net_device *in, struct net_device *out,
240 int (*okfn)(struct sk_buff *))
241{
242 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
243}
1da177e4
LT
244
245/* Call setsockopt() */
76108cea 246int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 247 unsigned int len);
76108cea 248int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 249 int *len);
c30f540b 250#ifdef CONFIG_COMPAT
76108cea 251int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 252 char __user *opt, unsigned int len);
76108cea 253int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 254 char __user *opt, int *len);
c30f540b 255#endif
3fdadf7d 256
089af26c
HW
257/* Call this before modifying an existing packet: ensures it is
258 modifiable and linear to the point you care about (writable_len).
259 Returns true or false. */
37d41879 260extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 261
1841a4c7 262struct flowi;
02f014d8 263struct nf_queue_entry;
c01cd429 264
bce8032e
PM
265struct nf_afinfo {
266 unsigned short family;
b51655b9 267 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 268 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
269 __sum16 (*checksum_partial)(struct sk_buff *skb,
270 unsigned int hook,
271 unsigned int dataoff,
272 unsigned int len,
273 u_int8_t protocol);
31ad3dd6 274 int (*route)(struct net *net, struct dst_entry **dst,
0fae2e77 275 struct flowi *fl, bool strict);
bce8032e 276 void (*saveroute)(const struct sk_buff *skb,
02f014d8 277 struct nf_queue_entry *entry);
3db05fea 278 int (*reroute)(struct sk_buff *skb,
02f014d8 279 const struct nf_queue_entry *entry);
bce8032e 280 int route_key_size;
2cc7d573
HW
281};
282
0e60ebe0 283extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 284static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
285{
286 return rcu_dereference(nf_afinfo[family]);
287}
2cc7d573 288
b51655b9 289static inline __sum16
422c346f
PM
290nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
291 u_int8_t protocol, unsigned short family)
292{
1e796fda 293 const struct nf_afinfo *afinfo;
b51655b9 294 __sum16 csum = 0;
422c346f
PM
295
296 rcu_read_lock();
297 afinfo = nf_get_afinfo(family);
298 if (afinfo)
299 csum = afinfo->checksum(skb, hook, dataoff, protocol);
300 rcu_read_unlock();
301 return csum;
302}
303
d63a6507
PM
304static inline __sum16
305nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
306 unsigned int dataoff, unsigned int len,
307 u_int8_t protocol, unsigned short family)
308{
309 const struct nf_afinfo *afinfo;
310 __sum16 csum = 0;
311
312 rcu_read_lock();
313 afinfo = nf_get_afinfo(family);
314 if (afinfo)
315 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
316 protocol);
317 rcu_read_unlock();
318 return csum;
319}
320
1e796fda
PM
321extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
322extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 323
eb9c7ebe
PM
324#include <net/flow.h>
325extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
326
327static inline void
76108cea 328nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 329{
051578cc 330#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
331 void (*decodefn)(struct sk_buff *, struct flowi *);
332
051578cc
PM
333 if (family == AF_INET) {
334 rcu_read_lock();
335 decodefn = rcu_dereference(ip_nat_decode_session);
336 if (decodefn)
337 decodefn(skb, fl);
338 rcu_read_unlock();
339 }
eb9c7ebe
PM
340#endif
341}
342
608c8e4f
HW
343#ifdef CONFIG_PROC_FS
344#include <linux/proc_fs.h>
345extern struct proc_dir_entry *proc_net_netfilter;
346#endif
347
1da177e4
LT
348#else /* !CONFIG_NETFILTER */
349#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
48d5cad8 350#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
76108cea 351static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea 352 struct sk_buff *skb,
f53b61d8
DM
353 struct net_device *indev,
354 struct net_device *outdev,
23f3733d 355 int (*okfn)(struct sk_buff *), int thresh)
f53b61d8 356{
3db05fea 357 return okfn(skb);
f53b61d8 358}
76108cea 359static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
f53b61d8
DM
360 struct net_device *indev, struct net_device *outdev,
361 int (*okfn)(struct sk_buff *))
362{
9c92d348 363 return 1;
f53b61d8 364}
f53b61d8 365struct flowi;
eb9c7ebe 366static inline void
76108cea
JE
367nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
368{
369}
1da177e4
LT
370#endif /*CONFIG_NETFILTER*/
371
5f79e0f9 372#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
0e60ebe0 373extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
5f79e0f9 374extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
0e60ebe0 375extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
5f79e0f9
YK
376#else
377static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
378#endif
379
1da177e4
LT
380#endif /*__KERNEL__*/
381#endif /*__LINUX_NETFILTER_H*/