2011-07-06 10:53:37 +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/misc.h>
|
|
|
|
#include <grub/mm.h>
|
2013-12-23 14:32:02 +00:00
|
|
|
#include <grub/env.h>
|
2010-09-16 19:57:31 +00:00
|
|
|
#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-07-06 10:53:37 +00:00
|
|
|
#define LLCADDRMASK 0x7f
|
|
|
|
|
|
|
|
struct etherhdr
|
|
|
|
{
|
|
|
|
grub_uint8_t dst[6];
|
|
|
|
grub_uint8_t src[6];
|
|
|
|
grub_uint16_t type;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-07-06 10:53:37 +00:00
|
|
|
|
|
|
|
struct llchdr
|
|
|
|
{
|
|
|
|
grub_uint8_t dsap;
|
|
|
|
grub_uint8_t ssap;
|
|
|
|
grub_uint8_t ctrl;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-07-06 10:53:37 +00:00
|
|
|
|
|
|
|
struct snaphdr
|
|
|
|
{
|
|
|
|
grub_uint8_t oui[3];
|
|
|
|
grub_uint16_t type;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-07-06 10:53:37 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
2010-09-16 19:57:31 +00:00
|
|
|
send_ethernet_packet (struct grub_net_network_level_interface *inf,
|
|
|
|
struct grub_net_buff *nb,
|
|
|
|
grub_net_link_level_address_t target_addr,
|
2011-10-05 20:15:30 +00:00
|
|
|
grub_net_ethertype_t ethertype)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-06-08 00:59:53 +00:00
|
|
|
struct etherhdr *eth;
|
2011-06-18 23:20:53 +00:00
|
|
|
grub_err_t err;
|
2013-12-23 14:32:02 +00:00
|
|
|
grub_uint8_t etherhdr_size;
|
|
|
|
grub_uint16_t vlantag_id = VLANTAG_IDENTIFIER;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2013-12-23 14:32:02 +00:00
|
|
|
etherhdr_size = sizeof (*eth);
|
|
|
|
COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
|
2011-07-08 22:27:27 +00:00
|
|
|
|
2013-12-23 14:32:02 +00:00
|
|
|
/* Increase ethernet header in case of vlantag */
|
|
|
|
if (inf->vlantag != 0)
|
|
|
|
etherhdr_size += 4;
|
|
|
|
|
|
|
|
err = grub_netbuff_push (nb, etherhdr_size);
|
2011-06-24 19:51:57 +00:00
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
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);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
|
|
|
eth->type = grub_cpu_to_be16 (ethertype);
|
2011-07-05 14:37:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-12-23 14:32:02 +00:00
|
|
|
|
|
|
|
/* Check and add a vlan-tag if needed. */
|
|
|
|
if (inf->vlantag != 0)
|
|
|
|
{
|
|
|
|
/* Move eth type to the right */
|
|
|
|
grub_memcpy ((char *) nb->data + etherhdr_size - 2,
|
|
|
|
(char *) nb->data + etherhdr_size - 6, 2);
|
|
|
|
|
|
|
|
/* Add the tag in the middle */
|
|
|
|
grub_memcpy ((char *) nb->data + etherhdr_size - 6, &vlantag_id, 2);
|
|
|
|
grub_memcpy ((char *) nb->data + etherhdr_size - 4, (char *) &(inf->vlantag), 2);
|
|
|
|
}
|
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
return inf->card->driver->send (inf->card, nb);
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
2011-10-12 21:15:02 +00:00
|
|
|
grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
|
|
|
|
struct grub_net_card *card)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct etherhdr *eth;
|
|
|
|
struct llchdr *llch;
|
|
|
|
struct snaphdr *snaph;
|
2011-10-05 20:15:30 +00:00
|
|
|
grub_net_ethertype_t type;
|
2011-06-24 18:35:25 +00:00
|
|
|
grub_net_link_level_address_t hwaddress;
|
2011-10-13 18:53:22 +00:00
|
|
|
grub_net_link_level_address_t src_hwaddress;
|
2011-06-18 23:20:53 +00:00
|
|
|
grub_err_t err;
|
2013-12-23 14:32:02 +00:00
|
|
|
grub_uint8_t etherhdr_size = sizeof (*eth);
|
|
|
|
grub_uint16_t vlantag = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* Check if a vlan-tag is present. If so, the ethernet header is 4 bytes */
|
|
|
|
/* longer than the original one. The vlantag id is extracted and the header */
|
|
|
|
/* is reseted to the original size. */
|
|
|
|
if (grub_get_unaligned16 (nb->data + etherhdr_size - 2) == VLANTAG_IDENTIFIER)
|
|
|
|
{
|
|
|
|
vlantag = grub_get_unaligned16 (nb->data + etherhdr_size);
|
|
|
|
etherhdr_size += 4;
|
|
|
|
/* Move eth type to the original position */
|
|
|
|
grub_memcpy((char *) nb->data + etherhdr_size - 6,
|
|
|
|
(char *) nb->data + etherhdr_size - 2, 2);
|
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
|
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);
|
2013-12-23 14:32:02 +00:00
|
|
|
err = grub_netbuff_pull (nb, etherhdr_size);
|
2011-06-24 19:51:57 +00:00
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2010-09-22 22:45:39 +00:00
|
|
|
if (type <= 1500)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-06-24 19:51:57 +00:00
|
|
|
err = grub_netbuff_pull (nb, sizeof (*llch));
|
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2010-09-16 19:57:31 +00:00
|
|
|
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));
|
2011-10-13 18:53:22 +00:00
|
|
|
src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
|
|
|
|
grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
|
2011-06-24 18:35:25 +00:00
|
|
|
|
2011-10-05 20:15:30 +00:00
|
|
|
switch (type)
|
2011-04-01 08:42:34 +00:00
|
|
|
{
|
2011-10-05 20:15:30 +00:00
|
|
|
/* ARP packet. */
|
|
|
|
case GRUB_NET_ETHERTYPE_ARP:
|
2013-12-23 14:32:02 +00:00
|
|
|
grub_net_arp_receive (nb, card, &vlantag);
|
2011-04-01 08:42:34 +00:00
|
|
|
grub_netbuff_free (nb);
|
2011-06-26 02:18:45 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2011-10-05 20:15:30 +00:00
|
|
|
/* IP packet. */
|
|
|
|
case GRUB_NET_ETHERTYPE_IP:
|
|
|
|
case GRUB_NET_ETHERTYPE_IP6:
|
2013-12-23 14:32:02 +00:00
|
|
|
return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress,
|
|
|
|
&vlantag);
|
2011-06-26 02:18:45 +00:00
|
|
|
}
|
|
|
|
grub_netbuff_free (nb);
|
2011-06-08 00:59:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|