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
29 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <net/route.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
37 #include <netinet/ip6.h>
39 #include <netinet/tcp.h>
50 #include "throughput.h"
57 #include "slcompress.h"
61 #include "descriptor.h"
73 * Compute the MSS as described in RFC 6691.
75 #define MAXMSS4(mtu) ((mtu) - sizeof(struct ip) - sizeof(struct tcphdr))
77 #define MAXMSS6(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct tcphdr))
82 * The following macro is used to update an
83 * internet checksum. "acc" is a 32-bit
84 * accumulation of all the changes to the
85 * checksum (adding in old 16-bit words and
86 * subtracting out new words), and "cksum"
87 * is the checksum value to be updated.
89 #define ADJUST_CHECKSUM(acc, cksum) { \
93 acc = (acc >> 16) + (acc & 0xffff); \
95 cksum = (u_short) ~acc; \
97 acc = (acc >> 16) + (acc & 0xffff); \
99 cksum = (u_short) acc; \
104 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
106 size_t hlen, olen, optlen;
111 hlen = tc->th_off << 2;
113 /* Invalid header length or header without options. */
114 if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
117 /* MSS option only allowed within SYN packets. */
118 if (!(tc->th_flags & TH_SYN))
121 for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
122 olen > 0; olen -= optlen, opt += optlen) {
123 if (*opt == TCPOPT_EOL)
125 else if (*opt == TCPOPT_NOP)
129 if (optlen <= 0 || optlen > olen)
131 if (*opt == TCPOPT_MAXSEG) {
132 if (optlen != TCPOLEN_MAXSEG)
134 mss = (u_int16_t *)(opt + 2);
135 if (ntohs(*mss) > maxmss) {
136 log_Printf(LogDEBUG, "MSS: %u -> %u\n",
137 ntohs(*mss), maxmss);
139 *mss = htons(maxmss);
141 ADJUST_CHECKSUM(accumulate, tc->th_sum);
149 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
153 struct ip6_hdr *pip6;
154 struct ip6_frag *pfrag;
158 if (!Enabled(bundle, OPT_TCPMSSFIXUP))
163 if (plen < sizeof(struct ip))
165 pip = (struct ip *)MBUF_CTOP(bp);
170 * Check for MSS option only for TCP packets with zero fragment offsets
171 * and correct total and header lengths.
173 hlen = pip->ip_hl << 2;
174 if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
175 ntohs(pip->ip_len) == plen && hlen <= plen &&
176 plen >= sizeof(struct tcphdr) + hlen)
177 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
178 MAXMSS4(bundle->iface->mtu));
181 case IPV6_VERSION >> 4:
183 * Check for MSS option only for TCP packets with no extension headers
184 * or a single extension header which is a fragmentation header with
185 * offset 0. Furthermore require that the length field is correct.
187 if (plen < sizeof(struct ip6_hdr))
189 pip6 = (struct ip6_hdr *)MBUF_CTOP(bp);
190 if (ntohs(pip6->ip6_plen) + sizeof(struct ip6_hdr) != plen)
193 switch (pip6->ip6_nxt) {
195 hlen = sizeof(struct ip6_hdr);
197 case IPPROTO_FRAGMENT:
198 if (plen >= sizeof(struct ip6_frag) + sizeof(struct ip6_hdr)) {
199 pfrag = (struct ip6_frag *)(MBUF_CTOP(bp) + sizeof(struct ip6_hdr));
200 if (pfrag->ip6f_nxt == IPPROTO_TCP &&
201 ntohs(pfrag->ip6f_offlg & IP6F_OFF_MASK) == 0)
202 hlen = sizeof(struct ip6_hdr)+ sizeof(struct ip6_frag);
206 if (hlen > 0 && plen >= sizeof(struct tcphdr) + hlen)
207 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
208 MAXMSS6(bundle->iface->mtu));
212 log_Printf(LogDEBUG, "tcpmss_Check: Unknown IP family %u\n", pip->ip_v);
219 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused,
220 struct mbuf *bp, int pri __unused, u_short *proto __unused)
222 return tcpmss_Check(bundle, bp);
226 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused,
227 struct mbuf *bp, u_short *proto __unused)
229 return tcpmss_Check(bundle, bp);
232 struct layer tcpmsslayer =
233 { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };