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