Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / net / ethernet.c
blobc397b1b348ce559070cabe1e0b6e28e4dbf254d8
1 /*
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>
20 #include <grub/mm.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>
25 #include <grub/net.h>
26 #include <grub/time.h>
27 #include <grub/net/arp.h>
29 #define LLCADDRMASK 0x7f
31 struct etherhdr
33 grub_uint8_t dst[6];
34 grub_uint8_t src[6];
35 grub_uint16_t type;
36 } GRUB_PACKED;
38 struct llchdr
40 grub_uint8_t dsap;
41 grub_uint8_t ssap;
42 grub_uint8_t ctrl;
43 } GRUB_PACKED;
45 struct snaphdr
47 grub_uint8_t oui[3];
48 grub_uint16_t type;
49 } GRUB_PACKED;
51 grub_err_t
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)
57 struct etherhdr *eth;
58 grub_err_t err;
60 COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);
62 err = grub_netbuff_push (nb, sizeof (*eth));
63 if (err)
64 return err;
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)
72 err = GRUB_ERR_NONE;
73 if (inf->card->driver->open)
74 err = inf->card->driver->open (inf->card);
75 if (err)
76 return err;
77 inf->card->opened = 1;
79 return inf->card->driver->send (inf->card, nb);
82 grub_err_t
83 grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
84 struct grub_net_card *card)
86 struct etherhdr *eth;
87 struct llchdr *llch;
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;
92 grub_err_t err;
94 eth = (struct etherhdr *) nb->data;
95 type = grub_be_to_cpu16 (eth->type);
96 err = grub_netbuff_pull (nb, sizeof (*eth));
97 if (err)
98 return err;
100 if (type <= 1500)
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));
108 if (err)
109 return err;
110 snaph = (struct snaphdr *) nb->data;
111 type = snaph->type;
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));
120 switch (type)
122 /* ARP packet. */
123 case GRUB_NET_ETHERTYPE_ARP:
124 grub_net_arp_receive (nb, card);
125 grub_netbuff_free (nb);
126 return GRUB_ERR_NONE;
127 /* IP packet. */
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;