From b56ff259dca10d2f51c8e2dd6016138d87a21d5f Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 09:13:42 +0200 Subject: [PATCH 01/28] uIP: remove leftover IPv4 code --- os/contiki-net.h | 2 - os/net/app-layer/coap/coap-uip.c | 4 -- os/net/ipv6/uip-nameserver.c | 6 +-- os/net/ipv6/uip.h | 91 +------------------------------- os/net/ipv6/uiplib.c | 2 - os/net/ipv6/uiplib.h | 4 -- os/net/ipv6/uipopt.h | 5 -- 7 files changed, 2 insertions(+), 112 deletions(-) diff --git a/os/contiki-net.h b/os/contiki-net.h index 1eb50dd8d..6384c51ef 100644 --- a/os/contiki-net.h +++ b/os/contiki-net.h @@ -44,10 +44,8 @@ #include "net/ipv6/uip-nameserver.h" #include "net/routing/routing.h" -#if NETSTACK_CONF_WITH_IPV6 #include "net/ipv6/uip-icmp6.h" #include "net/ipv6/uip-ds6.h" -#endif /* NETSTACK_CONF_WITH_IPV6 */ #include "net/ipv6/resolv.h" diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index e2be92326..1e817e657 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -79,11 +79,7 @@ /* direct access into the buffer */ #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -#if NETSTACK_CONF_WITH_IPV6 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#else -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#endif #ifdef WITH_DTLS static dtls_handler_t cb; diff --git a/os/net/ipv6/uip-nameserver.c b/os/net/ipv6/uip-nameserver.c index 83a1943b0..755b33992 100644 --- a/os/net/ipv6/uip-nameserver.c +++ b/os/net/ipv6/uip-nameserver.c @@ -108,7 +108,7 @@ uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime) * the the eldest ones */ } } - + if(e == NULL) { if((e = memb_alloc(&dnsmemb)) != NULL) { list_add(dns, e); @@ -221,11 +221,7 @@ uip_nameserver_count(void) } return list_length(dns); #else /* UIP_NAMESERVER_POOL_SIZE > 1 */ -#if NETSTACK_CONF_WITH_IPV6 if(uip_is_addr_unspecified(&serveraddr)) { -#else /* NETSTACK_CONF_WITH_IPV6 */ - if(uip_ipaddr_cmp(&serveraddr, &uip_all_zeroes_addr)) { -#endif /* NETSTACK_CONF_WITH_IPV6 */ return 0; } else { return 1; diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 9f5eb159d..e31e80201 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -65,7 +65,7 @@ #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN #define UIP_IPICMPH_LEN (UIP_IPH_LEN + UIP_ICMPH_LEN) /* Size of ICMP + IP header */ #define UIP_LLIPH_LEN (UIP_LLH_LEN + UIP_IPH_LEN) /* Size of L2 + IP header */ -#if NETSTACK_CONF_WITH_IPV6 + /** * The sums below are quite used in ND. When used for uip_buf, we * include link layer length when used for uip_len, we do not, hence @@ -76,8 +76,6 @@ #define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) #define uip_l3_hdr_len (UIP_IPH_LEN + uip_ext_len) #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) -#endif /*NETSTACK_CONF_WITH_IPV6*/ - #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" @@ -100,12 +98,7 @@ typedef union uip_ip6addr_t { uint16_t u16[8]; } uip_ip6addr_t; -#if NETSTACK_CONF_WITH_IPV6 typedef uip_ip6addr_t uip_ipaddr_t; -#else /* NETSTACK_CONF_WITH_IPV6 */ -typedef uip_ip4addr_t uip_ipaddr_t; -#endif /* NETSTACK_CONF_WITH_IPV6 */ - /*---------------------------------------------------------------------------*/ #define UIP_802154_SHORTADDR_LEN 2 @@ -1048,11 +1041,7 @@ struct uip_udp_conn *uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport); (addr1)->u16[1] == (addr2)->u16[1]) #define uip_ip6addr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0) -#if NETSTACK_CONF_WITH_IPV6 #define uip_ipaddr_cmp(addr1, addr2) uip_ip6addr_cmp(addr1, addr2) -#else /* NETSTACK_CONF_WITH_IPV6 */ -#define uip_ipaddr_cmp(addr1, addr2) uip_ip4addr_cmp(addr1, addr2) -#endif /* NETSTACK_CONF_WITH_IPV6 */ /** * Compare two IP addresses with netmasks @@ -1320,18 +1309,11 @@ extern uint16_t uip_urglen, uip_surglen; * This function clears the uIP buffer by reseting the uip_len and * uip_ext_len pointers. */ -#if NETSTACK_CONF_WITH_IPV6 #define uip_clear_buf() { \ uip_len = 0; \ uip_ext_len = 0; \ uipbuf_clear_attr();\ } -#else /*NETSTACK_CONF_WITH_IPV6*/ -#define uip_clear_buf() { \ - uip_len = 0; \ - uipbuf_clear_attr();\ -} -#endif /*NETSTACK_CONF_WITH_IPV6*/ /** * Representation of a uIP TCP connection. @@ -1494,13 +1476,11 @@ struct uip_stats { checksum. */ } udp; /**< UDP statistics. */ #endif /* UIP_UDP */ -#if NETSTACK_CONF_WITH_IPV6 struct { uip_stats_t drop; /**< Number of dropped ND6 packets. */ uip_stats_t recv; /**< Number of recived ND6 packets */ uip_stats_t sent; /**< Number of sent ND6 packets */ } nd6; -#endif /*NETSTACK_CONF_WITH_IPV6*/ }; @@ -1621,7 +1601,6 @@ void uip_process(uint8_t flag); /* The TCP and IP headers. */ struct uip_tcpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 /* IPv6 header. */ uint8_t vtc, tcflow; @@ -1629,18 +1608,6 @@ struct uip_tcpip_hdr { uint8_t len[2]; uint8_t proto, ttl; uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPv4 header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ /* TCP header. */ uint16_t srcport, @@ -1657,7 +1624,6 @@ struct uip_tcpip_hdr { /* The ICMP and IP headers. */ struct uip_icmpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 /* IPv6 header. */ uint8_t vtc, tcf; @@ -1665,32 +1631,15 @@ struct uip_icmpip_hdr { uint8_t len[2]; uint8_t proto, ttl; uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPv4 header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ /* ICMP header. */ uint8_t type, icode; uint16_t icmpchksum; -#if !NETSTACK_CONF_WITH_IPV6 - uint16_t id, seqno; - uint8_t payload[1]; -#endif /* !NETSTACK_CONF_WITH_IPV6 */ }; /* The UDP and IP headers. */ struct uip_udpip_hdr { -#if NETSTACK_CONF_WITH_IPV6 /* IPv6 header. */ uint8_t vtc, tcf; @@ -1698,18 +1647,6 @@ struct uip_udpip_hdr { uint8_t len[2]; uint8_t proto, ttl; uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IP header. */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ /* UDP header. */ uint16_t srcport, @@ -1725,7 +1662,6 @@ struct uip_udpip_hdr { */ /* The IP header */ struct uip_ip_hdr { -#if NETSTACK_CONF_WITH_IPV6 /* IPV6 header */ uint8_t vtc; uint8_t tcflow; @@ -1733,18 +1669,6 @@ struct uip_ip_hdr { uint8_t len[2]; uint8_t proto, ttl; uip_ip6addr_t srcipaddr, destipaddr; -#else /* NETSTACK_CONF_WITH_IPV6 */ - /* IPV4 header */ - uint8_t vhl, - tos, - len[2], - ipid[2], - ipoffset[2], - ttl, - proto; - uint16_t ipchksum; - uip_ipaddr_t srcipaddr, destipaddr; -#endif /* NETSTACK_CONF_WITH_IPV6 */ }; @@ -1860,9 +1784,6 @@ struct uip_tcp_hdr { struct uip_icmp_hdr { uint8_t type, icode; uint16_t icmpchksum; -#if !NETSTACK_CONF_WITH_IPV6 - uint16_t id, seqno; -#endif /* !NETSTACK_CONF_WITH_IPV6 */ }; @@ -1898,7 +1819,6 @@ struct uip_udp_hdr { #define UIP_PROTO_ICMP6 58 -#if NETSTACK_CONF_WITH_IPV6 /** @{ */ /** \brief extension headers types */ #define UIP_PROTO_HBHO 0 @@ -1937,9 +1857,6 @@ struct uip_udp_hdr { /** @} */ -#endif /* NETSTACK_CONF_WITH_IPV6 */ - - #if UIP_FIXEDADDR extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr; #else /* UIP_FIXEDADDR */ @@ -1954,10 +1871,6 @@ extern const uip_lladdr_t uip_lladdr; extern uip_lladdr_t uip_lladdr; #endif - - - -#if NETSTACK_CONF_WITH_IPV6 /** Length of the link local prefix */ #define UIP_LLPREF_LEN 10 @@ -2174,8 +2087,6 @@ extern uip_lladdr_t uip_lladdr; (((a)->u8[14]) == ((b)->u8[14])) && \ (((a)->u8[15]) == ((b)->u8[15]))) -#endif /*NETSTACK_CONF_WITH_IPV6*/ - /** * A non-error message that indicates that a packet should be * processed locally. diff --git a/os/net/ipv6/uiplib.c b/os/net/ipv6/uiplib.c index a6d8b4f0c..e4a7a49b7 100644 --- a/os/net/ipv6/uiplib.c +++ b/os/net/ipv6/uiplib.c @@ -56,7 +56,6 @@ #define LOG_LEVEL LOG_LEVEL_NONE /*---------------------------------------------------------------------------*/ -#if NETSTACK_CONF_WITH_IPV6 int uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr) { @@ -118,7 +117,6 @@ uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr) return 1; } -#endif /* NETSTACK_CONF_WITH_IPV6 */ /*---------------------------------------------------------------------------*/ /* Parse a IPv4-address from a string. Returns the number of characters read * for the address. */ diff --git a/os/net/ipv6/uiplib.h b/os/net/ipv6/uiplib.h index b30a1bcb5..aaf91282f 100644 --- a/os/net/ipv6/uiplib.h +++ b/os/net/ipv6/uiplib.h @@ -68,11 +68,7 @@ * \retval 0 If the IP address could not be parsed. * \retval Non-zero If the IP address was parsed. */ -#if NETSTACK_CONF_WITH_IPV6 #define uiplib_ipaddrconv uiplib_ip6addrconv -#else /* NETSTACK_CONF_WITH_IPV6 */ -#define uiplib_ipaddrconv uiplib_ip4addrconv -#endif /* NETSTACK_CONF_WITH_IPV6 */ int uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *addr); int uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *addr); diff --git a/os/net/ipv6/uipopt.h b/os/net/ipv6/uipopt.h index 15c79e20a..f01689f4f 100644 --- a/os/net/ipv6/uipopt.h +++ b/os/net/ipv6/uipopt.h @@ -248,11 +248,6 @@ void uip_log(char *msg); /** The maximum transmission unit at the IP Layer*/ #define UIP_LINK_MTU 1280 -#ifndef NETSTACK_CONF_WITH_IPV6 -/** Do we use IPv6 or not (default: no) */ -#define NETSTACK_CONF_WITH_IPV6 0 -#endif - #ifndef UIP_CONF_IPV6_QUEUE_PKT /** Do we do per %neighbor queuing during address resolution (default: no) */ #define UIP_CONF_IPV6_QUEUE_PKT 0 From ee5b722c6590c672519a24e152d121cb310e420c Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 09:17:41 +0200 Subject: [PATCH 02/28] uip.h: removed unused define --- os/net/ipv6/uip.h | 1 - 1 file changed, 1 deletion(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index e31e80201..5e72ba844 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -74,7 +74,6 @@ */ #define uip_l2_l3_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len) #define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) -#define uip_l3_hdr_len (UIP_IPH_LEN + uip_ext_len) #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) #include "net/ipv6/uipopt.h" From caa02ae51e2e81d479f613f6bd5463b51f2a2db2 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 09:24:52 +0200 Subject: [PATCH 03/28] Define UIP_IP_BUF globally in uip.h --- examples/libs/ipv6-hooks/ipv6-hooks.c | 2 -- examples/multicast/sink.c | 2 -- .../jn516x/tsch/simple-sensor-network/node/node.c | 2 -- os/net/app-layer/coap/coap-uip.c | 1 - os/net/ipv6/multicast/esmrf.c | 1 - os/net/ipv6/multicast/roll-tm.c | 1 - os/net/ipv6/multicast/smrf.c | 4 ---- os/net/ipv6/sicslowpan.c | 2 -- os/net/ipv6/simple-udp.c | 12 ++++++------ os/net/ipv6/tcpip.c | 1 - os/net/ipv6/udp-socket.c | 11 +++++------ os/net/ipv6/uip-icmp6.c | 1 - os/net/ipv6/uip-nd6.c | 1 - os/net/ipv6/uip.h | 5 +++++ os/net/ipv6/uip6.c | 1 - os/net/routing/rpl-classic/rpl-ext-header.c | 1 - os/net/routing/rpl-classic/rpl-icmp6.c | 1 - os/net/routing/rpl-classic/rpl-nbr-policy.c | 1 - os/net/routing/rpl-lite/rpl-ext-header.c | 1 - os/net/routing/rpl-lite/rpl-icmp6.c | 1 - os/net/routing/rpl-lite/rpl-nbr-policy.c | 1 - os/services/ip64/ip64-eth-interface.c | 2 -- os/services/ip64/ip64-slip-interface.c | 8 +++----- os/services/rpl-border-router/embedded/slip-bridge.c | 2 -- tools/sky/uip6-bridge/fakeuip.c | 2 -- tools/sky/uip6-bridge/sicslow_ethernet.c | 1 - tools/sky/uip6-bridge/uip6-bridge-tap.c | 2 -- 27 files changed, 19 insertions(+), 51 deletions(-) diff --git a/examples/libs/ipv6-hooks/ipv6-hooks.c b/examples/libs/ipv6-hooks/ipv6-hooks.c index d3ca8861c..8d8561049 100644 --- a/examples/libs/ipv6-hooks/ipv6-hooks.c +++ b/examples/libs/ipv6-hooks/ipv6-hooks.c @@ -10,8 +10,6 @@ #define LOG_MODULE "App" #define LOG_LEVEL LOG_LEVEL_INFO -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - /*---------------------------------------------------------------------------*/ PROCESS(ipv6_hooks_process, "IPv6 Hooks"); AUTOSTART_PROCESSES(&ipv6_hooks_process); diff --git a/examples/multicast/sink.c b/examples/multicast/sink.c index 34ad6009d..49180b69a 100644 --- a/examples/multicast/sink.c +++ b/examples/multicast/sink.c @@ -55,8 +55,6 @@ static struct uip_udp_conn *sink_conn; static uint16_t count; -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #if !NETSTACK_CONF_WITH_IPV6 || !UIP_CONF_ROUTER || !UIP_IPV6_MULTICAST || !UIP_CONF_IPV6_RPL #error "This example can not work with the current contiki configuration" #error "Check the values of: NETSTACK_CONF_WITH_IPV6, UIP_CONF_ROUTER, UIP_CONF_IPV6_RPL" diff --git a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c index 251b0d135..b3ef311fd 100644 --- a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c +++ b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c @@ -50,8 +50,6 @@ #define DEBUG DEBUG_PRINT #include "net/ipv6/uip-debug.h" -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define INTERVAL (10) #define BLINK_TIME (CLOCK_SECOND/4) diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index 1e817e657..3c9347bc6 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -78,7 +78,6 @@ #define SERVER_LISTEN_SECURE_PORT UIP_HTONS(COAP_DEFAULT_SECURE_PORT) /* direct access into the buffer */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #ifdef WITH_DTLS diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index 5b6f1b863..edbc32b16 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -105,7 +105,6 @@ static uip_ipaddr_t des_ip; /*---------------------------------------------------------------------------*/ /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 1d19512d6..20a681fc5 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -461,7 +461,6 @@ static uint16_t last_seq; #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_EXT_BUF_NEXT ((uint8_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + HBHO_TOTAL_LEN]) #define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + 2]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) extern uint16_t uip_slen; diff --git a/os/net/ipv6/multicast/smrf.c b/os/net/ipv6/multicast/smrf.c index 86a053648..bb23b060b 100644 --- a/os/net/ipv6/multicast/smrf.c +++ b/os/net/ipv6/multicast/smrf.c @@ -77,10 +77,6 @@ static uip_buf_t mcast_buf; static uint8_t fwd_delay; static uint8_t fwd_spread; /*---------------------------------------------------------------------------*/ -/* uIPv6 Pointers */ -/*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -/*---------------------------------------------------------------------------*/ static void mcast_fwd(void *p) { diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index 9e4e3a8b4..b5c23a3be 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -121,8 +121,6 @@ #define SICSLOWPAN_UDP_BUF(buf) ((struct uip_udp_hdr *)&buf[UIP_IPH_LEN]) #define SICSLOWPAN_IPPAYLOAD_BUF(buf) (&buf[UIP_IPH_LEN]) - -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_UDP_BUF(p) ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN + p]) #define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLIPH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN]) diff --git a/os/net/ipv6/simple-udp.c b/os/net/ipv6/simple-udp.c index c98107c99..49aab96dc 100644 --- a/os/net/ipv6/simple-udp.c +++ b/os/net/ipv6/simple-udp.c @@ -52,7 +52,7 @@ PROCESS(simple_udp_process, "Simple UDP process"); static uint8_t started = 0; static uint8_t databuffer[UIP_BUFSIZE]; -#define UIP_IP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) +#define UIP_UDPIP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) /*---------------------------------------------------------------------------*/ static void @@ -134,7 +134,7 @@ PROCESS_THREAD(simple_udp_process, ev, data) { struct simple_udp_connection *c; PROCESS_BEGIN(); - + while(1) { PROCESS_WAIT_EVENT(); if(ev == tcpip_event) { @@ -164,10 +164,10 @@ PROCESS_THREAD(simple_udp_process, ev, data) if(c->receive_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->client_process); c->receive_callback(c, - &(UIP_IP_BUF->srcipaddr), - UIP_HTONS(UIP_IP_BUF->srcport), - &(UIP_IP_BUF->destipaddr), - UIP_HTONS(UIP_IP_BUF->destport), + &(UIP_UDPIP_BUF->srcipaddr), + UIP_HTONS(UIP_UDPIP_BUF->srcport), + &(UIP_UDPIP_BUF->destipaddr), + UIP_HTONS(UIP_UDPIP_BUF->destport), databuffer, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/tcpip.c b/os/net/ipv6/tcpip.c index 8d546c1d7..740054010 100644 --- a/os/net/ipv6/tcpip.c +++ b/os/net/ipv6/tcpip.c @@ -56,7 +56,6 @@ #define LOG_LEVEL LOG_LEVEL_TCPIP #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN + uip_ext_len]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_TCP_BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) #ifdef UIP_FALLBACK_INTERFACE diff --git a/os/net/ipv6/udp-socket.c b/os/net/ipv6/udp-socket.c index 700306ca1..11126fedc 100644 --- a/os/net/ipv6/udp-socket.c +++ b/os/net/ipv6/udp-socket.c @@ -38,8 +38,7 @@ PROCESS(udp_socket_process, "UDP socket process"); static uint8_t buf[UIP_BUFSIZE]; -#define UIP_IP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - +#define UIP_UDPIP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) /*---------------------------------------------------------------------------*/ static void @@ -181,10 +180,10 @@ PROCESS_THREAD(udp_socket_process, ev, data) if(c->input_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->p); c->input_callback(c, c->ptr, - &(UIP_IP_BUF->srcipaddr), - UIP_HTONS(UIP_IP_BUF->srcport), - &(UIP_IP_BUF->destipaddr), - UIP_HTONS(UIP_IP_BUF->destport), + &(UIP_UDPIP_BUF->srcipaddr), + UIP_HTONS(UIP_UDPIP_BUF->srcport), + &(UIP_UDPIP_BUF->destipaddr), + UIP_HTONS(UIP_UDPIP_BUF->destport), buf, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 707a13309..13d03a859 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -53,7 +53,6 @@ #define LOG_MODULE "ICMPv6" #define LOG_LEVEL LOG_LEVEL_IPV6 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 63ae46b47..d3e50bdcf 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -94,7 +94,6 @@ * value of these length variables */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) /**< Pointer to IP header */ #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) /**< Pointer to ICMP header*/ /**@{ Pointers to messages just after icmp header */ #define UIP_ND6_RS_BUF ((uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len]) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 5e72ba844..fa3bdbd98 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -76,6 +76,11 @@ #define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) +/** + * Direct access to IPv6 header + */ +#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) + #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" #include "net/linkaddr.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index dfbec52b7..5b86de266 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -142,7 +142,6 @@ uint8_t uip_ext_opt_offset = 0; * @{ */ #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0]) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 8488d5ac6..18ebfa3a8 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -61,7 +61,6 @@ #include /*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) diff --git a/os/net/routing/rpl-classic/rpl-icmp6.c b/os/net/routing/rpl-classic/rpl-icmp6.c index a246e576e..ed0bde02a 100644 --- a/os/net/routing/rpl-classic/rpl-icmp6.c +++ b/os/net/routing/rpl-classic/rpl-icmp6.c @@ -70,7 +70,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) /*---------------------------------------------------------------------------*/ diff --git a/os/net/routing/rpl-classic/rpl-nbr-policy.c b/os/net/routing/rpl-classic/rpl-nbr-policy.c index 344840d0e..f6b26bae2 100644 --- a/os/net/routing/rpl-classic/rpl-nbr-policy.c +++ b/os/net/routing/rpl-classic/rpl-nbr-policy.c @@ -65,7 +65,6 @@ * neighbors and are not only MAC neighbors. */ #define MAX_CHILDREN (NBR_TABLE_MAX_NEIGHBORS - 2) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) static int num_parents; /* any node that are possible parents */ static int num_children; /* all children that we have as nexthop */ diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index af67cdaff..414a93104 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -54,7 +54,6 @@ #define LOG_LEVEL LOG_LEVEL_RPL /*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) diff --git a/os/net/routing/rpl-lite/rpl-icmp6.c b/os/net/routing/rpl-lite/rpl-icmp6.c index be5ea2948..f369b34c7 100644 --- a/os/net/routing/rpl-lite/rpl-icmp6.c +++ b/os/net/routing/rpl-lite/rpl-icmp6.c @@ -62,7 +62,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) diff --git a/os/net/routing/rpl-lite/rpl-nbr-policy.c b/os/net/routing/rpl-lite/rpl-nbr-policy.c index 9752b1446..0c542f780 100644 --- a/os/net/routing/rpl-lite/rpl-nbr-policy.c +++ b/os/net/routing/rpl-lite/rpl-nbr-policy.c @@ -59,7 +59,6 @@ * NOTE: this policy assumes that all neighbors end up being IPv6 * neighbors and are not only MAC neighbors. */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) static int num_parents; /* all nodes that are possible parents */ static int num_free; diff --git a/os/services/ip64/ip64-eth-interface.c b/os/services/ip64/ip64-eth-interface.c index 1dcaeb5ad..d70b7576a 100644 --- a/os/services/ip64/ip64-eth-interface.c +++ b/os/services/ip64/ip64-eth-interface.c @@ -39,8 +39,6 @@ #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" #define printf(...) diff --git a/os/services/ip64/ip64-slip-interface.c b/os/services/ip64/ip64-slip-interface.c index 10422f2c7..665a342d9 100644 --- a/os/services/ip64/ip64-slip-interface.c +++ b/os/services/ip64/ip64-slip-interface.c @@ -38,8 +38,6 @@ #include #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - #define DEBUG DEBUG_NONE #include "net/ipv6/uip-debug.h" @@ -88,12 +86,12 @@ input_callback(void) } uip_clear_buf(); } else { - + /* Save the last sender received over SLIP to avoid bouncing the packet back if no route is found */ uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr); - - uint16_t len = ip64_4to6(&uip_buf[UIP_LLH_LEN], uip_len, + + uint16_t len = ip64_4to6(&uip_buf[UIP_LLH_LEN], uip_len, ip64_packet_buffer); if(len > 0) { memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len); diff --git a/os/services/rpl-border-router/embedded/slip-bridge.c b/os/services/rpl-border-router/embedded/slip-bridge.c index d1f97339d..ff980ea69 100644 --- a/os/services/rpl-border-router/embedded/slip-bridge.c +++ b/os/services/rpl-border-router/embedded/slip-bridge.c @@ -43,8 +43,6 @@ #include "dev/slip.h" #include /*---------------------------------------------------------------------------*/ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) -/*---------------------------------------------------------------------------*/ /* Log configuration */ #include "sys/log.h" #define LOG_MODULE "SLIP" diff --git a/tools/sky/uip6-bridge/fakeuip.c b/tools/sky/uip6-bridge/fakeuip.c index 84f6440ce..86658429a 100644 --- a/tools/sky/uip6-bridge/fakeuip.c +++ b/tools/sky/uip6-bridge/fakeuip.c @@ -8,8 +8,6 @@ #include "net/ipv6/uip-ds6.h" #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - uip_buf_t uip_aligned_buf; uint16_t uip_len; diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 7217e3229..317c7abe8 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -155,7 +155,6 @@ //#define PRINTF printf #define PRINTF(...) -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) #define ETHBUF(x) ((struct uip_eth_hdr *)x) //For little endian, such as our friend mr. AVR diff --git a/tools/sky/uip6-bridge/uip6-bridge-tap.c b/tools/sky/uip6-bridge/uip6-bridge-tap.c index 694499b6d..19ce22a6f 100644 --- a/tools/sky/uip6-bridge/uip6-bridge-tap.c +++ b/tools/sky/uip6-bridge/uip6-bridge-tap.c @@ -48,8 +48,6 @@ #include #include -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) - PROCESS(uip6_bridge, "IPv6/6lowpan TAP bridge"); PROCESS(tcpip_process, "tcpip dummy"); AUTOSTART_PROCESSES(&uip6_bridge); From 1bd9ac781b8e869b5862348583148eb7efaba5a0 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 09:42:24 +0200 Subject: [PATCH 04/28] Define ICMP, UDP, and TCP buffer pointers globally in uip.h --- os/net/ipv6/multicast/esmrf.c | 6 ------ os/net/ipv6/multicast/roll-tm.c | 3 --- os/net/ipv6/resolv.c | 4 +--- os/net/ipv6/sicslowpan.c | 16 +++++++--------- os/net/ipv6/tcpip.c | 3 --- os/net/ipv6/uip-icmp6.c | 1 - os/net/ipv6/uip-nd6.c | 1 - os/net/ipv6/uip.h | 10 +++++++++- os/net/ipv6/uip6.c | 3 --- os/net/routing/rpl-classic/rpl-icmp6.c | 2 -- os/net/routing/rpl-lite/rpl-icmp6.c | 3 --- tools/sky/uip6-bridge/sicslow_ethernet.c | 1 - 12 files changed, 17 insertions(+), 36 deletions(-) diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index edbc32b16..d72d99fad 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -103,12 +103,6 @@ static struct uip_udp_conn *c; static uip_ipaddr_t src_ip; static uip_ipaddr_t des_ip; /*---------------------------------------------------------------------------*/ -/* uIPv6 Pointers */ -/*---------------------------------------------------------------------------*/ -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -/*---------------------------------------------------------------------------*/ /* Local function prototypes */ /*---------------------------------------------------------------------------*/ static void icmp_input(void); diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 20a681fc5..a8dee46a6 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -457,12 +457,9 @@ static uint16_t last_seq; /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ #define UIP_DATA_BUF ((uint8_t *)&uip_buf[uip_l2_l3_hdr_len + UIP_UDPH_LEN]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_EXT_BUF_NEXT ((uint8_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + HBHO_TOTAL_LEN]) #define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + 2]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) extern uint16_t uip_slen; /*---------------------------------------------------------------------------*/ /* Local function prototypes */ diff --git a/os/net/ipv6/resolv.c b/os/net/ipv6/resolv.c index 2bcd3983b..29af2b4cf 100644 --- a/os/net/ipv6/resolv.c +++ b/os/net/ipv6/resolv.c @@ -116,8 +116,6 @@ int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n); #endif /* __SDCC */ -#define UIP_UDP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - /* If RESOLV_CONF_SUPPORTS_MDNS is set, then queries * for domain names in the local TLD will use mDNS as * described by draft-cheshire-dnsext-multicastdns. @@ -837,7 +835,7 @@ newdata(void) } else { uip_udp_packet_sendto(resolv_conn, uip_appdata, mdns_prep_host_announce_packet(), - &UIP_UDP_BUF->srcipaddr, + &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport); } return; diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index b5c23a3be..2a1a9f34f 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -121,10 +121,8 @@ #define SICSLOWPAN_UDP_BUF(buf) ((struct uip_udp_hdr *)&buf[UIP_IPH_LEN]) #define SICSLOWPAN_IPPAYLOAD_BUF(buf) (&buf[UIP_IPH_LEN]) -#define UIP_UDP_BUF(p) ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN + p]) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLIPH_LEN]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN]) -#define UIP_IPPAYLOAD_BUF(pos) (&uip_buf[UIP_LLIPH_LEN + pos]) +#define UIP_IPPAYLOAD_BUF_POS(pos) (&uip_buf[UIP_LLIPH_LEN + (pos)]) +#define UIP_UDP_BUF_POS(pos) ((struct uip_udp_hdr *)UIP_IPPAYLOAD_BUF_POS(pos)) /** @} */ @@ -464,9 +462,9 @@ set_packet_attrs(void) /* assign values to the channel attribute (port or type + code) */ if(UIP_IP_BUF->proto == UIP_PROTO_UDP) { - c = UIP_UDP_BUF(0)->srcport; - if(UIP_UDP_BUF(0)->destport < c) { - c = UIP_UDP_BUF(0)->destport; + c = UIP_UDP_BUF_POS(0)->srcport; + if(UIP_UDP_BUF_POS(0)->destport < c) { + c = UIP_UDP_BUF_POS(0)->destport; } } else if(UIP_IP_BUF->proto == UIP_PROTO_TCP) { c = UIP_TCP_BUF->srcport; @@ -917,7 +915,7 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) /* Handle the header here! */ { struct uip_ext_hdr *ext_hdr = - (struct uip_ext_hdr *) UIP_IPPAYLOAD_BUF(ext_hdr_len); + (struct uip_ext_hdr *) UIP_IPPAYLOAD_BUF_POS(ext_hdr_len); int len; proto = proto == -1 ? SICSLOWPAN_NHC_ETX_HDR_DESTO : proto; /* Len is defined to be in octets from the length byte */ @@ -956,7 +954,7 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) case UIP_PROTO_UDP: /* allocate a byte for the next header posision as UDP has no next */ hc06_ptr++; - udp_buf = UIP_UDP_BUF(ext_hdr_len); + udp_buf = UIP_UDP_BUF_POS(ext_hdr_len); LOG_DBG("compression: inlined UDP ports on send side: %x, %x\n", UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport)); /* Mask out the last 4 bits can be used as a mask */ diff --git a/os/net/ipv6/tcpip.c b/os/net/ipv6/tcpip.c index 740054010..b7a10de9a 100644 --- a/os/net/ipv6/tcpip.c +++ b/os/net/ipv6/tcpip.c @@ -55,9 +55,6 @@ #define LOG_MODULE "TCP/IP" #define LOG_LEVEL LOG_LEVEL_TCPIP -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLIPH_LEN + uip_ext_len]) -#define UIP_TCP_BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) - #ifdef UIP_FALLBACK_INTERFACE extern struct uip_fallback_interface UIP_FALLBACK_INTERFACE; #endif diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 13d03a859..193c02aa4 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -53,7 +53,6 @@ #define LOG_MODULE "ICMPv6" #define LOG_LEVEL LOG_LEVEL_IPV6 -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN]) diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index d3e50bdcf..b2a17ff1d 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -94,7 +94,6 @@ * value of these length variables */ -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) /**< Pointer to ICMP header*/ /**@{ Pointers to messages just after icmp header */ #define UIP_ND6_RS_BUF ((uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len]) #define UIP_ND6_RA_BUF ((uip_nd6_ra *)&uip_buf[uip_l2_l3_icmp_hdr_len]) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index fa3bdbd98..556a3e4be 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -79,7 +79,15 @@ /** * Direct access to IPv6 header */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) +#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) + +/** + * Direct access to ICMP, UDP, and TCP headers and payload + */ +#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) +#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) +#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 5b86de266..0a6153a2a 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -142,9 +142,6 @@ uint8_t uip_ext_opt_offset = 0; * @{ */ #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ROUTING_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_FRAG_BUF ((struct uip_frag_hdr *)&uip_buf[uip_l2_l3_hdr_len]) diff --git a/os/net/routing/rpl-classic/rpl-icmp6.c b/os/net/routing/rpl-classic/rpl-icmp6.c index ed0bde02a..5230ab4e4 100644 --- a/os/net/routing/rpl-classic/rpl-icmp6.c +++ b/os/net/routing/rpl-classic/rpl-icmp6.c @@ -70,8 +70,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) /*---------------------------------------------------------------------------*/ static void dis_input(void); static void dio_input(void); diff --git a/os/net/routing/rpl-lite/rpl-icmp6.c b/os/net/routing/rpl-lite/rpl-icmp6.c index f369b34c7..260f9b168 100644 --- a/os/net/routing/rpl-lite/rpl-icmp6.c +++ b/os/net/routing/rpl-lite/rpl-icmp6.c @@ -62,9 +62,6 @@ #define RPL_DIO_MOP_MASK 0x38 #define RPL_DIO_PREFERENCE_MASK 0x07 -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) - /*---------------------------------------------------------------------------*/ static void dis_input(void); static void dio_input(void); diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 317c7abe8..0b241d71b 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -379,7 +379,6 @@ typedef struct { uint8_t data[16]; } icmp_opts_t; -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_ICMP_OPTS(x) ((icmp_opts_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + x]) void slide(uint8_t * data, uint8_t length, int16_t slide); From 2f9b60215e5b4de40126943acd661ad98ad82610 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 11:22:05 +0200 Subject: [PATCH 05/28] Removing tcpip and udpip data strcutures, unsafe in cases with IPv6 extension headers --- arch/platform/jn516x/slip.c | 1 - os/net/ipv6/simple-udp.c | 10 +++--- os/net/ipv6/udp-socket.c | 10 +++--- os/net/ipv6/uip.h | 56 -------------------------------- os/net/ipv6/uip6.c | 2 +- tools/sky/uip6-bridge/dev/slip.c | 1 - 6 files changed, 9 insertions(+), 71 deletions(-) diff --git a/arch/platform/jn516x/slip.c b/arch/platform/jn516x/slip.c index 173a14d60..572d9f71c 100644 --- a/arch/platform/jn516x/slip.c +++ b/arch/platform/jn516x/slip.c @@ -46,7 +46,6 @@ #include #include "net/ipv6/uip.h" -#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) #include "dev/slip.h" diff --git a/os/net/ipv6/simple-udp.c b/os/net/ipv6/simple-udp.c index 49aab96dc..08986d827 100644 --- a/os/net/ipv6/simple-udp.c +++ b/os/net/ipv6/simple-udp.c @@ -52,8 +52,6 @@ PROCESS(simple_udp_process, "Simple UDP process"); static uint8_t started = 0; static uint8_t databuffer[UIP_BUFSIZE]; -#define UIP_UDPIP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - /*---------------------------------------------------------------------------*/ static void init_simple_udp(void) @@ -164,10 +162,10 @@ PROCESS_THREAD(simple_udp_process, ev, data) if(c->receive_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->client_process); c->receive_callback(c, - &(UIP_UDPIP_BUF->srcipaddr), - UIP_HTONS(UIP_UDPIP_BUF->srcport), - &(UIP_UDPIP_BUF->destipaddr), - UIP_HTONS(UIP_UDPIP_BUF->destport), + &(UIP_IP_BUF->srcipaddr), + UIP_HTONS(UIP_UDP_BUF->srcport), + &(UIP_IP_BUF->destipaddr), + UIP_HTONS(UIP_UDP_BUF->destport), databuffer, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/udp-socket.c b/os/net/ipv6/udp-socket.c index 11126fedc..f104a6b6c 100644 --- a/os/net/ipv6/udp-socket.c +++ b/os/net/ipv6/udp-socket.c @@ -38,8 +38,6 @@ PROCESS(udp_socket_process, "UDP socket process"); static uint8_t buf[UIP_BUFSIZE]; -#define UIP_UDPIP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) - /*---------------------------------------------------------------------------*/ static void init(void) @@ -180,10 +178,10 @@ PROCESS_THREAD(udp_socket_process, ev, data) if(c->input_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->p); c->input_callback(c, c->ptr, - &(UIP_UDPIP_BUF->srcipaddr), - UIP_HTONS(UIP_UDPIP_BUF->srcport), - &(UIP_UDPIP_BUF->destipaddr), - UIP_HTONS(UIP_UDPIP_BUF->destport), + &(UIP_IP_BUF->srcipaddr), + UIP_HTONS(UIP_UDP_BUF->srcport), + &(UIP_IP_BUF->destipaddr), + UIP_HTONS(UIP_UDP_BUF->destport), buf, uip_datalen()); PROCESS_CONTEXT_END(); } diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 556a3e4be..255eeb1a5 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -1611,62 +1611,6 @@ void uip_process(uint8_t flag); #define UIP_STOPPED 16 -/* The TCP and IP headers. */ -struct uip_tcpip_hdr { - /* IPv6 header. */ - uint8_t vtc, - tcflow; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; - - /* TCP header. */ - uint16_t srcport, - destport; - uint8_t seqno[4], - ackno[4], - tcpoffset, - flags, - wnd[2]; - uint16_t tcpchksum; - uint8_t urgp[2]; - uint8_t optdata[4]; -}; - -/* The ICMP and IP headers. */ -struct uip_icmpip_hdr { - /* IPv6 header. */ - uint8_t vtc, - tcf; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; - - /* ICMP header. */ - uint8_t type, icode; - uint16_t icmpchksum; -}; - - -/* The UDP and IP headers. */ -struct uip_udpip_hdr { - /* IPv6 header. */ - uint8_t vtc, - tcf; - uint16_t flow; - uint8_t len[2]; - uint8_t proto, ttl; - uip_ip6addr_t srcipaddr, destipaddr; - - /* UDP header. */ - uint16_t srcport, - destport; - uint16_t udplen; - uint16_t udpchksum; -}; - /* * In IPv6 the length of the L3 headers before the transport header is * not fixed, due to the possibility to include extension option headers diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 0a6153a2a..615a658dc 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -141,7 +141,7 @@ uint8_t uip_ext_opt_offset = 0; * \name Buffer defines * @{ */ -#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0]) +#define FBUF ((struct uip_ip_hdr *)&uip_reassbuf[0]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_ROUTING_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_FRAG_BUF ((struct uip_frag_hdr *)&uip_buf[uip_l2_l3_hdr_len]) diff --git a/tools/sky/uip6-bridge/dev/slip.c b/tools/sky/uip6-bridge/dev/slip.c index e8062e515..7cde6437d 100644 --- a/tools/sky/uip6-bridge/dev/slip.c +++ b/tools/sky/uip6-bridge/dev/slip.c @@ -38,7 +38,6 @@ #include "contiki.h" #include "net/ipv6/uip.h" -#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) #include "dev/slip.h" From 86e9790e6e0181e66605deb5f814e9013331af67 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 11:43:34 +0200 Subject: [PATCH 06/28] Minor improvement to uip.h --- os/net/ipv6/uip.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 255eeb1a5..35f0d9cfd 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -84,10 +84,11 @@ /** * Direct access to ICMP, UDP, and TCP headers and payload */ -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) +#define UIP_IP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_hdr_len]) +#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD) #define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) +#define UIP_UDP_BUF ((struct uip_udp_hdr *)UIP_IP_PAYLOAD) +#define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" From 59852d33f224ba6bd17d0b1a267e0fc90efe4ec7 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 11:57:56 +0200 Subject: [PATCH 07/28] Use global uip.h buffer definitions in a number of other modules --- arch/cpu/msp430/uip-ipchksum.c | 2 +- os/net/app-layer/coap/coap-uip.c | 3 --- os/net/ipv6/uip-icmp6.c | 2 +- os/net/ipv6/uip-nd6.c | 8 ++++---- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/cpu/msp430/uip-ipchksum.c b/arch/cpu/msp430/uip-ipchksum.c index e4cb308d0..442c154dc 100644 --- a/arch/cpu/msp430/uip-ipchksum.c +++ b/arch/cpu/msp430/uip-ipchksum.c @@ -51,7 +51,7 @@ uint16_t uip_ipchksum(void) { /* Assumes proper alignement of uip_buf. */ - uint16_t *p = (uint16_t *)&uip_buf[UIP_LLH_LEN]; + uint16_t *p = (uint16_t *)UIP_IP_BUF; register uint16_t sum; sum = p[0]; diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index 3c9347bc6..2435b9d2d 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -77,9 +77,6 @@ #define SERVER_LISTEN_PORT UIP_HTONS(COAP_DEFAULT_PORT) #define SERVER_LISTEN_SECURE_PORT UIP_HTONS(COAP_DEFAULT_SECURE_PORT) -/* direct access into the buffer */ -#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len]) - #ifdef WITH_DTLS static dtls_handler_t cb; static dtls_context_t *dtls_context = NULL; diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 193c02aa4..de9cc2394 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -53,7 +53,7 @@ #define LOG_MODULE "ICMPv6" #define LOG_LEVEL LOG_LEVEL_IPV6 -#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)UIP_ICMP_PAYLOAD) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN]) diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index b2a17ff1d..446ad8346 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -95,10 +95,10 @@ */ /**@{ Pointers to messages just after icmp header */ -#define UIP_ND6_RS_BUF ((uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_RA_BUF ((uip_nd6_ra *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_NS_BUF ((uip_nd6_ns *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_ND6_NA_BUF ((uip_nd6_na *)&uip_buf[uip_l2_l3_icmp_hdr_len]) +#define UIP_ND6_RS_BUF ((uip_nd6_rs *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_RA_BUF ((uip_nd6_ra *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_NS_BUF ((uip_nd6_ns *)UIP_ICMP_PAYLOAD) +#define UIP_ND6_NA_BUF ((uip_nd6_na *)UIP_ICMP_PAYLOAD) /** @} */ /** Pointer to ND option */ #define UIP_ND6_OPT_HDR_BUF ((uip_nd6_opt_hdr *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) From a528395eb28c90731298f5ddb762f5547b5dfd4b Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 11:58:51 +0200 Subject: [PATCH 08/28] roll-tm.c: removing unused define --- os/net/ipv6/multicast/roll-tm.c | 1 - 1 file changed, 1 deletion(-) diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index a8dee46a6..3e1d14b5b 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -456,7 +456,6 @@ static uint16_t last_seq; /*---------------------------------------------------------------------------*/ /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ -#define UIP_DATA_BUF ((uint8_t *)&uip_buf[uip_l2_l3_hdr_len + UIP_UDPH_LEN]) #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) #define UIP_EXT_BUF_NEXT ((uint8_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + HBHO_TOTAL_LEN]) #define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + 2]) From 4035d840ddab6e44f68d0dd97d6551b50114bce9 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 15:57:39 +0200 Subject: [PATCH 09/28] Rework uip.h buffer definitions --- os/net/ipv6/uip.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 35f0d9cfd..213081a91 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -79,16 +79,19 @@ /** * Direct access to IPv6 header */ -#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) +#define UIP_IP_BUF_CHAR ((unsigned char *)uip_buf + UIP_LLH_LEN) +#define UIP_IP_BUF ((struct uip_ip_hdr *)UIP_IP_BUF_CHAR) +#define UIP_IP_PAYLOAD(ext) ((unsigned char *)UIP_IP_BUF_CHAR + UIP_IPH_LEN + (ext)) /** - * Direct access to ICMP, UDP, and TCP headers and payload + * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset */ -#define UIP_IP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD) -#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len]) -#define UIP_UDP_BUF ((struct uip_udp_hdr *)UIP_IP_PAYLOAD) -#define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD) +#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_ICMP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_ICMPH_LEN) +#define UIP_UDP_BUF ((struct uip_udp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_UDP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_UDPH_LEN) +#define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) +#define UIP_TCP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_TCPH_LEN) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" From 8152342c11e2ee3af53633fd8288e5ee8454ac4c Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 16:32:00 +0200 Subject: [PATCH 10/28] Rework uIP6 extension header access --- os/net/ipv6/uip-icmp6.c | 3 +- os/net/ipv6/uip.h | 9 +++ os/net/ipv6/uip6.c | 40 ++++++------- os/net/routing/rpl-classic/rpl-ext-header.c | 63 ++++++++++----------- os/net/routing/rpl-lite/rpl-ext-header.c | 63 ++++++++++----------- 5 files changed, 87 insertions(+), 91 deletions(-) diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index de9cc2394..90a53422d 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -54,7 +54,6 @@ #define LOG_LEVEL LOG_LEVEL_IPV6 #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)UIP_ICMP_PAYLOAD) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN]) /** \brief temporary IP address */ @@ -172,7 +171,7 @@ void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { /* check if originating packet is not an ICMP error */ if(uip_ext_len) { - if(UIP_EXT_BUF->next == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { + if(UIP_EXT_BUF(uip_ext_len)->next == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { uip_clear_buf(); return; } diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 213081a91..f4ee4dd2b 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -93,6 +93,15 @@ #define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) #define UIP_TCP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_TCPH_LEN) +/** + * Direct access to extension headers, with explicit ext header offset + */ +#define UIP_EXT_BUF(ext) ((struct uip_ext_hdr *)UIP_IP_PAYLOAD(ext)) +#define UIP_HBHO_BUF(ext) ((struct uip_hbho_hdr *)UIP_IP_PAYLOAD(ext)) +#define UIP_RH_BUF(ext) ((struct uip_routing_hdr *)UIP_IP_PAYLOAD(ext)) +#define UIP_FRAG_BUF(ext) ((struct uip_frag_hdr *)UIP_IP_PAYLOAD(ext)) +#define UIP_DESTO_BUF(ext) ((struct uip_desto_hdr *)UIP_IP_PAYLOAD(ext)) + #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" #include "net/linkaddr.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 615a658dc..943791311 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -142,14 +142,8 @@ uint8_t uip_ext_opt_offset = 0; * @{ */ #define FBUF ((struct uip_ip_hdr *)&uip_reassbuf[0]) -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_ROUTING_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_FRAG_BUF ((struct uip_frag_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_DESTO_BUF ((struct uip_desto_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) #define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len]) /** @} */ /** * \name Buffer variables @@ -520,7 +514,7 @@ remove_ext_hdr(void) } last_uip_ext_len = uip_ext_len; uip_ext_len = 0; - UIP_IP_BUF->proto = UIP_EXT_BUF->next; + UIP_IP_BUF->proto = UIP_EXT_BUF(uip_ext_len)->next; memmove(((uint8_t *)UIP_TCP_BUF), (uint8_t *)UIP_TCP_BUF + last_uip_ext_len, uip_len - UIP_IPH_LEN - last_uip_ext_len); @@ -658,7 +652,7 @@ uip_reass(void) etimer_set(&uip_reass_timer, UIP_REASS_MAXAGE*CLOCK_SECOND); uip_reass_on = 1; uip_reassflags = 0; - uip_id = UIP_FRAG_BUF->id; + uip_id = UIP_FRAG_BUF(uip_ext_len)->id; /* Clear the bitmap. */ memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap)); } @@ -669,9 +663,9 @@ uip_reass(void) */ if(uip_ipaddr_cmp(&FBUF->srcipaddr, &UIP_IP_BUF->srcipaddr) && uip_ipaddr_cmp(&FBUF->destipaddr, &UIP_IP_BUF->destipaddr) && - UIP_FRAG_BUF->id == uip_id) { + UIP_FRAG_BUF(uip_ext_len)->id == uip_id) { len = uip_len - uip_ext_len - UIP_IPH_LEN - UIP_FRAGH_LEN; - offset = (uip_ntohs(UIP_FRAG_BUF->offsetresmore) & 0xfff8); + offset = (uip_ntohs(UIP_FRAG_BUF(uip_ext_len)->offsetresmore) & 0xfff8); /* in byte, originaly in multiple of 8 bytes*/ LOG_INFO("len %d\n", len); LOG_INFO("offset %d\n", offset); @@ -682,7 +676,7 @@ uip_reass(void) * Part is obtained from the Next Header field of the first * fragment's Fragment header. */ - *uip_next_hdr = UIP_FRAG_BUF->next; + *uip_next_hdr = UIP_FRAG_BUF(uip_ext_len)->next; memcpy(FBUF, UIP_IP_BUF, uip_ext_len + UIP_IPH_LEN); LOG_INFO("src "); LOG_INFO_6ADDR(&FBUF->srcipaddr); @@ -703,7 +697,7 @@ uip_reass(void) /* If this fragment has the More Fragments flag set to zero, it is the last fragment*/ - if((uip_ntohs(UIP_FRAG_BUF->offsetresmore) & IP_MF) == 0) { + if((uip_ntohs(UIP_FRAG_BUF(uip_ext_len)->offsetresmore) & IP_MF) == 0) { uip_reassflags |= UIP_REASS_FLAG_LASTFRAG; /*calculate the size of the entire packet*/ uip_reasslen = offset + len; @@ -727,7 +721,7 @@ uip_reass(void) /* Copy the fragment into the reassembly buffer, at the right offset. */ memcpy((uint8_t *)FBUF + UIP_IPH_LEN + uip_ext_len + offset, - (uint8_t *)UIP_FRAG_BUF + UIP_FRAGH_LEN, len); + (uint8_t *)UIP_FRAG_BUF(uip_ext_len) + UIP_FRAGH_LEN, len); /* Update the bitmap. */ if(offset >> 6 == (offset + len) >> 6) { @@ -844,7 +838,7 @@ ext_hdr_options_process(void) * length field in an option : the length of data in the option */ uip_ext_opt_offset = 2; - while(uip_ext_opt_offset < ((UIP_EXT_BUF->len << 3) + 8)) { + while(uip_ext_opt_offset < ((UIP_EXT_BUF(uip_ext_len)->len << 3) + 8)) { switch(UIP_EXT_HDR_OPT_BUF->type) { /* * for now we do not support any options except padding ones @@ -1150,8 +1144,8 @@ uip_process(uint8_t flag) switch(ext_hdr_options_process()) { case 0: /* continue */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; case 1: LOG_ERR("Dropping packet after extension header processing\n"); @@ -1285,8 +1279,8 @@ uip_process(uint8_t flag) switch(ext_hdr_options_process()) { case 0: /*continue*/ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; case 1: /*silently discard*/ @@ -1314,8 +1308,8 @@ uip_process(uint8_t flag) switch(ext_hdr_options_process()) { case 0: /*continue*/ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; case 1: /*silently discard*/ @@ -1344,7 +1338,7 @@ uip_process(uint8_t flag) */ LOG_DBG("Processing Routing header\n"); - if(UIP_ROUTING_BUF->seg_left > 0) { + if(UIP_RH_BUF(uip_ext_len)->seg_left > 0) { if(NETSTACK_ROUTING.ext_header_srh_update()) { /* With routing header, the detination address is us and will @@ -1378,8 +1372,8 @@ uip_process(uint8_t flag) LOG_ERR("unrecognized routing type"); goto send; } - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; case UIP_PROTO_FRAG: /* Fragmentation header:call the reassembly function, then leave */ diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 18ebfa3a8..ebaa823aa 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -61,10 +61,7 @@ #include /*---------------------------------------------------------------------------*/ -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RH_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) #define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) #define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) @@ -80,12 +77,12 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uip_ds6_route_t *route; rpl_parent_t *sender = NULL; - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || UIP_EXT_HDR_OPT_RPL_BUF->opt_type != UIP_EXT_HDR_OPT_RPL || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF->len, + UIP_HBHO_BUF(uip_ext_len)->len, UIP_EXT_HDR_OPT_RPL_BUF->opt_type, UIP_EXT_HDR_OPT_RPL_BUF->opt_len); return 0; /* Drop */ @@ -200,8 +197,8 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * header. */ /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: uip_next_hdr = NULL; @@ -214,7 +211,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr); if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) || + && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -255,8 +252,8 @@ rpl_ext_header_srh_update(void) * header. */ /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: uip_next_hdr = NULL; @@ -265,7 +262,7 @@ rpl_ext_header_srh_update(void) } if(uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) { + && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) { /* SRH found, now look for next hop */ uint8_t cmpri, cmpre; uint8_t ext_len; @@ -274,8 +271,8 @@ rpl_ext_header_srh_update(void) uint8_t segments_left; uip_ipaddr_t current_dest_addr; - segments_left = UIP_RH_BUF->seg_left; - ext_len = (UIP_RH_BUF->len * 8) + 8; + segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; + ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; padding = UIP_RPL_SRH_BUF->pad >> 4; @@ -289,7 +286,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address and address[i] */ @@ -302,7 +299,7 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF->seg_left--; + UIP_RH_BUF(uip_ext_len)->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); @@ -428,13 +425,13 @@ insert_srh_header(void) memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); /* Insert source routing header */ - UIP_RH_BUF->next = UIP_IP_BUF->proto; + UIP_RH_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF->len = (ext_len - 8) / 8; - UIP_RH_BUF->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF->seg_left = path_len; + UIP_RH_BUF(uip_ext_len)->len = (ext_len - 8) / 8; + UIP_RH_BUF(uip_ext_len)->routing_type = RPL_RH_TYPE_SRH; + UIP_RH_BUF(uip_ext_len)->seg_left = path_len; /* Initialize RPL Source Routing Header */ UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; @@ -443,7 +440,7 @@ insert_srh_header(void) /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -484,7 +481,7 @@ update_hbh_header(void) uip_ext_opt_offset = 2; if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size (%u %u)\n", @@ -565,16 +562,16 @@ insert_hbh_header(const rpl_instance_t *instance) return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF, 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload UIP_EXT_BUF(uip_ext_len) further */ + memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF(uip_ext_len), uip_len - UIP_IPH_LEN); + memset(UIP_HBHO_BUF(uip_ext_len), 0, RPL_HOP_BY_HOP_LEN); /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF->next = UIP_IP_BUF->proto; + UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; @@ -612,8 +609,8 @@ rpl_ext_header_remove(void) case UIP_PROTO_ROUTING: if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF->next; - rpl_ext_hdr_len = (UIP_EXT_BUF->len * 8) + 8; + *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; + rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; temp_len = UIP_IP_BUF->len[1]; uip_len -= rpl_ext_hdr_len; UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; @@ -621,10 +618,10 @@ rpl_ext_header_remove(void) UIP_IP_BUF->len[0]--; } LOG_DBG("Removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF, ((uint8_t *)UIP_EXT_BUF) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); + memmove(UIP_EXT_BUF(uip_ext_len), ((uint8_t *)UIP_EXT_BUF(uip_ext_len)) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); } else { - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; } break; case UIP_PROTO_DESTO: @@ -637,8 +634,8 @@ rpl_ext_header_remove(void) * UIP_PROTO_DESTO. Otherwise, we'll return. */ /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: return; diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 414a93104..ab9e37188 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -54,10 +54,7 @@ #define LOG_LEVEL LOG_LEVEL_RPL /*---------------------------------------------------------------------------*/ -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len]) -#define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RH_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len]) #define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) #define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) #define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) @@ -84,8 +81,8 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * As per RFC 2460, only the Hop-by-Hop Options header and * Destination Options header can appear before the Routing header. */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: uip_next_hdr = NULL; @@ -101,7 +98,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) dest_node = uip_sr_get_node(NULL, &UIP_IP_BUF->destipaddr); if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF->routing_type == RPL_RH_TYPE_SRH) || + && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -143,8 +140,8 @@ rpl_ext_header_srh_update(void) * As per RFC 2460, only the Hop-by-Hop Options header and * Destination Options header can appear before the Routing header. */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: uip_next_hdr = NULL; @@ -153,15 +150,15 @@ rpl_ext_header_srh_update(void) } if(uip_next_hdr == NULL || *uip_next_hdr != UIP_PROTO_ROUTING - || UIP_RH_BUF->routing_type != RPL_RH_TYPE_SRH) { + || UIP_RH_BUF(uip_ext_len)->routing_type != RPL_RH_TYPE_SRH) { LOG_INFO("SRH not found\n"); uip_ext_len = last_uip_ext_len; return 0; } /* Parse SRH */ - segments_left = UIP_RH_BUF->seg_left; - ext_len = (UIP_RH_BUF->len * 8) + 8; + segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; + ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; padding = UIP_RPL_SRH_BUF->pad >> 4; @@ -176,7 +173,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address with address[i] */ @@ -189,7 +186,7 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF->seg_left--; + UIP_RH_BUF(uip_ext_len)->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); @@ -314,13 +311,13 @@ insert_srh_header(void) memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); /* Insert source routing header */ - UIP_RH_BUF->next = UIP_IP_BUF->proto; + UIP_RH_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF->len = (ext_len - 8) / 8; - UIP_RH_BUF->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF->seg_left = path_len; + UIP_RH_BUF(uip_ext_len)->len = (ext_len - 8) / 8; + UIP_RH_BUF(uip_ext_len)->routing_type = RPL_RH_TYPE_SRH; + UIP_RH_BUF(uip_ext_len)->seg_left = path_len; /* Initialize RPL Source Routing Header */ UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; @@ -329,7 +326,7 @@ insert_srh_header(void) /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -369,11 +366,11 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF->opt_type; uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF->opt_len; - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || opt_type != UIP_EXT_HDR_OPT_RPL || opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF->len, opt_type, opt_len); + UIP_HBHO_BUF(uip_ext_len)->len, opt_type, opt_len); return 0; /* Drop */ } @@ -425,7 +422,7 @@ update_hbh_header(void) uip_ext_opt_offset = 2; if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size (%u %u)\n", @@ -471,16 +468,16 @@ insert_hbh_header(void) return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF, 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload UIP_EXT_BUF(uip_ext_len) further */ + memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF(uip_ext_len), uip_len - UIP_IPH_LEN); + memset(UIP_HBHO_BUF(uip_ext_len), 0, RPL_HOP_BY_HOP_LEN); /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF->next = UIP_IP_BUF->proto; + UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; @@ -546,8 +543,8 @@ rpl_ext_header_remove(void) case UIP_PROTO_ROUTING: if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF->next; - rpl_ext_hdr_len = (UIP_EXT_BUF->len * 8) + 8; + *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; + rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; temp_len = UIP_IP_BUF->len[1]; uip_len -= rpl_ext_hdr_len; UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; @@ -555,10 +552,10 @@ rpl_ext_header_remove(void) UIP_IP_BUF->len[0]--; } LOG_INFO("removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF, ((uint8_t *)UIP_EXT_BUF) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); + memmove(UIP_EXT_BUF(uip_ext_len), ((uint8_t *)UIP_EXT_BUF(uip_ext_len)) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); } else { - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; } break; case UIP_PROTO_DESTO: @@ -571,8 +568,8 @@ rpl_ext_header_remove(void) * UIP_PROTO_DESTO. Otherwise, we'll return. */ /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF->next; - uip_ext_len += (UIP_EXT_BUF->len << 3) + 8; + uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; + uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; break; default: return; From 01b795a4c2e2235615c2f1599593f8e9e6f4dc74 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 16:58:51 +0200 Subject: [PATCH 11/28] Rework uIP6 extension header options access --- os/net/ipv6/uip-icmp6.c | 5 +- os/net/ipv6/uip.h | 8 +++ os/net/ipv6/uip6.c | 17 +++-- os/net/routing/rpl-classic/rpl-ext-header.c | 80 ++++++++++----------- os/net/routing/rpl-lite/rpl-ext-header.c | 67 ++++++++--------- tools/sky/uip6-bridge/sicslow_ethernet.c | 2 +- 6 files changed, 86 insertions(+), 93 deletions(-) diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 90a53422d..ca543d68b 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -54,7 +54,6 @@ #define LOG_LEVEL LOG_LEVEL_IPV6 #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)UIP_ICMP_PAYLOAD) -#define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN]) /** \brief temporary IP address */ static uip_ipaddr_t tmp_ipaddr; @@ -194,14 +193,14 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { uip_len = UIP_LINK_MTU; } - memmove((uint8_t *)UIP_ICMP6_ERROR_BUF + uip_ext_len + UIP_ICMP6_ERROR_LEN, + memmove(UIP_ICMP_PAYLOAD + uip_ext_len + UIP_ICMP6_ERROR_LEN, (void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - uip_ext_len - UIP_ICMP6_ERROR_LEN); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; if (uip_ext_len) { - UIP_FIRST_EXT_BUF->next = UIP_PROTO_ICMP6; + UIP_EXT_BUF(0)->next = UIP_PROTO_ICMP6; } else { UIP_IP_BUF->proto = UIP_PROTO_ICMP6; } diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index f4ee4dd2b..e4584de8c 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -101,6 +101,14 @@ #define UIP_RH_BUF(ext) ((struct uip_routing_hdr *)UIP_IP_PAYLOAD(ext)) #define UIP_FRAG_BUF(ext) ((struct uip_frag_hdr *)UIP_IP_PAYLOAD(ext)) #define UIP_DESTO_BUF(ext) ((struct uip_desto_hdr *)UIP_IP_PAYLOAD(ext)) +#define UIP_RPL_SRH_BUF(ext) ((struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(ext) + RPL_RH_LEN)) + +/** + * Direct access to extension header options, with explicit ext header and option offset + */ +#define UIP_EXT_HDR_OPT_BUF(ext, opt) ((struct uip_ext_hdr_opt *)(UIP_IP_PAYLOAD(ext) + (opt))) +#define UIP_EXT_HDR_OPT_PADN_BUF(ext, opt) ((struct uip_ext_hdr_opt_padn *)(UIP_IP_PAYLOAD(ext) + (opt))) +#define UIP_EXT_HDR_OPT_RPL_BUF(ext, opt) ((struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(ext) + (opt))) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 943791311..8b58e53c5 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -138,12 +138,11 @@ uint8_t uip_ext_opt_offset = 0; /* Buffers */ /*---------------------------------------------------------------------------*/ /** - * \name Buffer defines + * \name Reassembly buffer definition * @{ */ #define FBUF ((struct uip_ip_hdr *)&uip_reassbuf[0]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) + /** @} */ /** * \name Buffer variables @@ -839,7 +838,7 @@ ext_hdr_options_process(void) */ uip_ext_opt_offset = 2; while(uip_ext_opt_offset < ((UIP_EXT_BUF(uip_ext_len)->len << 3) + 8)) { - switch(UIP_EXT_HDR_OPT_BUF->type) { + switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type) { /* * for now we do not support any options except padding ones * PAD1 does not make sense as the header must be 8bytes aligned, @@ -851,7 +850,7 @@ ext_hdr_options_process(void) break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - uip_ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF->opt_len + 2; + uip_ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len + 2; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -866,7 +865,7 @@ ext_hdr_options_process(void) LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - uip_ext_opt_offset += (UIP_EXT_HDR_OPT_BUF->len) + 2; + uip_ext_opt_offset += (UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->len) + 2; return 0; default: /* @@ -882,8 +881,8 @@ ext_hdr_options_process(void) * Problem, Code 2, message to the packet's Source Address, * pointing to the unrecognized Option Type. */ - LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF->type); - switch(UIP_EXT_HDR_OPT_BUF->type & 0xC0) { + LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type); + switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type & 0xC0) { case 0: break; case 0x40: @@ -898,7 +897,7 @@ ext_hdr_options_process(void) return 2; } /* in the cases were we did not discard, update ext_opt* */ - uip_ext_opt_offset += UIP_EXT_HDR_OPT_BUF->len + 2; + uip_ext_opt_offset += UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->len + 2; break; } } diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index ebaa823aa..6a99b573a 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -60,12 +60,6 @@ #include #include -/*---------------------------------------------------------------------------*/ -#define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_RPL_BUF ((struct uip_ext_hdr_opt_rpl *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) /*---------------------------------------------------------------------------*/ int rpl_ext_header_hbh_update(int uip_ext_opt_offset) @@ -78,24 +72,24 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) rpl_parent_t *sender = NULL; if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_type != UIP_EXT_HDR_OPT_RPL - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { + || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type != UIP_EXT_HDR_OPT_RPL + || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n", UIP_HBHO_BUF(uip_ext_len)->len, - UIP_EXT_HDR_OPT_RPL_BUF->opt_type, - UIP_EXT_HDR_OPT_RPL_BUF->opt_len); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type, + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF->instance); + instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); if(instance == NULL) { LOG_ERR("Unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF->instance); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); return 0; } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_FWD_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("Forward error!\n"); /* We should try to repair it by removing the neighbor that caused the packet to be forwareded in the first place. We drop any @@ -119,14 +113,14 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) return 0; } down = 0; - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN) { + if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN) { down = 1; } - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF->senderrank); + sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank); sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR)) { + if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR)) { /* A rank error was signalled, attempt to repair it by updating * the sender's rank from ext header */ sender->rank = sender_rank; @@ -156,7 +150,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) instance->unicast_dio_target = sender; rpl_schedule_unicast_dio_immediately(instance); } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) { RPL_STAT(rpl_stats.loop_errors++); LOG_ERR(" Rank error signalled in RPL option!\n"); /* Packet must be dropped and dio trickle timer reset, see RFC6550 - 11.2.2.2 */ @@ -165,7 +159,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) } LOG_WARN("Single error tolerated\n"); RPL_STAT(rpl_stats.loop_warnings++); - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_RANK_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; return 1; } @@ -273,9 +267,9 @@ rpl_ext_header_srh_update(void) segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF->pad >> 4; + cmpri = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr >> 4; + cmpre = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr & 0x0f; + padding = UIP_RPL_SRH_BUF(uip_ext_len)->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -419,7 +413,7 @@ insert_srh_header(void) return 0; } - /* Move existing ext headers and payload uip_ext_len further */ + /* Move existing ext headers and payload ext_len further */ memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); @@ -434,8 +428,8 @@ insert_srh_header(void) UIP_RH_BUF(uip_ext_len)->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF->pad = padding << 4; + UIP_RPL_SRH_BUF(uip_ext_len)->cmpr = (cmpri << 4) + cmpre; + UIP_RPL_SRH_BUF(uip_ext_len)->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ @@ -480,17 +474,17 @@ update_hbh_header(void) uip_ext_len = 0; uip_ext_opt_offset = 2; - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { + || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF->opt_len, + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len, uip_ext_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF->instance); + instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); if(instance == NULL || !instance->used || !instance->current_dag->joined) { LOG_ERR("Unable to add/update hop-by-hop extension header: incorrect instance\n"); uip_ext_len = last_uip_ext_len; @@ -499,17 +493,17 @@ update_hbh_header(void) LOG_INFO("Updating RPL option\n"); /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = instance->instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = instance->instance_id; if(RPL_IS_STORING(instance)) { /* In non-storing mode, downwards traffic does not have the HBH option */ /* Check the direction of the down flag, as per Section 11.2.2.3, which states that if a packet is going down it should in general not go back up again. If this happens, a RPL_HDR_OPT_FWD_ERR should be flagged. */ - if((UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN)) { + if((UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN)) { if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_FWD_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_FWD_ERR; LOG_WARN("RPL forwarding error\n"); /* We should send back the packet to the originating parent, but it is not feasible yet, so we send a No-Path DAO instead */ @@ -528,11 +522,11 @@ update_hbh_header(void) if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { /* No route was found, so this packet will go towards the RPL root. If so, we should not set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF->flags &= ~RPL_HDR_OPT_DOWN; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags &= ~RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going up\n"); } else { /* A DAO route was found so we set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_DOWN; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going down\n"); } } @@ -562,9 +556,9 @@ insert_hbh_header(const rpl_instance_t *instance) return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF(uip_ext_len) further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF(uip_ext_len), uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF(uip_ext_len), 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */ + memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); + memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); /* Update IP and HBH protocol and fields */ UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; @@ -572,11 +566,11 @@ insert_hbh_header(const rpl_instance_t *instance) /* Initialize HBH option */ UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = instance->instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len = RPL_HDR_OPT_LEN; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags = 0; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = instance->instance_id; uip_len += RPL_HOP_BY_HOP_LEN; temp_len = UIP_IP_BUF->len[1]; UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; @@ -607,7 +601,7 @@ rpl_ext_header_remove(void) switch(*uip_next_hdr) { case UIP_PROTO_HBHO: case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { + if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL)) { /* Remove hop-by-hop and routing headers */ *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index ab9e37188..02a681476 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -53,13 +53,6 @@ #define LOG_MODULE "RPL" #define LOG_LEVEL LOG_LEVEL_RPL -/*---------------------------------------------------------------------------*/ -#define UIP_HBHO_NEXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_HOP_BY_HOP_LEN]) -#define UIP_RPL_SRH_BUF ((struct uip_rpl_srh_hdr *)&uip_buf[uip_l2_l3_hdr_len + RPL_RH_LEN]) -#define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) -#define UIP_EXT_HDR_OPT_RPL_BUF ((struct uip_ext_hdr_opt_rpl *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset]) - /*---------------------------------------------------------------------------*/ int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) @@ -159,9 +152,9 @@ rpl_ext_header_srh_update(void) /* Parse SRH */ segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF->pad >> 4; + cmpri = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr >> 4; + cmpre = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr & 0x0f; + padding = UIP_RPL_SRH_BUF(uip_ext_len)->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -305,7 +298,7 @@ insert_srh_header(void) return 0; } - /* Move existing ext headers and payload uip_ext_len further */ + /* Move existing ext headers and payload ext_len further */ memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); @@ -320,8 +313,8 @@ insert_srh_header(void) UIP_RH_BUF(uip_ext_len)->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF->pad = padding << 4; + UIP_RPL_SRH_BUF(uip_ext_len)->cmpr = (cmpri << 4) + cmpre; + UIP_RPL_SRH_BUF(uip_ext_len)->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ @@ -363,8 +356,8 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uint16_t sender_rank; uint8_t sender_closer; rpl_nbr_t *sender; - uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF->opt_type; - uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF->opt_len; + uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type; + uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len; if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || opt_type != UIP_EXT_HDR_OPT_RPL @@ -374,21 +367,21 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF->instance) { + if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance) { LOG_ERR("unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF->instance); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); return 0; /* Drop */ } - if(UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_FWD_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("forward error!\n"); return 0; /* Drop */ } - down = (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF->senderrank); + down = (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; + sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank); sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; + rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; sender_closer = sender_rank < curr_instance.dag.rank; loop_detected = (down && !sender_closer) || (!down && sender_closer); @@ -402,7 +395,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) if(loop_detected) { /* Set forward error flag */ - UIP_EXT_HDR_OPT_RPL_BUF->flags |= RPL_HDR_OPT_RANK_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; } return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled); @@ -421,24 +414,24 @@ update_hbh_header(void) uip_ext_len = 0; uip_ext_opt_offset = 2; - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF->opt_len != RPL_HDR_OPT_LEN) { + || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF->opt_len, uip_ext_len); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len, uip_ext_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF->instance) { + if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance) { LOG_ERR("unable to add/update hop-by-hop extension header: incorrect instance\n"); uip_ext_len = last_uip_ext_len; return 0; /* Drop */ } /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = curr_instance.instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = curr_instance.instance_id; } uip_ext_len = last_uip_ext_len; @@ -468,9 +461,9 @@ insert_hbh_header(void) return 0; } - /* Move existing ext headers and payload UIP_EXT_BUF(uip_ext_len) further */ - memmove(UIP_HBHO_NEXT_BUF, UIP_EXT_BUF(uip_ext_len), uip_len - UIP_IPH_LEN); - memset(UIP_HBHO_BUF(uip_ext_len), 0, RPL_HOP_BY_HOP_LEN); + /* Move existing ext headers and payload RPL_HOP_BY_HOP_LEN further */ + memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); + memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); /* Update IP and HBH protocol and fields */ UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; @@ -478,11 +471,11 @@ insert_hbh_header(void) /* Initialize HBH option */ UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF->instance = curr_instance.instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len = RPL_HDR_OPT_LEN; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags = 0; + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); + UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = curr_instance.instance_id; uip_len += RPL_HOP_BY_HOP_LEN; temp_len = UIP_IP_BUF->len[1]; UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; @@ -541,7 +534,7 @@ rpl_ext_header_remove(void) switch(*uip_next_hdr) { case UIP_PROTO_HBHO: case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF->opt_type == UIP_EXT_HDR_OPT_RPL)) { + if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL)) { /* Remove hop-by-hop and routing headers */ *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 0b241d71b..5274a7833 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -379,7 +379,7 @@ typedef struct { uint8_t data[16]; } icmp_opts_t; -#define UIP_ICMP_OPTS(x) ((icmp_opts_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + x]) +#define UIP_ICMP_OPTS(x) ((icmp_opts_t *)UIP_IP_PAYLOAD(x)) void slide(uint8_t * data, uint8_t length, int16_t slide); From bbb641a450e9be4cd6763ba6e328cf51f12408e0 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 17:24:53 +0200 Subject: [PATCH 12/28] Rework uip-nd6 buffer access --- os/net/ipv6/uip-nd6.c | 97 ++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 446ad8346..2dbf95b00 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -101,10 +101,11 @@ #define UIP_ND6_NA_BUF ((uip_nd6_na *)UIP_ICMP_PAYLOAD) /** @} */ /** Pointer to ND option */ -#define UIP_ND6_OPT_HDR_BUF ((uip_nd6_opt_hdr *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_PREFIX_BUF ((uip_nd6_opt_prefix_info *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_MTU_BUF ((uip_nd6_opt_mtu *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) -#define UIP_ND6_OPT_RDNSS_BUF ((uip_nd6_opt_dns *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]) +#define ND6_OPT(opt) ((unsigned char *)(UIP_ICMP_PAYLOAD + (opt))) +#define ND6_OPT_HDR_BUF(opt) ((uip_nd6_opt_hdr *)ND6_OPT(opt)) +#define ND6_OPT_PREFIX_BUF(opt) ((uip_nd6_opt_prefix_info *)ND6_OPT(opt)) +#define ND6_OPT_MTU_BUF(opt) ((uip_nd6_opt_mtu *)ND6_OPT(opt)) +#define ND6_OPT_RDNSS_BUF(opt) ((uip_nd6_opt_dns *)ND6_OPT(opt)) /** @} */ #if UIP_ND6_SEND_NS || UIP_ND6_SEND_NA || UIP_ND6_SEND_RA || !UIP_CONF_ROUTER @@ -201,12 +202,12 @@ ns_input(void) nd6_opt_offset = UIP_ND6_NS_LEN; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("NS received is bad\n"); goto discard; } #endif /* UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]; #if UIP_CONF_IPV6_CHECKS @@ -250,7 +251,7 @@ ns_input(void) LOG_WARN("ND option not supported in NS"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } addr = uip_ds6_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr); @@ -491,20 +492,20 @@ na_input(void) nd6_opt_llao = NULL; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("NA received is bad\n"); goto discard; } #endif /*UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_TLLAO: - nd6_opt_llao = (uint8_t *)UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset); break; default: LOG_WARN("ND option not supported in NA\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } addr = uip_ds6_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr); /* Message processing, including TLLAO if any */ @@ -642,20 +643,20 @@ rs_input(void) while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { #if UIP_CONF_IPV6_CHECKS - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("RS received is bad\n"); goto discard; } #endif /*UIP_CONF_IPV6_CHECKS */ - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: - nd6_opt_llao = (uint8_t *)UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset); break; default: LOG_WARN("ND option not supported in RS\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } /* Options processing: only SLLAO */ if(nd6_opt_llao != NULL) { @@ -744,31 +745,31 @@ uip_nd6_ra_output(uip_ipaddr_t * dest) for(prefix = uip_ds6_prefix_list; prefix < uip_ds6_prefix_list + UIP_DS6_PREFIX_NB; prefix++) { if((prefix->isused) && (prefix->advertise)) { - UIP_ND6_OPT_PREFIX_BUF->type = UIP_ND6_OPT_PREFIX_INFO; - UIP_ND6_OPT_PREFIX_BUF->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8; - UIP_ND6_OPT_PREFIX_BUF->preflen = prefix->length; - UIP_ND6_OPT_PREFIX_BUF->flagsreserved1 = prefix->l_a_reserved; - UIP_ND6_OPT_PREFIX_BUF->validlt = uip_htonl(prefix->vlifetime); - UIP_ND6_OPT_PREFIX_BUF->preferredlt = uip_htonl(prefix->plifetime); - UIP_ND6_OPT_PREFIX_BUF->reserved2 = 0; - uip_ipaddr_copy(&(UIP_ND6_OPT_PREFIX_BUF->prefix), &(prefix->ipaddr)); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_PREFIX_INFO; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_PREFIX_INFO_LEN / 8; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preflen = prefix->length; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->flagsreserved1 = prefix->l_a_reserved; + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->validlt = uip_htonl(prefix->vlifetime); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->preferredlt = uip_htonl(prefix->plifetime); + ND6_OPT_PREFIX_BUF(nd6_opt_offset)->reserved2 = 0; + uip_ipaddr_copy(&(ND6_OPT_PREFIX_BUF(nd6_opt_offset)->prefix), &(prefix->ipaddr)); nd6_opt_offset += UIP_ND6_OPT_PREFIX_INFO_LEN; uip_len += UIP_ND6_OPT_PREFIX_INFO_LEN; } } /* Source link-layer option */ - create_llao((uint8_t *)UIP_ND6_OPT_HDR_BUF, UIP_ND6_OPT_SLLAO); + create_llao((uint8_t *)ND6_OPT_HDR_BUF(nd6_opt_offset), UIP_ND6_OPT_SLLAO); uip_len += UIP_ND6_OPT_LLAO_LEN; nd6_opt_offset += UIP_ND6_OPT_LLAO_LEN; /* MTU */ - UIP_ND6_OPT_MTU_BUF->type = UIP_ND6_OPT_MTU; - UIP_ND6_OPT_MTU_BUF->len = UIP_ND6_OPT_MTU_LEN >> 3; - UIP_ND6_OPT_MTU_BUF->reserved = 0; - //UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(uip_ds6_if.link_mtu); - UIP_ND6_OPT_MTU_BUF->mtu = uip_htonl(1500); + ND6_OPT_MTU_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_MTU; + ND6_OPT_MTU_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_MTU_LEN >> 3; + ND6_OPT_MTU_BUF(nd6_opt_offset)->reserved = 0; + //ND6_OPT_MTU_BUF(nd6_opt_offset)->mtu = uip_htonl(uip_ds6_if.link_mtu); + ND6_OPT_MTU_BUF(nd6_opt_offset)->mtu = uip_htonl(1500); uip_len += UIP_ND6_OPT_MTU_LEN; nd6_opt_offset += UIP_ND6_OPT_MTU_LEN; @@ -776,22 +777,22 @@ uip_nd6_ra_output(uip_ipaddr_t * dest) #if UIP_ND6_RA_RDNSS if(uip_nameserver_count() > 0) { uint8_t i = 0; - uip_ipaddr_t *ip = &UIP_ND6_OPT_RDNSS_BUF->ip; + uip_ipaddr_t *ip = &ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip; uip_ipaddr_t *dns = NULL; - UIP_ND6_OPT_RDNSS_BUF->type = UIP_ND6_OPT_RDNSS; - UIP_ND6_OPT_RDNSS_BUF->reserved = 0; - UIP_ND6_OPT_RDNSS_BUF->lifetime = uip_nameserver_next_expiration(); - if(UIP_ND6_OPT_RDNSS_BUF->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) { - UIP_ND6_OPT_RDNSS_BUF->lifetime -= clock_seconds(); + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->type = UIP_ND6_OPT_RDNSS; + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->reserved = 0; + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime = uip_nameserver_next_expiration(); + if(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) { + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime -= clock_seconds(); } while((dns = uip_nameserver_get(i)) != NULL) { uip_ipaddr_copy(ip++, dns); i++; } - UIP_ND6_OPT_RDNSS_BUF->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1); + ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1); LOG_INFO("%d nameservers reported\n", i); - uip_len += UIP_ND6_OPT_RDNSS_BUF->len << 3; - nd6_opt_offset += UIP_ND6_OPT_RDNSS_BUF->len << 3; + uip_len += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3; + nd6_opt_offset += ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len << 3; } #endif /* UIP_ND6_RA_RDNSS */ @@ -902,14 +903,14 @@ ra_input(void) /* Options processing */ nd6_opt_offset = UIP_ND6_RA_LEN; while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) { - if(UIP_ND6_OPT_HDR_BUF->len == 0) { + if(ND6_OPT_HDR_BUF(nd6_opt_offset)->len == 0) { LOG_ERR("RA received is bad"); goto discard; } - switch (UIP_ND6_OPT_HDR_BUF->type) { + switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: LOG_DBG("Processing SLLAO option in RA\n"); - nd6_opt_llao = (uint8_t *) UIP_ND6_OPT_HDR_BUF; + nd6_opt_llao = (uint8_t *) ND6_OPT_HDR_BUF(nd6_opt_offset); nbr = uip_ds6_nbr_lookup(&UIP_IP_BUF->srcipaddr); if(!extract_lladdr_from_llao_aligned(&lladdr_aligned)) { /* failed to extract llao - discard packet */ @@ -942,11 +943,11 @@ ra_input(void) case UIP_ND6_OPT_MTU: LOG_DBG("Processing MTU option in RA\n"); uip_ds6_if.link_mtu = - uip_ntohl(((uip_nd6_opt_mtu *) UIP_ND6_OPT_HDR_BUF)->mtu); + uip_ntohl(((uip_nd6_opt_mtu *) ND6_OPT_HDR_BUF(nd6_opt_offset))->mtu); break; case UIP_ND6_OPT_PREFIX_INFO: LOG_DBG("Processing PREFIX option in RA\n"); - nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *) UIP_ND6_OPT_HDR_BUF; + nd6_opt_prefix_info = (uip_nd6_opt_prefix_info *) ND6_OPT_HDR_BUF(nd6_opt_offset); if((uip_ntohl(nd6_opt_prefix_info->validlt) >= uip_ntohl(nd6_opt_prefix_info->preferredlt)) && (!uip_is_addr_linklocal(&nd6_opt_prefix_info->prefix))) { @@ -1033,14 +1034,14 @@ ra_input(void) #if UIP_ND6_RA_RDNSS case UIP_ND6_OPT_RDNSS: LOG_DBG("Processing RDNSS option\n"); - uint8_t naddr = (UIP_ND6_OPT_RDNSS_BUF->len - 1) / 2; - uip_ipaddr_t *ip = (uip_ipaddr_t *)(&UIP_ND6_OPT_RDNSS_BUF->ip); + uint8_t naddr = (ND6_OPT_RDNSS_BUF(nd6_opt_offset)->len - 1) / 2; + uip_ipaddr_t *ip = (uip_ipaddr_t *)(&ND6_OPT_RDNSS_BUF(nd6_opt_offset)->ip); LOG_DBG("got %d nameservers\n", naddr); while(naddr-- > 0) { LOG_DBG("nameserver: "); LOG_DBG_6ADDR(ip); - LOG_DBG_(" lifetime: %"PRIx32"\n", uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime)); - uip_nameserver_update(ip, uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime)); + LOG_DBG_(" lifetime: %"PRIx32"\n", uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime)); + uip_nameserver_update(ip, uip_ntohl(ND6_OPT_RDNSS_BUF(nd6_opt_offset)->lifetime)); ip++; } break; @@ -1049,7 +1050,7 @@ ra_input(void) LOG_ERR("ND option not supported in RA\n"); break; } - nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3); + nd6_opt_offset += (ND6_OPT_HDR_BUF(nd6_opt_offset)->len << 3); } defrt = uip_ds6_defrt_lookup(&UIP_IP_BUF->srcipaddr); From 0531c555e87753415c1d9c3c7f48f274637e095e Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 17:25:04 +0200 Subject: [PATCH 13/28] Minor enhancements to uip6.c --- os/net/ipv6/uip.h | 1 - os/net/ipv6/uip6.c | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index e4584de8c..353e28f64 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -1788,7 +1788,6 @@ struct uip_udp_hdr { * \hideinitializer */ #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) -#define UIP_APPDATA_PTR (void *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN] #define UIP_PROTO_ICMP 1 #define UIP_PROTO_TCP 6 diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 8b58e53c5..d9aec0089 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -2328,8 +2328,7 @@ uip_send(const void *data, int len) if(uip_sappdata != NULL) { copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN - - (int)((char *)uip_sappdata - - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN])); + (int)((char *)uip_sappdata - (char *)UIP_TCP_PAYLOAD)); } else { copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN); } @@ -2337,8 +2336,7 @@ uip_send(const void *data, int len) uip_slen = copylen; if(data != uip_sappdata) { if(uip_sappdata == NULL) { - memcpy((char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN], - (data), uip_slen); + memcpy(UIP_TCP_PAYLOAD, (data), uip_slen); } else { memcpy(uip_sappdata, (data), uip_slen); } From 3384de64b70fc5849d44bac8d585f98292f741d9 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 17:30:12 +0200 Subject: [PATCH 14/28] Rework buffer access in roll-tm.c --- os/net/ipv6/multicast/roll-tm.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 3e1d14b5b..e9214f8d4 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -456,9 +456,8 @@ static uint16_t last_seq; /*---------------------------------------------------------------------------*/ /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ -#define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN]) -#define UIP_EXT_BUF_NEXT ((uint8_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + HBHO_TOTAL_LEN]) -#define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + 2]) +#define UIP_EXT_BUF_NEXT ((uint8_t *)(UIP_IP_PAYLOAD(HBHO_TOTAL_LEN))) +#define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)(UIP_IP_PAYLOAD(0) + 2)) extern uint16_t uip_slen; /*---------------------------------------------------------------------------*/ /* Local function prototypes */ @@ -1322,11 +1321,11 @@ out() } /* Slide 'right' by HBHO_TOTAL_LEN bytes */ - memmove(UIP_EXT_BUF_NEXT, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); - memset(UIP_EXT_BUF, 0, HBHO_TOTAL_LEN); + memmove(UIP_EXT_BUF_NEXT, UIP_EXT_BUF(0), uip_len - UIP_IPH_LEN); + memset(UIP_EXT_BUF(0), 0, HBHO_TOTAL_LEN); - UIP_EXT_BUF->next = UIP_IP_BUF->proto; - UIP_EXT_BUF->len = 0; + UIP_EXT_BUF(0)->next = UIP_IP_BUF->proto; + UIP_EXT_BUF(0)->len = 0; lochbhmptr = UIP_EXT_OPT_FIRST; lochbhmptr->type = HBHO_OPT_TYPE_TRICKLE; From 56b85b6475d9c3bc758359e77002de3877721f81 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 17:47:57 +0200 Subject: [PATCH 15/28] uip.h: clarify comment --- os/net/ipv6/uip.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 353e28f64..39714c135 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -84,7 +84,7 @@ #define UIP_IP_PAYLOAD(ext) ((unsigned char *)UIP_IP_BUF_CHAR + UIP_IPH_LEN + (ext)) /** - * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset + * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_ext_len) */ #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) #define UIP_ICMP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_ICMPH_LEN) From 22f4a19994904ffc96cdc907d6d21a15966edb25 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 18:09:40 +0200 Subject: [PATCH 16/28] Clean up usage of uip_ext_len --- os/net/ipv6/multicast/esmrf.c | 3 +- os/net/ipv6/multicast/roll-tm.c | 3 +- os/net/ipv6/tcpip.c | 4 +- os/net/ipv6/uip-icmp6.c | 73 ++---- os/net/ipv6/uip-nd6.c | 7 +- os/net/ipv6/uip.h | 12 +- os/net/ipv6/uip6.c | 33 ++- os/net/ipv6/uipbuf.c | 104 ++++++-- os/net/ipv6/uipbuf.h | 32 ++- os/net/routing/nullrouting/nullrouting.c | 4 +- os/net/routing/routing.h | 6 +- os/net/routing/rpl-classic/rpl-ext-header.c | 267 +++++++------------- os/net/routing/rpl-classic/rpl.h | 2 +- os/net/routing/rpl-lite/rpl-ext-header.c | 249 +++++++----------- os/net/routing/rpl-lite/rpl-ext-header.h | 6 +- 15 files changed, 369 insertions(+), 436 deletions(-) diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index d72d99fad..746f2f03b 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -108,7 +108,6 @@ static uip_ipaddr_t des_ip; static void icmp_input(void); static void icmp_output(void); static void mcast_fwd(void *p); -int remove_ext_hdr(void); /*---------------------------------------------------------------------------*/ /* Internal Data Structures */ /*---------------------------------------------------------------------------*/ @@ -192,7 +191,7 @@ icmp_input() } #endif - remove_ext_hdr(); + uip_remove_ext_hdr(); PRINTF("ESMRF: ICMPv6 In from "); PRINT6ADDR(&UIP_IP_BUF->srcipaddr); diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index e9214f8d4..c942dfd46 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -1349,8 +1349,7 @@ out() HBH_SET_M(lochbhmptr); #endif - uip_ext_len += HBHO_TOTAL_LEN; - uip_len += HBHO_TOTAL_LEN; + uipbuf_add_ext_hdr(HBHO_TOTAL_LEN); /* Update the proto and length field in the v6 header */ UIP_IP_BUF->proto = UIP_PROTO_HBHO; diff --git a/os/net/ipv6/tcpip.c b/os/net/ipv6/tcpip.c index b7a10de9a..d78cae510 100644 --- a/os/net/ipv6/tcpip.c +++ b/os/net/ipv6/tcpip.c @@ -451,15 +451,13 @@ tcpip_input(void) uip_clear_buf(); } /*---------------------------------------------------------------------------*/ -extern void remove_ext_hdr(void); -/*---------------------------------------------------------------------------*/ static void output_fallback(void) { #ifdef UIP_FALLBACK_INTERFACE LOG_INFO("fallback: removing ext hdrs & setting proto %d %d\n", uip_ext_len, *((uint8_t *)UIP_IP_BUF + 40)); - remove_ext_hdr(); + uip_remove_ext_hdr(); /* Inform the other end that the destination is not reachable. If it's * not informed routes might get lost unexpectedly until there's a need * to send a new packet to the peer */ diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index ca543d68b..c4003352f 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -130,22 +130,7 @@ echo_request_input(void) uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &tmp_ipaddr); } - if(uip_ext_len > 0) { - /* Remove extension headers if any */ - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - uip_len -= uip_ext_len; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); - /* move the echo request payload (starting after the icmp header) - * to the new location in the reply. - * The shift is equal to the length of the extension headers present - * Note: UIP_ICMP_BUF still points to the echo request at this stage - */ - memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len, - (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN, - (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN)); - uip_ext_len = 0; - } + uip_remove_ext_hdr(); /* Below is important for the correctness of UIP_ICMP_BUF and the * checksum @@ -169,41 +154,32 @@ echo_request_input(void) void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { /* check if originating packet is not an ICMP error */ - if(uip_ext_len) { - if(UIP_EXT_BUF(uip_ext_len)->next == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { - uip_clear_buf(); - return; - } - } else { - if(UIP_IP_BUF->proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { - uip_clear_buf(); - return; - } + uint16_t shift; + if(uip_last_proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { + uip_clear_buf(); + return; } - /* Remove all extension headers related to the routing protocol in place */ + /* Remove all extension headers related to the routing protocol in place. + * Keep all other extension headers, so as to match original packet. */ NETSTACK_ROUTING.ext_header_remove(); /* remember data of original packet before shifting */ uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->destipaddr); - uip_len += UIP_IPICMPH_LEN + UIP_ICMP6_ERROR_LEN; - - if(uip_len > UIP_LINK_MTU) { - uip_len = UIP_LINK_MTU; - } - - memmove(UIP_ICMP_PAYLOAD + uip_ext_len + UIP_ICMP6_ERROR_LEN, - (void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - uip_ext_len - UIP_ICMP6_ERROR_LEN); + /* The ICMPv6 error message contains as much of possible of the invoking packet + * (see RFC 4443 section 3). Make space for the additional IPv6 and + * ICMPv6 headers here and move payload to the "right". What we move includes + * extension headers */ + shift = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ICMP6_ERROR_LEN; + uip_len += shift; + uip_len = MIN(uip_len, UIP_LINK_MTU); + memmove(UIP_IP_BUF_CHAR + shift, (void *)UIP_IP_BUF, uip_len - shift); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; - if (uip_ext_len) { - UIP_EXT_BUF(0)->next = UIP_PROTO_ICMP6; - } else { - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - } + UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; /* the source should not be unspecified nor multicast, the check for @@ -293,22 +269,7 @@ echo_reply_input(void) uip_ipaddr_copy(&sender, &UIP_IP_BUF->srcipaddr); ttl = UIP_IP_BUF->ttl; - if(uip_ext_len > 0) { - /* Remove extension headers if any */ - UIP_IP_BUF->proto = UIP_PROTO_ICMP6; - uip_len -= uip_ext_len; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); - /* move the echo reply payload (starting after the icmp header) - * to the new location in the reply. The shift is equal to the - * length of the extension headers present Note: UIP_ICMP_BUF - * still points to the echo request at this stage - */ - memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len, - (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN, - (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN)); - uip_ext_len = 0; - } + uip_remove_ext_hdr(); /* Call all registered applications to let them know an echo reply has been received. */ diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 2dbf95b00..9fc7429fe 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -322,7 +322,7 @@ create_na: #if UIP_CONF_ROUTER flags = flags | UIP_ND6_NA_FLAG_ROUTER; #endif - uip_ext_len = 0; + uip_clear_buf(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; @@ -343,8 +343,7 @@ create_na: UIP_ICMP_BUF->icmpchksum = 0; UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); - uip_len = - UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len(UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN); UIP_STAT(++uip_stat.nd6.sent); LOG_INFO("Sending NA to "); @@ -368,7 +367,7 @@ discard: void uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) { - uip_ext_len = 0; + uip_clear_buf(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 39714c135..49a7a0b21 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -1330,6 +1330,10 @@ extern uint16_t uip_len; * The length of the extension headers */ extern uint8_t uip_ext_len; + +/** The final protocol after IPv6 extension headers: + * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */ +extern uint8_t uip_last_proto; /** @} */ #if UIP_URGDATA > 0 @@ -1345,6 +1349,7 @@ extern uint16_t uip_urglen, uip_surglen; #define uip_clear_buf() { \ uip_len = 0; \ uip_ext_len = 0; \ + uip_last_proto = 0; \ uipbuf_clear_attr();\ } @@ -1804,7 +1809,7 @@ struct uip_udp_hdr { #define UIP_PROTO_NONE 59 /** @} */ -#define uip_is_proto_ext_hdr(proto) (proto == UIP_PROTO_HBHO || proto == UIP_PROTO_DESTO || proto == UIP_PROTO_ROUTING || proto == UIP_PROTO_FRAG || proto == UIP_PROTO_NONE) +#define uip_is_proto_ext_hdr(proto) ((proto) != UIP_PROTO_TCP && (proto) != UIP_PROTO_UDP && (proto) != UIP_PROTO_ICMP6) /** @{ */ /** \brief Destination and Hop By Hop extension headers option types */ @@ -2175,6 +2180,11 @@ uint16_t uip_udpchksum(void); */ uint16_t uip_icmp6chksum(void); +/** + * Removes all IPv6 extension headers from uip_buf, updates length fields + * (uip_len and uip_ext_len) + */ +void uip_remove_ext_hdr(void); #endif /* UIP_H_ */ diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index d9aec0089..4ded21bdc 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -132,6 +132,9 @@ uint8_t uip_ext_bitmap = 0; uint8_t uip_ext_len = 0; /** \brief length of the header options read */ uint8_t uip_ext_opt_offset = 0; +/** \brief The final protocol after IPv6 extension headers: + * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */ +uint8_t uip_last_proto = 0; /** @} */ /*---------------------------------------------------------------------------*/ @@ -499,15 +502,15 @@ uip_connect(const uip_ipaddr_t *ripaddr, uint16_t rport) #endif /* UIP_TCP && UIP_ACTIVE_OPEN */ /*---------------------------------------------------------------------------*/ void -remove_ext_hdr(void) +uip_remove_ext_hdr(void) { int last_uip_ext_len; /* Remove ext header before TCP/UDP processing. */ if(uip_ext_len > 0) { - LOG_DBG("Cutting ext-header before processing (extlen: %d, uiplen: %d)\n", + LOG_DBG("Removing IPv6 extension headers (extlen: %d, uiplen: %d)\n", uip_ext_len, uip_len); if(uip_len < UIP_IPH_LEN + uip_ext_len) { - LOG_ERR("ERROR: uip_len too short compared to ext len\n"); + LOG_ERR("uip_len too short compared to ext len\n"); uip_clear_buf(); return; } @@ -861,7 +864,7 @@ ext_hdr_options_process(void) * Using this fix, the header is ignored, and the next header (if * present) is processed. */ - if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_opt_offset)) { + if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_len, uip_ext_opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } @@ -909,6 +912,7 @@ ext_hdr_options_process(void) void uip_process(uint8_t flag) { + uint8_t *last_header; #if UIP_TCP int c; uint16_t tmp16; @@ -1104,10 +1108,21 @@ uip_process(uint8_t flag) * header (40 bytes). */ } else { - LOG_ERR("packet shorter than reported in IP header."); + LOG_ERR("packet shorter than reported in IP header"); goto drop; } + /* Check sanity of extension headers, and compute the total extension header + * length (uip_ext_len) as well as the final protocol (uip_last_proto) */ + uip_last_proto = 0; + last_header = uipbuf_get_last_header(UIP_IP_BUF_CHAR, uip_len, &uip_last_proto); + if(last_header == NULL) { + LOG_ERR("invalid extension header chain"); + goto drop; + } + /* Set uip_ext_len */ + uip_ext_len = last_header - UIP_IP_BUF_CHAR; + LOG_INFO("packet received from "); LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); LOG_INFO_(" to "); @@ -1136,6 +1151,7 @@ uip_process(uint8_t flag) uip_next_hdr = &UIP_IP_BUF->proto; uip_ext_len = 0; uip_ext_bitmap = 0; + if(*uip_next_hdr == UIP_PROTO_HBHO) { #if UIP_CONF_IPV6_CHECKS uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; @@ -1254,15 +1270,18 @@ uip_process(uint8_t flag) #if UIP_TCP case UIP_PROTO_TCP: /* TCP, for both IPv4 and IPv6 */ + uip_last_proto = *uip_next_hdr; goto tcp_input; #endif /* UIP_TCP */ #if UIP_UDP case UIP_PROTO_UDP: /* UDP, for both IPv4 and IPv6 */ + uip_last_proto = *uip_next_hdr; goto udp_input; #endif /* UIP_UDP */ case UIP_PROTO_ICMP6: /* ICMPv6 */ + uip_last_proto = *uip_next_hdr; goto icmp6_input; case UIP_PROTO_HBHO: LOG_DBG("Processing hbh header\n"); @@ -1469,7 +1488,7 @@ uip_process(uint8_t flag) /* UDP input processing. */ udp_input: - remove_ext_hdr(); + uip_remove_ext_hdr(); LOG_INFO("Receiving UDP packet\n"); @@ -1581,7 +1600,7 @@ uip_process(uint8_t flag) /* TCP input processing. */ tcp_input: - remove_ext_hdr(); + uip_remove_ext_hdr(); UIP_STAT(++uip_stat.tcp.recv); LOG_INFO("Receiving TCP packet\n"); diff --git a/os/net/ipv6/uipbuf.c b/os/net/ipv6/uipbuf.c index 6ccbb99aa..efa2413e9 100644 --- a/os/net/ipv6/uipbuf.c +++ b/os/net/ipv6/uipbuf.c @@ -38,45 +38,119 @@ static uint16_t uipbuf_attrs[UIPBUF_ATTR_MAX]; +/*---------------------------------------------------------------------------*/ +bool +uipbuf_add_ext_hdr(int16_t len) +{ + if(len + uip_len <= UIP_LINK_MTU && len + uip_len >= 0) { + uip_ext_len += len; + uip_len += len; + return true; + } else { + return false; + } +} +/*---------------------------------------------------------------------------*/ +bool +uipbuf_set_len(uint16_t len) +{ + if(len <= UIP_LINK_MTU) { + uip_len = len; + return true; + } else { + return false; + } +} /*---------------------------------------------------------------------------*/ /* Get the next header given the buffer - start indicates that this is start of the IPv6 header - needs to be set to 0 when in an ext hdr */ -uint8_t* -uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, uint8_t start) +uint8_t * +uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, bool start) { - int ext_len = 0; - struct uip_ip_hdr *ipbuf = (struct uip_ip_hdr *) buffer; - struct uip_ext_hdr *ext = NULL; + int curr_hdr_len = 0; + int next_hdr_len = 0; + uint8_t *next_header = NULL; + struct uip_ip_hdr *ipbuf = NULL; + struct uip_ext_hdr *curr_ext = NULL; + struct uip_ext_hdr *next_ext = NULL; if(start) { /* protocol in the IP buffer */ + ipbuf = (struct uip_ip_hdr *)buffer; *protocol = ipbuf->proto; - return buffer + UIP_IPH_LEN; + curr_hdr_len = UIP_IPH_LEN; } else { /* protocol in the Ext hdr */ - ext = (struct uip_ext_hdr *) buffer; - *protocol = ext->next; + curr_ext = (struct uip_ext_hdr *)buffer; + *protocol = curr_ext->next; /* This is just an ext header */ - ext_len = (ext->len << 3) + 8; - return buffer + ext_len; + curr_hdr_len = (curr_ext->len << 3) + 8; } + + /* Check first if enough space for current header */ + if(curr_hdr_len > size) { + return NULL; + } + next_header = buffer + curr_hdr_len; + + /* Check if the buffer is large enough for the next header */ + if(uip_is_proto_ext_hdr(*protocol)) { + next_ext = (struct uip_ext_hdr *)next_header; + next_hdr_len = (next_ext->len << 3) + 8; + } else { + if(*protocol == UIP_PROTO_TCP) { + next_hdr_len = UIP_TCPH_LEN; + } else if(*protocol == UIP_PROTO_UDP) { + next_hdr_len = UIP_UDPH_LEN; + } else if(*protocol == UIP_PROTO_ICMP6) { + next_hdr_len = UIP_ICMPH_LEN; + } + } + + /* Size must be enough to hold both the current and next header */ + if(next_hdr_len == 0 || curr_hdr_len + next_hdr_len > size) { + return NULL; + } + + return next_header; } /*---------------------------------------------------------------------------*/ /* Get the final header given the buffer - that is assumed to be at start of an IPv6 header */ -uint8_t* +uint8_t * uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol) { uint8_t *nbuf; - nbuf = uipbuf_get_next_header(buffer, size, protocol, 1); - while(uip_is_proto_ext_hdr(*protocol)) { - /* send in and move beyond the ext hdr */ - nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), protocol, 0); + nbuf = uipbuf_get_next_header(buffer, size, protocol, true); + while(nbuf != NULL && uip_is_proto_ext_hdr(*protocol)) { + /* move to the ext hdr */ + nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), protocol, false); } + + /* In case the buffer wasn't large enough for all headers, return NULL */ return nbuf; } /*---------------------------------------------------------------------------*/ +uint8_t * +uipbuf_search_header(uint8_t *buffer, uint16_t size, uint8_t protocol) +{ + uint8_t *nbuf; + uint8_t next_proto; + + nbuf = uipbuf_get_next_header(buffer, size, &next_proto, true); + while(nbuf != NULL && next_proto != protocol && uip_is_proto_ext_hdr(next_proto)) { + /* move to the ext hdr */ + nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), &next_proto, false); + } + + if(next_proto == protocol) { + return nbuf; + } else { + return NULL; + } +} +/*---------------------------------------------------------------------------*/ /** * Common functions for uipbuf (attributes, etc). * diff --git a/os/net/ipv6/uipbuf.h b/os/net/ipv6/uipbuf.h index 337ffe17f..a2e091015 100644 --- a/os/net/ipv6/uipbuf.h +++ b/os/net/ipv6/uipbuf.h @@ -35,17 +35,31 @@ #include "contiki.h" +/** + * \brief Update uip buffer length for addition of an extension header + * \param len The length of the new extension header + * \retval true if the length fields were successfully set, false otherwise + */ +bool uipbuf_add_ext_hdr(int16_t len); + +/** + * \brief Set the length of the uIP buffer + * \param len The new length + * \retval true if the len was successfully set, false otherwise + */ +bool uipbuf_set_len(uint16_t len); + /** * \brief Get the next IPv6 header. * \param buffer A pointer to the buffer holding the IPv6 packet * \param size The size of the data in the buffer * \param protocol A pointer to a variable where the protocol of the header will be stored * \param start A flag that indicates if this is expected to be the IPv6 packet header or a later header (Extension header) - * \retval returns address of the starting position of the next header + * \retval returns address of the next header, or NULL in case of insufficient buffer space * * This function moves to the next header in a IPv6 packet. */ -uint8_t* uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, uint8_t start); +uint8_t *uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, bool start); /** @@ -53,12 +67,22 @@ uint8_t* uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protoco * \param buffer A pointer to the buffer holding the IPv6 packet * \param size The size of the data in the buffer * \param protocol A pointer to a variable where the protocol of the header will be stored - * \retval returns address of the starting position of the next header + * \retval returns address of the last header, or NULL in case of insufficient buffer space * * This function moves to the last header of the IPv6 packet. */ -uint8_t* uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol); +uint8_t *uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol); +/** + * \brief Get an IPv6 header with a given protocol field. + * \param buffer A pointer to the buffer holding the IPv6 packet + * \param size The size of the data in the buffer + * \param protocol The protocol we are looking for + * \retval returns address of the header if found, else NULL + * + * This function moves to the last header of the IPv6 packet. + */ +uint8_t *uipbuf_search_header(uint8_t *buffer, uint16_t size, uint8_t protocol); /** * \brief Get the value of the attribute diff --git a/os/net/routing/nullrouting/nullrouting.c b/os/net/routing/nullrouting/nullrouting.c index bb1399fbb..3443dbb13 100644 --- a/os/net/routing/nullrouting/nullrouting.c +++ b/os/net/routing/nullrouting/nullrouting.c @@ -107,7 +107,7 @@ static void ext_header_remove(void) { #if NETSTACK_CONF_WITH_IPV6 - uip_ext_len = 0; + uip_remove_ext_hdr(); #endif /* NETSTACK_CONF_WITH_IPV6 */ } /*---------------------------------------------------------------------------*/ @@ -118,7 +118,7 @@ ext_header_update(void) } /*---------------------------------------------------------------------------*/ static int -ext_header_hbh_update(int uip_ext_opt_offset) +ext_header_hbh_update(int ext_offset, int opt_offset) { return 1; } diff --git a/os/net/routing/routing.h b/os/net/routing/routing.h index 7fe6e7ca8..be1fe0173 100644 --- a/os/net/routing/routing.h +++ b/os/net/routing/routing.h @@ -131,12 +131,14 @@ struct routing_driver { * Process and update the routing protocol hob-by-hop * extention headers of the current uIP packet. * - * \param uip_ext_opt_offset The offset within the uIP packet where + * \param ext_offset The offset within the uIP packet where * extension headers start + * \param opt_offset The offset within the extension header where + * the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ - int (* ext_header_hbh_update)(int uip_ext_opt_offset); + int (* ext_header_hbh_update)(int ext_offset, int opt_offset); /** * Process and update SRH in-place, * i.e. internal address swapping as per RFC6554 diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 6a99b573a..b175edd38 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -62,7 +62,7 @@ /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int uip_ext_opt_offset) +rpl_ext_header_hbh_update(int ext_offset, int opt_offset) { rpl_instance_t *instance; int down; @@ -71,25 +71,25 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uip_ds6_route_t *route; rpl_parent_t *sender = NULL; - if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type != UIP_EXT_HDR_OPT_RPL - || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { + if(UIP_HBHO_BUF(ext_offset)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type != UIP_EXT_HDR_OPT_RPL + || UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF(uip_ext_len)->len, - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type, - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len); + UIP_HBHO_BUF(ext_offset)->len, + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type, + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); + instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); if(instance == NULL) { LOG_ERR("Unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); return 0; } - if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("Forward error!\n"); /* We should try to repair it by removing the neighbor that caused the packet to be forwareded in the first place. We drop any @@ -113,14 +113,14 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) return 0; } down = 0; - if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN) { + if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_DOWN) { down = 1; } - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank); + sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->senderrank); sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR)) { + if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR)) { /* A rank error was signalled, attempt to repair it by updating * the sender's rank from ext header */ sender->rank = sender_rank; @@ -150,7 +150,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) instance->unicast_dio_target = sender; rpl_schedule_unicast_dio_immediately(instance); } - if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) { RPL_STAT(rpl_stats.loop_errors++); LOG_ERR(" Rank error signalled in RPL option!\n"); /* Packet must be dropped and dio trickle timer reset, see RFC6550 - 11.2.2.2 */ @@ -159,7 +159,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) } LOG_WARN("Single error tolerated\n"); RPL_STAT(rpl_stats.loop_warnings++); - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; return 1; } @@ -171,41 +171,19 @@ int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) { #if RPL_WITH_NON_STORING - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; rpl_dag_t *dag; uip_sr_node_t *dest_node; uip_sr_node_t *root_node; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing - * header. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); dag = rpl_get_dag(&UIP_IP_BUF->destipaddr); root_node = uip_sr_get_node(dag, &dag->dag_id); dest_node = uip_sr_get_node(dag, &UIP_IP_BUF->destipaddr); - if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) || + if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -214,11 +192,9 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * forwarding to next hop */ uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr); uip_create_linklocal_prefix(ipaddr); - uip_ext_len = last_uip_ext_len; return 1; } - uip_ext_len = last_uip_ext_len; return 0; #else /* RPL_WITH_NON_STORING */ return 0; /* SRH not found */ @@ -229,34 +205,13 @@ int rpl_ext_header_srh_update(void) { #if RPL_WITH_NON_STORING - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; + struct uip_rpl_srh_hdr *srh_header; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing - * header. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } - - if(uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) { + if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) { /* SRH found, now look for next hop */ uint8_t cmpri, cmpre; uint8_t ext_len; @@ -265,11 +220,12 @@ rpl_ext_header_srh_update(void) uint8_t segments_left; uip_ipaddr_t current_dest_addr; - segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; - ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF(uip_ext_len)->pad >> 4; + srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN); + segments_left = rh_header->seg_left; + ext_len = rh_header->len * 8 + 8; + cmpri = srh_header->cmpr >> 4; + cmpre = srh_header->cmpr & 0x0f; + padding = srh_header->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -280,7 +236,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address and address[i] */ @@ -293,17 +249,15 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF(uip_ext_len)->seg_left--; + rh_header->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); } - uip_ext_len = last_uip_ext_len; return 1; } - uip_ext_len = last_uip_ext_len; return 0; #else /* RPL_WITH_NON_STORING */ return 0; /* SRH not found */ @@ -408,7 +362,7 @@ insert_srh_header(void) path_len, cmpri, cmpre, ext_len, padding); /* Check if there is enough space to store the extension header */ - if(uip_len + ext_len > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + ext_len > UIP_LINK_MTU) { LOG_ERR("Packet too long: impossible to add source routing header (%u bytes)\n", ext_len); return 0; } @@ -418,23 +372,23 @@ insert_srh_header(void) uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); - /* Insert source routing header */ - UIP_RH_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; + /* Insert source routing header (as first ext header) */ + UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF(uip_ext_len)->len = (ext_len - 8) / 8; - UIP_RH_BUF(uip_ext_len)->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF(uip_ext_len)->seg_left = path_len; + UIP_RH_BUF(0)->len = (ext_len - 8) / 8; + UIP_RH_BUF(0)->routing_type = RPL_RH_TYPE_SRH; + UIP_RH_BUF(0)->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF(uip_ext_len)->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF(uip_ext_len)->pad = padding << 4; + UIP_RPL_SRH_BUF(0)->cmpr = (cmpri << 4) + cmpre; + UIP_RPL_SRH_BUF(0)->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)UIP_RH_BUF(0)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -456,8 +410,7 @@ insert_srh_header(void) UIP_IP_BUF->len[0]++; } - uip_ext_len += ext_len; - uip_len += ext_len; + uipbuf_add_ext_hdr(ext_len); return 1; } @@ -466,44 +419,37 @@ static int update_hbh_header(void) { rpl_instance_t *instance; - int uip_ext_opt_offset; - int last_uip_ext_len; rpl_parent_t *parent; + int opt_offset = 2; - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(UIP_HBHO_BUF(0)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { - - LOG_ERR("Hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len, - uip_ext_len); + LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n", + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); + instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance); if(instance == NULL || !instance->used || !instance->current_dag->joined) { LOG_ERR("Unable to add/update hop-by-hop extension header: incorrect instance\n"); - uip_ext_len = last_uip_ext_len; return 0; /* Drop */ } LOG_INFO("Updating RPL option\n"); /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = instance->instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = instance->instance_id; if(RPL_IS_STORING(instance)) { /* In non-storing mode, downwards traffic does not have the HBH option */ /* Check the direction of the down flag, as per Section 11.2.2.3, which states that if a packet is going down it should in general not go back up again. If this happens, a RPL_HDR_OPT_FWD_ERR should be flagged. */ - if((UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN)) { + if((UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags & RPL_HDR_OPT_DOWN)) { if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_FWD_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags |= RPL_HDR_OPT_FWD_ERR; LOG_WARN("RPL forwarding error\n"); /* We should send back the packet to the originating parent, but it is not feasible yet, so we send a No-Path DAO instead */ @@ -522,37 +468,30 @@ update_hbh_header(void) if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { /* No route was found, so this packet will go towards the RPL root. If so, we should not set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags &= ~RPL_HDR_OPT_DOWN; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags &= ~RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going up\n"); } else { /* A DAO route was found so we set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_DOWN; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags |= RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going down\n"); } } } } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ static int insert_hbh_header(const rpl_instance_t *instance) { - int uip_ext_opt_offset; - int last_uip_ext_len; uint8_t temp_len; - - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + int opt_offset = 2; /* Insert hop-by-hop header */ LOG_DBG("Creating hop-by-hop option\n"); - if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) { LOG_ERR("Packet too long: impossible to add hop-by-hop option\n"); - uip_ext_len = last_uip_ext_len; return 0; } @@ -560,25 +499,25 @@ insert_hbh_header(const rpl_instance_t *instance) memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); - /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; + /* Insert HBH header (as first ext header) */ + UIP_HBHO_BUF(0)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = instance->instance_id; - uip_len += RPL_HOP_BY_HOP_LEN; + UIP_HBHO_BUF(0)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len = RPL_HDR_OPT_LEN; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags = 0; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = instance->instance_id; + temp_len = UIP_IP_BUF->len[1]; UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; if(UIP_IP_BUF->len[1] < temp_len) { UIP_IP_BUF->len[0]++; } - uip_ext_len = last_uip_ext_len + RPL_HOP_BY_HOP_LEN; + uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); /* Update header before returning */ return update_hbh_header(); @@ -587,52 +526,34 @@ insert_hbh_header(const rpl_instance_t *instance) void rpl_ext_header_remove(void) { - uint8_t temp_len; - uint8_t rpl_ext_hdr_len; - int uip_ext_opt_offset; - uint8_t *uip_next_hdr; + uint8_t *prev_proto_ptr; + uint8_t protocol; + uint8_t ext_len; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; + struct uip_ext_hdr_opt *opt_ptr; - uip_ext_len = 0; - uip_ext_opt_offset = 2; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for hop-by-hop and routing headers */ - while(uip_next_hdr != NULL) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL)) { - /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; - rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; - temp_len = UIP_IP_BUF->len[1]; - uip_len -= rpl_ext_hdr_len; - UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; - if(UIP_IP_BUF->len[1] > temp_len) { - UIP_IP_BUF->len[0]--; - } - LOG_DBG("Removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF(uip_ext_len), ((uint8_t *)UIP_EXT_BUF(uip_ext_len)) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); - } else { - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - } - break; - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, any header other than the Destination - * Options header does not appear between the Hop-by-Hop - * Options header and the Routing header. - * - * We're moving to the next header only if uip_next_hdr has - * UIP_PROTO_DESTO. Otherwise, we'll return. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - return; + next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &UIP_IP_BUF->proto; + while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { + opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2); + if(protocol == UIP_PROTO_ROUTING || (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) { + /* Remove ext header */ + *prev_proto_ptr = ext_ptr->next; + ext_len = ext_ptr->len * 8 + 8; + uipbuf_add_ext_hdr(-ext_len); + /* Update length field and rest of packer to the "left" */ + UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); + UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); + /* Update loop variables */ + protocol = *prev_proto_ptr; + } else { + /* move to the ext hdr */ + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &ext_ptr->next; } } } diff --git a/os/net/routing/rpl-classic/rpl.h b/os/net/routing/rpl-classic/rpl.h index db87cc824..170c97e58 100644 --- a/os/net/routing/rpl-classic/rpl.h +++ b/os/net/routing/rpl-classic/rpl.h @@ -275,7 +275,7 @@ rpl_dag_t *rpl_get_dag(const uip_ipaddr_t *addr); rpl_dag_t *rpl_get_any_dag(void); rpl_instance_t *rpl_get_instance(uint8_t instance_id); int rpl_ext_header_update(void); -int rpl_ext_header_hbh_update(int); +int rpl_ext_header_hbh_update(int, int); void rpl_insert_header(void); void rpl_ext_header_remove(void); const struct link_stats *rpl_get_parent_link_stats(rpl_parent_t *p); diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 02a681476..26d13c8ec 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -57,31 +57,12 @@ int rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) { - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; uip_sr_node_t *dest_node; uip_sr_node_t *root_node; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing header. - */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); if(!rpl_is_addr_in_our_dag(&UIP_IP_BUF->destipaddr)) { return 0; @@ -90,8 +71,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) root_node = uip_sr_get_node(NULL, &curr_instance.dag.dag_id); dest_node = uip_sr_get_node(NULL, &UIP_IP_BUF->destipaddr); - if((uip_next_hdr != NULL && *uip_next_hdr == UIP_PROTO_ROUTING - && UIP_RH_BUF(uip_ext_len)->routing_type == RPL_RH_TYPE_SRH) || + if((rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) || (dest_node != NULL && root_node != NULL && dest_node->parent == root_node)) { /* Routing header found or the packet destined for a direct child of the root. @@ -100,20 +80,18 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) * forwarding to next hop */ uip_ipaddr_copy(ipaddr, &UIP_IP_BUF->destipaddr); uip_create_linklocal_prefix(ipaddr); - uip_ext_len = last_uip_ext_len; return 1; } LOG_DBG("no SRH found\n"); - uip_ext_len = last_uip_ext_len; return 0; } /*---------------------------------------------------------------------------*/ int rpl_ext_header_srh_update(void) { - uint8_t *uip_next_hdr; - int last_uip_ext_len = uip_ext_len; + struct uip_routing_hdr *rh_header; + struct uip_rpl_srh_hdr *srh_header; uint8_t cmpri, cmpre; uint8_t ext_len; uint8_t padding; @@ -121,40 +99,21 @@ rpl_ext_header_srh_update(void) uint8_t segments_left; uip_ipaddr_t current_dest_addr; - uip_ext_len = 0; - uip_next_hdr = &UIP_IP_BUF->proto; + /* Look for routing ext header */ + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); - /* Look for routing header */ - while(uip_next_hdr != NULL && *uip_next_hdr != UIP_PROTO_ROUTING) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, only the Hop-by-Hop Options header and - * Destination Options header can appear before the Routing header. - */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - uip_next_hdr = NULL; - break; - } - } - - if(uip_next_hdr == NULL || *uip_next_hdr != UIP_PROTO_ROUTING - || UIP_RH_BUF(uip_ext_len)->routing_type != RPL_RH_TYPE_SRH) { + if(rh_header == NULL || rh_header->routing_type != RPL_RH_TYPE_SRH) { LOG_INFO("SRH not found\n"); - uip_ext_len = last_uip_ext_len; return 0; } /* Parse SRH */ - segments_left = UIP_RH_BUF(uip_ext_len)->seg_left; - ext_len = (UIP_RH_BUF(uip_ext_len)->len * 8) + 8; - cmpri = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr >> 4; - cmpre = UIP_RPL_SRH_BUF(uip_ext_len)->cmpr & 0x0f; - padding = UIP_RPL_SRH_BUF(uip_ext_len)->pad >> 4; + srh_header = (struct uip_rpl_srh_hdr *)(((uint8_t *)rh_header) + RPL_RH_LEN); + segments_left = rh_header->seg_left; + ext_len = rh_header->len * 8 + 8; + cmpri = srh_header->cmpr >> 4; + cmpre = srh_header->cmpr & 0x0f; + padding = srh_header->pad >> 4; path_len = ((ext_len - padding - RPL_RH_LEN - RPL_SRH_LEN - (16 - cmpre)) / (16 - cmpri)) + 1; (void)path_len; @@ -166,7 +125,7 @@ rpl_ext_header_srh_update(void) /* We are the final destination, do nothing */ } else { uint8_t i = path_len - segments_left; /* The index of the next address to be visited */ - uint8_t *addr_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); + uint8_t *addr_ptr = ((uint8_t *)rh_header) + RPL_RH_LEN + RPL_SRH_LEN + (i * (16 - cmpri)); uint8_t cmpr = segments_left == 1 ? cmpre : cmpri; /* As per RFC6554: swap the IPv6 destination address with address[i] */ @@ -179,14 +138,13 @@ rpl_ext_header_srh_update(void) memcpy(addr_ptr, ((uint8_t *)¤t_dest_addr) + cmpr, 16 - cmpr); /* Update segments left field */ - UIP_RH_BUF(uip_ext_len)->seg_left--; + rh_header->seg_left--; LOG_INFO("SRH next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ @@ -293,7 +251,7 @@ insert_srh_header(void) path_len, cmpri, cmpre, ext_len, padding); /* Check if there is enough space to store the extension header */ - if(uip_len + ext_len > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + ext_len > UIP_LINK_MTU) { LOG_ERR("packet too long: impossible to add source routing header (%u bytes)\n", ext_len); return 0; } @@ -303,23 +261,23 @@ insert_srh_header(void) uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); - /* Insert source routing header */ - UIP_RH_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; + /* Insert source routing header (as first ext header) */ + UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF(uip_ext_len)->len = (ext_len - 8) / 8; - UIP_RH_BUF(uip_ext_len)->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF(uip_ext_len)->seg_left = path_len; + UIP_RH_BUF(0)->len = (ext_len - 8) / 8; + UIP_RH_BUF(0)->routing_type = RPL_RH_TYPE_SRH; + UIP_RH_BUF(0)->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF(uip_ext_len)->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF(uip_ext_len)->pad = padding << 4; + UIP_RPL_SRH_BUF(0)->cmpr = (cmpri << 4) + cmpre; + UIP_RPL_SRH_BUF(0)->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF(uip_ext_len)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)UIP_RH_BUF(0)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); @@ -341,14 +299,13 @@ insert_srh_header(void) UIP_IP_BUF->len[0]++; } - uip_ext_len += ext_len; - uip_len += ext_len; + uipbuf_add_ext_hdr(ext_len); return 1; } /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int uip_ext_opt_offset) +rpl_ext_header_hbh_update(int ext_offset, int opt_offset) { int down; int rank_error_signaled; @@ -356,32 +313,32 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) uint16_t sender_rank; uint8_t sender_closer; rpl_nbr_t *sender; - uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type; - uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len; + uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type; + uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len; - if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + if(UIP_HBHO_BUF(ext_offset)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) || opt_type != UIP_EXT_HDR_OPT_RPL || opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF(uip_ext_len)->len, opt_type, opt_len); + UIP_HBHO_BUF(ext_offset)->len, opt_type, opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance) { + if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance) { LOG_ERR("unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance); + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); return 0; /* Drop */ } - if(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { + if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("forward error!\n"); return 0; /* Drop */ } - down = (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank); + down = (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; + sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->senderrank); sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; + rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; sender_closer = sender_rank < curr_instance.dag.rank; loop_detected = (down && !sender_closer) || (!down && sender_closer); @@ -395,7 +352,7 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) if(loop_detected) { /* Set forward error flag */ - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; + UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; } return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled); @@ -407,34 +364,27 @@ rpl_ext_header_hbh_update(int uip_ext_opt_offset) static int update_hbh_header(void) { - int uip_ext_opt_offset; - int last_uip_ext_len; + int opt_offset = 2; - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(UIP_HBHO_BUF(0)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF(uip_ext_len)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len != RPL_HDR_OPT_LEN) { - - LOG_ERR("hop-by-hop extension header has wrong size (%u %u)\n", - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len, uip_ext_len); + LOG_ERR("hop-by-hop extension header has wrong size (%u)\n", + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance) { + if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance) { LOG_ERR("unable to add/update hop-by-hop extension header: incorrect instance\n"); - uip_ext_len = last_uip_ext_len; return 0; /* Drop */ } /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = curr_instance.instance_id; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = curr_instance.instance_id; } - uip_ext_len = last_uip_ext_len; return 1; } /*---------------------------------------------------------------------------*/ @@ -445,19 +395,13 @@ update_hbh_header(void) static int insert_hbh_header(void) { - int uip_ext_opt_offset; - int last_uip_ext_len; uint8_t temp_len; - - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - uip_ext_opt_offset = 2; + int opt_offset = 2; /* Insert hop-by-hop header */ LOG_INFO("creating hop-by-hop option\n"); - if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + RPL_HOP_BY_HOP_LEN > UIP_LINK_MTU) { LOG_ERR("packet too long: impossible to add hop-by-hop option\n"); - uip_ext_len = last_uip_ext_len; return 0; } @@ -465,25 +409,25 @@ insert_hbh_header(void) memmove(UIP_IP_PAYLOAD(RPL_HOP_BY_HOP_LEN), UIP_IP_PAYLOAD(0), uip_len - UIP_IPH_LEN); memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); - /* Update IP and HBH protocol and fields */ - UIP_HBHO_BUF(uip_ext_len)->next = UIP_IP_BUF->proto; + /* Insert HBH header (as first ext header) */ + UIP_HBHO_BUF(0)->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF(uip_ext_len)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->instance = curr_instance.instance_id; - uip_len += RPL_HOP_BY_HOP_LEN; + UIP_HBHO_BUF(0)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len = RPL_HDR_OPT_LEN; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags = 0; + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); + UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = curr_instance.instance_id; + temp_len = UIP_IP_BUF->len[1]; UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; if(UIP_IP_BUF->len[1] < temp_len) { UIP_IP_BUF->len[0]++; } - uip_ext_len = last_uip_ext_len + RPL_HOP_BY_HOP_LEN; + uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); /* Update header before returning */ return update_hbh_header(); @@ -520,54 +464,35 @@ rpl_ext_header_update(void) void rpl_ext_header_remove(void) { - uint8_t temp_len; - uint8_t rpl_ext_hdr_len; - int uip_ext_opt_offset; - uint8_t *uip_next_hdr; + uint8_t *prev_proto_ptr; + uint8_t protocol; + uint8_t ext_len; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; + struct uip_ext_hdr_opt *opt_ptr; - uip_ext_len = 0; - uip_ext_opt_offset = 2; - uip_next_hdr = &UIP_IP_BUF->proto; - - /* Look for hop-by-hop and routing headers */ - while(uip_next_hdr != NULL) { - switch(*uip_next_hdr) { - case UIP_PROTO_HBHO: - case UIP_PROTO_ROUTING: - if((*uip_next_hdr != UIP_PROTO_HBHO || UIP_EXT_HDR_OPT_RPL_BUF(uip_ext_len, uip_ext_opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL)) { - /* Remove hop-by-hop and routing headers */ - *uip_next_hdr = UIP_EXT_BUF(uip_ext_len)->next; - rpl_ext_hdr_len = (UIP_EXT_BUF(uip_ext_len)->len * 8) + 8; - temp_len = UIP_IP_BUF->len[1]; - uip_len -= rpl_ext_hdr_len; - UIP_IP_BUF->len[1] -= rpl_ext_hdr_len; - if(UIP_IP_BUF->len[1] > temp_len) { - UIP_IP_BUF->len[0]--; - } - LOG_INFO("removing RPL extension header (type %u, len %u)\n", *uip_next_hdr, rpl_ext_hdr_len); - memmove(UIP_EXT_BUF(uip_ext_len), ((uint8_t *)UIP_EXT_BUF(uip_ext_len)) + rpl_ext_hdr_len, uip_len - UIP_IPH_LEN); - } else { - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - } - break; - case UIP_PROTO_DESTO: - /* - * As per RFC 2460, any header other than the Destination - * Options header does not appear between the Hop-by-Hop - * Options header and the Routing header. - * - * We're moving to the next header only if uip_next_hdr has - * UIP_PROTO_DESTO. Otherwise, we'll return. - */ - /* Move to next header */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - default: - return; + next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &UIP_IP_BUF->proto; + while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { + opt_ptr = (struct uip_ext_hdr_opt *)(next_header + 2); + if(protocol == UIP_PROTO_ROUTING || (protocol == UIP_PROTO_HBHO && opt_ptr->type == UIP_EXT_HDR_OPT_RPL)) { + /* Remove ext header */ + *prev_proto_ptr = ext_ptr->next; + ext_len = ext_ptr->len * 8 + 8; + uipbuf_add_ext_hdr(-ext_len); + /* Update length field and rest of packer to the "left" */ + UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); + UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); + /* Update loop variables */ + protocol = *prev_proto_ptr; + } else { + /* move to the ext hdr */ + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false); + ext_ptr = (struct uip_ext_hdr *)next_header; + prev_proto_ptr = &ext_ptr->next; } } } - /** @}*/ diff --git a/os/net/routing/rpl-lite/rpl-ext-header.h b/os/net/routing/rpl-lite/rpl-ext-header.h index df61bc325..7576c6a57 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.h +++ b/os/net/routing/rpl-lite/rpl-ext-header.h @@ -63,12 +63,14 @@ int rpl_ext_header_srh_update(void); * Process and update the RPL hop-by-hop extension headers of * the current uIP packet. * -* \param uip_ext_opt_offset The offset within the uIP packet where +* \param ext_offset The offset within the uIP packet where * extension headers start +* \param opt_offset The offset within the extension header where +* the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ -int rpl_ext_header_hbh_update(int uip_ext_opt_offset); +int rpl_ext_header_hbh_update(int ext_offset, int opt_offset); /** * Adds/updates all RPL extension headers to current uIP packet. From b4d45d7f563a1f3a61186995b0dd07ee5d59b5ef Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 23:28:58 +0200 Subject: [PATCH 17/28] Removed comment that was no longer necessary --- os/net/ipv6/uip-nd6.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 9fc7429fe..7728f2fe2 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -84,14 +84,6 @@ /*------------------------------------------------------------------*/ /** @{ */ /** \name Pointers to the header structures. - * All pointers except UIP_IP_BUF depend on uip_ext_len, which at - * packet reception, is the total length of the extension headers. - * - * The pointer to ND6 options header also depends on nd6_opt_offset, - * which we set in each function. - * - * Care should be taken when manipulating these buffers about the - * value of these length variables */ /**@{ Pointers to messages just after icmp header */ From d4bc09e0027b30bc398536b6403917e5a1c58d1b Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 23:33:02 +0200 Subject: [PATCH 18/28] Move function for clearing uip_buf to uipbuf module --- examples/slip-radio/slip-radio.c | 2 +- os/net/ipv6/multicast/esmrf.c | 4 ++-- os/net/ipv6/multicast/roll-tm.c | 2 +- os/net/ipv6/multicast/smrf.c | 2 +- os/net/ipv6/tcpip.c | 8 ++++---- os/net/ipv6/uip-icmp6.c | 8 ++++---- os/net/ipv6/uip-nd6.c | 14 ++++++------- os/net/ipv6/uip.h | 13 ------------ os/net/ipv6/uip6.c | 12 +++++------ os/net/ipv6/uipbuf.c | 9 +++++++++ os/net/ipv6/uipbuf.h | 5 +++++ os/net/routing/rpl-classic/rpl-icmp6.c | 20 +++++++++---------- os/net/routing/rpl-lite/rpl-icmp6.c | 8 ++++---- os/services/ip64/ip64-slip-interface.c | 6 +++--- .../rpl-border-router/embedded/slip-bridge.c | 8 ++++---- tools/sky/uip6-bridge/sicslow_ethernet.c | 12 +++++------ tools/sky/uip6-bridge/uip6-bridge-tap.c | 2 +- 17 files changed, 68 insertions(+), 67 deletions(-) diff --git a/examples/slip-radio/slip-radio.c b/examples/slip-radio/slip-radio.c index a20bd8676..84045e7e1 100644 --- a/examples/slip-radio/slip-radio.c +++ b/examples/slip-radio/slip-radio.c @@ -231,7 +231,7 @@ slip_input_callback(void) if(!cmd_input(&uip_buf[UIP_LLH_LEN], uip_len)) { cmd_send((uint8_t *)"EUnknown command", 16); } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index 746f2f03b..b418d5322 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -244,7 +244,7 @@ icmp_input() /* If we enter here, we will definitely forward */ tcpip_ipv6_output(); } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void @@ -254,7 +254,7 @@ mcast_fwd(void *p) uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index c942dfd46..cc0b09ac2 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -1376,7 +1376,7 @@ out() drop: uip_slen = 0; - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t diff --git a/os/net/ipv6/multicast/smrf.c b/os/net/ipv6/multicast/smrf.c index bb23b060b..c0e3bf0d0 100644 --- a/os/net/ipv6/multicast/smrf.c +++ b/os/net/ipv6/multicast/smrf.c @@ -84,7 +84,7 @@ mcast_fwd(void *p) uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static uint8_t diff --git a/os/net/ipv6/tcpip.c b/os/net/ipv6/tcpip.c index d78cae510..16176e6e2 100644 --- a/os/net/ipv6/tcpip.c +++ b/os/net/ipv6/tcpip.c @@ -125,7 +125,7 @@ tcpip_output(const uip_lladdr_t *a) return ret; } else { /* Ok, ignore and drop... */ - uip_clear_buf(); + uipbuf_clear(); return 0; } } @@ -448,7 +448,7 @@ tcpip_input(void) NETSTACK_IP_PROCESS) { process_post_synch(&tcpip_process, PACKET_INPUT, NULL); } /* else - do nothing and drop */ - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void @@ -652,7 +652,7 @@ tcpip_ipv6_output(void) if(!NETSTACK_ROUTING.ext_header_update()) { /* Packet can not be forwarded */ LOG_ERR("output: routing protocol extension header update error\n"); - uip_clear_buf(); + uipbuf_clear(); return; } @@ -740,7 +740,7 @@ send_packet: } exit: - uip_clear_buf(); + uipbuf_clear(); return; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index c4003352f..a7e42c55c 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -156,7 +156,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { /* check if originating packet is not an ICMP error */ uint16_t shift; if(uip_last_proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) { - uip_clear_buf(); + uipbuf_clear(); return; } @@ -185,7 +185,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { /* the source should not be unspecified nor multicast, the check for multicast is done in uip_process */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){ - uip_clear_buf(); + uipbuf_clear(); return; } @@ -195,7 +195,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { if(type == ICMP6_PARAM_PROB && code == ICMP6_PARAMPROB_OPTION){ uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr); } else { - uip_clear_buf(); + uipbuf_clear(); return; } } else { @@ -286,7 +286,7 @@ echo_reply_input(void) } } - uip_clear_buf(); + uipbuf_clear(); return; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 7728f2fe2..52e19eff5 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -314,7 +314,7 @@ create_na: #if UIP_CONF_ROUTER flags = flags | UIP_ND6_NA_FLAG_ROUTER; #endif - uip_clear_buf(); + uipbuf_clear(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; @@ -348,7 +348,7 @@ create_na: return; discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* UIP_ND6_SEND_NA */ @@ -359,7 +359,7 @@ discard: void uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) { - uip_clear_buf(); + uipbuf_clear(); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; @@ -388,7 +388,7 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) } if (uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { LOG_ERR("Dropping NS due to no suitable source address\n"); - uip_clear_buf(); + uipbuf_clear(); return; } UIP_IP_BUF->len[1] = @@ -595,7 +595,7 @@ na_input(void) #endif /*UIP_CONF_IPV6_QUEUE_PKT */ discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* UIP_ND6_SEND_NS */ @@ -691,7 +691,7 @@ rs_input(void) uip_ds6_send_ra_sollicited(); discard: - uip_clear_buf(); + uipbuf_clear(); return; } @@ -1082,7 +1082,7 @@ ra_input(void) #endif /*UIP_CONF_IPV6_QUEUE_PKT */ discard: - uip_clear_buf(); + uipbuf_clear(); return; } #endif /* !UIP_CONF_ROUTER */ diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 49a7a0b21..9deedb726 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -1340,19 +1340,6 @@ extern uint8_t uip_last_proto; extern uint16_t uip_urglen, uip_surglen; #endif /* UIP_URGDATA > 0 */ -/* - * Clear uIP buffer - * - * This function clears the uIP buffer by reseting the uip_len and - * uip_ext_len pointers. - */ -#define uip_clear_buf() { \ - uip_len = 0; \ - uip_ext_len = 0; \ - uip_last_proto = 0; \ - uipbuf_clear_attr();\ -} - /** * Representation of a uIP TCP connection. * diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 4ded21bdc..dce44552b 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -511,7 +511,7 @@ uip_remove_ext_hdr(void) uip_ext_len, uip_len); if(uip_len < UIP_IPH_LEN + uip_ext_len) { LOG_ERR("uip_len too short compared to ext len\n"); - uip_clear_buf(); + uipbuf_clear(); return; } last_uip_ext_len = uip_ext_len; @@ -802,7 +802,7 @@ uip_reass_over(void) * any RFC, we decided not to include it as it reduces the size of * the packet. */ - uip_clear_buf(); + uipbuf_clear(); memcpy(UIP_IP_BUF, FBUF, UIP_IPH_LEN); /* copy the header for src and dest address*/ uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY, 0); @@ -948,7 +948,7 @@ uip_process(uint8_t flag) } else if(flag == UIP_TIMER) { /* Reset the length variables. */ #if UIP_TCP - uip_clear_buf(); + uipbuf_clear(); uip_slen = 0; /* Increase the initial sequence number. */ @@ -1473,7 +1473,7 @@ uip_process(uint8_t flag) LOG_ERR("Unknown ICMPv6 message type/code %d\n", UIP_ICMP_BUF->type); UIP_STAT(++uip_stat.icmp.drop); UIP_STAT(++uip_stat.icmp.typeerr); - uip_clear_buf(); + uipbuf_clear(); } if(uip_len > 0) { @@ -1989,7 +1989,7 @@ uip_process(uint8_t flag) uip_add_rcv_nxt(1); uip_flags = UIP_CONNECTED | UIP_NEWDATA; uip_connr->len = 0; - uip_clear_buf(); + uipbuf_clear(); uip_slen = 0; UIP_APPCALL(); goto appsend; @@ -2322,7 +2322,7 @@ uip_process(uint8_t flag) return; drop: - uip_clear_buf(); + uipbuf_clear(); uip_ext_bitmap = 0; uip_flags = 0; return; diff --git a/os/net/ipv6/uipbuf.c b/os/net/ipv6/uipbuf.c index efa2413e9..876385890 100644 --- a/os/net/ipv6/uipbuf.c +++ b/os/net/ipv6/uipbuf.c @@ -38,6 +38,15 @@ static uint16_t uipbuf_attrs[UIPBUF_ATTR_MAX]; +/*---------------------------------------------------------------------------*/ +void +uipbuf_clear(void) +{ + uip_len = 0; + uip_ext_len = 0; + uip_last_proto = 0; + uipbuf_clear_attr(); +} /*---------------------------------------------------------------------------*/ bool uipbuf_add_ext_hdr(int16_t len) diff --git a/os/net/ipv6/uipbuf.h b/os/net/ipv6/uipbuf.h index a2e091015..3ced3cd45 100644 --- a/os/net/ipv6/uipbuf.h +++ b/os/net/ipv6/uipbuf.h @@ -35,6 +35,11 @@ #include "contiki.h" +/** + * \brief Resets uIP buffer + */ +void uipbuf_clear(void); + /** * \brief Update uip buffer length for addition of an extension header * \param len The length of the new extension header diff --git a/os/net/routing/rpl-classic/rpl-icmp6.c b/os/net/routing/rpl-classic/rpl-icmp6.c index 5230ab4e4..baa6b08a7 100644 --- a/os/net/routing/rpl-classic/rpl-icmp6.c +++ b/os/net/routing/rpl-classic/rpl-icmp6.c @@ -248,7 +248,7 @@ dis_input(void) } } } - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -468,7 +468,7 @@ dio_input(void) rpl_process_dio(&from, &dio); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -807,7 +807,7 @@ dao_input_storing(void) /* independent if we remove or not - ACK the request */ if(flags & RPL_DAO_K_FLAG) { /* indicate that we accepted the no-path DAO */ - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -897,7 +897,7 @@ fwd_dao: } if(should_ack) { LOG_DBG("Sending DAO ACK\n"); - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -1011,7 +1011,7 @@ dao_input_nonstoring(void) if(flags & RPL_DAO_K_FLAG) { LOG_DBG("Sending DAO ACK\n"); - uip_clear_buf(); + uipbuf_clear(); dao_ack_output(instance, &dao_sender_addr, sequence, RPL_DAO_ACK_UNCONDITIONAL_ACCEPT); } @@ -1044,7 +1044,7 @@ dao_input(void) } discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ #if RPL_WITH_DAO_ACK @@ -1283,7 +1283,7 @@ dao_ack_input(void) instance = rpl_get_instance(instance_id); if(instance == NULL) { - uip_clear_buf(); + uipbuf_clear(); return; } @@ -1291,7 +1291,7 @@ dao_ack_input(void) parent = rpl_find_parent(instance->current_dag, &UIP_IP_BUF->srcipaddr); if(parent == NULL) { /* not a known instance - drop the packet and ignore */ - uip_clear_buf(); + uipbuf_clear(); return; } } else { @@ -1300,7 +1300,7 @@ dao_ack_input(void) if(instance->current_dag->rank == ROOT_RANK(instance)) { LOG_DBG("DODAG root received a DAO ACK, ignoring it\n"); - uip_clear_buf(); + uipbuf_clear(); return; } @@ -1360,7 +1360,7 @@ dao_ack_input(void) } } #endif /* RPL_WITH_DAO_ACK */ - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void diff --git a/os/net/routing/rpl-lite/rpl-icmp6.c b/os/net/routing/rpl-lite/rpl-icmp6.c index 260f9b168..d376ff5e8 100644 --- a/os/net/routing/rpl-lite/rpl-icmp6.c +++ b/os/net/routing/rpl-lite/rpl-icmp6.c @@ -143,7 +143,7 @@ dis_input(void) rpl_process_dis(&UIP_IP_BUF->srcipaddr, uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -325,7 +325,7 @@ dio_input(void) rpl_process_dio(&from, &dio); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -536,7 +536,7 @@ dao_input(void) rpl_process_dao(&from, &dao); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void @@ -647,7 +647,7 @@ dao_ack_input(void) rpl_process_dao_ack(sequence, status); discard: - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ void diff --git a/os/services/ip64/ip64-slip-interface.c b/os/services/ip64/ip64-slip-interface.c index 665a342d9..209dfcb91 100644 --- a/os/services/ip64/ip64-slip-interface.c +++ b/os/services/ip64/ip64-slip-interface.c @@ -57,7 +57,7 @@ input_callback(void) /*PRINTF("SIN: %u\n", uip_len);*/ if(uip_buf[UIP_LLH_LEN] == '!') { PRINTF("Got configuration message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - uip_clear_buf(); + uipbuf_clear(); #if 0 if(uip_buf[UIP_LLH_LEN + 1] == 'P') { uip_ipaddr_t prefix; @@ -84,7 +84,7 @@ input_callback(void) uip_len = 18; slip_write(uip_buf, uip_len); } - uip_clear_buf(); + uipbuf_clear(); } else { /* Save the last sender received over SLIP to avoid bouncing the @@ -98,7 +98,7 @@ input_callback(void) uip_len = len; /* PRINTF("send len %d\n", len); */ } else { - uip_clear_buf(); + uipbuf_clear(); } } } diff --git a/os/services/rpl-border-router/embedded/slip-bridge.c b/os/services/rpl-border-router/embedded/slip-bridge.c index ff980ea69..6ce3f79fd 100644 --- a/os/services/rpl-border-router/embedded/slip-bridge.c +++ b/os/services/rpl-border-router/embedded/slip-bridge.c @@ -61,7 +61,7 @@ request_prefix(void) uip_buf[1] = 'P'; uip_len = 2; slip_write(uip_buf, uip_len); - uip_clear_buf(); + uipbuf_clear(); } /*---------------------------------------------------------------------------*/ static void @@ -77,14 +77,14 @@ slip_input_callback(void) memset(&prefix, 0, 16); memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8); - uip_clear_buf(); + uipbuf_clear(); LOG_INFO("Setting prefix "); LOG_INFO_6ADDR(&prefix); LOG_INFO_("\n"); set_prefix_64(&prefix); } - uip_clear_buf(); + uipbuf_clear(); } else if(uip_buf[UIP_LLH_LEN] == '?') { LOG_INFO("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); @@ -100,7 +100,7 @@ slip_input_callback(void) uip_len = 18; slip_write(uip_buf, uip_len); } - uip_clear_buf(); + uipbuf_clear(); } else { /* Save the last sender received over SLIP to avoid bouncing the packet back if no route is found */ diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 5274a7833..4acd660ff 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -236,13 +236,13 @@ void mac_ethernetToLowpan(uint8_t * ethHeader) if (((struct uip_eth_hdr *) ethHeader)->type != UIP_HTONS(UIP_ETHTYPE_IPV6)) { PRINTF("eth2low: Packet is not IPv6, dropping\n"); /* rndis_stat.txbad++; */ - uip_clear_buf(); + uipbuf_clear(); return; } // In sniffer mode we don't ever send anything if (usbstick_mode.sendToRf == 0) { - uip_clear_buf(); + uipbuf_clear(); return; } @@ -261,7 +261,7 @@ void mac_ethernetToLowpan(uint8_t * ethHeader) /* IPv6 does not use broadcast addresses, hence this should not happen */ PRINTF("eth2low: Ethernet broadcast address received, should not happen?\n"); /* rndis_stat.txbad++; */ - uip_clear_buf(); + uipbuf_clear(); return; } else { PRINTF("eth2low: Addressed packet received... "); @@ -269,7 +269,7 @@ void mac_ethernetToLowpan(uint8_t * ethHeader) if (mac_createSicslowpanLongAddr( &(((struct uip_eth_hdr *) ethHeader)->dest.addr[0]), &destAddr) == 0) { PRINTF(" translation failed\n"); /* rndis_stat.txbad++; */ - uip_clear_buf(); + uipbuf_clear(); return; } PRINTF(" translated OK\n"); @@ -293,7 +293,7 @@ void mac_ethernetToLowpan(uint8_t * ethHeader) } } - uip_clear_buf(); + uipbuf_clear(); } @@ -341,7 +341,7 @@ void mac_LowpanToEthernet(void) /* rndis_send(uip_buf, uip_len, 1); */ /* rndis_stat.rxok++; */ -/* uip_clear_buf(); */ +/* uipbuf_clear(); */ } /** diff --git a/tools/sky/uip6-bridge/uip6-bridge-tap.c b/tools/sky/uip6-bridge/uip6-bridge-tap.c index 19ce22a6f..3386c2039 100644 --- a/tools/sky/uip6-bridge/uip6-bridge-tap.c +++ b/tools/sky/uip6-bridge/uip6-bridge-tap.c @@ -87,7 +87,7 @@ tcpip_input(void) if(uip_len > 0) { slip_write(uip_buf, uip_len); leds_toggle(LEDS_RED); - uip_clear_buf(); + uipbuf_clear(); } } } From 4018b31123e0f7681c75a8933cb4ba6f38ea2eaa Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 17 Oct 2018 23:35:58 +0200 Subject: [PATCH 19/28] Removed need for global uip_ext_opt_offset --- os/net/ipv6/uip6.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index dce44552b..c46ab3968 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -130,8 +130,6 @@ uint8_t uip_ext_bitmap = 0; * a header */ uint8_t uip_ext_len = 0; -/** \brief length of the header options read */ -uint8_t uip_ext_opt_offset = 0; /** \brief The final protocol after IPv6 extension headers: * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */ uint8_t uip_last_proto = 0; @@ -839,9 +837,9 @@ ext_hdr_options_process(void) * 8 bytes, excluding the first 8 bytes * length field in an option : the length of data in the option */ - uip_ext_opt_offset = 2; - while(uip_ext_opt_offset < ((UIP_EXT_BUF(uip_ext_len)->len << 3) + 8)) { - switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type) { + uint8_t ext_opt_offset = 2; + while(ext_opt_offset < ((UIP_EXT_BUF(uip_ext_len)->len << 3) + 8)) { + switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type) { /* * for now we do not support any options except padding ones * PAD1 does not make sense as the header must be 8bytes aligned, @@ -849,11 +847,11 @@ ext_hdr_options_process(void) */ case UIP_EXT_HDR_OPT_PAD1: LOG_DBG("Processing PAD1 option\n"); - uip_ext_opt_offset += 1; + ext_opt_offset += 1; break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - uip_ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(uip_ext_len, uip_ext_opt_offset)->opt_len + 2; + ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(uip_ext_len, ext_opt_offset)->opt_len + 2; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -864,11 +862,11 @@ ext_hdr_options_process(void) * Using this fix, the header is ignored, and the next header (if * present) is processed. */ - if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_len, uip_ext_opt_offset)) { + if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_len, ext_opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - uip_ext_opt_offset += (UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->len) + 2; + ext_opt_offset += (UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->len) + 2; return 0; default: /* @@ -884,8 +882,8 @@ ext_hdr_options_process(void) * Problem, Code 2, message to the packet's Source Address, * pointing to the unrecognized Option Type. */ - LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type); - switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->type & 0xC0) { + LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type); + switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type & 0xC0) { case 0: break; case 0x40: @@ -896,11 +894,11 @@ ext_hdr_options_process(void) } case 0x80: uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, - (uint32_t)UIP_IPH_LEN + uip_ext_len + uip_ext_opt_offset); + (uint32_t)UIP_IPH_LEN + uip_ext_len + ext_opt_offset); return 2; } /* in the cases were we did not discard, update ext_opt* */ - uip_ext_opt_offset += UIP_EXT_HDR_OPT_BUF(uip_ext_len, uip_ext_opt_offset)->len + 2; + ext_opt_offset += UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->len + 2; break; } } From aaf06754eac66057aad4d09e82a98925565c3566 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 11:20:34 +0200 Subject: [PATCH 20/28] uipbuf_set_len_field --- os/net/ipv6/multicast/esmrf.c | 3 +-- os/net/ipv6/multicast/roll-tm.c | 6 ++---- os/net/ipv6/uip-icmp6.c | 3 +-- os/net/ipv6/uip-nd6.c | 14 ++++--------- os/net/ipv6/uip.h | 1 + os/net/ipv6/uip6.c | 12 ++++------- os/net/ipv6/uipbuf.c | 10 ++++++++- os/net/ipv6/uipbuf.h | 8 +++++++ os/net/routing/rpl-classic/rpl-ext-header.c | 22 +++++--------------- os/net/routing/rpl-lite/rpl-ext-header.c | 23 +++++---------------- tools/sky/uip6-bridge/sicslow_ethernet.c | 5 ++--- 11 files changed, 42 insertions(+), 65 deletions(-) diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index b418d5322..dab2c7b31 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -156,8 +156,7 @@ icmp_output() PRINT6ADDR(&UIP_IP_BUF->destipaddr); PRINTF("\n"); - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); UIP_ICMP_BUF->type = ICMP6_ESMRF; UIP_ICMP_BUF->icode = ESMRF_ICMP_CODE; diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index cc0b09ac2..bd4040b55 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -858,8 +858,7 @@ icmp_output() roll_tm_create_dest(&UIP_IP_BUF->destipaddr); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); UIP_ICMP_BUF->type = ICMP6_ROLL_TM; UIP_ICMP_BUF->icode = ROLL_TM_ICMP_CODE; @@ -1353,8 +1352,7 @@ out() /* Update the proto and length field in the v6 header */ UIP_IP_BUF->proto = UIP_PROTO_HBHO; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); PRINTF("ROLL TM: Multicast Out, HBHO: T=%u, L=%u, M=%u, S=0x%02x%02x\n", lochbhmptr->type, lochbhmptr->len, HBH_GET_M(lochbhmptr), diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index a7e42c55c..2f393913f 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -206,8 +206,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { UIP_ICMP_BUF->type = type; UIP_ICMP_BUF->icode = code; UIP_ICMP6_ERROR_BUF->param = uip_htonl(param); - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_ICMP_BUF->icmpchksum = 0; UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index 52e19eff5..eceeec961 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -318,8 +318,7 @@ create_na: UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; UIP_IP_BUF->flow = 0; - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ - UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN); UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT; @@ -375,7 +374,6 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) UIP_ICMP_BUF->icode = 0; UIP_ND6_NS_BUF->reserved = 0; uip_ipaddr_copy((uip_ipaddr_t *) &UIP_ND6_NS_BUF->tgtipaddr, tgt); - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ /* * check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY * (here yes), for Address resolution , MUST @@ -391,8 +389,7 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) uipbuf_clear(); return; } - UIP_IP_BUF->len[1] = - UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN); create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN], UIP_ND6_OPT_SLLAO); @@ -787,8 +784,7 @@ uip_nd6_ra_output(uip_ipaddr_t * dest) } #endif /* UIP_ND6_RA_RDNSS */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /*ICMP checksum */ UIP_ICMP_BUF->icmpchksum = 0; @@ -819,15 +815,13 @@ uip_nd6_rs_output(void) uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); UIP_ICMP_BUF->type = ICMP6_RS; UIP_ICMP_BUF->icode = 0; - UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN; uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN; } else { uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN; - UIP_IP_BUF->len[1] = - UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN); create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN], UIP_ND6_OPT_SLLAO); diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 9deedb726..a09fb6fb2 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -1630,6 +1630,7 @@ void uip_process(uint8_t flag); * after the IP header. hence we split here L3 and L4 headers */ /* The IP header */ + struct uip_ip_hdr { /* IPV6 header */ uint8_t vtc; diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index c46ab3968..d634e2e7d 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -521,8 +521,7 @@ uip_remove_ext_hdr(void) uip_len -= last_uip_ext_len; /* Update the IP length. */ - UIP_IP_BUF->len[0] = (uip_len - UIP_IPH_LEN) >> 8; - UIP_IP_BUF->len[1] = (uip_len - UIP_IPH_LEN) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); } } /*---------------------------------------------------------------------------*/ @@ -767,8 +766,7 @@ uip_reass(void) uip_reasslen += UIP_IPH_LEN + uip_ext_len; memcpy(UIP_IP_BUF, FBUF, uip_reasslen); - UIP_IP_BUF->len[0] = ((uip_reasslen - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_reasslen - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_reasslen - UIP_IPH_LEN); LOG_INFO("reassembled packet %d (%d)\n", uip_reasslen, (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]); @@ -1563,8 +1561,7 @@ uip_process(uint8_t flag) /* For IPv6, the IP length field does not include the IPv6 IP header length. */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0x00; @@ -2295,8 +2292,7 @@ uip_process(uint8_t flag) UIP_IP_BUF->proto = UIP_PROTO_TCP; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); UIP_TCP_BUF->urgp[0] = UIP_TCP_BUF->urgp[1] = 0; diff --git a/os/net/ipv6/uipbuf.c b/os/net/ipv6/uipbuf.c index 876385890..cefe118a3 100644 --- a/os/net/ipv6/uipbuf.c +++ b/os/net/ipv6/uipbuf.c @@ -30,7 +30,7 @@ * */ #include "contiki.h" -#include "uip.h" +#include "net/ipv6/uip.h" #include "net/ipv6/uipbuf.h" #include @@ -71,6 +71,14 @@ uipbuf_set_len(uint16_t len) } } /*---------------------------------------------------------------------------*/ +void +uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len) +{ + + hdr->len[0] = (len >> 8); + hdr->len[1] = (len & 0xff); +} +/*---------------------------------------------------------------------------*/ /* Get the next header given the buffer - start indicates that this is start of the IPv6 header - needs to be set to 0 when in an ext hdr */ uint8_t * diff --git a/os/net/ipv6/uipbuf.h b/os/net/ipv6/uipbuf.h index 3ced3cd45..655fedee9 100644 --- a/os/net/ipv6/uipbuf.h +++ b/os/net/ipv6/uipbuf.h @@ -34,6 +34,7 @@ #define UIPBUF_H_ #include "contiki.h" +struct uip_ip_hdr; /** * \brief Resets uIP buffer @@ -54,6 +55,13 @@ bool uipbuf_add_ext_hdr(int16_t len); */ bool uipbuf_set_len(uint16_t len); +/** + * \brief Updates the length field in the uIP buffer + * \param buffer The new IPv6 header + * \param len The new length value + */ +void uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len); + /** * \brief Get the next IPv6 header. * \param buffer A pointer to the buffer holding the IPv6 packet diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index b175edd38..77cfd0338 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -279,8 +279,7 @@ count_matching_bytes(const void *p1, const void *p2, size_t n) static int insert_srh_header(void) { - /* Implementation of RFC6554 */ - uint8_t temp_len; + /* Implementation of RFC6554 uint8_t path_len; uint8_t ext_len; uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */ @@ -403,14 +402,9 @@ insert_srh_header(void) NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr); - /* In-place update of IPv6 length field */ - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += ext_len; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - + /* Update the IPv6 length field */ uipbuf_add_ext_hdr(ext_len); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); return 1; } @@ -511,13 +505,8 @@ insert_hbh_header(const rpl_instance_t *instance) UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = instance->instance_id; - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /* Update header before returning */ return update_hbh_header(); @@ -544,8 +533,7 @@ rpl_ext_header_remove(void) ext_len = ext_ptr->len * 8 + 8; uipbuf_add_ext_hdr(-ext_len); /* Update length field and rest of packer to the "left" */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); /* Update loop variables */ protocol = *prev_proto_ptr; diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 26d13c8ec..6e6eb6c34 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -170,7 +170,6 @@ static int insert_srh_header(void) { /* Implementation of RFC6554 */ - uint8_t temp_len; uint8_t path_len; uint8_t ext_len; uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */ @@ -292,14 +291,9 @@ insert_srh_header(void) NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &node_addr); - /* In-place update of IPv6 length field */ - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += ext_len; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - + /* Update the IPv6 length field */ uipbuf_add_ext_hdr(ext_len); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); return 1; } @@ -395,7 +389,6 @@ update_hbh_header(void) static int insert_hbh_header(void) { - uint8_t temp_len; int opt_offset = 2; /* Insert hop-by-hop header */ @@ -421,13 +414,8 @@ insert_hbh_header(void) UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = curr_instance.instance_id; - temp_len = UIP_IP_BUF->len[1]; - UIP_IP_BUF->len[1] += RPL_HOP_BY_HOP_LEN; - if(UIP_IP_BUF->len[1] < temp_len) { - UIP_IP_BUF->len[0]++; - } - uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); /* Update header before returning */ return update_hbh_header(); @@ -481,9 +469,8 @@ rpl_ext_header_remove(void) *prev_proto_ptr = ext_ptr->next; ext_len = ext_ptr->len * 8 + 8; uipbuf_add_ext_hdr(-ext_len); - /* Update length field and rest of packer to the "left" */ - UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); - UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); + /* Update length field and move rest of packet to the "left" */ + uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); /* Update loop variables */ protocol = *prev_proto_ptr; diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 4acd660ff..5e532b90c 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -492,12 +492,11 @@ int8_t mac_translateIcmpLinkLayer(lltype_t target) } //Adjust the IP header length, as well as uIP length - iplen = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0]<<8); + iplen = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0] << 8); iplen += sizechange; len += sizechange; - UIP_IP_BUF->len[1] = (uint8_t)iplen; - UIP_IP_BUF->len[0] = (uint8_t)(iplen >> 8); + uipbuf_set_len_field(UIP_IP_BUF, iplen); uip_len += sizechange; From 999e7dc3a7c5e9adad81753dc514fd2a59f65d23 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 11:31:06 +0200 Subject: [PATCH 21/28] Rework uip6.c --- os/net/ipv6/uip-icmp6.c | 3 +- os/net/ipv6/uip-icmp6.h | 12 +- os/net/ipv6/uip6.c | 445 +++++++++----------- os/net/ipv6/uipbuf.c | 7 +- os/net/ipv6/uipbuf.h | 9 +- os/net/routing/rpl-classic/rpl-ext-header.c | 3 +- tools/sky/uip6-bridge/fakeuip.c | 2 +- tools/sky/uip6-bridge/sicslow_ethernet.c | 4 +- 8 files changed, 231 insertions(+), 254 deletions(-) diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index 2f393913f..d9d55a8fb 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -229,8 +229,7 @@ uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len) UIP_IP_BUF->flow = 0; UIP_IP_BUF->proto = UIP_PROTO_ICMP6; UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit; - UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8; - UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff; + uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + payload_len); memcpy(&UIP_IP_BUF->destipaddr, dest, sizeof(*dest)); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); diff --git a/os/net/ipv6/uip-icmp6.h b/os/net/ipv6/uip-icmp6.h index 2808ea7de..5f8caf080 100644 --- a/os/net/ipv6/uip-icmp6.h +++ b/os/net/ipv6/uip-icmp6.h @@ -38,7 +38,7 @@ /** * \file * Header file for ICMPv6 message and error handing (RFC 4443) - * \author Julien Abeille + * \author Julien Abeille * \author Mathilde Durvy */ @@ -52,8 +52,8 @@ /** @{ */ #define ICMP6_DST_UNREACH 1 /**< dest unreachable */ #define ICMP6_PACKET_TOO_BIG 2 /**< packet too big */ -#define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */ -#define ICMP6_PARAM_PROB 4 /**< ip6 header bad */ +#define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */ +#define ICMP6_PARAM_PROB 4 /**< ip6 header bad */ #define ICMP6_ECHO_REQUEST 128 /**< Echo request */ #define ICMP6_ECHO_REPLY 129 /**< Echo reply */ @@ -76,10 +76,10 @@ /** \name ICMPv6 Destination Unreachable message codes*/ /** @{ */ #define ICMP6_DST_UNREACH_NOROUTE 0 /**< no route to destination */ -#define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */ +#define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */ #define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /**< not a neighbor(obsolete) */ #define ICMP6_DST_UNREACH_BEYONDSCOPE 2 /**< beyond scope of source address */ -#define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */ +#define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */ #define ICMP6_DST_UNREACH_NOPORT 4 /**< port unreachable */ /** @} */ @@ -116,7 +116,7 @@ typedef struct uip_icmp6_error{ * \param param 32 bit parameter of the error message, semantic depends on error */ void -uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param); +uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param); /** * \brief Send an icmpv6 message diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index d634e2e7d..8b7eea361 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -114,20 +114,10 @@ uip_lladdr_t uip_lladdr = {{0x00,0x06,0x98,0x00,0x02,0x32}}; * @{ */ /*---------------------------------------------------------------------------*/ -/** - * \brief Type of the next header in IPv6 header or extension headers - * - * Can be the next header field in the IPv6 header or in an extension header. - * When doing fragment reassembly, we must change the value of the next header - * field in the header before the fragmentation header, hence we need a pointer - * to this field. - */ -uint8_t *uip_next_hdr; /** \brief bitmap we use to record which IPv6 headers we have already seen */ uint8_t uip_ext_bitmap = 0; /** - * \brief length of the extension headers read. updated each time we process - * a header + * \brief Total length of all IPv6 extension headers */ uint8_t uip_ext_len = 0; /** \brief The final protocol after IPv6 extension headers: @@ -352,10 +342,10 @@ upper_layer_chksum(uint8_t proto) volatile uint16_t upper_layer_len; uint16_t sum; - upper_layer_len = (((uint16_t)(UIP_IP_BUF->len[0]) << 8) + UIP_IP_BUF->len[1] - uip_ext_len); + upper_layer_len = uipbuf_get_len_field(UIP_IP_BUF) - uip_ext_len; LOG_DBG("Upper layer checksum len: %d from: %d\n", upper_layer_len, - UIP_IPH_LEN + UIP_LLH_LEN + uip_ext_len); + (int)(UIP_IP_PAYLOAD(uip_ext_len) - uip_buf)); /* First sum pseudoheader. */ /* IP protocol and length fields. This addition cannot carry. */ @@ -363,9 +353,8 @@ upper_layer_chksum(uint8_t proto) /* Sum IP source and destination addresses. */ sum = chksum(sum, (uint8_t *)&UIP_IP_BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); - /* Sum TCP header and data. */ - sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN + uip_ext_len], - upper_layer_len); + /* Sum upper-layer header and data. */ + sum = chksum(sum, UIP_IP_PAYLOAD(uip_ext_len), upper_layer_len); return (sum == 0) ? 0xffff : uip_htons(sum); } @@ -502,7 +491,6 @@ uip_connect(const uip_ipaddr_t *ripaddr, uint16_t rport) void uip_remove_ext_hdr(void) { - int last_uip_ext_len; /* Remove ext header before TCP/UDP processing. */ if(uip_ext_len > 0) { LOG_DBG("Removing IPv6 extension headers (extlen: %d, uiplen: %d)\n", @@ -512,15 +500,15 @@ uip_remove_ext_hdr(void) uipbuf_clear(); return; } - last_uip_ext_len = uip_ext_len; - uip_ext_len = 0; - UIP_IP_BUF->proto = UIP_EXT_BUF(uip_ext_len)->next; - memmove(((uint8_t *)UIP_TCP_BUF), (uint8_t *)UIP_TCP_BUF + last_uip_ext_len, - uip_len - UIP_IPH_LEN - last_uip_ext_len); - uip_len -= last_uip_ext_len; + /* Set proto */ + UIP_IP_BUF->proto = uip_last_proto; + /* Move IP payload to the "left"*/ + memmove(UIP_IP_PAYLOAD(0), UIP_IP_PAYLOAD(uip_ext_len), + uip_len - UIP_IPH_LEN - uip_ext_len); /* Update the IP length. */ + uipbuf_add_ext_hdr(-uip_ext_len); uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); } } @@ -635,11 +623,12 @@ static uint32_t uip_id; /* For every packet that is to be fragmented, the source #define IP_MF 0x0001 static uint16_t -uip_reass(void) +uip_reass(uint8_t *prev_proto_ptr) { uint16_t offset=0; uint16_t len; uint16_t i; + struct uip_frag_hdr *frag_buf = UIP_FRAG_BUF(uip_ext_len); /* If ip_reasstmr is zero, no packet is present in the buffer */ /* We first write the unfragmentable part of IP header into the reassembly @@ -651,7 +640,7 @@ uip_reass(void) etimer_set(&uip_reass_timer, UIP_REASS_MAXAGE*CLOCK_SECOND); uip_reass_on = 1; uip_reassflags = 0; - uip_id = UIP_FRAG_BUF(uip_ext_len)->id; + uip_id = frag_buf->id; /* Clear the bitmap. */ memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap)); } @@ -662,9 +651,9 @@ uip_reass(void) */ if(uip_ipaddr_cmp(&FBUF->srcipaddr, &UIP_IP_BUF->srcipaddr) && uip_ipaddr_cmp(&FBUF->destipaddr, &UIP_IP_BUF->destipaddr) && - UIP_FRAG_BUF(uip_ext_len)->id == uip_id) { + frag_buf->id == uip_id) { len = uip_len - uip_ext_len - UIP_IPH_LEN - UIP_FRAGH_LEN; - offset = (uip_ntohs(UIP_FRAG_BUF(uip_ext_len)->offsetresmore) & 0xfff8); + offset = (uip_ntohs(frag_buf->offsetresmore) & 0xfff8); /* in byte, originaly in multiple of 8 bytes*/ LOG_INFO("len %d\n", len); LOG_INFO("offset %d\n", offset); @@ -675,7 +664,7 @@ uip_reass(void) * Part is obtained from the Next Header field of the first * fragment's Fragment header. */ - *uip_next_hdr = UIP_FRAG_BUF(uip_ext_len)->next; + *prev_proto_ptr = frag_buf->next; memcpy(FBUF, UIP_IP_BUF, uip_ext_len + UIP_IPH_LEN); LOG_INFO("src "); LOG_INFO_6ADDR(&FBUF->srcipaddr); @@ -696,7 +685,7 @@ uip_reass(void) /* If this fragment has the More Fragments flag set to zero, it is the last fragment*/ - if((uip_ntohs(UIP_FRAG_BUF(uip_ext_len)->offsetresmore) & IP_MF) == 0) { + if((uip_ntohs(frag_buf->offsetresmore) & IP_MF) == 0) { uip_reassflags |= UIP_REASS_FLAG_LASTFRAG; /*calculate the size of the entire packet*/ uip_reasslen = offset + len; @@ -720,7 +709,7 @@ uip_reass(void) /* Copy the fragment into the reassembly buffer, at the right offset. */ memcpy((uint8_t *)FBUF + UIP_IPH_LEN + uip_ext_len + offset, - (uint8_t *)UIP_FRAG_BUF(uip_ext_len) + UIP_FRAGH_LEN, len); + (uint8_t *)frag_buf + UIP_FRAGH_LEN, len); /* Update the bitmap. */ if(offset >> 6 == (offset + len) >> 6) { @@ -767,8 +756,7 @@ uip_reass(void) uip_reasslen += UIP_IPH_LEN + uip_ext_len; memcpy(UIP_IP_BUF, FBUF, uip_reasslen); uipbuf_set_len_field(UIP_IP_BUF, uip_reasslen - UIP_IPH_LEN); - LOG_INFO("reassembled packet %d (%d)\n", uip_reasslen, - (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]); + LOG_INFO("reassembled packet %d (%d)\n", uip_reasslen, uipbuf_get_len_field(UIP_IP_BUF)); return uip_reasslen; @@ -828,16 +816,16 @@ uip_add_rcv_nxt(uint16_t n) * \brief Process the options in Destination and Hop By Hop extension headers */ static uint8_t -ext_hdr_options_process(void) +ext_hdr_options_process(int ext_offset) { /* * Length field in the extension header: length of the header in units of * 8 bytes, excluding the first 8 bytes * length field in an option : the length of data in the option */ - uint8_t ext_opt_offset = 2; - while(ext_opt_offset < ((UIP_EXT_BUF(uip_ext_len)->len << 3) + 8)) { - switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type) { + uint8_t opt_offset = 2; + while(opt_offset < ((UIP_EXT_BUF(ext_offset)->len << 3) + 8)) { + switch(UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type) { /* * for now we do not support any options except padding ones * PAD1 does not make sense as the header must be 8bytes aligned, @@ -845,11 +833,11 @@ ext_hdr_options_process(void) */ case UIP_EXT_HDR_OPT_PAD1: LOG_DBG("Processing PAD1 option\n"); - ext_opt_offset += 1; + opt_offset += 1; break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(uip_ext_len, ext_opt_offset)->opt_len + 2; + opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(ext_offset, opt_offset)->opt_len + 2; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -860,11 +848,12 @@ ext_hdr_options_process(void) * Using this fix, the header is ignored, and the next header (if * present) is processed. */ - if(!NETSTACK_ROUTING.ext_header_hbh_update(uip_ext_len, ext_opt_offset)) { + LOG_DBG("Processing RPL option\n"); + if(!NETSTACK_ROUTING.ext_header_hbh_update(ext_offset, opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - ext_opt_offset += (UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->len) + 2; + opt_offset += (UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->len) + 2; return 0; default: /* @@ -880,8 +869,8 @@ ext_hdr_options_process(void) * Problem, Code 2, message to the packet's Source Address, * pointing to the unrecognized Option Type. */ - LOG_DBG("MSB %x\n", UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type); - switch(UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->type & 0xC0) { + LOG_DBG("Unrecognized option, MSB 0x%x\n", UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type); + switch(UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type & 0xC0) { case 0: break; case 0x40: @@ -892,23 +881,49 @@ ext_hdr_options_process(void) } case 0x80: uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, - (uint32_t)UIP_IPH_LEN + uip_ext_len + ext_opt_offset); + (uint32_t)UIP_IPH_LEN + ext_offset + opt_offset); return 2; } /* in the cases were we did not discard, update ext_opt* */ - ext_opt_offset += UIP_EXT_HDR_OPT_BUF(uip_ext_len, ext_opt_offset)->len + 2; + opt_offset += UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->len + 2; break; } } return 0; } - - +/*---------------------------------------------------------------------------*/ +static bool +uip_check_mtu(void) +{ + if(uip_len > UIP_LINK_MTU) { + uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); + UIP_STAT(++uip_stat.ip.drop); + return false; + } else { + return true; + } +} +/*---------------------------------------------------------------------------*/ +static bool +uip_update_ttl(void) +{ + if(UIP_IP_BUF->ttl <= 1) { + uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT, 0); + UIP_STAT(++uip_stat.ip.drop); + return false; + } else { + UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; + return true; + } +} /*---------------------------------------------------------------------------*/ void uip_process(uint8_t flag) { uint8_t *last_header; + uint8_t protocol; + uint8_t *next_header; + struct uip_ext_hdr *ext_ptr; #if UIP_TCP int c; uint16_t tmp16; @@ -1090,8 +1105,8 @@ uip_process(uint8_t flag) * value.. */ - if((UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] <= uip_len) { - uip_len = (UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] + UIP_IPH_LEN; + if(uipbuf_get_len_field(UIP_IP_BUF) <= uip_len) { + uip_len = uipbuf_get_len_field(UIP_IP_BUF) + UIP_IPH_LEN; /* * The length reported in the IPv6 header is the * length of the payload that follows the @@ -1104,7 +1119,7 @@ uip_process(uint8_t flag) * header (40 bytes). */ } else { - LOG_ERR("packet shorter than reported in IP header"); + LOG_ERR("packet shorter than reported in IP header\n"); goto drop; } @@ -1113,11 +1128,11 @@ uip_process(uint8_t flag) uip_last_proto = 0; last_header = uipbuf_get_last_header(UIP_IP_BUF_CHAR, uip_len, &uip_last_proto); if(last_header == NULL) { - LOG_ERR("invalid extension header chain"); + LOG_ERR("invalid extension header chain\n"); goto drop; } /* Set uip_ext_len */ - uip_ext_len = last_header - UIP_IP_BUF_CHAR; + uip_ext_len = last_header - UIP_IP_PAYLOAD(0); LOG_INFO("packet received from "); LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); @@ -1140,33 +1155,20 @@ uip_process(uint8_t flag) #if UIP_CONF_ROUTER /* - * Next header field processing. In IPv6, we can have extension headers, - * if present, the Hop-by-Hop Option must be processed before forwarding + * If present, the Hop-by-Hop Option must be processed before forwarding * the packet. */ - uip_next_hdr = &UIP_IP_BUF->proto; - uip_ext_len = 0; - uip_ext_bitmap = 0; - if(*uip_next_hdr == UIP_PROTO_HBHO) { -#if UIP_CONF_IPV6_CHECKS - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; -#endif /* UIP_CONF_IPV6_CHECKS */ - switch(ext_hdr_options_process()) { + next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + if(next_header != NULL && protocol == UIP_PROTO_HBHO) { + switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { case 0: - /* continue */ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; + break; /* done */ case 1: - LOG_ERR("Dropping packet after extension header processing\n"); - /* silently discard */ - goto drop; + goto drop; /* silently discard */ case 2: - LOG_ERR("Sending error message after extension header processing\n"); - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ } } @@ -1202,23 +1204,12 @@ uip_process(uint8_t flag) !uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr) && !uip_is_addr_loopback(&UIP_IP_BUF->destipaddr)) { - - /* Check MTU */ - if(uip_len > UIP_LINK_MTU) { - uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - /* Check Hop Limit */ - if(UIP_IP_BUF->ttl <= 1) { - uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, - ICMP6_TIME_EXCEED_TRANSIT, 0); - UIP_STAT(++uip_stat.ip.drop); + if(!uip_check_mtu() || !uip_update_ttl()) { + /* Send ICMPv6 error, prepared by the function that just returned false */ goto send; } - UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; - LOG_INFO("Forwarding packet towards "); + LOG_INFO("Forwarding packet to next hop "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); UIP_STAT(++uip_stat.ip.forwarded); @@ -1247,38 +1238,20 @@ uip_process(uint8_t flag) UIP_STAT(++uip_stat.ip.drop); goto drop; } - - /* - * Next header field processing. In IPv6, we can have extension headers, - * they are processed here - */ - uip_next_hdr = &UIP_IP_BUF->proto; - uip_ext_len = 0; - uip_ext_bitmap = 0; #endif /* UIP_CONF_ROUTER */ #if UIP_IPV6_MULTICAST && UIP_CONF_ROUTER process: #endif /* UIP_IPV6_MULTICAST && UIP_CONF_ROUTER */ - while(1) { - switch(*uip_next_hdr){ -#if UIP_TCP - case UIP_PROTO_TCP: - /* TCP, for both IPv4 and IPv6 */ - uip_last_proto = *uip_next_hdr; - goto tcp_input; -#endif /* UIP_TCP */ -#if UIP_UDP - case UIP_PROTO_UDP: - /* UDP, for both IPv4 and IPv6 */ - uip_last_proto = *uip_next_hdr; - goto udp_input; -#endif /* UIP_UDP */ - case UIP_PROTO_ICMP6: - /* ICMPv6 */ - uip_last_proto = *uip_next_hdr; - goto icmp6_input; + /* IPv6 extension header processing: loop until reaching upper-layer protocol */ + uip_ext_bitmap = 0; + for(next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + next_header != NULL && uip_is_proto_ext_hdr(protocol); + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false)) { + + ext_ptr = (struct uip_ext_hdr *)next_header; + switch(protocol) { case UIP_PROTO_HBHO: LOG_DBG("Processing hbh header\n"); /* Hop by hop option header */ @@ -1290,145 +1263,140 @@ uip_process(uint8_t flag) uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; } #endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process()) { + switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { case 0: - /*continue*/ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; + break; /* done */ case 1: - /*silently discard*/ - goto drop; + goto drop; /* silently discard */ case 2: - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ } break; - case UIP_PROTO_DESTO: + case UIP_PROTO_DESTO: #if UIP_CONF_IPV6_CHECKS - /* Destination option header. if we saw two already, drop */ - LOG_DBG("Processing desto header\n"); - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO1) { - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO2) { - goto bad_hdr; - } else{ - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO2; - } - } else { - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1; - } -#endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process()) { - case 0: - /*continue*/ - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - case 1: - /*silently discard*/ - goto drop; - case 2: - /* send icmp error message (created in ext_hdr_options_process) - * and discard*/ - goto send; - } - break; - case UIP_PROTO_ROUTING: -#if UIP_CONF_IPV6_CHECKS - /* Routing header. If we saw one already, drop */ - if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_ROUTING) { - goto bad_hdr; - } else { - uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_ROUTING; - } -#endif /*UIP_CONF_IPV6_CHECKS*/ - /* - * Routing Header length field is in units of 8 bytes, excluding - * As per RFC2460 section 4.4, if routing type is unrecognized: - * if segments left = 0, ignore the header - * if segments left > 0, discard packet and send icmp error pointing - * to the routing type - */ - - LOG_DBG("Processing Routing header\n"); - if(UIP_RH_BUF(uip_ext_len)->seg_left > 0) { - if(NETSTACK_ROUTING.ext_header_srh_update()) { - - /* With routing header, the detination address is us and will - * be swapped later to the next hop. Because of this, the MTU - * and TTL were not checked and updated yet. Do this now. */ - - /* Check MTU */ - if(uip_len > UIP_LINK_MTU) { - uip_icmp6_error_output(ICMP6_PACKET_TOO_BIG, 0, UIP_LINK_MTU); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - /* Check Hop Limit */ - if(UIP_IP_BUF->ttl <= 1) { - uip_icmp6_error_output(ICMP6_TIME_EXCEEDED, - ICMP6_TIME_EXCEED_TRANSIT, 0); - UIP_STAT(++uip_stat.ip.drop); - goto send; - } - UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1; - - LOG_INFO("Forwarding packet to next hop "); - LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); - LOG_INFO_("\n"); - UIP_STAT(++uip_stat.ip.forwarded); - - goto send; /* Proceed to forwarding */ - } - uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, UIP_IPH_LEN + uip_ext_len + 2); - UIP_STAT(++uip_stat.ip.drop); - LOG_ERR("unrecognized routing type"); - goto send; - } - uip_next_hdr = &UIP_EXT_BUF(uip_ext_len)->next; - uip_ext_len += (UIP_EXT_BUF(uip_ext_len)->len << 3) + 8; - break; - case UIP_PROTO_FRAG: - /* Fragmentation header:call the reassembly function, then leave */ -#if UIP_CONF_IPV6_REASSEMBLY - LOG_INFO("Processing fragmentation header\n"); - uip_len = uip_reass(); - if(uip_len == 0) { - goto drop; - } - if(uip_reassflags & UIP_REASS_FLAG_ERROR_MSG){ - /* we are not done with reassembly, this is an error message */ - goto send; - } - /*packet is reassembled, reset the next hdr to the beginning - of the IP header and restart the parsing of the reassembled pkt*/ - LOG_INFO("Processing reassembled packet\n"); - uip_ext_len = 0; - uip_ext_bitmap = 0; - uip_next_hdr = &UIP_IP_BUF->proto; - break; -#else /* UIP_CONF_IPV6_REASSEMBLY */ - UIP_STAT(++uip_stat.ip.drop); - UIP_STAT(++uip_stat.ip.fragerr); - LOG_ERR("fragment dropped."); - goto drop; -#endif /* UIP_CONF_IPV6_REASSEMBLY */ - case UIP_PROTO_NONE: - goto drop; - default: + /* Destination option header. if we saw two already, drop */ + LOG_DBG("Processing desto header\n"); + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO1) { + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO2) { goto bad_hdr; + } else{ + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO2; + } + } else { + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1; + } +#endif /*UIP_CONF_IPV6_CHECKS*/ + switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { + case 0: + break; /* done */ + case 1: + goto drop; /* silently discard */ + case 2: + goto send; /* send icmp error message (created in + ext_hdr_options_process) and discard */ + } + break; + case UIP_PROTO_ROUTING: +#if UIP_CONF_IPV6_CHECKS + /* Routing header. If we saw one already, drop */ + if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_ROUTING) { + goto bad_hdr; + } else { + uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_ROUTING; + } +#endif /*UIP_CONF_IPV6_CHECKS*/ + /* + * Routing Header length field is in units of 8 bytes, excluding + * As per RFC2460 section 4.4, if routing type is unrecognized: + * if segments left = 0, ignore the header + * if segments left > 0, discard packet and send icmp error pointing + * to the routing type + */ + + LOG_DBG("Processing Routing header\n"); + if(((struct uip_routing_hdr *)ext_ptr)->seg_left > 0) { + /* Process source routing header */ + if(NETSTACK_ROUTING.ext_header_srh_update()) { + + /* The MTU and TTL were not checked and updated yet, because with + * a routing header, the IPv6 destination address was set to us + * even though we act only as forwarder. Check MTU and TTL now */ + if(!uip_check_mtu() || !uip_update_ttl()) { + /* Send ICMPv6 error, prepared by the function that just returned false */ + goto send; + } + + LOG_INFO("Forwarding packet to next hop "); + LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); + LOG_INFO_("\n"); + UIP_STAT(++uip_stat.ip.forwarded); + + goto send; /* Proceed to forwarding */ + } else { + LOG_ERR("Unrecognized routing type\n"); + goto bad_hdr; + } + } + break; + case UIP_PROTO_FRAG: + /* Fragmentation header:call the reassembly function, then leave */ +#if UIP_CONF_IPV6_REASSEMBLY + LOG_INFO("Processing fragmentation header\n"); + uip_len = uip_reass(&ext_ptr->next); + if(uip_len == 0) { + goto drop; + } + if(uip_reassflags & UIP_REASS_FLAG_ERROR_MSG) { + /* we are not done with reassembly, this is an error message */ + goto send; + } + /* packet is reassembled. Restart the parsing of the reassembled pkt */ + LOG_INFO("Processing reassembled packet\n"); + uip_ext_bitmap = 0; + next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + break; +#else /* UIP_CONF_IPV6_REASSEMBLY */ + UIP_STAT(++uip_stat.ip.drop); + UIP_STAT(++uip_stat.ip.fragerr); + LOG_ERR("fragment dropped."); + goto drop; +#endif /* UIP_CONF_IPV6_REASSEMBLY */ + case UIP_PROTO_NONE: + goto drop; + default: + goto bad_hdr; } } + + /* Process upper-layer input */ + if(next_header != NULL) { + switch(protocol) { +#if UIP_TCP + case UIP_PROTO_TCP: + /* TCP, for both IPv4 and IPv6 */ + goto tcp_input; +#endif +#if UIP_UDP + case UIP_PROTO_UDP: + /* UDP, for both IPv4 and IPv6 */ + goto udp_input; +#endif + case UIP_PROTO_ICMP6: + /* ICMPv6 */ + goto icmp6_input; + } + } + bad_hdr: /* * RFC 2460 send error message parameterr problem, code unrecognized * next header, pointing to the next header field */ - uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(uip_next_hdr - (uint8_t *)UIP_IP_BUF)); + uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(next_header - UIP_IP_BUF_CHAR)); UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.protoerr); - LOG_ERR("unrecognized header"); + LOG_ERR("unrecognized header\n"); goto send; /* End of headers processing */ @@ -1612,7 +1580,7 @@ uip_process(uint8_t flag) /* Make sure that the TCP port number is not zero. */ if(UIP_TCP_BUF->destport == 0 || UIP_TCP_BUF->srcport == 0) { - LOG_ERR("tcp: zero port."); + LOG_ERR("tcp: zero port\n"); goto drop; } @@ -1729,7 +1697,7 @@ uip_process(uint8_t flag) the remote end will retransmit the packet at a time when we have more spare connections. */ UIP_STAT(++uip_stat.tcp.syndrop); - LOG_ERR("tcp: found no unused connections."); + LOG_ERR("tcp: found no unused connections\n"); goto drop; } uip_conn = uip_connr; @@ -2307,8 +2275,7 @@ uip_process(uint8_t flag) #endif UIP_IP_BUF->flow = 0x00; send: - LOG_INFO("Sending packet with length %d (%d)\n", uip_len, - (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]); + LOG_INFO("Sending packet with length %d (%d)\n", uip_len, uipbuf_get_len_field(UIP_IP_BUF)); UIP_STAT(++uip_stat.ip.sent); /* Return and let the caller do the actual transmission. */ diff --git a/os/net/ipv6/uipbuf.c b/os/net/ipv6/uipbuf.c index cefe118a3..eaf43e308 100644 --- a/os/net/ipv6/uipbuf.c +++ b/os/net/ipv6/uipbuf.c @@ -74,11 +74,16 @@ uipbuf_set_len(uint16_t len) void uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len) { - hdr->len[0] = (len >> 8); hdr->len[1] = (len & 0xff); } /*---------------------------------------------------------------------------*/ +uint16_t +uipbuf_get_len_field(struct uip_ip_hdr *hdr) +{ + return ((uint16_t)(hdr->len[0]) << 8) + hdr->len[1]; +} +/*---------------------------------------------------------------------------*/ /* Get the next header given the buffer - start indicates that this is start of the IPv6 header - needs to be set to 0 when in an ext hdr */ uint8_t * diff --git a/os/net/ipv6/uipbuf.h b/os/net/ipv6/uipbuf.h index 655fedee9..96b180ee8 100644 --- a/os/net/ipv6/uipbuf.h +++ b/os/net/ipv6/uipbuf.h @@ -57,11 +57,18 @@ bool uipbuf_set_len(uint16_t len); /** * \brief Updates the length field in the uIP buffer - * \param buffer The new IPv6 header + * \param buffer The IPv6 header * \param len The new length value */ void uipbuf_set_len_field(struct uip_ip_hdr *hdr, uint16_t len); +/** + * \brief Returns the value of the length field in the uIP buffer + * \param buffer The IPv6 header + * \retvel The length value + */ +uint16_t uipbuf_get_len_field(struct uip_ip_hdr *hdr); + /** * \brief Get the next IPv6 header. * \param buffer A pointer to the buffer holding the IPv6 packet diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 77cfd0338..91a87079e 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -279,7 +279,7 @@ count_matching_bytes(const void *p1, const void *p2, size_t n) static int insert_srh_header(void) { - /* Implementation of RFC6554 + /* Implementation of RFC6554 */ uint8_t path_len; uint8_t ext_len; uint8_t cmpri, cmpre; /* ComprI and ComprE fields of the RPL Source Routing Header */ @@ -479,7 +479,6 @@ update_hbh_header(void) static int insert_hbh_header(const rpl_instance_t *instance) { - uint8_t temp_len; int opt_offset = 2; /* Insert hop-by-hop header */ diff --git a/tools/sky/uip6-bridge/fakeuip.c b/tools/sky/uip6-bridge/fakeuip.c index 86658429a..6f8d650eb 100644 --- a/tools/sky/uip6-bridge/fakeuip.c +++ b/tools/sky/uip6-bridge/fakeuip.c @@ -104,7 +104,7 @@ upper_layer_chksum(uint8_t proto) uint16_t upper_layer_len; uint16_t sum; - upper_layer_len = (((uint16_t)(UIP_IP_BUF->len[0]) << 8) + UIP_IP_BUF->len[1]) ; + upper_layer_len = uipbuf_get_len_field(UIP_IP_BUF); /* First sum pseudoheader. */ /* IP protocol and length fields. This addition cannot carry. */ diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c index 5e532b90c..cd60acd57 100644 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ b/tools/sky/uip6-bridge/sicslow_ethernet.c @@ -397,7 +397,7 @@ void slide(uint8_t * data, uint8_t length, int16_t slide); int8_t mac_translateIcmpLinkLayer(lltype_t target) { uint16_t icmp_opt_offset = 0; - int16_t len = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0] << 8); + int16_t len = uipbuf_get_len_field(UIP_IP_BUF); uint16_t iplen; @@ -492,7 +492,7 @@ int8_t mac_translateIcmpLinkLayer(lltype_t target) } //Adjust the IP header length, as well as uIP length - iplen = UIP_IP_BUF->len[1] | (UIP_IP_BUF->len[0] << 8); + iplen = uipbuf_get_len_field(UIP_IP_BUF); iplen += sizechange; len += sizechange; From ea32ab0f12ec1b5dd717d01d6526c49eb4514809 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 17:47:24 +0200 Subject: [PATCH 22/28] Removed UIP_LLH_LEN --- arch/cpu/native/net/tun6-net.c | 4 +- arch/platform/cooja/net/uip-driver.c | 7 +- arch/platform/jn516x/slip.c | 7 +- .../jn516x/tsch/common-conf-jn516x.h | 2 +- .../coap-demo/coap-server/coap-server.c | 1 - examples/slip-radio/slip-radio.c | 5 +- os/dev/slip.c | 7 +- os/net/app-layer/coap/coap-uip.c | 2 +- os/net/ipv6/multicast/esmrf.c | 18 +- os/net/ipv6/multicast/roll-tm.c | 6 +- os/net/ipv6/multicast/smrf.c | 4 +- os/net/ipv6/sicslowpan.c | 6 +- os/net/ipv6/uip-packetqueue.h | 2 +- os/net/ipv6/uip-udp-packet.c | 4 +- os/net/ipv6/uip.h | 10 +- os/net/ipv6/uip6.c | 44 +- os/net/ipv6/uipopt.h | 31 +- os/services/ip64/ip64-arp.c | 22 +- os/services/ip64/ip64-dhcpc.c | 24 +- os/services/ip64/ip64-eth-interface.c | 4 +- os/services/ip64/ip64-slip-interface.c | 22 +- .../rpl-border-router/embedded/slip-bridge.c | 14 +- .../rpl-border-router/native/tun-bridge.c | 4 +- tools/sky/uip6-bridge/Makefile | 39 - tools/sky/uip6-bridge/dev/slip.c | 322 ------- tools/sky/uip6-bridge/dev/slip.h | 92 -- tools/sky/uip6-bridge/fakeuip.c | 134 --- tools/sky/uip6-bridge/project-conf.h | 49 - tools/sky/uip6-bridge/radvd.conf | 16 - tools/sky/uip6-bridge/sicslow_ethernet.c | 879 ------------------ tools/sky/uip6-bridge/sicslow_ethernet.h | 78 -- tools/sky/uip6-bridge/uip6-bridge-tap.c | 150 --- tools/wpcapslip/wpcap.c | 2 +- 33 files changed, 113 insertions(+), 1898 deletions(-) delete mode 100644 tools/sky/uip6-bridge/Makefile delete mode 100644 tools/sky/uip6-bridge/dev/slip.c delete mode 100644 tools/sky/uip6-bridge/dev/slip.h delete mode 100644 tools/sky/uip6-bridge/fakeuip.c delete mode 100644 tools/sky/uip6-bridge/project-conf.h delete mode 100644 tools/sky/uip6-bridge/radvd.conf delete mode 100644 tools/sky/uip6-bridge/sicslow_ethernet.c delete mode 100644 tools/sky/uip6-bridge/sicslow_ethernet.h delete mode 100644 tools/sky/uip6-bridge/uip6-bridge-tap.c diff --git a/arch/cpu/native/net/tun6-net.c b/arch/cpu/native/net/tun6-net.c index d46d91d42..f1e8c2e77 100644 --- a/arch/cpu/native/net/tun6-net.c +++ b/arch/cpu/native/net/tun6-net.c @@ -263,7 +263,7 @@ output(const linkaddr_t *localdest) { LOG_DBG("SUT: %u\n", uip_len); if(uip_len > 0) { - return tun_output(&uip_buf[UIP_LLH_LEN], uip_len); + return tun_output(uip_buf, uip_len); } return 0; } @@ -297,7 +297,7 @@ handle_fd(fd_set *rset, fd_set *wset) LOG_INFO("Tun6-handle FD\n"); if(FD_ISSET(tunfd, rset)) { - size = tun_input(&uip_buf[UIP_LLH_LEN], sizeof(uip_buf)); + size = tun_input(uip_buf, sizeof(uip_buf)); LOG_DBG("TUN data incoming read:%d\n", size); uip_len = size; tcpip_input(); diff --git a/arch/platform/cooja/net/uip-driver.c b/arch/platform/cooja/net/uip-driver.c index 0e7209a6c..fd86cc9c6 100644 --- a/arch/platform/cooja/net/uip-driver.c +++ b/arch/platform/cooja/net/uip-driver.c @@ -48,7 +48,7 @@ static uint8_t uip_driver_send(const linkaddr_t *addr) { - packetbuf_copyfrom(&uip_buf[UIP_LLH_LEN], uip_len); + packetbuf_copyfrom(uip_buf, uip_len); /* XXX we should provide a callback function that is called when the packet is sent. For now, we just supply a NULL pointer. */ @@ -64,9 +64,8 @@ init(void) static void input(void) { - if(packetbuf_datalen() > 0 && - packetbuf_datalen() <= UIP_BUFSIZE - UIP_LLH_LEN) { - memcpy(&uip_buf[UIP_LLH_LEN], packetbuf_dataptr(), packetbuf_datalen()); + if(packetbuf_datalen() > 0 && packetbuf_datalen() <= UIP_BUFSIZE) { + memcpy(uip_buf, packetbuf_dataptr(), packetbuf_datalen()); uip_len = packetbuf_datalen(); tcpip_input(); } diff --git a/arch/platform/jn516x/slip.c b/arch/platform/jn516x/slip.c index 572d9f71c..41652e181 100644 --- a/arch/platform/jn516x/slip.c +++ b/arch/platform/jn516x/slip.c @@ -116,7 +116,7 @@ unsigned long slip_received, slip_frames; #ifdef SLIP_CONF_RX_BUFSIZE #define RX_BUFSIZE SLIP_CONF_RX_BUFSIZE -#if RX_BUFSIZE < (UIP_BUFSIZE - UIP_LLH_LEN + 16) +#if RX_BUFSIZE < (UIP_BUFSIZE + 16) #error "SLIP_CONF_RX_BUFSIZE too small for UIP_BUFSIZE" #endif @@ -194,7 +194,7 @@ slip_send(void) slip_arch_writeb(SLIP_END); - ptr = &uip_buf[UIP_LLH_LEN]; + ptr = uip_buf; for(i = 0; i < uip_len; ++i) { c = *ptr++; slip_write_char(c); @@ -316,8 +316,7 @@ PROCESS_THREAD(slip_process, ev, data) PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); + uip_len = slip_poll_handler(uip_buf, UIP_BUFSIZE); PRINTF("SLIP: recv bytes %u frames RECV: %u. is_full %u, is_dropping %u.\n", end_counter, uip_len, is_full, is_dropping); diff --git a/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h b/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h index 568fd4b6d..32f7534e7 100644 --- a/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h +++ b/examples/platform-specific/jn516x/tsch/common-conf-jn516x.h @@ -44,7 +44,7 @@ #define REST_MAX_CHUNK_SIZE 256 /* Network config */ -//#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + UIP_LLH_LEN + UIP_IPUDPH_LEN + COAP_MAX_HEADER_SIZE) +//#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + UIP_IPUDPH_LEN + COAP_MAX_HEADER_SIZE) //#define UIP_CONF_BUFFER_SIZE (REST_MAX_CHUNK_SIZE + 0 + 48 + 70) #define UIP_CONF_BUFFER_SIZE 1280 /* ipv6 required minimum */ diff --git a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c index 584f54318..9e38d5d3a 100644 --- a/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c +++ b/examples/platform-specific/nrf52dk/coap-demo/coap-server/coap-server.c @@ -97,7 +97,6 @@ PROCESS_THREAD(er_example_server, ev, data) PRINTF("Starting Erbium Example Server\n"); PRINTF("uIP buffer: %u\n", UIP_BUFSIZE); - PRINTF("LL header: %u\n", UIP_LLH_LEN); PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); PRINTF("REST max chunk: %u\n", COAP_MAX_CHUNK_SIZE); diff --git a/examples/slip-radio/slip-radio.c b/examples/slip-radio/slip-radio.c index 84045e7e1..8ef22ff55 100644 --- a/examples/slip-radio/slip-radio.c +++ b/examples/slip-radio/slip-radio.c @@ -226,9 +226,8 @@ slip_radio_cmd_handler(const uint8_t *data, int len) static void slip_input_callback(void) { - LOG_DBG("SR-SIN: %u '%c%c'\n", uip_len, - uip_buf[UIP_LLH_LEN], uip_buf[UIP_LLH_LEN + 1]); - if(!cmd_input(&uip_buf[UIP_LLH_LEN], uip_len)) { + LOG_DBG("SR-SIN: %u '%c%c'\n", uip_len, uip_buf[0], uip_buf[1]); + if(!cmd_input(uip_buf, uip_len)) { cmd_send((uint8_t *)"EUnknown command", 16); } uipbuf_clear(); diff --git a/os/dev/slip.c b/os/dev/slip.c index 3cfe9ea07..7426151c4 100644 --- a/os/dev/slip.c +++ b/os/dev/slip.c @@ -52,7 +52,7 @@ static uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; #endif /*---------------------------------------------------------------------------*/ /* Must be at least one byte larger than UIP_BUFSIZE! */ -#define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16) +#define RX_BUFSIZE (UIP_BUFSIZE + 16) /*---------------------------------------------------------------------------*/ enum { STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */ @@ -87,7 +87,7 @@ slip_set_input_callback(void (*c)(void)) void slip_send(void) { - slip_write(&uip_buf[UIP_LLH_LEN], uip_len); + slip_write(uip_buf, uip_len); } /*---------------------------------------------------------------------------*/ void @@ -252,8 +252,7 @@ PROCESS_THREAD(slip_process, ev, data) slip_active = 1; /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); + uip_len = slip_poll_handler(uip_buf, UIP_BUFSIZE); if(uip_len > 0) { if(input_callback) { diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index 2435b9d2d..8109db20e 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -70,7 +70,7 @@ #endif /* WITH_DTLS */ /* sanity check for configured values */ -#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPH_LEN - UIP_UDPH_LEN) +#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN) #error "UIP_CONF_BUFFER_SIZE too small for COAP_MAX_CHUNK_SIZE" #endif diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index dab2c7b31..c10ec4d4f 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -114,7 +114,7 @@ static void mcast_fwd(void *p); struct multicast_on_behalf{ /* ICMP message of multicast_on_behalf */ uint16_t mcast_port; uip_ipaddr_t mcast_ip; - uint8_t mcast_payload[UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN]; + uint8_t mcast_payload[UIP_BUFSIZE - UIP_IPUDPH_LEN]; }; #define UIP_ICMP_MOB 18 /* Size of multicast_on_behalf ICMP header */ /*---------------------------------------------------------------------------*/ @@ -135,7 +135,7 @@ icmp_output() struct multicast_on_behalf *mob; mob = (struct multicast_on_behalf *)UIP_ICMP_PAYLOAD; - memcpy(&mob->mcast_payload, &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], uip_slen); + memcpy(&mob->mcast_payload, &uip_buf[UIP_IPUDPH_LEN], uip_slen); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; @@ -215,13 +215,13 @@ icmp_input() c->rport = locmobptr->mcast_port; uip_slen = loclen; uip_udp_conn=c; - memcpy(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], locmobptr->mcast_payload, - loclen > UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN? - UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN: loclen); + memcpy(&uip_buf[UIP_IPUDPH_LEN], locmobptr->mcast_payload, + loclen > UIP_BUFSIZE - UIP_IPUDPH_LEN? + UIP_BUFSIZE - UIP_IPUDPH_LEN: loclen); uip_process(UIP_UDP_SEND_CONN); - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; /* pass the packet to our uip_process to check if it is allowed to * accept this packet or not */ @@ -231,7 +231,7 @@ icmp_input() uip_process(UIP_DATA); - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; /* Return the IP of the original Multicast sender */ uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &src_ip); @@ -249,7 +249,7 @@ icmp_input() static void mcast_fwd(void *p) { - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); @@ -343,7 +343,7 @@ in() fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread)); } - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL); } diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index bd4040b55..0f865c438 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -276,7 +276,7 @@ struct mcast_packet { uint16_t seq_val; /* host-byte order */ struct sliding_window *sw; /* Pointer to the SW this packet belongs to */ uint8_t flags; /* Is-Used, Must Send, Is Listed */ - uint8_t buff[UIP_BUFSIZE - UIP_LLH_LEN]; + uint8_t buff[UIP_BUFSIZE]; }; /* Flag bits */ @@ -289,7 +289,7 @@ struct mcast_packet { #define MCAST_PACKET_GET_SEED(p) ((seed_id_t *)&((p)->seed_id)) #else #define MCAST_PACKET_GET_SEED(p) \ - ((seed_id_t *)&((struct uip_ip_hdr *)&(p)->buff[UIP_LLH_LEN])->srcipaddr) + ((seed_id_t *)&((struct uip_ip_hdr *)&(p)->buff[0])->srcipaddr) #endif /** @@ -1314,7 +1314,7 @@ static void out() { - if(uip_len + HBHO_TOTAL_LEN > UIP_BUFSIZE - UIP_LLH_LEN) { + if(uip_len + HBHO_TOTAL_LEN > UIP_BUFSIZE) { PRINTF("ROLL TM: Multicast Out can not add HBHO. Packet too long\n"); goto drop; } diff --git a/os/net/ipv6/multicast/smrf.c b/os/net/ipv6/multicast/smrf.c index c0e3bf0d0..dd24968ba 100644 --- a/os/net/ipv6/multicast/smrf.c +++ b/os/net/ipv6/multicast/smrf.c @@ -80,7 +80,7 @@ static uint8_t fwd_spread; static void mcast_fwd(void *p) { - memcpy(&uip_buf[UIP_LLH_LEN], &mcast_buf, mcast_len); + memcpy(uip_buf, &mcast_buf, mcast_len); uip_len = mcast_len; UIP_IP_BUF->ttl--; tcpip_output(NULL); @@ -174,7 +174,7 @@ in() fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread)); } - memcpy(&mcast_buf, &uip_buf[UIP_LLH_LEN], uip_len); + memcpy(&mcast_buf, uip_buf, uip_len); mcast_len = uip_len; ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL); } diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index 2a1a9f34f..fa183b686 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -1932,12 +1932,12 @@ input(void) /* Sanity-check size of incoming packet to avoid buffer overflow */ { - int req_size = UIP_LLH_LEN + uncomp_hdr_len + (uint16_t)(frag_offset << 3) + int req_size = uncomp_hdr_len + (uint16_t)(frag_offset << 3) + packetbuf_payload_len; if(req_size > sizeof(uip_buf)) { LOG_ERR( - "input: packet dropped, minimum required IP_BUF size: %d+%d+%d+%d=%d (current size: %u)\n", - UIP_LLH_LEN, uncomp_hdr_len, (uint16_t)(frag_offset << 3), + "input: packet dropped, minimum required IP_BUF size: %d+%d+%d=%d (current size: %u)\n", + uncomp_hdr_len, (uint16_t)(frag_offset << 3), packetbuf_payload_len, req_size, (unsigned)sizeof(uip_buf)); return; } diff --git a/os/net/ipv6/uip-packetqueue.h b/os/net/ipv6/uip-packetqueue.h index b265b1ecf..1deacb9ba 100644 --- a/os/net/ipv6/uip-packetqueue.h +++ b/os/net/ipv6/uip-packetqueue.h @@ -7,7 +7,7 @@ struct uip_packetqueue_handle; struct uip_packetqueue_packet { struct uip_ds6_queued_packet *next; - uint8_t queue_buf[UIP_BUFSIZE - UIP_LLH_LEN]; + uint8_t queue_buf[UIP_BUFSIZE]; uint16_t queue_buf_len; struct ctimer lifetimer; struct uip_packetqueue_handle *handle; diff --git a/os/net/ipv6/uip-udp-packet.c b/os/net/ipv6/uip-udp-packet.c index 91ddf598c..c5526b162 100644 --- a/os/net/ipv6/uip-udp-packet.c +++ b/os/net/ipv6/uip-udp-packet.c @@ -51,10 +51,10 @@ void uip_udp_packet_send(struct uip_udp_conn *c, const void *data, int len) { #if UIP_UDP - if(data != NULL && len <= (UIP_BUFSIZE - (UIP_LLH_LEN + UIP_IPUDPH_LEN))) { + if(data != NULL && len <= (UIP_BUFSIZE - UIP_IPUDPH_LEN)) { uip_udp_conn = c; uip_slen = len; - memmove(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], data, len); + memmove(&uip_buf[UIP_IPUDPH_LEN], data, len); uip_process(UIP_UDP_SEND_CONN); #if UIP_IPV6_MULTICAST diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index a09fb6fb2..2e83607bd 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -64,7 +64,7 @@ #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP + TCP header */ #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN #define UIP_IPICMPH_LEN (UIP_IPH_LEN + UIP_ICMPH_LEN) /* Size of ICMP + IP header */ -#define UIP_LLIPH_LEN (UIP_LLH_LEN + UIP_IPH_LEN) /* Size of L2 + IP header */ +#define UIP_LLIPH_LEN (UIP_IPH_LEN) /* Size of L2 + IP header */ /** * The sums below are quite used in ND. When used for uip_buf, we @@ -72,14 +72,14 @@ * we need values with and without LLH_LEN we do not use capital * letters as these values are variable */ -#define uip_l2_l3_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len) -#define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) +#define uip_l2_l3_hdr_len (UIP_IPH_LEN + uip_ext_len) +#define uip_l2_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) /** * Direct access to IPv6 header */ -#define UIP_IP_BUF_CHAR ((unsigned char *)uip_buf + UIP_LLH_LEN) +#define UIP_IP_BUF_CHAR ((unsigned char *)uip_buf) #define UIP_IP_BUF ((struct uip_ip_hdr *)UIP_IP_BUF_CHAR) #define UIP_IP_PAYLOAD(ext) ((unsigned char *)UIP_IP_BUF_CHAR + UIP_IPH_LEN + (ext)) @@ -1780,7 +1780,7 @@ struct uip_udp_hdr { * * \hideinitializer */ -#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) +#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_TCPIP_HLEN) #define UIP_PROTO_ICMP 1 #define UIP_PROTO_TCP 6 diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 8b7eea361..19f786d3f 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -321,7 +321,7 @@ uip_ipchksum(void) { uint16_t sum; - sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN); + sum = chksum(0, uip_buf, UIP_IPH_LEN); LOG_DBG("uip_ipchksum: sum 0x%04x\n", sum); return (sum == 0) ? 0xffff : uip_htons(sum); } @@ -587,7 +587,7 @@ uip_listen(uint16_t port) /*---------------------------------------------------------------------------*/ #if UIP_CONF_IPV6_REASSEMBLY -#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN) +#define UIP_REASS_BUFSIZE (UIP_BUFSIZE) static uint8_t uip_reassbuf[UIP_REASS_BUFSIZE]; @@ -935,7 +935,7 @@ uip_process(uint8_t flag) goto udp_send; } #endif /* UIP_UDP */ - uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; /* Check if we were invoked because of a poll request for a particular connection. */ @@ -1072,7 +1072,7 @@ uip_process(uint8_t flag) if(flag == UIP_UDP_TIMER) { if(uip_udp_conn->lport != 0) { uip_conn = NULL; - uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_len = uip_slen = 0; uip_flags = UIP_POLL; UIP_UDP_APPCALL(); @@ -1512,10 +1512,10 @@ uip_process(uint8_t flag) UIP_STAT(++uip_stat.udp.recv); uip_len = uip_len - UIP_IPUDPH_LEN; - uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_conn = NULL; uip_flags = UIP_NEWDATA; - uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN + UIP_LLH_LEN]; + uip_sappdata = uip_appdata = &uip_buf[UIP_IPUDPH_LEN]; uip_slen = 0; UIP_UDP_APPCALL(); @@ -1545,7 +1545,7 @@ uip_process(uint8_t flag) uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &uip_udp_conn->ripaddr); uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr); - uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN]; + uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; #if UIP_UDP_CHECKSUMS /* Calculate UDP checksum. */ @@ -1728,7 +1728,7 @@ uip_process(uint8_t flag) /* Parse the TCP MSS option, if present. */ if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) { for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) { - opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c]; + opt = uip_buf[UIP_TCPIP_HLEN + c]; if(opt == TCP_OPT_END) { /* End of options. */ break; @@ -1736,10 +1736,10 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_TCPIP_HLEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = ((uint16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | - (uint16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c]; + tmp16 = ((uint16_t)uip_buf[UIP_TCPIP_HLEN + 2 + c] << 8) | + (uint16_t)uip_buf[UIP_IPTCPH_LEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1748,12 +1748,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { + if(uip_buf[UIP_TCPIP_HLEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; + c += uip_buf[UIP_TCPIP_HLEN + 1 + c]; } } } @@ -1915,7 +1915,7 @@ uip_process(uint8_t flag) /* Parse the TCP MSS option, if present. */ if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) { for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) { - opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c]; + opt = uip_buf[UIP_IPTCPH_LEN + c]; if(opt == TCP_OPT_END) { /* End of options. */ break; @@ -1923,10 +1923,10 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_TCPIP_HLEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | - uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c]; + tmp16 = (uip_buf[UIP_TCPIP_HLEN + 2 + c] << 8) | + uip_buf[UIP_TCPIP_HLEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1935,12 +1935,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { + if(uip_buf[UIP_TCPIP_HLEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; + c += uip_buf[UIP_TCPIP_HLEN + 1 + c]; } } } @@ -2054,7 +2054,7 @@ uip_process(uint8_t flag) When the application is called, the global variable uip_len contains the length of the incoming data. The application can access the incoming data through the global pointer - uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN + uip_appdata, which usually points UIP_IPTCPH_LEN bytes into the uip_buf array. If the application wishes to send any data, this data should be @@ -2307,10 +2307,10 @@ uip_send(const void *data, int len) int copylen; if(uip_sappdata != NULL) { - copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN - + copylen = MIN(len, UIP_BUFSIZE - UIP_TCPIP_HLEN - (int)((char *)uip_sappdata - (char *)UIP_TCP_PAYLOAD)); } else { - copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN); + copylen = MIN(len, UIP_BUFSIZE - UIP_TCPIP_HLEN); } if(copylen > 0) { uip_slen = copylen; diff --git a/os/net/ipv6/uipopt.h b/os/net/ipv6/uipopt.h index f01689f4f..bd2dcc502 100644 --- a/os/net/ipv6/uipopt.h +++ b/os/net/ipv6/uipopt.h @@ -121,26 +121,6 @@ * @{ */ -/** - * The link level header length. - * - * This is the offset into the uip_buf where the IP header can be - * found. For Ethernet, this should be set to 14. For SLIP, this - * should be set to 0. - * - * \note we probably won't use this constant for other link layers than - * ethernet as they have variable header length (this is due to variable - * number and type of address fields and to optional security features) - * E.g.: 802.15.4 -> 2 + (1/2*4/8) + 0/5/6/10/14 - * 802.11 -> 4 + (6*3/4) + 2 - * \hideinitializer - */ -#ifdef UIP_CONF_LLH_LEN -#define UIP_LLH_LEN (UIP_CONF_LLH_LEN) -#else /* UIP_LLH_LEN */ -#define UIP_LLH_LEN 0 -#endif /* UIP_CONF_LLH_LEN */ - /** * The size of the uIP packet buffer. * @@ -151,12 +131,11 @@ * \hideinitializer */ #ifndef UIP_CONF_BUFFER_SIZE -#define UIP_BUFSIZE (UIP_LINK_MTU + UIP_LLH_LEN) +#define UIP_BUFSIZE (UIP_LINK_MTU) #else /* UIP_CONF_BUFFER_SIZE */ #define UIP_BUFSIZE (UIP_CONF_BUFFER_SIZE) #endif /* UIP_CONF_BUFFER_SIZE */ - /** * Determines if statistics support should be compiled in. * @@ -435,15 +414,15 @@ void uip_log(char *msg); * The TCP maximum segment size. * * This is should not be to set to more than - * UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN. + * UIP_BUFSIZE - UIP_TCPIP_HLEN. */ #ifdef UIP_CONF_TCP_MSS -#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) +#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_TCPIP_HLEN) #error UIP_CONF_TCP_MSS is too large for the current UIP_BUFSIZE -#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) */ +#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_TCPIP_HLEN) */ #define UIP_TCP_MSS (UIP_CONF_TCP_MSS) #else /* UIP_CONF_TCP_MSS */ -#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN) +#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_TCPIP_HLEN) #endif /* UIP_CONF_TCP_MSS */ /** diff --git a/os/services/ip64/ip64-arp.c b/os/services/ip64/ip64-arp.c index 75197558d..daf27d959 100644 --- a/os/services/ip64/ip64-arp.c +++ b/os/services/ip64/ip64-arp.c @@ -139,7 +139,7 @@ ip64_arp_timer(void) { struct arp_entry *tabptr; int i; - + ++arptime; for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; @@ -157,7 +157,7 @@ arp_update(uip_ip4addr_t *ipaddr, struct uip_eth_addr *ethaddr) { register struct arp_entry *tabptr = arp_table; int i, c; - + /* Walk through the ARP mapping table and try to find an entry to update. If none is found, the IP -> MAC address mapping is inserted in the ARP table. */ @@ -170,7 +170,7 @@ arp_update(uip_ip4addr_t *ipaddr, struct uip_eth_addr *ethaddr) /* Check if the source IP address of the incoming packet matches the IP address in this ARP table entry. */ if(uip_ip4addr_cmp(ipaddr, &tabptr->ipaddr)) { - + /* An old entry found, update this and return. */ memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); tabptr->time = arptime; @@ -239,7 +239,7 @@ ip64_arp_arp_input(const uint8_t *packet, uint16_t packet_len) table, since it is likely that we will do more communication with this host in the future. */ arp_update(&arphdr->sipaddr, &arphdr->shwaddr); - + arphdr->opcode = UIP_HTONS(ARP_REPLY); memcpy(arphdr->dhwaddr.addr, arphdr->shwaddr.addr, 6); @@ -275,7 +275,7 @@ ip64_arp_check_cache(const uint8_t *nlhdr) printf("check cache %d.%d.%d.%d\n", uip_ipaddr_to_quad(&ipv4_hdr->destipaddr)); - + /* First check if destination is a local broadcast. */ uip_ipaddr(&broadcast_addr, 255,255,255,255); if(uip_ip4addr_cmp(&ipv4_hdr->destipaddr, &broadcast_addr)) { @@ -321,7 +321,7 @@ ip64_arp_create_ethhdr(uint8_t *llhdr, const uint8_t *nlhdr) struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)nlhdr; struct ip64_eth_hdr *ethhdr = (struct ip64_eth_hdr *)llhdr; uip_ip4addr_t broadcast_addr; - + /* Find the destination IP address in the ARP table and construct the Ethernet header. If the destination IP addres isn't on the local network, we use the default router's IP address instead. @@ -371,7 +371,7 @@ ip64_arp_create_ethhdr(uint8_t *llhdr, const uint8_t *nlhdr) } memcpy(ethhdr->src.addr, ip64_eth_addr.addr, 6); - + ethhdr->type = UIP_HTONS(IP64_ETH_TYPE_IP); return sizeof(struct ip64_eth_hdr); } @@ -382,7 +382,7 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)nlhdr; struct arp_hdr *arp_hdr = (struct arp_hdr *)llhdr; uip_ip4addr_t ipaddr; - + if(!uip_ipaddr_maskcmp(&ipv4_hdr->destipaddr, ip64_get_hostaddr(), ip64_get_netmask())) { @@ -394,7 +394,7 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) /* Else, we use the destination IP address. */ uip_ip4addr_copy(&ipaddr, &ipv4_hdr->destipaddr); } - + memset(arp_hdr->ethhdr.dest.addr, 0xff, 6); memset(arp_hdr->dhwaddr.addr, 0x00, 6); memcpy(arp_hdr->ethhdr.src.addr, ip64_eth_addr.addr, 6); @@ -408,8 +408,8 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) arp_hdr->hwlen = 6; arp_hdr->protolen = 4; arp_hdr->ethhdr.type = UIP_HTONS(IP64_ETH_TYPE_ARP); - - uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; + + uip_appdata = &uip_buf[UIP_TCPIP_HLEN]; return sizeof(struct arp_hdr); } diff --git a/os/services/ip64/ip64-dhcpc.c b/os/services/ip64/ip64-dhcpc.c index 8e8e286f8..10a5184e3 100644 --- a/os/services/ip64/ip64-dhcpc.c +++ b/os/services/ip64/ip64-dhcpc.c @@ -61,7 +61,7 @@ struct dhcp_msg { uint8_t options[312]; }; -#if (UIP_BUFSIZE - UIP_LLH_LEN - UIP_UDPIP_HLEN) < 548 +#if (UIP_BUFSIZE - UIP_UDPIP_HLEN) < 548 #error UIP_CONF_BUFFER_SIZE may be too small to accomodate DHCPv4 packets #error Increase UIP_CONF_BUFFER_SIZE in your project-conf.h, or contiki-conf.h #error A good size is 600 bytes @@ -191,12 +191,12 @@ send_request(void) struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; create_msg(m); - + end = add_msg_type(&m->options[4], DHCPREQUEST); end = add_server_id(end); end = add_req_ipaddr(end); end = add_end(end); - + uip_send(uip_appdata, (int)(end - (uint8_t *)uip_appdata)); } /*---------------------------------------------------------------------------*/ @@ -239,7 +239,7 @@ static uint8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; - + if(m->op == DHCP_REPLY && memcmp(m->xid, &xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { @@ -258,7 +258,7 @@ msg_for_me(void) struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; uint8_t *optptr = &m->options[4]; uint8_t *end = (uint8_t*)uip_appdata + uip_datalen(); - + if(m->op == DHCP_REPLY && memcmp(m->xid, &xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { @@ -280,7 +280,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) clock_time_t ticks; PT_BEGIN(&s.pt); - + init: xid++; s.state = STATE_SENDING; @@ -305,7 +305,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) s.ticks *= 2; } } - + selecting: s.ticks = CLOCK_SECOND; do { @@ -330,7 +330,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) goto init; } } while(s.state != STATE_CONFIG_RECEIVED); - + bound: #if 0 printf("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr)); @@ -343,7 +343,7 @@ PT_THREAD(handle_dhcp(process_event_t ev, void *data)) #endif ip64_dhcpc_configured(&s); - + #define MAX_TICKS (~((clock_time_t)0) / 2) #define MAX_TICKS32 (~((uint32_t)0)) #define IMIN(a, b) ((a) < (b) ? (a) : (b)) @@ -407,12 +407,12 @@ ip64_dhcpc_init(const void *mac_addr, int mac_len) uip_ip6addr_t v6addr; uip_ip4addr_t v4addr; struct uip_udp_conn *conn2; - + s.mac_addr = mac_addr; s.mac_len = mac_len; s.state = STATE_INITIAL; - uip_ipaddr(&v4addr, 255,255,255,255); + uip_ipaddr(&v4addr, 255,255,255,255); ip64_addr_4to6(&v4addr, &v6addr); s.conn = udp_new(&v6addr, UIP_HTONS(IP64_DHCPC_SERVER_PORT), NULL); conn2 = udp_new(NULL, UIP_HTONS(IP64_DHCPC_SERVER_PORT), NULL); @@ -437,7 +437,7 @@ void ip64_dhcpc_request(void) { uip_ipaddr_t ipaddr; - + if(s.state == STATE_INITIAL) { uip_ipaddr(&ipaddr, 0,0,0,0); uip_sethostaddr(&ipaddr); diff --git a/os/services/ip64/ip64-eth-interface.c b/os/services/ip64/ip64-eth-interface.c index d70b7576a..dc89610e5 100644 --- a/os/services/ip64/ip64-eth-interface.c +++ b/os/services/ip64/ip64-eth-interface.c @@ -60,7 +60,7 @@ ip64_eth_interface_input(uint8_t *packet, uint16_t len) printf("-------------->\n"); uip_len = ip64_4to6(&packet[sizeof(struct ip64_eth_hdr)], len - sizeof(struct ip64_eth_hdr), - &uip_buf[UIP_LLH_LEN]); + uip_buf); if(uip_len > 0) { printf("ip64_interface_process: converted %d bytes\n", uip_len); @@ -94,7 +94,7 @@ output(void) PRINTF("\n"); printf("<--------------\n"); - len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len, + len = ip64_6to4(uip_buf, uip_len, &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]); printf("ip64-interface: output len %d\n", len); diff --git a/os/services/ip64/ip64-slip-interface.c b/os/services/ip64/ip64-slip-interface.c index 209dfcb91..fb4f69627 100644 --- a/os/services/ip64/ip64-slip-interface.c +++ b/os/services/ip64/ip64-slip-interface.c @@ -55,24 +55,24 @@ static void input_callback(void) { /*PRINTF("SIN: %u\n", uip_len);*/ - if(uip_buf[UIP_LLH_LEN] == '!') { - PRINTF("Got configuration message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); + if(uip_buf[0] == '!') { + PRINTF("Got configuration message of type %c\n", uip_buf[1]); uipbuf_clear(); #if 0 - if(uip_buf[UIP_LLH_LEN + 1] == 'P') { + if(uip_buf[1] == 'P') { uip_ipaddr_t prefix; /* Here we set a prefix !!! */ memset(&prefix, 0, 16); - memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8); + memcpy(&prefix, &uip_buf[2], 8); PRINTF("Setting prefix "); PRINT6ADDR(&prefix); PRINTF("\n"); set_prefix_64(&prefix); } #endif - } else if(uip_buf[UIP_LLH_LEN] == '?') { - PRINTF("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'M') { + } else if(uip_buf[0] == '?') { + PRINTF("Got request message of type %c\n", uip_buf[1]); + if(uip_buf[1] == 'M') { const char *hexchar = "0123456789abcdef"; int j; /* this is just a test so far... just to see if it works */ @@ -91,10 +91,10 @@ input_callback(void) packet back if no route is found */ uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr); - uint16_t len = ip64_4to6(&uip_buf[UIP_LLH_LEN], uip_len, + uint16_t len = ip64_4to6(uip_buf, uip_len, ip64_packet_buffer); if(len > 0) { - memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len); + memcpy(uip_buf, ip64_packet_buffer, len); uip_len = len; /* PRINTF("send len %d\n", len); */ } else { @@ -127,11 +127,11 @@ output(void) if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) { PRINTF("ip64-interface: output, not sending bounced message\n"); } else { - len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len, + len = ip64_6to4(uip_buf, uip_len, ip64_packet_buffer); PRINTF("ip64-interface: output len %d\n", len); if(len > 0) { - memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len); + memcpy(uip_buf, ip64_packet_buffer, len); uip_len = len; slip_send(); return len; diff --git a/os/services/rpl-border-router/embedded/slip-bridge.c b/os/services/rpl-border-router/embedded/slip-bridge.c index 6ce3f79fd..b1349ac0a 100644 --- a/os/services/rpl-border-router/embedded/slip-bridge.c +++ b/os/services/rpl-border-router/embedded/slip-bridge.c @@ -68,14 +68,14 @@ static void slip_input_callback(void) { LOG_DBG("SIN: %u\n", uip_len); - if(uip_buf[UIP_LLH_LEN] == '!') { + if(uip_buf[0] == '!') { LOG_INFO("Got configuration message of type %c\n", - uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'P') { + uip_buf[1]); + if(uip_buf[1] == 'P') { uip_ipaddr_t prefix; /* Here we set a prefix !!! */ memset(&prefix, 0, 16); - memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8); + memcpy(&prefix, &uip_buf[2], 8); uipbuf_clear(); @@ -86,9 +86,9 @@ slip_input_callback(void) } uipbuf_clear(); - } else if(uip_buf[UIP_LLH_LEN] == '?') { - LOG_INFO("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]); - if(uip_buf[UIP_LLH_LEN + 1] == 'M') { + } else if(uip_buf[0] == '?') { + LOG_INFO("Got request message of type %c\n", uip_buf[1]); + if(uip_buf[1] == 'M') { char *hexchar = "0123456789abcdef"; int j; /* this is just a test so far... just to see if it works */ diff --git a/os/services/rpl-border-router/native/tun-bridge.c b/os/services/rpl-border-router/native/tun-bridge.c index f321577b3..b358bf2f9 100644 --- a/os/services/rpl-border-router/native/tun-bridge.c +++ b/os/services/rpl-border-router/native/tun-bridge.c @@ -256,7 +256,7 @@ output(void) { LOG_DBG("SUT: %u\n", uip_len); if(uip_len > 0) { - return tun_output(&uip_buf[UIP_LLH_LEN], uip_len); + return tun_output(uip_buf, uip_len); } return 0; } @@ -298,7 +298,7 @@ handle_fd(fd_set *rset, fd_set *wset) int size; if(FD_ISSET(tunfd, rset)) { - size = tun_input(&uip_buf[UIP_LLH_LEN], sizeof(uip_buf)); + size = tun_input(uip_buf, sizeof(uip_buf)); /* printf("TUN data incoming read:%d\n", size); */ uip_len = size; tcpip_input(); diff --git a/tools/sky/uip6-bridge/Makefile b/tools/sky/uip6-bridge/Makefile deleted file mode 100644 index 55da6798e..000000000 --- a/tools/sky/uip6-bridge/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -ifdef CONTIKI_HOME -CONTIKI = $(CONTIKI_HOME) -else -ifndef CONTIKI -CONTIKI=../../.. -endif -endif - -DEFINES=WITH_SLIP=1 - -ifndef TARGET -TARGET=sky -endif - -MODULES_REL += dev -PROJECT_SOURCEFILES = fakeuip.c sicslow_ethernet.c - -all: uip6-bridge-tap.sky - -upload: uip6-bridge-tap.ihex - cp $< $(IHEXFILE) - $(MAKE) sky-u.$(subst /,-,$(word $(MOTE), $(MOTES))) - -include $(CONTIKI)/Makefile.include - -../../tapslip6: ../../tapslip6.c - (cd ../../; $(MAKE) tapslip6) - -ifndef MOTE -MOTE=1 -endif - -connect: ../../tapslip6 - sudo ../../tapslip6 -t tap0 -s $(USBDEVPREFIX)$(word $(MOTE), $(CMOTES)) 127.0.0.1 255.0.0.0 - -bridge: - @sudo service radvd restart || echo radvd could not be restarted - sudo route add -6 fd00::/64 tap0 - sudo ip -6 address add fd00::1/64 dev tap0 diff --git a/tools/sky/uip6-bridge/dev/slip.c b/tools/sky/uip6-bridge/dev/slip.c deleted file mode 100644 index 7cde6437d..000000000 --- a/tools/sky/uip6-bridge/dev/slip.c +++ /dev/null @@ -1,322 +0,0 @@ -/* -*- C -*- */ -/* - * Copyright (c) 2005, Swedish Institute of Computer Science - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - */ - - -#include -#include -#include "dev/ds2411/ds2411.h" -#include "contiki.h" - -#include "net/ipv6/uip.h" - -#include "dev/slip.h" - -#define SLIP_END 0300 -#define SLIP_ESC 0333 -#define SLIP_ESC_END 0334 -#define SLIP_ESC_ESC 0335 - -PROCESS(slip_process, "SLIP driver"); - -uint8_t slip_active; - -#if 1 -#define SLIP_STATISTICS(statement) -#else -uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; -#define SLIP_STATISTICS(statement) statement -#endif - -/* Must be at least one byte larger than UIP_BUFSIZE! */ -#define RX_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN + 16) - -enum { - STATE_TWOPACKETS = 0, /* We have 2 packets and drop incoming data. */ - STATE_OK = 1, - STATE_ESC = 2, - STATE_RUBBISH = 3, -}; - -/* - * Variables begin and end manage the buffer space in a cyclic - * fashion. The first used byte is at begin and end is one byte past - * the last. I.e. [begin, end) is the actively used space. - * - * If begin != pkt_end we have a packet at [begin, pkt_end), - * furthermore, if state == STATE_TWOPACKETS we have one more packet at - * [pkt_end, end). If more bytes arrive in state STATE_TWOPACKETS - * they are discarded. - */ - -static uint8_t state = STATE_TWOPACKETS; -static uint16_t begin, end; -static uint8_t rxbuf[RX_BUFSIZE]; -static uint16_t pkt_end; /* SLIP_END tracker. */ - -static void (* input_callback)(void) = NULL; -static void (* tcpip_input_callback)(void) = NULL; -/*---------------------------------------------------------------------------*/ -void -slip_set_input_callback(void (*c)(void)) -{ - input_callback = c; -} -/*---------------------------------------------------------------------------*/ -void -slip_set_tcpip_input_callback(void (*c)(void)) -{ - tcpip_input_callback = c; -} -/*---------------------------------------------------------------------------*/ -uint8_t -slip_write(const void *_ptr, int len) -{ - const uint8_t *ptr = _ptr; - uint16_t i; - uint8_t c; - - slip_arch_writeb(SLIP_END); - - for(i = 0; i < len; ++i) { - c = *ptr++; - if(c == SLIP_END) { - slip_arch_writeb(SLIP_ESC); - c = SLIP_ESC_END; - } else if(c == SLIP_ESC) { - slip_arch_writeb(SLIP_ESC); - c = SLIP_ESC_ESC; - } - slip_arch_writeb(c); - } - slip_arch_writeb(SLIP_END); - - return len; -} -/*---------------------------------------------------------------------------*/ -static void -rxbuf_init(void) -{ - begin = end = pkt_end = 0; - state = STATE_OK; -} -/*---------------------------------------------------------------------------*/ -/* Upper half does the polling. */ -static uint16_t -slip_poll_handler(uint8_t *outbuf, uint16_t blen) -{ - /* This is a hack and won't work across buffer edge! */ - if(rxbuf[begin] == 'C') { - int i; - if(begin < end && (end - begin) >= 6 - && memcmp(&rxbuf[begin], "CLIENT", 6) == 0) { - state = STATE_TWOPACKETS; /* Interrupts do nothing. */ - memset(&rxbuf[begin], 0x0, 6); - - rxbuf_init(); - - for(i = 0; i < 13; i++) { - slip_arch_writeb("CLIENTSERVER\300"[i]); - } - return 0; - } - } else if(rxbuf[begin] == '?') { - int i, j; - char* hexchar = "0123456789abcdef"; - if(begin < end && (end - begin) >= 2 - && rxbuf[begin + 1] == 'M') { - state = STATE_TWOPACKETS; /* Interrupts do nothing. */ - rxbuf[begin] = 0; - rxbuf[begin + 1] = 0; - - rxbuf_init(); - - /* this is just a test so far... just to see if it works */ - slip_arch_writeb('!'); - slip_arch_writeb('M'); - for(j = 0; j < 8; j++) { - slip_arch_writeb(hexchar[ds2411_id[j] >> 4]); - slip_arch_writeb(hexchar[ds2411_id[j] & 15]); - } - slip_arch_writeb(SLIP_END); - return 0; - } - } - - /* - * Interrupt can not change begin but may change pkt_end. - * If pkt_end != begin it will not change again. - */ - if(begin != pkt_end) { - uint16_t len; - - if(begin < pkt_end) { - len = pkt_end - begin; - if(len > blen) { - len = 0; - } else { - memcpy(outbuf, &rxbuf[begin], len); - } - } else { - len = (RX_BUFSIZE - begin) + (pkt_end - 0); - if(len > blen) { - len = 0; - } else { - unsigned i; - for(i = begin; i < RX_BUFSIZE; i++) { - *outbuf++ = rxbuf[i]; - } - for(i = 0; i < pkt_end; i++) { - *outbuf++ = rxbuf[i]; - } - } - } - - /* Remove data from buffer together with the copied packet. */ - begin = pkt_end; - if(state == STATE_TWOPACKETS) { - pkt_end = end; - state = STATE_OK; /* Assume no bytes where lost! */ - - /* One more packet is buffered, need to be polled again! */ - process_poll(&slip_process); - } - return len; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ -PROCESS_THREAD(slip_process, ev, data) -{ - PROCESS_BEGIN(); - - rxbuf_init(); - - while(1) { - PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); - - slip_active = 1; - - /* Move packet from rxbuf to buffer provided by uIP. */ - uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN], - UIP_BUFSIZE - UIP_LLH_LEN); - if(uip_len > 0) { - if(tcpip_input_callback) { - tcpip_input_callback(); - } else { - tcpip_input(); - } - } - } - - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/ -int -slip_input_byte(unsigned char c) -{ - switch(state) { - case STATE_RUBBISH: - if(c == SLIP_END) { - state = STATE_OK; - } - return 0; - - case STATE_TWOPACKETS: /* Two packets are already buffered! */ - return 0; - - case STATE_ESC: - if(c == SLIP_ESC_END) { - c = SLIP_END; - } else if(c == SLIP_ESC_ESC) { - c = SLIP_ESC; - } else { - state = STATE_RUBBISH; - SLIP_STATISTICS(slip_rubbish++); - end = pkt_end; /* remove rubbish */ - return 0; - } - state = STATE_OK; - break; - - case STATE_OK: - if(c == SLIP_ESC) { - state = STATE_ESC; - return 0; - } else if(c == SLIP_END) { - /* - * We have a new packet, possibly of zero length. - * - * There may already be one packet buffered. - */ - if(end != pkt_end) { /* Non zero length. */ - if(begin == pkt_end) { /* None buffered. */ - pkt_end = end; - } else { - state = STATE_TWOPACKETS; - SLIP_STATISTICS(slip_twopackets++); - } - process_poll(&slip_process); - return 1; - } - return 0; - } - break; - } - - /* add_char: */ - { - unsigned next; - next = end + 1; - if(next == RX_BUFSIZE) { - next = 0; - } - if(next == begin) { /* rxbuf is full */ - state = STATE_RUBBISH; - SLIP_STATISTICS(slip_overflow++); - end = pkt_end; /* remove rubbish */ - return 0; - } - rxbuf[end] = c; - end = next; - } - - /* There could be a separate poll routine for this. */ - if(c == 'T' && rxbuf[begin] == 'C') { - process_poll(&slip_process); - return 1; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ diff --git a/tools/sky/uip6-bridge/dev/slip.h b/tools/sky/uip6-bridge/dev/slip.h deleted file mode 100644 index d164ee97c..000000000 --- a/tools/sky/uip6-bridge/dev/slip.h +++ /dev/null @@ -1,92 +0,0 @@ -/* -*- C -*- */ -/* - * Copyright (c) 2005, Swedish Institute of Computer Science - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - */ - -#ifndef SLIP_H_ -#define SLIP_H_ - -#include "contiki.h" - -PROCESS_NAME(slip_process); - -/** - * Send an IP packet from the uIP buffer with SLIP. - */ -uint8_t slip_send(void); - -/** - * Input a SLIP byte. - * - * This function is called by the RS232/SIO device driver to pass - * incoming bytes to the SLIP driver. The function can be called from - * an interrupt context. - * - * For systems using low-power CPU modes, the return value of the - * function can be used to determine if the CPU should be woken up or - * not. If the function returns non-zero, the CPU should be powered - * up. If the function returns zero, the CPU can continue to be - * powered down. - * - * \param c The data that is to be passed to the SLIP driver - * - * \return Non-zero if the CPU should be powered up, zero otherwise. - */ -int slip_input_byte(unsigned char c); - -uint8_t slip_write(const void *ptr, int len); - -/* Did we receive any bytes lately? */ -extern uint8_t slip_active; - -/* Statistics. */ -extern uint16_t slip_rubbish, slip_twopackets, slip_overflow, slip_ip_drop; - -/** - * Set a function to be called when there is activity on the SLIP - * interface; used for detecting if a node is a gateway node. - */ -void slip_set_input_callback(void (*callback)(void)); - -/** - * Set a function to be called when a packet has been received. - * Default is tcpip_input(). - */ -void slip_set_tcpip_input_callback(void (*callback)(void)); - -/* - * These machine dependent functions and an interrupt service routine - * must be provided externally (slip_arch.c). - */ -void slip_arch_init(void); -void slip_arch_writeb(unsigned char c); - -#endif /* SLIP_H_ */ diff --git a/tools/sky/uip6-bridge/fakeuip.c b/tools/sky/uip6-bridge/fakeuip.c deleted file mode 100644 index 6f8d650eb..000000000 --- a/tools/sky/uip6-bridge/fakeuip.c +++ /dev/null @@ -1,134 +0,0 @@ - -/* Various stub functions and uIP variables other code might need to - * compile. Allows you to save needing to compile all of uIP in just - * to get a few things */ - - -#include "net/ipv6/uip.h" -#include "net/ipv6/uip-ds6.h" -#include - -uip_buf_t uip_aligned_buf; - -uint16_t uip_len; - -struct uip_stats uip_stat; - -uip_lladdr_t uip_lladdr; - -uint16_t uip_htons(uint16_t val) { return UIP_HTONS(val);} - -uip_ds6_netif_t uip_ds6_if; - -/********** UIP_DS6.c **********/ - -void -uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr) -{ - /* We consider only links with IEEE EUI-64 identifier or - IEEE 48-bit MAC addresses */ -#if (UIP_LLADDR_LEN == 8) - memcpy(ipaddr->u8 + 8, lladdr, UIP_LLADDR_LEN); - ipaddr->u8[8] ^= 0x02; -#elif (UIP_LLADDR_LEN == 6) - memcpy(ipaddr->u8 + 8, lladdr, 3); - ipaddr->u8[11] = 0xff; - ipaddr->u8[12] = 0xfe; - memcpy(ipaddr->u8 + 13, lladdr + 3, 3); - ipaddr->u8[8] ^= 0x02; -#else -#error fakeuip.c cannot build interface address when UIP_LLADDR_LEN is not 6 or 8 -#endif -} - -/*---------------------------------------------------------------------------*/ -/* - * get a link local address - - * state = -1 => any address is ok. Otherwise state = desired state of addr. - * (TENTATIVE, PREFERRED, DEPRECATED) - */ -uip_ds6_addr_t * -uip_ds6_get_link_local(int8_t state) { - uip_ds6_addr_t *locaddr; - for(locaddr = uip_ds6_if.addr_list; - locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) { - if((locaddr->isused) && (state == - 1 || locaddr->state == state) - && (uip_is_addr_linklocal(&locaddr->ipaddr))) { - return locaddr; - } - } - return NULL; -} - -uip_ds6_addr_t * -uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type) -{ - return NULL; -} -/********** UIP.c ****************/ - -static uint16_t -chksum(uint16_t sum, const uint8_t *data, uint16_t len) -{ - uint16_t t; - const uint8_t *dataptr; - const uint8_t *last_byte; - - dataptr = data; - last_byte = data + len - 1; - - while(dataptr < last_byte) { /* At least two more bytes */ - t = (dataptr[0] << 8) + dataptr[1]; - sum += t; - if(sum < t) { - sum++; /* carry */ - } - dataptr += 2; - } - - if(dataptr == last_byte) { - t = (dataptr[0] << 8) + 0; - sum += t; - if(sum < t) { - sum++; /* carry */ - } - } - - /* Return sum in host byte order. */ - return sum; -} - -static uint16_t -upper_layer_chksum(uint8_t proto) -{ - uint16_t upper_layer_len; - uint16_t sum; - - upper_layer_len = uipbuf_get_len_field(UIP_IP_BUF); - - /* First sum pseudoheader. */ - /* IP protocol and length fields. This addition cannot carry. */ - sum = upper_layer_len + proto; - /* Sum IP source and destination addresses. */ - sum = chksum(sum, (uint8_t *)&UIP_IP_BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); - - /* Sum TCP header and data. */ - sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], - upper_layer_len); - - return (sum == 0) ? 0xffff : uip_htons(sum); -} - -/*---------------------------------------------------------------------------*/ -uint16_t -uip_icmp6chksum(void) -{ - return upper_layer_chksum(UIP_PROTO_ICMP6); -} - -/*---------------------------------------------------------------------------*/ -void -uip_ds6_link_callback(int status, int numtx) -{ - -} diff --git a/tools/sky/uip6-bridge/project-conf.h b/tools/sky/uip6-bridge/project-conf.h deleted file mode 100644 index 651d45cda..000000000 --- a/tools/sky/uip6-bridge/project-conf.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2009, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - */ - -/** - * \file - * A brief description of what this file is - * \author - * Niclas Finne - * Joakim Eriksson - */ - -#ifndef BRIDGE_CONF_H_ -#define BRIDGE_CONF_H_ - -#undef UIP_CONF_ROUTER - -#undef UIP_CONF_LLH_LEN -#define UIP_CONF_LLH_LEN 14 - -#undef UIP_CONF_BUFFER_SIZE -#define UIP_CONF_BUFFER_SIZE 256 -#endif /* BRIDGE_CONF_H_ */ diff --git a/tools/sky/uip6-bridge/radvd.conf b/tools/sky/uip6-bridge/radvd.conf deleted file mode 100644 index d6dd41e2b..000000000 --- a/tools/sky/uip6-bridge/radvd.conf +++ /dev/null @@ -1,16 +0,0 @@ -interface tap0 { - AdvSendAdvert on; - AdvLinkMTU 1280; - AdvCurHopLimit 128; - AdvReachableTime 360000; - MinRtrAdvInterval 100; - MaxRtrAdvInterval 150; - AdvDefaultLifetime 200; - prefix AAAA::/64 - { - AdvOnLink on; - AdvAutonomous on; - AdvPreferredLifetime 4294967295; - AdvValidLifetime 4294967295; - }; -}; diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.c b/tools/sky/uip6-bridge/sicslow_ethernet.c deleted file mode 100644 index cd60acd57..000000000 --- a/tools/sky/uip6-bridge/sicslow_ethernet.c +++ /dev/null @@ -1,879 +0,0 @@ -/** - * \file sicslow_ethernet.c - * Routines to interface between Ethernet and 6LowPan - * - * \author - * Colin O'Flynn - * - * \addtogroup usbstick - */ - -/* Copyright (c) 2008 by: - * Colin O'Flynn coflynn@newae.com - * Eric Gnoske egnoske@gmail.com - * Blake Leverett bleverett@gmail.com - * Mike Vidales mavida404@gmail.com - * Kevin Brown kbrown3@uccs.edu - * Nate Bohlmann nate@elfwerks.com - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of the copyright holders nor the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - \ingroup usbstick - \defgroup sicslowinterop 6LowPan Ethernet Interop - @{ -*/ - - -/** - \par Ethernet to 6LowPan Address Translation - - It should be obvious that since 802.15.4 addresses are 8 - bytes, and 802.3 addresses are 6 bytes, some form of - address translation is needed. These routines provide this - - \par 802.3 Address Formats - - 802.3 MAC addresses used here have this form: - - \verbatim - +----+----+----+----+----+----+----+----+ - + + + + + + TR + GL + MU + - +----+----+----+----+----+----+----+----+ - \endverbatim - - - It can be seen this is like a normal ethernet MAC address, - with GL being the Global/Local bit, and MU being the - Multicast/Unicast bit. - - The addition is the 'TR' bit, which if set indicates that - the address must be translated when going between 802.15.4 - and 802.3. - - \par Address Translation - - If the TRANSLATE (TR) bit is CLEAR, this means the 5th and - 4th LSBytes of the 802.15.4 address are fffe, aka the address - has the hexidecial form: - - xxxxxxfffexxxxxx - - \note - You should always aim to set the 802.15.4 addresses - of the devices on your network to ones that will - satisfy this requirement. Some examples are: - \note - 0x02 23 42 ff fe 73 92 28 - \note - 0x82 00 82 ff fe cd ee 22 - - \note - So the most significant octets MUST - have bit 0 CLEAR, bit 1 SET, and bit 2 CLEAR. The remaining - bits in this octet can be anything. - - If the TRANSLATE bit is SET, this means the address on the - 802.3 side does not directly convert to an 802.15.4 address. - To translate it, the remainder of the octet is used as an - index in a look-up table. This look-up table simply stores - the 4th, 5th, and 8th octet of the 802.15.4 address, and attaches - them to the remaining 5 bytes of the 802.3 address. - - In this way there can be 32 different 802.15.4 'prefixes', - requiring only 96 bytes of RAM in a storage table on the - 802.3 to 802.15.4 bridge. - - Mulitcast addresses on 802.3 are mapped to broadcast addresses on - 802.15.4 and vis-versa. Since IPv6 does not use 802.3 broadcast, - this code will drop all 802.3 broadcast packets. They are most - likely something unwanted, such as IPv4 packets that snuck in. - - \par Notes on how addresses are stored - - An 802.15.4 address will be reported for example as: - - 0x8877665544332211 - - Stored in the array as passed to these functions, it will be: - \verbatim - array[0] = 0x88; - array[1] = 0x77; - array[2] = 0x66; - etc. - \endverbatim - - An 802.3 address will be reported for example as: - 02:43:53:35:45:45 - - Stored in the array as passed to these functions, it will be: - \verbatim - array[0] = 0x02; - array[1] = 0x43; - array[2] = 0x53; - array[3] = 0x35 - etc. - \endverbatim -*/ - -#include "uip.h" -#include "uip_arp.h" /* For ethernet header structure */ -#include "net/ipv6/sicslowpan.h" -#include "sicslow_ethernet.h" - -#include -#include -#include - -//#define PRINTF printf -#define PRINTF(...) - -#define ETHBUF(x) ((struct uip_eth_hdr *)x) - -//For little endian, such as our friend mr. AVR -#ifndef LSB -#define LSB(u16) (((uint8_t *)&(u16))[0]) //!< Least significant byte of \a u16. -#define MSB(u16) (((uint8_t *)&(u16))[1]) //!< Most significant byte of \a u16. -#endif - -usbstick_mode_t usbstick_mode; - -uint8_t mac_createSicslowpanLongAddr(uint8_t * ethernet, uip_lladdr_t * lowpan); -uint8_t mac_createEthernetAddr(uint8_t * ethernet, uip_lladdr_t * lowpan); -/* uint8_t memcmp_reverse(uint8_t * a, uint8_t * b, uint8_t num); */ -/* void mac_ethhijack_nondata(const struct mac_driver *r); */ -/* void mac_ethhijack(const struct mac_driver *r); */ - -/* extern void (*sicslowmac_snifferhook)(const struct mac_driver *r); */ - - -//! Location of TRANSLATE (TR) bit in Ethernet address -#define TRANSLATE_BIT_MASK (1<<2) -//! Location of LOCAL (GL) bit in Ethernet address -#define LOCAL_BIT_MASK (1<<1) -//! Location of MULTICAST (MU) bit in Ethernet address -#define MULTICAST_BIT_MASK (1<<0) - -#define PREFIX_BUFFER_SIZE 32 - -uint8_t prefixCounter; -uint8_t prefixBuffer[PREFIX_BUFFER_SIZE][3]; - -/* 6lowpan max size + ethernet header size + 1 */ -uint8_t raw_buf[127+ UIP_LLH_LEN +1]; - - -/* void tcpip_input( void ) */ -/* { */ -/* mac_LowpanToEthernet(); */ -/* } */ - -/** - * \brief Perform any setup needed - */ -/* struct mac_driver * pmac; */ -void mac_ethernetSetup(void) -{ - usbstick_mode.sicslowpan = 1; - usbstick_mode.sendToRf = 1; - usbstick_mode.translate = 1; - usbstick_mode.raw = 1; - -/* sicslowinput = pinput; */ - - -/* pmac = sicslowmac_get_driver(); */ -/* pmac->set_receive_function(mac_ethhijack); */ -/* sicslowmac_snifferhook = mac_ethhijack_nondata; */ -} - - -/** - * \brief Take a packet received over the ethernet link, and send it - * out over 802.15.4 - */ -void mac_ethernetToLowpan(uint8_t * ethHeader) -{ - //Dest address - uip_lladdr_t destAddr; - uip_lladdr_t *destAddrPtr = NULL; - - PRINTF("Packet type: %x\n", ((struct uip_eth_hdr *) ethHeader)->type); - - //RUM doesn't support sending data - #if UIP_CONF_USE_RUM - return; - #endif - - //If not IPv6 we don't do anything - if (((struct uip_eth_hdr *) ethHeader)->type != UIP_HTONS(UIP_ETHTYPE_IPV6)) { - PRINTF("eth2low: Packet is not IPv6, dropping\n"); -/* rndis_stat.txbad++; */ - uipbuf_clear(); - return; - } - - // In sniffer mode we don't ever send anything - if (usbstick_mode.sendToRf == 0) { - uipbuf_clear(); - return; - } - - /* IPv6 uses 33-33-xx-xx-xx-xx prefix for multicast ND stuff */ - if ( (((struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0x33) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0x33) ) - { - PRINTF("eth2low: Ethernet multicast packet received\n"); - ;//Do Nothing - } else if ( (((struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[2] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[3] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[4] == 0xFF) && - (((struct uip_eth_hdr *) ethHeader)->dest.addr[5] == 0xFF) ) { - /* IPv6 does not use broadcast addresses, hence this should not happen */ - PRINTF("eth2low: Ethernet broadcast address received, should not happen?\n"); -/* rndis_stat.txbad++; */ - uipbuf_clear(); - return; - } else { - PRINTF("eth2low: Addressed packet received... "); - //Check this returns OK - if (mac_createSicslowpanLongAddr( &(((struct uip_eth_hdr *) ethHeader)->dest.addr[0]), &destAddr) == 0) { - PRINTF(" translation failed\n"); -/* rndis_stat.txbad++; */ - uipbuf_clear(); - return; - } - PRINTF(" translated OK\n"); - destAddrPtr = &destAddr; - } - - //Remove header from length before passing onward - if(uip_len > UIP_LLH_LEN) { - uip_len -= UIP_LLH_LEN; - - //Some IP packets have link layer in them, need to change them around! - if (usbstick_mode.translate) { - /* uint8_t transReturn = */ - mac_translateIPLinkLayer(ll_802154_type); - PRINTF("IPTranslation: returns %d\n", transReturn); - } - - if (usbstick_mode.sendToRf){ - tcpip_output(destAddrPtr); - /* rndis_stat.txok++; */ - } - } - - uipbuf_clear(); - -} - - -/** - * \brief Take a packet received over the 802.15.4 link, and send it - * out over ethernet, performing any translations needed. - */ -void mac_LowpanToEthernet(void) -{ -/* parsed_frame = sicslowmac_get_frame(); */ - - //Setup generic ethernet stuff - ETHBUF(uip_buf)->type = uip_htons(UIP_ETHTYPE_IPV6); - - //Check for broadcast message - if(packetbuf_holds_broadcast()) { -/* if( ( parsed_frame->fcf->destAddrMode == SHORTADDRMODE) && */ -/* ( parsed_frame->dest_addr->addr16 == 0xffff) ) { */ - ETHBUF(uip_buf)->dest.addr[0] = 0x33; - ETHBUF(uip_buf)->dest.addr[1] = 0x33; - ETHBUF(uip_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(uip_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(uip_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(uip_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(uip_buf)->dest.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); - } - - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(uip_buf)->src.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER)); - - //We only do address translation in network mode! - if (usbstick_mode.translate) { - //Some IP packets have link layer in them, need to change them around! - mac_translateIPLinkLayer(ll_8023_type); - } - - PRINTF("Low2Eth: Sending packet to ethernet\n"); - - uip_len += UIP_LLH_LEN; - -/* rndis_send(uip_buf, uip_len, 1); */ -/* rndis_stat.rxok++; */ -/* uipbuf_clear(); */ -} - -/** - * \brief Translate IP packet's possible link-layer addresses, passing - * the message to the appropriate higher level function for this - * packet (aka: ICMP) - * \param target The target we want to end up with - either ll_8023_type - * for ethernet, or ll_802154_type for 802.15.4 - * \return Returns how successful the translation was - * \retval 0 Addresses, if present, were translated. - * \retval <0 Negative return values indicate various errors, as defined - * by the higher level function. - */ -int8_t mac_translateIPLinkLayer(lltype_t target) -{ - -#if UIP_LLADDR_LEN == 8 - if (UIP_IP_BUF->proto == UIP_PROTO_ICMP6) { - PRINTF("eth2low: ICMP Message detected\n"); - return mac_translateIcmpLinkLayer(target); - } - return 0; -#else - return 1; -#endif - -} - -#include "net/ipv6/uip-icmp6.h" -#include "net/ipv6/uip-nd6.h" - -typedef struct { - uint8_t type; - uint8_t length; - uint8_t data[16]; -} icmp_opts_t; - -#define UIP_ICMP_OPTS(x) ((icmp_opts_t *)UIP_IP_PAYLOAD(x)) - -void slide(uint8_t * data, uint8_t length, int16_t slide); - -/** - * \brief Translate the link-layer (L2) addresses in an ICMP packet. - * This will just be NA/NS/RA/RS packets currently. - * \param target The target we want to end up with - either ll_8023_type - * for ethernet, or ll_802154_type for 802.15.4 - * \return Returns how successful the translation was - * \retval 0 Addresses, if present, were translated. - * \retval -1 ICMP message was unknown type, nothing done. - * \retval -2 ICMP Length does not make sense? - * \retval -3 Unknown 'target' type - */ -int8_t mac_translateIcmpLinkLayer(lltype_t target) -{ - uint16_t icmp_opt_offset = 0; - int16_t len = uipbuf_get_len_field(UIP_IP_BUF); - - uint16_t iplen; - - uint8_t i; - - int16_t sizechange; - - uint8_t llbuf[16]; - - //Figure out offset to start of options - switch(UIP_ICMP_BUF->type) { - case ICMP6_NS: - case ICMP6_NA: - icmp_opt_offset = 24; - break; - - case ICMP6_RS: - icmp_opt_offset = 8; - break; - - case ICMP6_RA: - icmp_opt_offset = 16; - break; - - case ICMP6_REDIRECT: - icmp_opt_offset = 40; - break; - - /** Things without link-layer */ - case ICMP6_DST_UNREACH: - case ICMP6_PACKET_TOO_BIG: - case ICMP6_TIME_EXCEEDED: - case ICMP6_PARAM_PROB: - case ICMP6_ECHO_REQUEST: - case ICMP6_ECHO_REPLY: - return 0; - break; - - default: - return -1; - } - - //Figure out length of options - len -= icmp_opt_offset; - - //Sanity check - if (len < 8) return -2; - - //While we have options to do... - while (len >= 8){ - - //If we have one of these, we have something useful! - if (((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_SLLAO) || - ((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_TLLAO) ) { - - /* Shrinking the buffer may thrash things, so we store the old - link-layer address */ - for(i = 0; i < (UIP_ICMP_OPTS(icmp_opt_offset)->length*8 - 2); i++) { - llbuf[i] = UIP_ICMP_OPTS(icmp_opt_offset)->data[i]; - } - - //Shrink/grow buffer as needed - if (target == ll_802154_type) { - //Current is 802.3, Hence current link-layer option is 6 extra bytes - sizechange = 8; - slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 6, len - 6, sizechange); - } else if (target == ll_8023_type) { - /* Current is 802.15.4, Hence current link-layer option is 14 extra - * bytes. - * (Actual LL is 8 bytes, but total option length is in multiples of - * 8 Bytes, hence 8 + 2 = 10. Closest is 16 bytes, then 16 bytes for - * total optional length - 2 bytes for type + length leaves 14 ) - */ - sizechange = -8; - slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 14, len - 14, sizechange); - } else { - return -3; //Uh-oh! - } - - //Translate addresses - if (target == ll_802154_type) { - mac_createSicslowpanLongAddr(llbuf, (uip_lladdr_t *)UIP_ICMP_OPTS(icmp_opt_offset)->data); - } else { - mac_createEthernetAddr(UIP_ICMP_OPTS(icmp_opt_offset)->data, (uip_lladdr_t *)llbuf); - } - - //Adjust the length - if (target == ll_802154_type) { - UIP_ICMP_OPTS(icmp_opt_offset)->length = 2; - } else { - UIP_ICMP_OPTS(icmp_opt_offset)->length = 1; - } - - //Adjust the IP header length, as well as uIP length - iplen = uipbuf_get_len_field(UIP_IP_BUF); - iplen += sizechange; - len += sizechange; - - uipbuf_set_len_field(UIP_IP_BUF, iplen); - - uip_len += sizechange; - - //We broke ICMP checksum, be sure to fix that - UIP_ICMP_BUF->icmpchksum = 0; - UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum(); - - //Finally set up next run in while loop - len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - } else { - - //Not an option we care about, ignore it - len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - - //This shouldn't happen! - if (UIP_ICMP_OPTS(icmp_opt_offset)->length == 0) { - PRINTF("Option in ND packet has length zero, error?\n"); - len = 0; - } - - icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length; - - } //If ICMP_OPT is one we care about - - } //while(len >= 8) - - return 0; - -} - - -/** - * \brief Create a 802.15.4 long address from a 802.3 address - * \param ethernet Pointer to ethernet address - * \param lowpan Pointer to 802.15.4 address - */ -uint8_t mac_createSicslowpanLongAddr(uint8_t * ethernet, uip_lladdr_t * lowpan) -{ - uint8_t index; - -#if UIP_LLADDR_LEN == 8 - //Special case - if the address is our address, we just copy over what we know to be - //our 802.15.4 address - -/* if (memcmp_reverse((uint8_t *)&rndis_ethernet_addr, ethernet, 6) == 0) { */ - if (memcmp((uint8_t *)&uip_lladdr.addr[2], ethernet, 6) == 0) { - memcpy((uint8_t *)lowpan, uip_lladdr.addr, 8); -/* byte_reverse((uint8_t *)lowpan, 8); */ - - return 1; - } - - //Check if translate bit is set, hence we have to look up the prefix - if (ethernet[0] & TRANSLATE_BIT_MASK) { - - //Get top bits - index = ethernet[0] >> 3; - - //Check this is plausible... - if (index >= prefixCounter) { - return 0; - } - - //Copy over prefix - lowpan->addr[0] = prefixBuffer[index][0]; - lowpan->addr[3] = prefixBuffer[index][1]; - lowpan->addr[4] = prefixBuffer[index][2]; - - //Bit is clear - //so we copy all six - } else { - lowpan->addr[0] = ethernet[0]; - lowpan->addr[3] = 0xff; - lowpan->addr[4] = 0xfe; - } - - //Copy over reamining five bytes - lowpan->addr[1] = ethernet[1]; - lowpan->addr[2] = ethernet[2]; - lowpan->addr[5] = ethernet[3]; - lowpan->addr[6] = ethernet[4]; - lowpan->addr[7] = ethernet[5]; - -#else - uint8_t i; - - for(i = 0; i < UIP_LLADDR_LEN; i++) { - lowpan->addr[i] = ethernet[i]; - } -#endif - - return 1; -} - - -/** - * \brief Create a 802.3 address from a 802.15.4 long address - * \param ethernet Pointer to ethernet address - * \param lowpan Pointer to 802.15.4 address - */ -uint8_t mac_createEthernetAddr(uint8_t * ethernet, uip_lladdr_t * lowpan) -{ - uint8_t index = 0; - uint8_t i; -/* uint8_t j, match; */ - -#if UIP_LLADDR_LEN == 8 - - //Special case - if the address is our address, we just copy over what we know to be - //our 802.3 address -/* if (memcmp_reverse(uip_lladdr.addr, (uint8_t *)lowpan, 8) == 0) { */ - if (memcmp(uip_lladdr.addr, (uint8_t *)lowpan, 8) == 0) { - memcpy(ethernet, &uip_lladdr.addr[2], 6); -/* byte_reverse(ethernet, 6); */ - - return 1; - } - - - //Check if we need to do anything: - if ((lowpan->addr[3] == 0xff) && (lowpan->addr[4] == 0xfe) && - ((lowpan->addr[0] & TRANSLATE_BIT_MASK) == 0) && - ((lowpan->addr[0] & MULTICAST_BIT_MASK) == 0) && - (lowpan->addr[0] & LOCAL_BIT_MASK)) { - - /** Nope: just copy over 6 bytes **/ - ethernet[0] = lowpan->addr[0]; - ethernet[1] = lowpan->addr[1]; - ethernet[2] = lowpan->addr[2]; - ethernet[3] = lowpan->addr[5]; - ethernet[4] = lowpan->addr[6]; - ethernet[5] = lowpan->addr[7]; - - - } else { - - /** Yes: need to store prefix **/ - for (i = 0; i < prefixCounter; i++) { - //Check the current prefix - if it fails, check next one - - - if ((lowpan->addr[0] == prefixBuffer[i][0]) && - (lowpan->addr[3] == prefixBuffer[i][1]) && - (lowpan->addr[4] == prefixBuffer[i][2])) { - break; - } - - } - - index = i; - - //Deal with overflow, iron-fist style - if (index >= PREFIX_BUFFER_SIZE) { - index = 0; - prefixCounter = PREFIX_BUFFER_SIZE; - } else { - //Are we making a new one? - if (index == prefixCounter) { - prefixCounter++; - } - } - - //Copy the prefix over, no matter if we have a new or old one - prefixBuffer[index][0] = lowpan->addr[0]; - prefixBuffer[index][1] = lowpan->addr[3]; - prefixBuffer[index][2] = lowpan->addr[4]; - - //Create ethernet MAC address now - ethernet[1] = lowpan->addr[1]; - ethernet[2] = lowpan->addr[2]; - ethernet[3] = lowpan->addr[5]; - ethernet[4] = lowpan->addr[6]; - ethernet[5] = lowpan->addr[7]; - - - ethernet[0] = TRANSLATE_BIT_MASK | LOCAL_BIT_MASK | (index << 3); - } - -#else - - //Create ethernet MAC address now - for(i = 0; i < UIP_LLADDR_LEN; i++) { - ethernet[i] = lowpan->addr[i]; - } -#endif - - return 1; -} - - -/** - * \brief Slide the pointed to memory up a certain amount, - * growing/shrinking a buffer - * \param data Pointer to start of data buffer - * \param length Length of the data buffer - * \param slide How many bytes to slide the buffer up in memory (if +) or - * down in memory (if -) - */ -void slide(uint8_t * data, uint8_t length, int16_t slide) -{ - //Sanity checks - if (!length) return; - if (!slide) return; - - uint8_t i = 0; - - while(length) { - length--; - - //If we are sliding up, we do from the top of the buffer down - if (slide > 0) { - *(data + length + slide) = *(data + length); - - //If we are sliding down, we do from the bottom of the buffer up - } else { - *(data + slide + i) = *(data + i); - } - - i++; - } -} - -/*--------------------------------------------------------------------*/ -/** \brief Process a received 6lowpan packet. Hijack function. - * \param r The MAC layer - * - * The 6lowpan packet is put in packetbuf by the MAC. This routine calls - * any other needed layers (either 6lowpan, or just raw ethernet dump) - */ -#if 0 -void mac_ethhijack(const struct mac_driver *r) -{ - if (usbstick_mode.raw) { - mac_802154raw(r); - } - - if (usbstick_mode.sicslowpan) { - -#if UIP_CONF_USE_RUM - if (parsed_frame->payload[4]) { /* RUM 6lowpan frame type */ -#endif - sicslowinput(r); -#if UIP_CONF_USE_RUM - } -#endif - - - } - -} -#endif - -#if 0 -void mac_ethhijack_nondata(const struct mac_driver *r) -{ - if (usbstick_mode.raw) - mac_802154raw(r); -} -#endif - -/*--------------------------------------------------------------------*/ -/*--------------------------------------------------------------------*/ -/** \brief Logs a sent 6lowpan frame - * - * This routine passes a frame - * directly to the ethernet layer without decompressing. - */ -#if 0 -void mac_logTXtoEthernet(frame_create_params_t *p,frame_result_t *frame_result) -{ - uint8_t sendlen; - - /* Make sure we are supposed to do this */ - if (usbstick_mode.raw == 0) return; - -/* Get the raw frame */ - memcpy(&raw_buf[UIP_LLH_LEN], frame_result->frame, frame_result->length); - sendlen = frame_result->length; - - //Setup generic ethernet stuff - ETHBUF(raw_buf)->type = uip_htons(UIP_ETHTYPE_802154); - - uint64_t tempaddr; - - - //Check for broadcast message - //if(linkaddr_cmp((const linkaddr_t *)destAddr, &linkaddr_null)) { - if( ( p->fcf.destAddrMode == SHORTADDRMODE) && - ( p->dest_addr.addr16 == 0xffff) ) { - ETHBUF(raw_buf)->dest.addr[0] = 0x33; - ETHBUF(raw_buf)->dest.addr[1] = 0x33; - ETHBUF(raw_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(raw_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(raw_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(raw_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - - tempaddr = p->dest_addr.addr64; - - byte_reverse((uint8_t *)&tempaddr, 8); - - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->dest.addr[0]), - (uip_lladdr_t *)&tempaddr); - - } - - tempaddr = p->src_addr.addr64; - - byte_reverse((uint8_t *)&tempaddr, 8); - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->src.addr[0]), - (uip_lladdr_t *)&tempaddr); - - PRINTF("Low2Eth: Sending 802.15.4 packet to ethernet\n"); - - sendlen += UIP_LLH_LEN; - - rndis_send(raw_buf, sendlen, 0); - rndis_stat.rxok++; - return; -} -#endif - -/*--------------------------------------------------------------------*/ -/** \brief Process a received 6lowpan packet. - * \param r The MAC layer - * - * The 6lowpan packet is put in packetbuf by the MAC. This routine passes - * it directly to the ethernet layer without decompressing. - */ -#if 0 -void mac_802154raw(const struct mac_driver *r) -{ - uint8_t sendlen; - - parsed_frame = sicslowmac_get_frame(); - - /* Get the raw frame */ - memcpy(&raw_buf[UIP_LLH_LEN], radio_frame_data(), radio_frame_length()); - sendlen = radio_frame_length(); - - //Setup generic ethernet stuff - ETHBUF(raw_buf)->type = uip_htons(UIP_ETHTYPE_802154); - - - //Check for broadcast message - //if(linkaddr_cmp((const linkaddr_t *)destAddr, &linkaddr_null)) { - if( ( parsed_frame->fcf->destAddrMode == SHORTADDRMODE) && - ( parsed_frame->dest_addr->addr16 == 0xffff) ) { - ETHBUF(raw_buf)->dest.addr[0] = 0x33; - ETHBUF(raw_buf)->dest.addr[1] = 0x33; - ETHBUF(raw_buf)->dest.addr[2] = UIP_IP_BUF->destipaddr.u8[12]; - ETHBUF(raw_buf)->dest.addr[3] = UIP_IP_BUF->destipaddr.u8[13]; - ETHBUF(raw_buf)->dest.addr[4] = UIP_IP_BUF->destipaddr.u8[14]; - ETHBUF(raw_buf)->dest.addr[5] = UIP_IP_BUF->destipaddr.u8[15]; - } else { - - //Otherwise we have a real address - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->dest.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER)); - - } - - mac_createEthernetAddr((uint8_t *) &(ETHBUF(raw_buf)->src.addr[0]), - (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER)); - - PRINTF("Low2Eth: Sending 802.15.4 packet to ethernet\n"); - - sendlen += UIP_LLH_LEN; - - rndis_send(raw_buf, sendlen, 1); - rndis_stat.rxok++; - - return; -} -#endif -/** @} */ -/** @} */ diff --git a/tools/sky/uip6-bridge/sicslow_ethernet.h b/tools/sky/uip6-bridge/sicslow_ethernet.h deleted file mode 100644 index 2e48c188e..000000000 --- a/tools/sky/uip6-bridge/sicslow_ethernet.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * \file sicslow_ethernet.c - * Routines to interface between Ethernet and 6LowPan - * - * \author - * Colin O'Flynn - * - * \addtogroup usbstick - */ - -/* Copyright (c) 2008 by: - - Colin O'Flynn coflynn@newae.com - Eric Gnoske egnoske@gmail.com - Blake Leverett bleverett@gmail.com - Mike Vidales mavida404@gmail.com - Kevin Brown kbrown3@uccs.edu - Nate Bohlmann nate@elfwerks.com - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - * Neither the name of the copyright holders nor the names of - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef SICSLOW_ETHERNET_H -#define SICSLOW_ETHERNET_H - -typedef enum { - ll_802154_type, - ll_8023_type -} lltype_t; - - -typedef struct { - uint8_t sicslowpan :1; - uint8_t sendToRf :1; - uint8_t translate :1; - uint8_t raw :1; -} usbstick_mode_t; - - -#define UIP_ETHTYPE_802154 0x809A - -extern usbstick_mode_t usbstick_mode; - - -int8_t mac_translateIcmpLinkLayer(lltype_t target); -int8_t mac_translateIPLinkLayer(lltype_t target); -void mac_LowpanToEthernet(void); -void mac_ethernetToLowpan(uint8_t * ethHeader); -void mac_ethernetSetup(void); -/* void mac_802154raw(const struct mac_driver *r); */ -/* void mac_logTXtoEthernet(frame_create_params_t *p,frame_result_t *frame_result); */ - -#endif diff --git a/tools/sky/uip6-bridge/uip6-bridge-tap.c b/tools/sky/uip6-bridge/uip6-bridge-tap.c deleted file mode 100644 index 3386c2039..000000000 --- a/tools/sky/uip6-bridge/uip6-bridge-tap.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2009, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * - */ - -/** - * \file - * A brief description of what this file is - * \author - * Niclas Finne - * Joakim Eriksson - */ - -#include "contiki.h" -#include "net/ipv6/uip.h" -#include "dev/slip.h" -#include "dev/leds.h" -#include "sicslow_ethernet.h" - -#include "net/packetbuf.h" - -#include -#include - -PROCESS(uip6_bridge, "IPv6/6lowpan TAP bridge"); -PROCESS(tcpip_process, "tcpip dummy"); -AUTOSTART_PROCESSES(&uip6_bridge); - -/*---------------------------------------------------------------------------*/ -static uint8_t (* outputfunc)(const uip_lladdr_t *a); -uint8_t -tcpip_output(const uip_lladdr_t *a) -{ - if(outputfunc != NULL) { - outputfunc(a); - leds_toggle(LEDS_GREEN); - } - return 0; -} -void -tcpip_ipv6_output(void) -{ -} -void -tcpip_set_outputfunc(uint8_t (*f)(const uip_lladdr_t *)) -{ - outputfunc = f; -} -PROCESS_THREAD(tcpip_process, ev, data) -{ - PROCESS_BEGIN(); - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/ -/* Packet from SICSLoWPAN */ -void -tcpip_input(void) -{ - if(uip_len > 0) { - mac_LowpanToEthernet(); - if(uip_len > 0) { - slip_write(uip_buf, uip_len); - leds_toggle(LEDS_RED); - uipbuf_clear(); - } - } -} -/*---------------------------------------------------------------------------*/ -/* Packet from SLIP */ -static void -slip_tcpip_input(void) -{ - /* TODO Should fix this in slip configuration */ - memmove(uip_buf, &uip_buf[UIP_LLH_LEN], uip_len); - mac_ethernetToLowpan(uip_buf); -} -/*---------------------------------------------------------------------------*/ -static void -slip_activity(void) -{ - leds_toggle(LEDS_BLUE); -} -/*---------------------------------------------------------------------------*/ -PROCESS_THREAD(uip6_bridge, ev, data) -{ - PROCESS_BEGIN(); - - printf("Setting up SLIP\n"); - - mac_ethernetSetup(); - - slip_arch_init(); - slip_set_input_callback(slip_activity); - slip_set_tcpip_input_callback(slip_tcpip_input); - process_start(&slip_process, NULL); - - PROCESS_END(); -} -/*---------------------------------------------------------------------------*/ -int -putchar(int c) -{ -#define SLIP_END 0300 - static char debug_frame = 0; - - if(!debug_frame) { /* Start of debug output */ - slip_arch_writeb(SLIP_END); - slip_arch_writeb('\r'); /* Type debug line == '\r' */ - debug_frame = 1; - } - - slip_arch_writeb((char)c); - - /* - * Line buffered output, a newline marks the end of debug output and - * implicitly flushes debug output. - */ - if(c == '\n') { - slip_arch_writeb(SLIP_END); - debug_frame = 0; - } - return c; -} -/*---------------------------------------------------------------------------*/ diff --git a/tools/wpcapslip/wpcap.c b/tools/wpcapslip/wpcap.c index 54fc4e873..7406d662f 100644 --- a/tools/wpcapslip/wpcap.c +++ b/tools/wpcapslip/wpcap.c @@ -451,7 +451,7 @@ arp_out(struct ethip_hdr *iphdr, int len) arphdr->protolen = 4; arphdr->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP); - /* uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];*/ + /* uip_appdata = &uip_buf[UIP_TCPIP_HLEN];*/ return sizeof(struct arp_hdr); } From 9b66b6931d7f64a4ac6eb75eb347f84d7263d43a Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 17:48:08 +0200 Subject: [PATCH 23/28] Removed UIP_IP_BUF_CHAR --- os/net/ipv6/uip-icmp6.c | 2 +- os/net/ipv6/uip.h | 5 ++--- os/net/ipv6/uip6.c | 12 ++++++------ os/net/routing/rpl-classic/rpl-ext-header.c | 10 +++++----- os/net/routing/rpl-lite/rpl-ext-header.c | 10 +++++----- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/os/net/ipv6/uip-icmp6.c b/os/net/ipv6/uip-icmp6.c index d9d55a8fb..18dcef89c 100644 --- a/os/net/ipv6/uip-icmp6.c +++ b/os/net/ipv6/uip-icmp6.c @@ -174,7 +174,7 @@ uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) { shift = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ICMP6_ERROR_LEN; uip_len += shift; uip_len = MIN(uip_len, UIP_LINK_MTU); - memmove(UIP_IP_BUF_CHAR + shift, (void *)UIP_IP_BUF, uip_len - shift); + memmove(uip_buf + shift, (void *)UIP_IP_BUF, uip_len - shift); UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 0; diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 2e83607bd..78b1fa305 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -79,9 +79,8 @@ /** * Direct access to IPv6 header */ -#define UIP_IP_BUF_CHAR ((unsigned char *)uip_buf) -#define UIP_IP_BUF ((struct uip_ip_hdr *)UIP_IP_BUF_CHAR) -#define UIP_IP_PAYLOAD(ext) ((unsigned char *)UIP_IP_BUF_CHAR + UIP_IPH_LEN + (ext)) +#define UIP_IP_BUF ((struct uip_ip_hdr *)uip_buf) +#define UIP_IP_PAYLOAD(ext) ((unsigned char *)uip_buf + UIP_IPH_LEN + (ext)) /** * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_ext_len) diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 19f786d3f..a6fa4494e 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -1126,7 +1126,7 @@ uip_process(uint8_t flag) /* Check sanity of extension headers, and compute the total extension header * length (uip_ext_len) as well as the final protocol (uip_last_proto) */ uip_last_proto = 0; - last_header = uipbuf_get_last_header(UIP_IP_BUF_CHAR, uip_len, &uip_last_proto); + last_header = uipbuf_get_last_header(uip_buf, uip_len, &uip_last_proto); if(last_header == NULL) { LOG_ERR("invalid extension header chain\n"); goto drop; @@ -1159,7 +1159,7 @@ uip_process(uint8_t flag) * the packet. */ - next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); if(next_header != NULL && protocol == UIP_PROTO_HBHO) { switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { case 0: @@ -1246,9 +1246,9 @@ uip_process(uint8_t flag) /* IPv6 extension header processing: loop until reaching upper-layer protocol */ uip_ext_bitmap = 0; - for(next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + for(next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); next_header != NULL && uip_is_proto_ext_hdr(protocol); - next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false)) { + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false)) { ext_ptr = (struct uip_ext_hdr *)next_header; switch(protocol) { @@ -1354,7 +1354,7 @@ uip_process(uint8_t flag) /* packet is reassembled. Restart the parsing of the reassembled pkt */ LOG_INFO("Processing reassembled packet\n"); uip_ext_bitmap = 0; - next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); break; #else /* UIP_CONF_IPV6_REASSEMBLY */ UIP_STAT(++uip_stat.ip.drop); @@ -1393,7 +1393,7 @@ uip_process(uint8_t flag) * RFC 2460 send error message parameterr problem, code unrecognized * next header, pointing to the next header field */ - uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(next_header - UIP_IP_BUF_CHAR)); + uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, (uint32_t)(next_header - uip_buf)); UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.protoerr); LOG_ERR("unrecognized header\n"); diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 91a87079e..2a77ddbc7 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -177,7 +177,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) uip_sr_node_t *root_node; /* Look for routing ext header */ - rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); dag = rpl_get_dag(&UIP_IP_BUF->destipaddr); root_node = uip_sr_get_node(dag, &dag->dag_id); @@ -209,7 +209,7 @@ rpl_ext_header_srh_update(void) struct uip_rpl_srh_hdr *srh_header; /* Look for routing ext header */ - rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); if(rh_header != NULL && rh_header->routing_type == RPL_RH_TYPE_SRH) { /* SRH found, now look for next hop */ @@ -521,7 +521,7 @@ rpl_ext_header_remove(void) struct uip_ext_hdr *ext_ptr; struct uip_ext_hdr_opt *opt_ptr; - next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); ext_ptr = (struct uip_ext_hdr *)next_header; prev_proto_ptr = &UIP_IP_BUF->proto; while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { @@ -533,12 +533,12 @@ rpl_ext_header_remove(void) uipbuf_add_ext_hdr(-ext_len); /* Update length field and rest of packer to the "left" */ uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); - memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); + memmove(next_header, next_header + ext_len, uip_len - (next_header - uip_buf)); /* Update loop variables */ protocol = *prev_proto_ptr; } else { /* move to the ext hdr */ - next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false); + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false); ext_ptr = (struct uip_ext_hdr *)next_header; prev_proto_ptr = &ext_ptr->next; } diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 6e6eb6c34..9811771a0 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -62,7 +62,7 @@ rpl_ext_header_srh_get_next_hop(uip_ipaddr_t *ipaddr) uip_sr_node_t *root_node; /* Look for routing ext header */ - rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); if(!rpl_is_addr_in_our_dag(&UIP_IP_BUF->destipaddr)) { return 0; @@ -100,7 +100,7 @@ rpl_ext_header_srh_update(void) uip_ipaddr_t current_dest_addr; /* Look for routing ext header */ - rh_header = (struct uip_routing_hdr *)uipbuf_search_header(UIP_IP_BUF_CHAR, uip_len, UIP_PROTO_ROUTING); + rh_header = (struct uip_routing_hdr *)uipbuf_search_header(uip_buf, uip_len, UIP_PROTO_ROUTING); if(rh_header == NULL || rh_header->routing_type != RPL_RH_TYPE_SRH) { LOG_INFO("SRH not found\n"); @@ -459,7 +459,7 @@ rpl_ext_header_remove(void) struct uip_ext_hdr *ext_ptr; struct uip_ext_hdr_opt *opt_ptr; - next_header = uipbuf_get_next_header(UIP_IP_BUF_CHAR, uip_len, &protocol, true); + next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); ext_ptr = (struct uip_ext_hdr *)next_header; prev_proto_ptr = &UIP_IP_BUF->proto; while(next_header != NULL && uip_is_proto_ext_hdr(protocol)) { @@ -471,12 +471,12 @@ rpl_ext_header_remove(void) uipbuf_add_ext_hdr(-ext_len); /* Update length field and move rest of packet to the "left" */ uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); - memmove(next_header, next_header + ext_len, uip_len - (next_header - UIP_IP_BUF_CHAR)); + memmove(next_header, next_header + ext_len, uip_len - (next_header - uip_buf)); /* Update loop variables */ protocol = *prev_proto_ptr; } else { /* move to the ext hdr */ - next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - UIP_IP_BUF_CHAR), &protocol, false); + next_header = uipbuf_get_next_header(next_header, uip_len - (next_header - uip_buf), &protocol, false); ext_ptr = (struct uip_ext_hdr *)next_header; prev_proto_ptr = &ext_ptr->next; } From 8456c544d8a4cf9d1cf78cc7a8757dd35eed314c Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 17:54:34 +0200 Subject: [PATCH 24/28] Simplify uip.h definitions --- os/net/ipv6/multicast/esmrf.c | 4 +-- os/net/ipv6/multicast/roll-tm.c | 4 +-- os/net/ipv6/sicslowpan.c | 2 +- os/net/ipv6/uip-nd6.c | 8 +++--- os/net/ipv6/uip.h | 13 +--------- os/net/ipv6/uip6.c | 28 ++++++++++----------- os/net/ipv6/uipopt.h | 8 +++--- os/net/routing/rpl-classic/rpl-ext-header.c | 6 ++--- os/net/routing/rpl-lite/rpl-ext-header.c | 6 ++--- os/services/ip64/ip64-arp.c | 2 +- tools/wpcapslip/wpcap.c | 2 +- 11 files changed, 36 insertions(+), 47 deletions(-) diff --git a/os/net/ipv6/multicast/esmrf.c b/os/net/ipv6/multicast/esmrf.c index c10ec4d4f..169064b25 100644 --- a/os/net/ipv6/multicast/esmrf.c +++ b/os/net/ipv6/multicast/esmrf.c @@ -201,11 +201,11 @@ icmp_input() VERBOSE_PRINTF("ESMRF: ICMPv6 In, parse from %p to %p\n", UIP_ICMP_PAYLOAD, (uint8_t *)UIP_ICMP_PAYLOAD + uip_len - - uip_l2_l3_icmp_hdr_len); + uip_l3_icmp_hdr_len); locmobptr = (struct multicast_on_behalf *) UIP_ICMP_PAYLOAD; - loclen = uip_len - (uip_l2_l3_icmp_hdr_len + UIP_ICMP_MOB); + loclen = uip_len - (uip_l3_icmp_hdr_len + UIP_ICMP_MOB); uip_ipaddr_copy(&src_ip, &UIP_IP_BUF->srcipaddr); uip_ipaddr_copy(&des_ip, &UIP_IP_BUF->destipaddr); diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 0f865c438..87121fc6d 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -1146,10 +1146,10 @@ icmp_input() VERBOSE_PRINTF("ROLL TM: ICMPv6 In, parse from %p to %p\n", UIP_ICMP_PAYLOAD, (uint8_t *)UIP_ICMP_PAYLOAD + uip_len - - uip_l2_l3_icmp_hdr_len); + uip_l3_icmp_hdr_len); while(locslhptr < (struct sequence_list_header *)((uint8_t *)UIP_ICMP_PAYLOAD + - uip_len - uip_l2_l3_icmp_hdr_len)) { + uip_len - uip_l3_icmp_hdr_len)) { VERBOSE_PRINTF("ROLL TM: ICMPv6 In, seq hdr @ %p\n", locslhptr); if((locslhptr->flags & SEQUENCE_LIST_RES) != 0) { diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index fa183b686..44851e93d 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -121,7 +121,7 @@ #define SICSLOWPAN_UDP_BUF(buf) ((struct uip_udp_hdr *)&buf[UIP_IPH_LEN]) #define SICSLOWPAN_IPPAYLOAD_BUF(buf) (&buf[UIP_IPH_LEN]) -#define UIP_IPPAYLOAD_BUF_POS(pos) (&uip_buf[UIP_LLIPH_LEN + (pos)]) +#define UIP_IPPAYLOAD_BUF_POS(pos) (&uip_buf[UIP_IPH_LEN + (pos)]) #define UIP_UDP_BUF_POS(pos) ((struct uip_udp_hdr *)UIP_IPPAYLOAD_BUF_POS(pos)) /** @} */ diff --git a/os/net/ipv6/uip-nd6.c b/os/net/ipv6/uip-nd6.c index eceeec961..f6efc83de 100644 --- a/os/net/ipv6/uip-nd6.c +++ b/os/net/ipv6/uip-nd6.c @@ -201,7 +201,7 @@ ns_input(void) #endif /* UIP_CONF_IPV6_CHECKS */ switch (ND6_OPT_HDR_BUF(nd6_opt_offset)->type) { case UIP_ND6_OPT_SLLAO: - nd6_opt_llao = &uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset]; + nd6_opt_llao = &uip_buf[uip_l3_icmp_hdr_len + nd6_opt_offset]; #if UIP_CONF_IPV6_CHECKS /* There must be NO option in a DAD NS */ if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) { @@ -328,7 +328,7 @@ create_na: UIP_ND6_NA_BUF->flagsreserved = flags; memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &addr->ipaddr, sizeof(uip_ipaddr_t)); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NA_LEN], UIP_ND6_OPT_TLLAO); UIP_ICMP_BUF->icmpchksum = 0; @@ -391,7 +391,7 @@ uip_nd6_ns_output(uip_ipaddr_t * src, uip_ipaddr_t * dest, uip_ipaddr_t * tgt) } uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_NS_LEN], UIP_ND6_OPT_SLLAO); uip_len = @@ -823,7 +823,7 @@ uip_nd6_rs_output(void) uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN; uipbuf_set_len_field(UIP_IP_BUF, UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN); - create_llao(&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN], + create_llao(&uip_buf[uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN], UIP_ND6_OPT_SLLAO); } diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 78b1fa305..4e915fe90 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -62,18 +62,7 @@ #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP + UDP header */ #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP + TCP header */ -#define UIP_TCPIP_HLEN UIP_IPTCPH_LEN -#define UIP_IPICMPH_LEN (UIP_IPH_LEN + UIP_ICMPH_LEN) /* Size of ICMP + IP header */ -#define UIP_LLIPH_LEN (UIP_IPH_LEN) /* Size of L2 + IP header */ -/** - * The sums below are quite used in ND. When used for uip_buf, we - * include link layer length when used for uip_len, we do not, hence - * we need values with and without LLH_LEN we do not use capital - * letters as these values are variable - */ -#define uip_l2_l3_hdr_len (UIP_IPH_LEN + uip_ext_len) -#define uip_l2_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN) /** @@ -1779,7 +1768,7 @@ struct uip_udp_hdr { * * \hideinitializer */ -#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_TCPIP_HLEN) +#define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_IPTCPH_LEN) #define UIP_PROTO_ICMP 1 #define UIP_PROTO_TCP 6 diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index a6fa4494e..6769561fc 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -1728,7 +1728,7 @@ uip_process(uint8_t flag) /* Parse the TCP MSS option, if present. */ if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) { for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) { - opt = uip_buf[UIP_TCPIP_HLEN + c]; + opt = uip_buf[UIP_IPTCPH_LEN + c]; if(opt == TCP_OPT_END) { /* End of options. */ break; @@ -1736,9 +1736,9 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_IPTCPH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = ((uint16_t)uip_buf[UIP_TCPIP_HLEN + 2 + c] << 8) | + tmp16 = ((uint16_t)uip_buf[UIP_IPTCPH_LEN + 2 + c] << 8) | (uint16_t)uip_buf[UIP_IPTCPH_LEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1748,12 +1748,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + 1 + c] == 0) { + if(uip_buf[UIP_IPTCPH_LEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + 1 + c]; + c += uip_buf[UIP_IPTCPH_LEN + 1 + c]; } } } @@ -1923,10 +1923,10 @@ uip_process(uint8_t flag) ++c; /* NOP option. */ } else if(opt == TCP_OPT_MSS && - uip_buf[UIP_TCPIP_HLEN + 1 + c] == TCP_OPT_MSS_LEN) { + uip_buf[UIP_IPTCPH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { /* An MSS option with the right option length. */ - tmp16 = (uip_buf[UIP_TCPIP_HLEN + 2 + c] << 8) | - uip_buf[UIP_TCPIP_HLEN + 3 + c]; + tmp16 = (uip_buf[UIP_IPTCPH_LEN + 2 + c] << 8) | + uip_buf[UIP_IPTCPH_LEN + 3 + c]; uip_connr->initialmss = uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; @@ -1935,12 +1935,12 @@ uip_process(uint8_t flag) } else { /* All other options have a length field, so that we easily can skip past them. */ - if(uip_buf[UIP_TCPIP_HLEN + 1 + c] == 0) { + if(uip_buf[UIP_IPTCPH_LEN + 1 + c] == 0) { /* If the length field is zero, the options are malformed and we don't process them further. */ break; } - c += uip_buf[UIP_TCPIP_HLEN + 1 + c]; + c += uip_buf[UIP_IPTCPH_LEN + 1 + c]; } } } @@ -2123,7 +2123,7 @@ uip_process(uint8_t flag) packet had new data in it, we must send out a packet. */ if(uip_slen > 0 && uip_connr->len > 0) { /* Add the length of the IP and TCP headers. */ - uip_len = uip_connr->len + UIP_TCPIP_HLEN; + uip_len = uip_connr->len + UIP_IPTCPH_LEN; /* We always set the ACK flag in response packets. */ UIP_TCP_BUF->flags = TCP_ACK | TCP_PSH; /* Send the packet. */ @@ -2132,7 +2132,7 @@ uip_process(uint8_t flag) /* If there is no data to send, just send out a pure ACK if there is newdata. */ if(uip_flags & UIP_NEWDATA) { - uip_len = UIP_TCPIP_HLEN; + uip_len = UIP_IPTCPH_LEN; UIP_TCP_BUF->flags = TCP_ACK; goto tcp_send_noopts; } @@ -2307,10 +2307,10 @@ uip_send(const void *data, int len) int copylen; if(uip_sappdata != NULL) { - copylen = MIN(len, UIP_BUFSIZE - UIP_TCPIP_HLEN - + copylen = MIN(len, UIP_BUFSIZE - UIP_IPTCPH_LEN - (int)((char *)uip_sappdata - (char *)UIP_TCP_PAYLOAD)); } else { - copylen = MIN(len, UIP_BUFSIZE - UIP_TCPIP_HLEN); + copylen = MIN(len, UIP_BUFSIZE - UIP_IPTCPH_LEN); } if(copylen > 0) { uip_slen = copylen; diff --git a/os/net/ipv6/uipopt.h b/os/net/ipv6/uipopt.h index bd2dcc502..a75c59ea0 100644 --- a/os/net/ipv6/uipopt.h +++ b/os/net/ipv6/uipopt.h @@ -414,15 +414,15 @@ void uip_log(char *msg); * The TCP maximum segment size. * * This is should not be to set to more than - * UIP_BUFSIZE - UIP_TCPIP_HLEN. + * UIP_BUFSIZE - UIP_IPTCPH_LEN. */ #ifdef UIP_CONF_TCP_MSS -#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_TCPIP_HLEN) +#if UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_IPTCPH_LEN) #error UIP_CONF_TCP_MSS is too large for the current UIP_BUFSIZE -#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_TCPIP_HLEN) */ +#endif /* UIP_CONF_TCP_MSS > (UIP_BUFSIZE - UIP_IPTCPH_LEN) */ #define UIP_TCP_MSS (UIP_CONF_TCP_MSS) #else /* UIP_CONF_TCP_MSS */ -#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_TCPIP_HLEN) +#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_IPTCPH_LEN) #endif /* UIP_CONF_TCP_MSS */ /** diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 2a77ddbc7..94367483a 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -367,9 +367,9 @@ insert_srh_header(void) } /* Move existing ext headers and payload ext_len further */ - memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, - uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); - memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); + memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len, + uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN); + memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); /* Insert source routing header (as first ext header) */ UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 9811771a0..e5adad703 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -256,9 +256,9 @@ insert_srh_header(void) } /* Move existing ext headers and payload ext_len further */ - memmove(uip_buf + uip_l2_l3_hdr_len + ext_len, - uip_buf + uip_l2_l3_hdr_len, uip_len - UIP_IPH_LEN); - memset(uip_buf + uip_l2_l3_hdr_len, 0, ext_len); + memmove(uip_buf + UIP_IPH_LEN + uip_ext_len + ext_len, + uip_buf + UIP_IPH_LEN + uip_ext_len, uip_len - UIP_IPH_LEN); + memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); /* Insert source routing header (as first ext header) */ UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; diff --git a/os/services/ip64/ip64-arp.c b/os/services/ip64/ip64-arp.c index daf27d959..6fd996920 100644 --- a/os/services/ip64/ip64-arp.c +++ b/os/services/ip64/ip64-arp.c @@ -409,7 +409,7 @@ ip64_arp_create_arp_request(uint8_t *llhdr, const uint8_t *nlhdr) arp_hdr->protolen = 4; arp_hdr->ethhdr.type = UIP_HTONS(IP64_ETH_TYPE_ARP); - uip_appdata = &uip_buf[UIP_TCPIP_HLEN]; + uip_appdata = &uip_buf[UIP_IPTCPH_LEN]; return sizeof(struct arp_hdr); } diff --git a/tools/wpcapslip/wpcap.c b/tools/wpcapslip/wpcap.c index 7406d662f..0d667d617 100644 --- a/tools/wpcapslip/wpcap.c +++ b/tools/wpcapslip/wpcap.c @@ -451,7 +451,7 @@ arp_out(struct ethip_hdr *iphdr, int len) arphdr->protolen = 4; arphdr->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP); - /* uip_appdata = &uip_buf[UIP_TCPIP_HLEN];*/ + /* uip_appdata = &uip_buf[UIP_IPTCPH_LEN];*/ return sizeof(struct arp_hdr); } From 6c11da58d84cac02e9380e316edfebc02c23460e Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 18:29:37 +0200 Subject: [PATCH 25/28] Rework rpl-ext-header.c --- os/net/ipv6/uip6.c | 2 +- os/net/routing/nullrouting/nullrouting.c | 2 +- os/net/routing/routing.h | 5 +- os/net/routing/rpl-classic/rpl-ext-header.c | 76 ++++++++++----------- os/net/routing/rpl-classic/rpl.h | 2 +- os/net/routing/rpl-lite/rpl-ext-header.c | 64 ++++++++--------- os/net/routing/rpl-lite/rpl-ext-header.h | 5 +- 7 files changed, 77 insertions(+), 79 deletions(-) diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 6769561fc..0ff2e7fc5 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -849,7 +849,7 @@ ext_hdr_options_process(int ext_offset) * present) is processed. */ LOG_DBG("Processing RPL option\n"); - if(!NETSTACK_ROUTING.ext_header_hbh_update(ext_offset, opt_offset)) { + if(!NETSTACK_ROUTING.ext_header_hbh_update(UIP_IP_PAYLOAD(ext_offset), opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } diff --git a/os/net/routing/nullrouting/nullrouting.c b/os/net/routing/nullrouting/nullrouting.c index 3443dbb13..318cd1baa 100644 --- a/os/net/routing/nullrouting/nullrouting.c +++ b/os/net/routing/nullrouting/nullrouting.c @@ -118,7 +118,7 @@ ext_header_update(void) } /*---------------------------------------------------------------------------*/ static int -ext_header_hbh_update(int ext_offset, int opt_offset) +ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { return 1; } diff --git a/os/net/routing/routing.h b/os/net/routing/routing.h index be1fe0173..4508f0c4e 100644 --- a/os/net/routing/routing.h +++ b/os/net/routing/routing.h @@ -131,14 +131,13 @@ struct routing_driver { * Process and update the routing protocol hob-by-hop * extention headers of the current uIP packet. * - * \param ext_offset The offset within the uIP packet where - * extension headers start + * \param ext_buf A pointer to the ext header buffer * \param opt_offset The offset within the extension header where * the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ - int (* ext_header_hbh_update)(int ext_offset, int opt_offset); + int (* ext_header_hbh_update)(uint8_t *ext_buf, int opt_offset); /** * Process and update SRH in-place, * i.e. internal address swapping as per RFC6554 diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 94367483a..7212bd6ba 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -62,34 +62,33 @@ /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int ext_offset, int opt_offset) +rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { rpl_instance_t *instance; int down; uint16_t sender_rank; uint8_t sender_closer; uip_ds6_route_t *route; - rpl_parent_t *sender = NULL; + rpl_parent_t *sender; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf; + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset); - if(UIP_HBHO_BUF(ext_offset)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type != UIP_EXT_HDR_OPT_RPL - || UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("Hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF(ext_offset)->len, - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type, - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len); + hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); + instance = rpl_get_instance(rpl_opt->instance); if(instance == NULL) { - LOG_ERR("Unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); + LOG_ERR("Unknown instance: %u\n", rpl_opt->instance); return 0; } - if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("Forward error!\n"); /* We should try to repair it by removing the neighbor that caused the packet to be forwareded in the first place. We drop any @@ -113,14 +112,14 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) return 0; } down = 0; - if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_DOWN) { + if(rpl_opt->flags & RPL_HDR_OPT_DOWN) { down = 1; } - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->senderrank); + sender_rank = UIP_HTONS(rpl_opt->senderrank); sender = nbr_table_get_from_lladdr(rpl_parents, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - if(sender != NULL && (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR)) { + if(sender != NULL && (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR)) { /* A rank error was signalled, attempt to repair it by updating * the sender's rank from ext header */ sender->rank = sender_rank; @@ -150,7 +149,7 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) instance->unicast_dio_target = sender; rpl_schedule_unicast_dio_immediately(instance); } - if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) { RPL_STAT(rpl_stats.loop_errors++); LOG_ERR(" Rank error signalled in RPL option!\n"); /* Packet must be dropped and dio trickle timer reset, see RFC6550 - 11.2.2.2 */ @@ -159,7 +158,7 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) } LOG_WARN("Single error tolerated\n"); RPL_STAT(rpl_stats.loop_warnings++); - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; + rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR; return 1; } @@ -414,18 +413,18 @@ update_hbh_header(void) { rpl_instance_t *instance; rpl_parent_t *parent; - int opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(0) + 2); - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF(0)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { - LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n", - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len); + LOG_ERR("Hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len); return 0; /* Drop */ } - instance = rpl_get_instance(UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance); + instance = rpl_get_instance(rpl_opt->instance); if(instance == NULL || !instance->used || !instance->current_dag->joined) { LOG_ERR("Unable to add/update hop-by-hop extension header: incorrect instance\n"); return 0; /* Drop */ @@ -433,17 +432,17 @@ update_hbh_header(void) LOG_INFO("Updating RPL option\n"); /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = instance->instance_id; + rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank); + rpl_opt->instance = instance->instance_id; if(RPL_IS_STORING(instance)) { /* In non-storing mode, downwards traffic does not have the HBH option */ /* Check the direction of the down flag, as per Section 11.2.2.3, which states that if a packet is going down it should in general not go back up again. If this happens, a RPL_HDR_OPT_FWD_ERR should be flagged. */ - if((UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags & RPL_HDR_OPT_DOWN)) { + if((rpl_opt->flags & RPL_HDR_OPT_DOWN)) { if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags |= RPL_HDR_OPT_FWD_ERR; + rpl_opt->flags |= RPL_HDR_OPT_FWD_ERR; LOG_WARN("RPL forwarding error\n"); /* We should send back the packet to the originating parent, but it is not feasible yet, so we send a No-Path DAO instead */ @@ -462,11 +461,11 @@ update_hbh_header(void) if(uip_ds6_route_lookup(&UIP_IP_BUF->destipaddr) == NULL) { /* No route was found, so this packet will go towards the RPL root. If so, we should not set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags &= ~RPL_HDR_OPT_DOWN; + rpl_opt->flags &= ~RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going up\n"); } else { /* A DAO route was found so we set the down flag. */ - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags |= RPL_HDR_OPT_DOWN; + rpl_opt->flags |= RPL_HDR_OPT_DOWN; LOG_DBG("RPL option going down\n"); } } @@ -479,7 +478,8 @@ update_hbh_header(void) static int insert_hbh_header(const rpl_instance_t *instance) { - int opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); /* Insert hop-by-hop header */ LOG_DBG("Creating hop-by-hop option\n"); @@ -493,16 +493,16 @@ insert_hbh_header(const rpl_instance_t *instance) memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); /* Insert HBH header (as first ext header) */ - UIP_HBHO_BUF(0)->next = UIP_IP_BUF->proto; + hbh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF(0)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(instance->current_dag->rank); - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = instance->instance_id; + hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL; + rpl_opt->opt_len = RPL_HDR_OPT_LEN; + rpl_opt->flags = 0; + rpl_opt->senderrank = UIP_HTONS(instance->current_dag->rank); + rpl_opt->instance = instance->instance_id; uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); diff --git a/os/net/routing/rpl-classic/rpl.h b/os/net/routing/rpl-classic/rpl.h index 170c97e58..f0c17cac9 100644 --- a/os/net/routing/rpl-classic/rpl.h +++ b/os/net/routing/rpl-classic/rpl.h @@ -275,7 +275,7 @@ rpl_dag_t *rpl_get_dag(const uip_ipaddr_t *addr); rpl_dag_t *rpl_get_any_dag(void); rpl_instance_t *rpl_get_instance(uint8_t instance_id); int rpl_ext_header_update(void); -int rpl_ext_header_hbh_update(int, int); +int rpl_ext_header_hbh_update(uint8_t *, int); void rpl_insert_header(void); void rpl_ext_header_remove(void); const struct link_stats *rpl_get_parent_link_stats(rpl_parent_t *p); diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index e5adad703..8a031c099 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -299,7 +299,7 @@ insert_srh_header(void) } /*---------------------------------------------------------------------------*/ int -rpl_ext_header_hbh_update(int ext_offset, int opt_offset) +rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset) { int down; int rank_error_signaled; @@ -307,32 +307,31 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) uint16_t sender_rank; uint8_t sender_closer; rpl_nbr_t *sender; - uint8_t opt_type = UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_type; - uint8_t opt_len = UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->opt_len; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)ext_buf; + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(ext_buf + opt_offset); - if(UIP_HBHO_BUF(ext_offset)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || opt_type != UIP_EXT_HDR_OPT_RPL - || opt_len != RPL_HDR_OPT_LEN) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_type != UIP_EXT_HDR_OPT_RPL + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { LOG_ERR("hop-by-hop extension header has wrong size or type (%u %u %u)\n", - UIP_HBHO_BUF(ext_offset)->len, opt_type, opt_len); + hbh_hdr->len, rpl_opt->opt_type, rpl_opt->opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance) { - LOG_ERR("unknown instance: %u\n", - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->instance); + if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) { + LOG_ERR("unknown instance: %u\n", rpl_opt->instance); return 0; /* Drop */ } - if(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_FWD_ERR) { + if(rpl_opt->flags & RPL_HDR_OPT_FWD_ERR) { LOG_ERR("forward error!\n"); return 0; /* Drop */ } - down = (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; - sender_rank = UIP_HTONS(UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->senderrank); + down = (rpl_opt->flags & RPL_HDR_OPT_DOWN) ? 1 : 0; + sender_rank = UIP_HTONS(rpl_opt->senderrank); sender = nbr_table_get_from_lladdr(rpl_neighbors, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - rank_error_signaled = (UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; + rank_error_signaled = (rpl_opt->flags & RPL_HDR_OPT_RANK_ERR) ? 1 : 0; sender_closer = sender_rank < curr_instance.dag.rank; loop_detected = (down && !sender_closer) || (!down && sender_closer); @@ -346,7 +345,7 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) if(loop_detected) { /* Set forward error flag */ - UIP_EXT_HDR_OPT_RPL_BUF(ext_offset, opt_offset)->flags |= RPL_HDR_OPT_RANK_ERR; + rpl_opt->flags |= RPL_HDR_OPT_RANK_ERR; } return rpl_process_hbh(sender, sender_rank, loop_detected, rank_error_signaled); @@ -358,25 +357,25 @@ rpl_ext_header_hbh_update(int ext_offset, int opt_offset) static int update_hbh_header(void) { - int opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); - if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type == UIP_EXT_HDR_OPT_RPL) { - if(UIP_HBHO_BUF(0)->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) - || UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len != RPL_HDR_OPT_LEN) { + if(UIP_IP_BUF->proto == UIP_PROTO_HBHO && rpl_opt->opt_type == UIP_EXT_HDR_OPT_RPL) { + if(hbh_hdr->len != ((RPL_HOP_BY_HOP_LEN - 8) / 8) + || rpl_opt->opt_len != RPL_HDR_OPT_LEN) { - LOG_ERR("hop-by-hop extension header has wrong size (%u)\n", - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len); + LOG_ERR("hop-by-hop extension header has wrong size (%u)\n", rpl_opt->opt_len); return 0; /* Drop */ } - if(!curr_instance.used || curr_instance.instance_id != UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance) { + if(!curr_instance.used || curr_instance.instance_id != rpl_opt->instance) { LOG_ERR("unable to add/update hop-by-hop extension header: incorrect instance\n"); return 0; /* Drop */ } /* Update sender rank and instance, will update flags next */ - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = curr_instance.instance_id; + rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank); + rpl_opt->instance = curr_instance.instance_id; } return 1; @@ -389,7 +388,8 @@ update_hbh_header(void) static int insert_hbh_header(void) { - int opt_offset = 2; + struct uip_hbho_hdr *hbh_hdr = (struct uip_hbho_hdr *)UIP_IP_PAYLOAD(0); + struct uip_ext_hdr_opt_rpl *rpl_opt = (struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(2)); /* Insert hop-by-hop header */ LOG_INFO("creating hop-by-hop option\n"); @@ -403,16 +403,16 @@ insert_hbh_header(void) memset(UIP_IP_PAYLOAD(0), 0, RPL_HOP_BY_HOP_LEN); /* Insert HBH header (as first ext header) */ - UIP_HBHO_BUF(0)->next = UIP_IP_BUF->proto; + hbh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_HBHO; /* Initialize HBH option */ - UIP_HBHO_BUF(0)->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_type = UIP_EXT_HDR_OPT_RPL; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->opt_len = RPL_HDR_OPT_LEN; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->flags = 0; - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->senderrank = UIP_HTONS(curr_instance.dag.rank); - UIP_EXT_HDR_OPT_RPL_BUF(0, opt_offset)->instance = curr_instance.instance_id; + hbh_hdr->len = (RPL_HOP_BY_HOP_LEN - 8) / 8; + rpl_opt->opt_type = UIP_EXT_HDR_OPT_RPL; + rpl_opt->opt_len = RPL_HDR_OPT_LEN; + rpl_opt->flags = 0; + rpl_opt->senderrank = UIP_HTONS(curr_instance.dag.rank); + rpl_opt->instance = curr_instance.instance_id; uipbuf_add_ext_hdr(RPL_HOP_BY_HOP_LEN); uipbuf_set_len_field(UIP_IP_BUF, uip_len - UIP_IPH_LEN); diff --git a/os/net/routing/rpl-lite/rpl-ext-header.h b/os/net/routing/rpl-lite/rpl-ext-header.h index 7576c6a57..c2d0db5bc 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.h +++ b/os/net/routing/rpl-lite/rpl-ext-header.h @@ -63,14 +63,13 @@ int rpl_ext_header_srh_update(void); * Process and update the RPL hop-by-hop extension headers of * the current uIP packet. * -* \param ext_offset The offset within the uIP packet where -* extension headers start +* \param ext_buf A pointer to the ext header buffer * \param opt_offset The offset within the extension header where * the option starts * \return 1 in case the packet is valid and to be processed further, * 0 in case the packet must be dropped. */ -int rpl_ext_header_hbh_update(int ext_offset, int opt_offset); +int rpl_ext_header_hbh_update(uint8_t *ext_buf, int opt_offset); /** * Adds/updates all RPL extension headers to current uIP packet. From 6c583efa3728b37749f3efebba22f89a9a9bf145 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 18:53:34 +0200 Subject: [PATCH 26/28] Rework ext_hdr_options_process --- os/net/ipv6/uip.h | 2 -- os/net/ipv6/uip6.c | 28 +++++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 4e915fe90..505769727 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -95,8 +95,6 @@ * Direct access to extension header options, with explicit ext header and option offset */ #define UIP_EXT_HDR_OPT_BUF(ext, opt) ((struct uip_ext_hdr_opt *)(UIP_IP_PAYLOAD(ext) + (opt))) -#define UIP_EXT_HDR_OPT_PADN_BUF(ext, opt) ((struct uip_ext_hdr_opt_padn *)(UIP_IP_PAYLOAD(ext) + (opt))) -#define UIP_EXT_HDR_OPT_RPL_BUF(ext, opt) ((struct uip_ext_hdr_opt_rpl *)(UIP_IP_PAYLOAD(ext) + (opt))) #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 0ff2e7fc5..e25a12d1c 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -816,7 +816,7 @@ uip_add_rcv_nxt(uint16_t n) * \brief Process the options in Destination and Hop By Hop extension headers */ static uint8_t -ext_hdr_options_process(int ext_offset) +ext_hdr_options_process(uint8_t *ext_buf) { /* * Length field in the extension header: length of the header in units of @@ -824,8 +824,10 @@ ext_hdr_options_process(int ext_offset) * length field in an option : the length of data in the option */ uint8_t opt_offset = 2; - while(opt_offset < ((UIP_EXT_BUF(ext_offset)->len << 3) + 8)) { - switch(UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type) { + struct uip_hbho_hdr *ext_hdr = (struct uip_hbho_hdr *)ext_buf; + while(opt_offset < ((ext_hdr->len << 3) + 8)) { + struct uip_ext_hdr_opt *opt_hdr = (struct uip_ext_hdr_opt *)(ext_buf + opt_offset); + switch(opt_hdr->type) { /* * for now we do not support any options except padding ones * PAD1 does not make sense as the header must be 8bytes aligned, @@ -837,7 +839,7 @@ ext_hdr_options_process(int ext_offset) break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - opt_offset += UIP_EXT_HDR_OPT_PADN_BUF(ext_offset, opt_offset)->opt_len + 2; + opt_offset += ((struct uip_ext_hdr_opt_padn *)opt_hdr)->opt_len + 2; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -849,11 +851,11 @@ ext_hdr_options_process(int ext_offset) * present) is processed. */ LOG_DBG("Processing RPL option\n"); - if(!NETSTACK_ROUTING.ext_header_hbh_update(UIP_IP_PAYLOAD(ext_offset), opt_offset)) { + if(!NETSTACK_ROUTING.ext_header_hbh_update(ext_buf, opt_offset)) { LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - opt_offset += (UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->len) + 2; + opt_offset += opt_hdr->len + 2; return 0; default: /* @@ -869,8 +871,8 @@ ext_hdr_options_process(int ext_offset) * Problem, Code 2, message to the packet's Source Address, * pointing to the unrecognized Option Type. */ - LOG_DBG("Unrecognized option, MSB 0x%x\n", UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type); - switch(UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->type & 0xC0) { + LOG_DBG("Unrecognized option, MSB 0x%x\n", opt_hdr->type); + switch(opt_hdr->type & 0xC0) { case 0: break; case 0x40: @@ -881,11 +883,11 @@ ext_hdr_options_process(int ext_offset) } case 0x80: uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, - (uint32_t)UIP_IPH_LEN + ext_offset + opt_offset); + (ext_buf + opt_offset) - uip_buf); return 2; } /* in the cases were we did not discard, update ext_opt* */ - opt_offset += UIP_EXT_HDR_OPT_BUF(ext_offset, opt_offset)->len + 2; + opt_offset += opt_hdr->len + 2; break; } } @@ -1161,7 +1163,7 @@ uip_process(uint8_t flag) next_header = uipbuf_get_next_header(uip_buf, uip_len, &protocol, true); if(next_header != NULL && protocol == UIP_PROTO_HBHO) { - switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { + switch(ext_hdr_options_process(next_header)) { case 0: break; /* done */ case 1: @@ -1263,7 +1265,7 @@ uip_process(uint8_t flag) uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO; } #endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { + switch(ext_hdr_options_process(next_header)) { case 0: break; /* done */ case 1: @@ -1287,7 +1289,7 @@ uip_process(uint8_t flag) uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1; } #endif /*UIP_CONF_IPV6_CHECKS*/ - switch(ext_hdr_options_process(next_header - UIP_IP_PAYLOAD(0))) { + switch(ext_hdr_options_process(next_header)) { case 0: break; /* done */ case 1: From 655faf0dca0e4a1482eab5af48aa12f34619607f Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 19:11:56 +0200 Subject: [PATCH 27/28] Further simplification of uip.h buffers --- os/net/ipv6/multicast/roll-tm.c | 9 +++++---- os/net/ipv6/uip.h | 15 --------------- os/net/ipv6/uip6.c | 2 +- os/net/routing/rpl-classic/rpl-ext-header.c | 18 +++++++++++------- os/net/routing/rpl-lite/rpl-ext-header.c | 18 +++++++++++------- 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/os/net/ipv6/multicast/roll-tm.c b/os/net/ipv6/multicast/roll-tm.c index 87121fc6d..909ebbfb6 100644 --- a/os/net/ipv6/multicast/roll-tm.c +++ b/os/net/ipv6/multicast/roll-tm.c @@ -456,6 +456,7 @@ static uint16_t last_seq; /*---------------------------------------------------------------------------*/ /* uIPv6 Pointers */ /*---------------------------------------------------------------------------*/ +#define UIP_EXT_BUF ((struct uip_ext_hdr *)UIP_IP_PAYLOAD(0)) #define UIP_EXT_BUF_NEXT ((uint8_t *)(UIP_IP_PAYLOAD(HBHO_TOTAL_LEN))) #define UIP_EXT_OPT_FIRST ((struct hbho_mcast *)(UIP_IP_PAYLOAD(0) + 2)) extern uint16_t uip_slen; @@ -1320,11 +1321,11 @@ out() } /* Slide 'right' by HBHO_TOTAL_LEN bytes */ - memmove(UIP_EXT_BUF_NEXT, UIP_EXT_BUF(0), uip_len - UIP_IPH_LEN); - memset(UIP_EXT_BUF(0), 0, HBHO_TOTAL_LEN); + memmove(UIP_EXT_BUF_NEXT, UIP_EXT_BUF, uip_len - UIP_IPH_LEN); + memset(UIP_EXT_BUF, 0, HBHO_TOTAL_LEN); - UIP_EXT_BUF(0)->next = UIP_IP_BUF->proto; - UIP_EXT_BUF(0)->len = 0; + UIP_EXT_BUF->next = UIP_IP_BUF->proto; + UIP_EXT_BUF->len = 0; lochbhmptr = UIP_EXT_OPT_FIRST; lochbhmptr->type = HBHO_OPT_TYPE_TRICKLE; diff --git a/os/net/ipv6/uip.h b/os/net/ipv6/uip.h index 505769727..32ffc6ed4 100755 --- a/os/net/ipv6/uip.h +++ b/os/net/ipv6/uip.h @@ -81,21 +81,6 @@ #define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD(uip_ext_len)) #define UIP_TCP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_TCPH_LEN) -/** - * Direct access to extension headers, with explicit ext header offset - */ -#define UIP_EXT_BUF(ext) ((struct uip_ext_hdr *)UIP_IP_PAYLOAD(ext)) -#define UIP_HBHO_BUF(ext) ((struct uip_hbho_hdr *)UIP_IP_PAYLOAD(ext)) -#define UIP_RH_BUF(ext) ((struct uip_routing_hdr *)UIP_IP_PAYLOAD(ext)) -#define UIP_FRAG_BUF(ext) ((struct uip_frag_hdr *)UIP_IP_PAYLOAD(ext)) -#define UIP_DESTO_BUF(ext) ((struct uip_desto_hdr *)UIP_IP_PAYLOAD(ext)) -#define UIP_RPL_SRH_BUF(ext) ((struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(ext) + RPL_RH_LEN)) - -/** - * Direct access to extension header options, with explicit ext header and option offset - */ -#define UIP_EXT_HDR_OPT_BUF(ext, opt) ((struct uip_ext_hdr_opt *)(UIP_IP_PAYLOAD(ext) + (opt))) - #include "net/ipv6/uipopt.h" #include "net/ipv6/uipbuf.h" #include "net/linkaddr.h" diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index e25a12d1c..34404b4b8 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -628,7 +628,7 @@ uip_reass(uint8_t *prev_proto_ptr) uint16_t offset=0; uint16_t len; uint16_t i; - struct uip_frag_hdr *frag_buf = UIP_FRAG_BUF(uip_ext_len); + struct uip_frag_hdr *frag_buf = (struct uip_frag_hdr *)UIP_IP_PAYLOAD(uip_ext_len); /* If ip_reasstmr is zero, no packet is present in the buffer */ /* We first write the unfragmentable part of IP header into the reassembly diff --git a/os/net/routing/rpl-classic/rpl-ext-header.c b/os/net/routing/rpl-classic/rpl-ext-header.c index 7212bd6ba..34d9b4907 100644 --- a/os/net/routing/rpl-classic/rpl-ext-header.c +++ b/os/net/routing/rpl-classic/rpl-ext-header.c @@ -290,6 +290,10 @@ insert_srh_header(void) rpl_dag_t *dag; uip_ipaddr_t node_addr; + /* Always insest SRH as first extension header */ + struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0); + struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN); + LOG_INFO("SRH creating source routing header with destination "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_("\n"); @@ -371,22 +375,22 @@ insert_srh_header(void) memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); /* Insert source routing header (as first ext header) */ - UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; + rh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF(0)->len = (ext_len - 8) / 8; - UIP_RH_BUF(0)->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF(0)->seg_left = path_len; + rh_hdr->len = (ext_len - 8) / 8; + rh_hdr->routing_type = RPL_RH_TYPE_SRH; + rh_hdr->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF(0)->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF(0)->pad = padding << 4; + srh_hdr->cmpr = (cmpri << 4) + cmpre; + srh_hdr->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF(0)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); diff --git a/os/net/routing/rpl-lite/rpl-ext-header.c b/os/net/routing/rpl-lite/rpl-ext-header.c index 8a031c099..cb8dcc190 100644 --- a/os/net/routing/rpl-lite/rpl-ext-header.c +++ b/os/net/routing/rpl-lite/rpl-ext-header.c @@ -180,6 +180,10 @@ insert_srh_header(void) uip_sr_node_t *node; uip_ipaddr_t node_addr; + /* Always insest SRH as first extension header */ + struct uip_routing_hdr *rh_hdr = (struct uip_routing_hdr *)UIP_IP_PAYLOAD(0); + struct uip_rpl_srh_hdr *srh_hdr = (struct uip_rpl_srh_hdr *)(UIP_IP_PAYLOAD(0) + RPL_RH_LEN); + LOG_INFO("SRH creating source routing header with destination "); LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr); LOG_INFO_(" \n"); @@ -261,22 +265,22 @@ insert_srh_header(void) memset(uip_buf + UIP_IPH_LEN + uip_ext_len, 0, ext_len); /* Insert source routing header (as first ext header) */ - UIP_RH_BUF(0)->next = UIP_IP_BUF->proto; + rh_hdr->next = UIP_IP_BUF->proto; UIP_IP_BUF->proto = UIP_PROTO_ROUTING; /* Initialize IPv6 Routing Header */ - UIP_RH_BUF(0)->len = (ext_len - 8) / 8; - UIP_RH_BUF(0)->routing_type = RPL_RH_TYPE_SRH; - UIP_RH_BUF(0)->seg_left = path_len; + rh_hdr->len = (ext_len - 8) / 8; + rh_hdr->routing_type = RPL_RH_TYPE_SRH; + rh_hdr->seg_left = path_len; /* Initialize RPL Source Routing Header */ - UIP_RPL_SRH_BUF(0)->cmpr = (cmpri << 4) + cmpre; - UIP_RPL_SRH_BUF(0)->pad = padding << 4; + srh_hdr->cmpr = (cmpri << 4) + cmpre; + srh_hdr->pad = padding << 4; /* Initialize addresses field (the actual source route). * From last to first. */ node = dest_node; - hop_ptr = ((uint8_t *)UIP_RH_BUF(0)) + ext_len - padding; /* Pointer where to write the next hop compressed address */ + hop_ptr = ((uint8_t *)rh_hdr) + ext_len - padding; /* Pointer where to write the next hop compressed address */ while(node != NULL && node->parent != root_node) { NETSTACK_ROUTING.get_sr_node_ipaddr(&node_addr, node); From 9194e8216868f8e6de5805b1ad5e2c5c9d0bdf81 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Fri, 19 Oct 2018 15:45:16 +0200 Subject: [PATCH 28/28] uip6: sanitize parsing of ext header options --- os/net/ipv6/uip6.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/os/net/ipv6/uip6.c b/os/net/ipv6/uip6.c index 34404b4b8..25e052f34 100644 --- a/os/net/ipv6/uip6.c +++ b/os/net/ipv6/uip6.c @@ -823,10 +823,21 @@ ext_hdr_options_process(uint8_t *ext_buf) * 8 bytes, excluding the first 8 bytes * length field in an option : the length of data in the option */ - uint8_t opt_offset = 2; + uint8_t opt_offset = 2; /* 2 first bytes in ext header */ struct uip_hbho_hdr *ext_hdr = (struct uip_hbho_hdr *)ext_buf; - while(opt_offset < ((ext_hdr->len << 3) + 8)) { + uint8_t ext_hdr_len = (ext_hdr->len << 3) + 8; + + while(opt_offset + 2 <= ext_hdr_len) { /* + 2 for opt header */ struct uip_ext_hdr_opt *opt_hdr = (struct uip_ext_hdr_opt *)(ext_buf + opt_offset); + uint8_t opt_len = opt_hdr->len + 2; + + if(opt_offset + opt_len > ext_hdr_len) { + LOG_ERR("RPL Option too long: Dropping Packet\n"); + uip_icmp6_error_output(ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, + (ext_buf + opt_offset) - uip_buf); + return 2; + } + switch(opt_hdr->type) { /* * for now we do not support any options except padding ones @@ -839,7 +850,7 @@ ext_hdr_options_process(uint8_t *ext_buf) break; case UIP_EXT_HDR_OPT_PADN: LOG_DBG("Processing PADN option\n"); - opt_offset += ((struct uip_ext_hdr_opt_padn *)opt_hdr)->opt_len + 2; + opt_offset += opt_len; break; case UIP_EXT_HDR_OPT_RPL: /* Fixes situation when a node that is not using RPL @@ -855,8 +866,8 @@ ext_hdr_options_process(uint8_t *ext_buf) LOG_ERR("RPL Option Error: Dropping Packet\n"); return 1; } - opt_offset += opt_hdr->len + 2; - return 0; + opt_offset += opt_len; + break; default: /* * check the two highest order bits of the option @@ -887,7 +898,7 @@ ext_hdr_options_process(uint8_t *ext_buf) return 2; } /* in the cases were we did not discard, update ext_opt* */ - opt_offset += opt_hdr->len + 2; + opt_offset += opt_len; break; } }