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