From 29788f178e48fa5ffe7d3262d73c9548e9285d2d Mon Sep 17 00:00:00 2001 From: Fu Siyuan Date: Wed, 18 Oct 2017 10:13:39 +0800 Subject: [PATCH] MdeModulePkg: Update IP4 stack to support point-to-point link with 31-bit mask. V2 update: Directly use NetIp4IsUnicast() to check station address in IP driver. This patch is to follow RFC3021 which allows to use 31-bit mask in point-to-point link. If a 31-bit subnet mask is assigned to a point-to-point link, it leaves the with only 1 bit. Consequently, only two possible addresses may result: {, 0} and {, -1} These addresses have historically been associated with network and broadcast addresses (see Section 2.2). In a point-to-point link with a 31-bit subnet mask, the two addresses above MUST be interpreted as host addresses. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Fu Siyuan Reviewed-by: Wu Jiaxin --- MdeModulePkg/Include/Library/NetLib.h | 6 +++-- MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 14 ++++++++--- .../Universal/Network/Ip4Dxe/Ip4Common.c | 25 +++---------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/Library/NetLib.h index 4cd42270f6..b9df46c50f 100644 --- a/MdeModulePkg/Include/Library/NetLib.h +++ b/MdeModulePkg/Include/Library/NetLib.h @@ -414,8 +414,10 @@ NetGetIpClass ( ASSERT if NetMask is zero. - If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address. - + If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address, + except when the originator is one of the endpoints of a point-to-point link with a 31-bit + mask (RFC3021). + @param[in] Ip The IP to check against. @param[in] NetMask The mask of the IP. diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index 1ee77fe036..b8544b89ab 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -637,7 +637,9 @@ NetGetIpClass ( ASSERT if NetMask is zero. - If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address. + If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address, + except when the originator is one of the endpoints of a point-to-point link with a 31-bit + mask (RFC3021). @param[in] Ip The IP to check against. @param[in] NetMask The mask of the IP. @@ -657,9 +659,13 @@ NetIp4IsUnicast ( if (Ip == 0 || IP4_IS_LOCAL_BROADCAST (Ip)) { return FALSE; } - - if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) { - return FALSE; + + if (NetGetMaskLength (NetMask) != 31) { + if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) { + return FALSE; + } + } else { + return TRUE; } return TRUE; diff --git a/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c b/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c index 7c7d182073..b582ccf13f 100644 --- a/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c +++ b/MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c @@ -287,10 +287,6 @@ Ip4StationAddressValid ( IN IP4_ADDR Netmask ) { - IP4_ADDR NetBrdcastMask; - INTN Len; - INTN Type; - // // Only support the station address with 0.0.0.0/0 to enable DHCP client. // @@ -301,29 +297,16 @@ Ip4StationAddressValid ( // // Only support the continuous net masks // - if ((Len = NetGetMaskLength (Netmask)) == (IP4_MASK_MAX + 1)) { + if (NetGetMaskLength (Netmask) == (IP4_MASK_MAX + 1)) { return FALSE; } // // Station address can't be class D or class E address // - if ((Type = NetGetIpClass (Ip)) > IP4_ADDR_CLASSC) { - return FALSE; - } - - // - // Station address can't be subnet broadcast/net broadcast address - // - if ((Ip == (Ip & Netmask)) || (Ip == (Ip | ~Netmask))) { + if (NetGetIpClass (Ip) > IP4_ADDR_CLASSC) { return FALSE; } - NetBrdcastMask = gIp4AllMasks[MIN (Len, Type << 3)]; - - if (Ip == (Ip | ~NetBrdcastMask)) { - return FALSE; - } - - return TRUE; -} \ No newline at end of file + return NetIp4IsUnicast (Ip, Netmask); +} -- 2.39.2