if_pppoe.c revision 1.32 1 /* $NetBSD: if_pppoe.c,v 1.32 2002/06/22 17:41:23 yamt 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.32 2002/06/22 17:41:23 yamt Exp $");
41
42 #include "pppoe.h"
43 #include "bpfilter.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/proc.h>
53 #include <sys/ioctl.h>
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 #if NBPFILTER > 0
62 #include <net/bpf.h>
63 #endif
64
65 #include <machine/intr.h>
66
67 #undef PPPOE_DEBUG /* XXX - remove this or make it an option */
68 /* #define PPPOE_DEBUG 1 */
69
70 struct pppoehdr {
71 u_int8_t vertype;
72 u_int8_t code;
73 u_int16_t session;
74 u_int16_t plen;
75 } __attribute__((__packed__));
76
77 struct pppoetag {
78 u_int16_t tag;
79 u_int16_t len;
80 } __attribute__((__packed__));
81
82 #define PPPOE_HEADERLEN sizeof(struct pppoehdr)
83 #define PPPOE_VERTYPE 0x11 /* VER=1, TYPE = 1 */
84
85 #define PPPOE_TAG_EOL 0x0000 /* end of list */
86 #define PPPOE_TAG_SNAME 0x0101 /* service name */
87 #define PPPOE_TAG_ACNAME 0x0102 /* access concentrator name */
88 #define PPPOE_TAG_HUNIQUE 0x0103 /* host unique */
89 #define PPPOE_TAG_ACCOOKIE 0x0104 /* AC cookie */
90 #define PPPOE_TAG_VENDOR 0x0105 /* vendor specific */
91 #define PPPOE_TAG_RELAYSID 0x0110 /* relay session id */
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 /* gerneric 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_HEADERLEN-2)
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_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
120 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
121
122 struct pppoe_softc {
123 struct sppp sc_sppp; /* contains a struct ifnet as first element */
124 LIST_ENTRY(pppoe_softc) sc_list;
125 struct ifnet *sc_eth_if; /* ethernet interface we are using */
126
127 int sc_state; /* discovery phase or session connected */
128 struct ether_addr sc_dest; /* hardware address of concentrator */
129 u_int16_t sc_session; /* PPPoE session id */
130
131 char *sc_service_name; /* if != NULL: requested name of service */
132 char *sc_concentrator_name; /* if != NULL: requested concentrator id */
133 u_int8_t *sc_ac_cookie; /* content of AC cookie we must echo back */
134 size_t sc_ac_cookie_len; /* length of cookie data */
135 struct callout sc_timeout; /* timeout while not in session state */
136 int sc_padi_retried; /* number of PADI retries already done */
137 int sc_padr_retried; /* number of PADR retries already done */
138 };
139
140 /* incoming traffic will be queued here */
141 struct ifqueue ppoediscinq = { NULL };
142 struct ifqueue ppoeinq = { NULL };
143
144 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
145 void * pppoe_softintr = NULL;
146 static void pppoe_softintr_handler(void *);
147 #else
148 struct callout pppoe_softintr = CALLOUT_INITIALIZER;
149 void pppoe_softintr_handler(void *);
150 #endif
151
152 extern int sppp_ioctl(struct ifnet *, unsigned long, void *);
153
154 /* input routines */
155 static void pppoe_input(void);
156 static void pppoe_disc_input(struct mbuf *);
157 static void pppoe_dispatch_disc_pkt(struct mbuf *, int);
158 static void pppoe_data_input(struct mbuf *);
159
160 /* management routines */
161 void pppoeattach(int);
162 static int pppoe_connect(struct pppoe_softc *);
163 static int pppoe_disconnect(struct pppoe_softc *);
164 static void pppoe_abort_connect(struct pppoe_softc *);
165 static int pppoe_ioctl(struct ifnet *, unsigned long, caddr_t);
166 static void pppoe_tls(struct sppp *);
167 static void pppoe_tlf(struct sppp *);
168 static void pppoe_start(struct ifnet *);
169
170 /* internal timeout handling */
171 static void pppoe_timeout(void *);
172
173 /* sending actual protocol controll packets */
174 static int pppoe_send_padi(struct pppoe_softc *);
175 static int pppoe_send_padr(struct pppoe_softc *);
176 static int pppoe_send_padt(struct pppoe_softc *);
177
178 /* raw output */
179 static int pppoe_output(struct pppoe_softc *, struct mbuf *);
180
181 /* internal helper functions */
182 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct ifnet *);
183 static struct pppoe_softc * pppoe_find_softc_by_hunique(u_int8_t *, size_t, struct ifnet *);
184
185 LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
186
187 int pppoe_clone_create __P((struct if_clone *, int));
188 void pppoe_clone_destroy __P((struct ifnet *));
189
190 struct if_clone pppoe_cloner =
191 IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy);
192
193 /* ARGSUSED */
194 void
195 pppoeattach(count)
196 int count;
197 {
198 LIST_INIT(&pppoe_softc_list);
199 if_clone_attach(&pppoe_cloner);
200
201 ppoediscinq.ifq_maxlen = IFQ_MAXLEN;
202 ppoeinq.ifq_maxlen = IFQ_MAXLEN;
203
204 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
205 pppoe_softintr = softintr_establish(IPL_SOFTNET, pppoe_softintr_handler, NULL);
206 #endif
207 }
208
209 int
210 pppoe_clone_create(ifc, unit)
211 struct if_clone *ifc;
212 int unit;
213 {
214 struct pppoe_softc *sc;
215
216 sc = malloc(sizeof(struct pppoe_softc), M_DEVBUF, M_WAITOK);
217 memset(sc, 0, sizeof(struct pppoe_softc));
218
219 sprintf(sc->sc_sppp.pp_if.if_xname, "pppoe%d", unit);
220 sc->sc_sppp.pp_if.if_softc = sc;
221 sc->sc_sppp.pp_if.if_mtu = PPPOE_MAXMTU;
222 sc->sc_sppp.pp_if.if_flags = IFF_SIMPLEX|IFF_POINTOPOINT|IFF_MULTICAST;
223 sc->sc_sppp.pp_if.if_type = IFT_PPP;
224 sc->sc_sppp.pp_if.if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN;
225 sc->sc_sppp.pp_if.if_dlt = DLT_PPP_ETHER;
226 sc->sc_sppp.pp_flags |= PP_KEEPALIVE | /* use LCP keepalive */
227 PP_NOFRAMING; /* no serial encapsulation */
228 sc->sc_sppp.pp_if.if_ioctl = pppoe_ioctl;
229 IFQ_SET_MAXLEN(&sc->sc_sppp.pp_if.if_snd, IFQ_MAXLEN);
230 IFQ_SET_READY(&sc->sc_sppp.pp_if.if_snd);
231
232 /* changed to real address later */
233 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
234
235 callout_init(&sc->sc_timeout);
236
237 sc->sc_sppp.pp_if.if_start = pppoe_start;
238 sc->sc_sppp.pp_tls = pppoe_tls;
239 sc->sc_sppp.pp_tlf = pppoe_tlf;
240 sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN; /* framing added to ppp packets */
241
242 if_attach(&sc->sc_sppp.pp_if);
243 sppp_attach(&sc->sc_sppp.pp_if);
244
245 #if NBPFILTER > 0
246 bpfattach(&sc->sc_sppp.pp_if, DLT_PPP_ETHER, 0);
247 #endif
248 LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
249 return 0;
250 }
251
252 void
253 pppoe_clone_destroy(ifp)
254 struct ifnet *ifp;
255 {
256 struct pppoe_softc * sc = ifp->if_softc;
257
258 LIST_REMOVE(sc, sc_list);
259 #if NBPFILTER > 0
260 bpfdetach(ifp);
261 #endif
262 sppp_detach(&sc->sc_sppp.pp_if);
263 if_detach(ifp);
264 if (sc->sc_concentrator_name)
265 free(sc->sc_concentrator_name, M_DEVBUF);
266 if (sc->sc_service_name)
267 free(sc->sc_service_name, M_DEVBUF);
268 if (sc->sc_ac_cookie)
269 free(sc->sc_ac_cookie, M_DEVBUF);
270 free(sc, M_DEVBUF);
271 }
272
273 /*
274 * Find the interface handling the specified session.
275 * Note: O(number of sessions open), this is a client-side only, mean
276 * and lean implementation, so number of open sessions typically should
277 * be 1.
278 */
279 static struct pppoe_softc *
280 pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif)
281 {
282 struct pppoe_softc *sc;
283
284 if (session == 0)
285 return NULL;
286
287 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
288 if (sc->sc_state == PPPOE_STATE_SESSION
289 && sc->sc_session == session) {
290 if (sc->sc_eth_if == rcvif)
291 return sc;
292 else
293 return NULL;
294 }
295 }
296 return NULL;
297 }
298
299 /* Check host unique token passed and return appropriate softc pointer,
300 * or NULL if token is bogus. */
301 static struct pppoe_softc *
302 pppoe_find_softc_by_hunique(u_int8_t *token, size_t len, struct ifnet *rcvif)
303 {
304 struct pppoe_softc *sc, *t;
305
306 if (LIST_EMPTY(&pppoe_softc_list))
307 return NULL;
308
309 if (len != sizeof sc)
310 return NULL;
311 memcpy(&t, token, len);
312
313 LIST_FOREACH(sc, &pppoe_softc_list, sc_list)
314 if (sc == t) break;
315
316 if (sc != t) {
317 #ifdef PPPOE_DEBUG
318 printf("pppoe: alien host unique tag, no session found\n");
319 #endif
320 return NULL;
321 }
322
323 /* should be safe to access *sc now */
324 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
325 printf("%s: host unique tag found, but it belongs to a connection in state %d\n",
326 sc->sc_sppp.pp_if.if_xname, sc->sc_state);
327 return NULL;
328 }
329 if (sc->sc_eth_if != rcvif) {
330 printf("%s: wrong interface, not accepting host unique\n",
331 sc->sc_sppp.pp_if.if_xname);
332 return NULL;
333 }
334 return sc;
335 }
336
337 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
338 static void pppoe_softintr_handler(void *dummy)
339 {
340 /* called at splsoftnet() */
341 pppoe_input();
342 }
343 #else
344 void pppoe_softintr_handler(void *dummy)
345 {
346 int s = splnet();
347 pppoe_input();
348 callout_deactivate(&pppoe_softintr);
349 splx(s);
350 }
351 #endif
352
353 /* called at appropriate protection level */
354 static void
355 pppoe_input()
356 {
357 struct mbuf *m;
358 int s, disc_done, data_done;
359
360 do {
361 disc_done = 0;
362 data_done = 0;
363 for (;;) {
364 s = splnet();
365 IF_DEQUEUE(&ppoediscinq, m);
366 splx(s);
367 if (m == NULL) break;
368 disc_done = 1;
369 pppoe_disc_input(m);
370 }
371
372 for (;;) {
373 s = splnet();
374 IF_DEQUEUE(&ppoeinq, m);
375 splx(s);
376 if (m == NULL) break;
377 data_done = 1;
378 pppoe_data_input(m);
379 }
380 } while (disc_done || data_done);
381 }
382
383 /* analyze and handle a single received packet while not in session state */
384 static void pppoe_dispatch_disc_pkt(struct mbuf *m, int off)
385 {
386 u_int16_t tag, len;
387 u_int16_t session, plen;
388 struct pppoe_softc *sc;
389 const char *err_msg = NULL;
390 u_int8_t *ac_cookie;
391 size_t ac_cookie_len;
392 struct pppoehdr *ph;
393 struct pppoetag *pt;
394 struct mbuf *n;
395 int noff;
396 struct ether_header *eh;
397
398 if (m->m_len < sizeof(*eh)) {
399 m = m_pullup(m, sizeof(*eh));
400 if (!m)
401 goto done;
402 }
403 eh = mtod(m, struct ether_header *);
404 off += sizeof(*eh);
405
406 ac_cookie = NULL;
407 ac_cookie_len = 0;
408 session = 0;
409 if (m->m_pkthdr.len - off <= PPPOE_HEADERLEN) {
410 printf("pppoe: packet too short: %d\n", m->m_pkthdr.len);
411 goto done;
412 }
413
414 n = m_pulldown(m, off, sizeof(*ph), &noff);
415 if (!n) {
416 printf("pppoe: could not get PPPoE header\n");
417 m = NULL;
418 goto done;
419 }
420 ph = (struct pppoehdr *)(mtod(n, caddr_t) + noff);
421 if (ph->vertype != PPPOE_VERTYPE) {
422 printf("pppoe: unknown version/type packet: 0x%x\n",
423 ph->vertype);
424 goto done;
425 }
426 session = ntohs(ph->session);
427 plen = ntohs(ph->plen);
428 off += sizeof(*ph);
429
430 if (plen + off > m->m_pkthdr.len) {
431 printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
432 m->m_pkthdr.len - off, plen);
433 goto done;
434 }
435 m_adj(m, off + plen - m->m_pkthdr.len); /* ignore trailing garbage */
436 tag = 0;
437 len = 0;
438 sc = NULL;
439 while (off + sizeof(*pt) <= m->m_pkthdr.len) {
440 n = m_pulldown(m, off, sizeof(*pt), &noff);
441 if (!n) {
442 printf("%s: parse error\n",
443 sc ? sc->sc_sppp.pp_if.if_xname : "pppoe");
444 m = NULL;
445 goto done;
446 }
447 pt = (struct pppoetag *)(mtod(n, caddr_t) + noff);
448 tag = ntohs(pt->tag);
449 len = ntohs(pt->len);
450 if (off + len > m->m_pkthdr.len) {
451 printf("pppoe: tag 0x%x len 0x%x is too long\n",
452 tag, len);
453 goto done;
454 }
455 switch (tag) {
456 case PPPOE_TAG_EOL:
457 goto breakbreak;
458 case PPPOE_TAG_SNAME:
459 break; /* ignored */
460 case PPPOE_TAG_ACNAME:
461 break; /* ignored */
462 case PPPOE_TAG_HUNIQUE:
463 if (sc != NULL)
464 break;
465 n = m_pulldown(m, off + sizeof(*pt), len, &noff);
466 if (!n) {
467 err_msg = "TAG HUNIQUE ERROR";
468 m = NULL;
469 goto done;
470 }
471 sc = pppoe_find_softc_by_hunique(mtod(n, caddr_t) + noff,
472 len, m->m_pkthdr.rcvif);
473 break;
474 case PPPOE_TAG_ACCOOKIE:
475 if (ac_cookie == NULL) {
476 n = m_pulldown(m, off + sizeof(*pt), len,
477 &noff);
478 if (!n) {
479 err_msg = "TAG ACCOOKIE ERROR";
480 m = NULL;
481 break;
482 }
483 ac_cookie = mtod(n, caddr_t) + noff;
484 ac_cookie_len = len;
485 }
486 break;
487 case PPPOE_TAG_SNAME_ERR:
488 err_msg = "SERVICE NAME ERROR";
489 break;
490 case PPPOE_TAG_ACSYS_ERR:
491 err_msg = "AC SYSTEM ERROR";
492 break;
493 case PPPOE_TAG_GENERIC_ERR:
494 err_msg = "GENERIC ERROR";
495 break;
496 }
497 if (err_msg) {
498 printf("%s: %s\n",
499 sc ? sc->sc_sppp.pp_if.if_xname : "pppoe",
500 err_msg);
501 goto done;
502 }
503 off += sizeof(*pt) + len;
504 }
505 breakbreak:;
506 switch (ph->code) {
507 case PPPOE_CODE_PADI:
508 case PPPOE_CODE_PADR:
509 /* ignore, we are no access concentrator */
510 goto done;
511 case PPPOE_CODE_PADO:
512 if (sc == NULL) {
513 /* be quiete if there is not a single pppoe instance */
514 if (!LIST_EMPTY(&pppoe_softc_list))
515 printf("pppoe: received PADO but could not find request for it\n");
516 goto done;
517 }
518 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
519 printf("%s: received unexpected PADO\n",
520 sc->sc_sppp.pp_if.if_xname);
521 goto done;
522 }
523 if (ac_cookie) {
524 if (sc->sc_ac_cookie)
525 free(sc->sc_ac_cookie, M_DEVBUF);
526 sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
527 M_DONTWAIT);
528 if (sc->sc_ac_cookie == NULL)
529 goto done;
530 sc->sc_ac_cookie_len = ac_cookie_len;
531 memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
532 }
533 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
534 callout_stop(&sc->sc_timeout);
535 sc->sc_padr_retried = 0;
536 sc->sc_state = PPPOE_STATE_PADR_SENT;
537 if (pppoe_send_padr(sc) == 0)
538 callout_reset(&sc->sc_timeout,
539 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
540 pppoe_timeout, sc);
541 else
542 pppoe_abort_connect(sc);
543 break;
544 case PPPOE_CODE_PADS:
545 if (sc == NULL)
546 goto done;
547 sc->sc_session = session;
548 callout_stop(&sc->sc_timeout);
549 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
550 printf("%s: session 0x%x connected\n",
551 sc->sc_sppp.pp_if.if_xname, session);
552 sc->sc_state = PPPOE_STATE_SESSION;
553 sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */
554 break;
555 case PPPOE_CODE_PADT:
556 if (sc == NULL)
557 goto done;
558 /* stop timer (we might be about to transmit a PADT ourself) */
559 callout_stop(&sc->sc_timeout);
560 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
561 printf("%s: session 0x%x terminated, received PADT\n",
562 sc->sc_sppp.pp_if.if_xname, session);
563 /* clean up softc */
564 sc->sc_state = PPPOE_STATE_INITIAL;
565 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
566 if (sc->sc_ac_cookie) {
567 free(sc->sc_ac_cookie, M_DEVBUF);
568 sc->sc_ac_cookie = NULL;
569 }
570 sc->sc_ac_cookie_len = 0;
571 sc->sc_session = 0;
572 /* signal upper layer */
573 sc->sc_sppp.pp_down(&sc->sc_sppp);
574 break;
575 default:
576 printf("%s: unknown code (0x%04x) session = 0x%04x\n",
577 sc? sc->sc_sppp.pp_if.if_xname : "pppoe",
578 ph->code, session);
579 break;
580 }
581
582 done:
583 m_freem(m);
584 return;
585 }
586
587 static void
588 pppoe_disc_input(struct mbuf *m)
589 {
590
591 /* avoid error messages if there is not a single pppoe instance */
592 if (!LIST_EMPTY(&pppoe_softc_list)) {
593 KASSERT(m->m_flags & M_PKTHDR);
594 pppoe_dispatch_disc_pkt(m, 0);
595 } else
596 m_freem(m);
597 }
598
599 static void
600 pppoe_data_input(struct mbuf *m)
601 {
602 u_int16_t session, plen;
603 struct pppoe_softc *sc;
604 struct pppoehdr *ph;
605
606 KASSERT(m->m_flags & M_PKTHDR);
607
608 m_adj(m, sizeof(struct ether_header));
609 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
610 printf("pppoe (data): dropping too short packet: %d bytes\n",
611 m->m_pkthdr.len);
612 goto drop;
613 }
614
615 if (m->m_len < sizeof(*ph)) {
616 m = m_pullup(m, sizeof(*ph));
617 if (!m) {
618 printf("pppoe: could not get PPPoE header\n");
619 return;
620 }
621 }
622 ph = mtod(m, struct pppoehdr *);
623
624 if (ph->vertype != PPPOE_VERTYPE) {
625 printf("pppoe (data): unknown version/type packet: 0x%x\n",
626 ph->vertype);
627 goto drop;
628 }
629 if (ph->code != 0)
630 goto drop;
631
632 session = ntohs(ph->session);
633 sc = pppoe_find_softc_by_session(session, m->m_pkthdr.rcvif);
634 if (sc == NULL)
635 goto drop;
636
637 plen = ntohs(ph->plen);
638
639 #if NBPFILTER > 0
640 if(sc->sc_sppp.pp_if.if_bpf)
641 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m);
642 #endif
643
644 m_adj(m, PPPOE_HEADERLEN);
645
646 #ifdef PPPOE_DEBUG
647 {
648 struct mbuf *p;
649
650 printf("%s: pkthdr.len=%d, pppoe.len=%d",
651 sc->sc_sppp.pp_if.if_xname,
652 m->m_pkthdr.len, plen);
653 p = m;
654 while (p) {
655 printf(" l=%d", p->m_len);
656 p = p->m_next;
657 }
658 printf("\n");
659 }
660 #endif
661
662 if (m->m_pkthdr.len < plen)
663 goto drop;
664
665 /* fix incoming interface pointer (not the raw ethernet interface anymore) */
666 m->m_pkthdr.rcvif = &sc->sc_sppp.pp_if;
667
668 /* pass packet up and account for it */
669 sc->sc_sppp.pp_if.if_ipackets++;
670 sppp_input(&sc->sc_sppp.pp_if, m);
671 return;
672
673 drop:
674 m_freem(m);
675 }
676
677 static int
678 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
679 {
680 struct sockaddr dst;
681 struct ether_header *eh;
682 u_int16_t etype;
683
684 if (sc->sc_eth_if == NULL)
685 return EIO;
686
687 memset(&dst, 0, sizeof dst);
688 dst.sa_family = AF_UNSPEC;
689 eh = (struct ether_header*)&dst.sa_data;
690 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
691 eh->ether_type = htons(etype);
692 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
693
694 #ifdef PPPOE_DEBUG
695 printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
696 sc->sc_sppp.pp_if.if_xname, etype,
697 sc->sc_state, sc->sc_session,
698 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
699 #endif
700
701 m->m_flags &= ~(M_BCAST|M_MCAST);
702 sc->sc_sppp.pp_if.if_opackets++;
703 return sc->sc_eth_if->if_output(sc->sc_eth_if, m, &dst, NULL);
704 }
705
706 static int
707 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
708 {
709 struct proc *p = curproc; /* XXX */
710 struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
711 int error = 0;
712
713 switch (cmd) {
714 case PPPOESETPARMS:
715 {
716 struct pppoediscparms *parms = (struct pppoediscparms*)data;
717 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
718 return error;
719 if (parms->eth_ifname[0] != 0) {
720 sc->sc_eth_if = ifunit(parms->eth_ifname);
721 if (sc->sc_eth_if == NULL)
722 return ENXIO;
723 }
724 if (parms->ac_name) {
725 size_t s;
726 char * p = malloc(parms->ac_name_len + 1, M_DEVBUF, M_WAITOK);
727 copyinstr(parms->ac_name, p, parms->ac_name_len, &s);
728 if (sc->sc_concentrator_name)
729 free(sc->sc_concentrator_name, M_DEVBUF);
730 sc->sc_concentrator_name = p;
731 }
732 if (parms->service_name) {
733 size_t s;
734 char * p = malloc(parms->service_name_len + 1, M_DEVBUF, M_WAITOK);
735 copyinstr(parms->service_name, p, parms->service_name_len, &s);
736 if (sc->sc_service_name)
737 free(sc->sc_service_name, M_DEVBUF);
738 sc->sc_service_name = p;
739 }
740 return 0;
741 }
742 break;
743 case PPPOEGETPARMS:
744 {
745 struct pppoediscparms *parms = (struct pppoediscparms*)data;
746 memset(parms, 0, sizeof *parms);
747 if (sc->sc_eth_if)
748 strncpy(parms->ifname, sc->sc_eth_if->if_xname, IFNAMSIZ);
749 return 0;
750 }
751 break;
752 case PPPOEGETSESSION:
753 {
754 struct pppoeconnectionstate *state = (struct pppoeconnectionstate*)data;
755 state->state = sc->sc_state;
756 state->session_id = sc->sc_session;
757 state->padi_retry_no = sc->sc_padi_retried;
758 state->padr_retry_no = sc->sc_padr_retried;
759 return 0;
760 }
761 break;
762 case SIOCSIFFLAGS:
763 {
764 struct ifreq *ifr = (struct ifreq*) data;
765 /*
766 * Prevent running re-establishment timers overriding
767 * administrators choice.
768 */
769 if ((ifr->ifr_flags & IFF_UP) == 0
770 && sc->sc_state >= PPPOE_STATE_PADI_SENT
771 && sc->sc_state < PPPOE_STATE_SESSION) {
772 callout_stop(&sc->sc_timeout);
773 sc->sc_state = PPPOE_STATE_INITIAL;
774 sc->sc_padi_retried = 0;
775 sc->sc_padr_retried = 0;
776 memcpy(&sc->sc_dest, etherbroadcastaddr,
777 sizeof(sc->sc_dest));
778 }
779 return sppp_ioctl(ifp, cmd, data);
780 }
781 case SIOCSIFMTU:
782 {
783 struct ifreq *ifr = (struct ifreq*) data;
784
785 if (ifr->ifr_mtu > PPPOE_MAXMTU)
786 return EINVAL;
787 return sppp_ioctl(ifp, cmd, data);
788 }
789 default:
790 return sppp_ioctl(ifp, cmd, data);
791 }
792 return 0;
793 }
794
795 /*
796 * Allocate a mbuf/cluster with space to store the given data length
797 * of payload, leaving space for prepending an ethernet header
798 * in front.
799 */
800 static struct mbuf *
801 pppoe_get_mbuf(size_t len)
802 {
803 struct mbuf *m;
804
805 MGETHDR(m, M_DONTWAIT, MT_DATA);
806 if (m == NULL)
807 return NULL;
808 if (len + sizeof(struct ether_header) > MHLEN) {
809 MCLGET(m, M_DONTWAIT);
810 if ((m->m_flags & M_EXT) == 0) {
811 struct mbuf *n;
812 MFREE(m, n);
813 return 0;
814 }
815 }
816 m->m_data += sizeof(struct ether_header);
817 m->m_len = len;
818 m->m_pkthdr.len = len;
819 m->m_pkthdr.rcvif = NULL;
820
821 return m;
822 }
823
824 static int
825 pppoe_send_padi(struct pppoe_softc *sc)
826 {
827 struct mbuf *m0;
828 int len, l1, l2;
829 u_int8_t *p;
830
831 if (sc->sc_state >PPPOE_STATE_PADI_SENT)
832 panic("pppoe_send_padi in state %d", sc->sc_state);
833
834 /* calculate length of frame (excluding ethernet header + pppoe header) */
835 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
836 if (sc->sc_service_name != NULL) {
837 l1 = strlen(sc->sc_service_name);
838 len += l1;
839 }
840 if (sc->sc_concentrator_name != NULL) {
841 l2 = strlen(sc->sc_concentrator_name);
842 len += 2 + 2 + l2;
843 }
844
845 /* allocate a buffer */
846 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); /* header len + payload len */
847 if (!m0)
848 return ENOBUFS;
849
850 /* fill in pkt */
851 p = mtod(m0, u_int8_t *);
852 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
853 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
854 if (sc->sc_service_name != NULL) {
855 PPPOE_ADD_16(p, l1);
856 memcpy(p, sc->sc_service_name, l1);
857 p += l1;
858 } else {
859 PPPOE_ADD_16(p, 0);
860 }
861 if (sc->sc_concentrator_name != NULL) {
862 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
863 PPPOE_ADD_16(p, l2);
864 memcpy(p, sc->sc_concentrator_name, l2);
865 p += l2;
866 }
867 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
868 PPPOE_ADD_16(p, sizeof(sc));
869 memcpy(p, &sc, sizeof sc);
870
871 #ifdef PPPOE_DEBUG
872 p += sizeof sc;
873 if (p - mtod(m0, u_int8_t *) != len + PPPOE_HEADERLEN)
874 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
875 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t *)));
876 #endif
877
878 /* send pkt */
879 return pppoe_output(sc, m0);
880 }
881
882 static void
883 pppoe_timeout(void *arg)
884 {
885 int x, retry_wait;
886 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
887
888 #ifdef PPPOE_DEBUG
889 printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
890 #endif
891
892 switch (sc->sc_state) {
893 case PPPOE_STATE_PADI_SENT:
894 /*
895 * We have two basic ways of retrying:
896 * - Quick retry mode: try a few times in short sequence
897 * - Slow retry mode: we already had a connection successfully
898 * established and will try infinitely (without user
899 * intervention)
900 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
901 * is not set and we already had a successfull connection.
902 */
903
904 /* initialize for quick retry mode */
905 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
906
907 x = splnet();
908 sc->sc_padi_retried++;
909 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
910 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0 &&
911 sc->sc_sppp.pp_if.if_ibytes) {
912 /* slow retry mode */
913 retry_wait = PPPOE_SLOW_RETRY;
914 } else {
915 pppoe_abort_connect(sc);
916 splx(x);
917 return;
918 }
919 }
920 if (pppoe_send_padi(sc) == 0)
921 callout_reset(&sc->sc_timeout, retry_wait,
922 pppoe_timeout, sc);
923 else
924 pppoe_abort_connect(sc);
925 splx(x);
926 break;
927
928 case PPPOE_STATE_PADR_SENT:
929 x = splnet();
930 sc->sc_padr_retried++;
931 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
932 memcpy(&sc->sc_dest, etherbroadcastaddr,
933 sizeof(sc->sc_dest));
934 sc->sc_state = PPPOE_STATE_PADI_SENT;
935 sc->sc_padr_retried = 0;
936 if (pppoe_send_padi(sc) == 0)
937 callout_reset(&sc->sc_timeout,
938 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried),
939 pppoe_timeout, sc);
940 else
941 pppoe_abort_connect(sc);
942 splx(x);
943 return;
944 }
945 if (pppoe_send_padr(sc) == 0)
946 callout_reset(&sc->sc_timeout,
947 PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried),
948 pppoe_timeout, sc);
949 else
950 pppoe_abort_connect(sc);
951 splx(x);
952 break;
953 case PPPOE_STATE_CLOSING:
954 pppoe_disconnect(sc);
955 break;
956 default:
957 return; /* all done, work in peace */
958 }
959 }
960
961 /* Start a connection (i.e. initiate discovery phase) */
962 static int
963 pppoe_connect(struct pppoe_softc *sc)
964 {
965 int x, err;
966
967 if (sc->sc_state != PPPOE_STATE_INITIAL)
968 return EBUSY;
969
970 x = splnet();
971 sc->sc_state = PPPOE_STATE_PADI_SENT;
972 sc->sc_padr_retried = 0;
973 err = pppoe_send_padi(sc);
974 if (err == 0)
975 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT,
976 pppoe_timeout, sc);
977 else
978 pppoe_abort_connect(sc);
979 splx(x);
980 return err;
981 }
982
983 /* disconnect */
984 static int
985 pppoe_disconnect(struct pppoe_softc *sc)
986 {
987 int err, x;
988
989 x = splnet();
990
991 if (sc->sc_state < PPPOE_STATE_SESSION)
992 err = EBUSY;
993 else {
994 if (sc->sc_sppp.pp_if.if_flags & IFF_DEBUG)
995 printf("%s: disconnecting\n",
996 sc->sc_sppp.pp_if.if_xname);
997 err = pppoe_send_padt(sc);
998 }
999
1000 /* cleanup softc */
1001 sc->sc_state = PPPOE_STATE_INITIAL;
1002 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1003 if (sc->sc_ac_cookie) {
1004 free(sc->sc_ac_cookie, M_DEVBUF);
1005 sc->sc_ac_cookie = NULL;
1006 }
1007 sc->sc_ac_cookie_len = 0;
1008 sc->sc_session = 0;
1009
1010 /* notify upper layer */
1011 sc->sc_sppp.pp_down(&sc->sc_sppp);
1012
1013 splx(x);
1014
1015 return err;
1016 }
1017
1018 /* Connection attempt aborted */
1019 static void
1020 pppoe_abort_connect(struct pppoe_softc *sc)
1021 {
1022 printf("%s: could not establish connection\n",
1023 sc->sc_sppp.pp_if.if_xname);
1024 sc->sc_state = PPPOE_STATE_INITIAL;
1025 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
1026
1027 /* notify upper layer */
1028 sc->sc_sppp.pp_down(&sc->sc_sppp);
1029 }
1030
1031 /* Send a PADR packet */
1032 static int
1033 pppoe_send_padr(struct pppoe_softc *sc)
1034 {
1035 struct mbuf *m0;
1036 u_int8_t *p;
1037 size_t len, l1;
1038
1039 if (sc->sc_state != PPPOE_STATE_PADR_SENT)
1040 return EIO;
1041
1042 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
1043 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1044 l1 = strlen(sc->sc_service_name);
1045 len += l1;
1046 }
1047 if (sc->sc_ac_cookie_len > 0)
1048 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
1049 m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
1050 if (!m0)
1051 return ENOBUFS;
1052 p = mtod(m0, u_int8_t *);
1053 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1054 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1055 if (sc->sc_service_name != NULL) {
1056 PPPOE_ADD_16(p, l1);
1057 memcpy(p, sc->sc_service_name, l1);
1058 p += l1;
1059 } else {
1060 PPPOE_ADD_16(p, 0);
1061 }
1062 if (sc->sc_ac_cookie_len > 0) {
1063 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1064 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1065 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1066 p += sc->sc_ac_cookie_len;
1067 }
1068 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1069 PPPOE_ADD_16(p, sizeof(sc));
1070 memcpy(p, &sc, sizeof sc);
1071
1072 #ifdef PPPOE_DEBUG
1073 p += sizeof sc;
1074 if (p - mtod(m0, u_int8_t *) != len + PPPOE_HEADERLEN)
1075 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
1076 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t *)));
1077 #endif
1078
1079 return pppoe_output(sc, m0);
1080 }
1081
1082 /* send a PADT packet */
1083 static int
1084 pppoe_send_padt(struct pppoe_softc *sc)
1085 {
1086 struct mbuf *m0;
1087 u_int8_t *p;
1088
1089 if (sc->sc_state < PPPOE_STATE_SESSION)
1090 return EIO;
1091
1092 #ifdef PPPOE_DEBUG
1093 printf("%s: sending PADT\n", sc->sc_sppp.pp_if.if_xname);
1094 #endif
1095 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
1096 if (!m0)
1097 return ENOBUFS;
1098 p = mtod(m0, u_int8_t *);
1099 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, sc->sc_session, 0);
1100 return pppoe_output(sc, m0);
1101 }
1102
1103 static void
1104 pppoe_tls(struct sppp *sp)
1105 {
1106 struct pppoe_softc *sc = (void *)sp;
1107 if (sc->sc_state != PPPOE_STATE_INITIAL)
1108 return;
1109 pppoe_connect(sc);
1110 }
1111
1112 static void
1113 pppoe_tlf(struct sppp *sp)
1114 {
1115 struct pppoe_softc *sc = (void *)sp;
1116 if (sc->sc_state < PPPOE_STATE_SESSION)
1117 return;
1118 /*
1119 * Do not call pppoe_disconnect here, the upper layer state
1120 * machine gets confused by this. We must return from this
1121 * function and defer disconnecting to the timeout handler.
1122 */
1123 sc->sc_state = PPPOE_STATE_CLOSING;
1124 callout_reset(&sc->sc_timeout, hz/50, pppoe_timeout, sc);
1125 }
1126
1127 static void
1128 pppoe_start(struct ifnet *ifp)
1129 {
1130 struct pppoe_softc *sc = (void *)ifp;
1131 struct mbuf *m;
1132 u_int8_t *p;
1133 size_t len;
1134
1135 if (sppp_isempty(ifp))
1136 return;
1137
1138 /* are we ready to proccess data yet? */
1139 if (sc->sc_state < PPPOE_STATE_SESSION) {
1140 sppp_flush(&sc->sc_sppp.pp_if);
1141 return;
1142 }
1143
1144 while ((m = sppp_dequeue(ifp)) != NULL) {
1145 len = m->m_pkthdr.len;
1146 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
1147 if (m == NULL) {
1148 m_free(m);
1149 break;
1150 }
1151 p = mtod(m, u_int8_t *);
1152 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1153
1154 #if NBPFILTER > 0
1155 if(sc->sc_sppp.pp_if.if_bpf)
1156 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m);
1157 #endif
1158
1159 pppoe_output(sc, m);
1160 }
1161 }
1162