vaseboot/VasEBoot-core/net/net.c

1828 lines
49 KiB
C

/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2010,2011,2012,2013 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/net.h>
#include <VasEBoot/net/netbuff.h>
#include <VasEBoot/time.h>
#include <VasEBoot/file.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/command.h>
#include <VasEBoot/env.h>
#include <VasEBoot/net/ethernet.h>
#include <VasEBoot/net/arp.h>
#include <VasEBoot/net/ip.h>
#include <VasEBoot/loader.h>
#include <VasEBoot/bufio.h>
#include <VasEBoot/kernel.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
char *VasEBoot_net_default_server;
struct VasEBoot_net_route *VasEBoot_net_routes = NULL;
struct VasEBoot_net_network_level_interface *VasEBoot_net_network_level_interfaces = NULL;
struct VasEBoot_net_card *VasEBoot_net_cards = NULL;
struct VasEBoot_net_network_level_protocol *VasEBoot_net_network_level_protocols = NULL;
static struct VasEBoot_fs VasEBoot_net_fs;
struct VasEBoot_net_link_layer_entry {
int avail;
VasEBoot_net_network_level_address_t nl_address;
VasEBoot_net_link_level_address_t ll_address;
};
#define LINK_LAYER_CACHE_SIZE 256
static struct VasEBoot_net_link_layer_entry *
link_layer_find_entry (const VasEBoot_net_network_level_address_t *proto,
const struct VasEBoot_net_card *card)
{
unsigned i;
if (!card->link_layer_table)
return NULL;
for (i = 0; i < LINK_LAYER_CACHE_SIZE; i++)
{
if (card->link_layer_table[i].avail == 1
&& VasEBoot_net_addr_cmp (&card->link_layer_table[i].nl_address,
proto) == 0)
return &card->link_layer_table[i];
}
return NULL;
}
void
VasEBoot_net_link_layer_add_address (struct VasEBoot_net_card *card,
const VasEBoot_net_network_level_address_t *nl,
const VasEBoot_net_link_level_address_t *ll,
int override)
{
struct VasEBoot_net_link_layer_entry *entry;
/* Check if the sender is in the cache table. */
entry = link_layer_find_entry (nl, card);
/* Update sender hardware address. */
if (entry && override)
VasEBoot_memcpy (&entry->ll_address, ll, sizeof (entry->ll_address));
if (entry)
return;
/* Add sender to cache table. */
if (card->link_layer_table == NULL)
card->link_layer_table = VasEBoot_zalloc (LINK_LAYER_CACHE_SIZE
* sizeof (card->link_layer_table[0]));
entry = &(card->link_layer_table[card->new_ll_entry]);
entry->avail = 1;
VasEBoot_memcpy (&entry->ll_address, ll, sizeof (entry->ll_address));
VasEBoot_memcpy (&entry->nl_address, nl, sizeof (entry->nl_address));
card->new_ll_entry++;
if (card->new_ll_entry == LINK_LAYER_CACHE_SIZE)
card->new_ll_entry = 0;
}
int
VasEBoot_net_link_layer_resolve_check (struct VasEBoot_net_network_level_interface *inf,
const VasEBoot_net_network_level_address_t *proto_addr)
{
struct VasEBoot_net_link_layer_entry *entry;
if (proto_addr->type == VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4
&& proto_addr->ipv4 == 0xffffffff)
return 1;
entry = link_layer_find_entry (proto_addr, inf->card);
if (entry)
return 1;
return 0;
}
VasEBoot_err_t
VasEBoot_net_link_layer_resolve (struct VasEBoot_net_network_level_interface *inf,
const VasEBoot_net_network_level_address_t *proto_addr,
VasEBoot_net_link_level_address_t *hw_addr)
{
struct VasEBoot_net_link_layer_entry *entry;
VasEBoot_err_t err;
if ((proto_addr->type == VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4
&& proto_addr->ipv4 == 0xffffffff)
|| proto_addr->type == VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
|| (proto_addr->type == VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6
&& proto_addr->ipv6[0] == VasEBoot_be_to_cpu64_compile_time (0xff02ULL
<< 48)
&& proto_addr->ipv6[1] == (VasEBoot_be_to_cpu64_compile_time (1))))
{
hw_addr->type = VasEBoot_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
VasEBoot_memset (hw_addr->mac, -1, 6);
return VasEBoot_ERR_NONE;
}
if (proto_addr->type == VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6
&& ((VasEBoot_be_to_cpu64 (proto_addr->ipv6[0]) >> 56) == 0xff))
{
hw_addr->type = VasEBoot_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
hw_addr->mac[0] = 0x33;
hw_addr->mac[1] = 0x33;
hw_addr->mac[2] = ((VasEBoot_be_to_cpu64 (proto_addr->ipv6[1]) >> 24) & 0xff);
hw_addr->mac[3] = ((VasEBoot_be_to_cpu64 (proto_addr->ipv6[1]) >> 16) & 0xff);
hw_addr->mac[4] = ((VasEBoot_be_to_cpu64 (proto_addr->ipv6[1]) >> 8) & 0xff);
hw_addr->mac[5] = ((VasEBoot_be_to_cpu64 (proto_addr->ipv6[1]) >> 0) & 0xff);
return VasEBoot_ERR_NONE;
}
/* Check cache table. */
entry = link_layer_find_entry (proto_addr, inf->card);
if (entry)
{
*hw_addr = entry->ll_address;
return VasEBoot_ERR_NONE;
}
switch (proto_addr->type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
err = VasEBoot_net_arp_send_request (inf, proto_addr);
break;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
err = VasEBoot_net_icmp6_send_request (inf, proto_addr);
break;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
return VasEBoot_error (VasEBoot_ERR_BUG, "shouldn't reach here");
default:
return VasEBoot_error (VasEBoot_ERR_BUG,
"unsupported address type %d", proto_addr->type);
}
if (err)
return err;
entry = link_layer_find_entry (proto_addr, inf->card);
if (entry)
{
*hw_addr = entry->ll_address;
return VasEBoot_ERR_NONE;
}
return VasEBoot_error (VasEBoot_ERR_TIMEOUT,
N_("timeout: could not resolve hardware address"));
}
void
VasEBoot_net_card_unregister (struct VasEBoot_net_card *card)
{
struct VasEBoot_net_network_level_interface *inf, *next;
FOR_NET_NETWORK_LEVEL_INTERFACES_SAFE(inf, next)
if (inf->card == card)
VasEBoot_net_network_level_interface_unregister (inf);
if (card->opened)
{
if (card->driver->close)
card->driver->close (card);
card->opened = 0;
}
VasEBoot_list_remove (VasEBoot_AS_LIST (card));
}
static struct VasEBoot_net_slaac_mac_list *
VasEBoot_net_ipv6_get_slaac (struct VasEBoot_net_card *card,
const VasEBoot_net_link_level_address_t *hwaddr)
{
struct VasEBoot_net_slaac_mac_list *slaac;
char *ptr;
for (slaac = card->slaac_list; slaac; slaac = slaac->next)
if (VasEBoot_net_hwaddr_cmp (&slaac->address, hwaddr) == 0)
return slaac;
slaac = VasEBoot_zalloc (sizeof (*slaac));
if (!slaac)
return NULL;
slaac->name = VasEBoot_malloc (VasEBoot_strlen (card->name)
+ VasEBoot_NET_MAX_STR_HWADDR_LEN
+ sizeof (":slaac"));
ptr = VasEBoot_stpcpy (slaac->name, card->name);
if (VasEBoot_net_hwaddr_cmp (&card->default_address, hwaddr) != 0)
{
ptr = VasEBoot_stpcpy (ptr, ":");
VasEBoot_net_hwaddr_to_str (hwaddr, ptr);
ptr += VasEBoot_strlen (ptr);
}
ptr = VasEBoot_stpcpy (ptr, ":slaac");
VasEBoot_memcpy (&slaac->address, hwaddr, sizeof (slaac->address));
slaac->next = card->slaac_list;
card->slaac_list = slaac;
return slaac;
}
static void
VasEBoot_net_network_level_interface_register (struct VasEBoot_net_network_level_interface *inter);
static struct VasEBoot_net_network_level_interface *
VasEBoot_net_add_addr_real (char *name,
struct VasEBoot_net_card *card,
const VasEBoot_net_network_level_address_t *addr,
const VasEBoot_net_link_level_address_t *hwaddress,
VasEBoot_net_interface_flags_t flags)
{
struct VasEBoot_net_network_level_interface *inter;
inter = VasEBoot_zalloc (sizeof (*inter));
if (!inter)
return NULL;
inter->name = name;
VasEBoot_memcpy (&(inter->address), addr, sizeof (inter->address));
VasEBoot_memcpy (&(inter->hwaddress), hwaddress, sizeof (inter->hwaddress));
inter->flags = flags;
inter->card = card;
inter->dhcp_ack = NULL;
inter->dhcp_acklen = 0;
VasEBoot_net_network_level_interface_register (inter);
return inter;
}
struct VasEBoot_net_network_level_interface *
VasEBoot_net_add_addr (const char *name,
struct VasEBoot_net_card *card,
const VasEBoot_net_network_level_address_t *addr,
const VasEBoot_net_link_level_address_t *hwaddress,
VasEBoot_net_interface_flags_t flags)
{
char *name_dup = VasEBoot_strdup (name);
struct VasEBoot_net_network_level_interface *ret;
if (!name_dup)
return NULL;
ret = VasEBoot_net_add_addr_real (name_dup, card, addr, hwaddress, flags);
if (!ret)
VasEBoot_free (name_dup);
return ret;
}
struct VasEBoot_net_network_level_interface *
VasEBoot_net_ipv6_get_link_local (struct VasEBoot_net_card *card,
const VasEBoot_net_link_level_address_t *hwaddr)
{
struct VasEBoot_net_network_level_interface *inf;
char *name;
char *ptr;
VasEBoot_net_network_level_address_t addr;
addr.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
addr.ipv6[0] = VasEBoot_cpu_to_be64_compile_time (0xfe80ULL << 48);
addr.ipv6[1] = VasEBoot_net_ipv6_get_id (hwaddr);
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
if (inf->card == card
&& VasEBoot_net_hwaddr_cmp (&inf->hwaddress, hwaddr) == 0
&& VasEBoot_net_addr_cmp (&inf->address, &addr) == 0)
return inf;
}
name = VasEBoot_malloc (VasEBoot_strlen (card->name)
+ VasEBoot_NET_MAX_STR_HWADDR_LEN
+ sizeof (":link"));
if (!name)
return NULL;
ptr = VasEBoot_stpcpy (name, card->name);
if (VasEBoot_net_hwaddr_cmp (&card->default_address, hwaddr) != 0)
{
ptr = VasEBoot_stpcpy (ptr, ":");
VasEBoot_net_hwaddr_to_str (hwaddr, ptr);
ptr += VasEBoot_strlen (ptr);
}
ptr = VasEBoot_stpcpy (ptr, ":link");
return VasEBoot_net_add_addr_real (name, card, &addr, hwaddr, 0);
}
/* FIXME: allow to specify mac address. */
static VasEBoot_err_t
VasEBoot_cmd_ipv6_autoconf (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char **args)
{
struct VasEBoot_net_card *card;
struct VasEBoot_net_network_level_interface **ifaces;
VasEBoot_size_t ncards = 0;
unsigned j = 0;
int interval;
VasEBoot_err_t err;
struct VasEBoot_net_slaac_mac_list **slaacs;
FOR_NET_CARDS (card)
{
if (argc > 0 && VasEBoot_strcmp (card->name, args[0]) != 0)
continue;
ncards++;
}
ifaces = VasEBoot_zalloc (ncards * sizeof (ifaces[0]));
slaacs = VasEBoot_zalloc (ncards * sizeof (slaacs[0]));
if (!ifaces || !slaacs)
{
VasEBoot_free (ifaces);
VasEBoot_free (slaacs);
return VasEBoot_errno;
}
FOR_NET_CARDS (card)
{
if (argc > 0 && VasEBoot_strcmp (card->name, args[0]) != 0)
continue;
ifaces[j] = VasEBoot_net_ipv6_get_link_local (card, &card->default_address);
if (!ifaces[j])
{
VasEBoot_free (ifaces);
VasEBoot_free (slaacs);
return VasEBoot_errno;
}
slaacs[j] = VasEBoot_net_ipv6_get_slaac (card, &card->default_address);
if (!slaacs[j])
{
VasEBoot_free (ifaces);
VasEBoot_free (slaacs);
return VasEBoot_errno;
}
j++;
}
for (interval = 200; interval < 10000; interval *= 2)
{
int done = 1;
for (j = 0; j < ncards; j++)
{
if (slaacs[j]->slaac_counter)
continue;
err = VasEBoot_net_icmp6_send_router_solicit (ifaces[j]);
if (err)
err = VasEBoot_ERR_NONE;
done = 0;
}
if (done)
break;
VasEBoot_net_poll_cards (interval, 0);
}
err = VasEBoot_ERR_NONE;
for (j = 0; j < ncards; j++)
{
if (slaacs[j]->slaac_counter)
continue;
err = VasEBoot_error (VasEBoot_ERR_FILE_NOT_FOUND,
N_("couldn't autoconfigure %s"),
ifaces[j]->card->name);
}
VasEBoot_free (ifaces);
VasEBoot_free (slaacs);
return err;
}
static int
parse_ip (const char *val, VasEBoot_uint32_t *ip, const char **rest)
{
VasEBoot_uint32_t newip = 0;
int i;
const char *ptr = val;
for (i = 0; i < 4; i++)
{
unsigned long t;
t = VasEBoot_strtoul (ptr, (char **) &ptr, 0);
if (VasEBoot_errno)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
return 0;
}
if (*ptr != '.' && i == 0)
{
newip = t;
break;
}
if (t & ~0xff)
return 0;
newip >>= 8;
newip |= (t << 24);
if (i != 3 && *ptr != '.')
return 0;
ptr++;
}
*ip = VasEBoot_cpu_to_le32 (newip);
if (rest)
*rest = (ptr - 1);
return 1;
}
static int
parse_ip6 (const char *val, VasEBoot_uint64_t *ip, const char **rest)
{
VasEBoot_uint16_t newip[8];
const char *ptr = val;
int word, quaddot = -1;
if (ptr[0] == ':' && ptr[1] != ':')
return 0;
if (ptr[0] == ':')
ptr++;
for (word = 0; word < 8; word++)
{
unsigned long t;
if (*ptr == ':')
{
quaddot = word;
word--;
ptr++;
continue;
}
t = VasEBoot_strtoul (ptr, (char **) &ptr, 16);
if (VasEBoot_errno)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
break;
}
if (t & ~0xffff)
return 0;
newip[word] = VasEBoot_cpu_to_be16 (t);
if (*ptr != ':')
break;
ptr++;
}
if (quaddot == -1 && word < 7)
return 0;
if (quaddot != -1)
{
VasEBoot_memmove (&newip[quaddot + 7 - word], &newip[quaddot],
(word - quaddot + 1) * sizeof (newip[0]));
VasEBoot_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
}
VasEBoot_memcpy (ip, newip, 16);
if (rest)
*rest = ptr;
return 1;
}
static int
match_net (const VasEBoot_net_network_level_netaddress_t *net,
const VasEBoot_net_network_level_address_t *addr)
{
if (net->type != addr->type)
return 0;
switch (net->type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
return 0;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
{
VasEBoot_uint32_t mask = (0xffffffffU << (32 - net->ipv4.masksize));
if (net->ipv4.masksize == 0)
mask = 0;
return ((VasEBoot_be_to_cpu32 (net->ipv4.base) & mask)
== (VasEBoot_be_to_cpu32 (addr->ipv4) & mask));
}
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
{
VasEBoot_uint64_t mask[2];
if (net->ipv6.masksize == 0)
return 1;
if (net->ipv6.masksize <= 64)
{
mask[0] = 0xffffffffffffffffULL << (64 - net->ipv6.masksize);
mask[1] = 0;
}
else
{
mask[0] = 0xffffffffffffffffULL;
mask[1] = 0xffffffffffffffffULL << (128 - net->ipv6.masksize);
}
return (((VasEBoot_be_to_cpu64 (net->ipv6.base[0]) & mask[0])
== (VasEBoot_be_to_cpu64 (addr->ipv6[0]) & mask[0]))
&& ((VasEBoot_be_to_cpu64 (net->ipv6.base[1]) & mask[1])
== (VasEBoot_be_to_cpu64 (addr->ipv6[1]) & mask[1])));
}
}
return 0;
}
VasEBoot_err_t
VasEBoot_net_resolve_address (const char *name,
VasEBoot_net_network_level_address_t *addr)
{
const char *rest;
VasEBoot_err_t err;
VasEBoot_size_t naddresses;
struct VasEBoot_net_network_level_address *addresses = 0;
if (parse_ip (name, &addr->ipv4, &rest) && *rest == 0)
{
addr->type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
return VasEBoot_ERR_NONE;
}
if (parse_ip6 (name, addr->ipv6, &rest) && *rest == 0)
{
addr->type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
return VasEBoot_ERR_NONE;
}
err = VasEBoot_net_dns_lookup (name, 0, 0, &naddresses, &addresses, 1);
if (err)
return err;
if (!naddresses)
VasEBoot_error (VasEBoot_ERR_NET_BAD_ADDRESS, N_("unresolvable address %s"),
name);
/* FIXME: use other results as well. */
*addr = addresses[0];
VasEBoot_free (addresses);
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_net_resolve_net_address (const char *name,
VasEBoot_net_network_level_netaddress_t *addr)
{
const char *rest;
if (parse_ip (name, &addr->ipv4.base, &rest))
{
addr->type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
if (*rest == '/')
{
addr->ipv4.masksize = VasEBoot_strtoul (rest + 1, (char **) &rest, 0);
if (!VasEBoot_errno && *rest == 0)
return VasEBoot_ERR_NONE;
VasEBoot_errno = VasEBoot_ERR_NONE;
}
else if (*rest == 0)
{
addr->ipv4.masksize = 32;
return VasEBoot_ERR_NONE;
}
}
if (parse_ip6 (name, addr->ipv6.base, &rest))
{
addr->type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
if (*rest == '/')
{
addr->ipv6.masksize = VasEBoot_strtoul (rest + 1, (char **) &rest, 0);
if (!VasEBoot_errno && *rest == 0)
return VasEBoot_ERR_NONE;
VasEBoot_errno = VasEBoot_ERR_NONE;
}
else if (*rest == 0)
{
addr->ipv6.masksize = 128;
return VasEBoot_ERR_NONE;
}
}
return VasEBoot_error (VasEBoot_ERR_NET_BAD_ADDRESS,
N_("unrecognised network address `%s'"),
name);
}
static int
route_cmp (const struct VasEBoot_net_route *a, const struct VasEBoot_net_route *b)
{
if (a == NULL && b == NULL)
return 0;
if (b == NULL)
return +1;
if (a == NULL)
return -1;
if (a->target.type < b->target.type)
return -1;
if (a->target.type > b->target.type)
return +1;
switch (a->target.type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
break;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
if (a->target.ipv6.masksize > b->target.ipv6.masksize)
return +1;
if (a->target.ipv6.masksize < b->target.ipv6.masksize)
return -1;
break;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
if (a->target.ipv4.masksize > b->target.ipv4.masksize)
return +1;
if (a->target.ipv4.masksize < b->target.ipv4.masksize)
return -1;
break;
}
return 0;
}
VasEBoot_err_t
VasEBoot_net_route_address (VasEBoot_net_network_level_address_t addr,
VasEBoot_net_network_level_address_t *gateway,
struct VasEBoot_net_network_level_interface **interf)
{
struct VasEBoot_net_route *route;
unsigned int depth = 0;
unsigned int routecnt = 0;
struct VasEBoot_net_network_level_protocol *prot = NULL;
VasEBoot_net_network_level_address_t curtarget = addr;
*gateway = addr;
FOR_NET_ROUTES(route)
routecnt++;
for (depth = 0; depth < routecnt + 2 && depth < VasEBoot_UINT_MAX; depth++)
{
struct VasEBoot_net_route *bestroute = NULL;
FOR_NET_ROUTES(route)
{
if (depth && prot != route->prot)
continue;
if (!match_net (&route->target, &curtarget))
continue;
if (route_cmp (route, bestroute) > 0)
bestroute = route;
}
if (bestroute == NULL)
return VasEBoot_error (VasEBoot_ERR_NET_NO_ROUTE,
N_("destination unreachable"));
if (!bestroute->is_gateway)
{
*interf = bestroute->interface;
return VasEBoot_ERR_NONE;
}
if (depth == 0)
{
*gateway = bestroute->gw;
if (bestroute->interface != NULL)
{
*interf = bestroute->interface;
return VasEBoot_ERR_NONE;
}
}
curtarget = bestroute->gw;
}
return VasEBoot_error (VasEBoot_ERR_NET_ROUTE_LOOP,
/* TRANSLATORS: route loop is a condition when e.g.
to contact server A you need to go through B
and to contact B you need to go through A. */
N_("route loop detected"));
}
static VasEBoot_err_t
VasEBoot_cmd_deladdr (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char **args)
{
struct VasEBoot_net_network_level_interface *inter;
if (argc != 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("one argument expected"));
FOR_NET_NETWORK_LEVEL_INTERFACES (inter)
if (VasEBoot_strcmp (inter->name, args[0]) == 0)
break;
if (inter == NULL)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("address not found"));
if (inter->flags & VasEBoot_NET_INTERFACE_PERMANENT)
return VasEBoot_error (VasEBoot_ERR_IO,
N_("you can't delete this address"));
VasEBoot_net_network_level_interface_unregister (inter);
VasEBoot_free (inter->name);
VasEBoot_free (inter);
return VasEBoot_ERR_NONE;
}
void
VasEBoot_net_addr_to_str (const VasEBoot_net_network_level_address_t *target, char *buf)
{
switch (target->type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
COMPILE_TIME_ASSERT (sizeof ("temporary") < VasEBoot_NET_MAX_STR_ADDR_LEN);
VasEBoot_strcpy (buf, "temporary");
return;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
{
char *ptr = buf;
VasEBoot_uint64_t n = VasEBoot_be_to_cpu64 (target->ipv6[0]);
int i;
for (i = 0; i < 4; i++)
{
VasEBoot_snprintf (ptr, 6, "%" PRIxVasEBoot_UINT64_T ":",
(n >> (48 - 16 * i)) & 0xffff);
ptr += VasEBoot_strlen (ptr);
}
n = VasEBoot_be_to_cpu64 (target->ipv6[1]);
for (i = 0; i < 3; i++)
{
VasEBoot_snprintf (ptr, 6, "%" PRIxVasEBoot_UINT64_T ":",
(n >> (48 - 16 * i)) & 0xffff);
ptr += VasEBoot_strlen (ptr);
}
VasEBoot_snprintf (ptr, 5, "%" PRIxVasEBoot_UINT64_T, n & 0xffff);
return;
}
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
{
VasEBoot_uint32_t n = VasEBoot_be_to_cpu32 (target->ipv4);
VasEBoot_snprintf (buf, VasEBoot_NET_MAX_STR_ADDR_LEN, "%d.%d.%d.%d",
((n >> 24) & 0xff), ((n >> 16) & 0xff),
((n >> 8) & 0xff), ((n >> 0) & 0xff));
}
return;
}
VasEBoot_snprintf (buf, VasEBoot_NET_MAX_STR_ADDR_LEN,
"Unknown address type %d", target->type);
}
void
VasEBoot_net_hwaddr_to_str (const VasEBoot_net_link_level_address_t *addr, char *str)
{
str[0] = 0;
switch (addr->type)
{
case VasEBoot_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
{
char *ptr;
unsigned i;
for (ptr = str, i = 0; i < ARRAY_SIZE (addr->mac); i++)
{
VasEBoot_snprintf (ptr, VasEBoot_NET_MAX_STR_HWADDR_LEN - (ptr - str),
"%02x:", addr->mac[i] & 0xff);
ptr += (sizeof ("XX:") - 1);
}
return;
}
}
VasEBoot_printf (_("Unsupported hw address type %d\n"), addr->type);
}
int
VasEBoot_net_hwaddr_cmp (const VasEBoot_net_link_level_address_t *a,
const VasEBoot_net_link_level_address_t *b)
{
if (a->type < b->type)
return -1;
if (a->type > b->type)
return +1;
switch (a->type)
{
case VasEBoot_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
return VasEBoot_memcmp (a->mac, b->mac, sizeof (a->mac));
}
VasEBoot_printf (_("Unsupported hw address type %d\n"), a->type);
return 1;
}
int
VasEBoot_net_addr_cmp (const VasEBoot_net_network_level_address_t *a,
const VasEBoot_net_network_level_address_t *b)
{
if (a->type < b->type)
return -1;
if (a->type > b->type)
return +1;
switch (a->type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
return VasEBoot_memcmp (&a->ipv4, &b->ipv4, sizeof (a->ipv4));
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
return VasEBoot_memcmp (&a->ipv6, &b->ipv6, sizeof (a->ipv6));
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
return 0;
}
VasEBoot_printf (_("Unsupported address type %d\n"), a->type);
return 1;
}
/* FIXME: implement this. */
static char *
hwaddr_set_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
return NULL;
}
/* FIXME: implement this. */
static char *
addr_set_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
return NULL;
}
static char *
defserver_set_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val)
{
VasEBoot_free (VasEBoot_net_default_server);
VasEBoot_net_default_server = VasEBoot_strdup (val);
return VasEBoot_strdup (val);
}
static const char *
defserver_get_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
return VasEBoot_net_default_server ? : "";
}
static const char *
defip_get_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
const char *intf = VasEBoot_env_get ("net_default_interface");
const char *ret = NULL;
if (intf)
{
char *buf = VasEBoot_xasprintf ("net_%s_ip", intf);
if (buf)
ret = VasEBoot_env_get (buf);
VasEBoot_free (buf);
}
return ret;
}
static char *
defip_set_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val)
{
const char *intf = VasEBoot_env_get ("net_default_interface");
if (intf)
{
char *buf = VasEBoot_xasprintf ("net_%s_ip", intf);
if (buf)
VasEBoot_env_set (buf, val);
VasEBoot_free (buf);
}
return NULL;
}
static const char *
defmac_get_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
const char *intf = VasEBoot_env_get ("net_default_interface");
const char *ret = NULL;
if (intf)
{
char *buf = VasEBoot_xasprintf ("net_%s_mac", intf);
if (buf)
ret = VasEBoot_env_get (buf);
VasEBoot_free (buf);
}
return ret;
}
static char *
defmac_set_env (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val)
{
const char *intf = VasEBoot_env_get ("net_default_interface");
if (intf)
{
char *buf = VasEBoot_xasprintf ("net_%s_mac", intf);
if (buf)
VasEBoot_env_set (buf, val);
VasEBoot_free (buf);
}
return NULL;
}
static void
VasEBoot_net_network_level_interface_register (struct VasEBoot_net_network_level_interface *inter)
{
{
char buf[VasEBoot_NET_MAX_STR_HWADDR_LEN];
char *name;
char *ptr;
VasEBoot_net_hwaddr_to_str (&inter->hwaddress, buf);
name = VasEBoot_xasprintf ("net_%s_mac", inter->name);
if (!name)
return;
for (ptr = name; *ptr; ptr++)
if (*ptr == ':')
*ptr = '_';
VasEBoot_env_set (name, buf);
VasEBoot_register_variable_hook (name, 0, hwaddr_set_env);
VasEBoot_env_export (name);
VasEBoot_free (name);
}
{
char buf[VasEBoot_NET_MAX_STR_ADDR_LEN];
char *name;
char *ptr;
VasEBoot_net_addr_to_str (&inter->address, buf);
name = VasEBoot_xasprintf ("net_%s_ip", inter->name);
if (!name)
return;
for (ptr = name; *ptr; ptr++)
if (*ptr == ':')
*ptr = '_';
VasEBoot_env_set (name, buf);
VasEBoot_register_variable_hook (name, 0, addr_set_env);
VasEBoot_env_export (name);
VasEBoot_free (name);
}
inter->card->num_ifaces++;
inter->prev = &VasEBoot_net_network_level_interfaces;
inter->next = VasEBoot_net_network_level_interfaces;
if (inter->next)
inter->next->prev = &inter->next;
VasEBoot_net_network_level_interfaces = inter;
}
VasEBoot_err_t
VasEBoot_net_add_ipv4_local (struct VasEBoot_net_network_level_interface *inter,
int mask)
{
VasEBoot_uint32_t ip_cpu;
struct VasEBoot_net_route *route;
if (inter->address.type != VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
return 0;
ip_cpu = VasEBoot_be_to_cpu32 (inter->address.ipv4);
if (mask == -1)
{
if (!(ip_cpu & 0x80000000))
mask = 8;
else if (!(ip_cpu & 0x40000000))
mask = 16;
else if (!(ip_cpu & 0x20000000))
mask = 24;
}
if (mask == -1)
return 0;
route = VasEBoot_zalloc (sizeof (*route));
if (!route)
return VasEBoot_errno;
route->name = VasEBoot_xasprintf ("%s:local", inter->name);
if (!route->name)
{
VasEBoot_free (route);
return VasEBoot_errno;
}
route->target.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
route->target.ipv4.base = VasEBoot_cpu_to_be32 (ip_cpu & (0xffffffff << (32 - mask)));
route->target.ipv4.masksize = mask;
route->is_gateway = 0;
route->interface = inter;
VasEBoot_net_route_register (route);
return 0;
}
/* FIXME: support MAC specifying. */
static VasEBoot_err_t
VasEBoot_cmd_addaddr (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char **args)
{
struct VasEBoot_net_card *card;
VasEBoot_net_network_level_address_t addr;
VasEBoot_err_t err;
VasEBoot_net_interface_flags_t flags = 0;
struct VasEBoot_net_network_level_interface *inf;
if (argc != 3)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("three arguments expected"));
FOR_NET_CARDS (card)
if (VasEBoot_strcmp (card->name, args[1]) == 0)
break;
if (card == NULL)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("card not found"));
err = VasEBoot_net_resolve_address (args[2], &addr);
if (err)
return err;
if (card->flags & VasEBoot_NET_CARD_NO_MANUAL_INTERFACES)
return VasEBoot_error (VasEBoot_ERR_IO,
"this card doesn't support address addition");
if (card->flags & VasEBoot_NET_CARD_HWADDRESS_IMMUTABLE)
flags |= VasEBoot_NET_INTERFACE_HWADDRESS_IMMUTABLE;
inf = VasEBoot_net_add_addr (args[0], card, &addr, &card->default_address,
flags);
if (inf)
VasEBoot_net_add_ipv4_local (inf, -1);
return VasEBoot_errno;
}
static VasEBoot_err_t
VasEBoot_cmd_delroute (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char **args)
{
struct VasEBoot_net_route *route;
struct VasEBoot_net_route **prev;
if (argc != 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("one argument expected"));
for (prev = &VasEBoot_net_routes, route = *prev; route; prev = &((*prev)->next),
route = *prev)
if (VasEBoot_strcmp (route->name, args[0]) == 0)
{
*prev = route->next;
VasEBoot_free (route->name);
VasEBoot_free (route);
if (!*prev)
break;
}
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_net_add_route (const char *name,
VasEBoot_net_network_level_netaddress_t target,
struct VasEBoot_net_network_level_interface *inter)
{
struct VasEBoot_net_route *route;
route = VasEBoot_zalloc (sizeof (*route));
if (!route)
return VasEBoot_errno;
route->name = VasEBoot_strdup (name);
if (!route->name)
{
VasEBoot_free (route);
return VasEBoot_errno;
}
route->target = target;
route->is_gateway = 0;
route->interface = inter;
VasEBoot_net_route_register (route);
return VasEBoot_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_net_add_route_gw (const char *name,
VasEBoot_net_network_level_netaddress_t target,
VasEBoot_net_network_level_address_t gw,
struct VasEBoot_net_network_level_interface *inter)
{
struct VasEBoot_net_route *route;
route = VasEBoot_zalloc (sizeof (*route));
if (!route)
return VasEBoot_errno;
route->name = VasEBoot_strdup (name);
if (!route->name)
{
VasEBoot_free (route);
return VasEBoot_errno;
}
route->target = target;
route->is_gateway = 1;
route->gw = gw;
route->interface = inter;
VasEBoot_net_route_register (route);
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_addroute (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char **args)
{
VasEBoot_net_network_level_netaddress_t target;
if (argc < 3)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT,
N_("three arguments expected"));
VasEBoot_net_resolve_net_address (args[1], &target);
if (VasEBoot_strcmp (args[2], "gw") == 0 && argc >= 4)
{
VasEBoot_err_t err;
VasEBoot_net_network_level_address_t gw;
err = VasEBoot_net_resolve_address (args[3], &gw);
if (err)
return err;
return VasEBoot_net_add_route_gw (args[0], target, gw, NULL);
}
else
{
struct VasEBoot_net_network_level_interface *inter;
FOR_NET_NETWORK_LEVEL_INTERFACES (inter)
if (VasEBoot_strcmp (inter->name, args[2]) == 0)
break;
if (!inter)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT,
N_("unrecognised network interface `%s'"), args[2]);
return VasEBoot_net_add_route (args[0], target, inter);
}
}
static void
print_net_address (const VasEBoot_net_network_level_netaddress_t *target)
{
switch (target->type)
{
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
/* TRANSLATORS: it refers to the network address. */
VasEBoot_printf ("%s\n", _("temporary"));
return;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
{
VasEBoot_uint32_t n = VasEBoot_be_to_cpu32 (target->ipv4.base);
VasEBoot_printf ("%d.%d.%d.%d/%d ", ((n >> 24) & 0xff),
((n >> 16) & 0xff),
((n >> 8) & 0xff),
((n >> 0) & 0xff),
target->ipv4.masksize);
}
return;
case VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
{
char buf[VasEBoot_NET_MAX_STR_ADDR_LEN];
struct VasEBoot_net_network_level_address base;
base.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
VasEBoot_memcpy (&base.ipv6, &target->ipv6, 16);
VasEBoot_net_addr_to_str (&base, buf);
VasEBoot_printf ("%s/%d ", buf, target->ipv6.masksize);
}
return;
}
VasEBoot_printf (_("Unknown address type %d\n"), target->type);
}
static void
print_address (const VasEBoot_net_network_level_address_t *target)
{
char buf[VasEBoot_NET_MAX_STR_ADDR_LEN];
VasEBoot_net_addr_to_str (target, buf);
VasEBoot_xputs (buf);
}
static VasEBoot_err_t
VasEBoot_cmd_listroutes (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct VasEBoot_net_route *route;
FOR_NET_ROUTES(route)
{
VasEBoot_printf ("%s ", route->name);
print_net_address (&route->target);
if (route->is_gateway)
{
VasEBoot_printf ("gw ");
print_address (&route->gw);
}
else
VasEBoot_printf ("%s", route->interface->name);
VasEBoot_printf ("\n");
}
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_listcards (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct VasEBoot_net_card *card;
FOR_NET_CARDS(card)
{
char buf[VasEBoot_NET_MAX_STR_HWADDR_LEN];
VasEBoot_net_hwaddr_to_str (&card->default_address, buf);
VasEBoot_printf ("%s %s\n", card->name, buf);
}
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_listaddrs (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct VasEBoot_net_network_level_interface *inf;
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
char bufh[VasEBoot_NET_MAX_STR_HWADDR_LEN];
char bufn[VasEBoot_NET_MAX_STR_ADDR_LEN];
VasEBoot_net_hwaddr_to_str (&inf->hwaddress, bufh);
VasEBoot_net_addr_to_str (&inf->address, bufn);
VasEBoot_printf ("%s %s %s\n", inf->name, bufh, bufn);
}
return VasEBoot_ERR_NONE;
}
VasEBoot_net_app_level_t VasEBoot_net_app_level_list;
struct VasEBoot_net_socket *VasEBoot_net_sockets;
static VasEBoot_net_t
VasEBoot_net_open_real (const char *name)
{
VasEBoot_net_app_level_t proto;
const char *protname, *server;
VasEBoot_size_t protnamelen;
int try, port = 0;
if (VasEBoot_strncmp (name, "pxe:", sizeof ("pxe:") - 1) == 0)
{
protname = "tftp";
protnamelen = sizeof ("tftp") - 1;
server = name + sizeof ("pxe:") - 1;
}
else if (VasEBoot_strcmp (name, "pxe") == 0)
{
protname = "tftp";
protnamelen = sizeof ("tftp") - 1;
server = VasEBoot_net_default_server;
}
else
{
const char *comma;
char *colon;
comma = VasEBoot_strchr (name, ',');
if (!comma)
{
comma = VasEBoot_strchr (name, ';');
}
colon = VasEBoot_strchr (name, ':');
if (colon)
{
port = (int) VasEBoot_strtol(colon+1, NULL, 10);
*colon = '\0';
}
if (comma)
{
protnamelen = comma - name;
server = comma + 1;
protname = name;
}
else
{
protnamelen = VasEBoot_strlen (name);
server = VasEBoot_net_default_server;
protname = name;
}
}
if (!server)
{
VasEBoot_error (VasEBoot_ERR_NET_BAD_ADDRESS,
N_("no server is specified"));
return NULL;
}
for (try = 0; try < 2; try++)
{
FOR_NET_APP_LEVEL (proto)
{
if (VasEBoot_memcmp (proto->name, protname, protnamelen) == 0
&& proto->name[protnamelen] == 0)
{
VasEBoot_net_t ret = VasEBoot_zalloc (sizeof (*ret));
if (!ret)
return NULL;
ret->protocol = proto;
ret->server = VasEBoot_strdup (server);
if (!ret->server)
{
VasEBoot_free (ret);
return NULL;
}
ret->port = port;
ret->fs = &VasEBoot_net_fs;
return ret;
}
}
if (try == 0)
{
const char *prefix, *root;
char *prefdev, *comma;
int skip = 0;
VasEBoot_size_t devlen;
/* Do not attempt to load module if it requires protocol provided
by this module - it results in infinite recursion. Just continue,
fail and cleanup on next iteration.
*/
prefix = VasEBoot_env_get ("prefix");
if (!prefix)
continue;
prefdev = VasEBoot_file_get_device_name (prefix);
if (!prefdev)
{
root = VasEBoot_env_get ("root");
if (!root)
continue;
prefdev = VasEBoot_strdup (root);
if (!prefdev)
continue;
}
if (VasEBoot_strncmp (prefdev, "pxe", sizeof ("pxe") - 1) == 0 &&
(!prefdev[sizeof ("pxe") - 1] || (prefdev[sizeof("pxe") - 1] == ':')))
{
VasEBoot_free (prefdev);
prefdev = VasEBoot_strdup ("tftp");
if (!prefdev)
continue;
}
comma = VasEBoot_strchr (prefdev, ',');
if (comma)
*comma = '\0';
devlen = VasEBoot_strlen (prefdev);
if (protnamelen == devlen && VasEBoot_memcmp (prefdev, protname, devlen) == 0)
skip = 1;
VasEBoot_free (prefdev);
if (skip)
continue;
if (sizeof ("http") - 1 == protnamelen
&& VasEBoot_memcmp ("http", protname, protnamelen) == 0)
{
VasEBoot_dl_load ("http");
VasEBoot_errno = VasEBoot_ERR_NONE;
continue;
}
if (sizeof ("tftp") - 1 == protnamelen
&& VasEBoot_memcmp ("tftp", protname, protnamelen) == 0)
{
VasEBoot_dl_load ("tftp");
VasEBoot_errno = VasEBoot_ERR_NONE;
continue;
}
}
break;
}
/* Restore original error. */
VasEBoot_error (VasEBoot_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
name);
return NULL;
}
static VasEBoot_err_t
VasEBoot_net_fs_dir (VasEBoot_device_t device, const char *path __attribute__ ((unused)),
VasEBoot_fs_dir_hook_t hook __attribute__ ((unused)),
void *hook_data __attribute__ ((unused)))
{
if (!device->net)
return VasEBoot_error (VasEBoot_ERR_BUG, "invalid net device");
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_net_fs_open (struct VasEBoot_file *file_out, const char *name)
{
VasEBoot_err_t err;
struct VasEBoot_file *file, *bufio;
file = VasEBoot_malloc (sizeof (*file));
if (!file)
return VasEBoot_errno;
VasEBoot_memcpy (file, file_out, sizeof (struct VasEBoot_file));
file->device->net->packs.first = NULL;
file->device->net->packs.last = NULL;
file->device->net->name = VasEBoot_strdup (name);
if (!file->device->net->name)
{
VasEBoot_free (file);
return VasEBoot_errno;
}
err = file->device->net->protocol->open (file, name);
if (err)
{
while (file->device->net->packs.first)
{
VasEBoot_netbuff_free (file->device->net->packs.first->nb);
VasEBoot_net_remove_packet (file->device->net->packs.first);
}
VasEBoot_free (file->device->net->name);
VasEBoot_free (file);
return err;
}
bufio = VasEBoot_bufio_open (file, 32768);
if (! bufio)
{
while (file->device->net->packs.first)
{
VasEBoot_netbuff_free (file->device->net->packs.first->nb);
VasEBoot_net_remove_packet (file->device->net->packs.first);
}
file->device->net->protocol->close (file);
VasEBoot_free (file->device->net->name);
VasEBoot_free (file);
return VasEBoot_errno;
}
VasEBoot_memcpy (file_out, bufio, sizeof (struct VasEBoot_file));
VasEBoot_free (bufio);
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_net_fs_close (VasEBoot_file_t file)
{
while (file->device->net->packs.first)
{
VasEBoot_netbuff_free (file->device->net->packs.first->nb);
VasEBoot_net_remove_packet (file->device->net->packs.first);
}
file->device->net->protocol->close (file);
VasEBoot_free (file->device->net->name);
return VasEBoot_ERR_NONE;
}
static void
receive_packets (struct VasEBoot_net_card *card, int *stop_condition)
{
int received = 0;
if (card->num_ifaces == 0)
return;
if (!card->opened)
{
VasEBoot_err_t err = VasEBoot_ERR_NONE;
if (card->driver->open)
err = card->driver->open (card);
if (err)
{
VasEBoot_errno = VasEBoot_ERR_NONE;
return;
}
card->opened = 1;
}
while (received < 100)
{
/* Maybe should be better have a fixed number of packets for each card
and just mark them as used and not used. */
struct VasEBoot_net_buff *nb;
if (received > 10 && stop_condition && *stop_condition)
break;
nb = card->driver->recv (card);
if (!nb)
{
card->last_poll = VasEBoot_get_time_ms ();
break;
}
received++;
VasEBoot_net_recv_ethernet_packet (nb, card);
if (VasEBoot_errno)
{
VasEBoot_dprintf ("net", "error receiving: %d: %s\n", VasEBoot_errno,
VasEBoot_errmsg);
VasEBoot_errno = VasEBoot_ERR_NONE;
}
}
VasEBoot_print_error ();
}
static char *
VasEBoot_env_write_readonly (struct VasEBoot_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))
{
return NULL;
}
VasEBoot_err_t
VasEBoot_env_set_net_property (const char *intername, const char *suffix,
const char *value, VasEBoot_size_t len)
{
char *varname, *varvalue;
char *ptr;
varname = VasEBoot_xasprintf ("net_%s_%s", intername, suffix);
if (!varname)
return VasEBoot_errno;
for (ptr = varname; *ptr; ptr++)
if (*ptr == ':')
*ptr = '_';
varvalue = VasEBoot_malloc (len + 1);
if (!varvalue)
{
VasEBoot_free (varname);
return VasEBoot_errno;
}
VasEBoot_memcpy (varvalue, value, len);
varvalue[len] = 0;
VasEBoot_err_t ret = VasEBoot_env_set (varname, varvalue);
VasEBoot_register_variable_hook (varname, 0, VasEBoot_env_write_readonly);
VasEBoot_env_export (varname);
VasEBoot_free (varname);
VasEBoot_free (varvalue);
return ret;
}
void
VasEBoot_net_poll_cards (unsigned time, int *stop_condition)
{
struct VasEBoot_net_card *card;
VasEBoot_uint64_t start_time;
start_time = VasEBoot_get_time_ms ();
while ((VasEBoot_get_time_ms () - start_time) < time
&& (!stop_condition || !*stop_condition))
FOR_NET_CARDS (card)
receive_packets (card, stop_condition);
VasEBoot_net_tcp_retransmit ();
}
static void
VasEBoot_net_poll_cards_idle_real (void)
{
struct VasEBoot_net_card *card;
FOR_NET_CARDS (card)
{
VasEBoot_uint64_t ctime = VasEBoot_get_time_ms ();
if (ctime < card->last_poll
|| ctime >= card->last_poll + card->idle_poll_delay_ms)
receive_packets (card, 0);
}
VasEBoot_net_tcp_retransmit ();
}
/* Read from the packets list*/
static VasEBoot_ssize_t
VasEBoot_net_fs_read_real (VasEBoot_file_t file, char *buf, VasEBoot_size_t len)
{
VasEBoot_net_t net = file->device->net;
struct VasEBoot_net_buff *nb;
char *ptr = buf;
VasEBoot_size_t amount, total = 0;
int try = 0;
while (try <= VasEBoot_NET_TRIES)
{
while (net->packs.first)
{
try = 0;
nb = net->packs.first->nb;
amount = nb->tail - nb->data;
if (amount > len)
amount = len;
len -= amount;
total += amount;
file->device->net->offset += amount;
if (VasEBoot_file_progress_hook)
VasEBoot_file_progress_hook (0, 0, amount, file);
if (buf)
{
VasEBoot_memcpy (ptr, nb->data, amount);
ptr += amount;
}
if (amount == (VasEBoot_size_t) (nb->tail - nb->data))
{
VasEBoot_netbuff_free (nb);
VasEBoot_net_remove_packet (net->packs.first);
}
else
nb->data += amount;
if (!len)
{
if (net->protocol->packets_pulled)
net->protocol->packets_pulled (file);
return total;
}
}
if (net->protocol->packets_pulled)
net->protocol->packets_pulled (file);
if (!net->eof)
{
try++;
VasEBoot_net_poll_cards (VasEBoot_NET_INTERVAL +
(try * VasEBoot_NET_INTERVAL_ADDITION), &net->stall);
}
else
return total;
}
VasEBoot_error (VasEBoot_ERR_TIMEOUT, N_("timeout reading `%s'"), net->name);
return -1;
}
static VasEBoot_off_t
have_ahead (struct VasEBoot_file *file)
{
VasEBoot_net_t net = file->device->net;
VasEBoot_off_t ret = net->offset;
struct VasEBoot_net_packet *pack;
for (pack = net->packs.first; pack; pack = pack->next)
ret += pack->nb->tail - pack->nb->data;
return ret;
}
static VasEBoot_err_t
VasEBoot_net_seek_real (struct VasEBoot_file *file, VasEBoot_off_t offset)
{
if (offset == file->device->net->offset)
return VasEBoot_ERR_NONE;
if (offset > file->device->net->offset)
{
if (!file->device->net->protocol->seek || have_ahead (file) >= offset)
{
VasEBoot_net_fs_read_real (file, NULL,
offset - file->device->net->offset);
return VasEBoot_errno;
}
return file->device->net->protocol->seek (file, offset);
}
{
VasEBoot_err_t err;
if (file->device->net->protocol->seek)
return file->device->net->protocol->seek (file, offset);
while (file->device->net->packs.first)
{
VasEBoot_netbuff_free (file->device->net->packs.first->nb);
VasEBoot_net_remove_packet (file->device->net->packs.first);
}
file->device->net->protocol->close (file);
file->device->net->packs.first = NULL;
file->device->net->packs.last = NULL;
file->device->net->offset = 0;
file->device->net->eof = 0;
file->device->net->stall = 0;
err = file->device->net->protocol->open (file, file->device->net->name);
if (err)
return err;
VasEBoot_net_fs_read_real (file, NULL, offset);
return VasEBoot_errno;
}
}
static VasEBoot_ssize_t
VasEBoot_net_fs_read (VasEBoot_file_t file, char *buf, VasEBoot_size_t len)
{
if (file->offset != file->device->net->offset)
{
VasEBoot_err_t err;
err = VasEBoot_net_seek_real (file, file->offset);
if (err)
return err;
}
return VasEBoot_net_fs_read_real (file, buf, len);
}
static struct VasEBoot_fs VasEBoot_net_fs =
{
.name = "netfs",
.dir = VasEBoot_net_fs_dir,
.open = VasEBoot_net_fs_open,
.read = VasEBoot_net_fs_read,
.close = VasEBoot_net_fs_close,
.label = NULL,
.uuid = NULL,
.mtime = NULL,
};
static VasEBoot_err_t
VasEBoot_net_fini_hw (int noreturn __attribute__ ((unused)))
{
struct VasEBoot_net_card *card;
FOR_NET_CARDS (card)
if (card->opened)
{
if (card->driver->close)
card->driver->close (card);
card->opened = 0;
}
return VasEBoot_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_net_restore_hw (void)
{
return VasEBoot_ERR_NONE;
}
static struct VasEBoot_preboot *fini_hnd;
static VasEBoot_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;
static VasEBoot_command_t cmd_lsroutes, cmd_lscards;
static VasEBoot_command_t cmd_lsaddr, cmd_slaac;
VasEBoot_MOD_INIT(net)
{
VasEBoot_register_variable_hook ("net_default_server", defserver_get_env,
defserver_set_env);
VasEBoot_env_export ("net_default_server");
VasEBoot_register_variable_hook ("pxe_default_server", defserver_get_env,
defserver_set_env);
VasEBoot_env_export ("pxe_default_server");
VasEBoot_register_variable_hook ("net_default_ip", defip_get_env,
defip_set_env);
VasEBoot_env_export ("net_default_ip");
VasEBoot_register_variable_hook ("net_default_mac", defmac_get_env,
defmac_set_env);
VasEBoot_env_export ("net_default_mac");
cmd_addaddr = VasEBoot_register_command ("net_add_addr", VasEBoot_cmd_addaddr,
/* TRANSLATORS: HWADDRESS stands for
"hardware address". */
N_("SHORTNAME CARD ADDRESS [HWADDRESS]"),
N_("Add a network address."));
cmd_slaac = VasEBoot_register_command ("net_ipv6_autoconf",
VasEBoot_cmd_ipv6_autoconf,
N_("[CARD [HWADDRESS]]"),
N_("Perform an IPV6 autoconfiguration"));
cmd_deladdr = VasEBoot_register_command ("net_del_addr", VasEBoot_cmd_deladdr,
N_("SHORTNAME"),
N_("Delete a network address."));
cmd_addroute = VasEBoot_register_command ("net_add_route", VasEBoot_cmd_addroute,
/* TRANSLATORS: "gw" is a keyword. */
N_("SHORTNAME NET [INTERFACE| gw GATEWAY]"),
N_("Add a network route."));
cmd_delroute = VasEBoot_register_command ("net_del_route", VasEBoot_cmd_delroute,
N_("SHORTNAME"),
N_("Delete a network route."));
cmd_lsroutes = VasEBoot_register_command ("net_ls_routes", VasEBoot_cmd_listroutes,
"", N_("list network routes"));
cmd_lscards = VasEBoot_register_command ("net_ls_cards", VasEBoot_cmd_listcards,
"", N_("list network cards"));
cmd_lsaddr = VasEBoot_register_command ("net_ls_addr", VasEBoot_cmd_listaddrs,
"", N_("list network addresses"));
VasEBoot_bootp_init ();
VasEBoot_dns_init ();
VasEBoot_net_open = VasEBoot_net_open_real;
fini_hnd = VasEBoot_loader_register_preboot_hook (VasEBoot_net_fini_hw,
VasEBoot_net_restore_hw,
VasEBoot_LOADER_PREBOOT_HOOK_PRIO_DISK);
VasEBoot_net_poll_cards_idle = VasEBoot_net_poll_cards_idle_real;
}
VasEBoot_MOD_FINI(net)
{
VasEBoot_register_variable_hook ("net_default_server", 0, 0);
VasEBoot_register_variable_hook ("pxe_default_server", 0, 0);
VasEBoot_bootp_fini ();
VasEBoot_dns_fini ();
VasEBoot_unregister_command (cmd_addaddr);
VasEBoot_unregister_command (cmd_deladdr);
VasEBoot_unregister_command (cmd_addroute);
VasEBoot_unregister_command (cmd_delroute);
VasEBoot_unregister_command (cmd_lsroutes);
VasEBoot_unregister_command (cmd_lscards);
VasEBoot_unregister_command (cmd_lsaddr);
VasEBoot_unregister_command (cmd_slaac);
VasEBoot_fs_unregister (&VasEBoot_net_fs);
VasEBoot_net_open = NULL;
VasEBoot_net_fini_hw (0);
VasEBoot_loader_unregister_preboot_hook (fini_hnd);
VasEBoot_net_poll_cards_idle = VasEBoot_net_poll_cards_idle_real;
}