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