Run indent on files.

This commit is contained in:
Manoel Rebelo Abranches 2011-06-07 21:59:53 -03:00
parent 423a1849ef
commit 4700d08bb4
8 changed files with 307 additions and 288 deletions

View file

@ -9,10 +9,10 @@
static struct arp_entry arp_table[10];
static grub_int8_t new_table_entry = -1;
static
void arp_init_table(void)
static void
arp_init_table (void)
{
grub_memset (arp_table, 0, sizeof (arp_table));
grub_memset (arp_table, 0, sizeof (arp_table));
new_table_entry = 0;
}
@ -20,19 +20,19 @@ static struct arp_entry *
arp_find_entry (const grub_net_network_level_address_t *proto)
{
unsigned i;
for(i = 0; i < ARRAY_SIZE (arp_table); i++)
for (i = 0; i < ARRAY_SIZE (arp_table); i++)
{
if(arp_table[i].avail == 1 &&
arp_table[i].nl_address.ipv4 == proto->ipv4)
return &(arp_table[i]);
if (arp_table[i].avail == 1 &&
arp_table[i].nl_address.ipv4 == proto->ipv4)
return &(arp_table[i]);
}
return NULL;
}
grub_err_t
grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *proto_addr,
grub_net_link_level_address_t *hw_addr)
grub_net_arp_resolve (struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *proto_addr,
grub_net_link_level_address_t *hw_addr)
{
struct arp_entry *entry;
struct grub_net_buff nb;
@ -51,10 +51,10 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
/* Build a request packet. */
nb.head = arp_data;
nb.end = arp_data + sizeof (arp_data);
grub_netbuff_clear (&nb);
grub_netbuff_clear (&nb);
grub_netbuff_reserve (&nb,128);
grub_netbuff_push (&nb, sizeof(*arp_header) + 2 * (6 + 4));
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);
@ -71,11 +71,11 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
grub_memcpy (aux, &inf->address.ipv4, 4);
aux += 4;
/* Target hardware address */
for(i = 0; i < 6; i++)
for (i = 0; i < 6; i++)
aux[i] = 0x00;
aux += 6;
/* Target protocol address */
grub_memcpy(aux, &proto_addr->ipv4, 4);
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);
@ -96,56 +96,59 @@ grub_net_arp_resolve(struct grub_net_network_level_interface *inf,
grub_err_t
grub_net_arp_receive (struct grub_net_buff *nb)
{
struct arphdr *arp_header = (struct arphdr *)nb->data;
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_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 */
/* Check if the sender is in the cache table. */
entry = arp_find_entry (&hwaddress);
/* Update sender hardware address. */
if (entry)
grub_memcpy(entry->ll_address.mac, sender_hardware_address, 6);
grub_memcpy (entry->ll_address.mac, sender_hardware_address, 6);
else
{
/* Add sender to cache table */
/* Add sender to cache table. */
if (new_table_entry == -1)
arp_init_table();
arp_init_table ();
entry = &(arp_table[new_table_entry]);
entry->avail = 1;
grub_memcpy(&entry->nl_address.ipv4, sender_protocol_address, 4);
grub_memcpy(entry->ll_address.mac, sender_hardware_address, 6);
grub_memcpy (&entry->nl_address.ipv4, sender_protocol_address, 4);
grub_memcpy (entry->ll_address.mac, sender_hardware_address, 6);
new_table_entry++;
if (new_table_entry == ARRAY_SIZE (arp_table))
new_table_entry = 0;
}
FOR_NET_NETWORK_LEVEL_INTERFACES(inf)
{
/* 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);
}
}
FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
{
/* 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;
}

View file

@ -13,7 +13,7 @@
static int fd;
static grub_err_t
static grub_err_t
send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
struct grub_net_buff *pack)
{
@ -22,7 +22,7 @@ send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
actual = write (fd, pack->data, pack->tail - pack->data);
if (actual < 0)
return grub_error (GRUB_ERR_IO, "couldn't send packets");
return GRUB_ERR_NONE;
}
@ -32,32 +32,32 @@ get_card_packet (struct grub_net_card *dev __attribute__ ((unused)),
{
ssize_t actual;
grub_netbuff_clear(pack);
grub_netbuff_clear (pack);
actual = read (fd, pack->data, 1500);
if (actual < 0)
return -1;
grub_netbuff_put (pack, actual);
return actual;
return actual;
}
static struct grub_net_card_driver emudriver =
{
.name = "emu",
.send = send_card_buffer,
.recv = get_card_packet
};
{
.name = "emu",
.send = send_card_buffer,
.recv = get_card_packet
};
static struct grub_net_card emucard =
{
.name = "emu0",
.driver = &emudriver,
.default_address = {
.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
{ .mac = { 0, 1, 2, 3, 4, 5} }
},
.flags = 0
};
{
.name = "emu0",
.driver = &emudriver,
.default_address = {
.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
{.mac = {0, 1, 2, 3, 4, 5}}
},
.flags = 0
};
GRUB_MOD_INIT(emunet)
{
@ -84,6 +84,3 @@ GRUB_MOD_FINI(emunet)
grub_net_card_unregister (&emucard);
}
}

View file

@ -7,15 +7,18 @@
GRUB_MOD_LICENSE ("GPLv3+");
static grub_err_t
static grub_err_t
card_open (struct grub_net_card *dev)
{
int status;
struct grub_ofnetcard_data *data = dev->data;
char path[grub_strlen(data->path) + grub_strlen(":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512") + 1];
char path[grub_strlen (data->path) +
grub_strlen (":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512") + 1];
/* The full string will prevent a bootp packet to be sent. Just put some valid ip in there. */
grub_snprintf(path,sizeof(path),"%s%s",data->path,":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512");
status = grub_ieee1275_open (path,&(data->handle));
grub_snprintf (path, sizeof (path), "%s%s", data->path,
":speed=auto,duplex=auto,1.1.1.1,dummy,1.1.1.1,1.1.1.1,5,5,1.1.1.1,512");
status = grub_ieee1275_open (path, &(data->handle));
if (status)
return grub_error (GRUB_ERR_IO, "Couldn't open network card.");
@ -23,23 +26,23 @@ card_open (struct grub_net_card *dev)
return GRUB_ERR_NONE;
}
static grub_err_t
static grub_err_t
card_close (struct grub_net_card *dev)
{
struct grub_ofnetcard_data *data = dev->data;
if (data->handle)
grub_ieee1275_close (data->handle);
return GRUB_ERR_NONE;
}
static grub_err_t
static grub_err_t
send_card_buffer (struct grub_net_card *dev, struct grub_net_buff *pack)
{
{
int actual;
int status;
struct grub_ofnetcard_data *data = dev->data;
status = grub_ieee1275_write (data->handle, pack->data,
pack->tail - pack->data, &actual);
@ -56,27 +59,27 @@ get_card_packet (struct grub_net_card *dev, struct grub_net_buff *nb)
struct grub_ofnetcard_data *data = dev->data;
grub_uint64_t start_time;
grub_netbuff_clear (nb);
grub_netbuff_clear (nb);
start_time = grub_get_time_ms ();
do
rc = grub_ieee1275_read (data->handle, nb->data, data->mtu, &actual);
while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
if (actual)
{
grub_netbuff_put (nb, actual);
grub_netbuff_put (nb, actual);
return actual;
}
return -1;
}
static struct grub_net_card_driver ofdriver =
{
.name = "ofnet",
.init = card_open,
.fini = card_close,
.send = send_card_buffer,
.recv = get_card_packet
};
static struct grub_net_card_driver ofdriver =
{
.name = "ofnet",
.init = card_open,
.fini = card_close,
.send = send_card_buffer,
.recv = get_card_packet
};
static const struct
{
@ -84,23 +87,23 @@ static const struct
int offset;
}
bootp_response_properties[] =
{
{ .name = "bootp-response", .offset = 0 },
{ .name = "dhcp-response", .offset = 0 },
{ .name = "bootpreply-packet", .offset = 0x2a },
};
bootp_response_properties[] =
{
{ .name = "bootp-response", .offset = 0},
{ .name = "dhcp-response", .offset = 0},
{ .name = "bootpreply-packet", .offset = 0x2a},
};
static grub_bootp_t
grub_getbootp_real ( void )
static grub_bootp_t
grub_getbootp_real (void)
{
grub_bootp_t packet = grub_malloc (sizeof *packet);
char *bootp_response;
char *bootp_response;
grub_ssize_t size;
unsigned int i;
for ( i = 0; i < ARRAY_SIZE (bootp_response_properties); i++)
if (grub_ieee1275_get_property_length (grub_ieee1275_chosen,
for (i = 0; i < ARRAY_SIZE (bootp_response_properties); i++)
if (grub_ieee1275_get_property_length (grub_ieee1275_chosen,
bootp_response_properties[i].name,
&size) >= 0)
break;
@ -111,8 +114,7 @@ grub_getbootp_real ( void )
bootp_response = grub_malloc (size);
if (grub_ieee1275_get_property (grub_ieee1275_chosen,
bootp_response_properties[i].name,
bootp_response ,
size, 0) < 0)
bootp_response, size, 0) < 0)
return NULL;
grub_memcpy (packet, bootp_response + bootp_response_properties[i].offset, sizeof (*packet));
@ -120,52 +122,55 @@ grub_getbootp_real ( void )
return packet;
}
static
void grub_ofnet_findcards (void)
static void
grub_ofnet_findcards (void)
{
struct grub_net_card *card;
grub_ieee1275_phandle_t devhandle;
grub_net_link_level_address_t lla;
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)
{
if ( !grub_strcmp (alias->type,"network") )
{
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);
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_strcmp (alias->type, "network"))
{
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 = 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->driver = NULL;
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->driver = NULL;
card->data = ofdata;
card->flags = 0;
card->name = grub_xasprintf("eth%d",i++); // grub_strdup (alias->name);
grub_net_card_register (card);
card->name = grub_xasprintf ("eth%d", i++);
grub_net_card_register (card);
return 0;
}
return 0;
}
return 0;
}
/* Look at all nodes for devices of the type network. */
grub_ieee1275_devices_iterate (search_net_devices);
}
static
void grub_ofnet_probecards (void)
static void
grub_ofnet_probecards (void)
{
struct grub_net_card *card;
struct grub_net_card_driver *driver;
@ -175,19 +180,21 @@ void grub_ofnet_probecards (void)
grub_net_network_level_netaddress_t net;
bootp_pckt = grub_getbootp ();
/* 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)
if (driver->init (card) == GRUB_ERR_NONE)
{
card->driver = driver;
if (bootp_pckt && grub_memcmp(bootp_pckt->chaddr,card->default_address.mac,6) == 0)
if (bootp_pckt
&& grub_memcmp (bootp_pckt->chaddr, card->default_address.mac, 6) == 0)
{
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);
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;
@ -197,30 +204,27 @@ void grub_ofnet_probecards (void)
grub_net_add_route ("bootp-router", net, inter);
}
break;
}
}
}
}
grub_free (bootp_pckt);
}
GRUB_MOD_INIT (ofnet)
GRUB_MOD_INIT(ofnet)
{
struct grub_net_card *card;
grub_getbootp = grub_getbootp_real;
grub_net_card_driver_register (&ofdriver);
grub_ofnet_findcards ();
grub_ofnet_probecards ();
FOR_NET_CARDS (card)
grub_ofnet_findcards ();
grub_ofnet_probecards ();
FOR_NET_CARDS (card)
if (card->driver == NULL)
grub_net_card_unregister (card);
}
GRUB_MOD_FINI (ofnet)
GRUB_MOD_FINI(ofnet)
{
grub_net_card_driver_unregister (&ofdriver);
grub_getbootp = NULL;
}

View file

@ -8,25 +8,25 @@
#include <grub/time.h>
#include <grub/net/arp.h>
grub_err_t
grub_err_t
send_ethernet_packet (struct grub_net_network_level_interface *inf,
struct grub_net_buff *nb,
grub_net_link_level_address_t target_addr,
grub_uint16_t ethertype)
{
struct etherhdr *eth;
struct etherhdr *eth;
grub_netbuff_push (nb,sizeof(*eth));
eth = (struct etherhdr *) nb->data;
grub_memcpy(eth->dst, target_addr.mac, 6);
grub_memcpy(eth->src, inf->hwaddress.mac, 6);
grub_netbuff_push (nb, sizeof (*eth));
eth = (struct etherhdr *) nb->data;
grub_memcpy (eth->dst, target_addr.mac, 6);
grub_memcpy (eth->src, inf->hwaddress.mac, 6);
eth->type = grub_cpu_to_be16 (ethertype);
return inf->card->driver->send (inf->card, nb);
}
grub_err_t
grub_err_t
grub_net_recv_ethernet_packet (struct grub_net_buff *nb)
{
struct etherhdr *eth;
@ -34,32 +34,32 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb)
struct snaphdr *snaph;
grub_uint16_t type;
eth = (struct etherhdr *) nb->data;
eth = (struct etherhdr *) nb->data;
type = grub_be_to_cpu16 (eth->type);
grub_netbuff_pull (nb, sizeof (*eth));
if (type <= 1500)
{
llch = (struct llchdr *) nb->data;
type = llch->dsap & LLCADDRMASK;
if (llch->dsap == 0xaa && llch->ssap == 0xaa && llch->ctrl == 0x3)
{
grub_netbuff_pull (nb,sizeof(*llch));
{
grub_netbuff_pull (nb, sizeof (*llch));
snaph = (struct snaphdr *) nb->data;
type = snaph->type;
}
}
/* ARP packet */
/* ARP packet. */
if (type == GRUB_NET_ETHERTYPE_ARP)
{
grub_net_arp_receive (nb);
grub_netbuff_free (nb);
}
/* IP packet */
/* IP packet. */
if (type == GRUB_NET_ETHERTYPE_IP)
grub_net_recv_ip_packets (nb);
return GRUB_ERR_NONE;
return GRUB_ERR_NONE;
}

View file

@ -8,54 +8,55 @@
#include <grub/mm.h>
grub_uint16_t
ipchksum(void *ipv, int len)
ipchksum (void *ipv, int len)
{
grub_uint16_t *ip = (grub_uint16_t *)ipv;
grub_uint32_t sum = 0;
grub_uint16_t *ip = (grub_uint16_t *) ipv;
grub_uint32_t sum = 0;
len >>= 1;
while (len--)
{
sum += grub_be_to_cpu16 (*(ip++));
if (sum > 0xFFFF)
sum -= 0xFFFF;
}
len >>= 1;
while (len--)
{
sum += grub_be_to_cpu16 (*(ip++));
if (sum > 0xFFFF)
sum -= 0xFFFF;
}
return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
return grub_cpu_to_be16 ((~sum) & 0x0000FFFF);
}
grub_err_t
grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
const grub_net_network_level_address_t *target,
struct grub_net_buff *nb)
{
{
struct iphdr *iph;
static int id = 0x2400;
static int id = 0x2400;
grub_net_link_level_address_t ll_target_addr;
grub_err_t err;
grub_netbuff_push (nb, sizeof(*iph));
iph = (struct iphdr *) nb->data;
grub_netbuff_push (nb, sizeof (*iph));
iph = (struct iphdr *) nb->data;
iph->verhdrlen = ((4 << 4) | 5);
iph->service = 0;
iph->len = grub_cpu_to_be16 (nb->tail - nb-> data);
iph->len = grub_cpu_to_be16 (nb->tail - nb->data);
iph->ident = grub_cpu_to_be16 (++id);
iph->frags = 0;
iph->ttl = 0xff;
iph->protocol = 0x11;
iph->src = inf->address.ipv4;
iph->dest = target->ipv4;
iph->chksum = 0 ;
iph->chksum = ipchksum((void *)nb->data, sizeof(*iph));
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);
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)
@ -80,7 +81,7 @@ ip_filter (struct grub_net_buff *nb)
return 1;
}
*/
grub_err_t
grub_err_t
grub_net_recv_ip_packets (struct grub_net_buff *nb)
{
struct iphdr *iph = (struct iphdr *) nb->data;
@ -88,11 +89,11 @@ grub_net_recv_ip_packets (struct grub_net_buff *nb)
switch (iph->protocol)
{
case IP_UDP:
return grub_net_recv_udp_packet (nb);
case IP_UDP:
return grub_net_recv_udp_packet (nb);
break;
default:
grub_netbuff_free (nb);
default:
grub_netbuff_free (nb);
break;
}

View file

@ -22,73 +22,84 @@
#include <grub/net/netbuff.h>
grub_err_t grub_netbuff_put (struct grub_net_buff *nb ,grub_size_t len)
grub_err_t
grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
{
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;
return GRUB_ERR_NONE;
}
grub_err_t grub_netbuff_unput (struct grub_net_buff *nb ,grub_size_t len)
grub_err_t
grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
{
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;
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 *nb ,grub_size_t len)
grub_err_t
grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
{
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;
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 *nb ,grub_size_t len)
grub_err_t
grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
{
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;
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 *nb ,grub_size_t len)
grub_err_t
grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
{
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;
return grub_error (GRUB_ERR_OUT_OF_RANGE,
"reserve out of the packet range.");
return GRUB_ERR_NONE;
}
struct grub_net_buff *grub_netbuff_alloc ( grub_size_t len )
struct grub_net_buff *
grub_netbuff_alloc (grub_size_t len)
{
struct grub_net_buff *nb;
void *data;
if (len < NETBUFFMINLEN)
if (len < NETBUFFMINLEN)
len = NETBUFFMINLEN;
len = ALIGN_UP (len,NETBUFF_ALIGN);
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 = (struct grub_net_buff *) ((grub_uint8_t *) data + len);
nb->head = nb->data = nb->tail = data;
nb->end = (char *) nb;
return nb;
nb->end = (char *) nb;
return nb;
}
grub_err_t grub_netbuff_free (struct grub_net_buff *nb)
grub_err_t
grub_netbuff_free (struct grub_net_buff *nb)
{
grub_free (nb->head);
return 0;
}
grub_err_t grub_netbuff_clear (struct grub_net_buff *nb)
grub_err_t
grub_netbuff_clear (struct grub_net_buff *nb)
{
nb->data = nb->tail = nb->head;
return 0;

View file

@ -11,15 +11,15 @@
GRUB_MOD_LICENSE ("GPLv3+");
static grub_err_t
static grub_err_t
tftp_open (struct grub_file *file, const char *filename)
{
struct tftphdr *tftph;
struct tftphdr *tftph;
char *rrq;
int i;
int rrqlen;
int hdrlen;
char open_data[1500];
char open_data[1500];
struct grub_net_buff nb;
tftp_data_t data;
grub_err_t err;
@ -27,47 +27,47 @@ tftp_open (struct grub_file *file, const char *filename)
data = grub_malloc (sizeof *data);
if (!data)
return grub_errno;
file->device->net->socket->data = (void *) data;
nb.head = open_data;
nb.end = open_data + sizeof (open_data);
grub_netbuff_clear (&nb);
grub_netbuff_clear (&nb);
grub_netbuff_reserve (&nb,1500);
grub_netbuff_push (&nb,sizeof (*tftph));
grub_netbuff_reserve (&nb, 1500);
grub_netbuff_push (&nb, sizeof (*tftph));
tftph = (struct tftphdr *) nb.data;
tftph = (struct tftphdr *) nb.data;
rrq = (char *) tftph->u.rrq;
rrqlen = 0;
tftph->opcode = grub_cpu_to_be16 (TFTP_RRQ);
grub_strcpy (rrq, filename);
rrqlen += grub_strlen (filename) + 1;
rrq += grub_strlen (filename) + 1;
grub_strcpy (rrq,"octet");
rrq += grub_strlen (filename) + 1;
grub_strcpy (rrq, "octet");
rrqlen += grub_strlen ("octet") + 1;
rrq += grub_strlen ("octet") + 1;
rrq += grub_strlen ("octet") + 1;
grub_strcpy (rrq,"blksize");
rrqlen += grub_strlen("blksize") + 1;
rrq += grub_strlen ("blksize") + 1;
grub_strcpy (rrq, "blksize");
rrqlen += grub_strlen ("blksize") + 1;
rrq += grub_strlen ("blksize") + 1;
grub_strcpy (rrq,"1024");
grub_strcpy (rrq, "1024");
rrqlen += grub_strlen ("1024") + 1;
rrq += grub_strlen ("1024") + 1;
grub_strcpy (rrq,"tsize");
rrqlen += grub_strlen ("tsize") + 1;
rrq += grub_strlen ("tsize") + 1;
rrq += grub_strlen ("1024") + 1;
grub_strcpy (rrq,"0");
grub_strcpy (rrq, "tsize");
rrqlen += grub_strlen ("tsize") + 1;
rrq += grub_strlen ("tsize") + 1;
grub_strcpy (rrq, "0");
rrqlen += grub_strlen ("0") + 1;
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));
file->device->net->socket->out_port = TFTP_SERVER_PORT;
@ -76,7 +76,7 @@ tftp_open (struct grub_file *file, const char *filename)
return err;
/* Receive OACK packet. */
for ( i = 0; i < 3; i++)
for (i = 0; i < 3; i++)
{
grub_net_poll_cards (100);
if (grub_errno)
@ -85,22 +85,22 @@ tftp_open (struct grub_file *file, const char *filename)
break;
/* Retry. */
/*err = grub_net_send_udp_packet (file->device->net->socket, &nb);
if (err)
return err;*/
if (err)
return err; */
}
if (file->device->net->socket->status == 0)
return grub_error (GRUB_ERR_TIMEOUT,"Time out opening tftp.");
return grub_error (GRUB_ERR_TIMEOUT, "Time out opening tftp.");
file->size = data->file_size;
return GRUB_ERR_NONE;
}
static grub_err_t
static grub_err_t
tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
{
struct tftphdr *tftph;
char nbdata[128];
char nbdata[128];
tftp_data_t data = sock->data;
grub_err_t err;
char *ptr;
@ -112,9 +112,9 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
tftph = (struct tftphdr *) nb->data;
switch (grub_be_to_cpu16 (tftph->opcode))
{
{
case TFTP_OACK:
for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail; )
for (ptr = nb->data + sizeof (tftph->opcode); ptr < nb->tail;)
{
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
{
@ -125,13 +125,14 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
ptr++;
ptr++;
}
sock->status = 1;
sock->status = 1;
data->block = 0;
grub_netbuff_clear(nb);
break;
grub_netbuff_clear (nb);
break;
case TFTP_DATA:
grub_netbuff_pull (nb,sizeof (tftph->opcode) + sizeof (tftph->u.data.block));
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++;
@ -140,22 +141,23 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
sock->status = 2;
/* Prevent garbage in broken cards. */
if (size > 1024)
grub_netbuff_unput (nb, size - 1024);
grub_netbuff_unput (nb, size - 1024);
}
else
grub_netbuff_clear(nb);
break;
grub_netbuff_clear (nb);
break;
case TFTP_ERROR:
grub_netbuff_clear (nb);
return grub_error (GRUB_ERR_IO, (char *)tftph->u.err.errmsg);
break;
}
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));
grub_netbuff_reserve (&nb_ack, 128);
grub_netbuff_push (&nb_ack, sizeof (tftph->opcode)
+ sizeof (tftph->u.ack.block));
tftph = (struct tftphdr *) nb_ack.data;
tftph = (struct tftphdr *) nb_ack.data;
tftph->opcode = grub_cpu_to_be16 (TFTP_ACK);
tftph->u.ack.block = grub_cpu_to_be16 (data->block);
@ -163,7 +165,7 @@ tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
return err;
}
static grub_err_t
static grub_err_t
tftp_close (struct grub_file *file __attribute__ ((unused)))
{
grub_free (file->device->net->socket->data);
@ -171,19 +173,19 @@ tftp_close (struct grub_file *file __attribute__ ((unused)))
}
static struct grub_net_app_protocol grub_tftp_protocol =
{
.name = "tftp",
.open = tftp_open,
.read = tftp_receive,
.close = tftp_close
};
{
.name = "tftp",
.open = tftp_open,
.read = tftp_receive,
.close = tftp_close
};
GRUB_MOD_INIT (tftp)
GRUB_MOD_INIT(tftp)
{
grub_net_app_level_register (&grub_tftp_protocol);
}
GRUB_MOD_FINI (tftp)
GRUB_MOD_FINI(tftp)
{
grub_net_app_level_unregister (&grub_tftp_protocol);
}

View file

@ -5,24 +5,25 @@
#include <grub/time.h>
grub_err_t
grub_net_send_udp_packet (const grub_net_socket_t socket , struct grub_net_buff *nb)
grub_net_send_udp_packet (const grub_net_socket_t socket,
struct grub_net_buff *nb)
{
struct udphdr *udph;
grub_netbuff_push (nb,sizeof(*udph));
udph = (struct udphdr *) nb->data;
grub_netbuff_push (nb, sizeof (*udph));
udph = (struct udphdr *) nb->data;
udph->src = grub_cpu_to_be16 (socket->in_port);
udph->dst = grub_cpu_to_be16 (socket->out_port);
/* No chechksum. */
udph->chksum = 0;
udph->chksum = 0;
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
return grub_net_send_ip_packet (socket->inf, &(socket->out_nla), nb);
}
grub_err_t
grub_err_t
grub_net_recv_udp_packet (struct grub_net_buff *nb)
{
struct udphdr *udph;
@ -30,24 +31,24 @@ grub_net_recv_udp_packet (struct grub_net_buff *nb)
udph = (struct udphdr *) nb->data;
grub_netbuff_pull (nb, sizeof (*udph));
FOR_NET_SOCKETS(sock)
{
if (grub_be_to_cpu16 (udph->dst) == sock->in_port)
{
if (sock->status == 0)
sock->out_port = grub_be_to_cpu16 (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);
FOR_NET_SOCKETS (sock)
{
if (grub_be_to_cpu16 (udph->dst) == sock->in_port)
{
if (sock->status == 0)
sock->out_port = grub_be_to_cpu16 (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;
}