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