Loading...
   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
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
/*
 * Copyright (c) 2021, 2023, 2024 Apple Inc. All rights reserved.
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

/*
 * LOCKING STRATEGY
 *
 * The struct socket's so_flow_db field (struct soflow_db and its hash entries
 * struct soflow_hash_entry) is protected by the socket lock. This covers all the
 * socket paths that calls soflow_get_flow() as well as the garbage collection.
 * For the socket detach path, soflow_detach() cannot assume the socket lock is
 * held. Thus, reference counts are added to both struct soflow_db and struct
 * soflow_hash_entry to avoid access after freed issues.
 *
 * The global list, soflow_entry_head, keeps track of all struct soflow_hash_entry
 * entries which is used by garbage collection when detecting idle entries.  This list
 * is protected by the global lock soflow_lck_rw.
 *
 */

#include <sys/types.h>
#include <sys/kern_control.h>
#include <sys/queue.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/socket.h>
#include <sys/socketvar.h>

#include <kern/sched_prim.h>
#include <kern/locks.h>
#include <kern/zalloc.h>
#include <kern/debug.h>
#include <net/ntstat.h>
#include <netinet6/in6_var.h>

#define _IP_VHL
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>

#include <string.h>
#include <libkern/libkern.h>
#include <kern/socket_flows.h>
#include <net/sockaddr_utils.h>

extern struct inpcbinfo ripcbinfo;

/*
 * Per-Socket Flow Management
 */

static int soflow_log_level = LOG_ERR;
static int soflow_log_port = 0;
static int soflow_log_pid = 0;
static int soflow_log_proto = 0;
static int soflow_nstat_disable = 0;
static int soflow_disable = 0;
static long soflow_attached_count = 0;
static long soflow_attached_high_water_mark = 0;
static os_log_t soflow_log_handle = NULL;

/*
 * Sysctls for debug logs control
 */
SYSCTL_NODE(_net, OID_AUTO, soflow, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "soflow");

SYSCTL_INT(_net_soflow, OID_AUTO, log_level, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_log_level, 0, "");

SYSCTL_INT(_net_soflow, OID_AUTO, log_port, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_log_port, 0, "");

SYSCTL_INT(_net_soflow, OID_AUTO, log_pid, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_log_pid, 0, "");

SYSCTL_INT(_net_soflow, OID_AUTO, log_proto, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_log_proto, 0, "");

SYSCTL_INT(_net_soflow, OID_AUTO, nstat_disable, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_nstat_disable, 0, "");

SYSCTL_INT(_net_soflow, OID_AUTO, disable, CTLFLAG_RW | CTLFLAG_LOCKED,
    &soflow_disable, 0, "");

SYSCTL_LONG(_net_soflow, OID_AUTO, count, CTLFLAG_LOCKED | CTLFLAG_RD, &soflow_attached_count, "");
SYSCTL_LONG(_net_soflow, OID_AUTO, high_water_mark, CTLFLAG_LOCKED | CTLFLAG_RD, &soflow_attached_high_water_mark, "");

#define SOFLOW_LOG(level, so, debug, fmt, ...)                                                                      \
do {                                                                                                                \
    if (soflow_log_level >= level && debug && soflow_log_handle) {                                                  \
	if (level == LOG_ERR) {                                                                                         \
	    os_log_error(soflow_log_handle, "SOFLOW - %s:%d <pid %d so %llx> " fmt "\n", __FUNCTION__, __LINE__,        \
	             so ? SOFLOW_SOCKET_PID(so) : 0, so ? (uint64_t)VM_KERNEL_ADDRPERM(so) : 0, ##__VA_ARGS__);         \
	} else {                                                                                                        \
	    os_log(soflow_log_handle, "SOFLOW - %s:%d <pid %d so %llx> " fmt "\n", __FUNCTION__, __LINE__,              \
	       so ? SOFLOW_SOCKET_PID(so) : 0, so ? (uint64_t)VM_KERNEL_ADDRPERM(so) : 0, ##__VA_ARGS__);               \
	}                                                                                                               \
    }                                                                                                               \
} while (0)

#define SOFLOW_ENTRY_LOG(level, so, entry, debug, msg)                                                              \
do {                                                                                                                \
    if (soflow_log_level >= level && entry && debug) {                                                              \
    soflow_entry_log(level, so, entry, msg);                                                                        \
    }                                                                                                               \
} while (0)

#define SOFLOW_HASH(laddr, faddr, lport, fport) ((faddr) ^ ((laddr) >> 16) ^ (fport) ^ (lport))

#define SOFLOW_IS_UDP(so) (so && SOCK_CHECK_TYPE(so, SOCK_DGRAM) && SOCK_CHECK_PROTO(so, IPPROTO_UDP))
#define SOFLOW_GET_SO_PROTO(so) (so ? SOCK_PROTO(so) : IPPROTO_MAX)

#define SOFLOW_SOCKET_PID(so) ((so->so_flags & SOF_DELEGATED) ? so->e_pid : so->last_pid)

#define SOFLOW_ENABLE_DEBUG(so, entry) \
    ((soflow_log_port == 0 || !entry || soflow_log_port == ntohs(entry->soflow_lport) || soflow_log_port == ntohs(entry->soflow_fport)) && \
     (soflow_log_pid == 0 || !so || soflow_log_pid == SOFLOW_SOCKET_PID(so)) && \
     (soflow_log_proto == 0 || !so || soflow_log_proto == SOFLOW_GET_SO_PROTO(so)))

os_refgrp_decl(static, soflow_refgrp, "soflow_ref_group", NULL);

#define SOFLOW_ENTRY_FREE(entry) \
    if (entry && (os_ref_release(&entry->soflow_ref_count) == 0)) { \
	soflow_entry_free(entry); \
    }

#define SOFLOW_DB_FREE(db) \
    if (db && (os_ref_release(&db->soflow_db_ref_count) == 0)) { \
    soflow_db_free(db); \
    }

static int soflow_initialized = 0;

TAILQ_HEAD(soflow_entry_head, soflow_hash_entry) soflow_entry_head;
static LCK_GRP_DECLARE(soflow_lck_grp, "Socket Flow");
static LCK_RW_DECLARE(soflow_lck_rw, &soflow_lck_grp);

#define SOFLOW_LOCK_EXCLUSIVE lck_rw_lock_exclusive(&soflow_lck_rw)
#define SOFLOW_UNLOCK_EXCLUSIVE lck_rw_unlock_exclusive(&soflow_lck_rw)
#define SOFLOW_LOCK_SHARED lck_rw_lock_shared(&soflow_lck_rw)
#define SOFLOW_UNLOCK_SHARED lck_rw_unlock_shared(&soflow_lck_rw)

/*
 * Flow Garbage Collection:
 */
static struct thread *soflow_gc_thread;
static soflow_feat_gc_needed_func soflow_feat_gc_needed_func_ptr = NULL;
static soflow_feat_gc_perform_func soflow_feat_gc_perform_func_ptr = NULL;

#define SOFLOW_GC_IDLE_TO            30  // Flow Idle Timeout in seconds
#define SOFLOW_GC_MAX_COUNT          100 // Max sockets to be handled per run
#define SOFLOW_GC_RUN_INTERVAL_NSEC  (10 * NSEC_PER_SEC)  // GC wakes up every 10 seconds

/*
 * Feature Context Handling:
 */
static soflow_feat_detach_entry_func soflow_feat_detach_entry_func_ptr = NULL;
static soflow_feat_detach_db_func soflow_feat_detach_db_func_ptr = NULL;

static void soflow_gc_thread_func(void *v, wait_result_t w);
static void soflow_gc_expire(void *v, wait_result_t w);
static boolean_t soflow_entry_local_address_needs_update(struct soflow_hash_entry *);
static boolean_t soflow_entry_local_port_needs_update(struct socket *, struct soflow_hash_entry *);

static void
soflow_init(void)
{
	if (soflow_initialized) {
		return;
	}
	soflow_initialized = 1;

	if (soflow_log_handle == NULL) {
		soflow_log_handle = os_log_create("com.apple.xnu.net.soflow", "soflow");
	}

	TAILQ_INIT(&soflow_entry_head);

	// Spawn thread for gargage collection
	if (kernel_thread_start(soflow_gc_thread_func, NULL,
	    &soflow_gc_thread) != KERN_SUCCESS) {
		panic_plain("%s: Can't create SOFLOW GC thread", __func__);
		/* NOTREACHED */
	}
	/* this must not fail */
	VERIFY(soflow_gc_thread != NULL);
}

static void
soflow_entry_log(int level, struct socket *so, struct soflow_hash_entry *entry, const char* msg)
{
#pragma unused(level, msg)
	char local[MAX_IPv6_STR_LEN + 6] = { 0 };
	char remote[MAX_IPv6_STR_LEN + 6] = { 0 };
	const void  *addr;

	// No sock or not UDP, no-op
	if (entry == NULL) {
		return;
	}

	switch (entry->soflow_family) {
	case AF_INET6:
		addr = &entry->soflow_laddr.addr6;
		inet_ntop(AF_INET6, addr, local, sizeof(local));
		addr = &entry->soflow_faddr.addr6;
		inet_ntop(AF_INET6, addr, remote, sizeof(local));
		break;
	case AF_INET:
		addr = &entry->soflow_laddr.addr46.ia46_addr4.s_addr;
		inet_ntop(AF_INET, addr, local, sizeof(local));
		addr = &entry->soflow_faddr.addr46.ia46_addr4.s_addr;
		inet_ntop(AF_INET, addr, remote, sizeof(local));
		break;
	default:
		return;
	}

	SOFLOW_LOG(level, so, entry->soflow_debug, "<%s>: %s <%s(%d) entry %p, featureID %llu, filter_ctl 0x%x> outifp %d lport %d fport %d laddr %s faddr %s hash %X "
	    "<rx p %llu b %llu, tx p %llu b %llu>",
	    msg, entry->soflow_outgoing ? "OUT" : "IN ",
	    SOFLOW_IS_UDP(so) ? "UDP" : "proto", SOFLOW_GET_SO_PROTO(so),
	    entry, entry->soflow_feat_ctxt_id,
	    entry->soflow_filter_control_unit,
	    entry->soflow_outifindex,
	    ntohs(entry->soflow_lport), ntohs(entry->soflow_fport), local, remote,
	    entry->soflow_flowhash,
	    entry->soflow_rxpackets, entry->soflow_rxbytes, entry->soflow_txpackets, entry->soflow_txbytes);
}

bool
soflow_fill_hash_entry_from_address(struct soflow_hash_entry *entry, bool isLocal, struct sockaddr *addr, bool islocalUpdate)
{
	struct sockaddr_in *sin = NULL;
	struct sockaddr_in6 *sin6 = NULL;

	if (entry == NULL || addr == NULL) {
		return FALSE;
	}

	switch (addr->sa_family) {
	case AF_INET:
		sin = satosin(addr);
		if (sin->sin_len < sizeof(*sin)) {
			return FALSE;
		}
		if (isLocal == TRUE) {
			if (sin->sin_port != 0) {
				entry->soflow_lport = sin->sin_port;
				if (islocalUpdate) {
					entry->soflow_lport_updated = TRUE;
				}
			}
			if (sin->sin_addr.s_addr != INADDR_ANY) {
				entry->soflow_laddr.addr46.ia46_addr4.s_addr = sin->sin_addr.s_addr;
				if (islocalUpdate) {
					entry->soflow_laddr_updated = TRUE;
				}
			}
		} else {
			if (sin->sin_port != 0) {
				entry->soflow_fport = sin->sin_port;
			}
			if (sin->sin_addr.s_addr != INADDR_ANY) {
				entry->soflow_faddr.addr46.ia46_addr4.s_addr = sin->sin_addr.s_addr;
			}
		}
		entry->soflow_family = AF_INET;
		return TRUE;
	case AF_INET6:
		sin6 = satosin6(addr);
		if (sin6->sin6_len < sizeof(*sin6)) {
			return FALSE;
		}
		if (isLocal == TRUE) {
			if (sin6->sin6_port != 0) {
				entry->soflow_lport = sin6->sin6_port;
				if (islocalUpdate) {
					entry->soflow_lport_updated = TRUE;
				}
			}
			if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
				entry->soflow_laddr.addr6 = sin6->sin6_addr;
				entry->soflow_laddr6_ifscope = sin6->sin6_scope_id;
				in6_verify_ifscope(&sin6->sin6_addr, sin6->sin6_scope_id);
				if (islocalUpdate) {
					entry->soflow_laddr_updated = TRUE;
				}
			}
		} else {
			if (sin6->sin6_port != 0) {
				entry->soflow_fport = sin6->sin6_port;
			}
			if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
				entry->soflow_faddr.addr6 = sin6->sin6_addr;
				entry->soflow_faddr6_ifscope = sin6->sin6_scope_id;
				in6_verify_ifscope(&sin6->sin6_addr, sin6->sin6_scope_id);
			}
		}
		entry->soflow_family = AF_INET6;
		return TRUE;
	default:
		return FALSE;
	}
}

bool
soflow_fill_hash_entry_from_inp(struct soflow_hash_entry *entry, bool isLocal, struct inpcb *inp, bool islocalUpdate)
{
	if (entry == NULL || inp == NULL) {
		return FALSE;
	}

	if (inp->inp_vflag & INP_IPV6) {
		if (isLocal == TRUE) {
			if (inp->inp_lport) {
				entry->soflow_lport = inp->inp_lport;
				if (islocalUpdate) {
					entry->soflow_lport_updated = TRUE;
				}
			}
			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
				entry->soflow_laddr.addr6 = inp->in6p_laddr;
				entry->soflow_laddr6_ifscope = inp->inp_lifscope;
				in6_verify_ifscope(&entry->soflow_laddr.addr6, inp->inp_lifscope);
				if (islocalUpdate) {
					entry->soflow_laddr_updated = TRUE;
				}
			}
		} else {
			if (inp->inp_fport) {
				entry->soflow_fport = inp->inp_fport;
			}
			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
				entry->soflow_faddr.addr6 = inp->in6p_faddr;
				entry->soflow_faddr6_ifscope = inp->inp_fifscope;
				in6_verify_ifscope(&entry->soflow_faddr.addr6, inp->inp_fifscope);
			}
		}
		entry->soflow_family = AF_INET6;
		return TRUE;
	} else if (inp->inp_vflag & INP_IPV4) {
		if (isLocal == TRUE) {
			if (inp->inp_lport) {
				entry->soflow_lport = inp->inp_lport;
				if (islocalUpdate) {
					entry->soflow_lport_updated = TRUE;
				}
			}
			if (inp->inp_laddr.s_addr) {
				entry->soflow_laddr.addr46.ia46_addr4.s_addr = inp->inp_laddr.s_addr;
				if (islocalUpdate) {
					entry->soflow_laddr_updated = TRUE;
				}
			}
		} else {
			if (inp->inp_fport) {
				entry->soflow_fport = inp->inp_fport;
			}
			if (inp->inp_faddr.s_addr) {
				entry->soflow_faddr.addr46.ia46_addr4.s_addr = inp->inp_faddr.s_addr;
			}
		}
		entry->soflow_family = AF_INET;
		return TRUE;
	}
	return FALSE;
}

static errno_t
soflow_db_init(struct socket *so)
{
	errno_t error = 0;
	struct soflow_db * __single db = NULL;
	struct soflow_hash_entry *hash_entry = NULL;

	db = kalloc_type(struct soflow_db, Z_WAITOK | Z_ZERO | Z_NOFAIL);
	db->soflow_db_so = so;
	void * __single hash = hashinit(SOFLOW_HASH_SIZE, M_CFIL, &db->soflow_db_hashmask);
	db->soflow_db_hashbase = __unsafe_forge_bidi_indexable(struct soflow_hash_head *, hash, SOFLOW_HASH_SIZE * sizeof(void*));
	if (db->soflow_db_hashbase == NULL) {
		kfree_type(struct soflow_db, db);
		error = ENOMEM;
		goto done;
	}
	db->soflow_db_debug = SOFLOW_ENABLE_DEBUG(so, hash_entry);
	os_ref_init(&db->soflow_db_ref_count, &soflow_refgrp);
	so->so_flow_db = db;
done:
	return error;
}

static void
soflow_entry_free(struct soflow_hash_entry *hash_entry)
{
	struct socket *so = (hash_entry && hash_entry->soflow_db) ? hash_entry->soflow_db->soflow_db_so : NULL;

	if (hash_entry == NULL) {
		return;
	}

	SOFLOW_ENTRY_LOG(LOG_INFO, so, hash_entry, hash_entry->soflow_debug, "Free entry");
	kfree_type(struct soflow_hash_entry, hash_entry);
}

static void
soflow_db_remove_entry(struct soflow_db *db, struct soflow_hash_entry *hash_entry)
{
	if (hash_entry == NULL) {
		return;
	}
	if (db == NULL || db->soflow_db_count == 0) {
		return;
	}

#if defined(NSTAT_EXTENSION_FILTER_DOMAIN_INFO)
	if (hash_entry->soflow_nstat_context != NULL) {
		SOFLOW_LOG(LOG_INFO, db->soflow_db_so, hash_entry->soflow_debug, "<Close nstat> - context %lX", (unsigned long)hash_entry->soflow_nstat_context);
		nstat_provider_stats_close(hash_entry->soflow_nstat_context);
		hash_entry->soflow_nstat_context = NULL;
		SOFLOW_ENTRY_FREE(hash_entry);
	}
#endif

	db->soflow_db_count--;
	if (db->soflow_db_only_entry == hash_entry) {
		db->soflow_db_only_entry = NULL;
	}
	LIST_REMOVE(hash_entry, soflow_entry_link);

	// Feature context present, give feature a chance to detach and clean up
	if (hash_entry->soflow_feat_ctxt != NULL && soflow_feat_detach_entry_func_ptr != NULL) {
		soflow_feat_detach_entry_func_ptr(db->soflow_db_so, hash_entry);
		hash_entry->soflow_feat_ctxt = NULL;
		hash_entry->soflow_feat_ctxt_id = 0;
	}

	hash_entry->soflow_db = NULL;

	SOFLOW_LOCK_EXCLUSIVE;
	if (soflow_initialized) {
		TAILQ_REMOVE(&soflow_entry_head, hash_entry, soflow_entry_list_link);
		soflow_attached_count--;
	}
	SOFLOW_UNLOCK_EXCLUSIVE;

	SOFLOW_ENTRY_FREE(hash_entry);
}

static void
soflow_db_free(struct soflow_db *db)
{
	struct soflow_hash_entry *entry = NULL;
	struct soflow_hash_entry *temp_entry = NULL;
	struct soflow_hash_head *flowhash = NULL;

	if (db == NULL) {
		return;
	}

	SOFLOW_LOG(LOG_INFO, db->soflow_db_so, db->soflow_db_debug, "<db %p> freeing db (count == %d)", db, db->soflow_db_count);

	for (int i = 0; i < SOFLOW_HASH_SIZE; i++) {
		flowhash = &db->soflow_db_hashbase[i];
		LIST_FOREACH_SAFE(entry, flowhash, soflow_entry_link, temp_entry) {
			SOFLOW_ENTRY_LOG(LOG_INFO, db->soflow_db_so, entry, entry->soflow_debug, "Remove entry");
			soflow_db_remove_entry(db, entry);
		}
	}

	if (soflow_feat_detach_db_func_ptr != NULL) {
		soflow_feat_detach_db_func_ptr(db->soflow_db_so, db);
	}

	// Make sure all entries are cleaned up!
	VERIFY(db->soflow_db_count == 0);
	hashdestroy(db->soflow_db_hashbase, M_CFIL, db->soflow_db_hashmask);
	kfree_type(struct soflow_db, db);
}

void
soflow_detach(struct socket *so)
{
	if (so == NULL || so->so_flow_db == NULL) {
		return;
	}
	SOFLOW_DB_FREE(so->so_flow_db);
	so->so_flow_db = NULL;
}

static boolean_t
soflow_match_entries_v4(struct soflow_hash_entry *entry1, struct soflow_hash_entry *entry2, boolean_t remoteOnly)
{
	if (entry1 == NULL || entry2 == NULL) {
		return false;
	}

	// Ignore local match if remoteOnly or if local has been updated since entry added
	boolean_t lport_matched = (remoteOnly || entry1->soflow_lport_updated || entry1->soflow_lport == entry2->soflow_lport);
	boolean_t laddr_matched = (remoteOnly || entry1->soflow_laddr_updated ||
	    entry1->soflow_laddr.addr46.ia46_addr4.s_addr == entry2->soflow_laddr.addr46.ia46_addr4.s_addr);

	// Entries match if local and remote ports and addresses all matched
	return lport_matched && entry1->soflow_fport == entry2->soflow_fport &&
	       laddr_matched && entry1->soflow_faddr.addr46.ia46_addr4.s_addr == entry2->soflow_faddr.addr46.ia46_addr4.s_addr;
}

static boolean_t
soflow_match_entries_v6(struct soflow_hash_entry *entry1, struct soflow_hash_entry *entry2, boolean_t remoteOnly)
{
	if (entry1 == NULL || entry2 == NULL) {
		return false;
	}

	// Ignore local match if remoteOnly or if local has been updated since entry added
	boolean_t lport_matched = (remoteOnly || entry1->soflow_lport_updated || entry1->soflow_lport == entry2->soflow_lport);
	boolean_t laddr_matched = (remoteOnly || entry1->soflow_laddr_updated ||
	    in6_are_addr_equal_scoped(&entry1->soflow_laddr.addr6, &entry2->soflow_laddr.addr6, entry1->soflow_laddr6_ifscope, entry2->soflow_laddr6_ifscope));

	// Entries match if local and remote ports and addresses all matched
	return lport_matched && entry1->soflow_fport == entry2->soflow_fport &&
	       laddr_matched && in6_are_addr_equal_scoped(&entry1->soflow_faddr.addr6, &entry2->soflow_faddr.addr6, entry1->soflow_faddr6_ifscope, entry2->soflow_faddr6_ifscope);
}

static struct soflow_hash_entry *
soflow_db_lookup_entry_internal(struct soflow_db *db, struct sockaddr *local, struct sockaddr *remote, boolean_t remoteOnly, boolean_t withLocalPort)
{
	struct soflow_hash_entry matchentry = { };
	struct soflow_hash_entry *nextentry = NULL;
	struct inpcb *inp = sotoinpcb(db->soflow_db_so);
	u_int32_t hashkey_faddr = 0, hashkey_laddr = 0;
	u_int16_t hashkey_fport = 0, hashkey_lport = 0;
	int inp_hash_element = 0;
	struct soflow_hash_head *flowhash = NULL;

	if (inp == NULL || db == NULL) {
		return NULL;
	}

	if (local != NULL) {
		soflow_fill_hash_entry_from_address(&matchentry, TRUE, local, FALSE);
	} else {
		soflow_fill_hash_entry_from_inp(&matchentry, TRUE, inp, FALSE);
	}
	if (remote != NULL) {
		soflow_fill_hash_entry_from_address(&matchentry, FALSE, remote, FALSE);
	} else {
		soflow_fill_hash_entry_from_inp(&matchentry, FALSE, inp, FALSE);
	}
	matchentry.soflow_debug = SOFLOW_ENABLE_DEBUG(db->soflow_db_so, (&matchentry));
	SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, &matchentry, true, "Looking for entry");

	if (inp->inp_vflag & INP_IPV6) {
		hashkey_faddr = matchentry.soflow_faddr.addr6.s6_addr32[3];
		hashkey_laddr = (remoteOnly == false) ? matchentry.soflow_laddr.addr6.s6_addr32[3] : 0;
	} else {
		hashkey_faddr = matchentry.soflow_faddr.addr46.ia46_addr4.s_addr;
		hashkey_laddr = (remoteOnly == false) ? matchentry.soflow_laddr.addr46.ia46_addr4.s_addr : 0;
	}

	hashkey_fport = matchentry.soflow_fport;
	hashkey_lport = (remoteOnly == false || withLocalPort == true) ? matchentry.soflow_lport : 0;

	inp_hash_element = SOFLOW_HASH(hashkey_laddr, hashkey_faddr, hashkey_lport, hashkey_fport);
	inp_hash_element &= db->soflow_db_hashmask;
	flowhash = &db->soflow_db_hashbase[inp_hash_element];

	LIST_FOREACH(nextentry, flowhash, soflow_entry_link) {
		if (inp->inp_vflag & INP_IPV6) {
			if (soflow_match_entries_v6(nextentry, &matchentry, remoteOnly)) {
				SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, nextentry, nextentry->soflow_debug, "Found entry v6");
				break;
			}
		} else if (inp->inp_vflag & INP_IPV4) {
			if (soflow_match_entries_v4(nextentry, &matchentry, remoteOnly)) {
				SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, nextentry, nextentry->soflow_debug, "Found entry v4");
				break;
			}
		}
	}

	if (nextentry == NULL) {
		SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, &matchentry, matchentry.soflow_debug, "Entry not found");
	}
	return nextentry;
}

static struct soflow_hash_entry *
soflow_db_lookup_entry(struct soflow_db *db, struct sockaddr *local, struct sockaddr *remote, boolean_t remoteOnly)
{
	struct soflow_hash_entry *entry = soflow_db_lookup_entry_internal(db, local, remote, remoteOnly, false);
	if (entry == NULL && remoteOnly == true) {
		entry = soflow_db_lookup_entry_internal(db, local, remote, remoteOnly, true);
	}
	return entry;
}

static struct soflow_hash_entry *
soflow_db_lookup_by_feature_context_id(struct soflow_db *db, u_int64_t feature_context_id)
{
	struct soflow_hash_head *flowhash = NULL;
	u_int32_t inp_hash_element = (u_int32_t)(feature_context_id & 0x0ffffffff);
	struct soflow_hash_entry *nextentry;

	inp_hash_element &= db->soflow_db_hashmask;
	flowhash = &db->soflow_db_hashbase[inp_hash_element];

	LIST_FOREACH(nextentry, flowhash, soflow_entry_link) {
		SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, nextentry, nextentry->soflow_debug, "Looking at entry");
		if (nextentry->soflow_feat_ctxt != NULL &&
		    nextentry->soflow_feat_ctxt_id == feature_context_id) {
			SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, nextentry, nextentry->soflow_debug, "Found entry by feature context id");
			break;
		}
	}

	if (nextentry == NULL) {
		SOFLOW_LOG(LOG_DEBUG, db->soflow_db_so, db->soflow_db_debug, "No entry found for featureID %llu <count %d hash %X %X>",
		    feature_context_id, db->soflow_db_count, inp_hash_element, (u_int32_t)(feature_context_id & 0x0ffffffff));
	}
	return nextentry;
}

void *
soflow_db_get_feature_context(struct soflow_db *db, u_int64_t feature_context_id)
{
	struct soflow_hash_entry *hash_entry = NULL;
	void * __single context = NULL;

	if (db == NULL || db->soflow_db_so == NULL || feature_context_id == 0) {
		return NULL;
	}

	socket_lock_assert_owned(db->soflow_db_so);

	// Take refcount of db before use.
	// Abort if db is already being freed.
	if (os_ref_retain_try(&db->soflow_db_ref_count) == false) {
		return NULL;
	}

	// This is an optimization for datagram sockets with only one single flow.
	if (db->soflow_db_count == 1) {
		if (db->soflow_db_only_entry != NULL &&
		    db->soflow_db_only_entry->soflow_feat_ctxt != NULL && db->soflow_db_only_entry->soflow_feat_ctxt_id == feature_context_id) {
			SOFLOW_ENTRY_LOG(LOG_DEBUG, db->soflow_db_so, db->soflow_db_only_entry, db->soflow_db_only_entry->soflow_debug, "MATCHED only entry for featureID");
			context = db->soflow_db_only_entry->soflow_feat_ctxt;
		} else {
			SOFLOW_LOG(LOG_DEBUG, db->soflow_db_so, db->soflow_db_debug, "MISMATCHED only entry for featureID %llu (entry %p - cfil %p id %llu)",
			    feature_context_id,
			    db->soflow_db_only_entry,
			    db->soflow_db_only_entry ? db->soflow_db_only_entry->soflow_feat_ctxt : NULL,
			    db->soflow_db_only_entry ? db->soflow_db_only_entry->soflow_feat_ctxt_id : 0);
		}
	} else {
		hash_entry = soflow_db_lookup_by_feature_context_id(db, feature_context_id);
		context = hash_entry != NULL ? hash_entry->soflow_feat_ctxt : NULL;
	}

	SOFLOW_DB_FREE(db);
	return context;
}

u_int64_t
soflow_db_get_feature_context_id(struct soflow_db *db, struct sockaddr *local, struct sockaddr *remote)
{
	struct soflow_hash_entry *hash_entry = NULL;
	uint64_t context_id = 0;

	if (db == NULL || db->soflow_db_so == NULL) {
		return 0;
	}

	socket_lock_assert_owned(db->soflow_db_so);

	// Take refcount of db before use.
	// Abort if db is already being freed.
	if (os_ref_retain_try(&db->soflow_db_ref_count) == false) {
		return 0;
	}

	hash_entry = soflow_db_lookup_entry(db, local, remote, false);
	if (hash_entry == NULL) {
		// No match with both local and remote, try match with remote only
		hash_entry = soflow_db_lookup_entry(db, local, remote, true);
	}
	if (hash_entry != NULL && hash_entry->soflow_feat_ctxt != NULL) {
		context_id = hash_entry->soflow_feat_ctxt_id;
	}

	SOFLOW_DB_FREE(db);

	return context_id;
}

static struct soflow_hash_entry *
soflow_db_add_entry(struct soflow_db *db, struct sockaddr *local, struct sockaddr *remote)
{
	struct soflow_hash_entry *entry = NULL;
	struct inpcb *inp = db ? sotoinpcb(db->soflow_db_so) : NULL;
	u_int32_t hashkey_faddr = 0, hashkey_laddr = 0;
	int inp_hash_element = 0;
	struct soflow_hash_head *flowhash = NULL;

	if (db == NULL || inp == NULL) {
		goto done;
	}

	entry = kalloc_type(struct soflow_hash_entry, Z_WAITOK | Z_ZERO | Z_NOFAIL);
	os_ref_init(&entry->soflow_ref_count, &soflow_refgrp);

	if (local != NULL) {
		soflow_fill_hash_entry_from_address(entry, TRUE, local, FALSE);
	} else {
		soflow_fill_hash_entry_from_inp(entry, TRUE, inp, FALSE);
	}
	if (remote != NULL) {
		soflow_fill_hash_entry_from_address(entry, FALSE, remote, FALSE);
	} else {
		soflow_fill_hash_entry_from_inp(entry, FALSE, inp, FALSE);
	}
	entry->soflow_lastused = net_uptime();
	entry->soflow_db = db;
	entry->soflow_debug = SOFLOW_ENABLE_DEBUG(db->soflow_db_so, entry);
	microuptime(&entry->soflow_timestamp);

	if (inp->inp_vflag & INP_IPV6) {
		hashkey_faddr = entry->soflow_faddr.addr6.s6_addr32[3];
		hashkey_laddr = entry->soflow_laddr.addr6.s6_addr32[3];
	} else {
		hashkey_faddr = entry->soflow_faddr.addr46.ia46_addr4.s_addr;
		hashkey_laddr = entry->soflow_laddr.addr46.ia46_addr4.s_addr;
	}
	entry->soflow_flowhash = SOFLOW_HASH(hashkey_laddr, hashkey_faddr,
	    entry->soflow_lport, entry->soflow_fport);
	inp_hash_element = entry->soflow_flowhash & db->soflow_db_hashmask;

	socket_lock_assert_owned(db->soflow_db_so);

	// Take refcount of db before use.
	// Abort if db is already being freed.
	if (os_ref_retain_try(&db->soflow_db_ref_count) == false) {
		return NULL;
	}

	flowhash = &db->soflow_db_hashbase[inp_hash_element];

	LIST_INSERT_HEAD(flowhash, entry, soflow_entry_link);
	db->soflow_db_count++;
	db->soflow_db_only_entry = entry;
	SOFLOW_LOG(LOG_INFO, db->soflow_db_so, db->soflow_db_debug, "total count %d", db->soflow_db_count);

	SOFLOW_DB_FREE(db);

done:
	return entry;
}

static boolean_t
soflow_udp_get_address_from_control(sa_family_t family, struct mbuf *control, uint8_t *__counted_by(*count) *address_ptr, int *count)
{
	struct cmsghdr *cm;
	struct in6_pktinfo *pi6;
	struct socket *so = NULL;

	if (control == NULL || address_ptr == NULL) {
		return false;
	}

	for (; control != NULL; control = control->m_next) {
		if (control->m_type != MT_CONTROL) {
			continue;
		}

		for (cm = M_FIRST_CMSGHDR(control);
		    is_cmsg_valid(control, cm);
		    cm = M_NXT_CMSGHDR(control, cm)) {
			SOFLOW_LOG(LOG_DEBUG, so, true, "Check control type %d", cm->cmsg_type);

			switch (cm->cmsg_type) {
			case IP_RECVDSTADDR:
				if (family == AF_INET &&
				    cm->cmsg_level == IPPROTO_IP &&
				    cm->cmsg_len == CMSG_LEN(sizeof(struct in_addr))) {
					*address_ptr = CMSG_DATA(cm);
					*count = sizeof(struct in_addr);
					return true;
				}
				break;
			case IPV6_PKTINFO:
			case IPV6_2292PKTINFO:
				if (family == AF_INET6 &&
				    cm->cmsg_level == IPPROTO_IPV6 &&
				    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
					pi6 = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
					*address_ptr = (uint8_t *)&pi6->ipi6_addr;
					*count = sizeof(struct in6_addr);
					return true;
				}
				break;
			default:
				break;
			}
		}
	}
	return false;
}

static boolean_t
soflow_entry_local_address_needs_update(struct soflow_hash_entry *entry)
{
	if (entry->soflow_family == AF_INET6) {
		return IN6_IS_ADDR_UNSPECIFIED(&entry->soflow_laddr.addr6);
	} else if (entry->soflow_family == AF_INET) {
		return entry->soflow_laddr.addr46.ia46_addr4.s_addr == INADDR_ANY;
	}
	return false;
}

static boolean_t
soflow_entry_local_port_needs_update(struct socket *so, struct soflow_hash_entry *entry)
{
	if (SOFLOW_IS_UDP(so)) {
		return entry->soflow_lport == 0;
	}
	return false;
}

static void
soflow_entry_update_local(struct soflow_db *db, struct soflow_hash_entry *entry, struct sockaddr *local, struct mbuf *control, u_short rcv_ifindex)
{
	struct inpcb *inp = sotoinpcb(db->soflow_db_so);
	union sockaddr_in_4_6 address_buf = { };

	if (inp == NULL || entry == NULL) {
		return;
	}

	if (entry->soflow_outifindex == 0 && (inp->inp_last_outifp != NULL || rcv_ifindex != 0)) {
		entry->soflow_outifindex = inp->inp_last_outifp ? inp->inp_last_outifp->if_index : rcv_ifindex;
		SOFLOW_ENTRY_LOG(LOG_INFO, db->soflow_db_so, entry, entry->soflow_debug, "Updated outifp");
	}

	if (soflow_entry_local_address_needs_update(entry)) {
		// Flow does not have a local address yet.  Retrieve local address
		// from control mbufs if present.
		if (local == NULL && control != NULL) {
			int size = 0;
			uint8_t * __counted_by(size) addr_ptr = NULL;
			boolean_t result = soflow_udp_get_address_from_control(entry->soflow_family, control, &addr_ptr, &size);

			if (result && size && addr_ptr) {
				switch (entry->soflow_family) {
				case AF_INET:
					if (size == sizeof(struct in_addr)) {
						address_buf.sin.sin_port = 0;
						address_buf.sin.sin_family = AF_INET;
						address_buf.sin.sin_len = sizeof(struct sockaddr_in);
						(void) memcpy(&address_buf.sin.sin_addr, addr_ptr, sizeof(struct in_addr));
						local = sintosa(&address_buf.sin);
					}
					break;
				case AF_INET6:
					if (size == sizeof(struct in6_addr)) {
						address_buf.sin6.sin6_port = 0;
						address_buf.sin6.sin6_family = AF_INET6;
						address_buf.sin6.sin6_len = sizeof(struct sockaddr_in6);
						(void) memcpy(&address_buf.sin6.sin6_addr, addr_ptr, sizeof(struct in6_addr));
						local = sin6tosa(&address_buf.sin6);
					}
					break;
				default:
					break;
				}
			}
		}
		if (local != NULL) {
			soflow_fill_hash_entry_from_address(entry, TRUE, local, TRUE);
		} else {
			soflow_fill_hash_entry_from_inp(entry, TRUE, inp, TRUE);
		}
		if (entry->soflow_laddr_updated) {
			SOFLOW_ENTRY_LOG(LOG_INFO, db->soflow_db_so, entry, entry->soflow_debug, "Updated address");
		}
	}

	if (soflow_entry_local_port_needs_update(db->soflow_db_so, entry)) {
		soflow_fill_hash_entry_from_inp(entry, TRUE, inp, TRUE);
		if (entry->soflow_lport_updated) {
			SOFLOW_ENTRY_LOG(LOG_INFO, db->soflow_db_so, entry, entry->soflow_debug, "Updated port");
		}
	}

	return;
}

#if defined(NSTAT_EXTENSION_FILTER_DOMAIN_INFO)
static u_int32_t
ifnet_to_flags(struct ifnet *ifp, struct socket *so)
{
	u_int32_t flags = 0;

	if (ifp != NULL) {
		flags = nstat_ifnet_to_flags(ifp);
		if ((flags & NSTAT_IFNET_IS_WIFI) && ((flags & (NSTAT_IFNET_IS_AWDL | NSTAT_IFNET_IS_LLW)) == 0)) {
			flags |= NSTAT_IFNET_IS_WIFI_INFRA;
		}
	} else {
		flags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
	}

	if (so != NULL && (so->so_flags1 & SOF1_CELLFALLBACK)) {
		flags |= NSTAT_IFNET_VIA_CELLFALLBACK;
	}
	return flags;
}

static bool
soflow_nstat_provider_request_vals(nstat_provider_context ctx,
    u_int32_t *ifflagsp,
    nstat_counts *countsp,
    void *metadatap)
{
	struct soflow_hash_entry *hash_entry = (struct soflow_hash_entry *) ctx;
	struct socket *so = (hash_entry && hash_entry->soflow_db) ? hash_entry->soflow_db->soflow_db_so : NULL;
	struct inpcb *inp = so ? sotoinpcb(so) : NULL;
	char local[MAX_IPv6_STR_LEN + 6] = { 0 };
	char remote[MAX_IPv6_STR_LEN + 6] = { 0 };
	const void *addr = NULL;

	if (hash_entry == NULL || so == NULL || inp == NULL) {
		return false;
	}

	if (ifflagsp) {
		if (hash_entry->soflow_outifindex) {
			struct ifnet *ifp = ifindex2ifnet[hash_entry->soflow_outifindex];
			*ifflagsp = ifnet_to_flags(ifp, so);
		}
		if ((countsp == NULL) && (metadatap == NULL)) {
			SOFLOW_LOG(LOG_DEBUG, so, hash_entry->soflow_debug, "ifflagsp set to 0x%X", *ifflagsp);
			goto done;
		}
	}

	if (countsp) {
		bzero(countsp, sizeof(*countsp));
		countsp->nstat_rxpackets = hash_entry->soflow_rxpackets;
		countsp->nstat_rxbytes = hash_entry->soflow_rxbytes;
		countsp->nstat_txpackets = hash_entry->soflow_txpackets;
		countsp->nstat_txbytes = hash_entry->soflow_txbytes;

		SOFLOW_LOG(LOG_DEBUG, so, hash_entry->soflow_debug,
		    "Collected NSTAT counts: rxpackets %llu rxbytes %llu txpackets %llu txbytes %llu",
		    countsp->nstat_rxpackets, countsp->nstat_rxbytes, countsp->nstat_txpackets, countsp->nstat_txbytes);
	}

	if (metadatap) {
		nstat_udp_descriptor *desc = (nstat_udp_descriptor *)metadatap;
		bzero(desc, sizeof(*desc));

		if (so->so_flags & SOF_DELEGATED) {
			desc->eupid = so->e_upid;
			desc->epid = so->e_pid;
			uuid_copy(desc->euuid, so->e_uuid);
		} else {
			desc->eupid = so->last_upid;
			desc->epid = so->last_pid;
			uuid_copy(desc->euuid, so->last_uuid);
		}

		uuid_copy(desc->vuuid, so->so_vuuid);
		uuid_copy(desc->fuuid, hash_entry->soflow_uuid);

		if (hash_entry->soflow_family == AF_INET6) {
			in6_ip6_to_sockaddr(&hash_entry->soflow_laddr.addr6, hash_entry->soflow_lport, hash_entry->soflow_laddr6_ifscope,
			    &desc->local.v6, sizeof(desc->local.v6));
			in6_ip6_to_sockaddr(&hash_entry->soflow_faddr.addr6, hash_entry->soflow_fport, hash_entry->soflow_faddr6_ifscope,
			    &desc->remote.v6, sizeof(desc->remote.v6));
		} else if (hash_entry->soflow_family == AF_INET) {
			desc->local.v4.sin_family = AF_INET;
			desc->local.v4.sin_len = sizeof(struct sockaddr_in);
			desc->local.v4.sin_port = hash_entry->soflow_lport;
			desc->local.v4.sin_addr = hash_entry->soflow_laddr.addr46.ia46_addr4;

			desc->remote.v4.sin_family = AF_INET;
			desc->remote.v4.sin_len = sizeof(struct sockaddr_in);
			desc->remote.v4.sin_port = hash_entry->soflow_fport;
			desc->remote.v4.sin_addr = hash_entry->soflow_faddr.addr46.ia46_addr4;
		}

		desc->ifindex = hash_entry->soflow_outifindex;
		if (hash_entry->soflow_outifindex) {
			struct ifnet *ifp = ifindex2ifnet[hash_entry->soflow_outifindex];
			desc->ifnet_properties = (uint16_t)ifnet_to_flags(ifp, so);
		}

		desc->rcvbufsize = so->so_rcv.sb_hiwat;
		desc->rcvbufused = so->so_rcv.sb_cc;
		desc->traffic_class = so->so_traffic_class;
		inp_get_activity_bitmap(inp, &desc->activity_bitmap);

		if (hash_entry->soflow_debug) {
			switch (hash_entry->soflow_family) {
			case AF_INET6:
				addr = &desc->local.v6;
				inet_ntop(AF_INET6, addr, local, sizeof(local));
				addr = &desc->remote.v6;
				inet_ntop(AF_INET6, addr, remote, sizeof(local));
				break;
			case AF_INET:
				addr = &desc->local.v4.sin_addr;
				inet_ntop(AF_INET, addr, local, sizeof(local));
				addr = &desc->remote.v4.sin_addr;
				inet_ntop(AF_INET, addr, remote, sizeof(local));
				break;
			default:
				break;
			}

			uint8_t *ptr = (uint8_t *)&desc->euuid;

			SOFLOW_LOG(LOG_DEBUG, so, hash_entry->soflow_debug,
			    "Collected NSTAT metadata: eupid %llu epid %d euuid %x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x "
			    "outifp %d properties 0x%X lport %d fport %d laddr %s faddr %s "
			    "rcvbufsize %u rcvbufused %u traffic_class %u",
			    desc->eupid, desc->epid,
			    ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
			    ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15],
			    desc->ifindex, desc->ifnet_properties,
			    ntohs(desc->local.v4.sin_port), ntohs(desc->remote.v4.sin_port), local, remote,
			    desc->rcvbufsize, desc->rcvbufused, desc->traffic_class);
		}
	}
done:
	return true;
}

static size_t
soflow_nstat_provider_request_extensions(nstat_provider_context ctx,
    int requested_extension,
    void *buf,
    size_t buf_size)
{
	struct soflow_hash_entry *hash_entry = (struct soflow_hash_entry *) ctx;
	struct socket *so = (hash_entry && hash_entry->soflow_db) ? hash_entry->soflow_db->soflow_db_so : NULL;
	struct inpcb *inp = so ? sotoinpcb(so) : NULL;
	struct nstat_domain_info *domain_info = NULL;
	size_t size = 0;

	if (hash_entry == NULL || so == NULL || inp == NULL) {
		return 0;
	}

	if (buf == NULL) {
		switch (requested_extension) {
		case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:
			return sizeof(nstat_domain_info);
		default:
			return 0;
		}
	}

	if (buf_size < sizeof(nstat_domain_info)) {
		return 0;
	}

	switch (requested_extension) {
	case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:

		domain_info = (struct nstat_domain_info *)buf;
		necp_copy_inp_domain_info(inp, so, domain_info);

		if (hash_entry->soflow_debug) {
			SOFLOW_LOG(LOG_DEBUG, so, hash_entry->soflow_debug, "Collected NSTAT domain_info:pid %d domain <%s> owner <%s> "
			    "ctxt <%s> bundle id <%s> is_tracker %d is_non_app_initiated %d is_silent %d",
			    so->so_flags & SOF_DELEGATED ? so->e_pid : so->last_pid,
			    domain_info->domain_name,
			    domain_info->domain_owner,
			    domain_info->domain_tracker_ctxt,
			    domain_info->domain_attributed_bundle_id,
			    domain_info->is_tracker,
			    domain_info->is_non_app_initiated,
			    domain_info->is_silent);
		}
		size = sizeof(nstat_domain_info);

	default:
		break;
	}

	return size;
}
#endif

static void
soflow_update_flow_stats(struct soflow_hash_entry *hash_entry, size_t data_size, bool outgoing)
{
	struct socket *so = (hash_entry && hash_entry->soflow_db) ? hash_entry->soflow_db->soflow_db_so : NULL;

	if (hash_entry != NULL) {
		if (outgoing) {
			hash_entry->soflow_txbytes += data_size;
			hash_entry->soflow_txpackets++;
			SOFLOW_ENTRY_LOG(LOG_DEBUG, so, hash_entry, hash_entry->soflow_debug, "Stats update - Outgoing");
		} else {
			hash_entry->soflow_rxbytes += data_size;
			hash_entry->soflow_rxpackets++;
			SOFLOW_ENTRY_LOG(LOG_DEBUG, so, hash_entry, hash_entry->soflow_debug, "Stats update - Incoming");
		}
	}
}

struct soflow_hash_entry *
soflow_get_flow(struct socket *so, struct sockaddr *local, struct sockaddr *remote, struct mbuf *control,
    size_t data_size, soflow_direction_t direction, uint16_t rcv_ifindex)
{
	struct soflow_hash_entry *hash_entry = NULL;
	struct inpcb *inp = sotoinpcb(so);

	// Check if feature is disabled
	if (soflow_disable) {
		return NULL;
	}

	socket_lock_assert_owned(so);

	if (so->so_flow_db != NULL) {
		// Take refcount of db before use.
		// Abort if db is already being freed.
		if (os_ref_retain_try(&so->so_flow_db->soflow_db_ref_count) == false) {
			return NULL;
		}

		// DB already exists, check if this is existing flow
		hash_entry = soflow_db_lookup_entry(so->so_flow_db, local, remote, false);
		if (hash_entry == NULL) {
			// No match with both local and remote, try match with remote only
			hash_entry = soflow_db_lookup_entry(so->so_flow_db, local, remote, true);
		}
		if (hash_entry != NULL) {
			// Take refcount of entry before use.
			// Abort if entry is already being freed.
			if (os_ref_retain_try(&hash_entry->soflow_ref_count) == false) {
				SOFLOW_DB_FREE(so->so_flow_db);
				return NULL;
			}

			// Try to update flow info from socket and/or control mbufs if necessary
			if (hash_entry->soflow_outifindex == 0 ||
			    soflow_entry_local_address_needs_update(hash_entry) || soflow_entry_local_port_needs_update(so, hash_entry)) {
				soflow_entry_update_local(so->so_flow_db, hash_entry, local, control, rcv_ifindex);
			}
			hash_entry->soflow_lastused = net_uptime();
			if (data_size > 0 && direction != SOFLOW_DIRECTION_UNKNOWN) {
				soflow_update_flow_stats(hash_entry, data_size, direction == SOFLOW_DIRECTION_OUTBOUND);
			}

			SOFLOW_DB_FREE(so->so_flow_db);
			return hash_entry;
		}

		SOFLOW_DB_FREE(so->so_flow_db);
	}

	// No flow was found. Only add a new flow if the direction is known.
	if (direction == SOFLOW_DIRECTION_UNKNOWN) {
		return NULL;
	}

	if (so->so_flow_db == NULL) {
		// If new socket, allocate cfil db
		if (soflow_db_init(so) != 0) {
			return NULL;
		}
	}

	hash_entry = soflow_db_add_entry(so->so_flow_db, local, remote);
	if (hash_entry == NULL) {
		SOFLOW_LOG(LOG_ERR, so, true, "Failed to add entry");
		return NULL;
	}

	// Take refcount of entry before use.
	// Abort if entry is already being freed.
	if (os_ref_retain_try(&hash_entry->soflow_ref_count) == false) {
		return NULL;
	}

	if (inp && (inp->inp_last_outifp != NULL || rcv_ifindex != 0)) {
		hash_entry->soflow_outifindex = inp->inp_last_outifp ? inp->inp_last_outifp->if_index : rcv_ifindex;
	}

	// Check if we can update the new flow's local address from control mbufs
	if (control != NULL) {
		soflow_entry_update_local(so->so_flow_db, hash_entry, local, control, rcv_ifindex);
	}
	hash_entry->soflow_outgoing = (direction == SOFLOW_DIRECTION_OUTBOUND);
	if (data_size > 0) {
		soflow_update_flow_stats(hash_entry, data_size, direction == SOFLOW_DIRECTION_OUTBOUND);
	}

	// Only report flow to NSTAT if unconnected UDP
	if (!soflow_nstat_disable && SOFLOW_IS_UDP(so) && !(so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING))) {
#if defined(NSTAT_EXTENSION_FILTER_DOMAIN_INFO)
		// Take refcount of entry before handing it to nstat. Abort if fail.
		if (os_ref_retain_try(&hash_entry->soflow_ref_count) == false) {
			return NULL;
		}
		uuid_generate_random(hash_entry->soflow_uuid);
		hash_entry->soflow_nstat_context = nstat_provider_stats_open((nstat_provider_context) hash_entry,
		    NSTAT_PROVIDER_UDP_SUBFLOW, 0,
		    soflow_nstat_provider_request_vals,
		    soflow_nstat_provider_request_extensions);
		SOFLOW_LOG(LOG_INFO, so, hash_entry->soflow_debug, "<Open nstat> - context %lX", (unsigned long)hash_entry->soflow_nstat_context);
#endif
	}

	SOFLOW_LOCK_EXCLUSIVE;
	if (soflow_initialized == 0) {
		soflow_init();
	}
	TAILQ_INSERT_TAIL(&soflow_entry_head, hash_entry, soflow_entry_list_link);
	if (soflow_attached_count == 0) {
		thread_wakeup((caddr_t)&soflow_attached_count);
	}
	soflow_attached_count++;
	if (soflow_attached_high_water_mark < soflow_attached_count) {
		soflow_attached_high_water_mark = soflow_attached_count;
	}
	SOFLOW_UNLOCK_EXCLUSIVE;

	SOFLOW_ENTRY_LOG(LOG_INFO, so, hash_entry, hash_entry->soflow_debug, "Added entry");
	return hash_entry;
}

void
soflow_free_flow(struct soflow_hash_entry *entry)
{
	SOFLOW_ENTRY_FREE(entry);
}

static bool
soflow_socket_safe_lock(struct inpcb *inp, struct inpcbinfo *pcbinfo)
{
	struct socket *so = NULL;

	VERIFY(pcbinfo != NULL);

	if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
		// Safeguarded the inp state, unlock pcbinfo before locking socket.
		lck_rw_done(&pcbinfo->ipi_lock);

		so = inp->inp_socket;
		socket_lock(so, 1);
		if (in_pcb_checkstate(inp, WNT_RELEASE, 1) != WNT_STOPUSING) {
			return true;
		}
	} else {
		// Failed to safeguarded the inp state, unlock pcbinfo and abort.
		lck_rw_done(&pcbinfo->ipi_lock);
	}

	if (so) {
		socket_unlock(so, 1);
	}
	return false;
}

static struct socket *
soflow_validate_dgram_socket(struct socket *so)
{
	struct inpcb *inp = NULL;
	struct inpcbinfo *pcbinfo = NULL;
	struct socket *locked = NULL;

	pcbinfo = &udbinfo;
	lck_rw_lock_shared(&pcbinfo->ipi_lock);
	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
		if (inp->inp_state != INPCB_STATE_DEAD && inp->inp_socket == so) {
			if (soflow_socket_safe_lock(inp, pcbinfo)) {
				locked = inp->inp_socket;
			}
			/* pcbinfo is already unlocked, we are done. */
			goto done;
		}
	}
	lck_rw_done(&pcbinfo->ipi_lock);
	if (locked != NULL) {
		goto done;
	}

	pcbinfo = &ripcbinfo;
	lck_rw_lock_shared(&pcbinfo->ipi_lock);
	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
		if (inp->inp_state != INPCB_STATE_DEAD && inp->inp_socket == so) {
			if (soflow_socket_safe_lock(inp, pcbinfo)) {
				locked = inp->inp_socket;
			}
			/* pcbinfo is already unlocked, we are done. */
			goto done;
		}
	}
	lck_rw_done(&pcbinfo->ipi_lock);

done:
	return locked;
}

static void
soflow_gc_thread_sleep(bool forever)
{
	if (forever) {
		(void) assert_wait((event_t) &soflow_attached_count,
		    THREAD_INTERRUPTIBLE);
	} else {
		uint64_t deadline = 0;
		nanoseconds_to_absolutetime(SOFLOW_GC_RUN_INTERVAL_NSEC, &deadline);
		clock_absolutetime_interval_to_deadline(deadline, &deadline);

		(void) assert_wait_deadline(&soflow_attached_count,
		    THREAD_INTERRUPTIBLE, deadline);
	}
}

static void
soflow_gc_thread_func(void *v, wait_result_t w)
{
#pragma unused(v, w)

	ASSERT(soflow_gc_thread == current_thread());
	thread_set_thread_name(current_thread(), "SOFLOW_GC");

	// Kick off gc shortly
	soflow_gc_thread_sleep(false);
	thread_block_parameter((thread_continue_t) soflow_gc_expire, NULL);
	/* NOTREACHED */
}

static bool
soflow_gc_idle_timed_out(struct soflow_hash_entry *hash_entry, int timeout, u_int64_t current_time)
{
	struct socket *so = (hash_entry && hash_entry->soflow_db) ? hash_entry->soflow_db->soflow_db_so : NULL;

	if (hash_entry && (current_time - hash_entry->soflow_lastused >= (u_int64_t)timeout)) {
		SOFLOW_ENTRY_LOG(LOG_INFO, so, hash_entry, hash_entry->soflow_debug, "GC Idle Timeout detected");
		return true;
	}
	return false;
}

static int
soflow_gc_cleanup(struct socket *so)
{
	struct soflow_hash_entry *entry = NULL;
	struct soflow_hash_entry *temp_entry = NULL;
	struct soflow_hash_head *flowhash = NULL;
	struct soflow_db *db = NULL;
	int cleaned = 0;

	if (so == NULL || so->so_flow_db == NULL) {
		return 0;
	}
	db = so->so_flow_db;

	// Do not collect garbage for databases that have only one flow.
	if (db->soflow_db_count == 1 && db->soflow_db_only_entry != NULL) {
		return 0;
	}

	socket_lock_assert_owned(so);

	// Take refcount of db before use.
	// Abort if db is already being freed.
	if (os_ref_retain_try(&db->soflow_db_ref_count) == false) {
		return 0;
	}

	for (int i = 0; i < SOFLOW_HASH_SIZE; i++) {
		flowhash = &db->soflow_db_hashbase[i];
		LIST_FOREACH_SAFE(entry, flowhash, soflow_entry_link, temp_entry) {
			if (entry->soflow_gc || entry->soflow_feat_gc) {
				if (entry->soflow_feat_ctxt != NULL && soflow_feat_gc_perform_func_ptr != NULL) {
					soflow_feat_gc_perform_func_ptr(so, entry);
					entry->soflow_feat_ctxt = NULL;
					entry->soflow_feat_ctxt_id = 0;
				}
				entry->soflow_feat_gc = 0;

				if (entry->soflow_gc) {
					SOFLOW_ENTRY_LOG(LOG_INFO, so, entry, entry->soflow_debug, "GC cleanup entry");
					entry->soflow_gc = 0;
					soflow_db_remove_entry(db, entry);
					cleaned++;
				}
			}
		}
	}

	SOFLOW_DB_FREE(db);
	return cleaned;
}

static void
soflow_gc_expire(void *v, wait_result_t w)
{
#pragma unused(v, w)

	static struct socket *socket_array[SOFLOW_GC_MAX_COUNT];
	struct soflow_hash_entry *hash_entry = NULL;
	struct socket *so = NULL;
	u_int64_t current_time = net_uptime();
	uint32_t socket_count = 0;
	uint32_t cleaned_count = 0;
	bool recorded = false;

	// Collect a list of socket with expired flows

	SOFLOW_LOCK_SHARED;

	if (soflow_attached_count == 0) {
		SOFLOW_UNLOCK_SHARED;
		goto go_sleep;
	}

	// Go thorough all flows in the flow list and record any socket with expired flows.
	TAILQ_FOREACH(hash_entry, &soflow_entry_head, soflow_entry_list_link) {
		if (socket_count >= SOFLOW_GC_MAX_COUNT) {
			break;
		}
		so = hash_entry->soflow_db ? hash_entry->soflow_db->soflow_db_so : NULL;

		// Check if we need to perform cleanup due to idle time or feature specified rules
		hash_entry->soflow_gc = soflow_gc_idle_timed_out(hash_entry, SOFLOW_GC_IDLE_TO, current_time);
		hash_entry->soflow_feat_gc = (soflow_feat_gc_needed_func_ptr != NULL && soflow_feat_gc_needed_func_ptr(so, hash_entry, current_time));

		if (hash_entry->soflow_gc || hash_entry->soflow_feat_gc) {
			if (so != NULL) {
				recorded = false;
				for (int i = 0; i < socket_count; i++) {
					if (socket_array[socket_count] == so) {
						recorded = true;
						break;
					}
				}
				if (recorded == false) {
					socket_array[socket_count] = so;
					socket_count++;
				}
			}
		}
	}
	SOFLOW_UNLOCK_SHARED;

	if (socket_count == 0) {
		goto go_sleep;
	}

	for (uint32_t i = 0; i < socket_count; i++) {
		// Validate socket and lock it
		so = soflow_validate_dgram_socket(socket_array[i]);
		if (so == NULL) {
			continue;
		}
		cleaned_count += soflow_gc_cleanup(so);
		socket_unlock(so, 1);
	}

	so = NULL;
	SOFLOW_LOG(LOG_INFO, so, true, "<GC cleaned %d flows>", cleaned_count);

go_sleep:

	// Sleep forever (until waken up) if no more UDP flow to clean
	SOFLOW_LOCK_SHARED;
	soflow_gc_thread_sleep(soflow_attached_count == 0 ? true : false);
	SOFLOW_UNLOCK_SHARED;
	thread_block_parameter((thread_continue_t)soflow_gc_expire, NULL);
	/* NOTREACHED */
}

void
soflow_feat_set_functions(soflow_feat_gc_needed_func gc_needed_fn,
    soflow_feat_gc_perform_func gc_perform_fn,
    soflow_feat_detach_entry_func feat_detach_entry_fn,
    soflow_feat_detach_db_func feat_detach_db_fn)
{
	soflow_feat_gc_needed_func_ptr = gc_needed_fn;
	soflow_feat_gc_perform_func_ptr = gc_perform_fn;
	soflow_feat_detach_entry_func_ptr = feat_detach_entry_fn;
	soflow_feat_detach_db_func_ptr = feat_detach_db_fn;
}

bool
soflow_db_apply(struct soflow_db *db, soflow_entry_apply_func entry_apply_fn, void *context)
{
	struct soflow_hash_entry *entry = NULL;
	struct soflow_hash_entry *temp_entry = NULL;
	struct soflow_hash_head *flowhash = NULL;

	if (db == NULL || db->soflow_db_so == NULL || entry_apply_fn == NULL) {
		return false;
	}

	socket_lock_assert_owned(db->soflow_db_so);

	// Take refcount of db before use.
	// Abort if db is already being freed.
	if (os_ref_retain_try(&db->soflow_db_ref_count) == false) {
		return false;
	}

	for (int i = 0; i < SOFLOW_HASH_SIZE; i++) {
		flowhash = &db->soflow_db_hashbase[i];
		LIST_FOREACH_SAFE(entry, flowhash, soflow_entry_link, temp_entry) {
			if (entry_apply_fn(db->soflow_db_so, entry, context) == false) {
				goto done;
			}
		}
	}

done:
	SOFLOW_DB_FREE(db);
	return true;
}