[ARM] 4182/1: iop3xx: fix the ioremap implementation to not remap static ranges
[linux-2.6/kvm.git] / arch / um / drivers / pcap_user.c
blob11921a7baa7b14066d2c2f7d7f653063535e516e
1 /*
2 * Copyright (C) 2002 Jeff Dike <jdike@karaya.com>
3 * Licensed under the GPL.
4 */
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"
15 #include "um_malloc.h"
17 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
19 #define PCAP_FD(p) (*(int *)(p))
21 static void pcap_user_init(void *data, void *dev)
23 struct pcap_data *pri = data;
24 pcap_t *p;
25 char errors[PCAP_ERRBUF_SIZE];
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);
31 return;
34 pri->dev = dev;
35 pri->pcap = p;
38 static int pcap_open(void *data)
40 struct pcap_data *pri = data;
41 __u32 netmask;
42 int err;
44 if(pri->pcap == NULL)
45 return(-ENODEV);
47 if(pri->filter != NULL){
48 err = dev_netmask(pri->dev, &netmask);
49 if(err < 0){
50 printk("pcap_open : dev_netmask failed\n");
51 return(-EIO);
54 pri->compiled = um_kmalloc(sizeof(struct bpf_program));
55 if(pri->compiled == NULL){
56 printk("pcap_open : kmalloc failed\n");
57 return(-ENOMEM);
60 err = pcap_compile(pri->pcap,
61 (struct bpf_program *) pri->compiled,
62 pri->filter, pri->optimize, netmask);
63 if(err < 0){
64 printk("pcap_open : pcap_compile failed - '%s'\n",
65 pcap_geterr(pri->pcap));
66 return(-EIO);
69 err = pcap_setfilter(pri->pcap, pri->compiled);
70 if(err < 0){
71 printk("pcap_open : pcap_setfilter failed - '%s'\n",
72 pcap_geterr(pri->pcap));
73 return(-EIO);
77 return(PCAP_FD(pri->pcap));
80 static void pcap_remove(void *data)
82 struct pcap_data *pri = data;
84 if(pri->compiled != NULL)
85 pcap_freecode(pri->compiled);
87 pcap_close(pri->pcap);
90 struct pcap_handler_data {
91 char *buffer;
92 int len;
95 static void handler(u_char *data, const struct pcap_pkthdr *header,
96 const u_char *packet)
98 int len;
100 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
102 len = hdata->len < header->caplen ? hdata->len : header->caplen;
103 memcpy(hdata->buffer, packet, len);
104 hdata->len = len;
107 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
109 struct pcap_handler_data hdata = ((struct pcap_handler_data)
110 { .buffer = buffer,
111 .len = len });
112 int n;
114 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
115 if(n < 0){
116 printk("pcap_dispatch failed - %s\n", pcap_geterr(pri->pcap));
117 return(-EIO);
119 else if(n == 0)
120 return(0);
121 return(hdata.len);
124 const struct net_user_info pcap_user_info = {
125 .init = pcap_user_init,
126 .open = pcap_open,
127 .close = NULL,
128 .remove = pcap_remove,
129 .set_mtu = NULL,
130 .add_address = NULL,
131 .delete_address = NULL,
132 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
136 * Overrides for Emacs so that we follow Linus's tabbing style.
137 * Emacs will notice this stuff at the end of the file and automatically
138 * adjust the settings for this buffer only. This must remain at the end
139 * of the file.
140 * ---------------------------------------------------------------------------
141 * Local variables:
142 * c-file-style: "linux"
143 * End: