Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1182106lqp; Fri, 22 Mar 2024 07:47:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVs0kYt4KRFYWbczzj1Ea87qrR2haE6DdFIbvOIebnWH/85tK54wy4FyiwiSQQNhbaS6qFciQx9Kq8quBPIABCpKOiknAoOt3vbA9q2hA== X-Google-Smtp-Source: AGHT+IGlB/akYmKF5gZQMN+3fkS44VG0MROeUQHtMyqSDtI+CmyRxck08zOmzbTSjK+hXZgAlWlz X-Received: by 2002:a05:6820:2609:b0:5a1:fb:f072 with SMTP id cy9-20020a056820260900b005a100fbf072mr3150206oob.3.1711118847281; Fri, 22 Mar 2024 07:47:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711118847; cv=pass; d=google.com; s=arc-20160816; b=aPM8ci5IAFRX7Rhc4zk1WKpxvRoTkP6jV3JdFUYRnwTjX6RYLOnrnp6eGLXCRRren5 TrmIrMxgbYmmjYwwICBiG17GIXz+AISiuiVkhkpuXpZ7bOkiz5nfSrRCOIq15CfFUVh+ 2zmWgtuE3mNMz4rFcDnu53k1+ElXg2wlcv8TBF4BtqdYfss3Y3cV1hBk4vKGyQjI92qo MtLt8Ebuf7TrcX75EQqzwjWj4Il+hHL5maVk0qJmrRA9CP3Anf8553YhNb3ICJXTjeFs fh2wgd+yxC3wlUeJ6sPgRI4FKHhOMn9Nk6jnbLC/NyDPRn0/zifzUZzM8JwbPSS3xwAh LgGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=oYy4v+/sbdKYlL+/8ZdK/bGpwxS4+Wt4hNJ2lCaZCH4=; fh=jmw5tth4c7+lD0hRu+AFK/EJCK08ivuzLGab9U+ESOI=; b=PoFf+mpp97Bd5D76unSSjRFZECpDywa5xy2YrzyxsYslZWy99ebe/FF7M2xeaSLYMd gtDIWwqr6oJAgN2BPxmo5mYT/T382l5OZDAyaeSS9KofN6V/uLgSIS4MPWBAVnayaXYH FoIk9lFWdrGBBevtNkKzJFWlEJZ7gvGd+0Fa7x4h6xjXfRLokiPvUtrTUtr4ofZxDn8B BOm9wtWFr5bN85e0FdX2geEnhmFuzN2jQ0WRA4b68qg7UxUKDH1sOEcO0M7iFtkQns84 VizzC4CTX42RHJNA2b9/DaAwFEiobIrw80Q6gmWh8i0f8o12A8qxe8WnZLpYAghz9Wve xO+w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SUVExRj1; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-111645-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111645-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s10-20020a05622a1a8a00b00430ebfa23afsi2145871qtc.747.2024.03.22.07.47.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 07:47:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111645-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SUVExRj1; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-111645-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111645-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E12DA1C2091F for ; Fri, 22 Mar 2024 14:47:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4814F4AEE1; Fri, 22 Mar 2024 14:46:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SUVExRj1" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AB8F4AEC3 for ; Fri, 22 Mar 2024 14:46:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711118784; cv=none; b=MGw3jD9KNkvIgV6v74/nkjyL/laYPe2weNWD8I6VjAfhuk2AL1IWXGMKLQCOUOK6PhWL8ZaYpl7UACL6l6KApI/osXBh6PpneuN1Qb12sLg4YBV+MkVsN+uPGhvYS7kjkSn4yA4kDD/xeOiWvuO1FTp9i7nsFyPAF0wXDYMP59M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711118784; c=relaxed/simple; bh=2OLBSmKbfWl0mQMfplJ+EOUjrlXFxoKD3AFdSuW0tt4=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=Kmy9qoCVYZZmVMDO2oMmFq/CZrC2CrcBJ3qGkBVPElRqB0ES+QCBA8JkCVMWZw6g+qP37QfPH58P2WhCuUmEnHfmtV1wb6Jk4S3kxGo2LfnlkevrzcZGr3DAZwLKxKx9jnUDZWceaOncXGAPgh47VVRjN1acVO1BgHRBi3hthx8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SUVExRj1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711118781; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oYy4v+/sbdKYlL+/8ZdK/bGpwxS4+Wt4hNJ2lCaZCH4=; b=SUVExRj1D9GMm/nqYsnTnpo3lXlM4Uzp1Of4jix5LaON8XEQAiDZFFiPY/cvf0RJ4Ly5us PpgllA5FkBMsMRaF0vg3nTCvs2jpGzxfXEzbjh3mnWswAXTe7g0hd5wmMkwq2uzRS19qDR DsvcJGtg+z/Y7XzRq/3vR2hUumz9nXY= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-449-dzdY3O5wMX2sqeDtP-A7ig-1; Fri, 22 Mar 2024 10:46:19 -0400 X-MC-Unique: dzdY3O5wMX2sqeDtP-A7ig-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-29dfc072e95so1471314a91.3 for ; Fri, 22 Mar 2024 07:46:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711118778; x=1711723578; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oYy4v+/sbdKYlL+/8ZdK/bGpwxS4+Wt4hNJ2lCaZCH4=; b=rL9AknEKa0nRMFix1tQQR/RyBjho83OnhdCkZf0OGb0zwkNRmlm9+wCcEpEXOxGlGq bqwfjszC1tQA8xXTNUpgfTWjaxV3hfOxsdhoUXu8fu2OPS80C2TGbslUdMqRgeEL8Hil LioFo7HKh0kn1cVlIqUFWB+4ZkVgNt5z+pNK9OXaHPfqmdFds4cJxfk7PfC6hM11Hzf2 wHh5aCwVBsp3t5hQBPzEwNEJfmemowT5kyQSbZxpCyTN038lZn4rMOVKYv7uSnvg25uU bTQ4ErrxBGmpB3h46u+BU0Z+seM6kOH+pb/GHgtFVHrnH6YyZJmFyS/C490h7C4Pwr48 3fpQ== X-Gm-Message-State: AOJu0YyMQHpOpxEvqRKSxp/PRGgnKnOCfr8Sn/Pq3Ciyqrrm1XFGzZpN rKhq6Lhaf7dn+MogIn0BoRsxBLAXpmQjv+CuddwwGFpH3ChzVEyJ4eK1myi5VfZfE84dhnOsYYw KpVz5xYtO+GbzkCu8gTMloKH9p6zoclSqs/Bg5bB5xSmjCmXTv21+6PwRJ0ZiWQ== X-Received: by 2002:a17:90a:38e5:b0:29d:51ce:607d with SMTP id x92-20020a17090a38e500b0029d51ce607dmr2360079pjb.27.1711118778083; Fri, 22 Mar 2024 07:46:18 -0700 (PDT) X-Received: by 2002:a17:90a:38e5:b0:29d:51ce:607d with SMTP id x92-20020a17090a38e500b0029d51ce607dmr2360049pjb.27.1711118777573; Fri, 22 Mar 2024 07:46:17 -0700 (PDT) Received: from smtpclient.apple ([115.96.118.209]) by smtp.gmail.com with ESMTPSA id sm17-20020a17090b2e5100b002a027bf39a7sm1970543pjb.43.2024.03.22.07.46.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Mar 2024 07:46:17 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.500.171.1.1\)) Subject: Re: [PATCH v5] hv/hv_kvp_daemon: Handle IPv4 and Ipv6 combination for keyfile format From: Ani Sinha In-Reply-To: <1711115162-11629-1-git-send-email-shradhagupta@linux.microsoft.com> Date: Fri, 22 Mar 2024 20:16:02 +0530 Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Olaf Hering , Shradha Gupta Content-Transfer-Encoding: quoted-printable Message-Id: <8767986C-0984-45ED-85E9-8F2271A16CCE@redhat.com> References: <1711115162-11629-1-git-send-email-shradhagupta@linux.microsoft.com> To: Shradha Gupta X-Mailer: Apple Mail (2.3774.500.171.1.1) > On 22 Mar 2024, at 19:16, Shradha Gupta = wrote: >=20 > If the network configuration strings are passed as a combination of = IPv4 > and IPv6 addresses, the current KVP daemon does not handle processing = for > the keyfile configuration format. > With these changes, the keyfile config generation logic scans through = the > list twice to generate IPv4 and IPv6 sections for the configuration = files > to handle this support. >=20 > Testcases ran:Rhel 9, Hyper-V VMs > (IPv4 only, IPv6 only, IPv4 and IPv6 combination) >=20 > Co-developed-by: Ani Sinha > Signed-off-by: Ani Sinha > Signed-off-by: Shradha Gupta > Reviewed-by: Easwar Hariharan Tested-by: Ani Sinha Reviewed-by: Ani Sinha > --- > Changes in v5 > * Included Ani's proposed patch and added him as co-author > --- > tools/hv/hv_kvp_daemon.c | 213 +++++++++++++++++++++++++++++++-------- > 1 file changed, 172 insertions(+), 41 deletions(-) >=20 > diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c > index 318e2dad27e0..ae57bf69ad4a 100644 > --- a/tools/hv/hv_kvp_daemon.c > +++ b/tools/hv/hv_kvp_daemon.c > @@ -76,6 +76,12 @@ enum { > DNS > }; >=20 > +enum { > + IPV4 =3D 1, > + IPV6, > + IP_TYPE_MAX > +}; > + > static int in_hand_shake; >=20 > static char *os_name =3D ""; > @@ -102,6 +108,11 @@ static struct utsname uts_buf; >=20 > #define MAX_FILE_NAME 100 > #define ENTRIES_PER_BLOCK 50 > +/* > + * Change this entry if the number of addresses increases in future > + */ > +#define MAX_IP_ENTRIES 64 > +#define OUTSTR_BUF_SIZE ((INET6_ADDRSTRLEN + 1) * MAX_IP_ENTRIES) >=20 > struct kvp_record { > char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; > @@ -1171,6 +1182,18 @@ static int process_ip_string(FILE *f, char = *ip_string, int type) > return 0; > } >=20 > +int ip_version_check(const char *input_addr) > +{ > + struct in6_addr addr; > + > + if (inet_pton(AF_INET, input_addr, &addr)) > + return IPV4; > + else if (inet_pton(AF_INET6, input_addr, &addr)) > + return IPV6; > + > + return -EINVAL; > +} > + > /* > * Only IPv4 subnet strings needs to be converted to plen > * For IPv6 the subnet is already privided in plen format > @@ -1197,14 +1220,75 @@ static int kvp_subnet_to_plen(char = *subnet_addr_str) > return plen; > } >=20 > +static int process_dns_gateway_nm(FILE *f, char *ip_string, int type, > + int ip_sec) > +{ > + char addr[INET6_ADDRSTRLEN], *output_str; > + int ip_offset =3D 0, error =3D 0, ip_ver; > + char *param_name; > + > + if (type =3D=3D DNS) > + param_name =3D "dns"; > + else if (type =3D=3D GATEWAY) > + param_name =3D "gateway"; > + else > + return -EINVAL; > + > + output_str =3D (char *)calloc(OUTSTR_BUF_SIZE, sizeof(char)); > + if (!output_str) > + return -ENOMEM; > + > + while (1) { > + memset(addr, 0, sizeof(addr)); > + > + if (!parse_ip_val_buffer(ip_string, &ip_offset, addr, > + (MAX_IP_ADDR_SIZE * 2))) > + break; > + > + ip_ver =3D ip_version_check(addr); > + if (ip_ver < 0) > + continue; > + > + if ((ip_ver =3D=3D IPV4 && ip_sec =3D=3D IPV4) || > + (ip_ver =3D=3D IPV6 && ip_sec =3D=3D IPV6)) { > + /* > + * do a bound check to avoid out-of bound writes > + */ > + if ((OUTSTR_BUF_SIZE - strlen(output_str)) > > + (strlen(addr) + 1)) { > + strncat(output_str, addr, > + OUTSTR_BUF_SIZE - > + strlen(output_str) - 1); > + strncat(output_str, ",", > + OUTSTR_BUF_SIZE - > + strlen(output_str) - 1); > + } > + } else { > + continue; > + } > + } > + > + if (strlen(output_str)) { > + /* > + * This is to get rid of that extra comma character > + * in the end of the string > + */ > + output_str[strlen(output_str) - 1] =3D '\0'; > + error =3D fprintf(f, "%s=3D%s\n", param_name, output_str); > + } > + > + free(output_str); > + return error; > +} > + > static int process_ip_string_nm(FILE *f, char *ip_string, char = *subnet, > - int is_ipv6) > + int ip_sec) > { > char addr[INET6_ADDRSTRLEN]; > char subnet_addr[INET6_ADDRSTRLEN]; > - int error, i =3D 0; > + int error =3D 0, i =3D 0; > int ip_offset =3D 0, subnet_offset =3D 0; > - int plen; > + int plen, ip_ver; >=20 > memset(addr, 0, sizeof(addr)); > memset(subnet_addr, 0, sizeof(subnet_addr)); > @@ -1216,10 +1300,16 @@ static int process_ip_string_nm(FILE *f, char = *ip_string, char *subnet, > subnet_addr, > (MAX_IP_ADDR_SIZE * > 2))) { > - if (!is_ipv6) > + ip_ver =3D ip_version_check(addr); > + if (ip_ver < 0) > + continue; > + > + if (ip_ver =3D=3D IPV4 && ip_sec =3D=3D IPV4) > plen =3D kvp_subnet_to_plen((char *)subnet_addr); > - else > + else if (ip_ver =3D=3D IPV6 && ip_sec =3D=3D IPV6) > plen =3D atoi(subnet_addr); > + else > + continue; >=20 > if (plen < 0) > return plen; > @@ -1233,17 +1323,16 @@ static int process_ip_string_nm(FILE *f, char = *ip_string, char *subnet, > memset(subnet_addr, 0, sizeof(subnet_addr)); > } >=20 > - return 0; > + return error; > } >=20 > static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value = *new_val) > { > - int error =3D 0; > + int error =3D 0, ip_ver; > char if_filename[PATH_MAX]; > char nm_filename[PATH_MAX]; > FILE *ifcfg_file, *nmfile; > char cmd[PATH_MAX]; > - int is_ipv6 =3D 0; > char *mac_addr; > int str_len; >=20 > @@ -1421,52 +1510,94 @@ static int kvp_set_ip_info(char *if_name, = struct hv_kvp_ipaddr_value *new_val) > if (error) > goto setval_error; >=20 > - if (new_val->addr_family & ADDR_FAMILY_IPV6) { > - error =3D fprintf(nmfile, "\n[ipv6]\n"); > - if (error < 0) > - goto setval_error; > - is_ipv6 =3D 1; > - } else { > - error =3D fprintf(nmfile, "\n[ipv4]\n"); > - if (error < 0) > - goto setval_error; > - } > - > /* > * Now we populate the keyfile format > + * > + * The keyfile format expects the IPv6 and IPv4 configuration in > + * different sections. Therefore we iterate through the list twice, > + * once to populate the IPv4 section and the next time for IPv6 > */ > + ip_ver =3D IPV4; > + do { > + if (ip_ver =3D=3D IPV4) { > + error =3D fprintf(nmfile, "\n[ipv4]\n"); > + if (error < 0) > + goto setval_error; > + } else { > + error =3D fprintf(nmfile, "\n[ipv6]\n"); > + if (error < 0) > + goto setval_error; > + } >=20 > - if (new_val->dhcp_enabled) { > - error =3D kvp_write_file(nmfile, "method", "", "auto"); > - if (error < 0) > - goto setval_error; > - } else { > - error =3D kvp_write_file(nmfile, "method", "", "manual"); > + /* > + * Write the configuration for ipaddress, netmask, gateway and > + * name services > + */ > + error =3D process_ip_string_nm(nmfile, (char *)new_val->ip_addr, > + (char *)new_val->sub_net, > + ip_ver); > if (error < 0) > goto setval_error; > - } >=20 > - /* > - * Write the configuration for ipaddress, netmask, gateway and > - * name services > - */ > - error =3D process_ip_string_nm(nmfile, (char *)new_val->ip_addr, > - (char *)new_val->sub_net, is_ipv6); > - if (error < 0) > - goto setval_error; > + /* > + * As dhcp_enabled is only valid for ipv4, we do not set dhcp > + * methods for ipv6 based on dhcp_enabled flag. > + * > + * For ipv4, set method to manual only when dhcp_enabled is > + * false and specific ipv4 addresses are configured. If neither > + * dhcp_enabled is true and no ipv4 addresses are configured, > + * set method to 'disabled'. > + * > + * For ipv6, set method to manual when we configure ipv6 > + * addresses. Otherwise set method to 'auto' so that SLAAC from > + * RA may be used. > + */ > + if (ip_ver =3D=3D IPV4) { > + if (new_val->dhcp_enabled) { > + error =3D kvp_write_file(nmfile, "method", "", > + "auto"); > + if (error < 0) > + goto setval_error; > + } else if (error) { > + error =3D kvp_write_file(nmfile, "method", "", > + "manual"); > + if (error < 0) > + goto setval_error; > + } else { > + error =3D kvp_write_file(nmfile, "method", "", > + "disabled"); > + if (error < 0) > + goto setval_error; > + } > + } else if (ip_ver =3D=3D IPV6) { > + if (error) { > + error =3D kvp_write_file(nmfile, "method", "", > + "manual"); > + if (error < 0) > + goto setval_error; > + } else { > + error =3D kvp_write_file(nmfile, "method", "", > + "auto"); > + if (error < 0) > + goto setval_error; > + } > + } >=20 > - /* we do not want ipv4 addresses in ipv6 section and vice versa */ > - if (is_ipv6 !=3D is_ipv4((char *)new_val->gate_way)) { > - error =3D fprintf(nmfile, "gateway=3D%s\n", (char = *)new_val->gate_way); > + error =3D process_dns_gateway_nm(nmfile, > + (char *)new_val->gate_way, > + GATEWAY, ip_ver); > if (error < 0) > goto setval_error; > - } >=20 > - if (is_ipv6 !=3D is_ipv4((char *)new_val->dns_addr)) { > - error =3D fprintf(nmfile, "dns=3D%s\n", (char *)new_val->dns_addr); > + error =3D process_dns_gateway_nm(nmfile, > + (char *)new_val->dns_addr, DNS, > + ip_ver); > if (error < 0) > goto setval_error; > - } > + > + ip_ver++; > + } while (ip_ver < IP_TYPE_MAX); > + > fclose(nmfile); > fclose(ifcfg_file); >=20 > --=20 > 2.34.1 >=20