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
/*	$NetBSD: if_ppp.c,v 1.172 2022/09/03 02:47:59 thorpej Exp $	*/
/*	Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp 	*/

/*
 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
 *
 * Copyright (c) 1984-2000 Carnegie Mellon University. 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. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Office of Technology Transfer
 *      Carnegie Mellon University
 *      5000 Forbes Avenue
 *      Pittsburgh, PA  15213-3890
 *      (412) 268-4387, fax: (412) 268-7395
 *      tech-transfer@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Based on:
 *	@(#)if_sl.c	7.6.1.2 (Berkeley) 2/15/89
 *
 * Copyright (c) 1987 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Serial Line interface
 *
 * Rick Adams
 * Center for Seismic Studies
 * 1300 N 17th Street, Suite 1450
 * Arlington, Virginia 22209
 * (703)276-7900
 * rick@seismo.ARPA
 * seismo!rick
 *
 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
 * Converted to 4.3BSD Beta by Chris Torek.
 * Other changes made at Berkeley, based in part on code by Kirk Smith.
 *
 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
 * Added VJ tcp header compression; more unified ioctls
 *
 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
 * Cleaned up a lot of the mbuf-related code to fix bugs that
 * caused system crashes and packet corruption.  Changed pppstart
 * so that it doesn't just give up with a collision if the whole
 * packet doesn't fit in the output ring buffer.
 *
 * Added priority queueing for interactive IP packets, following
 * the model of if_sl.c, plus hooks for bpf.
 * Paul Mackerras (paulus@cs.anu.edu.au).
 */

/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
/* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */

/*
 * XXX IMP ME HARDER
 *
 * This is an explanation of that comment.  This code used to use
 * splimp() to block both network and tty interrupts.  However,
 * that call is deprecated.  So, we have replaced the uses of
 * splimp() with splhigh() in order to applomplish what it needs
 * to accomplish, and added that happy little comment.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.172 2022/09/03 02:47:59 thorpej Exp $");

#ifdef _KERNEL_OPT
#include "ppp.h"
#include "opt_inet.h"
#include "opt_gateway.h"
#include "opt_ppp.h"
#endif

#ifdef INET
#define VJC
#endif
#define PPP_COMPRESS

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/once.h>
#include <sys/conf.h>
#include <sys/kauth.h>
#include <sys/intr.h>
#include <sys/socketvar.h>
#include <sys/device.h>
#include <sys/module.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#ifdef INET
#include <netinet/ip.h>
#endif

#include <net/bpf.h>
#include <net/slip.h>

#ifdef VJC
#include <net/slcompress.h>
#endif

#include <net/ppp_defs.h>
#include <net/if_ppp.h>
#include <net/if_pppvar.h>
#include <sys/cpu.h>

#ifdef PPP_COMPRESS
#define PACKETPTR	struct mbuf *
#include <net/ppp-comp.h>
#endif

#include "ioconf.h"

static int	pppsioctl(struct ifnet *, u_long, void *);
static void	ppp_requeue(struct ppp_softc *);
static void	ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
static void	ppp_ccp_closed(struct ppp_softc *);
static void	ppp_inproc(struct ppp_softc *, struct mbuf *);
static void	pppdumpm(struct mbuf *m0);
#ifdef ALTQ
static void	ppp_ifstart(struct ifnet *ifp);
#endif

static void	pppintr(void *);

extern struct linesw ppp_disc;

/*
 * We define two link layer specific mbuf flags, to mark high-priority
 * packets for output, and received packets following lost/corrupted
 * packets.
 */
#define	M_HIGHPRI	M_LINK0	/* output packet for sc_fastq */
#define	M_ERRMARK	M_LINK1	/* rx packet following lost/corrupted pkt */

static int ppp_clone_create(struct if_clone *, int);
static int ppp_clone_destroy(struct ifnet *);

static struct ppp_softc *ppp_create(const char *, int);

static struct {
	LIST_HEAD(ppp_sclist, ppp_softc) list;
	kmutex_t lock;
} ppp_softcs __cacheline_aligned;

struct if_clone ppp_cloner =
    IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);

#ifdef PPP_COMPRESS
static LIST_HEAD(, compressor) ppp_compressors = { NULL };
static kmutex_t ppp_compressors_mtx;

static int ppp_compressor_init(void);
static int ppp_compressor_destroy(void);
static struct compressor *ppp_get_compressor(uint8_t);
static void ppp_compressor_rele(struct compressor *);
#endif /* PPP_COMPRESS */


/*
 * Called from boot code to establish ppp interfaces.
 */
void
pppattach(int n __unused)
{

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

static void
pppinit(void)
{
	/* Init the compressor sub-sub-system */
	ppp_compressor_init();

	if (ttyldisc_attach(&ppp_disc) != 0)
		panic("%s", __func__);

	mutex_init(&ppp_softcs.lock, MUTEX_DEFAULT, IPL_NONE);
	LIST_INIT(&ppp_softcs.list);
	if_clone_attach(&ppp_cloner);
}

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

	if (!LIST_EMPTY(&ppp_softcs.list))
		error = EBUSY;

	if (error == 0)
		error = ttyldisc_detach(&ppp_disc);

	if (error == 0) {
		mutex_destroy(&ppp_softcs.lock);
		if_clone_detach(&ppp_cloner);
		ppp_compressor_destroy();
	}

	return error;
}

static struct ppp_softc *
ppp_create(const char *name, int unit)
{
	struct ppp_softc *sc, *sci, *scl = NULL;

	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);

	mutex_enter(&ppp_softcs.lock);
	if (unit == -1) {
		int i = 0;
		LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) {
			scl = sci;
			if (i < sci->sc_unit) {
				unit = i;
				break;
			} else {
#ifdef DIAGNOSTIC
				KASSERT(i == sci->sc_unit);
#endif
				i++;
			}
		}
		if (unit == -1)
			unit = i;
	} else {
		LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) {
			scl = sci;
			if (unit < sci->sc_unit)
				break;
			else if (unit == sci->sc_unit) {
				free(sc, M_DEVBUF);
				mutex_exit(&ppp_softcs.lock);
				return NULL;
			}
		}
	}

	if (sci != NULL)
		LIST_INSERT_BEFORE(sci, sc, sc_iflist);
	else if (scl != NULL)
		LIST_INSERT_AFTER(scl, sc, sc_iflist);
	else
		LIST_INSERT_HEAD(&ppp_softcs.list, sc, sc_iflist);

	mutex_exit(&ppp_softcs.lock);

	if_initname(&sc->sc_if, name, sc->sc_unit = unit);
	callout_init(&sc->sc_timo_ch, 0);
	sc->sc_if.if_softc = sc;
	sc->sc_if.if_mtu = PPP_MTU;
	sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
	sc->sc_if.if_type = IFT_PPP;
	sc->sc_if.if_hdrlen = PPP_HDRLEN;
	sc->sc_if.if_dlt = DLT_NULL;
	sc->sc_if.if_ioctl = pppsioctl;
	sc->sc_if.if_output = pppoutput;
#ifdef ALTQ
	sc->sc_if.if_start = ppp_ifstart;
#endif
	IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
	IFQ_SET_MAXLEN(&sc->sc_inq, IFQ_MAXLEN);
	IFQ_SET_MAXLEN(&sc->sc_fastq, IFQ_MAXLEN);
	IFQ_SET_MAXLEN(&sc->sc_rawq, IFQ_MAXLEN);

	IFQ_LOCK_INIT(&sc->sc_fastq);

	/* Ratio of 1:2 packets between the regular and the fast queue */
	sc->sc_maxfastq = 2;
	IFQ_SET_READY(&sc->sc_if.if_snd);
	if_attach(&sc->sc_if);
	if_alloc_sadl(&sc->sc_if);
	bpf_attach(&sc->sc_if, DLT_NULL, 0);
	return sc;
}

static int
ppp_clone_create(struct if_clone *ifc, int unit)
{
	return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
}

static int
ppp_clone_destroy(struct ifnet *ifp)
{
	struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;

	if (sc->sc_devp != NULL)
		return EBUSY; /* Not removing it */

	mutex_enter(&ppp_softcs.lock);
	LIST_REMOVE(sc, sc_iflist);
	mutex_exit(&ppp_softcs.lock);

	bpf_detach(ifp);
	if_detach(ifp);

	IFQ_LOCK_DESTROY(&sc->sc_fastq);

	free(sc, M_DEVBUF);
	return 0;
}

/*
 * Allocate a ppp interface unit and initialize it.
 */
struct ppp_softc *
pppalloc(pid_t pid)
{
	struct ppp_softc *sc = NULL, *scf;
	int i;

	mutex_enter(&ppp_softcs.lock);
	LIST_FOREACH(scf, &ppp_softcs.list, sc_iflist) {
		if (scf->sc_xfer == pid) {
			scf->sc_xfer = 0;
			mutex_exit(&ppp_softcs.lock);
			return scf;
		}
		if (scf->sc_devp == NULL && sc == NULL)
			sc = scf;
	}
	mutex_exit(&ppp_softcs.lock);

	if (sc == NULL)
		sc = ppp_create(ppp_cloner.ifc_name, -1);

	sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc);
	if (sc->sc_si == NULL) {
		printf("%s: unable to establish softintr\n",
		    sc->sc_if.if_xname);
		return NULL;
	}
	sc->sc_flags = 0;
	sc->sc_mru = PPP_MRU;
	sc->sc_relinq = NULL;
	(void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
#ifdef VJC
	sc->sc_comp = malloc(sizeof(struct slcompress), M_DEVBUF, M_NOWAIT);
	if (sc->sc_comp)
		sl_compress_init(sc->sc_comp);
#endif
#ifdef PPP_COMPRESS
	sc->sc_xc_state = NULL;
	sc->sc_rc_state = NULL;
#endif /* PPP_COMPRESS */
	for (i = 0; i < NUM_NP; ++i)
		sc->sc_npmode[i] = NPMODE_ERROR;
	sc->sc_npqueue = NULL;
	sc->sc_npqtail = &sc->sc_npqueue;
	sc->sc_last_sent = sc->sc_last_recv = time_second;

	return sc;
}

/*
 * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
 */
void
pppdealloc(struct ppp_softc *sc)
{
	struct mbuf *m;

	softint_disestablish(sc->sc_si);
	if_down(&sc->sc_if);
	sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
	sc->sc_devp = NULL;
	sc->sc_xfer = 0;
	for (;;) {
		IF_DEQUEUE(&sc->sc_rawq, m);
		if (m == NULL)
			break;
		m_freem(m);
	}
	for (;;) {
		IF_DEQUEUE(&sc->sc_inq, m);
		if (m == NULL)
			break;
		m_freem(m);
	}
	for (;;) {
		IF_DEQUEUE(&sc->sc_fastq, m);
		if (m == NULL)
			break;
		m_freem(m);
	}
	while ((m = sc->sc_npqueue) != NULL) {
		sc->sc_npqueue = m->m_nextpkt;
		m_freem(m);
	}
	if (sc->sc_togo != NULL) {
		m_freem(sc->sc_togo);
		sc->sc_togo = NULL;
	}
#ifdef PPP_COMPRESS
	ppp_ccp_closed(sc);
	sc->sc_xc_state = NULL;
	sc->sc_rc_state = NULL;
#endif /* PPP_COMPRESS */
#ifdef PPP_FILTER
	if (sc->sc_pass_filt_in.bf_insns != 0) {
		free(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
		sc->sc_pass_filt_in.bf_insns = 0;
		sc->sc_pass_filt_in.bf_len = 0;
	}
	if (sc->sc_pass_filt_out.bf_insns != 0) {
		free(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
		sc->sc_pass_filt_out.bf_insns = 0;
		sc->sc_pass_filt_out.bf_len = 0;
	}
	if (sc->sc_active_filt_in.bf_insns != 0) {
		free(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
		sc->sc_active_filt_in.bf_insns = 0;
		sc->sc_active_filt_in.bf_len = 0;
	}
	if (sc->sc_active_filt_out.bf_insns != 0) {
		free(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
		sc->sc_active_filt_out.bf_insns = 0;
		sc->sc_active_filt_out.bf_len = 0;
	}
#endif /* PPP_FILTER */
#ifdef VJC
	if (sc->sc_comp != 0) {
		free(sc->sc_comp, M_DEVBUF);
		sc->sc_comp = 0;
	}
#endif
	(void)ppp_clone_destroy(&sc->sc_if);
}

/*
 * Ioctl routine for generic ppp devices.
 */
int
pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag,
    struct lwp *l)
{
	int s, error, flags, mru, npx;
	u_int nb;
	struct ppp_option_data *odp;
	struct compressor *cp;
	struct npioctl *npi;
	time_t t;
#ifdef PPP_FILTER
	struct bpf_program *bp, *nbp;
	struct bpf_insn *newcode, *oldcode;
	int newcodelen;
#endif /* PPP_FILTER */
#ifdef	PPP_COMPRESS
	u_char ccp_option[CCP_MAX_OPTION_LENGTH];
#endif

	switch (cmd) {
	case PPPIOCSFLAGS:
	case PPPIOCSMRU:
	case PPPIOCSMAXCID:
	case PPPIOCSCOMPRESS:
	case PPPIOCSNPMODE:
		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
			KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if,
			KAUTH_ARG(cmd), NULL) != 0)
			return EPERM;
		break;
	case PPPIOCXFERUNIT:
		/* XXX: Why is this privileged?! */
		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
			KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if,
			KAUTH_ARG(cmd), NULL) != 0)
			return EPERM;
		break;
	default:
		break;
	}

	switch (cmd) {
	case FIONREAD:
		*(int *)data = sc->sc_inq.ifq_len;
		break;

	case PPPIOCGUNIT:
		*(int *)data = sc->sc_unit;
		break;

	case PPPIOCGFLAGS:
		*(u_int *)data = sc->sc_flags;
		break;

	case PPPIOCGRAWIN:
	{
		struct ppp_rawin *rwin = (struct ppp_rawin *)data;
		u_char c, q = 0;

		for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);)
			rwin->buf[q++] = sc->sc_rawin.buf[c++];

		for (c = 0; c < sc->sc_rawin_start;)
			rwin->buf[q++] = sc->sc_rawin.buf[c++];

		rwin->count = sc->sc_rawin.count;
	}
	break;

	case PPPIOCSFLAGS:
		flags = *(int *)data & SC_MASK;
		s = splsoftnet();
#ifdef PPP_COMPRESS
		if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
			ppp_ccp_closed(sc);
#endif
		splhigh();	/* XXX IMP ME HARDER */
		sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
		splx(s);
		break;

	case PPPIOCSMRU:
		mru = *(int *)data;
		if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
			sc->sc_mru = mru;
		break;

	case PPPIOCGMRU:
		*(int *)data = sc->sc_mru;
		break;

#ifdef VJC
	case PPPIOCSMAXCID:
		if (sc->sc_comp) {
			s = splsoftnet();
			sl_compress_setup(sc->sc_comp, *(int *)data);
			splx(s);
		}
		break;
#endif

	case PPPIOCXFERUNIT:
		sc->sc_xfer = l->l_proc->p_pid;
		break;

#ifdef PPP_COMPRESS
	case PPPIOCSCOMPRESS:
		odp = (struct ppp_option_data *)data;
		nb = odp->length;
		if (nb > sizeof(ccp_option))
			nb = sizeof(ccp_option);
		if (nb < 3)
			return EINVAL;
		if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
			return error;
		/* preliminary check on the length byte */
		if (ccp_option[1] < 2)
			return EINVAL;
		cp = ppp_get_compressor(ccp_option[0]);
		if (cp == NULL) {
			if (sc->sc_flags & SC_DEBUG)
				printf("%s: no compressor for [%x %x %x], %x\n",
				    sc->sc_if.if_xname, ccp_option[0],
				    ccp_option[1], ccp_option[2], nb);
			return EINVAL;	/* no handler found */
		}
		/*
		 * Found a handler for the protocol - try to allocate
		 * a compressor or decompressor.
		 */
		error = 0;
		if (odp->transmit) {
			s = splsoftnet();
			if (sc->sc_xc_state != NULL) {
				(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
				ppp_compressor_rele(sc->sc_xcomp);
			}
			sc->sc_xcomp = cp;
			sc->sc_xc_state = cp->comp_alloc(ccp_option, nb);
			if (sc->sc_xc_state == NULL) {
				if (sc->sc_flags & SC_DEBUG)
					printf("%s: comp_alloc failed\n",
					    sc->sc_if.if_xname);
				error = ENOBUFS;
			}
			splhigh();	/* XXX IMP ME HARDER */
			sc->sc_flags &= ~SC_COMP_RUN;
			splx(s);
		} else {
			s = splsoftnet();
			if (sc->sc_rc_state != NULL) {
				(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
				ppp_compressor_rele(sc->sc_rcomp);
			}
			sc->sc_rcomp = cp;
			sc->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
			if (sc->sc_rc_state == NULL) {
				if (sc->sc_flags & SC_DEBUG)
					printf("%s: decomp_alloc failed\n",
					    sc->sc_if.if_xname);
				error = ENOBUFS;
			}
			splhigh();	/* XXX IMP ME HARDER */
			sc->sc_flags &= ~SC_DECOMP_RUN;
			splx(s);
		}
		return error;
#endif /* PPP_COMPRESS */

	case PPPIOCGNPMODE:
	case PPPIOCSNPMODE:
		npi = (struct npioctl *)data;
		switch (npi->protocol) {
		case PPP_IP:
			npx = NP_IP;
			break;
		case PPP_IPV6:
			npx = NP_IPV6;
			break;
		default:
			return EINVAL;
		}
		if (cmd == PPPIOCGNPMODE) {
			npi->mode = sc->sc_npmode[npx];
		} else {
			if (npi->mode != sc->sc_npmode[npx]) {
				s = splnet();
				sc->sc_npmode[npx] = npi->mode;
				if (npi->mode != NPMODE_QUEUE) {
					ppp_requeue(sc);
					ppp_restart(sc);
				}
				splx(s);
			}
		}
		break;

	case PPPIOCGIDLE:
		s = splsoftnet();
		t = time_second;
		((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
		((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
		splx(s);
		break;

#ifdef PPP_FILTER
	case PPPIOCSPASS:
	case PPPIOCSACTIVE:
		/* These are no longer supported. */
		return EOPNOTSUPP;

	case PPPIOCSIPASS:
	case PPPIOCSOPASS:
	case PPPIOCSIACTIVE:
	case PPPIOCSOACTIVE:
		nbp = (struct bpf_program *)data;
		if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
			return EINVAL;
		newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
		if (newcodelen != 0) {
			newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
			/* WAITOK -- malloc() never fails. */
			if ((error = copyin((void *)nbp->bf_insns,
				    (void *)newcode, newcodelen)) != 0) {
				free(newcode, M_DEVBUF);
				return error;
			}
			if (!bpf_validate(newcode, nbp->bf_len)) {
				free(newcode, M_DEVBUF);
				return EINVAL;
			}
		} else
			newcode = 0;
		switch (cmd) {
		case PPPIOCSIPASS:
			bp = &sc->sc_pass_filt_in;
			break;

		case PPPIOCSOPASS:
			bp = &sc->sc_pass_filt_out;
			break;

		case PPPIOCSIACTIVE:
			bp = &sc->sc_active_filt_in;
			break;

		case PPPIOCSOACTIVE:
			bp = &sc->sc_active_filt_out;
			break;
		default:
			free(newcode, M_DEVBUF);
			return EPASSTHROUGH;
		}
		oldcode = bp->bf_insns;
		s = splnet();
		bp->bf_len = nbp->bf_len;
		bp->bf_insns = newcode;
		splx(s);
		if (oldcode != 0)
			free(oldcode, M_DEVBUF);
		break;
#endif /* PPP_FILTER */

	default:
		return EPASSTHROUGH;
	}
	return 0;
}

/*
 * Process an ioctl request to the ppp network interface.
 */
static int
pppsioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct ppp_softc *sc = ifp->if_softc;
	struct ifaddr *ifa = (struct ifaddr *)data;
	struct ifreq *ifr = (struct ifreq *)data;
	struct ppp_stats *psp;
#ifdef	PPP_COMPRESS
	struct ppp_comp_stats *pcp;
#endif
	int s = splnet(), error = 0;

	switch (cmd) {
	case SIOCSIFFLAGS:
		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
			break;
		if ((ifp->if_flags & IFF_RUNNING) == 0)
			ifp->if_flags &= ~IFF_UP;
		break;

	case SIOCINITIFADDR:
		switch (ifa->ifa_addr->sa_family) {
#ifdef INET
		case AF_INET:
			break;
#endif
#ifdef INET6
		case AF_INET6:
			break;
#endif
		default:
			printf("%s: af%d not supported\n", ifp->if_xname,
			    ifa->ifa_addr->sa_family);
			error = EAFNOSUPPORT;
			break;
		}
		ifa->ifa_rtrequest = p2p_rtrequest;
		break;

	case SIOCADDMULTI:
	case SIOCDELMULTI:
		if (ifr == NULL) {
			error = EAFNOSUPPORT;
			break;
		}
		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
#ifdef INET
		case AF_INET:
			break;
#endif
#ifdef INET6
		case AF_INET6:
			break;
#endif
		default:
			error = EAFNOSUPPORT;
			break;
		}
		break;

	case SIOCGPPPSTATS:
		psp = &((struct ifpppstatsreq *)data)->stats;
		memset(psp, 0, sizeof(*psp));
		psp->p = sc->sc_stats;
#if defined(VJC) && !defined(SL_NO_STATS)
		if (sc->sc_comp) {
			psp->vj.vjs_packets = sc->sc_comp->sls_packets;
			psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
			psp->vj.vjs_searches = sc->sc_comp->sls_searches;
			psp->vj.vjs_misses = sc->sc_comp->sls_misses;
			psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
			psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
			psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
			psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
		}
#endif /* VJC */
		break;

#ifdef PPP_COMPRESS
	case SIOCGPPPCSTATS:
		pcp = &((struct ifpppcstatsreq *)data)->stats;
		memset(pcp, 0, sizeof(*pcp));
		if (sc->sc_xc_state != NULL)
			(*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
		if (sc->sc_rc_state != NULL)
			(*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
		break;
#endif /* PPP_COMPRESS */

	default:
		if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET)
			error = 0;
		break;
	}
	splx(s);
	return error;
}

/*
 * Queue a packet.  Start transmission if not active.
 * Packet is placed in Information field of PPP frame.
 */
int
pppoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
    const struct rtentry *rtp)
{
	struct ppp_softc *sc = ifp->if_softc;
	int protocol, address, control;
	u_char *cp;
	int s, error;
#ifdef INET
	struct ip *ip;
#endif
	struct ifqueue *ifq;
	enum NPmode mode;
	int len;

	    if (sc->sc_devp == NULL ||
		(ifp->if_flags & IFF_RUNNING) == 0 ||
		((ifp->if_flags & IFF_UP) == 0 &&
		    dst->sa_family != AF_UNSPEC)) {
		    error = ENETDOWN;	/* sort of */
		    goto bad;
	    }

	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);

	/*
	 * Compute PPP header.
	 */
	m0->m_flags &= ~M_HIGHPRI;
	switch (dst->sa_family) {
#ifdef INET
	case AF_INET:
		address = PPP_ALLSTATIONS;
		control = PPP_UI;
		protocol = PPP_IP;
		mode = sc->sc_npmode[NP_IP];

		/*
		 * If this packet has the "low delay" bit set in the IP header,
		 * put it on the fastq instead.
		 */
		ip = mtod(m0, struct ip *);
		if (ip->ip_tos & IPTOS_LOWDELAY)
			m0->m_flags |= M_HIGHPRI;
		break;
#endif
#ifdef INET6
	case AF_INET6:
		address = PPP_ALLSTATIONS;	/*XXX*/
		control = PPP_UI;		/*XXX*/
		protocol = PPP_IPV6;
		mode = sc->sc_npmode[NP_IPV6];

#if 0	/* XXX flowinfo/traffic class, maybe? */
	/*
	 * If this packet has the "low delay" bit set in the IP header,
	 * put it on the fastq instead.
	 */
		ip = mtod(m0, struct ip *);
		if (ip->ip_tos & IPTOS_LOWDELAY)
			m0->m_flags |= M_HIGHPRI;
#endif
		break;
#endif
	case AF_UNSPEC:
		address = PPP_ADDRESS(dst->sa_data);
		control = PPP_CONTROL(dst->sa_data);
		protocol = PPP_PROTOCOL(dst->sa_data);
		mode = NPMODE_PASS;
		break;
	default:
		printf("%s: af%d not supported\n", ifp->if_xname,
		    dst->sa_family);
		error = EAFNOSUPPORT;
		goto bad;
	}

	/*
	 * Drop this packet, or return an error, if necessary.
	 */
	if (mode == NPMODE_ERROR) {
		error = ENETDOWN;
		goto bad;
	}
	if (mode == NPMODE_DROP) {
		error = 0;
		goto bad;
	}

	/*
	 * Add PPP header.
	 */
	M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
	if (m0 == NULL) {
		error = ENOBUFS;
		goto bad;
	}

	cp = mtod(m0, u_char *);
	*cp++ = address;
	*cp++ = control;
	*cp++ = protocol >> 8;
	*cp++ = protocol & 0xff;

	len = m_length(m0);

	if (sc->sc_flags & SC_LOG_OUTPKT) {
		printf("%s output: ", ifp->if_xname);
		pppdumpm(m0);
	}

	if ((protocol & 0x8000) == 0) {
#ifdef PPP_FILTER
		/*
		 * Apply the pass and active filters to the packet,
		 * but only if it is a data packet.
		 */
		if (sc->sc_pass_filt_out.bf_insns != 0 &&
		    bpf_filter(sc->sc_pass_filt_out.bf_insns,
			(u_char *)m0, len, 0) == 0) {
			error = 0;		/* drop this packet */
			goto bad;
		}

		/*
		 * Update the time we sent the most recent packet.
		 */
		if (sc->sc_active_filt_out.bf_insns == 0 ||
		    bpf_filter(sc->sc_active_filt_out.bf_insns,
			(u_char *)m0, len, 0))
			sc->sc_last_sent = time_second;
#else
		/*
		 * Update the time we sent the most recent packet.
		 */
		sc->sc_last_sent = time_second;
#endif /* PPP_FILTER */
	}

	/*
	 * See if bpf wants to look at the packet.
	 */
	bpf_mtap(&sc->sc_if, m0, BPF_D_OUT);

	/*
	 * Put the packet on the appropriate queue.
	 */
	s = splnet();
	if (mode == NPMODE_QUEUE) {
		/* XXX we should limit the number of packets on this queue */
		*sc->sc_npqtail = m0;
		m0->m_nextpkt = NULL;
		sc->sc_npqtail = &m0->m_nextpkt;
	} else {
		ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
		if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0)) != 0) {
			splx(s);
			if_statinc(&sc->sc_if, if_oerrors);
			sc->sc_stats.ppp_oerrors++;
			return error;
		}
		ppp_restart(sc);
	}
	if_statadd2(ifp, if_opackets, 1, if_obytes, len);

	splx(s);
	return 0;

bad:
	m_freem(m0);
	return error;
}

/*
 * After a change in the NPmode for some NP, move packets from the
 * npqueue to the send queue or the fast queue as appropriate.
 * Should be called at splnet, since we muck with the queues.
 */
static void
ppp_requeue(struct ppp_softc *sc)
{
	struct mbuf *m, **mpp;
	struct ifqueue *ifq;
	enum NPmode mode;
	int error;

	for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
		switch (PPP_PROTOCOL(mtod(m, u_char *))) {
		case PPP_IP:
			mode = sc->sc_npmode[NP_IP];
			break;
		case PPP_IPV6:
			mode = sc->sc_npmode[NP_IPV6];
			break;
		default:
			mode = NPMODE_PASS;
		}

		switch (mode) {
		case NPMODE_PASS:
			/*
			 * This packet can now go on one of the queues to
			 * be sent.
			 */
			*mpp = m->m_nextpkt;
			m->m_nextpkt = NULL;
			ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
			if ((error = ifq_enqueue2(&sc->sc_if, ifq, m)) != 0) {
				if_statinc(&sc->sc_if, if_oerrors);
				sc->sc_stats.ppp_oerrors++;
			}
			break;

		case NPMODE_DROP:
		case NPMODE_ERROR:
			*mpp = m->m_nextpkt;
			m_freem(m);
			break;

		case NPMODE_QUEUE:
			mpp = &m->m_nextpkt;
			break;
		}
	}
	sc->sc_npqtail = mpp;
}

/*
 * Transmitter has finished outputting some stuff;
 * remember to call sc->sc_start later at splsoftnet.
 */
void
ppp_restart(struct ppp_softc *sc)
{
	int s = splhigh();	/* XXX IMP ME HARDER */

	sc->sc_flags &= ~SC_TBUSY;
	softint_schedule(sc->sc_si);
	splx(s);
}

/*
 * Get a packet to send.  This procedure is intended to be called at
 * splsoftnet, since it may involve time-consuming operations such as
 * applying VJ compression, packet compression, address/control and/or
 * protocol field compression to the packet.
 */
struct mbuf *
ppp_dequeue(struct ppp_softc *sc)
{
	struct mbuf *m, *mp;
	u_char *cp;
	int address, control, protocol;
	int s;

	/*
	 * Grab a packet to send: first try the fast queue, then the
	 * normal queue.
	 */
	s = splnet();
	if (sc->sc_nfastq < sc->sc_maxfastq) {
		IF_DEQUEUE(&sc->sc_fastq, m);
		if (m != NULL)
			sc->sc_nfastq++;
		else
			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
	} else {
		sc->sc_nfastq = 0;
		IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
		if (m == NULL) {
			IF_DEQUEUE(&sc->sc_fastq, m);
			if (m != NULL)
				sc->sc_nfastq++;
		}
	}
	splx(s);

	if (m == NULL)
		return NULL;

	++sc->sc_stats.ppp_opackets;

	/*
	 * Extract the ppp header of the new packet.
	 * The ppp header will be in one mbuf.
	 */
	cp = mtod(m, u_char *);
	address = PPP_ADDRESS(cp);
	control = PPP_CONTROL(cp);
	protocol = PPP_PROTOCOL(cp);

	switch (protocol) {
	case PPP_IP:
#ifdef VJC
		/*
		 * If the packet is a TCP/IP packet, see if we can compress it.
		 */
		if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
			struct ip *ip;
			int type;

			mp = m;
			ip = (struct ip *)(cp + PPP_HDRLEN);
			if (mp->m_len <= PPP_HDRLEN) {
				mp = mp->m_next;
				if (mp == NULL)
					break;
				ip = mtod(mp, struct ip *);
			}
			/*
			 * This code assumes the IP/TCP header is in one
			 * non-shared mbuf
			 */
			if (ip->ip_p == IPPROTO_TCP) {
				type = sl_compress_tcp(mp, ip, sc->sc_comp,
				    !(sc->sc_flags & SC_NO_TCP_CCID));
				switch (type) {
				case TYPE_UNCOMPRESSED_TCP:
					protocol = PPP_VJC_UNCOMP;
					break;
				case TYPE_COMPRESSED_TCP:
					protocol = PPP_VJC_COMP;
					cp = mtod(m, u_char *);
					cp[0] = address; /* Header has moved */
					cp[1] = control;
					cp[2] = 0;
					break;
				}
				/* Update protocol in PPP header */
				cp[3] = protocol;
			}
		}
#endif	/* VJC */
		break;

#ifdef PPP_COMPRESS
	case PPP_CCP:
		ppp_ccp(sc, m, 0);
		break;
#endif	/* PPP_COMPRESS */
	}

#ifdef PPP_COMPRESS
	if (protocol != PPP_LCP && protocol != PPP_CCP &&
	    sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
		struct mbuf *mcomp = NULL;
		int slen;

		slen = 0;
		for (mp = m; mp != NULL; mp = mp->m_next)
			slen += mp->m_len;
		(*sc->sc_xcomp->compress)
		    (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
		if (mcomp != NULL) {
			if (sc->sc_flags & SC_CCP_UP) {
				/*
				 * Send the compressed packet instead of the
				 * original.
				 */
				m_freem(m);
				m = mcomp;
				cp = mtod(m, u_char *);
				protocol = cp[3];
			} else {
				/*
				 * Can't transmit compressed packets until CCP
				 * is up.
				 */
				m_freem(mcomp);
			}
		}
	}
#endif	/* PPP_COMPRESS */

	/*
	 * Compress the address/control and protocol, if possible.
	 */
	if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
	    control == PPP_UI && protocol != PPP_ALLSTATIONS &&
	    protocol != PPP_LCP) {
		/* can compress address/control */
		m->m_data += 2;
		m->m_len -= 2;
	}
	if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
		/* can compress protocol */
		if (mtod(m, u_char *) == cp) {
			cp[2] = cp[1];	/* move address/control up */
			cp[1] = cp[0];
		}
		++m->m_data;
		--m->m_len;
	}

	return m;
}

/*
 * Software interrupt routine, called at splsoftnet.
 */
static void
pppintr(void *arg)
{
	struct ppp_softc *sc = arg;
	struct mbuf *m;
	int s;

	mutex_enter(softnet_lock);
	if (!(sc->sc_flags & SC_TBUSY) &&
	    (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 ||
		sc->sc_fastq.ifq_head ||
		sc->sc_outm)) {
		s = splhigh();	/* XXX IMP ME HARDER */
		sc->sc_flags |= SC_TBUSY;
		splx(s);
		(*sc->sc_start)(sc);
	}
	for (;;) {
		s = splnet();
		IF_DEQUEUE(&sc->sc_rawq, m);
		splx(s);
		if (m == NULL)
			break;
		ppp_inproc(sc, m);
	}
	mutex_exit(softnet_lock);
}

#ifdef PPP_COMPRESS
/*
 * Handle a CCP packet.	 `rcvd' is 1 if the packet was received,
 * 0 if it is about to be transmitted.
 */
static void
ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
{
	u_char *dp, *ep;
	struct mbuf *mp;
	int slen, s;

	/*
	 * Get a pointer to the data after the PPP header.
	 */
	if (m->m_len <= PPP_HDRLEN) {
		mp = m->m_next;
		if (mp == NULL)
			return;
		dp = mtod(mp, u_char *);
	} else {
		mp = m;
		dp = mtod(mp, u_char *) + PPP_HDRLEN;
	}

	ep = mtod(mp, u_char *) + mp->m_len;
	if (dp + CCP_HDRLEN > ep)
		return;
	slen = CCP_LENGTH(dp);
	if (dp + slen > ep) {
		if (sc->sc_flags & SC_DEBUG)
			printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
			    dp, slen, mtod(mp, u_char *), mp->m_len);
		return;
	}

	switch (CCP_CODE(dp)) {
	case CCP_CONFREQ:
	case CCP_TERMREQ:
	case CCP_TERMACK:
		/* CCP must be going down - disable compression */
		if (sc->sc_flags & SC_CCP_UP) {
			s = splhigh();	/* XXX IMP ME HARDER */
			sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
			splx(s);
		}
		break;

	case CCP_CONFACK:
		if (sc->sc_flags & SC_CCP_OPEN &&
		    !(sc->sc_flags & SC_CCP_UP) &&
		    slen >= CCP_HDRLEN + CCP_OPT_MINLEN &&
		    slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
			if (!rcvd) {
				/* We're agreeing to send compressed packets. */
				if (sc->sc_xc_state != NULL &&
				    (*sc->sc_xcomp->comp_init)(sc->sc_xc_state,
					dp + CCP_HDRLEN, slen - CCP_HDRLEN,
					sc->sc_unit, 0,
					sc->sc_flags & SC_DEBUG)) {
					s = splhigh();	/* XXX IMP ME HARDER */
					sc->sc_flags |= SC_COMP_RUN;
					splx(s);
				}
			} else {
				/*
				 * Peer is agreeing to send compressed
				 * packets.
				 */
				if (sc->sc_rc_state != NULL &&
				    (*sc->sc_rcomp->decomp_init)(
					sc->sc_rc_state,
					dp + CCP_HDRLEN, slen - CCP_HDRLEN,
					sc->sc_unit, 0, sc->sc_mru,
					sc->sc_flags & SC_DEBUG)) {
					s = splhigh();	/* XXX IMP ME HARDER */
					sc->sc_flags |= SC_DECOMP_RUN;
					sc->sc_flags &=
					    ~(SC_DC_ERROR | SC_DC_FERROR);
					splx(s);
				}
			}
		}
		break;

	case CCP_RESETACK:
		if (sc->sc_flags & SC_CCP_UP) {
			if (!rcvd) {
				if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
					(*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
			} else {
				if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
					(*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
					s = splhigh();	/* XXX IMP ME HARDER */
					sc->sc_flags &= ~SC_DC_ERROR;
					splx(s);
				}
			}
		}
		break;
	}
}

/*
 * CCP is down; free (de)compressor state if necessary.
 */
static void
ppp_ccp_closed(struct ppp_softc *sc)
{
	if (sc->sc_xc_state) {
		(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
		ppp_compressor_rele(sc->sc_xcomp);
		sc->sc_xc_state = NULL;
	}
	if (sc->sc_rc_state) {
		(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
		ppp_compressor_rele(sc->sc_rcomp);
		sc->sc_rc_state = NULL;
	}
}
#endif /* PPP_COMPRESS */

/*
 * PPP packet input routine.
 * The caller has checked and removed the FCS and has inserted
 * the address/control bytes and the protocol high byte if they
 * were omitted.
 */
void
ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
{
	int s = splhigh();	/* XXX IMP ME HARDER */

	if (lost)
		m->m_flags |= M_ERRMARK;
	IF_ENQUEUE(&sc->sc_rawq, m);
	softint_schedule(sc->sc_si);
	splx(s);
}

/*
 * Process a received PPP packet, doing decompression as necessary.
 * Should be called at splsoftnet.
 */
#define COMPTYPE(proto)							      \
	((proto) == PPP_VJC_COMP ? TYPE_COMPRESSED_TCP			      \
	    : TYPE_UNCOMPRESSED_TCP)

static void
ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
{
	struct ifnet *ifp = &sc->sc_if;
	pktqueue_t *pktq = NULL;
	struct ifqueue *inq = NULL;
	int s, ilen, proto, rv;
	u_char *cp, adrs, ctrl;
	struct mbuf *mp, *dmp = NULL;
#ifdef VJC
	int xlen;
	u_char *iphdr;
	u_int hlen;
#endif

	sc->sc_stats.ppp_ipackets++;

	if (sc->sc_flags & SC_LOG_INPKT) {
		ilen = 0;
		for (mp = m; mp != NULL; mp = mp->m_next)
			ilen += mp->m_len;
		printf("%s: got %d bytes\n", ifp->if_xname, ilen);
		pppdumpm(m);
	}

	cp = mtod(m, u_char *);
	adrs = PPP_ADDRESS(cp);
	ctrl = PPP_CONTROL(cp);
	proto = PPP_PROTOCOL(cp);

	if (m->m_flags & M_ERRMARK) {
		m->m_flags &= ~M_ERRMARK;
		s = splhigh();	/* XXX IMP ME HARDER */
		sc->sc_flags |= SC_VJ_RESET;
		splx(s);
	}

#ifdef PPP_COMPRESS
	/*
	 * Decompress this packet if necessary, update the receiver's
	 * dictionary, or take appropriate action on a CCP packet.
	 */
	if (proto == PPP_COMP &&
	    sc->sc_rc_state &&
	    (sc->sc_flags & SC_DECOMP_RUN) &&
	    !(sc->sc_flags & SC_DC_ERROR) &&
	    !(sc->sc_flags & SC_DC_FERROR)) {
		/* Decompress this packet */
		rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
		if (rv == DECOMP_OK) {
			m_freem(m);
			if (dmp == NULL) {
				/*
				 * No error, but no decompressed packet
				 * produced
				 */
				return;
			}
			m = dmp;
			cp = mtod(m, u_char *);
			proto = PPP_PROTOCOL(cp);

		} else {
			/*
			 * An error has occurred in decompression.
			 * Pass the compressed packet up to pppd, which may
			 * take CCP down or issue a Reset-Req.
			 */
			if (sc->sc_flags & SC_DEBUG)
				printf("%s: decompress failed %d\n",
				    ifp->if_xname, rv);
			s = splhigh();	/* XXX IMP ME HARDER */
			sc->sc_flags |= SC_VJ_RESET;
			if (rv == DECOMP_ERROR)
				sc->sc_flags |= SC_DC_ERROR;
			else
				sc->sc_flags |= SC_DC_FERROR;
			splx(s);
		}

	} else {
		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN))
			(*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
		if (proto == PPP_CCP)
			ppp_ccp(sc, m, 1);
	}
#endif

	ilen = 0;
	for (mp = m; mp != NULL; mp = mp->m_next)
		ilen += mp->m_len;

#ifdef VJC
	if (sc->sc_flags & SC_VJ_RESET) {
		/*
		 * If we've missed a packet, we must toss subsequent compressed
		 * packets which don't have an explicit connection ID.
		 */
		if (sc->sc_comp)
			sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
		s = splhigh();	/* XXX IMP ME HARDER */
		sc->sc_flags &= ~SC_VJ_RESET;
		splx(s);
	}

	/*
	 * See if we have a VJ-compressed packet to uncompress.
	 */
	if (proto == PPP_VJC_COMP) {
		if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
			goto bad;

		xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN,
		    m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN,
		    TYPE_COMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen);
		if (xlen <= 0) {
			if (sc->sc_flags & SC_DEBUG) {
				printf("%s: VJ uncompress failed"
				    " on type comp\n",
				    ifp->if_xname);
			}
			goto bad;
		}

		/* Copy the PPP and IP headers into a new mbuf. */
		MGETHDR(mp, M_DONTWAIT, MT_DATA);
		if (mp == NULL)
			goto bad;
		mp->m_len = 0;
		mp->m_next = NULL;
		if (hlen + PPP_HDRLEN > MHLEN) {
			MCLGET(mp, M_DONTWAIT);
			if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
				/* Lose if big headers and no clusters */
				m_freem(mp);
				goto bad;
			}
		}
		cp = mtod(mp, u_char *);
		cp[0] = adrs;
		cp[1] = ctrl;
		cp[2] = 0;
		cp[3] = PPP_IP;
		proto = PPP_IP;
		bcopy(iphdr, cp + PPP_HDRLEN, hlen);
		mp->m_len = hlen + PPP_HDRLEN;

		/*
		 * Trim the PPP and VJ headers off the old mbuf
		 * and stick the new and old mbufs together.
		 */
		m->m_data += PPP_HDRLEN + xlen;
		m->m_len -= PPP_HDRLEN + xlen;
		if (m->m_len <= M_TRAILINGSPACE(mp)) {
			bcopy(mtod(m, u_char *),
			    mtod(mp, u_char *) + mp->m_len, m->m_len);
			mp->m_len += m->m_len;
			mp->m_next = m_free(m);
		} else
			mp->m_next = m;
		m = mp;
		ilen += hlen - xlen;

	} else if (proto == PPP_VJC_UNCOMP) {
		if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
			goto bad;

		xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN,
		    m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN,
		    TYPE_UNCOMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen);
		if (xlen < 0) {
			if (sc->sc_flags & SC_DEBUG) {
				printf("%s: VJ uncompress failed"
				    " on type uncomp\n",
				    ifp->if_xname);
			}
			goto bad;
		}

		proto = PPP_IP;
		cp[3] = PPP_IP;
	}
#endif /* VJC */

	/*
	 * If the packet will fit in a header mbuf, don't waste a
	 * whole cluster on it.
	 */
	if (ilen <= MHLEN && (m->m_flags & M_EXT)) {
		MGETHDR(mp, M_DONTWAIT, MT_DATA);
		if (mp != NULL) {
			m_copydata(m, 0, ilen, mtod(mp, void *));
			m_freem(m);
			m = mp;
			m->m_len = ilen;
		}
	}
	m->m_pkthdr.len = ilen;
	m_set_rcvif(m, ifp);

	if ((proto & 0x8000) == 0) {
#ifdef PPP_FILTER
		/*
		 * See whether we want to pass this packet, and
		 * if it counts as link activity.
		 */
		if (sc->sc_pass_filt_in.bf_insns != 0 &&
		    bpf_filter(sc->sc_pass_filt_in.bf_insns,
			(u_char *)m, ilen, 0) == 0) {
			/* drop this packet */
			m_freem(m);
			return;
		}
		if (sc->sc_active_filt_in.bf_insns == 0 ||
		    bpf_filter(sc->sc_active_filt_in.bf_insns,
			(u_char *)m, ilen, 0))
			sc->sc_last_recv = time_second;
#else
		/*
		 * Record the time that we received this packet.
		 */
		sc->sc_last_recv = time_second;
#endif /* PPP_FILTER */
	}

	/* See if bpf wants to look at the packet. */
	bpf_mtap(&sc->sc_if, m, BPF_D_IN);

	switch (proto) {
#ifdef INET
	case PPP_IP:
		/*
		 * IP packet - take off the ppp header and pass it up to IP.
		 */
		if ((ifp->if_flags & IFF_UP) == 0 ||
		    sc->sc_npmode[NP_IP] != NPMODE_PASS) {
			/* Interface is down - drop the packet. */
			m_freem(m);
			return;
		}
		m->m_pkthdr.len -= PPP_HDRLEN;
		m->m_data += PPP_HDRLEN;
		m->m_len -= PPP_HDRLEN;
#ifdef GATEWAY
		if (ipflow_fastforward(m))
			return;
#endif
		pktq = ip_pktq;
		break;
#endif

#ifdef INET6
	case PPP_IPV6:
		/*
		 * IPv6 packet - take off the ppp header and pass it up to
		 * IPv6.
		 */
		if ((ifp->if_flags & IFF_UP) == 0 ||
		    sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
			/* interface is down - drop the packet. */
			m_freem(m);
			return;
		}
		m->m_pkthdr.len -= PPP_HDRLEN;
		m->m_data += PPP_HDRLEN;
		m->m_len -= PPP_HDRLEN;
#ifdef GATEWAY
		if (ip6flow_fastforward(&m))
			return;
#endif
		pktq = ip6_pktq;
		break;
#endif

	default:
		/*
		 * Some other protocol - place on input queue for read().
		 */
		inq = &sc->sc_inq;
		pktq = NULL;
		break;
	}

	/*
	 * Put the packet on the appropriate input queue.
	 */
	s = splnet();

	/* pktq: inet or inet6 cases */
	if (__predict_true(pktq)) {
		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
			splx(s);
			if_statinc(ifp, if_iqdrops);
			goto bad;
		}
		if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);
		splx(s);
		return;
	}

	/* ifq: other protocol cases */
	if (!inq) {
		splx(s);
		goto bad;
	}
	if (IF_QFULL(inq)) {
		IF_DROP(inq);
		splx(s);
		if (sc->sc_flags & SC_DEBUG)
			printf("%s: input queue full\n", ifp->if_xname);
		if_statinc(ifp, if_iqdrops);
		goto bad;
	}
	IF_ENQUEUE(inq, m);
	splx(s);
	if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen);

	(*sc->sc_ctlp)(sc);

	return;

bad:
	m_freem(m);
	if_statinc(&sc->sc_if, if_ierrors);
	sc->sc_stats.ppp_ierrors++;
}

#define MAX_DUMP_BYTES	128

static void
pppdumpm(struct mbuf *m0)
{
	char buf[3*MAX_DUMP_BYTES+4];
	char *bp = buf;
	struct mbuf *m;

	for (m = m0; m; m = m->m_next) {
		int l = m->m_len;
		u_char *rptr = (u_char *)m->m_data;

		while (l--) {
			if (bp > buf + sizeof(buf) - 4)
				goto done;
			/* Convert byte to ascii hex */
			*bp++ = hexdigits[*rptr >> 4];
			*bp++ = hexdigits[*rptr++ & 0xf];
		}

		if (m->m_next) {
			if (bp > buf + sizeof(buf) - 3)
				goto done;
			*bp++ = '|';
		} else
			*bp++ = ' ';
	}
done:
	if (m)
		*bp++ = '>';
	*bp = 0;
	printf("%s\n", buf);
}

#ifdef ALTQ
/*
 * A wrapper to transmit a packet from if_start since ALTQ uses
 * if_start to send a packet.
 */
static void
ppp_ifstart(struct ifnet *ifp)
{
	struct ppp_softc *sc;

	sc = ifp->if_softc;
	(*sc->sc_start)(sc);
}
#endif

static const struct ppp_known_compressor {
	uint8_t code;
	const char *module;
} ppp_known_compressors[] = {
	{ CI_DEFLATE, "ppp_deflate" },
	{ CI_DEFLATE_DRAFT, "ppp_deflate" },
	{ CI_BSD_COMPRESS, "ppp_bsdcomp" },
	{ CI_MPPE, "ppp_mppe" },
	{ 0, NULL }
};

static int
ppp_compressor_init(void)
{

	mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE);
	return 0;
}

static int
ppp_compressor_destroy(void)
{

	mutex_destroy(&ppp_compressors_mtx);
	return 0;
}

static void
ppp_compressor_rele(struct compressor *cp)
{

	mutex_enter(&ppp_compressors_mtx);
	--cp->comp_refcnt;
	mutex_exit(&ppp_compressors_mtx);
}

static struct compressor *
ppp_get_compressor_noload(uint8_t ci, bool hold)
{
	struct compressor *cp;

	KASSERT(mutex_owned(&ppp_compressors_mtx));
	LIST_FOREACH(cp, &ppp_compressors, comp_list) {
		if (cp->compress_proto == ci) {
			if (hold)
				++cp->comp_refcnt;
			return cp;
		}
	}

	return NULL;
}

static struct compressor *
ppp_get_compressor(uint8_t ci)
{
	struct compressor *cp = NULL;
	const struct ppp_known_compressor *pkc;

	mutex_enter(&ppp_compressors_mtx);
	cp = ppp_get_compressor_noload(ci, true);
	mutex_exit(&ppp_compressors_mtx);
	if (cp != NULL)
		return cp;

	kernconfig_lock();
	mutex_enter(&ppp_compressors_mtx);
	cp = ppp_get_compressor_noload(ci, true);
	mutex_exit(&ppp_compressors_mtx);
	if (cp == NULL) {
		/* Not found, so try to autoload a module */
		for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) {
			if (pkc->code == ci) {
				if (module_autoload(pkc->module,
					MODULE_CLASS_MISC) != 0)
					break;
				mutex_enter(&ppp_compressors_mtx);
				cp = ppp_get_compressor_noload(ci, true);
				mutex_exit(&ppp_compressors_mtx);
				break;
			}
		}
	}
	kernconfig_unlock();

	return cp;
}

int
ppp_register_compressor(struct compressor *pc, size_t ncomp)
{
	int error = 0;
	size_t i;

	mutex_enter(&ppp_compressors_mtx);
	for (i = 0; i < ncomp; i++) {
		if (ppp_get_compressor_noload(pc[i].compress_proto,
			false) != NULL)
			error = EEXIST;
	}
	if (!error) {
		for (i = 0; i < ncomp; i++) {
			pc[i].comp_refcnt = 0;
			LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list);
		}
	}
	mutex_exit(&ppp_compressors_mtx);

	return error;
}

int
ppp_unregister_compressor(struct compressor *pc, size_t ncomp)
{
	int error = 0;
	size_t i;

	mutex_enter(&ppp_compressors_mtx);
	for (i = 0; i < ncomp; i++) {
		if (ppp_get_compressor_noload(pc[i].compress_proto,
			false) != &pc[i])
			error = ENOENT;
		else if (pc[i].comp_refcnt != 0)
			error = EBUSY;
	}
	if (!error) {
		for (i = 0; i < ncomp; i++) {
			LIST_REMOVE(&pc[i], comp_list);
		}
	}
	mutex_exit(&ppp_compressors_mtx);

	return error;
}

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

#ifdef PPP_FILTER
#define PPP_DEP "bpf_filter,"
#else
#define PPP_DEP
#endif

IF_MODULE(MODULE_CLASS_DRIVER, ppp, PPP_DEP "slcompress")