UEFI networking support including ipv6 + dhcpv6
v4 cover UEFI networking support including ipv6 + dhcpv6
This series is an omnibus of changes we carry for networking that has accumulated since at least 2015. While most of it did not receive review when previously posted, one did, so I'm using that as an excuse to call this v4.
At the start, this includes a proposed revert of ac8a37dda0eabdd80506bebe4fb9a5a9fd227935 ("net/http: Allow use of non-standard TCP/IP ports"). In 2016, a different approach to this more in keeping with the way port numbers are typically specified was posted on-list:
https://lists.gnu.org/archive/html/grub-devel/2016-04/msg00071.html [1]
At least Debian, Red Hat, SUSE, and Ubuntu have carried it since then, which means that code in the wild expects this syntax, not the other syntax. (Others may have as well - I didn't do an exhaustive check - but hopefully that's enough to convey the point.)
At this point, the division of code between patches is mostly organic. I've folded bugfixes into their original commits and moved a few other things around, so while this doesn't exactly match the division in the rhboot tree, it does match the code. At this version, the only code differences are style adjustments and rebasing around the vlan changes that merged since 2.06.
Be well,
--Robbie
1: For the curious, while the strtoull patch mentioned there was also not
taken, the issue is no longer present.
Aaron Miller (1):
net: read bracketed ipv6 addrs and port numbers
Ian Page Hands (1):
efinet: Add DHCP proxy support
Javier Martinez Canillas (3):
efi/http: Enclose literal IPv6 addresses in square brackets
http: Prepend prefix when the HTTP path is relative normal/main: Discover the device to read the config from as a fallback
Mark Salter (1):
use fw_path prefix when fallback searching for grub config
Michael Chang (6):
efinet: add structures for PXE messages
grub.texi: Add net_bootp6 doumentation
bootp: Process DHCPACK packet during HTTP Boot
efinet Configure network from UEFI device path
efinet: set DNS server from UEFI protocol
Support UEFI networking protocols
Paulo Flabiano Smorigo (1):
Add fw_path variable to detect config file on efi
Peter Jones (4):
efinet + bootp: add net_bootp6 command supporting dhcpv6
efinet: also use the firmware acceleration for http efi/http: match protocol+hostname of boot url in root_url Try mac/guid/etc before grub.cfg on tftp config files
Robbie Harwood (1):
Revert "net/http: Allow use of non-standard TCP/IP ports"
Stephen Benjamin (1):
Prepend prefix when HTTP path is relative
docs/grub.texi | 50 +- grub-core/Makefile.core.def | 12 + grub-core/io/bufio.c | 2 +- grub-core/kern/efi/efi.c | 96 +- grub-core/kern/ieee1275/init.c | 28 +- grub-core/kern/main.c | 22 +- grub-core/net/bootp.c | 965 ++++++++++++++++++- grub-core/net/drivers/efi/efinet.c | 521 +++++++++- grub-core/net/efi/dhcp.c | 357 +++++++ grub-core/net/efi/efi_netfs.c | 57 ++ grub-core/net/efi/http.c | 490 ++++++++++ grub-core/net/efi/ip4_config.c | 417 ++++++++ grub-core/net/efi/ip6_config.c | 423 +++++++++ grub-core/net/efi/net.c | 1416 ++++++++++++++++++++++++++++ grub-core/net/efi/pxe.c | 354 +++++++ grub-core/net/http.c | 76 +- grub-core/net/ip.c | 39 + grub-core/net/net.c | 235 ++++- grub-core/net/tftp.c | 10 +- grub-core/normal/main.c | 149 ++- include/grub/efi/api.h | 397 +++++++- include/grub/efi/dhcp.h | 347 +++++++ include/grub/efi/http.h | 215 +++++ include/grub/net.h | 81 ++ include/grub/net/efi.h | 144 +++ util/grub-mknetdir.c | 23 +- 26 files changed, 6736 insertions(+), 190 deletions(-) create mode 100644 grub-core/net/efi/dhcp.c create mode 100644 grub-core/net/efi/efi_netfs.c create mode 100644 grub-core/net/efi/http.c create mode 100644 grub-core/net/efi/ip4_config.c create mode 100644 grub-core/net/efi/ip6_config.c create mode 100644 grub-core/net/efi/net.c create mode 100644 grub-core/net/efi/pxe.c create mode 100644 include/grub/efi/dhcp.h create mode 100644 include/grub/efi/http.h create mode 100644 include/grub/net/efi.h
Signature · 1 line
2.39.0
PATCH v4 1/19 Revert "net/http: Allow use of non-standard TCP/IP ports"
This reverts commit ac8a37dda0eabdd80506bebe4fb9a5a9fd227935.
--- docs/grub.texi | 33 --------------------------------- grub-core/net/http.c | 40 ++-------------------------------------- 2 files changed, 2 insertions(+), 71 deletions(-)
docs/grub.texi +0 -33
@@ -3053,39 +3053,6 @@ environment variable @samp{net_default_server} is used. Before using the network drive, you must initialize the network. @xref{Network}, for more information. -For the @samp{http} network protocol, @code{@var{server}} may specify a -port number other than the default value of @samp{80}. The server name -and port number are separated by either @samp{,} or @samp{:}. -For IPv6 addresses, the server name and port number may only be separated -by @samp{,}. - -@itemize @bullet -@item -@code{(http,@var{server},@var{port})} - -@item -@code{(http,@var{server}:@var{port})} -@end itemize - -These examples all reference an @samp{http} server at address -@samp{192.0.2.1} listening on the non-standard port of @samp{3000}. -In these examples, the DNS name @samp{grub.example.com} is resolved -to @samp{192.0.2.1}. - -@example -(http,grub.example.com,3000) -(http,grub.example.com:3000) -(http,192.0.2.1,3000) -(http,192.0.2.1:3000) -@end example - -Referencing an @samp{http} server over IPv6 on the non-standard -port of @samp{3000} would look like this: - -@example -(http,2001:db8::1,3000) -@end example - If you boot GRUB from a CD-ROM, @samp{(cd)} is available. @xref{Making a GRUB bootable CD-ROM}, for details.
grub-core/net/http.c +2 -38
@@ -318,10 +318,6 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)int i;struct grub_net_buff *nb;grub_err_t err; -char *server_name; -char *port_string; -const char *port_string_end; -unsigned long port_number; nb =grub_netbuff_alloc (GRUB_NET_TCP_RESERVE_SIZE + sizeof ("GET ") - 1
@@ -400,42 +396,10 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)grub_netbuff_put (nb,2 );grub_memcpy (ptr,"\r\n" ,2 ); - port_string =grub_strrchr (file->device->net->server,',' ); -if (port_string ==NULL ) - { -/* If ",port" is not found in the http server string, look for ":port". */ - port_string =grub_strrchr (file->device->net->server,':' ); -/* For IPv6 addresses, the ":port" syntax is not supported and ",port" must be used. */ -if (port_string !=NULL &&grub_strchr (file->device->net->server,':' ) != port_string) - port_string =NULL ; - } -if (port_string !=NULL ) - { - port_number =grub_strtoul (port_string +1 , &port_string_end,10 ); -if (*(port_string +1 ) =='\0' || *port_string_end !='\0' ) -return grub_error (GRUB_ERR_BAD_NUMBER ,N_ ("non-numeric or invalid port number `%s'" ), port_string +1 ); -if (port_number ==0 || port_number >65535 ) -return grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("port number `%s' not in the range of 1 to 65535" ), port_string +1 ); - - server_name =grub_strdup (file->device->net->server); -if (server_name ==NULL ) -return grub_errno; - server_name[port_string - file->device->net->server] ='\0' ; - } -else - { - port_number =HTTP_PORT ; - server_name = file->device->net->server; - } - - data->sock =grub_net_tcp_open (server_name, - port_number, http_receive, + data->sock =grub_net_tcp_open (file->device->net->server, +HTTP_PORT , http_receive, http_err,NULL , file); - -if (server_name != file->device->net->server) -grub_free (server_name); -if (!data->sock) {grub_netbuff_free (nb);
Signature · 1 line
2.39.0
Re: [PATCH v4 01/19] Revert "net/http: Allow use of non-standard TCP/IP ports"
Quoted 1 line · Robbie Harwood
This reverts commit
ac8a37dda0eabdd80506bebe4fb9a5a9fd227935.
Please explain why we need to revert this patch.
Daniel
PATCH v4 2/19 net: read bracketed ipv6 addrs and port numbers
In reply to Robbie HarwoodFrom: Aaron Miller <[email protected]>
Allow specifying port numbers for http and tftp paths, and allow ipv6 addresses to be recognized with brackets around them, which is required to specify a port number.
--- grub-core/net/http.c | 25 ++++++++++--- grub-core/net/net.c | 87 +++++++++++++++++++++++++++++++++++++++++--- grub-core/net/tftp.c | 7 +++- include/grub/net.h | 1 + 4 files changed, 109 insertions(+), 11 deletions(-)
grub-core/net/http.c +20 -5
@@ -295,7 +295,9 @@ http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)), nb2 =grub_netbuff_alloc (data->chunk_rem);if (!nb2)return grub_errno; -grub_netbuff_put (nb2, data->chunk_rem); + err =grub_netbuff_put (nb2, data->chunk_rem); +if (err) +return grub_errno;grub_memcpy (nb2->data, nb->data, data->chunk_rem);if (file->device->net->packs.count >=20 ) {
@@ -318,12 +320,14 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)int i;struct grub_net_buff *nb;grub_err_t err; +char * server = file->device->net->server; +int port = file->device->net->port; nb = grub_netbuff_alloc (GRUB_NET_TCP_RESERVE_SIZE + sizeof ("GET ") - 1 + grub_strlen (data->filename) + sizeof (" HTTP/1.1\r\nHost: ") - 1 - + grub_strlen (file->device->net->server) + + grub_strlen (server) + sizeof (":XXXXXXXXXX") + sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n") - 1 + sizeof ("Range: bytes=XXXXXXXXXXXXXXXXXXXX"
@@ -362,7 +366,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial) sizeof (" HTTP/1.1\r\nHost: ") - 1); ptr = nb->tail; - err =grub_netbuff_put (nb,grub_strlen (file->device->net->server)); + err =grub_netbuff_put (nb,grub_strlen (server));if (err) {grub_netbuff_free (nb);
@@ -371,6 +375,15 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)grub_memcpy (ptr, file->device->net->server,grub_strlen (file->device->net->server)); +if (port) + { + ptr = nb->tail; +grub_snprintf ((char *) ptr, +sizeof (":XXXXXXXXXX" ), +":%d" , + port); + } + ptr = nb->tail; err =grub_netbuff_put (nb, sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n")
@@ -396,8 +409,10 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)grub_netbuff_put (nb,2 );grub_memcpy (ptr,"\r\n" ,2 ); - data->sock =grub_net_tcp_open (file->device->net->server, -HTTP_PORT , http_receive, +grub_dprintf ("http" ,"opening path %s on host %s TCP port %d\n" , + data->filename, server, port ? port :HTTP_PORT ); + data->sock =grub_net_tcp_open (server, + port ? port :HTTP_PORT , http_receive, http_err,NULL , file);if (!data->sock)
grub-core/net/net.c +82 -5
128 lines not on this page · raw message
grub-core/net/tftp.c +6 -1
@@ -295,6 +295,7 @@ tftp_open (struct grub_file *file, const char *filename)grub_err_t err;grub_uint8_t *nbd;grub_net_network_level_address_t addr; +int port = file->device->net->port; data =grub_zalloc (sizeof (*data));if (!data)
@@ -361,12 +362,16 @@ tftp_open (struct grub_file *file, const char *filename) err =grub_net_resolve_address (file->device->net->server, &addr);if (err) { +grub_dprintf ("tftp" ,"Address resolution failed: %d\n" , err); +grub_dprintf ("tftp" ,"file_size is %llu, block_size is %llu\n" , + (unsigned long long )data->file_size, + (unsigned long long )data->block_size);grub_free (data);return err; } data->sock =grub_net_udp_open (addr, -TFTP_SERVER_PORT , tftp_receive, + port ? port :TFTP_SERVER_PORT , tftp_receive, file);if (!data->sock) {
include/grub/net.h +1 -0
@@ -271,6 +271,7 @@ typedef struct grub_net {char *server;char *name; +int port;grub_net_app_level_t protocol;grub_net_packets_t packs;grub_off_t offset;
Signature · 1 line
2.39.0
Re: [PATCH v4 02/19] net: read bracketed ipv6 addrs and port numbers
Quoted 10 lines · Robbie Harwood
From: Aaron Miller <[email protected]>
Allow specifying port numbers for http and tftp paths, and allow ipv6 addresses to be recognized with brackets around them, which is required to specify a port number.
Signed-off-by Aaron Miller <[email protected]>Co-authored-by Peter Jones <[email protected]>Signed-off-by Peter Jones <[email protected]>Signed-off-by Robbie Harwood <[email protected]>
Quoted 5 lines
--- grub-core/net/http.c | 25 ++++++++++--- grub-core/net/net.c | 87 +++++++++++++++++++++++++++++++++++++++++--- grub-core/net/tftp.c | 7 +++- include/grub/net.h | 1 +
Please document this feature in the docs/grub.texi as it was in the patch you reverted earlier.
Quoted 7 lines
4 files changed, 109 insertions(+), 11 deletions(-)grub-core/net/http.c +0
-0
grub-core/net/http.c http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)), · line 300
Quoted 7 lines
grub-core/net/http.c +3
-1@@ -295,7 +295,9 @@ http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)), nb2 =grub_netbuff_alloc (data->chunk_rem);if (!nb2)return grub_errno; -grub_netbuff_put (nb2, data->chunk_rem); + err =grub_netbuff_put (nb2, data->chunk_rem); +if (err) +return grub_errno;
Hmmm... This change does not seem to belong to this patch...
grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 323
Quoted 8 lines
grub-core/net/http.c +1
-0grub_memcpy (nb2->data, nb->data, data->chunk_rem);if (file->device->net->packs.count >=20 ) {@@ -318,12 +320,14 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)int i;struct grub_net_buff *nb;grub_err_t err; +char * server = file->device->net->server;
s/* server/ *server/
Quoted 21 lines
grub-core/net/http.c +3
-2+int port = file->device->net->port; nb = grub_netbuff_alloc (GRUB_NET_TCP_RESERVE_SIZE + sizeof ("GET ") - 1 + grub_strlen (data->filename) + sizeof (" HTTP/1.1\r\nHost: ") - 1 - + grub_strlen (file->device->net->server) + + grub_strlen (server) + sizeof (":XXXXXXXXXX") + sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n") - 1 + sizeof ("Range: bytes=XXXXXXXXXXXXXXXXXXXX"@@ -362,7 +366,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial) sizeof (" HTTP/1.1\r\nHost: ") - 1); ptr = nb->tail; - err =grub_netbuff_put (nb,grub_strlen (file->device->net->server)); + err =grub_netbuff_put (nb,grub_strlen (server));if (err) {grub_netbuff_free (nb);
grub-core/net/http.c http_establish (struct grub_file *file, grub_off_t offset, int initial) · line 384
Quoted 10 lines
grub-core/net/http.c +7
-0@@ -371,6 +375,15 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)grub_memcpy (ptr, file->device->net->server,grub_strlen (file->device->net->server)); +if (port) + { + ptr = nb->tail; +grub_snprintf ((char *) ptr, +sizeof (":XXXXXXXXXX" ), +":%d" , + port);
Please put this in one line.
Quoted 23 lines
grub-core/net/http.c +6
-2+ } + ptr = nb->tail; err =grub_netbuff_put (nb, sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n")@@ -396,8 +409,10 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)grub_netbuff_put (nb,2 );grub_memcpy (ptr,"\r\n" ,2 ); - data->sock =grub_net_tcp_open (file->device->net->server, -HTTP_PORT , http_receive, +grub_dprintf ("http" ,"opening path %s on host %s TCP port %d\n" , + data->filename, server, port ? port :HTTP_PORT ); + data->sock =grub_net_tcp_open (server, + port ? port :HTTP_PORT , http_receive, http_err,NULL , file);if (!data->sock)grub-core/net/net.c +0
-0
grub-core/net/net.c parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest) · line 446
Quoted 4 lines
grub-core/net/net.c +1
-0@@ -443,6 +443,13 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)grub_uint16_t newip[8 ];const char *ptr = val;int word, quaddot =-1 ; +int bracketed =0 ;
bool?
Quoted 30 lines
grub-core/net/net.c +10
-0+ +if (ptr[0 ] =='[' ) + { + bracketed =1 ; + ptr++; + }if (ptr[0 ] ==':' && ptr[1 ] !=':' )return 0 ;@@ -481,6 +488,8 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)grub_memset (&newip[quaddot],0 , (7 - word) *sizeof (newip[0 ])); }grub_memcpy (ip, newip,16 ); +if (bracketed && *ptr ==']' ) + ptr++;if (rest) *rest = ptr;return 1 ;@@ -1319,8 +1328,10 @@ grub_net_open_real (const char *name) {grub_net_app_level_t proto;const char *protname, *server; +char *host;grub_size_t protnamelen;int try; +int port =0 ;if (grub_strncmp (name,"pxe:" ,sizeof ("pxe:" ) -1 ) ==0 ) {
grub-core/net/net.c grub_net_open_real (const char *name) · line 1372
Quoted 4 lines
grub-core/net/net.c +1
-0@@ -1358,6 +1369,72 @@ grub_net_open_real (const char *name)return NULL ; } +char *port_start;
Please define this at the beginning of the function.
grub-core/net/net.c grub_net_open_real (const char *name) · line 1383
Quoted 11 lines
grub-core/net/net.c +11
-0+/* ipv6 or port specified? */ +if ((port_start =grub_strchr (server,':' ))) + { +char *ipv6_begin; +if ((ipv6_begin =grub_strchr (server,'[' ))) + { +char *ipv6_end =grub_strchr (server,']' ); +if (!ipv6_end) + { +grub_error (GRUB_ERR_NET_BAD_ADDRESS , +N_ ("mismatched [ in address" ));
Please put this in one line.
grub-core/net/net.c grub_net_open_real (const char *name) · line 1393
Quoted 10 lines
grub-core/net/net.c +10
-0+return NULL ; + } +/* port number after bracketed ipv6 addr */ +if (ipv6_end[1 ] ==':' ) + { + port =grub_strtoul (ipv6_end +2 ,NULL ,10 ); +if (port >65535 ) + { +grub_error (GRUB_ERR_NET_BAD_ADDRESS , +N_ ("bad port number" ));
Ditto.
grub-core/net/net.c grub_net_open_real (const char *name) · line 1395
Quoted 2 lines
grub-core/net/net.c +2
-0+return NULL ; + }
This error check is not correct. Good example how it should be done is in the patch you reverted earlier.
Quoted 17 lines
grub-core/net/net.c +17
-0+ } + host =grub_strndup (ipv6_begin, (ipv6_end - ipv6_begin) +1 ); + } +else + { +if (grub_strchr (port_start +1 ,':' )) + { +int iplen =grub_strlen (server); +/* bracket bare ipv6 addrs */ + host =grub_malloc (iplen +3 ); +if (!host) + { +return NULL ; + } + host[0 ] ='[' ; +grub_memcpy (host +1 , server, iplen); + host[iplen +1 ] =']' ;
grub-core/net/net.c grub_net_open_real (const char *name) · line 1422
Quoted 10 lines
grub-core/net/net.c +10
-0+ host[iplen +2 ] ='\0' ; + } +else + { +/* hostname:port or ipv4:port */ + port =grub_strtol (port_start +1 ,NULL ,10 ); +if (port >65535 ) + { +grub_error (GRUB_ERR_NET_BAD_ADDRESS , +N_ ("bad port number" ));
Please put this in one line.
grub-core/net/net.c grub_net_open_real (const char *name) · line 1424
Quoted 2 lines
grub-core/net/net.c +2
-0+return NULL ; + }
Again, this error check is not correct...
grub-core/net/net.c grub_net_open_real (const char *name) · line 1436
Quoted 12 lines
grub-core/net/net.c +12
-0+ host =grub_strndup (server, port_start - server); + } + } + } +else + { + host =grub_strdup (server); + } +if (!host) + { +return NULL ; + }
Could you drop redundant curly braces here and a bit above?
Quoted 44 lines
grub-core/net/net.c +6
-5+for (try =0 ; try <2 ; try++) {FOR_NET_APP_LEVEL (proto)@@ -1367,14 +1444,13 @@ grub_net_open_real (const char *name) {grub_net_t ret =grub_zalloc (sizeof (*ret));if (!ret) -return NULL ; - ret->protocol = proto; - ret->server =grub_strdup (server); -if (!ret->server) { -grub_free (ret); +grub_free (host);return NULL ; } + ret->protocol = proto; + ret->port = port; + ret->server = host; ret->fs = &grub_net_fs;return ret; }@@ -1449,6 +1525,7 @@ grub_net_open_real (const char *name)grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("disk `%s' not found" ), name); +grub_free (host);return NULL ; }grub-core/net/tftp.c +1
-0@@ -295,6 +295,7 @@ tftp_open (struct grub_file *file, const char *filename)grub_err_t err;grub_uint8_t *nbd;grub_net_network_level_address_t addr; +int port = file->device->net->port; data =grub_zalloc (sizeof (*data));if (!data)
grub-core/net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 368
Quoted 7 lines
grub-core/net/tftp.c +4
-0@@ -361,12 +362,16 @@ tftp_open (struct grub_file *file, const char *filename) err =grub_net_resolve_address (file->device->net->server, &addr);if (err) { +grub_dprintf ("tftp" ,"Address resolution failed: %d\n" , err); +grub_dprintf ("tftp" ,"file_size is %llu, block_size is %llu\n" , + (unsigned long long )data->file_size, + (unsigned long long )data->block_size);
Why do not use proper PRIuGRUB_* format specifiers instead of casts here?
Quoted 15 lines
grub-core/net/tftp.c +1
-1grub_free (data);return err; } data->sock =grub_net_udp_open (addr, -TFTP_SERVER_PORT , tftp_receive, + port ? port :TFTP_SERVER_PORT , tftp_receive, file);if (!data->sock) {include/grub/net.h +0
-0
include/grub/net.h typedef struct grub_net · line 274
Quoted 4 lines
include/grub/net.h +1
-0@@ -271,6 +271,7 @@ typedef struct grub_net {char *server;char *name; +int port;
grub_uint16_t port;?
Daniel
PATCH v4 3/19 efinet + bootp: add net_bootp6 command supporting dhcpv6
In reply to Robbie HarwoodFrom: Peter Jones <[email protected]>
Implement new net_bootp6 command for IPv6 network auto configuration via the DHCPv6 protocol (RFC3315).
--- grub-core/net/bootp.c | 910 ++++++++++++++++++++++++++++- grub-core/net/drivers/efi/efinet.c | 52 +- grub-core/net/ip.c | 39 ++ grub-core/net/net.c | 72 +++ grub-core/net/tftp.c | 3 + include/grub/efi/api.h | 129 +++- include/grub/net.h | 79 +++ 7 files changed, 1267 insertions(+), 17 deletions(-)
grub-core/net/bootp.c +909 -1
@@ -24,6 +24,98 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> + +static int +dissect_url (const char *url,char **proto,char **host,char **path) +{ +const char *p, *ps; +grub_size_t l; + + *proto = *host = *path =NULL ; + ps = p = url; + +while ((p =grub_strchr (p,':' ))) + { +if (grub_strlen (p) <sizeof ("://" ) -1 ) +break ; +if (grub_memcmp (p,"://" ,sizeof ("://" ) -1 ) ==0 ) + { + l = p - ps; + *proto =grub_malloc (l +1 ); +if (!*proto) + { +grub_print_error (); +return 0 ; + } + +grub_memcpy (*proto, ps, l); + (*proto)[l] ='\0' ; + p +=sizeof ("://" ) -1 ; +break ; + } + ++p; + } + +if (!*proto) + { +grub_dprintf ("bootp" ,"url: %s is not valid, protocol not found\n" , url); +return 0 ; + } + + ps = p; + p =grub_strchr (p,'/' ); + +if (!p) + { +grub_dprintf ("bootp" ,"url: %s is not valid, host/path not found\n" , url); +grub_free (*proto); + *proto =NULL ; +return 0 ; + } + + l = p - ps; + +if (l >2 && ps[0 ] =='[' && ps[l -1 ] ==']' ) + { + *host =grub_malloc (l -1 ); +if (!*host) + { +grub_print_error (); +grub_free (*proto); + *proto =NULL ; +return 0 ; + } +grub_memcpy (*host, ps +1 , l -2 ); + (*host)[l -2 ] =0 ; + } +else + { + *host =grub_malloc (l +1 ); +if (!*host) + { +grub_print_error (); +grub_free (*proto); + *proto =NULL ; +return 0 ; + } +grub_memcpy (*host, ps, l); + (*host)[l] =0 ; + } + + *path =grub_strdup (p); +if (!*path) + { +grub_print_error (); +grub_free (*host); +grub_free (*proto); + *host =NULL ; + *proto =NULL ; +return 0 ; + } +return 1 ; +}struct grub_dhcp_discover_options {
@@ -608,6 +700,583 @@ out:return err; } +/* The default netbuff size for sending DHCPv6 packets which should be +large enough to hold the information */ +#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512 + +struct grub_dhcp6_options +{ +grub_uint8_t *client_duid; +grub_uint16_t client_duid_len; +grub_uint8_t *server_duid; +grub_uint16_t server_duid_len; +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_net_network_level_address_t *ia_addr; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime; +grub_net_network_level_address_t *dns_server_addrs; +grub_uint16_t num_dns_server; +char *boot_file_proto; +char *boot_file_server_ip; +char *boot_file_path; +}; + +typedef struct grub_dhcp6_options *grub_dhcp6_options_t ; + +struct grub_dhcp6_session +{ +struct grub_dhcp6_session *next; +struct grub_dhcp6_session **prev; +grub_uint32_t iaid; +grub_uint32_t transaction_id:24 ; +grub_uint64_t start_time; +struct grub_net_dhcp6_option_duid_ll duid; +struct grub_net_network_level_interface *iface; + +/* The associated dhcpv6 options */ +grub_dhcp6_options_t adv; +grub_dhcp6_options_t reply; +}; + +typedef struct grub_dhcp6_session *grub_dhcp6_session_t ; + +typedef void (*dhcp6_option_hook_fn ) (const struct grub_net_dhcp6_option *opt,void *data); + +static void +foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt,grub_size_t size, +dhcp6_option_hook_fn hook,void *hook_data); + +static void +parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt,void *data) +{ +grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data; + +grub_uint16_t code =grub_be_to_cpu16 (opt->code); +grub_uint16_t len =grub_be_to_cpu16 (opt->len); + +if (code ==GRUB_NET_DHCP6_OPTION_IAADDR ) + { +const struct grub_net_dhcp6_option_iaaddr *iaaddr; + iaaddr = (const struct grub_net_dhcp6_option_iaaddr *) opt->data; + +if (len <sizeof (*iaaddr)) + { +grub_dprintf ("bootp" ,"DHCPv6: code %u with insufficient length %u\n" , code, len); +return ; + } +if (!dhcp6->ia_addr) + { + dhcp6->ia_addr =grub_malloc (sizeof (*dhcp6->ia_addr)); + dhcp6->ia_addr->type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + dhcp6->ia_addr->ipv6[0 ] =grub_get_unaligned64 (iaaddr->addr); + dhcp6->ia_addr->ipv6[1 ] =grub_get_unaligned64 (iaaddr->addr +8 ); + dhcp6->preferred_lifetime =grub_be_to_cpu32 (iaaddr->preferred_lifetime); + dhcp6->valid_lifetime =grub_be_to_cpu32 (iaaddr->valid_lifetime); + } + } +} + +static void +parse_dhcp6_option (const struct grub_net_dhcp6_option *opt,void *data) +{ +grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data; +grub_uint16_t code =grub_be_to_cpu16 (opt->code); +grub_uint16_t len =grub_be_to_cpu16 (opt->len); + +switch (code) + { +case GRUB_NET_DHCP6_OPTION_CLIENTID : + +if (dhcp6->client_duid || !len) + { +grub_dprintf ("bootp" ,"Skipped DHCPv6 CLIENTID with length %u\n" , len); +break ; + } + dhcp6->client_duid =grub_malloc (len); +grub_memcpy (dhcp6->client_duid, opt->data, len); + dhcp6->client_duid_len = len; +break ; + +case GRUB_NET_DHCP6_OPTION_SERVERID : + +if (dhcp6->server_duid || !len) + { +grub_dprintf ("bootp" ,"Skipped DHCPv6 SERVERID with length %u\n" , len); +break ; + } + dhcp6->server_duid =grub_malloc (len); +grub_memcpy (dhcp6->server_duid, opt->data, len); + dhcp6->server_duid_len = len; +break ; + +case GRUB_NET_DHCP6_OPTION_IA_NA : + { +const struct grub_net_dhcp6_option_iana *ia_na; +grub_uint16_t data_len; + +if (dhcp6->iaid || len <sizeof (*ia_na)) + { +grub_dprintf ("bootp" ,"Skipped DHCPv6 IA_NA with length %u\n" , len); +break ; + } + ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data; + dhcp6->iaid =grub_be_to_cpu32 (ia_na->iaid); + dhcp6->t1 =grub_be_to_cpu32 (ia_na->t1); + dhcp6->t2 =grub_be_to_cpu32 (ia_na->t2); + + data_len = len -sizeof (*ia_na); +if (data_len) +foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6); + } +break ; + +case GRUB_NET_DHCP6_OPTION_DNS_SERVERS : + { +const grub_uint8_t *po; +grub_uint16_t ln; +grub_net_network_level_address_t *la; + +if (!len || len &0xf ) + { +grub_dprintf ("bootp" ,"Skip invalid length DHCPv6 DNS_SERVERS \n" ); +break ; + } + dhcp6->num_dns_server = ln = len >>4 ; + dhcp6->dns_server_addrs = la =grub_zalloc (ln *sizeof (*la)); + +for (po = opt->data; ln >0 ; po +=0x10 , la++, ln--) + { + la->type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + la->ipv6[0 ] =grub_get_unaligned64 (po); + la->ipv6[1 ] =grub_get_unaligned64 (po +8 ); + la->option =DNS_OPTION_PREFER_IPV6 ; + } + } +break ; + +case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL : +dissect_url ((const char *)opt->data, + &dhcp6->boot_file_proto, + &dhcp6->boot_file_server_ip, + &dhcp6->boot_file_path); +break ; + +default : +break ; + } +} + +static void +foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt,grub_size_t size,dhcp6_option_hook_fn hook,void *hook_data) +{ +while (size) + { +grub_uint16_t code, len; + +if (size <sizeof (*opt)) + { +grub_dprintf ("bootp" ,"DHCPv6: Options stopped with remaining size %" PRIxGRUB_SIZE"\n" , size); +break ; + } + size -=sizeof (*opt); + len =grub_be_to_cpu16 (opt->len); + code =grub_be_to_cpu16 (opt->code); +if (size < len) + { +grub_dprintf ("bootp" ,"DHCPv6: Options stopped at out of bound length %u for option %u\n" , len, code); +break ; + } +if (!len) + { +grub_dprintf ("bootp" ,"DHCPv6: Options stopped at zero length option %u\n" , code); +break ; + } +else + { +if (hook) +hook (opt, hook_data); + size -= len; + opt = (const struct grub_net_dhcp6_option *) ((grub_uint8_t *) opt + len +sizeof (*opt)); + } + } +} + +static grub_dhcp6_options_t +grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h, +grub_size_t size) +{ +grub_dhcp6_options_t options; + +if (size <sizeof (*v6h)) + { +grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("DHCPv6 packet size too small" )); +return NULL ; + } + + options =grub_zalloc (sizeof (*options)); +if (!options) +return NULL ; + +foreach_dhcp6_option ((const struct grub_net_dhcp6_option *) v6h->dhcp_options, + size -sizeof (*v6h), parse_dhcp6_option, options); + +return options; +} + +static void +grub_dhcp6_options_free (grub_dhcp6_options_t options) +{ +if (options->client_duid) +grub_free (options->client_duid); +if (options->server_duid) +grub_free (options->server_duid); +if (options->ia_addr) +grub_free (options->ia_addr); +if (options->dns_server_addrs) +grub_free (options->dns_server_addrs); +if (options->boot_file_proto) +grub_free (options->boot_file_proto); +if (options->boot_file_server_ip) +grub_free (options->boot_file_server_ip); +if (options->boot_file_path) +grub_free (options->boot_file_path); + +grub_free (options); +} + +static grub_dhcp6_session_t grub_dhcp6_sessions; +#define FOR_DHCP6_SESSIONS_SAFE (var, next) FOR_LIST_ELEMENTS_SAFE (var, next, grub_dhcp6_sessions) +#define FOR_DHCP6_SESSIONS (var) FOR_LIST_ELEMENTS (var, grub_dhcp6_sessions) + +static void +grub_net_configure_by_dhcp6_info (const char *name, +struct grub_net_card *card, +grub_dhcp6_options_t dhcp6, +int is_def, +int flags, +struct grub_net_network_level_interface **ret_inf) +{ +grub_net_network_level_netaddress_t netaddr; +struct grub_net_network_level_interface *inf; + +if (dhcp6->ia_addr) + { + inf =grub_net_add_addr (name, card, dhcp6->ia_addr, &card->default_address, flags); + + netaddr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + netaddr.ipv6.base[0 ] = dhcp6->ia_addr->ipv6[0 ]; + netaddr.ipv6.base[1 ] =0 ; + netaddr.ipv6.masksize =64 ; +grub_net_add_route (name, netaddr, inf); + +if (ret_inf) + *ret_inf = inf; + } + +if (dhcp6->dns_server_addrs) + { +grub_uint16_t i; + +for (i =0 ; i < dhcp6->num_dns_server; ++i) +grub_net_add_dns_server (dhcp6->dns_server_addrs + i); + } + +if (dhcp6->boot_file_path) +grub_env_set_net_property (name,"boot_file" , dhcp6->boot_file_path, +grub_strlen (dhcp6->boot_file_path)); + +if (is_def && dhcp6->boot_file_server_ip) + { + grub_net_default_server =grub_strdup (dhcp6->boot_file_server_ip); +grub_env_set ("net_default_interface" , name); +grub_env_export ("net_default_interface" ); + } +} + +static void +grub_dhcp6_session_add (struct grub_net_network_level_interface *iface, +grub_uint32_t iaid) +{ +grub_dhcp6_session_t se; +struct grub_datetime date; +grub_err_t err; +grub_int64_t t =0 ; + + se =grub_malloc (sizeof (*se)); + + err =grub_get_datetime (&date); +if (err || !grub_datetime2unixtime (&date, &t)) + { + grub_errno =GRUB_ERR_NONE ; + t =0 ; + } + + se->iface = iface; + se->iaid = iaid; + se->transaction_id = t; + se->start_time =grub_get_time_ms (); + se->duid.type =grub_cpu_to_be16_compile_time (3 ) ; + se->duid.hw_type =grub_cpu_to_be16_compile_time (1 ); +grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac,sizeof (se->duid.hwaddr)); + se->adv =NULL ; + se->reply =NULL ; +grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions),GRUB_AS_LIST (se)); +} + +static void +grub_dhcp6_session_remove (grub_dhcp6_session_t se) +{ +grub_list_remove (GRUB_AS_LIST (se)); +if (se->adv) +grub_dhcp6_options_free (se->adv); +if (se->reply) +grub_dhcp6_options_free (se->reply); +grub_free (se); +} + +static void +grub_dhcp6_session_remove_all (void ) +{ +grub_dhcp6_session_t se, next; + + FOR_DHCP6_SESSIONS_SAFE (se, next) + { +grub_dhcp6_session_remove (se); + } + grub_dhcp6_sessions =NULL ; +} + +static grub_err_t +grub_dhcp6_session_configure_network (grub_dhcp6_session_t se) +{ +char *name; + + name =grub_xasprintf ("%s:dhcp6" , se->iface->card->name); +if (!name) +return grub_errno; + +grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply,1 ,0 ,0 ); +grub_free (name); + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_dhcp6_session_send_request (grub_dhcp6_session_t se) +{ +struct grub_net_buff *nb; +struct grub_net_dhcp6_option *opt; +struct grub_net_dhcp6_packet *v6h; +struct grub_net_dhcp6_option_iana *ia_na; +struct grub_net_dhcp6_option_iaaddr *iaaddr; +struct udphdr *udph; +grub_net_network_level_address_t multicast; +grub_net_link_level_address_t ll_multicast; +grub_uint64_t elapsed; +struct grub_net_network_level_interface *inf = se->iface; +grub_dhcp6_options_t dhcp6 = se->adv; +grub_err_t err =GRUB_ERR_NONE ; + + multicast.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + multicast.ipv6[0 ] =grub_cpu_to_be64_compile_time (0xff02ULL <<48 ); + multicast.ipv6[1 ] =grub_cpu_to_be64_compile_time (0x10002ULL ); + + err =grub_net_link_layer_resolve (inf, &multicast, &ll_multicast); +if (err) +return err; + + nb =grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE ); +if (!nb) +return grub_errno; + + err =grub_netbuff_reserve (nb,GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE ); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + err =grub_netbuff_push (nb, dhcp6->client_duid_len +sizeof (*opt)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID ); + opt->len =grub_cpu_to_be16 (dhcp6->client_duid_len); +grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len); + + err =grub_netbuff_push (nb, dhcp6->server_duid_len +sizeof (*opt)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_SERVERID ); + opt->len =grub_cpu_to_be16 (dhcp6->server_duid_len); +grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len); + + err =grub_netbuff_push (nb,sizeof (*ia_na) +sizeof (*opt)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + +if (dhcp6->ia_addr) + { + err =grub_netbuff_push (nb,sizeof (*iaaddr) +sizeof (*opt)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + } + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA ); + opt->len =grub_cpu_to_be16 (sizeof (*ia_na)); +if (dhcp6->ia_addr) + opt->len +=grub_cpu_to_be16 (sizeof (*iaaddr) +sizeof (*opt)); + + ia_na = (struct grub_net_dhcp6_option_iana *) opt->data; + ia_na->iaid =grub_cpu_to_be32 (dhcp6->iaid); + + ia_na->t1 =grub_cpu_to_be32 (dhcp6->t1); + ia_na->t2 =grub_cpu_to_be32 (dhcp6->t2); + +if (dhcp6->ia_addr) + { + opt = (struct grub_net_dhcp6_option *) ia_na->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR ); + opt->len =grub_cpu_to_be16 (sizeof (*iaaddr)); + iaaddr = (struct grub_net_dhcp6_option_iaaddr *) opt->data; +grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0 ]); +grub_set_unaligned64 (iaaddr->addr +8 , dhcp6->ia_addr->ipv6[1 ]); + + iaaddr->preferred_lifetime =grub_cpu_to_be32 (dhcp6->preferred_lifetime); + iaaddr->valid_lifetime =grub_cpu_to_be32 (dhcp6->valid_lifetime); + } + + err =grub_netbuff_push (nb,sizeof (*opt) +2 *sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO ); + opt->len =grub_cpu_to_be16_compile_time (2 *sizeof (grub_uint16_t)); +grub_set_unaligned16 (opt->data,grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL )); +grub_set_unaligned16 (opt->data +2 ,grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS )); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME ); + opt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t)); + +/* the time is expressed in hundredths of a second */ + elapsed =grub_divmod64 (grub_get_time_ms () - se->start_time,10 ,0 ); + +if (elapsed >0xffff ) + elapsed =0xffff ; + +grub_set_unaligned16 (opt->data,grub_cpu_to_be16 ((grub_uint16_t ) elapsed)); + + err =grub_netbuff_push (nb,sizeof (*v6h)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + v6h = (struct grub_net_dhcp6_packet *) nb->data; + v6h->message_type =GRUB_NET_DHCP6_REQUEST ; + v6h->transaction_id = se->transaction_id; + + err =grub_netbuff_push (nb,sizeof (*udph)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT ); + udph->dst =grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT ); + udph->chksum =0 ; + udph->len =grub_cpu_to_be16 (nb->tail - nb->data); + + udph->chksum =grub_net_ip_transport_checksum (nb,GRUB_NET_IP_UDP , + &inf->address, + &multicast); + err =grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb, +GRUB_NET_IP_UDP ); + +grub_netbuff_free (nb); + +return err; +} + +struct grub_net_network_level_interface * +grub_net_configure_by_dhcpv6_reply (const char *name, +struct grub_net_card *card, +grub_net_interface_flags_t flags, +const struct grub_net_dhcp6_packet *v6h, +grub_size_t size, +int is_def, +char **device,char **path) +{ +struct grub_net_network_level_interface *inf; +grub_dhcp6_options_t dhcp6; +int mask =-1 ; + + dhcp6 =grub_dhcp6_options_get (v6h, size); +if (!dhcp6) + { +grub_print_error (); +return NULL ; + } + +grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags, &inf); + +if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip) + { + *device =grub_xasprintf ("%s,%s" , dhcp6->boot_file_proto, dhcp6->boot_file_server_ip); +grub_print_error (); + } +if (path && dhcp6->boot_file_path) + { + *path =grub_strdup (dhcp6->boot_file_path); +grub_print_error (); +if (*path) + { +char *slash; + slash =grub_strrchr (*path,'/' ); +if (slash) + *slash =0 ; +else + **path =0 ; + } + } + +grub_dhcp6_options_free (dhcp6); + +if (inf) +grub_net_add_ipv6_local (inf, mask); + +return inf; +} +/* * This is called directly from net/ip.c:handle_dgram(), because those * BOOTP/DHCP packets are a bit special due to their improper
@@ -676,6 +1345,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb, } } +grub_err_t +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card __attribute__ ((unused))) +{ +const struct grub_net_dhcp6_packet *v6h; +grub_dhcp6_session_t se; +grub_size_t size; +grub_dhcp6_options_t options; + + v6h = (const struct grub_net_dhcp6_packet *) nb->data; + size = nb->tail - nb->data; + + options =grub_dhcp6_options_get (v6h, size); +if (!options) +return grub_errno; + +if (!options->client_duid || !options->server_duid || !options->ia_addr) + { +grub_dhcp6_options_free (options); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Bad DHCPv6 Packet" ); + } + +FOR_DHCP6_SESSIONS (se) + { +if (se->transaction_id == v6h->transaction_id && +grub_memcmp (options->client_duid, &se->duid,sizeof (se->duid)) ==0 && + se->iaid == options->iaid) +break ; + } + +if (!se) + { +grub_dprintf ("bootp" ,"DHCPv6 session not found\n" ); +grub_dhcp6_options_free (options); +return GRUB_ERR_NONE ; + } + +if (v6h->message_type ==GRUB_NET_DHCP6_ADVERTISE ) + { +if (se->adv) + { +grub_dprintf ("bootp" ,"Skipped DHCPv6 Advertised .. \n" ); +grub_dhcp6_options_free (options); +return GRUB_ERR_NONE ; + } + + se->adv = options; +return grub_dhcp6_session_send_request (se); + } +else if (v6h->message_type ==GRUB_NET_DHCP6_REPLY ) + { +if (!se->adv) + { +grub_dprintf ("bootp" ,"Skipped DHCPv6 Reply .. \n" ); +grub_dhcp6_options_free (options); +return GRUB_ERR_NONE ; + } + + se->reply = options; +grub_dhcp6_session_configure_network (se); +grub_dhcp6_session_remove (se); +return GRUB_ERR_NONE ; + } +else + { +grub_dhcp6_options_free (options); + } + +return GRUB_ERR_NONE ; +} +static grub_err_t grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),int argc,char **args)
@@ -901,7 +1641,171 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),return err; } -static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp; +static grub_err_t +grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)), +int argc,char **args) +{ +struct grub_net_card *card; +grub_uint32_t iaid =0 ; +int interval; +grub_err_t err =GRUB_ERR_NONE ; +grub_dhcp6_session_t se; + +FOR_NET_CARDS (card) + { +struct grub_net_network_level_interface *iface; + +if (argc >0 &&grub_strcmp (card->name, args[0 ]) !=0 ) +continue ; + + iface =grub_net_ipv6_get_link_local (card, &card->default_address); +if (!iface) + { +grub_dhcp6_session_remove_all (); +return grub_errno; + } + +grub_dhcp6_session_add (iface, iaid++); + } + +for (interval =200 ; interval <10000 ; interval *=2 ) + { +int done =1 ; + +FOR_DHCP6_SESSIONS (se) + { +struct grub_net_buff *nb; +struct grub_net_dhcp6_option *opt; +struct grub_net_dhcp6_packet *v6h; +struct grub_net_dhcp6_option_duid_ll *duid; +struct grub_net_dhcp6_option_iana *ia_na; +grub_net_network_level_address_t multicast; +grub_net_link_level_address_t ll_multicast; +struct udphdr *udph; + + multicast.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + multicast.ipv6[0 ] =grub_cpu_to_be64_compile_time (0xff02ULL <<48 ); + multicast.ipv6[1 ] =grub_cpu_to_be64_compile_time (0x10002ULL ); + + err =grub_net_link_layer_resolve (se->iface, + &multicast, &ll_multicast); +if (err) + { +grub_dhcp6_session_remove_all (); +return err; + } + + nb =grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE ); +if (!nb) + { +grub_dhcp6_session_remove_all (); +return grub_errno; + } + + err =grub_netbuff_reserve (nb,GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE ); +if (err) + { +grub_dhcp6_session_remove_all (); +grub_netbuff_free (nb); +return err; + } + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (grub_uint16_t)); +if (err) + { +grub_dhcp6_session_remove_all (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME ); + opt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t)); +grub_set_unaligned16 (opt->data,0 ); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*duid)); +if (err) + { +grub_dhcp6_session_remove_all (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID ); + opt->len =grub_cpu_to_be16 (sizeof (*duid)); + + duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data; +grub_memcpy (duid, &se->duid,sizeof (*duid)); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*ia_na)); +if (err) + { +grub_dhcp6_session_remove_all (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_net_dhcp6_option *) nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA ); + opt->len =grub_cpu_to_be16 (sizeof (*ia_na)); + ia_na = (struct grub_net_dhcp6_option_iana *) opt->data; + ia_na->iaid =grub_cpu_to_be32 (se->iaid); + ia_na->t1 =0 ; + ia_na->t2 =0 ; + + err =grub_netbuff_push (nb,sizeof (*v6h)); +if (err) + { +grub_dhcp6_session_remove_all (); +grub_netbuff_free (nb); +return err; + } + + v6h = (struct grub_net_dhcp6_packet *) nb->data; + v6h->message_type =GRUB_NET_DHCP6_SOLICIT ; + v6h->transaction_id = se->transaction_id; + +grub_netbuff_push (nb,sizeof (*udph)); + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT ); + udph->dst =grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT ); + udph->chksum =0 ; + udph->len =grub_cpu_to_be16 (nb->tail - nb->data); + + udph->chksum =grub_net_ip_transport_checksum (nb,GRUB_NET_IP_UDP , + &se->iface->address, &multicast); + + err =grub_net_send_ip_packet (se->iface, &multicast, + &ll_multicast, nb,GRUB_NET_IP_UDP ); + done =0 ; +grub_netbuff_free (nb); + +if (err) + { +grub_dhcp6_session_remove_all (); +return err; + } + } +if (!done) +grub_net_poll_cards (interval,0 ); + } + +FOR_DHCP6_SESSIONS (se) + { +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("couldn't autoconfigure %s" ), + se->iface->card->name); + } + +grub_dhcp6_session_remove_all (); + +return err; +} + +static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp, cmd_bootp6;void grub_bootp_init (void )
@@ -915,11 +1819,15 @@ grub_bootp_init (void) cmd_getdhcp =grub_register_command ("net_get_dhcp_option" , grub_cmd_dhcpopt,N_ ("VAR INTERFACE NUMBER DESCRIPTION" ),N_ ("retrieve DHCP option and save it into VAR. If VAR is - then print the value." )); + cmd_bootp6 =grub_register_command ("net_bootp6" , grub_cmd_bootp6, +N_ ("[CARD]" ), +N_ ("perform a DHCPv6 autoconfiguration" )); }void grub_bootp_fini (void ) { +grub_unregister_command (cmd_bootp6);grub_unregister_command (cmd_getdhcp);grub_unregister_command (cmd_bootp);grub_unregister_command (cmd_dhcp);
grub-core/net/drivers/efi/efinet.c +42 -10
99 lines not on this page · raw message
grub-core/net/ip.c +39 -0
@@ -240,6 +240,45 @@ handle_dgram (struct grub_net_buff *nb, {struct udphdr *udph; udph = (struct udphdr *) nb->data; + +if (proto ==GRUB_NET_IP_UDP && udph->dst ==grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT )) + { +if (udph->chksum) + { +grub_uint16_t chk, expected; + chk = udph->chksum; + udph->chksum =0 ; + expected =grub_net_ip_transport_checksum (nb, +GRUB_NET_IP_UDP , + source, + dest); +if (expected != chk) + { +grub_dprintf ("net" ,"Invalid UDP checksum. " +"Expected %x, got %x\n" , +grub_be_to_cpu16 (expected), +grub_be_to_cpu16 (chk)); +grub_netbuff_free (nb); +return GRUB_ERR_NONE ; + } + udph->chksum = chk; + } + + err =grub_netbuff_pull (nb,sizeof (*udph)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + err =grub_net_process_dhcp6 (nb, card); +if (err) +grub_print_error (); + +grub_netbuff_free (nb); +return GRUB_ERR_NONE ; + } +if (proto ==GRUB_NET_IP_UDP &&grub_be_to_cpu16 (udph->dst) ==68 ) {const struct grub_net_bootp_packet *bootp;
grub-core/net/net.c +72 -0
78 lines not on this page · raw message
grub-core/net/tftp.c +3 -0
@@ -359,6 +359,7 @@ tftp_open (struct grub_file *file, const char *filename) file->not_easily_seekable =1 ; file->data = data; +grub_dprintf ("tftp" ,"resolving address for %s\n" , file->device->net->server); err =grub_net_resolve_address (file->device->net->server, &addr);if (err) {
@@ -370,11 +371,13 @@ tftp_open (struct grub_file *file, const char *filename)return err; } +grub_dprintf ("tftp" ,"opening connection\n" ); data->sock =grub_net_udp_open (addr, port ? port :TFTP_SERVER_PORT , tftp_receive, file);if (!data->sock) { +grub_dprintf ("tftp" ,"connection failed\n" );grub_free (data);return grub_errno; }
include/grub/efi/api.h +123 -6
149 lines not on this page · raw message
include/grub/net.h +79 -0
97 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH v4 4/19 efinet: add structures for PXE messages
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
When grub2 image is booted from UEFI IPv6 PXE, the DHCPv6 Reply packet is cached in firmware buffer which can be obtained by PXE Base Code protocol. The network interface can be setup through the parameters in that obtained packet.
Augment existing structures to represent this, and make them agnostic between ipv4 and ipv6.
--- grub-core/net/drivers/efi/efinet.c | 2 + include/grub/efi/api.h | 71 ++++++++++++++++++------------ 2 files changed, 46 insertions(+), 27 deletions(-)
grub-core/net/drivers/efi/efinet.c +2 -0
@@ -426,6 +426,8 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,grub_print_error ();if (device && path)grub_dprintf ("efinet" ,"device: `%s' path: `%s'\n" , *device, *path); +if (grub_errno) +grub_print_error (); }else {
include/grub/efi/api.h +44 -27
90 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH v4 5/19 grub.texi: Add net_bootp6 doumentation
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
Update grub documentation for net_bootp6 command.
--- docs/grub.texi | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
docs/grub.texi +17 -0
@@ -5720,6 +5720,7 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). * net_add_dns:: Add a DNS server * net_add_route:: Add routing entry * net_bootp:: Perform a bootp/DHCP autoconfiguration +* net_bootp6:: Perform a DHCPv6 autoconfiguration * net_del_addr:: Remove IP address from interface * net_del_dns:: Remove a DNS server * net_del_route:: Remove a route entry
@@ -5845,6 +5846,22 @@ Sets environment variable @samp{net_}@var{<card>}@samp{_boot_file} @end deffn +@node net_bootp6 +@subsection net_bootp6 + +@deffn Command net_bootp6 [@var{card}] +Perform configuration of @var{card} using DHCPv6 protocol. If no card name is +specified, try to configure all existing cards. If configuration was +successful, interface with name @var{card}@samp{:dhcp6} and configured address +is added to @var{card}. + +@table @samp +@item 1 (Domain Name Server) +Adds all servers from option value to the list of servers used during name +resolution. +@end table + +@end deffn @node net_get_dhcp_option @subsection net_get_dhcp_option
Signature · 1 line
2.39.0
PATCH v4 6/19 bootp: Process DHCPACK packet during HTTP Boot
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
The vendor class identifier with the string "HTTPClient" is used to denote the packet as responding to HTTP boot request. In DHCP4 config, the filename for HTTP boot is the URL of the boot file, while for PXE boot it is the path to the boot file. As a consequence, the next-server becomes obselete because the HTTP URL already contains the server address for the boot file. For DHCP6 config, there's no difference definition in existing config as dhcp6.bootfile-url can be used to specify URL for both HTTP and PXE boot file.
Add processing for "HTTPClient" vendor class identifier in DHCPACK packet by treating it as HTTP format, not as the PXE format.
--- grub-core/net/bootp.c | 55 +++++++++++++++++++++++++++++++++++++++++++ include/grub/net.h | 1 + 2 files changed, 56 insertions(+)
grub-core/net/bootp.c +55 -0
@@ -20,6 +20,7 @@#include <grub/env.h> #include <grub/i18n.h> #include <grub/command.h> +#include <grub/net.h> #include <grub/net/ip.h> #include <grub/net/netbuff.h> #include <grub/net/udp.h>
@@ -506,6 +507,60 @@ grub_net_configure_by_dhcp_ack (const char *name,if (opt && opt_len)grub_env_set_net_property (name,"rootpath" , (const char *) opt, opt_len); + opt =find_dhcp_option (bp, size,GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER , &opt_len); +if (opt && opt_len) + { +grub_env_set_net_property (name,"vendor_class_identifier" , (const char *) opt, opt_len); +if (opt &&grub_strcmp ((const char *) opt,"HTTPClient" ) ==0 ) + { +char *proto, *ip, *pa; + +if (!dissect_url (bp->boot_file, &proto, &ip, &pa)) +return inter; + +grub_env_set_net_property (name,"boot_file" , pa,grub_strlen (pa)); +if (is_def) + { + grub_net_default_server =grub_strdup (ip); +grub_env_set ("net_default_interface" , name); +grub_env_export ("net_default_interface" ); + } +if (device && !*device) + { + *device =grub_xasprintf ("%s,%s" , proto, ip); +grub_print_error (); + } +if (path) + { + *path =grub_strdup (pa); +grub_print_error (); +if (*path) + { +char *slash; + slash =grub_strrchr (*path,'/' ); +if (slash) + *slash =0 ; +else + **path =0 ; + } + } +grub_net_add_ipv4_local (inter, mask); + inter->dhcp_ack =grub_malloc (size); +if (inter->dhcp_ack) + { +grub_memcpy (inter->dhcp_ack, bp, size); + inter->dhcp_acklen = size; + } +else + grub_errno =GRUB_ERR_NONE ; + +grub_free (proto); +grub_free (ip); +grub_free (pa); +return inter; + } + } + opt =find_dhcp_option (bp, size,GRUB_NET_BOOTP_EXTENSIONS_PATH , &opt_len);if (opt && opt_len)grub_env_set_net_property (name,"extensionspath" , (const char *) opt, opt_len);
include/grub/net.h +1 -0
@@ -530,6 +530,7 @@ enumGRUB_NET_DHCP_MESSAGE_TYPE =53 ,GRUB_NET_DHCP_SERVER_IDENTIFIER =54 ,GRUB_NET_DHCP_PARAMETER_REQUEST_LIST =55 , +GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER =60 ,GRUB_NET_BOOTP_CLIENT_ID =61 ,GRUB_NET_DHCP_TFTP_SERVER_NAME =66 , GRUB_NET_DHCP_BOOTFILE_NAME = 67,
Signature · 1 line
2.39.0
PATCH v4 7/19 efinet Configure network from UEFI device path
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
The PXE Base Code protocol used to obtain cached PXE DHCPACK packet is no longer provided for HTTP Boot. Instead, we have to get the HTTP boot information from the device path nodes defined in following UEFI Specification sections.
9.3.5.12 IPv4 Device Path
9.3.5.13 IPv6 Device Path
9.3.5.23 Uniform Resource Identifiers (URI) Device PathThis patch basically does:
include/grub/efi/api.h:
Add new structure for Uniform Resource Identifiers (URI) Device Path
grub-core/net/drivers/efi/efinet.c:
Check if PXE Base Code is available. If not, try to obtain the netboot information from the device path where the image booted from. The DHCPACK packet is recoverd from the information in device patch and fed into the same DHCP packet processing functions to ensure the network interface is set up the same way it used to be.
--- grub-core/net/drivers/efi/efinet.c | 278 +++++++++++++++++++++++++++-- include/grub/efi/api.h | 11 ++ 2 files changed, 274 insertions(+), 15 deletions(-)
grub-core/net/drivers/efi/efinet.c +263 -15
@@ -26,6 +26,7 @@#include <grub/i18n.h> #include <grub/lib/hexdump.h> #include <grub/types.h> +#include <grub/net/netbuff.h> GRUB_MOD_LICENSE ("GPLv3+" );
@@ -344,6 +345,225 @@ grub_efinet_findcards (void)grub_free (handles); } +static struct grub_net_buff * +grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp,int *use_ipv6) +{ +grub_efi_uint16_t uri_len; +grub_efi_device_path_t *ldp, *ddp; +grub_efi_uri_device_path_t *uri_dp; +struct grub_net_buff *nb; +grub_err_t err; + + ddp =grub_efi_duplicate_device_path (dp); +if (!ddp) +return NULL ; + + ldp =grub_efi_find_last_device_path (ddp); +if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) !=GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE + ||GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_URI_DEVICE_PATH_SUBTYPE ) + { +grub_free (ddp); +return NULL ; + } + + uri_len =GRUB_EFI_DEVICE_PATH_LENGTH (ldp) >4 ?GRUB_EFI_DEVICE_PATH_LENGTH (ldp) -4 :0 ; + +if (!uri_len) + { +grub_free (ddp); +return NULL ; + } + + uri_dp = (grub_efi_uri_device_path_t *) ldp; + + ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; + ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; + ldp->length =sizeof (*ldp); + + ldp =grub_efi_find_last_device_path (ddp); +if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) !=GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE + || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE + &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE )) + { +grub_free (ddp); +return NULL ; + } + + nb =grub_netbuff_alloc (512 ); +if (!nb) + { +grub_free (ddp); +return NULL ; + } + +if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE ) + { +grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp; +struct grub_net_bootp_packet *bp; +grub_uint8_t *ptr; + + bp = (struct grub_net_bootp_packet *) nb->tail; + err =grub_netbuff_put (nb,sizeof (*bp) +4 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + +if (sizeof (bp->boot_file) < uri_len) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } +grub_memcpy (bp->boot_file, uri_dp->uri, uri_len); +grub_memcpy (&bp->your_ip, ipv4->local_ip_address,sizeof (bp->your_ip)); +grub_memcpy (&bp->server_ip, ipv4->remote_ip_address,sizeof (bp->server_ip)); + + bp->vendor[0 ] =GRUB_NET_BOOTP_RFC1048_MAGIC_0 ; + bp->vendor[1 ] =GRUB_NET_BOOTP_RFC1048_MAGIC_1 ; + bp->vendor[2 ] =GRUB_NET_BOOTP_RFC1048_MAGIC_2 ; + bp->vendor[3 ] =GRUB_NET_BOOTP_RFC1048_MAGIC_3 ; + + ptr = nb->tail; + err =grub_netbuff_put (nb,sizeof (ipv4->subnet_mask) +2 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + *ptr++ =GRUB_NET_BOOTP_NETMASK ; + *ptr++ =sizeof (ipv4->subnet_mask); +grub_memcpy (ptr, ipv4->subnet_mask,sizeof (ipv4->subnet_mask)); + + ptr = nb->tail; + err =grub_netbuff_put (nb,sizeof (ipv4->gateway_ip_address) +2 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + *ptr++ =GRUB_NET_BOOTP_ROUTER ; + *ptr++ =sizeof (ipv4->gateway_ip_address); +grub_memcpy (ptr, ipv4->gateway_ip_address,sizeof (ipv4->gateway_ip_address)); + + ptr = nb->tail; + err =grub_netbuff_put (nb,sizeof ("HTTPClient" ) +1 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + *ptr++ =GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER ; + *ptr++ =sizeof ("HTTPClient" ) -1 ; +grub_memcpy (ptr,"HTTPClient" ,sizeof ("HTTPClient" ) -1 ); + + ptr = nb->tail; + err =grub_netbuff_put (nb,1 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + *ptr =GRUB_NET_BOOTP_END ; + *use_ipv6 =0 ; + + ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; + ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; + ldp->length =sizeof (*ldp); + ldp =grub_efi_find_last_device_path (ddp); + +if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE ) + { +grub_efi_mac_address_device_path_t *mac = (grub_efi_mac_address_device_path_t *) ldp; + bp->hw_type = mac->if_type; + bp->hw_len =sizeof (bp->mac_addr); +grub_memcpy (bp->mac_addr, mac->mac_address, bp->hw_len); + } + } +else + { +grub_efi_ipv6_device_path_t *ipv6 = (grub_efi_ipv6_device_path_t *) ldp; + +struct grub_net_dhcp6_packet *d6p; +struct grub_net_dhcp6_option *opt; +struct grub_net_dhcp6_option_iana *iana; +struct grub_net_dhcp6_option_iaaddr *iaaddr; + + d6p = (struct grub_net_dhcp6_packet *)nb->tail; + err =grub_netbuff_put (nb,sizeof (*d6p)); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + d6p->message_type =GRUB_NET_DHCP6_REPLY ; + + opt = (struct grub_net_dhcp6_option *) nb->tail; + err =grub_netbuff_put (nb,sizeof (*opt)); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA ); + opt->len =grub_cpu_to_be16_compile_time (sizeof (*iana) +sizeof (*opt) +sizeof (*iaaddr)); + + err =grub_netbuff_put (nb,sizeof (*iana)); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + + opt = (struct grub_net_dhcp6_option *) nb->tail; + err =grub_netbuff_put (nb,sizeof (*opt)); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR ); + opt->len =grub_cpu_to_be16_compile_time (sizeof (*iaaddr)); + + iaaddr = (struct grub_net_dhcp6_option_iaaddr *) nb->tail; + err =grub_netbuff_put (nb,sizeof (*iaaddr)); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } +grub_memcpy (iaaddr->addr, ipv6->local_ip_address,sizeof (ipv6->local_ip_address)); + + opt = (struct grub_net_dhcp6_option *)nb->tail; + err =grub_netbuff_put (nb,sizeof (*opt) + uri_len); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL ); + opt->len =grub_cpu_to_be16 (uri_len); +grub_memcpy (opt->data, uri_dp->uri, uri_len); + + *use_ipv6 =1 ; + } + +grub_free (ddp); +return nb; +} +static void grub_efi_net_config_real (grub_efi_handle_t hnd,char **device,char **path)
@@ -363,7 +583,11 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device, {grub_efi_device_path_t *cdp;struct grub_efi_pxe *pxe; -struct grub_efi_pxe_mode *pxe_mode; +struct grub_efi_pxe_mode *pxe_mode =NULL ; +grub_uint8_t *packet_buf; +grub_size_t packet_bufsz ; +int ipv6; +struct grub_net_buff *nb =NULL ;if (card->driver != &efidriver)continue ;
@@ -387,11 +611,21 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,*/ if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) !=GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE - &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE )) + &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE + &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_URI_DEVICE_PATH_SUBTYPE ))continue ; dup_dp =grub_efi_duplicate_device_path (dp);if (!dup_dp)continue ; + +if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==GRUB_EFI_URI_DEVICE_PATH_SUBTYPE ) + { + dup_ldp =grub_efi_find_last_device_path (dup_dp); + dup_ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; + dup_ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; + dup_ldp->length =sizeof (*dup_ldp); + } + dup_ldp =grub_efi_find_last_device_path (dup_dp); dup_ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; dup_ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ;
@@ -404,23 +638,37 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device, pxe =grub_efi_open_protocol (hnd, &pxe_io_guid,GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL ); -if (! pxe) -continue ; +if (!pxe) + { + nb =grub_efinet_create_dhcp_ack_from_device_path (dp, &ipv6); +if (!nb) + { +grub_print_error (); +continue ; + } + packet_buf = nb->head; + packet_bufsz = nb->tail - nb->head; + } +else + { + pxe_mode = pxe->mode; + packet_buf = (grub_uint8_t *) &pxe_mode->dhcp_ack; + packet_bufsz =sizeof (pxe_mode->dhcp_ack); + ipv6 = pxe_mode->using_ipv6; + } - pxe_mode = pxe->mode; -if (pxe_mode->using_ipv6) +if (ipv6) {grub_dprintf ("efinet" ,"using ipv6 and dhcpv6\n" ); -grub_dprintf ("efinet" ,"dhcp_ack_received: %s%s\n" , - pxe_mode->dhcp_ack_received ?"yes" :"no" , - pxe_mode->dhcp_ack_received ?"" :" cannot continue" ); -if (!pxe_mode->dhcp_ack_received) -continue ; +if (pxe_mode) +grub_dprintf ("efinet" ,"dhcp_ack_received: %s%s\n" , + pxe_mode->dhcp_ack_received ?"yes" :"no" , + pxe_mode->dhcp_ack_received ?"" :" cannot continue" ); inter =grub_net_configure_by_dhcpv6_reply (card->name, card,0 , (struct grub_net_dhcp6_packet *) - &pxe_mode->dhcp_ack, -sizeof (pxe_mode->dhcp_ack), + packet_buf, + packet_bufsz,1 , device, path);if (grub_errno)grub_print_error ();
@@ -434,8 +682,8 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,grub_dprintf ("efinet" ,"using ipv4 and dhcp\n" ); inter =grub_net_configure_by_dhcp_ack (card->name, card,0 , (struct grub_net_bootp_packet *) - &pxe_mode->dhcp_ack, -sizeof (pxe_mode->dhcp_ack), + packet_buf, + packet_bufsz,1 , device, path);grub_dprintf ("efinet" ,"device: `%s' path: `%s'\n" , *device, *path); }
include/grub/efi/api.h +11 -0
@@ -895,6 +895,8 @@ struct grub_efi_ipv4_device_pathgrub_efi_uint16_t remote_port;grub_efi_uint16_t protocol;grub_efi_uint8_t static_ip_address; +grub_efi_ipv4_address_t gateway_ip_address; +grub_efi_ipv4_address_t subnet_mask; }GRUB_PACKED ;typedef struct grub_efi_ipv4_device_path grub_efi_ipv4_device_path_t ;
@@ -958,6 +960,15 @@ struct grub_efi_vlan_device_path }GRUB_PACKED ;typedef struct grub_efi_vlan_device_path grub_efi_vlan_device_path_t ; +#define GRUB_EFI_URI_DEVICE_PATH_SUBTYPE 24 + +struct grub_efi_uri_device_path +{ +grub_efi_device_path_t header; +grub_efi_uint8_t uri[0 ]; +}GRUB_PACKED ; +typedef struct grub_efi_uri_device_path grub_efi_uri_device_path_t ; +#define GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE 10/* Media Device Path. */
Signature · 1 line
2.39.0
PATCH v4 8/19 efinet: set DNS server from UEFI protocol
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
In the URI device path node, any name rather than address can be used for looking up the resources so that DNS service become needed to get answer of the name's address. Unfortunately, DNS is not defined in any of the device path nodes so that we use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL to obtain it.
These two protcols are defined the sections of UEFI specification.
27.5 EFI IPv4 Configuration II Protocol
27.7 EFI IPv6 Configuration Protocol
include/grub/efi/api.h:
Add new structure and protocol UUID of EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL.
grub-core/net/drivers/efi/efinet.c:
Use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL to obtain the list of DNS server address for IPv4 and IPv6 respectively. The address of DNS servers is structured into DHCPACK packet and feed into the same DHCP packet processing functions to ensure the network interface is setting up the same way it used to be.
--- grub-core/net/drivers/efi/efinet.c | 160 +++++++++++++++++++++++++++++ include/grub/efi/api.h | 77 ++++++++++++++ 2 files changed, 237 insertions(+)
grub-core/net/drivers/efi/efinet.c +160 -0
@@ -33,6 +33,8 @@ GRUB_MOD_LICENSE ("GPLv3+");/* GUID. */ static grub_efi_guid_t net_io_guid =GRUB_EFI_SIMPLE_NETWORK_GUID ;static grub_efi_guid_t pxe_io_guid =GRUB_EFI_PXE_GUID ; +static grub_efi_guid_t ip4_config_guid =GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID ; +static grub_efi_guid_t ip6_config_guid =GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID ; static grub_err_t send_card_buffer (struct grub_net_card *dev,
@@ -345,6 +347,122 @@ grub_efinet_findcards (void)grub_free (handles); } +static grub_efi_handle_t +grub_efi_locate_device_path (grub_efi_guid_t *protocol,grub_efi_device_path_t *device_path, +grub_efi_device_path_t **r_device_path) +{ +grub_efi_handle_t handle; +grub_efi_status_t status; + + status =efi_call_3 (grub_efi_system_table->boot_services->locate_device_path, + protocol, &device_path, &handle); + +if (status !=GRUB_EFI_SUCCESS ) +return 0 ; + +if (r_device_path) + *r_device_path = device_path; + +return handle; +} + +static grub_efi_ipv4_address_t * +grub_dns_server_ip4_address (grub_efi_device_path_t *dp,grub_efi_uintn_t *num_dns) +{ +grub_efi_handle_t hnd; +grub_efi_status_t status; +grub_efi_ip4_config2_protocol_t *conf; +grub_efi_ipv4_address_t *addrs; +grub_efi_uintn_t data_size =1 *sizeof (grub_efi_ipv4_address_t); + + hnd =grub_efi_locate_device_path (&ip4_config_guid, dp,NULL ); +if (!hnd) +return 0 ; + + conf =grub_efi_open_protocol (hnd, &ip4_config_guid, +GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL ); +if (!conf) +return 0 ; + + addrs =grub_malloc (data_size); +if (!addrs) +return 0 ; + + status =efi_call_4 (conf->get_data, conf, +GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER , + &data_size, addrs); + +if (status ==GRUB_EFI_BUFFER_TOO_SMALL ) + { +grub_free (addrs); + addrs =grub_malloc (data_size); +if (!addrs) +return 0 ; + + status =efi_call_4 (conf->get_data, conf, +GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER , + &data_size, addrs); + } + +if (status !=GRUB_EFI_SUCCESS ) + { +grub_free (addrs); +return 0 ; + } + + *num_dns = data_size /sizeof (grub_efi_ipv4_address_t); +return addrs; +} + +static grub_efi_ipv6_address_t * +grub_dns_server_ip6_address (grub_efi_device_path_t *dp,grub_efi_uintn_t *num_dns) +{ +grub_efi_handle_t hnd; +grub_efi_status_t status; +grub_efi_ip6_config_protocol_t *conf; +grub_efi_ipv6_address_t *addrs; +grub_efi_uintn_t data_size =1 *sizeof (grub_efi_ipv6_address_t); + + hnd =grub_efi_locate_device_path (&ip6_config_guid, dp,NULL ); +if (!hnd) +return 0 ; + + conf =grub_efi_open_protocol (hnd, &ip6_config_guid, +GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL ); + +if (!conf) +return 0 ; + + addrs =grub_malloc (data_size); +if (!addrs) +return 0 ; + + status =efi_call_4 (conf->get_data, conf, +GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER , + &data_size, addrs); + +if (status ==GRUB_EFI_BUFFER_TOO_SMALL ) + { +grub_free (addrs); + addrs =grub_malloc (data_size); +if (!addrs) +return 0 ; + + status =efi_call_4 (conf->get_data, conf, +GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER , + &data_size, addrs); + } + +if (status !=GRUB_EFI_SUCCESS ) + { +grub_free (addrs); +return 0 ; + } + + *num_dns = data_size /sizeof (grub_efi_ipv6_address_t); +return addrs; +} +static struct grub_net_buff *grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp,int *use_ipv6) {
@@ -401,6 +519,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *ugrub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;struct grub_net_bootp_packet *bp;grub_uint8_t *ptr; +grub_efi_ipv4_address_t *dns; +grub_efi_uintn_t num_dns; bp = (struct grub_net_bootp_packet *) nb->tail; err =grub_netbuff_put (nb,sizeof (*bp) +4 );
@@ -462,6 +582,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u *ptr++ =sizeof ("HTTPClient" ) -1 ;grub_memcpy (ptr,"HTTPClient" ,sizeof ("HTTPClient" ) -1 ); + dns =grub_dns_server_ip4_address (dp, &num_dns); +if (dns) + { +grub_efi_uintn_t size_dns =sizeof (*dns) * num_dns; + + ptr = nb->tail; + err =grub_netbuff_put (nb, size_dns +2 ); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + *ptr++ =GRUB_NET_BOOTP_DNS ; + *ptr++ = size_dns; +grub_memcpy (ptr, dns, size_dns); +grub_free (dns); + } + ptr = nb->tail; err =grub_netbuff_put (nb,1 );if (err)
@@ -494,6 +633,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *ustruct grub_net_dhcp6_option *opt;struct grub_net_dhcp6_option_iana *iana;struct grub_net_dhcp6_option_iaaddr *iaaddr; +grub_efi_ipv6_address_t *dns; +grub_efi_uintn_t num_dns; d6p = (struct grub_net_dhcp6_packet *)nb->tail; err =grub_netbuff_put (nb,sizeof (*d6p));
@@ -557,6 +698,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u opt->len =grub_cpu_to_be16 (uri_len);grub_memcpy (opt->data, uri_dp->uri, uri_len); + dns =grub_dns_server_ip6_address (dp, &num_dns); +if (dns) + { +grub_efi_uintn_t size_dns =sizeof (*dns) * num_dns; + + opt = (struct grub_net_dhcp6_option *) nb->tail; + err =grub_netbuff_put (nb,sizeof (*opt) + size_dns); +if (err) + { +grub_free (ddp); +grub_netbuff_free (nb); +return NULL ; + } + opt->code =grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS ); + opt->len =grub_cpu_to_be16 (size_dns); +grub_memcpy (opt->data, dns, size_dns); +grub_free (dns); + } + *use_ipv6 =1 ; }
include/grub/efi/api.h +77 -0
89 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH v4 9/19 Support UEFI networking protocols
In reply to Robbie HarwoodFrom: Michael Chang <[email protected]>
References: fate#320130, bsc#1015589, bsc#1076132, rhbz#1732765
--- grub-core/Makefile.core.def | 12 + grub-core/io/bufio.c | 2 +- grub-core/kern/efi/efi.c | 96 +- grub-core/net/drivers/efi/efinet.c | 28 + grub-core/net/efi/dhcp.c | 357 +++++++ grub-core/net/efi/efi_netfs.c | 57 ++ grub-core/net/efi/http.c | 409 ++++++++ grub-core/net/efi/ip4_config.c | 417 ++++++++ grub-core/net/efi/ip6_config.c | 423 +++++++++ grub-core/net/efi/net.c | 1413 ++++++++++++++++++++++++++++ grub-core/net/efi/pxe.c | 354 +++++++ grub-core/net/http.c | 5 +- grub-core/net/net.c | 74 ++ include/grub/efi/api.h | 193 +++- include/grub/efi/dhcp.h | 347 +++++++ include/grub/efi/http.h | 215 +++++ include/grub/net/efi.h | 144 +++ util/grub-mknetdir.c | 23 +- 18 files changed, 4526 insertions(+), 43 deletions(-) create mode 100644 grub-core/net/efi/dhcp.c create mode 100644 grub-core/net/efi/efi_netfs.c create mode 100644 grub-core/net/efi/http.c create mode 100644 grub-core/net/efi/ip4_config.c create mode 100644 grub-core/net/efi/ip6_config.c create mode 100644 grub-core/net/efi/net.c create mode 100644 grub-core/net/efi/pxe.c create mode 100644 include/grub/efi/dhcp.h create mode 100644 include/grub/efi/http.h create mode 100644 include/grub/net/efi.h
grub-core/Makefile.core.def +12 -0
@@ -2297,6 +2297,12 @@ module = { common = hook/datehook.c; }; +module = { + name = efi_netfs; + common = net/efi/efi_netfs.c; + enable = efi; +}; + module = { name = net; common = net/net.c;
@@ -2310,6 +2316,12 @@ module = { common = net/ethernet.c; common = net/arp.c; common = net/netbuff.c; + efi = net/efi/net.c; + efi = net/efi/http.c; + efi = net/efi/pxe.c; + efi = net/efi/ip4_config.c; + efi = net/efi/ip6_config.c; + efi = net/efi/dhcp.c; }; module = {
grub-core/io/bufio.c +1 -1
@@ -139,7 +139,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)return res;/* Need to read some more. */ - next_buf = (file->offset + res + len -1 ) & ~((grub_off_t ) bufio->block_size -1 ); + next_buf = (grub_divmod64 (file->offset + res + len -1 , bufio->block_size,NULL )) * bufio->block_size;/* Now read between file->offset + res and bufio->buffer_at. */ if (file->offset + res < next_buf) {
grub-core/kern/efi/efi.c +78 -18
124 lines not on this page · raw message
grub-core/net/drivers/efi/efinet.c +28 -0
@@ -27,6 +27,7 @@#include <grub/lib/hexdump.h> #include <grub/types.h> #include <grub/net/netbuff.h> +#include <grub/env.h> GRUB_MOD_LICENSE ("GPLv3+" );
@@ -499,6 +500,18 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u ldp->length =sizeof (*ldp); ldp =grub_efi_find_last_device_path (ddp); + +/* Skip the DNS Device */ +if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) ==GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE + &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE ) + { + ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; + ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; + ldp->length =sizeof (*ldp); + + ldp =grub_efi_find_last_device_path (ddp); + } +if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) !=GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE ))
@@ -772,6 +785,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) !=GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE + &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE &&GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) !=GRUB_EFI_URI_DEVICE_PATH_SUBTYPE ))continue ; dup_dp =grub_efi_duplicate_device_path (dp);
@@ -786,6 +800,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device, dup_ldp->length =sizeof (*dup_ldp); } + dup_ldp =grub_efi_find_last_device_path (dup_dp); +if (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) ==GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE ) + { + dup_ldp =grub_efi_find_last_device_path (dup_dp); + dup_ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; + dup_ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; + dup_ldp->length =sizeof (*dup_ldp); + } + dup_ldp =grub_efi_find_last_device_path (dup_dp); dup_ldp->type =GRUB_EFI_END_DEVICE_PATH_TYPE ; dup_ldp->subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ;
@@ -876,6 +899,9 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,GRUB_MOD_INIT (efinet) { +if (grub_efi_net_config) +return ; +grub_efinet_findcards (); grub_efi_net_config = grub_efi_net_config_real; }
@@ -887,5 +913,7 @@ GRUB_MOD_FINI(efinet) FOR_NET_CARDS_SAFE (card, next)if (card->driver == &efidriver)grub_net_card_unregister (card); + + grub_efi_net_config =NULL ; }
grub-core/net/efi/dhcp.c new file +357 -0
357 lines not on this page · raw message
grub-core/net/efi/efi_netfs.c new file +57 -0
57 lines not on this page · raw message
grub-core/net/efi/http.c new file +409 -0
409 lines not on this page · raw message
grub-core/net/efi/ip4_config.c new file +417 -0
417 lines not on this page · raw message
grub-core/net/efi/ip6_config.c new file +423 -0
423 lines not on this page · raw message
grub-core/net/efi/net.c new file +1413 -0
1,118 lines not on this page · raw message
grub-core/net/efi/pxe.c new file +354 -0
354 lines not on this page · raw message
grub-core/net/net.c +74 -0
74 lines not on this page · raw message
include/grub/efi/api.h +174 -19
193 lines not on this page · raw message
include/grub/efi/dhcp.h new file +347 -0
347 lines not on this page · raw message
include/grub/efi/http.h new file +215 -0
215 lines not on this page · raw message
include/grub/net/efi.h new file +144 -0
144 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.39.0
Part of the diff is not shown here. View the raw message.
PATCH v4 10/19 efinet: also use the firmware acceleration for http
In reply to Robbie HarwoodFrom: Peter Jones <[email protected]>
--- grub-core/net/efi/net.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
grub-core/net/efi/net.c +4 -1
@@ -1307,7 +1307,10 @@ grub_efi_net_boot_from_https (void) && (subtype == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)) {grub_efi_uri_device_path_t *uri_dp = (grub_efi_uri_device_path_t *) dp; -return (grub_strncmp ((const char *) uri_dp->uri,"https://" ,sizeof ("https://" ) -1 ) ==0 ) ?1 :0 ; + +grub_dprintf ("efinet" ,"url:%s\n" , (const char *) uri_dp->uri); +return (grub_strncmp ((const char *) uri_dp->uri,"https://" ,sizeof ("https://" ) -1 ) ==0 || +grub_strncmp ((const char *) uri_dp->uri,"http://" ,sizeof ("http://" ) -1 ) ==0 ); }if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
Signature · 1 line
2.39.0
PATCH v4 11/19 efi/http: match protocol+hostname of boot url in root_url
In reply to Robbie HarwoodFrom: Peter Jones <[email protected]>
This lets you write config files that don't know urls.
--- grub-core/net/efi/http.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
grub-core/net/efi/http.c +19 -0
@@ -3,6 +3,7 @@#include <grub/misc.h> #include <grub/net/efi.h> #include <grub/charset.h> +#include <grub/env.h> static void http_configure (struct grub_efi_net_device *dev,int prefer_ip6)
@@ -338,6 +339,24 @@ grub_efihttp_open (struct grub_efi_net_device *dev,grub_err_t err;grub_off_t size =0 ;char *buf =NULL ; +char *root_url; +grub_efi_ipv6_address_t address; +const char *rest; + +if (grub_efi_string_to_ip6_address (file->device->net->server, &address, &rest) && *rest ==0 ) + root_url =grub_xasprintf ("%s://[%s]" , type ?"https" :"http" , file->device->net->server); +else + root_url =grub_xasprintf ("%s://%s" , type ?"https" :"http" , file->device->net->server); +if (root_url) + { +grub_env_unset ("root_url" ); +grub_env_set ("root_url" , root_url); +grub_free (root_url); + } +else + { +return grub_errno; + } err =efihttp_request (dev->http, file->device->net->server, file->device->net->name, type,1 ,0 );if (err !=GRUB_ERR_NONE )
Signature · 1 line
2.39.0
PATCH v4 12/19 Add fw_path variable to detect config file on efi
In reply to Robbie HarwoodFrom: Paulo Flabiano Smorigo <[email protected]>
This patch makes grub look for its config file on efi where the app was found.
Resolves: rhbz#857936, rhbz#1616395
--- grub-core/kern/main.c | 14 +++++++------- grub-core/normal/main.c | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-)
grub-core/kern/main.c +7 -7
@@ -128,16 +128,16 @@ grub_set_prefix_and_root (void)grub_machine_get_bootlocation (&fwdevice, &fwpath); -if (fwdevice) +if (fwdevice && fwpath) { -char *cmdpath; +char *fw_path; - cmdpath =grub_xasprintf ("(%s)%s" , fwdevice, fwpath ? :"" ); -if (cmdpath) + fw_path =grub_xasprintf ("(%s)/%s" , fwdevice, fwpath); +if (fw_path) { -grub_env_set ("cmdpath" , cmdpath); -grub_env_export ("cmdpath" ); -grub_free (cmdpath); +grub_env_set ("fw_path" , fw_path); +grub_env_export ("fw_path" ); +grub_free (fw_path); } }
grub-core/normal/main.c +24 -1
@@ -320,7 +320,30 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),/* Guess the config filename. It is necessary to make CONFIG static, so that it won't get broken by longjmp. */ char *config; -const char *prefix; +const char *prefix, *fw_path; + + fw_path =grub_env_get ("fw_path" ); +if (fw_path) + { + config =grub_xasprintf ("%s/grub.cfg" , fw_path); +if (config) + { +grub_file_t file; + + file =grub_file_open (config,GRUB_FILE_TYPE_CONFIG ); +if (file) + { +grub_file_close (file); +grub_enter_normal_mode (config); + } +else + { +/* Ignore all errors. */ + grub_errno =0 ; + } +grub_free (config); + } + } prefix =grub_env_get ("prefix" );if (prefix)
Signature · 1 line
2.39.0
PATCH v4 13/19 use fw_path prefix when fallback searching for grub config
In reply to Robbie HarwoodFrom: Mark Salter <[email protected]>
When PXE booting via UEFI firmware, grub was searching for grub.cfg in the fw_path directory where the grub application was found. If that didn't exist, a fallback search would look for config file names based on MAC and IP address. However, the search would look in the prefix directory which may not be the same fw_path. This patch changes that behavior to use the fw_path directory for the fallback search. Only if fw_path is NULL will the prefix directory be searched.
--- grub-core/normal/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
grub-core/normal/main.c +3 -2
@@ -322,7 +322,7 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),char *config;const char *prefix, *fw_path; - fw_path =grub_env_get ("fw_path" ); + prefix = fw_path =grub_env_get ("fw_path" );if (fw_path) { config =grub_xasprintf ("%s/grub.cfg" , fw_path);
@@ -345,7 +345,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)), } } - prefix =grub_env_get ("prefix" ); +if (!prefix) + prefix =grub_env_get ("prefix" );if (prefix) {grub_size_t config_len;
Signature · 1 line
2.39.0
PATCH v4 14/19 Try mac/guid/etc before grub.cfg on tftp config files
In reply to Robbie HarwoodFrom: Peter Jones <[email protected]>
--- grub-core/kern/ieee1275/init.c | 28 +++---- grub-core/net/net.c | 2 +- grub-core/normal/main.c | 129 +++++++++++++++++---------------- 3 files changed, 82 insertions(+), 77 deletions(-)
grub-core/kern/ieee1275/init.c +15 -13
@@ -116,23 +116,25 @@ grub_machine_get_bootlocation (char **device, char **path)grub_free (canon); }else - *device =grub_ieee1275_encode_devname (bootpath); -grub_free (type); - - filename =grub_ieee1275_get_filename (bootpath); -if (filename) { -char *lastslash =grub_strrchr (filename,'\\' ); - -/* Truncate at last directory. */ -if (lastslash) + filename =grub_ieee1275_get_filename (bootpath); +if (filename) { - *lastslash ='\0' ; -grub_translate_ieee1275_path (filename); +char *lastslash =grub_strrchr (filename,'\\' ); - *path = filename; - } +/* Truncate at last directory. */ +if (lastslash) + { + *lastslash ='\0' ; +grub_translate_ieee1275_path (filename); + + *path = filename; + } + } + *device =grub_ieee1275_encode_devname (bootpath); } + +grub_free (type);grub_free (bootpath); }
grub-core/net/net.c +1 -1
@@ -2083,7 +2083,7 @@ grub_net_search_config_file (char *config)/* Remove the remaining minus sign at the end. */ config[config_len] ='\0' ; -return GRUB_ERR_NONE ; +return GRUB_ERR_FILE_NOT_FOUND ; }static struct grub_preboot *fini_hnd;
grub-core/normal/main.c +66 -63
142 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH v4 15/19 Prepend prefix when HTTP path is relative
In reply to Robbie HarwoodFrom: Stephen Benjamin <[email protected]>
This sets a couple of variables. With the url http://www.example.com/foo/bar :http_path: /foo/barhttp_url: http://www.example.com/foo/bar
Resolves: rhbz#1616395
--- grub-core/kern/main.c | 10 ++++- grub-core/net/efi/http.c | 85 ++++++++++++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 22 deletions(-)
grub-core/kern/main.c +9 -1
@@ -131,12 +131,20 @@ grub_set_prefix_and_root (void)if (fwdevice && fwpath) {char *fw_path; +char separator[3 ] =")" ; - fw_path =grub_xasprintf ("(%s)/%s" , fwdevice, fwpath); +grub_dprintf ("fw_path" ,"\n" ); +grub_dprintf ("fw_path" ,"fwdevice:\"%s\" fwpath:\"%s\"\n" , fwdevice, fwpath); + +if (!grub_strncmp (fwdevice,"http" ,4 ) && fwpath[0 ] !='/' ) +grub_strcpy (separator,")/" ); + + fw_path =grub_xasprintf ("(%s%s%s" , fwdevice, separator, fwpath);if (fw_path) {grub_env_set ("fw_path" , fw_path);grub_env_export ("fw_path" ); +grub_dprintf ("fw_path" ,"fw_path:\"%s\"\n" , fw_path);grub_free (fw_path); } }
grub-core/net/efi/http.c +64 -21
106 lines not on this page · raw message
Signature · 1 line
2.39.0
PATCH v4 16/19 efi/http: Enclose literal IPv6 addresses in square brackets
In reply to Robbie HarwoodFrom: Javier Martinez Canillas <[email protected]>
According to RFC 2732 (https://www.ietf.org/rfc/rfc2732.txt), literal IPv6 addresses must be enclosed in square brackets. But GRUB currently does not do this and is causing HTTP servers to send Bad Request (400) responses.
For example, the following is the HTTP stream when fetching a config file:
HEAD /EFI/BOOT/grub.cfg HTTP/1.1
Host: 2000:dead:beef:a::1
Accept: */*
User-Agent: UefiHttpBoot/1.0
HTTP/1.1 400 Bad Request
Date: Thu, 05 Mar 2020 14:46:02 GMT
Server: Apache/2.4.41 (Fedora) OpenSSL/1.1.1d
Connection: close
Content-Type: text/html; charset=iso-8859-1
and after enclosing the IPv6 address the HTTP request is successful:
HEAD /EFI/BOOT/grub.cfg HTTP/1.1
Host: [2000:dead:beef:a::1]
Accept: */*
User-Agent: UefiHttpBoot/1.0
HTTP/1.1 200 OK
Date: Thu, 05 Mar 2020 14:48:04 GMT
Server: Apache/2.4.41 (Fedora) OpenSSL/1.1.1d
Last-Modified: Thu, 27 Feb 2020 17:45:58 GMT
ETag: "206-59f924b24b1da"
Accept-Ranges: bytes
Content-Length: 518
Resolves: rhbz#1732765
--- grub-core/net/efi/http.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-)
grub-core/net/efi/http.c +28 -9
@@ -147,13 +147,7 @@ efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https,grub_efi_status_t status;grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;char *url =NULL ; - - request_headers[0 ].field_name = (grub_efi_char8_t *)"Host" ; - request_headers[0 ].field_value = (grub_efi_char8_t *) server; - request_headers[1 ].field_name = (grub_efi_char8_t *)"Accept" ; - request_headers[1 ].field_value = (grub_efi_char8_t *)"*/*" ; - request_headers[2 ].field_name = (grub_efi_char8_t *)"User-Agent" ; - request_headers[2 ].field_value = (grub_efi_char8_t *)"UefiHttpBoot/1.0" ; +char *hostname =NULL ; {grub_efi_ipv6_address_t address;
@@ -163,9 +157,24 @@ efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https,const char *protocol = (use_https ==1 ) ?"https" :"http" ;if (grub_efi_string_to_ip6_address (server, &address, &rest) && *rest ==0 ) - url =grub_xasprintf ("%s://[%s]%s" , protocol, server, name); + { + hostname =grub_xasprintf ("[%s]" , server); +if (!hostname) +return GRUB_ERR_OUT_OF_MEMORY ; + + server = hostname; + + url =grub_xasprintf ("%s://%s%s" , protocol, server, name); +if (!url) + { +grub_free (hostname); +return GRUB_ERR_OUT_OF_MEMORY ; + } + }else - url =grub_xasprintf ("%s://%s%s" , protocol, server, name); + { + url =grub_xasprintf ("%s://%s%s" , protocol, server, name); + }if (!url) {
@@ -190,6 +199,13 @@ efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https, request_data.url = ucs2_url; } + request_headers[0 ].field_name = (grub_efi_char8_t *)"Host" ; + request_headers[0 ].field_value = (grub_efi_char8_t *) server; + request_headers[1 ].field_name = (grub_efi_char8_t *)"Accept" ; + request_headers[1 ].field_value = (grub_efi_char8_t *)"*/*" ; + request_headers[2 ].field_name = (grub_efi_char8_t *)"User-Agent" ; + request_headers[2 ].field_value = (grub_efi_char8_t *)"UefiHttpBoot/1.0" ; + request_data.method = (headeronly >0 ) ?GRUB_EFI_HTTPMETHODHEAD :GRUB_EFI_HTTPMETHODGET ; request_message.data.request = &request_data;
@@ -219,6 +235,9 @@ efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https, status =efi_call_2 (http->request, http, &request_token); +if (hostname) +grub_free (hostname); +if (status !=GRUB_EFI_SUCCESS ) {efi_call_1 (b->close_event, request_token.event);
Signature · 1 line
2.39.0
PATCH v4 17/19 http: Prepend prefix when the HTTP path is relative
In reply to Robbie HarwoodFrom: Javier Martinez Canillas <[email protected]>
There are two different HTTP drivers that can be used when requesting an HTTP resource: the efi/http that uses the EFI_HTTP_PROTOCOL and the http that uses GRUB's HTTP and TCP/IP implementation.
The efi/http driver appends a prefix that is defined in the variable http_path, but the http driver doesn't. So using this driver and attempting to fetch a resource using a relative path fails. Match the behavior of efi/http.
--- grub-core/net/http.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
grub-core/net/http.c +9 -1
@@ -16,6 +16,7 @@* along with GRUB. If not, see <http://www.gnu.org/licenses/>. */ +#include <grub/env.h> #include <grub/misc.h> #include <grub/net/tcp.h> #include <grub/net/ip.h>
@@ -508,13 +509,20 @@ http_open (struct grub_file *file, const char *filename) {grub_err_t err;struct http_data *data; +const char *http_path; data =grub_zalloc (sizeof (*data));if (!data)return grub_errno; file->size =GRUB_FILE_SIZE_UNKNOWN ; - data->filename =grub_strdup (filename); +/* If path is relative, prepend http_path */ + http_path =grub_env_get ("http_path" ); +if (http_path && filename[0 ] !='/' ) + data->filename =grub_xasprintf ("%s/%s" , http_path, filename); +else + data->filename =grub_strdup (filename); +if (!data->filename) {grub_free (data);
Signature · 1 line
2.39.0
PATCH v4 18/19 normal/main: Discover the device to read the config from as a fallback
In reply to Robbie HarwoodFrom: Javier Martinez Canillas <[email protected]>
When core.img is generated locally, the grub2-probe tool figures out the device and partition that needs to be read to parse the GRUB configuration file.
But in some cases the core.img can't be generated on the host and instead has to be done at package build time. In particular, this will be true when it needs to be signed with a key that's only available on the package building infrastructure.
In that case, the prefix variable won't have a device and partition but only a directory path. So there's no way for GRUB to know from which device has to read the configuration file.
To allow GRUB to continue working on that scenario, fallback to iterating over all the available devices if reading the config failed when using the prefix and fw_path variables.
--- grub-core/normal/main.c | 58 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-)
grub-core/normal/main.c +51 -7
@@ -311,18 +311,13 @@ grub_enter_normal_mode (const char *config) }static grub_err_t -grub_try_normal (const char *variable) +grub_try_normal_prefix (const char *prefix) {char *config; -const char *prefix;grub_err_t err =GRUB_ERR_FILE_NOT_FOUND ;const char *net_search_cfg;int disable_net_search =0 ; - prefix =grub_env_get (variable); -if (!prefix) -return GRUB_ERR_FILE_NOT_FOUND ; - net_search_cfg =grub_env_get ("feature_net_search_cfg" );if (net_search_cfg && net_search_cfg[0 ] =='n' ) disable_net_search =1 ;
@@ -336,7 +331,7 @@ grub_try_normal (const char *variable) config =grub_malloc (config_len);if (! config) -return GRUB_ERR_FILE_NOT_FOUND ; +return err;grub_snprintf (config, config_len,"%s/grub.cfg" , prefix); err =grub_net_search_config_file (config);
@@ -365,6 +360,53 @@ grub_try_normal (const char *variable)return err; } +static int +grub_try_normal_dev (const char *name,void *data) +{ +grub_err_t err; +const char *prefix =grub_xasprintf ("(%s)%s" , name, (char *) data); + +if (!prefix) +return 0 ; + + err =grub_try_normal_prefix (prefix); +if (err ==GRUB_ERR_NONE ) +return 1 ; + +return 0 ; +} + +static grub_err_t +grub_try_normal_discover (void ) +{ +const char *prefix =grub_env_get ("prefix" ); +grub_err_t err =GRUB_ERR_FILE_NOT_FOUND ; + +if (!prefix) +return err; + +if (grub_device_iterate (grub_try_normal_dev, (void *) prefix)) +return GRUB_ERR_NONE ; + +return err; +} + +static grub_err_t +grub_try_normal (const char *variable) +{ +grub_err_t err =GRUB_ERR_FILE_NOT_FOUND ; +const char *prefix; + +if (!variable) +return err; + + prefix =grub_env_get (variable); +if (!prefix) +return err; + +return grub_try_normal_prefix (prefix); +} +/* Enter normal mode from rescue mode. */ static grub_err_t grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
@@ -379,6 +421,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)), err =grub_try_normal ("fw_path" );if (err ==GRUB_ERR_FILE_NOT_FOUND ) err =grub_try_normal ("prefix" ); +if (err ==GRUB_ERR_FILE_NOT_FOUND ) + err =grub_try_normal_discover ();if (err ==GRUB_ERR_FILE_NOT_FOUND )grub_enter_normal_mode (0 ); }
Signature · 1 line
2.39.0
PATCH v4 19/19 efinet: Add DHCP proxy support
In reply to Robbie HarwoodFrom: Ian Page Hands <[email protected]>
If a proxyDHCP configuration is used, the server name, server IP and boot file values should be taken from the DHCP proxy offer instead of the DHCP server ack packet.
--- grub-core/net/drivers/efi/efinet.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
grub-core/net/drivers/efi/efinet.c +25 -2
@@ -863,10 +863,33 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,else {grub_dprintf ("efinet" ,"using ipv4 and dhcp\n" ); + +struct grub_net_bootp_packet *dhcp_ack = (void *) &pxe_mode->dhcp_ack; + +if (pxe_mode->proxy_offer_received) + { +grub_dprintf ("efinet" ,"proxy offer receive" ); +struct grub_net_bootp_packet *proxy_offer = (void *) &pxe_mode->proxy_offer; + +if (proxy_offer && dhcp_ack->boot_file[0 ] =='\0' ) + { +grub_dprintf ("efinet" ,"setting values from proxy offer" ); +/* +* Here we got a proxy offer and the dhcp_ack has a nil +* boot_file Copy the proxy DHCP offer details into the +* bootp_packet we are sending forward as they are the deatils +* we need. +*/ + *dhcp_ack->server_name = *proxy_offer->server_name; + *dhcp_ack->boot_file = *proxy_offer->boot_file; + dhcp_ack->server_ip = proxy_offer->server_ip; + } + } + inter =grub_net_configure_by_dhcp_ack (card->name, card,0 , (struct grub_net_bootp_packet *) - packet_buf, - packet_bufsz, + &pxe_mode->dhcp_ack, +sizeof (pxe_mode->dhcp_ack),1 , device, path);grub_dprintf ("efinet" ,"device: `%s' path: `%s'\n" , *device, *path); }
Signature · 1 line
2.39.0