diff --git a/sys/net/sixlowpan/sixlowip.c b/sys/net/sixlowpan/sixlowip.c index 85ac8e4c4f..d49b90cdd7 100644 --- a/sys/net/sixlowpan/sixlowip.c +++ b/sys/net/sixlowpan/sixlowip.c @@ -4,21 +4,17 @@ #include "drivers/cc110x/cc1100.h" #include "radio/radio.h" -#define IP_BUFFER ((struct ipv6_hdr*)&buffer[LL_HEADER_LENGTH]) - -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINT6ADDR(addr) PRINTF("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15]) - +#define ipv6_buf ((struct ipv6_hdr*)&buffer[LL_HDR_LEN]) void bootstrapping(void){ #ifdef SIXLOWPAN_NODE /* create link-local address based on eui-64 */ RADIO.set_address(5); - create_link_local_prefix(&IP_BUFFER->srcaddr); - set_eui64(&IP_BUFFER->srcaddr); - PRINT6ADDR(&IP_BUFFER->srcaddr); + create_link_local_prefix(&ipv6_buf->srcaddr); + set_eui64(&ipv6_buf->srcaddr); + print6addr(&ipv6_buf->srcaddr); /* send router solicitation */ - send_rs(); + send_rtr_sol(); #endif } @@ -67,3 +63,13 @@ void create_link_local_prefix(ipv6_addr *ipaddr){ ipaddr->uint16[2] = 0; ipaddr->uint16[3] = 0; } + +void print6addr(ipv6_addr *ipaddr){ + printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", + ((uint8_t *)ipaddr)[0], ((uint8_t *)ipaddr)[1], ((uint8_t *)ipaddr)[2], + ((uint8_t *)ipaddr)[3], ((uint8_t *)ipaddr)[4], ((uint8_t *)ipaddr)[5], + ((uint8_t *)ipaddr)[6], ((uint8_t *)ipaddr)[7], ((uint8_t *)ipaddr)[8], + ((uint8_t *)ipaddr)[9], ((uint8_t *)ipaddr)[10], ((uint8_t *)ipaddr)[11], + ((uint8_t *)ipaddr)[12], ((uint8_t *)ipaddr)[13], ((uint8_t *)ipaddr)[14], + ((uint8_t *)ipaddr)[15]); +} diff --git a/sys/net/sixlowpan/sixlowip.h b/sys/net/sixlowpan/sixlowip.h index 488b6e5886..ad7ca45871 100644 --- a/sys/net/sixlowpan/sixlowip.h +++ b/sys/net/sixlowpan/sixlowip.h @@ -14,24 +14,15 @@ #define MTU 0x0 #endif -#define IPV6_VERSION 0x60 -#define ICMP_NEXTHEADER 0x3A -#define NEIGHBOR_DISCOVERY_HOPLIMIT 0xFF - -#define ICMP_HEADER_LENGTH 0x4 -#define IPV6_HEADER_LENGTH 0x28 - -/* link layer protocol control information length*/ -#ifdef MSBA2_SENSOR_NODE -#define LL_HEADER_LENGTH 0x4 -#else -#define LL_HEADER_LENGTH 0x0 -#endif +/* IPv6 field values */ +#define IPV6_VER 0x60 +#define ICMPV6_NXT_HDR 0x3A +#define ND_HOPLIMIT 0xFF #define SIXLOWPAN_IPV6_LL_ADDR_LEN 8 /* size of global buffer */ -#define BUFFER_SIZE (LL_HEADER_LENGTH + MTU) +#define BUFFER_SIZE (LL_HDR_LEN + MTU) #define MSBA2_OUI 0x005BA2 // 24bit OUI #define R8BIT 0xA2 // random 8bit @@ -45,13 +36,22 @@ #define RADIO_CONF radio_cc1100 #define RADIO RADIO_CONF -#define MUTLIHOP_HOPLIMIT 64 +#define MULTIHOP_HOPLIMIT 64 extern uint8_t ipv6_ext_hdr_len; -#define LLHDR_IPV6HDR_LENGTH (LL_HEADER_LENGTH + IPV6_HEADER_LENGTH + ipv6_ext_hdr_len) -#define LLHDR_ICMPV6HDR_LENGTH (LL_HEADER_LENGTH + IPV6_HEADER_LENGTH + ipv6_ext_hdr_len + ICMP_HEADER_LENGTH) -#define IPV6HDR_ICMPV6HDR_LENGTH (IPV6_HEADER_LENGTH + ipv6_ext_hdr_len + ICMP_HEADER_LENGTH) +/* base header lengths */ +#ifdef MSBA2_SENSOR_NODE + #define LL_HDR_LEN 0x4 +#else + #define LL_HDR_LEN 0x0 +#endif /* MSBA2_SENSOR_NODE */ +#define ICMPV6_HDR_LEN 0x4 +#define IPV6_HDR_LEN 0x28 +#define LLHDR_IPV6HDR_LEN (LL_HDR_LEN + IPV6_HDR_LEN + ipv6_ext_hdr_len) +#define LLHDR_ICMPV6HDR_LEN (LL_HDR_LEN + IPV6_HDR_LEN + ipv6_ext_hdr_len + ICMPV6_HDR_LEN) +#define IPV6HDR_ICMPV6HDR_LEN (IPV6_HDR_LEN + ipv6_ext_hdr_len + ICMPV6_HDR_LEN) + /* global buffer*/ uint8_t buffer[BUFFER_SIZE]; /* packet length*/ @@ -91,14 +91,12 @@ typedef struct link_layer_addr{ (((uint32_t) (a) & 0x0000ff00) << 8) | \ (((uint32_t) (a) & 0x000000ff) << 24)) +/* function prototypes */ void create_link_local_prefix(ipv6_addr *ipaddr); - void create_all_routers_mcast_addr(ipv6_addr *ipaddr); - void set_eui64(ipv6_addr *ipaddr); - link_layer_addr* get_eui(ipv6_addr *ipaddr); - void bootstrapping(void); +void print6addr(ipv6_addr *ipaddr); #endif /* SIXLOWIP_H*/ diff --git a/sys/net/sixlowpan/sixlowmac.c b/sys/net/sixlowpan/sixlowmac.c index 3ec791e40f..b3087d7cd2 100644 --- a/sys/net/sixlowpan/sixlowmac.c +++ b/sys/net/sixlowpan/sixlowmac.c @@ -5,15 +5,11 @@ #include "drivers/cc110x/cc1100.h" #include "radio/radio.h" -#define IP_BUFFER ((struct ipv6_hdr*)&buffer[LL_HEADER_LENGTH]) - -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINT6ADDR(addr) PRINTF("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15]) - +#define ipv6_buf ((struct ipv6_hdr*)&buffer[LL_HDR_LEN]) /* TODO: payload pointer, payload length */ void send(void){ - RADIO.send(get_radio_address(get_eui(&IP_BUFFER->destaddr)),NULL,NULL,NULL,NULL); + RADIO.send(get_radio_address(get_eui(&ipv6_buf->destaddr)),NULL,NULL,NULL,NULL); } uint16_t get_radio_address(link_layer_addr *lla){ diff --git a/sys/net/sixlowpan/sixlownd.c b/sys/net/sixlowpan/sixlownd.c index b115b46faa..16feac338d 100644 --- a/sys/net/sixlowpan/sixlownd.c +++ b/sys/net/sixlowpan/sixlownd.c @@ -2,142 +2,141 @@ #include "sixlownd.h" #include "sixlowmac.h" #include +#include -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINT6ADDR(addr) PRINTF("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15]) - -static uint8_t option_field_length; +uint8_t rtr_sol_count; +uint8_t option_field_length; uint8_t ipv6_ext_hdr_len = 0; uint16_t packet_length; -#define IP_BUFFER ((struct ipv6_hdr*)&buffer[LL_HEADER_LENGTH]) -#define ICMP_BUFFER ((struct icmpv6_hdr*)&buffer[LLHDR_IPV6HDR_LENGTH]) +#define ipv6_buf ((struct ipv6_hdr*)&buffer[LL_HDR_LEN]) +#define icmp_buf ((struct icmpv6_hdr*)&buffer[LLHDR_IPV6HDR_LEN]) /* fields after icmp header*/ -#define RA_BUFFER ((struct router_advertisement*)&buffer[LLHDR_ICMPV6HDR_LENGTH]) -#define IPH_LLH_BUFFER &buffer[LL_HEADER_LENGTH + IPV6_HEADER_LENGTH] -#define OPT_ST_FIELD_BUFFER ((struct option_src_trgt_lla*)&buffer[LLHDR_IPV6HDR_LENGTH + option_field_length]) -#define OPT_MTU_BUFFER ((struct option_mtu*)&buffer[LLHDR_IPV6HDR_LENGTH + option_field_length]) - -uint8_t rs_count; - -option_prefix_info *prefix; /** prefix list pointer */ +#define rtr_adv_buf ((struct rtr_adv*)&buffer[LLHDR_ICMPV6HDR_LEN]) +#define iph_llh_buf &buffer[LL_HDR_LEN + IPV6_HDR_LEN] +/* neighbour discovery options rfc4861 4.6 */ +#define opt_stllao_buf ((struct opt_stllao*)&buffer[LLHDR_IPV6HDR_LEN + option_field_length]) +#define opt_mtu_buf ((struct opt_mtu*)&buffer[LLHDR_IPV6HDR_LEN + option_field_length]) +#define opt_pi_buf ((struct opt_pi*)&buffer[LLHDR_IPV6HDR_LEN + option_field_length]) /* send router solicitation message - RFC4861 section 4.1 */ -void send_rs(void){ +void send_rtr_sol(void){ //uint8_t ipv6_ext_hdr_len = 0; - if(rs_count < MAX_RTR_SOL){ + if(rtr_sol_count < RTR_SOL_MAX){ packet_length = 0; - ICMP_BUFFER->type = ICMP_RTR_SOL; - ICMP_BUFFER->code = 0; + icmp_buf->type = ICMP_RTR_SOL; + icmp_buf->code = 0; - IP_BUFFER->version_trafficclass = IPV6_VERSION; - IP_BUFFER->trafficclass_flowlabel = 0; - IP_BUFFER->flowlabel = 0; - IP_BUFFER->nextheader = ICMP_NEXTHEADER; - IP_BUFFER->hoplimit = NEIGHBOR_DISCOVERY_HOPLIMIT; + ipv6_buf->version_trafficclass = IPV6_VER; + ipv6_buf->trafficclass_flowlabel = 0; + ipv6_buf->flowlabel = 0; + ipv6_buf->nextheader = ICMPV6_NXT_HDR; + ipv6_buf->hoplimit = ND_HOPLIMIT; + + create_all_routers_mcast_addr(&ipv6_buf->destaddr); + + print6addr(&ipv6_buf->destaddr); - create_all_routers_mcast_addr(&IP_BUFFER->destaddr); - PRINT6ADDR(&IP_BUFFER->destaddr); /* set payload length */ - IP_BUFFER->length = ICMP_HEADER_LENGTH + RTR_SOL_LENGTH + SLLAO_OPT_LENGTH; - set_llao(&buffer[LLHDR_ICMPV6HDR_LENGTH + RTR_SOL_LENGTH], SLLAO_OPT_LENGTH); - ICMP_BUFFER->checksum = 0; - ICMP_BUFFER->checksum = ~chksum_calc(ICMP_NEXTHEADER); - printf("%x\n",ICMP_BUFFER->checksum); + ipv6_buf->length = ICMPV6_HDR_LEN + RTR_SOL_LEN + OPT_STLLAO_LEN; + set_llao(&buffer[LLHDR_ICMPV6HDR_LEN + RTR_SOL_LEN], OPT_SLLAO_TYPE); + icmp_buf->checksum = 0; + icmp_buf->checksum = ~chksum_calc(ICMPV6_NXT_HDR); + printf("%x\n",icmp_buf->checksum); - rs_count++; + rtr_sol_count++; // sleep 4 sec swtimer_usleep(RTR_SOL_INTERVAL * 1000000); } } -void recv_rs(void){ - option_field_length = RTR_SOL_LENGTH; +void recv_rtr_sol(void){ + option_field_length = RTR_SOL_LEN; uint8_t *llao; /* get link layer address option from buf */ - if(OPT_ST_FIELD_BUFFER->type == SLLAO_OPT_LENGTH){ - llao = OPT_ST_FIELD_BUFFER; + if(opt_stllao_buf->type == OPT_STLLAO_LEN){ + llao = opt_stllao_buf; } if(llao != NULL){ // TODO: Neighbor lookup } - send_ra(); + send_rtr_adv(); } -void send_ra(ipv6_addr *addr){ - IP_BUFFER->version_trafficclass = IPV6_VERSION; - IP_BUFFER->trafficclass_flowlabel = 0; - IP_BUFFER->flowlabel = 0; - IP_BUFFER->nextheader = ICMP_NEXTHEADER; - IP_BUFFER->hoplimit = NEIGHBOR_DISCOVERY_HOPLIMIT; +void send_rtr_adv(ipv6_addr *addr){ + ipv6_buf->version_trafficclass = IPV6_VER; + ipv6_buf->trafficclass_flowlabel = 0; + ipv6_buf->flowlabel = 0; + ipv6_buf->nextheader = ICMPV6_NXT_HDR; + ipv6_buf->hoplimit = ND_HOPLIMIT; // not solicited - create_all_nodes_mcast_addr(&IP_BUFFER->destaddr); + create_all_nodes_mcast_addr(&ipv6_buf->destaddr); - ICMP_BUFFER->type = ICMP_RTR_ADV; - ICMP_BUFFER->code = 0; + icmp_buf->type = ICMP_RTR_ADV; + icmp_buf->code = 0; - // set current ttl - RA_BUFFER->hoplimit = MULTIHOP_HOPLIMIT; + //TODO: gethoplimit func, set current ttl + rtr_adv_buf->hoplimit = MULTIHOP_HOPLIMIT; // set M and O flag, last 6 bits are zero - RA_BUFFER->autoconfig_flags = (ND_M_FLAG << 7) | (ND_O_FLAG << 6); - RA_BUFFER->router_lifetime = MAX_RTR_ADV_INTERVAL * MAX_RTR_ADV; - RA_BUFFER->reachable_time = 0; - RA_BUFFER->retrans_timer = 0; + rtr_adv_buf->autoconfig_flags = (RTR_ADV_M_FLAG << 7) | (RTR_ADV_O_FLAG << 6); + rtr_adv_buf->router_lifetime = RTR_ADV_MAX_INTERVAL * RTR_ADV_MAX; + rtr_adv_buf->reachable_time = 0; + rtr_adv_buf->retrans_timer = 0; - set_llao((uint8_t *)OPT_ST_FIELD_BUFFER,SLLAO_OPT_LENGTH); + set_llao((uint8_t *)opt_stllao_buf,OPT_STLLAO_LEN); /* set MTU options */ - OPT_MTU_BUFFER->type = MTU_OPTION_TYPE; - OPT_MTU_BUFFER->length = MTU_OPTION_LENGTH; - OPT_MTU_BUFFER->reserved = 0; + opt_mtu_buf->type = OPT_MTU_TYPE; + opt_mtu_buf->length = OPT_MTU_LEN; + opt_mtu_buf->reserved = 0; // 1500 octets mtu - OPT_MTU_BUFFER->mtu = HTONL(1500); + opt_mtu_buf->mtu = HTONL(1500); /* set packet length */ - packet_length = IPV6_HEADER_LENGTH + ICMP_HEADER_LENGTH + - RTR_ADV_LENGTH + SLLAO_OPT_LEN + MTU_OPTION_HDR_LENGTH; + packet_length = IPV6_HDR_LEN + ICMPV6_HDR_LEN + + RTR_ADV_LEN + OPT_STLLAO_LEN + OPT_MTU_HDR_LEN; /* set payload length field */ /* set prefix option */ - for(){ - - } +// for(){ +// +// } - IP_BUFFER->length = packet_length - IPV6_HEADER_LENGTH; + ipv6_buf->length = packet_length - IPV6_HDR_LEN; /* calculate checksum */ - ICMP_BUFFER->checksum = 0; - ICMP_BUFFER->checksum = ~chksum_calc(ICMP_NEXTHEADER); + icmp_buf->checksum = 0; + icmp_buf->checksum = ~chksum_calc(ICMPV6_NXT_HDR); } /* link-layer address option - RFC4861 section 4.6.1/ RFC4944 8. */ void set_llao(uint8_t *llao, uint8_t type){ - llao[ND_OPT_TYPE] = type; - if(SLLAO_OPT_LENGTH == 16){ + llao[0] = type; + if(OPT_STLLAO_LEN == 16){ // 802.15.4 long address - llao[ND_OPT_LENGTH] = 2; + llao[1] = 2; } else // 16-bit short address - llao[ND_OPT_LENGTH] = 1; + llao[1] = 1; // get link layer address - link_layer_addr *mac = get_eui(&IP_BUFFER->srcaddr); - //get_link_layer_addr_from_ipaddr(&IP_BUFFER->srcaddr,&mac); + link_layer_addr *mac = get_eui(&ipv6_buf->srcaddr); + //get_link_layer_addr_from_ipaddr(&ipv6_buf->srcaddr,&mac); - memcpy(&llao[ND_OPT_DATA], mac, SIXLOWPAN_IPV6_LL_ADDR_LEN); + memcpy(&llao[2], mac, SIXLOWPAN_IPV6_LL_ADDR_LEN); // padding (all zeros) - 48bit - memset(&llao[ND_OPT_DATA + SIXLOWPAN_IPV6_LL_ADDR_LEN], 0, - SLLAO_OPT_LENGTH - SIXLOWPAN_IPV6_LL_ADDR_LEN - 2); + memset(&llao[2 + SIXLOWPAN_IPV6_LL_ADDR_LEN], 0, + OPT_STLLAO_LEN - SIXLOWPAN_IPV6_LL_ADDR_LEN - 2); } /* pseudo-header checksum calculation - RFC4443 Section 2.3*/ uint16_t chksum_calc(uint8_t type){ - uint16_t length = IP_BUFFER->length; + uint16_t length = ipv6_buf->length; uint16_t sum = length + type; - uint8_t *addrptr = (uint8_t)&IP_BUFFER->srcaddr; + uint8_t *addrptr = (uint8_t)&ipv6_buf->srcaddr; uint16_t addr_fields_length = (2 * sizeof(ipv6_addr)); while(addr_fields_length > 1){ sum += (addrptr[0] << 8) + addrptr[1]; @@ -148,7 +147,7 @@ uint16_t chksum_calc(uint8_t type){ sum += (addrptr[0] << 8) + 0; } - uint8_t *bufptr = IPH_LLH_BUFFER; + uint8_t *bufptr = iph_llh_buf; while(length > 1){ sum += (bufptr[0] << 8) + bufptr[1]; bufptr += 2; diff --git a/sys/net/sixlowpan/sixlownd.h b/sys/net/sixlowpan/sixlownd.h index ab603917d3..c503fdf644 100644 --- a/sys/net/sixlowpan/sixlownd.h +++ b/sys/net/sixlowpan/sixlownd.h @@ -1,39 +1,40 @@ #include -#define SLLAO_OPT_LENGTH 16 // 16 if 802.15.4 otherwise 8 -#define ND_OPT_TYPE 0 -#define ND_OPT_LENGTH 1 -#define ND_OPT_DATA 2 -/* router advertisment */ -#define ND_M_FLAG 0 -#define ND_O_FLAG 0 -#define MAX_RTR_ADV 3 -#define MAX_RTR_ADV_INTERVAL 600 -#define RTR_ADV_LENGTH 12 -#define ICMP_RTR_ADV 134 /* router solicitation */ -#define ICMP_RTR_ 133 -#define RTR_SOL_LENGTH 4 +#define RTR_SOL_LEN 4 #define RTR_SOL_INTERVAL 4 -#define MAX_RTR_SOL 3 -/* mtu option rfc4861 4.6.4*/ -#define MTU_OPTION_TYPE 5 -#define MTU_OPTION_LENGTH 1 -#define MTU_OPTION_HDR_LENGTH 8 +#define RTR_SOL_MAX 3 +/* router advertisment */ +#define RTR_ADV_M_FLAG 0 +#define RTR_ADV_O_FLAG 0 +#define RTR_ADV_MAX 3 +#define RTR_ADV_MAX_INTERVAL 600 +#define RTR_ADV_LEN 12 +/* icmp message types rfc4861 4.*/ +#define ICMP_RTR_ADV 134 +#define ICMP_RTR_SOL 133 +/* stllao option rfc4861 4.6.1 */ +#define OPT_STLLAO_LEN 16 +#define OPT_SLLAO_TYPE 1 +#define OPT_TLLAO_TYPE 2 +/* mtu option rfc4861 4.6.4 */ +#define OPT_MTU_TYPE 5 +#define OPT_MTU_LEN 1 +#define OPT_MTU_HDR_LEN 8 -typedef struct option_src_trgt_lla { +typedef struct opt_stllao { uint8_t type; uint8_t length; -} option_src_trgt_lla; +} opt_stllao; -typedef struct option_mtu { +typedef struct opt_mtu { uint8_t type; uint8_t length; uint16_t reserved; uint32_t mtu; -} option_mtu; +} opt_mtu; -typedef struct option_prefix_info { +typedef struct opt_pi { uint8_t type; uint8_t length; uint8_t prefix_length; @@ -41,9 +42,15 @@ typedef struct option_prefix_info { uint32_t valid_lifetime; uint32_t preferred_lifetime; ipv6_addr prefix; -} option_prefix_info; +} opt_pi; -struct router_advertisement { +typedef struct prefix_list { + ipv6_addr prefix; + uint8_t inuse; + uint8_t advertisment; +} prefix_list; + +struct rtr_adv { uint8_t hoplimit; uint8_t autoconfig_flags; uint16_t router_lifetime; @@ -51,5 +58,5 @@ struct router_advertisement { uint32_t retrans_timer; }; -void send_rs(void); +void send_rtr_sol(void); uint16_t chksum_calc(uint8_t type);