5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Julian Elischer <julian@freebsd.org>
40 * $Whistle: ng_lmi.c,v 1.38 1999/11/01 09:24:52 julian Exp $
44 * This node performs the frame relay LMI protocol. It knows how
45 * to do ITU Annex A, ANSI Annex D, and "Group-of-Four" variants
48 * A specific protocol can be forced by connecting the corresponding
49 * hook to DLCI 0 or 1023 (as appropriate) of a frame relay link.
51 * Alternately, this node can do auto-detection of the LMI protocol
52 * by connecting hook "auto0" to DLCI 0 and "auto1023" to DLCI 1023.
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/errno.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/syslog.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_lmi.h>
67 * Human readable names for LMI
69 #define NAME_ANNEXA NG_LMI_HOOK_ANNEXA
70 #define NAME_ANNEXD NG_LMI_HOOK_ANNEXD
71 #define NAME_GROUP4 NG_LMI_HOOK_GROUPOF4
72 #define NAME_NONE "None"
85 * Any received LMI frame should be at least this long
87 #define LMI_MIN_LENGTH 8 /* XXX verify */
90 * Netgraph node methods and type descriptor
92 static ng_constructor_t nglmi_constructor;
93 static ng_rcvmsg_t nglmi_rcvmsg;
94 static ng_shutdown_t nglmi_shutdown;
95 static ng_newhook_t nglmi_newhook;
96 static ng_rcvdata_t nglmi_rcvdata;
97 static ng_disconnect_t nglmi_disconnect;
98 static int nglmi_checkdata(hook_p hook, struct mbuf *m);
100 static struct ng_type typestruct = {
101 .version = NG_ABI_VERSION,
102 .name = NG_LMI_NODE_TYPE,
103 .constructor = nglmi_constructor,
104 .rcvmsg = nglmi_rcvmsg,
105 .shutdown = nglmi_shutdown,
106 .newhook = nglmi_newhook,
107 .rcvdata = nglmi_rcvdata,
108 .disconnect = nglmi_disconnect,
110 NETGRAPH_INIT(lmi, &typestruct);
113 * Info and status per node
116 node_p node; /* netgraph node */
117 int flags; /* state */
118 int poll_count; /* the count of times for autolmi */
119 int poll_state; /* state of auto detect machine */
120 u_char remote_seq; /* sequence number the remote sent */
121 u_char local_seq; /* last sequence number we sent */
122 u_char protoID; /* 9 for group of 4, 8 otherwise */
123 u_long seq_retries; /* sent this how many time so far */
124 struct callout_handle handle; /* see timeout(9) */
129 hook_p lmi_channel; /* whatever we ended up using */
133 hook_p lmi_channel0; /* auto-detect on DLCI 0 */
134 hook_p lmi_channel1023;/* auto-detect on DLCI 1023 */
135 char *protoname; /* cache protocol name */
136 u_char dlci_state[MAXDLCI + 1];
137 int invalidx; /* next dlci's to invalidate */
139 typedef struct nglmi_softc *sc_p;
142 * Other internal functions
144 static void LMI_ticker(void *arg);
145 static void nglmi_startup_fixed(sc_p sc, hook_p hook);
146 static void nglmi_startup_auto(sc_p sc);
147 static void nglmi_startup(sc_p sc);
148 static void nglmi_inquire(sc_p sc, int full);
149 static void ngauto_state_machine(sc_p sc);
152 * Values for 'flags' field
153 * NB: the SCF_CONNECTED flag is set if and only if the timer is running.
155 #define SCF_CONNECTED 0x01 /* connected to something */
156 #define SCF_AUTO 0x02 /* we are auto-detecting */
157 #define SCF_FIXED 0x04 /* we are fixed from the start */
159 #define SCF_LMITYPE 0x18 /* mask for determining Annex mode */
160 #define SCF_NOLMI 0x00 /* no LMI type selected yet */
161 #define SCF_ANNEX_A 0x08 /* running annex A mode */
162 #define SCF_ANNEX_D 0x10 /* running annex D mode */
163 #define SCF_GROUP4 0x18 /* running group of 4 */
165 #define SETLMITYPE(sc, annex) \
167 (sc)->flags &= ~SCF_LMITYPE; \
168 (sc)->flags |= (annex); \
171 #define NOPROTO(sc) (((sc)->flags & SCF_LMITYPE) == SCF_NOLMI)
172 #define ANNEXA(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_A)
173 #define ANNEXD(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_D)
174 #define GROUP4(sc) (((sc)->flags & SCF_LMITYPE) == SCF_GROUP4)
176 #define LMIPOLLSIZE 3
177 #define LMI_PATIENCE 8 /* declare all DLCI DOWN after N LMI failures */
183 nglmi_constructor(node_p node)
187 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
190 callout_handle_init(&sc->handle);
191 NG_NODE_SET_PRIVATE(node, sc);
192 sc->protoname = NAME_NONE;
194 sc->liv_per_full = NG_LMI_SEQ_PER_FULL; /* make this dynamic */
195 sc->liv_rate = NG_LMI_KEEPALIVE_RATE;
200 * The LMI channel has a private pointer which is the same as the
201 * node private pointer. The debug channel has a NULL private pointer.
204 nglmi_newhook(node_p node, hook_p hook, const char *name)
206 sc_p sc = NG_NODE_PRIVATE(node);
208 if (strcmp(name, NG_LMI_HOOK_DEBUG) == 0) {
209 NG_HOOK_SET_PRIVATE(hook, NULL);
212 if (sc->flags & SCF_CONNECTED) {
213 /* already connected, return an error */
216 if (strcmp(name, NG_LMI_HOOK_ANNEXA) == 0) {
217 sc->lmi_annexA = hook;
218 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
220 SETLMITYPE(sc, SCF_ANNEX_A);
221 sc->protoname = NAME_ANNEXA;
222 nglmi_startup_fixed(sc, hook);
223 } else if (strcmp(name, NG_LMI_HOOK_ANNEXD) == 0) {
224 sc->lmi_annexD = hook;
225 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
227 SETLMITYPE(sc, SCF_ANNEX_D);
228 sc->protoname = NAME_ANNEXD;
229 nglmi_startup_fixed(sc, hook);
230 } else if (strcmp(name, NG_LMI_HOOK_GROUPOF4) == 0) {
231 sc->lmi_group4 = hook;
232 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
234 SETLMITYPE(sc, SCF_GROUP4);
235 sc->protoname = NAME_GROUP4;
236 nglmi_startup_fixed(sc, hook);
237 } else if (strcmp(name, NG_LMI_HOOK_AUTO0) == 0) {
238 /* Note this, and if B is already installed, we're complete */
239 sc->lmi_channel0 = hook;
240 sc->protoname = NAME_NONE;
241 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
242 if (sc->lmi_channel1023)
243 nglmi_startup_auto(sc);
244 } else if (strcmp(name, NG_LMI_HOOK_AUTO1023) == 0) {
245 /* Note this, and if A is already installed, we're complete */
246 sc->lmi_channel1023 = hook;
247 sc->protoname = NAME_NONE;
248 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
249 if (sc->lmi_channel0)
250 nglmi_startup_auto(sc);
252 return (EINVAL); /* unknown hook */
257 * We have just attached to a live (we hope) node.
258 * Fire out a LMI inquiry, and then start up the timers.
261 LMI_ticker(void *arg)
266 if (sc->flags & SCF_AUTO) {
267 ngauto_state_machine(sc);
268 sc->handle = timeout(LMI_ticker, sc, NG_LMI_POLL_RATE * hz);
270 if (sc->livs++ >= sc->liv_per_full) {
271 nglmi_inquire(sc, 1);
272 /* sc->livs = 0; *//* do this when we get the answer! */
274 nglmi_inquire(sc, 0);
276 sc->handle = timeout(LMI_ticker, sc, sc->liv_rate * hz);
282 nglmi_startup_fixed(sc_p sc, hook_p hook)
284 sc->flags |= (SCF_FIXED | SCF_CONNECTED);
285 sc->lmi_channel = hook;
290 nglmi_startup_auto(sc_p sc)
292 sc->flags |= (SCF_AUTO | SCF_CONNECTED);
293 sc->poll_state = 0; /* reset state machine */
299 nglmi_startup(sc_p sc)
304 sc->livs = sc->liv_per_full - 1;
305 /* start off the ticker in 1 sec */
306 sc->handle = timeout(LMI_ticker, sc, hz);
310 nglmi_inquire(sc_p sc, int full)
313 struct ng_tag_prio *ptag;
317 if (sc->lmi_channel == NULL)
319 MGETHDR(m, M_DONTWAIT, MT_DATA);
321 log(LOG_ERR, "nglmi: unable to start up LMI processing\n");
324 m->m_pkthdr.rcvif = NULL;
326 /* Attach a tag to packet, marking it of link level state priority, so
327 * that device driver would put it in the beginning of queue */
329 ptag = (struct ng_tag_prio *)m_tag_alloc(NGM_GENERIC_COOKIE, NG_TAG_PRIO,
330 (sizeof(struct ng_tag_prio) - sizeof(struct m_tag)), M_NOWAIT);
331 if (ptag != NULL) { /* if it failed, well, it was optional anyhow */
332 ptag->priority = NG_PRIO_LINKSTATE;
333 ptag->discardability = -1;
334 m_tag_prepend(m, &ptag->tag);
337 m->m_data += 4; /* leave some room for a header */
338 cptr = start = mtod(m, char *);
339 /* add in the header for an LMI inquiry. */
340 *cptr++ = 0x03; /* UI frame */
342 *cptr++ = 0x09; /* proto discriminator */
344 *cptr++ = 0x08; /* proto discriminator */
345 *cptr++ = 0x00; /* call reference */
346 *cptr++ = 0x75; /* inquiry */
348 /* If we are Annex-D, there is this extra thing.. */
350 *cptr++ = 0x95; /* ??? */
351 /* Add a request type */
353 *cptr++ = 0x51; /* report type */
355 *cptr++ = 0x01; /* report type */
356 *cptr++ = 0x01; /* size = 1 */
358 *cptr++ = 0x00; /* full */
360 *cptr++ = 0x01; /* partial */
362 /* Add a link verification IE */
364 *cptr++ = 0x53; /* verification IE */
366 *cptr++ = 0x03; /* verification IE */
367 *cptr++ = 0x02; /* 2 extra bytes */
368 *cptr++ = sc->local_seq;
369 *cptr++ = sc->remote_seq;
373 m->m_len = m->m_pkthdr.len = cptr - start;
374 NG_SEND_DATA_ONLY(error, sc->lmi_channel, m);
376 /* If we've been sending requests for long enough, and there has
377 * been no response, then mark as DOWN, any DLCIs that are UP. */
378 if (sc->seq_retries == LMI_PATIENCE) {
381 for (count = 0; count < MAXDLCI; count++)
382 if (sc->dlci_state[count] == DLCI_UP)
383 sc->dlci_state[count] = DLCI_DOWN;
388 * State machine for LMI auto-detect. The transitions are ordered
389 * to try the more likely possibilities first.
392 ngauto_state_machine(sc_p sc)
394 if ((sc->poll_count <= 0) || (sc->poll_count > LMIPOLLSIZE)) {
395 /* time to change states in the auto probe machine */
396 /* capture wild values of poll_count while we are at it */
397 sc->poll_count = LMIPOLLSIZE;
400 switch (sc->poll_state) {
402 log(LOG_WARNING, "nglmi: no response from exchange\n");
403 default: /* capture bad states */
406 sc->lmi_channel = sc->lmi_channel0;
407 SETLMITYPE(sc, SCF_ANNEX_D);
410 sc->lmi_channel = sc->lmi_channel1023;
411 SETLMITYPE(sc, SCF_ANNEX_D);
414 sc->lmi_channel = sc->lmi_channel0;
415 SETLMITYPE(sc, SCF_ANNEX_A);
418 sc->lmi_channel = sc->lmi_channel1023;
419 SETLMITYPE(sc, SCF_GROUP4);
422 sc->lmi_channel = sc->lmi_channel1023;
423 SETLMITYPE(sc, SCF_ANNEX_A);
426 sc->lmi_channel = sc->lmi_channel0;
427 SETLMITYPE(sc, SCF_GROUP4);
431 /* send an inquirey encoded appropriatly */
432 nglmi_inquire(sc, 0);
437 * Receive a netgraph control message.
440 nglmi_rcvmsg(node_p node, item_p item, hook_p lasthook)
442 sc_p sc = NG_NODE_PRIVATE(node);
443 struct ng_mesg *resp = NULL;
447 NGI_GET_MSG(item, msg);
448 switch (msg->header.typecookie) {
449 case NGM_GENERIC_COOKIE:
450 switch (msg->header.cmd) {
451 case NGM_TEXT_STATUS:
456 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
462 pos = sprintf(arg, "protocol %s ", sc->protoname);
463 if (sc->flags & SCF_FIXED)
464 pos += sprintf(arg + pos, "fixed\n");
465 else if (sc->flags & SCF_AUTO)
466 pos += sprintf(arg + pos, "auto-detecting\n");
468 pos += sprintf(arg + pos, "auto on dlci %d\n",
469 (sc->lmi_channel == sc->lmi_channel0) ?
471 pos += sprintf(arg + pos,
472 "keepalive period: %d seconds\n", sc->liv_rate);
473 pos += sprintf(arg + pos,
474 "unacknowledged keepalives: %ld\n",
478 && (pos < (NG_TEXTRESPONSE - 20)));
480 if (sc->dlci_state[count]) {
481 pos += sprintf(arg + pos,
482 "dlci %d %s\n", count,
483 (sc->dlci_state[count]
484 == DLCI_UP) ? "up" : "down");
487 resp->header.arglen = pos + 1;
496 switch (msg->header.cmd) {
497 case NGM_LMI_GET_STATUS:
499 struct nglmistat *stat;
502 NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
507 stat = (struct nglmistat *) resp->data;
509 sc->protoname, sizeof(stat->proto) - 1);
511 sc->protoname, sizeof(stat->hook) - 1);
512 stat->autod = !!(sc->flags & SCF_AUTO);
513 stat->fixed = !!(sc->flags & SCF_FIXED);
514 for (k = 0; k <= MAXDLCI; k++) {
515 switch (sc->dlci_state[k]) {
517 stat->up[k / 8] |= (1 << (k % 8));
520 stat->seen[k / 8] |= (1 << (k % 8));
536 NG_RESPOND_MSG(error, node, item, resp);
541 #define STEPBY(stepsize) \
543 packetlen -= (stepsize); \
544 data += (stepsize); \
548 * receive data, and use it to update our status.
549 * Anything coming in on the debug port is discarded.
552 nglmi_rcvdata(hook_p hook, item_p item)
554 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
558 int resptype_seen = 0;
564 if (NG_HOOK_PRIVATE(hook) == NULL) {
567 packetlen = m->m_hdr.mh_len;
569 /* XXX what if it's more than 1 mbuf? */
570 if ((packetlen > MHLEN) && !(m->m_flags & M_EXT)) {
571 log(LOG_WARNING, "nglmi: packetlen (%d) too big\n", packetlen);
574 if (m->m_len < packetlen && (m = m_pullup(m, packetlen)) == NULL) {
576 "nglmi: m_pullup failed for %d bytes\n", packetlen);
579 if (nglmi_checkdata(hook, m) == 0)
582 /* pass the first 4 bytes (already checked in the nglmi_checkdata()) */
583 data = mtod(m, const u_char *);
586 /* Now check if there is a 'locking shift'. This is only seen in
587 * Annex D frames. don't bother checking, we already did that. Don't
588 * increment immediatly as it might not be there. */
592 /* If we get this far we should consider that it is a legitimate
593 * frame and we know what it is. */
594 if (sc->flags & SCF_AUTO) {
595 /* note the hook that this valid channel came from and drop
596 * out of auto probe mode. */
598 sc->protoname = NAME_ANNEXA;
600 sc->protoname = NAME_ANNEXD;
602 sc->protoname = NAME_GROUP4;
604 log(LOG_ERR, "nglmi: No known type\n");
607 sc->lmi_channel = hook;
608 sc->flags &= ~SCF_AUTO;
609 log(LOG_INFO, "nglmi: auto-detected %s LMI on DLCI %d\n",
610 sc->protoname, hook == sc->lmi_channel0 ? 0 : 1023);
613 /* While there is more data in the status packet, keep processing
614 * status items. First make sure there is enough data for the
615 * segment descriptor's length field. */
616 while (packetlen >= 2) {
617 u_int segtype = data[0];
618 u_int segsize = data[1];
620 /* Now that we know how long it claims to be, make sure
621 * there is enough data for the next seg. */
622 if (packetlen < segsize + 2)
628 log(LOG_WARNING, "nglmi: dup MSGTYPE\n");
632 /* The remote end tells us what kind of response
633 * this is. Only expect a type 0 or 1. if we are a
634 * full status, invalidate a few DLCIs just to see
635 * that they are still ok. */
640 /* partial status, do no extra processing */
645 int idx = sc->invalidx;
647 for (count = 0; count < 10; count++) {
650 if (sc->dlci_state[idx] == DLCI_UP)
651 sc->dlci_state[idx] = DLCI_DOWN;
655 /* we got and we wanted one. relax
656 * now.. but don't reset to 0 if it
657 * was unrequested. */
658 if (sc->livs > sc->liv_per_full)
666 /* The remote tells us what it thinks the sequence
667 * numbers are. If it's not size 2, it must be a
668 * duplicate to have gotten this far, skip it. */
669 if (seq_seen != 0) /* already seen seq numbers */
673 sc->remote_seq = data[2];
674 if (sc->local_seq == data[3]) {
677 /* Note that all 3 Frame protocols seem to
678 * not like 0 as a sequence number. */
679 if (sc->local_seq == 0)
685 /* The remote tells us about a DLCI that it knows
686 * about. There may be many of these in a single
689 case 6:/* only on 'group of 4' */
690 dlci = ((u_short) data[2] & 0xff) << 8;
691 dlci |= (data[3] & 0xff);
692 if ((dlci < 1024) && (dlci > 0)) {
697 dlci = ((u_short) data[2] & 0x3f) << 4;
698 dlci |= ((data[3] & 0x78) >> 3);
699 if ((dlci < 1024) && (dlci > 0)) {
700 /* set up the bottom half of the
701 * support for that dlci if it's not
702 * already been done */
703 /* store this information somewhere */
709 if (sc->dlci_state[dlci] != DLCI_UP) {
710 /* bring new DLCI to life */
711 /* may do more here some day */
712 if (sc->dlci_state[dlci] != DLCI_DOWN)
714 "nglmi: DLCI %d became active\n",
716 sc->dlci_state[dlci] = DLCI_UP;
732 * Check that a packet is entirely kosha.
733 * return 1 of ok, and 0 if not.
734 * All data is discarded if a 0 is returned.
737 nglmi_checkdata(hook_p hook, struct mbuf *m)
739 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
746 int resptype_seen = 0; /* 0 , 1 (partial) or 2 (full) */
747 int highest_dlci = 0;
749 packetlen = m->m_hdr.mh_len;
750 data = mtod(m, const u_char *);
752 log(LOG_WARNING, "nglmi: unexpected value in LMI(%d)\n", 1);
757 /* look at the protocol ID */
759 if (sc->flags & SCF_AUTO) {
760 SETLMITYPE(sc, SCF_NOLMI); /* start with a clean slate */
766 SETLMITYPE(sc, SCF_GROUP4);
770 log(LOG_WARNING, "nglmi: bad Protocol ID(%d)\n",
775 if (nextbyte != sc->protoID) {
776 log(LOG_WARNING, "nglmi: unexpected Protocol ID(%d)\n",
783 /* check call reference (always null in non ISDN frame relay) */
785 log(LOG_WARNING, "nglmi: unexpected Call Reference (0x%x)\n",
791 /* check message type */
792 switch ((type = *data)) {
793 case 0x75: /* Status enquiry */
794 log(LOG_WARNING, "nglmi: unexpected message type(0x%x)\n",
797 case 0x7D: /* Status message */
801 "nglmi: unexpected msg type(0x%x) \n", (int) type);
806 /* Now check if there is a 'locking shift'. This is only seen in
807 * Annex D frames. Don't increment immediately as it might not be
810 if (sc->flags & SCF_AUTO) {
812 if (nextbyte == 0x95) {
813 SETLMITYPE(sc, SCF_ANNEX_D);
816 SETLMITYPE(sc, SCF_ANNEX_A);
817 } else if (nextbyte == 0x95) {
818 log(LOG_WARNING, "nglmi: locking shift seen in G4\n");
827 "nglmi: locking shift missing\n");
830 } else if (*data == 0x95) {
831 log(LOG_WARNING, "nglmi: locking shift seen\n");
836 /* While there is more data in the status packet, keep processing
837 * status items. First make sure there is enough data for the
838 * segment descriptor's length field. */
839 while (packetlen >= 2) {
840 u_int segtype = data[0];
841 u_int segsize = data[1];
843 /* Now that we know how long it claims to be, make sure
844 * there is enough data for the next seg. */
845 if (packetlen < (segsize + 2)) {
846 log(LOG_WARNING, "nglmi: IE longer than packet\n");
852 /* According to MCI's HP analyser, we should just
853 * ignore if there is mor ethan one of these (?). */
855 log(LOG_WARNING, "nglmi: dup MSGTYPE\n");
859 log(LOG_WARNING, "nglmi: MSGTYPE wrong size\n");
862 /* The remote end tells us what kind of response
863 * this is. Only expect a type 0 or 1. if it was a
864 * full (type 0) check we just asked for a type
868 if (sc->livs > sc->liv_per_full) {
870 "nglmi: LIV when FULL expected\n");
871 goto reject; /* need full */
876 /* Full response is always acceptable */
881 "nglmi: Unknown report type %d\n", data[2]);
887 /* The remote tells us what it thinks the sequence
888 * numbers are. I would have thought that there
889 * needs to be one and only one of these, but MCI
890 * want us to just ignore extras. (?) */
891 if (resptype_seen == 0) {
892 log(LOG_WARNING, "nglmi: no TYPE before SEQ\n");
895 if (seq_seen != 0) /* already seen seq numbers */
898 log(LOG_WARNING, "nglmi: bad SEQ sts size\n");
901 if (sc->local_seq != data[3]) {
902 log(LOG_WARNING, "nglmi: unexpected SEQ\n");
909 /* The remote tells us about a DLCI that it knows
910 * about. There may be many of these in a single
912 if (seq_seen != 1) { /* already seen seq numbers? */
914 "nglmi: No sequence before DLCI\n");
917 if (resptype_seen != 2) { /* must be full */
919 "nglmi: No resp type before DLCI\n");
925 "nglmi: wrong IE segsize\n");
928 dlci = ((u_short) data[2] & 0xff) << 8;
929 dlci |= (data[3] & 0xff);
933 "nglmi: DLCI headersize of %d"
934 " not supported\n", segsize - 1);
937 dlci = ((u_short) data[2] & 0x3f) << 4;
938 dlci |= ((data[3] & 0x78) >> 3);
940 /* async can only have one of these */
941 #if 0 /* async not yet accepted */
942 if (async && highest_dlci) {
944 "nglmi: Async with > 1 DLCI\n");
948 /* Annex D says these will always be Ascending, but
949 * the HP test for G4 says we should accept
950 * duplicates, so for now allow that. ( <= vs. < ) */
952 /* MCI tests want us to accept out of order for AnxD */
953 if ((!GROUP4(sc)) && (dlci < highest_dlci)) {
954 /* duplicate or mis-ordered dlci */
955 /* (spec says they will increase in number) */
956 log(LOG_WARNING, "nglmi: DLCI out of order\n");
961 log(LOG_WARNING, "nglmi: DLCI out of range\n");
968 "nglmi: unknown LMI segment type %d\n", segtype);
973 if (packetlen != 0) { /* partial junk at end? */
975 "nglmi: %d bytes extra at end of packet\n", packetlen);
978 if (resptype_seen == 0) {
979 log(LOG_WARNING, "nglmi: No response type seen\n");
980 goto reject; /* had no response type */
983 log(LOG_WARNING, "nglmi: No sequence numbers seen\n");
984 goto reject; /* had no sequence numbers */
993 const u_char *bp = mtod(m, const u_char *);
996 loc = (m->m_hdr.mh_len - packetlen);
997 log(LOG_WARNING, "nglmi: error at location %d\n", loc);
998 while (k < m->m_hdr.mh_len) {
1001 while ((j++ < 16) && k < m->m_hdr.mh_len) {
1002 pos += sprintf(buf + pos, "%c%02x",
1003 ((loc == k) ? '>' : ' '),
1008 log(LOG_WARNING, "nglmi: packet data:%s\n", buf);
1010 log(LOG_WARNING, "%04d :%s\n", k, buf);
1020 const u_char *bp = mtod(m, const u_char *);
1023 loc = (m->m_hdr.mh_len - packetlen);
1024 log(LOG_WARNING, "nglmi: error at location %d\n", loc);
1025 while (k < m->m_hdr.mh_len) {
1028 while ((j++ < 16) && k < m->m_hdr.mh_len) {
1029 pos += sprintf(buf + pos, "%c%02x",
1030 ((loc == k) ? '>' : ' '),
1035 log(LOG_WARNING, "nglmi: packet data:%s\n", buf);
1037 log(LOG_WARNING, "%04d :%s\n", k, buf);
1046 * Do local shutdown processing..
1047 * Cut any remaining links and free our local resources.
1050 nglmi_shutdown(node_p node)
1052 const sc_p sc = NG_NODE_PRIVATE(node);
1054 NG_NODE_SET_PRIVATE(node, NULL);
1055 NG_NODE_UNREF(sc->node);
1056 FREE(sc, M_NETGRAPH);
1061 * Hook disconnection
1062 * For this type, removal of any link except "debug" destroys the node.
1065 nglmi_disconnect(hook_p hook)
1067 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1069 /* OK to remove debug hook(s) */
1070 if (NG_HOOK_PRIVATE(hook) == NULL)
1073 /* Stop timer if it's currently active */
1074 if (sc->flags & SCF_CONNECTED)
1075 untimeout(LMI_ticker, sc, sc->handle);
1078 if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
1079 ng_rmnode_self(NG_HOOK_NODE(hook));