xref: /openbmc/linux/net/netfilter/xt_repldata.h (revision a8da474e)
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 
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 
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; \
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); \
28 	if (tbl == NULL) \
29 		return NULL; \
30 	term = (struct type##_error *)&(((char *)tbl)[term_offset]); \
31 	strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
32 	*term = (struct type##_error)typ2##_ERROR_INIT;  \
33 	tbl->repl.valid_hooks = hook_mask; \
34 	tbl->repl.num_entries = nhooks + 1; \
35 	tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
36 			 sizeof(struct type##_error); \
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 })
48