Răsfoiți Sursa

adapt for rt-thread lwip 1.4.1

Meco Man 3 ani în urmă
părinte
comite
c6dfd2e33e

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+Porting network interface device for RT-Thread in lwIP.
+The major jobs following RT-Thread Team. The RT-Thread network interface device need to synchronize some network status and address information in lwIP, so it need to make some changes in the lwIP netwrok status and address operations function.
+The specific changes are as follows:
+
+ - netif.c: add RT-Thread netdev header file , status synchronize(UP, LINK_UP), address synchronize(IP, netmask, gateway);
+
+ - dns.c: add RT-Thread header file, dns servers synchronize;
+
+ - sockets.c: custom 'select' function implementation in RT-Thread by the wait queue mode.
+
+by ChenYong 2019/3/26 10:00 AM
+chenyong@rt-thread.com
+

+ 83 - 0
SConscript

@@ -0,0 +1,83 @@
+Import('RTT_ROOT')
+from building import *
+
+src = Split("""
+src/api/api_lib.c
+src/api/api_msg.c
+src/api/err.c
+src/api/netbuf.c
+src/api/netdb.c
+src/api/netifapi.c
+src/api/sockets.c
+src/api/tcpip.c
+src/core/def.c
+src/core/dhcp.c
+src/core/dns.c
+src/core/init.c
+src/core/memp.c
+src/core/netif.c
+src/core/pbuf.c
+src/core/raw.c
+src/core/stats.c
+src/core/sys.c
+src/core/tcp.c
+src/core/tcp_in.c
+src/core/tcp_out.c
+src/core/timers.c
+src/core/udp.c
+src/core/ipv4/autoip.c
+src/core/ipv4/icmp.c
+src/core/ipv4/igmp.c
+src/core/ipv4/inet.c
+src/core/ipv4/inet_chksum.c
+src/core/ipv4/ip.c
+src/core/ipv4/ip_addr.c
+src/core/ipv4/ip_frag.c
+src/netif/etharp.c
+src/netif/slipif.c
+""")
+
+snmp_src = Split("""
+src/core/snmp/asn1_dec.c
+src/core/snmp/asn1_enc.c
+src/core/snmp/mib2.c
+src/core/snmp/mib_structs.c
+src/core/snmp/msg_in.c
+src/core/snmp/msg_out.c
+""")
+
+ppp_src = Split("""
+src/netif/ppp/auth.c
+src/netif/ppp/chap.c
+src/netif/ppp/chpms.c
+src/netif/ppp/fsm.c
+src/netif/ppp/ipcp.c
+src/netif/ppp/lcp.c
+src/netif/ppp/magic.c
+src/netif/ppp/md5.c
+src/netif/ppp/pap.c
+src/netif/ppp/ppp.c
+src/netif/ppp/ppp_oe.c
+src/netif/ppp/randm.c
+src/netif/ppp/vj.c
+""")
+
+# The set of source files associated with this SConscript file.
+path = [GetCurrentDir() + '/src/include',
+    GetCurrentDir() + '/src/include/ipv4',
+    GetCurrentDir() + '/src/include/netif']
+
+if GetDepend(['RT_LWIP_SNMP']):
+    src += snmp_src
+
+if GetDepend(['RT_LWIP_PPP']):
+    src += ppp_src
+    path += [GetCurrentDir() + '/src/netif/ppp']
+
+# For testing apps
+if GetDepend(['RT_LWIP_USING_PING']):
+    src += Glob('src/apps/ping/ping.c')
+
+group = DefineGroup('lwIP', src, depend = ['RT_USING_LWIP', 'RT_USING_LWIP141'], CPPPATH = path)
+
+Return('group')

+ 1 - 0
src/api/api_msg.c

@@ -1235,6 +1235,7 @@ do_writemore(struct netconn *conn)
       /* partial write */
       err = ERR_OK;
       conn->current_msg->msg.w.len = conn->write_offset;
+      conn->write_offset = 0;
     }
   } else
 #endif /* LWIP_SO_SNDTIMEO */

+ 14 - 1
src/api/sockets.c

@@ -231,6 +231,18 @@ tryget_socket(int s)
   return &sockets[s];
 }
 
+/**
+ * Same as tryget_socket but a global routine.
+ *
+ * @param s externally used socket index
+ * @return struct lwip_sock for the socket or NULL if not found
+ */
+struct lwip_sock *
+lwip_tryget_socket(int s)
+{
+  return tryget_socket(s);
+}
+
 /**
  * Allocate a new socket for a given netconn.
  *
@@ -380,7 +392,8 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
     return -1;
   }
   LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
-  LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback);
+  /* RT-Thread has changed callback when using BSD socket API, so remove this assert. */
+  /* LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback); */
   nsock = &sockets[newsock];
 
   /* See event_callback: If data comes in right away after an accept, even

+ 245 - 0
src/apps/ping/ping.c

@@ -0,0 +1,245 @@
+/*
+ * netutils: ping implementation
+ */
+
+#include <rtthread.h>
+
+#ifdef RT_LWIP_ICMP    /* don't build if not configured for use in rtconfig.h */
+#include <lwip/opt.h>
+#include <lwip/init.h>
+#include <lwip/mem.h>
+#include <lwip/icmp.h>
+#include <lwip/netif.h>
+#include <lwip/sys.h>
+#include <lwip/inet.h>
+#include <lwip/inet_chksum.h>
+#include <lwip/ip.h>
+#include <lwip/netdb.h>
+#include <lwip/sockets.h>
+
+/**
+ * PING_DEBUG: Enable debugging for PING.
+ */
+#ifndef PING_DEBUG
+#define PING_DEBUG     LWIP_DBG_ON
+#endif
+
+/** ping receive timeout - in milliseconds */
+#define PING_RCV_TIMEO (2 * RT_TICK_PER_SECOND)
+/** ping delay - in milliseconds */
+#define PING_DELAY     (1 * RT_TICK_PER_SECOND)
+
+/** ping identifier - must fit on a u16_t */
+#ifndef PING_ID
+#define PING_ID        0xAFAF
+#endif
+
+/** ping additional data size to include in the packet */
+#ifndef PING_DATA_SIZE
+#define PING_DATA_SIZE 32
+#endif
+
+/* ping variables */
+static u16_t ping_seq_num;
+struct _ip_addr
+{
+    rt_uint8_t addr0, addr1, addr2, addr3;
+};
+
+/** Prepare a echo ICMP request */
+static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
+{
+    size_t i;
+    size_t data_len = len - sizeof(struct icmp_echo_hdr);
+
+    ICMPH_TYPE_SET(iecho, ICMP_ECHO);
+    ICMPH_CODE_SET(iecho, 0);
+    iecho->chksum = 0;
+    iecho->id     = PING_ID;
+    iecho->seqno  = htons(++ping_seq_num);
+
+    /* fill the additional data buffer with some data */
+    for (i = 0; i < data_len; i++)
+    {
+        ((char*) iecho)[sizeof(struct icmp_echo_hdr) + i] = (char) i;
+    }
+
+#ifdef RT_LWIP_USING_HW_CHECKSUM
+      iecho->chksum = 0;
+#else
+      iecho->chksum = inet_chksum(iecho, len);
+#endif
+
+}
+
+/* Ping using the socket ip */
+err_t lwip_ping_send(int s, ip_addr_t *addr, int size)
+{
+    int err;
+    struct icmp_echo_hdr *iecho;
+    struct sockaddr_in to;
+    int ping_size = sizeof(struct icmp_echo_hdr) + size;
+    LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
+
+    iecho = rt_malloc(ping_size);
+    if (iecho == RT_NULL)
+    {
+        return ERR_MEM;
+    }
+
+    ping_prepare_echo(iecho, (u16_t) ping_size);
+
+    to.sin_len = sizeof(to);
+    to.sin_family = AF_INET;
+#if LWIP_IPV4 && LWIP_IPV6
+    to.sin_addr.s_addr = addr->u_addr.ip4.addr;
+#elif LWIP_IPV4
+    to.sin_addr.s_addr = addr->addr;
+#elif LWIP_IPV6
+#error Not supported IPv6.
+#endif
+
+    err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
+    rt_free(iecho);
+
+    return (err == ping_size ? ERR_OK : ERR_VAL);
+}
+
+int lwip_ping_recv(int s, int *ttl)
+{
+    char buf[64];
+    int fromlen = sizeof(struct sockaddr_in), len;
+    struct sockaddr_in from;
+    struct ip_hdr *iphdr;
+    struct icmp_echo_hdr *iecho;
+
+    while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
+    {
+        if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
+        {
+            iphdr = (struct ip_hdr *) buf;
+            iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
+            if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
+            {
+                *ttl = iphdr->_ttl;
+                return len;
+            }
+        }
+    }
+
+    return len;
+}
+
+#ifndef RT_USING_NETDEV
+
+/* using the lwIP custom ping */
+rt_err_t ping(char* target_name, rt_uint32_t times, rt_size_t size)
+{
+#if LWIP_VERSION_MAJOR >= 2U
+    struct timeval timeout = { PING_RCV_TIMEO / RT_TICK_PER_SECOND, PING_RCV_TIMEO % RT_TICK_PER_SECOND };
+#else
+    int timeout = PING_RCV_TIMEO * 1000UL / RT_TICK_PER_SECOND;
+#endif
+
+    int s, ttl, recv_len;
+    ip_addr_t target_addr;
+    rt_uint32_t send_times;
+    rt_tick_t recv_start_tick;
+    struct addrinfo hint, *res = NULL;
+    struct sockaddr_in *h = NULL;
+    struct in_addr ina;
+
+    send_times = 0;
+    ping_seq_num = 0;
+
+    if (size == 0)
+    {
+        size = PING_DATA_SIZE;
+    }
+
+    memset(&hint, 0, sizeof(hint));
+    /* convert URL to IP */
+    if (lwip_getaddrinfo(target_name, NULL, &hint, &res) != 0)
+    {
+        rt_kprintf("ping: unknown host %s\n", target_name);
+        return -RT_ERROR;
+    }
+    memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
+    memcpy(&ina, &h->sin_addr, sizeof(ina));
+    lwip_freeaddrinfo(res);
+    if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
+    {
+        rt_kprintf("ping: unknown host %s\n", target_name);
+        return -RT_ERROR;
+    }
+    /* new a socket */
+    if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
+    {
+        rt_kprintf("ping: create socket failed\n");
+        return -RT_ERROR;
+    }
+
+    lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
+
+    while (1)
+    {
+        int elapsed_time;
+
+        if (lwip_ping_send(s, &target_addr, size) == ERR_OK)
+        {
+            recv_start_tick = rt_tick_get();
+            if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
+            {
+                elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
+                rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), send_times,
+                        ttl, elapsed_time);
+            }
+            else
+            {
+                rt_kprintf("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), send_times);
+            }
+        }
+        else
+        {
+            rt_kprintf("Send %s - error\n", inet_ntoa(ina));
+        }
+
+        send_times++;
+        if (send_times >= times)
+        {
+            /* send ping times reached, stop */
+            break;
+        }
+
+        rt_thread_delay(PING_DELAY); /* take a delay */
+    }
+
+    lwip_close(s);
+
+    return RT_EOK;
+}
+#ifdef RT_USING_FINSH
+#include <finsh.h>
+
+FINSH_FUNCTION_EXPORT(ping, ping network host);
+
+int cmd_ping(int argc, char **argv)
+{
+    if (argc == 1)
+    {
+        rt_kprintf("Please input: ping <host address>\n");
+    }
+    else
+    {
+        ping(argv[1], 4, 0);
+    }
+
+    return 0;
+}
+MSH_CMD_EXPORT_ALIAS(cmd_ping, ping, ping network host);
+#endif /* RT_USING_FINSH */
+
+#endif /* RT_USING_NETDEV */
+
+#endif /* RT_LWIP_ICMP */
+

+ 14 - 0
src/core/dns.c

@@ -83,6 +83,8 @@
 
 #include <string.h>
 
+#include <rtthread.h>
+
 /** DNS server IP address */
 #ifndef DNS_SERVER_ADDRESS
 #define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
@@ -275,6 +277,18 @@ dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
   if ((numdns < DNS_MAX_SERVERS) && (dns_pcb != NULL) &&
       (dnsserver != NULL) && !ip_addr_isany(dnsserver)) {
     dns_servers[numdns] = (*dnsserver);
+    
+#ifdef RT_USING_NETDEV
+      extern struct netif *netif_list;
+      extern struct netdev *netdev_get_by_name(const char *name);
+      extern void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server);
+      struct netif *netif = NULL;
+
+      /* set network interface device DNS server address */
+      for (netif = netif_list; netif != NULL; netif = netif->next) {
+        netdev_low_level_set_dns_server(netdev_get_by_name(netif->name), numdns, dnsserver);
+      }
+#endif /* RT_USING_NETDEV */
   }
 }
 

+ 32 - 5
src/core/ipv4/ip.c

@@ -312,6 +312,11 @@ ip_input(struct pbuf *p, struct netif *inp)
   int check_ip_src=1;
 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
 
+#if IP_NAT
+  extern u8_t ip_nat_input(struct pbuf *p);
+  extern u8_t ip_nat_out(struct pbuf *p);
+#endif
+
   IP_STATS_INC(ip.recv);
   snmp_inc_ipinreceives();
 
@@ -487,15 +492,30 @@ ip_input(struct pbuf *p, struct netif *inp)
 
   /* packet not for us? */
   if (netif == NULL) {
+#if IP_FORWARD || IP_NAT
+    u8_t taken = 0;
+#endif /* IP_FORWARD || IP_NAT */
     /* packet not for us, route or discard */
     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
-#if IP_FORWARD
+#if IP_FORWARD || IP_NAT
     /* non-broadcast packet? */
-    if (!ip_addr_isbroadcast(&current_iphdr_dest, inp)) {
-      /* try to forward IP packet on (other) interfaces */
-      ip_forward(p, iphdr, inp);
-    } else
+    if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
+#if IP_NAT
+      /* check if we want to perform NAT with this packet. */
+      taken = ip_nat_out(p);
+      if (!taken)
+#endif /* IP_NAT */
+      {
+#if IP_FORWARD
+        /* try to forward IP packet on (other) interfaces */
+        if (ip_forward(p, iphdr, inp) != NULL) {
+          taken = 1;
+        }
 #endif /* IP_FORWARD */
+      }
+    }
+    if (!taken)
+#endif /* IP_FORWARD || IP_NAT */
     {
       snmp_inc_ipinaddrerrors();
       snmp_inc_ipindiscards();
@@ -553,6 +573,13 @@ ip_input(struct pbuf *p, struct netif *inp)
   current_netif = inp;
   current_header = iphdr;
 
+#if IP_NAT
+  if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
+      (ip_nat_input(p) != 0)) {
+     LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet consumed by nat layer\n"));
+  } else
+#endif /* IP_NAT */
+
 #if LWIP_RAW
   /* raw input did not eat the packet? */
   if (raw_input(p, inp) == 0)

+ 42 - 0
src/core/netif.c

@@ -60,6 +60,13 @@
 #include "lwip/dhcp.h"
 #endif /* LWIP_DHCP */
 
+#include <rtthread.h>
+
+#ifdef RT_USING_NETDEV
+#include "lwip/netdb.h"
+#include <netdev.h>
+#endif /* RT_USING_NETDEV */
+
 #if LWIP_NETIF_STATUS_CALLBACK
 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
 #else
@@ -374,6 +381,11 @@ netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
     ip4_addr2_16(&netif->ip_addr),
     ip4_addr3_16(&netif->ip_addr),
     ip4_addr4_16(&netif->ip_addr)));
+
+#ifdef RT_USING_NETDEV
+  /* rt-thread sal network interface device set IP address operations */
+  netdev_low_level_set_ipaddr(netdev_get_by_name(netif->name), (ip_addr_t *)ipaddr);
+#endif /* RT_USING_NETDEV */
 }
 
 /**
@@ -394,6 +406,11 @@ netif_set_gw(struct netif *netif, ip_addr_t *gw)
     ip4_addr2_16(&netif->gw),
     ip4_addr3_16(&netif->gw),
     ip4_addr4_16(&netif->gw)));
+
+#ifdef RT_USING_NETDEV
+  /* rt_thread network interface device set gateway address */
+  netdev_low_level_set_gw(netdev_get_by_name(netif->name), (ip_addr_t *)gw);
+#endif /* RT_USING_NETDEV */
 }
 
 /**
@@ -418,6 +435,11 @@ netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
     ip4_addr2_16(&netif->netmask),
     ip4_addr3_16(&netif->netmask),
     ip4_addr4_16(&netif->netmask)));
+
+#ifdef RT_USING_NETDEV
+  /* rt-thread network interface device set netmask address */
+  netdev_low_level_set_netmask(netdev_get_by_name(netif->name), (ip_addr_t *)netmask);
+#endif /* RT_USING_NETDEV */
 }
 
 /**
@@ -476,6 +498,11 @@ void netif_set_up(struct netif *netif)
       }
 #endif /* LWIP_IGMP */
     }
+
+#ifdef RT_USING_NETDEV
+    /* rt-thread network interface device set up status */
+    netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_TRUE);
+#endif /* RT_USING_NETDEV */
   }
 }
 
@@ -501,6 +528,11 @@ void netif_set_down(struct netif *netif)
     }
 #endif /* LWIP_ARP */
     NETIF_STATUS_CALLBACK(netif);
+
+#ifdef RT_USING_NETDEV
+    /* rt-thread network interface device set down status */
+    netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_FALSE);
+#endif /* RT_USING_NETDEV */
   }
 }
 
@@ -565,6 +597,11 @@ void netif_set_link_up(struct netif *netif )
 #endif /* LWIP_IGMP */
     }
     NETIF_LINK_CALLBACK(netif);
+
+#ifdef RT_USING_NETDEV
+    /* rt-thread network interface device set link up status */
+    netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_TRUE);
+#endif /* RT_USING_NETDEV */
   }
 }
 
@@ -576,6 +613,11 @@ void netif_set_link_down(struct netif *netif )
   if (netif->flags & NETIF_FLAG_LINK_UP) {
     netif->flags &= ~NETIF_FLAG_LINK_UP;
     NETIF_LINK_CALLBACK(netif);
+
+#ifdef RT_USING_NETDEV
+    /* rt-thread network interface device set link down status */
+    netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_FALSE);
+#endif /* RT_USING_NETDEV */
   }
 }
 

+ 2 - 2
src/core/snmp/msg_out.c

@@ -306,7 +306,7 @@ snmp_authfail_trap(void)
  *
  * @param vb_len varbind-list length
  * @param rhl points to returned header lengths
- * @return the required lenght for encoding the response header
+ * @return the required length for encoding the response header
  */
 static u16_t
 snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
@@ -353,7 +353,7 @@ snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
  *
  * @param vb_len varbind-list length
  * @param thl points to returned header lengths
- * @return the required lenght for encoding the trap header
+ * @return the required length for encoding the trap header
  */
 static u16_t
 snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len)

+ 6 - 0
src/core/tcp_out.c

@@ -1248,6 +1248,12 @@ tcp_rexmit_rto(struct tcp_pcb *pcb)
   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
   /* concatenate unsent queue after unacked queue */
   seg->next = pcb->unsent;
+  #if TCP_OVERSIZE && TCP_OVERSIZE_DBGCHECK
+  /* if last unsent changed, we need to update unsent_oversize */
+  if (pcb->unsent == NULL) {
+	pcb->unsent_oversize = seg->oversize_left;
+  }
+  #endif /* TCP_OVERSIZE && TCP_OVERSIZE_DBGCHECK*/
   /* unsent queue is the concatenated queue (of unacked, unsent) */
   pcb->unsent = pcb->unacked;
   /* unacked queue is now empty */

+ 1 - 1
src/include/lwip/init.h

@@ -47,7 +47,7 @@ extern "C" {
 /** For release candidates, this is set to 1..254
   * For official releases, this is set to 255 (LWIP_RC_RELEASE)
   * For development versions (CVS), this is set to 0 (LWIP_RC_DEVELOPMENT) */
-#define LWIP_VERSION_RC         0U
+#define LWIP_VERSION_RC         255U
 
 /** LWIP_VERSION_RC is set to LWIP_RC_RELEASE for official releases */
 #define LWIP_RC_RELEASE         255U

+ 1 - 0
src/include/netif/etharp.h

@@ -139,6 +139,7 @@ PACK_STRUCT_END
 #define ETHTYPE_VLAN      0x8100U
 #define ETHTYPE_PPPOEDISC 0x8863U  /* PPP Over Ethernet Discovery Stage */
 #define ETHTYPE_PPPOE     0x8864U  /* PPP Over Ethernet Session Stage */
+#define ETHTYPE_EAPOL     0x888eU  /* EAPOL */
 
 /** MEMCPY-like macro to copy to/from struct eth_addr's that are local variables
  * or known to be 32-bit aligned within the protocol header. */

+ 27 - 27
src/netif/ppp/ppp_oe.c

@@ -262,13 +262,13 @@ pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif)
 
   /* should be safe to access *sc now */
   if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
-    printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
-      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);
+    PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
+      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state));
     return NULL;
   }
   if (sc->sc_ethif != rcvif) {
-    printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",
-      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
+    PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": wrong interface, not accepting host unique\n",
+      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
     return NULL;
   }
   return sc;
@@ -319,13 +319,13 @@ pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
 #endif
   session = 0;
   if (pb->len - off < PPPOE_HEADERLEN) {
-    printf("pppoe: packet too short: %d\n", pb->len);
+    PPPDEBUG(LOG_DEBUG, ("pppoe: packet too short: %d\n", pb->len));
     goto done;
   }
 
   ph = (struct pppoehdr *) (ethhdr + 1);
   if (ph->vertype != PPPOE_VERTYPE) {
-    printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);
+    PPPDEBUG(LOG_DEBUG, ("pppoe: unknown version/type packet: 0x%x\n", ph->vertype));
     goto done;
   }
   session = ntohs(ph->session);
@@ -333,8 +333,8 @@ pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
   off += sizeof(*ph);
 
   if (plen + off > pb->len) {
-    printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
-        pb->len - off, plen);
+    PPPDEBUG(LOG_DEBUG, ("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
+        pb->len - off, plen));
     goto done;
   }
   if(pb->tot_len == pb->len) {
@@ -348,7 +348,7 @@ pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
     tag = ntohs(pt.tag);
     len = ntohs(pt.len);
     if (off + sizeof(pt) + len > pb->len) {
-      printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);
+      PPPDEBUG(LOG_DEBUG, ("pppoe: tag 0x%x len 0x%x is too long\n", tag, len));
       goto done;
     }
     switch (tag) {
@@ -395,9 +395,9 @@ pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
         u16_t error_len = LWIP_MIN(len, sizeof(pppoe_error_tmp)-1);
         strncpy(pppoe_error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
         pppoe_error_tmp[error_len-1] = '\0';
-        printf("%s: %s: %s\n", devname, err_msg, pppoe_error_tmp);
+        PPPDEBUG(LOG_DEBUG, ("%s: %s: %s\n", devname, err_msg, pppoe_error_tmp));
       } else {
-        printf("%s: %s\n", devname, err_msg);
+        PPPDEBUG(LOG_DEBUG, ("%s: %s\n", devname, err_msg));
       }
       if (errortag) {
         goto done;
@@ -455,19 +455,19 @@ breakbreak:;
        */
       if (ac_cookie == NULL) {
         /* be quiet if there is not a single pppoe instance */
-        printf("pppoe: received PADR but not includes ac_cookie\n");
+        PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but not includes ac_cookie\n"));
         goto done;
       }
       sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
       if (sc == NULL) {
         /* be quiet if there is not a single pppoe instance */
         if (!LIST_EMPTY(&pppoe_softc_list)) {
-          printf("pppoe: received PADR but could not find request for it\n");
+          PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but could not find request for it\n"));
         }
         goto done;
       }
       if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
-        printf("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
+        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));
         goto done;
       }
       if (hunique) {
@@ -493,12 +493,12 @@ breakbreak:;
       if (sc == NULL) {
         /* be quiet if there is not a single pppoe instance */
         if (pppoe_softc_list != NULL) {
-          printf("pppoe: received PADO but could not find request for it\n");
+          PPPDEBUG(LOG_DEBUG, ("pppoe: received PADO but could not find request for it\n"));
         }
         goto done;
       }
       if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
-        printf("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
+        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));
         goto done;
       }
       if (ac_cookie) {
@@ -532,11 +532,11 @@ breakbreak:;
       break;
     default:
       if(sc) {
-        printf("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
+        PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
             sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
-            (u16_t)ph->code, session);
+            (u16_t)ph->code, session));
       } else {
-        printf("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session);
+        PPPDEBUG(LOG_DEBUG, ("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session));
       }
       break;
   }
@@ -580,18 +580,18 @@ pppoe_data_input(struct netif *netif, struct pbuf *pb)
   pb = pppSingleBuf (pb);
 
   if (pb->len <= PPPOE_HEADERLEN) {
-    printf("pppoe (data): dropping too short packet: %d bytes\n", pb->len);
+    PPPDEBUG(LOG_DEBUG, ("pppoe (data): dropping too short packet: %d bytes\n", pb->len));
     goto drop;
   }
 
   if (pb->len < sizeof(*ph)) {
-    printf("pppoe_data_input: could not get PPPoE header\n");
+    PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: could not get PPPoE header\n"));
     goto drop;
   }
   ph = (struct pppoehdr *)pb->payload;
 
   if (ph->vertype != PPPOE_VERTYPE) {
-    printf("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype);
+    PPPDEBUG(LOG_DEBUG, ("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype));
     goto drop;
   }
   if (ph->code != 0) {
@@ -602,8 +602,8 @@ pppoe_data_input(struct netif *netif, struct pbuf *pb)
   sc = pppoe_find_softc_by_session(session, netif);
   if (sc == NULL) {
 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
-    printf("pppoe: input for unknown session 0x%x, sending PADT\n", session);
-    pppoe_send_padt(netif, session, shost);
+    PPPDEBUG(LOG_DEBUG, ("pppoe: input for unknown session 0x%x, sending PADT\n", session);
+    pppoe_send_padt(netif, session, shost));
 #endif
     goto drop;
   }
@@ -874,7 +874,7 @@ pppoe_do_disconnect(struct pppoe_softc *sc)
 static void
 pppoe_abort_connect(struct pppoe_softc *sc)
 {
-  printf("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
+  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));
   sc->sc_state = PPPOE_STATE_CLOSING;
 
   sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */
@@ -1096,8 +1096,8 @@ pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)
     }
     if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
       sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
-      printf("%c%c%"U16_F": ethernet interface detached, going down\n",
-          sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
+      PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": ethernet interface detached, going down\n",
+          sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
     }
     sc->sc_ethif = NULL;
     pppoe_clear_softc(sc, "ethernet interface detached");