2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2007-10-16 16:27:29 +08:00
|
|
|
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-17 06:20:36 +08:00
|
|
|
* Licensed under the GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pcap.h>
|
2007-10-16 16:27:29 +08:00
|
|
|
#include <string.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/types.h>
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <net_user.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "pcap_user.h"
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <um_malloc.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#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-07 05:51:04 +08:00
|
|
|
static int pcap_user_init(void *data, void *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
pcap_t *p;
|
|
|
|
char errors[PCAP_ERRBUF_SIZE];
|
|
|
|
|
2007-10-16 16:27:31 +08:00
|
|
|
p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
|
|
|
|
pri->promisc, 0, errors);
|
2007-10-16 16:27:29 +08:00
|
|
|
if (p == NULL) {
|
2007-05-08 15:35:04 +08: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-07 05:51:04 +08:00
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08: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-07 05:51:04 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pcap_open(void *data)
|
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
__u32 netmask;
|
|
|
|
int err;
|
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
if (pri->pcap == NULL)
|
2007-05-07 05:51:02 +08:00
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
if (pri->filter != NULL) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = dev_netmask(pri->dev, &netmask);
|
2007-10-16 16:27:29 +08:00
|
|
|
if (err < 0) {
|
2007-05-08 15:35:04 +08:00
|
|
|
printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
|
2007-05-07 05:51:02 +08:00
|
|
|
return -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-05-21 13:32:11 +08:00
|
|
|
pri->compiled = uml_kmalloc(sizeof(struct bpf_program),
|
2007-10-16 16:27:29 +08:00
|
|
|
UM_GFP_KERNEL);
|
|
|
|
if (pri->compiled == NULL) {
|
2007-05-08 15:35:04 +08:00
|
|
|
printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
|
2007-05-07 05:51:02 +08:00
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-05-07 05:51:02 +08:00
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
err = pcap_compile(pri->pcap,
|
|
|
|
(struct bpf_program *) pri->compiled,
|
2005-04-17 06:20:36 +08:00
|
|
|
pri->filter, pri->optimize, netmask);
|
2007-10-16 16:27:29 +08:00
|
|
|
if (err < 0) {
|
2007-05-08 15:35:04 +08:00
|
|
|
printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
|
|
|
|
"'%s'\n", pcap_geterr(pri->pcap));
|
2007-10-16 16:27:29 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pcap_setfilter(pri->pcap, pri->compiled);
|
2007-10-16 16:27:29 +08:00
|
|
|
if (err < 0) {
|
2007-05-08 15:35:04 +08:00
|
|
|
printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
|
|
|
|
"failed - '%s'\n", pcap_geterr(pri->pcap));
|
2007-10-16 16:27:29 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2007-05-07 05:51:02 +08:00
|
|
|
|
|
|
|
return PCAP_FD(pri->pcap);
|
2007-10-16 16:27:29 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(pri->compiled);
|
|
|
|
return -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pcap_remove(void *data)
|
|
|
|
{
|
|
|
|
struct pcap_data *pri = data;
|
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
if (pri->compiled != NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
pcap_freecode(pri->compiled);
|
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
if (pri->pcap != NULL)
|
2007-05-08 15:35:04 +08:00
|
|
|
pcap_close(pri->pcap);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pcap_handler_data {
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
2007-10-16 16:27:29 +08:00
|
|
|
static void handler(u_char *data, const struct pcap_pkthdr *header,
|
2005-04-17 06:20:36 +08: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 16:27:29 +08:00
|
|
|
if (n < 0) {
|
2007-05-08 15:35:04 +08:00
|
|
|
printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
|
|
|
|
pcap_geterr(pri->pcap));
|
2007-05-07 05:51:02 +08:00
|
|
|
return -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-16 16:27:29 +08:00
|
|
|
else if (n == 0)
|
2007-05-07 05:51:02 +08:00
|
|
|
return 0;
|
|
|
|
return hdata.len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
const struct net_user_info pcap_user_info = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.init = pcap_user_init,
|
|
|
|
.open = pcap_open,
|
|
|
|
.close = NULL,
|
|
|
|
.remove = pcap_remove,
|
|
|
|
.add_address = NULL,
|
|
|
|
.delete_address = NULL,
|
2007-10-16 16:27:31 +08:00
|
|
|
.mtu = ETH_MAX_PACKET,
|
|
|
|
.max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|