2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000 Ruslan Ermilov and Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <net/route.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
39 #include <netinet/ip6.h>
41 #include <netinet/tcp.h>
52 #include "throughput.h"
59 #include "slcompress.h"
63 #include "descriptor.h"
75 * Compute the MSS as described in RFC 6691.
77 #define MAXMSS4(mtu) ((mtu) - sizeof(struct ip) - sizeof(struct tcphdr))
79 #define MAXMSS6(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct tcphdr))
84 * The following macro is used to update an
85 * internet checksum. "acc" is a 32-bit
86 * accumulation of all the changes to the
87 * checksum (adding in old 16-bit words and
88 * subtracting out new words), and "cksum"
89 * is the checksum value to be updated.
91 #define ADJUST_CHECKSUM(acc, cksum) { \
95 acc = (acc >> 16) + (acc & 0xffff); \
97 cksum = (u_short) ~acc; \
99 acc = (acc >> 16) + (acc & 0xffff); \
101 cksum = (u_short) acc; \
106 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
108 size_t hlen, olen, optlen;
113 hlen = tc->th_off << 2;
115 /* Invalid header length or header without options. */
116 if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
119 /* MSS option only allowed within SYN packets. */
120 if (!(tc->th_flags & TH_SYN))
123 for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
124 olen > 0; olen -= optlen, opt += optlen) {
125 if (*opt == TCPOPT_EOL)
127 else if (*opt == TCPOPT_NOP)
131 if (optlen <= 0 || optlen > olen)
133 if (*opt == TCPOPT_MAXSEG) {
134 if (optlen != TCPOLEN_MAXSEG)
136 mss = (u_int16_t *)(opt + 2);
137 if (ntohs(*mss) > maxmss) {
138 log_Printf(LogDEBUG, "MSS: %u -> %u\n",
139 ntohs(*mss), maxmss);
141 *mss = htons(maxmss);
143 ADJUST_CHECKSUM(accumulate, tc->th_sum);
151 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
155 struct ip6_hdr *pip6;
156 struct ip6_frag *pfrag;
160 if (!Enabled(bundle, OPT_TCPMSSFIXUP))
165 if (plen < sizeof(struct ip))
167 pip = (struct ip *)MBUF_CTOP(bp);
172 * Check for MSS option only for TCP packets with zero fragment offsets
173 * and correct total and header lengths.
175 hlen = pip->ip_hl << 2;
176 if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
177 ntohs(pip->ip_len) == plen && hlen <= plen &&
178 plen >= sizeof(struct tcphdr) + hlen)
179 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
180 MAXMSS4(bundle->iface->mtu));
183 case IPV6_VERSION >> 4:
185 * Check for MSS option only for TCP packets with no extension headers
186 * or a single extension header which is a fragmentation header with
187 * offset 0. Furthermore require that the length field is correct.
189 if (plen < sizeof(struct ip6_hdr))
191 pip6 = (struct ip6_hdr *)MBUF_CTOP(bp);
192 if (ntohs(pip6->ip6_plen) + sizeof(struct ip6_hdr) != plen)
195 switch (pip6->ip6_nxt) {
197 hlen = sizeof(struct ip6_hdr);
199 case IPPROTO_FRAGMENT:
200 if (plen >= sizeof(struct ip6_frag) + sizeof(struct ip6_hdr)) {
201 pfrag = (struct ip6_frag *)(MBUF_CTOP(bp) + sizeof(struct ip6_hdr));
202 if (pfrag->ip6f_nxt == IPPROTO_TCP &&
203 ntohs(pfrag->ip6f_offlg & IP6F_OFF_MASK) == 0)
204 hlen = sizeof(struct ip6_hdr)+ sizeof(struct ip6_frag);
208 if (hlen > 0 && plen >= sizeof(struct tcphdr) + hlen)
209 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
210 MAXMSS6(bundle->iface->mtu));
214 log_Printf(LogDEBUG, "tcpmss_Check: Unknown IP family %u\n", pip->ip_v);
221 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused,
222 struct mbuf *bp, int pri __unused, u_short *proto __unused)
224 return tcpmss_Check(bundle, bp);
228 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused,
229 struct mbuf *bp, u_short *proto __unused)
231 return tcpmss_Check(bundle, bp);
234 struct layer tcpmsslayer =
235 { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };