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
20 #include <ip6tables.h>
21 #include <linux/netfilter_ipv6/ip6_tables.h>
22 #include <linux/netfilter/xt_dscp.h>
24 /* This is evil, but it's my code - HW*/
25 #include "libipt_dscp_helper.c"
27 static void help(void)
30 "DSCP match v%s 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"
42 static struct option opts
[] = {
43 { "dscp", 1, 0, 'F' },
44 { "dscp-class", 1, 0, 'G' },
49 parse_dscp(const char *s
, struct xt_dscp_info
*dinfo
)
53 if (string_to_number(s
, 0, 255, &dscp
) == -1)
54 exit_error(PARAMETER_PROBLEM
,
55 "Invalid dscp `%s'\n", s
);
57 if (dscp
> XT_DSCP_MAX
)
58 exit_error(PARAMETER_PROBLEM
,
59 "DSCP `%d` out of range\n", dscp
);
61 dinfo
->dscp
= (u_int8_t
)dscp
;
67 parse_class(const char *s
, struct xt_dscp_info
*dinfo
)
69 unsigned int dscp
= class_to_dscp(s
);
71 /* Assign the value */
72 dinfo
->dscp
= (u_int8_t
)dscp
;
77 parse(int c
, char **argv
, int invert
, unsigned int *flags
,
78 const struct ip6t_entry
*entry
,
79 unsigned int *nfcache
,
80 struct ip6t_entry_match
**match
)
82 struct xt_dscp_info
*dinfo
83 = (struct xt_dscp_info
*)(*match
)->data
;
88 exit_error(PARAMETER_PROBLEM
,
89 "DSCP match: Only use --dscp ONCE!");
90 check_inverse(optarg
, &invert
, &optind
, 0);
91 parse_dscp(argv
[optind
-1], dinfo
);
99 exit_error(PARAMETER_PROBLEM
,
100 "DSCP match: Only use --dscp-class ONCE!");
101 check_inverse(optarg
, &invert
, &optind
, 0);
102 parse_class(argv
[optind
- 1], dinfo
);
116 final_check(unsigned int flags
)
119 exit_error(PARAMETER_PROBLEM
,
120 "DSCP match: Parameter --dscp is required");
124 print_dscp(u_int8_t dscp
, int invert
, int numeric
)
129 printf("0x%02x ", dscp
);
132 /* Prints out the matchinfo. */
134 print(const struct ip6t_ip6
*ip
,
135 const struct ip6t_entry_match
*match
,
138 const struct xt_dscp_info
*dinfo
=
139 (const struct xt_dscp_info
*)match
->data
;
140 printf("DSCP match ");
141 print_dscp(dinfo
->dscp
, dinfo
->invert
, numeric
);
144 /* Saves the union ipt_matchinfo in parsable form to stdout. */
146 save(const struct ip6t_ip6
*ip
, const struct ip6t_entry_match
*match
)
148 const struct xt_dscp_info
*dinfo
=
149 (const struct xt_dscp_info
*)match
->data
;
152 print_dscp(dinfo
->dscp
, dinfo
->invert
, 1);
155 static struct ip6tables_match dscp
= {
158 .version
= IPTABLES_VERSION
,
159 .size
= IP6T_ALIGN(sizeof(struct xt_dscp_info
)),
160 .userspacesize
= IP6T_ALIGN(sizeof(struct xt_dscp_info
)),
163 .final_check
= &final_check
,
171 register_match6(&dscp
);