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 <xtables.h>
18#include <linux/netfilter/xt_dscp.h>
19
20/* This is evil, but it's my code - HW*/
21#include "dscp_helper.c"
22
23enum {
24	O_DSCP = 0,
25	O_DSCP_CLASS,
26	F_DSCP       = 1 << O_DSCP,
27	F_DSCP_CLASS = 1 << O_DSCP_CLASS,
28};
29
30static void dscp_help(void)
31{
32	printf(
33"dscp match options\n"
34"[!] --dscp value		Match DSCP codepoint with numerical value\n"
35"  		                This value can be in decimal (ex: 32)\n"
36"               		or in hex (ex: 0x20)\n"
37"[!] --dscp-class name		Match the DiffServ class. This value may\n"
38"				be any of the BE,EF, AFxx or CSx classes\n"
39"\n"
40"				These two options are mutually exclusive !\n");
41}
42
43static const struct xt_option_entry dscp_opts[] = {
44	{.name = "dscp", .id = O_DSCP, .excl = F_DSCP_CLASS,
45	 .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX,
46	 .flags = XTOPT_INVERT | XTOPT_PUT,
47	 XTOPT_POINTER(struct xt_dscp_info, dscp)},
48	{.name = "dscp-class", .id = O_DSCP_CLASS, .excl = F_DSCP,
49	 .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
50	XTOPT_TABLEEND,
51};
52
53static void dscp_parse(struct xt_option_call *cb)
54{
55	struct xt_dscp_info *dinfo = cb->data;
56
57	xtables_option_parse(cb);
58	switch (cb->entry->id) {
59	case O_DSCP:
60		if (cb->invert)
61			dinfo->invert = 1;
62		break;
63	case O_DSCP_CLASS:
64		dinfo->dscp = class_to_dscp(cb->arg);
65		if (cb->invert)
66			dinfo->invert = 1;
67		break;
68	}
69}
70
71static void dscp_check(struct xt_fcheck_call *cb)
72{
73	if (cb->xflags == 0)
74		xtables_error(PARAMETER_PROBLEM,
75		           "DSCP match: Parameter --dscp is required");
76}
77
78static void
79dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
80{
81	const struct xt_dscp_info *dinfo =
82		(const struct xt_dscp_info *)match->data;
83	printf(" DSCP match %s0x%02x", dinfo->invert ? "!" : "", dinfo->dscp);
84}
85
86static void dscp_save(const void *ip, const struct xt_entry_match *match)
87{
88	const struct xt_dscp_info *dinfo =
89		(const struct xt_dscp_info *)match->data;
90
91	printf("%s --dscp 0x%02x", dinfo->invert ? " !" : "", dinfo->dscp);
92}
93
94static struct xtables_match dscp_match = {
95	.family		= NFPROTO_UNSPEC,
96	.name 		= "dscp",
97	.version 	= XTABLES_VERSION,
98	.size 		= XT_ALIGN(sizeof(struct xt_dscp_info)),
99	.userspacesize	= XT_ALIGN(sizeof(struct xt_dscp_info)),
100	.help		= dscp_help,
101	.print		= dscp_print,
102	.save		= dscp_save,
103	.x6_parse	= dscp_parse,
104	.x6_fcheck	= dscp_check,
105	.x6_options	= dscp_opts,
106};
107
108void _init(void)
109{
110	xtables_register_match(&dscp_match);
111}
112