-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowd.c
1940 lines (1662 loc) · 55.4 KB
/
flowd.c
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
/*
* Copyright (c) 2004,2005 Damien Miller <[email protected]>
*
* Permission to use, copy, modify, and 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 THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
*/
#include "flowd-common.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <poll.h>
#include "sys-queue.h"
#include "sys-tree.h"
#include "flowd.h"
#include "privsep.h"
#include "netflow.h"
#include "store.h"
#include "store-v2.h"
#include "atomicio.h"
#include "peer.h"
RCSID("$Id$");
/* Dump unknown packet types */
/* #define DEBUG_UNKNOWN */
/* Reams of netflow v.9 verbosity */
/* #define DEBUG_NF9 */
/* Number of errors on Unix Domain log socket before we reopen */
#define LOGSOCK_REOPEN_ERROR_COUNT 128
/* Ratelimit for Unix Domain log socket reopens */
#define LOGSOCK_REOPEN_DELAY 60 /* seconds */
/* Prototype this (can't make it static because it only #ifdef DEBUG_UNKNOWN) */
void dump_packet(const char *tag, const u_int8_t *p, int len);
/* Unix domain socket error detection and reopen counters */
static int logsock_first_error = 0;
static int logsock_num_errors = 0;
/* Flags set by signal handlers */
static sig_atomic_t exit_flag = 0;
static sig_atomic_t reconf_flag = 0;
static sig_atomic_t reopen_flag = 0;
static sig_atomic_t info_flag = 0;
/* Input queue management */
#define INPUT_MAX_PACKET_PER_FD 512
struct flow_packet {
TAILQ_ENTRY(flow_packet) entry;
struct timeval recv_time;
struct xaddr flow_source;
u_int len;
u_int8_t *packet;
};
TAILQ_HEAD(flow_packets, flow_packet);
struct flow_packets input_queue = TAILQ_HEAD_INITIALIZER(input_queue);
/* Allocate a new packet (XXX: make this use a pool of preallocated entries) */
static struct flow_packet
*flow_packet_alloc(void)
{
return (calloc(1, sizeof(struct flow_packet)));
}
/* Deallocate a flow packet (XXX: change to return entry to freelist) */
static void
flow_packet_dealloc(struct flow_packet *f)
{
if (f->packet != NULL)
free(f->packet);
free(f);
}
/* Enqueue a flow packet in the input queue */
static void
flow_packet_enqueue(struct flow_packet *f)
{
TAILQ_INSERT_TAIL(&input_queue, f, entry);
}
/* Pull the first flow packet off the queue */
static struct flow_packet
*flow_packet_dequeue(void)
{
struct flow_packet *f;
if ((f = TAILQ_FIRST(&input_queue)) != NULL)
TAILQ_REMOVE(&input_queue, f, entry);
return (f);
}
/* Output queue management */
#define OUTPUT_INITIAL_QLEN (1024*16)
#define OUTPUT_MAX_QLEN (1024*512) /* Must be 2^x multiple of initial */
u_int8_t *output_queue = NULL;
size_t output_queue_alloc = 0;
size_t output_queue_offset = 0;
/* Enqueue a flow for output, return 0 on success, -1 on queue full */
static int
output_flow_enqueue(u_int8_t *f, size_t len, int verbose)
{
/* Force flush on overflow */
if (output_queue_offset + len > OUTPUT_MAX_QLEN) {
logit(LOG_DEBUG, "%s: output queue full", __func__);
return (-1);
}
if (output_queue == NULL) {
output_queue_alloc = OUTPUT_INITIAL_QLEN;
if ((output_queue = malloc(output_queue_alloc)) == NULL) {
logerrx("Output queue allocation (%zu bytes) failed",
output_queue_alloc);
}
if (verbose) {
logit(LOG_DEBUG, "%s: initial allocation %zu", __func__,
output_queue_alloc);
}
}
while (output_queue_offset + len > output_queue_alloc) {
u_int8_t *tmp_q;
size_t tmp_len = output_queue_alloc << 1;
/* This should never happen if max = initial * 2^x */
if (tmp_len > OUTPUT_MAX_QLEN) {
logit(LOG_DEBUG, "%s: oops, tmp_len (%zu) > "
"OUTPUT_MAX_QLEN (%u)", __func__, tmp_len,
OUTPUT_MAX_QLEN);
return (-1);
}
if ((tmp_q = realloc(output_queue, tmp_len)) == NULL) {
logit(LOG_DEBUG, "%s: realloc of %zu fail", __func__,
tmp_len);
return (-1);
}
if (verbose) {
logit(LOG_DEBUG, "%s: increased output queue "
"from %zuKB to %zuKB", __func__,
output_queue_alloc >> 10, tmp_len >> 10);
}
output_queue = tmp_q;
output_queue_alloc = tmp_len;
}
memcpy(output_queue + output_queue_offset, f, len);
output_queue_offset += len;
if (verbose) {
logit(LOG_DEBUG, "%s: offset %zu alloc %zu", __func__,
output_queue_offset, output_queue_alloc);
}
return (0);
}
static void
output_flow_flush(int log_fd, int verbose)
{
char ebuf[512];
if (log_fd == -1)
return;
if (verbose) {
logit(LOG_DEBUG, "%s: flushing output queue len %zu", __func__,
output_queue_offset);
}
if (output_queue_offset == 0)
return;
if (store_put_buf(log_fd, output_queue, output_queue_offset, ebuf,
sizeof(ebuf)) != STORE_ERR_OK)
logerrx("%s: exiting on %s", __func__, ebuf);
output_queue_offset = 0;
}
/* Signal handlers */
static void
sighand_exit(int signo)
{
exit_flag = signo;
signal(signo, sighand_exit);
}
static void
sighand_reconf(int signo)
{
reconf_flag = 1;
reopen_flag = 1;
signal(signo, sighand_reconf);
}
static void
sighand_reopen(int signo)
{
reopen_flag = 1;
signal(signo, sighand_reopen);
}
static void
sighand_info(int signo)
{
info_flag = 1;
signal(signo, sighand_info);
}
/* Format data to a hex string */
static const char *
data_ntoa(const u_int8_t *p, int len)
{
static char buf[2048];
char tmp[3];
int i;
for (*buf = '\0', i = 0; i < len; i++) {
snprintf(tmp, sizeof(tmp), "%02x%s", p[i], i % 2 ? " " : "");
if (strlcat(buf, tmp, sizeof(buf) - 4) >= sizeof(buf) - 4) {
strlcat(buf, "...", sizeof(buf));
break;
}
}
return (buf);
}
/* Dump a packet */
void
dump_packet(const char *tag, const u_int8_t *p, int len)
{
if (tag == NULL)
logit(LOG_INFO, "packet len %d: %s", len, data_ntoa(p, len));
else {
logit(LOG_INFO, "%s: packet len %d: %s",
tag, len, data_ntoa(p, len));
}
}
static int
start_log(int monitor_fd)
{
int fd;
off_t r;
char ebuf[512];
if ((fd = client_open_log(monitor_fd)) == -1)
logerrx("Logfile open failed, exiting");
/* Don't try to write a v.3 log on the end of a v.2 one */
r = lseek(fd, 0, SEEK_END);
/*
* If there isn't a full legacy header in the file or an error occurs
* (r == -1, e.g. on a FIFO) then don't bother checking for an old
* log header.
*/
if (r < sizeof(struct store_v2_header))
return (fd);
if ((r = lseek(fd, 0, SEEK_SET)) == -1) {
if (errno == ESPIPE)
return fd;
logerr("%s: lseek", __func__);
}
switch (store_v2_check_header(fd, ebuf, sizeof(ebuf))) {
case STORE_ERR_OK:
/* Uh oh - an old flow log is in the way, don't try to write */
logerrx("Error: Cannot append to legacy (version 2) flow log, "
"please move it out of the way and restart flowd");
case STORE_ERR_BAD_MAGIC:
case STORE_ERR_UNSUP_VERSION:
/* Good - the existing flow log is a probably a new one */
if ((r = lseek(fd, 0, SEEK_END)) == -1)
logerr("%s: lseek", __func__);
return (fd);
default:
logerrx("%s: %s", __func__, ebuf);
}
/* NOTREACHED */
return (-1);
}
static int
start_socket(int monitor_fd)
{
int fd;
if ((fd = client_open_socket(monitor_fd)) == -1)
logerrx("Logsock open failed, exiting");
return (fd);
}
static void
process_flow(struct store_flow_complete *flow, struct flowd_config *conf,
int log_fd, int log_socket)
{
char ebuf[512], fbuf[1024];
int flen;
u_int filtres;
/* Another sanity check */
if (flow->src_addr.af != flow->dst_addr.af) {
logit(LOG_WARNING, "%s: flow src(%d)/dst(%d) AF mismatch",
__func__, flow->src_addr.af, flow->dst_addr.af);
return;
}
/* Prepare for writing */
flow->hdr.fields = htonl(flow->hdr.fields);
flow->recv_time.recv_sec = htonl(flow->recv_time.recv_sec);
flow->recv_time.recv_usec = htonl(flow->recv_time.recv_usec);
filtres = filter_flow(flow, &conf->filter_list);
if (conf->opts & FLOWD_OPT_VERBOSE) {
char fmtbuf[1024];
store_format_flow(flow, fmtbuf, sizeof(fmtbuf), 0,
STORE_DISPLAY_ALL, 0);
logit(LOG_DEBUG, "%s: %s flow %s", __func__,
filtres == FF_ACTION_DISCARD ? "DISCARD" : "ACCEPT", fmtbuf);
}
if (filtres == FF_ACTION_DISCARD)
return;
if (store_flow_serialise_masked(flow, conf->store_mask, fbuf,
sizeof(fbuf), &flen, ebuf, sizeof(ebuf)) != STORE_ERR_OK)
logerrx("%s: exiting on %s", __func__, ebuf);
if (log_fd != -1 && output_flow_enqueue(fbuf, flen,
conf->opts & FLOWD_OPT_VERBOSE) == -1) {
output_flow_flush(log_fd, conf->opts & FLOWD_OPT_VERBOSE);
/* Must not fail after flush */
if (output_flow_enqueue(fbuf, flen,
conf->opts & FLOWD_OPT_VERBOSE) == -1)
logerrx("%s: enqueue failed after flush", __func__);
}
/* Track failures to send on log socket so we can reopen it */
if (log_socket != -1 && send(log_socket, fbuf, flen, 0) == -1) {
if (logsock_num_errors > 0 &&
(logsock_num_errors % 10) == 0) {
logit(LOG_WARNING, "log socket send: %s "
"(num errors %d)", strerror(errno),
logsock_num_errors);
}
if (errno != ENOBUFS) {
if (logsock_first_error == 0)
logsock_first_error = time(NULL);
logsock_num_errors++;
}
} else {
/* Start to disregard errors after success */
if (logsock_num_errors > 0)
logsock_num_errors--;
if (logsock_num_errors == 0)
logsock_first_error = 0;
}
/* XXX reopen log file on one failure, exit on multiple */
}
static void
process_netflow_v1(struct flow_packet *fp, struct flowd_config *conf,
struct peer_state *peer, struct peers *peers, int log_fd, int log_socket)
{
struct NF1_HEADER *nf1_hdr = (struct NF1_HEADER *)fp->packet;
struct NF1_FLOW *nf1_flow;
struct store_flow_complete flow;
size_t offset;
u_int i, nflows;
if (fp->len < sizeof(*nf1_hdr)) {
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.1 packet %d bytes from %s",
fp->len, addr_ntop_buf(&fp->flow_source));
return;
}
nflows = ntohs(nf1_hdr->c.flows);
if (nflows == 0 || nflows > NF1_MAXFLOWS) {
peer->ninvalid++;
logit(LOG_WARNING, "Invalid number of flows (%u) in netflow "
"v.1 packet from %s", nflows,
addr_ntop_buf(&fp->flow_source));
return;
}
if (fp->len != NF1_PACKET_SIZE(nflows)) {
peer->ninvalid++;
logit(LOG_WARNING, "Inconsistent Netflow v.1 packet from %s: "
"len %u expected %zu", addr_ntop_buf(&fp->flow_source),
fp->len, NF1_PACKET_SIZE(nflows));
return;
}
logit(LOG_DEBUG, "Valid netflow v.1 packet %d flows", nflows);
update_peer(peers, peer, nflows, 1);
for (i = 0; i < nflows; i++) {
offset = NF1_PACKET_SIZE(i);
nf1_flow = (struct NF1_FLOW *)(fp->packet + offset);
bzero(&flow, sizeof(flow));
/* NB. These are converted to network byte order later */
flow.hdr.fields = STORE_FIELD_ALL;
/* flow.hdr.tag is set later */
flow.hdr.fields &= ~STORE_FIELD_TAG;
flow.hdr.fields &= ~STORE_FIELD_SRC_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_DST_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_GATEWAY_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_AS_INFO;
flow.hdr.fields &= ~STORE_FIELD_FLOW_ENGINE_INFO;
flow.recv_time.recv_sec = fp->recv_time.tv_sec;
flow.recv_time.recv_usec = fp->recv_time.tv_usec;
flow.pft.tcp_flags = nf1_flow->tcp_flags;
flow.pft.protocol = nf1_flow->protocol;
flow.pft.tos = nf1_flow->tos;
memcpy(&flow.agent_addr, &fp->flow_source,
sizeof(flow.agent_addr));
flow.src_addr.v4.s_addr = nf1_flow->src_ip;
flow.src_addr.af = AF_INET;
flow.dst_addr.v4.s_addr = nf1_flow->dest_ip;
flow.dst_addr.af = AF_INET;
flow.gateway_addr.v4.s_addr = nf1_flow->nexthop_ip;
flow.gateway_addr.af = AF_INET;
flow.ports.src_port = nf1_flow->src_port;
flow.ports.dst_port = nf1_flow->dest_port;
#define NTO64(a) (store_htonll(ntohl(a)))
flow.octets.flow_octets = NTO64(nf1_flow->flow_octets);
flow.packets.flow_packets = NTO64(nf1_flow->flow_packets);
#undef NTO64
flow.ifndx.if_index_in = htonl(ntohs(nf1_flow->if_index_in));
flow.ifndx.if_index_out = htonl(ntohs(nf1_flow->if_index_out));
flow.ainfo.sys_uptime_ms = nf1_hdr->uptime_ms;
flow.ainfo.time_sec = nf1_hdr->time_sec;
flow.ainfo.time_nanosec = nf1_hdr->time_nanosec;
flow.ainfo.netflow_version = nf1_hdr->c.version;
flow.ftimes.flow_start = nf1_flow->flow_start;
flow.ftimes.flow_finish = nf1_flow->flow_finish;
process_flow(&flow, conf, log_fd, log_socket);
}
}
static void
process_netflow_v5(struct flow_packet *fp, struct flowd_config *conf,
struct peer_state *peer, struct peers *peers, int log_fd, int log_socket)
{
struct NF5_HEADER *nf5_hdr = (struct NF5_HEADER *)fp->packet;
struct NF5_FLOW *nf5_flow;
struct store_flow_complete flow;
size_t offset;
u_int i, nflows;
if (fp->len < sizeof(*nf5_hdr)) {
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.5 packet %d bytes from %s",
fp->len, addr_ntop_buf(&fp->flow_source));
return;
}
nflows = ntohs(nf5_hdr->c.flows);
if (nflows == 0 || nflows > NF5_MAXFLOWS) {
peer->ninvalid++;
logit(LOG_WARNING, "Invalid number of flows (%u) in netflow "
"v.5 packet from %s", nflows,
addr_ntop_buf(&fp->flow_source));
return;
}
if (fp->len != NF5_PACKET_SIZE(nflows)) {
peer->ninvalid++;
logit(LOG_WARNING, "Inconsistent Netflow v.5 packet from %s: "
"len %u expected %zu", addr_ntop_buf(&fp->flow_source),
fp->len, NF5_PACKET_SIZE(nflows));
return;
}
logit(LOG_DEBUG, "Valid netflow v.5 packet %d flows", nflows);
update_peer(peers, peer, nflows, 5);
for (i = 0; i < nflows; i++) {
offset = NF5_PACKET_SIZE(i);
nf5_flow = (struct NF5_FLOW *)(fp->packet + offset);
bzero(&flow, sizeof(flow));
/* NB. These are converted to network byte order later */
flow.hdr.fields = STORE_FIELD_ALL;
/* flow.hdr.tag is set later */
flow.hdr.fields &= ~STORE_FIELD_TAG;
flow.hdr.fields &= ~STORE_FIELD_SRC_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_DST_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_GATEWAY_ADDR6;
flow.recv_time.recv_sec = fp->recv_time.tv_sec;
flow.recv_time.recv_usec = fp->recv_time.tv_usec;
flow.pft.tcp_flags = nf5_flow->tcp_flags;
flow.pft.protocol = nf5_flow->protocol;
flow.pft.tos = nf5_flow->tos;
memcpy(&flow.agent_addr, &fp->flow_source,
sizeof(flow.agent_addr));
flow.src_addr.v4.s_addr = nf5_flow->src_ip;
flow.src_addr.af = AF_INET;
flow.dst_addr.v4.s_addr = nf5_flow->dest_ip;
flow.dst_addr.af = AF_INET;
flow.gateway_addr.v4.s_addr = nf5_flow->nexthop_ip;
flow.gateway_addr.af = AF_INET;
flow.ports.src_port = nf5_flow->src_port;
flow.ports.dst_port = nf5_flow->dest_port;
#define NTO64(a) (store_htonll(ntohl(a)))
flow.octets.flow_octets = NTO64(nf5_flow->flow_octets);
flow.packets.flow_packets = NTO64(nf5_flow->flow_packets);
#undef NTO64
flow.ifndx.if_index_in = htonl(ntohs(nf5_flow->if_index_in));
flow.ifndx.if_index_out = htonl(ntohs(nf5_flow->if_index_out));
flow.ainfo.sys_uptime_ms = nf5_hdr->uptime_ms;
flow.ainfo.time_sec = nf5_hdr->time_sec;
flow.ainfo.time_nanosec = nf5_hdr->time_nanosec;
flow.ainfo.netflow_version = nf5_hdr->c.version;
flow.ftimes.flow_start = nf5_flow->flow_start;
flow.ftimes.flow_finish = nf5_flow->flow_finish;
flow.asinf.src_as = htonl(ntohs(nf5_flow->src_as));
flow.asinf.dst_as = htonl(ntohs(nf5_flow->dest_as));
flow.asinf.src_mask = nf5_flow->src_mask;
flow.asinf.dst_mask = nf5_flow->dst_mask;
flow.finf.engine_type = nf5_hdr->engine_type;
flow.finf.engine_id = nf5_hdr->engine_id;
flow.finf.flow_sequence = nf5_hdr->flow_sequence;
process_flow(&flow, conf, log_fd, log_socket);
}
}
static void
process_netflow_v7(struct flow_packet *fp, struct flowd_config *conf,
struct peer_state *peer, struct peers *peers, int log_fd, int log_socket)
{
struct NF7_HEADER *nf7_hdr = (struct NF7_HEADER *)fp->packet;
struct NF7_FLOW *nf7_flow;
struct store_flow_complete flow;
size_t offset;
u_int i, nflows;
if (fp->len < sizeof(*nf7_hdr)) {
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.7 packet %d bytes from %s",
fp->len, addr_ntop_buf(&fp->flow_source));
return;
}
nflows = ntohs(nf7_hdr->c.flows);
if (nflows == 0 || nflows > NF7_MAXFLOWS) {
peer->ninvalid++;
logit(LOG_WARNING, "Invalid number of flows (%u) in netflow "
"v.7 packet from %s", nflows,
addr_ntop_buf(&fp->flow_source));
return;
}
if (fp->len != NF7_PACKET_SIZE(nflows)) {
peer->ninvalid++;
logit(LOG_WARNING, "Inconsistent Netflow v.7 packet from %s: "
"len %u expected %zu", addr_ntop_buf(&fp->flow_source),
fp->len, NF7_PACKET_SIZE(nflows));
return;
}
logit(LOG_DEBUG, "Valid netflow v.7 packet %d flows", nflows);
update_peer(peers, peer, nflows, 7);
for (i = 0; i < nflows; i++) {
offset = NF7_PACKET_SIZE(i);
nf7_flow = (struct NF7_FLOW *)(fp->packet + offset);
bzero(&flow, sizeof(flow));
/* NB. These are converted to network byte order later */
flow.hdr.fields = STORE_FIELD_ALL;
/* flow.hdr.tag is set later */
flow.hdr.fields &= ~STORE_FIELD_TAG;
flow.hdr.fields &= ~STORE_FIELD_SRC_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_DST_ADDR6;
flow.hdr.fields &= ~STORE_FIELD_GATEWAY_ADDR6;
/*
* XXX: we can parse the (undocumented) flags1 and flags2
* fields of the packet to disable flow fields not set by
* the Cat5k (e.g. destination-only mls nde mode)
*/
flow.recv_time.recv_sec = fp->recv_time.tv_sec;
flow.recv_time.recv_usec = fp->recv_time.tv_usec;
flow.pft.tcp_flags = nf7_flow->tcp_flags;
flow.pft.protocol = nf7_flow->protocol;
flow.pft.tos = nf7_flow->tos;
memcpy(&flow.agent_addr, &fp->flow_source,
sizeof(flow.agent_addr));
flow.src_addr.v4.s_addr = nf7_flow->src_ip;
flow.src_addr.af = AF_INET;
flow.dst_addr.v4.s_addr = nf7_flow->dest_ip;
flow.dst_addr.af = AF_INET;
flow.gateway_addr.v4.s_addr = nf7_flow->nexthop_ip;
flow.gateway_addr.af = AF_INET;
flow.ports.src_port = nf7_flow->src_port;
flow.ports.dst_port = nf7_flow->dest_port;
#define NTO64(a) (store_htonll(ntohl(a)))
flow.octets.flow_octets = NTO64(nf7_flow->flow_octets);
flow.packets.flow_packets = NTO64(nf7_flow->flow_packets);
#undef NTO64
flow.ifndx.if_index_in = htonl(ntohs(nf7_flow->if_index_in));
flow.ifndx.if_index_out = htonl(ntohs(nf7_flow->if_index_out));
flow.ainfo.sys_uptime_ms = nf7_hdr->uptime_ms;
flow.ainfo.time_sec = nf7_hdr->time_sec;
flow.ainfo.time_nanosec = nf7_hdr->time_nanosec;
flow.ainfo.netflow_version = nf7_hdr->c.version;
flow.ftimes.flow_start = nf7_flow->flow_start;
flow.ftimes.flow_finish = nf7_flow->flow_finish;
flow.asinf.src_as = htonl(ntohs(nf7_flow->src_as));
flow.asinf.dst_as = htonl(ntohs(nf7_flow->dest_as));
flow.asinf.src_mask = nf7_flow->src_mask;
flow.asinf.dst_mask = nf7_flow->dst_mask;
flow.finf.flow_sequence = nf7_hdr->flow_sequence;
process_flow(&flow, conf, log_fd, log_socket);
}
}
static int
nf9_rec_to_flow(struct peer_nf9_record *rec, struct store_flow_complete *flow,
u_int8_t *data)
{
/* XXX: use a table-based interpreter */
switch (rec->type) {
/* Copy an int (possibly shorter than the target) keeping their LSBs aligned */
#define BE_COPY(a) memcpy((u_char*)&a + (sizeof(a) - rec->len), data, rec->len);
#define V9_FIELD(v9_field, store_field, flow_field) \
case v9_field: \
flow->hdr.fields |= STORE_FIELD_##store_field; \
BE_COPY(flow->flow_field); \
break
#define V9_FIELD_ADDR(v9_field, store_field, flow_field, sub, family) \
case v9_field: \
flow->hdr.fields |= STORE_FIELD_##store_field; \
memcpy(&flow->flow_field.v##sub, data, rec->len); \
flow->flow_field.af = AF_##family; \
break
V9_FIELD(NF9_IN_BYTES, OCTETS, octets.flow_octets);
V9_FIELD(NF9_IN_PACKETS, PACKETS, packets.flow_packets);
V9_FIELD(NF9_IN_PROTOCOL, PROTO_FLAGS_TOS, pft.protocol);
V9_FIELD(NF9_SRC_TOS, PROTO_FLAGS_TOS, pft.tos);
V9_FIELD(NF9_TCP_FLAGS, PROTO_FLAGS_TOS, pft.tcp_flags);
V9_FIELD(NF9_L4_SRC_PORT, SRCDST_PORT, ports.src_port);
V9_FIELD(NF9_SRC_MASK, AS_INFO, asinf.src_mask);
V9_FIELD(NF9_INPUT_SNMP, IF_INDICES, ifndx.if_index_in);
V9_FIELD(NF9_L4_DST_PORT, SRCDST_PORT, ports.dst_port);
V9_FIELD(NF9_DST_MASK, AS_INFO, asinf.dst_mask);
V9_FIELD(NF9_OUTPUT_SNMP, IF_INDICES, ifndx.if_index_out);
V9_FIELD(NF9_SRC_AS, AS_INFO, asinf.src_as);
V9_FIELD(NF9_DST_AS, AS_INFO, asinf.dst_as);
V9_FIELD(NF9_LAST_SWITCHED, FLOW_TIMES, ftimes.flow_finish);
V9_FIELD(NF9_FIRST_SWITCHED, FLOW_TIMES, ftimes.flow_start);
V9_FIELD(NF9_IPV6_SRC_MASK, AS_INFO, asinf.src_mask);
V9_FIELD(NF9_IPV6_DST_MASK, AS_INFO, asinf.dst_mask);
V9_FIELD(NF9_ENGINE_TYPE, FLOW_ENGINE_INFO, finf.engine_type);
V9_FIELD(NF9_ENGINE_ID, FLOW_ENGINE_INFO, finf.engine_id);
V9_FIELD_ADDR(NF9_IPV4_SRC_ADDR, SRC_ADDR4, src_addr, 4, INET);
V9_FIELD_ADDR(NF9_IPV4_DST_ADDR, DST_ADDR4, dst_addr, 4, INET);
V9_FIELD_ADDR(NF9_IPV4_NEXT_HOP, GATEWAY_ADDR4, gateway_addr, 4, INET);
V9_FIELD_ADDR(NF9_IPV6_SRC_ADDR, SRC_ADDR6, src_addr, 6, INET6);
V9_FIELD_ADDR(NF9_IPV6_DST_ADDR, DST_ADDR6, dst_addr, 6, INET6);
V9_FIELD_ADDR(NF9_IPV6_NEXT_HOP, GATEWAY_ADDR6, gateway_addr, 6, INET6);
#undef V9_FIELD
#undef V9_FIELD_ADDR
#undef BE_COPY
}
return (0);
}
static int
nf9_check_rec_len(u_int type, u_int len)
{
struct store_flow_complete t;
/* Sanity check */
if (len == 0 || len > 0x4000)
return (0);
/* XXX: use a table-based interpreter */
switch (type) {
#define V9_FIELD_LEN(v9_field, flow_field) \
case v9_field: \
return (len <= sizeof(t.flow_field));
V9_FIELD_LEN(NF9_IN_BYTES, octets.flow_octets);
V9_FIELD_LEN(NF9_IN_PACKETS, packets.flow_packets);
V9_FIELD_LEN(NF9_IN_PROTOCOL, pft.protocol);
V9_FIELD_LEN(NF9_SRC_TOS, pft.tos);
V9_FIELD_LEN(NF9_TCP_FLAGS, pft.tcp_flags);
V9_FIELD_LEN(NF9_L4_SRC_PORT, ports.src_port);
V9_FIELD_LEN(NF9_IPV4_SRC_ADDR, src_addr.v4);
V9_FIELD_LEN(NF9_SRC_MASK, asinf.src_mask);
V9_FIELD_LEN(NF9_INPUT_SNMP, ifndx.if_index_in);
V9_FIELD_LEN(NF9_L4_DST_PORT, ports.dst_port);
V9_FIELD_LEN(NF9_IPV4_DST_ADDR, dst_addr.v4);
V9_FIELD_LEN(NF9_DST_MASK, asinf.src_mask);
V9_FIELD_LEN(NF9_OUTPUT_SNMP, ifndx.if_index_out);
V9_FIELD_LEN(NF9_IPV4_NEXT_HOP, gateway_addr.v4);
V9_FIELD_LEN(NF9_SRC_AS, asinf.src_as);
V9_FIELD_LEN(NF9_DST_AS, asinf.dst_as);
V9_FIELD_LEN(NF9_LAST_SWITCHED, ftimes.flow_finish);
V9_FIELD_LEN(NF9_FIRST_SWITCHED, ftimes.flow_start);
V9_FIELD_LEN(NF9_IPV6_SRC_ADDR, src_addr.v6);
V9_FIELD_LEN(NF9_IPV6_DST_ADDR, dst_addr.v6);
V9_FIELD_LEN(NF9_IPV6_SRC_MASK, asinf.src_mask);
V9_FIELD_LEN(NF9_IPV6_DST_MASK, asinf.dst_mask);
V9_FIELD_LEN(NF9_ENGINE_TYPE, finf.engine_type);
V9_FIELD_LEN(NF9_ENGINE_ID, finf.engine_id);
V9_FIELD_LEN(NF9_IPV6_NEXT_HOP, gateway_addr.v6);
#undef V9_FIELD_LEN
default:
return (1);
}
}
static int
nf9_flowset_to_store(u_int8_t *pkt, size_t len, struct timeval *tv,
struct xaddr *flow_source, struct NF9_HEADER *nf9_hdr,
struct peer_nf9_template *template, u_int32_t source_id,
struct store_flow_complete *flow)
{
u_int offset, i;
if (template->total_len > len)
return (-1);
bzero(flow, sizeof(*flow));
flow->hdr.fields = STORE_FIELD_RECV_TIME | STORE_FIELD_AGENT_INFO |
STORE_FIELD_AGENT_ADDR | STORE_FIELD_FLOW_ENGINE_INFO;
flow->ainfo.sys_uptime_ms = nf9_hdr->uptime_ms;
flow->ainfo.time_sec = nf9_hdr->time_sec;
flow->ainfo.netflow_version = nf9_hdr->c.version;
flow->finf.flow_sequence = nf9_hdr->package_sequence;
flow->finf.source_id = htonl(source_id);
flow->recv_time.recv_sec = tv->tv_sec;
flow->recv_time.recv_usec = tv->tv_usec;
memcpy(&flow->agent_addr, flow_source, sizeof(flow->agent_addr));
offset = 0;
for (i = 0; i < template->num_records; i++) {
#ifdef DEBUG_NF9
logit(LOG_DEBUG, " record %d: type %d len %d: %s",
i, template->records[i].type, template->records[i].len,
data_ntoa(pkt + offset, template->records[i].len));
#endif
nf9_rec_to_flow(&template->records[i], flow, pkt + offset);
offset += template->records[i].len;
}
return (0);
}
static int
process_netflow_v9_template(u_int8_t *pkt, size_t len, struct peer_state *peer,
struct peers *peers, u_int32_t source_id)
{
struct NF9_FLOWSET_HEADER_COMMON *template_header;
struct NF9_TEMPLATE_FLOWSET_HEADER *tmplh;
struct NF9_TEMPLATE_FLOWSET_RECORD *tmplr;
u_int i, count, offset, template_id, total_size;
struct peer_nf9_record *recs;
struct peer_nf9_template *template;
logit(LOG_DEBUG, "netflow v.9 template flowset from source 0x%x "
"(len %zd)", source_id, len);
#ifdef DEBUG_NF9
dump_packet(__func__, pkt, len);
#endif
template_header = (struct NF9_FLOWSET_HEADER_COMMON *)pkt;
if (len < sizeof(*template_header)) {
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.9 flowset template header "
"%zd bytes from %s/0x%x", len, addr_ntop_buf(&peer->from),
source_id);
/* XXX ratelimit */
return (-1);
}
if (ntohs(template_header->flowset_id) != NF9_TEMPLATE_FLOWSET_ID)
logerrx("Confused template");
logit(LOG_DEBUG, "NetFlow v.9 template set from %s/0x%x with len %zd:",
addr_ntop_buf(&peer->from), source_id, len);
for (offset = sizeof(*template_header); offset < len;) {
tmplh = (struct NF9_TEMPLATE_FLOWSET_HEADER *)(pkt + offset);
template_id = ntohs(tmplh->template_id);
count = ntohs(tmplh->count);
offset += sizeof(*tmplh);
logit(LOG_DEBUG, " Contains template 0x%08x/0x%04x with "
"%d records (offset %d):", source_id, template_id,
count, offset);
if ((recs = calloc(count, sizeof(*recs))) == NULL)
logerrx("%s: calloc failed (num %d)", __func__, count);
total_size = 0;
for (i = 0; i < count; i++) {
if (offset >= len) {
free(recs);
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.9 flowset "
"template 0x%08x/0x%04x %zd bytes from %s",
source_id, template_id, len,
addr_ntop_buf(&peer->from));
/* XXX ratelimit */
return (-1);
}
tmplr = (struct NF9_TEMPLATE_FLOWSET_RECORD *)
(pkt + offset);
recs[i].type = ntohs(tmplr->type);
recs[i].len = ntohs(tmplr->length);
offset += sizeof(*tmplr);
#ifdef DEBUG_NF9
logit(LOG_DEBUG, " record %d: type %d len %d",
i, recs[i].type, recs[i].len);
#endif
total_size += recs[i].len;
if (total_size > peers->max_template_len) {
free(recs);
peer->ninvalid++;
logit(LOG_WARNING, "netflow v.9 flowset "
"template 0x%08x/0x%04x from %s too large "
"len %d > max %d", source_id, template_id,
addr_ntop_buf(&peer->from), total_size,
peers->max_template_len);
/* XXX ratelimit */
return (-1);
}
if (!nf9_check_rec_len(recs[i].type, recs[i].len)) {
peer->ninvalid++;
logit(LOG_WARNING, "Invalid field length in "
"netflow v.9 flowset template %d from "
"%s/0x%08x type %d len %d", template_id,
addr_ntop_buf(&peer->from), source_id,
recs[i].type, recs[i].len);
free(recs);
/* XXX ratelimit */
return (-1);
}
/* XXX kill existing template on error! */
}
template = peer_nf9_find_template(peer, source_id, template_id);
if (template == NULL) {
template = peer_nf9_new_template(peer, peers,
source_id, template_id);
}
if (template->records != NULL)
free(template->records);
template->records = recs;
template->num_records = i;
template->total_len = total_size;
}
return (0);
}
static int
process_netflow_v9_data(u_int8_t *pkt, size_t len, struct timeval *tv,
struct peer_state *peer, u_int32_t source_id, struct NF9_HEADER *nf9_hdr,
struct flowd_config *conf, int log_fd, int log_socket, u_int *num_flows)
{
struct store_flow_complete *flows;
struct peer_nf9_template *template;
struct NF9_DATA_FLOWSET_HEADER *dath;
u_int flowset_id, i, offset, num_flowsets;
*num_flows = 0;
logit(LOG_DEBUG, "netflow v.9 data flowset (len %zd) source 0x%08x",
len, source_id);
dath = (struct NF9_DATA_FLOWSET_HEADER *)pkt;
if (len < sizeof(*dath)) {
peer->ninvalid++;
logit(LOG_WARNING, "short netflow v.9 data flowset header "
"%zd bytes from %s", len, addr_ntop_buf(&peer->from));
/* XXX ratelimit */
return (-1);
}
flowset_id = ntohs(dath->c.flowset_id);
if ((template = peer_nf9_find_template(peer, source_id,
flowset_id)) == NULL) {
peer->no_template++;
logit(LOG_DEBUG, "netflow v.9 data flowset without template "
"%s/0x%08x/0x%04x", addr_ntop_buf(&peer->from), source_id,
flowset_id);
return (0);
}
if (template->records == NULL)
logerrx("%s: template->records == NULL", __func__);
offset = sizeof(*dath);
num_flowsets = (len - offset) / template->total_len;
if (num_flowsets == 0 || num_flowsets > 0x4000) {
logit(LOG_WARNING, "invalid netflow v.9 data flowset "
"from %s: strange number of flows %d",
addr_ntop_buf(&peer->from), num_flowsets);
return (-1);
}
if ((flows = calloc(num_flowsets, sizeof(*flows))) == NULL)
logerrx("%s: calloc failed (num %d)", __func__, num_flowsets);
for (i = 0; i < num_flowsets; i++) {
if (nf9_flowset_to_store(pkt + offset, template->total_len, tv,
&peer->from, nf9_hdr, template, source_id,
&flows[i]) == -1) {
peer->ninvalid++;
free(flows);
logit(LOG_WARNING, "invalid netflow v.9 data flowset "
"from %s", addr_ntop_buf(&peer->from));
/* XXX ratelimit */
return (-1);
}
offset += template->total_len;
}
*num_flows = i;