xref: /freebsd/usr.sbin/ppp/async.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
165309e5cSBrian Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
465309e5cSBrian Somers  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
565309e5cSBrian Somers  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
665309e5cSBrian Somers  *                           Internet Initiative Japan, Inc (IIJ)
765309e5cSBrian Somers  * All rights reserved.
8af57ed9fSAtsushi Murai  *
965309e5cSBrian Somers  * Redistribution and use in source and binary forms, with or without
1065309e5cSBrian Somers  * modification, are permitted provided that the following conditions
1165309e5cSBrian Somers  * are met:
1265309e5cSBrian Somers  * 1. Redistributions of source code must retain the above copyright
1365309e5cSBrian Somers  *    notice, this list of conditions and the following disclaimer.
1465309e5cSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
1565309e5cSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
1665309e5cSBrian Somers  *    documentation and/or other materials provided with the distribution.
17af57ed9fSAtsushi Murai  *
1865309e5cSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1965309e5cSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2065309e5cSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2165309e5cSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2265309e5cSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2365309e5cSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2465309e5cSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2565309e5cSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2665309e5cSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2765309e5cSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2865309e5cSBrian Somers  * SUCH DAMAGE.
29af57ed9fSAtsushi Murai  *
30af57ed9fSAtsushi Murai  */
3165309e5cSBrian Somers 
322764b86aSBrian Somers #include <sys/types.h>
3375240ed1SBrian Somers 
3475240ed1SBrian Somers #include <string.h>
3575240ed1SBrian Somers #include <termios.h>
3675240ed1SBrian Somers 
375d9e6103SBrian Somers #include "layer.h"
3875240ed1SBrian Somers #include "mbuf.h"
3975240ed1SBrian Somers #include "log.h"
4075240ed1SBrian Somers #include "defs.h"
4175240ed1SBrian Somers #include "timer.h"
42af57ed9fSAtsushi Murai #include "fsm.h"
43879ed6faSBrian Somers #include "lqr.h"
44af57ed9fSAtsushi Murai #include "hdlc.h"
45af57ed9fSAtsushi Murai #include "lcp.h"
465d9e6103SBrian Somers #include "proto.h"
4775240ed1SBrian Somers #include "async.h"
488c07a7b2SBrian Somers #include "throughput.h"
493b0f8d2eSBrian Somers #include "ccp.h"
508c07a7b2SBrian Somers #include "link.h"
5142d4d396SBrian Somers #include "descriptor.h"
5263b73463SBrian Somers #include "physical.h"
53af57ed9fSAtsushi Murai 
54af57ed9fSAtsushi Murai #define MODE_HUNT 0x01
55af57ed9fSAtsushi Murai #define MODE_ESC  0x02
56af57ed9fSAtsushi Murai 
57af57ed9fSAtsushi Murai void
async_Init(struct async * async)586140ba11SBrian Somers async_Init(struct async *async)
59af57ed9fSAtsushi Murai {
60ea59de37SBrian Somers   async_Setup(async);
61ea59de37SBrian Somers   memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
62ea59de37SBrian Somers }
63ea59de37SBrian Somers 
64ea59de37SBrian Somers void
async_Setup(struct async * async)65ea59de37SBrian Somers async_Setup(struct async *async)
66ea59de37SBrian Somers {
676140ba11SBrian Somers   async->mode = MODE_HUNT;
686140ba11SBrian Somers   async->length = 0;
696140ba11SBrian Somers   async->my_accmap = async->his_accmap = 0xffffffff;
70af57ed9fSAtsushi Murai }
71af57ed9fSAtsushi Murai 
72af57ed9fSAtsushi Murai void
async_SetLinkParams(struct async * async,u_int32_t mymap,u_int32_t hismap)73fb11a9c2SBrian Somers async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
74af57ed9fSAtsushi Murai {
75fb11a9c2SBrian Somers   async->my_accmap = mymap;
76fb11a9c2SBrian Somers   async->his_accmap = hismap | mymap;
77af57ed9fSAtsushi Murai }
78af57ed9fSAtsushi Murai 
79af57ed9fSAtsushi Murai /*
805d9e6103SBrian Somers  * Encode into async HDLC byte code
81af57ed9fSAtsushi Murai  */
82af57ed9fSAtsushi Murai static void
async_Encode(struct async * async,u_char ** cp,u_char c,int proto)835d9e6103SBrian Somers async_Encode(struct async *async, u_char **cp, u_char c, int proto)
84af57ed9fSAtsushi Murai {
85af57ed9fSAtsushi Murai   u_char *wp;
86af57ed9fSAtsushi Murai 
87af57ed9fSAtsushi Murai   wp = *cp;
886140ba11SBrian Somers   if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
89af57ed9fSAtsushi Murai       || (c == HDLC_ESC) || (c == HDLC_SYN)) {
90af57ed9fSAtsushi Murai     *wp++ = HDLC_ESC;
91af57ed9fSAtsushi Murai     c ^= HDLC_XOR;
92af57ed9fSAtsushi Murai   }
939991562dSBrian Somers   if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
94af57ed9fSAtsushi Murai     *wp++ = HDLC_ESC;
95af57ed9fSAtsushi Murai     c ^= HDLC_XOR;
96af57ed9fSAtsushi Murai   }
97af57ed9fSAtsushi Murai   *wp++ = c;
98af57ed9fSAtsushi Murai   *cp = wp;
99af57ed9fSAtsushi Murai }
100af57ed9fSAtsushi Murai 
1015d9e6103SBrian Somers static struct mbuf *
async_LayerPush(struct bundle * b __unused,struct link * l,struct mbuf * bp,int pri __unused,u_short * proto)102057f1760SBrian Somers async_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
103057f1760SBrian Somers                 int pri __unused, u_short *proto)
104af57ed9fSAtsushi Murai {
1055d9e6103SBrian Somers   struct physical *p = link2physical(l);
106af57ed9fSAtsushi Murai   u_char *cp, *sp, *ep;
107af57ed9fSAtsushi Murai   struct mbuf *wp;
108a57095e7SBrian Somers   size_t oldcnt;
109057f1760SBrian Somers   size_t cnt;
110af57ed9fSAtsushi Murai 
11126af0ae9SBrian Somers   if (!p || m_length(bp) > HDLCSIZE) {
11226af0ae9SBrian Somers     m_freem(bp);
1135d9e6103SBrian Somers     return NULL;
114af57ed9fSAtsushi Murai   }
1155d9e6103SBrian Somers 
116a57095e7SBrian Somers   oldcnt = m_length(bp);
117a57095e7SBrian Somers 
1185d9e6103SBrian Somers   cp = p->async.xbuff;
119af57ed9fSAtsushi Murai   ep = cp + HDLCSIZE - 10;
120af57ed9fSAtsushi Murai   wp = bp;
121af57ed9fSAtsushi Murai   *cp++ = HDLC_SYN;
122af57ed9fSAtsushi Murai   while (wp) {
123af57ed9fSAtsushi Murai     sp = MBUF_CTOP(wp);
12426af0ae9SBrian Somers     for (cnt = wp->m_len; cnt > 0; cnt--) {
1255d9e6103SBrian Somers       async_Encode(&p->async, &cp, *sp++, *proto);
126af57ed9fSAtsushi Murai       if (cp >= ep) {
12726af0ae9SBrian Somers 	m_freem(bp);
1285d9e6103SBrian Somers 	return NULL;
129af57ed9fSAtsushi Murai       }
130af57ed9fSAtsushi Murai     }
13126af0ae9SBrian Somers     wp = wp->m_next;
132af57ed9fSAtsushi Murai   }
133af57ed9fSAtsushi Murai   *cp++ = HDLC_SYN;
134af57ed9fSAtsushi Murai 
1355d9e6103SBrian Somers   cnt = cp - p->async.xbuff;
13626af0ae9SBrian Somers   m_freem(bp);
13726af0ae9SBrian Somers   bp = m_get(cnt, MB_ASYNCOUT);
1385d9e6103SBrian Somers   memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
139a57095e7SBrian Somers   bp->priv = cnt - oldcnt;
1406815097bSBrian Somers   log_DumpBp(LogASYNC, "Write", bp);
1415d9e6103SBrian Somers 
1425d9e6103SBrian Somers   return bp;
143af57ed9fSAtsushi Murai }
144af57ed9fSAtsushi Murai 
14575240ed1SBrian Somers static struct mbuf *
async_Decode(struct async * async,u_char c)1466140ba11SBrian Somers async_Decode(struct async *async, u_char c)
147af57ed9fSAtsushi Murai {
148af57ed9fSAtsushi Murai   struct mbuf *bp;
149af57ed9fSAtsushi Murai 
1506140ba11SBrian Somers   if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
151aa8e0519SBrian Somers     return NULL;
152af57ed9fSAtsushi Murai 
153af57ed9fSAtsushi Murai   switch (c) {
154af57ed9fSAtsushi Murai   case HDLC_SYN:
1556140ba11SBrian Somers     async->mode &= ~MODE_HUNT;
1566140ba11SBrian Somers     if (async->length) {		/* packet is ready. */
15726af0ae9SBrian Somers       bp = m_get(async->length, MB_ASYNCIN);
158dd7e2610SBrian Somers       mbuf_Write(bp, async->hbuff, async->length);
1596140ba11SBrian Somers       async->length = 0;
160aa8e0519SBrian Somers       return bp;
161af57ed9fSAtsushi Murai     }
162af57ed9fSAtsushi Murai     break;
163af57ed9fSAtsushi Murai   case HDLC_ESC:
1646140ba11SBrian Somers     if (!(async->mode & MODE_ESC)) {
1656140ba11SBrian Somers       async->mode |= MODE_ESC;
166af57ed9fSAtsushi Murai       break;
167af57ed9fSAtsushi Murai     }
168f0067240SPhilippe Charnier     /* FALLTHROUGH */
169af57ed9fSAtsushi Murai   default:
1706140ba11SBrian Somers     if (async->length >= HDLCSIZE) {
171af57ed9fSAtsushi Murai       /* packet is too large, discard it */
172a33b2ef7SBrian Somers       log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
173a33b2ef7SBrian Somers                  async->length);
1746140ba11SBrian Somers       async->length = 0;
1756140ba11SBrian Somers       async->mode = MODE_HUNT;
176af57ed9fSAtsushi Murai       break;
177af57ed9fSAtsushi Murai     }
1786140ba11SBrian Somers     if (async->mode & MODE_ESC) {
179af57ed9fSAtsushi Murai       c ^= HDLC_XOR;
1806140ba11SBrian Somers       async->mode &= ~MODE_ESC;
181af57ed9fSAtsushi Murai     }
1826140ba11SBrian Somers     async->hbuff[async->length++] = c;
183af57ed9fSAtsushi Murai     break;
184af57ed9fSAtsushi Murai   }
185aa8e0519SBrian Somers   return NULL;
186af57ed9fSAtsushi Murai }
187af57ed9fSAtsushi Murai 
1885d9e6103SBrian Somers static struct mbuf *
async_LayerPull(struct bundle * b __unused,struct link * l,struct mbuf * bp,u_short * proto __unused)189057f1760SBrian Somers async_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
190057f1760SBrian Somers                 u_short *proto __unused)
191af57ed9fSAtsushi Murai {
1925d9e6103SBrian Somers   struct mbuf *nbp, **last;
1935d9e6103SBrian Somers   struct physical *p = link2physical(l);
1945d9e6103SBrian Somers   u_char *ch;
1955d9e6103SBrian Somers   size_t cnt;
196af57ed9fSAtsushi Murai 
1975d9e6103SBrian Somers   if (!p) {
1985d9e6103SBrian Somers     log_Printf(LogERROR, "Can't Pull an async packet from a logical link\n");
1995d9e6103SBrian Somers     return bp;
2005d9e6103SBrian Somers   }
2018c07a7b2SBrian Somers 
2025d9e6103SBrian Somers   last = &nbp;
2035d9e6103SBrian Somers 
2046815097bSBrian Somers   log_DumpBp(LogASYNC, "Read", bp);
2055d9e6103SBrian Somers   while (bp) {
2065d9e6103SBrian Somers     ch = MBUF_CTOP(bp);
20726af0ae9SBrian Somers     for (cnt = bp->m_len; cnt; cnt--) {
2085d9e6103SBrian Somers       *last = async_Decode(&p->async, *ch++);
2095d9e6103SBrian Somers       if (*last != NULL)
21026af0ae9SBrian Somers         last = &(*last)->m_nextpkt;
211af57ed9fSAtsushi Murai     }
21226af0ae9SBrian Somers     bp = m_free(bp);
213af57ed9fSAtsushi Murai   }
2145d9e6103SBrian Somers 
2155d9e6103SBrian Somers   return nbp;
21653c9f6c0SAtsushi Murai }
2175d9e6103SBrian Somers 
2185d9e6103SBrian Somers struct layer asynclayer =
2195d9e6103SBrian Somers   { LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };
220