Skip to contentSkip to conversation

*** net: DHCPv6 protocol and commands ***

PATCH Superseded [email protected] 3 participants 12 messages Apr 7, 2023 – May 2, 2023 Networking

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#9 lore raw

PATCH v2 3/3 net: dhcp6: Add a sandbox test for dhcp6

In reply to [email protected]

From: Sean Edmond <[email protected]>

Requires proper environment with DHCP6 server provisioned.

---
 configs/sandbox_defconfig |  1 +
 test/py/tests/test_net.py | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
configs/sandbox_defconfig +1 -0
index 3a1f14c60f..f65965f864 100644
@@ -340,3 +340,4 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+CONFIG_CMD_DHCP6=y
test/py/tests/test_net.py +25 -0
index 9ca6743afd..0447c0b2e0 100644
@@ -29,6 +29,11 @@ env__net_uses_pci = True
 # set to False.
 env__net_dhcp_server = True
 
+# True if a DHCPv6 server is attached to the network, and should be tested.
+# If DHCPv6 testing is not possible or desired, this variable may be omitted or
+# set to False.
+env__net_dhcp6_server = True
+
 # A list of environment variables that should be set in order to configure a
 # static IP. If solely relying on DHCP, this variable may be omitted or set to
 # an empty list.
@@ -58,6 +63,7 @@ env__net_nfs_readable_file = {
 """
 
 net_set_up = False
+net6_set_up = False
 
 def test_net_pre_commands(u_boot_console):
     """Execute any commands required to enable network hardware.
@@ -93,6 +99,25 @@ def test_net_dhcp(u_boot_console):
     global net_set_up
     net_set_up = True
 
[email protected]('cmd_dhcp6')
+def test_net_dhcp6(u_boot_console):
+    """Test the dhcp6 command.
+
+    The boardenv_* file may be used to enable/disable this test; see the
+    comment at the beginning of this file.
+    """
+
+    test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
+    if not test_dhcp6:
+        pytest.skip('No DHCP6 server available')
+
+    u_boot_console.run_command('setenv autoload no')
+    output = u_boot_console.run_command('dhcp6')
+    assert 'DHCP6 client bound to ' in output
+
+    global net6_set_up
+    net6_set_up = True
+
 @pytest.mark.buildconfigspec('net')
 def test_net_setup_static(u_boot_console):
     """Set up a static IP configuration.
Signature · 1 line
2.40.0
#0 lore raw

v2 cover *** net: DHCPv6 protocol and commands ***

From: Sean Edmond <[email protected]>

The recently integrated IPv6 patch series relies on the link-local address, or a statically assigned IPv6 address for network operations. This patch series adds IPv6 address assignment through DHCPv6.

The implementation meets the requirements in RFC 8415 for "Client/Server Exchanges Involving Four Messages":
https://www.rfc-editor.org/rfc/rfc8415

The implementation sends/receives the minimum required DHCPv6 options to network boot.

A new command (dhcp6) will execute the protocol. In addition, IPv6 functionality has been extended to the existing pxe commands ("pxe get" and "pxe boot").

changes in v2:
- Add sandbox test in test_net.py
- Add CONFIG_CMD_DHCP6 to sandbox_defconfig
- fix comment style (/**/ instead of //)
- move addition of Kconfig from 1st patch to 2nd patch
- Fix warning (warning: label ‘error_exit’ defined but not used") when CONFIG_DHCP6_PXE_DHCP_OPTION not configured
- Fix dhcp6 command help
- Use net_set_timeout_handler(0, NULL) in dhcpv6.c
- Move USE_IP6_CMD_PARAM back to net6.h

Sean Edmond (3):
net: dhcp6: Add DHCPv6 (DHCP for IPv6)
net: dhcp6: pxe: Add DHCP/PXE commands for IPv6
net: dhcp6: Add a sandbox test for dhcp6

 boot/bootmeth_distro.c    |   2 +-
 boot/bootmeth_pxe.c       |   4 +-
 boot/pxe_utils.c          |   3 +-
 cmd/Kconfig               |  26 ++
 cmd/net.c                 |  23 ++
 cmd/pxe.c                 |  86 ++++-
 cmd/sysboot.c             |   2 +-
 configs/sandbox_defconfig |   1 +
 include/net.h             |   8 +-
 include/pxe_utils.h       |  10 +-
 net/Makefile              |   1 +
 net/dhcpv6.c              | 735 ++++++++++++++++++++++++++++++++++++++
 net/dhcpv6.h              | 212 +++++++++++
 net/net.c                 |  12 +
 test/py/tests/test_net.py |  25 ++
 15 files changed, 1132 insertions(+), 18 deletions(-)
 create mode 100644 net/dhcpv6.c
 create mode 100644 net/dhcpv6.h
Signature · 1 line
2.40.0
#1 lore raw

PATCH v2 1/3 net: dhcp6: Add DHCPv6 (DHCP for IPv6)

From: Sean Edmond <[email protected]>

Adds DHCPv6 protocol to u-boot.

Allows for address assignement with DHCPv6 4-message exchange (SOLICIT->ADVERTISE->REQUEST->REPLY). Includes DHCPv6 options required by RFC 8415. Also adds DHCPv6 options required for PXE boot.

Possible enhancements:
- Duplicate address detection on DHCPv6 assigned address
- IPv6 address assignement through SLAAC
- Sending/parsing other DHCPv6 options (NTP, DNS, etc...)

---
 include/net.h |   8 +-
 net/Makefile  |   1 +
 net/dhcpv6.c  | 735 ++++++++++++++++++++++++++++++++++++++++++++++++++
 net/dhcpv6.h  | 212 +++++++++++++++
 net/net.c     |  12 +
 5 files changed, 966 insertions(+), 2 deletions(-)
 create mode 100644 net/dhcpv6.c
 create mode 100644 net/dhcpv6.h
include/net.h +6 -2
index 399af5e064..cac818e292 100644
@@ -484,6 +484,10 @@ extern char	net_hostname[32];	/* Our hostname */
 #ifdef CONFIG_NET
 extern char	net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN];	/* Our root path */
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+extern char *pxelinux_configfile;
+#endif
 /** END OF BOOTP EXTENTIONS **/
 extern u8		net_ethaddr[ARP_HLEN];		/* Our ethernet address */
 extern u8		net_server_ethaddr[ARP_HLEN];	/* Boot server enet address */
@@ -504,8 +508,8 @@ extern ushort		net_native_vlan;	/* Our Native VLAN */
 extern int		net_restart_wrap;	/* Tried all network devices */
 
 enum proto_t {
-	BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS,
-	SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
+	BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
+	NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
 };
 
 extern char	net_boot_file_name[1024];/* Boot File name */
net/Makefile +1 -0
index bea000b206..5968110170 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_IPV6)     += net6.o
 obj-$(CONFIG_CMD_NFS)  += nfs.o
 obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_PING6) += ping6.o
+obj-$(CONFIG_CMD_DHCP6) += dhcpv6.o
 obj-$(CONFIG_CMD_PCAP) += pcap.o
 obj-$(CONFIG_CMD_RARP) += rarp.o
 obj-$(CONFIG_CMD_SNTP) += sntp.o
net/dhcpv6.c new file +735 -0

735 lines not on this page · raw message

net/dhcpv6.h new file +212 -0

212 lines not on this page · raw message

net/net.c +12 -0
index c9a749f6cc..73d5b2bc80 100644
@@ -122,6 +122,9 @@
 #endif
 #include <net/tcp.h>
 #include <net/wget.h>
+#if defined(CONFIG_CMD_DHCP6)
+#include "dhcpv6.h"
+#endif
 
 /** BOOTP EXTENTIONS **/
 
@@ -135,6 +138,10 @@ struct in_addr net_dns_server;
 /* Our 2nd DNS IP address */
 struct in_addr net_dns_server2;
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+char *pxelinux_configfile;
+#endif
 
 /** END OF BOOTP EXTENTIONS **/
 
@@ -510,6 +517,11 @@ restart:
 			dhcp_request();		/* Basically same as BOOTP */
 			break;
 #endif
+#if defined(CONFIG_CMD_DHCP6)
+		case DHCP6:
+			dhcp6_start();
+			break;
+#endif
 #if defined(CONFIG_CMD_BOOTP)
 		case BOOTP:
 			bootp_reset();
Signature · 1 line
2.40.0
#5 lore raw

PATCH v2 2/3 net: dhcp6: pxe: Add DHCP/PXE commands for IPv6

In reply to [email protected]

From: Sean Edmond <[email protected]>

Adds commands to support DHCP and PXE with IPv6.

New configs added:
- CMD_DHCP6
- DHCP6_PXE_CLIENTARCH
- DHCP6_PXE_DHCP_OPTION
- DHCP6_ENTERPRISE_ID

New commands added (when IPv6 is enabled):
- dhcp6
- pxe get -ipv6
- pxe boot -ipv6

---
 boot/bootmeth_distro.c |  2 +-
 boot/bootmeth_pxe.c    |  4 +-
 boot/pxe_utils.c       |  3 +-
 cmd/Kconfig            | 26 +++++++++++++
 cmd/net.c              | 23 +++++++++++
 cmd/pxe.c              | 86 +++++++++++++++++++++++++++++++++++++-----
 cmd/sysboot.c          |  2 +-
 include/pxe_utils.h    | 10 ++++-
 8 files changed, 140 insertions(+), 16 deletions(-)
boot/bootmeth_distro.c +1 -1
index 356929828b..b4b73ecbf5 100644
@@ -150,7 +150,7 @@ static int distro_boot(struct udevice *dev, struct bootflow *bflow)
 	info.dev = dev;
 	info.bflow = bflow;
 	ret = pxe_setup_ctx(&ctx, &cmdtp, distro_getfile, &info, true,
-			    bflow->subdir);
+			    bflow->subdir, false);
 	if (ret)
 		return log_msg_ret("ctx", -EINVAL);
 
boot/bootmeth_pxe.c +2 -2
index ecf8557af8..5a8af2bbd0 100644
@@ -70,7 +70,7 @@ static int distro_pxe_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 	addr = simple_strtoul(addr_str, NULL, 16);
 
 	log_debug("calling pxe_get()\n");
-	ret = pxe_get(addr, &bootdir, &size);
+	ret = pxe_get(addr, &bootdir, &size, false);
 	log_debug("pxe_get() returned %d\n", ret);
 	if (ret)
 		return log_msg_ret("pxeb", ret);
@@ -146,7 +146,7 @@ static int distro_pxe_boot(struct udevice *dev, struct bootflow *bflow)
 	info.bflow = bflow;
 	info.cmdtp = &cmdtp;
 	ret = pxe_setup_ctx(ctx, &cmdtp, distro_pxe_getfile, &info, false,
-			    bflow->subdir);
+			    bflow->subdir, false);
 	if (ret)
 		return log_msg_ret("ctx", -EINVAL);
 
boot/pxe_utils.c +2 -1
index 3a1e50f2b1..d13c47dd94 100644
@@ -1578,7 +1578,7 @@ void handle_pxe_menu(struct pxe_context *ctx, struct pxe_menu *cfg)
 
 int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
 		  pxe_getfile_func getfile, void *userdata,
-		  bool allow_abs_path, const char *bootfile)
+		  bool allow_abs_path, const char *bootfile, bool use_ipv6)
 {
 	const char *last_slash;
 	size_t path_len = 0;
@@ -1588,6 +1588,7 @@ int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
 	ctx->getfile = getfile;
 	ctx->userdata = userdata;
 	ctx->allow_abs_path = allow_abs_path;
+	ctx->use_ipv6 = use_ipv6;
 
 	/* figure out the boot directory, if there is one */
 	if (bootfile && strlen(bootfile) >= MAX_TFTP_PATH_LEN)
cmd/Kconfig +26 -0
index bab35fc667..8448cf3400 100644
@@ -1672,6 +1672,15 @@ config CMD_DHCP
 	help
 	  Boot image via network using DHCP/TFTP protocol
 
+config CMD_DHCP6
+	bool "dhcp6"
+	depends on IPV6
+	help
+	  Boot image via network using DHCPv6/TFTP protocol using IPv6.
+
+	  Will perform 4-message exchange with DHCPv6 server, requesting
+	  the minimum required options to TFTP boot. Complies with RFC 8415.
+
 config BOOTP_MAY_FAIL
 	bool "Allow for the BOOTP/DHCP server to not be found"
 	depends on CMD_BOOTP
@@ -1785,6 +1794,23 @@ config BOOTP_VCI_STRING
 	default "U-Boot.arm" if ARM
 	default "U-Boot"
 
+if CMD_DHCP6
+
+config DHCP6_PXE_CLIENTARCH
+	hex
+	default 0x16 if ARM64
+	default 0x15 if ARM
+	default 0xFF
+
+config DHCP6_PXE_DHCP_OPTION
+	bool "Request & store 'pxe_configfile' from DHCP6 server"
+
+config DHCP6_ENTERPRISE_ID
+	int "Enterprise ID to send in DHCPv6 Vendor Class Option"
+	default 0
+
+endif
+
 config CMD_TFTPBOOT
 	bool "tftpboot"
 	default y
cmd/net.c +23 -0
index d5e20843dd..95529a9d12 100644
@@ -111,6 +111,29 @@ U_BOOT_CMD(
 );
 #endif
 
+#if defined(CONFIG_CMD_DHCP6)
+static int do_dhcp6(struct cmd_tbl *cmdtp, int flag, int argc,
+		    char *const argv[])
+{
+	int i;
+	int dhcp_argc;
+	char *dhcp_argv[] = {NULL, NULL, NULL, NULL};
+
+	/* Add -ipv6 flag for autoload */
+	for (i = 0; i < argc; i++)
+		dhcp_argv[i] = argv[i];
+	dhcp_argc = argc + 1;
+	dhcp_argv[dhcp_argc - 1] =  USE_IP6_CMD_PARAM;
+
+	return netboot_common(DHCP6, cmdtp, dhcp_argc, dhcp_argv);
+}
+
+U_BOOT_CMD(dhcp6,	3,	1,	do_dhcp6,
+	   "boot image via network using DHCPv6/TFTP protocol. \n"
+	   "Use IPv6 hostIPaddr framed with [] brackets",
+	   "[loadAddress] [[hostIPaddr:]bootfilename]");
+#endif
+
 #if defined(CONFIG_CMD_DHCP)
 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[])
cmd/pxe.c +77 -9

178 lines not on this page · raw message

cmd/sysboot.c +1 -1
index 04c0702026..63a7806deb 100644
@@ -101,7 +101,7 @@ static int do_sysboot(struct cmd_tbl *cmdtp, int flag, int argc,
 	}
 
 	if (pxe_setup_ctx(&ctx, cmdtp, sysboot_read_file, &info, true,
-			  filename)) {
+			  filename, false)) {
 		printf("Out of memory\n");
 		return CMD_RET_FAILURE;
 	}
include/pxe_utils.h +8 -2
index 1e5e8424f5..9f19593048 100644
@@ -93,6 +93,7 @@ typedef int (*pxe_getfile_func)(struct pxe_context *ctx, const char *file_path,
  * @bootdir: Directory that files are loaded from ("" if no directory). This is
  *	allocated
  * @pxe_file_size: Size of the PXE file
+ * @use_ipv6: TRUE : use IPv6 addressing, FALSE : use IPv4 addressing
  */
 struct pxe_context {
 	struct cmd_tbl *cmdtp;
@@ -112,6 +113,7 @@ struct pxe_context {
 	bool allow_abs_path;
 	char *bootdir;
 	ulong pxe_file_size;
+	bool use_ipv6;
 };
 
 /**
@@ -209,12 +211,14 @@ int format_mac_pxe(char *outbuf, size_t outbuf_len);
  * @allow_abs_path: true to allow absolute paths
  * @bootfile: Bootfile whose directory loaded files are relative to, NULL if
  *	none
+ * @use_ipv6: TRUE : use IPv6 addressing
+ *            FALSE : use IPv4 addressing
  * Return: 0 if OK, -ENOMEM if out of memory, -E2BIG if bootfile is larger than
  *	MAX_TFTP_PATH_LEN bytes
  */
 int pxe_setup_ctx(struct pxe_context *ctx, struct cmd_tbl *cmdtp,
 		  pxe_getfile_func getfile, void *userdata,
-		  bool allow_abs_path, const char *bootfile);
+		  bool allow_abs_path, const char *bootfile, bool use_ipv6);
 
 /**
  * pxe_destroy_ctx() - Destroy a PXE context
@@ -251,7 +255,9 @@ int pxe_get_file_size(ulong *sizep);
  *	"rpi/info", which indicates that all files should be fetched from the
  *	"rpi/" subdirectory
  * @sizep: Size of the PXE file (not bootfile)
+ * @use_ipv6: TRUE : use IPv6 addressing
+ *            FALSE : use IPv4 addressing
  */
-int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep);
+int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6);
 
 #endif /* __PXE_UTILS_H */
Signature · 1 line
2.40.0
Reviewed-by on v2 3/3
#10 lore raw

Re: [PATCH v2 3/3] net: dhcp6: Add a sandbox test for dhcp6

In reply to [email protected]
Quoted 10 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Requires proper environment with DHCP6 server provisioned.

---
 configs/sandbox_defconfig |  1 +
 test/py/tests/test_net.py | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] net: dhcp6: pxe: Add DHCP/PXE commands for IPv6

In reply to [email protected]

Hi Sean,

Quoted 17 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds commands to support DHCP and PXE with IPv6.

New configs added:
- CMD_DHCP6
- DHCP6_PXE_CLIENTARCH
- DHCP6_PXE_DHCP_OPTION
- DHCP6_ENTERPRISE_ID

New commands added (when IPv6 is enabled):
- dhcp6
- pxe get -ipv6
- pxe boot -ipv6

Quoted 10 lines
---
 boot/bootmeth_distro.c |  2 +-
 boot/bootmeth_pxe.c    |  4 +-
 boot/pxe_utils.c       |  3 +-
 cmd/Kconfig            | 26 +++++++++++++
 cmd/net.c              | 23 +++++++++++
 cmd/pxe.c              | 86 +++++++++++++++++++++++++++++++++++++-----
 cmd/sysboot.c          |  2 +-
 include/pxe_utils.h    | 10 ++++-
 8 files changed, 140 insertions(+), 16 deletions(-)

With nits below:

[..]

cmd/Kconfig config BOOTP_VCI_STRING · line 1803

Quoted 7 lines
cmd/Kconfig +7 -0
+if CMD_DHCP6
+
+config DHCP6_PXE_CLIENTARCH
+       hex
+       default 0x16 if ARM64
+       default 0x15 if ARM
+       default 0xFF

Do we need a separate option or could we use BOOTP_PXE_CLIENTARCH ?

Quoted 84 lines
cmd/Kconfig +10 -0
+
+config DHCP6_PXE_DHCP_OPTION
+       bool "Request & store 'pxe_configfile' from DHCP6 server"
+
+config DHCP6_ENTERPRISE_ID
+       int "Enterprise ID to send in DHCPv6 Vendor Class Option"
+       default 0
+
+endif
+
 config CMD_TFTPBOOT
        bool "tftpboot"
        default y
cmd/net.c +23 -0
index d5e20843dd..95529a9d12 100644
@@ -111,6 +111,29 @@ U_BOOT_CMD(
 );
 #endif
 
+#if defined(CONFIG_CMD_DHCP6)
+static int do_dhcp6(struct cmd_tbl *cmdtp, int flag, int argc,
+                   char *const argv[])
+{
+       int i;
+       int dhcp_argc;
+       char *dhcp_argv[] = {NULL, NULL, NULL, NULL};
+
+       /* Add -ipv6 flag for autoload */
+       for (i = 0; i < argc; i++)
+               dhcp_argv[i] = argv[i];
+       dhcp_argc = argc + 1;
+       dhcp_argv[dhcp_argc - 1] =  USE_IP6_CMD_PARAM;
+
+       return netboot_common(DHCP6, cmdtp, dhcp_argc, dhcp_argv);
+}
+
+U_BOOT_CMD(dhcp6,      3,      1,      do_dhcp6,
+          "boot image via network using DHCPv6/TFTP protocol. \n"
+          "Use IPv6 hostIPaddr framed with [] brackets",
+          "[loadAddress] [[hostIPaddr:]bootfilename]");
+#endif
+
 #if defined(CONFIG_CMD_DHCP)
 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
                   char *const argv[])
cmd/pxe.c +10 -1
index db8e4697f2..71e6fd9633 100644
@@ -8,6 +8,7 @@
 #include <command.h>
 #include <fs.h>
 #include <net.h>
+#include <net6.h>
 
 #include "pxe_utils.h"
 
@@ -29,12 +30,20 @@ static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
 {
        char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
        int ret;
+       int num_args;
 
        tftp_argv[1] = file_addr;
        tftp_argv[2] = (void *)file_path;
+       if (ctx->use_ipv6) {
+               tftp_argv[3] = USE_IP6_CMD_PARAM;
+               num_args = 4;
+       } else {
+               num_args = 3;
+       }
 
-       if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
+       if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv))
                return -ENOENT;
+
        ret = pxe_get_file_size(sizep);
        if (ret)
                return log_msg_ret("tftp", ret);
@@ -43,6 +52,23 @@ static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
        return 1;
 }

cmd/pxe.c static int do_get_tftp(struct pxe_context *ctx, const char *file_path, · line 63

Quoted 10 lines
cmd/pxe.c +9 -0
 
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/*
+ * Looks for a pxe file with specified config file name,
+ * which is received from DHCPv4 option 209 or
+ * DHCPv6 option 60.
+ *
+ * Returns 1 on success or < 0 on error.
+ */
+static inline int pxe_dhcp_option_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)

Please drop the inline as the compiler can handle that.

Quoted 18 lines
cmd/pxe.c +9 -1
+{
+       int ret = get_pxe_file(ctx, pxelinux_configfile, pxefile_addr_r);
+
+       free(pxelinux_configfile);
+
+       return ret;
+}
+#endif
 /*
  * Looks for a pxe file with a name based on the pxeuuid environment variable.
  *
@@ -105,15 +131,25 @@ static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_
        return -ENOENT;
 }
 
-int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep)
+int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6)
 {

cmd/pxe.c static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_ · line 144

Quoted 10 lines
cmd/pxe.c +3 -1
        struct cmd_tbl cmdtp[] = {};    /* dummy */
        struct pxe_context ctx;
        int i;
 
        if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
-                         env_get("bootfile")))
+                         env_get("bootfile"), use_ipv6))
                return -ENOMEM;
+
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

It's a bit annoying that pxelinux_configfile causes these #ifdefs. How about always having pxelinux_configfile and then you don't need to worry. It can just be NULL when not used. It is only BSS space, after all...

Quoted 12 lines
cmd/pxe.c +8 -0
+       if (pxelinux_configfile && use_ipv6) {
+               if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
+                       goto done;
+
+               goto error_exit;
+       }
+#endif
+
        /*
         * Keep trying paths until we successfully get a file we're looking
         * for.

cmd/pxe.c int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep) · line 170

Quoted 4 lines
cmd/pxe.c +1 -0
@@ -131,9 +167,12 @@ int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep)
                i++;
        }
 
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

...then you can use if (IS_ENABLED(... here

Quoted 69 lines
cmd/pxe.c +22 -6
+error_exit:
        pxe_destroy_ctx(&ctx);
 
        return -ENOENT;
+#endif
 done:
        *bootdirp = env_get("bootfile");
 
@@ -169,9 +208,17 @@ do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        char *fname;
        ulong size;
        int ret;
+       bool use_ipv6 = false;
 
-       if (argc != 1)
-               return CMD_RET_USAGE;
+       if (IS_ENABLED(CONFIG_IPV6)) {
+               if (argc != 1 && argc != 2)
+                       return CMD_RET_USAGE;
+               if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
+                       use_ipv6 = true;
+       } else {
+               if (argc != 1)
+                       return CMD_RET_USAGE;
+       }
 
        pxefile_addr_str = from_env("pxefile_addr_r");
 
@@ -183,7 +230,7 @@ do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        if (ret < 0)
                return 1;
 
-       ret = pxe_get(pxefile_addr_r, &fname, &size);
+       ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6);
        switch (ret) {
        case 0:
                printf("Config file '%s' found\n", fname);
@@ -211,13 +258,19 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        char *pxefile_addr_str;
        struct pxe_context ctx;
        int ret;
+       bool use_ipv6 = false;
+
+       if (IS_ENABLED(CONFIG_IPV6)) {
+               if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
+                       use_ipv6 = true;
+       }
 
-       if (argc == 1) {
+       if (argc == 1 || (argc == 2 && use_ipv6)) {
                pxefile_addr_str = from_env("pxefile_addr_r");
                if (!pxefile_addr_str)
                        return 1;
 
-       } else if (argc == 2) {
+       } else if (argc == 2 || (argc == 3 && use_ipv6)) {
                pxefile_addr_str = argv[1];
        } else {
                return CMD_RET_USAGE;
@@ -229,7 +282,7 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        }
 
        if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
-                         env_get("bootfile"))) {
+                         env_get("bootfile"), use_ipv6)) {
                printf("Out of memory\n");
                return CMD_RET_FAILURE;
        }

cmd/pxe.c do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 306

Quoted 10 lines
cmd/pxe.c +5 -0
@@ -244,8 +297,13 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 }
 
 static struct cmd_tbl cmd_pxe_sub[] = {
+#if IS_ENABLED(CONFIG_IPV6)
+       U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""),
+       U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "")
+#else
        U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
        U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
+#endif

That's a bit ugly. How about using the max and then checking it in the C code?

Quoted 11 lines
cmd/pxe.c +4 -0
 };
 
 static void __maybe_unused pxe_reloc(void)
@@ -281,9 +339,19 @@ static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
        return CMD_RET_USAGE;
 }
 
+#if IS_ENABLED(CONFIG_IPV6)
+U_BOOT_CMD(pxe, 4, 1, do_pxe,
+          "commands to get and boot from pxe files\n"
+          "To use IPv6 add -ipv6 parameter",

cmd/pxe.c static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 355

Quoted 10 lines
cmd/pxe.c +4 -0
+          "get [" USE_IP6_CMD_PARAM "] - try to retrieve a pxe file using tftp\n"
+          "pxe boot [pxefile_addr_r] [-ipv6] - boot from the pxe file at pxefile_addr_r\n"
+);
+#else
 U_BOOT_CMD(pxe, 3, 1, do_pxe,
           "commands to get and boot from pxe files",
           "get - try to retrieve a pxe file using tftp\n"
           "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
 );
 #endif

same here

cmd/pxe.c static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 357

Quoted 2 lines
cmd/pxe.c +2 -0
+
+#endif /* CONFIG_CMD_NET */

[..]

Regards,
Simon

Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] net: dhcp6: Add DHCPv6 (DHCP for IPv6)

In reply to [email protected]

Hi,

Quoted 16 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds DHCPv6 protocol to u-boot.

Allows for address assignement with DHCPv6 4-message exchange (SOLICIT->ADVERTISE->REQUEST->REPLY). Includes DHCPv6 options required by RFC 8415. Also adds DHCPv6 options required for PXE boot.

Possible enhancements:
- Duplicate address detection on DHCPv6 assigned address
- IPv6 address assignement through SLAAC
- Sending/parsing other DHCPv6 options (NTP, DNS, etc...)

Quoted 9 lines
---
 include/net.h |   8 +-
 net/Makefile  |   1 +
 net/dhcpv6.c  | 735 ++++++++++++++++++++++++++++++++++++++++++++++++++
 net/dhcpv6.h  | 212 +++++++++++++++
 net/net.c     |  12 +
 5 files changed, 966 insertions(+), 2 deletions(-)
 create mode 100644 net/dhcpv6.c
 create mode 100644 net/dhcpv6.h

This looks good to me. I just have a few nits below. With those fixed:

include/net.h extern char net_hostname[32]; /* Our hostname */ · line 487

Quoted 10 lines
include/net.h +1 -0
index 399af5e064..cac818e292 100644
@@ -484,6 +484,10 @@ extern char        net_hostname[32];       /* Our hostname */
 #ifdef CONFIG_NET
 extern char    net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN];  /* Our root path */
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

You can drop this #ifdef as any reference to a non-existent var will give a build error.

Quoted 49 lines
include/net.h +5 -2
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+extern char *pxelinux_configfile;
+#endif
 /** END OF BOOTP EXTENTIONS **/
 extern u8              net_ethaddr[ARP_HLEN];          /* Our ethernet address */
 extern u8              net_server_ethaddr[ARP_HLEN];   /* Boot server enet address */
@@ -504,8 +508,8 @@ extern ushort               net_native_vlan;        /* Our Native VLAN */
 extern int             net_restart_wrap;       /* Tried all network devices */
 
 enum proto_t {
-       BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS,
-       SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
+       BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
+       NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
 };
 
 extern char    net_boot_file_name[1024];/* Boot File name */
net/Makefile +1 -0
index bea000b206..5968110170 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_IPV6)     += net6.o
 obj-$(CONFIG_CMD_NFS)  += nfs.o
 obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_PING6) += ping6.o
+obj-$(CONFIG_CMD_DHCP6) += dhcpv6.o
 obj-$(CONFIG_CMD_PCAP) += pcap.o
 obj-$(CONFIG_CMD_RARP) += rarp.o
 obj-$(CONFIG_CMD_SNTP) += sntp.o
net/dhcpv6.c new file +14 -0
new file mode 100644
index 0000000000..9204909c1f
@@ -0,0 +1,735 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+/* Simple DHCP6 network layer implementation. */
+
+#include <common.h>
+#include <bootstage.h>
+#include <command.h>
+#include <env.h>
+#include <efi_loader.h>

net/dhcpv6.c line 24

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#include <log.h>
+#include <net.h>
+#include <rand.h>
+#include <uuid.h>
+#include <linux/delay.h>
+#include <net/tftp.h>
+#include "dhcpv6.h"
+#include <net6.h>
+#include <malloc.h>
+#include "net_rand.h"

Please fix header order:
https://u-boot.readthedocs.io/en/latest/develop/codingstyle.html#include-files

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+
+#define PORT_DHCP6_S   547     /* DHCP6 server UDP port */
+#define PORT_DHCP6_C   546     /* DHCP6 client UDP port */
+
+/* default timeout parameters (in ms) */
+#define SOL_MAX_DELAY_MS       1000
+#define SOL_TIMEOUT_MS         1000
+#define SOL_MAX_RT_MS          3600000
+#define REQ_TIMEOUT_MS         1000

net/dhcpv6.c line 43

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#define REQ_MAX_RT_MS          30000
+#define REQ_MAX_RC             10
+#define MAX_WAIT_TIME_MS       60000
+
+/* global variable to track any updates from DHCP6 server */
+int updated_sol_max_rt_ms = SOL_MAX_RT_MS;
+
+static void dhcp6_timeout_handler(void);
+static void dhcp6_state_machine(bool timeout, uchar *rx_pkt, unsigned int len);
+static void dhcp6_parse_options(uchar *rx_pkt, unsigned int len);

Rather than forward decls can you reorder the functions?

net/dhcpv6.c line 49

Quoted 6 lines
net/dhcpv6.c new file +6 -0
+
+struct dhcp6_sm_params sm_params;
+
+/*
+ * Handle DHCP received packets (set as UDP handler)
+ */

Please check single-line comment style

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+static void dhcp6_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
+                         unsigned int src, unsigned int len)
+{
+       /* return if ports don't match DHCPv6 ports */
+       if (dest != PORT_DHCP6_C || src != PORT_DHCP6_S)
+               return;
+
+       dhcp6_state_machine(false, pkt, len);
+}
+

[..]

net/dhcpv6.c line 399

Quoted 13 lines
net/dhcpv6.c new file +13 -0
+               case DHCP6_OPTION_OPT_BOOTFILE_URL:
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_URL FOUND\n");
+                       copy_filename(net_boot_file_name, option_ptr, option_len + 1);
+                       debug("net_boot_file_name: %s\n", net_boot_file_name);
+
+                       /* copy server_ip6 (required for PXE) */
+                       s = strchr(net_boot_file_name, '[');
+                       e = strchr(net_boot_file_name, ']');
+                       if (s && e && e > s)
+                               string_to_ip6(s + 1, e - s - 1, &net_server_ip6);
+                       break;
+#if IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION)
+               case DHCP6_OPTION_OPT_BOOTFILE_PARAM:

Can you do something like this to avoid the #ifdef ?

case DHCP6_OPTION_OPT_BOOTFILE_PARAM:
if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION) {
...
}

You could even add a 'fallthough' if you move it to last position in the switch().

net/dhcpv6.c line 408

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_PARAM FOUND\n");
+
+                       if (pxelinux_configfile)
+                               free(pxelinux_configfile);
+
+                       pxelinux_configfile = (char *)malloc((option_len + 1) * sizeof(char));
+                       if (pxelinux_configfile) {
+                               memcpy(pxelinux_configfile, option_ptr, option_len);
+                               pxelinux_configfile[option_len] = '\0';

Does strlcpy() with option_len + 1 work here?

Quoted 1 line

+ }

If malloc() fails it needs to be reported.

Quoted 19 lines
net/dhcpv6.c new file +19 -0
+
+                       debug("PXE CONFIG FILE %s\n", pxelinux_configfile);
+                       break;
+#endif
+               case DHCP6_OPTION_PREFERENCE:
+                       debug("DHCP6_OPTION_PREFERENCE FOUND\n");
+                       sm_params.rx_status.preference = *option_ptr;
+                       break;
+               default:
+                       debug("Unknown Option ID: %d, skipping parsing\n",
+                             ntohs(option_hdr->option_id));
+                       break;
+               }
+               /* Increment to next option header */
+               option_hdr = (struct dhcp6_option_hdr *)(((uchar *)option_hdr) +
+                            sizeof(struct dhcp6_option_hdr) + option_len);
+       }
+}
+

[..]

Quoted 2 lines

+/*
+ * Timeout for DHCP6 SOLICIT/REQUEST.

Fix single-line comment format (globally)

Quoted 27 lines

+ */

net/dhcpv6.c new file +19 -0
+static void dhcp6_timeout_handler(void)
+{
+       /* call state machine with the timeout flag */
+       dhcp6_state_machine(true, NULL, 0);
+}
+
+/*
+ * Start or restart DHCP6
+ */
+void dhcp6_start(void)
+{
+       memset(&sm_params, 0, sizeof(struct dhcp6_sm_params));
+
+       /* seed the RNG with MAC address */
+       srand_mac();
+
+       sm_params.curr_state = DHCP6_INIT;
+       dhcp6_state_machine(false, NULL, 0);
+}
net/dhcpv6.h new file +1 -0
new file mode 100644
index 0000000000..6a0158127a
@@ -0,0 +1,212 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

net/dhcpv6.h line 11

Quoted 10 lines
net/dhcpv6.h new file +10 -0
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+#ifndef __DHCP6_H__
+#define __DHCP6_H__
+
+#include <net6.h>

Is this needed? If so it might be better to split out the bits you need into a net6_defs.h or something like that.

net/dhcpv6.h line 12

Quoted 1 line
net/dhcpv6.h new file +1 -0
+#include <net.h>

Please don't include net.h as it brings in heaps of stuff.

Quoted 6 lines
net/dhcpv6.h new file +6 -0
+
+/* Message types */
+#define DHCP6_MSG_SOLICIT      1
+#define DHCP6_MSG_ADVERTISE    2
+#define DHCP6_MSG_REQUEST      3
+#define DHCP6_MSG_REPLY                7

[..]

net/dhcpv6.h line 209

Quoted 2 lines
net/dhcpv6.h new file +2 -0
+
+/* Send a DHCPv6 request */

Can you add a bit more detail here, e.g. mention the state machine and what happens next?

net/net.c line 126

Quoted 13 lines
net/dhcpv6.h new file +3 -0
+void dhcp6_start(void);
+
+#endif /* __DHCP6_H__ */
net/net.c +2 -0
index c9a749f6cc..73d5b2bc80 100644
@@ -122,6 +122,9 @@
 #endif
 #include <net/tcp.h>
 #include <net/wget.h>
+#if defined(CONFIG_CMD_DHCP6)
+#include "dhcpv6.h"

No need to exclude the header so you can drop the #ifdef

Quoted 16 lines
net/net.c +5 -0
+#endif
 
 /** BOOTP EXTENTIONS **/
 
@@ -135,6 +138,10 @@ struct in_addr net_dns_server;
 /* Our 2nd DNS IP address */
 struct in_addr net_dns_server2;
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+char *pxelinux_configfile;
+#endif
 
 /** END OF BOOTP EXTENTIONS **/
 

net/net.c restart: · line 521

Quoted 5 lines
net/net.c +2 -0
@@ -510,6 +517,11 @@ restart:
                        dhcp_request();         /* Basically same as BOOTP */
                        break;
 #endif
+#if defined(CONFIG_CMD_DHCP6)
+               case DHCP6:

Can we use the if (IS_ENABLED()) thing again here to drop this #ifdef?

Quoted 9 lines
net/net.c +3 -1
+                       dhcp6_start();
+                       break;
+#endif
 #if defined(CONFIG_CMD_BOOTP)
                case BOOTP:
                        bootp_reset();
--

2.40.0

Regards,
Simon

#7 lore raw

Re: [PATCH v2 2/3] net: dhcp6: pxe: Add DHCP/PXE commands for IPv6

In reply to Simon Glass
Quoted 35 lines · Simon Glass

Hi Sean,

Quoted 16 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds commands to support DHCP and PXE with IPv6.

New configs added:
- CMD_DHCP6
- DHCP6_PXE_CLIENTARCH
- DHCP6_PXE_DHCP_OPTION
- DHCP6_ENTERPRISE_ID

New commands added (when IPv6 is enabled):
- dhcp6
- pxe get -ipv6
- pxe boot -ipv6

Quoted 10 lines
---
  boot/bootmeth_distro.c |  2 +-
  boot/bootmeth_pxe.c    |  4 +-
  boot/pxe_utils.c       |  3 +-
  cmd/Kconfig            | 26 +++++++++++++
  cmd/net.c              | 23 +++++++++++
  cmd/pxe.c              | 86 +++++++++++++++++++++++++++++++++++++-----
  cmd/sysboot.c          |  2 +-
  include/pxe_utils.h    | 10 ++++-
  8 files changed, 140 insertions(+), 16 deletions(-)

With nits below:

[..]

Quoted 8 lines

cmd/Kconfig config BOOTP_VCI_STRING · line 1803

Quoted 7 lines
cmd/Kconfig +7 -0
+if CMD_DHCP6
+
+config DHCP6_PXE_CLIENTARCH
+       hex
+       default 0x16 if ARM64
+       default 0x15 if ARM
+       default 0xFF

Do we need a separate option or could we use BOOTP_PXE_CLIENTARCH ?

I created a new option because I wanted to change the default to 0xFF ("undefined" Processor Architecture Types according to https://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml). I wanted to do this without changing BOOTP_PXE_CLIENTARCH , and potentially disrupting exisiting DHCPv4 implementations.

Quoted 257 lines
Quoted 84 lines
cmd/Kconfig +10 -0
+
+config DHCP6_PXE_DHCP_OPTION
+       bool "Request & store 'pxe_configfile' from DHCP6 server"
+
+config DHCP6_ENTERPRISE_ID
+       int "Enterprise ID to send in DHCPv6 Vendor Class Option"
+       default 0
+
+endif
+
  config CMD_TFTPBOOT
         bool "tftpboot"
         default y
cmd/net.c +23 -0
index d5e20843dd..95529a9d12 100644
@@ -111,6 +111,29 @@ U_BOOT_CMD(
  );
  #endif
 
+#if defined(CONFIG_CMD_DHCP6)
+static int do_dhcp6(struct cmd_tbl *cmdtp, int flag, int argc,
+                   char *const argv[])
+{
+       int i;
+       int dhcp_argc;
+       char *dhcp_argv[] = {NULL, NULL, NULL, NULL};
+
+       /* Add -ipv6 flag for autoload */
+       for (i = 0; i < argc; i++)
+               dhcp_argv[i] = argv[i];
+       dhcp_argc = argc + 1;
+       dhcp_argv[dhcp_argc - 1] =  USE_IP6_CMD_PARAM;
+
+       return netboot_common(DHCP6, cmdtp, dhcp_argc, dhcp_argv);
+}
+
+U_BOOT_CMD(dhcp6,      3,      1,      do_dhcp6,
+          "boot image via network using DHCPv6/TFTP protocol. \n"
+          "Use IPv6 hostIPaddr framed with [] brackets",
+          "[loadAddress] [[hostIPaddr:]bootfilename]");
+#endif
+
  #if defined(CONFIG_CMD_DHCP)
  static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
                    char *const argv[])
cmd/pxe.c +10 -1
index db8e4697f2..71e6fd9633 100644
@@ -8,6 +8,7 @@
  #include <command.h>
  #include <fs.h>
  #include <net.h>
+#include <net6.h>
 
  #include "pxe_utils.h"
 
@@ -29,12 +30,20 @@ static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
  {
         char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
         int ret;
+       int num_args;
 
         tftp_argv[1] = file_addr;
         tftp_argv[2] = (void *)file_path;
+       if (ctx->use_ipv6) {
+               tftp_argv[3] = USE_IP6_CMD_PARAM;
+               num_args = 4;
+       } else {
+               num_args = 3;
+       }
 
-       if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
+       if (do_tftpb(ctx->cmdtp, 0, num_args, tftp_argv))
                 return -ENOENT;
+
         ret = pxe_get_file_size(sizep);
         if (ret)
                 return log_msg_ret("tftp", ret);
@@ -43,6 +52,23 @@ static int do_get_tftp(struct pxe_context *ctx, const char *file_path,
         return 1;
  }

cmd/pxe.c static int do_get_tftp(struct pxe_context *ctx, const char *file_path, · line 63

Quoted 10 lines
cmd/pxe.c +9 -0
 
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/*
+ * Looks for a pxe file with specified config file name,
+ * which is received from DHCPv4 option 209 or
+ * DHCPv6 option 60.
+ *
+ * Returns 1 on success or < 0 on error.
+ */
+static inline int pxe_dhcp_option_path(struct pxe_context *ctx, unsigned long pxefile_addr_r)

Please drop the inline as the compiler can handle that.

Quoted 18 lines
cmd/pxe.c +9 -1
+{
+       int ret = get_pxe_file(ctx, pxelinux_configfile, pxefile_addr_r);
+
+       free(pxelinux_configfile);
+
+       return ret;
+}
+#endif
  /*
   * Looks for a pxe file with a name based on the pxeuuid environment variable.
   *
@@ -105,15 +131,25 @@ static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_
         return -ENOENT;
  }
 
-int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep)
+int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep, bool use_ipv6)
  {

cmd/pxe.c static int pxe_ipaddr_paths(struct pxe_context *ctx, unsigned long pxefile_addr_ · line 144

Quoted 10 lines
cmd/pxe.c +3 -1
         struct cmd_tbl cmdtp[] = {};    /* dummy */
         struct pxe_context ctx;
         int i;
 
         if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
-                         env_get("bootfile")))
+                         env_get("bootfile"), use_ipv6))
                 return -ENOMEM;
+
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

It's a bit annoying that pxelinux_configfile causes these #ifdefs. How about always having pxelinux_configfile and then you don't need to worry. It can just be NULL when not used. It is only BSS space, after all...

Quoted 12 lines
cmd/pxe.c +8 -0
+       if (pxelinux_configfile && use_ipv6) {
+               if (pxe_dhcp_option_path(&ctx, pxefile_addr_r) > 0)
+                       goto done;
+
+               goto error_exit;
+       }
+#endif
+
         /*
          * Keep trying paths until we successfully get a file we're looking
          * for.

cmd/pxe.c int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep) · line 170

Quoted 4 lines
cmd/pxe.c +1 -0
@@ -131,9 +167,12 @@ int pxe_get(ulong pxefile_addr_r, char **bootdirp, ulong *sizep)
                 i++;
         }
 
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

...then you can use if (IS_ENABLED(... here

Quoted 69 lines
cmd/pxe.c +22 -6
+error_exit:
         pxe_destroy_ctx(&ctx);
 
         return -ENOENT;
+#endif
  done:
         *bootdirp = env_get("bootfile");
 
@@ -169,9 +208,17 @@ do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
         char *fname;
         ulong size;
         int ret;
+       bool use_ipv6 = false;
 
-       if (argc != 1)
-               return CMD_RET_USAGE;
+       if (IS_ENABLED(CONFIG_IPV6)) {
+               if (argc != 1 && argc != 2)
+                       return CMD_RET_USAGE;
+               if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
+                       use_ipv6 = true;
+       } else {
+               if (argc != 1)
+                       return CMD_RET_USAGE;
+       }
 
         pxefile_addr_str = from_env("pxefile_addr_r");
 
@@ -183,7 +230,7 @@ do_pxe_get(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
         if (ret < 0)
                 return 1;
 
-       ret = pxe_get(pxefile_addr_r, &fname, &size);
+       ret = pxe_get(pxefile_addr_r, &fname, &size, use_ipv6);
         switch (ret) {
         case 0:
                 printf("Config file '%s' found\n", fname);
@@ -211,13 +258,19 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
         char *pxefile_addr_str;
         struct pxe_context ctx;
         int ret;
+       bool use_ipv6 = false;
+
+       if (IS_ENABLED(CONFIG_IPV6)) {
+               if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM))
+                       use_ipv6 = true;
+       }
 
-       if (argc == 1) {
+       if (argc == 1 || (argc == 2 && use_ipv6)) {
                 pxefile_addr_str = from_env("pxefile_addr_r");
                 if (!pxefile_addr_str)
                         return 1;
 
-       } else if (argc == 2) {
+       } else if (argc == 2 || (argc == 3 && use_ipv6)) {
                 pxefile_addr_str = argv[1];
         } else {
                 return CMD_RET_USAGE;
@@ -229,7 +282,7 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
         }
 
         if (pxe_setup_ctx(&ctx, cmdtp, do_get_tftp, NULL, false,
-                         env_get("bootfile"))) {
+                         env_get("bootfile"), use_ipv6)) {
                 printf("Out of memory\n");
                 return CMD_RET_FAILURE;
         }

cmd/pxe.c do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 306

Quoted 10 lines
cmd/pxe.c +5 -0
@@ -244,8 +297,13 @@ do_pxe_boot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  }
 
  static struct cmd_tbl cmd_pxe_sub[] = {
+#if IS_ENABLED(CONFIG_IPV6)
+       U_BOOT_CMD_MKENT(get, 2, 1, do_pxe_get, "", ""),
+       U_BOOT_CMD_MKENT(boot, 3, 1, do_pxe_boot, "", "")
+#else
         U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
         U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
+#endif

That's a bit ugly. How about using the max and then checking it in the C code?

Quoted 11 lines
cmd/pxe.c +4 -0
  };
 
  static void __maybe_unused pxe_reloc(void)
@@ -281,9 +339,19 @@ static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
         return CMD_RET_USAGE;
  }
 
+#if IS_ENABLED(CONFIG_IPV6)
+U_BOOT_CMD(pxe, 4, 1, do_pxe,
+          "commands to get and boot from pxe files\n"
+          "To use IPv6 add -ipv6 parameter",

cmd/pxe.c static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 355

Quoted 10 lines
cmd/pxe.c +4 -0
+          "get [" USE_IP6_CMD_PARAM "] - try to retrieve a pxe file using tftp\n"
+          "pxe boot [pxefile_addr_r] [-ipv6] - boot from the pxe file at pxefile_addr_r\n"
+);
+#else
  U_BOOT_CMD(pxe, 3, 1, do_pxe,
            "commands to get and boot from pxe files",
            "get - try to retrieve a pxe file using tftp\n"
            "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  );
  #endif

same here

cmd/pxe.c static int do_pxe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 357

Quoted 2 lines
cmd/pxe.c +2 -0
+
+#endif /* CONFIG_CMD_NET */

[..]

Regards,
Simon

#8 lore raw

Re: [PATCH v2 2/3] net: dhcp6: pxe: Add DHCP/PXE commands for IPv6

Hi Sean,

Quoted 46 lines · Sean Edmond
Quoted 35 lines · Simon Glass

Hi Sean,

Quoted 16 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds commands to support DHCP and PXE with IPv6.

New configs added:
- CMD_DHCP6
- DHCP6_PXE_CLIENTARCH
- DHCP6_PXE_DHCP_OPTION
- DHCP6_ENTERPRISE_ID

New commands added (when IPv6 is enabled):
- dhcp6
- pxe get -ipv6
- pxe boot -ipv6

Quoted 10 lines
---
  boot/bootmeth_distro.c |  2 +-
  boot/bootmeth_pxe.c    |  4 +-
  boot/pxe_utils.c       |  3 +-
  cmd/Kconfig            | 26 +++++++++++++
  cmd/net.c              | 23 +++++++++++
  cmd/pxe.c              | 86 +++++++++++++++++++++++++++++++++++++-----
  cmd/sysboot.c          |  2 +-
  include/pxe_utils.h    | 10 ++++-
  8 files changed, 140 insertions(+), 16 deletions(-)

With nits below:

[..]

Quoted 8 lines

cmd/Kconfig config BOOTP_VCI_STRING · line 1803

Quoted 7 lines
cmd/Kconfig +7 -0
+if CMD_DHCP6
+
+config DHCP6_PXE_CLIENTARCH
+       hex
+       default 0x16 if ARM64
+       default 0x15 if ARM
+       default 0xFF

Do we need a separate option or could we use BOOTP_PXE_CLIENTARCH ?

Quoted 5 lines

I created a new option because I wanted to change the default to 0xFF ("undefined" Processor Architecture Types according to
https://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml).
I wanted to do this without changing BOOTP_PXE_CLIENTARCH , and potentially disrupting exisiting DHCPv4 implementations.

OK, I see.

Regards,
Simon

#3 lore raw

Re: [PATCH v2 1/3] net: dhcp6: Add DHCPv6 (DHCP for IPv6)

In reply to Simon Glass
Quoted 331 lines · Simon Glass

Hi,

Quoted 16 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds DHCPv6 protocol to u-boot.

Allows for address assignement with DHCPv6 4-message exchange (SOLICIT->ADVERTISE->REQUEST->REPLY). Includes DHCPv6 options required by RFC 8415. Also adds DHCPv6 options required for PXE boot.

Possible enhancements:
- Duplicate address detection on DHCPv6 assigned address
- IPv6 address assignement through SLAAC
- Sending/parsing other DHCPv6 options (NTP, DNS, etc...)

Quoted 9 lines
---
 include/net.h |   8 +-
 net/Makefile  |   1 +
 net/dhcpv6.c  | 735 ++++++++++++++++++++++++++++++++++++++++++++++++++
 net/dhcpv6.h  | 212 +++++++++++++++
 net/net.c     |  12 +
 5 files changed, 966 insertions(+), 2 deletions(-)
 create mode 100644 net/dhcpv6.c
 create mode 100644 net/dhcpv6.h

This looks good to me. I just have a few nits below. With those fixed:

include/net.h extern char net_hostname[32]; /* Our hostname */ · line 487

Quoted 10 lines
include/net.h +1 -0
index 399af5e064..cac818e292 100644
@@ -484,6 +484,10 @@ extern char        net_hostname[32];       /* Our hostname */
 #ifdef CONFIG_NET
 extern char    net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN];  /* Our root path */
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

You can drop this #ifdef as any reference to a non-existent var will give a build error.

Quoted 49 lines
include/net.h +5 -2
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+extern char *pxelinux_configfile;
+#endif
 /** END OF BOOTP EXTENTIONS **/
 extern u8              net_ethaddr[ARP_HLEN];          /* Our ethernet address */
 extern u8              net_server_ethaddr[ARP_HLEN];   /* Boot server enet address */
@@ -504,8 +508,8 @@ extern ushort               net_native_vlan;        /* Our Native VLAN */
 extern int             net_restart_wrap;       /* Tried all network devices */
 
 enum proto_t {
-       BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS,
-       SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
+       BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
+       NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
 };
 
 extern char    net_boot_file_name[1024];/* Boot File name */
net/Makefile +1 -0
index bea000b206..5968110170 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_IPV6)     += net6.o
 obj-$(CONFIG_CMD_NFS)  += nfs.o
 obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_PING6) += ping6.o
+obj-$(CONFIG_CMD_DHCP6) += dhcpv6.o
 obj-$(CONFIG_CMD_PCAP) += pcap.o
 obj-$(CONFIG_CMD_RARP) += rarp.o
 obj-$(CONFIG_CMD_SNTP) += sntp.o
net/dhcpv6.c new file +14 -0
new file mode 100644
index 0000000000..9204909c1f
@@ -0,0 +1,735 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+/* Simple DHCP6 network layer implementation. */
+
+#include <common.h>
+#include <bootstage.h>
+#include <command.h>
+#include <env.h>
+#include <efi_loader.h>

net/dhcpv6.c line 24

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#include <log.h>
+#include <net.h>
+#include <rand.h>
+#include <uuid.h>
+#include <linux/delay.h>
+#include <net/tftp.h>
+#include "dhcpv6.h"
+#include <net6.h>
+#include <malloc.h>
+#include "net_rand.h"

Please fix header order:
https://u-boot.readthedocs.io/en/latest/develop/codingstyle.html#include-files

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+
+#define PORT_DHCP6_S   547     /* DHCP6 server UDP port */
+#define PORT_DHCP6_C   546     /* DHCP6 client UDP port */
+
+/* default timeout parameters (in ms) */
+#define SOL_MAX_DELAY_MS       1000
+#define SOL_TIMEOUT_MS         1000
+#define SOL_MAX_RT_MS          3600000
+#define REQ_TIMEOUT_MS         1000

net/dhcpv6.c line 43

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#define REQ_MAX_RT_MS          30000
+#define REQ_MAX_RC             10
+#define MAX_WAIT_TIME_MS       60000
+
+/* global variable to track any updates from DHCP6 server */
+int updated_sol_max_rt_ms = SOL_MAX_RT_MS;
+
+static void dhcp6_timeout_handler(void);
+static void dhcp6_state_machine(bool timeout, uchar *rx_pkt, unsigned int len);
+static void dhcp6_parse_options(uchar *rx_pkt, unsigned int len);

Rather than forward decls can you reorder the functions?

net/dhcpv6.c line 49

Quoted 6 lines
net/dhcpv6.c new file +6 -0
+
+struct dhcp6_sm_params sm_params;
+
+/*
+ * Handle DHCP received packets (set as UDP handler)
+ */

Please check single-line comment style

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+static void dhcp6_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
+                         unsigned int src, unsigned int len)
+{
+       /* return if ports don't match DHCPv6 ports */
+       if (dest != PORT_DHCP6_C || src != PORT_DHCP6_S)
+               return;
+
+       dhcp6_state_machine(false, pkt, len);
+}
+

[..]

net/dhcpv6.c line 399

Quoted 13 lines
net/dhcpv6.c new file +13 -0
+               case DHCP6_OPTION_OPT_BOOTFILE_URL:
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_URL FOUND\n");
+                       copy_filename(net_boot_file_name, option_ptr, option_len + 1);
+                       debug("net_boot_file_name: %s\n", net_boot_file_name);
+
+                       /* copy server_ip6 (required for PXE) */
+                       s = strchr(net_boot_file_name, '[');
+                       e = strchr(net_boot_file_name, ']');
+                       if (s && e && e > s)
+                               string_to_ip6(s + 1, e - s - 1, &net_server_ip6);
+                       break;
+#if IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION)
+               case DHCP6_OPTION_OPT_BOOTFILE_PARAM:

Can you do something like this to avoid the #ifdef ?

case DHCP6_OPTION_OPT_BOOTFILE_PARAM:

   if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION) {

...
}

You could even add a 'fallthough' if you move it to last position in the switch().

net/dhcpv6.c line 408

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_PARAM FOUND\n");
+
+                       if (pxelinux_configfile)
+                               free(pxelinux_configfile);
+
+                       pxelinux_configfile = (char *)malloc((option_len + 1) * sizeof(char));
+                       if (pxelinux_configfile) {
+                               memcpy(pxelinux_configfile, option_ptr, option_len);
+                               pxelinux_configfile[option_len] = '\0';

Does strlcpy() with option_len + 1 work here?

Quoted 1 line

+ }

If malloc() fails it needs to be reported.

Quoted 19 lines
net/dhcpv6.c new file +19 -0
+
+                       debug("PXE CONFIG FILE %s\n", pxelinux_configfile);
+                       break;
+#endif
+               case DHCP6_OPTION_PREFERENCE:
+                       debug("DHCP6_OPTION_PREFERENCE FOUND\n");
+                       sm_params.rx_status.preference = *option_ptr;
+                       break;
+               default:
+                       debug("Unknown Option ID: %d, skipping parsing\n",
+                             ntohs(option_hdr->option_id));
+                       break;
+               }
+               /* Increment to next option header */
+               option_hdr = (struct dhcp6_option_hdr *)(((uchar *)option_hdr) +
+                            sizeof(struct dhcp6_option_hdr) + option_len);
+       }
+}
+

[..]

Quoted 2 lines

+/*
+ * Timeout for DHCP6 SOLICIT/REQUEST.

Fix single-line comment format (globally)

Quoted 27 lines

+ */

net/dhcpv6.c new file +19 -0
+static void dhcp6_timeout_handler(void)
+{
+       /* call state machine with the timeout flag */
+       dhcp6_state_machine(true, NULL, 0);
+}
+
+/*
+ * Start or restart DHCP6
+ */
+void dhcp6_start(void)
+{
+       memset(&sm_params, 0, sizeof(struct dhcp6_sm_params));
+
+       /* seed the RNG with MAC address */
+       srand_mac();
+
+       sm_params.curr_state = DHCP6_INIT;
+       dhcp6_state_machine(false, NULL, 0);
+}
net/dhcpv6.h new file +1 -0
new file mode 100644
index 0000000000..6a0158127a
@@ -0,0 +1,212 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

net/dhcpv6.h line 11

Quoted 10 lines
net/dhcpv6.h new file +10 -0
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+#ifndef __DHCP6_H__
+#define __DHCP6_H__
+
+#include <net6.h>

Is this needed? If so it might be better to split out the bits you need into a net6_defs.h or something like that.

net/dhcpv6.h line 12

Quoted 1 line
net/dhcpv6.h new file +1 -0
+#include <net.h>

Please don't include net.h as it brings in heaps of stuff.

Quoted 6 lines
net/dhcpv6.h new file +6 -0
+
+/* Message types */
+#define DHCP6_MSG_SOLICIT      1
+#define DHCP6_MSG_ADVERTISE    2
+#define DHCP6_MSG_REQUEST      3
+#define DHCP6_MSG_REPLY                7

[..]

net/dhcpv6.h line 209

Quoted 2 lines
net/dhcpv6.h new file +2 -0
+
+/* Send a DHCPv6 request */

Can you add a bit more detail here, e.g. mention the state machine and what happens next?

net/net.c line 126

Quoted 13 lines
net/dhcpv6.h new file +3 -0
+void dhcp6_start(void);
+
+#endif /* __DHCP6_H__ */
net/net.c +2 -0
index c9a749f6cc..73d5b2bc80 100644
@@ -122,6 +122,9 @@
 #endif
 #include <net/tcp.h>
 #include <net/wget.h>
+#if defined(CONFIG_CMD_DHCP6)
+#include "dhcpv6.h"

No need to exclude the header so you can drop the #ifdef

Quoted 16 lines
net/net.c +5 -0
+#endif
 
 /** BOOTP EXTENTIONS **/
 
@@ -135,6 +138,10 @@ struct in_addr net_dns_server;
 /* Our 2nd DNS IP address */
 struct in_addr net_dns_server2;
 #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+char *pxelinux_configfile;
+#endif
 
 /** END OF BOOTP EXTENTIONS **/
 

net/net.c restart: · line 521

Quoted 5 lines
net/net.c +2 -0
@@ -510,6 +517,11 @@ restart:
                        dhcp_request();         /* Basically same as BOOTP */
                        break;
 #endif
+#if defined(CONFIG_CMD_DHCP6)
+               case DHCP6:

Can we use the if (IS_ENABLED()) thing again here to drop this #ifdef?

Quoted 9 lines
net/net.c +3 -1
+                       dhcp6_start();
+                       break;
+#endif
 #if defined(CONFIG_CMD_BOOTP)
                case BOOTP:
                        bootp_reset();
--

2.40.0

Quoted 2 lines

Regards,
Simon

Nicely done, please address Simon comments and I'll accept.

Acked-by
#11 lore raw

Re: [PATCH v2 3/3] net: dhcp6: Add a sandbox test for dhcp6

In reply to Simon Glass
Quoted 7 lines · Simon Glass
Quoted 5 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Requires proper environment with DHCP6 server provisioned.

Quoted 7 lines
Quoted 5 lines
---
 configs/sandbox_defconfig |  1 +
 test/py/tests/test_net.py | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
#4 lore raw

Re: [PATCH v2 1/3] net: dhcp6: Add DHCPv6 (DHCP for IPv6)

In reply to Ramon Fried
Quoted 312 lines · Ramon Fried
Quoted 311 lines · Simon Glass

Hi,

Quoted 22 lines · On Fri, 7 Apr 2023 at 18:56, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Adds DHCPv6 protocol to u-boot.

Allows for address assignement with DHCPv6 4-message exchange (SOLICIT->ADVERTISE->REQUEST->REPLY). Includes DHCPv6 options required by RFC 8415. Also adds DHCPv6 options required for PXE boot.

Possible enhancements:
- Duplicate address detection on DHCPv6 assigned address
- IPv6 address assignement through SLAAC
- Sending/parsing other DHCPv6 options (NTP, DNS, etc...)

---
  include/net.h |   8 +-
  net/Makefile  |   1 +
  net/dhcpv6.c  | 735 ++++++++++++++++++++++++++++++++++++++++++++++++++
  net/dhcpv6.h  | 212 +++++++++++++++
  net/net.c     |  12 +
  5 files changed, 966 insertions(+), 2 deletions(-)
Quoted 2 lines

create mode 100644 net/dhcpv6.c
create mode 100644 net/dhcpv6.h

This looks good to me. I just have a few nits below. With those fixed:

include/net.h extern char net_hostname[32]; /* Our hostname */ · line 487

Quoted 9 lines
include/net.h +1 -0
index 399af5e064..cac818e292 100644
@@ -484,6 +484,10 @@ extern char        net_hostname[32];       /* Our hostname */
  #ifdef CONFIG_NET
  extern char    net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN];  /* Our root path */
  #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)

You can drop this #ifdef as any reference to a non-existent var will give a build error.

Quoted 49 lines
include/net.h +5 -2
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+extern char *pxelinux_configfile;
+#endif
  /** END OF BOOTP EXTENTIONS **/
  extern u8              net_ethaddr[ARP_HLEN];          /* Our ethernet address */
  extern u8              net_server_ethaddr[ARP_HLEN];   /* Boot server enet address */
@@ -504,8 +508,8 @@ extern ushort               net_native_vlan;        /* Our Native VLAN */
  extern int             net_restart_wrap;       /* Tried all network devices */
 
  enum proto_t {
-       BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS,
-       SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
+       BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
+       NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
  };
 
  extern char    net_boot_file_name[1024];/* Boot File name */
net/Makefile +1 -0
index bea000b206..5968110170 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_IPV6)     += net6.o
  obj-$(CONFIG_CMD_NFS)  += nfs.o
  obj-$(CONFIG_CMD_PING) += ping.o
  obj-$(CONFIG_CMD_PING6) += ping6.o
+obj-$(CONFIG_CMD_DHCP6) += dhcpv6.o
  obj-$(CONFIG_CMD_PCAP) += pcap.o
  obj-$(CONFIG_CMD_RARP) += rarp.o
  obj-$(CONFIG_CMD_SNTP) += sntp.o
net/dhcpv6.c new file +14 -0
new file mode 100644
index 0000000000..9204909c1f
@@ -0,0 +1,735 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+/* Simple DHCP6 network layer implementation. */
+
+#include <common.h>
+#include <bootstage.h>
+#include <command.h>
+#include <env.h>
+#include <efi_loader.h>

net/dhcpv6.c line 24

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#include <log.h>
+#include <net.h>
+#include <rand.h>
+#include <uuid.h>
+#include <linux/delay.h>
+#include <net/tftp.h>
+#include "dhcpv6.h"
+#include <net6.h>
+#include <malloc.h>
+#include "net_rand.h"

Please fix header order:
https://u-boot.readthedocs.io/en/latest/develop/codingstyle.html#include-files

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+
+#define PORT_DHCP6_S   547     /* DHCP6 server UDP port */
+#define PORT_DHCP6_C   546     /* DHCP6 client UDP port */
+
+/* default timeout parameters (in ms) */
+#define SOL_MAX_DELAY_MS       1000
+#define SOL_TIMEOUT_MS         1000
+#define SOL_MAX_RT_MS          3600000
+#define REQ_TIMEOUT_MS         1000

net/dhcpv6.c line 43

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+#define REQ_MAX_RT_MS          30000
+#define REQ_MAX_RC             10
+#define MAX_WAIT_TIME_MS       60000
+
+/* global variable to track any updates from DHCP6 server */
+int updated_sol_max_rt_ms = SOL_MAX_RT_MS;
+
+static void dhcp6_timeout_handler(void);
+static void dhcp6_state_machine(bool timeout, uchar *rx_pkt, unsigned int len);
+static void dhcp6_parse_options(uchar *rx_pkt, unsigned int len);

Rather than forward decls can you reorder the functions?

net/dhcpv6.c line 49

Quoted 6 lines
net/dhcpv6.c new file +6 -0
+
+struct dhcp6_sm_params sm_params;
+
+/*
+ * Handle DHCP received packets (set as UDP handler)
+ */

Please check single-line comment style

Quoted 10 lines
net/dhcpv6.c new file +10 -0
+static void dhcp6_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
+                         unsigned int src, unsigned int len)
+{
+       /* return if ports don't match DHCPv6 ports */
+       if (dest != PORT_DHCP6_C || src != PORT_DHCP6_S)
+               return;
+
+       dhcp6_state_machine(false, pkt, len);
+}
+

[..]

net/dhcpv6.c line 399

Quoted 13 lines
net/dhcpv6.c new file +13 -0
+               case DHCP6_OPTION_OPT_BOOTFILE_URL:
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_URL FOUND\n");
+                       copy_filename(net_boot_file_name, option_ptr, option_len + 1);
+                       debug("net_boot_file_name: %s\n", net_boot_file_name);
+
+                       /* copy server_ip6 (required for PXE) */
+                       s = strchr(net_boot_file_name, '[');
+                       e = strchr(net_boot_file_name, ']');
+                       if (s && e && e > s)
+                               string_to_ip6(s + 1, e - s - 1, &net_server_ip6);
+                       break;
+#if IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION)
+               case DHCP6_OPTION_OPT_BOOTFILE_PARAM:

Can you do something like this to avoid the #ifdef ?

case DHCP6_OPTION_OPT_BOOTFILE_PARAM:

    if (IS_ENABLED(CONFIG_DHCP6_PXE_DHCP_OPTION) {
...
    }

You could even add a 'fallthough' if you move it to last position in the switch().

net/dhcpv6.c line 408

Quoted 9 lines
net/dhcpv6.c new file +9 -0
+                       debug("DHCP6_OPTION_OPT_BOOTFILE_PARAM FOUND\n");
+
+                       if (pxelinux_configfile)
+                               free(pxelinux_configfile);
+
+                       pxelinux_configfile = (char *)malloc((option_len + 1) * sizeof(char));
+                       if (pxelinux_configfile) {
+                               memcpy(pxelinux_configfile, option_ptr, option_len);
+                               pxelinux_configfile[option_len] = '\0';

Does strlcpy() with option_len + 1 work here?

Quoted 1 line

+ }

If malloc() fails it needs to be reported.

Quoted 19 lines
net/dhcpv6.c new file +19 -0
+
+                       debug("PXE CONFIG FILE %s\n", pxelinux_configfile);
+                       break;
+#endif
+               case DHCP6_OPTION_PREFERENCE:
+                       debug("DHCP6_OPTION_PREFERENCE FOUND\n");
+                       sm_params.rx_status.preference = *option_ptr;
+                       break;
+               default:
+                       debug("Unknown Option ID: %d, skipping parsing\n",
+                             ntohs(option_hdr->option_id));
+                       break;
+               }
+               /* Increment to next option header */
+               option_hdr = (struct dhcp6_option_hdr *)(((uchar *)option_hdr) +
+                            sizeof(struct dhcp6_option_hdr) + option_len);
+       }
+}
+

[..]

Quoted 2 lines

+/*
+ * Timeout for DHCP6 SOLICIT/REQUEST.

Fix single-line comment format (globally)

Quoted 27 lines

+ */

net/dhcpv6.c new file +19 -0
+static void dhcp6_timeout_handler(void)
+{
+       /* call state machine with the timeout flag */
+       dhcp6_state_machine(true, NULL, 0);
+}
+
+/*
+ * Start or restart DHCP6
+ */
+void dhcp6_start(void)
+{
+       memset(&sm_params, 0, sizeof(struct dhcp6_sm_params));
+
+       /* seed the RNG with MAC address */
+       srand_mac();
+
+       sm_params.curr_state = DHCP6_INIT;
+       dhcp6_state_machine(false, NULL, 0);
+}
net/dhcpv6.h new file +1 -0
new file mode 100644
index 0000000000..6a0158127a
@@ -0,0 +1,212 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

net/dhcpv6.h line 11

Quoted 10 lines
net/dhcpv6.h new file +10 -0
+/*
+ * Copyright (C) Microsoft Corporation
+ * Author: Sean Edmond <[email protected]>
+ *
+ */
+
+#ifndef __DHCP6_H__
+#define __DHCP6_H__
+
+#include <net6.h>

Is this needed? If so it might be better to split out the bits you need into a net6_defs.h or something like that.

net/dhcpv6.h line 12

Quoted 1 line
net/dhcpv6.h new file +1 -0
+#include <net.h>

Please don't include net.h as it brings in heaps of stuff.

Quoted 6 lines
net/dhcpv6.h new file +6 -0
+
+/* Message types */
+#define DHCP6_MSG_SOLICIT      1
+#define DHCP6_MSG_ADVERTISE    2
+#define DHCP6_MSG_REQUEST      3
+#define DHCP6_MSG_REPLY                7

[..]

net/dhcpv6.h line 209

Quoted 2 lines
net/dhcpv6.h new file +2 -0
+
+/* Send a DHCPv6 request */

Can you add a bit more detail here, e.g. mention the state machine and what happens next?

net/net.c line 126

Quoted 13 lines
net/dhcpv6.h new file +3 -0
+void dhcp6_start(void);
+
+#endif /* __DHCP6_H__ */
net/net.c +2 -0
index c9a749f6cc..73d5b2bc80 100644
@@ -122,6 +122,9 @@
  #endif
  #include <net/tcp.h>
  #include <net/wget.h>
+#if defined(CONFIG_CMD_DHCP6)
+#include "dhcpv6.h"

No need to exclude the header so you can drop the #ifdef

Quoted 16 lines
net/net.c +5 -0
+#endif
 
  /** BOOTP EXTENTIONS **/
 
@@ -135,6 +138,10 @@ struct in_addr net_dns_server;
  /* Our 2nd DNS IP address */
  struct in_addr net_dns_server2;
  #endif
+#if defined(CONFIG_DHCP6_PXE_DHCP_OPTION)
+/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
+char *pxelinux_configfile;
+#endif
 
  /** END OF BOOTP EXTENTIONS **/
 

net/net.c restart: · line 521

Quoted 5 lines
net/net.c +2 -0
@@ -510,6 +517,11 @@ restart:
                         dhcp_request();         /* Basically same as BOOTP */
                         break;
  #endif
+#if defined(CONFIG_CMD_DHCP6)
+               case DHCP6:

Can we use the if (IS_ENABLED()) thing again here to drop this #ifdef?

Quoted 9 lines
net/net.c +3 -1
+                       dhcp6_start();
+                       break;
+#endif
  #if defined(CONFIG_CMD_BOOTP)
                 case BOOTP:
                         bootp_reset();
--

2.40.0

Quoted 3 lines
Quoted 2 lines

Regards,
Simon

Nicely done, please address Simon comments and I'll accept.

All of Simon's comments were addressed in v3 of the patch series.  Let me know if you need me to add more detail.