1 /*****************************************************************************
2 * pppoe.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 ******************************************************************************
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>
29 *****************************************************************************/
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */
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
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 "netif/ppp/ppp_opts.h"
72 #if PPP_SUPPORT && PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
79 #include "lwip/timeouts.h"
80 #include "lwip/memp.h"
81 #include "lwip/stats.h"
82 #include "lwip/snmp.h"
84 #include "netif/ethernet.h"
85 #include "netif/ppp/ppp_impl.h"
86 #include "netif/ppp/lcp.h"
87 #include "netif/ppp/ipcp.h"
88 #include "netif/ppp/pppoe.h"
91 LWIP_MEMPOOL_DECLARE(PPPOE_IF, MEMP_NUM_PPPOE_INTERFACES, sizeof(struct pppoe_softc), "PPPOE_IF")
93 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
94 #define PPPOE_ADD_16(PTR, VAL) \
95 *(PTR)++ = (u8_t)((VAL) / 256); \
96 *(PTR)++ = (u8_t)((VAL) % 256)
98 /* Add a complete PPPoE header to the buffer pointed to by PTR */
99 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
100 *(PTR)++ = PPPOE_VERTYPE; \
102 PPPOE_ADD_16(PTR, SESS); \
103 PPPOE_ADD_16(PTR, LEN)
105 #define PPPOE_DISC_TIMEOUT (5*1000) /* base for quick timeout calculation */
106 #define PPPOE_SLOW_RETRY (60*1000) /* persistent retry interval */
107 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
108 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
111 #error "PPPOE_SERVER is not yet supported under lwIP!"
112 /* from if_spppsubr.c */
113 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
116 #define PPPOE_ERRORSTRING_LEN 64
119 /* callbacks called from PPP core */
120 static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p);
121 static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol);
122 static void pppoe_connect(ppp_pcb *ppp, void *ctx);
123 static void pppoe_disconnect(ppp_pcb *ppp, void *ctx);
124 static err_t pppoe_destroy(ppp_pcb *ppp, void *ctx);
126 /* management routines */
127 static void pppoe_abort_connect(struct pppoe_softc *);
129 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
132 /* internal timeout handling */
133 static void pppoe_timeout(void *);
135 /* sending actual protocol controll packets */
136 static err_t pppoe_send_padi(struct pppoe_softc *);
137 static err_t pppoe_send_padr(struct pppoe_softc *);
139 static err_t pppoe_send_pado(struct pppoe_softc *);
140 static err_t pppoe_send_pads(struct pppoe_softc *);
142 static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);
144 /* internal helper functions */
145 static err_t pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb);
146 static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif);
147 static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif);
149 /** linked list of created pppoe interfaces */
150 static struct pppoe_softc *pppoe_softc_list;
152 /* Callbacks structure for PPP core */
153 static const struct link_callbacks pppoe_callbacks = {
157 #endif /* PPP_SERVER */
167 * Create a new PPP Over Ethernet (PPPoE) connection.
169 * Return 0 on success, an error code on failure.
171 ppp_pcb *pppoe_create(struct netif *pppif,
173 const char *service_name, const char *concentrator_name,
174 ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
177 struct pppoe_softc *sc;
178 LWIP_UNUSED_ARG(service_name);
179 LWIP_UNUSED_ARG(concentrator_name);
181 sc = (struct pppoe_softc *)LWIP_MEMPOOL_ALLOC(PPPOE_IF);
186 ppp = ppp_new(pppif, &pppoe_callbacks, sc, link_status_cb, ctx_cb);
188 LWIP_MEMPOOL_FREE(PPPOE_IF, sc);
192 memset(sc, 0, sizeof(struct pppoe_softc));
194 sc->sc_ethif = ethif;
195 /* put the new interface at the head of the list */
196 sc->next = pppoe_softc_list;
197 pppoe_softc_list = sc;
201 /* Called by PPP core */
202 static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) {
203 struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
204 struct pbuf *ph; /* Ethernet + PPPoE header */
208 #else /* MIB2_STATS */
209 LWIP_UNUSED_ARG(ppp);
210 #endif /* MIB2_STATS */
212 /* skip address & flags */
213 pbuf_header(p, -(s16_t)2);
215 ph = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
217 LINK_STATS_INC(link.memerr);
218 LINK_STATS_INC(link.proterr);
219 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
224 pbuf_header(ph, -(s16_t)PPPOE_HEADERLEN); /* hide PPPoE header */
227 tot_len = ph->tot_len;
228 #endif /* MIB2_STATS */
230 ret = pppoe_xmit(sc, ph);
232 LINK_STATS_INC(link.err);
233 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
237 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len);
238 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
239 LINK_STATS_INC(link.xmit);
243 /* Called by PPP core */
244 static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol) {
245 struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
251 #else /* MIB2_STATS */
252 LWIP_UNUSED_ARG(ppp);
253 #endif /* MIB2_STATS */
255 /* @todo: try to use pbuf_header() here! */
256 pb = pbuf_alloc(PBUF_LINK, PPPOE_HEADERLEN + sizeof(protocol), PBUF_RAM);
258 LINK_STATS_INC(link.memerr);
259 LINK_STATS_INC(link.proterr);
260 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
264 pbuf_header(pb, -(s16_t)PPPOE_HEADERLEN);
266 pl = (u8_t*)pb->payload;
267 PUTSHORT(protocol, pl);
271 tot_len = pb->tot_len;
272 #endif /* MIB2_STATS */
274 if( (err = pppoe_xmit(sc, pb)) != ERR_OK) {
275 LINK_STATS_INC(link.err);
276 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
280 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
281 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
282 LINK_STATS_INC(link.xmit);
287 pppoe_destroy(ppp_pcb *ppp, void *ctx)
289 struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
290 struct pppoe_softc **copp, *freep;
291 LWIP_UNUSED_ARG(ppp);
293 sys_untimeout(pppoe_timeout, sc);
295 /* remove interface from list */
296 for (copp = &pppoe_softc_list; (freep = *copp); copp = &freep->next) {
304 if (sc->sc_concentrator_name) {
305 mem_free(sc->sc_concentrator_name);
307 if (sc->sc_service_name) {
308 mem_free(sc->sc_service_name);
310 #endif /* PPPOE_TODO */
311 LWIP_MEMPOOL_FREE(PPPOE_IF, sc);
317 * Find the interface handling the specified session.
318 * Note: O(number of sessions open), this is a client-side only, mean
319 * and lean implementation, so number of open sessions typically should
322 static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif) {
323 struct pppoe_softc *sc;
325 for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
326 if (sc->sc_state == PPPOE_STATE_SESSION
327 && sc->sc_session == session
328 && sc->sc_ethif == rcvif) {
335 /* Check host unique token passed and return appropriate softc pointer,
336 * or NULL if token is bogus. */
337 static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif) {
338 struct pppoe_softc *sc, *t;
340 if (len != sizeof sc) {
343 MEMCPY(&t, token, len);
345 for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
352 PPPDEBUG(LOG_DEBUG, ("pppoe: alien host unique tag, no session found\n"));
356 /* should be safe to access *sc now */
357 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
358 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
359 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state));
362 if (sc->sc_ethif != rcvif) {
363 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": wrong interface, not accepting host unique\n",
364 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
370 /* analyze and handle a single received packet while not in session state */
372 pppoe_disc_input(struct netif *netif, struct pbuf *pb)
376 struct pppoe_softc *sc;
378 const char *err_msg = NULL;
379 #endif /* PPP_DEBUG */
389 struct eth_hdr *ethhdr;
391 /* don't do anything if there is not a single PPPoE instance */
392 if (pppoe_softc_list == NULL) {
397 pb = ppp_singlebuf(pb);
399 if (pb->len < sizeof(*ethhdr)) {
402 ethhdr = (struct eth_hdr *)pb->payload;
403 off = sizeof(*ethhdr);
412 if (pb->len - off < (u16_t)PPPOE_HEADERLEN) {
413 PPPDEBUG(LOG_DEBUG, ("pppoe: packet too short: %d\n", pb->len));
417 ph = (struct pppoehdr *) (ethhdr + 1);
418 if (ph->vertype != PPPOE_VERTYPE) {
419 PPPDEBUG(LOG_DEBUG, ("pppoe: unknown version/type packet: 0x%x\n", ph->vertype));
422 session = lwip_ntohs(ph->session);
423 plen = lwip_ntohs(ph->plen);
426 if (plen + off > pb->len) {
427 PPPDEBUG(LOG_DEBUG, ("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
428 pb->len - off, plen));
431 if(pb->tot_len == pb->len) {
432 pb->tot_len = pb->len = (u16_t)off + plen; /* ignore trailing garbage */
437 while (off + sizeof(pt) <= pb->len) {
438 MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));
439 tag = lwip_ntohs(pt.tag);
440 len = lwip_ntohs(pt.len);
441 if (off + sizeof(pt) + len > pb->len) {
442 PPPDEBUG(LOG_DEBUG, ("pppoe: tag 0x%x len 0x%x is too long\n", tag, len));
448 case PPPOE_TAG_SNAME:
450 case PPPOE_TAG_ACNAME:
452 case PPPOE_TAG_HUNIQUE:
457 hunique = (u8_t*)pb->payload + off + sizeof(pt);
460 sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);
462 case PPPOE_TAG_ACCOOKIE:
463 if (ac_cookie == NULL) {
464 if (len > PPPOE_MAX_AC_COOKIE_LEN) {
465 PPPDEBUG(LOG_DEBUG, ("pppoe: AC cookie is too long: len = %d, max = %d\n", len, PPPOE_MAX_AC_COOKIE_LEN));
468 ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);
473 case PPPOE_TAG_SNAME_ERR:
474 err_msg = "SERVICE NAME ERROR";
476 case PPPOE_TAG_ACSYS_ERR:
477 err_msg = "AC SYSTEM ERROR";
479 case PPPOE_TAG_GENERIC_ERR:
480 err_msg = "GENERIC ERROR";
482 #endif /* PPP_DEBUG */
487 if (err_msg != NULL) {
488 char error_tmp[PPPOE_ERRORSTRING_LEN];
489 u16_t error_len = LWIP_MIN(len, sizeof(error_tmp)-1);
490 strncpy(error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
491 error_tmp[error_len] = '\0';
493 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": %s: %s\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err_msg, error_tmp));
495 PPPDEBUG(LOG_DEBUG, ("pppoe: %s: %s\n", err_msg, error_tmp));
498 #endif /* PPP_DEBUG */
499 off += sizeof(pt) + len;
504 case PPPOE_CODE_PADI:
507 * got service name, concentrator name, and/or host unique.
508 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
510 if (LIST_EMPTY(&pppoe_softc_list)) {
513 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
514 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
517 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
520 if (sc->sc_state == PPPOE_STATE_INITIAL) {
525 /* PPPDEBUG(LOG_DEBUG, ("pppoe: free passive interface is not found\n")); */
529 if (sc->sc_hunique) {
530 mem_free(sc->sc_hunique);
532 sc->sc_hunique = mem_malloc(hunique_len);
533 if (sc->sc_hunique == NULL) {
536 sc->sc_hunique_len = hunique_len;
537 MEMCPY(sc->sc_hunique, hunique, hunique_len);
539 MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
540 sc->sc_state = PPPOE_STATE_PADO_SENT;
543 #endif /* PPPOE_SERVER */
544 case PPPOE_CODE_PADR:
547 * get sc from ac_cookie if IFF_PASSIVE
549 if (ac_cookie == NULL) {
550 /* be quiet if there is not a single pppoe instance */
551 PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but not includes ac_cookie\n"));
554 sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
556 /* be quiet if there is not a single pppoe instance */
557 if (!LIST_EMPTY(&pppoe_softc_list)) {
558 PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but could not find request for it\n"));
562 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
563 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
567 if (sc->sc_hunique) {
568 mem_free(sc->sc_hunique);
570 sc->sc_hunique = mem_malloc(hunique_len);
571 if (sc->sc_hunique == NULL) {
574 sc->sc_hunique_len = hunique_len;
575 MEMCPY(sc->sc_hunique, hunique, hunique_len);
578 sc->sc_state = PPPOE_STATE_SESSION;
579 ppp_start(sc->pcb); /* notify upper layers */
582 /* ignore, we are no access concentrator */
584 #endif /* PPPOE_SERVER */
585 case PPPOE_CODE_PADO:
587 /* be quiet if there is not a single pppoe instance */
588 if (pppoe_softc_list != NULL) {
589 PPPDEBUG(LOG_DEBUG, ("pppoe: received PADO but could not find request for it\n"));
593 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
594 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
598 sc->sc_ac_cookie_len = ac_cookie_len;
599 MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
601 MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));
602 sys_untimeout(pppoe_timeout, sc);
603 sc->sc_padr_retried = 0;
604 sc->sc_state = PPPOE_STATE_PADR_SENT;
605 if ((err = pppoe_send_padr(sc)) != 0) {
606 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));
608 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
610 case PPPOE_CODE_PADS:
614 sc->sc_session = session;
615 sys_untimeout(pppoe_timeout, sc);
616 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));
617 sc->sc_state = PPPOE_STATE_SESSION;
618 ppp_start(sc->pcb); /* notify upper layers */
620 case PPPOE_CODE_PADT:
621 /* Don't disconnect here, we let the LCP Echo/Reply find the fact
622 * that PPP session is down. Asking the PPP stack to end the session
623 * require strict checking about the PPP phase to prevent endless
624 * disconnection loops.
627 if (sc == NULL) { /* PADT frames are rarely sent with a hunique tag, this is actually almost always true */
630 pppoe_clear_softc(sc, "received PADT");
635 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
636 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
637 (u16_t)ph->code, session));
639 PPPDEBUG(LOG_DEBUG, ("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session));
650 pppoe_data_input(struct netif *netif, struct pbuf *pb)
653 struct pppoe_softc *sc;
655 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
656 u8_t shost[ETHER_ADDR_LEN];
659 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
660 MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));
662 if (pbuf_header(pb, -(s16_t)sizeof(struct eth_hdr)) != 0) {
664 PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header failed\n"));
665 LINK_STATS_INC(link.lenerr);
669 if (pb->len < sizeof(*ph)) {
670 PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: could not get PPPoE header\n"));
673 ph = (struct pppoehdr *)pb->payload;
675 if (ph->vertype != PPPOE_VERTYPE) {
676 PPPDEBUG(LOG_DEBUG, ("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype));
683 session = lwip_ntohs(ph->session);
684 sc = pppoe_find_softc_by_session(session, netif);
686 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
687 PPPDEBUG(LOG_DEBUG, ("pppoe: input for unknown session 0x%x, sending PADT\n", session));
688 pppoe_send_padt(netif, session, shost);
693 plen = lwip_ntohs(ph->plen);
695 if (pbuf_header(pb, -(s16_t)(PPPOE_HEADERLEN)) != 0) {
697 PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));
698 LINK_STATS_INC(link.lenerr);
702 PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",
703 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
706 if (pb->tot_len < plen) {
710 /* Dispatch the packet thereby consuming it. */
711 ppp_input(sc->pcb, pb);
719 pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
721 struct eth_hdr *ethhdr;
725 /* make room for Ethernet header - should not fail */
726 if (pbuf_header(pb, (s16_t)(sizeof(struct eth_hdr))) != 0) {
728 PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_output: could not allocate room for Ethernet header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
729 LINK_STATS_INC(link.lenerr);
733 ethhdr = (struct eth_hdr *)pb->payload;
734 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
735 ethhdr->type = lwip_htons(etype);
736 MEMCPY(ðhdr->dest.addr, &sc->sc_dest.addr, sizeof(ethhdr->dest.addr));
737 MEMCPY(ðhdr->src.addr, &sc->sc_ethif->hwaddr, sizeof(ethhdr->src.addr));
739 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",
740 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,
741 sc->sc_state, sc->sc_session,
742 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],
745 res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);
753 pppoe_send_padi(struct pppoe_softc *sc)
759 int l1 = 0, l2 = 0; /* XXX: gcc */
760 #endif /* PPPOE_TODO */
762 /* calculate length of frame (excluding ethernet header + pppoe header) */
763 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
765 if (sc->sc_service_name != NULL) {
766 l1 = (int)strlen(sc->sc_service_name);
769 if (sc->sc_concentrator_name != NULL) {
770 l2 = (int)strlen(sc->sc_concentrator_name);
773 #endif /* PPPOE_TODO */
774 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
775 sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
777 /* allocate a buffer */
778 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
782 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
784 p = (u8_t*)pb->payload;
786 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
787 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
789 if (sc->sc_service_name != NULL) {
791 MEMCPY(p, sc->sc_service_name, l1);
794 #endif /* PPPOE_TODO */
799 if (sc->sc_concentrator_name != NULL) {
800 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
802 MEMCPY(p, sc->sc_concentrator_name, l2);
805 #endif /* PPPOE_TODO */
806 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
807 PPPOE_ADD_16(p, sizeof(sc));
808 MEMCPY(p, &sc, sizeof sc);
811 return pppoe_output(sc, pb);
815 pppoe_timeout(void *arg)
819 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
821 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
823 switch (sc->sc_state) {
824 case PPPOE_STATE_PADI_SENT:
826 * We have two basic ways of retrying:
827 * - Quick retry mode: try a few times in short sequence
828 * - Slow retry mode: we already had a connection successfully
829 * established and will try infinitely (without user
831 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
834 if (sc->sc_padi_retried < 0xff) {
835 sc->sc_padi_retried++;
837 if (!sc->pcb->settings.persist && sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
839 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
840 /* slow retry mode */
841 retry_wait = PPPOE_SLOW_RETRY;
845 pppoe_abort_connect(sc);
849 /* initialize for quick retry mode */
850 retry_wait = LWIP_MIN(PPPOE_DISC_TIMEOUT * sc->sc_padi_retried, PPPOE_SLOW_RETRY);
851 if ((err = pppoe_send_padi(sc)) != 0) {
852 sc->sc_padi_retried--;
853 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));
855 sys_timeout(retry_wait, pppoe_timeout, sc);
858 case PPPOE_STATE_PADR_SENT:
859 sc->sc_padr_retried++;
860 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
861 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
862 sc->sc_state = PPPOE_STATE_PADI_SENT;
863 sc->sc_padr_retried = 0;
864 if ((err = pppoe_send_padi(sc)) != 0) {
865 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));
867 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);
870 if ((err = pppoe_send_padr(sc)) != 0) {
871 sc->sc_padr_retried--;
872 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));
874 sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
877 return; /* all done, work in peace */
881 /* Start a connection (i.e. initiate discovery phase) */
883 pppoe_connect(ppp_pcb *ppp, void *ctx)
886 struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
889 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
890 ipcp_options *ipcp_wo;
891 ipcp_options *ipcp_ao;
892 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
895 sc->sc_ac_cookie_len = 0;
896 sc->sc_padi_retried = 0;
897 sc->sc_padr_retried = 0;
898 /* changed to real address later */
899 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
901 /* wait PADI if IFF_PASSIVE */
902 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
907 lcp_wo = &ppp->lcp_wantoptions;
908 lcp_wo->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2; /* two byte PPP protocol discriminator, then IP data */
909 lcp_wo->neg_asyncmap = 0;
910 lcp_wo->neg_pcompression = 0;
911 lcp_wo->neg_accompression = 0;
915 lcp_ao = &ppp->lcp_allowoptions;
916 lcp_ao->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2; /* two byte PPP protocol discriminator, then IP data */
917 lcp_ao->neg_asyncmap = 0;
918 lcp_ao->neg_pcompression = 0;
919 lcp_ao->neg_accompression = 0;
921 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
922 ipcp_wo = &ppp->ipcp_wantoptions;
926 ipcp_ao = &ppp->ipcp_allowoptions;
929 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
931 /* save state, in case we fail to send PADI */
932 sc->sc_state = PPPOE_STATE_PADI_SENT;
933 if ((err = pppoe_send_padi(sc)) != 0) {
934 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));
936 sys_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
941 pppoe_disconnect(ppp_pcb *ppp, void *ctx)
943 struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
945 PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
946 if (sc->sc_state == PPPOE_STATE_SESSION) {
947 pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);
950 /* stop any timer, disconnect can be called while initiating is in progress */
951 sys_untimeout(pppoe_timeout, sc);
952 sc->sc_state = PPPOE_STATE_INITIAL;
954 if (sc->sc_hunique) {
955 mem_free(sc->sc_hunique);
956 sc->sc_hunique = NULL; /* probably not necessary, if state is initial we shouldn't have to access hunique anyway */
958 sc->sc_hunique_len = 0; /* probably not necessary, if state is initial we shouldn't have to access hunique anyway */
960 ppp_link_end(ppp); /* notify upper layers */
964 /* Connection attempt aborted */
966 pppoe_abort_connect(struct pppoe_softc *sc)
968 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
969 sc->sc_state = PPPOE_STATE_INITIAL;
970 ppp_link_failed(sc->pcb); /* notify upper layers */
973 /* Send a PADR packet */
975 pppoe_send_padr(struct pppoe_softc *sc)
981 size_t l1 = 0; /* XXX: gcc */
982 #endif /* PPPOE_TODO */
984 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
986 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
987 l1 = strlen(sc->sc_service_name);
990 #endif /* PPPOE_TODO */
991 if (sc->sc_ac_cookie_len > 0) {
992 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
994 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
995 sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
996 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1000 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1001 p = (u8_t*)pb->payload;
1002 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1003 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1005 if (sc->sc_service_name != NULL) {
1006 PPPOE_ADD_16(p, l1);
1007 MEMCPY(p, sc->sc_service_name, l1);
1010 #endif /* PPPOE_TODO */
1014 if (sc->sc_ac_cookie_len > 0) {
1015 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1016 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1017 MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1018 p += sc->sc_ac_cookie_len;
1020 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1021 PPPOE_ADD_16(p, sizeof(sc));
1022 MEMCPY(p, &sc, sizeof sc);
1024 return pppoe_output(sc, pb);
1027 /* send a PADT packet */
1029 pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
1032 struct eth_hdr *ethhdr;
1036 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
1040 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1042 pbuf_header(pb, (s16_t)sizeof(struct eth_hdr));
1043 ethhdr = (struct eth_hdr *)pb->payload;
1044 ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
1045 MEMCPY(ðhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
1046 MEMCPY(ðhdr->src.addr, &outgoing_if->hwaddr, sizeof(ethhdr->src.addr));
1048 p = (u8_t*)(ethhdr + 1);
1049 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
1051 res = outgoing_if->linkoutput(outgoing_if, pb);
1060 pppoe_send_pado(struct pppoe_softc *sc)
1068 /* include ac_cookie */
1069 len += 2 + 2 + sizeof(sc);
1070 /* include hunique */
1071 len += 2 + 2 + sc->sc_hunique_len;
1072 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1076 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1077 p = (u8_t*)pb->payload;
1078 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1079 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1080 PPPOE_ADD_16(p, sizeof(sc));
1081 MEMCPY(p, &sc, sizeof(sc));
1083 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1084 PPPOE_ADD_16(p, sc->sc_hunique_len);
1085 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1086 return pppoe_output(sc, pb);
1090 pppoe_send_pads(struct pppoe_softc *sc)
1094 size_t len, l1 = 0; /* XXX: gcc */
1096 sc->sc_session = mono_time.tv_sec % 0xff + 1;
1099 /* include hunique */
1100 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
1101 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1102 l1 = strlen(sc->sc_service_name);
1105 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1109 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1110 p = (u8_t*)pb->payload;
1111 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1112 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1113 if (sc->sc_service_name != NULL) {
1114 PPPOE_ADD_16(p, l1);
1115 MEMCPY(p, sc->sc_service_name, l1);
1120 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1121 PPPOE_ADD_16(p, sc->sc_hunique_len);
1122 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1123 return pppoe_output(sc, pb);
1128 pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
1135 /* make room for PPPoE header - should not fail */
1136 if (pbuf_header(pb, (s16_t)(PPPOE_HEADERLEN)) != 0) {
1138 PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for PPPoE header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
1139 LINK_STATS_INC(link.lenerr);
1144 p = (u8_t*)pb->payload;
1145 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1147 return pppoe_output(sc, pb);
1150 #if 0 /*def PFIL_HOOKS*/
1152 pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)
1154 struct pppoe_softc *sc;
1157 if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {
1161 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1162 if (sc->sc_ethif != ifp) {
1165 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1166 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1167 PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": ethernet interface detached, going down\n",
1168 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
1170 sc->sc_ethif = NULL;
1171 pppoe_clear_softc(sc, "ethernet interface detached");
1180 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1182 LWIP_UNUSED_ARG(message);
1185 sys_untimeout(pppoe_timeout, sc);
1186 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));
1187 sc->sc_state = PPPOE_STATE_INITIAL;
1188 ppp_link_end(sc->pcb); /* notify upper layers - /!\ dangerous /!\ - see pppoe_disc_input() */
1191 #endif /* PPP_SUPPORT && PPPOE_SUPPORT */