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