xref: /freebsd/usr.sbin/ppp/tcpmss.c (revision 6cee8a83177f3fc763ccc101da626b33798e2a63)
1 /*-
2  * Copyright (c) 2000 Ruslan Ermilov and Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 
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>
36 #include <netinet/tcp.h>
37 #include <sys/un.h>
38 
39 #include <termios.h>
40 
41 #include "layer.h"
42 #include "defs.h"
43 #include "log.h"
44 #include "timer.h"
45 #include "fsm.h"
46 #include "mbuf.h"
47 #include "throughput.h"
48 #include "lqr.h"
49 #include "hdlc.h"
50 #include "lcp.h"
51 #include "ccp.h"
52 #include "link.h"
53 #include "iplist.h"
54 #include "slcompress.h"
55 #include "ipcp.h"
56 #include "filter.h"
57 #include "descriptor.h"
58 #include "mp.h"
59 #include "iface.h"
60 #ifndef NORADIUS
61 #include "radius.h"
62 #endif
63 #include "bundle.h"
64 
65 
66 /*-
67  * We are in a liberal position about MSS
68  * (RFC 879, section 7).
69  */
70 #define MAXMSS(mtu) (mtu - sizeof(struct ip) - sizeof(struct tcphdr))
71 
72 
73 /*-
74  * The following macro is used to update an
75  * internet checksum.  "acc" is a 32-bit
76  * accumulation of all the changes to the
77  * checksum (adding in old 16-bit words and
78  * subtracting out new words), and "cksum"
79  * is the checksum value to be updated.
80  */
81 #define ADJUST_CHECKSUM(acc, cksum) { \
82   acc += cksum; \
83   if (acc < 0) { \
84     acc = -acc; \
85     acc = (acc >> 16) + (acc & 0xffff); \
86     acc += acc >> 16; \
87     cksum = (u_short) ~acc; \
88   } else { \
89     acc = (acc >> 16) + (acc & 0xffff); \
90     acc += acc >> 16; \
91     cksum = (u_short) acc; \
92   } \
93 }
94 
95 static void
96 MSSFixup(struct tcphdr *tc, ssize_t pktlen, u_int16_t maxmss)
97 {
98   int hlen, olen, optlen;
99   u_char *opt;
100   u_int16_t *mss;
101   int accumulate;
102 
103   hlen = tc->th_off << 2;
104 
105   /* Invalid header length or header without options. */
106   if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
107     return;
108 
109   /* MSS option only allowed within SYN packets. */
110   if (!(tc->th_flags & TH_SYN))
111     return;
112 
113   for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
114        olen > 0; olen -= optlen, opt += optlen) {
115     if (*opt == TCPOPT_EOL)
116       break;
117     else if (*opt == TCPOPT_NOP)
118       optlen = 1;
119     else {
120       optlen = *(opt + 1);
121       if (optlen <= 0 || optlen > olen)
122         break;
123       if (*opt == TCPOPT_MAXSEG) {
124         if (optlen != TCPOLEN_MAXSEG)
125           continue;
126         mss = (u_int16_t *)(opt + 2);
127         if (ntohs(*mss) > maxmss) {
128           log_Printf(LogDEBUG, "MSS: %u -> %u\n",
129                ntohs(*mss), maxmss);
130           accumulate = *mss;
131           *mss = htons(maxmss);
132           accumulate -= *mss;
133           ADJUST_CHECKSUM(accumulate, tc->th_sum);
134         }
135       }
136     }
137   }
138 }
139 
140 static struct mbuf *
141 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
142 {
143   struct ip *pip;
144   int hlen, plen;
145 
146   if (!Enabled(bundle, OPT_TCPMSSFIXUP))
147     return bp;
148 
149   bp = m_pullup(bp);
150   plen = m_length(bp);
151   pip = (struct ip *)MBUF_CTOP(bp);
152   hlen = pip->ip_hl << 2;
153 
154   /*
155    * Check for MSS option only for TCP packets with zero fragment offsets
156    * and correct total and header lengths.
157    */
158   if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
159       ntohs(pip->ip_len) == plen && hlen <= plen &&
160       plen - hlen >= sizeof(struct tcphdr))
161     MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
162              MAXMSS(bundle->iface->mtu));
163 
164   return bp;
165 }
166 
167 static struct mbuf *
168 tcpmss_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
169                  int pri, u_short *proto)
170 {
171 	return tcpmss_Check(bundle, bp);
172 }
173 
174 static struct mbuf *
175 tcpmss_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
176                  u_short *proto)
177 {
178 	return tcpmss_Check(bundle, bp);
179 }
180 
181 struct layer tcpmsslayer =
182   { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };
183