xt_repldata.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Today's hack: quantum tunneling in structs
  4. *
  5. * 'entries' and 'term' are never anywhere referenced by word in code. In fact,
  6. * they serve as the hanging-off data accessed through repl.data[].
  7. */
  8. /* tbl has the following structure equivalent, but is C99 compliant:
  9. * struct {
  10. * struct type##_replace repl;
  11. * struct type##_standard entries[nhooks];
  12. * struct type##_error term;
  13. * } *tbl;
  14. */
  15. #define xt_alloc_initial_table(type, typ2) ({ \
  16. unsigned int hook_mask = info->valid_hooks; \
  17. unsigned int nhooks = hweight32(hook_mask); \
  18. unsigned int bytes = 0, hooknum = 0, i = 0; \
  19. struct { \
  20. struct type##_replace repl; \
  21. struct type##_standard entries[]; \
  22. } *tbl; \
  23. struct type##_error *term; \
  24. size_t term_offset = (offsetof(typeof(*tbl), entries[nhooks]) + \
  25. __alignof__(*term) - 1) & ~(__alignof__(*term) - 1); \
  26. tbl = kzalloc(term_offset + sizeof(*term), GFP_KERNEL); \
  27. if (tbl == NULL) \
  28. return NULL; \
  29. term = (struct type##_error *)&(((char *)tbl)[term_offset]); \
  30. strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
  31. *term = (struct type##_error)typ2##_ERROR_INIT; \
  32. tbl->repl.valid_hooks = hook_mask; \
  33. tbl->repl.num_entries = nhooks + 1; \
  34. tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
  35. sizeof(struct type##_error); \
  36. for (; hook_mask != 0; hook_mask >>= 1, ++hooknum) { \
  37. if (!(hook_mask & 1)) \
  38. continue; \
  39. tbl->repl.hook_entry[hooknum] = bytes; \
  40. tbl->repl.underflow[hooknum] = bytes; \
  41. tbl->entries[i++] = (struct type##_standard) \
  42. typ2##_STANDARD_INIT(NF_ACCEPT); \
  43. bytes += sizeof(struct type##_standard); \
  44. } \
  45. tbl; \
  46. })