2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010,2011 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/misc.h>
21 #include <grub/net/ethernet.h>
22 #include <grub/net/ip.h>
23 #include <grub/net/arp.h>
24 #include <grub/net/netbuff.h>
26 #include <grub/time.h>
27 #include <grub/net/arp.h>
29 #define LLCADDRMASK 0x7f
52 send_ethernet_packet (struct grub_net_network_level_interface
*inf
,
53 struct grub_net_buff
*nb
,
54 grub_net_link_level_address_t target_addr
,
55 grub_net_ethertype_t ethertype
)
60 COMPILE_TIME_ASSERT (sizeof (*eth
) < GRUB_NET_MAX_LINK_HEADER_SIZE
);
62 err
= grub_netbuff_push (nb
, sizeof (*eth
));
65 eth
= (struct etherhdr
*) nb
->data
;
66 grub_memcpy (eth
->dst
, target_addr
.mac
, 6);
67 grub_memcpy (eth
->src
, inf
->hwaddress
.mac
, 6);
69 eth
->type
= grub_cpu_to_be16 (ethertype
);
70 if (!inf
->card
->opened
)
73 if (inf
->card
->driver
->open
)
74 err
= inf
->card
->driver
->open (inf
->card
);
77 inf
->card
->opened
= 1;
79 return inf
->card
->driver
->send (inf
->card
, nb
);
83 grub_net_recv_ethernet_packet (struct grub_net_buff
*nb
,
84 struct grub_net_card
*card
)
88 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
;
94 eth
= (struct etherhdr
*) nb
->data
;
95 type
= grub_be_to_cpu16 (eth
->type
);
96 err
= grub_netbuff_pull (nb
, sizeof (*eth
));
102 llch
= (struct llchdr
*) nb
->data
;
103 type
= llch
->dsap
& LLCADDRMASK
;
105 if (llch
->dsap
== 0xaa && llch
->ssap
== 0xaa && llch
->ctrl
== 0x3)
107 err
= grub_netbuff_pull (nb
, sizeof (*llch
));
110 snaph
= (struct snaphdr
*) nb
->data
;
115 hwaddress
.type
= GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
;
116 grub_memcpy (hwaddress
.mac
, eth
->dst
, sizeof (hwaddress
.mac
));
117 src_hwaddress
.type
= GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
;
118 grub_memcpy (src_hwaddress
.mac
, eth
->src
, sizeof (src_hwaddress
.mac
));
123 case GRUB_NET_ETHERTYPE_ARP
:
124 grub_net_arp_receive (nb
, card
);
125 grub_netbuff_free (nb
);
126 return GRUB_ERR_NONE
;
128 case GRUB_NET_ETHERTYPE_IP
:
129 case GRUB_NET_ETHERTYPE_IP6
:
130 return grub_net_recv_ip_packets (nb
, card
, &hwaddress
, &src_hwaddress
);
132 grub_netbuff_free (nb
);
133 return GRUB_ERR_NONE
;