| Rusty Russell | 2ce6ec6 | 2000-08-30 05:49:06 +0000 | [diff] [blame] | 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 <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_tcpmss.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "tcpmss match v%s options:\n" |
| 17 | "[!] --mss value[:value] Match TCP MSS range.\n" |
| 18 | " (only valid for TCP SYN or SYN/ACK packets)\n", |
| 19 | NETFILTER_VERSION); |
| 20 | } |
| 21 | |
| 22 | static struct option opts[] = { |
| 23 | { "mss", 1, 0, '1' }, |
| 24 | {0} |
| 25 | }; |
| 26 | |
| 27 | /* Initialize the match. */ |
| 28 | static void |
| 29 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 30 | { |
| 31 | *nfcache |= NFC_IP_PROTO_UNKNOWN; |
| 32 | } |
| 33 | |
| 34 | static u_int16_t |
| 35 | parse_tcp_mssvalue(const char *mssvalue) |
| 36 | { |
| 37 | int mssvaluenum; |
| 38 | |
| 39 | if ((mssvaluenum = string_to_number(mssvalue, 0, 65535)) != -1) |
| 40 | return (u_int16_t)mssvaluenum; |
| 41 | |
| 42 | exit_error(PARAMETER_PROBLEM, |
| 43 | "Invalid mss `%s' specified", mssvalue); |
| 44 | } |
| 45 | |
| 46 | static void |
| 47 | parse_tcp_mssvalues(const char *mssvaluestring, |
| 48 | u_int16_t *mss_min, u_int16_t *mss_max) |
| 49 | { |
| 50 | char *buffer; |
| 51 | char *cp; |
| 52 | |
| 53 | buffer = strdup(mssvaluestring); |
| 54 | if ((cp = strchr(buffer, ':')) == NULL) |
| Rusty Russell | d4a8b28 | 2000-08-31 11:22:14 +0000 | [diff] [blame] | 55 | *mss_min = *mss_max = parse_tcp_mssvalue(buffer); |
| Rusty Russell | 2ce6ec6 | 2000-08-30 05:49:06 +0000 | [diff] [blame] | 56 | else { |
| 57 | *cp = '\0'; |
| 58 | cp++; |
| 59 | |
| Rusty Russell | d4a8b28 | 2000-08-31 11:22:14 +0000 | [diff] [blame] | 60 | *mss_min = buffer[0] ? parse_tcp_mssvalue(buffer) : 0; |
| 61 | *mss_max = cp[0] ? parse_tcp_mssvalue(cp) : 0xFFFF; |
| Rusty Russell | 2ce6ec6 | 2000-08-30 05:49:06 +0000 | [diff] [blame] | 62 | } |
| 63 | free(buffer); |
| 64 | } |
| 65 | |
| 66 | /* Function which parses command options; returns true if it |
| 67 | ate an option */ |
| 68 | static int |
| 69 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 70 | const struct ipt_entry *entry, |
| 71 | unsigned int *nfcache, |
| 72 | struct ipt_entry_match **match) |
| 73 | { |
| 74 | struct ipt_tcpmss_match_info *mssinfo = |
| 75 | (struct ipt_tcpmss_match_info *)(*match)->data; |
| 76 | |
| 77 | switch (c) { |
| 78 | case '1': |
| 79 | if (*flags) |
| 80 | exit_error(PARAMETER_PROBLEM, |
| 81 | "Only one `--mss' allowed"); |
| 82 | if (check_inverse(optarg, &invert)) |
| 83 | optind++; |
| 84 | parse_tcp_mssvalues(argv[optind-1], |
| 85 | &mssinfo->mss_min, &mssinfo->mss_max); |
| 86 | if (invert) |
| 87 | mssinfo->invert = 1; |
| 88 | *flags = 1; |
| 89 | break; |
| 90 | default: |
| 91 | return 0; |
| 92 | } |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | print_tcpmss(u_int16_t mss_min, u_int16_t mss_max, int invert, int numeric) |
| 98 | { |
| 99 | if (invert) |
| 100 | printf("! "); |
| 101 | |
| 102 | if (mss_min == mss_max) |
| 103 | printf("%u", mss_min); |
| 104 | else |
| 105 | printf("%u:%u", mss_min, mss_max); |
| 106 | } |
| 107 | |
| 108 | /* Final check; must have specified --mss. */ |
| 109 | static void |
| 110 | final_check(unsigned int flags) |
| 111 | { |
| 112 | if (!flags) |
| 113 | exit_error(PARAMETER_PROBLEM, |
| 114 | "tcpmss match: You must specify `--mss'"); |
| 115 | } |
| 116 | |
| 117 | /* Prints out the matchinfo. */ |
| 118 | static void |
| 119 | print(const struct ipt_ip *ip, |
| 120 | const struct ipt_entry_match *match, |
| 121 | int numeric) |
| 122 | { |
| 123 | const struct ipt_tcpmss_match_info *mssinfo = |
| 124 | (const struct ipt_tcpmss_match_info *)match->data; |
| 125 | |
| 126 | printf("tcpmss match "); |
| Rusty Russell | d4a8b28 | 2000-08-31 11:22:14 +0000 | [diff] [blame] | 127 | print_tcpmss(mssinfo->mss_min, mssinfo->mss_max, |
| 128 | mssinfo->invert, numeric); |
| Rusty Russell | 2ce6ec6 | 2000-08-30 05:49:06 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 132 | static void |
| 133 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 134 | { |
| 135 | const struct ipt_tcpmss_match_info *mssinfo = |
| 136 | (const struct ipt_tcpmss_match_info *)match->data; |
| 137 | |
| 138 | printf("--mss "); |
| Rusty Russell | d4a8b28 | 2000-08-31 11:22:14 +0000 | [diff] [blame] | 139 | print_tcpmss(mssinfo->mss_min, mssinfo->mss_max, |
| 140 | mssinfo->invert, 0); |
| Rusty Russell | 2ce6ec6 | 2000-08-30 05:49:06 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | struct iptables_match tcpmss |
| 144 | = { NULL, |
| 145 | "tcpmss", |
| 146 | NETFILTER_VERSION, |
| 147 | IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)), |
| 148 | IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)), |
| 149 | &help, |
| 150 | &init, |
| 151 | &parse, |
| 152 | &final_check, |
| 153 | &print, |
| 154 | &save, |
| 155 | opts |
| 156 | }; |
| 157 | |
| 158 | void _init(void) |
| 159 | { |
| 160 | register_match(&tcpmss); |
| 161 | } |