Home | History | Annotate | Line # | Download | only in net
if_pppoe.c revision 1.157
      1 /* $NetBSD: if_pppoe.c,v 1.157 2020/11/25 10:39:47 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.157 2020/11/25 10:39:47 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 		if (sc->sc_id == t) {
    536 			PPPOE_LOCK(sc, lock);
    537 			break;
    538 		}
    539 	}
    540 	rw_exit(&pppoe_softc_list_lock);
    541 
    542 	if (sc == NULL) {
    543 		pppoe_printf(NULL, "alien host unique tag"
    544 		    ", no session found\n");
    545 		return NULL;
    546 	}
    547 
    548 	/* should be safe to access *sc now */
    549 	if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
    550 		pppoe_printf(sc, "host unique tag found"
    551 		    ", but it belongs to a connection in state %d\n",
    552 		    sc->sc_state);
    553 		PPPOE_UNLOCK(sc);
    554 		return NULL;
    555 	}
    556 	if (sc->sc_eth_if != rcvif) {
    557 		pppoe_printf(sc, "wrong interface, not accepting host unique\n");
    558 		PPPOE_UNLOCK(sc);
    559 		return NULL;
    560 	}
    561 	return sc;
    562 }
    563 
    564 static void
    565 pppoe_softintr_handler(void *dummy)
    566 {
    567 	/* called at splsoftnet() */
    568 	pppoeintr();
    569 }
    570 
    571 /* called at appropriate protection level */
    572 static void
    573 pppoeintr(void)
    574 {
    575 	struct mbuf *m;
    576 	int disc_done, data_done;
    577 
    578 	SOFTNET_LOCK_UNLESS_NET_MPSAFE();
    579 
    580 	do {
    581 		disc_done = 0;
    582 		data_done = 0;
    583 		for (;;) {
    584 			IFQ_LOCK(&ppoediscinq);
    585 			IF_DEQUEUE(&ppoediscinq, m);
    586 			IFQ_UNLOCK(&ppoediscinq);
    587 			if (m == NULL)
    588 				break;
    589 			disc_done = 1;
    590 			pppoe_disc_input(m);
    591 		}
    592 
    593 		for (;;) {
    594 			IFQ_LOCK(&ppoeinq);
    595 			IF_DEQUEUE(&ppoeinq, m);
    596 			IFQ_UNLOCK(&ppoeinq);
    597 			if (m == NULL)
    598 				break;
    599 			data_done = 1;
    600 			pppoe_data_input(m);
    601 		}
    602 	} while (disc_done || data_done);
    603 
    604 	SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();
    605 }
    606 
    607 /* analyze and handle a single received packet while not in session state */
    608 static void
    609 pppoe_dispatch_disc_pkt(struct mbuf *m, int off)
    610 {
    611 	uint16_t tag, len;
    612 	uint16_t session, plen;
    613 	struct pppoe_softc *sc;
    614 	const char *err_msg;
    615 	char *error;
    616 	size_t dlen;
    617 	uint8_t *ac_cookie;
    618 	size_t ac_cookie_len;
    619 	uint8_t *relay_sid;
    620 	size_t relay_sid_len;
    621 	uint8_t *hunique;
    622 	size_t hunique_len;
    623 	struct pppoehdr *ph;
    624 	struct pppoetag *pt;
    625 	struct mbuf *n;
    626 	int noff, err, errortag;
    627 	struct ether_header *eh;
    628 	struct ifnet *rcvif;
    629 	struct psref psref;
    630 
    631 	if (m->m_len < sizeof(*eh)) {
    632 		m = m_pullup(m, sizeof(*eh));
    633 		if (m == NULL)
    634 			goto done;
    635 	}
    636 	eh = mtod(m, struct ether_header *);
    637 	off += sizeof(*eh);
    638 
    639 	if (m->m_pkthdr.len - off <= PPPOE_HEADERLEN) {
    640 		goto done;
    641 	}
    642 
    643 	M_REGION_GET(ph, struct pppoehdr *, m, off, sizeof(*ph));
    644 	if (ph == NULL) {
    645 		goto done;
    646 	}
    647 	if (ph->vertype != PPPOE_VERTYPE) {
    648 		goto done;
    649 	}
    650 
    651 	ac_cookie = NULL;
    652 	ac_cookie_len = 0;
    653 	relay_sid = NULL;
    654 	relay_sid_len = 0;
    655 	hunique = NULL;
    656 	hunique_len = 0;
    657 
    658 	session = ntohs(ph->session);
    659 	plen = ntohs(ph->plen);
    660 	off += sizeof(*ph);
    661 
    662 	if (plen + off > m->m_pkthdr.len) {
    663 		goto done;
    664 	}
    665 	m_adj(m, off + plen - m->m_pkthdr.len);	/* ignore trailing garbage */
    666 
    667 	tag = 0;
    668 	len = 0;
    669 	sc = NULL;
    670 	err_msg = NULL;
    671 	errortag = 0;
    672 	while (off + sizeof(*pt) <= m->m_pkthdr.len) {
    673 		M_REGION_GET(pt, struct pppoetag *, m, off, sizeof(*pt));
    674 		if (pt == NULL) {
    675 			goto done;
    676 		}
    677 
    678 		tag = ntohs(pt->tag);
    679 		len = ntohs(pt->len);
    680 		if (off + len + sizeof(*pt) > m->m_pkthdr.len) {
    681 			goto done;
    682 		}
    683 		switch (tag) {
    684 		case PPPOE_TAG_EOL:
    685 			goto breakbreak;
    686 		case PPPOE_TAG_SNAME:
    687 			break;	/* ignored */
    688 		case PPPOE_TAG_ACNAME:
    689 			if (len > 0) {
    690 				dlen = 4 * len + 1;
    691 				error = malloc(dlen, M_TEMP, M_NOWAIT);
    692 				if (error == NULL)
    693 					break;
    694 
    695 				n = m_pulldown(m, off + sizeof(*pt), len,
    696 				    &noff);
    697 				if (!n) {
    698 					m = NULL;
    699 					free(error, M_TEMP);
    700 					goto done;
    701 				}
    702 
    703 				strnvisx(error, dlen,
    704 				    mtod(n, char*) + noff, len,
    705 				    VIS_SAFE | VIS_OCTAL);
    706 				pppoe_printf(NULL, "connected to %s\n", error);
    707 				free(error, M_TEMP);
    708 			}
    709 			break;	/* ignored */
    710 		case PPPOE_TAG_HUNIQUE:
    711 			if (hunique == NULL) {
    712 				n = m_pulldown(m, off + sizeof(*pt), len,
    713 				    &noff);
    714 				if (!n) {
    715 					m = NULL;
    716 					err_msg = "TAG HUNIQUE ERROR";
    717 					break;
    718 				}
    719 
    720 				hunique = mtod(n, uint8_t *) + noff;
    721 				hunique_len = len;
    722 			}
    723 			break;
    724 		case PPPOE_TAG_ACCOOKIE:
    725 			if (ac_cookie == NULL) {
    726 				n = m_pulldown(m, off + sizeof(*pt), len,
    727 				    &noff);
    728 				if (!n) {
    729 					err_msg = "TAG ACCOOKIE ERROR";
    730 					m = NULL;
    731 					break;
    732 				}
    733 				ac_cookie = mtod(n, char *) + noff;
    734 				ac_cookie_len = len;
    735 			}
    736 			break;
    737 		case PPPOE_TAG_RELAYSID:
    738 			if (relay_sid == NULL) {
    739 				n = m_pulldown(m, off + sizeof(*pt), len,
    740 				    &noff);
    741 				if (!n) {
    742 					err_msg = "TAG RELAYSID ERROR";
    743 					m = NULL;
    744 					break;
    745 				}
    746 				relay_sid = mtod(n, char *) + noff;
    747 				relay_sid_len = len;
    748 			}
    749 			break;
    750 		case PPPOE_TAG_SNAME_ERR:
    751 			err_msg = "SERVICE NAME ERROR";
    752 			errortag = 1;
    753 			break;
    754 		case PPPOE_TAG_ACSYS_ERR:
    755 			err_msg = "AC SYSTEM ERROR";
    756 			errortag = 1;
    757 			break;
    758 		case PPPOE_TAG_GENERIC_ERR:
    759 			err_msg = "GENERIC ERROR";
    760 			errortag = 1;
    761 			break;
    762 		}
    763 		if (err_msg) {
    764 			error = NULL;
    765 			if (errortag && len) {
    766 				dlen = 4 * len + 1;
    767 				error = malloc(dlen, M_TEMP,
    768 				    M_NOWAIT|M_ZERO);
    769 				n = m_pulldown(m, off + sizeof(*pt), len,
    770 				    &noff);
    771 				if (!n) {
    772 					m = NULL;
    773 				} else if (error) {
    774 					strnvisx(error, dlen,
    775 					    mtod(n, char*) + noff, len,
    776 					    VIS_SAFE | VIS_OCTAL);
    777 				}
    778 			}
    779 			if (error) {
    780 				pppoe_printf(NULL, "%s: %s\n", err_msg, error);
    781 				free(error, M_TEMP);
    782 			} else
    783 				pppoe_printf(NULL, "%s\n", err_msg);
    784 			if (errortag || m == NULL)
    785 				goto done;
    786 		}
    787 		off += sizeof(*pt) + len;
    788 	}
    789 breakbreak:;
    790 
    791 	switch (ph->code) {
    792 	case PPPOE_CODE_PADI:
    793 #ifdef PPPOE_SERVER
    794 		/*
    795 		 * got service name, concentrator name, and/or host unique.
    796 		 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
    797 		 */
    798 		rw_enter(&pppoe_softc_list_lock, RW_READER);
    799 		if (LIST_EMPTY(&pppoe_softc_list)) {
    800 			rw_exit(&pppoe_softc_list_lock);
    801 			goto done;
    802 		}
    803 
    804 		LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
    805 			PPPOE_LOCK(sc, RW_WRITER);
    806 			if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
    807 				PPPOE_UNLOCK(sc);
    808 				continue;
    809 			}
    810 			if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
    811 				PPPOE_UNLOCK(sc);
    812 				continue;
    813 			}
    814 
    815 			if (sc->sc_state == PPPOE_STATE_INITIAL)
    816 				break;
    817 
    818 			PPPOE_UNLOCK(sc);
    819 		}
    820 		rw_exit(&pppoe_softc_list_lock);
    821 
    822 		if (sc == NULL) {
    823 			goto done;
    824 		}
    825 
    826 		if (hunique) {
    827 			if (sc->sc_hunique)
    828 				free(sc->sc_hunique, M_DEVBUF);
    829 			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
    830 			    M_DONTWAIT);
    831 			if (sc->sc_hunique == NULL) {
    832 				PPPOE_UNLOCK(sc);
    833 				goto done;
    834 			}
    835 			sc->sc_hunique_len = hunique_len;
    836 			memcpy(sc->sc_hunique, hunique, hunique_len);
    837 		}
    838 		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
    839 		sc->sc_state = PPPOE_STATE_PADO_SENT;
    840 		pppoe_send_pado(sc);
    841 		PPPOE_UNLOCK(sc);
    842 		break;
    843 #endif /* PPPOE_SERVER */
    844 
    845 	case PPPOE_CODE_PADR:
    846 #ifdef PPPOE_SERVER
    847 		/*
    848 		 * get sc from ac_cookie if IFF_PASSIVE
    849 		 */
    850 		if (ac_cookie == NULL) {
    851 			goto done;
    852 		}
    853 
    854 		rcvif = m_get_rcvif_psref(m, &psref);
    855 		if (__predict_true(rcvif != NULL)) {
    856 			sc = pppoe_find_softc_by_hunique(ac_cookie,
    857 			    ac_cookie_len, rcvif, RW_WRITER);
    858 		}
    859 		m_put_rcvif_psref(rcvif, &psref);
    860 		if (sc == NULL) {
    861 			/* be quiet if there is not a single pppoe instance */
    862 			rw_enter(&pppoe_softc_list_lock, RW_READER);
    863 			if (!LIST_EMPTY(&pppoe_softc_list)) {
    864 				pppoe_printf(NULL, "received PADR"
    865 				    " but could not find request for it\n");
    866 			}
    867 			rw_exit(&pppoe_softc_list_lock);
    868 			goto done;
    869 		}
    870 
    871 		if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
    872 			pppoe_printf(sc, "received unexpected PADR\n");
    873 			PPPOE_UNLOCK(sc);
    874 			goto done;
    875 		}
    876 
    877 		if (hunique) {
    878 			if (sc->sc_hunique)
    879 				free(sc->sc_hunique, M_DEVBUF);
    880 			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
    881 			    M_DONTWAIT);
    882 			if (sc->sc_hunique == NULL) {
    883 				PPPOE_UNLOCK(sc);
    884 				goto done;
    885 			}
    886 			sc->sc_hunique_len = hunique_len;
    887 			memcpy(sc->sc_hunique, hunique, hunique_len);
    888 		}
    889 		pppoe_send_pads(sc);
    890 		sc->sc_state = PPPOE_STATE_SESSION;
    891 		PPPOE_UNLOCK(sc);
    892 
    893 		sc->sc_sppp.pp_up(&sc->sc_sppp);
    894 		break;
    895 #else
    896 		/* ignore, we are no access concentrator */
    897 		goto done;
    898 #endif /* PPPOE_SERVER */
    899 
    900 	case PPPOE_CODE_PADO:
    901 		rcvif = m_get_rcvif_psref(m, &psref);
    902 		if (__predict_false(rcvif == NULL))
    903 			goto done;
    904 
    905 		if (hunique != NULL) {
    906 			sc = pppoe_find_softc_by_hunique(hunique,
    907 			    hunique_len, rcvif, RW_WRITER);
    908 		}
    909 
    910 		m_put_rcvif_psref(rcvif, &psref);
    911 
    912 		if (sc == NULL) {
    913 			/* be quiet if there is not a single pppoe instance */
    914 			rw_enter(&pppoe_softc_list_lock, RW_READER);
    915 			if (!LIST_EMPTY(&pppoe_softc_list)) {
    916 				pppoe_printf(NULL, "received PADO"
    917 				    " but could not find request for it\n");
    918 			}
    919 			rw_exit(&pppoe_softc_list_lock);
    920 			goto done;
    921 		}
    922 
    923 		if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
    924 			pppoe_printf(sc, "received unexpected PADO\n");
    925 			PPPOE_UNLOCK(sc);
    926 			goto done;
    927 		}
    928 
    929 		if (ac_cookie) {
    930 			if (sc->sc_ac_cookie)
    931 				free(sc->sc_ac_cookie, M_DEVBUF);
    932 			sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
    933 			    M_DONTWAIT);
    934 			if (sc->sc_ac_cookie == NULL) {
    935 				pppoe_printf(sc, "FATAL: could not allocate memory "
    936 				    "for AC cookie\n");
    937 				PPPOE_UNLOCK(sc);
    938 				goto done;
    939 			}
    940 			sc->sc_ac_cookie_len = ac_cookie_len;
    941 			memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
    942 		}
    943 		if (relay_sid) {
    944 			if (sc->sc_relay_sid)
    945 				free(sc->sc_relay_sid, M_DEVBUF);
    946 			sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF,
    947 			    M_DONTWAIT);
    948 			if (sc->sc_relay_sid == NULL) {
    949 				pppoe_printf(sc, "FATAL: could not allocate memory "
    950 				    "for relay SID\n");
    951 				PPPOE_UNLOCK(sc);
    952 				goto done;
    953 			}
    954 			sc->sc_relay_sid_len = relay_sid_len;
    955 			memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len);
    956 		}
    957 		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
    958 		callout_stop(&sc->sc_timeout);
    959 		sc->sc_padr_retried = 0;
    960 		sc->sc_state = PPPOE_STATE_PADR_SENT;
    961 		if ((err = pppoe_send_padr(sc)) != 0) {
    962 			pppoe_printf(sc,
    963 			    "failed to send PADR, error=%d\n", err);
    964 		}
    965 		callout_schedule(&sc->sc_timeout,
    966 		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));
    967 
    968 		PPPOE_UNLOCK(sc);
    969 		break;
    970 
    971 	case PPPOE_CODE_PADS:
    972 		rcvif = m_get_rcvif_psref(m, &psref);
    973 		if (__predict_false(rcvif == NULL))
    974 			goto done;
    975 
    976 		if (hunique != NULL) {
    977 			sc = pppoe_find_softc_by_hunique(hunique,
    978 			    hunique_len, rcvif, RW_WRITER);
    979 		}
    980 
    981 		m_put_rcvif_psref(rcvif, &psref);
    982 
    983 		if (sc == NULL)
    984 			goto done;
    985 
    986 		sc->sc_session = session;
    987 		callout_stop(&sc->sc_timeout);
    988 		pppoe_printf(sc, "session 0x%x connected\n", session);
    989 		sc->sc_state = PPPOE_STATE_SESSION;
    990 		PPPOE_UNLOCK(sc);
    991 
    992 		sc->sc_sppp.pp_up(&sc->sc_sppp);	/* notify upper layers */
    993 		break;
    994 
    995 	case PPPOE_CODE_PADT:
    996 		rcvif = m_get_rcvif_psref(m, &psref);
    997 		if (__predict_false(rcvif == NULL))
    998 			goto done;
    999 
   1000 		sc = pppoe_find_softc_by_session(session, rcvif,
   1001 		    RW_WRITER);
   1002 
   1003 		m_put_rcvif_psref(rcvif, &psref);
   1004 
   1005 		if (sc == NULL)
   1006 			goto done;
   1007 
   1008 		pppoe_clear_softc(sc, "received PADT");
   1009 		PPPOE_UNLOCK(sc);
   1010 		break;
   1011 
   1012 	default:
   1013 		rcvif = m_get_rcvif_psref(m, &psref);
   1014 		if (__predict_false(rcvif == NULL))
   1015 			goto done;
   1016 
   1017 		if (hunique != NULL) {
   1018 			sc = pppoe_find_softc_by_hunique(hunique,
   1019 			    hunique_len, rcvif, RW_READER);
   1020 		}
   1021 
   1022 		m_put_rcvif_psref(rcvif, &psref);
   1023 
   1024 		pppoe_printf(sc, "unknown code (0x%04x) session = 0x%04x\n",
   1025 		    ph->code, session);
   1026 		if (sc == NULL)
   1027 			goto done;
   1028 		PPPOE_UNLOCK(sc);
   1029 		break;
   1030 	}
   1031 
   1032 done:
   1033 	if (m)
   1034 		m_freem(m);
   1035 	return;
   1036 }
   1037 
   1038 static void
   1039 pppoe_disc_input(struct mbuf *m)
   1040 {
   1041 	KASSERT(m->m_flags & M_PKTHDR);
   1042 
   1043 	/*
   1044 	 * Avoid error messages if there is not a single PPPoE instance.
   1045 	 */
   1046 	rw_enter(&pppoe_softc_list_lock, RW_READER);
   1047 	if (!LIST_EMPTY(&pppoe_softc_list)) {
   1048 		rw_exit(&pppoe_softc_list_lock);
   1049 		pppoe_dispatch_disc_pkt(m, 0);
   1050 	} else {
   1051 		rw_exit(&pppoe_softc_list_lock);
   1052 		m_freem(m);
   1053 	}
   1054 }
   1055 
   1056 static bool
   1057 pppoe_is_my_frame(uint8_t *dhost, struct ifnet *rcvif)
   1058 {
   1059 
   1060 	if (memcmp(CLLADDR(rcvif->if_sadl), dhost, ETHER_ADDR_LEN) == 0)
   1061 		return true;
   1062 
   1063 	return false;
   1064 }
   1065 
   1066 static void
   1067 pppoe_data_input(struct mbuf *m)
   1068 {
   1069 	uint16_t session, plen;
   1070 	struct pppoe_softc *sc;
   1071 	struct pppoehdr *ph;
   1072 	struct ifnet *rcvif;
   1073 	struct psref psref;
   1074 	uint8_t shost[ETHER_ADDR_LEN];
   1075 	uint8_t dhost[ETHER_ADDR_LEN];
   1076 	bool term_unknown = pppoe_term_unknown;
   1077 
   1078 	KASSERT(m->m_flags & M_PKTHDR);
   1079 
   1080 	/*
   1081 	 * Avoid error messages if there is not a single PPPoE instance.
   1082 	 */
   1083 	rw_enter(&pppoe_softc_list_lock, RW_READER);
   1084 	if (LIST_EMPTY(&pppoe_softc_list)) {
   1085 		rw_exit(&pppoe_softc_list_lock);
   1086 		goto drop;
   1087 	}
   1088 	rw_exit(&pppoe_softc_list_lock);
   1089 
   1090 	if (term_unknown) {
   1091 		memcpy(shost, mtod(m, struct ether_header*)->ether_shost,
   1092 		    ETHER_ADDR_LEN);
   1093 		memcpy(dhost, mtod(m, struct ether_header*)->ether_dhost,
   1094 		    ETHER_ADDR_LEN);
   1095 	}
   1096 	m_adj(m, sizeof(struct ether_header));
   1097 	if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
   1098 		goto drop;
   1099 	}
   1100 
   1101 	if (m->m_len < sizeof(*ph)) {
   1102 		m = m_pullup(m, sizeof(*ph));
   1103 		if (m == NULL) {
   1104 			return;
   1105 		}
   1106 	}
   1107 	ph = mtod(m, struct pppoehdr *);
   1108 
   1109 	if (ph->vertype != PPPOE_VERTYPE) {
   1110 		goto drop;
   1111 	}
   1112 	if (ph->code != 0) {
   1113 		goto drop;
   1114 	}
   1115 
   1116 	session = ntohs(ph->session);
   1117 	rcvif = m_get_rcvif_psref(m, &psref);
   1118 	if (__predict_false(rcvif == NULL))
   1119 		goto drop;
   1120 	sc = pppoe_find_softc_by_session(session, rcvif, RW_READER);
   1121 	if (sc == NULL) {
   1122 		if (term_unknown) {
   1123 			static struct timeval lasttime = {0, 0};
   1124 			static int curpps = 0;
   1125 			/*
   1126 			 * avoid to send wrong PADT which is response from
   1127 			 * session stage packets for other hosts when parent
   1128 			 * ethernet is promiscuous mode.
   1129 			 */
   1130 			if (pppoe_is_my_frame(dhost, rcvif) &&
   1131 			    ppsratecheck(&lasttime, &curpps,
   1132 				pppoe_term_unknown_pps)) {
   1133 				pppoe_printf(NULL, "input for unknown session %#x, "
   1134 				    "sending PADT\n", session);
   1135 				pppoe_send_padt(rcvif, session, shost);
   1136 			}
   1137 		}
   1138 		m_put_rcvif_psref(rcvif, &psref);
   1139 		goto drop;
   1140 	}
   1141 
   1142 	m_put_rcvif_psref(rcvif, &psref);
   1143 
   1144 	plen = ntohs(ph->plen);
   1145 
   1146 	bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_IN);
   1147 
   1148 	m_adj(m, PPPOE_HEADERLEN);
   1149 
   1150 #ifdef PPPOE_DEBUG
   1151 	{
   1152 		struct mbuf *p;
   1153 
   1154 		printf("%s: pkthdr.len=%d, pppoe.len=%d",
   1155 		    sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen);
   1156 		p = m;
   1157 		while (p) {
   1158 			printf(" l=%d", p->m_len);
   1159 			p = p->m_next;
   1160 		}
   1161 		printf("\n");
   1162 	}
   1163 #endif
   1164 	PPPOE_UNLOCK(sc);
   1165 
   1166 	if (m->m_pkthdr.len < plen)
   1167 		goto drop;
   1168 
   1169 	/* ignore trailing garbage */
   1170 	m_adj(m, plen - m->m_pkthdr.len);
   1171 	/*
   1172 	 * Fix incoming interface pointer (not the raw ethernet interface
   1173 	 * anymore)
   1174 	 */
   1175 	m_set_rcvif(m, &sc->sc_sppp.pp_if);
   1176 
   1177 	/* pass packet up and account for it */
   1178 	if_statinc(&sc->sc_sppp.pp_if, if_ipackets);
   1179 	sppp_input(&sc->sc_sppp.pp_if, m);
   1180 	return;
   1181 
   1182 drop:
   1183 	m_freem(m);
   1184 }
   1185 
   1186 static int
   1187 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
   1188 {
   1189 	struct sockaddr dst;
   1190 	struct ether_header *eh;
   1191 	uint16_t etype;
   1192 
   1193 	if (sc->sc_eth_if == NULL) {
   1194 		m_freem(m);
   1195 		return EIO;
   1196 	}
   1197 
   1198 	memset(&dst, 0, sizeof dst);
   1199 	dst.sa_family = AF_UNSPEC;
   1200 	eh = (struct ether_header*)&dst.sa_data;
   1201 	etype = sc->sc_state == PPPOE_STATE_SESSION
   1202 	    ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
   1203 	eh->ether_type = htons(etype);
   1204 	memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
   1205 
   1206 	DPRINTF(sc, "(%x) state=%d, session=0x%x output -> %s, len=%d\n",
   1207 	    etype, sc->sc_state, sc->sc_session,
   1208 	    ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
   1209 
   1210 	m->m_flags &= ~(M_BCAST|M_MCAST);
   1211 	if_statinc(&sc->sc_sppp.pp_if, if_opackets);
   1212 	return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL);
   1213 }
   1214 
   1215 static int
   1216 pppoe_parm_cpyinstr(struct pppoe_softc *sc,
   1217     char **dst, const void *src, size_t len)
   1218 {
   1219 	int error = 0;
   1220 	char *next = NULL;
   1221 	size_t bufsiz, cpysiz, strsiz;
   1222 
   1223 	bufsiz = len + 1;
   1224 
   1225 	if (src == NULL)
   1226 		goto out;
   1227 
   1228 	bufsiz = len + 1;
   1229 	next = malloc(bufsiz, M_DEVBUF, M_WAITOK);
   1230 	if (next == NULL)
   1231 		return ENOMEM;
   1232 
   1233 	error = copyinstr(src, next, bufsiz, &cpysiz);
   1234 	if (error != 0)
   1235 		goto fail;
   1236 	if (cpysiz != bufsiz) {
   1237 		error = EINVAL;
   1238 		goto fail;
   1239 	}
   1240 
   1241 	strsiz = strnlen(next, bufsiz);
   1242 	if (strsiz == bufsiz) {
   1243 		error = EINVAL;
   1244 		goto fail;
   1245 	}
   1246 
   1247 out:
   1248 	PPPOE_LOCK(sc, RW_WRITER);
   1249 	if (*dst != NULL)
   1250 		free(*dst, M_DEVBUF);
   1251 	*dst = next;
   1252 	next = NULL;
   1253 	PPPOE_UNLOCK(sc);
   1254 fail:
   1255 	if (next != NULL)
   1256 		free(next, M_DEVBUF);
   1257 
   1258 	return error;
   1259 }
   1260 
   1261 static int
   1262 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   1263 {
   1264 	struct lwp *l = curlwp;	/* XXX */
   1265 	struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
   1266 	struct ifreq *ifr = data;
   1267 	int error = 0;
   1268 
   1269 	switch (cmd) {
   1270 	case PPPOESETPARMS:
   1271 	{
   1272 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
   1273 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
   1274 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1275 		    NULL) != 0)
   1276 			return EPERM;
   1277 		if (parms->eth_ifname[0] != 0) {
   1278 			struct ifnet *eth_if;
   1279 
   1280 			PPPOE_LOCK(sc, RW_WRITER);
   1281 			eth_if = ifunit(parms->eth_ifname);
   1282 			if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
   1283 				sc->sc_eth_if = NULL;
   1284 				PPPOE_UNLOCK(sc);
   1285 				return ENXIO;
   1286 			}
   1287 
   1288 			if (sc->sc_sppp.pp_if.if_mtu !=
   1289 			    eth_if->if_mtu - PPPOE_OVERHEAD) {
   1290 				sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
   1291 				    PPPOE_OVERHEAD;
   1292 			}
   1293 			sc->sc_eth_if = eth_if;
   1294 			PPPOE_UNLOCK(sc);
   1295 		}
   1296 
   1297 		error = pppoe_parm_cpyinstr(sc, &sc->sc_concentrator_name,
   1298 		    parms->ac_name, parms->ac_name_len);
   1299 		if (error != 0)
   1300 			return error;
   1301 
   1302 		error = pppoe_parm_cpyinstr(sc, &sc->sc_service_name,
   1303 		    parms->service_name, parms->service_name_len);
   1304 		if (error != 0)
   1305 			return error;
   1306 		return 0;
   1307 	}
   1308 	break;
   1309 	case PPPOEGETPARMS:
   1310 	{
   1311 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
   1312 		memset(parms, 0, sizeof *parms);
   1313 		PPPOE_LOCK(sc, RW_READER);
   1314 		if (sc->sc_eth_if)
   1315 			strlcpy(parms->ifname, sc->sc_eth_if->if_xname,
   1316 			    sizeof(parms->ifname));
   1317 		PPPOE_UNLOCK(sc);
   1318 		return 0;
   1319 	}
   1320 	break;
   1321 	case PPPOEGETSESSION:
   1322 	{
   1323 		struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
   1324 		PPPOE_LOCK(sc, RW_READER);
   1325 		state->state = sc->sc_state;
   1326 		state->session_id = sc->sc_session;
   1327 		state->padi_retry_no = sc->sc_padi_retried;
   1328 		state->padr_retry_no = sc->sc_padr_retried;
   1329 		PPPOE_UNLOCK(sc);
   1330 		return 0;
   1331 	}
   1332 	break;
   1333 	case SIOCSIFFLAGS:
   1334 		/*
   1335 		 * Prevent running re-establishment timers overriding
   1336 		 * administrators choice.
   1337 		 */
   1338 		PPPOE_LOCK(sc, RW_WRITER);
   1339 
   1340 		if ((ifr->ifr_flags & IFF_UP) == 0
   1341 		     && sc->sc_state < PPPOE_STATE_SESSION) {
   1342 			callout_stop(&sc->sc_timeout);
   1343 			sc->sc_state = PPPOE_STATE_INITIAL;
   1344 			sc->sc_padi_retried = 0;
   1345 			sc->sc_padr_retried = 0;
   1346 			memcpy(&sc->sc_dest, etherbroadcastaddr,
   1347 			    sizeof(sc->sc_dest));
   1348 		}
   1349 
   1350 		PPPOE_UNLOCK(sc);
   1351 
   1352 		error = sppp_ioctl(ifp, cmd, data);
   1353 
   1354 		return error;
   1355 	case SIOCSIFMTU:
   1356 		if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
   1357 		    PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
   1358 			return EINVAL;
   1359 		}
   1360 		/*FALLTHROUGH*/
   1361 	default:
   1362 		return sppp_ioctl(ifp, cmd, data);
   1363 	}
   1364 	return 0;
   1365 }
   1366 
   1367 /*
   1368  * Allocate a mbuf/cluster with space to store the given data length
   1369  * of payload, leaving space for prepending an ethernet header
   1370  * in front.
   1371  */
   1372 static struct mbuf *
   1373 pppoe_get_mbuf(size_t len)
   1374 {
   1375 	struct mbuf *m;
   1376 
   1377 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1378 	if (m == NULL)
   1379 		return NULL;
   1380 	if (len + sizeof(struct ether_header) > MHLEN) {
   1381 		MCLGET(m, M_DONTWAIT);
   1382 		if ((m->m_flags & M_EXT) == 0) {
   1383 			m_free(m);
   1384 			return NULL;
   1385 		}
   1386 	}
   1387 	m->m_data += sizeof(struct ether_header);
   1388 	m->m_len = len;
   1389 	m->m_pkthdr.len = len;
   1390 	m_reset_rcvif(m);
   1391 
   1392 	return m;
   1393 }
   1394 
   1395 static int
   1396 pppoe_send_padi(struct pppoe_softc *sc)
   1397 {
   1398 	struct mbuf *m0;
   1399 	int len, l1 = 0, l2 = 0;
   1400 	uint8_t *p;
   1401 
   1402 	if (sc->sc_state > PPPOE_STATE_PADI_SENT)
   1403 		panic("pppoe_send_padi in state %d", sc->sc_state);
   1404 
   1405 	/* Compute packet length. */
   1406 	len = sizeof(struct pppoetag);
   1407 	if (sc->sc_service_name != NULL) {
   1408 		l1 = strlen(sc->sc_service_name);
   1409 		len += l1;
   1410 	}
   1411 	if (sc->sc_concentrator_name != NULL) {
   1412 		l2 = strlen(sc->sc_concentrator_name);
   1413 		len += sizeof(struct pppoetag) + l2;
   1414 	}
   1415 	len += sizeof(struct pppoetag) + sizeof(sc->sc_id);
   1416 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1417 		len += sizeof(struct pppoetag) + 2;
   1418 	}
   1419 
   1420 	/* Allocate packet. */
   1421 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1422 	if (m0 == NULL)
   1423 		return ENOBUFS;
   1424 
   1425 	/* Fill in packet. */
   1426 	p = mtod(m0, uint8_t *);
   1427 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
   1428 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1429 	if (sc->sc_service_name != NULL) {
   1430 		PPPOE_ADD_16(p, l1);
   1431 		memcpy(p, sc->sc_service_name, l1);
   1432 		p += l1;
   1433 	} else {
   1434 		PPPOE_ADD_16(p, 0);
   1435 	}
   1436 	if (sc->sc_concentrator_name != NULL) {
   1437 		PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
   1438 		PPPOE_ADD_16(p, l2);
   1439 		memcpy(p, sc->sc_concentrator_name, l2);
   1440 		p += l2;
   1441 	}
   1442 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1443 	PPPOE_ADD_16(p, sizeof(sc->sc_id));
   1444 	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
   1445 	p += sizeof(sc->sc_id);
   1446 
   1447 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1448 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
   1449 		PPPOE_ADD_16(p, 2);
   1450 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
   1451 	}
   1452 
   1453 #ifdef PPPOE_DEBUG
   1454 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
   1455 		panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
   1456 		    (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
   1457 #endif
   1458 
   1459 	/* Send packet. */
   1460 	return pppoe_output(sc, m0);
   1461 }
   1462 
   1463 static void
   1464 pppoe_timeout_co(void *arg)
   1465 {
   1466 	struct pppoe_softc *sc = (struct pppoe_softc *)arg;
   1467 
   1468 	if (atomic_swap_uint(&sc->sc_timeout_scheduled, 1) != 0)
   1469 		return;
   1470 
   1471 	workqueue_enqueue(sc->sc_timeout_wq, &sc->sc_timeout_wk, NULL);
   1472 }
   1473 
   1474 static void
   1475 pppoe_timeout_co_halt(void *unused __unused)
   1476 {
   1477 
   1478 	/* do nothing to halt callout safely */
   1479 }
   1480 
   1481 static void
   1482 pppoe_timeout_wk(struct work *wk __unused, void *arg)
   1483 {
   1484 	struct pppoe_softc *sc = (struct pppoe_softc *)arg;
   1485 
   1486 	atomic_swap_uint(&sc->sc_timeout_scheduled, 0);
   1487 	pppoe_timeout(sc);
   1488 }
   1489 
   1490 static void
   1491 pppoe_timeout(struct pppoe_softc *sc)
   1492 {
   1493 	int retry_wait, err;
   1494 	DECLARE_SPLNET_VARIABLE;
   1495 
   1496 	pppoe_printf(sc, "timeout\n");
   1497 
   1498 	PPPOE_LOCK(sc, RW_WRITER);
   1499 	switch (sc->sc_state) {
   1500 	case PPPOE_STATE_INITIAL:
   1501 		/* delayed connect from pppoe_tls() */
   1502 		pppoe_connect(sc);
   1503 		break;
   1504 	case PPPOE_STATE_PADI_SENT:
   1505 		/*
   1506 		 * We have two basic ways of retrying:
   1507 		 *  - Quick retry mode: try a few times in short sequence
   1508 		 *  - Slow retry mode: we already had a connection successfully
   1509 		 *    established and will try infinitely (without user
   1510 		 *    intervention)
   1511 		 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
   1512 		 * is not set.
   1513 		 */
   1514 
   1515 		/* initialize for quick retry mode */
   1516 		retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
   1517 
   1518 		ACQUIRE_SPLNET();
   1519 		sc->sc_padi_retried++;
   1520 		if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
   1521 			if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
   1522 				/* slow retry mode */
   1523 				retry_wait = PPPOE_SLOW_RETRY;
   1524 			} else {
   1525 				pppoe_abort_connect(sc);
   1526 				RELEASE_SPLNET();
   1527 				PPPOE_UNLOCK(sc);
   1528 				return;
   1529 			}
   1530 		}
   1531 		if ((err = pppoe_send_padi(sc)) != 0) {
   1532 			sc->sc_padi_retried--;
   1533 			pppoe_printf(sc,
   1534 			    "failed to transmit PADI, error=%d\n", err);
   1535 		}
   1536 		callout_schedule(&sc->sc_timeout,retry_wait);
   1537 		RELEASE_SPLNET();
   1538 		break;
   1539 
   1540 	case PPPOE_STATE_PADR_SENT:
   1541 		ACQUIRE_SPLNET();
   1542 		sc->sc_padr_retried++;
   1543 		if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
   1544 			memcpy(&sc->sc_dest, etherbroadcastaddr,
   1545 			    sizeof(sc->sc_dest));
   1546 			sc->sc_state = PPPOE_STATE_PADI_SENT;
   1547 			sc->sc_padr_retried = 0;
   1548 			if ((err = pppoe_send_padi(sc)) != 0) {
   1549 				pppoe_printf(sc,
   1550 				    "failed to send PADI, error=%d\n", err);
   1551 			}
   1552 			callout_schedule(&sc->sc_timeout,
   1553 			    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried));
   1554 			RELEASE_SPLNET();
   1555 			PPPOE_UNLOCK(sc);
   1556 			return;
   1557 		}
   1558 		if ((err = pppoe_send_padr(sc)) != 0) {
   1559 			sc->sc_padr_retried--;
   1560 			pppoe_printf(sc,"failed to send PADR, error=%d", err);
   1561 		}
   1562 		callout_schedule(&sc->sc_timeout,
   1563 		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));
   1564 		RELEASE_SPLNET();
   1565 		break;
   1566 	case PPPOE_STATE_CLOSING:
   1567 		pppoe_disconnect(sc);
   1568 		break;
   1569 	default:
   1570 		PPPOE_UNLOCK(sc);
   1571 		return;	/* all done, work in peace */
   1572 	}
   1573 	PPPOE_UNLOCK(sc);
   1574 }
   1575 
   1576 /* Start a connection (i.e. initiate discovery phase) */
   1577 static int
   1578 pppoe_connect(struct pppoe_softc *sc)
   1579 {
   1580 	int err;
   1581 	DECLARE_SPLNET_VARIABLE;
   1582 
   1583 	KASSERT(PPPOE_WLOCKED(sc));
   1584 
   1585 	if (sc->sc_state != PPPOE_STATE_INITIAL)
   1586 		return EBUSY;
   1587 
   1588 #ifdef PPPOE_SERVER
   1589 	/* wait PADI if IFF_PASSIVE */
   1590 	if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
   1591 		return 0;
   1592 #endif
   1593 	ACQUIRE_SPLNET();
   1594 	/* save state, in case we fail to send PADI */
   1595 	sc->sc_state = PPPOE_STATE_PADI_SENT;
   1596 	sc->sc_padr_retried = 0;
   1597 	err = pppoe_send_padi(sc);
   1598 	if (err != 0)
   1599 		pppoe_printf(sc, "failed to send PADI, error=%d\n", err);
   1600 	callout_schedule(&sc->sc_timeout, PPPOE_DISC_TIMEOUT);
   1601 	RELEASE_SPLNET();
   1602 	return err;
   1603 }
   1604 
   1605 /* disconnect */
   1606 static int
   1607 pppoe_disconnect(struct pppoe_softc *sc)
   1608 {
   1609 	int err;
   1610 	DECLARE_SPLNET_VARIABLE;
   1611 
   1612 	KASSERT(PPPOE_WLOCKED(sc));
   1613 
   1614 	ACQUIRE_SPLNET();
   1615 
   1616 	if (sc->sc_state < PPPOE_STATE_SESSION)
   1617 		err = EBUSY;
   1618 	else {
   1619 		pppoe_printf(sc, "disconnecting\n");
   1620 		err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session,
   1621 		    (const uint8_t *)&sc->sc_dest);
   1622 	}
   1623 
   1624 	/* cleanup softc */
   1625 	sc->sc_state = PPPOE_STATE_INITIAL;
   1626 
   1627 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   1628 	if (sc->sc_ac_cookie) {
   1629 		free(sc->sc_ac_cookie, M_DEVBUF);
   1630 		sc->sc_ac_cookie = NULL;
   1631 	}
   1632 	sc->sc_ac_cookie_len = 0;
   1633 	if (sc->sc_relay_sid) {
   1634 		free(sc->sc_relay_sid, M_DEVBUF);
   1635 		sc->sc_relay_sid = NULL;
   1636 	}
   1637 	sc->sc_relay_sid_len = 0;
   1638 #ifdef PPPOE_SERVER
   1639 	if (sc->sc_hunique) {
   1640 		free(sc->sc_hunique, M_DEVBUF);
   1641 		sc->sc_hunique = NULL;
   1642 	}
   1643 	sc->sc_hunique_len = 0;
   1644 #endif
   1645 	sc->sc_session = 0;
   1646 
   1647 	PPPOE_UNLOCK(sc);
   1648 
   1649 	/* notify upper layer */
   1650 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   1651 
   1652 	PPPOE_LOCK(sc, RW_WRITER);
   1653 
   1654 	RELEASE_SPLNET();
   1655 	return err;
   1656 }
   1657 
   1658 /* Connection attempt aborted */
   1659 static void
   1660 pppoe_abort_connect(struct pppoe_softc *sc)
   1661 {
   1662 	KASSERT(PPPOE_WLOCKED(sc));
   1663 
   1664 	pppoe_printf(sc, "could not establish connection\n");
   1665 	sc->sc_state = PPPOE_STATE_CLOSING;
   1666 
   1667 	PPPOE_UNLOCK(sc);
   1668 
   1669 	/* notify upper layer */
   1670 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   1671 
   1672 	PPPOE_LOCK(sc, RW_WRITER);
   1673 
   1674 	/* clear connection state */
   1675 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   1676 	sc->sc_state = PPPOE_STATE_INITIAL;
   1677 }
   1678 
   1679 static int
   1680 pppoe_send_padr(struct pppoe_softc *sc)
   1681 {
   1682 	struct mbuf *m0;
   1683 	uint8_t *p;
   1684 	size_t len, l1 = 0;
   1685 
   1686 	if (sc->sc_state != PPPOE_STATE_PADR_SENT)
   1687 		return EIO;
   1688 
   1689 	/* Compute packet length. */
   1690 	len = sizeof(struct pppoetag);
   1691 	if (sc->sc_service_name != NULL) {
   1692 		l1 = strlen(sc->sc_service_name);
   1693 		len += l1;
   1694 	}
   1695 	if (sc->sc_ac_cookie_len > 0) {
   1696 		len += sizeof(struct pppoetag) + sc->sc_ac_cookie_len;
   1697 	}
   1698 	if (sc->sc_relay_sid_len > 0) {
   1699 		len += sizeof(struct pppoetag) + sc->sc_relay_sid_len;
   1700 	}
   1701 	len += sizeof(struct pppoetag) + sizeof(sc->sc_id);
   1702 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1703 		len += sizeof(struct pppoetag) + 2;
   1704 	}
   1705 
   1706 	/* Allocate packet. */
   1707 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1708 	if (m0 == NULL)
   1709 		return ENOBUFS;
   1710 
   1711 	/* Fill in packet. */
   1712 	p = mtod(m0, uint8_t *);
   1713 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
   1714 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1715 	if (sc->sc_service_name != NULL) {
   1716 		PPPOE_ADD_16(p, l1);
   1717 		memcpy(p, sc->sc_service_name, l1);
   1718 		p += l1;
   1719 	} else {
   1720 		PPPOE_ADD_16(p, 0);
   1721 	}
   1722 	if (sc->sc_ac_cookie_len > 0) {
   1723 		PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
   1724 		PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
   1725 		memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
   1726 		p += sc->sc_ac_cookie_len;
   1727 	}
   1728 	if (sc->sc_relay_sid_len > 0) {
   1729 		PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
   1730 		PPPOE_ADD_16(p, sc->sc_relay_sid_len);
   1731 		memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
   1732 		p += sc->sc_relay_sid_len;
   1733 	}
   1734 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1735 	PPPOE_ADD_16(p, sizeof(sc->sc_id));
   1736 	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
   1737 	p += sizeof(sc->sc_id);
   1738 
   1739 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1740 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
   1741 		PPPOE_ADD_16(p, 2);
   1742 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
   1743 	}
   1744 
   1745 #ifdef PPPOE_DEBUG
   1746 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
   1747 		panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
   1748 			(long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
   1749 #endif
   1750 
   1751 	/* Send packet. */
   1752 	return pppoe_output(sc, m0);
   1753 }
   1754 
   1755 /* send a PADT packet */
   1756 static int
   1757 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
   1758 {
   1759 	struct ether_header *eh;
   1760 	struct sockaddr dst;
   1761 	struct mbuf *m0;
   1762 	uint8_t *p;
   1763 
   1764 	m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
   1765 	if (!m0)
   1766 		return EIO;
   1767 	p = mtod(m0, uint8_t *);
   1768 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
   1769 
   1770 	memset(&dst, 0, sizeof dst);
   1771 	dst.sa_family = AF_UNSPEC;
   1772 	eh = (struct ether_header*)&dst.sa_data;
   1773 	eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
   1774 	memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
   1775 
   1776 	m0->m_flags &= ~(M_BCAST|M_MCAST);
   1777 	return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL);
   1778 }
   1779 
   1780 #ifdef PPPOE_SERVER
   1781 static int
   1782 pppoe_send_pado(struct pppoe_softc *sc)
   1783 {
   1784 	struct mbuf *m0;
   1785 	uint8_t *p;
   1786 	size_t len;
   1787 
   1788 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
   1789 		return EIO;
   1790 
   1791 	/* Include AC cookie. */
   1792 	len = sizeof(struct pppoetag) + sizeof(sc->sc_id);
   1793 	/* Include hunique. */
   1794 	len += sizeof(struct pppoetag) + sc->sc_hunique_len;
   1795 
   1796 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1797 	if (!m0)
   1798 		return EIO;
   1799 	p = mtod(m0, uint8_t *);
   1800 
   1801 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
   1802 	PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
   1803 	PPPOE_ADD_16(p, sizeof(sc->sc_id));
   1804 	memcpy(p, &sc->sc_id, sizeof(sc->sc_id));
   1805 	p += sizeof(sc->sc_id);
   1806 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1807 	PPPOE_ADD_16(p, sc->sc_hunique_len);
   1808 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
   1809 	return pppoe_output(sc, m0);
   1810 }
   1811 
   1812 static int
   1813 pppoe_send_pads(struct pppoe_softc *sc)
   1814 {
   1815 	struct bintime bt;
   1816 	struct mbuf *m0;
   1817 	uint8_t *p;
   1818 	size_t len, l1 = 0;	/* XXX: gcc */
   1819 
   1820 	KASSERT(PPPOE_WLOCKED(sc));
   1821 
   1822 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
   1823 		return EIO;
   1824 
   1825 	getbinuptime(&bt);
   1826 	sc->sc_session = bt.sec % 0xff + 1;
   1827 
   1828 	/* Include service name. */
   1829 	len = sizeof(struct pppoetag);
   1830 	if (sc->sc_service_name != NULL) {
   1831 		l1 = strlen(sc->sc_service_name);
   1832 		len += l1;
   1833 	}
   1834 	/* Include hunique. */
   1835 	len += sizeof(struct pppoetag) + sc->sc_hunique_len;
   1836 
   1837 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1838 	if (!m0)
   1839 		return ENOBUFS;
   1840 	p = mtod(m0, uint8_t *);
   1841 
   1842 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
   1843 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1844 	if (sc->sc_service_name != NULL) {
   1845 		PPPOE_ADD_16(p, l1);
   1846 		memcpy(p, sc->sc_service_name, l1);
   1847 		p += l1;
   1848 	} else {
   1849 		PPPOE_ADD_16(p, 0);
   1850 	}
   1851 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1852 	PPPOE_ADD_16(p, sc->sc_hunique_len);
   1853 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
   1854 	return pppoe_output(sc, m0);
   1855 }
   1856 #endif
   1857 
   1858 static void
   1859 pppoe_tls(struct sppp *sp)
   1860 {
   1861 	struct pppoe_softc *sc = (void *)sp;
   1862 	int wtime;
   1863 
   1864 	PPPOE_LOCK(sc, RW_READER);
   1865 
   1866 	if (sc->sc_state != PPPOE_STATE_INITIAL) {
   1867 		PPPOE_UNLOCK(sc);
   1868 		return;
   1869 	}
   1870 
   1871 	if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
   1872 	    sc->sc_sppp.pp_auth_failures > 0) {
   1873 		/*
   1874 		 * Delay trying to reconnect a bit more - the peer
   1875 		 * might have failed to contact its radius server.
   1876 		 */
   1877 		wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
   1878 		if (wtime > PPPOE_SLOW_RETRY)
   1879 			wtime = PPPOE_SLOW_RETRY;
   1880 	} else {
   1881 		wtime = PPPOE_RECON_IMMEDIATE;
   1882 	}
   1883 	callout_schedule(&sc->sc_timeout, wtime);
   1884 
   1885 	PPPOE_UNLOCK(sc);
   1886 }
   1887 
   1888 static void
   1889 pppoe_tlf(struct sppp *sp)
   1890 {
   1891 	struct pppoe_softc *sc = (void *)sp;
   1892 
   1893 	PPPOE_LOCK(sc, RW_WRITER);
   1894 
   1895 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1896 		callout_stop(&sc->sc_timeout);
   1897 		sc->sc_state = PPPOE_STATE_INITIAL;
   1898 		sc->sc_padi_retried = 0;
   1899 		sc->sc_padr_retried = 0;
   1900 		memcpy(&sc->sc_dest, etherbroadcastaddr,
   1901 		    sizeof(sc->sc_dest));
   1902 		PPPOE_UNLOCK(sc);
   1903 		return;
   1904 	}
   1905 
   1906 	/*
   1907 	 * Do not call pppoe_disconnect here, the upper layer state
   1908 	 * machine gets confused by this. We must return from this
   1909 	 * function and defer disconnecting to the timeout handler.
   1910 	 */
   1911 	sc->sc_state = PPPOE_STATE_CLOSING;
   1912 
   1913 	callout_schedule(&sc->sc_timeout, hz/50);
   1914 
   1915 	PPPOE_UNLOCK(sc);
   1916 }
   1917 
   1918 static void
   1919 pppoe_start(struct ifnet *ifp)
   1920 {
   1921 	struct pppoe_softc *sc = (void *)ifp;
   1922 	struct mbuf *m;
   1923 	uint8_t *p;
   1924 	size_t len;
   1925 
   1926 	if (sppp_isempty(ifp))
   1927 		return;
   1928 
   1929 	/* are we ready to process data yet? */
   1930 	PPPOE_LOCK(sc, RW_READER);
   1931 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1932 		sppp_flush(&sc->sc_sppp.pp_if);
   1933 		PPPOE_UNLOCK(sc);
   1934 		return;
   1935 	}
   1936 
   1937 	while ((m = sppp_dequeue(ifp)) != NULL) {
   1938 		len = m->m_pkthdr.len;
   1939 		M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
   1940 		if (m == NULL) {
   1941 			if_statinc(ifp, if_oerrors);
   1942 			continue;
   1943 		}
   1944 		p = mtod(m, uint8_t *);
   1945 		PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
   1946 
   1947 		bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT);
   1948 
   1949 		pppoe_output(sc, m);
   1950 	}
   1951 	PPPOE_UNLOCK(sc);
   1952 }
   1953 
   1954 #ifdef PPPOE_MPSAFE
   1955 static int
   1956 pppoe_transmit(struct ifnet *ifp, struct mbuf *m)
   1957 {
   1958 	struct pppoe_softc *sc = (void *)ifp;
   1959 	uint8_t *p;
   1960 	size_t len;
   1961 
   1962 	if (m == NULL)
   1963 		return EINVAL;
   1964 
   1965 	/* are we ready to process data yet? */
   1966 	PPPOE_LOCK(sc, RW_READER);
   1967 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1968 		PPPOE_UNLOCK(sc);
   1969 		m_freem(m);
   1970 		return ENOBUFS;
   1971 	}
   1972 
   1973 	len = m->m_pkthdr.len;
   1974 	M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
   1975 	if (m == NULL) {
   1976 		PPPOE_UNLOCK(sc);
   1977 		if_statinc(ifp, if_oerrors);
   1978 		return ENETDOWN;
   1979 	}
   1980 	p = mtod(m, uint8_t *);
   1981 	PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
   1982 
   1983 	bpf_mtap(&sc->sc_sppp.pp_if, m, BPF_D_OUT);
   1984 
   1985 	pppoe_output(sc, m);
   1986 	PPPOE_UNLOCK(sc);
   1987 	return 0;
   1988 }
   1989 #endif /* PPPOE_MPSAFE */
   1990 
   1991 static void
   1992 pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2)
   1993 {
   1994 	struct ifnet *ifp = arg2;
   1995 	struct pppoe_softc *sc;
   1996 	DECLARE_SPLNET_VARIABLE;
   1997 
   1998 	if (cmd != PFIL_IFNET_DETACH)
   1999 		return;
   2000 
   2001 	ACQUIRE_SPLNET();
   2002 	rw_enter(&pppoe_softc_list_lock, RW_READER);
   2003 	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
   2004 		PPPOE_LOCK(sc, RW_WRITER);
   2005 		if (sc->sc_eth_if != ifp) {
   2006 			PPPOE_UNLOCK(sc);
   2007 			continue;
   2008 		}
   2009 		if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
   2010 			sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
   2011 			pppoe_printf(sc,
   2012 			    "ethernet interface detached, going down\n");
   2013 		}
   2014 		sc->sc_eth_if = NULL;
   2015 		pppoe_clear_softc(sc, "ethernet interface detached");
   2016 		PPPOE_UNLOCK(sc);
   2017 	}
   2018 	rw_exit(&pppoe_softc_list_lock);
   2019 	RELEASE_SPLNET();
   2020 }
   2021 
   2022 static void
   2023 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
   2024 {
   2025 	KASSERT(PPPOE_WLOCKED(sc));
   2026 
   2027 	/* stop timer */
   2028 	callout_stop(&sc->sc_timeout);
   2029 	pppoe_printf(sc, "session 0x%x terminated, %s\n",
   2030 	    sc->sc_session, message);
   2031 
   2032 	/* fix our state */
   2033 	sc->sc_state = PPPOE_STATE_INITIAL;
   2034 
   2035 	PPPOE_UNLOCK(sc);
   2036 
   2037 	/* signal upper layer */
   2038 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   2039 
   2040 	PPPOE_LOCK(sc, RW_WRITER);
   2041 
   2042 	/* clean up softc */
   2043 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   2044 	if (sc->sc_ac_cookie) {
   2045 		free(sc->sc_ac_cookie, M_DEVBUF);
   2046 		sc->sc_ac_cookie = NULL;
   2047 	}
   2048 	if (sc->sc_relay_sid) {
   2049 		free(sc->sc_relay_sid, M_DEVBUF);
   2050 		sc->sc_relay_sid = NULL;
   2051 	}
   2052 	sc->sc_ac_cookie_len = 0;
   2053 	sc->sc_session = 0;
   2054 }
   2055 
   2056 static void
   2057 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
   2058 {
   2059 	if (m->m_flags & M_PROMISC) {
   2060 		m_freem(m);
   2061 		return;
   2062 	}
   2063 
   2064 #ifndef PPPOE_SERVER
   2065 	if (m->m_flags & (M_MCAST | M_BCAST)) {
   2066 		m_freem(m);
   2067 		return;
   2068 	}
   2069 #endif
   2070 
   2071 	IFQ_LOCK(inq);
   2072 	if (IF_QFULL(inq)) {
   2073 		IF_DROP(inq);
   2074 		IFQ_UNLOCK(inq);
   2075 		m_freem(m);
   2076 	} else {
   2077 		IF_ENQUEUE(inq, m);
   2078 		IFQ_UNLOCK(inq);
   2079 		softint_schedule(pppoe_softintr);
   2080 	}
   2081 	return;
   2082 }
   2083 
   2084 void
   2085 pppoe_input(struct ifnet *ifp, struct mbuf *m)
   2086 {
   2087 	pppoe_enqueue(&ppoeinq, m);
   2088 	return;
   2089 }
   2090 
   2091 void
   2092 pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
   2093 {
   2094 	pppoe_enqueue(&ppoediscinq, m);
   2095 	return;
   2096 }
   2097 
   2098 static void
   2099 sysctl_net_pppoe_setup(struct sysctllog **clog)
   2100 {
   2101 	const struct sysctlnode *node = NULL;
   2102 
   2103 	sysctl_createv(clog, 0, NULL, &node,
   2104 	    CTLFLAG_PERMANENT,
   2105 	    CTLTYPE_NODE, "pppoe",
   2106 	    SYSCTL_DESCR("PPPOE protocol"),
   2107 	    NULL, 0, NULL, 0,
   2108 	    CTL_NET, CTL_CREATE, CTL_EOL);
   2109 
   2110 	if (node == NULL)
   2111 		return;
   2112 
   2113 	sysctl_createv(clog, 0, &node, NULL,
   2114 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2115 	    CTLTYPE_BOOL, "term_unknown",
   2116 	    SYSCTL_DESCR("Terminate unknown sessions"),
   2117 	    NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown),
   2118 	    CTL_CREATE, CTL_EOL);
   2119 }
   2120 
   2121 /*
   2122  * Module infrastructure
   2123  */
   2124 #include "if_module.h"
   2125 
   2126 IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")
   2127