]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/Common/ethernet/lwIP_130/src/netif/ppp/ppp_oe.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP_130 / src / netif / ppp / ppp_oe.c
diff --git a/FreeRTOS/Demo/Common/ethernet/lwIP_130/src/netif/ppp/ppp_oe.c b/FreeRTOS/Demo/Common/ethernet/lwIP_130/src/netif/ppp/ppp_oe.c
new file mode 100644 (file)
index 0000000..2fcffdd
--- /dev/null
@@ -0,0 +1,1227 @@
+/*****************************************************************************\r
+* ppp_oe.c - PPP Over Ethernet implementation for lwIP.\r
+*\r
+* Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.\r
+*\r
+* The authors hereby grant permission to use, copy, modify, distribute,\r
+* and license this software and its documentation for any purpose, provided\r
+* that existing copyright notices are retained in all copies and that this\r
+* notice and the following disclaimer are included verbatim in any \r
+* distributions. No written agreement, license, or royalty fee is required\r
+* for any of the authorized uses.\r
+*\r
+* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR\r
+* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
+* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*\r
+******************************************************************************\r
+* REVISION HISTORY\r
+*\r
+* 06-01-01 Marc Boucher <marc@mbsi.ca>\r
+*   Ported to lwIP.\r
+*****************************************************************************/\r
+\r
+\r
+\r
+/* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */\r
+\r
+/*-\r
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.\r
+ * All rights reserved.\r
+ *\r
+ * This code is derived from software contributed to The NetBSD Foundation\r
+ * by Martin Husemann <martin@NetBSD.org>.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ *    notice, this list of conditions and the following disclaimer in the\r
+ *    documentation and/or other materials provided with the distribution.\r
+ * 3. All advertising materials mentioning features or use of this software\r
+ *    must display the following acknowledgement:\r
+ *        This product includes software developed by the NetBSD\r
+ *        Foundation, Inc. and its contributors.\r
+ * 4. Neither the name of The NetBSD Foundation nor the names of its\r
+ *    contributors may be used to endorse or promote products derived\r
+ *    from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS\r
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS\r
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+ * POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+\r
+#include "lwip/opt.h"\r
+\r
+#if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */\r
+\r
+#include "ppp.h"\r
+#include "pppdebug.h"\r
+\r
+#include "lwip/sys.h"\r
+\r
+#include "netif/ppp_oe.h"\r
+#include "netif/etharp.h"\r
+\r
+#include <string.h>\r
+#include <stdio.h>\r
+\r
+/** @todo Replace this part with a simple list like other lwIP lists */\r
+#ifndef _SYS_QUEUE_H_\r
+#define _SYS_QUEUE_H_\r
+\r
+/*\r
+ * A list is headed by a single forward pointer (or an array of forward\r
+ * pointers for a hash table header). The elements are doubly linked\r
+ * so that an arbitrary element can be removed without a need to\r
+ * traverse the list. New elements can be added to the list before\r
+ * or after an existing element or at the head of the list. A list\r
+ * may only be traversed in the forward direction.\r
+ *\r
+ * For details on the use of these macros, see the queue(3) manual page.\r
+ */\r
+\r
+/*\r
+ * List declarations.\r
+ */\r
+#define  LIST_HEAD(name, type)                                                 \\r
+struct name {                                                                  \\r
+  struct type *lh_first;  /* first element */                                  \\r
+}\r
+\r
+#define  LIST_HEAD_INITIALIZER(head)                                           \\r
+  { NULL }\r
+\r
+#define  LIST_ENTRY(type)                                                      \\r
+struct {                                                                       \\r
+  struct type *le_next;  /* next element */                                    \\r
+  struct type **le_prev; /* address of previous next element */                \\r
+}\r
+\r
+/*\r
+ * List functions.\r
+ */\r
+\r
+#define  LIST_EMPTY(head)  ((head)->lh_first == NULL)\r
+\r
+#define  LIST_FIRST(head)  ((head)->lh_first)\r
+\r
+#define  LIST_FOREACH(var, head, field)                                        \\r
+  for ((var) = LIST_FIRST((head));                                             \\r
+      (var);                                                                   \\r
+      (var) = LIST_NEXT((var), field))\r
+\r
+#define  LIST_INIT(head) do {                                                  \\r
+  LIST_FIRST((head)) = NULL;                                                   \\r
+} while (0)\r
+\r
+#define  LIST_INSERT_AFTER(listelm, elm, field) do {                           \\r
+  if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)         \\r
+    LIST_NEXT((listelm), field)->field.le_prev =                               \\r
+        &LIST_NEXT((elm), field);                                              \\r
+  LIST_NEXT((listelm), field) = (elm);                                         \\r
+  (elm)->field.le_prev = &LIST_NEXT((listelm), field);                         \\r
+} while (0)\r
+\r
+#define  LIST_INSERT_BEFORE(listelm, elm, field) do {                          \\r
+  (elm)->field.le_prev = (listelm)->field.le_prev;                             \\r
+  LIST_NEXT((elm), field) = (listelm);                                         \\r
+  *(listelm)->field.le_prev = (elm);                                           \\r
+  (listelm)->field.le_prev = &LIST_NEXT((elm), field);                         \\r
+} while (0)\r
+\r
+#define  LIST_INSERT_HEAD(head, elm, field) do {                               \\r
+  if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)                  \\r
+    LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);              \\r
+  LIST_FIRST((head)) = (elm);                                                  \\r
+  (elm)->field.le_prev = &LIST_FIRST((head));                                  \\r
+} while (0)\r
+\r
+#define  LIST_NEXT(elm, field)  ((elm)->field.le_next)\r
+\r
+#define  LIST_REMOVE(elm, field) do {                                          \\r
+  if (LIST_NEXT((elm), field) != NULL)                                         \\r
+    LIST_NEXT((elm), field)->field.le_prev =                                   \\r
+        (elm)->field.le_prev;                                                  \\r
+  *(elm)->field.le_prev = LIST_NEXT((elm), field);                             \\r
+} while (0)\r
+\r
+#endif /* !_SYS_QUEUE_H_ */\r
+\r
+\r
+/* Add a 16 bit unsigned value to a buffer pointed to by PTR */\r
+#define PPPOE_ADD_16(PTR, VAL) \\r
+    *(PTR)++ = (VAL) / 256;    \\r
+    *(PTR)++ = (VAL) % 256\r
+\r
+/* Add a complete PPPoE header to the buffer pointed to by PTR */\r
+#define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)  \\r
+    *(PTR)++ = PPPOE_VERTYPE;  \\r
+    *(PTR)++ = (CODE);         \\r
+    PPPOE_ADD_16(PTR, SESS);   \\r
+    PPPOE_ADD_16(PTR, LEN)\r
+\r
+#define PPPOE_DISC_TIMEOUT (5*1000)  /* base for quick timeout calculation */\r
+#define PPPOE_SLOW_RETRY   (60*1000) /* persistent retry interval */\r
+#define PPPOE_DISC_MAXPADI  4        /* retry PADI four times (quickly) */\r
+#define PPPOE_DISC_MAXPADR  2        /* retry PADR twice */\r
+\r
+#ifdef PPPOE_SERVER\r
+/* from if_spppsubr.c */\r
+#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */\r
+#endif\r
+\r
+struct pppoe_softc {\r
+  LIST_ENTRY(pppoe_softc) sc_list;\r
+  struct netif *sc_ethif;      /* ethernet interface we are using */\r
+  int sc_pd;                   /* ppp unit number */\r
+  void (*sc_linkStatusCB)(int pd, int up);\r
+\r
+  int sc_state;                /* discovery phase or session connected */\r
+  struct eth_addr sc_dest;     /* hardware address of concentrator */\r
+  u16_t sc_session;            /* PPPoE session id */\r
+\r
+  char *sc_service_name;       /* if != NULL: requested name of service */\r
+  char *sc_concentrator_name;  /* if != NULL: requested concentrator id */\r
+  u8_t *sc_ac_cookie;          /* content of AC cookie we must echo back */\r
+  size_t sc_ac_cookie_len;     /* length of cookie data */\r
+#ifdef PPPOE_SERVER\r
+  u8_t *sc_hunique;            /* content of host unique we must echo back */\r
+  size_t sc_hunique_len;       /* length of host unique */\r
+#endif\r
+  int sc_padi_retried;         /* number of PADI retries already done */\r
+  int sc_padr_retried;         /* number of PADR retries already done */\r
+};\r
+\r
+/* input routines */\r
+static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);\r
+\r
+/* management routines */\r
+static int pppoe_do_disconnect(struct pppoe_softc *);\r
+static void pppoe_abort_connect(struct pppoe_softc *);\r
+static void pppoe_clear_softc(struct pppoe_softc *, const char *);\r
+\r
+/* internal timeout handling */\r
+static void pppoe_timeout(void *);\r
+\r
+/* sending actual protocol controll packets */\r
+static err_t pppoe_send_padi(struct pppoe_softc *);\r
+static err_t pppoe_send_padr(struct pppoe_softc *);\r
+#ifdef PPPOE_SERVER\r
+static err_t pppoe_send_pado(struct pppoe_softc *);\r
+static err_t pppoe_send_pads(struct pppoe_softc *);\r
+#endif\r
+static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);\r
+\r
+/* internal helper functions */\r
+static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);\r
+static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);\r
+\r
+static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;\r
+\r
+int pppoe_hdrlen;\r
+\r
+void\r
+pppoe_init(void)\r
+{\r
+  pppoe_hdrlen = sizeof(struct eth_hdr) + PPPOE_HEADERLEN;\r
+  LIST_INIT(&pppoe_softc_list);\r
+}\r
+\r
+err_t\r
+pppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr)\r
+{\r
+  struct pppoe_softc *sc;\r
+\r
+  sc = mem_malloc(sizeof(struct pppoe_softc));\r
+  if(!sc) {\r
+    *scptr = NULL;\r
+    return ERR_MEM;\r
+  }\r
+  memset(sc, 0, sizeof(struct pppoe_softc));\r
+\r
+  /* changed to real address later */\r
+  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
+\r
+  sc->sc_pd = pd;\r
+  sc->sc_linkStatusCB = linkStatusCB;\r
+  sc->sc_ethif = ethif;\r
+\r
+  LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);\r
+\r
+  *scptr = sc;\r
+\r
+  return ERR_OK;\r
+}\r
+\r
+err_t\r
+pppoe_destroy(struct netif *ifp)\r
+{\r
+  struct pppoe_softc * sc;\r
+\r
+  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
+    if (sc->sc_ethif == ifp) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if(!(sc && (sc->sc_ethif == ifp))) {\r
+    return ERR_IF;\r
+  }\r
+\r
+  tcpip_untimeout(pppoe_timeout, sc);\r
+  LIST_REMOVE(sc, sc_list);\r
+\r
+  if (sc->sc_concentrator_name) {\r
+    mem_free(sc->sc_concentrator_name);\r
+  }\r
+  if (sc->sc_service_name) {\r
+    mem_free(sc->sc_service_name);\r
+  }\r
+  if (sc->sc_ac_cookie) {\r
+    mem_free(sc->sc_ac_cookie);\r
+  }\r
+  mem_free(sc);\r
+\r
+  return ERR_OK;\r
+}\r
+\r
+/*\r
+ * Find the interface handling the specified session.\r
+ * Note: O(number of sessions open), this is a client-side only, mean\r
+ * and lean implementation, so number of open sessions typically should\r
+ * be 1.\r
+ */\r
+static struct pppoe_softc *\r
+pppoe_find_softc_by_session(u_int session, struct netif *rcvif)\r
+{\r
+  struct pppoe_softc *sc;\r
+\r
+  if (session == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
+    if (sc->sc_state == PPPOE_STATE_SESSION\r
+        && sc->sc_session == session) {\r
+      if (sc->sc_ethif == rcvif) {\r
+        return sc;\r
+      } else {\r
+        return NULL;\r
+      }\r
+    }\r
+  }\r
+  return NULL;\r
+}\r
+\r
+/* Check host unique token passed and return appropriate softc pointer,\r
+ * or NULL if token is bogus. */\r
+static struct pppoe_softc *\r
+pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif)\r
+{\r
+  struct pppoe_softc *sc, *t;\r
+\r
+  if (LIST_EMPTY(&pppoe_softc_list)) {\r
+    return NULL;\r
+  }\r
+\r
+  if (len != sizeof sc) {\r
+    return NULL;\r
+  }\r
+  MEMCPY(&t, token, len);\r
+\r
+  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
+    if (sc == t) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (sc == NULL) {\r
+    PPPDEBUG((LOG_DEBUG, "pppoe: alien host unique tag, no session found\n"));\r
+    return NULL;\r
+  }\r
+\r
+  /* should be safe to access *sc now */\r
+  if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {\r
+    printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",\r
+      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);\r
+    return NULL;\r
+  }\r
+  if (sc->sc_ethif != rcvif) {\r
+    printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",\r
+      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+    return NULL;\r
+  }\r
+  return sc;\r
+}\r
+\r
+static void\r
+pppoe_linkstatus_up(void *arg)\r
+{\r
+  struct pppoe_softc *sc = (struct pppoe_softc*)arg;\r
+\r
+  sc->sc_linkStatusCB(sc->sc_pd, 1);\r
+}\r
+\r
+/* analyze and handle a single received packet while not in session state */\r
+static void\r
+pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)\r
+{\r
+  u16_t tag, len;\r
+  u16_t session, plen;\r
+  struct pppoe_softc *sc;\r
+  const char *err_msg;\r
+  char devname[6];\r
+  char *error;\r
+  u8_t *ac_cookie;\r
+  size_t ac_cookie_len;\r
+#ifdef PPPOE_SERVER\r
+  u8_t *hunique;\r
+  size_t hunique_len;\r
+#endif\r
+  struct pppoehdr *ph;\r
+  struct pppoetag pt;\r
+  int off = 0, err, errortag;\r
+  struct eth_hdr *ethhdr;\r
+\r
+  pb = pppSingleBuf(pb);\r
+\r
+  strcpy(devname, "pppoe");  /* as long as we don't know which instance */\r
+  err_msg = NULL;\r
+  errortag = 0;\r
+  if (pb->len < sizeof(*ethhdr)) {\r
+    goto done;\r
+  }\r
+  ethhdr = (struct eth_hdr *)pb->payload;\r
+  off += sizeof(*ethhdr);\r
+\r
+  ac_cookie = NULL;\r
+  ac_cookie_len = 0;\r
+#ifdef PPPOE_SERVER\r
+  hunique = NULL;\r
+  hunique_len = 0;\r
+#endif\r
+  session = 0;\r
+  if (pb->len - off <= PPPOE_HEADERLEN) {\r
+    printf("pppoe: packet too short: %d\n", pb->len);\r
+    goto done;\r
+  }\r
+\r
+  ph = (struct pppoehdr *) (ethhdr + 1);\r
+  if (ph->vertype != PPPOE_VERTYPE) {\r
+    printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);\r
+    goto done;\r
+  }\r
+  session = ntohs(ph->session);\r
+  plen = ntohs(ph->plen);\r
+  off += sizeof(*ph);\r
+\r
+  if (plen + off > pb->len) {\r
+    printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",\r
+        pb->len - off, plen);\r
+    goto done;\r
+  }\r
+  if(pb->tot_len == pb->len) {\r
+    pb->tot_len = pb->len = off + plen; /* ignore trailing garbage */\r
+  }\r
+  tag = 0;\r
+  len = 0;\r
+  sc = NULL;\r
+  while (off + sizeof(pt) <= pb->len) {\r
+    MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));\r
+    tag = ntohs(pt.tag);\r
+    len = ntohs(pt.len);\r
+    if (off + sizeof(pt) + len > pb->len) {\r
+      printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);\r
+      goto done;\r
+    }\r
+    switch (tag) {\r
+      case PPPOE_TAG_EOL:\r
+        goto breakbreak;\r
+      case PPPOE_TAG_SNAME:\r
+        break;  /* ignored */\r
+      case PPPOE_TAG_ACNAME:\r
+        break;  /* ignored */\r
+      case PPPOE_TAG_HUNIQUE:\r
+        if (sc != NULL) {\r
+          break;\r
+        }\r
+#ifdef PPPOE_SERVER\r
+        hunique = (u8_t*)pb->payload + off + sizeof(pt);\r
+        hunique_len = len;\r
+#endif\r
+        sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);\r
+        if (sc != NULL) {\r
+          snprintf(devname, sizeof(devname), "%c%c%"U16_F, sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+        }\r
+        break;\r
+      case PPPOE_TAG_ACCOOKIE:\r
+        if (ac_cookie == NULL) {\r
+          ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);\r
+          ac_cookie_len = len;\r
+        }\r
+        break;\r
+      case PPPOE_TAG_SNAME_ERR:\r
+        err_msg = "SERVICE NAME ERROR";\r
+        errortag = 1;\r
+        break;\r
+      case PPPOE_TAG_ACSYS_ERR:\r
+        err_msg = "AC SYSTEM ERROR";\r
+        errortag = 1;\r
+        break;\r
+      case PPPOE_TAG_GENERIC_ERR:\r
+        err_msg = "GENERIC ERROR";\r
+        errortag = 1;\r
+        break;\r
+    }\r
+    if (err_msg) {\r
+      error = NULL;\r
+      if (errortag && len) {\r
+        error = mem_malloc(len+1);\r
+        if (error) {\r
+          strncpy(error, (char*)pb->payload + off + sizeof(pt), len);\r
+          error[len-1] = '\0';\r
+        }\r
+      }\r
+      if (error) {\r
+        printf("%s: %s: %s\n", devname, err_msg, error);\r
+        mem_free(error);\r
+      } else {\r
+        printf("%s: %s\n", devname, err_msg);\r
+      }\r
+      if (errortag) {\r
+        goto done;\r
+      }\r
+    }\r
+    off += sizeof(pt) + len;\r
+  }\r
+\r
+breakbreak:;\r
+  switch (ph->code) {\r
+    case PPPOE_CODE_PADI:\r
+#ifdef PPPOE_SERVER\r
+      /*\r
+       * got service name, concentrator name, and/or host unique.\r
+       * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.\r
+       */\r
+      if (LIST_EMPTY(&pppoe_softc_list)) {\r
+        goto done;\r
+      }\r
+      LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
+        if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {\r
+          continue;\r
+        }\r
+        if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {\r
+          continue;\r
+        }\r
+        if (sc->sc_state == PPPOE_STATE_INITIAL) {\r
+          break;\r
+        }\r
+      }\r
+      if (sc == NULL) {\r
+        /* printf("pppoe: free passive interface is not found\n"); */\r
+        goto done;\r
+      }\r
+      if (hunique) {\r
+        if (sc->sc_hunique) {\r
+          mem_free(sc->sc_hunique);\r
+        }\r
+        sc->sc_hunique = mem_malloc(hunique_len);\r
+        if (sc->sc_hunique == NULL) {\r
+          goto done;\r
+        }\r
+        sc->sc_hunique_len = hunique_len;\r
+        MEMCPY(sc->sc_hunique, hunique, hunique_len);\r
+      }\r
+      MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);\r
+      sc->sc_state = PPPOE_STATE_PADO_SENT;\r
+      pppoe_send_pado(sc);\r
+      break;\r
+  #endif /* PPPOE_SERVER */\r
+    case PPPOE_CODE_PADR:\r
+  #ifdef PPPOE_SERVER\r
+      /*\r
+       * get sc from ac_cookie if IFF_PASSIVE\r
+       */\r
+      if (ac_cookie == NULL) {\r
+        /* be quiet if there is not a single pppoe instance */\r
+        printf("pppoe: received PADR but not includes ac_cookie\n");\r
+        goto done;\r
+      }\r
+      sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);\r
+      if (sc == NULL) {\r
+        /* be quiet if there is not a single pppoe instance */\r
+        if (!LIST_EMPTY(&pppoe_softc_list)) {\r
+          printf("pppoe: received PADR but could not find request for it\n");\r
+        }\r
+        goto done;\r
+      }\r
+      if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
+        printf("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+        goto done;\r
+      }\r
+      if (hunique) {\r
+        if (sc->sc_hunique) {\r
+          mem_free(sc->sc_hunique);\r
+        }\r
+        sc->sc_hunique = mem_malloc(hunique_len);\r
+        if (sc->sc_hunique == NULL) {\r
+          goto done;\r
+        }\r
+        sc->sc_hunique_len = hunique_len;\r
+        MEMCPY(sc->sc_hunique, hunique, hunique_len);\r
+      }\r
+      pppoe_send_pads(sc);\r
+      sc->sc_state = PPPOE_STATE_SESSION;\r
+      tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */\r
+      break;\r
+  #else\r
+      /* ignore, we are no access concentrator */\r
+      goto done;\r
+  #endif /* PPPOE_SERVER */\r
+    case PPPOE_CODE_PADO:\r
+      if (sc == NULL) {\r
+        /* be quiet if there is not a single pppoe instance */\r
+        if (!LIST_EMPTY(&pppoe_softc_list)) {\r
+          printf("pppoe: received PADO but could not find request for it\n");\r
+        }\r
+        goto done;\r
+      }\r
+      if (sc->sc_state != PPPOE_STATE_PADI_SENT) {\r
+        printf("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+        goto done;\r
+      }\r
+      if (ac_cookie) {\r
+        if (sc->sc_ac_cookie) {\r
+          mem_free(sc->sc_ac_cookie);\r
+        }\r
+        sc->sc_ac_cookie = mem_malloc(ac_cookie_len);\r
+        if (sc->sc_ac_cookie == NULL) {\r
+          goto done;\r
+        }\r
+        sc->sc_ac_cookie_len = ac_cookie_len;\r
+        MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);\r
+      }\r
+      MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));\r
+      tcpip_untimeout(pppoe_timeout, sc);\r
+      sc->sc_padr_retried = 0;\r
+      sc->sc_state = PPPOE_STATE_PADR_SENT;\r
+      if ((err = pppoe_send_padr(sc)) != 0) {\r
+        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));\r
+      }\r
+      tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);\r
+      break;\r
+    case PPPOE_CODE_PADS:\r
+      if (sc == NULL) {\r
+        goto done;\r
+      }\r
+      sc->sc_session = session;\r
+      tcpip_untimeout(pppoe_timeout, sc);\r
+      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));\r
+      sc->sc_state = PPPOE_STATE_SESSION;\r
+      tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */\r
+      break;\r
+    case PPPOE_CODE_PADT:\r
+      if (sc == NULL) {\r
+        goto done;\r
+      }\r
+      pppoe_clear_softc(sc, "received PADT");\r
+      break;\r
+    default:\r
+      if(sc) {\r
+        printf("%c%c%"U16_F": unknown code (0x%04x) session = 0x%04x\n",\r
+            sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,\r
+            ph->code, session);\r
+      } else {\r
+        printf("pppoe: unknown code (0x%04x) session = 0x%04x\n", ph->code, session);\r
+      }\r
+      break;\r
+  }\r
+\r
+done:\r
+  pbuf_free(pb);\r
+  return;\r
+}\r
+\r
+void\r
+pppoe_disc_input(struct netif *netif, struct pbuf *p)\r
+{\r
+  /* avoid error messages if there is not a single pppoe instance */\r
+  if (!LIST_EMPTY(&pppoe_softc_list)) {\r
+    pppoe_dispatch_disc_pkt(netif, p);\r
+  } else {\r
+    pbuf_free(p);\r
+  }\r
+}\r
+\r
+void\r
+pppoe_data_input(struct netif *netif, struct pbuf *pb)\r
+{\r
+  u16_t session, plen;\r
+  struct pppoe_softc *sc;\r
+  struct pppoehdr *ph;\r
+#ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
+  u8_t shost[ETHER_ADDR_LEN];\r
+#endif\r
+\r
+#ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
+  MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));\r
+#endif\r
+  if (pbuf_header(pb, -(int)sizeof(struct eth_hdr)) != 0) {\r
+    /* bail out */\r
+    PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header failed\n"));\r
+    LINK_STATS_INC(link.lenerr);\r
+    goto drop;\r
+  } \r
+\r
+  pb = pppSingleBuf (pb);\r
+\r
+  if (pb->len <= PPPOE_HEADERLEN) {\r
+    printf("pppoe (data): dropping too short packet: %d bytes\n", pb->len);\r
+    goto drop;\r
+  }\r
+\r
+  if (pb->len < sizeof(*ph)) {\r
+    printf("pppoe_data_input: could not get PPPoE header\n");\r
+    goto drop;\r
+  }\r
+  ph = (struct pppoehdr *)pb->payload;\r
+\r
+  if (ph->vertype != PPPOE_VERTYPE) {\r
+    printf("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype);\r
+    goto drop;\r
+  }\r
+  if (ph->code != 0) {\r
+    goto drop;\r
+  }\r
+\r
+  session = ntohs(ph->session);\r
+  sc = pppoe_find_softc_by_session(session, netif);\r
+  if (sc == NULL) {\r
+#ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
+    printf("pppoe: input for unknown session 0x%x, sending PADT\n", session);\r
+    pppoe_send_padt(netif, session, shost);\r
+#endif\r
+    goto drop;\r
+  }\r
+\r
+  plen = ntohs(ph->plen);\r
+\r
+  if (pbuf_header(pb, -(int)(PPPOE_HEADERLEN)) != 0) {\r
+    /* bail out */\r
+    PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));\r
+    LINK_STATS_INC(link.lenerr);\r
+    goto drop;\r
+  } \r
+\r
+  PPPDEBUG((LOG_DEBUG, "pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",\r
+        sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,\r
+        pb->len, plen));\r
+\r
+  if (pb->len < plen) {\r
+    goto drop;\r
+  }\r
+\r
+  pppInProcOverEthernet(sc->sc_pd, pb);\r
+\r
+  return;\r
+\r
+drop:\r
+  pbuf_free(pb);\r
+}\r
+\r
+static err_t\r
+pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)\r
+{\r
+  struct eth_hdr *ethhdr;\r
+  u16_t etype;\r
+  err_t res;\r
+\r
+  if (!sc->sc_ethif) {\r
+    pbuf_free(pb);\r
+    return ERR_IF;\r
+  }\r
+\r
+  ethhdr = (struct eth_hdr *)pb->payload;\r
+  etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;\r
+  ethhdr->type = htons(etype);\r
+  MEMCPY(ethhdr->dest.addr, sc->sc_dest.addr, sizeof(ethhdr->dest.addr));\r
+  MEMCPY(ethhdr->src.addr, ((struct eth_addr *)sc->sc_ethif->hwaddr)->addr, sizeof(ethhdr->src.addr));\r
+\r
+  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",\r
+      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,\r
+      sc->sc_state, sc->sc_session,\r
+      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],\r
+      pb->tot_len));\r
+\r
+  res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);\r
+\r
+  pbuf_free(pb);\r
+\r
+  return res;\r
+}\r
+\r
+static err_t\r
+pppoe_send_padi(struct pppoe_softc *sc)\r
+{\r
+  struct pbuf *pb;\r
+  u8_t *p;\r
+  int len, l1 = 0, l2 = 0; /* XXX: gcc */\r
+\r
+  if (sc->sc_state >PPPOE_STATE_PADI_SENT) {\r
+    PPPDEBUG((LOG_ERR, "ERROR: pppoe_send_padi in state %d", sc->sc_state));\r
+  }\r
+\r
+  /* calculate length of frame (excluding ethernet header + pppoe header) */\r
+  len = 2 + 2 + 2 + 2 + sizeof sc;  /* service name tag is required, host unique is send too */\r
+  if (sc->sc_service_name != NULL) {\r
+    l1 = strlen(sc->sc_service_name);\r
+    len += l1;\r
+  }\r
+  if (sc->sc_concentrator_name != NULL) {\r
+    l2 = strlen(sc->sc_concentrator_name);\r
+    len += 2 + 2 + l2;\r
+  }\r
+\r
+  /* allocate a buffer */\r
+  pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
+  if (!pb) {\r
+    return ERR_MEM;\r
+  }\r
+\r
+  p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
+  /* fill in pkt */\r
+  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);\r
+  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
+  if (sc->sc_service_name != NULL) {\r
+    PPPOE_ADD_16(p, l1);\r
+    MEMCPY(p, sc->sc_service_name, l1);\r
+    p += l1;\r
+  } else {\r
+    PPPOE_ADD_16(p, 0);\r
+  }\r
+  if (sc->sc_concentrator_name != NULL) {\r
+    PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);\r
+    PPPOE_ADD_16(p, l2);\r
+    MEMCPY(p, sc->sc_concentrator_name, l2);\r
+    p += l2;\r
+  }\r
+  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
+  PPPOE_ADD_16(p, sizeof(sc));\r
+  MEMCPY(p, &sc, sizeof sc);\r
+\r
+  /* send pkt */\r
+  return pppoe_output(sc, pb);\r
+}\r
+\r
+static void\r
+pppoe_timeout(void *arg)\r
+{\r
+  int retry_wait, err;\r
+  struct pppoe_softc *sc = (struct pppoe_softc*)arg;\r
+\r
+  PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));\r
+\r
+  switch (sc->sc_state) {\r
+    case PPPOE_STATE_PADI_SENT:\r
+      /*\r
+       * We have two basic ways of retrying:\r
+       *  - Quick retry mode: try a few times in short sequence\r
+       *  - Slow retry mode: we already had a connection successfully\r
+       *    established and will try infinitely (without user\r
+       *    intervention)\r
+       * We only enter slow retry mode if IFF_LINK1 (aka autodial)\r
+       * is not set.\r
+       */\r
+\r
+      /* initialize for quick retry mode */\r
+      retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);\r
+\r
+      sc->sc_padi_retried++;\r
+      if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {\r
+#if 0\r
+        if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {\r
+          /* slow retry mode */\r
+          retry_wait = PPPOE_SLOW_RETRY;\r
+        } else\r
+#endif\r
+        {\r
+          pppoe_abort_connect(sc);\r
+          return;\r
+        }\r
+      }\r
+      if ((err = pppoe_send_padi(sc)) != 0) {\r
+        sc->sc_padi_retried--;\r
+        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));\r
+      }\r
+      tcpip_timeout(retry_wait, pppoe_timeout, sc);\r
+      break;\r
+\r
+    case PPPOE_STATE_PADR_SENT:\r
+      sc->sc_padr_retried++;\r
+      if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {\r
+        MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
+        sc->sc_state = PPPOE_STATE_PADI_SENT;\r
+        sc->sc_padr_retried = 0;\r
+        if ((err = pppoe_send_padi(sc)) != 0) {\r
+          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));\r
+        }\r
+        tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);\r
+        return;\r
+      }\r
+      if ((err = pppoe_send_padr(sc)) != 0) {\r
+        sc->sc_padr_retried--;\r
+        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));\r
+      }\r
+      tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);\r
+      break;\r
+    case PPPOE_STATE_CLOSING:\r
+      pppoe_do_disconnect(sc);\r
+      break;\r
+    default:\r
+      return;  /* all done, work in peace */\r
+  }\r
+}\r
+\r
+/* Start a connection (i.e. initiate discovery phase) */\r
+int\r
+pppoe_connect(struct pppoe_softc *sc)\r
+{\r
+  int err;\r
+\r
+  if (sc->sc_state != PPPOE_STATE_INITIAL) {\r
+    return EBUSY;\r
+  }\r
+\r
+#ifdef PPPOE_SERVER\r
+  /* wait PADI if IFF_PASSIVE */\r
+  if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {\r
+    return 0;\r
+  }\r
+#endif\r
+  /* save state, in case we fail to send PADI */\r
+  sc->sc_state = PPPOE_STATE_PADI_SENT;\r
+  sc->sc_padr_retried = 0;\r
+  err = pppoe_send_padi(sc);\r
+  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));\r
+  tcpip_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);\r
+  return err;\r
+}\r
+\r
+/* disconnect */\r
+void\r
+pppoe_disconnect(struct pppoe_softc *sc)\r
+{\r
+  if (sc->sc_state < PPPOE_STATE_SESSION) {\r
+    return;\r
+  }\r
+  /*\r
+   * Do not call pppoe_disconnect here, the upper layer state\r
+   * machine gets confused by this. We must return from this\r
+   * function and defer disconnecting to the timeout handler.\r
+   */\r
+  sc->sc_state = PPPOE_STATE_CLOSING;\r
+  tcpip_timeout(20, pppoe_timeout, sc);\r
+}\r
+\r
+static int\r
+pppoe_do_disconnect(struct pppoe_softc *sc)\r
+{\r
+  int err;\r
+\r
+  if (sc->sc_state < PPPOE_STATE_SESSION) {\r
+    err = EBUSY;\r
+  } else {\r
+    PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));\r
+    err = pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);\r
+  }\r
+\r
+  /* cleanup softc */\r
+  sc->sc_state = PPPOE_STATE_INITIAL;\r
+  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
+  if (sc->sc_ac_cookie) {\r
+    mem_free(sc->sc_ac_cookie);\r
+    sc->sc_ac_cookie = NULL;\r
+  }\r
+  sc->sc_ac_cookie_len = 0;\r
+#ifdef PPPOE_SERVER\r
+  if (sc->sc_hunique) {\r
+    mem_free(sc->sc_hunique);\r
+    sc->sc_hunique = NULL;\r
+  }\r
+  sc->sc_hunique_len = 0;\r
+#endif\r
+  sc->sc_session = 0;\r
+\r
+  sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */\r
+\r
+  return err;\r
+}\r
+\r
+/* Connection attempt aborted */\r
+static void\r
+pppoe_abort_connect(struct pppoe_softc *sc)\r
+{\r
+  printf("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+  sc->sc_state = PPPOE_STATE_CLOSING;\r
+\r
+  sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */\r
+\r
+  /* clear connection state */\r
+  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
+  sc->sc_state = PPPOE_STATE_INITIAL;\r
+}\r
+\r
+/* Send a PADR packet */\r
+static err_t\r
+pppoe_send_padr(struct pppoe_softc *sc)\r
+{\r
+  struct pbuf *pb;\r
+  u8_t *p;\r
+  size_t len, l1 = 0; /* XXX: gcc */\r
+\r
+  if (sc->sc_state != PPPOE_STATE_PADR_SENT) {\r
+    return ERR_CONN;\r
+  }\r
+\r
+  len = 2 + 2 + 2 + 2 + sizeof(sc);    /* service name, host unique */\r
+  if (sc->sc_service_name != NULL) {    /* service name tag maybe empty */\r
+    l1 = strlen(sc->sc_service_name);\r
+    len += l1;\r
+  }\r
+  if (sc->sc_ac_cookie_len > 0) {\r
+    len += 2 + 2 + sc->sc_ac_cookie_len;  /* AC cookie */\r
+  }\r
+  pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
+  if (!pb) {\r
+    return ERR_MEM;\r
+  }\r
+  p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
+  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);\r
+  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
+  if (sc->sc_service_name != NULL) {\r
+    PPPOE_ADD_16(p, l1);\r
+    MEMCPY(p, sc->sc_service_name, l1);\r
+    p += l1;\r
+  } else {\r
+    PPPOE_ADD_16(p, 0);\r
+  }\r
+  if (sc->sc_ac_cookie_len > 0) {\r
+    PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);\r
+    PPPOE_ADD_16(p, sc->sc_ac_cookie_len);\r
+    MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);\r
+    p += sc->sc_ac_cookie_len;\r
+  }\r
+  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
+  PPPOE_ADD_16(p, sizeof(sc));\r
+  MEMCPY(p, &sc, sizeof sc);\r
+\r
+  return pppoe_output(sc, pb);\r
+}\r
+\r
+/* send a PADT packet */\r
+static err_t\r
+pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)\r
+{\r
+  struct pbuf *pb;\r
+  struct eth_hdr *ethhdr;\r
+  err_t res;\r
+  u8_t *p;\r
+\r
+  pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN, PBUF_RAM);\r
+  if (!pb) {\r
+    return ERR_MEM;\r
+  }\r
+\r
+  ethhdr = (struct eth_hdr *)pb->payload;\r
+  ethhdr->type = htons(ETHTYPE_PPPOEDISC);\r
+  MEMCPY(ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));\r
+  MEMCPY(ethhdr->src.addr, ((struct eth_addr *)outgoing_if->hwaddr)->addr, sizeof(ethhdr->src.addr));\r
+\r
+  p = (u8_t*)(ethhdr + 1);\r
+  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);\r
+\r
+  res = outgoing_if->linkoutput(outgoing_if, pb);\r
+\r
+  pbuf_free(pb);\r
+\r
+  return res;\r
+}\r
+\r
+#ifdef PPPOE_SERVER\r
+static err_t\r
+pppoe_send_pado(struct pppoe_softc *sc)\r
+{\r
+  struct pbuf *pb;\r
+  u8_t *p;\r
+  size_t len;\r
+\r
+  if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
+    return ERR_CONN;\r
+  }\r
+\r
+  /* calc length */\r
+  len = 0;\r
+  /* include ac_cookie */\r
+  len += 2 + 2 + sizeof(sc);\r
+  /* include hunique */\r
+  len += 2 + 2 + sc->sc_hunique_len;\r
+  pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
+  if (!pb) {\r
+    return ERR_MEM;\r
+  }\r
+  p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
+  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);\r
+  PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);\r
+  PPPOE_ADD_16(p, sizeof(sc));\r
+  MEMCPY(p, &sc, sizeof(sc));\r
+  p += sizeof(sc);\r
+  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
+  PPPOE_ADD_16(p, sc->sc_hunique_len);\r
+  MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);\r
+  return pppoe_output(sc, pb);\r
+}\r
+\r
+static err_t\r
+pppoe_send_pads(struct pppoe_softc *sc)\r
+{\r
+  struct pbuf *pb;\r
+  u8_t *p;\r
+  size_t len, l1 = 0;  /* XXX: gcc */\r
+\r
+  if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
+    return ERR_CONN;\r
+  }\r
+\r
+  sc->sc_session = mono_time.tv_sec % 0xff + 1;\r
+  /* calc length */\r
+  len = 0;\r
+  /* include hunique */\r
+  len += 2 + 2 + 2 + 2 + sc->sc_hunique_len;  /* service name, host unique*/\r
+  if (sc->sc_service_name != NULL) {    /* service name tag maybe empty */\r
+    l1 = strlen(sc->sc_service_name);\r
+    len += l1;\r
+  }\r
+  pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
+  if (!pb) {\r
+    return ERR_MEM;\r
+  }\r
+  p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
+  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);\r
+  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
+  if (sc->sc_service_name != NULL) {\r
+    PPPOE_ADD_16(p, l1);\r
+    MEMCPY(p, sc->sc_service_name, l1);\r
+    p += l1;\r
+  } else {\r
+    PPPOE_ADD_16(p, 0);\r
+  }\r
+  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
+  PPPOE_ADD_16(p, sc->sc_hunique_len);\r
+  MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);\r
+  return pppoe_output(sc, pb);\r
+}\r
+#endif\r
+\r
+err_t\r
+pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)\r
+{\r
+  u8_t *p;\r
+  size_t len;\r
+\r
+  /* are we ready to process data yet? */\r
+  if (sc->sc_state < PPPOE_STATE_SESSION) {\r
+    /*sppp_flush(&sc->sc_sppp.pp_if);*/\r
+    pbuf_free(pb);\r
+    return ERR_CONN;\r
+  }\r
+\r
+  len = pb->tot_len;\r
+\r
+  /* make room for Ethernet header - should not fail */\r
+  if (pbuf_header(pb, sizeof(struct eth_hdr) + PPPOE_HEADERLEN) != 0) {\r
+    /* bail out */\r
+    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));\r
+    LINK_STATS_INC(link.lenerr);\r
+    pbuf_free(pb);\r
+    return ERR_BUF;\r
+  } \r
+\r
+  p = (u8_t*)pb->payload + sizeof(struct eth_hdr);\r
+  PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);\r
+\r
+  return pppoe_output(sc, pb);\r
+}\r
+\r
+#if 0 /*def PFIL_HOOKS*/\r
+static int\r
+pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)\r
+{\r
+  struct pppoe_softc *sc;\r
+  int s;\r
+\r
+  if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {\r
+    return 0;\r
+  }\r
+\r
+  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
+    if (sc->sc_ethif != ifp) {\r
+      continue;\r
+    }\r
+    if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {\r
+      sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);\r
+      printf("%c%c%"U16_F": ethernet interface detached, going down\n",\r
+          sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
+    }\r
+    sc->sc_ethif = NULL;\r
+    pppoe_clear_softc(sc, "ethernet interface detached");\r
+  }\r
+\r
+  return 0;\r
+}\r
+#endif\r
+\r
+static void\r
+pppoe_clear_softc(struct pppoe_softc *sc, const char *message)\r
+{\r
+  LWIP_UNUSED_ARG(message);\r
+\r
+  /* stop timer */\r
+  tcpip_untimeout(pppoe_timeout, sc);\r
+  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));\r
+\r
+  /* fix our state */\r
+  sc->sc_state = PPPOE_STATE_INITIAL;\r
+\r
+  /* notify upper layers */\r
+  sc->sc_linkStatusCB(sc->sc_pd, 0);\r
+\r
+  /* clean up softc */\r
+  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
+  if (sc->sc_ac_cookie) {\r
+    mem_free(sc->sc_ac_cookie);\r
+    sc->sc_ac_cookie = NULL;\r
+  }\r
+  sc->sc_ac_cookie_len = 0;\r
+  sc->sc_session = 0;\r
+}\r
+\r
+#endif /* PPPOE_SUPPORT */\r
+\r