Skip to content

Commit

Permalink
Merge pull request #2944 from authmillenon/ipv6_netif/opt/auto-init-addr
Browse files Browse the repository at this point in the history
ipv6_netif: initialize device dependent values
  • Loading branch information
miri64 committed May 26, 2015
2 parents e3bdc53 + 8633043 commit 475bfe6
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
7 changes: 7 additions & 0 deletions drivers/ng_at86rf2xx/ng_at86rf2xx_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ static int _get(ng_netdev_t *device, ng_netconf_opt_t opt,
*((uint16_t *)val) = dev->pan;
return sizeof(uint16_t);

case NETCONF_OPT_PROTO:
if (max_len < sizeof(ng_nettype_t)) {
return -EOVERFLOW;
}
*((ng_nettype_t *)val) = dev->proto;
return sizeof(ng_nettype_t);

case NETCONF_OPT_CHANNEL:
if (max_len < sizeof(uint16_t)) {
return -EOVERFLOW;
Expand Down
8 changes: 8 additions & 0 deletions sys/auto_init/auto_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
#include "net/ng_ipv6.h"
#endif

#ifdef MODULE_NG_IPV6_NETIF
#include "net/ng_ipv6/netif.h"
#endif

#ifdef MODULE_L2_PING
#include "l2_ping.h"
#endif
Expand Down Expand Up @@ -332,4 +336,8 @@ void auto_init(void)
#endif

#endif /* MODULE_AUTO_INIT_NG_NETIF */

#ifdef MODULE_NG_IPV6_NETIF
ng_ipv6_netif_init_by_dev();
#endif
}
8 changes: 8 additions & 0 deletions sys/include/net/ng_ipv6/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ static inline bool ng_ipv6_netif_addr_is_non_unicast(const ng_ipv6_addr_t *addr)

}

/**
* @brief Initializes an interface with device-dependent values.
*
* @note Must be called after all interfaces were initialized and must not
* be called in an interface's thread (will otherwise hang up).
*/
void ng_ipv6_netif_init_by_dev(void);

#ifdef __cplusplus
}
#endif
Expand Down
117 changes: 115 additions & 2 deletions sys/net/network_layer/ng_ipv6/netif/ng_ipv6_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include "mutex.h"
#include "net/ng_ipv6/addr.h"
#include "net/ng_ndp.h"
#include "net/ng_netapi.h"
#include "net/ng_netif.h"
#include "net/ng_netif/hdr.h"

#include "net/ng_ipv6/netif.h"

Expand Down Expand Up @@ -52,8 +54,8 @@ static ng_ipv6_addr_t *_add_addr_to_entry(ng_ipv6_netif_t *entry, const ng_ipv6_

if (!tmp_addr) {
DEBUG("ipv6 netif: couldn't add %s/%" PRIu8 " to interface %" PRIkernel_pid "\n: No space left.",
ng_ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)),
prefix_len, entry->pid);
ng_ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)),
prefix_len, entry->pid);
return NULL;
}

Expand Down Expand Up @@ -442,6 +444,117 @@ ng_ipv6_addr_t *ng_ipv6_netif_find_best_src_addr(kernel_pid_t pid, const ng_ipv6
return _match_prefix(pid, dest, true);
}

/* TODO: put this somewhere more central and L2 protocol dependent */
#define IID_LEN (8)

static bool _hwaddr_to_iid(uint8_t *iid, const uint8_t *hwaddr, size_t hwaddr_len)
{
uint8_t i = 0;

memset(iid, 0, IID_LEN);

switch (hwaddr_len) {
case 8:
iid[0] = hwaddr[i++];
iid[0] ^= 0x02;
iid[1] = hwaddr[i++];
iid[2] = hwaddr[i++];
iid[3] = hwaddr[i++];
iid[4] = hwaddr[i++];
iid[5] = hwaddr[i++];
iid[6] = hwaddr[i++];
iid[7] = hwaddr[i++];
break;

case 6:
iid[0] = hwaddr[i++];
iid[0] ^= 0x02;
iid[1] = hwaddr[i++];
iid[2] = hwaddr[i++];
iid[3] = 0xff;
iid[4] = 0xfe;
iid[5] = hwaddr[i++];
iid[6] = hwaddr[i++];
iid[7] = hwaddr[i++];
break;

case 4:
iid[0] = hwaddr[i++];
iid[0] ^= 0x02;
iid[1] = hwaddr[i++];

case 2:
iid[6] = hwaddr[i++];

case 1:
iid[3] = 0xff;
iid[4] = 0xfe;
iid[7] = hwaddr[i++];
break;

default:
DEBUG("Unknown hardware address length\n");
return false;
}

return true;
}

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;
uint16_t hwaddr_len = 0;
uint8_t hwaddr[NG_NETIF_HDR_L2ADDR_MAX_LEN];
bool try_long = false;
ng_ipv6_netif_t *ipv6_if = ng_ipv6_netif_get(ifs[i]);
int res = 0;

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)) {
DEBUG("Set 6LoWPAN flag\n");
ipv6_ifs->flags |= NG_IPV6_NETIF_FLAGS_SIXLOWPAN;
}

#endif

if ((ng_netapi_get(ifs[i], NETCONF_OPT_SRC_LEN, 0, &hwaddr_len,
sizeof(hwaddr_len)) != -ENOTSUP) &&
(hwaddr_len == 8)) {
try_long = true;
}

if ((try_long && ((res = ng_netapi_get(ifs[i], NETCONF_OPT_ADDRESS_LONG, 0,
&hwaddr, sizeof(hwaddr))) > 0)) ||
((res = ng_netapi_get(ifs[i], NETCONF_OPT_ADDRESS, 0, &hwaddr,
sizeof(hwaddr))) > 0)) {
uint8_t iid[IID_LEN];
hwaddr_len = (uint16_t)res;

if (_hwaddr_to_iid(iid, hwaddr, hwaddr_len)) {
ng_ipv6_addr_set_aiid(&addr, iid);
ng_ipv6_addr_set_link_local_prefix(&addr);
_add_addr_to_entry(ipv6_if, &addr, 64, 0);
}
}

mutex_unlock(&ipv6_if->mutex);
}
}

/**
* @}
*/
2 changes: 1 addition & 1 deletion sys/shell/commands/sc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static void _netif_list(kernel_pid_t dev)
res = ng_netapi_get(dev, NETCONF_OPT_SRC_LEN, 0, &u16, sizeof(u16));

if (res >= 0) {
printf("Source address length: %" PRIu16 "\n ", u16);
printf("Source address length: %" PRIu16 "\n ", u16);
}

#ifdef MODULE_NG_IPV6_NETIF
Expand Down

0 comments on commit 475bfe6

Please sign in to comment.