Deleted Added
full compact
hdlc.c (50479) hdlc.c (54912)
1/*
2 * PPP High Level Link Control (HDLC) Module
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
1/*
2 * PPP High Level Link Control (HDLC) Module
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $FreeBSD: head/usr.sbin/ppp/hdlc.c 50479 1999-08-28 01:35:59Z peter $
20 * $FreeBSD: head/usr.sbin/ppp/hdlc.c 54912 1999-12-20 20:29:47Z brian $
21 *
22 * TODO:
23 */
24#include <sys/param.h>
25#include <sys/un.h>
26
27#include <stdio.h>
28#include <string.h>

--- 81 unchanged lines hidden (view full) ---

110}
111
112static inline u_short
113HdlcFcsBuf(u_short fcs, struct mbuf *m)
114{
115 int len;
116 u_char *pos, *end;
117
21 *
22 * TODO:
23 */
24#include <sys/param.h>
25#include <sys/un.h>
26
27#include <stdio.h>
28#include <string.h>

--- 81 unchanged lines hidden (view full) ---

110}
111
112static inline u_short
113HdlcFcsBuf(u_short fcs, struct mbuf *m)
114{
115 int len;
116 u_char *pos, *end;
117
118 len = mbuf_Length(m);
118 len = m_length(m);
119 pos = MBUF_CTOP(m);
119 pos = MBUF_CTOP(m);
120 end = pos + m->cnt;
120 end = pos + m->m_len;
121 while (len--) {
122 fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
123 if (pos == end && len) {
121 while (len--) {
122 fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
123 if (pos == end && len) {
124 m = m->next;
124 m = m->m_next;
125 pos = MBUF_CTOP(m);
125 pos = MBUF_CTOP(m);
126 end = pos + m->cnt;
126 end = pos + m->m_len;
127 }
128 }
129 return (fcs);
130}
131
132int
133hdlc_WrapperOctets(struct lcp *lcp, u_short proto)
134{
135 return 2;
136}
137
138static struct mbuf *
139hdlc_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
140 int pri, u_short *proto)
141{
142 struct mbuf *last;
143 u_char *cp;
144 u_short fcs;
145
127 }
128 }
129 return (fcs);
130}
131
132int
133hdlc_WrapperOctets(struct lcp *lcp, u_short proto)
134{
135 return 2;
136}
137
138static struct mbuf *
139hdlc_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
140 int pri, u_short *proto)
141{
142 struct mbuf *last;
143 u_char *cp;
144 u_short fcs;
145
146 mbuf_SetType(bp, MB_HDLCOUT);
146 m_settype(bp, MB_HDLCOUT);
147 fcs = HdlcFcsBuf(INITFCS, bp);
148 fcs = ~fcs;
149
147 fcs = HdlcFcsBuf(INITFCS, bp);
148 fcs = ~fcs;
149
150 for (last = bp; last->next; last = last->next)
150 for (last = bp; last->m_next; last = last->m_next)
151 ;
152
151 ;
152
153 if (last->size - last->offset - last->cnt >= 2) {
154 cp = MBUF_CTOP(last) + last->cnt;
155 last->cnt += 2;
153 if (last->m_size - last->m_offset - last->m_len >= 2) {
154 cp = MBUF_CTOP(last) + last->m_len;
155 last->m_len += 2;
156 } else {
156 } else {
157 struct mbuf *tail = mbuf_Alloc(2, MB_HDLCOUT);
158 last->next = tail;
157 struct mbuf *tail = m_get(2, MB_HDLCOUT);
158 last->m_next = tail;
159 cp = MBUF_CTOP(tail);
160 }
161
162 *cp++ = fcs & 0377; /* Low byte first (nothing like consistency) */
163 *cp++ = fcs >> 8;
164
165 log_DumpBp(LogHDLC, "hdlc_Output", bp);
166

--- 134 unchanged lines hidden (view full) ---

301 u_short fcs;
302 int len;
303
304 if (!p) {
305 log_Printf(LogERROR, "Can't Pull a hdlc packet from a logical link\n");
306 return bp;
307 }
308
159 cp = MBUF_CTOP(tail);
160 }
161
162 *cp++ = fcs & 0377; /* Low byte first (nothing like consistency) */
163 *cp++ = fcs >> 8;
164
165 log_DumpBp(LogHDLC, "hdlc_Output", bp);
166

--- 134 unchanged lines hidden (view full) ---

301 u_short fcs;
302 int len;
303
304 if (!p) {
305 log_Printf(LogERROR, "Can't Pull a hdlc packet from a logical link\n");
306 return bp;
307 }
308
309 log_DumpBp(LogHDLC, "hdlc_Input:", bp);
309 log_DumpBp(LogHDLC, "hdlc_LayerPull:", bp);
310
310
311 fcs = hdlc_Fcs(MBUF_CTOP(bp), bp->cnt);
311 fcs = hdlc_Fcs(MBUF_CTOP(bp), bp->m_len);
312
312
313 log_Printf(LogDEBUG, "%s: hdlc_Input: fcs = %04x (%s)\n",
313 log_Printf(LogDEBUG, "%s: hdlc_LayerPull: fcs = %04x (%s)\n",
314 p->link.name, fcs, (fcs == GOODFCS) ? "good" : "BAD!");
315
316 if (fcs != GOODFCS) {
317 p->hdlc.lqm.SaveInErrors++;
318 p->hdlc.stats.badfcs++;
314 p->link.name, fcs, (fcs == GOODFCS) ? "good" : "BAD!");
315
316 if (fcs != GOODFCS) {
317 p->hdlc.lqm.SaveInErrors++;
318 p->hdlc.stats.badfcs++;
319 mbuf_Free(bp);
319 m_freem(bp);
320 return NULL;
321 }
322
320 return NULL;
321 }
322
323 p->hdlc.lqm.SaveInOctets += bp->cnt + 1;
323 p->hdlc.lqm.SaveInOctets += bp->m_len + 1;
324 p->hdlc.lqm.SaveInPackets++;
325
324 p->hdlc.lqm.SaveInPackets++;
325
326 len = mbuf_Length(bp);
326 len = m_length(bp);
327 if (len < 4) { /* rfc1662 section 4.3 */
327 if (len < 4) { /* rfc1662 section 4.3 */
328 mbuf_Free(bp);
328 m_freem(bp);
329 bp = NULL;
330 }
331
329 bp = NULL;
330 }
331
332 bp = mbuf_Truncate(bp, len - 2); /* discard the FCS */
333 mbuf_SetType(bp, MB_HDLCIN);
332 bp = m_adj(bp, -2); /* discard the FCS */
333 m_settype(bp, MB_HDLCIN);
334
335 return bp;
336}
337
338/* Detect a HDLC frame */
339
340static const struct frameheader {
341 const u_char *data;

--- 89 unchanged lines hidden ---
334
335 return bp;
336}
337
338/* Detect a HDLC frame */
339
340static const struct frameheader {
341 const u_char *data;

--- 89 unchanged lines hidden ---