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
CLASSIFY_arp_save(const void * ip,const struct xt_entry_target * target)77 CLASSIFY_arp_save(const void *ip, const struct xt_entry_target *target)
78 {
79 const struct xt_classify_target_info *clinfo =
80 (const struct xt_classify_target_info *)target->data;
81
82 printf(" --set-class %x:%x",
83 TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
84 }
85
86 static void
CLASSIFY_arp_print(const void * ip,const struct xt_entry_target * target,int numeric)87 CLASSIFY_arp_print(const void *ip,
88 const struct xt_entry_target *target,
89 int numeric)
90 {
91 CLASSIFY_arp_save(ip, target);
92 }
93
CLASSIFY_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)94 static int CLASSIFY_xlate(struct xt_xlate *xl,
95 const struct xt_xlate_tg_params *params)
96 {
97 const struct xt_classify_target_info *clinfo =
98 (const struct xt_classify_target_info *)params->target->data;
99 __u32 handle = clinfo->priority;
100
101 xt_xlate_add(xl, "meta priority set ");
102
103 switch (handle) {
104 case TC_H_ROOT:
105 xt_xlate_add(xl, "root");
106 break;
107 case TC_H_UNSPEC:
108 xt_xlate_add(xl, "none");
109 break;
110 default:
111 xt_xlate_add(xl, "%0x:%0x", TC_H_MAJ(handle) >> 16,
112 TC_H_MIN(handle));
113 break;
114 }
115
116 return 1;
117 }
118
119 static struct xtables_target classify_tg_reg[] = {
120 {
121 .family = NFPROTO_UNSPEC,
122 .name = "CLASSIFY",
123 .version = XTABLES_VERSION,
124 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
125 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
126 .help = CLASSIFY_help,
127 .print = CLASSIFY_print,
128 .save = CLASSIFY_save,
129 .x6_parse = CLASSIFY_parse,
130 .x6_options = CLASSIFY_opts,
131 .xlate = CLASSIFY_xlate,
132 },
133 {
134 .family = NFPROTO_ARP,
135 .name = "CLASSIFY",
136 .version = XTABLES_VERSION,
137 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
138 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
139 .help = CLASSIFY_help,
140 .print = CLASSIFY_arp_print,
141 .save = CLASSIFY_arp_save,
142 .x6_parse = CLASSIFY_parse,
143 .x6_options = CLASSIFY_opts,
144 .xlate = CLASSIFY_xlate,
145 }
146 };
147
_init(void)148 void _init(void)
149 {
150 xtables_register_targets(classify_tg_reg, ARRAY_SIZE(classify_tg_reg));
151 }
152