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