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