1 #include <stdio.h> 2 #include <xtables.h> 3 #include <linux/netfilter/xt_cpu.h> 4 5 enum { 6 O_CPU = 0, 7 }; 8 9 static void cpu_help(void) 10 { 11 printf( 12 "cpu match options:\n" 13 "[!] --cpu number Match CPU number\n"); 14 } 15 16 static const struct xt_option_entry cpu_opts[] = { 17 {.name = "cpu", .id = O_CPU, .type = XTTYPE_UINT32, 18 .flags = XTOPT_INVERT | XTOPT_MAND | XTOPT_PUT, 19 XTOPT_POINTER(struct xt_cpu_info, cpu)}, 20 XTOPT_TABLEEND, 21 }; 22 23 static void cpu_parse(struct xt_option_call *cb) 24 { 25 struct xt_cpu_info *cpuinfo = cb->data; 26 27 xtables_option_parse(cb); 28 if (cb->invert) 29 cpuinfo->invert = true; 30 } 31 32 static void 33 cpu_print(const void *ip, const struct xt_entry_match *match, int numeric) 34 { 35 const struct xt_cpu_info *info = (void *)match->data; 36 37 printf(" cpu %s%u", info->invert ? "! ":"", info->cpu); 38 } 39 40 static void cpu_save(const void *ip, const struct xt_entry_match *match) 41 { 42 const struct xt_cpu_info *info = (void *)match->data; 43 44 printf("%s --cpu %u", info->invert ? " !" : "", info->cpu); 45 } 46 47 static int cpu_xlate(struct xt_xlate *xl, 48 const struct xt_xlate_mt_params *params) 49 { 50 const struct xt_cpu_info *info = (void *)params->match->data; 51 52 xt_xlate_add(xl, "cpu%s %u", info->invert ? " !=" : "", info->cpu); 53 54 return 1; 55 } 56 57 static struct xtables_match cpu_match = { 58 .family = NFPROTO_UNSPEC, 59 .name = "cpu", 60 .version = XTABLES_VERSION, 61 .size = XT_ALIGN(sizeof(struct xt_cpu_info)), 62 .userspacesize = XT_ALIGN(sizeof(struct xt_cpu_info)), 63 .help = cpu_help, 64 .print = cpu_print, 65 .save = cpu_save, 66 .x6_parse = cpu_parse, 67 .x6_options = cpu_opts, 68 .xlate = cpu_xlate, 69 }; 70 71 void _init(void) 72 { 73 xtables_register_match(&cpu_match); 74 } 75