57 #ifndef UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
58 #define UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS 0
59 #endif // ifndef UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
61 #ifndef UIP_CONF_AUTO_SUBSTITUTE_LOCAL_MAC_ADDR
62 #define UIP_CONF_AUTO_SUBSTITUTE_LOCAL_MAC_ADDR 1
63 #endif // ifndef UIP_CONF_AUTO_SUBSTITUTE_LOCAL_MAC_ADDR
65 #if UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
231 #include "sicslow_ethernet.h"
246 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
251 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
252 #define ETHBUF(x) ((struct uip_eth_hdr *)x)
256 #define LSB(u16) (((uint8_t *)&(u16))[0])
257 #define MSB(u16) (((uint8_t *)&(u16))[1])
260 static const uint64_t simple_trans_ethernet_addr = 0x3E3D3C3B3AF2ULL;
262 #if UIP_CONF_IPV6_RPL
266 extern uint64_t usb_ethernet_addr;
271 extern void (*pinput)(
const struct mac_driver *r);
272 void (*sicslowinput)(
const struct mac_driver *r);
273 parsed_frame_t * parsed_frame;
275 usbstick_mode_t usbstick_mode;
280 void mac_ethhijack_nondata(
const struct mac_driver *r);
283 extern void (*sicslowmac_snifferhook)(
const struct mac_driver *r);
287 #define TRANSLATE_BIT_MASK (1<<2)
289 #define LOCAL_BIT_MASK (1<<1)
291 #define MULTICAST_BIT_MASK (1<<0)
293 #define PREFIX_BUFFER_SIZE 32
295 uint8_t prefixCounter;
296 uint8_t prefixBuffer[PREFIX_BUFFER_SIZE][3];
307 void mac_ethernetSetup(
void)
309 usbstick_mode.sicslowpan = 1;
310 usbstick_mode.sendToRf = 1;
311 usbstick_mode.translate = 1;
312 usbstick_mode.debugOn= 1;
313 usbstick_mode.raw = 0;
314 usbstick_mode.sneeze=0;
317 sicslowinput = pinput;
319 pmac = sicslowmac_get_driver();
321 sicslowmac_snifferhook = mac_ethhijack_nondata;
336 PRINTF(
"Packet type: 0x%04x\n\r", uip_ntohs(((
struct uip_eth_hdr *) ethHeader)->type));
344 if ((usbstick_mode.sendToRf == 0) || (usbstick_mode.sneeze != 0)) {
352 PRINTF(
"eth2low: Dropping packet w/type=0x%04x\n",uip_ntohs(((
struct uip_eth_hdr *) ethHeader)->type));
355 usb_eth_stat.txbad++;
362 if ( (((
struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0x33) &&
363 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0x33) )
365 PRINTF(
"eth2low: Ethernet multicast packet received\n\r");
367 }
else if ( (((
struct uip_eth_hdr *) ethHeader)->dest.addr[0] == 0xFF) &&
368 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[1] == 0xFF) &&
369 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[2] == 0xFF) &&
370 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[3] == 0xFF) &&
371 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[4] == 0xFF) &&
372 (((
struct uip_eth_hdr *) ethHeader)->dest.addr[5] == 0xFF) ) {
374 PRINTF(
"eth2low: Dropping broadcast packet\n\r");
376 usb_eth_stat.txbad++;
383 if(memcmp((uint8_t *)&simple_trans_ethernet_addr, &(((
struct uip_eth_hdr *) ethHeader)->dest.addr[0]), 6) == 0) {
386 destAddr.addr[0] =
UIP_IP_BUF->destipaddr.u8[8] ^ 0x02;
387 destAddr.addr[1] =
UIP_IP_BUF->destipaddr.u8[9];
388 destAddr.addr[2] =
UIP_IP_BUF->destipaddr.u8[10];
389 destAddr.addr[3] =
UIP_IP_BUF->destipaddr.u8[11];
390 destAddr.addr[4] =
UIP_IP_BUF->destipaddr.u8[12];
391 destAddr.addr[5] =
UIP_IP_BUF->destipaddr.u8[13];
392 destAddr.addr[6] =
UIP_IP_BUF->destipaddr.u8[14];
393 destAddr.addr[7] =
UIP_IP_BUF->destipaddr.u8[15];
396 destAddr.addr[0] =
UIP_IP_BUF->destipaddr.u8[0] ^ 0x02;
397 destAddr.addr[1] =
UIP_IP_BUF->destipaddr.u8[1];
398 destAddr.addr[2] =
UIP_IP_BUF->destipaddr.u8[2];
399 destAddr.addr[3] =
UIP_IP_BUF->destipaddr.u8[3];
400 destAddr.addr[4] =
UIP_IP_BUF->destipaddr.u8[0];
401 destAddr.addr[5] =
UIP_IP_BUF->destipaddr.u8[1];
402 destAddr.addr[6] =
UIP_IP_BUF->destipaddr.u8[2];
403 destAddr.addr[7] =
UIP_IP_BUF->destipaddr.u8[3];
407 destAddrPtr = &destAddr;
409 #if UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
418 PRINTF(
"eth2low: Addressed packet received... ");
421 PRINTF(
" translation failed\n\r");
423 usb_eth_stat.txbad++;
428 PRINTF(
" translated OK\n\r");
429 destAddrPtr = &destAddr;
439 if (usbstick_mode.translate) {
442 PRINTF(
"IPTranslation: returns %d\n\r", transReturn);
450 #if UIP_CONF_IPV6_RPL
478 parsed_frame = sicslowmac_get_frame();
482 ETHBUF(uip_buf)->type =
uip_htons(UIP_ETHTYPE_IPV6);
490 if( ( parsed_frame->fcf->destAddrMode == SHORTADDRMODE) &&
491 ( parsed_frame->dest_addr->addr16 == 0xffff) ) {
493 ETHBUF(uip_buf)->dest.addr[0] = 0x33;
494 ETHBUF(uip_buf)->dest.addr[1] = 0x33;
497 ETHBUF(uip_buf)->dest.addr[2] =
UIP_IP_BUF->destipaddr.u8[12];
498 ETHBUF(uip_buf)->dest.addr[3] =
UIP_IP_BUF->destipaddr.u8[13];
499 ETHBUF(uip_buf)->dest.addr[4] =
UIP_IP_BUF->destipaddr.u8[14];
500 ETHBUF(uip_buf)->dest.addr[5] =
UIP_IP_BUF->destipaddr.u8[15];
503 ETHBUF(uip_buf)->dest.addr[2] =
UIP_IP_BUF->destipaddr.u8[0];
504 ETHBUF(uip_buf)->dest.addr[3] =
UIP_IP_BUF->destipaddr.u8[1];
505 ETHBUF(uip_buf)->dest.addr[4] =
UIP_IP_BUF->destipaddr.u8[2];
506 ETHBUF(uip_buf)->dest.addr[5] =
UIP_IP_BUF->destipaddr.u8[3];
511 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
514 #if !UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
517 (uint8_t *) &(ETHBUF(uip_buf)->src.addr[0]),
527 if (usbstick_mode.translate) {
532 #if UIP_CONF_IPV6_RPL
535 PRINTF(
"siclow_ethernet: Destination off-link but no route\n");
541 PRINTF(
"Low2Eth: Sending packet to ethernet\n\r");
566 #if UIP_LLADDR_LEN == 8
568 PRINTF(
"eth2low: ICMP Message detected\n\r");
587 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN])
588 #define UIP_ICMP_OPTS(x) ((icmp_opts_t *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN + x])
590 void slide(uint8_t * data, uint8_t length, int16_t
slide);
605 uint16_t icmp_opt_offset = 0;
620 icmp_opt_offset = 24;
628 icmp_opt_offset = 16;
632 icmp_opt_offset = 40;
650 len -= icmp_opt_offset;
653 if (len < 8)
return -2;
659 if (((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_SLLAO) ||
660 ((UIP_ICMP_OPTS(icmp_opt_offset)->type) == UIP_ND6_OPT_TLLAO) ) {
664 for(i = 0; i < (UIP_ICMP_OPTS(icmp_opt_offset)->length*8 - 2); i++) {
665 llbuf[i] = UIP_ICMP_OPTS(icmp_opt_offset)->data[i];
669 if (target == ll_802154_type) {
672 slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 6, len - 6, sizechange);
673 }
else if (target == ll_8023_type) {
681 slide(UIP_ICMP_OPTS(icmp_opt_offset)->data + 14, len - 14, sizechange);
687 if (target == ll_802154_type) {
690 #if !UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
697 if (target == ll_802154_type) {
698 UIP_ICMP_OPTS(icmp_opt_offset)->length = 2;
700 UIP_ICMP_OPTS(icmp_opt_offset)->length = 1;
715 #if UIP_CONF_IPV6 //allow non ipv6 builds
720 len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length;
721 icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length;
725 len -= 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length;
728 if (UIP_ICMP_OPTS(icmp_opt_offset)->length == 0) {
729 PRINTF(
"Option in ND packet has length zero, error?\n\r");
733 icmp_opt_offset += 8 * UIP_ICMP_OPTS(icmp_opt_offset)->length;
751 #if UIP_CONF_AUTO_SUBSTITUTE_LOCAL_MAC_ADDR
754 if (memcmp((uint8_t *)&usb_ethernet_addr, ethernet, 6) == 0)
756 memcpy((uint8_t *)lowpan, &
macLongAddr, UIP_LLADDR_LEN);
761 #if UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
764 lowpan->addr[0] = ethernet[0];
765 lowpan->addr[1] = ethernet[1];
766 lowpan->addr[2] = ethernet[2];
767 lowpan->addr[3] = 0xff;
768 lowpan->addr[4] = 0xfe;
769 lowpan->addr[5] = ethernet[3];
770 lowpan->addr[6] = ethernet[4];
771 lowpan->addr[7] = ethernet[5];
777 #if UIP_LLADDR_LEN == 8
781 index = ethernet[0] >> 3;
784 lowpan->addr[0] = prefixBuffer[index][0];
785 lowpan->addr[1] = prefixBuffer[index][1];
786 lowpan->addr[2] = prefixBuffer[index][2];
787 lowpan->addr[3] = ethernet[1];
788 lowpan->addr[4] = ethernet[2];
791 if (index >= prefixCounter)
794 lowpan->addr[0] = ethernet[0];
795 lowpan->addr[1] = ethernet[1];
796 lowpan->addr[2] = ethernet[2];
797 lowpan->addr[3] = 0xff;
798 lowpan->addr[4] = 0xfe;
801 lowpan->addr[5] = ethernet[3];
802 lowpan->addr[6] = ethernet[4];
803 lowpan->addr[7] = ethernet[5];
805 #else //UIP_LLADDR != 8
808 for(i = 0; i < UIP_LLADDR_LEN; i++) {
809 lowpan->addr[i] = ethernet[i];
811 #endif //UIP_LLADDR == 8
813 #endif //UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
826 #if UIP_CONF_AUTO_SUBSTITUTE_LOCAL_MAC_ADDR
829 if (memcmp((uint8_t *)&
macLongAddr, (uint8_t *)lowpan, UIP_LLADDR_LEN) == 0) {
830 usb_eth_get_mac_address(ethernet);
835 #if UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
838 ethernet[0] = lowpan->addr[0];
839 ethernet[1] = lowpan->addr[1];
840 ethernet[2] = lowpan->addr[2];
841 ethernet[3] = lowpan->addr[5];
842 ethernet[4] = lowpan->addr[6];
843 ethernet[5] = lowpan->addr[7];
850 #if UIP_LLADDR_LEN == 8
853 if ((lowpan->addr[3] == 0xff) && (lowpan->addr[4] == 0xfe) &&
859 ethernet[0] = lowpan->addr[0];
860 ethernet[1] = lowpan->addr[1];
861 ethernet[2] = lowpan->addr[2];
862 ethernet[3] = lowpan->addr[5];
863 ethernet[4] = lowpan->addr[6];
864 ethernet[5] = lowpan->addr[7];
870 for (i = 0; i < prefixCounter; i++) {
872 if ((lowpan->addr[0] == prefixBuffer[i][0]) &&
873 (lowpan->addr[1] == prefixBuffer[i][1]) &&
874 (lowpan->addr[2] == prefixBuffer[i][2])) {
880 if (index >= PREFIX_BUFFER_SIZE) {
883 ethernet[0] = lowpan->addr[0];
884 ethernet[1] = lowpan->addr[1];
885 ethernet[2] = lowpan->addr[2];
886 ethernet[3] = lowpan->addr[5];
887 ethernet[4] = lowpan->addr[6];
888 ethernet[5] = lowpan->addr[7];
892 if (index == prefixCounter) {
894 prefixBuffer[index][0] = lowpan->addr[0];
895 prefixBuffer[index][1] = lowpan->addr[1];
896 prefixBuffer[index][2] = lowpan->addr[2];
901 ethernet[1] = lowpan->addr[3];
902 ethernet[2] = lowpan->addr[4];
903 ethernet[3] = lowpan->addr[5];
904 ethernet[4] = lowpan->addr[6];
905 ethernet[5] = lowpan->addr[7];
909 #else //UIP_LLADDR_LEN != 8
912 for(i = 0; i < UIP_LLADDR_LEN; i++) {
913 ethernet[i] = lowpan->addr[i];
915 #endif //UIP_LLADDR_LEN == 8
917 #endif //UIP_CONF_SIMPLE_JACKDAW_ADDR_TRANS
927 memcpy(ethernet, &simple_trans_ethernet_addr, 6);
951 *(data + length +
slide) = *(data + length);
955 *(data + slide + i) = *(data + i);
966 mac_log_802_15_4_tx(
const uint8_t* buffer,
size_t total_len) {
967 if (usbstick_mode.raw != 0) {
977 ETHBUF(raw_buf)->type =
uip_htons(0x809A);
981 ETHBUF(raw_buf)->dest.addr[0] = 0x33;
982 ETHBUF(raw_buf)->dest.addr[1] = 0x33;
983 ETHBUF(raw_buf)->dest.addr[2] = 0x00;
984 ETHBUF(raw_buf)->dest.addr[3] = 0x00;
985 ETHBUF(raw_buf)->dest.addr[4] = 0x80;
986 ETHBUF(raw_buf)->dest.addr[5] = 0x9A;
997 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
1009 mac_log_802_15_4_rx(
const uint8_t* buf,
size_t len) {
1010 if (usbstick_mode.raw != 0) {
1018 ETHBUF(raw_buf)->type =
uip_htons(0x809A);
1022 ETHBUF(raw_buf)->dest.addr[0] = 0x33;
1023 ETHBUF(raw_buf)->dest.addr[1] = 0x33;
1024 ETHBUF(raw_buf)->dest.addr[2] = 0x00;
1025 ETHBUF(raw_buf)->dest.addr[3] = 0x00;
1026 ETHBUF(raw_buf)->dest.addr[4] = 0x80;
1027 ETHBUF(raw_buf)->dest.addr[5] = 0x9A;
1037 (
uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
1049 mac_is_send_enabled(
void) {
1050 if ((usbstick_mode.sendToRf == 0) || (usbstick_mode.sneeze != 0))
return 0;
1072 if (usbstick_mode.raw) {
1076 if (usbstick_mode.sicslowpan) {
1078 #if UIP_CONF_USE_RUM
1079 if (parsed_frame->payload[4]) {
1082 #if UIP_CONF_USE_RUM
1091 void mac_ethhijack_nondata(
const struct mac_driver *r)
1093 if (usbstick_mode.raw)
1107 mac_log_802_15_4_tx(frame_result->
frame, frame_result->
length);
1119 mac_log_802_15_4_tx(radio_frame_data(), radio_frame_length());