63 #include "dev/watchdog.h"
77 #define PRINTF(...) printf(__VA_ARGS__)
78 #define PRINTFI(...) printf(__VA_ARGS__)
79 #define PRINTFO(...) printf(__VA_ARGS__)
80 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
81 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5],lladdr->addr[6], lladdr->addr[7])
82 #define PRINTPACKETBUF() PRINTF("RIME buffer: "); for(p = 0; p < packetbuf_datalen(); p++){PRINTF("%.2X", *(rime_ptr + p));} PRINTF("\n")
83 #define PRINTUIPBUF() PRINTF("UIP buffer: "); for(p = 0; p < uip_len; p++){PRINTF("%.2X", uip_buf[p]);}PRINTF("\n")
84 #define PRINTSICSLOWPANBUF() PRINTF("SICSLOWPAN buffer: "); for(p = 0; p < sicslowpan_len; p++){PRINTF("%.2X", sicslowpan_buf[p]);}PRINTF("\n")
89 #define PRINT6ADDR(addr)
90 #define PRINTLLADDR(lladdr)
91 #define PRINTPACKETBUF()
93 #define PRINTSICSLOWPANBUF()
99 #define UIP_LOG(m) uip_log(m)
104 #ifdef SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
105 #define SICSLOWPAN_MAX_MAC_TRANSMISSIONS SICSLOWPAN_CONF_MAX_MAC_TRANSMISSIONS
107 #define SICSLOWPAN_MAX_MAC_TRANSMISSIONS 4
110 #ifndef SICSLOWPAN_COMPRESSION
111 #ifdef SICSLOWPAN_CONF_COMPRESSION
112 #define SICSLOWPAN_COMPRESSION SICSLOWPAN_CONF_COMPRESSION
114 #define SICSLOWPAN_COMPRESSION SICSLOWPAN_COMPRESSION_IPV6
118 #ifndef SICSLOWPAN_CONF_NEIGHBOR_INFO
120 #define SICSLOWPAN_CONF_NEIGHBOR_INFO UIP_CONF_IPV6_RPL
123 #define GET16(ptr,index) (((uint16_t)((ptr)[index] << 8)) | ((ptr)[(index) + 1]))
124 #define SET16(ptr,index,value) do { \
125 (ptr)[index] = ((value) >> 8) & 0xff; \
126 (ptr)[index + 1] = (value) & 0xff; \
132 #define RIME_FRAG_PTR (rime_ptr)
133 #define RIME_FRAG_DISPATCH_SIZE 0
134 #define RIME_FRAG_TAG 2
135 #define RIME_FRAG_OFFSET 4
138 #define RIME_IPHC_BUF ((uint8_t *)(rime_ptr + rime_hdr_len))
140 #define RIME_HC1_PTR (rime_ptr + rime_hdr_len)
141 #define RIME_HC1_DISPATCH 0
142 #define RIME_HC1_ENCODING 1
143 #define RIME_HC1_TTL 2
145 #define RIME_HC1_HC_UDP_PTR (rime_ptr + rime_hdr_len)
146 #define RIME_HC1_HC_UDP_DISPATCH 0
147 #define RIME_HC1_HC_UDP_HC1_ENCODING 1
148 #define RIME_HC1_HC_UDP_UDP_ENCODING 2
149 #define RIME_HC1_HC_UDP_TTL 3
150 #define RIME_HC1_HC_UDP_PORTS 4
151 #define RIME_HC1_HC_UDP_CHKSUM 5
156 #define SICSLOWPAN_IP_BUF ((struct uip_ip_hdr *)&sicslowpan_buf[UIP_LLH_LEN])
157 #define SICSLOWPAN_UDP_BUF ((struct uip_udp_hdr *)&sicslowpan_buf[UIP_LLIPH_LEN])
159 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
160 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
161 #define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[UIP_LLIPH_LEN])
166 #define MAC_MAX_PAYLOAD 102
172 #ifdef SICSLOWPAN_CONF_COMPRESSION_THRESHOLD
173 #define COMPRESSION_THRESHOLD SICSLOWPAN_CONF_COMPRESSION_THRESHOLD
175 #define COMPRESSION_THRESHOLD 0
184 #ifdef SICSLOWPAN_NH_COMPRESSOR
194 static u8_t *rime_ptr;
201 static u8_t rime_hdr_len;
209 static u8_t rime_payload_len;
215 static u8_t uncomp_hdr_len;
218 #if SICSLOWPAN_CONF_FRAG
223 static uint16_t sicslowpan_len;
231 #define sicslowpan_buf (sicslowpan_aligned_buf.u8)
239 static uint16_t processed_ip_len;
242 static uint16_t my_tag;
245 static uint16_t reass_tag;
248 rimeaddr_t frag_sender;
251 static struct timer reass_timer;
257 #define sicslowpan_buf uip_buf
258 #define sicslowpan_len uip_len
261 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC06
267 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
276 static uint8_t *hc06_ptr;
285 const uint8_t unc_llconf[] = {0x0f,0x28,0x22,0x20};
292 const uint8_t unc_ctxconf[] = {0x00,0x88,0x82,0x80};
299 const uint8_t unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
302 const uint8_t llprefix[] = {0xfe, 0x80};
305 static const uint8_t ttl_values[] = {0, 1, 64, 255};
316 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
319 if((addr_contexts[i].used == 1) &&
320 uip_ipaddr_prefixcmp(&addr_contexts[i].prefix, ipaddr, 64)) {
321 return &addr_contexts[i];
330 addr_context_lookup_by_number(u8_t number)
333 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
336 if((addr_contexts[i].used == 1) &&
337 addr_contexts[i].number == number) {
338 return &addr_contexts[i];
348 if(uip_is_addr_mac_addr_based(ipaddr, lladdr)) {
352 memcpy(hc06_ptr, &ipaddr->u16[7], 2);
357 memcpy(hc06_ptr, &ipaddr->u16[4], 8);
371 uncompress_addr(
uip_ipaddr_t *ipaddr, uint8_t
const prefix[],
374 uint8_t prefcount = pref_post_count >> 4;
375 uint8_t postcount = pref_post_count & 0x0f;
377 prefcount = prefcount == 15 ? 16 : prefcount;
378 postcount = postcount == 15 ? 16 : postcount;
380 PRINTF(
"Uncompressing %d + %d => ", prefcount, postcount);
383 memcpy(ipaddr, prefix, prefcount);
385 if(prefcount + postcount < 16) {
386 memset(&ipaddr->u8[prefcount], 0, 16 - (prefcount + postcount));
389 memcpy(&ipaddr->u8[16 - postcount], hc06_ptr, postcount);
390 if(postcount == 2 && prefcount < 11) {
392 ipaddr->u8[11] = 0xff;
393 ipaddr->u8[12] = 0xfe;
395 hc06_ptr += postcount;
396 }
else if (prefcount > 0) {
441 compress_hdr_hc06(rimeaddr_t *rime_destaddr)
443 uint8_t tmp, iphc0, iphc1;
445 PRINTF(
"before compression: ");
446 for(tmp = 0; tmp <
UIP_IP_BUF->len[1] + 40; tmp++) {
447 uint8_t data = ((uint8_t *) (
UIP_IP_BUF))[tmp];
448 PRINTF(
"%02x", data);
453 hc06_ptr = rime_ptr + 2;
461 iphc0 = SICSLOWPAN_DISPATCH_IPHC;
463 RIME_IPHC_BUF[2] = 0;
475 if(addr_context_lookup_by_prefix(&
UIP_IP_BUF->destipaddr) !=
NULL ||
478 PRINTF(
"IPHC: compressing dest or src ipaddr - setting CID\n");
479 iphc1 |= SICSLOWPAN_IPHC_CID;
492 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
497 iphc0 |= SICSLOWPAN_IPHC_FL_C;
501 iphc0 |= SICSLOWPAN_IPHC_TC_C;
512 iphc0 |= SICSLOWPAN_IPHC_TC_C;
513 *hc06_ptr = (tmp & 0xc0) |
531 iphc0 |= SICSLOWPAN_IPHC_NH_C;
534 #ifdef SICSLOWPAN_NH_COMPRESSOR
535 if(SICSLOWPAN_NH_COMPRESSOR.is_compressable(
UIP_IP_BUF->proto)) {
536 iphc0 |= SICSLOWPAN_IPHC_NH_C;
539 if ((iphc0 & SICSLOWPAN_IPHC_NH_C) == 0) {
553 iphc0 |= SICSLOWPAN_IPHC_TTL_1;
556 iphc0 |= SICSLOWPAN_IPHC_TTL_64;
559 iphc0 |= SICSLOWPAN_IPHC_TTL_255;
568 if(uip_is_addr_unspecified(&
UIP_IP_BUF->srcipaddr)) {
569 PRINTF(
"IPHC: compressing unspecified - setting SAC\n");
570 iphc1 |= SICSLOWPAN_IPHC_SAC;
571 iphc1 |= SICSLOWPAN_IPHC_SAM_00;
572 }
else if((context = addr_context_lookup_by_prefix(&
UIP_IP_BUF->srcipaddr))
575 PRINTF(
"IPHC: compressing src with context - setting CID & SAC ctx: %d\n",
577 iphc1 |= SICSLOWPAN_IPHC_CID | SICSLOWPAN_IPHC_SAC;
578 RIME_IPHC_BUF[2] |= context->number << 4;
581 iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_SAM_BIT,
584 }
else if(uip_is_addr_link_local(&
UIP_IP_BUF->srcipaddr) &&
588 iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_SAM_BIT,
592 iphc1 |= SICSLOWPAN_IPHC_SAM_00;
593 memcpy(hc06_ptr, &
UIP_IP_BUF->srcipaddr.u16[0], 16);
598 if(uip_is_addr_mcast(&
UIP_IP_BUF->destipaddr)) {
600 iphc1 |= SICSLOWPAN_IPHC_M;
601 if(sicslowpan_is_mcast_addr_compressable8(&
UIP_IP_BUF->destipaddr)) {
602 iphc1 |= SICSLOWPAN_IPHC_DAM_11;
606 }
else if(sicslowpan_is_mcast_addr_compressable32(&
UIP_IP_BUF->destipaddr)) {
607 iphc1 |= SICSLOWPAN_IPHC_DAM_10;
610 memcpy(hc06_ptr + 1, &
UIP_IP_BUF->destipaddr.u8[13], 3);
612 }
else if(sicslowpan_is_mcast_addr_compressable48(&
UIP_IP_BUF->destipaddr)) {
613 iphc1 |= SICSLOWPAN_IPHC_DAM_01;
616 memcpy(hc06_ptr + 1, &
UIP_IP_BUF->destipaddr.u8[11], 5);
619 iphc1 |= SICSLOWPAN_IPHC_DAM_00;
621 memcpy(hc06_ptr, &
UIP_IP_BUF->destipaddr.u8[0], 16);
626 if((context = addr_context_lookup_by_prefix(&
UIP_IP_BUF->destipaddr)) !=
NULL) {
628 iphc1 |= SICSLOWPAN_IPHC_DAC;
629 RIME_IPHC_BUF[2] |= context->number;
632 iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_DAM_BIT,
635 }
else if(uip_is_addr_link_local(&
UIP_IP_BUF->destipaddr) &&
639 iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_DAM_BIT,
643 iphc1 |= SICSLOWPAN_IPHC_DAM_00;
644 memcpy(hc06_ptr, &
UIP_IP_BUF->destipaddr.u16[0], 16);
649 uncomp_hdr_len = UIP_IPH_LEN;
654 PRINTF(
"IPHC: Uncompressed UDP ports on send side: %x, %x\n",
657 if(((
UIP_HTONS(UIP_UDP_BUF->srcport) & 0xfff0) == SICSLOWPAN_UDP_4_BIT_PORT_MIN) &&
658 ((
UIP_HTONS(UIP_UDP_BUF->destport) & 0xfff0) == SICSLOWPAN_UDP_4_BIT_PORT_MIN)) {
660 *hc06_ptr = SICSLOWPAN_NHC_UDP_CS_P_11;
661 PRINTF(
"IPHC: remove 12 b of both source & dest with prefix 0xFOB\n");
663 (u8_t)((
UIP_HTONS(UIP_UDP_BUF->srcport) -
664 SICSLOWPAN_UDP_4_BIT_PORT_MIN) << 4) +
666 SICSLOWPAN_UDP_4_BIT_PORT_MIN));
668 }
else if((
UIP_HTONS(UIP_UDP_BUF->destport) & 0xff00) == SICSLOWPAN_UDP_8_BIT_PORT_MIN) {
670 *hc06_ptr = SICSLOWPAN_NHC_UDP_CS_P_01;
671 PRINTF(
"IPHC: leave source, remove 8 bits of dest with prefix 0xF0\n");
672 memcpy(hc06_ptr + 1, &UIP_UDP_BUF->srcport, 2);
674 (u8_t)((
UIP_HTONS(UIP_UDP_BUF->destport) -
675 SICSLOWPAN_UDP_8_BIT_PORT_MIN));
677 }
else if((
UIP_HTONS(UIP_UDP_BUF->srcport) & 0xff00) == SICSLOWPAN_UDP_8_BIT_PORT_MIN) {
679 *hc06_ptr = SICSLOWPAN_NHC_UDP_CS_P_10;
680 PRINTF(
"IPHC: remove 8 bits of source with prefix 0xF0, leave dest. hch: %i\n", *hc06_ptr);
682 (u8_t)((
UIP_HTONS(UIP_UDP_BUF->srcport) -
683 SICSLOWPAN_UDP_8_BIT_PORT_MIN));
684 memcpy(hc06_ptr + 2, &UIP_UDP_BUF->destport, 2);
688 *hc06_ptr = SICSLOWPAN_NHC_UDP_CS_P_00;
689 PRINTF(
"IPHC: cannot compress headers\n");
690 memcpy(hc06_ptr + 1, &UIP_UDP_BUF->srcport, 4);
695 memcpy(hc06_ptr, &UIP_UDP_BUF->udpchksum, 2);
698 uncomp_hdr_len += UIP_UDPH_LEN;
702 #ifdef SICSLOWPAN_NH_COMPRESSOR
704 hc06_ptr += SICSLOWPAN_NH_COMPRESSOR.compress(hc06_ptr, &uncomp_hdr_len);
708 RIME_IPHC_BUF[0] = iphc0;
709 RIME_IPHC_BUF[1] = iphc1;
711 rime_hdr_len = hc06_ptr - rime_ptr;
732 uncompress_hdr_hc06(uint16_t ip_len)
734 uint8_t tmp, iphc0, iphc1;
736 hc06_ptr = rime_ptr + rime_hdr_len + 2;
738 iphc0 = RIME_IPHC_BUF[0];
739 iphc1 = RIME_IPHC_BUF[1];
742 if(iphc1 & SICSLOWPAN_IPHC_CID) {
743 PRINTF(
"IPHC: CID flag set - increase header with one\n");
748 if((iphc0 & SICSLOWPAN_IPHC_FL_C) == 0) {
750 if((iphc0 & SICSLOWPAN_IPHC_TC_C) == 0) {
752 memcpy(&SICSLOWPAN_IP_BUF->tcflow, hc06_ptr + 1, 3);
757 SICSLOWPAN_IP_BUF->vtc = 0x60 | ((tmp >> 2) & 0x0f);
759 SICSLOWPAN_IP_BUF->tcflow = ((tmp >> 2) & 0x30) | (tmp << 6) |
760 (SICSLOWPAN_IP_BUF->tcflow & 0x0f);
763 SICSLOWPAN_IP_BUF->vtc = 0x60;
765 SICSLOWPAN_IP_BUF->tcflow = (*hc06_ptr & 0x0F) |
766 ((*hc06_ptr >> 2) & 0x30);
767 memcpy(&SICSLOWPAN_IP_BUF->flow, hc06_ptr + 1, 2);
773 if((iphc0 & SICSLOWPAN_IPHC_TC_C) == 0) {
775 SICSLOWPAN_IP_BUF->vtc = 0x60 | ((*hc06_ptr >> 2) & 0x0f);
776 SICSLOWPAN_IP_BUF->tcflow = ((*hc06_ptr << 6) & 0xC0) | ((*hc06_ptr >> 2) & 0x30);
777 SICSLOWPAN_IP_BUF->flow = 0;
781 SICSLOWPAN_IP_BUF->vtc = 0x60;
782 SICSLOWPAN_IP_BUF->tcflow = 0;
783 SICSLOWPAN_IP_BUF->flow = 0;
788 if((iphc0 & SICSLOWPAN_IPHC_NH_C) == 0) {
790 SICSLOWPAN_IP_BUF->proto = *hc06_ptr;
791 PRINTF(
"IPHC: next header inline: %d\n", SICSLOWPAN_IP_BUF->proto);
796 if((iphc0 & 0x03) != SICSLOWPAN_IPHC_TTL_I) {
797 SICSLOWPAN_IP_BUF->ttl = ttl_values[iphc0 & 0x03];
799 SICSLOWPAN_IP_BUF->ttl = *hc06_ptr;
804 tmp = ((iphc1 & SICSLOWPAN_IPHC_SAM_11) >> SICSLOWPAN_IPHC_SAM_BIT) & 0x03;
807 if(iphc1 & SICSLOWPAN_IPHC_SAC) {
808 uint8_t sci = (iphc1 & SICSLOWPAN_IPHC_CID) ?
809 RIME_IPHC_BUF[2] >> 4 : 0;
813 context = addr_context_lookup_by_number(sci);
814 if(context ==
NULL) {
815 PRINTF(
"sicslowpan uncompress_hdr: error context not found\n");
820 uncompress_addr(&SICSLOWPAN_IP_BUF->srcipaddr,
821 tmp != 0 ? context->prefix :
NULL, unc_ctxconf[tmp],
825 uncompress_addr(&SICSLOWPAN_IP_BUF->srcipaddr, llprefix, unc_llconf[tmp],
831 tmp = ((iphc1 & SICSLOWPAN_IPHC_DAM_11) >> SICSLOWPAN_IPHC_DAM_BIT) & 0x03;
834 if(iphc1 & SICSLOWPAN_IPHC_M) {
836 if(iphc1 & SICSLOWPAN_IPHC_DAC) {
844 uint8_t prefix[] = {0xff, 0x02};
845 if(tmp > 0 && tmp < 3) {
846 prefix[1] = *hc06_ptr;
850 uncompress_addr(&SICSLOWPAN_IP_BUF->destipaddr, prefix,
851 unc_mxconf[tmp],
NULL);
856 if(iphc1 & SICSLOWPAN_IPHC_DAC) {
857 uint8_t dci = (iphc1 & SICSLOWPAN_IPHC_CID) ?
858 RIME_IPHC_BUF[2] & 0x0f : 0;
859 context = addr_context_lookup_by_number(dci);
862 if(context ==
NULL) {
863 PRINTF(
"sicslowpan uncompress_hdr: error context not found\n");
866 uncompress_addr(&SICSLOWPAN_IP_BUF->destipaddr, context->prefix,
868 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
871 uncompress_addr(&SICSLOWPAN_IP_BUF->destipaddr, llprefix,
873 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
876 uncomp_hdr_len += UIP_IPH_LEN;
879 if((iphc0 & SICSLOWPAN_IPHC_NH_C)) {
881 if((*hc06_ptr & SICSLOWPAN_NHC_UDP_MASK) == SICSLOWPAN_NHC_UDP_ID) {
882 uint8_t checksum_compressed;
883 SICSLOWPAN_IP_BUF->proto = UIP_PROTO_UDP;
884 checksum_compressed = *hc06_ptr & SICSLOWPAN_NHC_UDP_CHECKSUMC;
885 PRINTF(
"IPHC: Incoming header value: %i\n", *hc06_ptr);
886 switch(*hc06_ptr & SICSLOWPAN_NHC_UDP_CS_P_11) {
887 case SICSLOWPAN_NHC_UDP_CS_P_00:
889 memcpy(&SICSLOWPAN_UDP_BUF->srcport, hc06_ptr + 1, 2);
890 memcpy(&SICSLOWPAN_UDP_BUF->destport, hc06_ptr + 3, 2);
891 PRINTF(
"IPHC: Uncompressed UDP ports (ptr+5): %x, %x\n",
896 case SICSLOWPAN_NHC_UDP_CS_P_01:
898 PRINTF(
"IPHC: Decompressing destination\n");
899 memcpy(&SICSLOWPAN_UDP_BUF->srcport, hc06_ptr + 1, 2);
900 SICSLOWPAN_UDP_BUF->destport =
UIP_HTONS(SICSLOWPAN_UDP_8_BIT_PORT_MIN + (*(hc06_ptr + 3)));
901 PRINTF(
"IPHC: Uncompressed UDP ports (ptr+4): %x, %x\n",
906 case SICSLOWPAN_NHC_UDP_CS_P_10:
908 PRINTF(
"IPHC: Decompressing source\n");
909 SICSLOWPAN_UDP_BUF->srcport =
UIP_HTONS(SICSLOWPAN_UDP_8_BIT_PORT_MIN +
911 memcpy(&SICSLOWPAN_UDP_BUF->destport, hc06_ptr + 2, 2);
912 PRINTF(
"IPHC: Uncompressed UDP ports (ptr+4): %x, %x\n",
917 case SICSLOWPAN_NHC_UDP_CS_P_11:
919 SICSLOWPAN_UDP_BUF->srcport =
UIP_HTONS(SICSLOWPAN_UDP_4_BIT_PORT_MIN +
920 (*(hc06_ptr + 1) >> 4));
921 SICSLOWPAN_UDP_BUF->destport =
UIP_HTONS(SICSLOWPAN_UDP_4_BIT_PORT_MIN +
922 ((*(hc06_ptr + 1)) & 0x0F));
923 PRINTF(
"IPHC: Uncompressed UDP ports (ptr+2): %x, %x\n",
929 PRINTF(
"sicslowpan uncompress_hdr: error unsupported UDP compression\n");
932 if(!checksum_compressed) {
933 memcpy(&SICSLOWPAN_UDP_BUF->udpchksum, hc06_ptr, 2);
935 PRINTF(
"IPHC: sicslowpan uncompress_hdr: checksum included\n");
937 PRINTF(
"IPHC: sicslowpan uncompress_hdr: checksum *NOT* included\n");
939 uncomp_hdr_len += UIP_UDPH_LEN;
941 #ifdef SICSLOWPAN_NH_COMPRESSOR
943 hc06_ptr += SICSLOWPAN_NH_COMPRESSOR.uncompress(hc06_ptr,
sicslowpan_buf, &uncomp_hdr_len);
948 rime_hdr_len = hc06_ptr - rime_ptr;
953 SICSLOWPAN_IP_BUF->len[0] = 0;
954 SICSLOWPAN_IP_BUF->len[1] =
packetbuf_datalen() - rime_hdr_len + uncomp_hdr_len - UIP_IPH_LEN;
957 SICSLOWPAN_IP_BUF->len[0] = (ip_len - UIP_IPH_LEN) >> 8;
958 SICSLOWPAN_IP_BUF->len[1] = (ip_len - UIP_IPH_LEN) & 0x00FF;
962 if(SICSLOWPAN_IP_BUF->proto == UIP_PROTO_UDP) {
963 memcpy(&SICSLOWPAN_UDP_BUF->udplen, &SICSLOWPAN_IP_BUF->len[0], 2);
972 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC1
1026 compress_hdr_hc1(rimeaddr_t *rime_destaddr)
1035 !uip_is_addr_link_local(&
UIP_IP_BUF->srcipaddr) ||
1037 !uip_is_addr_link_local(&
UIP_IP_BUF->destipaddr) ||
1038 !uip_is_addr_mac_addr_based(&
UIP_IP_BUF->destipaddr,
1049 *rime_ptr = SICSLOWPAN_DISPATCH_IPV6;
1050 rime_hdr_len += SICSLOWPAN_IPV6_HDR_LEN;
1051 memcpy(rime_ptr + rime_hdr_len,
UIP_IP_BUF, UIP_IPH_LEN);
1052 rime_hdr_len += UIP_IPH_LEN;
1053 uncomp_hdr_len += UIP_IPH_LEN;
1061 RIME_HC1_PTR[RIME_HC1_DISPATCH] = SICSLOWPAN_DISPATCH_HC1;
1062 uncomp_hdr_len += UIP_IPH_LEN;
1064 case UIP_PROTO_ICMP6:
1066 RIME_HC1_PTR[RIME_HC1_ENCODING] = 0xFC;
1067 RIME_HC1_PTR[RIME_HC1_TTL] =
UIP_IP_BUF->ttl;
1068 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1073 RIME_HC1_PTR[RIME_HC1_ENCODING] = 0xFE;
1074 RIME_HC1_PTR[RIME_HC1_TTL] =
UIP_IP_BUF->ttl;
1075 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1085 PRINTF(
"local/remote port %u/%u\n",UIP_UDP_BUF->srcport,UIP_UDP_BUF->destport);
1086 if(
UIP_HTONS(UIP_UDP_BUF->srcport) >= SICSLOWPAN_UDP_PORT_MIN &&
1087 UIP_HTONS(UIP_UDP_BUF->srcport) < SICSLOWPAN_UDP_PORT_MAX &&
1088 UIP_HTONS(UIP_UDP_BUF->destport) >= SICSLOWPAN_UDP_PORT_MIN &&
1089 UIP_HTONS(UIP_UDP_BUF->destport) < SICSLOWPAN_UDP_PORT_MAX) {
1091 RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_HC1_ENCODING] = 0xFB;
1094 RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_UDP_ENCODING] = 0xE0;
1095 RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_TTL] =
UIP_IP_BUF->ttl;
1097 RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_PORTS] =
1099 SICSLOWPAN_UDP_PORT_MIN) << 4) +
1100 (u8_t)((
UIP_HTONS(UIP_UDP_BUF->destport) - SICSLOWPAN_UDP_PORT_MIN));
1101 memcpy(&RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_CHKSUM], &UIP_UDP_BUF->udpchksum, 2);
1102 rime_hdr_len += SICSLOWPAN_HC1_HC_UDP_HDR_LEN;
1103 uncomp_hdr_len += UIP_UDPH_LEN;
1106 RIME_HC1_PTR[RIME_HC1_ENCODING] = 0xFA;
1107 RIME_HC1_PTR[RIME_HC1_TTL] =
UIP_IP_BUF->ttl;
1108 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1134 uncompress_hdr_hc1(uint16_t ip_len)
1137 SICSLOWPAN_IP_BUF->vtc = 0x60;
1138 SICSLOWPAN_IP_BUF->tcflow = 0;
1139 SICSLOWPAN_IP_BUF->flow = 0;
1142 uip_ip6addr(&SICSLOWPAN_IP_BUF->srcipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
1143 uip_sd6_set_addr_iid(&SICSLOWPAN_IP_BUF->srcipaddr,
1144 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
1145 uip_ip6addr(&SICSLOWPAN_IP_BUF->destipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
1146 uip_sd6_set_addr_iid(&SICSLOWPAN_IP_BUF->destipaddr,
1147 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
1149 uncomp_hdr_len += UIP_IPH_LEN;
1152 switch(RIME_HC1_PTR[RIME_HC1_ENCODING] & 0x06) {
1153 case SICSLOWPAN_HC1_NH_ICMP6:
1154 SICSLOWPAN_IP_BUF->proto = UIP_PROTO_ICMP6;
1155 SICSLOWPAN_IP_BUF->ttl = RIME_HC1_PTR[RIME_HC1_TTL];
1156 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1159 case SICSLOWPAN_HC1_NH_TCP:
1160 SICSLOWPAN_IP_BUF->proto = UIP_PROTO_TCP;
1161 SICSLOWPAN_IP_BUF->ttl = RIME_HC1_PTR[RIME_HC1_TTL];
1162 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1166 case SICSLOWPAN_HC1_NH_UDP:
1167 SICSLOWPAN_IP_BUF->proto = UIP_PROTO_UDP;
1168 if(RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_HC1_ENCODING] & 0x01) {
1170 if(RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_UDP_ENCODING] !=
1171 SICSLOWPAN_HC_UDP_ALL_C) {
1172 PRINTF(
"sicslowpan (uncompress_hdr), packet not supported");
1176 SICSLOWPAN_IP_BUF->ttl = RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_TTL];
1178 SICSLOWPAN_UDP_BUF->srcport =
1180 (RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_PORTS] >> 4));
1181 SICSLOWPAN_UDP_BUF->destport =
1183 (RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_PORTS] & 0x0F));
1184 memcpy(&SICSLOWPAN_UDP_BUF->udpchksum, &RIME_HC1_HC_UDP_PTR[RIME_HC1_HC_UDP_CHKSUM], 2);
1185 uncomp_hdr_len += UIP_UDPH_LEN;
1186 rime_hdr_len += SICSLOWPAN_HC1_HC_UDP_HDR_LEN;
1188 rime_hdr_len += SICSLOWPAN_HC1_HDR_LEN;
1200 SICSLOWPAN_IP_BUF->len[0] = 0;
1201 SICSLOWPAN_IP_BUF->len[1] =
packetbuf_datalen() - rime_hdr_len + uncomp_hdr_len - UIP_IPH_LEN;
1204 SICSLOWPAN_IP_BUF->len[0] = (ip_len - UIP_IPH_LEN) >> 8;
1205 SICSLOWPAN_IP_BUF->len[1] = (ip_len - UIP_IPH_LEN) & 0x00FF;
1208 if(SICSLOWPAN_IP_BUF->proto == UIP_PROTO_UDP) {
1209 memcpy(&SICSLOWPAN_UDP_BUF->udplen, &SICSLOWPAN_IP_BUF->len[0], 2);
1235 compress_hdr_ipv6(rimeaddr_t *rime_destaddr)
1237 *rime_ptr = SICSLOWPAN_DISPATCH_IPV6;
1238 rime_hdr_len += SICSLOWPAN_IPV6_HDR_LEN;
1239 memcpy(rime_ptr + rime_hdr_len,
UIP_IP_BUF, UIP_IPH_LEN);
1240 rime_hdr_len += UIP_IPH_LEN;
1241 uncomp_hdr_len += UIP_IPH_LEN;
1254 packet_sent(
void *ptr,
int status,
int transmissions)
1256 #if SICSLOWPAN_CONF_NEIGHBOR_INFO
1267 send_packet(rimeaddr_t *dest)
1273 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, dest);
1276 #if SICSLOWPAN_CONF_ACK_ALL
1277 packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
1282 NETSTACK_MAC.send(&packet_sent,
NULL);
1286 watchdog_periodic();
1312 packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
1313 SICSLOWPAN_MAX_MAC_TRANSMISSIONS);
1315 #define TCP_FIN 0x01
1316 #define TCP_ACK 0x10
1317 #define TCP_CTL 0x3f
1320 (UIP_TCP_BUF->flags & TCP_FIN) == 0 &&
1321 (UIP_TCP_BUF->flags & TCP_CTL) != TCP_ACK) {
1322 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE,
1323 PACKETBUF_ATTR_PACKET_TYPE_STREAM);
1324 }
else if(
UIP_IP_BUF->proto == UIP_PROTO_TCP &&
1325 (UIP_TCP_BUF->flags & TCP_FIN) == TCP_FIN) {
1326 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE,
1327 PACKETBUF_ATTR_PACKET_TYPE_STREAM_END);
1335 if(localdest ==
NULL) {
1341 PRINTFO(
"sicslowpan output: sending packet len %d\n",
uip_len);
1345 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC1
1346 compress_hdr_hc1(&dest);
1348 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6
1349 compress_hdr_ipv6(&dest);
1351 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC06
1352 compress_hdr_hc06(&dest);
1355 compress_hdr_ipv6(&dest);
1357 PRINTFO(
"sicslowpan output: header of len %d\n", rime_hdr_len);
1360 #if SICSLOWPAN_CONF_FRAG
1370 PRINTFO(
"Fragmentation sending packet len %d\n",
uip_len);
1373 PRINTFO(
"sicslowpan output: 1rst fragment ");
1376 memmove(rime_ptr + SICSLOWPAN_FRAG1_HDR_LEN, rime_ptr, rime_hdr_len);
1384 SET16(RIME_FRAG_PTR, RIME_FRAG_DISPATCH_SIZE,
1385 ((SICSLOWPAN_DISPATCH_FRAG1 << 8) |
uip_len));
1387 SET16(RIME_FRAG_PTR, RIME_FRAG_TAG, my_tag);
1390 rime_hdr_len += SICSLOWPAN_FRAG1_HDR_LEN;
1392 PRINTFO(
"(len %d, tag %d)\n", rime_payload_len, my_tag);
1393 memcpy(rime_ptr + rime_hdr_len,
1394 (uint8_t *)
UIP_IP_BUF + uncomp_hdr_len, rime_payload_len);
1396 q = queuebuf_new_from_packetbuf();
1398 PRINTFO(
"could not allocate queuebuf for first fragment, dropping packet\n");
1402 queuebuf_to_packetbuf(q);
1407 processed_ip_len = rime_payload_len + uncomp_hdr_len;
1414 rime_hdr_len = SICSLOWPAN_FRAGN_HDR_LEN;
1417 SET16(RIME_FRAG_PTR, RIME_FRAG_DISPATCH_SIZE,
1418 ((SICSLOWPAN_DISPATCH_FRAGN << 8) |
uip_len));
1420 while(processed_ip_len <
uip_len) {
1421 PRINTFO(
"sicslowpan output: fragment ");
1422 RIME_FRAG_PTR[RIME_FRAG_OFFSET] = processed_ip_len >> 3;
1425 if(
uip_len - processed_ip_len < rime_payload_len) {
1427 rime_payload_len =
uip_len - processed_ip_len;
1429 PRINTFO(
"(offset %d, len %d, tag %d)\n",
1430 processed_ip_len >> 3, rime_payload_len, my_tag);
1431 memcpy(rime_ptr + rime_hdr_len,
1432 (uint8_t *)
UIP_IP_BUF + processed_ip_len, rime_payload_len);
1434 q = queuebuf_new_from_packetbuf();
1436 PRINTFO(
"could not allocate queuebuf, dropping fragment\n");
1440 queuebuf_to_packetbuf(q);
1443 processed_ip_len += rime_payload_len;
1448 processed_ip_len = 0;
1450 PRINTFO(
"sicslowpan output: Packet too large to be sent without fragmentation support; dropping packet\n");
1458 memcpy(rime_ptr + rime_hdr_len, (uint8_t *)
UIP_IP_BUF + uncomp_hdr_len,
1483 uint16_t frag_size = 0;
1485 uint8_t frag_offset = 0;
1486 #if SICSLOWPAN_CONF_FRAG
1488 uint16_t frag_tag = 0;
1489 uint8_t first_fragment = 0, last_fragment = 0;
1499 #if SICSLOWPAN_CONF_FRAG
1503 processed_ip_len = 0;
1509 switch((GET16(RIME_FRAG_PTR, RIME_FRAG_DISPATCH_SIZE) & 0xf800) >> 8) {
1510 case SICSLOWPAN_DISPATCH_FRAG1:
1511 PRINTFI(
"sicslowpan input: FRAG1 ");
1514 frag_size = GET16(RIME_FRAG_PTR, RIME_FRAG_DISPATCH_SIZE) & 0x07ff;
1516 frag_tag = GET16(RIME_FRAG_PTR, RIME_FRAG_TAG);
1517 PRINTFI(
"size %d, tag %d, offset %d)\n",
1518 frag_size, frag_tag, frag_offset);
1519 rime_hdr_len += SICSLOWPAN_FRAG1_HDR_LEN;
1523 case SICSLOWPAN_DISPATCH_FRAGN:
1528 PRINTFI(
"sicslowpan input: FRAGN ");
1529 frag_offset = RIME_FRAG_PTR[RIME_FRAG_OFFSET];
1530 frag_tag = GET16(RIME_FRAG_PTR, RIME_FRAG_TAG);
1531 frag_size = GET16(RIME_FRAG_PTR, RIME_FRAG_DISPATCH_SIZE) & 0x07ff;
1532 PRINTFI(
"size %d, tag %d, offset %d)\n",
1533 frag_size, frag_tag, frag_offset);
1534 rime_hdr_len += SICSLOWPAN_FRAGN_HDR_LEN;
1538 PRINTFI(
"last_fragment?: processed_ip_len %d rime_payload_len %d frag_size %d\n",
1549 if(processed_ip_len > 0) {
1552 if((frag_size > 0 &&
1553 (frag_size != sicslowpan_len ||
1554 reass_tag != frag_tag ||
1555 !
rimeaddr_cmp(&frag_sender, packetbuf_addr(PACKETBUF_ADDR_SENDER)))) ||
1561 PRINTFI(
"sicslowpan input: Dropping 6lowpan packet that is not a fragment of the packet currently being reassembled\n");
1570 sicslowpan_len = frag_size;
1571 reass_tag = frag_tag;
1573 PRINTFI(
"sicslowpan input: INIT FRAGMENTATION (len %d, tag %d)\n",
1574 sicslowpan_len, reass_tag);
1575 rimeaddr_copy(&frag_sender, packetbuf_addr(PACKETBUF_ADDR_SENDER));
1579 if(rime_hdr_len == SICSLOWPAN_FRAGN_HDR_LEN) {
1586 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC06
1587 if((RIME_HC1_PTR[RIME_HC1_DISPATCH] & 0xe0) == SICSLOWPAN_DISPATCH_IPHC) {
1588 PRINTFI(
"sicslowpan input: IPHC\n");
1589 uncompress_hdr_hc06(frag_size);
1592 switch(RIME_HC1_PTR[RIME_HC1_DISPATCH]) {
1593 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC1
1594 case SICSLOWPAN_DISPATCH_HC1:
1595 PRINTFI(
"sicslowpan input: HC1\n");
1596 uncompress_hdr_hc1(frag_size);
1599 case SICSLOWPAN_DISPATCH_IPV6:
1600 PRINTFI(
"sicslowpan input: IPV6\n");
1601 rime_hdr_len += SICSLOWPAN_IPV6_HDR_LEN;
1604 memcpy(SICSLOWPAN_IP_BUF, rime_ptr + rime_hdr_len, UIP_IPH_LEN);
1607 rime_hdr_len += UIP_IPH_LEN;
1608 uncomp_hdr_len += UIP_IPH_LEN;
1612 PRINTFI(
"sicslowpan input: unknown dispatch: %u\n",
1613 RIME_HC1_PTR[RIME_HC1_DISPATCH]);
1618 #if SICSLOWPAN_CONF_FRAG
1629 PRINTF(
"SICSLOWPAN: packet dropped due to header > total packet\n");
1633 memcpy((uint8_t *)SICSLOWPAN_IP_BUF + uncomp_hdr_len + (uint16_t)(frag_offset << 3), rime_ptr + rime_hdr_len, rime_payload_len);
1637 #if SICSLOWPAN_CONF_FRAG
1640 if(first_fragment != 0) {
1641 processed_ip_len += uncomp_hdr_len;
1645 if(last_fragment != 0) {
1646 processed_ip_len = frag_size;
1648 processed_ip_len += rime_payload_len;
1650 PRINTF(
"processed_ip_len %d, rime_payload_len %d\n", processed_ip_len, rime_payload_len);
1654 sicslowpan_len = rime_payload_len + uncomp_hdr_len;
1655 #if SICSLOWPAN_CONF_FRAG
1662 PRINTF(
"sicslowpan_init processed_ip_len %d, sicslowpan_len %d\n",
1663 processed_ip_len, sicslowpan_len);
1664 if(processed_ip_len == 0 || (processed_ip_len == sicslowpan_len)) {
1665 PRINTFI(
"sicslowpan input: IP packet ready (length %d)\n",
1667 memcpy((uint8_t *)
UIP_IP_BUF, (uint8_t *)SICSLOWPAN_IP_BUF, sicslowpan_len);
1670 processed_ip_len = 0;
1676 PRINTF(
"after decompression: ");
1677 for (tmp = 0; tmp < SICSLOWPAN_IP_BUF->len[1] + 40; tmp++) {
1678 uint8_t data = ((uint8_t *) (SICSLOWPAN_IP_BUF))[tmp];
1679 PRINTF(
"%02x", data);
1685 #if SICSLOWPAN_CONF_NEIGHBOR_INFO
1690 #if SICSLOWPAN_CONF_FRAG
1700 sicslowpan_init(
void)
1703 sicslowpan_mac = &NETSTACK_MAC;
1709 tcpip_set_outputfunc(output);
1711 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_HC06
1717 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
1718 addr_contexts[0].used = 1;
1719 addr_contexts[0].number = 0;
1720 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_0
1721 SICSLOWPAN_CONF_ADDR_CONTEXT_0;
1723 addr_contexts[0].prefix[0] = 0xaa;
1724 addr_contexts[0].prefix[1] = 0xaa;
1728 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 1
1732 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_1
1734 addr_contexts[1].used = 1;
1735 addr_contexts[1].number = 1;
1736 SICSLOWPAN_CONF_ADDR_CONTEXT_1;
1737 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_2
1739 addr_contexts[2].used = 1;
1740 addr_contexts[2].number = 2;
1741 SICSLOWPAN_CONF_ADDR_CONTEXT_2;
1744 addr_contexts[i].used = 0;
1747 addr_contexts[i].used = 0;