libxt_dscp.c revision d09b6d591ca7d7d7575cb6aa20384c9830f777ab
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 <stdbool.h>
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <getopt.h>
20
21#include <xtables.h>
22#include <linux/netfilter/x_tables.h>
23#include <linux/netfilter/xt_dscp.h>
24
25/* This is evil, but it's my code - HW*/
26#include "dscp_helper.c"
27
28static void dscp_help(void)
29{
30	printf(
31"dscp match options\n"
32"[!] --dscp value		Match DSCP codepoint with numerical value\n"
33"  		                This value can be in decimal (ex: 32)\n"
34"               		or in hex (ex: 0x20)\n"
35"[!] --dscp-class name		Match the DiffServ class. This value may\n"
36"				be any of the BE,EF, AFxx or CSx classes\n"
37"\n"
38"				These two options are mutually exclusive !\n");
39}
40
41static const struct option dscp_opts[] = {
42	{.name = "dscp",       .has_arg = true, .val = 'F'},
43	{.name = "dscp-class", .has_arg = true, .val = 'G'},
44	XT_GETOPT_TABLEEND,
45};
46
47static void
48parse_dscp(const char *s, struct xt_dscp_info *dinfo)
49{
50	unsigned int dscp;
51
52	if (!xtables_strtoui(s, NULL, &dscp, 0, UINT8_MAX))
53		xtables_error(PARAMETER_PROBLEM,
54			   "Invalid dscp `%s'\n", s);
55
56	if (dscp > XT_DSCP_MAX)
57		xtables_error(PARAMETER_PROBLEM,
58			   "DSCP `%d` out of range\n", dscp);
59
60	dinfo->dscp = dscp;
61}
62
63
64static void
65parse_class(const char *s, struct xt_dscp_info *dinfo)
66{
67	unsigned int dscp = class_to_dscp(s);
68
69	/* Assign the value */
70	dinfo->dscp = dscp;
71}
72
73
74static int
75dscp_parse(int c, char **argv, int invert, unsigned int *flags,
76           const void *entry, struct xt_entry_match **match)
77{
78	struct xt_dscp_info *dinfo
79		= (struct xt_dscp_info *)(*match)->data;
80
81	switch (c) {
82	case 'F':
83		if (*flags)
84			xtables_error(PARAMETER_PROBLEM,
85			           "DSCP match: Only use --dscp ONCE!");
86		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
87		parse_dscp(optarg, dinfo);
88		if (invert)
89			dinfo->invert = 1;
90		*flags = 1;
91		break;
92
93	case 'G':
94		if (*flags)
95			xtables_error(PARAMETER_PROBLEM,
96					"DSCP match: Only use --dscp-class ONCE!");
97		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
98		parse_class(optarg, dinfo);
99		if (invert)
100			dinfo->invert = 1;
101		*flags = 1;
102		break;
103	}
104
105	return 1;
106}
107
108static void dscp_check(unsigned int flags)
109{
110	if (!flags)
111		xtables_error(PARAMETER_PROBLEM,
112		           "DSCP match: Parameter --dscp is required");
113}
114
115static void
116dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
117{
118	const struct xt_dscp_info *dinfo =
119		(const struct xt_dscp_info *)match->data;
120	printf("DSCP match %s0x%02x", dinfo->invert ? "!" : "", dinfo->dscp);
121}
122
123static void dscp_save(const void *ip, const struct xt_entry_match *match)
124{
125	const struct xt_dscp_info *dinfo =
126		(const struct xt_dscp_info *)match->data;
127
128	printf("%s--dscp 0x%02x ", dinfo->invert ? "! " : "", dinfo->dscp);
129}
130
131static struct xtables_match dscp_match = {
132	.family		= NFPROTO_UNSPEC,
133	.name 		= "dscp",
134	.version 	= XTABLES_VERSION,
135	.size 		= XT_ALIGN(sizeof(struct xt_dscp_info)),
136	.userspacesize	= XT_ALIGN(sizeof(struct xt_dscp_info)),
137	.help		= dscp_help,
138	.parse		= dscp_parse,
139	.final_check	= dscp_check,
140	.print		= dscp_print,
141	.save		= dscp_save,
142	.extra_opts	= dscp_opts,
143};
144
145void _init(void)
146{
147	xtables_register_match(&dscp_match);
148}
149