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