Blame view

arch/um/drivers/pcap_user.c 2.91 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
cd1ae0e49   Jeff Dike   uml: network form...
2
   * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
   * Licensed under the GPL.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
  #include <errno.h>
  #include <pcap.h>
cd1ae0e49   Jeff Dike   uml: network form...
7
  #include <string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
  #include <asm/types.h>
  #include "net_user.h"
  #include "pcap_user.h"
cd1ae0e49   Jeff Dike   uml: network form...
11
  #include "um_malloc.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
  #define PCAP_FD(p) (*(int *)(p))
f34d9d2dc   Jeff Dike   uml: network inte...
14
  static int pcap_user_init(void *data, void *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
  {
  	struct pcap_data *pri = data;
  	pcap_t *p;
  	char errors[PCAP_ERRBUF_SIZE];
b53f35a80   Jeff Dike   uml: network driv...
19
20
  	p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
  			   pri->promisc, 0, errors);
cd1ae0e49   Jeff Dike   uml: network form...
21
  	if (p == NULL) {
7d98230a7   Jeff Dike   uml: network and ...
22
23
24
  		printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
  		       "'%s'
  ", errors);
f34d9d2dc   Jeff Dike   uml: network inte...
25
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
  	}
  
  	pri->dev = dev;
  	pri->pcap = p;
f34d9d2dc   Jeff Dike   uml: network inte...
30
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
35
36
37
  }
  
  static int pcap_open(void *data)
  {
  	struct pcap_data *pri = data;
  	__u32 netmask;
  	int err;
cd1ae0e49   Jeff Dike   uml: network form...
38
  	if (pri->pcap == NULL)
56bd194bb   Jeff Dike   uml: driver forma...
39
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

cd1ae0e49   Jeff Dike   uml: network form...
41
  	if (pri->filter != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  		err = dev_netmask(pri->dev, &netmask);
cd1ae0e49   Jeff Dike   uml: network form...
43
  		if (err < 0) {
7d98230a7   Jeff Dike   uml: network and ...
44
45
  			printk(UM_KERN_ERR "pcap_open : dev_netmask failed
  ");
56bd194bb   Jeff Dike   uml: driver forma...
46
  			return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  		}
296cd66f7   Al Viro   missed kmalloc() ...
48
  		pri->compiled = uml_kmalloc(sizeof(struct bpf_program),
cd1ae0e49   Jeff Dike   uml: network form...
49
50
  					UM_GFP_KERNEL);
  		if (pri->compiled == NULL) {
7d98230a7   Jeff Dike   uml: network and ...
51
52
  			printk(UM_KERN_ERR "pcap_open : kmalloc failed
  ");
56bd194bb   Jeff Dike   uml: driver forma...
53
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  		}
56bd194bb   Jeff Dike   uml: driver forma...
55

cd1ae0e49   Jeff Dike   uml: network form...
56
57
  		err = pcap_compile(pri->pcap,
  				   (struct bpf_program *) pri->compiled,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
  				   pri->filter, pri->optimize, netmask);
cd1ae0e49   Jeff Dike   uml: network form...
59
  		if (err < 0) {
7d98230a7   Jeff Dike   uml: network and ...
60
61
62
  			printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
  			       "'%s'
  ", pcap_geterr(pri->pcap));
cd1ae0e49   Jeff Dike   uml: network form...
63
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
  		}
  
  		err = pcap_setfilter(pri->pcap, pri->compiled);
cd1ae0e49   Jeff Dike   uml: network form...
67
  		if (err < 0) {
7d98230a7   Jeff Dike   uml: network and ...
68
69
70
  			printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
  			       "failed - '%s'
  ", pcap_geterr(pri->pcap));
cd1ae0e49   Jeff Dike   uml: network form...
71
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
  		}
  	}
56bd194bb   Jeff Dike   uml: driver forma...
74
75
  
  	return PCAP_FD(pri->pcap);
cd1ae0e49   Jeff Dike   uml: network form...
76
77
78
79
  
   out:
  	kfree(pri->compiled);
  	return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
  }
  
  static void pcap_remove(void *data)
  {
  	struct pcap_data *pri = data;
cd1ae0e49   Jeff Dike   uml: network form...
85
  	if (pri->compiled != NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  		pcap_freecode(pri->compiled);
cd1ae0e49   Jeff Dike   uml: network form...
87
  	if (pri->pcap != NULL)
7d98230a7   Jeff Dike   uml: network and ...
88
  		pcap_close(pri->pcap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
  }
  
  struct pcap_handler_data {
  	char *buffer;
  	int len;
  };
cd1ae0e49   Jeff Dike   uml: network form...
95
  static void handler(u_char *data, const struct pcap_pkthdr *header,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  		    const u_char *packet)
  {
  	int len;
  
  	struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
  
  	len = hdata->len < header->caplen ? hdata->len : header->caplen;
  	memcpy(hdata->buffer, packet, len);
  	hdata->len = len;
  }
  
  int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
  {
  	struct pcap_handler_data hdata = ((struct pcap_handler_data)
  		                          { .buffer  	= buffer,
  					    .len 	= len });
  	int n;
  
  	n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
cd1ae0e49   Jeff Dike   uml: network form...
115
  	if (n < 0) {
7d98230a7   Jeff Dike   uml: network and ...
116
117
118
  		printk(UM_KERN_ERR "pcap_dispatch failed - %s
  ",
  		       pcap_geterr(pri->pcap));
56bd194bb   Jeff Dike   uml: driver forma...
119
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  	}
cd1ae0e49   Jeff Dike   uml: network form...
121
  	else if (n == 0)
56bd194bb   Jeff Dike   uml: driver forma...
122
123
  		return 0;
  	return hdata.len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  }
5e7672ec3   Jeff Dike   [PATCH] uml: cons...
125
  const struct net_user_info pcap_user_info = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
  	.init		= pcap_user_init,
  	.open		= pcap_open,
  	.close	 	= NULL,
  	.remove	 	= pcap_remove,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
  	.add_address	= NULL,
  	.delete_address = NULL,
b53f35a80   Jeff Dike   uml: network driv...
132
133
  	.mtu		= ETH_MAX_PACKET,
  	.max_packet	= ETH_MAX_PACKET + ETH_HEADER_OTHER,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  };