DragonFlyBSD Kernel Audit
sys/net/if_ethersubr.c
← back
   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
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
/*
 * Copyright (c) 1982, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
 */

#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_mpls.h"
#include "opt_netgraph.h"
#include "opt_carp.h"
#include "opt_rss.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/globaldata.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/msgport.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/thread.h>

#include <sys/thread2.h>
#include <sys/mplock2.h>

#include <net/if.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/if_llc.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/ifq_var.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/vlan/if_vlan_ether.h>
#include <net/vlan/if_vlan_var.h>
#include <net/netmsg2.h>
#include <net/netisr2.h>

#if defined(INET) || defined(INET6)
#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/tcp_var.h>
#include <netinet/if_ether.h>
#include <netinet/ip_flow.h>
#include <net/ipfw/ip_fw.h>
#include <net/ipfw3/ip_fw.h>
#include <net/dummynet/ip_dummynet.h>
#endif
#ifdef INET6
#include <netinet6/nd6.h>
#endif

#ifdef CARP
#include <netinet/ip_carp.h>
#endif

#ifdef MPLS
#include <netproto/mpls/mpls.h>
#endif

/* netgraph node hooks for ng_ether(4) */
void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
void	(*ng_ether_attach_p)(struct ifnet *ifp);
void	(*ng_ether_detach_p)(struct ifnet *ifp);

void	(*vlan_input_p)(struct mbuf *);

static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
			struct rtentry *);
static void ether_restore_header(struct mbuf **, const struct ether_header *,
				 const struct ether_header *);
static int ether_characterize(struct mbuf **);
static void ether_dispatch(struct ifnet *, int, struct mbuf *, int);

/*
 * if_bridge support
 */
struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
int (*bridge_output_p)(struct ifnet *, struct mbuf *);
void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
struct ifnet *(*bridge_interface_p)(void *if_bridge);

static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
			      struct sockaddr *);

/*
 * if_lagg(4) support
 */
void	(*lagg_input_p)(struct ifnet *, struct mbuf *);
int (*lagg_output_p)(struct ifnet *, struct mbuf *);

const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#define gotoerr(e) do { error = (e); goto bad; } while (0)
#define IFP2AC(ifp) ((struct arpcom *)(ifp))

static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
				struct ip_fw **rule,
				const struct ether_header *eh);

static int ether_ipfw;
static u_long ether_restore_hdr;
static u_long ether_prepend_hdr;
static u_long ether_input_wronghash;
static int ether_debug;

#ifdef RSS_DEBUG
static u_long ether_pktinfo_try;
static u_long ether_pktinfo_hit;
static u_long ether_rss_nopi;
static u_long ether_rss_nohash;
static u_long ether_input_requeue;
#endif
static u_long ether_input_wronghwhash;
static int ether_input_ckhash;

#define ETHER_TSOLEN_DEFAULT	(4 * ETHERMTU)

#define ETHER_NMBCLUSTERS_DEFMIN	32
#define ETHER_NMBCLUSTERS_DEFAULT	256

static int ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
TUNABLE_INT("net.link.ether.tsolen", &ether_tsolen_default);

static int ether_nmbclusters_default = ETHER_NMBCLUSTERS_DEFAULT;
TUNABLE_INT("net.link.ether.nmbclusters", &ether_nmbclusters_default);

SYSCTL_DECL(_net_link);
SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
    &ether_debug, 0, "Ether debug");
SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
    &ether_ipfw, 0, "Pass ether pkts through firewall");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
    &ether_restore_hdr, 0, "# of ether header restoration");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
    &ether_prepend_hdr, 0,
    "# of ether header restoration which prepends mbuf");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
    &ether_input_wronghash, 0, "# of input packets with wrong hash");
SYSCTL_INT(_net_link_ether, OID_AUTO, tsolen, CTLFLAG_RW,
    &ether_tsolen_default, 0, "Default max TSO length");

#ifdef RSS_DEBUG
SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
    &ether_rss_nopi, 0, "# of packets do not have pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
    &ether_rss_nohash, 0, "# of packets do not have hash");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
    &ether_pktinfo_try, 0,
    "# of tries to find packets' msgport using pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
    &ether_pktinfo_hit, 0,
    "# of packets whose msgport are found using pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
    &ether_input_requeue, 0, "# of input packets gets requeued");
#endif
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
    &ether_input_wronghwhash, 0, "# of input packets with wrong hw hash");
SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
    &ether_input_ckhash, 0, "always check hash");

#define ETHER_KTR_STR		"ifp=%p"
#define ETHER_KTR_ARGS		struct ifnet *ifp
#ifndef KTR_ETHERNET
#define KTR_ETHERNET		KTR_ALL
#endif
KTR_INFO_MASTER(ether);
KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
#define logether(name, arg)	KTR_LOG(ether_ ## name, arg)

/*
 * Ethernet output routine.
 * Encapsulate a packet of type family for the local net.
 * Use trailer local net encapsulation if enough data in first
 * packet leaves a multiple of 512 bytes of data in remainder.
 * Assumes that ifp is actually pointer to arpcom structure.
 */
static int
ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
	     struct rtentry *rt)
{
	struct ether_header *eh, *deh;
	u_char *edst;
	int loop_copy = 0;
	int hlen = ETHER_HDR_LEN;	/* link layer header length */
	struct arpcom *ac = IFP2AC(ifp);
	int error;

	ASSERT_NETISR_NCPUS(mycpuid);
	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

	if (ifp->if_flags & IFF_MONITOR)
		gotoerr(ENETDOWN);
	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
		gotoerr(ENETDOWN);

	M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
	if (m == NULL)
		return (ENOBUFS);
	m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
	eh = mtod(m, struct ether_header *);
	edst = eh->ether_dhost;

	/*
	 * Fill in the destination ethernet address and frame type.
	 */
	switch (dst->sa_family) {
#ifdef INET
	case AF_INET:
		error = arpresolve(ifp, rt, m, dst, edst);
		if (error != 0)
			return error == EWOULDBLOCK ? 0 : error;
#ifdef MPLS
		if (m->m_flags & M_MPLSLABELED)
			eh->ether_type = htons(ETHERTYPE_MPLS);
		else
#endif
			eh->ether_type = htons(ETHERTYPE_IP);
		break;
	case AF_ARP:
	{
		struct arphdr *ah;

		ah = mtod(m, struct arphdr *);
		ah->ar_hrd = htons(ARPHRD_ETHER);

		loop_copy = -1;	/* if this is for us, don't do it */

		switch(ntohs(ah->ar_op)) {
		case ARPOP_REVREQUEST:
		case ARPOP_REVREPLY:
			eh->ether_type = htons(ETHERTYPE_REVARP);
			break;
		case ARPOP_REQUEST:
		case ARPOP_REPLY:
		default:
			eh->ether_type = htons(ETHERTYPE_ARP);
			break;
		}

		if (m->m_flags & M_BCAST)
			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
		else
			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
	}
#endif
#ifdef INET6
	case AF_INET6:
		error = nd6_resolve(&ac->ac_if, rt, m, dst, edst);
		if (error != 0)
			return error == EWOULDBLOCK ? 0 : error;
		eh->ether_type = htons(ETHERTYPE_IPV6);
		break;
#endif
	case pseudo_AF_HDRCMPLT:
	case AF_UNSPEC:
		loop_copy = -1; /* if this is for us, don't do it */
		deh = (struct ether_header *)dst->sa_data;
		memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
		eh->ether_type = deh->ether_type;
		break;

	default:
		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
		gotoerr(EAFNOSUPPORT);
	}

	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
		memcpy(eh->ether_shost,
		       ((struct ether_header *)dst->sa_data)->ether_shost,
		       ETHER_ADDR_LEN);
	else
		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);

	/*
	 * Bridges require special output handling.
	 */
	if (ifp->if_bridge) {
		KASSERT(bridge_output_p != NULL,
			("%s: if_bridge not loaded!", __func__));
		return bridge_output_p(ifp, m);
	}
#if 0 /* XXX */
	if (ifp->if_lagg) {
		KASSERT(lagg_output_p != NULL,
			("%s: if_lagg not loaded!", __func__));
		return lagg_output_p(ifp, m);
	}
#endif

	/*
	 * If a simplex interface, and the packet is being sent to our
	 * Ethernet address or a broadcast address, loopback a copy.
	 * XXX To make a simplex device behave exactly like a duplex
	 * device, we should copy in the case of sending to our own
	 * ethernet address (thus letting the original actually appear
	 * on the wire). However, we don't do that here for security
	 * reasons and compatibility with the original behavior.
	 */
	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
		int csum_flags = 0;

		if (m->m_pkthdr.csum_flags & CSUM_IP)
			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
			struct mbuf *n;

			if ((n = m_copypacket(m, M_NOWAIT)) != NULL) {
				n->m_pkthdr.csum_flags |= csum_flags;
				if (csum_flags & CSUM_DATA_VALID)
					n->m_pkthdr.csum_data = 0xffff;
				if_simloop(ifp, n, dst->sa_family, hlen);
			} else
				IFNET_STAT_INC(ifp, iqdrops, 1);
		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
				ETHER_ADDR_LEN) == 0) {
			m->m_pkthdr.csum_flags |= csum_flags;
			if (csum_flags & CSUM_DATA_VALID)
				m->m_pkthdr.csum_data = 0xffff;
			if_simloop(ifp, m, dst->sa_family, hlen);
			return (0);	/* XXX */
		}
	}

#ifdef CARP
	if (ifp->if_type == IFT_CARP) {
		ifp = carp_parent(ifp);
		if (ifp == NULL)
			gotoerr(ENETUNREACH);

		ac = IFP2AC(ifp);

		/*
		 * Check precondition again
		 */
		ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

		if (ifp->if_flags & IFF_MONITOR)
			gotoerr(ENETDOWN);
		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
		    (IFF_UP | IFF_RUNNING))
			gotoerr(ENETDOWN);
	}
#endif

	/* Handle ng_ether(4) processing, if any */
	if (ng_ether_output_p != NULL) {
		/*
		 * Hold BGL and recheck ng_ether_output_p
		 */
		get_mplock();
		if (ng_ether_output_p != NULL) {
			if ((error = ng_ether_output_p(ifp, &m)) != 0) {
				rel_mplock();
				goto bad;
			}
			if (m == NULL) {
				rel_mplock();
				return (0);
			}
		}
		rel_mplock();
	}

	/* Continue with link-layer output */
	return ether_output_frame(ifp, m);

bad:
	m_freem(m);
	return (error);
}

/*
 * Returns the bridge interface an ifp is associated
 * with.
 *
 * Only call if ifp->if_bridge != NULL.
 */
struct ifnet *
ether_bridge_interface(struct ifnet *ifp)
{
	if (bridge_interface_p)
		return(bridge_interface_p(ifp->if_bridge));
	return (ifp);
}

/*
 * Ethernet link layer output routine to send a raw frame to the device.
 *
 * This assumes that the 14 byte Ethernet header is present and contiguous
 * in the first mbuf.
 */
int
ether_output_frame(struct ifnet *ifp, struct mbuf *m)
{
	struct ip_fw *rule = NULL;
	int error = 0;
	struct altq_pktattr pktattr;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
		struct m_tag *mtag;

		/* Extract info from dummynet tag */
		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
		KKASSERT(mtag != NULL);
		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
		KKASSERT(rule != NULL);

		m_tag_delete(m, mtag);
		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
	}

	if (ifq_is_enabled(&ifp->if_snd))
		altq_etherclassify(&ifp->if_snd, m, &pktattr);
	crit_enter();
	if ((IPFW_LOADED || IPFW3_LOADED) && ether_ipfw != 0) {
		struct ether_header save_eh, *eh;

		eh = mtod(m, struct ether_header *);
		save_eh = *eh;
		m_adj(m, ETHER_HDR_LEN);
		if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
			crit_exit();
			if (m != NULL) {
				m_freem(m);
				return ENOBUFS; /* pkt dropped */
			} else
				return 0;	/* consumed e.g. in a pipe */
		}

		/* packet was ok, restore the ethernet header */
		ether_restore_header(&m, eh, &save_eh);
		if (m == NULL) {
			crit_exit();
			return ENOBUFS;
		}
	}
	crit_exit();

	/*
	 * Queue message on interface, update output statistics if
	 * successful, and start output if interface not yet active.
	 */
	error = ifq_dispatch(ifp, m, &pktattr);
	return (error);
}

/*
 * ipfw processing for ethernet packets (in and out).
 * The second parameter is NULL from ether_demux(), and ifp from
 * ether_output_frame().
 */
static boolean_t
ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
	       const struct ether_header *eh)
{
	struct ether_header save_eh = *eh;	/* might be a ptr in *m0 */
	struct ip_fw_args args;
	struct m_tag *mtag;
	struct mbuf *m;
	int i;

	if (*rule != NULL && fw_one_pass)
		return TRUE; /* dummynet packet, already partially processed */

	/*
	 * I need some amount of data to be contiguous.
	 */
	i = min((*m0)->m_pkthdr.len, max_protohdr);
	if ((*m0)->m_len < i) {
		*m0 = m_pullup(*m0, i);
		if (*m0 == NULL)
			return FALSE;
	}

	/*
	 * Clean up tags
	 */
	if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
		m_tag_delete(*m0, mtag);
	if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
		mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
		KKASSERT(mtag != NULL);
		m_tag_delete(*m0, mtag);
		(*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
	}

	args.flags = 0;
	args.xlat = NULL;
	args.m = *m0;		/* the packet we are looking at		*/
	args.oif = dst;		/* destination, if any			*/
	args.rule = *rule;	/* matching rule to restart		*/
	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
	i = ip_fw_chk_ptr(&args);
	*m0 = args.m;
	*rule = args.rule;

	if (*m0 == NULL)
		return FALSE;

	switch (i) {
	case IP_FW_PASS:
		return TRUE;

	case IP_FW_DIVERT:
	case IP_FW_TEE:
	case IP_FW_DENY:
		/*
		 * XXX at some point add support for divert/forward actions.
		 * If none of the above matches, we have to drop the pkt.
		 */
		return FALSE;

	case IP_FW_DUMMYNET:
		/*
		 * Pass the pkt to dummynet, which consumes it.
		 */
		m = *m0;	/* pass the original to dummynet */
		*m0 = NULL;	/* and nothing back to the caller */

		ether_restore_header(&m, eh, &save_eh);
		if (m == NULL)
			return FALSE;

		m = ip_fw_dn_io_ptr(m, args.cookie,
		    dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
		if (m != NULL)
			ip_dn_queue(m);
		return FALSE;

	default:
		panic("unknown ipfw return value: %d", i);
	}
}

/*
 * Perform common duties while attaching to interface list
 */
void
ether_ifattach(struct ifnet *ifp, const uint8_t *lla,
    lwkt_serialize_t serializer)
{
	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
	    serializer);
}

void
ether_ifattach_bpf(struct ifnet *ifp, const uint8_t *lla,
    u_int dlt, u_int hdrlen, lwkt_serialize_t serializer)
{
	struct sockaddr_dl *sdl;
	char ethstr[ETHER_ADDRSTRLEN + 1];
	struct ifaltq *ifq;
	int i;

	/*
	 * If driver does not configure # of mbuf clusters/jclusters
	 * that could sit on the device queues for quite some time,
	 * we then assume:
	 * - The device queues only consume mbuf clusters.
	 * - No more than ether_nmbclusters_default (by default 256)
	 *   mbuf clusters will sit on the device queues for quite
	 *   some time.
	 */
	if (ifp->if_nmbclusters <= 0 && ifp->if_nmbjclusters <= 0) {
		if (ether_nmbclusters_default < ETHER_NMBCLUSTERS_DEFMIN) {
			kprintf("ether nmbclusters %d -> %d\n",
			    ether_nmbclusters_default,
			    ETHER_NMBCLUSTERS_DEFAULT);
			ether_nmbclusters_default = ETHER_NMBCLUSTERS_DEFAULT;
		}
		ifp->if_nmbclusters = ether_nmbclusters_default;
	}

	ifp->if_type = IFT_ETHER;
	ifp->if_addrlen = ETHER_ADDR_LEN;
	ifp->if_hdrlen = ETHER_HDR_LEN;
	if_attach(ifp, serializer);
	ifq = &ifp->if_snd;
	for (i = 0; i < ifq->altq_subq_cnt; ++i) {
		struct ifaltq_subque *ifsq = ifq_get_subq(ifq, i);

		ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen *
		    (ETHER_MAX_LEN - ETHER_CRC_LEN);
	}
	ifp->if_mtu = ETHERMTU;
	if (ifp->if_tsolen <= 0) {
		if ((ether_tsolen_default / ETHERMTU) < 2) {
			kprintf("ether TSO maxlen %d -> %d\n",
			    ether_tsolen_default, ETHER_TSOLEN_DEFAULT);
			ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
		}
		ifp->if_tsolen = ether_tsolen_default;
	}
	if (ifp->if_baudrate == 0)
		ifp->if_baudrate = 10000000;
	ifp->if_output = ether_output;
	ifp->if_input = ether_input;
	ifp->if_resolvemulti = ether_resolvemulti;
	ifp->if_broadcastaddr = etherbroadcastaddr;
	sdl = IF_LLSOCKADDR(ifp);
	sdl->sdl_type = IFT_ETHER;
	sdl->sdl_alen = ifp->if_addrlen;
	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
	/*
	 * XXX Keep the current drivers happy.
	 * XXX Remove once all drivers have been cleaned up
	 */
	if (lla != IFP2AC(ifp)->ac_enaddr)
		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
	bpfattach(ifp, dlt, hdrlen);
	if (ng_ether_attach_p != NULL)
		(*ng_ether_attach_p)(ifp);

	if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
}

/*
 * Perform common duties while detaching an Ethernet interface
 */
void
ether_ifdetach(struct ifnet *ifp)
{
	if_down(ifp);

	if (ng_ether_detach_p != NULL)
		(*ng_ether_detach_p)(ifp);
	bpfdetach(ifp);
	if_detach(ifp);
}

int
ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
	struct ifaddr *ifa = (struct ifaddr *) data;
	struct ifreq *ifr = (struct ifreq *) data;
	int error = 0;

#define IF_INIT(ifp) \
do { \
	if (((ifp)->if_flags & IFF_UP) == 0) { \
		(ifp)->if_flags |= IFF_UP; \
		(ifp)->if_init((ifp)->if_softc); \
	} \
} while (0)

	ASSERT_IFNET_SERIALIZED_ALL(ifp);

	switch (command) {
	case SIOCSIFADDR:
		switch (ifa->ifa_addr->sa_family) {
#ifdef INET
		case AF_INET:
			IF_INIT(ifp);	/* before arpwhohas */
			arp_ifinit(ifp, ifa);
			break;
#endif
		default:
			IF_INIT(ifp);
			break;
		}
		break;

	case SIOCGIFADDR:
	case SIOCGHWADDR:
		error = copyout(IFP2AC(ifp)->ac_enaddr,
				((struct sockaddr *)ifr->ifr_data)->sa_data,
				ETHER_ADDR_LEN);
		break;

	case SIOCSIFMTU:
		/*
		 * Set the interface MTU.
		 */
		if (ifr->ifr_mtu > ETHERMTU) {
			error = EINVAL;
		} else {
			ifp->if_mtu = ifr->ifr_mtu;
		}
		break;
	default:
		error = EINVAL;
		break;
	}
	return (error);

#undef IF_INIT
}

static int
ether_resolvemulti(
	struct ifnet *ifp,
	struct sockaddr **llsa,
	struct sockaddr *sa)
{
	struct sockaddr_dl *sdl;
#ifdef INET
	struct sockaddr_in *sin;
#endif
#ifdef INET6
	struct sockaddr_in6 *sin6;
#endif
	u_char *e_addr;

	switch(sa->sa_family) {
	case AF_LINK:
		/*
		 * No mapping needed. Just check that it's a valid MC address.
		 */
		sdl = (struct sockaddr_dl *)sa;
		e_addr = LLADDR(sdl);
		if ((e_addr[0] & 1) != 1)
			return EADDRNOTAVAIL;
		*llsa = NULL;
		return 0;

#ifdef INET
	case AF_INET:
		sin = (struct sockaddr_in *)sa;
		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
			return EADDRNOTAVAIL;
		sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
		sdl->sdl_len = sizeof *sdl;
		sdl->sdl_family = AF_LINK;
		sdl->sdl_index = ifp->if_index;
		sdl->sdl_type = IFT_ETHER;
		sdl->sdl_alen = ETHER_ADDR_LEN;
		e_addr = LLADDR(sdl);
		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
		*llsa = (struct sockaddr *)sdl;
		return 0;
#endif
#ifdef INET6
	case AF_INET6:
		sin6 = (struct sockaddr_in6 *)sa;
		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
			/*
			 * An IP6 address of 0 means listen to all
			 * of the Ethernet multicast address used for IP6.
			 * (This is used for multicast routers.)
			 */
			ifp->if_flags |= IFF_ALLMULTI;
			*llsa = NULL;
			return 0;
		}
		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
			return EADDRNOTAVAIL;
		sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
		sdl->sdl_len = sizeof *sdl;
		sdl->sdl_family = AF_LINK;
		sdl->sdl_index = ifp->if_index;
		sdl->sdl_type = IFT_ETHER;
		sdl->sdl_alen = ETHER_ADDR_LEN;
		e_addr = LLADDR(sdl);
		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
		*llsa = (struct sockaddr *)sdl;
		return 0;
#endif

	default:
		/*
		 * Well, the text isn't quite right, but it's the name
		 * that counts...
		 */
		return EAFNOSUPPORT;
	}
}

#if 0
/*
 * This is for reference.  We have a table-driven version
 * of the little-endian crc32 generator, which is faster
 * than the double-loop.
 */
uint32_t
ether_crc32_le(const uint8_t *buf, size_t len)
{
	uint32_t c, crc, carry;
	size_t i, j;

	crc = 0xffffffffU;	/* initial value */

	for (i = 0; i < len; i++) {
		c = buf[i];
		for (j = 0; j < 8; j++) {
			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
			crc >>= 1;
			c >>= 1;
			if (carry)
				crc = (crc ^ ETHER_CRC_POLY_LE);
		}
	}

	return (crc);
}
#else
uint32_t
ether_crc32_le(const uint8_t *buf, size_t len)
{
	static const uint32_t crctab[] = {
		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
	};
	uint32_t crc;
	size_t i;

	crc = 0xffffffffU;	/* initial value */

	for (i = 0; i < len; i++) {
		crc ^= buf[i];
		crc = (crc >> 4) ^ crctab[crc & 0xf];
		crc = (crc >> 4) ^ crctab[crc & 0xf];
	}

	return (crc);
}
#endif

uint32_t
ether_crc32_be(const uint8_t *buf, size_t len)
{
	uint32_t c, crc, carry;
	size_t i, j;

	crc = 0xffffffffU;	/* initial value */

	for (i = 0; i < len; i++) {
		c = buf[i];
		for (j = 0; j < 8; j++) {
			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
			crc <<= 1;
			c >>= 1;
			if (carry)
				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
		}
	}

	return (crc);
}

/*
 * find the size of ethernet header, and call classifier
 */
void
altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
		   struct altq_pktattr *pktattr)
{
	struct ether_header *eh;
	uint16_t ether_type;
	int hlen, af, hdrsize;

	hlen = sizeof(struct ether_header);
	eh = mtod(m, struct ether_header *);

	ether_type = ntohs(eh->ether_type);
	if (ether_type < ETHERMTU) {
		/* ick! LLC/SNAP */
		struct llc *llc = (struct llc *)(eh + 1);
		hlen += 8;

		if (m->m_len < hlen ||
		    llc->llc_dsap != LLC_SNAP_LSAP ||
		    llc->llc_ssap != LLC_SNAP_LSAP ||
		    llc->llc_control != LLC_UI)
			goto bad;  /* not snap! */

		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
	}

	if (ether_type == ETHERTYPE_IP) {
		af = AF_INET;
		hdrsize = 20;  /* sizeof(struct ip) */
#ifdef INET6
	} else if (ether_type == ETHERTYPE_IPV6) {
		af = AF_INET6;
		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
#endif
	} else
		goto bad;

	while (m->m_len <= hlen) {
		hlen -= m->m_len;
		m = m->m_next;
	}
	if (m->m_len < hlen + hdrsize) {
		/*
		 * ip header is not in a single mbuf.  this should not
		 * happen in the current code.
		 * (todo: use m_pulldown in the future)
		 */
		goto bad;
	}
	m->m_data += hlen;
	m->m_len -= hlen;
	ifq_classify(ifq, m, af, pktattr);
	m->m_data -= hlen;
	m->m_len += hlen;

	return;

bad:
	pktattr->pattr_class = NULL;
	pktattr->pattr_hdr = NULL;
	pktattr->pattr_af = AF_UNSPEC;
}

static void
ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
		     const struct ether_header *save_eh)
{
	struct mbuf *m = *m0;

	ether_restore_hdr++;

	/*
	 * Prepend the header, optimize for the common case of
	 * eh pointing into the mbuf.
	 */
	if ((const void *)(eh + 1) == (void *)m->m_data) {
		m->m_data -= ETHER_HDR_LEN;
		m->m_len += ETHER_HDR_LEN;
		m->m_pkthdr.len += ETHER_HDR_LEN;
	} else {
		ether_prepend_hdr++;

		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
		if (m != NULL) {
			bcopy(save_eh, mtod(m, struct ether_header *),
			      ETHER_HDR_LEN);
		}
	}
	*m0 = m;
}

/*
 * Upper layer processing for a received Ethernet packet.
 */
void
ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
{
	struct ether_header *eh;
	int isr, discard = 0;
	u_short ether_type;
	struct ip_fw *rule = NULL;

	M_ASSERTPKTHDR(m);
	KASSERT(m->m_len >= ETHER_HDR_LEN,
		("ether header is not contiguous!"));

	eh = mtod(m, struct ether_header *);

	if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
		struct m_tag *mtag;

		/* Extract info from dummynet tag */
		mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
		KKASSERT(mtag != NULL);
		rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
		KKASSERT(rule != NULL);

		m_tag_delete(m, mtag);
		m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;

		/* packet is passing the second time */
		goto post_stats;
	}

	/*
	 * We got a packet which was unicast to a different Ethernet
	 * address.  If the driver is working properly, then this
	 * situation can only happen when the interface is in
	 * promiscuous mode.  We defer the packet discarding until the
	 * vlan processing is done, so that vlan/bridge or vlan/netgraph
	 * could work.
	 */
	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
		if (ether_debug & 1) {
			kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
				"%02x:%02x:%02x:%02x:%02x:%02x "
				"%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
				eh->ether_dhost[0],
				eh->ether_dhost[1],
				eh->ether_dhost[2],
				eh->ether_dhost[3],
				eh->ether_dhost[4],
				eh->ether_dhost[5],
				eh->ether_shost[0],
				eh->ether_shost[1],
				eh->ether_shost[2],
				eh->ether_shost[3],
				eh->ether_shost[4],
				eh->ether_shost[5],
				eh->ether_type,
				((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
				((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
				((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
				((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
				((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
				((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
			);
		}
		if ((ether_debug & 2) == 0)
			discard = 1;
	}

post_stats:
	if ((IPFW_LOADED || IPFW3_LOADED) && ether_ipfw != 0 && !discard) {
		struct ether_header save_eh = *eh;

		/* XXX old crufty stuff, needs to be removed */
		m_adj(m, sizeof(struct ether_header));

		if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
			m_freem(m);
			return;
		}

		ether_restore_header(&m, eh, &save_eh);
		if (m == NULL)
			return;
		eh = mtod(m, struct ether_header *);
	}

	ether_type = ntohs(eh->ether_type);
	KKASSERT(ether_type != ETHERTYPE_VLAN);

        /* Handle input from a lagg(4) port */
        if (ifp->if_type == IFT_IEEE8023ADLAG) {
                KASSERT(lagg_input_p != NULL,
                    ("%s: if_lagg not loaded!", __func__));
                (*lagg_input_p)(ifp, m);
		return;
        }

	if (m->m_flags & M_VLANTAG) {
		void (*vlan_input_func)(struct mbuf *);

		vlan_input_func = vlan_input_p;
		/* Make sure 'vlan_input_func' is really used. */
		cpu_ccfence();
		if (vlan_input_func != NULL) {
			vlan_input_func(m);
		} else {
			IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
			m_freem(m);
		}
		return;
	}

	/*
	 * If we have been asked to discard this packet
	 * (e.g. not for us), drop it before entering
	 * the upper layer.
	 */
	if (discard) {
		m_freem(m);
		return;
	}

	/*
	 * Clear protocol specific flags,
	 * before entering the upper layer.
	 */
	m->m_flags &= ~M_ETHER_FLAGS;

	/* Strip ethernet header. */
	m_adj(m, sizeof(struct ether_header));

	switch (ether_type) {
#ifdef INET
	case ETHERTYPE_IP:
		if ((m->m_flags & M_LENCHECKED) == 0) {
			if (!ip_lengthcheck(&m, 0))
				return;
		}
		if (ipflow_fastforward(m))
			return;
		isr = NETISR_IP;
		break;

	case ETHERTYPE_ARP:
		if (ifp->if_flags & IFF_NOARP) {
			/* Discard packet if ARP is disabled on interface */
			m_freem(m);
			return;
		}
		isr = NETISR_ARP;
		break;
#endif

#ifdef INET6
	case ETHERTYPE_IPV6:
		isr = NETISR_IPV6;
		break;
#endif

#ifdef MPLS
	case ETHERTYPE_MPLS:
	case ETHERTYPE_MPLS_MCAST:
		/* Should have been set by ether_input(). */
		KKASSERT(m->m_flags & M_MPLSLABELED);
		isr = NETISR_MPLS;
		break;
#endif

	default:
		/*
		 * The accurate msgport is not determined before
		 * we reach here, so recharacterize packet.
		 */
		m->m_flags &= ~M_HASH;
		if (ng_ether_input_orphan_p != NULL) {
			/*
			 * Put back the ethernet header so netgraph has a
			 * consistent view of inbound packets.
			 */
			M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
			if (m == NULL) {
				/*
				 * M_PREPEND frees the mbuf in case of failure.
				 */
				return;
			}
			/*
			 * Hold BGL and recheck ng_ether_input_orphan_p
			 */
			get_mplock();
			if (ng_ether_input_orphan_p != NULL) {
				ng_ether_input_orphan_p(ifp, m);
				rel_mplock();
				return;
			}
			rel_mplock();
		}
		m_freem(m);
		return;
	}

	if (m->m_flags & M_HASH) {
		if (&curthread->td_msgport ==
		    netisr_hashport(m->m_pkthdr.hash)) {
			netisr_handle(isr, m);
			return;
		} else {
			/*
			 * XXX Something is wrong,
			 * we probably should panic here!
			 */
			m->m_flags &= ~M_HASH;
			atomic_add_long(&ether_input_wronghash, 1);
		}
	}
#ifdef RSS_DEBUG
	atomic_add_long(&ether_input_requeue, 1);
#endif
	netisr_queue(isr, m);
}

/*
 * First we perform any link layer operations, then continue to the
 * upper layers with ether_demux_oncpu().
 */
static void
ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
{
#ifdef CARP
	void *carp;
#endif

	if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
		/*
		 * Receiving interface's flags are changed, when this
		 * packet is waiting for processing; discard it.
		 */
		m_freem(m);
		return;
	}

	/*
	 * A vlan tagged packet must be processed by ether_demux_oncpu()
	 * immediately, before any bridging or packet filtering.  If
	 * the vlan decides to process it, this function will be called
	 * again w/ the vlan interface for normal processing.
	 */
	if (m->m_flags & M_VLANTAG) {
		ether_demux_oncpu(ifp, m);
		return;
	}

	/*
	 * Tap the packet off here for a bridge.  bridge_input()
	 * will return NULL if it has consumed the packet, otherwise
	 * it gets processed as normal.  Note that bridge_input()
	 * will always return the original packet if we need to
	 * process it locally.
	 */
	if (ifp->if_bridge) {
		KASSERT(bridge_input_p != NULL,
			("%s: if_bridge not loaded!", __func__));

		if(m->m_flags & M_ETHER_BRIDGED) {
			m->m_flags &= ~M_ETHER_BRIDGED;
		} else {
			m = bridge_input_p(ifp, m);
			if (m == NULL)
				return;

			KASSERT(ifp == m->m_pkthdr.rcvif,
				("bridge_input_p changed rcvif"));
		}
	}

#ifdef CARP
	carp = ifp->if_carp;
	if (carp) {
		m = carp_input(carp, m);
		if (m == NULL)
			return;
		KASSERT(ifp == m->m_pkthdr.rcvif,
		    ("carp_input changed rcvif"));
	}
#endif

	/* Handle ng_ether(4) processing, if any */
	if (ng_ether_input_p != NULL) {
		/*
		 * Hold BGL and recheck ng_ether_input_p
		 */
		get_mplock();
		if (ng_ether_input_p != NULL)
			ng_ether_input_p(ifp, &m);
		rel_mplock();

		if (m == NULL)
			return;
	}

	/* Continue with upper layer processing */
	ether_demux_oncpu(ifp, m);
}

/*
 * Perform certain functions of ether_input():
 * - Test IFF_UP
 * - Update statistics
 * - Run bpf(4) tap if requested
 * Then pass the packet to ether_input_oncpu().
 *
 * This function should be used by pseudo interface (e.g. vlan(4)),
 * when it tries to claim that the packet is received by it.
 *
 * REINPUT_KEEPRCVIF
 * REINPUT_RUNBPF
 */
void
ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
{
	/* Discard packet if interface is not up */
	if (!(ifp->if_flags & IFF_UP)) {
		m_freem(m);
		return;
	}

	/*
	 * Change receiving interface.  The bridge will often pass a flag to
	 * ask that this not be done so ARPs get applied to the correct
	 * side.
	 */
	if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
	    m->m_pkthdr.rcvif == NULL)
	{
		m->m_pkthdr.rcvif = ifp;
	}

	/* Update statistics */
	IFNET_STAT_INC(ifp, ipackets, 1);
	IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
	if (m->m_flags & (M_MCAST | M_BCAST))
		IFNET_STAT_INC(ifp, imcasts, 1);

	if (reinput_flags & REINPUT_RUNBPF)
		BPF_MTAP(ifp, m);

	ether_input_oncpu(ifp, m);
}

static __inline boolean_t
ether_vlancheck(struct mbuf **m0)
{
	struct mbuf *m = *m0;
	struct ether_header *eh = mtod(m, struct ether_header *);
	uint16_t ether_type = ntohs(eh->ether_type);

	if (ether_type == ETHERTYPE_VLAN) {
		if ((m->m_flags & M_VLANTAG) == 0) {
			/*
			 * Extract vlan tag if hardware does not do
			 * it for us.
			 */
			vlan_ether_decap(&m);
			if (m == NULL)
				goto failed;

			eh = mtod(m, struct ether_header *);
			ether_type = ntohs(eh->ether_type);
			if (ether_type == ETHERTYPE_VLAN) {
				/*
				 * To prevent possible dangerous recursion,
				 * we don't do vlan-in-vlan.
				 */
				IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
				goto failed;
			}
		} else {
			/*
			 * To prevent possible dangerous recursion,
			 * we don't do vlan-in-vlan.
			 */
			IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
			goto failed;
		}
		KKASSERT(ether_type != ETHERTYPE_VLAN);
	}

	m->m_flags |= M_ETHER_VLANCHECKED;
	*m0 = m;
	return TRUE;
failed:
	if (m != NULL)
		m_freem(m);
	*m0 = NULL;
	return FALSE;
}

static void
ether_input_handler(netmsg_t nmsg)
{
	struct netmsg_packet *nmp = &nmsg->packet;	/* actual size */
	struct ether_header *eh;
	struct ifnet *ifp;
	struct mbuf *m;

	m = nmp->nm_packet;
	M_ASSERTPKTHDR(m);

	if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
		if (!ether_vlancheck(&m)) {
			KKASSERT(m == NULL);
			return;
		}
	}

	ifp = m->m_pkthdr.rcvif;
	if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH) ||
	    __predict_false(ether_input_ckhash)) {
		int isr;

		/*
		 * Need to verify the hash supplied by the hardware
		 * which could be wrong.
		 */
		m->m_flags &= ~(M_HASH | M_CKHASH);
		isr = ether_characterize(&m);
		if (m == NULL)
			return;
		KKASSERT(m->m_flags & M_HASH);

		if (netisr_hashcpu(m->m_pkthdr.hash) != mycpuid) {
			/*
			 * Wrong hardware supplied hash; redispatch
			 */
			ether_dispatch(ifp, isr, m, -1);
			if (__predict_false(ether_input_ckhash))
				atomic_add_long(&ether_input_wronghwhash, 1);
			return;
		}
	}

	eh = mtod(m, struct ether_header *);
	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
			 ifp->if_addrlen) == 0)
			m->m_flags |= M_BCAST;
		else
			m->m_flags |= M_MCAST;
		IFNET_STAT_INC(ifp, imcasts, 1);
	}

	ether_input_oncpu(ifp, m);
}

/*
 * Send the packet to the target netisr msgport
 *
 * At this point the packet must be characterized (M_HASH set),
 * so we know which netisr to send it to.
 */
static void
ether_dispatch(struct ifnet *ifp, int isr, struct mbuf *m, int cpuid)
{
	struct netmsg_packet *pmsg;
	int target_cpuid;

	KKASSERT(m->m_flags & M_HASH);
	target_cpuid = netisr_hashcpu(m->m_pkthdr.hash);

	pmsg = &m->m_hdr.mh_netmsg;
	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
		    0, ether_input_handler);
	pmsg->nm_packet = m;
	pmsg->base.lmsg.u.ms_result = isr;

	logether(disp_beg, NULL);
	if (target_cpuid == cpuid) {
		if ((ifp->if_flags & IFF_IDIRECT) && IN_NETISR_NCPUS(cpuid)) {
			ether_input_handler((netmsg_t)pmsg);
		} else {
			lwkt_sendmsg_oncpu(netisr_cpuport(target_cpuid),
			    &pmsg->base.lmsg);
		}
	} else {
		lwkt_sendmsg(netisr_cpuport(target_cpuid),
		    &pmsg->base.lmsg);
	}
	logether(disp_end, NULL);
}

/*
 * Process a received Ethernet packet.
 *
 * The ethernet header is assumed to be in the mbuf so the caller
 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
 * bytes in the first mbuf.
 *
 * If the caller knows that the current thread is stick to the current
 * cpu, e.g. the interrupt thread or the netisr thread, the current cpuid
 * (mycpuid) should be passed through 'cpuid' argument.  Else -1 should
 * be passed as 'cpuid' argument.
 */
void
ether_input(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
    int cpuid)
{
	int isr;

	M_ASSERTPKTHDR(m);

	/* Discard packet if interface is not up */
	if (!(ifp->if_flags & IFF_UP)) {
		m_freem(m);
		return;
	}

	if (m->m_len < sizeof(struct ether_header)) {
		/* XXX error in the caller. */
		m_freem(m);
		return;
	}

	m->m_pkthdr.rcvif = ifp;

	logether(pkt_beg, ifp);

	ETHER_BPF_MTAP(ifp, m);

	IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);

	if (ifp->if_flags & IFF_MONITOR) {
		struct ether_header *eh;

		eh = mtod(m, struct ether_header *);
		if (ETHER_IS_MULTICAST(eh->ether_dhost))
			IFNET_STAT_INC(ifp, imcasts, 1);

		/*
		 * Interface marked for monitoring; discard packet.
		 */
		m_freem(m);

		logether(pkt_end, ifp);
		return;
	}

	/*
	 * If the packet has been characterized (pi->pi_netisr / M_HASH)
	 * we can dispatch it immediately with trivial checks.
	 */
	if (pi != NULL && (m->m_flags & M_HASH)) {
#ifdef RSS_DEBUG
		atomic_add_long(&ether_pktinfo_try, 1);
#endif
		netisr_hashcheck(pi->pi_netisr, m, pi);
		if (m->m_flags & M_HASH) {
			ether_dispatch(ifp, pi->pi_netisr, m, cpuid);
#ifdef RSS_DEBUG
			atomic_add_long(&ether_pktinfo_hit, 1);
#endif
			logether(pkt_end, ifp);
			return;
		}
	}
#ifdef RSS_DEBUG
	else if (ifp->if_capenable & IFCAP_RSS) {
		if (pi == NULL)
			atomic_add_long(&ether_rss_nopi, 1);
		else
			atomic_add_long(&ether_rss_nohash, 1);
	}
#endif

	/*
	 * Packet hash will be recalculated by software, so clear
	 * the M_HASH and M_CKHASH flag set by the driver; the hash
	 * value calculated by the hardware may not be exactly what
	 * we want.
	 */
	m->m_flags &= ~(M_HASH | M_CKHASH);

	if (!ether_vlancheck(&m)) {
		KKASSERT(m == NULL);
		logether(pkt_end, ifp);
		return;
	}

	isr = ether_characterize(&m);
	if (m == NULL) {
		logether(pkt_end, ifp);
		return;
	}

	/*
	 * Finally dispatch it
	 */
	ether_dispatch(ifp, isr, m, cpuid);

	logether(pkt_end, ifp);
}

static int
ether_characterize(struct mbuf **m0)
{
	struct mbuf *m = *m0;
	struct ether_header *eh;
	uint16_t ether_type;
	int isr;

	eh = mtod(m, struct ether_header *);
	ether_type = ntohs(eh->ether_type);

	/*
	 * Map ether type to netisr id.
	 */
	switch (ether_type) {
#ifdef INET
	case ETHERTYPE_IP:
		isr = NETISR_IP;
		break;

	case ETHERTYPE_ARP:
		isr = NETISR_ARP;
		break;
#endif

#ifdef INET6
	case ETHERTYPE_IPV6:
		isr = NETISR_IPV6;
		break;
#endif

#ifdef MPLS
	case ETHERTYPE_MPLS:
	case ETHERTYPE_MPLS_MCAST:
		m->m_flags |= M_MPLSLABELED;
		isr = NETISR_MPLS;
		break;
#endif

	default:
		/*
		 * NETISR_MAX is an invalid value; it is chosen to let
		 * netisr_characterize() know that we have no clear
		 * idea where this packet should go.
		 */
		isr = NETISR_MAX;
		break;
	}

	/*
	 * Ask the isr to characterize the packet since we couldn't.
	 * This is an attempt to optimally get us onto the correct protocol
	 * thread.
	 */
	netisr_characterize(isr, &m, sizeof(struct ether_header));

	*m0 = m;
	return isr;
}

static void
ether_demux_handler(netmsg_t nmsg)
{
	struct netmsg_packet *nmp = &nmsg->packet;	/* actual size */
	struct ifnet *ifp;
	struct mbuf *m;

	m = nmp->nm_packet;
	M_ASSERTPKTHDR(m);
	ifp = m->m_pkthdr.rcvif;

	ether_demux_oncpu(ifp, m);
}

void
ether_demux(struct mbuf *m)
{
	struct netmsg_packet *pmsg;
	int isr;

	isr = ether_characterize(&m);
	if (m == NULL)
		return;

	KKASSERT(m->m_flags & M_HASH);
	pmsg = &m->m_hdr.mh_netmsg;
	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
	    0, ether_demux_handler);
	pmsg->nm_packet = m;
	pmsg->base.lmsg.u.ms_result = isr;

	lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg);
}

u_char *
kether_aton(const char *macstr, u_char *addr)
{
        unsigned int o0, o1, o2, o3, o4, o5;
        int n;

        if (macstr == NULL || addr == NULL)
                return NULL;

        n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
            &o3, &o4, &o5);
        if (n != 6)
                return NULL;

        addr[0] = o0;
        addr[1] = o1;
        addr[2] = o2;
        addr[3] = o3;
        addr[4] = o4;
        addr[5] = o5;

        return addr;
}

char *
kether_ntoa(const u_char *addr, char *buf)
{
        int len = ETHER_ADDRSTRLEN + 1;
        int n;

        n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0],
            addr[1], addr[2], addr[3], addr[4], addr[5]);

        if (n < 17)
                return NULL;

        return buf;
}

MODULE_VERSION(ether, 1);