Annotation of libaitio/src/bpf.c, revision 1.3.8.1
1.2 misho 1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.3.8.1 ! misho 6: * $Id: bpf.c,v 1.3 2013/07/09 00:35:36 misho Exp $
1.2 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /*
50: * io_get1stiface() - Get first interface of host
51: *
52: * @szIface = interface string buffer
53: * @iflen = size of interface buffer
54: * return: -1 error or 0 ok
55: */
56: int
57: io_get1stiface(char *szIface, int iflen)
58: {
59: struct ifaddrs *ifa;
60:
61: if (!szIface || !iflen)
62: return -1;
63:
64: getifaddrs(&ifa);
65: strlcpy(szIface, ifa->ifa_name, iflen);
66: freeifaddrs(ifa);
67: return 0;
68: }
69:
70: /*
71: * io_etherClose() - Close BPF interface
72: *
73: * @eth = bpf handle
74: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
75: * return: none
76: */
77: void
78: io_etherClose(int eth, void **zcbuf)
79: {
80: #ifdef __FreeBSD__
81: struct bpf_zbuf *zbuf = NULL;
82: #endif
83:
84: if (eth > STDERR_FILENO)
85: close(eth);
86:
87: if (zcbuf && *zcbuf) {
88: #ifdef __FreeBSD__
89: zbuf = *zcbuf;
90: munmap(zbuf->bz_bufb, zbuf->bz_buflen);
91: munmap(zbuf->bz_bufa, zbuf->bz_buflen);
92: e_free(*zcbuf);
93: *zcbuf = NULL;
94: #endif
95: }
96: }
97:
98: #ifdef __FreeBSD__
99: static inline struct bpf_zbuf *
100: allocZCbuf(u_int len)
101: {
102: struct bpf_zbuf *zbuf = NULL;
103:
104: zbuf = e_malloc(sizeof(struct bpf_zbuf));
105: if (!zbuf) {
106: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
107: return NULL;
108: } else
109: memset(zbuf, 0, sizeof(struct bpf_zbuf));
110:
111: zbuf->bz_buflen = roundup(len, getpagesize());
112: zbuf->bz_bufa = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
113: if (zbuf->bz_bufa == MAP_FAILED) {
114: LOGERR;
115: e_free(zbuf);
116: return NULL;
117: } else
118: memset(zbuf->bz_bufa, 0, zbuf->bz_buflen);
119: zbuf->bz_bufb = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
120: if (zbuf->bz_bufb == MAP_FAILED) {
121: LOGERR;
122: munmap(zbuf->bz_bufa, zbuf->bz_buflen);
123: e_free(zbuf);
124: return NULL;
125: } else
126: memset(zbuf->bz_bufb, 0, zbuf->bz_buflen);
127:
128: return zbuf;
129: }
130: #endif
131:
132: /*
133: * io_etherOpen() - Open BPF interface to device
134: *
135: * @csIface = interface name
136: * @flags = open flags
137: * @whdr = with complete headers
1.3 misho 138: * @wdlt = with data link type
1.2 misho 139: * @buflen = buffer length
140: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
141: * return: -1 error or >-1 bpf handle
142: */
143: int
1.3 misho 144: io_etherOpen(const char *csIface, int flags, int whdr, int wdlt,
145: u_int *buflen, void **zcbuf)
1.2 misho 146: {
147: int eth = -1;
148: register int i;
149: char szStr[STRSIZ];
150: struct ifreq ifr;
151: u_int n = 1;
152:
153: #ifndef __FreeBSD__
154: if (zcbuf) {
155: io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
156: return -1;
157: }
158: #endif
159:
160: for (i = 0; i < BPF_DEV_MAX; i++) {
161: memset(szStr, 0, sizeof szStr);
162: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
163: eth = open(szStr, flags);
164: if (eth > STDERR_FILENO)
165: break;
166: }
167: if (eth < 3) {
168: LOGERR;
169: return -1;
170: }
171:
172: if (!zcbuf) {
173: if (ioctl(eth, BIOCGBLEN, &n) == -1) {
174: LOGERR;
175: close(eth);
176: return -1;
177: } else
178: n = (buflen && *buflen) ? *buflen : n;
179: if (ioctl(eth, BIOCSBLEN, &n) == -1) {
180: LOGERR;
181: close(eth);
182: return -1;
183: }
184: if (buflen && *buflen)
185: *buflen = n;
186: } else {
187: #ifdef __FreeBSD__
188: n = BPF_BUFMODE_ZBUF;
189: if (ioctl(eth, BIOCSETBUFMODE, &n) == -1) {
190: LOGERR;
191: close(eth);
192: return -1;
193: }
194: if (ioctl(eth, BIOCGETZMAX, &n) == -1) {
195: LOGERR;
196: close(eth);
197: return -1;
198: } else
199: n = (buflen && *buflen) ? *buflen : n;
200: if (!(*zcbuf = allocZCbuf(n))) {
201: close(eth);
202: return -1;
203: }
204: if (ioctl(eth, BIOCSETZBUF, *zcbuf) == -1) {
205: LOGERR;
206: io_etherClose(eth, zcbuf);
207: return -1;
208: }
209: if (buflen && *buflen)
210: *buflen = n;
211: #endif
212: }
213:
214: if (csIface)
215: strlcpy(szStr, csIface, sizeof szStr);
216: else if (io_get1stiface(szStr, sizeof szStr) == -1) {
217: io_etherClose(eth, zcbuf);
218: return -1;
219: }
220: memset(&ifr, 0, sizeof ifr);
221: strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
222: if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
223: LOGERR;
224: io_etherClose(eth, zcbuf);
225: return -1;
226: }
227:
228: n = 1;
229: if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) {
230: LOGERR;
231: io_etherClose(eth, zcbuf);
232: return -1;
233: }
1.3 misho 234: if (wdlt && ioctl(eth, BIOCSDLT, &wdlt) == -1) {
235: LOGERR;
236: io_etherClose(eth, zcbuf);
237: return -1;
238: }
1.2 misho 239: if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
240: LOGERR;
241: io_etherClose(eth, zcbuf);
242: return -1;
243: }
244:
245: return eth;
246: }
247:
248: /*
249: * io_etherSend() - Send packet to bpf
250: *
251: * @eth = bpf handle
252: * @buf = buffer
253: * @buflen = buffer length
254: * return: -1 error or !=-1 written bytes
255: */
256: ssize_t
257: io_etherSend(int eth, const void *buf, size_t buflen)
258: {
259: ssize_t wlen = 0;
260:
261: if (!buf || !buflen) {
262: io_SetErr(EINVAL, "invalid arguments");
263: return -1;
264: }
265:
266: wlen = write(eth, buf, buflen);
267: if (wlen == -1)
268: LOGERR;
269: return wlen;
270: }
271:
272: #ifdef __FreeBSD__
273: static inline void
274: ackZCbuf(struct bpf_zbuf_header *bzh)
275: {
276: atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen);
277: }
278:
279: static inline int
280: chkZCbuf(struct bpf_zbuf_header *bzh)
281: {
282: /* return true if userspace owns buffer, and false otherwise. */
283: return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen));
284: }
285:
286: static ssize_t
287: nextZCbuf(int eth, struct bpf_zbuf * __restrict zbuf, void * __restrict buf, size_t buflen)
288: {
289: ssize_t rlen = 0;
290: struct bpf_zbuf bz;
291: struct bpf_zbuf_header *bzh;
292: off_t pos = 0;
293:
294: bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa;
295: if (chkZCbuf(bzh)) {
296: rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
297: memcpy(buf + pos, zbuf->bz_bufa + sizeof(struct bpf_zbuf_header), rlen);
298: ackZCbuf(bzh);
299: pos += rlen;
300: }
301: bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb;
302: if (chkZCbuf(bzh)) {
303: rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
304: memcpy(buf + pos, zbuf->bz_bufb + sizeof(struct bpf_zbuf_header), rlen);
305: ackZCbuf(bzh);
306: pos += rlen;
307: }
308:
309: if (!pos) {
310: if ((rlen = ioctl(eth, BIOCROTZBUF, &bz)) == -1)
311: LOGERR;
312: } else
313: rlen = pos;
314: return rlen;
315: }
316: #endif
317:
318: /*
319: * io_etherRecv() - Receive packet from bpf
320: *
321: * @eth = bpf handle
322: * @buf = buffer
323: * @buflen = buffer length
324: * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
325: * return: -1 error or !=-1 readed bytes
326: */
327: ssize_t
328: io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf)
329: {
330: ssize_t rlen = 0;
1.3 misho 331: struct bpf_hdr *h;
1.2 misho 332:
333: if (!buf || !buflen) {
334: io_SetErr(EINVAL, "invalid arguments");
335: return -1;
336: }
337:
338: if (!zcbuf) {
339: rlen = read(eth, buf, buflen);
340: if (rlen == -1)
341: LOGERR;
342: } else {
343: #ifdef __FreeBSD__
344: rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
345: if (!rlen)
346: rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
347: #else
348: rlen = -1;
349: io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
350: #endif
351: }
352:
1.3 misho 353: h = (struct bpf_hdr*) buf;
354: rlen -= h->bh_hdrlen;
355:
356: if (h->bh_caplen != rlen) {
357: if (h->bh_caplen < rlen)
358: rlen = h->bh_caplen;
359: else {
360: io_SetErr(EIO, "Captured %d bytes should be at most %d bytes",
361: h->bh_caplen, rlen);
362: return -1;
363: }
364: }
365:
366: memmove(buf, buf + h->bh_hdrlen, rlen);
1.2 misho 367: return rlen;
368: }
1.3.8.1 ! misho 369:
! 370: /*
! 371: * io_etherFilter() - BPF filter routine
! 372: *
! 373: * @eth = bpf handle
! 374: * @io = filter direction
! 375: * (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
! 376: * @insn = BPF filter instruction array
! 377: * @insnlen = Length of BPF filter instruction array
! 378: * return: -1 error or 0 ok
! 379: */
! 380: int
! 381: io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen)
! 382: {
! 383: int ret = 0;
! 384: struct bpf_program fcode = { 0 };
! 385:
! 386: if (io != IO_ETHER_FILTER_PROMISC && (!insn || !insnlen)) {
! 387: io_SetErr(EINVAL, "invalid arguments");
! 388: return -1;
! 389: }
! 390:
! 391: switch (io) {
! 392: case IO_ETHER_FILTER_PROMISC: /* promiscuous mode */
! 393: ret = ioctl(eth, BIOCPROMISC, NULL);
! 394: break;
! 395: case IO_ETHER_FILTER_NOTREAD: /* read not filter */
! 396: fcode.bf_len = insnlen / sizeof(struct bpf_insn);
! 397: fcode.bf_insns = insn;
! 398: ret = ioctl(eth, BIOCSETFNR, &fcode);
! 399: break;
! 400: case IO_ETHER_FILTER_READ: /* read filter */
! 401: fcode.bf_len = insnlen / sizeof(struct bpf_insn);
! 402: fcode.bf_insns = insn;
! 403: ret = ioctl(eth, BIOCSETF, &fcode);
! 404: break;
! 405: case IO_ETHER_FILTER_WRITE: /* write filter */
! 406: fcode.bf_len = insnlen / sizeof(struct bpf_insn);
! 407: fcode.bf_insns = insn;
! 408: ret = ioctl(eth, BIOCSETWF, &fcode);
! 409: break;
! 410: }
! 411:
! 412: if (ret == -1)
! 413: LOGERR;
! 414: return ret;
! 415: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>