2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
38 #include <netinet/tcp.h>
49 #include "throughput.h"
56 #include "slcompress.h"
60 #include "descriptor.h"
72 * We are in a liberal position about MSS
73 * (RFC 879, section 7).
75 #define MAXMSS(mtu) ((mtu) - sizeof(struct ip) - sizeof(struct tcphdr) - 12)
79 * The following macro is used to update an
80 * internet checksum. "acc" is a 32-bit
81 * accumulation of all the changes to the
82 * checksum (adding in old 16-bit words and
83 * subtracting out new words), and "cksum"
84 * is the checksum value to be updated.
86 #define ADJUST_CHECKSUM(acc, cksum) { \
90 acc = (acc >> 16) + (acc & 0xffff); \
92 cksum = (u_short) ~acc; \
94 acc = (acc >> 16) + (acc & 0xffff); \
96 cksum = (u_short) acc; \
101 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
103 size_t hlen, olen, optlen;
108 hlen = tc->th_off << 2;
110 /* Invalid header length or header without options. */
111 if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
114 /* MSS option only allowed within SYN packets. */
115 if (!(tc->th_flags & TH_SYN))
118 for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
119 olen > 0; olen -= optlen, opt += optlen) {
120 if (*opt == TCPOPT_EOL)
122 else if (*opt == TCPOPT_NOP)
126 if (optlen <= 0 || optlen > olen)
128 if (*opt == TCPOPT_MAXSEG) {
129 if (optlen != TCPOLEN_MAXSEG)
131 mss = (u_int16_t *)(opt + 2);
132 if (ntohs(*mss) > maxmss) {
133 log_Printf(LogDEBUG, "MSS: %u -> %u\n",
134 ntohs(*mss), maxmss);
136 *mss = htons(maxmss);
138 ADJUST_CHECKSUM(accumulate, tc->th_sum);
146 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
151 if (!Enabled(bundle, OPT_TCPMSSFIXUP))
156 pip = (struct ip *)MBUF_CTOP(bp);
157 hlen = pip->ip_hl << 2;
160 * Check for MSS option only for TCP packets with zero fragment offsets
161 * and correct total and header lengths.
163 if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
164 ntohs(pip->ip_len) == plen && hlen <= plen &&
165 plen >= sizeof(struct tcphdr) + hlen)
166 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
167 MAXMSS(bundle->iface->mtu));
173 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused,
174 struct mbuf *bp, int pri __unused, u_short *proto __unused)
176 return tcpmss_Check(bundle, bp);
180 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused,
181 struct mbuf *bp, u_short *proto __unused)
183 return tcpmss_Check(bundle, bp);
186 struct layer tcpmsslayer =
187 { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };