1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
/*
* ng_lmi.c
*
* Copyright (c) 1996-1999 Whistle Communications, Inc.
* All rights reserved.
*
* Subject to the following obligations and disclaimer of warranty, use and
* redistribution of this software, in source or object code forms, with or
* without modifications are expressly permitted by Whistle Communications;
* provided, however, that:
* 1. Any and all reproductions of the source or object code must include the
* copyright notice above and the following disclaimer of warranties; and
* 2. No rights are granted, in any manner or form, to use Whistle
* Communications, Inc. trademarks, including the mark "WHISTLE
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
* such appears in the above copyright notice or in the software.
*
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Julian Elischer <julian@freebsd.org>
*
* $FreeBSD$
* $Whistle: ng_lmi.c,v 1.38 1999/11/01 09:24:52 julian Exp $
*/
/*
* This node performs the frame relay LMI protocol. It knows how
* to do ITU Annex A, ANSI Annex D, and "Group-of-Four" variants
* of the protocol.
*
* A specific protocol can be forced by connecting the corresponding
* hook to DLCI 0 or 1023 (as appropriate) of a frame relay link.
*
* Alternately, this node can do auto-detection of the LMI protocol
* by connecting hook "auto0" to DLCI 0 and "auto1023" to DLCI 1023.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_lmi.h>
/*
* Human readable names for LMI
*/
#define NAME_ANNEXA NG_LMI_HOOK_ANNEXA
#define NAME_ANNEXD NG_LMI_HOOK_ANNEXD
#define NAME_GROUP4 NG_LMI_HOOK_GROUPOF4
#define NAME_NONE "None"
#define MAX_DLCIS 128
#define MAXDLCI 1023
/*
* DLCI states
*/
#define DLCI_NULL 0
#define DLCI_UP 1
#define DLCI_DOWN 2
/*
* Any received LMI frame should be at least this long
*/
#define LMI_MIN_LENGTH 8 /* XXX verify */
/*
* Netgraph node methods and type descriptor
*/
static ng_constructor_t nglmi_constructor;
static ng_rcvmsg_t nglmi_rcvmsg;
static ng_shutdown_t nglmi_shutdown;
static ng_newhook_t nglmi_newhook;
static ng_rcvdata_t nglmi_rcvdata;
static ng_disconnect_t nglmi_disconnect;
static int nglmi_checkdata(hook_p hook, struct mbuf *m);
static struct ng_type typestruct = {
NG_ABI_VERSION,
NG_LMI_NODE_TYPE,
NULL,
nglmi_constructor,
nglmi_rcvmsg,
nglmi_shutdown,
nglmi_newhook,
NULL,
NULL,
nglmi_rcvdata,
nglmi_disconnect,
NULL
};
NETGRAPH_INIT(lmi, &typestruct);
/*
* Info and status per node
*/
struct nglmi_softc {
node_p node; /* netgraph node */
int flags; /* state */
int poll_count; /* the count of times for autolmi */
int poll_state; /* state of auto detect machine */
u_char remote_seq; /* sequence number the remote sent */
u_char local_seq; /* last sequence number we sent */
u_char protoID; /* 9 for group of 4, 8 otherwise */
u_long seq_retries; /* sent this how many time so far */
struct callout_handle handle; /* see timeout(9) */
int liv_per_full;
int liv_rate;
int livs;
int need_full;
hook_p lmi_channel; /* whatever we ended up using */
hook_p lmi_annexA;
hook_p lmi_annexD;
hook_p lmi_group4;
hook_p lmi_channel0; /* auto-detect on DLCI 0 */
hook_p lmi_channel1023;/* auto-detect on DLCI 1023 */
char *protoname; /* cache protocol name */
u_char dlci_state[MAXDLCI + 1];
int invalidx; /* next dlci's to invalidate */
};
typedef struct nglmi_softc *sc_p;
/*
* Other internal functions
*/
static void LMI_ticker(void *arg);
static void nglmi_startup_fixed(sc_p sc, hook_p hook);
static void nglmi_startup_auto(sc_p sc);
static void nglmi_startup(sc_p sc);
static void nglmi_inquire(sc_p sc, int full);
static void ngauto_state_machine(sc_p sc);
/*
* Values for 'flags' field
* NB: the SCF_CONNECTED flag is set if and only if the timer is running.
*/
#define SCF_CONNECTED 0x01 /* connected to something */
#define SCF_AUTO 0x02 /* we are auto-detecting */
#define SCF_FIXED 0x04 /* we are fixed from the start */
#define SCF_LMITYPE 0x18 /* mask for determining Annex mode */
#define SCF_NOLMI 0x00 /* no LMI type selected yet */
#define SCF_ANNEX_A 0x08 /* running annex A mode */
#define SCF_ANNEX_D 0x10 /* running annex D mode */
#define SCF_GROUP4 0x18 /* running group of 4 */
#define SETLMITYPE(sc, annex) \
do { \
(sc)->flags &= ~SCF_LMITYPE; \
(sc)->flags |= (annex); \
} while (0)
#define NOPROTO(sc) (((sc)->flags & SCF_LMITYPE) == SCF_NOLMI)
#define ANNEXA(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_A)
#define ANNEXD(sc) (((sc)->flags & SCF_LMITYPE) == SCF_ANNEX_D)
#define GROUP4(sc) (((sc)->flags & SCF_LMITYPE) == SCF_GROUP4)
#define LMIPOLLSIZE 3
#define LMI_PATIENCE 8 /* declare all DLCI DOWN after N LMI failures */
/*
* Node constructor
*/
static int
nglmi_constructor(node_p node)
{
sc_p sc;
MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (sc == NULL)
return (ENOMEM);
callout_handle_init(&sc->handle);
NG_NODE_SET_PRIVATE(node, sc);
sc->protoname = NAME_NONE;
sc->node = node;
sc->liv_per_full = NG_LMI_SEQ_PER_FULL; /* make this dynamic */
sc->liv_rate = NG_LMI_KEEPALIVE_RATE;
return (0);
}
/*
* The LMI channel has a private pointer which is the same as the
* node private pointer. The debug channel has a NULL private pointer.
*/
static int
nglmi_newhook(node_p node, hook_p hook, const char *name)
{
sc_p sc = NG_NODE_PRIVATE(node);
if (strcmp(name, NG_LMI_HOOK_DEBUG) == 0) {
NG_HOOK_SET_PRIVATE(hook, NULL);
return (0);
}
if (sc->flags & SCF_CONNECTED) {
/* already connected, return an error */
return (EINVAL);
}
if (strcmp(name, NG_LMI_HOOK_ANNEXA) == 0) {
sc->lmi_annexA = hook;
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
sc->protoID = 8;
SETLMITYPE(sc, SCF_ANNEX_A);
sc->protoname = NAME_ANNEXA;
nglmi_startup_fixed(sc, hook);
} else if (strcmp(name, NG_LMI_HOOK_ANNEXD) == 0) {
sc->lmi_annexD = hook;
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
sc->protoID = 8;
SETLMITYPE(sc, SCF_ANNEX_D);
sc->protoname = NAME_ANNEXD;
nglmi_startup_fixed(sc, hook);
} else if (strcmp(name, NG_LMI_HOOK_GROUPOF4) == 0) {
sc->lmi_group4 = hook;
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
sc->protoID = 9;
SETLMITYPE(sc, SCF_GROUP4);
sc->protoname = NAME_GROUP4;
nglmi_startup_fixed(sc, hook);
} else if (strcmp(name, NG_LMI_HOOK_AUTO0) == 0) {
/* Note this, and if B is already installed, we're complete */
sc->lmi_channel0 = hook;
sc->protoname = NAME_NONE;
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
if (sc->lmi_channel1023)
nglmi_startup_auto(sc);
} else if (strcmp(name, NG_LMI_HOOK_AUTO1023) == 0) {
/* Note this, and if A is already installed, we're complete */
sc->lmi_channel1023 = hook;
sc->protoname = NAME_NONE;
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
if (sc->lmi_channel0)
nglmi_startup_auto(sc);
} else
return (EINVAL); /* unknown hook */
return (0);
}
/*
* We have just attached to a live (we hope) node.
* Fire out a LMI inquiry, and then start up the timers.
*/
static void
LMI_ticker(void *arg)
{
sc_p sc = arg;
int s = splnet();
if (sc->flags & SCF_AUTO) {
ngauto_state_machine(sc);
sc->handle = timeout(LMI_ticker, sc, NG_LMI_POLL_RATE * hz);
} else {
if (sc->livs++ >= sc->liv_per_full) {
nglmi_inquire(sc, 1);
/* sc->livs = 0; *//* do this when we get the answer! */
} else {
nglmi_inquire(sc, 0);
}
sc->handle = timeout(LMI_ticker, sc, sc->liv_rate * hz);
}
splx(s);
}
static void
nglmi_startup_fixed(sc_p sc, hook_p hook)
{
sc->flags |= (SCF_FIXED | SCF_CONNECTED);
sc->lmi_channel = hook;
nglmi_startup(sc);
}
static void
nglmi_startup_auto(sc_p sc)
{
sc->flags |= (SCF_AUTO | SCF_CONNECTED);
sc->poll_state = 0; /* reset state machine */
sc->poll_count = 0;
nglmi_startup(sc);
}
static void
nglmi_startup(sc_p sc)
{
sc->remote_seq = 0;
sc->local_seq = 1;
sc->seq_retries = 0;
sc->livs = sc->liv_per_full - 1;
/* start off the ticker in 1 sec */
sc->handle = timeout(LMI_ticker, sc, hz);
}
#define META_PAD 16
static void
nglmi_inquire(sc_p sc, int full)
{
struct mbuf *m;
char *cptr, *start;
int error;
meta_p meta = NULL;
if (sc->lmi_channel == NULL)
return;
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
log(LOG_ERR, "nglmi: unable to start up LMI processing\n");
return;
}
m->m_pkthdr.rcvif = NULL;
/* Allocate a meta struct (and leave some slop for options to be
* added by other modules). */
/* MALLOC(meta, meta_p, sizeof( struct ng_meta) + META_PAD,
* M_NETGRAPH, M_NOWAIT); */
MALLOC(meta, meta_p, sizeof(*meta) + META_PAD, M_NETGRAPH, M_NOWAIT);
if (meta != NULL) { /* if it failed, well, it was optional anyhow */
meta->used_len = (u_short) sizeof(struct ng_meta);
meta->allocated_len
= (u_short) sizeof(struct ng_meta) + META_PAD;
meta->flags = 0;
meta->priority = NG_LMI_LMI_PRIORITY;
meta->discardability = -1;
}
m->m_data += 4; /* leave some room for a header */
cptr = start = mtod(m, char *);
/* add in the header for an LMI inquiry. */
*cptr++ = 0x03; /* UI frame */
if (GROUP4(sc))
*cptr++ = 0x09; /* proto discriminator */
else
*cptr++ = 0x08; /* proto discriminator */
*cptr++ = 0x00; /* call reference */
*cptr++ = 0x75; /* inquiry */
/* If we are Annex-D, there is this extra thing.. */
if (ANNEXD(sc))
*cptr++ = 0x95; /* ??? */
/* Add a request type */
if (ANNEXA(sc))
*cptr++ = 0x51; /* report type */
else
*cptr++ = 0x01; /* report type */
*cptr++ = 0x01; /* size = 1 */
if (full)
*cptr++ = 0x00; /* full */
else
*cptr++ = 0x01; /* partial */
/* Add a link verification IE */
if (ANNEXA(sc))
*cptr++ = 0x53; /* verification IE */
else
*cptr++ = 0x03; /* verification IE */
*cptr++ = 0x02; /* 2 extra bytes */
*cptr++ = sc->local_seq;
*cptr++ = sc->remote_seq;
sc->seq_retries++;
/* Send it */
m->m_len = m->m_pkthdr.len = cptr - start;
NG_SEND_DATA(error, sc->lmi_channel, m, meta);
/* If we've been sending requests for long enough, and there has
* been no response, then mark as DOWN, any DLCIs that are UP. */
if (sc->seq_retries == LMI_PATIENCE) {
int count;
for (count = 0; count < MAXDLCI; count++)
if (sc->dlci_state[count] == DLCI_UP)
sc->dlci_state[count] = DLCI_DOWN;
}
}
/*
* State machine for LMI auto-detect. The transitions are ordered
* to try the more likely possibilities first.
*/
static void
ngauto_state_machine(sc_p sc)
{
if ((sc->poll_count <= 0) || (sc->poll_count > LMIPOLLSIZE)) {
/* time to change states in the auto probe machine */
/* capture wild values of poll_count while we are at it */
sc->poll_count = LMIPOLLSIZE;
sc->poll_state++;
}
switch (sc->poll_state) {
case 7:
log(LOG_WARNING, "nglmi: no response from exchange\n");
default: /* capture bad states */
sc->poll_state = 1;
case 1:
sc->lmi_channel = sc->lmi_channel0;
SETLMITYPE(sc, SCF_ANNEX_D);
break;
case 2:
sc->lmi_channel = sc->lmi_channel1023;
SETLMITYPE(sc, SCF_ANNEX_D);
break;
case 3:
sc->lmi_channel = sc->lmi_channel0;
SETLMITYPE(sc, SCF_ANNEX_A);
break;
case 4:
sc->lmi_channel = sc->lmi_channel1023;
SETLMITYPE(sc, SCF_GROUP4);
break;
case 5:
sc->lmi_channel = sc->lmi_channel1023;
SETLMITYPE(sc, SCF_ANNEX_A);
break;
case 6:
sc->lmi_channel = sc->lmi_channel0;
SETLMITYPE(sc, SCF_GROUP4);
break;
}
/* send an inquirey encoded appropriatly */
nglmi_inquire(sc, 0);
sc->poll_count--;
}
/*
* Receive a netgraph control message.
*/
static int
nglmi_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
sc_p sc = NG_NODE_PRIVATE(node);
struct ng_mesg *resp = NULL;
int error = 0;
struct ng_mesg *msg;
NGI_GET_MSG(item, msg);
switch (msg->header.typecookie) {
case NGM_GENERIC_COOKIE:
switch (msg->header.cmd) {
case NGM_TEXT_STATUS:
{
char *arg;
int pos, count;
NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
arg = resp->data;
pos = sprintf(arg, "protocol %s ", sc->protoname);
if (sc->flags & SCF_FIXED)
pos += sprintf(arg + pos, "fixed\n");
else if (sc->flags & SCF_AUTO)
pos += sprintf(arg + pos, "auto-detecting\n");
else
pos += sprintf(arg + pos, "auto on dlci %d\n",
(sc->lmi_channel == sc->lmi_channel0) ?
0 : 1023);
pos += sprintf(arg + pos,
"keepalive period: %d seconds\n", sc->liv_rate);
pos += sprintf(arg + pos,
"unacknowledged keepalives: %ld\n",
sc->seq_retries);
for (count = 0;
((count <= MAXDLCI)
&& (pos < (NG_TEXTRESPONSE - 20)));
count++) {
if (sc->dlci_state[count]) {
pos += sprintf(arg + pos,
"dlci %d %s\n", count,
(sc->dlci_state[count]
== DLCI_UP) ? "up" : "down");
}
}
resp->header.arglen = pos + 1;
break;
}
default:
error = EINVAL;
break;
}
break;
case NGM_LMI_COOKIE:
switch (msg->header.cmd) {
case NGM_LMI_GET_STATUS:
{
struct nglmistat *stat;
int k;
NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
if (!resp) {
error = ENOMEM;
break;
}
stat = (struct nglmistat *) resp->data;
strncpy(stat->proto,
sc->protoname, sizeof(stat->proto) - 1);
strncpy(stat->hook,
sc->protoname, sizeof(stat->hook) - 1);
stat->autod = !!(sc->flags & SCF_AUTO);
stat->fixed = !!(sc->flags & SCF_FIXED);
for (k = 0; k <= MAXDLCI; k++) {
switch (sc->dlci_state[k]) {
case DLCI_UP:
stat->up[k / 8] |= (1 << (k % 8));
/* fall through */
case DLCI_DOWN:
stat->seen[k / 8] |= (1 << (k % 8));
break;
}
}
break;
}
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
#define STEPBY(stepsize) \
do { \
packetlen -= (stepsize); \
data += (stepsize); \
} while (0)
/*
* receive data, and use it to update our status.
* Anything coming in on the debug port is discarded.
*/
static int
nglmi_rcvdata(hook_p hook, item_p item)
{
sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
u_char *data;
unsigned short dlci;
u_short packetlen;
int resptype_seen = 0;
int seq_seen = 0;
struct mbuf *m;
NGI_GET_M(item, m);
NG_FREE_ITEM(item);
if (NG_HOOK_PRIVATE(hook) == NULL) {
goto drop;
}
packetlen = m->m_hdr.mh_len;
/* XXX what if it's more than 1 mbuf? */
if ((packetlen > MHLEN) && !(m->m_flags & M_EXT)) {
log(LOG_WARNING, "nglmi: packetlen (%d) too big\n", packetlen);
goto drop;
}
if (m->m_len < packetlen && (m = m_pullup(m, packetlen)) == NULL) {
log(LOG_WARNING,
"nglmi: m_pullup failed for %d bytes\n", packetlen);
return (0);
}
if (nglmi_checkdata(hook, m) == 0)
return (0);
/* pass the first 4 bytes (already checked in the nglmi_checkdata()) */
data = mtod(m, u_char *);
STEPBY(4);
/* Now check if there is a 'locking shift'. This is only seen in
* Annex D frames. don't bother checking, we already did that. Don't
* increment immediatly as it might not be there. */
if (ANNEXD(sc))
STEPBY(1);
/* If we get this far we should consider that it is a legitimate
* frame and we know what it is. */
if (sc->flags & SCF_AUTO) {
/* note the hook that this valid channel came from and drop
* out of auto probe mode. */
if (ANNEXA(sc))
sc->protoname = NAME_ANNEXA;
else if (ANNEXD(sc))
sc->protoname = NAME_ANNEXD;
else if (GROUP4(sc))
sc->protoname = NAME_GROUP4;
else {
log(LOG_ERR, "nglmi: No known type\n");
goto drop;
}
sc->lmi_channel = hook;
sc->flags &= ~SCF_AUTO;
log(LOG_INFO, "nglmi: auto-detected %s LMI on DLCI %d\n",
sc->protoname, hook == sc->lmi_channel0 ? 0 : 1023);
}
/* While there is more data in the status packet, keep processing
* status items. First make sure there is enough data for the
* segment descriptor's length field. */
while (packetlen >= 2) {
u_int segtype = data[0];
u_int segsize = data[1];
/* Now that we know how long it claims to be, make sure
* there is enough data for the next seg. */
if (packetlen < segsize + 2)
break;
switch (segtype) {
case 0x01:
case 0x51:
if (resptype_seen) {
log(LOG_WARNING, "nglmi: dup MSGTYPE\n");
goto nextIE;
}
resptype_seen++;
/* The remote end tells us what kind of response
* this is. Only expect a type 0 or 1. if we are a
* full status, invalidate a few DLCIs just to see
* that they are still ok. */
if (segsize != 1)
goto nextIE;
switch (data[2]) {
case 1:
/* partial status, do no extra processing */
break;
case 0:
{
int count = 0;
int idx = sc->invalidx;
for (count = 0; count < 10; count++) {
if (idx > MAXDLCI)
idx = 0;
if (sc->dlci_state[idx] == DLCI_UP)
sc->dlci_state[idx] = DLCI_DOWN;
idx++;
}
sc->invalidx = idx;
/* we got and we wanted one. relax
* now.. but don't reset to 0 if it
* was unrequested. */
if (sc->livs > sc->liv_per_full)
sc->livs = 0;
break;
}
}
break;
case 0x03:
case 0x53:
/* The remote tells us what it thinks the sequence
* numbers are. If it's not size 2, it must be a
* duplicate to have gotten this far, skip it. */
if (seq_seen != 0) /* already seen seq numbers */
goto nextIE;
if (segsize != 2)
goto nextIE;
sc->remote_seq = data[2];
if (sc->local_seq == data[3]) {
sc->local_seq++;
sc->seq_retries = 0;
/* Note that all 3 Frame protocols seem to
* not like 0 as a sequence number. */
if (sc->local_seq == 0)
sc->local_seq = 1;
}
break;
case 0x07:
case 0x57:
/* The remote tells us about a DLCI that it knows
* about. There may be many of these in a single
* status response */
switch (segsize) {
case 6:/* only on 'group of 4' */
dlci = ((u_short) data[2] & 0xff) << 8;
dlci |= (data[3] & 0xff);
if ((dlci < 1024) && (dlci > 0)) {
/* XXX */
}
break;
case 3:
dlci = ((u_short) data[2] & 0x3f) << 4;
dlci |= ((data[3] & 0x78) >> 3);
if ((dlci < 1024) && (dlci > 0)) {
/* set up the bottom half of the
* support for that dlci if it's not
* already been done */
/* store this information somewhere */
}
break;
default:
goto nextIE;
}
if (sc->dlci_state[dlci] != DLCI_UP) {
/* bring new DLCI to life */
/* may do more here some day */
if (sc->dlci_state[dlci] != DLCI_DOWN)
log(LOG_INFO,
"nglmi: DLCI %d became active\n",
dlci);
sc->dlci_state[dlci] = DLCI_UP;
}
break;
}
nextIE:
STEPBY(segsize + 2);
}
NG_FREE_M(m);
return (0);
drop:
NG_FREE_M(m);
return (EINVAL);
}
/*
* Check that a packet is entirely kosha.
* return 1 of ok, and 0 if not.
* All data is discarded if a 0 is returned.
*/
static int
nglmi_checkdata(hook_p hook, struct mbuf *m)
{
sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
u_char *data;
u_short packetlen;
unsigned short dlci;
u_char type;
u_char nextbyte;
int seq_seen = 0;
int resptype_seen = 0; /* 0 , 1 (partial) or 2 (full) */
int highest_dlci = 0;
packetlen = m->m_hdr.mh_len;
data = mtod(m, u_char *);
if (*data != 0x03) {
log(LOG_WARNING, "nglmi: unexpected value in LMI(%d)\n", 1);
goto reject;
}
STEPBY(1);
/* look at the protocol ID */
nextbyte = *data;
if (sc->flags & SCF_AUTO) {
SETLMITYPE(sc, SCF_NOLMI); /* start with a clean slate */
switch (nextbyte) {
case 0x8:
sc->protoID = 8;
break;
case 0x9:
SETLMITYPE(sc, SCF_GROUP4);
sc->protoID = 9;
break;
default:
log(LOG_WARNING, "nglmi: bad Protocol ID(%d)\n",
(int) nextbyte);
goto reject;
}
} else {
if (nextbyte != sc->protoID) {
log(LOG_WARNING, "nglmi: unexpected Protocol ID(%d)\n",
(int) nextbyte);
goto reject;
}
}
STEPBY(1);
/* check call reference (always null in non ISDN frame relay) */
if (*data != 0x00) {
log(LOG_WARNING, "nglmi: unexpected Call Reference (0x%x)\n",
data[-1]);
goto reject;
}
STEPBY(1);
/* check message type */
switch ((type = *data)) {
case 0x75: /* Status enquiry */
log(LOG_WARNING, "nglmi: unexpected message type(0x%x)\n",
data[-1]);
goto reject;
case 0x7D: /* Status message */
break;
default:
log(LOG_WARNING,
"nglmi: unexpected msg type(0x%x) \n", (int) type);
goto reject;
}
STEPBY(1);
/* Now check if there is a 'locking shift'. This is only seen in
* Annex D frames. Don't increment immediately as it might not be
* there. */
nextbyte = *data;
if (sc->flags & SCF_AUTO) {
if (!(GROUP4(sc))) {
if (nextbyte == 0x95) {
SETLMITYPE(sc, SCF_ANNEX_D);
STEPBY(1);
} else
SETLMITYPE(sc, SCF_ANNEX_A);
} else if (nextbyte == 0x95) {
log(LOG_WARNING, "nglmi: locking shift seen in G4\n");
goto reject;
}
} else {
if (ANNEXD(sc)) {
if (*data == 0x95)
STEPBY(1);
else {
log(LOG_WARNING,
"nglmi: locking shift missing\n");
goto reject;
}
} else if (*data == 0x95) {
log(LOG_WARNING, "nglmi: locking shift seen\n");
goto reject;
}
}
/* While there is more data in the status packet, keep processing
* status items. First make sure there is enough data for the
* segment descriptor's length field. */
while (packetlen >= 2) {
u_int segtype = data[0];
u_int segsize = data[1];
/* Now that we know how long it claims to be, make sure
* there is enough data for the next seg. */
if (packetlen < (segsize + 2)) {
log(LOG_WARNING, "nglmi: IE longer than packet\n");
break;
}
switch (segtype) {
case 0x01:
case 0x51:
/* According to MCI's HP analyser, we should just
* ignore if there is mor ethan one of these (?). */
if (resptype_seen) {
log(LOG_WARNING, "nglmi: dup MSGTYPE\n");
goto nextIE;
}
if (segsize != 1) {
log(LOG_WARNING, "nglmi: MSGTYPE wrong size\n");
goto reject;
}
/* The remote end tells us what kind of response
* this is. Only expect a type 0 or 1. if it was a
* full (type 0) check we just asked for a type
* full. */
switch (data[2]) {
case 1:/* partial */
if (sc->livs > sc->liv_per_full) {
log(LOG_WARNING,
"nglmi: LIV when FULL expected\n");
goto reject; /* need full */
}
resptype_seen = 1;
break;
case 0:/* full */
/* Full response is always acceptable */
resptype_seen = 2;
break;
default:
log(LOG_WARNING,
"nglmi: Unknown report type %d\n", data[2]);
goto reject;
}
break;
case 0x03:
case 0x53:
/* The remote tells us what it thinks the sequence
* numbers are. I would have thought that there
* needs to be one and only one of these, but MCI
* want us to just ignore extras. (?) */
if (resptype_seen == 0) {
log(LOG_WARNING, "nglmi: no TYPE before SEQ\n");
goto reject;
}
if (seq_seen != 0) /* already seen seq numbers */
goto nextIE;
if (segsize != 2) {
log(LOG_WARNING, "nglmi: bad SEQ sts size\n");
goto reject;
}
if (sc->local_seq != data[3]) {
log(LOG_WARNING, "nglmi: unexpected SEQ\n");
goto reject;
}
seq_seen = 1;
break;
case 0x07:
case 0x57:
/* The remote tells us about a DLCI that it knows
* about. There may be many of these in a single
* status response */
if (seq_seen != 1) { /* already seen seq numbers? */
log(LOG_WARNING,
"nglmi: No sequence before DLCI\n");
goto reject;
}
if (resptype_seen != 2) { /* must be full */
log(LOG_WARNING,
"nglmi: No resp type before DLCI\n");
goto reject;
}
if (GROUP4(sc)) {
if (segsize != 6) {
log(LOG_WARNING,
"nglmi: wrong IE segsize\n");
goto reject;
}
dlci = ((u_short) data[2] & 0xff) << 8;
dlci |= (data[3] & 0xff);
} else {
if (segsize != 3) {
log(LOG_WARNING,
"nglmi: DLCI headersize of %d"
" not supported\n", segsize - 1);
goto reject;
}
dlci = ((u_short) data[2] & 0x3f) << 4;
dlci |= ((data[3] & 0x78) >> 3);
}
/* async can only have one of these */
#if 0 /* async not yet accepted */
if (async && highest_dlci) {
log(LOG_WARNING,
"nglmi: Async with > 1 DLCI\n");
goto reject;
}
#endif
/* Annex D says these will always be Ascending, but
* the HP test for G4 says we should accept
* duplicates, so for now allow that. ( <= vs. < ) */
#if 0
/* MCI tests want us to accept out of order for AnxD */
if ((!GROUP4(sc)) && (dlci < highest_dlci)) {
/* duplicate or mis-ordered dlci */
/* (spec says they will increase in number) */
log(LOG_WARNING, "nglmi: DLCI out of order\n");
goto reject;
}
#endif
if (dlci > 1023) {
log(LOG_WARNING, "nglmi: DLCI out of range\n");
goto reject;
}
highest_dlci = dlci;
break;
default:
log(LOG_WARNING,
"nglmi: unknown LMI segment type %d\n", segtype);
}
nextIE:
STEPBY(segsize + 2);
}
if (packetlen != 0) { /* partial junk at end? */
log(LOG_WARNING,
"nglmi: %d bytes extra at end of packet\n", packetlen);
goto print;
}
if (resptype_seen == 0) {
log(LOG_WARNING, "nglmi: No response type seen\n");
goto reject; /* had no response type */
}
if (seq_seen == 0) {
log(LOG_WARNING, "nglmi: No sequence numbers seen\n");
goto reject; /* had no sequence numbers */
}
return (1);
print:
{
int i, j, k, pos;
char buf[100];
int loc;
u_char *bp = mtod(m, u_char *);
k = i = 0;
loc = (m->m_hdr.mh_len - packetlen);
log(LOG_WARNING, "nglmi: error at location %d\n", loc);
while (k < m->m_hdr.mh_len) {
pos = 0;
j = 0;
while ((j++ < 16) && k < m->m_hdr.mh_len) {
pos += sprintf(buf + pos, "%c%02x",
((loc == k) ? '>' : ' '),
bp[k]);
k++;
}
if (i == 0)
log(LOG_WARNING, "nglmi: packet data:%s\n", buf);
else
log(LOG_WARNING, "%04d :%s\n", k, buf);
i++;
}
}
return (1);
reject:
{
int i, j, k, pos;
char buf[100];
int loc;
u_char *bp = mtod(m, u_char *);
k = i = 0;
loc = (m->m_hdr.mh_len - packetlen);
log(LOG_WARNING, "nglmi: error at location %d\n", loc);
while (k < m->m_hdr.mh_len) {
pos = 0;
j = 0;
while ((j++ < 16) && k < m->m_hdr.mh_len) {
pos += sprintf(buf + pos, "%c%02x",
((loc == k) ? '>' : ' '),
bp[k]);
k++;
}
if (i == 0)
log(LOG_WARNING, "nglmi: packet data:%s\n", buf);
else
log(LOG_WARNING, "%04d :%s\n", k, buf);
i++;
}
}
NG_FREE_M(m);
return (0);
}
/*
* Do local shutdown processing..
* Cut any remaining links and free our local resources.
*/
static int
nglmi_shutdown(node_p node)
{
const sc_p sc = NG_NODE_PRIVATE(node);
NG_NODE_SET_PRIVATE(node, NULL);
NG_NODE_UNREF(sc->node);
FREE(sc, M_NETGRAPH);
return (0);
}
/*
* Hook disconnection
* For this type, removal of any link except "debug" destroys the node.
*/
static int
nglmi_disconnect(hook_p hook)
{
const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
/* OK to remove debug hook(s) */
if (NG_HOOK_PRIVATE(hook) == NULL)
return (0);
/* Stop timer if it's currently active */
if (sc->flags & SCF_CONNECTED)
untimeout(LMI_ticker, sc, sc->handle);
/* Self-destruct */
if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
ng_rmnode_self(NG_HOOK_NODE(hook));
return (0);
}
|