blob: 79e8d76c1f54811fd890e03ab0a2ba4fcc81d29a [file] [log] [blame]
Rusty Russell2ce6ec62000-08-30 05:49:06 +00001/* 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. */
12static void
13help(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",
19NETFILTER_VERSION);
20}
21
22static struct option opts[] = {
23 { "mss", 1, 0, '1' },
24 {0}
25};
26
27/* Initialize the match. */
28static void
29init(struct ipt_entry_match *m, unsigned int *nfcache)
30{
31 *nfcache |= NFC_IP_PROTO_UNKNOWN;
32}
33
34static u_int16_t
35parse_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
46static void
47parse_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 Russelld4a8b282000-08-31 11:22:14 +000055 *mss_min = *mss_max = parse_tcp_mssvalue(buffer);
Rusty Russell2ce6ec62000-08-30 05:49:06 +000056 else {
57 *cp = '\0';
58 cp++;
59
Rusty Russelld4a8b282000-08-31 11:22:14 +000060 *mss_min = buffer[0] ? parse_tcp_mssvalue(buffer) : 0;
61 *mss_max = cp[0] ? parse_tcp_mssvalue(cp) : 0xFFFF;
Rusty Russell2ce6ec62000-08-30 05:49:06 +000062 }
63 free(buffer);
64}
65
66/* Function which parses command options; returns true if it
67 ate an option */
68static int
69parse(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
96static void
97print_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. */
109static void
110final_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. */
118static void
119print(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 Russelld4a8b282000-08-31 11:22:14 +0000127 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
128 mssinfo->invert, numeric);
Rusty Russell2ce6ec62000-08-30 05:49:06 +0000129}
130
131/* Saves the union ipt_matchinfo in parsable form to stdout. */
132static void
133save(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 Russelld4a8b282000-08-31 11:22:14 +0000139 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
140 mssinfo->invert, 0);
Rusty Russell2ce6ec62000-08-30 05:49:06 +0000141}
142
143struct 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
158void _init(void)
159{
160 register_match(&tcpmss);
161}