2011-10-05 20:15:30 +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/arp.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/net.h>
|
|
|
|
#include <grub/net/ethernet.h>
|
|
|
|
#include <grub/net/ip.h>
|
|
|
|
#include <grub/time.h>
|
|
|
|
|
2011-10-05 20:15:30 +00:00
|
|
|
/* ARP header operation codes */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARP_REQUEST = 1,
|
|
|
|
ARP_REPLY = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* IANA ARP constant to define hardware type as ethernet. */
|
|
|
|
GRUB_NET_ARPHRD_ETHERNET = 1
|
|
|
|
};
|
|
|
|
|
2015-03-20 20:14:23 +00:00
|
|
|
struct arppkt {
|
2011-10-05 20:15:30 +00:00
|
|
|
grub_uint16_t hrd;
|
|
|
|
grub_uint16_t pro;
|
|
|
|
grub_uint8_t hln;
|
|
|
|
grub_uint8_t pln;
|
|
|
|
grub_uint16_t op;
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_uint8_t sender_mac[6];
|
|
|
|
grub_uint32_t sender_ip;
|
|
|
|
grub_uint8_t recv_mac[6];
|
|
|
|
grub_uint32_t recv_ip;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-10-05 20:15:30 +00:00
|
|
|
|
2012-06-09 09:06:55 +00:00
|
|
|
static int have_pending;
|
|
|
|
static grub_uint32_t pending_req;
|
2011-10-05 20:15:30 +00:00
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_err_t
|
2011-10-12 21:15:02 +00:00
|
|
|
grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
|
|
|
|
const grub_net_network_level_address_t *proto_addr)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-04-01 08:42:34 +00:00
|
|
|
struct grub_net_buff nb;
|
2015-03-20 20:14:23 +00:00
|
|
|
struct arppkt *arp_packet;
|
|
|
|
grub_net_link_level_address_t target_mac_addr;
|
2011-06-18 23:20:53 +00:00
|
|
|
grub_err_t err;
|
2011-04-01 08:42:34 +00:00
|
|
|
int i;
|
2011-10-12 23:22:56 +00:00
|
|
|
grub_uint8_t *nbd;
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_uint8_t arp_data[128];
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2015-03-20 20:14:23 +00:00
|
|
|
if (proto_addr->type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BUG, "unsupported address family");
|
2011-10-05 20:15:30 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
/* Build a request packet. */
|
|
|
|
nb.head = arp_data;
|
|
|
|
nb.end = arp_data + sizeof (arp_data);
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_netbuff_clear (&nb);
|
|
|
|
grub_netbuff_reserve (&nb, 128);
|
2011-06-18 23:20:53 +00:00
|
|
|
|
2015-03-20 20:14:23 +00:00
|
|
|
err = grub_netbuff_push (&nb, sizeof (*arp_packet));
|
2011-06-24 19:51:57 +00:00
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
|
|
|
|
2015-03-20 20:14:23 +00:00
|
|
|
arp_packet = (struct arppkt *) nb.data;
|
|
|
|
arp_packet->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
|
|
|
|
arp_packet->hln = 6;
|
|
|
|
arp_packet->pro = grub_cpu_to_be16 (GRUB_NET_ETHERTYPE_IP);
|
|
|
|
arp_packet->pln = 4;
|
|
|
|
arp_packet->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
|
2011-04-01 08:42:34 +00:00
|
|
|
/* Sender hardware address. */
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6);
|
|
|
|
arp_packet->sender_ip = inf->address.ipv4;
|
|
|
|
grub_memset (arp_packet->recv_mac, 0, 6);
|
|
|
|
arp_packet->recv_ip = proto_addr->ipv4;
|
2010-09-16 19:57:31 +00:00
|
|
|
/* Target protocol address */
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_memset (&target_mac_addr.mac, 0xff, 6);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-10-12 23:22:56 +00:00
|
|
|
nbd = nb.data;
|
2015-03-20 20:14:23 +00:00
|
|
|
send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
|
2011-10-05 20:15:30 +00:00
|
|
|
for (i = 0; i < GRUB_NET_TRIES; i++)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-10-12 21:15:02 +00:00
|
|
|
if (grub_net_link_layer_resolve_check (inf, proto_addr))
|
|
|
|
return GRUB_ERR_NONE;
|
2012-06-09 09:06:55 +00:00
|
|
|
pending_req = proto_addr->ipv4;
|
|
|
|
have_pending = 0;
|
2014-01-21 13:03:51 +00:00
|
|
|
grub_net_poll_cards (GRUB_NET_INTERVAL + (i * GRUB_NET_INTERVAL_ADDITION),
|
|
|
|
&have_pending);
|
2011-10-12 21:15:02 +00:00
|
|
|
if (grub_net_link_layer_resolve_check (inf, proto_addr))
|
|
|
|
return GRUB_ERR_NONE;
|
2011-10-12 23:22:56 +00:00
|
|
|
nb.data = nbd;
|
2015-03-20 20:14:23 +00:00
|
|
|
send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
|
2011-05-19 13:39:34 +00:00
|
|
|
}
|
2011-04-01 08:42:34 +00:00
|
|
|
|
2011-10-12 21:15:02 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
2011-10-12 21:15:02 +00:00
|
|
|
grub_net_arp_receive (struct grub_net_buff *nb,
|
|
|
|
struct grub_net_card *card)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2015-03-20 20:14:23 +00:00
|
|
|
struct arppkt *arp_packet = (struct arppkt *) nb->data;
|
2011-10-05 20:15:30 +00:00
|
|
|
grub_net_network_level_address_t sender_addr, target_addr;
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_net_link_level_address_t sender_mac_addr;
|
2011-04-01 08:42:34 +00:00
|
|
|
struct grub_net_network_level_interface *inf;
|
2015-03-20 20:14:23 +00:00
|
|
|
|
|
|
|
if (arp_packet->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
|
|
|
|
|| arp_packet->pln != 4 || arp_packet->hln != 6
|
|
|
|
|| nb->tail - nb->data < (int) sizeof (*arp_packet))
|
2011-10-05 20:15:30 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2011-10-12 21:15:02 +00:00
|
|
|
|
2015-03-20 20:14:23 +00:00
|
|
|
sender_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
|
|
|
|
target_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
|
|
|
|
sender_addr.ipv4 = arp_packet->sender_ip;
|
|
|
|
target_addr.ipv4 = arp_packet->recv_ip;
|
|
|
|
if (arp_packet->sender_ip == pending_req)
|
|
|
|
have_pending = 1;
|
|
|
|
|
|
|
|
sender_mac_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
|
|
|
|
grub_memcpy (sender_mac_addr.mac, arp_packet->sender_mac,
|
|
|
|
sizeof (sender_mac_addr.mac));
|
|
|
|
grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
|
2010-09-22 22:45:39 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
|
|
|
|
{
|
|
|
|
/* Am I the protocol address target? */
|
2011-10-05 20:15:30 +00:00
|
|
|
if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
|
2015-03-20 20:14:23 +00:00
|
|
|
&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-10-05 20:15:30 +00:00
|
|
|
grub_net_link_level_address_t target;
|
2015-03-20 20:14:23 +00:00
|
|
|
struct grub_net_buff nb_reply;
|
|
|
|
struct arppkt *arp_reply;
|
|
|
|
grub_uint8_t arp_data[128];
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
nb_reply.head = arp_data;
|
|
|
|
nb_reply.end = arp_data + sizeof (arp_data);
|
|
|
|
grub_netbuff_clear (&nb_reply);
|
|
|
|
grub_netbuff_reserve (&nb_reply, 128);
|
|
|
|
|
|
|
|
err = grub_netbuff_push (&nb_reply, sizeof (*arp_packet));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
arp_reply = (struct arppkt *) nb_reply.data;
|
|
|
|
|
|
|
|
arp_reply->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
|
|
|
|
arp_reply->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
|
|
|
|
arp_reply->pln = 4;
|
|
|
|
arp_reply->hln = 6;
|
|
|
|
arp_reply->op = grub_cpu_to_be16_compile_time (ARP_REPLY);
|
|
|
|
arp_reply->sender_ip = arp_packet->recv_ip;
|
|
|
|
arp_reply->recv_ip = arp_packet->sender_ip;
|
|
|
|
arp_reply->hln = 6;
|
2011-10-05 20:15:30 +00:00
|
|
|
|
|
|
|
target.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
|
2015-03-20 20:14:23 +00:00
|
|
|
grub_memcpy (target.mac, arp_packet->sender_mac, 6);
|
|
|
|
grub_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6);
|
|
|
|
grub_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6);
|
2011-10-05 20:15:30 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
/* Change operation to REPLY and send packet */
|
2015-03-20 20:14:23 +00:00
|
|
|
send_ethernet_packet (inf, &nb_reply, target, GRUB_NET_ETHERTYPE_ARP);
|
2011-06-08 00:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|