2 * Copyright (C) 2002 Jeff Dike <jdike@karaya.com>
3 * Licensed under the GPL.
11 #include <asm/types.h>
13 #include "pcap_user.h"
16 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
18 #define PCAP_FD(p) (*(int *)(p))
20 static void pcap_user_init(void *data, void *dev)
22 struct pcap_data *pri = data;
24 char errors[PCAP_ERRBUF_SIZE];
26 p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
28 printk("pcap_user_init : pcap_open_live failed - '%s'\n",
37 static int pcap_open(void *data)
39 struct pcap_data *pri = data;
46 if(pri->filter != NULL){
47 err = dev_netmask(pri->dev, &netmask);
49 printk("pcap_open : dev_netmask failed\n");
53 pri->compiled = um_kmalloc(sizeof(struct bpf_program));
54 if(pri->compiled == NULL){
55 printk("pcap_open : kmalloc failed\n");
59 err = pcap_compile(pri->pcap,
60 (struct bpf_program *) pri->compiled,
61 pri->filter, pri->optimize, netmask);
63 printk("pcap_open : pcap_compile failed - '%s'\n",
64 pcap_geterr(pri->pcap));
68 err = pcap_setfilter(pri->pcap, pri->compiled);
70 printk("pcap_open : pcap_setfilter failed - '%s'\n",
71 pcap_geterr(pri->pcap));
76 return(PCAP_FD(pri->pcap));
79 static void pcap_remove(void *data)
81 struct pcap_data *pri = data;
83 if(pri->compiled != NULL)
84 pcap_freecode(pri->compiled);
86 pcap_close(pri->pcap);
89 struct pcap_handler_data {
94 static void handler(u_char *data, const struct pcap_pkthdr *header,
99 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
101 len = hdata->len < header->caplen ? hdata->len : header->caplen;
102 memcpy(hdata->buffer, packet, len);
106 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
108 struct pcap_handler_data hdata = ((struct pcap_handler_data)
113 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
115 printk("pcap_dispatch failed - %s\n", pcap_geterr(pri->pcap));
123 const struct net_user_info pcap_user_info = {
124 .init = pcap_user_init,
127 .remove = pcap_remove,
130 .delete_address = NULL,
131 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
135 * Overrides for Emacs so that we follow Linus's tabbing style.
136 * Emacs will notice this stuff at the end of the file and automatically
137 * adjust the settings for this buffer only. This must remain at the end
139 * ---------------------------------------------------------------------------
141 * c-file-style: "linux"