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