Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
cd1ae0e4 | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
3 | * Licensed under the GPL. |
4 | */ | |
5 | ||
1da177e4 LT |
6 | #include <errno.h> |
7 | #include <pcap.h> | |
cd1ae0e4 | 8 | #include <string.h> |
1da177e4 LT |
9 | #include <asm/types.h> |
10 | #include "net_user.h" | |
11 | #include "pcap_user.h" | |
7d98230a | 12 | #include "kern_constants.h" |
cd1ae0e4 JD |
13 | #include "um_malloc.h" |
14 | #include "user.h" | |
1da177e4 | 15 | |
1da177e4 LT |
16 | #define PCAP_FD(p) (*(int *)(p)) |
17 | ||
f34d9d2d | 18 | static int pcap_user_init(void *data, void *dev) |
1da177e4 LT |
19 | { |
20 | struct pcap_data *pri = data; | |
21 | pcap_t *p; | |
22 | char errors[PCAP_ERRBUF_SIZE]; | |
23 | ||
b53f35a8 JD |
24 | p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER, |
25 | pri->promisc, 0, errors); | |
cd1ae0e4 | 26 | if (p == NULL) { |
7d98230a JD |
27 | printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - " |
28 | "'%s'\n", errors); | |
f34d9d2d | 29 | return -EINVAL; |
1da177e4 LT |
30 | } |
31 | ||
32 | pri->dev = dev; | |
33 | pri->pcap = p; | |
f34d9d2d | 34 | return 0; |
1da177e4 LT |
35 | } |
36 | ||
37 | static int pcap_open(void *data) | |
38 | { | |
39 | struct pcap_data *pri = data; | |
40 | __u32 netmask; | |
41 | int err; | |
42 | ||
cd1ae0e4 | 43 | if (pri->pcap == NULL) |
56bd194b | 44 | return -ENODEV; |
1da177e4 | 45 | |
cd1ae0e4 | 46 | if (pri->filter != NULL) { |
1da177e4 | 47 | err = dev_netmask(pri->dev, &netmask); |
cd1ae0e4 | 48 | if (err < 0) { |
7d98230a | 49 | printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n"); |
56bd194b | 50 | return -EIO; |
1da177e4 LT |
51 | } |
52 | ||
296cd66f | 53 | pri->compiled = uml_kmalloc(sizeof(struct bpf_program), |
cd1ae0e4 JD |
54 | UM_GFP_KERNEL); |
55 | if (pri->compiled == NULL) { | |
7d98230a | 56 | printk(UM_KERN_ERR "pcap_open : kmalloc failed\n"); |
56bd194b | 57 | return -ENOMEM; |
1da177e4 | 58 | } |
56bd194b | 59 | |
cd1ae0e4 JD |
60 | err = pcap_compile(pri->pcap, |
61 | (struct bpf_program *) pri->compiled, | |
1da177e4 | 62 | pri->filter, pri->optimize, netmask); |
cd1ae0e4 | 63 | if (err < 0) { |
7d98230a JD |
64 | printk(UM_KERN_ERR "pcap_open : pcap_compile failed - " |
65 | "'%s'\n", pcap_geterr(pri->pcap)); | |
cd1ae0e4 | 66 | goto out; |
1da177e4 LT |
67 | } |
68 | ||
69 | err = pcap_setfilter(pri->pcap, pri->compiled); | |
cd1ae0e4 | 70 | if (err < 0) { |
7d98230a JD |
71 | printk(UM_KERN_ERR "pcap_open : pcap_setfilter " |
72 | "failed - '%s'\n", pcap_geterr(pri->pcap)); | |
cd1ae0e4 | 73 | goto out; |
1da177e4 LT |
74 | } |
75 | } | |
56bd194b JD |
76 | |
77 | return PCAP_FD(pri->pcap); | |
cd1ae0e4 JD |
78 | |
79 | out: | |
80 | kfree(pri->compiled); | |
81 | return -EIO; | |
1da177e4 LT |
82 | } |
83 | ||
84 | static void pcap_remove(void *data) | |
85 | { | |
86 | struct pcap_data *pri = data; | |
87 | ||
cd1ae0e4 | 88 | if (pri->compiled != NULL) |
1da177e4 LT |
89 | pcap_freecode(pri->compiled); |
90 | ||
cd1ae0e4 | 91 | if (pri->pcap != NULL) |
7d98230a | 92 | pcap_close(pri->pcap); |
1da177e4 LT |
93 | } |
94 | ||
95 | struct pcap_handler_data { | |
96 | char *buffer; | |
97 | int len; | |
98 | }; | |
99 | ||
cd1ae0e4 | 100 | static void handler(u_char *data, const struct pcap_pkthdr *header, |
1da177e4 LT |
101 | 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 | ||
112 | int 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); | |
cd1ae0e4 | 120 | if (n < 0) { |
7d98230a JD |
121 | printk(UM_KERN_ERR "pcap_dispatch failed - %s\n", |
122 | pcap_geterr(pri->pcap)); | |
56bd194b | 123 | return -EIO; |
1da177e4 | 124 | } |
cd1ae0e4 | 125 | else if (n == 0) |
56bd194b JD |
126 | return 0; |
127 | return hdata.len; | |
1da177e4 LT |
128 | } |
129 | ||
5e7672ec | 130 | const struct net_user_info pcap_user_info = { |
1da177e4 LT |
131 | .init = pcap_user_init, |
132 | .open = pcap_open, | |
133 | .close = NULL, | |
134 | .remove = pcap_remove, | |
1da177e4 LT |
135 | .add_address = NULL, |
136 | .delete_address = NULL, | |
b53f35a8 JD |
137 | .mtu = ETH_MAX_PACKET, |
138 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER, | |
1da177e4 | 139 | }; |