1 /*
2 * Copyright (c) 2003-2013 Patrick McHardy <kaber@trash.net>
3 */
4
5 #include <stdio.h>
6 #include <xtables.h>
7 #include <linux/netfilter/xt_CLASSIFY.h>
8 #include <linux/pkt_sched.h>
9
10 enum {
11 O_SET_CLASS = 0,
12 };
13
14 static void
CLASSIFY_help(void)15 CLASSIFY_help(void)
16 {
17 printf(
18 "CLASSIFY target options:\n"
19 "--set-class MAJOR:MINOR Set skb->priority value (always hexadecimal!)\n");
20 }
21
22 static const struct xt_option_entry CLASSIFY_opts[] = {
23 {.name = "set-class", .id = O_SET_CLASS, .type = XTTYPE_STRING,
24 .flags = XTOPT_MAND},
25 XTOPT_TABLEEND,
26 };
27
CLASSIFY_string_to_priority(const char * s,unsigned int * p)28 static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
29 {
30 unsigned int i, j;
31
32 if (sscanf(s, "%x:%x", &i, &j) != 2)
33 return 1;
34
35 *p = TC_H_MAKE(i<<16, j);
36 return 0;
37 }
38
CLASSIFY_parse(struct xt_option_call * cb)39 static void CLASSIFY_parse(struct xt_option_call *cb)
40 {
41 struct xt_classify_target_info *clinfo = cb->data;
42
43 xtables_option_parse(cb);
44 if (CLASSIFY_string_to_priority(cb->arg, &clinfo->priority))
45 xtables_error(PARAMETER_PROBLEM,
46 "Bad class value \"%s\"", cb->arg);
47 }
48
49 static void
CLASSIFY_print_class(unsigned int priority,int numeric)50 CLASSIFY_print_class(unsigned int priority, int numeric)
51 {
52 printf(" %x:%x", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
53 }
54
55 static void
CLASSIFY_print(const void * ip,const struct xt_entry_target * target,int numeric)56 CLASSIFY_print(const void *ip,
57 const struct xt_entry_target *target,
58 int numeric)
59 {
60 const struct xt_classify_target_info *clinfo =
61 (const struct xt_classify_target_info *)target->data;
62 printf(" CLASSIFY set");
63 CLASSIFY_print_class(clinfo->priority, numeric);
64 }
65
66 static void
CLASSIFY_save(const void * ip,const struct xt_entry_target * target)67 CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
68 {
69 const struct xt_classify_target_info *clinfo =
70 (const struct xt_classify_target_info *)target->data;
71
72 printf(" --set-class %.4x:%.4x",
73 TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
74 }
75
76 static void
arpCLASSIFY_print(const void * ip,const struct xt_entry_target * target,int numeric)77 arpCLASSIFY_print(const void *ip, const struct xt_entry_target *target,
78 int numeric)
79 {
80 CLASSIFY_save(ip, target);
81 }
82
CLASSIFY_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)83 static int CLASSIFY_xlate(struct xt_xlate *xl,
84 const struct xt_xlate_tg_params *params)
85 {
86 const struct xt_classify_target_info *clinfo =
87 (const struct xt_classify_target_info *)params->target->data;
88 __u32 handle = clinfo->priority;
89
90 xt_xlate_add(xl, "meta priority set ");
91
92 switch (handle) {
93 case TC_H_ROOT:
94 xt_xlate_add(xl, "root");
95 break;
96 case TC_H_UNSPEC:
97 xt_xlate_add(xl, "none");
98 break;
99 default:
100 xt_xlate_add(xl, "%0x:%0x", TC_H_MAJ(handle) >> 16,
101 TC_H_MIN(handle));
102 break;
103 }
104
105 return 1;
106 }
107
108 static struct xtables_target classify_target[] = {
109 {
110 .family = NFPROTO_UNSPEC,
111 .name = "CLASSIFY",
112 .version = XTABLES_VERSION,
113 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
114 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
115 .help = CLASSIFY_help,
116 .print = CLASSIFY_print,
117 .save = CLASSIFY_save,
118 .x6_parse = CLASSIFY_parse,
119 .x6_options = CLASSIFY_opts,
120 .xlate = CLASSIFY_xlate,
121 },
122 {
123 .family = NFPROTO_ARP,
124 .name = "CLASSIFY",
125 .version = XTABLES_VERSION,
126 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
127 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
128 .help = CLASSIFY_help,
129 .print = arpCLASSIFY_print,
130 .x6_parse = CLASSIFY_parse,
131 .x6_options = CLASSIFY_opts,
132 .xlate = CLASSIFY_xlate,
133 },
134 };
135
_init(void)136 void _init(void)
137 {
138 xtables_register_targets(classify_target, ARRAY_SIZE(classify_target));
139 }
140