From eeb2218b3dfa704ed87a183f7199015005e8ca89 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Wed, 18 Jul 2018 08:34:01 +0200 Subject: [PATCH] Revert "Test / RFC: Reformat a few files using clang-format" This reverts commit 8b4a8159a898795ef0fc9226dae1ce66531ad487. We do not want to do this shortly before a release. Reformatting (buggy reformatting) may introduce new bugs. --- src/api/api_lib.c | 115 ++- src/api/api_msg.c | 191 ++--- src/api/err.c | 36 +- src/api/if_api.c | 6 +- src/api/netbuf.c | 9 +- src/api/netdb.c | 33 +- src/api/netifapi.c | 98 +-- src/api/sockets.c | 841 ++++++++++----------- src/api/tcpip.c | 32 +- src/core/ipv4/autoip.c | 103 ++- src/core/ipv4/dhcp.c | 380 ++++------ src/core/ipv4/etharp.c | 235 +++--- src/core/ipv4/icmp.c | 46 +- src/core/ipv4/igmp.c | 138 ++-- src/core/ipv4/ip4.c | 311 ++++---- src/core/ipv4/ip4_addr.c | 15 +- src/core/ipv4/ip4_frag.c | 84 ++- src/core/ipv6/dhcp6.c | 127 ++-- src/core/ipv6/ethip6.c | 12 +- src/core/ipv6/icmp6.c | 219 +++--- src/core/ipv6/ip6.c | 986 ++++++++++++------------- src/core/ipv6/ip6_addr.c | 29 +- src/core/ipv6/ip6_frag.c | 109 +-- src/core/ipv6/mld6.c | 160 ++-- src/core/ipv6/nd6.c | 1507 +++++++++++++++++++------------------- 25 files changed, 2761 insertions(+), 3061 deletions(-) diff --git a/src/api/api_lib.c b/src/api/api_lib.c index 43b6b288..42edc57c 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -63,11 +63,11 @@ #include "lwip/memp.h" #include "lwip/ip.h" +#include "lwip/raw.h" +#include "lwip/udp.h" #include "lwip/priv/api_msg.h" #include "lwip/priv/tcp_priv.h" #include "lwip/priv/tcpip_priv.h" -#include "lwip/raw.h" -#include "lwip/udp.h" #ifdef LWIP_HOOK_FILENAME #include LWIP_HOOK_FILENAME @@ -75,11 +75,11 @@ #include -#define API_MSG_VAR_REF(name) API_VAR_REF(name) -#define API_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct api_msg, name) -#define API_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name, ERR_MEM) +#define API_MSG_VAR_REF(name) API_VAR_REF(name) +#define API_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct api_msg, name) +#define API_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name, ERR_MEM) #define API_MSG_VAR_ALLOC_RETURN_NULL(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name, NULL) -#define API_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_API_MSG, name) +#define API_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_API_MSG, name) #if TCP_LISTEN_BACKLOG /* need to allocate API message for accept so empty message pool does not result in event loss @@ -92,22 +92,18 @@ #endif /* TCP_LISTEN_BACKLOG */ #if LWIP_NETCONN_FULLDUPLEX -#define NETCONN_RECVMBOX_WAITABLE(conn) \ - (sys_mbox_valid(&(conn)->recvmbox) && (((conn)->flags & NETCONN_FLAG_MBOXINVALID) == 0)) -#define NETCONN_ACCEPTMBOX_WAITABLE(conn) \ - (sys_mbox_valid(&(conn)->acceptmbox) && (((conn)->flags & (NETCONN_FLAG_MBOXCLOSED | NETCONN_FLAG_MBOXINVALID)) == 0)) +#define NETCONN_RECVMBOX_WAITABLE(conn) (sys_mbox_valid(&(conn)->recvmbox) && (((conn)->flags & NETCONN_FLAG_MBOXINVALID) == 0)) +#define NETCONN_ACCEPTMBOX_WAITABLE(conn) (sys_mbox_valid(&(conn)->acceptmbox) && (((conn)->flags & (NETCONN_FLAG_MBOXCLOSED|NETCONN_FLAG_MBOXINVALID)) == 0)) #define NETCONN_MBOX_WAITING_INC(conn) SYS_ARCH_INC(conn->mbox_threads_waiting, 1) #define NETCONN_MBOX_WAITING_DEC(conn) SYS_ARCH_INC(conn->mbox_threads_waiting, 1) #else /* LWIP_NETCONN_FULLDUPLEX */ -#define NETCONN_RECVMBOX_WAITABLE(conn) sys_mbox_valid(&(conn)->recvmbox) -#define NETCONN_ACCEPTMBOX_WAITABLE(conn) \ - (sys_mbox_valid(&(conn)->acceptmbox) && (((conn)->flags & NETCONN_FLAG_MBOXCLOSED) == 0)) +#define NETCONN_RECVMBOX_WAITABLE(conn) sys_mbox_valid(&(conn)->recvmbox) +#define NETCONN_ACCEPTMBOX_WAITABLE(conn) (sys_mbox_valid(&(conn)->acceptmbox) && (((conn)->flags & NETCONN_FLAG_MBOXCLOSED) == 0)) #define NETCONN_MBOX_WAITING_INC(conn) #define NETCONN_MBOX_WAITING_DEC(conn) #endif /* LWIP_NETCONN_FULLDUPLEX */ -static err_t -netconn_close_shutdown(struct netconn *conn, u8_t how); +static err_t netconn_close_shutdown(struct netconn *conn, u8_t how); /** * Call the lower part of a netconn_* function @@ -286,7 +282,7 @@ netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local) err = netconn_apimsg(lwip_netconn_do_getaddr, &API_MSG_VAR_REF(msg)); *addr = msg->msg.ad.ipaddr; *port = msg->msg.ad.port; -#else /* LWIP_MPU_COMPATIBLE */ +#else /* LWIP_MPU_COMPATIBLE */ msg.msg.ad.ipaddr = addr; msg.msg.ad.port = port; err = netconn_apimsg(lwip_netconn_do_getaddr, &msg); @@ -326,7 +322,8 @@ netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port) /* "Socket API like" dual-stack support: If IP to bind to is IP6_ADDR_ANY, * and NETCONN_FLAG_IPV6_V6ONLY is 0, use IP_ANY_TYPE to bind */ - if ((netconn_get_ipv6only(conn) == 0) && ip_addr_cmp(addr, IP6_ADDR_ANY)) { + if ((netconn_get_ipv6only(conn) == 0) && + ip_addr_cmp(addr, IP6_ADDR_ANY)) { addr = IP_ANY_TYPE; } #endif /* LWIP_IPV4 && LWIP_IPV6 */ @@ -454,7 +451,7 @@ netconn_listen_with_backlog(struct netconn *conn, u8_t backlog) API_MSG_VAR_FREE(msg); return err; -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ LWIP_UNUSED_ARG(conn); LWIP_UNUSED_ARG(backlog); return ERR_ARG; @@ -481,9 +478,9 @@ netconn_accept(struct netconn *conn, struct netconn **new_conn) API_MSG_VAR_DECLARE(msg); #endif /* TCP_LISTEN_BACKLOG */ - LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;); *new_conn = NULL; - LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;); /* NOTE: Although the opengroup spec says a pending error shall be returned to send/recv/getsockopt(SO_ERROR) only, we return it for listening @@ -555,7 +552,7 @@ netconn_accept(struct netconn *conn, struct netconn **new_conn) *new_conn = newconn; /* don't set conn->last_err: it's only ERR_OK, anyway */ return ERR_OK; -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ LWIP_UNUSED_ARG(conn); LWIP_UNUSED_ARG(new_conn); return ERR_ARG; @@ -586,7 +583,7 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags) LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;); *new_buf = NULL; - LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); if (!NETCONN_RECVMBOX_WAITABLE(conn)) { err_t err = netconn_err(conn); @@ -598,8 +595,8 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags) } NETCONN_MBOX_WAITING_INC(conn); - if (netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK) || (conn->flags & NETCONN_FLAG_MBOXCLOSED) || - (conn->pending_err != ERR_OK)) { + if (netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK) || + (conn->flags & NETCONN_FLAG_MBOXCLOSED) || (conn->pending_err != ERR_OK)) { if (sys_arch_mbox_tryfetch(&conn->recvmbox, &buf) == SYS_ARCH_TIMEOUT) { err_t err; NETCONN_MBOX_WAITING_DEC(conn); @@ -668,7 +665,7 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags) /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVMINUS, len); - LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%" U16_F "\n", buf, len)); + LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len)); *new_buf = buf; /* don't set conn->last_err: it's only ERR_OK, anyway */ @@ -679,9 +676,8 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags) static err_t netconn_tcp_recvd_msg(struct netconn *conn, size_t len, struct api_msg *msg) { - LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;); msg->conn = conn; msg->msg.r.len = len; @@ -694,9 +690,8 @@ netconn_tcp_recvd(struct netconn *conn, size_t len) { err_t err; API_MSG_VAR_DECLARE(msg); - LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;); API_MSG_VAR_ALLOC(msg); err = netconn_tcp_recvd_msg(conn, len, &API_VAR_REF(msg)); @@ -725,7 +720,7 @@ netconn_recv_data_tcp(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags if (!(apiflags & NETCONN_NOAUTORCVD)) { /* need to allocate API message here so empty message pool does not result in event loss - * see bug #47512: MPU_COMPATIBLE may fail on empty pool */ + * see bug #47512: MPU_COMPATIBLE may fail on empty pool */ API_MSG_VAR_ALLOC(msg); } @@ -742,7 +737,7 @@ netconn_recv_data_tcp(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags u16_t len = buf ? buf->tot_len : 1; /* don't care for the return value of lwip_netconn_do_recv */ /* @todo: this should really be fixed, e.g. by retrying in poll on error */ - netconn_tcp_recvd_msg(conn, len, &API_VAR_REF(msg)); + netconn_tcp_recvd_msg(conn, len, &API_VAR_REF(msg)); API_MSG_VAR_FREE(msg); } @@ -754,7 +749,7 @@ netconn_recv_data_tcp(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags netconn_set_flags(conn, NETCONN_FIN_RX_PENDING); return ERR_WOULDBLOCK; } else { - handle_fin: +handle_fin: API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0); if (conn->pcb.ip == NULL) { /* race condition: RST during recv */ @@ -786,9 +781,8 @@ netconn_recv_data_tcp(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf) { - LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;); return netconn_recv_data_tcp(conn, new_buf, 0); } @@ -808,9 +802,8 @@ netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf) err_t netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags) { - LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;); return netconn_recv_data_tcp(conn, new_buf, apiflags); } @@ -828,9 +821,8 @@ netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t ap err_t netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf) { - LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;); return netconn_recv_data(conn, (void **)new_buf, 0); } @@ -849,9 +841,8 @@ netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf) err_t netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags) { - LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", - (conn != NULL) && NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, - return ERR_ARG;); + LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", (conn != NULL) && + NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;); return netconn_recv_data(conn, (void **)new_buf, apiflags); } @@ -875,7 +866,7 @@ netconn_recv(struct netconn *conn, struct netbuf **new_buf) LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;); *new_buf = NULL; - LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); #if LWIP_TCP #if (LWIP_UDP || LWIP_RAW) @@ -952,9 +943,9 @@ netconn_send(struct netconn *conn, struct netbuf *buf) API_MSG_VAR_DECLARE(msg); err_t err; - LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;); - LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %" U16_F " bytes\n", buf->p->tot_len)); + LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len)); API_MSG_VAR_ALLOC(msg); API_MSG_VAR_REF(msg).conn = conn; @@ -980,7 +971,8 @@ netconn_send(struct netconn *conn, struct netbuf *buf) * @return ERR_OK if data was sent, any other err_t on error */ err_t -netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags, size_t *bytes_written) +netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size, + u8_t apiflags, size_t *bytes_written) { struct netvector vector; vector.ptr = dataptr; @@ -1002,11 +994,8 @@ netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size, u8_ * @return ERR_OK if data was sent, any other err_t on error */ err_t -netconn_write_vectors_partly(struct netconn *conn, - struct netvector *vectors, - u16_t vectorcnt, - u8_t apiflags, - size_t *bytes_written) +netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt, + u8_t apiflags, size_t *bytes_written) { API_MSG_VAR_DECLARE(msg); err_t err; @@ -1014,8 +1003,8 @@ netconn_write_vectors_partly(struct netconn *conn, size_t size; int i; - LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;); - LWIP_ERROR("netconn_write: invalid conn->type", (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP), return ERR_VAL;); + LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_write: invalid conn->type", (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP), return ERR_VAL;); dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK); #if LWIP_SO_SNDTIMEO if (conn->send_timeout != 0) { @@ -1103,7 +1092,7 @@ netconn_close_shutdown(struct netconn *conn, u8_t how) err_t err; LWIP_UNUSED_ARG(how); - LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;); API_MSG_VAR_ALLOC(msg); API_MSG_VAR_REF(msg).conn = conn; @@ -1114,7 +1103,7 @@ netconn_close_shutdown(struct netconn *conn, u8_t how) /* get the time we started, which is later compared to sys_now() + conn->send_timeout */ API_MSG_VAR_REF(msg).msg.sd.time_started = sys_now(); -#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ +#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ API_MSG_VAR_REF(msg).msg.sd.polls_left = ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1; #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ @@ -1197,7 +1186,7 @@ netconn_join_leave_group(struct netconn *conn, API_MSG_VAR_DECLARE(msg); err_t err; - LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;); API_MSG_VAR_ALLOC(msg); @@ -1239,7 +1228,7 @@ netconn_join_leave_group_netif(struct netconn *conn, API_MSG_VAR_DECLARE(msg); err_t err; - LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;); + LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;); API_MSG_VAR_ALLOC(msg); @@ -1314,7 +1303,7 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr) #if LWIP_MPU_COMPATIBLE strncpy(API_VAR_REF(msg).name, name, DNS_MAX_NAME_LENGTH - 1); API_VAR_REF(msg).name[DNS_MAX_NAME_LENGTH - 1] = 0; -#else /* LWIP_MPU_COMPATIBLE */ +#else /* LWIP_MPU_COMPATIBLE */ msg.err = &err; msg.sem = &sem; API_VAR_REF(msg).addr = API_VAR_REF(addr); @@ -1325,7 +1314,7 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr) #endif /* LWIP_IPV4 && LWIP_IPV6 */ #if LWIP_NETCONN_SEM_PER_THREAD API_VAR_REF(msg).sem = LWIP_NETCONN_THREAD_SEM_GET(); -#else /* LWIP_NETCONN_SEM_PER_THREAD*/ +#else /* LWIP_NETCONN_SEM_PER_THREAD*/ err = sys_sem_new(API_EXPR_REF(API_VAR_REF(msg).sem), 0); if (err != ERR_OK) { API_VAR_FREE(MEMP_DNS_API_MSG, msg); diff --git a/src/api/api_msg.c b/src/api/api_msg.c index 3d8fd223..39531024 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -44,13 +44,13 @@ #include "lwip/ip.h" #include "lwip/ip_addr.h" -#include "lwip/raw.h" -#include "lwip/tcp.h" #include "lwip/udp.h" +#include "lwip/tcp.h" +#include "lwip/raw.h" -#include "lwip/dns.h" -#include "lwip/igmp.h" #include "lwip/memp.h" +#include "lwip/igmp.h" +#include "lwip/dns.h" #include "lwip/mld6.h" #include "lwip/priv/tcpip_priv.h" @@ -59,14 +59,10 @@ /* netconns are polled once per second (e.g. continue write on memory error) */ #define NETCONN_TCP_POLL_INTERVAL 2 -#define SET_NONBLOCKING_CONNECT(conn, val) \ - do { \ - if (val) { \ - netconn_set_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); \ - } else { \ - netconn_clear_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); \ - } \ - } while (0) +#define SET_NONBLOCKING_CONNECT(conn, val) do { if (val) { \ + netconn_set_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); \ +} else { \ + netconn_clear_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); }} while(0) #define IN_NONBLOCKING_CONNECT(conn) netconn_is_flag_set(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT) #if LWIP_NETCONN_FULLDUPLEX @@ -78,28 +74,22 @@ /* forward declarations */ #if LWIP_TCP #if LWIP_TCPIP_CORE_LOCKING -#define WRITE_DELAYED , 1 -#define WRITE_DELAYED_PARAM , u8_t delayed +#define WRITE_DELAYED , 1 +#define WRITE_DELAYED_PARAM , u8_t delayed #else /* LWIP_TCPIP_CORE_LOCKING */ #define WRITE_DELAYED #define WRITE_DELAYED_PARAM #endif /* LWIP_TCPIP_CORE_LOCKING */ -static err_t -lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM); -static err_t -lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM); +static err_t lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM); +static err_t lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM); #endif -static void -netconn_drain(struct netconn *conn); +static void netconn_drain(struct netconn *conn); #if LWIP_TCPIP_CORE_LOCKING #define TCPIP_APIMSG_ACK(m) #else /* LWIP_TCPIP_CORE_LOCKING */ -#define TCPIP_APIMSG_ACK(m) \ - do { \ - sys_sem_signal(LWIP_API_MSG_SEM(m)); \ - } while (0) +#define TCPIP_APIMSG_ACK(m) do { sys_sem_signal(LWIP_API_MSG_SEM(m)); } while(0) #endif /* LWIP_TCPIP_CORE_LOCKING */ #if LWIP_NETCONN_FULLDUPLEX @@ -156,6 +146,7 @@ lwip_netconn_is_err_msg(void *msg, err_t *err) } #endif /* LWIP_TCP */ + #if LWIP_RAW /** * Receive callback function for RAW netconns. @@ -165,7 +156,8 @@ lwip_netconn_is_err_msg(void *msg, err_t *err) * @see raw.h (struct raw_pcb.recv) for parameters and return value */ static u8_t -recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) +recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, + const ip_addr_t *addr) { struct pbuf *q; struct netbuf *buf; @@ -223,7 +215,8 @@ recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) * @see udp.h (struct udp_pcb.recv) for parameters */ static void -recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) +recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, + const ip_addr_t *addr, u16_t port) { struct netbuf *buf; struct netconn *conn; @@ -246,7 +239,8 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, #if LWIP_SO_RCVBUF SYS_ARCH_GET(conn->recv_avail, recv_avail); - if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox) || ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) { + if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox) || + ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) { #else /* LWIP_SO_RCVBUF */ if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox)) { #endif /* LWIP_SO_RCVBUF */ @@ -368,14 +362,14 @@ poll_tcp(void *arg, struct tcp_pcb *pcb) LWIP_ASSERT("conn != NULL", (conn != NULL)); if (conn->state == NETCONN_WRITE) { - lwip_netconn_do_writemore(conn WRITE_DELAYED); + lwip_netconn_do_writemore(conn WRITE_DELAYED); } else if (conn->state == NETCONN_CLOSE) { #if !LWIP_SO_SNDTIMEO && !LWIP_SO_LINGER if (conn->current_msg && conn->current_msg->msg.sd.polls_left) { conn->current_msg->msg.sd.polls_left--; } #endif /* !LWIP_SO_SNDTIMEO && !LWIP_SO_LINGER */ - lwip_netconn_do_close_internal(conn WRITE_DELAYED); + lwip_netconn_do_close_internal(conn WRITE_DELAYED); } /* @todo: implement connect timeout here? */ @@ -410,9 +404,9 @@ sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len) if (conn) { if (conn->state == NETCONN_WRITE) { - lwip_netconn_do_writemore(conn WRITE_DELAYED); + lwip_netconn_do_writemore(conn WRITE_DELAYED); } else if (conn->state == NETCONN_CLOSE) { - lwip_netconn_do_close_internal(conn WRITE_DELAYED); + lwip_netconn_do_close_internal(conn WRITE_DELAYED); } /* If the queued byte- or pbuf-count drops below the configured low-water limit, @@ -479,7 +473,8 @@ err_tcp(void *arg, err_t err) sys_mbox_trypost(&conn->acceptmbox, mbox_msg); } - if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) || (old_state == NETCONN_CONNECT)) { + if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) || + (old_state == NETCONN_CONNECT)) { /* calling lwip_netconn_do_writemore/lwip_netconn_do_close_internal is not necessary since the pcb has already been deleted! */ int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn); @@ -760,14 +755,14 @@ netconn_alloc(enum netconn_type t, netconn_callback callback) #if LWIP_TCP sys_mbox_set_invalid(&conn->acceptmbox); #endif - conn->state = NETCONN_NONE; + conn->state = NETCONN_NONE; #if LWIP_SOCKET /* initialize socket to -1 since 0 is a valid socket */ - conn->socket = -1; + conn->socket = -1; #endif /* LWIP_SOCKET */ - conn->callback = callback; + conn->callback = callback; #if LWIP_TCP - conn->current_msg = NULL; + conn->current_msg = NULL; #endif /* LWIP_TCP */ #if LWIP_SO_SNDTIMEO conn->send_timeout = 0; @@ -777,7 +772,7 @@ netconn_alloc(enum netconn_type t, netconn_callback callback) #endif /* LWIP_SO_RCVTIMEO */ #if LWIP_SO_RCVBUF conn->recv_bufsize = RECV_BUFSIZE_DEFAULT; - conn->recv_avail = 0; + conn->recv_avail = 0; #endif /* LWIP_SO_RCVBUF */ #if LWIP_SO_LINGER conn->linger = -1; @@ -805,9 +800,11 @@ netconn_free(struct netconn *conn) netconn_drain(conn); #endif /* LWIP_NETCONN_FULLDUPLEX */ - LWIP_ASSERT("recvmbox must be deallocated before calling this function", !sys_mbox_valid(&conn->recvmbox)); + LWIP_ASSERT("recvmbox must be deallocated before calling this function", + !sys_mbox_valid(&conn->recvmbox)); #if LWIP_TCP - LWIP_ASSERT("acceptmbox must be deallocated before calling this function", !sys_mbox_valid(&conn->acceptmbox)); + LWIP_ASSERT("acceptmbox must be deallocated before calling this function", + !sys_mbox_valid(&conn->acceptmbox)); #endif /* LWIP_TCP */ #if !LWIP_NETCONN_SEM_PER_THREAD @@ -920,7 +917,7 @@ netconn_mark_mbox_invalid(struct netconn *conn) * @param conn the TCP netconn to close */ static err_t -lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM) +lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM) { err_t err; u8_t shut, shut_rx, shut_tx, shut_close; @@ -944,7 +941,10 @@ lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM) (also if RD or WR side was shut down before already) */ if (shut == NETCONN_SHUT_RDWR) { shut_close = 1; - } else if (shut_rx && ((tpcb->state == FIN_WAIT_1) || (tpcb->state == FIN_WAIT_2) || (tpcb->state == CLOSING))) { + } else if (shut_rx && + ((tpcb->state == FIN_WAIT_1) || + (tpcb->state == FIN_WAIT_2) || + (tpcb->state == CLOSING))) { shut_close = 1; } else if (shut_tx && ((tpcb->flags & TF_RXCLOSED) != 0)) { shut_close = 1; @@ -988,7 +988,8 @@ lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM) if (netconn_is_nonblocking(conn)) { /* data left on a nonblocking netconn -> cannot linger */ err = ERR_WOULDBLOCK; - } else if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= (conn->linger * 1000)) { + } else if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= + (conn->linger * 1000)) { /* data left but linger timeout has expired (this happens on further calls to this function through poll_tcp */ tcp_abort(tpcb); @@ -1037,7 +1038,7 @@ lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM) } #endif if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= close_timeout) { -#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ +#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ if (conn->current_msg->msg.sd.polls_left == 0) { #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ close_finished = 1; @@ -1119,7 +1120,8 @@ lwip_netconn_do_delconn(void *m) #if LWIP_NETCONN_FULLDUPLEX /* In full duplex mode, blocking write/connect is aborted with ERR_CLSD */ if (state != NETCONN_NONE) { - if ((state == NETCONN_WRITE) || ((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) { + if ((state == NETCONN_WRITE) || + ((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) { /* close requested, abort running write/connect */ sys_sem_t *op_completed_sem; LWIP_ASSERT("msg->conn->current_msg != NULL", msg->conn->current_msg != NULL); @@ -1130,8 +1132,10 @@ lwip_netconn_do_delconn(void *m) sys_sem_signal(op_completed_sem); } } -#else /* LWIP_NETCONN_FULLDUPLEX */ - if (((state != NETCONN_NONE) && (state != NETCONN_LISTEN) && (state != NETCONN_CONNECT)) || +#else /* LWIP_NETCONN_FULLDUPLEX */ + if (((state != NETCONN_NONE) && + (state != NETCONN_LISTEN) && + (state != NETCONN_CONNECT)) || ((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) { /* This means either a blocking write or blocking connect is running (nonblocking write returns and sets state to NONE) */ @@ -1139,12 +1143,13 @@ lwip_netconn_do_delconn(void *m) } else #endif /* LWIP_NETCONN_FULLDUPLEX */ { - LWIP_ASSERT("blocking connect in progress", (state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn)); + LWIP_ASSERT("blocking connect in progress", + (state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn)); msg->err = ERR_OK; #if LWIP_NETCONN_FULLDUPLEX /* Mark mboxes invalid */ netconn_mark_mbox_invalid(msg->conn); -#else /* LWIP_NETCONN_FULLDUPLEX */ +#else /* LWIP_NETCONN_FULLDUPLEX */ netconn_drain(msg->conn); #endif /* LWIP_NETCONN_FULLDUPLEX */ @@ -1176,7 +1181,7 @@ lwip_netconn_do_delconn(void *m) LOCK_TCPIP_CORE(); LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE); } -#else /* LWIP_TCPIP_CORE_LOCKING */ +#else /* LWIP_TCPIP_CORE_LOCKING */ lwip_netconn_do_close_internal(msg->conn); #endif /* LWIP_TCPIP_CORE_LOCKING */ /* API_EVENT is called inside lwip_netconn_do_close_internal, before releasing @@ -1321,7 +1326,8 @@ lwip_netconn_do_connected(void *arg, struct tcp_pcb *pcb, err_t err) was_blocking = !IN_NONBLOCKING_CONNECT(conn); SET_NONBLOCKING_CONNECT(conn, 0); LWIP_ASSERT("blocking connect state error", - (was_blocking && op_completed_sem != NULL) || (!was_blocking && op_completed_sem == NULL)); + (was_blocking && op_completed_sem != NULL) || + (!was_blocking && op_completed_sem == NULL)); conn->current_msg = NULL; conn->state = NETCONN_NONE; API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0); @@ -1370,8 +1376,8 @@ lwip_netconn_do_connect(void *m) err = ERR_ISCONN; } else { setup_tcp(msg->conn); - err = tcp_connect( - msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port, lwip_netconn_do_connected); + err = tcp_connect(msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr), + msg->msg.bc.port, lwip_netconn_do_connected); if (err == ERR_OK) { u8_t non_blocking = netconn_is_nonblocking(msg->conn); msg->conn->state = NETCONN_CONNECT; @@ -1396,7 +1402,9 @@ lwip_netconn_do_connect(void *m) break; #endif /* LWIP_TCP */ default: - LWIP_ERROR("Invalid netconn type", 0, do { err = ERR_VAL; } while (0)); + LWIP_ERROR("Invalid netconn type", 0, do { + err = ERR_VAL; + } while (0)); break; } } @@ -1459,11 +1467,12 @@ lwip_netconn_do_listen(void *m) #endif /* TCP_LISTEN_BACKLOG */ #if LWIP_IPV4 && LWIP_IPV6 /* "Socket API like" dual-stack support: If IP to listen to is IP6_ADDR_ANY, - * and NETCONN_FLAG_IPV6_V6ONLY is NOT set, use IP_ANY_TYPE to listen - */ - if (ip_addr_cmp(&msg->conn->pcb.ip->local_ip, IP6_ADDR_ANY) && (netconn_get_ipv6only(msg->conn) == 0)) { + * and NETCONN_FLAG_IPV6_V6ONLY is NOT set, use IP_ANY_TYPE to listen + */ + if (ip_addr_cmp(&msg->conn->pcb.ip->local_ip, IP6_ADDR_ANY) && + (netconn_get_ipv6only(msg->conn) == 0)) { /* change PCB type to IPADDR_TYPE_ANY */ - IP_SET_TYPE_VAL(msg->conn->pcb.tcp->local_ip, IPADDR_TYPE_ANY); + IP_SET_TYPE_VAL(msg->conn->pcb.tcp->local_ip, IPADDR_TYPE_ANY); IP_SET_TYPE_VAL(msg->conn->pcb.tcp->remote_ip, IPADDR_TYPE_ANY); } #endif /* LWIP_IPV4 && LWIP_IPV6 */ @@ -1541,17 +1550,14 @@ lwip_netconn_do_send(void *m) case NETCONN_UDP: #if LWIP_CHECKSUM_ON_COPY if (ip_addr_isany(&msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) { - err = udp_send_chksum( - msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum); + err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p, + msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum); } else { - err = udp_sendto_chksum(msg->conn->pcb.udp, - msg->msg.b->p, - &msg->msg.b->addr, - msg->msg.b->port, - msg->msg.b->flags & NETBUF_FLAG_CHKSUM, - msg->msg.b->toport_chksum); + err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p, + &msg->msg.b->addr, msg->msg.b->port, + msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum); } -#else /* LWIP_CHECKSUM_ON_COPY */ +#else /* LWIP_CHECKSUM_ON_COPY */ if (ip_addr_isany_val(msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) { err = udp_send(msg->conn->pcb.udp, msg->msg.b->p); } else { @@ -1631,7 +1637,7 @@ lwip_netconn_do_accepted(void *m) * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished */ static err_t -lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) +lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) { err_t err; const void *dataptr; @@ -1654,7 +1660,8 @@ lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK); #if LWIP_SO_SNDTIMEO - if ((conn->send_timeout != 0) && ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) { + if ((conn->send_timeout != 0) && + ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) { write_finished = 1; if (conn->current_msg->msg.w.offset == 0) { /* nothing has been written */ @@ -1695,7 +1702,8 @@ lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) 1) We couldn't finish the current vector because of 16-bit size limitations. tcp_write() and tcp_sndbuf() both are limited to 16-bit sizes 2) We are sending the remainder of the current vector and have more */ - if ((len == 0xffff && diff > 0xffffUL) || (len == (u16_t)diff && conn->current_msg->msg.w.vector_cnt > 1)) { + if ((len == 0xffff && diff > 0xffffUL) || + (len == (u16_t)diff && conn->current_msg->msg.w.vector_cnt > 1)) { write_more = 1; apiflags |= TCP_WRITE_FLAG_MORE; } else { @@ -1718,13 +1726,14 @@ lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) } while (write_more && err == ERR_OK); /* if OK or memory error, check available space */ if ((err == ERR_OK) || (err == ERR_MEM)) { - err_mem: +err_mem: if (dontblock && (conn->current_msg->msg.w.offset < conn->current_msg->msg.w.len)) { /* non-blocking write did not write everything: mark the pcb non-writable and let poll_tcp check writable space to mark the pcb writable again */ API_EVENT(conn, NETCONN_EVT_SENDMINUS, 0); conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE; - } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) || (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) { + } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) || + (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) { /* The queued byte- or pbuf-count exceeds the configured low-water limit, let select mark this pcb as non-writable. */ API_EVENT(conn, NETCONN_EVT_SENDMINUS, 0); @@ -1826,7 +1835,7 @@ lwip_netconn_do_write(void *m) LOCK_TCPIP_CORE(); LWIP_ASSERT("state!", msg->conn->state != NETCONN_WRITE); } -#else /* LWIP_TCPIP_CORE_LOCKING */ +#else /* LWIP_TCPIP_CORE_LOCKING */ lwip_netconn_do_writemore(msg->conn); #endif /* LWIP_TCPIP_CORE_LOCKING */ /* for both cases: if lwip_netconn_do_writemore was called, don't ACK the APIMSG @@ -1835,7 +1844,7 @@ lwip_netconn_do_write(void *m) } else { err = ERR_CONN; } -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ err = ERR_VAL; #endif /* LWIP_TCP */ #if (LWIP_UDP || LWIP_RAW) @@ -1861,9 +1870,11 @@ lwip_netconn_do_getaddr(void *m) if (msg->conn->pcb.ip != NULL) { if (msg->msg.ad.local) { - ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr), msg->conn->pcb.ip->local_ip); + ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr), + msg->conn->pcb.ip->local_ip); } else { - ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr), msg->conn->pcb.ip->remote_ip); + ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr), + msg->conn->pcb.ip->remote_ip); } msg->err = ERR_OK; @@ -1898,8 +1909,7 @@ lwip_netconn_do_getaddr(void *m) /* pcb is not connected and remote name is requested */ msg->err = ERR_CONN; } else { - API_EXPR_DEREF(msg->msg.ad.port) = - (msg->msg.ad.local ? msg->conn->pcb.tcp->local_port : msg->conn->pcb.tcp->remote_port); + API_EXPR_DEREF(msg->msg.ad.port) = (msg->msg.ad.local ? msg->conn->pcb.tcp->local_port : msg->conn->pcb.tcp->remote_port); } break; #endif /* LWIP_TCP */ @@ -1929,7 +1939,8 @@ lwip_netconn_do_close(void *m) enum netconn_state state = msg->conn->state; /* First check if this is a TCP netconn and if it is in a correct state (LISTEN doesn't support half shutdown) */ - if ((msg->conn->pcb.tcp != NULL) && (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) && + if ((msg->conn->pcb.tcp != NULL) && + (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) && ((msg->msg.sd.shut == NETCONN_SHUT_RDWR) || (state != NETCONN_LISTEN))) { /* Check if we are in a connected state */ if (state == NETCONN_CONNECT) { @@ -1955,7 +1966,7 @@ lwip_netconn_do_close(void *m) } } if (state == NETCONN_NONE) { -#else /* LWIP_NETCONN_FULLDUPLEX */ +#else /* LWIP_NETCONN_FULLDUPLEX */ msg->err = ERR_INPROGRESS; } else { #endif /* LWIP_NETCONN_FULLDUPLEX */ @@ -1963,7 +1974,7 @@ lwip_netconn_do_close(void *m) #if LWIP_NETCONN_FULLDUPLEX /* Mark mboxes invalid */ netconn_mark_mbox_invalid(msg->conn); -#else /* LWIP_NETCONN_FULLDUPLEX */ +#else /* LWIP_NETCONN_FULLDUPLEX */ netconn_drain(msg->conn); #endif /* LWIP_NETCONN_FULLDUPLEX */ } @@ -1978,7 +1989,7 @@ lwip_netconn_do_close(void *m) LOCK_TCPIP_CORE(); LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE); } -#else /* LWIP_TCPIP_CORE_LOCKING */ +#else /* LWIP_TCPIP_CORE_LOCKING */ lwip_netconn_do_close_internal(msg->conn); #endif /* LWIP_TCPIP_CORE_LOCKING */ /* for tcp netconns, lwip_netconn_do_close_internal ACKs the message */ @@ -2064,18 +2075,22 @@ lwip_netconn_do_join_leave_group_netif(void *m) #if LWIP_IPV6 && LWIP_IPV6_MLD if (NETCONNTYPE_ISIPV6(msg->conn->type)) { if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { - msg->err = mld6_joingroup_netif(netif, ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr))); + msg->err = mld6_joingroup_netif(netif, + ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr))); } else { - msg->err = mld6_leavegroup_netif(netif, ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr))); + msg->err = mld6_leavegroup_netif(netif, + ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr))); } } else #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ { #if LWIP_IGMP if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { - msg->err = igmp_joingroup_netif(netif, ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr))); + msg->err = igmp_joingroup_netif(netif, + ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr))); } else { - msg->err = igmp_leavegroup_netif(netif, ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr))); + msg->err = igmp_leavegroup_netif(netif, + ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr))); } #endif /* LWIP_IGMP */ } @@ -2135,8 +2150,8 @@ lwip_netconn_do_gethostbyname(void *arg) LWIP_DNS_ADDRTYPE_DEFAULT; #endif - API_EXPR_DEREF(msg->err) = - dns_gethostbyname_addrtype(msg->name, API_EXPR_REF(msg->addr), lwip_netconn_do_dns_found, msg, addrtype); + API_EXPR_DEREF(msg->err) = dns_gethostbyname_addrtype(msg->name, + API_EXPR_REF(msg->addr), lwip_netconn_do_dns_found, msg, addrtype); #if LWIP_TCPIP_CORE_LOCKING /* For core locking, only block if we need to wait for answer/timeout */ if (API_EXPR_DEREF(msg->err) == ERR_INPROGRESS) { @@ -2145,7 +2160,7 @@ lwip_netconn_do_gethostbyname(void *arg) LOCK_TCPIP_CORE(); LWIP_ASSERT("do_gethostbyname still in progress!!", API_EXPR_DEREF(msg->err) != ERR_INPROGRESS); } -#else /* LWIP_TCPIP_CORE_LOCKING */ +#else /* LWIP_TCPIP_CORE_LOCKING */ if (API_EXPR_DEREF(msg->err) != ERR_INPROGRESS) { /* on error or immediate success, wake up the application * task waiting in netconn_gethostbyname */ diff --git a/src/api/err.c b/src/api/err.c index fe7206fd..dd2b62da 100644 --- a/src/api/err.c +++ b/src/api/err.c @@ -44,25 +44,25 @@ #if !NO_SYS /** Table to quickly map an lwIP error (err_t) to a socket error - * by using -err as an index */ + * by using -err as an index */ static const int err_to_errno_table[] = { - 0, /* ERR_OK 0 No error, everything OK. */ - ENOMEM, /* ERR_MEM -1 Out of memory error. */ - ENOBUFS, /* ERR_BUF -2 Buffer error. */ - EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */ - EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */ - EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */ - EINVAL, /* ERR_VAL -6 Illegal value. */ - EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */ - EADDRINUSE, /* ERR_USE -8 Address in use. */ - EALREADY, /* ERR_ALREADY -9 Already connecting. */ - EISCONN, /* ERR_ISCONN -10 Conn already established.*/ - ENOTCONN, /* ERR_CONN -11 Not connected. */ - -1, /* ERR_IF -12 Low-level netif error */ - ECONNABORTED, /* ERR_ABRT -13 Connection aborted. */ - ECONNRESET, /* ERR_RST -14 Connection reset. */ - ENOTCONN, /* ERR_CLSD -15 Connection closed. */ - EIO /* ERR_ARG -16 Illegal argument. */ + 0, /* ERR_OK 0 No error, everything OK. */ + ENOMEM, /* ERR_MEM -1 Out of memory error. */ + ENOBUFS, /* ERR_BUF -2 Buffer error. */ + EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */ + EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */ + EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */ + EINVAL, /* ERR_VAL -6 Illegal value. */ + EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */ + EADDRINUSE, /* ERR_USE -8 Address in use. */ + EALREADY, /* ERR_ALREADY -9 Already connecting. */ + EISCONN, /* ERR_ISCONN -10 Conn already established.*/ + ENOTCONN, /* ERR_CONN -11 Not connected. */ + -1, /* ERR_IF -12 Low-level netif error */ + ECONNABORTED, /* ERR_ABRT -13 Connection aborted. */ + ECONNRESET, /* ERR_RST -14 Connection reset. */ + ENOTCONN, /* ERR_CLSD -15 Connection closed. */ + EIO /* ERR_ARG -16 Illegal argument. */ }; int diff --git a/src/api/if_api.c b/src/api/if_api.c index 234d8415..8e094d09 100644 --- a/src/api/if_api.c +++ b/src/api/if_api.c @@ -67,7 +67,7 @@ lwip_if_indextoname(unsigned int ifindex, char *ifname) return ifname; } } -#else /* LWIP_NETIF_API */ +#else /* LWIP_NETIF_API */ LWIP_UNUSED_ARG(ifindex); LWIP_UNUSED_ARG(ifname); #endif /* LWIP_NETIF_API */ @@ -93,9 +93,9 @@ lwip_if_nametoindex(const char *ifname) if (!err) { return idx; } -#else /* LWIP_NETIF_API */ +#else /* LWIP_NETIF_API */ LWIP_UNUSED_ARG(ifname); -#endif /* LWIP_NETIF_API */ +#endif /* LWIP_NETIF_API */ return 0; /* invalid index */ } diff --git a/src/api/netbuf.c b/src/api/netbuf.c index cef35361..3b910de1 100644 --- a/src/api/netbuf.c +++ b/src/api/netbuf.c @@ -46,8 +46,8 @@ #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */ -#include "lwip/memp.h" #include "lwip/netbuf.h" +#include "lwip/memp.h" #include @@ -59,8 +59,8 @@ * @return a pointer to a new netbuf * NULL on lack of memory */ -struct netbuf * -netbuf_new(void) +struct +netbuf *netbuf_new(void) { struct netbuf *buf; @@ -111,7 +111,8 @@ netbuf_alloc(struct netbuf *buf, u16_t size) if (buf->p == NULL) { return NULL; } - LWIP_ASSERT("check that first pbuf can hold size", (buf->p->len >= size)); + LWIP_ASSERT("check that first pbuf can hold size", + (buf->p->len >= size)); buf->ptr = buf->p; return buf->p->payload; } diff --git a/src/api/netdb.c b/src/api/netdb.c index 314f7944..87714259 100644 --- a/src/api/netdb.c +++ b/src/api/netdb.c @@ -39,19 +39,18 @@ #if LWIP_DNS && LWIP_SOCKET -#include "lwip/api.h" -#include "lwip/dns.h" #include "lwip/err.h" -#include "lwip/ip_addr.h" #include "lwip/mem.h" #include "lwip/memp.h" +#include "lwip/ip_addr.h" +#include "lwip/api.h" +#include "lwip/dns.h" -#include /* atoi */ #include /* memset */ +#include /* atoi */ /** helper struct for gethostbyname_r to access the char* buffer */ -struct gethostbyname_r_helper -{ +struct gethostbyname_r_helper { ip_addr_t *addr_list[2]; ip_addr_t addr; char *aliases; @@ -130,8 +129,7 @@ lwip_gethostbyname(const char *name) u8_t idx; for (idx = 0; s_hostent.h_addr_list[idx]; idx++) { LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx])); - LWIP_DEBUGF(DNS_DEBUG, - ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t *)s_hostent.h_addr_list[idx]))); + LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t *)s_hostent.h_addr_list[idx]))); } } #endif /* DNS_DEBUG */ @@ -161,12 +159,8 @@ lwip_gethostbyname(const char *name) * is stored in *h_errnop instead of h_errno to be thread-safe */ int -lwip_gethostbyname_r(const char *name, - struct hostent *ret, - char *buf, - size_t buflen, - struct hostent **result, - int *h_errnop) +lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf, + size_t buflen, struct hostent **result, int *h_errnop) { err_t err; struct gethostbyname_r_helper *h; @@ -272,7 +266,8 @@ lwip_freeaddrinfo(struct addrinfo *ai) * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG */ int -lwip_getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res) +lwip_getaddrinfo(const char *nodename, const char *servname, + const struct addrinfo *hints, struct addrinfo **res) { err_t err; ip_addr_t addr; @@ -300,7 +295,7 @@ lwip_getaddrinfo(const char *nodename, const char *servname, const struct addrin #if LWIP_IPV6 && (ai_family != AF_INET6) #endif /* LWIP_IPV6 */ - ) { + ) { return EAI_FAMILY; } } else { @@ -324,7 +319,8 @@ lwip_getaddrinfo(const char *nodename, const char *servname, const struct addrin return EAI_NONAME; } #if LWIP_IPV4 && LWIP_IPV6 - if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) || (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) { + if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) || + (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) { return EAI_NONAME; } #endif /* LWIP_IPV4 && LWIP_IPV6 */ @@ -363,7 +359,8 @@ lwip_getaddrinfo(const char *nodename, const char *servname, const struct addrin total_size += namelen + 1; } /* If this fails, please report to lwip-devel! :-) */ - LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!", total_size <= NETDB_ELEM_SIZE); + LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!", + total_size <= NETDB_ELEM_SIZE); ai = (struct addrinfo *)memp_malloc(MEMP_NETDB); if (ai == NULL) { return EAI_MEMORY; diff --git a/src/api/netifapi.c b/src/api/netifapi.c index 066f1f96..25957cd5 100644 --- a/src/api/netifapi.c +++ b/src/api/netifapi.c @@ -43,16 +43,16 @@ #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */ #include "lwip/etharp.h" -#include "lwip/memp.h" #include "lwip/netifapi.h" +#include "lwip/memp.h" #include "lwip/priv/tcpip_priv.h" #include /* strncpy */ -#define NETIFAPI_VAR_REF(name) API_VAR_REF(name) -#define NETIFAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct netifapi_msg, name) -#define NETIFAPI_VAR_ALLOC(name) API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM) -#define NETIFAPI_VAR_FREE(name) API_VAR_FREE(MEMP_NETIFAPI_MSG, name) +#define NETIFAPI_VAR_REF(name) API_VAR_REF(name) +#define NETIFAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct netifapi_msg, name) +#define NETIFAPI_VAR_ALLOC(name) API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM) +#define NETIFAPI_VAR_FREE(name) API_VAR_FREE(MEMP_NETIFAPI_MSG, name) /** * Call netif_add() inside the tcpip_thread context. @@ -64,15 +64,15 @@ netifapi_do_netif_add(struct tcpip_api_call_data *m) * We know it works because the structs have been instantiated as struct netifapi_msg */ struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m; - if (!netif_add(msg->netif, + if (!netif_add( msg->netif, #if LWIP_IPV4 - API_EXPR_REF(msg->msg.add.ipaddr), - API_EXPR_REF(msg->msg.add.netmask), - API_EXPR_REF(msg->msg.add.gw), + API_EXPR_REF(msg->msg.add.ipaddr), + API_EXPR_REF(msg->msg.add.netmask), + API_EXPR_REF(msg->msg.add.gw), #endif /* LWIP_IPV4 */ - msg->msg.add.state, - msg->msg.add.init, - msg->msg.add.input)) { + msg->msg.add.state, + msg->msg.add.init, + msg->msg.add.input)) { return ERR_IF; } else { return ERR_OK; @@ -90,15 +90,17 @@ netifapi_do_netif_set_addr(struct tcpip_api_call_data *m) * We know it works because the structs have been instantiated as struct netifapi_msg */ struct netifapi_msg *msg = (struct netifapi_msg *)(void *)m; - netif_set_addr( - msg->netif, API_EXPR_REF(msg->msg.add.ipaddr), API_EXPR_REF(msg->msg.add.netmask), API_EXPR_REF(msg->msg.add.gw)); + netif_set_addr( msg->netif, + API_EXPR_REF(msg->msg.add.ipaddr), + API_EXPR_REF(msg->msg.add.netmask), + API_EXPR_REF(msg->msg.add.gw)); return ERR_OK; } #endif /* LWIP_IPV4 */ /** - * Call netif_name_to_index() inside the tcpip_thread context. - */ +* Call netif_name_to_index() inside the tcpip_thread context. +*/ static err_t netifapi_do_name_to_index(struct tcpip_api_call_data *m) { @@ -111,8 +113,8 @@ netifapi_do_name_to_index(struct tcpip_api_call_data *m) } /** - * Call netif_index_to_name() inside the tcpip_thread context. - */ +* Call netif_index_to_name() inside the tcpip_thread context. +*/ static err_t netifapi_do_index_to_name(struct tcpip_api_call_data *m) { @@ -219,13 +221,9 @@ netifapi_arp_remove(const ip4_addr_t *ipaddr, enum netifapi_arp_entry type) err_t netifapi_netif_add(struct netif *netif, #if LWIP_IPV4 - const ip4_addr_t *ipaddr, - const ip4_addr_t *netmask, - const ip4_addr_t *gw, + const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, #endif /* LWIP_IPV4 */ - void *state, - netif_init_fn init, - netif_input_fn input) + void *state, netif_init_fn init, netif_input_fn input) { err_t err; NETIFAPI_VAR_DECLARE(msg); @@ -245,13 +243,13 @@ netifapi_netif_add(struct netif *netif, NETIFAPI_VAR_REF(msg).netif = netif; #if LWIP_IPV4 - NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr); + NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr); NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask); - NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw); + NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw); #endif /* LWIP_IPV4 */ - NETIFAPI_VAR_REF(msg).msg.add.state = state; - NETIFAPI_VAR_REF(msg).msg.add.init = init; - NETIFAPI_VAR_REF(msg).msg.add.input = input; + NETIFAPI_VAR_REF(msg).msg.add.state = state; + NETIFAPI_VAR_REF(msg).msg.add.init = init; + NETIFAPI_VAR_REF(msg).msg.add.input = input; err = tcpip_api_call(netifapi_do_netif_add, &API_VAR_REF(msg).call); NETIFAPI_VAR_FREE(msg); return err; @@ -266,7 +264,10 @@ netifapi_netif_add(struct netif *netif, * @note for params @see netif_set_addr() */ err_t -netifapi_netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw) +netifapi_netif_set_addr(struct netif *netif, + const ip4_addr_t *ipaddr, + const ip4_addr_t *netmask, + const ip4_addr_t *gw) { err_t err; NETIFAPI_VAR_DECLARE(msg); @@ -283,9 +284,9 @@ netifapi_netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4 } NETIFAPI_VAR_REF(msg).netif = netif; - NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr); + NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr); NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask); - NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw); + NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw); err = tcpip_api_call(netifapi_do_netif_set_addr, &API_VAR_REF(msg).call); NETIFAPI_VAR_FREE(msg); return err; @@ -299,7 +300,8 @@ netifapi_netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4 * @note use only for functions where there is only "netif" parameter. */ err_t -netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc, netifapi_errt_fn errtfunc) +netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc, + netifapi_errt_fn errtfunc) { err_t err; NETIFAPI_VAR_DECLARE(msg); @@ -314,13 +316,13 @@ netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc, netifapi_e } /** - * @ingroup netifapi_netif - * Call netif_name_to_index() in a thread-safe way by running that function inside the - * tcpip_thread context. - * - * @param name the interface name of the netif - * @param idx output index of the found netif - */ +* @ingroup netifapi_netif +* Call netif_name_to_index() in a thread-safe way by running that function inside the +* tcpip_thread context. +* +* @param name the interface name of the netif +* @param idx output index of the found netif +*/ err_t netifapi_netif_name_to_index(const char *name, u8_t *idx) { @@ -345,14 +347,14 @@ netifapi_netif_name_to_index(const char *name, u8_t *idx) } /** - * @ingroup netifapi_netif - * Call netif_index_to_name() in a thread-safe way by running that function inside the - * tcpip_thread context. - * - * @param idx the interface index of the netif - * @param name output name of the found netif, empty '\0' string if netif not found. - * name should be of at least NETIF_NAMESIZE bytes - */ +* @ingroup netifapi_netif +* Call netif_index_to_name() in a thread-safe way by running that function inside the +* tcpip_thread context. +* +* @param idx the interface index of the netif +* @param name output name of the found netif, empty '\0' string if netif not found. +* name should be of at least NETIF_NAMESIZE bytes +*/ err_t netifapi_netif_index_to_name(u8_t idx, char *name) { diff --git a/src/api/sockets.c b/src/api/sockets.c index ddccccbf..0fb83a11 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -41,19 +41,19 @@ #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ +#include "lwip/sockets.h" +#include "lwip/priv/sockets_priv.h" #include "lwip/api.h" #include "lwip/igmp.h" #include "lwip/inet.h" -#include "lwip/memp.h" -#include "lwip/mld6.h" -#include "lwip/netif.h" -#include "lwip/pbuf.h" -#include "lwip/priv/sockets_priv.h" -#include "lwip/priv/tcpip_priv.h" -#include "lwip/raw.h" -#include "lwip/sockets.h" #include "lwip/tcp.h" +#include "lwip/raw.h" #include "lwip/udp.h" +#include "lwip/memp.h" +#include "lwip/pbuf.h" +#include "lwip/netif.h" +#include "lwip/priv/tcpip_priv.h" +#include "lwip/mld6.h" #if LWIP_CHECKSUM_ON_COPY #include "lwip/inet_chksum.h" #endif @@ -73,145 +73,115 @@ #if !LWIP_NETCONN #undef LWIP_NETCONN #define LWIP_NETCONN 1 -#include "api_lib.c" #include "api_msg.c" +#include "api_lib.c" #include "netbuf.c" #undef LWIP_NETCONN #define LWIP_NETCONN 0 #endif -#define API_SELECT_CB_VAR_REF(name) API_VAR_REF(name) -#define API_SELECT_CB_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_select_cb, name) -#define API_SELECT_CB_VAR_ALLOC(name, retblock) API_VAR_ALLOC_EXT(struct lwip_select_cb, MEMP_SELECT_CB, name, retblock) -#define API_SELECT_CB_VAR_FREE(name) API_VAR_FREE(MEMP_SELECT_CB, name) +#define API_SELECT_CB_VAR_REF(name) API_VAR_REF(name) +#define API_SELECT_CB_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_select_cb, name) +#define API_SELECT_CB_VAR_ALLOC(name, retblock) API_VAR_ALLOC_EXT(struct lwip_select_cb, MEMP_SELECT_CB, name, retblock) +#define API_SELECT_CB_VAR_FREE(name) API_VAR_FREE(MEMP_SELECT_CB, name) #if LWIP_IPV4 -#define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) \ - do { \ - (sin)->sin_len = sizeof(struct sockaddr_in); \ - (sin)->sin_family = AF_INET; \ - (sin)->sin_port = lwip_htons((port)); \ - inet_addr_from_ip4addr(&(sin)->sin_addr, ipaddr); \ - memset((sin)->sin_zero, 0, SIN_ZERO_LEN); \ - } while (0) -#define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) \ - do { \ - inet_addr_to_ip4addr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \ - (port) = lwip_ntohs((sin)->sin_port); \ - } while (0) +#define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) do { \ + (sin)->sin_len = sizeof(struct sockaddr_in); \ + (sin)->sin_family = AF_INET; \ + (sin)->sin_port = lwip_htons((port)); \ + inet_addr_from_ip4addr(&(sin)->sin_addr, ipaddr); \ + memset((sin)->sin_zero, 0, SIN_ZERO_LEN); }while(0) +#define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) do { \ + inet_addr_to_ip4addr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \ + (port) = lwip_ntohs((sin)->sin_port); }while(0) #endif /* LWIP_IPV4 */ #if LWIP_IPV6 -#define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) \ - do { \ - (sin6)->sin6_len = sizeof(struct sockaddr_in6); \ - (sin6)->sin6_family = AF_INET6; \ - (sin6)->sin6_port = lwip_htons((port)); \ - (sin6)->sin6_flowinfo = 0; \ - inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); \ - (sin6)->sin6_scope_id = ip6_addr_zone(ipaddr); \ - } while (0) -#define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) \ - do { \ - inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \ - if (ip6_addr_has_scope(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { \ - ip6_addr_set_zone(ip_2_ip6(ipaddr), (u8_t)((sin6)->sin6_scope_id)); \ - } \ - (port) = lwip_ntohs((sin6)->sin6_port); \ - } while (0) +#define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) do { \ + (sin6)->sin6_len = sizeof(struct sockaddr_in6); \ + (sin6)->sin6_family = AF_INET6; \ + (sin6)->sin6_port = lwip_htons((port)); \ + (sin6)->sin6_flowinfo = 0; \ + inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); \ + (sin6)->sin6_scope_id = ip6_addr_zone(ipaddr); }while(0) +#define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) do { \ + inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \ + if (ip6_addr_has_scope(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { \ + ip6_addr_set_zone(ip_2_ip6(ipaddr), (u8_t)((sin6)->sin6_scope_id)); \ + } \ + (port) = lwip_ntohs((sin6)->sin6_port); }while(0) #endif /* LWIP_IPV6 */ #if LWIP_IPV4 && LWIP_IPV6 -static void -sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port); +static void sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port); -#define IS_SOCK_ADDR_LEN_VALID(namelen) \ - (((namelen) == sizeof(struct sockaddr_in)) || ((namelen) == sizeof(struct sockaddr_in6))) -#define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || ((name)->sa_family == AF_INET6)) -#define SOCK_ADDR_TYPE_MATCH(name, sock) \ - ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \ - (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type)))) -#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ - do { \ - if (IP_IS_ANY_TYPE_VAL(*ipaddr) || IP_IS_V6_VAL(*ipaddr)) { \ - IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6 *)(void *)(sockaddr), ip_2_ip6(ipaddr), port); \ - } else { \ - IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in *)(void *)(sockaddr), ip_2_ip4(ipaddr), port); \ - } \ - } while (0) +#define IS_SOCK_ADDR_LEN_VALID(namelen) (((namelen) == sizeof(struct sockaddr_in)) || \ + ((namelen) == sizeof(struct sockaddr_in6))) +#define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || \ + ((name)->sa_family == AF_INET6)) +#define SOCK_ADDR_TYPE_MATCH(name, sock) \ + ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \ + (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type)))) +#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) do { \ + if (IP_IS_ANY_TYPE_VAL(*ipaddr) || IP_IS_V6_VAL(*ipaddr)) { \ + IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port); \ + } else { \ + IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port); \ + } } while(0) #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) sockaddr_to_ipaddr_port(sockaddr, ipaddr, &(port)) -#define DOMAIN_TO_NETCONN_TYPE(domain, type) \ - (((domain) == AF_INET) ? (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6)) +#define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? \ + (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6)) #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */ -#define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6)) -#define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6) +#define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6)) +#define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6) #define SOCK_ADDR_TYPE_MATCH(name, sock) 1 -#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ - IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6 *)(void *)(sockaddr), ip_2_ip6(ipaddr), port) -#define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ - SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6 *)(const void *)(sockaddr), ipaddr, port) +#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ + IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port) +#define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ + SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, port) #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) #else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */ -#define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in)) -#define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET) +#define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in)) +#define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET) #define SOCK_ADDR_TYPE_MATCH(name, sock) 1 -#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ - IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in *)(void *)(sockaddr), ip_2_ip4(ipaddr), port) -#define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ - SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in *)(const void *)(sockaddr), ipaddr, port) +#define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ + IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port) +#define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ + SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, port) #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) #endif /* LWIP_IPV6 */ -#define IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) (((name)->sa_family == AF_UNSPEC) || IS_SOCK_ADDR_TYPE_VALID(name)) -#define SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock) \ - (((name)->sa_family == AF_UNSPEC) || SOCK_ADDR_TYPE_MATCH(name, sock)) -#define IS_SOCK_ADDR_ALIGNED(name) ((((mem_ptr_t)(name)) % 4) == 0) +#define IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) (((name)->sa_family == AF_UNSPEC) || \ + IS_SOCK_ADDR_TYPE_VALID(name)) +#define SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock) (((name)->sa_family == AF_UNSPEC) || \ + SOCK_ADDR_TYPE_MATCH(name, sock)) +#define IS_SOCK_ADDR_ALIGNED(name) ((((mem_ptr_t)(name)) % 4) == 0) -#define LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype) \ - do { \ - if ((optlen) < sizeof(opttype)) { \ - done_socket(sock); \ - return EINVAL; \ - } \ - } while (0) -#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, opttype) \ - do { \ - LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ - if ((sock)->conn == NULL) { \ - done_socket(sock); \ - return EINVAL; \ - } \ - } while (0) -#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype) \ - do { \ - LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ - if (((sock)->conn == NULL) || ((sock)->conn->pcb.tcp == NULL)) { \ - done_socket(sock); \ - return EINVAL; \ - } \ - } while (0) -#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, opttype, netconntype) \ - do { \ - LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype); \ - if (NETCONNTYPE_GROUP(netconn_type((sock)->conn)) != netconntype) { \ - done_socket(sock); \ - return ENOPROTOOPT; \ - } \ - } while (0) -#define LWIP_SETGETSOCKOPT_DATA_VAR_REF(name) API_VAR_REF(name) +#define LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype) do { if ((optlen) < sizeof(opttype)) { done_socket(sock); return EINVAL; }}while(0) +#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, opttype) do { \ + LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ + if ((sock)->conn == NULL) { done_socket(sock); return EINVAL; } }while(0) +#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype) do { \ + LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ + if (((sock)->conn == NULL) || ((sock)->conn->pcb.tcp == NULL)) { done_socket(sock); return EINVAL; } }while(0) +#define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, opttype, netconntype) do { \ + LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype); \ + if (NETCONNTYPE_GROUP(netconn_type((sock)->conn)) != netconntype) { done_socket(sock); return ENOPROTOOPT; } }while(0) + + +#define LWIP_SETGETSOCKOPT_DATA_VAR_REF(name) API_VAR_REF(name) #define LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_setgetsockopt_data, name) -#define LWIP_SETGETSOCKOPT_DATA_VAR_FREE(name) API_VAR_FREE(MEMP_SOCKET_SETGETSOCKOPT_DATA, name) +#define LWIP_SETGETSOCKOPT_DATA_VAR_FREE(name) API_VAR_FREE(MEMP_SOCKET_SETGETSOCKOPT_DATA, name) #if LWIP_MPU_COMPATIBLE -#define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) \ - do { \ - name = (struct lwip_setgetsockopt_data *)memp_malloc(MEMP_SOCKET_SETGETSOCKOPT_DATA); \ - if (name == NULL) { \ - sock_set_errno(sock, ENOMEM); \ - done_socket(sock); \ - return -1; \ - } \ - } while (0) +#define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) do { \ + name = (struct lwip_setgetsockopt_data *)memp_malloc(MEMP_SOCKET_SETGETSOCKOPT_DATA); \ + if (name == NULL) { \ + sock_set_errno(sock, ENOMEM); \ + done_socket(sock); \ + return -1; \ + } }while(0) #else /* LWIP_MPU_COMPATIBLE */ #define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) #endif /* LWIP_MPU_COMPATIBLE */ @@ -219,24 +189,21 @@ sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_ #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD #define LWIP_SO_SNDRCVTIMEO_OPTTYPE int #define LWIP_SO_SNDRCVTIMEO_SET(optval, val) (*(int *)(optval) = (val)) -#define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((long)*(const int *)(optval)) +#define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((long)*(const int*)(optval)) #else #define LWIP_SO_SNDRCVTIMEO_OPTTYPE struct timeval -#define LWIP_SO_SNDRCVTIMEO_SET(optval, val) \ - do { \ - u32_t loc = (val); \ - ((struct timeval *)(optval))->tv_sec = (long)((loc) / 1000U); \ - ((struct timeval *)(optval))->tv_usec = (long)(((loc) % 1000U) * 1000U); \ - } while (0) -#define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) \ - ((((const struct timeval *)(optval))->tv_sec * 1000) + (((const struct timeval *)(optval))->tv_usec / 1000)) +#define LWIP_SO_SNDRCVTIMEO_SET(optval, val) do { \ + u32_t loc = (val); \ + ((struct timeval *)(optval))->tv_sec = (long)((loc) / 1000U); \ + ((struct timeval *)(optval))->tv_usec = (long)(((loc) % 1000U) * 1000U); }while(0) +#define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((((const struct timeval *)(optval))->tv_sec * 1000) + (((const struct timeval *)(optval))->tv_usec / 1000)) #endif + /** A struct sockaddr replacement that has the same alignment as sockaddr_in/ * sockaddr_in6 if instantiated. */ -union sockaddr_aligned -{ +union sockaddr_aligned { struct sockaddr sa; #if LWIP_IPV6 struct sockaddr_in6 sin6; @@ -254,8 +221,7 @@ union sockaddr_aligned #if LWIP_IGMP /* This is to keep track of IP_ADD_MEMBERSHIP calls to drop the membership when a socket is closed */ -struct lwip_socket_multicast_pair -{ +struct lwip_socket_multicast_pair { /** the socket */ struct lwip_sock *sock; /** the interface address */ @@ -266,19 +232,15 @@ struct lwip_socket_multicast_pair static struct lwip_socket_multicast_pair socket_ipv4_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS]; -static int -lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); -static void -lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); -static void -lwip_socket_drop_registered_memberships(int s); +static int lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); +static void lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); +static void lwip_socket_drop_registered_memberships(int s); #endif /* LWIP_IGMP */ #if LWIP_IPV6_MLD /* This is to keep track of IP_JOIN_GROUP calls to drop the membership when a socket is closed */ -struct lwip_socket_multicast_mld6_pair -{ +struct lwip_socket_multicast_mld6_pair { /** the socket */ struct lwip_sock *sock; /** the interface index */ @@ -289,12 +251,9 @@ struct lwip_socket_multicast_mld6_pair static struct lwip_socket_multicast_mld6_pair socket_ipv6_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS]; -static int -lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); -static void -lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); -static void -lwip_socket_drop_registered_mld6_memberships(int s); +static int lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); +static void lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); +static void lwip_socket_drop_registered_mld6_memberships(int s); #endif /* LWIP_IPV6_MLD */ /** The global array of available sockets */ @@ -304,13 +263,13 @@ static struct lwip_sock sockets[NUM_SOCKETS]; #if LWIP_TCPIP_CORE_LOCKING /* protect the select_cb_list using core lock */ #define LWIP_SOCKET_SELECT_DECL_PROTECT(lev) -#define LWIP_SOCKET_SELECT_PROTECT(lev) LOCK_TCPIP_CORE() +#define LWIP_SOCKET_SELECT_PROTECT(lev) LOCK_TCPIP_CORE() #define LWIP_SOCKET_SELECT_UNPROTECT(lev) UNLOCK_TCPIP_CORE() #else /* LWIP_TCPIP_CORE_LOCKING */ /* protect the select_cb_list using SYS_LIGHTWEIGHT_PROT */ -#define LWIP_SOCKET_SELECT_DECL_PROTECT(lev) SYS_ARCH_DECL_PROTECT(lev) -#define LWIP_SOCKET_SELECT_PROTECT(lev) SYS_ARCH_PROTECT(lev) -#define LWIP_SOCKET_SELECT_UNPROTECT(lev) SYS_ARCH_UNPROTECT(lev) +#define LWIP_SOCKET_SELECT_DECL_PROTECT(lev) SYS_ARCH_DECL_PROTECT(lev) +#define LWIP_SOCKET_SELECT_PROTECT(lev) SYS_ARCH_PROTECT(lev) +#define LWIP_SOCKET_SELECT_UNPROTECT(lev) SYS_ARCH_UNPROTECT(lev) /** This counter is increased from lwip_select when the list is changed and checked in select_check_waiters to see if it has changed. */ static volatile int select_cb_ctr; @@ -319,36 +278,28 @@ static volatile int select_cb_ctr; static struct lwip_select_cb *select_cb_list; #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */ -#define sock_set_errno(sk, e) \ - do { \ - const int sockerr = (e); \ - set_errno(sockerr); \ - } while (0) +#define sock_set_errno(sk, e) do { \ + const int sockerr = (e); \ + set_errno(sockerr); \ +} while (0) /* Forward declaration of some functions */ #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL -static void -event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len); +static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len); #define DEFAULT_SOCKET_EVENTCB event_callback -static void -select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent); +static void select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent); #else #define DEFAULT_SOCKET_EVENTCB NULL #endif #if !LWIP_TCPIP_CORE_LOCKING -static void -lwip_getsockopt_callback(void *arg); -static void -lwip_setsockopt_callback(void *arg); +static void lwip_getsockopt_callback(void *arg); +static void lwip_setsockopt_callback(void *arg); #endif -static int -lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen); -static int -lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen); -static int -free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata); -static void -free_socket_free_elements(int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata); +static int lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen); +static int lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen); +static int free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, + union lwip_sock_lastdata *lastdata); +static void free_socket_free_elements(int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata); #if LWIP_IPV4 && LWIP_IPV6 static void @@ -450,8 +401,8 @@ done_socket(struct lwip_sock *sock) } #else /* LWIP_NETCONN_FULLDUPLEX */ -#define sock_inc_used(sock) 1 -#define sock_inc_used_locked(sock) 1 +#define sock_inc_used(sock) 1 +#define sock_inc_used_locked(sock) 1 #define done_socket(sock) #endif /* LWIP_NETCONN_FULLDUPLEX */ @@ -563,21 +514,21 @@ alloc_socket(struct netconn *newconn, int accepted) SYS_ARCH_UNPROTECT(lev); continue; } - sockets[i].fd_used = 1; + sockets[i].fd_used = 1; sockets[i].fd_free_pending = 0; #endif - sockets[i].conn = newconn; + sockets[i].conn = newconn; /* The socket is not yet known to anyone, so no need to protect after having marked it as used. */ SYS_ARCH_UNPROTECT(lev); sockets[i].lastdata.pbuf = NULL; #if LWIP_SOCKET_SELECT LWIP_ASSERT("sockets[i].select_waiting == 0", sockets[i].select_waiting == 0); - sockets[i].rcvevent = 0; + sockets[i].rcvevent = 0; /* TCP sendbuf is empty, but the socket is not yet writable until connected * (unless it has been created by accept()). */ - sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1); - sockets[i].errevent = 0; + sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1); + sockets[i].errevent = 0; #endif /* LWIP_SOCKET_SELECT */ return i + LWIP_SOCKET_OFFSET; } @@ -594,7 +545,8 @@ alloc_socket(struct netconn *newconn, int accepted) * @param lastdata lastdata is stored here, must be freed externally */ static int -free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata) +free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, + union lwip_sock_lastdata *lastdata) { #if LWIP_NETCONN_FULLDUPLEX LWIP_ASSERT("sock->fd_used > 0", sock->fd_used > 0); @@ -603,7 +555,7 @@ free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, un sock->fd_free_pending = LWIP_SOCK_FD_FREE_FREE | (is_tcp ? LWIP_SOCK_FD_FREE_TCP : 0); return 0; } -#else /* LWIP_NETCONN_FULLDUPLEX */ +#else /* LWIP_NETCONN_FULLDUPLEX */ LWIP_UNUSED_ARG(is_tcp); #endif /* LWIP_NETCONN_FULLDUPLEX */ @@ -751,7 +703,7 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock)); ip_addr_debug_print_val(SOCKETS_DEBUG, naddr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F "\n", port)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port)); } else { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d", s, newsock)); } @@ -783,17 +735,15 @@ lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) } /* check size, family and alignment of 'name' */ - LWIP_ERROR("lwip_bind: invalid address", - (IS_SOCK_ADDR_LEN_VALID(namelen) && IS_SOCK_ADDR_TYPE_VALID(name) && IS_SOCK_ADDR_ALIGNED(name)), - sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); + LWIP_ERROR("lwip_bind: invalid address", (IS_SOCK_ADDR_LEN_VALID(namelen) && + IS_SOCK_ADDR_TYPE_VALID(name) && IS_SOCK_ADDR_ALIGNED(name)), + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_UNUSED_ARG(namelen); SOCKADDR_TO_IPADDR_PORT(name, &local_addr, local_port); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, local_addr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F ")\n", local_port)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", local_port)); #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ @@ -886,16 +836,14 @@ lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) u16_t remote_port; /* check size, family and alignment of 'name' */ - LWIP_ERROR("lwip_connect: invalid address", - IS_SOCK_ADDR_LEN_VALID(namelen) && IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) && IS_SOCK_ADDR_ALIGNED(name), - sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); + LWIP_ERROR("lwip_connect: invalid address", IS_SOCK_ADDR_LEN_VALID(namelen) && + IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) && IS_SOCK_ADDR_ALIGNED(name), + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); SOCKADDR_TO_IPADDR_PORT(name, &remote_addr, remote_port); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, remote_addr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F ")\n", remote_port)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", remote_port)); #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ @@ -995,7 +943,8 @@ lwip_recv_tcp(struct lwip_sock *sock, void *mem, size_t len, int flags) /* No data was left from the previous operation, so we try to get some from the network. */ err = netconn_recv_tcp_pbuf_flags(sock->conn, &p, apiflags); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: netconn_recv err=%d, pbuf=%p\n", err, (void *)p)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: netconn_recv err=%d, pbuf=%p\n", + err, (void *)p)); if (err != ERR_OK) { if (recvd > 0) { @@ -1004,7 +953,8 @@ lwip_recv_tcp(struct lwip_sock *sock, void *mem, size_t len, int flags) goto lwip_recv_tcp_done; } /* We should really do some error checking here. */ - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: p == NULL, error is \"%s\"!\n", lwip_strerr(err))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: p == NULL, error is \"%s\"!\n", + lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); if (err == ERR_CLSD) { return 0; @@ -1016,8 +966,8 @@ lwip_recv_tcp(struct lwip_sock *sock, void *mem, size_t len, int flags) sock->lastdata.pbuf = p; } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_recv_tcp: buflen=%" U16_F " recv_left=%d off=%d\n", p->tot_len, (int)recv_left, (int)recvd)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: buflen=%"U16_F" recv_left=%d off=%d\n", + p->tot_len, (int)recv_left, (int)recvd)); if (recv_left > p->tot_len) { copylen = p->tot_len; @@ -1070,7 +1020,8 @@ lwip_recv_tcp_done: /* Convert a netbuf's address data to struct sockaddr */ static int -lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port, struct sockaddr *from, socklen_t *fromlen) +lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port, + struct sockaddr *from, socklen_t *fromlen) { int truncated = 0; union sockaddr_aligned saddr; @@ -1102,12 +1053,7 @@ lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port, struc #if LWIP_TCP /* Helper function to get a tcp socket's remote address info */ static int -lwip_recv_tcp_from(struct lwip_sock *sock, - struct sockaddr *from, - socklen_t *fromlen, - const char *dbg_fn, - int dbg_s, - ssize_t dbg_ret) +lwip_recv_tcp_from(struct lwip_sock *sock, struct sockaddr *from, socklen_t *fromlen, const char *dbg_fn, int dbg_s, ssize_t dbg_ret) { if (sock == NULL) { return 0; @@ -1126,7 +1072,7 @@ lwip_recv_tcp_from(struct lwip_sock *sock, netconn_getaddr(sock->conn, &tmpaddr, &port, 0); LWIP_DEBUGF(SOCKETS_DEBUG, ("%s(%d): addr=", dbg_fn, dbg_s)); ip_addr_debug_print_val(SOCKETS_DEBUG, tmpaddr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F " len=%d\n", port, (int)dbg_ret)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, (int)dbg_ret)); if (from && fromlen) { return lwip_sock_make_addr(sock->conn, &tmpaddr, port, from, fromlen); } @@ -1148,8 +1094,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16 int i; LWIP_UNUSED_ARG(dbg_s); - LWIP_ERROR( - "lwip_recvfrom_udp_raw: invalid arguments", (msg->msg_iov != NULL) || (msg->msg_iovlen <= 0), return ERR_ARG;); + LWIP_ERROR("lwip_recvfrom_udp_raw: invalid arguments", (msg->msg_iov != NULL) || (msg->msg_iovlen <= 0), return ERR_ARG;); if (flags & MSG_DONTWAIT) { apiflags = NETCONN_DONTBLOCK; @@ -1157,15 +1102,15 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16 apiflags = 0; } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf)); /* Check if there is data left from the last recv operation. */ buf = sock->lastdata.netbuf; if (buf == NULL) { /* No data was left from the previous operation, so we try to get some from the network. */ err = netconn_recv_udp_raw_netbuf_flags(sock->conn, &buf, apiflags); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: netconn_recv err=%d, netbuf=%p\n", err, (void *)buf)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: netconn_recv err=%d, netbuf=%p\n", + err, (void *)buf)); if (err != ERR_OK) { return err; @@ -1174,7 +1119,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16 sock->lastdata.netbuf = buf; } buflen = buf->p->tot_len; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw: buflen=%" U16_F "\n", buflen)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw: buflen=%"U16_F"\n", buflen)); copied = 0; /* copy the pbuf payload into the iovs */ @@ -1199,10 +1144,10 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16 { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw(%d): addr=", dbg_s)); ip_addr_debug_print_val(SOCKETS_DEBUG, *netbuf_fromaddr(buf)); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F " len=%d\n", netbuf_fromport(buf), copied)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", netbuf_fromport(buf), copied)); if (msg->msg_name && msg->msg_namelen) { - lwip_sock_make_addr( - sock->conn, netbuf_fromaddr(buf), netbuf_fromport(buf), (struct sockaddr *)msg->msg_name, &msg->msg_namelen); + lwip_sock_make_addr(sock->conn, netbuf_fromaddr(buf), netbuf_fromport(buf), + (struct sockaddr *)msg->msg_name, &msg->msg_namelen); } } @@ -1251,12 +1196,13 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16 } ssize_t -lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) +lwip_recvfrom(int s, void *mem, size_t len, int flags, + struct sockaddr *from, socklen_t *fromlen) { struct lwip_sock *sock; ssize_t ret; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %" SZT_F ", 0x%x, ..)\n", s, mem, len, flags)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags)); sock = get_socket(s); if (!sock) { return -1; @@ -1285,7 +1231,8 @@ lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, so msg.msg_namelen = (fromlen ? *fromlen : 0); err = lwip_recvfrom_udp_raw(sock, flags, &msg, &datagram_len, s); if (err != ERR_OK) { - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", s, lwip_strerr(err))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", + s, lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; @@ -1339,8 +1286,8 @@ lwip_recvmsg(int s, struct msghdr *message, int flags) LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvmsg(%d, message=%p, flags=0x%x)\n", s, (void *)message, flags)); LWIP_ERROR("lwip_recvmsg: invalid message pointer", message != NULL, return ERR_ARG;); - LWIP_ERROR("lwip_recvmsg: unsupported flags", (flags & ~(MSG_PEEK | MSG_DONTWAIT)) == 0, set_errno(EOPNOTSUPP); - return -1;); + LWIP_ERROR("lwip_recvmsg: unsupported flags", (flags & ~(MSG_PEEK|MSG_DONTWAIT)) == 0, + set_errno(EOPNOTSUPP); return -1;); if ((message->msg_iovlen <= 0) || (message->msg_iovlen > IOV_MAX)) { set_errno(EMSGSIZE); @@ -1378,7 +1325,8 @@ lwip_recvmsg(int s, struct msghdr *message, int flags) /* sum up received bytes */ buflen += recvd_local; } - if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) || (flags & MSG_PEEK)) { + if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) || + (flags & MSG_PEEK)) { /* returned prematurely (or peeking, which might actually be limitated to the first iov) */ if (buflen <= 0) { /* nothing received at all, propagate the error */ @@ -1396,7 +1344,7 @@ lwip_recvmsg(int s, struct msghdr *message, int flags) /* " If the socket is connected, the msg_name and msg_namelen members shall be ignored." */ done_socket(sock); return buflen; -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1; @@ -1409,7 +1357,8 @@ lwip_recvmsg(int s, struct msghdr *message, int flags) err_t err; err = lwip_recvfrom_udp_raw(sock, flags, message, &datagram_len, s); if (err != ERR_OK) { - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvmsg[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", s, lwip_strerr(err))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvmsg[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", + s, lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; @@ -1422,7 +1371,7 @@ lwip_recvmsg(int s, struct msghdr *message, int flags) done_socket(sock); return (int)datagram_len; } -#else /* LWIP_UDP || LWIP_RAW */ +#else /* LWIP_UDP || LWIP_RAW */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1; @@ -1437,7 +1386,8 @@ lwip_send(int s, const void *data, size_t size, int flags) u8_t write_flags; size_t written; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%" SZT_F ", flags=0x%x)\n", s, data, size, flags)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n", + s, data, size, flags)); sock = get_socket(s); if (!sock) { @@ -1448,19 +1398,20 @@ lwip_send(int s, const void *data, size_t size, int flags) #if (LWIP_UDP || LWIP_RAW) done_socket(sock); return lwip_sendto(s, data, size, flags, NULL, 0); -#else /* (LWIP_UDP || LWIP_RAW) */ +#else /* (LWIP_UDP || LWIP_RAW) */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1; #endif /* (LWIP_UDP || LWIP_RAW) */ } - write_flags = - (u8_t)(NETCONN_COPY | ((flags & MSG_MORE) ? NETCONN_MORE : 0) | ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); + write_flags = (u8_t)(NETCONN_COPY | + ((flags & MSG_MORE) ? NETCONN_MORE : 0) | + ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); written = 0; err = netconn_write_partly(sock->conn, data, size, write_flags, &written); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d written=%" SZT_F "\n", s, err, written)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d written=%"SZT_F"\n", s, err, written)); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); /* casting 'written' to ssize_t is OK here since the netconn API limits it to SSIZE_MAX */ @@ -1482,21 +1433,14 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) return -1; } - LWIP_ERROR("lwip_sendmsg: invalid msghdr", msg != NULL, sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); - LWIP_ERROR("lwip_sendmsg: invalid msghdr iov", msg->msg_iov != NULL, sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); - LWIP_ERROR("lwip_sendmsg: maximum iovs exceeded", - (msg->msg_iovlen > 0) && (msg->msg_iovlen <= IOV_MAX), - sock_set_errno(sock, EMSGSIZE); - done_socket(sock); - return -1;); - LWIP_ERROR( - "lwip_sendmsg: unsupported flags", (flags & ~(MSG_DONTWAIT | MSG_MORE)) == 0, sock_set_errno(sock, EOPNOTSUPP); - done_socket(sock); - return -1;); + LWIP_ERROR("lwip_sendmsg: invalid msghdr", msg != NULL, + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); + LWIP_ERROR("lwip_sendmsg: invalid msghdr iov", msg->msg_iov != NULL, + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); + LWIP_ERROR("lwip_sendmsg: maximum iovs exceeded", (msg->msg_iovlen > 0) && (msg->msg_iovlen <= IOV_MAX), + sock_set_errno(sock, EMSGSIZE); done_socket(sock); return -1;); + LWIP_ERROR("lwip_sendmsg: unsupported flags", (flags & ~(MSG_DONTWAIT | MSG_MORE)) == 0, + sock_set_errno(sock, EOPNOTSUPP); done_socket(sock); return -1;); LWIP_UNUSED_ARG(msg->msg_control); LWIP_UNUSED_ARG(msg->msg_controllen); @@ -1504,17 +1448,17 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { #if LWIP_TCP - write_flags = - (u8_t)(NETCONN_COPY | ((flags & MSG_MORE) ? NETCONN_MORE : 0) | ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); + write_flags = (u8_t)(NETCONN_COPY | + ((flags & MSG_MORE) ? NETCONN_MORE : 0) | + ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); written = 0; - err = netconn_write_vectors_partly( - sock->conn, (struct netvector *)msg->msg_iov, (u16_t)msg->msg_iovlen, write_flags, &written); + err = netconn_write_vectors_partly(sock->conn, (struct netvector *)msg->msg_iov, (u16_t)msg->msg_iovlen, write_flags, &written); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); /* casting 'written' to ssize_t is OK here since the netconn API limits it to SSIZE_MAX */ return (err == ERR_OK ? (ssize_t)written : -1); -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1; @@ -1528,11 +1472,9 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) ssize_t size = 0; LWIP_UNUSED_ARG(flags); - LWIP_ERROR("lwip_sendmsg: invalid msghdr name", - (((msg->msg_name == NULL) && (msg->msg_namelen == 0)) || IS_SOCK_ADDR_LEN_VALID(msg->msg_namelen)), - sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); + LWIP_ERROR("lwip_sendmsg: invalid msghdr name", (((msg->msg_name == NULL) && (msg->msg_namelen == 0)) || + IS_SOCK_ADDR_LEN_VALID(msg->msg_namelen)), + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); /* initialize chain buffer with destination */ memset(&chain_buf, 0, sizeof(struct netbuf)); @@ -1572,7 +1514,7 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) #endif /* LWIP_CHECKSUM_ON_COPY */ err = ERR_OK; } -#else /* LWIP_NETIF_TX_SINGLE_PBUF */ +#else /* LWIP_NETIF_TX_SINGLE_PBUF */ /* create a chained netbuf from the IO vectors. NOTE: we assemble a pbuf chain manually to avoid having to allocate, chain, and delete a netbuf for each iov */ for (i = 0; i < msg->msg_iovlen; i++) { @@ -1626,13 +1568,13 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return (err == ERR_OK ? size : -1); - sendmsg_emsgsize: +sendmsg_emsgsize: sock_set_errno(sock, EMSGSIZE); netbuf_free(&chain_buf); done_socket(sock); return -1; } -#else /* LWIP_UDP || LWIP_RAW */ +#else /* LWIP_UDP || LWIP_RAW */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1; @@ -1640,7 +1582,8 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) } ssize_t -lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen) +lwip_sendto(int s, const void *data, size_t size, int flags, + const struct sockaddr *to, socklen_t tolen) { struct lwip_sock *sock; err_t err; @@ -1657,7 +1600,7 @@ lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockad #if LWIP_TCP done_socket(sock); return lwip_send(s, data, size, flags); -#else /* LWIP_TCP */ +#else /* LWIP_TCP */ LWIP_UNUSED_ARG(flags); sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); @@ -1672,13 +1615,10 @@ lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockad return -1; } short_size = (u16_t)size; - LWIP_ERROR( - "lwip_sendto: invalid address", - (((to == NULL) && (tolen == 0)) || - (IS_SOCK_ADDR_LEN_VALID(tolen) && ((to != NULL) && (IS_SOCK_ADDR_TYPE_VALID(to) && IS_SOCK_ADDR_ALIGNED(to))))), - sock_set_errno(sock, err_to_errno(ERR_ARG)); - done_socket(sock); - return -1;); + LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) || + (IS_SOCK_ADDR_LEN_VALID(tolen) && + ((to != NULL) && (IS_SOCK_ADDR_TYPE_VALID(to) && IS_SOCK_ADDR_ALIGNED(to))))), + sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_UNUSED_ARG(tolen); /* initialize a buffer */ @@ -1694,10 +1634,11 @@ lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockad } netbuf_fromport(&buf) = remote_port; - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_sendto(%d, data=%p, short_size=%" U16_F ", flags=0x%x to=", s, data, short_size, flags)); + + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=", + s, data, short_size, flags)); ip_addr_debug_print_val(SOCKETS_DEBUG, buf.addr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F "\n", remote_port)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port)); /* make the buffer point to the data that should be sent */ #if LWIP_NETIF_TX_SINGLE_PBUF @@ -1716,7 +1657,7 @@ lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockad } err = ERR_OK; } -#else /* LWIP_NETIF_TX_SINGLE_PBUF */ +#else /* LWIP_NETIF_TX_SINGLE_PBUF */ err = netbuf_ref(&buf, data, short_size); #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ if (err == ERR_OK) { @@ -1751,17 +1692,17 @@ lwip_socket(int domain, int type, int protocol) /* create a netconn */ switch (type) { case SOCK_RAW: - conn = netconn_new_with_proto_and_callback( - DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_RAW), (u8_t)protocol, DEFAULT_SOCKET_EVENTCB); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); + conn = netconn_new_with_proto_and_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_RAW), + (u8_t)protocol, DEFAULT_SOCKET_EVENTCB); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", + domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); break; case SOCK_DGRAM: - conn = netconn_new_with_callback( - DOMAIN_TO_NETCONN_TYPE(domain, ((protocol == IPPROTO_UDPLITE) ? NETCONN_UDPLITE : NETCONN_UDP)), - DEFAULT_SOCKET_EVENTCB); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); + conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain, + ((protocol == IPPROTO_UDPLITE) ? NETCONN_UDPLITE : NETCONN_UDP)), + DEFAULT_SOCKET_EVENTCB); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", + domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); #if LWIP_NETBUF_RECVINFO if (conn) { /* netconn layer enables pktinfo by default, sockets default to off */ @@ -1771,11 +1712,12 @@ lwip_socket(int domain, int type, int protocol) break; case SOCK_STREAM: conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_TCP), DEFAULT_SOCKET_EVENTCB); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", + domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", + domain, type, protocol)); set_errno(EINVAL); return -1; } @@ -1890,13 +1832,8 @@ lwip_unlink_select_cb(struct lwip_select_cb *select_cb) * @return number of sockets that had events (read/write/exception) (>= 0) */ static int -lwip_selscan(int maxfdp1, - fd_set *readset_in, - fd_set *writeset_in, - fd_set *exceptset_in, - fd_set *readset_out, - fd_set *writeset_out, - fd_set *exceptset_out) +lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in, + fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out) { int i, nready = 0; fd_set lreadset, lwriteset, lexceptset; @@ -1911,7 +1848,8 @@ lwip_selscan(int maxfdp1, currently match */ for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { /* if this FD is not in the set, continue */ - if (!(readset_in && FD_ISSET(i, readset_in)) && !(writeset_in && FD_ISSET(i, writeset_in)) && + if (!(readset_in && FD_ISSET(i, readset_in)) && + !(writeset_in && FD_ISSET(i, writeset_in)) && !(exceptset_in && FD_ISSET(i, exceptset_in))) { continue; } @@ -2023,7 +1961,8 @@ lwip_select_dec_sockets_used(int maxfdp, fd_set *used_sockets) #endif /* LWIP_NETCONN_FULLDUPLEX */ int -lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) +lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, + struct timeval *timeout) { u32_t waitres = 0; int nready; @@ -2039,14 +1978,10 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s #endif SYS_ARCH_DECL_PROTECT(lev); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_select(%d, %p, %p, %p, tvsec=%" S32_F " tvusec=%" S32_F ")\n", - maxfdp1, - (void *)readset, - (void *)writeset, - (void *)exceptset, - timeout ? (s32_t)timeout->tv_sec : (s32_t)-1, - timeout ? (s32_t)timeout->tv_usec : (s32_t)-1)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n", + maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset, + timeout ? (s32_t)timeout->tv_sec : (s32_t) - 1, + timeout ? (s32_t)timeout->tv_usec : (s32_t) - 1)); if ((maxfdp1 < 0) || (maxfdp1 > LWIP_SELECT_MAXNFDS)) { set_errno(EINVAL); @@ -2080,8 +2015,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s to use local variables (unless we're running in MPU compatible mode). */ API_SELECT_CB_VAR_DECLARE(select_cb); - API_SELECT_CB_VAR_ALLOC(select_cb, set_errno(ENOMEM); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); - return -1); + API_SELECT_CB_VAR_ALLOC(select_cb, set_errno(ENOMEM); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); return -1); memset(&API_SELECT_CB_VAR_REF(select_cb), 0, sizeof(struct lwip_select_cb)); API_SELECT_CB_VAR_REF(select_cb).readset = readset; @@ -2089,7 +2023,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s API_SELECT_CB_VAR_REF(select_cb).exceptset = exceptset; #if LWIP_NETCONN_SEM_PER_THREAD API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET(); -#else /* LWIP_NETCONN_SEM_PER_THREAD */ +#else /* LWIP_NETCONN_SEM_PER_THREAD */ if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { /* failed to create semaphore */ set_errno(ENOMEM); @@ -2104,7 +2038,8 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s /* Increase select_waiting for each socket we are interested in */ maxfdp2 = maxfdp1; for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { - if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || + if ((readset && FD_ISSET(i, readset)) || + (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { struct lwip_sock *sock; SYS_ARCH_PROTECT(lev); @@ -2162,7 +2097,8 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s /* Decrease select_waiting for each socket we are interested in */ for (i = LWIP_SOCKET_OFFSET; i < maxfdp2; i++) { - if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || + if ((readset && FD_ISSET(i, readset)) || + (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { struct lwip_sock *sock; SYS_ARCH_PROTECT(lev); @@ -2191,7 +2127,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, s /* don't leave the thread-local semaphore signalled */ sys_arch_sem_wait(API_SELECT_CB_VAR_REF(select_cb).sem, 1); } -#else /* LWIP_NETCONN_SEM_PER_THREAD */ +#else /* LWIP_NETCONN_SEM_PER_THREAD */ sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem); #endif /* LWIP_NETCONN_SEM_PER_THREAD */ API_SELECT_CB_VAR_FREE(select_cb); @@ -2276,7 +2212,7 @@ lwip_pollscan(struct pollfd *fds, nfds_t nfds, enum lwip_pollscan_opts opts) SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(fds[fdi].fd); if (sock != NULL) { - void *lastdata = sock->lastdata.pbuf; + void* lastdata = sock->lastdata.pbuf; s16_t rcvevent = sock->rcvevent; u16_t sendevent = sock->sendevent; u16_t errevent = sock->errevent; @@ -2349,7 +2285,7 @@ lwip_poll_inc_sockets_used(struct pollfd *fds, nfds_t nfds) { nfds_t fdi; - if (fds) { + if(fds) { /* Go through each struct pollfd in the array. */ for (fdi = 0; fdi < nfds; fdi++) { /* Increase the reference counter */ @@ -2364,7 +2300,7 @@ lwip_poll_dec_sockets_used(struct pollfd *fds, nfds_t nfds) { nfds_t fdi; - if (fds) { + if(fds) { /* Go through each struct pollfd in the array. */ for (fdi = 0; fdi < nfds; fdi++) { struct lwip_sock *sock = tryget_socket_unconn_nouse(fds[fdi].fd); @@ -2390,9 +2326,10 @@ lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout) int waited = 0; #endif - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll(%p, %d, %d)\n", (void *)fds, (int)nfds, timeout)); - LWIP_ERROR("lwip_poll: invalid fds", ((fds != NULL && nfds > 0) || (fds == NULL && nfds == 0)), set_errno(EINVAL); - return -1;); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll(%p, %d, %d)\n", + (void*)fds, (int)nfds, timeout)); + LWIP_ERROR("lwip_poll: invalid fds", ((fds != NULL && nfds > 0) || (fds == NULL && nfds == 0)), + set_errno(EINVAL); return -1;); lwip_poll_inc_sockets_used(fds, nfds); @@ -2425,7 +2362,7 @@ lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout) API_SELECT_CB_VAR_REF(select_cb).poll_nfds = nfds; #if LWIP_NETCONN_SEM_PER_THREAD API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET(); -#else /* LWIP_NETCONN_SEM_PER_THREAD */ +#else /* LWIP_NETCONN_SEM_PER_THREAD */ if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { /* failed to create semaphore */ set_errno(EAGAIN); @@ -2469,7 +2406,7 @@ lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout) /* don't leave the thread-local semaphore signalled */ sys_arch_sem_wait(API_SELECT_CB_VAR_REF(select_cb).sem, 1); } -#else /* LWIP_NETCONN_SEM_PER_THREAD */ +#else /* LWIP_NETCONN_SEM_PER_THREAD */ sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem); #endif /* LWIP_NETCONN_SEM_PER_THREAD */ API_SELECT_CB_VAR_FREE(select_cb); @@ -2637,8 +2574,7 @@ event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len) * select_cb_list during our UNPROTECT/PROTECT. We use a generational counter to * detect this change and restart the list walk. The list is expected to be small */ -static void -select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent) +static void select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent) { struct lwip_select_cb *scb; #if !LWIP_TCPIP_CORE_LOCKING @@ -2785,7 +2721,8 @@ lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local) #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Map IPv4 addresses to IPv4 mapped IPv6 */ - if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && IP_IS_V4_VAL(naddr)) { + if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && + IP_IS_V4_VAL(naddr)) { ip4_2_ipv4_mapped_ipv6(ip_2_ip6(&naddr), ip_2_ip4(&naddr)); IP_SET_TYPE_VAL(naddr, IPADDR_TYPE_V6); } @@ -2795,7 +2732,7 @@ lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local) LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, naddr); - LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%" U16_F ")\n", port)); + LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", port)); if (*namelen > saddr.sa.sa_len) { *namelen = saddr.sa.sa_len; @@ -2882,7 +2819,8 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) /* write back optlen and optval */ *optlen = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen; #if LWIP_MPU_COMPATIBLE - MEMCPY(optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen); + MEMCPY(optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, + LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen); #endif /* LWIP_MPU_COMPATIBLE */ /* maybe lwip_getsockopt_internal has changed err */ @@ -2906,19 +2844,17 @@ lwip_getsockopt_callback(void *arg) LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data *)arg; - data->err = lwip_getsockopt_impl(data->s, - data->level, - data->optname, + data->err = lwip_getsockopt_impl(data->s, data->level, data->optname, #if LWIP_MPU_COMPATIBLE data->optval, -#else /* LWIP_MPU_COMPATIBLE */ +#else /* LWIP_MPU_COMPATIBLE */ data->optval.p, #endif /* LWIP_MPU_COMPATIBLE */ &data->optlen); sys_sem_signal((sys_sem_t *)(data->completed_sem)); } -#endif /* LWIP_TCPIP_CORE_LOCKING */ +#endif /* LWIP_TCPIP_CORE_LOCKING */ static int lwip_sockopt_to_ipopt(int optname) @@ -2928,15 +2864,15 @@ lwip_sockopt_to_ipopt(int optname) * being in sync with ip.h. */ switch (optname) { - case SO_BROADCAST: - return SOF_BROADCAST; - case SO_KEEPALIVE: - return SOF_KEEPALIVE; - case SO_REUSEADDR: - return SOF_REUSEADDR; - default: - LWIP_ASSERT("Unknown socket option", 0); - return 0; + case SO_BROADCAST: + return SOF_BROADCAST; + case SO_KEEPALIVE: + return SOF_KEEPALIVE; + case SO_REUSEADDR: + return SOF_REUSEADDR; + default: + LWIP_ASSERT("Unknown socket option", 0); + return 0; } } @@ -2985,7 +2921,8 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt #if SO_REUSE case SO_REUSEADDR: #endif /* SO_REUSE */ - if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { + if ((optname == SO_BROADCAST) && + (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { done_socket(sock); return ENOPROTOOPT; } @@ -2994,9 +2931,8 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = ip_get_option(sock->conn->pcb.ip, optname); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", s, optname, (*(int *)optval ? "on" : "off"))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", + s, optname, (*(int *)optval ? "on" : "off"))); break; case SO_TYPE: @@ -3013,17 +2949,19 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt break; default: /* unrecognized socket type */ *(int *)optval = netconn_type(sock->conn); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", s, *(int *)optval)); - } /* switch (netconn_type(sock->conn)) */ - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, + ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", + s, *(int *)optval)); + } /* switch (netconn_type(sock->conn)) */ + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", + s, *(int *)optval)); break; case SO_ERROR: LWIP_SOCKOPT_CHECK_OPTLEN(sock, *optlen, int); *(int *)optval = err_to_errno(netconn_err(sock->conn)); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", + s, *(int *)optval)); break; #if LWIP_SO_SNDTIMEO @@ -3057,7 +2995,8 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt linger->l_onoff = 0; linger->l_linger = 0; } - } break; + } + break; #endif /* LWIP_SO_LINGER */ #if LWIP_UDP case SO_NO_CHECK: @@ -3073,10 +3012,11 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt break; #endif /* LWIP_UDP*/ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; /* Level: IPPROTO_IP */ @@ -3085,12 +3025,14 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt case IP_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = sock->conn->pcb.ip->ttl; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", + s, *(int *)optval)); break; case IP_TOS: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = sock->conn->pcb.ip->tos; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", + s, *(int *)optval)); break; #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP case IP_MULTICAST_TTL: @@ -3100,7 +3042,8 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt return ENOPROTOOPT; } *(u8_t *)optval = udp_get_multicast_ttl(sock->conn->pcb.udp); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n", + s, *(int *)optval)); break; case IP_MULTICAST_IF: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct in_addr); @@ -3109,8 +3052,8 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt return ENOPROTOOPT; } inet_addr_from_ip4addr((struct in_addr *)optval, udp_get_multicast_netif_addr(sock->conn->pcb.udp)); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%" X32_F "\n", s, *(u32_t *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n", + s, *(u32_t *)optval)); break; case IP_MULTICAST_LOOP: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t); @@ -3119,14 +3062,16 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt } else { *(u8_t *)optval = 0; } - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n", + s, *(int *)optval)); break; #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #if LWIP_TCP @@ -3141,33 +3086,38 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt switch (optname) { case TCP_NODELAY: *(int *)optval = tcp_nagle_disabled(sock->conn->pcb.tcp); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", s, (*(int *)optval) ? "on" : "off")); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", + s, (*(int *)optval) ? "on" : "off") ); break; case TCP_KEEPALIVE: *(int *)optval = (int)sock->conn->pcb.tcp->keep_idle; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) = %d\n", + s, *(int *)optval)); break; #if LWIP_TCP_KEEPALIVE case TCP_KEEPIDLE: *(int *)optval = (int)(sock->conn->pcb.tcp->keep_idle / 1000); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) = %d\n", + s, *(int *)optval)); break; case TCP_KEEPINTVL: *(int *)optval = (int)(sock->conn->pcb.tcp->keep_intvl / 1000); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) = %d\n", + s, *(int *)optval)); break; case TCP_KEEPCNT: *(int *)optval = (int)sock->conn->pcb.tcp->keep_cnt; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) = %d\n", + s, *(int *)optval)); break; #endif /* LWIP_TCP_KEEPALIVE */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_TCP */ @@ -3178,13 +3128,15 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt case IPV6_V6ONLY: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int); *(int *)optval = (netconn_get_ipv6only(sock->conn) ? 1 : 0); - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY) = %d\n", s, *(int *)optval)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY) = %d\n", + s, *(int *)optval)); break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_IPV6 */ @@ -3201,19 +3153,20 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt switch (optname) { case UDPLITE_SEND_CSCOV: *(int *)optval = sock->conn->pcb.udp->chksum_len_tx; - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n", s, (*(int *)optval))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n", + s, (*(int *)optval)) ); break; case UDPLITE_RECV_CSCOV: *(int *)optval = sock->conn->pcb.udp->chksum_len_rx; - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n", s, (*(int *)optval))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n", + s, (*(int *)optval)) ); break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_UDP */ /* Level: IPPROTO_RAW */ @@ -3227,17 +3180,20 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt } else { *(int *)optval = sock->conn->pcb.raw->chksum_offset; } - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM) = %d\n", s, (*(int *)optval))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM) = %d\n", + s, (*(int *)optval)) ); break; #endif /* LWIP_IPV6 && LWIP_RAW */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", + s, level, optname)); err = ENOPROTOOPT; break; } /* switch (level) */ @@ -3290,7 +3246,7 @@ lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t opt LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = optlen; #if LWIP_MPU_COMPATIBLE MEMCPY(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, optval, optlen); -#else /* LWIP_MPU_COMPATIBLE */ +#else /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.pc = (const void *)optval; #endif /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0; @@ -3311,7 +3267,7 @@ lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t opt /* maybe lwip_getsockopt_internal has changed err */ err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err; LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data); -#endif /* LWIP_TCPIP_CORE_LOCKING */ +#endif /* LWIP_TCPIP_CORE_LOCKING */ sock_set_errno(sock, err); done_socket(sock); @@ -3329,19 +3285,17 @@ lwip_setsockopt_callback(void *arg) LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data *)arg; - data->err = lwip_setsockopt_impl(data->s, - data->level, - data->optname, + data->err = lwip_setsockopt_impl(data->s, data->level, data->optname, #if LWIP_MPU_COMPATIBLE data->optval, -#else /* LWIP_MPU_COMPATIBLE */ +#else /* LWIP_MPU_COMPATIBLE */ data->optval.pc, #endif /* LWIP_MPU_COMPATIBLE */ data->optlen); sys_sem_signal((sys_sem_t *)(data->completed_sem)); } -#endif /* LWIP_TCPIP_CORE_LOCKING */ +#endif /* LWIP_TCPIP_CORE_LOCKING */ /** lwip_setsockopt_impl: the actual implementation of setsockopt: * same argument as lwip_setsockopt, either called directly or through callback @@ -3375,7 +3329,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ #if SO_REUSE case SO_REUSEADDR: #endif /* SO_REUSE */ - if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { + if ((optname == SO_BROADCAST) && + (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { done_socket(sock); return ENOPROTOOPT; } @@ -3388,11 +3343,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { ip_reset_option(sock->conn->pcb.ip, optname); } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", - s, - optname, - (*(const int *)optval ? "on" : "off"))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", + s, optname, (*(const int *)optval ? "on" : "off"))); break; /* SO_TYPE is get-only */ @@ -3447,7 +3399,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { sock->conn->linger = -1; } - } break; + } + break; #endif /* LWIP_SO_LINGER */ #if LWIP_UDP case SO_NO_CHECK: @@ -3501,12 +3454,14 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ LWIP_ASSERT("Unhandled netconn type in SO_BINDTODEVICE", 0); break; } - } break; + } + break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; /* Level: IPPROTO_IP */ @@ -3515,14 +3470,14 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ case IP_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); sock->conn->pcb.ip->ttl = (u8_t)(*(const int *)optval); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n", s, sock->conn->pcb.ip->ttl)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n", + s, sock->conn->pcb.ip->ttl)); break; case IP_TOS: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); sock->conn->pcb.ip->tos = (u8_t)(*(const int *)optval); - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n", s, sock->conn->pcb.ip->tos)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n", + s, sock->conn->pcb.ip->tos)); break; #if LWIP_NETBUF_RECVINFO case IP_PKTINFO: @@ -3544,7 +3499,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct in_addr, NETCONN_UDP); inet_addr_to_ip4addr(&if_addr, (const struct in_addr *)optval); udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr); - } break; + } + break; case IP_MULTICAST_LOOP: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP); if (*(const u8_t *)optval) { @@ -3580,13 +3536,15 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ if (igmp_err != ERR_OK) { err = EADDRNOTAVAIL; } - } break; + } + break; #endif /* LWIP_IGMP */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #if LWIP_TCP @@ -3605,42 +3563,38 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { tcp_nagle_enable(sock->conn->pcb.tcp); } - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", s, (*(const int *)optval) ? "on" : "off")); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", + s, (*(const int *)optval) ? "on" : "off") ); break; case TCP_KEEPALIVE: sock->conn->pcb.tcp->keep_idle = (u32_t)(*(const int *)optval); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %" U32_F "\n", s, sock->conn->pcb.tcp->keep_idle)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n", + s, sock->conn->pcb.tcp->keep_idle)); break; #if LWIP_TCP_KEEPALIVE case TCP_KEEPIDLE: sock->conn->pcb.tcp->keep_idle = 1000 * (u32_t)(*(const int *)optval); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %" U32_F "\n", s, sock->conn->pcb.tcp->keep_idle)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n", + s, sock->conn->pcb.tcp->keep_idle)); break; case TCP_KEEPINTVL: sock->conn->pcb.tcp->keep_intvl = 1000 * (u32_t)(*(const int *)optval); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %" U32_F "\n", s, sock->conn->pcb.tcp->keep_intvl)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n", + s, sock->conn->pcb.tcp->keep_intvl)); break; case TCP_KEEPCNT: sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(const int *)optval); - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %" U32_F "\n", s, sock->conn->pcb.tcp->keep_cnt)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n", + s, sock->conn->pcb.tcp->keep_cnt)); break; #endif /* LWIP_TCP_KEEPALIVE */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_TCP*/ @@ -3655,10 +3609,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { netconn_set_ipv6only(sock->conn, 0); } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY, ..) -> %d\n", - s, - (netconn_get_ipv6only(sock->conn) ? 1 : 0))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY, ..) -> %d\n", + s, (netconn_get_ipv6only(sock->conn) ? 1 : 0))); break; #if LWIP_IPV6_MLD case IPV6_JOIN_GROUP: @@ -3692,13 +3644,15 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ if (mld6_err != ERR_OK) { err = EADDRNOTAVAIL; } - } break; + } + break; #endif /* LWIP_IPV6_MLD */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_IPV6 */ @@ -3720,8 +3674,8 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { sock->conn->pcb.udp->chksum_len_tx = (u16_t) * (const int *)optval; } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n", s, (*(const int *)optval))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n", + s, (*(const int *)optval)) ); break; case UDPLITE_RECV_CSCOV: if ((*(const int *)optval != 0) && ((*(const int *)optval < 8) || (*(const int *)optval > 0xffff))) { @@ -3730,14 +3684,15 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } else { sock->conn->pcb.udp->chksum_len_rx = (u16_t) * (const int *)optval; } - LWIP_DEBUGF(SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n", s, (*(const int *)optval))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n", + s, (*(const int *)optval)) ); break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; #endif /* LWIP_UDP */ /* Level: IPPROTO_RAW */ @@ -3763,22 +3718,23 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ sock->conn->pcb.raw->chksum_reqd = 1; sock->conn->pcb.raw->chksum_offset = (u16_t) * (const int *)optval; } - LWIP_DEBUGF( - SOCKETS_DEBUG, - ("lwip_setsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM, ..) -> %d\n", s, sock->conn->pcb.raw->chksum_reqd)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM, ..) -> %d\n", + s, sock->conn->pcb.raw->chksum_reqd)); break; #endif /* LWIP_IPV6 && LWIP_RAW */ default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", s, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", + s, optname)); err = ENOPROTOOPT; break; - } /* switch (optname) */ + } /* switch (optname) */ break; default: - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname)); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", + s, level, optname)); err = ENOPROTOOPT; break; - } /* switch (level) */ + } /* switch (level) */ done_socket(sock); return err; @@ -3843,11 +3799,11 @@ lwip_ioctl(int s, long cmd, void *argp) } *((int *)argp) = recv_avail; - LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %" U16_F "\n", s, argp, *((u16_t *)argp))); + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t *)argp))); sock_set_errno(sock, 0); done_socket(sock); return 0; -#else /* LWIP_SO_RCVBUF */ +#else /* LWIP_SO_RCVBUF */ break; #endif /* LWIP_SO_RCVBUF */ #endif /* LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE */ @@ -3926,8 +3882,7 @@ lwip_fcntl(int s, int cmd, int val) break; case F_SETFL: - /* Bits corresponding to the file access mode and the file creation flags [..] that are set in arg shall be - * ignored */ + /* Bits corresponding to the file access mode and the file creation flags [..] that are set in arg shall be ignored */ val &= ~(O_RDONLY | O_WRONLY | O_RDWR); if ((val & ~O_NONBLOCK) == 0) { /* only O_NONBLOCK, all other bits are zero */ @@ -4131,7 +4086,7 @@ lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_ for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if (socket_ipv6_multicast_memberships[i].sock == NULL) { - socket_ipv6_multicast_memberships[i].sock = sock; + socket_ipv6_multicast_memberships[i].sock = sock; socket_ipv6_multicast_memberships[i].if_idx = (u8_t)if_idx; ip6_addr_copy(socket_ipv6_multicast_memberships[i].multi_addr, *multi_addr); done_socket(sock); @@ -4158,10 +4113,10 @@ lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_add } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { - if ((socket_ipv6_multicast_memberships[i].sock == sock) && + if ((socket_ipv6_multicast_memberships[i].sock == sock) && (socket_ipv6_multicast_memberships[i].if_idx == if_idx) && ip6_addr_cmp(&socket_ipv6_multicast_memberships[i].multi_addr, multi_addr)) { - socket_ipv6_multicast_memberships[i].sock = NULL; + socket_ipv6_multicast_memberships[i].sock = NULL; socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX; ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr); break; @@ -4192,7 +4147,7 @@ lwip_socket_drop_registered_mld6_memberships(int s) ip_addr_copy_from_ip6(multi_addr, socket_ipv6_multicast_memberships[i].multi_addr); if_idx = socket_ipv6_multicast_memberships[i].if_idx; - socket_ipv6_multicast_memberships[i].sock = NULL; + socket_ipv6_multicast_memberships[i].sock = NULL; socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX; ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr); diff --git a/src/api/tcpip.c b/src/api/tcpip.c index f1e0fe24..743553a5 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -40,20 +40,20 @@ #if !NO_SYS /* don't build if not configured for use in lwipopts.h */ -#include "lwip/etharp.h" -#include "lwip/init.h" -#include "lwip/ip.h" -#include "lwip/mem.h" -#include "lwip/memp.h" -#include "lwip/pbuf.h" #include "lwip/priv/tcpip_priv.h" #include "lwip/sys.h" +#include "lwip/memp.h" +#include "lwip/mem.h" +#include "lwip/init.h" +#include "lwip/ip.h" +#include "lwip/pbuf.h" +#include "lwip/etharp.h" #include "netif/ethernet.h" -#define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name) +#define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name) #define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name) -#define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM) -#define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name) +#define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM) +#define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name) /* global variables */ static tcpip_init_done_fn tcpip_init_done; @@ -65,8 +65,7 @@ static sys_mbox_t tcpip_mbox; sys_mutex_t lock_tcpip_core; #endif /* LWIP_TCPIP_CORE_LOCKING */ -static void -tcpip_thread_handle_msg(struct tcpip_msg *msg); +static void tcpip_thread_handle_msg(struct tcpip_msg *msg); #if !LWIP_TIMERS /* wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */ @@ -137,7 +136,7 @@ tcpip_thread(void *arg) tcpip_init_done(tcpip_init_done_arg); } - while (1) { /* MAIN Loop */ + while (1) { /* MAIN Loop */ LWIP_TCPIP_THREAD_ALIVE(); /* wait for a message, timeouts are processed while waiting */ TCPIP_MBOX_FETCH(&tcpip_mbox, (void **)&msg); @@ -247,7 +246,7 @@ tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn) ret = input_fn(p, inp); UNLOCK_TCPIP_CORE(); return ret; -#else /* LWIP_TCPIP_CORE_LOCKING_INPUT */ +#else /* LWIP_TCPIP_CORE_LOCKING_INPUT */ struct tcpip_msg *msg; LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox)); @@ -421,6 +420,7 @@ tcpip_untimeout(sys_timeout_handler h, void *arg) } #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */ + /** * Sends a message to TCPIP thread to call a function. Caller thread blocks on * on a provided semaphore, which ist NOT automatically signalled by TCPIP thread, @@ -442,7 +442,7 @@ tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem) fn(apimsg); UNLOCK_TCPIP_CORE(); return ERR_OK; -#else /* LWIP_TCPIP_CORE_LOCKING */ +#else /* LWIP_TCPIP_CORE_LOCKING */ TCPIP_MSG_VAR_DECLARE(msg); LWIP_ASSERT("semaphore not initialized", sys_sem_valid(sem)); @@ -496,7 +496,7 @@ tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call) TCPIP_MSG_VAR_REF(msg).msg.api_call.function = fn; #if LWIP_NETCONN_SEM_PER_THREAD TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = LWIP_NETCONN_THREAD_SEM_GET(); -#else /* LWIP_NETCONN_SEM_PER_THREAD */ +#else /* LWIP_NETCONN_SEM_PER_THREAD */ TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = &call->sem; #endif /* LWIP_NETCONN_SEM_PER_THREAD */ sys_mbox_post(&tcpip_mbox, &TCPIP_MSG_VAR_REF(msg)); @@ -519,7 +519,7 @@ tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call) * e.g. the message is allocated once and posted several times from an IRQ * using tcpip_callbackmsg_trycallback(). * Example usage: Trigger execution of an ethernet IRQ DPC routine in lwIP thread context. - * + * * @param function the function to call * @param ctx parameter passed to function * @return a struct pointer to pass to tcpip_callbackmsg_trycallback(). diff --git a/src/core/ipv4/autoip.c b/src/core/ipv4/autoip.c index 0bbf7540..9f7139bc 100644 --- a/src/core/ipv4/autoip.c +++ b/src/core/ipv4/autoip.c @@ -62,10 +62,10 @@ #include "lwip/mem.h" /* #include "lwip/udp.h" */ -#include "lwip/autoip.h" -#include "lwip/etharp.h" #include "lwip/ip_addr.h" #include "lwip/netif.h" +#include "lwip/autoip.h" +#include "lwip/etharp.h" #include "lwip/prot/autoip.h" #include @@ -73,10 +73,11 @@ /** Pseudo random macro based on netif informations. * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */ #ifndef LWIP_AUTOIP_RAND -#define LWIP_AUTOIP_RAND(netif) \ - ((((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \ - ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | ((u32_t)((netif->hwaddr[4]) & 0xff))) + \ - (netif_autoip_data(netif) ? netif_autoip_data(netif)->tried_llipaddr : 0)) +#define LWIP_AUTOIP_RAND(netif) ( (((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | \ + ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \ + ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \ + ((u32_t)((netif->hwaddr[4]) & 0xff))) + \ + (netif_autoip_data(netif)? netif_autoip_data(netif)->tried_llipaddr : 0)) #endif /* LWIP_AUTOIP_RAND */ /** @@ -84,15 +85,14 @@ * If you want to override this, define it to something else in lwipopts.h. */ #ifndef LWIP_AUTOIP_CREATE_SEED_ADDR -#define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \ - lwip_htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8))) +#define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \ + lwip_htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \ + ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8))) #endif /* LWIP_AUTOIP_CREATE_SEED_ADDR */ /* static functions */ -static err_t -autoip_arp_announce(struct netif *netif); -static void -autoip_start_probing(struct netif *netif); +static err_t autoip_arp_announce(struct netif *netif); +static void autoip_start_probing(struct netif *netif); /** * @ingroup autoip @@ -108,7 +108,8 @@ autoip_set_struct(struct netif *netif, struct autoip *autoip) LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("autoip != NULL", autoip != NULL); - LWIP_ASSERT("netif already has a struct autoip set", netif_autoip_data(netif) == NULL); + LWIP_ASSERT("netif already has a struct autoip set", + netif_autoip_data(netif) == NULL); /* clear data structure */ memset(autoip, 0, sizeof(struct autoip)); @@ -184,16 +185,14 @@ autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr) if (addr > AUTOIP_RANGE_END) { addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1; } - LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) && (addr <= AUTOIP_RANGE_END)); + LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) && + (addr <= AUTOIP_RANGE_END)); ip4_addr_set_u32(ipaddr, lwip_htonl(addr)); LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("autoip_create_addr(): tried_llipaddr=%" U16_F ", %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - (u16_t)(autoip->tried_llipaddr), - ip4_addr1_16(ipaddr), - ip4_addr2_16(ipaddr), - ip4_addr3_16(ipaddr), - ip4_addr4_16(ipaddr))); + ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + (u16_t)(autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), + ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr))); } /** @@ -232,15 +231,10 @@ autoip_bind(struct netif *netif) ip4_addr_t sn_mask, gw_addr; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, - ("autoip_bind(netif=%p) %c%c%" U16_F " %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - (void *)netif, - netif->name[0], - netif->name[1], - (u16_t)netif->num, - ip4_addr1_16(&autoip->llipaddr), - ip4_addr2_16(&autoip->llipaddr), - ip4_addr3_16(&autoip->llipaddr), - ip4_addr4_16(&autoip->llipaddr))); + ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num, + ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), + ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); IP4_ADDR(&sn_mask, 255, 255, 0, 0); IP4_ADDR(&gw_addr, 0, 0, 0, 0); @@ -271,15 +265,17 @@ autoip_start(struct netif *netif) */ netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); - LWIP_DEBUGF( - AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("autoip_start(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, + ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], + netif->name[1], (u16_t)netif->num)); if (autoip == NULL) { /* no AutoIP client attached yet? */ - LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): starting new AUTOIP client\n")); + LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, + ("autoip_start(): starting new AUTOIP client\n")); autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip)); if (autoip == NULL) { - LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): could not allocate autoip\n")); + LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, + ("autoip_start(): could not allocate autoip\n")); return ERR_MEM; } /* store this AutoIP client in the netif */ @@ -307,11 +303,9 @@ autoip_start_probing(struct netif *netif) autoip->state = AUTOIP_STATE_PROBING; autoip->sent_num = 0; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("autoip_start_probing(): changing state to PROBING: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(&autoip->llipaddr), - ip4_addr2_16(&autoip->llipaddr), - ip4_addr3_16(&autoip->llipaddr), - ip4_addr4_16(&autoip->llipaddr))); + ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), + ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); /* time to wait to first probe, this is randomly * chosen out of 0 to PROBE_WAIT seconds. @@ -374,8 +368,7 @@ autoip_tmr(void) { struct netif *netif; /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { struct autoip *autoip = netif_autoip_data(netif); /* only act on AutoIP configured interfaces */ if (autoip != NULL) { @@ -384,7 +377,8 @@ autoip_tmr(void) } LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, - ("autoip_tmr() AutoIP-State: %" U16_F ", ttw=%" U16_F "\n", (u16_t)(autoip->state), autoip->ttw)); + ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n", + (u16_t)(autoip->state), autoip->ttw)); if (autoip->ttw > 0) { autoip->ttw--; @@ -402,11 +396,9 @@ autoip_tmr(void) autoip->sent_num = 1; autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("autoip_tmr(): changing state to ANNOUNCING: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(&autoip->llipaddr), - ip4_addr2_16(&autoip->llipaddr), - ip4_addr3_16(&autoip->llipaddr), - ip4_addr4_16(&autoip->llipaddr))); + ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), + ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); } else { autoip_arp_probe(netif); LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() PROBING Sent Probe\n")); @@ -416,7 +408,8 @@ autoip_tmr(void) autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND; } else { /* calculate time to wait to next probe */ - autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) % ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND)) + + autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) % + ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) + PROBE_MIN * AUTOIP_TICKS_PER_SECOND); } } @@ -435,11 +428,9 @@ autoip_tmr(void) autoip->sent_num = 0; autoip->ttw = 0; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("autoip_tmr(): changing state to BOUND: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(&autoip->llipaddr), - ip4_addr2_16(&autoip->llipaddr), - ip4_addr3_16(&autoip->llipaddr), - ip4_addr4_16(&autoip->llipaddr))); + ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), + ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); } } break; @@ -488,7 +479,8 @@ autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) * ip.dst == llipaddr && hw.src != own hwaddr */ if ((ip4_addr_cmp(&sipaddr, &autoip->llipaddr)) || - (ip4_addr_isany_val(sipaddr) && ip4_addr_cmp(&dipaddr, &autoip->llipaddr) && + (ip4_addr_isany_val(sipaddr) && + ip4_addr_cmp(&dipaddr, &autoip->llipaddr) && !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) { LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, ("autoip_arp_reply(): Probe Conflict detected\n")); @@ -499,7 +491,8 @@ autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) * in any state we have a conflict if * ip.src == llipaddr && hw.src != own hwaddr */ - if (ip4_addr_cmp(&sipaddr, &autoip->llipaddr) && !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) { + if (ip4_addr_cmp(&sipaddr, &autoip->llipaddr) && + !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) { LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, ("autoip_arp_reply(): Conflicting ARP-Packet detected\n")); autoip_handle_arp_conflict(netif); diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index ea891351..24eb059c 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -67,18 +67,18 @@ #if LWIP_IPV4 && LWIP_DHCP /* don't build if not configured for use in lwipopts.h */ -#include "lwip/autoip.h" +#include "lwip/stats.h" +#include "lwip/mem.h" +#include "lwip/udp.h" +#include "lwip/ip_addr.h" +#include "lwip/netif.h" #include "lwip/def.h" #include "lwip/dhcp.h" +#include "lwip/autoip.h" #include "lwip/dns.h" #include "lwip/etharp.h" -#include "lwip/ip_addr.h" -#include "lwip/mem.h" -#include "lwip/netif.h" #include "lwip/prot/dhcp.h" #include "lwip/prot/iana.h" -#include "lwip/stats.h" -#include "lwip/udp.h" #include @@ -89,17 +89,14 @@ #define LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, state, msg, msg_type, options_len_ptr) #endif #ifndef LWIP_HOOK_DHCP_PARSE_OPTION -#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) \ - do { \ - LWIP_UNUSED_ARG(msg); \ - } while (0) +#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) do { LWIP_UNUSED_ARG(msg); } while(0) #endif /** DHCP_CREATE_RAND_XID: if this is set to 1, the xid is created using * LWIP_RAND() (this overrides DHCP_GLOBAL_XID) */ #ifndef DHCP_CREATE_RAND_XID -#define DHCP_CREATE_RAND_XID 1 +#define DHCP_CREATE_RAND_XID 1 #endif /** Default for DHCP_GLOBAL_XID is 0xABCD0000 @@ -113,12 +110,12 @@ /** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU * MTU is checked to be big enough in dhcp_start */ -#define DHCP_MAX_MSG_LEN(netif) (netif->mtu) -#define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576 +#define DHCP_MAX_MSG_LEN(netif) (netif->mtu) +#define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576 /** Minimum length for reply before packet is parsed */ -#define DHCP_MIN_REPLY_LEN 44 +#define DHCP_MIN_REPLY_LEN 44 -#define REBOOT_TRIES 2 +#define REBOOT_TRIES 2 #if LWIP_DNS && LWIP_DHCP_MAX_DNS_SERVERS #if DNS_MAX_SERVERS > LWIP_DHCP_MAX_DNS_SERVERS @@ -135,8 +132,7 @@ * This might be moved into the struct dhcp (not necessarily since * lwIP is single-threaded and the array is only used while in recv * callback). */ -enum dhcp_option_idx -{ +enum dhcp_option_idx { DHCP_OPTION_IDX_OVERLOAD = 0, DHCP_OPTION_IDX_MSG_TYPE, DHCP_OPTION_IDX_SERVER_ID, @@ -162,18 +158,17 @@ u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX]; /** Holds a flag which option was received and is contained in dhcp_rx_options_val, only valid while in dhcp_recv. @todo: move this into struct dhcp? */ -u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX]; +u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX]; -static u8_t dhcp_discover_request_options[] = { DHCP_OPTION_SUBNET_MASK, - DHCP_OPTION_ROUTER, - DHCP_OPTION_BROADCAST +static u8_t dhcp_discover_request_options[] = { + DHCP_OPTION_SUBNET_MASK, + DHCP_OPTION_ROUTER, + DHCP_OPTION_BROADCAST #if LWIP_DHCP_PROVIDE_DNS_SERVERS - , - DHCP_OPTION_DNS_SERVER + , DHCP_OPTION_DNS_SERVER #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */ #if LWIP_DHCP_GET_NTP_SRV - , - DHCP_OPTION_NTP + , DHCP_OPTION_NTP #endif /* LWIP_DHCP_GET_NTP_SRV */ }; @@ -182,67 +177,49 @@ static u32_t xid; static u8_t xid_initialised; #endif /* DHCP_GLOBAL_XID */ -#define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0) -#define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1) -#define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0) -#define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given))) -#define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx]) +#define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0) +#define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1) +#define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0) +#define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given))) +#define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx]) #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val)) static struct udp_pcb *dhcp_pcb; static u8_t dhcp_pcb_refcount; /* DHCP client state machine functions */ -static err_t -dhcp_discover(struct netif *netif); -static err_t -dhcp_select(struct netif *netif); -static void -dhcp_bind(struct netif *netif); +static err_t dhcp_discover(struct netif *netif); +static err_t dhcp_select(struct netif *netif); +static void dhcp_bind(struct netif *netif); #if DHCP_DOES_ARP_CHECK -static err_t -dhcp_decline(struct netif *netif); +static err_t dhcp_decline(struct netif *netif); #endif /* DHCP_DOES_ARP_CHECK */ -static err_t -dhcp_rebind(struct netif *netif); -static err_t -dhcp_reboot(struct netif *netif); -static void -dhcp_set_state(struct dhcp *dhcp, u8_t new_state); +static err_t dhcp_rebind(struct netif *netif); +static err_t dhcp_reboot(struct netif *netif); +static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state); /* receive, unfold, parse and free incoming messages */ -static void -dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); +static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); /* set the DHCP timers */ -static void -dhcp_timeout(struct netif *netif); -static void -dhcp_t1_timeout(struct netif *netif); -static void -dhcp_t2_timeout(struct netif *netif); +static void dhcp_timeout(struct netif *netif); +static void dhcp_t1_timeout(struct netif *netif); +static void dhcp_t2_timeout(struct netif *netif); /* build outgoing messages */ /* create a DHCP message, fill in common headers */ -static struct pbuf * -dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t *options_out_len); +static struct pbuf *dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t *options_out_len); /* add a DHCP option (type, then length in bytes) */ -static u16_t -dhcp_option(u16_t options_out_len, u8_t *options, u8_t option_type, u8_t option_len); +static u16_t dhcp_option(u16_t options_out_len, u8_t *options, u8_t option_type, u8_t option_len); /* add option values */ -static u16_t -dhcp_option_byte(u16_t options_out_len, u8_t *options, u8_t value); -static u16_t -dhcp_option_short(u16_t options_out_len, u8_t *options, u16_t value); -static u16_t -dhcp_option_long(u16_t options_out_len, u8_t *options, u32_t value); +static u16_t dhcp_option_byte(u16_t options_out_len, u8_t *options, u8_t value); +static u16_t dhcp_option_short(u16_t options_out_len, u8_t *options, u16_t value); +static u16_t dhcp_option_long(u16_t options_out_len, u8_t *options, u32_t value); #if LWIP_NETIF_HOSTNAME -static u16_t -dhcp_option_hostname(u16_t options_out_len, u8_t *options, struct netif *netif); +static u16_t dhcp_option_hostname(u16_t options_out_len, u8_t *options, struct netif *netif); #endif /* LWIP_NETIF_HOSTNAME */ /* always add the DHCP options trailer to end and pad */ -static void -dhcp_option_trailer(u16_t options_out_len, u8_t *options, struct pbuf *p_out); +static void dhcp_option_trailer(u16_t options_out_len, u8_t *options, struct pbuf *p_out); /** Ensure DHCP PCB is allocated and bound */ static err_t @@ -301,9 +278,8 @@ dhcp_handle_nak(struct netif *netif) { struct dhcp *dhcp = netif_dhcp_data(netif); - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_handle_nak(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n", + (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* Change to a defined state - set this before assigning the address to ensure the callback can use dhcp_supplied_address() */ dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF); @@ -329,8 +305,8 @@ dhcp_check(struct netif *netif) struct dhcp *dhcp = netif_dhcp_data(netif); err_t result; u16_t msecs; - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0], (s16_t)netif->name[1])); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0], + (s16_t)netif->name[1])); dhcp_set_state(dhcp, DHCP_STATE_CHECKING); /* create an ARP query for the offered IP address, expecting that no host responds, as the IP address should not be in use. */ @@ -343,8 +319,7 @@ dhcp_check(struct netif *netif) } msecs = 500; dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_check(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs)); } #endif /* DHCP_DOES_ARP_CHECK */ @@ -358,20 +333,19 @@ dhcp_handle_offer(struct netif *netif, struct dhcp_msg *msg_in) { struct dhcp *dhcp = netif_dhcp_data(netif); - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_handle_offer(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n", + (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* obtain the server address */ if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) { dhcp->request_timeout = 0; /* stop timer */ ip_addr_set_ip4_u32(&dhcp->server_ip_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID))); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, - ("dhcp_handle_offer(): server 0x%08" X32_F "\n", ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr)))); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n", + ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr)))); /* remember offered address */ ip4_addr_copy(dhcp->offered_ip_addr, msg_in->yiaddr); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, - ("dhcp_handle_offer(): offer for 0x%08" X32_F "\n", ip4_addr_get_u32(&dhcp->offered_ip_addr))); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n", + ip4_addr_get_u32(&dhcp->offered_ip_addr))); dhcp_select(netif); } else { @@ -402,32 +376,24 @@ dhcp_select(struct netif *netif) dhcp = netif_dhcp_data(netif); LWIP_ERROR("dhcp_select: dhcp != NULL", (dhcp != NULL), return ERR_VAL;); - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_select(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); dhcp_set_state(dhcp, DHCP_STATE_REQUESTING); /* create and initialize the DHCP message header */ p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len); if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif)); /* MUST request the offered IP address */ options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4); - options_out_len = - dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); + options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_SERVER_ID, 4); - options_out_len = dhcp_option_long( - options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr)))); + options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr)))); - options_out_len = dhcp_option(options_out_len, - msg_out->options, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - LWIP_ARRAYSIZE(dhcp_discover_request_options)); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options)); for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) { options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]); } @@ -444,8 +410,7 @@ dhcp_select(struct netif *netif) pbuf_free(p_out); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n")); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("dhcp_select: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_select: could not allocate DHCP request\n")); result = ERR_MEM; } if (dhcp->tries < 255) { @@ -453,7 +418,7 @@ dhcp_select(struct netif *netif) } msecs = (u16_t)((dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000); dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs)); return result; } @@ -467,8 +432,7 @@ dhcp_coarse_tmr(void) struct netif *netif; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n")); /* iterate through all network interfaces */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* only act on DHCP configured interfaces */ struct dhcp *dhcp = netif_dhcp_data(netif); if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) { @@ -505,8 +469,7 @@ dhcp_fine_tmr(void) { struct netif *netif; /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { struct dhcp *dhcp = netif_dhcp_data(netif); /* only act on DHCP configured interfaces */ if (dhcp != NULL) { @@ -548,8 +511,7 @@ dhcp_timeout(struct netif *netif) if (dhcp->tries <= 5) { dhcp_select(netif); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_timeout(): REQUESTING, releasing, restarting\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n")); dhcp_release_and_stop(netif); dhcp_start(netif); } @@ -590,7 +552,8 @@ dhcp_t1_timeout(struct netif *netif) (dhcp->state == DHCP_STATE_RENEWING)) { /* just retry to renew - note that the rebind timer (t2) will * eventually time-out if renew tries fail. */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t1_timeout(): must renew\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, + ("dhcp_t1_timeout(): must renew\n")); /* This slightly different to RFC2131: DHCPREQUEST will be sent from state DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */ dhcp_renew(netif); @@ -615,7 +578,8 @@ dhcp_t2_timeout(struct netif *netif) if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) { /* just retry to rebind */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout(): must rebind\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, + ("dhcp_t2_timeout(): must rebind\n")); /* This slightly different to RFC2131: DHCPREQUEST will be sent from state DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */ dhcp_rebind(netif); @@ -745,8 +709,7 @@ dhcp_set_struct(struct netif *netif, struct dhcp *dhcp) * * @param netif the netif from which to remove the struct dhcp */ -void -dhcp_cleanup(struct netif *netif) +void dhcp_cleanup(struct netif *netif) { LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("netif != NULL", netif != NULL); @@ -780,9 +743,7 @@ dhcp_start(struct netif *netif) LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;); LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;); dhcp = netif_dhcp_data(netif); - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_start(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* check MTU of the netif */ if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) { @@ -831,6 +792,7 @@ dhcp_start(struct netif *netif) } #endif /* LWIP_DHCP_CHECK_LINK_UP */ + /* (re)start the DHCP negotiation */ result = dhcp_discover(netif); if (result != ERR_OK) { @@ -872,8 +834,7 @@ dhcp_inform(struct netif *netif) p_out = dhcp_create_msg(netif, &dhcp, DHCP_INFORM, &options_out_len); if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif)); LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, &dhcp, DHCP_STATE_INFORMING, msg_out, DHCP_INFORM, &options_out_len); @@ -885,8 +846,7 @@ dhcp_inform(struct netif *netif) pbuf_free(p_out); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_inform: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n")); } dhcp_dec_pcb_refcount(); /* delete DHCP PCB if not needed any more */ @@ -952,8 +912,8 @@ dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr) LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n")); /* is a DHCP client doing an ARP check? */ if ((dhcp != NULL) && (dhcp->state == DHCP_STATE_CHECKING)) { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08" X32_F "\n", ip4_addr_get_u32(addr))); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", + ip4_addr_get_u32(addr))); /* did a host respond with the address we were offered by the DHCP server? */ if (ip4_addr_cmp(addr, &dhcp->offered_ip_addr)) { @@ -990,8 +950,7 @@ dhcp_decline(struct netif *netif) if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4); - options_out_len = - dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); + options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_BACKING_OFF, msg_out, DHCP_DECLINE, &options_out_len); dhcp_option_trailer(options_out_len, msg_out->options, p_out); @@ -1010,11 +969,12 @@ dhcp_decline(struct netif *netif) } msecs = 10 * 1000; dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs)); return result; } #endif /* DHCP_DOES_ARP_CHECK */ + /** * Start the DHCP process, discover a DHCP server. * @@ -1040,29 +1000,23 @@ dhcp_discover(struct netif *netif) struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n")); - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif)); - options_out_len = dhcp_option(options_out_len, - msg_out->options, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - LWIP_ARRAYSIZE(dhcp_discover_request_options)); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options)); for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) { options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]); } LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_SELECTING, msg_out, DHCP_DISCOVER, &options_out_len); dhcp_option_trailer(options_out_len, msg_out->options, p_out); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER)\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER)\n")); udp_sendto_if_src(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif, IP4_ADDR_ANY); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n")); pbuf_free(p_out); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n")); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_discover: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n")); } if (dhcp->tries < 255) { dhcp->tries++; @@ -1075,11 +1029,11 @@ dhcp_discover(struct netif *netif) #endif /* LWIP_DHCP_AUTOIP_COOP */ msecs = (u16_t)((dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000); dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_discover(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs)); return result; } + /** * Bind the interface to the offered IP address. * @@ -1094,17 +1048,14 @@ dhcp_bind(struct netif *netif) LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;); dhcp = netif_dhcp_data(netif); LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;); - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_bind(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* reset time used of lease */ dhcp->lease_used = 0; if (dhcp->offered_t0_lease != 0xffffffffUL) { /* set renewal period timer */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_bind(): t0 renewal timer %" U32_F " secs\n", dhcp->offered_t0_lease)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease)); timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS; if (timeout > 0xffff) { timeout = 0xffff; @@ -1113,15 +1064,13 @@ dhcp_bind(struct netif *netif) if (dhcp->t0_timeout == 0) { dhcp->t0_timeout = 1; } - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_bind(): set request timeout %" U32_F " msecs\n", dhcp->offered_t0_lease * 1000)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease * 1000)); } /* temporary DHCP lease? */ if (dhcp->offered_t1_renew != 0xffffffffUL) { /* set renewal period timer */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_bind(): t1 renewal timer %" U32_F " secs\n", dhcp->offered_t1_renew)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew)); timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS; if (timeout > 0xffff) { timeout = 0xffff; @@ -1130,14 +1079,12 @@ dhcp_bind(struct netif *netif) if (dhcp->t1_timeout == 0) { dhcp->t1_timeout = 1; } - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_bind(): set request timeout %" U32_F " msecs\n", dhcp->offered_t1_renew * 1000)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew * 1000)); dhcp->t1_renew_time = dhcp->t1_timeout; } /* set renewal period timer */ if (dhcp->offered_t2_rebind != 0xffffffffUL) { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_bind(): t2 rebind timer %" U32_F " secs\n", dhcp->offered_t2_rebind)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind)); timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS; if (timeout > 0xffff) { timeout = 0xffff; @@ -1146,8 +1093,7 @@ dhcp_bind(struct netif *netif) if (dhcp->t2_timeout == 0) { dhcp->t2_timeout = 1; } - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_bind(): set request timeout %" U32_F " msecs\n", dhcp->offered_t2_rebind * 1000)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind * 1000)); dhcp->t2_rebind_time = dhcp->t2_timeout; } @@ -1187,11 +1133,8 @@ dhcp_bind(struct netif *netif) } #endif /* LWIP_DHCP_AUTOIP_COOP */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, - ("dhcp_bind(): IP: 0x%08" X32_F " SN: 0x%08" X32_F " GW: 0x%08" X32_F "\n", - ip4_addr_get_u32(&dhcp->offered_ip_addr), - ip4_addr_get_u32(&sn_mask), - ip4_addr_get_u32(&gw_addr))); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F" SN: 0x%08"X32_F" GW: 0x%08"X32_F"\n", + ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr))); /* netif is now bound to DHCP leased address - set this before assigning the address to ensure the callback can use dhcp_supplied_address() */ dhcp_set_state(dhcp, DHCP_STATE_BOUND); @@ -1224,14 +1167,10 @@ dhcp_renew(struct netif *netif) p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len); if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif)); - options_out_len = dhcp_option(options_out_len, - msg_out->options, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - LWIP_ARRAYSIZE(dhcp_discover_request_options)); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options)); for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) { options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]); } @@ -1248,8 +1187,7 @@ dhcp_renew(struct netif *netif) LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n")); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_renew: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n")); result = ERR_MEM; } if (dhcp->tries < 255) { @@ -1258,8 +1196,7 @@ dhcp_renew(struct netif *netif) /* back-off on retries, but to a maximum of 20 seconds */ msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000); dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_renew(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs)); return result; } @@ -1285,14 +1222,10 @@ dhcp_rebind(struct netif *netif) p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len); if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif)); - options_out_len = dhcp_option(options_out_len, - msg_out->options, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - LWIP_ARRAYSIZE(dhcp_discover_request_options)); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options)); for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) { options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]); } @@ -1309,8 +1242,7 @@ dhcp_rebind(struct netif *netif) pbuf_free(p_out); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n")); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_rebind: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n")); result = ERR_MEM; } if (dhcp->tries < 255) { @@ -1318,8 +1250,7 @@ dhcp_rebind(struct netif *netif) } msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000); dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_rebind(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs)); return result; } @@ -1345,18 +1276,13 @@ dhcp_reboot(struct netif *netif) p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len); if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; - options_out_len = - dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN_MIN_REQUIRED); options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4); - options_out_len = - dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); + options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr))); - options_out_len = dhcp_option(options_out_len, - msg_out->options, - DHCP_OPTION_PARAMETER_REQUEST_LIST, - LWIP_ARRAYSIZE(dhcp_discover_request_options)); + options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options)); for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) { options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]); } @@ -1373,8 +1299,7 @@ dhcp_reboot(struct netif *netif) pbuf_free(p_out); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n")); } else { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_reboot: could not allocate DHCP request\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n")); result = ERR_MEM; } if (dhcp->tries < 255) { @@ -1382,8 +1307,7 @@ dhcp_reboot(struct netif *netif) } msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000); dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp_reboot(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs)); return result; } @@ -1432,8 +1356,7 @@ dhcp_release_and_stop(struct netif *netif) if (p_out != NULL) { struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload; options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_SERVER_ID, 4); - options_out_len = - dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr)))); + options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr)))); LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, dhcp->state, msg_out, DHCP_RELEASE, &options_out_len); dhcp_option_trailer(options_out_len, msg_out->options, p_out); @@ -1442,10 +1365,8 @@ dhcp_release_and_stop(struct netif *netif) pbuf_free(p_out); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_STATE_OFF\n")); } else { - /* sending release failed, but that's not a problem since the correct behaviour of dhcp does not rely on release - */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp_release: could not allocate DHCP request\n")); + /* sending release failed, but that's not a problem since the correct behaviour of dhcp does not rely on release */ + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n")); } } @@ -1513,8 +1434,7 @@ dhcp_set_state(struct dhcp *dhcp, u8_t new_state) static u16_t dhcp_option(u16_t options_out_len, u8_t *options, u8_t option_type, u8_t option_len) { - LWIP_ASSERT("dhcp_option: options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", - options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN); + LWIP_ASSERT("dhcp_option: options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN); options[options_out_len++] = option_type; options[options_out_len++] = option_len; return options_out_len; @@ -1536,7 +1456,7 @@ dhcp_option_short(u16_t options_out_len, u8_t *options, u16_t value) { LWIP_ASSERT("dhcp_option_short: options_out_len + 2 <= DHCP_OPTIONS_LEN", options_out_len + 2U <= DHCP_OPTIONS_LEN); options[options_out_len++] = (u8_t)((value & 0xff00U) >> 8); - options[options_out_len++] = (u8_t)(value & 0x00ffU); + options[options_out_len++] = (u8_t) (value & 0x00ffU); return options_out_len; } @@ -1718,18 +1638,10 @@ again: break; default: decode_len = 0; - LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %" U16_F " in options\n", (u16_t)op)); - LWIP_HOOK_DHCP_PARSE_OPTION(ip_current_netif(), - dhcp, - dhcp->state, - msg_in, - dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) - ? (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) - : 0, - op, - len, - q, - val_offset); + LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", (u16_t)op)); + LWIP_HOOK_DHCP_PARSE_OPTION(ip_current_netif(), dhcp, dhcp->state, msg_in, + dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) ? (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) : 0, + op, len, q, val_offset); break; } if (op == DHCP_OPTION_PAD) { @@ -1743,7 +1655,7 @@ again: if (decode_len > 0) { u32_t value = 0; u16_t copy_len; - decode_next: +decode_next: LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX); if (!dhcp_option_given(dhcp, decode_idx)) { copy_len = LWIP_MIN(decode_len, 4); @@ -1826,15 +1738,15 @@ again: if (!file_overloaded) { /* only do this for ACK messages */ if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) && - (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK)) - /* copy bootp file name, don't care for sname (server hostname) */ - if (pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN - 1, DHCP_FILE_OFS) != (DHCP_FILE_LEN - 1)) { - return ERR_BUF; - } + (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK)) + /* copy bootp file name, don't care for sname (server hostname) */ + if (pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS) != (DHCP_FILE_LEN-1)) { + return ERR_BUF; + } /* make sure the string is really NULL-terminated */ - dhcp->boot_file_name[DHCP_FILE_LEN - 1] = 0; + dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0; } -#endif /* LWIP_DHCP_BOOTP_FILE */ +#endif /* LWIP_DHCP_BOOTP_FILE */ return ERR_OK; } @@ -1860,16 +1772,10 @@ dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, LWIP_ASSERT("invalid server address type", IP_IS_V4(addr)); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, - ("dhcp_recv(pbuf = %p) from DHCP server %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F " port %" U16_F "\n", - (void *)p, - ip4_addr1_16(ip_2_ip4(addr)), - ip4_addr2_16(ip_2_ip4(addr)), - ip4_addr3_16(ip_2_ip4(addr)), - ip4_addr4_16(ip_2_ip4(addr)), - port)); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %" U16_F "\n", p->len)); - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %" U16_F "\n", p->tot_len)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void *)p, + ip4_addr1_16(ip_2_ip4(addr)), ip4_addr2_16(ip_2_ip4(addr)), ip4_addr3_16(ip_2_ip4(addr)), ip4_addr4_16(ip_2_ip4(addr)), port)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len)); /* prevent warnings about unused arguments */ LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); @@ -1881,28 +1787,22 @@ dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, } if (reply_msg->op != DHCP_BOOTREPLY) { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("not a DHCP reply message, but type %" U16_F "\n", (u16_t)reply_msg->op)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op)); goto free_pbuf_and_return; } /* iterate through hardware address and match against DHCP message */ for (i = 0; i < netif->hwaddr_len && i < LWIP_MIN(DHCP_CHADDR_LEN, NETIF_MAX_HWADDR_LEN); i++) { if (netif->hwaddr[i] != reply_msg->chaddr[i]) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("netif->hwaddr[%" U16_F "]==%02" X16_F " != reply_msg->chaddr[%" U16_F "]==%02" X16_F "\n", - (u16_t)i, - (u16_t)netif->hwaddr[i], - (u16_t)i, - (u16_t)reply_msg->chaddr[i])); + ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n", + (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i])); goto free_pbuf_and_return; } } /* match transaction ID against what we expected */ if (lwip_ntohl(reply_msg->xid) != dhcp->xid) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("transaction id mismatch reply_msg->xid(%" X32_F ")!=dhcp->xid(%" X32_F ")\n", - lwip_ntohl(reply_msg->xid), - dhcp->xid)); + ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n", lwip_ntohl(reply_msg->xid), dhcp->xid)); goto free_pbuf_and_return; } /* option fields could be unfold? */ @@ -1949,8 +1849,9 @@ dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, } } /* received a DHCP_NAK in appropriate state? */ - else if ((msg_type == DHCP_NAK) && ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) || - (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING))) { + else if ((msg_type == DHCP_NAK) && + ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) || + (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING ))) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n")); dhcp_handle_nak(netif); } @@ -1987,7 +1888,7 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t * at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */ #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND) static u32_t xid; -#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ +#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ static u32_t xid = 0xABCD0000; #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ #else @@ -2000,7 +1901,8 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return NULL;); p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM); if (p_out == NULL) { - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_create_msg(): could not allocate pbuf\n")); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, + ("dhcp_create_msg(): could not allocate pbuf\n")); return NULL; } LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg", @@ -2012,13 +1914,14 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t if (dhcp->tries == 0) { #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND) xid = LWIP_RAND(); -#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ +#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ xid++; #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */ } dhcp->xid = xid; } - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("transaction id xid(%" X32_F ")\n", xid)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, + ("transaction id xid(%"X32_F")\n", xid)); msg_out = (struct dhcp_msg *)p_out->payload; memset(msg_out, 0, sizeof(struct dhcp_msg)); @@ -2061,7 +1964,8 @@ dhcp_option_trailer(u16_t options_out_len, u8_t *options, struct pbuf *p_out) { options[options_out_len++] = DHCP_OPTION_END; /* packet is too small, or not 4 byte aligned? */ - while (((options_out_len < DHCP_MIN_OPTIONS_LEN) || (options_out_len & 3)) && (options_out_len < DHCP_OPTIONS_LEN)) { + while (((options_out_len < DHCP_MIN_OPTIONS_LEN) || (options_out_len & 3)) && + (options_out_len < DHCP_OPTIONS_LEN)) { /* add a fill/padding byte */ options[options_out_len++] = 0; } diff --git a/src/core/ipv4/etharp.c b/src/core/ipv4/etharp.c index fc6c8b25..442aac08 100644 --- a/src/core/ipv4/etharp.c +++ b/src/core/ipv4/etharp.c @@ -47,12 +47,12 @@ #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */ -#include "lwip/autoip.h" -#include "lwip/dhcp.h" #include "lwip/etharp.h" -#include "lwip/prot/iana.h" -#include "lwip/snmp.h" #include "lwip/stats.h" +#include "lwip/snmp.h" +#include "lwip/dhcp.h" +#include "lwip/autoip.h" +#include "lwip/prot/iana.h" #include "netif/ethernet.h" #include @@ -63,7 +63,7 @@ /** Re-request a used ARP entry 1 minute before it would expire to prevent * breaking a steadily used connection because the ARP entry timed out. */ -#define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30) +#define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30) #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15) /** the time an ARP entry stays pending after first request, @@ -76,25 +76,22 @@ #define ARP_MAXPENDING 5 /** ARP states */ -enum etharp_state -{ +enum etharp_state { ETHARP_STATE_EMPTY = 0, ETHARP_STATE_PENDING, ETHARP_STATE_STABLE, ETHARP_STATE_STABLE_REREQUESTING_1, ETHARP_STATE_STABLE_REREQUESTING_2 #if ETHARP_SUPPORT_STATIC_ENTRIES - , - ETHARP_STATE_STATIC + , ETHARP_STATE_STATIC #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ }; -struct etharp_entry -{ +struct etharp_entry { #if ARP_QUEUEING /** Pointer to queue of pending outgoing packets on this ARP entry. */ struct etharp_q_entry *q; -#else /* ARP_QUEUEING */ +#else /* ARP_QUEUEING */ /** Pointer to a single pending outgoing packet on this ARP entry. */ struct pbuf *q; #endif /* ARP_QUEUEING */ @@ -113,39 +110,32 @@ static netif_addr_idx_t etharp_cached_entry; /** Try hard to create a new entry - we want the IP address to appear in the cache (even if this means removing an active entry or so). */ -#define ETHARP_FLAG_TRY_HARD 1 -#define ETHARP_FLAG_FIND_ONLY 2 +#define ETHARP_FLAG_TRY_HARD 1 +#define ETHARP_FLAG_FIND_ONLY 2 #if ETHARP_SUPPORT_STATIC_ENTRIES #define ETHARP_FLAG_STATIC_ENTRY 4 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ #if LWIP_NETIF_HWADDRHINT -#define ETHARP_SET_ADDRHINT(netif, addrhint) \ - do { \ - if (((netif) != NULL) && ((netif)->hints != NULL)) { \ - (netif)->hints->addr_hint = (addrhint); \ - } \ - } while (0) +#define ETHARP_SET_ADDRHINT(netif, addrhint) do { if (((netif) != NULL) && ((netif)->hints != NULL)) { \ + (netif)->hints->addr_hint = (addrhint); }} while(0) #else /* LWIP_NETIF_HWADDRHINT */ -#define ETHARP_SET_ADDRHINT(netif, addrhint) (etharp_cached_entry = (addrhint)) +#define ETHARP_SET_ADDRHINT(netif, addrhint) (etharp_cached_entry = (addrhint)) #endif /* LWIP_NETIF_HWADDRHINT */ + /* Check for maximum ARP_TABLE_SIZE */ #if (ARP_TABLE_SIZE > NETIF_ADDR_IDX_MAX) #error "ARP_TABLE_SIZE must fit in an s16_t, you have to reduce it in your lwipopts.h" #endif -static err_t -etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr); -static err_t -etharp_raw(struct netif *netif, - const struct eth_addr *ethsrc_addr, - const struct eth_addr *ethdst_addr, - const struct eth_addr *hwsrc_addr, - const ip4_addr_t *ipsrc_addr, - const struct eth_addr *hwdst_addr, - const ip4_addr_t *ipdst_addr, - const u16_t opcode); + +static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr); +static err_t etharp_raw(struct netif *netif, + const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr, + const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, + const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, + const u16_t opcode); #if ARP_QUEUEING /** @@ -182,9 +172,7 @@ etharp_free_entry(int i) /* and empty packet queue */ if (arp_table[i].q != NULL) { /* remove all queued packets */ - LWIP_DEBUGF( - ETHARP_DEBUG, - ("etharp_free_entry: freeing entry %" U16_F ", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); + LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; } @@ -218,15 +206,14 @@ etharp_tmr(void) #if ETHARP_SUPPORT_STATIC_ENTRIES && (state != ETHARP_STATE_STATIC) #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ - ) { + ) { arp_table[i].ctime++; if ((arp_table[i].ctime >= ARP_MAXAGE) || - ((arp_table[i].state == ETHARP_STATE_PENDING) && (arp_table[i].ctime >= ARP_MAXPENDING))) { + ((arp_table[i].state == ETHARP_STATE_PENDING) && + (arp_table[i].ctime >= ARP_MAXPENDING))) { /* pending or stable entry has become old! */ - LWIP_DEBUGF(ETHARP_DEBUG, - ("etharp_timer: expired %s entry %d.\n", - arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", - i)); + LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %d.\n", + arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", i)); /* clean up entries that have just been expired */ etharp_free_entry(i); } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) { @@ -308,7 +295,7 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) #if ETHARP_TABLE_MATCH_NETIF && ((netif == NULL) || (netif == arp_table[i].netif)) #endif /* ETHARP_TABLE_MATCH_NETIF */ - ) { + ) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %d\n", (int)i)); /* found exact IP address match, simply bail out */ return i; @@ -322,7 +309,7 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) age_queue = arp_table[i].ctime; } } else - /* pending without queued packets? */ + /* pending without queued packets? */ { if (arp_table[i].ctime >= age_pending) { old_pending = i; @@ -351,8 +338,7 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) || /* or no empty entry found and not allowed to recycle? */ ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) { - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_find_entry: no empty entry found and not allowed to recycle\n")); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n")); return (s16_t)ERR_MEM; } @@ -381,16 +367,12 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) } else if (old_pending < ARP_TABLE_SIZE) { /* recycle oldest pending */ i = old_pending; - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i)); /* 4) found recyclable pending entry with queued packets? */ } else if (old_queue < ARP_TABLE_SIZE) { /* recycle oldest pending (queued packets are free in etharp_free_entry) */ i = old_queue; - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", - (int)i, - (void *)(arp_table[i].q))); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", (int)i, (void *)(arp_table[i].q))); /* no empty or recyclable entries found */ } else { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n")); @@ -403,7 +385,8 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) } LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); - LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY", arp_table[i].state == ETHARP_STATE_EMPTY); + LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY", + arp_table[i].state == ETHARP_STATE_EMPTY); /* IP address given? */ if (ipaddr != NULL) { @@ -440,23 +423,15 @@ etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct et { s16_t i; LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_update_arp_entry: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F " - %02" X16_F ":%02" X16_F - ":%02" X16_F ":%02" X16_F ":%02" X16_F ":%02" X16_F "\n", - ip4_addr1_16(ipaddr), - ip4_addr2_16(ipaddr), - ip4_addr3_16(ipaddr), - ip4_addr4_16(ipaddr), - (u16_t)ethaddr->addr[0], - (u16_t)ethaddr->addr[1], - (u16_t)ethaddr->addr[2], - (u16_t)ethaddr->addr[3], - (u16_t)ethaddr->addr[4], - (u16_t)ethaddr->addr[5])); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n", + ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), + (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], + (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); /* non-unicast address? */ - if (ip4_addr_isany(ipaddr) || ip4_addr_isbroadcast(ipaddr, netif) || ip4_addr_ismulticast(ipaddr)) { - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n")); + if (ip4_addr_isany(ipaddr) || + ip4_addr_isbroadcast(ipaddr, netif) || + ip4_addr_ismulticast(ipaddr)) { + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n")); return ERR_ARG; } /* find or create ARP entry */ @@ -485,7 +460,7 @@ etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct et /* insert in SNMP ARP index tree */ mib2_add_arp_entry(netif, &arp_table[i].ipaddr); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %" S16_F "\n", i)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", i)); /* update address */ SMEMCPY(&arp_table[i].ethaddr, ethaddr, ETH_HWADDR_LEN); /* reset time stamp */ @@ -502,7 +477,7 @@ etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct et p = q->p; /* now queue entry can be freed */ memp_free(MEMP_ARP_QUEUE, q); -#else /* ARP_QUEUEING */ +#else /* ARP_QUEUEING */ if (arp_table[i].q != NULL) { struct pbuf *p = arp_table[i].q; arp_table[i].q = NULL; @@ -529,19 +504,10 @@ etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr) { struct netif *netif; LWIP_ASSERT_CORE_LOCKED(); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_add_static_entry: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F " - %02" X16_F ":%02" X16_F - ":%02" X16_F ":%02" X16_F ":%02" X16_F ":%02" X16_F "\n", - ip4_addr1_16(ipaddr), - ip4_addr2_16(ipaddr), - ip4_addr3_16(ipaddr), - ip4_addr4_16(ipaddr), - (u16_t)ethaddr->addr[0], - (u16_t)ethaddr->addr[1], - (u16_t)ethaddr->addr[2], - (u16_t)ethaddr->addr[3], - (u16_t)ethaddr->addr[4], - (u16_t)ethaddr->addr[5])); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n", + ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), + (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], + (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); netif = ip4_route(ipaddr); if (netif == NULL) { @@ -564,12 +530,8 @@ etharp_remove_static_entry(const ip4_addr_t *ipaddr) { s16_t i; LWIP_ASSERT_CORE_LOCKED(); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_remove_static_entry: %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(ipaddr), - ip4_addr2_16(ipaddr), - ip4_addr3_16(ipaddr), - ip4_addr4_16(ipaddr))); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr))); /* find or create ARP entry */ i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL); @@ -618,11 +580,13 @@ etharp_cleanup_netif(struct netif *netif) * @return table index if found, -1 otherwise */ ssize_t -etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr **eth_ret, const ip4_addr_t **ip_ret) +etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr, + struct eth_addr **eth_ret, const ip4_addr_t **ip_ret) { s16_t i; - LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL", eth_ret != NULL && ip_ret != NULL); + LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL", + eth_ret != NULL && ip_ret != NULL); LWIP_UNUSED_ARG(netif); @@ -652,8 +616,8 @@ etharp_get_entry(size_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL); if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { - *ipaddr = &arp_table[i].ipaddr; - *netif = arp_table[i].netif; + *ipaddr = &arp_table[i].ipaddr; + *netif = arp_table[i].netif; *eth_ret = &arp_table[i].ethaddr; return 1; } else { @@ -688,15 +652,13 @@ etharp_input(struct pbuf *p, struct netif *netif) hdr = (struct etharp_hdr *)p->payload; /* RFC 826 "Packet Reception": */ - if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) || (hdr->hwlen != ETH_HWADDR_LEN) || - (hdr->protolen != sizeof(ip4_addr_t)) || (hdr->proto != PP_HTONS(ETHTYPE_IP))) { + if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) || + (hdr->hwlen != ETH_HWADDR_LEN) || + (hdr->protolen != sizeof(ip4_addr_t)) || + (hdr->proto != PP_HTONS(ETHTYPE_IP))) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%" U16_F - "/%" U16_F "/%" U16_F "/%" U16_F ")\n", - hdr->hwtype, - (u16_t)hdr->hwlen, - hdr->proto, - (u16_t)hdr->protolen)); + ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n", + hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen)); ETHARP_STATS_INC(etharp.proterr); ETHARP_STATS_INC(etharp.drop); pbuf_free(p); @@ -729,7 +691,8 @@ etharp_input(struct pbuf *p, struct netif *netif) can result in directly sending the queued packets for this host. ARP message not directed to us? -> update the source IP address in the cache, if present */ - etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY); + etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), + for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY); /* now act on the message itself */ switch (hdr->opcode) { @@ -739,17 +702,14 @@ etharp_input(struct pbuf *p, struct netif *netif) * reply. In any case, we time-stamp any existing ARP entry, * and possibly send out an IP packet that was queued on it. */ - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n")); + LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n")); /* ARP request for our address? */ if (for_us) { /* send ARP response */ etharp_raw(netif, - (struct eth_addr *)netif->hwaddr, - &hdr->shwaddr, - (struct eth_addr *)netif->hwaddr, - netif_ip4_addr(netif), - &hdr->shwaddr, - &sipaddr, + (struct eth_addr *)netif->hwaddr, &hdr->shwaddr, + (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), + &hdr->shwaddr, &sipaddr, ARP_REPLY); /* we are not configured? */ } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { @@ -773,8 +733,7 @@ etharp_input(struct pbuf *p, struct netif *netif) #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */ break; default: - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_input: ARP unknown opcode type %" S16_F "\n", lwip_htons(hdr->opcode))); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode))); ETHARP_STATS_INC(etharp.err); break; } @@ -788,7 +747,8 @@ etharp_input(struct pbuf *p, struct netif *netif) static err_t etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, netif_addr_idx_t arp_idx) { - LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE", arp_table[arp_idx].state >= ETHARP_STATE_STABLE); + LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE", + arp_table[arp_idx].state >= ETHARP_STATE_STABLE); /* if arp table entry is about to expire: re-request it, but only if its state is ETHARP_STATE_STABLE to prevent flooding the network with ARP requests if this address is used frequently. */ @@ -862,7 +822,8 @@ etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) netif_addr_idx_t i; /* outside local network? if so, this can neither be a global broadcast nor a subnet broadcast. */ - if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && !ip4_addr_islinklocal(ipaddr)) { + if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && + !ip4_addr_islinklocal(ipaddr)) { #if LWIP_AUTOIP struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr *, q->payload); /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with @@ -978,7 +939,9 @@ etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) netif_addr_idx_t i; /* non-unicast address? */ - if (ip4_addr_isbroadcast(ipaddr, netif) || ip4_addr_ismulticast(ipaddr) || ip4_addr_isany(ipaddr)) { + if (ip4_addr_isbroadcast(ipaddr, netif) || + ip4_addr_ismulticast(ipaddr) || + ip4_addr_isany(ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n")); return ERR_ARG; } @@ -1008,7 +971,8 @@ etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) /* { i is either a STABLE or (new or existing) PENDING entry } */ LWIP_ASSERT("arp_table[i].state == PENDING or STABLE", - ((arp_table[i].state == ETHARP_STATE_PENDING) || (arp_table[i].state >= ETHARP_STATE_STABLE))); + ((arp_table[i].state == ETHARP_STATE_PENDING) || + (arp_table[i].state >= ETHARP_STATE_STABLE))); /* do we have a new entry? or an implicit query request? */ if (is_new_entry || (q == NULL)) { @@ -1091,33 +1055,27 @@ etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) memp_free(MEMP_ARP_QUEUE, old); } #endif - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_query: queued packet %p on ARP entry %" U16_F "\n", (void *)q, i)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, i)); result = ERR_OK; } else { /* the pool MEMP_ARP_QUEUE is empty */ pbuf_free(p); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); result = ERR_MEM; } -#else /* ARP_QUEUEING */ +#else /* ARP_QUEUEING */ /* always queue one packet per ARP request only, freeing a previously queued packet */ if (arp_table[i].q != NULL) { - LWIP_DEBUGF( - ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_query: dropped previously queued packet %p for ARP entry %" U16_F "\n", (void *)q, (u16_t)i)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"U16_F"\n", (void *)q, (u16_t)i)); pbuf_free(arp_table[i].q); } arp_table[i].q = p; result = ERR_OK; - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_query: queued packet %p on ARP entry %" U16_F "\n", (void *)q, (u16_t)i)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, (u16_t)i)); #endif /* ARP_QUEUEING */ } else { ETHARP_STATS_INC(etharp.memerr); - LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, - ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); + LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); result = ERR_MEM; } } @@ -1140,13 +1098,10 @@ etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) * any other err_t on failure */ static err_t -etharp_raw(struct netif *netif, - const struct eth_addr *ethsrc_addr, +etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr, - const struct eth_addr *hwsrc_addr, - const ip4_addr_t *ipsrc_addr, - const struct eth_addr *hwdst_addr, - const ip4_addr_t *ipdst_addr, + const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, + const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, const u16_t opcode) { struct pbuf *p; @@ -1164,7 +1119,8 @@ etharp_raw(struct netif *netif, ETHARP_STATS_INC(etharp.memerr); return ERR_MEM; } - LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr", (p->len >= SIZEOF_ETHARP_HDR)); + LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr", + (p->len >= SIZEOF_ETHARP_HDR)); hdr = (struct etharp_hdr *)p->payload; LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n")); @@ -1224,14 +1180,9 @@ etharp_raw(struct netif *netif, static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr) { - return etharp_raw(netif, - (struct eth_addr *)netif->hwaddr, - hw_dst_addr, - (struct eth_addr *)netif->hwaddr, - netif_ip4_addr(netif), - ðzero, - ipaddr, - ARP_REQUEST); + return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr, + (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero, + ipaddr, ARP_REQUEST); } /** diff --git a/src/core/ipv4/icmp.c b/src/core/ipv4/icmp.c index 915ad51b..3fe13cc3 100644 --- a/src/core/ipv4/icmp.c +++ b/src/core/ipv4/icmp.c @@ -43,10 +43,10 @@ #if LWIP_IPV4 && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */ -#include "lwip/def.h" #include "lwip/icmp.h" #include "lwip/inet_chksum.h" #include "lwip/ip.h" +#include "lwip/def.h" #include "lwip/stats.h" #include @@ -65,8 +65,7 @@ /* The amount of data from the original packet to return in a dest-unreachable */ #define ICMP_DEST_UNREACH_DATASIZE 8 -static void -icmp_send_response(struct pbuf *p, u8_t type, u8_t code); +static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code); /** * Processes ICMP input packets, called from ip_input(). @@ -95,11 +94,11 @@ icmp_input(struct pbuf *p, struct netif *inp) iphdr_in = ip4_current_header(); hlen = IPH_HL_BYTES(iphdr_in); if (hlen < IP_HLEN) { - LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%" S16_F " bytes) received\n", hlen)); + LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen)); goto lenerr; } if (p->len < sizeof(u16_t) * 2) { - LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%" U16_F " bytes) received\n", p->tot_len)); + LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } @@ -123,7 +122,7 @@ icmp_input(struct pbuf *p, struct netif *inp) #if LWIP_MULTICAST_PING /* For multicast, use address of receiving interface as source address */ src = netif_ip4_addr(inp); -#else /* LWIP_MULTICAST_PING */ +#else /* LWIP_MULTICAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n")); goto icmperr; #endif /* LWIP_MULTICAST_PING */ @@ -133,7 +132,7 @@ icmp_input(struct pbuf *p, struct netif *inp) #if LWIP_BROADCAST_PING /* For broadcast, use address of receiving interface as source address */ src = netif_ip4_addr(inp); -#else /* LWIP_BROADCAST_PING */ +#else /* LWIP_BROADCAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n")); goto icmperr; #endif /* LWIP_BROADCAST_PING */ @@ -144,8 +143,7 @@ icmp_input(struct pbuf *p, struct netif *inp) goto lenerr; } #if CHECKSUM_CHECK_ICMP - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) - { + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) { if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); @@ -216,8 +214,7 @@ icmp_input(struct pbuf *p, struct netif *inp) ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); ICMPH_TYPE_SET(iecho, ICMP_ER); #if CHECKSUM_GEN_ICMP - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) - { + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) { /* adjust the checksum */ if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS((u16_t)(ICMP_ECHO << 8)) + 1); @@ -226,9 +223,11 @@ icmp_input(struct pbuf *p, struct netif *inp) } } #if LWIP_CHECKSUM_CTRL_PER_NETIF - else { iecho->chksum = 0; } + else { + iecho->chksum = 0; + } #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ -#else /* CHECKSUM_GEN_ICMP */ +#else /* CHECKSUM_GEN_ICMP */ iecho->chksum = 0; #endif /* CHECKSUM_GEN_ICMP */ @@ -236,7 +235,9 @@ icmp_input(struct pbuf *p, struct netif *inp) IPH_TTL_SET(iphdr, ICMP_TTL); IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) { IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen)); } + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) { + IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen)); + } #endif /* CHECKSUM_GEN_IP */ ICMP_STATS_INC(icmp.xmit); @@ -246,7 +247,8 @@ icmp_input(struct pbuf *p, struct netif *inp) MIB2_STATS_INC(mib2.icmpoutechoreps); /* send an ICMP packet */ - ret = ip4_output_if(p, src, LWIP_IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); + ret = ip4_output_if(p, src, LWIP_IP_HDRINCL, + ICMP_TTL, 0, IP_PROTO_ICMP, inp); if (ret != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret))); } @@ -272,8 +274,8 @@ icmp_input(struct pbuf *p, struct netif *inp) } else if (type == ICMP_AMR) { MIB2_STATS_INC(mib2.icmpinaddrmaskreps); } - LWIP_DEBUGF(ICMP_DEBUG, - ("icmp_input: ICMP type %" S16_F " code %" S16_F " not supported.\n", (s16_t)type, (s16_t)code)); + LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", + (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } @@ -348,7 +350,8 @@ icmp_send_response(struct pbuf *p, u8_t type, u8_t code) MIB2_STATS_INC(mib2.icmpoutmsgs); /* ICMP header + IP header + 8 bytes of data */ - q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE, PBUF_RAM); + q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE, + PBUF_RAM); if (q == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n")); MIB2_STATS_INC(mib2.icmpouterrors); @@ -371,7 +374,8 @@ icmp_send_response(struct pbuf *p, u8_t type, u8_t code) icmphdr->seqno = 0; /* copy fields from original packet */ - SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload, IP_HLEN + ICMP_DEST_UNREACH_DATASIZE); + SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload, + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE); ip4_addr_copy(iphdr_src, iphdr->src); #ifdef LWIP_HOOK_IP4_ROUTE_SRC @@ -387,7 +391,9 @@ icmp_send_response(struct pbuf *p, u8_t type, u8_t code) /* calculate checksum */ icmphdr->chksum = 0; #if CHECKSUM_GEN_ICMP - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) { icmphdr->chksum = inet_chksum(icmphdr, q->len); } + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) { + icmphdr->chksum = inet_chksum(icmphdr, q->len); + } #endif ICMP_STATS_INC(icmp.xmit); ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif); diff --git a/src/core/ipv4/igmp.c b/src/core/ipv4/igmp.c index 7ce30094..b655aa3a 100644 --- a/src/core/ipv4/igmp.c +++ b/src/core/ipv4/igmp.c @@ -39,7 +39,7 @@ * The Swedish Institute of Computer Science and Adam Dunkels * are specifically granted permission to redistribute this * source code. - */ +*/ /*------------------------------------------------------------- Note 1) @@ -70,9 +70,10 @@ Steve Reynolds * RFC 988 - Host extensions for IP multicasting - V0 * RFC 1054 - Host extensions for IP multicasting - * RFC 1112 - Host extensions for IP multicasting - V1 - * RFC 2236 - Internet Group Management Protocol, Version 2 - V2 <- this code is based on this RFC (it's - *the "de facto" standard) RFC 3376 - Internet Group Management Protocol, Version 3 - V3 RFC 4604 - Using - *Internet Group Management Protocol Version 3... - V3+ RFC 2113 - IP Router Alert Option - + * RFC 2236 - Internet Group Management Protocol, Version 2 - V2 <- this code is based on this RFC (it's the "de facto" standard) + * RFC 3376 - Internet Group Management Protocol, Version 3 - V3 + * RFC 4604 - Using Internet Group Management Protocol Version 3... - V3+ + * RFC 2113 - IP Router Alert Option - *----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------- @@ -83,35 +84,28 @@ Steve Reynolds #if LWIP_IPV4 && LWIP_IGMP /* don't build if not configured for use in lwipopts.h */ +#include "lwip/igmp.h" #include "lwip/debug.h" #include "lwip/def.h" -#include "lwip/igmp.h" -#include "lwip/inet_chksum.h" -#include "lwip/ip.h" #include "lwip/mem.h" +#include "lwip/ip.h" +#include "lwip/inet_chksum.h" #include "lwip/netif.h" -#include "lwip/prot/igmp.h" #include "lwip/stats.h" +#include "lwip/prot/igmp.h" #include -static struct igmp_group * -igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr); -static err_t -igmp_remove_group(struct netif *netif, struct igmp_group *group); -static void -igmp_timeout(struct netif *netif, struct igmp_group *group); -static void -igmp_start_timer(struct igmp_group *group, u8_t max_time); -static void -igmp_delaying_member(struct igmp_group *group, u8_t maxresp); -static err_t -igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, struct netif *netif); -static void -igmp_send(struct netif *netif, struct igmp_group *group, u8_t type); +static struct igmp_group *igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr); +static err_t igmp_remove_group(struct netif *netif, struct igmp_group *group); +static void igmp_timeout(struct netif *netif, struct igmp_group *group); +static void igmp_start_timer(struct igmp_group *group, u8_t max_time); +static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp); +static err_t igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, struct netif *netif); +static void igmp_send(struct netif *netif, struct igmp_group *group, u8_t type); -static ip4_addr_t allsystems; -static ip4_addr_t allrouters; +static ip4_addr_t allsystems; +static ip4_addr_t allrouters; /** * Initialize the IGMP module @@ -263,15 +257,16 @@ igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr) group = (struct igmp_group *)memp_malloc(MEMP_IGMP_GROUP); if (group != NULL) { ip4_addr_set(&(group->group_address), addr); - group->timer = 0; /* Not running */ - group->group_state = IGMP_GROUP_NON_MEMBER; + group->timer = 0; /* Not running */ + group->group_state = IGMP_GROUP_NON_MEMBER; group->last_reporter_flag = 0; - group->use = 0; + group->use = 0; /* Ensure allsystems group is always first in list */ if (list_head == NULL) { /* this is the first entry in linked list */ - LWIP_ASSERT("igmp_lookup_group: first group must be allsystems", (ip4_addr_cmp(addr, &allsystems) != 0)); + LWIP_ASSERT("igmp_lookup_group: first group must be allsystems", + (ip4_addr_cmp(addr, &allsystems) != 0)); group->next = NULL; netif_set_client_data(ifp, LWIP_NETIF_CLIENT_DATA_INDEX_IGMP, group); } else { @@ -283,8 +278,7 @@ igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr) } } - LWIP_DEBUGF(IGMP_DEBUG, - ("igmp_lookup_group: %sallocated a new group with address ", (group ? "" : "impossible to "))); + LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group ? "" : "impossible to "))); ip4_addr_debug_print(IGMP_DEBUG, addr); LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)ifp)); @@ -328,7 +322,7 @@ igmp_remove_group(struct netif *netif, struct igmp_group *group) void igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest) { - struct igmp_msg *igmp; + struct igmp_msg *igmp; struct igmp_group *group; struct igmp_group *groupref; @@ -374,15 +368,11 @@ igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest) /* IGMP_MEMB_QUERY to the "all systems" address ? */ if ((ip4_addr_cmp(dest, &allsystems)) && ip4_addr_isany(&igmp->igmp_group_address)) { /* THIS IS THE GENERAL QUERY */ - LWIP_DEBUGF(IGMP_DEBUG, - ("igmp_input: General IGMP_MEMB_QUERY on \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", - (int)(igmp->igmp_maxresp))); + LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: General IGMP_MEMB_QUERY on \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp))); if (igmp->igmp_maxresp == 0) { IGMP_STATS_INC(igmp.rx_v1); - LWIP_DEBUGF( - IGMP_DEBUG, - ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n")); + LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n")); igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR; } else { IGMP_STATS_INC(igmp.rx_general); @@ -407,14 +397,12 @@ igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest) ip4_addr_debug_print_val(IGMP_DEBUG, igmp->igmp_group_address); if (ip4_addr_cmp(dest, &allsystems)) { ip4_addr_t groupaddr; - LWIP_DEBUGF(IGMP_DEBUG, - (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp))); + LWIP_DEBUGF(IGMP_DEBUG, (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp))); /* we first need to re-look for the group since we used dest last time */ ip4_addr_copy(groupaddr, igmp->igmp_group_address); group = igmp_lookfor_group(inp, &groupaddr); } else { - LWIP_DEBUGF(IGMP_DEBUG, - (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp))); + LWIP_DEBUGF(IGMP_DEBUG, (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp))); } if (group != NULL) { @@ -439,12 +427,8 @@ igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest) } break; default: - LWIP_DEBUGF(IGMP_DEBUG, - ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n", - igmp->igmp_msgtype, - group->group_state, - (void *)&group, - (void *)inp)); + LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n", + igmp->igmp_msgtype, group->group_state, (void *)&group, (void *)inp)); IGMP_STATS_INC(igmp.proterr); break; } @@ -471,12 +455,10 @@ igmp_joingroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr) /* make sure it is multicast address */ LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); - LWIP_ERROR( - "igmp_joingroup: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); + LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* Should we join this interface ? */ if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) { err = igmp_joingroup_netif(netif, groupaddr); @@ -507,15 +489,11 @@ igmp_joingroup_netif(struct netif *netif, const ip4_addr_t *groupaddr) LWIP_ASSERT_CORE_LOCKED(); /* make sure it is multicast address */ - LWIP_ERROR( - "igmp_joingroup_netif: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); - LWIP_ERROR("igmp_joingroup_netif: attempt to join allsystems address", - (!ip4_addr_cmp(groupaddr, &allsystems)), - return ERR_VAL;); + LWIP_ERROR("igmp_joingroup_netif: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); + LWIP_ERROR("igmp_joingroup_netif: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); /* make sure it is an igmp-enabled netif */ - LWIP_ERROR( - "igmp_joingroup_netif: attempt to join on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;); + LWIP_ERROR("igmp_joingroup_netif: attempt to join on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;); /* find group or create a new one if not found */ group = igmp_lookup_group(netif, groupaddr); @@ -573,14 +551,11 @@ igmp_leavegroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr) LWIP_ASSERT_CORE_LOCKED(); /* make sure it is multicast address */ - LWIP_ERROR( - "igmp_leavegroup: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); - LWIP_ERROR( - "igmp_leavegroup: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); + LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); + LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* Should we leave this interface ? */ if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) { err_t res = igmp_leavegroup_netif(netif, groupaddr); @@ -610,15 +585,11 @@ igmp_leavegroup_netif(struct netif *netif, const ip4_addr_t *groupaddr) LWIP_ASSERT_CORE_LOCKED(); /* make sure it is multicast address */ - LWIP_ERROR( - "igmp_leavegroup_netif: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); - LWIP_ERROR("igmp_leavegroup_netif: attempt to leave allsystems address", - (!ip4_addr_cmp(groupaddr, &allsystems)), - return ERR_VAL;); + LWIP_ERROR("igmp_leavegroup_netif: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;); + LWIP_ERROR("igmp_leavegroup_netif: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;); /* make sure it is an igmp-enabled netif */ - LWIP_ERROR( - "igmp_leavegroup_netif: attempt to leave on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;); + LWIP_ERROR("igmp_leavegroup_netif: attempt to leave on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;); /* find group */ group = igmp_lookfor_group(netif, groupaddr); @@ -671,8 +642,7 @@ igmp_tmr(void) { struct netif *netif; - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { struct igmp_group *group = netif_igmp_data(netif); while (group != NULL) { @@ -698,7 +668,8 @@ igmp_timeout(struct netif *netif, struct igmp_group *group) { /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group (unless it is the allsystems group) */ - if ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) && (!(ip4_addr_cmp(&(group->group_address), &allsystems)))) { + if ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) && + (!(ip4_addr_cmp(&(group->group_address), &allsystems)))) { LWIP_DEBUGF(IGMP_DEBUG, ("igmp_timeout: report membership for group with address ")); ip4_addr_debug_print_val(IGMP_DEBUG, group->group_address); LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)netif)); @@ -722,7 +693,7 @@ igmp_start_timer(struct igmp_group *group, u8_t max_time) { #ifdef LWIP_RAND group->timer = (u16_t)(max_time > 2 ? (LWIP_RAND() % max_time) : 1); -#else /* LWIP_RAND */ +#else /* LWIP_RAND */ /* ATTENTION: use this only if absolutely necessary! */ group->timer = max_time / 2; #endif /* LWIP_RAND */ @@ -742,12 +713,14 @@ static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp) { if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) || - ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) && ((group->timer == 0) || (maxresp < group->timer)))) { + ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) && + ((group->timer == 0) || (maxresp < group->timer)))) { igmp_start_timer(group, maxresp); group->group_state = IGMP_GROUP_DELAYING_MEMBER; } } + /** * Sends an IP packet on a network interface. This function constructs the IP header * and calculates the IP header checksum. If the source IP address is NULL, @@ -784,17 +757,18 @@ igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, static void igmp_send(struct netif *netif, struct igmp_group *group, u8_t type) { - struct pbuf *p = NULL; + struct pbuf *p = NULL; struct igmp_msg *igmp = NULL; - ip4_addr_t src = *IP4_ADDR_ANY4; - ip4_addr_t *dest = NULL; + ip4_addr_t src = *IP4_ADDR_ANY4; + ip4_addr_t *dest = NULL; /* IP header + "router alert" option + IGMP header */ p = pbuf_alloc(PBUF_TRANSPORT, IGMP_MINLEN, PBUF_RAM); if (p) { igmp = (struct igmp_msg *)p->payload; - LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg", (p->len >= sizeof(struct igmp_msg))); + LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg", + (p->len >= sizeof(struct igmp_msg))); ip4_addr_copy(src, *netif_ip4_addr(netif)); if (type == IGMP_V2_MEMB_REPORT) { @@ -809,8 +783,8 @@ igmp_send(struct netif *netif, struct igmp_group *group, u8_t type) } if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) { - igmp->igmp_msgtype = type; - igmp->igmp_maxresp = 0; + igmp->igmp_msgtype = type; + igmp->igmp_maxresp = 0; igmp->igmp_checksum = 0; igmp->igmp_checksum = inet_chksum(igmp, IGMP_MINLEN); diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c index b1385060..5614561a 100644 --- a/src/core/ipv4/ip4.c +++ b/src/core/ipv4/ip4.c @@ -42,20 +42,20 @@ #if LWIP_IPV4 -#include "lwip/autoip.h" +#include "lwip/ip.h" #include "lwip/def.h" +#include "lwip/mem.h" +#include "lwip/ip4_frag.h" +#include "lwip/inet_chksum.h" +#include "lwip/netif.h" #include "lwip/icmp.h" #include "lwip/igmp.h" -#include "lwip/inet_chksum.h" -#include "lwip/ip.h" -#include "lwip/ip4_frag.h" -#include "lwip/mem.h" -#include "lwip/netif.h" #include "lwip/priv/raw_priv.h" -#include "lwip/priv/tcp_priv.h" -#include "lwip/prot/iana.h" -#include "lwip/stats.h" #include "lwip/udp.h" +#include "lwip/priv/tcp_priv.h" +#include "lwip/autoip.h" +#include "lwip/stats.h" +#include "lwip/prot/iana.h" #include @@ -67,16 +67,16 @@ * generate the IP checksum (in contrast to calculating it on-the-fly). */ #ifndef LWIP_INLINE_IP_CHKSUM #if LWIP_CHECKSUM_CTRL_PER_NETIF -#define LWIP_INLINE_IP_CHKSUM 0 +#define LWIP_INLINE_IP_CHKSUM 0 #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */ -#define LWIP_INLINE_IP_CHKSUM 1 +#define LWIP_INLINE_IP_CHKSUM 1 #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ #endif #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP -#define CHECKSUM_GEN_IP_INLINE 1 +#define CHECKSUM_GEN_IP_INLINE 1 #else -#define CHECKSUM_GEN_IP_INLINE 0 +#define CHECKSUM_GEN_IP_INLINE 0 #endif #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT) @@ -89,8 +89,8 @@ */ #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) /* accept DHCP client port and custom port */ -#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) \ - (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) || (LWIP_IP_ACCEPT_UDP_PORT(port))) +#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \ + || (LWIP_IP_ACCEPT_UDP_PORT(port))) #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ /* accept custom port only */ #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port)) @@ -164,8 +164,7 @@ ip4_route(const ip4_addr_t *dest) #endif /* LWIP_MULTICAST_TX_OPTIONS */ /* iterate through netifs */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* is the netif up, does it have a link and a valid address? */ if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { /* network mask matches? */ @@ -189,8 +188,7 @@ ip4_route(const ip4_addr_t *dest) return netif_default; } /* default netif is not up, just use any netif for loopback traffic */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (netif_is_up(netif)) { return netif; } @@ -216,12 +214,8 @@ ip4_route(const ip4_addr_t *dest) ip4_addr_isany_val(*netif_ip4_addr(netif_default))) { /* No matching netif found and default netif is not usable. If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */ - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("ip4_route: No route to %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(dest), - ip4_addr2_16(dest), - ip4_addr3_16(dest), - ip4_addr4_16(dest))); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); IP_STATS_INC(ip.rterr); MIB2_STATS_INC(mib2.ipoutnoroutes); return NULL; @@ -294,24 +288,18 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) /* RFC3927 2.7: do not forward link-local addresses */ if (ip4_addr_islinklocal(ip4_current_dest_addr())) { - LWIP_DEBUGF(IP_DEBUG, - ("ip4_forward: not forwarding LLA %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(ip4_current_dest_addr()), - ip4_addr2_16(ip4_current_dest_addr()), - ip4_addr3_16(ip4_current_dest_addr()), - ip4_addr4_16(ip4_current_dest_addr()))); + LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); goto return_noroute; } /* Find network interface where to forward this IP packet to. */ netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr()); if (netif == NULL) { - LWIP_DEBUGF(IP_DEBUG, - ("ip4_forward: no forwarding route for %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F " found\n", - ip4_addr1_16(ip4_current_dest_addr()), - ip4_addr2_16(ip4_current_dest_addr()), - ip4_addr3_16(ip4_current_dest_addr()), - ip4_addr4_16(ip4_current_dest_addr()))); + LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); /* @todo: send ICMP_DUR_NET? */ goto return_noroute; } @@ -345,12 +333,9 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100))); } - LWIP_DEBUGF(IP_DEBUG, - ("ip4_forward: forwarding packet to %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(ip4_current_dest_addr()), - ip4_addr2_16(ip4_current_dest_addr()), - ip4_addr3_16(ip4_current_dest_addr()), - ip4_addr4_16(ip4_current_dest_addr()))); + LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); IP_STATS_INC(ip.fw); MIB2_STATS_INC(mib2.ipforwdatagrams); @@ -362,7 +347,7 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) { #if IP_FRAG ip4_frag(p, netif, ip4_current_dest_addr()); -#else /* IP_FRAG */ +#else /* IP_FRAG */ /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */ #endif /* IP_FRAG */ } else { @@ -385,14 +370,11 @@ return_noroute: static int ip4_input_accept(struct netif *netif) { - LWIP_DEBUGF(IP_DEBUG, - ("ip_input: iphdr->dest 0x%" X32_F " netif->ip_addr 0x%" X32_F " (0x%" X32_F ", 0x%" X32_F ", 0x%" X32_F - ")\n", - ip4_addr_get_u32(ip4_current_dest_addr()), - ip4_addr_get_u32(netif_ip4_addr(netif)), - ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)), - ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)), - ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif)))); + LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n", + ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)), + ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)), + ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)), + ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif)))); /* interface is up and configured? */ if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) { @@ -403,8 +385,9 @@ ip4_input_accept(struct netif *netif) #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK)) #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */ - ) { - LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n", netif->name[0], netif->name[1])); + ) { + LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n", + netif->name[0], netif->name[1])); /* accept on this netif */ return 1; } @@ -412,7 +395,8 @@ ip4_input_accept(struct netif *netif) /* connections to link-local addresses must persist after changing the netif's address (RFC3927 ch. 1.9) */ if (autoip_accept_packet(netif, ip4_current_dest_addr())) { - LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n", netif->name[0], netif->name[1])); + LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n", + netif->name[0], netif->name[1])); /* accept on this netif */ return 1; } @@ -457,8 +441,7 @@ ip4_input(struct pbuf *p, struct netif *inp) /* identify the IP header */ iphdr = (struct ip_hdr *)p->payload; if (IPH_V(iphdr) != 4) { - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, - ("IP packet dropped due to bad version number %" U16_F "\n", (u16_t)IPH_V(iphdr))); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr))); ip4_debug_print(p); pbuf_free(p); IP_STATS_INC(ip.err); @@ -488,18 +471,17 @@ ip4_input(struct pbuf *p, struct netif *inp) if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) { if (iphdr_hlen < IP_HLEN) { LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("ip4_input: short IP header (%" U16_F " bytes) received, IP packet dropped\n", iphdr_hlen)); + ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen)); } if (iphdr_hlen > p->len) { LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IP header (len %" U16_F ") does not fit in first pbuf (len %" U16_F "), IP packet dropped.\n", - iphdr_hlen, - p->len)); + ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n", + iphdr_hlen, p->len)); } if (iphdr_len > p->tot_len) { - LWIP_DEBUGF( - IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IP (len %" U16_F ") is longer than pbuf (len %" U16_F "), IP packet dropped.\n", iphdr_len, p->tot_len)); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, + ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n", + iphdr_len, p->tot_len)); } /* free (drop) packet pbufs */ pbuf_free(p); @@ -511,12 +493,11 @@ ip4_input(struct pbuf *p, struct netif *inp) /* verify checksum */ #if CHECKSUM_CHECK_IP - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) - { + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) { if (inet_chksum(iphdr, iphdr_hlen) != 0) { LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("Checksum (0x%" X16_F ") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen))); + ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen))); ip4_debug_print(p); pbuf_free(p); IP_STATS_INC(ip.chkerr); @@ -538,14 +519,15 @@ ip4_input(struct pbuf *p, struct netif *inp) /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */ ip4_addr_t allsystems; IP4_ADDR(&allsystems, 224, 0, 0, 1); - if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) && ip4_addr_isany(ip4_current_src_addr())) { + if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) && + ip4_addr_isany(ip4_current_src_addr())) { check_ip_src = 0; } netif = inp; } else { netif = NULL; } -#else /* LWIP_IGMP */ +#else /* LWIP_IGMP */ if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) { netif = inp; } else { @@ -567,8 +549,7 @@ ip4_input(struct pbuf *p, struct netif *inp) #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */ { #if !LWIP_SINGLE_NETIF - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (netif == inp) { /* we checked that before already */ continue; @@ -596,8 +577,8 @@ ip4_input(struct pbuf *p, struct netif *inp) /* remote port is DHCP server? */ if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen); - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, - ("ip4_input: UDP packet to DHCP client port %" U16_F "\n", lwip_ntohs(udphdr->dest))); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n", + lwip_ntohs(udphdr->dest))); if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) { LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n")); netif = inp; @@ -614,10 +595,11 @@ ip4_input(struct pbuf *p, struct netif *inp) /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */ && !ip4_addr_isany_val(*ip4_current_src_addr()) #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ - ) + ) #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */ { - if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) || (ip4_addr_ismulticast(ip4_current_src_addr()))) { + if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) || + (ip4_addr_ismulticast(ip4_current_src_addr()))) { /* packet source is not valid */ LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n")); /* free (drop) packet pbufs */ @@ -651,14 +633,8 @@ ip4_input(struct pbuf *p, struct netif *inp) /* packet consists of multiple fragments? */ if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) { #if IP_REASSEMBLY /* packet fragment reassembly code present? */ - LWIP_DEBUGF(IP_DEBUG, - ("IP packet is a fragment (id=0x%04" X16_F " tot_len=%" U16_F " len=%" U16_F " MF=%" U16_F - " offset=%" U16_F "), calling ip4_reass()\n", - lwip_ntohs(IPH_ID(iphdr)), - p->tot_len, - lwip_ntohs(IPH_LEN(iphdr)), - (u16_t) !!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), - (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8))); + LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip4_reass()\n", + lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8))); /* reassemble the packet*/ p = ip4_reass(p); /* packet not fully reassembled yet? */ @@ -666,11 +642,10 @@ ip4_input(struct pbuf *p, struct netif *inp) return ERR_OK; } iphdr = (const struct ip_hdr *)p->payload; -#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */ +#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */ pbuf_free(p); - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IP packet dropped since it was fragmented (0x%" X16_F ") (while IP_REASSEMBLY == 0).\n", - lwip_ntohs(IPH_OFFSET(iphdr)))); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n", + lwip_ntohs(IPH_OFFSET(iphdr)))); IP_STATS_INC(ip.opterr); IP_STATS_INC(ip.drop); /* unsupported protocol feature */ @@ -683,12 +658,11 @@ ip4_input(struct pbuf *p, struct netif *inp) #if LWIP_IGMP /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */ - if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) { + if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) { #else if (iphdr_hlen > IP_HLEN) { #endif /* LWIP_IGMP */ - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n")); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n")); pbuf_free(p); IP_STATS_INC(ip.opterr); IP_STATS_INC(ip.drop); @@ -701,7 +675,7 @@ ip4_input(struct pbuf *p, struct netif *inp) /* send to upper layers */ LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n")); ip4_debug_print(p); - LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %" U16_F " p->tot_len %" U16_F "\n", p->len, p->tot_len)); + LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len)); ip_data.current_netif = netif; ip_data.current_input_netif = inp; @@ -752,14 +726,14 @@ ip4_input(struct pbuf *p, struct netif *inp) { #if LWIP_ICMP /* send ICMP destination protocol unreachable unless is was a broadcast */ - if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) && !ip4_addr_ismulticast(ip4_current_dest_addr())) { + if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) && + !ip4_addr_ismulticast(ip4_current_dest_addr())) { pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */ icmp_dest_unreach(p, ICMP_DUR_PROTO); } #endif /* LWIP_ICMP */ - LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("Unsupported transport protocol %" U16_F "\n", (u16_t)IPH_PROTO(iphdr))); + LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr))); IP_STATS_INC(ip.proterr); IP_STATS_INC(ip.drop); @@ -807,13 +781,9 @@ ip4_input(struct pbuf *p, struct netif *inp) * unique identifiers independent of destination" */ err_t -ip4_output_if(struct pbuf *p, - const ip4_addr_t *src, - const ip4_addr_t *dest, - u8_t ttl, - u8_t tos, - u8_t proto, - struct netif *netif) +ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, + u8_t proto, struct netif *netif) { #if IP_OPTIONS_SEND return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0); @@ -826,14 +796,8 @@ ip4_output_if(struct pbuf *p, * @ param optlen length of ip_options */ err_t -ip4_output_if_opt(struct pbuf *p, - const ip4_addr_t *src, - const ip4_addr_t *dest, - u8_t ttl, - u8_t tos, - u8_t proto, - struct netif *netif, - void *ip_options, +ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, u16_t optlen) { #endif /* IP_OPTIONS_SEND */ @@ -845,8 +809,9 @@ ip4_output_if_opt(struct pbuf *p, } #if IP_OPTIONS_SEND - return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif, ip_options, optlen); -#else /* IP_OPTIONS_SEND */ + return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif, + ip_options, optlen); +#else /* IP_OPTIONS_SEND */ return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif); #endif /* IP_OPTIONS_SEND */ } @@ -856,13 +821,9 @@ ip4_output_if_opt(struct pbuf *p, * when it is 'any'. */ err_t -ip4_output_if_src(struct pbuf *p, - const ip4_addr_t *src, - const ip4_addr_t *dest, - u8_t ttl, - u8_t tos, - u8_t proto, - struct netif *netif) +ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, + u8_t proto, struct netif *netif) { #if IP_OPTIONS_SEND return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0); @@ -873,14 +834,8 @@ ip4_output_if_src(struct pbuf *p, * when it is 'any'. */ err_t -ip4_output_if_opt_src(struct pbuf *p, - const ip4_addr_t *src, - const ip4_addr_t *dest, - u8_t ttl, - u8_t tos, - u8_t proto, - struct netif *netif, - void *ip_options, +ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, u16_t optlen) { #endif /* IP_OPTIONS_SEND */ @@ -943,7 +898,8 @@ ip4_output_if_opt_src(struct pbuf *p, } iphdr = (struct ip_hdr *)p->payload; - LWIP_ASSERT("check that first pbuf can hold struct ip_hdr", (p->len >= sizeof(struct ip_hdr))); + LWIP_ASSERT("check that first pbuf can hold struct ip_hdr", + (p->len >= sizeof(struct ip_hdr))); IPH_TTL_SET(iphdr, ttl); IPH_PROTO_SET(iphdr, proto); @@ -987,14 +943,20 @@ ip4_output_if_opt_src(struct pbuf *p, chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF); chk_sum = (chk_sum >> 16) + chk_sum; chk_sum = ~chk_sum; - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { iphdr->_chksum = (u16_t)chk_sum; /* network order */ } + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { + iphdr->_chksum = (u16_t)chk_sum; /* network order */ + } #if LWIP_CHECKSUM_CTRL_PER_NETIF - else { IPH_CHKSUM_SET(iphdr, 0); } + else { + IPH_CHKSUM_SET(iphdr, 0); + } #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/ -#else /* CHECKSUM_GEN_IP_INLINE */ +#else /* CHECKSUM_GEN_IP_INLINE */ IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); } + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { + IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); + } #endif /* CHECKSUM_GEN_IP */ #endif /* CHECKSUM_GEN_IP_INLINE */ } else { @@ -1012,7 +974,7 @@ ip4_output_if_opt_src(struct pbuf *p, IP_STATS_INC(ip.xmit); - LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%" U16_F "\n", netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num)); ip4_debug_print(p); #if ENABLE_LOOPBACK @@ -1020,7 +982,7 @@ ip4_output_if_opt_src(struct pbuf *p, #if !LWIP_HAVE_LOOPIF || ip4_addr_isloopback(dest) #endif /* !LWIP_HAVE_LOOPIF */ - ) { + ) { /* Packet to self, enqueue it for loopback */ LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()")); return netif_loop_output(netif, p); @@ -1060,19 +1022,16 @@ ip4_output_if_opt_src(struct pbuf *p, * see ip_output_if() for more return values */ err_t -ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, u8_t ttl, u8_t tos, u8_t proto) +ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, u8_t proto) { struct netif *netif; LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); if ((netif = ip4_route_src(src, dest)) == NULL) { - LWIP_DEBUGF(IP_DEBUG, - ("ip4_output: No route to %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(dest), - ip4_addr2_16(dest), - ip4_addr3_16(dest), - ip4_addr4_16(dest))); + LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); IP_STATS_INC(ip.rterr); return ERR_RTE; } @@ -1100,13 +1059,8 @@ ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, u8_t t * see ip_output_if() for more return values */ err_t -ip4_output_hinted(struct pbuf *p, - const ip4_addr_t *src, - const ip4_addr_t *dest, - u8_t ttl, - u8_t tos, - u8_t proto, - struct netif_hint *netif_hint) +ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, + u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint) { struct netif *netif; err_t err; @@ -1114,12 +1068,8 @@ ip4_output_hinted(struct pbuf *p, LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); if ((netif = ip4_route_src(src, dest)) == NULL) { - LWIP_DEBUGF(IP_DEBUG, - ("ip4_output: No route to %" U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n", - ip4_addr1_16(dest), - ip4_addr2_16(dest), - ip4_addr3_16(dest), - ip4_addr4_16(dest))); + LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", + ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); IP_STATS_INC(ip.rterr); return ERR_RTE; } @@ -1143,40 +1093,35 @@ ip4_debug_print(struct pbuf *p) LWIP_DEBUGF(IP_DEBUG, ("IP header:\n")); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP_DEBUG, - ("|%2" S16_F " |%2" S16_F " | 0x%02" X16_F " | %5" U16_F " | (v, hl, tos, len)\n", - (u16_t)IPH_V(iphdr), - (u16_t)IPH_HL(iphdr), - (u16_t)IPH_TOS(iphdr), - lwip_ntohs(IPH_LEN(iphdr)))); + LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n", + (u16_t)IPH_V(iphdr), + (u16_t)IPH_HL(iphdr), + (u16_t)IPH_TOS(iphdr), + lwip_ntohs(IPH_LEN(iphdr)))); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP_DEBUG, - ("| %5" U16_F " |%" U16_F "%" U16_F "%" U16_F "| %4" U16_F " | (id, flags, offset)\n", - lwip_ntohs(IPH_ID(iphdr)), - (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1), - (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1), - (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1), - (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK))); + LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n", + lwip_ntohs(IPH_ID(iphdr)), + (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1), + (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1), + (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1), + (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK))); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP_DEBUG, - ("| %3" U16_F " | %3" U16_F " | 0x%04" X16_F " | (ttl, proto, chksum)\n", - (u16_t)IPH_TTL(iphdr), - (u16_t)IPH_PROTO(iphdr), - lwip_ntohs(IPH_CHKSUM(iphdr)))); + LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n", + (u16_t)IPH_TTL(iphdr), + (u16_t)IPH_PROTO(iphdr), + lwip_ntohs(IPH_CHKSUM(iphdr)))); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP_DEBUG, - ("| %3" U16_F " | %3" U16_F " | %3" U16_F " | %3" U16_F " | (src)\n", - ip4_addr1_16_val(iphdr->src), - ip4_addr2_16_val(iphdr->src), - ip4_addr3_16_val(iphdr->src), - ip4_addr4_16_val(iphdr->src))); + LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n", + ip4_addr1_16_val(iphdr->src), + ip4_addr2_16_val(iphdr->src), + ip4_addr3_16_val(iphdr->src), + ip4_addr4_16_val(iphdr->src))); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP_DEBUG, - ("| %3" U16_F " | %3" U16_F " | %3" U16_F " | %3" U16_F " | (dest)\n", - ip4_addr1_16_val(iphdr->dest), - ip4_addr2_16_val(iphdr->dest), - ip4_addr3_16_val(iphdr->dest), - ip4_addr4_16_val(iphdr->dest))); + LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n", + ip4_addr1_16_val(iphdr->dest), + ip4_addr2_16_val(iphdr->dest), + ip4_addr3_16_val(iphdr->dest), + ip4_addr4_16_val(iphdr->dest))); LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); } #endif /* IP_DEBUG */ diff --git a/src/core/ipv4/ip4_addr.c b/src/core/ipv4/ip4_addr.c index 6d664820..33204d11 100644 --- a/src/core/ipv4/ip4_addr.c +++ b/src/core/ipv4/ip4_addr.c @@ -61,7 +61,8 @@ ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif) ip4_addr_set_u32(&ipaddr, addr); /* all ones (broadcast) or all zeroes (old skool broadcast) */ - if ((~addr == IPADDR_ANY) || (addr == IPADDR_ANY)) { + if ((~addr == IPADDR_ANY) || + (addr == IPADDR_ANY)) { return 1; /* no broadcast support on this network interface? */ } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) { @@ -95,7 +96,7 @@ ip4_addr_netmask_valid(u32_t netmask) u32_t nm_hostorder = lwip_htonl(netmask); /* first, check for the first zero */ - for (mask = 1UL << 31; mask != 0; mask >>= 1) { + for (mask = 1UL << 31 ; mask != 0; mask >>= 1) { if ((nm_hostorder & mask) == 0) { break; } @@ -210,12 +211,12 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) switch (pp - parts + 1) { case 0: - return 0; /* initial nondigit */ + return 0; /* initial nondigit */ - case 1: /* a -- 32 bits */ + case 1: /* a -- 32 bits */ break; - case 2: /* a.b -- 8.24 bits */ + case 2: /* a.b -- 8.24 bits */ if (val > 0xffffffUL) { return 0; } @@ -225,7 +226,7 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) val |= parts[0] << 24; break; - case 3: /* a.b.c -- 8.8.16 bits */ + case 3: /* a.b.c -- 8.8.16 bits */ if (val > 0xffff) { return 0; } @@ -235,7 +236,7 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) val |= (parts[0] << 24) | (parts[1] << 16); break; - case 4: /* a.b.c.d -- 8.8.8.8 bits */ + case 4: /* a.b.c.d -- 8.8.8.8 bits */ if (val > 0xff) { return 0; } diff --git a/src/core/ipv4/ip4_frag.c b/src/core/ipv4/ip4_frag.c index 58ff06b7..a445530e 100644 --- a/src/core/ipv4/ip4_frag.c +++ b/src/core/ipv4/ip4_frag.c @@ -42,12 +42,12 @@ #if LWIP_IPV4 -#include "lwip/def.h" -#include "lwip/icmp.h" -#include "lwip/inet_chksum.h" #include "lwip/ip4_frag.h" +#include "lwip/def.h" +#include "lwip/inet_chksum.h" #include "lwip/netif.h" #include "lwip/stats.h" +#include "lwip/icmp.h" #include @@ -79,9 +79,9 @@ #define IP_REASS_FLAG_LASTFRAG 0x01 -#define IP_REASS_VALIDATE_TELEGRAM_FINISHED 1 -#define IP_REASS_VALIDATE_PBUF_QUEUED 0 -#define IP_REASS_VALIDATE_PBUF_DROPPED -1 +#define IP_REASS_VALIDATE_TELEGRAM_FINISHED 1 +#define IP_REASS_VALIDATE_PBUF_QUEUED 0 +#define IP_REASS_VALIDATE_PBUF_DROPPED -1 /** This is a helper struct which holds the starting * offset and the ending offset of this fragment to @@ -92,35 +92,31 @@ * this struct doesn't need packing, too.) */ #ifdef PACK_STRUCT_USE_INCLUDES -#include "arch/bpstruct.h" +# include "arch/bpstruct.h" #endif PACK_STRUCT_BEGIN -struct ip_reass_helper -{ +struct ip_reass_helper { PACK_STRUCT_FIELD(struct pbuf *next_pbuf); PACK_STRUCT_FIELD(u16_t start); PACK_STRUCT_FIELD(u16_t end); } PACK_STRUCT_STRUCT; PACK_STRUCT_END #ifdef PACK_STRUCT_USE_INCLUDES -#include "arch/epstruct.h" +# include "arch/epstruct.h" #endif -#define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \ - (ip4_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && ip4_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \ - IPH_ID(iphdrA) == IPH_ID(iphdrB)) \ - ? 1 \ - : 0 +#define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \ + (ip4_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \ + ip4_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \ + IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0 /* global variables */ static struct ip_reassdata *reassdatagrams; static u16_t ip_reass_pbufcount; /* function prototypes */ -static void -ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); -static int -ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); +static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); +static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); /** * Reassembly timer base function @@ -139,7 +135,7 @@ ip_reass_tmr(void) * clean up the incomplete fragment assembly */ if (r->timer > 0) { r->timer--; - LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %" U16_F "\n", (u16_t)r->timer)); + LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n", (u16_t)r->timer)); prev = r; r = r->next; } else { @@ -282,7 +278,7 @@ static struct ip_reassdata * ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen) { struct ip_reassdata *ipr; -#if !IP_REASS_FREE_OLDEST +#if ! IP_REASS_FREE_OLDEST LWIP_UNUSED_ARG(clen); #endif @@ -368,7 +364,8 @@ ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct /* overwrite the fragment's ip header from the pbuf with our helper struct, * and setup the embedded helper structure. */ /* make sure the struct ip_reass_helper fits into the IP header */ - LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN", sizeof(struct ip_reass_helper) <= IP_HLEN); + LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN", + sizeof(struct ip_reass_helper) <= IP_HLEN); iprh = (struct ip_reass_helper *)new_p->payload; iprh->next_pbuf = NULL; iprh->start = offset; @@ -446,7 +443,8 @@ ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct } } else { #if IP_REASS_CHECK_OVERLAP - LWIP_ASSERT("no previous fragment, this must be the first fragment!", ipr->p == NULL); + LWIP_ASSERT("no previous fragment, this must be the first fragment!", + ipr->p == NULL); #endif /* IP_REASS_CHECK_OVERLAP */ /* this is the first fragment we ever received for this ip datagram */ ipr->p = new_p; @@ -479,8 +477,10 @@ ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct * (because to the MF==0 already arrived */ if (valid) { LWIP_ASSERT("sanity check", ipr->p != NULL); - LWIP_ASSERT("sanity check", ((struct ip_reass_helper *)ipr->p->payload) != iprh); - LWIP_ASSERT("validate_datagram:next_pbuf!=NULL", iprh->next_pbuf == NULL); + LWIP_ASSERT("sanity check", + ((struct ip_reass_helper *)ipr->p->payload) != iprh); + LWIP_ASSERT("validate_datagram:next_pbuf!=NULL", + iprh->next_pbuf == NULL); } } } @@ -535,13 +535,13 @@ ip4_reass(struct pbuf *p) clen = pbuf_clen(p); if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) { #if IP_REASS_FREE_OLDEST - if (!ip_reass_remove_oldest_datagram(fraghdr, clen) || ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS)) + if (!ip_reass_remove_oldest_datagram(fraghdr, clen) || + ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS)) #endif /* IP_REASS_FREE_OLDEST */ { /* No datagram could be freed and still too many pbufs enqueued */ - LWIP_DEBUGF( - IP_REASS_DEBUG, - ("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n", ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS)); + LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n", + ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS)); IPFRAG_STATS_INC(ip_frag.memerr); /* @todo: send ICMP time exceeded here? */ /* drop this pbuf */ @@ -556,8 +556,8 @@ ip4_reass(struct pbuf *p) in the reassembly buffer. If so, we proceed with copying the fragment into the buffer. */ if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) { - LWIP_DEBUGF(IP_REASS_DEBUG, - ("ip4_reass: matching previous fragment ID=%" X16_F "\n", lwip_ntohs(IPH_ID(fraghdr)))); + LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: matching previous fragment ID=%"X16_F"\n", + lwip_ntohs(IPH_ID(fraghdr)))); IPFRAG_STATS_INC(ip_frag.cachehit); break; } @@ -609,7 +609,9 @@ ip4_reass(struct pbuf *p) u16_t datagram_len = (u16_t)(offset + len); ipr->datagram_len = datagram_len; ipr->flags |= IP_REASS_FLAG_LASTFRAG; - LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: last fragment seen, total len %" S16_F "\n", ipr->datagram_len)); + LWIP_DEBUGF(IP_REASS_DEBUG, + ("ip4_reass: last fragment seen, total len %"S16_F"\n", + ipr->datagram_len)); } if (valid == IP_REASS_VALIDATE_TELEGRAM_FINISHED) { @@ -629,8 +631,7 @@ ip4_reass(struct pbuf *p) IPH_CHKSUM_SET(fraghdr, 0); /* @todo: do we need to set/calculate the correct checksum? */ #if CHECKSUM_GEN_IP - IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) - { + IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) { IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN)); } #endif /* CHECKSUM_GEN_IP */ @@ -779,7 +780,8 @@ ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest) if (rambuf == NULL) { goto memerr; } - LWIP_ASSERT("this needs a pbuf in one piece!", (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL)); + LWIP_ASSERT("this needs a pbuf in one piece!", + (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL)); poff += pbuf_copy_partial(p, rambuf->payload, fragsize, poff); /* make room for the IP header */ if (pbuf_add_header(rambuf, IP_HLEN)) { @@ -789,7 +791,7 @@ ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest) /* fill in the IP header */ SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN); iphdr = (struct ip_hdr *)rambuf->payload; -#else /* LWIP_NETIF_TX_SINGLE_PBUF */ +#else /* LWIP_NETIF_TX_SINGLE_PBUF */ /* When not using a static buffer, create a chain of pbufs. * The first will be a PBUF_RAM holding the link and IP header. * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged, @@ -799,7 +801,8 @@ ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest) if (rambuf == NULL) { goto memerr; } - LWIP_ASSERT("this needs a pbuf in one piece!", (rambuf->len >= (IP_HLEN))); + LWIP_ASSERT("this needs a pbuf in one piece!", + (rambuf->len >= (IP_HLEN))); SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN); iphdr = (struct ip_hdr *)rambuf->payload; @@ -821,7 +824,8 @@ ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest) goto memerr; } /* Mirror this pbuf, although we might not need all of it. */ - newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, (u8_t *)p->payload + poff, newpbuflen); + newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, + (u8_t *)p->payload + poff, newpbuflen); if (newpbuf == NULL) { ip_frag_free_pbuf_custom_ref(pcr); pbuf_free(rambuf); @@ -857,7 +861,9 @@ ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest) IPH_LEN_SET(iphdr, lwip_htons((u16_t)(fragsize + IP_HLEN))); IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); } + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { + IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); + } #endif /* CHECKSUM_GEN_IP */ /* No need for separate header pbuf - we allowed room for it in rambuf diff --git a/src/core/ipv6/dhcp6.c b/src/core/ipv6/dhcp6.c index fe2f18b5..7cf98a52 100644 --- a/src/core/ipv6/dhcp6.c +++ b/src/core/ipv6/dhcp6.c @@ -59,11 +59,11 @@ #if LWIP_IPV6 && LWIP_IPV6_DHCP6 /* don't build if not configured for use in lwipopts.h */ -#include "lwip/def.h" #include "lwip/dhcp6.h" -#include "lwip/dns.h" #include "lwip/prot/dhcp6.h" +#include "lwip/def.h" #include "lwip/udp.h" +#include "lwip/dns.h" #include @@ -74,10 +74,7 @@ #define LWIP_HOOK_DHCP6_APPEND_OPTIONS(netif, dhcp6, state, msg, msg_type, options_len_ptr, max_len) #endif #ifndef LWIP_HOOK_DHCP6_PARSE_OPTION -#define LWIP_HOOK_DHCP6_PARSE_OPTION(netif, dhcp6, state, msg, msg_type, option, len, pbuf, offset) \ - do { \ - LWIP_UNUSED_ARG(msg); \ - } while (0) +#define LWIP_HOOK_DHCP6_PARSE_OPTION(netif, dhcp6, state, msg, msg_type, option, len, pbuf, offset) do { LWIP_UNUSED_ARG(msg); } while(0) #endif #if LWIP_DNS && LWIP_DHCP6_MAX_DNS_SERVERS @@ -90,13 +87,13 @@ #define LWIP_DHCP6_PROVIDE_DNS_SERVERS 0 #endif + /** Option handling: options are parsed in dhcp6_parse_reply * and saved in an array where other functions can load them from. * This might be moved into the struct dhcp6 (not necessarily since * lwIP is single-threaded and the array is only used while in recv * callback). */ -enum dhcp6_option_idx -{ +enum dhcp6_option_idx { DHCP6_OPTION_IDX_CLI_ID = 0, DHCP6_OPTION_IDX_SERVER_ID, #if LWIP_DHCP6_PROVIDE_DNS_SERVERS @@ -109,8 +106,7 @@ enum dhcp6_option_idx DHCP6_OPTION_IDX_MAX }; -struct dhcp6_option_info -{ +struct dhcp6_option_info { u8_t option_given; u16_t val_start; u16_t val_length; @@ -119,17 +115,14 @@ struct dhcp6_option_info /** Holds the decoded option info, only valid while in dhcp6_recv. */ struct dhcp6_option_info dhcp6_rx_options[DHCP6_OPTION_IDX_MAX]; -#define dhcp6_option_given(dhcp6, idx) (dhcp6_rx_options[idx].option_given != 0) -#define dhcp6_got_option(dhcp6, idx) (dhcp6_rx_options[idx].option_given = 1) -#define dhcp6_clear_option(dhcp6, idx) (dhcp6_rx_options[idx].option_given = 0) -#define dhcp6_clear_all_options(dhcp6) (memset(dhcp6_rx_options, 0, sizeof(dhcp6_rx_options))) -#define dhcp6_get_option_start(dhcp6, idx) (dhcp6_rx_options[idx].val_start) -#define dhcp6_get_option_length(dhcp6, idx) (dhcp6_rx_options[idx].val_length) -#define dhcp6_set_option(dhcp6, idx, start, len) \ - do { \ - dhcp6_rx_options[idx].val_start = (start); \ - dhcp6_rx_options[idx].val_length = (len); \ - } while (0) +#define dhcp6_option_given(dhcp6, idx) (dhcp6_rx_options[idx].option_given != 0) +#define dhcp6_got_option(dhcp6, idx) (dhcp6_rx_options[idx].option_given = 1) +#define dhcp6_clear_option(dhcp6, idx) (dhcp6_rx_options[idx].option_given = 0) +#define dhcp6_clear_all_options(dhcp6) (memset(dhcp6_rx_options, 0, sizeof(dhcp6_rx_options))) +#define dhcp6_get_option_start(dhcp6, idx) (dhcp6_rx_options[idx].val_start) +#define dhcp6_get_option_length(dhcp6, idx) (dhcp6_rx_options[idx].val_length) +#define dhcp6_set_option(dhcp6, idx, start, len) do { dhcp6_rx_options[idx].val_start = (start); dhcp6_rx_options[idx].val_length = (len); }while(0) + const ip_addr_t dhcp6_All_DHCP6_Relay_Agents_and_Servers = IPADDR6_INIT_HOST(0xFF020000, 0, 0, 0x00010002); const ip_addr_t dhcp6_All_DHCP6_Servers = IPADDR6_INIT_HOST(0xFF020000, 0, 0, 0x00010003); @@ -137,9 +130,9 @@ const ip_addr_t dhcp6_All_DHCP6_Servers = IPADDR6_INIT_HOST(0xFF020000, 0, 0, 0x static struct udp_pcb *dhcp6_pcb; static u8_t dhcp6_pcb_refcount; + /* receive, unfold, parse and free incoming messages */ -static void -dhcp6_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); +static void dhcp6_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); /** Ensure DHCP PCB is allocated and bound */ static err_t @@ -210,8 +203,7 @@ dhcp6_set_struct(struct netif *netif, struct dhcp6 *dhcp6) * * @param netif the netif from which to remove the struct dhcp */ -void -dhcp6_cleanup(struct netif *netif) +void dhcp6_cleanup(struct netif *netif) { LWIP_ASSERT("netif != NULL", netif != NULL); @@ -221,7 +213,7 @@ dhcp6_cleanup(struct netif *netif) } } -static struct dhcp6 * +static struct dhcp6* dhcp6_get_struct(struct netif *netif, const char *dbg_requester) { struct dhcp6 *dhcp6 = netif_dhcp6_data(netif); @@ -261,8 +253,8 @@ dhcp6_get_struct(struct netif *netif, const char *dbg_requester) static void dhcp6_set_state(struct dhcp6 *dhcp6, u8_t new_state, const char *dbg_caller) { - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("DHCPv6 state: %d -> %d (%s)\n", dhcp6->state, new_state, dbg_caller)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("DHCPv6 state: %d -> %d (%s)\n", + dhcp6->state, new_state, dbg_caller)); if (new_state != dhcp6->state) { dhcp6->state = new_state; dhcp6->tries = 0; @@ -273,7 +265,8 @@ dhcp6_set_state(struct dhcp6 *dhcp6, u8_t new_state, const char *dbg_caller) static int dhcp6_stateless_enabled(struct dhcp6 *dhcp6) { - if ((dhcp6->state == DHCP6_STATE_STATELESS_IDLE) || (dhcp6->state == DHCP6_STATE_REQUESTING_CONFIG)) { + if ((dhcp6->state == DHCP6_STATE_STATELESS_IDLE) || + (dhcp6->state == DHCP6_STATE_REQUESTING_CONFIG)) { return 1; } return 0; @@ -324,12 +317,7 @@ dhcp6_enable_stateless(struct netif *netif) { struct dhcp6 *dhcp6; - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp6_enable_stateless(netif=%p) %c%c%" U16_F "\n", - (void *)netif, - netif->name[0], - netif->name[1], - (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp6_enable_stateless(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); dhcp6 = dhcp6_get_struct(netif, "dhcp6_enable_stateless()"); if (dhcp6 == NULL) { @@ -359,16 +347,13 @@ dhcp6_disable(struct netif *netif) { struct dhcp6 *dhcp6; - LWIP_DEBUGF( - DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp6_disable(netif=%p) %c%c%" U16_F "\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp6_disable(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); dhcp6 = netif_dhcp6_data(netif); if (dhcp6 != NULL) { if (dhcp6->state != DHCP6_STATE_OFF) { - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, - ("dhcp6_disable(): DHCPv6 disabled (old state: %s)\n", - (dhcp6_stateless_enabled(dhcp6) ? "stateless" : "stateful"))); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("dhcp6_disable(): DHCPv6 disabled (old state: %s)\n", + (dhcp6_stateless_enabled(dhcp6) ? "stateless" : "stateful"))); dhcp6_set_state(dhcp6, DHCP6_STATE_OFF, "dhcp6_disable"); if (dhcp6->pcb_allocated != 0) { dhcp6_dec_pcb_refcount(); /* free DHCPv6 PCB if not needed any more */ @@ -389,11 +374,8 @@ dhcp6_disable(struct netif *netif) * @return a pbuf for the message */ static struct pbuf * -dhcp6_create_msg(struct netif *netif, - struct dhcp6 *dhcp6, - u8_t message_type, - u16_t opt_len_alloc, - u16_t *options_out_len) +dhcp6_create_msg(struct netif *netif, struct dhcp6 *dhcp6, u8_t message_type, + u16_t opt_len_alloc, u16_t *options_out_len) { struct pbuf *p_out; struct dhcp6_msg *msg_out; @@ -415,7 +397,8 @@ dhcp6_create_msg(struct netif *netif, dhcp6->xid = LWIP_RAND() & 0xFFFFFF; } - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("transaction id xid(%" X32_F ")\n", dhcp6->xid)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, + ("transaction id xid(%"X32_F")\n", dhcp6->xid)); msg_out = (struct dhcp6_msg *)p_out->payload; memset(msg_out, 0, sizeof(struct dhcp6_msg) + opt_len_alloc); @@ -432,22 +415,19 @@ static u16_t dhcp6_option_short(u16_t options_out_len, u8_t *options, u16_t value) { options[options_out_len++] = (u8_t)((value & 0xff00U) >> 8); - options[options_out_len++] = (u8_t)(value & 0x00ffU); + options[options_out_len++] = (u8_t) (value & 0x00ffU); return options_out_len; } static u16_t -dhcp6_option_optionrequest(u16_t options_out_len, - u8_t *options, - const u16_t *req_options, - u16_t num_req_options, - u16_t max_len) +dhcp6_option_optionrequest(u16_t options_out_len, u8_t *options, const u16_t *req_options, + u16_t num_req_options, u16_t max_len) { size_t i; u16_t ret; LWIP_ASSERT("dhcp6_option_optionrequest: options_out_len + sizeof(struct dhcp6_msg) + addlen <= max_len", - sizeof(struct dhcp6_msg) + options_out_len + 4U + (2U * num_req_options) <= max_len); + sizeof(struct dhcp6_msg) + options_out_len + 4U + (2U * num_req_options) <= max_len); LWIP_UNUSED_ARG(max_len); ret = dhcp6_option_short(options_out_len, options, DHCP6_OPTION_ORO); @@ -466,11 +446,12 @@ dhcp6_msg_finalize(u16_t options_out_len, struct pbuf *p_out) pbuf_realloc(p_out, (u16_t)(sizeof(struct dhcp6_msg) + options_out_len)); } + #if LWIP_IPV6_DHCP6_STATELESS static void dhcp6_information_request(struct netif *netif, struct dhcp6 *dhcp6) { - const u16_t requested_options[] = { DHCP6_OPTION_DNS_SERVERS, DHCP6_OPTION_DOMAIN_LIST, DHCP6_OPTION_SNTP_SERVERS }; + const u16_t requested_options[] = {DHCP6_OPTION_DNS_SERVERS, DHCP6_OPTION_DOMAIN_LIST, DHCP6_OPTION_SNTP_SERVERS}; u16_t msecs; struct pbuf *p_out; u16_t options_out_len; @@ -483,20 +464,18 @@ dhcp6_information_request(struct netif *netif, struct dhcp6 *dhcp6) u8_t *options = (u8_t *)(msg_out + 1); LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("dhcp6_information_request: making request\n")); - options_out_len = dhcp6_option_optionrequest( - options_out_len, options, requested_options, LWIP_ARRAYSIZE(requested_options), p_out->len); - LWIP_HOOK_DHCP6_APPEND_OPTIONS( - netif, dhcp6, DHCP6_STATE_REQUESTING_CONFIG, msg_out, DHCP6_INFOREQUEST, options_out_len, p_out->len); + options_out_len = dhcp6_option_optionrequest(options_out_len, options, requested_options, + LWIP_ARRAYSIZE(requested_options), p_out->len); + LWIP_HOOK_DHCP6_APPEND_OPTIONS(netif, dhcp6, DHCP6_STATE_REQUESTING_CONFIG, msg_out, + DHCP6_INFOREQUEST, options_out_len, p_out->len); dhcp6_msg_finalize(options_out_len, p_out); err = udp_sendto_if(dhcp6_pcb, p_out, &dhcp6_All_DHCP6_Relay_Agents_and_Servers, DHCP6_SERVER_PORT, netif); pbuf_free(p_out); - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp6_information_request: INFOREQUESTING -> %d\n", (int)err)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp6_information_request: INFOREQUESTING -> %d\n", (int)err)); LWIP_UNUSED_ARG(err); } else { - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, - ("dhcp6_information_request: could not allocate DHCP6 request\n")); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp6_information_request: could not allocate DHCP6 request\n")); } dhcp6_set_state(dhcp6, DHCP6_STATE_REQUESTING_CONFIG, "dhcp6_information_request"); if (dhcp6->tries < 255) { @@ -504,8 +483,7 @@ dhcp6_information_request(struct netif *netif, struct dhcp6 *dhcp6) } msecs = (u16_t)((dhcp6->tries < 6 ? 1 << dhcp6->tries : 60) * 1000); dhcp6->request_timeout = (u16_t)((msecs + DHCP6_TIMER_MSECS - 1) / DHCP6_TIMER_MSECS); - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, - ("dhcp6_information_request(): set request timeout %" U16_F " msecs\n", msecs)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp6_information_request(): set request timeout %"U16_F" msecs\n", msecs)); } static err_t @@ -698,9 +676,9 @@ dhcp6_parse_reply(struct pbuf *p, struct dhcp6 *dhcp6) break; #endif /* LWIP_DHCP6_GET_NTP_SRV*/ default: - LWIP_DEBUGF(DHCP6_DEBUG, ("skipping option %" U16_F " in options\n", op)); - LWIP_HOOK_DHCP6_PARSE_OPTION( - ip_current_netif(), dhcp6, dhcp6->state, msg_in, msg_in->msgtype, op, len, q, val_offset); + LWIP_DEBUGF(DHCP6_DEBUG, ("skipping option %"U16_F" in options\n", op)); + LWIP_HOOK_DHCP6_PARSE_OPTION(ip_current_netif(), dhcp6, dhcp6->state, msg_in, + msg_in->msgtype, op, len, q, val_offset); break; } } @@ -725,10 +703,10 @@ dhcp6_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr LWIP_ERROR("invalid server address type", IP_IS_V6(addr), goto free_pbuf_and_return;); - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, - ("dhcp6_recv(pbuf = %p) from DHCPv6 server %s port %" U16_F "\n", (void *)p, ipaddr_ntoa(addr), port)); - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %" U16_F "\n", p->len)); - LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %" U16_F "\n", p->tot_len)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("dhcp6_recv(pbuf = %p) from DHCPv6 server %s port %"U16_F"\n", (void *)p, + ipaddr_ntoa(addr), port)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len)); + LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len)); /* prevent warnings about unused arguments */ LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); @@ -745,7 +723,7 @@ dhcp6_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr xid |= reply_msg->transaction_id[2]; if (xid != dhcp6->xid) { LWIP_DEBUGF(DHCP6_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, - ("transaction id mismatch reply_msg->xid(%" X32_F ")!= dhcp6->xid(%" X32_F ")\n", xid, dhcp6->xid)); + ("transaction id mismatch reply_msg->xid(%"X32_F")!= dhcp6->xid(%"X32_F")\n", xid, dhcp6->xid)); goto free_pbuf_and_return; } /* option fields could be unfold? */ @@ -813,8 +791,7 @@ dhcp6_tmr(void) { struct netif *netif; /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { struct dhcp6 *dhcp6 = netif_dhcp6_data(netif); /* only act on DHCPv6 configured interfaces */ if (dhcp6 != NULL) { diff --git a/src/core/ipv6/ethip6.c b/src/core/ipv6/ethip6.c index e4268096..fec8b28a 100644 --- a/src/core/ipv6/ethip6.c +++ b/src/core/ipv6/ethip6.c @@ -44,13 +44,13 @@ #if LWIP_IPV6 && LWIP_ETHERNET #include "lwip/ethip6.h" -#include "lwip/icmp6.h" -#include "lwip/inet_chksum.h" +#include "lwip/nd6.h" +#include "lwip/pbuf.h" #include "lwip/ip6.h" #include "lwip/ip6_addr.h" -#include "lwip/nd6.h" +#include "lwip/inet_chksum.h" #include "lwip/netif.h" -#include "lwip/pbuf.h" +#include "lwip/icmp6.h" #include "lwip/prot/ethernet.h" #include "netif/ethernet.h" @@ -98,7 +98,7 @@ ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr) dest.addr[5] = ((const u8_t *)(&(ip6addr->addr[3])))[3]; /* Send out. */ - return ethernet_output(netif, q, (const struct eth_addr *)(netif->hwaddr), &dest, ETHTYPE_IPV6); + return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6); } /* We have a unicast destination IP address */ @@ -117,7 +117,7 @@ ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr) /* Send out the packet using the returned hardware address. */ SMEMCPY(dest.addr, hwaddr, 6); - return ethernet_output(netif, q, (const struct eth_addr *)(netif->hwaddr), &dest, ETHTYPE_IPV6); + return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6); } #endif /* LWIP_IPV6 && LWIP_ETHERNET */ diff --git a/src/core/ipv6/icmp6.c b/src/core/ipv6/icmp6.c index f9715836..167738a0 100644 --- a/src/core/ipv6/icmp6.c +++ b/src/core/ipv6/icmp6.c @@ -44,42 +44,31 @@ #if LWIP_ICMP6 && LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ #include "lwip/icmp6.h" -#include "lwip/inet_chksum.h" -#include "lwip/ip.h" +#include "lwip/prot/icmp6.h" #include "lwip/ip6.h" #include "lwip/ip6_addr.h" -#include "lwip/mld6.h" -#include "lwip/nd6.h" -#include "lwip/netif.h" +#include "lwip/inet_chksum.h" #include "lwip/pbuf.h" -#include "lwip/prot/icmp6.h" +#include "lwip/netif.h" +#include "lwip/nd6.h" +#include "lwip/mld6.h" +#include "lwip/ip.h" #include "lwip/stats.h" #include #if LWIP_ICMP6_DATASIZE == 0 #undef LWIP_ICMP6_DATASIZE -#define LWIP_ICMP6_DATASIZE 8 +#define LWIP_ICMP6_DATASIZE 8 #endif /* Forward declarations */ -static void -icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type); -static void -icmp6_send_response_with_addrs(struct pbuf *p, - u8_t code, - u32_t data, - u8_t type, - const ip6_addr_t *src_addr, - const ip6_addr_t *dest_addr); -static void -icmp6_send_response_with_addrs_and_netif(struct pbuf *p, - u8_t code, - u32_t data, - u8_t type, - const ip6_addr_t *src_addr, - const ip6_addr_t *dest_addr, - struct netif *netif); +static void icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type); +static void icmp6_send_response_with_addrs(struct pbuf *p, u8_t code, u32_t data, + u8_t type, const ip6_addr_t *src_addr, const ip6_addr_t *dest_addr); +static void icmp6_send_response_with_addrs_and_netif(struct pbuf *p, u8_t code, u32_t data, + u8_t type, const ip6_addr_t *src_addr, const ip6_addr_t *dest_addr, struct netif *netif); + /** * Process an input ICMPv6 message. Called by ip6_input. @@ -111,9 +100,9 @@ icmp6_input(struct pbuf *p, struct netif *inp) icmp6hdr = (struct icmp6_hdr *)p->payload; #if CHECKSUM_CHECK_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP6) - { - if (ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->tot_len, ip6_current_src_addr(), ip6_current_dest_addr()) != 0) { + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP6) { + if (ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->tot_len, ip6_current_src_addr(), + ip6_current_dest_addr()) != 0) { /* Checksum failed */ pbuf_free(p); ICMP6_STATS_INC(icmp6.chkerr); @@ -124,97 +113,99 @@ icmp6_input(struct pbuf *p, struct netif *inp) #endif /* CHECKSUM_CHECK_ICMP6 */ switch (icmp6hdr->type) { - case ICMP6_TYPE_NA: /* Neighbor advertisement */ - case ICMP6_TYPE_NS: /* Neighbor solicitation */ - case ICMP6_TYPE_RA: /* Router advertisement */ - case ICMP6_TYPE_RD: /* Redirect */ - case ICMP6_TYPE_PTB: /* Packet too big */ - nd6_input(p, inp); - return; - case ICMP6_TYPE_RS: + case ICMP6_TYPE_NA: /* Neighbor advertisement */ + case ICMP6_TYPE_NS: /* Neighbor solicitation */ + case ICMP6_TYPE_RA: /* Router advertisement */ + case ICMP6_TYPE_RD: /* Redirect */ + case ICMP6_TYPE_PTB: /* Packet too big */ + nd6_input(p, inp); + return; + case ICMP6_TYPE_RS: #if LWIP_IPV6_FORWARD - /* @todo implement router functionality */ + /* @todo implement router functionality */ #endif - break; + break; #if LWIP_IPV6_MLD - case ICMP6_TYPE_MLQ: - case ICMP6_TYPE_MLR: - case ICMP6_TYPE_MLD: - mld6_input(p, inp); - return; + case ICMP6_TYPE_MLQ: + case ICMP6_TYPE_MLR: + case ICMP6_TYPE_MLD: + mld6_input(p, inp); + return; #endif - case ICMP6_TYPE_EREQ: + case ICMP6_TYPE_EREQ: #if !LWIP_MULTICAST_PING - /* multicast destination address? */ - if (ip6_addr_ismulticast(ip6_current_dest_addr())) { - /* drop */ - pbuf_free(p); - ICMP6_STATS_INC(icmp6.drop); - return; - } + /* multicast destination address? */ + if (ip6_addr_ismulticast(ip6_current_dest_addr())) { + /* drop */ + pbuf_free(p); + ICMP6_STATS_INC(icmp6.drop); + return; + } #endif /* LWIP_MULTICAST_PING */ - /* Allocate reply. */ - r = pbuf_alloc(PBUF_IP, p->tot_len, PBUF_RAM); - if (r == NULL) { - /* drop */ - pbuf_free(p); - ICMP6_STATS_INC(icmp6.memerr); - return; - } + /* Allocate reply. */ + r = pbuf_alloc(PBUF_IP, p->tot_len, PBUF_RAM); + if (r == NULL) { + /* drop */ + pbuf_free(p); + ICMP6_STATS_INC(icmp6.memerr); + return; + } - /* Copy echo request. */ - if (pbuf_copy(r, p) != ERR_OK) { + /* Copy echo request. */ + if (pbuf_copy(r, p) != ERR_OK) { + /* drop */ + pbuf_free(p); + pbuf_free(r); + ICMP6_STATS_INC(icmp6.err); + return; + } + + /* Determine reply source IPv6 address. */ +#if LWIP_MULTICAST_PING + if (ip6_addr_ismulticast(ip6_current_dest_addr())) { + reply_src = ip_2_ip6(ip6_select_source_address(inp, ip6_current_src_addr())); + if (reply_src == NULL) { /* drop */ pbuf_free(p); pbuf_free(r); - ICMP6_STATS_INC(icmp6.err); + ICMP6_STATS_INC(icmp6.rterr); return; } - - /* Determine reply source IPv6 address. */ -#if LWIP_MULTICAST_PING - if (ip6_addr_ismulticast(ip6_current_dest_addr())) { - reply_src = ip_2_ip6(ip6_select_source_address(inp, ip6_current_src_addr())); - if (reply_src == NULL) { - /* drop */ - pbuf_free(p); - pbuf_free(r); - ICMP6_STATS_INC(icmp6.rterr); - return; - } - } else + } + else #endif /* LWIP_MULTICAST_PING */ - { - reply_src = ip6_current_dest_addr(); - } + { + reply_src = ip6_current_dest_addr(); + } - /* Set fields in reply. */ - ((struct icmp6_echo_hdr *)(r->payload))->type = ICMP6_TYPE_EREP; - ((struct icmp6_echo_hdr *)(r->payload))->chksum = 0; + /* Set fields in reply. */ + ((struct icmp6_echo_hdr *)(r->payload))->type = ICMP6_TYPE_EREP; + ((struct icmp6_echo_hdr *)(r->payload))->chksum = 0; #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP6) - { - ((struct icmp6_echo_hdr *)(r->payload))->chksum = - ip6_chksum_pseudo(r, IP6_NEXTH_ICMP6, r->tot_len, reply_src, ip6_current_src_addr()); - } + IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP6) { + ((struct icmp6_echo_hdr *)(r->payload))->chksum = ip6_chksum_pseudo(r, + IP6_NEXTH_ICMP6, r->tot_len, reply_src, ip6_current_src_addr()); + } #endif /* CHECKSUM_GEN_ICMP6 */ - /* Send reply. */ - ICMP6_STATS_INC(icmp6.xmit); - ip6_output_if(r, reply_src, ip6_current_src_addr(), LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, inp); - pbuf_free(r); + /* Send reply. */ + ICMP6_STATS_INC(icmp6.xmit); + ip6_output_if(r, reply_src, ip6_current_src_addr(), + LWIP_ICMP6_HL, 0, IP6_NEXTH_ICMP6, inp); + pbuf_free(r); - break; - default: - ICMP6_STATS_INC(icmp6.proterr); - ICMP6_STATS_INC(icmp6.drop); - break; + break; + default: + ICMP6_STATS_INC(icmp6.proterr); + ICMP6_STATS_INC(icmp6.drop); + break; } pbuf_free(p); } + /** * Send an icmpv6 'destination unreachable' packet. * @@ -279,10 +270,8 @@ icmp6_time_exceeded(struct pbuf *p, enum icmp6_te_code c) * information */ void -icmp6_time_exceeded_with_addrs(struct pbuf *p, - enum icmp6_te_code c, - const ip6_addr_t *src_addr, - const ip6_addr_t *dest_addr) +icmp6_time_exceeded_with_addrs(struct pbuf *p, enum icmp6_te_code c, + const ip6_addr_t *src_addr, const ip6_addr_t *dest_addr) { icmp6_send_response_with_addrs(p, c, 0, ICMP6_TYPE_TE, src_addr, dest_addr); } @@ -354,12 +343,8 @@ icmp6_send_response(struct pbuf *p, u8_t code, u32_t data, u8_t type) * @param dest_addr original destination address */ static void -icmp6_send_response_with_addrs(struct pbuf *p, - u8_t code, - u32_t data, - u8_t type, - const ip6_addr_t *src_addr, - const ip6_addr_t *dest_addr) +icmp6_send_response_with_addrs(struct pbuf *p, u8_t code, u32_t data, u8_t type, + const ip6_addr_t *src_addr, const ip6_addr_t *dest_addr) { const struct ip6_addr *reply_src, *reply_dest; struct netif *netif; @@ -380,7 +365,8 @@ icmp6_send_response_with_addrs(struct pbuf *p, ICMP6_STATS_INC(icmp6.rterr); return; } - icmp6_send_response_with_addrs_and_netif(p, code, data, type, reply_src, reply_dest, netif); + icmp6_send_response_with_addrs_and_netif(p, code, data, type, reply_src, + reply_dest, netif); } /** @@ -396,26 +382,22 @@ icmp6_send_response_with_addrs(struct pbuf *p, * @param netif netif to send the packet */ static void -icmp6_send_response_with_addrs_and_netif(struct pbuf *p, - u8_t code, - u32_t data, - u8_t type, - const ip6_addr_t *reply_src, - const ip6_addr_t *reply_dest, - struct netif *netif) +icmp6_send_response_with_addrs_and_netif(struct pbuf *p, u8_t code, u32_t data, u8_t type, + const ip6_addr_t *reply_src, const ip6_addr_t *reply_dest, struct netif *netif) { struct pbuf *q; struct icmp6_hdr *icmp6hdr; /* ICMPv6 header + IPv6 header + data */ - q = pbuf_alloc(PBUF_IP, sizeof(struct icmp6_hdr) + IP6_HLEN + LWIP_ICMP6_DATASIZE, PBUF_RAM); + q = pbuf_alloc(PBUF_IP, sizeof(struct icmp6_hdr) + IP6_HLEN + LWIP_ICMP6_DATASIZE, + PBUF_RAM); if (q == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMPv6 packet.\n")); ICMP6_STATS_INC(icmp6.memerr); return; } LWIP_ASSERT("check that first pbuf can hold icmp 6message", - (q->len >= (sizeof(struct icmp6_hdr) + IP6_HLEN + LWIP_ICMP6_DATASIZE))); + (q->len >= (sizeof(struct icmp6_hdr) + IP6_HLEN + LWIP_ICMP6_DATASIZE))); icmp6hdr = (struct icmp6_hdr *)q->payload; icmp6hdr->type = type; @@ -423,14 +405,15 @@ icmp6_send_response_with_addrs_and_netif(struct pbuf *p, icmp6hdr->data = lwip_htonl(data); /* copy fields from original packet */ - SMEMCPY((u8_t *)q->payload + sizeof(struct icmp6_hdr), (u8_t *)p->payload, IP6_HLEN + LWIP_ICMP6_DATASIZE); + SMEMCPY((u8_t *)q->payload + sizeof(struct icmp6_hdr), (u8_t *)p->payload, + IP6_HLEN + LWIP_ICMP6_DATASIZE); /* calculate checksum */ icmp6hdr->chksum = 0; #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) - { - icmp6hdr->chksum = ip6_chksum_pseudo(q, IP6_NEXTH_ICMP6, q->tot_len, reply_src, reply_dest); + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { + icmp6hdr->chksum = ip6_chksum_pseudo(q, IP6_NEXTH_ICMP6, q->tot_len, + reply_src, reply_dest); } #endif /* CHECKSUM_GEN_ICMP6 */ diff --git a/src/core/ipv6/ip6.c b/src/core/ipv6/ip6.c index 0ad60c28..eda11dc8 100644 --- a/src/core/ipv6/ip6.c +++ b/src/core/ipv6/ip6.c @@ -41,24 +41,24 @@ #include "lwip/opt.h" -#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ +#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ -#include "lwip/debug.h" #include "lwip/def.h" -#include "lwip/dhcp6.h" -#include "lwip/icmp6.h" +#include "lwip/mem.h" +#include "lwip/netif.h" #include "lwip/ip.h" #include "lwip/ip6.h" #include "lwip/ip6_addr.h" #include "lwip/ip6_frag.h" -#include "lwip/mem.h" -#include "lwip/mld6.h" -#include "lwip/nd6.h" -#include "lwip/netif.h" +#include "lwip/icmp6.h" #include "lwip/priv/raw_priv.h" -#include "lwip/priv/tcp_priv.h" -#include "lwip/stats.h" #include "lwip/udp.h" +#include "lwip/priv/tcp_priv.h" +#include "lwip/dhcp6.h" +#include "lwip/nd6.h" +#include "lwip/mld6.h" +#include "lwip/debug.h" +#include "lwip/stats.h" #ifdef LWIP_HOOK_FILENAME #include LWIP_HOOK_FILENAME @@ -113,9 +113,9 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) IP6_ADDR_ZONECHECK(dest); /* Find a netif based on the zone. For custom mappings, one zone may map * to multiple netifs, so find one that can actually send a packet. */ - NETIF_FOREACH(netif) - { - if (ip6_addr_test_zone(dest, netif) && netif_is_up(netif) && netif_is_link_up(netif)) { + NETIF_FOREACH(netif) { + if (ip6_addr_test_zone(dest, netif) && + netif_is_up(netif) && netif_is_link_up(netif)) { return netif; } } @@ -140,18 +140,19 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) * wants, regardless of whether the source address is scoped. Finally, some * of this story also applies if scoping is disabled altogether. */ #if LWIP_IPV6_SCOPES - if (ip6_addr_has_scope(dest, IP6_UNKNOWN) || ip6_addr_has_scope(src, IP6_UNICAST) || -#else /* LWIP_IPV6_SCOPES */ - if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_iflocal(dest) || ip6_addr_ismulticast_linklocal(dest) || - ip6_addr_islinklocal(src) || + if (ip6_addr_has_scope(dest, IP6_UNKNOWN) || + ip6_addr_has_scope(src, IP6_UNICAST) || +#else /* LWIP_IPV6_SCOPES */ + if (ip6_addr_islinklocal(dest) || ip6_addr_ismulticast_iflocal(dest) || + ip6_addr_ismulticast_linklocal(dest) || ip6_addr_islinklocal(src) || #endif /* LWIP_IPV6_SCOPES */ ip6_addr_isloopback(src)) { #if LWIP_IPV6_SCOPES if (ip6_addr_has_zone(src)) { /* Find a netif matching the source zone (relatively cheap). */ - NETIF_FOREACH(netif) - { - if (netif_is_up(netif) && netif_is_link_up(netif) && ip6_addr_test_zone(src, netif)) { + NETIF_FOREACH(netif) { + if (netif_is_up(netif) && netif_is_link_up(netif) && + ip6_addr_test_zone(src, netif)) { return netif; } } @@ -159,8 +160,7 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) #endif /* LWIP_IPV6_SCOPES */ { /* Find a netif matching the source address (relatively expensive). */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (!netif_is_up(netif) || !netif_is_link_up(netif)) { continue; } @@ -193,14 +193,15 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) * such, the destination address may still match a local address, and so we * still need to check for exact matches here. By (lwIP) policy, statically * configured addresses do always have an implied local /64 subnet. */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (!netif_is_up(netif) || !netif_is_link_up(netif)) { continue; } for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_netcmp(dest, netif_ip6_addr(netif, i)) && - (netif_ip6_addr_isstatic(netif, i) || ip6_addr_nethostcmp(dest, netif_ip6_addr(netif, i)))) { + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + ip6_addr_netcmp(dest, netif_ip6_addr(netif, i)) && + (netif_ip6_addr_isstatic(netif, i) || + ip6_addr_nethostcmp(dest, netif_ip6_addr(netif, i)))) { return netif; } } @@ -215,13 +216,13 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) /* Try with the netif that matches the source address. Given the earlier rule * for scoped source addresses, this applies to unscoped addresses only. */ if (!ip6_addr_isany(src)) { - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (!netif_is_up(netif) || !netif_is_link_up(netif)) { continue; } for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_cmp(src, netif_ip6_addr(netif, i))) { + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + ip6_addr_cmp(src, netif_ip6_addr(netif, i))) { return netif; } } @@ -236,8 +237,7 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) return netif_default; } /* default netif is not up, just use any netif for loopback traffic */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (netif_is_up(netif)) { return netif; } @@ -338,9 +338,9 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest) } if ((best_addr == NULL) || /* no alternative yet */ ((cand_scope < best_scope) && (cand_scope >= dest_scope)) || - ((cand_scope > best_scope) && (best_scope < dest_scope)) || /* Rule 2 */ - ((cand_scope == best_scope) && ((cand_pref > best_pref) || /* Rule 3 */ - ((cand_pref == best_pref) && (cand_bits > best_bits))))) { /* Rule 8 */ + ((cand_scope > best_scope) && (best_scope < dest_scope)) || /* Rule 2 */ + ((cand_scope == best_scope) && ((cand_pref > best_pref) || /* Rule 3 */ + ((cand_pref == best_pref) && (cand_bits > best_bits))))) { /* Rule 8 */ /* We found a new "winning" candidate. */ best_addr = netif_ip_addr6(netif, i); best_scope = cand_scope; @@ -368,7 +368,8 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp) struct netif *netif; /* do not forward link-local or loopback addresses */ - if (ip6_addr_islinklocal(ip6_current_dest_addr()) || ip6_addr_isloopback(ip6_current_dest_addr())) { + if (ip6_addr_islinklocal(ip6_current_dest_addr()) || + ip6_addr_isloopback(ip6_current_dest_addr())) { LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding link-local address.\n")); IP6_STATS_INC(ip6.rterr); IP6_STATS_INC(ip6.drop); @@ -378,17 +379,15 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp) /* Find network interface where to forward this IP packet to. */ netif = ip6_route(IP6_ADDR_ANY6, ip6_current_dest_addr()); if (netif == NULL) { - LWIP_DEBUGF(IP6_DEBUG, - ("ip6_forward: no route for %" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F - ":%" X16_F "\n", - IP6_ADDR_BLOCK1(ip6_current_dest_addr()), - IP6_ADDR_BLOCK2(ip6_current_dest_addr()), - IP6_ADDR_BLOCK3(ip6_current_dest_addr()), - IP6_ADDR_BLOCK4(ip6_current_dest_addr()), - IP6_ADDR_BLOCK5(ip6_current_dest_addr()), - IP6_ADDR_BLOCK6(ip6_current_dest_addr()), - IP6_ADDR_BLOCK7(ip6_current_dest_addr()), - IP6_ADDR_BLOCK8(ip6_current_dest_addr()))); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n", + IP6_ADDR_BLOCK1(ip6_current_dest_addr()), + IP6_ADDR_BLOCK2(ip6_current_dest_addr()), + IP6_ADDR_BLOCK3(ip6_current_dest_addr()), + IP6_ADDR_BLOCK4(ip6_current_dest_addr()), + IP6_ADDR_BLOCK5(ip6_current_dest_addr()), + IP6_ADDR_BLOCK6(ip6_current_dest_addr()), + IP6_ADDR_BLOCK7(ip6_current_dest_addr()), + IP6_ADDR_BLOCK8(ip6_current_dest_addr()))); #if LWIP_ICMP6 /* Don't send ICMP messages in response to ICMP messages */ if (IP6H_NEXTH(iphdr) != IP6_NEXTH_ICMP6) { @@ -404,7 +403,8 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp) * outside of their zone. We determined the zone a bit earlier, so we know * that the address is properly zoned here, so we can safely use has_zone. * Also skip packets with a loopback source address (link-local implied). */ - if ((ip6_addr_has_zone(ip6_current_src_addr()) && !ip6_addr_test_zone(ip6_current_src_addr(), netif)) || + if ((ip6_addr_has_zone(ip6_current_src_addr()) && + !ip6_addr_test_zone(ip6_current_src_addr(), netif)) || ip6_addr_isloopback(ip6_current_src_addr())) { LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: not forwarding packet beyond its source address zone.\n")); IP6_STATS_INC(ip6.rterr); @@ -446,17 +446,15 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp) return; } - LWIP_DEBUGF(IP6_DEBUG, - ("ip6_forward: forwarding packet to %" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F - ":%" X16_F ":%" X16_F "\n", - IP6_ADDR_BLOCK1(ip6_current_dest_addr()), - IP6_ADDR_BLOCK2(ip6_current_dest_addr()), - IP6_ADDR_BLOCK3(ip6_current_dest_addr()), - IP6_ADDR_BLOCK4(ip6_current_dest_addr()), - IP6_ADDR_BLOCK5(ip6_current_dest_addr()), - IP6_ADDR_BLOCK6(ip6_current_dest_addr()), - IP6_ADDR_BLOCK7(ip6_current_dest_addr()), - IP6_ADDR_BLOCK8(ip6_current_dest_addr()))); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_forward: forwarding packet to %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n", + IP6_ADDR_BLOCK1(ip6_current_dest_addr()), + IP6_ADDR_BLOCK2(ip6_current_dest_addr()), + IP6_ADDR_BLOCK3(ip6_current_dest_addr()), + IP6_ADDR_BLOCK4(ip6_current_dest_addr()), + IP6_ADDR_BLOCK5(ip6_current_dest_addr()), + IP6_ADDR_BLOCK6(ip6_current_dest_addr()), + IP6_ADDR_BLOCK7(ip6_current_dest_addr()), + IP6_ADDR_BLOCK8(ip6_current_dest_addr()))); /* transmit pbuf on chosen interface */ netif->output_ip6(netif, p, ip6_current_dest_addr()); @@ -475,13 +473,14 @@ ip6_input_accept(struct netif *netif) u8_t i; /* unicast to this interface address? address configured? */ /* If custom scopes are used, the destination zone will be tested as - * part of the local-address comparison, but we need to test the source - * scope as well (e.g., is this interface on the same link?). */ + * part of the local-address comparison, but we need to test the source + * scope as well (e.g., is this interface on the same link?). */ for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_cmp(ip6_current_dest_addr(), netif_ip6_addr(netif, i)) #if IPV6_CUSTOM_SCOPES - && (!ip6_addr_has_zone(ip6_current_src_addr()) || ip6_addr_test_zone(ip6_current_src_addr(), netif)) + && (!ip6_addr_has_zone(ip6_current_src_addr()) || + ip6_addr_test_zone(ip6_current_src_addr(), netif)) #endif /* IPV6_CUSTOM_SCOPES */ ) { /* accept on this netif */ @@ -513,10 +512,10 @@ ip6_input(struct pbuf *p, struct netif *inp) struct netif *netif; const u8_t *nexth; u16_t hlen, hlen_tot; /* the current header length */ -#if 0 /*IP_ACCEPT_LINK_LAYER_ADDRESSING*/ +#if 0 /*IP_ACCEPT_LINK_LAYER_ADDRESSING*/ @todo int check_ip_src=1; -#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ +#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ #if LWIP_RAW raw_input_state_t raw_status; #endif /* LWIP_RAW */ @@ -528,8 +527,8 @@ ip6_input(struct pbuf *p, struct netif *inp) /* identify the IP header */ ip6hdr = (struct ip6_hdr *)p->payload; if (IP6H_V(ip6hdr) != 6) { - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, - ("IPv6 packet dropped due to bad version number %" U32_F "\n", IP6H_V(ip6hdr))); + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IPv6 packet dropped due to bad version number %"U32_F"\n", + IP6H_V(ip6hdr))); pbuf_free(p); IP6_STATS_INC(ip6.err); IP6_STATS_INC(ip6.drop); @@ -547,15 +546,13 @@ ip6_input(struct pbuf *p, struct netif *inp) if ((IP6_HLEN > p->len) || (IP6H_PLEN(ip6hdr) > (p->tot_len - IP6_HLEN))) { if (IP6_HLEN > p->len) { LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 header (len %" U16_F ") does not fit in first pbuf (len %" U16_F "), IP packet dropped.\n", - (u16_t)IP6_HLEN, - p->len)); + ("IPv6 header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n", + (u16_t)IP6_HLEN, p->len)); } if ((IP6H_PLEN(ip6hdr) + IP6_HLEN) > p->tot_len) { LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 (plen %" U16_F ") is longer than pbuf (len %" U16_F "), IP packet dropped.\n", - (u16_t)(IP6H_PLEN(ip6hdr) + IP6_HLEN), - p->tot_len)); + ("IPv6 (plen %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n", + (u16_t)(IP6H_PLEN(ip6hdr) + IP6_HLEN), p->tot_len)); } /* free (drop) packet pbufs */ pbuf_free(p); @@ -575,8 +572,8 @@ ip6_input(struct pbuf *p, struct netif *inp) /* Don't accept virtual IPv4 mapped IPv6 addresses. * Don't accept multicast source addresses. */ if (ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_dest)) || - ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_src)) || - ip6_addr_ismulticast(ip_2_ip6(&ip_data.current_iphdr_src))) { + ip6_addr_isipv4mappedipv6(ip_2_ip6(&ip_data.current_iphdr_src)) || + ip6_addr_ismulticast(ip_2_ip6(&ip_data.current_iphdr_src))) { /* free (drop) packet pbufs */ pbuf_free(p); IP6_STATS_INC(ip6.err); @@ -606,7 +603,7 @@ ip6_input(struct pbuf *p, struct netif *inp) else if (mld6_lookfor_group(inp, ip6_current_dest_addr())) { netif = inp; } -#else /* LWIP_IPV6_MLD */ +#else /* LWIP_IPV6_MLD */ else if (ip6_addr_issolicitednode(ip6_current_dest_addr())) { u8_t i; /* Filter solicited node packets when MLD is not enabled @@ -616,9 +613,8 @@ ip6_input(struct pbuf *p, struct netif *inp) if (ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) && ip6_addr_cmp_solicitednode(ip6_current_dest_addr(), netif_ip6_addr(inp, i))) { netif = inp; - LWIP_DEBUGF( - IP6_DEBUG, - ("ip6_input: solicited node packet accepted on interface %c%c\n", netif->name[0], netif->name[1])); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: solicited node packet accepted on interface %c%c\n", + netif->name[0], netif->name[1])); break; } } @@ -636,26 +632,27 @@ ip6_input(struct pbuf *p, struct netif *inp) netif = NULL; #if !IPV6_CUSTOM_SCOPES /* Shortcut: stop looking for other interfaces if either the source or - * the destination has a scope constrained to this interface. Custom - * scopes may break the 1:1 link/interface mapping, however. */ - if (ip6_addr_islinklocal(ip6_current_dest_addr()) || ip6_addr_islinklocal(ip6_current_src_addr())) { + * the destination has a scope constrained to this interface. Custom + * scopes may break the 1:1 link/interface mapping, however. */ + if (ip6_addr_islinklocal(ip6_current_dest_addr()) || + ip6_addr_islinklocal(ip6_current_src_addr())) { goto netif_found; } #endif /* !IPV6_CUSTOM_SCOPES */ #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF /* The loopback address is to be considered link-local. Packets to it - * should be dropped on other interfaces, as per RFC 4291 Sec. 2.5.3. - * Its implied scope means packets *from* the loopback address should - * not be accepted on other interfaces, either. These requirements - * cannot be implemented in the case that loopback traffic is sent - * across a non-loopback interface, however. */ - if (ip6_addr_isloopback(ip6_current_dest_addr()) || ip6_addr_isloopback(ip6_current_src_addr())) { + * should be dropped on other interfaces, as per RFC 4291 Sec. 2.5.3. + * Its implied scope means packets *from* the loopback address should + * not be accepted on other interfaces, either. These requirements + * cannot be implemented in the case that loopback traffic is sent + * across a non-loopback interface, however. */ + if (ip6_addr_isloopback(ip6_current_dest_addr()) || + ip6_addr_isloopback(ip6_current_src_addr())) { goto netif_found; } #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */ #if !LWIP_SINGLE_NETIF - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { if (netif == inp) { /* we checked that before already */ continue; @@ -666,14 +663,14 @@ ip6_input(struct pbuf *p, struct netif *inp) } #endif /* !LWIP_SINGLE_NETIF */ } - netif_found: - LWIP_DEBUGF( - IP6_DEBUG, - ("ip6_input: packet accepted on interface %c%c\n", netif ? netif->name[0] : 'X', netif ? netif->name[1] : 'X')); +netif_found: + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet accepted on interface %c%c\n", + netif ? netif->name[0] : 'X', netif? netif->name[1] : 'X')); } /* "::" packet source address? (used in duplicate address detection) */ - if (ip6_addr_isany(ip6_current_src_addr()) && (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) { + if (ip6_addr_isany(ip6_current_src_addr()) && + (!ip6_addr_issolicitednode(ip6_current_dest_addr()))) { /* packet source is not valid */ /* free (drop) packet pbufs */ LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with src ANY_ADDRESS dropped\n")); @@ -710,330 +707,332 @@ ip6_input(struct pbuf *p, struct netif *inp) pbuf_remove_header(p, IP6_HLEN); /* Process known option extension headers, if present. */ - while (*nexth != IP6_NEXTH_NONE) { + while (*nexth != IP6_NEXTH_NONE) + { switch (*nexth) { - case IP6_NEXTH_HOPBYHOP: { - s32_t opt_offset; - struct ip6_hbh_hdr *hbh_hdr; - struct ip6_opt_hdr *opt_hdr; - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n")); + case IP6_NEXTH_HOPBYHOP: + { + s32_t opt_offset; + struct ip6_hbh_hdr *hbh_hdr; + struct ip6_opt_hdr *opt_hdr; + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header\n")); - /* Get and check the header length, while staying in packet bounds. */ - hbh_hdr = (struct ip6_hbh_hdr *)p->payload; + /* Get and check the header length, while staying in packet bounds. */ + hbh_hdr = (struct ip6_hbh_hdr *)p->payload; - /* Get next header type. */ - nexth = &IP6_HBH_NEXTH(hbh_hdr); + /* Get next header type. */ + nexth = &IP6_HBH_NEXTH(hbh_hdr); - /* Get the header length. */ - hlen = (u16_t)(8 * (1 + hbh_hdr->_hlen)); + /* Get the header length. */ + hlen = (u16_t)(8 * (1 + hbh_hdr->_hlen)); - if ((p->len < 8) || (hlen > p->len)) { - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 options header (hlen %" U16_F ") does not fit in first pbuf (len %" U16_F - "), IPv6 packet dropped.\n", - hlen, - p->len)); - /* free (drop) packet pbufs */ - pbuf_free(p); - IP6_STATS_INC(ip6.lenerr); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - } - - hlen_tot = (u16_t)(hlen_tot + hlen); - - /* The extended option header starts right after Hop-by-Hop header. */ - opt_offset = IP6_HBH_HLEN; - while (opt_offset < hlen) { - s32_t opt_dlen = 0; - - opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + opt_offset); - - switch (IP6_OPT_TYPE(opt_hdr)) { - /* @todo: process IPV6 Hop-by-Hop option data */ - case IP6_PAD1_OPTION: - /* PAD1 option doesn't have length and value field */ - opt_dlen = -1; - break; - case IP6_PADN_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - case IP6_ROUTER_ALERT_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - case IP6_JUMBO_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - default: - /* Check 2 MSB of Hop-by-Hop header type. */ - switch (IP6_OPT_TYPE_ACTION(opt_hdr)) { - case 1: - /* Discard the packet. */ - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - case 2: - /* Send ICMP Parameter Problem */ - icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - case 3: - /* Send ICMP Parameter Problem if destination address is not a multicast address */ - if (!ip6_addr_ismulticast(ip6_current_dest_addr())) { - icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); - } - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - default: - /* Skip over this option. */ - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - } - break; - } - - /* Adjust the offset to move to the next extended option header */ - opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen; - } - pbuf_remove_header(p, hlen); - break; + if ((p->len < 8) || (hlen > p->len)) { + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, + ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n", + hlen, p->len)); + /* free (drop) packet pbufs */ + pbuf_free(p); + IP6_STATS_INC(ip6.lenerr); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; } - case IP6_NEXTH_DESTOPTS: { - s32_t opt_offset; - struct ip6_dest_hdr *dest_hdr; - struct ip6_opt_hdr *opt_hdr; - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n")); - dest_hdr = (struct ip6_dest_hdr *)p->payload; + hlen_tot = (u16_t)(hlen_tot + hlen); - /* Get next header type. */ - nexth = &IP6_DEST_NEXTH(dest_hdr); + /* The extended option header starts right after Hop-by-Hop header. */ + opt_offset = IP6_HBH_HLEN; + while (opt_offset < hlen) + { + s32_t opt_dlen = 0; - /* Get the header length. */ - hlen = 8 * (1 + dest_hdr->_hlen); - if ((p->len < 8) || (hlen > p->len)) { - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 options header (hlen %" U16_F ") does not fit in first pbuf (len %" U16_F - "), IPv6 packet dropped.\n", - hlen, - p->len)); - /* free (drop) packet pbufs */ - pbuf_free(p); - IP6_STATS_INC(ip6.lenerr); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - } + opt_hdr = (struct ip6_opt_hdr *)((u8_t *)hbh_hdr + opt_offset); - hlen_tot = (u16_t)(hlen_tot + hlen); - - /* The extended option header starts right after Destination header. */ - opt_offset = IP6_DEST_HLEN; - while (opt_offset < hlen) { - s32_t opt_dlen = 0; - - opt_hdr = (struct ip6_opt_hdr *)((u8_t *)dest_hdr + opt_offset); - - switch (IP6_OPT_TYPE(opt_hdr)) { - /* @todo: process IPV6 Destination option data */ - case IP6_PAD1_OPTION: - /* PAD1 option deosn't have length and value field */ - opt_dlen = -1; - break; - case IP6_PADN_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - case IP6_ROUTER_ALERT_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - case IP6_JUMBO_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - case IP6_HOME_ADDRESS_OPTION: - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - default: - /* Check 2 MSB of Destination header type. */ - switch (IP6_OPT_TYPE_ACTION(opt_hdr)) { - case 1: - /* Discard the packet. */ - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - case 2: - /* Send ICMP Parameter Problem */ - icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - case 3: - /* Send ICMP Parameter Problem if destination address is not a multicast address */ - if (!ip6_addr_ismulticast(ip6_current_dest_addr())) { - icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); - } - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - default: - /* Skip over this option. */ - opt_dlen = IP6_OPT_DLEN(opt_hdr); - break; - } - break; - } - - /* Adjust the offset to move to the next extended option header */ - opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen; - } - - pbuf_remove_header(p, hlen); - break; - } - case IP6_NEXTH_ROUTING: { - struct ip6_rout_hdr *rout_hdr; - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n")); - - rout_hdr = (struct ip6_rout_hdr *)p->payload; - - /* Get next header type. */ - nexth = &IP6_ROUT_NEXTH(rout_hdr); - - /* Get the header length. */ - hlen = 8 * (1 + rout_hdr->_hlen); - - if ((p->len < 8) || (hlen > p->len)) { - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 options header (hlen %" U16_F ") does not fit in first pbuf (len %" U16_F - "), IPv6 packet dropped.\n", - hlen, - p->len)); - /* free (drop) packet pbufs */ - pbuf_free(p); - IP6_STATS_INC(ip6.lenerr); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - } - - /* Skip over this header. */ - hlen_tot = (u16_t)(hlen_tot + hlen); - - /* if segment left value is 0 in routing header, ignore the option */ - if (IP6_ROUT_SEG_LEFT(rout_hdr)) { - /* The length field of routing option header must be even */ - if (rout_hdr->_hlen & 0x1) { - /* Discard and send parameter field error */ - icmp6_param_problem(p, ICMP6_PP_FIELD, &rout_hdr->_hlen); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n")); + switch (IP6_OPT_TYPE(opt_hdr)) { + /* @todo: process IPV6 Hop-by-Hop option data */ + case IP6_PAD1_OPTION: + /* PAD1 option doesn't have length and value field */ + opt_dlen = -1; + break; + case IP6_PADN_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + case IP6_ROUTER_ALERT_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + case IP6_JUMBO_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + default: + /* Check 2 MSB of Hop-by-Hop header type. */ + switch (IP6_OPT_TYPE_ACTION(opt_hdr)) { + case 1: + /* Discard the packet. */ + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); pbuf_free(p); IP6_STATS_INC(ip6.drop); goto ip6_input_cleanup; - } - - switch (IP6_ROUT_TYPE(rout_hdr)) { - /* TODO: process routing by the type */ - case IP6_ROUT_TYPE2: - break; - case IP6_ROUT_RPL: - break; - default: - /* Discard unrecognized routing type and send parameter field error */ - icmp6_param_problem(p, ICMP6_PP_FIELD, &IP6_ROUT_TYPE(rout_hdr)); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - } - } - - pbuf_remove_header(p, hlen); - break; - } - case IP6_NEXTH_FRAGMENT: { - struct ip6_frag_hdr *frag_hdr; - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n")); - - frag_hdr = (struct ip6_frag_hdr *)p->payload; - - /* Get next header type. */ - nexth = &IP6_FRAG_NEXTH(frag_hdr); - - /* Fragment Header length. */ - hlen = 8; - - /* Make sure this header fits in current pbuf. */ - if (hlen > p->len) { - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("IPv6 options header (hlen %" U16_F ") does not fit in first pbuf (len %" U16_F - "), IPv6 packet dropped.\n", - hlen, - p->len)); - /* free (drop) packet pbufs */ - pbuf_free(p); - IP6_FRAG_STATS_INC(ip6_frag.lenerr); - IP6_FRAG_STATS_INC(ip6_frag.drop); - goto ip6_input_cleanup; - } - - hlen_tot = (u16_t)(hlen_tot + hlen); - - /* check payload length is multiple of 8 octets when mbit is set */ - if (IP6_FRAG_MBIT(frag_hdr) && (IP6H_PLEN(ip6hdr) & 0x7)) { - /* ipv6 payload length is not multiple of 8 octets */ - icmp6_param_problem(p, ICMP6_PP_FIELD, LWIP_PACKED_CAST(const void *, &ip6hdr->_plen)); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid payload length dropped\n")); - pbuf_free(p); - IP6_STATS_INC(ip6.drop); - goto ip6_input_cleanup; - } - - /* Offset == 0 and more_fragments == 0? */ - if ((frag_hdr->_fragment_offset & PP_HTONS(IP6_FRAG_OFFSET_MASK | IP6_FRAG_MORE_FLAG)) == 0) { - /* This is a 1-fragment packet. Skip this header and continue. */ - pbuf_remove_header(p, hlen); - } else { -#if LWIP_IPV6_REASS - /* reassemble the packet */ - ip_data.current_ip_header_tot_len = hlen_tot; - p = ip6_reass(p); - /* packet not fully reassembled yet? */ - if (p == NULL) { + case 2: + /* Send ICMP Parameter Problem */ + icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); goto ip6_input_cleanup; + case 3: + /* Send ICMP Parameter Problem if destination address is not a multicast address */ + if (!ip6_addr_ismulticast(ip6_current_dest_addr())) { + icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); + } + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid Hop-by-Hop option type dropped.\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + default: + /* Skip over this option. */ + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; } + break; + } - /* Returned p point to IPv6 header. - * Update all our variables and pointers and continue. */ - ip6hdr = (struct ip6_hdr *)p->payload; - nexth = &IP6H_NEXTH(ip6hdr); - hlen = hlen_tot = IP6_HLEN; - pbuf_remove_header(p, IP6_HLEN); + /* Adjust the offset to move to the next extended option header */ + opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen; + } + pbuf_remove_header(p, hlen); + break; + } + case IP6_NEXTH_DESTOPTS: + { + s32_t opt_offset; + struct ip6_dest_hdr *dest_hdr; + struct ip6_opt_hdr *opt_hdr; + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Destination options header\n")); -#else /* LWIP_IPV6_REASS */ - /* free (drop) packet pbufs */ - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n")); + dest_hdr = (struct ip6_dest_hdr *)p->payload; + + /* Get next header type. */ + nexth = &IP6_DEST_NEXTH(dest_hdr); + + /* Get the header length. */ + hlen = 8 * (1 + dest_hdr->_hlen); + if ((p->len < 8) || (hlen > p->len)) { + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, + ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n", + hlen, p->len)); + /* free (drop) packet pbufs */ + pbuf_free(p); + IP6_STATS_INC(ip6.lenerr); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + } + + hlen_tot = (u16_t)(hlen_tot + hlen); + + /* The extended option header starts right after Destination header. */ + opt_offset = IP6_DEST_HLEN; + while (opt_offset < hlen) + { + s32_t opt_dlen = 0; + + opt_hdr = (struct ip6_opt_hdr *)((u8_t *)dest_hdr + opt_offset); + + switch (IP6_OPT_TYPE(opt_hdr)) + { + /* @todo: process IPV6 Destination option data */ + case IP6_PAD1_OPTION: + /* PAD1 option deosn't have length and value field */ + opt_dlen = -1; + break; + case IP6_PADN_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + case IP6_ROUTER_ALERT_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + case IP6_JUMBO_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + case IP6_HOME_ADDRESS_OPTION: + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + default: + /* Check 2 MSB of Destination header type. */ + switch (IP6_OPT_TYPE_ACTION(opt_hdr)) + { + case 1: + /* Discard the packet. */ + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + case 2: + /* Send ICMP Parameter Problem */ + icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + case 3: + /* Send ICMP Parameter Problem if destination address is not a multicast address */ + if (!ip6_addr_ismulticast(ip6_current_dest_addr())) { + icmp6_param_problem(p, ICMP6_PP_OPTION, opt_hdr); + } + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid destination option type dropped.\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + default: + /* Skip over this option. */ + opt_dlen = IP6_OPT_DLEN(opt_hdr); + break; + } + break; + } + + /* Adjust the offset to move to the next extended option header */ + opt_offset = opt_offset + IP6_OPT_HLEN + opt_dlen; + } + + pbuf_remove_header(p, hlen); + break; + } + case IP6_NEXTH_ROUTING: + { + struct ip6_rout_hdr *rout_hdr; + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Routing header\n")); + + rout_hdr = (struct ip6_rout_hdr *)p->payload; + + /* Get next header type. */ + nexth = &IP6_ROUT_NEXTH(rout_hdr); + + /* Get the header length. */ + hlen = 8 * (1 + rout_hdr->_hlen); + + if ((p->len < 8) || (hlen > p->len)) { + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, + ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n", + hlen, p->len)); + /* free (drop) packet pbufs */ + pbuf_free(p); + IP6_STATS_INC(ip6.lenerr); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + } + + /* Skip over this header. */ + hlen_tot = (u16_t)(hlen_tot + hlen); + + /* if segment left value is 0 in routing header, ignore the option */ + if (IP6_ROUT_SEG_LEFT(rout_hdr)) { + /* The length field of routing option header must be even */ + if (rout_hdr->_hlen & 0x1) { + /* Discard and send parameter field error */ + icmp6_param_problem(p, ICMP6_PP_FIELD, &rout_hdr->_hlen); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + } + + switch (IP6_ROUT_TYPE(rout_hdr)) + { + /* TODO: process routing by the type */ + case IP6_ROUT_TYPE2: + break; + case IP6_ROUT_RPL: + break; + default: + /* Discard unrecognized routing type and send parameter field error */ + icmp6_param_problem(p, ICMP6_PP_FIELD, &IP6_ROUT_TYPE(rout_hdr)); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid routing type dropped\n")); pbuf_free(p); - IP6_STATS_INC(ip6.opterr); IP6_STATS_INC(ip6.drop); goto ip6_input_cleanup; -#endif /* LWIP_IPV6_REASS */ } - break; } - default: - goto options_done; + + pbuf_remove_header(p, hlen); + break; + } + case IP6_NEXTH_FRAGMENT: + { + struct ip6_frag_hdr *frag_hdr; + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header\n")); + + frag_hdr = (struct ip6_frag_hdr *)p->payload; + + /* Get next header type. */ + nexth = &IP6_FRAG_NEXTH(frag_hdr); + + /* Fragment Header length. */ + hlen = 8; + + /* Make sure this header fits in current pbuf. */ + if (hlen > p->len) { + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, + ("IPv6 options header (hlen %"U16_F") does not fit in first pbuf (len %"U16_F"), IPv6 packet dropped.\n", + hlen, p->len)); + /* free (drop) packet pbufs */ + pbuf_free(p); + IP6_FRAG_STATS_INC(ip6_frag.lenerr); + IP6_FRAG_STATS_INC(ip6_frag.drop); + goto ip6_input_cleanup; + } + + hlen_tot = (u16_t)(hlen_tot + hlen); + + /* check payload length is multiple of 8 octets when mbit is set */ + if (IP6_FRAG_MBIT(frag_hdr) && (IP6H_PLEN(ip6hdr) & 0x7)) { + /* ipv6 payload length is not multiple of 8 octets */ + icmp6_param_problem(p, ICMP6_PP_FIELD, LWIP_PACKED_CAST(const void *, &ip6hdr->_plen)); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with invalid payload length dropped\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; + } + + /* Offset == 0 and more_fragments == 0? */ + if ((frag_hdr->_fragment_offset & + PP_HTONS(IP6_FRAG_OFFSET_MASK | IP6_FRAG_MORE_FLAG)) == 0) { + /* This is a 1-fragment packet. Skip this header and continue. */ + pbuf_remove_header(p, hlen); + } else { +#if LWIP_IPV6_REASS + /* reassemble the packet */ + ip_data.current_ip_header_tot_len = hlen_tot; + p = ip6_reass(p); + /* packet not fully reassembled yet? */ + if (p == NULL) { + goto ip6_input_cleanup; + } + + /* Returned p point to IPv6 header. + * Update all our variables and pointers and continue. */ + ip6hdr = (struct ip6_hdr *)p->payload; + nexth = &IP6H_NEXTH(ip6hdr); + hlen = hlen_tot = IP6_HLEN; + pbuf_remove_header(p, IP6_HLEN); + +#else /* LWIP_IPV6_REASS */ + /* free (drop) packet pbufs */ + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Fragment header dropped (with LWIP_IPV6_REASS==0)\n")); + pbuf_free(p); + IP6_STATS_INC(ip6.opterr); + IP6_STATS_INC(ip6.drop); + goto ip6_input_cleanup; +#endif /* LWIP_IPV6_REASS */ + } + break; + } + default: + goto options_done; } if (*nexth == IP6_NEXTH_HOPBYHOP) { /* Hop-by-Hop header comes only as a first option */ icmp6_param_problem(p, ICMP6_PP_HEADER, nexth); - LWIP_DEBUGF(IP6_DEBUG, - ("ip6_input: packet with Hop-by-Hop options header dropped (only valid as a first option)\n")); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: packet with Hop-by-Hop options header dropped (only valid as a first option)\n")); pbuf_free(p); IP6_STATS_INC(ip6.drop); goto ip6_input_cleanup; @@ -1045,44 +1044,45 @@ options_done: /* send to upper layers */ LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: \n")); ip6_debug_print(p); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %" U16_F " p->tot_len %" U16_F "\n", p->len, p->tot_len)); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len)); ip_data.current_ip_header_tot_len = hlen_tot; - + #if LWIP_RAW /* p points to IPv6 header again for raw_input. */ pbuf_add_header_force(p, hlen_tot); /* raw input did not eat the packet? */ raw_status = raw_input(p, inp); - if (raw_status != RAW_INPUT_EATEN) { + if (raw_status != RAW_INPUT_EATEN) + { /* Point to payload. */ pbuf_remove_header(p, hlen_tot); -#else /* LWIP_RAW */ +#else /* LWIP_RAW */ { #endif /* LWIP_RAW */ switch (*nexth) { - case IP6_NEXTH_NONE: - pbuf_free(p); - break; + case IP6_NEXTH_NONE: + pbuf_free(p); + break; #if LWIP_UDP - case IP6_NEXTH_UDP: + case IP6_NEXTH_UDP: #if LWIP_UDPLITE - case IP6_NEXTH_UDPLITE: + case IP6_NEXTH_UDPLITE: #endif /* LWIP_UDPLITE */ - udp_input(p, inp); - break; + udp_input(p, inp); + break; #endif /* LWIP_UDP */ #if LWIP_TCP - case IP6_NEXTH_TCP: - tcp_input(p, inp); - break; + case IP6_NEXTH_TCP: + tcp_input(p, inp); + break; #endif /* LWIP_TCP */ #if LWIP_ICMP6 - case IP6_NEXTH_ICMP6: - icmp6_input(p, inp); - break; + case IP6_NEXTH_ICMP6: + icmp6_input(p, inp); + break; #endif /* LWIP_ICMP */ - default: + default: #if LWIP_RAW if (raw_status == RAW_INPUT_DELIVERED) { /* @todo: ipv6 mib in-delivers? */ @@ -1090,20 +1090,20 @@ options_done: #endif /* LWIP_RAW */ { #if LWIP_ICMP6 - /* p points to IPv6 header again for raw_input. */ - pbuf_add_header_force(p, hlen_tot); - /* send ICMP parameter problem unless it was a multicast or ICMPv6 */ - if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) && (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) { - icmp6_param_problem(p, ICMP6_PP_HEADER, nexth); - } -#endif /* LWIP_ICMP */ - LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, - ("ip6_input: Unsupported transport protocol %" U16_F "\n", (u16_t)IP6H_NEXTH(ip6hdr))); - IP6_STATS_INC(ip6.proterr); - IP6_STATS_INC(ip6.drop); + /* p points to IPv6 header again for raw_input. */ + pbuf_add_header_force(p, hlen_tot); + /* send ICMP parameter problem unless it was a multicast or ICMPv6 */ + if ((!ip6_addr_ismulticast(ip6_current_dest_addr())) && + (IP6H_NEXTH(ip6hdr) != IP6_NEXTH_ICMP6)) { + icmp6_param_problem(p, ICMP6_PP_HEADER, nexth); } - pbuf_free(p); - break; +#endif /* LWIP_ICMP */ + LWIP_DEBUGF(IP6_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip6_input: Unsupported transport protocol %"U16_F"\n", (u16_t)IP6H_NEXTH(ip6hdr))); + IP6_STATS_INC(ip6.proterr); + IP6_STATS_INC(ip6.drop); + } + pbuf_free(p); + break; } } @@ -1118,6 +1118,7 @@ ip6_input_cleanup: return ERR_OK; } + /** * Sends an IPv6 packet on a network interface. This function constructs * the IPv6 header. If the source IPv6 address is NULL, the IPv6 "ANY" address is @@ -1145,13 +1146,9 @@ ip6_input_cleanup: * returns errors returned by netif->output_ip6 */ err_t -ip6_output_if(struct pbuf *p, - const ip6_addr_t *src, - const ip6_addr_t *dest, - u8_t hl, - u8_t tc, - u8_t nexth, - struct netif *netif) +ip6_output_if(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, + u8_t hl, u8_t tc, + u8_t nexth, struct netif *netif) { const ip6_addr_t *src_used = src; if (dest != LWIP_IP_HDRINCL) { @@ -1173,13 +1170,9 @@ ip6_output_if(struct pbuf *p, * when it is 'any'. */ err_t -ip6_output_if_src(struct pbuf *p, - const ip6_addr_t *src, - const ip6_addr_t *dest, - u8_t hl, - u8_t tc, - u8_t nexth, - struct netif *netif) +ip6_output_if_src(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, + u8_t hl, u8_t tc, + u8_t nexth, struct netif *netif) { struct ip6_hdr *ip6hdr; ip6_addr_t dest_addr; @@ -1211,7 +1204,8 @@ ip6_output_if_src(struct pbuf *p, } ip6hdr = (struct ip6_hdr *)p->payload; - LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr", (p->len >= sizeof(struct ip6_hdr))); + LWIP_ASSERT("check that first pbuf can hold struct ip6_hdr", + (p->len >= sizeof(struct ip6_hdr))); IP6H_HOPLIM_SET(ip6hdr, hl); IP6H_NEXTH_SET(ip6hdr, nexth); @@ -1238,7 +1232,7 @@ ip6_output_if_src(struct pbuf *p, IP6_STATS_INC(ip6.xmit); - LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%" U16_F "\n", netif->name[0], netif->name[1], (u16_t)netif->num)); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num)); ip6_debug_print(p); #if ENABLE_LOOPBACK @@ -1250,7 +1244,8 @@ ip6_output_if_src(struct pbuf *p, } #endif /* !LWIP_HAVE_LOOPIF */ for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && ip6_addr_cmp(dest, netif_ip6_addr(netif, i))) { + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + ip6_addr_cmp(dest, netif_ip6_addr(netif, i))) { /* Packet to self, enqueue it for loopback */ LWIP_DEBUGF(IP6_DEBUG, ("netif_loop_output()\n")); return netif_loop_output(netif, p); @@ -1293,7 +1288,8 @@ ip6_output_if_src(struct pbuf *p, * see ip_output_if() for more return values */ err_t -ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, u8_t hl, u8_t tc, u8_t nexth) +ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, + u8_t hl, u8_t tc, u8_t nexth) { struct netif *netif; struct ip6_hdr *ip6hdr; @@ -1312,17 +1308,15 @@ ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, u8_t h } if (netif == NULL) { - LWIP_DEBUGF(IP6_DEBUG, - ("ip6_output: no route for %" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F - ":%" X16_F "\n", - IP6_ADDR_BLOCK1(dest), - IP6_ADDR_BLOCK2(dest), - IP6_ADDR_BLOCK3(dest), - IP6_ADDR_BLOCK4(dest), - IP6_ADDR_BLOCK5(dest), - IP6_ADDR_BLOCK6(dest), - IP6_ADDR_BLOCK7(dest), - IP6_ADDR_BLOCK8(dest))); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n", + IP6_ADDR_BLOCK1(dest), + IP6_ADDR_BLOCK2(dest), + IP6_ADDR_BLOCK3(dest), + IP6_ADDR_BLOCK4(dest), + IP6_ADDR_BLOCK5(dest), + IP6_ADDR_BLOCK6(dest), + IP6_ADDR_BLOCK7(dest), + IP6_ADDR_BLOCK8(dest))); IP6_STATS_INC(ip6.rterr); return ERR_RTE; } @@ -1330,6 +1324,7 @@ ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, u8_t h return ip6_output_if(p, src, dest, hl, tc, nexth, netif); } + #if LWIP_NETIF_USE_HINTS /** Like ip6_output, but takes and addr_hint pointer that is passed on to netif->addr_hint * before calling ip6_output_if. @@ -1351,13 +1346,8 @@ ip6_output(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, u8_t h * see ip_output_if() for more return values */ err_t -ip6_output_hinted(struct pbuf *p, - const ip6_addr_t *src, - const ip6_addr_t *dest, - u8_t hl, - u8_t tc, - u8_t nexth, - struct netif_hint *netif_hint) +ip6_output_hinted(struct pbuf *p, const ip6_addr_t *src, const ip6_addr_t *dest, + u8_t hl, u8_t tc, u8_t nexth, struct netif_hint *netif_hint) { struct netif *netif; struct ip6_hdr *ip6hdr; @@ -1377,17 +1367,15 @@ ip6_output_hinted(struct pbuf *p, } if (netif == NULL) { - LWIP_DEBUGF(IP6_DEBUG, - ("ip6_output: no route for %" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F - ":%" X16_F "\n", - IP6_ADDR_BLOCK1(dest), - IP6_ADDR_BLOCK2(dest), - IP6_ADDR_BLOCK3(dest), - IP6_ADDR_BLOCK4(dest), - IP6_ADDR_BLOCK5(dest), - IP6_ADDR_BLOCK6(dest), - IP6_ADDR_BLOCK7(dest), - IP6_ADDR_BLOCK8(dest))); + LWIP_DEBUGF(IP6_DEBUG, ("ip6_output: no route for %"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F":%"X16_F"\n", + IP6_ADDR_BLOCK1(dest), + IP6_ADDR_BLOCK2(dest), + IP6_ADDR_BLOCK3(dest), + IP6_ADDR_BLOCK4(dest), + IP6_ADDR_BLOCK5(dest), + IP6_ADDR_BLOCK6(dest), + IP6_ADDR_BLOCK7(dest), + IP6_ADDR_BLOCK8(dest))); IP6_STATS_INC(ip6.rterr); return ERR_RTE; } @@ -1466,43 +1454,37 @@ ip6_debug_print(struct pbuf *p) LWIP_DEBUGF(IP6_DEBUG, ("IPv6 header:\n")); LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP6_DEBUG, - ("| %2" U16_F " | %3" U16_F " | %7" U32_F " | (ver, class, flow)\n", - IP6H_V(ip6hdr), - IP6H_TC(ip6hdr), - IP6H_FL(ip6hdr))); + LWIP_DEBUGF(IP6_DEBUG, ("| %2"U16_F" | %3"U16_F" | %7"U32_F" | (ver, class, flow)\n", + IP6H_V(ip6hdr), + IP6H_TC(ip6hdr), + IP6H_FL(ip6hdr))); LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP6_DEBUG, - ("| %5" U16_F " | %3" U16_F " | %3" U16_F " | (plen, nexth, hopl)\n", - IP6H_PLEN(ip6hdr), - IP6H_NEXTH(ip6hdr), - IP6H_HOPLIM(ip6hdr))); + LWIP_DEBUGF(IP6_DEBUG, ("| %5"U16_F" | %3"U16_F" | %3"U16_F" | (plen, nexth, hopl)\n", + IP6H_PLEN(ip6hdr), + IP6H_NEXTH(ip6hdr), + IP6H_HOPLIM(ip6hdr))); LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP6_DEBUG, - ("| %4" X32_F " | %4" X32_F " | %4" X32_F " | %4" X32_F " | (src)\n", - IP6_ADDR_BLOCK1(&(ip6hdr->src)), - IP6_ADDR_BLOCK2(&(ip6hdr->src)), - IP6_ADDR_BLOCK3(&(ip6hdr->src)), - IP6_ADDR_BLOCK4(&(ip6hdr->src)))); - LWIP_DEBUGF(IP6_DEBUG, - ("| %4" X32_F " | %4" X32_F " | %4" X32_F " | %4" X32_F " |\n", - IP6_ADDR_BLOCK5(&(ip6hdr->src)), - IP6_ADDR_BLOCK6(&(ip6hdr->src)), - IP6_ADDR_BLOCK7(&(ip6hdr->src)), - IP6_ADDR_BLOCK8(&(ip6hdr->src)))); + LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (src)\n", + IP6_ADDR_BLOCK1(&(ip6hdr->src)), + IP6_ADDR_BLOCK2(&(ip6hdr->src)), + IP6_ADDR_BLOCK3(&(ip6hdr->src)), + IP6_ADDR_BLOCK4(&(ip6hdr->src)))); + LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n", + IP6_ADDR_BLOCK5(&(ip6hdr->src)), + IP6_ADDR_BLOCK6(&(ip6hdr->src)), + IP6_ADDR_BLOCK7(&(ip6hdr->src)), + IP6_ADDR_BLOCK8(&(ip6hdr->src)))); LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n")); - LWIP_DEBUGF(IP6_DEBUG, - ("| %4" X32_F " | %4" X32_F " | %4" X32_F " | %4" X32_F " | (dest)\n", - IP6_ADDR_BLOCK1(&(ip6hdr->dest)), - IP6_ADDR_BLOCK2(&(ip6hdr->dest)), - IP6_ADDR_BLOCK3(&(ip6hdr->dest)), - IP6_ADDR_BLOCK4(&(ip6hdr->dest)))); - LWIP_DEBUGF(IP6_DEBUG, - ("| %4" X32_F " | %4" X32_F " | %4" X32_F " | %4" X32_F " |\n", - IP6_ADDR_BLOCK5(&(ip6hdr->dest)), - IP6_ADDR_BLOCK6(&(ip6hdr->dest)), - IP6_ADDR_BLOCK7(&(ip6hdr->dest)), - IP6_ADDR_BLOCK8(&(ip6hdr->dest)))); + LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" | (dest)\n", + IP6_ADDR_BLOCK1(&(ip6hdr->dest)), + IP6_ADDR_BLOCK2(&(ip6hdr->dest)), + IP6_ADDR_BLOCK3(&(ip6hdr->dest)), + IP6_ADDR_BLOCK4(&(ip6hdr->dest)))); + LWIP_DEBUGF(IP6_DEBUG, ("| %4"X32_F" | %4"X32_F" | %4"X32_F" | %4"X32_F" |\n", + IP6_ADDR_BLOCK5(&(ip6hdr->dest)), + IP6_ADDR_BLOCK6(&(ip6hdr->dest)), + IP6_ADDR_BLOCK7(&(ip6hdr->dest)), + IP6_ADDR_BLOCK8(&(ip6hdr->dest)))); LWIP_DEBUGF(IP6_DEBUG, ("+-------------------------------+\n")); } #endif /* IP6_DEBUG */ diff --git a/src/core/ipv6/ip6_addr.c b/src/core/ipv6/ip6_addr.c index 47782ffc..687c02f7 100644 --- a/src/core/ipv6/ip6_addr.c +++ b/src/core/ipv6/ip6_addr.c @@ -42,21 +42,21 @@ #include "lwip/opt.h" -#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ +#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ -#include "lwip/def.h" #include "lwip/ip_addr.h" +#include "lwip/def.h" #include #if LWIP_IPV4 #include "lwip/ip4_addr.h" /* for ip6addr_aton to handle IPv4-mapped addresses */ -#endif /* LWIP_IPV4 */ +#endif /* LWIP_IPV4 */ /* used by IP6_ADDR_ANY(6) in ip6_addr.h */ const ip_addr_t ip6_addr_any = IPADDR6_INIT(0ul, 0ul, 0ul, 0ul); -#define lwip_xchar(i) ((char)((i) < 10 ? '0' + (i) : 'A' + (i)-10)) +#define lwip_xchar(i) ((char)((i) < 10 ? '0' + (i) : 'A' + (i) - 10)) /** * Check whether "cp" is a valid ascii representation @@ -84,7 +84,7 @@ ip6addr_aton(const char *cp, ip6_addr_t *addr) zero_blocks--; #if LWIP_IPV4 } else if (*s == '.') { - if ((zero_blocks == 5) || (zero_blocks == 2)) { + if ((zero_blocks == 5) ||(zero_blocks == 2)) { check_ipv4_mapped = 1; /* last block could be the start of an IPv4 address */ zero_blocks--; @@ -108,7 +108,8 @@ ip6addr_aton(const char *cp, ip6_addr_t *addr) if (addr) { if (current_block_index & 0x1) { addr->addr[addr_index++] |= current_block_value; - } else { + } + else { addr->addr[addr_index] = current_block_value << 16; } } @@ -159,9 +160,9 @@ ip6addr_aton(const char *cp, ip6_addr_t *addr) } } else if (lwip_isxdigit(*s)) { /* add current digit */ - current_block_value = - (current_block_value << 4) + - (lwip_isdigit(*s) ? (u32_t)(*s - '0') : (u32_t)(10 + (lwip_islower(*s) ? *s - 'a' : *s - 'A'))); + current_block_value = (current_block_value << 4) + + (lwip_isdigit(*s) ? (u32_t)(*s - '0') : + (u32_t)(10 + (lwip_islower(*s) ? *s - 'a' : *s - 'A'))); } else { /* unexpected digit, space? CRLF? */ break; @@ -171,11 +172,12 @@ ip6addr_aton(const char *cp, ip6_addr_t *addr) if (addr) { if (current_block_index & 0x1) { addr->addr[addr_index++] |= current_block_value; - } else { + } + else { addr->addr[addr_index] = current_block_value << 16; } #if LWIP_IPV4 - fix_byte_order_and_return: +fix_byte_order_and_return: #endif /* convert to network byte order. */ for (addr_index = 0; addr_index < 4; addr_index++) { @@ -269,7 +271,7 @@ ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen) * according to current formatting suggestions RFC 5952. */ next_block_value = lwip_htonl(addr->addr[(current_block_index + 1) >> 1]); if ((current_block_index & 0x1) == 0x01) { - next_block_value = next_block_value >> 16; + next_block_value = next_block_value >> 16; } next_block_value &= 0xffff; if (next_block_value == 0) { @@ -318,7 +320,8 @@ ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen) if (((current_block_value & 0xf0) == 0) && (zero_flag)) { /* do nothing */ - } else { + } + else { buf[i++] = lwip_xchar(((current_block_value & 0xf0) >> 4)); zero_flag = 0; if (i >= buflen) { diff --git a/src/core/ipv6/ip6_frag.c b/src/core/ipv6/ip6_frag.c index 6cc93d0e..d6c5d223 100644 --- a/src/core/ipv6/ip6_frag.c +++ b/src/core/ipv6/ip6_frag.c @@ -39,20 +39,21 @@ * */ -#include "lwip/ip6_frag.h" -#include "lwip/icmp6.h" -#include "lwip/ip.h" -#include "lwip/ip6.h" -#include "lwip/nd6.h" #include "lwip/opt.h" +#include "lwip/ip6_frag.h" +#include "lwip/ip6.h" +#include "lwip/icmp6.h" +#include "lwip/nd6.h" +#include "lwip/ip.h" -#include "lwip/memp.h" #include "lwip/pbuf.h" +#include "lwip/memp.h" #include "lwip/stats.h" #include -#if LWIP_IPV6 && LWIP_IPV6_REASS /* don't build if not configured for use in lwipopts.h */ +#if LWIP_IPV6 && LWIP_IPV6_REASS /* don't build if not configured for use in lwipopts.h */ + /** Setting this to 0, you can turn off checking the fragments for overlapping * regions. The code gets a little smaller. Only use this if you know that @@ -85,18 +86,17 @@ * track of the various fragments. */ #ifdef PACK_STRUCT_USE_INCLUDES -#include "arch/bpstruct.h" +# include "arch/bpstruct.h" #endif PACK_STRUCT_BEGIN -struct ip6_reass_helper -{ +struct ip6_reass_helper { PACK_STRUCT_FIELD(struct pbuf *next_pbuf); PACK_STRUCT_FIELD(u16_t start); PACK_STRUCT_FIELD(u16_t end); } PACK_STRUCT_STRUCT; PACK_STRUCT_END #ifdef PACK_STRUCT_USE_INCLUDES -#include "arch/epstruct.h" +# include "arch/epstruct.h" #endif /* static variables */ @@ -104,11 +104,9 @@ static struct ip6_reassdata *reassdatagrams; static u16_t ip6_reass_pbufcount; /* Forward declarations. */ -static void -ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr); +static void ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr); #if IP_REASS_FREE_OLDEST -static void -ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed); +static void ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed); #endif /* IP_REASS_FREE_OLDEST */ void @@ -118,7 +116,7 @@ ip6_reass_tmr(void) #if !IPV6_FRAG_COPYHEADER LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1", - sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN); + sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN); #endif /* !IPV6_FRAG_COPYHEADER */ r = reassdatagrams; @@ -135,8 +133,8 @@ ip6_reass_tmr(void) r = r->next; /* free the helper struct and all enqueued pbufs */ ip6_reass_free_complete_datagram(tmp); - } - } + } + } } /** @@ -167,9 +165,10 @@ ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr) MEMCPY(p->payload, ipr->orig_hdr, sizeof(iprh)); /* Then, move back to the original ipv6 header (we are now pointing to Fragment header). This cannot fail since we already checked when receiving this fragment. */ - if (pbuf_header_force(p, (s16_t)((u8_t *)p->payload - (u8_t *)ipr->iphdr))) { + if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)ipr->iphdr))) { LWIP_ASSERT("ip6_reass_free: moving p->payload to ip6 header failed\n", 0); - } else { + } + else { /* Reconstruct the zoned source and destination addresses, so that we do * not end up sending the ICMP response over the wrong link. */ ip6_addr_t src_addr, dest_addr; @@ -273,7 +272,7 @@ struct pbuf * ip6_reass(struct pbuf *p) { struct ip6_reassdata *ipr, *ipr_prev; - struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev = NULL; + struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL; struct ip6_frag_hdr *frag_hdr; u16_t offset, len, start, end; ptrdiff_t hdrdiff; @@ -284,9 +283,10 @@ ip6_reass(struct pbuf *p) IP6_FRAG_STATS_INC(ip6_frag.recv); /* ip6_frag_hdr must be in the first pbuf, not chained. Checked by caller. */ - LWIP_ASSERT("IPv6 fragment header does not fit in first pbuf", p->len >= sizeof(struct ip6_frag_hdr)); + LWIP_ASSERT("IPv6 fragment header does not fit in first pbuf", + p->len >= sizeof(struct ip6_frag_hdr)); - frag_hdr = (struct ip6_frag_hdr *)p->payload; + frag_hdr = (struct ip6_frag_hdr *) p->payload; clen = pbuf_clen(p); @@ -296,7 +296,7 @@ ip6_reass(struct pbuf *p) * Adjust for headers before Fragment Header. * And finally adjust by Fragment Header length. */ len = lwip_ntohs(ip6_current_header()->_plen); - hdrdiff = (u8_t *)p->payload - (const u8_t *)ip6_current_header(); + hdrdiff = (u8_t*)p->payload - (const u8_t*)ip6_current_header(); LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff <= 0xFFFF); LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff >= IP6_HLEN); hdrdiff -= IP6_HLEN; @@ -329,7 +329,7 @@ ip6_reass(struct pbuf *p) } if (ipr == NULL) { - /* Enqueue a new datagram into the datagram queue */ + /* Enqueue a new datagram into the datagram queue */ ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA); if (ipr == NULL) { #if IP_REASS_FREE_OLDEST @@ -414,9 +414,9 @@ ip6_reass(struct pbuf *p) LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */ LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0); } -#else /* IPV6_FRAG_COPYHEADER */ +#else /* IPV6_FRAG_COPYHEADER */ LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1", - sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN); + sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN); #endif /* IPV6_FRAG_COPYHEADER */ /* Prepare the pointer to the helper structure, and its initial values. @@ -431,7 +431,7 @@ ip6_reass(struct pbuf *p) /* Iterate through until we either get to the end of the list (append), * or we find on with a larger offset (insert). */ for (q = ipr->p; q != NULL;) { - iprh_tmp = (struct ip6_reass_helper *)q->payload; + iprh_tmp = (struct ip6_reass_helper*)q->payload; if (start < iprh_tmp->start) { #if IP_REASS_CHECK_OVERLAP if (end > iprh_tmp->start) { @@ -494,7 +494,8 @@ ip6_reass(struct pbuf *p) } } else { #if IP_REASS_CHECK_OVERLAP - LWIP_ASSERT("no previous fragment, this must be the first fragment!", ipr->p == NULL); + LWIP_ASSERT("no previous fragment, this must be the first fragment!", + ipr->p == NULL); #endif /* IP_REASS_CHECK_OVERLAP */ /* this is the first fragment we ever received for this ip datagram */ ipr->p = p; @@ -527,7 +528,7 @@ ip6_reass(struct pbuf *p) } /* Additional validity tests: we have received first and last fragment. */ - iprh_tmp = (struct ip6_reass_helper *)ipr->p->payload; + iprh_tmp = (struct ip6_reass_helper*)ipr->p->payload; if (iprh_tmp->start != 0) { valid = 0; } @@ -539,7 +540,7 @@ ip6_reass(struct pbuf *p) iprh_prev = iprh; q = iprh->next_pbuf; while ((q != NULL) && valid) { - iprh = (struct ip6_reass_helper *)q->payload; + iprh = (struct ip6_reass_helper*)q->payload; if (iprh_prev->end != iprh->start) { valid = 0; break; @@ -550,15 +551,15 @@ ip6_reass(struct pbuf *p) if (valid) { /* All fragments have been received */ - struct ip6_hdr *iphdr_ptr; + struct ip6_hdr* iphdr_ptr; /* chain together the pbufs contained within the ip6_reassdata list. */ - iprh = (struct ip6_reass_helper *)ipr->p->payload; + iprh = (struct ip6_reass_helper*) ipr->p->payload; while (iprh != NULL) { next_pbuf = iprh->next_pbuf; if (next_pbuf != NULL) { /* Save next helper struct (will be hidden in next step). */ - iprh_tmp = (struct ip6_reass_helper *)next_pbuf->payload; + iprh_tmp = (struct ip6_reass_helper*)next_pbuf->payload; /* hide the fragment header for every succeeding fragment */ pbuf_remove_header(next_pbuf, IP6_FRAG_HLEN); @@ -571,7 +572,8 @@ ip6_reass(struct pbuf *p) } #endif pbuf_cat(ipr->p, next_pbuf); - } else { + } + else { iprh_tmp = NULL; } @@ -604,14 +606,16 @@ ip6_reass(struct pbuf *p) * accordingly. This works because all these headers are in the first pbuf * of the chain, and because the caller adjusts all its pointers on * successful reassembly. */ - MEMMOVE( - (u8_t *)ipr->iphdr + sizeof(struct ip6_frag_hdr), ipr->iphdr, (size_t)((u8_t *)p->payload - (u8_t *)ipr->iphdr)); + MEMMOVE((u8_t*)ipr->iphdr + sizeof(struct ip6_frag_hdr), ipr->iphdr, + (size_t)((u8_t*)p->payload - (u8_t*)ipr->iphdr)); /* This is where the IPv6 header is now. */ - iphdr_ptr = (struct ip6_hdr *)((u8_t *)ipr->iphdr + sizeof(struct ip6_frag_hdr)); + iphdr_ptr = (struct ip6_hdr*)((u8_t*)ipr->iphdr + + sizeof(struct ip6_frag_hdr)); /* Adjust datagram length by adding header lengths. */ - ipr->datagram_len = (u16_t)(ipr->datagram_len + ((u8_t *)p->payload - (u8_t *)iphdr_ptr) - IP6_HLEN); + ipr->datagram_len = (u16_t)(ipr->datagram_len + ((u8_t*)p->payload - (u8_t*)iphdr_ptr) + - IP6_HLEN); /* Set payload length in ip header. */ iphdr_ptr->_plen = lwip_htons(ipr->datagram_len); @@ -647,7 +651,7 @@ ip6_reass(struct pbuf *p) ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount - clen); /* Move pbuf back to IPv6 header. This should never fail. */ - if (pbuf_header_force(p, (s16_t)((u8_t *)p->payload - (u8_t *)iphdr_ptr))) { + if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)iphdr_ptr))) { LWIP_ASSERT("ip6_reass: moving p->payload to ip6 header failed\n", 0); pbuf_free(p); return NULL; @@ -671,15 +675,15 @@ nullreturn: #if !LWIP_NETIF_TX_SINGLE_PBUF /** Allocate a new struct pbuf_custom_ref */ -static struct pbuf_custom_ref * +static struct pbuf_custom_ref* ip6_frag_alloc_pbuf_custom_ref(void) { - return (struct pbuf_custom_ref *)memp_malloc(MEMP_FRAG_PBUF); + return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF); } /** Free a struct pbuf_custom_ref */ static void -ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref *p) +ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p) { LWIP_ASSERT("p != NULL", p != NULL); memp_free(MEMP_FRAG_PBUF, p); @@ -690,9 +694,9 @@ ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref *p) static void ip6_frag_free_pbuf_custom(struct pbuf *p) { - struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref *)p; + struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p; LWIP_ASSERT("pcr != NULL", pcr != NULL); - LWIP_ASSERT("pcr == p", (void *)pcr == (void *)p); + LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p); if (pcr->original != NULL) { pbuf_free(pcr->original); } @@ -752,8 +756,9 @@ ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest) IP6_FRAG_STATS_INC(ip6_frag.memerr); return ERR_MEM; } - LWIP_ASSERT("this needs a pbuf in one piece!", (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL)); - poff += pbuf_copy_partial(p, (u8_t *)rambuf->payload + IP6_FRAG_HLEN, cop, poff); + LWIP_ASSERT("this needs a pbuf in one piece!", + (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL)); + poff += pbuf_copy_partial(p, (u8_t*)rambuf->payload + IP6_FRAG_HLEN, cop, poff); /* make room for the IP header */ if (pbuf_add_header(rambuf, IP6_HLEN)) { pbuf_free(rambuf); @@ -763,7 +768,7 @@ ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest) /* fill in the IP header */ SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN); ip6hdr = (struct ip6_hdr *)rambuf->payload; - frag_hdr = (struct ip6_frag_hdr *)((u8_t *)rambuf->payload + IP6_HLEN); + frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN); #else /* When not using a static buffer, create a chain of pbufs. * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header. @@ -775,10 +780,11 @@ ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest) IP6_FRAG_STATS_INC(ip6_frag.memerr); return ERR_MEM; } - LWIP_ASSERT("this needs a pbuf in one piece!", (p->len >= (IP6_HLEN))); + LWIP_ASSERT("this needs a pbuf in one piece!", + (p->len >= (IP6_HLEN))); SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN); ip6hdr = (struct ip6_hdr *)rambuf->payload; - frag_hdr = (struct ip6_frag_hdr *)((u8_t *)rambuf->payload + IP6_HLEN); + frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN); /* Can just adjust p directly for needed offset. */ p->payload = (u8_t *)p->payload + poff; @@ -827,8 +833,7 @@ ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest) /* Set headers */ frag_hdr->_nexth = original_ip6hdr->_nexth; frag_hdr->reserved = 0; - frag_hdr->_fragment_offset = - lwip_htons((u16_t)((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG))); + frag_hdr->_fragment_offset = lwip_htons((u16_t)((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG))); frag_hdr->_identification = lwip_htonl(identification); IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT); diff --git a/src/core/ipv6/mld6.c b/src/core/ipv6/mld6.c index 857a1c20..6387d468 100644 --- a/src/core/ipv6/mld6.c +++ b/src/core/ipv6/mld6.c @@ -6,7 +6,7 @@ * @ingroup ip6 * Multicast listener discovery for IPv6. Aims to be compliant with RFC 2710. * No support for MLDv2.\n - * Note: The allnodes (ff01::1, ff02::1) group is assumed be received by your + * Note: The allnodes (ff01::1, ff02::1) group is assumed be received by your * netif since it must always be received for correct IPv6 operation (e.g. SLAAC). * Ensure the netif filters are configured accordingly!\n * The netif flags also need NETIF_FLAG_MLD6 flag set to enable MLD6 on a @@ -53,41 +53,39 @@ #include "lwip/opt.h" -#if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */ +#if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */ +#include "lwip/mld6.h" +#include "lwip/prot/mld6.h" #include "lwip/icmp6.h" -#include "lwip/inet_chksum.h" -#include "lwip/ip.h" #include "lwip/ip6.h" #include "lwip/ip6_addr.h" -#include "lwip/memp.h" -#include "lwip/mld6.h" -#include "lwip/netif.h" +#include "lwip/ip.h" +#include "lwip/inet_chksum.h" #include "lwip/pbuf.h" -#include "lwip/prot/mld6.h" +#include "lwip/netif.h" +#include "lwip/memp.h" #include "lwip/stats.h" #include + /* * MLD constants */ -#define MLD6_HL 1 -#define MLD6_JOIN_DELAYING_MEMBER_TMR_MS (500) +#define MLD6_HL 1 +#define MLD6_JOIN_DELAYING_MEMBER_TMR_MS (500) -#define MLD6_GROUP_NON_MEMBER 0 -#define MLD6_GROUP_DELAYING_MEMBER 1 -#define MLD6_GROUP_IDLE_MEMBER 2 +#define MLD6_GROUP_NON_MEMBER 0 +#define MLD6_GROUP_DELAYING_MEMBER 1 +#define MLD6_GROUP_IDLE_MEMBER 2 /* Forward declarations. */ -static struct mld_group * -mld6_new_group(struct netif *ifp, const ip6_addr_t *addr); -static err_t -mld6_remove_group(struct netif *netif, struct mld_group *group); -static void -mld6_delayed_report(struct mld_group *group, u16_t maxresp); -static void -mld6_send(struct netif *netif, struct mld_group *group, u8_t type); +static struct mld_group *mld6_new_group(struct netif *ifp, const ip6_addr_t *addr); +static err_t mld6_remove_group(struct netif *netif, struct mld_group *group); +static void mld6_delayed_report(struct mld_group *group, u16_t maxresp); +static void mld6_send(struct netif *netif, struct mld_group *group, u8_t type); + /** * Stop MLD processing on interface @@ -157,6 +155,7 @@ mld6_lookfor_group(struct netif *ifp, const ip6_addr_t *addr) return NULL; } + /** * create a new group * @@ -173,11 +172,11 @@ mld6_new_group(struct netif *ifp, const ip6_addr_t *addr) group = (struct mld_group *)memp_malloc(MEMP_MLD6_GROUP); if (group != NULL) { ip6_addr_set(&(group->group_address), addr); - group->timer = 0; /* Not running */ - group->group_state = MLD6_GROUP_IDLE_MEMBER; + group->timer = 0; /* Not running */ + group->group_state = MLD6_GROUP_IDLE_MEMBER; group->last_reporter_flag = 0; - group->use = 0; - group->next = netif_mld6_data(ifp); + group->use = 0; + group->next = netif_mld6_data(ifp); netif_set_client_data(ifp, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, group); } @@ -217,6 +216,7 @@ mld6_remove_group(struct netif *netif, struct mld_group *group) return err; } + /** * Process an input MLD message. Called by icmp6_input. * @@ -243,53 +243,54 @@ mld6_input(struct pbuf *p, struct netif *inp) mld_hdr = (struct mld_header *)p->payload; switch (mld_hdr->type) { - case ICMP6_TYPE_MLQ: /* Multicast listener query. */ - /* Is it a general query? */ - if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) && ip6_addr_isany(&(mld_hdr->multicast_address))) { - MLD6_STATS_INC(mld6.rx_general); - /* Report all groups, except all nodes group, and if-local groups. */ - group = netif_mld6_data(inp); - while (group != NULL) { - if ((!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) && - (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) { - mld6_delayed_report(group, mld_hdr->max_resp_delay); - } - group = group->next; - } - } else { - /* Have we joined this group? - * We use IP6 destination address to have a memory aligned copy. - * mld_hdr->multicast_address should be the same. */ - MLD6_STATS_INC(mld6.rx_group); - group = mld6_lookfor_group(inp, ip6_current_dest_addr()); - if (group != NULL) { - /* Schedule a report. */ + case ICMP6_TYPE_MLQ: /* Multicast listener query. */ + /* Is it a general query? */ + if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) && + ip6_addr_isany(&(mld_hdr->multicast_address))) { + MLD6_STATS_INC(mld6.rx_general); + /* Report all groups, except all nodes group, and if-local groups. */ + group = netif_mld6_data(inp); + while (group != NULL) { + if ((!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) && + (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) { mld6_delayed_report(group, mld_hdr->max_resp_delay); } + group = group->next; } - break; /* ICMP6_TYPE_MLQ */ - case ICMP6_TYPE_MLR: /* Multicast listener report. */ + } else { /* Have we joined this group? * We use IP6 destination address to have a memory aligned copy. * mld_hdr->multicast_address should be the same. */ - MLD6_STATS_INC(mld6.rx_report); + MLD6_STATS_INC(mld6.rx_group); group = mld6_lookfor_group(inp, ip6_current_dest_addr()); if (group != NULL) { - /* If we are waiting to report, cancel it. */ - if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) { - group->timer = 0; /* stopped */ - group->group_state = MLD6_GROUP_IDLE_MEMBER; - group->last_reporter_flag = 0; - } + /* Schedule a report. */ + mld6_delayed_report(group, mld_hdr->max_resp_delay); } - break; /* ICMP6_TYPE_MLR */ - case ICMP6_TYPE_MLD: /* Multicast listener done. */ - /* Do nothing, router will query us. */ - break; /* ICMP6_TYPE_MLD */ - default: - MLD6_STATS_INC(mld6.proterr); - MLD6_STATS_INC(mld6.drop); - break; + } + break; /* ICMP6_TYPE_MLQ */ + case ICMP6_TYPE_MLR: /* Multicast listener report. */ + /* Have we joined this group? + * We use IP6 destination address to have a memory aligned copy. + * mld_hdr->multicast_address should be the same. */ + MLD6_STATS_INC(mld6.rx_report); + group = mld6_lookfor_group(inp, ip6_current_dest_addr()); + if (group != NULL) { + /* If we are waiting to report, cancel it. */ + if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) { + group->timer = 0; /* stopped */ + group->group_state = MLD6_GROUP_IDLE_MEMBER; + group->last_reporter_flag = 0; + } + } + break; /* ICMP6_TYPE_MLR */ + case ICMP6_TYPE_MLD: /* Multicast listener done. */ + /* Do nothing, router will query us. */ + break; /* ICMP6_TYPE_MLD */ + default: + MLD6_STATS_INC(mld6.proterr); + MLD6_STATS_INC(mld6.drop); + break; } pbuf_free(p); @@ -313,16 +314,16 @@ mld6_input(struct pbuf *p, struct netif *inp) err_t mld6_joingroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr) { - err_t err = ERR_VAL; /* no matching interface */ + err_t err = ERR_VAL; /* no matching interface */ struct netif *netif; LWIP_ASSERT_CORE_LOCKED(); /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* Should we join this interface ? */ - if (ip6_addr_isany(srcaddr) || netif_get_ip6_addr_match(netif, srcaddr) >= 0) { + if (ip6_addr_isany(srcaddr) || + netif_get_ip6_addr_match(netif, srcaddr) >= 0) { err = mld6_joingroup_netif(netif, groupaddr); if (err != ERR_OK) { return err; @@ -402,16 +403,16 @@ mld6_joingroup_netif(struct netif *netif, const ip6_addr_t *groupaddr) err_t mld6_leavegroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr) { - err_t err = ERR_VAL; /* no matching interface */ + err_t err = ERR_VAL; /* no matching interface */ struct netif *netif; LWIP_ASSERT_CORE_LOCKED(); /* loop through netif's */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { /* Should we leave this interface ? */ - if (ip6_addr_isany(srcaddr) || netif_get_ip6_addr_match(netif, srcaddr) >= 0) { + if (ip6_addr_isany(srcaddr) || + netif_get_ip6_addr_match(netif, srcaddr) >= 0) { err_t res = mld6_leavegroup_netif(netif, groupaddr); if (err != ERR_OK) { /* Store this result if we have not yet gotten a success */ @@ -484,6 +485,7 @@ mld6_leavegroup_netif(struct netif *netif, const ip6_addr_t *groupaddr) return ERR_VAL; } + /** * Periodic timer for mld processing. Must be called every * MLD6_TMR_INTERVAL milliseconds (100). @@ -495,8 +497,7 @@ mld6_tmr(void) { struct netif *netif; - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { struct mld_group *group = netif_mld6_data(netif); while (group != NULL) { @@ -542,7 +543,8 @@ mld6_delayed_report(struct mld_group *group, u16_t maxresp_in) /* Apply timer value if no report has been scheduled already. */ if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) || - ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) && ((group->timer == 0) || (maxresp < group->timer)))) { + ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) && + ((group->timer == 0) || (maxresp < group->timer)))) { group->timer = maxresp; group->group_state = MLD6_GROUP_DELAYING_MEMBER; } @@ -600,9 +602,9 @@ mld6_send(struct netif *netif, struct mld_group *group, u8_t type) ip6_addr_copy_to_packed(mld_hdr->multicast_address, group->group_address); #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) - { - mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, &(group->group_address)); + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { + mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, + src_addr, &(group->group_address)); } #endif /* CHECKSUM_GEN_ICMP6 */ @@ -616,8 +618,8 @@ mld6_send(struct netif *netif, struct mld_group *group, u8_t type) /* Send the packet out. */ MLD6_STATS_INC(mld6.xmit); - ip6_output_if( - p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address), MLD6_HL, 0, IP6_NEXTH_HOPBYHOP, netif); + ip6_output_if(p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address), + MLD6_HL, 0, IP6_NEXTH_HOPBYHOP, netif); pbuf_free(p); } diff --git a/src/core/ipv6/nd6.c b/src/core/ipv6/nd6.c index 21cd72df..db0c132e 100644 --- a/src/core/ipv6/nd6.c +++ b/src/core/ipv6/nd6.c @@ -43,25 +43,25 @@ #include "lwip/opt.h" -#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ +#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ -#include "lwip/dhcp6.h" -#include "lwip/dns.h" -#include "lwip/icmp6.h" -#include "lwip/inet_chksum.h" -#include "lwip/ip.h" -#include "lwip/ip6.h" -#include "lwip/ip6_addr.h" +#include "lwip/nd6.h" +#include "lwip/priv/nd6_priv.h" +#include "lwip/prot/nd6.h" +#include "lwip/prot/icmp6.h" +#include "lwip/pbuf.h" #include "lwip/mem.h" #include "lwip/memp.h" -#include "lwip/mld6.h" -#include "lwip/nd6.h" +#include "lwip/ip6.h" +#include "lwip/ip6_addr.h" +#include "lwip/inet_chksum.h" #include "lwip/netif.h" -#include "lwip/pbuf.h" -#include "lwip/priv/nd6_priv.h" -#include "lwip/prot/icmp6.h" -#include "lwip/prot/nd6.h" +#include "lwip/icmp6.h" +#include "lwip/mld6.h" +#include "lwip/dhcp6.h" +#include "lwip/ip.h" #include "lwip/stats.h" +#include "lwip/dns.h" #include @@ -93,67 +93,48 @@ static ip6_addr_t multicast_address; static u8_t nd6_tmr_rs_reduction; /* Static buffer to parse RA packet options */ -union ra_options -{ - struct lladdr_option lladdr; - struct mtu_option mtu; - struct prefix_option prefix; +union ra_options { + struct lladdr_option lladdr; + struct mtu_option mtu; + struct prefix_option prefix; #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS - struct rdnss_option rdnss; + struct rdnss_option rdnss; #endif }; static union ra_options nd6_ra_buffer; /* Forward declarations. */ -static s8_t -nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr); -static s8_t -nd6_new_neighbor_cache_entry(void); -static void -nd6_free_neighbor_cache_entry(s8_t i); -static s16_t -nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr); -static s16_t -nd6_new_destination_cache_entry(void); -static int -nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif); -static s8_t -nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif); -static s8_t -nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif); -static s8_t -nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif); -static s8_t -nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); -static s8_t -nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); -static s8_t -nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif); -static err_t -nd6_queue_packet(s8_t neighbor_index, struct pbuf *q); +static s8_t nd6_find_neighbor_cache_entry(const ip6_addr_t *ip6addr); +static s8_t nd6_new_neighbor_cache_entry(void); +static void nd6_free_neighbor_cache_entry(s8_t i); +static s16_t nd6_find_destination_cache_entry(const ip6_addr_t *ip6addr); +static s16_t nd6_new_destination_cache_entry(void); +static int nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif); +static s8_t nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif); +static s8_t nd6_get_router(const ip6_addr_t *router_addr, struct netif *netif); +static s8_t nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif); +static s8_t nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); +static s8_t nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif); +static s8_t nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif); +static err_t nd6_queue_packet(s8_t neighbor_index, struct pbuf *q); #define ND6_SEND_FLAG_MULTICAST_DEST 0x01 #define ND6_SEND_FLAG_ALLNODES_DEST 0x02 #define ND6_SEND_FLAG_ANY_SRC 0x04 -static void -nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); -static void -nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); -static void -nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags); +static void nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); +static void nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags); +static void nd6_send_neighbor_cache_probe(struct nd6_neighbor_cache_entry *entry, u8_t flags); #if LWIP_IPV6_SEND_ROUTER_SOLICIT -static err_t -nd6_send_rs(struct netif *netif); +static err_t nd6_send_rs(struct netif *netif); #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ #if LWIP_ND6_QUEUEING -static void -nd6_free_q(struct nd6_q_entry *q); +static void nd6_free_q(struct nd6_q_entry *q); #else /* LWIP_ND6_QUEUEING */ #define nd6_free_q(q) pbuf_free(q) #endif /* LWIP_ND6_QUEUEING */ -static void -nd6_send_q(s8_t i); +static void nd6_send_q(s8_t i); + /** * A local address has been determined to be a duplicate. Take the appropriate @@ -182,7 +163,8 @@ nd6_duplicate_addr_detected(struct netif *netif, s8_t addr_idx) if (addr_idx == 0) { s8_t i; for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) && !netif_ip6_addr_isstatic(netif, i)) { + if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) && + !netif_ip6_addr_isstatic(netif, i)) { netif_ip6_addr_set_state(netif, i, IP6_ADDR_DUPLICATED); } } @@ -201,7 +183,8 @@ nd6_duplicate_addr_detected(struct netif *netif, s8_t addr_idx) * @param prefix_addr an aligned copy of the prefix address */ static void -nd6_process_autoconfig_prefix(struct netif *netif, struct prefix_option *prefix_opt, const ip6_addr_t *prefix_addr) +nd6_process_autoconfig_prefix(struct netif *netif, + struct prefix_option *prefix_opt, const ip6_addr_t *prefix_addr) { ip6_addr_t ip6addr; u32_t valid_life, pref_life; @@ -258,8 +241,8 @@ nd6_process_autoconfig_prefix(struct netif *netif, struct prefix_option *prefix_ * creating addresses even if the link-local address is still in tentative * state though, and deal with the fallout of that upon DAD collision. */ addr_state = netif_ip6_addr_state(netif, 0); - if (!netif->ip6_autoconfig_enabled || valid_life == IP6_ADDR_LIFE_STATIC || ip6_addr_isinvalid(addr_state) || - ip6_addr_isduplicated(addr_state)) { + if (!netif->ip6_autoconfig_enabled || valid_life == IP6_ADDR_LIFE_STATIC || + ip6_addr_isinvalid(addr_state) || ip6_addr_isduplicated(addr_state)) { return; } @@ -268,11 +251,8 @@ nd6_process_autoconfig_prefix(struct netif *netif, struct prefix_option *prefix_ * all. As a side effect, find a free slot. Note that we cannot use * netif_add_ip6_address() here, as it would return ERR_OK if the address * already did exist, resulting in that address being given lifetimes. */ - IP6_ADDR(&ip6addr, - prefix_addr->addr[0], - prefix_addr->addr[1], - netif_ip6_addr(netif, 0)->addr[2], - netif_ip6_addr(netif, 0)->addr[3]); + IP6_ADDR(&ip6addr, prefix_addr->addr[0], prefix_addr->addr[1], + netif_ip6_addr(netif, 0)->addr[2], netif_ip6_addr(netif, 0)->addr[3]); ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif); free_idx = 0; @@ -314,14 +294,62 @@ nd6_input(struct pbuf *p, struct netif *inp) msg_type = *((u8_t *)p->payload); switch (msg_type) { - case ICMP6_TYPE_NA: /* Neighbor Advertisement. */ - { - struct na_header *na_hdr; - struct lladdr_option *lladdr_opt; - ip6_addr_t target_address; + case ICMP6_TYPE_NA: /* Neighbor Advertisement. */ + { + struct na_header *na_hdr; + struct lladdr_option *lladdr_opt; + ip6_addr_t target_address; - /* Check that na header fits in packet. */ - if (p->len < (sizeof(struct na_header))) { + /* Check that na header fits in packet. */ + if (p->len < (sizeof(struct na_header))) { + /* @todo debug message */ + pbuf_free(p); + ND6_STATS_INC(nd6.lenerr); + ND6_STATS_INC(nd6.drop); + return; + } + + na_hdr = (struct na_header *)p->payload; + + /* Create an aligned, zoned copy of the target address. */ + ip6_addr_copy_from_packed(target_address, na_hdr->target_address); + ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); + + /* Check a subset of the other RFC 4861 Sec. 7.1.2 requirements. */ + if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || na_hdr->code != 0 || + ip6_addr_ismulticast(&target_address)) { + pbuf_free(p); + ND6_STATS_INC(nd6.proterr); + ND6_STATS_INC(nd6.drop); + return; + } + + /* @todo RFC MUST: if IP destination is multicast, Solicited flag is zero */ + /* @todo RFC MUST: all included options have a length greater than zero */ + + /* Unsolicited NA?*/ + if (ip6_addr_ismulticast(ip6_current_dest_addr())) { + /* This is an unsolicited NA. + * link-layer changed? + * part of DAD mechanism? */ + +#if LWIP_IPV6_DUP_DETECT_ATTEMPTS + /* If the target address matches this netif, it is a DAD response. */ + for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { + if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && + !ip6_addr_isduplicated(netif_ip6_addr_state(inp, i)) && + ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { + /* We are using a duplicate address. */ + nd6_duplicate_addr_detected(inp, i); + + pbuf_free(p); + return; + } + } +#endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */ + + /* Check that link-layer address option also fits in packet. */ + if (p->len < (sizeof(struct na_header) + 2)) { /* @todo debug message */ pbuf_free(p); ND6_STATS_INC(nd6.lenerr); @@ -329,45 +357,39 @@ nd6_input(struct pbuf *p, struct netif *inp) return; } - na_hdr = (struct na_header *)p->payload; + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); - /* Create an aligned, zoned copy of the target address. */ - ip6_addr_copy_from_packed(target_address, na_hdr->target_address); - ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); - - /* Check a subset of the other RFC 4861 Sec. 7.1.2 requirements. */ - if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || na_hdr->code != 0 || - ip6_addr_ismulticast(&target_address)) { + if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) { + /* @todo debug message */ pbuf_free(p); - ND6_STATS_INC(nd6.proterr); + ND6_STATS_INC(nd6.lenerr); ND6_STATS_INC(nd6.drop); return; } - /* @todo RFC MUST: if IP destination is multicast, Solicited flag is zero */ - /* @todo RFC MUST: all included options have a length greater than zero */ - - /* Unsolicited NA?*/ - if (ip6_addr_ismulticast(ip6_current_dest_addr())) { - /* This is an unsolicited NA. - * link-layer changed? - * part of DAD mechanism? */ - -#if LWIP_IPV6_DUP_DETECT_ATTEMPTS - /* If the target address matches this netif, it is a DAD response. */ - for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && - !ip6_addr_isduplicated(netif_ip6_addr_state(inp, i)) && - ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { - /* We are using a duplicate address. */ - nd6_duplicate_addr_detected(inp, i); - - pbuf_free(p); - return; - } + /* This is an unsolicited NA, most likely there was a LLADDR change. */ + i = nd6_find_neighbor_cache_entry(&target_address); + if (i >= 0) { + if (na_hdr->flags & ND6_FLAG_OVERRIDE) { + MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); } -#endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */ + } + } else { + /* This is a solicited NA. + * neighbor address resolution response? + * neighbor unreachability detection response? */ + /* Find the cache entry corresponding to this na. */ + i = nd6_find_neighbor_cache_entry(&target_address); + if (i < 0) { + /* We no longer care about this target address. drop it. */ + pbuf_free(p); + return; + } + + /* Update cache entry. */ + if ((na_hdr->flags & ND6_FLAG_OVERRIDE) || + (neighbor_cache[i].state == ND6_INCOMPLETE)) { /* Check that link-layer address option also fits in packet. */ if (p->len < (sizeof(struct na_header) + 2)) { /* @todo debug message */ @@ -377,7 +399,7 @@ nd6_input(struct pbuf *p, struct netif *inp) return; } - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct na_header)); + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) { /* @todo debug message */ @@ -387,564 +409,531 @@ nd6_input(struct pbuf *p, struct netif *inp) return; } - /* This is an unsolicited NA, most likely there was a LLADDR change. */ - i = nd6_find_neighbor_cache_entry(&target_address); - if (i >= 0) { - if (na_hdr->flags & ND6_FLAG_OVERRIDE) { - MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - } - } - } else { - /* This is a solicited NA. - * neighbor address resolution response? - * neighbor unreachability detection response? */ - - /* Find the cache entry corresponding to this na. */ - i = nd6_find_neighbor_cache_entry(&target_address); - if (i < 0) { - /* We no longer care about this target address. drop it. */ - pbuf_free(p); - return; - } - - /* Update cache entry. */ - if ((na_hdr->flags & ND6_FLAG_OVERRIDE) || (neighbor_cache[i].state == ND6_INCOMPLETE)) { - /* Check that link-layer address option also fits in packet. */ - if (p->len < (sizeof(struct na_header) + 2)) { - /* @todo debug message */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; - } - - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct na_header)); - - if (p->len < (sizeof(struct na_header) + (lladdr_opt->length << 3))) { - /* @todo debug message */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; - } - - MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - } - - neighbor_cache[i].netif = inp; - neighbor_cache[i].state = ND6_REACHABLE; - neighbor_cache[i].counter.reachable_time = reachable_time; - - /* Send queued packets, if any. */ - if (neighbor_cache[i].q != NULL) { - nd6_send_q(i); - } + MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); } - break; /* ICMP6_TYPE_NA */ + neighbor_cache[i].netif = inp; + neighbor_cache[i].state = ND6_REACHABLE; + neighbor_cache[i].counter.reachable_time = reachable_time; + + /* Send queued packets, if any. */ + if (neighbor_cache[i].q != NULL) { + nd6_send_q(i); + } } - case ICMP6_TYPE_NS: /* Neighbor solicitation. */ - { - struct ns_header *ns_hdr; - struct lladdr_option *lladdr_opt; - ip6_addr_t target_address; - u8_t accepted; - /* Check that ns header fits in packet. */ - if (p->len < sizeof(struct ns_header)) { - /* @todo debug message */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; + break; /* ICMP6_TYPE_NA */ + } + case ICMP6_TYPE_NS: /* Neighbor solicitation. */ + { + struct ns_header *ns_hdr; + struct lladdr_option *lladdr_opt; + ip6_addr_t target_address; + u8_t accepted; + + /* Check that ns header fits in packet. */ + if (p->len < sizeof(struct ns_header)) { + /* @todo debug message */ + pbuf_free(p); + ND6_STATS_INC(nd6.lenerr); + ND6_STATS_INC(nd6.drop); + return; + } + + ns_hdr = (struct ns_header *)p->payload; + + /* Create an aligned, zoned copy of the target address. */ + ip6_addr_copy_from_packed(target_address, ns_hdr->target_address); + ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); + + /* Check a subset of the other RFC 4861 Sec. 7.1.1 requirements. */ + if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ns_hdr->code != 0 || + ip6_addr_ismulticast(&target_address)) { + pbuf_free(p); + ND6_STATS_INC(nd6.proterr); + ND6_STATS_INC(nd6.drop); + return; + } + + /* @todo RFC MUST: all included options have a length greater than zero */ + /* @todo RFC MUST: if IP source is 'any', destination is solicited-node multicast address */ + /* @todo RFC MUST: if IP source is 'any', there is no source LL address option */ + + /* Check if there is a link-layer address provided. Only point to it if in this buffer. */ + if (p->len >= (sizeof(struct ns_header) + 2)) { + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header)); + if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) { + lladdr_opt = NULL; } + } else { + lladdr_opt = NULL; + } - ns_hdr = (struct ns_header *)p->payload; + /* Check if the target address is configured on the receiving netif. */ + accepted = 0; + for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { + if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) || + (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) && + ip6_addr_isany(ip6_current_src_addr()))) && + ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { + accepted = 1; + break; + } + } - /* Create an aligned, zoned copy of the target address. */ - ip6_addr_copy_from_packed(target_address, ns_hdr->target_address); - ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); + /* NS not for us? */ + if (!accepted) { + pbuf_free(p); + return; + } - /* Check a subset of the other RFC 4861 Sec. 7.1.1 requirements. */ - if (IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ns_hdr->code != 0 || - ip6_addr_ismulticast(&target_address)) { + /* Check for ANY address in src (DAD algorithm). */ + if (ip6_addr_isany(ip6_current_src_addr())) { + /* Sender is validating this address. */ + for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { + if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && + ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { + /* Send a NA back so that the sender does not use this address. */ + nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST); + if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) { + /* We shouldn't use this address either. */ + nd6_duplicate_addr_detected(inp, i); + } + } + } + } else { + /* Sender is trying to resolve our address. */ + /* Verify that they included their own link-layer address. */ + if (lladdr_opt == NULL) { + /* Not a valid message. */ pbuf_free(p); ND6_STATS_INC(nd6.proterr); ND6_STATS_INC(nd6.drop); return; } - /* @todo RFC MUST: all included options have a length greater than zero */ - /* @todo RFC MUST: if IP source is 'any', destination is solicited-node multicast address */ - /* @todo RFC MUST: if IP source is 'any', there is no source LL address option */ - - /* Check if there is a link-layer address provided. Only point to it if in this buffer. */ - if (p->len >= (sizeof(struct ns_header) + 2)) { - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct ns_header)); - if (p->len < (sizeof(struct ns_header) + (lladdr_opt->length << 3))) { - lladdr_opt = NULL; - } - } else { - lladdr_opt = NULL; - } - - /* Check if the target address is configured on the receiving netif. */ - accepted = 0; - for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { - if ((ip6_addr_isvalid(netif_ip6_addr_state(inp, i)) || - (ip6_addr_istentative(netif_ip6_addr_state(inp, i)) && ip6_addr_isany(ip6_current_src_addr()))) && - ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { - accepted = 1; - break; - } - } - - /* NS not for us? */ - if (!accepted) { - pbuf_free(p); - return; - } - - /* Check for ANY address in src (DAD algorithm). */ - if (ip6_addr_isany(ip6_current_src_addr())) { - /* Sender is validating this address. */ - for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { - if (!ip6_addr_isinvalid(netif_ip6_addr_state(inp, i)) && - ip6_addr_cmp(&target_address, netif_ip6_addr(inp, i))) { - /* Send a NA back so that the sender does not use this address. */ - nd6_send_na(inp, netif_ip6_addr(inp, i), ND6_FLAG_OVERRIDE | ND6_SEND_FLAG_ALLNODES_DEST); - if (ip6_addr_istentative(netif_ip6_addr_state(inp, i))) { - /* We shouldn't use this address either. */ - nd6_duplicate_addr_detected(inp, i); - } - } - } - } else { - /* Sender is trying to resolve our address. */ - /* Verify that they included their own link-layer address. */ - if (lladdr_opt == NULL) { - /* Not a valid message. */ - pbuf_free(p); - ND6_STATS_INC(nd6.proterr); - ND6_STATS_INC(nd6.drop); - return; - } - - i = nd6_find_neighbor_cache_entry(ip6_current_src_addr()); - if (i >= 0) { - /* We already have a record for the solicitor. */ - if (neighbor_cache[i].state == ND6_INCOMPLETE) { - neighbor_cache[i].netif = inp; - MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - - /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */ - neighbor_cache[i].state = ND6_DELAY; - neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; - } - } else { - /* Add their IPv6 address and link-layer address to neighbor cache. - * We will need it at least to send a unicast NA message, but most - * likely we will also be communicating with this node soon. */ - i = nd6_new_neighbor_cache_entry(); - if (i < 0) { - /* We couldn't assign a cache entry for this neighbor. - * we won't be able to reply. drop it. */ - pbuf_free(p); - ND6_STATS_INC(nd6.memerr); - return; - } + i = nd6_find_neighbor_cache_entry(ip6_current_src_addr()); + if (i>= 0) { + /* We already have a record for the solicitor. */ + if (neighbor_cache[i].state == ND6_INCOMPLETE) { neighbor_cache[i].netif = inp; MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr()); - /* Receiving a message does not prove reachability: only in one direction. - * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ + /* Delay probe in case we get confirmation of reachability from upper layer (TCP). */ neighbor_cache[i].state = ND6_DELAY; neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; } + } else { + /* Add their IPv6 address and link-layer address to neighbor cache. + * We will need it at least to send a unicast NA message, but most + * likely we will also be communicating with this node soon. */ + i = nd6_new_neighbor_cache_entry(); + if (i < 0) { + /* We couldn't assign a cache entry for this neighbor. + * we won't be able to reply. drop it. */ + pbuf_free(p); + ND6_STATS_INC(nd6.memerr); + return; + } + neighbor_cache[i].netif = inp; + MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); + ip6_addr_set(&(neighbor_cache[i].next_hop_address), ip6_current_src_addr()); - /* Send back a NA for us. Allocate the reply pbuf. */ - nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE); + /* Receiving a message does not prove reachability: only in one direction. + * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ + neighbor_cache[i].state = ND6_DELAY; + neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; } - break; /* ICMP6_TYPE_NS */ + /* Send back a NA for us. Allocate the reply pbuf. */ + nd6_send_na(inp, &target_address, ND6_FLAG_SOLICITED | ND6_FLAG_OVERRIDE); } - case ICMP6_TYPE_RA: /* Router Advertisement. */ - { - struct ra_header *ra_hdr; - u8_t *buffer; /* Used to copy options. */ - u16_t offset; + + break; /* ICMP6_TYPE_NS */ + } + case ICMP6_TYPE_RA: /* Router Advertisement. */ + { + struct ra_header *ra_hdr; + u8_t *buffer; /* Used to copy options. */ + u16_t offset; #if LWIP_ND6_RDNSS_MAX_DNS_SERVERS - /* There can be multiple RDNSS options per RA */ - u8_t rdnss_server_idx = 0; + /* There can be multiple RDNSS options per RA */ + u8_t rdnss_server_idx = 0; #endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */ - /* Check that RA header fits in packet. */ - if (p->len < sizeof(struct ra_header)) { - /* @todo debug message */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; - } - - ra_hdr = (struct ra_header *)p->payload; - - /* Check a subset of the other RFC 4861 Sec. 6.1.2 requirements. */ - if (!ip6_addr_islinklocal(ip6_current_src_addr()) || IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || - ra_hdr->code != 0) { - pbuf_free(p); - ND6_STATS_INC(nd6.proterr); - ND6_STATS_INC(nd6.drop); - return; - } - - /* @todo RFC MUST: all included options have a length greater than zero */ - - /* If we are sending RS messages, stop. */ -#if LWIP_IPV6_SEND_ROUTER_SOLICIT - /* ensure at least one solicitation is sent (see RFC 4861, ch. 6.3.7) */ - if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) || (nd6_send_rs(inp) == ERR_OK)) { - inp->rs_count = 0; - } else { - inp->rs_count = 1; - } -#endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ - - /* Get the matching default router entry. */ - i = nd6_get_router(ip6_current_src_addr(), inp); - if (i < 0) { - /* Create a new router entry. */ - i = nd6_new_router(ip6_current_src_addr(), inp); - } - - if (i < 0) { - /* Could not create a new router entry. */ - pbuf_free(p); - ND6_STATS_INC(nd6.memerr); - return; - } - - /* Re-set invalidation timer. */ - default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime); - - /* Re-set default timer values. */ -#if LWIP_ND6_ALLOW_RA_UPDATES - if (ra_hdr->retrans_timer > 0) { - retrans_timer = lwip_htonl(ra_hdr->retrans_timer); - } - if (ra_hdr->reachable_time > 0) { - reachable_time = lwip_htonl(ra_hdr->reachable_time); - } -#endif /* LWIP_ND6_ALLOW_RA_UPDATES */ - - /* @todo set default hop limit... */ - /* ra_hdr->current_hop_limit;*/ - - /* Update flags in local entry (incl. preference). */ - default_router_list[i].flags = ra_hdr->flags; - -#if LWIP_IPV6_DHCP6 - /* Trigger DHCPv6 if enabled */ - dhcp6_nd6_ra_trigger( - inp, ra_hdr->flags & ND6_RA_FLAG_MANAGED_ADDR_CONFIG, ra_hdr->flags & ND6_RA_FLAG_OTHER_CONFIG); -#endif - - /* Offset to options. */ - offset = sizeof(struct ra_header); - - /* Process each option. */ - while ((p->tot_len - offset) >= 2) { - u8_t option_type; - u16_t option_len; - int option_len8 = pbuf_try_get_at(p, offset + 1); - if (option_len8 <= 0) { - /* read beyond end or zero length */ - goto lenerr_drop_free_return; - } - option_len = ((u8_t)option_len8) << 3; - if (option_len > p->tot_len - offset) { - /* short packet (option does not fit in) */ - goto lenerr_drop_free_return; - } - if (p->len == p->tot_len) { - /* no need to copy from contiguous pbuf */ - buffer = &((u8_t *)p->payload)[offset]; - } else { - /* check if this option fits into our buffer */ - if (option_len > sizeof(nd6_ra_buffer)) { - option_type = pbuf_get_at(p, offset); - /* invalid option length */ - if (option_type != ND6_OPTION_TYPE_RDNSS) { - goto lenerr_drop_free_return; - } - /* we allow RDNSS option to be longer - we'll just drop some servers */ - option_len = sizeof(nd6_ra_buffer); - } - buffer = (u8_t *)&nd6_ra_buffer; - option_len = pbuf_copy_partial(p, &nd6_ra_buffer, option_len, offset); - } - option_type = buffer[0]; - switch (option_type) { - case ND6_OPTION_TYPE_SOURCE_LLADDR: { - struct lladdr_option *lladdr_opt; - if (option_len < sizeof(struct lladdr_option)) { - goto lenerr_drop_free_return; - } - lladdr_opt = (struct lladdr_option *)buffer; - if ((default_router_list[i].neighbor_entry != NULL) && - (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) { - SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len); - default_router_list[i].neighbor_entry->state = ND6_REACHABLE; - default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time; - } - break; - } - case ND6_OPTION_TYPE_MTU: { - struct mtu_option *mtu_opt; - u32_t mtu32; - if (option_len < sizeof(struct mtu_option)) { - goto lenerr_drop_free_return; - } - mtu_opt = (struct mtu_option *)buffer; - mtu32 = lwip_htonl(mtu_opt->mtu); - if ((mtu32 >= 1280) && (mtu32 <= 0xffff)) { -#if LWIP_ND6_ALLOW_RA_UPDATES - if (inp->mtu) { - /* don't set the mtu for IPv6 higher than the netif driver supports */ - inp->mtu6 = LWIP_MIN(inp->mtu, (u16_t)mtu32); - } else { - inp->mtu6 = (u16_t)mtu32; - } -#endif /* LWIP_ND6_ALLOW_RA_UPDATES */ - } - break; - } - case ND6_OPTION_TYPE_PREFIX_INFO: { - struct prefix_option *prefix_opt; - ip6_addr_t prefix_addr; - if (option_len < sizeof(struct prefix_option)) { - goto lenerr_drop_free_return; - } - - prefix_opt = (struct prefix_option *)buffer; - - /* Get a memory-aligned copy of the prefix. */ - ip6_addr_copy_from_packed(prefix_addr, prefix_opt->prefix); - ip6_addr_assign_zone(&prefix_addr, IP6_UNICAST, inp); - - if (!ip6_addr_islinklocal(&prefix_addr)) { - if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) && (prefix_opt->prefix_length == 64)) { - /* Add to on-link prefix list. */ - u32_t valid_life; - s8_t prefix; - - valid_life = lwip_htonl(prefix_opt->valid_lifetime); - - /* find cache entry for this prefix. */ - prefix = nd6_get_onlink_prefix(&prefix_addr, inp); - if (prefix < 0 && valid_life > 0) { - /* Create a new cache entry. */ - prefix = nd6_new_onlink_prefix(&prefix_addr, inp); - } - if (prefix >= 0) { - prefix_list[prefix].invalidation_timer = valid_life; - } - } -#if LWIP_IPV6_AUTOCONFIG - if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) { - /* Perform processing for autoconfiguration. */ - nd6_process_autoconfig_prefix(inp, prefix_opt, &prefix_addr); - } -#endif /* LWIP_IPV6_AUTOCONFIG */ - } - - break; - } - case ND6_OPTION_TYPE_ROUTE_INFO: - /* @todo implement preferred routes. - struct route_option * route_opt; - route_opt = (struct route_option *)buffer;*/ - - break; -#if LWIP_ND6_RDNSS_MAX_DNS_SERVERS - case ND6_OPTION_TYPE_RDNSS: { - u8_t num, n; - u16_t copy_offset = offset + SIZEOF_RDNSS_OPTION_BASE; - struct rdnss_option *rdnss_opt; - if (option_len < SIZEOF_RDNSS_OPTION_BASE) { - goto lenerr_drop_free_return; - } - - rdnss_opt = (struct rdnss_option *)buffer; - num = (rdnss_opt->length - 1) / 2; - for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++) { - ip_addr_t rdnss_address; - - /* Copy directly from pbuf to get an aligned, zoned copy of the prefix. */ - if (pbuf_copy_partial(p, &rdnss_address, sizeof(ip6_addr_p_t), copy_offset) == sizeof(ip6_addr_p_t)) { - IP_SET_TYPE_VAL(rdnss_address, IPADDR_TYPE_V6); - ip6_addr_assign_zone(ip_2_ip6(&rdnss_address), IP6_UNKNOWN, inp); - - if (htonl(rdnss_opt->lifetime) > 0) { - /* TODO implement Lifetime > 0 */ - dns_setserver(rdnss_server_idx++, &rdnss_address); - } else { - /* TODO implement DNS removal in dns.c */ - u8_t s; - for (s = 0; s < DNS_MAX_SERVERS; s++) { - const ip_addr_t *addr = dns_getserver(s); - if (ip_addr_cmp(addr, &rdnss_address)) { - dns_setserver(s, NULL); - } - } - } - } - } - break; - } -#endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */ - default: - /* Unrecognized option, abort. */ - ND6_STATS_INC(nd6.proterr); - break; - } - /* option length is checked earlier to be non-zero to make sure loop ends */ - offset += 8 * (u8_t)option_len8; - } - - break; /* ICMP6_TYPE_RA */ - } - case ICMP6_TYPE_RD: /* Redirect */ - { - struct redirect_header *redir_hdr; - struct lladdr_option *lladdr_opt; - ip6_addr_t destination_address, target_address; - - /* Check that Redir header fits in packet. */ - if (p->len < sizeof(struct redirect_header)) { - /* @todo debug message */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; - } - - redir_hdr = (struct redirect_header *)p->payload; - - /* Create an aligned, zoned copy of the destination address. */ - ip6_addr_copy_from_packed(destination_address, redir_hdr->destination_address); - ip6_addr_assign_zone(&destination_address, IP6_UNICAST, inp); - - /* Check a subset of the other RFC 4861 Sec. 8.1 requirements. */ - if (!ip6_addr_islinklocal(ip6_current_src_addr()) || IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || - redir_hdr->code != 0 || ip6_addr_ismulticast(&destination_address)) { - pbuf_free(p); - ND6_STATS_INC(nd6.proterr); - ND6_STATS_INC(nd6.drop); - return; - } - - /* @todo RFC MUST: IP source address equals first-hop router for destination_address */ - /* @todo RFC MUST: ICMP target address is either link-local address or same as destination_address */ - /* @todo RFC MUST: all included options have a length greater than zero */ - - if (p->len >= (sizeof(struct redirect_header) + 2)) { - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct redirect_header)); - if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) { - lladdr_opt = NULL; - } - } else { - lladdr_opt = NULL; - } - - /* Find dest address in cache */ - dest_idx = nd6_find_destination_cache_entry(&destination_address); - if (dest_idx < 0) { - /* Destination not in cache, drop packet. */ - pbuf_free(p); - return; - } - - /* Create an aligned, zoned copy of the target address. */ - ip6_addr_copy_from_packed(target_address, redir_hdr->target_address); - ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); - - /* Set the new target address. */ - ip6_addr_copy(destination_cache[dest_idx].next_hop_addr, target_address); - - /* If Link-layer address of other router is given, try to add to neighbor cache. */ - if (lladdr_opt != NULL) { - if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) { - i = nd6_find_neighbor_cache_entry(&target_address); - if (i < 0) { - i = nd6_new_neighbor_cache_entry(); - if (i >= 0) { - neighbor_cache[i].netif = inp; - MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - ip6_addr_copy(neighbor_cache[i].next_hop_address, target_address); - - /* Receiving a message does not prove reachability: only in one direction. - * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ - neighbor_cache[i].state = ND6_DELAY; - neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; - } - } - if (i >= 0) { - if (neighbor_cache[i].state == ND6_INCOMPLETE) { - MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); - /* Receiving a message does not prove reachability: only in one direction. - * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ - neighbor_cache[i].state = ND6_DELAY; - neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; - } - } - } - } - break; /* ICMP6_TYPE_RD */ - } - case ICMP6_TYPE_PTB: /* Packet too big */ - { - struct icmp6_hdr *icmp6hdr; /* Packet too big message */ - struct ip6_hdr *ip6hdr; /* IPv6 header of the packet which caused the error */ - u32_t pmtu; - ip6_addr_t destination_address; - - /* Check that ICMPv6 header + IPv6 header fit in payload */ - if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) { - /* drop short packets */ - pbuf_free(p); - ND6_STATS_INC(nd6.lenerr); - ND6_STATS_INC(nd6.drop); - return; - } - - icmp6hdr = (struct icmp6_hdr *)p->payload; - ip6hdr = (struct ip6_hdr *)((u8_t *)p->payload + sizeof(struct icmp6_hdr)); - - /* Create an aligned, zoned copy of the destination address. */ - ip6_addr_copy_from_packed(destination_address, ip6hdr->dest); - ip6_addr_assign_zone(&destination_address, IP6_UNKNOWN, inp); - - /* Look for entry in destination cache. */ - dest_idx = nd6_find_destination_cache_entry(&destination_address); - if (dest_idx < 0) { - /* Destination not in cache, drop packet. */ - pbuf_free(p); - return; - } - - /* Change the Path MTU. */ - pmtu = lwip_htonl(icmp6hdr->data); - destination_cache[dest_idx].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF); - - break; /* ICMP6_TYPE_PTB */ + /* Check that RA header fits in packet. */ + if (p->len < sizeof(struct ra_header)) { + /* @todo debug message */ + pbuf_free(p); + ND6_STATS_INC(nd6.lenerr); + ND6_STATS_INC(nd6.drop); + return; } - default: + ra_hdr = (struct ra_header *)p->payload; + + /* Check a subset of the other RFC 4861 Sec. 6.1.2 requirements. */ + if (!ip6_addr_islinklocal(ip6_current_src_addr()) || + IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || ra_hdr->code != 0) { + pbuf_free(p); ND6_STATS_INC(nd6.proterr); ND6_STATS_INC(nd6.drop); - break; /* default */ + return; + } + + /* @todo RFC MUST: all included options have a length greater than zero */ + + /* If we are sending RS messages, stop. */ +#if LWIP_IPV6_SEND_ROUTER_SOLICIT + /* ensure at least one solicitation is sent (see RFC 4861, ch. 6.3.7) */ + if ((inp->rs_count < LWIP_ND6_MAX_MULTICAST_SOLICIT) || + (nd6_send_rs(inp) == ERR_OK)) { + inp->rs_count = 0; + } else { + inp->rs_count = 1; + } +#endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ + + /* Get the matching default router entry. */ + i = nd6_get_router(ip6_current_src_addr(), inp); + if (i < 0) { + /* Create a new router entry. */ + i = nd6_new_router(ip6_current_src_addr(), inp); + } + + if (i < 0) { + /* Could not create a new router entry. */ + pbuf_free(p); + ND6_STATS_INC(nd6.memerr); + return; + } + + /* Re-set invalidation timer. */ + default_router_list[i].invalidation_timer = lwip_htons(ra_hdr->router_lifetime); + + /* Re-set default timer values. */ +#if LWIP_ND6_ALLOW_RA_UPDATES + if (ra_hdr->retrans_timer > 0) { + retrans_timer = lwip_htonl(ra_hdr->retrans_timer); + } + if (ra_hdr->reachable_time > 0) { + reachable_time = lwip_htonl(ra_hdr->reachable_time); + } +#endif /* LWIP_ND6_ALLOW_RA_UPDATES */ + + /* @todo set default hop limit... */ + /* ra_hdr->current_hop_limit;*/ + + /* Update flags in local entry (incl. preference). */ + default_router_list[i].flags = ra_hdr->flags; + +#if LWIP_IPV6_DHCP6 + /* Trigger DHCPv6 if enabled */ + dhcp6_nd6_ra_trigger(inp, ra_hdr->flags & ND6_RA_FLAG_MANAGED_ADDR_CONFIG, + ra_hdr->flags & ND6_RA_FLAG_OTHER_CONFIG); +#endif + + /* Offset to options. */ + offset = sizeof(struct ra_header); + + /* Process each option. */ + while ((p->tot_len - offset) >= 2) { + u8_t option_type; + u16_t option_len; + int option_len8 = pbuf_try_get_at(p, offset + 1); + if (option_len8 <= 0) { + /* read beyond end or zero length */ + goto lenerr_drop_free_return; + } + option_len = ((u8_t)option_len8) << 3; + if (option_len > p->tot_len - offset) { + /* short packet (option does not fit in) */ + goto lenerr_drop_free_return; + } + if (p->len == p->tot_len) { + /* no need to copy from contiguous pbuf */ + buffer = &((u8_t*)p->payload)[offset]; + } else { + /* check if this option fits into our buffer */ + if (option_len > sizeof(nd6_ra_buffer)) { + option_type = pbuf_get_at(p, offset); + /* invalid option length */ + if (option_type != ND6_OPTION_TYPE_RDNSS) { + goto lenerr_drop_free_return; + } + /* we allow RDNSS option to be longer - we'll just drop some servers */ + option_len = sizeof(nd6_ra_buffer); + } + buffer = (u8_t*)&nd6_ra_buffer; + option_len = pbuf_copy_partial(p, &nd6_ra_buffer, option_len, offset); + } + option_type = buffer[0]; + switch (option_type) { + case ND6_OPTION_TYPE_SOURCE_LLADDR: + { + struct lladdr_option *lladdr_opt; + if (option_len < sizeof(struct lladdr_option)) { + goto lenerr_drop_free_return; + } + lladdr_opt = (struct lladdr_option *)buffer; + if ((default_router_list[i].neighbor_entry != NULL) && + (default_router_list[i].neighbor_entry->state == ND6_INCOMPLETE)) { + SMEMCPY(default_router_list[i].neighbor_entry->lladdr, lladdr_opt->addr, inp->hwaddr_len); + default_router_list[i].neighbor_entry->state = ND6_REACHABLE; + default_router_list[i].neighbor_entry->counter.reachable_time = reachable_time; + } + break; + } + case ND6_OPTION_TYPE_MTU: + { + struct mtu_option *mtu_opt; + u32_t mtu32; + if (option_len < sizeof(struct mtu_option)) { + goto lenerr_drop_free_return; + } + mtu_opt = (struct mtu_option *)buffer; + mtu32 = lwip_htonl(mtu_opt->mtu); + if ((mtu32 >= 1280) && (mtu32 <= 0xffff)) { +#if LWIP_ND6_ALLOW_RA_UPDATES + if (inp->mtu) { + /* don't set the mtu for IPv6 higher than the netif driver supports */ + inp->mtu6 = LWIP_MIN(inp->mtu, (u16_t)mtu32); + } else { + inp->mtu6 = (u16_t)mtu32; + } +#endif /* LWIP_ND6_ALLOW_RA_UPDATES */ + } + break; + } + case ND6_OPTION_TYPE_PREFIX_INFO: + { + struct prefix_option *prefix_opt; + ip6_addr_t prefix_addr; + if (option_len < sizeof(struct prefix_option)) { + goto lenerr_drop_free_return; + } + + prefix_opt = (struct prefix_option *)buffer; + + /* Get a memory-aligned copy of the prefix. */ + ip6_addr_copy_from_packed(prefix_addr, prefix_opt->prefix); + ip6_addr_assign_zone(&prefix_addr, IP6_UNICAST, inp); + + if (!ip6_addr_islinklocal(&prefix_addr)) { + if ((prefix_opt->flags & ND6_PREFIX_FLAG_ON_LINK) && + (prefix_opt->prefix_length == 64)) { + /* Add to on-link prefix list. */ + u32_t valid_life; + s8_t prefix; + + valid_life = lwip_htonl(prefix_opt->valid_lifetime); + + /* find cache entry for this prefix. */ + prefix = nd6_get_onlink_prefix(&prefix_addr, inp); + if (prefix < 0 && valid_life > 0) { + /* Create a new cache entry. */ + prefix = nd6_new_onlink_prefix(&prefix_addr, inp); + } + if (prefix >= 0) { + prefix_list[prefix].invalidation_timer = valid_life; + } + } +#if LWIP_IPV6_AUTOCONFIG + if (prefix_opt->flags & ND6_PREFIX_FLAG_AUTONOMOUS) { + /* Perform processing for autoconfiguration. */ + nd6_process_autoconfig_prefix(inp, prefix_opt, &prefix_addr); + } +#endif /* LWIP_IPV6_AUTOCONFIG */ + } + + break; + } + case ND6_OPTION_TYPE_ROUTE_INFO: + /* @todo implement preferred routes. + struct route_option * route_opt; + route_opt = (struct route_option *)buffer;*/ + + break; +#if LWIP_ND6_RDNSS_MAX_DNS_SERVERS + case ND6_OPTION_TYPE_RDNSS: + { + u8_t num, n; + u16_t copy_offset = offset + SIZEOF_RDNSS_OPTION_BASE; + struct rdnss_option * rdnss_opt; + if (option_len < SIZEOF_RDNSS_OPTION_BASE) { + goto lenerr_drop_free_return; + } + + rdnss_opt = (struct rdnss_option *)buffer; + num = (rdnss_opt->length - 1) / 2; + for (n = 0; (rdnss_server_idx < DNS_MAX_SERVERS) && (n < num); n++) { + ip_addr_t rdnss_address; + + /* Copy directly from pbuf to get an aligned, zoned copy of the prefix. */ + if (pbuf_copy_partial(p, &rdnss_address, sizeof(ip6_addr_p_t), copy_offset) == sizeof(ip6_addr_p_t)) { + IP_SET_TYPE_VAL(rdnss_address, IPADDR_TYPE_V6); + ip6_addr_assign_zone(ip_2_ip6(&rdnss_address), IP6_UNKNOWN, inp); + + if (htonl(rdnss_opt->lifetime) > 0) { + /* TODO implement Lifetime > 0 */ + dns_setserver(rdnss_server_idx++, &rdnss_address); + } else { + /* TODO implement DNS removal in dns.c */ + u8_t s; + for (s = 0; s < DNS_MAX_SERVERS; s++) { + const ip_addr_t *addr = dns_getserver(s); + if(ip_addr_cmp(addr, &rdnss_address)) { + dns_setserver(s, NULL); + } + } + } + } + } + break; + } +#endif /* LWIP_ND6_RDNSS_MAX_DNS_SERVERS */ + default: + /* Unrecognized option, abort. */ + ND6_STATS_INC(nd6.proterr); + break; + } + /* option length is checked earlier to be non-zero to make sure loop ends */ + offset += 8 * (u8_t)option_len8; + } + + break; /* ICMP6_TYPE_RA */ + } + case ICMP6_TYPE_RD: /* Redirect */ + { + struct redirect_header *redir_hdr; + struct lladdr_option *lladdr_opt; + ip6_addr_t destination_address, target_address; + + /* Check that Redir header fits in packet. */ + if (p->len < sizeof(struct redirect_header)) { + /* @todo debug message */ + pbuf_free(p); + ND6_STATS_INC(nd6.lenerr); + ND6_STATS_INC(nd6.drop); + return; + } + + redir_hdr = (struct redirect_header *)p->payload; + + /* Create an aligned, zoned copy of the destination address. */ + ip6_addr_copy_from_packed(destination_address, redir_hdr->destination_address); + ip6_addr_assign_zone(&destination_address, IP6_UNICAST, inp); + + /* Check a subset of the other RFC 4861 Sec. 8.1 requirements. */ + if (!ip6_addr_islinklocal(ip6_current_src_addr()) || + IP6H_HOPLIM(ip6_current_header()) != ND6_HOPLIM || + redir_hdr->code != 0 || ip6_addr_ismulticast(&destination_address)) { + pbuf_free(p); + ND6_STATS_INC(nd6.proterr); + ND6_STATS_INC(nd6.drop); + return; + } + + /* @todo RFC MUST: IP source address equals first-hop router for destination_address */ + /* @todo RFC MUST: ICMP target address is either link-local address or same as destination_address */ + /* @todo RFC MUST: all included options have a length greater than zero */ + + if (p->len >= (sizeof(struct redirect_header) + 2)) { + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct redirect_header)); + if (p->len < (sizeof(struct redirect_header) + (lladdr_opt->length << 3))) { + lladdr_opt = NULL; + } + } else { + lladdr_opt = NULL; + } + + /* Find dest address in cache */ + dest_idx = nd6_find_destination_cache_entry(&destination_address); + if (dest_idx < 0) { + /* Destination not in cache, drop packet. */ + pbuf_free(p); + return; + } + + /* Create an aligned, zoned copy of the target address. */ + ip6_addr_copy_from_packed(target_address, redir_hdr->target_address); + ip6_addr_assign_zone(&target_address, IP6_UNICAST, inp); + + /* Set the new target address. */ + ip6_addr_copy(destination_cache[dest_idx].next_hop_addr, target_address); + + /* If Link-layer address of other router is given, try to add to neighbor cache. */ + if (lladdr_opt != NULL) { + if (lladdr_opt->type == ND6_OPTION_TYPE_TARGET_LLADDR) { + i = nd6_find_neighbor_cache_entry(&target_address); + if (i < 0) { + i = nd6_new_neighbor_cache_entry(); + if (i >= 0) { + neighbor_cache[i].netif = inp; + MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); + ip6_addr_copy(neighbor_cache[i].next_hop_address, target_address); + + /* Receiving a message does not prove reachability: only in one direction. + * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ + neighbor_cache[i].state = ND6_DELAY; + neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; + } + } + if (i >= 0) { + if (neighbor_cache[i].state == ND6_INCOMPLETE) { + MEMCPY(neighbor_cache[i].lladdr, lladdr_opt->addr, inp->hwaddr_len); + /* Receiving a message does not prove reachability: only in one direction. + * Delay probe in case we get confirmation of reachability from upper layer (TCP). */ + neighbor_cache[i].state = ND6_DELAY; + neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; + } + } + } + } + break; /* ICMP6_TYPE_RD */ + } + case ICMP6_TYPE_PTB: /* Packet too big */ + { + struct icmp6_hdr *icmp6hdr; /* Packet too big message */ + struct ip6_hdr *ip6hdr; /* IPv6 header of the packet which caused the error */ + u32_t pmtu; + ip6_addr_t destination_address; + + /* Check that ICMPv6 header + IPv6 header fit in payload */ + if (p->len < (sizeof(struct icmp6_hdr) + IP6_HLEN)) { + /* drop short packets */ + pbuf_free(p); + ND6_STATS_INC(nd6.lenerr); + ND6_STATS_INC(nd6.drop); + return; + } + + icmp6hdr = (struct icmp6_hdr *)p->payload; + ip6hdr = (struct ip6_hdr *)((u8_t*)p->payload + sizeof(struct icmp6_hdr)); + + /* Create an aligned, zoned copy of the destination address. */ + ip6_addr_copy_from_packed(destination_address, ip6hdr->dest); + ip6_addr_assign_zone(&destination_address, IP6_UNKNOWN, inp); + + /* Look for entry in destination cache. */ + dest_idx = nd6_find_destination_cache_entry(&destination_address); + if (dest_idx < 0) { + /* Destination not in cache, drop packet. */ + pbuf_free(p); + return; + } + + /* Change the Path MTU. */ + pmtu = lwip_htonl(icmp6hdr->data); + destination_cache[dest_idx].pmtu = (u16_t)LWIP_MIN(pmtu, 0xFFFF); + + break; /* ICMP6_TYPE_PTB */ + } + + default: + ND6_STATS_INC(nd6.proterr); + ND6_STATS_INC(nd6.drop); + break; /* default */ } pbuf_free(p); @@ -955,6 +944,7 @@ lenerr_drop_free_return: pbuf_free(p); } + /** * Periodic timer for Neighbor discovery functions: * @@ -974,57 +964,57 @@ nd6_tmr(void) /* Process neighbor entries. */ for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { switch (neighbor_cache[i].state) { - case ND6_INCOMPLETE: - if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && - (!neighbor_cache[i].isrouter)) { - /* Retries exceeded. */ - nd6_free_neighbor_cache_entry(i); - } else { - /* Send a NS for this entry. */ - neighbor_cache[i].counter.probes_sent++; - nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST); - } - break; - case ND6_REACHABLE: - /* Send queued packets, if any are left. Should have been sent already. */ - if (neighbor_cache[i].q != NULL) { - nd6_send_q(i); - } - if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) { - /* Change to stale state. */ - neighbor_cache[i].state = ND6_STALE; - neighbor_cache[i].counter.stale_time = 0; - } else { - neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL; - } - break; - case ND6_STALE: - neighbor_cache[i].counter.stale_time++; - break; - case ND6_DELAY: - if (neighbor_cache[i].counter.delay_time <= 1) { - /* Change to PROBE state. */ - neighbor_cache[i].state = ND6_PROBE; - neighbor_cache[i].counter.probes_sent = 0; - } else { - neighbor_cache[i].counter.delay_time--; - } - break; - case ND6_PROBE: - if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && - (!neighbor_cache[i].isrouter)) { - /* Retries exceeded. */ - nd6_free_neighbor_cache_entry(i); - } else { - /* Send a NS for this entry. */ - neighbor_cache[i].counter.probes_sent++; - nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0); - } - break; - case ND6_NO_ENTRY: - default: - /* Do nothing. */ - break; + case ND6_INCOMPLETE: + if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && + (!neighbor_cache[i].isrouter)) { + /* Retries exceeded. */ + nd6_free_neighbor_cache_entry(i); + } else { + /* Send a NS for this entry. */ + neighbor_cache[i].counter.probes_sent++; + nd6_send_neighbor_cache_probe(&neighbor_cache[i], ND6_SEND_FLAG_MULTICAST_DEST); + } + break; + case ND6_REACHABLE: + /* Send queued packets, if any are left. Should have been sent already. */ + if (neighbor_cache[i].q != NULL) { + nd6_send_q(i); + } + if (neighbor_cache[i].counter.reachable_time <= ND6_TMR_INTERVAL) { + /* Change to stale state. */ + neighbor_cache[i].state = ND6_STALE; + neighbor_cache[i].counter.stale_time = 0; + } else { + neighbor_cache[i].counter.reachable_time -= ND6_TMR_INTERVAL; + } + break; + case ND6_STALE: + neighbor_cache[i].counter.stale_time++; + break; + case ND6_DELAY: + if (neighbor_cache[i].counter.delay_time <= 1) { + /* Change to PROBE state. */ + neighbor_cache[i].state = ND6_PROBE; + neighbor_cache[i].counter.probes_sent = 0; + } else { + neighbor_cache[i].counter.delay_time--; + } + break; + case ND6_PROBE: + if ((neighbor_cache[i].counter.probes_sent >= LWIP_ND6_MAX_MULTICAST_SOLICIT) && + (!neighbor_cache[i].isrouter)) { + /* Retries exceeded. */ + nd6_free_neighbor_cache_entry(i); + } else { + /* Send a NS for this entry. */ + neighbor_cache[i].counter.probes_sent++; + nd6_send_neighbor_cache_probe(&neighbor_cache[i], 0); + } + break; + case ND6_NO_ENTRY: + default: + /* Do nothing. */ + break; } } @@ -1043,8 +1033,8 @@ nd6_tmr(void) s8_t j; for (j = 0; j < LWIP_ND6_NUM_DESTINATIONS; j++) { if (ip6_addr_cmp(&destination_cache[j].next_hop_addr, - &default_router_list[i].neighbor_entry->next_hop_address)) { - ip6_addr_set_any(&destination_cache[j].destination_addr); + &default_router_list[i].neighbor_entry->next_hop_address)) { + ip6_addr_set_any(&destination_cache[j].destination_addr); } } default_router_list[i].neighbor_entry->isrouter = 0; @@ -1071,8 +1061,7 @@ nd6_tmr(void) } /* Process our own addresses, updating address lifetimes and/or DAD state. */ - NETIF_FOREACH(netif) - { + NETIF_FOREACH(netif) { for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) { u8_t addr_state; #if LWIP_IPV6_ADDRESS_LIFETIMES @@ -1091,7 +1080,8 @@ nd6_tmr(void) * late/never could clog up address slots on the netif. * As a result, we may end up expiring addresses of either type here. */ - if (!ip6_addr_isinvalid(addr_state) && !netif_ip6_addr_isstatic(netif, i)) { + if (!ip6_addr_isinvalid(addr_state) && + !netif_ip6_addr_isstatic(netif, i)) { u32_t life = netif_ip6_addr_valid_life(netif, i); if (life <= ND6_TMR_INTERVAL / 1000) { /* The address has expired. */ @@ -1129,7 +1119,8 @@ nd6_tmr(void) * deprecated right away. That should almost never happen, though. */ addr_state = IP6_ADDR_PREFERRED; #if LWIP_IPV6_ADDRESS_LIFETIMES - if (!netif_ip6_addr_isstatic(netif, i) && netif_ip6_addr_pref_life(netif, i) == 0) { + if (!netif_ip6_addr_isstatic(netif, i) && + netif_ip6_addr_pref_life(netif, i) == 0) { addr_state = IP6_ADDR_DEPRECATED; } #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */ @@ -1140,7 +1131,8 @@ nd6_tmr(void) /* Send a NS for this address. Use the unspecified address as source * address in all cases (RFC 4862 Sec. 5.4.2), not in the least * because as it is, we only consider multicast replies for DAD. */ - nd6_send_ns(netif, netif_ip6_addr(netif, i), ND6_SEND_FLAG_MULTICAST_DEST | ND6_SEND_FLAG_ANY_SRC); + nd6_send_ns(netif, netif_ip6_addr(netif, i), + ND6_SEND_FLAG_MULTICAST_DEST | ND6_SEND_FLAG_ANY_SRC); } } } @@ -1150,9 +1142,9 @@ nd6_tmr(void) /* Send router solicitation messages, if necessary. */ if (!nd6_tmr_rs_reduction) { nd6_tmr_rs_reduction = (ND6_RTR_SOLICITATION_INTERVAL / ND6_TMR_INTERVAL) - 1; - NETIF_FOREACH(netif) - { - if ((netif->rs_count > 0) && netif_is_up(netif) && netif_is_link_up(netif) && + NETIF_FOREACH(netif) { + if ((netif->rs_count > 0) && netif_is_up(netif) && + netif_is_link_up(netif) && !ip6_addr_isinvalid(netif_ip6_addr_state(netif, 0)) && !ip6_addr_isduplicated(netif_ip6_addr_state(netif, 0))) { if (nd6_send_rs(netif) == ERR_OK) { @@ -1164,6 +1156,7 @@ nd6_tmr(void) nd6_tmr_rs_reduction--; } #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ + } /** Send a neighbor solicitation message for a specific neighbor cache entry @@ -1194,7 +1187,8 @@ nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) LWIP_ASSERT("target address is required", target_addr != NULL); - if (!(flags & ND6_SEND_FLAG_ANY_SRC) && ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) { + if (!(flags & ND6_SEND_FLAG_ANY_SRC) && + ip6_addr_isvalid(netif_ip6_addr_state(netif,0))) { /* Use link-local address as source address. */ src_addr = netif_ip6_addr(netif, 0); /* calculate option length (in 8-byte-blocks) */ @@ -1222,7 +1216,7 @@ nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) ip6_addr_copy_to_packed(ns_hdr->target_address, *target_addr); if (lladdr_opt_len != 0) { - struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct ns_header)); + struct lladdr_option *lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct ns_header)); lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR; lladdr_opt->length = (u8_t)lladdr_opt_len; SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len); @@ -1236,15 +1230,16 @@ nd6_send_ns(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) } #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) - { - ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, target_addr); + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { + ns_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, + target_addr); } #endif /* CHECKSUM_GEN_ICMP6 */ /* Send the packet out. */ ND6_STATS_INC(nd6.xmit); - ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr, ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); + ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, target_addr, + ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); pbuf_free(p); } @@ -1282,7 +1277,7 @@ nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) /* Set fields. */ na_hdr = (struct na_header *)p->payload; - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct na_header)); + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct na_header)); na_hdr->type = ICMP6_TYPE_NA; na_hdr->code = 0; @@ -1311,15 +1306,16 @@ nd6_send_na(struct netif *netif, const ip6_addr_t *target_addr, u8_t flags) } #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) - { - na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, dest_addr); + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { + na_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, + dest_addr); } #endif /* CHECKSUM_GEN_ICMP6 */ /* Send the packet out. */ ND6_STATS_INC(nd6.xmit); - ip6_output_if(p, src_addr, dest_addr, ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); + ip6_output_if(p, src_addr, dest_addr, + ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); pbuf_free(p); } @@ -1370,24 +1366,24 @@ nd6_send_rs(struct netif *netif) if (src_addr != IP6_ADDR_ANY6) { /* Include our hw address. */ - lladdr_opt = (struct lladdr_option *)((u8_t *)p->payload + sizeof(struct rs_header)); + lladdr_opt = (struct lladdr_option *)((u8_t*)p->payload + sizeof(struct rs_header)); lladdr_opt->type = ND6_OPTION_TYPE_SOURCE_LLADDR; lladdr_opt->length = (u8_t)lladdr_opt_len; SMEMCPY(lladdr_opt->addr, netif->hwaddr, netif->hwaddr_len); } #if CHECKSUM_GEN_ICMP6 - IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) - { - rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, &multicast_address); + IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) { + rs_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len, src_addr, + &multicast_address); } #endif /* CHECKSUM_GEN_ICMP6 */ /* Send the packet out. */ ND6_STATS_INC(nd6.xmit); - err = ip6_output_if( - p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address, ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); + err = ip6_output_if(p, (src_addr == IP6_ADDR_ANY6) ? NULL : src_addr, &multicast_address, + ND6_HOPLIM, 0, IP6_NEXTH_ICMP6, netif); pbuf_free(p); return err; @@ -1429,6 +1425,7 @@ nd6_new_neighbor_cache_entry(void) s8_t j; u32_t time; + /* First, try to find an empty entry. */ for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { if (neighbor_cache[i].state == ND6_NO_ENTRY) { @@ -1440,7 +1437,8 @@ nd6_new_neighbor_cache_entry(void) /* Next, try to find a Stale entry. */ for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].state == ND6_STALE) && (!neighbor_cache[i].isrouter)) { + if ((neighbor_cache[i].state == ND6_STALE) && + (!neighbor_cache[i].isrouter)) { nd6_free_neighbor_cache_entry(i); return i; } @@ -1448,7 +1446,8 @@ nd6_new_neighbor_cache_entry(void) /* Next, try to find a Probe entry. */ for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].state == ND6_PROBE) && (!neighbor_cache[i].isrouter)) { + if ((neighbor_cache[i].state == ND6_PROBE) && + (!neighbor_cache[i].isrouter)) { nd6_free_neighbor_cache_entry(i); return i; } @@ -1456,7 +1455,8 @@ nd6_new_neighbor_cache_entry(void) /* Next, try to find a Delayed entry. */ for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].state == ND6_DELAY) && (!neighbor_cache[i].isrouter)) { + if ((neighbor_cache[i].state == ND6_DELAY) && + (!neighbor_cache[i].isrouter)) { nd6_free_neighbor_cache_entry(i); return i; } @@ -1466,7 +1466,8 @@ nd6_new_neighbor_cache_entry(void) time = 0xfffffffful; j = -1; for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].state == ND6_REACHABLE) && (!neighbor_cache[i].isrouter)) { + if ((neighbor_cache[i].state == ND6_REACHABLE) && + (!neighbor_cache[i].isrouter)) { if (neighbor_cache[i].counter.reachable_time < time) { j = i; time = neighbor_cache[i].counter.reachable_time; @@ -1482,7 +1483,10 @@ nd6_new_neighbor_cache_entry(void) time = 0; j = -1; for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].q == NULL) && (neighbor_cache[i].state == ND6_INCOMPLETE) && (!neighbor_cache[i].isrouter)) { + if ( + (neighbor_cache[i].q == NULL) && + (neighbor_cache[i].state == ND6_INCOMPLETE) && + (!neighbor_cache[i].isrouter)) { if (neighbor_cache[i].counter.probes_sent >= time) { j = i; time = neighbor_cache[i].counter.probes_sent; @@ -1498,7 +1502,8 @@ nd6_new_neighbor_cache_entry(void) time = 0; j = -1; for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) { - if ((neighbor_cache[i].state == ND6_INCOMPLETE) && (!neighbor_cache[i].isrouter)) { + if ((neighbor_cache[i].state == ND6_INCOMPLETE) && + (!neighbor_cache[i].isrouter)) { if (neighbor_cache[i].counter.probes_sent >= time) { j = i; time = neighbor_cache[i].counter.probes_sent; @@ -1628,7 +1633,8 @@ nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif) /* Check to see if the address matches an on-link prefix. */ for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) { - if ((prefix_list[i].netif == netif) && (prefix_list[i].invalidation_timer > 0) && + if ((prefix_list[i].netif == netif) && + (prefix_list[i].invalidation_timer > 0) && ip6_addr_netcmp(ip6addr, &(prefix_list[i].prefix))) { return 1; } @@ -1638,7 +1644,8 @@ nd6_is_prefix_in_netif(const ip6_addr_t *ip6addr, struct netif *netif) * addresses (from autoconfiguration) have no implied subnet assignment, and * are thus effectively /128 assignments. See RFC 5942 for more on this. */ for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { - if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && netif_ip6_addr_isstatic(netif, i) && + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + netif_ip6_addr_isstatic(netif, i) && ip6_addr_netcmp(ip6addr, netif_ip6_addr(netif, i))) { return 1; } @@ -1676,8 +1683,8 @@ nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif) /* Is the router netif both set and apppropriate? */ if (default_router_list[i].neighbor_entry != NULL) { router_netif = default_router_list[i].neighbor_entry->netif; - if ((router_netif != NULL) && - (netif != NULL ? netif == router_netif : (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { + if ((router_netif != NULL) && (netif != NULL ? netif == router_netif : + (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { /* Is the router valid, i.e., reachable or probably reachable as per * RFC 4861 Sec. 6.3.6? Note that we will never return a router that * has no neighbor cache entry, due to the netif association tests. */ @@ -1707,8 +1714,8 @@ nd6_select_router(const ip6_addr_t *ip6addr, struct netif *netif) for (j = 0; j < LWIP_ND6_NUM_ROUTERS; j++) { if (default_router_list[i].neighbor_entry != NULL) { router_netif = default_router_list[i].neighbor_entry->netif; - if ((router_netif != NULL) && - (netif != NULL ? netif == router_netif : (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { + if ((router_netif != NULL) && (netif != NULL ? netif == router_netif : + (netif_is_up(router_netif) && netif_is_link_up(router_netif)))) { return i; } } @@ -1743,8 +1750,8 @@ nd6_find_route(const ip6_addr_t *ip6addr) * matches. Pick the first one that is associated with a suitable netif. */ for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { netif = prefix_list[i].netif; - if ((netif != NULL) && ip6_addr_netcmp(&prefix_list[i].prefix, ip6addr) && netif_is_up(netif) && - netif_is_link_up(netif)) { + if ((netif != NULL) && ip6_addr_netcmp(&prefix_list[i].prefix, ip6addr) && + netif_is_up(netif) && netif_is_link_up(netif)) { return netif; } } @@ -1752,7 +1759,8 @@ nd6_find_route(const ip6_addr_t *ip6addr) /* No on-link prefix match. Find a router that can forward the packet. */ i = nd6_select_router(ip6addr, NULL); if (i >= 0) { - LWIP_ASSERT("selected router must have a neighbor entry", default_router_list[i].neighbor_entry != NULL); + LWIP_ASSERT("selected router must have a neighbor entry", + default_router_list[i].neighbor_entry != NULL); return default_router_list[i].neighbor_entry->netif; } @@ -1827,9 +1835,9 @@ nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif) for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) { /* check if router already exists (this is a special case for 2 netifs on the same subnet - e.g. wifi and cable) */ - if (default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])) { - return router_index; - } + if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){ + return router_index; + } if (default_router_list[router_index].neighbor_entry == NULL) { /* remember lowest free index to create a new entry */ free_router_index = router_index; @@ -1863,7 +1871,8 @@ nd6_get_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif) /* Look for prefix in list. */ for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { - if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) && (prefix_list[i].netif == netif)) { + if ((ip6_addr_netcmp(&(prefix_list[i].prefix), prefix)) && + (prefix_list[i].netif == netif)) { return i; } } @@ -1886,7 +1895,8 @@ nd6_new_onlink_prefix(const ip6_addr_t *prefix, struct netif *netif) /* Create new entry. */ for (i = 0; i < LWIP_ND6_NUM_PREFIXES; ++i) { - if ((prefix_list[i].netif == NULL) || (prefix_list[i].invalidation_timer == 0)) { + if ((prefix_list[i].netif == NULL) || + (prefix_list[i].invalidation_timer == 0)) { /* Found empty prefix entry. */ prefix_list[i].netif = netif; ip6_addr_set(&(prefix_list[i].prefix), prefix); @@ -1959,11 +1969,11 @@ nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif) ip6_addr_set(&(destination_cache[nd6_cached_destination_index].destination_addr), ip6addr); /* Now find the next hop. is it a neighbor? */ - if (ip6_addr_islinklocal(ip6addr) || nd6_is_prefix_in_netif(ip6addr, netif)) { + if (ip6_addr_islinklocal(ip6addr) || + nd6_is_prefix_in_netif(ip6addr, netif)) { /* Destination in local link. */ destination_cache[nd6_cached_destination_index].pmtu = netif_mtu6(netif); - ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, - destination_cache[nd6_cached_destination_index].destination_addr); + ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, destination_cache[nd6_cached_destination_index].destination_addr); #ifdef LWIP_HOOK_ND6_GET_GW } else if ((next_hop_addr = LWIP_HOOK_ND6_GET_GW(netif, ip6addr)) != NULL) { /* Next hop for destination provided by hook function. */ @@ -1978,10 +1988,8 @@ nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif) ip6_addr_set_any(&(destination_cache[nd6_cached_destination_index].destination_addr)); return ERR_RTE; } - destination_cache[nd6_cached_destination_index].pmtu = - netif_mtu6(netif); /* Start with netif mtu, correct through ICMPv6 if necessary */ - ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, - default_router_list[i].neighbor_entry->next_hop_address); + destination_cache[nd6_cached_destination_index].pmtu = netif_mtu6(netif); /* Start with netif mtu, correct through ICMPv6 if necessary */ + ip6_addr_copy(destination_cache[nd6_cached_destination_index].next_hop_addr, default_router_list[i].neighbor_entry->next_hop_address); } } } @@ -2016,7 +2024,8 @@ nd6_get_next_hop_entry(const ip6_addr_t *ip6addr, struct netif *netif) } /* Initialize fields. */ - ip6_addr_copy(neighbor_cache[i].next_hop_address, destination_cache[nd6_cached_destination_index].next_hop_addr); + ip6_addr_copy(neighbor_cache[i].next_hop_address, + destination_cache[nd6_cached_destination_index].next_hop_addr); neighbor_cache[i].isrouter = 0; neighbor_cache[i].netif = netif; neighbor_cache[i].state = ND6_INCOMPLETE; @@ -2072,7 +2081,7 @@ nd6_queue_packet(s8_t neighbor_index, struct pbuf *q) neighbor_cache[neighbor_index].q = r->next; r->next = NULL; nd6_free_q(r); -#else /* LWIP_ND6_QUEUEING */ +#else /* LWIP_ND6_QUEUEING */ pbuf_free(neighbor_cache[neighbor_index].q); neighbor_cache[neighbor_index].q = NULL; #endif /* LWIP_ND6_QUEUEING */ @@ -2111,8 +2120,7 @@ nd6_queue_packet(s8_t neighbor_index, struct pbuf *q) /* queue did not exist, first item in queue */ neighbor_cache[neighbor_index].q = new_entry; } - LWIP_DEBUGF(LWIP_DBG_TRACE, - ("ipv6: queued packet %p on neighbor entry %" S16_F "\n", (void *)p, (s16_t)neighbor_index)); + LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index)); result = ERR_OK; } else { /* the pool MEMP_ND6_QUEUE is empty */ @@ -2120,14 +2128,13 @@ nd6_queue_packet(s8_t neighbor_index, struct pbuf *q) LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: could not queue a copy of packet %p (out of memory)\n", (void *)p)); /* { result == ERR_MEM } through initialization */ } -#else /* LWIP_ND6_QUEUEING */ +#else /* LWIP_ND6_QUEUEING */ /* Queue a single packet. If an older packet is already queued, free it as per RFC. */ if (neighbor_cache[neighbor_index].q != NULL) { pbuf_free(neighbor_cache[neighbor_index].q); } neighbor_cache[neighbor_index].q = p; - LWIP_DEBUGF(LWIP_DBG_TRACE, - ("ipv6: queued packet %p on neighbor entry %" S16_F "\n", (void *)p, (s16_t)neighbor_index)); + LWIP_DEBUGF(LWIP_DBG_TRACE, ("ipv6: queued packet %p on neighbor entry %"S16_F"\n", (void *)p, (s16_t)neighbor_index)); result = ERR_OK; #endif /* LWIP_ND6_QUEUEING */ } else { @@ -2197,7 +2204,7 @@ nd6_send_q(s8_t i) /* now queue entry can be freed */ memp_free(MEMP_ND6_QUEUE, q); } -#else /* LWIP_ND6_QUEUEING */ +#else /* LWIP_ND6_QUEUEING */ if (neighbor_cache[i].q != NULL) { /* Get ipv6 header. */ ip6hdr = (struct ip6_hdr *)(neighbor_cache[i].q->payload); @@ -2255,7 +2262,8 @@ nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_ad neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL; } /* @todo should we send or queue if PROBE? send for now, to let unicast NS pass. */ - if ((neighbor_cache[i].state == ND6_REACHABLE) || (neighbor_cache[i].state == ND6_DELAY) || + if ((neighbor_cache[i].state == ND6_REACHABLE) || + (neighbor_cache[i].state == ND6_DELAY) || (neighbor_cache[i].state == ND6_PROBE)) { /* Tell the caller to send out the packet now. */ @@ -2268,6 +2276,7 @@ nd6_get_next_hop_addr_or_queue(struct netif *netif, struct pbuf *q, const ip6_ad return nd6_queue_packet(i, q); } + /** * Get the Path MTU for a destination. * @@ -2294,6 +2303,7 @@ nd6_get_destination_mtu(const ip6_addr_t *ip6addr, struct netif *netif) return 1280; /* Minimum MTU */ } + #if LWIP_ND6_TCP_REACHABILITY_HINTS /** * Provide the Neighbor discovery process with a hint that a @@ -2322,8 +2332,7 @@ nd6_reachability_hint(const ip6_addr_t *ip6addr) } /* Find next hop neighbor in cache. */ - if (ip6_addr_cmp(&(destination_cache[dst_idx].next_hop_addr), - &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) { + if (ip6_addr_cmp(&(destination_cache[dst_idx].next_hop_addr), &(neighbor_cache[nd6_cached_neighbor_index].next_hop_address))) { i = nd6_cached_neighbor_index; ND6_STATS_INC(nd6.cachehit); } else {