blob: e9809356c5305408e702a70e43bb8652d193c8b9 [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 Dike7d982302007-05-08 00:35:04 -070012#include "kern_constants.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070013#include "um_malloc.h"
14#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define PCAP_FD(p) (*(int *)(p))
17
Jeff Dikef34d9d22007-05-06 14:51:04 -070018static int pcap_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 struct pcap_data *pri = data;
21 pcap_t *p;
22 char errors[PCAP_ERRBUF_SIZE];
23
Jeff Dikeb53f35a2007-10-16 01:27:31 -070024 p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
25 pri->promisc, 0, errors);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070026 if (p == NULL) {
Jeff Dike7d982302007-05-08 00:35:04 -070027 printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
28 "'%s'\n", errors);
Jeff Dikef34d9d22007-05-06 14:51:04 -070029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 }
31
32 pri->dev = dev;
33 pri->pcap = p;
Jeff Dikef34d9d22007-05-06 14:51:04 -070034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37static int pcap_open(void *data)
38{
39 struct pcap_data *pri = data;
40 __u32 netmask;
41 int err;
42
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070043 if (pri->pcap == NULL)
Jeff Dike56bd1942007-05-06 14:51:02 -070044 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070046 if (pri->filter != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 err = dev_netmask(pri->dev, &netmask);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070048 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070049 printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070050 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070053 pri->compiled = kmalloc(sizeof(struct bpf_program),
54 UM_GFP_KERNEL);
55 if (pri->compiled == NULL) {
Jeff Dike7d982302007-05-08 00:35:04 -070056 printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070057 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Jeff Dike56bd1942007-05-06 14:51:02 -070059
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070060 err = pcap_compile(pri->pcap,
61 (struct bpf_program *) pri->compiled,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 pri->filter, pri->optimize, netmask);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070063 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070064 printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
65 "'%s'\n", pcap_geterr(pri->pcap));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
69 err = pcap_setfilter(pri->pcap, pri->compiled);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070070 if (err < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -070071 printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
72 "failed - '%s'\n", pcap_geterr(pri->pcap));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 }
Jeff Dike56bd1942007-05-06 14:51:02 -070076
77 return PCAP_FD(pri->pcap);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070078
79 out:
80 kfree(pri->compiled);
81 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static void pcap_remove(void *data)
85{
86 struct pcap_data *pri = data;
87
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070088 if (pri->compiled != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 pcap_freecode(pri->compiled);
90
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070091 if (pri->pcap != NULL)
Jeff Dike7d982302007-05-08 00:35:04 -070092 pcap_close(pri->pcap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95struct pcap_handler_data {
96 char *buffer;
97 int len;
98};
99
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700100static void handler(u_char *data, const struct pcap_pkthdr *header,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 const u_char *packet)
102{
103 int len;
104
105 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
106
107 len = hdata->len < header->caplen ? hdata->len : header->caplen;
108 memcpy(hdata->buffer, packet, len);
109 hdata->len = len;
110}
111
112int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
113{
114 struct pcap_handler_data hdata = ((struct pcap_handler_data)
115 { .buffer = buffer,
116 .len = len });
117 int n;
118
119 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700120 if (n < 0) {
Jeff Dike7d982302007-05-08 00:35:04 -0700121 printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
122 pcap_geterr(pri->pcap));
Jeff Dike56bd1942007-05-06 14:51:02 -0700123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700125 else if (n == 0)
Jeff Dike56bd1942007-05-06 14:51:02 -0700126 return 0;
127 return hdata.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jeff Dike5e7672e2006-09-27 01:50:33 -0700130const struct net_user_info pcap_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .init = pcap_user_init,
132 .open = pcap_open,
133 .close = NULL,
134 .remove = pcap_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .add_address = NULL,
136 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700137 .mtu = ETH_MAX_PACKET,
138 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};