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