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