libxt_dscp.c revision 181dead3f13befe02769ef479bcbb51801b7fc4e
1/* Shared library add-on to iptables for DSCP
2 *
3 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This program is distributed under the terms of GNU GPL v2, 1991
6 *
7 * libipt_dscp.c borrowed heavily from libipt_tos.c
8 *
9 * --class support added by Iain Barnes
10 *
11 * For a list of DSCP codepoints see
12 * http://www.iana.org/assignments/dscp-registry
13 *
14 */
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <getopt.h>
19
20#include <xtables.h>
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_dscp.h>
23
24/* This is evil, but it's my code - HW*/
25#include "libipt_dscp_helper.c"
26
27static void dscp_help(void)
28{
29	printf(
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"
36"\n"
37"				These two options are mutually exclusive !\n"
38				, IPTABLES_VERSION
39);
40}
41
42static const struct option dscp_opts[] = {
43	{ "dscp", 1, NULL, 'F' },
44	{ "dscp-class", 1, NULL, 'G' },
45	{ }
46};
47
48static void
49parse_dscp(const char *s, struct xt_dscp_info *dinfo)
50{
51	unsigned int dscp;
52
53	if (string_to_number(s, 0, 255, &dscp) == -1)
54		exit_error(PARAMETER_PROBLEM,
55			   "Invalid dscp `%s'\n", s);
56
57	if (dscp > XT_DSCP_MAX)
58		exit_error(PARAMETER_PROBLEM,
59			   "DSCP `%d` out of range\n", dscp);
60
61    	dinfo->dscp = (u_int8_t )dscp;
62    	return;
63}
64
65
66static void
67parse_class(const char *s, struct xt_dscp_info *dinfo)
68{
69	unsigned int dscp = class_to_dscp(s);
70
71	/* Assign the value */
72	dinfo->dscp = (u_int8_t)dscp;
73}
74
75
76static int
77dscp_parse(int c, char **argv, int invert, unsigned int *flags,
78           const void *entry, struct xt_entry_match **match)
79{
80	struct xt_dscp_info *dinfo
81		= (struct xt_dscp_info *)(*match)->data;
82
83	switch (c) {
84	case 'F':
85		if (*flags)
86			exit_error(PARAMETER_PROBLEM,
87			           "DSCP match: Only use --dscp ONCE!");
88		check_inverse(optarg, &invert, &optind, 0);
89		parse_dscp(argv[optind-1], dinfo);
90		if (invert)
91			dinfo->invert = 1;
92		*flags = 1;
93		break;
94
95	case 'G':
96		if (*flags)
97			exit_error(PARAMETER_PROBLEM,
98					"DSCP match: Only use --dscp-class ONCE!");
99		check_inverse(optarg, &invert, &optind, 0);
100		parse_class(argv[optind - 1], dinfo);
101		if (invert)
102			dinfo->invert = 1;
103		*flags = 1;
104		break;
105
106	default:
107		return 0;
108	}
109
110	return 1;
111}
112
113static void dscp_check(unsigned int flags)
114{
115	if (!flags)
116		exit_error(PARAMETER_PROBLEM,
117		           "DSCP match: Parameter --dscp is required");
118}
119
120static void
121print_dscp(u_int8_t dscp, int invert, int numeric)
122{
123	if (invert)
124		fputc('!', stdout);
125
126 	printf("0x%02x ", dscp);
127}
128
129/* Prints out the matchinfo. */
130static void
131dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
132{
133	const struct xt_dscp_info *dinfo =
134		(const struct xt_dscp_info *)match->data;
135	printf("DSCP match ");
136	print_dscp(dinfo->dscp, dinfo->invert, numeric);
137}
138
139/* Saves the union ipt_matchinfo in parsable form to stdout. */
140static void dscp_save(const void *ip, const struct xt_entry_match *match)
141{
142	const struct xt_dscp_info *dinfo =
143		(const struct xt_dscp_info *)match->data;
144
145	printf("--dscp ");
146	print_dscp(dinfo->dscp, dinfo->invert, 1);
147}
148
149static struct xtables_match dscp_match = {
150	.family		= AF_INET,
151	.name 		= "dscp",
152	.version 	= IPTABLES_VERSION,
153	.size 		= XT_ALIGN(sizeof(struct xt_dscp_info)),
154	.userspacesize	= XT_ALIGN(sizeof(struct xt_dscp_info)),
155	.help		= dscp_help,
156	.parse		= dscp_parse,
157	.final_check	= dscp_check,
158	.print		= dscp_print,
159	.save		= dscp_save,
160	.extra_opts	= dscp_opts,
161};
162
163static struct xtables_match dscp_match6 = {
164	.family		= AF_INET6,
165	.name 		= "dscp",
166	.version 	= IPTABLES_VERSION,
167	.size 		= XT_ALIGN(sizeof(struct xt_dscp_info)),
168	.userspacesize	= XT_ALIGN(sizeof(struct xt_dscp_info)),
169	.help		= dscp_help,
170	.parse		= dscp_parse,
171	.final_check	= dscp_check,
172	.print		= dscp_print,
173	.save		= dscp_save,
174	.extra_opts	= dscp_opts,
175};
176
177void _init(void)
178{
179	xtables_register_match(&dscp_match);
180	xtables_register_match(&dscp_match6);
181}
182