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