2005-04-16 16:20:36 -06:00
|
|
|
/*
|
2007-10-16 02:27:29 -06:00
|
|
|
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 16:20:36 -06:00
|
|
|
* Licensed under the GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pcap.h>
|
2007-10-16 02:27:29 -06:00
|
|
|
#include <string.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <asm/types.h>
|
|
|
|
#include "net_user.h"
|
|
|
|
#include "pcap_user.h"
|
2007-05-08 01:35:04 -06:00
|
|
|
#include "kern_constants.h"
|
2007-10-16 02:27:29 -06:00
|
|
|
#include "um_malloc.h"
|
|
|
|
#include "user.h"
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
|
|
|
|
|
|
|
|
#define PCAP_FD(p) (*(int *)(p))
|
|
|
|
|
uml: network interface hotplug error handling
This fixes a number of problems associated with network interface hotplug.
The userspace initialization function can fail in some cases, but the
failure was never passed back to eth_configure, which proceeded with the
configuration. This results in a zombie device that is present, but can't
work. This is fixed by allowing the initialization routines to return an
error, which is checked, and the configuration aborted on failure.
eth_configure failed to check for many failures. Even when it did check,
it didn't undo whatever initializations has already happened, so a present,
but partially initialized and non-working device could result. It now
checks everything that can fail, and bails out, undoing whatever had been
done.
The return value of eth_configure was always ignored, so it is now just
void.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Cc: Jeff Garzik <jeff@garzik.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 15:51:04 -06:00
|
|
|
static int pcap_user_init(void *data, void *dev)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
pcap_t *p;
|
|
|
|
char errors[PCAP_ERRBUF_SIZE];
|
|
|
|
|
|
|
|
p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
|
2007-10-16 02:27:29 -06:00
|
|
|
if (p == NULL) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
|
|
|
|
"'%s'\n", errors);
|
uml: network interface hotplug error handling
This fixes a number of problems associated with network interface hotplug.
The userspace initialization function can fail in some cases, but the
failure was never passed back to eth_configure, which proceeded with the
configuration. This results in a zombie device that is present, but can't
work. This is fixed by allowing the initialization routines to return an
error, which is checked, and the configuration aborted on failure.
eth_configure failed to check for many failures. Even when it did check,
it didn't undo whatever initializations has already happened, so a present,
but partially initialized and non-working device could result. It now
checks everything that can fail, and bails out, undoing whatever had been
done.
The return value of eth_configure was always ignored, so it is now just
void.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Cc: Jeff Garzik <jeff@garzik.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 15:51:04 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pri->dev = dev;
|
|
|
|
pri->pcap = p;
|
uml: network interface hotplug error handling
This fixes a number of problems associated with network interface hotplug.
The userspace initialization function can fail in some cases, but the
failure was never passed back to eth_configure, which proceeded with the
configuration. This results in a zombie device that is present, but can't
work. This is fixed by allowing the initialization routines to return an
error, which is checked, and the configuration aborted on failure.
eth_configure failed to check for many failures. Even when it did check,
it didn't undo whatever initializations has already happened, so a present,
but partially initialized and non-working device could result. It now
checks everything that can fail, and bails out, undoing whatever had been
done.
The return value of eth_configure was always ignored, so it is now just
void.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Cc: Jeff Garzik <jeff@garzik.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 15:51:04 -06:00
|
|
|
return 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pcap_open(void *data)
|
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
__u32 netmask;
|
|
|
|
int err;
|
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
if (pri->pcap == NULL)
|
2007-05-06 15:51:02 -06:00
|
|
|
return -ENODEV;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
if (pri->filter != NULL) {
|
2005-04-16 16:20:36 -06:00
|
|
|
err = dev_netmask(pri->dev, &netmask);
|
2007-10-16 02:27:29 -06:00
|
|
|
if (err < 0) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
|
2007-05-06 15:51:02 -06:00
|
|
|
return -EIO;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
pri->compiled = kmalloc(sizeof(struct bpf_program),
|
|
|
|
UM_GFP_KERNEL);
|
|
|
|
if (pri->compiled == NULL) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
|
2007-05-06 15:51:02 -06:00
|
|
|
return -ENOMEM;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2007-05-06 15:51:02 -06:00
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
err = pcap_compile(pri->pcap,
|
|
|
|
(struct bpf_program *) pri->compiled,
|
2005-04-16 16:20:36 -06:00
|
|
|
pri->filter, pri->optimize, netmask);
|
2007-10-16 02:27:29 -06:00
|
|
|
if (err < 0) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
|
|
|
|
"'%s'\n", pcap_geterr(pri->pcap));
|
2007-10-16 02:27:29 -06:00
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pcap_setfilter(pri->pcap, pri->compiled);
|
2007-10-16 02:27:29 -06:00
|
|
|
if (err < 0) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
|
|
|
|
"failed - '%s'\n", pcap_geterr(pri->pcap));
|
2007-10-16 02:27:29 -06:00
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
2007-05-06 15:51:02 -06:00
|
|
|
|
|
|
|
return PCAP_FD(pri->pcap);
|
2007-10-16 02:27:29 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(pri->compiled);
|
|
|
|
return -EIO;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pcap_remove(void *data)
|
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
if (pri->compiled != NULL)
|
2005-04-16 16:20:36 -06:00
|
|
|
pcap_freecode(pri->compiled);
|
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
if (pri->pcap != NULL)
|
2007-05-08 01:35:04 -06:00
|
|
|
pcap_close(pri->pcap);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pcap_handler_data {
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
2007-10-16 02:27:29 -06:00
|
|
|
static void handler(u_char *data, const struct pcap_pkthdr *header,
|
2005-04-16 16:20:36 -06:00
|
|
|
const u_char *packet)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
|
|
|
|
|
|
|
|
len = hdata->len < header->caplen ? hdata->len : header->caplen;
|
|
|
|
memcpy(hdata->buffer, packet, len);
|
|
|
|
hdata->len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
|
|
|
|
{
|
|
|
|
struct pcap_handler_data hdata = ((struct pcap_handler_data)
|
|
|
|
{ .buffer = buffer,
|
|
|
|
.len = len });
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
|
2007-10-16 02:27:29 -06:00
|
|
|
if (n < 0) {
|
2007-05-08 01:35:04 -06:00
|
|
|
printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
|
|
|
|
pcap_geterr(pri->pcap));
|
2007-05-06 15:51:02 -06:00
|
|
|
return -EIO;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2007-10-16 02:27:29 -06:00
|
|
|
else if (n == 0)
|
2007-05-06 15:51:02 -06:00
|
|
|
return 0;
|
|
|
|
return hdata.len;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-09-27 02:50:33 -06:00
|
|
|
const struct net_user_info pcap_user_info = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.init = pcap_user_init,
|
|
|
|
.open = pcap_open,
|
|
|
|
.close = NULL,
|
|
|
|
.remove = pcap_remove,
|
|
|
|
.set_mtu = NULL,
|
|
|
|
.add_address = NULL,
|
|
|
|
.delete_address = NULL,
|
|
|
|
.max_packet = MAX_PACKET - ETH_HEADER_OTHER
|
|
|
|
};
|