From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CiTE8Slc; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 45BFA5A0269 for ; Fri, 12 Jun 2026 01:04:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1781219086; 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=1JnkF2Os8H2HHDnqeBoIxIAm9NZIO8m2hoU8FclI/fw=; b=CiTE8Slc5fZO9d7ntCWM/7OBaNMklUq5Wh4YQRT+Jru7hfavIIXpcAtb0H4vujOg0eQQFs r5XG+x9QVmgZbHAoIpBH8FcFvVFOBXIEg0GcvZm5v0a28L4pTYwIJB0OcQDuBwTkVOXvjs YIvtQZj9/3v7uI0PXj5XrfiS017BIJg= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-X66yuIkAOOSM7kPlSnHIpQ-1; Thu, 11 Jun 2026 19:04:44 -0400 X-MC-Unique: X66yuIkAOOSM7kPlSnHIpQ-1 X-Mimecast-MFC-AGG-ID: X66yuIkAOOSM7kPlSnHIpQ_1781219083 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-490b9cd54f3so1507695e9.2 for ; Thu, 11 Jun 2026 16:04:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781219083; x=1781823883; h=date:content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l3f8bx3kzvV14fDlNtoHKsrWIVlkZAc7LvWgo3NAlz4=; b=jf8viycgB8SSeR+sZlXTdBAWFGD0q3ApKDInukEhLkcUSR0HmKDWikZS4AXuqbmZ7T FgjcMIDajGWHPLsHQDEpmiTCd0s4HATOd+q/MZze+RM96a/YlAYXZ8HXCZx8EGYvBPOV Na3cFRr+Y+qpeyRSq3NvXa8odjSxFWuz6rZKe8S6jUHVaIGdor9f4msl4wXIH0mtpHhO udB4MHJNxAABX1AkA6/yZR4/iWTaS1VDUm1XAPhpx+L22jVkg1bF9M/1T1ZjtUMr+LuO +6hKLHFbyF9kB0e2Iwcf/u3WwLsks9Ru4c04zAMVeJWjvwEA23Zrbey92AX84ucV4SuV 50kg== X-Gm-Message-State: AOJu0Yxvgpuee32hiVF1EV1UD7HOq300NdpF3M0q+dJXy4OJtRS0xTD5 lCZcn+0rlhLkndq1Ofn59h/kOcmGvRMqqsecNFosu+stPIOtSrS15QfRw7FfYT0tAYx7ezvSviz /ybTv3jh+yo9lvPkyRv3uGoSUAEdnBWJevFAU30ixim2uYOcW9pbyQA== X-Gm-Gg: Acq92OHFs0JXOHcMy4aBoxcRMoVYfmEIfhLhq/VLnGdQdS4l/B5+o8nHXX4wiR0DCJ0 ybpVFurBuprXj6zXu6AzJSIcy7AAWqmlNeCFIffcia/W8Aad8XbFDkmlA4LXLKL1f6fVmB+C/aY 5V2oCp1CHIOrr90XCrB1GazGme3YY/e6/Ds/8TIqsHNROazWZbBhDnGqpAH70LY9zF5LUMUjhiS 6s+t9G4Vpl91RlMWE5/cHKJl9RCAkL+txtP4IRo7HpsDBDAm7qLFfZ58wX1+maa53lFqlQeB6ul f4CigJG7z2CLHys3M63GNqK9Q3HDhgpiLG0L/BjRLjSSjPQsv+j6/n/PeR6oVatZ1NQM00x66jz l6AGVlCQzpw4TWvoggviZjHKB/O0R80geQJkUwyL4h+kbqWrIKA== X-Received: by 2002:a05:600d:844e:20b0:490:e60b:5fd8 with SMTP id 5b1f17b1804b1-490ec50939fmr339095e9.26.1781219083201; Thu, 11 Jun 2026 16:04:43 -0700 (PDT) X-Received: by 2002:a05:600d:844e:20b0:490:e60b:5fd8 with SMTP id 5b1f17b1804b1-490ec50939fmr338815e9.26.1781219082645; Thu, 11 Jun 2026 16:04:42 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490ea7c871dsm18903085e9.5.2026.06.11.16.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jun 2026 16:04:42 -0700 (PDT) From: Stefano Brivio To: Anshu Kumari Subject: Re: [PATCH v3 3/6] dhcp: Add option type table and value parser Message-ID: <20260612010441.18dcd4a1@elisabeth> In-Reply-To: <20260601073758.1571317-4-anskuma@redhat.com> References: <20260601073758.1571317-1-anskuma@redhat.com> <20260601073758.1571317-4-anskuma@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 Date: Fri, 12 Jun 2026 01:04:41 +0200 (CEST) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 591vXaAjuwDHk3ToKqu90kzvGxXpRhUqz6oaltVbOQU_1781219083 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: FYNPG5MLH3FOURSQMXVB2LPURS2CXX5P X-Message-ID-Hash: FYNPG5MLH3FOURSQMXVB2LPURS2CXX5P X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, david@gibson.dropbear.id.au, jmaloy@redhat.com, lvivier@redhat.com X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Mon, 1 Jun 2026 13:07:53 +0530 Anshu Kumari wrote: > Add an RFC 2132 type lookup table mapping DHCP option codes to their > expected value formats, and a dhcp_opt_parse() function that converts > CLI string values into their binary wire representation. >=20 > Wire dhcp_opt_parse() into dhcp_add_option() so that values are > validated and encoded at configuration time. >=20 > Link: https://bugs.passt.top/show_bug.cgi?id=3D192 > Signed-off-by: Anshu Kumari > --- > v3: > - Replaced DHCP_OPT_INTEGER with separate DHCP_OPT_INT8/INT16/INT32 > enums, removed dhcp_opt_int_width[] array. > - Shared logic between DHCP_OPT_IPV4 and DHCP_OPT_IPV4_LIST =E2=80=94 p= arse > both as list, error if >1 in single case. > - Added errno =3D 0 before strtoul() and check after. > - Fixed range check: 1ULL << (width * 8) for all widths including > width=3D=3D4. > - strncpy =E2=86=92 memcpy for DHCP_OPT_STR. > - Moved enum to dhcp.c since not used in other files. > - Removed options 55, 61 (client-only), 119 (DNS compression, use > --dhcp-search instead), 33 (IP pairs not supported). > - DHCP_OPT_PARSE_BUF 1024 =E2=86=92 char tmp[256]. > - Upgraded dhcp_add_option() to call dhcp_opt_parse() and populate > val[]/len. > - Aligned array entries for readability. > - Added tab after @DHCP_OPT_IPV4_LIST: in kerneldoc. > - Reject empty value strings before parsing > - Reject leading/trailing/consecutive commas in IP list values. >=20 > v2: > - Replaced struct lookup table + dhcp_opt_type_lookup() function with f= lat dhcp_opt_types[256] array indexed by code. > - Consolidated DHCP_OPT_UINT8/UINT16/UINT32 into single DHCP_OPT_INTEGE= R with dhcp_opt_int_width[256] table. > - Dropped DHCP_OPT_ROUTES / option 121 entirely. > - Added kerneldoc for enum dhcp_opt_type values. > - Removed curly braces from switch cases, declarations before switch. > - Added newlines before return statements. > - Changed IP list delimiter from space to comma (--dhcp-opt 6,1.1.1.1,8= .8.8.8). > - Defined DHCP_OPT_PARSE_BUF constant for bare 1024. > - Added len and val[255] fields to struct here (moved from patch 1). > - Added kerneldoc for @custom_opts.len and @custom_opts.val. > - Wired dhcp_opt_parse() into case 32 (--dhcp-boot) to populate val/len= . > --- > dhcp.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- > passt.h | 4 ++ > 2 files changed, 181 insertions(+), 3 deletions(-) >=20 > diff --git a/dhcp.c b/dhcp.c > index c5fbf37..07a42b9 100644 > --- a/dhcp.c > +++ b/dhcp.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include > =20 > #include "util.h" > #include "ip.h" > @@ -33,6 +34,170 @@ > #include "log.h" > #include "dhcp.h" > =20 > +/** > + * enum dhcp_opt_type - DHCP option value types per RFC 2132 > + * @DHCP_OPT_NONE:=09Unsupported or unknown option > + * @DHCP_OPT_STR:=09Variable-length string > + * @DHCP_OPT_IPV4:=09Single IPv4 address > + * @DHCP_OPT_IPV4_LIST:=09Multiple IPv4 addresses, comma-separated > + * @DHCP_OPT_INT8:=09Unsigned 8-bit integer > + * @DHCP_OPT_INT16:=09Unsigned 16-bit integer > + * @DHCP_OPT_INT32:=09Unsigned 32-bit integer > + */ > +enum dhcp_opt_type { > +=09DHCP_OPT_NONE, > +=09DHCP_OPT_STR, > +=09DHCP_OPT_IPV4, > +=09DHCP_OPT_IPV4_LIST, > +=09DHCP_OPT_INT8, > +=09DHCP_OPT_INT16, > +=09DHCP_OPT_INT32, > +}; > + > +/** > + * dhcp_opt_types - Maps option code to RFC 2132 value type, indexed by = code > + */ > +static const enum dhcp_opt_type dhcp_opt_types[256] =3D { > +=09[1] =3D DHCP_OPT_IPV4,=09=09/* Subnet Mask */ > +=09[2] =3D DHCP_OPT_INT32,=09=09/* Time Offset */ > +=09[3] =3D DHCP_OPT_IPV4_LIST,=09/* Router */ > +=09[4] =3D DHCP_OPT_IPV4_LIST,=09/* Time Server */ > +=09[5] =3D DHCP_OPT_IPV4_LIST,=09/* Name Server */ > +=09[6] =3D DHCP_OPT_IPV4_LIST,=09/* Domain Name Server */ > +=09[7] =3D DHCP_OPT_IPV4_LIST,=09/* Log Server */ > +=09[8] =3D DHCP_OPT_IPV4_LIST,=09/* Cookie Server */ > +=09[9] =3D DHCP_OPT_IPV4_LIST,=09/* LPR Server */ > +=09[10] =3D DHCP_OPT_IPV4_LIST,=09/* Impress Server */ > +=09[11] =3D DHCP_OPT_IPV4_LIST,=09/* Resource Location Server */ > +=09[12] =3D DHCP_OPT_STR,=09=09/* Host Name */ > +=09[13] =3D DHCP_OPT_INT16,=09=09/* Boot File Size */ > +=09[15] =3D DHCP_OPT_STR,=09=09/* Domain Name */ > +=09[16] =3D DHCP_OPT_IPV4,=09=09/* Swap Server */ > +=09[17] =3D DHCP_OPT_STR,=09=09/* Root Path */ > +=09[19] =3D DHCP_OPT_INT8,=09=09/* IP Forwarding */ > +=09[23] =3D DHCP_OPT_INT8,=09=09/* Default IP TTL */ > +=09[26] =3D DHCP_OPT_INT16,=09=09/* Interface MTU */ > +=09[28] =3D DHCP_OPT_IPV4,=09=09/* Broadcast Address */ > +=09[37] =3D DHCP_OPT_INT8,=09=09/* TCP Default TTL */ > +=09[38] =3D DHCP_OPT_INT32,=09=09/* TCP Keepalive Interval */ > +=09[40] =3D DHCP_OPT_STR,=09=09/* NIS Domain Name */ > +=09[41] =3D DHCP_OPT_IPV4_LIST,=09/* NIS Servers */ > +=09[42] =3D DHCP_OPT_IPV4_LIST,=09/* NTP Servers */ > +=09[44] =3D DHCP_OPT_IPV4_LIST,=09/* NetBIOS Name Server */ > +=09[50] =3D DHCP_OPT_IPV4,=09=09/* Requested IP Address */ > +=09[51] =3D DHCP_OPT_INT32,=09=09/* IP Address Lease Time */ > +=09[53] =3D DHCP_OPT_INT8,=09=09/* DHCP Message Type */ > +=09[54] =3D DHCP_OPT_IPV4,=09=09/* Server Identifier */ > +=09[57] =3D DHCP_OPT_INT16,=09=09/* Max DHCP Message Size */ > +=09[58] =3D DHCP_OPT_INT32,=09=09/* Renewal (T1) Time */ > +=09[59] =3D DHCP_OPT_INT32,=09=09/* Rebinding (T2) Time */ > +=09[60] =3D DHCP_OPT_STR,=09=09/* Vendor Class Identifier */ > +=09[66] =3D DHCP_OPT_STR,=09=09/* TFTP Server Name */ > +=09[67] =3D DHCP_OPT_STR,=09=09/* Bootfile Name */ > +=09[252] =3D DHCP_OPT_STR,=09=09/* WPAD URL */ > +}; > + > +/** > + * dhcp_opt_parse() - Parse a DHCP option value > + * @code:=09DHCP option code > + * @str:=09Value string from command line > + * @buf:=09Output buffer for binary value > + * @buf_len:=09Size of output buffer > + * > + * Return: number of bytes written to @buf, or -1 on error > + */ > +static int dhcp_opt_parse(uint8_t code, const char *str, > +=09=09=09 uint8_t *buf, size_t buf_len) > +{ > +=09enum dhcp_opt_type type =3D dhcp_opt_types[code]; > +=09char *tok, *saveptr, *end; > +=09struct in_addr addr; > +=09unsigned long val; > +=09unsigned int i; > +=09uint8_t width; > +=09char tmp[256]; > +=09size_t slen; > +=09int len; > + > +=09if (!*str) > +=09=09die("Empty value for DHCP option %u", code); > + > +=09switch (type) { > +=09case DHCP_OPT_NONE: > +=09=09die("Unsupported DHCP option: %u," > +=09=09 " see passt(1) for supported codes", code); > +=09case DHCP_OPT_IPV4: > +=09case DHCP_OPT_IPV4_LIST: > +=09=09len =3D 0; > + > +=09=09/* Reject empty, leading/trailing, or consecutive commas */ > +=09=09if (!*str || *str =3D=3D ',' || str[strlen(str) - 1] =3D=3D ',' || > +=09=09 strstr(str, ",,")) > +=09=09=09return -1; > + > +=09=09if (snprintf_check(tmp, sizeof(tmp), "%s", str)) > +=09=09=09return -1; > + > +=09=09for (tok =3D strtok_r(tmp, ",", &saveptr); tok; > +=09=09 tok =3D strtok_r(NULL, ",", &saveptr)) { > +=09=09=09if (inet_pton(AF_INET, tok, &addr) !=3D 1) > +=09=09=09=09return -1; > + > +=09=09=09if (len + (int)sizeof(addr) > (int)buf_len) > +=09=09=09=09return -1; > + > +=09=09=09memcpy(buf + len, &addr, sizeof(addr)); > +=09=09=09len +=3D sizeof(addr); > + > +=09=09=09if (type =3D=3D DHCP_OPT_IPV4) > +=09=09=09=09break; > +=09=09} > + > +=09=09if (type =3D=3D DHCP_OPT_IPV4 && strtok_r(NULL, ",", &saveptr)) > +=09=09=09return -1; > + > +=09=09return len; > +=09case DHCP_OPT_INT8: > +=09case DHCP_OPT_INT16: > +=09case DHCP_OPT_INT32: > +=09=09if (type =3D=3D DHCP_OPT_INT8) > +=09=09=09width =3D 1; > +=09=09else if (type =3D=3D DHCP_OPT_INT16) > +=09=09=09width =3D 2; > +=09=09else > +=09=09=09width =3D 4; > + > +=09=09errno =3D 0; > +=09=09val =3D strtoul(str, &end, 0); > + > +=09=09if (*end || errno) > +=09=09=09return -1; > + > +=09=09if (buf_len < width) > +=09=09=09return -1; > + > +=09=09if (val >=3D (1ULL << (width * 8))) > +=09=09=09return -1; > + > +=09=09for (i =3D width; i > 0; i--) { > +=09=09=09buf[i - 1] =3D val & 0xff; > +=09=09=09val >>=3D 8; > +=09=09} > + > +=09=09return width; > +=09case DHCP_OPT_STR: > +=09=09slen =3D strlen(str); > + > +=09=09if (!slen || slen >=3D buf_len) > +=09=09=09return -1; > + > +=09=09memcpy(buf, str, slen); > + > +=09=09return slen; > +=09} > + > +=09return -1; > +} > =20 > /** > * dhcp_add_option() - Add or update a custom DHCP option > @@ -40,14 +205,15 @@ > * @code:=09DHCP option code > * @val_str:=09Value string from command line > * > - * If @code was already added, the previous value is overwritten. > - * Calls die() on any error. > + * Parses @val_str according to the type registered for @code in > + * dhcp_opt_types[]. If @code was already added, the previous value > + * is overwritten. Calls die() on any error. > * > * Return: 0 on success > */ > int dhcp_add_option(struct ctx *c, uint8_t code, const char *val_str) > { > -=09int idx; > +=09int idx, ret; > =20 > =09for (idx =3D 0; idx < c->custom_opts_count; idx++) { > =09=09if (c->custom_opts[idx].code =3D=3D code) > @@ -61,7 +227,15 @@ int dhcp_add_option(struct ctx *c, uint8_t code, cons= t char *val_str) > =09=09c->custom_opts_count++; > =09} > =20 > +=09ret =3D dhcp_opt_parse(code, val_str, > +=09=09=09 c->custom_opts[idx].val, > +=09=09=09 sizeof(c->custom_opts[0].val)); > +=09if (ret < 0) > +=09=09die("Invalid value for DHCP option %u: %s", > +=09=09 code, val_str); > + > =09c->custom_opts[idx].code =3D code; > +=09c->custom_opts[idx].len =3D ret; > =20 > =09if (snprintf_check(c->custom_opts[idx].str, > =09=09=09 sizeof(c->custom_opts[0].str), > diff --git a/passt.h b/passt.h > index 3a0816f..751fee3 100644 > --- a/passt.h > +++ b/passt.h > @@ -184,6 +184,8 @@ struct ip6_ctx { > * @fqdn:=09=09Guest FQDN > * @custom_opts:=09User-specified DHCP options from --dhcp-opt > * @custom_opts.code:=09DHCP option code > + * @custom_opts.len:=09Length of binary value in @val > + * @custom_opts.val:=09Binary-encoded option value But we already have struct opt opts[255] in dhcp.c, why don't you use that directly? It would have the advantage of clearly separating what belongs to the configuration proper and what belongs to the specific DHCP encoding. Besides, I see the point in maintaining what was set via command line, but not some temporary value that would just be copied to struct opt[]. This would be a third copy. > * @custom_opts.str:=09Original string value from command line > * @custom_opts_count:=09Number of entries in @custom_opts > * @ifi6:=09=09Template interface for IPv6, -1: none, 0: IPv6 disabled > @@ -271,6 +273,8 @@ struct ctx { > =20 > =09struct { > =09=09uint8_t code; > +=09=09uint8_t len; > +=09=09uint8_t val[255]; > =09=09char str[256]; > =09} custom_opts[MAX_CUSTOM_DHCP_OPTS]; > =09int custom_opts_count; --=20 Stefano