Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Today's hack: quantum tunneling in structs
0004  *
0005  * 'entries' and 'term' are never anywhere referenced by word in code. In fact,
0006  * they serve as the hanging-off data accessed through repl.data[].
0007  */
0008 
0009 /* tbl has the following structure equivalent, but is C99 compliant:
0010  * struct {
0011  *  struct type##_replace repl;
0012  *  struct type##_standard entries[nhooks];
0013  *  struct type##_error term;
0014  * } *tbl;
0015  */
0016 
0017 #define xt_alloc_initial_table(type, typ2) ({ \
0018     unsigned int hook_mask = info->valid_hooks; \
0019     unsigned int nhooks = hweight32(hook_mask); \
0020     unsigned int bytes = 0, hooknum = 0, i = 0; \
0021     struct { \
0022         struct type##_replace repl; \
0023         struct type##_standard entries[]; \
0024     } *tbl; \
0025     struct type##_error *term; \
0026     size_t term_offset = (offsetof(typeof(*tbl), entries[nhooks]) + \
0027         __alignof__(*term) - 1) & ~(__alignof__(*term) - 1); \
0028     tbl = kzalloc(term_offset + sizeof(*term), GFP_KERNEL); \
0029     if (tbl == NULL) \
0030         return NULL; \
0031     term = (struct type##_error *)&(((char *)tbl)[term_offset]); \
0032     strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
0033     *term = (struct type##_error)typ2##_ERROR_INIT;  \
0034     tbl->repl.valid_hooks = hook_mask; \
0035     tbl->repl.num_entries = nhooks + 1; \
0036     tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
0037              sizeof(struct type##_error); \
0038     for (; hook_mask != 0; hook_mask >>= 1, ++hooknum) { \
0039         if (!(hook_mask & 1)) \
0040             continue; \
0041         tbl->repl.hook_entry[hooknum] = bytes; \
0042         tbl->repl.underflow[hooknum]  = bytes; \
0043         tbl->entries[i++] = (struct type##_standard) \
0044             typ2##_STANDARD_INIT(NF_ACCEPT); \
0045         bytes += sizeof(struct type##_standard); \
0046     } \
0047     tbl; \
0048 })