Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
/* $NetBSD: if_pppoe.c,v 1.183 2022/08/15 08:28:41 knakahara Exp $ */

/*
 * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Martin Husemann <martin@NetBSD.org>.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.183 2022/08/15 08:28:41 knakahara Exp $");

#ifdef _KERNEL_OPT
#include "pppoe.h"
#include "opt_pppoe.h"
#include "opt_net_mpsafe.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/atomic.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <sys/kauth.h>
#include <sys/intr.h>
#include <sys/socketvar.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/rwlock.h>
#include <sys/mutex.h>
#include <sys/psref.h>
#include <sys/cprng.h>
#include <sys/workqueue.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/if_ether.h>
#include <net/if_sppp.h>
#include <net/if_spppvar.h>
#include <net/if_pppoe.h>
#include <net/if_dl.h>

#include <net/bpf.h>

#include "ioconf.h"

#ifdef NET_MPSAFE
#define PPPOE_MPSAFE	1
#endif

#ifndef PPPOE_DEQUEUE_MAXLEN
#define PPPOE_DEQUEUE_MAXLEN	IFQ_MAXLEN
#endif

struct pppoehdr {
	uint8_t vertype;
	uint8_t code;
	uint16_t session;
	uint16_t plen;
} __packed;

struct pppoetag {
	uint16_t tag;
	uint16_t len;
} __packed;

#define	PPPOE_HEADERLEN	sizeof(struct pppoehdr)
#define	PPPOE_OVERHEAD	(PPPOE_HEADERLEN + 2)
#define	PPPOE_VERTYPE	0x11	/* VER=1, TYPE = 1 */

#define	PPPOE_TAG_EOL		0x0000		/* end of list */
#define	PPPOE_TAG_SNAME		0x0101		/* service name */
#define	PPPOE_TAG_ACNAME	0x0102		/* access concentrator name */
#define	PPPOE_TAG_HUNIQUE	0x0103		/* host unique */
#define	PPPOE_TAG_ACCOOKIE	0x0104		/* AC cookie */
#define	PPPOE_TAG_VENDOR	0x0105		/* vendor specific */
#define	PPPOE_TAG_RELAYSID	0x0110		/* relay session id */
#define	PPPOE_TAG_MAX_PAYLOAD	0x0120		/* max payload */
#define	PPPOE_TAG_SNAME_ERR	0x0201		/* service name error */
#define	PPPOE_TAG_ACSYS_ERR	0x0202		/* AC system error */
#define	PPPOE_TAG_GENERIC_ERR	0x0203		/* generic error */

#define	PPPOE_CODE_PADI		0x09		/* Active Discovery Initiation */
#define	PPPOE_CODE_PADO		0x07		/* Active Discovery Offer */
#define	PPPOE_CODE_PADR		0x19		/* Active Discovery Request */
#define	PPPOE_CODE_PADS		0x65		/* Active Discovery Session confirmation */
#define	PPPOE_CODE_PADT		0xA7		/* Active Discovery Terminate */

/* two byte PPP protocol discriminator, then IP data */
#define	PPPOE_MAXMTU	(ETHERMTU - PPPOE_OVERHEAD)

/* Add a 16 bit unsigned value to a buffer pointed to by PTR */
#define	PPPOE_ADD_16(PTR, VAL)			\
		*(PTR)++ = (VAL) / 256;		\
		*(PTR)++ = (VAL) % 256

/* Add a complete PPPoE header to the buffer pointed to by PTR */
#define	PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)	\
		*(PTR)++ = PPPOE_VERTYPE;	\
		*(PTR)++ = (CODE);		\
		PPPOE_ADD_16(PTR, SESS);	\
		PPPOE_ADD_16(PTR, LEN)

#define	PPPOE_DISC_TIMEOUT	(hz*5)	/* base for quick timeout calculation */
#define	PPPOE_SLOW_RETRY	(hz*60)	/* persistent retry interval */
#define	PPPOE_RECON_FAST	(hz*15)	/* first retry after auth failure */
#define	PPPOE_RECON_IMMEDIATE	(hz/10)	/* "no delay" reconnect */
#define	PPPOE_RECON_PADTRCVD	(hz*5)	/* reconnect delay after PADT received */
#define	PPPOE_DISC_MAXPADI	4	/* retry PADI four times (quickly) */
#define	PPPOE_DISC_MAXPADR	2	/* retry PADR twice */

#ifdef PPPOE_SERVER
/* from if_spppsubr.c */
#define	IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
#endif

#define PPPOE_LOCK(_sc, _op)	rw_enter(&(_sc)->sc_lock, (_op))
#define PPPOE_UNLOCK(_sc)	rw_exit(&(_sc)->sc_lock)
#define PPPOE_WLOCKED(_sc)	rw_write_held(&(_sc)->sc_lock)

#ifdef PPPOE_MPSAFE
#define DECLARE_SPLNET_VARIABLE
#define ACQUIRE_SPLNET()	do { } while (0)
#define RELEASE_SPLNET()	do { } while (0)
#else
#define DECLARE_SPLNET_VARIABLE	int __s
#define ACQUIRE_SPLNET()	do {					\
					__s = splnet();			\
				} while (0)
#define RELEASE_SPLNET()	do {					\
					splx(__s);			\
				} while (0)
#endif

#ifdef PPPOE_DEBUG
#define DPRINTF(_sc, _fmt, _arg...)	pppoe_printf((_sc), (_fmt), ##_arg)
#else
#define DPRINTF(_sc, _fmt, _arg...)	__nothing
#endif

struct pppoe_softc {
	struct sppp sc_sppp;		/* contains a struct ifnet as first element */
	LIST_ENTRY(pppoe_softc) sc_list;
	struct ifnet *sc_eth_if;	/* ethernet interface we are using */

	uint64_t sc_id;			/* id of this softc, our hunique */
	int sc_state;			/* discovery phase or session connected */
	struct ether_addr sc_dest;	/* hardware address of concentrator */
	uint16_t sc_session;		/* PPPoE session id */

	char *sc_service_name;		/* if != NULL: requested name of service */
	char *sc_concentrator_name;	/* if != NULL: requested concentrator id */
	uint8_t *sc_ac_cookie;		/* content of AC cookie we must echo back */
	size_t sc_ac_cookie_len;	/* length of cookie data */
	uint8_t *sc_relay_sid;		/* content of relay SID we must echo back */
	size_t sc_relay_sid_len;	/* length of relay SID data */
#ifdef PPPOE_SERVER
	uint8_t *sc_hunique;		/* content of host unique we must echo back */
	size_t sc_hunique_len;		/* length of host unique */
#endif
	callout_t sc_timeout;		/* timeout while not in session state */
	struct workqueue *sc_timeout_wq;	/* workqueue for timeout */
	struct work sc_timeout_wk;
	u_int sc_timeout_scheduled;
	int sc_padi_retried;		/* number of PADI retries already done */
	int sc_padr_retried;		/* number of PADR retries already done */
	krwlock_t sc_lock;	/* lock of sc_state, sc_session, and sc_eth_if */
	bool sc_detaching;
};

/* incoming traffic will be queued here */
struct ifqueue ppoediscinq = { .ifq_maxlen = IFQ_MAXLEN };
struct ifqueue ppoeinq = { .ifq_maxlen = IFQ_MAXLEN };

void *pppoe_softintr = NULL;
static void pppoe_softintr_handler(void *);

extern int sppp_ioctl(struct ifnet *, unsigned long, void *);

/* input routines */
static void pppoeintr(void);
static void pppoe_disc_input(struct mbuf *);
static void pppoe_dispatch_disc_pkt(struct mbuf *, int);
static void pppoe_data_input(struct mbuf *);
static void pppoe_enqueue(struct ifqueue *, struct mbuf *);

/* management routines */
static int pppoe_connect(struct pppoe_softc *);
static int pppoe_disconnect(struct pppoe_softc *);
static void pppoe_abort_connect(struct pppoe_softc *);
static int pppoe_ioctl(struct ifnet *, unsigned long, void *);
static void pppoe_tls(struct sppp *);
static void pppoe_tlf(struct sppp *);
static void pppoe_start(struct ifnet *);
#ifdef PPPOE_MPSAFE
static int pppoe_transmit(struct ifnet *, struct mbuf *);
#endif
static void pppoe_clear_softc(struct pppoe_softc *, const char *);
static void pppoe_printf(struct pppoe_softc *, const char *, ...);

/* internal timeout handling */
static void pppoe_timeout_co(void *);
static void pppoe_timeout_co_halt(void *);
static void pppoe_timeout_wk(struct work *, void *);
static void pppoe_timeout(struct pppoe_softc *);

/* sending actual protocol control packets */
static int pppoe_send_padi(struct pppoe_softc *);
static int pppoe_send_padr(struct pppoe_softc *);
#ifdef PPPOE_SERVER
static int pppoe_send_pado(struct pppoe_softc *);
static int pppoe_send_pads(struct pppoe_softc *);
#endif
static int pppoe_send_padt(struct ifnet *, u_int, const uint8_t *);

/* raw output */
static int pppoe_output(struct pppoe_softc *, struct mbuf *);

/* internal helper functions */
static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *,
    krw_t);
static struct pppoe_softc * pppoe_find_softc_by_hunique(uint8_t *, size_t,
    struct ifnet *, krw_t);
static struct mbuf *pppoe_get_mbuf(size_t len);

static void pppoe_ifattach_hook(void *, unsigned long, void *);

static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
static krwlock_t pppoe_softc_list_lock;

static int	pppoe_clone_create(struct if_clone *, int);
static int	pppoe_clone_destroy(struct ifnet *);

static bool	pppoe_term_unknown = false;
static int	pppoe_term_unknown_pps = 1;

static struct sysctllog	*pppoe_sysctl_clog;
static void sysctl_net_pppoe_setup(struct sysctllog **);

static struct if_clone pppoe_cloner =
    IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy);

/* ARGSUSED */
void
pppoeattach(int count)
{

	/*
	 * Nothing to do here, initialization is handled by the
	 * module initialization code in pppoeinit() below).
	 */
}

static void
pppoeinit(void)
{

	LIST_INIT(&pppoe_softc_list);
	rw_init(&pppoe_softc_list_lock);
	if_clone_attach(&pppoe_cloner);

	pppoe_softintr = softint_establish(SOFTINT_MPSAFE|SOFTINT_NET,
	    pppoe_softintr_handler, NULL);
	sysctl_net_pppoe_setup(&pppoe_sysctl_clog);

	IFQ_LOCK_INIT(&ppoediscinq);
	IFQ_LOCK_INIT(&ppoeinq);
}

static int
pppoedetach(void)
{
	int error = 0;

	rw_enter(&pppoe_softc_list_lock, RW_READER);
	if (!LIST_EMPTY(&pppoe_softc_list)) {
		rw_exit(&pppoe_softc_list_lock);
		error = EBUSY;
	}

	if (error == 0) {
		if_clone_detach(&pppoe_cloner);
		softint_disestablish(pppoe_softintr);
		/* Remove our sysctl sub-tree */
		sysctl_teardown(&pppoe_sysctl_clog);
	}

	return error;
}

static void
pppoe_softc_genid(uint64_t *id)
{
	struct pppoe_softc *sc;
	uint64_t rndid;

	rw_enter(&pppoe_softc_list_lock, RW_READER);

	while (1) {
		rndid = cprng_strong64();

		sc = NULL;
		LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
			if (sc->sc_id == rndid)
				break;
		}
		if (sc == NULL) {
			break;
		}
	}

	rw_exit(&pppoe_softc_list_lock);
	*id = rndid;
}

static int
pppoe_clone_create(struct if_clone *ifc, int unit)
{
	struct pppoe_softc *sc;
	struct ifnet *ifp;
	int rv;

	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
	ifp = &sc->sc_sppp.pp_if;

	rw_init(&sc->sc_lock);
	pppoe_softc_genid(&sc->sc_id);
	/* changed to real address later */
	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));

	if_initname(ifp, "pppoe", unit);
	ifp->if_softc = sc;
	ifp->if_mtu = PPPOE_MAXMTU;
	ifp->if_flags = IFF_SIMPLEX|IFF_POINTOPOINT|IFF_MULTICAST;
#ifdef PPPOE_MPSAFE
	ifp->if_extflags = IFEF_MPSAFE;
#endif
	ifp->if_type = IFT_PPP;
	ifp->if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN;
	ifp->if_dlt = DLT_PPP_ETHER;
	ifp->if_ioctl = pppoe_ioctl;
	ifp->if_start = pppoe_start;
#ifdef PPPOE_MPSAFE
	ifp->if_transmit = pppoe_transmit;
#endif
	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
	IFQ_SET_READY(&ifp->if_snd);

	sc->sc_sppp.pp_tls = pppoe_tls;
	sc->sc_sppp.pp_tlf = pppoe_tlf;
	sc->sc_sppp.pp_flags |= PP_KEEPALIVE |	/* use LCP keepalive */
				PP_NOFRAMING;	/* no serial encapsulation */
	sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN;	/* framing added to ppp packets */

	rv = workqueue_create(&sc->sc_timeout_wq,
	    ifp->if_xname, pppoe_timeout_wk, sc,
	    PRI_SOFTNET, IPL_SOFTNET, 0);
	if (rv != 0)
		goto destroy_sclock;

	callout_init(&sc->sc_timeout, CALLOUT_MPSAFE);
	callout_setfunc(&sc->sc_timeout, pppoe_timeout_co, sc);

	if_initialize(ifp);

	ifp->if_percpuq = if_percpuq_create(ifp);

	rw_enter(&pppoe_softc_list_lock, RW_READER);
	if (LIST_EMPTY(&pppoe_softc_list)) {
		pfil_add_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil);
	}
	LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
	rw_exit(&pppoe_softc_list_lock);

	sppp_attach(ifp);
	bpf_attach(ifp, DLT_PPP_ETHER, 0);
	if_register(ifp);

	return 0;

destroy_sclock:
	rw_destroy(&sc->sc_lock);
	kmem_free(sc, sizeof(*sc));

	return rv;
}

static int
pppoe_clone_destroy(struct ifnet *ifp)
{
	struct pppoe_softc * sc = ifp->if_softc;

	PPPOE_LOCK(sc, RW_WRITER);
	/* stop ioctls */
	sc->sc_detaching = true;

	if (ifp->if_flags & IFF_RUNNING) {
		pppoe_clear_softc(sc, "destroy interface");
		sc->sc_eth_if = NULL;
	}
	PPPOE_UNLOCK(sc);

	rw_enter(&pppoe_softc_list_lock, RW_WRITER);
	LIST_REMOVE(sc, sc_list);

	if (LIST_EMPTY(&pppoe_softc_list)) {
		pfil_remove_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil);
	}
	rw_exit(&pppoe_softc_list_lock);

	bpf_detach(ifp);
	sppp_detach(&sc->sc_sppp.pp_if);
	if_detach(ifp);

	callout_setfunc(&sc->sc_timeout, pppoe_timeout_co_halt, sc);

	workqueue_wait(sc->sc_timeout_wq, &sc->sc_timeout_wk);
	workqueue_destroy(sc->sc_timeout_wq);

	callout_halt(&sc->sc_timeout, NULL);
	callout_destroy(&sc->sc_timeout);

#ifdef PPPOE_SERVER
	if (sc->sc_hunique) {
		free(sc->sc_hunique, M_DEVBUF);
		sc->sc_hunique = NULL;
		sc->sc_hunique_len = 0;
	}
#endif
	if (sc->sc_concentrator_name)
		free(sc->sc_concentrator_name, M_DEVBUF);
	if (sc->sc_service_name)
		free(sc->sc_service_name, M_DEVBUF);
	if (sc->sc_ac_cookie)
		free(sc->sc_ac_cookie, M_DEVBUF);
	if (sc->sc_relay_sid)
		free(sc->sc_relay_sid, M_DEVBUF);

	rw_destroy(&sc->sc_lock);

	kmem_free(sc, sizeof(*sc));

	return 0;
}

static void
pppoe_printf(struct pppoe_softc *sc, const char *fmt, ...)
{
	va_list ap;
	bool pppoe_debug;

#ifdef PPPOE_DEBUG
	pppoe_debug = true;
#else
	pppoe_debug = false;
#endif

	if (sc == NULL) {
		if (!pppoe_debug)
			return;

		printf("pppoe: ");
	} else {
		if (!ISSET(sc->sc_sppp.pp_if.if_flags, IFF_DEBUG))
			return;

		printf("%s: ", sc->sc_sppp.pp_if.if_xname);
	}

	va_start(ap, fmt);
	vprintf(fmt, ap);
	va_end(ap);
}

/*
 * Find the interface handling the specified session.
 * Note: O(number of sessions open), this is a client-side only, mean
 * and lean implementation, so number of open sessions typically should
 * be 1.
 */
static struct pppoe_softc *
pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif, krw_t lock)
{
	struct pppoe_softc *sc = NULL;

	if (session == 0)
		return NULL;
	rw_enter(&pppoe_softc_list_lock, RW_READER);
	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
		PPPOE_LOCK(sc, lock);
		if ( sc->sc_state == PPPOE_STATE_SESSION
		    && sc->sc_session == session
		    && sc->sc_eth_if == rcvif)
			break;

		PPPOE_UNLOCK(sc);
	}
	rw_exit(&pppoe_softc_list_lock);
	return sc;
}

/* Check host unique token passed and return appropriate softc pointer,
 * or NULL if token is bogus. */
static struct pppoe_softc *
pppoe_find_softc_by_hunique(uint8_t *token, size_t len,
    struct ifnet *rcvif, krw_t lock)
{
	struct pppoe_softc *sc;
	uint64_t t;

	CTASSERT(sizeof(t) == sizeof(sc->sc_id));

	rw_enter(&pppoe_softc_list_lock, RW_READER);
	if (LIST_EMPTY(&pppoe_softc_list)) {
		rw_exit(&pppoe_softc_list_lock);
		return NULL;
	}

	if (len != sizeof(sc->sc_id)) {
		rw_exit(&pppoe_softc_list_lock);
		return NULL;
	}
	memcpy(&t, token, len);

	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
		PPPOE_LOCK(sc, lock);
		if (sc->sc_id == t && sc->sc_eth_if != NULL) {
			break;
		}
		PPPOE_UNLOCK(sc);
	}
	rw_exit(&pppoe_softc_list_lock);

	if (sc == NULL) {
		pppoe_printf(NULL, "alien host unique tag"
		    ", no session found\n");
		return NULL;
	}

	/* should be safe to access *sc now */
	if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
		pppoe_printf(sc, "host unique tag found"
		    ", but it belongs to a connection in state %d\n",
		    sc->sc_state);
		PPPOE_UNLOCK(sc);
		return NULL;
	}
	if (sc->sc_eth_if != rcvif) {
		pppoe_printf(sc, "wrong interface, not accepting host unique\n");
		PPPOE_UNLOCK(sc);
		return NULL;
	}
	return sc;
}

static void
pppoe_softintr_handler(void *dummy)
{
	/* called at splsoftnet() */
	pppoeintr();
}

/* called at appropriate protection level */
static void
pppoeintr(void)
{
	struct mbuf *m;
	int i;

	SOFTNET_LOCK_UNLESS_NET_MPSAFE();

	for (i = 0; i < PPPOE_DEQUEUE_MAXLEN; i++) {
		IFQ_LOCK(&ppoediscinq);
		IF_DEQUEUE(&ppoediscinq, m);
		IFQ_UNLOCK(&ppoediscinq);
		if (m == NULL)
			break;
		pppoe_disc_input(m);
	}

	for (i = 0; i < PPPOE_DEQUEUE_MAXLEN; i++) {
		IFQ_LOCK(&ppoeinq);
		IF_DEQUEUE(&ppoeinq, m);
		IFQ_UNLOCK(&ppoeinq);
		if (m == NULL)
			break;
		pppoe_data_input(m);
	}

#if PPPOE_DEQUEUE_MAXLEN < IFQ_MAXLEN
	if (!IF_IS_EMPTY(&ppoediscinq) || !IF_IS_EMPTY(&ppoeinq))
		softint_schedule(pppoe_softintr);
#endif

	SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();
}

/* analyze and handle a single received packet while not in session state */
static void
pppoe_dispatch_disc_pkt(struct mbuf *m, int off)
{
	uint16_t tag, len;
	uint16_t session, plen;
	struct pppoe_softc *sc;
	const char *err_msg;
	char *error;
	size_t dlen;
	uint8_t *ac_cookie;
	size_t ac_cookie_len;
	uint8_t *relay_sid;
	size_t relay_sid_len;
	uint8_t *hunique;
	size_t hunique_len;
	struct pppoehdr *ph;
	struct pppoetag *pt;
	struct mbuf *n;
	int noff, err, errortag;
	struct ether_header *eh;
	struct ifnet *rcvif;
	struct psref psref;

	if (m->m_len < sizeof(*eh)) {
		m = m_pullup(m, sizeof(*eh));
		if (m == NULL)
			goto done;
	}
	eh = mtod(m, struct ether_header *);
	off += sizeof(*eh);

	if (m->m_pkthdr.len - off < PPPOE_HEADERLEN) {
		goto done;
	}

	M_REGION_GET(ph, struct pppoehdr *, m, off, sizeof(*ph));
	if (ph == NULL) {
		goto done;
	}
	if (ph->vertype != PPPOE_VERTYPE) {
		goto done;
	}

	ac_cookie = NULL;
	ac_cookie_len = 0;
	relay_sid = NULL;
	relay_sid_len = 0;
	hunique = NULL;
	hunique_len = 0;

	session = ntohs(ph->session);
	plen = ntohs(ph->plen);
	off += sizeof(*ph);

	if (plen + off > m->m_pkthdr.len) {
		goto done;
	}
	m_adj(m, off + plen - m->m_pkthdr.len);	/* ignore trailing garbage */

	tag = 0;
	len = 0;
	sc = NULL;
	err_msg = NULL;
	errortag = 0;
	while (off + sizeof(*pt) <= m->m_pkthdr.len) {
		M_REGION_GET(pt, struct pppoetag *, m, off, sizeof(*pt));
		if (pt == NULL) {
			goto done;
		}

		tag = ntohs(pt->tag);
		len = ntohs(pt->len);
		if (off + len + sizeof(*pt) > m->m_pkthdr.len) {
			goto done;
		}
		switch (tag) {
		case PPPOE_TAG_EOL:
			goto breakbreak;
		case PPPOE_TAG_SNAME:
			break;	/* ignored */
		case PPPOE_TAG_ACNAME:
			if (len > 0) {
				dlen = 4 * len + 1;
				error = malloc(dlen, M_TEMP, M_NOWAIT);
				if (error == NULL)
					break;

				n = m_pulldown(m, off + sizeof(*pt), len,
				    &noff);
				if (!n) {
					m = NULL;
					free(error, M_TEMP);
					goto done;
				}

				strnvisx(error, dlen,
				    mtod(n, char*) + noff, len,
				    VIS_SAFE | VIS_OCTAL);
				pppoe_printf(NULL, "connected to %s\n", error);
				free(error, M_TEMP);
			}
			break;	/* ignored */
		case PPPOE_TAG_HUNIQUE:
			if (hunique == NULL) {
				n = m_pulldown(m, off + sizeof(*pt), len,
				    &noff);
				if (!n) {
					m = NULL;
					err_msg = "TAG HUNIQUE ERROR";
					break;
				}

				hunique = mtod(n, uint8_t *) + noff;
				hunique_len = len;
			}
			break;
		case PPPOE_TAG_ACCOOKIE:
			if (ac_cookie == NULL) {
				n = m_pulldown(m, off + sizeof(*pt), len,
				    &noff);
				if (!n) {
					err_msg = "TAG ACCOOKIE ERROR";
					m = NULL;
					break;
				}
				ac_cookie = mtod(n, char *) + noff;
				ac_cookie_len = len;
			}
			break;
		case PPPOE_TAG_RELAYSID:
			if (relay_sid == NULL) {
				n = m_pulldown(m, off + sizeof(*pt), len,
				    &noff);
				if (!n) {
					err_msg = "TAG RELAYSID ERROR";
					m = NULL;
					break;
				}
				relay_sid = mtod(n, char *) + noff;
				relay_sid_len = len;
			}
			break;
		case PPPOE_TAG_SNAME_ERR:
			err_msg = "SERVICE NAME ERROR";
			errortag = 1;
			break;
		case PPPOE_TAG_ACSYS_ERR:
			err_msg = "AC SYSTEM ERROR";
			errortag = 1;
			break;
		case PPPOE_TAG_GENERIC_ERR:
			err_msg = "GENERIC ERROR";
			errortag = 1;
			break;
		}
		if (err_msg) {
			error = NULL;
			if (errortag && len) {
				dlen = 4 * len + 1;
				error = malloc(dlen, M_TEMP,
				    M_NOWAIT|M_ZERO);
				n = m_pulldown(m, off + sizeof(*pt), len,
				    &noff);
				if (!n) {
					m = NULL;
				} else if (error) {
					strnvisx(error, dlen,
					    mtod(n, char*) + noff, len,
					    VIS_SAFE | VIS_OCTAL);
				}
			}
			if (error) {
				pppoe_printf(NULL, "%s: %s\n", err_msg, error);
				free(error, M_TEMP);
			} else
				pppoe_printf(NULL, "%s\n", err_msg);
			if (errortag || m == NULL)
				goto done;
		}
		off += sizeof(*pt) + len;
	}
breakbreak:;

	switch (ph->code) {
	case PPPOE_CODE_PADI:
#ifdef PPPOE_SERVER
		/*
		 * got service name, concentrator name, and/or host unique.
		 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
		 */
		rw_enter(&pppoe_softc_list_lock, RW_READER);
		if (LIST_EMPTY(&pppoe_softc_list)) {
			rw_exit(&pppoe_softc_list_lock);
			goto done;
		}

		LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
			PPPOE_LOCK(sc, RW_WRITER);
			if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
				PPPOE_UNLOCK(sc);
				continue;
			}
			if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
				PPPOE_UNLOCK(sc);
				continue;
			}

			if (sc->sc_state == PPPOE_STATE_INITIAL)
				break;

			PPPOE_UNLOCK(sc);
		}
		rw_exit(&pppoe_softc_list_lock);

		if (sc == NULL) {
			goto done;
		}

		if (hunique) {
			if (sc->sc_hunique)
				free(sc->sc_hunique, M_DEVBUF);
			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
			    M_DONTWAIT);
			if (sc->sc_hunique == NULL) {
				PPPOE_UNLOCK(sc);
				goto done;
			}
			sc->sc_hunique_len = hunique_len;
			memcpy(sc->sc_hunique, hunique, hunique_len);
		}
		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
		sc->sc_state = PPPOE_STATE_PADO_SENT;
		pppoe_send_pado(sc);
		PPPOE_UNLOCK(sc);
		break;
#endif /* PPPOE_SERVER */

	case PPPOE_CODE_PADR:
#ifdef PPPOE_SERVER
		/*
		 * get sc from ac_cookie if IFF_PASSIVE
		 */
		if (ac_cookie == NULL) {
			goto done;
		}

		rcvif = m_get_rcvif_psref(m, &psref);
		if (__predict_true(rcvif != NULL)) {
			sc = pppoe_find_softc_by_hunique(ac_cookie,
			    ac_cookie_len, rcvif, RW_WRITER);
		}
		m_put_rcvif_psref(rcvif, &psref);
		if (sc == NULL) {
			/* be quiet if there is not a single pppoe instance */
			rw_enter(&pppoe_softc_list_lock, RW_READER);
			if (!LIST_EMPTY(&pppoe_softc_list)) {
				pppoe_printf(NULL, "received PADR"
				    " but could not find request for it\n");
			}
			rw_exit(&pppoe_softc_list_lock);
			goto done;
		}

		if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
			pppoe_printf(sc, "received unexpected PADR\n");
			PPPOE_UNLOCK(sc);
			goto done;
		}

		if (hunique) {
			if (sc->sc_hunique)
				free(sc->sc_hunique, M_DEVBUF);
			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
			    M_DONTWAIT);
			if (sc->sc_hunique == NULL) {
				PPPOE_UNLOCK(sc);
				goto done;
			}
			sc->sc_hunique_len = hunique_len;
			memcpy(sc->sc_hunique, hunique, hunique_len);
		}
		pppoe_send_pads(sc);
		sc->sc_state = PPPOE_STATE_SESSION;
		PPPOE_UNLOCK(sc);

		sc->sc_sppp.pp_up(&sc->sc_sppp);
		break;
#else
		/* ignore, we are no access concentrator */
		goto done;
#endif /* PPPOE_SERVER */

	case PPPOE_CODE_PADO:
		rcvif = m_get_rcvif_psref(m, &psref);
		if (__predict_false(rcvif == NULL))
			goto done;

		if (hunique != NULL) {
			sc = pppoe_find_softc_by_hunique(hunique,
			    hunique_len, rcvif, RW_WRITER);
		}

		m_put_rcvif_psref(rcvif, &psref);

		if (sc == NULL) {
			/* be quiet if there is not a single pppoe instance */
			rw_enter(&pppoe_softc_list_lock, RW_READER);
			if (!LIST_EMPTY(&pppoe_softc_list)) {
				pppoe_printf(NULL, "received PADO"
				    " but could not find request for it\n");
			}
			rw_exit(&pppoe_softc_list_lock);
			goto done;
		}

		if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
			pppoe_printf(sc, "received unexpected PADO\n");
			PPPOE_UNLOCK(sc);
			goto done;
		}

		if (ac_cookie) {
			if (sc->sc_ac_cookie)
				free(sc->sc_ac_cookie, M_DEVBUF);
			sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
			    M_DONTWAIT);
			if (sc->sc_ac_cookie == NULL) {
				pppoe_printf(sc, "FATAL: could not allocate memory "
				    "for AC cookie\n");
				sc->sc_ac_cookie_len = 0;
				PPPOE_UNLOCK(sc);
				goto done;
			}
			sc->sc_ac_cookie_len = ac_cookie_len;
			memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
		} else if (sc->sc_ac_cookie) {
			free(sc->sc_ac_cookie, M_DEVBUF);
			sc->sc_ac_cookie = NULL;
			sc->sc_ac_cookie_len = 0;
		}
		if (relay_sid) {
			if (sc->sc_relay_sid)
				free(sc->sc_relay_sid, M_DEVBUF);
			sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF,
			    M_DONTWAIT);
			if (sc->sc_relay_sid == NULL) {
				pppoe_printf(sc, "FATAL: could not allocate memory "
				    "for relay SID\n");
				sc->sc_relay_sid_len = 0;
				PPPOE_UNLOCK(sc);
				goto done;
			}
			sc->sc_relay_sid_len = relay_sid_len;
			memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len);
		} else if (sc->sc_relay_sid) {
			free(sc->sc_relay_sid, M_DEVBUF);
			sc->sc_relay_sid = NULL;
			sc->sc_relay_sid_len = 0;
		}
		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
		callout_stop(&sc->sc_timeout);
		sc->sc_padr_retried = 0;
		sc->sc_state = PPPOE_STATE_PADR_SENT;
		if ((err = pppoe_send_padr(sc)) != 0) {
			if (err == ENOBUFS) {
				pppoe_printf(sc, "PADO is too large, drop it\n");
				/*
				 * Cannot send PADR generated by received PADO,
				 * so restart from sending PAD*I*.
				 */
				sc->sc_state = PPPOE_STATE_PADI_SENT;
				callout_schedule(&sc->sc_timeout,
				    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried));
				PPPOE_UNLOCK(sc);
				goto done;
			}

			pppoe_printf(sc,
			    "failed to send PADR, error=%d\n", err);
		}
		callout_schedule(&sc->sc_timeout,
		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));

		PPPOE_UNLOCK(sc);
		break;

	case PPPOE_CODE_PADS:
		rcvif = m_get_rcvif_psref(m, &psref);
		if (__predict_false(rcvif == NULL))
			goto done;

		if (hunique != NULL) {
			sc = pppoe_find_softc_by_hunique(hunique,
			    hunique_len, rcvif, RW_WRITER);
		}

		m_put_rcvif_psref(rcvif, &psref);

		if (sc == NULL)
			goto done;

		if (memcmp(&sc->sc_dest, eh->ether_shost,
		    sizeof sc->sc_dest) != 0) {
			PPPOE_UNLOCK(sc);
			goto done;
		}

		sc->sc_session = session;
		callout_stop(&sc->sc_timeout);
		pppoe_printf(sc, "session 0x%x connected\n", session);
		sc->sc_state = PPPOE_STATE_SESSION;
		PPPOE_UNLOCK(sc);

		sc->sc_sppp.pp_up(&sc->sc_sppp);	/* notify upper layers */
		break;

	case PPPOE_CODE_PADT:
		rcvif = m_get_rcvif_psref(m, &psref);
		if (__predict_false(rcvif == NULL))
			goto done;

		sc = pppoe_find_softc_by_session(session, rcvif,
		    RW_WRITER);

		m_put_rcvif_psref(rcvif, &psref);

		if (sc == NULL)
			goto done;

		if (memcmp(&sc->sc_dest, eh->ether_shost,
		    sizeof sc->sc_dest) != 0) {
			PPPOE_UNLOCK(sc);
			goto done;
		}

		pppoe_clear_softc(sc, "received PADT");
		if (sc->sc_sppp.pp_if.if_flags & IFF_RUNNING) {
			pppoe_printf(sc, "wait for reconnect\n");
			callout_schedule(&sc->sc_timeout,
			    PPPOE_RECON_PADTRCVD);
		}
		PPPOE_UNLOCK(sc);
		break;

	default:
		rcvif = m_get_rcvif_psref(m, &psref);
		if (__predict_false(rcvif == NULL))
			goto done;

		if (hunique != NULL) {
			sc = pppoe_find_softc_by_hunique(hunique,
			    hunique_len, rcvif, RW_READER);
		}

		m_put_rcvif_psref(rcvif, &psref);

		pppoe_printf(sc, "unknown code (0x%04x) session = 0x%04x\n",
		    ph->code, session);
		if (sc == NULL)
			goto done;
		PPPOE_UNLOCK(sc);
		break;
	}

done:
	if (m)
		m_freem(m);
	return;
}

static void
pppoe_disc_input(struct mbuf *m)
{
	KASSERT(m->m_flags & M_PKTHDR);

	/*
	 * Avoid error messages if there is not a single PPPoE instance.
	 */
	rw_enter(&pppoe_softc_list_lock, RW_READER);
	if (!LIST_EMPTY(&pppoe_softc_list)) {
		rw_exit(&pppoe_softc_list_lock);
		pppoe_dispatch_disc_pkt(m, 0);
	} else {
		rw_exit(&pppoe_softc_list_lock);
		m_freem(m);
	}
}

static bool
pppoe_is_my_frame(uint8_t *dhost, struct ifnet *rcvif)
{

	if (memcmp(CLLADDR(rcvif->if_sadl), dhost, ETHER_ADDR_LEN) == 0)
		return true;

	return false;
}

static void
pppoe_data_input(struct mbuf *m)
{
	uint16_t session, plen;
	struct pppoe_softc *sc;
	struct pppoehdr *ph;
	struct ifnet *rcvif;
	struct psref psref;
	uint8_t shost[ETHER_ADDR_LEN];
	uint8_t dhost[ETHER_ADDR_LEN];
	bool term_unknown = pppoe_term_unknown;

	KASSERT(m->m_flags & M_PKTHDR);

	/*
	 * Avoid error messages if there is not a single PPPoE instance.
	 */
	rw_enter(&pppoe_softc_list_lock, RW_READER);
	if (LIST_EMPTY(&pppoe_softc_list)) {
		rw_exit(&pppoe_softc_list_lock);
		goto drop;
	}
	rw_exit(&pppoe_softc_list_lock);

	if (term_unknown) {
		memcpy(shost, mtod(m, struct ether_header*)->ether_shost,
		    ETHER_ADDR_LEN);
		memcpy(dhost, mtod(m, struct ether_header*)->ether_dhost,
		    ETHER_ADDR_LEN);
	}
	m_adj(m, sizeof(struct ether_header));
	if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
		goto drop;
	}

	if (m->m_len < sizeof(*ph)) {
		m = m_pullup(m, sizeof(*ph));
		if (m == NULL) {
			return;
		}
	}
	ph = mtod(m, struct pppoehdr *);

	if (ph->vertype != PPPOE_VERTYPE) {
		goto drop;
	}
	if (ph->code != 0) {
		goto drop;
	}

	session = ntohs(ph->session);
	rcvif = m_get_rcvif_psref(m, &psref);
	if (__predict_false(rcvif == NULL))
		goto drop;
	sc = pppoe_find_softc_by_session(session, rcvif, RW_READER);
	if (sc == NULL) {
		if (term_unknown) {
			static struct timeval lasttime = {0, 0};
			static int curpps = 0;
			/*
			 * avoid to send wrong PADT which is response from
			 * session stage packets for other hosts when parent
			 * ethernet is promiscuous mode.
			 */
			if (pppoe_is_my_frame(dhost, rcvif) &&
			    ppsratecheck(&lasttime, &curpps,
				pppoe_term_unknown_pps)) {
				pppoe_printf(NULL, "input for unknown session %#x, "
				    "sending PADT\n", session);
				pppoe_send_padt(rcvif, session, shost);
			}
		}
		m_put_rcvif_psref(rcvif, &psref);
		goto drop;
	}

	m_put_rcvif_psref(rcvif, &psref);

	plen = ntohs(ph->plen);

	bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_IN);

	m_adj(m, PPPOE_HEADERLEN);

#ifdef PPPOE_DEBUG
	{
		struct mbuf *p;

		printf("%s: pkthdr.len=%d, pppoe.len=%d",
		    sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen);
		p = m;
		while (p) {
			printf(" l=%d", p->m_len);
			p = p->m_next;
		}
		printf("\n");
	}
#endif
	PPPOE_UNLOCK(sc);

	if (m->m_pkthdr.len < plen)
		goto drop;

	/* ignore trailing garbage */
	m_adj(m, plen - m->m_pkthdr.len);
	/*
	 * Fix incoming interface pointer (not the raw ethernet interface
	 * anymore)
	 */
	m_set_rcvif(m, &sc->sc_sppp.pp_if);

	/* pass packet up and account for it */
	if_statinc(&sc->sc_sppp.pp_if, if_ipackets);
	sppp_input(&sc->sc_sppp.pp_if, m);
	return;

drop:
	m_freem(m);
}

static int
pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
{
	struct sockaddr dst;
	struct ether_header *eh;
	uint16_t etype;

	if (sc->sc_eth_if == NULL) {
		m_freem(m);
		return EIO;
	}

	memset(&dst, 0, sizeof dst);
	dst.sa_family = AF_UNSPEC;
	eh = (struct ether_header*)&dst.sa_data;
	etype = sc->sc_state == PPPOE_STATE_SESSION
	    ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
	eh->ether_type = htons(etype);
	memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);

	DPRINTF(sc, "(%x) state=%d, session=0x%x output -> %s, len=%d\n",
	    etype, sc->sc_state, sc->sc_session,
	    ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);

	m->m_flags &= ~(M_BCAST|M_MCAST);
	if_statinc(&sc->sc_sppp.pp_if, if_opackets);
	return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL);
}

static int
pppoe_parm_cpyinstr(struct pppoe_softc *sc,
    char **dst, const void *src, size_t len)
{
	int error = 0;
	char *next = NULL;
	size_t bufsiz, cpysiz, strsiz;

	bufsiz = len + 1;

	if (src == NULL)
		goto out;

	bufsiz = len + 1;
	next = malloc(bufsiz, M_DEVBUF, M_WAITOK);
	if (next == NULL)
		return ENOMEM;

	error = copyinstr(src, next, bufsiz, &cpysiz);
	if (error != 0)
		goto fail;
	if (cpysiz != bufsiz) {
		error = EINVAL;
		goto fail;
	}

	strsiz = strnlen(next, bufsiz);
	if (strsiz == bufsiz) {
		error = EINVAL;
		goto fail;
	}

out:
	PPPOE_LOCK(sc, RW_WRITER);
	if (*dst != NULL)
		free(*dst, M_DEVBUF);
	*dst = next;
	next = NULL;
	PPPOE_UNLOCK(sc);
fail:
	if (next != NULL)
		free(next, M_DEVBUF);

	return error;
}

static int
pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
{
	struct lwp *l = curlwp;	/* XXX */
	struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
	struct ifreq *ifr = data;
	int error = 0;

	switch (cmd) {
	case PPPOESETPARMS:
	{
		struct pppoediscparms *parms = (struct pppoediscparms*)data;
		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
		    NULL) != 0)
			return EPERM;
		if (parms->eth_ifname[0] != 0) {
			struct ifnet *eth_if;

			PPPOE_LOCK(sc, RW_WRITER);
			if (sc->sc_detaching) {
				PPPOE_UNLOCK(sc);
				return ENXIO;
			}
			eth_if = ifunit(parms->eth_ifname);
			if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
				sc->sc_eth_if = NULL;
				PPPOE_UNLOCK(sc);
				return ENXIO;
			}

			if (sc->sc_sppp.pp_if.if_mtu !=
			    eth_if->if_mtu - PPPOE_OVERHEAD) {
				sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
				    PPPOE_OVERHEAD;
			}
			sc->sc_eth_if = eth_if;
			PPPOE_UNLOCK(sc);
		}

		error = pppoe_parm_cpyinstr(sc, &sc->sc_concentrator_name,
		    parms->ac_name, parms->ac_name_len);
		if (error != 0)
			return error;

		error = pppoe_parm_cpyinstr(sc, &sc->sc_service_name,
		    parms->service_name, parms->service_name_len);
		if (error != 0)
			return error;
		return 0;
	}
	break;
	case PPPOEGETPARMS:
	{
		struct pppoediscparms *parms = (struct pppoediscparms*)data;
		memset(parms, 0, sizeof *parms);
		PPPOE_LOCK(sc, RW_READER);
		if (sc->sc_eth_if)
			strlcpy(parms->ifname, sc->sc_eth_if->if_xname,
			    sizeof(parms->ifname));
		PPPOE_UNLOCK(sc);
		return 0;
	}
	break;
	case PPPOEGETSESSION:
	{
		struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
		PPPOE_LOCK(sc, RW_READER);
		state->state = sc->sc_state;
		state->session_id = sc->sc_session;
		state->padi_retry_no = sc->sc_padi_retried;
		state->padr_retry_no = sc->sc_padr_retried;
		PPPOE_UNLOCK(sc);
		return 0;
	}
	break;
	case SIOCSIFFLAGS:
		/*
		 * Prevent running re-establishment timers overriding
		 * administrators choice.
		 */
		PPPOE_LOCK(sc, RW_WRITER);
		if (sc->sc_detaching) {
			PPPOE_UNLOCK(sc);
			return ENXIO;
		}

		if ((ifr->ifr_flags & IFF_UP) == 0
		     && sc->sc_state < PPPOE_STATE_SESSION) {
			callout_stop(&sc->sc_timeout);
			sc->sc_state = PPPOE_STATE_INITIAL;
			sc->sc_padi_retried = 0;
			sc->sc_padr_retried = 0;
			memcpy(&sc->sc_dest, etherbroadcastaddr,
			    sizeof(sc->sc_dest));
		}

		PPPOE_UNLOCK(sc);

		error = sppp_ioctl(ifp, cmd, data);

		return error;
	case SIOCSIFMTU:
		if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
		    PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
			return EINVAL;
		}
		/*FALLTHROUGH*/
	default:
		return sppp_ioctl(ifp, cmd, data);
	}
	return 0;
}

/*
 * Allocate a mbuf/cluster with space to store the given data length
 * of payload, leaving space for prepending an ethernet header
 * in front.
 */
static struct mbuf *
pppoe_get_mbuf(size_t len)
{
	struct mbuf *m;

	if (len + sizeof(struct ether_header) > MCLBYTES)
		return NULL;

	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == NULL)
		return NULL;
	if (len + sizeof(struct ether_header) > MHLEN) {
		MCLGET(m, M_DONTWAIT);
		if ((m->m_flags & M_EXT) == 0) {
			m_free(m);
			return NULL;
		}
	}
	m->m_data += sizeof(struct ether_header);
	m->m_len = len;
	m->m_pkthdr.len = len;
	m_reset_rcvif(m);

	return m;
}

static int
pppoe_send_padi(struct pppoe_softc *sc)
{
	struct mbuf *m0;
	int len, l1 = 0, l2 = 0;
	uint8_t *p;

	if (sc->sc_state > PPPOE_STATE_PADI_SENT)
		panic("pppoe_send_padi in state %d", sc->sc_state);

	/* Compute packet length. */
	len = sizeof(struct pppoetag);
	if (sc->sc_service_name != NULL) {
		l1 = strlen(sc->sc_service_name);
		len += l1;
	}
	if (sc->sc_concentrator_name != NULL) {
		l2 = strlen(sc->sc_concentrator_name);
		len += sizeof(struct pppoetag) + l2;
	}
	len += sizeof(struct pppoetag) + sizeof(sc->sc_id);
	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
		len += sizeof(struct pppoetag) + 2;
	}

	/* Allocate packet. */
	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
	if (m0 == NULL)
		return ENOBUFS;

	/* Fill in packet. */
	p = mtod(m0, uint8_t *);
	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
	if (sc->sc_service_name != NULL) {
		PPPOE_ADD_16(p, l1);
		memcpy(p, sc->sc_service_name, l1);
		p += l1;
	} else {
		PPPOE_ADD_16(p, 0);
	}
	if (sc->sc_concentrator_name != NULL) {
		PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
		PPPOE_ADD_16(p, l2);
		memcpy(p, sc->sc_concentrator_name, l2);
		p += l2;
	}
	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
	PPPOE_ADD_16(p, sizeof(sc->sc_id));
	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
	p += sizeof(sc->sc_id);

	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
		PPPOE_ADD_16(p, 2);
		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
	}

#ifdef PPPOE_DEBUG
	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
		panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
		    (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
#endif

	/* Send packet. */
	return pppoe_output(sc, m0);
}

static void
pppoe_timeout_co(void *arg)
{
	struct pppoe_softc *sc = (struct pppoe_softc *)arg;

	if (atomic_swap_uint(&sc->sc_timeout_scheduled, 1) != 0)
		return;

	workqueue_enqueue(sc->sc_timeout_wq, &sc->sc_timeout_wk, NULL);
}

static void
pppoe_timeout_co_halt(void *unused __unused)
{

	/* do nothing to halt callout safely */
}

static void
pppoe_timeout_wk(struct work *wk __unused, void *arg)
{
	struct pppoe_softc *sc = (struct pppoe_softc *)arg;

	atomic_swap_uint(&sc->sc_timeout_scheduled, 0);
	pppoe_timeout(sc);
}

static void
pppoe_timeout(struct pppoe_softc *sc)
{
	int retry_wait, err;
	DECLARE_SPLNET_VARIABLE;

	pppoe_printf(sc, "timeout\n");

	PPPOE_LOCK(sc, RW_WRITER);
	switch (sc->sc_state) {
	case PPPOE_STATE_INITIAL:
		/* delayed connect from pppoe_tls() */
		if (!sc->sc_detaching)
			pppoe_connect(sc);
		break;
	case PPPOE_STATE_PADI_SENT:
		/*
		 * We have two basic ways of retrying:
		 *  - Quick retry mode: try a few times in short sequence
		 *  - Slow retry mode: we already had a connection successfully
		 *    established and will try infinitely (without user
		 *    intervention)
		 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
		 * is not set.
		 */

		/* initialize for quick retry mode */
		retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);

		ACQUIRE_SPLNET();
		sc->sc_padi_retried++;
		if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
			if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
				/* slow retry mode */
				retry_wait = PPPOE_SLOW_RETRY;
			} else {
				pppoe_abort_connect(sc);
				RELEASE_SPLNET();
				PPPOE_UNLOCK(sc);
				return;
			}
		}
		if ((err = pppoe_send_padi(sc)) != 0) {
			sc->sc_padi_retried--;
			pppoe_printf(sc,
			    "failed to transmit PADI, error=%d\n", err);
		}
		callout_schedule(&sc->sc_timeout,retry_wait);
		RELEASE_SPLNET();
		break;

	case PPPOE_STATE_PADR_SENT:
		ACQUIRE_SPLNET();
		sc->sc_padr_retried++;
		if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
			memcpy(&sc->sc_dest, etherbroadcastaddr,
			    sizeof(sc->sc_dest));
			sc->sc_state = PPPOE_STATE_PADI_SENT;
			sc->sc_padi_retried = 0;
			sc->sc_padr_retried = 0;
			if ((err = pppoe_send_padi(sc)) != 0) {
				pppoe_printf(sc,
				    "failed to send PADI, error=%d\n", err);
			}
			callout_schedule(&sc->sc_timeout,
			    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried));
			RELEASE_SPLNET();
			PPPOE_UNLOCK(sc);
			return;
		}
		if ((err = pppoe_send_padr(sc)) != 0) {
			pppoe_printf(sc,"failed to send PADR, error=%d", err);
		}
		callout_schedule(&sc->sc_timeout,
		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));
		RELEASE_SPLNET();
		break;
	case PPPOE_STATE_CLOSING:
		pppoe_disconnect(sc);
		break;
	default:
		PPPOE_UNLOCK(sc);
		return;	/* all done, work in peace */
	}
	PPPOE_UNLOCK(sc);
}

/* Start a connection (i.e. initiate discovery phase) */
static int
pppoe_connect(struct pppoe_softc *sc)
{
	int err;
	DECLARE_SPLNET_VARIABLE;

	KASSERT(PPPOE_WLOCKED(sc));

	if (sc->sc_state != PPPOE_STATE_INITIAL)
		return EBUSY;

#ifdef PPPOE_SERVER
	/* wait PADI if IFF_PASSIVE */
	if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
		return 0;
#endif
	ACQUIRE_SPLNET();
	/* save state, in case we fail to send PADI */
	sc->sc_state = PPPOE_STATE_PADI_SENT;
	sc->sc_padi_retried = 0;
	sc->sc_padr_retried = 0;
	err = pppoe_send_padi(sc);
	if (err != 0)
		pppoe_printf(sc, "failed to send PADI, error=%d\n", err);
	callout_schedule(&sc->sc_timeout, PPPOE_DISC_TIMEOUT);
	RELEASE_SPLNET();
	return err;
}

/* disconnect */
static int
pppoe_disconnect(struct pppoe_softc *sc)
{
	int err;
	DECLARE_SPLNET_VARIABLE;

	KASSERT(PPPOE_WLOCKED(sc));

	ACQUIRE_SPLNET();

	if (sc->sc_state < PPPOE_STATE_SESSION)
		err = EBUSY;
	else {
		pppoe_printf(sc, "disconnecting\n");
		err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session,
		    (const uint8_t *)&sc->sc_dest);
	}

	/* cleanup softc */
	sc->sc_state = PPPOE_STATE_INITIAL;

	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
	if (sc->sc_ac_cookie) {
		free(sc->sc_ac_cookie, M_DEVBUF);
		sc->sc_ac_cookie = NULL;
	}
	sc->sc_ac_cookie_len = 0;
	if (sc->sc_relay_sid) {
		free(sc->sc_relay_sid, M_DEVBUF);
		sc->sc_relay_sid = NULL;
	}
	sc->sc_relay_sid_len = 0;
#ifdef PPPOE_SERVER
	if (sc->sc_hunique) {
		free(sc->sc_hunique, M_DEVBUF);
		sc->sc_hunique = NULL;
	}
	sc->sc_hunique_len = 0;
#endif
	sc->sc_session = 0;

	PPPOE_UNLOCK(sc);

	/* notify upper layer */
	sc->sc_sppp.pp_down(&sc->sc_sppp);

	PPPOE_LOCK(sc, RW_WRITER);

	RELEASE_SPLNET();
	return err;
}

/* Connection attempt aborted */
static void
pppoe_abort_connect(struct pppoe_softc *sc)
{
	KASSERT(PPPOE_WLOCKED(sc));

	pppoe_printf(sc, "could not establish connection\n");
	sc->sc_state = PPPOE_STATE_CLOSING;

	PPPOE_UNLOCK(sc);

	/* notify upper layer */
	sc->sc_sppp.pp_down(&sc->sc_sppp);

	PPPOE_LOCK(sc, RW_WRITER);

	/* clear connection state */
	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
	sc->sc_state = PPPOE_STATE_INITIAL;
}

static int
pppoe_send_padr(struct pppoe_softc *sc)
{
	struct mbuf *m0;
	uint8_t *p;
	size_t len, l1 = 0;

	if (sc->sc_state != PPPOE_STATE_PADR_SENT)
		return EIO;

	/* Compute packet length. */
	len = sizeof(struct pppoetag);
	if (sc->sc_service_name != NULL) {
		l1 = strlen(sc->sc_service_name);
		len += l1;
	}
	if (sc->sc_ac_cookie_len > 0) {
		len += sizeof(struct pppoetag) + sc->sc_ac_cookie_len;
	}
	if (sc->sc_relay_sid_len > 0) {
		len += sizeof(struct pppoetag) + sc->sc_relay_sid_len;
	}
	len += sizeof(struct pppoetag) + sizeof(sc->sc_id);
	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
		len += sizeof(struct pppoetag) + 2;
	}

	/* Allocate packet. */
	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
	if (m0 == NULL)
		return ENOBUFS;

	/* Fill in packet. */
	p = mtod(m0, uint8_t *);
	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
	if (sc->sc_service_name != NULL) {
		PPPOE_ADD_16(p, l1);
		memcpy(p, sc->sc_service_name, l1);
		p += l1;
	} else {
		PPPOE_ADD_16(p, 0);
	}
	if (sc->sc_ac_cookie_len > 0) {
		PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
		PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
		memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
		p += sc->sc_ac_cookie_len;
	}
	if (sc->sc_relay_sid_len > 0) {
		PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
		PPPOE_ADD_16(p, sc->sc_relay_sid_len);
		memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
		p += sc->sc_relay_sid_len;
	}
	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
	PPPOE_ADD_16(p, sizeof(sc->sc_id));
	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
	p += sizeof(sc->sc_id);

	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
		PPPOE_ADD_16(p, 2);
		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
	}

#ifdef PPPOE_DEBUG
	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
		panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
			(long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
#endif

	/* Send packet. */
	return pppoe_output(sc, m0);
}

/* send a PADT packet */
static int
pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
{
	struct ether_header *eh;
	struct sockaddr dst;
	struct mbuf *m0;
	uint8_t *p;

	m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
	if (!m0)
		return EIO;
	p = mtod(m0, uint8_t *);
	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);

	memset(&dst, 0, sizeof dst);
	dst.sa_family = AF_UNSPEC;
	eh = (struct ether_header*)&dst.sa_data;
	eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
	memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);

	m0->m_flags &= ~(M_BCAST|M_MCAST);
	return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL);
}

#ifdef PPPOE_SERVER
static int
pppoe_send_pado(struct pppoe_softc *sc)
{
	struct mbuf *m0;
	uint8_t *p;
	size_t len;

	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
		return EIO;

	/* Include AC cookie. */
	len = sizeof(struct pppoetag) + sizeof(sc->sc_id);
	/* Include hunique. */
	len += sizeof(struct pppoetag) + sc->sc_hunique_len;

	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
	if (!m0)
		return EIO;
	p = mtod(m0, uint8_t *);

	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
	PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
	PPPOE_ADD_16(p, sizeof(sc->sc_id));
	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
	p += sizeof(sc->sc_id);
	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
	PPPOE_ADD_16(p, sc->sc_hunique_len);
	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
	return pppoe_output(sc, m0);
}

static int
pppoe_send_pads(struct pppoe_softc *sc)
{
	struct bintime bt;
	struct mbuf *m0;
	uint8_t *p;
	size_t len, l1 = 0;	/* XXX: gcc */

	KASSERT(PPPOE_WLOCKED(sc));

	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
		return EIO;

	getbinuptime(&bt);
	sc->sc_session = bt.sec % 0xff + 1;

	/* Include service name. */
	len = sizeof(struct pppoetag);
	if (sc->sc_service_name != NULL) {
		l1 = strlen(sc->sc_service_name);
		len += l1;
	}
	/* Include hunique. */
	len += sizeof(struct pppoetag) + sc->sc_hunique_len;

	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
	if (!m0)
		return ENOBUFS;
	p = mtod(m0, uint8_t *);

	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
	if (sc->sc_service_name != NULL) {
		PPPOE_ADD_16(p, l1);
		memcpy(p, sc->sc_service_name, l1);
		p += l1;
	} else {
		PPPOE_ADD_16(p, 0);
	}
	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
	PPPOE_ADD_16(p, sc->sc_hunique_len);
	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
	return pppoe_output(sc, m0);
}
#endif

static void
pppoe_tls(struct sppp *sp)
{
	struct pppoe_softc *sc = (void *)sp;
	int wtime;

	PPPOE_LOCK(sc, RW_READER);

	if (sc->sc_state != PPPOE_STATE_INITIAL) {
		PPPOE_UNLOCK(sc);
		return;
	}

	if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
	    sc->sc_sppp.pp_auth_failures > 0) {
		/*
		 * Delay trying to reconnect a bit more - the peer
		 * might have failed to contact its radius server.
		 */
		wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
		if (wtime > PPPOE_SLOW_RETRY)
			wtime = PPPOE_SLOW_RETRY;
	} else {
		wtime = PPPOE_RECON_IMMEDIATE;
	}
	callout_schedule(&sc->sc_timeout, wtime);

	PPPOE_UNLOCK(sc);
}

static void
pppoe_tlf(struct sppp *sp)
{
	struct pppoe_softc *sc = (void *)sp;

	PPPOE_LOCK(sc, RW_WRITER);

	if (sc->sc_state < PPPOE_STATE_SESSION) {
		callout_stop(&sc->sc_timeout);
		sc->sc_state = PPPOE_STATE_INITIAL;
		sc->sc_padi_retried = 0;
		sc->sc_padr_retried = 0;
		memcpy(&sc->sc_dest, etherbroadcastaddr,
		    sizeof(sc->sc_dest));
		PPPOE_UNLOCK(sc);
		return;
	}

	/*
	 * Do not call pppoe_disconnect here, the upper layer state
	 * machine gets confused by this. We must return from this
	 * function and defer disconnecting to the timeout handler.
	 */
	sc->sc_state = PPPOE_STATE_CLOSING;

	callout_schedule(&sc->sc_timeout, hz/50);

	PPPOE_UNLOCK(sc);
}

static void
pppoe_start(struct ifnet *ifp)
{
	struct pppoe_softc *sc = (void *)ifp;
	struct mbuf *m;
	uint8_t *p;
	size_t len;

	if (sppp_isempty(ifp))
		return;

	/* are we ready to process data yet? */
	PPPOE_LOCK(sc, RW_READER);
	if (sc->sc_state < PPPOE_STATE_SESSION) {
		sppp_flush(&sc->sc_sppp.pp_if);
		PPPOE_UNLOCK(sc);
		return;
	}

	while ((m = sppp_dequeue(ifp)) != NULL) {
		len = m->m_pkthdr.len;
		M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
		if (m == NULL) {
			if_statinc(ifp, if_oerrors);
			continue;
		}
		p = mtod(m, uint8_t *);
		PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);

		bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT);

		pppoe_output(sc, m);
	}
	PPPOE_UNLOCK(sc);
}

#ifdef PPPOE_MPSAFE
static int
pppoe_transmit(struct ifnet *ifp, struct mbuf *m)
{
	struct pppoe_softc *sc = (void *)ifp;
	uint8_t *p;
	size_t len;

	if (m == NULL)
		return EINVAL;

	/* are we ready to process data yet? */
	PPPOE_LOCK(sc, RW_READER);
	if (sc->sc_state < PPPOE_STATE_SESSION) {
		PPPOE_UNLOCK(sc);
		m_freem(m);
		return ENOBUFS;
	}

	len = m->m_pkthdr.len;
	M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
	if (m == NULL) {
		PPPOE_UNLOCK(sc);
		if_statinc(ifp, if_oerrors);
		return ENETDOWN;
	}
	p = mtod(m, uint8_t *);
	PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);

	bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT);

	pppoe_output(sc, m);
	PPPOE_UNLOCK(sc);
	return 0;
}
#endif /* PPPOE_MPSAFE */

static void
pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2)
{
	struct ifnet *ifp = arg2;
	struct pppoe_softc *sc;
	DECLARE_SPLNET_VARIABLE;

	if (cmd != PFIL_IFNET_DETACH)
		return;

	ACQUIRE_SPLNET();
	rw_enter(&pppoe_softc_list_lock, RW_READER);
	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
		PPPOE_LOCK(sc, RW_WRITER);
		if (sc->sc_eth_if != ifp) {
			PPPOE_UNLOCK(sc);
			continue;
		}
		if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
			sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
			pppoe_printf(sc,
			    "ethernet interface detached, going down\n");
		}
		sc->sc_eth_if = NULL;
		pppoe_clear_softc(sc, "ethernet interface detached");
		PPPOE_UNLOCK(sc);
	}
	rw_exit(&pppoe_softc_list_lock);
	RELEASE_SPLNET();
}

static void
pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
{
	KASSERT(PPPOE_WLOCKED(sc));

	/* stop timer */
	callout_stop(&sc->sc_timeout);
	pppoe_printf(sc, "session 0x%x terminated, %s\n",
	    sc->sc_session, message);

	/* fix our state */
	sc->sc_state = PPPOE_STATE_INITIAL;

	PPPOE_UNLOCK(sc);

	/* signal upper layer */
	sc->sc_sppp.pp_down(&sc->sc_sppp);

	PPPOE_LOCK(sc, RW_WRITER);

	/* clean up softc */
	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
	if (sc->sc_ac_cookie) {
		free(sc->sc_ac_cookie, M_DEVBUF);
		sc->sc_ac_cookie = NULL;
	}
	if (sc->sc_relay_sid) {
		free(sc->sc_relay_sid, M_DEVBUF);
		sc->sc_relay_sid = NULL;
	}
	sc->sc_ac_cookie_len = 0;
	sc->sc_session = 0;
}

static void
pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
{
	if (m->m_flags & M_PROMISC) {
		m_freem(m);
		return;
	}

#ifndef PPPOE_SERVER
	if (m->m_flags & (M_MCAST | M_BCAST)) {
		m_freem(m);
		return;
	}
#endif

	IFQ_LOCK(inq);
	if (IF_QFULL(inq)) {
		IF_DROP(inq);
		IFQ_UNLOCK(inq);
		m_freem(m);
	} else {
		IF_ENQUEUE(inq, m);
		IFQ_UNLOCK(inq);
		softint_schedule(pppoe_softintr);
	}
	return;
}

void
pppoe_input(struct ifnet *ifp, struct mbuf *m)
{
	pppoe_enqueue(&ppoeinq, m);
	return;
}

void
pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
{
	pppoe_enqueue(&ppoediscinq, m);
	return;
}

static void
sysctl_net_pppoe_setup(struct sysctllog **clog)
{
	const struct sysctlnode *node = NULL;
	extern pktq_rps_hash_func_t sppp_pktq_rps_hash_p;

	sppp_pktq_rps_hash_p = pktq_rps_hash_default;

	sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT,
	    CTLTYPE_NODE, "pppoe",
	    SYSCTL_DESCR("PPPOE protocol"),
	    NULL, 0, NULL, 0,
	    CTL_NET, CTL_CREATE, CTL_EOL);

	if (node == NULL)
		return;

	sysctl_createv(clog, 0, &node, NULL,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_BOOL, "term_unknown",
	    SYSCTL_DESCR("Terminate unknown sessions"),
	    NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown),
	    CTL_CREATE, CTL_EOL);

	sysctl_createv(clog, 0, &node, NULL,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_STRING, "rps_hash",
	    SYSCTL_DESCR("Interface rps hash function control"),
	    sysctl_pktq_rps_hash_handler, 0, (void *)&sppp_pktq_rps_hash_p,
	    PKTQ_RPS_HASH_NAME_LEN,
	    CTL_CREATE, CTL_EOL);
}

/*
 * Module infrastructure
 */
#include "if_module.h"

IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")