Merge branch 'linux-2.6' into for-2.6.24
[linux-2.6] / net / netfilter / xt_TCPMSS.c
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
42 tcpmss_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
53         if (!skb_make_writable(pskb, (*pskb)->len))
54                 return -1;
55
56         tcplen = (*pskb)->len - tcphoff;
57         tcph = (struct tcphdr *)(skb_network_header(*pskb) + tcphoff);
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",
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;
96                         opt[i+3] = newmss & 0x00ff;
97
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) {
108                 struct sk_buff *newskb;
109
110                 newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
111                                          TCPOLEN_MSS, GFP_ATOMIC);
112                 if (!newskb)
113                         return -1;
114                 kfree_skb(*pskb);
115                 *pskb = newskb;
116                 tcph = (struct tcphdr *)(skb_network_header(*pskb) + tcphoff);
117         }
118
119         skb_put((*pskb), TCPOLEN_MSS);
120
121         opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
122         memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
123
124         nf_proto_csum_replace2(&tcph->check, *pskb,
125                                htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1);
126         opt[0] = TCPOPT_MSS;
127         opt[1] = TCPOLEN_MSS;
128         opt[2] = (newmss & 0xff00) >> 8;
129         opt[3] = newmss & 0x00ff;
130
131         nf_proto_csum_replace4(&tcph->check, *pskb, 0, *((__be32 *)opt), 0);
132
133         oldval = ((__be16 *)tcph)[6];
134         tcph->doff += TCPOLEN_MSS/4;
135         nf_proto_csum_replace2(&tcph->check, *pskb,
136                                 oldval, ((__be16 *)tcph)[6], 0);
137         return TCPOLEN_MSS;
138 }
139
140 static unsigned int
141 xt_tcpmss_target4(struct sk_buff **pskb,
142                   const struct net_device *in,
143                   const struct net_device *out,
144                   unsigned int hooknum,
145                   const struct xt_target *target,
146                   const void *targinfo)
147 {
148         struct iphdr *iph = ip_hdr(*pskb);
149         __be16 newlen;
150         int ret;
151
152         ret = tcpmss_mangle_packet(pskb, targinfo, iph->ihl * 4,
153                                    sizeof(*iph) + sizeof(struct tcphdr));
154         if (ret < 0)
155                 return NF_DROP;
156         if (ret > 0) {
157                 iph = ip_hdr(*pskb);
158                 newlen = htons(ntohs(iph->tot_len) + ret);
159                 nf_csum_replace2(&iph->check, iph->tot_len, newlen);
160                 iph->tot_len = newlen;
161         }
162         return XT_CONTINUE;
163 }
164
165 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
166 static unsigned int
167 xt_tcpmss_target6(struct sk_buff **pskb,
168                   const struct net_device *in,
169                   const struct net_device *out,
170                   unsigned int hooknum,
171                   const struct xt_target *target,
172                   const void *targinfo)
173 {
174         struct ipv6hdr *ipv6h = ipv6_hdr(*pskb);
175         u8 nexthdr;
176         int tcphoff;
177         int ret;
178
179         nexthdr = ipv6h->nexthdr;
180         tcphoff = ipv6_skip_exthdr(*pskb, sizeof(*ipv6h), &nexthdr);
181         if (tcphoff < 0) {
182                 WARN_ON(1);
183                 return NF_DROP;
184         }
185         ret = tcpmss_mangle_packet(pskb, targinfo, tcphoff,
186                                    sizeof(*ipv6h) + sizeof(struct tcphdr));
187         if (ret < 0)
188                 return NF_DROP;
189         if (ret > 0) {
190                 ipv6h = ipv6_hdr(*pskb);
191                 ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret);
192         }
193         return XT_CONTINUE;
194 }
195 #endif
196
197 #define TH_SYN 0x02
198
199 /* Must specify -p tcp --syn */
200 static inline bool find_syn_match(const struct xt_entry_match *m)
201 {
202         const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data;
203
204         if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
205             tcpinfo->flg_cmp & TH_SYN &&
206             !(tcpinfo->invflags & XT_TCP_INV_FLAGS))
207                 return true;
208
209         return false;
210 }
211
212 static bool
213 xt_tcpmss_checkentry4(const char *tablename,
214                       const void *entry,
215                       const struct xt_target *target,
216                       void *targinfo,
217                       unsigned int hook_mask)
218 {
219         const struct xt_tcpmss_info *info = targinfo;
220         const struct ipt_entry *e = entry;
221
222         if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
223             (hook_mask & ~((1 << NF_IP_FORWARD) |
224                            (1 << NF_IP_LOCAL_OUT) |
225                            (1 << NF_IP_POST_ROUTING))) != 0) {
226                 printk("xt_TCPMSS: path-MTU clamping only supported in "
227                        "FORWARD, OUTPUT and POSTROUTING hooks\n");
228                 return false;
229         }
230         if (IPT_MATCH_ITERATE(e, find_syn_match))
231                 return true;
232         printk("xt_TCPMSS: Only works on TCP SYN packets\n");
233         return false;
234 }
235
236 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
237 static bool
238 xt_tcpmss_checkentry6(const char *tablename,
239                       const void *entry,
240                       const struct xt_target *target,
241                       void *targinfo,
242                       unsigned int hook_mask)
243 {
244         const struct xt_tcpmss_info *info = targinfo;
245         const struct ip6t_entry *e = entry;
246
247         if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
248             (hook_mask & ~((1 << NF_IP6_FORWARD) |
249                            (1 << NF_IP6_LOCAL_OUT) |
250                            (1 << NF_IP6_POST_ROUTING))) != 0) {
251                 printk("xt_TCPMSS: path-MTU clamping only supported in "
252                        "FORWARD, OUTPUT and POSTROUTING hooks\n");
253                 return false;
254         }
255         if (IP6T_MATCH_ITERATE(e, find_syn_match))
256                 return true;
257         printk("xt_TCPMSS: Only works on TCP SYN packets\n");
258         return false;
259 }
260 #endif
261
262 static struct xt_target xt_tcpmss_reg[] __read_mostly = {
263         {
264                 .family         = AF_INET,
265                 .name           = "TCPMSS",
266                 .checkentry     = xt_tcpmss_checkentry4,
267                 .target         = xt_tcpmss_target4,
268                 .targetsize     = sizeof(struct xt_tcpmss_info),
269                 .proto          = IPPROTO_TCP,
270                 .me             = THIS_MODULE,
271         },
272 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
273         {
274                 .family         = AF_INET6,
275                 .name           = "TCPMSS",
276                 .checkentry     = xt_tcpmss_checkentry6,
277                 .target         = xt_tcpmss_target6,
278                 .targetsize     = sizeof(struct xt_tcpmss_info),
279                 .proto          = IPPROTO_TCP,
280                 .me             = THIS_MODULE,
281         },
282 #endif
283 };
284
285 static int __init xt_tcpmss_init(void)
286 {
287         return xt_register_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
288 }
289
290 static void __exit xt_tcpmss_fini(void)
291 {
292         xt_unregister_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
293 }
294
295 module_init(xt_tcpmss_init);
296 module_exit(xt_tcpmss_fini);