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"
16 #include "kern_constants.h"
18 #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER)
20 #define PCAP_FD(p) (*(int *)(p))
22 static int pcap_user_init(void *data
, void *dev
)
24 struct pcap_data
*pri
= data
;
26 char errors
[PCAP_ERRBUF_SIZE
];
28 p
= pcap_open_live(pri
->host_if
, MAX_PACKET
, pri
->promisc
, 0, errors
);
30 printk(UM_KERN_ERR
"pcap_user_init : pcap_open_live failed - "
40 static int pcap_open(void *data
)
42 struct pcap_data
*pri
= data
;
49 if(pri
->filter
!= NULL
){
50 err
= dev_netmask(pri
->dev
, &netmask
);
52 printk(UM_KERN_ERR
"pcap_open : dev_netmask failed\n");
56 pri
->compiled
= um_kmalloc(sizeof(struct bpf_program
));
57 if(pri
->compiled
== NULL
){
58 printk(UM_KERN_ERR
"pcap_open : kmalloc failed\n");
62 err
= pcap_compile(pri
->pcap
,
63 (struct bpf_program
*) pri
->compiled
,
64 pri
->filter
, pri
->optimize
, netmask
);
66 printk(UM_KERN_ERR
"pcap_open : pcap_compile failed - "
67 "'%s'\n", pcap_geterr(pri
->pcap
));
71 err
= pcap_setfilter(pri
->pcap
, pri
->compiled
);
73 printk(UM_KERN_ERR
"pcap_open : pcap_setfilter "
74 "failed - '%s'\n", pcap_geterr(pri
->pcap
));
79 return PCAP_FD(pri
->pcap
);
82 static void pcap_remove(void *data
)
84 struct pcap_data
*pri
= data
;
86 if(pri
->compiled
!= NULL
)
87 pcap_freecode(pri
->compiled
);
90 pcap_close(pri
->pcap
);
93 struct pcap_handler_data
{
98 static void handler(u_char
*data
, const struct pcap_pkthdr
*header
,
103 struct pcap_handler_data
*hdata
= (struct pcap_handler_data
*) data
;
105 len
= hdata
->len
< header
->caplen
? hdata
->len
: header
->caplen
;
106 memcpy(hdata
->buffer
, packet
, len
);
110 int pcap_user_read(int fd
, void *buffer
, int len
, struct pcap_data
*pri
)
112 struct pcap_handler_data hdata
= ((struct pcap_handler_data
)
117 n
= pcap_dispatch(pri
->pcap
, 1, handler
, (u_char
*) &hdata
);
119 printk(UM_KERN_ERR
"pcap_dispatch failed - %s\n",
120 pcap_geterr(pri
->pcap
));
128 const struct net_user_info pcap_user_info
= {
129 .init
= pcap_user_init
,
132 .remove
= pcap_remove
,
135 .delete_address
= NULL
,
136 .max_packet
= MAX_PACKET
- ETH_HEADER_OTHER