Merge grub_net into net. Compiles but is broken right now.
This commit is contained in:
commit
7bb47706c9
30 changed files with 1544 additions and 24 deletions
158
grub-core/net/arp.c
Normal file
158
grub-core/net/arp.c
Normal file
|
@ -0,0 +1,158 @@
|
|||
#include <grub/net/arp.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/net/interface.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net/ip.h>
|
||||
#include <grub/time.h>
|
||||
|
||||
static struct arp_entry arp_table[SIZE_ARP_TABLE];
|
||||
static grub_int8_t new_table_entry = -1;
|
||||
|
||||
static
|
||||
void arp_init_table(void)
|
||||
{
|
||||
grub_memset (arp_table, 0, sizeof (arp_table));
|
||||
new_table_entry = 0;
|
||||
}
|
||||
|
||||
static struct arp_entry *
|
||||
arp_find_entry (const grub_net_network_level_address_t *proto)
|
||||
{
|
||||
grub_uint8_t i;
|
||||
for(i=0;i < SIZE_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)
|
||||
{
|
||||
struct arp_entry *entry;
|
||||
struct grub_net_buff *nb;
|
||||
struct arphdr *arp_header;
|
||||
grub_net_link_level_address_t target_hw_addr;
|
||||
grub_uint8_t *aux, i;
|
||||
|
||||
/* 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_malloc (2048);
|
||||
grub_netbuff_reserve(nb, 2048);
|
||||
grub_netbuff_push(nb, sizeof(*arp_header) + 2 * (6 + 6));
|
||||
arp_header = (struct arphdr *)nb->data;
|
||||
arp_header->hrd = 0;
|
||||
arp_header->pro = 0;
|
||||
arp_header->hln = 6;
|
||||
arp_header->pln = 6;
|
||||
arp_header->op = ARP_REQUEST;
|
||||
aux = (grub_uint8_t *)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);
|
||||
aux += 6;
|
||||
/* Target hardware address */
|
||||
for(i=0; i < 6; i++)
|
||||
aux[i] = 0x00;
|
||||
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, ARP_ETHERTYPE);
|
||||
grub_netbuff_clear(nb);
|
||||
grub_netbuff_reserve(nb, 2048);
|
||||
|
||||
grub_uint64_t start_time, current_time;
|
||||
start_time = grub_get_time_ms();
|
||||
do
|
||||
{
|
||||
grub_net_recv_ethernet_packet (inf, nb, ARP_ETHERTYPE);
|
||||
/* Now check cache table again */
|
||||
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 > TIMEOUT_TIME_MS)
|
||||
break;
|
||||
} while (! entry);
|
||||
grub_netbuff_clear(nb);
|
||||
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)
|
||||
{
|
||||
struct arphdr *arp_header = (struct arphdr *)nb->data;
|
||||
struct arp_entry *entry;
|
||||
grub_uint8_t merge = 0;
|
||||
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;
|
||||
|
||||
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 */
|
||||
if (entry)
|
||||
{
|
||||
grub_memcpy(entry->ll_address.mac, sender_hardware_address, 6);
|
||||
merge = 1;
|
||||
}
|
||||
/* Am I the protocol address target? */
|
||||
if (! grub_memcmp(target_protocol_address, inf->hwaddress.mac, 6))
|
||||
{
|
||||
/* Add sender to cache table */
|
||||
if (! merge)
|
||||
{
|
||||
if (new_table_entry == -1)
|
||||
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);
|
||||
new_table_entry++;
|
||||
if (new_table_entry == SIZE_ARP_TABLE)
|
||||
new_table_entry = 0;
|
||||
}
|
||||
if (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 = ARP_REPLY;
|
||||
grub_memcpy (aux.mac, target_hardware_address, 6);
|
||||
send_ethernet_packet (inf, nb, aux, ARP_ETHERTYPE);
|
||||
}
|
||||
}
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
0
grub-core/net/device.c
Normal file
0
grub-core/net/device.c
Normal file
175
grub-core/net/drivers/ieee1275/ofnet.c
Normal file
175
grub-core/net/drivers/ieee1275/ofnet.c
Normal file
|
@ -0,0 +1,175 @@
|
|||
#include <grub/net/netbuff.h>
|
||||
#include <grub/ieee1275/ofnet.h>
|
||||
#include <grub/ieee1275/ieee1275.h>
|
||||
#include <grub/net.h>
|
||||
|
||||
static
|
||||
grub_err_t card_open (struct grub_net_card *dev)
|
||||
{
|
||||
|
||||
struct grub_ofnetcard_data *data = dev->data;
|
||||
return grub_ieee1275_open (data->path,&(data->handle));
|
||||
}
|
||||
|
||||
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 send_card_buffer (struct grub_net_card *dev, struct grub_net_buff *pack)
|
||||
{
|
||||
|
||||
int actual;
|
||||
struct grub_ofnetcard_data *data = dev->data;
|
||||
|
||||
return grub_ieee1275_write (data->handle,pack->data,pack->tail - pack->data,&actual);
|
||||
}
|
||||
|
||||
static
|
||||
grub_err_t get_card_packet (struct grub_net_card *dev, struct grub_net_buff *pack)
|
||||
{
|
||||
|
||||
int actual, rc;
|
||||
struct grub_ofnetcard_data *data = dev->data;
|
||||
grub_netbuff_clear(pack);
|
||||
|
||||
do
|
||||
{
|
||||
rc = grub_ieee1275_read (data->handle,pack->data,1500,&actual);
|
||||
|
||||
}while (actual <= 0 || rc < 0);
|
||||
grub_netbuff_put (pack, actual);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
char *name;
|
||||
int offset;
|
||||
}
|
||||
|
||||
bootp_response_properties[] =
|
||||
{
|
||||
{ .name = "bootp-response", .offset = 0 },
|
||||
{ .name = "dhcp-response", .offset = 0 },
|
||||
{ .name = "bootpreply-packet", .offset = 0x2a },
|
||||
};
|
||||
|
||||
|
||||
grub_bootp_t
|
||||
grub_getbootp( void )
|
||||
{
|
||||
grub_bootp_t packet = grub_malloc(sizeof *packet);
|
||||
void *bootp_response = NULL;
|
||||
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,
|
||||
bootp_response_properties[i].name,
|
||||
&size) >= 0)
|
||||
break;
|
||||
|
||||
if (size < 0)
|
||||
{
|
||||
grub_printf("Error to get bootp\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
return packet;
|
||||
}
|
||||
|
||||
void grub_ofnet_findcards (void)
|
||||
{
|
||||
struct grub_net_card *card;
|
||||
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);
|
||||
card->data = ofdata;
|
||||
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*/
|
||||
grub_ieee1275_devices_iterate (search_net_devices);
|
||||
|
||||
}
|
||||
|
||||
void grub_ofnet_probecards (void)
|
||||
{
|
||||
struct grub_net_card *card;
|
||||
struct grub_net_card_driver *driver;
|
||||
|
||||
/*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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GRUB_MOD_INIT(ofnet)
|
||||
{
|
||||
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_MODE_FINI(ofnet)
|
||||
{
|
||||
grub_net_card_driver_unregister (&ofdriver);
|
||||
}
|
||||
|
||||
|
||||
|
70
grub-core/net/ethernet.c
Normal file
70
grub-core/net/ethernet.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <grub/misc.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net/ip.h>
|
||||
#include <grub/net/arp.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/net/interface.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/time.h>
|
||||
#include <grub/net/arp.h>
|
||||
|
||||
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;
|
||||
|
||||
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_net_recv_ethernet_packet (struct grub_net_network_level_interface *inf,
|
||||
struct grub_net_buff *nb,
|
||||
grub_uint16_t ethertype)
|
||||
{
|
||||
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 = eth->type;
|
||||
grub_netbuff_pull(nb,sizeof (*eth));
|
||||
|
||||
if (eth->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));
|
||||
snaph = (struct snaphdr *) nb->data;
|
||||
type = snaph->type;
|
||||
}
|
||||
}
|
||||
|
||||
/* ARP packet */
|
||||
if (type == ARP_ETHERTYPE)
|
||||
{
|
||||
grub_net_arp_receive(inf, nb);
|
||||
if (ethertype == ARP_ETHERTYPE)
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
/* IP packet */
|
||||
else if(type == IP_ETHERTYPE && ethertype == IP_ETHERTYPE)
|
||||
return GRUB_ERR_NONE;
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
|
@ -132,10 +132,8 @@ grub_pxefs_open (struct grub_file *file, const char *name)
|
|||
|| grub_strncmp (file->device->net->name,
|
||||
"pxe:", sizeof ("pxe:") - 1) == 0)
|
||||
{
|
||||
const char *ptr;
|
||||
|
||||
ptr = name + sizeof ("pxe,") - 1;
|
||||
err = grub_net_resolve_address (name + sizeof ("pxe,") - 1, &addr);
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof ("pxe,") - 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
@ -298,16 +296,14 @@ static struct grub_fs grub_pxefs_fs =
|
|||
|
||||
static grub_size_t
|
||||
grub_pxe_recv (struct grub_net_card *dev __attribute__ ((unused)),
|
||||
void *buf __attribute__ ((unused)),
|
||||
grub_size_t buflen __attribute__ ((unused)))
|
||||
struct grub_net_buff *buf __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_pxe_send (struct grub_net_card *dev __attribute__ ((unused)),
|
||||
void *buf __attribute__ ((unused)),
|
||||
grub_size_t buflen __attribute__ ((unused)))
|
||||
struct grub_net_buff *buf __attribute__ ((unused)))
|
||||
{
|
||||
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "not implemented");
|
||||
}
|
||||
|
|
37
grub-core/net/interface.c
Normal file
37
grub-core/net/interface.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*#include <grub/net/interface.h>
|
||||
|
||||
#define INTERFACE_REGISTER_FUNCTIONS(layerprevious,layernext) \
|
||||
struct grub_net_##layername_layer_protocol *grub_net_##layername_layer_protocols;\
|
||||
\
|
||||
void grub_net_##layerprevious_##layernext_interface_register (struct grub_net_##layername_layer_protocol *prot)\
|
||||
{\
|
||||
grub_list_push (GRUB_AS_LIST_P (&grub_net_##layername_layer_protocols),\
|
||||
GRUB_AS_LIST (prot));\
|
||||
}\
|
||||
\
|
||||
void grub_net_##layerprevious_##layernext_interface_unregister (struct grub_net_##layername_layer_protocol *prot);\
|
||||
{\
|
||||
grub_list_remove (GRUB_AS_LIST_P (&grub_net_##layername_layer_protocols),\
|
||||
GRUB_AS_LIST (prot));\
|
||||
}\
|
||||
|
||||
INTERFACE_REGISTER_FUNCTIONS("application","transport");
|
||||
INTERFACE_REGISTER_FUNCTIONS("transport","network");
|
||||
INTERFACE_REGISTER_FUNCTIONS("network","link");
|
||||
INTERFACE_REGISTER_FUNCTIONS("link");*/
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/net/interface.h>
|
||||
struct grub_net_protocol_stack
|
||||
*grub_net_protocol_stack_get (char *name)
|
||||
{
|
||||
struct grub_net_protocol_stack *p;
|
||||
|
||||
for (p = grub_net_protocol_stacks; p; p = p->next)
|
||||
{
|
||||
if (!grub_strcmp(p->name,name))
|
||||
return p;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
93
grub-core/net/ip.c
Normal file
93
grub-core/net/ip.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
#include <grub/net/ip.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/net/arp.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net/interface.h>
|
||||
#include <grub/net/type_net.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/mm.h>
|
||||
|
||||
grub_uint16_t
|
||||
ipchksum(void *ipv, int len)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
grub_net_link_level_address_t ll_target_addr;
|
||||
grub_err_t err;
|
||||
|
||||
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->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));
|
||||
|
||||
/* 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, IP_ETHERTYPE);
|
||||
}
|
||||
|
||||
static int
|
||||
ip_filter (struct grub_net_buff *nb,
|
||||
struct grub_net_network_level_interface *inf)
|
||||
{
|
||||
struct iphdr *iph = (struct iphdr *) nb->data;
|
||||
grub_err_t err;
|
||||
|
||||
if (nb->end - nb->data < (signed) sizeof (*iph))
|
||||
return 0;
|
||||
|
||||
if (iph->protocol != 0x11 ||
|
||||
iph->dest != inf->address.ipv4)
|
||||
return 0;
|
||||
|
||||
grub_netbuff_pull (nb, sizeof (iph));
|
||||
err = grub_net_put_packet (&inf->nl_pending, nb);
|
||||
if (err)
|
||||
{
|
||||
grub_print_error ();
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_net_recv_ip_packets (struct grub_net_network_level_interface *inf)
|
||||
{
|
||||
struct grub_net_buff nb;
|
||||
grub_net_recv_ethernet_packet (inf, &nb, IP_ETHERTYPE);
|
||||
ip_filter (&nb, inf);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
94
grub-core/net/netbuff.c
Normal file
94
grub-core/net/netbuff.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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.
|
||||
*
|
||||
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/err.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
|
||||
|
||||
grub_err_t grub_netbuff_put (struct grub_net_buff *net_buff ,grub_size_t len)
|
||||
{
|
||||
net_buff->tail += len;
|
||||
if (net_buff->tail > net_buff->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)
|
||||
{
|
||||
net_buff->tail -= len;
|
||||
if (net_buff->tail < net_buff->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)
|
||||
{
|
||||
net_buff->data -= len;
|
||||
if (net_buff->data < net_buff->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)
|
||||
{
|
||||
net_buff->data += len;
|
||||
if (net_buff->data > net_buff->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)
|
||||
{
|
||||
net_buff->data += len;
|
||||
net_buff->tail += len;
|
||||
if ((net_buff->tail > net_buff->end) || (net_buff->data > net_buff->end))
|
||||
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 *nb;
|
||||
void *data;
|
||||
|
||||
if (len < NETBUFFMINLEN)
|
||||
len = NETBUFFMINLEN;
|
||||
|
||||
len = ALIGN_UP (len,NETBUFF_ALIGN);
|
||||
data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
|
||||
nb = (struct grub_net_buff *) ((int)data + len);
|
||||
nb->head = nb->data = nb->tail = data;
|
||||
nb->end = (char *) nb;
|
||||
|
||||
return nb;
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_free (struct grub_net_buff *net_buff)
|
||||
{
|
||||
grub_free (net_buff->head);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
grub_err_t grub_netbuff_clear (struct grub_net_buff *net_buff)
|
||||
{
|
||||
net_buff->data = net_buff->tail = net_buff->head;
|
||||
return 0;
|
||||
}
|
215
grub-core/net/tftp.c
Normal file
215
grub-core/net/tftp.c
Normal file
|
@ -0,0 +1,215 @@
|
|||
#include <grub/misc.h>
|
||||
#include <grub/net/tftp.h>
|
||||
#include <grub/net/udp.h>
|
||||
#include <grub/net/ip.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/net/interface.h>
|
||||
#include <grub/net/type_net.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/file.h>
|
||||
|
||||
struct {
|
||||
int block_size;
|
||||
int size;
|
||||
} tftp_file;
|
||||
static int block;
|
||||
|
||||
|
||||
static char *get_tok_val(char **tok, char **val, char **str_opt,char *end);
|
||||
static void process_option(char *tok, char *val);
|
||||
|
||||
static char *
|
||||
get_tok_val(char **tok, char **val,char **str_opt,char *end)
|
||||
{
|
||||
char *p = *str_opt;
|
||||
*tok = p;
|
||||
p += grub_strlen(p) + 1;
|
||||
|
||||
if(p > end)
|
||||
return NULL;
|
||||
|
||||
*val = p;
|
||||
p += grub_strlen(p) + 1;
|
||||
*str_opt = p;
|
||||
return *tok;
|
||||
}
|
||||
|
||||
static void
|
||||
process_option(char *tok, char *val)
|
||||
{
|
||||
if (!grub_strcmp(tok,"blksize"))
|
||||
{
|
||||
tftp_file.block_size = grub_strtoul (val,NULL,0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!grub_strcmp(tok,"tsize"))
|
||||
{
|
||||
tftp_file.size = grub_strtoul (val,NULL,0);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//void tftp_open (char *options);
|
||||
|
||||
/*send read request*/
|
||||
static grub_err_t
|
||||
tftp_open (struct grub_file *file, const char *filename)
|
||||
{
|
||||
struct tftphdr *tftph;
|
||||
char *rrq;
|
||||
int rrqlen;
|
||||
int hdrlen;
|
||||
struct grub_net_buff nb;
|
||||
grub_net_network_level_address_t addr;
|
||||
grub_err_t err;
|
||||
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof ("tftp,") - 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
grub_memset (&nb, 0, sizeof (nb));
|
||||
grub_netbuff_push (&nb,sizeof (*tftph));
|
||||
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
|
||||
rrq = (char *) tftph->u.rrq;
|
||||
rrqlen = 0;
|
||||
|
||||
tftph->opcode = TFTP_RRQ;
|
||||
grub_strcpy (rrq, filename);
|
||||
rrqlen += grub_strlen (filename) + 1;
|
||||
rrq += grub_strlen (filename) + 1;
|
||||
/*passar opcoes como parametro ou usar default?*/
|
||||
|
||||
grub_strcpy (rrq,"octet");
|
||||
rrqlen += grub_strlen ("octet") + 1;
|
||||
rrq += grub_strlen ("octet") + 1;
|
||||
|
||||
//grub_strcpy (rrq,"netascii");
|
||||
//rrqlen += grub_strlen ("netascii") + 1;
|
||||
//rrq += grub_strlen ("netascii") + 1;
|
||||
|
||||
grub_strcpy (rrq,"blksize");
|
||||
rrqlen += grub_strlen("blksize") + 1;
|
||||
rrq += grub_strlen ("blksize") + 1;
|
||||
|
||||
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;
|
||||
|
||||
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_net_send_udp_packet (&addr,
|
||||
&nb, TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
|
||||
grub_net_send_udp_packet (&addr, &nb, TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
/*Receive OACK*/
|
||||
grub_netbuff_clear (&nb);
|
||||
grub_netbuff_reserve (&nb,2048);
|
||||
file->size = tftp_file.size;
|
||||
|
||||
return grub_net_recv_udp_packet (&addr, &nb,
|
||||
TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
}
|
||||
|
||||
static grub_ssize_t
|
||||
tftp_receive (struct grub_file *file, char *buf, grub_size_t len)
|
||||
{
|
||||
struct tftphdr *tftph;
|
||||
char *token,*value,*temp;
|
||||
grub_err_t err;
|
||||
grub_net_network_level_address_t addr;
|
||||
struct grub_net_buff nb;
|
||||
|
||||
err = grub_net_resolve_address (file->device->net->name
|
||||
+ sizeof ("tftp,") - 1, &addr);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
grub_net_recv_udp_packet (&addr, &nb,
|
||||
TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
switch (tftph->opcode)
|
||||
{
|
||||
case TFTP_OACK:
|
||||
/*process oack packet*/
|
||||
temp = (char *) tftph->u.oack.data;
|
||||
while(get_tok_val(&token,&value,&temp,nb.tail))
|
||||
{
|
||||
process_option(token,value);
|
||||
}
|
||||
|
||||
//buff_clean
|
||||
grub_netbuff_clear(&nb);
|
||||
// grub_printf("OACK---------------------------------------------------------\n");
|
||||
//grub_printf("block_size=%d\n",tftp_file.block_size);
|
||||
// grub_printf("file_size=%d\n",tftp_file.size);
|
||||
// grub_printf("OACK---------------------------------------------------------\n");
|
||||
block = 0;
|
||||
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;
|
||||
case TFTP_ERROR:
|
||||
grub_netbuff_clear (&nb);
|
||||
return grub_error (GRUB_ERR_ACCESS_DENIED, (char *)tftph->u.err.errmsg);
|
||||
break;
|
||||
}
|
||||
|
||||
nb.data = nb.tail = nb.end;
|
||||
|
||||
grub_netbuff_push (&nb,sizeof (tftph->opcode) + sizeof (tftph->u.ack.block));
|
||||
|
||||
tftph = (struct tftphdr *) nb.data;
|
||||
tftph->opcode = TFTP_ACK;
|
||||
tftph->u.ack.block = block;
|
||||
|
||||
return grub_net_send_udp_packet (&addr, &nb, TFTP_CLIENT_PORT, TFTP_SERVER_PORT);
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
tftp_close (struct grub_file *file __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct grub_fs grub_tftp_protocol =
|
||||
{
|
||||
.name = "tftp",
|
||||
.open = tftp_open,
|
||||
.read = tftp_receive,
|
||||
.close = tftp_close
|
||||
};
|
||||
|
||||
GRUB_MOD_INIT (tftp)
|
||||
{
|
||||
grub_net_app_level_register (&grub_tftp_protocol);
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI (tftp)
|
||||
{
|
||||
grub_net_app_level_unregister (&grub_tftp_protocol);
|
||||
}
|
70
grub-core/net/udp.c
Normal file
70
grub-core/net/udp.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <grub/net.h>
|
||||
#include <grub/net/udp.h>
|
||||
#include <grub/net/ip.h>
|
||||
#include <grub/net/type_net.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/net/protocol.h>
|
||||
#include <grub/net/interface.h>
|
||||
#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)
|
||||
{
|
||||
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);
|
||||
|
||||
/* No chechksum. */
|
||||
udph->chksum = 0;
|
||||
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
|
||||
|
||||
return grub_net_send_ip_packet (inf, target, 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_err_t err;
|
||||
struct grub_net_packet *pkt;
|
||||
struct grub_net_network_level_interface *inf;
|
||||
grub_net_network_level_address_t gateway;
|
||||
|
||||
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)
|
||||
{
|
||||
struct udphdr *udph;
|
||||
struct grub_net_buff *nb = pkt->nb;
|
||||
udph = (struct udphdr *) nb->data;
|
||||
if (grub_be_to_cpu16 (udph->dst) == destport)
|
||||
{
|
||||
grub_net_remove_packet (pkt);
|
||||
grub_netbuff_pull (nb, sizeof(*udph));
|
||||
grub_memcpy (buf, nb, sizeof (buf));
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
}
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue