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