6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_lmi.c,v 1.25 2006/01/14 14:17:27 glebius Exp $
41 * $DragonFly: src/sys/netgraph7/ng_lmi.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_lmi.c,v 1.38 1999/11/01 09:24:52 julian Exp $
46 * This node performs the frame relay LMI protocol. It knows how
47 * to do ITU Annex A, ANSI Annex D, and "Group-of-Four" variants
50 * A specific protocol can be forced by connecting the corresponding
51 * hook to DLCI 0 or 1023 (as appropriate) of a frame relay link.
53 * Alternately, this node can do auto-detection of the LMI protocol
54 * by connecting hook "auto0" to DLCI 0 and "auto1023" to DLCI 1023.
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/errno.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/syslog.h>
64 #include "ng_message.h"
69 * Human readable names for LMI
71 #define NAME_ANNEXA NG_LMI_HOOK_ANNEXA
72 #define NAME_ANNEXD NG_LMI_HOOK_ANNEXD
73 #define NAME_GROUP4 NG_LMI_HOOK_GROUPOF4
74 #define NAME_NONE "None"
87 * Any received LMI frame should be at least this long
89 #define LMI_MIN_LENGTH 8 /* XXX verify */
92 * Netgraph node methods and type descriptor
94 static ng_constructor_t nglmi_constructor
;
95 static ng_rcvmsg_t nglmi_rcvmsg
;
96 static ng_shutdown_t nglmi_shutdown
;
97 static ng_newhook_t nglmi_newhook
;
98 static ng_rcvdata_t nglmi_rcvdata
;
99 static ng_disconnect_t nglmi_disconnect
;
100 static int nglmi_checkdata(hook_p hook
, struct mbuf
*m
);
102 static struct ng_type typestruct
= {
103 .version
= NG_ABI_VERSION
,
104 .name
= NG_LMI_NODE_TYPE
,
105 .constructor
= nglmi_constructor
,
106 .rcvmsg
= nglmi_rcvmsg
,
107 .shutdown
= nglmi_shutdown
,
108 .newhook
= nglmi_newhook
,
109 .rcvdata
= nglmi_rcvdata
,
110 .disconnect
= nglmi_disconnect
,
112 NETGRAPH_INIT(lmi
, &typestruct
);
115 * Info and status per node
118 node_p node
; /* netgraph node */
119 int flags
; /* state */
120 int poll_count
; /* the count of times for autolmi */
121 int poll_state
; /* state of auto detect machine */
122 u_char remote_seq
; /* sequence number the remote sent */
123 u_char local_seq
; /* last sequence number we sent */
124 u_char protoID
; /* 9 for group of 4, 8 otherwise */
125 u_long seq_retries
; /* sent this how many time so far */
126 struct callout handle
; /* see timeout(9) */
131 hook_p lmi_channel
; /* whatever we ended up using */
135 hook_p lmi_channel0
; /* auto-detect on DLCI 0 */
136 hook_p lmi_channel1023
;/* auto-detect on DLCI 1023 */
137 char *protoname
; /* cache protocol name */
138 u_char dlci_state
[MAXDLCI
+ 1];
139 int invalidx
; /* next dlci's to invalidate */
141 typedef struct nglmi_softc
*sc_p
;
144 * Other internal functions
146 static void LMI_ticker(node_p node
, hook_p hook
, void *arg1
, int arg2
);
147 static void nglmi_startup_fixed(sc_p sc
, hook_p hook
);
148 static void nglmi_startup_auto(sc_p sc
);
149 static void nglmi_startup(sc_p sc
);
150 static void nglmi_inquire(sc_p sc
, int full
);
151 static void ngauto_state_machine(sc_p sc
);
154 * Values for 'flags' field
155 * NB: the SCF_CONNECTED flag is set if and only if the timer is running.
157 #define SCF_CONNECTED 0x01 /* connected to something */
158 #define SCF_AUTO 0x02 /* we are auto-detecting */
159 #define SCF_FIXED 0x04 /* we are fixed from the start */
161 #define SCF_LMITYPE 0x18 /* mask for determining Annex mode */
162 #define SCF_NOLMI 0x00 /* no LMI type selected yet */
163 #define SCF_ANNEX_A 0x08 /* running annex A mode */
164 #define SCF_ANNEX_D 0x10 /* running annex D mode */
165 #define SCF_GROUP4 0x18 /* running group of 4 */
167 #define SETLMITYPE(sc, annex) \
169 (sc)->flags &= ~SCF_LMITYPE; \
170 (sc)->flags |= (annex); \
173 #define NOPROTO(sc) (((sc)->flags & SCF_LMITYPE) == SCF_NOLMI)
174 #define ANNEXA(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_A)
175 #define ANNEXD(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_D)
176 #define GROUP4(sc) (((sc)->flags & SCF_LMITYPE) == SCF_GROUP4)
178 #define LMIPOLLSIZE 3
179 #define LMI_PATIENCE 8 /* declare all DLCI DOWN after N LMI failures */
185 nglmi_constructor(node_p node
)
189 MALLOC(sc
, sc_p
, sizeof(*sc
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
193 NG_NODE_SET_PRIVATE(node
, sc
);
196 ng_callout_init(&sc
->handle
);
197 sc
->protoname
= NAME_NONE
;
198 sc
->liv_per_full
= NG_LMI_SEQ_PER_FULL
; /* make this dynamic */
199 sc
->liv_rate
= NG_LMI_KEEPALIVE_RATE
;
204 * The LMI channel has a private pointer which is the same as the
205 * node private pointer. The debug channel has a NULL private pointer.
208 nglmi_newhook(node_p node
, hook_p hook
, const char *name
)
210 sc_p sc
= NG_NODE_PRIVATE(node
);
212 if (strcmp(name
, NG_LMI_HOOK_DEBUG
) == 0) {
213 NG_HOOK_SET_PRIVATE(hook
, NULL
);
216 if (sc
->flags
& SCF_CONNECTED
) {
217 /* already connected, return an error */
220 if (strcmp(name
, NG_LMI_HOOK_ANNEXA
) == 0) {
221 sc
->lmi_annexA
= hook
;
222 NG_HOOK_SET_PRIVATE(hook
, NG_NODE_PRIVATE(node
));
224 SETLMITYPE(sc
, SCF_ANNEX_A
);
225 sc
->protoname
= NAME_ANNEXA
;
226 nglmi_startup_fixed(sc
, hook
);
227 } else if (strcmp(name
, NG_LMI_HOOK_ANNEXD
) == 0) {
228 sc
->lmi_annexD
= hook
;
229 NG_HOOK_SET_PRIVATE(hook
, NG_NODE_PRIVATE(node
));
231 SETLMITYPE(sc
, SCF_ANNEX_D
);
232 sc
->protoname
= NAME_ANNEXD
;
233 nglmi_startup_fixed(sc
, hook
);
234 } else if (strcmp(name
, NG_LMI_HOOK_GROUPOF4
) == 0) {
235 sc
->lmi_group4
= hook
;
236 NG_HOOK_SET_PRIVATE(hook
, NG_NODE_PRIVATE(node
));
238 SETLMITYPE(sc
, SCF_GROUP4
);
239 sc
->protoname
= NAME_GROUP4
;
240 nglmi_startup_fixed(sc
, hook
);
241 } else if (strcmp(name
, NG_LMI_HOOK_AUTO0
) == 0) {
242 /* Note this, and if B is already installed, we're complete */
243 sc
->lmi_channel0
= hook
;
244 sc
->protoname
= NAME_NONE
;
245 NG_HOOK_SET_PRIVATE(hook
, NG_NODE_PRIVATE(node
));
246 if (sc
->lmi_channel1023
)
247 nglmi_startup_auto(sc
);
248 } else if (strcmp(name
, NG_LMI_HOOK_AUTO1023
) == 0) {
249 /* Note this, and if A is already installed, we're complete */
250 sc
->lmi_channel1023
= hook
;
251 sc
->protoname
= NAME_NONE
;
252 NG_HOOK_SET_PRIVATE(hook
, NG_NODE_PRIVATE(node
));
253 if (sc
->lmi_channel0
)
254 nglmi_startup_auto(sc
);
256 return (EINVAL
); /* unknown hook */
261 * We have just attached to a live (we hope) node.
262 * Fire out a LMI inquiry, and then start up the timers.
265 LMI_ticker(node_p node
, hook_p hook
, void *arg1
, int arg2
)
267 sc_p sc
= NG_NODE_PRIVATE(node
);
269 if (sc
->flags
& SCF_AUTO
) {
270 ngauto_state_machine(sc
);
271 ng_callout(&sc
->handle
, node
, NULL
, NG_LMI_POLL_RATE
* hz
,
272 LMI_ticker
, NULL
, 0);
274 if (sc
->livs
++ >= sc
->liv_per_full
) {
275 nglmi_inquire(sc
, 1);
276 /* sc->livs = 0; *//* do this when we get the answer! */
278 nglmi_inquire(sc
, 0);
280 ng_callout(&sc
->handle
, node
, NULL
, sc
->liv_rate
* hz
,
281 LMI_ticker
, NULL
, 0);
286 nglmi_startup_fixed(sc_p sc
, hook_p hook
)
288 sc
->flags
|= (SCF_FIXED
| SCF_CONNECTED
);
289 sc
->lmi_channel
= hook
;
294 nglmi_startup_auto(sc_p sc
)
296 sc
->flags
|= (SCF_AUTO
| SCF_CONNECTED
);
297 sc
->poll_state
= 0; /* reset state machine */
303 nglmi_startup(sc_p sc
)
308 sc
->livs
= sc
->liv_per_full
- 1;
309 /* start off the ticker in 1 sec */
310 ng_callout(&sc
->handle
, sc
->node
, NULL
, hz
, LMI_ticker
, NULL
, 0);
314 nglmi_inquire(sc_p sc
, int full
)
317 struct ng_tag_prio
*ptag
;
321 if (sc
->lmi_channel
== NULL
)
323 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
325 log(LOG_ERR
, "nglmi: unable to start up LMI processing\n");
328 m
->m_pkthdr
.rcvif
= NULL
;
330 /* Attach a tag to packet, marking it of link level state priority, so
331 * that device driver would put it in the beginning of queue */
333 ptag
= (struct ng_tag_prio
*)m_tag_alloc(NGM_GENERIC_COOKIE
, NG_TAG_PRIO
,
334 (sizeof(struct ng_tag_prio
) - sizeof(struct m_tag
)), MB_DONTWAIT
);
335 if (ptag
!= NULL
) { /* if it failed, well, it was optional anyhow */
336 ptag
->priority
= NG_PRIO_LINKSTATE
;
337 ptag
->discardability
= -1;
338 m_tag_prepend(m
, &ptag
->tag
);
341 m
->m_data
+= 4; /* leave some room for a header */
342 cptr
= start
= mtod(m
, char *);
343 /* add in the header for an LMI inquiry. */
344 *cptr
++ = 0x03; /* UI frame */
346 *cptr
++ = 0x09; /* proto discriminator */
348 *cptr
++ = 0x08; /* proto discriminator */
349 *cptr
++ = 0x00; /* call reference */
350 *cptr
++ = 0x75; /* inquiry */
352 /* If we are Annex-D, add locking shift to codeset 5. */
354 *cptr
++ = 0x95; /* locking shift */
355 /* Add a request type */
357 *cptr
++ = 0x51; /* report type */
359 *cptr
++ = 0x01; /* report type */
360 *cptr
++ = 0x01; /* size = 1 */
362 *cptr
++ = 0x00; /* full */
364 *cptr
++ = 0x01; /* partial */
366 /* Add a link verification IE */
368 *cptr
++ = 0x53; /* verification IE */
370 *cptr
++ = 0x03; /* verification IE */
371 *cptr
++ = 0x02; /* 2 extra bytes */
372 *cptr
++ = sc
->local_seq
;
373 *cptr
++ = sc
->remote_seq
;
377 m
->m_len
= m
->m_pkthdr
.len
= cptr
- start
;
378 NG_SEND_DATA_ONLY(error
, sc
->lmi_channel
, m
);
380 /* If we've been sending requests for long enough, and there has
381 * been no response, then mark as DOWN, any DLCIs that are UP. */
382 if (sc
->seq_retries
== LMI_PATIENCE
) {
385 for (count
= 0; count
< MAXDLCI
; count
++)
386 if (sc
->dlci_state
[count
] == DLCI_UP
)
387 sc
->dlci_state
[count
] = DLCI_DOWN
;
392 * State machine for LMI auto-detect. The transitions are ordered
393 * to try the more likely possibilities first.
396 ngauto_state_machine(sc_p sc
)
398 if ((sc
->poll_count
<= 0) || (sc
->poll_count
> LMIPOLLSIZE
)) {
399 /* time to change states in the auto probe machine */
400 /* capture wild values of poll_count while we are at it */
401 sc
->poll_count
= LMIPOLLSIZE
;
404 switch (sc
->poll_state
) {
406 log(LOG_WARNING
, "nglmi: no response from exchange\n");
407 default: /* capture bad states */
410 sc
->lmi_channel
= sc
->lmi_channel0
;
411 SETLMITYPE(sc
, SCF_ANNEX_D
);
414 sc
->lmi_channel
= sc
->lmi_channel1023
;
415 SETLMITYPE(sc
, SCF_ANNEX_D
);
418 sc
->lmi_channel
= sc
->lmi_channel0
;
419 SETLMITYPE(sc
, SCF_ANNEX_A
);
422 sc
->lmi_channel
= sc
->lmi_channel1023
;
423 SETLMITYPE(sc
, SCF_GROUP4
);
426 sc
->lmi_channel
= sc
->lmi_channel1023
;
427 SETLMITYPE(sc
, SCF_ANNEX_A
);
430 sc
->lmi_channel
= sc
->lmi_channel0
;
431 SETLMITYPE(sc
, SCF_GROUP4
);
435 /* send an inquirey encoded appropriatly */
436 nglmi_inquire(sc
, 0);
441 * Receive a netgraph control message.
444 nglmi_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
446 sc_p sc
= NG_NODE_PRIVATE(node
);
447 struct ng_mesg
*resp
= NULL
;
451 NGI_GET_MSG(item
, msg
);
452 switch (msg
->header
.typecookie
) {
453 case NGM_GENERIC_COOKIE
:
454 switch (msg
->header
.cmd
) {
455 case NGM_TEXT_STATUS
:
460 NG_MKRESPONSE(resp
, msg
, NG_TEXTRESPONSE
, M_WAITOK
| M_NULLOK
);
466 pos
= sprintf(arg
, "protocol %s ", sc
->protoname
);
467 if (sc
->flags
& SCF_FIXED
)
468 pos
+= sprintf(arg
+ pos
, "fixed\n");
469 else if (sc
->flags
& SCF_AUTO
)
470 pos
+= sprintf(arg
+ pos
, "auto-detecting\n");
472 pos
+= sprintf(arg
+ pos
, "auto on dlci %d\n",
473 (sc
->lmi_channel
== sc
->lmi_channel0
) ?
475 pos
+= sprintf(arg
+ pos
,
476 "keepalive period: %d seconds\n", sc
->liv_rate
);
477 pos
+= sprintf(arg
+ pos
,
478 "unacknowledged keepalives: %ld\n",
482 && (pos
< (NG_TEXTRESPONSE
- 20)));
484 if (sc
->dlci_state
[count
]) {
485 pos
+= sprintf(arg
+ pos
,
486 "dlci %d %s\n", count
,
487 (sc
->dlci_state
[count
]
488 == DLCI_UP
) ? "up" : "down");
491 resp
->header
.arglen
= pos
+ 1;
500 switch (msg
->header
.cmd
) {
501 case NGM_LMI_GET_STATUS
:
503 struct nglmistat
*stat
;
506 NG_MKRESPONSE(resp
, msg
, sizeof(*stat
), M_WAITOK
| M_NULLOK
);
511 stat
= (struct nglmistat
*) resp
->data
;
513 sc
->protoname
, sizeof(stat
->proto
) - 1);
515 sc
->protoname
, sizeof(stat
->hook
) - 1);
516 stat
->autod
= !!(sc
->flags
& SCF_AUTO
);
517 stat
->fixed
= !!(sc
->flags
& SCF_FIXED
);
518 for (k
= 0; k
<= MAXDLCI
; k
++) {
519 switch (sc
->dlci_state
[k
]) {
521 stat
->up
[k
/ 8] |= (1 << (k
% 8));
524 stat
->seen
[k
/ 8] |= (1 << (k
% 8));
540 NG_RESPOND_MSG(error
, node
, item
, resp
);
545 #define STEPBY(stepsize) \
547 packetlen -= (stepsize); \
548 data += (stepsize); \
552 * receive data, and use it to update our status.
553 * Anything coming in on the debug port is discarded.
556 nglmi_rcvdata(hook_p hook
, item_p item
)
558 sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
562 int resptype_seen
= 0;
567 if (NG_HOOK_PRIVATE(hook
) == NULL
) {
570 packetlen
= m
->m_len
;
572 /* XXX what if it's more than 1 mbuf? */
573 if ((packetlen
> MHLEN
) && !(m
->m_flags
& M_EXT
)) {
574 log(LOG_WARNING
, "nglmi: packetlen (%d) too big\n", packetlen
);
577 if (m
->m_len
< packetlen
&& (m
= m_pullup(m
, packetlen
)) == NULL
) {
579 "nglmi: m_pullup failed for %d bytes\n", packetlen
);
582 if (nglmi_checkdata(hook
, m
) == 0)
585 /* pass the first 4 bytes (already checked in the nglmi_checkdata()) */
586 data
= mtod(m
, const u_char
*);
589 /* Now check if there is a 'locking shift'. This is only seen in
590 * Annex D frames. don't bother checking, we already did that. Don't
591 * increment immediatly as it might not be there. */
595 /* If we get this far we should consider that it is a legitimate
596 * frame and we know what it is. */
597 if (sc
->flags
& SCF_AUTO
) {
598 /* note the hook that this valid channel came from and drop
599 * out of auto probe mode. */
601 sc
->protoname
= NAME_ANNEXA
;
603 sc
->protoname
= NAME_ANNEXD
;
605 sc
->protoname
= NAME_GROUP4
;
607 log(LOG_ERR
, "nglmi: No known type\n");
610 sc
->lmi_channel
= hook
;
611 sc
->flags
&= ~SCF_AUTO
;
612 log(LOG_INFO
, "nglmi: auto-detected %s LMI on DLCI %d\n",
613 sc
->protoname
, hook
== sc
->lmi_channel0
? 0 : 1023);
616 /* While there is more data in the status packet, keep processing
617 * status items. First make sure there is enough data for the
618 * segment descriptor's length field. */
619 while (packetlen
>= 2) {
620 u_int segtype
= data
[0];
621 u_int segsize
= data
[1];
623 /* Now that we know how long it claims to be, make sure
624 * there is enough data for the next seg. */
625 if (packetlen
< segsize
+ 2)
631 log(LOG_WARNING
, "nglmi: dup MSGTYPE\n");
635 /* The remote end tells us what kind of response
636 * this is. Only expect a type 0 or 1. if we are a
637 * full status, invalidate a few DLCIs just to see
638 * that they are still ok. */
643 /* partial status, do no extra processing */
648 int idx
= sc
->invalidx
;
650 for (count
= 0; count
< 10; count
++) {
653 if (sc
->dlci_state
[idx
] == DLCI_UP
)
654 sc
->dlci_state
[idx
] = DLCI_DOWN
;
658 /* we got and we wanted one. relax
659 * now.. but don't reset to 0 if it
660 * was unrequested. */
661 if (sc
->livs
> sc
->liv_per_full
)
669 /* The remote tells us what it thinks the sequence
670 * numbers are. If it's not size 2, it must be a
671 * duplicate to have gotten this far, skip it. */
674 sc
->remote_seq
= data
[2];
675 if (sc
->local_seq
== data
[3]) {
678 /* Note that all 3 Frame protocols seem to
679 * not like 0 as a sequence number. */
680 if (sc
->local_seq
== 0)
686 /* The remote tells us about a DLCI that it knows
687 * about. There may be many of these in a single
690 case 6:/* only on 'group of 4' */
691 dlci
= ((u_short
) data
[2] & 0xff) << 8;
692 dlci
|= (data
[3] & 0xff);
693 if ((dlci
< 1024) && (dlci
> 0)) {
698 dlci
= ((u_short
) data
[2] & 0x3f) << 4;
699 dlci
|= ((data
[3] & 0x78) >> 3);
700 if ((dlci
< 1024) && (dlci
> 0)) {
701 /* set up the bottom half of the
702 * support for that dlci if it's not
703 * already been done */
704 /* store this information somewhere */
710 if (sc
->dlci_state
[dlci
] != DLCI_UP
) {
711 /* bring new DLCI to life */
712 /* may do more here some day */
713 if (sc
->dlci_state
[dlci
] != DLCI_DOWN
)
715 "nglmi: DLCI %d became active\n",
717 sc
->dlci_state
[dlci
] = DLCI_UP
;
733 * Check that a packet is entirely kosha.
734 * return 1 of ok, and 0 if not.
735 * All data is discarded if a 0 is returned.
738 nglmi_checkdata(hook_p hook
, struct mbuf
*m
)
740 sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
747 int resptype_seen
= 0; /* 0 , 1 (partial) or 2 (full) */
748 int highest_dlci
= 0;
750 packetlen
= m
->m_len
;
751 data
= mtod(m
, const u_char
*);
753 log(LOG_WARNING
, "nglmi: unexpected value in LMI(%d)\n", 1);
758 /* look at the protocol ID */
760 if (sc
->flags
& SCF_AUTO
) {
761 SETLMITYPE(sc
, SCF_NOLMI
); /* start with a clean slate */
767 SETLMITYPE(sc
, SCF_GROUP4
);
771 log(LOG_WARNING
, "nglmi: bad Protocol ID(%d)\n",
776 if (nextbyte
!= sc
->protoID
) {
777 log(LOG_WARNING
, "nglmi: unexpected Protocol ID(%d)\n",
784 /* check call reference (always null in non ISDN frame relay) */
786 log(LOG_WARNING
, "nglmi: unexpected Call Reference (0x%x)\n",
792 /* check message type */
793 switch ((type
= *data
)) {
794 case 0x75: /* Status enquiry */
795 log(LOG_WARNING
, "nglmi: unexpected message type(0x%x)\n",
798 case 0x7D: /* Status message */
802 "nglmi: unexpected msg type(0x%x) \n", (int) type
);
807 /* Now check if there is a 'locking shift'. This is only seen in
808 * Annex D frames. Don't increment immediately as it might not be
811 if (sc
->flags
& SCF_AUTO
) {
813 if (nextbyte
== 0x95) {
814 SETLMITYPE(sc
, SCF_ANNEX_D
);
817 SETLMITYPE(sc
, SCF_ANNEX_A
);
818 } else if (nextbyte
== 0x95) {
819 log(LOG_WARNING
, "nglmi: locking shift seen in G4\n");
828 "nglmi: locking shift missing\n");
831 } else if (*data
== 0x95) {
832 log(LOG_WARNING
, "nglmi: locking shift seen\n");
837 /* While there is more data in the status packet, keep processing
838 * status items. First make sure there is enough data for the
839 * segment descriptor's length field. */
840 while (packetlen
>= 2) {
841 u_int segtype
= data
[0];
842 u_int segsize
= data
[1];
844 /* Now that we know how long it claims to be, make sure
845 * there is enough data for the next seg. */
846 if (packetlen
< (segsize
+ 2)) {
847 log(LOG_WARNING
, "nglmi: IE longer than packet\n");
853 /* According to MCI's HP analyser, we should just
854 * ignore if there is mor ethan one of these (?). */
856 log(LOG_WARNING
, "nglmi: dup MSGTYPE\n");
860 log(LOG_WARNING
, "nglmi: MSGTYPE wrong size\n");
863 /* The remote end tells us what kind of response
864 * this is. Only expect a type 0 or 1. if it was a
865 * full (type 0) check we just asked for a type
869 if (sc
->livs
> sc
->liv_per_full
) {
871 "nglmi: LIV when FULL expected\n");
872 goto reject
; /* need full */
877 /* Full response is always acceptable */
882 "nglmi: Unknown report type %d\n", data
[2]);
888 /* The remote tells us what it thinks the sequence
889 * numbers are. I would have thought that there
890 * needs to be one and only one of these, but MCI
891 * want us to just ignore extras. (?) */
892 if (resptype_seen
== 0) {
893 log(LOG_WARNING
, "nglmi: no TYPE before SEQ\n");
896 if (seq_seen
!= 0) /* already seen seq numbers */
899 log(LOG_WARNING
, "nglmi: bad SEQ sts size\n");
902 if (sc
->local_seq
!= data
[3]) {
903 log(LOG_WARNING
, "nglmi: unexpected SEQ\n");
910 /* The remote tells us about a DLCI that it knows
911 * about. There may be many of these in a single
913 if (seq_seen
!= 1) { /* already seen seq numbers? */
915 "nglmi: No sequence before DLCI\n");
918 if (resptype_seen
!= 2) { /* must be full */
920 "nglmi: No resp type before DLCI\n");
926 "nglmi: wrong IE segsize\n");
929 dlci
= ((u_short
) data
[2] & 0xff) << 8;
930 dlci
|= (data
[3] & 0xff);
934 "nglmi: DLCI headersize of %d"
935 " not supported\n", segsize
- 1);
938 dlci
= ((u_short
) data
[2] & 0x3f) << 4;
939 dlci
|= ((data
[3] & 0x78) >> 3);
941 /* async can only have one of these */
942 #if 0 /* async not yet accepted */
943 if (async
&& highest_dlci
) {
945 "nglmi: Async with > 1 DLCI\n");
949 /* Annex D says these will always be Ascending, but
950 * the HP test for G4 says we should accept
951 * duplicates, so for now allow that. ( <= vs. < ) */
953 /* MCI tests want us to accept out of order for AnxD */
954 if ((!GROUP4(sc
)) && (dlci
< highest_dlci
)) {
955 /* duplicate or mis-ordered dlci */
956 /* (spec says they will increase in number) */
957 log(LOG_WARNING
, "nglmi: DLCI out of order\n");
962 log(LOG_WARNING
, "nglmi: DLCI out of range\n");
969 "nglmi: unknown LMI segment type %d\n", segtype
);
974 if (packetlen
!= 0) { /* partial junk at end? */
976 "nglmi: %d bytes extra at end of packet\n", packetlen
);
979 if (resptype_seen
== 0) {
980 log(LOG_WARNING
, "nglmi: No response type seen\n");
981 goto reject
; /* had no response type */
984 log(LOG_WARNING
, "nglmi: No sequence numbers seen\n");
985 goto reject
; /* had no sequence numbers */
994 const u_char
*bp
= mtod(m
, const u_char
*);
997 loc
= (m
->m_len
- packetlen
);
998 log(LOG_WARNING
, "nglmi: error at location %d\n", loc
);
999 while (k
< m
->m_len
) {
1002 while ((j
++ < 16) && k
< m
->m_len
) {
1003 pos
+= sprintf(buf
+ pos
, "%c%02x",
1004 ((loc
== k
) ? '>' : ' '),
1009 log(LOG_WARNING
, "nglmi: packet data:%s\n", buf
);
1011 log(LOG_WARNING
, "%04d :%s\n", k
, buf
);
1021 const u_char
*bp
= mtod(m
, const u_char
*);
1024 loc
= (m
->m_len
- packetlen
);
1025 log(LOG_WARNING
, "nglmi: error at location %d\n", loc
);
1026 while (k
< m
->m_len
) {
1029 while ((j
++ < 16) && k
< m
->m_len
) {
1030 pos
+= sprintf(buf
+ pos
, "%c%02x",
1031 ((loc
== k
) ? '>' : ' '),
1036 log(LOG_WARNING
, "nglmi: packet data:%s\n", buf
);
1038 log(LOG_WARNING
, "%04d :%s\n", k
, buf
);
1047 * Do local shutdown processing..
1048 * Cut any remaining links and free our local resources.
1051 nglmi_shutdown(node_p node
)
1053 const sc_p sc
= NG_NODE_PRIVATE(node
);
1055 NG_NODE_SET_PRIVATE(node
, NULL
);
1056 NG_NODE_UNREF(sc
->node
);
1057 FREE(sc
, M_NETGRAPH
);
1062 * Hook disconnection
1063 * For this type, removal of any link except "debug" destroys the node.
1066 nglmi_disconnect(hook_p hook
)
1068 const sc_p sc
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
1070 /* OK to remove debug hook(s) */
1071 if (NG_HOOK_PRIVATE(hook
) == NULL
)
1074 /* Stop timer if it's currently active */
1075 if (sc
->flags
& SCF_CONNECTED
)
1076 ng_uncallout(&sc
->handle
, sc
->node
);
1079 if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook
)))
1080 ng_rmnode_self(NG_HOOK_NODE(hook
));