Home | History | Annotate | Line # | Download | only in common
pf.c revision 1.11
      1 /*	$NetBSD: pf.c,v 1.11 2009/10/20 00:51:13 snj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is partly derived from rarpd.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: pf.c,v 1.11 2009/10/20 00:51:13 snj Exp $");
     36 #endif
     37 
     38 #include "os.h"
     39 
     40 #include <paths.h>
     41 #include <sys/uio.h>
     42 #include <net/bpf.h>
     43 
     44 #include "mopdef.h"
     45 #include "pf.h"
     46 #include "log.h"
     47 
     48 /*
     49  * Variables
     50  */
     51 
     52 extern int promisc;
     53 
     54 /*
     55  * Return information to device.c how to open device.
     56  * In this case the driver can handle both Ethernet type II and
     57  * IEEE 802.3 frames (SNAP) in a single pfOpen.
     58  */
     59 
     60 int
     61 pfTrans(interface)
     62 	char *interface;
     63 {
     64 	return TRANS_ETHER+TRANS_8023+TRANS_AND;
     65 }
     66 
     67 /*
     68  * Open and initialize packet filter.
     69  */
     70 
     71 int
     72 pfInit(interface, mode, protocol, typ)
     73 	char *interface;
     74 	u_short protocol;
     75 	int typ, mode;
     76 {
     77 	int	fd;
     78 	struct ifreq ifr;
     79 	u_int	dlt;
     80 	int	immediate;
     81 	u_int	bufsize;
     82 	const char *device = _PATH_BPF;
     83 
     84 	static struct bpf_insn insns[] = {
     85 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
     86 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0),
     87 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
     88 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3),
     89 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14),
     90 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1),
     91 		BPF_STMT(BPF_RET | BPF_K, 1520),
     92 		BPF_STMT(BPF_RET | BPF_K, 0),
     93 	};
     94 	static struct bpf_program filter = {
     95 		sizeof insns / sizeof(insns[0]),
     96 		insns
     97 	};
     98 
     99 	fd = open(device, mode);
    100 	if (fd < 0) {
    101       		mopLogWarn("pfInit: open %s", device);
    102 		return(-1);
    103 	}
    104 
    105 	/* Set immediate mode so packets are processed as they arrive. */
    106 	immediate = 1;
    107 	if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
    108       		mopLogWarn("pfInit: BIOCIMMEDIATE");
    109 		return(-1);
    110 	}
    111 	bufsize = 32768;
    112 	if (ioctl(fd, BIOCSBLEN, &bufsize) < 0) {
    113       		mopLogWarn("pfInit: BIOCSBLEN(%d)", bufsize);
    114 	}
    115 	(void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name);
    116 	if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
    117       		mopLogWarn("pfInit: BIOCSETIF");
    118 		return(-1);
    119 	}
    120 	/* Check that the data link layer is an Ethernet; this code won't work
    121 	 * with anything else. */
    122 	if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
    123       		mopLogWarn("pfInit: BIOCGDLT");
    124 		return(-1);
    125 	}
    126 	if (dlt != DLT_EN10MB) {
    127       		mopLogWarnX("pfInit: %s is not ethernet", device);
    128 		return(-1);
    129 	}
    130 	if (promisc) {
    131 		/* Set promiscuous mode. */
    132 		if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) {
    133       			mopLogWarn("pfInit: BIOCPROMISC");
    134 			return(-1);
    135 		}
    136 	}
    137 	/* Set filter program. */
    138 	insns[1].k = protocol;
    139 	insns[3].k = protocol;
    140 
    141 	if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
    142       		mopLogWarn("pfInit: BIOCSETF");
    143 		return(-1);
    144 	}
    145 	return(fd);
    146 }
    147 
    148 /*
    149  * Add a Multicast address to the interface
    150  */
    151 
    152 int
    153 pfAddMulti(s, interface, addr)
    154 	int s;
    155 	char *interface, *addr;
    156 {
    157 	struct ifreq ifr;
    158 	int	fd;
    159 
    160 	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    161 
    162 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    163 	memmove(ifr.ifr_addr.sa_data, addr, 6);
    164 
    165 	/*
    166 	 * open a socket, temporarily, to use for SIOC* ioctls
    167 	 *
    168 	 */
    169 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    170 		mopLogWarn("pfAddMulti: socket");
    171 		return(-1);
    172 	}
    173 	if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) {
    174 		mopLogWarn("pfAddMulti: SIOCADDMULTI");
    175 		close(fd);
    176 		return(-1);
    177 	}
    178 	close(fd);
    179 
    180 	return(0);
    181 }
    182 
    183 /*
    184  * Delete a Multicast address from the interface
    185  */
    186 
    187 int
    188 pfDelMulti(s, interface, addr)
    189 	int s;
    190 	char *interface, *addr;
    191 {
    192 	struct ifreq ifr;
    193 	int	fd;
    194 
    195 	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    196 
    197 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    198 	memmove(ifr.ifr_addr.sa_data, addr, 6);
    199 
    200 	/*
    201 	 * open a socket, temporarily, to use for SIOC* ioctls
    202 	 *
    203 	 */
    204 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    205 		mopLogWarn("pfDelMulti: socket");
    206 		return(-1);
    207 	}
    208 	if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) {
    209 		mopLogWarn("pfAddMulti: SIOCDELMULTI");
    210 		close(fd);
    211 		return(-1);
    212 	}
    213 	close(fd);
    214 
    215 	return(0);
    216 }
    217 
    218 /*
    219  * read a packet
    220  */
    221 
    222 int
    223 pfRead(fd, buf, len)
    224 	int	fd, len;
    225 	u_char *buf;
    226 {
    227 	return(read(fd, buf, len));
    228 }
    229 
    230 /*
    231  * write a packet
    232  */
    233 
    234 int
    235 pfWrite(fd, buf, len, trans)
    236 	int fd, len, trans;
    237 	u_char *buf;
    238 {
    239 
    240 	struct iovec iov[2];
    241 
    242 	switch (trans) {
    243 	case TRANS_8023:
    244 		iov[0].iov_base = (caddr_t)buf;
    245 		iov[0].iov_len = 22;
    246 		iov[1].iov_base = (caddr_t)buf+22;
    247 		iov[1].iov_len = len-22;
    248 		break;
    249 	default:
    250 		iov[0].iov_base = (caddr_t)buf;
    251 		iov[0].iov_len = 14;
    252 		iov[1].iov_base = (caddr_t)buf+14;
    253 		iov[1].iov_len = len-14;
    254 		break;
    255 	}
    256 
    257 	if (writev(fd, iov, 2) == len)
    258 		return(len);
    259 
    260 	return(-1);
    261 }
    262 
    263