2009-11-17 21:58:07 +03:00
|
|
|
/* $NetBSD: pf.c,v 1.12 2009/11/17 18:58:07 drochner Exp $ */
|
1997-03-25 06:06:58 +03:00
|
|
|
|
1997-03-17 01:23:34 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
|
|
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is partly derived from rarpd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2009-10-20 04:51:13 +04:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
1997-03-17 01:23:34 +03:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-17 03:24:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2009-11-17 21:58:07 +03:00
|
|
|
__RCSID("$NetBSD: pf.c,v 1.12 2009/11/17 18:58:07 drochner Exp $");
|
1997-03-17 01:23:34 +03:00
|
|
|
#endif
|
|
|
|
|
1997-10-17 03:24:50 +04:00
|
|
|
#include "os.h"
|
1997-03-17 01:23:34 +03:00
|
|
|
|
2004-12-02 02:15:08 +03:00
|
|
|
#include <paths.h>
|
1997-10-17 03:24:50 +04:00
|
|
|
#include <sys/uio.h>
|
1997-03-17 01:23:34 +03:00
|
|
|
#include <net/bpf.h>
|
|
|
|
|
1997-10-17 03:24:50 +04:00
|
|
|
#include "mopdef.h"
|
|
|
|
#include "pf.h"
|
2003-04-20 04:17:22 +04:00
|
|
|
#include "log.h"
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern int promisc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return information to device.c how to open device.
|
|
|
|
* In this case the driver can handle both Ethernet type II and
|
|
|
|
* IEEE 802.3 frames (SNAP) in a single pfOpen.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfTrans(const char *interface)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
return TRANS_ETHER+TRANS_8023+TRANS_AND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open and initialize packet filter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfInit(const char *interface, int mode, u_short protocol, int typ)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct ifreq ifr;
|
|
|
|
u_int dlt;
|
|
|
|
int immediate;
|
2004-04-10 21:53:05 +04:00
|
|
|
u_int bufsize;
|
2004-12-02 02:15:08 +03:00
|
|
|
const char *device = _PATH_BPF;
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
static struct bpf_insn insns[] = {
|
|
|
|
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0),
|
|
|
|
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3),
|
|
|
|
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14),
|
|
|
|
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1),
|
|
|
|
BPF_STMT(BPF_RET | BPF_K, 1520),
|
|
|
|
BPF_STMT(BPF_RET | BPF_K, 0),
|
|
|
|
};
|
|
|
|
static struct bpf_program filter = {
|
|
|
|
sizeof insns / sizeof(insns[0]),
|
|
|
|
insns
|
|
|
|
};
|
|
|
|
|
2004-12-02 02:15:08 +03:00
|
|
|
fd = open(device, mode);
|
1997-03-17 01:23:34 +03:00
|
|
|
if (fd < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: open %s", device);
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set immediate mode so packets are processed as they arrive. */
|
|
|
|
immediate = 1;
|
|
|
|
if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: BIOCIMMEDIATE");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
2004-04-10 21:53:05 +04:00
|
|
|
bufsize = 32768;
|
|
|
|
if (ioctl(fd, BIOCSBLEN, &bufsize) < 0) {
|
|
|
|
mopLogWarn("pfInit: BIOCSBLEN(%d)", bufsize);
|
|
|
|
}
|
1997-03-17 01:23:34 +03:00
|
|
|
(void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name);
|
|
|
|
if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: BIOCSETIF");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
/* Check that the data link layer is an Ethernet; this code won't work
|
|
|
|
* with anything else. */
|
|
|
|
if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: BIOCGDLT");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (dlt != DLT_EN10MB) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarnX("pfInit: %s is not ethernet", device);
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (promisc) {
|
|
|
|
/* Set promiscuous mode. */
|
|
|
|
if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: BIOCPROMISC");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set filter program. */
|
|
|
|
insns[1].k = protocol;
|
|
|
|
insns[3].k = protocol;
|
|
|
|
|
|
|
|
if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfInit: BIOCSETF");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a Multicast address to the interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfAddMulti(int s, const char *interface, const char *addr)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2002-08-22 11:18:42 +04:00
|
|
|
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
ifr.ifr_addr.sa_family = AF_UNSPEC;
|
1997-10-17 03:24:50 +04:00
|
|
|
memmove(ifr.ifr_addr.sa_data, addr, 6);
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* open a socket, temporarily, to use for SIOC* ioctls
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfAddMulti: socket");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfAddMulti: SIOCADDMULTI");
|
1997-03-17 01:23:34 +03:00
|
|
|
close(fd);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete a Multicast address from the interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfDelMulti(int s, const char *interface, const char *addr)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2002-08-22 11:18:42 +04:00
|
|
|
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
ifr.ifr_addr.sa_family = AF_UNSPEC;
|
1997-10-17 03:24:50 +04:00
|
|
|
memmove(ifr.ifr_addr.sa_data, addr, 6);
|
1997-03-17 01:23:34 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* open a socket, temporarily, to use for SIOC* ioctls
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfDelMulti: socket");
|
1997-03-17 01:23:34 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) {
|
2003-04-20 04:17:22 +04:00
|
|
|
mopLogWarn("pfAddMulti: SIOCDELMULTI");
|
1997-03-17 01:23:34 +03:00
|
|
|
close(fd);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read a packet
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfRead(int fd, u_char *buf, int len)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
return(read(fd, buf, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a packet
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-11-17 21:58:07 +03:00
|
|
|
pfWrite(int fd, const u_char *buf, int len, int trans)
|
1997-03-17 01:23:34 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
struct iovec iov[2];
|
|
|
|
|
|
|
|
switch (trans) {
|
|
|
|
case TRANS_8023:
|
2009-11-17 21:58:07 +03:00
|
|
|
iov[0].iov_base = (caddr_t)__UNCONST(buf);
|
1997-03-17 01:23:34 +03:00
|
|
|
iov[0].iov_len = 22;
|
2009-11-17 21:58:07 +03:00
|
|
|
iov[1].iov_base = (caddr_t)__UNCONST(buf+22);
|
1997-03-17 01:23:34 +03:00
|
|
|
iov[1].iov_len = len-22;
|
|
|
|
break;
|
|
|
|
default:
|
2009-11-17 21:58:07 +03:00
|
|
|
iov[0].iov_base = (caddr_t)__UNCONST(buf);
|
1997-03-17 01:23:34 +03:00
|
|
|
iov[0].iov_len = 14;
|
2009-11-17 21:58:07 +03:00
|
|
|
iov[1].iov_base = (caddr_t)__UNCONST(buf+14);
|
1997-03-17 01:23:34 +03:00
|
|
|
iov[1].iov_len = len-14;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writev(fd, iov, 2) == len)
|
|
|
|
return(len);
|
|
|
|
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|