0001
0002 #ifndef _X_TABLES_H
0003 #define _X_TABLES_H
0004
0005
0006 #include <linux/netdevice.h>
0007 #include <linux/static_key.h>
0008 #include <linux/netfilter.h>
0009 #include <uapi/linux/netfilter/x_tables.h>
0010
0011
0012 #define NF_INVF(ptr, flag, boolean) \
0013 ((boolean) ^ !!((ptr)->invflags & (flag)))
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 struct xt_action_param {
0031 union {
0032 const struct xt_match *match;
0033 const struct xt_target *target;
0034 };
0035 union {
0036 const void *matchinfo, *targinfo;
0037 };
0038 const struct nf_hook_state *state;
0039 unsigned int thoff;
0040 u16 fragoff;
0041 bool hotdrop;
0042 };
0043
0044 static inline struct net *xt_net(const struct xt_action_param *par)
0045 {
0046 return par->state->net;
0047 }
0048
0049 static inline struct net_device *xt_in(const struct xt_action_param *par)
0050 {
0051 return par->state->in;
0052 }
0053
0054 static inline const char *xt_inname(const struct xt_action_param *par)
0055 {
0056 return par->state->in->name;
0057 }
0058
0059 static inline struct net_device *xt_out(const struct xt_action_param *par)
0060 {
0061 return par->state->out;
0062 }
0063
0064 static inline const char *xt_outname(const struct xt_action_param *par)
0065 {
0066 return par->state->out->name;
0067 }
0068
0069 static inline unsigned int xt_hooknum(const struct xt_action_param *par)
0070 {
0071 return par->state->hook;
0072 }
0073
0074 static inline u_int8_t xt_family(const struct xt_action_param *par)
0075 {
0076 return par->state->pf;
0077 }
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 struct xt_mtchk_param {
0093 struct net *net;
0094 const char *table;
0095 const void *entryinfo;
0096 const struct xt_match *match;
0097 void *matchinfo;
0098 unsigned int hook_mask;
0099 u_int8_t family;
0100 bool nft_compat;
0101 };
0102
0103
0104
0105
0106
0107 struct xt_mtdtor_param {
0108 struct net *net;
0109 const struct xt_match *match;
0110 void *matchinfo;
0111 u_int8_t family;
0112 };
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 struct xt_tgchk_param {
0124 struct net *net;
0125 const char *table;
0126 const void *entryinfo;
0127 const struct xt_target *target;
0128 void *targinfo;
0129 unsigned int hook_mask;
0130 u_int8_t family;
0131 bool nft_compat;
0132 };
0133
0134
0135 struct xt_tgdtor_param {
0136 struct net *net;
0137 const struct xt_target *target;
0138 void *targinfo;
0139 u_int8_t family;
0140 };
0141
0142 struct xt_match {
0143 struct list_head list;
0144
0145 const char name[XT_EXTENSION_MAXNAMELEN];
0146 u_int8_t revision;
0147
0148
0149
0150
0151
0152
0153 bool (*match)(const struct sk_buff *skb,
0154 struct xt_action_param *);
0155
0156
0157 int (*checkentry)(const struct xt_mtchk_param *);
0158
0159
0160 void (*destroy)(const struct xt_mtdtor_param *);
0161 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
0162
0163 void (*compat_from_user)(void *dst, const void *src);
0164 int (*compat_to_user)(void __user *dst, const void *src);
0165 #endif
0166
0167 struct module *me;
0168
0169 const char *table;
0170 unsigned int matchsize;
0171 unsigned int usersize;
0172 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
0173 unsigned int compatsize;
0174 #endif
0175 unsigned int hooks;
0176 unsigned short proto;
0177
0178 unsigned short family;
0179 };
0180
0181
0182 struct xt_target {
0183 struct list_head list;
0184
0185 const char name[XT_EXTENSION_MAXNAMELEN];
0186 u_int8_t revision;
0187
0188
0189
0190
0191 unsigned int (*target)(struct sk_buff *skb,
0192 const struct xt_action_param *);
0193
0194
0195
0196
0197
0198 int (*checkentry)(const struct xt_tgchk_param *);
0199
0200
0201 void (*destroy)(const struct xt_tgdtor_param *);
0202 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
0203
0204 void (*compat_from_user)(void *dst, const void *src);
0205 int (*compat_to_user)(void __user *dst, const void *src);
0206 #endif
0207
0208 struct module *me;
0209
0210 const char *table;
0211 unsigned int targetsize;
0212 unsigned int usersize;
0213 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
0214 unsigned int compatsize;
0215 #endif
0216 unsigned int hooks;
0217 unsigned short proto;
0218
0219 unsigned short family;
0220 };
0221
0222
0223 struct xt_table {
0224 struct list_head list;
0225
0226
0227 unsigned int valid_hooks;
0228
0229
0230 struct xt_table_info *private;
0231
0232
0233 struct nf_hook_ops *ops;
0234
0235
0236 struct module *me;
0237
0238 u_int8_t af;
0239 int priority;
0240
0241
0242 const char name[XT_TABLE_MAXNAMELEN];
0243 };
0244
0245 #include <linux/netfilter_ipv4.h>
0246
0247
0248 struct xt_table_info {
0249
0250 unsigned int size;
0251
0252 unsigned int number;
0253
0254 unsigned int initial_entries;
0255
0256
0257 unsigned int hook_entry[NF_INET_NUMHOOKS];
0258 unsigned int underflow[NF_INET_NUMHOOKS];
0259
0260
0261
0262
0263
0264 unsigned int stacksize;
0265 void ***jumpstack;
0266
0267 unsigned char entries[] __aligned(8);
0268 };
0269
0270 int xt_register_target(struct xt_target *target);
0271 void xt_unregister_target(struct xt_target *target);
0272 int xt_register_targets(struct xt_target *target, unsigned int n);
0273 void xt_unregister_targets(struct xt_target *target, unsigned int n);
0274
0275 int xt_register_match(struct xt_match *target);
0276 void xt_unregister_match(struct xt_match *target);
0277 int xt_register_matches(struct xt_match *match, unsigned int n);
0278 void xt_unregister_matches(struct xt_match *match, unsigned int n);
0279
0280 int xt_check_entry_offsets(const void *base, const char *elems,
0281 unsigned int target_offset,
0282 unsigned int next_offset);
0283
0284 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks);
0285
0286 unsigned int *xt_alloc_entry_offsets(unsigned int size);
0287 bool xt_find_jump_offset(const unsigned int *offsets,
0288 unsigned int target, unsigned int size);
0289
0290 int xt_check_proc_name(const char *name, unsigned int size);
0291
0292 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u16 proto,
0293 bool inv_proto);
0294 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u16 proto,
0295 bool inv_proto);
0296
0297 int xt_match_to_user(const struct xt_entry_match *m,
0298 struct xt_entry_match __user *u);
0299 int xt_target_to_user(const struct xt_entry_target *t,
0300 struct xt_entry_target __user *u);
0301 int xt_data_to_user(void __user *dst, const void *src,
0302 int usersize, int size, int aligned_size);
0303
0304 void *xt_copy_counters(sockptr_t arg, unsigned int len,
0305 struct xt_counters_info *info);
0306 struct xt_counters *xt_counters_alloc(unsigned int counters);
0307
0308 struct xt_table *xt_register_table(struct net *net,
0309 const struct xt_table *table,
0310 struct xt_table_info *bootstrap,
0311 struct xt_table_info *newinfo);
0312 void *xt_unregister_table(struct xt_table *table);
0313
0314 struct xt_table_info *xt_replace_table(struct xt_table *table,
0315 unsigned int num_counters,
0316 struct xt_table_info *newinfo,
0317 int *error);
0318
0319 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
0320 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
0321 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
0322 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
0323 int *err);
0324
0325 struct xt_table *xt_find_table(struct net *net, u8 af, const char *name);
0326 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
0327 const char *name);
0328 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
0329 const char *name);
0330 void xt_table_unlock(struct xt_table *t);
0331
0332 int xt_proto_init(struct net *net, u_int8_t af);
0333 void xt_proto_fini(struct net *net, u_int8_t af);
0334
0335 struct xt_table_info *xt_alloc_table_info(unsigned int size);
0336 void xt_free_table_info(struct xt_table_info *info);
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 DECLARE_PER_CPU(seqcount_t, xt_recseq);
0347
0348
0349
0350
0351
0352 extern struct static_key xt_tee_enabled;
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 static inline unsigned int xt_write_recseq_begin(void)
0365 {
0366 unsigned int addend;
0367
0368
0369
0370
0371
0372 addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
0373
0374
0375
0376
0377
0378
0379 __this_cpu_add(xt_recseq.sequence, addend);
0380 smp_mb();
0381
0382 return addend;
0383 }
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 static inline void xt_write_recseq_end(unsigned int addend)
0394 {
0395
0396 smp_wmb();
0397 __this_cpu_add(xt_recseq.sequence, addend);
0398 }
0399
0400
0401
0402
0403 static inline unsigned long ifname_compare_aligned(const char *_a,
0404 const char *_b,
0405 const char *_mask)
0406 {
0407 const unsigned long *a = (const unsigned long *)_a;
0408 const unsigned long *b = (const unsigned long *)_b;
0409 const unsigned long *mask = (const unsigned long *)_mask;
0410 unsigned long ret;
0411
0412 ret = (a[0] ^ b[0]) & mask[0];
0413 if (IFNAMSIZ > sizeof(unsigned long))
0414 ret |= (a[1] ^ b[1]) & mask[1];
0415 if (IFNAMSIZ > 2 * sizeof(unsigned long))
0416 ret |= (a[2] ^ b[2]) & mask[2];
0417 if (IFNAMSIZ > 3 * sizeof(unsigned long))
0418 ret |= (a[3] ^ b[3]) & mask[3];
0419 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
0420 return ret;
0421 }
0422
0423 struct xt_percpu_counter_alloc_state {
0424 unsigned int off;
0425 const char __percpu *mem;
0426 };
0427
0428 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
0429 struct xt_counters *counter);
0430 void xt_percpu_counter_free(struct xt_counters *cnt);
0431
0432 static inline struct xt_counters *
0433 xt_get_this_cpu_counter(struct xt_counters *cnt)
0434 {
0435 if (nr_cpu_ids > 1)
0436 return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
0437
0438 return cnt;
0439 }
0440
0441 static inline struct xt_counters *
0442 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
0443 {
0444 if (nr_cpu_ids > 1)
0445 return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
0446
0447 return cnt;
0448 }
0449
0450 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
0451
0452 int xt_register_template(const struct xt_table *t, int(*table_init)(struct net *net));
0453 void xt_unregister_template(const struct xt_table *t);
0454
0455 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
0456 #include <net/compat.h>
0457
0458 struct compat_xt_entry_match {
0459 union {
0460 struct {
0461 u_int16_t match_size;
0462 char name[XT_FUNCTION_MAXNAMELEN - 1];
0463 u_int8_t revision;
0464 } user;
0465 struct {
0466 u_int16_t match_size;
0467 compat_uptr_t match;
0468 } kernel;
0469 u_int16_t match_size;
0470 } u;
0471 unsigned char data[];
0472 };
0473
0474 struct compat_xt_entry_target {
0475 union {
0476 struct {
0477 u_int16_t target_size;
0478 char name[XT_FUNCTION_MAXNAMELEN - 1];
0479 u_int8_t revision;
0480 } user;
0481 struct {
0482 u_int16_t target_size;
0483 compat_uptr_t target;
0484 } kernel;
0485 u_int16_t target_size;
0486 } u;
0487 unsigned char data[];
0488 };
0489
0490
0491
0492
0493
0494 struct compat_xt_counters {
0495 compat_u64 pcnt, bcnt;
0496 };
0497
0498 struct compat_xt_counters_info {
0499 char name[XT_TABLE_MAXNAMELEN];
0500 compat_uint_t num_counters;
0501 struct compat_xt_counters counters[];
0502 };
0503
0504 struct _compat_xt_align {
0505 __u8 u8;
0506 __u16 u16;
0507 __u32 u32;
0508 compat_u64 u64;
0509 };
0510
0511 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
0512
0513 void xt_compat_lock(u_int8_t af);
0514 void xt_compat_unlock(u_int8_t af);
0515
0516 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
0517 void xt_compat_flush_offsets(u_int8_t af);
0518 int xt_compat_init_offsets(u8 af, unsigned int number);
0519 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
0520
0521 int xt_compat_match_offset(const struct xt_match *match);
0522 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
0523 unsigned int *size);
0524 int xt_compat_match_to_user(const struct xt_entry_match *m,
0525 void __user **dstptr, unsigned int *size);
0526
0527 int xt_compat_target_offset(const struct xt_target *target);
0528 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
0529 unsigned int *size);
0530 int xt_compat_target_to_user(const struct xt_entry_target *t,
0531 void __user **dstptr, unsigned int *size);
0532 int xt_compat_check_entry_offsets(const void *base, const char *elems,
0533 unsigned int target_offset,
0534 unsigned int next_offset);
0535
0536 #endif
0537 #endif