Blame view
include/linux/netfilter.h
12.2 KB
1da177e4c
|
1 2 |
#ifndef __LINUX_NETFILTER_H #define __LINUX_NETFILTER_H |
1da177e4c
|
3 |
#include <linux/init.h> |
1da177e4c
|
4 5 6 |
#include <linux/skbuff.h> #include <linux/net.h> #include <linux/if.h> |
2e3075a2c
|
7 8 |
#include <linux/in.h> #include <linux/in6.h> |
1da177e4c
|
9 10 |
#include <linux/wait.h> #include <linux/list.h> |
d1c85c2eb
|
11 |
#include <linux/static_key.h> |
a263653ed
|
12 |
#include <linux/netfilter_defs.h> |
085db2c04
|
13 14 |
#include <linux/netdevice.h> #include <net/net_namespace.h> |
a263653ed
|
15 |
|
1da177e4c
|
16 |
#ifdef CONFIG_NETFILTER |
f615df76e
|
17 18 19 20 |
static inline int NF_DROP_GETERR(int verdict) { return -(verdict >> NF_VERDICT_QBITS); } |
1da177e4c
|
21 |
|
b8beedd25
|
22 23 24 25 26 27 28 29 |
static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *a2) { return a1->all[0] == a2->all[0] && a1->all[1] == a2->all[1] && a1->all[2] == a2->all[2] && a1->all[3] == a2->all[3]; } |
efdedd542
|
30 31 32 33 34 35 36 37 38 |
static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, union nf_inet_addr *result, const union nf_inet_addr *mask) { result->all[0] = a1->all[0] & mask->all[0]; result->all[1] = a1->all[1] & mask->all[1]; result->all[2] = a1->all[2] & mask->all[2]; result->all[3] = a1->all[3] & mask->all[3]; } |
a0f4ecf34
|
39 |
int netfilter_init(void); |
1da177e4c
|
40 |
|
1da177e4c
|
41 |
struct sk_buff; |
1da177e4c
|
42 |
|
795aa6ef6
|
43 |
struct nf_hook_ops; |
cfdfab314
|
44 |
|
1c984f8a5
|
45 |
struct sock; |
cfdfab314
|
46 47 48 49 50 51 |
struct nf_hook_state { unsigned int hook; int thresh; u_int8_t pf; struct net_device *in; struct net_device *out; |
1c984f8a5
|
52 |
struct sock *sk; |
b11b1f652
|
53 |
struct net *net; |
f71914834
|
54 |
struct list_head *hook_list; |
0c4b51f00
|
55 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *); |
cfdfab314
|
56 |
}; |
107a9f4dc
|
57 |
static inline void nf_hook_state_init(struct nf_hook_state *p, |
f71914834
|
58 |
struct list_head *hook_list, |
107a9f4dc
|
59 60 61 62 |
unsigned int hook, int thresh, u_int8_t pf, struct net_device *indev, struct net_device *outdev, |
1c984f8a5
|
63 |
struct sock *sk, |
b11b1f652
|
64 |
struct net *net, |
0c4b51f00
|
65 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
107a9f4dc
|
66 67 68 69 70 71 |
{ p->hook = hook; p->thresh = thresh; p->pf = pf; p->in = indev; p->out = outdev; |
1c984f8a5
|
72 |
p->sk = sk; |
b11b1f652
|
73 |
p->net = net; |
f71914834
|
74 |
p->hook_list = hook_list; |
107a9f4dc
|
75 76 |
p->okfn = okfn; } |
06198b34a
|
77 |
typedef unsigned int nf_hookfn(void *priv, |
3db05fea5
|
78 |
struct sk_buff *skb, |
238e54c9c
|
79 |
const struct nf_hook_state *state); |
1da177e4c
|
80 |
|
d94d9fee9
|
81 |
struct nf_hook_ops { |
87d5c18ce
|
82 |
struct list_head list; |
1da177e4c
|
83 84 |
/* User fills in from here down. */ |
87d5c18ce
|
85 |
nf_hookfn *hook; |
e687ad60a
|
86 |
struct net_device *dev; |
87d5c18ce
|
87 88 89 |
void *priv; u_int8_t pf; unsigned int hooknum; |
1da177e4c
|
90 |
/* Hooks are ordered in ascending priority. */ |
87d5c18ce
|
91 |
int priority; |
1da177e4c
|
92 |
}; |
d94d9fee9
|
93 |
struct nf_sockopt_ops { |
1da177e4c
|
94 |
struct list_head list; |
76108cea0
|
95 |
u_int8_t pf; |
1da177e4c
|
96 97 98 99 100 |
/* Non-inclusive ranges: use 0/0/NULL to never get called. */ int set_optmin; int set_optmax; int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); |
c30f540b6
|
101 |
#ifdef CONFIG_COMPAT |
3fdadf7d2
|
102 103 |
int (*compat_set)(struct sock *sk, int optval, void __user *user, unsigned int len); |
c30f540b6
|
104 |
#endif |
1da177e4c
|
105 106 107 |
int get_optmin; int get_optmax; int (*get)(struct sock *sk, int optval, void __user *user, int *len); |
c30f540b6
|
108 |
#ifdef CONFIG_COMPAT |
3fdadf7d2
|
109 110 |
int (*compat_get)(struct sock *sk, int optval, void __user *user, int *len); |
c30f540b6
|
111 |
#endif |
16fcec35e
|
112 113 |
/* Use the module struct to lock set/get code in place */ struct module *owner; |
1da177e4c
|
114 |
}; |
1da177e4c
|
115 |
/* Function to register/unregister hook points. */ |
085db2c04
|
116 117 118 119 120 121 |
int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops); void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops); int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, unsigned int n); void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, unsigned int n); |
1da177e4c
|
122 123 |
int nf_register_hook(struct nf_hook_ops *reg); void nf_unregister_hook(struct nf_hook_ops *reg); |
972d1cb14
|
124 125 |
int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n); void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n); |
1da177e4c
|
126 127 128 129 130 |
/* Functions to register get/setsockopt ranges (non-inclusive). You need to check permissions yourself! */ int nf_register_sockopt(struct nf_sockopt_ops *reg); void nf_unregister_sockopt(struct nf_sockopt_ops *reg); |
d1c85c2eb
|
131 |
#ifdef HAVE_JUMP_LABEL |
c5905afb0
|
132 |
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
d1c85c2eb
|
133 |
|
3bbd14e0a
|
134 |
static inline bool nf_hook_list_active(struct list_head *hook_list, |
b8d0aad0c
|
135 |
u_int8_t pf, unsigned int hook) |
a2d7ec58a
|
136 137 138 |
{ if (__builtin_constant_p(pf) && __builtin_constant_p(hook)) |
c5905afb0
|
139 |
return static_key_false(&nf_hooks_needed[pf][hook]); |
a2d7ec58a
|
140 |
|
3bbd14e0a
|
141 |
return !list_empty(hook_list); |
a2d7ec58a
|
142 143 |
} #else |
3bbd14e0a
|
144 |
static inline bool nf_hook_list_active(struct list_head *hook_list, |
b8d0aad0c
|
145 |
u_int8_t pf, unsigned int hook) |
a2d7ec58a
|
146 |
{ |
3bbd14e0a
|
147 |
return !list_empty(hook_list); |
a2d7ec58a
|
148 149 |
} #endif |
cfdfab314
|
150 |
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state); |
16a6677fd
|
151 152 153 |
/** * nf_hook_thresh - call a netfilter hook |
b8d0aad0c
|
154 |
* |
16a6677fd
|
155 156 157 158 |
* Returns 1 if the hook has allowed the packet to pass. The function * okfn must be invoked by the caller in this case. Any other return * value indicates the packet has been consumed by the hook. */ |
76108cea0
|
159 |
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
7a7735044
|
160 |
struct net *net, |
7026b1ddb
|
161 |
struct sock *sk, |
3db05fea5
|
162 |
struct sk_buff *skb, |
16a6677fd
|
163 164 |
struct net_device *indev, struct net_device *outdev, |
0c4b51f00
|
165 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
7026b1ddb
|
166 |
int thresh) |
16a6677fd
|
167 |
{ |
3bbd14e0a
|
168 |
struct list_head *hook_list = &net->nf.hooks[pf][hook]; |
70aa99660
|
169 |
|
3bbd14e0a
|
170 |
if (nf_hook_list_active(hook_list, pf, hook)) { |
107a9f4dc
|
171 |
struct nf_hook_state state; |
cfdfab314
|
172 |
|
3bbd14e0a
|
173 |
nf_hook_state_init(&state, hook_list, hook, thresh, |
b11b1f652
|
174 |
pf, indev, outdev, sk, net, okfn); |
cfdfab314
|
175 176 |
return nf_hook_slow(skb, &state); } |
a2d7ec58a
|
177 |
return 1; |
16a6677fd
|
178 |
} |
29a26a568
|
179 180 181 |
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, struct net_device *indev, struct net_device *outdev, |
0c4b51f00
|
182 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
16a6677fd
|
183 |
{ |
7a7735044
|
184 |
return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN); |
16a6677fd
|
185 |
} |
1da177e4c
|
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
/* Activate hook; either okfn or kfree_skb called, unless a hook returns NF_STOLEN (in which case, it's up to the hook to deal with the consequences). Returns -ERRNO if packet dropped. Zero means queued, stolen or accepted. */ /* RR: > I don't want nf_hook to return anything because people might forget > about async and trust the return value to mean "packet was ok". AK: Just document it clearly, then you can expect some sense from kernel coders :) */ |
2249065f4
|
203 |
static inline int |
29a26a568
|
204 |
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
7026b1ddb
|
205 206 |
struct sk_buff *skb, struct net_device *in, struct net_device *out, |
0c4b51f00
|
207 208 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *), int thresh) |
2249065f4
|
209 |
{ |
7a7735044
|
210 |
int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh); |
2249065f4
|
211 |
if (ret == 1) |
0c4b51f00
|
212 |
ret = okfn(net, sk, skb); |
2249065f4
|
213 214 |
return ret; } |
48d5cad87
|
215 |
|
2249065f4
|
216 |
static inline int |
29a26a568
|
217 |
NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
7026b1ddb
|
218 |
struct sk_buff *skb, struct net_device *in, struct net_device *out, |
0c4b51f00
|
219 220 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *), bool cond) |
2249065f4
|
221 |
{ |
4bac6b180
|
222 223 224 |
int ret; if (!cond || |
7a7735044
|
225 |
((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1)) |
0c4b51f00
|
226 |
ret = okfn(net, sk, skb); |
2249065f4
|
227 228 |
return ret; } |
1da177e4c
|
229 |
|
2249065f4
|
230 |
static inline int |
29a26a568
|
231 |
NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, |
2249065f4
|
232 |
struct net_device *in, struct net_device *out, |
0c4b51f00
|
233 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
2249065f4
|
234 |
{ |
29a26a568
|
235 |
return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN); |
2249065f4
|
236 |
} |
1da177e4c
|
237 238 |
/* Call setsockopt() */ |
76108cea0
|
239 |
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
b7058842c
|
240 |
unsigned int len); |
76108cea0
|
241 |
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
1da177e4c
|
242 |
int *len); |
c30f540b6
|
243 |
#ifdef CONFIG_COMPAT |
76108cea0
|
244 |
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, |
b7058842c
|
245 |
char __user *opt, unsigned int len); |
76108cea0
|
246 |
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, |
3fdadf7d2
|
247 |
char __user *opt, int *len); |
c30f540b6
|
248 |
#endif |
3fdadf7d2
|
249 |
|
089af26c7
|
250 251 252 |
/* Call this before modifying an existing packet: ensures it is modifiable and linear to the point you care about (writable_len). Returns true or false. */ |
a0f4ecf34
|
253 |
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
089af26c7
|
254 |
|
1841a4c7a
|
255 |
struct flowi; |
02f014d88
|
256 |
struct nf_queue_entry; |
c01cd429f
|
257 |
|
bce8032ef
|
258 259 |
struct nf_afinfo { unsigned short family; |
b51655b95
|
260 |
__sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
422c346fa
|
261 |
unsigned int dataoff, u_int8_t protocol); |
d63a65073
|
262 263 264 265 266 |
__sum16 (*checksum_partial)(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, unsigned int len, u_int8_t protocol); |
31ad3dd64
|
267 |
int (*route)(struct net *net, struct dst_entry **dst, |
0fae2e774
|
268 |
struct flowi *fl, bool strict); |
bce8032ef
|
269 |
void (*saveroute)(const struct sk_buff *skb, |
02f014d88
|
270 |
struct nf_queue_entry *entry); |
d815d90bb
|
271 |
int (*reroute)(struct net *net, struct sk_buff *skb, |
02f014d88
|
272 |
const struct nf_queue_entry *entry); |
bce8032ef
|
273 |
int route_key_size; |
2cc7d5730
|
274 |
}; |
0e60ebe04
|
275 |
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO]; |
1e796fda0
|
276 |
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family) |
bce8032ef
|
277 278 279 |
{ return rcu_dereference(nf_afinfo[family]); } |
2cc7d5730
|
280 |
|
b51655b95
|
281 |
static inline __sum16 |
422c346fa
|
282 283 284 |
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, u_int8_t protocol, unsigned short family) { |
1e796fda0
|
285 |
const struct nf_afinfo *afinfo; |
b51655b95
|
286 |
__sum16 csum = 0; |
422c346fa
|
287 288 289 290 291 292 293 294 |
rcu_read_lock(); afinfo = nf_get_afinfo(family); if (afinfo) csum = afinfo->checksum(skb, hook, dataoff, protocol); rcu_read_unlock(); return csum; } |
d63a65073
|
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
static inline __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, unsigned int len, u_int8_t protocol, unsigned short family) { const struct nf_afinfo *afinfo; __sum16 csum = 0; rcu_read_lock(); afinfo = nf_get_afinfo(family); if (afinfo) csum = afinfo->checksum_partial(skb, hook, dataoff, len, protocol); rcu_read_unlock(); return csum; } |
a0f4ecf34
|
311 312 |
int nf_register_afinfo(const struct nf_afinfo *afinfo); void nf_unregister_afinfo(const struct nf_afinfo *afinfo); |
bce8032ef
|
313 |
|
eb9c7ebe6
|
314 |
#include <net/flow.h> |
c7232c997
|
315 |
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
eb9c7ebe6
|
316 317 |
static inline void |
76108cea0
|
318 |
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
eb9c7ebe6
|
319 |
{ |
051578ccb
|
320 |
#ifdef CONFIG_NF_NAT_NEEDED |
eb9c7ebe6
|
321 |
void (*decodefn)(struct sk_buff *, struct flowi *); |
c7232c997
|
322 323 324 325 326 |
rcu_read_lock(); decodefn = rcu_dereference(nf_nat_decode_session_hook); if (decodefn) decodefn(skb, fl); rcu_read_unlock(); |
eb9c7ebe6
|
327 328 |
#endif } |
1da177e4c
|
329 |
#else /* !CONFIG_NETFILTER */ |
008027c31
|
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
static inline int NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, struct net_device *in, struct net_device *out, int (*okfn)(struct net *, struct sock *, struct sk_buff *), bool cond) { return okfn(net, sk, skb); } static inline int NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, struct net_device *in, struct net_device *out, int (*okfn)(struct net *, struct sock *, struct sk_buff *)) { return okfn(net, sk, skb); } |
29a26a568
|
346 347 348 |
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, struct net_device *indev, struct net_device *outdev, |
0c4b51f00
|
349 |
int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
f53b61d8c
|
350 |
{ |
9c92d3486
|
351 |
return 1; |
f53b61d8c
|
352 |
} |
f53b61d8c
|
353 |
struct flowi; |
eb9c7ebe6
|
354 |
static inline void |
76108cea0
|
355 356 357 |
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) { } |
1da177e4c
|
358 |
#endif /*CONFIG_NETFILTER*/ |
5f79e0f91
|
359 |
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
62da98656
|
360 |
#include <linux/netfilter/nf_conntrack_zones_common.h> |
312a0c16c
|
361 |
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu; |
a0f4ecf34
|
362 |
void nf_ct_attach(struct sk_buff *, const struct sk_buff *); |
0e60ebe04
|
363 |
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu; |
b7bd1809e
|
364 365 366 |
#else static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} #endif |
9cb017665
|
367 368 |
struct nf_conn; |
41d73ec05
|
369 |
enum ip_conntrack_info; |
9cb017665
|
370 |
struct nlattr; |
a4b4766c3
|
371 |
struct nfnl_ct_hook { |
224a05975
|
372 |
struct nf_conn *(*get_ct)(const struct sk_buff *skb, |
b7bd1809e
|
373 |
enum ip_conntrack_info *ctinfo); |
9cb017665
|
374 |
size_t (*build_size)(const struct nf_conn *ct); |
b7bd1809e
|
375 376 377 |
int (*build)(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo, u_int16_t ct_attr, u_int16_t ct_info_attr); |
9cb017665
|
378 |
int (*parse)(const struct nlattr *attr, struct nf_conn *ct); |
bd0779370
|
379 380 |
int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, u32 portid, u32 report); |
8c88f87cb
|
381 |
void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, |
41d73ec05
|
382 |
enum ip_conntrack_info ctinfo, s32 off); |
9cb017665
|
383 |
}; |
a4b4766c3
|
384 |
extern struct nfnl_ct_hook __rcu *nfnl_ct_hook; |
5f79e0f91
|
385 |
|
e7c8899f3
|
386 387 388 389 390 391 392 393 394 395 |
/** * nf_skb_duplicated - TEE target has sent a packet * * When a xtables target sends a packet, the OUTPUT and POSTROUTING * hooks are traversed again, i.e. nft and xtables are invoked recursively. * * This is used by xtables TEE target to prevent the duplicated skb from * being duplicated again. */ DECLARE_PER_CPU(bool, nf_skb_duplicated); |
1da177e4c
|
396 |
#endif /*__LINUX_NETFILTER_H*/ |