libxt_cpu.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* Shared library add-on to iptables to add CPU match support. */ 2#include <stdbool.h> 3#include <stdio.h> 4#include <netdb.h> 5#include <string.h> 6#include <stdlib.h> 7#include <getopt.h> 8#include <xtables.h> 9#include <linux/netfilter/xt_cpu.h> 10 11static void cpu_help(void) 12{ 13 printf( 14"cpu match options:\n" 15"[!] --cpu number Match CPU number\n"); 16} 17 18static const struct option cpu_opts[] = { 19 {.name = "cpu", .has_arg = true, .val = '1'}, 20 XT_GETOPT_TABLEEND, 21}; 22 23static void 24parse_cpu(const char *s, struct xt_cpu_info *info) 25{ 26 unsigned int cpu; 27 char *end; 28 29 if (!xtables_strtoui(s, &end, &cpu, 0, UINT32_MAX)) 30 xtables_param_act(XTF_BAD_VALUE, "cpu", "--cpu", s); 31 32 if (*end != '\0') 33 xtables_param_act(XTF_BAD_VALUE, "cpu", "--cpu", s); 34 35 info->cpu = cpu; 36} 37 38static int 39cpu_parse(int c, char **argv, int invert, unsigned int *flags, 40 const void *entry, struct xt_entry_match **match) 41{ 42 struct xt_cpu_info *cpuinfo = (struct xt_cpu_info *)(*match)->data; 43 44 switch (c) { 45 case '1': 46 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 47 parse_cpu(optarg, cpuinfo); 48 if (invert) 49 cpuinfo->invert = 1; 50 *flags = 1; 51 break; 52 53 default: 54 return 0; 55 } 56 57 return 1; 58} 59 60static void cpu_check(unsigned int flags) 61{ 62 if (!flags) 63 xtables_error(PARAMETER_PROBLEM, 64 "You must specify `--cpu'"); 65} 66 67static void 68cpu_print(const void *ip, const struct xt_entry_match *match, int numeric) 69{ 70 const struct xt_cpu_info *info = (void *)match->data; 71 72 printf("cpu %s%u ", info->invert ? "! ":"", info->cpu); 73} 74 75static void cpu_save(const void *ip, const struct xt_entry_match *match) 76{ 77 const struct xt_cpu_info *info = (void *)match->data; 78 79 printf("%s--cpu %u ", info->invert ? "! ":"", info->cpu); 80} 81 82static struct xtables_match cpu_match = { 83 .family = NFPROTO_UNSPEC, 84 .name = "cpu", 85 .version = XTABLES_VERSION, 86 .size = XT_ALIGN(sizeof(struct xt_cpu_info)), 87 .userspacesize = XT_ALIGN(sizeof(struct xt_cpu_info)), 88 .help = cpu_help, 89 .parse = cpu_parse, 90 .final_check = cpu_check, 91 .print = cpu_print, 92 .save = cpu_save, 93 .extra_opts = cpu_opts, 94}; 95 96void _init(void) 97{ 98 xtables_register_match(&cpu_match); 99} 100