summaryrefslogtreecommitdiffstats
path: root/sys/netatm/atm_if.c
blob: 1825eaf0b413a8f37d0cc25d4b7d0b8486c9fdb4 (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
/*
 *
 * ===================================
 * 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$
 *
 */

/*
 * Core ATM Services
 * -----------------
 *
 * ATM interface management
 *
 */

#include <netatm/kern_include.h>

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


#if (defined(BSD) && (BSD < 199506))
extern int		ifqmaxlen;
#endif

/*
 * Local functions
 */
static int	atm_physif_ioctl __P((int, caddr_t, caddr_t));
#if (defined(BSD) && (BSD >= 199306))
static int	atm_netif_rtdel __P((struct radix_node *, void *));
#endif
static int	atm_if_ioctl __P((struct ifnet *, u_long, caddr_t));
static int	atm_ifparse __P((char *, char *, int, int *));

/*
 * Local variables
 */
static int	(*atm_ifouttbl[AF_MAX+1])
			__P((struct ifnet *, KBuffer *, struct sockaddr *))
				= {NULL};


/*
 * Register an ATM physical interface
 * 
 * Each ATM device interface must register itself here upon completing
 * its internal initialization.  This applies to both linked and loaded
 * device drivers.  The interface must be registered before a signalling
 * manager can be attached.
 *
 * Arguments:
 *	cup	pointer to interface's common unit structure
 *	name	pointer to device name string
 *	sdp	pointer to interface's stack services
 *
 * Returns:
 *	0	registration successful
 *	errno	registration failed - reason indicated
 *
 */
int
atm_physif_register(cup, name, sdp)
	Cmn_unit		*cup;
	char			*name;
	struct stack_defn	*sdp;
{
	struct atm_pif	*pip;
	int		s;

	/*
	 * See if we need to be initialized
	 */
	if (!atm_init)
		atm_initialize();

	/*
	 * Make sure we're not already registered
	 */
	if (cup->cu_flags & CUF_REGISTER) {
		return (EALREADY);
	}

	s = splnet();

	/*
	 * Make sure an interface is only registered once
	 */
	for (pip = atm_interface_head; pip != NULL; pip = pip->pif_next) {
		if ((cup->cu_unit == pip->pif_unit) && 
		    (strcmp(name, pip->pif_name) == 0)) {
			(void) splx(s);
			return (EEXIST);
		}
	}

	/*
	 * Fill in physical interface parameters
	 */
	pip = &cup->cu_pif;
	pip->pif_name = name;
	pip->pif_unit = cup->cu_unit;
	pip->pif_flags = PIF_UP;
	pip->pif_services = sdp;
	pip->pif_ioctl = atm_physif_ioctl;

	/*
	 * Link in the interface and mark us registered
	 */
	LINK2TAIL(pip, struct atm_pif, atm_interface_head, pif_next);
	cup->cu_flags |= CUF_REGISTER;

	(void) splx(s);
	return (0);
}


/*
 * De-register an ATM physical interface
 * 
 * Each ATM interface must de-register itself before downing the interface.  
 * The interface's signalling manager will be detached and any network
 * interface and VCC control blocks will be freed.  
 *
 * Arguments:
 *	cup	pointer to interface's common unit structure
 *
 * Returns:
 *	0	de-registration successful
 *	errno	de-registration failed - reason indicated
 *
 */
int
atm_physif_deregister(cup)
	Cmn_unit	*cup;
{
	struct atm_pif	*pip = (struct atm_pif *)&cup->cu_pif;
	Cmn_vcc		*cvp;
	int	err;
	int	s = splnet();

	/*
	 * Detach and deregister, if needed
	 */
	if ((cup->cu_flags & CUF_REGISTER)) {

		/*
		 * Detach from signalling manager
		 */
		if (pip->pif_sigmgr != NULL) {
			err = atm_sigmgr_detach(pip);
			if (err && (err != ENOENT)) {
				(void) splx(s);
				return (err);
			}
		}

		/*
		 * Make sure signalling manager is detached
		 */
		if (pip->pif_sigmgr != NULL) {
			(void) splx(s);
			return (EBUSY);
		}

		/*
		 * Unlink interface
		 */
		UNLINK(pip, struct atm_pif, atm_interface_head, pif_next);

		cup->cu_flags &= ~CUF_REGISTER;
	}

	/*
	 * Free all of our network interfaces
	 */
	atm_physif_freenifs(pip);

	/*
	 * Free unit's vcc information
	 */
	cvp = cup->cu_vcc;
	while (cvp) {
		atm_free(cvp);
		cvp = cvp->cv_next;
	}
	cup->cu_vcc = (Cmn_vcc *)NULL;

	(void) splx(s);

	return (0);
}


/*
 * Free all network interfaces on a physical interface
 *
 * Arguments
 *	pip		pointer to physical interface structure
 *
 * Returns
 *	none
 *
 */
void
atm_physif_freenifs(pip)
	struct atm_pif	*pip;
{
	struct atm_nif	*nip = pip->pif_nif;
	int	s = splnet();

	while ( nip ) 
	{
		/*
		 * atm_nif_detach zeros pointers - save so we can
		 * walk the chain.
		 */
		struct atm_nif	*nipp = nip->nif_pnext;

		/*
		 * Clean up network i/f trails
		 */
		atm_nif_detach ( nip );
		atm_free ((caddr_t)nip);
		nip = nipp;
	}
	pip->pif_nif = (struct atm_nif *)NULL;

	(void) splx(s);

	return;
}


/*
 * Handle physical interface ioctl's
 *
 * See <netatm/atm_ioctl.h> for definitions.
 *
 * Called at splnet.
 *
 * Arguments:
 *	code			Ioctl function (sub)code
 *	data			Data block. On input contains command,
 *					on output, contains results
 *	arg			Optional code specific arguments
 *
 * Returns:
 *	0			Request processed successfully
 *	errno			Request failed - reason code
 *
 */
static int
atm_physif_ioctl(code, data, arg)
	int	code;
	caddr_t	data;
	caddr_t	arg;
{
	struct atminfreq	*aip = (struct atminfreq *)data;
	struct atmsetreq	*asr = (struct atmsetreq *)data;
	struct atm_pif		*pip;
	struct atm_nif		*nip;
	struct sigmgr		*smp;
	struct siginst		*sip;
	struct ifnet		*ifp;
	Cmn_unit		*cup;
	Atm_config		*acp;
	caddr_t			buf = aip->air_buf_addr;
	struct air_phy_stat_rsp	*apsp;
	struct air_int_rsp	apr;
	struct air_netif_rsp	anr;
	struct air_cfg_rsp	acr;
	int			count, len, buf_len = aip->air_buf_len;
	int			err = 0;
	char			ifname[2*IFNAMSIZ];
#if (defined(BSD) && (BSD >= 199103))
	struct ifaddr		*ifa;
	struct in_ifaddr	*ia;
	struct sockaddr_dl	*sdl;
#endif
 

	switch ( aip->air_opcode ) {

	case AIOCS_INF_INT:
		/*
		 * Get physical interface information
		 */
		aip = (struct atminfreq *)data;
		pip = (struct atm_pif *)arg;

		/*
		 * Make sure there's room in user buffer
		 */
		if (aip->air_buf_len < sizeof(apr)) {
			err = ENOSPC;
			break;
		}

		/*
		 * Fill in info to be returned
		 */
		KM_ZERO((caddr_t)&apr, sizeof(apr));
		smp = pip->pif_sigmgr;
		sip = pip->pif_siginst;
		(void) snprintf(apr.anp_intf, sizeof(apr.anp_intf),
			"%s%d", pip->pif_name, pip->pif_unit );
		if ( pip->pif_nif )
		{
			strcpy(apr.anp_nif_pref, pip->pif_nif->nif_if.if_name);

			nip = pip->pif_nif;
			while ( nip ) {
				apr.anp_nif_cnt++;
				nip = nip->nif_pnext;
			}
		}
		if (sip) {
			ATM_ADDR_COPY(&sip->si_addr, &apr.anp_addr);
			ATM_ADDR_COPY(&sip->si_subaddr, &apr.anp_subaddr);
			apr.anp_sig_proto = smp->sm_proto;
			apr.anp_sig_state = sip->si_state;
		}

		/*
		 * Copy data to user buffer
		 */
		err = copyout((caddr_t)&apr, aip->air_buf_addr, sizeof(apr));
		if (err)
			break;

		/*
		 * Update buffer pointer/count
		 */
		aip->air_buf_addr += sizeof(apr);
		aip->air_buf_len -= sizeof(apr);
		break;

	case AIOCS_INF_NIF:
		/*
		 * Get network interface information
		 */
		aip = (struct atminfreq *)data;
		nip = (struct atm_nif *)arg;
		ifp = &nip->nif_if;
		pip = nip->nif_pif;

		/*
		 * Make sure there's room in user buffer
		 */
		if (aip->air_buf_len < sizeof(anr)) {
			err = ENOSPC;
			break;
		}

		/*
		 * Fill in info to be returned
		 */
		KM_ZERO((caddr_t)&anr, sizeof(anr));
		(void) snprintf(anr.anp_intf, sizeof(anr.anp_intf),
		    "%s%d", ifp->if_name, ifp->if_unit);
		IFP_TO_IA(ifp, ia);
		if (ia) {
			anr.anp_proto_addr = *ia->ia_ifa.ifa_addr;
		}
		(void) snprintf(anr.anp_phy_intf, sizeof(anr.anp_phy_intf),
		    "%s%d", pip->pif_name, pip->pif_unit);

		/*
		 * Copy data to user buffer
		 */
		err = copyout((caddr_t)&anr, aip->air_buf_addr, sizeof(anr));
		if (err)
			break;

		/*
		 * Update buffer pointer/count
		 */
		aip->air_buf_addr += sizeof(anr);
		aip->air_buf_len -= sizeof(anr);
		break;

	case AIOCS_INF_PIS:
		/*
		 * Get per interface statistics
		 */
		pip = (struct atm_pif *)arg;
		if ( pip == NULL )
			return ( ENXIO );
		snprintf ( ifname, sizeof(ifname),
		    "%s%d", pip->pif_name, pip->pif_unit );

		/*
		 * Cast response into users buffer
		 */
		apsp = (struct air_phy_stat_rsp *)buf;

		/*
		 * Sanity check
		 */
		len = sizeof ( struct air_phy_stat_rsp );
		if ( buf_len < len )
			return ( ENOSPC );

		/*
		 * Copy interface name into response
		 */
		if ((err = copyout ( ifname, apsp->app_intf, IFNAMSIZ)) != 0)
			break;

		/*
		 * Copy counters
		 */
		if ((err = copyout(&pip->pif_ipdus, &apsp->app_ipdus,
		    len - sizeof(apsp->app_intf))) != 0)
			break;

		/*
		 * Adjust buffer elements
		 */
		buf += len;
		buf_len -= len;

		aip->air_buf_addr = buf;
		aip->air_buf_len = buf_len;
		break;

	case AIOCS_SET_NIF:
		/*
		 * Set NIF - allow user to configure 1 or more logical
		 *	interfaces per physical interface.
		 */

		/*
		 * Get pointer to physical interface structure from
		 * ioctl argument.
		 */
		pip = (struct atm_pif *)arg;
		cup = (Cmn_unit *)pip;

		/*
		 * Sanity check - are we already connected to something?
		 */
		if ( pip->pif_sigmgr )
		{
			err = EBUSY;
			break;
		}

		/*
		 * Free any previously allocated NIFs
		 */
		atm_physif_freenifs(pip);

		/*
		 * Add list of interfaces
		 */
		for ( count = 0; count < asr->asr_nif_cnt; count++ )
		{
			nip = (struct atm_nif *)atm_allocate(cup->cu_nif_pool);
			if ( nip == NULL )
			{
				/*
				 * Destroy any successful nifs
				 */
				atm_physif_freenifs(pip);
				err = ENOMEM;
				break;
			}

			nip->nif_pif = pip;
			ifp = &nip->nif_if;

			strcpy ( nip->nif_name, asr->asr_nif_pref );
			nip->nif_sel = count;

			ifp->if_name = nip->nif_name;
			ifp->if_unit = count;
			ifp->if_mtu = ATM_NIF_MTU;
			ifp->if_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
			ifp->if_output = atm_ifoutput;
			ifp->if_ioctl = atm_if_ioctl;
			ifp->if_snd.ifq_maxlen = ifqmaxlen;
#if (defined(BSD) && (BSD >= 199103))
			/*
			 * Set if_type and if_baudrate
			 */
			ifp->if_type = IFT_ATM;
			switch ( cup->cu_config.ac_media ) {
			case MEDIA_TAXI_100:
				ifp->if_baudrate = 100000000;
				break;
			case MEDIA_TAXI_140:
				ifp->if_baudrate = 140000000;
				break;
			case MEDIA_OC3C:
			case MEDIA_OC12C:
			case MEDIA_UTP155:
				ifp->if_baudrate = 155000000;
				break;
			}
#endif

			if ((err = atm_nif_attach(nip)) != 0) {
				atm_free ( (caddr_t)nip );

				/*
				 * Destroy any successful nifs
				 */
				atm_physif_freenifs(pip);
				break;
			}
#if (defined(BSD) && (BSD >= 199103))
			/*
			 * Set macaddr in <Link> address
			 */
			ifp->if_addrlen = 6;
			ifa = ifnet_addrs[ifp->if_index - 1];
			if ( ifa ) {
				sdl = (struct sockaddr_dl *)
					ifa->ifa_addr;
				sdl->sdl_type = IFT_ETHER;
				sdl->sdl_alen = ifp->if_addrlen;
				bcopy ( (caddr_t)&cup->cu_config.ac_macaddr,
					LLADDR(sdl), ifp->if_addrlen );
			}
#endif
		}
		break;

	case AIOCS_INF_CFG:
		/*
		 * Get adapter configuration information
		 */
		aip = (struct atminfreq *)data;
		pip = (struct atm_pif *)arg;
		cup = (Cmn_unit *)pip;
		acp = &cup->cu_config;

		/*
		 * Make sure there's room in user buffer
		 */
		if (aip->air_buf_len < sizeof(acr)) {
			err = ENOSPC;
			break;
		}

		/*
		 * Fill in info to be returned
		 */
		KM_ZERO((caddr_t)&acr, sizeof(acr));
		(void) snprintf(acr.acp_intf, sizeof(acr.acp_intf),
		    "%s%d", pip->pif_name, pip->pif_unit);
		KM_COPY((caddr_t)acp, (caddr_t)&acr.acp_cfg,
				sizeof(Atm_config));

		/*
		 * Copy data to user buffer
		 */
		err = copyout((caddr_t)&acr, aip->air_buf_addr,
				sizeof(acr));
		if (err)
			break;

		/*
		 * Update buffer pointer/count
		 */
		aip->air_buf_addr += sizeof(acr);
		aip->air_buf_len -= sizeof(acr);
		break;

	case AIOCS_INF_VST:
		/*
		 * Pass off to device-specific handler
		 */
		cup = (Cmn_unit *)arg;
		if (cup == NULL)
			err = ENXIO;
		else
			err = (*cup->cu_ioctl)(code, data, arg);
		break;

	default:
		err = ENOSYS;
	}

	return ( err );
}


/*
 * Register a Network Convergence Module
 * 
 * Each ATM network convergence module must register itself here before
 * it will receive network interface status notifications. 
 *
 * Arguments:
 *	ncp	pointer to network convergence definition structure
 *
 * Returns:
 *	0	registration successful
 *	errno	registration failed - reason indicated
 *
 */
int
atm_netconv_register(ncp)
	struct atm_ncm	*ncp;
{
	struct atm_ncm	*tdp;
	int		s = splnet();

	/*
	 * See if we need to be initialized
	 */
	if (!atm_init)
		atm_initialize();

	/*
	 * Validate protocol family
	 */
	if (ncp->ncm_family > AF_MAX) {
		(void) splx(s);
		return (EINVAL);
	}

	/*
	 * Ensure no duplicates
	 */
	for (tdp = atm_netconv_head; tdp != NULL; tdp = tdp->ncm_next) {
		if (tdp->ncm_family == ncp->ncm_family) {
			(void) splx(s);
			return (EEXIST);
		}
	}

	/*
	 * Add module to list
	 */
	LINK2TAIL(ncp, struct atm_ncm, atm_netconv_head, ncm_next);

	/*
	 * Add new interface output function
	 */
	atm_ifouttbl[ncp->ncm_family] = ncp->ncm_ifoutput;

	(void) splx(s);
	return (0);
}


/*
 * De-register an ATM Network Convergence Module
 * 
 * Each ATM network convergence provider must de-register its registered 
 * service(s) before terminating.  Specifically, loaded kernel modules
 * must de-register their services before unloading themselves.
 *
 * Arguments:
 *	ncp	pointer to network convergence definition structure
 *
 * Returns:
 *	0	de-registration successful 
 *	errno	de-registration failed - reason indicated
 *
 */
int
atm_netconv_deregister(ncp)
	struct atm_ncm	*ncp;
{
	int	found, s = splnet();

	/*
	 * Remove module from list
	 */
	UNLINKF(ncp, struct atm_ncm, atm_netconv_head, ncm_next, found);

	if (!found) {
		(void) splx(s);
		return (ENOENT);
	}

	/*
	 * Remove module's interface output function
	 */
	atm_ifouttbl[ncp->ncm_family] = NULL;

	(void) splx(s);
	return (0);
}


/*
 * Attach an ATM Network Interface
 * 
 * Before an ATM network interface can be used by the system, the owning
 * device interface must attach the network interface using this function.
 * The physical interface for this network interface must have been previously
 * registered (using atm_interface_register).  The network interface will be
 * added to the kernel's interface list and to the physical interface's list.
 * The caller is responsible for initializing the control block fields.
 *
 * Arguments:
 *	nip	pointer to atm network interface control block
 *
 * Returns:
 *	0	attach successful
 *	errno	attach failed - reason indicated
 *
 */
int
atm_nif_attach(nip)
	struct atm_nif	*nip;
{
	struct atm_pif	*pip, *pip2;
	struct ifnet	*ifp;
	struct atm_ncm	*ncp;
	int		s;

	ifp = &nip->nif_if;
	pip = nip->nif_pif;

	s = splimp();

	/*
	 * Verify physical interface is registered
	 */
	for (pip2 = atm_interface_head; pip2 != NULL; pip2 = pip2->pif_next) {
		if (pip == pip2)
			break;
	}
	if ((pip == NULL) || (pip2 == NULL)) {
		(void) splx(s);
		return (EFAULT);
	}

	/*
	 * Add to system interface list 
	 */
	if_attach(ifp);

	/*
	 * Add to physical interface list
	 */
	LINK2TAIL(nip, struct atm_nif, pip->pif_nif, nif_pnext);

	/*
	 * Notify network convergence modules of new network i/f
	 */
	for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
		int	err;

		err = (*ncp->ncm_stat)(NCM_ATTACH, nip, 0);
		if (err) {
			atm_nif_detach(nip);
			(void) splx(s);
			return (err);
		}
	}

	(void) splx(s);
	return (0);
}


/*
 * Detach an ATM Network Interface
 * 
 * Before an ATM network interface control block can be freed, all kernel
 * references to/from this block must be released.  This function will delete
 * all routing references to the interface and free all interface addresses
 * for the interface.  The network interface will then be removed from the
 * kernel's interface list and from the owning physical interface's list.
 * The caller is responsible for free'ing the control block.
 *
 * Arguments:
 *	nip	pointer to atm network interface control block
 *
 * Returns:
 *	none
 *
 */
void
atm_nif_detach(nip)
	struct atm_nif	*nip;
{
	struct atm_ncm	*ncp;
	int		s, i;
	struct ifnet	*ifp = &nip->nif_if;
	struct ifaddr	*ifa;
	struct in_ifaddr	*ia;
	struct radix_node_head	*rnh;


	s = splimp();

	/*
	 * Notify convergence modules of network i/f demise
	 */
	for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
		(void) (*ncp->ncm_stat)(NCM_DETACH, nip, 0);
	}

	/*
	 * Mark interface down
	 */
	if_down(ifp);

	/*
	 * Free all interface routes and addresses
	 */
	while (1) {
		IFP_TO_IA(ifp, ia);
		if (ia == NULL)
			break;

		/* Delete interface route */
		in_ifscrub(ifp, ia);

		/* Remove interface address from queues */
		ifa = &ia->ia_ifa;
		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
		TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);

		/* Free interface address */
		IFAFREE(ifa);
	}

	/*
	 * Delete all remaining routes using this interface
	 * Unfortuneatly the only way to do this is to slog through
	 * the entire routing table looking for routes which point
	 * to this interface...oh well...
	 */
	for (i = 1; i <= AF_MAX; i++) {
		if ((rnh = rt_tables[i]) == NULL)
			continue;
		(void) rnh->rnh_walktree(rnh, atm_netif_rtdel, ifp);
	}

	/*
	 * Remove from system interface list (ie. if_detach())
	 */
	TAILQ_REMOVE(&ifnet, ifp, if_link);

	/*
	 * Remove from physical interface list
	 */
	UNLINK(nip, struct atm_nif, nip->nif_pif->pif_nif, nif_pnext);

	(void) splx(s);
}


/*
 * Delete Routes for a Network Interface
 * 
 * Called for each routing entry via the rnh->rnh_walktree() call above
 * to delete all route entries referencing a detaching network interface.
 *
 * Arguments:
 *	rn	pointer to node in the routing table
 *	arg	argument passed to rnh->rnh_walktree() - detaching interface
 *
 * Returns:
 *	0	successful
 *	errno	failed - reason indicated
 *
 */
static int
atm_netif_rtdel(rn, arg)
	struct radix_node	*rn;
	void			*arg;
{
	struct rtentry	*rt = (struct rtentry *)rn;
	struct ifnet	*ifp = arg;
	int		err;

	if (rt->rt_ifp == ifp) {

		/*
		 * Protect (sorta) against walktree recursion problems
		 * with cloned routes
		 */
		if ((rt->rt_flags & RTF_UP) == 0)
			return (0);

		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
				rt_mask(rt), rt->rt_flags,
				(struct rtentry **) NULL);
		if (err) {
			log(LOG_WARNING, "atm_netif_rtdel: error %d\n", err);
		}
	}

	return (0);
}


/*
 * Set an ATM Network Interface address
 * 
 * This is called from a device interface when processing an SIOCSIFADDR
 * ioctl request.  We just notify all convergence modules of the new address
 * and hope everyone has non-overlapping interests, since if someone reports
 * an error we don't go back and tell everyone to undo the change.
 *
 * Arguments:
 *	nip	pointer to atm network interface control block
 *	ifa	pointer to new interface address
 *
 * Returns:
 *	0	set successful
 *	errno	set failed - reason indicated
 *
 */
int
atm_nif_setaddr(nip, ifa)
	struct atm_nif	*nip;
	struct ifaddr	*ifa;
{
	struct atm_ncm	*ncp;
	int	err = 0, s = splnet();

	/*
	 * Notify convergence modules of network i/f change
	 */
	for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
		err = (*ncp->ncm_stat)(NCM_SETADDR, nip, (int)ifa);
		if (err)
			break;
	}
	(void) splx(s);

	return (err);
}


/*
 * ATM Interface Packet Output
 * 
 * All ATM network interfaces must have their ifnet if_output address set to
 * this function.  Since no existing network layer code is to be modified 
 * for ATM support, this function serves as the hook to allow network output
 * packets to be assigned to their proper outbound VCC.  Each network address
 * family which is to be supported over ATM must be assigned an output
 * packet processing function via atm_netconv_register().
 *
 * Arguments:
 *	ifp	pointer to ifnet structure
 *	m	pointer to packet buffer chain to be output
 *	dst	pointer to packet's network destination address
 *
 * Returns:
 *	0	packet queued to interface
 *	errno	output failed - reason indicated
 *
 */
int
#if (defined(BSD) && (BSD >= 199103))
atm_ifoutput(ifp, m, dst, rt)
#else
atm_ifoutput(ifp, m, dst)
#endif
	struct ifnet	*ifp;
	KBuffer		*m;
	struct sockaddr	*dst;
#if (defined(BSD) && (BSD >= 199103))
	struct rtentry	*rt;
#endif
{
	u_short		fam = dst->sa_family;
	int		(*func)__P((struct ifnet *, KBuffer *,
					struct sockaddr *));

	/*
	 * Validate address family
	 */
	if (fam > AF_MAX) {
		KB_FREEALL(m);
		return (EAFNOSUPPORT);
	}

	/*
	 * Hand packet off for dst-to-VCC mapping
	 */
	func = atm_ifouttbl[fam];
	if (func == NULL) {
		KB_FREEALL(m);
		return (EAFNOSUPPORT);
	}
	return ((*func)(ifp, m, dst));
}


/*
 * Handle interface ioctl requests. 
 *
 * Arguments:
 *	ifp		pointer to network interface structure
 *	cmd		IOCTL cmd
 *	data		arguments to/from ioctl
 *
 * Returns:
 *	error		errno value
 */
static int
atm_if_ioctl(ifp, cmd, data)
	struct ifnet *ifp;
	u_long	cmd;
	caddr_t data;
{
	register struct ifreq *ifr = (struct ifreq *)data;
	struct atm_nif	*nip = (struct atm_nif *)ifp;
	int	error = 0;
	int	s = splnet();

	switch ( cmd )
	{
	case SIOCGIFADDR:
		KM_COPY ( (caddr_t)&(nip->nif_pif->pif_macaddr),
			(caddr_t)ifr->ifr_addr.sa_data, 
			sizeof(struct mac_addr) );
		break;

	case SIOCSIFADDR:
		error = atm_nif_setaddr ( nip, (struct ifaddr *)data);
		ifp->if_flags |= IFF_UP | IFF_RUNNING | IFF_BROADCAST;
		break;

	case SIOCGIFFLAGS:
		*(short *)data = ifp->if_flags;
		break;

	case SIOCSIFFLAGS:
		break;

	default:
		error = EINVAL;
		break;
	}

	(void) splx(s);
	return ( error );
}


/*
 * Parse interface name
 * 
 * Parses an interface name string into a name and a unit component.
 *
 * Arguments:
 *	name	pointer to interface name string
 *	namep	address to store interface name
 *	size	size available at namep
 *	unitp	address to store interface unit number
 *
 * Returns:
 *	0 	name parsed
 *	else	parse error
 *
 */
static int
atm_ifparse(name, namep, size, unitp)
	char		*name;
	char		*namep;
	int		size;
	int		*unitp;
{
	char		*cp, *np;
	int		len = 0, unit = 0;

	/*
	 * Separate supplied string into name and unit parts.
	 */
	cp = name;
	np = namep;
	while (*cp) {
		if (*cp >= '0' && *cp <= '9')
			break;
		if (++len >= size)
			return (-1);
		*np++ = *cp++;
	}
	*np = '\0';
	while (*cp && *cp >= '0' && *cp <= '9')
		unit = 10 * unit + *cp++ - '0';

	*unitp = unit;

	return (0);
}


/*
 * Locate ATM physical interface via name
 * 
 * Uses the supplied interface name string to locate a registered
 * ATM physical interface.
 *
 * Arguments:
 *	name	pointer to interface name string
 *
 * Returns:
 *	0 	interface not found
 *	else	pointer to atm physical interface structure
 *
 */
struct atm_pif *
atm_pifname(name)
	char		*name;
{
	struct atm_pif	*pip;
	char		n[IFNAMSIZ];
	int		unit;

	/*
	 * Break down name
	 */
	if (atm_ifparse(name, n, sizeof(n), &unit))
		return ((struct atm_pif *)0);

	/*
	 * Look for the physical interface
	 */
	for (pip = atm_interface_head; pip; pip = pip->pif_next) {
		if ((pip->pif_unit == unit) && (strcmp(pip->pif_name, n) == 0))
			break;
	}

	return (pip);
}


/*
 * Locate ATM network interface via name
 * 
 * Uses the supplied interface name string to locate an ATM network interface.
 *
 * Arguments:
 *	name	pointer to interface name string
 *
 * Returns:
 *	0 	interface not found
 *	else	pointer to atm network interface structure
 *
 */
struct atm_nif *
atm_nifname(name)
	char		*name;
{
	struct atm_pif	*pip;
	struct atm_nif	*nip;
	char		n[IFNAMSIZ];
	int		unit;

	/*
	 * Break down name
	 */
	if (atm_ifparse(name, n, sizeof(n), &unit))
		return ((struct atm_nif *)0);

	/*
	 * Search thru each physical interface
	 */
	for (pip = atm_interface_head; pip; pip = pip->pif_next) {
		/*
		 * Looking for network interface
		 */
		for (nip = pip->pif_nif; nip; nip = nip->nif_pnext) {
			struct ifnet	*ifp = (struct ifnet *)nip;
			if ((ifp->if_unit == unit) && 
			    (strcmp(ifp->if_name, n) == 0))
				return (nip);
		}
	}
	return (NULL);
}

OpenPOWER on IntegriCloud