Debian Codegraph @

common/net_io_filter.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * NetIO Filtering.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <time.h>
18 #include <sys/time.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #include <pthread.h>
29
30 #ifdef GEN_ETH
31 #include <pcap.h>
32 #endif
33
34 #include "registry.h"
35 #include "net.h"
36 #include "net_io.h"
37 #include "net_io_filter.h"
38
39 /* Filter list */
40 static netio_pktfilter_t *pf_list = NULL;
41
42 /* Find a filter */
43 netio_pktfilter_t *netio_filter_find(char *name)
44 {
45 netio_pktfilter_t *pf;
46
47 for(pf=pf_list;pf;pf=pf->next)
48 if (!strcmp(pf->name,name))
49 return pf;
50
51 return NULL;
52 }
53
54 /* Add a new filter */
55 int netio_filter_add(netio_pktfilter_t *pf)
56 {
57 if (netio_filter_find(pf->name) != NULL)
58 return(-1);
59
60 pf->next = pf_list;
61 pf_list = pf;
62 return(0);
63 }
64
65 /* Bind a filter to a NIO */
66 int netio_filter_bind(netio_desc_t *nio,int direction,char *pf_name)
67 {
68 netio_pktfilter_t *pf;
69
70 if (!(pf = netio_filter_find(pf_name)))
71 return(-1);
72
73 if (direction == NETIO_FILTER_DIR_RX) {
74 nio->rx_filter_data = NULL;
75 nio->rx_filter = pf;
76 } else if (direction == NETIO_FILTER_DIR_TX) {
77 nio->tx_filter_data = NULL;
78 nio->tx_filter = pf;
79 } else {
80 nio->both_filter_data = NULL;
81 nio->both_filter = pf;
82 }
83 return(0);
84 }
85
86 /* Unbind a filter from a NIO */
87 int netio_filter_unbind(netio_desc_t *nio,int direction)
88 {
89 netio_pktfilter_t *pf;
90 void **opt;
91
92 if (direction == NETIO_FILTER_DIR_RX) {
93 opt = &nio->rx_filter_data;
94 pf = nio->rx_filter;
95 } else if (direction == NETIO_FILTER_DIR_TX) {
96 opt = &nio->tx_filter_data;
97 pf = nio->tx_filter;
98 } else {
99 opt = &nio->both_filter_data;
100 pf = nio->both_filter;
101 }
102
103 if (!pf)
104 return(-1);
105
106 pf->free(nio,opt);
107 return(0);
108 }
109
110 /* Setup a filter */
111 int netio_filter_setup(netio_desc_t *nio,int direction,int argc,char *argv[])
112 {
113 netio_pktfilter_t *pf;
114 void **opt;
115
116 if (direction == NETIO_FILTER_DIR_RX) {
117 opt = &nio->rx_filter_data;
118 pf = nio->rx_filter;
119 } else if (direction == NETIO_FILTER_DIR_TX) {
120 opt = &nio->tx_filter_data;
121 pf = nio->tx_filter;
122 } else {
123 opt = &nio->both_filter_data;
124 pf = nio->both_filter;
125 }
126
127 if (!pf)
128 return(-1);
129
130 return(pf->setup(nio,opt,argc,argv));
131 }
132
133 /* ======================================================================== */
134 /* Packet Capture ("capture") */
135 /* GFA */
136 /* ======================================================================== */
137 #ifdef GEN_ETH
138
139 struct netio_filter_capture {
140 pcap_t *desc;
141 pcap_dumper_t *dumper;
142 pthread_mutex_t lock;
143 };
144
145 /* Free resources used by filter */
146 static void pf_capture_free(netio_desc_t *nio,void **opt)
147 {
148 struct netio_filter_capture *c = *opt;
149
150 if (c != NULL) {
151 printf("NIO %s: ending packet capture.\n",nio->name);
152
153 /* Close dumper */
154 if (c->dumper)
155 pcap_dump_close(c->dumper);
156
157 /* Close PCAP descriptor */
158 if (c->desc)
159 pcap_close(c->desc);
160
161 pthread_mutex_destroy(&c->lock);
162
163 free(c);
164 *opt = NULL;
165 }
166 }
167
168 /* Setup filter resources */
169 static int pf_capture_setup(netio_desc_t *nio,void **opt,
170 int argc,char *argv[])
171 {
172 struct netio_filter_capture *c;
173 int link_type;
174
175 /* We must have a link type and a filename */
176 if (argc != 2)
177 return(-1);
178
179 /* Free resources if something has already been done */
180 pf_capture_free(nio,opt);
181
182 /* Allocate structure to hold PCAP info */
183 if (!(c = malloc(sizeof(*c))))
184 return(-1);
185
186 if (pthread_mutex_init(&c->lock,NULL)) {
187 fprintf(stderr,"NIO %s: pthread_mutex_init failure (file %s)\n",
188 nio->name,argv[0]);
189 goto pcap_lock_err;
190 }
191
192 if ((link_type = pcap_datalink_name_to_val(argv[0])) == -1) {
193 fprintf(stderr,"NIO %s: unknown link type %s, assuming Ethernet.\n",
194 nio->name,argv[0]);
195 link_type = DLT_EN10MB;
196 }
197
198 /* Open a dead pcap descriptor */
199 if (!(c->desc = pcap_open_dead(link_type,65535))) {
200 fprintf(stderr,"NIO %s: pcap_open_dead failure\n",nio->name);
201 goto pcap_open_err;
202 }
203
204 /* Open the output file */
205 if (!(c->dumper = pcap_dump_open(c->desc,argv[1]))) {
206 fprintf(stderr,"NIO %s: pcap_dump_open failure (file %s)\n",
207 nio->name,argv[0]);
208 goto pcap_dump_err;
209 }
210
211 printf("NIO %s: capturing to file '%s'\n",nio->name,argv[1]);
212 *opt = c;
213 return(0);
214
215 pcap_dump_err:
216 pcap_close(c->desc);
217 pcap_open_err:
218 pthread_mutex_destroy(&c->lock);
219 pcap_lock_err:
220 free(c);
221 return(-1);
222 }
223
224 /* Packet handler: write packets to a file in CAP format */
225 static int pf_capture_pkt_handler(netio_desc_t *nio,void *pkt,size_t len,
226 void *opt)
227 {
228 struct netio_filter_capture *c = opt;
229 struct pcap_pkthdr pkt_hdr;
230
231 if (c != NULL) {
232 gettimeofday(&pkt_hdr.ts,0);
233 pkt_hdr.caplen = m_min(len, (u_int)pcap_snapshot(c->desc));
234 pkt_hdr.len = len;
235
236 /* thread safe dump */
237 pthread_mutex_lock(&c->lock);
238 pcap_dump((u_char *)c->dumper,&pkt_hdr,pkt);
239 pcap_dump_flush(c->dumper);
240 pthread_mutex_unlock(&c->lock);
241 }
242
243 return(NETIO_FILTER_ACTION_PASS);
244 }
245
246 /* Packet capture */
247 static netio_pktfilter_t pf_capture_def = {
248 "capture",
249 pf_capture_setup,
250 pf_capture_free,
251 pf_capture_pkt_handler,
252 NULL,
253 };
254
255 #endif
256
257 /* ======================================================================== */
258 /* Frequency Dropping ("freq_drop"). */
259 /* ======================================================================== */
260
261 struct pf_freqdrop_data {
262 int frequency;
263 int current;
264 };
265
266 /* Setup filter ressources */
267 static int pf_freqdrop_setup(netio_desc_t *nio,void **opt,
268 int argc,char *argv[])
269 {
270 struct pf_freqdrop_data *data = *opt;
271
272 if (argc != 1)
273 return(-1);
274
275 if (!data) {
276 if (!(data = malloc(sizeof(*data))))
277 return(-1);
278
279 *opt = data;
280 }
281
282 data->current = 0;
283 data->frequency = atoi(argv[0]);
284 return(0);
285 }
286
287 /* Free ressources used by filter */
288 static void pf_freqdrop_free(netio_desc_t *nio,void **opt)
289 {
290 if (*opt)
291 free(*opt);
292
293 *opt = NULL;
294 }
295
296 /* Packet handler: drop 1 out of n packets */
297 static int pf_freqdrop_pkt_handler(netio_desc_t *nio,void *pkt,size_t len,
298 void *opt)
299 {
300 struct pf_freqdrop_data *data = opt;
301
302 if (data != NULL) {
303 switch(data->frequency) {
304 case -1:
305 return(NETIO_FILTER_ACTION_DROP);
306 case 0:
307 return(NETIO_FILTER_ACTION_PASS);
308 default:
309 data->current++;
310
311 if (data->current == data->frequency) {
312 data->current = 0;
313 return(NETIO_FILTER_ACTION_DROP);
314 }
315 }
316 }
317
318 return(NETIO_FILTER_ACTION_PASS);
319 }
320
321 /* Packet dropping at 1/n frequency */
322 static netio_pktfilter_t pf_freqdrop_def = {
323 "freq_drop",
324 pf_freqdrop_setup,
325 pf_freqdrop_free,
326 pf_freqdrop_pkt_handler,
327 NULL,
328 };
329
330 /* ======================================================================== */
331 /* Initialization of packet filters. */
332 /* ======================================================================== */
333
334 void netio_filter_load_all(void)
335 {
336 netio_filter_add(&pf_freqdrop_def);
337 #ifdef GEN_ETH
338 netio_filter_add(&pf_capture_def);
339 #endif
340 }