From: Simon Kuenzer Date: Thu, 7 Dec 2023 23:18:00 +0000 (+0100) Subject: init: Support parsing of IPv4 CIDR (`UK_NETDEV_IPV4_CIDR`) X-Git-Tag: RELEASE-0.16.2~10 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=5ea209de65819c5b5f7889aa87581cd65a385ac9;p=unikraft%2Flibs%2Flwip.git init: Support parsing of IPv4 CIDR (`UK_NETDEV_IPV4_CIDR`) This commit introduces support for parsing a statically assigned IP address from the the netdev's extended information (einfo) field: `UK_NETDEV_IPV4_CIDR`. The CIDR notation is provided in preference by `lib/uknetdev` and must be read before looking into `UK_NETDEV_IPV4_ADDR` and `UK_NETDEV_IPV4_MASK` fields. Signed-off-by: Simon Kuenzer Reviewed-by: Stefan Jumarea Approved-by: Razvan Deaconescu GitHub-Closes: #42 --- diff --git a/init.c b/init.c index 3402380..49eb04c 100644 --- a/init.c +++ b/init.c @@ -32,6 +32,7 @@ */ #include +#include #include "lwip/opt.h" #include "lwip/tcpip.h" #include "lwip/init.h" @@ -202,6 +203,55 @@ static int liblwip_init(struct uk_init_ctx *ictx __unused) mask4_arg = NULL; gw4_arg = NULL; + /* CIDR (IP and mask) */ + strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_CIDR); + if (strcfg) { + long maskbits; + char str_ipaddr[16]; /* Can hold "255.255.255.255\0" */ + size_t strlen_ipaddr; + const char *str_maskbits; + + str_maskbits = strchr(strcfg, '/'); + if (!str_maskbits) { + uk_pr_err("Failed to find maskbits separator of CIDR IP address: %s\n", + strcfg); + goto ipv4_legacy; + } + strlen_ipaddr = (size_t) str_maskbits - (size_t) strcfg; + if (strlen_ipaddr > ARRAY_SIZE(str_ipaddr)) { + uk_pr_err("IP address length out of range: %s\n", + str_ipaddr); + goto ipv4_legacy; + } + strncpy(str_ipaddr, strcfg, strlen_ipaddr); + str_ipaddr[strlen_ipaddr] = '\0'; + str_maskbits++; /* skip '/' */ + + if (ip4addr_aton(str_ipaddr, &ip4) != 1) { + uk_pr_err("Error converting IP address: %s\n", + str_ipaddr); + goto ipv4_legacy; + } + maskbits = strtol(str_maskbits, NULL, 10); + if (maskbits < 0 || maskbits > 32) { + uk_pr_err("Mask bits out of range: %s\n", + str_ipaddr); + goto ipv4_legacy; + } + ip4_addr_set_u32(&mask4, + lwip_htonl(~(__u32)((1LL << + (32 - maskbits)) - 1))); + + uk_pr_debug("Detected IP from IPv4 CIDR: %s\n", + str_ipaddr); + uk_pr_debug("Detected mask bits from IPv4 CIDR: %ld\n", + maskbits); + ip4_arg = &ip4; + mask4_arg = &mask4; + goto ipv4_gw; + } + +ipv4_legacy: /* IP */ strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_ADDR); if (strcfg) { @@ -227,6 +277,7 @@ static int liblwip_init(struct uk_init_ctx *ictx __unused) ip4_addr_set_u32(&mask4, lwip_htonl(IP_CLASSC_NET)); mask4_arg = &mask4; +ipv4_gw: /* gateway */ strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_GW); if (strcfg) {