1 /*-
2  * Copyright (c) 2001, 2002, 2004 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $Id: loop-bpf.c,v 1.15 2007/04/22 07:22:40 vlm Exp $
27  */
28 
29 #include "ipcad.h"
30 #include "opt.h"
31 
32 #define	bpf_header	((struct bpf_hdr *)packet)
33 
34 
35 /*
36  * This is the interface loop - a busy horse handling interface statistics.
37  */
38 void *
process_bpf(void * psp)39 process_bpf(void *psp) {
40 	packet_source_t *ps = psp;
41 	ssize_t rd;
42 	char *packet, *safe_end;
43 	struct pollfd pfd;
44 
45 	assert(ps->iface_type == IFACE_BPF);
46 
47 	pfd.fd = ps->fd;
48 	pfd.events = POLLIN;
49 
50 	for(rd = 0;;) {
51 
52 		if(signoff_now)
53 			break;
54 
55 		if(rd == -1) {
56 			if(errno != EAGAIN) {
57 				ps->fd = -1;
58 				close(pfd.fd);
59 				pfd.fd = -1;
60 			}
61 		}
62 
63 		if(ps->state != PST_READY || ps->fd == -1) {
64 			if(init_packet_source(ps, 1)) {
65 				sleep(5);
66 				continue;
67 			}
68 			assert(ps->state == PST_READY);
69 			fprintf(stderr, "\n");
70 			pfd.fd = ps->fd;
71 		}
72 
73 		rd = poll(&pfd, 1, 3000);
74 
75 		/* Poll timeout is almost impossible thing */
76 		if(rd == 0)
77 			continue;
78 
79 		if((rd == -1) || !(pfd.revents & POLLIN)) {
80 			if(rd > 0 && pfd.revents & POLLNVAL) {
81 				fprintf(stderr,
82 					"[%s: INVALID INTERFACE! (%d/%d/%d)]\n",
83 					ps->ifName, (int)rd, pfd.fd, pfd.revents);
84 				sleep(1);
85 			}
86 			rd = -1;
87 			continue;
88 		}
89 
90 		if(signoff_now)
91 			break;
92 
93 		rd = read(ps->fd, ps->buf, ps->bufsize);
94 
95 		/*
96 		 * Processing packets
97 		 */
98 		for(packet = ps->buf,
99 		    safe_end = packet + rd - sizeof(struct bpf_hdr);
100 			packet < safe_end;
101 				packet += BPF_WORDALIGN(bpf_header->bh_hdrlen
102 				  + bpf_header->bh_caplen) )
103 		{
104 			void *ip_start = packet + bpf_header->bh_hdrlen;
105 			int toend_size = rd-((char *)ip_start-(char *)ps->buf);
106 			int captured_size = bpf_header->bh_caplen;
107 			if(toend_size < captured_size)	/* Paranoya */
108 				captured_size = toend_size;
109 			process_packet_data(ps, ip_start, captured_size, 0, 0);
110 		}
111 
112 	}
113 
114 	return 0;
115 
116 }
117 
118