if_pppoe.c revision 1.92
1/* $NetBSD: if_pppoe.c,v 1.92 2008/08/19 22:03:05 martin 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@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.92 2008/08/19 22:03:05 martin Exp $"); 34 35#include "pppoe.h" 36#include "bpfilter.h" 37#include "opt_pfil_hooks.h" 38#include "opt_pppoe.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/callout.h> 44#include <sys/malloc.h> 45#include <sys/mbuf.h> 46#include <sys/socket.h> 47#include <sys/proc.h> 48#include <sys/ioctl.h> 49#include <sys/kauth.h> 50#include <sys/intr.h> 51#include <sys/socketvar.h> 52 53#include <net/if.h> 54#include <net/if_types.h> 55#include <net/if_ether.h> 56#include <net/if_sppp.h> 57#include <net/if_spppvar.h> 58#include <net/if_pppoe.h> 59 60#if NBPFILTER > 0 61#include <net/bpf.h> 62#endif 63 64 65#undef PPPOE_DEBUG /* XXX - remove this or make it an option */ 66/* #define PPPOE_DEBUG 1 */ 67 68struct pppoehdr { 69 uint8_t vertype; 70 uint8_t code; 71 uint16_t session; 72 uint16_t plen; 73} __packed; 74 75struct pppoetag { 76 uint16_t tag; 77 uint16_t len; 78} __packed; 79 80#define PPPOE_HEADERLEN sizeof(struct pppoehdr) 81#define PPPOE_OVERHEAD (PPPOE_HEADERLEN + 2) 82#define PPPOE_VERTYPE 0x11 /* VER=1, TYPE = 1 */ 83 84#define PPPOE_TAG_EOL 0x0000 /* end of list */ 85#define PPPOE_TAG_SNAME 0x0101 /* service name */ 86#define PPPOE_TAG_ACNAME 0x0102 /* access concentrator name */ 87#define PPPOE_TAG_HUNIQUE 0x0103 /* host unique */ 88#define PPPOE_TAG_ACCOOKIE 0x0104 /* AC cookie */ 89#define PPPOE_TAG_VENDOR 0x0105 /* vendor specific */ 90#define PPPOE_TAG_RELAYSID 0x0110 /* relay session id */ 91#define PPPOE_TAG_SNAME_ERR 0x0201 /* service name error */ 92#define PPPOE_TAG_ACSYS_ERR 0x0202 /* AC system error */ 93#define PPPOE_TAG_GENERIC_ERR 0x0203 /* gerneric error */ 94 95#define PPPOE_CODE_PADI 0x09 /* Active Discovery Initiation */ 96#define PPPOE_CODE_PADO 0x07 /* Active Discovery Offer */ 97#define PPPOE_CODE_PADR 0x19 /* Active Discovery Request */ 98#define PPPOE_CODE_PADS 0x65 /* Active Discovery Session confirmation */ 99#define PPPOE_CODE_PADT 0xA7 /* Active Discovery Terminate */ 100 101/* two byte PPP protocol discriminator, then IP data */ 102#define PPPOE_MAXMTU (ETHERMTU - PPPOE_OVERHEAD) 103 104/* Add a 16 bit unsigned value to a buffer pointed to by PTR */ 105#define PPPOE_ADD_16(PTR, VAL) \ 106 *(PTR)++ = (VAL) / 256; \ 107 *(PTR)++ = (VAL) % 256 108 109/* Add a complete PPPoE header to the buffer pointed to by PTR */ 110#define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \ 111 *(PTR)++ = PPPOE_VERTYPE; \ 112 *(PTR)++ = (CODE); \ 113 PPPOE_ADD_16(PTR, SESS); \ 114 PPPOE_ADD_16(PTR, LEN) 115 116#define PPPOE_DISC_TIMEOUT (hz*5) /* base for quick timeout calculation */ 117#define PPPOE_SLOW_RETRY (hz*60) /* persistent retry interval */ 118#define PPPOE_RECON_FAST (hz*15) /* first retry after auth failure */ 119#define PPPOE_RECON_IMMEDIATE (hz/10) /* "no delay" reconnect */ 120#define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */ 121#define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */ 122 123#ifdef PPPOE_SERVER 124/* from if_spppsubr.c */ 125#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */ 126#endif 127 128struct pppoe_softc { 129 struct sppp sc_sppp; /* contains a struct ifnet as first element */ 130 LIST_ENTRY(pppoe_softc) sc_list; 131 struct ifnet *sc_eth_if; /* ethernet interface we are using */ 132 133 int sc_state; /* discovery phase or session connected */ 134 struct ether_addr sc_dest; /* hardware address of concentrator */ 135 uint16_t sc_session; /* PPPoE session id */ 136 137 char *sc_service_name; /* if != NULL: requested name of service */ 138 char *sc_concentrator_name; /* if != NULL: requested concentrator id */ 139 uint8_t *sc_ac_cookie; /* content of AC cookie we must echo back */ 140 size_t sc_ac_cookie_len; /* length of cookie data */ 141#ifdef PPPOE_SERVER 142 uint8_t *sc_hunique; /* content of host unique we must echo back */ 143 size_t sc_hunique_len; /* length of host unique */ 144#endif 145 callout_t sc_timeout; /* timeout while not in session state */ 146 int sc_padi_retried; /* number of PADI retries already done */ 147 int sc_padr_retried; /* number of PADR retries already done */ 148}; 149 150/* incoming traffic will be queued here */ 151struct ifqueue ppoediscinq = { .ifq_maxlen = IFQ_MAXLEN }; 152struct ifqueue ppoeinq = { .ifq_maxlen = IFQ_MAXLEN }; 153 154void *pppoe_softintr = NULL; 155static void pppoe_softintr_handler(void *); 156 157extern int sppp_ioctl(struct ifnet *, unsigned long, void *); 158 159/* input routines */ 160static void pppoe_input(void); 161static void pppoe_disc_input(struct mbuf *); 162static void pppoe_dispatch_disc_pkt(struct mbuf *, int); 163static void pppoe_data_input(struct mbuf *); 164 165/* management routines */ 166void pppoeattach(int); 167static int pppoe_connect(struct pppoe_softc *); 168static int pppoe_disconnect(struct pppoe_softc *); 169static void pppoe_abort_connect(struct pppoe_softc *); 170static int pppoe_ioctl(struct ifnet *, unsigned long, void *); 171static void pppoe_tls(struct sppp *); 172static void pppoe_tlf(struct sppp *); 173static void pppoe_start(struct ifnet *); 174static void pppoe_clear_softc(struct pppoe_softc *, const char *); 175 176/* internal timeout handling */ 177static void pppoe_timeout(void *); 178 179/* sending actual protocol controll packets */ 180static int pppoe_send_padi(struct pppoe_softc *); 181static int pppoe_send_padr(struct pppoe_softc *); 182#ifdef PPPOE_SERVER 183static int pppoe_send_pado(struct pppoe_softc *); 184static int pppoe_send_pads(struct pppoe_softc *); 185#endif 186static int pppoe_send_padt(struct ifnet *, u_int, const uint8_t *); 187 188/* raw output */ 189static int pppoe_output(struct pppoe_softc *, struct mbuf *); 190 191/* internal helper functions */ 192static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *); 193static struct pppoe_softc * pppoe_find_softc_by_hunique(uint8_t *, size_t, struct ifnet *); 194static struct mbuf *pppoe_get_mbuf(size_t len); 195 196#ifdef PFIL_HOOKS 197static int pppoe_ifattach_hook(void *, struct mbuf **, struct ifnet *, int); 198#endif 199 200static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list; 201 202static int pppoe_clone_create(struct if_clone *, int); 203static int pppoe_clone_destroy(struct ifnet *); 204 205static struct if_clone pppoe_cloner = 206 IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy); 207 208/* ARGSUSED */ 209void 210pppoeattach(int count) 211{ 212 LIST_INIT(&pppoe_softc_list); 213 if_clone_attach(&pppoe_cloner); 214 215 pppoe_softintr = softint_establish(SOFTINT_NET, pppoe_softintr_handler, NULL); 216} 217 218static int 219pppoe_clone_create(struct if_clone *ifc, int unit) 220{ 221 struct pppoe_softc *sc; 222 223 sc = malloc(sizeof(struct pppoe_softc), M_DEVBUF, M_WAITOK|M_ZERO); 224 225 if_initname(&sc->sc_sppp.pp_if, "pppoe", unit); 226 sc->sc_sppp.pp_if.if_softc = sc; 227 sc->sc_sppp.pp_if.if_mtu = PPPOE_MAXMTU; 228 sc->sc_sppp.pp_if.if_flags = IFF_SIMPLEX|IFF_POINTOPOINT|IFF_MULTICAST; 229 sc->sc_sppp.pp_if.if_type = IFT_PPP; 230 sc->sc_sppp.pp_if.if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN; 231 sc->sc_sppp.pp_if.if_dlt = DLT_PPP_ETHER; 232 sc->sc_sppp.pp_flags |= PP_KEEPALIVE | /* use LCP keepalive */ 233 PP_NOFRAMING; /* no serial encapsulation */ 234 sc->sc_sppp.pp_if.if_ioctl = pppoe_ioctl; 235 IFQ_SET_MAXLEN(&sc->sc_sppp.pp_if.if_snd, IFQ_MAXLEN); 236 IFQ_SET_READY(&sc->sc_sppp.pp_if.if_snd); 237 238 /* changed to real address later */ 239 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 240 241 callout_init(&sc->sc_timeout, 0); 242 243 sc->sc_sppp.pp_if.if_start = pppoe_start; 244 sc->sc_sppp.pp_tls = pppoe_tls; 245 sc->sc_sppp.pp_tlf = pppoe_tlf; 246 sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN; /* framing added to ppp packets */ 247 248 if_attach(&sc->sc_sppp.pp_if); 249 sppp_attach(&sc->sc_sppp.pp_if); 250 251#if NBPFILTER > 0 252 bpfattach(&sc->sc_sppp.pp_if, DLT_PPP_ETHER, 0); 253#endif 254#ifdef PFIL_HOOKS 255 if (LIST_EMPTY(&pppoe_softc_list)) 256 pfil_add_hook(pppoe_ifattach_hook, NULL, 257 PFIL_IFNET|PFIL_WAITOK, &if_pfil); 258#endif 259 LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list); 260 return 0; 261} 262 263static int 264pppoe_clone_destroy(struct ifnet *ifp) 265{ 266 struct pppoe_softc * sc = ifp->if_softc; 267 268 callout_stop(&sc->sc_timeout); 269 LIST_REMOVE(sc, sc_list); 270#ifdef PFIL_HOOKS 271 if (LIST_EMPTY(&pppoe_softc_list)) 272 pfil_remove_hook(pppoe_ifattach_hook, NULL, 273 PFIL_IFNET|PFIL_WAITOK, &if_pfil); 274#endif 275#if NBPFILTER > 0 276 bpfdetach(ifp); 277#endif 278 sppp_detach(&sc->sc_sppp.pp_if); 279 if_detach(ifp); 280 if (sc->sc_concentrator_name) 281 free(sc->sc_concentrator_name, M_DEVBUF); 282 if (sc->sc_service_name) 283 free(sc->sc_service_name, M_DEVBUF); 284 if (sc->sc_ac_cookie) 285 free(sc->sc_ac_cookie, M_DEVBUF); 286 callout_destroy(&sc->sc_timeout); 287 free(sc, M_DEVBUF); 288 289 return (0); 290} 291 292/* 293 * Find the interface handling the specified session. 294 * Note: O(number of sessions open), this is a client-side only, mean 295 * and lean implementation, so number of open sessions typically should 296 * be 1. 297 */ 298static struct pppoe_softc * 299pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif) 300{ 301 struct pppoe_softc *sc; 302 303 if (session == 0) 304 return NULL; 305 306 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 307 if (sc->sc_state == PPPOE_STATE_SESSION 308 && sc->sc_session == session) { 309 if (sc->sc_eth_if == rcvif) 310 return sc; 311 else 312 return NULL; 313 } 314 } 315 return NULL; 316} 317 318/* Check host unique token passed and return appropriate softc pointer, 319 * or NULL if token is bogus. */ 320static struct pppoe_softc * 321pppoe_find_softc_by_hunique(uint8_t *token, size_t len, struct ifnet *rcvif) 322{ 323 struct pppoe_softc *sc, *t; 324 325 if (LIST_EMPTY(&pppoe_softc_list)) 326 return NULL; 327 328 if (len != sizeof sc) 329 return NULL; 330 memcpy(&t, token, len); 331 332 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) 333 if (sc == t) break; 334 335 if (sc == NULL) { 336#ifdef PPPOE_DEBUG 337 printf("pppoe: alien host unique tag, no session found\n"); 338#endif 339 return NULL; 340 } 341 342 /* should be safe to access *sc now */ 343 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) { 344 printf("%s: host unique tag found, but it belongs to a connection in state %d\n", 345 sc->sc_sppp.pp_if.if_xname, sc->sc_state); 346 return NULL; 347 } 348 if (sc->sc_eth_if != rcvif) { 349 printf("%s: wrong interface, not accepting host unique\n", 350 sc->sc_sppp.pp_if.if_xname); 351 return NULL; 352 } 353 return sc; 354} 355 356static void 357pppoe_softintr_handler(void *dummy) 358{ 359 /* called at splsoftnet() */ 360 mutex_enter(softnet_lock); 361 pppoe_input(); 362 mutex_exit(softnet_lock); 363} 364 365/* called at appropriate protection level */ 366static void 367pppoe_input(void) 368{ 369 struct mbuf *m; 370 int s, disc_done, data_done; 371 372 do { 373 disc_done = 0; 374 data_done = 0; 375 for (;;) { 376 s = splnet(); 377 IF_DEQUEUE(&ppoediscinq, m); 378 splx(s); 379 if (m == NULL) break; 380 disc_done = 1; 381 pppoe_disc_input(m); 382 } 383 384 for (;;) { 385 s = splnet(); 386 IF_DEQUEUE(&ppoeinq, m); 387 splx(s); 388 if (m == NULL) break; 389 data_done = 1; 390 pppoe_data_input(m); 391 } 392 } while (disc_done || data_done); 393} 394 395/* analyze and handle a single received packet while not in session state */ 396static void 397pppoe_dispatch_disc_pkt(struct mbuf *m, int off) 398{ 399 uint16_t tag, len; 400 uint16_t session, plen; 401 struct pppoe_softc *sc; 402 const char *err_msg, *devname; 403 char *error; 404 uint8_t *ac_cookie; 405 size_t ac_cookie_len; 406#ifdef PPPOE_SERVER 407 uint8_t *hunique; 408 size_t hunique_len; 409#endif 410 struct pppoehdr *ph; 411 struct pppoetag *pt; 412 struct mbuf *n; 413 int noff, err, errortag; 414 struct ether_header *eh; 415 416 devname = "pppoe"; /* as long as we don't know which instance */ 417 err_msg = NULL; 418 errortag = 0; 419 if (m->m_len < sizeof(*eh)) { 420 m = m_pullup(m, sizeof(*eh)); 421 if (!m) 422 goto done; 423 } 424 eh = mtod(m, struct ether_header *); 425 off += sizeof(*eh); 426 427 ac_cookie = NULL; 428 ac_cookie_len = 0; 429#ifdef PPPOE_SERVER 430 hunique = NULL; 431 hunique_len = 0; 432#endif 433 session = 0; 434 if (m->m_pkthdr.len - off <= PPPOE_HEADERLEN) { 435 printf("pppoe: packet too short: %d\n", m->m_pkthdr.len); 436 goto done; 437 } 438 439 n = m_pulldown(m, off, sizeof(*ph), &noff); 440 if (!n) { 441 printf("pppoe: could not get PPPoE header\n"); 442 m = NULL; 443 goto done; 444 } 445 ph = (struct pppoehdr *)(mtod(n, char *) + noff); 446 if (ph->vertype != PPPOE_VERTYPE) { 447 printf("pppoe: unknown version/type packet: 0x%x\n", 448 ph->vertype); 449 goto done; 450 } 451 session = ntohs(ph->session); 452 plen = ntohs(ph->plen); 453 off += sizeof(*ph); 454 455 if (plen + off > m->m_pkthdr.len) { 456 printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n", 457 m->m_pkthdr.len - off, plen); 458 goto done; 459 } 460 m_adj(m, off + plen - m->m_pkthdr.len); /* ignore trailing garbage */ 461 tag = 0; 462 len = 0; 463 sc = NULL; 464 while (off + sizeof(*pt) <= m->m_pkthdr.len) { 465 n = m_pulldown(m, off, sizeof(*pt), &noff); 466 if (!n) { 467 printf("%s: parse error\n", devname); 468 m = NULL; 469 goto done; 470 } 471 pt = (struct pppoetag *)(mtod(n, char *) + noff); 472 tag = ntohs(pt->tag); 473 len = ntohs(pt->len); 474 if (off + len + sizeof(*pt) > m->m_pkthdr.len) { 475 printf("pppoe: tag 0x%x len 0x%x is too long\n", 476 tag, len); 477 goto done; 478 } 479 switch (tag) { 480 case PPPOE_TAG_EOL: 481 goto breakbreak; 482 case PPPOE_TAG_SNAME: 483 break; /* ignored */ 484 case PPPOE_TAG_ACNAME: 485 error = NULL; 486 if (sc != NULL && len > 0) { 487 error = malloc(len+1, M_TEMP, M_NOWAIT); 488 if (error) { 489 n = m_pulldown(m, off + sizeof(*pt), 490 len, &noff); 491 if (n) { 492 strncpy(error, 493 mtod(n, char*) + noff, 494 len); 495 error[len] = '\0'; 496 } 497 printf("%s: connected to %s\n", 498 devname, error); 499 free(error, M_TEMP); 500 } 501 } 502 break; /* ignored */ 503 case PPPOE_TAG_HUNIQUE: 504 if (sc != NULL) 505 break; 506 n = m_pulldown(m, off + sizeof(*pt), len, &noff); 507 if (!n) { 508 m = NULL; 509 err_msg = "TAG HUNIQUE ERROR"; 510 break; 511 } 512#ifdef PPPOE_SERVER 513 hunique = mtod(n, uint8_t *) + noff; 514 hunique_len = len; 515#endif 516 sc = pppoe_find_softc_by_hunique(mtod(n, char *) + noff, 517 len, m->m_pkthdr.rcvif); 518 if (sc != NULL) 519 devname = sc->sc_sppp.pp_if.if_xname; 520 break; 521 case PPPOE_TAG_ACCOOKIE: 522 if (ac_cookie == NULL) { 523 n = m_pulldown(m, off + sizeof(*pt), len, 524 &noff); 525 if (!n) { 526 err_msg = "TAG ACCOOKIE ERROR"; 527 m = NULL; 528 break; 529 } 530 ac_cookie = mtod(n, char *) + noff; 531 ac_cookie_len = len; 532 } 533 break; 534 case PPPOE_TAG_SNAME_ERR: 535 err_msg = "SERVICE NAME ERROR"; 536 errortag = 1; 537 break; 538 case PPPOE_TAG_ACSYS_ERR: 539 err_msg = "AC SYSTEM ERROR"; 540 errortag = 1; 541 break; 542 case PPPOE_TAG_GENERIC_ERR: 543 err_msg = "GENERIC ERROR"; 544 errortag = 1; 545 break; 546 } 547 if (err_msg) { 548 error = NULL; 549 if (errortag && len) { 550 error = malloc(len+1, M_TEMP, M_NOWAIT); 551 n = m_pulldown(m, off + sizeof(*pt), len, 552 &noff); 553 if (n && error) { 554 strncpy(error, 555 mtod(n, char *) + noff, len); 556 error[len] = '\0'; 557 } 558 } 559 if (error) { 560 printf("%s: %s: %s\n", devname, 561 err_msg, error); 562 free(error, M_TEMP); 563 } else 564 printf("%s: %s\n", devname, err_msg); 565 if (errortag || m == NULL) 566 goto done; 567 } 568 off += sizeof(*pt) + len; 569 } 570breakbreak:; 571 switch (ph->code) { 572 case PPPOE_CODE_PADI: 573#ifdef PPPOE_SERVER 574 /* 575 * got service name, concentrator name, and/or host unique. 576 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP. 577 */ 578 if (LIST_EMPTY(&pppoe_softc_list)) 579 goto done; 580 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 581 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) 582 continue; 583 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) 584 continue; 585 if (sc->sc_state == PPPOE_STATE_INITIAL) 586 break; 587 } 588 if (sc == NULL) { 589/* printf("pppoe: free passive interface is not found\n");*/ 590 goto done; 591 } 592 if (hunique) { 593 if (sc->sc_hunique) 594 free(sc->sc_hunique, M_DEVBUF); 595 sc->sc_hunique = malloc(hunique_len, M_DEVBUF, 596 M_DONTWAIT); 597 if (sc->sc_hunique == NULL) 598 goto done; 599 sc->sc_hunique_len = hunique_len; 600 memcpy(sc->sc_hunique, hunique, hunique_len); 601 } 602 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest); 603 sc->sc_state = PPPOE_STATE_PADO_SENT; 604 pppoe_send_pado(sc); 605 break; 606#endif /* PPPOE_SERVER */ 607 case PPPOE_CODE_PADR: 608#ifdef PPPOE_SERVER 609 /* 610 * get sc from ac_cookie if IFF_PASSIVE 611 */ 612 if (ac_cookie == NULL) { 613 /* be quiet if there is not a single pppoe instance */ 614 printf("pppoe: received PADR but not includes ac_cookie\n"); 615 goto done; 616 } 617 sc = pppoe_find_softc_by_hunique(ac_cookie, 618 ac_cookie_len, 619 m->m_pkthdr.rcvif); 620 if (sc == NULL) { 621 /* be quiet if there is not a single pppoe instance */ 622 if (!LIST_EMPTY(&pppoe_softc_list)) 623 printf("pppoe: received PADR but could not find request for it\n"); 624 goto done; 625 } 626 if (sc->sc_state != PPPOE_STATE_PADO_SENT) { 627 printf("%s: received unexpected PADR\n", 628 sc->sc_sppp.pp_if.if_xname); 629 goto done; 630 } 631 if (hunique) { 632 if (sc->sc_hunique) 633 free(sc->sc_hunique, M_DEVBUF); 634 sc->sc_hunique = malloc(hunique_len, M_DEVBUF, 635 M_DONTWAIT); 636 if (sc->sc_hunique == NULL) 637 goto done; 638 sc->sc_hunique_len = hunique_len; 639 memcpy(sc->sc_hunique, hunique, hunique_len); 640 } 641 pppoe_send_pads(sc); 642 sc->sc_state = PPPOE_STATE_SESSION; 643 sc->sc_sppp.pp_up(&sc->sc_sppp); 644 break; 645#else 646 /* ignore, we are no access concentrator */ 647 goto done; 648#endif /* PPPOE_SERVER */ 649 case PPPOE_CODE_PADO: 650 if (sc == NULL) { 651 /* be quiet if there is not a single pppoe instance */ 652 if (!LIST_EMPTY(&pppoe_softc_list)) 653 printf("pppoe: received PADO but could not find request for it\n"); 654 goto done; 655 } 656 if (sc->sc_state != PPPOE_STATE_PADI_SENT) { 657 printf("%s: received unexpected PADO\n", 658 sc->sc_sppp.pp_if.if_xname); 659 goto done; 660 } 661 if (ac_cookie) { 662 if (sc->sc_ac_cookie) 663 free(sc->sc_ac_cookie, M_DEVBUF); 664 sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF, 665 M_DONTWAIT); 666 if (sc->sc_ac_cookie == NULL) { 667 printf("%s: FATAL: could not allocate memory " 668 "for AC cookie\n", 669 sc->sc_sppp.pp_if.if_xname); 670 goto done; 671 } 672 sc->sc_ac_cookie_len = ac_cookie_len; 673 memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len); 674 } 675 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest); 676 callout_stop(&sc->sc_timeout); 677 sc->sc_padr_retried = 0; 678 sc->sc_state = PPPOE_STATE_PADR_SENT; 679 if ((err = pppoe_send_padr(sc)) != 0) { 680 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 681 printf("%s: failed to send PADR, " 682 "error=%d\n", sc->sc_sppp.pp_if.if_xname, 683 err); 684 } 685 callout_reset(&sc->sc_timeout, 686 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), 687 pppoe_timeout, sc); 688 break; 689 case PPPOE_CODE_PADS: 690 if (sc == NULL) 691 goto done; 692 sc->sc_session = session; 693 callout_stop(&sc->sc_timeout); 694 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 695 printf("%s: session 0x%x connected\n", 696 sc->sc_sppp.pp_if.if_xname, session); 697 sc->sc_state = PPPOE_STATE_SESSION; 698 sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */ 699 break; 700 case PPPOE_CODE_PADT: 701 if (sc == NULL) 702 goto done; 703 pppoe_clear_softc(sc, "received PADT"); 704 break; 705 default: 706 printf("%s: unknown code (0x%04x) session = 0x%04x\n", 707 sc? sc->sc_sppp.pp_if.if_xname : "pppoe", 708 ph->code, session); 709 break; 710 } 711 712done: 713 if (m) 714 m_freem(m); 715 return; 716} 717 718static void 719pppoe_disc_input(struct mbuf *m) 720{ 721 722 /* avoid error messages if there is not a single pppoe instance */ 723 if (!LIST_EMPTY(&pppoe_softc_list)) { 724 KASSERT(m->m_flags & M_PKTHDR); 725 pppoe_dispatch_disc_pkt(m, 0); 726 } else 727 m_freem(m); 728} 729 730static void 731pppoe_data_input(struct mbuf *m) 732{ 733 uint16_t session, plen; 734 struct pppoe_softc *sc; 735 struct pppoehdr *ph; 736#ifdef PPPOE_TERM_UNKNOWN_SESSIONS 737 uint8_t shost[ETHER_ADDR_LEN]; 738#endif 739 740 KASSERT(m->m_flags & M_PKTHDR); 741 742#ifdef PPPOE_TERM_UNKNOWN_SESSIONS 743 memcpy(shost, mtod(m, struct ether_header*)->ether_shost, ETHER_ADDR_LEN); 744#endif 745 m_adj(m, sizeof(struct ether_header)); 746 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) { 747 printf("pppoe (data): dropping too short packet: %d bytes\n", 748 m->m_pkthdr.len); 749 goto drop; 750 } 751 752 if (m->m_len < sizeof(*ph)) { 753 m = m_pullup(m, sizeof(*ph)); 754 if (!m) { 755 printf("pppoe: could not get PPPoE header\n"); 756 return; 757 } 758 } 759 ph = mtod(m, struct pppoehdr *); 760 761 if (ph->vertype != PPPOE_VERTYPE) { 762 printf("pppoe (data): unknown version/type packet: 0x%x\n", 763 ph->vertype); 764 goto drop; 765 } 766 if (ph->code != 0) 767 goto drop; 768 769 session = ntohs(ph->session); 770 sc = pppoe_find_softc_by_session(session, m->m_pkthdr.rcvif); 771 if (sc == NULL) { 772#ifdef PPPOE_TERM_UNKNOWN_SESSIONS 773 printf("pppoe: input for unknown session 0x%x, sending PADT\n", 774 session); 775 pppoe_send_padt(m->m_pkthdr.rcvif, session, shost); 776#endif 777 goto drop; 778 } 779 780 plen = ntohs(ph->plen); 781 782#if NBPFILTER > 0 783 if(sc->sc_sppp.pp_if.if_bpf) 784 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m); 785#endif 786 787 m_adj(m, PPPOE_HEADERLEN); 788 789#ifdef PPPOE_DEBUG 790 { 791 struct mbuf *p; 792 793 printf("%s: pkthdr.len=%d, pppoe.len=%d", 794 sc->sc_sppp.pp_if.if_xname, 795 m->m_pkthdr.len, plen); 796 p = m; 797 while (p) { 798 printf(" l=%d", p->m_len); 799 p = p->m_next; 800 } 801 printf("\n"); 802 } 803#endif 804 805 if (m->m_pkthdr.len < plen) 806 goto drop; 807 808 /* fix incoming interface pointer (not the raw ethernet interface anymore) */ 809 m->m_pkthdr.rcvif = &sc->sc_sppp.pp_if; 810 811 /* pass packet up and account for it */ 812 sc->sc_sppp.pp_if.if_ipackets++; 813 sppp_input(&sc->sc_sppp.pp_if, m); 814 return; 815 816drop: 817 m_freem(m); 818} 819 820static int 821pppoe_output(struct pppoe_softc *sc, struct mbuf *m) 822{ 823 struct sockaddr dst; 824 struct ether_header *eh; 825 uint16_t etype; 826 827 if (sc->sc_eth_if == NULL) { 828 m_freem(m); 829 return EIO; 830 } 831 832 memset(&dst, 0, sizeof dst); 833 dst.sa_family = AF_UNSPEC; 834 eh = (struct ether_header*)&dst.sa_data; 835 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC; 836 eh->ether_type = htons(etype); 837 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest); 838 839#ifdef PPPOE_DEBUG 840 printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n", 841 sc->sc_sppp.pp_if.if_xname, etype, 842 sc->sc_state, sc->sc_session, 843 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len); 844#endif 845 846 m->m_flags &= ~(M_BCAST|M_MCAST); 847 sc->sc_sppp.pp_if.if_opackets++; 848 return sc->sc_eth_if->if_output(sc->sc_eth_if, m, &dst, NULL); 849} 850 851static int 852pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data) 853{ 854 struct lwp *l = curlwp; /* XXX */ 855 struct pppoe_softc *sc = (struct pppoe_softc*)ifp; 856 struct ifreq *ifr = data; 857 int error = 0; 858 859 switch (cmd) { 860 case PPPOESETPARMS: 861 { 862 struct pppoediscparms *parms = (struct pppoediscparms*)data; 863 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 864 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 865 NULL) != 0) 866 return (EPERM); 867 if (parms->eth_ifname[0] != 0) { 868 struct ifnet *eth_if; 869 870 eth_if = ifunit(parms->eth_ifname); 871 if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) { 872 sc->sc_eth_if = NULL; 873 return ENXIO; 874 } 875 876 if (sc->sc_sppp.pp_if.if_mtu > 877 eth_if->if_mtu - PPPOE_OVERHEAD) { 878 sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu - 879 PPPOE_OVERHEAD; 880 } 881 sc->sc_eth_if = eth_if; 882 } 883 if (parms->ac_name != NULL) { 884 size_t s; 885 char *b = malloc(parms->ac_name_len + 1, M_DEVBUF, 886 M_WAITOK); 887 if (b == NULL) 888 return ENOMEM; 889 error = copyinstr(parms->ac_name, b, 890 parms->ac_name_len+1, &s); 891 if (error != 0) { 892 free(b, M_DEVBUF); 893 return error; 894 } 895 if (s != parms->ac_name_len+1) { 896 free(b, M_DEVBUF); 897 return EINVAL; 898 } 899 if (sc->sc_concentrator_name) 900 free(sc->sc_concentrator_name, M_DEVBUF); 901 sc->sc_concentrator_name = b; 902 } 903 if (parms->service_name != NULL) { 904 size_t s; 905 char *b = malloc(parms->service_name_len + 1, M_DEVBUF, 906 M_WAITOK); 907 if (b == NULL) 908 return ENOMEM; 909 error = copyinstr(parms->service_name, b, 910 parms->service_name_len+1, &s); 911 if (error != 0) { 912 free(b, M_DEVBUF); 913 return error; 914 } 915 if (s != parms->service_name_len+1) { 916 free(b, M_DEVBUF); 917 return EINVAL; 918 } 919 if (sc->sc_service_name) 920 free(sc->sc_service_name, M_DEVBUF); 921 sc->sc_service_name = b; 922 } 923 return 0; 924 } 925 break; 926 case PPPOEGETPARMS: 927 { 928 struct pppoediscparms *parms = (struct pppoediscparms*)data; 929 memset(parms, 0, sizeof *parms); 930 if (sc->sc_eth_if) 931 strncpy(parms->ifname, sc->sc_eth_if->if_xname, IFNAMSIZ); 932 return 0; 933 } 934 break; 935 case PPPOEGETSESSION: 936 { 937 struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data; 938 state->state = sc->sc_state; 939 state->session_id = sc->sc_session; 940 state->padi_retry_no = sc->sc_padi_retried; 941 state->padr_retry_no = sc->sc_padr_retried; 942 return 0; 943 } 944 break; 945 case SIOCSIFFLAGS: 946 /* 947 * Prevent running re-establishment timers overriding 948 * administrators choice. 949 */ 950 if ((ifr->ifr_flags & IFF_UP) == 0 951 && sc->sc_state >= PPPOE_STATE_PADI_SENT 952 && sc->sc_state < PPPOE_STATE_SESSION) { 953 callout_stop(&sc->sc_timeout); 954 sc->sc_state = PPPOE_STATE_INITIAL; 955 sc->sc_padi_retried = 0; 956 sc->sc_padr_retried = 0; 957 memcpy(&sc->sc_dest, etherbroadcastaddr, 958 sizeof(sc->sc_dest)); 959 } 960 return sppp_ioctl(ifp, cmd, data); 961 case SIOCSIFMTU: 962 if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ? 963 PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) { 964 return EINVAL; 965 } 966 /*FALLTHROUGH*/ 967 default: 968 return sppp_ioctl(ifp, cmd, data); 969 } 970 return 0; 971} 972 973/* 974 * Allocate a mbuf/cluster with space to store the given data length 975 * of payload, leaving space for prepending an ethernet header 976 * in front. 977 */ 978static struct mbuf * 979pppoe_get_mbuf(size_t len) 980{ 981 struct mbuf *m; 982 983 MGETHDR(m, M_DONTWAIT, MT_DATA); 984 if (m == NULL) 985 return NULL; 986 if (len + sizeof(struct ether_header) > MHLEN) { 987 MCLGET(m, M_DONTWAIT); 988 if ((m->m_flags & M_EXT) == 0) { 989 struct mbuf *n; 990 MFREE(m, n); 991 return 0; 992 } 993 } 994 m->m_data += sizeof(struct ether_header); 995 m->m_len = len; 996 m->m_pkthdr.len = len; 997 m->m_pkthdr.rcvif = NULL; 998 999 return m; 1000} 1001 1002static int 1003pppoe_send_padi(struct pppoe_softc *sc) 1004{ 1005 struct mbuf *m0; 1006 int len, l1 = 0, l2 = 0; /* XXX: gcc */ 1007 uint8_t *p; 1008 1009 if (sc->sc_state >PPPOE_STATE_PADI_SENT) 1010 panic("pppoe_send_padi in state %d", sc->sc_state); 1011 1012 /* calculate length of frame (excluding ethernet header + pppoe header) */ 1013 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */ 1014 if (sc->sc_service_name != NULL) { 1015 l1 = strlen(sc->sc_service_name); 1016 len += l1; 1017 } 1018 if (sc->sc_concentrator_name != NULL) { 1019 l2 = strlen(sc->sc_concentrator_name); 1020 len += 2 + 2 + l2; 1021 } 1022 1023 /* allocate a buffer */ 1024 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); /* header len + payload len */ 1025 if (!m0) 1026 return ENOBUFS; 1027 1028 /* fill in pkt */ 1029 p = mtod(m0, uint8_t *); 1030 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len); 1031 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1032 if (sc->sc_service_name != NULL) { 1033 PPPOE_ADD_16(p, l1); 1034 memcpy(p, sc->sc_service_name, l1); 1035 p += l1; 1036 } else { 1037 PPPOE_ADD_16(p, 0); 1038 } 1039 if (sc->sc_concentrator_name != NULL) { 1040 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME); 1041 PPPOE_ADD_16(p, l2); 1042 memcpy(p, sc->sc_concentrator_name, l2); 1043 p += l2; 1044 } 1045 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1046 PPPOE_ADD_16(p, sizeof(sc)); 1047 memcpy(p, &sc, sizeof sc); 1048 1049#ifdef PPPOE_DEBUG 1050 p += sizeof sc; 1051 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN) 1052 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld", 1053 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *))); 1054#endif 1055 1056 /* send pkt */ 1057 return pppoe_output(sc, m0); 1058} 1059 1060static void 1061pppoe_timeout(void *arg) 1062{ 1063 int x, retry_wait, err; 1064 struct pppoe_softc *sc = (struct pppoe_softc*)arg; 1065 1066#ifdef PPPOE_DEBUG 1067 printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname); 1068#endif 1069 1070 switch (sc->sc_state) { 1071 case PPPOE_STATE_INITIAL: 1072 /* delayed connect from pppoe_tls() */ 1073 pppoe_connect(sc); 1074 break; 1075 case PPPOE_STATE_PADI_SENT: 1076 /* 1077 * We have two basic ways of retrying: 1078 * - Quick retry mode: try a few times in short sequence 1079 * - Slow retry mode: we already had a connection successfully 1080 * established and will try infinitely (without user 1081 * intervention) 1082 * We only enter slow retry mode if IFF_LINK1 (aka autodial) 1083 * is not set. 1084 */ 1085 1086 /* initialize for quick retry mode */ 1087 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried); 1088 1089 x = splnet(); 1090 sc->sc_padi_retried++; 1091 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) { 1092 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) { 1093 /* slow retry mode */ 1094 retry_wait = PPPOE_SLOW_RETRY; 1095 } else { 1096 pppoe_abort_connect(sc); 1097 splx(x); 1098 return; 1099 } 1100 } 1101 if ((err = pppoe_send_padi(sc)) != 0) { 1102 sc->sc_padi_retried--; 1103 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1104 printf("%s: failed to transmit PADI, " 1105 "error=%d\n", 1106 sc->sc_sppp.pp_if.if_xname, err); 1107 } 1108 callout_reset(&sc->sc_timeout, retry_wait, 1109 pppoe_timeout, sc); 1110 splx(x); 1111 break; 1112 1113 case PPPOE_STATE_PADR_SENT: 1114 x = splnet(); 1115 sc->sc_padr_retried++; 1116 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) { 1117 memcpy(&sc->sc_dest, etherbroadcastaddr, 1118 sizeof(sc->sc_dest)); 1119 sc->sc_state = PPPOE_STATE_PADI_SENT; 1120 sc->sc_padr_retried = 0; 1121 if ((err = pppoe_send_padi(sc)) != 0) { 1122 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1123 printf("%s: failed to send PADI" 1124 ", error=%d\n", 1125 sc->sc_sppp.pp_if.if_xname, 1126 err); 1127 } 1128 callout_reset(&sc->sc_timeout, 1129 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), 1130 pppoe_timeout, sc); 1131 splx(x); 1132 return; 1133 } 1134 if ((err = pppoe_send_padr(sc)) != 0) { 1135 sc->sc_padr_retried--; 1136 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1137 printf("%s: failed to send PADR, " 1138 "error=%d\n", sc->sc_sppp.pp_if.if_xname, 1139 err); 1140 } 1141 callout_reset(&sc->sc_timeout, 1142 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), 1143 pppoe_timeout, sc); 1144 splx(x); 1145 break; 1146 case PPPOE_STATE_CLOSING: 1147 pppoe_disconnect(sc); 1148 break; 1149 default: 1150 return; /* all done, work in peace */ 1151 } 1152} 1153 1154/* Start a connection (i.e. initiate discovery phase) */ 1155static int 1156pppoe_connect(struct pppoe_softc *sc) 1157{ 1158 int x, err; 1159 1160 if (sc->sc_state != PPPOE_STATE_INITIAL) 1161 return EBUSY; 1162 1163#ifdef PPPOE_SERVER 1164 /* wait PADI if IFF_PASSIVE */ 1165 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) 1166 return 0; 1167#endif 1168 x = splnet(); 1169 /* save state, in case we fail to send PADI */ 1170 sc->sc_state = PPPOE_STATE_PADI_SENT; 1171 sc->sc_padr_retried = 0; 1172 err = pppoe_send_padi(sc); 1173 if (err != 0 && sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1174 printf("%s: failed to send PADI, error=%d\n", 1175 sc->sc_sppp.pp_if.if_xname, err); 1176 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc); 1177 splx(x); 1178 return err; 1179} 1180 1181/* disconnect */ 1182static int 1183pppoe_disconnect(struct pppoe_softc *sc) 1184{ 1185 int err, x; 1186 1187 x = splnet(); 1188 1189 if (sc->sc_state < PPPOE_STATE_SESSION) 1190 err = EBUSY; 1191 else { 1192 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1193 printf("%s: disconnecting\n", 1194 sc->sc_sppp.pp_if.if_xname); 1195 err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session, (const uint8_t *)&sc->sc_dest); 1196 } 1197 1198 /* cleanup softc */ 1199 sc->sc_state = PPPOE_STATE_INITIAL; 1200 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 1201 if (sc->sc_ac_cookie) { 1202 free(sc->sc_ac_cookie, M_DEVBUF); 1203 sc->sc_ac_cookie = NULL; 1204 } 1205 sc->sc_ac_cookie_len = 0; 1206#ifdef PPPOE_SERVER 1207 if (sc->sc_hunique) { 1208 free(sc->sc_hunique, M_DEVBUF); 1209 sc->sc_hunique = NULL; 1210 } 1211 sc->sc_hunique_len = 0; 1212#endif 1213 sc->sc_session = 0; 1214 1215 /* notify upper layer */ 1216 sc->sc_sppp.pp_down(&sc->sc_sppp); 1217 1218 splx(x); 1219 1220 return err; 1221} 1222 1223/* Connection attempt aborted */ 1224static void 1225pppoe_abort_connect(struct pppoe_softc *sc) 1226{ 1227 printf("%s: could not establish connection\n", 1228 sc->sc_sppp.pp_if.if_xname); 1229 sc->sc_state = PPPOE_STATE_CLOSING; 1230 1231 /* notify upper layer */ 1232 sc->sc_sppp.pp_down(&sc->sc_sppp); 1233 1234 /* clear connection state */ 1235 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 1236 sc->sc_state = PPPOE_STATE_INITIAL; 1237} 1238 1239/* Send a PADR packet */ 1240static int 1241pppoe_send_padr(struct pppoe_softc *sc) 1242{ 1243 struct mbuf *m0; 1244 uint8_t *p; 1245 size_t len, l1 = 0; /* XXX: gcc */ 1246 1247 if (sc->sc_state != PPPOE_STATE_PADR_SENT) 1248 return EIO; 1249 1250 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */ 1251 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */ 1252 l1 = strlen(sc->sc_service_name); 1253 len += l1; 1254 } 1255 if (sc->sc_ac_cookie_len > 0) 1256 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */ 1257 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1258 if (!m0) 1259 return ENOBUFS; 1260 p = mtod(m0, uint8_t *); 1261 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len); 1262 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1263 if (sc->sc_service_name != NULL) { 1264 PPPOE_ADD_16(p, l1); 1265 memcpy(p, sc->sc_service_name, l1); 1266 p += l1; 1267 } else { 1268 PPPOE_ADD_16(p, 0); 1269 } 1270 if (sc->sc_ac_cookie_len > 0) { 1271 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE); 1272 PPPOE_ADD_16(p, sc->sc_ac_cookie_len); 1273 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len); 1274 p += sc->sc_ac_cookie_len; 1275 } 1276 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1277 PPPOE_ADD_16(p, sizeof(sc)); 1278 memcpy(p, &sc, sizeof sc); 1279 1280#ifdef PPPOE_DEBUG 1281 p += sizeof sc; 1282 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN) 1283 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld", 1284 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *))); 1285#endif 1286 1287 return pppoe_output(sc, m0); 1288} 1289 1290/* send a PADT packet */ 1291static int 1292pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest) 1293{ 1294 struct ether_header *eh; 1295 struct sockaddr dst; 1296 struct mbuf *m0; 1297 uint8_t *p; 1298 1299 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN); 1300 if (!m0) 1301 return EIO; 1302 p = mtod(m0, uint8_t *); 1303 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0); 1304 1305 memset(&dst, 0, sizeof dst); 1306 dst.sa_family = AF_UNSPEC; 1307 eh = (struct ether_header*)&dst.sa_data; 1308 eh->ether_type = htons(ETHERTYPE_PPPOEDISC); 1309 memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN); 1310 1311 m0->m_flags &= ~(M_BCAST|M_MCAST); 1312 return outgoing_if->if_output(outgoing_if, m0, &dst, NULL); 1313} 1314 1315#ifdef PPPOE_SERVER 1316static int 1317pppoe_send_pado(struct pppoe_softc *sc) 1318{ 1319 struct mbuf *m0; 1320 uint8_t *p; 1321 size_t len; 1322 1323 if (sc->sc_state != PPPOE_STATE_PADO_SENT) 1324 return EIO; 1325 1326 /* calc length */ 1327 len = 0; 1328 /* include ac_cookie */ 1329 len += 2 + 2 + sizeof(sc); 1330 /* include hunique */ 1331 len += 2 + 2 + sc->sc_hunique_len; 1332 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1333 if (!m0) 1334 return EIO; 1335 p = mtod(m0, uint8_t *); 1336 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len); 1337 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE); 1338 PPPOE_ADD_16(p, sizeof(sc)); 1339 memcpy(p, &sc, sizeof(sc)); 1340 p += sizeof(sc); 1341 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1342 PPPOE_ADD_16(p, sc->sc_hunique_len); 1343 memcpy(p, sc->sc_hunique, sc->sc_hunique_len); 1344 return pppoe_output(sc, m0); 1345} 1346 1347static int 1348pppoe_send_pads(struct pppoe_softc *sc) 1349{ 1350 struct bintime bt; 1351 struct mbuf *m0; 1352 uint8_t *p; 1353 size_t len, l1 = 0; /* XXX: gcc */ 1354 1355 if (sc->sc_state != PPPOE_STATE_PADO_SENT) 1356 return EIO; 1357 1358 getbinuptime(&bt); 1359 sc->sc_session = bt.sec % 0xff + 1; 1360 /* calc length */ 1361 len = 0; 1362 /* include hunique */ 1363 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/ 1364 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */ 1365 l1 = strlen(sc->sc_service_name); 1366 len += l1; 1367 } 1368 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); 1369 if (!m0) 1370 return ENOBUFS; 1371 p = mtod(m0, uint8_t *); 1372 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len); 1373 PPPOE_ADD_16(p, PPPOE_TAG_SNAME); 1374 if (sc->sc_service_name != NULL) { 1375 PPPOE_ADD_16(p, l1); 1376 memcpy(p, sc->sc_service_name, l1); 1377 p += l1; 1378 } else { 1379 PPPOE_ADD_16(p, 0); 1380 } 1381 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE); 1382 PPPOE_ADD_16(p, sc->sc_hunique_len); 1383 memcpy(p, sc->sc_hunique, sc->sc_hunique_len); 1384 return pppoe_output(sc, m0); 1385} 1386#endif 1387 1388static void 1389pppoe_tls(struct sppp *sp) 1390{ 1391 struct pppoe_softc *sc = (void *)sp; 1392 int wtime; 1393 1394 if (sc->sc_state != PPPOE_STATE_INITIAL) 1395 return; 1396 1397 if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH && 1398 sc->sc_sppp.pp_auth_failures > 0) { 1399 /* 1400 * Delay trying to reconnect a bit more - the peer 1401 * might have failed to contact it's radius server. 1402 */ 1403 wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures; 1404 if (wtime > PPPOE_SLOW_RETRY) 1405 wtime = PPPOE_SLOW_RETRY; 1406 } else { 1407 wtime = PPPOE_RECON_IMMEDIATE; 1408 } 1409 callout_reset(&sc->sc_timeout, wtime, pppoe_timeout, sc); 1410} 1411 1412static void 1413pppoe_tlf(struct sppp *sp) 1414{ 1415 struct pppoe_softc *sc = (void *)sp; 1416 if (sc->sc_state < PPPOE_STATE_SESSION) 1417 return; 1418 /* 1419 * Do not call pppoe_disconnect here, the upper layer state 1420 * machine gets confused by this. We must return from this 1421 * function and defer disconnecting to the timeout handler. 1422 */ 1423 sc->sc_state = PPPOE_STATE_CLOSING; 1424 callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc); 1425} 1426 1427static void 1428pppoe_start(struct ifnet *ifp) 1429{ 1430 struct pppoe_softc *sc = (void *)ifp; 1431 struct mbuf *m; 1432 uint8_t *p; 1433 size_t len; 1434 1435 if (sppp_isempty(ifp)) 1436 return; 1437 1438 /* are we ready to process data yet? */ 1439 if (sc->sc_state < PPPOE_STATE_SESSION) { 1440 sppp_flush(&sc->sc_sppp.pp_if); 1441 return; 1442 } 1443 1444 while ((m = sppp_dequeue(ifp)) != NULL) { 1445 len = m->m_pkthdr.len; 1446 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT); 1447 if (m == NULL) { 1448 ifp->if_oerrors++; 1449 continue; 1450 } 1451 p = mtod(m, uint8_t *); 1452 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len); 1453 1454#if NBPFILTER > 0 1455 if(sc->sc_sppp.pp_if.if_bpf) 1456 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m); 1457#endif 1458 1459 pppoe_output(sc, m); 1460 } 1461} 1462 1463 1464#ifdef PFIL_HOOKS 1465static int 1466pppoe_ifattach_hook(void *arg, struct mbuf **mp, struct ifnet *ifp, 1467 int dir) 1468{ 1469 struct pppoe_softc *sc; 1470 int s; 1471 1472 if (mp != (struct mbuf **)PFIL_IFNET_DETACH) 1473 return 0; 1474 1475 s = splnet(); 1476 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) { 1477 if (sc->sc_eth_if != ifp) 1478 continue; 1479 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) { 1480 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1481 printf("%s: ethernet interface detached, going down\n", 1482 sc->sc_sppp.pp_if.if_xname); 1483 } 1484 sc->sc_eth_if = NULL; 1485 pppoe_clear_softc(sc, "ethernet interface detached"); 1486 } 1487 splx(s); 1488 1489 return 0; 1490} 1491#endif 1492 1493static void 1494pppoe_clear_softc(struct pppoe_softc *sc, const char *message) 1495{ 1496 /* stop timer */ 1497 callout_stop(&sc->sc_timeout); 1498 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) 1499 printf("%s: session 0x%x terminated, %s\n", 1500 sc->sc_sppp.pp_if.if_xname, sc->sc_session, message); 1501 1502 /* fix our state */ 1503 sc->sc_state = PPPOE_STATE_INITIAL; 1504 1505 /* signal upper layer */ 1506 sc->sc_sppp.pp_down(&sc->sc_sppp); 1507 1508 /* clean up softc */ 1509 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest)); 1510 if (sc->sc_ac_cookie) { 1511 free(sc->sc_ac_cookie, M_DEVBUF); 1512 sc->sc_ac_cookie = NULL; 1513 } 1514 sc->sc_ac_cookie_len = 0; 1515 sc->sc_session = 0; 1516} 1517