blob: fce14c26c22979053468f92014e65294a90ba2a6 [file] [log] [blame]
Harald Welte487d1d32002-03-14 12:22:06 +00001/* 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 *
Iain Barnes0ddae8f2002-06-21 17:35:55 +00009 * --class support added by Iain Barnes
10 *
Harald Welte487d1d32002-03-14 12:22:06 +000011 * 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
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000020#include <xtables.h>
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_dscp.h>
Harald Welte487d1d32002-03-14 12:22:06 +000023
Iain Barnes0ddae8f2002-06-21 17:35:55 +000024/* This is evil, but it's my code - HW*/
Jan Engelhardtf82070f2008-01-20 13:14:00 +000025#include "dscp_helper.c"
Iain Barnes0ddae8f2002-06-21 17:35:55 +000026
Jan Engelhardt181dead2007-10-04 16:27:07 +000027static void dscp_help(void)
Harald Welte487d1d32002-03-14 12:22:06 +000028{
29 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020030"dscp match options\n"
Harald Welte487d1d32002-03-14 12:22:06 +000031"[!] --dscp value Match DSCP codepoint with numerical value\n"
32" This value can be in decimal (ex: 32)\n"
Iain Barnes0ddae8f2002-06-21 17:35:55 +000033" or in hex (ex: 0x20)\n"
Harald Weltea49ded02002-08-07 09:55:37 +000034"[!] --dscp-class name Match the DiffServ class. This value may\n"
Iain Barnes0ddae8f2002-06-21 17:35:55 +000035" be any of the BE,EF, AFxx or CSx classes\n"
36"\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020037" These two options are mutually exclusive !\n");
Harald Welte487d1d32002-03-14 12:22:06 +000038}
39
Jan Engelhardt181dead2007-10-04 16:27:07 +000040static const struct option dscp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000041 { "dscp", 1, NULL, 'F' },
42 { "dscp-class", 1, NULL, 'G' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000043 { .name = NULL }
Harald Welte487d1d32002-03-14 12:22:06 +000044};
45
46static void
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000047parse_dscp(const char *s, struct xt_dscp_info *dinfo)
Harald Welte487d1d32002-03-14 12:22:06 +000048{
49 unsigned int dscp;
50
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010051 if (!xtables_strtoui(s, NULL, &dscp, 0, UINT8_MAX))
Harald Welte487d1d32002-03-14 12:22:06 +000052 exit_error(PARAMETER_PROBLEM,
53 "Invalid dscp `%s'\n", s);
54
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000055 if (dscp > XT_DSCP_MAX)
Harald Welte487d1d32002-03-14 12:22:06 +000056 exit_error(PARAMETER_PROBLEM,
57 "DSCP `%d` out of range\n", dscp);
58
Jan Engelhardt213e1852009-01-27 17:24:34 +010059 dinfo->dscp = dscp;
Harald Welte487d1d32002-03-14 12:22:06 +000060}
61
Iain Barnes0ddae8f2002-06-21 17:35:55 +000062
63static void
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000064parse_class(const char *s, struct xt_dscp_info *dinfo)
Iain Barnes0ddae8f2002-06-21 17:35:55 +000065{
66 unsigned int dscp = class_to_dscp(s);
67
68 /* Assign the value */
Jan Engelhardt213e1852009-01-27 17:24:34 +010069 dinfo->dscp = dscp;
Iain Barnes0ddae8f2002-06-21 17:35:55 +000070}
71
72
Harald Welte487d1d32002-03-14 12:22:06 +000073static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000074dscp_parse(int c, char **argv, int invert, unsigned int *flags,
75 const void *entry, struct xt_entry_match **match)
Harald Welte487d1d32002-03-14 12:22:06 +000076{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000077 struct xt_dscp_info *dinfo
78 = (struct xt_dscp_info *)(*match)->data;
Harald Welte487d1d32002-03-14 12:22:06 +000079
80 switch (c) {
81 case 'F':
82 if (*flags)
83 exit_error(PARAMETER_PROBLEM,
84 "DSCP match: Only use --dscp ONCE!");
85 check_inverse(optarg, &invert, &optind, 0);
86 parse_dscp(argv[optind-1], dinfo);
87 if (invert)
88 dinfo->invert = 1;
89 *flags = 1;
90 break;
91
Iain Barnes0ddae8f2002-06-21 17:35:55 +000092 case 'G':
93 if (*flags)
94 exit_error(PARAMETER_PROBLEM,
Harald Weltea49ded02002-08-07 09:55:37 +000095 "DSCP match: Only use --dscp-class ONCE!");
Iain Barnes0ddae8f2002-06-21 17:35:55 +000096 check_inverse(optarg, &invert, &optind, 0);
97 parse_class(argv[optind - 1], dinfo);
98 if (invert)
99 dinfo->invert = 1;
100 *flags = 1;
101 break;
102
Harald Welte487d1d32002-03-14 12:22:06 +0000103 default:
104 return 0;
105 }
106
107 return 1;
108}
109
Jan Engelhardt181dead2007-10-04 16:27:07 +0000110static void dscp_check(unsigned int flags)
Harald Welte487d1d32002-03-14 12:22:06 +0000111{
112 if (!flags)
113 exit_error(PARAMETER_PROBLEM,
114 "DSCP match: Parameter --dscp is required");
115}
116
117static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000118dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Welte487d1d32002-03-14 12:22:06 +0000119{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000120 const struct xt_dscp_info *dinfo =
121 (const struct xt_dscp_info *)match->data;
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100122 printf("DSCP match %s0x%02x", dinfo->invert ? "!" : "", dinfo->dscp);
Harald Welte487d1d32002-03-14 12:22:06 +0000123}
124
Jan Engelhardt181dead2007-10-04 16:27:07 +0000125static void dscp_save(const void *ip, const struct xt_entry_match *match)
Harald Welte487d1d32002-03-14 12:22:06 +0000126{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000127 const struct xt_dscp_info *dinfo =
128 (const struct xt_dscp_info *)match->data;
Harald Welte487d1d32002-03-14 12:22:06 +0000129
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100130 printf("%s--dscp 0x%02x ", dinfo->invert ? "! " : "", dinfo->dscp);
Harald Welte487d1d32002-03-14 12:22:06 +0000131}
132
Jan Engelhardt181dead2007-10-04 16:27:07 +0000133static struct xtables_match dscp_match = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100134 .family = NFPROTO_IPV4,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000135 .name = "dscp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200136 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000137 .size = XT_ALIGN(sizeof(struct xt_dscp_info)),
138 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000139 .help = dscp_help,
140 .parse = dscp_parse,
141 .final_check = dscp_check,
142 .print = dscp_print,
143 .save = dscp_save,
144 .extra_opts = dscp_opts,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000145};
146
Jan Engelhardt181dead2007-10-04 16:27:07 +0000147static struct xtables_match dscp_match6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100148 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000149 .name = "dscp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200150 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000151 .size = XT_ALIGN(sizeof(struct xt_dscp_info)),
152 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000153 .help = dscp_help,
154 .parse = dscp_parse,
155 .final_check = dscp_check,
156 .print = dscp_print,
157 .save = dscp_save,
158 .extra_opts = dscp_opts,
Harald Welte487d1d32002-03-14 12:22:06 +0000159};
160
161void _init(void)
162{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000163 xtables_register_match(&dscp_match);
164 xtables_register_match(&dscp_match6);
Harald Welte487d1d32002-03-14 12:22:06 +0000165}