1
2#ifndef _X_TABLES_H
3#define _X_TABLES_H
4
5
6#include <linux/netdevice.h>
7#include <linux/static_key.h>
8#include <linux/netfilter.h>
9#include <uapi/linux/netfilter/x_tables.h>
10
11
12#define NF_INVF(ptr, flag, boolean) \
13 ((boolean) ^ !!((ptr)->invflags & (flag)))
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30struct xt_action_param {
31 union {
32 const struct xt_match *match;
33 const struct xt_target *target;
34 };
35 union {
36 const void *matchinfo, *targinfo;
37 };
38 const struct nf_hook_state *state;
39 int fragoff;
40 unsigned int thoff;
41 bool hotdrop;
42};
43
44static inline struct net *xt_net(const struct xt_action_param *par)
45{
46 return par->state->net;
47}
48
49static inline struct net_device *xt_in(const struct xt_action_param *par)
50{
51 return par->state->in;
52}
53
54static inline const char *xt_inname(const struct xt_action_param *par)
55{
56 return par->state->in->name;
57}
58
59static inline struct net_device *xt_out(const struct xt_action_param *par)
60{
61 return par->state->out;
62}
63
64static inline const char *xt_outname(const struct xt_action_param *par)
65{
66 return par->state->out->name;
67}
68
69static inline unsigned int xt_hooknum(const struct xt_action_param *par)
70{
71 return par->state->hook;
72}
73
74static inline u_int8_t xt_family(const struct xt_action_param *par)
75{
76 return par->state->pf;
77}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92struct xt_mtchk_param {
93 struct net *net;
94 const char *table;
95 const void *entryinfo;
96 const struct xt_match *match;
97 void *matchinfo;
98 unsigned int hook_mask;
99 u_int8_t family;
100 bool nft_compat;
101};
102
103
104
105
106
107struct xt_mtdtor_param {
108 struct net *net;
109 const struct xt_match *match;
110 void *matchinfo;
111 u_int8_t family;
112};
113
114
115
116
117
118
119
120
121
122
123struct xt_tgchk_param {
124 struct net *net;
125 const char *table;
126 const void *entryinfo;
127 const struct xt_target *target;
128 void *targinfo;
129 unsigned int hook_mask;
130 u_int8_t family;
131 bool nft_compat;
132};
133
134
135struct xt_tgdtor_param {
136 struct net *net;
137 const struct xt_target *target;
138 void *targinfo;
139 u_int8_t family;
140};
141
142struct xt_match {
143 struct list_head list;
144
145 const char name[XT_EXTENSION_MAXNAMELEN];
146 u_int8_t revision;
147
148
149
150
151
152
153 bool (*match)(const struct sk_buff *skb,
154 struct xt_action_param *);
155
156
157 int (*checkentry)(const struct xt_mtchk_param *);
158
159
160 void (*destroy)(const struct xt_mtdtor_param *);
161#ifdef CONFIG_COMPAT
162
163 void (*compat_from_user)(void *dst, const void *src);
164 int (*compat_to_user)(void __user *dst, const void *src);
165#endif
166
167 struct module *me;
168
169 const char *table;
170 unsigned int matchsize;
171 unsigned int usersize;
172#ifdef CONFIG_COMPAT
173 unsigned int compatsize;
174#endif
175 unsigned int hooks;
176 unsigned short proto;
177
178 unsigned short family;
179};
180
181
182struct xt_target {
183 struct list_head list;
184
185 const char name[XT_EXTENSION_MAXNAMELEN];
186 u_int8_t revision;
187
188
189
190
191 unsigned int (*target)(struct sk_buff *skb,
192 const struct xt_action_param *);
193
194
195
196
197
198 int (*checkentry)(const struct xt_tgchk_param *);
199
200
201 void (*destroy)(const struct xt_tgdtor_param *);
202#ifdef CONFIG_COMPAT
203
204 void (*compat_from_user)(void *dst, const void *src);
205 int (*compat_to_user)(void __user *dst, const void *src);
206#endif
207
208 struct module *me;
209
210 const char *table;
211 unsigned int targetsize;
212 unsigned int usersize;
213#ifdef CONFIG_COMPAT
214 unsigned int compatsize;
215#endif
216 unsigned int hooks;
217 unsigned short proto;
218
219 unsigned short family;
220};
221
222
223struct xt_table {
224 struct list_head list;
225
226
227 unsigned int valid_hooks;
228
229
230 struct xt_table_info *private;
231
232
233 struct module *me;
234
235 u_int8_t af;
236 int priority;
237
238
239 int (*table_init)(struct net *net);
240
241
242 const char name[XT_TABLE_MAXNAMELEN];
243};
244
245#include <linux/netfilter_ipv4.h>
246
247
248struct xt_table_info {
249
250 unsigned int size;
251
252 unsigned int number;
253
254 unsigned int initial_entries;
255
256
257 unsigned int hook_entry[NF_INET_NUMHOOKS];
258 unsigned int underflow[NF_INET_NUMHOOKS];
259
260
261
262
263
264 unsigned int stacksize;
265 void ***jumpstack;
266
267 unsigned char entries[0] __aligned(8);
268};
269
270int xt_register_target(struct xt_target *target);
271void xt_unregister_target(struct xt_target *target);
272int xt_register_targets(struct xt_target *target, unsigned int n);
273void xt_unregister_targets(struct xt_target *target, unsigned int n);
274
275int xt_register_match(struct xt_match *target);
276void xt_unregister_match(struct xt_match *target);
277int xt_register_matches(struct xt_match *match, unsigned int n);
278void xt_unregister_matches(struct xt_match *match, unsigned int n);
279
280int xt_check_entry_offsets(const void *base, const char *elems,
281 unsigned int target_offset,
282 unsigned int next_offset);
283
284int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks);
285
286unsigned int *xt_alloc_entry_offsets(unsigned int size);
287bool xt_find_jump_offset(const unsigned int *offsets,
288 unsigned int target, unsigned int size);
289
290int xt_check_proc_name(const char *name, unsigned int size);
291
292int xt_check_match(struct xt_mtchk_param *, unsigned int size, u16 proto,
293 bool inv_proto);
294int xt_check_target(struct xt_tgchk_param *, unsigned int size, u16 proto,
295 bool inv_proto);
296
297int xt_match_to_user(const struct xt_entry_match *m,
298 struct xt_entry_match __user *u);
299int xt_target_to_user(const struct xt_entry_target *t,
300 struct xt_entry_target __user *u);
301int xt_data_to_user(void __user *dst, const void *src,
302 int usersize, int size, int aligned_size);
303
304void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
305 struct xt_counters_info *info, bool compat);
306struct xt_counters *xt_counters_alloc(unsigned int counters);
307
308struct xt_table *xt_register_table(struct net *net,
309 const struct xt_table *table,
310 struct xt_table_info *bootstrap,
311 struct xt_table_info *newinfo);
312void *xt_unregister_table(struct xt_table *table);
313
314struct xt_table_info *xt_replace_table(struct xt_table *table,
315 unsigned int num_counters,
316 struct xt_table_info *newinfo,
317 int *error);
318
319struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
320struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
321struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
322int xt_find_revision(u8 af, const char *name, u8 revision, int target,
323 int *err);
324
325struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
326 const char *name);
327struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
328 const char *name);
329void xt_table_unlock(struct xt_table *t);
330
331int xt_proto_init(struct net *net, u_int8_t af);
332void xt_proto_fini(struct net *net, u_int8_t af);
333
334struct xt_table_info *xt_alloc_table_info(unsigned int size);
335void xt_free_table_info(struct xt_table_info *info);
336
337
338
339
340
341
342
343
344
345DECLARE_PER_CPU(seqcount_t, xt_recseq);
346
347
348
349
350
351extern struct static_key xt_tee_enabled;
352
353
354
355
356
357
358
359
360
361
362
363static inline unsigned int xt_write_recseq_begin(void)
364{
365 unsigned int addend;
366
367
368
369
370
371 addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
372
373
374
375
376
377
378 __this_cpu_add(xt_recseq.sequence, addend);
379 smp_wmb();
380
381 return addend;
382}
383
384
385
386
387
388
389
390
391
392static inline void xt_write_recseq_end(unsigned int addend)
393{
394
395 smp_wmb();
396 __this_cpu_add(xt_recseq.sequence, addend);
397}
398
399
400
401
402static inline unsigned long ifname_compare_aligned(const char *_a,
403 const char *_b,
404 const char *_mask)
405{
406 const unsigned long *a = (const unsigned long *)_a;
407 const unsigned long *b = (const unsigned long *)_b;
408 const unsigned long *mask = (const unsigned long *)_mask;
409 unsigned long ret;
410
411 ret = (a[0] ^ b[0]) & mask[0];
412 if (IFNAMSIZ > sizeof(unsigned long))
413 ret |= (a[1] ^ b[1]) & mask[1];
414 if (IFNAMSIZ > 2 * sizeof(unsigned long))
415 ret |= (a[2] ^ b[2]) & mask[2];
416 if (IFNAMSIZ > 3 * sizeof(unsigned long))
417 ret |= (a[3] ^ b[3]) & mask[3];
418 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
419 return ret;
420}
421
422struct xt_percpu_counter_alloc_state {
423 unsigned int off;
424 const char __percpu *mem;
425};
426
427bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
428 struct xt_counters *counter);
429void xt_percpu_counter_free(struct xt_counters *cnt);
430
431static inline struct xt_counters *
432xt_get_this_cpu_counter(struct xt_counters *cnt)
433{
434 if (nr_cpu_ids > 1)
435 return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
436
437 return cnt;
438}
439
440static inline struct xt_counters *
441xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
442{
443 if (nr_cpu_ids > 1)
444 return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
445
446 return cnt;
447}
448
449struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
450
451#ifdef CONFIG_COMPAT
452#include <net/compat.h>
453
454struct compat_xt_entry_match {
455 union {
456 struct {
457 u_int16_t match_size;
458 char name[XT_FUNCTION_MAXNAMELEN - 1];
459 u_int8_t revision;
460 } user;
461 struct {
462 u_int16_t match_size;
463 compat_uptr_t match;
464 } kernel;
465 u_int16_t match_size;
466 } u;
467 unsigned char data[0];
468};
469
470struct compat_xt_entry_target {
471 union {
472 struct {
473 u_int16_t target_size;
474 char name[XT_FUNCTION_MAXNAMELEN - 1];
475 u_int8_t revision;
476 } user;
477 struct {
478 u_int16_t target_size;
479 compat_uptr_t target;
480 } kernel;
481 u_int16_t target_size;
482 } u;
483 unsigned char data[0];
484};
485
486
487
488
489
490struct compat_xt_counters {
491 compat_u64 pcnt, bcnt;
492};
493
494struct compat_xt_counters_info {
495 char name[XT_TABLE_MAXNAMELEN];
496 compat_uint_t num_counters;
497 struct compat_xt_counters counters[0];
498};
499
500struct _compat_xt_align {
501 __u8 u8;
502 __u16 u16;
503 __u32 u32;
504 compat_u64 u64;
505};
506
507#define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
508
509void xt_compat_lock(u_int8_t af);
510void xt_compat_unlock(u_int8_t af);
511
512int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
513void xt_compat_flush_offsets(u_int8_t af);
514int xt_compat_init_offsets(u8 af, unsigned int number);
515int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
516
517int xt_compat_match_offset(const struct xt_match *match);
518void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
519 unsigned int *size);
520int xt_compat_match_to_user(const struct xt_entry_match *m,
521 void __user **dstptr, unsigned int *size);
522
523int xt_compat_target_offset(const struct xt_target *target);
524void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
525 unsigned int *size);
526int xt_compat_target_to_user(const struct xt_entry_target *t,
527 void __user **dstptr, unsigned int *size);
528int xt_compat_check_entry_offsets(const void *base, const char *elems,
529 unsigned int target_offset,
530 unsigned int next_offset);
531
532#endif
533#endif
534