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