grub/grub-core/net/ethernet.c

91 lines
2.2 KiB
C
Raw Normal View History

#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/net/ethernet.h>
#include <grub/net/ip.h>
#include <grub/net/arp.h>
#include <grub/net/netbuff.h>
#include <grub/net.h>
#include <grub/time.h>
#include <grub/net/arp.h>
2011-06-08 00:59:53 +00:00
grub_err_t
send_ethernet_packet (struct grub_net_network_level_interface *inf,
struct grub_net_buff *nb,
grub_net_link_level_address_t target_addr,
grub_uint16_t ethertype)
{
2011-06-08 00:59:53 +00:00
struct etherhdr *eth;
grub_err_t err;
err = grub_netbuff_push (nb, sizeof (*eth));
if (err)
return err;
2011-06-08 00:59:53 +00:00
eth = (struct etherhdr *) nb->data;
grub_memcpy (eth->dst, target_addr.mac, 6);
grub_memcpy (eth->src, inf->hwaddress.mac, 6);
eth->type = grub_cpu_to_be16 (ethertype);
if (!inf->card->opened)
{
err = GRUB_ERR_NONE;
if (inf->card->driver->open)
err = inf->card->driver->open (inf->card);
if (err)
return err;
inf->card->opened = 1;
}
2011-04-01 08:42:34 +00:00
return inf->card->driver->send (inf->card, nb);
}
2011-06-08 00:59:53 +00:00
grub_err_t
2011-06-24 18:35:25 +00:00
grub_net_recv_ethernet_packet (struct grub_net_buff * nb,
const struct grub_net_card * card)
{
struct etherhdr *eth;
struct llchdr *llch;
struct snaphdr *snaph;
grub_uint16_t type;
2011-06-24 18:35:25 +00:00
grub_net_link_level_address_t hwaddress;
grub_err_t err;
2011-06-08 00:59:53 +00:00
eth = (struct etherhdr *) nb->data;
2010-09-22 22:45:39 +00:00
type = grub_be_to_cpu16 (eth->type);
err = grub_netbuff_pull (nb, sizeof (*eth));
if (err)
return err;
2011-06-08 00:59:53 +00:00
2010-09-22 22:45:39 +00:00
if (type <= 1500)
{
llch = (struct llchdr *) nb->data;
type = llch->dsap & LLCADDRMASK;
if (llch->dsap == 0xaa && llch->ssap == 0xaa && llch->ctrl == 0x3)
2011-06-08 00:59:53 +00:00
{
err = grub_netbuff_pull (nb, sizeof (*llch));
if (err)
return err;
snaph = (struct snaphdr *) nb->data;
type = snaph->type;
}
}
2011-06-24 18:35:25 +00:00
hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
/* ARP packet. */
2010-09-22 22:45:39 +00:00
if (type == GRUB_NET_ETHERTYPE_ARP)
2011-04-01 08:42:34 +00:00
{
grub_net_arp_receive (nb);
grub_netbuff_free (nb);
2011-06-26 02:18:45 +00:00
return GRUB_ERR_NONE;
2011-04-01 08:42:34 +00:00
}
2011-06-08 00:59:53 +00:00
/* IP packet. */
2011-04-01 08:42:34 +00:00
if (type == GRUB_NET_ETHERTYPE_IP)
2011-06-26 02:18:45 +00:00
{
grub_net_recv_ip_packets (nb, card, &hwaddress);
return GRUB_ERR_NONE;
}
grub_netbuff_free (nb);
2011-06-08 00:59:53 +00:00
return GRUB_ERR_NONE;
}