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