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