if_pppoe.c revision 1.135 1 /* $NetBSD: if_pppoe.c,v 1.135 2018/04/18 07:36:26 knakahara 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.135 2018/04/18 07:36:26 knakahara 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 bool term_unknown = pppoe_term_unknown;
964
965 KASSERT(m->m_flags & M_PKTHDR);
966
967 if (term_unknown)
968 memcpy(shost, mtod(m, struct ether_header*)->ether_shost,
969 ETHER_ADDR_LEN);
970 m_adj(m, sizeof(struct ether_header));
971 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
972 printf("pppoe (data): dropping too short packet: %d bytes\n",
973 m->m_pkthdr.len);
974 goto drop;
975 }
976
977 if (m->m_len < sizeof(*ph)) {
978 m = m_pullup(m, sizeof(*ph));
979 if (!m) {
980 printf("pppoe: could not get PPPoE header\n");
981 return;
982 }
983 }
984 ph = mtod(m, struct pppoehdr *);
985
986 if (ph->vertype != PPPOE_VERTYPE) {
987 printf("pppoe (data): unknown version/type packet: 0x%x\n",
988 ph->vertype);
989 goto drop;
990 }
991 if (ph->code != 0)
992 goto drop;
993
994 session = ntohs(ph->session);
995 rcvif = m_get_rcvif_psref(m, &psref);
996 if (__predict_false(rcvif == NULL))
997 goto drop;
998 sc = pppoe_find_softc_by_session(session, rcvif, RW_READER);
999 if (sc == NULL) {
1000 if (term_unknown) {
1001 printf("pppoe: input for unknown session %#x, "
1002 "sending PADT\n", session);
1003 pppoe_send_padt(rcvif, session, shost);
1004 }
1005 m_put_rcvif_psref(rcvif, &psref);
1006 goto drop;
1007 }
1008
1009 m_put_rcvif_psref(rcvif, &psref);
1010
1011 plen = ntohs(ph->plen);
1012
1013 bpf_mtap(&sc->sc_sppp.pp_if, m);
1014
1015 m_adj(m, PPPOE_HEADERLEN);
1016
1017 #ifdef PPPOE_DEBUG
1018 {
1019 struct mbuf *p;
1020
1021 printf("%s: pkthdr.len=%d, pppoe.len=%d",
1022 sc->sc_sppp.pp_if.if_xname, m->m_pkthdr.len, plen);
1023 p = m;
1024 while (p) {
1025 printf(" l=%d", p->m_len);
1026 p = p->m_next;
1027 }
1028 printf("\n");
1029 }
1030 #endif
1031 PPPOE_UNLOCK(sc);
1032
1033 if (m->m_pkthdr.len < plen)
1034 goto drop;
1035
1036 /*
1037 * Fix incoming interface pointer (not the raw ethernet interface
1038 * anymore)
1039 */
1040 m_set_rcvif(m, &sc->sc_sppp.pp_if);
1041
1042 /* pass packet up and account for it */
1043 sc->sc_sppp.pp_if.if_ipackets++;
1044 sppp_input(&sc->sc_sppp.pp_if, m);
1045 return;
1046
1047 drop:
1048 m_freem(m);
1049 }
1050
1051 static int
1052 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
1053 {
1054 struct sockaddr dst;
1055 struct ether_header *eh;
1056 uint16_t etype;
1057
1058 if (sc->sc_eth_if == NULL) {
1059 m_freem(m);
1060 return EIO;
1061 }
1062
1063 memset(&dst, 0, sizeof dst);
1064 dst.sa_family = AF_UNSPEC;
1065 eh = (struct ether_header*)&dst.sa_data;
1066 etype = sc->sc_state == PPPOE_STATE_SESSION
1067 ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
1068 eh->ether_type = htons(etype);
1069 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
1070
1071 #ifdef PPPOE_DEBUG
1072 printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
1073 sc->sc_sppp.pp_if.if_xname, etype,
1074 sc->sc_state, sc->sc_session,
1075 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
1076 #endif
1077
1078 m->m_flags &= ~(M_BCAST|M_MCAST);
1079 sc->sc_sppp.pp_if.if_opackets++;
1080 return if_output_lock(sc->sc_eth_if, sc->sc_eth_if, m, &dst, NULL);
1081 }
1082
1083 static int
1084 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1085 {
1086 struct lwp *l = curlwp; /* XXX */
1087 struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
1088 struct ifreq *ifr = data;
1089 int error = 0;
1090
1091 switch (cmd) {
1092 case PPPOESETPARMS:
1093 {
1094 struct pppoediscparms *parms = (struct pppoediscparms*)data;
1095 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1096 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1097 NULL) != 0)
1098 return EPERM;
1099 if (parms->eth_ifname[0] != 0) {
1100 struct ifnet *eth_if;
1101
1102 PPPOE_LOCK(sc, RW_WRITER);
1103 eth_if = ifunit(parms->eth_ifname);
1104 if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
1105 sc->sc_eth_if = NULL;
1106 PPPOE_UNLOCK(sc);
1107 return ENXIO;
1108 }
1109
1110 if (sc->sc_sppp.pp_if.if_mtu !=
1111 eth_if->if_mtu - PPPOE_OVERHEAD) {
1112 sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
1113 PPPOE_OVERHEAD;
1114 }
1115 sc->sc_eth_if = eth_if;
1116 PPPOE_UNLOCK(sc);
1117 }
1118 if (parms->ac_name != NULL) {
1119 size_t s;
1120 char *b = malloc(parms->ac_name_len + 1, M_DEVBUF,
1121 M_WAITOK);
1122 if (b == NULL)
1123 return ENOMEM;
1124 error = copyinstr(parms->ac_name, b,
1125 parms->ac_name_len+1, &s);
1126 if (error != 0) {
1127 free(b, M_DEVBUF);
1128 return error;
1129 }
1130 if (s != parms->ac_name_len+1) {
1131 free(b, M_DEVBUF);
1132 return EINVAL;
1133 }
1134
1135 PPPOE_LOCK(sc, RW_WRITER);
1136 if (sc->sc_concentrator_name)
1137 free(sc->sc_concentrator_name, M_DEVBUF);
1138 sc->sc_concentrator_name = b;
1139 PPPOE_UNLOCK(sc);
1140 }
1141 if (parms->service_name != NULL) {
1142 size_t s;
1143 char *b = malloc(parms->service_name_len + 1, M_DEVBUF,
1144 M_WAITOK);
1145 if (b == NULL)
1146 return ENOMEM;
1147 error = copyinstr(parms->service_name, b,
1148 parms->service_name_len+1, &s);
1149 if (error != 0) {
1150 free(b, M_DEVBUF);
1151 return error;
1152 }
1153 if (s != parms->service_name_len+1) {
1154 free(b, M_DEVBUF);
1155 return EINVAL;
1156 }
1157
1158 PPPOE_LOCK(sc, RW_WRITER);
1159 if (sc->sc_service_name)
1160 free(sc->sc_service_name, M_DEVBUF);
1161 sc->sc_service_name = b;
1162 PPPOE_UNLOCK(sc);
1163 }
1164 return 0;
1165 }
1166 break;
1167 case PPPOEGETPARMS:
1168 {
1169 struct pppoediscparms *parms = (struct pppoediscparms*)data;
1170 memset(parms, 0, sizeof *parms);
1171 PPPOE_LOCK(sc, RW_READER);
1172 if (sc->sc_eth_if)
1173 strlcpy(parms->ifname, sc->sc_eth_if->if_xname,
1174 sizeof(parms->ifname));
1175 PPPOE_UNLOCK(sc);
1176 return 0;
1177 }
1178 break;
1179 case PPPOEGETSESSION:
1180 {
1181 struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
1182 PPPOE_LOCK(sc, RW_READER);
1183 state->state = sc->sc_state;
1184 state->session_id = sc->sc_session;
1185 state->padi_retry_no = sc->sc_padi_retried;
1186 state->padr_retry_no = sc->sc_padr_retried;
1187 PPPOE_UNLOCK(sc);
1188 return 0;
1189 }
1190 break;
1191 case SIOCSIFFLAGS:
1192 /*
1193 * Prevent running re-establishment timers overriding
1194 * administrators choice.
1195 */
1196 PPPOE_LOCK(sc, RW_WRITER);
1197
1198 if ((ifr->ifr_flags & IFF_UP) == 0
1199 && sc->sc_state >= PPPOE_STATE_PADI_SENT
1200 && sc->sc_state < PPPOE_STATE_SESSION) {
1201 callout_stop(&sc->sc_timeout);
1202 sc->sc_state = PPPOE_STATE_INITIAL;
1203 sc->sc_padi_retried = 0;
1204 sc->sc_padr_retried = 0;
1205 memcpy(&sc->sc_dest, etherbroadcastaddr,
1206 sizeof(sc->sc_dest));
1207 }
1208
1209 PPPOE_UNLOCK(sc);
1210
1211 error = sppp_ioctl(ifp, cmd, data);
1212
1213 return error;
1214 case SIOCSIFMTU:
1215 if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
1216 PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
1217 return EINVAL;
1218 }
1219 /*FALLTHROUGH*/
1220 default:
1221 return sppp_ioctl(ifp, cmd, data);
1222 }
1223 return 0;
1224 }
1225
1226 /*
1227 * Allocate a mbuf/cluster with space to store the given data length
1228 * of payload, leaving space for prepending an ethernet header
1229 * in front.
1230 */
1231 static struct mbuf *
1232 pppoe_get_mbuf(size_t len)
1233 {
1234 struct mbuf *m;
1235
1236 MGETHDR(m, M_DONTWAIT, MT_DATA);
1237 if (m == NULL)
1238 return NULL;
1239 if (len + sizeof(struct ether_header) > MHLEN) {
1240 MCLGET(m, M_DONTWAIT);
1241 if ((m->m_flags & M_EXT) == 0) {
1242 m_free(m);
1243 return NULL;
1244 }
1245 }
1246 m->m_data += sizeof(struct ether_header);
1247 m->m_len = len;
1248 m->m_pkthdr.len = len;
1249 m_reset_rcvif(m);
1250
1251 return m;
1252 }
1253
1254 static int
1255 pppoe_send_padi(struct pppoe_softc *sc)
1256 {
1257 struct mbuf *m0;
1258 int len, l1 = 0, l2 = 0; /* XXX: gcc */
1259 uint8_t *p;
1260
1261 if (sc->sc_state >PPPOE_STATE_PADI_SENT)
1262 panic("pppoe_send_padi in state %d", sc->sc_state);
1263
1264 /* calculate length of frame (excluding ethernet header + pppoe header) */
1265 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
1266 if (sc->sc_service_name != NULL) {
1267 l1 = strlen(sc->sc_service_name);
1268 len += l1;
1269 }
1270 if (sc->sc_concentrator_name != NULL) {
1271 l2 = strlen(sc->sc_concentrator_name);
1272 len += 2 + 2 + l2;
1273 }
1274 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1275 len += 2 + 2 + 2;
1276 }
1277
1278 /* allocate a buffer */
1279 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); /* header len + payload len */
1280 if (!m0)
1281 return ENOBUFS;
1282
1283 /* fill in pkt */
1284 p = mtod(m0, uint8_t *);
1285 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
1286 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1287 if (sc->sc_service_name != NULL) {
1288 PPPOE_ADD_16(p, l1);
1289 memcpy(p, sc->sc_service_name, l1);
1290 p += l1;
1291 } else {
1292 PPPOE_ADD_16(p, 0);
1293 }
1294 if (sc->sc_concentrator_name != NULL) {
1295 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
1296 PPPOE_ADD_16(p, l2);
1297 memcpy(p, sc->sc_concentrator_name, l2);
1298 p += l2;
1299 }
1300 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1301 PPPOE_ADD_16(p, sizeof(sc));
1302 memcpy(p, &sc, sizeof sc);
1303 p += sizeof(sc);
1304
1305 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1306 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1307 PPPOE_ADD_16(p, 2);
1308 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1309 }
1310
1311 #ifdef PPPOE_DEBUG
1312 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1313 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
1314 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1315 #endif
1316
1317 /* send pkt */
1318 return pppoe_output(sc, m0);
1319 }
1320
1321 static void
1322 pppoe_timeout(void *arg)
1323 {
1324 int retry_wait, err;
1325 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
1326 DECLARE_SPLNET_VARIABLE;
1327
1328 #ifdef PPPOE_DEBUG
1329 printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
1330 #endif
1331
1332 PPPOE_LOCK(sc, RW_WRITER);
1333 switch (sc->sc_state) {
1334 case PPPOE_STATE_INITIAL:
1335 /* delayed connect from pppoe_tls() */
1336 pppoe_connect(sc);
1337 break;
1338 case PPPOE_STATE_PADI_SENT:
1339 /*
1340 * We have two basic ways of retrying:
1341 * - Quick retry mode: try a few times in short sequence
1342 * - Slow retry mode: we already had a connection successfully
1343 * established and will try infinitely (without user
1344 * intervention)
1345 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
1346 * is not set.
1347 */
1348
1349 /* initialize for quick retry mode */
1350 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
1351
1352 ACQUIRE_SPLNET();
1353 sc->sc_padi_retried++;
1354 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
1355 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
1356 /* slow retry mode */
1357 retry_wait = PPPOE_SLOW_RETRY;
1358 } else {
1359 pppoe_abort_connect(sc);
1360 RELEASE_SPLNET();
1361 PPPOE_UNLOCK(sc);
1362 return;
1363 }
1364 }
1365 if ((err = pppoe_send_padi(sc)) != 0) {
1366 sc->sc_padi_retried--;
1367 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1368 printf("%s: failed to transmit PADI, "
1369 "error=%d\n",
1370 sc->sc_sppp.pp_if.if_xname, err);
1371 }
1372 callout_reset(&sc->sc_timeout, retry_wait,
1373 pppoe_timeout, sc);
1374 RELEASE_SPLNET();
1375 break;
1376
1377 case PPPOE_STATE_PADR_SENT:
1378 ACQUIRE_SPLNET();
1379 sc->sc_padr_retried++;
1380 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
1381 memcpy(&sc->sc_dest, etherbroadcastaddr,
1382 sizeof(sc->sc_dest));
1383 sc->sc_state = PPPOE_STATE_PADI_SENT;
1384 sc->sc_padr_retried = 0;
1385 if ((err = pppoe_send_padi(sc)) != 0) {
1386 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1387 printf("%s: failed to send PADI"
1388 ", error=%d\n",
1389 sc->sc_sppp.pp_if.if_xname, err);
1390 }
1391 callout_reset(&sc->sc_timeout,
1392 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried),
1393 pppoe_timeout, sc);
1394 RELEASE_SPLNET();
1395 PPPOE_UNLOCK(sc);
1396 return;
1397 }
1398 if ((err = pppoe_send_padr(sc)) != 0) {
1399 sc->sc_padr_retried--;
1400 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1401 printf("%s: failed to send PADR, "
1402 "error=%d\n", sc->sc_sppp.pp_if.if_xname,
1403 err);
1404 }
1405 callout_reset(&sc->sc_timeout,
1406 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
1407 pppoe_timeout, sc);
1408 RELEASE_SPLNET();
1409 break;
1410 case PPPOE_STATE_CLOSING:
1411 pppoe_disconnect(sc);
1412 break;
1413 default:
1414 PPPOE_UNLOCK(sc);
1415 return; /* all done, work in peace */
1416 }
1417 PPPOE_UNLOCK(sc);
1418 }
1419
1420 /* Start a connection (i.e. initiate discovery phase) */
1421 static int
1422 pppoe_connect(struct pppoe_softc *sc)
1423 {
1424 int err;
1425 DECLARE_SPLNET_VARIABLE;
1426
1427 KASSERT(PPPOE_WLOCKED(sc));
1428
1429 if (sc->sc_state != PPPOE_STATE_INITIAL)
1430 return EBUSY;
1431
1432 #ifdef PPPOE_SERVER
1433 /* wait PADI if IFF_PASSIVE */
1434 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
1435 return 0;
1436 #endif
1437 ACQUIRE_SPLNET();
1438 /* save state, in case we fail to send PADI */
1439 sc->sc_state = PPPOE_STATE_PADI_SENT;
1440 sc->sc_padr_retried = 0;
1441 err = pppoe_send_padi(sc);
1442 if (err != 0 && sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1443 printf("%s: failed to send PADI, error=%d\n",
1444 sc->sc_sppp.pp_if.if_xname, err);
1445 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
1446 RELEASE_SPLNET();
1447 return err;
1448 }
1449
1450 /* disconnect */
1451 static int
1452 pppoe_disconnect(struct pppoe_softc *sc)
1453 {
1454 int err;
1455 DECLARE_SPLNET_VARIABLE;
1456
1457 KASSERT(PPPOE_WLOCKED(sc));
1458
1459 ACQUIRE_SPLNET();
1460
1461 if (sc->sc_state < PPPOE_STATE_SESSION)
1462 err = EBUSY;
1463 else {
1464 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1465 printf("%s: disconnecting\n",
1466 sc->sc_sppp.pp_if.if_xname);
1467 err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session,
1468 (const uint8_t *)&sc->sc_dest);
1469 }
1470
1471 /* cleanup softc */
1472 sc->sc_state = PPPOE_STATE_INITIAL;
1473
1474 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1475 if (sc->sc_ac_cookie) {
1476 free(sc->sc_ac_cookie, M_DEVBUF);
1477 sc->sc_ac_cookie = NULL;
1478 }
1479 sc->sc_ac_cookie_len = 0;
1480 if (sc->sc_relay_sid) {
1481 free(sc->sc_relay_sid, M_DEVBUF);
1482 sc->sc_relay_sid = NULL;
1483 }
1484 sc->sc_relay_sid_len = 0;
1485 #ifdef PPPOE_SERVER
1486 if (sc->sc_hunique) {
1487 free(sc->sc_hunique, M_DEVBUF);
1488 sc->sc_hunique = NULL;
1489 }
1490 sc->sc_hunique_len = 0;
1491 #endif
1492 sc->sc_session = 0;
1493
1494 PPPOE_UNLOCK(sc);
1495
1496 /* notify upper layer */
1497 sc->sc_sppp.pp_down(&sc->sc_sppp);
1498
1499 PPPOE_LOCK(sc, RW_WRITER);
1500
1501 RELEASE_SPLNET();
1502 return err;
1503 }
1504
1505 /* Connection attempt aborted */
1506 static void
1507 pppoe_abort_connect(struct pppoe_softc *sc)
1508 {
1509 KASSERT(PPPOE_WLOCKED(sc));
1510
1511 printf("%s: could not establish connection\n",
1512 sc->sc_sppp.pp_if.if_xname);
1513 sc->sc_state = PPPOE_STATE_CLOSING;
1514
1515 PPPOE_UNLOCK(sc);
1516
1517 /* notify upper layer */
1518 sc->sc_sppp.pp_down(&sc->sc_sppp);
1519
1520 PPPOE_LOCK(sc, RW_WRITER);
1521
1522 /* clear connection state */
1523 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1524 sc->sc_state = PPPOE_STATE_INITIAL;
1525 }
1526
1527 /* Send a PADR packet */
1528 static int
1529 pppoe_send_padr(struct pppoe_softc *sc)
1530 {
1531 struct mbuf *m0;
1532 uint8_t *p;
1533 size_t len, l1 = 0; /* XXX: gcc */
1534
1535 if (sc->sc_state != PPPOE_STATE_PADR_SENT)
1536 return EIO;
1537
1538 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
1539 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1540 l1 = strlen(sc->sc_service_name);
1541 len += l1;
1542 }
1543 if (sc->sc_ac_cookie_len > 0)
1544 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
1545 if (sc->sc_relay_sid_len > 0)
1546 len += 2 + 2 + sc->sc_relay_sid_len; /* Relay SID */
1547 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1548 len += 2 + 2 + 2;
1549 }
1550 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1551 if (!m0)
1552 return ENOBUFS;
1553 p = mtod(m0, uint8_t *);
1554 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1555 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1556 if (sc->sc_service_name != NULL) {
1557 PPPOE_ADD_16(p, l1);
1558 memcpy(p, sc->sc_service_name, l1);
1559 p += l1;
1560 } else {
1561 PPPOE_ADD_16(p, 0);
1562 }
1563 if (sc->sc_ac_cookie_len > 0) {
1564 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1565 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1566 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1567 p += sc->sc_ac_cookie_len;
1568 }
1569 if (sc->sc_relay_sid_len > 0) {
1570 PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
1571 PPPOE_ADD_16(p, sc->sc_relay_sid_len);
1572 memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
1573 p += sc->sc_relay_sid_len;
1574 }
1575 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1576 PPPOE_ADD_16(p, sizeof(sc));
1577 memcpy(p, &sc, sizeof sc);
1578 p += sizeof(sc);
1579
1580 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1581 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1582 PPPOE_ADD_16(p, 2);
1583 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1584 }
1585
1586 #ifdef PPPOE_DEBUG
1587 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1588 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
1589 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1590 #endif
1591
1592 return pppoe_output(sc, m0);
1593 }
1594
1595 /* send a PADT packet */
1596 static int
1597 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
1598 {
1599 struct ether_header *eh;
1600 struct sockaddr dst;
1601 struct mbuf *m0;
1602 uint8_t *p;
1603
1604 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
1605 if (!m0)
1606 return EIO;
1607 p = mtod(m0, uint8_t *);
1608 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
1609
1610 memset(&dst, 0, sizeof dst);
1611 dst.sa_family = AF_UNSPEC;
1612 eh = (struct ether_header*)&dst.sa_data;
1613 eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
1614 memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
1615
1616 m0->m_flags &= ~(M_BCAST|M_MCAST);
1617 return if_output_lock(outgoing_if, outgoing_if, m0, &dst, NULL);
1618 }
1619
1620 #ifdef PPPOE_SERVER
1621 static int
1622 pppoe_send_pado(struct pppoe_softc *sc)
1623 {
1624 struct mbuf *m0;
1625 uint8_t *p;
1626 size_t len;
1627
1628 if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1629 return EIO;
1630
1631 /* calc length */
1632 len = 0;
1633 /* include ac_cookie */
1634 len += 2 + 2 + sizeof(sc);
1635 /* include hunique */
1636 len += 2 + 2 + sc->sc_hunique_len;
1637 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1638 if (!m0)
1639 return EIO;
1640 p = mtod(m0, uint8_t *);
1641 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1642 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1643 PPPOE_ADD_16(p, sizeof(sc));
1644 memcpy(p, &sc, sizeof(sc));
1645 p += sizeof(sc);
1646 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1647 PPPOE_ADD_16(p, sc->sc_hunique_len);
1648 memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1649 return pppoe_output(sc, m0);
1650 }
1651
1652 static int
1653 pppoe_send_pads(struct pppoe_softc *sc)
1654 {
1655 struct bintime bt;
1656 struct mbuf *m0;
1657 uint8_t *p;
1658 size_t len, l1 = 0; /* XXX: gcc */
1659
1660 KASSERT(PPPOE_WLOCKED(sc));
1661
1662 if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1663 return EIO;
1664
1665 getbinuptime(&bt);
1666 sc->sc_session = bt.sec % 0xff + 1;
1667 /* calc length */
1668 len = 0;
1669 /* include hunique */
1670 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
1671 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1672 l1 = strlen(sc->sc_service_name);
1673 len += l1;
1674 }
1675 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1676 if (!m0)
1677 return ENOBUFS;
1678 p = mtod(m0, uint8_t *);
1679 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1680 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1681 if (sc->sc_service_name != NULL) {
1682 PPPOE_ADD_16(p, l1);
1683 memcpy(p, sc->sc_service_name, l1);
1684 p += l1;
1685 } else {
1686 PPPOE_ADD_16(p, 0);
1687 }
1688 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1689 PPPOE_ADD_16(p, sc->sc_hunique_len);
1690 memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1691 return pppoe_output(sc, m0);
1692 }
1693 #endif
1694
1695 static void
1696 pppoe_tls(struct sppp *sp)
1697 {
1698 struct pppoe_softc *sc = (void *)sp;
1699 int wtime;
1700
1701 PPPOE_LOCK(sc, RW_READER);
1702
1703 if (sc->sc_state != PPPOE_STATE_INITIAL) {
1704 PPPOE_UNLOCK(sc);
1705 return;
1706 }
1707
1708 if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
1709 sc->sc_sppp.pp_auth_failures > 0) {
1710 /*
1711 * Delay trying to reconnect a bit more - the peer
1712 * might have failed to contact its radius server.
1713 */
1714 wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
1715 if (wtime > PPPOE_SLOW_RETRY)
1716 wtime = PPPOE_SLOW_RETRY;
1717 } else {
1718 wtime = PPPOE_RECON_IMMEDIATE;
1719 }
1720 callout_reset(&sc->sc_timeout, wtime, pppoe_timeout, sc);
1721
1722 PPPOE_UNLOCK(sc);
1723 }
1724
1725 static void
1726 pppoe_tlf(struct sppp *sp)
1727 {
1728 struct pppoe_softc *sc = (void *)sp;
1729
1730 PPPOE_LOCK(sc, RW_WRITER);
1731
1732 if (sc->sc_state < PPPOE_STATE_SESSION) {
1733 PPPOE_UNLOCK(sc);
1734 return;
1735 }
1736 /*
1737 * Do not call pppoe_disconnect here, the upper layer state
1738 * machine gets confused by this. We must return from this
1739 * function and defer disconnecting to the timeout handler.
1740 */
1741 sc->sc_state = PPPOE_STATE_CLOSING;
1742
1743 callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc);
1744
1745 PPPOE_UNLOCK(sc);
1746 }
1747
1748 static void
1749 pppoe_start(struct ifnet *ifp)
1750 {
1751 struct pppoe_softc *sc = (void *)ifp;
1752 struct mbuf *m;
1753 uint8_t *p;
1754 size_t len;
1755
1756 if (sppp_isempty(ifp))
1757 return;
1758
1759 /* are we ready to process data yet? */
1760 PPPOE_LOCK(sc, RW_READER);
1761 if (sc->sc_state < PPPOE_STATE_SESSION) {
1762 sppp_flush(&sc->sc_sppp.pp_if);
1763 PPPOE_UNLOCK(sc);
1764 return;
1765 }
1766
1767 while ((m = sppp_dequeue(ifp)) != NULL) {
1768 len = m->m_pkthdr.len;
1769 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1770 if (m == NULL) {
1771 ifp->if_oerrors++;
1772 continue;
1773 }
1774 p = mtod(m, uint8_t *);
1775 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1776
1777 bpf_mtap(&sc->sc_sppp.pp_if, m);
1778
1779 pppoe_output(sc, m);
1780 }
1781 PPPOE_UNLOCK(sc);
1782 }
1783
1784 #ifdef PPPOE_MPSAFE
1785 static int
1786 pppoe_transmit(struct ifnet *ifp, struct mbuf *m)
1787 {
1788 struct pppoe_softc *sc = (void *)ifp;
1789 uint8_t *p;
1790 size_t len;
1791
1792 if (m == NULL)
1793 return EINVAL;
1794
1795 /* are we ready to process data yet? */
1796 PPPOE_LOCK(sc, RW_READER);
1797 if (sc->sc_state < PPPOE_STATE_SESSION) {
1798 PPPOE_UNLOCK(sc);
1799 m_freem(m);
1800 return ENOBUFS;
1801 }
1802
1803 len = m->m_pkthdr.len;
1804 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1805 if (m == NULL) {
1806 PPPOE_UNLOCK(sc);
1807 ifp->if_oerrors++;
1808 return ENETDOWN;
1809 }
1810 p = mtod(m, uint8_t *);
1811 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1812
1813 bpf_mtap(&sc->sc_sppp.pp_if, m);
1814
1815 pppoe_output(sc, m);
1816 PPPOE_UNLOCK(sc);
1817 return 0;
1818 }
1819 #endif /* PPPOE_MPSAFE */
1820
1821 static void
1822 pppoe_ifattach_hook(void *arg, unsigned long cmd, void *arg2)
1823 {
1824 struct ifnet *ifp = arg2;
1825 struct pppoe_softc *sc;
1826 DECLARE_SPLNET_VARIABLE;
1827
1828 if (cmd != PFIL_IFNET_DETACH)
1829 return;
1830
1831 ACQUIRE_SPLNET();
1832 rw_enter(&pppoe_softc_list_lock, RW_READER);
1833 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1834 PPPOE_LOCK(sc, RW_WRITER);
1835 if (sc->sc_eth_if != ifp) {
1836 PPPOE_UNLOCK(sc);
1837 continue;
1838 }
1839 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1840 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1841 printf("%s: ethernet interface detached, going down\n",
1842 sc->sc_sppp.pp_if.if_xname);
1843 }
1844 sc->sc_eth_if = NULL;
1845 pppoe_clear_softc(sc, "ethernet interface detached");
1846 PPPOE_UNLOCK(sc);
1847 }
1848 rw_exit(&pppoe_softc_list_lock);
1849 RELEASE_SPLNET();
1850 }
1851
1852 static void
1853 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1854 {
1855 KASSERT(PPPOE_WLOCKED(sc));
1856
1857 /* stop timer */
1858 callout_stop(&sc->sc_timeout);
1859 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1860 printf("%s: session 0x%x terminated, %s\n",
1861 sc->sc_sppp.pp_if.if_xname, sc->sc_session, message);
1862
1863 /* fix our state */
1864 sc->sc_state = PPPOE_STATE_INITIAL;
1865
1866 PPPOE_UNLOCK(sc);
1867
1868 /* signal upper layer */
1869 sc->sc_sppp.pp_down(&sc->sc_sppp);
1870
1871 PPPOE_LOCK(sc, RW_WRITER);
1872
1873 /* clean up softc */
1874 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1875 if (sc->sc_ac_cookie) {
1876 free(sc->sc_ac_cookie, M_DEVBUF);
1877 sc->sc_ac_cookie = NULL;
1878 }
1879 if (sc->sc_relay_sid) {
1880 free(sc->sc_relay_sid, M_DEVBUF);
1881 sc->sc_relay_sid = NULL;
1882 }
1883 sc->sc_ac_cookie_len = 0;
1884 sc->sc_session = 0;
1885 }
1886
1887 static void
1888 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
1889 {
1890 if (m->m_flags & M_PROMISC) {
1891 m_freem(m);
1892 return;
1893 }
1894
1895 #ifndef PPPOE_SERVER
1896 if (m->m_flags & (M_MCAST | M_BCAST)) {
1897 m_freem(m);
1898 return;
1899 }
1900 #endif
1901
1902 IFQ_LOCK(inq);
1903 if (IF_QFULL(inq)) {
1904 IF_DROP(inq);
1905 IFQ_UNLOCK(inq);
1906 m_freem(m);
1907 } else {
1908 IF_ENQUEUE(inq, m);
1909 IFQ_UNLOCK(inq);
1910 softint_schedule(pppoe_softintr);
1911 }
1912 return;
1913 }
1914
1915 void
1916 pppoe_input(struct ifnet *ifp, struct mbuf *m)
1917 {
1918 pppoe_enqueue(&ppoeinq, m);
1919 return;
1920 }
1921
1922 void
1923 pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
1924 {
1925 pppoe_enqueue(&ppoediscinq, m);
1926 return;
1927 }
1928
1929 static void
1930 sysctl_net_pppoe_setup(struct sysctllog **clog)
1931 {
1932 const struct sysctlnode *node = NULL;
1933
1934 sysctl_createv(clog, 0, NULL, &node,
1935 CTLFLAG_PERMANENT,
1936 CTLTYPE_NODE, "pppoe",
1937 SYSCTL_DESCR("PPPOE protocol"),
1938 NULL, 0, NULL, 0,
1939 CTL_NET, CTL_CREATE, CTL_EOL);
1940
1941 if (node == NULL)
1942 return;
1943
1944 sysctl_createv(clog, 0, &node, NULL,
1945 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1946 CTLTYPE_BOOL, "term_unknown",
1947 SYSCTL_DESCR("Terminate unknown sessions"),
1948 NULL, 0, &pppoe_term_unknown, sizeof(pppoe_term_unknown),
1949 CTL_CREATE, CTL_EOL);
1950 }
1951
1952 /*
1953 * Module infrastructure
1954 */
1955 #include "if_module.h"
1956
1957 IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")
1958