blob: f111edf5f7754516a7743bae3064958ad0010d65 [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
42tcpmss_mangle_packet(struct sk_buff **pskb,
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
Herbert Xu37d41872007-10-14 00:39:18 -070053 if (!skb_make_writable(*pskb, (*pskb)->len))
Patrick McHardycdd289a2007-02-07 15:09:46 -080054 return -1;
55
56 tcplen = (*pskb)->len - tcphoff;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070057 tcph = (struct tcphdr *)(skb_network_header(*pskb) + 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",
67 (*pskb)->len);
68 return -1;
69 }
70
71 if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
72 if (dst_mtu((*pskb)->dst) <= minlen) {
73 if (net_ratelimit())
74 printk(KERN_ERR "xt_TCPMSS: "
75 "unknown or invalid path-MTU (%u)\n",
76 dst_mtu((*pskb)->dst));
77 return -1;
78 }
79 newmss = dst_mtu((*pskb)->dst) - minlen;
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;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070096 opt[i+3] = newmss & 0x00ff;
Patrick McHardycdd289a2007-02-07 15:09:46 -080097
98 nf_proto_csum_replace2(&tcph->check, *pskb,
99 htons(oldmss), htons(newmss), 0);
100 return 0;
101 }
102 }
103
104 /*
105 * MSS Option not found ?! add it..
106 */
107 if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700108 if (pskb_expand_head(*pskb, 0,
109 TCPOLEN_MSS - skb_tailroom(*pskb),
110 GFP_ATOMIC))
Patrick McHardycdd289a2007-02-07 15:09:46 -0800111 return -1;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700112 tcph = (struct tcphdr *)(skb_network_header(*pskb) + tcphoff);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800113 }
114
115 skb_put((*pskb), TCPOLEN_MSS);
116
117 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
118 memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
119
120 nf_proto_csum_replace2(&tcph->check, *pskb,
121 htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1);
122 opt[0] = TCPOPT_MSS;
123 opt[1] = TCPOLEN_MSS;
124 opt[2] = (newmss & 0xff00) >> 8;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700125 opt[3] = newmss & 0x00ff;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800126
127 nf_proto_csum_replace4(&tcph->check, *pskb, 0, *((__be32 *)opt), 0);
128
129 oldval = ((__be16 *)tcph)[6];
130 tcph->doff += TCPOLEN_MSS/4;
131 nf_proto_csum_replace2(&tcph->check, *pskb,
132 oldval, ((__be16 *)tcph)[6], 0);
133 return TCPOLEN_MSS;
134}
135
136static unsigned int
137xt_tcpmss_target4(struct sk_buff **pskb,
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{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700144 struct iphdr *iph = ip_hdr(*pskb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800145 __be16 newlen;
146 int ret;
147
148 ret = tcpmss_mangle_packet(pskb, targinfo, iph->ihl * 4,
149 sizeof(*iph) + sizeof(struct tcphdr));
150 if (ret < 0)
151 return NF_DROP;
152 if (ret > 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700153 iph = ip_hdr(*pskb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800154 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)
162static unsigned int
163xt_tcpmss_target6(struct sk_buff **pskb,
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{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700170 struct ipv6hdr *ipv6h = ipv6_hdr(*pskb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800171 u8 nexthdr;
172 int tcphoff;
173 int ret;
174
175 nexthdr = ipv6h->nexthdr;
176 tcphoff = ipv6_skip_exthdr(*pskb, sizeof(*ipv6h), &nexthdr);
177 if (tcphoff < 0) {
178 WARN_ON(1);
179 return NF_DROP;
180 }
181 ret = tcpmss_mangle_packet(pskb, targinfo, tcphoff,
182 sizeof(*ipv6h) + sizeof(struct tcphdr));
183 if (ret < 0)
184 return NF_DROP;
185 if (ret > 0) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700186 ipv6h = ipv6_hdr(*pskb);
Patrick McHardycdd289a2007-02-07 15:09:46 -0800187 ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret);
188 }
189 return XT_CONTINUE;
190}
191#endif
192
193#define TH_SYN 0x02
194
195/* Must specify -p tcp --syn */
Jan Engelhardte1931b72007-07-07 22:16:26 -0700196static inline bool find_syn_match(const struct xt_entry_match *m)
Patrick McHardycdd289a2007-02-07 15:09:46 -0800197{
198 const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data;
199
200 if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
201 tcpinfo->flg_cmp & TH_SYN &&
202 !(tcpinfo->invflags & XT_TCP_INV_FLAGS))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700203 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800204
Jan Engelhardte1931b72007-07-07 22:16:26 -0700205 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800206}
207
Jan Engelhardte1931b72007-07-07 22:16:26 -0700208static bool
Patrick McHardycdd289a2007-02-07 15:09:46 -0800209xt_tcpmss_checkentry4(const char *tablename,
210 const void *entry,
211 const struct xt_target *target,
212 void *targinfo,
213 unsigned int hook_mask)
214{
215 const struct xt_tcpmss_info *info = targinfo;
216 const struct ipt_entry *e = entry;
217
218 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
219 (hook_mask & ~((1 << NF_IP_FORWARD) |
220 (1 << NF_IP_LOCAL_OUT) |
221 (1 << NF_IP_POST_ROUTING))) != 0) {
222 printk("xt_TCPMSS: path-MTU clamping only supported in "
223 "FORWARD, OUTPUT and POSTROUTING hooks\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700224 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800225 }
226 if (IPT_MATCH_ITERATE(e, find_syn_match))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700227 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800228 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700229 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800230}
231
232#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Jan Engelhardte1931b72007-07-07 22:16:26 -0700233static bool
Patrick McHardycdd289a2007-02-07 15:09:46 -0800234xt_tcpmss_checkentry6(const char *tablename,
235 const void *entry,
236 const struct xt_target *target,
237 void *targinfo,
238 unsigned int hook_mask)
239{
240 const struct xt_tcpmss_info *info = targinfo;
241 const struct ip6t_entry *e = entry;
242
243 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
244 (hook_mask & ~((1 << NF_IP6_FORWARD) |
245 (1 << NF_IP6_LOCAL_OUT) |
246 (1 << NF_IP6_POST_ROUTING))) != 0) {
247 printk("xt_TCPMSS: path-MTU clamping only supported in "
248 "FORWARD, OUTPUT and POSTROUTING hooks\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700249 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800250 }
251 if (IP6T_MATCH_ITERATE(e, find_syn_match))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700252 return true;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800253 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700254 return false;
Patrick McHardycdd289a2007-02-07 15:09:46 -0800255}
256#endif
257
Patrick McHardy9f15c532007-07-07 22:22:02 -0700258static struct xt_target xt_tcpmss_reg[] __read_mostly = {
Patrick McHardycdd289a2007-02-07 15:09:46 -0800259 {
260 .family = AF_INET,
261 .name = "TCPMSS",
262 .checkentry = xt_tcpmss_checkentry4,
263 .target = xt_tcpmss_target4,
264 .targetsize = sizeof(struct xt_tcpmss_info),
265 .proto = IPPROTO_TCP,
266 .me = THIS_MODULE,
267 },
268#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
269 {
270 .family = AF_INET6,
271 .name = "TCPMSS",
272 .checkentry = xt_tcpmss_checkentry6,
273 .target = xt_tcpmss_target6,
274 .targetsize = sizeof(struct xt_tcpmss_info),
275 .proto = IPPROTO_TCP,
276 .me = THIS_MODULE,
277 },
278#endif
279};
280
281static int __init xt_tcpmss_init(void)
282{
283 return xt_register_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
284}
285
286static void __exit xt_tcpmss_fini(void)
287{
288 xt_unregister_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
289}
290
291module_init(xt_tcpmss_init);
292module_exit(xt_tcpmss_fini);