1 /* Shared library add-on to iptables for DSCP
2 *
3 * (C) 2000- 2002 by Matthew G. Marsh <mgm@paktronix.com>,
4 * Harald Welte <laforge@gnumonks.org>
5 *
6 * This program is distributed under the terms of GNU GPL v2, 1991
7 *
8 * libipt_DSCP.c borrowed heavily from libipt_TOS.c
9 *
10 * --set-class added by Iain Barnes
11 */
12 #include <stdio.h>
13 #include <string.h>
14 #include <xtables.h>
15 #include <linux/netfilter/xt_DSCP.h>
16
17 /* This is evil, but it's my code - HW*/
18 #include "dscp_helper.c"
19
20 enum {
21 O_SET_DSCP = 0,
22 O_SET_DSCP_CLASS,
23 F_SET_DSCP = 1 << O_SET_DSCP,
24 F_SET_DSCP_CLASS = 1 << O_SET_DSCP_CLASS,
25 };
26
DSCP_help(void)27 static void DSCP_help(void)
28 {
29 printf(
30 "DSCP target options\n"
31 " --set-dscp value Set DSCP field in packet header to value\n"
32 " This value can be in decimal (ex: 32)\n"
33 " or in hex (ex: 0x20)\n"
34 " --set-dscp-class class Set the DSCP field in packet header to the\n"
35 " value represented by the DiffServ class value.\n"
36 " This class may be EF,BE or any of the CSxx\n"
37 " or AFxx classes.\n"
38 "\n"
39 " These two options are mutually exclusive !\n"
40 );
41 }
42
43 static const struct xt_option_entry DSCP_opts[] = {
44 {.name = "set-dscp", .id = O_SET_DSCP, .excl = F_SET_DSCP_CLASS,
45 .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX,
46 .flags = XTOPT_PUT,
47 XTOPT_POINTER(struct xt_DSCP_info, dscp)},
48 {.name = "set-dscp-class", .id = O_SET_DSCP_CLASS, .excl = F_SET_DSCP,
49 .type = XTTYPE_STRING},
50 XTOPT_TABLEEND,
51 };
52
DSCP_parse(struct xt_option_call * cb)53 static void DSCP_parse(struct xt_option_call *cb)
54 {
55 struct xt_DSCP_info *dinfo = cb->data;
56
57 xtables_option_parse(cb);
58 switch (cb->entry->id) {
59 case O_SET_DSCP_CLASS:
60 dinfo->dscp = class_to_dscp(cb->arg);
61 break;
62 }
63 }
64
DSCP_check(struct xt_fcheck_call * cb)65 static void DSCP_check(struct xt_fcheck_call *cb)
66 {
67 if (cb->xflags == 0)
68 xtables_error(PARAMETER_PROBLEM,
69 "DSCP target: Parameter --set-dscp is required");
70 }
71
72 static void
print_dscp(uint8_t dscp,int numeric)73 print_dscp(uint8_t dscp, int numeric)
74 {
75 printf(" 0x%02x", dscp);
76 }
77
DSCP_print(const void * ip,const struct xt_entry_target * target,int numeric)78 static void DSCP_print(const void *ip, const struct xt_entry_target *target,
79 int numeric)
80 {
81 const struct xt_DSCP_info *dinfo =
82 (const struct xt_DSCP_info *)target->data;
83 printf(" DSCP set");
84 print_dscp(dinfo->dscp, numeric);
85 }
86
DSCP_save(const void * ip,const struct xt_entry_target * target)87 static void DSCP_save(const void *ip, const struct xt_entry_target *target)
88 {
89 const struct xt_DSCP_info *dinfo =
90 (const struct xt_DSCP_info *)target->data;
91
92 printf(" --set-dscp 0x%02x", dinfo->dscp);
93 }
94
95
DSCP_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)96 static int DSCP_xlate(struct xt_xlate *xl,
97 const struct xt_xlate_tg_params *params)
98 {
99 const struct xt_DSCP_info *dinfo =
100 (struct xt_DSCP_info *)params->target->data;
101
102 xt_xlate_add(xl, "ip dscp set 0x%02x", dinfo->dscp);
103 return 1;
104 }
105
DSCP_xlate6(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)106 static int DSCP_xlate6(struct xt_xlate *xl,
107 const struct xt_xlate_tg_params *params)
108 {
109 const struct xt_DSCP_info *dinfo =
110 (struct xt_DSCP_info *)params->target->data;
111
112 xt_xlate_add(xl, "ip6 dscp set 0x%02x", dinfo->dscp);
113 return 1;
114 }
115
116 static struct xtables_target dscp_target[] = {
117 {
118 .family = NFPROTO_IPV4,
119 .name = "DSCP",
120 .version = XTABLES_VERSION,
121 .size = XT_ALIGN(sizeof(struct xt_DSCP_info)),
122 .userspacesize = XT_ALIGN(sizeof(struct xt_DSCP_info)),
123 .help = DSCP_help,
124 .print = DSCP_print,
125 .save = DSCP_save,
126 .x6_parse = DSCP_parse,
127 .x6_fcheck = DSCP_check,
128 .x6_options = DSCP_opts,
129 .xlate = DSCP_xlate,
130 },
131 {
132 .family = NFPROTO_IPV6,
133 .name = "DSCP",
134 .version = XTABLES_VERSION,
135 .size = XT_ALIGN(sizeof(struct xt_DSCP_info)),
136 .userspacesize = XT_ALIGN(sizeof(struct xt_DSCP_info)),
137 .help = DSCP_help,
138 .print = DSCP_print,
139 .save = DSCP_save,
140 .x6_parse = DSCP_parse,
141 .x6_fcheck = DSCP_check,
142 .x6_options = DSCP_opts,
143 .xlate = DSCP_xlate6,
144 },
145 };
146
_init(void)147 void _init(void)
148 {
149 xtables_register_targets(dscp_target, ARRAY_SIZE(dscp_target));
150 }
151