Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / mopd / common / pf.c
blobd3b940cd06906ffbc89150d3eedc032b47bc4365
1 /* $NetBSD: pf.c,v 1.11 2009/10/20 00:51:13 snj Exp $ */
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.
8 * This code is partly derived from rarpd.
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.
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.
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
38 #include "os.h"
40 #include <paths.h>
41 #include <sys/uio.h>
42 #include <net/bpf.h>
44 #include "mopdef.h"
45 #include "pf.h"
46 #include "log.h"
49 * Variables
52 extern int promisc;
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.
60 int
61 pfTrans(const char *interface)
63 return TRANS_ETHER+TRANS_8023+TRANS_AND;
67 * Open and initialize packet filter.
70 int
71 pfInit(const char *interface, int mode, u_short protocol, int typ)
73 int fd;
74 struct ifreq ifr;
75 u_int dlt;
76 int immediate;
77 u_int bufsize;
78 const char *device = _PATH_BPF;
80 static struct bpf_insn insns[] = {
81 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
82 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0),
83 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
84 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3),
85 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14),
86 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1),
87 BPF_STMT(BPF_RET | BPF_K, 1520),
88 BPF_STMT(BPF_RET | BPF_K, 0),
90 static struct bpf_program filter = {
91 sizeof insns / sizeof(insns[0]),
92 insns
95 fd = open(device, mode);
96 if (fd < 0) {
97 mopLogWarn("pfInit: open %s", device);
98 return(-1);
101 /* Set immediate mode so packets are processed as they arrive. */
102 immediate = 1;
103 if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
104 mopLogWarn("pfInit: BIOCIMMEDIATE");
105 return(-1);
107 bufsize = 32768;
108 if (ioctl(fd, BIOCSBLEN, &bufsize) < 0) {
109 mopLogWarn("pfInit: BIOCSBLEN(%d)", bufsize);
111 (void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name);
112 if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
113 mopLogWarn("pfInit: BIOCSETIF");
114 return(-1);
116 /* Check that the data link layer is an Ethernet; this code won't work
117 * with anything else. */
118 if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
119 mopLogWarn("pfInit: BIOCGDLT");
120 return(-1);
122 if (dlt != DLT_EN10MB) {
123 mopLogWarnX("pfInit: %s is not ethernet", device);
124 return(-1);
126 if (promisc) {
127 /* Set promiscuous mode. */
128 if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) {
129 mopLogWarn("pfInit: BIOCPROMISC");
130 return(-1);
133 /* Set filter program. */
134 insns[1].k = protocol;
135 insns[3].k = protocol;
137 if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
138 mopLogWarn("pfInit: BIOCSETF");
139 return(-1);
141 return(fd);
145 * Add a Multicast address to the interface
149 pfAddMulti(int s, const char *interface, const char *addr)
151 struct ifreq ifr;
152 int fd;
154 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
156 ifr.ifr_addr.sa_family = AF_UNSPEC;
157 memmove(ifr.ifr_addr.sa_data, addr, 6);
160 * open a socket, temporarily, to use for SIOC* ioctls
163 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
164 mopLogWarn("pfAddMulti: socket");
165 return(-1);
167 if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) {
168 mopLogWarn("pfAddMulti: SIOCADDMULTI");
169 close(fd);
170 return(-1);
172 close(fd);
174 return(0);
178 * Delete a Multicast address from the interface
182 pfDelMulti(int s, const char *interface, const char *addr)
184 struct ifreq ifr;
185 int fd;
187 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
189 ifr.ifr_addr.sa_family = AF_UNSPEC;
190 memmove(ifr.ifr_addr.sa_data, addr, 6);
193 * open a socket, temporarily, to use for SIOC* ioctls
196 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
197 mopLogWarn("pfDelMulti: socket");
198 return(-1);
200 if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) {
201 mopLogWarn("pfAddMulti: SIOCDELMULTI");
202 close(fd);
203 return(-1);
205 close(fd);
207 return(0);
211 * read a packet
215 pfRead(int fd, u_char *buf, int len)
217 return(read(fd, buf, len));
221 * write a packet
225 pfWrite(int fd, const u_char *buf, int len, int trans)
228 struct iovec iov[2];
230 switch (trans) {
231 case TRANS_8023:
232 iov[0].iov_base = (caddr_t)__UNCONST(buf);
233 iov[0].iov_len = 22;
234 iov[1].iov_base = (caddr_t)__UNCONST(buf+22);
235 iov[1].iov_len = len-22;
236 break;
237 default:
238 iov[0].iov_base = (caddr_t)__UNCONST(buf);
239 iov[0].iov_len = 14;
240 iov[1].iov_base = (caddr_t)__UNCONST(buf+14);
241 iov[1].iov_len = len-14;
242 break;
245 if (writev(fd, iov, 2) == len)
246 return(len);
248 return(-1);