blob: 8fd324116e6f127e63205339f2bc9345a26d74ea [file] [log] [blame]
Jan Engelhardte3eaa992009-06-17 22:14:54 +02001/*
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 Charlebois066c6802014-02-11 19:26:05 -08008/* 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 Engelhardte3eaa992009-06-17 22:14:54 +020016#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 Charlebois066c6802014-02-11 19:26:05 -080022 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 Engelhardte3eaa992009-06-17 22:14:54 +020028 if (tbl == NULL) \
29 return NULL; \
Mark Charlebois066c6802014-02-11 19:26:05 -080030 term = (struct type##_error *)&(((char *)tbl)[term_offset]); \
Jan Engelhardte3eaa992009-06-17 22:14:54 +020031 strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
Mark Charlebois066c6802014-02-11 19:26:05 -080032 *term = (struct type##_error)typ2##_ERROR_INIT; \
Jan Engelhardte3eaa992009-06-17 22:14:54 +020033 tbl->repl.valid_hooks = hook_mask; \
34 tbl->repl.num_entries = nhooks + 1; \
35 tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
Mark Charlebois066c6802014-02-11 19:26:05 -080036 sizeof(struct type##_error); \
Jan Engelhardte3eaa992009-06-17 22:14:54 +020037 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})