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