[PATCH] Lib: accept 240.0.0.0/4 as a valid range
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 | 31 ++++++++++++------------------- sysdep/config.h | 5 +++++ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/ip.c b/lib/ip.c index fcc72cafb4de..8f0f32d25d61 100644 --- a/lib/ip.c +++ b/lib/ip.c @@ -80,30 +80,23 @@ ip6_masklen(ip6_addr *a) } int -ip4_classify(ip4_addr ad) +ip4_classify(u32 a) { - 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
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
FYI, and in as much as it constitutes a form of support for this, we at Project Calico made a similar change to our BIRD 1.6 fork with these commits. (Of which the first is rather embarrassing...) https://github.com/projectcalico/bird/commit/5cc183424d9ffb898171ef16360c400... https://github.com/projectcalico/bird/commit/4c6f0ce840456cc04483a820007b433... https://github.com/projectcalico/bird/commit/49d883ed225d8eb0310009a5a79b1c9... We went for SCOPE_SITE. Best wishes, Neil On Mon, Mar 14, 2022 at 9:35 AM Vincent Bernat <vincent@bernat.ch> wrote:
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
On Wednesday, March 16, 2022 7:24:22 AM ADT Neil Jerram wrote:
FYI, and in as much as it constitutes a form of support for this, we at Project Calico made a similar change to our BIRD 1.6 fork with these commits. (Of which the first is rather embarrassing...) https://github.com/projectcalico/bird/commit/5cc183424d9ffb898171ef16360c400 414f91bf0 https://github.com/projectcalico/bird/commit/4c6f0ce840456cc04483a820007b43 3721706c07 https://github.com/projectcalico/bird/commit/49d883ed225d8eb0310009a5a79b1c 9ee0afbca9
We went for SCOPE_SITE.
We have a similar patch for our 2.0.x builds. We chose SCOPE_SITE as well: --- a/lib/ip.c +++ b/lib/ip.c @@ -103,6 +103,10 @@ if (a == 0xffffffff) return IADDR_BROADCAST | SCOPE_LINK; + /* Allow use of 240.0.0.0/4 as a private range */ + if (b >= 0xf0) + return IADDR_HOST | SCOPE_SITE; + return IADDR_INVALID; } -- James Oakley james@ttgi.io
On Mon, Mar 14, 2022 at 10:02:25AM +0100, Vincent Bernat wrote:
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.
Hi Updated BIRD to accept 240/4 as 'site' scope. We went with slightly different patch: https://gitlab.nic.cz/labs/bird/-/commit/269bfff9bf4b2349248bb48ff61009cf1c5... -- Elen sila lumenn' omentielvo Ondrej 'Santiago' Zajicek (email: santiago@crfreenet.org) OpenPGP encrypted e-mails preferred (KeyID 0x11DEADC3, wwwkeys.pgp.net) "To err is human -- to blame it on a computer is even more so."
❦ 16 March 2022 20:21 +01, Ondrej Zajicek:
Updated BIRD to accept 240/4 as 'site' scope. We went with slightly different patch:
https://gitlab.nic.cz/labs/bird/-/commit/269bfff9bf4b2349248bb48ff61009cf1c5...
Not related, but there is a .orig file lying around in Git. -- October 12, the Discovery. It was wonderful to find America, but it would have been more wonderful to miss it. -- Mark Twain, "Pudd'nhead Wilson's Calendar"
On Wed, Mar 16, 2022 at 08:52:00PM +0100, Vincent Bernat wrote:
❦ 16 March 2022 20:21 +01, Ondrej Zajicek:
Updated BIRD to accept 240/4 as 'site' scope. We went with slightly different patch:
https://gitlab.nic.cz/labs/bird/-/commit/269bfff9bf4b2349248bb48ff61009cf1c5...
Not related, but there is a .orig file lying around in Git.
Thanks, removed. -- Elen sila lumenn' omentielvo Ondrej 'Santiago' Zajicek (email: santiago@crfreenet.org) OpenPGP encrypted e-mails preferred (KeyID 0x11DEADC3, wwwkeys.pgp.net) "To err is human -- to blame it on a computer is even more so."
participants (5)
-
James Oakley -
Neil Jerram -
Ondrej Zajicek -
Vincent Bernat -
Vincent Bernat