2 * Copyright (C) 2002 Jeff Dike <jdike@karaya.com>
3 * Licensed under the GPL.
11 #include <asm/types.h>
13 #include "pcap_user.h"
15 #include "um_malloc.h"
17 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
19 #define PCAP_FD(p) (*(int *)(p))
21 static void pcap_user_init(void *data, void *dev)
23 struct pcap_data *pri = data;
25 char errors[PCAP_ERRBUF_SIZE];
27 p = pcap_open_live(pri->host_if, MAX_PACKET, pri->promisc, 0, errors);
29 printk("pcap_user_init : pcap_open_live failed - '%s'\n",
38 static int pcap_open(void *data)
40 struct pcap_data *pri = data;
47 if(pri->filter != NULL){
48 err = dev_netmask(pri->dev, &netmask);
50 printk("pcap_open : dev_netmask failed\n");
54 pri->compiled = um_kmalloc(sizeof(struct bpf_program));
55 if(pri->compiled == NULL){
56 printk("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("pcap_open : pcap_compile failed - '%s'\n",
65 pcap_geterr(pri->pcap));
69 err = pcap_setfilter(pri->pcap, pri->compiled);
71 printk("pcap_open : pcap_setfilter failed - '%s'\n",
72 pcap_geterr(pri->pcap));
77 return(PCAP_FD(pri->pcap));
80 static void pcap_remove(void *data)
82 struct pcap_data *pri = data;
84 if(pri->compiled != NULL)
85 pcap_freecode(pri->compiled);
87 pcap_close(pri->pcap);
90 struct pcap_handler_data {
95 static void handler(u_char *data, const struct pcap_pkthdr *header,
100 struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
102 len = hdata->len < header->caplen ? hdata->len : header->caplen;
103 memcpy(hdata->buffer, packet, len);
107 int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
109 struct pcap_handler_data hdata = ((struct pcap_handler_data)
114 n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
116 printk("pcap_dispatch failed - %s\n", pcap_geterr(pri->pcap));
124 const struct net_user_info pcap_user_info = {
125 .init = pcap_user_init,
128 .remove = pcap_remove,
131 .delete_address = NULL,
132 .max_packet = MAX_PACKET - ETH_HEADER_OTHER
136 * Overrides for Emacs so that we follow Linus's tabbing style.
137 * Emacs will notice this stuff at the end of the file and automatically
138 * adjust the settings for this buffer only. This must remain at the end
140 * ---------------------------------------------------------------------------
142 * c-file-style: "linux"