libxt_tcpmss.c revision ddac6c5bc636003d664d25c08ea3fe176565096c
1/* Shared library add-on to iptables to add tcp MSS matching support. */ 2#include <stdio.h> 3#include <netdb.h> 4#include <string.h> 5#include <stdlib.h> 6#include <getopt.h> 7 8#include <xtables.h> 9#include <linux/netfilter/xt_tcpmss.h> 10 11static void tcpmss_help(void) 12{ 13 printf( 14"tcpmss match options:\n" 15"[!] --mss value[:value] Match TCP MSS range.\n" 16" (only valid for TCP SYN or SYN/ACK packets)\n"); 17} 18 19static const struct option tcpmss_opts[] = { 20 { "mss", 1, NULL, '1' }, 21 { .name = NULL } 22}; 23 24static u_int16_t 25parse_tcp_mssvalue(const char *mssvalue) 26{ 27 unsigned int mssvaluenum; 28 29 if (string_to_number(mssvalue, 0, 65535, &mssvaluenum) != -1) 30 return (u_int16_t)mssvaluenum; 31 32 exit_error(PARAMETER_PROBLEM, 33 "Invalid mss `%s' specified", mssvalue); 34} 35 36static void 37parse_tcp_mssvalues(const char *mssvaluestring, 38 u_int16_t *mss_min, u_int16_t *mss_max) 39{ 40 char *buffer; 41 char *cp; 42 43 buffer = strdup(mssvaluestring); 44 if ((cp = strchr(buffer, ':')) == NULL) 45 *mss_min = *mss_max = parse_tcp_mssvalue(buffer); 46 else { 47 *cp = '\0'; 48 cp++; 49 50 *mss_min = buffer[0] ? parse_tcp_mssvalue(buffer) : 0; 51 *mss_max = cp[0] ? parse_tcp_mssvalue(cp) : 0xFFFF; 52 } 53 free(buffer); 54} 55 56static int 57tcpmss_parse(int c, char **argv, int invert, unsigned int *flags, 58 const void *entry, struct xt_entry_match **match) 59{ 60 struct xt_tcpmss_match_info *mssinfo = 61 (struct xt_tcpmss_match_info *)(*match)->data; 62 63 switch (c) { 64 case '1': 65 if (*flags) 66 exit_error(PARAMETER_PROBLEM, 67 "Only one `--mss' allowed"); 68 check_inverse(optarg, &invert, &optind, 0); 69 parse_tcp_mssvalues(argv[optind-1], 70 &mssinfo->mss_min, &mssinfo->mss_max); 71 if (invert) 72 mssinfo->invert = 1; 73 *flags = 1; 74 break; 75 default: 76 return 0; 77 } 78 return 1; 79} 80 81static void 82print_tcpmss(u_int16_t mss_min, u_int16_t mss_max, int invert, int numeric) 83{ 84 if (invert) 85 printf("! "); 86 87 if (mss_min == mss_max) 88 printf("%u ", mss_min); 89 else 90 printf("%u:%u ", mss_min, mss_max); 91} 92 93static void tcpmss_check(unsigned int flags) 94{ 95 if (!flags) 96 exit_error(PARAMETER_PROBLEM, 97 "tcpmss match: You must specify `--mss'"); 98} 99 100static void 101tcpmss_print(const void *ip, const struct xt_entry_match *match, int numeric) 102{ 103 const struct xt_tcpmss_match_info *mssinfo = 104 (const struct xt_tcpmss_match_info *)match->data; 105 106 printf("tcpmss match "); 107 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max, 108 mssinfo->invert, numeric); 109} 110 111static void tcpmss_save(const void *ip, const struct xt_entry_match *match) 112{ 113 const struct xt_tcpmss_match_info *mssinfo = 114 (const struct xt_tcpmss_match_info *)match->data; 115 116 printf("--mss "); 117 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max, 118 mssinfo->invert, 0); 119} 120 121static struct xtables_match tcpmss_match = { 122 .family = AF_INET, 123 .name = "tcpmss", 124 .version = XTABLES_VERSION, 125 .size = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)), 126 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)), 127 .help = tcpmss_help, 128 .parse = tcpmss_parse, 129 .final_check = tcpmss_check, 130 .print = tcpmss_print, 131 .save = tcpmss_save, 132 .extra_opts = tcpmss_opts, 133}; 134 135static struct xtables_match tcpmss_match6 = { 136 .family = AF_INET6, 137 .name = "tcpmss", 138 .version = XTABLES_VERSION, 139 .size = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)), 140 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)), 141 .help = tcpmss_help, 142 .parse = tcpmss_parse, 143 .final_check = tcpmss_check, 144 .print = tcpmss_print, 145 .save = tcpmss_save, 146 .extra_opts = tcpmss_opts, 147}; 148 149void _init(void) 150{ 151 xtables_register_match(&tcpmss_match); 152 xtables_register_match(&tcpmss_match6); 153} 154