Support DHCPv6 and UEFI IPv6 PXE
PATCH 1/1 Support DHCPv6 and UEFI IPv6 PXE
This patch set tries to make support of configuring IPv6 network interface through the DHCPv6 protocol. A new command, net_bootp6, is therefore introduced to serve the purpose. You can think it as DHCPv6/IPv6 version of the existing net_bootp command.
In addition to that, the UEFI IPv6 PXE support can be easily done by sharing the same routine with net_bootp6 to parse DHCPv6 Reply packet cached in the firmware.
changes in v1:
- Added upper boundary check in find_dhcpv6_option
- Fix memory leak and freeing NULL pointer
- Improved error message logging to not get lost
- Use grub_cpu_to_be16_compile_time for endianess conversion when appropriate
- Removed grub_dhcpv6_dns_servers structure and use 16 bytes blocks
- Avoud magic numbers and use more descriptive sizeof when populating netbuff
- Move include/grub/efi/api.h to UEFI IPv6 PXE support patch
- Document the net_bootp6 command
PATCH 2/3 UEFI IPv6 PXE support
When system is booted from UEFI IPv6 PXE, the network interface can be configured directly by cached DHCPv6 packet in firmware without doing it all over again by contacting DHCP server.
--- grub-core/net/drivers/efi/efinet.c | 24 ++++++++++++--- include/grub/efi/api.h | 55 +++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-)
grub-core/net/drivers/efi/efinet.c +19 -5
@@ -349,11 +349,25 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,if (! pxe)continue ; pxe_mode = pxe->mode; -grub_net_configure_by_dhcp_ack (card->name, card,0 , - (struct grub_net_bootp_packet *) - &pxe_mode->dhcp_ack, -sizeof (pxe_mode->dhcp_ack), -1 , device, path); + +if (pxe_mode->using_ipv6) + { +grub_net_configure_by_dhcpv6_reply (card->name, card,0 , + (struct grub_net_dhcpv6_packet *) + &pxe_mode->dhcp_ack, +sizeof (pxe_mode->dhcp_ack), +1 , device, path); +if (grub_errno) +grub_print_error (); + } +else + { +grub_net_configure_by_dhcp_ack (card->name, card,0 , + (struct grub_net_bootp_packet *) + &pxe_mode->dhcp_ack, +sizeof (pxe_mode->dhcp_ack), +1 , device, path); + }return ; } }
include/grub/efi/api.h +54 -1
68 lines not on this page · raw message
Signature · 1 line
1.7.3.4
PATCH 3/3 Update document for net_bootp6 command
In reply to Michael ChangUpdate document for net_bootp6 command
--- docs/grub.texi | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
docs/grub.texi +17 -0
@@ -5110,6 +5110,7 @@ List available video modes. If resolution is given, show only matching modes. * net_add_dns:: Add a DNS server * net_add_route:: Add routing entry * net_bootp:: Perform a bootp 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
@@ -5189,6 +5190,22 @@ Sets environment variable @samp{net_}@var{<card>}@samp{_dhcp_extensionspath} @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_del_addr @subsection net_del_addr
Signature · 1 line
1.7.3.4
PATCH 1/3 Added net_bootp6 command
In reply to Michael ChangThe net_bootp6 is used to configure the ipv6 network interface through the DHCPv6 protocol Solict/Advertise/Request/Reply.
--- grub-core/net/bootp.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++++- grub-core/net/ip.c | 35 ++ include/grub/net.h | 19 + 3 files changed, 948 insertions(+), 1 deletions(-)
grub-core/net/bootp.c +894 -1
@@ -24,6 +24,8 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> static void parse_dhcp_vendor (const char *name,const void *vend,int limit,int *mask)
@@ -256,6 +258,646 @@ grub_net_configure_by_dhcp_ack (const char *name,return inter; } +struct grub_dhcpv6_option { +grub_uint16_t code; +grub_uint16_t len; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + + +struct grub_dhcpv6_iana_option { +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_dhcpv6_iaaddr_option { +grub_uint8_t addr[16 ]; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_DUID_LL +{ +grub_uint16_t type; +grub_uint16_t hw_type; +grub_uint8_t hwaddr[6 ]; +}GRUB_PACKED ; + +#define GRUB_DHCPv6_ADVERTISE 2 +#define GRUB_DHCPv6_REQUEST 3 +#define GRUB_DHCPv6_REPLY 7 + +#define GRUB_DHCPv6_OPTION_CLIENTID 1 +#define GRUB_DHCPv6_OPTION_SERVERID 2 +#define GRUB_DHCPv6_OPTION_IA_NA 3 +#define GRUB_DHCPv6_OPTION_IAADDR 5 +#define GRUB_DHCPv6_OPTION_ORO 6 +#define GRUB_DHCPv6_OPTION_ELAPSED_TIME 8 +#define GRUB_DHCPv6_OPTION_DNS_SERVERS 23 +#define GRUB_DHCPv6_OPTION_BOOTFILE_URL 59 + +/* The default netbuff size for sending DHCPv6 packets which should be +large enough to hold the information */ +#define GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE 512 + +struct grub_dhcpv6_session +{ +struct grub_dhcpv6_session *next; +struct grub_dhcpv6_session **prev; +grub_uint32_t iaid; +grub_uint32_t transaction_id:24 ; +grub_uint64_t start_time; +struct grub_net_network_level_interface *iface; +}; + +static struct grub_dhcpv6_session *grub_dhcpv6_sessions =NULL ; +#define FOR_DHCPV6_SESSIONS (var) FOR_LIST_ELEMENTS (var, grub_dhcpv6_sessions) + +static void +grub_dhcpv6_session_add (struct grub_dhcpv6_session *session) +{ +struct grub_datetime date; +grub_err_t err; +grub_int32_t t =0 ; + + err =grub_get_datetime (&date); +if (err || !grub_datetime2unixtime (&date, &t)) + { + grub_errno =GRUB_ERR_NONE ; + t =0 ; + } + + session->transaction_id = t; + session->start_time =grub_get_time_ms (); +grub_list_push (GRUB_AS_LIST_P (&grub_dhcpv6_sessions),GRUB_AS_LIST (session)); + +return ; +} + +static void +grub_dhcpv6_sessions_free (void ) +{ +struct grub_dhcpv6_session *session; + +FOR_DHCPV6_SESSIONS (session) + { +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return ; +} + +static const char * +get_dhcpv6_option_name (grub_uint16_t option) +{ +switch (option) + { +case GRUB_DHCPv6_OPTION_BOOTFILE_URL: +return "BOOTFILE URL" ; +case GRUB_DHCPv6_OPTION_DNS_SERVERS: +return "DNS SERVERS" ; +case GRUB_DHCPv6_OPTION_IA_NA: +return "IA NA" ; +case GRUB_DHCPv6_OPTION_IAADDR: +return "IAADDR" ; +case GRUB_DHCPv6_OPTION_CLIENTID: +return "CLIENTID" ; +case GRUB_DHCPv6_OPTION_SERVERID: +return "SERVERID" ; +case GRUB_DHCPv6_OPTION_ORO: +return "ORO" ; +case GRUB_DHCPv6_OPTION_ELAPSED_TIME: +return "ELAPSED TIME" ; +default : +return "UNKNOWN" ; + } +} + +static const struct grub_dhcpv6_option * +find_dhcpv6_option (const struct grub_dhcpv6_option *popt,grub_size_t size,grub_uint16_t option) +{ +while (size >0 ) + { +grub_uint16_t code, len; + + code =grub_be_to_cpu16 (popt->code); + len =grub_be_to_cpu16 (popt->len) +sizeof (*popt); + +if (size < len) + { +grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("DHCPv6 option size overflow detected" )); +return NULL ; + } + +if (option == code) +return popt; + +if (code ==0 ) +break ; +else + { + size -= len; + popt = (const struct grub_dhcpv6_option *)((grub_uint8_t *)popt + len); + } + } + +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 Option (%u):%s not found" ), option,get_dhcpv6_option_name (option)); +return NULL ; +} + +static const struct grub_dhcpv6_option * +find_dhcpv6_option_in_packet (const struct grub_net_dhcpv6_packet *packet, +grub_size_t size,grub_uint16_t option) +{ +if (!size || !packet) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("null or zero sized DHCPv6 packet buffer" )); +return NULL ; + } + +if (size <=sizeof (*packet)) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("DHCPv6 packet size too small" )); +return NULL ; + } + +return find_dhcpv6_option ((const struct grub_dhcpv6_option *)packet->dhcp_options, + size -sizeof (*packet), option); +} + +static const grub_uint8_t * +find_dhcpv6_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size) +{ +const struct grub_dhcpv6_option * popt; +const struct grub_dhcpv6_iana_option *ia_na; +const struct grub_dhcpv6_iaaddr_option *iaaddr; +grub_uint16_t ia_na_len; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_IA_NA); + +if (!popt) +return NULL ; + + ia_na = (const struct grub_dhcpv6_iana_option *) popt->data; + ia_na_len =grub_be_to_cpu16 (popt->len); + + popt =find_dhcpv6_option ((const struct grub_dhcpv6_option *) ia_na->data, + ia_na_len -sizeof (*ia_na), GRUB_DHCPv6_OPTION_IAADDR); + +if (!popt) +return NULL ; + + iaaddr = (const struct grub_dhcpv6_iaaddr_option *)popt->data; +return iaaddr->addr; +} + +static void +get_dhcpv6_dns_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +grub_net_network_level_address_t **addr,grub_uint16_t *naddr) +{ +const struct grub_dhcpv6_option * popt; +grub_uint16_t len, ln; +const grub_uint8_t *pa; +grub_net_network_level_address_t *la; + +if (!addr || !naddr) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("bad argument for get_dhcpv6_dns_address" )); +return ; + } + + *addr =NULL ; + *naddr =0 ; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_DNS_SERVERS); +if (!popt) +return ; + + len =grub_be_to_cpu16 (popt->len); +if (len ==0 || len &0xf ) + { +grub_error (GRUB_ERR_IO ,N_ ("invalid dns address length" )); +return ; + } + + *naddr = ln = len >>4 ; + *addr = la =grub_zalloc (sizeof (grub_net_network_level_address_t) * ln); + +if (!la) + { + *addr =NULL ; + *naddr =0 ; +return ; + } + +for (pa = popt->data; ln >0 ; pa +=0x10 , la++, ln--) + { + la->type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + la->ipv6[0 ] =grub_get_unaligned64 (pa); + la->ipv6[1 ] =grub_get_unaligned64 (pa +8 ); + la->option =DNS_OPTION_PREFER_IPV6 ; + } + +return ; +} + +static void +find_dhcpv6_bootfile_url (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +char **proto,char **server_ip,char **boot_file) +{ +char *bootfile_url; +const struct grub_dhcpv6_option * opt_url; +char *ip_start, *ip_end; +char *path; +grub_size_t ip_len; +grub_uint16_t len; +const char *protos[] = {"tftp://" ,"http://" ,NULL }; +const char *pr; +int i; + +if (proto) + *proto =NULL ; + +if (server_ip) + *server_ip =NULL ; + +if (boot_file) + *boot_file =NULL ; + + opt_url =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_BOOTFILE_URL); + +if (!opt_url) +return ; + + len =grub_be_to_cpu16 (opt_url->len); + + bootfile_url =grub_malloc (len +1 ); + +if (!bootfile_url) +return ; + +grub_memcpy (bootfile_url, opt_url->data, len); + bootfile_url[len] ='\0' ; + +for (i =0 ; (pr = *(protos + i)); ++i) +if (grub_strncmp (bootfile_url, pr,grub_strlen (pr)) ==0 ) +break ; + +if (!pr) + { +grub_error (GRUB_ERR_IO , +N_ ("unsupported protocol, only tftp and http are supported" )); + goto cleanup; + } + + ip_start = ip_end =NULL ; + ip_start = bootfile_url +grub_strlen (pr); + +/* Follow elilo and edk2 that check for starting and ending delimiter '[..]' +in which IPv6 server address is enclosed. */ +if (*ip_start !='[' ) + ip_start =NULL ; +else + ip_end =grub_strchr (++ip_start,']' ); + +if (!ip_start || !ip_end) + { +grub_error (GRUB_ERR_IO ,N_ ("IPv6-address not in square brackets" )); + goto cleanup; + } + + ip_len = ip_end - ip_start; + +if (proto) + { +grub_size_t proto_len =grub_strlen (pr) -3 ; + + *proto =grub_malloc (proto_len +1 ); +if (!*proto) + goto cleanup; + +grub_memcpy (*proto, pr, proto_len); + *(*proto + proto_len) ='\0' ; + } + +if (server_ip) + { + *server_ip =grub_malloc (ip_len +1 ); + +if (!*server_ip) + goto cleanup; + +grub_memcpy (*server_ip, ip_start, ip_len); + *(*server_ip + ip_len) ='\0' ; + } + + path = ip_end +1 ; + +if (boot_file) + { + *boot_file =grub_strdup (path); + +if (!*boot_file) + goto cleanup; + } + +cleanup: + +if (bootfile_url) +grub_free (bootfile_url); + +if (grub_errno) + { +if (proto && *proto) + { +grub_free (*proto); + *proto =NULL ; + } + +if (server_ip && *server_ip) + { +grub_free (*server_ip); + *server_ip =NULL ; + } + +if (boot_file && *boot_file) + { +grub_free (*boot_file); + *boot_file =NULL ; + } + } + +return ; +} + + +static grub_err_t +grub_net_configure_by_dhcpv6_adv (const struct grub_net_dhcpv6_packet *v6_adv, +grub_size_t size, +struct grub_dhcpv6_session *session) +{ +struct grub_net_buff *nb; +const struct grub_dhcpv6_option *opt_client, *opt_server, *opt_iana; +struct grub_dhcpv6_option *popt; +struct grub_net_dhcpv6_packet *v6; +struct udphdr *udph; +grub_net_network_level_address_t multicast; +grub_net_link_level_address_t ll_multicast; +struct grub_net_network_level_interface *inf; +grub_uint16_t len; +grub_uint64_t elapsed; +grub_err_t err =GRUB_ERR_NONE ; + + opt_client =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_CLIENTID); +if (grub_errno) +grub_error_push (); + + opt_server =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_SERVERID); +if (grub_errno) +grub_error_push (); + + opt_iana =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_IA_NA); +if (grub_errno) +grub_error_push (); + +grub_error_pop (); +if (grub_errno) +return grub_errno; + + inf = session->iface; + + 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_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb) +return grub_errno; + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + len =grub_cpu_to_be16 (opt_client->len); + err =grub_netbuff_push (nb, len +sizeof (*opt_client)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_client, len +sizeof (*opt_client)); + + len =grub_cpu_to_be16 (opt_server->len); + err =grub_netbuff_push (nb, len +sizeof (*opt_server)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_server, len +sizeof (*opt_server)); + + len =grub_cpu_to_be16 (opt_iana->len); + err =grub_netbuff_push (nb, len +sizeof (*opt_iana)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_iana, len +sizeof (*opt_iana)); + + err =grub_netbuff_push (nb,sizeof (*popt) +2 *sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ORO); + popt->len =grub_cpu_to_be16_compile_time (2 *sizeof (grub_uint16_t)); +grub_set_unaligned16 (popt->data,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_BOOTFILE_URL)); +grub_set_unaligned16 (popt->data +2 ,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_DNS_SERVERS)); + + err =grub_netbuff_push (nb,sizeof (*popt) +sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ELAPSED_TIME); + popt->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 () - session->start_time,10 ,0 ); + +if (elapsed >0xffff ) + elapsed =0xffff ; + +grub_set_unaligned16 (popt->data,grub_cpu_to_be16 ((grub_uint16_t )elapsed)); + + err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *) nb->data; + v6->message_type = GRUB_DHCPv6_REQUEST; + v6->transaction_id = v6_adv->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 (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + 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_dhcpv6_packet *v6, +grub_size_t size, +int is_def, +char **device,char **path) +{ +grub_net_network_level_address_t addr; +grub_net_network_level_netaddress_t netaddr; +struct grub_net_network_level_interface *inf; +const grub_uint8_t *your_ip; +char *proto; +char *server_ip; +char *boot_file; +grub_net_network_level_address_t *dns; +grub_uint16_t num_dns; + +if (device) + *device =NULL ; + +if (path) + *path =NULL ; + +if (v6->message_type != GRUB_DHCPv6_REPLY) + { +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 info not found" )); +return NULL ; + } + + your_ip =find_dhcpv6_address (v6, size); + +if (!your_ip) +return NULL ; + + addr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + addr.ipv6[0 ] =grub_get_unaligned64 (your_ip); + addr.ipv6[1 ] =grub_get_unaligned64 (your_ip +8 ); + inf =grub_net_add_addr (name, card, &addr, &card->default_address, flags); + + netaddr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + netaddr.ipv6.base[0 ] =grub_get_unaligned64 (your_ip); + netaddr.ipv6.base[1 ] =0 ; + netaddr.ipv6.masksize =64 ; +grub_net_add_route (name, netaddr, inf); + +get_dhcpv6_dns_address (v6, size, &dns, &num_dns); + +if (grub_errno) + grub_errno =GRUB_ERR_NONE ; + +if (dns && num_dns) + { +int i; + +for (i =0 ; i < num_dns; ++i) +grub_net_add_dns_server (dns + i); + +grub_free (dns); + } + +find_dhcpv6_bootfile_url (v6, size, &proto, &server_ip, &boot_file); + +if (grub_errno) +grub_print_error (); + +if (boot_file) +grub_env_set_net_property (name,"boot_file" , boot_file, +grub_strlen (boot_file)); + +if (is_def && server_ip) + { + grub_net_default_server =grub_strdup (server_ip); +grub_env_set ("net_default_interface" , name); +grub_env_export ("net_default_interface" ); + } + +if (device && server_ip && proto) + { + *device =grub_xasprintf ("%s,%s" , proto, server_ip); +if (!*device) +grub_print_error (); + } + +if (path && boot_file) + { + *path =grub_strdup (boot_file); +if (*path) + { +char *slash; + slash =grub_strrchr (*path,'/' ); +if (slash) + *slash =0 ; +else + **path =0 ; + } +else +grub_print_error (); + } + +if (proto) +grub_free (proto); + +if (server_ip) +grub_free (server_ip); + +if (boot_file) +grub_free (boot_file); + +return inf; +} +void grub_net_process_dhcp (struct grub_net_buff *nb,struct grub_net_card *card)
@@ -288,6 +930,70 @@ grub_net_process_dhcp (struct grub_net_buff *nb, } } +void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card __attribute__ ((unused))) +{ +const struct grub_net_dhcpv6_packet *v6; +struct grub_dhcpv6_session *session; +const struct grub_dhcpv6_option *opt_iana; +const struct grub_dhcpv6_iana_option *ia_na; +grub_size_t size; + + v6 = (const struct grub_net_dhcpv6_packet *) nb->data; + + size = nb->tail - nb->data; + + opt_iana =find_dhcpv6_option_in_packet (v6, size, GRUB_DHCPv6_OPTION_IA_NA); +if (!opt_iana) + { +grub_print_error (); +return ; + } + + ia_na = (const struct grub_dhcpv6_iana_option *)opt_iana->data; +FOR_DHCPV6_SESSIONS (session) + { +if (session->transaction_id == v6->transaction_id + && session->iaid ==grub_cpu_to_be32 (ia_na->iaid)) +break ; + } + +if (!session) +return ; + +if (v6->message_type == GRUB_DHCPv6_ADVERTISE) + { +grub_net_configure_by_dhcpv6_adv (v6, size, session); + } +else if (v6->message_type == GRUB_DHCPv6_REPLY) + { +char *name; +struct grub_net_network_level_interface *inf; + + inf = session->iface; + name =grub_xasprintf ("%s:dhcp6" , inf->card->name); +if (!name) +return ; + +grub_net_configure_by_dhcpv6_reply (name, inf->card, +0 , v6, size,0 ,0 ,0 ); + +if (!grub_errno) + { +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + } + +grub_free (name); + } + +if (grub_errno) +grub_print_error (); + +return ; +} +static char hexdigit (grub_uint8_t val) {
@@ -564,7 +1270,190 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),return err; } -static grub_command_t cmd_getdhcp, cmd_bootp; + +static grub_err_t +grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)), +int argc,char **args) +{ +struct grub_net_card *card; +unsigned j =0 ; +int interval; +grub_err_t err; +struct grub_dhcpv6_session *session; + + err =GRUB_ERR_NONE ; + +FOR_NET_CARDS (card) + { +if (argc >0 &&grub_strcmp (card->name, args[0 ]) !=0 ) +continue ; + } + +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_dhcpv6_sessions_free (); +return grub_errno; + } + + session =grub_zalloc (sizeof (*session)); + session->iface = iface; + session->iaid = j; +grub_dhcpv6_session_add (session); + j++; + } + +for (interval =200 ; interval <10000 ; interval *=2 ) + { +int done =1 ; + +FOR_DHCPV6_SESSIONS (session) + { +struct grub_net_buff *nb; +struct grub_dhcpv6_option *opt; +struct grub_net_dhcpv6_packet *v6; +struct grub_DUID_LL *duid; +struct grub_dhcpv6_iana_option *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 (session->iface, + &multicast, &ll_multicast); +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + + nb =grub_netbuff_alloc (GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb) + { +grub_dhcpv6_sessions_free (); +return grub_errno; + } + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (grub_uint16_t)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_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_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_CLIENTID); + opt->len =grub_cpu_to_be16 (sizeof (*duid)); + + duid = (struct grub_DUID_LL *) opt->data; + + duid->type =grub_cpu_to_be16_compile_time (3 ) ; + duid->hw_type =grub_cpu_to_be16_compile_time (1 ); +grub_memcpy (&duid->hwaddr, &session->iface->hwaddress.mac, +sizeof (session->iface->hwaddress.mac)); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*ia_na)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_IA_NA); + opt->len =grub_cpu_to_be16 (sizeof (*ia_na)); + ia_na = (struct grub_dhcpv6_iana_option *)opt->data; + ia_na->iaid =grub_cpu_to_be32 (session->iaid); + ia_na->t1 =0 ; + ia_na->t2 =0 ; + + err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *)nb->data; + v6->message_type =1 ; + v6->transaction_id = session->transaction_id; + +grub_netbuff_push (nb,sizeof (*udph)); + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + 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 , + &session->iface->address, &multicast); + + err =grub_net_send_ip_packet (session->iface, &multicast, + &ll_multicast, nb,GRUB_NET_IP_UDP ); + done =0 ; +grub_netbuff_free (nb); + +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + } +if (!done) +grub_net_poll_cards (interval,0 ); + } + +FOR_DHCPV6_SESSIONS (session) + { +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("couldn't autoconfigure %s" ), + session->iface->card->name); +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return err; +} + +static grub_command_t cmd_getdhcp, cmd_bootp, cmd_bootp6;void grub_bootp_init (void )
@@ -575,6 +1464,9 @@ 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
@@ -582,4 +1474,5 @@ grub_bootp_fini (void) {grub_unregister_command (cmd_getdhcp);grub_unregister_command (cmd_bootp); +grub_unregister_command (cmd_bootp6); }
grub-core/net/ip.c +35 -0
@@ -238,6 +238,41 @@ 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 (546 )) + { +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; + } +grub_net_process_dhcp6 (nb, card); +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;
include/grub/net.h +19 -0
@@ -418,6 +418,13 @@ struct grub_net_bootp_packetgrub_uint8_t vendor[0 ]; }GRUB_PACKED ; +struct grub_net_dhcpv6_packet +{ +grub_uint32_t message_type:8 ; +grub_uint32_t transaction_id:24 ; +grub_uint8_t dhcp_options[0 ]; +}GRUB_PACKED ; +#define GRUB_NET_BOOTP_RFC1048_MAGIC_0 0x63#define GRUB_NET_BOOTP_RFC1048_MAGIC_1 0x82#define GRUB_NET_BOOTP_RFC1048_MAGIC_2 0x53
@@ -444,6 +451,14 @@ grub_net_configure_by_dhcp_ack (const char *name,grub_size_t size, int is_def, char **device, char **path); +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_dhcpv6_packet *v6, +grub_size_t size, +int is_def,char **device,char **path); +grub_err_t grub_net_add_ipv4_local (struct grub_net_network_level_interface *inf,int mask);
@@ -452,6 +467,10 @@ void grub_net_process_dhcp (struct grub_net_buff *nb, struct grub_net_card *card); +void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card); +int grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,const grub_net_link_level_address_t *b);
Signature · 1 line
1.7.3.4
Re: [PATCH 1/3] Added net_bootp6 command
В Tue, 12 May 2015 16:49:48 +0800
Michael Chang <[email protected]> пишет:
Quoted 22 lines
The
net_bootp6is used to configure the ipv6 network interface through the DHCPv6 protocol Solict/Advertise/Request/Reply.--- grub-core/net/bootp.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++++- grub-core/net/ip.c | 35 ++ include/grub/net.h | 19 + 3 files changed, 948 insertions(+), 1 deletions(-)grub-core/net/bootp.c +2
-0@@ -24,6 +24,8 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> static void parse_dhcp_vendor (const char *name,const void *vend,int limit,int *mask)
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 263
Quoted 6 lines
grub-core/net/bootp.c +3
-0@@ -256,6 +258,646 @@ grub_net_configure_by_dhcp_ack (const char *name,return inter; } +struct grub_dhcpv6_option { +grub_uint16_t code; +grub_uint16_t len;
Won't do; options in packet are unaligned (see below) so you have to walk byte buffer using get_unaligned and
grub_uint8_t code[2]grub_uint8_t len[2]
Quoted 15 lines
grub-core/net/bootp.c +15
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + + +struct grub_dhcpv6_iana_option { +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_dhcpv6_iaaddr_option { +grub_uint8_t addr[16 ]; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 288
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_DUID_LL +{ +grub_uint16_t type; +grub_uint16_t hw_type; +grub_uint8_t hwaddr[6 ]; +}GRUB_PACKED ; +
Probably the same applies to all option definitions.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 292
Quoted 4 lines
grub-core/net/bootp.c +4
-0+#define GRUB_DHCPv6_ADVERTISE 2 +#define GRUB_DHCPv6_REQUEST 3 +#define GRUB_DHCPv6_REPLY 7 +
SOLICIT is missing; please enum as well.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 301
Quoted 9 lines
grub-core/net/bootp.c +9
-0+#define GRUB_DHCPv6_OPTION_CLIENTID 1 +#define GRUB_DHCPv6_OPTION_SERVERID 2 +#define GRUB_DHCPv6_OPTION_IA_NA 3 +#define GRUB_DHCPv6_OPTION_IAADDR 5 +#define GRUB_DHCPv6_OPTION_ORO 6 +#define GRUB_DHCPv6_OPTION_ELAPSED_TIME 8 +#define GRUB_DHCPv6_OPTION_DNS_SERVERS 23 +#define GRUB_DHCPv6_OPTION_BOOTFILE_URL 59 +
enum please. These can also be anonymous, no need to invent fancy names.
Quoted 26 lines
grub-core/net/bootp.c +26
-0+/* The default netbuff size for sending DHCPv6 packets which should be +large enough to hold the information */ +#define GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE 512 + +struct grub_dhcpv6_session +{ +struct grub_dhcpv6_session *next; +struct grub_dhcpv6_session **prev; +grub_uint32_t iaid; +grub_uint32_t transaction_id:24 ; +grub_uint64_t start_time; +struct grub_net_network_level_interface *iface; +}; + +static struct grub_dhcpv6_session *grub_dhcpv6_sessions =NULL ; +#define FOR_DHCPV6_SESSIONS (var) FOR_LIST_ELEMENTS (var, grub_dhcpv6_sessions) + +static void +grub_dhcpv6_session_add (struct grub_dhcpv6_session *session) +{ +struct grub_datetime date; +grub_err_t err; +grub_int32_t t =0 ; + + err =grub_get_datetime (&date); +if (err || !grub_datetime2unixtime (&date, &t))
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 337
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ { + grub_errno =GRUB_ERR_NONE ; + t =0 ; + } + + session->transaction_id = t; + session->start_time =grub_get_time_ms (); +grub_list_push (GRUB_AS_LIST_P (&grub_dhcpv6_sessions),GRUB_AS_LIST (session)); + +return ;
return is redundant
Quoted 5 lines
grub-core/net/bootp.c +5
-0+} + +static void +grub_dhcpv6_sessions_free (void ) +{
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 352
Quoted 10 lines
grub-core/net/bootp.c +10
-0+struct grub_dhcpv6_session *session; + +FOR_DHCPV6_SESSIONS (session) + { +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return ;
and here too
Quoted 25 lines
grub-core/net/bootp.c +25
-0+} + +static const char * +get_dhcpv6_option_name (grub_uint16_t option) +{ +switch (option) + { +case GRUB_DHCPv6_OPTION_BOOTFILE_URL: +return "BOOTFILE URL" ; +case GRUB_DHCPv6_OPTION_DNS_SERVERS: +return "DNS SERVERS" ; +case GRUB_DHCPv6_OPTION_IA_NA: +return "IA NA" ; +case GRUB_DHCPv6_OPTION_IAADDR: +return "IAADDR" ; +case GRUB_DHCPv6_OPTION_CLIENTID: +return "CLIENTID" ; +case GRUB_DHCPv6_OPTION_SERVERID: +return "SERVERID" ; +case GRUB_DHCPv6_OPTION_ORO: +return "ORO" ; +case GRUB_DHCPv6_OPTION_ELAPSED_TIME: +return "ELAPSED TIME" ; +default : +return "UNKNOWN" ;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 387
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ } +} + +static const struct grub_dhcpv6_option * +find_dhcpv6_option (const struct grub_dhcpv6_option *popt,grub_size_t size,grub_uint16_t option) +{ +while (size >0 ) + { +grub_uint16_t code, len; +
if (size < sizeof(*popt))
grub_error
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 390
Quoted 3 lines
grub-core/net/bootp.c +3
-0+ code =grub_be_to_cpu16 (popt->code); + len =grub_be_to_cpu16 (popt->len) +sizeof (*popt); +
According to rfc3315 - Options are byte-aligned but are not aligned in any other way such as on 2 or 4 byte boundaries; it should be grub_get_unaligned.
To avoid warnings from static checkers (possible overflow)
size -= sizeof(*popt)
len = grub_be_to_cpu...
Quoted 5 lines
grub-core/net/bootp.c +5
-0+if (size < len) + { +grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("DHCPv6 option size overflow detected" )); +return NULL ; + }
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 405
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ +if (option == code) +return popt; + +if (code ==0 ) +break ; +else + { + size -= len; + popt = (const struct grub_dhcpv6_option *)((grub_uint8_t *)popt + len);
assuming changes above - + sizeof (*popt)
Quoted 7 lines
grub-core/net/bootp.c +7
-0+ } + } + +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 Option (%u):%s not found" ), option,get_dhcpv6_option_name (option)); +return NULL ; +} +
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 422
Quoted 10 lines
grub-core/net/bootp.c +10
-0+static const struct grub_dhcpv6_option * +find_dhcpv6_option_in_packet (const struct grub_net_dhcpv6_packet *packet, +grub_size_t size,grub_uint16_t option) +{ +if (!size || !packet) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("null or zero sized DHCPv6 packet buffer" )); +return NULL ; + } +
Is it really possible? We come here from processing real packet or from EFI with fixed buffer. Just curious in case I miss something obvious.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 428
Quoted 6 lines
grub-core/net/bootp.c +6
-0+if (size <=sizeof (*packet)) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("DHCPv6 packet size too small" )); +return NULL ; + } +
You need to check size in grub_net_process_dhcp6() anyway. When you come from EFI you have fixed buffer size and packet exists so there is single entry point where size has to be verified. No need to do it every time here.
Quoted 9 lines
grub-core/net/bootp.c +9
-0+return find_dhcpv6_option ((const struct grub_dhcpv6_option *)packet->dhcp_options, + size -sizeof (*packet), option); +} + +static const grub_uint8_t * +find_dhcpv6_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size) +{ +const struct grub_dhcpv6_option * popt; +const struct grub_dhcpv6_iana_option *ia_na;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 447
Quoted 10 lines
grub-core/net/bootp.c +10
-0+const struct grub_dhcpv6_iaaddr_option *iaaddr; +grub_uint16_t ia_na_len; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_IA_NA); + +if (!popt) +return NULL ; + + ia_na = (const struct grub_dhcpv6_iana_option *) popt->data; + ia_na_len =grub_be_to_cpu16 (popt->len);
grub_get_unaligned. It makes sense to add helper for option length, it's going to be used often.
Quoted 16 lines
grub-core/net/bootp.c +16
-0+ + popt =find_dhcpv6_option ((const struct grub_dhcpv6_option *) ia_na->data, + ia_na_len -sizeof (*ia_na), GRUB_DHCPv6_OPTION_IAADDR); + +if (!popt) +return NULL ; + + iaaddr = (const struct grub_dhcpv6_iaaddr_option *)popt->data; +return iaaddr->addr; +} + +static void +get_dhcpv6_dns_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +grub_net_network_level_address_t **addr,grub_uint16_t *naddr) +{ +const struct grub_dhcpv6_option * popt;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 473
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_uint16_t len, ln; +const grub_uint8_t *pa; +grub_net_network_level_address_t *la; + +if (!addr || !naddr) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("bad argument for get_dhcpv6_dns_address" )); +return ; + } +
it is called exactly once and grub_errno is immediately reset to GRUB_ERR_NONE. So what's the point of returning error at all?
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 481
Quoted 8 lines
grub-core/net/bootp.c +8
-0+ *addr =NULL ; + *naddr =0 ; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_DNS_SERVERS); +if (!popt) +return ; + + len =grub_be_to_cpu16 (popt->len);
unaligned
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 487
Quoted 6 lines
grub-core/net/bootp.c +6
-0+if (len ==0 || len &0xf ) + { +grub_error (GRUB_ERR_IO ,N_ ("invalid dns address length" )); +return ; + } +
same question about grub_error. May be grub_debug?
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 489
Quoted 2 lines
grub-core/net/bootp.c +2
-0+ *naddr = ln = len >>4 ; + *addr = la =grub_zalloc (sizeof (grub_net_network_level_address_t) * ln);
You are overwriting it immediately, no need to zalloc
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 493
Quoted 4 lines
grub-core/net/bootp.c +4
-0+ +if (!la) + { + *addr =NULL ;
We just checked it is NULL above
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 494
Quoted 1 line
grub-core/net/bootp.c +1
-0+ *naddr =0 ;
Just move assignment after NULL check
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 506
Quoted 12 lines
grub-core/net/bootp.c +12
-0+return ; + } + +for (pa = popt->data; ln >0 ; pa +=0x10 , la++, ln--) + { + la->type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + la->ipv6[0 ] =grub_get_unaligned64 (pa); + la->ipv6[1 ] =grub_get_unaligned64 (pa +8 ); + la->option =DNS_OPTION_PREFER_IPV6 ; + } + +return ;
redundant return
Quoted 22 lines
grub-core/net/bootp.c +22
-0+} + +static void +find_dhcpv6_bootfile_url (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +char **proto,char **server_ip,char **boot_file) +{ +char *bootfile_url; +const struct grub_dhcpv6_option * opt_url; +char *ip_start, *ip_end; +char *path; +grub_size_t ip_len; +grub_uint16_t len; +const char *protos[] = {"tftp://" ,"http://" ,NULL }; +const char *pr; +int i; + +if (proto) + *proto =NULL ; + +if (server_ip) + *server_ip =NULL ; +
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 538
Quoted 10 lines
grub-core/net/bootp.c +10
-0+if (boot_file) + *boot_file =NULL ; + + opt_url =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_BOOTFILE_URL); + +if (!opt_url) +return ; + + len =grub_be_to_cpu16 (opt_url->len); +
get_unaligned
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 547
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ bootfile_url =grub_malloc (len +1 ); + +if (!bootfile_url) +return ; + +grub_memcpy (bootfile_url, opt_url->data, len); + bootfile_url[len] ='\0' ; + +for (i =0 ; (pr = *(protos + i)); ++i)
for (pr = protos; *pr; pr++)
Quoted 16 lines
grub-core/net/bootp.c +16
-0+if (grub_strncmp (bootfile_url, pr,grub_strlen (pr)) ==0 ) +break ; + +if (!pr) + { +grub_error (GRUB_ERR_IO , +N_ ("unsupported protocol, only tftp and http are supported" )); + goto cleanup; + } + + ip_start = ip_end =NULL ; + ip_start = bootfile_url +grub_strlen (pr); + +/* Follow elilo and edk2 that check for starting and ending delimiter '[..]' +in which IPv6 server address is enclosed. */ +if (*ip_start !='[' )
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 573
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ ip_start =NULL ; + else + ip_end =grub_strchr (++ip_start,']' ); + +if (!ip_start || !ip_end) + { +grub_error (GRUB_ERR_IO ,N_ ("IPv6-address not in square brackets" )); + goto cleanup; + } +
RFC5970 says "Clients that have DNS implementations SHOULD support the use of domain names in the URL" and in general string can also be valid IPv4 address, nothing restricts it to IPv6. So I do not think you should return error here, just return full string. I wish grub supported IPv6 literals so we could just skip this check entirely.
Quoted 52 lines
grub-core/net/bootp.c +52
-0+ ip_len = ip_end - ip_start; + +if (proto) + { +grub_size_t proto_len =grub_strlen (pr) -3 ; + + *proto =grub_malloc (proto_len +1 ); +if (!*proto) + goto cleanup; + +grub_memcpy (*proto, pr, proto_len); + *(*proto + proto_len) ='\0' ; + } + +if (server_ip) + { + *server_ip =grub_malloc (ip_len +1 ); + +if (!*server_ip) + goto cleanup; + +grub_memcpy (*server_ip, ip_start, ip_len); + *(*server_ip + ip_len) ='\0' ; + } + + path = ip_end +1 ; + +if (boot_file) + { + *boot_file =grub_strdup (path); + +if (!*boot_file) + goto cleanup; + } + +cleanup: + +if (bootfile_url) +grub_free (bootfile_url); + +if (grub_errno) + { +if (proto && *proto) + { +grub_free (*proto); + *proto =NULL ; + } + +if (server_ip && *server_ip) + { +grub_free (*server_ip); + *server_ip =NULL ;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 635
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ } + +if (boot_file && *boot_file) + { +grub_free (*boot_file); + *boot_file =NULL ; + } + } + +return ;
redundant return
Quoted 14 lines
grub-core/net/bootp.c +14
-0+} + + +static grub_err_t +grub_net_configure_by_dhcpv6_adv (const struct grub_net_dhcpv6_packet *v6_adv, +grub_size_t size, +struct grub_dhcpv6_session *session) +{ +struct grub_net_buff *nb; +const struct grub_dhcpv6_option *opt_client, *opt_server, *opt_iana; +struct grub_dhcpv6_option *popt; +struct grub_net_dhcpv6_packet *v6; +struct udphdr *udph; +grub_net_network_level_address_t multicast;
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 659
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_net_link_level_address_t ll_multicast; +struct grub_net_network_level_interface *inf; +grub_uint16_t len; +grub_uint64_t elapsed; +grub_err_t err =GRUB_ERR_NONE ; + + opt_client =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_CLIENTID); +if (grub_errno) +grub_error_push (); +
Should not we check that is it actually one of our sessions? From RFC3315 validation
- the contents of the Client Identifier option does not match the client's DUID.
- the "transaction-id" field value does not match the value the client used in its Solicit message.
transaction ID is checked earlier, we also need to check client ID somewhere
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 671
Quoted 12 lines
grub-core/net/bootp.c +12
-0+ opt_server =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_SERVERID); +if (grub_errno) +grub_error_push (); + + opt_iana =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_IA_NA); +if (grub_errno) +grub_error_push (); + +grub_error_pop (); +if (grub_errno) +return grub_errno; +
Do we really need this complication? Why not just return on the first missing option?
Quoted 13 lines
grub-core/net/bootp.c +13
-0+ inf = session->iface; + + 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_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb)
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 694
Quoted 10 lines
grub-core/net/bootp.c +10
-0+return grub_errno; + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + len =grub_cpu_to_be16 (opt_client->len);
get_unaligned, grub_be_to_cpu16
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 703
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_client)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_client, len +sizeof (*opt_client)); + + len =grub_cpu_to_be16 (opt_server->len);
ditto
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 712
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_server)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_server, len +sizeof (*opt_server)); + + len =grub_cpu_to_be16 (opt_iana->len);
ditto
Quoted 7 lines
grub-core/net/bootp.c +7
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_iana)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_iana, len +sizeof (*opt_iana));
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 729
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ + err =grub_netbuff_push (nb,sizeof (*popt) +2 *sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ORO);
From here it starts to be interesting. At least server ID option is out of our control so everything after it may be unaligned.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 730
Quoted 1 line
grub-core/net/bootp.c +1
-0+ popt->len =grub_cpu_to_be16_compile_time (2 *sizeof (grub_uint16_t));
unaligned?
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 743
Quoted 13 lines
grub-core/net/bootp.c +13
-0+grub_set_unaligned16 (popt->data,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_BOOTFILE_URL)); +grub_set_unaligned16 (popt->data +2 ,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_DNS_SERVERS)); + + err =grub_netbuff_push (nb,sizeof (*popt) +sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ELAPSED_TIME); + popt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t)); +
unaligned?
Quoted 8 lines
grub-core/net/bootp.c +8
-0+/* the time is expressed in hundredths of a second */ + elapsed =grub_divmod64 (grub_get_time_ms () - session->start_time,10 ,0 ); + +if (elapsed >0xffff ) + elapsed =0xffff ; + +grub_set_unaligned16 (popt->data,grub_cpu_to_be16 ((grub_uint16_t )elapsed)); +
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 761
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *) nb->data; + v6->message_type = GRUB_DHCPv6_REQUEST; + v6->transaction_id = v6_adv->transaction_id;
Not sure how compiler handles alignment in this case.
Quoted 4 lines
grub-core/net/bootp.c +4
-0+ + err =grub_netbuff_push (nb,sizeof (*udph)); +if (err) + {
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 775
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_netbuff_free (nb); +return err; + } + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + udph->chksum =0 ; + udph->len =grub_cpu_to_be16 (nb->tail - nb->data); +
And even here header may be unaligned after starting from server ID option. I suggest to do it differently - compute total length, reserve aligned space and make server ID last to make sure everything before it is properly aligned. And add comment explaining it :)
Quoted 43 lines
grub-core/net/bootp.c +43
-0+ 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_dhcpv6_packet *v6, +grub_size_t size, +int is_def, +char **device,char **path) +{ +grub_net_network_level_address_t addr; +grub_net_network_level_netaddress_t netaddr; +struct grub_net_network_level_interface *inf; +const grub_uint8_t *your_ip; +char *proto; +char *server_ip; +char *boot_file; +grub_net_network_level_address_t *dns; +grub_uint16_t num_dns; + +if (device) + *device =NULL ; + +if (path) + *path =NULL ; + +if (v6->message_type != GRUB_DHCPv6_REPLY) + { +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 info not found" )); +return NULL ; + } +
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 828
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ your_ip =find_dhcpv6_address (v6, size); + +if (!your_ip) +return NULL ; + + addr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + addr.ipv6[0 ] =grub_get_unaligned64 (your_ip); + addr.ipv6[1 ] =grub_get_unaligned64 (your_ip +8 ); + inf =grub_net_add_addr (name, card, &addr, &card->default_address, flags); +
NULL check. It is not clear if it makes sense to continue if we failed to actually set address at this point.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 833
Quoted 5 lines
grub-core/net/bootp.c +5
-0+ netaddr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + netaddr.ipv6.base[0 ] =grub_get_unaligned64 (your_ip); + netaddr.ipv6.base[1 ] =0 ; + netaddr.ipv6.masksize =64 ; +grub_net_add_route (name, netaddr, inf);
This will crash in net_ls_routes later if inf was NULL. Yes, I know that it is used this way currently, but that needs fixing in other places as well.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 839
Quoted 6 lines
grub-core/net/bootp.c +6
-0+ +get_dhcpv6_dns_address (v6, size, &dns, &num_dns); + +if (grub_errno) + grub_errno =GRUB_ERR_NONE ; +
This ignores legitimate errors like out of memory. As mentioned above, does it need to set any grub_errno on its own? If not, errors should not be ignored.
Quoted 12 lines
grub-core/net/bootp.c +12
-0+if (dns && num_dns) + { +int i; + +for (i =0 ; i < num_dns; ++i) +grub_net_add_dns_server (dns + i); + +grub_free (dns); + } + +find_dhcpv6_bootfile_url (v6, size, &proto, &server_ip, &boot_file); +
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 861
Quoted 10 lines
grub-core/net/bootp.c +10
-0+if (grub_errno) +grub_print_error (); + +if (boot_file) +grub_env_set_net_property (name,"boot_file" , boot_file, +grub_strlen (boot_file)); + +if (is_def && server_ip) + { + grub_net_default_server =grub_strdup (server_ip);
NULL check
Quoted 50 lines
grub-core/net/bootp.c +43
-0+grub_env_set ("net_default_interface" , name); +grub_env_export ("net_default_interface" ); + } + +if (device && server_ip && proto) + { + *device =grub_xasprintf ("%s,%s" , proto, server_ip); +if (!*device) +grub_print_error (); + } + +if (path && boot_file) + { + *path =grub_strdup (boot_file); +if (*path) + { +char *slash; + slash =grub_strrchr (*path,'/' ); +if (slash) + *slash =0 ; +else + **path =0 ; + } +else +grub_print_error (); + } + +if (proto) +grub_free (proto); + +if (server_ip) +grub_free (server_ip); + +if (boot_file) +grub_free (boot_file); + +return inf; +} +void grub_net_process_dhcp (struct grub_net_buff *nb,struct grub_net_card *card)@@ -288,6 +930,70 @@ grub_net_process_dhcp (struct grub_net_buff *nb, } } +void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card __attribute__ ((unused))) +{
grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb, · line 946
Quoted 10 lines
grub-core/net/bootp.c +10
-0+const struct grub_net_dhcpv6_packet *v6; +struct grub_dhcpv6_session *session; +const struct grub_dhcpv6_option *opt_iana; +const struct grub_dhcpv6_iana_option *ia_na; +grub_size_t size; + + v6 = (const struct grub_net_dhcpv6_packet *) nb->data; + + size = nb->tail - nb->data; +
Size check here looks like the most natural place.
grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb, · line 958
Quoted 12 lines
grub-core/net/bootp.c +12
-0+ opt_iana =find_dhcpv6_option_in_packet (v6, size, GRUB_DHCPv6_OPTION_IA_NA); +if (!opt_iana) + { +grub_print_error (); +return ; + } + + ia_na = (const struct grub_dhcpv6_iana_option *)opt_iana->data; +FOR_DHCPV6_SESSIONS (session) + { +if (session->transaction_id == v6->transaction_id + && session->iaid ==grub_cpu_to_be32 (ia_na->iaid))
get_unaligned
Quoted 55 lines
grub-core/net/bootp.c +47
-1+break ; + } + +if (!session) +return ; + +if (v6->message_type == GRUB_DHCPv6_ADVERTISE) + { +grub_net_configure_by_dhcpv6_adv (v6, size, session); + } +else if (v6->message_type == GRUB_DHCPv6_REPLY) + { +char *name; +struct grub_net_network_level_interface *inf; + + inf = session->iface; + name =grub_xasprintf ("%s:dhcp6" , inf->card->name); +if (!name) +return ; + +grub_net_configure_by_dhcpv6_reply (name, inf->card, +0 , v6, size,0 ,0 ,0 ); + +if (!grub_errno) + { +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + } + +grub_free (name); + } + +if (grub_errno) +grub_print_error (); + +return ; +} +static char hexdigit (grub_uint8_t val) {@@ -564,7 +1270,190 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),return err; } -static grub_command_t cmd_getdhcp, cmd_bootp; + +static grub_err_t +grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)), +int argc,char **args) +{ +struct grub_net_card *card; +unsigned j =0 ; +int interval; +grub_err_t err;
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1291
Quoted 10 lines
grub-core/net/bootp.c +10
-0+struct grub_dhcpv6_session *session; + + err =GRUB_ERR_NONE ; + +FOR_NET_CARDS (card) + { +if (argc >0 &&grub_strcmp (card->name, args[0 ]) !=0 ) +continue ; + } +
What is the goal of this loop?
Quoted 12 lines
grub-core/net/bootp.c +12
-0+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_dhcpv6_sessions_free (); +return grub_errno;
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1313
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ } + + session =grub_zalloc (sizeof (*session)); + session->iface = iface; + session->iaid = j; +grub_dhcpv6_session_add (session); + j++; + } + +for (interval =200 ; interval <10000 ; interval *=2 )
In general that does not follow RFC3315 required client behavior at all. But we need at least something that can be improved later.
Quoted 43 lines
grub-core/net/bootp.c +43
-0+ { +int done =1 ; + +FOR_DHCPV6_SESSIONS (session) + { +struct grub_net_buff *nb; +struct grub_dhcpv6_option *opt; +struct grub_net_dhcpv6_packet *v6; +struct grub_DUID_LL *duid; +struct grub_dhcpv6_iana_option *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 (session->iface, + &multicast, &ll_multicast); +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + + nb =grub_netbuff_alloc (GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb) + { +grub_dhcpv6_sessions_free (); +return grub_errno; + } + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (grub_uint16_t));
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1366
Quoted 10 lines
grub-core/net/bootp.c +10
-0+if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ELAPSED_TIME); + opt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
As far as I can tell, we remain 2 bytes aligned here. But we need to change prototype to account for possible unaligned values on input, it likely needs change through the whole function.
Quoted 24 lines
grub-core/net/bootp.c +24
-0+grub_set_unaligned16 (opt->data,0 ); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*duid)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_CLIENTID); + opt->len =grub_cpu_to_be16 (sizeof (*duid)); + + duid = (struct grub_DUID_LL *) opt->data; + + duid->type =grub_cpu_to_be16_compile_time (3 ) ; + duid->hw_type =grub_cpu_to_be16_compile_time (1 ); +grub_memcpy (&duid->hwaddr, &session->iface->hwaddress.mac, +sizeof (session->iface->hwaddress.mac)); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*ia_na)); +if (err) + {
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1400
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_IA_NA); + opt->len =grub_cpu_to_be16 (sizeof (*ia_na)); + ia_na = (struct grub_dhcpv6_iana_option *)opt->data; + ia_na->iaid =grub_cpu_to_be32 (session->iaid);
Are you sure we are still aligned to 4 bytes here? You go from top to bottom and some fields are not multiple of 4 bytes. As mentioned earlier, may be ensure buffer is aligned in advance.
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1413
Quoted 13 lines
grub-core/net/bootp.c +13
-0+ ia_na->t1 =0 ; + ia_na->t2 =0 ; + + err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *)nb->data; + v6->message_type =1 ;
no magic constants, please.
Quoted 152 lines
grub-core/net/bootp.c +47
-0+ v6->transaction_id = session->transaction_id; + +grub_netbuff_push (nb,sizeof (*udph)); + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + 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 , + &session->iface->address, &multicast); + + err =grub_net_send_ip_packet (session->iface, &multicast, + &ll_multicast, nb,GRUB_NET_IP_UDP ); + done =0 ; +grub_netbuff_free (nb); + +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + } +if (!done) +grub_net_poll_cards (interval,0 ); + } + +FOR_DHCPV6_SESSIONS (session) + { +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("couldn't autoconfigure %s" ), + session->iface->card->name); +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return err; +} + +static grub_command_t cmd_getdhcp, cmd_bootp, cmd_bootp6;void grub_bootp_init (void )@@ -575,6 +1464,9 @@ 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 @@ -582,4 +1474,5 @@ grub_bootp_fini (void) {grub_unregister_command (cmd_getdhcp);grub_unregister_command (cmd_bootp); +grub_unregister_command (cmd_bootp6); }grub-core/net/ip.c +35
-0@@ -238,6 +238,41 @@ 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 (546 )) + { +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; + } +grub_net_process_dhcp6 (nb, card); +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;include/grub/net.h +19
-0@@ -418,6 +418,13 @@ struct grub_net_bootp_packetgrub_uint8_t vendor[0 ]; }GRUB_PACKED ; +struct grub_net_dhcpv6_packet +{ +grub_uint32_t message_type:8 ; +grub_uint32_t transaction_id:24 ; +grub_uint8_t dhcp_options[0 ]; +}GRUB_PACKED ; +#define GRUB_NET_BOOTP_RFC1048_MAGIC_0 0x63#define GRUB_NET_BOOTP_RFC1048_MAGIC_1 0x82#define GRUB_NET_BOOTP_RFC1048_MAGIC_2 0x53@@ -444,6 +451,14 @@ grub_net_configure_by_dhcp_ack (const char *name,grub_size_t size, int is_def, char **device, char **path); +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_dhcpv6_packet *v6, +grub_size_t size, +int is_def,char **device,char **path); +grub_err_t grub_net_add_ipv4_local (struct grub_net_network_level_interface *inf,int mask);@@ -452,6 +467,10 @@ void grub_net_process_dhcp (struct grub_net_buff *nb, struct grub_net_card *card); +void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card); +int grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,const grub_net_link_level_address_t *b);
В Tue, 12 May 2015 16:49:47 +0800
Michael Chang <[email protected]> пишет:
Quoted 5 lines
This patch set tries to make support of configuring IPv6 network interface through the DHCPv6 protocol. A new command,
net_bootp6, is therefore introduced to serve the purpose. You can think it as DHCPv6/IPv6 version of the existingnet_bootpcommand.
Note that spec actually suggests (requires?) that DHCPv6 transaction is triggered by receiving Router Advertisement with M/O bits set.
Quoted 4 lines
In addition to that, the UEFI IPv6 PXE support can be easily done by sharing the same routine with
net_bootp6to parse DHCPv6 Reply packet cached in the firmware.
Looking in EFI spec I do not see it explaining what content this dhcp_ack packet has for IPv6. I mean, DHCPv6 has neither Discover not Acknowledge, and full DHCPv6 transaction requires *two* packets - one for getting address and one for getting other options (DNS/boot server). Let's see how it works in real world :)
Quoted 15 lines
changes in v1:
- Added upper boundary check infind_dhcpv6_option
- Fix memory leak and freeing NULL pointer
- Improved error message logging to not get lost
- Usegrub_cpu_to_be16_compile_timefor endianess conversion when appropriate
- Removedgrub_dhcpv6_dns_serversstructure and use 16 bytes blocks
- Avoud magic numbers and use more descriptive sizeof when populating netbuff
- Moveinclude/grub/efi/api.hto UEFI IPv6 PXE support patch
- Document thenet_bootp6command_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/3] Added net_bootp6 command
In reply to Andrei BorzenkovQuoted 28 lines · Andrei Borzenkov
В Tue, 12 May 2015 16:49:48 +0800
Michael Chang <[email protected]> пишет:Quoted 25 lines
The
net_bootp6is used to configure the ipv6 network interface through the DHCPv6 protocol Solict/Advertise/Request/Reply.--- grub-core/net/bootp.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++++- grub-core/net/ip.c | 35 ++ include/grub/net.h | 19 + 3 files changed, 948 insertions(+), 1 deletions(-)grub-core/net/bootp.c +2
-0@@ -24,6 +24,8 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> static void parse_dhcp_vendor (const char *name,const void *vend,int limit,int *mask)@@ -256,6 +258,646 @@ grub_net_configure_by_dhcp_ack (const char *name,return inter; }
Quoted 6 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 263
Quoted 3 lines
grub-core/net/bootp.c +3
-0+struct grub_dhcpv6_option { +grub_uint16_t code; +grub_uint16_t len;Won't do; options in packet are unaligned (see below) so you have to walk byte buffer using
get_unalignedand
I don't get it. Wouldn't the structure attribute GRUB_PACKED do the trick for us?
Quoted 23 lines
grub_uint8_tcode[2]grub_uint8_tlen[2]Quoted 19 lines
grub-core/net/bootp.c +19
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + + +struct grub_dhcpv6_iana_option { +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_dhcpv6_iaaddr_option { +grub_uint8_t addr[16 ]; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_DUID_LL
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 288
Quoted 6 lines
grub-core/net/bootp.c +6
-0+{ +grub_uint16_t type; +grub_uint16_t hw_type; +grub_uint8_t hwaddr[6 ]; +}GRUB_PACKED ; +Probably the same applies to all option definitions.
That would require quite a lot of rewritting basically. (So I'd like it to be clear to me in the first).
Thanks,
Michael
Quoted 11 lines · Andrei Borzenkov
В Tue, 12 May 2015 16:49:47 +0800
Michael Chang <[email protected]> пишет:Quoted 5 lines
This patch set tries to make support of configuring IPv6 network interface through the DHCPv6 protocol. A new command,
net_bootp6, is therefore introduced to serve the purpose. You can think it as DHCPv6/IPv6 version of the existingnet_bootpcommand.Note that spec actually suggests (requires?) that DHCPv6 transaction is triggered by receiving Router Advertisement with M/O bits set.
I know that. But UEFI IPv6 PXE booting just works without it. We can still add it in the future if it is really mandatory.
Quoted 11 lines
Quoted 5 lines
In addition to that, the UEFI IPv6 PXE support can be easily done by sharing the same routine with
net_bootp6to parse DHCPv6 Reply packet cached in the firmware.Looking in EFI spec I do not see it explaining what content this
dhcp_ackpacket has for IPv6. I mean, DHCPv6 has neither Discover not Acknowledge, and full DHCPv6 transaction requires *two* packets - one for getting address and one for getting other options (DNS/boot server). Let's see how it works in real world :)
Yes, I also noticed the problem. But In real world, it has been tested for a while, as that's the same with how elilo handles it. That is treating dhcp_ack packet as a single DHCPv6 reply packet and parse the options in it.
Thanks,
Michael
Quoted 17 lines
Quoted 16 lines
changes in v1:
- Added upper boundary check infind_dhcpv6_option
- Fix memory leak and freeing NULL pointer
- Improved error message logging to not get lost
- Usegrub_cpu_to_be16_compile_timefor endianess conversion when appropriate
- Removedgrub_dhcpv6_dns_serversstructure and use 16 bytes blocks
- Avoud magic numbers and use more descriptive sizeof when populating netbuff
- Moveinclude/grub/efi/api.hto UEFI IPv6 PXE support patch
- Document thenet_bootp6command_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/3] Added net_bootp6 command
In reply to Michael ChangВ Fri, 15 May 2015 21:57:28 +0800
Michael Chang <[email protected]> пишет:
Quoted 36 lines · Andrei Borzenkov
Quoted 28 lines
В Tue, 12 May 2015 16:49:48 +0800
Michael Chang <[email protected]> пишет:Quoted 25 lines
The
net_bootp6is used to configure the ipv6 network interface through the DHCPv6 protocol Solict/Advertise/Request/Reply.--- grub-core/net/bootp.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++++- grub-core/net/ip.c | 35 ++ include/grub/net.h | 19 + 3 files changed, 948 insertions(+), 1 deletions(-)grub-core/net/bootp.c +2
-0@@ -24,6 +24,8 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> static void parse_dhcp_vendor (const char *name,const void *vend,int limit,int *mask)@@ -256,6 +258,646 @@ grub_net_configure_by_dhcp_ack (const char *name,return inter; }Quoted 6 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 263
Quoted 3 lines
grub-core/net/bootp.c +3
-0+struct grub_dhcpv6_option { +grub_uint16_t code; +grub_uint16_t len;Won't do; options in packet are unaligned (see below) so you have to walk byte buffer using
get_unalignedand
Quoted 3 lines
I don't get it. Wouldn't the structure attribute
GRUB_PACKEDdo the trick for us?
Yes, you are right. I apologize for confusion.
Sorry :(
Quoted 35 lines
Quoted 23 lines
grub_uint8_tcode[2]grub_uint8_tlen[2]Quoted 19 lines
grub-core/net/bootp.c +19
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + + +struct grub_dhcpv6_iana_option { +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_dhcpv6_iaaddr_option { +grub_uint8_t addr[16 ]; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_DUID_LL Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 288
Quoted 6 lines
grub-core/net/bootp.c +6
-0+{ +grub_uint16_t type; +grub_uint16_t hw_type; +grub_uint8_t hwaddr[6 ]; +}GRUB_PACKED ; +Probably the same applies to all option definitions.
That would require quite a lot of rewritting basically. (So I'd like it to be clear to me in the first).
Re: [PATCH 1/3] Added net_bootp6 command
In reply to Andrei BorzenkovHi Andrey,
Please see my inline comments. And if I have left anything un-answered or not clear please let me know.
And thanks a lot to your time and effort for doing the review.
Thanks,
Michael
Quoted 66 lines · Andrei Borzenkov
В Tue, 12 May 2015 16:49:48 +0800
Michael Chang <[email protected]> пишет:Quoted 22 lines
The
net_bootp6is used to configure the ipv6 network interface through the DHCPv6 protocol Solict/Advertise/Request/Reply.--- grub-core/net/bootp.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++++- grub-core/net/ip.c | 35 ++ include/grub/net.h | 19 + 3 files changed, 948 insertions(+), 1 deletions(-)grub-core/net/bootp.c +2
-0@@ -24,6 +24,8 @@#include <grub/net/netbuff.h> #include <grub/net/udp.h> #include <grub/datetime.h> +#include <grub/time.h> +#include <grub/list.h> static void parse_dhcp_vendor (const char *name,const void *vend,int limit,int *mask)grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 263
Quoted 6 lines
grub-core/net/bootp.c +3
-0@@ -256,6 +258,646 @@ grub_net_configure_by_dhcp_ack (const char *name,return inter; } +struct grub_dhcpv6_option { +grub_uint16_t code; +grub_uint16_t len;Won't do; options in packet are unaligned (see below) so you have to walk byte buffer using
get_unalignedand
grub_uint8_tcode[2]grub_uint8_tlen[2]Quoted 15 lines
grub-core/net/bootp.c +15
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + + +struct grub_dhcpv6_iana_option { +grub_uint32_t iaid; +grub_uint32_t t1; +grub_uint32_t t2; +grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_dhcpv6_iaaddr_option { +grub_uint8_t addr[16 ]; +grub_uint32_t preferred_lifetime; +grub_uint32_t valid_lifetime;grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 288
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_uint8_t data[0 ]; +}GRUB_PACKED ; + +struct grub_DUID_LL +{ +grub_uint16_t type; +grub_uint16_t hw_type; +grub_uint8_t hwaddr[6 ]; +}GRUB_PACKED ; +Probably the same applies to all option definitions.
Quoted 6 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 292
Quoted 4 lines
grub-core/net/bootp.c +4
-0+#define GRUB_DHCPv6_ADVERTISE 2 +#define GRUB_DHCPv6_REQUEST 3 +#define GRUB_DHCPv6_REPLY 7 +SOLICIT is missing; please enum as well.
OK.
Quoted 13 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 301
Quoted 10 lines
grub-core/net/bootp.c +9
-0+#define GRUB_DHCPv6_OPTION_CLIENTID 1 +#define GRUB_DHCPv6_OPTION_SERVERID 2 +#define GRUB_DHCPv6_OPTION_IA_NA 3 +#define GRUB_DHCPv6_OPTION_IAADDR 5 +#define GRUB_DHCPv6_OPTION_ORO 6 +#define GRUB_DHCPv6_OPTION_ELAPSED_TIME 8 +#define GRUB_DHCPv6_OPTION_DNS_SERVERS 23 +#define GRUB_DHCPv6_OPTION_BOOTFILE_URL 59 +enum please. These can also be anonymous, no need to invent fancy names.
I don't understand. Do you want me to use enum type here rather than macro ? Where are the fancy names invented here ? (All of the listed maros have it's name taken from spec, and the prefix according to your previous reviews).
Quoted 31 lines
Quoted 31 lines
grub-core/net/bootp.c +30
-0+/* The default netbuff size for sending DHCPv6 packets which should be +large enough to hold the information */ +#define GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE 512 + +struct grub_dhcpv6_session +{ +struct grub_dhcpv6_session *next; +struct grub_dhcpv6_session **prev; +grub_uint32_t iaid; +grub_uint32_t transaction_id:24 ; +grub_uint64_t start_time; +struct grub_net_network_level_interface *iface; +}; + +static struct grub_dhcpv6_session *grub_dhcpv6_sessions =NULL ; +#define FOR_DHCPV6_SESSIONS (var) FOR_LIST_ELEMENTS (var, grub_dhcpv6_sessions) + +static void +grub_dhcpv6_session_add (struct grub_dhcpv6_session *session) +{ +struct grub_datetime date; +grub_err_t err; +grub_int32_t t =0 ; + + err =grub_get_datetime (&date); +if (err || !grub_datetime2unixtime (&date, &t)) + { + grub_errno =GRUB_ERR_NONE ; + t =0 ; + }
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 337
Quoted 6 lines
grub-core/net/bootp.c +6
-0+ + session->transaction_id = t; + session->start_time =grub_get_time_ms (); +grub_list_push (GRUB_AS_LIST_P (&grub_dhcpv6_sessions),GRUB_AS_LIST (session)); + +return ;return is redundant
OK.
Quoted 10 lines
Quoted 10 lines
grub-core/net/bootp.c +9
-0+} + +static void +grub_dhcpv6_sessions_free (void ) +{ +struct grub_dhcpv6_session *session; + +FOR_DHCPV6_SESSIONS (session) + {
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 352
Quoted 6 lines
grub-core/net/bootp.c +6
-0+grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return ;and here too
OK.
Quoted 31 lines
Quoted 31 lines
grub-core/net/bootp.c +30
-0+} + +static const char * +get_dhcpv6_option_name (grub_uint16_t option) +{ +switch (option) + { +case GRUB_DHCPv6_OPTION_BOOTFILE_URL: +return "BOOTFILE URL" ; +case GRUB_DHCPv6_OPTION_DNS_SERVERS: +return "DNS SERVERS" ; +case GRUB_DHCPv6_OPTION_IA_NA: +return "IA NA" ; +case GRUB_DHCPv6_OPTION_IAADDR: +return "IAADDR" ; +case GRUB_DHCPv6_OPTION_CLIENTID: +return "CLIENTID" ; +case GRUB_DHCPv6_OPTION_SERVERID: +return "SERVERID" ; +case GRUB_DHCPv6_OPTION_ORO: +return "ORO" ; +case GRUB_DHCPv6_OPTION_ELAPSED_TIME: +return "ELAPSED TIME" ; +default : +return "UNKNOWN" ; + } +} + +static const struct grub_dhcpv6_option * +find_dhcpv6_option (const struct grub_dhcpv6_option *popt,grub_size_t size,grub_uint16_t option)
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 387
Quoted 5 lines
grub-core/net/bootp.c +5
-0+{ +while (size >0 ) + { +grub_uint16_t code, len; +if (size <
sizeof(*popt))
grub_error
Nice catch. Thanks.
Quoted 5 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 390
Quoted 4 lines
grub-core/net/bootp.c +3
-0+ code =grub_be_to_cpu16 (popt->code); + len =grub_be_to_cpu16 (popt->len) +sizeof (*popt); +
Quoted 8 lines
According to rfc3315 - Options are byte-aligned but are not aligned in any other way such as on 2 or 4 byte boundaries; it should be
grub_get_unaligned.To avoid warnings from static checkers (possible overflow)
size -=
sizeof(*popt)
len =grub_be_to_cpu...
OK.
Quoted 10 lines
Quoted 10 lines
grub-core/net/bootp.c +9
-0+if (size < len) + { +grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("DHCPv6 option size overflow detected" )); +return NULL ; + } + +if (option == code) +return popt; +
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 405
Quoted 6 lines
grub-core/net/bootp.c +6
-0+if (code ==0 ) +break ; +else + { + size -= len; + popt = (const struct grub_dhcpv6_option *)((grub_uint8_t *)popt + len);assuming changes above - + sizeof (*popt)
OK.
Quoted 14 lines
Quoted 14 lines
grub-core/net/bootp.c +13
-0+ } + } + +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 Option (%u):%s not found" ), option,get_dhcpv6_option_name (option)); +return NULL ; +} + +static const struct grub_dhcpv6_option * +find_dhcpv6_option_in_packet (const struct grub_net_dhcpv6_packet *packet, +grub_size_t size,grub_uint16_t option) +{ +if (!size || !packet) + {
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 422
Quoted 4 lines
grub-core/net/bootp.c +4
-0+grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("null or zero sized DHCPv6 packet buffer" )); +return NULL ; + } +Is it really possible? We come here from processing real packet or from EFI with fixed buffer. Just curious in case I miss something obvious.
In general, a function should check it's input before processing them, that's why these checks were added.
With the understanding it's in static scope with a few known callers, probably these check are not doing anything so that I'm fine to remove them.
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 428
Quoted 7 lines
grub-core/net/bootp.c +6
-0+if (size <=sizeof (*packet)) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("DHCPv6 packet size too small" )); +return NULL ; + } +
Quoted 4 lines
You need to check size in
grub_net_process_dhcp6()anyway. When you come from EFI you have fixed buffer size and packet exists so there is single entry point where size has to be verified. No need to do it every time here.
The reason is pretty much the same with above, if you don't like redundant checks I can remove them.
Quoted 45 lines
Quoted 10 lines
grub-core/net/bootp.c +9
-0+return find_dhcpv6_option ((const struct grub_dhcpv6_option *)packet->dhcp_options, + size -sizeof (*packet), option); +} + +static const grub_uint8_t * +find_dhcpv6_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size) +{ +const struct grub_dhcpv6_option * popt; +const struct grub_dhcpv6_iana_option *ia_na;grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 447
Quoted 10 lines
grub-core/net/bootp.c +10
-0+const struct grub_dhcpv6_iaaddr_option *iaaddr; +grub_uint16_t ia_na_len; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_IA_NA); + +if (!popt) +return NULL ; + + ia_na = (const struct grub_dhcpv6_iana_option *) popt->data; + ia_na_len =grub_be_to_cpu16 (popt->len);
grub_get_unaligned. It makes sense to add helper for option length, it's going to be used often.Quoted 21 lines
grub-core/net/bootp.c +21
-0+ + popt =find_dhcpv6_option ((const struct grub_dhcpv6_option *) ia_na->data, + ia_na_len -sizeof (*ia_na), GRUB_DHCPv6_OPTION_IAADDR); + +if (!popt) +return NULL ; + + iaaddr = (const struct grub_dhcpv6_iaaddr_option *)popt->data; +return iaaddr->addr; +} + +static void +get_dhcpv6_dns_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +grub_net_network_level_address_t **addr,grub_uint16_t *naddr) +{ +const struct grub_dhcpv6_option * popt; +grub_uint16_t len, ln; +const grub_uint8_t *pa; +grub_net_network_level_address_t *la; + +if (!addr || !naddr)
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 473
Quoted 5 lines
grub-core/net/bootp.c +5
-0+ { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("bad argument for get_dhcpv6_dns_address" )); +return ; + } +it is called exactly once and
grub_errnois immediately reset toGRUB_ERR_NONE. So what's the point of returning error at all?
Because a function wants to report error for bad inputs, while it's caller wants to suppress the error message because dns information could be missing in packets and is allowed (not treated as error).
Quoted 12 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 481
Quoted 9 lines
grub-core/net/bootp.c +8
-0+ *addr =NULL ; + *naddr =0 ; + + popt =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_DNS_SERVERS); +if (!popt) +return ; + + len =grub_be_to_cpu16 (popt->len);unaligned
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 487
Quoted 6 lines
grub-core/net/bootp.c +6
-0+if (len ==0 || len &0xf ) + { +grub_error (GRUB_ERR_IO ,N_ ("invalid dns address length" )); +return ; + } +same question about
grub_error. May begrub_debug?
No grub_debug, maybe grub_dprintf ? Maybe we should keep it and use specific error number for option not found ? See below for details.
Quoted 5 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 489
Quoted 3 lines
grub-core/net/bootp.c +2
-0+ *naddr = ln = len >>4 ; + *addr = la =grub_zalloc (sizeof (grub_net_network_level_address_t) * ln);You are overwriting it immediately, no need to zalloc
OK.
Quoted 6 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 493
Quoted 5 lines
grub-core/net/bootp.c +4
-0+ +if (!la) + { + *addr =NULL ;We just checked it is NULL above
OK.
Quoted 4 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 494
Quoted 2 lines
grub-core/net/bootp.c +1
-0+ *naddr =0 ;Just move assignment after NULL check
OK.
Quoted 15 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 506
Quoted 13 lines
grub-core/net/bootp.c +12
-0+return ; + } + +for (pa = popt->data; ln >0 ; pa +=0x10 , la++, ln--) + { + la->type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + la->ipv6[0 ] =grub_get_unaligned64 (pa); + la->ipv6[1 ] =grub_get_unaligned64 (pa +8 ); + la->option =DNS_OPTION_PREFER_IPV6 ; + } + +return ;redundant return
OK.
Quoted 36 lines
Quoted 23 lines
grub-core/net/bootp.c +22
-0+} + +static void +find_dhcpv6_bootfile_url (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +char **proto,char **server_ip,char **boot_file) +{ +char *bootfile_url; +const struct grub_dhcpv6_option * opt_url; +char *ip_start, *ip_end; +char *path; +grub_size_t ip_len; +grub_uint16_t len; +const char *protos[] = {"tftp://" ,"http://" ,NULL }; +const char *pr; +int i; + +if (proto) + *proto =NULL ; + +if (server_ip) + *server_ip =NULL ; +grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 538
Quoted 10 lines
grub-core/net/bootp.c +10
-0+if (boot_file) + *boot_file =NULL ; + + opt_url =find_dhcpv6_option_in_packet (packet, size, GRUB_DHCPv6_OPTION_BOOTFILE_URL); + +if (!opt_url) +return ; + + len =grub_be_to_cpu16 (opt_url->len); +
get_unaligned
Quoted 10 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 547
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ bootfile_url =grub_malloc (len +1 ); + +if (!bootfile_url) +return ; + +grub_memcpy (bootfile_url, opt_url->data, len); + bootfile_url[len] ='\0' ; + +for (i =0 ; (pr = *(protos + i)); ++i)for (pr = protos; *pr; pr++)
OK.
Quoted 23 lines
Quoted 23 lines
grub-core/net/bootp.c +22
-0+if (grub_strncmp (bootfile_url, pr,grub_strlen (pr)) ==0 ) +break ; + +if (!pr) + { +grub_error (GRUB_ERR_IO , +N_ ("unsupported protocol, only tftp and http are supported" )); + goto cleanup; + } + + ip_start = ip_end =NULL ; + ip_start = bootfile_url +grub_strlen (pr); + +/* Follow elilo and edk2 that check for starting and ending delimiter '[..]' +in which IPv6 server address is enclosed. */ +if (*ip_start !='[' ) + ip_start =NULL ; +else + ip_end =grub_strchr (++ip_start,']' ); + +if (!ip_start || !ip_end) + {
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 573
Quoted 4 lines
grub-core/net/bootp.c +4
-0+grub_error (GRUB_ERR_IO ,N_ ("IPv6-address not in square brackets" )); + goto cleanup; + } +RFC5970 says "Clients that have DNS implementations SHOULD support the use of domain names in the URL" and in general string can also be valid IPv4 address, nothing restricts it to IPv6.
But the descriptions preceding it did explicitly say IPv6:
" If the host in the URL is expressed using an IPv6 address rather than a domain name, the address in the URL then MUST be enclosed in "[" and "]" characters, conforming to [RFC3986]. "
Quoted 3 lines
So I do not think you
should return error here, just return full string. I wish grub supported IPv6 literals so we could just skip this check entirely.
OK. I'll return the string by assuming it's domain name.
Quoted 57 lines
Quoted 57 lines
grub-core/net/bootp.c +56
-0+ ip_len = ip_end - ip_start; + +if (proto) + { +grub_size_t proto_len =grub_strlen (pr) -3 ; + + *proto =grub_malloc (proto_len +1 ); +if (!*proto) + goto cleanup; + +grub_memcpy (*proto, pr, proto_len); + *(*proto + proto_len) ='\0' ; + } + +if (server_ip) + { + *server_ip =grub_malloc (ip_len +1 ); + +if (!*server_ip) + goto cleanup; + +grub_memcpy (*server_ip, ip_start, ip_len); + *(*server_ip + ip_len) ='\0' ; + } + + path = ip_end +1 ; + +if (boot_file) + { + *boot_file =grub_strdup (path); + +if (!*boot_file) + goto cleanup; + } + +cleanup: + +if (bootfile_url) +grub_free (bootfile_url); + +if (grub_errno) + { +if (proto && *proto) + { +grub_free (*proto); + *proto =NULL ; + } + +if (server_ip && *server_ip) + { +grub_free (*server_ip); + *server_ip =NULL ; + } + +if (boot_file && *boot_file) + {
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 635
Quoted 6 lines
grub-core/net/bootp.c +6
-0+grub_free (*boot_file); + *boot_file =NULL ; + } + } + +return ;redundant return
OK.
Quoted 29 lines
Quoted 15 lines
grub-core/net/bootp.c +14
-0+} + + +static grub_err_t +grub_net_configure_by_dhcpv6_adv (const struct grub_net_dhcpv6_packet *v6_adv, +grub_size_t size, +struct grub_dhcpv6_session *session) +{ +struct grub_net_buff *nb; +const struct grub_dhcpv6_option *opt_client, *opt_server, *opt_iana; +struct grub_dhcpv6_option *popt; +struct grub_net_dhcpv6_packet *v6; +struct udphdr *udph; +grub_net_network_level_address_t multicast;grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 659
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_net_link_level_address_t ll_multicast; +struct grub_net_network_level_interface *inf; +grub_uint16_t len; +grub_uint64_t elapsed; +grub_err_t err =GRUB_ERR_NONE ; + + opt_client =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_CLIENTID); +if (grub_errno) +grub_error_push (); +Should not we check that is it actually one of our sessions? From RFC3315 validation
Quoted 8 lines
- the contents of the Client Identifier option does not match the client's DUID.
- the "transaction-id" field value does not match the value the client used in its Solicit message.
transaction ID is checked earlier, we also need to check client ID somewhere
Yes, you're right, that check is necessary to have.
Quoted 16 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 671
Quoted 13 lines
grub-core/net/bootp.c +12
-0+ opt_server =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_SERVERID); +if (grub_errno) +grub_error_push (); + + opt_iana =find_dhcpv6_option_in_packet (v6_adv, size, GRUB_DHCPv6_OPTION_IA_NA); +if (grub_errno) +grub_error_push (); + +grub_error_pop (); +if (grub_errno) +return grub_errno; +Do we really need this complication? Why not just return on the first missing option?
The intention is to report all missing options once so that admin can understand better what's going wrong. It's not a strict thing to follow so that I'm fine with returing first missing option.
Quoted 179 lines
Quoted 14 lines
grub-core/net/bootp.c +13
-0+ inf = session->iface; + + 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_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb)grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 694
Quoted 10 lines
grub-core/net/bootp.c +10
-0+return grub_errno; + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + len =grub_cpu_to_be16 (opt_client->len);
get_unaligned,grub_be_to_cpu16grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 703
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_client)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_client, len +sizeof (*opt_client)); + + len =grub_cpu_to_be16 (opt_server->len);ditto
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 712
Quoted 9 lines
grub-core/net/bootp.c +9
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_server)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_server, len +sizeof (*opt_server)); + + len =grub_cpu_to_be16 (opt_iana->len);ditto
Quoted 7 lines
grub-core/net/bootp.c +7
-0+ err =grub_netbuff_push (nb, len +sizeof (*opt_iana)); +if (err) + { +grub_netbuff_free (nb); +return err; + } +grub_memcpy (nb->data, opt_iana, len +sizeof (*opt_iana));grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 729
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ + err =grub_netbuff_push (nb,sizeof (*popt) +2 *sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ORO);From here it starts to be interesting. At least server ID option is out of our control so everything after it may be unaligned.
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 730
Quoted 1 line
grub-core/net/bootp.c +1
-0+ popt->len =grub_cpu_to_be16_compile_time (2 *sizeof (grub_uint16_t));unaligned?
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 743
Quoted 13 lines
grub-core/net/bootp.c +13
-0+grub_set_unaligned16 (popt->data,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_BOOTFILE_URL)); +grub_set_unaligned16 (popt->data +2 ,grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_DNS_SERVERS)); + + err =grub_netbuff_push (nb,sizeof (*popt) +sizeof (grub_uint16_t)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + popt = (struct grub_dhcpv6_option *) nb->data; + popt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ELAPSED_TIME); + popt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t)); +unaligned?
Quoted 8 lines
grub-core/net/bootp.c +8
-0+/* the time is expressed in hundredths of a second */ + elapsed =grub_divmod64 (grub_get_time_ms () - session->start_time,10 ,0 ); + +if (elapsed >0xffff ) + elapsed =0xffff ; + +grub_set_unaligned16 (popt->data,grub_cpu_to_be16 ((grub_uint16_t )elapsed)); +grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 761
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *) nb->data; + v6->message_type = GRUB_DHCPv6_REQUEST; + v6->transaction_id = v6_adv->transaction_id;Not sure how compiler handles alignment in this case.
Quoted 4 lines
grub-core/net/bootp.c +4
-0+ + err =grub_netbuff_push (nb,sizeof (*udph)); +if (err) + {grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 775
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_netbuff_free (nb); +return err; + } + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + udph->chksum =0 ; + udph->len =grub_cpu_to_be16 (nb->tail - nb->data); +And even here header may be unaligned after starting from server ID option. I suggest to do it differently - compute total length, reserve aligned space and make server ID last to make sure everything before it is properly aligned. And add comment explaining it :)
Quoted 48 lines
grub-core/net/bootp.c +48
-0+ 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_dhcpv6_packet *v6, +grub_size_t size, +int is_def, +char **device,char **path) +{ +grub_net_network_level_address_t addr; +grub_net_network_level_netaddress_t netaddr; +struct grub_net_network_level_interface *inf; +const grub_uint8_t *your_ip; +char *proto; +char *server_ip; +char *boot_file; +grub_net_network_level_address_t *dns; +grub_uint16_t num_dns; + +if (device) + *device =NULL ; + +if (path) + *path =NULL ; + +if (v6->message_type != GRUB_DHCPv6_REPLY) + { +grub_error (GRUB_ERR_IO ,N_ ("DHCPv6 info not found" )); +return NULL ; + } + + your_ip =find_dhcpv6_address (v6, size); + +if (!your_ip) +return NULL ; +
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 828
Quoted 5 lines
grub-core/net/bootp.c +5
-0+ addr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + addr.ipv6[0 ] =grub_get_unaligned64 (your_ip); + addr.ipv6[1 ] =grub_get_unaligned64 (your_ip +8 ); + inf =grub_net_add_addr (name, card, &addr, &card->default_address, flags); +NULL check. It is not clear if it makes sense to continue if we failed to actually set address at this point.
Ok.
Quoted 10 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 833
Quoted 6 lines
grub-core/net/bootp.c +5
-0+ netaddr.type =GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6 ; + netaddr.ipv6.base[0 ] =grub_get_unaligned64 (your_ip); + netaddr.ipv6.base[1 ] =0 ; + netaddr.ipv6.masksize =64 ; +grub_net_add_route (name, netaddr, inf);This will crash in
net_ls_routeslater if inf was NULL. Yes, I know that it is used this way currently, but that needs fixing in other places as well.\
Ok.
Quoted 11 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 839
Quoted 7 lines
grub-core/net/bootp.c +6
-0+ +get_dhcpv6_dns_address (v6, size, &dns, &num_dns); + +if (grub_errno) + grub_errno =GRUB_ERR_NONE ; +This ignores legitimate errors like out of memory. As mentioned above, does it need to set any
grub_errnoon its own? If not, errors should not be ignored.
Because missing dns option is totally fine so that I don't want to treat it as error. We can use a specific error number for missing options and report other errors, but I can't find a suitable one for it (in include/grub/err.h).
Quoted 17 lines
Quoted 17 lines
grub-core/net/bootp.c +16
-0+if (dns && num_dns) + { +int i; + +for (i =0 ; i < num_dns; ++i) +grub_net_add_dns_server (dns + i); + +grub_free (dns); + } + +find_dhcpv6_bootfile_url (v6, size, &proto, &server_ip, &boot_file); + +if (grub_errno) +grub_print_error (); + +if (boot_file)
Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 861
Quoted 6 lines
grub-core/net/bootp.c +6
-0+grub_env_set_net_property (name,"boot_file" , boot_file, +grub_strlen (boot_file)); + +if (is_def && server_ip) + { + grub_net_default_server =grub_strdup (server_ip);NULL check
OK.
Quoted 47 lines
Quoted 47 lines
grub-core/net/bootp.c +39
-0+grub_env_set ("net_default_interface" , name); +grub_env_export ("net_default_interface" ); + } + +if (device && server_ip && proto) + { + *device =grub_xasprintf ("%s,%s" , proto, server_ip); +if (!*device) +grub_print_error (); + } + +if (path && boot_file) + { + *path =grub_strdup (boot_file); +if (*path) + { +char *slash; + slash =grub_strrchr (*path,'/' ); +if (slash) + *slash =0 ; +else + **path =0 ; + } +else +grub_print_error (); + } + +if (proto) +grub_free (proto); + +if (server_ip) +grub_free (server_ip); + +if (boot_file) +grub_free (boot_file); + +return inf; +} +void grub_net_process_dhcp (struct grub_net_buff *nb,struct grub_net_card *card)@@ -288,6 +930,70 @@ grub_net_process_dhcp (struct grub_net_buff *nb, } }
Quoted 16 lines
Quoted 4 lines
grub-core/net/bootp.c +4
-0+void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card __attribute__ ((unused))) +{grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb, · line 946
Quoted 10 lines
grub-core/net/bootp.c +10
-0+const struct grub_net_dhcpv6_packet *v6; +struct grub_dhcpv6_session *session; +const struct grub_dhcpv6_option *opt_iana; +const struct grub_dhcpv6_iana_option *ia_na; +grub_size_t size; + + v6 = (const struct grub_net_dhcpv6_packet *) nb->data; + + size = nb->tail - nb->data; +Size check here looks like the most natural place.
OK.
Quoted 75 lines
grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb, · line 958
Quoted 13 lines
grub-core/net/bootp.c +12
-0+ opt_iana =find_dhcpv6_option_in_packet (v6, size, GRUB_DHCPv6_OPTION_IA_NA); +if (!opt_iana) + { +grub_print_error (); +return ; + } + + ia_na = (const struct grub_dhcpv6_iana_option *)opt_iana->data; +FOR_DHCPV6_SESSIONS (session) + { +if (session->transaction_id == v6->transaction_id + && session->iaid ==grub_cpu_to_be32 (ia_na->iaid))
get_unalignedQuoted 59 lines
grub-core/net/bootp.c +51
-1+break ; + } + +if (!session) +return ; + +if (v6->message_type == GRUB_DHCPv6_ADVERTISE) + { +grub_net_configure_by_dhcpv6_adv (v6, size, session); + } +else if (v6->message_type == GRUB_DHCPv6_REPLY) + { +char *name; +struct grub_net_network_level_interface *inf; + + inf = session->iface; + name =grub_xasprintf ("%s:dhcp6" , inf->card->name); +if (!name) +return ; + +grub_net_configure_by_dhcpv6_reply (name, inf->card, +0 , v6, size,0 ,0 ,0 ); + +if (!grub_errno) + { +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + } + +grub_free (name); + } + +if (grub_errno) +grub_print_error (); + +return ; +} +static char hexdigit (grub_uint8_t val) {@@ -564,7 +1270,190 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),return err; } -static grub_command_t cmd_getdhcp, cmd_bootp; + +static grub_err_t +grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)), +int argc,char **args) +{ +struct grub_net_card *card; +unsigned j =0 ; +int interval; +grub_err_t err; +struct grub_dhcpv6_session *session; + + err =GRUB_ERR_NONE ; +
Quoted 8 lines
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1291
Quoted 6 lines
grub-core/net/bootp.c +6
-0+FOR_NET_CARDS (card) + { +if (argc >0 &&grub_strcmp (card->name, args[0 ]) !=0 ) +continue ; + } +What is the goal of this loop?
It's dead code somehow not get cleaned from previous version. Sorry.
Quoted 18 lines
Quoted 18 lines
grub-core/net/bootp.c +17
-0+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_dhcpv6_sessions_free (); +return grub_errno; + } + + session =grub_zalloc (sizeof (*session)); + session->iface = iface; + session->iaid = j;
Quoted 8 lines
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1313
Quoted 5 lines
grub-core/net/bootp.c +5
-0+grub_dhcpv6_session_add (session); + j++; + } + +for (interval =200 ; interval <10000 ; interval *=2 )In general that does not follow RFC3315 required client behavior at all. But we need at least something that can be improved later.
OK.
Quoted 98 lines
Quoted 44 lines
grub-core/net/bootp.c +43
-0+ { +int done =1 ; + +FOR_DHCPV6_SESSIONS (session) + { +struct grub_net_buff *nb; +struct grub_dhcpv6_option *opt; +struct grub_net_dhcpv6_packet *v6; +struct grub_DUID_LL *duid; +struct grub_dhcpv6_iana_option *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 (session->iface, + &multicast, &ll_multicast); +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + + nb =grub_netbuff_alloc (GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); + +if (!nb) + { +grub_dhcpv6_sessions_free (); +return grub_errno; + } + + err =grub_netbuff_reserve (nb, GRUB_DHCPv6_DEFAULT_NETBUFF_ALLOC_SIZE); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (grub_uint16_t));grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1366
Quoted 10 lines
grub-core/net/bootp.c +10
-0+if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_ELAPSED_TIME); + opt->len =grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));As far as I can tell, we remain 2 bytes aligned here. But we need to change prototype to account for possible unaligned values on input, it likely needs change through the whole function.
Quoted 24 lines
grub-core/net/bootp.c +24
-0+grub_set_unaligned16 (opt->data,0 ); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*duid)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_CLIENTID); + opt->len =grub_cpu_to_be16 (sizeof (*duid)); + + duid = (struct grub_DUID_LL *) opt->data; + + duid->type =grub_cpu_to_be16_compile_time (3 ) ; + duid->hw_type =grub_cpu_to_be16_compile_time (1 ); +grub_memcpy (&duid->hwaddr, &session->iface->hwaddress.mac, +sizeof (session->iface->hwaddress.mac)); + + err =grub_netbuff_push (nb,sizeof (*opt) +sizeof (*ia_na)); +if (err) + {grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1400
Quoted 10 lines
grub-core/net/bootp.c +10
-0+grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + opt = (struct grub_dhcpv6_option *)nb->data; + opt->code =grub_cpu_to_be16_compile_time (GRUB_DHCPv6_OPTION_IA_NA); + opt->len =grub_cpu_to_be16 (sizeof (*ia_na)); + ia_na = (struct grub_dhcpv6_iana_option *)opt->data; + ia_na->iaid =grub_cpu_to_be32 (session->iaid);Are you sure we are still aligned to 4 bytes here? You go from top to bottom and some fields are not multiple of 4 bytes. As mentioned earlier, may be ensure buffer is aligned in advance.
Quoted 15 lines
grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), · line 1413
Quoted 13 lines
grub-core/net/bootp.c +13
-0+ ia_na->t1 =0 ; + ia_na->t2 =0 ; + + err =grub_netbuff_push (nb,sizeof (*v6)); +if (err) + { +grub_dhcpv6_sessions_free (); +grub_netbuff_free (nb); +return err; + } + + v6 = (struct grub_net_dhcpv6_packet *)nb->data; + v6->message_type =1 ;no magic constants, please.
OK.
Quoted 154 lines
Quoted 153 lines
grub-core/net/bootp.c +47
-0+ v6->transaction_id = session->transaction_id; + +grub_netbuff_push (nb,sizeof (*udph)); + + udph = (struct udphdr *) nb->data; + udph->src =grub_cpu_to_be16_compile_time (546 ); + udph->dst =grub_cpu_to_be16_compile_time (547 ); + 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 , + &session->iface->address, &multicast); + + err =grub_net_send_ip_packet (session->iface, &multicast, + &ll_multicast, nb,GRUB_NET_IP_UDP ); + done =0 ; +grub_netbuff_free (nb); + +if (err) + { +grub_dhcpv6_sessions_free (); +return err; + } + } +if (!done) +grub_net_poll_cards (interval,0 ); + } + +FOR_DHCPV6_SESSIONS (session) + { +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("couldn't autoconfigure %s" ), + session->iface->card->name); +grub_list_remove (GRUB_AS_LIST (session)); +grub_free (session); + session = grub_dhcpv6_sessions; + } + +return err; +} + +static grub_command_t cmd_getdhcp, cmd_bootp, cmd_bootp6;void grub_bootp_init (void )@@ -575,6 +1464,9 @@ 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 @@ -582,4 +1474,5 @@ grub_bootp_fini (void) {grub_unregister_command (cmd_getdhcp);grub_unregister_command (cmd_bootp); +grub_unregister_command (cmd_bootp6); }grub-core/net/ip.c +35
-0@@ -238,6 +238,41 @@ 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 (546 )) + { +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; + } +grub_net_process_dhcp6 (nb, card); +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;include/grub/net.h +19
-0@@ -418,6 +418,13 @@ struct grub_net_bootp_packetgrub_uint8_t vendor[0 ]; }GRUB_PACKED ; +struct grub_net_dhcpv6_packet +{ +grub_uint32_t message_type:8 ; +grub_uint32_t transaction_id:24 ; +grub_uint8_t dhcp_options[0 ]; +}GRUB_PACKED ; +#define GRUB_NET_BOOTP_RFC1048_MAGIC_0 0x63#define GRUB_NET_BOOTP_RFC1048_MAGIC_1 0x82#define GRUB_NET_BOOTP_RFC1048_MAGIC_2 0x53@@ -444,6 +451,14 @@ grub_net_configure_by_dhcp_ack (const char *name,grub_size_t size, int is_def, char **device, char **path); +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_dhcpv6_packet *v6, +grub_size_t size, +int is_def,char **device,char **path); +grub_err_t grub_net_add_ipv4_local (struct grub_net_network_level_interface *inf,int mask);@@ -452,6 +467,10 @@ void grub_net_process_dhcp (struct grub_net_buff *nb, struct grub_net_card *card); +void +grub_net_process_dhcp6 (struct grub_net_buff *nb, +struct grub_net_card *card); +int grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,const grub_net_link_level_address_t *b);
Re: [PATCH 1/3] Added net_bootp6 command
В Tue, 19 May 2015 16:42:00 +0800
Michael Chang <[email protected]> пишет:
Quoted 14 lines
Quoted 13 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 301
Quoted 10 lines
grub-core/net/bootp.c +9
-0+#define GRUB_DHCPv6_OPTION_CLIENTID 1 +#define GRUB_DHCPv6_OPTION_SERVERID 2 +#define GRUB_DHCPv6_OPTION_IA_NA 3 +#define GRUB_DHCPv6_OPTION_IAADDR 5 +#define GRUB_DHCPv6_OPTION_ORO 6 +#define GRUB_DHCPv6_OPTION_ELAPSED_TIME 8 +#define GRUB_DHCPv6_OPTION_DNS_SERVERS 23 +#define GRUB_DHCPv6_OPTION_BOOTFILE_URL 59 +enum please. These can also be anonymous, no need to invent fancy names.
Quoted 2 lines
I don't understand. Do you want me to use enum type here rather than macro ? Where are the fancy names invented here ?
Sorry I mean there is no need to name enume type. Just
enum
{
GRUB_DHCPv6_OPTION_CLIENTID = 1
...
};
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
grub-core/net/bootp.c +10
-0+static void +get_dhcpv6_dns_address (const struct grub_net_dhcpv6_packet *packet,grub_size_t size, +grub_net_network_level_address_t **addr,grub_uint16_t *naddr) +{ +const struct grub_dhcpv6_option * popt; +grub_uint16_t len, ln; +const grub_uint8_t *pa; +grub_net_network_level_address_t *la; + +if (!addr || !naddr)Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 473
Quoted 5 lines
grub-core/net/bootp.c +5
-0+ { +grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("bad argument for get_dhcpv6_dns_address" )); +return ; + } +it is called exactly once and
grub_errnois immediately reset toGRUB_ERR_NONE. So what's the point of returning error at all?
Quoted 4 lines
Because a function wants to report error for bad inputs, while it's caller wants to suppress the error message because dns information could be missing in packets and is allowed (not treated as error).
Sure but it is called exactly once and result is not used. It is not like this is library function, it is more of a convenience macro. For the same reasons NULL check on input is probably redundant as well.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
grub-core/net/bootp.c +6
-0+if (len ==0 || len &0xf ) + { +grub_error (GRUB_ERR_IO ,N_ ("invalid dns address length" )); +return ; + } +
Quoted 5 lines
Quoted 1 line
same question about
grub_error. May begrub_debug?No
grub_debug, maybegrub_dprintf? Maybe we should keep it and use specific error number for option not found ? See below for details.
Sure, I mean dprintf, sorry.
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
grub-core/net/bootp.c +10
-0+ +/* Follow elilo and edk2 that check for starting and ending delimiter '[..]' +in which IPv6 server address is enclosed. */ +if (*ip_start !='[' ) + ip_start =NULL ; +else + ip_end =grub_strchr (++ip_start,']' ); + +if (!ip_start || !ip_end) + {Quoted 8 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 573
Quoted 4 lines
grub-core/net/bootp.c +4
-0+grub_error (GRUB_ERR_IO ,N_ ("IPv6-address not in square brackets" )); + goto cleanup; + } +RFC5970 says "Clients that have DNS implementations SHOULD support the use of domain names in the URL" and in general string can also be valid IPv4 address, nothing restricts it to IPv6.
Quoted 6 lines
But the descriptions preceding it did explicitly say IPv6:
" If the host in the URL is expressed using an IPv6 address rather than a domain name, the address in the URL then MUST be enclosed in "[" and "]" characters, conforming to [RFC3986]. "
it says "*if* it is using IPv6". Or at least so I understand it.
Quoted 6 lines
Quoted 3 lines
So I do not think you
should return error here, just return full string. I wish grub supported IPv6 literals so we could just skip this check entirely.OK. I'll return the string by assuming it's domain name.
...
Quoted 12 lines
Quoted 11 lines
grub-core/net/bootp.c grub_net_configure_by_dhcp_ack (const char *name, · line 839
Quoted 7 lines
grub-core/net/bootp.c +6
-0+ +get_dhcpv6_dns_address (v6, size, &dns, &num_dns); + +if (grub_errno) + grub_errno =GRUB_ERR_NONE ; +This ignores legitimate errors like out of memory. As mentioned above, does it need to set any
grub_errnoon its own? If not, errors should not be ignored.
Quoted 5 lines
Because missing dns option is totally fine so that I don't want to treat it as error. We can use a specific error number for missing options and report other errors, but I can't find a suitable one for it (in
include/grub/err.h).
If missing DNS option is totally fine it should not be returned as error. You already have indication (num_dns == 0) which is enough if you want to inform user. Errors should be returned in case of e-h-h errors :)
Thanks! And sorry for delay.