File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / bpf.c
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:06:31 2016 UTC (7 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

    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.8 2016/08/18 09:06:31 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 - 2016
   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_etherClose() - Close BPF interface
   51:  *
   52:  * @eth = bpf handle
   53:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
   54:  * return: none
   55:  */
   56: void
   57: io_etherClose(int eth, void **zcbuf)
   58: {
   59: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
   60: 	struct bpf_zbuf *zbuf = NULL;
   61: #endif
   62: 
   63: 	if (eth > STDERR_FILENO)
   64: 		close(eth);
   65: 
   66: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
   67: 	if (zcbuf && *zcbuf) {
   68: 		zbuf = *zcbuf;
   69: 		munmap(zbuf->bz_bufb, zbuf->bz_buflen);
   70: 		munmap(zbuf->bz_bufa, zbuf->bz_buflen);
   71: 		e_free(*zcbuf);
   72: 		*zcbuf = NULL;
   73: 	}
   74: #endif
   75: }
   76: 
   77: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
   78: static inline struct bpf_zbuf *
   79: allocZCbuf(u_int len)
   80: {
   81: 	struct bpf_zbuf *zbuf = NULL;
   82: 
   83: 	zbuf = e_malloc(sizeof(struct bpf_zbuf));
   84: 	if (!zbuf) {
   85: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
   86: 		return NULL;
   87: 	} else
   88: 		memset(zbuf, 0, sizeof(struct bpf_zbuf));
   89: 
   90: 	zbuf->bz_buflen = roundup(len, getpagesize());
   91: 	zbuf->bz_bufa = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
   92: 	if (zbuf->bz_bufa == MAP_FAILED) {
   93: 		LOGERR;
   94: 		e_free(zbuf);
   95: 		return NULL;
   96: 	} else
   97: 		memset(zbuf->bz_bufa, 0, zbuf->bz_buflen);
   98: 	zbuf->bz_bufb = mmap(NULL, zbuf->bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
   99: 	if (zbuf->bz_bufb == MAP_FAILED) {
  100: 		LOGERR;
  101: 		munmap(zbuf->bz_bufa, zbuf->bz_buflen);
  102: 		e_free(zbuf);
  103: 		return NULL;
  104: 	} else
  105: 		memset(zbuf->bz_bufb, 0, zbuf->bz_buflen);
  106: 
  107: 	return zbuf;
  108: }
  109: #endif
  110: 
  111: /*
  112:  * io_etherOpen() - Open BPF interface to device
  113:  *
  114:  * @csIface = interface name
  115:  * @flags = open flags
  116:  * @whdr = with complete headers
  117:  * @wdlt = with data link type, on Linux is protocol number
  118:  * @buflen = buffer length
  119:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  120:  * return: -1 error or >-1 bpf handle
  121:  */
  122: int
  123: io_etherOpen(const char *csIface, int flags, u_int whdr, u_int wdlt, 
  124: 		u_int *buflen, void **zcbuf)
  125: {
  126: 	int eth = -1;
  127: 	char szStr[STRSIZ];
  128: 	struct ifreq ifr;
  129: #ifndef __linux__
  130: 	register int i;
  131: 	u_int n = 1;
  132: #else
  133: 	sockaddr_t sa = {{ 0 }};
  134: #endif
  135: 
  136: #if !defined(__FreeBSD__) || !defined(ZCBUF_ENABLE)
  137: 	if (zcbuf) {
  138: 		io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
  139: 		return -1;
  140: 	}
  141: #endif
  142: 
  143: #ifndef __linux__
  144: 	for (i = 0; i < BPF_DEV_MAX; i++) {
  145: 		memset(szStr, 0, sizeof szStr);
  146: 		snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
  147: 		eth = open(szStr, flags);
  148: 		if (eth > STDERR_FILENO)
  149: 			break;
  150: 	}
  151: #else
  152: 	eth = socket(AF_PACKET, whdr ? SOCK_RAW : SOCK_DGRAM, htons((u_short) wdlt));
  153: #endif
  154: 	if (eth < 3) {
  155: 		LOGERR;
  156: 		return -1;
  157: 	}
  158: 
  159: 	if (csIface)
  160: 		strlcpy(szStr, csIface, sizeof szStr);
  161: 	else if (e_get1stiface(szStr, sizeof szStr) == -1) {
  162: 		close(eth);
  163: 		return -1;
  164: 	}
  165: 
  166: #ifndef __linux__
  167: 	n = 1;
  168: 	if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) {
  169: 		LOGERR;
  170: 		close(eth);
  171: 		return -1;
  172: 	}
  173: 	if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) {
  174: 		LOGERR;
  175: 		close(eth);
  176: 		return -1;
  177: 	}
  178: 
  179: 	if (!zcbuf) {
  180: 		if (ioctl(eth, BIOCGBLEN, &n) == -1) {
  181: 			LOGERR;
  182: 			close(eth);
  183: 			return -1;
  184: 		} else
  185: 			n = (buflen && *buflen) ? *buflen : getpagesize();
  186: 
  187: 		if (ioctl(eth, BIOCSBLEN, &n) == -1) {
  188: 			LOGERR;
  189: 			close(eth);
  190: 			return -1;
  191: 		}
  192: 		if (buflen)
  193: 			*buflen = n;
  194: 	} else {
  195: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  196: 		n = BPF_BUFMODE_ZBUF;
  197: 		if (ioctl(eth, BIOCSETBUFMODE, &n) == -1) {
  198: 			LOGERR;
  199: 			close(eth);
  200: 			return -1;
  201: 		}
  202: 		if (ioctl(eth, BIOCGETZMAX, &n) == -1) {
  203: 			LOGERR;
  204: 			close(eth);
  205: 			return -1;
  206: 		} else
  207: 			n = (buflen && *buflen) ? *buflen : n;
  208: 		if (!(*zcbuf = allocZCbuf(n))) {
  209: 			close(eth);
  210: 			return -1;
  211: 		}
  212: 		if (ioctl(eth, BIOCSETZBUF, *zcbuf) == -1) {
  213: 			LOGERR;
  214: 			io_etherClose(eth, zcbuf);
  215: 			return -1;
  216: 		}
  217: 		if (buflen && *buflen)
  218: 			*buflen = n;
  219: #endif
  220: 	}
  221: 
  222: 	memset(&ifr, 0, sizeof ifr);
  223: 	strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
  224: 	if (ioctl(eth, BIOCSETIF, &ifr) == -1) {
  225: 		LOGERR;
  226: 		io_etherClose(eth, zcbuf);
  227: 		return -1;
  228: 	}
  229: 
  230: 	n = wdlt;
  231: 	if (wdlt && ioctl(eth, BIOCSDLT, &n) == -1) {
  232: 		LOGERR;
  233: 		close(eth);
  234: 		return -1;
  235: 	}
  236: #else
  237: 	memset(&ifr, 0, sizeof ifr);
  238: 	strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name);
  239: 	if (ioctl(eth, SIOCGIFINDEX, &ifr)) {
  240: 		LOGERR;
  241: 		close(eth);
  242: 		return -1;
  243: 	}
  244: 
  245: 	sa.sll.sll_family = AF_PACKET;
  246: 	sa.sll.sll_ifindex = ifr.ifr_ifindex;
  247: 	sa.sll.sll_protocol = htons((u_short) wdlt);
  248: 	if (bind(eth, &sa.sa, sizeof(sa.sll)) == -1) {
  249: 		LOGERR;
  250: 		close(eth);
  251: 		return -1;
  252: 	}
  253: #endif
  254: 
  255: 	return eth;
  256: }
  257: 
  258: /*
  259:  * io_etherSend() - Send packet to bpf
  260:  *
  261:  * @eth = bpf handle
  262:  * @buf = buffer
  263:  * @buflen = buffer length
  264:  * return: -1 error or !=-1 written bytes
  265:  */
  266: ssize_t
  267: io_etherSend(int eth, const void *buf, size_t buflen)
  268: {
  269: 	ssize_t wlen = 0;
  270: 
  271: 	if (!buf || !buflen) {
  272: 		io_SetErr(EINVAL, "invalid arguments");
  273: 		return -1;
  274: 	}
  275: 
  276: 	wlen = write(eth, buf, buflen);
  277: 	if (wlen == -1)
  278: 		LOGERR;
  279: 	return wlen;
  280: }
  281: 
  282: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  283: static inline void
  284: ackZCbuf(struct bpf_zbuf_header *bzh)
  285: {
  286: 	atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen);
  287: }
  288: 
  289: static inline int
  290: chkZCbuf(struct bpf_zbuf_header *bzh)
  291: {
  292: 	/* return true if userspace owns buffer, and false otherwise. */
  293: 	return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen));
  294: }
  295: 
  296: static ssize_t
  297: nextZCbuf(int eth, struct bpf_zbuf * __restrict zbuf, void * __restrict buf, size_t buflen)
  298: {
  299: 	ssize_t rlen = 0;
  300: 	struct bpf_zbuf bz;
  301: 	struct bpf_zbuf_header *bzh;
  302: 	off_t pos = 0;
  303: 
  304: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa;
  305: 	if (chkZCbuf(bzh)) {
  306: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  307: 		memcpy(buf + pos, zbuf->bz_bufa + sizeof(struct bpf_zbuf_header), rlen);
  308: 		ackZCbuf(bzh);
  309: 		pos += rlen;
  310: 	}
  311: 	bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb;
  312: 	if (chkZCbuf(bzh)) {
  313: 		rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen);
  314: 		memcpy(buf + pos, zbuf->bz_bufb + sizeof(struct bpf_zbuf_header), rlen);
  315: 		ackZCbuf(bzh);
  316: 		pos += rlen;
  317: 	}
  318: 
  319: 	if (!pos) {
  320: 		if ((rlen = ioctl(eth, BIOCROTZBUF, &bz)) == -1)
  321: 			LOGERR;
  322: 	} else
  323: 		rlen = pos;
  324: 	return rlen;
  325: }
  326: #endif
  327: 
  328: /*
  329:  * io_etherRecv() - Receive packet from bpf
  330:  *
  331:  * @eth = bpf handle
  332:  * @buf = buffer
  333:  * @buflen = buffer length
  334:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  335:  * return: -1 error or !=-1 readed bytes
  336:  */
  337: ssize_t
  338: io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf)
  339: {
  340: 	ssize_t rlen = 0;
  341: #ifndef __linux__
  342: 	struct bpf_hdr *h;
  343: #endif
  344: 
  345: 	if (!buf || !buflen) {
  346: 		io_SetErr(EINVAL, "invalid arguments");
  347: 		return -1;
  348: 	}
  349: 
  350: 	if (!zcbuf) {
  351: 		rlen = read(eth, buf, buflen);
  352: 		if (rlen == -1) {
  353: 			LOGERR;
  354: 			return -1;
  355: 		}
  356: 	} else {
  357: #if defined(__FreeBSD__) && defined(ZCBUF_ENABLE)
  358: 		rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  359: 		if (!rlen)
  360: 			rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);
  361: #else
  362: 		rlen = -1;
  363: 		io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");
  364: #endif
  365: 	}
  366: 
  367: #ifndef __linux__
  368: 	h = (struct bpf_hdr*) buf;
  369: 	rlen -= h->bh_hdrlen;
  370: 
  371: 	if (h->bh_caplen != rlen) {
  372: 		if (h->bh_caplen < rlen)
  373: 			rlen = h->bh_caplen;
  374: 		else {
  375: 			io_SetErr(EIO, "Captured %d bytes should be at most %d bytes", 
  376: 					h->bh_caplen, rlen);
  377: 			return -1;
  378: 		}
  379: 	}
  380: 
  381: 	memmove(buf, buf + h->bh_hdrlen, rlen);
  382: #endif
  383: 	return rlen;
  384: }
  385: 
  386: #ifndef __linux__
  387: /*
  388:  * io_etherFilter() - BPF filter routine
  389:  *
  390:  * @eth = bpf handle
  391:  * @io = filter direction 
  392:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
  393:  * @insn = BPF filter instruction array
  394:  * @insnlen = Length of BPF filter instruction array
  395:  * return: -1 error or 0 ok
  396:  */
  397: int
  398: io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen)
  399: {
  400: 	int ret = 0;
  401: 	struct bpf_program fcode = { 0 };
  402: 
  403: 	if (io != IO_ETHER_FILTER_PROMISC && (!insn || !insnlen)) {
  404: 		io_SetErr(EINVAL, "invalid arguments");
  405: 		return -1;
  406: 	}
  407: 
  408: 	switch (io) {
  409: 		case IO_ETHER_FILTER_PROMISC:	/* promiscuous mode */
  410: 			ret = ioctl(eth, BIOCPROMISC, NULL);
  411: 			break;
  412: 		case IO_ETHER_FILTER_NOTREAD:	/* read not filter */
  413: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  414: 			fcode.bf_insns = insn;
  415: 			ret = ioctl(eth, BIOCSETFNR, &fcode);
  416: 			break;
  417: 		case IO_ETHER_FILTER_READ:	/* read filter */
  418: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  419: 			fcode.bf_insns = insn;
  420: 			ret = ioctl(eth, BIOCSETF, &fcode);
  421: 			break;
  422: 		case IO_ETHER_FILTER_WRITE:	/* write filter */
  423: 			fcode.bf_len = insnlen / sizeof(struct bpf_insn);
  424: 			fcode.bf_insns = insn;
  425: 			ret = ioctl(eth, BIOCSETWF, &fcode);
  426: 			break;
  427: 	}
  428: 
  429: 	if (ret == -1)
  430: 		LOGERR;
  431: 	return ret;
  432: }
  433: #endif

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