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