File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / bpf.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Sat Feb 8 22:06:17 2014 UTC (10 years, 5 months ago) by misho
Branches: MAIN
CVS tags: io7_2, io7_1, io7_0, IO7_1, IO7_0, IO6_9, HEAD
version 6.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.6 2014/02/08 22:06:17 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 - 2014
   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_getmaciface() - Get MAC address from interface name
   72:  *
   73:  * @csIface = interface name
   74:  * @ea = ethernet address
   75:  * return: -1 error, 0 ok or 1 not found
   76:  */
   77: int
   78: io_getmaciface(const char *csIface, ether_addr_t * __restrict ea)
   79: {
   80: 	struct ifaddrs *ifa, *ifp;
   81: 	struct sockaddr_dl *dl;
   82: 	int ret = 1;
   83: 
   84: 	if (!csIface || !ea)
   85: 		return -1;
   86: 	else
   87: 		memset(ea, 0, sizeof(ether_addr_t));
   88: 
   89: 	getifaddrs(&ifa);
   90: 	for (ifp = ifa; ifp; ifp = ifp->ifa_next)
   91: 		if (!strcmp(csIface, ifp->ifa_name) && ifp->ifa_addr && 
   92: 				ifp->ifa_addr->sa_family == AF_LINK) {
   93: 			dl = (struct sockaddr_dl*) ifp->ifa_addr;
   94: 			if ((dl->sdl_type == IFT_ETHER || dl->sdl_type == IFT_L2VLAN || 
   95: 					dl->sdl_type == IFT_BRIDGE) && 
   96: 					dl->sdl_alen == ETHER_ADDR_LEN) {
   97: 				memcpy(ea, LLADDR(dl), sizeof(ether_addr_t));
   98: 				ret = 0;
   99: 				break;
  100: 			}
  101: 		}
  102: 	freeifaddrs(ifa);
  103: 
  104: 	return ret;
  105: }
  106: 
  107: /*
  108:  * io_etherClose() - Close BPF interface
  109:  *
  110:  * @eth = bpf handle
  111:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  112:  * return: none
  113:  */
  114: void
  115: io_etherClose(int eth, void **zcbuf)
  116: {
  117: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  118: 	struct bpf_zbuf *zbuf = NULL;
  119: #endif
  120: 
  121: 	if (eth > STDERR_FILENO)
  122: 		close(eth);
  123: 
  124: 	if (zcbuf && *zcbuf) {
  125: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  126: 		zbuf = *zcbuf;
  127: 		munmap(zbuf->bz_bufb, zbuf->bz_buflen);
  128: 		munmap(zbuf->bz_bufa, zbuf->bz_buflen);
  129: 		e_free(*zcbuf);
  130: 		*zcbuf = NULL;
  131: #endif
  132: 	}
  133: }
  134: 
  135: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  136: static inline struct bpf_zbuf *
  137: allocZCbuf(u_int len)
  138: {
  139: 	struct bpf_zbuf *zbuf = NULL;
  140: 
  141: 	zbuf = e_malloc(sizeof(struct bpf_zbuf));
  142: 	if (!zbuf) {
  143: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  144: 		return NULL;
  145: 	} else
  146: 		memset(zbuf, 0, sizeof(struct bpf_zbuf));
  147: 
  148: 	zbuf->bz_buflen = roundup(len, getpagesize());
  149: 	zbuf->bz_bufa = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
  150: 	if (zbuf->bz_bufa == MAP_FAILED) {
  151: 		LOGERR;
  152: 		e_free(zbuf);
  153: 		return NULL;
  154: 	} else
  155: 		memset(zbuf->bz_bufa, 0, zbuf->bz_buflen);
  156: 	zbuf->bz_bufb = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
  157: 	if (zbuf->bz_bufb == MAP_FAILED) {
  158: 		LOGERR;
  159: 		munmap(zbuf->bz_bufa, zbuf->bz_buflen);
  160: 		e_free(zbuf);
  161: 		return NULL;
  162: 	} else
  163: 		memset(zbuf->bz_bufb, 0, zbuf->bz_buflen);
  164: 
  165: 	return zbuf;
  166: }
  167: #endif
  168: 
  169: /*
  170:  * io_etherOpen() - Open BPF interface to device
  171:  *
  172:  * @csIface = interface name
  173:  * @flags = open flags
  174:  * @whdr = with complete headers
  175:  * @wdlt = with data link type
  176:  * @buflen = buffer length
  177:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  178:  * return: -1 error or >-1 bpf handle
  179:  */
  180: int
  181: io_etherOpen(const char *csIface, int flags, u_int whdr, u_int wdlt, 
  182: 		u_int *buflen, void **zcbuf)
  183: {
  184: 	int eth = -1;
  185: 	register int i;
  186: 	char szStr[STRSIZ];
  187: 	struct ifreq ifr;
  188: 	u_int n = 1;
  189: 
  190: #if !defined(__FreeBSD__) || !defined(ZCBUF_ENABLE)
  191: 	if (zcbuf) {
  192: 		io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
  193: 		return -1;
  194: 	}
  195: #endif
  196: 
  197: 	for (i = 0; i < BPF_DEV_MAX; i++) {
  198: 		memset(szStr, 0, sizeof szStr);
  199: 		snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
  200: 		eth = open(szStr, flags);
  201: 		if (eth > STDERR_FILENO)
  202: 			break;
  203: 	}
  204: 	if (eth < 3) {
  205: 		LOGERR;
  206: 		return -1;
  207: 	}
  208: 
  209: 	if (csIface)
  210: 		strlcpy(szStr, csIface, sizeof szStr);
  211: 	else if (io_get1stiface(szStr, sizeof szStr) == -1) {
  212: 		close(eth);
  213: 		return -1;
  214: 	}
  215: 
  216: 	n = 1;
  217: 	if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) {
  218: 		LOGERR;
  219: 		close(eth);
  220: 		return -1;
  221: 	}
  222: 	if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
  223: 		LOGERR;
  224: 		close(eth);
  225: 		return -1;
  226: 	}
  227: 
  228: 	if (!zcbuf) {
  229: 		if (ioctl(eth, BIOCGBLEN, &n) == -1) {
  230: 			LOGERR;
  231: 			close(eth);
  232: 			return -1;
  233: 		} else
  234: 			n = (buflen && *buflen) ? *buflen : getpagesize();
  235: 
  236: 		if (ioctl(eth, BIOCSBLEN, &n) == -1) {
  237: 			LOGERR;
  238: 			close(eth);
  239: 			return -1;
  240: 		}
  241: 		if (buflen)
  242: 			*buflen = n;
  243: 	} else {
  244: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  245: 		n = BPF_BUFMODE_ZBUF;
  246: 		if (ioctl(eth, BIOCSETBUFMODE, &n) == -1) {
  247: 			LOGERR;
  248: 			close(eth);
  249: 			return -1;
  250: 		}
  251: 		if (ioctl(eth, BIOCGETZMAX, &n) == -1) {
  252: 			LOGERR;
  253: 			close(eth);
  254: 			return -1;
  255: 		} else
  256: 			n = (buflen && *buflen) ? *buflen : n;
  257: 		if (!(*zcbuf = allocZCbuf(n))) {
  258: 			close(eth);
  259: 			return -1;
  260: 		}
  261: 		if (ioctl(eth, BIOCSETZBUF, *zcbuf) == -1) {
  262: 			LOGERR;
  263: 			io_etherClose(eth, zcbuf);
  264: 			return -1;
  265: 		}
  266: 		if (buflen && *buflen)
  267: 			*buflen = n;
  268: #endif
  269: 	}
  270: 
  271: 	memset(&ifr, 0, sizeof ifr);
  272: 	strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
  273: 	if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
  274: 		LOGERR;
  275: 		io_etherClose(eth, zcbuf);
  276: 		return -1;
  277: 	}
  278: 
  279: 	n = wdlt;
  280: 	if (wdlt && ioctl(eth, BIOCSDLT, &n) == -1) {
  281: 		LOGERR;
  282: 		close(eth);
  283: 		return -1;
  284: 	}
  285: 
  286: 	return eth;
  287: }
  288: 
  289: /*
  290:  * io_etherSend() - Send packet to bpf
  291:  *
  292:  * @eth = bpf handle
  293:  * @buf = buffer
  294:  * @buflen = buffer length
  295:  * return: -1 error or !=-1 written bytes
  296:  */
  297: ssize_t
  298: io_etherSend(int eth, const void *buf, size_t buflen)
  299: {
  300: 	ssize_t wlen = 0;
  301: 
  302: 	if (!buf || !buflen) {
  303: 		io_SetErr(EINVAL, "invalid arguments");
  304: 		return -1;
  305: 	}
  306: 
  307: 	wlen = write(eth, buf, buflen);
  308: 	if (wlen == -1)
  309: 		LOGERR;
  310: 	return wlen;
  311: }
  312: 
  313: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  314: static inline void
  315: ackZCbuf(struct bpf_zbuf_header *bzh)
  316: {
  317: 	atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen);
  318: }
  319: 
  320: static inline int
  321: chkZCbuf(struct bpf_zbuf_header *bzh)
  322: {
  323: 	/* return true if userspace owns buffer, and false otherwise. */
  324: 	return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen));
  325: }
  326: 
  327: static ssize_t
  328: nextZCbuf(int eth, struct bpf_zbuf * __restrict zbuf, void * __restrict buf, size_t buflen)
  329: {
  330: 	ssize_t rlen = 0;
  331: 	struct bpf_zbuf bz;
  332: 	struct bpf_zbuf_header *bzh;
  333: 	off_t pos = 0;
  334: 
  335: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa;
  336: 	if (chkZCbuf(bzh)) {
  337: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  338: 		memcpy(buf + pos, zbuf->bz_bufa + sizeof(struct bpf_zbuf_header), rlen);
  339: 		ackZCbuf(bzh);
  340: 		pos += rlen;
  341: 	}
  342: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb;
  343: 	if (chkZCbuf(bzh)) {
  344: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  345: 		memcpy(buf + pos, zbuf->bz_bufb + sizeof(struct bpf_zbuf_header), rlen);
  346: 		ackZCbuf(bzh);
  347: 		pos += rlen;
  348: 	}
  349: 
  350: 	if (!pos) {
  351: 		if ((rlen = ioctl(eth, BIOCROTZBUF, &bz)) == -1)
  352: 			LOGERR;
  353: 	} else
  354: 		rlen = pos;
  355: 	return rlen;
  356: }
  357: #endif
  358: 
  359: /*
  360:  * io_etherRecv() - Receive packet from bpf
  361:  *
  362:  * @eth = bpf handle
  363:  * @buf = buffer
  364:  * @buflen = buffer length
  365:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  366:  * return: -1 error or !=-1 readed bytes
  367:  */
  368: ssize_t
  369: io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf)
  370: {
  371: 	ssize_t rlen = 0;
  372: 	struct bpf_hdr *h;
  373: 
  374: 	if (!buf || !buflen) {
  375: 		io_SetErr(EINVAL, "invalid arguments");
  376: 		return -1;
  377: 	}
  378: 
  379: 	if (!zcbuf) {
  380: 		rlen = read(eth, buf, buflen);
  381: 		if (rlen == -1) {
  382: 			LOGERR;
  383: 			return -1;
  384: 		}
  385: 	} else {
  386: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  387: 		rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  388: 		if (!rlen)
  389: 			rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  390: #else
  391: 		rlen = -1;
  392: 		io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
  393: #endif
  394: 	}
  395: 
  396: 	h = (struct bpf_hdr*) buf;
  397: 	rlen -= h->bh_hdrlen;
  398: 
  399: 	if (h->bh_caplen != rlen) {
  400: 		if (h->bh_caplen < rlen)
  401: 			rlen = h->bh_caplen;
  402: 		else {
  403: 			io_SetErr(EIO, "Captured %d bytes should be at most %d bytes", 
  404: 					h->bh_caplen, rlen);
  405: 			return -1;
  406: 		}
  407: 	}
  408: 
  409: 	memmove(buf, buf + h->bh_hdrlen, rlen);
  410: 	return rlen;
  411: }
  412: 
  413: /*
  414:  * io_etherFilter() - BPF filter routine
  415:  *
  416:  * @eth = bpf handle
  417:  * @io = filter direction 
  418:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
  419:  * @insn = BPF filter instruction array
  420:  * @insnlen = Length of BPF filter instruction array
  421:  * return: -1 error or 0 ok
  422:  */
  423: int
  424: io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen)
  425: {
  426: 	int ret = 0;
  427: 	struct bpf_program fcode = { 0 };
  428: 
  429: 	if (io != IO_ETHER_FILTER_PROMISC && (!insn || !insnlen)) {
  430: 		io_SetErr(EINVAL, "invalid arguments");
  431: 		return -1;
  432: 	}
  433: 
  434: 	switch (io) {
  435: 		case IO_ETHER_FILTER_PROMISC:	/* promiscuous mode */
  436: 			ret = ioctl(eth, BIOCPROMISC, NULL);
  437: 			break;
  438: 		case IO_ETHER_FILTER_NOTREAD:	/* read not filter */
  439: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  440: 			fcode.bf_insns = insn;
  441: 			ret = ioctl(eth, BIOCSETFNR, &fcode);
  442: 			break;
  443: 		case IO_ETHER_FILTER_READ:	/* read filter */
  444: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  445: 			fcode.bf_insns = insn;
  446: 			ret = ioctl(eth, BIOCSETF, &fcode);
  447: 			break;
  448: 		case IO_ETHER_FILTER_WRITE:	/* write filter */
  449: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  450: 			fcode.bf_insns = insn;
  451: 			ret = ioctl(eth, BIOCSETWF, &fcode);
  452: 			break;
  453: 	}
  454: 
  455: 	if (ret == -1)
  456: 		LOGERR;
  457: 	return ret;
  458: }

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