grub/grub-core/net/udp.c
Vladimir 'phcoder' Serbinenko 198e150aaf 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.
2011-12-20 18:17:07 +01:00

211 lines
5.5 KiB
C

/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/net.h>
#include <grub/net/udp.h>
#include <grub/net/ip.h>
#include <grub/net/netbuff.h>
#include <grub/time.h>
struct grub_net_udp_socket
{
struct grub_net_udp_socket *next;
enum { GRUB_NET_SOCKET_START,
GRUB_NET_SOCKET_ESTABLISHED,
GRUB_NET_SOCKET_CLOSED } status;
int in_port;
int out_port;
grub_err_t (*recv_hook) (grub_net_udp_socket_t sock, struct grub_net_buff *nb,
void *recv);
void *recv_hook_data;
grub_net_network_level_address_t out_nla;
grub_net_link_level_address_t ll_target_addr;
struct grub_net_network_level_interface *inf;
};
static struct grub_net_udp_socket *udp_sockets;
#define FOR_UDP_SOCKETS(var) for (var = udp_sockets; var; var = var->next)
static inline void
udp_socket_register (grub_net_udp_socket_t sock)
{
grub_list_push (GRUB_AS_LIST_P (&udp_sockets),
GRUB_AS_LIST (sock));
}
void
grub_net_udp_close (grub_net_udp_socket_t sock)
{
grub_list_remove (GRUB_AS_LIST_P (&udp_sockets),
GRUB_AS_LIST (sock));
grub_free (sock);
}
grub_net_udp_socket_t
grub_net_udp_open (grub_net_network_level_address_t addr,
grub_uint16_t out_port,
grub_err_t (*recv_hook) (grub_net_udp_socket_t sock,
struct grub_net_buff *nb,
void *data),
void *recv_hook_data)
{
grub_err_t err;
struct grub_net_network_level_interface *inf;
grub_net_network_level_address_t gateway;
grub_net_udp_socket_t socket;
static int in_port = 25300;
grub_net_link_level_address_t ll_target_addr;
if (addr.type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4
&& addr.type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "not an IP address");
return NULL;
}
err = grub_net_route_address (addr, &gateway, &inf);
if (err)
return NULL;
err = grub_net_link_layer_resolve (inf, &gateway, &ll_target_addr);
if (err)
return NULL;
socket = grub_zalloc (sizeof (*socket));
if (socket == NULL)
return NULL;
socket->out_port = out_port;
socket->inf = inf;
socket->out_nla = addr;
socket->ll_target_addr = ll_target_addr;
socket->in_port = in_port++;
socket->status = GRUB_NET_SOCKET_START;
socket->recv_hook = recv_hook;
socket->recv_hook_data = recv_hook_data;
udp_socket_register (socket);
return socket;
}
grub_err_t
grub_net_send_udp_packet (const grub_net_udp_socket_t socket,
struct grub_net_buff *nb)
{
struct udphdr *udph;
grub_err_t err;
COMPILE_TIME_ASSERT (GRUB_NET_UDP_HEADER_SIZE == sizeof (*udph));
err = grub_netbuff_push (nb, sizeof (*udph));
if (err)
return err;
udph = (struct udphdr *) nb->data;
udph->src = grub_cpu_to_be16 (socket->in_port);
udph->dst = grub_cpu_to_be16 (socket->out_port);
udph->chksum = 0;
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
&socket->inf->address,
&socket->out_nla);
return grub_net_send_ip_packet (socket->inf, &(socket->out_nla),
&(socket->ll_target_addr), nb,
GRUB_NET_IP_UDP);
}
grub_err_t
grub_net_recv_udp_packet (struct grub_net_buff *nb,
struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *source)
{
struct udphdr *udph;
grub_net_udp_socket_t sock;
grub_err_t err;
/* Ignore broadcast. */
if (!inf)
{
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
udph = (struct udphdr *) nb->data;
if (nb->tail - nb->data < (grub_ssize_t) sizeof (*udph))
{
grub_dprintf ("net", "UDP packet too short: %" PRIuGRUB_SIZE "\n",
nb->tail - nb->data);
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
FOR_UDP_SOCKETS (sock)
{
if (grub_be_to_cpu16 (udph->dst) == sock->in_port
&& inf == sock->inf
&& grub_net_addr_cmp (source, &sock->out_nla) == 0
&& (sock->status == GRUB_NET_SOCKET_START
|| grub_be_to_cpu16 (udph->src) == sock->out_port))
{
if (udph->chksum)
{
grub_uint16_t chk, expected;
chk = udph->chksum;
udph->chksum = 0;
expected = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
&sock->out_nla,
&sock->inf->address);
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;
}
if (sock->status == GRUB_NET_SOCKET_START)
{
sock->out_port = grub_be_to_cpu16 (udph->src);
sock->status = GRUB_NET_SOCKET_ESTABLISHED;
}
err = grub_netbuff_pull (nb, sizeof (*udph));
if (err)
return err;
/* App protocol remove its own reader. */
if (sock->recv_hook)
sock->recv_hook (sock, nb, sock->recv_hook_data);
else
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
}
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}