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
|
/*
* copy diverted (or tee'd) packets to a file in 'tcpdump' format
* (ie. this uses the '-lpcap' routines).
*
* example usage:
* # ipfwpcap -r 8091 divt.log &
* # ipfw add 2864 divert 8091 ip from 128.432.53.82 to any
* # ipfw add 2864 divert 8091 ip from any to 128.432.53.82
*
* the resulting dump file can be read with ...
* # tcpdump -nX -r divt.log
*/
/*
* Written by P Kern { pkern [AT] cns.utoronto.ca }
*
* Copyright (c) 2004 University of Toronto. All rights reserved.
* Anyone may use or copy this software except that this copyright
* notice remain intact and that credit is given where it is due.
* The University of Toronto and the author make no warranty and
* accept no liability for this software.
*
* From: Header: /local/src/local.lib/SRC/ipfwpcap/RCS/ipfwpcap.c,v 1.4 2004/01/15 16:19:07 pkern Exp
*
* $FreeBSD$
*/
#include <stdio.h>
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h> /* for MAXPATHLEN */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h> /* for IP_MAXPACKET */
#include <netinet/ip.h> /* for IP_MAXPACKET */
/* XXX normally defined in config.h */
#define HAVE_STRLCPY 1
#define HAVE_SNPRINTF 1
#define HAVE_VSNPRINTF 1
#include <pcap-int.h> /* see pcap(3) and /usr/src/contrib/libpcap/. */
#ifdef IP_MAXPACKET
#define BUFMAX IP_MAXPACKET
#else
#define BUFMAX 65535
#endif
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
static int debug = 0;
static int reflect = 0; /* 1 == write packet back to socket. */
static ssize_t totbytes = 0, maxbytes = 0;
static ssize_t totpkts = 0, maxpkts = 0;
char *prog = NULL;
char pidfile[MAXPATHLEN] = { '\0' };
/*
* tidy up.
*/
void
quit(sig)
int sig;
{
(void) unlink(pidfile);
exit(sig);
}
/*
* do the "paper work"
* - save my own pid in /var/run/$0.{port#}.pid
*/
okay(pn)
int pn;
{
FILE *fp;
int fd, numlen, n;
char *p, numbuf[80];
numlen = sizeof(numbuf);
bzero(numbuf, numlen);
snprintf(numbuf, numlen-1, "%ld\n", getpid());
numlen = strlen(numbuf);
if (pidfile[0] == '\0') {
p = (char *)rindex(prog, '/');
p = (p == NULL) ? prog : p+1 ;
snprintf(pidfile, sizeof(pidfile)-1,
"%s%s.%d.pid", _PATH_VARRUN, p, pn);
}
fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644);
if (fd < 0) { perror(pidfile); exit(21); }
siginterrupt(SIGTERM, 1);
siginterrupt(SIGHUP, 1);
signal (SIGTERM, quit);
signal (SIGHUP, quit);
n = write(fd, numbuf, numlen);
if (n < 0) { perror(pidfile); quit(23); }
(void) close(fd);
}
usage()
{
fprintf(stderr, "\
\n\
usage:\n\
%s [-dr] [-b maxbytes] [-p maxpkts] [-P pidfile] portnum dumpfile\n\
\n\
where:\n\
'-d' = enable debugging messages.\n\
'-r' = reflect. write packets back to the divert socket.\n\
(ie. simulate the original intent of \"ipfw tee\").\n\
'-rr' = indicate that it is okay to quit if packet-count or\n\
byte-count limits are reached (see the NOTE below\n\
about what this implies).\n\
'-b bytcnt' = stop dumping after {bytcnt} bytes.\n\
'-p pktcnt' = stop dumping after {pktcnt} packets.\n\
'-P pidfile' = alternate file to store the PID\n\
(default: /var/run/%s.{portnum}.pid).\n\
\n\
portnum = divert(4) socket port number.\n\
dumpfile = file to write captured packets (tcpdump format).\n\
(specify '-' to write packets to stdout).\n\
\n\
", prog, prog);
fprintf(stderr, "\
The '-r' option should not be necessary, but because \"ipfw tee\" is broken\n\
(see BUGS in ipfw(8) for details) this feature can be used along with\n\
an \"ipfw divert\" rule to simulate the original intent of \"ipfw tee\".\n\
\n\
NOTE: With an \"ipfw divert\" rule, diverted packets will silently\n\
disappear if there is nothing listening to the divert socket.\n\
\n\
");
exit(-1);
}
main(ac, av)
int ac;
char *av[];
{
int r, sd, portnum, l;
struct sockaddr_in sin;
int errflg = 0;
int nfd;
fd_set rds;
ssize_t nr;
char *dumpf, buf[BUFMAX];
pcap_t *p;
pcap_dumper_t *dp;
struct pcap_pkthdr phd;
prog = av[0];
while ((r = getopt(ac, av, "drb:p:P:")) != -1) {
switch (r) {
case 'd':
debug++;
break;
case 'r':
reflect++;
break;
case 'b':
maxbytes = (ssize_t) atol(optarg);
break;
case 'p':
maxpkts = (ssize_t) atoi(optarg);
break;
case 'P':
strcpy(pidfile, optarg);
break;
case '?':
default:
errflg++;
break;
}
}
if ((ac - optind) != 2 || errflg)
usage();
portnum = atoi(av[optind++]);
dumpf = av[optind];
if (debug) fprintf(stderr, "bind to %d.\ndump to '%s'.\n", portnum, dumpf);
if ((r = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT)) == -1) {
perror("socket(DIVERT)");
exit(2);
}
sd = r;
sin.sin_port = htons(portnum);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("bind(divert)");
exit(3);
}
p = pcap_open_dead(DLT_RAW, BUFMAX);
dp = pcap_dump_open(p, dumpf);
if (dp == NULL) {
pcap_perror(p, dumpf);
exit(4);
}
okay(portnum);
nfd = sd + 1;
for (;;) {
FD_ZERO(&rds);
FD_SET(sd, &rds);
r = select(nfd, &rds, NULL, NULL, NULL);
if (r == -1) {
if (errno == EINTR) continue;
perror("select");
quit(11);
}
if (!FD_ISSET(sd, &rds))
/* hmm. no work. */
continue;
/*
* use recvfrom(3 and sendto(3) as in natd(8).
* see /usr/src/sbin/natd/natd.c
* see ipfw(8) about using 'divert' and 'tee'.
*/
/*
* read packet.
*/
l = sizeof(sin);
nr = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &l);
if (debug) fprintf(stderr, "recvfrom(%d) = %d (%d)\n", sd, nr, l);
if (nr < 0 && errno != EINTR) {
perror("recvfrom(sd)");
quit(12);
}
if (nr <= 0) continue;
if (reflect) {
/*
* write packet back so it can continue
* being processed by any further IPFW rules.
*/
l = sizeof(sin);
r = sendto(sd, buf, nr, 0, (struct sockaddr *)&sin, l);
if (debug) fprintf(stderr, " sendto(%d) = %d\n", sd, r);
if (r < 0) { perror("sendto(sd)"); quit(13); }
}
/*
* check maximums, if any.
* but don't quit if must continue reflecting packets.
*/
if (maxpkts) {
totpkts++;
if (totpkts > maxpkts) {
if (reflect == 1) continue;
quit(0);
}
}
if (maxbytes) {
totbytes += nr;
if (totbytes > maxbytes) {
if (reflect == 1) continue;
quit(0);
}
}
/*
* save packet in tcpdump(1) format. see pcap(3).
* divert packets are fully assembled. see ipfw(8).
*/
(void) gettimeofday(&(phd.ts), NULL);
phd.caplen = phd.len = nr;
pcap_dump((u_char *)dp, &phd, buf);
if (ferror((FILE *)dp)) { perror(dumpf); quit(14); }
(void) fflush((FILE *)dp);
}
quit(0);
}
|