File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / bpf.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Mon Oct 21 21:12:42 2013 UTC (10 years, 8 months ago) by misho
Branches: MAIN
CVS tags: io6_7, io6_6, io6_5, io6_4, io6_3, io6_2, io6_1, io6_0, IO6_6, IO6_5, IO6_4, IO6_3, IO6_2, IO6_1, IO6_0, IO5_9, HEAD
version 5.9

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: bpf.c,v 1.4 2013/10/21 21:12:42 misho Exp $
    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
  138:  * @wdlt = with data link type
  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
  144: io_etherOpen(const char *csIface, int flags, int whdr, int wdlt, 
  145: 		u_int *buflen, void **zcbuf)
  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 (csIface)
  173: 		strlcpy(szStr, csIface, sizeof szStr);
  174: 	else if (io_get1stiface(szStr, sizeof szStr) == -1) {
  175: 		close(eth);
  176: 		return -1;
  177: 	}
  178: 
  179: 	n = 1;
  180: 	if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) {
  181: 		LOGERR;
  182: 		close(eth);
  183: 		return -1;
  184: 	}
  185: 	if (wdlt && ioctl(eth, BIOCSDLT, &wdlt) == -1) {
  186: 		LOGERR;
  187: 		close(eth);
  188: 		return -1;
  189: 	}
  190: 	if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
  191: 		LOGERR;
  192: 		close(eth);
  193: 		return -1;
  194: 	}
  195: 
  196: 	if (!zcbuf) {
  197: 		if (ioctl(eth, BIOCGBLEN, &n) == -1) {
  198: 			LOGERR;
  199: 			close(eth);
  200: 			return -1;
  201: 		} else
  202: 			n = (buflen && *buflen) ? *buflen : getpagesize();
  203: 
  204: 		if (ioctl(eth, BIOCSBLEN, &n) == -1) {
  205: 			LOGERR;
  206: 			close(eth);
  207: 			return -1;
  208: 		}
  209: 		if (buflen)
  210: 			*buflen = n;
  211: 	} else {
  212: #ifdef __FreeBSD__
  213: 		n = BPF_BUFMODE_ZBUF;
  214: 		if (ioctl(eth, BIOCSETBUFMODE, &n) == -1) {
  215: 			LOGERR;
  216: 			close(eth);
  217: 			return -1;
  218: 		}
  219: 		if (ioctl(eth, BIOCGETZMAX, &n) == -1) {
  220: 			LOGERR;
  221: 			close(eth);
  222: 			return -1;
  223: 		} else
  224: 			n = (buflen && *buflen) ? *buflen : n;
  225: 		if (!(*zcbuf = allocZCbuf(n))) {
  226: 			close(eth);
  227: 			return -1;
  228: 		}
  229: 		if (ioctl(eth, BIOCSETZBUF, *zcbuf) == -1) {
  230: 			LOGERR;
  231: 			io_etherClose(eth, zcbuf);
  232: 			return -1;
  233: 		}
  234: 		if (buflen && *buflen)
  235: 			*buflen = n;
  236: #endif
  237: 	}
  238: 
  239: 	memset(&ifr, 0, sizeof ifr);
  240: 	strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
  241: 	if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
  242: 		LOGERR;
  243: 		io_etherClose(eth, zcbuf);
  244: 		return -1;
  245: 	}
  246: 
  247: 	return eth;
  248: }
  249: 
  250: /*
  251:  * io_etherSend() - Send packet to bpf
  252:  *
  253:  * @eth = bpf handle
  254:  * @buf = buffer
  255:  * @buflen = buffer length
  256:  * return: -1 error or !=-1 written bytes
  257:  */
  258: ssize_t
  259: io_etherSend(int eth, const void *buf, size_t buflen)
  260: {
  261: 	ssize_t wlen = 0;
  262: 
  263: 	if (!buf || !buflen) {
  264: 		io_SetErr(EINVAL, "invalid arguments");
  265: 		return -1;
  266: 	}
  267: 
  268: 	wlen = write(eth, buf, buflen);
  269: 	if (wlen == -1)
  270: 		LOGERR;
  271: 	return wlen;
  272: }
  273: 
  274: #ifdef __FreeBSD__
  275: static inline void
  276: ackZCbuf(struct bpf_zbuf_header *bzh)
  277: {
  278: 	atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen);
  279: }
  280: 
  281: static inline int
  282: chkZCbuf(struct bpf_zbuf_header *bzh)
  283: {
  284: 	/* return true if userspace owns buffer, and false otherwise. */
  285: 	return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen));
  286: }
  287: 
  288: static ssize_t
  289: nextZCbuf(int eth, struct bpf_zbuf * __restrict zbuf, void * __restrict buf, size_t buflen)
  290: {
  291: 	ssize_t rlen = 0;
  292: 	struct bpf_zbuf bz;
  293: 	struct bpf_zbuf_header *bzh;
  294: 	off_t pos = 0;
  295: 
  296: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa;
  297: 	if (chkZCbuf(bzh)) {
  298: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  299: 		memcpy(buf + pos, zbuf->bz_bufa + sizeof(struct bpf_zbuf_header), rlen);
  300: 		ackZCbuf(bzh);
  301: 		pos += rlen;
  302: 	}
  303: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb;
  304: 	if (chkZCbuf(bzh)) {
  305: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  306: 		memcpy(buf + pos, zbuf->bz_bufb + sizeof(struct bpf_zbuf_header), rlen);
  307: 		ackZCbuf(bzh);
  308: 		pos += rlen;
  309: 	}
  310: 
  311: 	if (!pos) {
  312: 		if ((rlen = ioctl(eth, BIOCROTZBUF, &bz)) == -1)
  313: 			LOGERR;
  314: 	} else
  315: 		rlen = pos;
  316: 	return rlen;
  317: }
  318: #endif
  319: 
  320: /*
  321:  * io_etherRecv() - Receive packet from bpf
  322:  *
  323:  * @eth = bpf handle
  324:  * @buf = buffer
  325:  * @buflen = buffer length
  326:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  327:  * return: -1 error or !=-1 readed bytes
  328:  */
  329: ssize_t
  330: io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf)
  331: {
  332: 	ssize_t rlen = 0;
  333: 	struct bpf_hdr *h;
  334: 
  335: 	if (!buf || !buflen) {
  336: 		io_SetErr(EINVAL, "invalid arguments");
  337: 		return -1;
  338: 	}
  339: 
  340: 	if (!zcbuf) {
  341: 		rlen = read(eth, buf, buflen);
  342: 		if (rlen == -1) {
  343: 			LOGERR;
  344: 			return -1;
  345: 		}
  346: 	} else {
  347: #ifdef __FreeBSD__
  348: 		rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  349: 		if (!rlen)
  350: 			rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  351: #else
  352: 		rlen = -1;
  353: 		io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
  354: #endif
  355: 	}
  356: 
  357: 	h = (struct bpf_hdr*) buf;
  358: 	rlen -= h->bh_hdrlen;
  359: 
  360: 	if (h->bh_caplen != rlen) {
  361: 		if (h->bh_caplen < rlen)
  362: 			rlen = h->bh_caplen;
  363: 		else {
  364: 			io_SetErr(EIO, "Captured %d bytes should be at most %d bytes", 
  365: 					h->bh_caplen, rlen);
  366: 			return -1;
  367: 		}
  368: 	}
  369: 
  370: 	memmove(buf, buf + h->bh_hdrlen, rlen);
  371: 	return rlen;
  372: }
  373: 
  374: /*
  375:  * io_etherFilter() - BPF filter routine
  376:  *
  377:  * @eth = bpf handle
  378:  * @io = filter direction 
  379:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
  380:  * @insn = BPF filter instruction array
  381:  * @insnlen = Length of BPF filter instruction array
  382:  * return: -1 error or 0 ok
  383:  */
  384: int
  385: io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen)
  386: {
  387: 	int ret = 0;
  388: 	struct bpf_program fcode = { 0 };
  389: 
  390: 	if (io != IO_ETHER_FILTER_PROMISC && (!insn || !insnlen)) {
  391: 		io_SetErr(EINVAL, "invalid arguments");
  392: 		return -1;
  393: 	}
  394: 
  395: 	switch (io) {
  396: 		case IO_ETHER_FILTER_PROMISC:	/* promiscuous mode */
  397: 			ret = ioctl(eth, BIOCPROMISC, NULL);
  398: 			break;
  399: 		case IO_ETHER_FILTER_NOTREAD:	/* read not filter */
  400: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  401: 			fcode.bf_insns = insn;
  402: 			ret = ioctl(eth, BIOCSETFNR, &fcode);
  403: 			break;
  404: 		case IO_ETHER_FILTER_READ:	/* read filter */
  405: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  406: 			fcode.bf_insns = insn;
  407: 			ret = ioctl(eth, BIOCSETF, &fcode);
  408: 			break;
  409: 		case IO_ETHER_FILTER_WRITE:	/* write filter */
  410: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  411: 			fcode.bf_insns = insn;
  412: 			ret = ioctl(eth, BIOCSETWF, &fcode);
  413: 			break;
  414: 	}
  415: 
  416: 	if (ret == -1)
  417: 		LOGERR;
  418: 	return ret;
  419: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>