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