Home | History | Annotate | Line # | Download | only in net
if_pppoe.c revision 1.136
      1 /* $NetBSD: if_pppoe.c,v 1.136 2018/04/18 07:40:40 knakahara Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Martin Husemann <martin (at) NetBSD.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.136 2018/04/18 07:40:40 knakahara Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "pppoe.h"
     37 #include "opt_pppoe.h"
     38 #include "opt_net_mpsafe.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/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 #include <net/if_dl.h>
     67 
     68 #include <net/bpf.h>
     69 
     70 #include "ioconf.h"
     71 
     72 #ifdef NET_MPSAFE
     73 #define PPPOE_MPSAFE	1
     74 #endif
     75 
     76 struct pppoehdr {
     77 	uint8_t vertype;
     78 	uint8_t code;
     79 	uint16_t session;
     80 	uint16_t plen;
     81 } __packed;
     82 
     83 struct pppoetag {
     84 	uint16_t tag;
     85 	uint16_t len;
     86 } __packed;
     87 
     88 #define	PPPOE_HEADERLEN	sizeof(struct pppoehdr)
     89 #define	PPPOE_OVERHEAD	(PPPOE_HEADERLEN + 2)
     90 #define	PPPOE_VERTYPE	0x11	/* VER=1, TYPE = 1 */
     91 
     92 #define	PPPOE_TAG_EOL		0x0000		/* end of list */
     93 #define	PPPOE_TAG_SNAME		0x0101		/* service name */
     94 #define	PPPOE_TAG_ACNAME	0x0102		/* access concentrator name */
     95 #define	PPPOE_TAG_HUNIQUE	0x0103		/* host unique */
     96 #define	PPPOE_TAG_ACCOOKIE	0x0104		/* AC cookie */
     97 #define	PPPOE_TAG_VENDOR	0x0105		/* vendor specific */
     98 #define	PPPOE_TAG_RELAYSID	0x0110		/* relay session id */
     99 #define	PPPOE_TAG_MAX_PAYLOAD	0x0120		/* max payload */
    100 #define	PPPOE_TAG_SNAME_ERR	0x0201		/* service name error */
    101 #define	PPPOE_TAG_ACSYS_ERR	0x0202		/* AC system error */
    102 #define	PPPOE_TAG_GENERIC_ERR	0x0203		/* generic error */
    103 
    104 #define	PPPOE_CODE_PADI		0x09		/* Active Discovery Initiation */
    105 #define	PPPOE_CODE_PADO		0x07		/* Active Discovery Offer */
    106 #define	PPPOE_CODE_PADR		0x19		/* Active Discovery Request */
    107 #define	PPPOE_CODE_PADS		0x65		/* Active Discovery Session confirmation */
    108 #define	PPPOE_CODE_PADT		0xA7		/* Active Discovery Terminate */
    109 
    110 /* two byte PPP protocol discriminator, then IP data */
    111 #define	PPPOE_MAXMTU	(ETHERMTU - PPPOE_OVERHEAD)
    112 
    113 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
    114 #define	PPPOE_ADD_16(PTR, VAL)			\
    115 		*(PTR)++ = (VAL) / 256;		\
    116 		*(PTR)++ = (VAL) % 256
    117 
    118 /* Add a complete PPPoE header to the buffer pointed to by PTR */
    119 #define	PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)	\
    120 		*(PTR)++ = PPPOE_VERTYPE;	\
    121 		*(PTR)++ = (CODE);		\
    122 		PPPOE_ADD_16(PTR, SESS);	\
    123 		PPPOE_ADD_16(PTR, LEN)
    124 
    125 #define	PPPOE_DISC_TIMEOUT	(hz*5)	/* base for quick timeout calculation */
    126 #define	PPPOE_SLOW_RETRY	(hz*60)	/* persistent retry interval */
    127 #define	PPPOE_RECON_FAST	(hz*15)	/* first retry after auth failure */
    128 #define	PPPOE_RECON_IMMEDIATE	(hz/10)	/* "no delay" reconnect */
    129 #define	PPPOE_DISC_MAXPADI	4	/* retry PADI four times (quickly) */
    130 #define	PPPOE_DISC_MAXPADR	2	/* retry PADR twice */
    131 
    132 #ifdef PPPOE_SERVER
    133 /* from if_spppsubr.c */
    134 #define	IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
    135 #endif
    136 
    137 #define PPPOE_LOCK(_sc, _op)	rw_enter(&(_sc)->sc_lock, (_op))
    138 #define PPPOE_UNLOCK(_sc)	rw_exit(&(_sc)->sc_lock)
    139 #define PPPOE_WLOCKED(_sc)	rw_write_held(&(_sc)->sc_lock)
    140 
    141 #ifdef PPPOE_MPSAFE
    142 #define DECLARE_SPLNET_VARIABLE
    143 #define ACQUIRE_SPLNET()	do { } while (0)
    144 #define RELEASE_SPLNET()	do { } while (0)
    145 #else
    146 #define DECLARE_SPLNET_VARIABLE	int __s
    147 #define ACQUIRE_SPLNET()	do {					\
    148 					__s = splnet();			\
    149 				} while (0)
    150 #define RELEASE_SPLNET()	do {					\
    151 					splx(__s);			\
    152 				} while (0)
    153 #endif
    154 
    155 struct pppoe_softc {
    156 	struct sppp sc_sppp;		/* contains a struct ifnet as first element */
    157 	LIST_ENTRY(pppoe_softc) sc_list;
    158 	struct ifnet *sc_eth_if;	/* ethernet interface we are using */
    159 
    160 	int sc_state;			/* discovery phase or session connected */
    161 	struct ether_addr sc_dest;	/* hardware address of concentrator */
    162 	uint16_t sc_session;		/* PPPoE session id */
    163 
    164 	char *sc_service_name;		/* if != NULL: requested name of service */
    165 	char *sc_concentrator_name;	/* if != NULL: requested concentrator id */
    166 	uint8_t *sc_ac_cookie;		/* content of AC cookie we must echo back */
    167 	size_t sc_ac_cookie_len;	/* length of cookie data */
    168 	uint8_t *sc_relay_sid;		/* content of relay SID we must echo back */
    169 	size_t sc_relay_sid_len;	/* length of relay SID data */
    170 #ifdef PPPOE_SERVER
    171 	uint8_t *sc_hunique;		/* content of host unique we must echo back */
    172 	size_t sc_hunique_len;		/* length of host unique */
    173 #endif
    174 	callout_t sc_timeout;	/* timeout while not in session state */
    175 	int sc_padi_retried;		/* number of PADI retries already done */
    176 	int sc_padr_retried;		/* number of PADR retries already done */
    177 	krwlock_t sc_lock;	/* lock of sc_state, sc_session, and sc_eth_if */
    178 };
    179 
    180 /* incoming traffic will be queued here */
    181 struct ifqueue ppoediscinq = { .ifq_maxlen = IFQ_MAXLEN };
    182 struct ifqueue ppoeinq = { .ifq_maxlen = IFQ_MAXLEN };
    183 
    184 void *pppoe_softintr = NULL;
    185 static void pppoe_softintr_handler(void *);
    186 
    187 extern int sppp_ioctl(struct ifnet *, unsigned long, void *);
    188 
    189 /* input routines */
    190 static void pppoeintr(void);
    191 static void pppoe_disc_input(struct mbuf *);
    192 static void pppoe_dispatch_disc_pkt(struct mbuf *, int);
    193 static void pppoe_data_input(struct mbuf *);
    194 static void pppoe_enqueue(struct ifqueue *, struct mbuf *);
    195 
    196 /* management routines */
    197 static int pppoe_connect(struct pppoe_softc *);
    198 static int pppoe_disconnect(struct pppoe_softc *);
    199 static void pppoe_abort_connect(struct pppoe_softc *);
    200 static int pppoe_ioctl(struct ifnet *, unsigned long, void *);
    201 static void pppoe_tls(struct sppp *);
    202 static void pppoe_tlf(struct sppp *);
    203 static void pppoe_start(struct ifnet *);
    204 #ifdef PPPOE_MPSAFE
    205 static int pppoe_transmit(struct ifnet *, struct mbuf *);
    206 #endif
    207 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
    208 
    209 /* internal timeout handling */
    210 static void pppoe_timeout(void *);
    211 
    212 /* sending actual protocol controll packets */
    213 static int pppoe_send_padi(struct pppoe_softc *);
    214 static int pppoe_send_padr(struct pppoe_softc *);
    215 #ifdef PPPOE_SERVER
    216 static int pppoe_send_pado(struct pppoe_softc *);
    217 static int pppoe_send_pads(struct pppoe_softc *);
    218 #endif
    219 static int pppoe_send_padt(struct ifnet *, u_int, const uint8_t *);
    220 
    221 /* raw output */
    222 static int pppoe_output(struct pppoe_softc *, struct mbuf *);
    223 
    224 /* internal helper functions */
    225 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *,
    226     krw_t);
    227 static struct pppoe_softc * pppoe_find_softc_by_hunique(uint8_t *, size_t,
    228     struct ifnet *, krw_t);
    229 static struct mbuf *pppoe_get_mbuf(size_t len);
    230 
    231 static void pppoe_ifattach_hook(void *, unsigned long, void *);
    232 
    233 static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
    234 static krwlock_t pppoe_softc_list_lock;
    235 
    236 static int	pppoe_clone_create(struct if_clone *, int);
    237 static int	pppoe_clone_destroy(struct ifnet *);
    238 
    239 static bool	pppoe_term_unknown = false;
    240 static int	pppoe_term_unknown_pps = 1;
    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 bool
    957 pppoe_is_my_frame(uint8_t *dhost, struct ifnet *rcvif)
    958 {
    959 
    960 	if (memcmp(CLLADDR(rcvif->if_sadl), dhost, ETHER_ADDR_LEN) == 0)
    961 		return true;
    962 
    963 	return false;
    964 }
    965 
    966 static void
    967 pppoe_data_input(struct mbuf *m)
    968 {
    969 	uint16_t session, plen;
    970 	struct pppoe_softc *sc;
    971 	struct pppoehdr *ph;
    972 	struct ifnet *rcvif;
    973 	struct psref psref;
    974 	uint8_t shost[ETHER_ADDR_LEN];
    975 	uint8_t dhost[ETHER_ADDR_LEN];
    976 	bool term_unknown = pppoe_term_unknown;
    977 
    978 	KASSERT(m->m_flags & M_PKTHDR);
    979 
    980 	if (term_unknown) {
    981 		memcpy(shost, mtod(m, struct ether_header*)->ether_shost,
    982 		    ETHER_ADDR_LEN);
    983 		memcpy(dhost, mtod(m, struct ether_header*)->ether_dhost,
    984 		    ETHER_ADDR_LEN);
    985 	}
    986 	m_adj(m, sizeof(struct ether_header));
    987 	if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
    988 		printf("pppoe (data): dropping too short packet: %d bytes\n",
    989 		    m->m_pkthdr.len);
    990 		goto drop;
    991 	}
    992 
    993 	if (m->m_len < sizeof(*ph)) {
    994 		m = m_pullup(m, sizeof(*ph));
    995 		if (!m) {
    996 			printf("pppoe: could not get PPPoE header\n");
    997 			return;
    998 		}
    999 	}
   1000 	ph = mtod(m, struct pppoehdr *);
   1001 
   1002 	if (ph->vertype != PPPOE_VERTYPE) {
   1003 		printf("pppoe (data): unknown version/type packet: 0x%x\n",
   1004 		    ph->vertype);
   1005 		goto drop;
   1006 	}
   1007 	if (ph->code != 0)
   1008 		goto drop;
   1009 
   1010 	session = ntohs(ph->session);
   1011 	rcvif = m_get_rcvif_psref(m, &psref);
   1012 	if (__predict_false(rcvif == NULL))
   1013 		goto drop;
   1014 	sc = pppoe_find_softc_by_session(session, rcvif, RW_READER);
   1015 	if (sc == NULL) {
   1016 		if (term_unknown) {
   1017 			static struct timeval lasttime = {0, 0};
   1018 			static int curpps = 0;
   1019 			/*
   1020 			 * avoid to send wrong PADT which is response from
   1021 			 * session stage pakcets for other hosts when parent
   1022 			 * ethernet is promiscuous mode.
   1023 			 */
   1024 			if (pppoe_is_my_frame(dhost, rcvif)
   1025 			    && ppsratecheck(&lasttime, &curpps,
   1026 				pppoe_term_unknown_pps)) {
   1027 				printf("pppoe: input for unknown session %#x, "
   1028 				    "sending PADT\n", session);
   1029 				pppoe_send_padt(rcvif, session, shost);
   1030 			}
   1031 		}
   1032 		m_put_rcvif_psref(rcvif, &psref);
   1033 		goto drop;
   1034 	}
   1035 
   1036 	m_put_rcvif_psref(rcvif, &psref);
   1037 
   1038 	plen = ntohs(ph->plen);
   1039 
   1040 	bpf_mtap(&sc->sc_sppp.pp_if, m);
   1041 
   1042 	m_adj(m, PPPOE_HEADERLEN);
   1043 
   1044 #ifdef PPPOE_DEBUG
   1045 	{
   1046 		struct mbuf *p;
   1047 
   1048 		printf("%s: pkthdr.len=%d, pppoe.len=%d",
   1049 		    sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen);
   1050 		p = m;
   1051 		while (p) {
   1052 			printf(" l=%d", p->m_len);
   1053 			p = p->m_next;
   1054 		}
   1055 		printf("\n");
   1056 	}
   1057 #endif
   1058 	PPPOE_UNLOCK(sc);
   1059 
   1060 	if (m->m_pkthdr.len < plen)
   1061 		goto drop;
   1062 
   1063 	/*
   1064 	 *  Fix incoming interface pointer (not the raw ethernet interface
   1065 	 * anymore)
   1066 	 */
   1067 	m_set_rcvif(m, &sc->sc_sppp.pp_if);
   1068 
   1069 	/* pass packet up and account for it */
   1070 	sc->sc_sppp.pp_if.if_ipackets++;
   1071 	sppp_input(&sc->sc_sppp.pp_if, m);
   1072 	return;
   1073 
   1074 drop:
   1075 	m_freem(m);
   1076 }
   1077 
   1078 static int
   1079 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
   1080 {
   1081 	struct sockaddr dst;
   1082 	struct ether_header *eh;
   1083 	uint16_t etype;
   1084 
   1085 	if (sc->sc_eth_if == NULL) {
   1086 		m_freem(m);
   1087 		return EIO;
   1088 	}
   1089 
   1090 	memset(&dst, 0, sizeof dst);
   1091 	dst.sa_family = AF_UNSPEC;
   1092 	eh = (struct ether_header*)&dst.sa_data;
   1093 	etype = sc->sc_state == PPPOE_STATE_SESSION
   1094 	    ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
   1095 	eh->ether_type = htons(etype);
   1096 	memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
   1097 
   1098 #ifdef PPPOE_DEBUG
   1099 	printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
   1100 	    sc->sc_sppp.pp_if.if_xname, etype,
   1101 	    sc->sc_state, sc->sc_session,
   1102 	    ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
   1103 #endif
   1104 
   1105 	m->m_flags &= ~(M_BCAST|M_MCAST);
   1106 	sc->sc_sppp.pp_if.if_opackets++;
   1107 	return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL);
   1108 }
   1109 
   1110 static int
   1111 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   1112 {
   1113 	struct lwp *l = curlwp;	/* XXX */
   1114 	struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
   1115 	struct ifreq *ifr = data;
   1116 	int error = 0;
   1117 
   1118 	switch (cmd) {
   1119 	case PPPOESETPARMS:
   1120 	{
   1121 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
   1122 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
   1123 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1124 		    NULL) != 0)
   1125 			return EPERM;
   1126 		if (parms->eth_ifname[0] != 0) {
   1127 			struct ifnet	*eth_if;
   1128 
   1129 			PPPOE_LOCK(sc, RW_WRITER);
   1130 			eth_if = ifunit(parms->eth_ifname);
   1131 			if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
   1132 				sc->sc_eth_if = NULL;
   1133 				PPPOE_UNLOCK(sc);
   1134 				return ENXIO;
   1135 			}
   1136 
   1137 			if (sc->sc_sppp.pp_if.if_mtu !=
   1138 			    eth_if->if_mtu - PPPOE_OVERHEAD) {
   1139 				sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
   1140 				    PPPOE_OVERHEAD;
   1141 			}
   1142 			sc->sc_eth_if = eth_if;
   1143 			PPPOE_UNLOCK(sc);
   1144 		}
   1145 		if (parms->ac_name != NULL) {
   1146 			size_t s;
   1147 			char *b = malloc(parms->ac_name_len + 1, M_DEVBUF,
   1148 			    M_WAITOK);
   1149 			if (b == NULL)
   1150 				return ENOMEM;
   1151 			error = copyinstr(parms->ac_name, b,
   1152 			    parms->ac_name_len+1, &s);
   1153 			if (error != 0) {
   1154 				free(b, M_DEVBUF);
   1155 				return error;
   1156 			}
   1157 			if (s != parms->ac_name_len+1) {
   1158 				free(b, M_DEVBUF);
   1159 				return EINVAL;
   1160 			}
   1161 
   1162 			PPPOE_LOCK(sc, RW_WRITER);
   1163 			if (sc->sc_concentrator_name)
   1164 				free(sc->sc_concentrator_name, M_DEVBUF);
   1165 			sc->sc_concentrator_name = b;
   1166 			PPPOE_UNLOCK(sc);
   1167 		}
   1168 		if (parms->service_name != NULL) {
   1169 			size_t s;
   1170 			char *b = malloc(parms->service_name_len + 1, M_DEVBUF,
   1171 			    M_WAITOK);
   1172 			if (b == NULL)
   1173 				return ENOMEM;
   1174 			error = copyinstr(parms->service_name, b,
   1175 			    parms->service_name_len+1, &s);
   1176 			if (error != 0) {
   1177 				free(b, M_DEVBUF);
   1178 				return error;
   1179 			}
   1180 			if (s != parms->service_name_len+1) {
   1181 				free(b, M_DEVBUF);
   1182 				return EINVAL;
   1183 			}
   1184 
   1185 			PPPOE_LOCK(sc, RW_WRITER);
   1186 			if (sc->sc_service_name)
   1187 				free(sc->sc_service_name, M_DEVBUF);
   1188 			sc->sc_service_name = b;
   1189 			PPPOE_UNLOCK(sc);
   1190 		}
   1191 		return 0;
   1192 	}
   1193 	break;
   1194 	case PPPOEGETPARMS:
   1195 	{
   1196 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
   1197 		memset(parms, 0, sizeof *parms);
   1198 		PPPOE_LOCK(sc, RW_READER);
   1199 		if (sc->sc_eth_if)
   1200 			strlcpy(parms->ifname, sc->sc_eth_if->if_xname,
   1201 			    sizeof(parms->ifname));
   1202 		PPPOE_UNLOCK(sc);
   1203 		return 0;
   1204 	}
   1205 	break;
   1206 	case PPPOEGETSESSION:
   1207 	{
   1208 		struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
   1209 		PPPOE_LOCK(sc, RW_READER);
   1210 		state->state = sc->sc_state;
   1211 		state->session_id = sc->sc_session;
   1212 		state->padi_retry_no = sc->sc_padi_retried;
   1213 		state->padr_retry_no = sc->sc_padr_retried;
   1214 		PPPOE_UNLOCK(sc);
   1215 		return 0;
   1216 	}
   1217 	break;
   1218 	case SIOCSIFFLAGS:
   1219 		/*
   1220 		 * Prevent running re-establishment timers overriding
   1221 		 * administrators choice.
   1222 		 */
   1223 		PPPOE_LOCK(sc, RW_WRITER);
   1224 
   1225 		if ((ifr->ifr_flags & IFF_UP) == 0
   1226 		     && sc->sc_state >= PPPOE_STATE_PADI_SENT
   1227 		     && sc->sc_state < PPPOE_STATE_SESSION) {
   1228 			callout_stop(&sc->sc_timeout);
   1229 			sc->sc_state = PPPOE_STATE_INITIAL;
   1230 			sc->sc_padi_retried = 0;
   1231 			sc->sc_padr_retried = 0;
   1232 			memcpy(&sc->sc_dest, etherbroadcastaddr,
   1233 			    sizeof(sc->sc_dest));
   1234 		}
   1235 
   1236 		PPPOE_UNLOCK(sc);
   1237 
   1238 		error = sppp_ioctl(ifp, cmd, data);
   1239 
   1240 		return error;
   1241 	case SIOCSIFMTU:
   1242 		if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
   1243 		    PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
   1244 			return EINVAL;
   1245 		}
   1246 		/*FALLTHROUGH*/
   1247 	default:
   1248 		return sppp_ioctl(ifp, cmd, data);
   1249 	}
   1250 	return 0;
   1251 }
   1252 
   1253 /*
   1254  * Allocate a mbuf/cluster with space to store the given data length
   1255  * of payload, leaving space for prepending an ethernet header
   1256  * in front.
   1257  */
   1258 static struct mbuf *
   1259 pppoe_get_mbuf(size_t len)
   1260 {
   1261 	struct mbuf *m;
   1262 
   1263 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1264 	if (m == NULL)
   1265 		return NULL;
   1266 	if (len + sizeof(struct ether_header) > MHLEN) {
   1267 		MCLGET(m, M_DONTWAIT);
   1268 		if ((m->m_flags & M_EXT) == 0) {
   1269 			m_free(m);
   1270 			return NULL;
   1271 		}
   1272 	}
   1273 	m->m_data += sizeof(struct ether_header);
   1274 	m->m_len = len;
   1275 	m->m_pkthdr.len = len;
   1276 	m_reset_rcvif(m);
   1277 
   1278 	return m;
   1279 }
   1280 
   1281 static int
   1282 pppoe_send_padi(struct pppoe_softc *sc)
   1283 {
   1284 	struct mbuf *m0;
   1285 	int len, l1 = 0, l2 = 0; /* XXX: gcc */
   1286 	uint8_t *p;
   1287 
   1288 	if (sc->sc_state >PPPOE_STATE_PADI_SENT)
   1289 		panic("pppoe_send_padi in state %d", sc->sc_state);
   1290 
   1291 	/* calculate length of frame (excluding ethernet header + pppoe header) */
   1292 	len = 2 + 2 + 2 + 2 + sizeof sc;	/* service name tag is required, host unique is send too */
   1293 	if (sc->sc_service_name != NULL) {
   1294 		l1 = strlen(sc->sc_service_name);
   1295 		len += l1;
   1296 	}
   1297 	if (sc->sc_concentrator_name != NULL) {
   1298 		l2 = strlen(sc->sc_concentrator_name);
   1299 		len += 2 + 2 + l2;
   1300 	}
   1301 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1302 		len += 2 + 2 + 2;
   1303 	}
   1304 
   1305 	/* allocate a buffer */
   1306 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);	/* header len + payload len */
   1307 	if (!m0)
   1308 		return ENOBUFS;
   1309 
   1310 	/* fill in pkt */
   1311 	p = mtod(m0, uint8_t *);
   1312 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
   1313 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1314 	if (sc->sc_service_name != NULL) {
   1315 		PPPOE_ADD_16(p, l1);
   1316 		memcpy(p, sc->sc_service_name, l1);
   1317 		p += l1;
   1318 	} else {
   1319 		PPPOE_ADD_16(p, 0);
   1320 	}
   1321 	if (sc->sc_concentrator_name != NULL) {
   1322 		PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
   1323 		PPPOE_ADD_16(p, l2);
   1324 		memcpy(p, sc->sc_concentrator_name, l2);
   1325 		p += l2;
   1326 	}
   1327 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1328 	PPPOE_ADD_16(p, sizeof(sc));
   1329 	memcpy(p, &sc, sizeof sc);
   1330 	p += sizeof(sc);
   1331 
   1332 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1333 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
   1334 		PPPOE_ADD_16(p, 2);
   1335 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
   1336 	}
   1337 
   1338 #ifdef PPPOE_DEBUG
   1339 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
   1340 		panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
   1341 		    (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
   1342 #endif
   1343 
   1344 	/* send pkt */
   1345 	return pppoe_output(sc, m0);
   1346 }
   1347 
   1348 static void
   1349 pppoe_timeout(void *arg)
   1350 {
   1351 	int retry_wait, err;
   1352 	struct pppoe_softc *sc = (struct pppoe_softc*)arg;
   1353 	DECLARE_SPLNET_VARIABLE;
   1354 
   1355 #ifdef PPPOE_DEBUG
   1356 	printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
   1357 #endif
   1358 
   1359 	PPPOE_LOCK(sc, RW_WRITER);
   1360 	switch (sc->sc_state) {
   1361 	case PPPOE_STATE_INITIAL:
   1362 		/* delayed connect from pppoe_tls() */
   1363 		pppoe_connect(sc);
   1364 		break;
   1365 	case PPPOE_STATE_PADI_SENT:
   1366 		/*
   1367 		 * We have two basic ways of retrying:
   1368 		 *  - Quick retry mode: try a few times in short sequence
   1369 		 *  - Slow retry mode: we already had a connection successfully
   1370 		 *    established and will try infinitely (without user
   1371 		 *    intervention)
   1372 		 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
   1373 		 * is not set.
   1374 		 */
   1375 
   1376 		/* initialize for quick retry mode */
   1377 		retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
   1378 
   1379 		ACQUIRE_SPLNET();
   1380 		sc->sc_padi_retried++;
   1381 		if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
   1382 			if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
   1383 				/* slow retry mode */
   1384 				retry_wait = PPPOE_SLOW_RETRY;
   1385 			} else {
   1386 				pppoe_abort_connect(sc);
   1387 				RELEASE_SPLNET();
   1388 				PPPOE_UNLOCK(sc);
   1389 				return;
   1390 			}
   1391 		}
   1392 		if ((err = pppoe_send_padi(sc)) != 0) {
   1393 			sc->sc_padi_retried--;
   1394 			if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1395 				printf("%s: failed to transmit PADI, "
   1396 				    "error=%d\n",
   1397 				    sc->sc_sppp.pp_if.if_xname, err);
   1398 		}
   1399 		callout_reset(&sc->sc_timeout, retry_wait,
   1400 		    pppoe_timeout, sc);
   1401 		RELEASE_SPLNET();
   1402 		break;
   1403 
   1404 	case PPPOE_STATE_PADR_SENT:
   1405 		ACQUIRE_SPLNET();
   1406 		sc->sc_padr_retried++;
   1407 		if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
   1408 			memcpy(&sc->sc_dest, etherbroadcastaddr,
   1409 			    sizeof(sc->sc_dest));
   1410 			sc->sc_state = PPPOE_STATE_PADI_SENT;
   1411 			sc->sc_padr_retried = 0;
   1412 			if ((err = pppoe_send_padi(sc)) != 0) {
   1413 				if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1414 					printf("%s: failed to send PADI"
   1415 					    ", error=%d\n",
   1416 					    sc->sc_sppp.pp_if.if_xname, err);
   1417 			}
   1418 			callout_reset(&sc->sc_timeout,
   1419 			    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried),
   1420 			    pppoe_timeout, sc);
   1421 			RELEASE_SPLNET();
   1422 			PPPOE_UNLOCK(sc);
   1423 			return;
   1424 		}
   1425 		if ((err = pppoe_send_padr(sc)) != 0) {
   1426 			sc->sc_padr_retried--;
   1427 			if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1428 				printf("%s: failed to send PADR, "
   1429 				    "error=%d\n", sc->sc_sppp.pp_if.if_xname,
   1430 				    err);
   1431 		}
   1432 		callout_reset(&sc->sc_timeout,
   1433 		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
   1434 		    pppoe_timeout, sc);
   1435 		RELEASE_SPLNET();
   1436 		break;
   1437 	case PPPOE_STATE_CLOSING:
   1438 		pppoe_disconnect(sc);
   1439 		break;
   1440 	default:
   1441 		PPPOE_UNLOCK(sc);
   1442 		return;	/* all done, work in peace */
   1443 	}
   1444 	PPPOE_UNLOCK(sc);
   1445 }
   1446 
   1447 /* Start a connection (i.e. initiate discovery phase) */
   1448 static int
   1449 pppoe_connect(struct pppoe_softc *sc)
   1450 {
   1451 	int err;
   1452 	DECLARE_SPLNET_VARIABLE;
   1453 
   1454 	KASSERT(PPPOE_WLOCKED(sc));
   1455 
   1456 	if (sc->sc_state != PPPOE_STATE_INITIAL)
   1457 		return EBUSY;
   1458 
   1459 #ifdef PPPOE_SERVER
   1460 	/* wait PADI if IFF_PASSIVE */
   1461 	if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
   1462 		return 0;
   1463 #endif
   1464 	ACQUIRE_SPLNET();
   1465 	/* save state, in case we fail to send PADI */
   1466 	sc->sc_state = PPPOE_STATE_PADI_SENT;
   1467 	sc->sc_padr_retried = 0;
   1468 	err = pppoe_send_padi(sc);
   1469 	if (err != 0 && sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1470 		printf("%s: failed to send PADI, error=%d\n",
   1471 		    sc->sc_sppp.pp_if.if_xname, err);
   1472 	callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
   1473 	RELEASE_SPLNET();
   1474 	return err;
   1475 }
   1476 
   1477 /* disconnect */
   1478 static int
   1479 pppoe_disconnect(struct pppoe_softc *sc)
   1480 {
   1481 	int err;
   1482 	DECLARE_SPLNET_VARIABLE;
   1483 
   1484 	KASSERT(PPPOE_WLOCKED(sc));
   1485 
   1486 	ACQUIRE_SPLNET();
   1487 
   1488 	if (sc->sc_state < PPPOE_STATE_SESSION)
   1489 		err = EBUSY;
   1490 	else {
   1491 		if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1492 			printf("%s: disconnecting\n",
   1493 			    sc->sc_sppp.pp_if.if_xname);
   1494 		err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session,
   1495 		    (const uint8_t *)&sc->sc_dest);
   1496 	}
   1497 
   1498 	/* cleanup softc */
   1499 	sc->sc_state = PPPOE_STATE_INITIAL;
   1500 
   1501 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   1502 	if (sc->sc_ac_cookie) {
   1503 		free(sc->sc_ac_cookie, M_DEVBUF);
   1504 		sc->sc_ac_cookie = NULL;
   1505 	}
   1506 	sc->sc_ac_cookie_len = 0;
   1507 	if (sc->sc_relay_sid) {
   1508 		free(sc->sc_relay_sid, M_DEVBUF);
   1509 		sc->sc_relay_sid = NULL;
   1510 	}
   1511 	sc->sc_relay_sid_len = 0;
   1512 #ifdef PPPOE_SERVER
   1513 	if (sc->sc_hunique) {
   1514 		free(sc->sc_hunique, M_DEVBUF);
   1515 		sc->sc_hunique = NULL;
   1516 	}
   1517 	sc->sc_hunique_len = 0;
   1518 #endif
   1519 	sc->sc_session = 0;
   1520 
   1521 	PPPOE_UNLOCK(sc);
   1522 
   1523 	/* notify upper layer */
   1524 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   1525 
   1526 	PPPOE_LOCK(sc, RW_WRITER);
   1527 
   1528 	RELEASE_SPLNET();
   1529 	return err;
   1530 }
   1531 
   1532 /* Connection attempt aborted */
   1533 static void
   1534 pppoe_abort_connect(struct pppoe_softc *sc)
   1535 {
   1536 	KASSERT(PPPOE_WLOCKED(sc));
   1537 
   1538 	printf("%s: could not establish connection\n",
   1539 		sc->sc_sppp.pp_if.if_xname);
   1540 	sc->sc_state = PPPOE_STATE_CLOSING;
   1541 
   1542 	PPPOE_UNLOCK(sc);
   1543 
   1544 	/* notify upper layer */
   1545 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   1546 
   1547 	PPPOE_LOCK(sc, RW_WRITER);
   1548 
   1549 	/* clear connection state */
   1550 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   1551 	sc->sc_state = PPPOE_STATE_INITIAL;
   1552 }
   1553 
   1554 /* Send a PADR packet */
   1555 static int
   1556 pppoe_send_padr(struct pppoe_softc *sc)
   1557 {
   1558 	struct mbuf *m0;
   1559 	uint8_t *p;
   1560 	size_t len, l1 = 0; /* XXX: gcc */
   1561 
   1562 	if (sc->sc_state != PPPOE_STATE_PADR_SENT)
   1563 		return EIO;
   1564 
   1565 	len = 2 + 2 + 2 + 2 + sizeof(sc);		/* service name, host unique */
   1566 	if (sc->sc_service_name != NULL) {		/* service name tag maybe empty */
   1567 		l1 = strlen(sc->sc_service_name);
   1568 		len += l1;
   1569 	}
   1570 	if (sc->sc_ac_cookie_len > 0)
   1571 		len += 2 + 2 + sc->sc_ac_cookie_len;	/* AC cookie */
   1572 	if (sc->sc_relay_sid_len > 0)
   1573 		len += 2 + 2 + sc->sc_relay_sid_len;	/* Relay SID */
   1574 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1575 		len += 2 + 2 + 2;
   1576 	}
   1577 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1578 	if (!m0)
   1579 		return ENOBUFS;
   1580 	p = mtod(m0, uint8_t *);
   1581 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
   1582 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1583 	if (sc->sc_service_name != NULL) {
   1584 		PPPOE_ADD_16(p, l1);
   1585 		memcpy(p, sc->sc_service_name, l1);
   1586 		p += l1;
   1587 	} else {
   1588 		PPPOE_ADD_16(p, 0);
   1589 	}
   1590 	if (sc->sc_ac_cookie_len > 0) {
   1591 		PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
   1592 		PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
   1593 		memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
   1594 		p += sc->sc_ac_cookie_len;
   1595 	}
   1596 	if (sc->sc_relay_sid_len > 0) {
   1597 		PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
   1598 		PPPOE_ADD_16(p, sc->sc_relay_sid_len);
   1599 		memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
   1600 		p += sc->sc_relay_sid_len;
   1601 	}
   1602 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1603 	PPPOE_ADD_16(p, sizeof(sc));
   1604 	memcpy(p, &sc, sizeof sc);
   1605 	p += sizeof(sc);
   1606 
   1607 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
   1608 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
   1609 		PPPOE_ADD_16(p, 2);
   1610 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
   1611 	}
   1612 
   1613 #ifdef PPPOE_DEBUG
   1614 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
   1615 		panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
   1616 			(long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
   1617 #endif
   1618 
   1619 	return pppoe_output(sc, m0);
   1620 }
   1621 
   1622 /* send a PADT packet */
   1623 static int
   1624 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
   1625 {
   1626 	struct ether_header *eh;
   1627 	struct sockaddr dst;
   1628 	struct mbuf *m0;
   1629 	uint8_t *p;
   1630 
   1631 	m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
   1632 	if (!m0)
   1633 		return EIO;
   1634 	p = mtod(m0, uint8_t *);
   1635 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
   1636 
   1637 	memset(&dst, 0, sizeof dst);
   1638 	dst.sa_family = AF_UNSPEC;
   1639 	eh = (struct ether_header*)&dst.sa_data;
   1640 	eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
   1641 	memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
   1642 
   1643 	m0->m_flags &= ~(M_BCAST|M_MCAST);
   1644 	return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL);
   1645 }
   1646 
   1647 #ifdef PPPOE_SERVER
   1648 static int
   1649 pppoe_send_pado(struct pppoe_softc *sc)
   1650 {
   1651 	struct mbuf *m0;
   1652 	uint8_t *p;
   1653 	size_t len;
   1654 
   1655 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
   1656 		return EIO;
   1657 
   1658 	/* calc length */
   1659 	len = 0;
   1660 	/* include ac_cookie */
   1661 	len += 2 + 2 + sizeof(sc);
   1662 	/* include hunique */
   1663 	len += 2 + 2 + sc->sc_hunique_len;
   1664 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1665 	if (!m0)
   1666 		return EIO;
   1667 	p = mtod(m0, uint8_t *);
   1668 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
   1669 	PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
   1670 	PPPOE_ADD_16(p, sizeof(sc));
   1671 	memcpy(p, &sc, sizeof(sc));
   1672 	p += sizeof(sc);
   1673 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1674 	PPPOE_ADD_16(p, sc->sc_hunique_len);
   1675 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
   1676 	return pppoe_output(sc, m0);
   1677 }
   1678 
   1679 static int
   1680 pppoe_send_pads(struct pppoe_softc *sc)
   1681 {
   1682 	struct bintime bt;
   1683 	struct mbuf *m0;
   1684 	uint8_t *p;
   1685 	size_t len, l1 = 0;	/* XXX: gcc */
   1686 
   1687 	KASSERT(PPPOE_WLOCKED(sc));
   1688 
   1689 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
   1690 		return EIO;
   1691 
   1692 	getbinuptime(&bt);
   1693 	sc->sc_session = bt.sec % 0xff + 1;
   1694 	/* calc length */
   1695 	len = 0;
   1696 	/* include hunique */
   1697 	len += 2 + 2 + 2 + 2 + sc->sc_hunique_len;	/* service name, host unique*/
   1698 	if (sc->sc_service_name != NULL) {		/* service name tag maybe empty */
   1699 		l1 = strlen(sc->sc_service_name);
   1700 		len += l1;
   1701 	}
   1702 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
   1703 	if (!m0)
   1704 		return ENOBUFS;
   1705 	p = mtod(m0, uint8_t *);
   1706 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
   1707 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
   1708 	if (sc->sc_service_name != NULL) {
   1709 		PPPOE_ADD_16(p, l1);
   1710 		memcpy(p, sc->sc_service_name, l1);
   1711 		p += l1;
   1712 	} else {
   1713 		PPPOE_ADD_16(p, 0);
   1714 	}
   1715 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   1716 	PPPOE_ADD_16(p, sc->sc_hunique_len);
   1717 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
   1718 	return pppoe_output(sc, m0);
   1719 }
   1720 #endif
   1721 
   1722 static void
   1723 pppoe_tls(struct sppp *sp)
   1724 {
   1725 	struct pppoe_softc *sc = (void *)sp;
   1726 	int wtime;
   1727 
   1728 	PPPOE_LOCK(sc, RW_READER);
   1729 
   1730 	if (sc->sc_state != PPPOE_STATE_INITIAL) {
   1731 		PPPOE_UNLOCK(sc);
   1732 		return;
   1733 	}
   1734 
   1735 	if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
   1736 	    sc->sc_sppp.pp_auth_failures > 0) {
   1737 		/*
   1738 		 * Delay trying to reconnect a bit more - the peer
   1739 		 * might have failed to contact its radius server.
   1740 		 */
   1741 		wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
   1742 		if (wtime > PPPOE_SLOW_RETRY)
   1743 			wtime = PPPOE_SLOW_RETRY;
   1744 	} else {
   1745 		wtime = PPPOE_RECON_IMMEDIATE;
   1746 	}
   1747 	callout_reset(&sc->sc_timeout, wtime, pppoe_timeout, sc);
   1748 
   1749 	PPPOE_UNLOCK(sc);
   1750 }
   1751 
   1752 static void
   1753 pppoe_tlf(struct sppp *sp)
   1754 {
   1755 	struct pppoe_softc *sc = (void *)sp;
   1756 
   1757 	PPPOE_LOCK(sc, RW_WRITER);
   1758 
   1759 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1760 		PPPOE_UNLOCK(sc);
   1761 		return;
   1762 	}
   1763 	/*
   1764 	 * Do not call pppoe_disconnect here, the upper layer state
   1765 	 * machine gets confused by this. We must return from this
   1766 	 * function and defer disconnecting to the timeout handler.
   1767 	 */
   1768 	sc->sc_state = PPPOE_STATE_CLOSING;
   1769 
   1770 	callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc);
   1771 
   1772 	PPPOE_UNLOCK(sc);
   1773 }
   1774 
   1775 static void
   1776 pppoe_start(struct ifnet *ifp)
   1777 {
   1778 	struct pppoe_softc *sc = (void *)ifp;
   1779 	struct mbuf *m;
   1780 	uint8_t *p;
   1781 	size_t len;
   1782 
   1783 	if (sppp_isempty(ifp))
   1784 		return;
   1785 
   1786 	/* are we ready to process data yet? */
   1787 	PPPOE_LOCK(sc, RW_READER);
   1788 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1789 		sppp_flush(&sc->sc_sppp.pp_if);
   1790 		PPPOE_UNLOCK(sc);
   1791 		return;
   1792 	}
   1793 
   1794 	while ((m = sppp_dequeue(ifp)) != NULL) {
   1795 		len = m->m_pkthdr.len;
   1796 		M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
   1797 		if (m == NULL) {
   1798 			ifp->if_oerrors++;
   1799 			continue;
   1800 		}
   1801 		p = mtod(m, uint8_t *);
   1802 		PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
   1803 
   1804 		bpf_mtap(&sc->sc_sppp.pp_if, m);
   1805 
   1806 		pppoe_output(sc, m);
   1807 	}
   1808 	PPPOE_UNLOCK(sc);
   1809 }
   1810 
   1811 #ifdef PPPOE_MPSAFE
   1812 static int
   1813 pppoe_transmit(struct ifnet *ifp, struct mbuf *m)
   1814 {
   1815 	struct pppoe_softc *sc = (void *)ifp;
   1816 	uint8_t *p;
   1817 	size_t len;
   1818 
   1819 	if (m == NULL)
   1820 		return EINVAL;
   1821 
   1822 	/* are we ready to process data yet? */
   1823 	PPPOE_LOCK(sc, RW_READER);
   1824 	if (sc->sc_state < PPPOE_STATE_SESSION) {
   1825 		PPPOE_UNLOCK(sc);
   1826 		m_freem(m);
   1827 		return ENOBUFS;
   1828 	}
   1829 
   1830 	len = m->m_pkthdr.len;
   1831 	M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
   1832 	if (m == NULL) {
   1833 		PPPOE_UNLOCK(sc);
   1834 		ifp->if_oerrors++;
   1835 		return ENETDOWN;
   1836 	}
   1837 	p = mtod(m, uint8_t *);
   1838 	PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
   1839 
   1840 	bpf_mtap(&sc->sc_sppp.pp_if, m);
   1841 
   1842 	pppoe_output(sc, m);
   1843 	PPPOE_UNLOCK(sc);
   1844 	return 0;
   1845 }
   1846 #endif /* PPPOE_MPSAFE */
   1847 
   1848 static void
   1849 pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2)
   1850 {
   1851 	struct ifnet *ifp = arg2;
   1852 	struct pppoe_softc *sc;
   1853 	DECLARE_SPLNET_VARIABLE;
   1854 
   1855 	if (cmd != PFIL_IFNET_DETACH)
   1856 		return;
   1857 
   1858 	ACQUIRE_SPLNET();
   1859 	rw_enter(&pppoe_softc_list_lock, RW_READER);
   1860 	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
   1861 		PPPOE_LOCK(sc, RW_WRITER);
   1862 		if (sc->sc_eth_if != ifp) {
   1863 			PPPOE_UNLOCK(sc);
   1864 			continue;
   1865 		}
   1866 		if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
   1867 			sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
   1868 			printf("%s: ethernet interface detached, going down\n",
   1869 			    sc->sc_sppp.pp_if.if_xname);
   1870 		}
   1871 		sc->sc_eth_if = NULL;
   1872 		pppoe_clear_softc(sc, "ethernet interface detached");
   1873 		PPPOE_UNLOCK(sc);
   1874 	}
   1875 	rw_exit(&pppoe_softc_list_lock);
   1876 	RELEASE_SPLNET();
   1877 }
   1878 
   1879 static void
   1880 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
   1881 {
   1882 	KASSERT(PPPOE_WLOCKED(sc));
   1883 
   1884 	/* stop timer */
   1885 	callout_stop(&sc->sc_timeout);
   1886 	if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
   1887 		printf("%s: session 0x%x terminated, %s\n",
   1888 		    sc->sc_sppp.pp_if.if_xname, sc->sc_session, message);
   1889 
   1890 	/* fix our state */
   1891 	sc->sc_state = PPPOE_STATE_INITIAL;
   1892 
   1893 	PPPOE_UNLOCK(sc);
   1894 
   1895 	/* signal upper layer */
   1896 	sc->sc_sppp.pp_down(&sc->sc_sppp);
   1897 
   1898 	PPPOE_LOCK(sc, RW_WRITER);
   1899 
   1900 	/* clean up softc */
   1901 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
   1902 	if (sc->sc_ac_cookie) {
   1903 		free(sc->sc_ac_cookie, M_DEVBUF);
   1904 		sc->sc_ac_cookie = NULL;
   1905 	}
   1906 	if (sc->sc_relay_sid) {
   1907 		free(sc->sc_relay_sid, M_DEVBUF);
   1908 		sc->sc_relay_sid = NULL;
   1909 	}
   1910 	sc->sc_ac_cookie_len = 0;
   1911 	sc->sc_session = 0;
   1912 }
   1913 
   1914 static void
   1915 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
   1916 {
   1917 	if (m->m_flags & M_PROMISC) {
   1918 		m_freem(m);
   1919 		return;
   1920 	}
   1921 
   1922 #ifndef PPPOE_SERVER
   1923 	if (m->m_flags & (M_MCAST | M_BCAST)) {
   1924 		m_freem(m);
   1925 		return;
   1926 	}
   1927 #endif
   1928 
   1929 	IFQ_LOCK(inq);
   1930 	if (IF_QFULL(inq)) {
   1931 		IF_DROP(inq);
   1932 		IFQ_UNLOCK(inq);
   1933 		m_freem(m);
   1934 	} else {
   1935 		IF_ENQUEUE(inq, m);
   1936 		IFQ_UNLOCK(inq);
   1937 		softint_schedule(pppoe_softintr);
   1938 	}
   1939 	return;
   1940 }
   1941 
   1942 void
   1943 pppoe_input(struct ifnet *ifp, struct mbuf *m)
   1944 {
   1945 	pppoe_enqueue(&ppoeinq, m);
   1946 	return;
   1947 }
   1948 
   1949 void
   1950 pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
   1951 {
   1952 	pppoe_enqueue(&ppoediscinq, m);
   1953 	return;
   1954 }
   1955 
   1956 static void
   1957 sysctl_net_pppoe_setup(struct sysctllog **clog)
   1958 {
   1959 	const struct sysctlnode *node = NULL;
   1960 
   1961 	sysctl_createv(clog, 0, NULL, &node,
   1962 	    CTLFLAG_PERMANENT,
   1963 	    CTLTYPE_NODE, "pppoe",
   1964 	    SYSCTL_DESCR("PPPOE protocol"),
   1965 	    NULL, 0, NULL, 0,
   1966 	    CTL_NET, CTL_CREATE, CTL_EOL);
   1967 
   1968 	if (node == NULL)
   1969 		return;
   1970 
   1971 	sysctl_createv(clog, 0, &node, NULL,
   1972 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1973 	    CTLTYPE_BOOL, "term_unknown",
   1974 	    SYSCTL_DESCR("Terminate unknown sessions"),
   1975 	    NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown),
   1976 	    CTL_CREATE, CTL_EOL);
   1977 }
   1978 
   1979 /*
   1980  * Module infrastructure
   1981  */
   1982 #include "if_module.h"
   1983 
   1984 IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")
   1985