ppp: Raise WARNS to 5.
[dragonfly.git] / usr.sbin / ppp / tcpmss.c
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: 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 $
28  */
29
30 #include <sys/param.h>
31
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>
38 #include <sys/un.h>
39
40 #include <termios.h>
41
42 #include "layer.h"
43 #include "defs.h"
44 #include "log.h"
45 #include "timer.h"
46 #include "fsm.h"
47 #include "mbuf.h"
48 #include "throughput.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "lcp.h"
52 #include "ccp.h"
53 #include "link.h"
54 #include "iplist.h"
55 #include "slcompress.h"
56 #include "ncpaddr.h"
57 #include "ipcp.h"
58 #include "filter.h"
59 #include "descriptor.h"
60 #include "mp.h"
61 #include "iface.h"
62 #ifndef NORADIUS
63 #include "radius.h"
64 #endif
65 #include "ipv6cp.h"
66 #include "ncp.h"
67 #include "bundle.h"
68
69
70 /*-
71  * We are in a liberal position about MSS
72  * (RFC 879, section 7).
73  */
74 #define MAXMSS(mtu) (mtu - sizeof(struct ip) - sizeof(struct tcphdr))
75
76
77 /*-
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.
84  */
85 #define ADJUST_CHECKSUM(acc, cksum) { \
86   acc += cksum; \
87   if (acc < 0) { \
88     acc = -acc; \
89     acc = (acc >> 16) + (acc & 0xffff); \
90     acc += acc >> 16; \
91     cksum = (u_short) ~acc; \
92   } else { \
93     acc = (acc >> 16) + (acc & 0xffff); \
94     acc += acc >> 16; \
95     cksum = (u_short) acc; \
96   } \
97 }
98
99 static void
100 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
101 {
102   size_t hlen, olen, optlen;
103   u_char *opt;
104   u_int16_t *mss;
105   int accumulate;
106
107   hlen = tc->th_off << 2;
108
109   /* Invalid header length or header without options. */
110   if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
111     return;
112
113   /* MSS option only allowed within SYN packets. */
114   if (!(tc->th_flags & TH_SYN))
115     return;
116
117   for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
118        olen > 0; olen -= optlen, opt += optlen) {
119     if (*opt == TCPOPT_EOL)
120       break;
121     else if (*opt == TCPOPT_NOP)
122       optlen = 1;
123     else {
124       optlen = *(opt + 1);
125       if (optlen <= 0 || optlen > olen)
126         break;
127       if (*opt == TCPOPT_MAXSEG) {
128         if (optlen != TCPOLEN_MAXSEG)
129           continue;
130         mss = (u_int16_t *)(opt + 2);
131         if (ntohs(*mss) > maxmss) {
132           log_Printf(LogDEBUG, "MSS: %u -> %u\n",
133                ntohs(*mss), maxmss);
134           accumulate = *mss;
135           *mss = htons(maxmss);
136           accumulate -= *mss;
137           ADJUST_CHECKSUM(accumulate, tc->th_sum);
138         }
139       }
140     }
141   }
142 }
143
144 static struct mbuf *
145 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
146 {
147   struct ip *pip;
148   size_t hlen, plen;
149
150   if (!Enabled(bundle, OPT_TCPMSSFIXUP))
151     return bp;
152
153   bp = m_pullup(bp);
154   plen = m_length(bp);
155   pip = (struct ip *)MBUF_CTOP(bp);
156   hlen = pip->ip_hl << 2;
157
158   /*
159    * Check for MSS option only for TCP packets with zero fragment offsets
160    * and correct total and header lengths.
161    */
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));
167
168   return bp;
169 }
170
171 static struct mbuf *
172 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
173                  int pri __unused, u_short *proto __unused)
174 {
175         return tcpmss_Check(bundle, bp);
176 }
177
178 static struct mbuf *
179 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
180                  u_short *proto __unused)
181 {
182         return tcpmss_Check(bundle, bp);
183 }
184
185 struct layer tcpmsslayer =
186   { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };