From 83b8abaf8ad83e40cf580eeadc193672b44cf84c Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Thu, 24 Jan 2019 15:56:10 +0100 Subject: [PATCH] net: ip: Introduce mesh_local address flag This commit introduces a concept of mesh-local IPv6 addresses. Such addresses should only be used for mesh-local communication, therefore should not be used to communicate with different subnets (i. e. destinations outside the mesh). As `addr_type` field already holds different kind of information (whether address was created automatically/manually) it was not used in this case. Instead a mesh_local flag was added, so that we do not lose information on how address was created. Address with such flag set will only be selected as a source address automatically if the destination address is within the same subnet it belongs to. Signed-off-by: Robert Lubos --- include/net/net_if.h | 5 ++++- subsys/net/ip/net_if.c | 7 +++++++ subsys/net/ip/net_shell.c | 5 +++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/net/net_if.h b/include/net/net_if.h index ab7345c55d..ca1643a5a1 100644 --- a/include/net/net_if.h +++ b/include/net/net_if.h @@ -75,7 +75,10 @@ struct net_if_addr { /** Is this IP address used or not */ u8_t is_used : 1; - u8_t _unused : 6; + /** Is this IP address usage limited to the subnet (mesh) or not */ + u8_t is_mesh_local : 1; + + u8_t _unused : 5; }; /** diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index 3ad0aa55a2..78cb5219da 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -1970,6 +1970,13 @@ static struct in6_addr *net_if_ipv6_get_best_match(struct net_if *iface, len = get_diff_ipv6(dst, &ipv6->unicast[i].address.in6_addr); if (len >= *best_so_far) { + /* Mesh local address can only be selected for the same + * subnet. + */ + if (ipv6->unicast[i].is_mesh_local && len < 64) { + continue; + } + *best_so_far = len; src = &ipv6->unicast[i].address.in6_addr; } diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c index 43d1d99d39..da40d2414c 100644 --- a/subsys/net/ip/net_shell.c +++ b/subsys/net/ip/net_shell.c @@ -348,11 +348,12 @@ static void iface_cb(struct net_if *iface, void *user_data) continue; } - PR("\t%s %s %s%s\n", + PR("\t%s %s %s%s%s\n", net_sprint_ipv6_addr(&unicast->address.in6_addr), addrtype2str(unicast->addr_type), addrstate2str(unicast->addr_state), - unicast->is_infinite ? " infinite" : ""); + unicast->is_infinite ? " infinite" : "", + unicast->is_mesh_local ? " meshlocal" : ""); count++; }