Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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 | * |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 9 | * --class support added by Iain Barnes |
| 10 | * |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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 | |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 20 | #include <xtables.h> |
| 21 | #include <linux/netfilter/x_tables.h> |
| 22 | #include <linux/netfilter/xt_dscp.h> |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 23 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 24 | /* This is evil, but it's my code - HW*/ |
Jan Engelhardt | f82070f | 2008-01-20 13:14:00 +0000 | [diff] [blame] | 25 | #include "dscp_helper.c" |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 26 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 27 | static void dscp_help(void) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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" |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 33 | " or in hex (ex: 0x20)\n" |
Harald Welte | a49ded0 | 2002-08-07 09:55:37 +0000 | [diff] [blame] | 34 | "[!] --dscp-class name Match the DiffServ class. This value may\n" |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 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 |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 39 | ); |
| 40 | } |
| 41 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 42 | static const struct option dscp_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 43 | { "dscp", 1, NULL, 'F' }, |
| 44 | { "dscp-class", 1, NULL, 'G' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 45 | { .name = NULL } |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static void |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 49 | parse_dscp(const char *s, struct xt_dscp_info *dinfo) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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 | |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 57 | if (dscp > XT_DSCP_MAX) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 65 | |
| 66 | static void |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 67 | parse_class(const char *s, struct xt_dscp_info *dinfo) |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned int dscp = class_to_dscp(s); |
| 70 | |
| 71 | /* Assign the value */ |
| 72 | dinfo->dscp = (u_int8_t)dscp; |
| 73 | } |
| 74 | |
| 75 | |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 76 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 77 | dscp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 78 | const void *entry, struct xt_entry_match **match) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 79 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 80 | struct xt_dscp_info *dinfo |
| 81 | = (struct xt_dscp_info *)(*match)->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 95 | case 'G': |
| 96 | if (*flags) |
| 97 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | a49ded0 | 2002-08-07 09:55:37 +0000 | [diff] [blame] | 98 | "DSCP match: Only use --dscp-class ONCE!"); |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 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 | |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 106 | default: |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | return 1; |
| 111 | } |
| 112 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 113 | static void dscp_check(unsigned int flags) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 114 | { |
| 115 | if (!flags) |
| 116 | exit_error(PARAMETER_PROBLEM, |
| 117 | "DSCP match: Parameter --dscp is required"); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | print_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. */ |
| 130 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 131 | dscp_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 132 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 133 | const struct xt_dscp_info *dinfo = |
| 134 | (const struct xt_dscp_info *)match->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 140 | static void dscp_save(const void *ip, const struct xt_entry_match *match) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 141 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 142 | const struct xt_dscp_info *dinfo = |
| 143 | (const struct xt_dscp_info *)match->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 144 | |
| 145 | printf("--dscp "); |
| 146 | print_dscp(dinfo->dscp, dinfo->invert, 1); |
| 147 | } |
| 148 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 149 | static struct xtables_match dscp_match = { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 150 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 151 | .name = "dscp", |
| 152 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 153 | .size = XT_ALIGN(sizeof(struct xt_dscp_info)), |
| 154 | .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 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, |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 163 | static struct xtables_match dscp_match6 = { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 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)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 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, |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | void _init(void) |
| 178 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 179 | xtables_register_match(&dscp_match); |
| 180 | xtables_register_match(&dscp_match6); |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 181 | } |