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=ZRVjrDTr; 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 7F5A35A0269 for ; Mon, 27 Jul 2026 08:19:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785133167; 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=zRQz4GKNpzLQ7fC8lTZGsSqFhH6buUqOIzHUl0EeP8I=; b=ZRVjrDTrplXdmNyvSVIw3kSfKnP3IHeodid/oYhvqyj5wfMA++es4Ht2mmnFYnqpd6CzV1 4Ba2rwz0nk3ic89FwbqrRwEocuKBwBI3/+jQVVKdLKPlwZeDTWj/HSY9lUQOT2jiPEJtiK nqUOuQ3gXu9oQ+7tGegPdHJjU8xE9Fc= 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-615-pQN_9AQ8PKenrZwV0fDyxQ-1; Mon, 27 Jul 2026 02:19:25 -0400 X-MC-Unique: pQN_9AQ8PKenrZwV0fDyxQ-1 X-Mimecast-MFC-AGG-ID: pQN_9AQ8PKenrZwV0fDyxQ_1785133164 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-492488f8583so21477165e9.2 for ; Sun, 26 Jul 2026 23:19:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785133164; x=1785737964; 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=4gI2fAVZ5gvUhXF5H2Mzs/l1X/Qhejzpnh9FmIDq78w=; b=YT/MSBTMDOQ6Fi5xclZWbmFs6WLMdFhLllgPqlSJ09GkGbK+Mx0il9YS9sXvzp8ldN 3okEverzcDNtyOxpSWNsHeRo1RlSsIOmBKr5zSX9c6jlMUeqoun5bQBQL2abGXMLAv7h Q7uzhpjzKr4ai5IamgRQe+z16zyYf7+fBkz+mI9jEeA6ZIWus5h6JTob2P7rpdaCu4FO ZjMkC/dY60cA17DJzZSO8dgnkdnLmu0JRdrJp2kdebyb45go8hSBQtkIbOIa3644M5Pv jOem+fqs2IHgudyNRphwli34eN9DFKEQZvcDQQnUNtkHbi6ovwHJbxaAos2LWlixYDWj wD/g== X-Gm-Message-State: AOJu0Yxa3W6z82SOV+JTBhkeP/gsAt7WH+32+2HIlzveABATWqDwy12I jnWAdIouOIG4WRmaTSmn54SDBwbgPONWahPO9mlSGwgvBIKuckFNcl0Sze2/+DhtSOOcjs3vbBM zPSdglXSSjn5PexY28mEINlrm+yLR7D6a857XfJxh0PfXRvDWNnsvvN3T+CGdtg== X-Gm-Gg: AR+sD12lpWVpPv8t9lOVMudRpD8iuSpsHbQDHMN3jYetK5AWO7NWy78O/Rkm+wlTDQd u/r66uATi+94Rgneugx18Vz2qNeYo/Facv6jVFnKVMl6XRhOuRUOPOFw2mTHch1RPzWFRoD6+Lu zC/LWddn4CJ8iO3Re33Nixp/M/tpCh8FzAAgD8BdwEkyhYtsT3MehdiUEYBTeuNfJ86ARBhtaa2 p36dB3lvXE4kXms42OAKd+veq/HpHayYkfJ79rH/tP0Sg97oVAHFj1Q7hWuB6Z36EzGJBw56Kz0 W1jdBgHj0HiA7OoJJWPtPIeh52MTkkW5G7hRhhSfwWFPjq+BwCNLz8MNmoWK1nv9tyDS/sTi1Gt Tq5dR4xW5jKz/ubJGTiBaMryUvB0p X-Received: by 2002:a05:600c:19c9:b0:492:4a50:41fe with SMTP id 5b1f17b1804b1-496b56ff171mr96390055e9.22.1785133163924; Sun, 26 Jul 2026 23:19:23 -0700 (PDT) X-Received: by 2002:a05:600c:19c9:b0:492:4a50:41fe with SMTP id 5b1f17b1804b1-496b56ff171mr96389575e9.22.1785133163177; Sun, 26 Jul 2026 23:19:23 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f939c1465sm31506271f8f.26.2026.07.26.23.19.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Jul 2026 23:19:22 -0700 (PDT) From: Stefano Brivio To: Anshu Kumari Subject: Re: [PATCH v5 4/7] dhcp: Add --dhcp-opt with option table and value parser Message-ID: <20260727081921.56a92d58@elisabeth> In-Reply-To: <20260717175648.879152-5-anskuma@redhat.com> References: <20260717175648.879152-1-anskuma@redhat.com> <20260717175648.879152-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: Mon, 27 Jul 2026 08:19:21 +0200 (CEST) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: _7xge84wt6olLhAJnD_TAavwZzFoRJGZEuY4AKNy-Pg_1785133164 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 5Q25W4CH2CAPVH5TBXU7NBXID7P4XTUH X-Message-ID-Hash: 5Q25W4CH2CAPVH5TBXU7NBXID7P4XTUH 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, lvivier@redhat.com, david@gibson.dropbear.id.au, jmaloy@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 Fri, 17 Jul 2026 23:26:41 +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 > --- > 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= . >=20 > --- > conf.c | 25 ++++- > dhcp.c | 338 +++++++++++++++++++++++++++++++++++++++++++++++++++----- > dhcp.h | 2 + > passt.1 | 42 +++++++ > 4 files changed, 381 insertions(+), 26 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index 5b6cc2b..e7f3ea0 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" > @@ -615,7 +616,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 > @@ -843,6 +845,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++) > +=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++) { > @@ -1316,6 +1321,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:"; > @@ -1330,10 +1336,13 @@ void conf(struct ctx *c, int argc, char **argv) > =09unsigned int ifi4 =3D 0, ifi6 =3D 0; > =09bool opt_a_is_prefix =3D false; > =09const char *logfile =3D NULL; > +=09unsigned long optcode; > =09char *runas =3D NULL; > =09size_t logsize =3D 0; > +=09const char *comma; > =09uint8_t opt_n =3D 0; > =09int name, ret; > +=09char *end; > =09uid_t uid; > =09gid_t gid; > =20 > @@ -1557,6 +1566,20 @@ 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=09comma =3D strchr(optarg, ','); > +=09=09=09if (!comma) > +=09=09=09=09die("--dhcp-opt requires CODE,VALUE format"); > + > +=09=09=09errno =3D 0; > +=09=09=09optcode =3D strtoul(optarg, &end, 0); > +=09=09=09if (end !=3D comma || errno || > +=09=09=09 optcode < 1 || optcode > 254) Same comment as on 3/7: if the body of a conditional clause is on multiple lines, we use curly brackets (for coding style, nothing else). > +=09=09=09=09die("DHCP option code must be 1-254: %s", > +=09=09=09=09 optarg); > + > +=09=09=09dhcp_set_opt(optcode, comma + 1); > +=09=09=09break; > =09=09case 'd': > =09=09=09c->debug =3D 1; > =09=09=09c->quiet =3D 0; > diff --git a/dhcp.c b/dhcp.c > index 39f7952..cc910ee 100644 > --- a/dhcp.c > +++ b/dhcp.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include > =20 > #include "util.h" > #include "ip.h" > @@ -37,10 +38,12 @@ > * enum opt_state - DHCP option state > * @OPT_UNSET:=09=09Option not configured > * @OPT_DEFAULT:=09Option set from host config > + * @OPT_USER:=09=09Option set via --dhcp-opt command line > */ > enum opt_state { > =09OPT_UNSET, > =09OPT_DEFAULT, > +=09OPT_USER, > }; > =20 > /** > @@ -50,7 +53,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; > @@ -88,19 +91,20 @@ void dhcp_init(void) > =09int i; > =20 > =09for (i =3D 0; i < ARRAY_SIZE(opts); i++) > -=09=09opts[i].state =3D OPT_UNSET; > - > -=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=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, }= ; Hint to avoid exceeding 80 columns: drop the ',' after OPT_DEFAULT. Yes, we usually add one, but it's not strictly needed and, here, _not_ having it improves readability. > +=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 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, }; > +=09if (opts[53].state !=3D OPT_USER)=09=09=09/* Type */ We don't let users set option 53 because it wouldn't make sense (the value depends on where we are in the DHCP exchange), so I guess we shouldn't add a conditional here. > +=09=09opts[53] =3D (struct opt) { 0, 1, { 0 }, 0, { 0 }, OPT_DEFAULT, }= ; > +=09if (opts[54].state !=3D OPT_USER)=09=09=09/* Server ID */ > +=09=09opts[54] =3D (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }= ; > } > =20 > /** > @@ -145,6 +149,280 @@ 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[53] =3D DHCP_OPT_UINT8,=09=09/* DHCP Message Type */ I wonder if we should skip option 53 here, so that we get a die() call from dhcp_set_opt() as a bonus (we don't want the user to set it, because we can't honour that setting anyway). > +=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 uint8_t *buf, size_t buf_len) > +{ > +=09enum dhcp_opt_type type; > +=09unsigned long val; > +=09unsigned int i; > +=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=09while (*str) { > +=09=09=09char ipbuf[INET_ADDRSTRLEN]; > +=09=09=09size_t chunk; > + > +=09=09=09chunk =3D strcspn(str, ","); > + > +=09=09=09if (!chunk || chunk >=3D sizeof(ipbuf)) > +=09=09=09=09return -1; > + > +=09=09=09memcpy(ipbuf, str, chunk); > +=09=09=09ipbuf[chunk] =3D '\0'; > + > +=09=09=09if (len + sizeof(struct in_addr) > buf_len) > +=09=09=09=09return -1; > + > +=09=09=09if (inet_pton(AF_INET, ipbuf, buf + len) !=3D 1) > +=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=09if (str[chunk] =3D=3D ',') > +=09=09=09=09=09return -1; > +=09=09=09=09break; > +=09=09=09} > + > +=09=09=09str +=3D chunk + (str[chunk] =3D=3D ','); > +=09=09} > + > +=09=09if (!len) > +=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=09for (i =3D width; i > 0; i--) { > +=09=09=09buf[i - 1] =3D val & 0xff; > +=09=09=09val >>=3D 8; I think I commented on this earlier but I haven't seen any reply about it: this reimplements htons() / htonl(). If you could call the right function (htons(), htonl(), or nothing) while you set 'width' above, you could probably skip this and things would look a bit cleaner. I haven't tried though, maybe it's not the case. > +=09=09} > + > +=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 user-set, NULL otherwise > + */ > +const char *dhcp_opt_to_str(uint8_t code, char *buf, size_t buf_len) > +{ > +=09enum dhcp_opt_type type; > +=09int off =3D 0; > +=09unsigned int i; > + > +=09if (opts[code].state !=3D OPT_USER || opts[code].slen < 0) > +=09=09return NULL; > + > +=09if (code >=3D ARRAY_SIZE(dhcp_opt_types)) > +=09=09return NULL; > + > +=09type =3D dhcp_opt_types[code]; > + > +=09switch (type) { > +=09case DHCP_OPT_IPV4: > +=09case DHCP_OPT_IPV4_LIST: > +=09=09for (i =3D 0; i + 4 <=3D (unsigned int)opts[code].slen; i +=3D 4) = { > +=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: > +=09=09if (snprintf(buf, buf_len, "%u", > +=09=09=09 opts[code].s[0]) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09case DHCP_OPT_UINT16: > +=09=09if (snprintf(buf, buf_len, "%u", > +=09=09=09 ((unsigned)opts[code].s[0] << 8) | > +=09=09=09 opts[code].s[1]) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09case DHCP_OPT_UINT32: > +=09=09if (snprintf(buf, buf_len, "%u", > +=09=09=09 ((unsigned)opts[code].s[0] << 24) | > +=09=09=09 ((unsigned)opts[code].s[1] << 16) | > +=09=09=09 ((unsigned)opts[code].s[2] << 8) | > +=09=09=09 opts[code].s[3]) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09case DHCP_OPT_INT32: > +=09=09if (snprintf(buf, buf_len, "%d", > +=09=09=09 (int)(((unsigned)opts[code].s[0] << 24) | > +=09=09=09=09 ((unsigned)opts[code].s[1] << 16) | > +=09=09=09=09 ((unsigned)opts[code].s[2] << 8) | > +=09=09=09=09 opts[code].s[3])) >=3D (int)buf_len) > +=09=09=09return NULL; > +=09=09return buf; > +=09case DHCP_OPT_STR: > +=09=09if ((size_t)opts[code].slen < buf_len) { > +=09=09=09memcpy(buf, opts[code].s, opts[code].slen); > +=09=09=09buf[opts[code].slen] =3D '\0'; > +=09=09} else { > +=09=09=09memcpy(buf, opts[code].s, buf_len - 1); > +=09=09=09buf[buf_len - 1] =3D '\0'; > +=09=09} > +=09=09return buf; > +=09default: > +=09=09return NULL; > +=09} > +} > + > /** > * fill_one() - Fill a single option into a buffer > * @buf:=09Buffer to write option > @@ -482,14 +760,19 @@ 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)); Same as above with curly brackets. > =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; > @@ -501,27 +784,32 @@ 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=09for (i =3D 0, opts[6].slen =3D 0; > +=09=09 !c->no_dhcp_dns && i < ARRAY_SIZE(c->ip4.dns); i++) { Then I'd say use three lines, one for each part, it's easier to read. > +=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].slen =3D -1; > =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 995590a..215a289 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), > +53 (DHCP Message Type, 8-bit), 57 (Max DHCP Message Size, 16-bit), 53 not really... I mean yes, it's supported, but not in this context. > +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