blob: 7db2e4cd522ac3faaa0cc9502e32522713098e47 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Shared library add-on to iptables to add CLASSIFY target support. */
2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include <getopt.h>
6
7#include <xtables.h>
8#include <linux/netfilter/x_tables.h>
9#include <linux/netfilter/xt_CLASSIFY.h>
10#include <linux/types.h>
11#include <linux/pkt_sched.h>
12
13static void
14CLASSIFY_help(void)
15{
16 printf(
17"CLASSIFY target options:\n"
18"--set-class MAJOR:MINOR Set skb->priority value (always hexadecimal!)\n");
19}
20
21static const struct option CLASSIFY_opts[] = {
22 { "set-class", 1, NULL, '1' },
23 { .name = NULL }
24};
25
26static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
27{
28 unsigned int i, j;
29
30 if (sscanf(s, "%x:%x", &i, &j) != 2)
31 return 1;
32
33 *p = TC_H_MAKE(i<<16, j);
34 return 0;
35}
36
37static int
38CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags,
39 const void *entry,
40 struct xt_entry_target **target)
41{
42 struct xt_classify_target_info *clinfo
43 = (struct xt_classify_target_info *)(*target)->data;
44
45 switch (c) {
46 case '1':
47 if (CLASSIFY_string_to_priority(optarg, &clinfo->priority))
48 xtables_error(PARAMETER_PROBLEM,
49 "Bad class value `%s'", optarg);
50 if (*flags)
51 xtables_error(PARAMETER_PROBLEM,
52 "CLASSIFY: Can't specify --set-class twice");
53 *flags = 1;
54 break;
55
56 default:
57 return 0;
58 }
59
60 return 1;
61}
62
63static void
64CLASSIFY_final_check(unsigned int flags)
65{
66 if (!flags)
67 xtables_error(PARAMETER_PROBLEM,
68 "CLASSIFY: Parameter --set-class is required");
69}
70
71static void
72CLASSIFY_print_class(unsigned int priority, int numeric)
73{
74 printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
75}
76
77static void
78CLASSIFY_print(const void *ip,
79 const struct xt_entry_target *target,
80 int numeric)
81{
82 const struct xt_classify_target_info *clinfo =
83 (const struct xt_classify_target_info *)target->data;
84 printf("CLASSIFY set ");
85 CLASSIFY_print_class(clinfo->priority, numeric);
86}
87
88static void
89CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
90{
91 const struct xt_classify_target_info *clinfo =
92 (const struct xt_classify_target_info *)target->data;
93
94 printf("--set-class %.4x:%.4x ",
95 TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
96}
97
98static struct xtables_target classify_target = {
99 .family = AF_UNSPEC,
100 .name = "CLASSIFY",
101 .version = XTABLES_VERSION,
102 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
103 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
104 .help = CLASSIFY_help,
105 .parse = CLASSIFY_parse,
106 .final_check = CLASSIFY_final_check,
107 .print = CLASSIFY_print,
108 .save = CLASSIFY_save,
109 .extra_opts = CLASSIFY_opts,
110};
111
112void _init(void)
113{
114 xtables_register_target(&classify_target);
115}