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
/*
 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 * 
 * 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_OSREFERENCE_LICENSE_HEADER_END@
 */

#include "kpi_interface.h"

#include <sys/queue.h>
#include <sys/param.h>	/* for definition of NULL */
#include <kern/debug.h> /* for panic */
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/kern_event.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kpi_mbuf.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <net/dlil.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/if_arp.h>
#include <libkern/libkern.h>
#include <libkern/OSAtomic.h>
#include <kern/locks.h>

#if IF_LASTCHANGEUPTIME
#define TOUCHLASTCHANGE(__if_lastchange) microuptime(__if_lastchange)
#else
#define TOUCHLASTCHANGE(__if_lastchange) microtime(__if_lastchange)
#endif

extern struct dlil_threading_info *dlil_lo_thread_ptr;
extern int dlil_multithreaded_input;

static errno_t
ifnet_list_get_common(ifnet_family_t, boolean_t, ifnet_t **, u_int32_t *);

/*
	Temporary work around until we have real reference counting
	
	We keep the bits about calling dlil_if_release (which should be
	called recycle) transparent by calling it from our if_free function
	pointer. We have to keep the client's original detach function
	somewhere so we can call it.
 */
static void
ifnet_kpi_free(
	ifnet_t ifp)
{
	ifnet_detached_func	detach_func = ifp->if_kpi_storage;
	
	if (detach_func)
		detach_func(ifp);
	
	if (ifp->if_broadcast.length > sizeof(ifp->if_broadcast.u.buffer)) {
		FREE(ifp->if_broadcast.u.ptr, M_IFADDR);
		ifp->if_broadcast.u.ptr = NULL;
	}
	
	dlil_if_release(ifp);
}

static __inline__ void*
_cast_non_const(const void * ptr) {
	union {
		const void*		cval;
		void*			val;
	} ret;
	
	ret.cval = ptr;
	return (ret.val);
}

errno_t
ifnet_allocate(
	const struct ifnet_init_params *init,
	ifnet_t *interface)
{
	int error;
	struct ifnet *ifp = NULL;
	
	if (init->family == 0)
		return EINVAL;
	if (init->name == NULL ||
		init->output == NULL)
		return EINVAL;
	if (strlen(init->name) >= IFNAMSIZ)
		return EINVAL;
	if ((init->type & 0xFFFFFF00) != 0 || init->type == 0)
		return EINVAL;
	
	error = dlil_if_acquire(init->family, init->uniqueid, init->uniqueid_len, &ifp);
	if (error == 0)
	{
		/*
		 * Cast ifp->if_name as non const. dlil_if_acquire sets it up
		 * to point to storage of at least IFNAMSIZ bytes. It is safe
		 * to write to this.
		 */
		strncpy(_cast_non_const(ifp->if_name), init->name, IFNAMSIZ);
		ifp->if_type = init->type;
		ifp->if_family = init->family;
		ifp->if_unit = init->unit;
		ifp->if_output = init->output;
		ifp->if_demux = init->demux;
		ifp->if_add_proto = init->add_proto;
		ifp->if_del_proto = init->del_proto;
		ifp->if_check_multi = init->check_multi;
		ifp->if_framer = init->framer;
		ifp->if_softc = init->softc;
		ifp->if_ioctl = init->ioctl;
		ifp->if_set_bpf_tap = init->set_bpf_tap;
		ifp->if_free = ifnet_kpi_free;
		ifp->if_event = init->event;
		ifp->if_kpi_storage = init->detach;
		ifp->if_eflags |= IFEF_USEKPI;
		
		if (init->broadcast_len && init->broadcast_addr) {
			if (init->broadcast_len > sizeof(ifp->if_broadcast.u.buffer)) {
				MALLOC(ifp->if_broadcast.u.ptr, u_char*, init->broadcast_len, M_IFADDR, M_NOWAIT);
				if (ifp->if_broadcast.u.ptr == NULL) {
					error = ENOMEM;
				}
				else {
					bcopy(init->broadcast_addr, ifp->if_broadcast.u.ptr, init->broadcast_len);
				}
			}
			else {
				bcopy(init->broadcast_addr, ifp->if_broadcast.u.buffer, init->broadcast_len);
			}
			ifp->if_broadcast.length = init->broadcast_len;
		}
		else {
			bzero(&ifp->if_broadcast, sizeof(ifp->if_broadcast));
		}
		
		if (error == 0) {
			*interface = ifp;
			ifnet_reference(ifp); // temporary - this should be done in dlil_if_acquire
		}
		else {
			dlil_if_release(ifp);
			*interface = 0;
		}
	}
	
	/*
	  Note: We should do something here to indicate that we haven't been
	  attached yet. By doing so, we can catch the case in ifnet_release
	  where the reference count reaches zero and call the recycle
	  function. If the interface is attached, the interface will be
	  recycled when the interface's if_free function is called. If the
	  interface is never attached, the if_free function will never be
	  called and the interface will never be recycled.
	*/
	
	return error;
}

errno_t
ifnet_reference(
	ifnet_t	ifp)
{
	int	oldval;
	
	if (ifp == NULL) return EINVAL;
	
	oldval = OSIncrementAtomic((SInt32 *)&ifp->if_refcnt);
	
	return 0;
}

errno_t
ifnet_release(
	ifnet_t	ifp)
{
	int	oldval;
	
	if (ifp == NULL) return EINVAL;
	
	oldval = OSDecrementAtomic((SInt32*)&ifp->if_refcnt);
	if (oldval == 0)
		panic("ifnet_release - refcount decremented past zero!");
	
	return 0;
}

void*
ifnet_softc(
	ifnet_t interface)
{
	return interface == NULL ? NULL : interface->if_softc;
}

const char*
ifnet_name(
	ifnet_t interface)
{
	return interface == NULL ? NULL : interface->if_name;
}

ifnet_family_t
ifnet_family(
	ifnet_t interface)
{
	return interface == NULL ? 0 : interface->if_family;
}

u_int32_t
ifnet_unit(
	ifnet_t interface)
{
	return interface == NULL ? (u_int32_t)0xffffffff : (u_int32_t)interface->if_unit;
}

u_int32_t
ifnet_index(
	ifnet_t interface)
{
	return interface == NULL ? (u_int32_t)0xffffffff : interface->if_index;
}

errno_t
ifnet_set_flags(
	ifnet_t interface,
	u_int16_t new_flags,
	u_int16_t mask)
{
	int lock;
	
	if (interface == NULL) return EINVAL;
	lock = (interface->if_lock != 0);
	
	if (lock) ifnet_lock_exclusive(interface);
	
	/* If we are modifying the up/down state, call if_updown */
	if (lock && (mask & IFF_UP) != 0) {
		if_updown(interface, (new_flags & IFF_UP) == IFF_UP);
	}
	
	interface->if_flags = (new_flags & mask) | (interface->if_flags & ~mask);
	if (lock) ifnet_lock_done(interface);
	
	return 0;
}

u_int16_t
ifnet_flags(
	ifnet_t interface)
{
	return interface == NULL ? 0 : interface->if_flags;
}

errno_t
ifnet_set_eflags(
	ifnet_t interface,
	u_int32_t new_flags,
	u_int32_t mask)
{
	int lock;
	
	if (interface == NULL) return EINVAL;
	lock = (interface->if_lock != 0);
	
	if (lock) ifnet_lock_exclusive(interface);
	interface->if_eflags = (new_flags & mask) | (interface->if_eflags & ~mask);
	if (lock) ifnet_lock_done(interface);
	
	return 0;
}

u_int32_t
ifnet_eflags(
	ifnet_t interface)
{
	return interface == NULL ? 0 : interface->if_eflags;
}

static const ifnet_offload_t offload_mask = IFNET_CSUM_IP | IFNET_CSUM_TCP |
			IFNET_CSUM_UDP | IFNET_CSUM_FRAGMENT | IFNET_IP_FRAGMENT |
			IFNET_CSUM_SUM16 | IFNET_VLAN_TAGGING | IFNET_VLAN_MTU |
			IFNET_MULTIPAGES;

errno_t
ifnet_set_offload(
	ifnet_t interface,
	ifnet_offload_t offload)
{
	int lock;
	
	if (interface == NULL) return EINVAL;
	lock = (interface->if_lock != 0);
	
	if (lock) ifnet_lock_exclusive(interface);
	interface->if_hwassist = (offload & offload_mask);
	if (lock) ifnet_lock_done(interface);
	
	return 0;
}

ifnet_offload_t
ifnet_offload(
	ifnet_t interface)
{
	return interface == NULL ? 0 : (interface->if_hwassist & offload_mask);
}

/*
 * Should MIB data store a copy?
 */
errno_t
ifnet_set_link_mib_data(
	ifnet_t interface,
	void* mibData,
	u_int32_t mibLen)
{
	int lock;
	
	if (interface == NULL) return EINVAL;
	lock = (interface->if_lock != 0);
	
	if (lock) ifnet_lock_exclusive(interface);
	interface->if_linkmib = (void*)mibData;
	interface->if_linkmiblen = mibLen;
	if (lock) ifnet_lock_done(interface);
	return 0;
}

errno_t
ifnet_get_link_mib_data(
	ifnet_t interface,
	void *mibData,
	u_int32_t *mibLen)
{
	errno_t	result = 0;
	int lock;
	
	if (interface == NULL) return EINVAL;
	lock = (interface->if_lock != NULL);
	
	if (lock) ifnet_lock_shared(interface);
	if (*mibLen < interface->if_linkmiblen)
		result = EMSGSIZE;
	if (result == 0 && interface->if_linkmib == NULL)
		result = ENOTSUP;
	
	if (result == 0) {
		*mibLen = interface->if_linkmiblen;
		bcopy(interface->if_linkmib, mibData, *mibLen);
	}
	if (lock) ifnet_lock_done(interface);
	
	return result;
}

u_int32_t
ifnet_get_link_mib_data_length(
	ifnet_t interface)
{
	return interface == NULL ? 0 : interface->if_linkmiblen;
}

errno_t
ifnet_output(
	ifnet_t interface,
	protocol_family_t protocol_family,
	mbuf_t m,
	void *route,
	const struct sockaddr *dest)
{
	if (interface == NULL || protocol_family == 0 || m == NULL) {
		if (m)
			mbuf_freem_list(m);
		return EINVAL;
	}
	return dlil_output(interface, protocol_family, m, route, dest, 0);
}

errno_t
ifnet_output_raw(
	ifnet_t interface,
	protocol_family_t protocol_family,
	mbuf_t m)
{
	if (interface == NULL || m == NULL) {
		if (m)
			mbuf_freem_list(m);
		return EINVAL;
	}
	return dlil_output(interface, protocol_family, m, NULL, NULL, 1);
}

errno_t
ifnet_set_mtu(
	ifnet_t interface,
	u_int32_t mtu)
{
	if (interface == NULL) return EINVAL;
	interface->if_data.ifi_mtu = mtu;
	return 0;
}

u_int32_t
ifnet_mtu(
	ifnet_t interface)
{
	u_int32_t retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_mtu;
	return retval;
}

u_char
ifnet_type(
	ifnet_t interface)
{
	u_char retval;
	
	retval = interface == NULL ? 0 : interface->if_data.ifi_type;
	return retval;
}

#if 0
errno_t
ifnet_set_typelen(
	ifnet_t interface,
	u_char typelen)
{
	int lock = (interface->if_lock != 0);
	if (lock) ifnet_lock_exclusive(interface);
	interface->if_data.ifi_typelen = typelen;
	if (lock) ifnet_lock_done(interface);
	return 0;
}

u_char
ifnet_typelen(
	ifnet_t interface)
{
	u_char retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_typelen;
	return retval;
}
#endif

errno_t
ifnet_set_addrlen(
	ifnet_t interface,
	u_char addrlen)
{
	if (interface == NULL) return EINVAL;
	interface->if_data.ifi_addrlen = addrlen;
	return 0;
}

u_char
ifnet_addrlen(
	ifnet_t interface)
{
	u_char retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_addrlen;
	return retval;
}

errno_t
ifnet_set_hdrlen(
	ifnet_t interface,
	u_char hdrlen)
{
	if (interface == NULL) return EINVAL;
	interface->if_data.ifi_hdrlen = hdrlen;
	return 0;
}

u_char
ifnet_hdrlen(
	ifnet_t interface)
{
	u_char retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_hdrlen;
	return retval;
}

errno_t
ifnet_set_metric(
	ifnet_t interface,
	u_int32_t metric)
{
	if (interface == NULL) return EINVAL;
	interface->if_data.ifi_metric = metric;
	return 0;
}

u_int32_t
ifnet_metric(
	ifnet_t interface)
{
	u_int32_t retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_metric;
	return retval;
}

errno_t
ifnet_set_baudrate(
	ifnet_t interface,
	u_int64_t baudrate)
{
	if (interface == NULL) return EINVAL;
	/* Pin baudrate to 32 bits until we can change the storage size */
	interface->if_data.ifi_baudrate = baudrate > 0xFFFFFFFF ? 0xFFFFFFFF : baudrate;
	return 0;
}

u_int64_t
ifnet_baudrate(
	ifnet_t interface)
{
	u_int64_t retval;
	retval = interface == NULL ? 0 : interface->if_data.ifi_baudrate;
	return retval;
}

errno_t
ifnet_stat_increment(
	ifnet_t interface,
	const struct ifnet_stat_increment_param *counts)
{
	struct dlil_threading_info *thread;
	if (interface == NULL) return EINVAL;

       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	interface->if_data.ifi_ipackets += counts->packets_in;
	interface->if_data.ifi_ibytes += counts->bytes_in;
	interface->if_data.ifi_ierrors += counts->errors_in;

	interface->if_data.ifi_opackets += counts->packets_out;
	interface->if_data.ifi_obytes += counts->bytes_out;
	interface->if_data.ifi_oerrors += counts->errors_out;

	interface->if_data.ifi_collisions += counts->collisions;
	interface->if_data.ifi_iqdrops += counts->dropped;
	
	/* Touch the last change time. */
	TOUCHLASTCHANGE(&interface->if_lastchange);

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_stat_increment_in(
	ifnet_t interface,
	u_int32_t packets_in,
	u_int32_t bytes_in,
	u_int32_t errors_in)
{
	struct dlil_threading_info *thread;

	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	interface->if_data.ifi_ipackets += packets_in;
	interface->if_data.ifi_ibytes += bytes_in;
	interface->if_data.ifi_ierrors += errors_in;

	TOUCHLASTCHANGE(&interface->if_lastchange);

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_stat_increment_out(
	ifnet_t interface,
	u_int32_t packets_out,
	u_int32_t bytes_out,
	u_int32_t errors_out)
{
	struct dlil_threading_info *thread;
	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	interface->if_data.ifi_opackets += packets_out;
	interface->if_data.ifi_obytes += bytes_out;
	interface->if_data.ifi_oerrors += errors_out;

	TOUCHLASTCHANGE(&interface->if_lastchange);

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_set_stat(
	ifnet_t interface,
	const struct ifnet_stats_param *stats)
{
	struct dlil_threading_info *thread;

	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	interface->if_data.ifi_ipackets = stats->packets_in;
	interface->if_data.ifi_ibytes = stats->bytes_in;
	interface->if_data.ifi_imcasts = stats->multicasts_in;
	interface->if_data.ifi_ierrors = stats->errors_in;
	
	interface->if_data.ifi_opackets = stats->packets_out;
	interface->if_data.ifi_obytes = stats->bytes_out;
	interface->if_data.ifi_omcasts = stats->multicasts_out;
	interface->if_data.ifi_oerrors = stats->errors_out;
	
	interface->if_data.ifi_collisions = stats->collisions;
	interface->if_data.ifi_iqdrops = stats->dropped;
	interface->if_data.ifi_noproto = stats->no_protocol;

	/* Touch the last change time. */
	TOUCHLASTCHANGE(&interface->if_lastchange);

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_stat(
	ifnet_t interface,
	struct ifnet_stats_param *stats)
{
	struct dlil_threading_info *thread;

	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	stats->packets_in = interface->if_data.ifi_ipackets;
	stats->bytes_in = interface->if_data.ifi_ibytes;
	stats->multicasts_in = interface->if_data.ifi_imcasts;
	stats->errors_in = interface->if_data.ifi_ierrors;

	stats->packets_out = interface->if_data.ifi_opackets;
	stats->bytes_out = interface->if_data.ifi_obytes;
	stats->multicasts_out = interface->if_data.ifi_omcasts;
	stats->errors_out = interface->if_data.ifi_oerrors;

	stats->collisions = interface->if_data.ifi_collisions;
	stats->dropped = interface->if_data.ifi_iqdrops;
	stats->no_protocol = interface->if_data.ifi_noproto;

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_touch_lastchange(
	ifnet_t interface)
{
	struct dlil_threading_info *thread;

	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	TOUCHLASTCHANGE(&interface->if_lastchange);

	lck_mtx_unlock(thread->input_lck);
	
	return 0;
}

errno_t
ifnet_lastchange(
	ifnet_t interface,
	struct timeval *last_change)
{
	struct dlil_threading_info *thread;

	if (interface == NULL) return EINVAL;
	
       	if ((thread = interface->if_input_thread) == NULL || (dlil_multithreaded_input == 0))
		thread = dlil_lo_thread_ptr;

	lck_mtx_lock(thread->input_lck);

	*last_change = interface->if_data.ifi_lastchange;
	
	lck_mtx_unlock(thread->input_lck);
	
#if IF_LASTCHANGEUPTIME
	/* Crude conversion from uptime to calendar time */
	last_change->tv_sec += boottime_sec();
#endif

	return 0;
}

errno_t
ifnet_get_address_list(
	ifnet_t interface,
	ifaddr_t **addresses)
{
	if (addresses == NULL) return EINVAL;
	return ifnet_get_address_list_family(interface, addresses, 0);
}

errno_t
ifnet_get_address_list_family(
	ifnet_t interface,
	ifaddr_t **addresses,
	sa_family_t	family)
{
	struct ifnet *ifp;
	int count = 0;
	int cmax = 0;
	
	if (addresses == NULL) return EINVAL;
	*addresses = NULL;
	
	ifnet_head_lock_shared();
	TAILQ_FOREACH(ifp, &ifnet, if_link)
	{
		if (interface && ifp != interface) continue;
		
		ifnet_lock_shared(ifp);
		if ((ifp->if_eflags & IFEF_DETACHING) == 0) {
			if (interface == NULL || interface == ifp)
			{
				struct ifaddr *addr;
				TAILQ_FOREACH(addr, &ifp->if_addrhead, ifa_link)
				{
					if (family == 0 || addr->ifa_addr->sa_family == family)
						cmax++;
				}
			}
		}
		else if (interface != NULL) {
			ifnet_lock_done(ifp);
			ifnet_head_done();
			return ENXIO;
		}
		ifnet_lock_done(ifp);
	}
	
	MALLOC(*addresses, ifaddr_t*, sizeof(ifaddr_t) * (cmax + 1), M_TEMP, M_NOWAIT);
	if (*addresses == NULL) {
		ifnet_head_done();
		return ENOMEM;
	}
	
	TAILQ_FOREACH(ifp, &ifnet, if_link)
	{
		if (interface && ifp != interface) continue;
		
		ifnet_lock_shared(ifp);
		if ((ifp->if_eflags & IFEF_DETACHING) == 0) {
			if (interface == NULL || (struct ifnet*)interface == ifp)
			{
				struct ifaddr *addr;
				TAILQ_FOREACH(addr, &ifp->if_addrhead, ifa_link)
				{
					if (count + 1 > cmax) break;
					if (family == 0 || addr->ifa_addr->sa_family == family) {
						(*addresses)[count] = (ifaddr_t)addr;
						ifaddr_reference((*addresses)[count]);
						count++;
					}
				}
			}
		}
		ifnet_lock_done(ifp);
		if (interface || count == cmax)
			break;
	}
	ifnet_head_done();
	(*addresses)[cmax] = 0;
	
	return 0;
}

void
ifnet_free_address_list(
	ifaddr_t *addresses)
{
	int i;
	
	if (addresses == NULL) return;
	
	for (i = 0; addresses[i] != NULL; i++)
	{
		ifaddr_release(addresses[i]);
	}
	
	FREE(addresses, M_TEMP);
}

void*
ifnet_lladdr(
	ifnet_t	interface)
{
	if (interface == NULL) return NULL;
	return LLADDR(SDL(interface->if_addrhead.tqh_first->ifa_addr));
}

errno_t
ifnet_llbroadcast_copy_bytes(
	ifnet_t	interface,
	void	*addr,
	size_t	buffer_len,
	size_t	*out_len)
{
	if (interface == NULL || addr == NULL || out_len == NULL) return EINVAL;
	
	*out_len = interface->if_broadcast.length;
	
	if (buffer_len < interface->if_broadcast.length) {
		return EMSGSIZE;
	}
	
	if (interface->if_broadcast.length == 0)
		return ENXIO;
	
	if (interface->if_broadcast.length <= sizeof(interface->if_broadcast.u.buffer)) {
		bcopy(interface->if_broadcast.u.buffer, addr, interface->if_broadcast.length);
	}
	else {
		bcopy(interface->if_broadcast.u.ptr, addr, interface->if_broadcast.length);
	}
	
	return 0;
}

errno_t
ifnet_lladdr_copy_bytes(
	ifnet_t	interface,
	void*	lladdr,
	size_t	lladdr_len)
{
	struct sockaddr_dl *sdl;
	if (interface == NULL || lladdr == NULL) return EINVAL;
	
	sdl = SDL(interface->if_addrhead.tqh_first->ifa_addr);
	
	while (1) {
		if (lladdr_len != sdl->sdl_alen) {
			bzero(lladdr, lladdr_len);
			return EMSGSIZE;
		}
		bcopy(LLADDR(sdl), lladdr, lladdr_len);
		if (bcmp(lladdr, LLADDR(sdl), lladdr_len) == 0 &&
			lladdr_len == sdl->sdl_alen)
			break;
	}
	return 0;
}

static errno_t
ifnet_set_lladdr_internal(
	ifnet_t interface,
	const void *lladdr,
	size_t lladdr_len,
	u_char new_type,
	int apply_type)
{
	struct ifaddr *ifa;
	struct sockaddr_dl	*sdl;
	errno_t	error = 0;
	
	if (interface == NULL) return EINVAL;
	
	if (lladdr_len != 0 && (lladdr_len != interface->if_addrlen || lladdr == 0))
		return EINVAL;
	
	ifnet_head_lock_shared();
	ifa = ifnet_addrs[interface->if_index - 1];
	if (ifa != NULL) {
		sdl = (struct sockaddr_dl*)ifa->ifa_addr;
		if (lladdr_len != 0) {
			bcopy(lladdr, LLADDR(sdl), lladdr_len);
		}
		else {
			bzero(LLADDR(sdl), interface->if_addrlen);
		}
		sdl->sdl_alen = lladdr_len;
		
		if (apply_type) {
			sdl->sdl_type = new_type;
		}
	}
	else {
		error = ENXIO;
	}
	ifnet_head_done();
	
	/* Generate a kernel event */
	if (error == 0) {
		dlil_post_msg(interface, KEV_DL_SUBCLASS,
			KEV_DL_LINK_ADDRESS_CHANGED, NULL, 0);
	}
	
	return error;
}

errno_t
ifnet_set_lladdr(
	ifnet_t interface,
	const void* lladdr,
	size_t lladdr_len)
{
	return ifnet_set_lladdr_internal(interface, lladdr, lladdr_len, 0, 0);
}

errno_t
ifnet_set_lladdr_and_type(
	ifnet_t interface,
	const void* lladdr,
	size_t lladdr_len,
	u_char type)
{
	return ifnet_set_lladdr_internal(interface, lladdr, lladdr_len, type, 1);
}

errno_t
ifnet_add_multicast(
	ifnet_t interface,
	const struct sockaddr *maddr,
	ifmultiaddr_t *address)
{
	if (interface == NULL || maddr == NULL) return EINVAL;
	return if_addmulti(interface, maddr, address);
}

errno_t
ifnet_remove_multicast(
	ifmultiaddr_t address)
{
	if (address == NULL) return EINVAL;
	return if_delmultiaddr(address, 0);
}

errno_t ifnet_get_multicast_list(ifnet_t interface, ifmultiaddr_t **addresses)
{
	int count = 0;
	int cmax = 0;
	struct ifmultiaddr *addr;
	int lock;
	
	if (interface == NULL || addresses == NULL)
		return EINVAL;
	
	lock = (interface->if_lock != 0);
	if (lock) ifnet_lock_shared(interface);
	if ((interface->if_eflags & IFEF_DETACHING) == 0) {
		LIST_FOREACH(addr, &interface->if_multiaddrs, ifma_link)
		{
			cmax++;
		}
	}
	else {
		if (lock) ifnet_lock_done(interface);
		return ENXIO;
	}
	
	MALLOC(*addresses, ifmultiaddr_t*, sizeof(ifmultiaddr_t) * (cmax + 1), M_TEMP, M_NOWAIT);
	if (*addresses == NULL) {
		if (lock) ifnet_lock_done(interface);
		return ENOMEM;
	}
	
	LIST_FOREACH(addr, &interface->if_multiaddrs, ifma_link)
	{
		if (count + 1 > cmax)
			break;
		(*addresses)[count] = (ifmultiaddr_t)addr;
		ifmaddr_reference((*addresses)[count]);
		count++;
	}
	(*addresses)[cmax] = 0;
	if (lock) ifnet_lock_done(interface);
	
	return 0;
}

void
ifnet_free_multicast_list(
	ifmultiaddr_t *addresses)
{
	int i;
	
	if (addresses == NULL) return;
	
	for (i = 0; addresses[i] != NULL; i++)
	{
		ifmaddr_release(addresses[i]);
	}
	
	FREE(addresses, M_TEMP);
}

errno_t
ifnet_find_by_name(
	const char *ifname,
	ifnet_t *interface)
{
	struct ifnet *ifp;
	int	namelen;
	
	if (ifname == NULL) return EINVAL;
	
	namelen = strlen(ifname);
	
	*interface = NULL;
	
	ifnet_head_lock_shared();
	TAILQ_FOREACH(ifp, &ifnet, if_link)
	{
		struct ifaddr *ifa = ifnet_addrs[ifp->if_index - 1];
		struct sockaddr_dl *ll_addr;

		if (!ifa || !ifa->ifa_addr)
			continue;

		ll_addr = (struct sockaddr_dl *)ifa->ifa_addr;

		if ((ifp->if_eflags & IFEF_DETACHING) == 0 &&
			namelen == ll_addr->sdl_nlen &&
			(strncmp(ll_addr->sdl_data, ifname, ll_addr->sdl_nlen) == 0))
		{
			break;
		}
	}
	if (ifp) {
		*interface = ifp;
		ifnet_reference(*interface);
	}
	ifnet_head_done();
	
	return (ifp == NULL) ? ENXIO : 0;
}

errno_t
ifnet_list_get(ifnet_family_t family, ifnet_t **list, u_int32_t *count)
{
	return (ifnet_list_get_common(family, FALSE, list, count));
}

__private_extern__ errno_t
ifnet_list_get_all(ifnet_family_t family, ifnet_t **list, u_int32_t *count)
{
	return (ifnet_list_get_common(family, TRUE, list, count));
}

static errno_t
ifnet_list_get_common(ifnet_family_t family, boolean_t get_all, ifnet_t **list,
    u_int32_t *count)
{
	struct ifnet *ifp;
	u_int32_t cmax = 0;
	*count = 0;
	errno_t	result = 0;

	if (list == NULL || count == NULL)
		return (EINVAL);

	ifnet_head_lock_shared();
	TAILQ_FOREACH(ifp, &ifnet, if_link) {
		if ((ifp->if_eflags & IFEF_DETACHING) && !get_all)
			continue;
		if (family == IFNET_FAMILY_ANY || ifp->if_family == family)
			cmax++;
	}

	if (cmax == 0)
		result = ENXIO;

	if (result == 0) {
		MALLOC(*list, ifnet_t*, sizeof(ifnet_t) * (cmax + 1),
		    M_TEMP, M_NOWAIT);
		if (*list == NULL)
			result = ENOMEM;
	}

	if (result == 0) {
		TAILQ_FOREACH(ifp, &ifnet, if_link) {
			if ((ifp->if_eflags & IFEF_DETACHING) && !get_all)
				continue;
			if (*count + 1 > cmax)
				break;
			if (family == IFNET_FAMILY_ANY ||
			    ((ifnet_family_t)ifp->if_family) == family) {
				(*list)[*count] = (ifnet_t)ifp;
				ifnet_reference((*list)[*count]);
				(*count)++;
			}
		}
		(*list)[*count] = NULL;
	}
	ifnet_head_done();

	return (result);
}

void
ifnet_list_free(ifnet_t *interfaces)
{
	int i;

	if (interfaces == NULL)
		return;

	for (i = 0; interfaces[i]; i++) {
		ifnet_release(interfaces[i]);
	}

	FREE(interfaces, M_TEMP);
}

/****************************************************************************/
/* ifaddr_t accessors														*/
/****************************************************************************/

errno_t
ifaddr_reference(
	ifaddr_t ifa)
{
	if (ifa == NULL) return EINVAL;
	ifaref(ifa);
	return 0;
}

errno_t
ifaddr_release(
	ifaddr_t ifa)
{
	if (ifa == NULL) return EINVAL;
	ifafree(ifa);
	return 0;
}

sa_family_t
ifaddr_address_family(
	ifaddr_t ifa)
{
	if (ifa && ifa->ifa_addr)
		return ifa->ifa_addr->sa_family;
	
	return 0;
}

errno_t
ifaddr_address(
	ifaddr_t ifa,
	struct sockaddr *out_addr,
	u_int32_t addr_size)
{
	u_int32_t copylen;
	
	if (ifa == NULL || out_addr == NULL) return EINVAL;
	if (ifa->ifa_addr == NULL) return ENOTSUP;
	
	copylen = (addr_size >= ifa->ifa_addr->sa_len) ? ifa->ifa_addr->sa_len : addr_size;
	bcopy(ifa->ifa_addr, out_addr, copylen);
	
	if (ifa->ifa_addr->sa_len > addr_size) return EMSGSIZE;
	
	return 0;
}

errno_t
ifaddr_dstaddress(
	ifaddr_t ifa,
	struct sockaddr *out_addr,
	u_int32_t addr_size)
{
	u_int32_t copylen;
	if (ifa == NULL || out_addr == NULL) return EINVAL;
	if (ifa->ifa_dstaddr == NULL) return ENOTSUP;
	
	copylen = (addr_size >= ifa->ifa_dstaddr->sa_len) ? ifa->ifa_dstaddr->sa_len : addr_size;
	bcopy(ifa->ifa_dstaddr, out_addr, copylen);

	if (ifa->ifa_dstaddr->sa_len > addr_size) return EMSGSIZE;
	
	return 0;
}

errno_t
ifaddr_netmask(
	ifaddr_t ifa,
	struct sockaddr *out_addr,
	u_int32_t addr_size)
{
	u_int32_t copylen;
	if (ifa == NULL || out_addr == NULL) return EINVAL;
	if (ifa->ifa_netmask == NULL) return ENOTSUP;
	
	copylen = addr_size >= ifa->ifa_netmask->sa_len ? ifa->ifa_netmask->sa_len : addr_size;
	bcopy(ifa->ifa_netmask, out_addr, copylen);
	
	if (ifa->ifa_netmask->sa_len > addr_size) return EMSGSIZE;
	
	return 0;
}

ifnet_t
ifaddr_ifnet(
	ifaddr_t ifa)
{
	struct ifnet *ifp;
	if (ifa == NULL) return NULL;
	ifp = ifa->ifa_ifp;
	
	return (ifnet_t)ifp;
}

ifaddr_t
ifaddr_withaddr(
	const struct sockaddr* address)
{
	if (address == NULL) return NULL;
	return ifa_ifwithaddr(address);
}

ifaddr_t
ifaddr_withdstaddr(
	const struct sockaddr* address)
{
	if (address == NULL) return NULL;
	return ifa_ifwithdstaddr(address);
}

ifaddr_t
ifaddr_withnet(
	const struct sockaddr* net)
{
	if (net == NULL) return NULL;
	return ifa_ifwithnet(net);
}

ifaddr_t
ifaddr_withroute(
	int flags,
	const struct sockaddr* destination,
	const struct sockaddr* gateway)
{
	if (destination == NULL || gateway == NULL) return NULL;
	return ifa_ifwithroute(flags, destination, gateway);
}

ifaddr_t
ifaddr_findbestforaddr(
	const struct sockaddr *addr,
	ifnet_t interface)
{
	if (addr == NULL || interface == NULL) return NULL;
	return ifaof_ifpforaddr(addr, interface);
}

errno_t
ifmaddr_reference(
	ifmultiaddr_t ifmaddr)
{
	if (ifmaddr == NULL) return EINVAL;
	ifma_reference(ifmaddr);
	return 0;
}

errno_t
ifmaddr_release(
	ifmultiaddr_t ifmaddr)
{
	if (ifmaddr == NULL) return EINVAL;
	ifma_release(ifmaddr);	
	return 0;
}

errno_t
ifmaddr_address(
	ifmultiaddr_t ifmaddr,
	struct sockaddr *out_addr,
	u_int32_t addr_size)
{
	u_int32_t copylen;
	
	if (ifmaddr == NULL || out_addr == NULL) return EINVAL;
	if (ifmaddr->ifma_addr == NULL) return ENOTSUP;
	
	copylen = addr_size >= ifmaddr->ifma_addr->sa_len ? ifmaddr->ifma_addr->sa_len : addr_size;
	bcopy(ifmaddr->ifma_addr, out_addr, copylen);
	
	if (ifmaddr->ifma_addr->sa_len > addr_size) return EMSGSIZE;
	
	return 0;
}

errno_t
ifmaddr_lladdress(
	ifmultiaddr_t ifmaddr,
	struct sockaddr *out_addr,
	u_int32_t addr_size)
{
	if (ifmaddr == NULL || out_addr == NULL) return EINVAL;
	if (ifmaddr->ifma_ll == NULL) return ENOTSUP;
	
	return ifmaddr_address(ifmaddr->ifma_ll, out_addr, addr_size);
}

ifnet_t
ifmaddr_ifnet(
	ifmultiaddr_t ifmaddr)
{
	if (ifmaddr == NULL || ifmaddr->ifma_ifp == NULL) return NULL;
	return ifmaddr->ifma_ifp;
}