blob: dc0a903ef9a6fa547032bc1d7bbdfb70c7c3013f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Jeff Dike <jdike@karaya.com>
3 * Licensed under the GPL.
4 */
5
6#include <unistd.h>
7#include <stdlib.h>
8#include <string.h>
9#include <errno.h>
10#include <pcap.h>
11#include <asm/types.h>
12#include "net_user.h"
13#include "pcap_user.h"
14#include "user.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070015#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
18
19#define PCAP_FD(p) (*(int *)(p))
20
Jeff Dikef34d9d22007-05-06 14:51:04 -070021static int pcap_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 struct pcap_data *pri = data;
24 pcap_t *p;
25 char errors[PCAP_ERRBUF_SIZE];
26
27 p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
28 if(p == NULL){
29 printk("pcap_user_init : pcap_open_live failed - '%s'\n",
30 errors);
Jeff Dikef34d9d22007-05-06 14:51:04 -070031 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 }
33
34 pri->dev = dev;
35 pri->pcap = p;
Jeff Dikef34d9d22007-05-06 14:51:04 -070036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39static int pcap_open(void *data)
40{
41 struct pcap_data *pri = data;
42 __u32 netmask;
43 int err;
44
45 if(pri->pcap == NULL)
Jeff Dike56bd1942007-05-06 14:51:02 -070046 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 if(pri->filter != NULL){
49 err = dev_netmask(pri->dev, &netmask);
50 if(err < 0){
51 printk("pcap_open : dev_netmask failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070052 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
54
55 pri->compiled = um_kmalloc(sizeof(struct bpf_program));
56 if(pri->compiled == NULL){
57 printk("pcap_open : kmalloc failed\n");
Jeff Dike56bd1942007-05-06 14:51:02 -070058 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Jeff Dike56bd1942007-05-06 14:51:02 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 err = pcap_compile(pri->pcap,
62 (struct bpf_program *) pri->compiled,
63 pri->filter, pri->optimize, netmask);
64 if(err < 0){
65 printk("pcap_open : pcap_compile failed - '%s'\n",
66 pcap_geterr(pri->pcap));
Jeff Dike56bd1942007-05-06 14:51:02 -070067 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69
70 err = pcap_setfilter(pri->pcap, pri->compiled);
71 if(err < 0){
72 printk("pcap_open : pcap_setfilter failed - '%s'\n",
73 pcap_geterr(pri->pcap));
Jeff Dike56bd1942007-05-06 14:51:02 -070074 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 }
Jeff Dike56bd1942007-05-06 14:51:02 -070077
78 return PCAP_FD(pri->pcap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void pcap_remove(void *data)
82{
83 struct pcap_data *pri = data;
84
85 if(pri->compiled != NULL)
86 pcap_freecode(pri->compiled);
87
88 pcap_close(pri->pcap);
89}
90
91struct pcap_handler_data {
92 char *buffer;
93 int len;
94};
95
96static void handler(u_char *data, const struct pcap_pkthdr *header,
97 const u_char *packet)
98{
99 int len;
100
101 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
102
103 len = hdata->len < header->caplen ? hdata->len : header->caplen;
104 memcpy(hdata->buffer, packet, len);
105 hdata->len = len;
106}
107
108int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
109{
110 struct pcap_handler_data hdata = ((struct pcap_handler_data)
111 { .buffer = buffer,
112 .len = len });
113 int n;
114
115 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
116 if(n < 0){
117 printk("pcap_dispatch failed - %s\n", pcap_geterr(pri->pcap));
Jeff Dike56bd1942007-05-06 14:51:02 -0700118 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 else if(n == 0)
Jeff Dike56bd1942007-05-06 14:51:02 -0700121 return 0;
122 return hdata.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jeff Dike5e7672e2006-09-27 01:50:33 -0700125const struct net_user_info pcap_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .init = pcap_user_init,
127 .open = pcap_open,
128 .close = NULL,
129 .remove = pcap_remove,
130 .set_mtu = NULL,
131 .add_address = NULL,
132 .delete_address = NULL,
133 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
134};