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