Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6] / arch / um / drivers / pcap_user.c
1 /*
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"
15 #include "um_malloc.h"
16 #include "kern_constants.h"
17
18 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
19
20 #define PCAP_FD(p) (*(int *)(p))
21
22 static int pcap_user_init(void *data, void *dev)
23 {
24         struct pcap_data *pri = data;
25         pcap_t *p;
26         char errors[PCAP_ERRBUF_SIZE];
27
28         p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
29         if(p == NULL){
30                 printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
31                        "'%s'\n", errors);
32                 return -EINVAL;
33         }
34
35         pri->dev = dev;
36         pri->pcap = p;
37         return 0;
38 }
39
40 static int pcap_open(void *data)
41 {
42         struct pcap_data *pri = data;
43         __u32 netmask;
44         int err;
45
46         if(pri->pcap == NULL)
47                 return -ENODEV;
48
49         if(pri->filter != NULL){
50                 err = dev_netmask(pri->dev, &netmask);
51                 if(err < 0){
52                         printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
53                         return -EIO;
54                 }
55
56                 pri->compiled = kmalloc(sizeof(struct bpf_program), UM_GFP_KERNEL);
57                 if(pri->compiled == NULL){
58                         printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
59                         return -ENOMEM;
60                 }
61
62                 err = pcap_compile(pri->pcap, 
63                                    (struct bpf_program *) pri->compiled, 
64                                    pri->filter, pri->optimize, netmask);
65                 if(err < 0){
66                         printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
67                                "'%s'\n", pcap_geterr(pri->pcap));
68                         return -EIO;
69                 }
70
71                 err = pcap_setfilter(pri->pcap, pri->compiled);
72                 if(err < 0){
73                         printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
74                                "failed - '%s'\n", pcap_geterr(pri->pcap));
75                         return -EIO;
76                 }
77         }
78
79         return PCAP_FD(pri->pcap);
80 }
81
82 static void pcap_remove(void *data)
83 {
84         struct pcap_data *pri = data;
85
86         if(pri->compiled != NULL)
87                 pcap_freecode(pri->compiled);
88
89         if(pri->pcap != NULL)
90                 pcap_close(pri->pcap);
91 }
92
93 struct pcap_handler_data {
94         char *buffer;
95         int len;
96 };
97
98 static void handler(u_char *data, const struct pcap_pkthdr *header, 
99                     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
110 int 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);
118         if(n < 0){
119                 printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
120                        pcap_geterr(pri->pcap));
121                 return -EIO;
122         }
123         else if(n == 0) 
124                 return 0;
125         return hdata.len;
126 }
127
128 const struct net_user_info pcap_user_info = {
129         .init           = pcap_user_init,
130         .open           = pcap_open,
131         .close          = NULL,
132         .remove         = pcap_remove,
133         .set_mtu        = NULL,
134         .add_address    = NULL,
135         .delete_address = NULL,
136         .max_packet     = MAX_PACKET - ETH_HEADER_OTHER
137 };