summaryrefslogtreecommitdiffstats
path: root/sys/contrib/ipfilter/netinet/ip_htable.c
blob: 62707f40edd2cbad1015ca17f6453e6c3cb26975 (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
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
/*	$FreeBSD$	*/

/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 */
#if defined(KERNEL) || defined(_KERNEL)
# undef KERNEL
# undef _KERNEL
# define        KERNEL	1
# define        _KERNEL	1
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/file.h>
#if !defined(_KERNEL)
# include <stdlib.h>
# include <string.h>
# define _KERNEL
# ifdef __OpenBSD__
struct file;
# endif
# include <sys/uio.h>
# undef _KERNEL
#endif
#include <sys/socket.h>
#if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
# include <sys/malloc.h>
#endif
#if defined(__FreeBSD__)
#  include <sys/cdefs.h>
#  include <sys/proc.h>
#endif
#if !defined(__svr4__) && !defined(__SVR4) && !defined(__hpux) && \
    !defined(linux)
# include <sys/mbuf.h>
#endif
#if defined(_KERNEL)
# include <sys/systm.h>
#else
# include "ipf.h"
#endif
#include <netinet/in.h>
#include <net/if.h>

#include "netinet/ip_compat.h"
#include "netinet/ip_fil.h"
#include "netinet/ip_lookup.h"
#include "netinet/ip_htable.h"
/* END OF INCLUDES */

#if !defined(lint)
static const char rcsid[] = "@(#)$Id$";
#endif

# ifdef USE_INET6
static iphtent_t *ipf_iphmfind6 __P((iphtable_t *, i6addr_t *));
# endif
static iphtent_t *ipf_iphmfind __P((iphtable_t *, struct in_addr *));
static int ipf_iphmfindip __P((ipf_main_softc_t *, void *, int, void *, u_int));
static int ipf_htable_clear __P((ipf_main_softc_t *, void *, iphtable_t *));
static int ipf_htable_create __P((ipf_main_softc_t *, void *, iplookupop_t *));
static int ipf_htable_deref __P((ipf_main_softc_t *, void *, void *));
static int ipf_htable_destroy __P((ipf_main_softc_t *, void *, int, char *));
static void *ipf_htable_exists __P((void *, int, char *));
static size_t ipf_htable_flush __P((ipf_main_softc_t *, void *,
				    iplookupflush_t *));
static void ipf_htable_free __P((void *, iphtable_t *));
static int ipf_htable_iter_deref __P((ipf_main_softc_t *, void *, int,
				      int, void *));
static int ipf_htable_iter_next __P((ipf_main_softc_t *, void *, ipftoken_t *,
				     ipflookupiter_t *));
static int ipf_htable_node_add __P((ipf_main_softc_t *, void *,
				    iplookupop_t *, int));
static int ipf_htable_node_del __P((ipf_main_softc_t *, void *,
				    iplookupop_t *, int));
static int ipf_htable_remove __P((ipf_main_softc_t *, void *, iphtable_t *));
static void *ipf_htable_soft_create __P((ipf_main_softc_t *));
static void ipf_htable_soft_destroy __P((ipf_main_softc_t *, void *));
static int ipf_htable_soft_init __P((ipf_main_softc_t *, void *));
static void ipf_htable_soft_fini __P((ipf_main_softc_t *, void *));
static int ipf_htable_stats_get __P((ipf_main_softc_t *, void *,
				     iplookupop_t *));
static int ipf_htable_table_add __P((ipf_main_softc_t *, void *,
				     iplookupop_t *));
static int ipf_htable_table_del __P((ipf_main_softc_t *, void *,
				     iplookupop_t *));
static int ipf_htent_deref __P((void *, iphtent_t *));
static iphtent_t *ipf_htent_find __P((iphtable_t *, iphtent_t *));
static int ipf_htent_insert __P((ipf_main_softc_t *, void *, iphtable_t *,
				 iphtent_t *));
static int ipf_htent_remove __P((ipf_main_softc_t *, void *, iphtable_t *,
				 iphtent_t *));
static void *ipf_htable_select_add_ref __P((void *, int, char *));
static void ipf_htable_expire __P((ipf_main_softc_t *, void *));


typedef struct ipf_htable_softc_s {
	u_long		ipht_nomem[LOOKUP_POOL_SZ];
	u_long		ipf_nhtables[LOOKUP_POOL_SZ];
	u_long		ipf_nhtnodes[LOOKUP_POOL_SZ];
	iphtable_t	*ipf_htables[LOOKUP_POOL_SZ];
	iphtent_t	*ipf_node_explist;
} ipf_htable_softc_t;

ipf_lookup_t ipf_htable_backend = {
	IPLT_HASH,
	ipf_htable_soft_create,
	ipf_htable_soft_destroy,
	ipf_htable_soft_init,
	ipf_htable_soft_fini,
	ipf_iphmfindip,
	ipf_htable_flush,
	ipf_htable_iter_deref,
	ipf_htable_iter_next,
	ipf_htable_node_add,
	ipf_htable_node_del,
	ipf_htable_stats_get,
	ipf_htable_table_add,
	ipf_htable_table_del,
	ipf_htable_deref,
	ipf_htable_exists,
	ipf_htable_select_add_ref,
	NULL,
	ipf_htable_expire,
	NULL
};


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_soft_create                                      */
/* Returns:     void *   - NULL = failure, else pointer to local context    */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*                                                                          */
/* Initialise the routing table data structures where required.             */
/* ------------------------------------------------------------------------ */
static void *
ipf_htable_soft_create(softc)
	ipf_main_softc_t *softc;
{
	ipf_htable_softc_t *softh;

	KMALLOC(softh, ipf_htable_softc_t *);
	if (softh == NULL) {
		IPFERROR(30026);
		return NULL;
	}

	bzero((char *)softh, sizeof(*softh));

	return softh;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_soft_destroy                                     */
/* Returns:     Nil                                                         */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*                                                                          */
/* Clean up the pool by free'ing the radix tree associated with it and free */
/* up the pool context too.                                                 */
/* ------------------------------------------------------------------------ */
static void
ipf_htable_soft_destroy(softc, arg)
	ipf_main_softc_t *softc;
	void *arg;
{
	ipf_htable_softc_t *softh = arg;

	KFREE(softh);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_soft_init                                        */
/* Returns:     int     - 0 = success, else error                           */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*                                                                          */
/* Initialise the hash table ready for use.                                 */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_soft_init(softc, arg)
	ipf_main_softc_t *softc;
	void *arg;
{
	ipf_htable_softc_t *softh = arg;

	bzero((char *)softh, sizeof(*softh));

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_soft_fini                                        */
/* Returns:     Nil                                                         */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/* Locks:       WRITE(ipf_global)                                           */
/*                                                                          */
/* Clean up all the pool data structures allocated and call the cleanup     */
/* function for the radix tree that supports the pools. ipf_pool_destroy is */
/* used to delete the pools one by one to ensure they're properly freed up. */
/* ------------------------------------------------------------------------ */
static void
ipf_htable_soft_fini(softc, arg)
	ipf_main_softc_t *softc;
	void *arg;
{
	iplookupflush_t fop;

	fop.iplf_type = IPLT_HASH;
	fop.iplf_unit = IPL_LOGALL;
	fop.iplf_arg = 0;
	fop.iplf_count = 0;
	*fop.iplf_name = '\0';
	ipf_htable_flush(softc, arg, &fop);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_stats_get                                        */
/* Returns:     int - 0 = success, else error                               */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* Copy the relevant statistics out of internal structures and into the     */
/* structure used to export statistics.                                     */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_stats_get(softc, arg, op)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupop_t *op;
{
	ipf_htable_softc_t *softh = arg;
	iphtstat_t stats;
	int err;

	if (op->iplo_size != sizeof(stats)) {
		IPFERROR(30001);
		return EINVAL;
	}

	stats.iphs_tables = softh->ipf_htables[op->iplo_unit + 1];
	stats.iphs_numtables = softh->ipf_nhtables[op->iplo_unit + 1];
	stats.iphs_numnodes = softh->ipf_nhtnodes[op->iplo_unit + 1];
	stats.iphs_nomem = softh->ipht_nomem[op->iplo_unit + 1];

	err = COPYOUT(&stats, op->iplo_struct, sizeof(stats));
	if (err != 0) {
		IPFERROR(30013);
		return EFAULT;
	}
	return 0;

}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_create                                           */
/* Returns:     int - 0 = success, else error                               */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* Create a new hash table using the template passed.                       */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_create(softc, arg, op)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupop_t *op;
{
	ipf_htable_softc_t *softh = arg;
	iphtable_t htab, *iph, *oiph;
	char name[FR_GROUPLEN];
	int err, i, unit;

	if (op->iplo_size != sizeof(htab)) {
		IPFERROR(30024);
		return EINVAL;
	}
	err = COPYIN(op->iplo_struct, &htab, sizeof(htab));
	if (err != 0) {
		IPFERROR(30003);
		return EFAULT;
	}

	unit = op->iplo_unit;
	if (htab.iph_unit != unit) {
		IPFERROR(30005);
		return EINVAL;
	}
	if (htab.iph_size < 1) {
		IPFERROR(30025);
		return EINVAL;
	}


	if ((op->iplo_arg & IPHASH_ANON) == 0) {
		iph = ipf_htable_exists(softh, unit, op->iplo_name);
		if (iph != NULL) {
			if ((iph->iph_flags & IPHASH_DELETE) == 0) {
				IPFERROR(30004);
				return EEXIST;
			}
			iph->iph_flags &= ~IPHASH_DELETE;
			iph->iph_ref++;
			return 0;
		}
	}

	KMALLOC(iph, iphtable_t *);
	if (iph == NULL) {
		softh->ipht_nomem[op->iplo_unit + 1]++;
		IPFERROR(30002);
		return ENOMEM;
	}
	*iph = htab;

	if ((op->iplo_arg & IPHASH_ANON) != 0) {
		i = IPHASH_ANON;
		do {
			i++;
#if defined(SNPRINTF) && defined(_KERNEL)
			SNPRINTF(name, sizeof(name), "%u", i);
#else
			(void)sprintf(name, "%u", i);
#endif
			for (oiph = softh->ipf_htables[unit + 1]; oiph != NULL;
			     oiph = oiph->iph_next)
				if (strncmp(oiph->iph_name, name,
					    sizeof(oiph->iph_name)) == 0)
					break;
		} while (oiph != NULL);

		(void)strncpy(iph->iph_name, name, sizeof(iph->iph_name));
		(void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
		iph->iph_type |= IPHASH_ANON;
	} else {
		(void)strncpy(iph->iph_name, op->iplo_name,
			      sizeof(iph->iph_name));
		iph->iph_name[sizeof(iph->iph_name) - 1] = '\0';
	}

	KMALLOCS(iph->iph_table, iphtent_t **,
		 iph->iph_size * sizeof(*iph->iph_table));
	if (iph->iph_table == NULL) {
		KFREE(iph);
		softh->ipht_nomem[unit + 1]++;
		IPFERROR(30006);
		return ENOMEM;
	}

	bzero((char *)iph->iph_table, iph->iph_size * sizeof(*iph->iph_table));
	iph->iph_maskset[0] = 0;
	iph->iph_maskset[1] = 0;
	iph->iph_maskset[2] = 0;
	iph->iph_maskset[3] = 0;

	iph->iph_ref = 1;
	iph->iph_list = NULL;
	iph->iph_tail = &iph->iph_list;
	iph->iph_next = softh->ipf_htables[unit + 1];
	iph->iph_pnext = &softh->ipf_htables[unit + 1];
	if (softh->ipf_htables[unit + 1] != NULL)
		softh->ipf_htables[unit + 1]->iph_pnext = &iph->iph_next;
	softh->ipf_htables[unit + 1] = iph;

	softh->ipf_nhtables[unit + 1]++;

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_table_del                                        */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_table_del(softc, arg, op)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupop_t *op;
{
	return ipf_htable_destroy(softc, arg, op->iplo_unit, op->iplo_name);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_destroy                                          */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* Find the hash table that belongs to the relevant part of ipfilter with a */
/* matching name and attempt to destroy it.  If it is in use, empty it out  */
/* and mark it for deletion so that when all the references disappear, it   */
/* can be removed.                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_destroy(softc, arg, unit, name)
	ipf_main_softc_t *softc;
	void *arg;
	int unit;
	char *name;
{
	iphtable_t *iph;

	iph = ipf_htable_find(arg, unit, name);
	if (iph == NULL) {
		IPFERROR(30007);
		return ESRCH;
	}

	if (iph->iph_unit != unit) {
		IPFERROR(30008);
		return EINVAL;
	}

	if (iph->iph_ref != 0) {
		ipf_htable_clear(softc, arg, iph);
		iph->iph_flags |= IPHASH_DELETE;
		return 0;
	}

	ipf_htable_remove(softc, arg, iph);

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_clear                                            */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              iph(I)   - pointer to hash table to destroy                 */
/*                                                                          */
/* Clean out the hash table by walking the list of entries and removing     */
/* each one, one by one.                                                    */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_clear(softc, arg, iph)
	ipf_main_softc_t *softc;
	void *arg;
	iphtable_t *iph;
{
	iphtent_t *ipe;

	while ((ipe = iph->iph_list) != NULL)
		if (ipf_htent_remove(softc, arg, iph, ipe) != 0)
			return 1;
	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_free                                             */
/* Returns:     Nil                                                         */
/* Parameters:  arg(I) - pointer to local context to use                    */
/*              iph(I) - pointer to hash table to destroy                   */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static void
ipf_htable_free(arg, iph)
	void *arg;
	iphtable_t *iph;
{
	ipf_htable_softc_t *softh = arg;

	if (iph->iph_next != NULL)
		iph->iph_next->iph_pnext = iph->iph_pnext;
	if (iph->iph_pnext != NULL)
		*iph->iph_pnext = iph->iph_next;
	iph->iph_pnext = NULL;
	iph->iph_next = NULL;

	softh->ipf_nhtables[iph->iph_unit + 1]--;

	KFREES(iph->iph_table, iph->iph_size * sizeof(*iph->iph_table));
	KFREE(iph);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_remove                                           */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              iph(I)   - pointer to hash table to destroy                 */
/*                                                                          */
/* It is necessary to unlink here as well as free (called by deref) so that */
/* the while loop in ipf_htable_flush() functions properly.                 */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_remove(softc, arg, iph)
	ipf_main_softc_t *softc;
	void *arg;
	iphtable_t *iph;
{

	if (ipf_htable_clear(softc, arg, iph) != 0)
		return 1;

	if (iph->iph_pnext != NULL)
		*iph->iph_pnext = iph->iph_next;
	if (iph->iph_next != NULL)
		iph->iph_next->iph_pnext = iph->iph_pnext;
	iph->iph_pnext = NULL;
	iph->iph_next = NULL;

	return ipf_htable_deref(softc, arg, iph);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_node_del                                         */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*              uid(I)   - real uid of process doing operation              */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_node_del(softc, arg, op, uid)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupop_t *op;
	int uid;
{
        iphtable_t *iph;
        iphtent_t hte, *ent;
	int err;

	if (op->iplo_size != sizeof(hte)) {
		IPFERROR(30014);
		return EINVAL;
	}

	err = COPYIN(op->iplo_struct, &hte, sizeof(hte));
	if (err != 0) {
		IPFERROR(30015);
		return EFAULT;
	}

	iph = ipf_htable_find(arg, op->iplo_unit, op->iplo_name);
	if (iph == NULL) {
		IPFERROR(30016);
		return ESRCH;
	}

	ent = ipf_htent_find(iph, &hte);
	if (ent == NULL) {
		IPFERROR(30022);
		return ESRCH;
	}

	if ((uid != 0) && (ent->ipe_uid != uid)) {
		IPFERROR(30023);
		return EACCES;
	}

	err = ipf_htent_remove(softc, arg, iph, ent);

	return err;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_node_del                                         */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_table_add(softc, arg, op)
	ipf_main_softc_t *softc;
	void *arg;
        iplookupop_t *op;
{
	int err;

	if (ipf_htable_find(arg, op->iplo_unit, op->iplo_name) != NULL) {
		IPFERROR(30017);
		err = EEXIST;
	} else {
		err = ipf_htable_create(softc, arg, op);
	}

	return err;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htent_remove                                            */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              iph(I)   - pointer to hash table                            */
/*              ipe(I)   - pointer to hash table entry to remove            */
/*                                                                          */
/* Delete an entry from a hash table.                                       */
/* ------------------------------------------------------------------------ */
static int
ipf_htent_remove(softc, arg, iph, ipe)
	ipf_main_softc_t *softc;
	void *arg;
	iphtable_t *iph;
	iphtent_t *ipe;
{

	if (iph->iph_tail == &ipe->ipe_next)
		iph->iph_tail = ipe->ipe_pnext;

	if (ipe->ipe_hnext != NULL)
		ipe->ipe_hnext->ipe_phnext = ipe->ipe_phnext;
	if (ipe->ipe_phnext != NULL)
		*ipe->ipe_phnext = ipe->ipe_hnext;
	ipe->ipe_phnext = NULL;
	ipe->ipe_hnext = NULL;

	if (ipe->ipe_dnext != NULL)
		ipe->ipe_dnext->ipe_pdnext = ipe->ipe_pdnext;
	if (ipe->ipe_pdnext != NULL)
		*ipe->ipe_pdnext = ipe->ipe_dnext;
	ipe->ipe_pdnext = NULL;
	ipe->ipe_dnext = NULL;

	if (ipe->ipe_next != NULL)
		ipe->ipe_next->ipe_pnext = ipe->ipe_pnext;
	if (ipe->ipe_pnext != NULL)
		*ipe->ipe_pnext = ipe->ipe_next;
	ipe->ipe_pnext = NULL;
	ipe->ipe_next = NULL;

	switch (iph->iph_type & ~IPHASH_ANON)
	{
	case IPHASH_GROUPMAP :
		if (ipe->ipe_group != NULL)
			ipf_group_del(softc, ipe->ipe_ptr, NULL);
		break;

	default :
		ipe->ipe_ptr = NULL;
		ipe->ipe_value = 0;
		break;
	}

	return ipf_htent_deref(arg, ipe);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_deref                                            */
/* Returns:     int       - 0 = success, else error                         */
/* Parameters:  softc(I)  - pointer to soft context main structure          */
/*              arg(I)    - pointer to local context to use                 */
/*              object(I) - pointer to hash table                           */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_deref(softc, arg, object)
	ipf_main_softc_t *softc;
	void *arg, *object;
{
	ipf_htable_softc_t *softh = arg;
	iphtable_t *iph = object;
	int refs;

	iph->iph_ref--;
	refs = iph->iph_ref;

	if (iph->iph_ref == 0) {
		ipf_htable_free(softh, iph);
	}

	return refs;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htent_deref                                             */
/* Parameters:  arg(I) - pointer to local context to use                    */
/*              ipe(I) -                                                    */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htent_deref(arg, ipe)
	void *arg;
	iphtent_t *ipe;
{
	ipf_htable_softc_t *softh = arg;

	ipe->ipe_ref--;
	if (ipe->ipe_ref == 0) {
		softh->ipf_nhtnodes[ipe->ipe_unit + 1]--;
		KFREE(ipe);

		return 0;
	}

	return ipe->ipe_ref;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_exists                                           */
/* Parameters:  arg(I) - pointer to local context to use                    */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static void *
ipf_htable_exists(arg, unit, name)
	void *arg;
	int unit;
	char *name;
{
	ipf_htable_softc_t *softh = arg;
	iphtable_t *iph;

	if (unit == IPL_LOGALL) {
		int i;

		for (i = 0; i <= LOOKUP_POOL_MAX; i++) {
			for (iph = softh->ipf_htables[i]; iph != NULL;
			     iph = iph->iph_next) {
				if (strncmp(iph->iph_name, name,
					    sizeof(iph->iph_name)) == 0)
					break;
			}
			if (iph != NULL)
				break;
		}
	} else {
		for (iph = softh->ipf_htables[unit + 1]; iph != NULL;
		     iph = iph->iph_next) {
			if (strncmp(iph->iph_name, name,
				    sizeof(iph->iph_name)) == 0)
				break;
		}
	}
	return iph;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_select_add_ref                                   */
/* Returns:     void *  - NULL = failure, else pointer to the hash table    */
/* Parameters:  arg(I)  - pointer to local context to use                   */
/*              unit(I) - ipfilter device to which we are working on        */
/*              name(I) - name of the hash table                            */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static void *
ipf_htable_select_add_ref(arg, unit, name)
	void *arg;
	int unit;
	char *name;
{
	iphtable_t *iph;

	iph = ipf_htable_exists(arg, unit, name);
	if (iph != NULL) {
		ATOMIC_INC32(iph->iph_ref);
	}
	return iph;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_find                                             */
/* Returns:     void *  - NULL = failure, else pointer to the hash table    */
/* Parameters:  arg(I)  - pointer to local context to use                   */
/*              unit(I) - ipfilter device to which we are working on        */
/*              name(I) - name of the hash table                            */
/*                                                                          */
/* This function is exposed becaues it is used in the group-map feature.    */
/* ------------------------------------------------------------------------ */
iphtable_t *
ipf_htable_find(arg, unit, name)
	void *arg;
	int unit;
	char *name;
{
	iphtable_t *iph;

	iph = ipf_htable_exists(arg, unit, name);
	if ((iph != NULL) && (iph->iph_flags & IPHASH_DELETE) == 0)
		return iph;

	return NULL;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_flush                                            */
/* Returns:     size_t   - number of entries flushed                        */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static size_t
ipf_htable_flush(softc, arg, op)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupflush_t *op;
{
	ipf_htable_softc_t *softh = arg;
	iphtable_t *iph;
	size_t freed;
	int i;

	freed = 0;

	for (i = -1; i <= IPL_LOGMAX; i++) {
		if (op->iplf_unit == i || op->iplf_unit == IPL_LOGALL) {
			while ((iph = softh->ipf_htables[i + 1]) != NULL) {
				if (ipf_htable_remove(softc, arg, iph) == 0) {
					freed++;
				} else {
					iph->iph_flags |= IPHASH_DELETE;
				}
			}
		}
	}

	return freed;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_node_add                                         */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*              uid(I)   - real uid of process doing operation              */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_node_add(softc, arg, op, uid)
	ipf_main_softc_t *softc;
	void *arg;
	iplookupop_t *op;
	int uid;
{
	iphtable_t *iph;
	iphtent_t hte;
	int err;

	if (op->iplo_size != sizeof(hte)) {
		IPFERROR(30018);
		return EINVAL;
	}

	err = COPYIN(op->iplo_struct, &hte, sizeof(hte));
	if (err != 0) {
		IPFERROR(30019);
		return EFAULT;
	}
	hte.ipe_uid = uid;

	iph = ipf_htable_find(arg, op->iplo_unit, op->iplo_name);
	if (iph == NULL) {
		IPFERROR(30020);
		return ESRCH;
	}

	if (ipf_htent_find(iph, &hte) != NULL) {
		IPFERROR(30021);
		return EEXIST;
	}

	err = ipf_htent_insert(softc, arg, iph, &hte);

	return err;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htent_insert                                            */
/* Returns:     int      - 0 = success, -1 =  error                         */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              op(I)    - pointer to lookup operation data                 */
/*              ipeo(I)  -                                                  */
/*                                                                          */
/* Add an entry to a hash table.                                            */
/* ------------------------------------------------------------------------ */
static int
ipf_htent_insert(softc, arg, iph, ipeo)
	ipf_main_softc_t *softc;
	void *arg;
	iphtable_t *iph;
	iphtent_t *ipeo;
{
	ipf_htable_softc_t *softh = arg;
	iphtent_t *ipe;
	u_int hv;
	int bits;

	KMALLOC(ipe, iphtent_t *);
	if (ipe == NULL)
		return -1;

	bcopy((char *)ipeo, (char *)ipe, sizeof(*ipe));
	ipe->ipe_addr.i6[0] &= ipe->ipe_mask.i6[0];
	if (ipe->ipe_family == AF_INET) {
		bits = count4bits(ipe->ipe_mask.in4_addr);
		ipe->ipe_addr.i6[1] = 0;
		ipe->ipe_addr.i6[2] = 0;
		ipe->ipe_addr.i6[3] = 0;
		ipe->ipe_mask.i6[1] = 0;
		ipe->ipe_mask.i6[2] = 0;
		ipe->ipe_mask.i6[3] = 0;
		hv = IPE_V4_HASH_FN(ipe->ipe_addr.in4_addr,
				    ipe->ipe_mask.in4_addr, iph->iph_size);
	} else
#ifdef USE_INET6
	if (ipe->ipe_family == AF_INET6) {
		ipe->ipe_addr.i6[1] &= ipe->ipe_mask.i6[1];
		ipe->ipe_addr.i6[2] &= ipe->ipe_mask.i6[2];
		ipe->ipe_addr.i6[3] &= ipe->ipe_mask.i6[3];

		bits = count6bits(ipe->ipe_mask.i6);
		hv = IPE_V6_HASH_FN(ipe->ipe_addr.i6,
				    ipe->ipe_mask.i6, iph->iph_size);
	} else
#endif
	{
		KFREE(ipe);
		return -1;
	}

	ipe->ipe_owner = iph;
	ipe->ipe_ref = 1;
	ipe->ipe_hnext = iph->iph_table[hv];
	ipe->ipe_phnext = iph->iph_table + hv;

	if (iph->iph_table[hv] != NULL)
		iph->iph_table[hv]->ipe_phnext = &ipe->ipe_hnext;
	iph->iph_table[hv] = ipe;

	ipe->ipe_pnext = iph->iph_tail;
	*iph->iph_tail = ipe;
	iph->iph_tail = &ipe->ipe_next;
	ipe->ipe_next = NULL;

	if (ipe->ipe_die != 0) {
		/*
		 * If the new node has a given expiration time, insert it
		 * into the list of expiring nodes with the ones to be
		 * removed first added to the front of the list. The
		 * insertion is O(n) but it is kept sorted for quick scans
		 * at expiration interval checks.
		 */
		iphtent_t *n;

		ipe->ipe_die = softc->ipf_ticks + IPF_TTLVAL(ipe->ipe_die);
		for (n = softh->ipf_node_explist; n != NULL; n = n->ipe_dnext) {
			if (ipe->ipe_die < n->ipe_die)
				break;
			if (n->ipe_dnext == NULL) {
				/*
				 * We've got to the last node and everything
				 * wanted to be expired before this new node,
				 * so we have to tack it on the end...
				 */
				n->ipe_dnext = ipe;
				ipe->ipe_pdnext = &n->ipe_dnext;
				n = NULL;
				break;
			}
		}

		if (softh->ipf_node_explist == NULL) {
			softh->ipf_node_explist = ipe;
			ipe->ipe_pdnext = &softh->ipf_node_explist;
		} else if (n != NULL) {
			ipe->ipe_dnext = n;
			ipe->ipe_pdnext = n->ipe_pdnext;
			n->ipe_pdnext = &ipe->ipe_dnext;
		}
	}

	if (ipe->ipe_family == AF_INET) {
		ipf_inet_mask_add(bits, &iph->iph_v4_masks);
	}
#ifdef USE_INET6
	else if (ipe->ipe_family == AF_INET6) {
		ipf_inet6_mask_add(bits, &ipe->ipe_mask, &iph->iph_v6_masks);
	}
#endif

	switch (iph->iph_type & ~IPHASH_ANON)
	{
	case IPHASH_GROUPMAP :
		ipe->ipe_ptr = ipf_group_add(softc, ipe->ipe_group, NULL,
					   iph->iph_flags, IPL_LOGIPF,
					   softc->ipf_active);
		break;

	default :
		ipe->ipe_ptr = NULL;
		ipe->ipe_value = 0;
		break;
	}

	ipe->ipe_unit = iph->iph_unit;
	softh->ipf_nhtnodes[ipe->ipe_unit + 1]++;

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htent_find                                              */
/* Returns:     int     - 0 = success, else error                           */
/* Parameters:  iph(I)  - pointer to table to search                        */
/*              ipeo(I) - pointer to entry to find                          */
/*                                                                          */
/* While it isn't absolutely necessary to for the address and mask to be    */
/* passed in through an iphtent_t structure, one is always present when it  */
/* is time to call this function, so it is just more convenient.            */
/* ------------------------------------------------------------------------ */
static iphtent_t *
ipf_htent_find(iph, ipeo)
	iphtable_t *iph;
	iphtent_t *ipeo;
{
	iphtent_t ipe, *ent;
	u_int hv;
	int bits;

	bcopy((char *)ipeo, (char *)&ipe, sizeof(ipe));
	ipe.ipe_addr.i6[0] &= ipe.ipe_mask.i6[0];
	ipe.ipe_addr.i6[1] &= ipe.ipe_mask.i6[1];
	ipe.ipe_addr.i6[2] &= ipe.ipe_mask.i6[2];
	ipe.ipe_addr.i6[3] &= ipe.ipe_mask.i6[3];
	if (ipe.ipe_family == AF_INET) {
		bits = count4bits(ipe.ipe_mask.in4_addr);
		ipe.ipe_addr.i6[1] = 0;
		ipe.ipe_addr.i6[2] = 0;
		ipe.ipe_addr.i6[3] = 0;
		ipe.ipe_mask.i6[1] = 0;
		ipe.ipe_mask.i6[2] = 0;
		ipe.ipe_mask.i6[3] = 0;
		hv = IPE_V4_HASH_FN(ipe.ipe_addr.in4_addr,
				    ipe.ipe_mask.in4_addr, iph->iph_size);
	} else
#ifdef USE_INET6
	if (ipe.ipe_family == AF_INET6) {
		bits = count6bits(ipe.ipe_mask.i6);
		hv = IPE_V6_HASH_FN(ipe.ipe_addr.i6,
				    ipe.ipe_mask.i6, iph->iph_size);
	} else
#endif
		return NULL;

	for (ent = iph->iph_table[hv]; ent != NULL; ent = ent->ipe_hnext) {
		if (ent->ipe_family != ipe.ipe_family)
			continue;
		if (IP6_NEQ(&ipe.ipe_addr, &ent->ipe_addr))
			continue;
		if (IP6_NEQ(&ipe.ipe_mask, &ent->ipe_mask))
			continue;
		break;
	}

	return ent;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_iphmfindgroup                                           */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              tptr(I)  -                                                  */
/*              aptr(I)  -                                                  */
/*                                                                          */
/* Search a hash table for a matching entry and return the pointer stored   */
/* in it for use as the next group of rules to search.                      */
/*                                                                          */
/* This function is exposed becaues it is used in the group-map feature.    */
/* ------------------------------------------------------------------------ */
void *
ipf_iphmfindgroup(softc, tptr, aptr)
	ipf_main_softc_t *softc;
	void *tptr, *aptr;
{
	struct in_addr *addr;
	iphtable_t *iph;
	iphtent_t *ipe;
	void *rval;

	READ_ENTER(&softc->ipf_poolrw);
	iph = tptr;
	addr = aptr;

	ipe = ipf_iphmfind(iph, addr);
	if (ipe != NULL)
		rval = ipe->ipe_ptr;
	else
		rval = NULL;
	RWLOCK_EXIT(&softc->ipf_poolrw);
	return rval;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_iphmfindip                                              */
/* Returns:     int     - 0 == +ve match, -1 == error, 1 == -ve/no match    */
/* Parameters:  softc(I)     - pointer to soft context main structure       */
/*              tptr(I)      - pointer to the pool to search                */
/*              ipversion(I) - IP protocol version (4 or 6)                 */
/*              aptr(I)      - pointer to address information               */
/*              bytes(I)     - packet length                                */
/*                                                                          */
/* Search the hash table for a given address and return a search result.    */
/* ------------------------------------------------------------------------ */
static int
ipf_iphmfindip(softc, tptr, ipversion, aptr, bytes)
	ipf_main_softc_t *softc;
	void *tptr, *aptr;
	int ipversion;
	u_int bytes;
{
	struct in_addr *addr;
	iphtable_t *iph;
	iphtent_t *ipe;
	int rval;

	if (tptr == NULL || aptr == NULL)
		return -1;

	iph = tptr;
	addr = aptr;

	READ_ENTER(&softc->ipf_poolrw);
	if (ipversion == 4) {
		ipe = ipf_iphmfind(iph, addr);
#ifdef USE_INET6
	} else if (ipversion == 6) {
		ipe = ipf_iphmfind6(iph, (i6addr_t *)addr);
#endif
	} else {
		ipe = NULL;
	}

	if (ipe != NULL) {
		rval = 0;
		ipe->ipe_hits++;
		ipe->ipe_bytes += bytes;
	} else {
		rval = 1;
	}
	RWLOCK_EXIT(&softc->ipf_poolrw);
	return rval;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_iphmfindip                                              */
/* Parameters:  iph(I)  - pointer to hash table                             */
/*              addr(I) - pointer to IPv4 address                           */
/* Locks:  ipf_poolrw                                                       */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static iphtent_t *
ipf_iphmfind(iph, addr)
	iphtable_t *iph;
	struct in_addr *addr;
{
	u_32_t msk, ips;
	iphtent_t *ipe;
	u_int hv;
	int i;

	i = 0;
maskloop:
	msk = iph->iph_v4_masks.imt4_active[i];
	ips = addr->s_addr & msk;
	hv = IPE_V4_HASH_FN(ips, msk, iph->iph_size);
	for (ipe = iph->iph_table[hv]; (ipe != NULL); ipe = ipe->ipe_hnext) {
		if ((ipe->ipe_family != AF_INET) ||
		    (ipe->ipe_mask.in4_addr != msk) ||
		    (ipe->ipe_addr.in4_addr != ips)) {
			continue;
		}
		break;
	}

	if (ipe == NULL) {
		i++;
		if (i < iph->iph_v4_masks.imt4_max)
			goto maskloop;
	}
	return ipe;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_iter_next                                        */
/* Returns:     int      - 0 = success, else error                          */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              token(I) -                                                  */
/*              ilp(I)   -                                                  */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_iter_next(softc, arg, token, ilp)
	ipf_main_softc_t *softc;
	void *arg;
	ipftoken_t *token;
	ipflookupiter_t *ilp;
{
	ipf_htable_softc_t *softh = arg;
	iphtent_t *node, zn, *nextnode;
	iphtable_t *iph, zp, *nextiph;
	void *hnext;
	int err;

	err = 0;
	iph = NULL;
	node = NULL;
	nextiph = NULL;
	nextnode = NULL;

	READ_ENTER(&softc->ipf_poolrw);

	switch (ilp->ili_otype)
	{
	case IPFLOOKUPITER_LIST :
		iph = token->ipt_data;
		if (iph == NULL) {
			nextiph = softh->ipf_htables[(int)ilp->ili_unit + 1];
		} else {
			nextiph = iph->iph_next;
		}

		if (nextiph != NULL) {
			ATOMIC_INC(nextiph->iph_ref);
			token->ipt_data = nextiph;
		} else {
			bzero((char *)&zp, sizeof(zp));
			nextiph = &zp;
			token->ipt_data = NULL;
		}
		hnext = nextiph->iph_next;
		break;

	case IPFLOOKUPITER_NODE :
		node = token->ipt_data;
		if (node == NULL) {
			iph = ipf_htable_find(arg, ilp->ili_unit,
					      ilp->ili_name);
			if (iph == NULL) {
				IPFERROR(30009);
				err = ESRCH;
			} else {
				nextnode = iph->iph_list;
			}
		} else {
			nextnode = node->ipe_next;
		}

		if (nextnode != NULL) {
			ATOMIC_INC(nextnode->ipe_ref);
			token->ipt_data = nextnode;
		} else {
			bzero((char *)&zn, sizeof(zn));
			nextnode = &zn;
			token->ipt_data = NULL;
		}
		hnext = nextnode->ipe_next;
		break;

	default :
		IPFERROR(30010);
		err = EINVAL;
		hnext = NULL;
		break;
	}

	RWLOCK_EXIT(&softc->ipf_poolrw);
	if (err != 0)
		return err;

	switch (ilp->ili_otype)
	{
	case IPFLOOKUPITER_LIST :
		err = COPYOUT(nextiph, ilp->ili_data, sizeof(*nextiph));
		if (err != 0) {
			IPFERROR(30011);
			err = EFAULT;
		}
		if (iph != NULL) {
			WRITE_ENTER(&softc->ipf_poolrw);
			ipf_htable_deref(softc, softh, iph);
			RWLOCK_EXIT(&softc->ipf_poolrw);
		}
		break;

	case IPFLOOKUPITER_NODE :
		err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
		if (err != 0) {
			IPFERROR(30012);
			err = EFAULT;
		}
		if (node != NULL) {
			WRITE_ENTER(&softc->ipf_poolrw);
			ipf_htent_deref(softc, node);
			RWLOCK_EXIT(&softc->ipf_poolrw);
		}
		break;
	}

	if (hnext == NULL)
		ipf_token_mark_complete(token);

	return err;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_htable_iter_deref                                       */
/* Returns:     int      - 0 = success, else  error                         */
/* Parameters:  softc(I) - pointer to soft context main structure           */
/*              arg(I)   - pointer to local context to use                  */
/*              otype(I) - which data structure type is being walked        */
/*              unit(I)  - ipfilter device to which we are working on       */
/*              data(I)  - pointer to old data structure                    */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static int
ipf_htable_iter_deref(softc, arg, otype, unit, data)
	ipf_main_softc_t *softc;
	void *arg;
	int otype;
	int unit;
	void *data;
{

	if (data == NULL)
		return EFAULT;

	if (unit < -1 || unit > IPL_LOGMAX)
		return EINVAL;

	switch (otype)
	{
	case IPFLOOKUPITER_LIST :
		ipf_htable_deref(softc, arg, (iphtable_t *)data);
		break;

	case IPFLOOKUPITER_NODE :
		ipf_htent_deref(arg, (iphtent_t *)data);
		break;
	default :
		break;
	}

	return 0;
}


#ifdef USE_INET6
/* ------------------------------------------------------------------------ */
/* Function:    ipf_iphmfind6                                               */
/* Parameters:  iph(I)  - pointer to hash table                             */
/*              addr(I) - pointer to IPv6 address                           */
/* Locks:  ipf_poolrw                                                       */
/*                                                                          */
/* ------------------------------------------------------------------------ */
static iphtent_t *
ipf_iphmfind6(iph, addr)
	iphtable_t *iph;
	i6addr_t *addr;
{
	i6addr_t *msk, ips;
	iphtent_t *ipe;
	u_int hv;
	int i;

	i = 0;
maskloop:
	msk = iph->iph_v6_masks.imt6_active + i;
	ips.i6[0] = addr->i6[0] & msk->i6[0];
	ips.i6[1] = addr->i6[1] & msk->i6[1];
	ips.i6[2] = addr->i6[2] & msk->i6[2];
	ips.i6[3] = addr->i6[3] & msk->i6[3];
	hv = IPE_V6_HASH_FN(ips.i6, msk->i6, iph->iph_size);
	for (ipe = iph->iph_table[hv]; (ipe != NULL); ipe = ipe->ipe_next) {
		if ((ipe->ipe_family != AF_INET6) ||
		    IP6_NEQ(&ipe->ipe_mask, msk) ||
		    IP6_NEQ(&ipe->ipe_addr, &ips)) {
			continue;
		}
		break;
	}

	if (ipe == NULL) {
		i++;
		if (i < iph->iph_v6_masks.imt6_max)
			goto maskloop;
	}
	return ipe;
}
#endif


static void
ipf_htable_expire(softc, arg)
	ipf_main_softc_t *softc;
	void *arg;
{
	ipf_htable_softc_t *softh = arg;
	iphtent_t *n;

	while ((n = softh->ipf_node_explist) != NULL) {
		if (n->ipe_die > softc->ipf_ticks)
			break;

		ipf_htent_remove(softc, softh, n->ipe_owner, n);
	}
}


#ifndef _KERNEL

/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ------------------------------------------------------------------------ */
void
ipf_htable_dump(softc, arg)
	ipf_main_softc_t *softc;
	void *arg;
{
	ipf_htable_softc_t *softh = arg;
	iphtable_t *iph;
	int i;

	printf("List of configured hash tables\n");
	for (i = 0; i < IPL_LOGSIZE; i++)
		for (iph = softh->ipf_htables[i]; iph != NULL;
		     iph = iph->iph_next)
			printhash(iph, bcopywrap, NULL, opts, NULL);

}
#endif
OpenPOWER on IntegriCloud