1 /* Shared library add-on to iptables for DSCP
3 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
5 * This program is distributed under the terms of GNU GPL v2, 1991
7 * libipt_dscp.c borrowed heavily from libipt_tos.c
9 * --class support added by Iain Barnes
11 * For a list of DSCP codepoints see
12 * http://www.iana.org/assignments/dscp-registry
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_dscp.h>
24 /* This is evil, but it's my code - HW*/
25 #include "dscp_helper.c"
27 static void dscp_help(void)
30 "dscp match options\n"
31 "[!] --dscp value Match DSCP codepoint with numerical value\n"
32 " This value can be in decimal (ex: 32)\n"
33 " or in hex (ex: 0x20)\n"
34 "[!] --dscp-class name Match the DiffServ class. This value may\n"
35 " be any of the BE,EF, AFxx or CSx classes\n"
37 " These two options are mutually exclusive !\n");
40 static const struct option dscp_opts[] = {
41 { "dscp", 1, NULL, 'F' },
42 { "dscp-class", 1, NULL, 'G' },
47 parse_dscp(const char *s, struct xt_dscp_info *dinfo)
51 if (string_to_number(s, 0, 255, &dscp) == -1)
52 exit_error(PARAMETER_PROBLEM,
53 "Invalid dscp `%s'\n", s);
55 if (dscp > XT_DSCP_MAX)
56 exit_error(PARAMETER_PROBLEM,
57 "DSCP `%d` out of range\n", dscp);
59 dinfo->dscp = (u_int8_t )dscp;
65 parse_class(const char *s, struct xt_dscp_info *dinfo)
67 unsigned int dscp = class_to_dscp(s);
69 /* Assign the value */
70 dinfo->dscp = (u_int8_t)dscp;
75 dscp_parse(int c, char **argv, int invert, unsigned int *flags,
76 const void *entry, struct xt_entry_match **match)
78 struct xt_dscp_info *dinfo
79 = (struct xt_dscp_info *)(*match)->data;
84 exit_error(PARAMETER_PROBLEM,
85 "DSCP match: Only use --dscp ONCE!");
86 check_inverse(optarg, &invert, &optind, 0);
87 parse_dscp(argv[optind-1], dinfo);
95 exit_error(PARAMETER_PROBLEM,
96 "DSCP match: Only use --dscp-class ONCE!");
97 check_inverse(optarg, &invert, &optind, 0);
98 parse_class(argv[optind - 1], dinfo);
111 static void dscp_check(unsigned int flags)
114 exit_error(PARAMETER_PROBLEM,
115 "DSCP match: Parameter --dscp is required");
119 print_dscp(u_int8_t dscp, int invert, int numeric)
124 printf("0x%02x ", dscp);
127 /* Prints out the matchinfo. */
129 dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
131 const struct xt_dscp_info *dinfo =
132 (const struct xt_dscp_info *)match->data;
133 printf("DSCP match ");
134 print_dscp(dinfo->dscp, dinfo->invert, numeric);
137 /* Saves the union ipt_matchinfo in parsable form to stdout. */
138 static void dscp_save(const void *ip, const struct xt_entry_match *match)
140 const struct xt_dscp_info *dinfo =
141 (const struct xt_dscp_info *)match->data;
144 print_dscp(dinfo->dscp, dinfo->invert, 1);
147 static struct xtables_match dscp_match = {
150 .version = XTABLES_VERSION,
151 .size = XT_ALIGN(sizeof(struct xt_dscp_info)),
152 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)),
155 .final_check = dscp_check,
158 .extra_opts = dscp_opts,
161 static struct xtables_match dscp_match6 = {
164 .version = XTABLES_VERSION,
165 .size = XT_ALIGN(sizeof(struct xt_dscp_info)),
166 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)),
169 .final_check = dscp_check,
172 .extra_opts = dscp_opts,
177 xtables_register_match(&dscp_match);
178 xtables_register_match(&dscp_match6);