VM: simplify slab allocator
[minix.git] / lib / liblwip / netif / ppp / ppp_oe.c
blob040a0bc93c92c7f16c9e4b92c81b4baa8a75d635
1 /*****************************************************************************
2 * ppp_oe.c - PPP Over Ethernet implementation for lwIP.
4 * Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice and the following disclaimer are included verbatim in any
10 * distributions. No written agreement, license, or royalty fee is required
11 * for any of the authorized uses.
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 ******************************************************************************
25 * REVISION HISTORY
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>
28 * Ported to lwIP.
29 *****************************************************************************/
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */
35 /*-
36 * Copyright (c) 2002 The NetBSD Foundation, Inc.
37 * All rights reserved.
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Martin Husemann <martin@NetBSD.org>.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
71 #include "lwip/opt.h"
73 #if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
75 #include "netif/ppp_oe.h"
77 #include "ppp.h"
78 #include "pppdebug.h"
80 #include "lwip/timers.h"
81 #include "lwip/memp.h"
83 #include <string.h>
84 #include <stdio.h>
87 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
88 #define PPPOE_ADD_16(PTR, VAL) \
89 *(PTR)++ = (u8_t)((VAL) / 256); \
90 *(PTR)++ = (u8_t)((VAL) % 256)
92 /* Add a complete PPPoE header to the buffer pointed to by PTR */
93 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
94 *(PTR)++ = PPPOE_VERTYPE; \
95 *(PTR)++ = (CODE); \
96 PPPOE_ADD_16(PTR, SESS); \
97 PPPOE_ADD_16(PTR, LEN)
99 #define PPPOE_DISC_TIMEOUT (5*1000) /* base for quick timeout calculation */
100 #define PPPOE_SLOW_RETRY (60*1000) /* persistent retry interval */
101 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
102 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
104 #ifdef PPPOE_SERVER
105 #error "PPPOE_SERVER is not yet supported under lwIP!"
106 /* from if_spppsubr.c */
107 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
108 #endif
110 #ifndef PPPOE_ERRORSTRING_LEN
111 #define PPPOE_ERRORSTRING_LEN 64
112 #endif
113 static char pppoe_error_tmp[PPPOE_ERRORSTRING_LEN];
116 /* input routines */
117 static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);
119 /* management routines */
120 static int pppoe_do_disconnect(struct pppoe_softc *);
121 static void pppoe_abort_connect(struct pppoe_softc *);
122 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
124 /* internal timeout handling */
125 static void pppoe_timeout(void *);
127 /* sending actual protocol controll packets */
128 static err_t pppoe_send_padi(struct pppoe_softc *);
129 static err_t pppoe_send_padr(struct pppoe_softc *);
130 #ifdef PPPOE_SERVER
131 static err_t pppoe_send_pado(struct pppoe_softc *);
132 static err_t pppoe_send_pads(struct pppoe_softc *);
133 #endif
134 static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);
136 /* internal helper functions */
137 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);
138 static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);
140 /** linked list of created pppoe interfaces */
141 static struct pppoe_softc *pppoe_softc_list;
143 err_t
144 pppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr)
146 struct pppoe_softc *sc;
148 sc = (struct pppoe_softc *)memp_malloc(MEMP_PPPOE_IF);
149 if (sc == NULL) {
150 *scptr = NULL;
151 return ERR_MEM;
153 memset(sc, 0, sizeof(struct pppoe_softc));
155 /* changed to real address later */
156 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
158 sc->sc_pd = pd;
159 sc->sc_linkStatusCB = linkStatusCB;
160 sc->sc_ethif = ethif;
162 /* put the new interface at the head of the list */
163 sc->next = pppoe_softc_list;
164 pppoe_softc_list = sc;
166 *scptr = sc;
168 return ERR_OK;
171 err_t
172 pppoe_destroy(struct netif *ifp)
174 struct pppoe_softc *sc, *prev = NULL;
176 for (sc = pppoe_softc_list; sc != NULL; prev = sc, sc = sc->next) {
177 if (sc->sc_ethif == ifp) {
178 break;
182 if(!(sc && (sc->sc_ethif == ifp))) {
183 return ERR_IF;
186 sys_untimeout(pppoe_timeout, sc);
187 if (prev == NULL) {
188 /* remove sc from the head of the list */
189 pppoe_softc_list = sc->next;
190 } else {
191 /* remove sc from the list */
192 prev->next = sc->next;
195 #ifdef PPPOE_TODO
196 if (sc->sc_concentrator_name) {
197 mem_free(sc->sc_concentrator_name);
199 if (sc->sc_service_name) {
200 mem_free(sc->sc_service_name);
202 #endif /* PPPOE_TODO */
203 memp_free(MEMP_PPPOE_IF, sc);
205 return ERR_OK;
209 * Find the interface handling the specified session.
210 * Note: O(number of sessions open), this is a client-side only, mean
211 * and lean implementation, so number of open sessions typically should
212 * be 1.
214 static struct pppoe_softc *
215 pppoe_find_softc_by_session(u_int session, struct netif *rcvif)
217 struct pppoe_softc *sc;
219 if (session == 0) {
220 return NULL;
223 for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
224 if (sc->sc_state == PPPOE_STATE_SESSION
225 && sc->sc_session == session) {
226 if (sc->sc_ethif == rcvif) {
227 return sc;
228 } else {
229 return NULL;
233 return NULL;
236 /* Check host unique token passed and return appropriate softc pointer,
237 * or NULL if token is bogus. */
238 static struct pppoe_softc *
239 pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif)
241 struct pppoe_softc *sc, *t;
243 if (pppoe_softc_list == NULL) {
244 return NULL;
247 if (len != sizeof sc) {
248 return NULL;
250 MEMCPY(&t, token, len);
252 for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
253 if (sc == t) {
254 break;
258 if (sc == NULL) {
259 PPPDEBUG(LOG_DEBUG, ("pppoe: alien host unique tag, no session found\n"));
260 return NULL;
263 /* should be safe to access *sc now */
264 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
265 printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
266 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);
267 return NULL;
269 if (sc->sc_ethif != rcvif) {
270 printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",
271 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
272 return NULL;
274 return sc;
277 static void
278 pppoe_linkstatus_up(struct pppoe_softc *sc)
280 sc->sc_linkStatusCB(sc->sc_pd, 1);
283 /* analyze and handle a single received packet while not in session state */
284 static void
285 pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
287 u16_t tag, len;
288 u16_t session, plen;
289 struct pppoe_softc *sc;
290 const char *err_msg;
291 char devname[6];
292 u8_t *ac_cookie;
293 u16_t ac_cookie_len;
294 #ifdef PPPOE_SERVER
295 u8_t *hunique;
296 size_t hunique_len;
297 #endif
298 struct pppoehdr *ph;
299 struct pppoetag pt;
300 int off, err, errortag;
301 struct eth_hdr *ethhdr;
303 pb = pppSingleBuf(pb);
305 strcpy(devname, "pppoe"); /* as long as we don't know which instance */
306 err_msg = NULL;
307 errortag = 0;
308 if (pb->len < sizeof(*ethhdr)) {
309 goto done;
311 ethhdr = (struct eth_hdr *)pb->payload;
312 off = sizeof(*ethhdr);
314 ac_cookie = NULL;
315 ac_cookie_len = 0;
316 #ifdef PPPOE_SERVER
317 hunique = NULL;
318 hunique_len = 0;
319 #endif
320 session = 0;
321 if (pb->len - off < PPPOE_HEADERLEN) {
322 printf("pppoe: packet too short: %d\n", pb->len);
323 goto done;
326 ph = (struct pppoehdr *) (ethhdr + 1);
327 if (ph->vertype != PPPOE_VERTYPE) {
328 printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);
329 goto done;
331 session = ntohs(ph->session);
332 plen = ntohs(ph->plen);
333 off += sizeof(*ph);
335 if (plen + off > pb->len) {
336 printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
337 pb->len - off, plen);
338 goto done;
340 if(pb->tot_len == pb->len) {
341 pb->tot_len = pb->len = (u16_t)off + plen; /* ignore trailing garbage */
343 tag = 0;
344 len = 0;
345 sc = NULL;
346 while (off + sizeof(pt) <= pb->len) {
347 MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));
348 tag = ntohs(pt.tag);
349 len = ntohs(pt.len);
350 if (off + sizeof(pt) + len > pb->len) {
351 printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);
352 goto done;
354 switch (tag) {
355 case PPPOE_TAG_EOL:
356 goto breakbreak;
357 case PPPOE_TAG_SNAME:
358 break; /* ignored */
359 case PPPOE_TAG_ACNAME:
360 break; /* ignored */
361 case PPPOE_TAG_HUNIQUE:
362 if (sc != NULL) {
363 break;
365 #ifdef PPPOE_SERVER
366 hunique = (u8_t*)pb->payload + off + sizeof(pt);
367 hunique_len = len;
368 #endif
369 sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);
370 if (sc != NULL) {
371 snprintf(devname, sizeof(devname), "%c%c%"U16_F, sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
373 break;
374 case PPPOE_TAG_ACCOOKIE:
375 if (ac_cookie == NULL) {
376 ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);
377 ac_cookie_len = len;
379 break;
380 case PPPOE_TAG_SNAME_ERR:
381 err_msg = "SERVICE NAME ERROR";
382 errortag = 1;
383 break;
384 case PPPOE_TAG_ACSYS_ERR:
385 err_msg = "AC SYSTEM ERROR";
386 errortag = 1;
387 break;
388 case PPPOE_TAG_GENERIC_ERR:
389 err_msg = "GENERIC ERROR";
390 errortag = 1;
391 break;
393 if (err_msg) {
394 if (errortag && len) {
395 u16_t error_len = LWIP_MIN(len, sizeof(pppoe_error_tmp)-1);
396 strncpy(pppoe_error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
397 pppoe_error_tmp[error_len-1] = '\0';
398 printf("%s: %s: %s\n", devname, err_msg, pppoe_error_tmp);
399 } else {
400 printf("%s: %s\n", devname, err_msg);
402 if (errortag) {
403 goto done;
406 off += sizeof(pt) + len;
409 breakbreak:;
410 switch (ph->code) {
411 case PPPOE_CODE_PADI:
412 #ifdef PPPOE_SERVER
414 * got service name, concentrator name, and/or host unique.
415 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
417 if (LIST_EMPTY(&pppoe_softc_list)) {
418 goto done;
420 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
421 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
422 continue;
424 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
425 continue;
427 if (sc->sc_state == PPPOE_STATE_INITIAL) {
428 break;
431 if (sc == NULL) {
432 /* printf("pppoe: free passive interface is not found\n"); */
433 goto done;
435 if (hunique) {
436 if (sc->sc_hunique) {
437 mem_free(sc->sc_hunique);
439 sc->sc_hunique = mem_malloc(hunique_len);
440 if (sc->sc_hunique == NULL) {
441 goto done;
443 sc->sc_hunique_len = hunique_len;
444 MEMCPY(sc->sc_hunique, hunique, hunique_len);
446 MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
447 sc->sc_state = PPPOE_STATE_PADO_SENT;
448 pppoe_send_pado(sc);
449 break;
450 #endif /* PPPOE_SERVER */
451 case PPPOE_CODE_PADR:
452 #ifdef PPPOE_SERVER
454 * get sc from ac_cookie if IFF_PASSIVE
456 if (ac_cookie == NULL) {
457 /* be quiet if there is not a single pppoe instance */
458 printf("pppoe: received PADR but not includes ac_cookie\n");
459 goto done;
461 sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
462 if (sc == NULL) {
463 /* be quiet if there is not a single pppoe instance */
464 if (!LIST_EMPTY(&pppoe_softc_list)) {
465 printf("pppoe: received PADR but could not find request for it\n");
467 goto done;
469 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
470 printf("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
471 goto done;
473 if (hunique) {
474 if (sc->sc_hunique) {
475 mem_free(sc->sc_hunique);
477 sc->sc_hunique = mem_malloc(hunique_len);
478 if (sc->sc_hunique == NULL) {
479 goto done;
481 sc->sc_hunique_len = hunique_len;
482 MEMCPY(sc->sc_hunique, hunique, hunique_len);
484 pppoe_send_pads(sc);
485 sc->sc_state = PPPOE_STATE_SESSION;
486 pppoe_linkstatus_up(sc); /* notify upper layers */
487 break;
488 #else
489 /* ignore, we are no access concentrator */
490 goto done;
491 #endif /* PPPOE_SERVER */
492 case PPPOE_CODE_PADO:
493 if (sc == NULL) {
494 /* be quiet if there is not a single pppoe instance */
495 if (pppoe_softc_list != NULL) {
496 printf("pppoe: received PADO but could not find request for it\n");
498 goto done;
500 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
501 printf("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
502 goto done;
504 if (ac_cookie) {
505 sc->sc_ac_cookie_len = ac_cookie_len;
506 MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
508 MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));
509 sys_untimeout(pppoe_timeout, sc);
510 sc->sc_padr_retried = 0;
511 sc->sc_state = PPPOE_STATE_PADR_SENT;
512 if ((err = pppoe_send_padr(sc)) != 0) {
513 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
515 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
516 break;
517 case PPPOE_CODE_PADS:
518 if (sc == NULL) {
519 goto done;
521 sc->sc_session = session;
522 sys_untimeout(pppoe_timeout, sc);
523 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": session 0x%x connected\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, session));
524 sc->sc_state = PPPOE_STATE_SESSION;
525 pppoe_linkstatus_up(sc); /* notify upper layers */
526 break;
527 case PPPOE_CODE_PADT:
528 if (sc == NULL) {
529 goto done;
531 pppoe_clear_softc(sc, "received PADT");
532 break;
533 default:
534 if(sc) {
535 printf("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
536 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
537 (u16_t)ph->code, session);
538 } else {
539 printf("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session);
541 break;
544 done:
545 pbuf_free(pb);
546 return;
549 void
550 pppoe_disc_input(struct netif *netif, struct pbuf *p)
552 /* avoid error messages if there is not a single pppoe instance */
553 if (pppoe_softc_list != NULL) {
554 pppoe_dispatch_disc_pkt(netif, p);
555 } else {
556 pbuf_free(p);
560 void
561 pppoe_data_input(struct netif *netif, struct pbuf *pb)
563 u16_t session, plen;
564 struct pppoe_softc *sc;
565 struct pppoehdr *ph;
566 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
567 u8_t shost[ETHER_ADDR_LEN];
568 #endif
570 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
571 MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));
572 #endif
573 if (pbuf_header(pb, -(int)sizeof(struct eth_hdr)) != 0) {
574 /* bail out */
575 PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header failed\n"));
576 LINK_STATS_INC(link.lenerr);
577 goto drop;
580 pb = pppSingleBuf (pb);
582 if (pb->len <= PPPOE_HEADERLEN) {
583 printf("pppoe (data): dropping too short packet: %d bytes\n", pb->len);
584 goto drop;
587 if (pb->len < sizeof(*ph)) {
588 printf("pppoe_data_input: could not get PPPoE header\n");
589 goto drop;
591 ph = (struct pppoehdr *)pb->payload;
593 if (ph->vertype != PPPOE_VERTYPE) {
594 printf("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype);
595 goto drop;
597 if (ph->code != 0) {
598 goto drop;
601 session = ntohs(ph->session);
602 sc = pppoe_find_softc_by_session(session, netif);
603 if (sc == NULL) {
604 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
605 printf("pppoe: input for unknown session 0x%x, sending PADT\n", session);
606 pppoe_send_padt(netif, session, shost);
607 #endif
608 goto drop;
611 plen = ntohs(ph->plen);
613 if (pbuf_header(pb, -(int)(PPPOE_HEADERLEN)) != 0) {
614 /* bail out */
615 PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));
616 LINK_STATS_INC(link.lenerr);
617 goto drop;
620 PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",
621 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
622 pb->len, plen));
624 if (pb->len < plen) {
625 goto drop;
628 pppInProcOverEthernet(sc->sc_pd, pb);
630 return;
632 drop:
633 pbuf_free(pb);
636 static err_t
637 pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
639 struct eth_hdr *ethhdr;
640 u16_t etype;
641 err_t res;
643 if (!sc->sc_ethif) {
644 pbuf_free(pb);
645 return ERR_IF;
648 ethhdr = (struct eth_hdr *)pb->payload;
649 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
650 ethhdr->type = htons(etype);
651 MEMCPY(ethhdr->dest.addr, sc->sc_dest.addr, sizeof(ethhdr->dest.addr));
652 MEMCPY(ethhdr->src.addr, ((struct eth_addr *)sc->sc_ethif->hwaddr)->addr, sizeof(ethhdr->src.addr));
654 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F" (%x) state=%d, session=0x%x output -> %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F", len=%d\n",
655 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,
656 sc->sc_state, sc->sc_session,
657 sc->sc_dest.addr[0], sc->sc_dest.addr[1], sc->sc_dest.addr[2], sc->sc_dest.addr[3], sc->sc_dest.addr[4], sc->sc_dest.addr[5],
658 pb->tot_len));
660 res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);
662 pbuf_free(pb);
664 return res;
667 static err_t
668 pppoe_send_padi(struct pppoe_softc *sc)
670 struct pbuf *pb;
671 u8_t *p;
672 int len;
673 #ifdef PPPOE_TODO
674 int l1 = 0, l2 = 0; /* XXX: gcc */
675 #endif /* PPPOE_TODO */
677 if (sc->sc_state >PPPOE_STATE_PADI_SENT) {
678 PPPDEBUG(LOG_ERR, ("ERROR: pppoe_send_padi in state %d", sc->sc_state));
681 /* calculate length of frame (excluding ethernet header + pppoe header) */
682 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
683 #ifdef PPPOE_TODO
684 if (sc->sc_service_name != NULL) {
685 l1 = (int)strlen(sc->sc_service_name);
686 len += l1;
688 if (sc->sc_concentrator_name != NULL) {
689 l2 = (int)strlen(sc->sc_concentrator_name);
690 len += 2 + 2 + l2;
692 #endif /* PPPOE_TODO */
693 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
694 sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
696 /* allocate a buffer */
697 pb = pbuf_alloc(PBUF_LINK, (u16_t)(sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len), PBUF_RAM);
698 if (!pb) {
699 return ERR_MEM;
701 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
703 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
704 /* fill in pkt */
705 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
706 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
707 #ifdef PPPOE_TODO
708 if (sc->sc_service_name != NULL) {
709 PPPOE_ADD_16(p, l1);
710 MEMCPY(p, sc->sc_service_name, l1);
711 p += l1;
712 } else
713 #endif /* PPPOE_TODO */
715 PPPOE_ADD_16(p, 0);
717 #ifdef PPPOE_TODO
718 if (sc->sc_concentrator_name != NULL) {
719 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
720 PPPOE_ADD_16(p, l2);
721 MEMCPY(p, sc->sc_concentrator_name, l2);
722 p += l2;
724 #endif /* PPPOE_TODO */
725 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
726 PPPOE_ADD_16(p, sizeof(sc));
727 MEMCPY(p, &sc, sizeof sc);
729 /* send pkt */
730 return pppoe_output(sc, pb);
733 static void
734 pppoe_timeout(void *arg)
736 int retry_wait, err;
737 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
739 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
741 switch (sc->sc_state) {
742 case PPPOE_STATE_PADI_SENT:
744 * We have two basic ways of retrying:
745 * - Quick retry mode: try a few times in short sequence
746 * - Slow retry mode: we already had a connection successfully
747 * established and will try infinitely (without user
748 * intervention)
749 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
750 * is not set.
753 /* initialize for quick retry mode */
754 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
756 sc->sc_padi_retried++;
757 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
758 #if 0
759 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
760 /* slow retry mode */
761 retry_wait = PPPOE_SLOW_RETRY;
762 } else
763 #endif
765 pppoe_abort_connect(sc);
766 return;
769 if ((err = pppoe_send_padi(sc)) != 0) {
770 sc->sc_padi_retried--;
771 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to transmit PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
773 sys_timeout(retry_wait, pppoe_timeout, sc);
774 break;
776 case PPPOE_STATE_PADR_SENT:
777 sc->sc_padr_retried++;
778 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
779 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
780 sc->sc_state = PPPOE_STATE_PADI_SENT;
781 sc->sc_padr_retried = 0;
782 if ((err = pppoe_send_padi(sc)) != 0) {
783 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
785 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);
786 return;
788 if ((err = pppoe_send_padr(sc)) != 0) {
789 sc->sc_padr_retried--;
790 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
792 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
793 break;
794 case PPPOE_STATE_CLOSING:
795 pppoe_do_disconnect(sc);
796 break;
797 default:
798 return; /* all done, work in peace */
802 /* Start a connection (i.e. initiate discovery phase) */
804 pppoe_connect(struct pppoe_softc *sc)
806 int err;
808 if (sc->sc_state != PPPOE_STATE_INITIAL) {
809 return EBUSY;
812 #ifdef PPPOE_SERVER
813 /* wait PADI if IFF_PASSIVE */
814 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
815 return 0;
817 #endif
818 /* save state, in case we fail to send PADI */
819 sc->sc_state = PPPOE_STATE_PADI_SENT;
820 sc->sc_padr_retried = 0;
821 err = pppoe_send_padi(sc);
822 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
823 sys_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
824 return err;
827 /* disconnect */
828 void
829 pppoe_disconnect(struct pppoe_softc *sc)
831 if (sc->sc_state < PPPOE_STATE_SESSION) {
832 return;
835 * Do not call pppoe_disconnect here, the upper layer state
836 * machine gets confused by this. We must return from this
837 * function and defer disconnecting to the timeout handler.
839 sc->sc_state = PPPOE_STATE_CLOSING;
840 sys_timeout(20, pppoe_timeout, sc);
843 static int
844 pppoe_do_disconnect(struct pppoe_softc *sc)
846 int err;
848 if (sc->sc_state < PPPOE_STATE_SESSION) {
849 err = EBUSY;
850 } else {
851 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
852 err = pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);
855 /* cleanup softc */
856 sc->sc_state = PPPOE_STATE_INITIAL;
857 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
858 sc->sc_ac_cookie_len = 0;
859 #ifdef PPPOE_SERVER
860 if (sc->sc_hunique) {
861 mem_free(sc->sc_hunique);
862 sc->sc_hunique = NULL;
864 sc->sc_hunique_len = 0;
865 #endif
866 sc->sc_session = 0;
868 sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */
870 return err;
873 /* Connection attempt aborted */
874 static void
875 pppoe_abort_connect(struct pppoe_softc *sc)
877 printf("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
878 sc->sc_state = PPPOE_STATE_CLOSING;
880 sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */
882 /* clear connection state */
883 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
884 sc->sc_state = PPPOE_STATE_INITIAL;
887 /* Send a PADR packet */
888 static err_t
889 pppoe_send_padr(struct pppoe_softc *sc)
891 struct pbuf *pb;
892 u8_t *p;
893 size_t len;
894 #ifdef PPPOE_TODO
895 size_t l1 = 0; /* XXX: gcc */
896 #endif /* PPPOE_TODO */
898 if (sc->sc_state != PPPOE_STATE_PADR_SENT) {
899 return ERR_CONN;
902 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
903 #ifdef PPPOE_TODO
904 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
905 l1 = strlen(sc->sc_service_name);
906 len += l1;
908 #endif /* PPPOE_TODO */
909 if (sc->sc_ac_cookie_len > 0) {
910 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
912 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
913 sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
914 pb = pbuf_alloc(PBUF_LINK, (u16_t)(sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len), PBUF_RAM);
915 if (!pb) {
916 return ERR_MEM;
918 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
919 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
920 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
921 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
922 #ifdef PPPOE_TODO
923 if (sc->sc_service_name != NULL) {
924 PPPOE_ADD_16(p, l1);
925 MEMCPY(p, sc->sc_service_name, l1);
926 p += l1;
927 } else
928 #endif /* PPPOE_TODO */
930 PPPOE_ADD_16(p, 0);
932 if (sc->sc_ac_cookie_len > 0) {
933 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
934 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
935 MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
936 p += sc->sc_ac_cookie_len;
938 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
939 PPPOE_ADD_16(p, sizeof(sc));
940 MEMCPY(p, &sc, sizeof sc);
942 return pppoe_output(sc, pb);
945 /* send a PADT packet */
946 static err_t
947 pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
949 struct pbuf *pb;
950 struct eth_hdr *ethhdr;
951 err_t res;
952 u8_t *p;
954 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN, PBUF_RAM);
955 if (!pb) {
956 return ERR_MEM;
958 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
960 ethhdr = (struct eth_hdr *)pb->payload;
961 ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
962 MEMCPY(ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
963 MEMCPY(ethhdr->src.addr, ((struct eth_addr *)outgoing_if->hwaddr)->addr, sizeof(ethhdr->src.addr));
965 p = (u8_t*)(ethhdr + 1);
966 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
968 res = outgoing_if->linkoutput(outgoing_if, pb);
970 pbuf_free(pb);
972 return res;
975 #ifdef PPPOE_SERVER
976 static err_t
977 pppoe_send_pado(struct pppoe_softc *sc)
979 struct pbuf *pb;
980 u8_t *p;
981 size_t len;
983 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
984 return ERR_CONN;
987 /* calc length */
988 len = 0;
989 /* include ac_cookie */
990 len += 2 + 2 + sizeof(sc);
991 /* include hunique */
992 len += 2 + 2 + sc->sc_hunique_len;
993 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
994 if (!pb) {
995 return ERR_MEM;
997 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
998 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
999 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1000 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1001 PPPOE_ADD_16(p, sizeof(sc));
1002 MEMCPY(p, &sc, sizeof(sc));
1003 p += sizeof(sc);
1004 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1005 PPPOE_ADD_16(p, sc->sc_hunique_len);
1006 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1007 return pppoe_output(sc, pb);
1010 static err_t
1011 pppoe_send_pads(struct pppoe_softc *sc)
1013 struct pbuf *pb;
1014 u8_t *p;
1015 size_t len, l1 = 0; /* XXX: gcc */
1017 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
1018 return ERR_CONN;
1021 sc->sc_session = mono_time.tv_sec % 0xff + 1;
1022 /* calc length */
1023 len = 0;
1024 /* include hunique */
1025 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
1026 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1027 l1 = strlen(sc->sc_service_name);
1028 len += l1;
1030 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
1031 if (!pb) {
1032 return ERR_MEM;
1034 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1035 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
1036 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1037 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1038 if (sc->sc_service_name != NULL) {
1039 PPPOE_ADD_16(p, l1);
1040 MEMCPY(p, sc->sc_service_name, l1);
1041 p += l1;
1042 } else {
1043 PPPOE_ADD_16(p, 0);
1045 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1046 PPPOE_ADD_16(p, sc->sc_hunique_len);
1047 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1048 return pppoe_output(sc, pb);
1050 #endif
1052 err_t
1053 pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
1055 u8_t *p;
1056 size_t len;
1058 /* are we ready to process data yet? */
1059 if (sc->sc_state < PPPOE_STATE_SESSION) {
1060 /*sppp_flush(&sc->sc_sppp.pp_if);*/
1061 pbuf_free(pb);
1062 return ERR_CONN;
1065 len = pb->tot_len;
1067 /* make room for Ethernet header - should not fail */
1068 if (pbuf_header(pb, sizeof(struct eth_hdr) + PPPOE_HEADERLEN) != 0) {
1069 /* bail out */
1070 PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
1071 LINK_STATS_INC(link.lenerr);
1072 pbuf_free(pb);
1073 return ERR_BUF;
1076 p = (u8_t*)pb->payload + sizeof(struct eth_hdr);
1077 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1079 return pppoe_output(sc, pb);
1082 #if 0 /*def PFIL_HOOKS*/
1083 static int
1084 pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)
1086 struct pppoe_softc *sc;
1087 int s;
1089 if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {
1090 return 0;
1093 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1094 if (sc->sc_ethif != ifp) {
1095 continue;
1097 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1098 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1099 printf("%c%c%"U16_F": ethernet interface detached, going down\n",
1100 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
1102 sc->sc_ethif = NULL;
1103 pppoe_clear_softc(sc, "ethernet interface detached");
1106 return 0;
1108 #endif
1110 static void
1111 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1113 LWIP_UNUSED_ARG(message);
1115 /* stop timer */
1116 sys_untimeout(pppoe_timeout, sc);
1117 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": session 0x%x terminated, %s\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_session, message));
1119 /* fix our state */
1120 sc->sc_state = PPPOE_STATE_INITIAL;
1122 /* notify upper layers */
1123 sc->sc_linkStatusCB(sc->sc_pd, 0);
1125 /* clean up softc */
1126 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
1127 sc->sc_ac_cookie_len = 0;
1128 sc->sc_session = 0;
1131 #endif /* PPPOE_SUPPORT */