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