blob: bf6249e44068f51b524ae42da32faf59cb92c674 [file] [log] [blame]
Patrick McHardycdd289a2007-02-07 15:09:46 -08001/*
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
25MODULE_LICENSE("GPL");
26MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
27MODULE_DESCRIPTION("x_tables TCP MSS modification module");
28MODULE_ALIAS("ipt_TCPMSS");
29MODULE_ALIAS("ip6t_TCPMSS");
30
31static inline unsigned int
32optlen(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
41static int
Herbert Xu3db05fe2007-10-15 00:53:15 -070042tcpmss_mangle_packet(struct sk_buff *skb,
Patrick McHardycdd289a2007-02-07 15:09:46 -080043 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
Herbert Xu3db05fe2007-10-15 00:53:15 -070053 if (!skb_make_writable(skb, skb->len))
Patrick McHardycdd289a2007-02-07 15:09:46 -080054 return -1;
55
Herbert Xu3db05fe2007-10-15 00:53:15 -070056 tcplen = skb->len - tcphoff;
57 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
Patrick McHardycdd289a2007-02-07 15:09:46 -080058
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",
Herbert Xu3db05fe2007-10-15 00:53:15 -070067 skb->len);
Patrick McHardycdd289a2007-02-07 15:09:46 -080068 return -1;
69 }
70
71 if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
Herbert Xu3db05fe2007-10-15 00:53:15 -070072 if (dst_mtu(skb->dst) <= minlen) {
Patrick McHardycdd289a2007-02-07 15:09:46 -080073 if (net_ratelimit())
74 printk(KERN_ERR "xt_TCPMSS: "
75 "unknown or invalid path-MTU (%u)\n",
Herbert Xu3db05fe2007-10-15 00:53:15 -070076 dst_mtu(skb->dst));
Patrick McHardycdd289a2007-02-07 15:09:46 -080077 return -1;
78 }
Herbert Xu3db05fe2007-10-15 00:53:15 -070079 newmss = dst_mtu(skb->dst) - minlen;
Patrick McHardycdd289a2007-02-07 15:09:46 -080080 } 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;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070096 opt[i+3] = newmss & 0x00ff;
Patrick McHardycdd289a2007-02-07 15:09:46 -080097
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110098 inet_proto_csum_replace2(&tcph->check, skb,
99 htons(oldmss), htons(newmss),
100 0);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800101 return 0;
102 }
103 }
104
105 /*
106 * MSS Option not found ?! add it..
107 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700108 if (skb_tailroom(skb) < TCPOLEN_MSS) {
109 if (pskb_expand_head(skb, 0,
110 TCPOLEN_MSS - skb_tailroom(skb),
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700111 GFP_ATOMIC))
Patrick McHardycdd289a2007-02-07 15:09:46 -0800112 return -1;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700113 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800114 }
115
Herbert Xu3db05fe2007-10-15 00:53:15 -0700116 skb_put(skb, TCPOLEN_MSS);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800117
118 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
119 memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
120
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100121 inet_proto_csum_replace2(&tcph->check, skb,
122 htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800123 opt[0] = TCPOPT_MSS;
124 opt[1] = TCPOLEN_MSS;
125 opt[2] = (newmss & 0xff00) >> 8;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700126 opt[3] = newmss & 0x00ff;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800127
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100128 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800129
130 oldval = ((__be16 *)tcph)[6];
131 tcph->doff += TCPOLEN_MSS/4;
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100132 inet_proto_csum_replace2(&tcph->check, skb,
133 oldval, ((__be16 *)tcph)[6], 0);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800134 return TCPOLEN_MSS;
135}
136
137static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700138xt_tcpmss_target4(struct sk_buff *skb,
Patrick McHardycdd289a2007-02-07 15:09:46 -0800139 const struct net_device *in,
140 const struct net_device *out,
141 unsigned int hooknum,
142 const struct xt_target *target,
143 const void *targinfo)
144{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700145 struct iphdr *iph = ip_hdr(skb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800146 __be16 newlen;
147 int ret;
148
Herbert Xu3db05fe2007-10-15 00:53:15 -0700149 ret = tcpmss_mangle_packet(skb, targinfo, iph->ihl * 4,
Patrick McHardycdd289a2007-02-07 15:09:46 -0800150 sizeof(*iph) + sizeof(struct tcphdr));
151 if (ret < 0)
152 return NF_DROP;
153 if (ret > 0) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700154 iph = ip_hdr(skb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800155 newlen = htons(ntohs(iph->tot_len) + ret);
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100156 csum_replace2(&iph->check, iph->tot_len, newlen);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800157 iph->tot_len = newlen;
158 }
159 return XT_CONTINUE;
160}
161
162#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
163static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700164xt_tcpmss_target6(struct sk_buff *skb,
Patrick McHardycdd289a2007-02-07 15:09:46 -0800165 const struct net_device *in,
166 const struct net_device *out,
167 unsigned int hooknum,
168 const struct xt_target *target,
169 const void *targinfo)
170{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700171 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800172 u8 nexthdr;
173 int tcphoff;
174 int ret;
175
176 nexthdr = ipv6h->nexthdr;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700177 tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr);
Patrick McHardy9dc05642007-11-30 23:58:03 +1100178 if (tcphoff < 0)
Patrick McHardycdd289a2007-02-07 15:09:46 -0800179 return NF_DROP;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700180 ret = tcpmss_mangle_packet(skb, targinfo, tcphoff,
Patrick McHardycdd289a2007-02-07 15:09:46 -0800181 sizeof(*ipv6h) + sizeof(struct tcphdr));
182 if (ret < 0)
183 return NF_DROP;
184 if (ret > 0) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700185 ipv6h = ipv6_hdr(skb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800186 ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret);
187 }
188 return XT_CONTINUE;
189}
190#endif
191
192#define TH_SYN 0x02
193
194/* Must specify -p tcp --syn */
Jan Engelhardte1931b72007-07-07 22:16:26 -0700195static inline bool find_syn_match(const struct xt_entry_match *m)
Patrick McHardycdd289a2007-02-07 15:09:46 -0800196{
197 const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data;
198
199 if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
200 tcpinfo->flg_cmp & TH_SYN &&
201 !(tcpinfo->invflags & XT_TCP_INV_FLAGS))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700202 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800203
Jan Engelhardte1931b72007-07-07 22:16:26 -0700204 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800205}
206
Jan Engelhardte1931b72007-07-07 22:16:26 -0700207static bool
Patrick McHardycdd289a2007-02-07 15:09:46 -0800208xt_tcpmss_checkentry4(const char *tablename,
209 const void *entry,
210 const struct xt_target *target,
211 void *targinfo,
212 unsigned int hook_mask)
213{
214 const struct xt_tcpmss_info *info = targinfo;
215 const struct ipt_entry *e = entry;
216
217 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800218 (hook_mask & ~((1 << NF_INET_FORWARD) |
219 (1 << NF_INET_LOCAL_OUT) |
220 (1 << NF_INET_POST_ROUTING))) != 0) {
Patrick McHardycdd289a2007-02-07 15:09:46 -0800221 printk("xt_TCPMSS: path-MTU clamping only supported in "
222 "FORWARD, OUTPUT and POSTROUTING hooks\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700223 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800224 }
225 if (IPT_MATCH_ITERATE(e, find_syn_match))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700226 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800227 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700228 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800229}
230
231#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Jan Engelhardte1931b72007-07-07 22:16:26 -0700232static bool
Patrick McHardycdd289a2007-02-07 15:09:46 -0800233xt_tcpmss_checkentry6(const char *tablename,
234 const void *entry,
235 const struct xt_target *target,
236 void *targinfo,
237 unsigned int hook_mask)
238{
239 const struct xt_tcpmss_info *info = targinfo;
240 const struct ip6t_entry *e = entry;
241
242 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800243 (hook_mask & ~((1 << NF_INET_FORWARD) |
244 (1 << NF_INET_LOCAL_OUT) |
245 (1 << NF_INET_POST_ROUTING))) != 0) {
Patrick McHardycdd289a2007-02-07 15:09:46 -0800246 printk("xt_TCPMSS: path-MTU clamping only supported in "
247 "FORWARD, OUTPUT and POSTROUTING hooks\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700248 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800249 }
250 if (IP6T_MATCH_ITERATE(e, find_syn_match))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700251 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800252 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700253 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800254}
255#endif
256
Patrick McHardy9f15c532007-07-07 22:22:02 -0700257static struct xt_target xt_tcpmss_reg[] __read_mostly = {
Patrick McHardycdd289a2007-02-07 15:09:46 -0800258 {
259 .family = AF_INET,
260 .name = "TCPMSS",
261 .checkentry = xt_tcpmss_checkentry4,
262 .target = xt_tcpmss_target4,
263 .targetsize = sizeof(struct xt_tcpmss_info),
264 .proto = IPPROTO_TCP,
265 .me = THIS_MODULE,
266 },
267#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
268 {
269 .family = AF_INET6,
270 .name = "TCPMSS",
271 .checkentry = xt_tcpmss_checkentry6,
272 .target = xt_tcpmss_target6,
273 .targetsize = sizeof(struct xt_tcpmss_info),
274 .proto = IPPROTO_TCP,
275 .me = THIS_MODULE,
276 },
277#endif
278};
279
280static int __init xt_tcpmss_init(void)
281{
282 return xt_register_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
283}
284
285static void __exit xt_tcpmss_fini(void)
286{
287 xt_unregister_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
288}
289
290module_init(xt_tcpmss_init);
291module_exit(xt_tcpmss_fini);