xref: /freebsd/usr.sbin/ppp/async.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6  *                           Internet Initiative Japan, Inc (IIJ)
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/types.h>
36 
37 #include <string.h>
38 #include <termios.h>
39 
40 #include "layer.h"
41 #include "mbuf.h"
42 #include "log.h"
43 #include "defs.h"
44 #include "timer.h"
45 #include "fsm.h"
46 #include "lqr.h"
47 #include "hdlc.h"
48 #include "lcp.h"
49 #include "proto.h"
50 #include "async.h"
51 #include "throughput.h"
52 #include "ccp.h"
53 #include "link.h"
54 #include "descriptor.h"
55 #include "physical.h"
56 
57 #define MODE_HUNT 0x01
58 #define MODE_ESC  0x02
59 
60 void
61 async_Init(struct async *async)
62 {
63   async_Setup(async);
64   memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
65 }
66 
67 void
68 async_Setup(struct async *async)
69 {
70   async->mode = MODE_HUNT;
71   async->length = 0;
72   async->my_accmap = async->his_accmap = 0xffffffff;
73 }
74 
75 void
76 async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
77 {
78   async->my_accmap = mymap;
79   async->his_accmap = hismap | mymap;
80 }
81 
82 /*
83  * Encode into async HDLC byte code
84  */
85 static void
86 async_Encode(struct async *async, u_char **cp, u_char c, int proto)
87 {
88   u_char *wp;
89 
90   wp = *cp;
91   if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
92       || (c == HDLC_ESC) || (c == HDLC_SYN)) {
93     *wp++ = HDLC_ESC;
94     c ^= HDLC_XOR;
95   }
96   if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
97     *wp++ = HDLC_ESC;
98     c ^= HDLC_XOR;
99   }
100   *wp++ = c;
101   *cp = wp;
102 }
103 
104 static struct mbuf *
105 async_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
106                 int pri __unused, u_short *proto)
107 {
108   struct physical *p = link2physical(l);
109   u_char *cp, *sp, *ep;
110   struct mbuf *wp;
111   size_t oldcnt;
112   size_t cnt;
113 
114   if (!p || m_length(bp) > HDLCSIZE) {
115     m_freem(bp);
116     return NULL;
117   }
118 
119   oldcnt = m_length(bp);
120 
121   cp = p->async.xbuff;
122   ep = cp + HDLCSIZE - 10;
123   wp = bp;
124   *cp++ = HDLC_SYN;
125   while (wp) {
126     sp = MBUF_CTOP(wp);
127     for (cnt = wp->m_len; cnt > 0; cnt--) {
128       async_Encode(&p->async, &cp, *sp++, *proto);
129       if (cp >= ep) {
130 	m_freem(bp);
131 	return NULL;
132       }
133     }
134     wp = wp->m_next;
135   }
136   *cp++ = HDLC_SYN;
137 
138   cnt = cp - p->async.xbuff;
139   m_freem(bp);
140   bp = m_get(cnt, MB_ASYNCOUT);
141   memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
142   bp->priv = cnt - oldcnt;
143   log_DumpBp(LogASYNC, "Write", bp);
144 
145   return bp;
146 }
147 
148 static struct mbuf *
149 async_Decode(struct async *async, u_char c)
150 {
151   struct mbuf *bp;
152 
153   if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
154     return NULL;
155 
156   switch (c) {
157   case HDLC_SYN:
158     async->mode &= ~MODE_HUNT;
159     if (async->length) {		/* packet is ready. */
160       bp = m_get(async->length, MB_ASYNCIN);
161       mbuf_Write(bp, async->hbuff, async->length);
162       async->length = 0;
163       return bp;
164     }
165     break;
166   case HDLC_ESC:
167     if (!(async->mode & MODE_ESC)) {
168       async->mode |= MODE_ESC;
169       break;
170     }
171     /* FALLTHROUGH */
172   default:
173     if (async->length >= HDLCSIZE) {
174       /* packet is too large, discard it */
175       log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
176                  async->length);
177       async->length = 0;
178       async->mode = MODE_HUNT;
179       break;
180     }
181     if (async->mode & MODE_ESC) {
182       c ^= HDLC_XOR;
183       async->mode &= ~MODE_ESC;
184     }
185     async->hbuff[async->length++] = c;
186     break;
187   }
188   return NULL;
189 }
190 
191 static struct mbuf *
192 async_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
193                 u_short *proto __unused)
194 {
195   struct mbuf *nbp, **last;
196   struct physical *p = link2physical(l);
197   u_char *ch;
198   size_t cnt;
199 
200   if (!p) {
201     log_Printf(LogERROR, "Can't Pull an async packet from a logical link\n");
202     return bp;
203   }
204 
205   last = &nbp;
206 
207   log_DumpBp(LogASYNC, "Read", bp);
208   while (bp) {
209     ch = MBUF_CTOP(bp);
210     for (cnt = bp->m_len; cnt; cnt--) {
211       *last = async_Decode(&p->async, *ch++);
212       if (*last != NULL)
213         last = &(*last)->m_nextpkt;
214     }
215     bp = m_free(bp);
216   }
217 
218   return nbp;
219 }
220 
221 struct layer asynclayer =
222   { LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };
223