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( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 30 | "dscp match options\n" |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 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" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 37 | " These two options are mutually exclusive !\n"); |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 40 | static const struct option dscp_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 41 | { "dscp", 1, NULL, 'F' }, |
| 42 | { "dscp-class", 1, NULL, 'G' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 43 | { .name = NULL } |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static void |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 47 | parse_dscp(const char *s, struct xt_dscp_info *dinfo) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 48 | { |
| 49 | unsigned int dscp; |
| 50 | |
| 51 | if (string_to_number(s, 0, 255, &dscp) == -1) |
| 52 | exit_error(PARAMETER_PROBLEM, |
| 53 | "Invalid dscp `%s'\n", s); |
| 54 | |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 55 | if (dscp > XT_DSCP_MAX) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 56 | exit_error(PARAMETER_PROBLEM, |
| 57 | "DSCP `%d` out of range\n", dscp); |
| 58 | |
| 59 | dinfo->dscp = (u_int8_t )dscp; |
| 60 | return; |
| 61 | } |
| 62 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 63 | |
| 64 | static void |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 65 | parse_class(const char *s, struct xt_dscp_info *dinfo) |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 66 | { |
| 67 | unsigned int dscp = class_to_dscp(s); |
| 68 | |
| 69 | /* Assign the value */ |
| 70 | dinfo->dscp = (u_int8_t)dscp; |
| 71 | } |
| 72 | |
| 73 | |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 74 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 75 | dscp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 76 | const void *entry, struct xt_entry_match **match) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 77 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 78 | struct xt_dscp_info *dinfo |
| 79 | = (struct xt_dscp_info *)(*match)->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 80 | |
| 81 | switch (c) { |
| 82 | case 'F': |
| 83 | if (*flags) |
| 84 | exit_error(PARAMETER_PROBLEM, |
| 85 | "DSCP match: Only use --dscp ONCE!"); |
| 86 | check_inverse(optarg, &invert, &optind, 0); |
| 87 | parse_dscp(argv[optind-1], dinfo); |
| 88 | if (invert) |
| 89 | dinfo->invert = 1; |
| 90 | *flags = 1; |
| 91 | break; |
| 92 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 93 | case 'G': |
| 94 | if (*flags) |
| 95 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | a49ded0 | 2002-08-07 09:55:37 +0000 | [diff] [blame] | 96 | "DSCP match: Only use --dscp-class ONCE!"); |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 97 | check_inverse(optarg, &invert, &optind, 0); |
| 98 | parse_class(argv[optind - 1], dinfo); |
| 99 | if (invert) |
| 100 | dinfo->invert = 1; |
| 101 | *flags = 1; |
| 102 | break; |
| 103 | |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 104 | default: |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 111 | static void dscp_check(unsigned int flags) |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 112 | { |
| 113 | if (!flags) |
| 114 | exit_error(PARAMETER_PROBLEM, |
| 115 | "DSCP match: Parameter --dscp is required"); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | print_dscp(u_int8_t dscp, int invert, int numeric) |
| 120 | { |
| 121 | if (invert) |
| 122 | fputc('!', stdout); |
| 123 | |
| 124 | printf("0x%02x ", dscp); |
| 125 | } |
| 126 | |
| 127 | /* Prints out the matchinfo. */ |
| 128 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 129 | 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] | 130 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 131 | const struct xt_dscp_info *dinfo = |
| 132 | (const struct xt_dscp_info *)match->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 133 | printf("DSCP match "); |
| 134 | print_dscp(dinfo->dscp, dinfo->invert, numeric); |
| 135 | } |
| 136 | |
| 137 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 138 | 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] | 139 | { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 140 | const struct xt_dscp_info *dinfo = |
| 141 | (const struct xt_dscp_info *)match->data; |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 142 | |
| 143 | printf("--dscp "); |
| 144 | print_dscp(dinfo->dscp, dinfo->invert, 1); |
| 145 | } |
| 146 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 147 | static struct xtables_match dscp_match = { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 148 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 149 | .name = "dscp", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 150 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 151 | .size = XT_ALIGN(sizeof(struct xt_dscp_info)), |
| 152 | .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 153 | .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, |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 161 | static struct xtables_match dscp_match6 = { |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 162 | .family = AF_INET6, |
| 163 | .name = "dscp", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 164 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 18e0608 | 2007-07-24 07:17:23 +0000 | [diff] [blame] | 165 | .size = XT_ALIGN(sizeof(struct xt_dscp_info)), |
| 166 | .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 167 | .help = dscp_help, |
| 168 | .parse = dscp_parse, |
| 169 | .final_check = dscp_check, |
| 170 | .print = dscp_print, |
| 171 | .save = dscp_save, |
| 172 | .extra_opts = dscp_opts, |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | void _init(void) |
| 176 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 177 | xtables_register_match(&dscp_match); |
| 178 | xtables_register_match(&dscp_match6); |
Harald Welte | 487d1d3 | 2002-03-14 12:22:06 +0000 | [diff] [blame] | 179 | } |