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