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