static void test_ipv6_netif_remove__not_allocated(void) { test_ipv6_netif_add__success(); /* adds DEFAULT_TEST_NETIF as interface */ ng_ipv6_netif_remove(OTHER_TEST_NETIF); TEST_ASSERT_NULL(ng_ipv6_netif_get(OTHER_TEST_NETIF)); TEST_ASSERT_NOT_NULL(ng_ipv6_netif_get(DEFAULT_TEST_NETIF)); }
static void _send_to_iface(kernel_pid_t iface, ng_pktsnip_t *pkt) { ng_ipv6_netif_t *if_entry = ng_ipv6_netif_get(iface); ((ng_netif_hdr_t *)pkt->data)->if_pid = iface; if ((if_entry != NULL) && (if_entry->flags & NG_IPV6_NETIF_FLAGS_SIXLOWPAN)) { DEBUG("ipv6: send to 6LoWPAN instead\n"); ng_netreg_entry_t *reg = ng_netreg_lookup(NG_NETTYPE_SIXLOWPAN, NG_NETREG_DEMUX_CTX_ALL); if (reg != NULL) { ng_pktbuf_hold(pkt, ng_netreg_num(NG_NETTYPE_SIXLOWPAN, NG_NETREG_DEMUX_CTX_ALL) - 1); } else { DEBUG("ipv6: no 6LoWPAN thread found"); } while (reg) { ng_netapi_send(reg->pid, pkt); reg = ng_netreg_getnext(reg); } } else { ng_netapi_send(iface, pkt); } }
void ng_ndp_retrans_nbr_sol(ng_ipv6_nc_t *nc_entry) { if ((ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_INCOMPLETE) || (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_PROBE)) { if (nc_entry->probes_remaining > 1) { ng_ipv6_addr_t dst; DEBUG("ndp: Retransmit neighbor solicitation for %s\n", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str))); /* retransmit neighbor solicatation */ if (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_INCOMPLETE) { ng_ipv6_addr_set_solicited_nodes(&dst, &nc_entry->ipv6_addr); } else { dst.u64[0] = nc_entry->ipv6_addr.u64[0]; dst.u64[1] = nc_entry->ipv6_addr.u64[1]; } nc_entry->probes_remaining--; if (nc_entry->iface == KERNEL_PID_UNDEF) { timex_t t = { 0, NG_NDP_RETRANS_TIMER }; kernel_pid_t ifs[NG_NETIF_NUMOF]; size_t ifnum = ng_netif_get(ifs); for (size_t i = 0; i < ifnum; i++) { _send_nbr_sol(ifs[i], &nc_entry->ipv6_addr, &dst); } vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, t, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); } else { ng_ipv6_netif_t *ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); _send_nbr_sol(nc_entry->iface, &nc_entry->ipv6_addr, &dst); mutex_lock(&ipv6_iface->mutex); vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, ipv6_iface->retrans_timer, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); mutex_unlock(&ipv6_iface->mutex); } } else if (nc_entry->probes_remaining <= 1) { DEBUG("ndp: Remove nc entry %s for interface %" PRIkernel_pid "\n", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str)), nc_entry->iface); #ifdef MODULE_FIB fib_remove_entry((uint8_t *) & (nc_entry->ipv6_addr), sizeof(ng_ipv6_addr_t)); #endif ng_ipv6_nc_remove(nc_entry->iface, &nc_entry->ipv6_addr); } } }
static void test_ipv6_netif_add__success(void) { ng_ipv6_netif_t *entry; ng_ipv6_netif_add(DEFAULT_TEST_NETIF); TEST_ASSERT_NOT_NULL((entry = ng_ipv6_netif_get(DEFAULT_TEST_NETIF))); TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_NETIF, entry->pid); }
static int _netif_flag(char *cmd, kernel_pid_t dev, char *flag) { ng_netconf_enable_t set = NETCONF_ENABLE; if (flag[0] == '-') { set = NETCONF_DISABLE; flag++; } if (strcmp(flag, "promisc") == 0) { return _netif_set_flag(dev, NETCONF_OPT_PROMISCUOUSMODE, set); } else if (strcmp(flag, "preload") == 0) { return _netif_set_flag(dev, NETCONF_OPT_PRELOADING, set); } else if (strcmp(flag, "autoack") == 0) { return _netif_set_flag(dev, NETCONF_OPT_AUTOACK, set); } else if (strcmp(flag, "raw") == 0) { return _netif_set_flag(dev, NETCONF_OPT_RAWMODE, set); } else if (strcmp(flag, "6lo") == 0) { #ifdef MODULE_NG_IPV6_NETIF ng_ipv6_netif_t *entry = ng_ipv6_netif_get(dev); if (entry == NULL) { puts("error: unable to (un)set 6LoWPAN support"); return 1; } mutex_lock(&entry->mutex); if (set) { entry->flags |= NG_IPV6_NETIF_FLAGS_SIXLOWPAN; printf("success: set 6LoWPAN support on interface %" PRIkernel_pid "\n", dev); } else { entry->flags &= ~NG_IPV6_NETIF_FLAGS_SIXLOWPAN; printf("success: unset 6LoWPAN support on interface %" PRIkernel_pid "\n", dev); } mutex_unlock(&entry->mutex); return 0; #else puts("error: unable to (un)set 6LoWPAN support"); return 1; #endif } _flag_usage(cmd); return 1; }
static void _set_state(ng_ipv6_nc_t *nc_entry, uint8_t state) { ng_ipv6_netif_t *ipv6_iface; timex_t t = { NG_NDP_FIRST_PROBE_DELAY, 0 }; nc_entry->flags &= ~NG_IPV6_NC_STATE_MASK; nc_entry->flags |= state; switch (state) { case NG_IPV6_NC_STATE_REACHABLE: ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); t = ipv6_iface->reach_time; /* we intentionally fall through here to set the desired timeout t */ case NG_IPV6_NC_STATE_DELAY: vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, t, ng_ipv6_pid, NG_NDP_MSG_NC_STATE_TIMEOUT, nc_entry); break; case NG_IPV6_NC_STATE_PROBE: ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); nc_entry->probes_remaining = NG_NDP_MAX_UC_NBR_SOL_NUMOF; _send_nbr_sol(nc_entry->iface, &nc_entry->ipv6_addr, &nc_entry->ipv6_addr); mutex_lock(&ipv6_iface->mutex); vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, ipv6_iface->retrans_timer, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); mutex_unlock(&ipv6_iface->mutex); break; default: break; } }
void ng_ipv6_netif_reset_addr(kernel_pid_t pid) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); if (entry == NULL) { return; } mutex_lock(&entry->mutex); _reset_addr_from_entry(entry); mutex_unlock(&entry->mutex); }
static void test_netif_add__success_with_ipv6(void) { kernel_pid_t *pids; size_t pid_num; ng_ipv6_netif_t *entry; ng_ipv6_addr_t exp_addr = NG_IPV6_ADDR_ALL_NODES_LINK_LOCAL; ng_netif_add(DEFAULT_TEST_NETIF); TEST_ASSERT_NOT_NULL((pids = ng_netif_get(&pid_num))); TEST_ASSERT_EQUAL_INT(1, pid_num); TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_NETIF, pids[0]); TEST_ASSERT_NOT_NULL((entry = ng_ipv6_netif_get(DEFAULT_TEST_NETIF))); TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_NETIF, entry->pid); TEST_ASSERT_EQUAL_STRING((char *)exp_addr.u8, (char *)entry->addrs[0].addr.u8); }
void ng_ipv6_netif_remove(kernel_pid_t pid) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); if (entry == NULL) { return; } mutex_lock(&entry->mutex); _reset_addr_from_entry(entry); DEBUG("Remove IPv6 interface %" PRIkernel_pid "\n", pid); entry->pid = KERNEL_PID_UNDEF; mutex_unlock(&entry->mutex); }
static void _send_to_iface(kernel_pid_t iface, ng_pktsnip_t *pkt) { ng_ipv6_netif_t *if_entry = ng_ipv6_netif_get(iface); ((ng_netif_hdr_t *)pkt->data)->if_pid = iface; if ((if_entry != NULL) && (if_entry->flags & NG_IPV6_NETIF_FLAGS_SIXLOWPAN)) { DEBUG("ipv6: send to 6LoWPAN instead\n"); if (!ng_netapi_dispatch_send(NG_NETTYPE_SIXLOWPAN, NG_NETREG_DEMUX_CTX_ALL, pkt)) { DEBUG("ipv6: no 6LoWPAN thread found"); ng_pktbuf_release(pkt); } } else { ng_netapi_send(iface, pkt); } }
static void test_ipv6_netif_add__despite_free_entry(void) { /* Tests for possible duplicates as described in http://github.com/RIOT-OS/RIOT/issues/2965 */ ng_ipv6_netif_add(OTHER_TEST_NETIF); test_ipv6_netif_add__success(); /* adds DEFAULT_TEST_NETIF as interface */ /* create space by removing first entry */ ng_ipv6_netif_remove(OTHER_TEST_NETIF); /* add DEFAULT_TEST_NETIF yet again and remove it */ test_ipv6_netif_add__success(); ng_ipv6_netif_remove(DEFAULT_TEST_NETIF); /* see if there's a duplicate left */ TEST_ASSERT_NULL(ng_ipv6_netif_get(DEFAULT_TEST_NETIF)); }
void ng_ipv6_netif_remove_addr(kernel_pid_t pid, ng_ipv6_addr_t *addr) { if (pid == KERNEL_PID_UNDEF) { for (int i = 0; i < NG_NETIF_NUMOF; i++) { if (ipv6_ifs[i].pid == KERNEL_PID_UNDEF) { continue; } _remove_addr_from_entry(ipv6_ifs + i, addr); } } else { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); _remove_addr_from_entry(entry, addr); } }
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); } }
static ng_ipv6_addr_t *_match_prefix(kernel_pid_t pid, const ng_ipv6_addr_t *addr, bool only_unicast) { ng_ipv6_addr_t *res = NULL; ng_ipv6_netif_t *iface = ng_ipv6_netif_get(pid); mutex_lock(&(iface->mutex)); if (_find_by_prefix_unsafe(&res, iface, addr, only_unicast) > 0) { mutex_unlock(&(iface->mutex)); return res; } mutex_unlock(&(iface->mutex)); return NULL; }
ng_ipv6_addr_t *ng_ipv6_netif_add_addr(kernel_pid_t pid, const ng_ipv6_addr_t *addr, uint8_t prefix_len, uint8_t flags) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); ng_ipv6_addr_t *res; if ((entry == NULL) || (addr == NULL) || (ng_ipv6_addr_is_unspecified(addr)) || ((prefix_len - 1) > 127)) { /* prefix_len < 1 || prefix_len > 128 */ return NULL; } mutex_lock(&entry->mutex); res = _add_addr_to_entry(entry, addr, prefix_len, flags); mutex_unlock(&entry->mutex); return res; }
void ng_ipv6_netif_remove(kernel_pid_t pid) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); if (entry == NULL) { return; } #ifdef MODULE_NG_NDP ng_ndp_netif_remove(entry); #endif mutex_lock(&entry->mutex); _reset_addr_from_entry(entry); DEBUG("ipv6 netif: Remove IPv6 interface %" PRIkernel_pid "\n", pid); entry->pid = KERNEL_PID_UNDEF; entry->flags = 0; mutex_unlock(&entry->mutex); }
int ng_ipv6_netif_add_addr(kernel_pid_t pid, const ng_ipv6_addr_t *addr, uint8_t prefix_len, bool anycast) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); int res; if (entry == NULL) { return -ENOENT; } if ((addr == NULL) || (ng_ipv6_addr_is_unspecified(addr)) || ((prefix_len - 1) > 127)) { /* prefix_len < 1 || prefix_len > 128 */ return -EINVAL; } mutex_lock(&entry->mutex); res = _add_addr_to_entry(entry, addr, prefix_len, anycast); mutex_unlock(&entry->mutex); return res; }
void ng_ipv6_netif_remove_addr(kernel_pid_t pid, ng_ipv6_addr_t *addr) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); if (entry == NULL) { return; } mutex_lock(&entry->mutex); for (int i = 0; i < NG_IPV6_NETIF_ADDR_NUMOF; i++) { if (ng_ipv6_addr_equal(&(entry->addrs[i].addr), addr)) { DEBUG("Remove %s to interface %" PRIkernel_pid "\n", ng_ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)), pid); ng_ipv6_addr_set_unspecified(&(entry->addrs[i].addr)); entry->addrs[i].flags = 0; mutex_unlock(&entry->mutex); return; } } mutex_unlock(&entry->mutex); }
ng_ipv6_addr_t *ng_ipv6_netif_find_addr(kernel_pid_t pid, const ng_ipv6_addr_t *addr) { ng_ipv6_netif_t *entry = ng_ipv6_netif_get(pid); if (entry == NULL) { return NULL; } mutex_lock(&entry->mutex); for (int i = 0; i < NG_IPV6_NETIF_ADDR_NUMOF; i++) { if (ng_ipv6_addr_equal(&(entry->addrs[i].addr), addr)) { mutex_unlock(&entry->mutex); DEBUG("ipv6 netif: Found %s on interface %" PRIkernel_pid "\n", ng_ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)), pid); return &(entry->addrs[i].addr); } } mutex_unlock(&entry->mutex); return NULL; }
void ng_ndp_retrans_nbr_sol(ng_ipv6_nc_t *nc_entry) { if ((nc_entry->probes_remaining > 1) && ((ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_INCOMPLETE) || (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_PROBE))) { ng_ipv6_addr_t dst; DEBUG("ndp: Retransmit neighbor solicitation for %s\n", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str))); /* retransmit neighbor solicatation */ if (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_INCOMPLETE) { ng_ipv6_addr_set_solicited_nodes(&dst, &nc_entry->ipv6_addr); } else { dst.u64[0] = nc_entry->ipv6_addr.u64[0]; dst.u64[1] = nc_entry->ipv6_addr.u64[1]; } nc_entry->probes_remaining--; if (nc_entry->iface == KERNEL_PID_UNDEF) { timex_t t = { 0, NG_NDP_RETRANS_TIMER }; kernel_pid_t ifs[NG_NETIF_NUMOF]; size_t ifnum = ng_netif_get(ifs); for (size_t i = 0; i < ifnum; i++) { _send_nbr_sol(ifs[i], &nc_entry->ipv6_addr, &dst); } vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, t, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); } else { ng_ipv6_netif_t *ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); _send_nbr_sol(nc_entry->iface, &nc_entry->ipv6_addr, &dst); mutex_lock(&ipv6_iface->mutex); vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, ipv6_iface->retrans_timer, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); mutex_unlock(&ipv6_iface->mutex); } } else if (nc_entry->probes_remaining <= 1) { ng_pktqueue_node_t *queue_node; /* No need to call ng_ipv6_nc_remove() we know already were the * entry is */ DEBUG("ndp: Remove nc entry %s for interface %" PRIkernel_pid "\n", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str)), nc_entry->iface); while ((queue_node = ng_pktqueue_remove_head(&nc_entry->pkts))) { ng_pktbuf_release(queue_node->data); queue_node->data = NULL; } ng_ipv6_addr_set_unspecified(&(nc_entry->ipv6_addr)); nc_entry->iface = KERNEL_PID_UNDEF; nc_entry->flags = 0; nc_entry->probes_remaining = 0; } }
static void test_ipv6_netif_get__empty(void) { for (kernel_pid_t pid = KERNEL_PID_FIRST; pid <= KERNEL_PID_LAST; pid++) { TEST_ASSERT_NULL(ng_ipv6_netif_get(pid)); } }
static void _set_state(ng_ipv6_nc_t *nc_entry, uint8_t state) { ng_ipv6_netif_t *ipv6_iface; timex_t t = { NG_NDP_FIRST_PROBE_DELAY, 0 }; nc_entry->flags &= ~NG_IPV6_NC_STATE_MASK; nc_entry->flags |= state; DEBUG("ndp: set %s state to ", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str))); switch (state) { case NG_IPV6_NC_STATE_REACHABLE: ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); DEBUG("REACHABLE (reachable time = %" PRIu32 ".%06" PRIu32 ")\n", ipv6_iface->reach_time.seconds, ipv6_iface->reach_time.microseconds); t = ipv6_iface->reach_time; /* we intentionally fall through here to set the desired timeout t */ case NG_IPV6_NC_STATE_DELAY: #if ENABLE_DEBUG if (state == NG_IPV6_NC_STATE_DELAY) { DEBUG("DELAY (probe with unicast NS in %u seconds)\n", NG_NDP_FIRST_PROBE_DELAY); } #endif vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, t, ng_ipv6_pid, NG_NDP_MSG_NC_STATE_TIMEOUT, nc_entry); break; case NG_IPV6_NC_STATE_PROBE: ipv6_iface = ng_ipv6_netif_get(nc_entry->iface); nc_entry->probes_remaining = NG_NDP_MAX_UC_NBR_SOL_NUMOF; DEBUG("PROBE (probe with %" PRIu8 " unicast NS every %" PRIu32 ".%06" PRIu32 " seconds)\n", nc_entry->probes_remaining, ipv6_iface->retrans_timer.seconds, ipv6_iface->retrans_timer.microseconds); _send_nbr_sol(nc_entry->iface, &nc_entry->ipv6_addr, &nc_entry->ipv6_addr); mutex_lock(&ipv6_iface->mutex); vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, ipv6_iface->retrans_timer, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); mutex_unlock(&ipv6_iface->mutex); break; #ifdef ENABLE_DEBUG case NG_IPV6_NC_STATE_STALE: DEBUG("STALE (go into DELAY on next packet)\n"); break; #endif default: DEBUG("errorneous or unknown\n"); break; } }
static void _send_nbr_adv(kernel_pid_t iface, ng_ipv6_addr_t *tgt, ng_ipv6_addr_t *dst, bool supply_tl2a) { ng_pktsnip_t *hdr, *pkt = NULL; uint8_t adv_flags = 0; DEBUG("ndp: send neighbor advertisement (iface: %" PRIkernel_pid ", tgt: %s, ", iface, ng_ipv6_addr_to_str(addr_str, tgt, sizeof(addr_str))); DEBUG("dst: %s, supply_tl2a: %d)\n", ng_ipv6_addr_to_str(addr_str, dst, sizeof(addr_str)), supply_tl2a); if (ng_ipv6_netif_get(iface)->flags & NG_IPV6_NETIF_FLAGS_ROUTER) { adv_flags |= NG_NDP_NBR_ADV_FLAGS_R; } if (ng_ipv6_addr_is_unspecified(dst)) { ng_ipv6_addr_set_all_nodes_multicast(dst, NG_IPV6_ADDR_MCAST_SCP_LINK_LOCAL); } else { adv_flags |= NG_NDP_NBR_ADV_FLAGS_S; } if (supply_tl2a) { uint8_t l2src[8]; uint16_t l2src_len; /* we previously checked if we are the target, so we can take our L2src */ l2src_len = _get_l2src(l2src, sizeof(l2src), iface); if (l2src_len > 0) { /* add target address link-layer address option */ pkt = ng_ndp_opt_tl2a_build(l2src, l2src_len, NULL); if (pkt == NULL) { DEBUG("ndp: error allocating Target Link-layer address option.\n"); ng_pktbuf_release(pkt); return; } } } /* TODO: also check if the node provides proxy servies for tgt */ if ((pkt != NULL) && !ng_ipv6_netif_addr_is_non_unicast(tgt)) { /* TL2A is not supplied and tgt is not anycast */ adv_flags |= NG_NDP_NBR_ADV_FLAGS_O; } hdr = ng_ndp_nbr_adv_build(adv_flags, tgt, pkt); if (hdr == NULL) { DEBUG("ndp: error allocating Neighbor advertisement.\n"); ng_pktbuf_release(pkt); return; } pkt = hdr; hdr = ng_ipv6_hdr_build(pkt, NULL, 0, (uint8_t *)dst, sizeof(ng_ipv6_addr_t)); if (hdr == NULL) { DEBUG("ndp: error allocating IPv6 header.\n"); ng_pktbuf_release(pkt); return; } ((ng_ipv6_hdr_t *)hdr->data)->hl = 255; pkt = hdr; /* add netif header for send interface specification */ hdr = ng_netif_hdr_build(NULL, 0, NULL, 0); if (hdr == NULL) { DEBUG("ndp: error allocating netif header.\n"); return; } ((ng_netif_hdr_t *)hdr->data)->if_pid = iface; LL_PREPEND(pkt, hdr); if (ng_ipv6_netif_addr_is_non_unicast(tgt)) { /* avoid collision for anycast addresses * (see https://tools.ietf.org/html/rfc4861#section-7.2.7) */ timex_t delay = { _rand(0, NG_NDP_MAX_AC_TGT_DELAY), 0 }; ng_ipv6_nc_t *nc_entry = ng_ipv6_nc_get(iface, tgt); DEBUG("ndp: delay neighbor advertisement for %" PRIu32 " sec.", delay.seconds); /* nc_entry must be set so no need to check it */ _send_delayed(&nc_entry->nbr_adv_timer, delay, pkt); } else { ng_netapi_send(ng_ipv6_pid, pkt); } }
kernel_pid_t ng_ndp_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_len, kernel_pid_t iface, ng_ipv6_addr_t *dst, ng_pktsnip_t *pkt) { ng_ipv6_addr_t *next_hop_ip = NULL, *prefix = NULL; #ifdef MODULE_NG_IPV6_EXT_RH next_hop_ip = ng_ipv6_ext_rh_next_hop(hdr); #endif #ifdef MODULE_FIB size_t next_hop_size = sizeof(ng_ipv6_addr_t); uint32_t next_hop_flags = 0; ng_ipv6_addr_t next_hop_actual; /* FIB copies address into this variable */ if ((next_hop_ip == NULL) && (fib_get_next_hop(&iface, next_hop_actual.u8, &next_hop_size, &next_hop_flags, (uint8_t *)dst, sizeof(ng_ipv6_addr_t), 0) >= 0) && (next_hop_size == sizeof(ng_ipv6_addr_t))) { next_hop_ip = &next_hop_actual; } #endif if (next_hop_ip == NULL) { /* no route to host */ if (iface == KERNEL_PID_UNDEF) { /* ng_ipv6_netif_t doubles as prefix list */ iface = ng_ipv6_netif_find_by_prefix(&prefix, dst); } else { /* ng_ipv6_netif_t doubles as prefix list */ prefix = ng_ipv6_netif_match_prefix(iface, dst); } if ((prefix != NULL) && /* prefix is on-link */ (ng_ipv6_netif_addr_get(prefix)->flags & NG_IPV6_NETIF_ADDR_FLAGS_NDP_ON_LINK)) { next_hop_ip = dst; #ifdef MODULE_FIB /* We don't care if FIB is full, this is just for efficiency * for later sends */ fib_add_entry(iface, (uint8_t *)dst, sizeof(ng_ipv6_addr_t), 0, (uint8_t *)next_hop_ip, sizeof(ng_ipv6_addr_t), 0, FIB_LIFETIME_NO_EXPIRE); #endif } } if (next_hop_ip == NULL) { next_hop_ip = _default_router(); #ifdef MODULE_FIB /* We don't care if FIB is full, this is just for efficiency for later * sends */ fib_add_entry(iface, (uint8_t *)dst, sizeof(ng_ipv6_addr_t), 0, (uint8_t *)next_hop_ip, sizeof(ng_ipv6_addr_t), 0, FIB_LIFETIME_NO_EXPIRE); #endif } if (next_hop_ip != NULL) { ng_ipv6_nc_t *nc_entry = ng_ipv6_nc_get(iface, next_hop_ip); if ((nc_entry != NULL) && ng_ipv6_nc_is_reachable(nc_entry)) { DEBUG("ndp: found reachable neighbor (%s => ", ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str))); DEBUG("%s)\n", ng_netif_addr_to_str(addr_str, sizeof(addr_str), nc_entry->l2_addr, nc_entry->l2_addr_len)); if (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_STALE) { _set_state(nc_entry, NG_IPV6_NC_STATE_DELAY); } memcpy(l2addr, nc_entry->l2_addr, nc_entry->l2_addr_len); *l2addr_len = nc_entry->l2_addr_len; /* TODO: unreachability check */ return nc_entry->iface; } else if (nc_entry == NULL) { ng_pktqueue_t *pkt_node; ng_ipv6_addr_t dst_sol; nc_entry = ng_ipv6_nc_add(iface, next_hop_ip, NULL, 0, NG_IPV6_NC_STATE_INCOMPLETE << NG_IPV6_NC_STATE_POS); if (nc_entry == NULL) { DEBUG("ndp: could not create neighbor cache entry\n"); return KERNEL_PID_UNDEF; } pkt_node = _alloc_pkt_node(pkt); if (pkt_node == NULL) { DEBUG("ndp: could not add packet to packet queue\n"); } else { /* prevent packet from being released by IPv6 */ ng_pktbuf_hold(pkt_node->pkt, 1); ng_pktqueue_add(&nc_entry->pkts, pkt_node); } /* address resolution */ ng_ipv6_addr_set_solicited_nodes(&dst_sol, next_hop_ip); if (iface == KERNEL_PID_UNDEF) { timex_t t = { 0, NG_NDP_RETRANS_TIMER }; kernel_pid_t ifs[NG_NETIF_NUMOF]; size_t ifnum = ng_netif_get(ifs); for (size_t i = 0; i < ifnum; i++) { _send_nbr_sol(ifs[i], next_hop_ip, &dst_sol); } vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, t, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); } else { ng_ipv6_netif_t *ipv6_iface = ng_ipv6_netif_get(iface); _send_nbr_sol(iface, next_hop_ip, &dst_sol); mutex_lock(&ipv6_iface->mutex); vtimer_remove(&nc_entry->nbr_sol_timer); vtimer_set_msg(&nc_entry->nbr_sol_timer, ipv6_iface->retrans_timer, ng_ipv6_pid, NG_NDP_MSG_NBR_SOL_RETRANS, nc_entry); mutex_unlock(&ipv6_iface->mutex); } } } return KERNEL_PID_UNDEF; }
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 int _fill_ipv6_hdr(kernel_pid_t iface, ng_pktsnip_t *ipv6, ng_pktsnip_t *payload) { int res; ng_ipv6_hdr_t *hdr = ipv6->data; hdr->len = byteorder_htons(ng_pkt_len(payload)); DEBUG("ipv6: set payload length to %zu (network byteorder %04" PRIx16 ")\n", ng_pkt_len(payload), hdr->len.u16); /* check if e.g. extension header was not already marked */ if (hdr->nh == NG_PROTNUM_RESERVED) { hdr->nh = ng_nettype_to_protnum(payload->type); /* if still reserved: mark no next header */ if (hdr->nh == NG_PROTNUM_RESERVED) { hdr->nh = NG_PROTNUM_IPV6_NONXT; } } DEBUG("ipv6: set next header to %" PRIu8 "\n", hdr->nh); if (hdr->hl == 0) { hdr->hl = ng_ipv6_netif_get(iface)->cur_hl; } if (ng_ipv6_addr_is_unspecified(&hdr->src)) { ng_ipv6_addr_t *src = ng_ipv6_netif_find_best_src_addr(iface, &hdr->dst); if (src != NULL) { DEBUG("ipv6: set packet source to %s\n", ng_ipv6_addr_to_str(addr_str, src, sizeof(addr_str))); memcpy(&hdr->src, src, sizeof(ng_ipv6_addr_t)); } /* Otherwise leave unspecified */ } DEBUG("ipv6: calculate checksum for upper header.\n"); #if NG_NETIF_NUMOF > 1 if (payload->users > 1) { ng_pktsnip_t *ptr = ipv6; /* We deal with multiple interfaces here (multicast) => possible * different source addresses => duplication of payload needed */ while (ptr != payload->next) { ng_pktsnip_t *old = ptr->next; /* duplicate everything including payload */ ptr->next = ng_pktbuf_start_write(ptr->next); if (ptr->next == NULL) { DEBUG("ipv6: unable to get write access to payload, drop it\n"); return -ENOBUFS; } ptr = old; } } #endif /* NG_NETIF_NUMOF */ if ((res = ng_netreg_calc_csum(payload, ipv6)) < 0) { if (res != -ENOENT) { /* if there is no checksum we are okay */ DEBUG("ipv6: checksum calculation failed.\n"); return res; } } return 0; }