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