Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables for DSCP |
| 2 | * |
| 3 | * (C) 2000- 2002 by Matthew G. Marsh <mgm@paktronix.com>, |
| 4 | * Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * This program is distributed under the terms of GNU GPL v2, 1991 |
| 7 | * |
| 8 | * libipt_DSCP.c borrowed heavily from libipt_TOS.c |
| 9 | * |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 10 | * --set-class added by Iain Barnes |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 11 | */ |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <getopt.h> |
| 16 | |
| 17 | #include <iptables.h> |
| 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 19 | #include <linux/netfilter_ipv4/ipt_DSCP.h> |
| 20 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 21 | /* This is evil, but it's my code - HW*/ |
| 22 | #include "libipt_dscp_helper.c" |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 23 | |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 24 | |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 25 | static void init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | static void help(void) |
| 30 | { |
| 31 | printf( |
| 32 | "DSCP target options\n" |
| 33 | " --set-dscp value Set DSCP field in packet header to value\n" |
| 34 | " This value can be in decimal (ex: 32)\n" |
| 35 | " or in hex (ex: 0x20)\n" |
Harald Welte | a49ded0 | 2002-08-07 09:55:37 +0000 | [diff] [blame] | 36 | " --set-dscp-class class Set the DSCP field in packet header to the\n" |
| 37 | " value represented by the DiffServ class value.\n" |
Maciej Soltysiak | 920463d | 2004-03-04 00:14:03 +0000 | [diff] [blame] | 38 | " This class may be EF,BE or any of the CSxx\n" |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 39 | " or AFxx classes.\n" |
| 40 | "\n" |
| 41 | " These two options are mutually exclusive !\n" |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 42 | ); |
| 43 | } |
| 44 | |
| 45 | static struct option opts[] = { |
| 46 | { "set-dscp", 1, 0, 'F' }, |
Harald Welte | a49ded0 | 2002-08-07 09:55:37 +0000 | [diff] [blame] | 47 | { "set-dscp-class", 1, 0, 'G' }, |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 48 | { 0 } |
| 49 | }; |
| 50 | |
| 51 | static void |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 52 | parse_dscp(const unsigned char *s, struct ipt_DSCP_info *dinfo) |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 53 | { |
| 54 | unsigned int dscp; |
| 55 | |
| 56 | if (string_to_number(s, 0, 255, &dscp) == -1) |
| 57 | exit_error(PARAMETER_PROBLEM, |
| 58 | "Invalid dscp `%s'\n", s); |
| 59 | |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 60 | if (dscp > IPT_DSCP_MAX) |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 61 | exit_error(PARAMETER_PROBLEM, |
| 62 | "DSCP `%d` out of range\n", dscp); |
| 63 | |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 64 | dinfo->dscp = (u_int8_t )dscp; |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 68 | |
| 69 | static void |
| 70 | parse_class(const unsigned char *s, struct ipt_DSCP_info *dinfo) |
| 71 | { |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 72 | unsigned int dscp = class_to_dscp(s); |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 73 | |
Iain Barnes | 0ddae8f | 2002-06-21 17:35:55 +0000 | [diff] [blame] | 74 | /* Assign the value */ |
| 75 | dinfo->dscp = (u_int8_t)dscp; |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 79 | static int |
| 80 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 81 | const struct ipt_entry *entry, |
| 82 | struct ipt_entry_target **target) |
| 83 | { |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 84 | struct ipt_DSCP_info *dinfo |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 85 | = (struct ipt_DSCP_info *)(*target)->data; |
| 86 | |
| 87 | switch (c) { |
| 88 | case 'F': |
| 89 | if (*flags) |
| 90 | exit_error(PARAMETER_PROBLEM, |
| 91 | "DSCP target: Only use --set-dscp ONCE!"); |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 92 | parse_dscp(optarg, dinfo); |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 93 | *flags = 1; |
| 94 | break; |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +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 target: Only use --set-dscp-class ONCE!"); |
Iain Barnes | df5e13f | 2002-04-11 10:48:53 +0000 | [diff] [blame] | 99 | parse_class(optarg, dinfo); |
| 100 | *flags = 1; |
| 101 | break; |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 102 | |
| 103 | default: |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | final_check(unsigned int flags) |
| 112 | { |
| 113 | if (!flags) |
| 114 | exit_error(PARAMETER_PROBLEM, |
| 115 | "DSCP target: Parameter --set-dscp is required"); |
| 116 | } |
| 117 | |
| 118 | static void |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 119 | print_dscp(u_int8_t dscp, int numeric) |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 120 | { |
| 121 | printf("0x%02x ", dscp); |
| 122 | } |
| 123 | |
| 124 | /* Prints out the targinfo. */ |
| 125 | static void |
| 126 | print(const struct ipt_ip *ip, |
| 127 | const struct ipt_entry_target *target, |
| 128 | int numeric) |
| 129 | { |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 130 | const struct ipt_DSCP_info *dinfo = |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 131 | (const struct ipt_DSCP_info *)target->data; |
| 132 | printf("DSCP set "); |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 133 | print_dscp(dinfo->dscp, numeric); |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 137 | static void |
| 138 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 139 | { |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 140 | const struct ipt_DSCP_info *dinfo = |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 141 | (const struct ipt_DSCP_info *)target->data; |
| 142 | |
Harald Welte | ed18bad | 2002-02-17 21:28:51 +0000 | [diff] [blame] | 143 | printf("--set-dscp 0x%02x ", dinfo->dscp); |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static |
| 147 | struct iptables_target dscp |
| 148 | = { NULL, |
| 149 | "DSCP", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 150 | IPTABLES_VERSION, |
Harald Welte | 2e7377d | 2002-02-17 19:54:42 +0000 | [diff] [blame] | 151 | IPT_ALIGN(sizeof(struct ipt_DSCP_info)), |
| 152 | IPT_ALIGN(sizeof(struct ipt_DSCP_info)), |
| 153 | &help, |
| 154 | &init, |
| 155 | &parse, |
| 156 | &final_check, |
| 157 | &print, |
| 158 | &save, |
| 159 | opts |
| 160 | }; |
| 161 | |
| 162 | void _init(void) |
| 163 | { |
| 164 | register_target(&dscp); |
| 165 | } |