ethernet.c 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  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/>.
 */

19 20 21 22 23 24 25 26 27 28
#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>

29 30 31 32 33 34 35
#define LLCADDRMASK 0x7f

struct etherhdr
{
  grub_uint8_t dst[6];
  grub_uint8_t src[6];
  grub_uint16_t type;
36
} GRUB_PACKED;
37 38 39 40 41 42

struct llchdr
{
  grub_uint8_t dsap;
  grub_uint8_t ssap;
  grub_uint8_t ctrl;
43
} GRUB_PACKED;
44 45 46 47 48

struct snaphdr
{
  grub_uint8_t oui[3]; 
  grub_uint16_t type;
49
} GRUB_PACKED;
50

51
grub_err_t
52 53 54
send_ethernet_packet (struct grub_net_network_level_interface *inf,
		      struct grub_net_buff *nb,
		      grub_net_link_level_address_t target_addr,
55
		      grub_net_ethertype_t ethertype)
56
{
57
  struct etherhdr *eth;
58
  grub_err_t err;
59

60 61
  COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);

62 63
  err = grub_netbuff_push (nb, sizeof (*eth));
  if (err)
64
    return err;
65 66 67
  eth = (struct etherhdr *) nb->data;
  grub_memcpy (eth->dst, target_addr.mac, 6);
  grub_memcpy (eth->src, inf->hwaddress.mac, 6);
68 69

  eth->type = grub_cpu_to_be16 (ethertype);
70 71 72 73 74 75 76 77 78
  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;
    }
79
  return inf->card->driver->send (inf->card, nb);
80 81
}

82
grub_err_t
83 84
grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
			       struct grub_net_card *card)
85 86 87 88
{
  struct etherhdr *eth;
  struct llchdr *llch;
  struct snaphdr *snaph;
89
  grub_net_ethertype_t type;
90
  grub_net_link_level_address_t hwaddress;
91
  grub_net_link_level_address_t src_hwaddress;
92
  grub_err_t err;
93

94
  eth = (struct etherhdr *) nb->data;
95
  type = grub_be_to_cpu16 (eth->type);
96 97
  err = grub_netbuff_pull (nb, sizeof (*eth));
  if (err)
98
    return err;
99

100
  if (type <= 1500)
101 102 103 104 105
    {
      llch = (struct llchdr *) nb->data;
      type = llch->dsap & LLCADDRMASK;

      if (llch->dsap == 0xaa && llch->ssap == 0xaa && llch->ctrl == 0x3)
106
	{
107 108
	  err = grub_netbuff_pull (nb, sizeof (*llch));
	  if (err)
109
	    return err;
110 111 112 113 114
	  snaph = (struct snaphdr *) nb->data;
	  type = snaph->type;
	}
    }

115 116
  hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
117 118
  src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
119

120
  switch (type)
121
    {
122 123
      /* ARP packet. */
    case GRUB_NET_ETHERTYPE_ARP:
124
      grub_net_arp_receive (nb, card);
125
      grub_netbuff_free (nb);
126
      return GRUB_ERR_NONE;
127 128 129
      /* IP packet.  */
    case GRUB_NET_ETHERTYPE_IP:
    case GRUB_NET_ETHERTYPE_IP6:
130
      return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress);
131 132
    }
  grub_netbuff_free (nb);
133
  return GRUB_ERR_NONE;
134
}