blob: 702a75b190ee552876e2e0a7e715e536e3cd154c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <errno.h>
7#include <pcap.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07008#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/types.h>
10#include "net_user.h"
11#include "pcap_user.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070012#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define PCAP_FD(p) (*(int *)(p))
15
Jeff Dikef34d9d22007-05-06 14:51:04 -070016static int pcap_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct pcap_data *pri = data;
19 pcap_t *p;
20 char errors[PCAP_ERRBUF_SIZE];
21
Jeff Dikeb53f35a2007-10-16 01:27:31 -070022 p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
23 pri->promisc, 0, errors);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070024 if (p == NULL) {
Jeff Dike7d982302007-05-08 00:35:04 -070025 printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
26 "'%s'\n", errors);
Jeff Dikef34d9d22007-05-06 14:51:04 -070027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 }
29
30 pri->dev = dev;
31 pri->pcap = p;
Jeff Dikef34d9d22007-05-06 14:51:04 -070032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
35static int pcap_open(void *data)
36{
37 struct pcap_data *pri = data;
38 __u32 netmask;
39 int err;
40
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070041 if (pri->pcap == NULL)
Jeff Dike56bd1942007-05-06 14:51:02 -070042 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070044 if (pri->filter != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 err = dev_netmask(pri->dev, &netmask);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070046 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070047 printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070048 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50
Al Viro296cd662008-05-21 06:32:11 +010051 pri->compiled = uml_kmalloc(sizeof(struct bpf_program),
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070052 UM_GFP_KERNEL);
53 if (pri->compiled == NULL) {
Jeff Dike7d982302007-05-08 00:35:04 -070054 printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070055 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Jeff Dike56bd1942007-05-06 14:51:02 -070057
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070058 err = pcap_compile(pri->pcap,
59 (struct bpf_program *) pri->compiled,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 pri->filter, pri->optimize, netmask);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070061 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070062 printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
63 "'%s'\n", pcap_geterr(pri->pcap));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66
67 err = pcap_setfilter(pri->pcap, pri->compiled);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070068 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070069 printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
70 "failed - '%s'\n", pcap_geterr(pri->pcap));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 }
Jeff Dike56bd1942007-05-06 14:51:02 -070074
75 return PCAP_FD(pri->pcap);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070076
77 out:
78 kfree(pri->compiled);
79 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static void pcap_remove(void *data)
83{
84 struct pcap_data *pri = data;
85
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070086 if (pri->compiled != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 pcap_freecode(pri->compiled);
88
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070089 if (pri->pcap != NULL)
Jeff Dike7d982302007-05-08 00:35:04 -070090 pcap_close(pri->pcap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93struct pcap_handler_data {
94 char *buffer;
95 int len;
96};
97
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070098static void handler(u_char *data, const struct pcap_pkthdr *header,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 const u_char *packet)
100{
101 int len;
102
103 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
104
105 len = hdata->len < header->caplen ? hdata->len : header->caplen;
106 memcpy(hdata->buffer, packet, len);
107 hdata->len = len;
108}
109
110int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
111{
112 struct pcap_handler_data hdata = ((struct pcap_handler_data)
113 { .buffer = buffer,
114 .len = len });
115 int n;
116
117 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700118 if (n < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -0700119 printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
120 pcap_geterr(pri->pcap));
Jeff Dike56bd1942007-05-06 14:51:02 -0700121 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700123 else if (n == 0)
Jeff Dike56bd1942007-05-06 14:51:02 -0700124 return 0;
125 return hdata.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Dike5e7672e2006-09-27 01:50:33 -0700128const struct net_user_info pcap_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .init = pcap_user_init,
130 .open = pcap_open,
131 .close = NULL,
132 .remove = pcap_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .add_address = NULL,
134 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700135 .mtu = ETH_MAX_PACKET,
136 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};