blob: 87353bfe07149909782f4328324e56570f6fc3fe [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{
Harald Welteb4719762001-07-23 02:14:22 +000037 unsigned int mssvaluenum;
Rusty Russell2ce6ec62000-08-30 05:49:06 +000038
Harald Welteb4719762001-07-23 02:14:22 +000039 if (string_to_number(mssvalue, 0, 65535, &mssvaluenum) != -1)
Rusty Russell2ce6ec62000-08-30 05:49:06 +000040 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");
Harald Welteb77f1da2002-03-14 11:35:58 +000082 check_inverse(optarg, &invert, &optind, 0);
Rusty Russell2ce6ec62000-08-30 05:49:06 +000083 parse_tcp_mssvalues(argv[optind-1],
84 &mssinfo->mss_min, &mssinfo->mss_max);
85 if (invert)
86 mssinfo->invert = 1;
87 *flags = 1;
88 break;
89 default:
90 return 0;
91 }
92 return 1;
93}
94
95static void
96print_tcpmss(u_int16_t mss_min, u_int16_t mss_max, int invert, int numeric)
97{
98 if (invert)
99 printf("! ");
100
101 if (mss_min == mss_max)
102 printf("%u", mss_min);
103 else
104 printf("%u:%u", mss_min, mss_max);
105}
106
107/* Final check; must have specified --mss. */
108static void
109final_check(unsigned int flags)
110{
111 if (!flags)
112 exit_error(PARAMETER_PROBLEM,
113 "tcpmss match: You must specify `--mss'");
114}
115
116/* Prints out the matchinfo. */
117static void
118print(const struct ipt_ip *ip,
119 const struct ipt_entry_match *match,
120 int numeric)
121{
122 const struct ipt_tcpmss_match_info *mssinfo =
123 (const struct ipt_tcpmss_match_info *)match->data;
124
125 printf("tcpmss match ");
Rusty Russelld4a8b282000-08-31 11:22:14 +0000126 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
127 mssinfo->invert, numeric);
Rusty Russell2ce6ec62000-08-30 05:49:06 +0000128}
129
130/* Saves the union ipt_matchinfo in parsable form to stdout. */
131static void
132save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
133{
134 const struct ipt_tcpmss_match_info *mssinfo =
135 (const struct ipt_tcpmss_match_info *)match->data;
136
137 printf("--mss ");
Rusty Russelld4a8b282000-08-31 11:22:14 +0000138 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
139 mssinfo->invert, 0);
Rusty Russell2ce6ec62000-08-30 05:49:06 +0000140}
141
Harald Welte3efb6ea2001-08-06 18:50:21 +0000142static
Rusty Russell2ce6ec62000-08-30 05:49:06 +0000143struct 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}