Linux 2.6.17.7
[linux/fpc-iii.git] / arch / um / drivers / pcap_user.c
blobedfcb29273e1abf19f601d523a4b125cdc35289f
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"
16 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
18 #define PCAP_FD(p) (*(int *)(p))
20 static void pcap_user_init(void *data, void *dev)
22 struct pcap_data *pri = data;
23 pcap_t *p;
24 char errors[PCAP_ERRBUF_SIZE];
26 p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
27 if(p == NULL){
28 printk("pcap_user_init : pcap_open_live failed - '%s'\n",
29 errors);
30 return;
33 pri->dev = dev;
34 pri->pcap = p;
37 static int pcap_open(void *data)
39 struct pcap_data *pri = data;
40 __u32 netmask;
41 int err;
43 if(pri->pcap == NULL)
44 return(-ENODEV);
46 if(pri->filter != NULL){
47 err = dev_netmask(pri->dev, &netmask);
48 if(err < 0){
49 printk("pcap_open : dev_netmask failed\n");
50 return(-EIO);
53 pri->compiled = um_kmalloc(sizeof(struct bpf_program));
54 if(pri->compiled == NULL){
55 printk("pcap_open : kmalloc failed\n");
56 return(-ENOMEM);
59 err = pcap_compile(pri->pcap,
60 (struct bpf_program *) pri->compiled,
61 pri->filter, pri->optimize, netmask);
62 if(err < 0){
63 printk("pcap_open : pcap_compile failed - '%s'\n",
64 pcap_geterr(pri->pcap));
65 return(-EIO);
68 err = pcap_setfilter(pri->pcap, pri->compiled);
69 if(err < 0){
70 printk("pcap_open : pcap_setfilter failed - '%s'\n",
71 pcap_geterr(pri->pcap));
72 return(-EIO);
76 return(PCAP_FD(pri->pcap));
79 static void pcap_remove(void *data)
81 struct pcap_data *pri = data;
83 if(pri->compiled != NULL)
84 pcap_freecode(pri->compiled);
86 pcap_close(pri->pcap);
89 struct pcap_handler_data {
90 char *buffer;
91 int len;
94 static void handler(u_char *data, const struct pcap_pkthdr *header,
95 const u_char *packet)
97 int len;
99 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
101 len = hdata->len < header->caplen ? hdata->len : header->caplen;
102 memcpy(hdata->buffer, packet, len);
103 hdata->len = len;
106 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
108 struct pcap_handler_data hdata = ((struct pcap_handler_data)
109 { .buffer = buffer,
110 .len = len });
111 int n;
113 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
114 if(n < 0){
115 printk("pcap_dispatch failed - %s\n", pcap_geterr(pri->pcap));
116 return(-EIO);
118 else if(n == 0)
119 return(0);
120 return(hdata.len);
123 struct net_user_info pcap_user_info = {
124 .init = pcap_user_init,
125 .open = pcap_open,
126 .close = NULL,
127 .remove = pcap_remove,
128 .set_mtu = NULL,
129 .add_address = NULL,
130 .delete_address = NULL,
131 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
135 * Overrides for Emacs so that we follow Linus's tabbing style.
136 * Emacs will notice this stuff at the end of the file and automatically
137 * adjust the settings for this buffer only. This must remain at the end
138 * of the file.
139 * ---------------------------------------------------------------------------
140 * Local variables:
141 * c-file-style: "linux"
142 * End: