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