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