grub/grub-core/net/drivers/emu/emunet.c

123 lines
2.8 KiB
C
Raw Normal View History

2011-07-08 16:49:35 +00:00
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2011 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/>.
*/
2010-09-22 22:45:39 +00:00
#include <grub/dl.h>
2010-09-22 15:14:43 +00:00
#include <grub/net/netbuff.h>
#include <sys/socket.h>
#include <grub/net.h>
2010-09-22 22:45:39 +00:00
#include <sys/types.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <grub/term.h>
#include <grub/i18n.h>
2010-09-22 15:14:43 +00:00
2011-10-05 21:50:43 +00:00
GRUB_MOD_LICENSE ("GPLv3+");
2010-09-22 22:45:39 +00:00
static int fd;
2010-09-22 15:14:43 +00:00
2011-06-24 18:35:25 +00:00
static grub_err_t
send_card_buffer (struct grub_net_card *dev __attribute__ ((unused)),
2010-09-22 22:45:39 +00:00
struct grub_net_buff *pack)
2010-09-22 15:14:43 +00:00
{
ssize_t actual;
2010-09-22 22:45:39 +00:00
actual = write (fd, pack->data, pack->tail - pack->data);
2010-09-22 15:14:43 +00:00
if (actual < 0)
return grub_error (GRUB_ERR_IO, N_("couldn't send network packet"));
2011-06-08 00:59:53 +00:00
2010-09-22 15:14:43 +00:00
return GRUB_ERR_NONE;
}
static struct grub_net_buff *
get_card_packet (struct grub_net_card *dev __attribute__ ((unused)))
2010-09-22 15:14:43 +00:00
{
ssize_t actual;
struct grub_net_buff *nb;
2010-09-22 15:14:43 +00:00
nb = grub_netbuff_alloc (1536);
if (!nb)
return NULL;
/* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
by 4. So that IP header is aligned on 4 bytes. */
grub_netbuff_reserve (nb, 2);
if (!nb)
{
grub_netbuff_free (nb);
return NULL;
}
actual = read (fd, nb->data, 1536);
2010-09-22 15:14:43 +00:00
if (actual < 0)
{
grub_netbuff_free (nb);
return NULL;
}
grub_netbuff_put (nb, actual);
2010-09-22 15:14:43 +00:00
return nb;
2010-09-22 15:14:43 +00:00
}
static struct grub_net_card_driver emudriver =
2011-06-08 00:59:53 +00:00
{
.name = "emu",
.send = send_card_buffer,
.recv = get_card_packet
};
2010-09-22 15:14:43 +00:00
2010-09-22 22:45:39 +00:00
static struct grub_net_card emucard =
2011-06-08 00:59:53 +00:00
{
.name = "emu0",
.driver = &emudriver,
2011-07-08 16:49:35 +00:00
.mtu = 1500,
2011-06-08 00:59:53 +00:00
.default_address = {
.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
{.mac = {0, 1, 2, 3, 4, 5}}
},
.flags = 0
};
2010-09-22 22:45:39 +00:00
2010-09-22 15:14:43 +00:00
GRUB_MOD_INIT(emunet)
{
2010-09-22 22:45:39 +00:00
struct ifreq ifr;
fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK);
if (fd < 0)
return;
grub_memset (&ifr, 0, sizeof (ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl (fd, TUNSETIFF, &ifr) < 0)
{
close (fd);
fd = -1;
return;
}
grub_net_card_register (&emucard);
2010-09-22 15:14:43 +00:00
}
2010-09-22 22:45:39 +00:00
GRUB_MOD_FINI(emunet)
2010-09-22 15:14:43 +00:00
{
2010-09-22 22:45:39 +00:00
if (fd >= 0)
{
close (fd);
grub_net_card_unregister (&emucard);
}
2010-09-22 15:14:43 +00:00
}