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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/* $OpenBSD: pf-snit.c,v 1.6 2003/12/01 00:56:51 avsm Exp $ */
/*
* Copyright (c) 1993-96 Mats O Jansson. All rights reserved.
*
* 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-snit.c,v 1.6 2003/12/01 00:56:51 avsm Exp $";
#endif
#include <stdio.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>
#define DEV_NIT "/dev/nit"
#include <net/nit.h>
#include <net/nit_if.h>
#include <net/nit_pf.h>
#include <net/nit_buf.h>
#include <net/packetfilt.h>
#include <stropts.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netdb.h>
#include <ctype.h>
#include <syslog.h>
#include "common/mopdef.h"
/*
* Variables
*/
/* struct ifreq ifr; */
extern int errno;
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.
*/
int
pfTrans(interface)
char *interface;
{
return TRANS_ETHER+TRANS_8023+TRANS_AND;
}
/*
* Open and initialize packet filter.
*/
int
pfInit(interface, mode, protocol, trans)
char *interface;
u_short protocol;
int trans, mode;
{
int fd;
int ioarg;
char device[64];
unsigned long if_flags;
struct ifreq ifr;
struct strioctl si;
/* get clone */
if ((fd = open(DEV_NIT, mode)) < 0) {
syslog(LOG_ERR,"pfInit: open nit %m");
return(-1);
}
/*
* set filter for protocol
*/
if (setup_pf(fd, protocol, trans) < 0)
return(-1);
/*
* set options, bind to underlying interface
*/
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
/* bind */
si.ic_cmd = NIOCBIND; /* bind to underlying interface */
si.ic_timout = 10;
si.ic_len = sizeof(ifr);
si.ic_dp = (caddr_t)𝔦
if (ioctl(fd, I_STR, (caddr_t)&si) < 0) {
syslog(LOG_ERR,"pfinit: I_STR %m");
return(-1);
}
if (promisc) {
if_flags = NI_PROMISC;
si.ic_cmd = NIOCSFLAGS;
si.ic_timout = 10;
si.ic_len = sizeof(if_flags);
si.ic_dp = (caddr_t)&if_flags;
if (ioctl(fd, I_STR, (caddr_t)&si) < 0) {
syslog(LOG_ERR,"pfInit: I_STR (promisc) %m");
return(-1);
}
}
/* set up messages */
if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { /* want messages */
syslog(LOG_ERR,"pfInit: I_SRDOPT %m");
return(-1);
}
/* flush read queue */
if (ioctl(fd, I_FLUSH, (char *)FLUSHR) < 0) {
syslog(LOG_ERR,"pfInit: I_FLUSH %m");
return(-1);
}
return(fd);
}
/*
* establish protocol filter
*/
int
setup_pf(s, prot, trans)
int s, trans;
u_short prot;
{
int ioarg;
u_short offset;
struct packetfilt pf;
u_short *fwp = pf.Pf_Filter;
struct strioctl si;
#define s_offset(structp, element) (&(((structp)0)->element))
bzero(&pf, sizeof(pf));
pf.Pf_Priority = 128;
offset = ((int)s_offset(struct ether_header *, ether_type))/sizeof(u_short);
*fwp++ = ENF_PUSHWORD + offset; /* Check Ethernet type II */
*fwp++ = ENF_PUSHLIT | ENF_EQ; /* protocol prot */
*fwp++ = htons(prot);
*fwp++ = ENF_PUSHWORD + offset + 4; /* Check 802.3 protocol prot */
*fwp++ = ENF_PUSHLIT | ENF_EQ;
*fwp++ = htons(prot);
*fwp++ = ENF_PUSHWORD + offset + 1; /* Check for SSAP and DSAP */
*fwp++ = ENF_PUSHLIT | ENF_EQ;
*fwp++ = htons(0xaaaa);
*fwp++ = ENF_AND;
*fwp++ = ENF_OR;
pf.Pf_FilterLen = 11;
si.ic_cmd = NIOCSETF;
si.ic_timout = 10;
si.ic_len = sizeof(pf);
si.ic_dp = (char *)&pf;
if (ioctl(s, I_PUSH, "pf") < 0) {
syslog(LOG_ERR,"setup_pf: I_PUSH %m");
return(-1);
}
if (ioctl(s, I_STR, (char *)&si) < 0) {
syslog(LOG_ERR,"setup_pf: I_STR %m");
return(-1);
}
return(0);
}
/*
* Get the interface ethernet address
*/
int
pfEthAddr(fd, addr)
int fd;
u_char *addr;
{
struct ifreq ifr;
struct sockaddr *sa;
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
syslog(LOG_ERR,"pfEthAddr: SIOCGIFADDR %m");
return(-1);
}
sa = (struct sockaddr *)ifr.ifr_data;
bcopy((char *)sa->sa_data, (char *)addr, 6);
return(0);
}
/*
* Add a Multicast address to the interface
*/
int
pfAddMulti(s, interface, addr)
int s;
char *interface, *addr;
{
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, interface, sizeof (ifr.ifr_name) -1);
ifr.ifr_name[sizeof(ifr.ifr_name)] = 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, (caddr_t)&ifr) < 0) {
syslog(LOG_ERR,"pfAddMulti: SIOCADDMULTI %m");
close(fd);
return(-1);
}
close(fd);
return(0);
}
/*
* delete a multicast address from the interface
*/
int
pfDelMulti(s, interface, addr)
int s;
char *interface, *addr;
{
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, interface, sizeof (ifr.ifr_name) -1);
ifr.ifr_name[sizeof(ifr.ifr_name)] = 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, (caddr_t)&ifr) < 0) {
syslog(LOG_ERR,"pfDelMulti: SIOCDELMULTI %m");
close(fd);
return(-1);
}
close(fd);
return(0);
}
/*
* read a packet
*/
int
pfRead(fd, buf, len)
int fd, len;
u_char *buf;
{
return(read(fd, buf, len));
}
/*
* write a packet
*/
int
pfWrite(fd, buf, len, trans)
int fd, len, trans;
u_char *buf;
{
struct sockaddr sa;
struct strbuf pbuf, dbuf;
sa.sa_family = AF_UNSPEC;
bcopy(buf, sa.sa_data, sizeof(sa.sa_data));
switch (trans) {
default:
pbuf.len = sizeof(struct sockaddr);
pbuf.buf = (char *) &sa;
dbuf.len = len-14;
dbuf.buf = (char *)buf+14;
break;
}
if (putmsg(fd, &pbuf, &dbuf, 0) == 0)
return(len);
return(-1);
}
|