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