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