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