From aaf06754eac66057aad4d09e82a98925565c3566 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Thu, 18 Oct 2018 11:20:34 +0200 Subject: [PATCH] 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;