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