2011-07-08 12:41:52 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/net.h>
|
|
|
|
#include <grub/net/udp.h>
|
|
|
|
#include <grub/net/ip.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/time.h>
|
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
struct grub_net_udp_socket
|
|
|
|
{
|
|
|
|
struct grub_net_udp_socket *next;
|
2012-01-24 12:31:12 +00:00
|
|
|
struct grub_net_udp_socket **prev;
|
2011-07-08 12:41:52 +00:00
|
|
|
|
|
|
|
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;
|
2011-10-13 18:53:22 +00:00
|
|
|
grub_net_network_level_address_t out_nla;
|
|
|
|
grub_net_link_level_address_t ll_target_addr;
|
2011-07-08 12:41:52 +00:00
|
|
|
struct grub_net_network_level_interface *inf;
|
|
|
|
};
|
|
|
|
|
2011-12-20 17:17:07 +00:00
|
|
|
static struct grub_net_udp_socket *udp_sockets;
|
2011-07-08 12:41:52 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2012-01-24 12:31:12 +00:00
|
|
|
grub_list_remove (GRUB_AS_LIST (sock));
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_free (sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_net_udp_socket_t
|
2011-10-14 17:21:59 +00:00
|
|
|
grub_net_udp_open (grub_net_network_level_address_t addr,
|
2011-07-02 20:13:33 +00:00
|
|
|
grub_uint16_t out_port,
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_err_t (*recv_hook) (grub_net_udp_socket_t sock,
|
2011-07-02 20:13:33 +00:00
|
|
|
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;
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_udp_socket_t socket;
|
2011-07-02 20:13:33 +00:00
|
|
|
static int in_port = 25300;
|
2011-10-13 18:53:22 +00:00
|
|
|
grub_net_link_level_address_t ll_target_addr;
|
2011-07-02 20:13:33 +00:00
|
|
|
|
2011-10-12 23:22:56 +00:00
|
|
|
if (addr.type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4
|
|
|
|
&& addr.type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6)
|
2011-07-08 12:41:52 +00:00
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BUG, "not an IP address");
|
2011-07-08 12:41:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-02 20:13:33 +00:00
|
|
|
|
|
|
|
err = grub_net_route_address (addr, &gateway, &inf);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
2011-10-13 18:53:22 +00:00
|
|
|
err = grub_net_link_layer_resolve (inf, &gateway, &ll_target_addr);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
socket = grub_zalloc (sizeof (*socket));
|
|
|
|
if (socket == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
socket->out_port = out_port;
|
|
|
|
socket->inf = inf;
|
|
|
|
socket->out_nla = addr;
|
2011-10-13 18:53:22 +00:00
|
|
|
socket->ll_target_addr = ll_target_addr;
|
2011-07-08 12:41:52 +00:00
|
|
|
socket->in_port = in_port++;
|
|
|
|
socket->status = GRUB_NET_SOCKET_START;
|
2011-07-02 20:13:33 +00:00
|
|
|
socket->recv_hook = recv_hook;
|
|
|
|
socket->recv_hook_data = recv_hook_data;
|
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
udp_socket_register (socket);
|
2011-07-02 20:13:33 +00:00
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_err_t
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_send_udp_packet (const grub_net_udp_socket_t socket,
|
2011-06-08 00:59:53 +00:00
|
|
|
struct grub_net_buff *nb)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct udphdr *udph;
|
2011-06-18 23:20:53 +00:00
|
|
|
grub_err_t err;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-08 22:27:27 +00:00
|
|
|
COMPILE_TIME_ASSERT (GRUB_NET_UDP_HEADER_SIZE == sizeof (*udph));
|
|
|
|
|
2011-06-24 19:51:57 +00:00
|
|
|
err = grub_netbuff_push (nb, sizeof (*udph));
|
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
udph = (struct udphdr *) nb->data;
|
2011-07-08 12:41:52 +00:00
|
|
|
udph->src = grub_cpu_to_be16 (socket->in_port);
|
|
|
|
udph->dst = grub_cpu_to_be16 (socket->out_port);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
udph->chksum = 0;
|
2010-09-16 19:57:31 +00:00
|
|
|
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-10 06:46:48 +00:00
|
|
|
udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
|
|
|
|
&socket->inf->address,
|
|
|
|
&socket->out_nla);
|
|
|
|
|
2011-10-12 23:22:56 +00:00
|
|
|
return grub_net_send_ip_packet (socket->inf, &(socket->out_nla),
|
2011-10-13 18:53:22 +00:00
|
|
|
&(socket->ll_target_addr), nb,
|
|
|
|
GRUB_NET_IP_UDP);
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
2011-07-08 12:41:52 +00:00
|
|
|
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)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-04-01 08:12:44 +00:00
|
|
|
struct udphdr *udph;
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_udp_socket_t sock;
|
2011-07-02 18:11:29 +00:00
|
|
|
grub_err_t err;
|
2011-07-10 06:46:48 +00:00
|
|
|
|
2011-10-12 08:20:55 +00:00
|
|
|
/* Ignore broadcast. */
|
|
|
|
if (!inf)
|
|
|
|
{
|
|
|
|
grub_netbuff_free (nb);
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2011-04-01 08:12:44 +00:00
|
|
|
udph = (struct udphdr *) nb->data;
|
2011-07-10 06:46:48 +00:00
|
|
|
if (nb->tail - nb->data < (grub_ssize_t) sizeof (*udph))
|
|
|
|
{
|
|
|
|
grub_dprintf ("net", "UDP packet too short: %" PRIuGRUB_SIZE "\n",
|
2013-08-21 19:02:14 +00:00
|
|
|
(grub_size_t) (nb->tail - nb->data));
|
2011-07-10 06:46:48 +00:00
|
|
|
grub_netbuff_free (nb);
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
FOR_UDP_SOCKETS (sock)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-07-08 12:41:52 +00:00
|
|
|
if (grub_be_to_cpu16 (udph->dst) == sock->in_port
|
2011-07-10 06:46:48 +00:00
|
|
|
&& inf == sock->inf
|
2011-10-12 23:22:56 +00:00
|
|
|
&& grub_net_addr_cmp (source, &sock->out_nla) == 0
|
2011-07-10 06:46:48 +00:00
|
|
|
&& (sock->status == GRUB_NET_SOCKET_START
|
|
|
|
|| grub_be_to_cpu16 (udph->src) == sock->out_port))
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-07-10 06:46:48 +00:00
|
|
|
if (udph->chksum)
|
2011-07-02 20:13:33 +00:00
|
|
|
{
|
2011-07-10 06:46:48 +00:00
|
|
|
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;
|
2011-07-02 20:13:33 +00:00
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
if (sock->status == GRUB_NET_SOCKET_START)
|
2011-07-02 20:13:33 +00:00
|
|
|
{
|
2011-07-08 12:41:52 +00:00
|
|
|
sock->out_port = grub_be_to_cpu16 (udph->src);
|
|
|
|
sock->status = GRUB_NET_SOCKET_ESTABLISHED;
|
2011-07-02 20:13:33 +00:00
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-10 06:46:48 +00:00
|
|
|
err = grub_netbuff_pull (nb, sizeof (*udph));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
/* App protocol remove its own reader. */
|
2011-07-10 06:46:48 +00:00
|
|
|
if (sock->recv_hook)
|
|
|
|
sock->recv_hook (sock, nb, sock->recv_hook_data);
|
|
|
|
else
|
|
|
|
grub_netbuff_free (nb);
|
2011-06-08 00:59:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grub_netbuff_free (nb);
|
2010-09-16 19:57:31 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|