xref: /netbsd-src/sys/net/if_pppoe.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* $NetBSD: if_pppoe.c,v 1.137 2018/05/03 16:52:42 maxv Exp $ */
2 
3 /*-
4  * Copyright (c) 2002, 2008 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@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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.137 2018/05/03 16:52:42 maxv Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "pppoe.h"
37 #include "opt_pppoe.h"
38 #include "opt_net_mpsafe.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/proc.h>
49 #include <sys/ioctl.h>
50 #include <sys/kauth.h>
51 #include <sys/intr.h>
52 #include <sys/socketvar.h>
53 #include <sys/device.h>
54 #include <sys/module.h>
55 #include <sys/sysctl.h>
56 #include <sys/rwlock.h>
57 #include <sys/mutex.h>
58 #include <sys/psref.h>
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/if_ether.h>
63 #include <net/if_sppp.h>
64 #include <net/if_spppvar.h>
65 #include <net/if_pppoe.h>
66 #include <net/if_dl.h>
67 
68 #include <net/bpf.h>
69 
70 #include "ioconf.h"
71 
72 #ifdef NET_MPSAFE
73 #define PPPOE_MPSAFE	1
74 #endif
75 
76 struct pppoehdr {
77 	uint8_t vertype;
78 	uint8_t code;
79 	uint16_t session;
80 	uint16_t plen;
81 } __packed;
82 
83 struct pppoetag {
84 	uint16_t tag;
85 	uint16_t len;
86 } __packed;
87 
88 #define	PPPOE_HEADERLEN	sizeof(struct pppoehdr)
89 #define	PPPOE_OVERHEAD	(PPPOE_HEADERLEN + 2)
90 #define	PPPOE_VERTYPE	0x11	/* VER=1, TYPE = 1 */
91 
92 #define	PPPOE_TAG_EOL		0x0000		/* end of list */
93 #define	PPPOE_TAG_SNAME		0x0101		/* service name */
94 #define	PPPOE_TAG_ACNAME	0x0102		/* access concentrator name */
95 #define	PPPOE_TAG_HUNIQUE	0x0103		/* host unique */
96 #define	PPPOE_TAG_ACCOOKIE	0x0104		/* AC cookie */
97 #define	PPPOE_TAG_VENDOR	0x0105		/* vendor specific */
98 #define	PPPOE_TAG_RELAYSID	0x0110		/* relay session id */
99 #define	PPPOE_TAG_MAX_PAYLOAD	0x0120		/* max payload */
100 #define	PPPOE_TAG_SNAME_ERR	0x0201		/* service name error */
101 #define	PPPOE_TAG_ACSYS_ERR	0x0202		/* AC system error */
102 #define	PPPOE_TAG_GENERIC_ERR	0x0203		/* generic error */
103 
104 #define	PPPOE_CODE_PADI		0x09		/* Active Discovery Initiation */
105 #define	PPPOE_CODE_PADO		0x07		/* Active Discovery Offer */
106 #define	PPPOE_CODE_PADR		0x19		/* Active Discovery Request */
107 #define	PPPOE_CODE_PADS		0x65		/* Active Discovery Session confirmation */
108 #define	PPPOE_CODE_PADT		0xA7		/* Active Discovery Terminate */
109 
110 /* two byte PPP protocol discriminator, then IP data */
111 #define	PPPOE_MAXMTU	(ETHERMTU - PPPOE_OVERHEAD)
112 
113 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
114 #define	PPPOE_ADD_16(PTR, VAL)			\
115 		*(PTR)++ = (VAL) / 256;		\
116 		*(PTR)++ = (VAL) % 256
117 
118 /* Add a complete PPPoE header to the buffer pointed to by PTR */
119 #define	PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)	\
120 		*(PTR)++ = PPPOE_VERTYPE;	\
121 		*(PTR)++ = (CODE);		\
122 		PPPOE_ADD_16(PTR, SESS);	\
123 		PPPOE_ADD_16(PTR, LEN)
124 
125 #define	PPPOE_DISC_TIMEOUT	(hz*5)	/* base for quick timeout calculation */
126 #define	PPPOE_SLOW_RETRY	(hz*60)	/* persistent retry interval */
127 #define	PPPOE_RECON_FAST	(hz*15)	/* first retry after auth failure */
128 #define	PPPOE_RECON_IMMEDIATE	(hz/10)	/* "no delay" reconnect */
129 #define	PPPOE_DISC_MAXPADI	4	/* retry PADI four times (quickly) */
130 #define	PPPOE_DISC_MAXPADR	2	/* retry PADR twice */
131 
132 #ifdef PPPOE_SERVER
133 /* from if_spppsubr.c */
134 #define	IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
135 #endif
136 
137 #define PPPOE_LOCK(_sc, _op)	rw_enter(&(_sc)->sc_lock, (_op))
138 #define PPPOE_UNLOCK(_sc)	rw_exit(&(_sc)->sc_lock)
139 #define PPPOE_WLOCKED(_sc)	rw_write_held(&(_sc)->sc_lock)
140 
141 #ifdef PPPOE_MPSAFE
142 #define DECLARE_SPLNET_VARIABLE
143 #define ACQUIRE_SPLNET()	do { } while (0)
144 #define RELEASE_SPLNET()	do { } while (0)
145 #else
146 #define DECLARE_SPLNET_VARIABLE	int __s
147 #define ACQUIRE_SPLNET()	do {					\
148 					__s = splnet();			\
149 				} while (0)
150 #define RELEASE_SPLNET()	do {					\
151 					splx(__s);			\
152 				} while (0)
153 #endif
154 
155 struct pppoe_softc {
156 	struct sppp sc_sppp;		/* contains a struct ifnet as first element */
157 	LIST_ENTRY(pppoe_softc) sc_list;
158 	struct ifnet *sc_eth_if;	/* ethernet interface we are using */
159 
160 	int sc_state;			/* discovery phase or session connected */
161 	struct ether_addr sc_dest;	/* hardware address of concentrator */
162 	uint16_t sc_session;		/* PPPoE session id */
163 
164 	char *sc_service_name;		/* if != NULL: requested name of service */
165 	char *sc_concentrator_name;	/* if != NULL: requested concentrator id */
166 	uint8_t *sc_ac_cookie;		/* content of AC cookie we must echo back */
167 	size_t sc_ac_cookie_len;	/* length of cookie data */
168 	uint8_t *sc_relay_sid;		/* content of relay SID we must echo back */
169 	size_t sc_relay_sid_len;	/* length of relay SID data */
170 #ifdef PPPOE_SERVER
171 	uint8_t *sc_hunique;		/* content of host unique we must echo back */
172 	size_t sc_hunique_len;		/* length of host unique */
173 #endif
174 	callout_t sc_timeout;	/* timeout while not in session state */
175 	int sc_padi_retried;		/* number of PADI retries already done */
176 	int sc_padr_retried;		/* number of PADR retries already done */
177 	krwlock_t sc_lock;	/* lock of sc_state, sc_session, and sc_eth_if */
178 };
179 
180 /* incoming traffic will be queued here */
181 struct ifqueue ppoediscinq = { .ifq_maxlen = IFQ_MAXLEN };
182 struct ifqueue ppoeinq = { .ifq_maxlen = IFQ_MAXLEN };
183 
184 void *pppoe_softintr = NULL;
185 static void pppoe_softintr_handler(void *);
186 
187 extern int sppp_ioctl(struct ifnet *, unsigned long, void *);
188 
189 /* input routines */
190 static void pppoeintr(void);
191 static void pppoe_disc_input(struct mbuf *);
192 static void pppoe_dispatch_disc_pkt(struct mbuf *, int);
193 static void pppoe_data_input(struct mbuf *);
194 static void pppoe_enqueue(struct ifqueue *, struct mbuf *);
195 
196 /* management routines */
197 static int pppoe_connect(struct pppoe_softc *);
198 static int pppoe_disconnect(struct pppoe_softc *);
199 static void pppoe_abort_connect(struct pppoe_softc *);
200 static int pppoe_ioctl(struct ifnet *, unsigned long, void *);
201 static void pppoe_tls(struct sppp *);
202 static void pppoe_tlf(struct sppp *);
203 static void pppoe_start(struct ifnet *);
204 #ifdef PPPOE_MPSAFE
205 static int pppoe_transmit(struct ifnet *, struct mbuf *);
206 #endif
207 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
208 
209 /* internal timeout handling */
210 static void pppoe_timeout(void *);
211 
212 /* sending actual protocol controll packets */
213 static int pppoe_send_padi(struct pppoe_softc *);
214 static int pppoe_send_padr(struct pppoe_softc *);
215 #ifdef PPPOE_SERVER
216 static int pppoe_send_pado(struct pppoe_softc *);
217 static int pppoe_send_pads(struct pppoe_softc *);
218 #endif
219 static int pppoe_send_padt(struct ifnet *, u_int, const uint8_t *);
220 
221 /* raw output */
222 static int pppoe_output(struct pppoe_softc *, struct mbuf *);
223 
224 /* internal helper functions */
225 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *,
226     krw_t);
227 static struct pppoe_softc * pppoe_find_softc_by_hunique(uint8_t *, size_t,
228     struct ifnet *, krw_t);
229 static struct mbuf *pppoe_get_mbuf(size_t len);
230 
231 static void pppoe_ifattach_hook(void *, unsigned long, void *);
232 
233 static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
234 static krwlock_t pppoe_softc_list_lock;
235 
236 static int	pppoe_clone_create(struct if_clone *, int);
237 static int	pppoe_clone_destroy(struct ifnet *);
238 
239 static bool	pppoe_term_unknown = false;
240 static int	pppoe_term_unknown_pps = 1;
241 
242 static struct sysctllog	*pppoe_sysctl_clog;
243 static void sysctl_net_pppoe_setup(struct sysctllog **);
244 
245 static struct if_clone pppoe_cloner =
246     IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy);
247 
248 /* ARGSUSED */
249 void
250 pppoeattach(int count)
251 {
252 
253 	/*
254 	 * Nothing to do here, initialization is handled by the
255 	 * module initialization code in pppoeinit() below).
256 	 */
257 }
258 
259 static void
260 pppoeinit(void)
261 {
262 
263 	LIST_INIT(&pppoe_softc_list);
264 	rw_init(&pppoe_softc_list_lock);
265 	if_clone_attach(&pppoe_cloner);
266 
267 	pppoe_softintr = softint_establish(SOFTINT_MPSAFE|SOFTINT_NET,
268 	    pppoe_softintr_handler, NULL);
269 	sysctl_net_pppoe_setup(&pppoe_sysctl_clog);
270 
271 	IFQ_LOCK_INIT(&ppoediscinq);
272 	IFQ_LOCK_INIT(&ppoeinq);
273 }
274 
275 static int
276 pppoedetach(void)
277 {
278 	int error = 0;
279 
280 	if (!LIST_EMPTY(&pppoe_softc_list))
281 		error = EBUSY;
282 
283 	if (error == 0) {
284 		if_clone_detach(&pppoe_cloner);
285 		softint_disestablish(pppoe_softintr);
286 		/* Remove our sysctl sub-tree */
287 		sysctl_teardown(&pppoe_sysctl_clog);
288 	}
289 
290 	return error;
291 }
292 
293 static int
294 pppoe_clone_create(struct if_clone *ifc, int unit)
295 {
296 	struct pppoe_softc *sc;
297 	int rv;
298 
299 	sc = malloc(sizeof(struct pppoe_softc), M_DEVBUF, M_WAITOK|M_ZERO);
300 
301 	if_initname(&sc->sc_sppp.pp_if, "pppoe", unit);
302 	sc->sc_sppp.pp_if.if_softc = sc;
303 	sc->sc_sppp.pp_if.if_mtu = PPPOE_MAXMTU;
304 	sc->sc_sppp.pp_if.if_flags = IFF_SIMPLEX|IFF_POINTOPOINT|IFF_MULTICAST;
305 #ifdef PPPOE_MPSAFE
306 	sc->sc_sppp.pp_if.if_extflags = IFEF_MPSAFE;
307 #endif
308 	sc->sc_sppp.pp_if.if_type = IFT_PPP;
309 	sc->sc_sppp.pp_if.if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN;
310 	sc->sc_sppp.pp_if.if_dlt = DLT_PPP_ETHER;
311 	sc->sc_sppp.pp_flags |= PP_KEEPALIVE |	/* use LCP keepalive */
312 				PP_NOFRAMING;	/* no serial encapsulation */
313 	sc->sc_sppp.pp_if.if_ioctl = pppoe_ioctl;
314 	IFQ_SET_MAXLEN(&sc->sc_sppp.pp_if.if_snd, IFQ_MAXLEN);
315 	IFQ_SET_READY(&sc->sc_sppp.pp_if.if_snd);
316 
317 	/* changed to real address later */
318 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
319 
320 	callout_init(&sc->sc_timeout, CALLOUT_MPSAFE);
321 
322 	sc->sc_sppp.pp_if.if_start = pppoe_start;
323 #ifdef PPPOE_MPSAFE
324 	sc->sc_sppp.pp_if.if_transmit = pppoe_transmit;
325 #endif
326 	sc->sc_sppp.pp_tls = pppoe_tls;
327 	sc->sc_sppp.pp_tlf = pppoe_tlf;
328 	sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN;	/* framing added to ppp packets */
329 
330 	rv = if_initialize(&sc->sc_sppp.pp_if);
331 	if (rv != 0) {
332 		callout_halt(&sc->sc_timeout, NULL);
333 		callout_destroy(&sc->sc_timeout);
334 		free(sc, M_DEVBUF);
335 		return rv;
336 	}
337 	sc->sc_sppp.pp_if.if_percpuq = if_percpuq_create(&sc->sc_sppp.pp_if);
338 	sppp_attach(&sc->sc_sppp.pp_if);
339 	if_register(&sc->sc_sppp.pp_if);
340 
341 	bpf_attach(&sc->sc_sppp.pp_if, DLT_PPP_ETHER, 0);
342 	if (LIST_EMPTY(&pppoe_softc_list)) {
343 		pfil_add_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil);
344 	}
345 
346 	rw_init(&sc->sc_lock);
347 
348 	rw_enter(&pppoe_softc_list_lock, RW_WRITER);
349 	LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
350 	rw_exit(&pppoe_softc_list_lock);
351 	return 0;
352 }
353 
354 static int
355 pppoe_clone_destroy(struct ifnet *ifp)
356 {
357 	struct pppoe_softc * sc = ifp->if_softc;
358 
359 	rw_enter(&pppoe_softc_list_lock, RW_WRITER);
360 
361 	PPPOE_LOCK(sc, RW_WRITER);
362 	callout_halt(&sc->sc_timeout, NULL);
363 
364 	LIST_REMOVE(sc, sc_list);
365 
366 	if (LIST_EMPTY(&pppoe_softc_list)) {
367 		pfil_remove_ihook(pppoe_ifattach_hook, NULL, PFIL_IFNET, if_pfil);
368 	}
369 	rw_exit(&pppoe_softc_list_lock);
370 
371 	bpf_detach(ifp);
372 	sppp_detach(&sc->sc_sppp.pp_if);
373 	if_detach(ifp);
374 	if (sc->sc_concentrator_name)
375 		free(sc->sc_concentrator_name, M_DEVBUF);
376 	if (sc->sc_service_name)
377 		free(sc->sc_service_name, M_DEVBUF);
378 	if (sc->sc_ac_cookie)
379 		free(sc->sc_ac_cookie, M_DEVBUF);
380 	if (sc->sc_relay_sid)
381 		free(sc->sc_relay_sid, M_DEVBUF);
382 	callout_destroy(&sc->sc_timeout);
383 
384 	PPPOE_UNLOCK(sc);
385 	rw_destroy(&sc->sc_lock);
386 
387 	free(sc, M_DEVBUF);
388 
389 	return 0;
390 }
391 
392 /*
393  * Find the interface handling the specified session.
394  * Note: O(number of sessions open), this is a client-side only, mean
395  * and lean implementation, so number of open sessions typically should
396  * be 1.
397  */
398 static struct pppoe_softc *
399 pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif, krw_t lock)
400 {
401 	struct pppoe_softc *sc = NULL;
402 
403 	if (session == 0)
404 		return NULL;
405 	rw_enter(&pppoe_softc_list_lock, RW_READER);
406 	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
407 		PPPOE_LOCK(sc, lock);
408 		if ( sc->sc_state == PPPOE_STATE_SESSION
409 		    && sc->sc_session == session
410 		    && sc->sc_eth_if == rcvif)
411 			break;
412 
413 		PPPOE_UNLOCK(sc);
414 	}
415 	rw_exit(&pppoe_softc_list_lock);
416 	return sc;
417 }
418 
419 /* Check host unique token passed and return appropriate softc pointer,
420  * or NULL if token is bogus. */
421 static struct pppoe_softc *
422 pppoe_find_softc_by_hunique(uint8_t *token, size_t len,
423     struct ifnet *rcvif, krw_t lock)
424 {
425 	struct pppoe_softc *sc, *t;
426 
427 	if (LIST_EMPTY(&pppoe_softc_list))
428 		return NULL;
429 
430 	if (len != sizeof sc)
431 		return NULL;
432 	memcpy(&t, token, len);
433 
434 	rw_enter(&pppoe_softc_list_lock, RW_READER);
435 	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
436 		if (sc == t) {
437 			PPPOE_LOCK(sc, lock);
438 			break;
439 		}
440 	}
441 	rw_exit(&pppoe_softc_list_lock);
442 
443 	if (sc == NULL) {
444 #ifdef PPPOE_DEBUG
445 		printf("pppoe: alien host unique tag, no session found\n");
446 #endif
447 		return NULL;
448 	}
449 
450 	/* should be safe to access *sc now */
451 	if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
452 		printf("%s: host unique tag found, but it belongs to a connection in state %d\n",
453 			sc->sc_sppp.pp_if.if_xname, sc->sc_state);
454 		PPPOE_UNLOCK(sc);
455 		return NULL;
456 	}
457 	if (sc->sc_eth_if != rcvif) {
458 		printf("%s: wrong interface, not accepting host unique\n",
459 			sc->sc_sppp.pp_if.if_xname);
460 		PPPOE_UNLOCK(sc);
461 		return NULL;
462 	}
463 	return sc;
464 }
465 
466 static void
467 pppoe_softintr_handler(void *dummy)
468 {
469 	/* called at splsoftnet() */
470 	pppoeintr();
471 }
472 
473 /* called at appropriate protection level */
474 static void
475 pppoeintr(void)
476 {
477 	struct mbuf *m;
478 	int disc_done, data_done;
479 
480 	SOFTNET_LOCK_UNLESS_NET_MPSAFE();
481 
482 	do {
483 		disc_done = 0;
484 		data_done = 0;
485 		for (;;) {
486 			IFQ_LOCK(&ppoediscinq);
487 			IF_DEQUEUE(&ppoediscinq, m);
488 			IFQ_UNLOCK(&ppoediscinq);
489 			if (m == NULL) break;
490 			disc_done = 1;
491 			pppoe_disc_input(m);
492 		}
493 
494 		for (;;) {
495 			IFQ_LOCK(&ppoeinq);
496 			IF_DEQUEUE(&ppoeinq, m);
497 			IFQ_UNLOCK(&ppoeinq);
498 			if (m == NULL) break;
499 			data_done = 1;
500 			pppoe_data_input(m);
501 		}
502 	} while (disc_done || data_done);
503 
504 	SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();
505 }
506 
507 /* analyze and handle a single received packet while not in session state */
508 static void
509 pppoe_dispatch_disc_pkt(struct mbuf *m, int off)
510 {
511 	uint16_t tag, len;
512 	uint16_t session, plen;
513 	struct pppoe_softc *sc;
514 	const char *err_msg;
515 	char devname[IF_NAMESIZE];
516 	char *error;
517 	uint8_t *ac_cookie;
518 	size_t ac_cookie_len;
519 	uint8_t *relay_sid;
520 	size_t relay_sid_len;
521 #ifdef PPPOE_SERVER
522 	uint8_t *hunique;
523 	size_t hunique_len;
524 #endif
525 	struct pppoehdr *ph;
526 	struct pppoetag *pt;
527 	struct mbuf *n;
528 	int noff, err, errortag;
529 	struct ether_header *eh;
530 
531 	/* as long as we don't know which instance */
532 	strlcpy(devname, "pppoe", sizeof(devname));
533 
534 	err_msg = NULL;
535 	errortag = 0;
536 	if (m->m_len < sizeof(*eh)) {
537 		m = m_pullup(m, sizeof(*eh));
538 		if (!m)
539 			goto done;
540 	}
541 	eh = mtod(m, struct ether_header *);
542 	off += sizeof(*eh);
543 
544 	ac_cookie = NULL;
545 	ac_cookie_len = 0;
546 	relay_sid = NULL;
547 	relay_sid_len = 0;
548 #ifdef PPPOE_SERVER
549 	hunique = NULL;
550 	hunique_len = 0;
551 #endif
552 	session = 0;
553 	if (m->m_pkthdr.len - off <= PPPOE_HEADERLEN) {
554 		printf("pppoe: packet too short: %d\n", m->m_pkthdr.len);
555 		goto done;
556 	}
557 
558 	n = m_pulldown(m, off, sizeof(*ph), &noff);
559 	if (!n) {
560 		printf("pppoe: could not get PPPoE header\n");
561 		m = NULL;
562 		goto done;
563 	}
564 	ph = (struct pppoehdr *)(mtod(n, char *) + noff);
565 	if (ph->vertype != PPPOE_VERTYPE) {
566 		printf("pppoe: unknown version/type packet: 0x%x\n",
567 		    ph->vertype);
568 		goto done;
569 	}
570 	session = ntohs(ph->session);
571 	plen = ntohs(ph->plen);
572 	off += sizeof(*ph);
573 
574 	if (plen + off > m->m_pkthdr.len) {
575 		printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
576 		    m->m_pkthdr.len - off, plen);
577 		goto done;
578 	}
579 	m_adj(m, off + plen - m->m_pkthdr.len);	/* ignore trailing garbage */
580 	tag = 0;
581 	len = 0;
582 	sc = NULL;
583 	while (off + sizeof(*pt) <= m->m_pkthdr.len) {
584 		n = m_pulldown(m, off, sizeof(*pt), &noff);
585 		if (!n) {
586 			printf("%s: parse error\n", devname);
587 			m = NULL;
588 			goto done;
589 		}
590 		pt = (struct pppoetag *)(mtod(n, char *) + noff);
591 		tag = ntohs(pt->tag);
592 		len = ntohs(pt->len);
593 		if (off + len + sizeof(*pt) > m->m_pkthdr.len) {
594 			printf("pppoe: tag 0x%x len 0x%x is too long\n",
595 			    tag, len);
596 			goto done;
597 		}
598 		switch (tag) {
599 		case PPPOE_TAG_EOL:
600 			goto breakbreak;
601 		case PPPOE_TAG_SNAME:
602 			break;	/* ignored */
603 		case PPPOE_TAG_ACNAME:
604 			error = NULL;
605 			if (sc != NULL && len > 0) {
606 				error = malloc(len + 1, M_TEMP, M_NOWAIT);
607 				if (error == NULL)
608 					break;
609 
610 				n = m_pulldown(m, off + sizeof(*pt), len,
611 				    &noff);
612 				if (!n) {
613 					m = NULL;
614 					free(error, M_TEMP);
615 					goto done;
616 				}
617 
618 				strlcpy(error, mtod(n, char*) + noff, len + 1);
619 				printf("%s: connected to %s\n", devname, error);
620 				free(error, M_TEMP);
621 			}
622 			break;	/* ignored */
623 		case PPPOE_TAG_HUNIQUE: {
624 			struct ifnet *rcvif;
625 			struct psref psref;
626 
627 			if (sc != NULL)
628 				break;
629 			n = m_pulldown(m, off + sizeof(*pt), len, &noff);
630 			if (!n) {
631 				m = NULL;
632 				err_msg = "TAG HUNIQUE ERROR";
633 				break;
634 			}
635 #ifdef PPPOE_SERVER
636 			hunique = mtod(n, uint8_t *) + noff;
637 			hunique_len = len;
638 #endif
639 			rcvif = m_get_rcvif_psref(m, &psref);
640 			if (rcvif != NULL) {
641 				sc = pppoe_find_softc_by_hunique(
642 					mtod(n, char *) + noff, len, rcvif,
643 					RW_READER);
644 			}
645 			m_put_rcvif_psref(rcvif, &psref);
646 			if (sc != NULL) {
647 				strlcpy(devname, sc->sc_sppp.pp_if.if_xname,
648 				    sizeof(devname));
649 				PPPOE_UNLOCK(sc);
650 			}
651 			break;
652 		}
653 		case PPPOE_TAG_ACCOOKIE:
654 			if (ac_cookie == NULL) {
655 				n = m_pulldown(m, off + sizeof(*pt), len,
656 				    &noff);
657 				if (!n) {
658 					err_msg = "TAG ACCOOKIE ERROR";
659 					m = NULL;
660 					break;
661 				}
662 				ac_cookie = mtod(n, char *) + noff;
663 				ac_cookie_len = len;
664 			}
665 			break;
666 		case PPPOE_TAG_RELAYSID:
667 			if (relay_sid == NULL) {
668 				n = m_pulldown(m, off + sizeof(*pt), len,
669 				    &noff);
670 				if (!n) {
671 					err_msg = "TAG RELAYSID ERROR";
672 					m = NULL;
673 					break;
674 				}
675 				relay_sid = mtod(n, char *) + noff;
676 				relay_sid_len = len;
677 			}
678 			break;
679 		case PPPOE_TAG_SNAME_ERR:
680 			err_msg = "SERVICE NAME ERROR";
681 			errortag = 1;
682 			break;
683 		case PPPOE_TAG_ACSYS_ERR:
684 			err_msg = "AC SYSTEM ERROR";
685 			errortag = 1;
686 			break;
687 		case PPPOE_TAG_GENERIC_ERR:
688 			err_msg = "GENERIC ERROR";
689 			errortag = 1;
690 			break;
691 		}
692 		if (err_msg) {
693 			error = NULL;
694 			if (errortag && len) {
695 				error = malloc(len + 1, M_TEMP,
696 				    M_NOWAIT|M_ZERO);
697 				n = m_pulldown(m, off + sizeof(*pt), len,
698 				    &noff);
699 				if (!n) {
700 					m = NULL;
701 				} else if (error) {
702 					strlcpy(error, mtod(n, char *) + noff,
703 					    len + 1);
704 				}
705 			}
706 			if (error) {
707 				printf("%s: %s: %s\n", devname,
708 				    err_msg, error);
709 				free(error, M_TEMP);
710 			} else
711 				printf("%s: %s\n", devname, err_msg);
712 			if (errortag || m == NULL)
713 				goto done;
714 		}
715 		off += sizeof(*pt) + len;
716 	}
717 breakbreak:;
718 	switch (ph->code) {
719 	case PPPOE_CODE_PADI:
720 #ifdef PPPOE_SERVER
721 		/*
722 		 * got service name, concentrator name, and/or host unique.
723 		 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
724 		 */
725 		if (LIST_EMPTY(&pppoe_softc_list))
726 			goto done;
727 		rw_enter(&pppoe_softc_list_lock, RW_READER);
728 		LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
729 			PPPOE_LOCK(sc, RW_WRITER);
730 			if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
731 				PPPOE_UNLOCK(sc);
732 				continue;
733 			}
734 			if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
735 				PPPOE_UNLOCK(sc);
736 				continue;
737 			}
738 
739 			if (sc->sc_state == PPPOE_STATE_INITIAL)
740 				break;
741 
742 			PPPOE_UNLOCK(sc);
743 		}
744 		rw_exit(&pppoe_softc_list_lock);
745 
746 		if (sc == NULL) {
747 /*			printf("pppoe: free passive interface is not found\n");*/
748 			goto done;
749 		}
750 
751 		if (hunique) {
752 			if (sc->sc_hunique)
753 				free(sc->sc_hunique, M_DEVBUF);
754 			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
755 			    M_DONTWAIT);
756 			if (sc->sc_hunique == NULL) {
757 				PPPOE_UNLOCK(sc);
758 				goto done;
759 			}
760 			sc->sc_hunique_len = hunique_len;
761 			memcpy(sc->sc_hunique, hunique, hunique_len);
762 		}
763 		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
764 		sc->sc_state = PPPOE_STATE_PADO_SENT;
765 		pppoe_send_pado(sc);
766 		PPPOE_UNLOCK(sc);
767 		break;
768 #endif /* PPPOE_SERVER */
769 	case PPPOE_CODE_PADR:
770 #ifdef PPPOE_SERVER
771 	    {
772 		struct ifnet *rcvif;
773 		struct psref psref;
774 		/*
775 		 * get sc from ac_cookie if IFF_PASSIVE
776 		 */
777 		if (ac_cookie == NULL) {
778 			/* be quiet if there is not a single pppoe instance */
779 			printf("pppoe: received PADR but not includes ac_cookie\n");
780 			goto done;
781 		}
782 
783 		rcvif = m_get_rcvif_psref(m, &psref);
784 		if (__predict_true(rcvif != NULL)) {
785 			sc = pppoe_find_softc_by_hunique(ac_cookie,
786 							 ac_cookie_len,
787 							 rcvif,
788 							 RW_WRITER);
789 		}
790 		m_put_rcvif_psref(rcvif, &psref);
791 		if (sc == NULL) {
792 			/* be quiet if there is not a single pppoe instance */
793 			if (!LIST_EMPTY(&pppoe_softc_list))
794 				printf("pppoe: received PADR but could not find request for it\n");
795 			goto done;
796 		}
797 
798 		if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
799 			printf("%s: received unexpected PADR\n",
800 			    sc->sc_sppp.pp_if.if_xname);
801 			PPPOE_UNLOCK(sc);
802 			goto done;
803 		}
804 
805 		if (hunique) {
806 			if (sc->sc_hunique)
807 				free(sc->sc_hunique, M_DEVBUF);
808 			sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
809 			    M_DONTWAIT);
810 			if (sc->sc_hunique == NULL) {
811 				PPPOE_UNLOCK(sc);
812 				goto done;
813 			}
814 			sc->sc_hunique_len = hunique_len;
815 			memcpy(sc->sc_hunique, hunique, hunique_len);
816 		}
817 		pppoe_send_pads(sc);
818 		sc->sc_state = PPPOE_STATE_SESSION;
819 		PPPOE_UNLOCK(sc);
820 
821 		sc->sc_sppp.pp_up(&sc->sc_sppp);
822 		break;
823 	    }
824 #else
825 		/* ignore, we are no access concentrator */
826 		goto done;
827 #endif /* PPPOE_SERVER */
828 	case PPPOE_CODE_PADO:
829 		if (sc == NULL) {
830 			/* be quiet if there is not a single pppoe instance */
831 			if (!LIST_EMPTY(&pppoe_softc_list))
832 				printf("pppoe: received PADO but could not find request for it\n");
833 			goto done;
834 		}
835 
836 		PPPOE_LOCK(sc, RW_WRITER);
837 
838 		if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
839 			printf("%s: received unexpected PADO\n",
840 			    sc->sc_sppp.pp_if.if_xname);
841 			PPPOE_UNLOCK(sc);
842 			goto done;
843 		}
844 
845 		if (ac_cookie) {
846 			if (sc->sc_ac_cookie)
847 				free(sc->sc_ac_cookie, M_DEVBUF);
848 			sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
849 			    M_DONTWAIT);
850 			if (sc->sc_ac_cookie == NULL) {
851 				printf("%s: FATAL: could not allocate memory "
852 				    "for AC cookie\n",
853 				    sc->sc_sppp.pp_if.if_xname);
854 				PPPOE_UNLOCK(sc);
855 				goto done;
856 			}
857 			sc->sc_ac_cookie_len = ac_cookie_len;
858 			memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
859 		}
860 		if (relay_sid) {
861 			if (sc->sc_relay_sid)
862 				free(sc->sc_relay_sid, M_DEVBUF);
863 			sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF,
864 			    M_DONTWAIT);
865 			if (sc->sc_relay_sid == NULL) {
866 				printf("%s: FATAL: could not allocate memory "
867 				    "for relay SID\n",
868 				    sc->sc_sppp.pp_if.if_xname);
869 				PPPOE_UNLOCK(sc);
870 				goto done;
871 			}
872 			sc->sc_relay_sid_len = relay_sid_len;
873 			memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len);
874 		}
875 		memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
876 		callout_stop(&sc->sc_timeout);
877 		sc->sc_padr_retried = 0;
878 		sc->sc_state = PPPOE_STATE_PADR_SENT;
879 		if ((err = pppoe_send_padr(sc)) != 0) {
880 			if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
881 				printf("%s: failed to send PADR, "
882 				    "error=%d\n", sc->sc_sppp.pp_if.if_xname,
883 				    err);
884 		}
885 		callout_reset(&sc->sc_timeout,
886 		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
887 		    pppoe_timeout, sc);
888 
889 		PPPOE_UNLOCK(sc);
890 		break;
891 	case PPPOE_CODE_PADS:
892 		if (sc == NULL)
893 			goto done;
894 
895 		PPPOE_LOCK(sc, RW_WRITER);
896 
897 		sc->sc_session = session;
898 		callout_stop(&sc->sc_timeout);
899 		if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
900 			printf("%s: session 0x%x connected\n",
901 			    sc->sc_sppp.pp_if.if_xname, session);
902 		sc->sc_state = PPPOE_STATE_SESSION;
903 		PPPOE_UNLOCK(sc);
904 
905 		sc->sc_sppp.pp_up(&sc->sc_sppp);	/* notify upper layers */
906 		break;
907 	case PPPOE_CODE_PADT: {
908 		struct ifnet *rcvif;
909 		struct psref psref;
910 
911 		rcvif = m_get_rcvif_psref(m, &psref);
912 		if (__predict_true(rcvif != NULL)) {
913 			sc = pppoe_find_softc_by_session(session, rcvif,
914 			    RW_WRITER);
915 		}
916 		m_put_rcvif_psref(rcvif, &psref);
917 		if (sc == NULL)
918 			goto done;
919 
920 		pppoe_clear_softc(sc, "received PADT");
921 		PPPOE_UNLOCK(sc);
922 		break;
923 	}
924 	default:
925 		if (sc != NULL) {
926 			PPPOE_LOCK(sc, RW_READER);
927 			strlcpy(devname, sc->sc_sppp.pp_if.if_xname,
928 			    sizeof(devname));
929 			PPPOE_UNLOCK(sc);
930 		} else
931 			strlcpy(devname, "pppoe", sizeof(devname));
932 
933 		printf("%s: unknown code (0x%04x) session = 0x%04x\n",
934 		    devname, ph->code, session);
935 		break;
936 	}
937 
938 done:
939 	if (m)
940 		m_freem(m);
941 	return;
942 }
943 
944 static void
945 pppoe_disc_input(struct mbuf *m)
946 {
947 	KASSERT(m->m_flags & M_PKTHDR);
948 
949 	/*
950 	 * Avoid error messages if there is not a single PPPoE instance.
951 	 */
952 	if (!LIST_EMPTY(&pppoe_softc_list)) {
953 		pppoe_dispatch_disc_pkt(m, 0);
954 	} else {
955 		m_freem(m);
956 	}
957 }
958 
959 static bool
960 pppoe_is_my_frame(uint8_t *dhost, struct ifnet *rcvif)
961 {
962 
963 	if (memcmp(CLLADDR(rcvif->if_sadl), dhost, ETHER_ADDR_LEN) == 0)
964 		return true;
965 
966 	return false;
967 }
968 
969 static void
970 pppoe_data_input(struct mbuf *m)
971 {
972 	uint16_t session, plen;
973 	struct pppoe_softc *sc;
974 	struct pppoehdr *ph;
975 	struct ifnet *rcvif;
976 	struct psref psref;
977 	uint8_t shost[ETHER_ADDR_LEN];
978 	uint8_t dhost[ETHER_ADDR_LEN];
979 	bool term_unknown = pppoe_term_unknown;
980 
981 	KASSERT(m->m_flags & M_PKTHDR);
982 
983 	/*
984 	 * Avoid error messages if there is not a single PPPoE instance.
985 	 */
986 	if (LIST_EMPTY(&pppoe_softc_list)) {
987 		goto drop;
988 	}
989 
990 	if (term_unknown) {
991 		memcpy(shost, mtod(m, struct ether_header*)->ether_shost,
992 		    ETHER_ADDR_LEN);
993 		memcpy(dhost, mtod(m, struct ether_header*)->ether_dhost,
994 		    ETHER_ADDR_LEN);
995 	}
996 	m_adj(m, sizeof(struct ether_header));
997 	if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
998 		printf("pppoe (data): dropping too short packet: %d bytes\n",
999 		    m->m_pkthdr.len);
1000 		goto drop;
1001 	}
1002 
1003 	if (m->m_len < sizeof(*ph)) {
1004 		m = m_pullup(m, sizeof(*ph));
1005 		if (!m) {
1006 			printf("pppoe: could not get PPPoE header\n");
1007 			return;
1008 		}
1009 	}
1010 	ph = mtod(m, struct pppoehdr *);
1011 
1012 	if (ph->vertype != PPPOE_VERTYPE) {
1013 		printf("pppoe (data): unknown version/type packet: 0x%x\n",
1014 		    ph->vertype);
1015 		goto drop;
1016 	}
1017 	if (ph->code != 0)
1018 		goto drop;
1019 
1020 	session = ntohs(ph->session);
1021 	rcvif = m_get_rcvif_psref(m, &psref);
1022 	if (__predict_false(rcvif == NULL))
1023 		goto drop;
1024 	sc = pppoe_find_softc_by_session(session, rcvif, RW_READER);
1025 	if (sc == NULL) {
1026 		if (term_unknown) {
1027 			static struct timeval lasttime = {0, 0};
1028 			static int curpps = 0;
1029 			/*
1030 			 * avoid to send wrong PADT which is response from
1031 			 * session stage pakcets for other hosts when parent
1032 			 * ethernet is promiscuous mode.
1033 			 */
1034 			if (pppoe_is_my_frame(dhost, rcvif)
1035 			    && ppsratecheck(&lasttime, &curpps,
1036 				pppoe_term_unknown_pps)) {
1037 				printf("pppoe: input for unknown session %#x, "
1038 				    "sending PADT\n", session);
1039 				pppoe_send_padt(rcvif, session, shost);
1040 			}
1041 		}
1042 		m_put_rcvif_psref(rcvif, &psref);
1043 		goto drop;
1044 	}
1045 
1046 	m_put_rcvif_psref(rcvif, &psref);
1047 
1048 	plen = ntohs(ph->plen);
1049 
1050 	bpf_mtap(&sc->sc_sppp.pp_if, m);
1051 
1052 	m_adj(m, PPPOE_HEADERLEN);
1053 
1054 #ifdef PPPOE_DEBUG
1055 	{
1056 		struct mbuf *p;
1057 
1058 		printf("%s: pkthdr.len=%d, pppoe.len=%d",
1059 		    sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen);
1060 		p = m;
1061 		while (p) {
1062 			printf(" l=%d", p->m_len);
1063 			p = p->m_next;
1064 		}
1065 		printf("\n");
1066 	}
1067 #endif
1068 	PPPOE_UNLOCK(sc);
1069 
1070 	if (m->m_pkthdr.len < plen)
1071 		goto drop;
1072 
1073 	/*
1074 	 *  Fix incoming interface pointer (not the raw ethernet interface
1075 	 * anymore)
1076 	 */
1077 	m_set_rcvif(m, &sc->sc_sppp.pp_if);
1078 
1079 	/* pass packet up and account for it */
1080 	sc->sc_sppp.pp_if.if_ipackets++;
1081 	sppp_input(&sc->sc_sppp.pp_if, m);
1082 	return;
1083 
1084 drop:
1085 	m_freem(m);
1086 }
1087 
1088 static int
1089 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
1090 {
1091 	struct sockaddr dst;
1092 	struct ether_header *eh;
1093 	uint16_t etype;
1094 
1095 	if (sc->sc_eth_if == NULL) {
1096 		m_freem(m);
1097 		return EIO;
1098 	}
1099 
1100 	memset(&dst, 0, sizeof dst);
1101 	dst.sa_family = AF_UNSPEC;
1102 	eh = (struct ether_header*)&dst.sa_data;
1103 	etype = sc->sc_state == PPPOE_STATE_SESSION
1104 	    ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
1105 	eh->ether_type = htons(etype);
1106 	memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
1107 
1108 #ifdef PPPOE_DEBUG
1109 	printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
1110 	    sc->sc_sppp.pp_if.if_xname, etype,
1111 	    sc->sc_state, sc->sc_session,
1112 	    ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
1113 #endif
1114 
1115 	m->m_flags &= ~(M_BCAST|M_MCAST);
1116 	sc->sc_sppp.pp_if.if_opackets++;
1117 	return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL);
1118 }
1119 
1120 static int
1121 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1122 {
1123 	struct lwp *l = curlwp;	/* XXX */
1124 	struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
1125 	struct ifreq *ifr = data;
1126 	int error = 0;
1127 
1128 	switch (cmd) {
1129 	case PPPOESETPARMS:
1130 	{
1131 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
1132 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1133 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1134 		    NULL) != 0)
1135 			return EPERM;
1136 		if (parms->eth_ifname[0] != 0) {
1137 			struct ifnet	*eth_if;
1138 
1139 			PPPOE_LOCK(sc, RW_WRITER);
1140 			eth_if = ifunit(parms->eth_ifname);
1141 			if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
1142 				sc->sc_eth_if = NULL;
1143 				PPPOE_UNLOCK(sc);
1144 				return ENXIO;
1145 			}
1146 
1147 			if (sc->sc_sppp.pp_if.if_mtu !=
1148 			    eth_if->if_mtu - PPPOE_OVERHEAD) {
1149 				sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
1150 				    PPPOE_OVERHEAD;
1151 			}
1152 			sc->sc_eth_if = eth_if;
1153 			PPPOE_UNLOCK(sc);
1154 		}
1155 		if (parms->ac_name != NULL) {
1156 			size_t s;
1157 			char *b = malloc(parms->ac_name_len + 1, M_DEVBUF,
1158 			    M_WAITOK);
1159 			if (b == NULL)
1160 				return ENOMEM;
1161 			error = copyinstr(parms->ac_name, b,
1162 			    parms->ac_name_len+1, &s);
1163 			if (error != 0) {
1164 				free(b, M_DEVBUF);
1165 				return error;
1166 			}
1167 			if (s != parms->ac_name_len+1) {
1168 				free(b, M_DEVBUF);
1169 				return EINVAL;
1170 			}
1171 
1172 			PPPOE_LOCK(sc, RW_WRITER);
1173 			if (sc->sc_concentrator_name)
1174 				free(sc->sc_concentrator_name, M_DEVBUF);
1175 			sc->sc_concentrator_name = b;
1176 			PPPOE_UNLOCK(sc);
1177 		}
1178 		if (parms->service_name != NULL) {
1179 			size_t s;
1180 			char *b = malloc(parms->service_name_len + 1, M_DEVBUF,
1181 			    M_WAITOK);
1182 			if (b == NULL)
1183 				return ENOMEM;
1184 			error = copyinstr(parms->service_name, b,
1185 			    parms->service_name_len+1, &s);
1186 			if (error != 0) {
1187 				free(b, M_DEVBUF);
1188 				return error;
1189 			}
1190 			if (s != parms->service_name_len+1) {
1191 				free(b, M_DEVBUF);
1192 				return EINVAL;
1193 			}
1194 
1195 			PPPOE_LOCK(sc, RW_WRITER);
1196 			if (sc->sc_service_name)
1197 				free(sc->sc_service_name, M_DEVBUF);
1198 			sc->sc_service_name = b;
1199 			PPPOE_UNLOCK(sc);
1200 		}
1201 		return 0;
1202 	}
1203 	break;
1204 	case PPPOEGETPARMS:
1205 	{
1206 		struct pppoediscparms *parms = (struct pppoediscparms*)data;
1207 		memset(parms, 0, sizeof *parms);
1208 		PPPOE_LOCK(sc, RW_READER);
1209 		if (sc->sc_eth_if)
1210 			strlcpy(parms->ifname, sc->sc_eth_if->if_xname,
1211 			    sizeof(parms->ifname));
1212 		PPPOE_UNLOCK(sc);
1213 		return 0;
1214 	}
1215 	break;
1216 	case PPPOEGETSESSION:
1217 	{
1218 		struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
1219 		PPPOE_LOCK(sc, RW_READER);
1220 		state->state = sc->sc_state;
1221 		state->session_id = sc->sc_session;
1222 		state->padi_retry_no = sc->sc_padi_retried;
1223 		state->padr_retry_no = sc->sc_padr_retried;
1224 		PPPOE_UNLOCK(sc);
1225 		return 0;
1226 	}
1227 	break;
1228 	case SIOCSIFFLAGS:
1229 		/*
1230 		 * Prevent running re-establishment timers overriding
1231 		 * administrators choice.
1232 		 */
1233 		PPPOE_LOCK(sc, RW_WRITER);
1234 
1235 		if ((ifr->ifr_flags & IFF_UP) == 0
1236 		     && sc->sc_state >= PPPOE_STATE_PADI_SENT
1237 		     && sc->sc_state < PPPOE_STATE_SESSION) {
1238 			callout_stop(&sc->sc_timeout);
1239 			sc->sc_state = PPPOE_STATE_INITIAL;
1240 			sc->sc_padi_retried = 0;
1241 			sc->sc_padr_retried = 0;
1242 			memcpy(&sc->sc_dest, etherbroadcastaddr,
1243 			    sizeof(sc->sc_dest));
1244 		}
1245 
1246 		PPPOE_UNLOCK(sc);
1247 
1248 		error = sppp_ioctl(ifp, cmd, data);
1249 
1250 		return error;
1251 	case SIOCSIFMTU:
1252 		if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
1253 		    PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
1254 			return EINVAL;
1255 		}
1256 		/*FALLTHROUGH*/
1257 	default:
1258 		return sppp_ioctl(ifp, cmd, data);
1259 	}
1260 	return 0;
1261 }
1262 
1263 /*
1264  * Allocate a mbuf/cluster with space to store the given data length
1265  * of payload, leaving space for prepending an ethernet header
1266  * in front.
1267  */
1268 static struct mbuf *
1269 pppoe_get_mbuf(size_t len)
1270 {
1271 	struct mbuf *m;
1272 
1273 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1274 	if (m == NULL)
1275 		return NULL;
1276 	if (len + sizeof(struct ether_header) > MHLEN) {
1277 		MCLGET(m, M_DONTWAIT);
1278 		if ((m->m_flags & M_EXT) == 0) {
1279 			m_free(m);
1280 			return NULL;
1281 		}
1282 	}
1283 	m->m_data += sizeof(struct ether_header);
1284 	m->m_len = len;
1285 	m->m_pkthdr.len = len;
1286 	m_reset_rcvif(m);
1287 
1288 	return m;
1289 }
1290 
1291 static int
1292 pppoe_send_padi(struct pppoe_softc *sc)
1293 {
1294 	struct mbuf *m0;
1295 	int len, l1 = 0, l2 = 0; /* XXX: gcc */
1296 	uint8_t *p;
1297 
1298 	if (sc->sc_state >PPPOE_STATE_PADI_SENT)
1299 		panic("pppoe_send_padi in state %d", sc->sc_state);
1300 
1301 	/* calculate length of frame (excluding ethernet header + pppoe header) */
1302 	len = 2 + 2 + 2 + 2 + sizeof sc;	/* service name tag is required, host unique is send too */
1303 	if (sc->sc_service_name != NULL) {
1304 		l1 = strlen(sc->sc_service_name);
1305 		len += l1;
1306 	}
1307 	if (sc->sc_concentrator_name != NULL) {
1308 		l2 = strlen(sc->sc_concentrator_name);
1309 		len += 2 + 2 + l2;
1310 	}
1311 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1312 		len += 2 + 2 + 2;
1313 	}
1314 
1315 	/* allocate a buffer */
1316 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);	/* header len + payload len */
1317 	if (!m0)
1318 		return ENOBUFS;
1319 
1320 	/* fill in pkt */
1321 	p = mtod(m0, uint8_t *);
1322 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
1323 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1324 	if (sc->sc_service_name != NULL) {
1325 		PPPOE_ADD_16(p, l1);
1326 		memcpy(p, sc->sc_service_name, l1);
1327 		p += l1;
1328 	} else {
1329 		PPPOE_ADD_16(p, 0);
1330 	}
1331 	if (sc->sc_concentrator_name != NULL) {
1332 		PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
1333 		PPPOE_ADD_16(p, l2);
1334 		memcpy(p, sc->sc_concentrator_name, l2);
1335 		p += l2;
1336 	}
1337 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1338 	PPPOE_ADD_16(p, sizeof(sc));
1339 	memcpy(p, &sc, sizeof sc);
1340 	p += sizeof(sc);
1341 
1342 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1343 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1344 		PPPOE_ADD_16(p, 2);
1345 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1346 	}
1347 
1348 #ifdef PPPOE_DEBUG
1349 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1350 		panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
1351 		    (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1352 #endif
1353 
1354 	/* send pkt */
1355 	return pppoe_output(sc, m0);
1356 }
1357 
1358 static void
1359 pppoe_timeout(void *arg)
1360 {
1361 	int retry_wait, err;
1362 	struct pppoe_softc *sc = (struct pppoe_softc*)arg;
1363 	DECLARE_SPLNET_VARIABLE;
1364 
1365 #ifdef PPPOE_DEBUG
1366 	printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
1367 #endif
1368 
1369 	PPPOE_LOCK(sc, RW_WRITER);
1370 	switch (sc->sc_state) {
1371 	case PPPOE_STATE_INITIAL:
1372 		/* delayed connect from pppoe_tls() */
1373 		pppoe_connect(sc);
1374 		break;
1375 	case PPPOE_STATE_PADI_SENT:
1376 		/*
1377 		 * We have two basic ways of retrying:
1378 		 *  - Quick retry mode: try a few times in short sequence
1379 		 *  - Slow retry mode: we already had a connection successfully
1380 		 *    established and will try infinitely (without user
1381 		 *    intervention)
1382 		 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
1383 		 * is not set.
1384 		 */
1385 
1386 		/* initialize for quick retry mode */
1387 		retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
1388 
1389 		ACQUIRE_SPLNET();
1390 		sc->sc_padi_retried++;
1391 		if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
1392 			if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
1393 				/* slow retry mode */
1394 				retry_wait = PPPOE_SLOW_RETRY;
1395 			} else {
1396 				pppoe_abort_connect(sc);
1397 				RELEASE_SPLNET();
1398 				PPPOE_UNLOCK(sc);
1399 				return;
1400 			}
1401 		}
1402 		if ((err = pppoe_send_padi(sc)) != 0) {
1403 			sc->sc_padi_retried--;
1404 			if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1405 				printf("%s: failed to transmit PADI, "
1406 				    "error=%d\n",
1407 				    sc->sc_sppp.pp_if.if_xname, err);
1408 		}
1409 		callout_reset(&sc->sc_timeout, retry_wait,
1410 		    pppoe_timeout, sc);
1411 		RELEASE_SPLNET();
1412 		break;
1413 
1414 	case PPPOE_STATE_PADR_SENT:
1415 		ACQUIRE_SPLNET();
1416 		sc->sc_padr_retried++;
1417 		if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
1418 			memcpy(&sc->sc_dest, etherbroadcastaddr,
1419 			    sizeof(sc->sc_dest));
1420 			sc->sc_state = PPPOE_STATE_PADI_SENT;
1421 			sc->sc_padr_retried = 0;
1422 			if ((err = pppoe_send_padi(sc)) != 0) {
1423 				if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1424 					printf("%s: failed to send PADI"
1425 					    ", error=%d\n",
1426 					    sc->sc_sppp.pp_if.if_xname, err);
1427 			}
1428 			callout_reset(&sc->sc_timeout,
1429 			    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried),
1430 			    pppoe_timeout, sc);
1431 			RELEASE_SPLNET();
1432 			PPPOE_UNLOCK(sc);
1433 			return;
1434 		}
1435 		if ((err = pppoe_send_padr(sc)) != 0) {
1436 			sc->sc_padr_retried--;
1437 			if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1438 				printf("%s: failed to send PADR, "
1439 				    "error=%d\n", sc->sc_sppp.pp_if.if_xname,
1440 				    err);
1441 		}
1442 		callout_reset(&sc->sc_timeout,
1443 		    PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
1444 		    pppoe_timeout, sc);
1445 		RELEASE_SPLNET();
1446 		break;
1447 	case PPPOE_STATE_CLOSING:
1448 		pppoe_disconnect(sc);
1449 		break;
1450 	default:
1451 		PPPOE_UNLOCK(sc);
1452 		return;	/* all done, work in peace */
1453 	}
1454 	PPPOE_UNLOCK(sc);
1455 }
1456 
1457 /* Start a connection (i.e. initiate discovery phase) */
1458 static int
1459 pppoe_connect(struct pppoe_softc *sc)
1460 {
1461 	int err;
1462 	DECLARE_SPLNET_VARIABLE;
1463 
1464 	KASSERT(PPPOE_WLOCKED(sc));
1465 
1466 	if (sc->sc_state != PPPOE_STATE_INITIAL)
1467 		return EBUSY;
1468 
1469 #ifdef PPPOE_SERVER
1470 	/* wait PADI if IFF_PASSIVE */
1471 	if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
1472 		return 0;
1473 #endif
1474 	ACQUIRE_SPLNET();
1475 	/* save state, in case we fail to send PADI */
1476 	sc->sc_state = PPPOE_STATE_PADI_SENT;
1477 	sc->sc_padr_retried = 0;
1478 	err = pppoe_send_padi(sc);
1479 	if (err != 0 && sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1480 		printf("%s: failed to send PADI, error=%d\n",
1481 		    sc->sc_sppp.pp_if.if_xname, err);
1482 	callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
1483 	RELEASE_SPLNET();
1484 	return err;
1485 }
1486 
1487 /* disconnect */
1488 static int
1489 pppoe_disconnect(struct pppoe_softc *sc)
1490 {
1491 	int err;
1492 	DECLARE_SPLNET_VARIABLE;
1493 
1494 	KASSERT(PPPOE_WLOCKED(sc));
1495 
1496 	ACQUIRE_SPLNET();
1497 
1498 	if (sc->sc_state < PPPOE_STATE_SESSION)
1499 		err = EBUSY;
1500 	else {
1501 		if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1502 			printf("%s: disconnecting\n",
1503 			    sc->sc_sppp.pp_if.if_xname);
1504 		err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session,
1505 		    (const uint8_t *)&sc->sc_dest);
1506 	}
1507 
1508 	/* cleanup softc */
1509 	sc->sc_state = PPPOE_STATE_INITIAL;
1510 
1511 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1512 	if (sc->sc_ac_cookie) {
1513 		free(sc->sc_ac_cookie, M_DEVBUF);
1514 		sc->sc_ac_cookie = NULL;
1515 	}
1516 	sc->sc_ac_cookie_len = 0;
1517 	if (sc->sc_relay_sid) {
1518 		free(sc->sc_relay_sid, M_DEVBUF);
1519 		sc->sc_relay_sid = NULL;
1520 	}
1521 	sc->sc_relay_sid_len = 0;
1522 #ifdef PPPOE_SERVER
1523 	if (sc->sc_hunique) {
1524 		free(sc->sc_hunique, M_DEVBUF);
1525 		sc->sc_hunique = NULL;
1526 	}
1527 	sc->sc_hunique_len = 0;
1528 #endif
1529 	sc->sc_session = 0;
1530 
1531 	PPPOE_UNLOCK(sc);
1532 
1533 	/* notify upper layer */
1534 	sc->sc_sppp.pp_down(&sc->sc_sppp);
1535 
1536 	PPPOE_LOCK(sc, RW_WRITER);
1537 
1538 	RELEASE_SPLNET();
1539 	return err;
1540 }
1541 
1542 /* Connection attempt aborted */
1543 static void
1544 pppoe_abort_connect(struct pppoe_softc *sc)
1545 {
1546 	KASSERT(PPPOE_WLOCKED(sc));
1547 
1548 	printf("%s: could not establish connection\n",
1549 		sc->sc_sppp.pp_if.if_xname);
1550 	sc->sc_state = PPPOE_STATE_CLOSING;
1551 
1552 	PPPOE_UNLOCK(sc);
1553 
1554 	/* notify upper layer */
1555 	sc->sc_sppp.pp_down(&sc->sc_sppp);
1556 
1557 	PPPOE_LOCK(sc, RW_WRITER);
1558 
1559 	/* clear connection state */
1560 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1561 	sc->sc_state = PPPOE_STATE_INITIAL;
1562 }
1563 
1564 /* Send a PADR packet */
1565 static int
1566 pppoe_send_padr(struct pppoe_softc *sc)
1567 {
1568 	struct mbuf *m0;
1569 	uint8_t *p;
1570 	size_t len, l1 = 0; /* XXX: gcc */
1571 
1572 	if (sc->sc_state != PPPOE_STATE_PADR_SENT)
1573 		return EIO;
1574 
1575 	len = 2 + 2 + 2 + 2 + sizeof(sc);		/* service name, host unique */
1576 	if (sc->sc_service_name != NULL) {		/* service name tag maybe empty */
1577 		l1 = strlen(sc->sc_service_name);
1578 		len += l1;
1579 	}
1580 	if (sc->sc_ac_cookie_len > 0)
1581 		len += 2 + 2 + sc->sc_ac_cookie_len;	/* AC cookie */
1582 	if (sc->sc_relay_sid_len > 0)
1583 		len += 2 + 2 + sc->sc_relay_sid_len;	/* Relay SID */
1584 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1585 		len += 2 + 2 + 2;
1586 	}
1587 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1588 	if (!m0)
1589 		return ENOBUFS;
1590 	p = mtod(m0, uint8_t *);
1591 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1592 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1593 	if (sc->sc_service_name != NULL) {
1594 		PPPOE_ADD_16(p, l1);
1595 		memcpy(p, sc->sc_service_name, l1);
1596 		p += l1;
1597 	} else {
1598 		PPPOE_ADD_16(p, 0);
1599 	}
1600 	if (sc->sc_ac_cookie_len > 0) {
1601 		PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1602 		PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1603 		memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1604 		p += sc->sc_ac_cookie_len;
1605 	}
1606 	if (sc->sc_relay_sid_len > 0) {
1607 		PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
1608 		PPPOE_ADD_16(p, sc->sc_relay_sid_len);
1609 		memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
1610 		p += sc->sc_relay_sid_len;
1611 	}
1612 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1613 	PPPOE_ADD_16(p, sizeof(sc));
1614 	memcpy(p, &sc, sizeof sc);
1615 	p += sizeof(sc);
1616 
1617 	if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1618 		PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1619 		PPPOE_ADD_16(p, 2);
1620 		PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1621 	}
1622 
1623 #ifdef PPPOE_DEBUG
1624 	if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1625 		panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
1626 			(long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1627 #endif
1628 
1629 	return pppoe_output(sc, m0);
1630 }
1631 
1632 /* send a PADT packet */
1633 static int
1634 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
1635 {
1636 	struct ether_header *eh;
1637 	struct sockaddr dst;
1638 	struct mbuf *m0;
1639 	uint8_t *p;
1640 
1641 	m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
1642 	if (!m0)
1643 		return EIO;
1644 	p = mtod(m0, uint8_t *);
1645 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
1646 
1647 	memset(&dst, 0, sizeof dst);
1648 	dst.sa_family = AF_UNSPEC;
1649 	eh = (struct ether_header*)&dst.sa_data;
1650 	eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
1651 	memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
1652 
1653 	m0->m_flags &= ~(M_BCAST|M_MCAST);
1654 	return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL);
1655 }
1656 
1657 #ifdef PPPOE_SERVER
1658 static int
1659 pppoe_send_pado(struct pppoe_softc *sc)
1660 {
1661 	struct mbuf *m0;
1662 	uint8_t *p;
1663 	size_t len;
1664 
1665 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1666 		return EIO;
1667 
1668 	/* calc length */
1669 	len = 0;
1670 	/* include ac_cookie */
1671 	len += 2 + 2 + sizeof(sc);
1672 	/* include hunique */
1673 	len += 2 + 2 + sc->sc_hunique_len;
1674 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1675 	if (!m0)
1676 		return EIO;
1677 	p = mtod(m0, uint8_t *);
1678 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1679 	PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1680 	PPPOE_ADD_16(p, sizeof(sc));
1681 	memcpy(p, &sc, sizeof(sc));
1682 	p += sizeof(sc);
1683 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1684 	PPPOE_ADD_16(p, sc->sc_hunique_len);
1685 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1686 	return pppoe_output(sc, m0);
1687 }
1688 
1689 static int
1690 pppoe_send_pads(struct pppoe_softc *sc)
1691 {
1692 	struct bintime bt;
1693 	struct mbuf *m0;
1694 	uint8_t *p;
1695 	size_t len, l1 = 0;	/* XXX: gcc */
1696 
1697 	KASSERT(PPPOE_WLOCKED(sc));
1698 
1699 	if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1700 		return EIO;
1701 
1702 	getbinuptime(&bt);
1703 	sc->sc_session = bt.sec % 0xff + 1;
1704 	/* calc length */
1705 	len = 0;
1706 	/* include hunique */
1707 	len += 2 + 2 + 2 + 2 + sc->sc_hunique_len;	/* service name, host unique*/
1708 	if (sc->sc_service_name != NULL) {		/* service name tag maybe empty */
1709 		l1 = strlen(sc->sc_service_name);
1710 		len += l1;
1711 	}
1712 	m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1713 	if (!m0)
1714 		return ENOBUFS;
1715 	p = mtod(m0, uint8_t *);
1716 	PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1717 	PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1718 	if (sc->sc_service_name != NULL) {
1719 		PPPOE_ADD_16(p, l1);
1720 		memcpy(p, sc->sc_service_name, l1);
1721 		p += l1;
1722 	} else {
1723 		PPPOE_ADD_16(p, 0);
1724 	}
1725 	PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1726 	PPPOE_ADD_16(p, sc->sc_hunique_len);
1727 	memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1728 	return pppoe_output(sc, m0);
1729 }
1730 #endif
1731 
1732 static void
1733 pppoe_tls(struct sppp *sp)
1734 {
1735 	struct pppoe_softc *sc = (void *)sp;
1736 	int wtime;
1737 
1738 	PPPOE_LOCK(sc, RW_READER);
1739 
1740 	if (sc->sc_state != PPPOE_STATE_INITIAL) {
1741 		PPPOE_UNLOCK(sc);
1742 		return;
1743 	}
1744 
1745 	if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
1746 	    sc->sc_sppp.pp_auth_failures > 0) {
1747 		/*
1748 		 * Delay trying to reconnect a bit more - the peer
1749 		 * might have failed to contact its radius server.
1750 		 */
1751 		wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
1752 		if (wtime > PPPOE_SLOW_RETRY)
1753 			wtime = PPPOE_SLOW_RETRY;
1754 	} else {
1755 		wtime = PPPOE_RECON_IMMEDIATE;
1756 	}
1757 	callout_reset(&sc->sc_timeout, wtime, pppoe_timeout, sc);
1758 
1759 	PPPOE_UNLOCK(sc);
1760 }
1761 
1762 static void
1763 pppoe_tlf(struct sppp *sp)
1764 {
1765 	struct pppoe_softc *sc = (void *)sp;
1766 
1767 	PPPOE_LOCK(sc, RW_WRITER);
1768 
1769 	if (sc->sc_state < PPPOE_STATE_SESSION) {
1770 		PPPOE_UNLOCK(sc);
1771 		return;
1772 	}
1773 	/*
1774 	 * Do not call pppoe_disconnect here, the upper layer state
1775 	 * machine gets confused by this. We must return from this
1776 	 * function and defer disconnecting to the timeout handler.
1777 	 */
1778 	sc->sc_state = PPPOE_STATE_CLOSING;
1779 
1780 	callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc);
1781 
1782 	PPPOE_UNLOCK(sc);
1783 }
1784 
1785 static void
1786 pppoe_start(struct ifnet *ifp)
1787 {
1788 	struct pppoe_softc *sc = (void *)ifp;
1789 	struct mbuf *m;
1790 	uint8_t *p;
1791 	size_t len;
1792 
1793 	if (sppp_isempty(ifp))
1794 		return;
1795 
1796 	/* are we ready to process data yet? */
1797 	PPPOE_LOCK(sc, RW_READER);
1798 	if (sc->sc_state < PPPOE_STATE_SESSION) {
1799 		sppp_flush(&sc->sc_sppp.pp_if);
1800 		PPPOE_UNLOCK(sc);
1801 		return;
1802 	}
1803 
1804 	while ((m = sppp_dequeue(ifp)) != NULL) {
1805 		len = m->m_pkthdr.len;
1806 		M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1807 		if (m == NULL) {
1808 			ifp->if_oerrors++;
1809 			continue;
1810 		}
1811 		p = mtod(m, uint8_t *);
1812 		PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1813 
1814 		bpf_mtap(&sc->sc_sppp.pp_if, m);
1815 
1816 		pppoe_output(sc, m);
1817 	}
1818 	PPPOE_UNLOCK(sc);
1819 }
1820 
1821 #ifdef PPPOE_MPSAFE
1822 static int
1823 pppoe_transmit(struct ifnet *ifp, struct mbuf *m)
1824 {
1825 	struct pppoe_softc *sc = (void *)ifp;
1826 	uint8_t *p;
1827 	size_t len;
1828 
1829 	if (m == NULL)
1830 		return EINVAL;
1831 
1832 	/* are we ready to process data yet? */
1833 	PPPOE_LOCK(sc, RW_READER);
1834 	if (sc->sc_state < PPPOE_STATE_SESSION) {
1835 		PPPOE_UNLOCK(sc);
1836 		m_freem(m);
1837 		return ENOBUFS;
1838 	}
1839 
1840 	len = m->m_pkthdr.len;
1841 	M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1842 	if (m == NULL) {
1843 		PPPOE_UNLOCK(sc);
1844 		ifp->if_oerrors++;
1845 		return ENETDOWN;
1846 	}
1847 	p = mtod(m, uint8_t *);
1848 	PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1849 
1850 	bpf_mtap(&sc->sc_sppp.pp_if, m);
1851 
1852 	pppoe_output(sc, m);
1853 	PPPOE_UNLOCK(sc);
1854 	return 0;
1855 }
1856 #endif /* PPPOE_MPSAFE */
1857 
1858 static void
1859 pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2)
1860 {
1861 	struct ifnet *ifp = arg2;
1862 	struct pppoe_softc *sc;
1863 	DECLARE_SPLNET_VARIABLE;
1864 
1865 	if (cmd != PFIL_IFNET_DETACH)
1866 		return;
1867 
1868 	ACQUIRE_SPLNET();
1869 	rw_enter(&pppoe_softc_list_lock, RW_READER);
1870 	LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1871 		PPPOE_LOCK(sc, RW_WRITER);
1872 		if (sc->sc_eth_if != ifp) {
1873 			PPPOE_UNLOCK(sc);
1874 			continue;
1875 		}
1876 		if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1877 			sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1878 			printf("%s: ethernet interface detached, going down\n",
1879 			    sc->sc_sppp.pp_if.if_xname);
1880 		}
1881 		sc->sc_eth_if = NULL;
1882 		pppoe_clear_softc(sc, "ethernet interface detached");
1883 		PPPOE_UNLOCK(sc);
1884 	}
1885 	rw_exit(&pppoe_softc_list_lock);
1886 	RELEASE_SPLNET();
1887 }
1888 
1889 static void
1890 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1891 {
1892 	KASSERT(PPPOE_WLOCKED(sc));
1893 
1894 	/* stop timer */
1895 	callout_stop(&sc->sc_timeout);
1896 	if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1897 		printf("%s: session 0x%x terminated, %s\n",
1898 		    sc->sc_sppp.pp_if.if_xname, sc->sc_session, message);
1899 
1900 	/* fix our state */
1901 	sc->sc_state = PPPOE_STATE_INITIAL;
1902 
1903 	PPPOE_UNLOCK(sc);
1904 
1905 	/* signal upper layer */
1906 	sc->sc_sppp.pp_down(&sc->sc_sppp);
1907 
1908 	PPPOE_LOCK(sc, RW_WRITER);
1909 
1910 	/* clean up softc */
1911 	memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1912 	if (sc->sc_ac_cookie) {
1913 		free(sc->sc_ac_cookie, M_DEVBUF);
1914 		sc->sc_ac_cookie = NULL;
1915 	}
1916 	if (sc->sc_relay_sid) {
1917 		free(sc->sc_relay_sid, M_DEVBUF);
1918 		sc->sc_relay_sid = NULL;
1919 	}
1920 	sc->sc_ac_cookie_len = 0;
1921 	sc->sc_session = 0;
1922 }
1923 
1924 static void
1925 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
1926 {
1927 	if (m->m_flags & M_PROMISC) {
1928 		m_freem(m);
1929 		return;
1930 	}
1931 
1932 #ifndef PPPOE_SERVER
1933 	if (m->m_flags & (M_MCAST | M_BCAST)) {
1934 		m_freem(m);
1935 		return;
1936 	}
1937 #endif
1938 
1939 	IFQ_LOCK(inq);
1940 	if (IF_QFULL(inq)) {
1941 		IF_DROP(inq);
1942 		IFQ_UNLOCK(inq);
1943 		m_freem(m);
1944 	} else {
1945 		IF_ENQUEUE(inq, m);
1946 		IFQ_UNLOCK(inq);
1947 		softint_schedule(pppoe_softintr);
1948 	}
1949 	return;
1950 }
1951 
1952 void
1953 pppoe_input(struct ifnet *ifp, struct mbuf *m)
1954 {
1955 	pppoe_enqueue(&ppoeinq, m);
1956 	return;
1957 }
1958 
1959 void
1960 pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
1961 {
1962 	pppoe_enqueue(&ppoediscinq, m);
1963 	return;
1964 }
1965 
1966 static void
1967 sysctl_net_pppoe_setup(struct sysctllog **clog)
1968 {
1969 	const struct sysctlnode *node = NULL;
1970 
1971 	sysctl_createv(clog, 0, NULL, &node,
1972 	    CTLFLAG_PERMANENT,
1973 	    CTLTYPE_NODE, "pppoe",
1974 	    SYSCTL_DESCR("PPPOE protocol"),
1975 	    NULL, 0, NULL, 0,
1976 	    CTL_NET, CTL_CREATE, CTL_EOL);
1977 
1978 	if (node == NULL)
1979 		return;
1980 
1981 	sysctl_createv(clog, 0, &node, NULL,
1982 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1983 	    CTLTYPE_BOOL, "term_unknown",
1984 	    SYSCTL_DESCR("Terminate unknown sessions"),
1985 	    NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown),
1986 	    CTL_CREATE, CTL_EOL);
1987 }
1988 
1989 /*
1990  * Module infrastructure
1991  */
1992 #include "if_module.h"
1993 
1994 IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")
1995