-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtcplog_dumper.c
More file actions
2309 lines (2042 loc) · 59.8 KB
/
Copy pathtcplog_dumper.c
File metadata and controls
2309 lines (2042 loc) · 59.8 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
/*-
* Copyright (c) 2016 Netflix, Inc.
*
* 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 REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
*/
#include <sys/param.h>
#include <sys/endian.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/uio.h>
#include <dev/tcp_log/tcp_log_dev.h>
#include <machine/atomic.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/tcp_log_buf.h>
#include <netinet/tcp_var.h>
#include <pcap/pcap.h>
#include <assert.h>
#include <bitstring.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "tcplog_dumper.h"
static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
/* PCAP-NG defines */
#define MAGIC_NG 0x1A2B3C4D
#define MAJOR_NG 1
#define MINOR_NG 0
#define BT_IDB 0x00000001
#define BT_EPB 0x00000006
#define BT_SHB 0x0A0D0D0A
#define BT_CB_COPY 0x00000BAD
#define BT_CB_NOCOPY 0x40000BAD
#define OPT_ENDOFOPT 0
#define OPT_COMMENT 1
#define OPT_EPB_FLAGS_WORD 2
#define OPT_CUST_BIN_COPY 2989
#define OPT_CUST_BIN_NOCOPY 2989
#define MAX_SNAPLEN (sizeof(uint32_t) + sizeof(struct ip6_hdr) + TCP_MAXHLEN)
#define MAX_IOVS 64
enum compression_types {
COMPRESSION_NONE,
COMPRESSION_XZ,
};
static enum compression_types compression = COMPRESSION_NONE;
static const char default_directory[] = "/var/log/tcplog_dumps";
static const char default_filename[] = "/dev/tcp_log";
static const char default_username[] = "nobody";
static bool do_syslog = false;
static volatile bool quit_requested = false;
static bool reset_log_file = false;
static int exit_code = 0;
static volatile int queued_records = 0;
#define QUEUED_RECORDS_HIWAT 1000
#define QUEUED_RECORDS_LOWAT 900
struct log_queue {
STAILQ_ENTRY(log_queue) lq_link;
void *lq_log;
int lq_dirfd;
};
STAILQ_HEAD(loghead, log_queue);
static struct loghead bbr_loghead[NUM_THREADS];
static pthread_mutex_t bbr_queue_mtx[NUM_THREADS];
static pthread_cond_t bbr_queue_cond[NUM_THREADS];
static pthread_t bbr_tid[NUM_THREADS];
static int bbr_threads = 0;
static pthread_t main_thread_tid;
static pthread_mutex_t log_record_mutex;
static pthread_mutex_t queuewait_mutex;
static pthread_cond_t queuewait_cond;
static void
log_message(int priority, const char *message, ...) __printflike(2, 3);
static void
process_sighup(int signo __unused)
{
reset_log_file = true;
}
static void
process_sigterm(int signo __unused)
{
quit_requested = true;
}
/* A no-op; this just serves to break us from a blocked read. */
static void
process_sigusr2(int signo __unused)
{
return;
}
static void
log_message(int priority, const char *message, ...)
{
va_list ap;
va_start(ap, message);
if (do_syslog)
vsyslog(priority, message, ap);
else {
vfprintf(stderr, message, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}
/*
* Wake up any threads that are waiting on a condition.
*/
static void
signal_all_threads(void)
{
int i;
for (i = 0; i < bbr_threads; i++) {
pthread_mutex_lock(&bbr_queue_mtx[i]);
pthread_cond_signal(&bbr_queue_cond[i]);
pthread_mutex_unlock(&bbr_queue_mtx[i]);
}
/* Wake up the main thread. */
if (queued_records >= QUEUED_RECORDS_LOWAT) {
pthread_mutex_lock(&queuewait_mutex);
pthread_cond_signal(&queuewait_cond);
pthread_mutex_unlock(&queuewait_mutex);
}
if (pthread_self() != main_thread_tid)
pthread_kill(main_thread_tid, SIGUSR2);
}
void
do_exit(int rv)
{
exit_code = rv;
quit_requested = 1;
signal_all_threads();
exit(rv);
}
void
do_err(int rv, const char *message, ...)
{
va_list ap;
exit_code = rv;
quit_requested = 1;
signal_all_threads();
va_start(ap, message);
verr(rv, message, ap);
va_end(ap);
}
/*
* Create an IP checksum from a header.
*/
static void
add_ip_cksum(aligned_ip_hdr *hdr)
{
uint16_t *in;
uint32_t sum;
/* Assert on alignment. */
assert(((uintptr_t)hdr) % 2 == 0);
/*
* We optimize this in two ways:
* 1. Assume we will never have options.
* 2. Ignore the checksum field (index 5).
*
* For purposes of this file, these should be safe assumptions.
*/
in = (uint16_t *)hdr;
sum = in[0] + in[1] + in[2] + in[3] + in[4] + in[6] + in[7] +
in[8] + in[9];
while (sum > 0xffff)
sum = (sum & 0xffff) + (sum >> 16);
sum = (~sum & 0xffff);
hdr->ip_sum = sum;
}
/*
* Create a TCP checksum from a header.
*/
static void
add_tcp_cksum(const struct extract_context *ctx, aligned_tcp_hdr *th,
uint16_t len)
{
register const uint16_t *in;
register uint32_t sum;
int tcp_hlen;
/* Assert on alignment. */
assert(((uintptr_t)th) % 2 == 0);
if (ctx->af == AF_INET) {
/* Add the pseudo_hdr. */
in = (const uint16_t *)(&ctx->in_iphdr);
sum = in[6] + in[7] + in[8] + in[9];
sum += htons(IPPROTO_TCP);
sum += htons(len);
} else {
/* Add the pseudo_hdr. */
in = (const uint16_t *)(&ctx->in_ip6hdr);
sum = in[4] + in[5] + in[6] + in[7] + in[8] + in[9] + in[10] +
in[11] + in[12] + in[13] + in[14] + in[15] + in[16] +
in[17] + in[18] + in[19];
sum += htons(len);
sum += htons(IPPROTO_TCP);
}
/* Add the fixed TCP header (less the checksum). */
in = (const uint16_t *)th;
sum += in[0] + in[1] + in[2] + in[3] + in[4] + in[5] + in[6] +
in[7] + in[9];
/* Add the TCP options. */
tcp_hlen = th->th_off << 2;
tcp_hlen -= sizeof(struct tcphdr);
in = (const uint16_t *)(th + 1);
while (tcp_hlen) {
sum += in[0] + in[1];
in += 2;
tcp_hlen -= 4;
}
/*
* Because we always fill the data portion with 0s, we don't need
* to include it in the checksum calculation. Yay!
*/
/* Calculate and store the final checksum. */
while (sum > 0xffff)
sum = (sum & 0xffff) + (sum >> 16);
sum = (~sum & 0xffff);
th->th_sum = sum;
}
struct pcapng_sh {
uint32_t type;
uint32_t hdrlen1;
uint32_t magic;
uint16_t major;
uint16_t minor;
int64_t seclen;
/* Options go here */
} __packed;
struct pcapng_idb {
uint32_t type;
uint32_t len1;
uint16_t linktype;
uint16_t reserved;
uint32_t snaplen;
/* Options go here */
} __packed;
struct pcapng_epb {
uint32_t type;
uint32_t len1;
uint32_t intid;
uint32_t ts_high;
uint32_t ts_low;
uint32_t caplen;
uint32_t pktlen;
/*
* Start of packet data. Because we use a NULL header, we just
* include it here.
*/
uint32_t protocol;
uint8_t pktdata[0];
} __packed;
struct pcapng_epb_flags_opt {
uint16_t code;
uint16_t len;
uint32_t flags_word;
} __packed;
struct pcapng_nflx_block {
uint32_t type;
uint32_t len1;
uint32_t pen;
uint32_t nflx_type;
} __packed;
enum netflix_block_types {
NFLX_EVENT_BLOCK=1,
NFLX_SKIPPED_BLOCK,
};
struct pcapng_nflx_eventblock {
struct pcapng_nflx_block hdr;
};
struct pcapng_nflx_skipped {
struct pcapng_nflx_block hdr;
uint32_t num_skipped;
};
struct pcapng_blockend {
uint32_t lastopt;
uint32_t blocklen;
} __packed;
struct pcapng_nflx_opt {
uint16_t code;
uint16_t len;
uint32_t pen;
uint32_t nflx_type;
uint8_t optdata[0];
} __packed;
enum netflix_opt_types {
NFLX_OPT_VERSION=1,
NFLX_OPT_TCPINFO,
NFLX_OPT_TCPVERBOSE,
NFLX_OPT_DUMPINFO,
NFLX_OPT_DUMPTIME,
NFLX_OPT_STACKNAME,
};
/* https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers */
#define NFLX_PEN 10949
struct stacknames {
const char *s_stackname;
int s_strlen;
};
static struct stacknames stacknames[256];
static void *stacknamebuf = NULL;
static pthread_rwlock_t stacknames_lock;
/*
* Refesh the stack name-to-ID mappings.
*
* This will clear all the old mappings and create new ones. It must be
* called while holding a write lock on stacknames_lock.
* Further, because this function will free the storage used for stack names,
* any caller that desires to use the stack name must hold at least a read
* lock on stacknames_lock as long as it needs the stack name pointer to
* be valid.
*/
static void refresh_stack_names(void) __requires_exclusive(stacknames_lock);
static void
refresh_stack_names(void)
{
struct tcp_function_info *tfi;
caddr_t tfi_end;
size_t len;
int rv;
/* Get the new mappings. */
len = 0;
rv = sysctlbyname("net.inet.tcp.function_info", NULL, &len, NULL, 0);
if (rv < 0 && errno != ENOMEM) {
log_message(LOG_ERR, "Unable to retrieve length of function "
"ID mappings (%d: %s)", rv, strerror(errno));
return;
}
if (len == 0)
return;
tfi = malloc(len);
if (tfi == NULL) {
log_message(LOG_ERR, "Unable to allocate buffer for function "
"ID mappings");
return;
}
rv = sysctlbyname("net.inet.tcp.function_info", tfi, &len, NULL, 0);
if (rv < 0) {
log_message(LOG_ERR, "Unable to retrieve function ID "
"mappings (%d: %s)", rv, strerror(errno));
free(tfi);
return;
}
/*
* Now that we have the mappings, clear the old mappings and install
* the new ones.
*/
memset(stacknames, 0, sizeof(stacknames));
if (stacknamebuf != NULL)
free(stacknamebuf);
stacknamebuf = tfi;
tfi_end = (caddr_t)tfi + len;
for (; (caddr_t)tfi < tfi_end; tfi++) {
/* Skip aliases. */
if (strncmp(tfi->tfi_name, tfi->tfi_alias,
TCP_FUNCTION_NAME_LEN_MAX))
continue;
stacknames[tfi->tfi_id].s_stackname = tfi->tfi_name;
stacknames[tfi->tfi_id].s_strlen = strlen(tfi->tfi_name);
}
}
static void
init_stack_names(void)
{
pthread_rwlock_init(&stacknames_lock, NULL);
pthread_rwlock_wrlock(&stacknames_lock);
memset(stacknames, 0, sizeof(stacknames));
refresh_stack_names();
pthread_rwlock_unlock(&stacknames_lock);
}
static void
free_iov_allocs(struct iovec *iov, int iovcnt, bitstr_t *free_map)
{
int i;
for (bit_ffs(free_map, iovcnt, &i); i >= 0;
bit_ffs_at(free_map, i + 1, iovcnt, &i))
free(iov[i].iov_base);
}
static char junk[MAX_SNAPLEN];
#ifndef CTASSERT
#define CTASSERT(x) _Static_assert(x, "Compile-time assertion failed")
#endif
/*
* pcap_epb_flags_opt() adds a EPB flags word option indicating the packet direction.
* This option is only included in the enhanced packet block.
*/
static int
pcap_epb_flags_opt(bool inbound, struct iovec *iov, int *iovcnt, bitstr_t *free_map)
{
struct pcapng_epb_flags_opt *epb_flags_opt;
epb_flags_opt = malloc(sizeof(struct pcapng_epb_flags_opt));
if (epb_flags_opt == NULL) {
warn("Error allocating space for the EPB flags word");
return (0);
}
epb_flags_opt->code = OPT_EPB_FLAGS_WORD;
epb_flags_opt->len = sizeof(uint32_t);
if (inbound) {
/* Inbound packet. */
epb_flags_opt->flags_word = 0x00000001;
} else {
/* Outbound packet. */
epb_flags_opt->flags_word = 0x00000002;
}
iov[*iovcnt].iov_base = epb_flags_opt;
iov[*iovcnt].iov_len = sizeof(struct pcapng_epb_flags_opt);
bit_set(free_map, *iovcnt);
(*iovcnt)++;
return (sizeof(struct pcapng_epb_flags_opt));
}
/*
* pcap_nflx_opt_alloc() allocates memory for a Netflix custom option.
* These options can be used in custom or standard blocks.
*/
static void *
pcap_nflx_opt_alloc(uint32_t type, uint32_t len, bool in_cb)
{
struct pcapng_nflx_opt *opt;
assert(len + 8 <= UINT16_MAX);
opt = malloc(sizeof(struct pcapng_nflx_opt));
if (opt == NULL) {
warn("Error allocating space for an option buffer");
return (NULL);
}
/*
* When using this custom option in a custom block, store the code, len, and
* pen in little endian.
* In all other cases, store these fields in host byte order.
* The custom option value, including the NFLX type, is always stored in
* little endian.
*/
if (in_cb) {
opt->code = htole16(OPT_CUST_BIN_COPY);
opt->len = htole16(len + 8);
opt->pen = htole32(NFLX_PEN);
} else {
opt->code = OPT_CUST_BIN_COPY;
opt->len = len + 8;
opt->pen = NFLX_PEN;
}
opt->nflx_type = htole32(type);
return (opt);
}
/*
* We assume that we will end up with data aligned on a 4-byte boundary when
* we copy out the tcp log buffer up to (but not including) the tlb_th member.
*/
CTASSERT(offsetof(struct tcp_log_buffer, tlb_th) % 4 == 0);
/*
* We also assume that time_t is a 64-bit entity, so basically this excludes i386.
*/
CTASSERT(sizeof(time_t) == 8);
/*
* pcap_dumptime_opt() adds a custom option containing the time (in seconds since the
* Unix epoch) when the file was written.
* This custom option is only included in the section header block.
*/
static int
pcap_dumptime_opt(time_t *dumptime, struct iovec *iov, int *iovcnt,
bitstr_t *free_map)
{
void *opt;
time_t *time;
int rv;
/*
* Allocate the netflix option header and body. If either fails,
* give up and return an error.
*/
opt = pcap_nflx_opt_alloc(NFLX_OPT_DUMPTIME, sizeof(time_t), false);
if (opt == NULL)
return (0);
time = malloc(sizeof(time_t));
if (time == NULL) {
warn("Error allocating space for the dumptime");
free(opt);
return (0);
}
*time = htole64(*dumptime);
/*
* Add the option header and body to the IOV. Because we allocated
* space for these, we also need to mark these in the free_map.
*/
iov[*iovcnt].iov_base = opt;
iov[*iovcnt].iov_len = sizeof(struct pcapng_nflx_opt);
rv = iov[*iovcnt].iov_len;
bit_set(free_map, *iovcnt);
(*iovcnt)++;
iov[*iovcnt].iov_base = time;
iov[*iovcnt].iov_len = sizeof(time_t);
rv += iov[*iovcnt].iov_len;
bit_set(free_map, *iovcnt);
(*iovcnt)++;
return (rv);
}
/*
* pcap_version_opt() adds a custom option contaning the version of the file format.
* This custom option is only included in the section header block.
*/
static int
pcap_version_opt(struct iovec *iov, int *iovcnt, bitstr_t *free_map)
{
void *opt;
uint32_t *version;
int rv;
/*
* Allocate the netflix option header and body. If either fails,
* give up and return an error.
*/
opt = pcap_nflx_opt_alloc(NFLX_OPT_VERSION, sizeof(uint32_t), false);
if (opt == NULL)
return (0);
version = malloc(sizeof(uint32_t));
if (version == NULL) {
warn("Error allocating space for the version identifier");
free(opt);
return (0);
}
/*
* We checked the log format version at startup. So, we can
* statically compile that here.
*/
*version = htole32(TCP_LOG_BUF_VER);
/*
* Add the option header and body to the IOV. Because we allocated
* space for these, we also need to mark these in the free_map.
*/
iov[*iovcnt].iov_base = opt;
iov[*iovcnt].iov_len = sizeof(struct pcapng_nflx_opt);
rv = iov[*iovcnt].iov_len;
bit_set(free_map, *iovcnt);
(*iovcnt)++;
iov[*iovcnt].iov_base = version;
iov[*iovcnt].iov_len = sizeof(uint32_t);
rv += iov[*iovcnt].iov_len;
bit_set(free_map, *iovcnt);
(*iovcnt)++;
return (rv);
}
/*
* We assume that struct timeval consists of two 64-bit entities, so basically assumes
* a 64-bit platform.
*/
CTASSERT(sizeof(struct timeval) == 16);
/*
* pcap_dumpinfo_opt() adds a custom option contaning the stack ID and stack name.
* This custom option is only included in the section header block.
*/
static int
pcap_dumpinfo_opt(struct tcp_log_header *hdr, struct iovec *iov, int *iovcnt,
bitstr_t *free_map)
{
void *opt;
struct tcp_log_header *hdr_le;
int rv;
opt = pcap_nflx_opt_alloc(NFLX_OPT_DUMPINFO,
sizeof(struct tcp_log_header), false);
if (opt == NULL)
return (0);
#if BYTE_ORDER == LITTLE_ENDIAN
hdr_le = hdr;
#else
hdr_le = malloc(sizeof(struct tcp_log_header));
if (hdr_le == NULL) {
warn("Error allocating space for the tcp_log_header");
free(opt);
return (0);
}
/*
* Convert all fields, except the endpoint information, to litte endian.
* Keep the endpoint information in network byte order, aka big endian.
*/
hdr_le->tlh_version = htole32(hdr->tlh_version);
hdr_le->tlh_type = htole32(hdr->tlh_type);
hdr_le->tlh_length = htole64(hdr->tlh_length);
hdr_le->tlh_ie = hdr->tlh_ie;
hdr_le->tlh_offset.tv_sec = htole64(hdr->tlh_offset.tv_sec);
hdr_le->tlh_offset.tv_usec = htole64(hdr->tlh_offset.tv_usec);
memcpy(hdr_le->tlh_id, hdr->tlh_id ,TCP_LOG_ID_LEN);
memcpy(hdr_le->tlh_reason, hdr->tlh_reason, TCP_LOG_REASON_LEN);
memcpy(hdr_le->tlh_tag, hdr->tlh_tag, TCP_LOG_TAG_LEN);
hdr_le->tlh_af = hdr->tlh_af;
memcpy(hdr_le->_pad, hdr->_pad, 7);
#endif
/* Update the IOV. */
iov[*iovcnt].iov_base = opt;
iov[*iovcnt].iov_len = sizeof(struct pcapng_nflx_opt);
rv = iov[*iovcnt].iov_len;
bit_set(free_map, *iovcnt);
(*iovcnt)++;
iov[*iovcnt].iov_base = hdr_le;
iov[*iovcnt].iov_len = sizeof(struct tcp_log_header);
rv += iov[*iovcnt].iov_len;
#if BYTE_ORDER == BIG_ENDIAN
bit_set(free_map, *iovcnt);
#endif
(*iovcnt)++;
return (rv);
}
/*
* pcap_stackname_opt() adds a custom option containing the stack ID and stack name.
* This custom option is only included in the section header block.
*/
static int pcap_stackname_opt(uint8_t *stackid, struct iovec *iov, int *iovcnt,
bitstr_t *free_map) __locks_shared(stacknames_lock);
static int
pcap_stackname_opt(uint8_t *stackid, struct iovec *iov, int *iovcnt,
bitstr_t *free_map)
{
const char *stackname;
void *opt;
int stacknamelen, rv;
/* Try to find the stack. */
pthread_rwlock_rdlock(&stacknames_lock);
stackname = stacknames[*stackid].s_stackname;
stacknamelen = stacknames[*stackid].s_strlen;
/*
* If we didn't acquire the stack name, try to refresh our view of
* the stack names.
*/
if (stackname == NULL) {
pthread_rwlock_unlock(&stacknames_lock);
pthread_rwlock_wrlock(&stacknames_lock);
refresh_stack_names();
pthread_rwlock_unlock(&stacknames_lock);
pthread_rwlock_rdlock(&stacknames_lock);
stackname = stacknames[*stackid].s_stackname;
stacknamelen = stacknames[*stackid].s_strlen;
/*
* If we still don't have the stack name, just call it
* "unknown".
*/
if (stackname == NULL) {
stackname = "unknown";
stacknamelen = strlen(stackname);
}
}
stacknamelen = strlen(stackname);
opt = pcap_nflx_opt_alloc(NFLX_OPT_STACKNAME, stacknamelen + 1, false);
if (opt == NULL)
return (0);
/* We allocated space. Mark the fact that we will need to clear it. */
bit_set(free_map, *iovcnt);
/* Update the IOV. */
iov[*iovcnt].iov_base = opt;
iov[*iovcnt].iov_len = sizeof(struct pcapng_nflx_opt);
rv = iov[*iovcnt].iov_len;
(*iovcnt)++;
iov[*iovcnt].iov_base = stackid;
iov[*iovcnt].iov_len = sizeof(*stackid);
rv += iov[*iovcnt].iov_len;
(*iovcnt)++;
iov[*iovcnt].iov_base = __DECONST(void *, stackname);
iov[*iovcnt].iov_len = stacknamelen;
rv += iov[*iovcnt].iov_len;
(*iovcnt)++;
/* Pad, if necessary. */
if (rv % 4) {
iov[*iovcnt].iov_base = junk;
iov[*iovcnt].iov_len = 4 - (rv % 4);
rv += iov[*iovcnt].iov_len;
(*iovcnt)++;
}
assert((rv % 4) == 0);
return (rv);
}
/*
* pcap_tcpbuf_opt() adds a custom option containing information about the current state
* of the TCP stack.
* This custom option is used in the enhanced packet block and also in the custom block
* (the event block).
*/
static int
pcap_tcpbuf_opt(struct tcp_log_buffer *tlb, bool in_cb, struct iovec *iov, int *iovcnt,
bitstr_t *free_map)
{
void *opt;
struct tcp_log_buffer *tlb_le;
#if (BYTE_ORDER == BIG_ENDIAN) && defined(NETFLIX_TCP_STACK)
union tcp_log_userdata *tlu, *tlu_le;
unsigned int i;
#endif
int rv;
opt = pcap_nflx_opt_alloc(NFLX_OPT_TCPINFO,
offsetof(struct tcp_log_buffer, tlb_th), in_cb);
if (opt == NULL)
return (0);
#if BYTE_ORDER == LITTLE_ENDIAN
tlb_le = tlb;
#else
tlb_le = malloc(sizeof(struct tcp_log_buffer));
if (tlb_le == NULL) {
warn("Error allocating space for the tcp_log_buffer");
free(opt);
return (0);
}
/* Convert all fields to litte endian. */
tlb_le->tlb_tv.tv_sec = htole64(tlb->tlb_tv.tv_sec);
tlb_le->tlb_tv.tv_usec = htole64(tlb->tlb_tv.tv_usec);
tlb_le->tlb_ticks = htole32(tlb->tlb_ticks);
tlb_le->tlb_sn = htole32(tlb->tlb_sn);
tlb_le->tlb_stackid = tlb->tlb_stackid;
tlb_le->tlb_eventid = tlb->tlb_eventid;
tlb_le->tlb_eventflags = htole16(tlb->tlb_eventflags);
tlb_le->tlb_errno = htole32(tlb->tlb_errno);
tlb_le->tlb_rxbuf.tls_sb_acc = htole32(tlb->tlb_rxbuf.tls_sb_acc);
tlb_le->tlb_rxbuf.tls_sb_ccc = htole32(tlb->tlb_rxbuf.tls_sb_ccc);
tlb_le->tlb_rxbuf.tls_sb_spare = htole32(tlb->tlb_rxbuf.tls_sb_spare);
tlb_le->tlb_txbuf.tls_sb_acc = htole32(tlb->tlb_txbuf.tls_sb_acc);
tlb_le->tlb_txbuf.tls_sb_ccc = htole32(tlb->tlb_txbuf.tls_sb_ccc);
tlb_le->tlb_txbuf.tls_sb_spare = htole32(tlb->tlb_txbuf.tls_sb_spare);
tlb_le->tlb_state = htole32(tlb->tlb_state);
tlb_le->tlb_starttime = htole32(tlb->tlb_starttime);
tlb_le->tlb_iss = htole32(tlb->tlb_iss);
tlb_le->tlb_flags = htole32(tlb->tlb_flags);
tlb_le->tlb_snd_una = htole32(tlb->tlb_snd_una);
tlb_le->tlb_snd_max = htole32(tlb->tlb_snd_max);
tlb_le->tlb_snd_cwnd = htole32(tlb->tlb_snd_cwnd);
tlb_le->tlb_snd_nxt = htole32(tlb->tlb_snd_nxt);
tlb_le->tlb_snd_recover = htole32(tlb->tlb_snd_recover);
tlb_le->tlb_snd_wnd = htole32(tlb->tlb_snd_wnd);
tlb_le->tlb_snd_ssthresh = htole32(tlb->tlb_snd_ssthresh);
tlb_le->tlb_srtt = htole32(tlb->tlb_srtt);
tlb_le->tlb_rttvar = htole32(tlb->tlb_rttvar);
tlb_le->tlb_rcv_up = htole32(tlb->tlb_rcv_up);
tlb_le->tlb_rcv_adv = htole32(tlb->tlb_rcv_adv);
tlb_le->tlb_flags2 = htole32(tlb->tlb_flags2);
tlb_le->tlb_rcv_nxt = htole32(tlb->tlb_rcv_nxt);
tlb_le->tlb_rcv_wnd = htole32(tlb->tlb_rcv_wnd);
tlb_le->tlb_dupacks = htole32(tlb->tlb_dupacks);
tlb_le->tlb_segqlen = htole32(tlb->tlb_segqlen);
tlb_le->tlb_snd_numholes = htole32(tlb->tlb_snd_numholes);
tlb_le->tlb_flex1 = htole32(tlb->tlb_flex1);
tlb_le->tlb_flex2 = htole32(tlb->tlb_flex2);
tlb_le->tlb_fbyte_in = htole32(tlb->tlb_fbyte_in);
tlb_le->tlb_fbyte_out = htole32(tlb->tlb_fbyte_out);
tlb_le->tlb_snd_scale = tlb->tlb_rcv_scale;
tlb_le->tlb_rcv_scale = tlb->tlb_snd_scale;
memcpy(tlb_le->_pad, tlb->_pad, 3);
switch (tlb->tlb_eventid) {
#ifdef NETFLIX_TCP_STACK
case TCP_LOG_SENDFILE:
tlb_le->tlb_stackinfo.u_sf.offset =
htole64(tlb->tlb_stackinfo.u_sf.offset);
tlb_le->tlb_stackinfo.u_sf.length =
htole64(tlb->tlb_stackinfo.u_sf.length);
tlb_le->tlb_stackinfo.u_sf.flags =
htole32(tlb->tlb_stackinfo.u_sf.flags);
/*
* Copy over the rest without modification, since the structure is not
* known. It should be padding.
*/
memcpy((char *)tlb_le + sizeof(struct tcp_log_sendfile),
(char *)tlb + sizeof(struct tcp_log_sendfile),
sizeof(union tcp_log_stackspecific) -
sizeof(struct tcp_log_sendfile));
break;
case TCP_LOG_USER_EVENT:
tlu = (union tcp_log_userdata *)tlb;
tlu_le = (union tcp_log_userdata *)tlb_le;
switch (tlb->flex1) {
case TCP_LOG_USER_HTTPD:
tlu_le->http_req.timestamp = htole64(tlu_le->http_req.timestamp);
tlu_le->http_req.start = htole64(tlu_le->http_req.start);
tlu_le->http_req.end = htole64(tlu_le->http_req.end);
tlu_le->http_req.flags = htole32(tlu_le->http_req.flags);
/*
* Copy over the rest without modification, since the structure
* is not known. It should be padding.
*/
memcpy((char *)tlu_le + sizeof(union tcp_log_userdata),
(char *)tlu + sizeof(union tcp_log_userdata),
sizeof(union tcp_log_stackspecific) -
sizeof(union tcp_log_userdata));
break;
default:
/*
* Copy over without modification, since the structure is not
* known.
*/
memcpy(tlu_le, tlu, sizeof(union tcp_log_stackspecific));
break;
}
break;
case TCP_LOG_ACCOUNTING:
for (i = 0; i < 4; i++) {
tlb_le->tlb_stackinfo.u_raw.u64_flex[i] =
htole64(tlb->tlb_stackinfo.u_raw.u64_flex[i]);
}
for (i = 0; i < 14; i++) {
tlb_le->tlb_stackinfo.u_raw.u32_flex[i] =
htole32(tlb->tlb_stackinfo.u_raw.u32_flex[i]);
}
for (i = 0; i < 3; i++) {
tlb_le->tlb_stackinfo.u_raw.u16_flex[i] =
htole16(tlb->tlb_stackinfo.u_raw.u16_flex[i]);
}
for (i = 0; i < 6; i++) {
tlb_le->tlb_stackinfo.u_raw.u8_flex[i] =
tlb->tlb_stackinfo.u_raw.u8_flex[i];
}
tlb_le->tlb_stackinfo.u_raw.u32_flex2[0] =
htole32(tlb->tlb_stackinfo.u_raw.u32_flex2[0]);
break;
#endif
default:
tlb_le->tlb_stackinfo.u_bbr.cur_del_rate =
htole64(tlb->tlb_stackinfo.u_bbr.cur_del_rate);
tlb_le->tlb_stackinfo.u_bbr.delRate =
htole64(tlb->tlb_stackinfo.u_bbr.delRate);
tlb_le->tlb_stackinfo.u_bbr.rttProp =
htole64(tlb->tlb_stackinfo.u_bbr.rttProp);
tlb_le->tlb_stackinfo.u_bbr.bw_inuse =
htole64(tlb->tlb_stackinfo.u_bbr.bw_inuse);
tlb_le->tlb_stackinfo.u_bbr.inflight =
htole32(tlb->tlb_stackinfo.u_bbr.inflight);
tlb_le->tlb_stackinfo.u_bbr.applimited =
htole32(tlb->tlb_stackinfo.u_bbr.applimited);
tlb_le->tlb_stackinfo.u_bbr.delivered =
htole32(tlb->tlb_stackinfo.u_bbr.delivered);
tlb_le->tlb_stackinfo.u_bbr.timeStamp =
htole32(tlb->tlb_stackinfo.u_bbr.timeStamp);
tlb_le->tlb_stackinfo.u_bbr.epoch =
htole32(tlb->tlb_stackinfo.u_bbr.epoch);
tlb_le->tlb_stackinfo.u_bbr.lt_epoch =
htole32(tlb->tlb_stackinfo.u_bbr.lt_epoch);
tlb_le->tlb_stackinfo.u_bbr.pkts_out =
htole32(tlb->tlb_stackinfo.u_bbr.pkts_out);
tlb_le->tlb_stackinfo.u_bbr.flex1 =
htole32(tlb->tlb_stackinfo.u_bbr.flex1);
tlb_le->tlb_stackinfo.u_bbr.flex2 =
htole32(tlb->tlb_stackinfo.u_bbr.flex2);
tlb_le->tlb_stackinfo.u_bbr.flex3 =
htole32(tlb->tlb_stackinfo.u_bbr.flex3);
tlb_le->tlb_stackinfo.u_bbr.flex4 =
htole32(tlb->tlb_stackinfo.u_bbr.flex4);
tlb_le->tlb_stackinfo.u_bbr.flex5 =
htole32(tlb->tlb_stackinfo.u_bbr.flex5);
tlb_le->tlb_stackinfo.u_bbr.flex6 =
htole32(tlb->tlb_stackinfo.u_bbr.flex6);
tlb_le->tlb_stackinfo.u_bbr.lost =
htole32(tlb->tlb_stackinfo.u_bbr.lost);
tlb_le->tlb_stackinfo.u_bbr.pacing_gain =
htole16(tlb->tlb_stackinfo.u_bbr.pacing_gain);
tlb_le->tlb_stackinfo.u_bbr.cwnd_gain =
htole16(tlb->tlb_stackinfo.u_bbr.cwnd_gain);
tlb_le->tlb_stackinfo.u_bbr.flex7 =
htole16(tlb->tlb_stackinfo.u_bbr.flex7);
tlb_le->tlb_stackinfo.u_bbr.bbr_state =
tlb->tlb_stackinfo.u_bbr.bbr_state;
tlb_le->tlb_stackinfo.u_bbr.bbr_substate =
tlb->tlb_stackinfo.u_bbr.bbr_substate;
tlb_le->tlb_stackinfo.u_bbr.inhpts =
tlb->tlb_stackinfo.u_bbr.inhpts;
tlb_le->tlb_stackinfo.u_bbr.ininput =
tlb->tlb_stackinfo.u_bbr.ininput;
tlb_le->tlb_stackinfo.u_bbr.use_lt_bw =
tlb->tlb_stackinfo.u_bbr.use_lt_bw;
tlb_le->tlb_stackinfo.u_bbr.flex8 =
tlb->tlb_stackinfo.u_bbr.flex8;
tlb_le->tlb_stackinfo.u_bbr.pkt_epoch =
htole32(tlb->tlb_stackinfo.u_bbr.pkt_epoch);
break;
}
tlb_le->tlb_len = htole32(tlb->tlb_len);
#endif