Commit c82bd6e1 authored by Jozef Kralik's avatar Jozef Kralik

fix IPv6

parent b26679b7
...@@ -24,6 +24,7 @@ ESP32 ...@@ -24,6 +24,7 @@ ESP32
- idf.py menuconfig // set wifi, mbedtls - idf.py menuconfig // set wifi, mbedtls
- ( cd esp-idf/components/mbedtls/mbedtls && git am ../../../../patches/mbedtls/*.patch ) - ( cd esp-idf/components/mbedtls/mbedtls && git am ../../../../patches/mbedtls/*.patch )
- ( cd esp-idf && patch -p1 < ../patches/esp-idf/*.patch ) - ( cd esp-idf && patch -p1 < ../patches/esp-idf/*.patch )
- ( cd esp-idf/components/lwip/lwip && patch -p1 < ../patches/lwip/*.patch )
- idf.py build - idf.py build
- idf.py -p (PORT) flash monitor - idf.py -p (PORT) flash monitor
......
...@@ -30,9 +30,6 @@ ...@@ -30,9 +30,6 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
//#include <ifaddrs.h>
//#include <linux/netlink.h>
//#include <linux/rtnetlink.h>
#include <net/if.h> #include <net/if.h>
#include <netdb.h> #include <netdb.h>
#include <signal.h> #include <signal.h>
...@@ -49,17 +46,9 @@ ...@@ -49,17 +46,9 @@
#include "esp_netif.h" #include "esp_netif.h"
#define ipi_spec_dst ipi_addr #define ipi_spec_dst ipi_addr
struct in6_pktinfo
{
struct in6_addr ipi6_addr; /* src/dst IPv6 address */
unsigned int ipi6_ifindex; /* send/recv interface index */
};
#define IN6_IS_ADDR_V4MAPPED(a) \ #define IN6_IS_ADDR_V4MAPPED(a) \
((((__const uint32_t *)(a))[0] == 0) && (((__const uint32_t *)(a))[1] == 0) && (((__const uint32_t *)(a))[2] == htonl(0xffff))) ((((__const uint32_t *)(a))[0] == 0) && (((__const uint32_t *)(a))[1] == 0) && (((__const uint32_t *)(a))[2] == htonl(0xffff)))
/* As not defined, just need to define is as something innocuous */
// #define IPV6_PKTINFO IPV6_CHECKSUM
/* Some outdated toolchains do not define IFA_FLAGS. /* Some outdated toolchains do not define IFA_FLAGS.
Note: Requires Linux kernel 3.14 or later. */ Note: Requires Linux kernel 3.14 or later. */
#ifndef IFA_FLAGS #ifndef IFA_FLAGS
...@@ -693,7 +682,7 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size, ...@@ -693,7 +682,7 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size,
struct cmsghdr *cmsg; struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != 0; cmsg = CMSG_NXTHDR(&msg, cmsg)) for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != 0; cmsg = CMSG_NXTHDR(&msg, cmsg))
{ {
if (cmsg->cmsg_level == IPPROTO_IPV6 /*&& cmsg->cmsg_type == IPV6_PKTINFO*/) if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO)
{ {
if (msg.msg_namelen != sizeof(struct sockaddr_in6)) if (msg.msg_namelen != sizeof(struct sockaddr_in6))
{ {
...@@ -710,7 +699,6 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size, ...@@ -710,7 +699,6 @@ recv_msg(int sock, uint8_t *recv_buf, int recv_buf_size,
/* Set receiving network interface index */ /* Set receiving network interface index */
struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg); struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
endpoint->interface_index = pktinfo->ipi6_ifindex; endpoint->interface_index = pktinfo->ipi6_ifindex;
/* For a unicast receiving socket, extract the destination address /* For a unicast receiving socket, extract the destination address
* of the UDP packet into the endpoint's addr_local attribute. * of the UDP packet into the endpoint's addr_local attribute.
* This would be used to set the source address of a response that * This would be used to set the source address of a response that
...@@ -983,7 +971,7 @@ send_msg(int sock, struct sockaddr_storage *receiver, oc_message_t *message) ...@@ -983,7 +971,7 @@ send_msg(int sock, struct sockaddr_storage *receiver, oc_message_t *message)
cmsg = CMSG_FIRSTHDR(&msg); cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IPV6; cmsg->cmsg_level = IPPROTO_IPV6;
// cmsg->cmsg_type = IPV6_PKTINFO; cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg); pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
...@@ -1573,14 +1561,12 @@ int oc_connectivity_init(size_t device) ...@@ -1573,14 +1561,12 @@ int oc_connectivity_init(size_t device)
#endif /* OC_SECURITY */ #endif /* OC_SECURITY */
int on = 1; int on = 1;
// TODO
#if 0
if (setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, if (setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) == -1) { sizeof(on)) == -1) {
OC_ERR("setting recvpktinfo option %d\n", errno); OC_ERR("setting recvpktinfo option %d\n", errno);
return -1; return -1;
} }
#endif
if (setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, if (setsockopt(dev->server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof(on)) == -1) sizeof(on)) == -1)
{ {
...@@ -1630,14 +1616,11 @@ int oc_connectivity_init(size_t device) ...@@ -1630,14 +1616,11 @@ int oc_connectivity_init(size_t device)
return -1; return -1;
} }
// TODO
#if 0
if (setsockopt(dev->mcast_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, if (setsockopt(dev->mcast_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) == -1) { sizeof(on)) == -1) {
OC_ERR("setting recvpktinfo option %d\n", errno); OC_ERR("setting recvpktinfo option %d\n", errno);
return -1; return -1;
} }
#endif
if (setsockopt(dev->mcast_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == if (setsockopt(dev->mcast_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ==
-1) -1)
{ {
...@@ -1660,14 +1643,14 @@ int oc_connectivity_init(size_t device) ...@@ -1660,14 +1643,14 @@ int oc_connectivity_init(size_t device)
} }
#ifdef OC_SECURITY #ifdef OC_SECURITY
/*
if (setsockopt(dev->secure_sock, IPPROTO_IPV6, IPV6_PKTINFO, &on, if (setsockopt(dev->secure_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
sizeof(on)) == -1) sizeof(on)) == -1)
{ {
OC_ERR("setting recvpktinfo option %d\n", errno); OC_ERR("setting recvpktinfo option %d\n", errno);
return -1; return -1;
} }
*/
if (setsockopt(dev->secure_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == if (setsockopt(dev->secure_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ==
-1) -1)
{ {
...@@ -1742,17 +1725,18 @@ int oc_connectivity_init(size_t device) ...@@ -1742,17 +1725,18 @@ int oc_connectivity_init(size_t device)
ifchange_initialized = true; ifchange_initialized = true;
} }
pthread_attr_t attr; pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0 ) { if (pthread_attr_init(&attr) != 0)
{
OC_ERR("pthread_attr_init"); OC_ERR("pthread_attr_init");
return -1; return -1;
} }
if (pthread_attr_setstacksize(&attr, 24000) != 0 ) { if (pthread_attr_setstacksize(&attr, 24000) != 0)
{
OC_ERR("pthread_attr_setstacksize"); OC_ERR("pthread_attr_setstacksize");
return -1; return -1;
} }
if (pthread_create(&dev->event_thread, &attr, &network_event_thread, dev) != if (pthread_create(&dev->event_thread, &attr, &network_event_thread, dev) !=
0) 0)
{ {
...@@ -1760,7 +1744,8 @@ int oc_connectivity_init(size_t device) ...@@ -1760,7 +1744,8 @@ int oc_connectivity_init(size_t device)
return -1; return -1;
} }
if (pthread_attr_destroy(&attr) != 0 ) { if (pthread_attr_destroy(&attr) != 0)
{
OC_ERR("pthread_attr_destroy"); OC_ERR("pthread_attr_destroy");
return -1; return -1;
} }
......
...@@ -343,7 +343,7 @@ static void server_main(void *pvParameter) ...@@ -343,7 +343,7 @@ static void server_main(void *pvParameter)
#ifdef OC_IPV4 #ifdef OC_IPV4
xEventGroupWaitBits(wifi_event_group, IPV4_CONNECTED_BIT | IPV6_CONNECTED_BIT, false, true, portMAX_DELAY); xEventGroupWaitBits(wifi_event_group, IPV4_CONNECTED_BIT | IPV6_CONNECTED_BIT, false, true, portMAX_DELAY);
#else #else
xEventGroupWaitBits(wifi_event_group, IPV4_CONNECTED_BIT, false, true, portMAX_DELAY); xEventGroupWaitBits(wifi_event_group, IPV6_CONNECTED_BIT, false, true, portMAX_DELAY);
#endif #endif
#ifdef OC_IPV4 #ifdef OC_IPV4
......
diff --git a/components/coap/port/include/coap_config_posix.h b/components/coap/port/include/coap_config_posix.h
index 3f6b7c605..8b80ff3ac 100644
--- a/components/coap/port/include/coap_config_posix.h
+++ b/components/coap/port/include/coap_config_posix.h
@@ -32,18 +32,11 @@
#define HAVE_STRUCT_CMSGHDR
#define ipi_spec_dst ipi_addr
-struct in6_pktinfo {
- struct in6_addr ipi6_addr; /* src/dst IPv6 address */
- unsigned int ipi6_ifindex; /* send/recv interface index */
-};
#define IN6_IS_ADDR_V4MAPPED(a) \
((((__const uint32_t *) (a))[0] == 0) \
&& (((__const uint32_t *) (a))[1] == 0) \
&& (((__const uint32_t *) (a))[2] == htonl (0xffff)))
-/* As not defined, just need to define is as something innocuous */
-#define IPV6_PKTINFO IPV6_CHECKSUM
-
#define PACKAGE_NAME "libcoap-posix"
#define PACKAGE_VERSION "?"
diff --git a/src/api/sockets.c b/src/api/sockets.c
index e807825b..982c7d4d 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -1235,6 +1235,22 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
msg->msg_flags |= MSG_CTRUNC;
}
#endif /* LWIP_IPV4 */
+ } else if (IP_IS_V6(&buf->toaddr)) {
+#if LWIP_IPV6
+ if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in6_pktinfo))) {
+ struct cmsghdr *chdr = CMSG_FIRSTHDR(msg); /* This will always return a header!! */
+ struct in6_pktinfo *pkti = (struct in6_pktinfo *)CMSG_DATA(chdr);
+ chdr->cmsg_level = IPPROTO_IPV6;
+ chdr->cmsg_type = IPV6_PKTINFO;
+ chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
+ pkti->ipi6_ifindex = buf->p->if_idx;
+ inet6_addr_from_ip6addr(&pkti->ipi6_addr, ip_2_ip6(netbuf_destaddr(buf)));
+ msg->msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
+ wrote_msg = 1;
+ } else {
+ msg->msg_flags |= MSG_CTRUNC;
+ }
+#endif /* LWIP_IPV6 */
}
}
#endif /* LWIP_NETBUF_RECVINFO */
@@ -3789,6 +3805,16 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_
}
break;
#endif /* LWIP_IPV6_MLD */
+#if LWIP_NETBUF_RECVINFO
+ case IPV6_RECVPKTINFO:
+ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_UDP);
+ if (*(const int *)optval) {
+ sock->conn->flags |= NETCONN_FLAG_PKTINFO;
+ } else {
+ sock->conn->flags &= ~NETCONN_FLAG_PKTINFO;
+ }
+ break;
+#endif /* LWIP_NETBUF_RECVINFO */
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n",
s, optname));
diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index f342d8a5..925ef4dd 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -289,6 +289,8 @@ struct linger {
*/
#define IPV6_CHECKSUM 7 /* RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. */
#define IPV6_V6ONLY 27 /* RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. */
+#define IPV6_RECVPKTINFO 49 /* RFC3542: enables the receiving of ancillary data via setsockopt() */
+#define IPV6_PKTINFO 50 /* RFC3542: ancillary data in recvmsg() received */
#endif /* LWIP_IPV6 */
#if LWIP_UDP && LWIP_UDPLITE
@@ -329,6 +331,13 @@ struct in_pktinfo {
};
#endif /* LWIP_IPV4 */
+#if LWIP_IPV6
+struct in6_pktinfo {
+ unsigned int ipi6_ifindex; /* Interface index */
+ struct in6_addr ipi6_addr; /* Destination (from header) address */
+};
+#endif /* LWIP_IPV6 */
+
#if LWIP_IPV6_MLD
/*
* Options and types related to IPv6 multicast membership
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment