Merge branch 'master'
[linux-2.6] / net / ipv4 / netfilter / ipt_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
14 #include <linux/ip.h>
15 #include <net/tcp.h>
16
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv4/ipt_TCPMSS.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22 MODULE_DESCRIPTION("iptables TCP MSS modification module");
23
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(format, args...)
28 #endif
29
30 static u_int16_t
31 cheat_check(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck)
32 {
33         u_int32_t diffs[] = { oldvalinv, newval };
34         return csum_fold(csum_partial((char *)diffs, sizeof(diffs),
35                                       oldcheck^0xFFFF));
36 }
37
38 static inline unsigned int
39 optlen(const u_int8_t *opt, unsigned int offset)
40 {
41         /* Beware zero-length options: make finite progress */
42         if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0) return 1;
43         else return opt[offset+1];
44 }
45
46 static unsigned int
47 ipt_tcpmss_target(struct sk_buff **pskb,
48                   const struct net_device *in,
49                   const struct net_device *out,
50                   unsigned int hooknum,
51                   const struct xt_target *target,
52                   const void *targinfo,
53                   void *userinfo)
54 {
55         const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
56         struct tcphdr *tcph;
57         struct iphdr *iph;
58         u_int16_t tcplen, newtotlen, oldval, newmss;
59         unsigned int i;
60         u_int8_t *opt;
61
62         if (!skb_make_writable(pskb, (*pskb)->len))
63                 return NF_DROP;
64
65         if ((*pskb)->ip_summed == CHECKSUM_HW &&
66             skb_checksum_help(*pskb, out == NULL))
67                 return NF_DROP;
68
69         iph = (*pskb)->nh.iph;
70         tcplen = (*pskb)->len - iph->ihl*4;
71
72         tcph = (void *)iph + iph->ihl*4;
73
74         /* Since it passed flags test in tcp match, we know it is is
75            not a fragment, and has data >= tcp header length.  SYN
76            packets should not contain data: if they did, then we risk
77            running over MTU, sending Frag Needed and breaking things
78            badly. --RR */
79         if (tcplen != tcph->doff*4) {
80                 if (net_ratelimit())
81                         printk(KERN_ERR
82                                "ipt_tcpmss_target: bad length (%d bytes)\n",
83                                (*pskb)->len);
84                 return NF_DROP;
85         }
86
87         if(tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) {
88                 if(!(*pskb)->dst) {
89                         if (net_ratelimit())
90                                 printk(KERN_ERR
91                                         "ipt_tcpmss_target: no dst?! can't determine path-MTU\n");
92                         return NF_DROP; /* or IPT_CONTINUE ?? */
93                 }
94
95                 if(dst_mtu((*pskb)->dst) <= (sizeof(struct iphdr) + sizeof(struct tcphdr))) {
96                         if (net_ratelimit())
97                                 printk(KERN_ERR
98                                         "ipt_tcpmss_target: unknown or invalid path-MTU (%d)\n", dst_mtu((*pskb)->dst));
99                         return NF_DROP; /* or IPT_CONTINUE ?? */
100                 }
101
102                 newmss = dst_mtu((*pskb)->dst) - sizeof(struct iphdr) - sizeof(struct tcphdr);
103         } else
104                 newmss = tcpmssinfo->mss;
105
106         opt = (u_int8_t *)tcph;
107         for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)){
108                 if ((opt[i] == TCPOPT_MSS) &&
109                     ((tcph->doff*4 - i) >= TCPOLEN_MSS) &&
110                     (opt[i+1] == TCPOLEN_MSS)) {
111                         u_int16_t oldmss;
112
113                         oldmss = (opt[i+2] << 8) | opt[i+3];
114
115                         if((tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) &&
116                                 (oldmss <= newmss))
117                                         return IPT_CONTINUE;
118
119                         opt[i+2] = (newmss & 0xff00) >> 8;
120                         opt[i+3] = (newmss & 0x00ff);
121
122                         tcph->check = cheat_check(htons(oldmss)^0xFFFF,
123                                                   htons(newmss),
124                                                   tcph->check);
125
126                         DEBUGP(KERN_INFO "ipt_tcpmss_target: %u.%u.%u.%u:%hu"
127                                "->%u.%u.%u.%u:%hu changed TCP MSS option"
128                                " (from %u to %u)\n", 
129                                NIPQUAD((*pskb)->nh.iph->saddr),
130                                ntohs(tcph->source),
131                                NIPQUAD((*pskb)->nh.iph->daddr),
132                                ntohs(tcph->dest),
133                                oldmss, newmss);
134                         goto retmodified;
135                 }
136         }
137
138         /*
139          * MSS Option not found ?! add it..
140          */
141         if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
142                 struct sk_buff *newskb;
143
144                 newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
145                                          TCPOLEN_MSS, GFP_ATOMIC);
146                 if (!newskb) {
147                         if (net_ratelimit())
148                                 printk(KERN_ERR "ipt_tcpmss_target:"
149                                        " unable to allocate larger skb\n");
150                         return NF_DROP;
151                 }
152
153                 kfree_skb(*pskb);
154                 *pskb = newskb;
155                 iph = (*pskb)->nh.iph;
156                 tcph = (void *)iph + iph->ihl*4;
157         }
158
159         skb_put((*pskb), TCPOLEN_MSS);
160
161         opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
162         memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
163
164         tcph->check = cheat_check(htons(tcplen) ^ 0xFFFF,
165                                   htons(tcplen + TCPOLEN_MSS), tcph->check);
166         tcplen += TCPOLEN_MSS;
167
168         opt[0] = TCPOPT_MSS;
169         opt[1] = TCPOLEN_MSS;
170         opt[2] = (newmss & 0xff00) >> 8;
171         opt[3] = (newmss & 0x00ff);
172
173         tcph->check = cheat_check(~0, *((u_int32_t *)opt), tcph->check);
174
175         oldval = ((u_int16_t *)tcph)[6];
176         tcph->doff += TCPOLEN_MSS/4;
177         tcph->check = cheat_check(oldval ^ 0xFFFF,
178                                   ((u_int16_t *)tcph)[6], tcph->check);
179
180         newtotlen = htons(ntohs(iph->tot_len) + TCPOLEN_MSS);
181         iph->check = cheat_check(iph->tot_len ^ 0xFFFF,
182                                  newtotlen, iph->check);
183         iph->tot_len = newtotlen;
184
185         DEBUGP(KERN_INFO "ipt_tcpmss_target: %u.%u.%u.%u:%hu"
186                "->%u.%u.%u.%u:%hu added TCP MSS option (%u)\n",
187                NIPQUAD((*pskb)->nh.iph->saddr),
188                ntohs(tcph->source),
189                NIPQUAD((*pskb)->nh.iph->daddr),
190                ntohs(tcph->dest),
191                newmss);
192
193  retmodified:
194         return IPT_CONTINUE;
195 }
196
197 #define TH_SYN 0x02
198
199 static inline int find_syn_match(const struct ipt_entry_match *m)
200 {
201         const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
202
203         if (strcmp(m->u.kernel.match->name, "tcp") == 0
204             && (tcpinfo->flg_cmp & TH_SYN)
205             && !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
206                 return 1;
207
208         return 0;
209 }
210
211 /* Must specify -p tcp --syn/--tcp-flags SYN */
212 static int
213 ipt_tcpmss_checkentry(const char *tablename,
214                       const void *e_void,
215                       const struct xt_target *target,
216                       void *targinfo,
217                       unsigned int targinfosize,
218                       unsigned int hook_mask)
219 {
220         const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
221         const struct ipt_entry *e = e_void;
222
223         if((tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) && 
224                         ((hook_mask & ~((1 << NF_IP_FORWARD)
225                                 | (1 << NF_IP_LOCAL_OUT)
226                                 | (1 << NF_IP_POST_ROUTING))) != 0)) {
227                 printk("TCPMSS: path-MTU clamping only supported in FORWARD, OUTPUT and POSTROUTING hooks\n");
228                 return 0;
229         }
230
231         if (IPT_MATCH_ITERATE(e, find_syn_match))
232                 return 1;
233         printk("TCPMSS: Only works on TCP SYN packets\n");
234         return 0;
235 }
236
237 static struct ipt_target ipt_tcpmss_reg = {
238         .name           = "TCPMSS",
239         .target         = ipt_tcpmss_target,
240         .targetsize     = sizeof(struct ipt_tcpmss_info),
241         .proto          = IPPROTO_TCP,
242         .checkentry     = ipt_tcpmss_checkentry,
243         .me             = THIS_MODULE,
244 };
245
246 static int __init ipt_tcpmss_init(void)
247 {
248         return ipt_register_target(&ipt_tcpmss_reg);
249 }
250
251 static void __exit ipt_tcpmss_fini(void)
252 {
253         ipt_unregister_target(&ipt_tcpmss_reg);
254 }
255
256 module_init(ipt_tcpmss_init);
257 module_exit(ipt_tcpmss_fini);