]>
Commit | Line | Data |
---|---|---|
cdd289a2 PM |
1 | /* |
2 | * This is a module which is used for setting the MSS option in TCP packets. | |
3 | * | |
4 | * Copyright (C) 2000 Marc Boucher <marc@mbsi.ca> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/ip.h> | |
14 | #include <linux/ipv6.h> | |
15 | #include <linux/tcp.h> | |
16 | #include <net/ipv6.h> | |
17 | #include <net/tcp.h> | |
18 | ||
19 | #include <linux/netfilter_ipv4/ip_tables.h> | |
20 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
21 | #include <linux/netfilter/x_tables.h> | |
22 | #include <linux/netfilter/xt_tcpudp.h> | |
23 | #include <linux/netfilter/xt_TCPMSS.h> | |
24 | ||
25 | MODULE_LICENSE("GPL"); | |
26 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); | |
27 | MODULE_DESCRIPTION("x_tables TCP MSS modification module"); | |
28 | MODULE_ALIAS("ipt_TCPMSS"); | |
29 | MODULE_ALIAS("ip6t_TCPMSS"); | |
30 | ||
31 | static inline unsigned int | |
32 | optlen(const u_int8_t *opt, unsigned int offset) | |
33 | { | |
34 | /* Beware zero-length options: make finite progress */ | |
35 | if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0) | |
36 | return 1; | |
37 | else | |
38 | return opt[offset+1]; | |
39 | } | |
40 | ||
41 | static int | |
3db05fea | 42 | tcpmss_mangle_packet(struct sk_buff *skb, |
cdd289a2 PM |
43 | const struct xt_tcpmss_info *info, |
44 | unsigned int tcphoff, | |
45 | unsigned int minlen) | |
46 | { | |
47 | struct tcphdr *tcph; | |
48 | unsigned int tcplen, i; | |
49 | __be16 oldval; | |
50 | u16 newmss; | |
51 | u8 *opt; | |
52 | ||
3db05fea | 53 | if (!skb_make_writable(skb, skb->len)) |
cdd289a2 PM |
54 | return -1; |
55 | ||
3db05fea HX |
56 | tcplen = skb->len - tcphoff; |
57 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); | |
cdd289a2 PM |
58 | |
59 | /* Since it passed flags test in tcp match, we know it is is | |
60 | not a fragment, and has data >= tcp header length. SYN | |
61 | packets should not contain data: if they did, then we risk | |
62 | running over MTU, sending Frag Needed and breaking things | |
63 | badly. --RR */ | |
64 | if (tcplen != tcph->doff*4) { | |
65 | if (net_ratelimit()) | |
66 | printk(KERN_ERR "xt_TCPMSS: bad length (%u bytes)\n", | |
3db05fea | 67 | skb->len); |
cdd289a2 PM |
68 | return -1; |
69 | } | |
70 | ||
71 | if (info->mss == XT_TCPMSS_CLAMP_PMTU) { | |
3db05fea | 72 | if (dst_mtu(skb->dst) <= minlen) { |
cdd289a2 PM |
73 | if (net_ratelimit()) |
74 | printk(KERN_ERR "xt_TCPMSS: " | |
75 | "unknown or invalid path-MTU (%u)\n", | |
3db05fea | 76 | dst_mtu(skb->dst)); |
cdd289a2 PM |
77 | return -1; |
78 | } | |
3db05fea | 79 | newmss = dst_mtu(skb->dst) - minlen; |
cdd289a2 PM |
80 | } else |
81 | newmss = info->mss; | |
82 | ||
83 | opt = (u_int8_t *)tcph; | |
84 | for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) { | |
85 | if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS && | |
86 | opt[i+1] == TCPOLEN_MSS) { | |
87 | u_int16_t oldmss; | |
88 | ||
89 | oldmss = (opt[i+2] << 8) | opt[i+3]; | |
90 | ||
91 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
92 | oldmss <= newmss) | |
93 | return 0; | |
94 | ||
95 | opt[i+2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 96 | opt[i+3] = newmss & 0x00ff; |
cdd289a2 | 97 | |
3db05fea | 98 | nf_proto_csum_replace2(&tcph->check, skb, |
cdd289a2 PM |
99 | htons(oldmss), htons(newmss), 0); |
100 | return 0; | |
101 | } | |
102 | } | |
103 | ||
104 | /* | |
105 | * MSS Option not found ?! add it.. | |
106 | */ | |
3db05fea HX |
107 | if (skb_tailroom(skb) < TCPOLEN_MSS) { |
108 | if (pskb_expand_head(skb, 0, | |
109 | TCPOLEN_MSS - skb_tailroom(skb), | |
2ca7b0ac | 110 | GFP_ATOMIC)) |
cdd289a2 | 111 | return -1; |
3db05fea | 112 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
cdd289a2 PM |
113 | } |
114 | ||
3db05fea | 115 | skb_put(skb, TCPOLEN_MSS); |
cdd289a2 PM |
116 | |
117 | opt = (u_int8_t *)tcph + sizeof(struct tcphdr); | |
118 | memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr)); | |
119 | ||
3db05fea | 120 | nf_proto_csum_replace2(&tcph->check, skb, |
cdd289a2 PM |
121 | htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1); |
122 | opt[0] = TCPOPT_MSS; | |
123 | opt[1] = TCPOLEN_MSS; | |
124 | opt[2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 125 | opt[3] = newmss & 0x00ff; |
cdd289a2 | 126 | |
3db05fea | 127 | nf_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0); |
cdd289a2 PM |
128 | |
129 | oldval = ((__be16 *)tcph)[6]; | |
130 | tcph->doff += TCPOLEN_MSS/4; | |
3db05fea | 131 | nf_proto_csum_replace2(&tcph->check, skb, |
cdd289a2 PM |
132 | oldval, ((__be16 *)tcph)[6], 0); |
133 | return TCPOLEN_MSS; | |
134 | } | |
135 | ||
136 | static unsigned int | |
3db05fea | 137 | xt_tcpmss_target4(struct sk_buff *skb, |
cdd289a2 PM |
138 | const struct net_device *in, |
139 | const struct net_device *out, | |
140 | unsigned int hooknum, | |
141 | const struct xt_target *target, | |
142 | const void *targinfo) | |
143 | { | |
3db05fea | 144 | struct iphdr *iph = ip_hdr(skb); |
cdd289a2 PM |
145 | __be16 newlen; |
146 | int ret; | |
147 | ||
3db05fea | 148 | ret = tcpmss_mangle_packet(skb, targinfo, iph->ihl * 4, |
cdd289a2 PM |
149 | sizeof(*iph) + sizeof(struct tcphdr)); |
150 | if (ret < 0) | |
151 | return NF_DROP; | |
152 | if (ret > 0) { | |
3db05fea | 153 | iph = ip_hdr(skb); |
cdd289a2 PM |
154 | newlen = htons(ntohs(iph->tot_len) + ret); |
155 | nf_csum_replace2(&iph->check, iph->tot_len, newlen); | |
156 | iph->tot_len = newlen; | |
157 | } | |
158 | return XT_CONTINUE; | |
159 | } | |
160 | ||
161 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
162 | static unsigned int | |
3db05fea | 163 | xt_tcpmss_target6(struct sk_buff *skb, |
cdd289a2 PM |
164 | const struct net_device *in, |
165 | const struct net_device *out, | |
166 | unsigned int hooknum, | |
167 | const struct xt_target *target, | |
168 | const void *targinfo) | |
169 | { | |
3db05fea | 170 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
cdd289a2 PM |
171 | u8 nexthdr; |
172 | int tcphoff; | |
173 | int ret; | |
174 | ||
175 | nexthdr = ipv6h->nexthdr; | |
3db05fea | 176 | tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr); |
9dc0564e | 177 | if (tcphoff < 0) |
cdd289a2 | 178 | return NF_DROP; |
3db05fea | 179 | ret = tcpmss_mangle_packet(skb, targinfo, tcphoff, |
cdd289a2 PM |
180 | sizeof(*ipv6h) + sizeof(struct tcphdr)); |
181 | if (ret < 0) | |
182 | return NF_DROP; | |
183 | if (ret > 0) { | |
3db05fea | 184 | ipv6h = ipv6_hdr(skb); |
cdd289a2 PM |
185 | ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret); |
186 | } | |
187 | return XT_CONTINUE; | |
188 | } | |
189 | #endif | |
190 | ||
191 | #define TH_SYN 0x02 | |
192 | ||
193 | /* Must specify -p tcp --syn */ | |
e1931b78 | 194 | static inline bool find_syn_match(const struct xt_entry_match *m) |
cdd289a2 PM |
195 | { |
196 | const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data; | |
197 | ||
198 | if (strcmp(m->u.kernel.match->name, "tcp") == 0 && | |
199 | tcpinfo->flg_cmp & TH_SYN && | |
200 | !(tcpinfo->invflags & XT_TCP_INV_FLAGS)) | |
e1931b78 | 201 | return true; |
cdd289a2 | 202 | |
e1931b78 | 203 | return false; |
cdd289a2 PM |
204 | } |
205 | ||
e1931b78 | 206 | static bool |
cdd289a2 PM |
207 | xt_tcpmss_checkentry4(const char *tablename, |
208 | const void *entry, | |
209 | const struct xt_target *target, | |
210 | void *targinfo, | |
211 | unsigned int hook_mask) | |
212 | { | |
213 | const struct xt_tcpmss_info *info = targinfo; | |
214 | const struct ipt_entry *e = entry; | |
215 | ||
216 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
6e23ae2a PM |
217 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
218 | (1 << NF_INET_LOCAL_OUT) | | |
219 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
cdd289a2 PM |
220 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
221 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); | |
e1931b78 | 222 | return false; |
cdd289a2 PM |
223 | } |
224 | if (IPT_MATCH_ITERATE(e, find_syn_match)) | |
e1931b78 | 225 | return true; |
cdd289a2 | 226 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
e1931b78 | 227 | return false; |
cdd289a2 PM |
228 | } |
229 | ||
230 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
e1931b78 | 231 | static bool |
cdd289a2 PM |
232 | xt_tcpmss_checkentry6(const char *tablename, |
233 | const void *entry, | |
234 | const struct xt_target *target, | |
235 | void *targinfo, | |
236 | unsigned int hook_mask) | |
237 | { | |
238 | const struct xt_tcpmss_info *info = targinfo; | |
239 | const struct ip6t_entry *e = entry; | |
240 | ||
241 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
6e23ae2a PM |
242 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
243 | (1 << NF_INET_LOCAL_OUT) | | |
244 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
cdd289a2 PM |
245 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
246 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); | |
e1931b78 | 247 | return false; |
cdd289a2 PM |
248 | } |
249 | if (IP6T_MATCH_ITERATE(e, find_syn_match)) | |
e1931b78 | 250 | return true; |
cdd289a2 | 251 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
e1931b78 | 252 | return false; |
cdd289a2 PM |
253 | } |
254 | #endif | |
255 | ||
9f15c530 | 256 | static struct xt_target xt_tcpmss_reg[] __read_mostly = { |
cdd289a2 PM |
257 | { |
258 | .family = AF_INET, | |
259 | .name = "TCPMSS", | |
260 | .checkentry = xt_tcpmss_checkentry4, | |
261 | .target = xt_tcpmss_target4, | |
262 | .targetsize = sizeof(struct xt_tcpmss_info), | |
263 | .proto = IPPROTO_TCP, | |
264 | .me = THIS_MODULE, | |
265 | }, | |
266 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
267 | { | |
268 | .family = AF_INET6, | |
269 | .name = "TCPMSS", | |
270 | .checkentry = xt_tcpmss_checkentry6, | |
271 | .target = xt_tcpmss_target6, | |
272 | .targetsize = sizeof(struct xt_tcpmss_info), | |
273 | .proto = IPPROTO_TCP, | |
274 | .me = THIS_MODULE, | |
275 | }, | |
276 | #endif | |
277 | }; | |
278 | ||
279 | static int __init xt_tcpmss_init(void) | |
280 | { | |
281 | return xt_register_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg)); | |
282 | } | |
283 | ||
284 | static void __exit xt_tcpmss_fini(void) | |
285 | { | |
286 | xt_unregister_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg)); | |
287 | } | |
288 | ||
289 | module_init(xt_tcpmss_init); | |
290 | module_exit(xt_tcpmss_fini); |