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