2 * Shared library add-on to iptables to add TOS target support
4 * Copyright © CC Computer Consultants GmbH, 2007
5 * Contact: Jan Engelhardt <jengelh@computergmbh.de>
11 #include <netinet/in.h>
14 #include <linux/netfilter/xt_DSCP.h>
15 #include <linux/netfilter_ipv4/ipt_TOS.h>
16 #include "tos_values.c"
22 static const struct option tos_tg_opts_v0
[] = {
23 {.name
= "set-tos", .has_arg
= true, .val
= '='},
27 static const struct option tos_tg_opts
[] = {
28 {.name
= "set-tos", .has_arg
= true, .val
= '='},
29 {.name
= "and-tos", .has_arg
= true, .val
= '&'},
30 {.name
= "or-tos", .has_arg
= true, .val
= '|'},
31 {.name
= "xor-tos", .has_arg
= true, .val
= '^'},
35 static void tos_tg_help_v0(void)
37 const struct tos_symbol_info
*symbol
;
40 "TOS target options:\n"
41 " --set-tos value Set Type of Service/Priority field to value\n"
42 " --set-tos symbol Set TOS field (IPv4 only) by symbol\n"
43 " Accepted symbolic names for value are:\n");
45 for (symbol
= tos_symbol_names
; symbol
->name
!= NULL
; ++symbol
)
46 printf(" (0x%02x) %2u %s\n",
47 symbol
->value
, symbol
->value
, symbol
->name
);
52 static void tos_tg_help(void)
54 const struct tos_symbol_info
*symbol
;
57 "TOS target v%s options:\n"
58 " --set-tos value[/mask] Set Type of Service/Priority field to value\n"
59 " (Zero out bits in mask and XOR value into TOS)\n"
60 " --set-tos symbol Set TOS field (IPv4 only) by symbol\n"
61 " (this zeroes the 4-bit Precedence part!)\n"
62 " Accepted symbolic names for value are:\n",
65 for (symbol
= tos_symbol_names
; symbol
->name
!= NULL
; ++symbol
)
66 printf(" (0x%02x) %2u %s\n",
67 symbol
->value
, symbol
->value
, symbol
->name
);
71 " --and-tos bits Binary AND the TOS value with bits\n"
72 " --or-tos bits Binary OR the TOS value with bits\n"
73 " --xor-tos bits Binary XOR the TOS value with bits\n"
77 static int tos_tg_parse_v0(int c
, char **argv
, int invert
, unsigned int *flags
,
78 const void *entry
, struct xt_entry_target
**target
)
80 struct ipt_tos_target_info
*info
= (void *)(*target
)->data
;
81 struct tos_value_mask tvm
;
85 xtables_param_act(XTF_ONLY_ONCE
, "TOS", "--set-tos", *flags
& FLAG_TOS
);
86 xtables_param_act(XTF_NO_INVERT
, "TOS", "--set-tos", invert
);
87 if (!tos_parse_symbolic(optarg
, &tvm
, 0xFF))
88 xtables_param_act(XTF_BAD_VALUE
, "TOS", "--set-tos", optarg
);
90 xtables_error(PARAMETER_PROBLEM
, "tos match: Your kernel "
91 "is too old to support anything besides "
93 info
->tos
= tvm
.value
;
101 static int tos_tg_parse(int c
, char **argv
, int invert
, unsigned int *flags
,
102 const void *entry
, struct xt_entry_target
**target
)
104 struct xt_tos_target_info
*info
= (void *)(*target
)->data
;
105 struct tos_value_mask tvm
;
109 case '=': /* --set-tos */
110 xtables_param_act(XTF_ONLY_ONCE
, "TOS", "--set-tos", *flags
& FLAG_TOS
);
111 xtables_param_act(XTF_NO_INVERT
, "TOS", "--set-tos", invert
);
112 if (!tos_parse_symbolic(optarg
, &tvm
, 0x3F))
113 xtables_param_act(XTF_BAD_VALUE
, "TOS", "--set-tos", optarg
);
114 info
->tos_value
= tvm
.value
;
115 info
->tos_mask
= tvm
.mask
;
118 case '&': /* --and-tos */
119 xtables_param_act(XTF_ONLY_ONCE
, "TOS", "--and-tos", *flags
& FLAG_TOS
);
120 xtables_param_act(XTF_NO_INVERT
, "TOS", "--and-tos", invert
);
121 if (!xtables_strtoui(optarg
, NULL
, &bits
, 0, UINT8_MAX
))
122 xtables_param_act(XTF_BAD_VALUE
, "TOS", "--and-tos", optarg
);
124 info
->tos_mask
= ~bits
;
127 case '|': /* --or-tos */
128 xtables_param_act(XTF_ONLY_ONCE
, "TOS", "--or-tos", *flags
& FLAG_TOS
);
129 xtables_param_act(XTF_NO_INVERT
, "TOS", "--or-tos", invert
);
130 if (!xtables_strtoui(optarg
, NULL
, &bits
, 0, UINT8_MAX
))
131 xtables_param_act(XTF_BAD_VALUE
, "TOS", "--or-tos", optarg
);
132 info
->tos_value
= bits
;
133 info
->tos_mask
= bits
;
136 case '^': /* --xor-tos */
137 xtables_param_act(XTF_ONLY_ONCE
, "TOS", "--xor-tos", *flags
& FLAG_TOS
);
138 xtables_param_act(XTF_NO_INVERT
, "TOS", "--xor-tos", invert
);
139 if (!xtables_strtoui(optarg
, NULL
, &bits
, 0, UINT8_MAX
))
140 xtables_param_act(XTF_BAD_VALUE
, "TOS", "--xor-tos", optarg
);
141 info
->tos_value
= bits
;
153 static void tos_tg_check(unsigned int flags
)
156 xtables_error(PARAMETER_PROBLEM
,
157 "TOS: The --set-tos parameter is required");
160 static void tos_tg_print_v0(const void *ip
,
161 const struct xt_entry_target
*target
, int numeric
)
163 const struct ipt_tos_target_info
*info
= (const void *)target
->data
;
166 if (numeric
|| !tos_try_print_symbolic("", info
->tos
, 0xFF))
167 printf("0x%02x ", info
->tos
);
170 static void tos_tg_print(const void *ip
, const struct xt_entry_target
*target
,
173 const struct xt_tos_target_info
*info
= (const void *)target
->data
;
176 printf("TOS set 0x%02x/0x%02x ",
177 info
->tos_value
, info
->tos_mask
);
178 else if (tos_try_print_symbolic("TOS set ",
179 info
->tos_value
, info
->tos_mask
))
180 /* already printed by call */
182 else if (info
->tos_value
== 0)
183 printf("TOS and 0x%02x ",
184 (unsigned int)(u_int8_t
)~info
->tos_mask
);
185 else if (info
->tos_value
== info
->tos_mask
)
186 printf("TOS or 0x%02x ", info
->tos_value
);
187 else if (info
->tos_mask
== 0)
188 printf("TOS xor 0x%02x ", info
->tos_value
);
190 printf("TOS set 0x%02x/0x%02x ",
191 info
->tos_value
, info
->tos_mask
);
194 static void tos_tg_save_v0(const void *ip
, const struct xt_entry_target
*target
)
196 const struct ipt_tos_target_info
*info
= (const void *)target
->data
;
198 printf("--set-tos 0x%02x ", info
->tos
);
201 static void tos_tg_save(const void *ip
, const struct xt_entry_target
*target
)
203 const struct xt_tos_target_info
*info
= (const void *)target
->data
;
205 printf("--set-tos 0x%02x/0x%02x ", info
->tos_value
, info
->tos_mask
);
208 static struct xtables_target tos_tg_reg_v0
= {
209 .version
= XTABLES_VERSION
,
212 .family
= NFPROTO_IPV4
,
213 .size
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
214 .userspacesize
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
215 .help
= tos_tg_help_v0
,
216 .parse
= tos_tg_parse_v0
,
217 .final_check
= tos_tg_check
,
218 .print
= tos_tg_print_v0
,
219 .save
= tos_tg_save_v0
,
220 .extra_opts
= tos_tg_opts_v0
,
223 static struct xtables_target tos_tg_reg
= {
224 .version
= XTABLES_VERSION
,
227 .family
= NFPROTO_IPV4
,
228 .size
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
229 .userspacesize
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
231 .parse
= tos_tg_parse
,
232 .final_check
= tos_tg_check
,
233 .print
= tos_tg_print
,
235 .extra_opts
= tos_tg_opts
,
238 static struct xtables_target tos_tg6_reg
= {
239 .version
= XTABLES_VERSION
,
241 .family
= NFPROTO_IPV6
,
243 .size
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
244 .userspacesize
= XT_ALIGN(sizeof(struct xt_tos_target_info
)),
246 .parse
= tos_tg_parse
,
247 .final_check
= tos_tg_check
,
248 .print
= tos_tg_print
,
250 .extra_opts
= tos_tg_opts
,
255 xtables_register_target(&tos_tg_reg_v0
);
256 xtables_register_target(&tos_tg_reg
);
257 xtables_register_target(&tos_tg6_reg
);