1 /* $NetBSD: if_pppoe.c,v 1.189 2026/07/28 07:10:42 yamaguchi Exp $ */ 2 3 /* 4 * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Martin Husemann <martin (at) NetBSD.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.189 2026/07/28 07:10:42 yamaguchi Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "pppoe.h" 37 #include "opt_sppp_filter.h" 38 #include "opt_pppoe.h" 39 #include "opt_net_mpsafe.h" 40 #endif 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/atomic.h> 46 #include <sys/callout.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/proc.h> 51 #include <sys/ioctl.h> 52 #include <sys/kauth.h> 53 #include <sys/intr.h> 54 #include <sys/socketvar.h> 55 #include <sys/device.h> 56 #include <sys/module.h> 57 #include <sys/sysctl.h> 58 #include <sys/rwlock.h> 59 #include <sys/mutex.h> 60 #include <sys/psref.h> 61 #include <sys/cprng.h> 62 #include <sys/workqueue.h> 63 64 #include <net/if.h> 65 #include <net/if_types.h> 66 #include <net/if_ether.h> 67 #include <net/if_sppp.h> 68 #include <net/if_spppvar.h> 69 #include <net/if_pppoe.h> 70 #include <net/if_dl.h> 71 72 #include <net/bpf.h> 73 74 #include "ioconf.h" 75 76 #ifdef NET_MPSAFE 77 #define PPPOE_MPSAFE 1 78 #endif 79 80 #ifndef PPPOE_DEQUEUE_MAXLEN 81 #define PPPOE_DEQUEUE_MAXLEN IFQ_MAXLEN 82 #endif 83 84 #ifndef PPPOE_RETRY_DELAY 85 #define PPPOE_RETRY_DELAY 60 86 #endif 87 88 struct pppoehdr { 89 uint8_t vertype; 90 uint8_t code; 91 uint16_t session; 92 uint16_t plen; 93 } __packed; 94 95 struct pppoetag { 96 uint16_t tag; 97 uint16_t len; 98 } __packed; 99 100 #define PPPOE_HEADERLEN sizeof(struct pppoehdr) 101 #define PPPOE_OVERHEAD (PPPOE_HEADERLEN + 2) 102 #define PPPOE_VERTYPE 0x11 /* VER=1, TYPE = 1 */ 103 104 #define PPPOE_TAG_EOL 0x0000 /* end of list */ 105 #define PPPOE_TAG_SNAME 0x0101 /* service name */ 106 #define PPPOE_TAG_ACNAME 0x0102 /* access concentrator name */ 107 #define PPPOE_TAG_HUNIQUE 0x0103 /* host unique */ 108 #define PPPOE_TAG_ACCOOKIE 0x0104 /* AC cookie */ 109 #define PPPOE_TAG_VENDOR 0x0105 /* vendor specific */ 110 #define PPPOE_TAG_RELAYSID 0x0110 /* relay session id */ 111 #define PPPOE_TAG_MAX_PAYLOAD 0x0120 /* max payload */ 112 #define PPPOE_TAG_SNAME_ERR 0x0201 /* service name error */ 113 #define PPPOE_TAG_ACSYS_ERR 0x0202 /* AC system error */ 114 #define PPPOE_TAG_GENERIC_ERR 0x0203 /* generic error */ 115 116 #define PPPOE_CODE_PADI 0x09 /* Active Discovery Initiation */ 117 #define PPPOE_CODE_PADO 0x07 /* Active Discovery Offer */ 118 #define PPPOE_CODE_PADR 0x19 /* Active Discovery Request */ 119 #define PPPOE_CODE_PADS 0x65 /* Active Discovery Session confirmation */ 120 #define PPPOE_CODE_PADT 0xA7 /* Active Discovery Terminate */ 121 122 /* two byte PPP protocol discriminator, then IP data */ 123 #define PPPOE_MAXMTU (ETHERMTU - PPPOE_OVERHEAD) 124 125 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */ 126 #define PPPOE_ADD_16(PTR, VAL) \ 127 *(PTR)++ = (VAL) / 256; \ 128 *(PTR)++ = (VAL) % 256 129 130 /* Add a complete PPPoE header to the buffer pointed to by PTR */ 131 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \ 132 *(PTR)++ = PPPOE_VERTYPE; \ 133 *(PTR)++ = (CODE); \ 134 PPPOE_ADD_16(PTR, SESS); \ 135 PPPOE_ADD_16(PTR, LEN) 136 137 #define PPPOE_DISC_TIMEOUT (hz*5) /* base for quick timeout calculation */ 138 #define PPPOE_SLOW_RETRY (hz*PPPOE_RETRY_DELAY) 139 /* persistent retry interval */ 140 #define PPPOE_RECON_FAST (hz*15) /* first retry after auth failure */ 141 #define PPPOE_RECON_IMMEDIATE (hz/10) /* "no delay" reconnect */ 142 #define PPPOE_RECON_PADTRCVD (hz*5) /* reconnect delay after PADT received */ 143 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */ 144 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */ 145 146 #ifdef PPPOE_SERVER 147 /* from if_spppsubr.c */ 148 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */ 149 #endif 150 151 #define PPPOE_LOCK(_sc, _op) rw_enter(&(_sc)->sc_lock, (_op)) 152 #define PPPOE_UNLOCK(_sc) rw_exit(&(_sc)->sc_lock) 153 #define PPPOE_WLOCKED(_sc) rw_write_held(&(_sc)->sc_lock) 154 155 #ifdef PPPOE_MPSAFE 156 #define DECLARE_SPLNET_VARIABLE 157 #define ACQUIRE_SPLNET() do { } while (0) 158 #define RELEASE_SPLNET() do { } while (0) 159 #else 160 #define DECLARE_SPLNET_VARIABLE int __s 161 #define ACQUIRE_SPLNET() do { \ 162 __s = splnet(); \ 163 } while (0) 164 #define RELEASE_SPLNET() do { \ 165 splx(__s); \ 166 } while (0) 167 #endif 168 169 #ifdef PPPOE_DEBUG 170 #define DPRINTF(_sc, _fmt, _arg...) pppoe_printf((_sc), (_fmt), ##_arg) 171 #else 172 #define DPRINTF(_sc, _fmt, _arg...) __nothing 173 #endif 174 175 struct pppoe_softc { 176 struct sppp sc_sppp; /* contains a struct ifnet as first element */ 177 LIST_ENTRY(pppoe_softc) sc_list; 178 struct ifnet *sc_eth_if; /* ethernet interface we are using */ 179 180 uint64_t sc_id; /* id of this softc, our hunique */ 181 int sc_state; /* discovery phase or session connected */ 182 struct ether_addr sc_dest; /* hardware address of concentrator */ 183 uint16_t sc_session; /* PPPoE session id */ 184 185 char *sc_service_name; /* if != NULL: requested name of service */ 186 char *sc_concentrator_name; /* if != NULL: requested concentrator id */ 187 uint8_t *sc_ac_cookie; /* content of AC cookie we must echo back */ 188 size_t sc_ac_cookie_len; /* length of cookie data */ 189 uint8_t *sc_relay_sid; /* content of relay SID we must echo back */ 190 size_t sc_relay_sid_len; /* length of relay SID data */ 191 #ifdef PPPOE_SERVER 192 uint8_t *sc_hunique; /* content of host unique we must echo back */ 193 size_t sc_hunique_len; /* length of host unique */ 194 #endif 195 callout_t sc_timeout; /* timeout while not in session state */ 196 struct workqueue *sc_timeout_wq; /* workqueue for timeout */ 197 struct work sc_timeout_wk; 198 u_int sc_timeout_scheduled; 199 int sc_padi_retried; /* number of PADI retries already done */ 200 int sc_padr_retried; /* number of PADR retries already done */ 201 krwlock_t sc_lock; /* lock of sc_state, sc_session, and sc_eth_if */ 202 bool sc_detaching; 203 }; 204 205 /* incoming traffic will be queued here */ 206 struct ifqueue ppoediscinq = { .ifq_maxlen = IFQ_MAXLEN }; 207 struct ifqueue ppoeinq = { .ifq_maxlen = IFQ_MAXLEN }; 208 209 void *pppoe_softintr = NULL; 210 static void pppoe_softintr_handler(void *); 211 212 extern int sppp_ioctl(struct ifnet *, unsigned long, void *); 213 214 /* input routines */ 215 static void pppoeintr(void); 216 static void pppoe_disc_input(struct mbuf *); 217 static void pppoe_dispatch_disc_pkt(struct mbuf *, int); 218 static void pppoe_data_input(struct mbuf *); 219 static void pppoe_enqueue(struct ifqueue *, struct mbuf *); 220 221 /* management routines */ 222 static int pppoe_connect(struct pppoe_softc *); 223 static int pppoe_disconnect(struct pppoe_softc *); 224 static void pppoe_abort_connect(struct pppoe_softc *); 225 static int pppoe_ioctl(struct ifnet *, unsigned long, void *); 226 static void pppoe_tls(struct sppp *); 227 static void pppoe_tlf(struct sppp *); 228 static void pppoe_start(struct ifnet *); 229 #ifdef PPPOE_MPSAFE 230 static int pppoe_transmit(struct ifnet *, struct mbuf *); 231 #endif 232 static void pppoe_clear_softc(struct pppoe_softc *, const char *); 233 static void pppoe_printf(struct pppoe_softc *, const char *, ...); 234 235 /* internal timeout handling */ 236 static void pppoe_timeout_co(void *); 237 static void pppoe_timeout_co_halt(void *); 238 static void pppoe_timeout_wk(struct work *, void *); 239 static void pppoe_timeout(struct pppoe_softc *); 240 241 /* sending actual protocol control packets */ 242 static int pppoe_send_padi(struct pppoe_softc *); 243 static int pppoe_send_padr(struct pppoe_softc *); 244 #ifdef PPPOE_SERVER 245 static int pppoe_send_pado(struct pppoe_softc *); 246 static int pppoe_send_pads(struct pppoe_softc *); 247 #endif 248 static int pppoe_send_padt(struct ifnet *, u_int, const uint8_t *); 249 250 /* raw output */ 251 static int pppoe_output(struct pppoe_softc *, struct mbuf *); 252 253 /* internal helper functions */ 254 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *, 255 krw_t); 256 static struct pppoe_softc * pppoe_find_softc_by_hunique(uint8_t *, size_t, 257 struct ifnet *, krw_t); 258 static struct mbuf *pppoe_get_mbuf(size_t len); 259 260 static void pppoe_ifattach_hook(void *, unsigned long, void *); 261 262 static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list; 263 static krwlock_t pppoe_softc_list_lock; 264 265 static int pppoe_clone_create(struct if_clone *, int); 266 static int pppoe_clone_destroy(struct ifnet *); 267 268 static bool pppoe_term_unknown = false; 269 static int pppoe_term_unknown_pps = 1; 270 271 static struct sysctllog *pppoe_sysctl_clog; 272 static void sysctl_net_pppoe_setup(struct sysctllog **); 273 274 static struct if_clone pppoe_cloner = 275 IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy); 276 277 /* ARGSUSED */ 278 void 279 pppoeattach(int count) 280 { 281 282 /* 283 * Nothing to do here, initialization is handled by the 284 * module initialization code in pppoeinit() below). 285 */ 286 } 287 288 static void 289 pppoeinit(void) 290 { 291 292 LIST_INIT(&pppoe_softc_list); 293 rw_init(&pppoe_softc_list_lock); 294 if_clone_attach(&pppoe_cloner); 295 296 pppoe_softintr = softint_establish(SOFTINT_MPSAFE|SOFTINT_NET, 297 pppoe_softintr_handler, NULL); 298 sysctl_net_pppoe_setup(&pppoe_sysctl_clog); 299 300 IFQ_LOCK_INIT(&ppoediscinq); 301 IFQ_LOCK_INIT(&ppoeinq); 302 } 303 304 static int 305 pppoedetach(void) 306 { 307 int error = 0; 308 309 rw_enter(&pppoe_softc_list_lock, RW_READER); 310 if (!LIST_EMPTY(&pppoe_softc_list)) { 311 rw_exit(&pppoe_softc_list_lock); 312 error = EBUSY; 313 } 314 315 if (error == 0) { 316 if_clone_detach(&pppoe_cloner); 317 softint_disestablish(pppoe_softintr); 318 /* Remove our sysctl sub-tree */ 319 sysctl_teardown(&pppoe_sysctl_clog); 320 } 321 322 return error; 323 } 324 325 static void 326 pppoe_softc_genid(uint64_t *id) 327 { 328 struct pppoe_softc *sc; 329 uint64_t rndid; 330 331 rw_enter(&pppoe_softc_list_lock, RW_READER); 332 333 while (1) { 334 rndid = cprng_strong64(); 335 336 sc = NULL; 337 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 338 if (sc->sc_id == rndid) 339 break; 340 } 341 if (sc == NULL) { 342 break; 343 } 344 } 345 346 rw_exit(&pppoe_softc_list_lock); 347 *id = rndid; 348 } 349 350 static int 351 pppoe_clone_create(struct if_clone *ifc, int unit) 352 { 353 struct pppoe_softc *sc; 354 struct ifnet *ifp; 355 int rv; 356 357 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 358 ifp = &sc->sc_sppp.pp_if; 359 360 rw_init(&sc->sc_lock); 361 pppoe_softc_genid(&sc->sc_id); 362 /* changed to real address later */ 363 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 364 365 if_initname(ifp, "pppoe", unit); 366 ifp->if_softc = sc; 367 ifp->if_mtu = PPPOE_MAXMTU; 368 ifp->if_flags = IFF_SIMPLEX|IFF_POINTOPOINT|IFF_MULTICAST; 369 #ifdef PPPOE_MPSAFE 370 ifp->if_extflags = IFEF_MPSAFE; 371 #endif 372 ifp->if_type = IFT_PPP; 373 ifp->if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN; 374 ifp->if_dlt = DLT_PPP_ETHER; 375 ifp->if_ioctl = pppoe_ioctl; 376 ifp->if_start = pppoe_start; 377 #ifdef PPPOE_MPSAFE 378 ifp->if_transmit = pppoe_transmit; 379 #endif 380 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 381 IFQ_SET_READY(&ifp->if_snd); 382 383 sc->sc_sppp.pp_tls = pppoe_tls; 384 sc->sc_sppp.pp_tlf = pppoe_tlf; 385 SET(sc->sc_sppp.pp_dev_flags, 386 (PP_DEVF_KEEPALIVE | /* use LCP keepalive */ 387 PP_DEVF_NOFRAMING)); /* no serial encapsulation */ 388 sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN; /* framing added to ppp packets */ 389 390 rv = workqueue_create(&sc->sc_timeout_wq, 391 ifp->if_xname, pppoe_timeout_wk, sc, 392 PRI_SOFTNET, IPL_SOFTNET, 0); 393 if (rv != 0) 394 goto destroy_sclock; 395 396 callout_init(&sc->sc_timeout, CALLOUT_MPSAFE); 397 callout_setfunc(&sc->sc_timeout, pppoe_timeout_co, sc); 398 399 if_initialize(ifp); 400 401 ifp->if_percpuq = if_percpuq_create(ifp); 402 403 rw_enter(&pppoe_softc_list_lock, RW_READER); 404 if (LIST_EMPTY(&pppoe_softc_list)) { 405 pfil_add_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil); 406 } 407 LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list); 408 rw_exit(&pppoe_softc_list_lock); 409 410 sppp_attach(ifp); 411 bpf_attach(ifp, DLT_PPP_ETHER, 0); 412 if_register(ifp); 413 414 return 0; 415 416 destroy_sclock: 417 rw_destroy(&sc->sc_lock); 418 kmem_free(sc, sizeof(*sc)); 419 420 return rv; 421 } 422 423 static int 424 pppoe_clone_destroy(struct ifnet *ifp) 425 { 426 struct pppoe_softc * sc = ifp->if_softc; 427 428 PPPOE_LOCK(sc, RW_WRITER); 429 /* stop ioctls */ 430 sc->sc_detaching = true; 431 432 if (ifp->if_flags & IFF_RUNNING) { 433 pppoe_clear_softc(sc, "destroy interface"); 434 sc->sc_eth_if = NULL; 435 } 436 PPPOE_UNLOCK(sc); 437 438 rw_enter(&pppoe_softc_list_lock, RW_WRITER); 439 LIST_REMOVE(sc, sc_list); 440 441 if (LIST_EMPTY(&pppoe_softc_list)) { 442 pfil_remove_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil); 443 } 444 rw_exit(&pppoe_softc_list_lock); 445 446 bpf_detach(ifp); 447 sppp_detach(&sc->sc_sppp.pp_if); 448 if_detach(ifp); 449 450 callout_setfunc(&sc->sc_timeout, pppoe_timeout_co_halt, sc); 451 452 workqueue_wait(sc->sc_timeout_wq, &sc->sc_timeout_wk); 453 workqueue_destroy(sc->sc_timeout_wq); 454 455 callout_halt(&sc->sc_timeout, NULL); 456 callout_destroy(&sc->sc_timeout); 457 458 #ifdef PPPOE_SERVER 459 if (sc->sc_hunique) { 460 free(sc->sc_hunique, M_DEVBUF); 461 sc->sc_hunique = NULL; 462 sc->sc_hunique_len = 0; 463 } 464 #endif 465 if (sc->sc_concentrator_name) 466 free(sc->sc_concentrator_name, M_DEVBUF); 467 if (sc->sc_service_name) 468 free(sc->sc_service_name, M_DEVBUF); 469 if (sc->sc_ac_cookie) 470 free(sc->sc_ac_cookie, M_DEVBUF); 471 if (sc->sc_relay_sid) 472 free(sc->sc_relay_sid, M_DEVBUF); 473 474 rw_destroy(&sc->sc_lock); 475 476 kmem_free(sc, sizeof(*sc)); 477 478 return 0; 479 } 480 481 static void 482 pppoe_printf(struct pppoe_softc *sc, const char *fmt, ...) 483 { 484 va_list ap; 485 bool pppoe_debug; 486 487 #ifdef PPPOE_DEBUG 488 pppoe_debug = true; 489 #else 490 pppoe_debug = false; 491 #endif 492 493 if (sc == NULL) { 494 if (!pppoe_debug) 495 return; 496 497 printf("pppoe: "); 498 } else { 499 if (!ISSET(sc->sc_sppp.pp_if.if_flags, IFF_DEBUG)) 500 return; 501 502 printf("%s: ", sc->sc_sppp.pp_if.if_xname); 503 } 504 505 va_start(ap, fmt); 506 vprintf(fmt, ap); 507 va_end(ap); 508 } 509 510 /* 511 * Find the interface handling the specified session. 512 * Note: O(number of sessions open), this is a client-side only, mean 513 * and lean implementation, so number of open sessions typically should 514 * be 1. 515 */ 516 static struct pppoe_softc * 517 pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif, krw_t lock) 518 { 519 struct pppoe_softc *sc = NULL; 520 521 if (session == 0) 522 return NULL; 523 rw_enter(&pppoe_softc_list_lock, RW_READER); 524 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 525 PPPOE_LOCK(sc, lock); 526 if ( sc->sc_state == PPPOE_STATE_SESSION 527 && sc->sc_session == session 528 && sc->sc_eth_if == rcvif) 529 break; 530 531 PPPOE_UNLOCK(sc); 532 } 533 rw_exit(&pppoe_softc_list_lock); 534 return sc; 535 } 536 537 /* Check host unique token passed and return appropriate softc pointer, 538 * or NULL if token is bogus. */ 539 static struct pppoe_softc * 540 pppoe_find_softc_by_hunique(uint8_t *token, size_t len, 541 struct ifnet *rcvif, krw_t lock) 542 { 543 struct pppoe_softc *sc; 544 uint64_t t; 545 546 CTASSERT(sizeof(t) == sizeof(sc->sc_id)); 547 548 rw_enter(&pppoe_softc_list_lock, RW_READER); 549 if (LIST_EMPTY(&pppoe_softc_list)) { 550 rw_exit(&pppoe_softc_list_lock); 551 return NULL; 552 } 553 554 if (len != sizeof(sc->sc_id)) { 555 rw_exit(&pppoe_softc_list_lock); 556 return NULL; 557 } 558 memcpy(&t, token, len); 559 560 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 561 PPPOE_LOCK(sc, lock); 562 if (sc->sc_id == t && sc->sc_eth_if != NULL) { 563 break; 564 } 565 PPPOE_UNLOCK(sc); 566 } 567 rw_exit(&pppoe_softc_list_lock); 568 569 if (sc == NULL) { 570 pppoe_printf(NULL, "alien host unique tag" 571 ", no session found\n"); 572 return NULL; 573 } 574 575 /* should be safe to access *sc now */ 576 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) { 577 pppoe_printf(sc, "host unique tag found" 578 ", but it belongs to a connection in state %d\n", 579 sc->sc_state); 580 PPPOE_UNLOCK(sc); 581 return NULL; 582 } 583 if (sc->sc_eth_if != rcvif) { 584 pppoe_printf(sc, "wrong interface, not accepting host unique\n"); 585 PPPOE_UNLOCK(sc); 586 return NULL; 587 } 588 return sc; 589 } 590 591 static void 592 pppoe_softintr_handler(void *dummy) 593 { 594 /* called at splsoftnet() */ 595 pppoeintr(); 596 } 597 598 /* called at appropriate protection level */ 599 static void 600 pppoeintr(void) 601 { 602 struct mbuf *m; 603 int i; 604 605 SOFTNET_LOCK_UNLESS_NET_MPSAFE(); 606 607 for (i = 0; i < PPPOE_DEQUEUE_MAXLEN; i++) { 608 IFQ_LOCK(&ppoediscinq); 609 IF_DEQUEUE(&ppoediscinq, m); 610 IFQ_UNLOCK(&ppoediscinq); 611 if (m == NULL) 612 break; 613 pppoe_disc_input(m); 614 } 615 616 for (i = 0; i < PPPOE_DEQUEUE_MAXLEN; i++) { 617 IFQ_LOCK(&ppoeinq); 618 IF_DEQUEUE(&ppoeinq, m); 619 IFQ_UNLOCK(&ppoeinq); 620 if (m == NULL) 621 break; 622 pppoe_data_input(m); 623 } 624 625 #if PPPOE_DEQUEUE_MAXLEN < IFQ_MAXLEN 626 if (!IF_IS_EMPTY(&ppoediscinq) || !IF_IS_EMPTY(&ppoeinq)) 627 softint_schedule(pppoe_softintr); 628 #endif 629 630 SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); 631 } 632 633 /* analyze and handle a single received packet while not in session state */ 634 static void 635 pppoe_dispatch_disc_pkt(struct mbuf *m, int off) 636 { 637 uint16_t tag, len; 638 uint16_t session, plen; 639 struct pppoe_softc *sc; 640 const char *err_msg; 641 char *error; 642 size_t dlen; 643 uint8_t *ac_cookie; 644 size_t ac_cookie_len; 645 uint8_t *relay_sid; 646 size_t relay_sid_len; 647 uint8_t *hunique; 648 size_t hunique_len; 649 struct pppoehdr *ph; 650 struct pppoetag *pt; 651 struct mbuf *n; 652 int noff, err, errortag; 653 struct ether_header *eh; 654 struct ifnet *rcvif; 655 struct psref psref; 656 657 if (m->m_len < sizeof(*eh)) { 658 m = m_pullup(m, sizeof(*eh)); 659 if (m == NULL) 660 goto done; 661 } 662 eh = mtod(m, struct ether_header *); 663 off += sizeof(*eh); 664 665 if (m->m_pkthdr.len - off < PPPOE_HEADERLEN) { 666 goto done; 667 } 668 669 M_REGION_GET(ph, struct pppoehdr *, m, off, sizeof(*ph)); 670 if (ph == NULL) { 671 goto done; 672 } 673 if (ph->vertype != PPPOE_VERTYPE) { 674 goto done; 675 } 676 677 ac_cookie = NULL; 678 ac_cookie_len = 0; 679 relay_sid = NULL; 680 relay_sid_len = 0; 681 hunique = NULL; 682 hunique_len = 0; 683 684 session = ntohs(ph->session); 685 plen = ntohs(ph->plen); 686 off += sizeof(*ph); 687 688 if (plen + off > m->m_pkthdr.len) { 689 goto done; 690 } 691 m_adj(m, off + plen - m->m_pkthdr.len); /* ignore trailing garbage */ 692 693 tag = 0; 694 len = 0; 695 sc = NULL; 696 err_msg = NULL; 697 errortag = 0; 698 while (off + sizeof(*pt) <= m->m_pkthdr.len) { 699 M_REGION_GET(pt, struct pppoetag *, m, off, sizeof(*pt)); 700 if (pt == NULL) { 701 goto done; 702 } 703 704 tag = ntohs(pt->tag); 705 len = ntohs(pt->len); 706 if (off + len + sizeof(*pt) > m->m_pkthdr.len) { 707 goto done; 708 } 709 switch (tag) { 710 case PPPOE_TAG_EOL: 711 goto breakbreak; 712 case PPPOE_TAG_SNAME: 713 break; /* ignored */ 714 case PPPOE_TAG_ACNAME: 715 if (len > 0) { 716 dlen = 4 * len + 1; 717 error = malloc(dlen, M_TEMP, M_NOWAIT); 718 if (error == NULL) 719 break; 720 721 n = m_pulldown(m, off + sizeof(*pt), len, 722 &noff); 723 if (!n) { 724 m = NULL; 725 free(error, M_TEMP); 726 goto done; 727 } 728 729 strnvisx(error, dlen, 730 mtod(n, char*) + noff, len, 731 VIS_SAFE | VIS_OCTAL); 732 pppoe_printf(NULL, "connected to %s\n", error); 733 free(error, M_TEMP); 734 } 735 break; /* ignored */ 736 case PPPOE_TAG_HUNIQUE: 737 if (hunique == NULL) { 738 n = m_pulldown(m, off + sizeof(*pt), len, 739 &noff); 740 if (!n) { 741 m = NULL; 742 err_msg = "TAG HUNIQUE ERROR"; 743 break; 744 } 745 746 hunique = mtod(n, uint8_t *) + noff; 747 hunique_len = len; 748 } 749 break; 750 case PPPOE_TAG_ACCOOKIE: 751 if (ac_cookie == NULL) { 752 n = m_pulldown(m, off + sizeof(*pt), len, 753 &noff); 754 if (!n) { 755 err_msg = "TAG ACCOOKIE ERROR"; 756 m = NULL; 757 break; 758 } 759 ac_cookie = mtod(n, char *) + noff; 760 ac_cookie_len = len; 761 } 762 break; 763 case PPPOE_TAG_RELAYSID: 764 if (relay_sid == NULL) { 765 n = m_pulldown(m, off + sizeof(*pt), len, 766 &noff); 767 if (!n) { 768 err_msg = "TAG RELAYSID ERROR"; 769 m = NULL; 770 break; 771 } 772 relay_sid = mtod(n, char *) + noff; 773 relay_sid_len = len; 774 } 775 break; 776 case PPPOE_TAG_SNAME_ERR: 777 err_msg = "SERVICE NAME ERROR"; 778 errortag = 1; 779 break; 780 case PPPOE_TAG_ACSYS_ERR: 781 err_msg = "AC SYSTEM ERROR"; 782 errortag = 1; 783 break; 784 case PPPOE_TAG_GENERIC_ERR: 785 err_msg = "GENERIC ERROR"; 786 errortag = 1; 787 break; 788 } 789 if (err_msg) { 790 error = NULL; 791 if (errortag && len) { 792 dlen = 4 * len + 1; 793 error = malloc(dlen, M_TEMP, 794 M_NOWAIT|M_ZERO); 795 n = m_pulldown(m, off + sizeof(*pt), len, 796 &noff); 797 if (!n) { 798 m = NULL; 799 } else if (error) { 800 strnvisx(error, dlen, 801 mtod(n, char*) + noff, len, 802 VIS_SAFE | VIS_OCTAL); 803 } 804 } 805 if (error) { 806 pppoe_printf(NULL, "%s: %s\n", err_msg, error); 807 free(error, M_TEMP); 808 } else 809 pppoe_printf(NULL, "%s\n", err_msg); 810 if (errortag || m == NULL) 811 goto done; 812 } 813 off += sizeof(*pt) + len; 814 } 815 breakbreak:; 816 817 switch (ph->code) { 818 case PPPOE_CODE_PADI: 819 #ifdef PPPOE_SERVER 820 /* 821 * got service name, concentrator name, and/or host unique. 822 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP. 823 */ 824 rw_enter(&pppoe_softc_list_lock, RW_READER); 825 if (LIST_EMPTY(&pppoe_softc_list)) { 826 rw_exit(&pppoe_softc_list_lock); 827 goto done; 828 } 829 830 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 831 PPPOE_LOCK(sc, RW_WRITER); 832 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) { 833 PPPOE_UNLOCK(sc); 834 continue; 835 } 836 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) { 837 PPPOE_UNLOCK(sc); 838 continue; 839 } 840 841 if (sc->sc_state == PPPOE_STATE_INITIAL) 842 break; 843 844 PPPOE_UNLOCK(sc); 845 } 846 rw_exit(&pppoe_softc_list_lock); 847 848 if (sc == NULL) { 849 goto done; 850 } 851 852 if (hunique) { 853 if (sc->sc_hunique) 854 free(sc->sc_hunique, M_DEVBUF); 855 sc->sc_hunique = malloc(hunique_len, M_DEVBUF, 856 M_DONTWAIT); 857 if (sc->sc_hunique == NULL) { 858 PPPOE_UNLOCK(sc); 859 goto done; 860 } 861 sc->sc_hunique_len = hunique_len; 862 memcpy(sc->sc_hunique, hunique, hunique_len); 863 } 864 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest); 865 sc->sc_state = PPPOE_STATE_PADO_SENT; 866 pppoe_send_pado(sc); 867 PPPOE_UNLOCK(sc); 868 break; 869 #endif /* PPPOE_SERVER */ 870 871 case PPPOE_CODE_PADR: 872 #ifdef PPPOE_SERVER 873 /* 874 * get sc from ac_cookie if IFF_PASSIVE 875 */ 876 if (ac_cookie == NULL) { 877 goto done; 878 } 879 880 rcvif = m_get_rcvif_psref(m, &psref); 881 if (__predict_true(rcvif != NULL)) { 882 sc = pppoe_find_softc_by_hunique(ac_cookie, 883 ac_cookie_len, rcvif, RW_WRITER); 884 } 885 m_put_rcvif_psref(rcvif, &psref); 886 if (sc == NULL) { 887 /* be quiet if there is not a single pppoe instance */ 888 rw_enter(&pppoe_softc_list_lock, RW_READER); 889 if (!LIST_EMPTY(&pppoe_softc_list)) { 890 pppoe_printf(NULL, "received PADR" 891 " but could not find request for it\n"); 892 } 893 rw_exit(&pppoe_softc_list_lock); 894 goto done; 895 } 896 897 if (sc->sc_state != PPPOE_STATE_PADO_SENT) { 898 pppoe_printf(sc, "received unexpected PADR\n"); 899 PPPOE_UNLOCK(sc); 900 goto done; 901 } 902 903 if (hunique) { 904 if (sc->sc_hunique) 905 free(sc->sc_hunique, M_DEVBUF); 906 sc->sc_hunique = malloc(hunique_len, M_DEVBUF, 907 M_DONTWAIT); 908 if (sc->sc_hunique == NULL) { 909 PPPOE_UNLOCK(sc); 910 goto done; 911 } 912 sc->sc_hunique_len = hunique_len; 913 memcpy(sc->sc_hunique, hunique, hunique_len); 914 } 915 pppoe_send_pads(sc); 916 sc->sc_state = PPPOE_STATE_SESSION; 917 PPPOE_UNLOCK(sc); 918 919 sc->sc_sppp.pp_up(&sc->sc_sppp); 920 break; 921 #else 922 /* ignore, we are no access concentrator */ 923 goto done; 924 #endif /* PPPOE_SERVER */ 925 926 case PPPOE_CODE_PADO: 927 rcvif = m_get_rcvif_psref(m, &psref); 928 if (__predict_false(rcvif == NULL)) 929 goto done; 930 931 if (hunique != NULL) { 932 sc = pppoe_find_softc_by_hunique(hunique, 933 hunique_len, rcvif, RW_WRITER); 934 } 935 936 m_put_rcvif_psref(rcvif, &psref); 937 938 if (sc == NULL) { 939 /* be quiet if there is not a single pppoe instance */ 940 rw_enter(&pppoe_softc_list_lock, RW_READER); 941 if (!LIST_EMPTY(&pppoe_softc_list)) { 942 pppoe_printf(NULL, "received PADO" 943 " but could not find request for it\n"); 944 } 945 rw_exit(&pppoe_softc_list_lock); 946 goto done; 947 } 948 949 if (sc->sc_state != PPPOE_STATE_PADI_SENT) { 950 pppoe_printf(sc, "received unexpected PADO\n"); 951 PPPOE_UNLOCK(sc); 952 goto done; 953 } 954 955 if (ac_cookie) { 956 if (sc->sc_ac_cookie) 957 free(sc->sc_ac_cookie, M_DEVBUF); 958 sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF, 959 M_DONTWAIT); 960 if (sc->sc_ac_cookie == NULL) { 961 pppoe_printf(sc, "FATAL: could not allocate memory " 962 "for AC cookie\n"); 963 sc->sc_ac_cookie_len = 0; 964 PPPOE_UNLOCK(sc); 965 goto done; 966 } 967 sc->sc_ac_cookie_len = ac_cookie_len; 968 memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len); 969 } else if (sc->sc_ac_cookie) { 970 free(sc->sc_ac_cookie, M_DEVBUF); 971 sc->sc_ac_cookie = NULL; 972 sc->sc_ac_cookie_len = 0; 973 } 974 if (relay_sid) { 975 if (sc->sc_relay_sid) 976 free(sc->sc_relay_sid, M_DEVBUF); 977 sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF, 978 M_DONTWAIT); 979 if (sc->sc_relay_sid == NULL) { 980 pppoe_printf(sc, "FATAL: could not allocate memory " 981 "for relay SID\n"); 982 sc->sc_relay_sid_len = 0; 983 PPPOE_UNLOCK(sc); 984 goto done; 985 } 986 sc->sc_relay_sid_len = relay_sid_len; 987 memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len); 988 } else if (sc->sc_relay_sid) { 989 free(sc->sc_relay_sid, M_DEVBUF); 990 sc->sc_relay_sid = NULL; 991 sc->sc_relay_sid_len = 0; 992 } 993 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest); 994 callout_stop(&sc->sc_timeout); 995 sc->sc_padr_retried = 0; 996 sc->sc_state = PPPOE_STATE_PADR_SENT; 997 if ((err = pppoe_send_padr(sc)) != 0) { 998 if (err == ENOBUFS) { 999 pppoe_printf(sc, "PADO is too large, drop it\n"); 1000 /* 1001 * Cannot send PADR generated by received PADO, 1002 * so restart from sending PAD*I*. 1003 */ 1004 sc->sc_state = PPPOE_STATE_PADI_SENT; 1005 callout_schedule(&sc->sc_timeout, 1006 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried)); 1007 PPPOE_UNLOCK(sc); 1008 goto done; 1009 } 1010 1011 pppoe_printf(sc, 1012 "failed to send PADR, error=%d\n", err); 1013 } 1014 callout_schedule(&sc->sc_timeout, 1015 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried)); 1016 1017 PPPOE_UNLOCK(sc); 1018 break; 1019 1020 case PPPOE_CODE_PADS: 1021 rcvif = m_get_rcvif_psref(m, &psref); 1022 if (__predict_false(rcvif == NULL)) 1023 goto done; 1024 1025 if (hunique != NULL) { 1026 sc = pppoe_find_softc_by_hunique(hunique, 1027 hunique_len, rcvif, RW_WRITER); 1028 } 1029 1030 m_put_rcvif_psref(rcvif, &psref); 1031 1032 if (sc == NULL) 1033 goto done; 1034 1035 if (memcmp(&sc->sc_dest, eh->ether_shost, 1036 sizeof sc->sc_dest) != 0) { 1037 PPPOE_UNLOCK(sc); 1038 goto done; 1039 } 1040 1041 sc->sc_session = session; 1042 callout_stop(&sc->sc_timeout); 1043 pppoe_printf(sc, "session 0x%x connected\n", session); 1044 sc->sc_state = PPPOE_STATE_SESSION; 1045 PPPOE_UNLOCK(sc); 1046 1047 sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */ 1048 break; 1049 1050 case PPPOE_CODE_PADT: 1051 rcvif = m_get_rcvif_psref(m, &psref); 1052 if (__predict_false(rcvif == NULL)) 1053 goto done; 1054 1055 sc = pppoe_find_softc_by_session(session, rcvif, 1056 RW_WRITER); 1057 1058 m_put_rcvif_psref(rcvif, &psref); 1059 1060 if (sc == NULL) 1061 goto done; 1062 1063 if (memcmp(&sc->sc_dest, eh->ether_shost, 1064 sizeof sc->sc_dest) != 0) { 1065 PPPOE_UNLOCK(sc); 1066 goto done; 1067 } 1068 1069 pppoe_clear_softc(sc, "received PADT"); 1070 if (sppp_is_connecting(&sc->sc_sppp.pp_if)) { 1071 pppoe_printf(sc, "wait for reconnect\n"); 1072 callout_schedule(&sc->sc_timeout, 1073 PPPOE_RECON_PADTRCVD); 1074 } 1075 PPPOE_UNLOCK(sc); 1076 break; 1077 1078 default: 1079 rcvif = m_get_rcvif_psref(m, &psref); 1080 if (__predict_false(rcvif == NULL)) 1081 goto done; 1082 1083 if (hunique != NULL) { 1084 sc = pppoe_find_softc_by_hunique(hunique, 1085 hunique_len, rcvif, RW_READER); 1086 } 1087 1088 m_put_rcvif_psref(rcvif, &psref); 1089 1090 pppoe_printf(sc, "unknown code (0x%04x) session = 0x%04x\n", 1091 ph->code, session); 1092 if (sc == NULL) 1093 goto done; 1094 PPPOE_UNLOCK(sc); 1095 break; 1096 } 1097 1098 done: 1099 m_freem(m); 1100 return; 1101 } 1102 1103 static void 1104 pppoe_disc_input(struct mbuf *m) 1105 { 1106 KASSERT(m->m_flags & M_PKTHDR); 1107 1108 /* 1109 * Avoid error messages if there is not a single PPPoE instance. 1110 */ 1111 rw_enter(&pppoe_softc_list_lock, RW_READER); 1112 if (!LIST_EMPTY(&pppoe_softc_list)) { 1113 rw_exit(&pppoe_softc_list_lock); 1114 pppoe_dispatch_disc_pkt(m, 0); 1115 } else { 1116 rw_exit(&pppoe_softc_list_lock); 1117 m_freem(m); 1118 } 1119 } 1120 1121 static bool 1122 pppoe_is_my_frame(uint8_t *dhost, struct ifnet *rcvif) 1123 { 1124 1125 if (memcmp(CLLADDR(rcvif->if_sadl), dhost, ETHER_ADDR_LEN) == 0) 1126 return true; 1127 1128 return false; 1129 } 1130 1131 static void 1132 pppoe_data_input(struct mbuf *m) 1133 { 1134 uint16_t session, plen; 1135 struct pppoe_softc *sc; 1136 struct pppoehdr *ph; 1137 struct ifnet *rcvif; 1138 struct psref psref; 1139 uint8_t shost[ETHER_ADDR_LEN]; 1140 uint8_t dhost[ETHER_ADDR_LEN]; 1141 bool term_unknown = pppoe_term_unknown; 1142 1143 KASSERT(m->m_flags & M_PKTHDR); 1144 1145 /* 1146 * Avoid error messages if there is not a single PPPoE instance. 1147 */ 1148 rw_enter(&pppoe_softc_list_lock, RW_READER); 1149 if (LIST_EMPTY(&pppoe_softc_list)) { 1150 rw_exit(&pppoe_softc_list_lock); 1151 goto drop; 1152 } 1153 rw_exit(&pppoe_softc_list_lock); 1154 1155 if (term_unknown) { 1156 memcpy(shost, mtod(m, struct ether_header*)->ether_shost, 1157 ETHER_ADDR_LEN); 1158 memcpy(dhost, mtod(m, struct ether_header*)->ether_dhost, 1159 ETHER_ADDR_LEN); 1160 } 1161 m_adj(m, sizeof(struct ether_header)); 1162 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) { 1163 goto drop; 1164 } 1165 1166 if (m->m_len < sizeof(*ph)) { 1167 m = m_pullup(m, sizeof(*ph)); 1168 if (m == NULL) { 1169 return; 1170 } 1171 } 1172 ph = mtod(m, struct pppoehdr *); 1173 1174 if (ph->vertype != PPPOE_VERTYPE) { 1175 goto drop; 1176 } 1177 if (ph->code != 0) { 1178 goto drop; 1179 } 1180 1181 session = ntohs(ph->session); 1182 rcvif = m_get_rcvif_psref(m, &psref); 1183 if (__predict_false(rcvif == NULL)) 1184 goto drop; 1185 sc = pppoe_find_softc_by_session(session, rcvif, RW_READER); 1186 if (sc == NULL) { 1187 if (term_unknown) { 1188 static struct timeval lasttime = {0, 0}; 1189 static int curpps = 0; 1190 /* 1191 * avoid to send wrong PADT which is response from 1192 * session stage packets for other hosts when parent 1193 * ethernet is promiscuous mode. 1194 */ 1195 if (pppoe_is_my_frame(dhost, rcvif) && 1196 ppsratecheck(&lasttime, &curpps, 1197 pppoe_term_unknown_pps)) { 1198 pppoe_printf(NULL, "input for unknown session %#x, " 1199 "sending PADT\n", session); 1200 pppoe_send_padt(rcvif, session, shost); 1201 } 1202 } 1203 m_put_rcvif_psref(rcvif, &psref); 1204 goto drop; 1205 } 1206 1207 m_put_rcvif_psref(rcvif, &psref); 1208 1209 plen = ntohs(ph->plen); 1210 1211 bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_IN); 1212 1213 m_adj(m, PPPOE_HEADERLEN); 1214 1215 #ifdef PPPOE_DEBUG 1216 { 1217 struct mbuf *p; 1218 1219 printf("%s: pkthdr.len=%d, pppoe.len=%d", 1220 sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen); 1221 p = m; 1222 while (p) { 1223 printf(" l=%d", p->m_len); 1224 p = p->m_next; 1225 } 1226 printf("\n"); 1227 } 1228 #endif 1229 PPPOE_UNLOCK(sc); 1230 1231 if (m->m_pkthdr.len < plen) 1232 goto drop; 1233 1234 /* ignore trailing garbage */ 1235 m_adj(m, plen - m->m_pkthdr.len); 1236 /* 1237 * Fix incoming interface pointer (not the raw ethernet interface 1238 * anymore) 1239 */ 1240 m_set_rcvif(m, &sc->sc_sppp.pp_if); 1241 1242 /* pass packet up and account for it */ 1243 if_statinc(&sc->sc_sppp.pp_if, if_ipackets); 1244 sppp_input(&sc->sc_sppp.pp_if, m); 1245 return; 1246 1247 drop: 1248 m_freem(m); 1249 } 1250 1251 static int 1252 pppoe_output(struct pppoe_softc *sc, struct mbuf *m) 1253 { 1254 struct sockaddr dst; 1255 struct ether_header *eh; 1256 uint16_t etype; 1257 1258 if (sc->sc_eth_if == NULL) { 1259 m_freem(m); 1260 return EIO; 1261 } 1262 1263 memset(&dst, 0, sizeof dst); 1264 dst.sa_family = AF_UNSPEC; 1265 eh = (struct ether_header*)&dst.sa_data; 1266 etype = sc->sc_state == PPPOE_STATE_SESSION 1267 ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC; 1268 eh->ether_type = htons(etype); 1269 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest); 1270 1271 DPRINTF(sc, "(%x) state=%d, session=0x%x output -> %s, len=%d\n", 1272 etype, sc->sc_state, sc->sc_session, 1273 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len); 1274 1275 m->m_flags &= ~(M_BCAST|M_MCAST); 1276 if_statinc(&sc->sc_sppp.pp_if, if_opackets); 1277 return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL); 1278 } 1279 1280 static int 1281 pppoe_parm_cpyinstr(struct pppoe_softc *sc, 1282 char **dst, const void *src, size_t len) 1283 { 1284 int error = 0; 1285 char *next = NULL; 1286 size_t bufsiz, cpysiz, strsiz; 1287 1288 bufsiz = len + 1; 1289 1290 if (src == NULL) 1291 goto out; 1292 1293 bufsiz = len + 1; 1294 next = malloc(bufsiz, M_DEVBUF, M_WAITOK); 1295 if (next == NULL) 1296 return ENOMEM; 1297 1298 error = copyinstr(src, next, bufsiz, &cpysiz); 1299 if (error != 0) 1300 goto fail; 1301 if (cpysiz != bufsiz) { 1302 error = EINVAL; 1303 goto fail; 1304 } 1305 1306 strsiz = strnlen(next, bufsiz); 1307 if (strsiz == bufsiz) { 1308 error = EINVAL; 1309 goto fail; 1310 } 1311 1312 out: 1313 PPPOE_LOCK(sc, RW_WRITER); 1314 if (*dst != NULL) 1315 free(*dst, M_DEVBUF); 1316 *dst = next; 1317 next = NULL; 1318 PPPOE_UNLOCK(sc); 1319 fail: 1320 if (next != NULL) 1321 free(next, M_DEVBUF); 1322 1323 return error; 1324 } 1325 1326 static int 1327 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data) 1328 { 1329 struct lwp *l = curlwp; /* XXX */ 1330 struct pppoe_softc *sc = (struct pppoe_softc*)ifp; 1331 struct ifreq *ifr = data; 1332 int error = 0; 1333 1334 switch (cmd) { 1335 case PPPOESETPARMS: 1336 { 1337 struct pppoediscparms *parms = (struct pppoediscparms*)data; 1338 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 1339 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1340 NULL) != 0) 1341 return EPERM; 1342 if (parms->eth_ifname[0] != 0) { 1343 struct ifnet *eth_if; 1344 1345 PPPOE_LOCK(sc, RW_WRITER); 1346 if (sc->sc_detaching) { 1347 PPPOE_UNLOCK(sc); 1348 return ENXIO; 1349 } 1350 eth_if = ifunit(parms->eth_ifname); 1351 if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) { 1352 sc->sc_eth_if = NULL; 1353 PPPOE_UNLOCK(sc); 1354 return ENXIO; 1355 } 1356 1357 if (sc->sc_sppp.pp_if.if_mtu != 1358 eth_if->if_mtu - PPPOE_OVERHEAD) { 1359 sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu - 1360 PPPOE_OVERHEAD; 1361 } 1362 sc->sc_eth_if = eth_if; 1363 PPPOE_UNLOCK(sc); 1364 } 1365 1366 error = pppoe_parm_cpyinstr(sc, &sc->sc_concentrator_name, 1367 parms->ac_name, parms->ac_name_len); 1368 if (error != 0) 1369 return error; 1370 1371 error = pppoe_parm_cpyinstr(sc, &sc->sc_service_name, 1372 parms->service_name, parms->service_name_len); 1373 if (error != 0) 1374 return error; 1375 return 0; 1376 } 1377 break; 1378 case PPPOEGETPARMS: 1379 { 1380 struct pppoediscparms *parms = (struct pppoediscparms*)data; 1381 memset(parms, 0, sizeof *parms); 1382 PPPOE_LOCK(sc, RW_READER); 1383 if (sc->sc_eth_if) 1384 strlcpy(parms->ifname, sc->sc_eth_if->if_xname, 1385 sizeof(parms->ifname)); 1386 PPPOE_UNLOCK(sc); 1387 return 0; 1388 } 1389 break; 1390 case PPPOEGETSESSION: 1391 { 1392 struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data; 1393 PPPOE_LOCK(sc, RW_READER); 1394 state->state = sc->sc_state; 1395 state->session_id = sc->sc_session; 1396 state->padi_retry_no = sc->sc_padi_retried; 1397 state->padr_retry_no = sc->sc_padr_retried; 1398 PPPOE_UNLOCK(sc); 1399 return 0; 1400 } 1401 break; 1402 case SIOCSIFFLAGS: 1403 /* 1404 * Prevent running re-establishment timers overriding 1405 * administrators choice. 1406 */ 1407 PPPOE_LOCK(sc, RW_WRITER); 1408 if (sc->sc_detaching) { 1409 PPPOE_UNLOCK(sc); 1410 return ENXIO; 1411 } 1412 1413 if ((ifr->ifr_flags & IFF_UP) == 0 1414 && sc->sc_state < PPPOE_STATE_SESSION) { 1415 callout_stop(&sc->sc_timeout); 1416 sc->sc_state = PPPOE_STATE_INITIAL; 1417 sc->sc_padi_retried = 0; 1418 sc->sc_padr_retried = 0; 1419 memcpy(&sc->sc_dest, etherbroadcastaddr, 1420 sizeof(sc->sc_dest)); 1421 } 1422 1423 PPPOE_UNLOCK(sc); 1424 1425 error = sppp_ioctl(ifp, cmd, data); 1426 1427 return error; 1428 case SIOCSIFMTU: 1429 if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ? 1430 PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) { 1431 return EINVAL; 1432 } 1433 /*FALLTHROUGH*/ 1434 default: 1435 return sppp_ioctl(ifp, cmd, data); 1436 } 1437 return 0; 1438 } 1439 1440 /* 1441 * Allocate a mbuf/cluster with space to store the given data length 1442 * of payload, leaving space for prepending an ethernet header 1443 * in front. 1444 */ 1445 static struct mbuf * 1446 pppoe_get_mbuf(size_t len) 1447 { 1448 struct mbuf *m; 1449 1450 if (len + sizeof(struct ether_header) > MCLBYTES) 1451 return NULL; 1452 1453 MGETHDR(m, M_DONTWAIT, MT_DATA); 1454 if (m == NULL) 1455 return NULL; 1456 if (len + sizeof(struct ether_header) > MHLEN) { 1457 MCLGET(m, M_DONTWAIT); 1458 if ((m->m_flags & M_EXT) == 0) { 1459 m_free(m); 1460 return NULL; 1461 } 1462 } 1463 m->m_data += sizeof(struct ether_header); 1464 m->m_len = len; 1465 m->m_pkthdr.len = len; 1466 m_reset_rcvif(m); 1467 1468 return m; 1469 } 1470 1471 static int 1472 pppoe_send_padi(struct pppoe_softc *sc) 1473 { 1474 struct mbuf *m0; 1475 int len, l1 = 0, l2 = 0; 1476 uint8_t *p; 1477 1478 if (sc->sc_state > PPPOE_STATE_PADI_SENT) 1479 panic("pppoe_send_padi in state %d", sc->sc_state); 1480 1481 /* Compute packet length. */ 1482 len = sizeof(struct pppoetag); 1483 if (sc->sc_service_name != NULL) { 1484 l1 = strlen(sc->sc_service_name); 1485 len += l1; 1486 } 1487 if (sc->sc_concentrator_name != NULL) { 1488 l2 = strlen(sc->sc_concentrator_name); 1489 len += sizeof(struct pppoetag) + l2; 1490 } 1491 len += sizeof(struct pppoetag) + sizeof(sc->sc_id); 1492 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) { 1493 len += sizeof(struct pppoetag) + 2; 1494 } 1495 1496 /* Allocate packet. */ 1497 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1498 if (m0 == NULL) 1499 return ENOBUFS; 1500 1501 /* Fill in packet. */ 1502 p = mtod(m0, uint8_t *); 1503 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len); 1504 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1505 if (sc->sc_service_name != NULL) { 1506 PPPOE_ADD_16(p, l1); 1507 memcpy(p, sc->sc_service_name, l1); 1508 p += l1; 1509 } else { 1510 PPPOE_ADD_16(p, 0); 1511 } 1512 if (sc->sc_concentrator_name != NULL) { 1513 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME); 1514 PPPOE_ADD_16(p, l2); 1515 memcpy(p, sc->sc_concentrator_name, l2); 1516 p += l2; 1517 } 1518 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1519 PPPOE_ADD_16(p, sizeof(sc->sc_id)); 1520 memcpy(p, &sc->sc_id, sizeof(sc->sc_id)); 1521 p += sizeof(sc->sc_id); 1522 1523 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) { 1524 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD); 1525 PPPOE_ADD_16(p, 2); 1526 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu); 1527 } 1528 1529 #ifdef PPPOE_DEBUG 1530 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN) 1531 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld", 1532 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *))); 1533 #endif 1534 1535 /* Send packet. */ 1536 return pppoe_output(sc, m0); 1537 } 1538 1539 static void 1540 pppoe_timeout_co(void *arg) 1541 { 1542 struct pppoe_softc *sc = (struct pppoe_softc *)arg; 1543 1544 if (atomic_swap_uint(&sc->sc_timeout_scheduled, 1) != 0) 1545 return; 1546 1547 workqueue_enqueue(sc->sc_timeout_wq, &sc->sc_timeout_wk, NULL); 1548 } 1549 1550 static void 1551 pppoe_timeout_co_halt(void *unused __unused) 1552 { 1553 1554 /* do nothing to halt callout safely */ 1555 } 1556 1557 static void 1558 pppoe_timeout_wk(struct work *wk __unused, void *arg) 1559 { 1560 struct pppoe_softc *sc = (struct pppoe_softc *)arg; 1561 1562 atomic_swap_uint(&sc->sc_timeout_scheduled, 0); 1563 pppoe_timeout(sc); 1564 } 1565 1566 static void 1567 pppoe_timeout(struct pppoe_softc *sc) 1568 { 1569 int retry_wait, err; 1570 DECLARE_SPLNET_VARIABLE; 1571 1572 pppoe_printf(sc, "timeout\n"); 1573 1574 PPPOE_LOCK(sc, RW_WRITER); 1575 switch (sc->sc_state) { 1576 case PPPOE_STATE_INITIAL: 1577 /* delayed connect from pppoe_tls() */ 1578 if (!sc->sc_detaching) 1579 pppoe_connect(sc); 1580 break; 1581 case PPPOE_STATE_PADI_SENT: 1582 /* 1583 * We have two basic ways of retrying: 1584 * - Quick retry mode: try a few times in short sequence 1585 * - Slow retry mode: we already had a connection successfully 1586 * established and will try infinitely (without user 1587 * intervention) 1588 * We only enter slow retry mode if on-demand is disabled. 1589 */ 1590 1591 /* initialize for quick retry mode */ 1592 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried); 1593 1594 ACQUIRE_SPLNET(); 1595 sc->sc_padi_retried++; 1596 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) { 1597 if (!sppp_ondemand_enabled(&sc->sc_sppp.pp_if)) { 1598 /* slow retry mode */ 1599 retry_wait = PPPOE_SLOW_RETRY; 1600 } else { 1601 pppoe_abort_connect(sc); 1602 RELEASE_SPLNET(); 1603 PPPOE_UNLOCK(sc); 1604 return; 1605 } 1606 } 1607 if ((err = pppoe_send_padi(sc)) != 0) { 1608 sc->sc_padi_retried--; 1609 pppoe_printf(sc, 1610 "failed to transmit PADI, error=%d\n", err); 1611 } 1612 callout_schedule(&sc->sc_timeout,retry_wait); 1613 RELEASE_SPLNET(); 1614 break; 1615 1616 case PPPOE_STATE_PADR_SENT: 1617 ACQUIRE_SPLNET(); 1618 sc->sc_padr_retried++; 1619 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) { 1620 memcpy(&sc->sc_dest, etherbroadcastaddr, 1621 sizeof(sc->sc_dest)); 1622 sc->sc_state = PPPOE_STATE_PADI_SENT; 1623 sc->sc_padi_retried = 0; 1624 sc->sc_padr_retried = 0; 1625 if ((err = pppoe_send_padi(sc)) != 0) { 1626 pppoe_printf(sc, 1627 "failed to send PADI, error=%d\n", err); 1628 } 1629 callout_schedule(&sc->sc_timeout, 1630 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried)); 1631 RELEASE_SPLNET(); 1632 PPPOE_UNLOCK(sc); 1633 return; 1634 } 1635 if ((err = pppoe_send_padr(sc)) != 0) { 1636 pppoe_printf(sc,"failed to send PADR, error=%d", err); 1637 } 1638 callout_schedule(&sc->sc_timeout, 1639 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried)); 1640 RELEASE_SPLNET(); 1641 break; 1642 case PPPOE_STATE_CLOSING: 1643 pppoe_disconnect(sc); 1644 break; 1645 default: 1646 PPPOE_UNLOCK(sc); 1647 return; /* all done, work in peace */ 1648 } 1649 PPPOE_UNLOCK(sc); 1650 } 1651 1652 /* Start a connection (i.e. initiate discovery phase) */ 1653 static int 1654 pppoe_connect(struct pppoe_softc *sc) 1655 { 1656 int err; 1657 DECLARE_SPLNET_VARIABLE; 1658 1659 KASSERT(PPPOE_WLOCKED(sc)); 1660 1661 if (sc->sc_state != PPPOE_STATE_INITIAL) 1662 return EBUSY; 1663 1664 #ifdef PPPOE_SERVER 1665 /* wait PADI if IFF_PASSIVE */ 1666 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) 1667 return 0; 1668 #endif 1669 ACQUIRE_SPLNET(); 1670 /* save state, in case we fail to send PADI */ 1671 sc->sc_state = PPPOE_STATE_PADI_SENT; 1672 sc->sc_padi_retried = 0; 1673 sc->sc_padr_retried = 0; 1674 err = pppoe_send_padi(sc); 1675 if (err != 0) 1676 pppoe_printf(sc, "failed to send PADI, error=%d\n", err); 1677 callout_schedule(&sc->sc_timeout, PPPOE_DISC_TIMEOUT); 1678 RELEASE_SPLNET(); 1679 return err; 1680 } 1681 1682 /* disconnect */ 1683 static int 1684 pppoe_disconnect(struct pppoe_softc *sc) 1685 { 1686 int err; 1687 DECLARE_SPLNET_VARIABLE; 1688 1689 KASSERT(PPPOE_WLOCKED(sc)); 1690 1691 ACQUIRE_SPLNET(); 1692 1693 if (sc->sc_state < PPPOE_STATE_SESSION) 1694 err = EBUSY; 1695 else { 1696 pppoe_printf(sc, "disconnecting\n"); 1697 err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session, 1698 (const uint8_t *)&sc->sc_dest); 1699 } 1700 1701 /* cleanup softc */ 1702 sc->sc_state = PPPOE_STATE_INITIAL; 1703 1704 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 1705 if (sc->sc_ac_cookie) { 1706 free(sc->sc_ac_cookie, M_DEVBUF); 1707 sc->sc_ac_cookie = NULL; 1708 } 1709 sc->sc_ac_cookie_len = 0; 1710 if (sc->sc_relay_sid) { 1711 free(sc->sc_relay_sid, M_DEVBUF); 1712 sc->sc_relay_sid = NULL; 1713 } 1714 sc->sc_relay_sid_len = 0; 1715 #ifdef PPPOE_SERVER 1716 if (sc->sc_hunique) { 1717 free(sc->sc_hunique, M_DEVBUF); 1718 sc->sc_hunique = NULL; 1719 } 1720 sc->sc_hunique_len = 0; 1721 #endif 1722 sc->sc_session = 0; 1723 1724 1725 /* notify upper layer */ 1726 PPPOE_UNLOCK(sc); 1727 sc->sc_sppp.pp_down(&sc->sc_sppp); 1728 PPPOE_LOCK(sc, RW_WRITER); 1729 1730 RELEASE_SPLNET(); 1731 return err; 1732 } 1733 1734 /* Connection attempt aborted */ 1735 static void 1736 pppoe_abort_connect(struct pppoe_softc *sc) 1737 { 1738 KASSERT(PPPOE_WLOCKED(sc)); 1739 1740 pppoe_printf(sc, "could not establish connection\n"); 1741 sc->sc_state = PPPOE_STATE_CLOSING; 1742 1743 /* clear connection state */ 1744 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 1745 sc->sc_state = PPPOE_STATE_INITIAL; 1746 1747 /* notify upper layer */ 1748 PPPOE_UNLOCK(sc); 1749 sppp_abort_connect(&sc->sc_sppp.pp_if); 1750 PPPOE_LOCK(sc, RW_WRITER); 1751 } 1752 1753 static int 1754 pppoe_send_padr(struct pppoe_softc *sc) 1755 { 1756 struct mbuf *m0; 1757 uint8_t *p; 1758 size_t len, l1 = 0; 1759 1760 if (sc->sc_state != PPPOE_STATE_PADR_SENT) 1761 return EIO; 1762 1763 /* Compute packet length. */ 1764 len = sizeof(struct pppoetag); 1765 if (sc->sc_service_name != NULL) { 1766 l1 = strlen(sc->sc_service_name); 1767 len += l1; 1768 } 1769 if (sc->sc_ac_cookie_len > 0) { 1770 len += sizeof(struct pppoetag) + sc->sc_ac_cookie_len; 1771 } 1772 if (sc->sc_relay_sid_len > 0) { 1773 len += sizeof(struct pppoetag) + sc->sc_relay_sid_len; 1774 } 1775 len += sizeof(struct pppoetag) + sizeof(sc->sc_id); 1776 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) { 1777 len += sizeof(struct pppoetag) + 2; 1778 } 1779 1780 /* Allocate packet. */ 1781 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1782 if (m0 == NULL) 1783 return ENOBUFS; 1784 1785 /* Fill in packet. */ 1786 p = mtod(m0, uint8_t *); 1787 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len); 1788 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1789 if (sc->sc_service_name != NULL) { 1790 PPPOE_ADD_16(p, l1); 1791 memcpy(p, sc->sc_service_name, l1); 1792 p += l1; 1793 } else { 1794 PPPOE_ADD_16(p, 0); 1795 } 1796 if (sc->sc_ac_cookie_len > 0) { 1797 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE); 1798 PPPOE_ADD_16(p, sc->sc_ac_cookie_len); 1799 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len); 1800 p += sc->sc_ac_cookie_len; 1801 } 1802 if (sc->sc_relay_sid_len > 0) { 1803 PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID); 1804 PPPOE_ADD_16(p, sc->sc_relay_sid_len); 1805 memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len); 1806 p += sc->sc_relay_sid_len; 1807 } 1808 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1809 PPPOE_ADD_16(p, sizeof(sc->sc_id)); 1810 memcpy(p, &sc->sc_id, sizeof(sc->sc_id)); 1811 p += sizeof(sc->sc_id); 1812 1813 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) { 1814 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD); 1815 PPPOE_ADD_16(p, 2); 1816 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu); 1817 } 1818 1819 #ifdef PPPOE_DEBUG 1820 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN) 1821 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld", 1822 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *))); 1823 #endif 1824 1825 /* Send packet. */ 1826 return pppoe_output(sc, m0); 1827 } 1828 1829 /* send a PADT packet */ 1830 static int 1831 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest) 1832 { 1833 struct ether_header *eh; 1834 struct sockaddr dst; 1835 struct mbuf *m0; 1836 uint8_t *p; 1837 1838 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN); 1839 if (!m0) 1840 return EIO; 1841 p = mtod(m0, uint8_t *); 1842 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0); 1843 1844 memset(&dst, 0, sizeof dst); 1845 dst.sa_family = AF_UNSPEC; 1846 eh = (struct ether_header*)&dst.sa_data; 1847 eh->ether_type = htons(ETHERTYPE_PPPOEDISC); 1848 memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN); 1849 1850 m0->m_flags &= ~(M_BCAST|M_MCAST); 1851 return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL); 1852 } 1853 1854 #ifdef PPPOE_SERVER 1855 static int 1856 pppoe_send_pado(struct pppoe_softc *sc) 1857 { 1858 struct mbuf *m0; 1859 uint8_t *p; 1860 size_t len; 1861 1862 if (sc->sc_state != PPPOE_STATE_PADO_SENT) 1863 return EIO; 1864 1865 /* Include AC cookie. */ 1866 len = sizeof(struct pppoetag) + sizeof(sc->sc_id); 1867 /* Include hunique. */ 1868 len += sizeof(struct pppoetag) + sc->sc_hunique_len; 1869 1870 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1871 if (!m0) 1872 return EIO; 1873 p = mtod(m0, uint8_t *); 1874 1875 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len); 1876 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE); 1877 PPPOE_ADD_16(p, sizeof(sc->sc_id)); 1878 memcpy(p, &sc->sc_id, sizeof(sc->sc_id)); 1879 p += sizeof(sc->sc_id); 1880 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1881 PPPOE_ADD_16(p, sc->sc_hunique_len); 1882 memcpy(p, sc->sc_hunique, sc->sc_hunique_len); 1883 return pppoe_output(sc, m0); 1884 } 1885 1886 static int 1887 pppoe_send_pads(struct pppoe_softc *sc) 1888 { 1889 struct bintime bt; 1890 struct mbuf *m0; 1891 uint8_t *p; 1892 size_t len, l1 = 0; /* XXX: gcc */ 1893 1894 KASSERT(PPPOE_WLOCKED(sc)); 1895 1896 if (sc->sc_state != PPPOE_STATE_PADO_SENT) 1897 return EIO; 1898 1899 getbinuptime(&bt); 1900 sc->sc_session = bt.sec % 0xff + 1; 1901 1902 /* Include service name. */ 1903 len = sizeof(struct pppoetag); 1904 if (sc->sc_service_name != NULL) { 1905 l1 = strlen(sc->sc_service_name); 1906 len += l1; 1907 } 1908 /* Include hunique. */ 1909 len += sizeof(struct pppoetag) + sc->sc_hunique_len; 1910 1911 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1912 if (!m0) 1913 return ENOBUFS; 1914 p = mtod(m0, uint8_t *); 1915 1916 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len); 1917 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1918 if (sc->sc_service_name != NULL) { 1919 PPPOE_ADD_16(p, l1); 1920 memcpy(p, sc->sc_service_name, l1); 1921 p += l1; 1922 } else { 1923 PPPOE_ADD_16(p, 0); 1924 } 1925 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1926 PPPOE_ADD_16(p, sc->sc_hunique_len); 1927 memcpy(p, sc->sc_hunique, sc->sc_hunique_len); 1928 return pppoe_output(sc, m0); 1929 } 1930 #endif 1931 1932 static void 1933 pppoe_tls(struct sppp *sp) 1934 { 1935 struct pppoe_softc *sc = (void *)sp; 1936 int wtime; 1937 1938 PPPOE_LOCK(sc, RW_READER); 1939 1940 if (sc->sc_state != PPPOE_STATE_INITIAL) { 1941 PPPOE_UNLOCK(sc); 1942 return; 1943 } 1944 1945 if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH && 1946 sc->sc_sppp.pp_auth_failures > 0) { 1947 /* 1948 * Delay trying to reconnect a bit more - the peer 1949 * might have failed to contact its radius server. 1950 */ 1951 wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures; 1952 if (wtime > PPPOE_SLOW_RETRY) 1953 wtime = PPPOE_SLOW_RETRY; 1954 } else { 1955 wtime = PPPOE_RECON_IMMEDIATE; 1956 } 1957 callout_schedule(&sc->sc_timeout, wtime); 1958 1959 PPPOE_UNLOCK(sc); 1960 } 1961 1962 static void 1963 pppoe_tlf(struct sppp *sp) 1964 { 1965 struct pppoe_softc *sc = (void *)sp; 1966 1967 PPPOE_LOCK(sc, RW_WRITER); 1968 1969 if (sc->sc_state < PPPOE_STATE_SESSION) { 1970 callout_stop(&sc->sc_timeout); 1971 sc->sc_state = PPPOE_STATE_INITIAL; 1972 sc->sc_padi_retried = 0; 1973 sc->sc_padr_retried = 0; 1974 memcpy(&sc->sc_dest, etherbroadcastaddr, 1975 sizeof(sc->sc_dest)); 1976 PPPOE_UNLOCK(sc); 1977 return; 1978 } 1979 1980 /* 1981 * Do not call pppoe_disconnect here, the upper layer state 1982 * machine gets confused by this. We must return from this 1983 * function and defer disconnecting to the timeout handler. 1984 */ 1985 sc->sc_state = PPPOE_STATE_CLOSING; 1986 1987 callout_schedule(&sc->sc_timeout, hz/50); 1988 1989 PPPOE_UNLOCK(sc); 1990 } 1991 1992 static void 1993 pppoe_start(struct ifnet *ifp) 1994 { 1995 struct pppoe_softc *sc = (void *)ifp; 1996 struct mbuf *m; 1997 uint8_t *p; 1998 size_t len; 1999 2000 if (sppp_isempty(ifp)) 2001 return; 2002 2003 /* are we ready to process data yet? */ 2004 PPPOE_LOCK(sc, RW_READER); 2005 if (sc->sc_state < PPPOE_STATE_SESSION) { 2006 sppp_flush(&sc->sc_sppp.pp_if); 2007 PPPOE_UNLOCK(sc); 2008 return; 2009 } 2010 2011 while ((m = sppp_dequeue(ifp)) != NULL) { 2012 len = m->m_pkthdr.len; 2013 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT); 2014 if (m == NULL) { 2015 if_statinc(ifp, if_oerrors); 2016 continue; 2017 } 2018 p = mtod(m, uint8_t *); 2019 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len); 2020 2021 bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT); 2022 2023 pppoe_output(sc, m); 2024 } 2025 PPPOE_UNLOCK(sc); 2026 } 2027 2028 #ifdef PPPOE_MPSAFE 2029 static int 2030 pppoe_transmit(struct ifnet *ifp, struct mbuf *m) 2031 { 2032 struct pppoe_softc *sc = (void *)ifp; 2033 uint8_t *p; 2034 size_t len; 2035 2036 if (m == NULL) 2037 return EINVAL; 2038 2039 /* are we ready to process data yet? */ 2040 PPPOE_LOCK(sc, RW_READER); 2041 if (sc->sc_state < PPPOE_STATE_SESSION) { 2042 PPPOE_UNLOCK(sc); 2043 m_freem(m); 2044 return ENOBUFS; 2045 } 2046 2047 len = m->m_pkthdr.len; 2048 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT); 2049 if (m == NULL) { 2050 PPPOE_UNLOCK(sc); 2051 if_statinc(ifp, if_oerrors); 2052 return ENETDOWN; 2053 } 2054 p = mtod(m, uint8_t *); 2055 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len); 2056 2057 bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT); 2058 2059 pppoe_output(sc, m); 2060 PPPOE_UNLOCK(sc); 2061 return 0; 2062 } 2063 #endif /* PPPOE_MPSAFE */ 2064 2065 static void 2066 pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2) 2067 { 2068 struct ifnet *ifp = arg2; 2069 struct pppoe_softc *sc; 2070 DECLARE_SPLNET_VARIABLE; 2071 2072 if (cmd != PFIL_IFNET_DETACH) 2073 return; 2074 2075 ACQUIRE_SPLNET(); 2076 rw_enter(&pppoe_softc_list_lock, RW_READER); 2077 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 2078 PPPOE_LOCK(sc, RW_WRITER); 2079 if (sc->sc_eth_if != ifp) { 2080 PPPOE_UNLOCK(sc); 2081 continue; 2082 } 2083 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) { 2084 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 2085 pppoe_printf(sc, 2086 "ethernet interface detached, going down\n"); 2087 } 2088 sc->sc_eth_if = NULL; 2089 pppoe_clear_softc(sc, "ethernet interface detached"); 2090 PPPOE_UNLOCK(sc); 2091 } 2092 rw_exit(&pppoe_softc_list_lock); 2093 RELEASE_SPLNET(); 2094 } 2095 2096 static void 2097 pppoe_clear_softc(struct pppoe_softc *sc, const char *message) 2098 { 2099 KASSERT(PPPOE_WLOCKED(sc)); 2100 2101 /* stop timer */ 2102 callout_stop(&sc->sc_timeout); 2103 pppoe_printf(sc, "session 0x%x terminated, %s\n", 2104 sc->sc_session, message); 2105 2106 /* fix our state */ 2107 sc->sc_state = PPPOE_STATE_INITIAL; 2108 2109 /* signal upper layer */ 2110 PPPOE_UNLOCK(sc); 2111 sc->sc_sppp.pp_down(&sc->sc_sppp); 2112 PPPOE_LOCK(sc, RW_WRITER); 2113 2114 /* clean up softc */ 2115 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 2116 if (sc->sc_ac_cookie) { 2117 free(sc->sc_ac_cookie, M_DEVBUF); 2118 sc->sc_ac_cookie = NULL; 2119 } 2120 if (sc->sc_relay_sid) { 2121 free(sc->sc_relay_sid, M_DEVBUF); 2122 sc->sc_relay_sid = NULL; 2123 } 2124 sc->sc_ac_cookie_len = 0; 2125 sc->sc_session = 0; 2126 } 2127 2128 static void 2129 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m) 2130 { 2131 if (m->m_flags & M_PROMISC) { 2132 m_freem(m); 2133 return; 2134 } 2135 2136 #ifndef PPPOE_SERVER 2137 if (m->m_flags & (M_MCAST | M_BCAST)) { 2138 m_freem(m); 2139 return; 2140 } 2141 #endif 2142 2143 IFQ_LOCK(inq); 2144 if (IF_QFULL(inq)) { 2145 IF_DROP(inq); 2146 IFQ_UNLOCK(inq); 2147 m_freem(m); 2148 } else { 2149 IF_ENQUEUE(inq, m); 2150 IFQ_UNLOCK(inq); 2151 softint_schedule(pppoe_softintr); 2152 } 2153 return; 2154 } 2155 2156 void 2157 pppoe_input(struct ifnet *ifp, struct mbuf *m) 2158 { 2159 pppoe_enqueue(&ppoeinq, m); 2160 return; 2161 } 2162 2163 void 2164 pppoedisc_input(struct ifnet *ifp, struct mbuf *m) 2165 { 2166 pppoe_enqueue(&ppoediscinq, m); 2167 return; 2168 } 2169 2170 static void 2171 sysctl_net_pppoe_setup(struct sysctllog **clog) 2172 { 2173 const struct sysctlnode *node = NULL; 2174 extern pktq_rps_hash_func_t sppp_pktq_rps_hash_p; 2175 2176 sppp_pktq_rps_hash_p = pktq_rps_hash_default; 2177 2178 sysctl_createv(clog, 0, NULL, &node, 2179 CTLFLAG_PERMANENT, 2180 CTLTYPE_NODE, "pppoe", 2181 SYSCTL_DESCR("PPPOE protocol"), 2182 NULL, 0, NULL, 0, 2183 CTL_NET, CTL_CREATE, CTL_EOL); 2184 2185 if (node == NULL) 2186 return; 2187 2188 sysctl_createv(clog, 0, &node, NULL, 2189 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 2190 CTLTYPE_BOOL, "term_unknown", 2191 SYSCTL_DESCR("Terminate unknown sessions"), 2192 NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown), 2193 CTL_CREATE, CTL_EOL); 2194 2195 sysctl_createv(clog, 0, &node, NULL, 2196 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 2197 CTLTYPE_STRING, "rps_hash", 2198 SYSCTL_DESCR("Interface rps hash function control"), 2199 sysctl_pktq_rps_hash_handler, 0, (void *)&sppp_pktq_rps_hash_p, 2200 PKTQ_RPS_HASH_NAME_LEN, 2201 CTL_CREATE, CTL_EOL); 2202 } 2203 2204 /* 2205 * Module infrastructure 2206 */ 2207 #include "if_module.h" 2208 2209 IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr") 2210