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> | |
f229f6ce | 5 | * Copyright (C) 2007 Patrick McHardy <kaber@trash.net> |
cdd289a2 PM |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
8bee4bad | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
cdd289a2 PM |
12 | #include <linux/module.h> |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/ip.h> | |
5a0e3ad6 | 15 | #include <linux/gfp.h> |
cdd289a2 PM |
16 | #include <linux/ipv6.h> |
17 | #include <linux/tcp.h> | |
37c08387 JE |
18 | #include <net/dst.h> |
19 | #include <net/flow.h> | |
cdd289a2 | 20 | #include <net/ipv6.h> |
37c08387 | 21 | #include <net/route.h> |
cdd289a2 PM |
22 | #include <net/tcp.h> |
23 | ||
24 | #include <linux/netfilter_ipv4/ip_tables.h> | |
25 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
26 | #include <linux/netfilter/x_tables.h> | |
27 | #include <linux/netfilter/xt_tcpudp.h> | |
28 | #include <linux/netfilter/xt_TCPMSS.h> | |
29 | ||
30 | MODULE_LICENSE("GPL"); | |
31 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); | |
2ae15b64 | 32 | MODULE_DESCRIPTION("Xtables: TCP Maximum Segment Size (MSS) adjustment"); |
cdd289a2 PM |
33 | MODULE_ALIAS("ipt_TCPMSS"); |
34 | MODULE_ALIAS("ip6t_TCPMSS"); | |
35 | ||
36 | static inline unsigned int | |
37 | optlen(const u_int8_t *opt, unsigned int offset) | |
38 | { | |
39 | /* Beware zero-length options: make finite progress */ | |
40 | if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0) | |
41 | return 1; | |
42 | else | |
43 | return opt[offset+1]; | |
44 | } | |
45 | ||
7722e0d1 G |
46 | static u_int32_t tcpmss_reverse_mtu(struct net *net, |
47 | const struct sk_buff *skb, | |
de1389b1 G |
48 | unsigned int family) |
49 | { | |
50 | struct flowi fl; | |
de1389b1 G |
51 | struct rtable *rt = NULL; |
52 | u_int32_t mtu = ~0U; | |
53 | ||
54 | if (family == PF_INET) { | |
55 | struct flowi4 *fl4 = &fl.u.ip4; | |
56 | memset(fl4, 0, sizeof(*fl4)); | |
57 | fl4->daddr = ip_hdr(skb)->saddr; | |
58 | } else { | |
59 | struct flowi6 *fl6 = &fl.u.ip6; | |
60 | ||
61 | memset(fl6, 0, sizeof(*fl6)); | |
62 | fl6->daddr = ipv6_hdr(skb)->saddr; | |
63 | } | |
de1389b1 | 64 | |
3f87c08c | 65 | nf_route(net, (struct dst_entry **)&rt, &fl, false, family); |
de1389b1 G |
66 | if (rt != NULL) { |
67 | mtu = dst_mtu(&rt->dst); | |
68 | dst_release(&rt->dst); | |
69 | } | |
70 | return mtu; | |
71 | } | |
72 | ||
cdd289a2 | 73 | static int |
3db05fea | 74 | tcpmss_mangle_packet(struct sk_buff *skb, |
70d19f80 | 75 | const struct xt_action_param *par, |
de1389b1 | 76 | unsigned int family, |
cdd289a2 PM |
77 | unsigned int tcphoff, |
78 | unsigned int minlen) | |
79 | { | |
70d19f80 | 80 | const struct xt_tcpmss_info *info = par->targinfo; |
cdd289a2 | 81 | struct tcphdr *tcph; |
71ffe9c7 PNA |
82 | int len, tcp_hdrlen; |
83 | unsigned int i; | |
cdd289a2 PM |
84 | __be16 oldval; |
85 | u16 newmss; | |
86 | u8 *opt; | |
87 | ||
b396966c PO |
88 | /* This is a fragment, no TCP header is available */ |
89 | if (par->fragoff != 0) | |
1205e1fa | 90 | return 0; |
b396966c | 91 | |
3db05fea | 92 | if (!skb_make_writable(skb, skb->len)) |
cdd289a2 PM |
93 | return -1; |
94 | ||
71ffe9c7 PNA |
95 | len = skb->len - tcphoff; |
96 | if (len < (int)sizeof(struct tcphdr)) | |
97 | return -1; | |
98 | ||
3db05fea | 99 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
71ffe9c7 | 100 | tcp_hdrlen = tcph->doff * 4; |
cdd289a2 | 101 | |
2638fd0f | 102 | if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr)) |
cdd289a2 | 103 | return -1; |
cdd289a2 PM |
104 | |
105 | if (info->mss == XT_TCPMSS_CLAMP_PMTU) { | |
613dbd95 | 106 | struct net *net = xt_net(par); |
7722e0d1 | 107 | unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family); |
50f4c7b7 | 108 | unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu); |
de1389b1 | 109 | |
50f4c7b7 | 110 | if (min_mtu <= minlen) { |
e87cc472 | 111 | net_err_ratelimited("unknown or invalid path-MTU (%u)\n", |
50f4c7b7 | 112 | min_mtu); |
cdd289a2 PM |
113 | return -1; |
114 | } | |
50f4c7b7 | 115 | newmss = min_mtu - minlen; |
cdd289a2 PM |
116 | } else |
117 | newmss = info->mss; | |
118 | ||
119 | opt = (u_int8_t *)tcph; | |
71ffe9c7 PNA |
120 | for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) { |
121 | if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) { | |
cdd289a2 PM |
122 | u_int16_t oldmss; |
123 | ||
124 | oldmss = (opt[i+2] << 8) | opt[i+3]; | |
125 | ||
17008064 BL |
126 | /* Never increase MSS, even when setting it, as |
127 | * doing so results in problems for hosts that rely | |
128 | * on MSS being set correctly. | |
129 | */ | |
130 | if (oldmss <= newmss) | |
cdd289a2 PM |
131 | return 0; |
132 | ||
133 | opt[i+2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 134 | opt[i+3] = newmss & 0x00ff; |
cdd289a2 | 135 | |
be0ea7d5 PM |
136 | inet_proto_csum_replace2(&tcph->check, skb, |
137 | htons(oldmss), htons(newmss), | |
4b048d6d | 138 | false); |
cdd289a2 PM |
139 | return 0; |
140 | } | |
141 | } | |
142 | ||
10a19939 | 143 | /* There is data after the header so the option can't be added |
71ffe9c7 PNA |
144 | * without moving it, and doing so may make the SYN packet |
145 | * itself too large. Accept the packet unmodified instead. | |
146 | */ | |
147 | if (len > tcp_hdrlen) | |
10a19939 | 148 | return 0; |
2638fd0f ED |
149 | |
150 | /* tcph->doff has 4 bits, do not wrap it to 0 */ | |
151 | if (tcp_hdrlen >= 15 * 4) | |
152 | return 0; | |
10a19939 | 153 | |
cdd289a2 PM |
154 | /* |
155 | * MSS Option not found ?! add it.. | |
156 | */ | |
3db05fea HX |
157 | if (skb_tailroom(skb) < TCPOLEN_MSS) { |
158 | if (pskb_expand_head(skb, 0, | |
159 | TCPOLEN_MSS - skb_tailroom(skb), | |
2ca7b0ac | 160 | GFP_ATOMIC)) |
cdd289a2 | 161 | return -1; |
3db05fea | 162 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
cdd289a2 PM |
163 | } |
164 | ||
3db05fea | 165 | skb_put(skb, TCPOLEN_MSS); |
cdd289a2 | 166 | |
70d19f80 PO |
167 | /* |
168 | * IPv4: RFC 1122 states "If an MSS option is not received at | |
169 | * connection setup, TCP MUST assume a default send MSS of 536". | |
170 | * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum | |
171 | * length IPv6 header of 60, ergo the default MSS value is 1220 | |
172 | * Since no MSS was provided, we must use the default values | |
409b545a | 173 | */ |
613dbd95 | 174 | if (xt_family(par) == NFPROTO_IPV4) |
70d19f80 PO |
175 | newmss = min(newmss, (u16)536); |
176 | else | |
177 | newmss = min(newmss, (u16)1220); | |
409b545a | 178 | |
cdd289a2 | 179 | opt = (u_int8_t *)tcph + sizeof(struct tcphdr); |
71ffe9c7 | 180 | memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr)); |
cdd289a2 | 181 | |
be0ea7d5 | 182 | inet_proto_csum_replace2(&tcph->check, skb, |
4b048d6d | 183 | htons(len), htons(len + TCPOLEN_MSS), true); |
cdd289a2 PM |
184 | opt[0] = TCPOPT_MSS; |
185 | opt[1] = TCPOLEN_MSS; | |
186 | opt[2] = (newmss & 0xff00) >> 8; | |
7c4e36bc | 187 | opt[3] = newmss & 0x00ff; |
cdd289a2 | 188 | |
4b048d6d | 189 | inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false); |
cdd289a2 PM |
190 | |
191 | oldval = ((__be16 *)tcph)[6]; | |
192 | tcph->doff += TCPOLEN_MSS/4; | |
be0ea7d5 | 193 | inet_proto_csum_replace2(&tcph->check, skb, |
4b048d6d | 194 | oldval, ((__be16 *)tcph)[6], false); |
cdd289a2 PM |
195 | return TCPOLEN_MSS; |
196 | } | |
197 | ||
198 | static unsigned int | |
4b560b44 | 199 | tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par) |
cdd289a2 | 200 | { |
3db05fea | 201 | struct iphdr *iph = ip_hdr(skb); |
cdd289a2 PM |
202 | __be16 newlen; |
203 | int ret; | |
204 | ||
70d19f80 | 205 | ret = tcpmss_mangle_packet(skb, par, |
de1389b1 | 206 | PF_INET, |
37c08387 | 207 | iph->ihl * 4, |
cdd289a2 PM |
208 | sizeof(*iph) + sizeof(struct tcphdr)); |
209 | if (ret < 0) | |
210 | return NF_DROP; | |
211 | if (ret > 0) { | |
3db05fea | 212 | iph = ip_hdr(skb); |
cdd289a2 | 213 | newlen = htons(ntohs(iph->tot_len) + ret); |
be0ea7d5 | 214 | csum_replace2(&iph->check, iph->tot_len, newlen); |
cdd289a2 PM |
215 | iph->tot_len = newlen; |
216 | } | |
217 | return XT_CONTINUE; | |
218 | } | |
219 | ||
c0cd1156 | 220 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
cdd289a2 | 221 | static unsigned int |
4b560b44 | 222 | tcpmss_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
cdd289a2 | 223 | { |
3db05fea | 224 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
cdd289a2 | 225 | u8 nexthdr; |
d6b3347b | 226 | __be16 frag_off, oldlen, newlen; |
cdd289a2 PM |
227 | int tcphoff; |
228 | int ret; | |
229 | ||
230 | nexthdr = ipv6h->nexthdr; | |
75f2811c | 231 | tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off); |
9dc0564e | 232 | if (tcphoff < 0) |
cdd289a2 | 233 | return NF_DROP; |
70d19f80 | 234 | ret = tcpmss_mangle_packet(skb, par, |
de1389b1 | 235 | PF_INET6, |
37c08387 | 236 | tcphoff, |
cdd289a2 PM |
237 | sizeof(*ipv6h) + sizeof(struct tcphdr)); |
238 | if (ret < 0) | |
239 | return NF_DROP; | |
240 | if (ret > 0) { | |
3db05fea | 241 | ipv6h = ipv6_hdr(skb); |
d6b3347b ED |
242 | oldlen = ipv6h->payload_len; |
243 | newlen = htons(ntohs(oldlen) + ret); | |
244 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
245 | skb->csum = csum_add(csum_sub(skb->csum, oldlen), | |
246 | newlen); | |
247 | ipv6h->payload_len = newlen; | |
cdd289a2 PM |
248 | } |
249 | return XT_CONTINUE; | |
250 | } | |
251 | #endif | |
252 | ||
cdd289a2 | 253 | /* Must specify -p tcp --syn */ |
e1931b78 | 254 | static inline bool find_syn_match(const struct xt_entry_match *m) |
cdd289a2 PM |
255 | { |
256 | const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data; | |
257 | ||
258 | if (strcmp(m->u.kernel.match->name, "tcp") == 0 && | |
a3433f35 | 259 | tcpinfo->flg_cmp & TCPHDR_SYN && |
cdd289a2 | 260 | !(tcpinfo->invflags & XT_TCP_INV_FLAGS)) |
e1931b78 | 261 | return true; |
cdd289a2 | 262 | |
e1931b78 | 263 | return false; |
cdd289a2 PM |
264 | } |
265 | ||
135367b8 | 266 | static int tcpmss_tg4_check(const struct xt_tgchk_param *par) |
cdd289a2 | 267 | { |
af5d6dc2 JE |
268 | const struct xt_tcpmss_info *info = par->targinfo; |
269 | const struct ipt_entry *e = par->entryinfo; | |
dcea992a | 270 | const struct xt_entry_match *ematch; |
cdd289a2 PM |
271 | |
272 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
af5d6dc2 | 273 | (par->hook_mask & ~((1 << NF_INET_FORWARD) | |
6e23ae2a PM |
274 | (1 << NF_INET_LOCAL_OUT) | |
275 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
b2606644 | 276 | pr_info_ratelimited("path-MTU clamping only supported in FORWARD, OUTPUT and POSTROUTING hooks\n"); |
d6b00a53 | 277 | return -EINVAL; |
cdd289a2 | 278 | } |
55917a21 PNA |
279 | if (par->nft_compat) |
280 | return 0; | |
281 | ||
dcea992a JE |
282 | xt_ematch_foreach(ematch, e) |
283 | if (find_syn_match(ematch)) | |
d6b00a53 | 284 | return 0; |
b2606644 | 285 | pr_info_ratelimited("Only works on TCP SYN packets\n"); |
d6b00a53 | 286 | return -EINVAL; |
cdd289a2 PM |
287 | } |
288 | ||
c0cd1156 | 289 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
135367b8 | 290 | static int tcpmss_tg6_check(const struct xt_tgchk_param *par) |
cdd289a2 | 291 | { |
af5d6dc2 JE |
292 | const struct xt_tcpmss_info *info = par->targinfo; |
293 | const struct ip6t_entry *e = par->entryinfo; | |
dcea992a | 294 | const struct xt_entry_match *ematch; |
cdd289a2 PM |
295 | |
296 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && | |
af5d6dc2 | 297 | (par->hook_mask & ~((1 << NF_INET_FORWARD) | |
6e23ae2a PM |
298 | (1 << NF_INET_LOCAL_OUT) | |
299 | (1 << NF_INET_POST_ROUTING))) != 0) { | |
b2606644 | 300 | pr_info_ratelimited("path-MTU clamping only supported in FORWARD, OUTPUT and POSTROUTING hooks\n"); |
d6b00a53 | 301 | return -EINVAL; |
cdd289a2 | 302 | } |
55917a21 PNA |
303 | if (par->nft_compat) |
304 | return 0; | |
305 | ||
dcea992a JE |
306 | xt_ematch_foreach(ematch, e) |
307 | if (find_syn_match(ematch)) | |
d6b00a53 | 308 | return 0; |
b2606644 | 309 | pr_info_ratelimited("Only works on TCP SYN packets\n"); |
d6b00a53 | 310 | return -EINVAL; |
cdd289a2 PM |
311 | } |
312 | #endif | |
313 | ||
d3c5ee6d | 314 | static struct xt_target tcpmss_tg_reg[] __read_mostly = { |
cdd289a2 | 315 | { |
ee999d8b | 316 | .family = NFPROTO_IPV4, |
cdd289a2 | 317 | .name = "TCPMSS", |
d3c5ee6d JE |
318 | .checkentry = tcpmss_tg4_check, |
319 | .target = tcpmss_tg4, | |
cdd289a2 PM |
320 | .targetsize = sizeof(struct xt_tcpmss_info), |
321 | .proto = IPPROTO_TCP, | |
322 | .me = THIS_MODULE, | |
323 | }, | |
c0cd1156 | 324 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
cdd289a2 | 325 | { |
ee999d8b | 326 | .family = NFPROTO_IPV6, |
cdd289a2 | 327 | .name = "TCPMSS", |
d3c5ee6d JE |
328 | .checkentry = tcpmss_tg6_check, |
329 | .target = tcpmss_tg6, | |
cdd289a2 PM |
330 | .targetsize = sizeof(struct xt_tcpmss_info), |
331 | .proto = IPPROTO_TCP, | |
332 | .me = THIS_MODULE, | |
333 | }, | |
334 | #endif | |
335 | }; | |
336 | ||
d3c5ee6d | 337 | static int __init tcpmss_tg_init(void) |
cdd289a2 | 338 | { |
d3c5ee6d | 339 | return xt_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
cdd289a2 PM |
340 | } |
341 | ||
d3c5ee6d | 342 | static void __exit tcpmss_tg_exit(void) |
cdd289a2 | 343 | { |
d3c5ee6d | 344 | xt_unregister_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
cdd289a2 PM |
345 | } |
346 | ||
d3c5ee6d JE |
347 | module_init(tcpmss_tg_init); |
348 | module_exit(tcpmss_tg_exit); |