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