summaryrefslogtreecommitdiffstats
path: root/sys/netatm/uni/uniarp.c
blob: a237278bcd6e03041e3af81b0d3ad805bec1d30c (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
/*
 *
 * ===================================
 * 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$
 *
 */

/*
 * ATM Forum UNI Support
 * ---------------------
 *
 * UNI ATMARP support (RFC1577)
 *
 */

#include <netatm/kern_include.h>

#include <netatm/ipatm/ipatm_var.h>
#include <netatm/ipatm/ipatm_serv.h>
#include <netatm/uni/unisig_var.h>
#include <netatm/uni/uniip_var.h>

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


/*
 * Global variables
 */
struct uniarp		*uniarp_arptab[UNIARP_HASHSIZ] = {NULL};
struct uniarp		*uniarp_nomaptab = NULL;
struct uniarp		*uniarp_pvctab = NULL;
struct atm_time		uniarp_timer = {0, 0};		/* Aging timer */
struct uniarp_stat	uniarp_stat = {0};
int			uniarp_print = 0;

Atm_endpoint	uniarp_endpt = {
	NULL,
	ENDPT_ATMARP,
	uniarp_ioctl,
	uniarp_getname,
	uniarp_connected,
	uniarp_cleared,
	NULL,
	NULL,
	NULL,
	NULL,
	uniarp_cpcs_data,
	NULL,
	NULL,
	NULL,
	NULL
};

struct sp_info	uniarp_pool = {
	"uni arp pool",			/* si_name */
	sizeof(struct uniarp),		/* si_blksiz */
	10,				/* si_blkcnt */
	200				/* si_maxallow */
};


/*
 * Local variables
 */
static void	uniarp_server_mode __P((struct uniip *));
static void	uniarp_client_mode __P((struct uniip *, Atm_addr *));


/*
 * Process module loading notification
 * 
 * Called whenever the uni module is initializing.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	0	initialization successful
 *	errno	initialization failed - reason indicated
 *
 */
int
uniarp_start()
{
	int	err;

	/*
	 * Register our endpoint
	 */
	err = atm_endpoint_register(&uniarp_endpt);

	return (err);
}


/*
 * Process module unloading notification
 * 
 * Called whenever the uni module is about to be unloaded.  All signalling
 * instances will have been previously detached.  All uniarp resources 
 * must be freed now.
 *
 * Arguments:
 *	none
 *
 * Returns:
 *	none
 *
 */
void
uniarp_stop()
{
	int	i;

	/* 
	 * Make sure the arp table is empty
	 */
	for (i = 0; i < UNIARP_HASHSIZ; i++) {
		if (uniarp_arptab[i] != NULL)
			panic("uniarp_stop: arp table not empty");
	}

	/*
	 * Cancel timers
	 */
	(void) atm_untimeout(&uniarp_timer);

	/*
	 * De-register ourselves
	 */
	(void) atm_endpoint_deregister(&uniarp_endpt);

	/*
	 * Free our storage pools
	 */
	atm_release_pool(&uniarp_pool);
}


/*
 * Process IP Network Interface Activation
 * 
 * Called whenever an IP network interface becomes active.
 *
 * Called at splnet.
 *
 * Arguments:
 *      uip     pointer to UNI IP interface
 *
 * Returns:
 *      none
 *
 */
void
uniarp_ipact(uip)
	struct uniip		*uip;
{
	struct unisig		*usp;

	ATM_DEBUG1("uniarp_ipact: uip=%p\n", uip);

	/*
	 * Set initial state
	 */
	uip->uip_arpstate = UIAS_NOTCONF;
	uip->uip_arpsvratm.address_format = T_ATM_ABSENT;
	uip->uip_arpsvratm.address_length = 0;
	uip->uip_arpsvrsub.address_format = T_ATM_ABSENT;
	uip->uip_arpsvrsub.address_length = 0;

	usp = (struct unisig *)uip->uip_ipnif->inf_nif->nif_pif->pif_siginst;
	if (usp->us_addr.address_format != T_ATM_ABSENT)
		uip->uip_flags |= UIF_IFADDR;

	/*
	 * Make sure aging timer is running
	 */
	if ((uniarp_timer.ti_flag & TIF_QUEUED) == 0)
		atm_timeout(&uniarp_timer, UNIARP_AGING, uniarp_aging);

	return;
}


/*
 * Process IP Network Interface Deactivation
 * 
 * Called whenever an IP network interface becomes inactive.  All VCCs
 * for this interface should already have been closed.
 *
 * Called at splnet.
 *
 * Arguments:
 *      uip     pointer to UNI IP interface
 *
 * Returns:
 *      none
 *
 */
void
uniarp_ipdact(uip)
	struct uniip		*uip;
{
	struct uniarp		*uap, *unext;
	int	i;

	ATM_DEBUG1("uniarp_ipdact: uip=%p\n", uip);

	/* 
	 * Delete all interface entries
	 */
	for (i = 0; i < UNIARP_HASHSIZ; i++) {
		for (uap = uniarp_arptab[i]; uap; uap = unext) {
			unext = uap->ua_next;

			if (uap->ua_intf != uip)
				continue;

			/*
			 * All VCCs should (better) be gone by now
			 */
			if (uap->ua_ivp)
				panic("uniarp_ipdact: entry not empty");

			/*
			 * Clean up any loose ends
			 */
			UNIARP_CANCEL(uap);

			/*
			 * Delete entry from arp table and free entry
			 */
			UNIARP_DELETE(uap);
			atm_free((caddr_t)uap);
		}
	}

	/*
	 * Clean up 'nomap' table
	 */
	for (uap = uniarp_nomaptab; uap; uap = unext) {
		unext = uap->ua_next;

		if (uap->ua_intf != uip)
			continue;

		/*
		 * All VCCs should (better) be gone by now
		 */
		if (uap->ua_ivp)
			panic("uniarp_ipdact: entry not empty");

		/*
		 * Clean up any loose ends
		 */
		UNIARP_CANCEL(uap);

		/*
		 * Delete entry from 'no map' table and free entry
		 */
		UNLINK(uap, struct uniarp, uniarp_nomaptab, ua_next);
		atm_free((caddr_t)uap);
	}

	/*
	 * Also clean up pvc table
	 */
	for (uap = uniarp_pvctab; uap; uap = unext) {
		unext = uap->ua_next;

		if (uap->ua_intf != uip)
			continue;

		/*
		 * All PVCs should (better) be gone by now
		 */
		panic("uniarp_ipdact: pvc table not empty");
	}

	/*
	 * Cancel arp interface timer
	 */
	UNIIP_ARP_CANCEL(uip);

	/*
	 * Stop aging timer if this is the last active interface
	 */
	if (uniip_head == uip && uip->uip_next == NULL)
		(void) atm_untimeout(&uniarp_timer);
}


/*
 * Process Interface ATM Address Change
 * 
 * This function is called whenever the ATM address for a physical
 * interface is set/changed.
 *
 * Called at splnet.
 *
 * Arguments:
 *      sip     pointer to interface's UNI signalling instance
 *
 * Returns:
 *	none
 *
 */
void
uniarp_ifaddr(sip)
	struct siginst		*sip;
{
	struct atm_nif		*nip;
	struct uniip		*uip;

	ATM_DEBUG1("uniarp_ifaddr: sip=%p\n", sip);

	/*
	 * We've got to handle this for every network interface
	 */
	for (nip = sip->si_pif->pif_nif; nip; nip = nip->nif_pnext) {

		/*
		 * Find our control blocks
		 */
		for (uip = uniip_head; uip; uip = uip->uip_next) {
			if (uip->uip_ipnif->inf_nif == nip)
				break;
		}
		if (uip == NULL)
			continue;

		/*
		 * We don't support changing prefix (yet)
		 */
		if (uip->uip_flags & UIF_IFADDR) {
			log(LOG_ERR, "uniarp_ifaddr: change not supported\n");
			continue;
		}

		/*
		 * Note that address has been set and figure out what
		 * to do next
		 */
		uip->uip_flags |= UIF_IFADDR;

		if (uip->uip_arpstate == UIAS_CLIENT_PADDR) {
			/*
			 * This is what we're waiting for
			 */
			uniarp_client_mode(uip, NULL);
		} else if (uip->uip_arpstate == UIAS_SERVER_ACTIVE) {
			/*
			 * Set new local arpserver atm address
			 */
			ATM_ADDR_SEL_COPY(&sip->si_addr, nip->nif_sel,
						&uip->uip_arpsvratm);
		}
	}

	return;
}


/*
 * Set ATMARP Server Mode
 * 
 * This function is called to configure the local node to become the 
 * ATMARP server for the specified LIS.
 *
 * Called at splnet.
 *
 * Arguments:
 *      uip     pointer to UNI IP interface
 *
 * Returns:
 *	none
 *
 */
static void
uniarp_server_mode(uip)
	struct uniip		*uip;
{
	struct ip_nif	*inp;
	struct atm_nif	*nip;
	struct siginst	*sgp;
	struct ipvcc	*ivp, *inext;
	struct uniarp	*uap, *unext;
	int		i;

	ATM_DEBUG1("uniarp_server_mode: uip=%p\n", uip);

	/*
	 * Handle client/server mode changes first
	 */
	switch (uip->uip_arpstate) {

	case UIAS_NOTCONF:
	case UIAS_SERVER_ACTIVE:
	case UIAS_CLIENT_PADDR:
		/*
		 * Nothing to undo
		 */
		break;

	case UIAS_CLIENT_POPEN:
		/*
		 * We're becoming the server, so kill the pending connection
		 */
		UNIIP_ARP_CANCEL(uip);
		if ((ivp = uip->uip_arpsvrvcc) != NULL) {
			ivp->iv_flags &= ~IVF_NOIDLE;
			uip->uip_arpsvrvcc = NULL;
			(*ivp->iv_ipnif->inf_arpnotify)(ivp, MAP_FAILED);
		}
		break;

	case UIAS_CLIENT_REGISTER:
	case UIAS_CLIENT_ACTIVE:
		/*
		 * We're becoming the server, but leave existing VCC as a
		 * "normal" IP VCC
		 */
		UNIIP_ARP_CANCEL(uip);
		ivp = uip->uip_arpsvrvcc;
		ivp->iv_flags &= ~IVF_NOIDLE;
		uip->uip_arpsvrvcc = NULL;
		break;
	}

	/*
	 * Revalidate status for all arp entries on this interface
	 */
	for (i = 0; i < UNIARP_HASHSIZ; i++) {
		for (uap = uniarp_arptab[i]; uap; uap = unext) {
			unext = uap->ua_next;

			if (uap->ua_intf != uip)
				continue;

			if (uap->ua_origin >= UAO_PERM)
				continue;

			if (uap->ua_origin >= UAO_SCSP) {
				if (uniarp_validate_ip(uip, &uap->ua_dstip,
							uap->ua_origin) == 0)
					continue;
			}

			if (uap->ua_ivp == NULL) {
				UNIARP_CANCEL(uap);
				UNIARP_DELETE(uap);
				atm_free((caddr_t)uap);
				continue;
			}

			if (uap->ua_flags & UAF_VALID) {
				uap->ua_flags |= UAF_LOCKED;
				for (ivp = uap->ua_ivp; ivp; ivp = inext) {
					inext = ivp->iv_arpnext;
					(*ivp->iv_ipnif->inf_arpnotify)
							(ivp, MAP_INVALID);
				}
				uap->ua_flags &= ~(UAF_LOCKED | UAF_VALID);
			}
			uap->ua_aging = 1;
			uap->ua_origin = 0;
		}
	}

	/*
	 * OK, now let's make ourselves the server
	 */
	inp = uip->uip_ipnif;
	nip = inp->inf_nif;
	sgp = nip->nif_pif->pif_siginst;
	ATM_ADDR_SEL_COPY(&sgp->si_addr, nip->nif_sel, &uip->uip_arpsvratm);
	uip->uip_arpsvrip = IA_SIN(inp->inf_addr)->sin_addr;
	uip->uip_arpstate = UIAS_SERVER_ACTIVE;
	return;
}


/*
 * Set ATMARP Client Mode
 * 
 * This function is called to configure the local node to be an ATMARP 
 * client on the specified LIS using the specified ATMARP server.
 *
 * Called at splnet.
 *
 * Arguments:
 *      uip     pointer to UNI IP interface
 *      aap     pointer to the ATMARP server's ATM address
 *
 * Returns:
 *	none
 *
 */
static void
uniarp_client_mode(uip, aap)
	struct uniip		*uip;
	Atm_addr		*aap;
{
	struct ip_nif		*inp = uip->uip_ipnif;
	struct uniarp		*uap, *unext;
	struct ipvcc		*ivp, *inext;
	int			i;

	ATM_DEBUG2("uniarp_client_mode: uip=%p, atm=(%s,-)\n",
		uip, aap ? unisig_addr_print(aap): "-");

	/*
	 * Handle client/server mode changes first
	 */
	switch (uip->uip_arpstate) {

	case UIAS_NOTCONF:
	case UIAS_CLIENT_PADDR:
		/*
		 * Nothing to undo
		 */
		break;

	case UIAS_CLIENT_POPEN:
		/*
		 * If this is this a timeout retry, just go do it
		 */
		if (aap == NULL)
			break;

		/*
		 * If this isn't really a different arpserver, we're done
		 */
		if (ATM_ADDR_EQUAL(aap, &uip->uip_arpsvratm))
			return;

		/*
		 * We're changing servers, so kill the pending connection
		 */
		UNIIP_ARP_CANCEL(uip);
		if ((ivp = uip->uip_arpsvrvcc) != NULL) {
			ivp->iv_flags &= ~IVF_NOIDLE;
			uip->uip_arpsvrvcc = NULL;
			(*ivp->iv_ipnif->inf_arpnotify)(ivp, MAP_FAILED);
		}
		break;

	case UIAS_CLIENT_REGISTER:
	case UIAS_CLIENT_ACTIVE:
		/*
		 * If this isn't really a different arpserver, we're done
		 */
		if (ATM_ADDR_EQUAL(aap, &uip->uip_arpsvratm))
			return;

		/*
		 * We're changing servers, but leave existing VCC as a
		 * "normal" IP VCC
		 */
		UNIIP_ARP_CANCEL(uip);
		ivp = uip->uip_arpsvrvcc;
		ivp->iv_flags &= ~IVF_NOIDLE;
		uip->uip_arpsvrvcc = NULL;
		break;

	case UIAS_SERVER_ACTIVE:
		/*
		 * We're changing from server mode, so...
		 *
		 * Reset valid/authoritative status for all arp entries
		 * on this interface
		 */
		for (i = 0; i < UNIARP_HASHSIZ; i++) {
			for (uap = uniarp_arptab[i]; uap; uap = unext) {
				unext = uap->ua_next;

				if (uap->ua_intf != uip)
					continue;

				if (uap->ua_origin >= UAO_PERM)
					continue;

				if (uap->ua_ivp == NULL) {
					UNIARP_CANCEL(uap);
					UNIARP_DELETE(uap);
					atm_free((caddr_t)uap);
					continue;
				}

				if (uap->ua_flags & UAF_VALID) {
					uap->ua_flags |= UAF_LOCKED;
					for (ivp = uap->ua_ivp; ivp;
								ivp = inext) {
						inext = ivp->iv_arpnext;
						(*ivp->iv_ipnif->inf_arpnotify)
							(ivp, MAP_INVALID);
					}
					uap->ua_flags &=
						~(UAF_LOCKED | UAF_VALID);
				}
				uap->ua_aging = 1;
				uap->ua_origin = 0;
			}
		}
		uip->uip_arpsvratm.address_format = T_ATM_ABSENT;
		uip->uip_arpsvratm.address_length = 0;
		uip->uip_arpsvrsub.address_format = T_ATM_ABSENT;
		uip->uip_arpsvrsub.address_length = 0;
		uip->uip_arpsvrip.s_addr = 0;
		break;
	}

	/*
	 * Save the arp server address, if supplied now
	 */
	if (aap)
		ATM_ADDR_COPY(aap, &uip->uip_arpsvratm);

	/*
	 * If the interface's ATM address isn't set yet, then we
	 * can't do much until it is
	 */
	if ((uip->uip_flags & UIF_IFADDR) == 0) {
		uip->uip_arpstate = UIAS_CLIENT_PADDR;
		return;
	}

	/*
	 * Just to keep things simple, if we already have (or are trying to
	 * setup) any SVCs to our new server, kill the connections so we can
	 * open a "fresh" SVC for the arpserver connection.
	 */
	for (i = 0; i < UNIARP_HASHSIZ; i++) {
		for (uap = uniarp_arptab[i]; uap; uap = unext) {
			unext = uap->ua_next;

			if (ATM_ADDR_EQUAL(&uip->uip_arpsvratm,
							&uap->ua_dstatm) &&
			    ATM_ADDR_EQUAL(&uip->uip_arpsvrsub,
							&uap->ua_dstatmsub)) {
				uap->ua_flags &= ~UAF_VALID;
				for (ivp = uap->ua_ivp; ivp; ivp = inext) {
					inext = ivp->iv_arpnext;
					(*inp->inf_arpnotify)(ivp, MAP_FAILED);
				}
			}
		}
	}
	for (uap = uniarp_nomaptab; uap; uap = unext) {
		unext = uap->ua_next;

		if (ATM_ADDR_EQUAL(&uip->uip_arpsvratm, &uap->ua_dstatm) &&
		    ATM_ADDR_EQUAL(&uip->uip_arpsvrsub, &uap->ua_dstatmsub)) {
			uap->ua_flags &= ~UAF_VALID;
			for (ivp = uap->ua_ivp; ivp; ivp = inext) {
				inext = ivp->iv_arpnext;
				(*inp->inf_arpnotify)(ivp, MAP_FAILED);
			}
		}
	}

	/*
	 * Now, get an arp entry for the server connection
	 */
	uip->uip_arpstate = UIAS_CLIENT_POPEN;
	uap = (struct uniarp *)atm_allocate(&uniarp_pool);
	if (uap == NULL) {
		UNIIP_ARP_TIMER(uip, 1 * ATM_HZ);
		return;
	}

	/*
	 * Next, initiate an SVC to the server
	 */
	if ((*inp->inf_createsvc)(&inp->inf_nif->nif_if, AF_ATM,
			(caddr_t)&uip->uip_arpsvratm, &ivp)) {
		atm_free((caddr_t)uap);
		UNIIP_ARP_TIMER(uip, 1 * ATM_HZ);
		return;
	}

	/*
	 * Finally, get everything set up and wait for the SVC
	 * connection to complete
	 */
	uip->uip_arpsvrvcc = ivp;
	ivp->iv_flags |= IVF_NOIDLE;

	ATM_ADDR_COPY(&uip->uip_arpsvratm, &uap->ua_dstatm);
	ATM_ADDR_COPY(&uip->uip_arpsvrsub, &uap->ua_dstatmsub);
	uap->ua_intf = uip;

	LINK2TAIL(ivp, struct ipvcc, uap->ua_ivp, iv_arpnext);
	ivp->iv_arpent = (struct arpmap *)uap;

	LINK2TAIL(uap, struct uniarp, uniarp_nomaptab, ua_next);

	return;
}


/*
 * Process a UNI ARP interface timeout
 * 
 * Called when a previously scheduled uniip arp interface timer expires.
 * Processing will be based on the current uniip arp state.
 *
 * Called at splnet.
 *
 * Arguments:
 *	tip	pointer to uniip arp timer control block
 *
 * Returns:
 *	none
 *
 */
void
uniarp_iftimeout(tip)
	struct atm_time	*tip;
{
	struct ip_nif	*inp;
	struct uniip	*uip;


	/*
	 * Back-off to uniip control block
	 */
	uip = (struct uniip *)
		((caddr_t)tip - (int)(&((struct uniip *)0)->uip_arptime));

	ATM_DEBUG2("uniarp_iftimeout: uip=%p, state=%d\n", uip, 
		uip->uip_arpstate);

	/*
	 * Process timeout based on protocol state
	 */
	switch (uip->uip_arpstate) {

	case UIAS_CLIENT_POPEN:
		/*
		 * Retry opening arp server connection
		 */
		uniarp_client_mode(uip, NULL);
		break;

	case UIAS_CLIENT_REGISTER:
		/*
		 * Resend registration request
		 */
		inp = uip->uip_ipnif;
		(void) uniarp_arp_req(uip, &(IA_SIN(inp->inf_addr)->sin_addr));

		/*
		 * Restart timer
		 */
		UNIIP_ARP_TIMER(uip, 2 * ATM_HZ);

		break;

	case UIAS_CLIENT_ACTIVE:
		/*
		 * Refresh our registration
		 */
		inp = uip->uip_ipnif;
		(void) uniarp_arp_req(uip, &(IA_SIN(inp->inf_addr)->sin_addr));

		/*
		 * Restart timer
		 */
		UNIIP_ARP_TIMER(uip, UNIARP_REGIS_RETRY);

		break;

	default:
		log(LOG_ERR, "uniarp_iftimeout: invalid state %d\n",
			uip->uip_arpstate);
	}
}


/*
 * UNI ARP 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
 *
 */
int
uniarp_ioctl(code, data, arg1)
        int		code;
        caddr_t		data;
        caddr_t		arg1;
{
	struct atmaddreq	*aap;
	struct atmdelreq	*adp;
	struct atmsetreq	*asp;
	struct atminfreq	*aip;
	struct air_arp_rsp	aar;
	struct air_asrv_rsp	asr;
	struct atm_pif		*pip;
	struct atm_nif		*nip;
	struct ipvcc		*ivp, *inext;
	struct uniip		*uip;
	struct uniarp		*uap;
	struct unisig		*usp;
	struct in_addr		ip;
	Atm_addr		atmsub;
	u_long			dst;
	int			err = 0, i, buf_len;
	caddr_t			buf_addr;

	switch (code) {

	case AIOCS_ADD_ARP:
		/*
		 * Add a permanent ARP mapping
		 */
		aap = (struct atmaddreq *)data;
		uip = (struct uniip *)arg1;
		if (aap->aar_arp_addr.address_format != T_ATM_ENDSYS_ADDR) {
			err = EINVAL;
			break;
		}
		atmsub.address_format = T_ATM_ABSENT;
		atmsub.address_length = 0;
		ip = SATOSIN(&aap->aar_arp_dst)->sin_addr;

		/*
		 * Validate IP address
		 */
		if (uniarp_validate_ip(uip, &ip, aap->aar_arp_origin) != 0) {
			err = EADDRNOTAVAIL;
			break;
		}

		/*
		 * Add an entry to the cache
		 */
		err = uniarp_cache_svc(uip, &ip, &aap->aar_arp_addr,
				&atmsub, aap->aar_arp_origin);
		break;

	case AIOCS_DEL_ARP:
		/*
		 * Delete an ARP mapping
		 */
		adp = (struct atmdelreq *)data;
		uip = (struct uniip *)arg1;
		ip = SATOSIN(&adp->adr_arp_dst)->sin_addr;

		/*
		 * Now find the entry to be deleted
		 */
		UNIARP_LOOKUP(ip.s_addr, uap);
		if (uap == NULL) {
			err = ENOENT;
			break;
		}

		/*
		 * Notify all VCCs using this entry that they must finish
		 * up now.  
		 */
		uap->ua_flags |= UAF_LOCKED;
		for (ivp = uap->ua_ivp; ivp; ivp = inext) {
			inext = ivp->iv_arpnext;
			(*ivp->iv_ipnif->inf_arpnotify)(ivp, MAP_FAILED);
		}

		/*
		 * Now free up the entry
		 */
		UNIARP_CANCEL(uap);
		UNIARP_DELETE(uap);
		atm_free((caddr_t)uap);
		break;

	case AIOCS_SET_ASV:
		/*
		 * Set interface ARP server address
		 */
		asp = (struct atmsetreq *)data;
		for (uip = uniip_head; uip; uip = uip->uip_next) {
			if (uip->uip_ipnif->inf_nif == (struct atm_nif *)arg1)
				break;
		}
		if (uip == NULL) {
			err = ENOPROTOOPT;
			break;
		}

		/*
		 * Check for our own address
		 */
		usp = (struct unisig *)
				uip->uip_ipnif->inf_nif->nif_pif->pif_siginst;
		if (ATM_ADDR_EQUAL(&asp->asr_arp_addr, &usp->us_addr)) {
			asp->asr_arp_addr.address_format = T_ATM_ABSENT;
		}

		/*
		 * If we're going into server mode, make sure we can get 
		 * the memory for the prefix list before continuing
		 */
		if (asp->asr_arp_addr.address_format == T_ATM_ABSENT) {
			i = asp->asr_arp_plen / sizeof(struct uniarp_prf);
			if (i <= 0) {
				err = EINVAL;
				break;
			}
			buf_len = i * sizeof(struct uniarp_prf);
			buf_addr = KM_ALLOC(buf_len, M_DEVBUF, M_NOWAIT);
			if (buf_addr == NULL) {
				err = ENOMEM;
				break;
			}
			err = copyin(asp->asr_arp_pbuf, buf_addr, buf_len);
			if (err) {
				KM_FREE(buf_addr, buf_len, M_DEVBUF);
				break;
			}
		} else {
			/* Silence the compiler */
			i = 0;
			buf_addr = NULL;
		}

		/*
		 * Free any existing prefix address list
		 */
		if (uip->uip_prefix != NULL) {
			KM_FREE(uip->uip_prefix, 
				uip->uip_nprefix * sizeof(struct uniarp_prf),
				M_DEVBUF);
			uip->uip_prefix = NULL;
			uip->uip_nprefix = 0;
		}

		if (asp->asr_arp_addr.address_format == T_ATM_ABSENT) {
			/*
			 * Set ATMARP server mode
			 */
			uip->uip_prefix = (struct uniarp_prf *)buf_addr;
			uip->uip_nprefix = i;
			uniarp_server_mode(uip);
		} else
			/*
			 * Set ATMARP client mode
			 */
			uniarp_client_mode(uip, &asp->asr_arp_addr);
		break;

	case AIOCS_INF_ARP:
		/*
		 * Get ARP table information
		 */
		aip = (struct atminfreq *)data;

		if (aip->air_arp_addr.sa_family != AF_INET)
			break;
		dst = SATOSIN(&aip->air_arp_addr)->sin_addr.s_addr;

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

		pip = ((struct siginst *)arg1)->si_pif;

		/*
		 * Run through entire arp table
		 */
		for (i = 0; i < UNIARP_HASHSIZ; i++) {
			for (uap = uniarp_arptab[i]; uap; uap = uap->ua_next) {
				/*
				 * We only want valid entries learned
				 * from the supplied interface.
				 */
				nip = uap->ua_intf->uip_ipnif->inf_nif;
				if (nip->nif_pif != pip)
					continue;
				if ((dst != INADDR_ANY) &&
				    (dst != uap->ua_dstip.s_addr))
					continue;

				/*
				 * Make sure there's room in the user's buffer
				 */
				if (buf_len < sizeof(aar)) {
					err = ENOSPC;
					break;
				}

				/*
				 * Fill in info to be returned
				 */
				SATOSIN(&aar.aap_arp_addr)->sin_family =
					AF_INET;
				SATOSIN(&aar.aap_arp_addr)->sin_addr.s_addr =
					uap->ua_dstip.s_addr;
				(void) snprintf(aar.aap_intf,
				    sizeof(aar.aap_intf), "%s%d",
					nip->nif_if.if_name,
					nip->nif_if.if_unit);
				aar.aap_flags = uap->ua_flags;
				aar.aap_origin = uap->ua_origin;
				if (uap->ua_flags & UAF_VALID)
					aar.aap_age = uap->ua_aging + 
					    uap->ua_retry * UNIARP_RETRY_AGE;
				else
					aar.aap_age = 0;
				ATM_ADDR_COPY(&uap->ua_dstatm, &aar.aap_addr);
				ATM_ADDR_COPY(&uap->ua_dstatmsub,
					&aar.aap_subaddr);

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

		/*
		 * Now go through the 'nomap' table
		 */
		if (err || (dst != INADDR_ANY))
			goto updbuf;
		for (uap = uniarp_nomaptab; uap; uap = uap->ua_next) {
			/*
			 * We only want valid entries learned
			 * from the supplied interface.
			 */
			nip = uap->ua_intf->uip_ipnif->inf_nif;
			if (nip->nif_pif != pip)
				continue;

			/*
			 * Make sure there's room in the user's buffer
			 */
			if (buf_len < sizeof(aar)) {
				err = ENOSPC;
				break;
			}

			/*
			 * Fill in info to be returned
			 */
			SATOSIN(&aar.aap_arp_addr)->sin_family = AF_INET;
			SATOSIN(&aar.aap_arp_addr)->sin_addr.s_addr = 0;
			(void) snprintf(aar.aap_intf,
			    sizeof(aar.aap_intf), "%s%d",
				nip->nif_if.if_name, nip->nif_if.if_unit);
			aar.aap_flags = 0;
			aar.aap_origin = uap->ua_origin;
			aar.aap_age = 0;
			ATM_ADDR_COPY(&uap->ua_dstatm, &aar.aap_addr);
			ATM_ADDR_COPY(&uap->ua_dstatmsub,
				&aar.aap_subaddr);

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

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

		/*
		 * If the user wants the refresh status reset and no 
		 * errors have been encountered, then do the reset
		 */
		if ((err == 0) && (aip->air_arp_flags & ARP_RESET_REF)) {
			for (i = 0; i < UNIARP_HASHSIZ; i++) {
				for (uap = uniarp_arptab[i]; uap; 
							uap = uap->ua_next) {
					/*
					 * We only want valid entries learned
					 * from the supplied interface.
					 */
					nip = uap->ua_intf->uip_ipnif->inf_nif;
					if (nip->nif_pif != pip)
						continue;
					if ((dst != INADDR_ANY) &&
					    (dst != uap->ua_dstip.s_addr))
						continue;

					/*
					 * Reset refresh flag
					 */
					uap->ua_flags &= ~UAF_REFRESH;
				}
			}
		}
		break;

	case AIOCS_INF_ASV:
		/*
		 * Get ARP server information
		 */
		aip = (struct atminfreq *)data;
		nip = (struct atm_nif *)arg1;

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

		for (uip = uniip_head; uip; uip = uip->uip_next) {

			if (uip->uip_ipnif->inf_nif != nip)
				continue;

			/*
			 * Make sure there's room in the user's buffer
			 */
			if (buf_len < sizeof(asr)) {
				err = ENOSPC;
				break;
			}

			/*
			 * Fill in info to be returned
			 */
			(void) snprintf(asr.asp_intf,
			    sizeof(asr.asp_intf), "%s%d",
				nip->nif_if.if_name, nip->nif_if.if_unit);
			asr.asp_state = uip->uip_arpstate;
			if (uip->uip_arpstate == UIAS_SERVER_ACTIVE) {
				asr.asp_addr.address_format = T_ATM_ABSENT;
				asr.asp_addr.address_length = 0;
			} else {
				ATM_ADDR_COPY(&uip->uip_arpsvratm,
						&asr.asp_addr);
			}
			asr.asp_subaddr.address_format = T_ATM_ABSENT;
			asr.asp_subaddr.address_length = 0;
			asr.asp_nprefix = uip->uip_nprefix;

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

			/*
			 * Copy the prefix list into the user's buffer
			 */
			if (uip->uip_nprefix) {
				i = uip->uip_nprefix 
						* sizeof(struct uniarp_prf);
				if (buf_len < i) {
					err = ENOSPC;
					break;
				}
				if ((err = copyout(uip->uip_prefix, buf_addr, i)) != 0)
					break;
				buf_addr += i;
				buf_len -= i;
			}
		}

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

	default:
		err = EOPNOTSUPP;
	}

	return (err);
}


/*
 * Get Connection's Application/Owner Name
 * 
 * Arguments:
 *	tok	uniarp connection token (pointer to ipvcc)
 *
 * Returns:
 *	addr	pointer to string containing our name
 *
 */
caddr_t
uniarp_getname(tok)
	void		*tok;
{
	return ("ATMARP");
}

OpenPOWER on IntegriCloud