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