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_ipv4/ip_tables.h>
22 #include <linux/netfilter_ipv4/ipt_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 ipt_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
> IPT_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 ipt_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 ipt_entry
*entry
,
79 unsigned int *nfcache
,
80 struct ipt_entry_match
**match
)
82 struct ipt_dscp_info
*dinfo
83 = (struct ipt_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 ipt_ip
*ip
,
135 const struct ipt_entry_match
*match
,
138 const struct ipt_dscp_info
*dinfo
=
139 (const struct ipt_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 ipt_ip
*ip
, const struct ipt_entry_match
*match
)
148 const struct ipt_dscp_info
*dinfo
=
149 (const struct ipt_dscp_info
*)match
->data
;
152 print_dscp(dinfo
->dscp
, dinfo
->invert
, 1);
155 static struct iptables_match dscp
= {
158 .version
= IPTABLES_VERSION
,
159 .size
= IPT_ALIGN(sizeof(struct ipt_dscp_info
)),
160 .userspacesize
= IPT_ALIGN(sizeof(struct ipt_dscp_info
)),
163 .final_check
= &final_check
,
171 register_match(&dscp
);