MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / net / ipv4 / netfilter / ipt_TCPMSS.c
blob8207882fd5344d05d2f7ac4636590c91939af0e7
1 /*
2 * This is a module which is used for setting the MSS option in TCP packets.
4 * Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
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 */
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <net/tcp.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv4/ipt_TCPMSS.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22 MODULE_DESCRIPTION("iptables TCP MSS modification module");
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(format, args...)
28 #endif
30 #define HDRSIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
32 static u_int16_t
33 cheat_check(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck)
35 u_int32_t diffs[] = { oldvalinv, newval };
36 return csum_fold(csum_partial((char *)diffs, sizeof(diffs),
37 oldcheck^0xFFFF));
40 static inline unsigned int
41 optlen(const u_int8_t *opt, unsigned int offset)
43 /* Beware zero-length options: make finite progress */
44 if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
45 return 1;
46 else
47 return opt[offset+1];
50 static unsigned int
51 ipt_tcpmss_target(struct sk_buff **pskb,
52 const struct net_device *in,
53 const struct net_device *out,
54 unsigned int hooknum,
55 const struct xt_target *target,
56 const void *targinfo)
58 const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
59 struct tcphdr *tcph;
60 struct iphdr *iph;
61 u_int16_t tcplen, newmss;
62 __be16 newtotlen, oldval;
63 unsigned int i;
64 u_int8_t *opt;
66 if (!skb_make_writable(pskb, (*pskb)->len))
67 return NF_DROP;
69 iph = (*pskb)->nh.iph;
70 tcplen = (*pskb)->len - iph->ihl*4;
71 tcph = (void *)iph + iph->ihl*4;
73 /* Since it passed flags test in tcp match, we know it is is
74 not a fragment, and has data >= tcp header length. SYN
75 packets should not contain data: if they did, then we risk
76 running over MTU, sending Frag Needed and breaking things
77 badly. --RR */
78 if (tcplen != tcph->doff*4) {
79 if (net_ratelimit())
80 printk(KERN_ERR
81 "ipt_tcpmss_target: bad length (%d bytes)\n",
82 (*pskb)->len);
83 return IPT_CONTINUE;
86 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) {
87 if (dst_mtu((*pskb)->dst) <= sizeof(struct iphdr) +
88 sizeof(struct tcphdr)) {
89 if (net_ratelimit())
90 printk(KERN_ERR "ipt_tcpmss_target: "
91 "unknown or invalid path-MTU (%d)\n",
92 dst_mtu((*pskb)->dst));
93 return NF_DROP; /* or IPT_CONTINUE ?? */
96 newmss = dst_mtu((*pskb)->dst) - sizeof(struct iphdr) -
97 sizeof(struct tcphdr);
98 } else
99 newmss = tcpmssinfo->mss;
101 opt = (u_int8_t *)tcph;
102 for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) {
103 if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS &&
104 opt[i+1] == TCPOLEN_MSS) {
105 u_int16_t oldmss;
107 oldmss = (opt[i+2] << 8) | opt[i+3];
109 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
110 oldmss <= newmss)
111 return IPT_CONTINUE;
113 opt[i+2] = (newmss & 0xff00) >> 8;
114 opt[i+3] = (newmss & 0x00ff);
116 tcph->check = nf_proto_csum_update(*pskb,
117 htons(oldmss)^htons(0xFFFF),
118 htons(newmss),
119 tcph->check, 0);
120 return IPT_CONTINUE;
125 * MSS Option not found ?! add it..
127 if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
128 struct sk_buff *newskb;
130 newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
131 TCPOLEN_MSS, GFP_ATOMIC);
132 if (!newskb)
133 return NF_DROP;
134 kfree_skb(*pskb);
135 *pskb = newskb;
136 iph = (*pskb)->nh.iph;
137 tcph = (void *)iph + iph->ihl*4;
140 skb_put((*pskb), TCPOLEN_MSS);
142 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
143 memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
145 tcph->check = nf_proto_csum_update(*pskb,
146 htons(tcplen) ^ htons(0xFFFF),
147 htons(tcplen + TCPOLEN_MSS),
148 tcph->check, 1);
149 opt[0] = TCPOPT_MSS;
150 opt[1] = TCPOLEN_MSS;
151 opt[2] = (newmss & 0xff00) >> 8;
152 opt[3] = (newmss & 0x00ff);
154 tcph->check = nf_proto_csum_update(*pskb, htonl(~0), *((__be32 *)opt),
155 tcph->check, 0);
157 oldval = ((__be16 *)tcph)[6];
158 tcph->doff += TCPOLEN_MSS/4;
159 tcph->check = nf_proto_csum_update(*pskb,
160 oldval ^ htons(0xFFFF),
161 ((__be16 *)tcph)[6],
162 tcph->check, 0);
164 newtotlen = htons(ntohs(iph->tot_len) + TCPOLEN_MSS);
165 iph->check = nf_csum_update(iph->tot_len ^ htons(0xFFFF),
166 newtotlen, iph->check);
167 iph->tot_len = newtotlen;
168 return IPT_CONTINUE;
171 #define TH_SYN 0x02
173 static inline int find_syn_match(const struct ipt_entry_match *m)
175 const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
177 if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
178 tcpinfo->flg_cmp & TH_SYN &&
179 !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
180 return 1;
182 return 0;
185 /* Must specify -p tcp --syn/--tcp-flags SYN */
186 static int
187 ipt_tcpmss_checkentry(const char *tablename,
188 const void *e_void,
189 const struct xt_target *target,
190 void *targinfo,
191 unsigned int hook_mask)
193 const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
194 const struct ipt_entry *e = e_void;
196 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
197 (hook_mask & ~((1 << NF_IP_FORWARD) |
198 (1 << NF_IP_LOCAL_OUT) |
199 (1 << NF_IP_POST_ROUTING))) != 0) {
200 printk("TCPMSS: path-MTU clamping only supported in "
201 "FORWARD, OUTPUT and POSTROUTING hooks\n");
202 return 0;
205 if (IPT_MATCH_ITERATE(e, find_syn_match))
206 return 1;
207 printk("TCPMSS: Only works on TCP SYN packets\n");
208 return 0;
211 static struct ipt_target ipt_tcpmss_reg = {
212 .name = "TCPMSS",
213 .target = ipt_tcpmss_target,
214 .targetsize = sizeof(struct ipt_tcpmss_info),
215 .proto = IPPROTO_TCP,
216 .checkentry = ipt_tcpmss_checkentry,
217 .me = THIS_MODULE,
220 static int __init ipt_tcpmss_init(void)
222 return ipt_register_target(&ipt_tcpmss_reg);
225 static void __exit ipt_tcpmss_fini(void)
227 ipt_unregister_target(&ipt_tcpmss_reg);
230 module_init(ipt_tcpmss_init);
231 module_exit(ipt_tcpmss_fini);