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