2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL.
11 #include "pcap_user.h"
12 #include "kern_constants.h"
13 #include "um_malloc.h"
16 #define PCAP_FD(p) (*(int *)(p))
18 static int pcap_user_init(void *data, void *dev)
20 struct pcap_data *pri = data;
22 char errors[PCAP_ERRBUF_SIZE];
24 p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
25 pri->promisc, 0, errors);
27 printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
37 static int pcap_open(void *data)
39 struct pcap_data *pri = data;
43 if (pri->pcap == NULL)
46 if (pri->filter != NULL) {
47 err = dev_netmask(pri->dev, &netmask);
49 printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
53 pri->compiled = kmalloc(sizeof(struct bpf_program),
55 if (pri->compiled == NULL) {
56 printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
60 err = pcap_compile(pri->pcap,
61 (struct bpf_program *) pri->compiled,
62 pri->filter, pri->optimize, netmask);
64 printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
65 "'%s'\n", pcap_geterr(pri->pcap));
69 err = pcap_setfilter(pri->pcap, pri->compiled);
71 printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
72 "failed - '%s'\n", pcap_geterr(pri->pcap));
77 return PCAP_FD(pri->pcap);
84 static void pcap_remove(void *data)
86 struct pcap_data *pri = data;
88 if (pri->compiled != NULL)
89 pcap_freecode(pri->compiled);
91 if (pri->pcap != NULL)
92 pcap_close(pri->pcap);
95 struct pcap_handler_data {
100 static void handler(u_char *data, const struct pcap_pkthdr *header,
101 const u_char *packet)
105 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
107 len = hdata->len < header->caplen ? hdata->len : header->caplen;
108 memcpy(hdata->buffer, packet, len);
112 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
114 struct pcap_handler_data hdata = ((struct pcap_handler_data)
119 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
121 printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
122 pcap_geterr(pri->pcap));
130 const struct net_user_info pcap_user_info = {
131 .init = pcap_user_init,
134 .remove = pcap_remove,
136 .delete_address = NULL,
137 .mtu = ETH_MAX_PACKET,
138 .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,