]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/async.c
Don't use SignalBundle if it's not set
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / async.c
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  * $FreeBSD$
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 #define MODE_HUNT 0x01
54 #define MODE_ESC  0x02
55
56 void
57 async_Init(struct async *async)
58 {
59   async_Setup(async);
60   memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
61 }
62
63 void
64 async_Setup(struct async *async)
65 {
66   async->mode = MODE_HUNT;
67   async->length = 0;
68   async->my_accmap = async->his_accmap = 0xffffffff;
69 }
70
71 void
72 async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
73 {
74   async->my_accmap = mymap;
75   async->his_accmap = hismap | mymap;
76 }
77
78 /*
79  * Encode into async HDLC byte code
80  */
81 static void
82 async_Encode(struct async *async, u_char **cp, u_char c, int proto)
83 {
84   u_char *wp;
85
86   wp = *cp;
87   if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
88       || (c == HDLC_ESC) || (c == HDLC_SYN)) {
89     *wp++ = HDLC_ESC;
90     c ^= HDLC_XOR;
91   }
92   if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
93     *wp++ = HDLC_ESC;
94     c ^= HDLC_XOR;
95   }
96   *wp++ = c;
97   *cp = wp;
98 }
99
100 static struct mbuf *
101 async_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
102                 int pri, u_short *proto)
103 {
104   struct physical *p = link2physical(l);
105   u_char *cp, *sp, *ep;
106   struct mbuf *wp;
107   int cnt;
108
109   if (!p || m_length(bp) > HDLCSIZE) {
110     m_freem(bp);
111     return NULL;
112   }
113
114   cp = p->async.xbuff;
115   ep = cp + HDLCSIZE - 10;
116   wp = bp;
117   *cp++ = HDLC_SYN;
118   while (wp) {
119     sp = MBUF_CTOP(wp);
120     for (cnt = wp->m_len; cnt > 0; cnt--) {
121       async_Encode(&p->async, &cp, *sp++, *proto);
122       if (cp >= ep) {
123         m_freem(bp);
124         return NULL;
125       }
126     }
127     wp = wp->m_next;
128   }
129   *cp++ = HDLC_SYN;
130
131   cnt = cp - p->async.xbuff;
132   m_freem(bp);
133   bp = m_get(cnt, MB_ASYNCOUT);
134   memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
135   log_DumpBp(LogASYNC, "Write", bp);
136
137   return bp;
138 }
139
140 static struct mbuf *
141 async_Decode(struct async *async, u_char c)
142 {
143   struct mbuf *bp;
144
145   if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
146     return NULL;
147
148   switch (c) {
149   case HDLC_SYN:
150     async->mode &= ~MODE_HUNT;
151     if (async->length) {                /* packet is ready. */
152       bp = m_get(async->length, MB_ASYNCIN);
153       mbuf_Write(bp, async->hbuff, async->length);
154       async->length = 0;
155       return bp;
156     }
157     break;
158   case HDLC_ESC:
159     if (!(async->mode & MODE_ESC)) {
160       async->mode |= MODE_ESC;
161       break;
162     }
163     /* Fall into ... */
164   default:
165     if (async->length >= HDLCSIZE) {
166       /* packet is too large, discard it */
167       log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
168                  async->length);
169       async->length = 0;
170       async->mode = MODE_HUNT;
171       break;
172     }
173     if (async->mode & MODE_ESC) {
174       c ^= HDLC_XOR;
175       async->mode &= ~MODE_ESC;
176     }
177     async->hbuff[async->length++] = c;
178     break;
179   }
180   return NULL;
181 }
182
183 static struct mbuf *
184 async_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp,
185                 u_short *proto)
186 {
187   struct mbuf *nbp, **last;
188   struct physical *p = link2physical(l);
189   u_char *ch;
190   size_t cnt;
191
192   if (!p) {
193     log_Printf(LogERROR, "Can't Pull an async packet from a logical link\n");
194     return bp;
195   }
196
197   last = &nbp;
198
199   log_DumpBp(LogASYNC, "Read", bp);
200   while (bp) {
201     ch = MBUF_CTOP(bp);
202     for (cnt = bp->m_len; cnt; cnt--) {
203       *last = async_Decode(&p->async, *ch++);
204       if (*last != NULL)
205         last = &(*last)->m_nextpkt;
206     }
207     bp = m_free(bp);
208   }
209
210   return nbp;
211 }
212
213 struct layer asynclayer =
214   { LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };