addrconf: add functionality to check on rpl requirements
authorAlexander Aring <alex.aring@gmail.com>
Fri, 27 Mar 2020 22:00:19 +0000 (18:00 -0400)
committerDavid S. Miller <davem@davemloft.net>
Mon, 30 Mar 2020 05:30:57 +0000 (22:30 -0700)
This patch adds a functionality to addrconf to check on a specific RPL
address configuration. According to RFC 6554:

To detect loops in the SRH, a router MUST determine if the SRH
includes multiple addresses assigned to any interface on that
router. If such addresses appear more than once and are separated by
at least one address not assigned to that router.

Signed-off-by: Alexander Aring <alex.aring@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/addrconf.h
net/ipv6/addrconf.c

index a088349dd94f26f2645f0a7bed65d5be89d5a5da..e0eabe58aa8ba810e1300a56ffcd6cb6103f9075 100644 (file)
@@ -90,6 +90,9 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr);
 #endif
 
+int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
+                         unsigned char nsegs);
+
 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
                                   const unsigned int prefix_len,
                                   struct net_device *dev);
index 5b9de773ce732100757617c7bfb1bd7eab5567d6..594963a7e1ec3059457054a15e6e4d0de0e97633 100644 (file)
@@ -4398,6 +4398,59 @@ int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
 }
 #endif
 
+/* RFC6554 has some algorithm to avoid loops in segment routing by
+ * checking if the segments contains any of a local interface address.
+ *
+ * Quote:
+ *
+ * To detect loops in the SRH, a router MUST determine if the SRH
+ * includes multiple addresses assigned to any interface on that router.
+ * If such addresses appear more than once and are separated by at least
+ * one address not assigned to that router.
+ */
+int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
+                         unsigned char nsegs)
+{
+       const struct in6_addr *addr;
+       int i, ret = 0, found = 0;
+       struct inet6_ifaddr *ifp;
+       bool separated = false;
+       unsigned int hash;
+       bool hash_found;
+
+       rcu_read_lock();
+       for (i = 0; i < nsegs; i++) {
+               addr = &segs[i];
+               hash = inet6_addr_hash(net, addr);
+
+               hash_found = false;
+               hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
+                       if (!net_eq(dev_net(ifp->idev->dev), net))
+                               continue;
+
+                       if (ipv6_addr_equal(&ifp->addr, addr)) {
+                               hash_found = true;
+                               break;
+                       }
+               }
+
+               if (hash_found) {
+                       if (found > 1 && separated) {
+                               ret = 1;
+                               break;
+                       }
+
+                       separated = false;
+                       found++;
+               } else {
+                       separated = true;
+               }
+       }
+       rcu_read_unlock();
+
+       return ret;
+}
+
 /*
  *     Periodic address status verification
  */