Blame view
include/linux/netfilter.h
9.83 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> |
607ca46e9
|
11 |
#include <uapi/linux/netfilter.h> |
1da177e4c
|
12 |
#ifdef CONFIG_NETFILTER |
f615df76e
|
13 14 15 16 |
static inline int NF_DROP_GETERR(int verdict) { return -(verdict >> NF_VERDICT_QBITS); } |
1da177e4c
|
17 |
|
b8beedd25
|
18 19 20 21 22 23 24 25 |
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
|
26 27 28 29 30 31 32 33 34 |
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]; } |
6d11cfdba
|
35 |
extern int netfilter_init(void); |
1da177e4c
|
36 37 38 39 40 |
/* Largest hook number + 1 */ #define NF_MAX_HOOKS 8 struct sk_buff; |
1da177e4c
|
41 42 |
typedef unsigned int nf_hookfn(unsigned int hooknum, |
3db05fea5
|
43 |
struct sk_buff *skb, |
1da177e4c
|
44 45 46 |
const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)); |
d94d9fee9
|
47 |
struct nf_hook_ops { |
1da177e4c
|
48 49 50 51 52 |
struct list_head list; /* User fills in from here down. */ nf_hookfn *hook; struct module *owner; |
76108cea0
|
53 54 |
u_int8_t pf; unsigned int hooknum; |
1da177e4c
|
55 56 57 |
/* Hooks are ordered in ascending priority. */ int priority; }; |
d94d9fee9
|
58 |
struct nf_sockopt_ops { |
1da177e4c
|
59 |
struct list_head list; |
76108cea0
|
60 |
u_int8_t pf; |
1da177e4c
|
61 62 63 64 65 |
/* 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
|
66 |
#ifdef CONFIG_COMPAT |
3fdadf7d2
|
67 68 |
int (*compat_set)(struct sock *sk, int optval, void __user *user, unsigned int len); |
c30f540b6
|
69 |
#endif |
1da177e4c
|
70 71 72 |
int get_optmin; int get_optmax; int (*get)(struct sock *sk, int optval, void __user *user, int *len); |
c30f540b6
|
73 |
#ifdef CONFIG_COMPAT |
3fdadf7d2
|
74 75 |
int (*compat_get)(struct sock *sk, int optval, void __user *user, int *len); |
c30f540b6
|
76 |
#endif |
16fcec35e
|
77 78 |
/* Use the module struct to lock set/get code in place */ struct module *owner; |
1da177e4c
|
79 |
}; |
1da177e4c
|
80 81 82 |
/* Function to register/unregister hook points. */ int nf_register_hook(struct nf_hook_ops *reg); void nf_unregister_hook(struct nf_hook_ops *reg); |
972d1cb14
|
83 84 |
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
|
85 86 87 88 89 |
/* 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); |
7e9c6eeb1
|
90 |
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
1da177e4c
|
91 |
|
a2d7ec58a
|
92 |
#if defined(CONFIG_JUMP_LABEL) |
c5905afb0
|
93 94 |
#include <linux/static_key.h> extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
a2d7ec58a
|
95 96 97 98 |
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook) { if (__builtin_constant_p(pf) && __builtin_constant_p(hook)) |
c5905afb0
|
99 |
return static_key_false(&nf_hooks_needed[pf][hook]); |
a2d7ec58a
|
100 101 102 103 104 105 106 107 108 |
return !list_empty(&nf_hooks[pf][hook]); } #else static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook) { return !list_empty(&nf_hooks[pf][hook]); } #endif |
76108cea0
|
109 |
int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
16a6677fd
|
110 111 112 113 114 115 116 117 118 119 |
struct net_device *indev, struct net_device *outdev, int (*okfn)(struct sk_buff *), int thresh); /** * nf_hook_thresh - call a netfilter hook * * 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
|
120 |
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
3db05fea5
|
121 |
struct sk_buff *skb, |
16a6677fd
|
122 123 |
struct net_device *indev, struct net_device *outdev, |
23f3733d4
|
124 |
int (*okfn)(struct sk_buff *), int thresh) |
16a6677fd
|
125 |
{ |
a2d7ec58a
|
126 127 128 |
if (nf_hooks_active(pf, hook)) return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh); return 1; |
16a6677fd
|
129 |
} |
76108cea0
|
130 |
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
16a6677fd
|
131 132 133 |
struct net_device *indev, struct net_device *outdev, int (*okfn)(struct sk_buff *)) { |
23f3733d4
|
134 |
return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN); |
16a6677fd
|
135 |
} |
1da177e4c
|
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/* 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
|
153 154 155 156 157 158 159 160 161 162 |
static inline int NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb, struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *), int thresh) { int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh); if (ret == 1) ret = okfn(skb); return ret; } |
48d5cad87
|
163 |
|
2249065f4
|
164 165 166 167 168 |
static inline int NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb, struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *), bool cond) { |
4bac6b180
|
169 170 171 |
int ret; if (!cond || |
ac5aa2e33
|
172 |
((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1)) |
2249065f4
|
173 174 175 |
ret = okfn(skb); return ret; } |
1da177e4c
|
176 |
|
2249065f4
|
177 178 179 180 181 182 183 |
static inline int NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb, struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *)) { return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN); } |
1da177e4c
|
184 185 |
/* Call setsockopt() */ |
76108cea0
|
186 |
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
b7058842c
|
187 |
unsigned int len); |
76108cea0
|
188 |
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
1da177e4c
|
189 |
int *len); |
c30f540b6
|
190 |
#ifdef CONFIG_COMPAT |
76108cea0
|
191 |
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, |
b7058842c
|
192 |
char __user *opt, unsigned int len); |
76108cea0
|
193 |
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, |
3fdadf7d2
|
194 |
char __user *opt, int *len); |
c30f540b6
|
195 |
#endif |
3fdadf7d2
|
196 |
|
089af26c7
|
197 198 199 |
/* 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. */ |
37d418792
|
200 |
extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
089af26c7
|
201 |
|
1841a4c7a
|
202 |
struct flowi; |
02f014d88
|
203 |
struct nf_queue_entry; |
c01cd429f
|
204 |
|
bce8032ef
|
205 206 |
struct nf_afinfo { unsigned short family; |
b51655b95
|
207 |
__sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
422c346fa
|
208 |
unsigned int dataoff, u_int8_t protocol); |
d63a65073
|
209 210 211 212 213 |
__sum16 (*checksum_partial)(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, unsigned int len, u_int8_t protocol); |
31ad3dd64
|
214 |
int (*route)(struct net *net, struct dst_entry **dst, |
0fae2e774
|
215 |
struct flowi *fl, bool strict); |
bce8032ef
|
216 |
void (*saveroute)(const struct sk_buff *skb, |
02f014d88
|
217 |
struct nf_queue_entry *entry); |
3db05fea5
|
218 |
int (*reroute)(struct sk_buff *skb, |
02f014d88
|
219 |
const struct nf_queue_entry *entry); |
bce8032ef
|
220 |
int route_key_size; |
2cc7d5730
|
221 |
}; |
0e60ebe04
|
222 |
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO]; |
1e796fda0
|
223 |
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family) |
bce8032ef
|
224 225 226 |
{ return rcu_dereference(nf_afinfo[family]); } |
2cc7d5730
|
227 |
|
b51655b95
|
228 |
static inline __sum16 |
422c346fa
|
229 230 231 |
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, u_int8_t protocol, unsigned short family) { |
1e796fda0
|
232 |
const struct nf_afinfo *afinfo; |
b51655b95
|
233 |
__sum16 csum = 0; |
422c346fa
|
234 235 236 237 238 239 240 241 |
rcu_read_lock(); afinfo = nf_get_afinfo(family); if (afinfo) csum = afinfo->checksum(skb, hook, dataoff, protocol); rcu_read_unlock(); return csum; } |
d63a65073
|
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
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; } |
1e796fda0
|
258 259 |
extern int nf_register_afinfo(const struct nf_afinfo *afinfo); extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo); |
bce8032ef
|
260 |
|
eb9c7ebe6
|
261 |
#include <net/flow.h> |
c7232c997
|
262 |
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
eb9c7ebe6
|
263 264 |
static inline void |
76108cea0
|
265 |
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
eb9c7ebe6
|
266 |
{ |
051578ccb
|
267 |
#ifdef CONFIG_NF_NAT_NEEDED |
eb9c7ebe6
|
268 |
void (*decodefn)(struct sk_buff *, struct flowi *); |
c7232c997
|
269 270 271 272 273 |
rcu_read_lock(); decodefn = rcu_dereference(nf_nat_decode_session_hook); if (decodefn) decodefn(skb, fl); rcu_read_unlock(); |
eb9c7ebe6
|
274 275 |
#endif } |
1da177e4c
|
276 277 |
#else /* !CONFIG_NETFILTER */ #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) |
48d5cad87
|
278 |
#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) |
76108cea0
|
279 |
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
3db05fea5
|
280 |
struct sk_buff *skb, |
f53b61d8c
|
281 282 |
struct net_device *indev, struct net_device *outdev, |
23f3733d4
|
283 |
int (*okfn)(struct sk_buff *), int thresh) |
f53b61d8c
|
284 |
{ |
3db05fea5
|
285 |
return okfn(skb); |
f53b61d8c
|
286 |
} |
76108cea0
|
287 |
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb, |
f53b61d8c
|
288 289 290 |
struct net_device *indev, struct net_device *outdev, int (*okfn)(struct sk_buff *)) { |
9c92d3486
|
291 |
return 1; |
f53b61d8c
|
292 |
} |
f53b61d8c
|
293 |
struct flowi; |
eb9c7ebe6
|
294 |
static inline void |
76108cea0
|
295 296 297 |
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) { } |
1da177e4c
|
298 |
#endif /*CONFIG_NETFILTER*/ |
5f79e0f91
|
299 |
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
312a0c16c
|
300 301 |
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu; extern void nf_ct_attach(struct sk_buff *, const struct sk_buff *); |
0e60ebe04
|
302 |
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu; |
9cb017665
|
303 304 |
struct nf_conn; |
41d73ec05
|
305 |
enum ip_conntrack_info; |
9cb017665
|
306 307 308 309 310 311 |
struct nlattr; struct nfq_ct_hook { size_t (*build_size)(const struct nf_conn *ct); int (*build)(struct sk_buff *skb, struct nf_conn *ct); int (*parse)(const struct nlattr *attr, struct nf_conn *ct); |
bd0779370
|
312 313 |
int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, u32 portid, u32 report); |
8c88f87cb
|
314 |
void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, |
41d73ec05
|
315 |
enum ip_conntrack_info ctinfo, s32 off); |
9cb017665
|
316 |
}; |
41d73ec05
|
317 |
extern struct nfq_ct_hook __rcu *nfq_ct_hook; |
5f79e0f91
|
318 319 320 |
#else static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} #endif |
1da177e4c
|
321 |
#endif /*__LINUX_NETFILTER_H*/ |