Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 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> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 5 | * Copyright (C) 2007 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 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 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/ip.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 16 | #include <linux/ipv6.h> |
| 17 | #include <linux/tcp.h> |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 18 | #include <net/dst.h> |
| 19 | #include <net/flow.h> |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 20 | #include <net/ipv6.h> |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 21 | #include <net/route.h> |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 32 | MODULE_DESCRIPTION("Xtables: TCP Maximum Segment Size (MSS) adjustment"); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 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 | |
| 46 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 47 | tcpmss_mangle_packet(struct sk_buff *skb, |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 48 | const struct xt_action_param *par, |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 49 | unsigned int in_mtu, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 50 | unsigned int tcphoff, |
| 51 | unsigned int minlen) |
| 52 | { |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 53 | const struct xt_tcpmss_info *info = par->targinfo; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 54 | struct tcphdr *tcph; |
| 55 | unsigned int tcplen, i; |
| 56 | __be16 oldval; |
| 57 | u16 newmss; |
| 58 | u8 *opt; |
| 59 | |
Phil Oester | b396966 | 2013-06-12 10:58:20 +0200 | [diff] [blame] | 60 | /* This is a fragment, no TCP header is available */ |
| 61 | if (par->fragoff != 0) |
| 62 | return XT_CONTINUE; |
| 63 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 64 | if (!skb_make_writable(skb, skb->len)) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 65 | return -1; |
| 66 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 67 | tcplen = skb->len - tcphoff; |
| 68 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 69 | |
Simon Arlott | 10a1993 | 2010-02-02 15:33:38 +0100 | [diff] [blame] | 70 | /* Header cannot be larger than the packet */ |
| 71 | if (tcplen < tcph->doff*4) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 72 | return -1; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 73 | |
| 74 | if (info->mss == XT_TCPMSS_CLAMP_PMTU) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 75 | if (dst_mtu(skb_dst(skb)) <= minlen) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 76 | net_err_ratelimited("unknown or invalid path-MTU (%u)\n", |
| 77 | dst_mtu(skb_dst(skb))); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 78 | return -1; |
| 79 | } |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 80 | if (in_mtu <= minlen) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 81 | net_err_ratelimited("unknown or invalid path-MTU (%u)\n", |
| 82 | in_mtu); |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 83 | return -1; |
| 84 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 85 | newmss = min(dst_mtu(skb_dst(skb)), in_mtu) - minlen; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 86 | } else |
| 87 | newmss = info->mss; |
| 88 | |
| 89 | opt = (u_int8_t *)tcph; |
| 90 | for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) { |
| 91 | if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS && |
| 92 | opt[i+1] == TCPOLEN_MSS) { |
| 93 | u_int16_t oldmss; |
| 94 | |
| 95 | oldmss = (opt[i+2] << 8) | opt[i+3]; |
| 96 | |
Benjamin LaHaise | 1700806 | 2007-12-17 22:27:36 -0800 | [diff] [blame] | 97 | /* Never increase MSS, even when setting it, as |
| 98 | * doing so results in problems for hosts that rely |
| 99 | * on MSS being set correctly. |
| 100 | */ |
| 101 | if (oldmss <= newmss) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 102 | return 0; |
| 103 | |
| 104 | opt[i+2] = (newmss & 0xff00) >> 8; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 105 | opt[i+3] = newmss & 0x00ff; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 106 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 107 | inet_proto_csum_replace2(&tcph->check, skb, |
| 108 | htons(oldmss), htons(newmss), |
| 109 | 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | |
Simon Arlott | 10a1993 | 2010-02-02 15:33:38 +0100 | [diff] [blame] | 114 | /* There is data after the header so the option can't be added |
| 115 | without moving it, and doing so may make the SYN packet |
| 116 | itself too large. Accept the packet unmodified instead. */ |
| 117 | if (tcplen > tcph->doff*4) |
| 118 | return 0; |
| 119 | |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 120 | /* |
| 121 | * MSS Option not found ?! add it.. |
| 122 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 123 | if (skb_tailroom(skb) < TCPOLEN_MSS) { |
| 124 | if (pskb_expand_head(skb, 0, |
| 125 | TCPOLEN_MSS - skb_tailroom(skb), |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 126 | GFP_ATOMIC)) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 127 | return -1; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 128 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 131 | skb_put(skb, TCPOLEN_MSS); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 132 | |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 133 | /* |
| 134 | * IPv4: RFC 1122 states "If an MSS option is not received at |
| 135 | * connection setup, TCP MUST assume a default send MSS of 536". |
| 136 | * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum |
| 137 | * length IPv6 header of 60, ergo the default MSS value is 1220 |
| 138 | * Since no MSS was provided, we must use the default values |
Phil Oester | 409b545 | 2013-06-04 05:09:27 +0000 | [diff] [blame] | 139 | */ |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 140 | if (par->family == NFPROTO_IPV4) |
| 141 | newmss = min(newmss, (u16)536); |
| 142 | else |
| 143 | newmss = min(newmss, (u16)1220); |
Phil Oester | 409b545 | 2013-06-04 05:09:27 +0000 | [diff] [blame] | 144 | |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 145 | opt = (u_int8_t *)tcph + sizeof(struct tcphdr); |
| 146 | memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr)); |
| 147 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 148 | inet_proto_csum_replace2(&tcph->check, skb, |
| 149 | htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 150 | opt[0] = TCPOPT_MSS; |
| 151 | opt[1] = TCPOLEN_MSS; |
| 152 | opt[2] = (newmss & 0xff00) >> 8; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 153 | opt[3] = newmss & 0x00ff; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 154 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 155 | inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 156 | |
| 157 | oldval = ((__be16 *)tcph)[6]; |
| 158 | tcph->doff += TCPOLEN_MSS/4; |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 159 | inet_proto_csum_replace2(&tcph->check, skb, |
| 160 | oldval, ((__be16 *)tcph)[6], 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 161 | return TCPOLEN_MSS; |
| 162 | } |
| 163 | |
Jan Engelhardt | db1a75b | 2008-07-21 10:02:59 -0700 | [diff] [blame] | 164 | static u_int32_t tcpmss_reverse_mtu(const struct sk_buff *skb, |
| 165 | unsigned int family) |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 166 | { |
David S. Miller | a1bbb0e | 2011-03-12 02:16:48 -0500 | [diff] [blame] | 167 | struct flowi fl; |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 168 | const struct nf_afinfo *ai; |
| 169 | struct rtable *rt = NULL; |
| 170 | u_int32_t mtu = ~0U; |
| 171 | |
David S. Miller | a1bbb0e | 2011-03-12 02:16:48 -0500 | [diff] [blame] | 172 | if (family == PF_INET) { |
| 173 | struct flowi4 *fl4 = &fl.u.ip4; |
| 174 | memset(fl4, 0, sizeof(*fl4)); |
| 175 | fl4->daddr = ip_hdr(skb)->saddr; |
| 176 | } else { |
| 177 | struct flowi6 *fl6 = &fl.u.ip6; |
Jan Engelhardt | db1a75b | 2008-07-21 10:02:59 -0700 | [diff] [blame] | 178 | |
David S. Miller | a1bbb0e | 2011-03-12 02:16:48 -0500 | [diff] [blame] | 179 | memset(fl6, 0, sizeof(*fl6)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 180 | fl6->daddr = ipv6_hdr(skb)->saddr; |
David S. Miller | a1bbb0e | 2011-03-12 02:16:48 -0500 | [diff] [blame] | 181 | } |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 182 | rcu_read_lock(); |
Jan Engelhardt | db1a75b | 2008-07-21 10:02:59 -0700 | [diff] [blame] | 183 | ai = nf_get_afinfo(family); |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 184 | if (ai != NULL) |
Florian Westphal | 0fae2e7 | 2011-04-04 17:00:54 +0200 | [diff] [blame] | 185 | ai->route(&init_net, (struct dst_entry **)&rt, &fl, false); |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 186 | rcu_read_unlock(); |
| 187 | |
| 188 | if (rt != NULL) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 189 | mtu = dst_mtu(&rt->dst); |
| 190 | dst_release(&rt->dst); |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 191 | } |
| 192 | return mtu; |
| 193 | } |
| 194 | |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 195 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 196 | tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 197 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 198 | struct iphdr *iph = ip_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 199 | __be16 newlen; |
| 200 | int ret; |
| 201 | |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 202 | ret = tcpmss_mangle_packet(skb, par, |
Jan Engelhardt | db1a75b | 2008-07-21 10:02:59 -0700 | [diff] [blame] | 203 | tcpmss_reverse_mtu(skb, PF_INET), |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 204 | iph->ihl * 4, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 205 | sizeof(*iph) + sizeof(struct tcphdr)); |
| 206 | if (ret < 0) |
| 207 | return NF_DROP; |
| 208 | if (ret > 0) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 209 | iph = ip_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 210 | newlen = htons(ntohs(iph->tot_len) + ret); |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 211 | csum_replace2(&iph->check, iph->tot_len, newlen); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 212 | iph->tot_len = newlen; |
| 213 | } |
| 214 | return XT_CONTINUE; |
| 215 | } |
| 216 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 217 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 218 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 219 | tcpmss_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 220 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 221 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 222 | u8 nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 223 | __be16 frag_off; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 224 | int tcphoff; |
| 225 | int ret; |
| 226 | |
| 227 | nexthdr = ipv6h->nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 228 | tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off); |
Patrick McHardy | 9dc0564 | 2007-11-30 23:58:03 +1100 | [diff] [blame] | 229 | if (tcphoff < 0) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 230 | return NF_DROP; |
Phil Oester | 70d19f8 | 2013-06-12 10:44:51 +0200 | [diff] [blame] | 231 | ret = tcpmss_mangle_packet(skb, par, |
Jan Engelhardt | db1a75b | 2008-07-21 10:02:59 -0700 | [diff] [blame] | 232 | tcpmss_reverse_mtu(skb, PF_INET6), |
Jan Engelhardt | 37c0838 | 2008-01-31 04:06:10 -0800 | [diff] [blame] | 233 | tcphoff, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 234 | sizeof(*ipv6h) + sizeof(struct tcphdr)); |
| 235 | if (ret < 0) |
| 236 | return NF_DROP; |
| 237 | if (ret > 0) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 238 | ipv6h = ipv6_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 239 | ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret); |
| 240 | } |
| 241 | return XT_CONTINUE; |
| 242 | } |
| 243 | #endif |
| 244 | |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 245 | /* Must specify -p tcp --syn */ |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 246 | static inline bool find_syn_match(const struct xt_entry_match *m) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 247 | { |
| 248 | const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data; |
| 249 | |
| 250 | if (strcmp(m->u.kernel.match->name, "tcp") == 0 && |
Changli Gao | a3433f3 | 2010-06-12 14:01:43 +0000 | [diff] [blame] | 251 | tcpinfo->flg_cmp & TCPHDR_SYN && |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 252 | !(tcpinfo->invflags & XT_TCP_INV_FLAGS)) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 253 | return true; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 254 | |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 255 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 258 | static int tcpmss_tg4_check(const struct xt_tgchk_param *par) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 259 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 260 | const struct xt_tcpmss_info *info = par->targinfo; |
| 261 | const struct ipt_entry *e = par->entryinfo; |
Jan Engelhardt | dcea992 | 2010-02-24 18:34:48 +0100 | [diff] [blame] | 262 | const struct xt_entry_match *ematch; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 263 | |
| 264 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 265 | (par->hook_mask & ~((1 << NF_INET_FORWARD) | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 266 | (1 << NF_INET_LOCAL_OUT) | |
| 267 | (1 << NF_INET_POST_ROUTING))) != 0) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 268 | pr_info("path-MTU clamping only supported in " |
| 269 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 270 | return -EINVAL; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 271 | } |
Jan Engelhardt | dcea992 | 2010-02-24 18:34:48 +0100 | [diff] [blame] | 272 | xt_ematch_foreach(ematch, e) |
| 273 | if (find_syn_match(ematch)) |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 274 | return 0; |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 275 | pr_info("Only works on TCP SYN packets\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 276 | return -EINVAL; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 279 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 280 | static int tcpmss_tg6_check(const struct xt_tgchk_param *par) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 281 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 282 | const struct xt_tcpmss_info *info = par->targinfo; |
| 283 | const struct ip6t_entry *e = par->entryinfo; |
Jan Engelhardt | dcea992 | 2010-02-24 18:34:48 +0100 | [diff] [blame] | 284 | const struct xt_entry_match *ematch; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 285 | |
| 286 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 287 | (par->hook_mask & ~((1 << NF_INET_FORWARD) | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 288 | (1 << NF_INET_LOCAL_OUT) | |
| 289 | (1 << NF_INET_POST_ROUTING))) != 0) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 290 | pr_info("path-MTU clamping only supported in " |
| 291 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 292 | return -EINVAL; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 293 | } |
Jan Engelhardt | dcea992 | 2010-02-24 18:34:48 +0100 | [diff] [blame] | 294 | xt_ematch_foreach(ematch, e) |
| 295 | if (find_syn_match(ematch)) |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 296 | return 0; |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 297 | pr_info("Only works on TCP SYN packets\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 298 | return -EINVAL; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 299 | } |
| 300 | #endif |
| 301 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 302 | static struct xt_target tcpmss_tg_reg[] __read_mostly = { |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 303 | { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 304 | .family = NFPROTO_IPV4, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 305 | .name = "TCPMSS", |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 306 | .checkentry = tcpmss_tg4_check, |
| 307 | .target = tcpmss_tg4, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 308 | .targetsize = sizeof(struct xt_tcpmss_info), |
| 309 | .proto = IPPROTO_TCP, |
| 310 | .me = THIS_MODULE, |
| 311 | }, |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 312 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 313 | { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 314 | .family = NFPROTO_IPV6, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 315 | .name = "TCPMSS", |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 316 | .checkentry = tcpmss_tg6_check, |
| 317 | .target = tcpmss_tg6, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 318 | .targetsize = sizeof(struct xt_tcpmss_info), |
| 319 | .proto = IPPROTO_TCP, |
| 320 | .me = THIS_MODULE, |
| 321 | }, |
| 322 | #endif |
| 323 | }; |
| 324 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 325 | static int __init tcpmss_tg_init(void) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 326 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 327 | return xt_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 330 | static void __exit tcpmss_tg_exit(void) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 331 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 332 | xt_unregister_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 335 | module_init(tcpmss_tg_init); |
| 336 | module_exit(tcpmss_tg_exit); |