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