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