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=Fs2utdlh; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id E6A075A0269 for ; Thu, 10 Sep 2026 09:04:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1789023866; 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=i059exxw0UPp/+NkNJXJ6If8xm+7p2k+XM0xnpGIzCA=; b=Fs2utdlhVllMd3omis47MBmfIJWz60YAzF0pt88QiV1KMJ+QJ3K2RId2JpvosH0hNb5kLk 251Utz1lGEbSFOzfKpI9V490cRwJlw6gXAeqfDhQ88/5/kKkYn53ddTTaDGqZLgPltc7nX kI4N9SbxUWEn1pkjZTBpWI1fIrERS48= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-178-n4Tomv58PkmGPpyxKzEYWg-1; Thu, 10 Sep 2026 03:04:24 -0400 X-MC-Unique: n4Tomv58PkmGPpyxKzEYWg-1 X-Mimecast-MFC-AGG-ID: n4Tomv58PkmGPpyxKzEYWg_1789023864 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-49cf4cc2125so34280095e9.2 for ; Thu, 10 Sep 2026 00:04:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789023863; x=1789628663; h=date:content-transfer-encoding:content-type: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:content-type; bh=OykwneMgCuhy+yNByziSaUlo0cjXp9TDaCwlA0IotBQ=; b=Z4xZ7UHl5RkCqDINi8FkzQtzofMsR6nNrLOhMMyAj42Sm0fXbXran5wFHKwfpV+ogN jvk4Fj0QKit33jeWIbQyIjsFac6TjwMGbks4C8nOQ6PbxGKyc0c+fSqN/ksZ5L8FRIDh Wy1qxUEGY+eJe35Pciwy1ks+WygJO9ybq+1OEvG8ffxf6GLgHjCVz0spy02zNT7HC2jM J44kP62eb49QaL8ipPtLMFThYd6tR/5p/4kWGnxvg7FOfyMPW4obmRMtG2bskYqtRXzB 8NptyjClgl5Tn8/yu9BXVTfQpoBhjeJyXnF+bTsPyFPeKVYA8AkpAAOgUhohTfIg1nIZ E0XQ== X-Gm-Message-State: AFuF++k+qP0lVIwo8J20izP0HOuQvVb+ZU4FsnIoVsploZXErR9/O52j ZbBx44DXhoKqJY8SOlc3294QVKlWXJ3AhAn99cJANiQW9P/gFyX3dh9/FnJsjnBg0HPzcPlEhrU OiQfWVr9VkOpOdLL+V2xTZmtfkA4vGQOfXCLySDYsqdpDbTN/gGOLEw== X-Gm-Gg: AYBFou33U0XpE5iWNuqAjWh8vCj/gmb3thaJeTeovTeZtu8Go0MIejy0B4DlaPlVHjM sLHLKZ+88YgKRqvVIOUDlgnrFKtc2MZf3ghSV3+6RgJPa633yYspdlr7Ik00tM61FlFKnRPW7iS PzivlpU6RHlK+U7zhCIj+avQ4fGRjI/AYvEjfw3oJFRtjcVPoAdo4gH+7STsUgbj4L62BYEdLfK MUh+7NskrEcnlIx5cTtsPrGL8322sJlI0tWuavQhtVdZD/XsUIiPEoY04i+zSciw5Jiz5vyXwn6 iaJ5VvQW6hLSSsJnSTa5j9LqEtV+wgO+Ny7nSVvC9hEAA8ucF/IeXYhhlJ+TvoaDrLFEdS0G1dx 9jgXPXGUM7uA= X-Received: by 2002:a05:600c:8b74:b0:49d:2555:1a6d with SMTP id 5b1f17b1804b1-49d25551fc4mr113027135e9.16.1789023863270; Thu, 10 Sep 2026 00:04:23 -0700 (PDT) X-Received: by 2002:a05:600c:8b74:b0:49d:2555:1a6d with SMTP id 5b1f17b1804b1-49d25551fc4mr113025515e9.16.1789023862627; Thu, 10 Sep 2026 00:04:22 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [2a10:fc81:a806:d6a9::1]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49d20da3354sm88596195e9.2.2026.09.10.00.04.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2026 00:04:21 -0700 (PDT) From: Stefano Brivio To: Anshu Kumari Subject: Re: [PATCH v6 4/6] dhcp: Add --dhcp-opt with option table and value parser Message-ID: <20260910090419.50d1d57a@elisabeth> In-Reply-To: <20260824134436.282300-5-anskuma@redhat.com> References: <20260824134436.282300-1-anskuma@redhat.com> <20260824134436.282300-5-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: Thu, 10 Sep 2026 09:04:20 +0200 (CEST) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: E3B1J6QdH0YF6ugCC8mSMymgFEoYME4OYvgadfDw0DE_1789023864 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: GXPJP3Z7D5RF7PDIQK2EI6XW6YYVU7F3 X-Message-ID-Hash: GXPJP3Z7D5RF7PDIQK2EI6XW6YYVU7F3 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, 24 Aug 2026 19:14:32 +0530 Anshu Kumari wrote: > Introduce the --dhcp-opt flag that allows setting arbitrary DHCP > options from command-line in the form [--dhcp-opt CODE,VALUE]. >=20 > Add a type lookup table mapping option codes to RFC 2132 value types > (IPv4, IPv4 list, integer, string) and dhcp_opt_parse() to convert > CLI strings to binary wire format. Parsed options are stored in > struct opts[] and injected into DHCP replies. If the same option code > is given more than once, the last value wins. >=20 > Link: https://bugs.passt.top/show_bug.cgi?id=3D192 > Signed-off-by: Anshu Kumari > --- > v6: > - dropped option 53. > - Used parse_unsigned(), parse_literal(), parse_ipv4() > from parse.c instead of manual strtoul/inet_pton. > - Moved option-parsing variables into case 34 block > scope. > - Used htons()/htonl() for integer encoding. > - Used assert() for boundary checks in dhcp_opt_to_str(). > - Used snprintf() with %.*s for string formatting in > dhcp_opt_to_str(). > - Used sizeof(struct in_addr) instead of literal 4. > - dhcp_opt_to_str() checks OPT_UNSET for all states, > not just OPT_USER. > - Added curly brackets for multi-line conditionals. > - Dropped trailing comma after OPT_DEFAULT in > initializers to reduce line length. >=20 > v5: > - Rename enum values: INT8/INT16/INT32/SINT32 =E2=86=92 UINT8/UINT16/UI= NT32/INT32 > - Change int len to size_t len in dhcp_opt_parse(), remove int casts > - Make dhcp_opt_parse() static, add dhcp_set_opt() to store options=20 > directly in opts[] at startup instead of double-parsing via ctx->dhcp= _opts > - Add OPT_USER to enum opt_state to protect user options from being > overwritten by built-in defaults > - Add dhcp_opt_to_str() for conf_print() display > - Remove dhcp_opts[] from struct ctx in passt.h > - Remove conf_dhcp_option() wrapper from conf.c >=20 > v4: > - Renamed custom_opts to dhcp_opts, 256 entries indexed by option > code, removed MAX_CUSTOM_DHCP_OPTS and count field. > - Changed str buffer from 256 to 255 bytes. > - Moved function to conf.c as static conf_dhcp_option(), renamed > from dhcp_add_option(). > - Made dhcp_opt_parse() non-static, declared in dhcp.h > - Dropped val/len from ctx struct; conf_dhcp_option() validates > with temp buffer, dhcp() parses str directly into opts[] at > reply time. > - Replaced strtok_r() + 256-byte buffer with strcspn() + > INET_ADDRSTRLEN buffer. > - Added DHCP_OPT_SINT32 for option 2 (Time Offset), uses strtol() > per RFC 2132 Section 8.2. > - All errors in dhcp_opt_parse() return -1, removed die() calls; > caller handles error message consistently. > - Removed redundant !slen check in DHCP_OPT_STR case. > - Omitted explicit array size for dhcp_opt_types[], arraydded bounds > check before lookup. > - Added errno =3D 0 + errno check for strtoul() in case 34. > - Fixed usage text: "Set DHCP option CODE to VAL". > - Improved man page: added format description and examples >=20 > 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= . > --- > conf.c | 26 ++++- > dhcp.c | 327 +++++++++++++++++++++++++++++++++++++++++++++++++++----- > dhcp.h | 2 + > passt.1 | 42 ++++++++ > 4 files changed, 369 insertions(+), 28 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index faf2681..e5f1108 100644 > --- a/conf.c > +++ b/conf.c > @@ -47,6 +47,7 @@ > #include "lineread.h" > #include "isolation.h" > #include "log.h" > +#include "dhcp.h" > #include "vhost_user.h" > #include "epoll_ctl.h" > #include "conf.h" > @@ -632,7 +633,8 @@ static void usage(const char *name, FILE *f, int stat= us) > =09=09" -S, --search LIST=09Space-separated list, search domains\n" > =09=09" a single, empty option disables the DNS search list\n" > =09=09" -H, --hostname NAME =09Hostname to configure client with\n" > -=09=09" --fqdn NAME=09=09FQDN to configure client with\n"); > +=09=09" --fqdn NAME=09=09FQDN to configure client with\n" > +=09=09" --dhcp-opt CODE,VAL=09Set DHCP option CODE to VAL\n"); > =09if (strstr(name, "pasta")) > =09=09FPRINTF(f, " default: don't use any search list\n"); > =09else > @@ -860,6 +862,9 @@ static void conf_print(const struct ctx *c) > =09=09=09info(" router: %s", > =09=09=09 inet_ntop(AF_INET, &c->ip4.guest_gw, > =09=09=09=09 buf, sizeof(buf))); > +=09=09=09for (i =3D 1; i < 255; i++) Same as my comment to 2/6: curly brackets would be nice here. > +=09=09=09=09if (dhcp_opt_to_str(i, buf, sizeof(buf))) > +=09=09=09=09=09info(" option %u: %s", i, buf); > =09=09} > =20 > =09=09for (i =3D 0; i < ARRAY_SIZE(c->ip4.dns); i++) { > @@ -1348,6 +1353,7 @@ void conf(struct ctx *c, int argc, char **argv) > =09=09{"stats", required_argument,=09=09NULL,=09=0931 }, > =09=09{"conf-path",=09required_argument,=09NULL,=09=09'c' }, > =09=09{"chroot-fallback", no_argument,=09NULL, =09=0932 }, > +=09=09{"dhcp-opt", required_argument,=09=09NULL,=09=0934 }, > =09=09{ 0 }, > =09}; > =09const char *optstring =3D "+dqfel:hs:c:F:I:p:P:m:a:n:M:g:i:o:D:S:H:46= 1t:u:T:U:"; > @@ -1589,6 +1595,24 @@ void conf(struct ctx *c, int argc, char **argv) > =09=09case 32: > =09=09=09c->chroot_fallback =3D true; > =09=09=09break; > +=09=09case 34: { > +=09=09=09unsigned long optcode; > + > +=09=09=09p =3D optarg; > + > +=09=09=09if (!parse_unsigned(&p, 0, &optcode) || > +=09=09=09 !parse_literal(&p, ",")) { > +=09=09=09=09die("--dhcp-opt requires CODE,VALUE format"); > +=09=09=09} > + > +=09=09=09if (optcode < 1 || optcode > 254) { > +=09=09=09=09die("DHCP option code must be 1-254: %s", > +=09=09=09=09 optarg); > +=09=09=09} > + > +=09=09=09dhcp_set_opt(optcode, p); > +=09=09=09break; > +=09=09} > =09=09case 'd': > =09=09=09c->debug =3D 1; > =09=09=09c->quiet =3D 0; > diff --git a/dhcp.c b/dhcp.c > index 02c7744..43ce133 100644 > --- a/dhcp.c > +++ b/dhcp.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include > =20 > #include "util.h" > #include "ip.h" > @@ -32,15 +33,18 @@ > #include "tap.h" > #include "log.h" > #include "dhcp.h" > +#include "parse.h" > =20 > /** > * enum opt_state - DHCP option state > * @OPT_UNSET:=09=09Option not configured > * @OPT_DEFAULT:=09Option derived from host configuration > + * @OPT_USER:=09=09Option set via --dhcp-opt command line > */ > enum opt_state { > =09OPT_UNSET =3D 0, > =09OPT_DEFAULT, > +=09OPT_USER, > }; > =20 > /** > @@ -50,7 +54,7 @@ enum opt_state { > * @s:=09=09Option payload from server > * @clen:=09Length of option received from client, -1 if not received > * @c:=09=09Option payload from client > - * @state:=09Option state (unset or default) > + * @state:=09Option state (unset, default, or user) > */ > struct opt { > =09int sent; > @@ -85,17 +89,24 @@ static struct opt opts[256]; > */ > void dhcp_init(void) > { > -=09/* Mask */ > -=09opts[1] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > -=09/* Router */ > -=09opts[3] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > -=09/* Lease time */ > -=09opts[51] =3D (struct opt) { 0, 4, { 0xff, 0xff, 0xff, 0xff }, > -=09=09=09=09=09=09 0, { 0 }, OPT_DEFAULT }; > +=09int i; > + > +=09for (i =3D 0; i < ARRAY_SIZE(opts); i++) > +=09=09if (opts[i].state !=3D OPT_USER) > +=09=09=09opts[i].state =3D OPT_UNSET; > + > +=09if (opts[1].state !=3D OPT_USER)=09=09=09/* Mask */ > +=09=09opts[1] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT }; > +=09if (opts[3].state !=3D OPT_USER)=09=09=09/* Router */ > +=09=09opts[3] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT }; > +=09if (opts[51].state !=3D OPT_USER)=09=09=09/* Lease time */ > +=09=09opts[51] =3D (struct opt) { 0, 4, { 0xff, 0xff, 0xff, 0xff }, > +=09=09=09=09=09 0, { 0 }, OPT_DEFAULT }; > =09/* Type */ > -=09opts[53] =3D (struct opt) { 0, 1, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > -=09/* Server ID */ > -=09opts[54] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > +=09opts[53] =3D (struct opt) { 0, 1, { 0 }, 0, { 0 }, OPT_DEFAULT }; > +=09if (opts[54].state !=3D OPT_USER)=09=09=09/* Server ID */ Same here (curly brackets). > +=09=09opts[54] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, > +=09=09=09=09=09=09=09OPT_DEFAULT }; > } > =20 > /** > @@ -140,6 +151,258 @@ struct msg { > =09uint8_t o[OPT_MAX + 1 /* End option */ ]; > } __attribute__((__packed__)); > =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_UINT8:=09Unsigned 8-bit integer > + * @DHCP_OPT_UINT16:=09Unsigned 16-bit integer > + * @DHCP_OPT_UINT32:=09Unsigned 32-bit integer > + * @DHCP_OPT_INT32:=09Signed 32-bit integer > + */ > +enum dhcp_opt_type { > +=09DHCP_OPT_NONE, > +=09DHCP_OPT_STR, > +=09DHCP_OPT_IPV4, > +=09DHCP_OPT_IPV4_LIST, > +=09DHCP_OPT_UINT8, > +=09DHCP_OPT_UINT16, > +=09DHCP_OPT_UINT32, > +=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[] =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_UINT16,=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_UINT8,=09=09/* IP Forwarding */ > +=09[23] =3D DHCP_OPT_UINT8,=09=09/* Default IP TTL */ > +=09[26] =3D DHCP_OPT_UINT16,=09/* Interface MTU */ > +=09[28] =3D DHCP_OPT_IPV4,=09=09/* Broadcast Address */ > +=09[37] =3D DHCP_OPT_UINT8,=09=09/* TCP Default TTL */ > +=09[38] =3D DHCP_OPT_UINT32,=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_UINT32,=09/* IP Address Lease Time */ > +=09[54] =3D DHCP_OPT_IPV4,=09=09/* Server Identifier */ > +=09[57] =3D DHCP_OPT_UINT16,=09/* Max DHCP Message Size */ > +=09[58] =3D DHCP_OPT_UINT32,=09/* Renewal (T1) Time */ > +=09[59] =3D DHCP_OPT_UINT32,=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; > +=09unsigned long val; > +=09uint8_t width; > +=09size_t slen; > +=09size_t len; > +=09char *end; > + > +=09if (code >=3D ARRAY_SIZE(dhcp_opt_types)) > +=09=09return -1; > + > +=09type =3D dhcp_opt_types[code]; > + > +=09if (!*str) > +=09=09return -1; > + > +=09switch (type) { > +=09case DHCP_OPT_NONE: > +=09=09return -1; > +=09case DHCP_OPT_IPV4: > +=09case DHCP_OPT_IPV4_LIST: > +=09=09len =3D 0; > + > +=09=09do { > +=09=09=09if (len + sizeof(struct in_addr) > buf_len) > +=09=09=09=09return -1; > + > +=09=09=09if (!parse_ipv4(&str, > +=09=09=09=09=09(struct in_addr *)(buf + len))) > +=09=09=09=09return -1; > + > +=09=09=09len +=3D sizeof(struct in_addr); > + > +=09=09=09if (type =3D=3D DHCP_OPT_IPV4) > +=09=09=09=09break; > +=09=09} while (parse_literal(&str, ",")); > + > +=09=09if (!len || !parse_eoi(str)) > +=09=09=09return -1; > + > +=09=09return len; > +=09case DHCP_OPT_UINT8: > +=09case DHCP_OPT_UINT16: > +=09case DHCP_OPT_UINT32: > +=09case DHCP_OPT_INT32: > +=09=09if (type =3D=3D DHCP_OPT_UINT8) > +=09=09=09width =3D 1; > +=09=09else if (type =3D=3D DHCP_OPT_UINT16) > +=09=09=09width =3D 2; > +=09=09else > +=09=09=09width =3D 4; > + > +=09=09if (buf_len < width) > +=09=09=09return -1; > + > +=09=09errno =3D 0; > +=09=09if (type =3D=3D DHCP_OPT_INT32) { > +=09=09=09long sval; > + > +=09=09=09sval =3D strtol(str, &end, 0); > +=09=09=09if (*end || errno || > +=09=09=09 sval < INT32_MIN || sval > INT32_MAX) > +=09=09=09=09return -1; > +=09=09=09val =3D (uint32_t)sval; > +=09=09} else { > +=09=09=09val =3D strtoul(str, &end, 0); > +=09=09=09if (*end || errno || > +=09=09=09 val >=3D (1ULL << (width * 8))) > +=09=09=09=09return -1; > +=09=09} > + > +=09=09if (type =3D=3D DHCP_OPT_UINT16) > +=09=09=09*(uint16_t *)buf =3D htons(val); > +=09=09else if (type =3D=3D DHCP_OPT_UINT32 || > +=09=09=09 type =3D=3D DHCP_OPT_INT32) > +=09=09=09*(uint32_t *)buf =3D htonl(val); > +=09=09else > +=09=09=09buf[0] =3D val; > + > +=09=09return width; > +=09case DHCP_OPT_STR: > +=09=09slen =3D strlen(str); > + > +=09=09if (slen >=3D buf_len) > +=09=09=09return -1; > + > +=09=09memcpy(buf, str, slen); > + > +=09=09return slen; > +=09} > + > +=09return -1; > +} > + > +/** > + * dhcp_set_opt() - Parse and store a user-specified DHCP option > + * @code:=09DHCP option code > + * @val_str:=09Value string from command line > + */ > +void dhcp_set_opt(uint8_t code, const char *val_str) > +{ > +=09int ret; > + > +=09ret =3D dhcp_opt_parse(code, val_str, opts[code].s, sizeof(opts[code]= .s)); > +=09if (ret < 0) > +=09=09die("Invalid value for DHCP option %u: %s", code, val_str); > + > +=09opts[code].slen =3D ret; > +=09opts[code].state =3D OPT_USER; > +} > + > +/** > + * dhcp_opt_to_str() - Render a binary DHCP option value to a printable = string > + * @code:=09DHCP option code > + * @buf:=09Output string buffer > + * @buf_len:=09Size of output buffer > + * > + * Return: pointer to @buf if option is set, NULL otherwise > + */ > +const char *dhcp_opt_to_str(uint8_t code, char *buf, size_t buf_len) > +{ > +=09enum dhcp_opt_type type; > +=09unsigned int i; > +=09int off =3D 0; > + > +=09if (opts[code].state =3D=3D OPT_UNSET) > +=09=09return NULL; > + > +=09assert(code < ARRAY_SIZE(dhcp_opt_types)); > + > +=09type =3D dhcp_opt_types[code]; > + > +=09switch (type) { > +=09case DHCP_OPT_IPV4: > +=09case DHCP_OPT_IPV4_LIST: > +=09=09for (i =3D 0; i + sizeof(struct in_addr) <=3D (unsigned int)opts[c= ode].slen; > +=09=09 i +=3D sizeof(struct in_addr)) { > +=09=09=09if (off) > +=09=09=09=09off +=3D snprintf(buf + off, buf_len - off, ","); > +=09=09=09inet_ntop(AF_INET, opts[code].s + i, > +=09=09=09=09 buf + off, buf_len - off); > +=09=09=09off =3D strlen(buf); > +=09=09} > +=09=09return buf; > +=09case DHCP_OPT_UINT8: > +=09case DHCP_OPT_UINT16: > +=09case DHCP_OPT_UINT32: { > +=09=09unsigned val =3D 0; > +=09=09int j; > + > +=09=09for (j =3D 0; j < opts[code].slen; j++) > +=09=09=09val =3D (val << 8) | opts[code].s[j]; > + > +=09=09if (snprintf(buf, buf_len, "%u", val) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09} > +=09case DHCP_OPT_INT32: { > +=09=09int val =3D 0; > +=09=09int j; > + > +=09=09for (j =3D 0; j < opts[code].slen; j++) > +=09=09=09val =3D (val << 8) | opts[code].s[j]; > + > +=09=09if (snprintf(buf, buf_len, "%d", val) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09} > +=09case DHCP_OPT_STR: > +=09=09(void)snprintf(buf, buf_len, "%.*s", > +=09=09=09 opts[code].slen, opts[code].s); > +=09=09return buf; > +=09default: > +=09=09assert(0); > +=09} > +} > + > /** > * fill_one() - Fill a single option into a buffer > * @buf:=09Buffer to write option > @@ -474,14 +737,20 @@ int dhcp(const struct ctx *c, struct iov_tail *data= ) > =09info(" from %s", eth_ntop(m->chaddr, macstr, sizeof(macstr))); > =20 > =09mask.s_addr =3D htonl(0xffffffff << (32 - c->ip4.prefix_len)); > -=09memcpy(opts[1].s, &mask, sizeof(mask)); > -=09memcpy(opts[3].s, &c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); > -=09memcpy(opts[54].s, &c->ip4.our_tap_addr, sizeof(c->ip4.our_tap_addr))= ; > +=09if (opts[1].state !=3D OPT_USER) > +=09=09memcpy(opts[1].s, &mask, sizeof(mask)); > +=09if (opts[3].state !=3D OPT_USER) > +=09=09memcpy(opts[3].s, &c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); > +=09if (opts[54].state !=3D OPT_USER) { > +=09=09memcpy(opts[54].s, &c->ip4.our_tap_addr, > +=09=09 sizeof(c->ip4.our_tap_addr)); > +=09} > =20 > =09/* If the gateway is not on the assigned subnet, send an option 121 > =09 * (Classless Static Routing) adding a dummy route to it. > =09 */ > -=09if ((c->ip4.addr.s_addr & mask.s_addr) > +=09if (opts[121].state !=3D OPT_USER && > +=09 (c->ip4.addr.s_addr & mask.s_addr) > =09 !=3D (c->ip4.guest_gw.s_addr & mask.s_addr)) { > =09=09/* a.b.c.d/32:0.0.0.0, 0:a.b.c.d */ > =09=09opts[121].slen =3D 14; > @@ -493,27 +762,31 @@ int dhcp(const struct ctx *c, struct iov_tail *data= ) > =09=09 &c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); > =09} > =20 > -=09if (c->mtu) { > +=09if (opts[26].state !=3D OPT_USER && c->mtu) { > =09=09opts[26].slen =3D 2; > =09=09opts[26].state =3D OPT_DEFAULT; > =09=09opts[26].s[0] =3D c->mtu / 256; > =09=09opts[26].s[1] =3D c->mtu % 256; > =09} > =20 > -=09for (i =3D 0, opts[6].slen =3D 0; > -=09 !c->no_dhcp_dns && i < ARRAY_SIZE(c->ip4.dns); i++) { > -=09=09if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i])) > -=09=09=09break; > -=09=09((struct in_addr *)opts[6].s)[i] =3D c->ip4.dns[i]; > -=09=09opts[6].slen +=3D sizeof(uint32_t); > +=09if (opts[6].state !=3D OPT_USER) { > +=09=09opts[6].slen =3D 0; > +=09=09for (i =3D 0; > +=09=09 !c->no_dhcp_dns && i < ARRAY_SIZE(c->ip4.dns); > +=09=09 i++) { > +=09=09=09if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i])) > +=09=09=09=09break; > +=09=09=09((struct in_addr *)opts[6].s)[i] =3D c->ip4.dns[i]; > +=09=09=09opts[6].slen +=3D sizeof(uint32_t); > +=09=09} > +=09=09if (opts[6].slen) > +=09=09=09opts[6].state =3D OPT_DEFAULT; > +=09=09else > +=09=09=09opts[6].state =3D OPT_UNSET; > =09} > -=09if (opts[6].slen) > -=09=09opts[6].state =3D OPT_DEFAULT; > -=09else > -=09=09opts[6].state =3D OPT_UNSET; > =20 > =09opt_len =3D strlen(c->hostname); > -=09if (opt_len > 0) { > +=09if (opts[12].state !=3D OPT_USER && opt_len > 0) { > =09=09opts[12].slen =3D opt_len; > =09=09opts[12].state =3D OPT_DEFAULT; > =09=09memcpy(opts[12].s, &c->hostname, opt_len); > diff --git a/dhcp.h b/dhcp.h > index cd50c99..80abda6 100644 > --- a/dhcp.h > +++ b/dhcp.h > @@ -8,5 +8,7 @@ > =20 > int dhcp(const struct ctx *c, struct iov_tail *data); > void dhcp_init(void); > +void dhcp_set_opt(uint8_t code, const char *val_str); > +const char *dhcp_opt_to_str(uint8_t code, char *buf, size_t buf_len); > =20 > #endif /* DHCP_H */ > diff --git a/passt.1 b/passt.1 > index 53e072a..b4a44c5 100644 > --- a/passt.1 > +++ b/passt.1 > @@ -440,6 +440,48 @@ Send \fIname\fR as DHCP option 12 (hostname). > FQDN to configure the client with. > Send \fIname\fR as Client FQDN: DHCP option 81 and DHCPv6 option 39. > =20 > +.TP > +.BR \-\-dhcp-opt " " \fICODE\fR,\fIVALUE\fR > +Set DHCP option \fICODE\fR (1\-254) to \fIVALUE\fR. The value format dep= ends > +on the option type and is determined automatically from the option code. > +Multiple IPv4 addresses are comma-separated. > +This option can be specified multiple times. If the same option code is > +given more than once, the last value wins. Options set with > +\fB\-\-dhcp-opt\fR override built-in values. > +.PP > +Examples: > +.nf > + \-\-dhcp-opt 6,8.8.8.8,4.4.4.4 > + \-\-dhcp-opt 12,myhostname > +.fi > +.PP > +Only the following option codes are supported (unsupported codes cause a= n error): > +.RS > +.TP > +.B IPv4 address options > +1 (Subnet Mask), 16 (Swap Server), 28 (Broadcast Address), 50 (Requested= IP), > +54 (Server Identifier) > +.TP > +.B IPv4 address list options (comma-separated) > +3 (Router), 4 (Time Server), 5 (Name Server), 6 (DNS), 7 (Log Server), > +8 (Cookie Server), 9 (LPR Server), 10 (Impress Server), > +11 (Resource Location Server), 41 (NIS Servers), > +42 (NTP Servers), 44 (NetBIOS Name Server) > +.TP > +.B Integer options > +2 (Time Offset, 32-bit), 13 (Boot File Size, 16-bit), 19 (IP Forwarding,= 8-bit), > +23 (Default IP TTL, 8-bit), 26 (Interface MTU, 16-bit), > +37 (TCP Default TTL, 8-bit), 38 (TCP Keepalive Interval, 32-bit), > +51 (IP Address Lease Time, 32-bit), > +57 (Max DHCP Message Size, 16-bit), > +58 (Renewal Time, 32-bit), 59 (Rebinding Time, 32-bit) > +.TP > +.B String options > +12 (Host Name), 15 (Domain Name), 17 (Root Path), 40 (NIS Domain Name), > +60 (Vendor Class Identifier), 66 (TFTP Server Name), > +67 (Bootfile Name), 252 (WPAD URL) > +.RE > + > .TP > .BR \-t ", " \-\-tcp-ports " " \fIspec > Configure TCP port forwarding to guest or namespace. \fIspec\fR can be e= ither: --=20 Stefano