OSDN Git Service

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