kernel: Remove the COMPAT_43 kernel option along with all related code.
[dragonfly.git] / usr.sbin / ppp / async.c
blob4c3c284d8163cc902893ea93c6f4621524151aa5
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.
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.
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.
28 * $FreeBSD: src/usr.sbin/ppp/async.c,v 1.23.2.3 2002/09/01 02:12:22 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/async.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
32 #include <sys/types.h>
33 #include <sys/select.h>
35 #include <string.h>
36 #include <termios.h>
38 #include "layer.h"
39 #include "mbuf.h"
40 #include "log.h"
41 #include "defs.h"
42 #include "timer.h"
43 #include "fsm.h"
44 #include "lqr.h"
45 #include "hdlc.h"
46 #include "lcp.h"
47 #include "proto.h"
48 #include "async.h"
49 #include "throughput.h"
50 #include "ccp.h"
51 #include "link.h"
52 #include "descriptor.h"
53 #include "physical.h"
55 #define MODE_HUNT 0x01
56 #define MODE_ESC 0x02
58 void
59 async_Init(struct async *async)
61 async_Setup(async);
62 memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
65 void
66 async_Setup(struct async *async)
68 async->mode = MODE_HUNT;
69 async->length = 0;
70 async->my_accmap = async->his_accmap = 0xffffffff;
73 void
74 async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
76 async->my_accmap = mymap;
77 async->his_accmap = hismap | mymap;
81 * Encode into async HDLC byte code
83 static void
84 async_Encode(struct async *async, u_char **cp, u_char c, int proto)
86 u_char *wp;
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;
94 if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
95 *wp++ = HDLC_ESC;
96 c ^= HDLC_XOR;
98 *wp++ = c;
99 *cp = wp;
102 static struct mbuf *
103 async_LayerPush(struct bundle *bundle __unused, struct link *l, struct mbuf *bp,
104 int pri __unused, u_short *proto)
106 struct physical *p = link2physical(l);
107 u_char *cp, *sp, *ep;
108 struct mbuf *wp;
109 size_t cnt;
111 if (!p || m_length(bp) > HDLCSIZE) {
112 m_freem(bp);
113 return NULL;
116 cp = p->async.xbuff;
117 ep = cp + HDLCSIZE - 10;
118 wp = bp;
119 *cp++ = HDLC_SYN;
120 while (wp) {
121 sp = MBUF_CTOP(wp);
122 for (cnt = wp->m_len; cnt > 0; cnt--) {
123 async_Encode(&p->async, &cp, *sp++, *proto);
124 if (cp >= ep) {
125 m_freem(bp);
126 return NULL;
129 wp = wp->m_next;
131 *cp++ = HDLC_SYN;
133 cnt = cp - p->async.xbuff;
134 m_freem(bp);
135 bp = m_get(cnt, MB_ASYNCOUT);
136 memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
137 log_DumpBp(LogASYNC, "Write", bp);
139 return bp;
142 static struct mbuf *
143 async_Decode(struct async *async, u_char c)
145 struct mbuf *bp;
147 if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
148 return NULL;
150 switch (c) {
151 case HDLC_SYN:
152 async->mode &= ~MODE_HUNT;
153 if (async->length) { /* packet is ready. */
154 bp = m_get(async->length, MB_ASYNCIN);
155 mbuf_Write(bp, async->hbuff, async->length);
156 async->length = 0;
157 return bp;
159 break;
160 case HDLC_ESC:
161 if (!(async->mode & MODE_ESC)) {
162 async->mode |= MODE_ESC;
163 break;
165 /* FALLTHROUGH */
166 default:
167 if (async->length >= HDLCSIZE) {
168 /* packet is too large, discard it */
169 log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
170 async->length);
171 async->length = 0;
172 async->mode = MODE_HUNT;
173 break;
175 if (async->mode & MODE_ESC) {
176 c ^= HDLC_XOR;
177 async->mode &= ~MODE_ESC;
179 async->hbuff[async->length++] = c;
180 break;
182 return NULL;
185 static struct mbuf *
186 async_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
187 u_short *proto __unused)
189 struct mbuf *nbp, **last;
190 struct physical *p = link2physical(l);
191 u_char *ch;
192 size_t cnt;
194 if (!p) {
195 log_Printf(LogERROR, "Can't Pull an async packet from a logical link\n");
196 return bp;
199 last = &nbp;
201 log_DumpBp(LogASYNC, "Read", bp);
202 while (bp) {
203 ch = MBUF_CTOP(bp);
204 for (cnt = bp->m_len; cnt; cnt--) {
205 *last = async_Decode(&p->async, *ch++);
206 if (*last != NULL)
207 last = &(*last)->m_nextpkt;
209 bp = m_free(bp);
212 return nbp;
215 struct layer asynclayer =
216 { LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };