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