merge my and Abranches' branches
This commit is contained in:
commit
ecb2a8b656
215 changed files with 10573 additions and 1733 deletions
|
@ -35,36 +35,40 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
|
|||
grub_net_link_level_address_t *hw_addr)
|
||||
{
|
||||
struct arp_entry *entry;
|
||||
struct grub_net_buff *nb;
|
||||
struct grub_net_buff nb;
|
||||
struct arphdr *arp_header;
|
||||
grub_net_link_level_address_t target_hw_addr;
|
||||
grub_uint8_t *aux, i;
|
||||
char *aux, arp_data[128];
|
||||
int i;
|
||||
|
||||
/* Check cache table */
|
||||
/* Check cache table. */
|
||||
entry = arp_find_entry (proto_addr);
|
||||
if (entry)
|
||||
{
|
||||
*hw_addr = entry->ll_address;
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
/* Build a request packet */
|
||||
nb = grub_netbuff_alloc (2048);
|
||||
if (!nb)
|
||||
return grub_errno;
|
||||
grub_netbuff_reserve(nb, 2048);
|
||||
grub_netbuff_push(nb, sizeof(*arp_header) + 2 * (6 + 6));
|
||||
arp_header = (struct arphdr *)nb->data;
|
||||
/* Build a request packet. */
|
||||
nb.head = arp_data;
|
||||
nb.end = arp_data + sizeof (arp_data);
|
||||
grub_netbuff_clear (&nb);
|
||||
|
||||
grub_netbuff_reserve (&nb,128);
|
||||
grub_netbuff_push (&nb, sizeof(*arp_header) + 2 * (6 + 4));
|
||||
arp_header = (struct arphdr *) nb.data;
|
||||
arp_header->hrd = grub_cpu_to_be16 (GRUB_NET_ARPHRD_ETHERNET);
|
||||
arp_header->pro = grub_cpu_to_be16 (GRUB_NET_ETHERTYPE_IP);
|
||||
/* FIXME Add support to ipv6 address. */
|
||||
arp_header->hln = 6;
|
||||
arp_header->pln = 4;
|
||||
arp_header->op = grub_cpu_to_be16 (ARP_REQUEST);
|
||||
aux = (grub_uint8_t *)arp_header + sizeof(*arp_header);
|
||||
/* Sender hardware address */
|
||||
grub_memcpy(aux, &inf->hwaddress.mac, 6);
|
||||
aux = (char *) arp_header + sizeof (*arp_header);
|
||||
/* Sender hardware address. */
|
||||
grub_memcpy (aux, &inf->hwaddress.mac, 6);
|
||||
|
||||
aux += 6;
|
||||
/* Sender protocol address */
|
||||
grub_memcpy(aux, &inf->address.ipv4, 4);
|
||||
grub_memcpy (aux, &inf->address.ipv4, 4);
|
||||
aux += 4;
|
||||
/* Target hardware address */
|
||||
for(i = 0; i < 6; i++)
|
||||
|
@ -72,49 +76,40 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
|
|||
aux += 6;
|
||||
/* Target protocol address */
|
||||
grub_memcpy(aux, &proto_addr->ipv4, 4);
|
||||
|
||||
grub_memset (&target_hw_addr.mac, 0xff, 6);
|
||||
|
||||
send_ethernet_packet (inf, nb, target_hw_addr, GRUB_NET_ETHERTYPE_ARP);
|
||||
grub_netbuff_clear(nb);
|
||||
grub_netbuff_reserve(nb, 2048);
|
||||
|
||||
grub_uint64_t start_time, current_time;
|
||||
start_time = grub_get_time_ms();
|
||||
do
|
||||
send_ethernet_packet (inf, &nb, target_hw_addr, GRUB_NET_ETHERTYPE_ARP);
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
grub_net_recv_ethernet_packet (inf, nb, GRUB_NET_ETHERTYPE_ARP);
|
||||
/* Now check cache table again */
|
||||
entry = arp_find_entry(proto_addr);
|
||||
entry = arp_find_entry (proto_addr);
|
||||
if (entry)
|
||||
{
|
||||
grub_memcpy(hw_addr, &entry->ll_address, sizeof (*hw_addr));
|
||||
grub_netbuff_clear(nb);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
current_time = grub_get_time_ms();
|
||||
if (current_time - start_time > 3000)
|
||||
break;
|
||||
} while (! entry);
|
||||
grub_netbuff_clear(nb);
|
||||
{
|
||||
grub_memcpy (hw_addr, &entry->ll_address, sizeof (*hw_addr));
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
grub_net_pool_cards (200);
|
||||
|
||||
}
|
||||
|
||||
return grub_error (GRUB_ERR_TIMEOUT, "Timeout: could not resolve hardware address.");
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_net_arp_receive (struct grub_net_network_level_interface *inf,
|
||||
struct grub_net_buff *nb)
|
||||
grub_net_arp_receive (struct grub_net_buff *nb)
|
||||
{
|
||||
struct arphdr *arp_header = (struct arphdr *)nb->data;
|
||||
struct arp_entry *entry;
|
||||
grub_uint8_t *sender_hardware_address, *sender_protocol_address;
|
||||
grub_uint8_t *target_hardware_address, *target_protocol_address;
|
||||
grub_net_network_level_address_t hwaddress;
|
||||
struct grub_net_network_level_interface *inf;
|
||||
|
||||
sender_hardware_address = (grub_uint8_t *) arp_header + sizeof(*arp_header);
|
||||
sender_protocol_address = sender_hardware_address + arp_header->hln;
|
||||
target_hardware_address = sender_protocol_address + arp_header->pln;
|
||||
target_protocol_address = target_hardware_address + arp_header->hln;
|
||||
grub_memcpy (&hwaddress.ipv4, sender_protocol_address, 4);
|
||||
|
||||
/* Check if the sender is in the cache table */
|
||||
entry = arp_find_entry(&hwaddress);
|
||||
/* Update sender hardware address */
|
||||
|
@ -134,21 +129,24 @@ grub_net_arp_receive (struct grub_net_network_level_interface *inf,
|
|||
new_table_entry = 0;
|
||||
}
|
||||
|
||||
/* Am I the protocol address target? */
|
||||
if (grub_memcmp(target_protocol_address, inf->hwaddress.mac, 6) == 0
|
||||
&& grub_be_to_cpu16 (arp_header->op) == ARP_REQUEST)
|
||||
FOR_NET_NETWORK_LEVEL_INTERFACES(inf)
|
||||
{
|
||||
grub_net_link_level_address_t aux;
|
||||
/* Swap hardware fields */
|
||||
grub_memcpy(target_hardware_address, sender_hardware_address, arp_header->hln);
|
||||
grub_memcpy(sender_hardware_address, inf->hwaddress.mac, 6);
|
||||
grub_memcpy(aux.mac, sender_protocol_address, 6);
|
||||
grub_memcpy(sender_protocol_address, target_protocol_address, arp_header->pln);
|
||||
grub_memcpy(target_protocol_address, aux.mac, arp_header->pln);
|
||||
/* Change operation to REPLY and send packet */
|
||||
arp_header->op = grub_be_to_cpu16 (ARP_REPLY);
|
||||
grub_memcpy (aux.mac, target_hardware_address, 6);
|
||||
send_ethernet_packet (inf, nb, aux, GRUB_NET_ETHERTYPE_ARP);
|
||||
/* Am I the protocol address target? */
|
||||
if (grub_memcmp (target_protocol_address, &inf->address.ipv4, 6) == 0
|
||||
&& grub_be_to_cpu16 (arp_header->op) == ARP_REQUEST)
|
||||
{
|
||||
grub_net_link_level_address_t aux;
|
||||
/* Swap hardware fields */
|
||||
grub_memcpy (target_hardware_address, sender_hardware_address, arp_header->hln);
|
||||
grub_memcpy (sender_hardware_address, inf->hwaddress.mac, 6);
|
||||
grub_memcpy (aux.mac, sender_protocol_address, 6);
|
||||
grub_memcpy (sender_protocol_address, target_protocol_address, arp_header->pln);
|
||||
grub_memcpy (target_protocol_address, aux.mac, arp_header->pln);
|
||||
/* Change operation to REPLY and send packet */
|
||||
arp_header->op = grub_be_to_cpu16 (ARP_REPLY);
|
||||
grub_memcpy (aux.mac, target_hardware_address, 6);
|
||||
send_ethernet_packet (inf, nb, aux, GRUB_NET_ETHERTYPE_ARP);
|
||||
}
|
||||
}
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
|
@ -78,12 +78,11 @@ bootp_response_properties[] =
|
|||
{ .name = "bootpreply-packet", .offset = 0x2a },
|
||||
};
|
||||
|
||||
|
||||
grub_bootp_t
|
||||
grub_getbootp( void )
|
||||
static grub_bootp_t
|
||||
grub_getbootp_real ( void )
|
||||
{
|
||||
grub_bootp_t packet = grub_malloc(sizeof *packet);
|
||||
void *bootp_response = NULL;
|
||||
grub_bootp_t packet = grub_malloc (sizeof *packet);
|
||||
char *bootp_response;
|
||||
grub_ssize_t size;
|
||||
unsigned int i;
|
||||
|
||||
|
@ -94,30 +93,27 @@ grub_getbootp( void )
|
|||
break;
|
||||
|
||||
if (size < 0)
|
||||
{
|
||||
grub_printf("Error to get bootp\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bootp_response = grub_malloc (size);
|
||||
if (grub_ieee1275_get_property (grub_ieee1275_chosen,
|
||||
bootp_response_properties[i].name,
|
||||
bootp_response ,
|
||||
size, 0) < 0)
|
||||
{
|
||||
grub_printf("Error to get bootp\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
packet = (void *) ((int)bootp_response
|
||||
+ bootp_response_properties[i].offset);
|
||||
grub_memcpy (packet, bootp_response + bootp_response_properties[i].offset, sizeof (*packet));
|
||||
grub_free (bootp_response);
|
||||
return packet;
|
||||
}
|
||||
|
||||
static
|
||||
void grub_ofnet_findcards (void)
|
||||
{
|
||||
struct grub_net_card *card;
|
||||
grub_ieee1275_phandle_t devhandle;
|
||||
grub_net_link_level_address_t lla;
|
||||
int i = 0;
|
||||
|
||||
auto int search_net_devices (struct grub_ieee1275_devalias *alias);
|
||||
|
||||
int search_net_devices (struct grub_ieee1275_devalias *alias)
|
||||
|
@ -128,53 +124,82 @@ void grub_ofnet_findcards (void)
|
|||
card = grub_malloc (sizeof (struct grub_net_card));
|
||||
struct grub_ofnetcard_data *ofdata = grub_malloc (sizeof (struct grub_ofnetcard_data));
|
||||
ofdata->path = grub_strdup (alias->path);
|
||||
card->data = ofdata;
|
||||
|
||||
grub_ieee1275_finddevice (ofdata->path, &devhandle);
|
||||
|
||||
if (grub_ieee1275_get_integer_property
|
||||
(devhandle, "max-frame-size", &(ofdata->mtu), sizeof (ofdata->mtu), 0))
|
||||
return grub_error (GRUB_ERR_IO, "Couldn't retrieve mtu size.");
|
||||
|
||||
if (grub_ieee1275_get_property (devhandle, "mac-address", &(lla.mac), 6, 0))
|
||||
return grub_error (GRUB_ERR_IO, "Couldn't retrieve mac address.");
|
||||
|
||||
lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
|
||||
card->default_address = lla;
|
||||
|
||||
card->data = ofdata;
|
||||
card->flags = 0;
|
||||
card->name = grub_xasprintf("eth%d",i++); // grub_strdup (alias->name);
|
||||
grub_net_card_register (card);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*Look at all nodes for devices of the type network*/
|
||||
/* Look at all nodes for devices of the type network. */
|
||||
grub_ieee1275_devices_iterate (search_net_devices);
|
||||
|
||||
}
|
||||
|
||||
static
|
||||
void grub_ofnet_probecards (void)
|
||||
{
|
||||
struct grub_net_card *card;
|
||||
struct grub_net_card_driver *driver;
|
||||
struct grub_net_network_level_interface *inter;
|
||||
grub_bootp_t bootp_pckt;
|
||||
grub_net_network_level_address_t addr;
|
||||
grub_net_network_level_netaddress_t net;
|
||||
|
||||
/*Assign correspondent driver for each device. */
|
||||
/* Assign correspondent driver for each device. */
|
||||
FOR_NET_CARDS (card)
|
||||
{
|
||||
FOR_NET_CARD_DRIVERS (driver)
|
||||
{
|
||||
if (driver->init(card) == GRUB_ERR_NONE)
|
||||
{
|
||||
card->driver = driver;
|
||||
continue;
|
||||
}
|
||||
{
|
||||
card->driver = driver;
|
||||
bootp_pckt = grub_getbootp ();
|
||||
if (bootp_pckt)
|
||||
{
|
||||
addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
|
||||
addr.ipv4 = bootp_pckt->yiaddr;
|
||||
grub_net_add_addr ("bootp_cli_addr", card, addr, card->default_address, 0);
|
||||
FOR_NET_NETWORK_LEVEL_INTERFACES (inter)
|
||||
if (grub_strcmp (inter->name, "bootp_cli_addr") == 0)
|
||||
break;
|
||||
net.type = addr.type;
|
||||
net.ipv4.base = addr.ipv4;
|
||||
net.ipv4.masksize = 24;
|
||||
grub_net_add_route ("bootp-router", net, inter);
|
||||
}
|
||||
grub_free (bootp_pckt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GRUB_MOD_INIT(ofnet)
|
||||
GRUB_MOD_INIT (ofnet)
|
||||
{
|
||||
grub_getbootp = grub_getbootp_real;
|
||||
grub_net_card_driver_register (&ofdriver);
|
||||
grub_ofnet_findcards();
|
||||
grub_ofnet_probecards();
|
||||
|
||||
/*init tftp stack - will be handled by module subsystem in the future*/
|
||||
tftp_ini ();
|
||||
/*get bootp packet - won't be needed in the future*/
|
||||
bootp_pckt = grub_getbootp ();
|
||||
grub_disknet_init();
|
||||
grub_ofnet_findcards ();
|
||||
grub_ofnet_probecards ();
|
||||
}
|
||||
|
||||
GRUB_MODE_FINI(ofnet)
|
||||
GRUB_MOD_FINI (ofnet)
|
||||
{
|
||||
grub_net_card_driver_unregister (&ofdriver);
|
||||
grub_getbootp = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -23,23 +23,20 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
|
|||
|
||||
eth->type = grub_cpu_to_be16 (ethertype);
|
||||
|
||||
return inf->card->driver->send (inf->card,nb);
|
||||
return inf->card->driver->send (inf->card, nb);
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_net_recv_ethernet_packet (struct grub_net_network_level_interface *inf,
|
||||
struct grub_net_buff *nb,
|
||||
grub_uint16_t ethertype)
|
||||
grub_net_recv_ethernet_packet (struct grub_net_buff *nb)
|
||||
{
|
||||
struct etherhdr *eth;
|
||||
struct llchdr *llch;
|
||||
struct snaphdr *snaph;
|
||||
grub_uint16_t type;
|
||||
|
||||
inf->card->driver->recv (inf->card, nb);
|
||||
eth = (struct etherhdr *) nb->data;
|
||||
type = grub_be_to_cpu16 (eth->type);
|
||||
grub_netbuff_pull(nb,sizeof (*eth));
|
||||
grub_netbuff_pull (nb, sizeof (*eth));
|
||||
|
||||
if (type <= 1500)
|
||||
{
|
||||
|
@ -56,10 +53,13 @@ grub_net_recv_ethernet_packet (struct grub_net_network_level_interface *inf,
|
|||
|
||||
/* ARP packet */
|
||||
if (type == GRUB_NET_ETHERTYPE_ARP)
|
||||
grub_net_arp_receive(inf, nb);
|
||||
{
|
||||
grub_net_arp_receive (nb);
|
||||
grub_netbuff_free (nb);
|
||||
}
|
||||
/* IP packet */
|
||||
if(type == GRUB_NET_ETHERTYPE_IP && ethertype == GRUB_NET_ETHERTYPE_IP)
|
||||
return GRUB_ERR_NONE;
|
||||
if (type == GRUB_NET_ETHERTYPE_IP)
|
||||
grub_net_recv_ip_packets (nb);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
|
@ -292,7 +292,7 @@ grub_pxefs_label (grub_device_t device __attribute ((unused)),
|
|||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static struct grub_fs grub_pxefs_fs =
|
||||
static struct grub_net_app_protocol grub_pxefs_fs =
|
||||
{
|
||||
.name = "pxe",
|
||||
.dir = grub_pxefs_dir,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <grub/net/ip.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/net/arp.h>
|
||||
#include <grub/net/udp.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
|
@ -33,7 +34,7 @@ grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
|
|||
grub_net_link_level_address_t ll_target_addr;
|
||||
grub_err_t err;
|
||||
|
||||
grub_netbuff_push(nb,sizeof(*iph));
|
||||
grub_netbuff_push (nb, sizeof(*iph));
|
||||
iph = (struct iphdr *) nb->data;
|
||||
|
||||
iph->verhdrlen = ((4 << 4) | 5);
|
||||
|
@ -49,17 +50,15 @@ grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
|
|||
iph->chksum = 0 ;
|
||||
iph->chksum = ipchksum((void *)nb->data, sizeof(*iph));
|
||||
|
||||
/* Determine link layer target address via ARP */
|
||||
err = grub_net_arp_resolve(inf, target, &ll_target_addr);
|
||||
/* Determine link layer target address via ARP. */
|
||||
err = grub_net_arp_resolve (inf, target, &ll_target_addr);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return send_ethernet_packet (inf, nb, ll_target_addr, GRUB_NET_ETHERTYPE_IP);
|
||||
}
|
||||
|
||||
/*
|
||||
static int
|
||||
ip_filter (struct grub_net_buff *nb,
|
||||
struct grub_net_network_level_interface *inf)
|
||||
ip_filter (struct grub_net_buff *nb)
|
||||
{
|
||||
struct iphdr *iph = (struct iphdr *) nb->data;
|
||||
grub_err_t err;
|
||||
|
@ -80,15 +79,22 @@ ip_filter (struct grub_net_buff *nb,
|
|||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
*/
|
||||
grub_err_t
|
||||
grub_net_recv_ip_packets (struct grub_net_network_level_interface *inf)
|
||||
grub_net_recv_ip_packets (struct grub_net_buff *nb)
|
||||
{
|
||||
struct grub_net_buff *nb;
|
||||
nb = grub_netbuff_alloc (2048);
|
||||
if (!nb)
|
||||
return grub_errno;
|
||||
grub_net_recv_ethernet_packet (inf, nb, GRUB_NET_ETHERTYPE_IP);
|
||||
ip_filter (nb, inf);
|
||||
struct iphdr *iph = (struct iphdr *) nb->data;
|
||||
grub_netbuff_pull (nb, sizeof (*iph));
|
||||
|
||||
switch (iph->protocol)
|
||||
{
|
||||
case IP_UDP:
|
||||
return grub_net_recv_udp_packet (nb);
|
||||
break;
|
||||
default:
|
||||
grub_netbuff_free (nb);
|
||||
break;
|
||||
}
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
|
@ -17,8 +17,12 @@
|
|||
*/
|
||||
|
||||
#include <grub/net.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/time.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/command.h>
|
||||
#include <grub/env.h>
|
||||
|
@ -40,6 +44,7 @@ struct grub_net_route
|
|||
struct grub_net_route *grub_net_routes = NULL;
|
||||
struct grub_net_network_level_interface *grub_net_network_level_interfaces = NULL;
|
||||
struct grub_net_card *grub_net_cards = NULL;
|
||||
struct grub_net_card_driver *grub_net_card_drivers = NULL;
|
||||
struct grub_net_network_level_protocol *grub_net_network_level_protocols = NULL;
|
||||
|
||||
static inline void
|
||||
|
@ -105,7 +110,7 @@ match_net (const grub_net_network_level_netaddress_t *net,
|
|||
{
|
||||
case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
|
||||
{
|
||||
grub_int32_t mask = (1 << net->ipv4.masksize) - 1;
|
||||
grub_int32_t mask = ((1 << net->ipv4.masksize) - 1) << (32 - net->ipv4.masksize);
|
||||
return ((grub_be_to_cpu32 (net->ipv4.base) & mask)
|
||||
== (grub_be_to_cpu32 (addr->ipv4) & mask));
|
||||
}
|
||||
|
@ -538,6 +543,7 @@ grub_cmd_listcards (struct grub_command *cmd __attribute__ ((unused)),
|
|||
}
|
||||
|
||||
grub_net_app_level_t grub_net_app_level_list;
|
||||
struct grub_net_socket *grub_net_sockets;
|
||||
|
||||
static grub_net_t
|
||||
grub_net_open_real (const char *name)
|
||||
|
@ -570,6 +576,159 @@ grub_net_open_real (const char *name)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_net_file_open_real (struct grub_file *file, const char *name)
|
||||
{
|
||||
grub_err_t err;
|
||||
grub_net_network_level_address_t addr;
|
||||
struct grub_net_network_level_interface *inf;
|
||||
grub_net_network_level_address_t gateway;
|
||||
grub_net_socket_t socket;
|
||||
static int port = 25300;
|
||||
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof (file->device->net->protocol->name) + 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = grub_net_route_address (addr, &gateway, &inf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if ((socket = (grub_net_socket_t) grub_malloc (sizeof (*socket))) == NULL)
|
||||
return GRUB_ERR_OUT_OF_MEMORY;
|
||||
|
||||
socket->inf = inf;
|
||||
socket->out_nla = addr;
|
||||
socket->in_port = port++;
|
||||
socket->status = 0;
|
||||
socket->app = file->device->net->protocol;
|
||||
socket->packs = NULL;
|
||||
file->device->net->socket = socket;
|
||||
grub_net_socket_register (socket);
|
||||
|
||||
if ((err = file->device->net->protocol->open (file,name)))
|
||||
goto fail;
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
fail:
|
||||
grub_net_socket_unregister (socket);
|
||||
grub_free (socket);
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
receive_packets (struct grub_net_card *card)
|
||||
{
|
||||
/* Maybe should be better have a fixed number of packets for each card
|
||||
and just mark them as used and not used. */
|
||||
struct grub_net_buff *nb;
|
||||
nb = grub_netbuff_alloc (1500);
|
||||
if (!nb)
|
||||
return grub_errno;
|
||||
|
||||
card->driver->recv (card, nb);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
void
|
||||
grub_net_pool_cards (unsigned time)
|
||||
{
|
||||
struct grub_net_card *card;
|
||||
grub_uint64_t start_time;
|
||||
FOR_NET_CARDS (card)
|
||||
{
|
||||
start_time = grub_get_time_ms ();
|
||||
while( (grub_get_time_ms () - start_time) < time)
|
||||
receive_packets (card);
|
||||
}
|
||||
}
|
||||
|
||||
static grub_ssize_t
|
||||
process_packets (grub_file_t file, void *buf, grub_size_t len,
|
||||
void *NESTED_FUNC_ATTR (hook) (void *dest, const void *src, grub_size_t n))
|
||||
{
|
||||
grub_net_socket_t sock = file->device->net->socket;
|
||||
struct grub_net_buff *nb;
|
||||
char *ptr = (char *) buf;
|
||||
grub_size_t amount, total = 0;
|
||||
int try = 0;
|
||||
while (try <= 3)
|
||||
{
|
||||
while (sock->packs->first)
|
||||
{
|
||||
try = 0;
|
||||
nb = sock->packs->first->nb;
|
||||
amount = (grub_size_t)(len <= (grub_size_t) (nb->tail - nb->data))? len :(grub_size_t)(nb->tail - nb->data);
|
||||
len -= amount;
|
||||
total += amount;
|
||||
hook (ptr, nb->data, amount);
|
||||
ptr += amount;
|
||||
if (amount == (grub_size_t) (nb->tail - nb->data))
|
||||
{
|
||||
grub_netbuff_free (nb);
|
||||
grub_net_remove_packet (sock->packs->first);
|
||||
}
|
||||
else
|
||||
nb->data += amount;
|
||||
|
||||
if (!len)
|
||||
return total;
|
||||
}
|
||||
if (sock->status == 1)
|
||||
{
|
||||
try++;
|
||||
grub_net_pool_cards (200);
|
||||
}
|
||||
else
|
||||
return total;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
/* Read from the packets list*/
|
||||
static grub_ssize_t
|
||||
grub_net_read_real (grub_file_t file, void *buf, grub_size_t len )
|
||||
{
|
||||
auto void *NESTED_FUNC_ATTR memcpy_hook (void *dest, const void *src, grub_size_t n);
|
||||
void *NESTED_FUNC_ATTR memcpy_hook (void *dest __attribute__ ((unused)), const void *src __attribute__ ((unused)),
|
||||
grub_size_t n __attribute__ ((unused)))
|
||||
{
|
||||
return grub_memcpy (dest, src, n);
|
||||
}
|
||||
return process_packets (file, buf, len, memcpy_hook);
|
||||
}
|
||||
|
||||
/* Read from the packets list*/
|
||||
static grub_err_t
|
||||
grub_net_seek_real (struct grub_file *file, grub_off_t offset)
|
||||
{
|
||||
grub_net_socket_t sock = file->device->net->socket;
|
||||
struct grub_net_buff *nb;
|
||||
grub_size_t len = offset - file->offset;
|
||||
|
||||
if (!len)
|
||||
return GRUB_ERR_NONE;
|
||||
|
||||
/* We cant seek backwards past the current packet. */
|
||||
if (file->offset > offset)
|
||||
{
|
||||
nb = sock->packs->first->nb;
|
||||
return grub_netbuff_push (nb, file->offset - offset);
|
||||
}
|
||||
|
||||
auto void *NESTED_FUNC_ATTR dummy (void *dest, const void *src, grub_size_t n);
|
||||
void *NESTED_FUNC_ATTR dummy (void *dest __attribute__ ((unused)), const void *src __attribute__ ((unused)),
|
||||
grub_size_t n __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
process_packets (file, NULL, len, dummy);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static char *
|
||||
grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
|
||||
const char *val __attribute__ ((unused)))
|
||||
|
@ -857,6 +1016,9 @@ GRUB_MOD_INIT(net)
|
|||
N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value."));
|
||||
|
||||
grub_net_open = grub_net_open_real;
|
||||
grub_file_net_open = grub_net_file_open_real;
|
||||
grub_file_net_read = grub_net_read_real;
|
||||
grub_file_net_seek = grub_net_seek_real;
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(net)
|
||||
|
@ -869,4 +1031,6 @@ GRUB_MOD_FINI(net)
|
|||
grub_unregister_command (cmd_lscards);
|
||||
grub_unregister_command (cmd_getdhcp);
|
||||
grub_net_open = NULL;
|
||||
grub_file_net_read = NULL;
|
||||
grub_file_net_open = NULL;
|
||||
}
|
||||
|
|
|
@ -22,43 +22,43 @@
|
|||
#include <grub/net/netbuff.h>
|
||||
|
||||
|
||||
grub_err_t grub_netbuff_put (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
grub_err_t grub_netbuff_put (struct grub_net_buff *nb ,grub_size_t len)
|
||||
{
|
||||
net_buff->tail += len;
|
||||
if (net_buff->tail > net_buff->end)
|
||||
nb->tail += len;
|
||||
if (nb->tail > nb->end)
|
||||
return grub_error (GRUB_ERR_OUT_OF_RANGE, "put out of the packet range.");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_unput (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
grub_err_t grub_netbuff_unput (struct grub_net_buff *nb ,grub_size_t len)
|
||||
{
|
||||
net_buff->tail -= len;
|
||||
if (net_buff->tail < net_buff->head)
|
||||
nb->tail -= len;
|
||||
if (nb->tail < nb->head)
|
||||
return grub_error (GRUB_ERR_OUT_OF_RANGE, "unput out of the packet range.");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_push (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
grub_err_t grub_netbuff_push (struct grub_net_buff *nb ,grub_size_t len)
|
||||
{
|
||||
net_buff->data -= len;
|
||||
if (net_buff->data < net_buff->head)
|
||||
nb->data -= len;
|
||||
if (nb->data < nb->head)
|
||||
return grub_error (GRUB_ERR_OUT_OF_RANGE, "push out of the packet range.");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_pull (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
grub_err_t grub_netbuff_pull (struct grub_net_buff *nb ,grub_size_t len)
|
||||
{
|
||||
net_buff->data += len;
|
||||
if (net_buff->data > net_buff->end)
|
||||
nb->data += len;
|
||||
if (nb->data > nb->end)
|
||||
return grub_error (GRUB_ERR_OUT_OF_RANGE, "pull out of the packet range.");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_reserve (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
grub_err_t grub_netbuff_reserve (struct grub_net_buff *nb ,grub_size_t len)
|
||||
{
|
||||
net_buff->data += len;
|
||||
net_buff->tail += len;
|
||||
if ((net_buff->tail > net_buff->end) || (net_buff->data > net_buff->end))
|
||||
nb->data += len;
|
||||
nb->tail += len;
|
||||
if ((nb->tail > nb->end) || (nb->data > nb->end))
|
||||
return grub_error (GRUB_ERR_OUT_OF_RANGE, "reserve out of the packet range.");
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
@ -73,22 +73,23 @@ struct grub_net_buff *grub_netbuff_alloc ( grub_size_t len )
|
|||
|
||||
len = ALIGN_UP (len,NETBUFF_ALIGN);
|
||||
data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
|
||||
if (!data)
|
||||
return NULL;
|
||||
nb = (struct grub_net_buff *) ((grub_uint8_t *) data + len);
|
||||
nb->head = nb->data = nb->tail = data;
|
||||
nb->end = (char *) nb;
|
||||
|
||||
nb->end = (char *) nb;
|
||||
return nb;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_free (struct grub_net_buff *net_buff)
|
||||
grub_err_t grub_netbuff_free (struct grub_net_buff *nb)
|
||||
{
|
||||
grub_free (net_buff->head);
|
||||
grub_free (nb->head);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_clear (struct grub_net_buff *net_buff)
|
||||
grub_err_t grub_netbuff_clear (struct grub_net_buff *nb)
|
||||
{
|
||||
net_buff->data = net_buff->tail = net_buff->head;
|
||||
nb->data = nb->tail = nb->head;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,26 +14,23 @@ tftp_open (struct grub_file *file, const char *filename)
|
|||
{
|
||||
struct tftphdr *tftph;
|
||||
char *rrq;
|
||||
char *ptr;
|
||||
int i;
|
||||
int rrqlen;
|
||||
int hdrlen;
|
||||
struct grub_net_buff *nb;
|
||||
grub_net_network_level_address_t addr;
|
||||
int hdrlen;
|
||||
char open_data[1500];
|
||||
struct grub_net_buff nb;
|
||||
tftp_data_t data = grub_malloc (sizeof *data);
|
||||
grub_err_t err;
|
||||
|
||||
file->device->net->socket->data = (void *) data;
|
||||
nb.head = open_data;
|
||||
nb.end = open_data + sizeof (open_data);
|
||||
grub_netbuff_clear (&nb);
|
||||
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof ("tftp,") - 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
grub_netbuff_reserve (&nb,1500);
|
||||
grub_netbuff_push (&nb,sizeof (*tftph));
|
||||
|
||||
nb = grub_netbuff_alloc (2048);
|
||||
if (!nb)
|
||||
return grub_errno;
|
||||
|
||||
grub_netbuff_reserve (nb,2048);
|
||||
grub_netbuff_push (nb,sizeof (*tftph));
|
||||
|
||||
tftph = (struct tftphdr *) nb->data;
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
|
||||
rrq = (char *) tftph->u.rrq;
|
||||
rrqlen = 0;
|
||||
|
@ -64,89 +61,93 @@ tftp_open (struct grub_file *file, const char *filename)
|
|||
rrq += grub_strlen ("0") + 1;
|
||||
hdrlen = sizeof (tftph->opcode) + rrqlen;
|
||||
|
||||
grub_netbuff_unput (nb,nb->tail - (nb->data+hdrlen));
|
||||
grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen));
|
||||
|
||||
err = grub_net_send_udp_packet (&addr,
|
||||
nb, TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
file->device->net->socket->out_port = TFTP_SERVER_PORT;
|
||||
|
||||
err = grub_net_send_udp_packet (file->device->net->socket, &nb);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
/* Receive OACK. */
|
||||
grub_netbuff_clear (nb);
|
||||
grub_netbuff_reserve (nb,2048);
|
||||
|
||||
do
|
||||
/* Receive OACK packet. */
|
||||
for ( i = 0; i < 3; i++)
|
||||
{
|
||||
err = grub_net_recv_udp_packet (&addr, nb,
|
||||
TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
if (err)
|
||||
return err;
|
||||
grub_net_pool_cards (100);
|
||||
if (grub_errno)
|
||||
return grub_errno;
|
||||
if (file->device->net->socket->status != 0)
|
||||
break;
|
||||
/* Retry. */
|
||||
//err = grub_net_send_udp_packet (file->device->net->socket, &nb);
|
||||
// if (err)
|
||||
// return err;
|
||||
}
|
||||
while (nb->tail == nb->data);
|
||||
|
||||
file->size = 0;
|
||||
if (file->device->net->socket->status == 0)
|
||||
return grub_error (GRUB_ERR_TIMEOUT,"Time out opening tftp.");
|
||||
file->size = data->file_size;
|
||||
|
||||
for (ptr = nb->data; ptr < nb->tail; )
|
||||
grub_printf ("%02x ", *ptr);
|
||||
|
||||
for (ptr = nb->data; ptr < nb->tail; )
|
||||
{
|
||||
if (grub_memcmp (ptr, "tsize\0=", sizeof ("tsize\0=") - 1) == 0)
|
||||
{
|
||||
file->size = grub_strtoul (ptr + sizeof ("tsize\0=") - 1, 0, 0);
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
}
|
||||
while (ptr < nb->tail && *ptr)
|
||||
ptr++;
|
||||
ptr++;
|
||||
}
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_ssize_t
|
||||
tftp_receive (struct grub_file *file, char *buf, grub_size_t len)
|
||||
static grub_err_t
|
||||
tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
|
||||
{
|
||||
struct tftphdr *tftph;
|
||||
// char *token,*value,*temp;
|
||||
char nbdata[128];
|
||||
tftp_data_t data = sock->data;
|
||||
grub_err_t err;
|
||||
grub_net_network_level_address_t addr;
|
||||
struct grub_net_buff nb;
|
||||
char *ptr;
|
||||
struct grub_net_buff nb_ack;
|
||||
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof ("tftp,") - 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
nb_ack.head = nbdata;
|
||||
nb_ack.end = nbdata + sizeof (nbdata);
|
||||
|
||||
grub_net_recv_udp_packet (&addr, &nb,
|
||||
TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
|
||||
tftph = (struct tftphdr *) nb->data;
|
||||
switch (grub_be_to_cpu16 (tftph->opcode))
|
||||
{
|
||||
case TFTP_OACK:
|
||||
for (ptr = nb->data; ptr < nb->tail; )
|
||||
{
|
||||
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
|
||||
data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1, 0, 0);
|
||||
while (ptr < nb->tail && *ptr)
|
||||
ptr++;
|
||||
ptr++;
|
||||
}
|
||||
sock->status = 1;
|
||||
data->block = 0;
|
||||
grub_netbuff_clear(nb);
|
||||
break;
|
||||
case TFTP_DATA:
|
||||
grub_netbuff_pull (&nb,sizeof (tftph->opcode) + sizeof (tftph->u.data.block));
|
||||
// if (tftph->u.data.block == block + 1)
|
||||
//{
|
||||
// block = tftph->u.data.block;
|
||||
grub_memcpy (buf, nb.data, len);
|
||||
//}
|
||||
//else
|
||||
//grub_netbuff_clear(&nb);
|
||||
break;
|
||||
grub_netbuff_pull (nb,sizeof (tftph->opcode) + sizeof (tftph->u.data.block));
|
||||
|
||||
if (grub_be_to_cpu16 (tftph->u.data.block) == data->block + 1)
|
||||
{
|
||||
data->block++;
|
||||
if (nb->tail - nb->data < 1024)
|
||||
sock->status = 2;
|
||||
}
|
||||
else
|
||||
grub_netbuff_clear(nb);
|
||||
|
||||
break;
|
||||
case TFTP_ERROR:
|
||||
grub_netbuff_clear (&nb);
|
||||
grub_netbuff_clear (nb);
|
||||
return grub_error (GRUB_ERR_IO, (char *)tftph->u.err.errmsg);
|
||||
break;
|
||||
}
|
||||
grub_netbuff_clear (&nb_ack);
|
||||
grub_netbuff_reserve (&nb_ack,128);
|
||||
grub_netbuff_push (&nb_ack,sizeof (tftph->opcode) + sizeof (tftph->u.ack.block));
|
||||
|
||||
nb.data = nb.tail = nb.end;
|
||||
|
||||
grub_netbuff_push (&nb,sizeof (tftph->opcode) + sizeof (tftph->u.ack.block));
|
||||
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
tftph = (struct tftphdr *) nb_ack.data;
|
||||
tftph->opcode = grub_cpu_to_be16 (TFTP_ACK);
|
||||
// tftph->u.ack.block = block;
|
||||
tftph->u.ack.block = data->block;
|
||||
|
||||
return grub_net_send_udp_packet (&addr, &nb, TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
err = grub_net_send_udp_packet (sock, &nb_ack);
|
||||
return err;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
|
@ -155,7 +156,7 @@ tftp_close (struct grub_file *file __attribute__ ((unused)))
|
|||
return 0;
|
||||
}
|
||||
|
||||
static struct grub_fs grub_tftp_protocol =
|
||||
static struct grub_net_app_protocol grub_tftp_protocol =
|
||||
{
|
||||
.name = "tftp",
|
||||
.open = tftp_open,
|
||||
|
|
|
@ -5,63 +5,51 @@
|
|||
#include <grub/time.h>
|
||||
|
||||
grub_err_t
|
||||
grub_net_send_udp_packet (const grub_net_network_level_address_t *target,
|
||||
struct grub_net_buff *nb, grub_uint16_t srcport,
|
||||
grub_uint16_t destport)
|
||||
grub_net_send_udp_packet (const grub_net_socket_t socket , struct grub_net_buff *nb)
|
||||
{
|
||||
struct udphdr *udph;
|
||||
struct grub_net_network_level_interface *inf;
|
||||
grub_err_t err;
|
||||
grub_net_network_level_address_t gateway;
|
||||
|
||||
err = grub_net_route_address (*target, &gateway, &inf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
grub_netbuff_push (nb,sizeof(*udph));
|
||||
|
||||
udph = (struct udphdr *) nb->data;
|
||||
udph->src = grub_cpu_to_be16 (srcport);
|
||||
udph->dst = grub_cpu_to_be16 (destport);
|
||||
udph->src = grub_cpu_to_be16 (socket->in_port);
|
||||
udph->dst = grub_cpu_to_be16 (socket->out_port);
|
||||
|
||||
/* No chechksum. */
|
||||
udph->chksum = 0;
|
||||
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
|
||||
|
||||
return grub_net_send_ip_packet (inf, target, nb);
|
||||
return grub_net_send_ip_packet (socket->inf, &(socket->out_nla), nb);
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_net_recv_udp_packet (const grub_net_network_level_address_t *target,
|
||||
struct grub_net_buff *buf,
|
||||
grub_uint16_t srcport, grub_uint16_t destport)
|
||||
grub_net_recv_udp_packet (struct grub_net_buff *nb)
|
||||
{
|
||||
grub_err_t err;
|
||||
struct grub_net_packet *pkt;
|
||||
struct grub_net_network_level_interface *inf;
|
||||
grub_net_network_level_address_t gateway;
|
||||
//grub_err_t err;
|
||||
struct udphdr *udph;
|
||||
grub_net_socket_t sock;
|
||||
udph = (struct udphdr *) nb->data;
|
||||
grub_netbuff_pull (nb, sizeof (*udph));
|
||||
|
||||
err = grub_net_route_address (*target, &gateway, &inf);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
(void) srcport;
|
||||
|
||||
err = grub_net_recv_ip_packets (inf);
|
||||
|
||||
FOR_NET_NL_PACKETS(inf, pkt)
|
||||
FOR_NET_SOCKETS(sock)
|
||||
{
|
||||
struct udphdr *udph;
|
||||
struct grub_net_buff *nb = pkt->nb;
|
||||
udph = (struct udphdr *) nb->data;
|
||||
if (grub_be_to_cpu16 (udph->dst) == destport)
|
||||
if (grub_be_to_cpu16 (udph->dst) == sock->in_port)
|
||||
{
|
||||
grub_net_remove_packet (pkt);
|
||||
grub_netbuff_pull (nb, sizeof(*udph));
|
||||
grub_memcpy (buf, nb, sizeof (buf));
|
||||
|
||||
if (sock->status == 0)
|
||||
sock->out_port = udph->src;
|
||||
|
||||
|
||||
/* App protocol remove its own reader. */
|
||||
sock->app->read (sock,nb);
|
||||
|
||||
/* If there is data, puts packet in socket list */
|
||||
if ((nb->tail - nb->data) > 0)
|
||||
grub_net_put_packet (sock->packs, nb);
|
||||
else
|
||||
grub_netbuff_free (nb);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
}
|
||||
grub_netbuff_free (nb);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue