From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=YicQXOhO; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 692805A0269 for ; Tue, 02 Jun 2026 04:25:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1780367126; bh=jSWUpuU1qtD9oHDxNaN6h8XzcGgf2o6+m2ivNC/5Zbg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=YicQXOhOAWAaL0pNyLc2ElgUbgYEyCisw7ud9LTf5x/o2WAkJy+BPto7JMgR8g+LS anVLCXo7eD6X+/MDDx2CE/zhbEf0d+Z523lDa14PnNQXuCYeyCmcgyv4FIJbDxgF4N eQ5bAKj5bTeSjgFpNtoIGC896dgZ0UYSNlNvjv66Lf0ujeGlyx9t1geSmRfFotHVyK yxgOiYuDwRlV66kzmpEIs6Zd2PQNdPevxoUtyOztr6hp3qLNr9fN0ZXSyeV5rd4+8Z I/18TG93xt+D2S9RaY520f7XvB9ylKv1pbo6TM2XNLFf+nN9mTP7MdkKZUz8nNdMSn FiZPvM48bBRog== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gTvnL0kx8z4wKS; Tue, 02 Jun 2026 12:25:26 +1000 (AEST) Date: Tue, 2 Jun 2026 12:23:47 +1000 From: David Gibson To: Anshu Kumari Subject: Re: [PATCH v3 3/6] dhcp: Add option type table and value parser Message-ID: References: <20260601073758.1571317-1-anskuma@redhat.com> <20260601073758.1571317-4-anskuma@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="lBQAIsiv5yFtdH79" Content-Disposition: inline In-Reply-To: <20260601073758.1571317-4-anskuma@redhat.com> Message-ID-Hash: VA6BALA7PSHYRN3PZMHF7P2K23SAHIBL X-Message-ID-Hash: VA6BALA7PSHYRN3PZMHF7P2K23SAHIBL X-MailFrom: dgibson@gandalf.ozlabs.org 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, sbrivio@redhat.com, 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: --lBQAIsiv5yFtdH79 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jun 01, 2026 at 01:07:53PM +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. Thanks for the detailed changelogs, by the way. I know these are a bunch of work to maintain, but they really help when reviewing. > 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= =2E8.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: Unsupported or unknown option > + * @DHCP_OPT_STR: Variable-length string > + * @DHCP_OPT_IPV4: Single IPv4 address > + * @DHCP_OPT_IPV4_LIST: Multiple IPv4 addresses, comma-separated > + * @DHCP_OPT_INT8: Unsigned 8-bit integer > + * @DHCP_OPT_INT16: Unsigned 16-bit integer > + * @DHCP_OPT_INT32: Unsigned 32-bit integer > + */ > +enum dhcp_opt_type { > + DHCP_OPT_NONE, > + DHCP_OPT_STR, > + DHCP_OPT_IPV4, > + DHCP_OPT_IPV4_LIST, > + DHCP_OPT_INT8, > + DHCP_OPT_INT16, > + DHCP_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 { > + [1] =3D DHCP_OPT_IPV4, /* Subnet Mask */ > + [2] =3D DHCP_OPT_INT32, /* Time Offset */ > + [3] =3D DHCP_OPT_IPV4_LIST, /* Router */ I'm still a bit unsure if we want to allow user modification of the options, like this one, which we already manage ourselves. > + [4] =3D DHCP_OPT_IPV4_LIST, /* Time Server */ > + [5] =3D DHCP_OPT_IPV4_LIST, /* Name Server */ > + [6] =3D DHCP_OPT_IPV4_LIST, /* Domain Name Server */ > + [7] =3D DHCP_OPT_IPV4_LIST, /* Log Server */ > + [8] =3D DHCP_OPT_IPV4_LIST, /* Cookie Server */ > + [9] =3D DHCP_OPT_IPV4_LIST, /* LPR Server */ > + [10] =3D DHCP_OPT_IPV4_LIST, /* Impress Server */ > + [11] =3D DHCP_OPT_IPV4_LIST, /* Resource Location Server */ > + [12] =3D DHCP_OPT_STR, /* Host Name */ > + [13] =3D DHCP_OPT_INT16, /* Boot File Size */ > + [15] =3D DHCP_OPT_STR, /* Domain Name */ > + [16] =3D DHCP_OPT_IPV4, /* Swap Server */ > + [17] =3D DHCP_OPT_STR, /* Root Path */ > + [19] =3D DHCP_OPT_INT8, /* IP Forwarding */ > + [23] =3D DHCP_OPT_INT8, /* Default IP TTL */ > + [26] =3D DHCP_OPT_INT16, /* Interface MTU */ > + [28] =3D DHCP_OPT_IPV4, /* Broadcast Address */ > + [37] =3D DHCP_OPT_INT8, /* TCP Default TTL */ > + [38] =3D DHCP_OPT_INT32, /* TCP Keepalive Interval */ > + [40] =3D DHCP_OPT_STR, /* NIS Domain Name */ > + [41] =3D DHCP_OPT_IPV4_LIST, /* NIS Servers */ > + [42] =3D DHCP_OPT_IPV4_LIST, /* NTP Servers */ > + [44] =3D DHCP_OPT_IPV4_LIST, /* NetBIOS Name Server */ > + [50] =3D DHCP_OPT_IPV4, /* Requested IP Address */ > + [51] =3D DHCP_OPT_INT32, /* IP Address Lease Time */ > + [53] =3D DHCP_OPT_INT8, /* DHCP Message Type */ > + [54] =3D DHCP_OPT_IPV4, /* Server Identifier */ > + [57] =3D DHCP_OPT_INT16, /* Max DHCP Message Size */ > + [58] =3D DHCP_OPT_INT32, /* Renewal (T1) Time */ > + [59] =3D DHCP_OPT_INT32, /* Rebinding (T2) Time */ > + [60] =3D DHCP_OPT_STR, /* Vendor Class Identifier */ > + [66] =3D DHCP_OPT_STR, /* TFTP Server Name */ > + [67] =3D DHCP_OPT_STR, /* Bootfile Name */ > + [252] =3D DHCP_OPT_STR, /* WPAD URL */ > +}; > + > +/** > + * dhcp_opt_parse() - Parse a DHCP option value > + * @code: DHCP option code > + * @str: Value string from command line > + * @buf: Output buffer for binary value > + * @buf_len: Size 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, > + uint8_t *buf, size_t buf_len) > +{ > + enum dhcp_opt_type type =3D dhcp_opt_types[code]; > + char *tok, *saveptr, *end; > + struct in_addr addr; > + unsigned long val; > + unsigned int i; > + uint8_t width; > + char tmp[256]; > + size_t slen; > + int len; > + > + if (!*str) > + die("Empty value for DHCP option %u", code); > + > + switch (type) { > + case DHCP_OPT_NONE: > + die("Unsupported DHCP option: %u," > + " see passt(1) for supported codes", code); > + case DHCP_OPT_IPV4: > + case DHCP_OPT_IPV4_LIST: > + len =3D 0; > + > + /* Reject empty, leading/trailing, or consecutive commas */ > + if (!*str || *str =3D=3D ',' || str[strlen(str) - 1] =3D=3D ',' || > + strstr(str, ",,")) > + return -1; > + > + if (snprintf_check(tmp, sizeof(tmp), "%s", str)) > + return -1; The arbitrary 256 byte buffer limit here isn't great. The string encoding of an IPv4 address can be nearly 4 times as long as the binary encoding, so we could potentially hit this with a longish, but not ridiculous address list. > + for (tok =3D strtok_r(tmp, ",", &saveptr); tok; > + tok =3D strtok_r(NULL, ",", &saveptr)) { One way to avoid that would be to avoid using strtok_r() which relies in in-place modifying the input. Instead you'd need to repeatedly find the length of the next chunk with strchr() or strcpsn(), then extract each one into a tmp of length INET_ADDRSTRLEN to call inet_pton(). On the plus side, that should naturally deal with the case of extraneous commas (it would show up as an empth entry), rather than requiring an explicit check at the top. > + if (inet_pton(AF_INET, tok, &addr) !=3D 1) > + return -1; > + > + if (len + (int)sizeof(addr) > (int)buf_len) > + return -1; You could make this check before the inet_pton(), then do the conversion directly into buf, avoiding the addr temporary. > + > + memcpy(buf + len, &addr, sizeof(addr)); > + len +=3D sizeof(addr); > + > + if (type =3D=3D DHCP_OPT_IPV4) > + break; > + } > + > + if (type =3D=3D DHCP_OPT_IPV4 && strtok_r(NULL, ",", &saveptr)) > + return -1; > + > + return len; > + case DHCP_OPT_INT8: > + case DHCP_OPT_INT16: > + case DHCP_OPT_INT32: > + if (type =3D=3D DHCP_OPT_INT8) > + width =3D 1; > + else if (type =3D=3D DHCP_OPT_INT16) > + width =3D 2; > + else > + width =3D 4; > + > + errno =3D 0; > + val =3D strtoul(str, &end, 0); > + > + if (*end || errno) > + return -1; > + > + if (buf_len < width) > + return -1; > + > + if (val >=3D (1ULL << (width * 8))) > + return -1; > + > + for (i =3D width; i > 0; i--) { > + buf[i - 1] =3D val & 0xff; > + val >>=3D 8; > + } > + > + return width; > + case DHCP_OPT_STR: > + slen =3D strlen(str); > + > + if (!slen || slen >=3D buf_len) > + return -1; > + > + memcpy(buf, str, slen); Do you need to include the terminating \0 here? If so you'll need slen + 1. > + > + return slen; > + } > + > + return -1; > +} > =20 > /** > * dhcp_add_option() - Add or update a custom DHCP option > @@ -40,14 +205,15 @@ > * @code: DHCP option code > * @val_str: Value 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) > { > - int idx; > + int idx, ret; > =20 > for (idx =3D 0; idx < c->custom_opts_count; idx++) { > if (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) > c->custom_opts_count++; > } > =20 > + ret =3D dhcp_opt_parse(code, val_str, > + c->custom_opts[idx].val, > + sizeof(c->custom_opts[0].val)); Now that this parsing and adding code is all in dhcp.c, could we parse the options directly into the existing opts[] global, rather than requiring both the string and parsed forms in c->custom_opts? > + if (ret < 0) > + die("Invalid value for DHCP option %u: %s", > + code, val_str); > + > c->custom_opts[idx].code =3D code; > + c->custom_opts[idx].len =3D ret; > =20 > if (snprintf_check(c->custom_opts[idx].str, > 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: Guest FQDN > * @custom_opts: User-specified DHCP options from --dhcp-opt > * @custom_opts.code: DHCP option code > + * @custom_opts.len: Length of binary value in @val > + * @custom_opts.val: Binary-encoded option value > * @custom_opts.str: Original string value from command line > * @custom_opts_count: Number of entries in @custom_opts > * @ifi6: Template interface for IPv6, -1: none, 0: IPv6 disabled > @@ -271,6 +273,8 @@ struct ctx { > =20 > struct { > uint8_t code; > + uint8_t len; > + uint8_t val[255]; > char str[256]; > } custom_opts[MAX_CUSTOM_DHCP_OPTS]; > int custom_opts_count; > --=20 > 2.54.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --lBQAIsiv5yFtdH79 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmoePrIACgkQzQJF27ox 2Gfi0xAAiZsEaheULg7o8TZnMROHyu0H30Omm9ceueHTUxhi36qG4UaVl2c0ONXP AV8TW/b+joR2kojji0uTocyyoXOGgWqVLZD7QHam+2jFm4YF+0eMD83xaRGrYawL Jx+aPqxTht8LoIQcR/tqVIezKu7MagJ5E9AVAiGulhRnlKk197WBTB9GXofUu1fX TpuXtQcsq8/nvoHA3q2S1Odn9Lj/BtMiW2C5cHSMhZPvdEt7evWa3jBC1Rzi/ZXf aqh+51308WmWExn9UQkVea8GiE9jZc6Nw83i2UyNPn/FALNn5Tc5OfOhzGGeZyqI kOJUgDFRpzd+UkqFkyLAknW2QlHATKX9aUa/qndVuKaArs2gIbbPI9YaV+nW8Ns5 oAWzFo5R8Q6tx8lYVi5DEcoaJ/92q+RMd5lcPSP3IdzwPbiEPTpmdMs8Bpk8/eqR 9X1VgtU8Z49gWXpginJFqc6rJv/gRvccKCRgZX+71+PycVDLrg0DPP2VBvkonkP5 tIxMacwbbrOH8Mzg5V1N2CWjJ9Rtrv8Mqr6Y5UE6Ka22j2fiFluYnn+w/bOcjxId LvGvRFuq+LxPTMkf7cULGav8kQwDoddc4GCepexE2OL9LIAEM6wKQhfu2SQcmdhY tHfaO0hLwoCwI42Rt/DiRlhSehn5zKfCae7rlPe8qa5wmgy4pqg= =me1J -----END PGP SIGNATURE----- --lBQAIsiv5yFtdH79--