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