Commit | Line | Data |
---|---|---|
cdd289a2 PM |
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 | #include <linux/ip.h> | |
14 | #include <linux/ipv6.h> | |
15 | #include <linux/tcp.h> | |
16 | #include <net/ipv6.h> | |
17 | #include <net/tcp.h> | |
18 | ||
19 | #include <linux/netfilter_ipv4/ip_tables.h> | |
20 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
21 | #include <linux/netfilter/x_tables.h> | |
22 | #include <linux/netfilter/xt_tcpudp.h> | |
23 | #include <linux/netfilter/xt_TCPMSS.h> | |
24 | ||
25 | MODULE_LICENSE("GPL"); | |
26 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); | |
27 | MODULE_DESCRIPTION("x_tables TCP MSS modification module"); | |
28 | MODULE_ALIAS("ipt_TCPMSS"); | |
29 | MODULE_ALIAS("ip6t_TCPMSS"); | |
30 | ||
31 | static inline unsigned int | |
32 | optlen(const u_int8_t *opt, unsigned int offset) | |
33 | { | |
34 | /* Beware zero-length options: make finite progress */ | |
35 | if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0) | |
36 | return 1; | |
37 | else | |
38 | return opt[offset+1]; | |
39 | } | |
40 | ||
41 | static int | |
3db05fea | 42 | tcpmss_mangle_packet(struct sk_buff *skb, |
cdd289a2 PM |
43 | const struct xt_tcpmss_info *info, |
44 | unsigned int tcphoff, | |
45 | unsigned int minlen) | |
46 | { | |
47 | struct tcphdr *tcph; | |
48 | unsigned int tcplen, i; | |
49 | __be16 oldval; | |
50 | u16 newmss; | |
51 | u8 *opt; | |
52 | ||
3db05fea | 53 | if (!skb_make_writable(skb, skb->len)) |
cdd289a2 PM |
54 | return -1; |
55 | ||
3db05fea HX |
56 | tcplen = skb->len - tcphoff; |
57 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); | |
cdd289a2 PM |
58 | |
59 | /* Since it passed flags test in tcp match, we know it is is | |
60 | not a fragment, and has data >= tcp header length. SYN | |
61 | packets should not contain data: if they did, then we risk | |
62 | running over MTU, sending Frag Needed and breaking things | |
63 | badly. --RR */ | |
64 | if (tcplen != tcph->doff*4) { | |
65 | if (net_ratelimit()) | |
66 | printk(KERN_ERR "xt_TCPMSS: bad length (%u bytes)\n", | |
3db05fea | 67 | skb->len); |
cdd289a2 PM |
68 | return -1; |
69 | } | |
70 | ||
71 | if (info->mss == XT_TCPMSS_CLAMP_PMTU) { | |
3db05fea | 72 | if (dst_mtu(skb->dst) <= minlen) { |
cdd289a2 PM |
73 | if (net_ratelimit()) |
74 | printk(KERN_ERR "xt_TCPMSS: " | |
75 | "unknown or invalid path-MTU (%u)\n", | |
3db05fea | 76 | dst_mtu(skb->dst)); |
cdd289a2 PM |
77 | return -1; |
78 | } | |
3db05fea | 79 | newmss = dst_mtu(skb->dst) - minlen; |
cdd289a2 PM |
80 | } else |
81 | newmss = info->mss; | |
82 | ||
83 | opt = (u_int8_t *)tcph; | |
84 | for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) { | |
85 | if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS && | |
86 | opt[i+1] == TCPOLEN_MSS) { | |
87 | u_int16_t oldmss; | |
88 | ||
89 | oldmss = (opt[i+2] << 8) | opt[i+3]; | |
90 | ||
91 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
92 | oldmss <= newmss) | |
93 | return 0; | |
94 | ||
95 | opt[i+2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 96 | opt[i+3] = newmss & 0x00ff; |
cdd289a2 | 97 | |
be0ea7d5 PM |
98 | inet_proto_csum_replace2(&tcph->check, skb, |
99 | htons(oldmss), htons(newmss), | |
100 | 0); | |
cdd289a2 PM |
101 | return 0; |
102 | } | |
103 | } | |
104 | ||
105 | /* | |
106 | * MSS Option not found ?! add it.. | |
107 | */ | |
3db05fea HX |
108 | if (skb_tailroom(skb) < TCPOLEN_MSS) { |
109 | if (pskb_expand_head(skb, 0, | |
110 | TCPOLEN_MSS - skb_tailroom(skb), | |
2ca7b0ac | 111 | GFP_ATOMIC)) |
cdd289a2 | 112 | return -1; |
3db05fea | 113 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
cdd289a2 PM |
114 | } |
115 | ||
3db05fea | 116 | skb_put(skb, TCPOLEN_MSS); |
cdd289a2 PM |
117 | |
118 | opt = (u_int8_t *)tcph + sizeof(struct tcphdr); | |
119 | memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr)); | |
120 | ||
be0ea7d5 PM |
121 | inet_proto_csum_replace2(&tcph->check, skb, |
122 | htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1); | |
cdd289a2 PM |
123 | opt[0] = TCPOPT_MSS; |
124 | opt[1] = TCPOLEN_MSS; | |
125 | opt[2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 126 | opt[3] = newmss & 0x00ff; |
cdd289a2 | 127 | |
be0ea7d5 | 128 | inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0); |
cdd289a2 PM |
129 | |
130 | oldval = ((__be16 *)tcph)[6]; | |
131 | tcph->doff += TCPOLEN_MSS/4; | |
be0ea7d5 PM |
132 | inet_proto_csum_replace2(&tcph->check, skb, |
133 | oldval, ((__be16 *)tcph)[6], 0); | |
cdd289a2 PM |
134 | return TCPOLEN_MSS; |
135 | } | |
136 | ||
137 | static unsigned int | |
d3c5ee6d JE |
138 | tcpmss_tg4(struct sk_buff *skb, const struct net_device *in, |
139 | const struct net_device *out, unsigned int hooknum, | |
140 | const struct xt_target *target, const void *targinfo) | |
cdd289a2 | 141 | { |
3db05fea | 142 | struct iphdr *iph = ip_hdr(skb); |
cdd289a2 PM |
143 | __be16 newlen; |
144 | int ret; | |
145 | ||
3db05fea | 146 | ret = tcpmss_mangle_packet(skb, targinfo, iph->ihl * 4, |
cdd289a2 PM |
147 | sizeof(*iph) + sizeof(struct tcphdr)); |
148 | if (ret < 0) | |
149 | return NF_DROP; | |
150 | if (ret > 0) { | |
3db05fea | 151 | iph = ip_hdr(skb); |
cdd289a2 | 152 | newlen = htons(ntohs(iph->tot_len) + ret); |
be0ea7d5 | 153 | csum_replace2(&iph->check, iph->tot_len, newlen); |
cdd289a2 PM |
154 | iph->tot_len = newlen; |
155 | } | |
156 | return XT_CONTINUE; | |
157 | } | |
158 | ||
159 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
160 | static unsigned int | |
d3c5ee6d JE |
161 | tcpmss_tg6(struct sk_buff *skb, const struct net_device *in, |
162 | const struct net_device *out, unsigned int hooknum, | |
163 | const struct xt_target *target, const void *targinfo) | |
cdd289a2 | 164 | { |
3db05fea | 165 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
cdd289a2 PM |
166 | u8 nexthdr; |
167 | int tcphoff; | |
168 | int ret; | |
169 | ||
170 | nexthdr = ipv6h->nexthdr; | |
3db05fea | 171 | tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr); |
9dc0564e | 172 | if (tcphoff < 0) |
cdd289a2 | 173 | return NF_DROP; |
3db05fea | 174 | ret = tcpmss_mangle_packet(skb, targinfo, tcphoff, |
cdd289a2 PM |
175 | sizeof(*ipv6h) + sizeof(struct tcphdr)); |
176 | if (ret < 0) | |
177 | return NF_DROP; | |
178 | if (ret > 0) { | |
3db05fea | 179 | ipv6h = ipv6_hdr(skb); |
cdd289a2 PM |
180 | ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret); |
181 | } | |
182 | return XT_CONTINUE; | |
183 | } | |
184 | #endif | |
185 | ||
186 | #define TH_SYN 0x02 | |
187 | ||
188 | /* Must specify -p tcp --syn */ | |
e1931b78 | 189 | static inline bool find_syn_match(const struct xt_entry_match *m) |
cdd289a2 PM |
190 | { |
191 | const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data; | |
192 | ||
193 | if (strcmp(m->u.kernel.match->name, "tcp") == 0 && | |
194 | tcpinfo->flg_cmp & TH_SYN && | |
195 | !(tcpinfo->invflags & XT_TCP_INV_FLAGS)) | |
e1931b78 | 196 | return true; |
cdd289a2 | 197 | |
e1931b78 | 198 | return false; |
cdd289a2 PM |
199 | } |
200 | ||
e1931b78 | 201 | static bool |
d3c5ee6d JE |
202 | tcpmss_tg4_check(const char *tablename, const void *entry, |
203 | const struct xt_target *target, void *targinfo, | |
204 | unsigned int hook_mask) | |
cdd289a2 PM |
205 | { |
206 | const struct xt_tcpmss_info *info = targinfo; | |
207 | const struct ipt_entry *e = entry; | |
208 | ||
209 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
6e23ae2a PM |
210 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
211 | (1 << NF_INET_LOCAL_OUT) | | |
212 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
cdd289a2 PM |
213 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
214 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); | |
e1931b78 | 215 | return false; |
cdd289a2 PM |
216 | } |
217 | if (IPT_MATCH_ITERATE(e, find_syn_match)) | |
e1931b78 | 218 | return true; |
cdd289a2 | 219 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
e1931b78 | 220 | return false; |
cdd289a2 PM |
221 | } |
222 | ||
223 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
e1931b78 | 224 | static bool |
d3c5ee6d JE |
225 | tcpmss_tg6_check(const char *tablename, const void *entry, |
226 | const struct xt_target *target, void *targinfo, | |
227 | unsigned int hook_mask) | |
cdd289a2 PM |
228 | { |
229 | const struct xt_tcpmss_info *info = targinfo; | |
230 | const struct ip6t_entry *e = entry; | |
231 | ||
232 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
6e23ae2a PM |
233 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
234 | (1 << NF_INET_LOCAL_OUT) | | |
235 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
cdd289a2 PM |
236 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
237 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); | |
e1931b78 | 238 | return false; |
cdd289a2 PM |
239 | } |
240 | if (IP6T_MATCH_ITERATE(e, find_syn_match)) | |
e1931b78 | 241 | return true; |
cdd289a2 | 242 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
e1931b78 | 243 | return false; |
cdd289a2 PM |
244 | } |
245 | #endif | |
246 | ||
d3c5ee6d | 247 | static struct xt_target tcpmss_tg_reg[] __read_mostly = { |
cdd289a2 PM |
248 | { |
249 | .family = AF_INET, | |
250 | .name = "TCPMSS", | |
d3c5ee6d JE |
251 | .checkentry = tcpmss_tg4_check, |
252 | .target = tcpmss_tg4, | |
cdd289a2 PM |
253 | .targetsize = sizeof(struct xt_tcpmss_info), |
254 | .proto = IPPROTO_TCP, | |
255 | .me = THIS_MODULE, | |
256 | }, | |
257 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
258 | { | |
259 | .family = AF_INET6, | |
260 | .name = "TCPMSS", | |
d3c5ee6d JE |
261 | .checkentry = tcpmss_tg6_check, |
262 | .target = tcpmss_tg6, | |
cdd289a2 PM |
263 | .targetsize = sizeof(struct xt_tcpmss_info), |
264 | .proto = IPPROTO_TCP, | |
265 | .me = THIS_MODULE, | |
266 | }, | |
267 | #endif | |
268 | }; | |
269 | ||
d3c5ee6d | 270 | static int __init tcpmss_tg_init(void) |
cdd289a2 | 271 | { |
d3c5ee6d | 272 | return xt_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
cdd289a2 PM |
273 | } |
274 | ||
d3c5ee6d | 275 | static void __exit tcpmss_tg_exit(void) |
cdd289a2 | 276 | { |
d3c5ee6d | 277 | xt_unregister_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
cdd289a2 PM |
278 | } |
279 | ||
d3c5ee6d JE |
280 | module_init(tcpmss_tg_init); |
281 | module_exit(tcpmss_tg_exit); |