static uint16_t _get_l2src(uint8_t *l2src, size_t l2src_size, kernel_pid_t iface) { bool try_long = false; int res; uint16_t l2src_len; /* maximum address length that fits into a minimum length (8) S/TL2A option */ const uint16_t max_short_len = 6; /* try getting source address */ if ((ng_netapi_get(iface, NG_NETOPT_SRC_LEN, 0, &l2src_len, sizeof(l2src_len)) >= 0) && (l2src_len > max_short_len)) { try_long = true; } if (try_long && ((res = ng_netapi_get(iface, NG_NETOPT_ADDRESS_LONG, 0, l2src, l2src_size)) > max_short_len)) { l2src_len = (uint16_t)res; } else if ((res = ng_netapi_get(iface, NG_NETOPT_ADDRESS, 0, l2src, l2src_size)) >= 0) { l2src_len = (uint16_t)res; } else { DEBUG("ndp: no link-layer address found.\n"); l2src_len = 0; } return l2src_len; }
static uint16_t _get_l2src(uint8_t *l2src, size_t l2src_size, kernel_pid_t iface) { bool try_long = false; int res; uint16_t l2src_len; /* try getting source address */ if ((ng_netapi_get(iface, NETCONF_OPT_SRC_LEN, 0, &l2src_len, sizeof(l2src_len)) >= 0) && (l2src_len == 8)) { try_long = true; } if ((try_long && ((res = ng_netapi_get(iface, NETCONF_OPT_ADDRESS_LONG, 0, l2src, l2src_size)) < 0)) || ((res = ng_netapi_get(iface, NETCONF_OPT_ADDRESS, 0, l2src, l2src_size)) < 0)) { DEBUG("ndp: no link-layer address found.\n"); l2src_len = 0; } else { l2src_len = (uint16_t)res; } return l2src_len; }
void ng_ipv6_netif_init_by_dev(void) { kernel_pid_t ifs[NG_NETIF_NUMOF]; size_t ifnum = ng_netif_get(ifs); for (size_t i = 0; i < ifnum; i++) { ng_ipv6_addr_t addr; eui64_t iid; ng_ipv6_netif_t *ipv6_if = ng_ipv6_netif_get(ifs[i]); if (ipv6_if == NULL) { continue; } mutex_lock(&ipv6_if->mutex); #ifdef MODULE_NG_SIXLOWPAN ng_nettype_t if_type = NG_NETTYPE_UNDEF; if ((ng_netapi_get(ifs[i], NETCONF_OPT_PROTO, 0, &if_type, sizeof(if_type)) != -ENOTSUP) && (if_type == NG_NETTYPE_SIXLOWPAN)) { uint16_t src_len = 8; DEBUG("Set 6LoWPAN flag\n"); ipv6_ifs[i].flags |= NG_IPV6_NETIF_FLAGS_SIXLOWPAN; /* use EUI-64 (8-byte address) for IID generation and for sending * packets */ ng_netapi_set(ifs[i], NETCONF_OPT_SRC_LEN, 0, &src_len, sizeof(src_len)); /* don't care for result */ } #endif if ((ng_netapi_get(ifs[i], NETCONF_OPT_IPV6_IID, 0, &iid, sizeof(eui64_t)) < 0)) { mutex_unlock(&ipv6_if->mutex); continue; } ng_ipv6_addr_set_aiid(&addr, iid.uint8); ng_ipv6_addr_set_link_local_prefix(&addr); _add_addr_to_entry(ipv6_if, &addr, 64, 0); mutex_unlock(&ipv6_if->mutex); } }
ng_nettest_res_t ng_nettest_set(kernel_pid_t pid, ng_netconf_opt_t opt, uint16_t context, void *data, size_t data_len, int exp_res) { if (exp_res != ng_netapi_get(pid, opt, context, data, data_len)) { return NG_NETTEST_FAIL; } return NG_NETTEST_SUCCESS; }
static void _netif_list(kernel_pid_t dev) { uint8_t hwaddr[MAX_ADDR_LEN]; uint16_t u16; int16_t i16; int res; ng_netconf_state_t state; ng_netconf_enable_t enable; bool linebreak = false; #ifdef MODULE_NG_IPV6_NETIF ng_ipv6_netif_t *entry = ng_ipv6_netif_get(dev); char ipv6_addr[NG_IPV6_ADDR_MAX_STR_LEN]; #endif printf("Iface %2d ", dev); res = ng_netapi_get(dev, NETCONF_OPT_ADDRESS, 0, hwaddr, sizeof(hwaddr)); if (res >= 0) { char hwaddr_str[res * 3]; printf(" HWaddr: "); printf("%s", ng_netif_addr_to_str(hwaddr_str, sizeof(hwaddr_str), hwaddr, res)); printf(" "); } res = ng_netapi_get(dev, NETCONF_OPT_CHANNEL, 0, &u16, sizeof(u16)); if (res >= 0) { printf(" Channel: %" PRIu16 " ", u16); } res = ng_netapi_get(dev, NETCONF_OPT_NID, 0, &u16, sizeof(u16)); if (res >= 0) { printf(" NID: 0x%" PRIx16 " ", u16); } res = ng_netapi_get(dev, NETCONF_OPT_TX_POWER, 0, &i16, sizeof(i16)); if (res >= 0) { printf(" TX-Power: %" PRIi16 "dBm ", i16); } res = ng_netapi_get(dev, NETCONF_OPT_STATE, 0, &state, sizeof(state)); if (res >= 0) { printf(" State: "); _print_netconf_state(state); } printf("\n "); res = ng_netapi_get(dev, NETCONF_OPT_ADDRESS_LONG, 0, hwaddr, sizeof(hwaddr)); if (res >= 0) { char hwaddr_str[res * 3]; printf("Long HWaddr: "); printf("%s", ng_netif_addr_to_str(hwaddr_str, sizeof(hwaddr_str), hwaddr, res)); printf("\n "); } res = ng_netapi_get(dev, NETCONF_OPT_PROMISCUOUSMODE, 0, &enable, sizeof(enable)); if ((res >= 0) && (enable == NETCONF_ENABLE)) { printf("PROMISC "); linebreak = true; } res = ng_netapi_get(dev, NETCONF_OPT_AUTOACK, 0, &enable, sizeof(enable)); if ((res >= 0) && (enable == NETCONF_ENABLE)) { printf("AUTOACK "); linebreak = true; } res = ng_netapi_get(dev, NETCONF_OPT_PRELOADING, 0, &enable, sizeof(enable)); if ((res >= 0) && (enable == NETCONF_ENABLE)) { printf("PRELOAD "); linebreak = true; } res = ng_netapi_get(dev, NETCONF_OPT_RAWMODE, 0, &enable, sizeof(enable)); if ((res >= 0) && (enable == NETCONF_ENABLE)) { printf("RAWMODE "); linebreak = true; } #ifdef MODULE_NG_IPV6_NETIF if ((entry != NULL) && (entry->flags & NG_IPV6_NETIF_FLAGS_SIXLOWPAN)) { printf("6LO "); linebreak = true; } #endif if (linebreak) { printf("\n "); } res = ng_netapi_get(dev, NETCONF_OPT_SRC_LEN, 0, &u16, sizeof(u16)); if (res >= 0) { printf("Source address length: %" PRIu16 "\n ", u16); } #ifdef MODULE_NG_IPV6_NETIF for (int i = 0; i < NG_IPV6_NETIF_ADDR_NUMOF; i++) { if (!ng_ipv6_addr_is_unspecified(&entry->addrs[i].addr)) { printf("inet6 addr: "); if (ng_ipv6_addr_to_str(ipv6_addr, &entry->addrs[i].addr, NG_IPV6_ADDR_MAX_STR_LEN)) { printf("%s/%" PRIu8 " scope: ", ipv6_addr, entry->addrs[i].prefix_len); if ((ng_ipv6_addr_is_link_local(&entry->addrs[i].addr))) { printf("local"); } else { printf("global"); } if (entry->addrs[i].flags & NG_IPV6_NETIF_ADDR_FLAGS_NON_UNICAST) { if (ng_ipv6_addr_is_multicast(&entry->addrs[i].addr)) { printf(" [multicast]"); } else { printf(" [anycast]"); } } } else { printf("error in conversion"); } printf("\n "); } } #endif puts(""); }
static void _send(ng_pktsnip_t *pkt) { ng_netif_hdr_t *hdr; ng_pktsnip_t *ipv6, *sixlowpan; ng_sixlowpan_netif_t *iface; /* cppcheck: datagram_size will be read by frag */ /* cppcheck-suppress unreadVariable */ size_t payload_len, datagram_size; uint16_t max_frag_size; /* cppcheck: disp is needed in other build paths on this level already */ /* cppcheck-suppress variableScope */ uint8_t *disp; if ((pkt == NULL) || (pkt->size < sizeof(ng_netif_hdr_t))) { DEBUG("6lo: Sending packet has no netif header\n"); ng_pktbuf_release(pkt); return; } hdr = pkt->data; ipv6 = pkt->next; if ((ipv6 == NULL) || (ipv6->type != NG_NETTYPE_IPV6)) { DEBUG("6lo: Sending packet has no IPv6 header\n"); ng_pktbuf_release(pkt); return; } /* payload length and datagram size are different in that the payload * length is the length of the IPv6 datagram + 6LoWPAN dispatches, * while the datagram size is the size of only the IPv6 datagram */ payload_len = ng_pkt_len(ipv6); /* cppcheck: datagram_size will be read by ng_sixlowpan_frag implementation */ /* cppcheck-suppress unreadVariable */ datagram_size = (uint16_t)payload_len; /* use sixlowpan packet snip as temporary one */ sixlowpan = ng_pktbuf_start_write(pkt); if (sixlowpan == NULL) { DEBUG("6lo: no space left in packet buffer\n"); ng_pktbuf_release(pkt); return; } pkt = sixlowpan; iface = ng_sixlowpan_netif_get(hdr->if_pid); if (iface == NULL) { if (ng_netapi_get(hdr->if_pid, NETCONF_OPT_MAX_PACKET_SIZE, 0, &max_frag_size, sizeof(max_frag_size)) < 0) { /* if error we assume it works */ DEBUG("6lo: can not get max packet size from interface %" PRIkernel_pid "\n", hdr->if_pid); max_frag_size = UINT16_MAX; } ng_sixlowpan_netif_add(hdr->if_pid, max_frag_size); iface = ng_sixlowpan_netif_get(hdr->if_pid); } else { max_frag_size = iface->max_frag_size; } #ifdef MODULE_NG_SIXLOWPAN_IPHC if (iface->iphc_enabled) { if (!ng_sixlowpan_iphc_encode(pkt)) { DEBUG("6lo: error on IPHC encoding\n"); ng_pktbuf_release(pkt); return; } } else { DEBUG("6lo: Send uncompressed\n"); sixlowpan = ng_pktbuf_add(NULL, NULL, sizeof(uint8_t), NG_NETTYPE_SIXLOWPAN); if (sixlowpan == NULL) { DEBUG("6lo: no space left in packet buffer\n"); ng_pktbuf_release(pkt); return; } sixlowpan->next = ipv6; pkt->next = sixlowpan; disp = sixlowpan->data; disp[0] = NG_SIXLOWPAN_UNCOMPRESSED; payload_len++; } #else DEBUG("6lo: Send uncompressed\n"); sixlowpan = ng_pktbuf_add(NULL, NULL, sizeof(uint8_t), NG_NETTYPE_SIXLOWPAN); if (sixlowpan == NULL) { DEBUG("6lo: no space left in packet buffer\n"); ng_pktbuf_release(pkt); return; } sixlowpan->next = ipv6; pkt->next = sixlowpan; disp = sixlowpan->data; disp[0] = NG_SIXLOWPAN_UNCOMPRESSED; payload_len++; #endif DEBUG("6lo: max_frag_size = %" PRIu16 " for interface %" PRIkernel_pid "\n", max_frag_size, hdr->if_pid); /* IP should not send anything here if it is not a 6LoWPAN interface, * so we don't need to check for NULL pointers */ if (payload_len <= max_frag_size) { DEBUG("6lo: Send SND command for %p to %" PRIu16 "\n", (void *)pkt, hdr->if_pid); ng_netapi_send(hdr->if_pid, pkt); return; } #ifdef MODULE_NG_SIXLOWPAN_FRAG else { DEBUG("6lo: Send fragmented (%u > %" PRIu16 ")\n", (unsigned int)payload_len, max_frag_size); ng_sixlowpan_frag_send(hdr->if_pid, pkt, payload_len, datagram_size); } #else (void)datagram_size; DEBUG("6lo: packet too big (%u> %" PRIu16 ")\n", (unsigned int)payload_len, max_frag_size); #endif }