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