summaryrefslogtreecommitdiffstats
path: root/sys/netatm/ipatm/ipatm_vcm.c
blob: 76c8a551a567011ae16c0b77ebe080d62da6fcc6 (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
/*
 *
 * ===================================
 * 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$
 *
 */

/*
 * IP Over ATM Support
 * -------------------
 *
 * Virtual Channel Manager
 *
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.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_stack.h>
#include <netatm/atm_pcb.h>
#include <netatm/atm_var.h>

#include <netatm/ipatm/ipatm.h>
#include <netatm/ipatm/ipatm_var.h>
#include <netatm/ipatm/ipatm_serv.h>

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


Atm_attributes	ipatm_aal5llc = {
	NULL,			/* nif */
	CMAPI_CPCS,		/* api */
	0,			/* api_init */
	0,			/* headin */
	0,			/* headout */
	{			/* aal */
		T_ATM_PRESENT,
		ATM_AAL5
	},
	{			/* traffic */
		T_ATM_PRESENT,
		{
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			T_YES
		},
	},
	{			/* bearer */
		T_ATM_PRESENT,
		{
			T_ATM_CLASS_X,
			T_ATM_NULL,
			T_ATM_NULL,
			T_NO,
			T_ATM_1_TO_1
		}
	},
	{			/* bhli */
		T_ATM_ABSENT
	},
	{			/* blli */
		T_ATM_PRESENT,
		T_ATM_ABSENT,
		{
			{
				T_ATM_SIMPLE_ID,
			},
			{
				T_ATM_ABSENT
			}
		}
	},
	{			/* llc */
		T_ATM_PRESENT,
		{
			T_ATM_LLC_SHARING,
			IPATM_LLC_LEN,
			IPATM_LLC_HDR
		}
	},
	{			/* called */
		T_ATM_PRESENT,
	},
	{			/* calling */
		T_ATM_ABSENT
	},
	{			/* qos */
		T_ATM_PRESENT,
		{
			T_ATM_NETWORK_CODING,
			{
				T_ATM_QOS_CLASS_0,
			},
			{
				T_ATM_QOS_CLASS_0
			}
		}
	},
	{			/* transit */
		T_ATM_ABSENT
	},
	{			/* cause */
		T_ATM_ABSENT
	}
};

Atm_attributes	ipatm_aal5null = {
	NULL,			/* nif */
	CMAPI_CPCS,		/* api */
	0,			/* api_init */
	sizeof(struct ifnet *),	/* headin */
	0,			/* headout */
	{			/* aal */
		T_ATM_PRESENT,
		ATM_AAL5
	},
	{			/* traffic */
		T_ATM_PRESENT,
		{
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			T_YES
		},
	},
	{			/* bearer */
		T_ATM_PRESENT,
		{
			T_ATM_CLASS_X,
			T_ATM_NULL,
			T_ATM_NULL,
			T_NO,
			T_ATM_1_TO_1
		}
	},
	{			/* bhli */
		T_ATM_ABSENT
	},
	{			/* blli */
		T_ATM_ABSENT,
		T_ATM_ABSENT
	},
	{			/* llc */
		T_ATM_ABSENT
	},
	{			/* called */
		T_ATM_PRESENT,
	},
	{			/* calling */
		T_ATM_ABSENT
	},
	{			/* qos */
		T_ATM_PRESENT,
		{
			T_ATM_NETWORK_CODING,
			{
				T_ATM_QOS_CLASS_0,
			},
			{
				T_ATM_QOS_CLASS_0
			}
		}
	},
	{			/* transit */
		T_ATM_ABSENT
	},
	{			/* cause */
		T_ATM_ABSENT
	}
};

Atm_attributes	ipatm_aal4null = {
	NULL,			/* nif */
	CMAPI_CPCS,		/* api */
	0,			/* api_init */
	sizeof(struct ifnet *),	/* headin */
	0,			/* headout */
	{			/* aal */
		T_ATM_PRESENT,
		ATM_AAL3_4
	},
	{			/* traffic */
		T_ATM_PRESENT,
		{
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			{
				T_ATM_ABSENT,
				0,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_ATM_ABSENT,
				T_NO
			},
			T_YES
		},
	},
	{			/* bearer */
		T_ATM_PRESENT,
		{
			T_ATM_CLASS_X,
			T_ATM_NULL,
			T_ATM_NULL,
			T_NO,
			T_ATM_1_TO_1
		}
	},
	{			/* bhli */
		T_ATM_ABSENT
	},
	{			/* blli */
		T_ATM_ABSENT,
		T_ATM_ABSENT
	},
	{			/* llc */
		T_ATM_ABSENT
	},
	{			/* called */
		T_ATM_PRESENT,
	},
	{			/* calling */
		T_ATM_ABSENT
	},
	{			/* qos */
		T_ATM_PRESENT,
		{
			T_ATM_NETWORK_CODING,
			{
				T_ATM_QOS_CLASS_0,
			},
			{
				T_ATM_QOS_CLASS_0
			}
		}
	},
	{			/* transit */
		T_ATM_ABSENT
	},
	{			/* cause */
		T_ATM_ABSENT
	}
};

static struct t_atm_cause	ipatm_cause = {
	T_ATM_ITU_CODING,
	T_ATM_LOC_USER,
	0,
	{0, 0, 0, 0}
};


/*
 * Open an IP PVC
 * 
 * This function will perform all actions necessary to activate a
 * PVC for IP usage.  In particular, it will allocate control blocks, 
 * open the PVC, initialize PVC stack, and initiate whatever ARP
 * procedures are required.
 *
 * Arguments:
 *	pvp	pointer to PVC parameter structure
 *	sivp	address to return pointer to IP PVC control block
 *
 * Returns:
 *	0 	PVC was successfully opened
 *	errno	open failed - reason indicated
 *
 */
int
ipatm_openpvc(pvp, sivp)
	struct ipatmpvc	*pvp;
	struct ipvcc	**sivp;
{
	struct ipvcc	*ivp = NULL;	/* XXX pacify gcc-3.1 */
	Atm_attributes	*ap;
	Atm_addr_pvc	*pvcp;
	struct atm_nif	*nip;
	struct ip_nif	*inp;
	int	s, err = 0;

	inp = pvp->ipp_ipnif;
	nip = inp->inf_nif;

	/*
	 * Make sure interface is ready to go
	 */
	if (inp->inf_state != IPNIF_ACTIVE) {
		err = ENETDOWN;
		goto done;
	}

	/*
	 * Validate fixed destination IP address
	 */
	if (pvp->ipp_dst.sin_addr.s_addr != INADDR_ANY) {
		if (in_broadcast(pvp->ipp_dst.sin_addr, &nip->nif_if) ||
		    IN_MULTICAST(ntohl(pvp->ipp_dst.sin_addr.s_addr)) ||
		    ipatm_chknif(pvp->ipp_dst.sin_addr, inp)) {
			err = EINVAL;
			goto done;
		}
	}

	/*
	 * Allocate IP VCC block
	 */
	ivp = uma_zalloc(ipatm_vc_zone, 0);
	if (ivp == NULL) {
		err = ENOMEM;
		goto done;
	}

	/*
	 * Initialize the PVC
	 */
	ivp->iv_flags = IVF_PVC;
	if (pvp->ipp_encaps == ATM_ENC_LLC)
		ivp->iv_flags |= IVF_LLC;

	/*
	 * Fill out connection attributes
	 */
	if (pvp->ipp_aal == ATM_AAL5) {
		if (pvp->ipp_encaps == ATM_ENC_LLC)
			ap = &ipatm_aal5llc;
		else
			ap = &ipatm_aal5null;
	} else {
		ap = &ipatm_aal4null;
	}

	ap->nif = nip;
	ap->traffic.v.forward.PCR_all_traffic = nip->nif_pif->pif_pcr;
	ap->traffic.v.backward.PCR_all_traffic = nip->nif_pif->pif_pcr;
	ap->called.addr.address_format = T_ATM_PVC_ADDR;
	ap->called.addr.address_length = sizeof(Atm_addr_pvc);
	pvcp = (Atm_addr_pvc *)ap->called.addr.address;
	ATM_PVC_SET_VPI(pvcp, pvp->ipp_vpi);
	ATM_PVC_SET_VCI(pvcp, pvp->ipp_vci);
	ap->called.subaddr.address_format = T_ATM_ABSENT;
	ap->called.subaddr.address_length = 0;

	/*
	 * Create PVC
	 */
	err = atm_cm_connect(&ipatm_endpt, ivp, ap, &ivp->iv_conn);
	if (err) {
		uma_zfree(ipatm_vc_zone, ivp);
		goto done;
	}

	/*
	 * Save PVC information and link in VCC
	 */
	/* ivp->iv_ = ap->headout; */

	/*
	 * Queue VCC onto its network interface
	 */
	s = splnet();
	ipatm_vccnt++;
	ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
	ivp->iv_ipnif = inp;
	(void) splx(s);

	/*
	 * Set destination IP address and IPVCC state
	 */
	if (pvp->ipp_dst.sin_addr.s_addr == INADDR_ANY) {
		/*
		 * Initiate ARP processing
		 */
		switch ((*inp->inf_serv->is_arp_pvcopen)(ivp)) {

		case MAP_PROCEEDING:
			/*
			 * Wait for answer
			 */
			ivp->iv_state = IPVCC_ACTIVE;
			break;

		case MAP_VALID:
			/*
			 * We've got our answer already
			 */
			ivp->iv_state = IPVCC_ACTIVE;
			ivp->iv_flags |= IVF_MAPOK;
			ivp->iv_dst.s_addr = ivp->iv_arpent->am_dstip.s_addr;
			break;

		case MAP_FAILED:
			/*
			 * Try again later
			 */
			ivp->iv_state = IPVCC_ACTPENT;
			IPVCC_TIMER(ivp, 1 * ATM_HZ);
			break;

		default:
			panic("ipatm_openpvc: invalid arp_pvcopen return");
		}

	} else {
		/*
		 * Use configured IP destination
		 */
		ivp->iv_dst.s_addr = pvp->ipp_dst.sin_addr.s_addr;
		ivp->iv_state = IPVCC_ACTIVE;
		ivp->iv_flags |= IVF_MAPOK;
	}

done:
	if (err)
		*sivp = NULL;
	else
		*sivp = ivp;
	return (err);
}


/*
 * Create an IP SVC
 * 
 * This function will initiate the creation of an IP SVC.  The IP VCC
 * control block will be initialized and, if required, we will initiate
 * ARP processing in order to resolve the destination's ATM address.  Once
 * the destination ATM address is known, ipatm_opensvc() will be called.
 *
 * Arguments:
 *	ifp	pointer to destination ifnet structure
 *	daf	destination address family type
 *	dst	pointer to destination address
 *	sivp	address to return pointer to IP SVC control block
 *
 * Returns:
 *	0 	SVC creation was successfully initiated
 *	errno	creation failed - reason indicated
 *
 */
int
ipatm_createsvc(ifp, daf, dst, sivp)
	struct ifnet		*ifp;
	u_short			daf;
	caddr_t			dst;
	struct ipvcc		**sivp;
{
	struct atm_nif	*nip = (struct atm_nif *)ifp;
	struct ip_nif	*inp;
	struct ipvcc	*ivp = NULL;	/* XXX pacify gcc-3.1 */
	struct in_addr	*ip;
	Atm_addr	*atm;
	int	s, err = 0;

	/*
	 * Get IP interface and make sure its ready
	 */
	for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
		if (inp->inf_nif == nip)
			break;
	}
	if (inp == NULL) {
		err = ENXIO;
		goto done;
	}
	if (inp->inf_state != IPNIF_ACTIVE) {
		err = ENETDOWN;
		goto done;
	}

	/*
	 * Validate destination address
	 */
	if (daf == AF_INET) {
		/*
		 * Destination is IP address
		 */
		ip = (struct in_addr *)dst;
		atm = NULL;
		if (ip->s_addr == INADDR_ANY) {
			err = EADDRNOTAVAIL;
			goto done;
		}
	} else if (daf == AF_ATM) {
		/*
		 * Destination is ATM address
		 */
		atm = (Atm_addr *)dst;
		ip = NULL;
		if (atm->address_format == T_ATM_ABSENT) {
			err = EINVAL;
			goto done;
		}
	} else {
		err = EINVAL;
		goto done;
	}

	/*
	 * Make sure we have services provider and ARP support
	 */
	if ((inp->inf_serv == NULL) ||
	    (inp->inf_serv->is_arp_svcout == NULL)) {
		err = ENETDOWN;
		goto done;
	}

	/*
	 * Allocate IP VCC
	 */
	ivp = uma_zalloc(ipatm_vc_zone, 0);
	if (ivp == NULL) {
		err = ENOMEM;
		goto done;
	}

	/*
	 * Initialize SVC
	 */
	ivp->iv_flags = IVF_SVC;
	ivp->iv_ipnif = inp;

	/*
	 * Get destination ATM address
	 */
	if (daf == AF_INET) {
		/*
		 * ARP is the way...
		 */
		ivp->iv_dst.s_addr = ip->s_addr;

		switch ((*inp->inf_serv->is_arp_svcout)(ivp, ip)) {

		case MAP_PROCEEDING:
			/*
			 * Wait for answer
			 */
			ivp->iv_state = IPVCC_PMAP;
			IPVCC_TIMER(ivp, IPATM_ARP_TIME);
			break;

		case MAP_VALID:
			/*
			 * We've got our answer already, so open SVC
			 */
			ivp->iv_flags |= IVF_MAPOK;
			err = ipatm_opensvc(ivp);
			if (err) {
				(*inp->inf_serv->is_arp_close)(ivp);
				uma_zfree(ipatm_vc_zone, ivp);
				goto done;
			}
			break;

		case MAP_FAILED:
			/*
			 * So sorry...come again
			 */
			uma_zfree(ipatm_vc_zone, ivp);
			err = ENETDOWN;
			goto done;

		default:
			panic("ipatm_createsvc: invalid arp_svcout return");
		}
	} else {
		/*
		 * We were given the ATM address, so open the SVC
		 *
		 * Create temporary arp map entry so that opensvc() works.
		 * Caller must set up a permanent entry immediately! (yuk)
		 */
		struct arpmap	map;

		ATM_ADDR_COPY(atm, &map.am_dstatm);
		map.am_dstatmsub.address_format = T_ATM_ABSENT;
		map.am_dstatmsub.address_length = 0;
		ivp->iv_arpent = &map;
		err = ipatm_opensvc(ivp);
		if (err) {
			uma_zfree(ipatm_vc_zone, ivp);
			goto done;
		}
		ivp->iv_arpent = NULL;
	}

	/*
	 * Queue VCC onto its network interface
	 */
	s = splnet();
	ipatm_vccnt++;
	ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
	(void) splx(s);

done:
	if (err)
		*sivp = NULL;
	else
		*sivp = ivp;
	return (err);
}


/*
 * Open an IP SVC
 * 
 * This function will continue the IP SVC creation process.  Here, we
 * will issue an SVC open to the signalling manager and then wait for
 * the final SVC setup results.
 *
 * Arguments:
 *	ivp	pointer to IP SVC to open
 *
 * Returns:
 *	0 	SVC open was successfully initiated
 *	errno	open failed - reason indicated
 *
 */
int
ipatm_opensvc(ivp)
	struct ipvcc	*ivp;
{
	struct ip_nif	*inp = ivp->iv_ipnif;
	Atm_attributes	*ap;
	int	err = 0, i;

	/*
	 * Cancel possible arp timeout
	 */
	IPVCC_CANCEL(ivp);

	/*
	 * Fill out connection attributes
	 */
	i = ivp->iv_parmx;
	if (inp->inf_serv->is_vccparm[i].ivc_aal == ATM_AAL5) {
		if (inp->inf_serv->is_vccparm[i].ivc_encaps == ATM_ENC_LLC) {
			ap = &ipatm_aal5llc;
			ivp->iv_flags |= IVF_LLC;
		} else {
			ap = &ipatm_aal5null;
			ivp->iv_flags &= ~IVF_LLC;
		}
	} else {
		ap = &ipatm_aal4null;
		ivp->iv_flags &= ~IVF_LLC;
	}

	ap->nif = inp->inf_nif;
	ap->traffic.v.forward.PCR_all_traffic = inp->inf_nif->nif_pif->pif_pcr;
	ap->traffic.v.backward.PCR_all_traffic = inp->inf_nif->nif_pif->pif_pcr;

	ATM_ADDR_COPY(&ivp->iv_arpent->am_dstatm, &ap->called.addr);
	ATM_ADDR_COPY(&ivp->iv_arpent->am_dstatmsub, &ap->called.subaddr);

	/*
	 * Initiate SVC open
	 */
	err = atm_cm_connect(&ipatm_endpt, ivp, ap, &ivp->iv_conn);
	switch (err) {

	case EINPROGRESS:
		/*
		 * Call is progressing
		 */
		/* ivp->iv_ = ap->headout; */

		/*
		 * Now we just wait for a CALL_CONNECTED event
		 */
		ivp->iv_state = IPVCC_POPEN;
		IPVCC_TIMER(ivp, IPATM_SVC_TIME);
		err = 0;
		break;

	case 0:
		/*
		 * We've been hooked up with a shared VCC
		 */
		/* ivp->iv_ = ap->headout; */
		ipatm_activate(ivp);
		break;
	}

	return (err);
}


/*
 * Retry an IP SVC Open
 * 
 * This function will attempt to retry a failed SVC open request.  The IP
 * interface service provider specifies a list of possible VCC parameters
 * for IP to use.  We will try each set of parameters in turn until either
 * an open succeeds or we reach the end of the list.
 * 
 * Arguments:
 *	ivp	pointer to IP SVC
 *
 * Returns:
 *	0 	SVC (re)open was successfully initiated
 *	else	retry failed
 *
 */
int
ipatm_retrysvc(ivp)
	struct ipvcc	*ivp;
{
	struct ip_nif	*inp = ivp->iv_ipnif;

	/*
	 * If there isn't another set of vcc parameters to try, return
	 */
	if ((++ivp->iv_parmx >= IPATM_VCCPARMS) ||
	    (inp->inf_serv->is_vccparm[ivp->iv_parmx].ivc_aal == 0))
		return (1);

	/*
	 * Okay, now initiate open with a new set of parameters
	 */
	return (ipatm_opensvc(ivp));
}


/*
 * Finish IP SVC Activation
 * 
 * Arguments:
 *	ivp	pointer to IP SVC
 *
 * Returns:
 *	none
 *
 */
void
ipatm_activate(ivp)
	struct ipvcc	*ivp;
{

	/*
	 * Connection is now active
	 */
	ivp->iv_state = IPVCC_ACTIVE;
	IPVCC_CANCEL(ivp);

	/*
	 * Tell ARP module that connection is active
	 */
	if ((*ivp->iv_ipnif->inf_serv->is_arp_svcact)(ivp)) {
		(void) ipatm_closevc(ivp, T_ATM_CAUSE_TEMPORARY_FAILURE);
		return;
	}

	/*
	 * Send any queued packet
	 */
	if ((ivp->iv_flags & IVF_MAPOK) && ivp->iv_queue) {
		struct sockaddr_in	sin;
		struct ifnet		*ifp;

		sin.sin_family = AF_INET;
		sin.sin_addr.s_addr = ivp->iv_dst.s_addr;
		ifp = (struct ifnet *)ivp->iv_ipnif->inf_nif;
		(void) ipatm_ifoutput(ifp, ivp->iv_queue, 
			(struct sockaddr *)&sin);
		ivp->iv_queue = NULL;
	}
}


/*
 * Process Incoming Calls
 * 
 * This function will receive control when an incoming call has been matched
 * to one of our registered listen parameter blocks.  Assuming the call passes
 * acceptance criteria and all required resources are available, we will
 * create an IP SVC and notify the connection manager of our decision.  We
 * will then await notification of the final SVC setup results.  If any
 * problems are encountered, we will just tell the connection manager to
 * reject the call.
 *
 * Called at splnet.
 *
 * Arguments:
 *	tok	owner's matched listening token
 *	cop	pointer to incoming call's connection block
 *	ap	pointer to incoming call's attributes
 *	tokp	pointer to location to store our connection token
 *
 * Returns:
 *	0	call is accepted
 *	errno	call rejected - reason indicated
 *
 */
int
ipatm_incoming(tok, cop, ap, tokp)
	void		*tok;
	Atm_connection	*cop;
	Atm_attributes	*ap;
	void		**tokp;
{
	struct atm_nif	*nip = ap->nif;
	struct ip_nif	*inp;
	struct ipvcc	*ivp = NULL;
	int	err, cause;
	int	usellc = 0, mtu = ATM_NIF_MTU;

	/*
	 * Get IP interface and make sure its ready
	 */
	for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
		if (inp->inf_nif == nip)
			break;
	}
	if ((inp == NULL) || (inp->inf_state != IPNIF_ACTIVE)) {
		err = ENETUNREACH;
		cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
		goto reject;
	}

	/*
	 * Make sure we have services provider and ARP support
	 */
	if ((inp->inf_serv == NULL) ||
	    (inp->inf_serv->is_arp_svcin == NULL)) {
		err = ENETUNREACH;
		cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
		goto reject;
	}

	/*
	 * Check for LLC encapsulation
	 */
	if ((ap->blli.tag_l2 == T_ATM_PRESENT) &&
	    (ap->blli.v.layer_2_protocol.ID_type == T_ATM_SIMPLE_ID) &&
	    (ap->blli.v.layer_2_protocol.ID.simple_ID == T_ATM_BLLI2_I8802)) {
		usellc = 1;
		mtu += IPATM_LLC_LEN;
	}

	/*
	 * Verify requested MTU
	 */
	if (ap->aal.type == ATM_AAL5) {
		if ((ap->aal.v.aal5.forward_max_SDU_size > mtu) ||
		    (ap->aal.v.aal5.backward_max_SDU_size < mtu)) {
			err = ENETUNREACH;
			cause = T_ATM_CAUSE_AAL_PARAMETERS_NOT_SUPPORTED;
			goto reject;
		}
	} else {
		if ((ap->aal.v.aal4.forward_max_SDU_size > mtu) ||
		    (ap->aal.v.aal4.backward_max_SDU_size < mtu)) {
			err = ENETUNREACH;
			cause = T_ATM_CAUSE_AAL_PARAMETERS_NOT_SUPPORTED;
			goto reject;
		}
	}

	/*
	 * Allocate IP VCC
	 */
	ivp = uma_zalloc(ipatm_vc_zone, 0);
	if (ivp == NULL) {
		err = ENOMEM;
		cause = T_ATM_CAUSE_UNSPECIFIED_RESOURCE_UNAVAILABLE;
		goto reject;
	}

	/*
	 * Initialize SVC
	 */
	ivp->iv_flags = IVF_SVC;
	ivp->iv_ipnif = inp;
	if (usellc)
		ivp->iv_flags |= IVF_LLC;

	/*
	 * Lookup ARP entry for destination
	 */
	switch ((*inp->inf_serv->is_arp_svcin)
			(ivp, &ap->calling.addr, &ap->calling.subaddr)) {

	case MAP_PROCEEDING:
		/*
		 * We'll be (hopefully) notified later
		 */
		break;

	case MAP_VALID:
		/*
		 * We've got our answer already
		 */
		ivp->iv_flags |= IVF_MAPOK;
		ivp->iv_dst.s_addr = ivp->iv_arpent->am_dstip.s_addr;
		break;

	case MAP_FAILED:
		/*
		 * So sorry...come again
		 */
		err = ENETUNREACH;
		cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
		goto reject;

	default:
		panic("ipatm_incoming: invalid arp_svcin return");
	}

	/*
	 * Accept SVC connection
	 */
	ivp->iv_state = IPVCC_PACCEPT;

	/*
	 * Save VCC information
	 */
	ivp->iv_conn = cop;
	*tokp = ivp;
	/* ivp->iv_ = ap->headout; */

	/*
	 * Queue VCC onto its network interface
	 */
	ipatm_vccnt++;
	ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);

	/*
	 * Wait for a CALL_CONNECTED event
	 */
	IPVCC_TIMER(ivp, IPATM_SVC_TIME);

	return (0);

reject:
	/*
	 * Clean up after call failure
	 */
	if (ivp) {
		(*inp->inf_serv->is_arp_close)(ivp);
		uma_zfree(ipatm_vc_zone, ivp);
	}
	ap->cause.tag = T_ATM_PRESENT;
	ap->cause.v = ipatm_cause;
	ap->cause.v.cause_value = cause;
	return (err);
}


/*
 * Close an IP VCC
 * 
 * This function will close an IP VCC (PVC or SVC), including notifying 
 * the signalling and ARP subsystems of the VCC's demise and cleaning 
 * up memory after ourselves.
 *
 * Arguments:
 *	ivp	pointer to VCC
 *	code	cause code
 *
 * Returns:
 *	0 	VCC successfully closed
 *	errno	close failed - reason indicated
 *
 */
int
ipatm_closevc(ivp, code)
	struct ipvcc	*ivp;
	int		code;
{
	struct ip_nif	*inp = ivp->iv_ipnif;
	int	s, err;

	/*
	 * Make sure VCC hasn't been through here already
	 */
	switch (ivp->iv_state) {

	case IPVCC_FREE:
		return (EALREADY);
	}

	/*
	 * Reset lookup cache
	 */
	if (last_map_ipvcc == ivp) {
		last_map_ipvcc = NULL;
		last_map_ipdst = 0;
	}

	/*
	 * Tell ARP about SVCs and dynamic PVCs
	 */
	if (inp->inf_serv && 
	    ((ivp->iv_flags & IVF_SVC) || inp->inf_serv->is_arp_pvcopen)) {
		(*inp->inf_serv->is_arp_close)(ivp);
	}

	/*
	 * Free queued packets
	 */
	if (ivp->iv_queue)
		KB_FREEALL(ivp->iv_queue);

	/*
	 * Cancel any timers
	 */
	IPVCC_CANCEL(ivp);

	/*
	 * Close VCC
	 */
	switch (ivp->iv_state) {

	case IPVCC_PMAP:
		break;

	case IPVCC_POPEN:
	case IPVCC_PACCEPT:
	case IPVCC_ACTPENT:
	case IPVCC_ACTIVE:
		ipatm_cause.cause_value = code;
		err = atm_cm_release(ivp->iv_conn, &ipatm_cause);
		if (err) {
			log(LOG_ERR, 
				"ipatm_closevc: release fail: err=%d\n", err);
		}
		break;

	case IPVCC_CLOSED:
		break;

	default:
		log(LOG_ERR, 
			"ipatm_closevc: unknown state: ivp=%p, state=%d\n",
                       	ivp, ivp->iv_state);
	}

	/*
	 * Remove VCC from network i/f
	 */
	s = splnet();
	DEQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);

	/*
	 * Reset state just to be sure
	 */
	ivp->iv_state = IPVCC_FREE;

	/*
	 * If ARP module is done with VCC too, then free it
	 */
	if (ivp->iv_arpconn == NULL)
		uma_zfree(ipatm_vc_zone, ivp);
	ipatm_vccnt--;
	(void) splx(s);

	return (0);
}


/*
 * Check if IP address is valid on a Network Interface
 * 
 * Checks whether the supplied IP address is allowed to be assigned to
 * the supplied IP network interface.
 *
 * Arguments:
 *	in	IP address
 *	inp	pointer to IP network interface
 *
 * Returns:
 *	0 - OK to assign
 *	1 - not valid to assign
 *
 */
int
ipatm_chknif(in, inp)
	struct in_addr	in;
	struct ip_nif	*inp;
{
	struct in_ifaddr	*ia;
	u_long	i;

	/*
	 * Make sure there's an interface requested
	 */
	if (inp == NULL)
		return (1);

	/*
	 * Make sure we have an IP address
	 */
	i = ntohl(in.s_addr);
	if (i == 0)
		return (1);

	/*
	 * Make sure an interface address is set 
	 */
	ia = inp->inf_addr;
	if (ia == NULL)
		return (1);

	/*
	 * Make sure we're on the right subnet
	 */
	if ((i & ia->ia_subnetmask) != ia->ia_subnet)
		return (1);

	return (0);
}


/*
 * Map an IP Address to an IP VCC
 * 
 * Given a destination IP address, this function will return a pointer
 * to the appropriate output IP VCC to which to send the packet.
 * This is currently implemented using a one-behind cache containing the 
 * last successful mapping result.  If the cache lookup fails, then a
 * simple linear search of all IP VCCs on the destination network interface 
 * is performed.  This is obviously an area to look at for performance 
 * improvements.
 *
 * Arguments:
 *	dst	pointer to destination IP address
 *	nip	pointer to destination network interface
 *
 * Returns:
 *	addr 	pointer to located IP VCC
 *	0	no such mapping exists
 *
 */
struct ipvcc *
ipatm_iptovc(dst, nip)
	struct sockaddr_in	*dst;
	struct atm_nif		*nip;
{
	struct ip_nif	*inp;
	struct ipvcc	*ivp;
	u_long		dstip = dst->sin_addr.s_addr;
	int	s;

	/*
	 * Look in cache first
	 */
	if (last_map_ipdst == dstip)
		return (last_map_ipvcc);

	/*
	 * Oh well, we've got to search for it...first find the interface
	 */
	s = splnet();
	for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
		if (inp->inf_nif == nip)
			break;
	}
	if (inp == NULL) {
		(void) splx(s);
		return (NULL);
	}

	/*
	 * Now home in on the VCC
	 */
	for (ivp = Q_HEAD(inp->inf_vcq, struct ipvcc); ivp;
				ivp = Q_NEXT(ivp, struct ipvcc, iv_elem)) {
		if (ivp->iv_dst.s_addr == dstip)
			break;
	}

	/*
	 * Update lookup cache
	 */
	if (ivp) {
		last_map_ipdst = dstip;
		last_map_ipvcc = ivp;
	}
	(void) splx(s);

	return (ivp);
}

OpenPOWER on IntegriCloud