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