if_pppoe.c revision 1.105 1 /* $NetBSD: if_pppoe.c,v 1.105 2016/04/15 01:31:29 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.105 2016/04/15 01:31:29 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 strncpy(error,
489 mtod(n, char*) + noff,
490 len);
491 error[len] = '\0';
492 }
493 printf("%s: connected to %s\n",
494 devname, error);
495 free(error, M_TEMP);
496 }
497 }
498 break; /* ignored */
499 case PPPOE_TAG_HUNIQUE:
500 if (sc != NULL)
501 break;
502 n = m_pulldown(m, off + sizeof(*pt), len, &noff);
503 if (!n) {
504 m = NULL;
505 err_msg = "TAG HUNIQUE ERROR";
506 break;
507 }
508 #ifdef PPPOE_SERVER
509 hunique = mtod(n, uint8_t *) + noff;
510 hunique_len = len;
511 #endif
512 sc = pppoe_find_softc_by_hunique(mtod(n, char *) + noff,
513 len, m->m_pkthdr.rcvif);
514 if (sc != NULL)
515 devname = sc->sc_sppp.pp_if.if_xname;
516 break;
517 case PPPOE_TAG_ACCOOKIE:
518 if (ac_cookie == NULL) {
519 n = m_pulldown(m, off + sizeof(*pt), len,
520 &noff);
521 if (!n) {
522 err_msg = "TAG ACCOOKIE ERROR";
523 m = NULL;
524 break;
525 }
526 ac_cookie = mtod(n, char *) + noff;
527 ac_cookie_len = len;
528 }
529 break;
530 case PPPOE_TAG_RELAYSID:
531 if (relay_sid == NULL) {
532 n = m_pulldown(m, off + sizeof(*pt), len,
533 &noff);
534 if (!n) {
535 err_msg = "TAG RELAYSID ERROR";
536 m = NULL;
537 break;
538 }
539 relay_sid = mtod(n, char *) + noff;
540 relay_sid_len = len;
541 }
542 break;
543 case PPPOE_TAG_SNAME_ERR:
544 err_msg = "SERVICE NAME ERROR";
545 errortag = 1;
546 break;
547 case PPPOE_TAG_ACSYS_ERR:
548 err_msg = "AC SYSTEM ERROR";
549 errortag = 1;
550 break;
551 case PPPOE_TAG_GENERIC_ERR:
552 err_msg = "GENERIC ERROR";
553 errortag = 1;
554 break;
555 }
556 if (err_msg) {
557 error = NULL;
558 if (errortag && len) {
559 error = malloc(len+1, M_TEMP, M_NOWAIT);
560 n = m_pulldown(m, off + sizeof(*pt), len,
561 &noff);
562 if (n && error) {
563 strncpy(error,
564 mtod(n, char *) + noff, len);
565 error[len] = '\0';
566 }
567 }
568 if (error) {
569 printf("%s: %s: %s\n", devname,
570 err_msg, error);
571 free(error, M_TEMP);
572 } else
573 printf("%s: %s\n", devname, err_msg);
574 if (errortag || m == NULL)
575 goto done;
576 }
577 off += sizeof(*pt) + len;
578 }
579 breakbreak:;
580 switch (ph->code) {
581 case PPPOE_CODE_PADI:
582 #ifdef PPPOE_SERVER
583 /*
584 * got service name, concentrator name, and/or host unique.
585 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
586 */
587 if (LIST_EMPTY(&pppoe_softc_list))
588 goto done;
589 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
590 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP))
591 continue;
592 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
593 continue;
594 if (sc->sc_state == PPPOE_STATE_INITIAL)
595 break;
596 }
597 if (sc == NULL) {
598 /* printf("pppoe: free passive interface is not found\n");*/
599 goto done;
600 }
601 if (hunique) {
602 if (sc->sc_hunique)
603 free(sc->sc_hunique, M_DEVBUF);
604 sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
605 M_DONTWAIT);
606 if (sc->sc_hunique == NULL)
607 goto done;
608 sc->sc_hunique_len = hunique_len;
609 memcpy(sc->sc_hunique, hunique, hunique_len);
610 }
611 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
612 sc->sc_state = PPPOE_STATE_PADO_SENT;
613 pppoe_send_pado(sc);
614 break;
615 #endif /* PPPOE_SERVER */
616 case PPPOE_CODE_PADR:
617 #ifdef PPPOE_SERVER
618 /*
619 * get sc from ac_cookie if IFF_PASSIVE
620 */
621 if (ac_cookie == NULL) {
622 /* be quiet if there is not a single pppoe instance */
623 printf("pppoe: received PADR but not includes ac_cookie\n");
624 goto done;
625 }
626 sc = pppoe_find_softc_by_hunique(ac_cookie,
627 ac_cookie_len,
628 m->m_pkthdr.rcvif);
629 if (sc == NULL) {
630 /* be quiet if there is not a single pppoe instance */
631 if (!LIST_EMPTY(&pppoe_softc_list))
632 printf("pppoe: received PADR but could not find request for it\n");
633 goto done;
634 }
635 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
636 printf("%s: received unexpected PADR\n",
637 sc->sc_sppp.pp_if.if_xname);
638 goto done;
639 }
640 if (hunique) {
641 if (sc->sc_hunique)
642 free(sc->sc_hunique, M_DEVBUF);
643 sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
644 M_DONTWAIT);
645 if (sc->sc_hunique == NULL)
646 goto done;
647 sc->sc_hunique_len = hunique_len;
648 memcpy(sc->sc_hunique, hunique, hunique_len);
649 }
650 pppoe_send_pads(sc);
651 sc->sc_state = PPPOE_STATE_SESSION;
652 sc->sc_sppp.pp_up(&sc->sc_sppp);
653 break;
654 #else
655 /* ignore, we are no access concentrator */
656 goto done;
657 #endif /* PPPOE_SERVER */
658 case PPPOE_CODE_PADO:
659 if (sc == NULL) {
660 /* be quiet if there is not a single pppoe instance */
661 if (!LIST_EMPTY(&pppoe_softc_list))
662 printf("pppoe: received PADO but could not find request for it\n");
663 goto done;
664 }
665 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
666 printf("%s: received unexpected PADO\n",
667 sc->sc_sppp.pp_if.if_xname);
668 goto done;
669 }
670 if (ac_cookie) {
671 if (sc->sc_ac_cookie)
672 free(sc->sc_ac_cookie, M_DEVBUF);
673 sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
674 M_DONTWAIT);
675 if (sc->sc_ac_cookie == NULL) {
676 printf("%s: FATAL: could not allocate memory "
677 "for AC cookie\n",
678 sc->sc_sppp.pp_if.if_xname);
679 goto done;
680 }
681 sc->sc_ac_cookie_len = ac_cookie_len;
682 memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
683 }
684 if (relay_sid) {
685 if (sc->sc_relay_sid)
686 free(sc->sc_relay_sid, M_DEVBUF);
687 sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF,
688 M_DONTWAIT);
689 if (sc->sc_relay_sid == NULL) {
690 printf("%s: FATAL: could not allocate memory "
691 "for relay SID\n",
692 sc->sc_sppp.pp_if.if_xname);
693 goto done;
694 }
695 sc->sc_relay_sid_len = relay_sid_len;
696 memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len);
697 }
698 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
699 callout_stop(&sc->sc_timeout);
700 sc->sc_padr_retried = 0;
701 sc->sc_state = PPPOE_STATE_PADR_SENT;
702 if ((err = pppoe_send_padr(sc)) != 0) {
703 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
704 printf("%s: failed to send PADR, "
705 "error=%d\n", sc->sc_sppp.pp_if.if_xname,
706 err);
707 }
708 callout_reset(&sc->sc_timeout,
709 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
710 pppoe_timeout, sc);
711 break;
712 case PPPOE_CODE_PADS:
713 if (sc == NULL)
714 goto done;
715 sc->sc_session = session;
716 callout_stop(&sc->sc_timeout);
717 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
718 printf("%s: session 0x%x connected\n",
719 sc->sc_sppp.pp_if.if_xname, session);
720 sc->sc_state = PPPOE_STATE_SESSION;
721 sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */
722 break;
723 case PPPOE_CODE_PADT:
724 sc = pppoe_find_softc_by_session(session, m->m_pkthdr.rcvif);
725 if (sc == NULL)
726 goto done;
727 pppoe_clear_softc(sc, "received PADT");
728 break;
729 default:
730 printf("%s: unknown code (0x%04x) session = 0x%04x\n",
731 sc? sc->sc_sppp.pp_if.if_xname : "pppoe",
732 ph->code, session);
733 break;
734 }
735
736 done:
737 if (m)
738 m_freem(m);
739 return;
740 }
741
742 static void
743 pppoe_disc_input(struct mbuf *m)
744 {
745
746 /* avoid error messages if there is not a single pppoe instance */
747 if (!LIST_EMPTY(&pppoe_softc_list)) {
748 KASSERT(m->m_flags & M_PKTHDR);
749 pppoe_dispatch_disc_pkt(m, 0);
750 } else
751 m_freem(m);
752 }
753
754 static void
755 pppoe_data_input(struct mbuf *m)
756 {
757 uint16_t session, plen;
758 struct pppoe_softc *sc;
759 struct pppoehdr *ph;
760 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
761 uint8_t shost[ETHER_ADDR_LEN];
762 #endif
763
764 KASSERT(m->m_flags & M_PKTHDR);
765
766 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
767 memcpy(shost, mtod(m, struct ether_header*)->ether_shost, ETHER_ADDR_LEN);
768 #endif
769 m_adj(m, sizeof(struct ether_header));
770 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
771 printf("pppoe (data): dropping too short packet: %d bytes\n",
772 m->m_pkthdr.len);
773 goto drop;
774 }
775
776 if (m->m_len < sizeof(*ph)) {
777 m = m_pullup(m, sizeof(*ph));
778 if (!m) {
779 printf("pppoe: could not get PPPoE header\n");
780 return;
781 }
782 }
783 ph = mtod(m, struct pppoehdr *);
784
785 if (ph->vertype != PPPOE_VERTYPE) {
786 printf("pppoe (data): unknown version/type packet: 0x%x\n",
787 ph->vertype);
788 goto drop;
789 }
790 if (ph->code != 0)
791 goto drop;
792
793 session = ntohs(ph->session);
794 sc = pppoe_find_softc_by_session(session, m->m_pkthdr.rcvif);
795 if (sc == NULL) {
796 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
797 printf("pppoe: input for unknown session 0x%x, sending PADT\n",
798 session);
799 pppoe_send_padt(m->m_pkthdr.rcvif, session, shost);
800 #endif
801 goto drop;
802 }
803
804 plen = ntohs(ph->plen);
805
806 bpf_mtap(&sc->sc_sppp.pp_if, m);
807
808 m_adj(m, PPPOE_HEADERLEN);
809
810 #ifdef PPPOE_DEBUG
811 {
812 struct mbuf *p;
813
814 printf("%s: pkthdr.len=%d, pppoe.len=%d",
815 sc->sc_sppp.pp_if.if_xname,
816 m->m_pkthdr.len, plen);
817 p = m;
818 while (p) {
819 printf(" l=%d", p->m_len);
820 p = p->m_next;
821 }
822 printf("\n");
823 }
824 #endif
825
826 if (m->m_pkthdr.len < plen)
827 goto drop;
828
829 /* fix incoming interface pointer (not the raw ethernet interface anymore) */
830 m->m_pkthdr.rcvif = &sc->sc_sppp.pp_if;
831
832 /* pass packet up and account for it */
833 sc->sc_sppp.pp_if.if_ipackets++;
834 sppp_input(&sc->sc_sppp.pp_if, m);
835 return;
836
837 drop:
838 m_freem(m);
839 }
840
841 static int
842 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
843 {
844 struct sockaddr dst;
845 struct ether_header *eh;
846 uint16_t etype;
847
848 if (sc->sc_eth_if == NULL) {
849 m_freem(m);
850 return EIO;
851 }
852
853 memset(&dst, 0, sizeof dst);
854 dst.sa_family = AF_UNSPEC;
855 eh = (struct ether_header*)&dst.sa_data;
856 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
857 eh->ether_type = htons(etype);
858 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
859
860 #ifdef PPPOE_DEBUG
861 printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
862 sc->sc_sppp.pp_if.if_xname, etype,
863 sc->sc_state, sc->sc_session,
864 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
865 #endif
866
867 m->m_flags &= ~(M_BCAST|M_MCAST);
868 sc->sc_sppp.pp_if.if_opackets++;
869 return sc->sc_eth_if->if_output(sc->sc_eth_if, m, &dst, NULL);
870 }
871
872 static int
873 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
874 {
875 struct lwp *l = curlwp; /* XXX */
876 struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
877 struct ifreq *ifr = data;
878 int error = 0;
879
880 switch (cmd) {
881 case PPPOESETPARMS:
882 {
883 struct pppoediscparms *parms = (struct pppoediscparms*)data;
884 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
885 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
886 NULL) != 0)
887 return (EPERM);
888 if (parms->eth_ifname[0] != 0) {
889 struct ifnet *eth_if;
890
891 eth_if = ifunit(parms->eth_ifname);
892 if (eth_if == NULL || eth_if->if_dlt != DLT_EN10MB) {
893 sc->sc_eth_if = NULL;
894 return ENXIO;
895 }
896
897 if (sc->sc_sppp.pp_if.if_mtu !=
898 eth_if->if_mtu - PPPOE_OVERHEAD) {
899 sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
900 PPPOE_OVERHEAD;
901 }
902 sc->sc_eth_if = eth_if;
903 }
904 if (parms->ac_name != NULL) {
905 size_t s;
906 char *b = malloc(parms->ac_name_len + 1, M_DEVBUF,
907 M_WAITOK);
908 if (b == NULL)
909 return ENOMEM;
910 error = copyinstr(parms->ac_name, b,
911 parms->ac_name_len+1, &s);
912 if (error != 0) {
913 free(b, M_DEVBUF);
914 return error;
915 }
916 if (s != parms->ac_name_len+1) {
917 free(b, M_DEVBUF);
918 return EINVAL;
919 }
920 if (sc->sc_concentrator_name)
921 free(sc->sc_concentrator_name, M_DEVBUF);
922 sc->sc_concentrator_name = b;
923 }
924 if (parms->service_name != NULL) {
925 size_t s;
926 char *b = malloc(parms->service_name_len + 1, M_DEVBUF,
927 M_WAITOK);
928 if (b == NULL)
929 return ENOMEM;
930 error = copyinstr(parms->service_name, b,
931 parms->service_name_len+1, &s);
932 if (error != 0) {
933 free(b, M_DEVBUF);
934 return error;
935 }
936 if (s != parms->service_name_len+1) {
937 free(b, M_DEVBUF);
938 return EINVAL;
939 }
940 if (sc->sc_service_name)
941 free(sc->sc_service_name, M_DEVBUF);
942 sc->sc_service_name = b;
943 }
944 return 0;
945 }
946 break;
947 case PPPOEGETPARMS:
948 {
949 struct pppoediscparms *parms = (struct pppoediscparms*)data;
950 memset(parms, 0, sizeof *parms);
951 if (sc->sc_eth_if)
952 strncpy(parms->ifname, sc->sc_eth_if->if_xname, IFNAMSIZ);
953 return 0;
954 }
955 break;
956 case PPPOEGETSESSION:
957 {
958 struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
959 state->state = sc->sc_state;
960 state->session_id = sc->sc_session;
961 state->padi_retry_no = sc->sc_padi_retried;
962 state->padr_retry_no = sc->sc_padr_retried;
963 return 0;
964 }
965 break;
966 case SIOCSIFFLAGS:
967 /*
968 * Prevent running re-establishment timers overriding
969 * administrators choice.
970 */
971 if ((ifr->ifr_flags & IFF_UP) == 0
972 && sc->sc_state >= PPPOE_STATE_PADI_SENT
973 && sc->sc_state < PPPOE_STATE_SESSION) {
974 callout_stop(&sc->sc_timeout);
975 sc->sc_state = PPPOE_STATE_INITIAL;
976 sc->sc_padi_retried = 0;
977 sc->sc_padr_retried = 0;
978 memcpy(&sc->sc_dest, etherbroadcastaddr,
979 sizeof(sc->sc_dest));
980 }
981 return sppp_ioctl(ifp, cmd, data);
982 case SIOCSIFMTU:
983 if (ifr->ifr_mtu > (sc->sc_eth_if == NULL ?
984 PPPOE_MAXMTU : (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))) {
985 return EINVAL;
986 }
987 /*FALLTHROUGH*/
988 default:
989 return sppp_ioctl(ifp, cmd, data);
990 }
991 return 0;
992 }
993
994 /*
995 * Allocate a mbuf/cluster with space to store the given data length
996 * of payload, leaving space for prepending an ethernet header
997 * in front.
998 */
999 static struct mbuf *
1000 pppoe_get_mbuf(size_t len)
1001 {
1002 struct mbuf *m;
1003
1004 MGETHDR(m, M_DONTWAIT, MT_DATA);
1005 if (m == NULL)
1006 return NULL;
1007 if (len + sizeof(struct ether_header) > MHLEN) {
1008 MCLGET(m, M_DONTWAIT);
1009 if ((m->m_flags & M_EXT) == 0) {
1010 m_free(m);
1011 return NULL;
1012 }
1013 }
1014 m->m_data += sizeof(struct ether_header);
1015 m->m_len = len;
1016 m->m_pkthdr.len = len;
1017 m->m_pkthdr.rcvif = NULL;
1018
1019 return m;
1020 }
1021
1022 static int
1023 pppoe_send_padi(struct pppoe_softc *sc)
1024 {
1025 struct mbuf *m0;
1026 int len, l1 = 0, l2 = 0; /* XXX: gcc */
1027 uint8_t *p;
1028
1029 if (sc->sc_state >PPPOE_STATE_PADI_SENT)
1030 panic("pppoe_send_padi in state %d", sc->sc_state);
1031
1032 /* calculate length of frame (excluding ethernet header + pppoe header) */
1033 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
1034 if (sc->sc_service_name != NULL) {
1035 l1 = strlen(sc->sc_service_name);
1036 len += l1;
1037 }
1038 if (sc->sc_concentrator_name != NULL) {
1039 l2 = strlen(sc->sc_concentrator_name);
1040 len += 2 + 2 + l2;
1041 }
1042 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1043 len += 2 + 2 + 2;
1044 }
1045
1046 /* allocate a buffer */
1047 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); /* header len + payload len */
1048 if (!m0)
1049 return ENOBUFS;
1050
1051 /* fill in pkt */
1052 p = mtod(m0, uint8_t *);
1053 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
1054 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1055 if (sc->sc_service_name != NULL) {
1056 PPPOE_ADD_16(p, l1);
1057 memcpy(p, sc->sc_service_name, l1);
1058 p += l1;
1059 } else {
1060 PPPOE_ADD_16(p, 0);
1061 }
1062 if (sc->sc_concentrator_name != NULL) {
1063 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
1064 PPPOE_ADD_16(p, l2);
1065 memcpy(p, sc->sc_concentrator_name, l2);
1066 p += l2;
1067 }
1068 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1069 PPPOE_ADD_16(p, sizeof(sc));
1070 memcpy(p, &sc, sizeof sc);
1071 p += sizeof(sc);
1072
1073 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1074 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1075 PPPOE_ADD_16(p, 2);
1076 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1077 }
1078
1079 #ifdef PPPOE_DEBUG
1080 p += sizeof sc;
1081 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1082 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
1083 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1084 #endif
1085
1086 /* send pkt */
1087 return pppoe_output(sc, m0);
1088 }
1089
1090 static void
1091 pppoe_timeout(void *arg)
1092 {
1093 int x, retry_wait, err;
1094 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
1095
1096 #ifdef PPPOE_DEBUG
1097 printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
1098 #endif
1099
1100 switch (sc->sc_state) {
1101 case PPPOE_STATE_INITIAL:
1102 /* delayed connect from pppoe_tls() */
1103 pppoe_connect(sc);
1104 break;
1105 case PPPOE_STATE_PADI_SENT:
1106 /*
1107 * We have two basic ways of retrying:
1108 * - Quick retry mode: try a few times in short sequence
1109 * - Slow retry mode: we already had a connection successfully
1110 * established and will try infinitely (without user
1111 * intervention)
1112 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
1113 * is not set.
1114 */
1115
1116 /* initialize for quick retry mode */
1117 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
1118
1119 x = splnet();
1120 sc->sc_padi_retried++;
1121 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
1122 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
1123 /* slow retry mode */
1124 retry_wait = PPPOE_SLOW_RETRY;
1125 } else {
1126 pppoe_abort_connect(sc);
1127 splx(x);
1128 return;
1129 }
1130 }
1131 if ((err = pppoe_send_padi(sc)) != 0) {
1132 sc->sc_padi_retried--;
1133 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1134 printf("%s: failed to transmit PADI, "
1135 "error=%d\n",
1136 sc->sc_sppp.pp_if.if_xname, err);
1137 }
1138 callout_reset(&sc->sc_timeout, retry_wait,
1139 pppoe_timeout, sc);
1140 splx(x);
1141 break;
1142
1143 case PPPOE_STATE_PADR_SENT:
1144 x = splnet();
1145 sc->sc_padr_retried++;
1146 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
1147 memcpy(&sc->sc_dest, etherbroadcastaddr,
1148 sizeof(sc->sc_dest));
1149 sc->sc_state = PPPOE_STATE_PADI_SENT;
1150 sc->sc_padr_retried = 0;
1151 if ((err = pppoe_send_padi(sc)) != 0) {
1152 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1153 printf("%s: failed to send PADI"
1154 ", error=%d\n",
1155 sc->sc_sppp.pp_if.if_xname,
1156 err);
1157 }
1158 callout_reset(&sc->sc_timeout,
1159 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried),
1160 pppoe_timeout, sc);
1161 splx(x);
1162 return;
1163 }
1164 if ((err = pppoe_send_padr(sc)) != 0) {
1165 sc->sc_padr_retried--;
1166 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1167 printf("%s: failed to send PADR, "
1168 "error=%d\n", sc->sc_sppp.pp_if.if_xname,
1169 err);
1170 }
1171 callout_reset(&sc->sc_timeout,
1172 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
1173 pppoe_timeout, sc);
1174 splx(x);
1175 break;
1176 case PPPOE_STATE_CLOSING:
1177 pppoe_disconnect(sc);
1178 break;
1179 default:
1180 return; /* all done, work in peace */
1181 }
1182 }
1183
1184 /* Start a connection (i.e. initiate discovery phase) */
1185 static int
1186 pppoe_connect(struct pppoe_softc *sc)
1187 {
1188 int x, err;
1189
1190 if (sc->sc_state != PPPOE_STATE_INITIAL)
1191 return EBUSY;
1192
1193 #ifdef PPPOE_SERVER
1194 /* wait PADI if IFF_PASSIVE */
1195 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
1196 return 0;
1197 #endif
1198 x = splnet();
1199 /* save state, in case we fail to send PADI */
1200 sc->sc_state = PPPOE_STATE_PADI_SENT;
1201 sc->sc_padr_retried = 0;
1202 err = pppoe_send_padi(sc);
1203 if (err != 0 && sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1204 printf("%s: failed to send PADI, error=%d\n",
1205 sc->sc_sppp.pp_if.if_xname, err);
1206 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
1207 splx(x);
1208 return err;
1209 }
1210
1211 /* disconnect */
1212 static int
1213 pppoe_disconnect(struct pppoe_softc *sc)
1214 {
1215 int err, x;
1216
1217 x = splnet();
1218
1219 if (sc->sc_state < PPPOE_STATE_SESSION)
1220 err = EBUSY;
1221 else {
1222 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1223 printf("%s: disconnecting\n",
1224 sc->sc_sppp.pp_if.if_xname);
1225 err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session, (const uint8_t *)&sc->sc_dest);
1226 }
1227
1228 /* cleanup softc */
1229 sc->sc_state = PPPOE_STATE_INITIAL;
1230 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1231 if (sc->sc_ac_cookie) {
1232 free(sc->sc_ac_cookie, M_DEVBUF);
1233 sc->sc_ac_cookie = NULL;
1234 }
1235 sc->sc_ac_cookie_len = 0;
1236 if (sc->sc_relay_sid) {
1237 free(sc->sc_relay_sid, M_DEVBUF);
1238 sc->sc_relay_sid = NULL;
1239 }
1240 sc->sc_relay_sid_len = 0;
1241 #ifdef PPPOE_SERVER
1242 if (sc->sc_hunique) {
1243 free(sc->sc_hunique, M_DEVBUF);
1244 sc->sc_hunique = NULL;
1245 }
1246 sc->sc_hunique_len = 0;
1247 #endif
1248 sc->sc_session = 0;
1249
1250 /* notify upper layer */
1251 sc->sc_sppp.pp_down(&sc->sc_sppp);
1252
1253 splx(x);
1254
1255 return err;
1256 }
1257
1258 /* Connection attempt aborted */
1259 static void
1260 pppoe_abort_connect(struct pppoe_softc *sc)
1261 {
1262 printf("%s: could not establish connection\n",
1263 sc->sc_sppp.pp_if.if_xname);
1264 sc->sc_state = PPPOE_STATE_CLOSING;
1265
1266 /* notify upper layer */
1267 sc->sc_sppp.pp_down(&sc->sc_sppp);
1268
1269 /* clear connection state */
1270 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1271 sc->sc_state = PPPOE_STATE_INITIAL;
1272 }
1273
1274 /* Send a PADR packet */
1275 static int
1276 pppoe_send_padr(struct pppoe_softc *sc)
1277 {
1278 struct mbuf *m0;
1279 uint8_t *p;
1280 size_t len, l1 = 0; /* XXX: gcc */
1281
1282 if (sc->sc_state != PPPOE_STATE_PADR_SENT)
1283 return EIO;
1284
1285 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
1286 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1287 l1 = strlen(sc->sc_service_name);
1288 len += l1;
1289 }
1290 if (sc->sc_ac_cookie_len > 0)
1291 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
1292 if (sc->sc_relay_sid_len > 0)
1293 len += 2 + 2 + sc->sc_relay_sid_len; /* Relay SID */
1294 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1295 len += 2 + 2 + 2;
1296 }
1297 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1298 if (!m0)
1299 return ENOBUFS;
1300 p = mtod(m0, uint8_t *);
1301 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1302 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1303 if (sc->sc_service_name != NULL) {
1304 PPPOE_ADD_16(p, l1);
1305 memcpy(p, sc->sc_service_name, l1);
1306 p += l1;
1307 } else {
1308 PPPOE_ADD_16(p, 0);
1309 }
1310 if (sc->sc_ac_cookie_len > 0) {
1311 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1312 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1313 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1314 p += sc->sc_ac_cookie_len;
1315 }
1316 if (sc->sc_relay_sid_len > 0) {
1317 PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
1318 PPPOE_ADD_16(p, sc->sc_relay_sid_len);
1319 memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
1320 p += sc->sc_relay_sid_len;
1321 }
1322 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1323 PPPOE_ADD_16(p, sizeof(sc));
1324 memcpy(p, &sc, sizeof sc);
1325 p += sizeof(sc);
1326
1327 if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MAXMTU) {
1328 PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
1329 PPPOE_ADD_16(p, 2);
1330 PPPOE_ADD_16(p, (uint16_t)sc->sc_sppp.pp_if.if_mtu);
1331 }
1332
1333 #ifdef PPPOE_DEBUG
1334 p += sizeof sc;
1335 if (p - mtod(m0, uint8_t *) != len + PPPOE_HEADERLEN)
1336 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
1337 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, uint8_t *)));
1338 #endif
1339
1340 return pppoe_output(sc, m0);
1341 }
1342
1343 /* send a PADT packet */
1344 static int
1345 pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const uint8_t *dest)
1346 {
1347 struct ether_header *eh;
1348 struct sockaddr dst;
1349 struct mbuf *m0;
1350 uint8_t *p;
1351
1352 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
1353 if (!m0)
1354 return EIO;
1355 p = mtod(m0, uint8_t *);
1356 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
1357
1358 memset(&dst, 0, sizeof dst);
1359 dst.sa_family = AF_UNSPEC;
1360 eh = (struct ether_header*)&dst.sa_data;
1361 eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
1362 memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
1363
1364 m0->m_flags &= ~(M_BCAST|M_MCAST);
1365 return outgoing_if->if_output(outgoing_if, m0, &dst, NULL);
1366 }
1367
1368 #ifdef PPPOE_SERVER
1369 static int
1370 pppoe_send_pado(struct pppoe_softc *sc)
1371 {
1372 struct mbuf *m0;
1373 uint8_t *p;
1374 size_t len;
1375
1376 if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1377 return EIO;
1378
1379 /* calc length */
1380 len = 0;
1381 /* include ac_cookie */
1382 len += 2 + 2 + sizeof(sc);
1383 /* include hunique */
1384 len += 2 + 2 + sc->sc_hunique_len;
1385 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1386 if (!m0)
1387 return EIO;
1388 p = mtod(m0, uint8_t *);
1389 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1390 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1391 PPPOE_ADD_16(p, sizeof(sc));
1392 memcpy(p, &sc, sizeof(sc));
1393 p += sizeof(sc);
1394 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1395 PPPOE_ADD_16(p, sc->sc_hunique_len);
1396 memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1397 return pppoe_output(sc, m0);
1398 }
1399
1400 static int
1401 pppoe_send_pads(struct pppoe_softc *sc)
1402 {
1403 struct bintime bt;
1404 struct mbuf *m0;
1405 uint8_t *p;
1406 size_t len, l1 = 0; /* XXX: gcc */
1407
1408 if (sc->sc_state != PPPOE_STATE_PADO_SENT)
1409 return EIO;
1410
1411 getbinuptime(&bt);
1412 sc->sc_session = bt.sec % 0xff + 1;
1413 /* calc length */
1414 len = 0;
1415 /* include hunique */
1416 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
1417 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1418 l1 = strlen(sc->sc_service_name);
1419 len += l1;
1420 }
1421 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1422 if (!m0)
1423 return ENOBUFS;
1424 p = mtod(m0, uint8_t *);
1425 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1426 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1427 if (sc->sc_service_name != NULL) {
1428 PPPOE_ADD_16(p, l1);
1429 memcpy(p, sc->sc_service_name, l1);
1430 p += l1;
1431 } else {
1432 PPPOE_ADD_16(p, 0);
1433 }
1434 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1435 PPPOE_ADD_16(p, sc->sc_hunique_len);
1436 memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
1437 return pppoe_output(sc, m0);
1438 }
1439 #endif
1440
1441 static void
1442 pppoe_tls(struct sppp *sp)
1443 {
1444 struct pppoe_softc *sc = (void *)sp;
1445 int wtime;
1446
1447 if (sc->sc_state != PPPOE_STATE_INITIAL)
1448 return;
1449
1450 if (sc->sc_sppp.pp_phase == SPPP_PHASE_ESTABLISH &&
1451 sc->sc_sppp.pp_auth_failures > 0) {
1452 /*
1453 * Delay trying to reconnect a bit more - the peer
1454 * might have failed to contact its radius server.
1455 */
1456 wtime = PPPOE_RECON_FAST * sc->sc_sppp.pp_auth_failures;
1457 if (wtime > PPPOE_SLOW_RETRY)
1458 wtime = PPPOE_SLOW_RETRY;
1459 } else {
1460 wtime = PPPOE_RECON_IMMEDIATE;
1461 }
1462 callout_reset(&sc->sc_timeout, wtime, pppoe_timeout, sc);
1463 }
1464
1465 static void
1466 pppoe_tlf(struct sppp *sp)
1467 {
1468 struct pppoe_softc *sc = (void *)sp;
1469 if (sc->sc_state < PPPOE_STATE_SESSION)
1470 return;
1471 /*
1472 * Do not call pppoe_disconnect here, the upper layer state
1473 * machine gets confused by this. We must return from this
1474 * function and defer disconnecting to the timeout handler.
1475 */
1476 sc->sc_state = PPPOE_STATE_CLOSING;
1477 callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc);
1478 }
1479
1480 static void
1481 pppoe_start(struct ifnet *ifp)
1482 {
1483 struct pppoe_softc *sc = (void *)ifp;
1484 struct mbuf *m;
1485 uint8_t *p;
1486 size_t len;
1487
1488 if (sppp_isempty(ifp))
1489 return;
1490
1491 /* are we ready to process data yet? */
1492 if (sc->sc_state < PPPOE_STATE_SESSION) {
1493 sppp_flush(&sc->sc_sppp.pp_if);
1494 return;
1495 }
1496
1497 while ((m = sppp_dequeue(ifp)) != NULL) {
1498 len = m->m_pkthdr.len;
1499 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1500 if (m == NULL) {
1501 ifp->if_oerrors++;
1502 continue;
1503 }
1504 p = mtod(m, uint8_t *);
1505 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1506
1507 bpf_mtap(&sc->sc_sppp.pp_if, m);
1508
1509 pppoe_output(sc, m);
1510 }
1511 }
1512
1513
1514 static int
1515 pppoe_ifattach_hook(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
1516 {
1517 struct pppoe_softc *sc;
1518 int s;
1519
1520 if (mp != (struct mbuf **)PFIL_IFNET_DETACH)
1521 return 0;
1522
1523 s = splnet();
1524 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1525 if (sc->sc_eth_if != ifp)
1526 continue;
1527 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1528 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1529 printf("%s: ethernet interface detached, going down\n",
1530 sc->sc_sppp.pp_if.if_xname);
1531 }
1532 sc->sc_eth_if = NULL;
1533 pppoe_clear_softc(sc, "ethernet interface detached");
1534 }
1535 splx(s);
1536
1537 return 0;
1538 }
1539
1540 static void
1541 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1542 {
1543 /* stop timer */
1544 callout_stop(&sc->sc_timeout);
1545 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
1546 printf("%s: session 0x%x terminated, %s\n",
1547 sc->sc_sppp.pp_if.if_xname, sc->sc_session, message);
1548
1549 /* fix our state */
1550 sc->sc_state = PPPOE_STATE_INITIAL;
1551
1552 /* signal upper layer */
1553 sc->sc_sppp.pp_down(&sc->sc_sppp);
1554
1555 /* clean up softc */
1556 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1557 if (sc->sc_ac_cookie) {
1558 free(sc->sc_ac_cookie, M_DEVBUF);
1559 sc->sc_ac_cookie = NULL;
1560 }
1561 if (sc->sc_relay_sid) {
1562 free(sc->sc_relay_sid, M_DEVBUF);
1563 sc->sc_relay_sid = NULL;
1564 }
1565 sc->sc_ac_cookie_len = 0;
1566 sc->sc_session = 0;
1567 }
1568
1569 static void
1570 pppoe_enqueue(struct ifqueue *inq, struct mbuf *m)
1571 {
1572 if (m->m_flags & M_PROMISC) {
1573 m_free(m);
1574 return;
1575 }
1576
1577 #ifndef PPPOE_SERVER
1578 if (m->m_flags & (M_MCAST | M_BCAST)) {
1579 m_free(m);
1580 return;
1581 }
1582 #endif
1583
1584 if (IF_QFULL(inq)) {
1585 IF_DROP(inq);
1586 m_freem(m);
1587 } else {
1588 IF_ENQUEUE(inq, m);
1589 softint_schedule(pppoe_softintr);
1590 }
1591 return;
1592 }
1593
1594 void
1595 pppoe_input(struct ifnet *ifp, struct mbuf *m)
1596 {
1597 pppoe_enqueue(&ppoeinq, m);
1598 return;
1599 }
1600
1601 void
1602 pppoedisc_input(struct ifnet *ifp, struct mbuf *m)
1603 {
1604 pppoe_enqueue(&ppoediscinq, m);
1605 return;
1606 }
1607