1 /* Shared library add-on to iptables for DSCP
3 * (C) 2000- 2002 by Matthew G. Marsh <mgm@paktronix.com>,
4 * Harald Welte <laforge@gnumonks.org>
6 * This program is distributed under the terms of GNU GPL v2, 1991
8 * libipt_DSCP.c borrowed heavily from libipt_TOS.c
10 * --set-class added by Iain Barnes
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_DSCP.h>
21 /* This is evil, but it's my code - HW*/
22 #include "libipt_dscp_helper.c"
25 static void init(struct ipt_entry_target *t, unsigned int *nfcache)
29 static void help(void)
32 "DSCP target options\n"
33 " --set-dscp value Set DSCP field in packet header to value\n"
34 " This value can be in decimal (ex: 32)\n"
35 " or in hex (ex: 0x20)\n"
36 " --set-dscp-class class Set the DSCP field in packet header to the\n"
37 " value represented by the DiffServ class value.\n"
38 " This class may be EF,BE or any of the CSxx\n"
41 " These two options are mutually exclusive !\n"
45 static struct option opts[] = {
46 { "set-dscp", 1, 0, 'F' },
47 { "set-dscp-class", 1, 0, 'G' },
52 parse_dscp(const char *s, struct ipt_DSCP_info *dinfo)
56 if (string_to_number(s, 0, 255, &dscp) == -1)
57 exit_error(PARAMETER_PROBLEM,
58 "Invalid dscp `%s'\n", s);
60 if (dscp > IPT_DSCP_MAX)
61 exit_error(PARAMETER_PROBLEM,
62 "DSCP `%d` out of range\n", dscp);
64 dinfo->dscp = (u_int8_t )dscp;
70 parse_class(const char *s, struct ipt_DSCP_info *dinfo)
72 unsigned int dscp = class_to_dscp(s);
74 /* Assign the value */
75 dinfo->dscp = (u_int8_t)dscp;
80 parse(int c, char **argv, int invert, unsigned int *flags,
81 const struct ipt_entry *entry,
82 struct ipt_entry_target **target)
84 struct ipt_DSCP_info *dinfo
85 = (struct ipt_DSCP_info *)(*target)->data;
90 exit_error(PARAMETER_PROBLEM,
91 "DSCP target: Only use --set-dscp ONCE!");
92 parse_dscp(optarg, dinfo);
97 exit_error(PARAMETER_PROBLEM,
98 "DSCP target: Only use --set-dscp-class ONCE!");
99 parse_class(optarg, dinfo);
111 final_check(unsigned int flags)
114 exit_error(PARAMETER_PROBLEM,
115 "DSCP target: Parameter --set-dscp is required");
119 print_dscp(u_int8_t dscp, int numeric)
121 printf("0x%02x ", dscp);
124 /* Prints out the targinfo. */
126 print(const struct ipt_ip *ip,
127 const struct ipt_entry_target *target,
130 const struct ipt_DSCP_info *dinfo =
131 (const struct ipt_DSCP_info *)target->data;
133 print_dscp(dinfo->dscp, numeric);
136 /* Saves the union ipt_targinfo in parsable form to stdout. */
138 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
140 const struct ipt_DSCP_info *dinfo =
141 (const struct ipt_DSCP_info *)target->data;
143 printf("--set-dscp 0x%02x ", dinfo->dscp);
146 static struct iptables_target dscp = {
149 .version = IPTABLES_VERSION,
150 .size = IPT_ALIGN(sizeof(struct ipt_DSCP_info)),
151 .userspacesize = IPT_ALIGN(sizeof(struct ipt_DSCP_info)),
155 .final_check = &final_check,
163 register_target(&dscp);