xt_repldata.h 1.6 KB

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