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