2 * Copyright (c) 2000 Ruslan Ermilov and Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.sbin/ppp/tcpmss.c,v 1.1.4.4 2002/09/01 02:12:32 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/tcpmss.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
30 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <net/route.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <netinet/tcp.h>
48 #include "throughput.h"
55 #include "slcompress.h"
59 #include "descriptor.h"
71 * We are in a liberal position about MSS
72 * (RFC 879, section 7).
74 #define MAXMSS(mtu) (mtu - sizeof(struct ip) - sizeof(struct tcphdr))
78 * The following macro is used to update an
79 * internet checksum. "acc" is a 32-bit
80 * accumulation of all the changes to the
81 * checksum (adding in old 16-bit words and
82 * subtracting out new words), and "cksum"
83 * is the checksum value to be updated.
85 #define ADJUST_CHECKSUM(acc, cksum) { \
89 acc = (acc >> 16) + (acc & 0xffff); \
91 cksum = (u_short) ~acc; \
93 acc = (acc >> 16) + (acc & 0xffff); \
95 cksum = (u_short) acc; \
100 MSSFixup(struct tcphdr
*tc
, size_t pktlen
, u_int16_t maxmss
)
102 size_t hlen
, olen
, optlen
;
107 hlen
= tc
->th_off
<< 2;
109 /* Invalid header length or header without options. */
110 if (hlen
<= sizeof(struct tcphdr
) || hlen
> pktlen
)
113 /* MSS option only allowed within SYN packets. */
114 if (!(tc
->th_flags
& TH_SYN
))
117 for (olen
= hlen
- sizeof(struct tcphdr
), opt
= (u_char
*)(tc
+ 1);
118 olen
> 0; olen
-= optlen
, opt
+= optlen
) {
119 if (*opt
== TCPOPT_EOL
)
121 else if (*opt
== TCPOPT_NOP
)
125 if (optlen
<= 0 || optlen
> olen
)
127 if (*opt
== TCPOPT_MAXSEG
) {
128 if (optlen
!= TCPOLEN_MAXSEG
)
130 mss
= (u_int16_t
*)(opt
+ 2);
131 if (ntohs(*mss
) > maxmss
) {
132 log_Printf(LogDEBUG
, "MSS: %u -> %u\n",
133 ntohs(*mss
), maxmss
);
135 *mss
= htons(maxmss
);
137 ADJUST_CHECKSUM(accumulate
, tc
->th_sum
);
145 tcpmss_Check(struct bundle
*bundle
, struct mbuf
*bp
)
150 if (!Enabled(bundle
, OPT_TCPMSSFIXUP
))
155 pip
= (struct ip
*)MBUF_CTOP(bp
);
156 hlen
= pip
->ip_hl
<< 2;
159 * Check for MSS option only for TCP packets with zero fragment offsets
160 * and correct total and header lengths.
162 if (pip
->ip_p
== IPPROTO_TCP
&& (ntohs(pip
->ip_off
) & IP_OFFMASK
) == 0 &&
163 ntohs(pip
->ip_len
) == plen
&& hlen
<= plen
&&
164 plen
- hlen
>= sizeof(struct tcphdr
))
165 MSSFixup((struct tcphdr
*)(MBUF_CTOP(bp
) + hlen
), plen
- hlen
,
166 MAXMSS(bundle
->iface
->mtu
));
172 tcpmss_LayerPush(struct bundle
*bundle
, struct link
*l __unused
, struct mbuf
*bp
,
173 int pri __unused
, u_short
*proto __unused
)
175 return tcpmss_Check(bundle
, bp
);
179 tcpmss_LayerPull(struct bundle
*bundle
, struct link
*l __unused
, struct mbuf
*bp
,
180 u_short
*proto __unused
)
182 return tcpmss_Check(bundle
, bp
);
185 struct layer tcpmsslayer
=
186 { LAYER_PROTO
, "tcpmss", tcpmss_LayerPush
, tcpmss_LayerPull
};