1
2#ifndef __LINUX_NETFILTER_H
3#define __LINUX_NETFILTER_H
4
5#include <linux/init.h>
6#include <linux/skbuff.h>
7#include <linux/net.h>
8#include <linux/if.h>
9#include <linux/in.h>
10#include <linux/in6.h>
11#include <linux/wait.h>
12#include <linux/list.h>
13#include <linux/static_key.h>
14#include <linux/netfilter_defs.h>
15#include <linux/netdevice.h>
16#include <net/net_namespace.h>
17
18static inline int NF_DROP_GETERR(int verdict)
19{
20 return -(verdict >> NF_VERDICT_QBITS);
21}
22
23static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
24 const union nf_inet_addr *a2)
25{
26#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
27 const unsigned long *ul1 = (const unsigned long *)a1;
28 const unsigned long *ul2 = (const unsigned long *)a2;
29
30 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
31#else
32 return a1->all[0] == a2->all[0] &&
33 a1->all[1] == a2->all[1] &&
34 a1->all[2] == a2->all[2] &&
35 a1->all[3] == a2->all[3];
36#endif
37}
38
39static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
40 union nf_inet_addr *result,
41 const union nf_inet_addr *mask)
42{
43#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
44 const unsigned long *ua = (const unsigned long *)a1;
45 unsigned long *ur = (unsigned long *)result;
46 const unsigned long *um = (const unsigned long *)mask;
47
48 ur[0] = ua[0] & um[0];
49 ur[1] = ua[1] & um[1];
50#else
51 result->all[0] = a1->all[0] & mask->all[0];
52 result->all[1] = a1->all[1] & mask->all[1];
53 result->all[2] = a1->all[2] & mask->all[2];
54 result->all[3] = a1->all[3] & mask->all[3];
55#endif
56}
57
58int netfilter_init(void);
59
60struct sk_buff;
61
62struct nf_hook_ops;
63
64struct sock;
65
66struct nf_hook_state {
67 unsigned int hook;
68 u_int8_t pf;
69 struct net_device *in;
70 struct net_device *out;
71 struct sock *sk;
72 struct net *net;
73 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
74};
75
76typedef unsigned int nf_hookfn(void *priv,
77 struct sk_buff *skb,
78 const struct nf_hook_state *state);
79struct nf_hook_ops {
80
81 nf_hookfn *hook;
82 struct net_device *dev;
83 void *priv;
84 u_int8_t pf;
85 unsigned int hooknum;
86
87 int priority;
88};
89
90struct nf_hook_entry {
91 nf_hookfn *hook;
92 void *priv;
93};
94
95struct nf_hook_entries_rcu_head {
96 struct rcu_head head;
97 void *allocation;
98};
99
100struct nf_hook_entries {
101 u16 num_hook_entries;
102
103 struct nf_hook_entry hooks[];
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118};
119
120#ifdef CONFIG_NETFILTER
121static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
122{
123 unsigned int n = e->num_hook_entries;
124 const void *hook_end;
125
126 hook_end = &e->hooks[n];
127
128 return (struct nf_hook_ops **)hook_end;
129}
130
131static inline int
132nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
133 struct nf_hook_state *state)
134{
135 return entry->hook(entry->priv, skb, state);
136}
137
138static inline void nf_hook_state_init(struct nf_hook_state *p,
139 unsigned int hook,
140 u_int8_t pf,
141 struct net_device *indev,
142 struct net_device *outdev,
143 struct sock *sk,
144 struct net *net,
145 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
146{
147 p->hook = hook;
148 p->pf = pf;
149 p->in = indev;
150 p->out = outdev;
151 p->sk = sk;
152 p->net = net;
153 p->okfn = okfn;
154}
155
156
157
158struct nf_sockopt_ops {
159 struct list_head list;
160
161 u_int8_t pf;
162
163
164 int set_optmin;
165 int set_optmax;
166 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
167#ifdef CONFIG_COMPAT
168 int (*compat_set)(struct sock *sk, int optval,
169 void __user *user, unsigned int len);
170#endif
171 int get_optmin;
172 int get_optmax;
173 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
174#ifdef CONFIG_COMPAT
175 int (*compat_get)(struct sock *sk, int optval,
176 void __user *user, int *len);
177#endif
178
179 struct module *owner;
180};
181
182
183int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
184void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
185int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
186 unsigned int n);
187void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
188 unsigned int n);
189
190
191
192int nf_register_sockopt(struct nf_sockopt_ops *reg);
193void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
194
195#ifdef CONFIG_JUMP_LABEL
196extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
197#endif
198
199int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
200 const struct nf_hook_entries *e, unsigned int i);
201
202
203
204
205
206
207
208
209static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
210 struct sock *sk, struct sk_buff *skb,
211 struct net_device *indev, struct net_device *outdev,
212 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
213{
214 struct nf_hook_entries *hook_head = NULL;
215 int ret = 1;
216
217#ifdef CONFIG_JUMP_LABEL
218 if (__builtin_constant_p(pf) &&
219 __builtin_constant_p(hook) &&
220 !static_key_false(&nf_hooks_needed[pf][hook]))
221 return 1;
222#endif
223
224 rcu_read_lock();
225 switch (pf) {
226 case NFPROTO_IPV4:
227 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
228 break;
229 case NFPROTO_IPV6:
230 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
231 break;
232 case NFPROTO_ARP:
233#ifdef CONFIG_NETFILTER_FAMILY_ARP
234 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
235 break;
236 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
237#endif
238 break;
239 case NFPROTO_BRIDGE:
240#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
241 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
242#endif
243 break;
244#if IS_ENABLED(CONFIG_DECNET)
245 case NFPROTO_DECNET:
246 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
247 break;
248#endif
249 default:
250 WARN_ON_ONCE(1);
251 break;
252 }
253
254 if (hook_head) {
255 struct nf_hook_state state;
256
257 nf_hook_state_init(&state, hook, pf, indev, outdev,
258 sk, net, okfn);
259
260 ret = nf_hook_slow(skb, &state, hook_head, 0);
261 }
262 rcu_read_unlock();
263
264 return ret;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284static inline int
285NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
286 struct sk_buff *skb, struct net_device *in, struct net_device *out,
287 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
288 bool cond)
289{
290 int ret;
291
292 if (!cond ||
293 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
294 ret = okfn(net, sk, skb);
295 return ret;
296}
297
298static inline int
299NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
300 struct net_device *in, struct net_device *out,
301 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
302{
303 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
304 if (ret == 1)
305 ret = okfn(net, sk, skb);
306 return ret;
307}
308
309static inline void
310NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
311 struct list_head *head, struct net_device *in, struct net_device *out,
312 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
313{
314 struct sk_buff *skb, *next;
315 struct list_head sublist;
316
317 INIT_LIST_HEAD(&sublist);
318 list_for_each_entry_safe(skb, next, head, list) {
319 list_del(&skb->list);
320 if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
321 list_add_tail(&skb->list, &sublist);
322 }
323
324 list_splice(&sublist, head);
325}
326
327
328int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
329 unsigned int len);
330int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
331 int *len);
332#ifdef CONFIG_COMPAT
333int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
334 char __user *opt, unsigned int len);
335int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
336 char __user *opt, int *len);
337#endif
338
339struct flowi;
340struct nf_queue_entry;
341
342__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
343 unsigned int dataoff, u_int8_t protocol,
344 unsigned short family);
345
346__sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
347 unsigned int dataoff, unsigned int len,
348 u_int8_t protocol, unsigned short family);
349int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
350 bool strict, unsigned short family);
351int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
352
353#include <net/flow.h>
354
355struct nf_conn;
356enum nf_nat_manip_type;
357struct nlattr;
358enum ip_conntrack_dir;
359
360struct nf_nat_hook {
361 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
362 const struct nlattr *attr);
363 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
364 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
365 enum nf_nat_manip_type mtype,
366 enum ip_conntrack_dir dir);
367};
368
369extern struct nf_nat_hook __rcu *nf_nat_hook;
370
371static inline void
372nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
373{
374#if IS_ENABLED(CONFIG_NF_NAT)
375 struct nf_nat_hook *nat_hook;
376
377 rcu_read_lock();
378 nat_hook = rcu_dereference(nf_nat_hook);
379 if (nat_hook && nat_hook->decode_session)
380 nat_hook->decode_session(skb, fl);
381 rcu_read_unlock();
382#endif
383}
384
385#else
386static inline int
387NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
388 struct sk_buff *skb, struct net_device *in, struct net_device *out,
389 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
390 bool cond)
391{
392 return okfn(net, sk, skb);
393}
394
395static inline int
396NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
397 struct sk_buff *skb, struct net_device *in, struct net_device *out,
398 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
399{
400 return okfn(net, sk, skb);
401}
402
403static inline void
404NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
405 struct list_head *head, struct net_device *in, struct net_device *out,
406 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
407{
408
409}
410
411static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
412 struct sock *sk, struct sk_buff *skb,
413 struct net_device *indev, struct net_device *outdev,
414 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
415{
416 return 1;
417}
418struct flowi;
419static inline void
420nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
421{
422}
423#endif
424
425#if IS_ENABLED(CONFIG_NF_CONNTRACK)
426#include <linux/netfilter/nf_conntrack_zones_common.h>
427
428extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
429void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
430struct nf_conntrack_tuple;
431bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
432 const struct sk_buff *skb);
433#else
434static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
435struct nf_conntrack_tuple;
436static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
437 const struct sk_buff *skb)
438{
439 return false;
440}
441#endif
442
443struct nf_conn;
444enum ip_conntrack_info;
445
446struct nf_ct_hook {
447 int (*update)(struct net *net, struct sk_buff *skb);
448 void (*destroy)(struct nf_conntrack *);
449 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
450 const struct sk_buff *);
451};
452extern struct nf_ct_hook __rcu *nf_ct_hook;
453
454struct nlattr;
455
456struct nfnl_ct_hook {
457 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
458 enum ip_conntrack_info *ctinfo);
459 size_t (*build_size)(const struct nf_conn *ct);
460 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
461 enum ip_conntrack_info ctinfo,
462 u_int16_t ct_attr, u_int16_t ct_info_attr);
463 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
464 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
465 u32 portid, u32 report);
466 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
467 enum ip_conntrack_info ctinfo, s32 off);
468};
469extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
470
471
472
473
474
475
476
477
478
479
480DECLARE_PER_CPU(bool, nf_skb_duplicated);
481
482#endif
483