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