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