Skip to content

gnrc_netif: use link-local source address if destination is link-local#16665

Closed
benpicco wants to merge 1 commit intoRIOT-OS:masterfrom
benpicco:gnrc_netif-ll_source
Closed

gnrc_netif: use link-local source address if destination is link-local#16665
benpicco wants to merge 1 commit intoRIOT-OS:masterfrom
benpicco:gnrc_netif-ll_source

Conversation

@benpicco
Copy link
Copy Markdown
Contributor

Contribution description

So while debugging some strange behavior in GNRC I noticed that sometimes a packet would be send to a link-local address with a global address as the source if the source address is left unspecified.

Is this the intended behavior? From my naive understanding I would expect to only use link-local addresses when targeting another on-link host, but I might be wrong.

Testing procedure

Issues/PRs references

@benpicco benpicco added the Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR label Jul 21, 2021
@github-actions github-actions bot added Area: network Area: Networking Area: sys Area: System labels Jul 21, 2021
@miri64
Copy link
Copy Markdown
Member

miri64 commented Jul 21, 2021

The source address selection is quite involved and specified in https://datatracker.ietf.org/doc/html/rfc6724, but from what I remember, rule 2 should make sure that if a link-local address is present the appropriate scope will be selected. So I'd rather not touch that by enforcing it. E.g. what if, for whatever reason, the interface does not have a link-local address. With this change the source address selection will always fail then. So there must be another reason why a global address is picked in your case. Usually, a link-local address should be picked.

@miri64 miri64 added the State: invalid State: The issue / PR is not valid label Jul 21, 2021
@benpicco benpicco closed this Jul 21, 2021
@benpicco benpicco deleted the gnrc_netif-ll_source branch July 21, 2021 09:52
@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Jul 23, 2021

Right now gnrc_netif_ipv6_addr_best_src() just returns the first address of the candidate set - shouldn't we still try to prefer link-local addresses if the destination is link-local?

The candidate set is just compromised of all valid addresses on the interface, there is nothing really involved about this function.

How about

--- a/sys/net/gnrc/netif/gnrc_netif.c
+++ b/sys/net/gnrc/netif/gnrc_netif.c
@@ -1019,7 +1019,7 @@ static int _create_candidate_set(const gnrc_netif_t *netif,
     /* currently this implementation supports only addresses as source address
      * candidates assigned to this interface. Thus we assume all addresses to be
      * on interface @p netif */
-    (void) dst;
+    bool ll_prefer = ipv6_addr_is_link_local(dst);
     for (int i = 0; i < CONFIG_GNRC_NETIF_IPV6_ADDRS_NUMOF; i++) {
         const ipv6_addr_t *tmp = &(netif->ipv6.addrs[i]);
 
@@ -1047,6 +1047,10 @@ static int _create_candidate_set(const gnrc_netif_t *netif,
         if (ll_only && !ipv6_addr_is_link_local(tmp)) {
             continue;
         }
+        /* prefer link-local source for link-local destination */
+        if (ll_prefer && ipv6_addr_is_link_local(tmp)) {
+            res = i;
+        }
         /* "For all multicast and link-local destination addresses, the set of
          *  candidate source addresses MUST only include addresses assigned to
          *  interfaces belonging to the same link as the outgoing interface."

@miri64
Copy link
Copy Markdown
Member

miri64 commented Jul 23, 2021

Why pick out link-local? According to rule 2 this should hold for all scopes, right?

@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Jul 23, 2021

It certainly is convenient to use link-local addresses in the context of 6loWPAN

The RFC you referenced even says

The destination address selection rules give preference to destinations of smaller scope. For example, a link-local destination will be sorted before a global scope destination when the two are otherwise equally suitable.

Now _create_candidate_set() doesn't do any sorting as is, but giving preference to link-local would be an easy addition.

edit ah sorry I completely missed the _src_addr_selection() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: network Area: Networking Area: sys Area: System Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR State: invalid State: The issue / PR is not valid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants