IPv6, TCP, HTTP, ICMP and DNS support. Several cleanups and bugfixes.

* grub-core/Makefile.core.def (net): Add net/dns.c, net/tcp.c,
	net/icmp.c and net/icmp6.c.
	(http): New module.
	(priority_queue): Likewise.
	* grub-core/io/bufio.c: Rewritten.
	* grub-core/lib/legacy_parse.c (legacy_command): New argument type
	TYPE_WITH_CONFIGFILE_OPTION.
	(legacy_commands): Add bootp and dhcp.
	(is_option): Handle TYPE_WITH_CONFIGFILE_OPTION.
	(grub_legacy_parse): Likewise.
	* grub-core/lib/priority_queue.c: New file.
	* grub-core/net/arp.c: Add missing license header.
	(arp_find_entry): Removed.
	(arp_find_entry): Likewise.
	(grub_net_arp_resolve): Rename to ...
	(grub_net_arp_send_request): ...this.
	(grub_net_arp_receive): New card argument.
	* grub-core/net/bootp.c (parse_dhcp_vendor): Clean up.
	Set router and DNS server.
	(grub_net_configure_by_dhcp_ack): Handle routing information.
	(grub_cmd_bootp): Set checksum.
	(grub_bootp_init): Remove net_dhcp.
	* grub-core/net/dns.c: New file.
	* grub-core/net/drivers/efi/efinet.c (send_card_buffer): Wait for
	completion.
	(get_card_packet): Handle allocation.
	(grub_efinet_findcards): Set mtu.
	* grub-core/net/drivers/emu/emunet.c: Add missing license header.
	(get_card_packet): Handle allocation.
	(emucard): Set mtu.
	* grub-core/net/drivers/i386/pc/pxe.c (grub_pxe_recv): Handle allocation
	(GRUB_MOD_INIT): Set mtu.
	* grub-core/net/drivers/ieee1275/ofnet.c (grub_ofnetcard_data): Remove
	mtu.
	(get_card_packet): Handle allocation.
	(grub_ofnet_findcards): Set mtu.
	* grub-core/net/ethernet.c (send_ethernet_packet): Add compile time
	assert.
	(grub_net_recv_ethernet_packet): Handle IPv6.
	* grub-core/net/http.c: New file.
	* grub-core/net/icmp.c: Likewise.
	* grub-core/net/icmp6.c: Likewise.
	* grub-core/net/ip.c (ip6addr): New type.
	(ip6hdr): Likewise.
	(reassemble): Likewise.
	(cmp): New function.
	(reassembles): New variable.
	(grub_net_ip_chksum): Handle 0xffff sum and unaligned buffers.
	(id): New variable.
	(send_fragmented): New function.
	(grub_net_send_ip_packet): Rename to ...
	(grub_net_send_ip4_packet): ... this. Send fragmented if needed.
	Handle non-UDP.
	(grub_net_recv_ip_packets): Rename to ...
	(handle_dgram): ... this. Check checksum. Handle non-UDP.
	(free_rsm): New function.
	(free_old_fragments): Likewise.
	(grub_net_recv_ip4_packets): New function.
	(grub_net_send_ip6_packet): Likewise.
	(grub_net_send_ip_packet): Likewise.
	(grub_net_recv_ip6_packets): Likewise.
	(grub_net_recv_ip_packets): Likewise.
	* grub-core/net/net.c (grub_net_link_layer_entry): New struct.
	(LINK_LAYER_CACHE_SIZE): New const.
	(link_layer_find_entry): New function.
	(grub_net_link_layer_add_address): Likewise.
	(grub_net_link_layer_resolve_check): Likewise.
	(grub_net_link_layer_resolve): Likewise.
	(grub_net_ipv6_get_slaac): Likewise.
	(grub_net_ipv6_get_link_local): Likewise.
	(grub_cmd_ipv6_autoconf): Likewise.
	(parse_ip): Handle one number representation.
	(parse_ip6): New functoion.
	(match_net): Handle IPv6.
	(grub_net_resolve_address): Handle IPv6 and DNS.
	(grub_net_resolve_net_address): Handle IPv6.
	(route_cmp): New function.
	(grub_net_route_address): Find best route.
	(grub_net_addr_to_str): Handle IPv6.
	(grub_net_addr_cmp): New function.
	(grub_net_add_addr): Register local route.
	(print_net_address): Handle net address.
	(grub_net_poll_cards): Retransmit TCP.
	(grub_net_poll_cards_idle_real): Likewise.
	(have_ahead): New function.
	(grub_net_seek_real): Use underlying seek.
	(GRUB_MOD_INIT): Register net_ipv6_autoconf and init dns.
	* grub-core/net/tcp.c: New file.
	* grub-core/net/tftp.c (tftp_data): Add priority_queue.
	(cmp): New function.
	(ack): Likewise.
	(tftp_receive): Handle unordered input.
	(destroy_pq): New function.
	(tftp_close): Close pq.
	* grub-core/net/udp.c: Put missing license header.
	(grub_net_udp_socket): New function.
	(udp_socket_register): Likewise.
	(grub_net_udp_close): Likewise.
	(grub_net_recv_udp_packet): Check checksum.
	* include/grub/efi/api.h (grub_efi_simple_network): Add status.
	* include/grub/misc.h (grub_memchr): New function.
	* include/grub/net.h (GRUB_NET_*_SIZE): New enum.
	(grub_net_card_driver): Return buf in recv.
	(grub_net_slaac_mac_list): New struct.
	(grub_network_level_protocol_id): Add ipv6.
	(grub_net_network_level_addr): Likewise.
	(grub_net_network_level_net_addr): Likewise.
	(grub_net_app_protocol): Add seek.
	(grub_net_socket): Removed.
	(grub_net_sockets): Likewise.
	(grub_net_socket_register): Likewise.
	(grub_net_socket_unregister): Likewise.
	(FOR_NET_SOCKETS): Likewise.
	(grub_net_add_addr): Add const.
	(GRUB_NET_BOOTP_*): New enum.
	(grub_net_addr_cmp): New proto.
	(GRUB_NET_MAX_STR_ADDR_LEN): Take IPV6 into account.
	(GRUB_NET_MAX_STR_HWADDR_LEN): New define.
	(grub_net_hwaddr_to_str): NEw proto.
	(FOR_NET_NETWORK_LEVEL_INTERFACES): New macro.
	(FOR_NET_NETWORK_LEVEL_INTERFACES_SAFE): Handle NULL.
	(grub_dns_init): New proto.
	(grub_dns_fini): Likewise.
	(grub_net_tcp_retransmit): Likewise.
	(grub_net_link_layer_add_address): Likewise.
	(grub_net_link_layer_resolve_check): Likewise.
	(grub_net_link_layer_resolve): Likewise.
	(grub_net_dns_lookup): Likewise.
	(grub_net_add_dns_server): Likewise.
	(grub_net_remove_dns_server): Likewise.
	(GRUB_NET_TRIES): New const.
	(GRUB_NET_INTERVAL): Likewise.
	* include/grub/net/arp.h: Mostly rewritten.
	* include/grub/net/ethernet.h (grub_net_ethertype_t): New enum.
	* include/grub/net/ip.h: Mostly rewritten.
	* include/grub/net/netbuff.h: Indent.
	* include/grub/net/tcp.h: New file.
	* include/grub/net/udp.h: Mostly rewritten.
	* include/grub/priority_queue.h: New file.
	* include/grub/types.h (PRIdGRUB_SSIZE): New define.
	(grub_swap_bytes64_compile_time): Likewise.
	(grub_cpu_to_be16_compile_time): Likewise.
	(grub_cpu_to_be32_compile_time): Likewise.
	(grub_cpu_to_be64_compile_time): Likewise.
	(grub_be_to_cpu64_compile_time): Likewise.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2011-12-20 18:17:07 +01:00
commit 198e150aaf
34 changed files with 5588 additions and 617 deletions

View file

@ -25,6 +25,7 @@
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/file.h>
#include <grub/priority_queue.h>
GRUB_MOD_LICENSE ("GPLv3+");
@ -102,24 +103,63 @@ typedef struct tftp_data
grub_uint64_t block;
grub_uint32_t block_size;
int have_oack;
grub_net_socket_t sock;
grub_net_udp_socket_t sock;
grub_priority_queue_t pq;
} *tftp_data_t;
static int
cmp (const void *a__, const void *b__)
{
struct grub_net_buff *a_ = *(struct grub_net_buff **) a__;
struct grub_net_buff *b_ = *(struct grub_net_buff **) b__;
struct tftphdr *a = (struct tftphdr *) a_->data;
struct tftphdr *b = (struct tftphdr *) b_->data;
/* We want the first elements to be on top. */
if (grub_be_to_cpu16 (a->u.data.block) < grub_be_to_cpu16 (b->u.data.block))
return +1;
if (grub_be_to_cpu16 (a->u.data.block) > grub_be_to_cpu16 (b->u.data.block))
return -1;
return 0;
}
static grub_err_t
tftp_receive (grub_net_socket_t sock __attribute__ ((unused)),
ack (grub_net_udp_socket_t sock, grub_uint16_t block)
{
struct tftphdr *tftph_ack;
grub_uint8_t nbdata[512];
struct grub_net_buff nb_ack;
grub_err_t err;
nb_ack.head = nbdata;
nb_ack.end = nbdata + sizeof (nbdata);
grub_netbuff_clear (&nb_ack);
grub_netbuff_reserve (&nb_ack, 512);
err = grub_netbuff_push (&nb_ack, sizeof (tftph_ack->opcode)
+ sizeof (tftph_ack->u.ack.block));
if (err)
return err;
tftph_ack = (struct tftphdr *) nb_ack.data;
tftph_ack->opcode = grub_cpu_to_be16 (TFTP_ACK);
tftph_ack->u.ack.block = block;
err = grub_net_send_udp_packet (sock, &nb_ack);
return err;
}
static grub_err_t
tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
struct grub_net_buff *nb,
void *f)
{
grub_file_t file = f;
struct tftphdr *tftph = (void *) nb->data;
char nbdata[512];
tftp_data_t data = file->data;
grub_err_t err;
char *ptr;
struct grub_net_buff nb_ack;
grub_uint8_t *ptr;
nb_ack.head = nbdata;
nb_ack.end = nbdata + sizeof (nbdata);
if (nb->tail - nb->data < (grub_ssize_t) sizeof (tftph->opcode))
return grub_error (GRUB_ERR_OUT_OF_RANGE, "TFTP packet too small");
tftph = (struct tftphdr *) nb->data;
switch (grub_be_to_cpu16 (tftph->opcode))
@ -130,76 +170,99 @@ tftp_receive (grub_net_socket_t sock __attribute__ ((unused)),
for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail;)
{
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
{
data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1,
0, 0);
}
data->file_size = grub_strtoul ((char *) ptr + sizeof ("tsize\0")
- 1, 0, 0);
if (grub_memcmp (ptr, "blksize\0", sizeof ("blksize\0") - 1) == 0)
{
data->block_size = grub_strtoul (ptr + sizeof ("blksize\0") - 1,
0, 0);
}
data->block_size = grub_strtoul ((char *) ptr + sizeof ("blksize\0")
- 1, 0, 0);
while (ptr < nb->tail && *ptr)
ptr++;
ptr++;
}
data->block = 0;
grub_netbuff_free (nb);
break;
case TFTP_DATA:
err = grub_netbuff_pull (nb, sizeof (tftph->opcode) +
sizeof (tftph->u.data.block));
err = ack (data->sock, 0);
if (err)
return err;
if (grub_be_to_cpu16 (tftph->u.data.block) == data->block + 1)
{
unsigned size = nb->tail - nb->data;
data->block++;
if (size < data->block_size)
{
file->device->net->eof = 1;
}
/* Prevent garbage in broken cards. */
if (size > data->block_size)
{
err = grub_netbuff_unput (nb, size - data->block_size);
if (err)
return err;
}
/* If there is data, puts packet in socket list. */
if ((nb->tail - nb->data) > 0)
grub_net_put_packet (&file->device->net->packs, nb);
else
grub_netbuff_free (nb);
}
else
{
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
break;
return GRUB_ERR_NONE;
case TFTP_DATA:
if (nb->tail - nb->data < (grub_ssize_t) (sizeof (tftph->opcode)
+ sizeof (tftph->u.data.block)))
return grub_error (GRUB_ERR_OUT_OF_RANGE, "TFTP packet too small");
err = ack (data->sock, tftph->u.data.block);
if (err)
return err;
err = grub_priority_queue_push (data->pq, &nb);
if (err)
return err;
{
struct grub_net_buff **nb_top_p, *nb_top;
while (1)
{
nb_top_p = grub_priority_queue_top (data->pq);
if (!nb_top_p)
return GRUB_ERR_NONE;
nb_top = *nb_top_p;
tftph = (struct tftphdr *) nb_top->data;
if (grub_be_to_cpu16 (tftph->u.data.block) >= data->block + 1)
break;
grub_priority_queue_pop (data->pq);
}
if (grub_be_to_cpu16 (tftph->u.data.block) == data->block + 1)
{
unsigned size;
grub_priority_queue_pop (data->pq);
err = grub_netbuff_pull (nb_top, sizeof (tftph->opcode) +
sizeof (tftph->u.data.block));
if (err)
return err;
size = nb_top->tail - nb_top->data;
data->block++;
if (size < data->block_size)
{
file->device->net->eof = 1;
grub_net_udp_close (data->sock);
data->sock = NULL;
}
/* Prevent garbage in broken cards. Is it still necessary
given that IP implementation has been fixed?
*/
if (size > data->block_size)
{
err = grub_netbuff_unput (nb_top, size - data->block_size);
if (err)
return err;
}
/* If there is data, puts packet in socket list. */
if ((nb_top->tail - nb_top->data) > 0)
grub_net_put_packet (&file->device->net->packs, nb_top);
else
grub_netbuff_free (nb);
}
}
return GRUB_ERR_NONE;
case TFTP_ERROR:
grub_netbuff_free (nb);
return grub_error (GRUB_ERR_IO, (char *) tftph->u.err.errmsg);
default:
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
grub_netbuff_clear (&nb_ack);
grub_netbuff_reserve (&nb_ack, 512);
err = grub_netbuff_push (&nb_ack, sizeof (tftph->opcode)
+ sizeof (tftph->u.ack.block));
if (err)
return err;
}
tftph = (struct tftphdr *) nb_ack.data;
tftph->opcode = grub_cpu_to_be16 (TFTP_ACK);
tftph->u.ack.block = grub_cpu_to_be16 (data->block);
static void
destroy_pq (tftp_data_t data)
{
struct grub_net_buff **nb_p;
while ((nb_p = grub_priority_queue_top (data->pq)))
grub_netbuff_free (*nb_p);
err = grub_net_send_udp_packet (data->sock, &nb_ack);
if (file->device->net->eof)
{
grub_net_udp_close (data->sock);
data->sock = NULL;
}
return err;
grub_priority_queue_destroy (data->pq);
}
static grub_err_t
@ -210,10 +273,12 @@ tftp_open (struct grub_file *file, const char *filename)
int i;
int rrqlen;
int hdrlen;
char open_data[1500];
grub_uint8_t open_data[1500];
struct grub_net_buff nb;
tftp_data_t data;
grub_err_t err;
grub_uint8_t *nbd;
grub_net_network_level_address_t addr;
data = grub_zalloc (sizeof (*data));
if (!data)
@ -265,39 +330,48 @@ tftp_open (struct grub_file *file, const char *filename)
file->not_easily_seekable = 1;
file->data = data;
data->sock = grub_net_udp_open (file->device->net->server,
TFTP_SERVER_PORT, tftp_receive,
file);
if (!data->sock)
data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
if (!data->pq)
return grub_errno;
err = grub_net_send_udp_packet (data->sock, &nb);
err = grub_net_resolve_address (file->device->net->server, &addr);
if (err)
{
grub_net_udp_close (data->sock);
destroy_pq (data);
return err;
}
/* Receive OACK packet. */
for (i = 0; i < 3; i++)
data->sock = grub_net_udp_open (addr,
TFTP_SERVER_PORT, tftp_receive,
file);
if (!data->sock)
{
grub_net_poll_cards (100);
if (grub_errno)
return grub_errno;
if (data->have_oack)
break;
/* Retry. */
destroy_pq (data);
return grub_errno;
}
/* Receive OACK packet. */
nbd = nb.data;
for (i = 0; i < GRUB_NET_TRIES; i++)
{
nb.data = nbd;
err = grub_net_send_udp_packet (data->sock, &nb);
if (err)
{
grub_net_udp_close (data->sock);
destroy_pq (data);
return err;
}
grub_net_poll_cards (GRUB_NET_INTERVAL);
if (data->have_oack)
break;
}
if (!data->have_oack)
{
grub_net_udp_close (data->sock);
destroy_pq (data);
return grub_error (GRUB_ERR_TIMEOUT, "Time out opening tftp.");
}
file->size = data->file_size;
@ -312,7 +386,7 @@ tftp_close (struct grub_file *file)
if (data->sock)
{
char nbdata[512];
grub_uint8_t nbdata[512];
grub_err_t err;
struct grub_net_buff nb_err;
struct tftphdr *tftph;
@ -338,6 +412,7 @@ tftp_close (struct grub_file *file)
grub_print_error ();
grub_net_udp_close (data->sock);
}
destroy_pq (data);
grub_free (data);
return GRUB_ERR_NONE;
}