blob: a28af0966f2aae9e421230aa78021fd63a5e9db9 [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(
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"
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"
37" These two options are mutually exclusive !\n"
38 , IPTABLES_VERSION
Harald Welte487d1d32002-03-14 12:22:06 +000039);
40}
41
Jan Engelhardt181dead2007-10-04 16:27:07 +000042static const struct option dscp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000043 { "dscp", 1, NULL, 'F' },
44 { "dscp-class", 1, NULL, 'G' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000045 { .name = NULL }
Harald Welte487d1d32002-03-14 12:22:06 +000046};
47
48static void
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000049parse_dscp(const char *s, struct xt_dscp_info *dinfo)
Harald Welte487d1d32002-03-14 12:22:06 +000050{
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
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000057 if (dscp > XT_DSCP_MAX)
Harald Welte487d1d32002-03-14 12:22:06 +000058 exit_error(PARAMETER_PROBLEM,
59 "DSCP `%d` out of range\n", dscp);
60
61 dinfo->dscp = (u_int8_t )dscp;
62 return;
63}
64
Iain Barnes0ddae8f2002-06-21 17:35:55 +000065
66static void
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000067parse_class(const char *s, struct xt_dscp_info *dinfo)
Iain Barnes0ddae8f2002-06-21 17:35:55 +000068{
69 unsigned int dscp = class_to_dscp(s);
70
71 /* Assign the value */
72 dinfo->dscp = (u_int8_t)dscp;
73}
74
75
Harald Welte487d1d32002-03-14 12:22:06 +000076static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000077dscp_parse(int c, char **argv, int invert, unsigned int *flags,
78 const void *entry, struct xt_entry_match **match)
Harald Welte487d1d32002-03-14 12:22:06 +000079{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +000080 struct xt_dscp_info *dinfo
81 = (struct xt_dscp_info *)(*match)->data;
Harald Welte487d1d32002-03-14 12:22:06 +000082
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
Iain Barnes0ddae8f2002-06-21 17:35:55 +000095 case 'G':
96 if (*flags)
97 exit_error(PARAMETER_PROBLEM,
Harald Weltea49ded02002-08-07 09:55:37 +000098 "DSCP match: Only use --dscp-class ONCE!");
Iain Barnes0ddae8f2002-06-21 17:35:55 +000099 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
Harald Welte487d1d32002-03-14 12:22:06 +0000106 default:
107 return 0;
108 }
109
110 return 1;
111}
112
Jan Engelhardt181dead2007-10-04 16:27:07 +0000113static void dscp_check(unsigned int flags)
Harald Welte487d1d32002-03-14 12:22:06 +0000114{
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
Jan Engelhardt181dead2007-10-04 16:27:07 +0000131dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Welte487d1d32002-03-14 12:22:06 +0000132{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000133 const struct xt_dscp_info *dinfo =
134 (const struct xt_dscp_info *)match->data;
Harald Welte487d1d32002-03-14 12:22:06 +0000135 printf("DSCP match ");
136 print_dscp(dinfo->dscp, dinfo->invert, numeric);
137}
138
139/* Saves the union ipt_matchinfo in parsable form to stdout. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000140static void dscp_save(const void *ip, const struct xt_entry_match *match)
Harald Welte487d1d32002-03-14 12:22:06 +0000141{
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000142 const struct xt_dscp_info *dinfo =
143 (const struct xt_dscp_info *)match->data;
Harald Welte487d1d32002-03-14 12:22:06 +0000144
145 printf("--dscp ");
146 print_dscp(dinfo->dscp, dinfo->invert, 1);
147}
148
Jan Engelhardt181dead2007-10-04 16:27:07 +0000149static struct xtables_match dscp_match = {
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000150 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000151 .name = "dscp",
152 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000153 .size = XT_ALIGN(sizeof(struct xt_dscp_info)),
154 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000155 .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,
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000161};
162
Jan Engelhardt181dead2007-10-04 16:27:07 +0000163static struct xtables_match dscp_match6 = {
Yasuyuki KOZAKAI18e06082007-07-24 07:17:23 +0000164 .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)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000169 .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,
Harald Welte487d1d32002-03-14 12:22:06 +0000175};
176
177void _init(void)
178{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000179 xtables_register_match(&dscp_match);
180 xtables_register_match(&dscp_match6);
Harald Welte487d1d32002-03-14 12:22:06 +0000181}