K2.6 patches and update.
[tomato.git] / release / src / router / iptables / extensions / libipt_tcpmss.c
blob9a399bbddbac254d469937bccbf21dfcb2391054
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>
8 #include <iptables.h>
9 #include <linux/netfilter_ipv4/ipt_tcpmss.h>
11 /* Function which prints out usage message. */
12 static void
13 help(void)
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 IPTABLES_VERSION);
22 static struct option opts[] = {
23 { "mss", 1, 0, '1' },
24 {0}
27 static u_int16_t
28 parse_tcp_mssvalue(const char *mssvalue)
30 unsigned int mssvaluenum;
32 if (string_to_number(mssvalue, 0, 65535, &mssvaluenum) != -1)
33 return (u_int16_t)mssvaluenum;
35 exit_error(PARAMETER_PROBLEM,
36 "Invalid mss `%s' specified", mssvalue);
39 static void
40 parse_tcp_mssvalues(const char *mssvaluestring,
41 u_int16_t *mss_min, u_int16_t *mss_max)
43 char *buffer;
44 char *cp;
46 buffer = strdup(mssvaluestring);
47 if ((cp = strchr(buffer, ':')) == NULL)
48 *mss_min = *mss_max = parse_tcp_mssvalue(buffer);
49 else {
50 *cp = '\0';
51 cp++;
53 *mss_min = buffer[0] ? parse_tcp_mssvalue(buffer) : 0;
54 *mss_max = cp[0] ? parse_tcp_mssvalue(cp) : 0xFFFF;
56 free(buffer);
59 /* Function which parses command options; returns true if it
60 ate an option */
61 static int
62 parse(int c, char **argv, int invert, unsigned int *flags,
63 const struct ipt_entry *entry,
64 unsigned int *nfcache,
65 struct ipt_entry_match **match)
67 struct ipt_tcpmss_match_info *mssinfo =
68 (struct ipt_tcpmss_match_info *)(*match)->data;
70 switch (c) {
71 case '1':
72 if (*flags)
73 exit_error(PARAMETER_PROBLEM,
74 "Only one `--mss' allowed");
75 check_inverse(optarg, &invert, &optind, 0);
76 parse_tcp_mssvalues(argv[optind-1],
77 &mssinfo->mss_min, &mssinfo->mss_max);
78 if (invert)
79 mssinfo->invert = 1;
80 *flags = 1;
81 break;
82 default:
83 return 0;
85 return 1;
88 static void
89 print_tcpmss(u_int16_t mss_min, u_int16_t mss_max, int invert, int numeric)
91 if (invert)
92 printf("! ");
94 if (mss_min == mss_max)
95 printf("%u ", mss_min);
96 else
97 printf("%u:%u ", mss_min, mss_max);
100 /* Final check; must have specified --mss. */
101 static void
102 final_check(unsigned int flags)
104 if (!flags)
105 exit_error(PARAMETER_PROBLEM,
106 "tcpmss match: You must specify `--mss'");
109 /* Prints out the matchinfo. */
110 static void
111 print(const struct ipt_ip *ip,
112 const struct ipt_entry_match *match,
113 int numeric)
115 const struct ipt_tcpmss_match_info *mssinfo =
116 (const struct ipt_tcpmss_match_info *)match->data;
118 printf("tcpmss match ");
119 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
120 mssinfo->invert, numeric);
123 /* Saves the union ipt_matchinfo in parsable form to stdout. */
124 static void
125 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
127 const struct ipt_tcpmss_match_info *mssinfo =
128 (const struct ipt_tcpmss_match_info *)match->data;
130 printf("--mss ");
131 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
132 mssinfo->invert, 0);
135 static struct iptables_match tcpmss = {
136 .next = NULL,
137 .name = "tcpmss",
138 .version = IPTABLES_VERSION,
139 .size = IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)),
140 .userspacesize = IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)),
141 .help = &help,
142 .parse = &parse,
143 .final_check = &final_check,
144 .print = &print,
145 .save = &save,
146 .extra_opts = opts
149 void _init(void)
151 register_match(&tcpmss);