1 /* $NetBSD: if_ppp.c,v 1.173 2024/07/05 04:31:53 rin Exp $ */ 2 /* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */ 3 4 /* 5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver. 6 * 7 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The name "Carnegie Mellon University" must not be used to 22 * endorse or promote products derived from this software without 23 * prior written permission. For permission or any legal 24 * details, please contact 25 * Office of Technology Transfer 26 * Carnegie Mellon University 27 * 5000 Forbes Avenue 28 * Pittsburgh, PA 15213-3890 29 * (412) 268-4387, fax: (412) 268-7395 30 * tech-transfer@andrew.cmu.edu 31 * 32 * 4. Redistributions of any form whatsoever must retain the following 33 * acknowledgment: 34 * "This product includes software developed by Computing Services 35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)." 36 * 37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO 38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 44 * 45 * Based on: 46 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89 47 * 48 * Copyright (c) 1987 Regents of the University of California. 49 * All rights reserved. 50 * 51 * Redistribution and use in source and binary forms are permitted 52 * provided that the above copyright notice and this paragraph are 53 * duplicated in all such forms and that any documentation, 54 * advertising materials, and other materials related to such 55 * distribution and use acknowledge that the software was developed 56 * by the University of California, Berkeley. The name of the 57 * University may not be used to endorse or promote products derived 58 * from this software without specific prior written permission. 59 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 60 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 61 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 62 * 63 * Serial Line interface 64 * 65 * Rick Adams 66 * Center for Seismic Studies 67 * 1300 N 17th Street, Suite 1450 68 * Arlington, Virginia 22209 69 * (703)276-7900 70 * rick@seismo.ARPA 71 * seismo!rick 72 * 73 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). 74 * Converted to 4.3BSD Beta by Chris Torek. 75 * Other changes made at Berkeley, based in part on code by Kirk Smith. 76 * 77 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com) 78 * Added VJ tcp header compression; more unified ioctls 79 * 80 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au). 81 * Cleaned up a lot of the mbuf-related code to fix bugs that 82 * caused system crashes and packet corruption. Changed pppstart 83 * so that it doesn't just give up with a collision if the whole 84 * packet doesn't fit in the output ring buffer. 85 * 86 * Added priority queueing for interactive IP packets, following 87 * the model of if_sl.c, plus hooks for bpf. 88 * Paul Mackerras (paulus@cs.anu.edu.au). 89 */ 90 91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ 92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */ 93 94 /* 95 * XXX IMP ME HARDER 96 * 97 * This is an explanation of that comment. This code used to use 98 * splimp() to block both network and tty interrupts. However, 99 * that call is deprecated. So, we have replaced the uses of 100 * splimp() with splhigh() in order to applomplish what it needs 101 * to accomplish, and added that happy little comment. 102 */ 103 104 #include <sys/cdefs.h> 105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.173 2024/07/05 04:31:53 rin Exp $"); 106 107 #ifdef _KERNEL_OPT 108 #include "ppp.h" 109 #include "opt_inet.h" 110 #include "opt_gateway.h" 111 #include "opt_ppp.h" 112 #endif 113 114 #ifdef INET 115 #define VJC 116 #endif 117 #define PPP_COMPRESS 118 119 #include <sys/param.h> 120 #include <sys/proc.h> 121 #include <sys/mbuf.h> 122 #include <sys/socket.h> 123 #include <sys/ioctl.h> 124 #include <sys/kernel.h> 125 #include <sys/systm.h> 126 #include <sys/time.h> 127 #include <sys/malloc.h> 128 #include <sys/module.h> 129 #include <sys/mutex.h> 130 #include <sys/once.h> 131 #include <sys/conf.h> 132 #include <sys/kauth.h> 133 #include <sys/intr.h> 134 #include <sys/socketvar.h> 135 #include <sys/device.h> 136 #include <sys/module.h> 137 138 #include <net/if.h> 139 #include <net/if_types.h> 140 #include <net/route.h> 141 142 #include <netinet/in.h> 143 #include <netinet/in_systm.h> 144 #include <netinet/in_var.h> 145 #ifdef INET 146 #include <netinet/ip.h> 147 #endif 148 149 #include <net/bpf.h> 150 #include <net/slip.h> 151 152 #ifdef VJC 153 #include <net/slcompress.h> 154 #endif 155 156 #include <net/ppp_defs.h> 157 #include <net/if_ppp.h> 158 #include <net/if_pppvar.h> 159 #include <sys/cpu.h> 160 161 #ifdef PPP_COMPRESS 162 #define PACKETPTR struct mbuf * 163 #include <net/ppp-comp.h> 164 #endif 165 166 #include "ioconf.h" 167 168 static int pppsioctl(struct ifnet *, u_long, void *); 169 static void ppp_requeue(struct ppp_softc *); 170 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd); 171 static void ppp_ccp_closed(struct ppp_softc *); 172 static void ppp_inproc(struct ppp_softc *, struct mbuf *); 173 static void pppdumpm(struct mbuf *m0); 174 #ifdef ALTQ 175 static void ppp_ifstart(struct ifnet *ifp); 176 #endif 177 178 static void pppintr(void *); 179 180 extern struct linesw ppp_disc; 181 182 /* 183 * We define two link layer specific mbuf flags, to mark high-priority 184 * packets for output, and received packets following lost/corrupted 185 * packets. 186 */ 187 #define M_HIGHPRI M_LINK0 /* output packet for sc_fastq */ 188 #define M_ERRMARK M_LINK1 /* rx packet following lost/corrupted pkt */ 189 190 static int ppp_clone_create(struct if_clone *, int); 191 static int ppp_clone_destroy(struct ifnet *); 192 193 static struct ppp_softc *ppp_create(const char *, int); 194 195 static struct { 196 LIST_HEAD(ppp_sclist, ppp_softc) list; 197 kmutex_t lock; 198 } ppp_softcs __cacheline_aligned; 199 200 struct if_clone ppp_cloner = 201 IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy); 202 203 #ifdef PPP_COMPRESS 204 static LIST_HEAD(, compressor) ppp_compressors = { NULL }; 205 static kmutex_t ppp_compressors_mtx; 206 207 static int ppp_compressor_init(void); 208 static int ppp_compressor_destroy(void); 209 static struct compressor *ppp_get_compressor(uint8_t); 210 static void ppp_compressor_rele(struct compressor *); 211 #endif /* PPP_COMPRESS */ 212 213 214 /* 215 * Called from boot code to establish ppp interfaces. 216 */ 217 void 218 pppattach(int n __unused) 219 { 220 221 /* 222 * Nothing to do here, initialization is handled by the 223 * module initialization code in pppinit() below). 224 */ 225 } 226 227 static void 228 pppinit(void) 229 { 230 /* Init the compressor sub-sub-system */ 231 ppp_compressor_init(); 232 233 if (ttyldisc_attach(&ppp_disc) != 0) 234 panic("%s", __func__); 235 236 mutex_init(&ppp_softcs.lock, MUTEX_DEFAULT, IPL_NONE); 237 LIST_INIT(&ppp_softcs.list); 238 if_clone_attach(&ppp_cloner); 239 } 240 241 static int 242 pppdetach(void) 243 { 244 int error = 0; 245 246 if (!LIST_EMPTY(&ppp_softcs.list)) 247 error = EBUSY; 248 249 if (error == 0) 250 error = ttyldisc_detach(&ppp_disc); 251 252 if (error == 0) { 253 mutex_destroy(&ppp_softcs.lock); 254 if_clone_detach(&ppp_cloner); 255 ppp_compressor_destroy(); 256 } 257 258 return error; 259 } 260 261 static struct ppp_softc * 262 ppp_create(const char *name, int unit) 263 { 264 struct ppp_softc *sc, *sci, *scl = NULL; 265 266 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO); 267 268 mutex_enter(&ppp_softcs.lock); 269 if (unit == -1) { 270 int i = 0; 271 LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) { 272 scl = sci; 273 if (i < sci->sc_unit) { 274 unit = i; 275 break; 276 } else { 277 #ifdef DIAGNOSTIC 278 KASSERT(i == sci->sc_unit); 279 #endif 280 i++; 281 } 282 } 283 if (unit == -1) 284 unit = i; 285 } else { 286 LIST_FOREACH(sci, &ppp_softcs.list, sc_iflist) { 287 scl = sci; 288 if (unit < sci->sc_unit) 289 break; 290 else if (unit == sci->sc_unit) { 291 free(sc, M_DEVBUF); 292 mutex_exit(&ppp_softcs.lock); 293 return NULL; 294 } 295 } 296 } 297 298 if (sci != NULL) 299 LIST_INSERT_BEFORE(sci, sc, sc_iflist); 300 else if (scl != NULL) 301 LIST_INSERT_AFTER(scl, sc, sc_iflist); 302 else 303 LIST_INSERT_HEAD(&ppp_softcs.list, sc, sc_iflist); 304 305 mutex_exit(&ppp_softcs.lock); 306 307 if_initname(&sc->sc_if, name, sc->sc_unit = unit); 308 callout_init(&sc->sc_timo_ch, 0); 309 sc->sc_if.if_softc = sc; 310 sc->sc_if.if_mtu = PPP_MTU; 311 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 312 sc->sc_if.if_type = IFT_PPP; 313 sc->sc_if.if_hdrlen = PPP_HDRLEN; 314 sc->sc_if.if_dlt = DLT_NULL; 315 sc->sc_if.if_ioctl = pppsioctl; 316 sc->sc_if.if_output = pppoutput; 317 #ifdef ALTQ 318 sc->sc_if.if_start = ppp_ifstart; 319 #endif 320 IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN); 321 IFQ_SET_MAXLEN(&sc->sc_inq, IFQ_MAXLEN); 322 IFQ_SET_MAXLEN(&sc->sc_fastq, IFQ_MAXLEN); 323 IFQ_SET_MAXLEN(&sc->sc_rawq, IFQ_MAXLEN); 324 325 IFQ_LOCK_INIT(&sc->sc_fastq); 326 327 /* Ratio of 1:2 packets between the regular and the fast queue */ 328 sc->sc_maxfastq = 2; 329 IFQ_SET_READY(&sc->sc_if.if_snd); 330 if_attach(&sc->sc_if); 331 if_alloc_sadl(&sc->sc_if); 332 bpf_attach(&sc->sc_if, DLT_NULL, 0); 333 return sc; 334 } 335 336 static int 337 ppp_clone_create(struct if_clone *ifc, int unit) 338 { 339 return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0; 340 } 341 342 static int 343 ppp_clone_destroy(struct ifnet *ifp) 344 { 345 struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc; 346 347 if (sc->sc_devp != NULL) 348 return EBUSY; /* Not removing it */ 349 350 mutex_enter(&ppp_softcs.lock); 351 LIST_REMOVE(sc, sc_iflist); 352 mutex_exit(&ppp_softcs.lock); 353 354 bpf_detach(ifp); 355 if_detach(ifp); 356 357 IFQ_LOCK_DESTROY(&sc->sc_fastq); 358 359 free(sc, M_DEVBUF); 360 return 0; 361 } 362 363 /* 364 * Allocate a ppp interface unit and initialize it. 365 */ 366 struct ppp_softc * 367 pppalloc(pid_t pid) 368 { 369 struct ppp_softc *sc = NULL, *scf; 370 int i; 371 372 mutex_enter(&ppp_softcs.lock); 373 LIST_FOREACH(scf, &ppp_softcs.list, sc_iflist) { 374 if (scf->sc_xfer == pid) { 375 scf->sc_xfer = 0; 376 mutex_exit(&ppp_softcs.lock); 377 return scf; 378 } 379 if (scf->sc_devp == NULL && sc == NULL) 380 sc = scf; 381 } 382 mutex_exit(&ppp_softcs.lock); 383 384 if (sc == NULL) 385 sc = ppp_create(ppp_cloner.ifc_name, -1); 386 387 sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc); 388 if (sc->sc_si == NULL) { 389 printf("%s: unable to establish softintr\n", 390 sc->sc_if.if_xname); 391 return NULL; 392 } 393 sc->sc_flags = 0; 394 sc->sc_mru = PPP_MRU; 395 sc->sc_relinq = NULL; 396 (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats)); 397 #ifdef VJC 398 sc->sc_comp = malloc(sizeof(struct slcompress), M_DEVBUF, M_NOWAIT); 399 if (sc->sc_comp) 400 sl_compress_init(sc->sc_comp); 401 #endif 402 #ifdef PPP_COMPRESS 403 sc->sc_xc_state = NULL; 404 sc->sc_rc_state = NULL; 405 #endif /* PPP_COMPRESS */ 406 for (i = 0; i < NUM_NP; ++i) 407 sc->sc_npmode[i] = NPMODE_ERROR; 408 sc->sc_npqueue = NULL; 409 sc->sc_npqtail = &sc->sc_npqueue; 410 sc->sc_last_sent = sc->sc_last_recv = time_second; 411 412 return sc; 413 } 414 415 /* 416 * Deallocate a ppp unit. Must be called at splsoftnet or higher. 417 */ 418 void 419 pppdealloc(struct ppp_softc *sc) 420 { 421 struct mbuf *m; 422 423 softint_disestablish(sc->sc_si); 424 if_down(&sc->sc_if); 425 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 426 sc->sc_devp = NULL; 427 sc->sc_xfer = 0; 428 for (;;) { 429 IF_DEQUEUE(&sc->sc_rawq, m); 430 if (m == NULL) 431 break; 432 m_freem(m); 433 } 434 for (;;) { 435 IF_DEQUEUE(&sc->sc_inq, m); 436 if (m == NULL) 437 break; 438 m_freem(m); 439 } 440 for (;;) { 441 IF_DEQUEUE(&sc->sc_fastq, m); 442 if (m == NULL) 443 break; 444 m_freem(m); 445 } 446 while ((m = sc->sc_npqueue) != NULL) { 447 sc->sc_npqueue = m->m_nextpkt; 448 m_freem(m); 449 } 450 m_freem(sc->sc_togo); 451 sc->sc_togo = NULL; 452 #ifdef PPP_COMPRESS 453 ppp_ccp_closed(sc); 454 sc->sc_xc_state = NULL; 455 sc->sc_rc_state = NULL; 456 #endif /* PPP_COMPRESS */ 457 #ifdef PPP_FILTER 458 if (sc->sc_pass_filt_in.bf_insns != 0) { 459 free(sc->sc_pass_filt_in.bf_insns, M_DEVBUF); 460 sc->sc_pass_filt_in.bf_insns = 0; 461 sc->sc_pass_filt_in.bf_len = 0; 462 } 463 if (sc->sc_pass_filt_out.bf_insns != 0) { 464 free(sc->sc_pass_filt_out.bf_insns, M_DEVBUF); 465 sc->sc_pass_filt_out.bf_insns = 0; 466 sc->sc_pass_filt_out.bf_len = 0; 467 } 468 if (sc->sc_active_filt_in.bf_insns != 0) { 469 free(sc->sc_active_filt_in.bf_insns, M_DEVBUF); 470 sc->sc_active_filt_in.bf_insns = 0; 471 sc->sc_active_filt_in.bf_len = 0; 472 } 473 if (sc->sc_active_filt_out.bf_insns != 0) { 474 free(sc->sc_active_filt_out.bf_insns, M_DEVBUF); 475 sc->sc_active_filt_out.bf_insns = 0; 476 sc->sc_active_filt_out.bf_len = 0; 477 } 478 #endif /* PPP_FILTER */ 479 #ifdef VJC 480 if (sc->sc_comp != 0) { 481 free(sc->sc_comp, M_DEVBUF); 482 sc->sc_comp = 0; 483 } 484 #endif 485 (void)ppp_clone_destroy(&sc->sc_if); 486 } 487 488 /* 489 * Ioctl routine for generic ppp devices. 490 */ 491 int 492 pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag, 493 struct lwp *l) 494 { 495 int s, error, flags, mru, npx; 496 u_int nb; 497 struct ppp_option_data *odp; 498 struct compressor *cp; 499 struct npioctl *npi; 500 time_t t; 501 #ifdef PPP_FILTER 502 struct bpf_program *bp, *nbp; 503 struct bpf_insn *newcode, *oldcode; 504 int newcodelen; 505 #endif /* PPP_FILTER */ 506 #ifdef PPP_COMPRESS 507 u_char ccp_option[CCP_MAX_OPTION_LENGTH]; 508 #endif 509 510 switch (cmd) { 511 case PPPIOCSFLAGS: 512 case PPPIOCSMRU: 513 case PPPIOCSMAXCID: 514 case PPPIOCSCOMPRESS: 515 case PPPIOCSNPMODE: 516 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 517 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if, 518 KAUTH_ARG(cmd), NULL) != 0) 519 return EPERM; 520 break; 521 case PPPIOCXFERUNIT: 522 /* XXX: Why is this privileged?! */ 523 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 524 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if, 525 KAUTH_ARG(cmd), NULL) != 0) 526 return EPERM; 527 break; 528 default: 529 break; 530 } 531 532 switch (cmd) { 533 case FIONREAD: 534 *(int *)data = sc->sc_inq.ifq_len; 535 break; 536 537 case PPPIOCGUNIT: 538 *(int *)data = sc->sc_unit; 539 break; 540 541 case PPPIOCGFLAGS: 542 *(u_int *)data = sc->sc_flags; 543 break; 544 545 case PPPIOCGRAWIN: 546 { 547 struct ppp_rawin *rwin = (struct ppp_rawin *)data; 548 u_char c, q = 0; 549 550 for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);) 551 rwin->buf[q++] = sc->sc_rawin.buf[c++]; 552 553 for (c = 0; c < sc->sc_rawin_start;) 554 rwin->buf[q++] = sc->sc_rawin.buf[c++]; 555 556 rwin->count = sc->sc_rawin.count; 557 } 558 break; 559 560 case PPPIOCSFLAGS: 561 flags = *(int *)data & SC_MASK; 562 s = splsoftnet(); 563 #ifdef PPP_COMPRESS 564 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN)) 565 ppp_ccp_closed(sc); 566 #endif 567 splhigh(); /* XXX IMP ME HARDER */ 568 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags; 569 splx(s); 570 break; 571 572 case PPPIOCSMRU: 573 mru = *(int *)data; 574 if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU) 575 sc->sc_mru = mru; 576 break; 577 578 case PPPIOCGMRU: 579 *(int *)data = sc->sc_mru; 580 break; 581 582 #ifdef VJC 583 case PPPIOCSMAXCID: 584 if (sc->sc_comp) { 585 s = splsoftnet(); 586 sl_compress_setup(sc->sc_comp, *(int *)data); 587 splx(s); 588 } 589 break; 590 #endif 591 592 case PPPIOCXFERUNIT: 593 sc->sc_xfer = l->l_proc->p_pid; 594 break; 595 596 #ifdef PPP_COMPRESS 597 case PPPIOCSCOMPRESS: 598 odp = (struct ppp_option_data *)data; 599 nb = odp->length; 600 if (nb > sizeof(ccp_option)) 601 nb = sizeof(ccp_option); 602 if (nb < 3) 603 return EINVAL; 604 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0) 605 return error; 606 /* preliminary check on the length byte */ 607 if (ccp_option[1] < 2) 608 return EINVAL; 609 cp = ppp_get_compressor(ccp_option[0]); 610 if (cp == NULL) { 611 if (sc->sc_flags & SC_DEBUG) 612 printf("%s: no compressor for [%x %x %x], %x\n", 613 sc->sc_if.if_xname, ccp_option[0], 614 ccp_option[1], ccp_option[2], nb); 615 return EINVAL; /* no handler found */ 616 } 617 /* 618 * Found a handler for the protocol - try to allocate 619 * a compressor or decompressor. 620 */ 621 error = 0; 622 if (odp->transmit) { 623 s = splsoftnet(); 624 if (sc->sc_xc_state != NULL) { 625 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); 626 ppp_compressor_rele(sc->sc_xcomp); 627 } 628 sc->sc_xcomp = cp; 629 sc->sc_xc_state = cp->comp_alloc(ccp_option, nb); 630 if (sc->sc_xc_state == NULL) { 631 if (sc->sc_flags & SC_DEBUG) 632 printf("%s: comp_alloc failed\n", 633 sc->sc_if.if_xname); 634 error = ENOBUFS; 635 } 636 splhigh(); /* XXX IMP ME HARDER */ 637 sc->sc_flags &= ~SC_COMP_RUN; 638 splx(s); 639 } else { 640 s = splsoftnet(); 641 if (sc->sc_rc_state != NULL) { 642 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); 643 ppp_compressor_rele(sc->sc_rcomp); 644 } 645 sc->sc_rcomp = cp; 646 sc->sc_rc_state = cp->decomp_alloc(ccp_option, nb); 647 if (sc->sc_rc_state == NULL) { 648 if (sc->sc_flags & SC_DEBUG) 649 printf("%s: decomp_alloc failed\n", 650 sc->sc_if.if_xname); 651 error = ENOBUFS; 652 } 653 splhigh(); /* XXX IMP ME HARDER */ 654 sc->sc_flags &= ~SC_DECOMP_RUN; 655 splx(s); 656 } 657 return error; 658 #endif /* PPP_COMPRESS */ 659 660 case PPPIOCGNPMODE: 661 case PPPIOCSNPMODE: 662 npi = (struct npioctl *)data; 663 switch (npi->protocol) { 664 case PPP_IP: 665 npx = NP_IP; 666 break; 667 case PPP_IPV6: 668 npx = NP_IPV6; 669 break; 670 default: 671 return EINVAL; 672 } 673 if (cmd == PPPIOCGNPMODE) { 674 npi->mode = sc->sc_npmode[npx]; 675 } else { 676 if (npi->mode != sc->sc_npmode[npx]) { 677 s = splnet(); 678 sc->sc_npmode[npx] = npi->mode; 679 if (npi->mode != NPMODE_QUEUE) { 680 ppp_requeue(sc); 681 ppp_restart(sc); 682 } 683 splx(s); 684 } 685 } 686 break; 687 688 case PPPIOCGIDLE: 689 s = splsoftnet(); 690 t = time_second; 691 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent; 692 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv; 693 splx(s); 694 break; 695 696 #ifdef PPP_FILTER 697 case PPPIOCSPASS: 698 case PPPIOCSACTIVE: 699 /* These are no longer supported. */ 700 return EOPNOTSUPP; 701 702 case PPPIOCSIPASS: 703 case PPPIOCSOPASS: 704 case PPPIOCSIACTIVE: 705 case PPPIOCSOACTIVE: 706 nbp = (struct bpf_program *)data; 707 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) 708 return EINVAL; 709 newcodelen = nbp->bf_len * sizeof(struct bpf_insn); 710 if (newcodelen != 0) { 711 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK); 712 /* WAITOK -- malloc() never fails. */ 713 if ((error = copyin((void *)nbp->bf_insns, 714 (void *)newcode, newcodelen)) != 0) { 715 free(newcode, M_DEVBUF); 716 return error; 717 } 718 if (!bpf_validate(newcode, nbp->bf_len)) { 719 free(newcode, M_DEVBUF); 720 return EINVAL; 721 } 722 } else 723 newcode = 0; 724 switch (cmd) { 725 case PPPIOCSIPASS: 726 bp = &sc->sc_pass_filt_in; 727 break; 728 729 case PPPIOCSOPASS: 730 bp = &sc->sc_pass_filt_out; 731 break; 732 733 case PPPIOCSIACTIVE: 734 bp = &sc->sc_active_filt_in; 735 break; 736 737 case PPPIOCSOACTIVE: 738 bp = &sc->sc_active_filt_out; 739 break; 740 default: 741 free(newcode, M_DEVBUF); 742 return EPASSTHROUGH; 743 } 744 oldcode = bp->bf_insns; 745 s = splnet(); 746 bp->bf_len = nbp->bf_len; 747 bp->bf_insns = newcode; 748 splx(s); 749 if (oldcode != 0) 750 free(oldcode, M_DEVBUF); 751 break; 752 #endif /* PPP_FILTER */ 753 754 default: 755 return EPASSTHROUGH; 756 } 757 return 0; 758 } 759 760 /* 761 * Process an ioctl request to the ppp network interface. 762 */ 763 static int 764 pppsioctl(struct ifnet *ifp, u_long cmd, void *data) 765 { 766 struct ppp_softc *sc = ifp->if_softc; 767 struct ifaddr *ifa = (struct ifaddr *)data; 768 struct ifreq *ifr = (struct ifreq *)data; 769 struct ppp_stats *psp; 770 #ifdef PPP_COMPRESS 771 struct ppp_comp_stats *pcp; 772 #endif 773 int s = splnet(), error = 0; 774 775 switch (cmd) { 776 case SIOCSIFFLAGS: 777 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 778 break; 779 if ((ifp->if_flags & IFF_RUNNING) == 0) 780 ifp->if_flags &= ~IFF_UP; 781 break; 782 783 case SIOCINITIFADDR: 784 switch (ifa->ifa_addr->sa_family) { 785 #ifdef INET 786 case AF_INET: 787 break; 788 #endif 789 #ifdef INET6 790 case AF_INET6: 791 break; 792 #endif 793 default: 794 printf("%s: af%d not supported\n", ifp->if_xname, 795 ifa->ifa_addr->sa_family); 796 error = EAFNOSUPPORT; 797 break; 798 } 799 ifa->ifa_rtrequest = p2p_rtrequest; 800 break; 801 802 case SIOCADDMULTI: 803 case SIOCDELMULTI: 804 if (ifr == NULL) { 805 error = EAFNOSUPPORT; 806 break; 807 } 808 switch (ifreq_getaddr(cmd, ifr)->sa_family) { 809 #ifdef INET 810 case AF_INET: 811 break; 812 #endif 813 #ifdef INET6 814 case AF_INET6: 815 break; 816 #endif 817 default: 818 error = EAFNOSUPPORT; 819 break; 820 } 821 break; 822 823 case SIOCGPPPSTATS: 824 psp = &((struct ifpppstatsreq *)data)->stats; 825 memset(psp, 0, sizeof(*psp)); 826 psp->p = sc->sc_stats; 827 #if defined(VJC) && !defined(SL_NO_STATS) 828 if (sc->sc_comp) { 829 psp->vj.vjs_packets = sc->sc_comp->sls_packets; 830 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed; 831 psp->vj.vjs_searches = sc->sc_comp->sls_searches; 832 psp->vj.vjs_misses = sc->sc_comp->sls_misses; 833 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin; 834 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin; 835 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin; 836 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed; 837 } 838 #endif /* VJC */ 839 break; 840 841 #ifdef PPP_COMPRESS 842 case SIOCGPPPCSTATS: 843 pcp = &((struct ifpppcstatsreq *)data)->stats; 844 memset(pcp, 0, sizeof(*pcp)); 845 if (sc->sc_xc_state != NULL) 846 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c); 847 if (sc->sc_rc_state != NULL) 848 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d); 849 break; 850 #endif /* PPP_COMPRESS */ 851 852 default: 853 if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET) 854 error = 0; 855 break; 856 } 857 splx(s); 858 return error; 859 } 860 861 /* 862 * Queue a packet. Start transmission if not active. 863 * Packet is placed in Information field of PPP frame. 864 */ 865 int 866 pppoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, 867 const struct rtentry *rtp) 868 { 869 struct ppp_softc *sc = ifp->if_softc; 870 int protocol, address, control; 871 u_char *cp; 872 int s, error; 873 #ifdef INET 874 struct ip *ip; 875 #endif 876 struct ifqueue *ifq; 877 enum NPmode mode; 878 int len; 879 880 if (sc->sc_devp == NULL || 881 (ifp->if_flags & IFF_RUNNING) == 0 || 882 ((ifp->if_flags & IFF_UP) == 0 && 883 dst->sa_family != AF_UNSPEC)) { 884 error = ENETDOWN; /* sort of */ 885 goto bad; 886 } 887 888 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family); 889 890 /* 891 * Compute PPP header. 892 */ 893 m0->m_flags &= ~M_HIGHPRI; 894 switch (dst->sa_family) { 895 #ifdef INET 896 case AF_INET: 897 address = PPP_ALLSTATIONS; 898 control = PPP_UI; 899 protocol = PPP_IP; 900 mode = sc->sc_npmode[NP_IP]; 901 902 /* 903 * If this packet has the "low delay" bit set in the IP header, 904 * put it on the fastq instead. 905 */ 906 ip = mtod(m0, struct ip *); 907 if (ip->ip_tos & IPTOS_LOWDELAY) 908 m0->m_flags |= M_HIGHPRI; 909 break; 910 #endif 911 #ifdef INET6 912 case AF_INET6: 913 address = PPP_ALLSTATIONS; /*XXX*/ 914 control = PPP_UI; /*XXX*/ 915 protocol = PPP_IPV6; 916 mode = sc->sc_npmode[NP_IPV6]; 917 918 #if 0 /* XXX flowinfo/traffic class, maybe? */ 919 /* 920 * If this packet has the "low delay" bit set in the IP header, 921 * put it on the fastq instead. 922 */ 923 ip = mtod(m0, struct ip *); 924 if (ip->ip_tos & IPTOS_LOWDELAY) 925 m0->m_flags |= M_HIGHPRI; 926 #endif 927 break; 928 #endif 929 case AF_UNSPEC: 930 address = PPP_ADDRESS(dst->sa_data); 931 control = PPP_CONTROL(dst->sa_data); 932 protocol = PPP_PROTOCOL(dst->sa_data); 933 mode = NPMODE_PASS; 934 break; 935 default: 936 printf("%s: af%d not supported\n", ifp->if_xname, 937 dst->sa_family); 938 error = EAFNOSUPPORT; 939 goto bad; 940 } 941 942 /* 943 * Drop this packet, or return an error, if necessary. 944 */ 945 if (mode == NPMODE_ERROR) { 946 error = ENETDOWN; 947 goto bad; 948 } 949 if (mode == NPMODE_DROP) { 950 error = 0; 951 goto bad; 952 } 953 954 /* 955 * Add PPP header. 956 */ 957 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT); 958 if (m0 == NULL) { 959 error = ENOBUFS; 960 goto bad; 961 } 962 963 cp = mtod(m0, u_char *); 964 *cp++ = address; 965 *cp++ = control; 966 *cp++ = protocol >> 8; 967 *cp++ = protocol & 0xff; 968 969 len = m_length(m0); 970 971 if (sc->sc_flags & SC_LOG_OUTPKT) { 972 printf("%s output: ", ifp->if_xname); 973 pppdumpm(m0); 974 } 975 976 if ((protocol & 0x8000) == 0) { 977 #ifdef PPP_FILTER 978 /* 979 * Apply the pass and active filters to the packet, 980 * but only if it is a data packet. 981 */ 982 if (sc->sc_pass_filt_out.bf_insns != 0 && 983 bpf_filter(sc->sc_pass_filt_out.bf_insns, 984 (u_char *)m0, len, 0) == 0) { 985 error = 0; /* drop this packet */ 986 goto bad; 987 } 988 989 /* 990 * Update the time we sent the most recent packet. 991 */ 992 if (sc->sc_active_filt_out.bf_insns == 0 || 993 bpf_filter(sc->sc_active_filt_out.bf_insns, 994 (u_char *)m0, len, 0)) 995 sc->sc_last_sent = time_second; 996 #else 997 /* 998 * Update the time we sent the most recent packet. 999 */ 1000 sc->sc_last_sent = time_second; 1001 #endif /* PPP_FILTER */ 1002 } 1003 1004 /* 1005 * See if bpf wants to look at the packet. 1006 */ 1007 bpf_mtap(&sc->sc_if, m0, BPF_D_OUT); 1008 1009 /* 1010 * Put the packet on the appropriate queue. 1011 */ 1012 s = splnet(); 1013 if (mode == NPMODE_QUEUE) { 1014 /* XXX we should limit the number of packets on this queue */ 1015 *sc->sc_npqtail = m0; 1016 m0->m_nextpkt = NULL; 1017 sc->sc_npqtail = &m0->m_nextpkt; 1018 } else { 1019 ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL; 1020 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0)) != 0) { 1021 splx(s); 1022 if_statinc(&sc->sc_if, if_oerrors); 1023 sc->sc_stats.ppp_oerrors++; 1024 return error; 1025 } 1026 ppp_restart(sc); 1027 } 1028 if_statadd2(ifp, if_opackets, 1, if_obytes, len); 1029 1030 splx(s); 1031 return 0; 1032 1033 bad: 1034 m_freem(m0); 1035 return error; 1036 } 1037 1038 /* 1039 * After a change in the NPmode for some NP, move packets from the 1040 * npqueue to the send queue or the fast queue as appropriate. 1041 * Should be called at splnet, since we muck with the queues. 1042 */ 1043 static void 1044 ppp_requeue(struct ppp_softc *sc) 1045 { 1046 struct mbuf *m, **mpp; 1047 struct ifqueue *ifq; 1048 enum NPmode mode; 1049 int error; 1050 1051 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) { 1052 switch (PPP_PROTOCOL(mtod(m, u_char *))) { 1053 case PPP_IP: 1054 mode = sc->sc_npmode[NP_IP]; 1055 break; 1056 case PPP_IPV6: 1057 mode = sc->sc_npmode[NP_IPV6]; 1058 break; 1059 default: 1060 mode = NPMODE_PASS; 1061 } 1062 1063 switch (mode) { 1064 case NPMODE_PASS: 1065 /* 1066 * This packet can now go on one of the queues to 1067 * be sent. 1068 */ 1069 *mpp = m->m_nextpkt; 1070 m->m_nextpkt = NULL; 1071 ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL; 1072 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m)) != 0) { 1073 if_statinc(&sc->sc_if, if_oerrors); 1074 sc->sc_stats.ppp_oerrors++; 1075 } 1076 break; 1077 1078 case NPMODE_DROP: 1079 case NPMODE_ERROR: 1080 *mpp = m->m_nextpkt; 1081 m_freem(m); 1082 break; 1083 1084 case NPMODE_QUEUE: 1085 mpp = &m->m_nextpkt; 1086 break; 1087 } 1088 } 1089 sc->sc_npqtail = mpp; 1090 } 1091 1092 /* 1093 * Transmitter has finished outputting some stuff; 1094 * remember to call sc->sc_start later at splsoftnet. 1095 */ 1096 void 1097 ppp_restart(struct ppp_softc *sc) 1098 { 1099 int s = splhigh(); /* XXX IMP ME HARDER */ 1100 1101 sc->sc_flags &= ~SC_TBUSY; 1102 softint_schedule(sc->sc_si); 1103 splx(s); 1104 } 1105 1106 /* 1107 * Get a packet to send. This procedure is intended to be called at 1108 * splsoftnet, since it may involve time-consuming operations such as 1109 * applying VJ compression, packet compression, address/control and/or 1110 * protocol field compression to the packet. 1111 */ 1112 struct mbuf * 1113 ppp_dequeue(struct ppp_softc *sc) 1114 { 1115 struct mbuf *m, *mp; 1116 u_char *cp; 1117 int address, control, protocol; 1118 int s; 1119 1120 /* 1121 * Grab a packet to send: first try the fast queue, then the 1122 * normal queue. 1123 */ 1124 s = splnet(); 1125 if (sc->sc_nfastq < sc->sc_maxfastq) { 1126 IF_DEQUEUE(&sc->sc_fastq, m); 1127 if (m != NULL) 1128 sc->sc_nfastq++; 1129 else 1130 IFQ_DEQUEUE(&sc->sc_if.if_snd, m); 1131 } else { 1132 sc->sc_nfastq = 0; 1133 IFQ_DEQUEUE(&sc->sc_if.if_snd, m); 1134 if (m == NULL) { 1135 IF_DEQUEUE(&sc->sc_fastq, m); 1136 if (m != NULL) 1137 sc->sc_nfastq++; 1138 } 1139 } 1140 splx(s); 1141 1142 if (m == NULL) 1143 return NULL; 1144 1145 ++sc->sc_stats.ppp_opackets; 1146 1147 /* 1148 * Extract the ppp header of the new packet. 1149 * The ppp header will be in one mbuf. 1150 */ 1151 cp = mtod(m, u_char *); 1152 address = PPP_ADDRESS(cp); 1153 control = PPP_CONTROL(cp); 1154 protocol = PPP_PROTOCOL(cp); 1155 1156 switch (protocol) { 1157 case PPP_IP: 1158 #ifdef VJC 1159 /* 1160 * If the packet is a TCP/IP packet, see if we can compress it. 1161 */ 1162 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) { 1163 struct ip *ip; 1164 int type; 1165 1166 mp = m; 1167 ip = (struct ip *)(cp + PPP_HDRLEN); 1168 if (mp->m_len <= PPP_HDRLEN) { 1169 mp = mp->m_next; 1170 if (mp == NULL) 1171 break; 1172 ip = mtod(mp, struct ip *); 1173 } 1174 /* 1175 * This code assumes the IP/TCP header is in one 1176 * non-shared mbuf 1177 */ 1178 if (ip->ip_p == IPPROTO_TCP) { 1179 type = sl_compress_tcp(mp, ip, sc->sc_comp, 1180 !(sc->sc_flags & SC_NO_TCP_CCID)); 1181 switch (type) { 1182 case TYPE_UNCOMPRESSED_TCP: 1183 protocol = PPP_VJC_UNCOMP; 1184 break; 1185 case TYPE_COMPRESSED_TCP: 1186 protocol = PPP_VJC_COMP; 1187 cp = mtod(m, u_char *); 1188 cp[0] = address; /* Header has moved */ 1189 cp[1] = control; 1190 cp[2] = 0; 1191 break; 1192 } 1193 /* Update protocol in PPP header */ 1194 cp[3] = protocol; 1195 } 1196 } 1197 #endif /* VJC */ 1198 break; 1199 1200 #ifdef PPP_COMPRESS 1201 case PPP_CCP: 1202 ppp_ccp(sc, m, 0); 1203 break; 1204 #endif /* PPP_COMPRESS */ 1205 } 1206 1207 #ifdef PPP_COMPRESS 1208 if (protocol != PPP_LCP && protocol != PPP_CCP && 1209 sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) { 1210 struct mbuf *mcomp = NULL; 1211 int slen; 1212 1213 slen = 0; 1214 for (mp = m; mp != NULL; mp = mp->m_next) 1215 slen += mp->m_len; 1216 (*sc->sc_xcomp->compress) 1217 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN); 1218 if (mcomp != NULL) { 1219 if (sc->sc_flags & SC_CCP_UP) { 1220 /* 1221 * Send the compressed packet instead of the 1222 * original. 1223 */ 1224 m_freem(m); 1225 m = mcomp; 1226 cp = mtod(m, u_char *); 1227 protocol = cp[3]; 1228 } else { 1229 /* 1230 * Can't transmit compressed packets until CCP 1231 * is up. 1232 */ 1233 m_freem(mcomp); 1234 } 1235 } 1236 } 1237 #endif /* PPP_COMPRESS */ 1238 1239 /* 1240 * Compress the address/control and protocol, if possible. 1241 */ 1242 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS && 1243 control == PPP_UI && protocol != PPP_ALLSTATIONS && 1244 protocol != PPP_LCP) { 1245 /* can compress address/control */ 1246 m->m_data += 2; 1247 m->m_len -= 2; 1248 } 1249 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) { 1250 /* can compress protocol */ 1251 if (mtod(m, u_char *) == cp) { 1252 cp[2] = cp[1]; /* move address/control up */ 1253 cp[1] = cp[0]; 1254 } 1255 ++m->m_data; 1256 --m->m_len; 1257 } 1258 1259 return m; 1260 } 1261 1262 /* 1263 * Software interrupt routine, called at splsoftnet. 1264 */ 1265 static void 1266 pppintr(void *arg) 1267 { 1268 struct ppp_softc *sc = arg; 1269 struct mbuf *m; 1270 int s; 1271 1272 mutex_enter(softnet_lock); 1273 if (!(sc->sc_flags & SC_TBUSY) && 1274 (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || 1275 sc->sc_fastq.ifq_head || 1276 sc->sc_outm)) { 1277 s = splhigh(); /* XXX IMP ME HARDER */ 1278 sc->sc_flags |= SC_TBUSY; 1279 splx(s); 1280 (*sc->sc_start)(sc); 1281 } 1282 for (;;) { 1283 s = splnet(); 1284 IF_DEQUEUE(&sc->sc_rawq, m); 1285 splx(s); 1286 if (m == NULL) 1287 break; 1288 ppp_inproc(sc, m); 1289 } 1290 mutex_exit(softnet_lock); 1291 } 1292 1293 #ifdef PPP_COMPRESS 1294 /* 1295 * Handle a CCP packet. `rcvd' is 1 if the packet was received, 1296 * 0 if it is about to be transmitted. 1297 */ 1298 static void 1299 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd) 1300 { 1301 u_char *dp, *ep; 1302 struct mbuf *mp; 1303 int slen, s; 1304 1305 /* 1306 * Get a pointer to the data after the PPP header. 1307 */ 1308 if (m->m_len <= PPP_HDRLEN) { 1309 mp = m->m_next; 1310 if (mp == NULL) 1311 return; 1312 dp = mtod(mp, u_char *); 1313 } else { 1314 mp = m; 1315 dp = mtod(mp, u_char *) + PPP_HDRLEN; 1316 } 1317 1318 ep = mtod(mp, u_char *) + mp->m_len; 1319 if (dp + CCP_HDRLEN > ep) 1320 return; 1321 slen = CCP_LENGTH(dp); 1322 if (dp + slen > ep) { 1323 if (sc->sc_flags & SC_DEBUG) 1324 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n", 1325 dp, slen, mtod(mp, u_char *), mp->m_len); 1326 return; 1327 } 1328 1329 switch (CCP_CODE(dp)) { 1330 case CCP_CONFREQ: 1331 case CCP_TERMREQ: 1332 case CCP_TERMACK: 1333 /* CCP must be going down - disable compression */ 1334 if (sc->sc_flags & SC_CCP_UP) { 1335 s = splhigh(); /* XXX IMP ME HARDER */ 1336 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN); 1337 splx(s); 1338 } 1339 break; 1340 1341 case CCP_CONFACK: 1342 if (sc->sc_flags & SC_CCP_OPEN && 1343 !(sc->sc_flags & SC_CCP_UP) && 1344 slen >= CCP_HDRLEN + CCP_OPT_MINLEN && 1345 slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) { 1346 if (!rcvd) { 1347 /* We're agreeing to send compressed packets. */ 1348 if (sc->sc_xc_state != NULL && 1349 (*sc->sc_xcomp->comp_init)(sc->sc_xc_state, 1350 dp + CCP_HDRLEN, slen - CCP_HDRLEN, 1351 sc->sc_unit, 0, 1352 sc->sc_flags & SC_DEBUG)) { 1353 s = splhigh(); /* XXX IMP ME HARDER */ 1354 sc->sc_flags |= SC_COMP_RUN; 1355 splx(s); 1356 } 1357 } else { 1358 /* 1359 * Peer is agreeing to send compressed 1360 * packets. 1361 */ 1362 if (sc->sc_rc_state != NULL && 1363 (*sc->sc_rcomp->decomp_init)( 1364 sc->sc_rc_state, 1365 dp + CCP_HDRLEN, slen - CCP_HDRLEN, 1366 sc->sc_unit, 0, sc->sc_mru, 1367 sc->sc_flags & SC_DEBUG)) { 1368 s = splhigh(); /* XXX IMP ME HARDER */ 1369 sc->sc_flags |= SC_DECOMP_RUN; 1370 sc->sc_flags &= 1371 ~(SC_DC_ERROR | SC_DC_FERROR); 1372 splx(s); 1373 } 1374 } 1375 } 1376 break; 1377 1378 case CCP_RESETACK: 1379 if (sc->sc_flags & SC_CCP_UP) { 1380 if (!rcvd) { 1381 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) 1382 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state); 1383 } else { 1384 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) { 1385 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state); 1386 s = splhigh(); /* XXX IMP ME HARDER */ 1387 sc->sc_flags &= ~SC_DC_ERROR; 1388 splx(s); 1389 } 1390 } 1391 } 1392 break; 1393 } 1394 } 1395 1396 /* 1397 * CCP is down; free (de)compressor state if necessary. 1398 */ 1399 static void 1400 ppp_ccp_closed(struct ppp_softc *sc) 1401 { 1402 if (sc->sc_xc_state) { 1403 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); 1404 ppp_compressor_rele(sc->sc_xcomp); 1405 sc->sc_xc_state = NULL; 1406 } 1407 if (sc->sc_rc_state) { 1408 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); 1409 ppp_compressor_rele(sc->sc_rcomp); 1410 sc->sc_rc_state = NULL; 1411 } 1412 } 1413 #endif /* PPP_COMPRESS */ 1414 1415 /* 1416 * PPP packet input routine. 1417 * The caller has checked and removed the FCS and has inserted 1418 * the address/control bytes and the protocol high byte if they 1419 * were omitted. 1420 */ 1421 void 1422 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost) 1423 { 1424 int s = splhigh(); /* XXX IMP ME HARDER */ 1425 1426 if (lost) 1427 m->m_flags |= M_ERRMARK; 1428 IF_ENQUEUE(&sc->sc_rawq, m); 1429 softint_schedule(sc->sc_si); 1430 splx(s); 1431 } 1432 1433 /* 1434 * Process a received PPP packet, doing decompression as necessary. 1435 * Should be called at splsoftnet. 1436 */ 1437 #define COMPTYPE(proto) \ 1438 ((proto) == PPP_VJC_COMP ? TYPE_COMPRESSED_TCP \ 1439 : TYPE_UNCOMPRESSED_TCP) 1440 1441 static void 1442 ppp_inproc(struct ppp_softc *sc, struct mbuf *m) 1443 { 1444 struct ifnet *ifp = &sc->sc_if; 1445 pktqueue_t *pktq = NULL; 1446 struct ifqueue *inq = NULL; 1447 int s, ilen, proto, rv; 1448 u_char *cp, adrs, ctrl; 1449 struct mbuf *mp, *dmp = NULL; 1450 #ifdef VJC 1451 int xlen; 1452 u_char *iphdr; 1453 u_int hlen; 1454 #endif 1455 1456 sc->sc_stats.ppp_ipackets++; 1457 1458 if (sc->sc_flags & SC_LOG_INPKT) { 1459 ilen = 0; 1460 for (mp = m; mp != NULL; mp = mp->m_next) 1461 ilen += mp->m_len; 1462 printf("%s: got %d bytes\n", ifp->if_xname, ilen); 1463 pppdumpm(m); 1464 } 1465 1466 cp = mtod(m, u_char *); 1467 adrs = PPP_ADDRESS(cp); 1468 ctrl = PPP_CONTROL(cp); 1469 proto = PPP_PROTOCOL(cp); 1470 1471 if (m->m_flags & M_ERRMARK) { 1472 m->m_flags &= ~M_ERRMARK; 1473 s = splhigh(); /* XXX IMP ME HARDER */ 1474 sc->sc_flags |= SC_VJ_RESET; 1475 splx(s); 1476 } 1477 1478 #ifdef PPP_COMPRESS 1479 /* 1480 * Decompress this packet if necessary, update the receiver's 1481 * dictionary, or take appropriate action on a CCP packet. 1482 */ 1483 if (proto == PPP_COMP && 1484 sc->sc_rc_state && 1485 (sc->sc_flags & SC_DECOMP_RUN) && 1486 !(sc->sc_flags & SC_DC_ERROR) && 1487 !(sc->sc_flags & SC_DC_FERROR)) { 1488 /* Decompress this packet */ 1489 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp); 1490 if (rv == DECOMP_OK) { 1491 m_freem(m); 1492 if (dmp == NULL) { 1493 /* 1494 * No error, but no decompressed packet 1495 * produced 1496 */ 1497 return; 1498 } 1499 m = dmp; 1500 cp = mtod(m, u_char *); 1501 proto = PPP_PROTOCOL(cp); 1502 1503 } else { 1504 /* 1505 * An error has occurred in decompression. 1506 * Pass the compressed packet up to pppd, which may 1507 * take CCP down or issue a Reset-Req. 1508 */ 1509 if (sc->sc_flags & SC_DEBUG) 1510 printf("%s: decompress failed %d\n", 1511 ifp->if_xname, rv); 1512 s = splhigh(); /* XXX IMP ME HARDER */ 1513 sc->sc_flags |= SC_VJ_RESET; 1514 if (rv == DECOMP_ERROR) 1515 sc->sc_flags |= SC_DC_ERROR; 1516 else 1517 sc->sc_flags |= SC_DC_FERROR; 1518 splx(s); 1519 } 1520 1521 } else { 1522 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) 1523 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m); 1524 if (proto == PPP_CCP) 1525 ppp_ccp(sc, m, 1); 1526 } 1527 #endif 1528 1529 ilen = 0; 1530 for (mp = m; mp != NULL; mp = mp->m_next) 1531 ilen += mp->m_len; 1532 1533 #ifdef VJC 1534 if (sc->sc_flags & SC_VJ_RESET) { 1535 /* 1536 * If we've missed a packet, we must toss subsequent compressed 1537 * packets which don't have an explicit connection ID. 1538 */ 1539 if (sc->sc_comp) 1540 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp); 1541 s = splhigh(); /* XXX IMP ME HARDER */ 1542 sc->sc_flags &= ~SC_VJ_RESET; 1543 splx(s); 1544 } 1545 1546 /* 1547 * See if we have a VJ-compressed packet to uncompress. 1548 */ 1549 if (proto == PPP_VJC_COMP) { 1550 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0) 1551 goto bad; 1552 1553 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, 1554 m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN, 1555 TYPE_COMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen); 1556 if (xlen <= 0) { 1557 if (sc->sc_flags & SC_DEBUG) { 1558 printf("%s: VJ uncompress failed" 1559 " on type comp\n", 1560 ifp->if_xname); 1561 } 1562 goto bad; 1563 } 1564 1565 /* Copy the PPP and IP headers into a new mbuf. */ 1566 MGETHDR(mp, M_DONTWAIT, MT_DATA); 1567 if (mp == NULL) 1568 goto bad; 1569 mp->m_len = 0; 1570 mp->m_next = NULL; 1571 if (hlen + PPP_HDRLEN > MHLEN) { 1572 MCLGET(mp, M_DONTWAIT); 1573 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) { 1574 /* Lose if big headers and no clusters */ 1575 m_freem(mp); 1576 goto bad; 1577 } 1578 } 1579 cp = mtod(mp, u_char *); 1580 cp[0] = adrs; 1581 cp[1] = ctrl; 1582 cp[2] = 0; 1583 cp[3] = PPP_IP; 1584 proto = PPP_IP; 1585 bcopy(iphdr, cp + PPP_HDRLEN, hlen); 1586 mp->m_len = hlen + PPP_HDRLEN; 1587 1588 /* 1589 * Trim the PPP and VJ headers off the old mbuf 1590 * and stick the new and old mbufs together. 1591 */ 1592 m->m_data += PPP_HDRLEN + xlen; 1593 m->m_len -= PPP_HDRLEN + xlen; 1594 if (m->m_len <= M_TRAILINGSPACE(mp)) { 1595 bcopy(mtod(m, u_char *), 1596 mtod(mp, u_char *) + mp->m_len, m->m_len); 1597 mp->m_len += m->m_len; 1598 mp->m_next = m_free(m); 1599 } else 1600 mp->m_next = m; 1601 m = mp; 1602 ilen += hlen - xlen; 1603 1604 } else if (proto == PPP_VJC_UNCOMP) { 1605 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0) 1606 goto bad; 1607 1608 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, 1609 m->m_len - PPP_HDRLEN, ilen - PPP_HDRLEN, 1610 TYPE_UNCOMPRESSED_TCP, sc->sc_comp, &iphdr, &hlen); 1611 if (xlen < 0) { 1612 if (sc->sc_flags & SC_DEBUG) { 1613 printf("%s: VJ uncompress failed" 1614 " on type uncomp\n", 1615 ifp->if_xname); 1616 } 1617 goto bad; 1618 } 1619 1620 proto = PPP_IP; 1621 cp[3] = PPP_IP; 1622 } 1623 #endif /* VJC */ 1624 1625 /* 1626 * If the packet will fit in a header mbuf, don't waste a 1627 * whole cluster on it. 1628 */ 1629 if (ilen <= MHLEN && (m->m_flags & M_EXT)) { 1630 MGETHDR(mp, M_DONTWAIT, MT_DATA); 1631 if (mp != NULL) { 1632 m_copydata(m, 0, ilen, mtod(mp, void *)); 1633 m_freem(m); 1634 m = mp; 1635 m->m_len = ilen; 1636 } 1637 } 1638 m->m_pkthdr.len = ilen; 1639 m_set_rcvif(m, ifp); 1640 1641 if ((proto & 0x8000) == 0) { 1642 #ifdef PPP_FILTER 1643 /* 1644 * See whether we want to pass this packet, and 1645 * if it counts as link activity. 1646 */ 1647 if (sc->sc_pass_filt_in.bf_insns != 0 && 1648 bpf_filter(sc->sc_pass_filt_in.bf_insns, 1649 (u_char *)m, ilen, 0) == 0) { 1650 /* drop this packet */ 1651 m_freem(m); 1652 return; 1653 } 1654 if (sc->sc_active_filt_in.bf_insns == 0 || 1655 bpf_filter(sc->sc_active_filt_in.bf_insns, 1656 (u_char *)m, ilen, 0)) 1657 sc->sc_last_recv = time_second; 1658 #else 1659 /* 1660 * Record the time that we received this packet. 1661 */ 1662 sc->sc_last_recv = time_second; 1663 #endif /* PPP_FILTER */ 1664 } 1665 1666 /* See if bpf wants to look at the packet. */ 1667 bpf_mtap(&sc->sc_if, m, BPF_D_IN); 1668 1669 switch (proto) { 1670 #ifdef INET 1671 case PPP_IP: 1672 /* 1673 * IP packet - take off the ppp header and pass it up to IP. 1674 */ 1675 if ((ifp->if_flags & IFF_UP) == 0 || 1676 sc->sc_npmode[NP_IP] != NPMODE_PASS) { 1677 /* Interface is down - drop the packet. */ 1678 m_freem(m); 1679 return; 1680 } 1681 m->m_pkthdr.len -= PPP_HDRLEN; 1682 m->m_data += PPP_HDRLEN; 1683 m->m_len -= PPP_HDRLEN; 1684 #ifdef GATEWAY 1685 if (ipflow_fastforward(m)) 1686 return; 1687 #endif 1688 pktq = ip_pktq; 1689 break; 1690 #endif 1691 1692 #ifdef INET6 1693 case PPP_IPV6: 1694 /* 1695 * IPv6 packet - take off the ppp header and pass it up to 1696 * IPv6. 1697 */ 1698 if ((ifp->if_flags & IFF_UP) == 0 || 1699 sc->sc_npmode[NP_IPV6] != NPMODE_PASS) { 1700 /* interface is down - drop the packet. */ 1701 m_freem(m); 1702 return; 1703 } 1704 m->m_pkthdr.len -= PPP_HDRLEN; 1705 m->m_data += PPP_HDRLEN; 1706 m->m_len -= PPP_HDRLEN; 1707 #ifdef GATEWAY 1708 if (ip6flow_fastforward(&m)) 1709 return; 1710 #endif 1711 pktq = ip6_pktq; 1712 break; 1713 #endif 1714 1715 default: 1716 /* 1717 * Some other protocol - place on input queue for read(). 1718 */ 1719 inq = &sc->sc_inq; 1720 pktq = NULL; 1721 break; 1722 } 1723 1724 /* 1725 * Put the packet on the appropriate input queue. 1726 */ 1727 s = splnet(); 1728 1729 /* pktq: inet or inet6 cases */ 1730 if (__predict_true(pktq)) { 1731 if (__predict_false(!pktq_enqueue(pktq, m, 0))) { 1732 splx(s); 1733 if_statinc(ifp, if_iqdrops); 1734 goto bad; 1735 } 1736 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen); 1737 splx(s); 1738 return; 1739 } 1740 1741 /* ifq: other protocol cases */ 1742 if (!inq) { 1743 splx(s); 1744 goto bad; 1745 } 1746 if (IF_QFULL(inq)) { 1747 IF_DROP(inq); 1748 splx(s); 1749 if (sc->sc_flags & SC_DEBUG) 1750 printf("%s: input queue full\n", ifp->if_xname); 1751 if_statinc(ifp, if_iqdrops); 1752 goto bad; 1753 } 1754 IF_ENQUEUE(inq, m); 1755 splx(s); 1756 if_statadd2(ifp, if_ipackets, 1, if_ibytes, ilen); 1757 1758 (*sc->sc_ctlp)(sc); 1759 1760 return; 1761 1762 bad: 1763 m_freem(m); 1764 if_statinc(&sc->sc_if, if_ierrors); 1765 sc->sc_stats.ppp_ierrors++; 1766 } 1767 1768 #define MAX_DUMP_BYTES 128 1769 1770 static void 1771 pppdumpm(struct mbuf *m0) 1772 { 1773 char buf[3*MAX_DUMP_BYTES+4]; 1774 char *bp = buf; 1775 struct mbuf *m; 1776 1777 for (m = m0; m; m = m->m_next) { 1778 int l = m->m_len; 1779 u_char *rptr = (u_char *)m->m_data; 1780 1781 while (l--) { 1782 if (bp > buf + sizeof(buf) - 4) 1783 goto done; 1784 /* Convert byte to ascii hex */ 1785 *bp++ = hexdigits[*rptr >> 4]; 1786 *bp++ = hexdigits[*rptr++ & 0xf]; 1787 } 1788 1789 if (m->m_next) { 1790 if (bp > buf + sizeof(buf) - 3) 1791 goto done; 1792 *bp++ = '|'; 1793 } else 1794 *bp++ = ' '; 1795 } 1796 done: 1797 if (m) 1798 *bp++ = '>'; 1799 *bp = 0; 1800 printf("%s\n", buf); 1801 } 1802 1803 #ifdef ALTQ 1804 /* 1805 * A wrapper to transmit a packet from if_start since ALTQ uses 1806 * if_start to send a packet. 1807 */ 1808 static void 1809 ppp_ifstart(struct ifnet *ifp) 1810 { 1811 struct ppp_softc *sc; 1812 1813 sc = ifp->if_softc; 1814 (*sc->sc_start)(sc); 1815 } 1816 #endif 1817 1818 static const struct ppp_known_compressor { 1819 uint8_t code; 1820 const char *module; 1821 } ppp_known_compressors[] = { 1822 { CI_DEFLATE, "ppp_deflate" }, 1823 { CI_DEFLATE_DRAFT, "ppp_deflate" }, 1824 { CI_BSD_COMPRESS, "ppp_bsdcomp" }, 1825 { CI_MPPE, "ppp_mppe" }, 1826 { 0, NULL } 1827 }; 1828 1829 static int 1830 ppp_compressor_init(void) 1831 { 1832 1833 mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE); 1834 return 0; 1835 } 1836 1837 static int 1838 ppp_compressor_destroy(void) 1839 { 1840 1841 mutex_destroy(&ppp_compressors_mtx); 1842 return 0; 1843 } 1844 1845 static void 1846 ppp_compressor_rele(struct compressor *cp) 1847 { 1848 1849 mutex_enter(&ppp_compressors_mtx); 1850 --cp->comp_refcnt; 1851 mutex_exit(&ppp_compressors_mtx); 1852 } 1853 1854 static struct compressor * 1855 ppp_get_compressor_noload(uint8_t ci, bool hold) 1856 { 1857 struct compressor *cp; 1858 1859 KASSERT(mutex_owned(&ppp_compressors_mtx)); 1860 LIST_FOREACH(cp, &ppp_compressors, comp_list) { 1861 if (cp->compress_proto == ci) { 1862 if (hold) 1863 ++cp->comp_refcnt; 1864 return cp; 1865 } 1866 } 1867 1868 return NULL; 1869 } 1870 1871 static struct compressor * 1872 ppp_get_compressor(uint8_t ci) 1873 { 1874 struct compressor *cp = NULL; 1875 const struct ppp_known_compressor *pkc; 1876 1877 mutex_enter(&ppp_compressors_mtx); 1878 cp = ppp_get_compressor_noload(ci, true); 1879 mutex_exit(&ppp_compressors_mtx); 1880 if (cp != NULL) 1881 return cp; 1882 1883 kernconfig_lock(); 1884 mutex_enter(&ppp_compressors_mtx); 1885 cp = ppp_get_compressor_noload(ci, true); 1886 mutex_exit(&ppp_compressors_mtx); 1887 if (cp == NULL) { 1888 /* Not found, so try to autoload a module */ 1889 for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) { 1890 if (pkc->code == ci) { 1891 if (module_autoload(pkc->module, 1892 MODULE_CLASS_MISC) != 0) 1893 break; 1894 mutex_enter(&ppp_compressors_mtx); 1895 cp = ppp_get_compressor_noload(ci, true); 1896 mutex_exit(&ppp_compressors_mtx); 1897 break; 1898 } 1899 } 1900 } 1901 kernconfig_unlock(); 1902 1903 return cp; 1904 } 1905 1906 int 1907 ppp_register_compressor(struct compressor *pc, size_t ncomp) 1908 { 1909 int error = 0; 1910 size_t i; 1911 1912 mutex_enter(&ppp_compressors_mtx); 1913 for (i = 0; i < ncomp; i++) { 1914 if (ppp_get_compressor_noload(pc[i].compress_proto, 1915 false) != NULL) 1916 error = EEXIST; 1917 } 1918 if (!error) { 1919 for (i = 0; i < ncomp; i++) { 1920 pc[i].comp_refcnt = 0; 1921 LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list); 1922 } 1923 } 1924 mutex_exit(&ppp_compressors_mtx); 1925 1926 return error; 1927 } 1928 1929 int 1930 ppp_unregister_compressor(struct compressor *pc, size_t ncomp) 1931 { 1932 int error = 0; 1933 size_t i; 1934 1935 mutex_enter(&ppp_compressors_mtx); 1936 for (i = 0; i < ncomp; i++) { 1937 if (ppp_get_compressor_noload(pc[i].compress_proto, 1938 false) != &pc[i]) 1939 error = ENOENT; 1940 else if (pc[i].comp_refcnt != 0) 1941 error = EBUSY; 1942 } 1943 if (!error) { 1944 for (i = 0; i < ncomp; i++) { 1945 LIST_REMOVE(&pc[i], comp_list); 1946 } 1947 } 1948 mutex_exit(&ppp_compressors_mtx); 1949 1950 return error; 1951 } 1952 1953 /* 1954 * Module infrastructure 1955 */ 1956 #include "if_module.h" 1957 1958 #ifdef PPP_FILTER 1959 #define PPP_DEP "bpf_filter," 1960 #else 1961 #define PPP_DEP 1962 #endif 1963 1964 IF_MODULE(MODULE_CLASS_DRIVER, ppp, PPP_DEP "slcompress") 1965