more ipv6 code. Now ipv6 ping succeeds

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2011-10-12 23:15:02 +02:00
parent a53cf6534d
commit 7c006811f8
9 changed files with 541 additions and 141 deletions

View file

@ -43,10 +43,15 @@ struct router_adv
grub_uint8_t options[0];
} __attribute__ ((packed));
struct prefix_option
struct option_header
{
grub_uint8_t type;
grub_uint8_t len;
} __attribute__ ((packed));
struct prefix_option
{
struct option_header header;
grub_uint8_t prefixlen;
grub_uint8_t flags;
grub_uint32_t valid_lifetime;
@ -55,6 +60,18 @@ struct prefix_option
grub_uint64_t prefix[2];
} __attribute__ ((packed));
struct neighbour_solicit
{
grub_uint32_t reserved;
grub_uint64_t target[2];
} __attribute__ ((packed));
struct neighbour_advertise
{
grub_uint32_t flags;
grub_uint64_t target[2];
} __attribute__ ((packed));
enum
{
FLAG_SLAAC = 0x40
@ -64,7 +81,22 @@ enum
{
ICMP6_ECHO = 128,
ICMP6_ECHO_REPLY = 129,
ICMP6_ROUTER_ADVERTISE = 134
ICMP6_ROUTER_ADVERTISE = 134,
ICMP6_NEIGHBOUR_SOLICIT = 135,
ICMP6_NEIGHBOUR_ADVERTISE = 136,
};
enum
{
OPTION_SOURCE_LINK_LAYER_ADDRESS = 1,
OPTION_TARGET_LINK_LAYER_ADDRESS = 2,
OPTION_PREFIX = 3
};
enum
{
FLAG_SOLICITED = (1 << 30),
FLAG_OVERRIDE = (1 << 29)
};
grub_err_t
@ -72,7 +104,8 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
struct grub_net_card *card,
struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *source,
const grub_net_network_level_address_t *dest)
const grub_net_network_level_address_t *dest,
grub_uint8_t ttl)
{
struct icmp_header *icmph;
grub_err_t err;
@ -107,7 +140,10 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
err = grub_netbuff_pull (nb, sizeof (*icmph));
if (err)
return err;
{
grub_netbuff_free (nb);
return err;
}
grub_dprintf ("net", "ICMPv6 message: %02x, %02x\n",
icmph->type, icmph->code);
@ -154,24 +190,180 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
grub_netbuff_free (nb_reply);
return err;
}
case ICMP6_ROUTER_ADVERTISE:
case ICMP6_NEIGHBOUR_SOLICIT:
{
struct neighbour_solicit *nbh;
struct grub_net_buff *nb_reply;
struct option_header *ohdr;
struct neighbour_advertise *adv;
struct icmp_header *icmphr;
grub_uint8_t *ptr;
if (icmph->code)
break;
if (ttl != 0xff)
break;
nbh = (struct neighbour_solicit *) nb->data;
err = grub_netbuff_pull (nb, sizeof (struct router_adv));
if (err)
return err;
for (ptr = (grub_uint8_t *) nb->data + sizeof (struct router_adv);
ptr < nb->tail; )
{
grub_dprintf ("net", "option %02x, %02x\n", ptr[0], ptr[1]);
if (ptr + 2 >= nb->tail || ptr[1] == 0)
grub_netbuff_free (nb);
return err;
}
for (ptr = (grub_uint8_t *) nb->data; ptr < nb->tail;
ptr += ohdr->len * 8)
{
ohdr = (struct option_header *) ptr;
if (ohdr->len == 0 || ptr + 8 * ohdr->len > nb->tail)
{
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
if (ptr[0] == 3 && ptr[1] == 4)
if (ohdr->type == OPTION_SOURCE_LINK_LAYER_ADDRESS
&& ohdr->len == 1)
{
grub_net_link_level_address_t ll_address;
ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
grub_net_link_layer_add_address (card, source, &ll_address, 0);
}
}
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
if (inf->card == card
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
&& grub_memcmp (&inf->address.ipv6, &nbh->target, 16) == 0)
break;
}
if (!inf)
break;
nb_reply = grub_netbuff_alloc (sizeof (struct neighbour_advertise)
+ sizeof (struct option_header)
+ 6
+ sizeof (struct icmp_header)
+ GRUB_NET_OUR_IPV6_HEADER_SIZE
+ GRUB_NET_MAX_LINK_HEADER_SIZE);
if (!nb_reply)
{
grub_netbuff_free (nb);
return grub_errno;
}
err = grub_netbuff_reserve (nb_reply,
sizeof (struct neighbour_advertise)
+ sizeof (struct option_header)
+ 6
+ sizeof (struct icmp_header)
+ GRUB_NET_OUR_IPV6_HEADER_SIZE
+ GRUB_NET_MAX_LINK_HEADER_SIZE);
if (err)
goto ndp_fail;
err = grub_netbuff_push (nb_reply, 6);
if (err)
goto ndp_fail;
grub_memcpy (nb_reply->data, inf->hwaddress.mac, 6);
err = grub_netbuff_push (nb_reply, sizeof (*ohdr));
if (err)
goto ndp_fail;
ohdr = (struct option_header *) nb_reply->data;
ohdr->type = OPTION_TARGET_LINK_LAYER_ADDRESS;
ohdr->len = 1;
err = grub_netbuff_push (nb_reply, sizeof (*adv));
if (err)
goto ndp_fail;
adv = (struct neighbour_advertise *) nb_reply->data;
adv->flags = grub_cpu_to_be32_compile_time (FLAG_SOLICITED
| FLAG_OVERRIDE);
grub_memcpy (&adv->target, &nbh->target, 16);
err = grub_netbuff_push (nb_reply, sizeof (*icmphr));
if (err)
goto ndp_fail;
icmphr = (struct icmp_header *) nb_reply->data;
icmphr->type = ICMP6_NEIGHBOUR_ADVERTISE;
icmphr->code = 0;
icmphr->checksum = 0;
icmphr->checksum = grub_net_ip_transport_checksum (nb_reply,
GRUB_NET_IP_ICMPV6,
&inf->address,
source);
err = grub_net_send_ip_packet (inf, source, nb_reply,
GRUB_NET_IP_ICMPV6);
ndp_fail:
grub_netbuff_free (nb);
grub_netbuff_free (nb_reply);
return err;
}
case ICMP6_NEIGHBOUR_ADVERTISE:
{
struct neighbour_advertise *nbh;
grub_uint8_t *ptr;
struct option_header *ohdr;
if (icmph->code)
break;
if (ttl != 0xff)
break;
nbh = (struct neighbour_advertise *) nb->data;
err = grub_netbuff_pull (nb, sizeof (*nbh));
if (err)
{
grub_netbuff_free (nb);
return err;
}
for (ptr = (grub_uint8_t *) nb->data; ptr < nb->tail;
ptr += ohdr->len * 8)
{
ohdr = (struct option_header *) ptr;
if (ohdr->len == 0 || ptr + 8 * ohdr->len > nb->tail)
{
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
if (ohdr->type == OPTION_TARGET_LINK_LAYER_ADDRESS
&& ohdr->len == 1)
{
grub_net_link_level_address_t ll_address;
ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
grub_net_link_layer_add_address (card, source, &ll_address, 0);
}
}
break;
}
case ICMP6_ROUTER_ADVERTISE:
{
grub_uint8_t *ptr;
struct option_header *ohdr;
if (icmph->code)
break;
err = grub_netbuff_pull (nb, sizeof (struct router_adv));
if (err)
{
grub_netbuff_free (nb);
return err;
}
for (ptr = (grub_uint8_t *) nb->data; ptr < nb->tail;
ptr += ohdr->len * 8)
{
ohdr = (struct option_header *) ptr;
if (ohdr->len == 0 || ptr + 8 * ohdr->len > nb->tail)
{
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
if (ohdr->type == OPTION_SOURCE_LINK_LAYER_ADDRESS
&& ohdr->len == 1)
{
grub_net_link_level_address_t ll_address;
ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
grub_net_link_layer_add_address (card, source, &ll_address, 0);
}
if (ohdr->type == OPTION_PREFIX && ohdr->len == 4)
{
struct prefix_option *opt = (struct prefix_option *) ptr;
struct grub_net_slaac_mac_list *slaac;
@ -181,7 +373,6 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
> grub_be_to_cpu32 (opt->valid_lifetime))
|| opt->prefixlen != 64)
{
ptr += ptr[1] * 8;
grub_dprintf ("net", "discarded prefix: %d, %d, %d, %d\n",
!(opt->flags & FLAG_SLAAC),
(grub_be_to_cpu64 (opt->prefix[0]) >> 48) == 0xfe80,
@ -223,7 +414,6 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
}
}
}
ptr += ptr[1] * 8;
}
if (ptr != nb->tail)
break;
@ -233,3 +423,91 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
grub_netbuff_free (nb);
return GRUB_ERR_NONE;
}
grub_err_t
grub_net_icmp6_send_request (struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *proto_addr)
{
struct grub_net_buff *nb;
grub_err_t err = GRUB_ERR_NONE;
int i;
struct option_header *ohdr;
struct neighbour_solicit *sol;
struct icmp_header *icmphr;
grub_net_network_level_address_t multicast;
multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
multicast.ipv6[0] = grub_be_to_cpu64_compile_time (0xff02ULL << 48);
multicast.ipv6[1] = (grub_be_to_cpu64_compile_time (0x01ff000000ULL)
| (proto_addr->ipv6[1]
& grub_be_to_cpu64_compile_time (0xffffff)));
nb = grub_netbuff_alloc (sizeof (struct neighbour_solicit)
+ sizeof (struct option_header)
+ 6
+ sizeof (struct icmp_header)
+ GRUB_NET_OUR_IPV6_HEADER_SIZE
+ GRUB_NET_MAX_LINK_HEADER_SIZE);
if (!nb)
return grub_errno;
err = grub_netbuff_reserve (nb,
sizeof (struct neighbour_solicit)
+ sizeof (struct option_header)
+ 6
+ sizeof (struct icmp_header)
+ GRUB_NET_OUR_IPV6_HEADER_SIZE
+ GRUB_NET_MAX_LINK_HEADER_SIZE);
err = grub_netbuff_push (nb, 6);
if (err)
goto fail;
grub_memcpy (nb->data, inf->hwaddress.mac, 6);
err = grub_netbuff_push (nb, sizeof (*ohdr));
if (err)
goto fail;
ohdr = (struct option_header *) nb->data;
ohdr->type = OPTION_TARGET_LINK_LAYER_ADDRESS;
ohdr->len = 1;
err = grub_netbuff_push (nb, sizeof (*sol));
if (err)
goto fail;
sol = (struct neighbour_solicit *) nb->data;
sol->reserved = 0;
grub_memcpy (&sol->target, &proto_addr->ipv6, 16);
err = grub_netbuff_push (nb, sizeof (*icmphr));
if (err)
goto fail;
icmphr = (struct icmp_header *) nb->data;
icmphr->type = ICMP6_NEIGHBOUR_ADVERTISE;
icmphr->code = 0;
icmphr->checksum = 0;
icmphr->checksum = grub_net_ip_transport_checksum (nb,
GRUB_NET_IP_ICMPV6,
&inf->address,
&multicast);
err = grub_net_send_ip_packet (inf, &multicast, nb,
GRUB_NET_IP_ICMPV6);
if (err)
goto fail;
for (i = 0; i < GRUB_NET_TRIES; i++)
{
if (grub_net_link_layer_resolve_check (inf, proto_addr))
break;
grub_net_poll_cards (GRUB_NET_INTERVAL);
if (grub_net_link_layer_resolve_check (inf, proto_addr))
break;
err = grub_net_send_ip_packet (inf, &multicast, nb,
GRUB_NET_IP_ICMPV6);
if (err)
break;
}
fail:
grub_netbuff_free (nb);
return err;
}