Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp218992lqp; Wed, 20 Mar 2024 20:56:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXU+OE8Tnmsq5FZDnt2u1N4d4C2R7dD3+Co9oor/DDMSdwDAh9LDX6tovyx8g9Fo9v6rnXjcXRxeXMkGQdfDZQpgyZAHPMnMb7DpNM1Tg== X-Google-Smtp-Source: AGHT+IFq0RTMjQPQMaHZpQFhbiuKEdM/cFVuvUzOQEoS+fAtXL0/QF+uLASpkHEAGeUnIV44v3me X-Received: by 2002:a05:6358:c017:b0:17e:ad29:91d0 with SMTP id kv23-20020a056358c01700b0017ead2991d0mr919465rwc.11.1710993368944; Wed, 20 Mar 2024 20:56:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710993368; cv=pass; d=google.com; s=arc-20160816; b=k2rAUBLM1JgBiFTjMxpT4UX8eRb1+vANJ7+/9lWAMz5bQTo6etPo8ET1WH1DylMXv6 Nhv+C5Sby1JZAZXWZs19oXrf49FAZsFBiAaROCAFX7XHAKXfzEqtt30ijeEMlxA7GShP M52L3ITXi3gVyl2Dq2HG3N5OYAfCmz3rwUcyI6HLpGMVsr4QagdEhkJEMv5Lnk/xm/vs tMILyJ6FDARsQbb2mMmyNXsF6hQ76hyXfuDOrDnnBoYVU40ot/tZ+10ZudVF3TyEA1QA APZnjPidUnPi6PzlDmQxWZ3Ev2N89j6n9qot/7rXzGuxLFWkl9J8yZmcDRXXvodBsAfy 79DQ== 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=WLDaXjS944vu5ps0Y9+6g4qp34PG74kRIMEPH2hmsvc=; fh=jmw5tth4c7+lD0hRu+AFK/EJCK08ivuzLGab9U+ESOI=; b=aLm4yzOW83w4rhI9IDROKAHKOGPwaXroFU9BT46zvwh/VN0F3i+0N6b21gMaQ+JsVo 6X91ZVBgUGddlqqZixo/jjME0qYVI3qLY2ki2dE4DKOAXnzJJ2RgzKQApArQLEpjLz4K HBV8RQxucyqCHfEIW1kY1qiEGu/RbESSj/ljBkenzkvIDozfx0QQjSmYNQpf09ZRqhmf 0R5L/s6uQgVswc5lIr+Elsvj40hfX+leYH8IzLhRTHsHJQbE9pqRUAInlzQ5UVuIkyC2 glA37rqZMDmA6u/AS6ESNW0YP3ngooPQ1x9Socm4ln7LeAJBfW25kbBUujcdEaWOWmt3 gy6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VGDQWshl; 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-109638-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109638-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id g25-20020a633759000000b005dc4219ac31si13720004pgn.139.2024.03.20.20.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 20:56:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-109638-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VGDQWshl; 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-109638-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109638-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9417F284D2A for ; Thu, 21 Mar 2024 03:56:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 588A0BE48; Thu, 21 Mar 2024 03:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VGDQWshl" 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 1FFDBB672 for ; Thu, 21 Mar 2024 03:55:59 +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=1710993362; cv=none; b=fU4zO93oNiArNmMzEPgU3cYVDMnwhqtHGjr5QgY/YO24iPhgvuDLOyEIS4LFlCy5jTyXwGJe8S65V4aFCQI15LSTvj4Q8AXolPP/uGCenHWa/xQjnqSV0H/LnapAo3ldoIOdxaOuNJ2pOw+/8fi+gpNEGdho7cGBgwWGsCF2Ac0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710993362; c=relaxed/simple; bh=r0fBW5aye32LjaWlKa2QSwnu95nVcNDYV/wru0cmp3w=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=D4LaKpJ21VtPTtEVx2belVkdk1ufmws0UIK4U15+4QeYw5T4XQVK//xjy7hnlmgd9Ml4Pr0J9dR5XWcmzPUeK2eMz6l/LYVGX1pcNq+/kJd+ZYlrjQ6MnDQVGSSBIVi1OknO2lcDwrsImZuRxiyYbwR0haY5F6fXuQXWw/1odEU= 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=VGDQWshl; 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=1710993358; 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=WLDaXjS944vu5ps0Y9+6g4qp34PG74kRIMEPH2hmsvc=; b=VGDQWshl3m68zn+d7O198pn0zgI4K9jzj7s+XOCOk/jUvmq9TM/xApUPu9b1pkPdbB5Hi2 AZSiZ2LTtP6+2GMJXNZBYVPW1IOTO4x8XifXzJqpNwbiNqIsgiELdgC2G9zZv8dUvni/Jb Le+vKX5gLEZIAPh8TUviGONb/kuRol8= Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-57-U2_u_gAfOBa0WLeV-s595g-1; Wed, 20 Mar 2024 23:55:57 -0400 X-MC-Unique: U2_u_gAfOBa0WLeV-s595g-1 Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-3c38f7e7c46so493130b6e.2 for ; Wed, 20 Mar 2024 20:55:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710993356; x=1711598156; 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=WLDaXjS944vu5ps0Y9+6g4qp34PG74kRIMEPH2hmsvc=; b=uy1/x+W58kapRKoDZxT1BWX1dztpgNzvousJ+eaH3JQATXiF8CoSTo/GN81hG8bOV/ CrE0hkgQuDfX2ZNyfP7V7hagBaQKXb7ccR0k6ljjp5HKe8sbqPa0LWxAPikbpVWoogu7 ZHP8swS7qfyQSmfsye2G1g/WAKpJkFsAjiyLD7Y9rL90pim99ABDA4Ej65IcC/KJiRsA 3kzHG5mupmEiT6opz0xKTORf02mMPfjzKaLzmwAWIDlaQbg1SjWG+89a4vunvy4sovPh WR3DXz6PbdHdTcXCyZcwFH4VUzYJW2p8fsbps7+r4UJe0WYuH2N/YeslCk7A43hhpuLC Vh4w== X-Gm-Message-State: AOJu0Yyo81Meplw+fN1Q+yE9PW2AXCIunnw7KcYj5MvMB05qejy9A4fq dW4T+Zsb0iLMdbBGOCFFO+JnkBd5wcXI3g4kGk0ApRcXvfmpR2VBMS+zUOUTJ1QHsd2TUSr01nX wRbap1r/LPKCtIVN/OGr0ZRhWsc63pQ0MpLFStv7dtGtHvkJFk1fClUO6FXPjsA== X-Received: by 2002:a05:6808:3207:b0:3c3:adaf:fd8f with SMTP id cb7-20020a056808320700b003c3adaffd8fmr482727oib.18.1710993356429; Wed, 20 Mar 2024 20:55:56 -0700 (PDT) X-Received: by 2002:a05:6808:3207:b0:3c3:adaf:fd8f with SMTP id cb7-20020a056808320700b003c3adaffd8fmr482712oib.18.1710993356050; Wed, 20 Mar 2024 20:55:56 -0700 (PDT) Received: from smtpclient.apple ([115.96.142.122]) by smtp.gmail.com with ESMTPSA id a26-20020a62e21a000000b006e6ae26625asm12353684pfi.68.2024.03.20.20.55.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Mar 2024 20:55:55 -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 v4] hv/hv_kvp_daemon: Handle IPv4 and Ipv6 combination for keyfile format From: Ani Sinha In-Reply-To: <1710933451-6312-1-git-send-email-shradhagupta@linux.microsoft.com> Date: Thu, 21 Mar 2024 09:25:40 +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: <9879EB6C-FEC4-42AD-8B40-90457455F0DD@redhat.com> References: <1710933451-6312-1-git-send-email-shradhagupta@linux.microsoft.com> To: Shradha Gupta X-Mailer: Apple Mail (2.3774.500.171.1.1) > On 20 Mar 2024, at 16:47, 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) > Signed-off-by: Shradha Gupta > --- > Changes in v4 > * Removed the unnecessary memset for addr in the start > * Added a comment to describe how we erase the last comma character > * Fixed some typos in the commit description > * While using strncat, skip copying the '\0' character. > --- > tools/hv/hv_kvp_daemon.c | 181 ++++++++++++++++++++++++++++++--------- > 1 file changed, 140 insertions(+), 41 deletions(-) >=20 > diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c > index 318e2dad27e0..d64d548a802f 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 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; > @@ -1238,12 +1328,11 @@ static int process_ip_string_nm(FILE *f, char = *ip_string, char *subnet, >=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,62 @@ 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"); > + /* > + * Now we populate the keyfile format > + */ > + > + 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"); > + if (error < 0) > + goto setval_error; There is a problem with this code. dhcp_enabled is only valid for ipv4. = =46rom looking at ifcfg files that were generated before, I do not see = IPV6_AUTOCONF related settings. So maybe we should set method only for = ipv4 and not for ipv6. If the user configures only ipv6, then we do not want to have a section = with method =3D manual for ipv4. method =3D manual without an IP address does = not work. So I suggest that we set method =3D manual only after checking = that ipv4 addresses were added. So maybe move this section a little = below after call to process_ip_string_nm(). This function can return a = specific value to indicate that address were indeed written to the if = cfg/kefile. I am not sure what happens when dhcp_enabled is True and the = user provides specific IP addresses. > + } > + > + /* > + * 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; > - > - /* 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