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