xref: /netbsd-src/usr.sbin/mopd/common/pf.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: pf.c,v 1.3 1997/03/25 03:07:27 thorpej 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 #ifndef LINT
37 static char rcsid[] = "$NetBSD: pf.c,v 1.3 1997/03/25 03:07:27 thorpej Exp $";
38 #endif
39 
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/ioctl.h>
45 #include <sys/file.h>
46 #include <sys/socket.h>
47 #include <sys/uio.h>
48 #include <net/if.h>
49 
50 #include <net/bpf.h>
51 #include <sys/errno.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 
56 #include <netdb.h>
57 #include <ctype.h>
58 #include <strings.h>
59 
60 #include <syslog.h>
61 #include <varargs.h>
62 
63 #include "common/mopdef.h"
64 
65 /*
66  * Variables
67  */
68 
69 extern int errno;
70 extern int promisc;
71 
72 /*
73  * Return information to device.c how to open device.
74  * In this case the driver can handle both Ethernet type II and
75  * IEEE 802.3 frames (SNAP) in a single pfOpen.
76  */
77 
78 int
79 pfTrans(interface)
80 	char *interface;
81 {
82 	return TRANS_ETHER+TRANS_8023+TRANS_AND;
83 }
84 
85 /*
86  * Open and initialize packet filter.
87  */
88 
89 int
90 pfInit(interface, mode, protocol, typ)
91 	char *interface;
92 	u_short protocol;
93 	int typ, mode;
94 {
95 	int	fd;
96 	int	n = 0;
97 	char	device[sizeof "/dev/bpf000"];
98 	struct ifreq ifr;
99 	u_int	dlt;
100 	int	immediate;
101 
102 	static struct bpf_insn insns[] = {
103 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
104 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 4, 0),
105 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
106 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x4711, 0, 3),
107 		BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 14),
108 		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xaaaa, 0, 1),
109 		BPF_STMT(BPF_RET | BPF_K, 1520),
110 		BPF_STMT(BPF_RET | BPF_K, 0),
111 	};
112 	static struct bpf_program filter = {
113 		sizeof insns / sizeof(insns[0]),
114 		insns
115 	};
116 
117   	/* Go through all the minors and find one that isn't in use. */
118 	do {
119 		(void) sprintf(device, "/dev/bpf%d", n++);
120 		fd = open(device, mode);
121 	} while (fd < 0 && errno == EBUSY);
122 
123 	if (fd < 0) {
124       		syslog(LOG_ERR,"pfInit: open bpf %m");
125 		return(-1);
126 	}
127 
128 	/* Set immediate mode so packets are processed as they arrive. */
129 	immediate = 1;
130 	if (ioctl(fd, BIOCIMMEDIATE, &immediate) < 0) {
131       		syslog(LOG_ERR,"pfInit: BIOCIMMEDIATE: %m");
132 		return(-1);
133 	}
134 	(void) strncpy(ifr.ifr_name, interface, sizeof ifr.ifr_name);
135 	if (ioctl(fd, BIOCSETIF, (caddr_t) & ifr) < 0) {
136       		syslog(LOG_ERR,"pfInit: BIOCSETIF: %m");
137 		return(-1);
138 	}
139 	/* Check that the data link layer is an Ethernet; this code won't work
140 	 * with anything else. */
141 	if (ioctl(fd, BIOCGDLT, (caddr_t) & dlt) < 0) {
142       		syslog(LOG_ERR,"pfInit: BIOCGDLT: %m");
143 		return(-1);
144 	}
145 	if (dlt != DLT_EN10MB) {
146       		syslog(LOG_ERR,"pfInit: %s is not ethernet", device);
147 		return(-1);
148 	}
149 	if (promisc) {
150 		/* Set promiscuous mode. */
151 		if (ioctl(fd, BIOCPROMISC, (caddr_t)0) < 0) {
152       			syslog(LOG_ERR,"pfInit: BIOCPROMISC: %m");
153 			return(-1);
154 		}
155 	}
156 	/* Set filter program. */
157 	insns[1].k = protocol;
158 	insns[3].k = protocol;
159 
160 	if (ioctl(fd, BIOCSETF, (caddr_t) & filter) < 0) {
161       		syslog(LOG_ERR,"pfInit: BIOCSETF: %m");
162 		return(-1);
163 	}
164 	return(fd);
165 }
166 
167 /*
168  * Add a Multicast address to the interface
169  */
170 
171 int
172 pfAddMulti(s, interface, addr)
173 	int s;
174 	char *interface, *addr;
175 {
176 	struct ifreq ifr;
177 	int	fd;
178 
179 	strcpy(ifr.ifr_name, interface);
180 
181 	ifr.ifr_addr.sa_family = AF_UNSPEC;
182 	bcopy(addr, ifr.ifr_addr.sa_data, 6);
183 
184 	/*
185 	 * open a socket, temporarily, to use for SIOC* ioctls
186 	 *
187 	 */
188 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
189 		syslog(LOG_ERR, "pfAddMulti: socket: %m");
190 		return(-1);
191 	}
192 	if (ioctl(fd, SIOCADDMULTI, (caddr_t)&ifr) < 0) {
193 		syslog(LOG_ERR, "pfAddMulti: SIOCADDMULTI: %m");
194 		close(fd);
195 		return(-1);
196 	}
197 	close(fd);
198 
199 	return(0);
200 }
201 
202 /*
203  * Delete a Multicast address from the interface
204  */
205 
206 int
207 pfDelMulti(s, interface, addr)
208 	int s;
209 	char *interface, *addr;
210 {
211 	struct ifreq ifr;
212 	int	fd;
213 
214 	strcpy(ifr.ifr_name, interface);
215 
216 	ifr.ifr_addr.sa_family = AF_UNSPEC;
217 	bcopy(addr, ifr.ifr_addr.sa_data, 6);
218 
219 	/*
220 	 * open a socket, temporarily, to use for SIOC* ioctls
221 	 *
222 	 */
223 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
224 		syslog(LOG_ERR, "pfDelMulti: socket: %m");
225 		return(-1);
226 	}
227 	if (ioctl(fd, SIOCDELMULTI, (caddr_t)&ifr) < 0) {
228 		syslog(LOG_ERR, "pfAddMulti: SIOCDELMULTI: %m");
229 		close(fd);
230 		return(-1);
231 	}
232 	close(fd);
233 
234 	return(0);
235 }
236 
237 /*
238  * read a packet
239  */
240 
241 int
242 pfRead(fd, buf, len)
243 	int	fd, len;
244 	u_char *buf;
245 {
246 	return(read(fd, buf, len));
247 }
248 
249 /*
250  * write a packet
251  */
252 
253 int
254 pfWrite(fd, buf, len, trans)
255 	int fd, len, trans;
256 	u_char *buf;
257 {
258 
259 	struct iovec iov[2];
260 
261 	switch (trans) {
262 	case TRANS_8023:
263 		iov[0].iov_base = (caddr_t)buf;
264 		iov[0].iov_len = 22;
265 		iov[1].iov_base = (caddr_t)buf+22;
266 		iov[1].iov_len = len-22;
267 		break;
268 	default:
269 		iov[0].iov_base = (caddr_t)buf;
270 		iov[0].iov_len = 14;
271 		iov[1].iov_base = (caddr_t)buf+14;
272 		iov[1].iov_len = len-14;
273 		break;
274 	}
275 
276 	if (writev(fd, iov, 2) == len)
277 		return(len);
278 
279 	return(-1);
280 }
281 
282