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/net/ip.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/net/arp.h>
|
2011-04-01 08:42:34 +00:00
|
|
|
#include <grub/net/udp.h>
|
2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/net/ethernet.h>
|
|
|
|
#include <grub/net.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
|
2011-07-06 10:53:37 +00:00
|
|
|
struct iphdr {
|
|
|
|
grub_uint8_t verhdrlen;
|
|
|
|
grub_uint8_t service;
|
|
|
|
grub_uint16_t len;
|
|
|
|
grub_uint16_t ident;
|
|
|
|
grub_uint16_t frags;
|
|
|
|
grub_uint8_t ttl;
|
|
|
|
grub_uint8_t protocol;
|
|
|
|
grub_uint16_t chksum;
|
|
|
|
grub_uint32_t src;
|
|
|
|
grub_uint32_t dest;
|
|
|
|
} __attribute__ ((packed)) ;
|
|
|
|
|
|
|
|
struct ip6hdr
|
|
|
|
{
|
|
|
|
grub_uint8_t version:4, priority:4;
|
|
|
|
grub_uint8_t flow_lbl[3];
|
|
|
|
grub_uint16_t payload_len;
|
|
|
|
grub_uint8_t nexthdr;
|
|
|
|
grub_uint8_t hop_limit;
|
|
|
|
grub_uint8_t saddr[16];
|
|
|
|
grub_uint8_t daddr[16];
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_uint16_t
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_ip_chksum (void *ipv, grub_size_t len)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_uint16_t *ip = (grub_uint16_t *) ipv;
|
|
|
|
grub_uint32_t sum = 0;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
for (; len >= 2; len -= 2)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
|
|
|
sum += grub_be_to_cpu16 (*(ip++));
|
|
|
|
if (sum > 0xFFFF)
|
|
|
|
sum -= 0xFFFF;
|
|
|
|
}
|
2011-07-08 12:41:52 +00:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
sum += *((grub_uint8_t *) ip) << 8;
|
|
|
|
if (sum > 0xFFFF)
|
|
|
|
sum -= 0xFFFF;
|
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
2011-06-24 18:35:25 +00:00
|
|
|
grub_net_send_ip_packet (struct grub_net_network_level_interface * inf,
|
|
|
|
const grub_net_network_level_address_t * target,
|
2011-07-08 12:41:52 +00:00
|
|
|
struct grub_net_buff * nb,
|
|
|
|
grub_net_ip_protocol_t proto)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2010-09-16 19:57:31 +00:00
|
|
|
struct iphdr *iph;
|
2011-06-08 00:59:53 +00:00
|
|
|
static int id = 0x2400;
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_net_link_level_address_t ll_target_addr;
|
|
|
|
grub_err_t err;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
grub_netbuff_push (nb, sizeof (*iph));
|
|
|
|
iph = (struct iphdr *) nb->data;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
|
|
|
iph->verhdrlen = ((4 << 4) | 5);
|
|
|
|
iph->service = 0;
|
2011-06-08 00:59:53 +00:00
|
|
|
iph->len = grub_cpu_to_be16 (nb->tail - nb->data);
|
2010-09-16 19:57:31 +00:00
|
|
|
iph->ident = grub_cpu_to_be16 (++id);
|
|
|
|
iph->frags = 0;
|
|
|
|
iph->ttl = 0xff;
|
2011-07-08 12:41:52 +00:00
|
|
|
iph->protocol = proto;
|
2010-09-16 19:57:31 +00:00
|
|
|
iph->src = inf->address.ipv4;
|
|
|
|
iph->dest = target->ipv4;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
iph->chksum = 0;
|
2011-07-06 10:53:37 +00:00
|
|
|
iph->chksum = grub_net_ip_chksum ((void *) nb->data, sizeof (*iph));
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
/* Determine link layer target address via ARP. */
|
|
|
|
err = grub_net_arp_resolve (inf, target, &ll_target_addr);
|
2010-09-16 19:57:31 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2011-06-24 18:35:25 +00:00
|
|
|
return send_ethernet_packet (inf, nb, ll_target_addr,
|
|
|
|
GRUB_NET_ETHERTYPE_IP);
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_net_recv_ip_packets (struct grub_net_buff * nb,
|
|
|
|
const struct grub_net_card * card,
|
|
|
|
const grub_net_link_level_address_t * hwaddress)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct iphdr *iph = (struct iphdr *) nb->data;
|
|
|
|
grub_err_t err;
|
2011-07-02 18:11:29 +00:00
|
|
|
struct grub_net_network_level_interface *inf = NULL;
|
2011-07-08 12:41:52 +00:00
|
|
|
grub_net_network_level_address_t source;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
err = grub_netbuff_pull (nb, sizeof (*iph));
|
2010-09-16 19:57:31 +00:00
|
|
|
if (err)
|
2011-06-24 18:35:25 +00:00
|
|
|
return err;
|
|
|
|
|
2011-07-02 18:11:29 +00:00
|
|
|
/* DHCP needs special treatment since we don't know IP yet. */
|
2011-06-24 18:35:25 +00:00
|
|
|
{
|
2011-07-02 18:11:29 +00:00
|
|
|
struct udphdr *udph;
|
|
|
|
udph = (struct udphdr *) nb->data;
|
2011-07-08 12:41:52 +00:00
|
|
|
if (iph->protocol == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
|
2011-07-02 18:11:29 +00:00
|
|
|
{
|
|
|
|
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
|
|
|
|
if (inf->card == card
|
|
|
|
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
|
|
|
|
&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
|
|
|
|
{
|
|
|
|
err = grub_netbuff_pull (nb, sizeof (*udph));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
grub_net_process_dhcp (nb, inf->card);
|
|
|
|
grub_netbuff_free (nb);
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2011-06-24 18:35:25 +00:00
|
|
|
}
|
2011-07-02 18:11:29 +00:00
|
|
|
|
2011-06-24 18:35:25 +00:00
|
|
|
if (!inf)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-06-24 18:35:25 +00:00
|
|
|
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
|
2011-07-02 18:11:29 +00:00
|
|
|
{
|
2011-06-24 18:35:25 +00:00
|
|
|
if (inf->card == card
|
2011-07-02 18:11:29 +00:00
|
|
|
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4
|
|
|
|
&& inf->address.ipv4 == iph->dest
|
2011-06-24 18:35:25 +00:00
|
|
|
&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
|
2011-07-02 18:11:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
2011-04-01 08:42:34 +00:00
|
|
|
|
2011-07-08 12:41:52 +00:00
|
|
|
source.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
|
|
|
|
source.ipv4 = iph->src;
|
|
|
|
|
2011-04-01 08:42:34 +00:00
|
|
|
switch (iph->protocol)
|
|
|
|
{
|
2011-07-08 12:41:52 +00:00
|
|
|
case GRUB_NET_IP_UDP:
|
|
|
|
return grub_net_recv_udp_packet (nb, inf, &source);
|
|
|
|
case GRUB_NET_IP_ICMP:
|
|
|
|
return grub_net_recv_icmp_packet (nb, inf, &source);
|
2011-06-08 00:59:53 +00:00
|
|
|
default:
|
|
|
|
grub_netbuff_free (nb);
|
2011-04-01 08:42:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|