-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncap_create.c
More file actions
1794 lines (1601 loc) · 41.3 KB
/
ncap_create.c
File metadata and controls
1794 lines (1601 loc) · 41.3 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ncap_create.c - wrapper around libpcap to provide higher level abstraction
*
* By: Paul Vixie, ISC, August 2007
*/
#ifndef lint
static const char rcsid[] = "$Id$";
static const char copyright[] =
"Copyright (c) 2007 by Internet Systems Consortium, Inc. (\"ISC\")";
#endif
/*
* Copyright (c) 2007 by Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Import. */
#include "ncap_port.h"
#include "ncap_port_net.h"
#include "ncap_pvt.h"
#include <sys/uio.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef DMALLOC
# include <dmalloc.h>
#endif
/* Typedefs. */
#ifdef HAVE_BPF_TIMEVAL
typedef struct bpf_timeval my_bpftimeval;
#else
typedef struct timeval my_bpftimeval;
#endif
/* Forward. */
static ncap_result_e add_if(ncap_t, const char *netif,
const char *bpf, int,
const int vlans[], int nvlan,
int *fdes);
static ncap_result_e drop_if(ncap_t, int fdes);
static ncap_result_e add_nf(ncap_t, int fdes, const char *);
static ncap_result_e drop_nf(ncap_t, int fdes);
static void deactivate_nf(ncap_nf_ptr nfp);
static ncap_result_e add_pf(ncap_t, FILE *fp, const char *);
static ncap_result_e drop_pf(ncap_t, FILE *fp);
static ncap_result_e add_dg(ncap_t, int fdes, const char *);
static ncap_result_e drop_dg(ncap_t, int fdes);
static ncap_result_e filter(ncap_t, const char *);
static ncap_result_e collect(ncap_t, int, ncap_callback_t, void *);
static void stop(ncap_t);
static struct ncap_msg cons(ncap_t, struct timespec,
unsigned, unsigned,
ncap_np_e, ncap_np_ct,
ncap_tp_e, ncap_tp_ct,
size_t, const u_char *);
static int match(ncap_t, ncap_msg_ct);
static ncap_result_e ncap_write(ncap_t, ncap_msg_ct, int fdes);
static ncap_result_e ncap_fwrite(ncap_t, ncap_msg_ct, FILE *fp);
static ncap_result_e ncap_send(ncap_t, ncap_msg_ct, int fdes, int flags);
static void destroy(ncap_t);
static void ncap_pcap_if(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void ncap_pcap_pf(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void ncap_pcap_dl(ncap_t, int, const char *,
const int *, int,
ncap_callback_t, void *,
const struct pcap_pkthdr *,
const u_char *);
static void ncap_pcap_np(ncap_t, const char *,
ncap_callback_t, void *,
my_bpftimeval, unsigned,
const u_char *, size_t);
static void ncap_pcap_tp(ncap_t, const char *,
ncap_callback_t, void *,
my_bpftimeval, ncap_np_e, ncap_np_t,
unsigned, const u_char *, size_t);
static int my_getc_refill(ncap_nf_ptr nfp);
static int ncap_read_dgmsg(ncap_dg_ptr, ncap_callback_t, void *);
static int ncap_read_nfmsg(ncap_nf_ptr, ncap_callback_t, void *);
static ssize_t import_mh1(const u_char msghdr[], size_t len,
ncap_msg_t, size_t *, size_t *, size_t *);
static ssize_t import_mh2(const u_char msghdr[], size_t len,
size_t netlen, size_t tplen, ncap_msg_t);
static size_t export_hdr(ncap_t, u_char filehdr[]);
static ssize_t export_msg(ncap_t, ncap_msg_ct msg, u_char msghdr[]);
static void free_filters(ncap_t);
static void free_filter(ncap_t, ncap_filter_ptr);
/* Constants. */
#ifndef IPV6_VERSION
# define IPV6_VERSION 0x60
#endif
#ifndef IPV6_VERSION_MASK
# define IPV6_VERSION_MASK 0xf0
#endif
#define MAX_VLAN 4095
#define TO_MS 50
#define GRANULARITY 10
#define SNAPLEN 65536
#define THOUSAND 1000
#define MILLION (THOUSAND*THOUSAND)
/* Macros. */
#define FAILURE(m) do { \
NCAP_SETERR(m); \
return (ncap_failure); \
} while (0)
#define FAILZERO(m) do { \
NCAP_SETERR(m); \
return (0); \
} while (0)
#define FAILVOID(m) do { \
NCAP_SETERR(m); \
return; \
} while (0)
#define MY_GETC(nfp) \
((nfp->fbcur < nfp->fblen) \
? nfp->fb[nfp->fbcur++] \
: my_getc_refill(nfp))
/* Private data. */
static struct ncap_filtermaker filtermakers[] = {
{ "dns", ncap_filter_dns },
{ "icmp", ncap_filter_icmp },
{ NULL, NULL }
};
/* Export. */
/* Create an NCAP object and return it or return NULL on failure.
*/
ncap_t
ncap_create(int maxmsg) {
ncap_t ncap;
ncap = malloc(sizeof *ncap);
if (ncap == NULL)
return (NULL);
memset(ncap, 0, sizeof *ncap);
ncap->errstr = strdup("");
if (ncap->errstr == NULL) {
free(ncap);
return (NULL);
}
ncap->pvt = malloc(sizeof *ncap->pvt);
if (ncap->pvt == NULL) {
free(ncap->errstr);
free(ncap);
return (NULL);
}
memset(ncap->pvt, 0, sizeof *ncap->pvt);
ncap->pvt->reasm_ip = reasm_ip_new ();
if (ncap->pvt->reasm_ip == NULL) {
free(ncap->pvt);
free(ncap->errstr);
free(ncap);
return (NULL);
}
reasm_ip_set_timeout(ncap->pvt->reasm_ip, 60);
ncap->pvt->maxmsg = maxmsg;
ISC_LIST_INIT(ncap->pvt->ifs);
ISC_LIST_INIT(ncap->pvt->nfs);
ISC_LIST_INIT(ncap->pvt->pfs);
ISC_LIST_INIT(ncap->pvt->dgs);
FD_ZERO(&ncap->pvt->fdset);
ncap->add_if = add_if;
ncap->drop_if = drop_if;
ncap->add_nf = add_nf;
ncap->drop_nf = drop_nf;
ncap->add_pf = add_pf;
ncap->drop_pf = drop_pf;
ncap->add_dg = add_dg;
ncap->drop_dg = drop_dg;
ncap->filter = filter;
ncap->collect = collect;
ncap->stop = stop;
ncap->cons = cons;
ncap->match = match;
ncap->write = ncap_write;
ncap->fwrite = ncap_fwrite;
ncap->send = ncap_send;
ncap->destroy = destroy;
return (ncap);
}
/* Methods. */
/* Add an interface to an NCAP object.
*/
static ncap_result_e
add_if(ncap_t ncap, const char *name,
const char *bpf, int promisc,
const int vlans[], int nvlan,
int *ret_fdes)
{
#ifdef __APPLE__
unsigned int ioarg = 1;
#endif
char errbuf[PCAP_ERRBUF_SIZE];
ncap_if_ptr ifp;
pcap_t *pcap;
char *dynbpf;
/* Get the pcap open and condition it. */
pcap = pcap_open_live(name, SNAPLEN, promisc, TO_MS, errbuf);
if (pcap == NULL)
FAILURE(errbuf);
if (pcap_setnonblock(pcap, true, errbuf) < 0) {
pcap_close(pcap);
FAILURE(errbuf);
}
/* If we've got filters, gang the BPF's out of them. */
dynbpf = NULL;
if (!ISC_LIST_EMPTY(ncap->pvt->filters)) {
ncap_filter_ptr filt;
if (bpf != NULL) {
pcap_close(pcap);
FAILURE("add_if(BPF) incompatible with filter()");
}
for (filt = ISC_LIST_HEAD(ncap->pvt->filters);
filt != NULL;
filt = ISC_LIST_NEXT(filt, link))
{
if (filt->bpf == NULL || *filt->bpf == '\0')
continue;
if (dynbpf == NULL) {
dynbpf = strdup(filt->bpf);
assert(dynbpf != NULL);
} else {
char *new;
ncap_asprintf(&new, "(%s) or (%s)",
dynbpf, filt->bpf);
assert(new != NULL);
free(dynbpf);
dynbpf = new;
}
}
bpf = dynbpf;
}
if (bpf != NULL) {
struct bpf_program bpfp;
char *bpft;
/* Ask for fragments in addition to the caller's prefs.
*/
if (ncap_asprintf(&bpft, "(%s) or (%s) or (%s)",
"ip[6:2] & 0x1fff != 0",
"ip6",
bpf) < 0)
{
pcap_close(pcap);
FAILURE("asprintf failed");
}
/* Ask for VLANs if nec'y. */
if (nvlan != 0) {
char *new;
if (ncap_asprintf(&new, "vlan and (%s)", bpft) < 0) {
free(bpft);
pcap_close(pcap);
FAILURE("asprintf failed");
}
free(bpft);
bpft = new;
}
if (pcap_compile(pcap, &bpfp, bpft, true, 0) < 0 ||
pcap_setfilter(pcap, &bpfp) < 0)
{
strcpy(errbuf, pcap_geterr(pcap));
pcap_close(pcap);
FAILURE(errbuf);
}
pcap_freecode(&bpfp);
free(bpft);
}
if (dynbpf != NULL)
free(dynbpf);
/* Save it. */
ifp = malloc(sizeof *ifp);
if (ifp == NULL) {
pcap_close(pcap);
FAILURE("malloc failed");
}
memset(ifp, 0, sizeof *ifp);
ISC_LINK_INIT(ifp, link);
ifp->ncap = ncap;
ifp->pcap = pcap;
ifp->dlt = pcap_datalink(pcap);
ifp->fdes = pcap_get_selectable_fd(pcap);
#ifdef __APPLE__
ioctl(ifp->fdes, BIOCIMMEDIATE, &ioarg);
#endif
if (nvlan != 0) {
ifp->vlans = malloc(nvlan * sizeof vlans[0]);
if (ifp->vlans == NULL) {
pcap_close(pcap);
free(ifp);
FAILURE("malloc failed");
}
memcpy(ifp->vlans, vlans, nvlan * sizeof vlans[0]);
ifp->nvlan = nvlan;
}
ncap_asprintf(&ifp->label, "pcap if %s", name);
if (ifp->label == NULL) {
pcap_close(pcap);
free(ifp->vlans);
free(ifp);
FAILURE("asprintf failed");
}
ISC_LIST_APPEND(ncap->pvt->ifs, ifp, link);
/* Side effects. */
assert(!FD_ISSET(ifp->fdes, &ncap->pvt->fdset));
FD_SET(ifp->fdes, &ncap->pvt->fdset);
if (ret_fdes != NULL)
*ret_fdes = ifp->fdes;
if (ifp->fdes > ncap->pvt->highest_fd)
ncap->pvt->highest_fd = ifp->fdes;
return (ncap_success);
}
/* Drop an interface from an NCAP object.
*/
static ncap_result_e
drop_if(ncap_t ncap, int fdes) {
ncap_if_ptr ifp;
for (ifp = ISC_LIST_HEAD(ncap->pvt->ifs);
ifp != NULL;
ifp = ISC_LIST_NEXT(ifp, link))
if (pcap_get_selectable_fd(ifp->pcap) == fdes)
break;
if (ifp == NULL)
FAILURE("no matching fdes");
ISC_LIST_UNLINK(ncap->pvt->ifs, ifp, link);
assert(FD_ISSET(fdes, &ncap->pvt->fdset));
FD_CLR(fdes, &ncap->pvt->fdset);
if (ifp->vlans != NULL)
free(ifp->vlans);
free(ifp->label);
free(ifp);
return (ncap_success);
}
/* Add an NCAP input file to an NCAP object.
*/
static ncap_result_e
add_nf(ncap_t ncap, int fdes, const char *nf_label) {
ncap_nf_ptr nfp;
/* Check it. */
if (FD_ISSET(fdes, &ncap->pvt->fdset))
FAILURE("fdes already selected");
/* Save it. */
nfp = malloc(sizeof *nfp);
if (nfp == NULL)
FAILURE("malloc failed");
memset(nfp, 0, sizeof *nfp);
ISC_LINK_INIT(nfp, link);
ISC_LIST_APPEND(ncap->pvt->nfs, nfp, link);
nfp->ncap = ncap;
nfp->fdes = fdes;
ncap_asprintf(&nfp->label, "nf %s", nf_label);
if (nfp->label == NULL)
FAILURE("asprintf failed");
/* Side effects. */
FD_SET(nfp->fdes, &ncap->pvt->fdset);
if (nfp->fdes > ncap->pvt->highest_fd)
ncap->pvt->highest_fd = nfp->fdes;
fcntl(nfp->fdes, F_SETFL, fcntl(nfp->fdes, F_GETFL) | O_NONBLOCK);
return (ncap_success);
}
/* Drop an NCAP input file from an NCAP object.
*/
static ncap_result_e
drop_nf(ncap_t ncap, int fdes) {
ncap_nf_ptr nfp;
for (nfp = ISC_LIST_HEAD(ncap->pvt->nfs);
nfp != NULL;
nfp = ISC_LIST_NEXT(nfp, link))
if (nfp->fdes == fdes)
break;
if (nfp == NULL)
FAILURE("no matching nf");
ISC_LIST_UNLINK(ncap->pvt->nfs, nfp, link);
if (nfp->label != NULL)
deactivate_nf(nfp);
free(nfp);
return (ncap_success);
}
/* Remove an NCAP input file from consideration by select().
*/
static void
deactivate_nf(ncap_nf_ptr nfp) {
assert(FD_ISSET(nfp->fdes, &nfp->ncap->pvt->fdset));
FD_CLR(nfp->fdes, &nfp->ncap->pvt->fdset);
if (nfp->varbuf != NULL) {
free(nfp->varbuf);
nfp->varbuf = NULL;
nfp->varsiz = 0;
}
free(nfp->label);
nfp->label = NULL;
}
/* Add a PCAP input file to an NCAP object.
*/
static ncap_result_e
add_pf(ncap_t ncap, FILE *fp, const char *pf_label) {
char errbuf[PCAP_ERRBUF_SIZE];
ncap_pf_ptr pfp;
pcap_t *pcap;
pcap = pcap_fopen_offline(fp, errbuf);
if (pcap == NULL)
FAILURE(errbuf);
/* Save it. */
pfp = malloc(sizeof *pfp);
if (pfp == NULL) {
if (pcap != NULL)
pcap_close(pcap);
FAILURE("malloc failed");
}
ISC_LINK_INIT(pfp, link);
ISC_LIST_APPEND(ncap->pvt->pfs, pfp, link);
pfp->ncap = ncap;
pfp->fp = fp;
ncap_asprintf(&pfp->label, "pf %s", pf_label);
if (pfp->label == NULL) {
pcap_close(pcap);
FAILURE("asprintf failed");
}
pfp->pcap = pcap;
pfp->fdes = pcap_get_selectable_fd(pcap);
pfp->dlt = pcap_datalink(pcap);
/* Side effects. */
assert(!FD_ISSET(pfp->fdes, &ncap->pvt->fdset));
FD_SET(pfp->fdes, &ncap->pvt->fdset);
if (pfp->fdes > ncap->pvt->highest_fd)
ncap->pvt->highest_fd = pfp->fdes;
return (ncap_success);
}
/* Drop a PCAP input file from an NCAP object.
*/
static ncap_result_e
drop_pf(ncap_t ncap, FILE *fp) {
ncap_pf_ptr pfp;
for (pfp = ISC_LIST_HEAD(ncap->pvt->pfs);
pfp != NULL;
pfp = ISC_LIST_NEXT(pfp, link))
if (pfp->fp == fp)
break;
if (pfp == NULL)
FAILURE("no matching pfes");
ISC_LIST_UNLINK(ncap->pvt->pfs, pfp, link);
assert(FD_ISSET(pfp->fdes, &ncap->pvt->fdset));
FD_CLR(pfp->fdes, &ncap->pvt->fdset);
pcap_close(pfp->pcap);
free(pfp->label);
free(pfp);
return (ncap_success);
}
static ncap_result_e
add_dg(ncap_t ncap, int fdes, const char *dg_label) {
ncap_dg_ptr dgp;
/* Check it. */
if (FD_ISSET(fdes, &ncap->pvt->fdset))
FAILURE("fdes already selected");
/* Save it. */
dgp = malloc(sizeof *dgp);
if (dgp == NULL)
FAILURE("malloc failed");
ISC_LINK_INIT(dgp, link);
ISC_LIST_APPEND(ncap->pvt->dgs, dgp, link);
dgp->ncap = ncap;
dgp->fdes = fdes;
ncap_asprintf(&dgp->label, "dg %s", dg_label);
if (dgp->label == NULL) {
free(dgp);
FAILURE("asprintf failed");
}
/* Side effects. */
FD_SET(dgp->fdes, &ncap->pvt->fdset);
if (dgp->fdes > ncap->pvt->highest_fd)
ncap->pvt->highest_fd = dgp->fdes;
fcntl(dgp->fdes, F_SETFL, fcntl(dgp->fdes, F_GETFL) | O_NONBLOCK);
return (ncap_success);
}
static ncap_result_e
drop_dg(ncap_t ncap, int fdes) {
ncap_dg_ptr dgp;
for (dgp = ISC_LIST_HEAD(ncap->pvt->dgs);
dgp != NULL;
dgp = ISC_LIST_NEXT(dgp, link))
if (dgp->fdes == fdes)
break;
if (dgp == NULL)
FAILURE("no matching fdes");
ISC_LIST_UNLINK(ncap->pvt->dgs, dgp, link);
assert(FD_ISSET(fdes, &ncap->pvt->fdset));
FD_CLR(fdes, &ncap->pvt->fdset);
free(dgp->label);
free(dgp);
return (ncap_success);
}
/* Install an NCAP filter, specified in ASCII text.
*/
static ncap_result_e
filter(ncap_t ncap, const char *ospec) {
char *spec = strdup(ospec);
ncap_filtermaker_ptr fmp;
ncap_filter_ptr filt;
char *word;
free_filters(ncap);
filt = NULL;
fmp = NULL;
for (word = strtok(spec, "\040\t");
word != NULL;
word = strtok(NULL, "\040\t"))
{
char *arg;
arg = strchr(word, '=');
if (arg == NULL)
arg = strchr(word, '#');
if (arg == NULL) {
for (fmp = filtermakers; fmp->name != NULL; fmp++)
if (strcasecmp(word, fmp->name) == 0)
break;
if (fmp->name == NULL) {
NCAP_SETERR("filter family name not recognized");
goto failure;
}
filt = malloc(sizeof *filt);
assert(filt != NULL);
memset(filt, 0, sizeof *filt);
ISC_LINK_INIT(filt, link);
ISC_LIST_INIT(filt->rules);
ISC_LIST_APPEND(ncap->pvt->filters, filt, link);
filt->bpf = (*fmp->rulemaker)(ncap, NULL, '\0', NULL);
assert(filt->bpf != NULL);
} else {
char sep, *add;
if (fmp == NULL) {
NCAP_SETERR("filter term without a family");
goto failure;
}
sep = *arg;
*arg++ = '\0';
add = (*fmp->rulemaker)(ncap, word, sep, arg);
if (add == NULL)
goto failure;
if (add[0] != '\0') {
char *new;
ncap_asprintf(&new, "%s and (%s)",
filt->bpf, add);
assert(new != NULL);
free(filt->bpf);
filt->bpf = new;
}
free(add);
}
}
free(spec);
return (ncap_success);
failure:
if (filt != NULL)
free_filter(ncap, filt);
free(spec);
return (ncap_failure);
}
/* Collect and process packets from an NCAP's data sources.
*/
static ncap_result_e
collect(ncap_t ncap, int polling, ncap_callback_t callback, void *closure) {
ncap->pvt->flags &= ~(NCAP_STOPPING|NCAP_FAILURE);
do {
ncap_if_ptr ifp, ifpn;
ncap_nf_ptr nfp, nfpn;
ncap_pf_ptr pfp, pfpn;
ncap_dg_ptr dgp, dgpn;
fd_set readfds;
int n, i;
do {
static struct timeval tv_zero;
if (ISC_LIST_EMPTY(ncap->pvt->ifs) &&
ISC_LIST_EMPTY(ncap->pvt->nfs) &&
ISC_LIST_EMPTY(ncap->pvt->pfs) &&
ISC_LIST_EMPTY(ncap->pvt->dgs))
return (ncap_success);
memcpy(&readfds, &ncap->pvt->fdset, sizeof(fd_set));
n = select(ncap->pvt->highest_fd + 1,
&readfds, NULL, NULL,
polling ? &tv_zero : NULL);
if ((ncap->pvt->flags & NCAP_STOPPING) != 0)
return (ncap_success);
} while (n < 0 && errno == EINTR);
if (n < 0)
FAILURE("select failed");
for (ifp = ISC_LIST_HEAD(ncap->pvt->ifs);
ifp != NULL;
ifp = ifpn)
{
ifpn = ISC_LIST_NEXT(ifp, link);
if (FD_ISSET(ifp->fdes, &readfds)) {
struct ncap_if_closure ic;
ic.callback = callback;
ic.closure = closure;
ic.ifp = ifp;
n = pcap_dispatch(ifp->pcap, -1,
ncap_pcap_if,
(u_char *)&ic);
if (n == -1 && errno != EAGAIN) {
char tmp[99];
strcpy(tmp, "pcap_dispatch");
pcap_perror(ifp->pcap, tmp);
drop_if(ncap, ifp->fdes);
}
if ((ncap->pvt->flags & NCAP_STOPPING) != 0)
return (ncap_success);
if ((ncap->pvt->flags & NCAP_FAILURE) != 0)
return (ncap_failure);
}
}
for (nfp = ISC_LIST_HEAD(ncap->pvt->nfs);
nfp != NULL;
nfp = nfpn)
{
nfpn = ISC_LIST_NEXT(nfp, link);
if (FD_ISSET(nfp->fdes, &readfds)) {
for (i = 0; i < GRANULARITY; i++) {
n = ncap_read_nfmsg(nfp,
callback,
closure);
if (n == -1) {
drop_nf(ncap, nfp->fdes);
break;
} else if (n == -2) {
if (errno == EAGAIN)
break;
return (ncap_failure);
}
}
if ((ncap->pvt->flags & NCAP_STOPPING) != 0)
return (ncap_success);
if ((ncap->pvt->flags & NCAP_FAILURE) != 0)
return (ncap_failure);
}
}
for (pfp = ISC_LIST_HEAD(ncap->pvt->pfs);
pfp != NULL;
pfp = pfpn)
{
pfpn = ISC_LIST_NEXT(pfp, link);
if (FD_ISSET(pfp->fdes, &readfds)) {
struct ncap_pf_closure fc;
fc.callback = callback;
fc.closure = closure;
fc.pfp = pfp;
/* Do the whole file at once, since stdio
* and select() have an impedence mismatch.
*/
n = pcap_dispatch(pfp->pcap, -1,
ncap_pcap_pf,
(u_char *)&fc);
if (n == -1) {
char tmp[99];
strcpy(tmp, "pcap_dispatch");
pcap_perror(pfp->pcap, tmp);
}
drop_pf(ncap, pfp->fp);
if ((ncap->pvt->flags & NCAP_STOPPING) != 0)
return (ncap_success);
if ((ncap->pvt->flags & NCAP_FAILURE) != 0)
return (ncap_failure);
}
}
for (dgp = ISC_LIST_HEAD(ncap->pvt->dgs);
dgp != NULL;
dgp = dgpn)
{
dgpn = ISC_LIST_NEXT(dgp, link);
if (FD_ISSET(dgp->fdes, &readfds)) {
for (i = 0; i < GRANULARITY; i++) {
if (ncap_read_dgmsg(dgp, callback,
closure) == 0)
break;
if ((ncap->pvt->flags &
NCAP_STOPPING) != 0)
return (ncap_success);
if ((ncap->pvt->flags &
NCAP_FAILURE) != 0)
return (ncap_failure);
}
}
}
} while (!polling);
return (ncap_success);
}
static struct ncap_msg
cons(ncap_t ncap __attribute__((unused)), struct timespec ts,
unsigned user1, unsigned user2,
ncap_np_e np, ncap_np_ct npu,
ncap_tp_e tp, ncap_tp_ct tpu,
size_t paylen, const u_char *payload)
{
struct ncap_msg msg;
memset(&msg, 0, sizeof msg);
msg.ts = ts;
msg.user1 = user1;
msg.user2 = user2;
msg.np = np;
msg.tp = tp;
msg.paylen = paylen;
msg.npu = *npu;
msg.tpu = *tpu;
msg.payload = payload;
return (msg);
}
/* Stop a collect() if one is running.
*
* Note: can be called from a signal handler or from a collect callback.
*/
static void
stop(ncap_t ncap) {
ncap->pvt->flags |= NCAP_STOPPING;
}
/* Test a message against an installed filter (if any).
*
* Every rule in some filter has to match.
*/
static int
match(ncap_t ncap, ncap_msg_ct msg) {
ncap_filter_ptr filt;
/* If there are no filters, then all msgs are considered matching. */
if (ISC_LIST_EMPTY(ncap->pvt->filters))
return (true);
/* Test every filter. */
for (filt = ISC_LIST_HEAD(ncap->pvt->filters);
filt != NULL;
filt = ISC_LIST_NEXT(filt, link))
{
ncap_rule_ptr rule;
assert(!ISC_LIST_EMPTY(filt->rules));
/* Test every rule. */
for (rule = ISC_LIST_HEAD(filt->rules);
rule != NULL;
rule = ISC_LIST_NEXT(rule, link))
if (!(*rule->match)(rule, msg))
break;
/* Did all rules in the filter match? */
if (rule == NULL)
return (true);
}
/* No filter matched. */
return (false);
}
/* Write an NCAP MSG to an output file in export format.
*/
static ncap_result_e
ncap_write(ncap_t ncap, ncap_msg_ct msg, int fdes) {
static u_char padding[NCAP_PADFACTOR];
u_char fixedbuf[NCAP_MSGHDR + NCAP_NETHDR + NCAP_TPHDR], *p;
ssize_t fixedlen, msglen, msgpad, writ;
struct iovec iov[3];
int iovlen;
/* If there is no message, we're writing a file header. */
if (msg == NULL) {
u_char filebuf[NCAP_FILEHDR];
ssize_t len;
len = export_hdr(ncap, filebuf);
assert(len == NCAP_FILEHDR);
writ = write(fdes, filebuf, len);
if (writ < 0)
FAILURE(strerror(errno));
if (writ < len) {
errno = 0;
FAILURE("short write()");
}
return (ncap_success);
}
/* Make a header, then fix overall length. */
fixedlen = export_msg(ncap, msg, fixedbuf);
if (fixedlen < 0) {
errno = 0;
return (ncap_failure);
}
msglen = fixedlen + msg->paylen;
msgpad = (msglen % NCAP_PADFACTOR);
if (msgpad != 0) {
msgpad = NCAP_PADFACTOR - msgpad;
msglen += msgpad;
}
p = fixedbuf;
NCAP_PUT32(msglen, p);
iovlen = 0;
iov[iovlen].iov_base = (void *) fixedbuf;
iov[iovlen].iov_len = fixedlen;
iovlen++;
/* Note: casting to (u_long) is done to strip off "const". */
iov[iovlen].iov_base = (void *) (u_long) msg->payload;
iov[iovlen].iov_len = msg->paylen;
iovlen++;
if (msgpad != 0) {
iov[iovlen].iov_base = (void *) padding;
iov[iovlen].iov_len = msgpad;
iovlen++;
}
writ = writev(fdes, iov, iovlen);
if (writ < 0)
FAILURE(strerror(errno));
if (writ < (ssize_t)(fixedlen + msg->paylen + msgpad)) {
errno = 0;
FAILURE("short send()");
}
return (ncap_success);
}
/* Write an NCAP MSG to an output stream in export format.
*/
static ncap_result_e
ncap_fwrite(ncap_t ncap, ncap_msg_ct msg, FILE *fp) {
static const u_char padding[NCAP_PADFACTOR];
u_char fixedbuf[NCAP_MSGHDR + NCAP_NETHDR + NCAP_TPHDR], *p;
ssize_t fixedlen, msglen, msgpad;
size_t writ;
/* If there is no message, we're writing a file header. */
if (msg == NULL) {
u_char filebuf[NCAP_FILEHDR];
ssize_t len;
len = export_hdr(ncap, filebuf);
assert(len == NCAP_FILEHDR);
writ = fwrite(filebuf, 1, len, fp);
if ((ssize_t)writ != len)
FAILURE(strerror(errno));
return (ncap_success);
}
/* Make a header, then fix overall length. */
fixedlen = export_msg(ncap, msg, fixedbuf);
if (fixedlen < 0) {
errno = 0;
return (ncap_failure);
}
msglen = fixedlen + msg->paylen;
msgpad = (msglen % NCAP_PADFACTOR);
if (msgpad != 0) {
msgpad = NCAP_PADFACTOR - msgpad;
msglen += msgpad;
}
p = fixedbuf;
NCAP_PUT32(msglen, p);
writ = fwrite(fixedbuf, 1, fixedlen, fp);
if ((ssize_t)writ != fixedlen)
FAILURE(strerror(errno));
writ = fwrite(msg->payload, 1, msg->paylen, fp);
if (writ != msg->paylen)
FAILURE(strerror(errno));
if (msgpad != 0) {
writ = fwrite(padding, 1, msgpad, fp);
if ((ssize_t)writ != msgpad)
FAILURE(strerror(errno));
}
return (ncap_success);
}
/* Write an NCAP MSG to an output socket in export format.
*/
static ncap_result_e
ncap_send(ncap_t ncap, ncap_msg_ct msg, int fdes, int flags) {
static u_char padding[NCAP_PADFACTOR];
u_char fixedbuf[NCAP_MSGHDR + NCAP_NETHDR + NCAP_TPHDR], *p;
ssize_t fixedlen, msglen, msgpad, writ;
struct iovec iov[3];
struct msghdr mh;
int iovlen;
/* If there is no message, we're writing a file header. */
if (msg == NULL) {
u_char filebuf[NCAP_FILEHDR];
ssize_t len;
len = export_hdr(ncap, filebuf);
assert(len == NCAP_FILEHDR);
writ = send(fdes, filebuf, len, flags);
if (writ < 0)
FAILURE(strerror(errno));
if (writ < len) {
errno = 0;
FAILURE("short send()");
}
return (ncap_success);
}
/* Make a header, then fix overall length. */
fixedlen = export_msg(ncap, msg, fixedbuf);
if (fixedlen < 0) {
errno = 0;
return (ncap_failure);
}
msglen = fixedlen + msg->paylen;
msgpad = (msglen % NCAP_PADFACTOR);
if (msgpad != 0) {
msgpad = NCAP_PADFACTOR - msgpad;
msglen += msgpad;
}
p = fixedbuf;
NCAP_PUT32(msglen, p);