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