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