net: fix ipv6 routing

ipv6 routing in grub2 is broken, we cannot talk to anything outside our local
network or anything that doesn't route in our global namespace.  This patch
fixes this by doing a couple of things

1) Read the router information off of the router advertisement.  If we have a
router lifetime we need to take the source address and create a route from it.

2) Changes the routing stuff slightly to allow you to specify a gateway _and_ an
interface.  Since the router advertisements come in on the link local address we
need to associate it with the global address on the card.  So when we are
processing the router advertisement, either use the SLAAC interface we create
and add the route to that interface, or loop through the global addresses we
currently have on our interface and associate it with one of those addresses.
We need to have a special case here for the default route so that it gets used,
we do this by setting the masksize to 0 to mean it encompasses all networks.
The routing code will automatically select the best route so if there is a
closer match we will use that.

With this patch I can now talk to ipv6 addresses outside of my local network.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
This commit is contained in:
Josef Bacik 2016-02-10 13:21:00 -08:00 committed by Andrei Borzenkov
parent 94c56a4c65
commit eb9f401fc1
5 changed files with 103 additions and 31 deletions

View file

@ -115,6 +115,7 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
grub_uint8_t ttl)
{
struct icmp_header *icmph;
struct grub_net_network_level_interface *orig_inf = inf;
grub_err_t err;
grub_uint16_t checksum;
@ -345,14 +346,31 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
{
grub_uint8_t *ptr;
struct option_header *ohdr;
struct router_adv *radv;
struct grub_net_network_level_interface *route_inf = NULL;
int default_route = 0;
if (icmph->code)
break;
radv = (struct router_adv *)nb->data;
err = grub_netbuff_pull (nb, sizeof (struct router_adv));
if (err)
{
grub_netbuff_free (nb);
return err;
}
if (grub_be_to_cpu16 (radv->router_lifetime) > 0)
{
struct grub_net_route *route;
FOR_NET_ROUTES (route)
{
if (!grub_memcmp (&route->gw, source, sizeof (route->gw)))
break;
}
if (route == NULL)
default_route = 1;
}
for (ptr = (grub_uint8_t *) nb->data; ptr < nb->tail;
ptr += ohdr->len * 8)
{
@ -413,7 +431,11 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
/* Update lease time if needed here once we have
lease times. */
if (inf)
continue;
{
if (!route_inf)
route_inf = inf;
continue;
}
grub_dprintf ("net", "creating slaac\n");
@ -429,12 +451,51 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
inf = grub_net_add_addr (name,
card, &addr,
&slaac->address, 0);
if (!route_inf)
route_inf = inf;
grub_net_add_route (name, netaddr, inf);
grub_free (name);
}
}
}
}
if (default_route)
{
char *name;
grub_net_network_level_netaddress_t netaddr;
name = grub_xasprintf ("%s:ra:default6", card->name);
if (!name)
{
grub_errno = GRUB_ERR_NONE;
goto next;
}
/* Default routes take alll of the traffic, so make the mask huge */
netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
netaddr.ipv6.masksize = 0;
netaddr.ipv6.base[0] = 0;
netaddr.ipv6.base[1] = 0;
/* May not have gotten slaac info, find a global address on this
card. */
if (route_inf == NULL)
{
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
if (inf->card == card && inf != orig_inf
&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
&& grub_net_hwaddr_cmp(&inf->hwaddress,
&orig_inf->hwaddress) == 0)
{
route_inf = inf;
break;
}
}
}
if (route_inf != NULL)
grub_net_add_route_gw (name, netaddr, *source, route_inf);
grub_free (name);
}
next:
if (ptr != nb->tail)
break;
}