grub/grub-core/net/ip.c

119 lines
2.8 KiB
C
Raw Normal View History

#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>
#include <grub/net/ethernet.h>
#include <grub/net.h>
#include <grub/net/netbuff.h>
#include <grub/mm.h>
grub_uint16_t
2011-06-08 00:59:53 +00:00
ipchksum (void *ipv, int len)
{
2011-06-08 00:59:53 +00:00
grub_uint16_t *ip = (grub_uint16_t *) ipv;
grub_uint32_t sum = 0;
2011-06-08 00:59:53 +00:00
len >>= 1;
while (len--)
{
sum += grub_be_to_cpu16 (*(ip++));
if (sum > 0xFFFF)
sum -= 0xFFFF;
}
2011-06-08 00:59:53 +00:00
return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
}
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,
struct grub_net_buff * nb)
2011-06-08 00:59:53 +00:00
{
struct iphdr *iph;
2011-06-08 00:59:53 +00:00
static int id = 0x2400;
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;
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);
iph->ident = grub_cpu_to_be16 (++id);
iph->frags = 0;
iph->ttl = 0xff;
iph->protocol = 0x11;
iph->src = inf->address.ipv4;
iph->dest = target->ipv4;
2011-06-08 00:59:53 +00:00
iph->chksum = 0;
iph->chksum = ipchksum ((void *) nb->data, sizeof (*iph));
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);
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);
}
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)
{
struct iphdr *iph = (struct iphdr *) nb->data;
grub_err_t err;
2011-06-24 18:35:25 +00:00
struct grub_net_network_level_interface *inf;
2011-06-24 18:35:25 +00:00
err = grub_netbuff_pull (nb, sizeof (*iph));
if (err)
2011-06-24 18:35:25 +00:00
return err;
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
if (inf->card == card
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4
&& inf->address.ipv4 == iph->dest
&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
break;
}
if (!inf)
{
2011-06-24 18:35:25 +00:00
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
if (inf->card == card
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_PROMISC
&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
break;
}
if (!inf)
{
if (iph->protocol == IP_UDP
&& grub_net_hwaddr_cmp (&card->default_address, hwaddress) == 0)
{
struct udphdr *udph;
udph = (struct udphdr *) nb->data;
err = grub_netbuff_pull (nb, sizeof (*udph));
if (err)
return err;
2011-06-24 18:35:25 +00:00
if (grub_be_to_cpu16 (udph->dst) == 68)
grub_net_process_dhcp (nb, card);
}
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
2011-04-01 08:42:34 +00:00
switch (iph->protocol)
{
2011-06-08 00:59:53 +00:00
case IP_UDP:
2011-06-24 18:35:25 +00:00
return grub_net_recv_udp_packet (nb, inf);
2011-04-01 08:42:34 +00:00
break;
2011-06-08 00:59:53 +00:00
default:
grub_netbuff_free (nb);
2011-04-01 08:42:34 +00:00
break;
}
return GRUB_ERR_NONE;
}