libxt_cpu.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
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
54	return 1;
55}
56
57static void cpu_check(unsigned int flags)
58{
59	if (!flags)
60		xtables_error(PARAMETER_PROBLEM,
61			      "You must specify `--cpu'");
62}
63
64static void
65cpu_print(const void *ip, const struct xt_entry_match *match, int numeric)
66{
67	const struct xt_cpu_info *info = (void *)match->data;
68
69	printf(" cpu %s%u", info->invert ? "! ":"", info->cpu);
70}
71
72static void cpu_save(const void *ip, const struct xt_entry_match *match)
73{
74	const struct xt_cpu_info *info = (void *)match->data;
75
76	printf("%s --cpu %u", info->invert ? " !" : "", info->cpu);
77}
78
79static struct xtables_match cpu_match = {
80	.family		= NFPROTO_UNSPEC,
81 	.name		= "cpu",
82	.version	= XTABLES_VERSION,
83	.size		= XT_ALIGN(sizeof(struct xt_cpu_info)),
84	.userspacesize	= XT_ALIGN(sizeof(struct xt_cpu_info)),
85	.help		= cpu_help,
86	.parse		= cpu_parse,
87	.final_check	= cpu_check,
88	.print		= cpu_print,
89	.save		= cpu_save,
90	.extra_opts	= cpu_opts,
91};
92
93void _init(void)
94{
95	xtables_register_match(&cpu_match);
96}
97