]> Git Repo - linux.git/blob - include/linux/netfilter/x_tables.h
lib: radix-tree: check accounting of existing slot replacement users
[linux.git] / include / linux / netfilter / x_tables.h
1 #ifndef _X_TABLES_H
2 #define _X_TABLES_H
3
4
5 #include <linux/netdevice.h>
6 #include <linux/static_key.h>
7 #include <linux/netfilter.h>
8 #include <uapi/linux/netfilter/x_tables.h>
9
10 /* Test a struct->invflags and a boolean for inequality */
11 #define NF_INVF(ptr, flag, boolean)                                     \
12         ((boolean) ^ !!((ptr)->invflags & (flag)))
13
14 /**
15  * struct xt_action_param - parameters for matches/targets
16  *
17  * @match:      the match extension
18  * @target:     the target extension
19  * @matchinfo:  per-match data
20  * @targetinfo: per-target data
21  * @state:      pointer to hook state this packet came from
22  * @fragoff:    packet is a fragment, this is the data offset
23  * @thoff:      position of transport header relative to skb->data
24  *
25  * Fields written to by extensions:
26  *
27  * @hotdrop:    drop packet if we had inspection problems
28  */
29 struct xt_action_param {
30         union {
31                 const struct xt_match *match;
32                 const struct xt_target *target;
33         };
34         union {
35                 const void *matchinfo, *targinfo;
36         };
37         const struct nf_hook_state *state;
38         int fragoff;
39         unsigned int thoff;
40         bool hotdrop;
41 };
42
43 static inline struct net *xt_net(const struct xt_action_param *par)
44 {
45         return par->state->net;
46 }
47
48 static inline struct net_device *xt_in(const struct xt_action_param *par)
49 {
50         return par->state->in;
51 }
52
53 static inline const char *xt_inname(const struct xt_action_param *par)
54 {
55         return par->state->in->name;
56 }
57
58 static inline struct net_device *xt_out(const struct xt_action_param *par)
59 {
60         return par->state->out;
61 }
62
63 static inline const char *xt_outname(const struct xt_action_param *par)
64 {
65         return par->state->out->name;
66 }
67
68 static inline unsigned int xt_hooknum(const struct xt_action_param *par)
69 {
70         return par->state->hook;
71 }
72
73 static inline u_int8_t xt_family(const struct xt_action_param *par)
74 {
75         return par->state->pf;
76 }
77
78 /**
79  * struct xt_mtchk_param - parameters for match extensions'
80  * checkentry functions
81  *
82  * @net:        network namespace through which the check was invoked
83  * @table:      table the rule is tried to be inserted into
84  * @entryinfo:  the family-specific rule data
85  *              (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
86  * @match:      struct xt_match through which this function was invoked
87  * @matchinfo:  per-match data
88  * @hook_mask:  via which hooks the new rule is reachable
89  * Other fields as above.
90  */
91 struct xt_mtchk_param {
92         struct net *net;
93         const char *table;
94         const void *entryinfo;
95         const struct xt_match *match;
96         void *matchinfo;
97         unsigned int hook_mask;
98         u_int8_t family;
99         bool nft_compat;
100 };
101
102 /**
103  * struct xt_mdtor_param - match destructor parameters
104  * Fields as above.
105  */
106 struct xt_mtdtor_param {
107         struct net *net;
108         const struct xt_match *match;
109         void *matchinfo;
110         u_int8_t family;
111 };
112
113 /**
114  * struct xt_tgchk_param - parameters for target extensions'
115  * checkentry functions
116  *
117  * @entryinfo:  the family-specific rule data
118  *              (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
119  *
120  * Other fields see above.
121  */
122 struct xt_tgchk_param {
123         struct net *net;
124         const char *table;
125         const void *entryinfo;
126         const struct xt_target *target;
127         void *targinfo;
128         unsigned int hook_mask;
129         u_int8_t family;
130         bool nft_compat;
131 };
132
133 /* Target destructor parameters */
134 struct xt_tgdtor_param {
135         struct net *net;
136         const struct xt_target *target;
137         void *targinfo;
138         u_int8_t family;
139 };
140
141 struct xt_match {
142         struct list_head list;
143
144         const char name[XT_EXTENSION_MAXNAMELEN];
145         u_int8_t revision;
146
147         /* Return true or false: return FALSE and set *hotdrop = 1 to
148            force immediate packet drop. */
149         /* Arguments changed since 2.6.9, as this must now handle
150            non-linear skb, using skb_header_pointer and
151            skb_ip_make_writable. */
152         bool (*match)(const struct sk_buff *skb,
153                       struct xt_action_param *);
154
155         /* Called when user tries to insert an entry of this type. */
156         int (*checkentry)(const struct xt_mtchk_param *);
157
158         /* Called when entry of this type deleted. */
159         void (*destroy)(const struct xt_mtdtor_param *);
160 #ifdef CONFIG_COMPAT
161         /* Called when userspace align differs from kernel space one */
162         void (*compat_from_user)(void *dst, const void *src);
163         int (*compat_to_user)(void __user *dst, const void *src);
164 #endif
165         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
166         struct module *me;
167
168         const char *table;
169         unsigned int matchsize;
170 #ifdef CONFIG_COMPAT
171         unsigned int compatsize;
172 #endif
173         unsigned int hooks;
174         unsigned short proto;
175
176         unsigned short family;
177 };
178
179 /* Registration hooks for targets. */
180 struct xt_target {
181         struct list_head list;
182
183         const char name[XT_EXTENSION_MAXNAMELEN];
184         u_int8_t revision;
185
186         /* Returns verdict. Argument order changed since 2.6.9, as this
187            must now handle non-linear skbs, using skb_copy_bits and
188            skb_ip_make_writable. */
189         unsigned int (*target)(struct sk_buff *skb,
190                                const struct xt_action_param *);
191
192         /* Called when user tries to insert an entry of this type:
193            hook_mask is a bitmask of hooks from which it can be
194            called. */
195         /* Should return 0 on success or an error code otherwise (-Exxxx). */
196         int (*checkentry)(const struct xt_tgchk_param *);
197
198         /* Called when entry of this type deleted. */
199         void (*destroy)(const struct xt_tgdtor_param *);
200 #ifdef CONFIG_COMPAT
201         /* Called when userspace align differs from kernel space one */
202         void (*compat_from_user)(void *dst, const void *src);
203         int (*compat_to_user)(void __user *dst, const void *src);
204 #endif
205         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
206         struct module *me;
207
208         const char *table;
209         unsigned int targetsize;
210 #ifdef CONFIG_COMPAT
211         unsigned int compatsize;
212 #endif
213         unsigned int hooks;
214         unsigned short proto;
215
216         unsigned short family;
217 };
218
219 /* Furniture shopping... */
220 struct xt_table {
221         struct list_head list;
222
223         /* What hooks you will enter on */
224         unsigned int valid_hooks;
225
226         /* Man behind the curtain... */
227         struct xt_table_info *private;
228
229         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
230         struct module *me;
231
232         u_int8_t af;            /* address/protocol family */
233         int priority;           /* hook order */
234
235         /* called when table is needed in the given netns */
236         int (*table_init)(struct net *net);
237
238         /* A unique name... */
239         const char name[XT_TABLE_MAXNAMELEN];
240 };
241
242 #include <linux/netfilter_ipv4.h>
243
244 /* The table itself */
245 struct xt_table_info {
246         /* Size per table */
247         unsigned int size;
248         /* Number of entries: FIXME. --RR */
249         unsigned int number;
250         /* Initial number of entries. Needed for module usage count */
251         unsigned int initial_entries;
252
253         /* Entry points and underflows */
254         unsigned int hook_entry[NF_INET_NUMHOOKS];
255         unsigned int underflow[NF_INET_NUMHOOKS];
256
257         /*
258          * Number of user chains. Since tables cannot have loops, at most
259          * @stacksize jumps (number of user chains) can possibly be made.
260          */
261         unsigned int stacksize;
262         void ***jumpstack;
263
264         unsigned char entries[0] __aligned(8);
265 };
266
267 int xt_register_target(struct xt_target *target);
268 void xt_unregister_target(struct xt_target *target);
269 int xt_register_targets(struct xt_target *target, unsigned int n);
270 void xt_unregister_targets(struct xt_target *target, unsigned int n);
271
272 int xt_register_match(struct xt_match *target);
273 void xt_unregister_match(struct xt_match *target);
274 int xt_register_matches(struct xt_match *match, unsigned int n);
275 void xt_unregister_matches(struct xt_match *match, unsigned int n);
276
277 int xt_check_entry_offsets(const void *base, const char *elems,
278                            unsigned int target_offset,
279                            unsigned int next_offset);
280
281 unsigned int *xt_alloc_entry_offsets(unsigned int size);
282 bool xt_find_jump_offset(const unsigned int *offsets,
283                          unsigned int target, unsigned int size);
284
285 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
286                    bool inv_proto);
287 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
288                     bool inv_proto);
289
290 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
291                                  struct xt_counters_info *info, bool compat);
292
293 struct xt_table *xt_register_table(struct net *net,
294                                    const struct xt_table *table,
295                                    struct xt_table_info *bootstrap,
296                                    struct xt_table_info *newinfo);
297 void *xt_unregister_table(struct xt_table *table);
298
299 struct xt_table_info *xt_replace_table(struct xt_table *table,
300                                        unsigned int num_counters,
301                                        struct xt_table_info *newinfo,
302                                        int *error);
303
304 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
305 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
306 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
307 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
308 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
309                      int *err);
310
311 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
312                                     const char *name);
313 void xt_table_unlock(struct xt_table *t);
314
315 int xt_proto_init(struct net *net, u_int8_t af);
316 void xt_proto_fini(struct net *net, u_int8_t af);
317
318 struct xt_table_info *xt_alloc_table_info(unsigned int size);
319 void xt_free_table_info(struct xt_table_info *info);
320
321 /**
322  * xt_recseq - recursive seqcount for netfilter use
323  * 
324  * Packet processing changes the seqcount only if no recursion happened
325  * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
326  * because we use the normal seqcount convention :
327  * Low order bit set to 1 if a writer is active.
328  */
329 DECLARE_PER_CPU(seqcount_t, xt_recseq);
330
331 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
332  *
333  * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
334  */
335 extern struct static_key xt_tee_enabled;
336
337 /**
338  * xt_write_recseq_begin - start of a write section
339  *
340  * Begin packet processing : all readers must wait the end
341  * 1) Must be called with preemption disabled
342  * 2) softirqs must be disabled too (or we should use this_cpu_add())
343  * Returns :
344  *  1 if no recursion on this cpu
345  *  0 if recursion detected
346  */
347 static inline unsigned int xt_write_recseq_begin(void)
348 {
349         unsigned int addend;
350
351         /*
352          * Low order bit of sequence is set if we already
353          * called xt_write_recseq_begin().
354          */
355         addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
356
357         /*
358          * This is kind of a write_seqcount_begin(), but addend is 0 or 1
359          * We dont check addend value to avoid a test and conditional jump,
360          * since addend is most likely 1
361          */
362         __this_cpu_add(xt_recseq.sequence, addend);
363         smp_wmb();
364
365         return addend;
366 }
367
368 /**
369  * xt_write_recseq_end - end of a write section
370  * @addend: return value from previous xt_write_recseq_begin()
371  *
372  * End packet processing : all readers can proceed
373  * 1) Must be called with preemption disabled
374  * 2) softirqs must be disabled too (or we should use this_cpu_add())
375  */
376 static inline void xt_write_recseq_end(unsigned int addend)
377 {
378         /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
379         smp_wmb();
380         __this_cpu_add(xt_recseq.sequence, addend);
381 }
382
383 /*
384  * This helper is performance critical and must be inlined
385  */
386 static inline unsigned long ifname_compare_aligned(const char *_a,
387                                                    const char *_b,
388                                                    const char *_mask)
389 {
390         const unsigned long *a = (const unsigned long *)_a;
391         const unsigned long *b = (const unsigned long *)_b;
392         const unsigned long *mask = (const unsigned long *)_mask;
393         unsigned long ret;
394
395         ret = (a[0] ^ b[0]) & mask[0];
396         if (IFNAMSIZ > sizeof(unsigned long))
397                 ret |= (a[1] ^ b[1]) & mask[1];
398         if (IFNAMSIZ > 2 * sizeof(unsigned long))
399                 ret |= (a[2] ^ b[2]) & mask[2];
400         if (IFNAMSIZ > 3 * sizeof(unsigned long))
401                 ret |= (a[3] ^ b[3]) & mask[3];
402         BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
403         return ret;
404 }
405
406 struct xt_percpu_counter_alloc_state {
407         unsigned int off;
408         const char __percpu *mem;
409 };
410
411 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
412                              struct xt_counters *counter);
413 void xt_percpu_counter_free(struct xt_counters *cnt);
414
415 static inline struct xt_counters *
416 xt_get_this_cpu_counter(struct xt_counters *cnt)
417 {
418         if (nr_cpu_ids > 1)
419                 return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
420
421         return cnt;
422 }
423
424 static inline struct xt_counters *
425 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
426 {
427         if (nr_cpu_ids > 1)
428                 return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
429
430         return cnt;
431 }
432
433 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
434
435 #ifdef CONFIG_COMPAT
436 #include <net/compat.h>
437
438 struct compat_xt_entry_match {
439         union {
440                 struct {
441                         u_int16_t match_size;
442                         char name[XT_FUNCTION_MAXNAMELEN - 1];
443                         u_int8_t revision;
444                 } user;
445                 struct {
446                         u_int16_t match_size;
447                         compat_uptr_t match;
448                 } kernel;
449                 u_int16_t match_size;
450         } u;
451         unsigned char data[0];
452 };
453
454 struct compat_xt_entry_target {
455         union {
456                 struct {
457                         u_int16_t target_size;
458                         char name[XT_FUNCTION_MAXNAMELEN - 1];
459                         u_int8_t revision;
460                 } user;
461                 struct {
462                         u_int16_t target_size;
463                         compat_uptr_t target;
464                 } kernel;
465                 u_int16_t target_size;
466         } u;
467         unsigned char data[0];
468 };
469
470 /* FIXME: this works only on 32 bit tasks
471  * need to change whole approach in order to calculate align as function of
472  * current task alignment */
473
474 struct compat_xt_counters {
475         compat_u64 pcnt, bcnt;                  /* Packet and byte counters */
476 };
477
478 struct compat_xt_counters_info {
479         char name[XT_TABLE_MAXNAMELEN];
480         compat_uint_t num_counters;
481         struct compat_xt_counters counters[0];
482 };
483
484 struct _compat_xt_align {
485         __u8 u8;
486         __u16 u16;
487         __u32 u32;
488         compat_u64 u64;
489 };
490
491 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
492
493 void xt_compat_lock(u_int8_t af);
494 void xt_compat_unlock(u_int8_t af);
495
496 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
497 void xt_compat_flush_offsets(u_int8_t af);
498 void xt_compat_init_offsets(u_int8_t af, unsigned int number);
499 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
500
501 int xt_compat_match_offset(const struct xt_match *match);
502 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
503                               unsigned int *size);
504 int xt_compat_match_to_user(const struct xt_entry_match *m,
505                             void __user **dstptr, unsigned int *size);
506
507 int xt_compat_target_offset(const struct xt_target *target);
508 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
509                                 unsigned int *size);
510 int xt_compat_target_to_user(const struct xt_entry_target *t,
511                              void __user **dstptr, unsigned int *size);
512 int xt_compat_check_entry_offsets(const void *base, const char *elems,
513                                   unsigned int target_offset,
514                                   unsigned int next_offset);
515
516 #endif /* CONFIG_COMPAT */
517 #endif /* _X_TABLES_H */
This page took 0.05556 seconds and 4 git commands to generate.