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