summaryrefslogtreecommitdiffstats
path: root/sys/netatm/spans/spans_if.c
blob: 70b0ed071536ef313e0f485ecf95e8b1d04ea026 (plain)
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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*
 *
 * ===================================
 * HARP  |  Host ATM Research Platform
 * ===================================
 *
 *
 * This Host ATM Research Platform ("HARP") file (the "Software") is
 * made available by Network Computing Services, Inc. ("NetworkCS")
 * "AS IS".  NetworkCS does not provide maintenance, improvements or
 * support of any kind.
 *
 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
 * In no event shall NetworkCS be responsible for any damages, including
 * but not limited to consequential damages, arising from or relating to
 * any use of the Software or related support.
 *
 * Copyright 1994-1998 Network Computing Services, Inc.
 *
 * Copies of this Software may be made, however, the above copyright
 * notice must be reproduced on all copies.
 *
 *	@(#) $FreeBSD$
 *
 */

/*
 * SPANS Signalling Manager
 * ---------------------------
 *
 * External interfaces to SPANS manager.  Includes support for
 * running as a loadable kernel module.
 *
 */

#ifndef ATM_SPANS_MODULE
#include "opt_atm.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syslog.h>
#include <machine/clock.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netatm/port.h>
#include <netatm/queue.h>
#include <netatm/atm.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_cm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_vc.h>
#include <netatm/atm_ioctl.h>
#include <netatm/atm_sigmgr.h>
#include <netatm/atm_stack.h>
#include <netatm/atm_pcb.h>
#include <netatm/atm_var.h>

#include "spans_xdr.h"
#include <netatm/spans/spans_var.h>

#ifndef lint
__RCSID("@(#) $FreeBSD$");
#endif

/*
 * Global variables
 */
struct sp_info	spans_vcpool = {
	"spans vcc pool",		/* si_name */
	sizeof(struct spans_vccb),	/* si_blksiz */
	10,				/* si_blkcnt */
	50				/* si_maxallow */
};

struct sp_info	spans_msgpool = {
	"spans message pool",		/* si_name */
	sizeof(spans_msg),		/* si_blksiz */
	10,				/* si_blkcnt */
	50				/* si_maxallow */
};

/*
 * Local functions
 */
static int	spans_start __P((void));
static int	spans_stop __P((void));
static int	spans_attach __P((struct sigmgr *, struct atm_pif *));
static int	spans_detach __P((struct atm_pif *));
static int	spans_setup __P((Atm_connvc *, int *));
static int	spans_release __P((struct vccb *, int *));
static int	spans_accept __P((struct vccb *, int *));
static int	spans_reject __P((struct vccb *, int *));
static int	spans_ioctl __P((int, caddr_t, caddr_t));

/*
 * Local variables
 */
static struct sigmgr	*spans_mgr = NULL;


/*
 * Initialize SPANS processing
 *
 * This will be called during module loading.  We'll just register
 * the SPANS protocol descriptor and wait for a SPANS ATM interface
 * to come online.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	0	startup was successful
 *	errno	startup failed - reason indicated
 *
 */
static int
spans_start()
{
	int	err = 0;

	/*
	 * Verify software version
	 */
	if (atm_version != ATM_VERSION) {
		log(LOG_ERR, "version mismatch: spans=%d.%d kernel=%d.%d\n",
				ATM_VERS_MAJ(ATM_VERSION),
				ATM_VERS_MIN(ATM_VERSION),
				ATM_VERS_MAJ(atm_version),
				ATM_VERS_MIN(atm_version));
		return (EINVAL);
	}

	/*
	 * Allocate protocol definition structure
	 */
	spans_mgr = (struct sigmgr *)KM_ALLOC(sizeof(struct sigmgr),
			M_DEVBUF, M_NOWAIT);
	if (spans_mgr == NULL) {
		err = ENOMEM;
		goto done;
	}
	KM_ZERO(spans_mgr, sizeof(struct sigmgr));

	/*
	 * Initialize protocol invariant values
	 */
	spans_mgr->sm_proto = ATM_SIG_SPANS;
	spans_mgr->sm_attach = spans_attach;
	spans_mgr->sm_detach = spans_detach;
	spans_mgr->sm_setup = spans_setup;
	spans_mgr->sm_release = spans_release;
	spans_mgr->sm_accept = spans_accept;
	spans_mgr->sm_reject = spans_reject;
	spans_mgr->sm_free = spans_free;
	spans_mgr->sm_ioctl = spans_ioctl;

	/*
	 * Register ourselves with system
	 */
	err = atm_sigmgr_register(spans_mgr);
	if (err)
		goto done;

	/*
	 * Start up Connectionless Service
	 */
	err = spanscls_start();
	if (err)
		goto done;

done:
	return (err);
}


/*
 * Halt SPANS processing
 *
 * This should be called just prior to unloading the module from
 * memory.  All SPANS interfaces must be deregistered before the
 * protocol can be shutdown.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	0	startup was successful
 *	errno	startup failed - reason indicated
 *
 */
static int
spans_stop()
{
	int	err = 0;
	int	s = splnet();

	/*
	 * Is protocol even set up?
	 */
	if (spans_mgr) {

		/*
		 * Any protocol instances still registered?
		 */
		if (spans_mgr->sm_prinst) {

			/* Yes, can't stop now */
			err = EBUSY;
			goto done;
		}

		/*
		 * Stop Connectionless Service
		 */
		spanscls_stop();

		/*
		 * De-register from system
		 */
		err = atm_sigmgr_deregister(spans_mgr);

		/*
		 * Free up protocol block
		 */
		KM_FREE(spans_mgr, sizeof(struct sigmgr), M_DEVBUF);
		spans_mgr = NULL;

		/*
		 * Free up our storage pools
		 */
		atm_release_pool(&spans_vcpool);
		atm_release_pool(&spans_msgpool);
	} else
		err = ENXIO;

done:
	(void) splx(s);
	return (err);
}


/*
 * Attach a SPANS-controlled interface
 *
 * Each ATM physical interface must be attached with the signalling
 * manager for the interface's signalling protocol (via the
 * atm_sigmgr_attach function).  This function will handle the
 * attachment for SPANS-controlled interfaces.  A new SPANS protocol
 * instance will be created and then we'll just sit around waiting for
 * status or connection requests.
 *
 * Function must be called at splnet.
 *
 * Arguments:
 *	smp	pointer to SPANS signalling manager control block
 *	pip	pointer to ATM physical interface control block
 *
 * Returns:
 *	0	attach successful
 *	errno	attach failed - reason indicated
 *
 */
static int
spans_attach(smp, pip)
	struct sigmgr	*smp;
	struct atm_pif	*pip;
{
	int		err = 0, n = 0, s;
	struct spans	*spp = NULL;
	struct atm_nif	*np;

	ATM_DEBUG2("spans_attach: smp=%p, pip=%p\n", smp, pip);

	/*
	 * Count network interfaces attached to the physical interface.
	 * If there are more or less than one, we have big problems.
	 */
	np = pip->pif_nif;
	while (np) {
		n++;
		np = np->nif_pnext;
	}
	if (n != 1) {
		err = ETOOMANYREFS;
		goto done;
	}

	/*
	 * Allocate SPANS protocol instance control block
	 */
	spp = (struct spans *)KM_ALLOC(sizeof(struct spans),
			M_DEVBUF, M_NOWAIT);
	if (spp == NULL) {
		err = ENOMEM;
		goto done;
	}
	KM_ZERO(spp, sizeof(struct spans));

	/*
	 * Set variables in SPANS protocol instance control block
	 */
	spp->sp_state = SPANS_INIT;
	spp->sp_h_epoch = time_second;
	spp->sp_s_epoch = 0;
	spp->sp_addr.address_format = T_ATM_ABSENT;
	spp->sp_addr.address_length = 0;
	spp->sp_subaddr.address_format = T_ATM_ABSENT;
	spp->sp_subaddr.address_length = 0;
	spp->sp_probe_ct = 0;
	spp->sp_alloc_vci = SPANS_MIN_VCI;
	spp->sp_alloc_vpi = SPANS_VPI;
	spp->sp_min_vci = SPANS_MIN_VCI;
	spp->sp_max_vci = pip->pif_maxvci;

	/*
	 * Link instance into manager's chain
	 */
	LINK2TAIL((struct siginst *)spp, struct siginst, smp->sm_prinst,
			si_next);

	/*
	 * Link in interface
	 */
	spp->sp_pif = pip;
	pip->pif_sigmgr = smp;
	pip->pif_siginst = (struct siginst *) spp;

	/*
	 * Kick-start the SPANS protocol
	 */
	SPANS_TIMER(spp, 0);

	/*
	 * Notify Connectionless Service
	 */
	err = spanscls_attach(spp);

	/*
	 * Log the fact that we've attached
	 */
	if (!err)
		log(LOG_INFO, "spans: attached to interface %s%d\n",
				pip->pif_name, pip->pif_unit);

done:
	/*
	 * Reset our work if attach fails
	 */
	if (err) {
		if (spp) {
			SPANS_CANCEL(spp);
			UNLINK((struct siginst *)spp, struct siginst,
					smp->sm_prinst, si_next);
			KM_FREE(spp, sizeof(struct spans), M_DEVBUF);
		}
		s = splimp();
		pip->pif_sigmgr = NULL;
		pip->pif_siginst = NULL;
		(void) splx(s);
	}

	return (err);
}


/*
 * Detach a SPANS-controlled interface
 *
 * Each ATM physical interface may be detached from its signalling
 * manager (via the atm_sigmgr_detach function).  This function will
 * handle the detachment for all SPANS-controlled interfaces.  All
 * circuits will be immediately terminated.
 *
 * Function must be called at splnet.
 *
 * Arguments:
 *	pip	pointer to ATM physical interface control block
 *
 * Returns:
 *	0	detach successful
 *	errno	detach failed - reason indicated
 *
 */
static int
spans_detach(pip)
	struct atm_pif	*pip;
{
	struct spans		*spp;
	struct vccb		*vcp, *vnext;
	Atm_connection		*cop;
	int			err;

	ATM_DEBUG1("spans_detach: pip=%p\n", pip);

	/*
	 * Get SPANS protocol instance
	 */
	spp = (struct spans *)pip->pif_siginst;

	/*
	 * Return an error if we're already detaching
	 */
	if (spp->sp_state == SPANS_DETACH) {
		return(EALREADY);
	}

	/*
	 * Cancel any outstanding timer
	 */
	SPANS_CANCEL(spp);

	/*
	 * Notify Connectionless Service
	 */
	spanscls_detach(spp);

	/*
	 * Terminate all of our VCCs
	 */
	for (vcp = Q_HEAD(spp->sp_vccq, struct vccb); vcp; vcp = vnext) {

		vnext = Q_NEXT(vcp, struct vccb, vc_sigelem);

		/*
		 * Don't close the signalling VCC yet
		 */
		if (vcp->vc_connvc && vcp->vc_connvc->cvc_conn ==
				spp->sp_conn)
			continue;

		/*
		 * Close VCC and notify owner
		 */
		err = spans_clear_vcc(spp, (struct spans_vccb *)vcp);
		if (err) {
			log(LOG_ERR, "spans: error %d clearing VCCB %p\n",
					err, vcp);
		}
	}

	/*
	 * Now close the SPANS signalling VCC
	 */
	if ((cop = spp->sp_conn) != NULL) {
		err = atm_cm_release(cop, &spans_cause);
		if (err)
			ATM_DEBUG2(
					"spans_detach: close failed for SPANS signalling channel; cop=%p, err=%d\n",
					cop, err);
	}
	

	/*
	 * Get rid of protocol instance if there are no VCCs queued
	 */
	if (Q_HEAD(spp->sp_vccq, struct vccb) == NULL) {
		struct sigmgr   *smp = pip->pif_sigmgr;

		pip->pif_sigmgr = NULL;
		pip->pif_siginst = NULL;
		UNLINK((struct siginst *)spp, struct siginst,
				smp->sm_prinst, si_next);
		KM_FREE(spp, sizeof(struct spans), M_DEVBUF);
	} else {
		/*
		 * Otherwise, wait for protocol instance to be freed
		 * during spans_free processing for the last queued VCC.
		 */
		spp->sp_state = SPANS_DETACH;
	}

	/*
	 * Log the fact that we've detached
	 */
	log(LOG_INFO, "spans: detached from interface %s%d\n",
			pip->pif_name, pip->pif_unit);

	return (0);
}


/*
 * Open a SPANS ATM Connection
 *
 * All service user requests to open a VC connection (via
 * atm_open_connection) over an ATM interface attached to the SPANS
 * signalling manager are handled here.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	cvp	pointer to user's requested connection parameters
 *	errp	pointer to an int for extended error information
 *
 * Returns:
 *	CALL_PROCEEDING	connection establishment is in progress
 *	CALL_FAILED	connection establishment failed
 *	CALL_CONNECTED	connection has been successfully established
 *
 */
static int
spans_setup(cvp, errp)
	Atm_connvc	*cvp;
	int		*errp;
{
	struct atm_pif	*pip = cvp->cvc_attr.nif->nif_pif;
	struct spans	*spp = (struct spans *)pip->pif_siginst;
	int		rc = 0;

	ATM_DEBUG1("spans_setup: cvp=%p\n", cvp);

	/*
	 * Intialize the returned error code
	 */
	*errp = 0;

	/*
	 * Open the connection
	 */
	switch (cvp->cvc_attr.called.addr.address_format) {
	case T_ATM_PVC_ADDR:
		/*
		 * Create a PVC
		 */
		*errp = spans_open_vcc(spp, cvp);
		rc = (*errp ? CALL_FAILED : CALL_CONNECTED);
		break;

	case T_ATM_SPANS_ADDR:

		/*
		 * Create an SVC
		 */
		*errp = spans_open_vcc(spp, cvp);
		rc = (*errp ? CALL_FAILED : CALL_PROCEEDING);
		break;

	default:
		*errp = EPROTONOSUPPORT;
		rc = CALL_FAILED;
	}

	return (rc);
}


/*
 * Close a SPANS ATM Connection
 *
 * All service user requests to terminate a previously open VC
 * connection (via the atm_close_connection function), which is running
 * over an interface attached to the SPANS signalling manager, are
 * handled here.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	vcp	pointer to connection's VC control block
 *	errp	pointer to an int for extended error information
 *
 * Returns:
 *	CALL_PROCEEDING	connection termination is in progress
 *	CALL_FAILED	connection termination failed
 *	CALL_CLEARED	connection has been successfully terminated
 *
 */
static int
spans_release(vcp, errp)
	struct vccb	*vcp;
	int		*errp;
{
	int		rc = 0;
	struct atm_pif	*pip = vcp->vc_pif;
	struct spans	*spp = (struct spans *)pip->pif_siginst;

	ATM_DEBUG1("spans_release: vcp=%p\n", vcp);

	/*
	 * Initialize returned error code
	 */
	*errp = 0;

	/*
	 * Make sure VCC is open
	 */
	if ((vcp->vc_sstate == SPANS_VC_NULL) ||
			(vcp->vc_sstate == SPANS_VC_CLOSE) ||
			(vcp->vc_sstate == SPANS_VC_FREE) ||
			(vcp->vc_ustate == VCCU_NULL) ||
			(vcp->vc_ustate == VCCU_CLOSED)) {
		*errp = EALREADY;
		return(CALL_FAILED);
	}

	/*
	 * Validate the connection type (PVC or SVC)
	 */
	if (!(vcp->vc_type & (VCC_PVC | VCC_SVC))) {
		*errp = EPROTONOSUPPORT;
		return(CALL_FAILED);
	}

	/*
	 * Close the VCCB
	 */
	*errp = spans_close_vcc(spp, (struct spans_vccb *)vcp, FALSE);

	/*
	 * Set the return code
	 */
	if (vcp->vc_type & VCC_PVC) {
		rc = (*errp ? CALL_FAILED : CALL_CLEARED);
	} else {
		rc = (*errp ? CALL_FAILED : CALL_PROCEEDING);
	}

	return (rc);
}


/*
 * Accept a SPANS Open from a remote host
 *
 * A user calls this routine (via the atm_accept_call function)
 * after it is notified that an open request was received for it.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	vcp	pointer to user's VCCB
 *	errp	pointer to an int for extended error information
 *
 * Returns:
 *	CALL_PROCEEDING	connection establishment is in progress
 *	CALL_FAILED	connection establishment failed
 *	CALL_CONNECTED	connection has been successfully established
 *
 */
static int
spans_accept(vcp, errp)
	struct vccb	*vcp;
	int		*errp;
{
	struct atm_pif		*pip = vcp->vc_pif;
	struct spans		*spp = (struct spans *)pip->pif_siginst;
	struct spans_vccb	*svp = (struct spans_vccb *)vcp;

	ATM_DEBUG1("spans_accept: vcp=%p\n", vcp);

	/*
	 * Initialize the returned error code
	 */
	*errp = 0;

	/*
	 * Return an error if we're detaching
	 */
	if (spp->sp_state == SPANS_DETACH) {
		*errp = ENETDOWN;
		ATM_DEBUG0("spans_accept: detaching\n");
		return(CALL_FAILED);
	}

	/*
	 * Respond to the open request
	 */
	*errp = spans_send_open_rsp(spp, svp, SPANS_OK);
	if (*errp) {
		ATM_DEBUG0("spans_accept: spans_send_open_rsp failed\n");
		goto failed;
	}

	/*
	 * Update the VCC states
	 */
	svp->sv_sstate = SPANS_VC_OPEN;
	svp->sv_ustate = VCCU_OPEN;

	return(CALL_CONNECTED);

failed:
	/*
	 * On error, free the VCCB and return CALL_FAILED
	 */
	svp->sv_sstate = SPANS_VC_FREE;
	svp->sv_ustate = VCCU_CLOSED;
	DEQUEUE(svp, struct spans_vccb, sv_sigelem, spp->sp_vccq);
	spans_free((struct vccb *)svp);

	return(CALL_FAILED);
}


/*
 * Reject a SPANS Open from a remote host
 *
 * A user calls this routine (via the atm_reject_call function)
 * after it is notified that an open request was received for it.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	vcp	pointer to user's VCCB
 *	errp	pointer to an int for extended error information
 *
 * Returns:
 *	CALL_CLEARED	call request rejected
 *	CALL_FAILED	call rejection failed
 *
 */
static int
spans_reject(vcp, errp)
	struct vccb	*vcp;
	int		*errp;
{
	struct atm_pif		*pip = vcp->vc_pif;
	struct spans		*spp = (struct spans *)pip->pif_siginst;
	struct spans_vccb	*svp = (struct spans_vccb *)vcp;

	ATM_DEBUG1("spans_reject: vcp=%p\n", vcp);

	/*
	 * Initialize the returned error code
	 */
	*errp = 0;

	/*
	 * Return an error if we're detaching
	 */
	if (spp->sp_state == SPANS_DETACH) {
		*errp = ENETDOWN;
		ATM_DEBUG0("spans_reject: detaching\n");
		return(CALL_FAILED);
	}

	ATM_DEBUG1("spans_reject: cause code is %d\n",
			vcp->vc_connvc->cvc_attr.cause.v.cause_value);

	/*
	 * Clean up the VCCB--the connection manager will free it
	 * spans_close_vcc will send a SPANS open response
	 */
	if ((*errp = spans_close_vcc(spp, svp, TRUE)) != 0) {
		ATM_DEBUG0("spans_reject: spans_close_vcc failed\n");
		return(CALL_FAILED);
	}

	return(CALL_CLEARED);
}


/*
 * Abort a SPANS ATM Connection
 *
 * All (non-user) requests to abort a previously open VC connection (via
 * the atm_abort_connection function), which is running over an
 * interface attached to the SPANS signalling manager, are handled here.
 * The VCC owner will be notified of the request, in order to initiate
 * termination of the connection.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	vcp	pointer to connection's VC control block
 *
 * Returns:
 *	0	connection release was succesful
 *	errno	connection release failed - reason indicated
 *
 */
int
spans_abort(vcp)
	struct vccb	*vcp;
{

	/*
	 * Make sure VCC is available
	 */
	if ((vcp->vc_sstate == SPANS_VC_NULL) ||
			(vcp->vc_sstate == SPANS_VC_CLOSE) ||
			(vcp->vc_sstate == SPANS_VC_FREE) ||
			(vcp->vc_ustate == VCCU_NULL) ||
			(vcp->vc_ustate == VCCU_CLOSED)) {
		return(EALREADY);
	}

	/*
	 * Only abort once
	 */
	if (vcp->vc_sstate == SPANS_VC_ABORT) {
		return (EALREADY);
	}

	/*
	 * Cancel any timer that might be running
	 */
	SPANS_VC_CANCEL(vcp);

	/*
	 * Set immediate timer to schedule connection termination
	 */
	vcp->vc_sstate = SPANS_VC_ABORT;
	SPANS_VC_TIMER(vcp, 0);

	return (0);
}


/*
 * Free SPANS ATM connection resources
 *
 * All service user requests to free the resources of a closed
 * VCC connection (via the atm_free_connection function), which
 * is running over an interface attached to the SigPVC signalling
 * manager, are handled here.
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	vcp	pointer to connection's VC control block
 *
 * Returns:
 *	0	connection free was successful
 *	errno	connection free failed - reason indicated
 *
 */
int
spans_free(vcp)
	struct vccb	*vcp;
{
	struct atm_pif *pip = vcp->vc_pif;
	struct spans *spp = (struct spans *)pip->pif_siginst;

	ATM_DEBUG1("spans_free: vcp = %p\n", vcp);

	/*
	 * Make sure VCC has been closed
	 */
	if ((vcp->vc_ustate != VCCU_CLOSED) ||
			(vcp->vc_sstate != SPANS_VC_FREE)) {
		ATM_DEBUG2("spans_free: bad state, sstate=%d, ustate=%d\n",
				vcp->vc_sstate, vcp->vc_ustate);
		return(EEXIST);
	}

	/*
	 * Remove VCCB from protocol queue
	 */
	DEQUEUE(vcp, struct vccb, vc_sigelem, spp->sp_vccq);

	/*
	 * Free VCCB storage
	 */
	vcp->vc_ustate = VCCU_NULL;
	vcp->vc_sstate = SPANS_VC_NULL;
	atm_free((caddr_t)vcp);

	/*
	 * If we're detaching and this was the last VCC queued,
	 * get rid of the protocol instance
	 */
	if ((spp->sp_state == SPANS_DETACH) &&
			(Q_HEAD(spp->sp_vccq, struct vccb) == NULL)) {
		struct sigmgr   *smp = pip->pif_sigmgr;

		pip->pif_sigmgr = NULL;
		pip->pif_siginst = NULL;
		UNLINK((struct siginst *)spp, struct siginst, smp->sm_prinst,
				si_next);
		KM_FREE(spp, sizeof(struct spans), M_DEVBUF);
	}

	return (0);
}


/*
 * SPANS IOCTL support
 *
 * Function will be called at splnet.
 *
 * Arguments:
 *	code	PF_ATM sub-operation code
 *      data    pointer to code specific parameter data area
 *      arg1    pointer to code specific argument
 *
 * Returns:
 *	0	request procesed
 *	errno	error processing request - reason indicated
 *
 */
static int
spans_ioctl(code, data, arg1)
        int		code;
        caddr_t		data;
        caddr_t		arg1;
{
	struct atmdelreq	*adp;
	struct atminfreq	*aip;
	struct spans		*spp;
	struct spans_vccb	*svp;
	struct air_vcc_rsp	rsp;
	Atm_connection		*cop;
	int			buf_len, err = 0, i, vpi, vci;
	caddr_t			buf_addr;


	switch (code) {

	case AIOCS_DEL_PVC:
	case AIOCS_DEL_SVC:
		/*
		 * Delete a VCC
		 */
		adp = (struct atmdelreq *)data;
		spp = (struct spans *)arg1;

		/*
		 * Don't let a user close the SPANS signalling VC or
		 * the SPANS CLS VC
		 */
		vpi = adp->adr_pvc_vpi;
		vci = adp->adr_pvc_vci;
		if ((vpi == SPANS_SIG_VPI && vci == SPANS_SIG_VCI) ||
				(vpi == SPANS_CLS_VPI &&
				vci == SPANS_CLS_VCI))
			return(EINVAL);

		/*
		 * Find requested VCC
		 */
		for (svp = Q_HEAD(spp->sp_vccq, struct spans_vccb); svp;
				svp = Q_NEXT(svp, struct spans_vccb, sv_sigelem)) {
			if ((svp->sv_vpi == vpi) && (svp->sv_vci == vci))
				break;
		}
		if (svp == NULL)
			return (ENOENT);

		/*
		 * Check VCC type
		 */
		switch (code) {
		case AIOCS_DEL_PVC:
			if (!(svp->sv_type & VCC_PVC)) {
				return(EINVAL);
			}
			break;
		case AIOCS_DEL_SVC:
			if (!(svp->sv_type & VCC_SVC)) {
				return(EINVAL);
			}
			break;
		}

		/*
		 * Schedule VCC termination
		 */
		err = spans_abort((struct vccb *)svp);
		break;

	case AIOCS_INF_VCC:
		/*
		 * Return VCC information
		 */
		aip = (struct atminfreq *)data;
		spp = (struct spans *)arg1;

		buf_addr = aip->air_buf_addr;
		buf_len = aip->air_buf_len;

		/*
		 * Loop through the VCC queue
		 */
		for (svp = Q_HEAD(spp->sp_vccq, struct spans_vccb); svp;
				svp = Q_NEXT(svp, struct spans_vccb, sv_sigelem)) {
			/*
			 * Make sure there's room in the user's buffer
			 */
			if (buf_len < sizeof(rsp)) {
				err = ENOSPC;
				break;
			}

			/*
			 * Fill out the response struct for the VCC
			 */
			(void) snprintf(rsp.avp_intf,
				    sizeof(rsp.avp_intf), "%s%d",
					spp->sp_pif->pif_name,
					spp->sp_pif->pif_unit);
			rsp.avp_vpi = svp->sv_vpi;
			rsp.avp_vci = svp->sv_vci;
			rsp.avp_type = svp->sv_type;
			rsp.avp_aal = svp->sv_connvc->cvc_attr.aal.type;
			rsp.avp_sig_proto = svp->sv_proto;
			cop = svp->sv_connvc->cvc_conn;
			if (cop)
				rsp.avp_encaps = cop->co_mpx;
			else
				rsp.avp_encaps = 0;
			rsp.avp_state = svp->sv_sstate;
			KM_ZERO(rsp.avp_owners, sizeof(rsp.avp_owners));
			for (i = 0; cop && i < sizeof(rsp.avp_owners);
					cop = cop->co_next,
					i += T_ATM_APP_NAME_LEN+1) {
				strncpy(&rsp.avp_owners[i],
					cop->co_endpt->ep_getname(cop->co_toku),
					T_ATM_APP_NAME_LEN);
			}
			rsp.avp_daddr.address_format = T_ATM_SPANS_ADDR;
			rsp.avp_daddr.address_length = 
					sizeof(Atm_addr_spans);
			if (svp->sv_type & VCC_OUT) {
				spans_addr_copy(&svp->sv_conn.con_dst,
						rsp.avp_daddr.address);
			} else {
				spans_addr_copy(&svp->sv_conn.con_src,
						rsp.avp_daddr.address);
			}
			rsp.avp_dsubaddr.address_format = T_ATM_ABSENT;
			rsp.avp_dsubaddr.address_length = 0;
			rsp.avp_ipdus = svp->sv_ipdus;
			rsp.avp_opdus = svp->sv_opdus;
			rsp.avp_ibytes = svp->sv_ibytes;
			rsp.avp_obytes = svp->sv_obytes;
			rsp.avp_ierrors = svp->sv_ierrors;
			rsp.avp_oerrors = svp->sv_oerrors;
			rsp.avp_tstamp = svp->sv_tstamp;

			/*
			 * Copy the response into the user's buffer
			 */
			if ((err = copyout((caddr_t)&rsp, buf_addr,
					sizeof(rsp))) != 0)
				break;
			buf_addr += sizeof(rsp);
			buf_len -= sizeof(rsp);
		}

		/*
		 * Update the buffer pointer and length
		 */
		aip->air_buf_addr = buf_addr;
		aip->air_buf_len = buf_len;
		break;

	case AIOCS_ADD_ARP:
	case AIOCS_DEL_ARP:
	case AIOCS_INF_ARP:
	case AIOCS_INF_ASV:
		/*
		 * ARP specific ioctl's
		 */
		err = spansarp_ioctl(code, data, arg1);
		break;

	default:
		err = EOPNOTSUPP;
	}

	return (err);
}


#ifdef ATM_SPANS_MODULE
/*
 *******************************************************************
 *
 * Loadable Module Support
 *
 *******************************************************************
 */
static int	spans_doload __P((void));
static int	spans_dounload __P((void));

/*
 * Generic module load processing
 * 
 * This function is called by an OS-specific function when this
 * module is being loaded.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	0 	load was successful 
 *	errno	load failed - reason indicated
 *
 */
static int
spans_doload()
{
	int	err = 0;

	/*
	 * Start us up
	 */
	err = spans_start();
	if (err)
		/* Problems, clean up */
		(void)spans_stop();

	return (err);
}


/*
 * Generic module unload processing
 * 
 * This function is called by an OS-specific function when this
 * module is being unloaded.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	0 	unload was successful 
 *	errno	unload failed - reason indicated
 *
 */
static int
spans_dounload()
{
	int	err = 0;

	/*
	 * OK, try to clean up our mess
	 */
	err = spans_stop();

	return (err);
}




#include <sys/exec.h>
#include <sys/sysent.h>
#include <sys/lkm.h>

/*
 * Loadable miscellaneous module description
 */
MOD_MISC(spans);


/*
 * Loadable module support "load" entry point
 * 
 * This is the routine called by the lkm driver whenever the
 * modload(1) command is issued for this module.
 *
 * Arguments:
 *	lkmtp	pointer to lkm drivers's structure
 *	cmd	lkm command code
 *
 * Returns:
 *	0 	command was successful 
 *	errno	command failed - reason indicated
 *
 */
static int
spans_load(lkmtp, cmd)
	struct lkm_table	*lkmtp;
	int		cmd;
{
	return(spans_doload());
}


/*
 * Loadable module support "unload" entry point
 * 
 * This is the routine called by the lkm driver whenever the
 * modunload(1) command is issued for this module.
 *
 * Arguments:
 *	lkmtp	pointer to lkm drivers's structure
 *	cmd	lkm command code
 *
 * Returns:
 *	0 	command was successful 
 *	errno	command failed - reason indicated
 *
 */
static int
spans_unload(lkmtp, cmd)
	struct lkm_table	*lkmtp;
	int		cmd;
{
	return(spans_dounload());
}


/*
 * Loadable module support entry point
 * 
 * This is the routine called by the lkm driver for all loadable module
 * functions for this driver.  This routine name must be specified
 * on the modload(1) command.  This routine will be called whenever the
 * modload(1), modunload(1) or modstat(1) commands are issued for this
 * module.
 *
 * Arguments:
 *	lkmtp	pointer to lkm drivers's structure
 *	cmd	lkm command code
 *	ver	lkm version
 *
 * Returns:
 *	0 	command was successful 
 *	errno	command failed - reason indicated
 *
 */
int
spans_mod(lkmtp, cmd, ver)
	struct lkm_table	*lkmtp;
	int		cmd;
	int		ver;
{
	MOD_DISPATCH(spans, lkmtp, cmd, ver,
		spans_load, spans_unload, lkm_nullcmd);
}

#else	/* !ATM_SPANS_MODULE */

/*
 *******************************************************************
 *
 * Kernel Compiled Module Support
 *
 *******************************************************************
 */
static void	spans_doload __P((void *));

SYSINIT(atmspans, SI_SUB_PROTO_END, SI_ORDER_ANY, spans_doload, NULL)

/*
 * Kernel initialization
 * 
 * Arguments:
 *	arg	Not used
 *
 * Returns:
 *	none
 *
 */
static void
spans_doload(void *arg)
{
	int	err = 0;

	/*
	 * Start us up
	 */
	err = spans_start();
	if (err) {
		/* Problems, clean up */
		(void)spans_stop();

		log(LOG_ERR, "ATM SPANS unable to initialize (%d)!!\n", err);
	}
	return;
}
#endif	/* ATM_SPANS_MODULE */

OpenPOWER on IntegriCloud