libxt_dscp.c revision ea146a982e26c42f9954f140276f8deeb2edbe98
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 * 9 * --class support added by Iain Barnes 10 * 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 20#include <xtables.h> 21#include <linux/netfilter/x_tables.h> 22#include <linux/netfilter/xt_dscp.h> 23 24/* This is evil, but it's my code - HW*/ 25#include "libipt_dscp_helper.c" 26 27static void help(void) 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" 33" or in hex (ex: 0x20)\n" 34"[!] --dscp-class name Match the DiffServ class. This value may\n" 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 39); 40} 41 42static const struct option opts[] = { 43 { "dscp", 1, 0, 'F' }, 44 { "dscp-class", 1, 0, 'G' }, 45 { 0 } 46}; 47 48static void 49parse_dscp(const char *s, struct xt_dscp_info *dinfo) 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 57 if (dscp > XT_DSCP_MAX) 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 65 66static void 67parse_class(const char *s, struct xt_dscp_info *dinfo) 68{ 69 unsigned int dscp = class_to_dscp(s); 70 71 /* Assign the value */ 72 dinfo->dscp = (u_int8_t)dscp; 73} 74 75 76static int 77parse(int c, char **argv, int invert, unsigned int *flags, 78 const void *entry, 79 struct xt_entry_match **match) 80{ 81 struct xt_dscp_info *dinfo 82 = (struct xt_dscp_info *)(*match)->data; 83 84 switch (c) { 85 case 'F': 86 if (*flags) 87 exit_error(PARAMETER_PROBLEM, 88 "DSCP match: Only use --dscp ONCE!"); 89 check_inverse(optarg, &invert, &optind, 0); 90 parse_dscp(argv[optind-1], dinfo); 91 if (invert) 92 dinfo->invert = 1; 93 *flags = 1; 94 break; 95 96 case 'G': 97 if (*flags) 98 exit_error(PARAMETER_PROBLEM, 99 "DSCP match: Only use --dscp-class ONCE!"); 100 check_inverse(optarg, &invert, &optind, 0); 101 parse_class(argv[optind - 1], dinfo); 102 if (invert) 103 dinfo->invert = 1; 104 *flags = 1; 105 break; 106 107 default: 108 return 0; 109 } 110 111 return 1; 112} 113 114static void 115final_check(unsigned int flags) 116{ 117 if (!flags) 118 exit_error(PARAMETER_PROBLEM, 119 "DSCP match: Parameter --dscp is required"); 120} 121 122static void 123print_dscp(u_int8_t dscp, int invert, int numeric) 124{ 125 if (invert) 126 fputc('!', stdout); 127 128 printf("0x%02x ", dscp); 129} 130 131/* Prints out the matchinfo. */ 132static void 133print(const void *ip, 134 const struct xt_entry_match *match, 135 int numeric) 136{ 137 const struct xt_dscp_info *dinfo = 138 (const struct xt_dscp_info *)match->data; 139 printf("DSCP match "); 140 print_dscp(dinfo->dscp, dinfo->invert, numeric); 141} 142 143/* Saves the union ipt_matchinfo in parsable form to stdout. */ 144static void 145save(const void *ip, const struct xt_entry_match *match) 146{ 147 const struct xt_dscp_info *dinfo = 148 (const struct xt_dscp_info *)match->data; 149 150 printf("--dscp "); 151 print_dscp(dinfo->dscp, dinfo->invert, 1); 152} 153 154static struct xtables_match dscp = { 155 .family = AF_INET, 156 .name = "dscp", 157 .version = IPTABLES_VERSION, 158 .size = XT_ALIGN(sizeof(struct xt_dscp_info)), 159 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), 160 .help = &help, 161 .parse = &parse, 162 .final_check = &final_check, 163 .print = &print, 164 .save = &save, 165 .extra_opts = opts 166}; 167 168static struct xtables_match dscp6 = { 169 .family = AF_INET6, 170 .name = "dscp", 171 .version = IPTABLES_VERSION, 172 .size = XT_ALIGN(sizeof(struct xt_dscp_info)), 173 .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), 174 .help = &help, 175 .parse = &parse, 176 .final_check = &final_check, 177 .print = &print, 178 .save = &save, 179 .extra_opts = opts 180}; 181 182void _init(void) 183{ 184 xtables_register_match(&dscp); 185 xtables_register_match(&dscp6); 186} 187