[PATCH v2] Lib: accept 240.0.0.0/4 as a valid range

Vincent Bernat vincent at bernat.ch
Mon Mar 14 10:12:05 CET 2022


240.0.0.0/4 is marked as reserved and considered invalid by BIRD. At
work, we are using this range internally since all RFC 1918 are full
and 100.64.0.0/10 is already used too. BIRD complains loudly for each
interface using this range.

This change makes it possible to use this range. I have used scope
"universe". But I would be happy with "site" too. While widely
discussed, I don't think 240/4 will become routable on the Internet
one day.

As a bonus, I added some comments and unrolled a condition for each
block. I also have added some hints for the compiler to avoid using
jumps in the hotpath (tested on Godbolt, see
https://godbolt.org/z/rGjz336K3).
---
 lib/ip.c        | 28 +++++++++++-----------------
 sysdep/config.h |  5 +++++
 2 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/lib/ip.c b/lib/ip.c
index fcc72cafb4de..4d0dff636e17 100644
--- a/lib/ip.c
+++ b/lib/ip.c
@@ -85,25 +85,19 @@ ip4_classify(ip4_addr ad)
   u32 a = _I(ad);
   u32 b = a >> 24U;
 
-  if (b && b <= 0xdf)
-  {
-    if (b == 0x7f)
-      return IADDR_HOST | SCOPE_HOST;
-    else if ((b == 0x0a) ||
-	     ((a & 0xffff0000) == 0xc0a80000) ||
-	     ((a & 0xfff00000) == 0xac100000))
-      return IADDR_HOST | SCOPE_SITE;
-    else
-      return IADDR_HOST | SCOPE_UNIVERSE;
-  }
-
-  if (b >= 0xe0 && b <= 0xef)
+  if (unlikely(b == 0x00))
+    return IADDR_INVALID;                   /* 0.0.0.0/8       This network */
+  if (unlikely(b == 0x7f))                  /* 127.0.0.0/8     Loopback */
+    return IADDR_HOST | SCOPE_HOST;
+  if ((b == 0x0a) ||                        /* 10.0.0.0/8      Private-use */
+      ((a & 0xffff0000) == 0xc0a80000) ||   /* 192.168.0.0/16  Private-use */
+      ((a & 0xfff00000) == 0xac100000))     /* 172.16.0.0/12   Private-use */
+    return IADDR_HOST | SCOPE_SITE;
+  if (unlikely(b >= 0xe0 && b <= 0xef))     /* 224.0.0.0/4     Multicast */
     return IADDR_MULTICAST | SCOPE_UNIVERSE;
-
-  if (a == 0xffffffff)
+  if (unlikely(a == 0xffffffff))            /* 255.255.255.255 Limited broadcast */
     return IADDR_BROADCAST | SCOPE_LINK;
-
-  return IADDR_INVALID;
+  return IADDR_HOST | SCOPE_UNIVERSE;
 }
 
 int
diff --git a/sysdep/config.h b/sysdep/config.h
index b0531844af9f..4d73543c3894 100644
--- a/sysdep/config.h
+++ b/sysdep/config.h
@@ -30,6 +30,11 @@
  */
 #include "sysdep/paths.h"
 
+/* Likely/unlikely macros */
+
+#define likely(x)   __builtin_expect((x),1)
+#define unlikely(x) __builtin_expect((x),0)
+
 /* Types */
 
 #include <stdint.h>
-- 
2.35.1



More information about the Bird-users mailing list