Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 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 | |
Mark Charlebois | 066c680 | 2014-02-11 19:26:05 -0800 | [diff] [blame] | 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 | |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 16 | #define xt_alloc_initial_table(type, typ2) ({ \ |
| 17 | unsigned int hook_mask = info->valid_hooks; \ |
| 18 | unsigned int nhooks = hweight32(hook_mask); \ |
| 19 | unsigned int bytes = 0, hooknum = 0, i = 0; \ |
| 20 | struct { \ |
| 21 | struct type##_replace repl; \ |
Mark Charlebois | 066c680 | 2014-02-11 19:26:05 -0800 | [diff] [blame] | 22 | struct type##_standard entries[]; \ |
| 23 | } *tbl; \ |
| 24 | struct type##_error *term; \ |
| 25 | size_t term_offset = (offsetof(typeof(*tbl), entries[nhooks]) + \ |
| 26 | __alignof__(*term) - 1) & ~(__alignof__(*term) - 1); \ |
| 27 | tbl = kzalloc(term_offset + sizeof(*term), GFP_KERNEL); \ |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 28 | if (tbl == NULL) \ |
| 29 | return NULL; \ |
Mark Charlebois | 066c680 | 2014-02-11 19:26:05 -0800 | [diff] [blame] | 30 | term = (struct type##_error *)&(((char *)tbl)[term_offset]); \ |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 31 | strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \ |
Mark Charlebois | 066c680 | 2014-02-11 19:26:05 -0800 | [diff] [blame] | 32 | *term = (struct type##_error)typ2##_ERROR_INIT; \ |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 33 | tbl->repl.valid_hooks = hook_mask; \ |
| 34 | tbl->repl.num_entries = nhooks + 1; \ |
| 35 | tbl->repl.size = nhooks * sizeof(struct type##_standard) + \ |
Mark Charlebois | 066c680 | 2014-02-11 19:26:05 -0800 | [diff] [blame] | 36 | sizeof(struct type##_error); \ |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 37 | for (; hook_mask != 0; hook_mask >>= 1, ++hooknum) { \ |
| 38 | if (!(hook_mask & 1)) \ |
| 39 | continue; \ |
| 40 | tbl->repl.hook_entry[hooknum] = bytes; \ |
| 41 | tbl->repl.underflow[hooknum] = bytes; \ |
| 42 | tbl->entries[i++] = (struct type##_standard) \ |
| 43 | typ2##_STANDARD_INIT(NF_ACCEPT); \ |
| 44 | bytes += sizeof(struct type##_standard); \ |
| 45 | } \ |
| 46 | tbl; \ |
| 47 | }) |