Coverage Report

Created: 2026-10-17 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpcap/sf-pcapng.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3
 *  The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 *
21
 * sf-pcapng.c - pcapng-file-format-specific code from savefile.c
22
 */
23
24
#include <config.h>
25
26
#include <pcap/pcap-inttypes.h>
27
28
#include <errno.h>
29
#include <memory.h>
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include "pcap-int.h"
35
#include "pcap-util.h"
36
37
#include "pcap-common.h"
38
39
#ifdef HAVE_OS_PROTO_H
40
#include "os-proto.h"
41
#endif
42
43
#include "sf-pcapng.h"
44
45
/*
46
 * Block types.
47
 */
48
49
/*
50
 * Common part at the beginning of all blocks.
51
 */
52
struct block_header {
53
  bpf_u_int32 block_type;
54
  bpf_u_int32 total_length;
55
};
56
57
/*
58
 * Common trailer at the end of all blocks.
59
 */
60
struct block_trailer {
61
  bpf_u_int32 total_length;
62
};
63
64
/*
65
 * Common options.
66
 */
67
280
#define OPT_ENDOFOPT  0  /* end of options */
68
#define OPT_COMMENT 1 /* comment string */
69
70
/*
71
 * Option header.
72
 */
73
struct option_header {
74
  u_short   option_code;
75
  u_short   option_length;
76
};
77
78
/*
79
 * Structures for the part of each block type following the common
80
 * part.
81
 */
82
83
/*
84
 * Section Header Block.
85
 */
86
2.09k
#define BT_SHB      0x0A0D0D0A
87
1.27k
#define BT_SHB_INSANE_MAX       1024U*1024U*1U  /* 1MB should be enough */
88
struct section_header_block {
89
  bpf_u_int32 byte_order_magic;
90
  u_short   major_version;
91
  u_short   minor_version;
92
  uint64_t  section_length;
93
  /* followed by options and trailer */
94
};
95
96
/*
97
 * Byte-order magic value.
98
 */
99
2.26k
#define BYTE_ORDER_MAGIC  0x1A2B3C4D
100
101
/*
102
 * Current version number.  If major_version isn't PCAP_NG_VERSION_MAJOR,
103
 * or if minor_version isn't PCAP_NG_VERSION_MINOR or 2, that means that
104
 * this code can't read the file.
105
 */
106
2.69k
#define PCAP_NG_VERSION_MAJOR 1
107
2.22k
#define PCAP_NG_VERSION_MINOR 0
108
109
/*
110
 * Interface Description Block.
111
 */
112
3.83k
#define BT_IDB      0x00000001
113
114
struct interface_description_block {
115
  u_short   linktype;
116
  u_short   reserved;
117
  bpf_u_int32 snaplen;
118
  /* followed by options and trailer */
119
};
120
121
/*
122
 * Options in the IDB.
123
 */
124
#define IF_NAME   2 /* interface name string */
125
#define IF_DESCRIPTION  3 /* interface description string */
126
#define IF_IPV4ADDR 4 /* interface's IPv4 address and netmask */
127
#define IF_IPV6ADDR 5 /* interface's IPv6 address and prefix length */
128
#define IF_MACADDR  6 /* interface's MAC address */
129
#define IF_EUIADDR  7 /* interface's EUI address */
130
#define IF_SPEED  8 /* interface's speed, in bits/s */
131
1.22k
#define IF_TSRESOL  9  /* interface's time stamp resolution */
132
#define IF_TZONE  10  /* interface's time zone */
133
#define IF_FILTER 11  /* filter used when capturing on interface */
134
#define IF_OS   12  /* string OS on which capture on this interface was done */
135
#define IF_FCSLEN 13  /* FCS length for this interface */
136
408
#define IF_TSOFFSET 14  /* time stamp offset for this interface */
137
138
/*
139
 * Enhanced Packet Block.
140
 */
141
476
#define BT_EPB      0x00000006
142
143
struct enhanced_packet_block {
144
  bpf_u_int32 interface_id;
145
  bpf_u_int32 timestamp_high;
146
  bpf_u_int32 timestamp_low;
147
  bpf_u_int32 caplen;
148
  bpf_u_int32 len;
149
  /* followed by packet data, options, and trailer */
150
};
151
152
/*
153
 * Simple Packet Block.
154
 */
155
1.60k
#define BT_SPB      0x00000003
156
157
struct simple_packet_block {
158
  bpf_u_int32 len;
159
  /* followed by packet data and trailer */
160
};
161
162
/*
163
 * Packet Block.
164
 */
165
473
#define BT_PB     0x00000002
166
167
struct packet_block {
168
  u_short   interface_id;
169
  u_short   drops_count;
170
  bpf_u_int32 timestamp_high;
171
  bpf_u_int32 timestamp_low;
172
  bpf_u_int32 caplen;
173
  bpf_u_int32 len;
174
  /* followed by packet data, options, and trailer */
175
};
176
177
/*
178
 * Block cursor - used when processing the contents of a block.
179
 * Contains a pointer into the data being processed and a count
180
 * of bytes remaining in the block.
181
 */
182
struct block_cursor {
183
  u_char    *data;
184
  size_t    data_remaining;
185
  bpf_u_int32 block_type;
186
};
187
188
typedef enum {
189
  PASS_THROUGH,
190
  SCALE_UP_DEC,
191
  SCALE_DOWN_DEC,
192
  SCALE_UP_BIN,
193
  SCALE_DOWN_BIN
194
} tstamp_scale_type_t;
195
196
/*
197
 * Per-interface information.
198
 */
199
struct pcap_ng_if {
200
  uint32_t snaplen;   /* snapshot length */
201
  uint64_t tsresol;   /* time stamp resolution */
202
  tstamp_scale_type_t scale_type; /* how to scale */
203
  uint64_t scale_factor;    /* time stamp scale factor for power-of-10 tsresol */
204
  int64_t tsoffset;   /* time stamp offset */
205
};
206
207
/*
208
 * Per-pcap_t private data.
209
 *
210
 * max_blocksize is the maximum size of a block that we'll accept.  We
211
 * reject blocks bigger than this, so we don't consume too much memory
212
 * with a truly huge block.  It can change as we see IDBs with different
213
 * link-layer header types.  (Currently, we don't support IDBs with
214
 * different link-layer header types, but we will support it in the
215
 * future, when we offer file-reading APIs that support it.)
216
 *
217
 * XXX - that's an issue on ILP32 platforms, where the maximum block
218
 * size of 2^31-1 would eat all but one byte of the entire address space.
219
 * It's less of an issue on ILP64/LLP64 platforms, but the actual size
220
 * of the address space may be limited by 1) the number of *significant*
221
 * address bits (currently, x86-64 only supports 48 bits of address), 2)
222
 * any limitations imposed by the operating system; 3) any limitations
223
 * imposed by the amount of available backing store for anonymous pages,
224
 * so we impose a limit regardless of the size of a pointer.
225
 */
226
struct pcap_ng_sf {
227
  uint64_t user_tsresol;    /* time stamp resolution requested by the user */
228
  u_int max_blocksize;    /* don't grow buffer size past this */
229
  bpf_u_int32 ifcount;    /* number of interfaces seen in this capture */
230
  bpf_u_int32 ifaces_size;  /* size of array below */
231
  struct pcap_ng_if *ifaces;  /* array of interface information */
232
};
233
234
/*
235
 * The maximum block size we start with; we use an arbitrary value of
236
 * 16 MiB.
237
 */
238
1.18k
#define INITIAL_MAX_BLOCKSIZE (16*1024*1024)
239
240
/*
241
 * Maximum block size for a given maximum snapshot length; we define it
242
 * as the size of an EPB with a max_snaplen-sized packet and 128KB of
243
 * options.
244
 */
245
#define MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen) \
246
872
  (sizeof (struct block_header) + \
247
872
   sizeof (struct enhanced_packet_block) + \
248
872
   (max_snaplen) + 131072 + \
249
872
   sizeof (struct block_trailer))
250
251
static void pcap_ng_cleanup(pcap_t *p);
252
static int pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr,
253
    u_char **data);
254
255
static int
256
read_bytes(FILE *fp, void *buf, size_t bytes_to_read, int fail_on_eof,
257
    char *errbuf)
258
17.9k
{
259
17.9k
  size_t amt_read;
260
261
17.9k
  amt_read = fread(buf, 1, bytes_to_read, fp);
262
17.9k
  if (amt_read != bytes_to_read) {
263
515
    if (ferror(fp)) {
264
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
265
0
          errno, "error reading dump file");
266
515
    } else {
267
515
      if (amt_read == 0 && !fail_on_eof)
268
371
        return (0);  /* EOF */
269
144
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
270
144
          "truncated pcapng dump file; tried to read %zu bytes, only got %zu",
271
144
          bytes_to_read, amt_read);
272
144
    }
273
144
    return (-1);
274
515
  }
275
17.4k
  return (1);
276
17.9k
}
277
278
static int
279
read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
280
8.63k
{
281
8.63k
  struct pcap_ng_sf *ps;
282
8.63k
  int status;
283
8.63k
  struct block_header bhdr;
284
8.63k
  struct block_trailer *btrlr;
285
8.63k
  u_char *bdata;
286
8.63k
  size_t data_remaining;
287
288
8.63k
  ps = p->priv;
289
290
8.63k
  status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
291
8.63k
  if (status <= 0)
292
413
    return (status);  /* error or EOF */
293
294
8.22k
  if (p->swapped) {
295
3.41k
    bhdr.block_type = PCAP_BSWAP_32(bhdr.block_type);
296
3.41k
    bhdr.total_length = PCAP_BSWAP_32(bhdr.total_length);
297
3.41k
  }
298
299
  /*
300
   * Is this block "too small" - i.e., is it shorter than a block
301
   * header plus a block trailer?
302
   */
303
8.22k
  if (bhdr.total_length < sizeof(struct block_header) +
304
8.22k
      sizeof(struct block_trailer)) {
305
7
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
306
7
        "block in pcapng dump file has a length of %u < %zu",
307
7
        bhdr.total_length,
308
7
        sizeof(struct block_header) + sizeof(struct block_trailer));
309
7
    return (-1);
310
7
  }
311
312
  /*
313
   * Is the block total length a multiple of 4?
314
   */
315
8.21k
  if ((bhdr.total_length % 4) != 0) {
316
    /*
317
     * No.  Report that as an error.
318
     */
319
17
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
320
17
        "block in pcapng dump file has a length of %u that is not a multiple of 4",
321
17
        bhdr.total_length);
322
17
    return (-1);
323
17
  }
324
325
  /*
326
   * Is the buffer big enough?
327
   */
328
8.19k
  if (p->bufsize < bhdr.total_length) {
329
    /*
330
     * No - make it big enough, unless it's too big, in
331
     * which case we fail.
332
     */
333
125
    if (bhdr.total_length > ps->max_blocksize) {
334
51
      snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcapng block size %u > maximum %u", bhdr.total_length,
335
51
          ps->max_blocksize);
336
51
      return (-1);
337
51
    }
338
74
    if (!pcapint_grow_buffer(p, bhdr.total_length, errbuf))
339
0
      return (-1);
340
74
  }
341
342
  /*
343
   * Copy the stuff we've read to the buffer, and read the rest
344
   * of the block.
345
   */
346
8.14k
  memcpy(p->buffer, &bhdr, sizeof(bhdr));
347
8.14k
  bdata = p->buffer + sizeof(bhdr);
348
8.14k
  data_remaining = bhdr.total_length - sizeof(bhdr);
349
8.14k
  if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
350
50
    return (-1);
351
352
  /*
353
   * Get the block size from the trailer.
354
   */
355
8.09k
  btrlr = (struct block_trailer *)(bdata + data_remaining - sizeof (struct block_trailer));
356
8.09k
  if (p->swapped)
357
3.37k
    btrlr->total_length = PCAP_BSWAP_32(btrlr->total_length);
358
359
  /*
360
   * Is the total length from the trailer the same as the total
361
   * length from the header?
362
   */
363
8.09k
  if (bhdr.total_length != btrlr->total_length) {
364
    /*
365
     * No.
366
     */
367
54
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
368
54
        "block total length in header %u and trailer %u don't match",
369
54
        bhdr.total_length, btrlr->total_length);
370
54
    return (-1);
371
54
  }
372
373
  /*
374
   * Initialize the cursor.
375
   */
376
8.04k
  cursor->data = bdata;
377
8.04k
  cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
378
8.04k
  cursor->block_type = bhdr.block_type;
379
8.04k
  return (1);
380
8.09k
}
381
382
static void *
383
get_from_block_data(struct block_cursor *cursor, size_t chunk_size,
384
    char *errbuf)
385
15.2k
{
386
15.2k
  void *data;
387
388
  /*
389
   * Make sure we have the specified amount of data remaining in
390
   * the block data.
391
   */
392
15.2k
  if (cursor->data_remaining < chunk_size) {
393
145
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
394
145
        "block of type %u in pcapng dump file is too short",
395
145
        cursor->block_type);
396
145
    return (NULL);
397
145
  }
398
399
  /*
400
   * Return the current pointer, and skip past the chunk.
401
   */
402
15.0k
  data = cursor->data;
403
15.0k
  cursor->data += chunk_size;
404
15.0k
  cursor->data_remaining -= chunk_size;
405
15.0k
  return (data);
406
15.2k
}
407
408
static struct option_header *
409
get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
410
2.93k
{
411
2.93k
  struct option_header *opthdr;
412
413
2.93k
  opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
414
2.93k
  if (opthdr == NULL) {
415
    /*
416
     * Option header is cut short.
417
     */
418
0
    return (NULL);
419
0
  }
420
421
  /*
422
   * Byte-swap it if necessary.
423
   */
424
2.93k
  if (p->swapped) {
425
977
    opthdr->option_code = PCAP_BSWAP_16(opthdr->option_code);
426
977
    opthdr->option_length = PCAP_BSWAP_16(opthdr->option_length);
427
977
  }
428
429
2.93k
  return (opthdr);
430
2.93k
}
431
432
static void *
433
get_optvalue_from_block_data(struct block_cursor *cursor,
434
    struct option_header *opthdr, char *errbuf)
435
2.93k
{
436
2.93k
  size_t padded_option_len;
437
2.93k
  void *optvalue;
438
439
  /* Pad option length to 4-byte boundary */
440
2.93k
  padded_option_len = opthdr->option_length;
441
2.93k
  padded_option_len = ((padded_option_len + 3)/4)*4;
442
443
2.93k
  optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
444
2.93k
  if (optvalue == NULL) {
445
    /*
446
     * Option value is cut short.
447
     */
448
30
    return (NULL);
449
30
  }
450
451
2.90k
  return (optvalue);
452
2.93k
}
453
454
static int
455
process_idb_options(pcap_t *p, struct block_cursor *cursor, uint64_t *tsresol,
456
    int64_t *tsoffset, int *is_binary, char *errbuf)
457
3.77k
{
458
3.77k
  struct option_header *opthdr;
459
3.77k
  void *optvalue;
460
3.77k
  int saw_tsresol, saw_tsoffset;
461
3.77k
  uint8_t tsresol_opt;
462
3.77k
  u_int i;
463
464
3.77k
  saw_tsresol = 0;
465
3.77k
  saw_tsoffset = 0;
466
6.37k
  while (cursor->data_remaining != 0) {
467
    /*
468
     * Get the option header.
469
     */
470
2.93k
    opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
471
2.93k
    if (opthdr == NULL) {
472
      /*
473
       * Option header is cut short.
474
       */
475
0
      return (-1);
476
0
    }
477
478
    /*
479
     * Get option value.
480
     */
481
2.93k
    optvalue = get_optvalue_from_block_data(cursor, opthdr,
482
2.93k
        errbuf);
483
2.93k
    if (optvalue == NULL) {
484
      /*
485
       * Option value is cut short.
486
       */
487
30
      return (-1);
488
30
    }
489
490
2.90k
    switch (opthdr->option_code) {
491
492
280
    case OPT_ENDOFOPT:
493
280
      if (opthdr->option_length != 0) {
494
25
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
495
25
            "Interface Description Block has opt_endofopt option with length %u != 0",
496
25
            opthdr->option_length);
497
25
        return (-1);
498
25
      }
499
255
      goto done;
500
501
1.22k
    case IF_TSRESOL:
502
1.22k
      if (opthdr->option_length != 1) {
503
8
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
504
8
            "Interface Description Block has if_tsresol option with length %u != 1",
505
8
            opthdr->option_length);
506
8
        return (-1);
507
8
      }
508
1.22k
      if (saw_tsresol) {
509
1
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
510
1
            "Interface Description Block has more than one if_tsresol option");
511
1
        return (-1);
512
1
      }
513
1.22k
      saw_tsresol = 1;
514
1.22k
      memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
515
1.22k
      if (tsresol_opt & 0x80) {
516
        /*
517
         * Resolution is negative power of 2.
518
         */
519
482
        uint8_t tsresol_shift = (tsresol_opt & 0x7F);
520
521
482
        if (tsresol_shift > 63) {
522
          /*
523
           * Resolution is too high; 2^-{res}
524
           * won't fit in a 64-bit value.
525
           */
526
7
          snprintf(errbuf, PCAP_ERRBUF_SIZE,
527
7
              "Interface Description Block if_tsresol option resolution 2^-%u is too high",
528
7
              tsresol_shift);
529
7
          return (-1);
530
7
        }
531
475
        *is_binary = 1;
532
475
        *tsresol = ((uint64_t)1) << tsresol_shift;
533
738
      } else {
534
        /*
535
         * Resolution is negative power of 10.
536
         */
537
738
        if (tsresol_opt > 19) {
538
          /*
539
           * Resolution is too high; 2^-{res}
540
           * won't fit in a 64-bit value (the
541
           * largest power of 10 that fits
542
           * in a 64-bit value is 10^19, as
543
           * the largest 64-bit unsigned
544
           * value is ~1.8*10^19).
545
           */
546
1
          snprintf(errbuf, PCAP_ERRBUF_SIZE,
547
1
              "Interface Description Block if_tsresol option resolution 10^-%u is too high",
548
1
              tsresol_opt);
549
1
          return (-1);
550
1
        }
551
737
        *is_binary = 0;
552
737
        *tsresol = 1;
553
5.34k
        for (i = 0; i < tsresol_opt; i++)
554
4.61k
          *tsresol *= 10;
555
737
      }
556
1.21k
      break;
557
558
1.21k
    case IF_TSOFFSET:
559
408
      if (opthdr->option_length != 8) {
560
14
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
561
14
            "Interface Description Block has if_tsoffset option with length %u != 8",
562
14
            opthdr->option_length);
563
14
        return (-1);
564
14
      }
565
394
      if (saw_tsoffset) {
566
1
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
567
1
            "Interface Description Block has more than one if_tsoffset option");
568
1
        return (-1);
569
1
      }
570
393
      saw_tsoffset = 1;
571
393
      memcpy(tsoffset, optvalue, sizeof(*tsoffset));
572
393
      if (p->swapped)
573
197
        *tsoffset = PCAP_BSWAP_64(*tsoffset);
574
393
      break;
575
576
986
    default:
577
986
      break;
578
2.90k
    }
579
2.90k
  }
580
581
3.69k
done:
582
3.69k
  return (0);
583
3.77k
}
584
585
static int
586
add_interface(pcap_t *p, struct interface_description_block *idbp,
587
    struct block_cursor *cursor, char *errbuf)
588
3.77k
{
589
3.77k
  struct pcap_ng_sf *ps;
590
3.77k
  bpf_u_int32 new_ifcount;
591
3.77k
  bpf_u_int32 new_if_index;
592
3.77k
  uint64_t tsresol;
593
3.77k
  int64_t tsoffset;
594
3.77k
  int is_binary;
595
596
3.77k
  ps = p->priv;
597
598
  /*
599
   * Count this interface.
600
   */
601
3.77k
  new_ifcount = ps->ifcount + 1;
602
603
  /*
604
   * The array of interfaces is 0-based, so the current number of
605
   * interfaces will be the index of the new interface.
606
   */
607
3.77k
  new_if_index = ps->ifcount;
608
609
  /*
610
   * Grow the array of per-interface information as necessary.
611
   */
612
3.77k
  if (new_ifcount > ps->ifaces_size) {
613
    /*
614
     * We need to grow the array.
615
     */
616
1.29k
    bpf_u_int32 new_ifaces_size;
617
1.29k
    struct pcap_ng_if *new_ifaces;
618
619
1.29k
    if (ps->ifaces_size == 0) {
620
      /*
621
       * It's currently empty.
622
       *
623
       * (The Clang static analyzer doesn't do enough,
624
       * err, umm, dataflow *analysis* to realize that
625
       * ps->ifaces_size == 0 if ps->ifaces == NULL,
626
       * and so complains about a possible zero argument
627
       * to realloc(), so we check for the former
628
       * condition to shut it up.
629
       *
630
       * However, it doesn't complain that one of the
631
       * multiplications below could overflow, which is
632
       * a real, albeit extremely unlikely, problem (you'd
633
       * need a pcapng file with tens of millions of
634
       * interfaces).)
635
       */
636
906
      new_ifaces_size = 1;
637
906
      new_ifaces = malloc(sizeof (struct pcap_ng_if));
638
906
    } else {
639
      /*
640
       * It's not currently empty; double its size.
641
       * (Perhaps overkill once we have a lot of interfaces.)
642
       *
643
       * Check for overflow if we double it.
644
       */
645
392
      if (ps->ifaces_size * 2 < ps->ifaces_size) {
646
        /*
647
         * The maximum number of interfaces before
648
         * ps->ifaces_size overflows is the largest
649
         * possible 32-bit power of 2, as we do
650
         * size doubling.
651
         */
652
0
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
653
0
            "more than %u interfaces in the file",
654
0
            0x80000000U);
655
0
        return (0);
656
0
      }
657
658
      /*
659
       * ps->ifaces_size * 2 doesn't overflow, so it's
660
       * safe to multiply.
661
       */
662
392
      new_ifaces_size = ps->ifaces_size * 2;
663
664
      /*
665
       * Now make sure that's not so big that it overflows
666
       * if we multiply by sizeof (struct pcap_ng_if).
667
       *
668
       * That can happen on 32-bit platforms, with a 32-bit
669
       * size_t; it shouldn't happen on 64-bit platforms,
670
       * with a 64-bit size_t, as new_ifaces_size is
671
       * 32 bits.
672
       */
673
392
      if (new_ifaces_size * sizeof (struct pcap_ng_if) < new_ifaces_size) {
674
        /*
675
         * As this fails only with 32-bit size_t,
676
         * the multiplication was 32x32->32, and
677
         * the largest 32-bit value that can safely
678
         * be multiplied by sizeof (struct pcap_ng_if)
679
         * without overflow is the largest 32-bit
680
         * (unsigned) value divided by
681
         * sizeof (struct pcap_ng_if).
682
         */
683
0
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
684
0
            "more than %u interfaces in the file",
685
0
            0xFFFFFFFFU / ((u_int)sizeof (struct pcap_ng_if)));
686
0
        return (0);
687
0
      }
688
392
      new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
689
392
    }
690
1.29k
    if (new_ifaces == NULL) {
691
      /*
692
       * We ran out of memory.
693
       * Give up.
694
       */
695
0
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
696
0
          "out of memory for per-interface information (%u interfaces)",
697
0
          new_ifcount);
698
0
      return (0);
699
0
    }
700
1.29k
    ps->ifaces_size = new_ifaces_size;
701
1.29k
    ps->ifaces = new_ifaces;
702
1.29k
  }
703
704
3.77k
  ps->ifaces[new_if_index].snaplen = idbp->snaplen;
705
706
  /*
707
   * Set the default time stamp resolution and offset.
708
   */
709
3.77k
  tsresol = 1000000;  /* microsecond resolution */
710
3.77k
  is_binary = 0;    /* which is a power of 10 */
711
3.77k
  tsoffset = 0;   /* absolute timestamps */
712
713
  /*
714
   * Now look for various time stamp options, so we know
715
   * how to interpret the time stamps for this interface.
716
   */
717
3.77k
  if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
718
3.77k
      errbuf) == -1)
719
87
    return (0);
720
721
3.69k
  ps->ifaces[new_if_index].tsresol = tsresol;
722
3.69k
  ps->ifaces[new_if_index].tsoffset = tsoffset;
723
724
  /*
725
   * Determine whether we're scaling up or down or not
726
   * at all for this interface.
727
   */
728
3.69k
  if (tsresol == ps->user_tsresol) {
729
    /*
730
     * The resolution is the resolution the user wants,
731
     * so we don't have to do scaling.
732
     */
733
2.48k
    ps->ifaces[new_if_index].scale_type = PASS_THROUGH;
734
2.48k
  } else if (tsresol > ps->user_tsresol) {
735
    /*
736
     * The resolution is greater than what the user wants,
737
     * so we have to scale the timestamps down.
738
     */
739
578
    if (is_binary)
740
265
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_BIN;
741
313
    else {
742
      /*
743
       * Calculate the scale factor.
744
       */
745
313
      ps->ifaces[new_if_index].scale_factor = tsresol/ps->user_tsresol;
746
313
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_DEC;
747
313
    }
748
630
  } else {
749
    /*
750
     * The resolution is less than what the user wants,
751
     * so we have to scale the timestamps up.
752
     */
753
630
    if (is_binary)
754
209
      ps->ifaces[new_if_index].scale_type = SCALE_UP_BIN;
755
421
    else {
756
      /*
757
       * Calculate the scale factor.
758
       */
759
421
      ps->ifaces[new_if_index].scale_factor = ps->user_tsresol/tsresol;
760
421
      ps->ifaces[new_if_index].scale_type = SCALE_UP_DEC;
761
421
    }
762
630
  }
763
764
  /*
765
   * We've succeeded, so update the interface count.
766
   */
767
3.69k
  ps->ifcount = new_ifcount;
768
3.69k
  return (1);
769
3.77k
}
770
771
/*
772
 * Check whether this is a pcapng savefile and, if it is, extract the
773
 * relevant information from the header.
774
 */
775
pcap_t *
776
pcap_ng_check_header(const uint8_t *magic, FILE *fp, u_int precision,
777
    char *errbuf, int *err)
778
1.53k
{
779
1.53k
  bpf_u_int32 magic_int;
780
1.53k
  size_t amt_read;
781
1.53k
  bpf_u_int32 total_length;
782
1.53k
  bpf_u_int32 byte_order_magic;
783
1.53k
  struct block_header *bhdrp;
784
1.53k
  struct section_header_block *shbp;
785
1.53k
  pcap_t *p;
786
1.53k
  int swapped = 0;
787
1.53k
  struct pcap_ng_sf *ps;
788
1.53k
  int status;
789
1.53k
  struct block_cursor cursor;
790
1.53k
  struct interface_description_block *idbp;
791
792
  /*
793
   * Assume no read errors.
794
   */
795
1.53k
  *err = 0;
796
797
  /*
798
   * Check whether the first 4 bytes of the file are the block
799
   * type for a pcapng savefile.
800
   */
801
1.53k
  memcpy(&magic_int, magic, sizeof(magic_int));
802
1.53k
  if (magic_int != BT_SHB) {
803
    /*
804
     * XXX - check whether this looks like what the block
805
     * type would be after being munged by mapping between
806
     * UN*X and DOS/Windows text file format and, if it
807
     * does, look for the byte-order magic number in
808
     * the appropriate place and, if we find it, report
809
     * this as possibly being a pcapng file transferred
810
     * between UN*X and Windows in text file format?
811
     */
812
211
    return (NULL);  /* nope */
813
211
  }
814
815
  /*
816
   * OK, they are.  However, that's just \n\r\r\n, so it could,
817
   * conceivably, be an ordinary text file.
818
   *
819
   * It could not, however, conceivably be any other type of
820
   * capture file, so we can read the rest of the putative
821
   * Section Header Block; put the block type in the common
822
   * header, read the rest of the common header and the
823
   * fixed-length portion of the SHB, and look for the byte-order
824
   * magic value.
825
   */
826
1.32k
  amt_read = fread(&total_length, 1, sizeof(total_length), fp);
827
1.32k
  if (amt_read < sizeof(total_length)) {
828
0
    if (ferror(fp)) {
829
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
830
0
          errno, "error reading dump file");
831
0
      *err = 1;
832
0
      return (NULL);  /* fail */
833
0
    }
834
835
    /*
836
     * Possibly a weird short text file, so just say
837
     * "not pcapng".
838
     */
839
0
    return (NULL);
840
0
  }
841
1.32k
  amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
842
1.32k
  if (amt_read < sizeof(byte_order_magic)) {
843
0
    if (ferror(fp)) {
844
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
845
0
          errno, "error reading dump file");
846
0
      *err = 1;
847
0
      return (NULL);  /* fail */
848
0
    }
849
850
    /*
851
     * Possibly a weird short text file, so just say
852
     * "not pcapng".
853
     */
854
0
    return (NULL);
855
0
  }
856
1.32k
  if (byte_order_magic != BYTE_ORDER_MAGIC) {
857
504
    byte_order_magic = PCAP_BSWAP_32(byte_order_magic);
858
504
    if (byte_order_magic != BYTE_ORDER_MAGIC) {
859
      /*
860
       * Not a pcapng file.
861
       */
862
94
      return (NULL);
863
94
    }
864
410
    swapped = 1;
865
410
    total_length = PCAP_BSWAP_32(total_length);
866
410
  }
867
868
  /*
869
   * Check the sanity of the total length.
870
   */
871
1.23k
  if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer) ||
872
1.22k
            (total_length > BT_SHB_INSANE_MAX)) {
873
49
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
874
49
        "Section Header Block in pcapng dump file has invalid length %zu < _%u_ < %u (BT_SHB_INSANE_MAX)",
875
49
        sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer),
876
49
        total_length,
877
49
        BT_SHB_INSANE_MAX);
878
879
49
    *err = 1;
880
49
    return (NULL);
881
49
  }
882
883
  /*
884
   * OK, this is a good pcapng file.
885
   * Allocate a pcap_t for it.
886
   */
887
1.18k
  p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_ng_sf);
888
1.18k
  if (p == NULL) {
889
    /* Allocation failed. */
890
0
    *err = 1;
891
0
    return (NULL);
892
0
  }
893
1.18k
  p->swapped = swapped;
894
1.18k
  ps = p->priv;
895
896
  /*
897
   * What precision does the user want?
898
   */
899
1.18k
  switch (precision) {
900
901
1.18k
  case PCAP_TSTAMP_PRECISION_MICRO:
902
1.18k
    ps->user_tsresol = 1000000;
903
1.18k
    break;
904
905
0
  case PCAP_TSTAMP_PRECISION_NANO:
906
0
    ps->user_tsresol = 1000000000;
907
0
    break;
908
909
0
  default:
910
0
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
911
0
        "unknown time stamp resolution %u", precision);
912
0
    free(p);
913
0
    *err = 1;
914
0
    return (NULL);
915
1.18k
  }
916
917
1.18k
  p->opt.tstamp_precision = precision;
918
919
  /*
920
   * Allocate a buffer into which to read blocks.  We default to
921
   * the maximum of:
922
   *
923
   *  the total length of the SHB for which we read the header;
924
   *
925
   *  2K, which should be more than large enough for an Enhanced
926
   *  Packet Block containing a full-size Ethernet frame, and
927
   *  leaving room for some options.
928
   *
929
   * If we find a bigger block, we reallocate the buffer, up to
930
   * the maximum size.  We start out with a maximum size of
931
   * INITIAL_MAX_BLOCKSIZE; if we see any link-layer header types
932
   * with a maximum snapshot that results in a larger maximum
933
   * block length, we boost the maximum.
934
   */
935
1.18k
  p->bufsize = 2048;
936
1.18k
  if (p->bufsize < total_length)
937
37
    p->bufsize = total_length;
938
1.18k
  p->buffer = malloc(p->bufsize);
939
1.18k
  if (p->buffer == NULL) {
940
0
    snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
941
0
    free(p);
942
0
    *err = 1;
943
0
    return (NULL);
944
0
  }
945
1.18k
  ps->max_blocksize = INITIAL_MAX_BLOCKSIZE;
946
947
  /*
948
   * Copy the stuff we've read to the buffer, and read the rest
949
   * of the SHB.
950
   */
951
1.18k
  bhdrp = (struct block_header *)p->buffer;
952
1.18k
  shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header));
953
1.18k
  bhdrp->block_type = magic_int;
954
1.18k
  bhdrp->total_length = total_length;
955
1.18k
  shbp->byte_order_magic = byte_order_magic;
956
1.18k
  if (read_bytes(fp,
957
1.18k
      p->buffer + (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
958
1.18k
      total_length - (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
959
1.18k
      1, errbuf) == -1)
960
52
    goto fail;
961
962
1.13k
  if (p->swapped) {
963
    /*
964
     * Byte-swap the fields we've read.
965
     */
966
391
    shbp->major_version = PCAP_BSWAP_16(shbp->major_version);
967
391
    shbp->minor_version = PCAP_BSWAP_16(shbp->minor_version);
968
969
    /*
970
     * XXX - we don't care about the section length.
971
     */
972
391
  }
973
  /* Currently only SHB versions 1.0 and 1.2 are supported;
974
     version 1.2 is treated as being the same as version 1.0.
975
     See the current version of the pcapng specification.
976
977
     Version 1.2 is written by some programs that write additional
978
     block types (which can be read by any code that handles them,
979
     regardless of whether the minor version if 0 or 2, so that's
980
     not a reason to change the minor version number).
981
982
     XXX - the pcapng specification says that readers should
983
     just ignore sections with an unsupported version number;
984
     presumably they can also report an error if they skip
985
     all the way to the end of the file without finding
986
     any versions that they support. */
987
1.13k
  if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
988
1.11k
         (shbp->minor_version == PCAP_NG_VERSION_MINOR ||
989
60
          shbp->minor_version == 2))) {
990
38
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
991
38
        "unsupported pcapng savefile version %u.%u",
992
38
        shbp->major_version, shbp->minor_version);
993
38
    goto fail;
994
38
  }
995
1.09k
  p->version_major = shbp->major_version;
996
1.09k
  p->version_minor = shbp->minor_version;
997
998
  /*
999
   * Save the time stamp resolution the user requested.
1000
   */
1001
1.09k
  p->opt.tstamp_precision = precision;
1002
1003
  /*
1004
   * Now start looking for an Interface Description Block.
1005
   */
1006
1.85k
  for (;;) {
1007
    /*
1008
     * Read the next block.
1009
     */
1010
1.85k
    status = read_block(fp, p, &cursor, errbuf);
1011
1.85k
    if (status == 0) {
1012
      /* EOF - no IDB in this file */
1013
16
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
1014
16
          "the capture file has no Interface Description Blocks");
1015
16
      goto fail;
1016
16
    }
1017
1.83k
    if (status == -1)
1018
165
      goto fail;  /* error */
1019
1.66k
    switch (cursor.block_type) {
1020
1021
908
    case BT_IDB:
1022
      /*
1023
       * Get a pointer to the fixed-length portion of the
1024
       * IDB.
1025
       */
1026
908
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1027
908
          errbuf);
1028
908
      if (idbp == NULL)
1029
2
        goto fail;  /* error */
1030
1031
      /*
1032
       * Byte-swap it if necessary.
1033
       */
1034
906
      if (p->swapped) {
1035
323
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1036
323
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1037
323
      }
1038
1039
      /*
1040
       * Try to add this interface.
1041
       */
1042
906
      if (!add_interface(p, idbp, &cursor, errbuf))
1043
85
        goto fail;
1044
1045
821
      goto done;
1046
1047
821
    case BT_EPB:
1048
2
    case BT_SPB:
1049
3
    case BT_PB:
1050
      /*
1051
       * Saw a packet before we saw any IDBs.  That's
1052
       * not valid, as we don't know what link-layer
1053
       * encapsulation the packet has.
1054
       */
1055
3
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
1056
3
          "the capture file has a packet block before any Interface Description Blocks");
1057
3
      goto fail;
1058
1059
758
    default:
1060
      /*
1061
       * Just ignore it.
1062
       */
1063
758
      break;
1064
1.66k
    }
1065
1.66k
  }
1066
1067
821
done:
1068
821
  p->linktype = linktype_to_dlt(idbp->linktype);
1069
821
  p->snapshot = pcapint_adjust_snapshot(p->linktype, idbp->snaplen);
1070
821
  p->linktype_ext = 0;
1071
1072
  /*
1073
   * If the maximum block size for a packet with the maximum
1074
   * snapshot length for this DLT_ is bigger than the current
1075
   * maximum block size, increase the maximum.
1076
   */
1077
821
  if (MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
1078
51
    ps->max_blocksize = MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype));
1079
1080
821
  p->next_packet_op = pcap_ng_next_packet;
1081
821
  p->cleanup_op = pcap_ng_cleanup;
1082
1083
821
  return (p);
1084
1085
361
fail:
1086
361
  free(ps->ifaces);
1087
361
  free(p->buffer);
1088
361
  free(p);
1089
361
  *err = 1;
1090
361
  return (NULL);
1091
1.09k
}
1092
1093
static void
1094
pcap_ng_cleanup(pcap_t *p)
1095
821
{
1096
821
  struct pcap_ng_sf *ps = p->priv;
1097
1098
821
  free(ps->ifaces);
1099
821
  pcapint_sf_cleanup(p);
1100
821
}
1101
1102
/*
1103
 * Read and return the next packet from the savefile.  Return the header
1104
 * in hdr and a pointer to the contents in data.  Return 1 on success, 0
1105
 * if there were no more packets, and -1 on an error.
1106
 */
1107
static int
1108
pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
1109
3.15k
{
1110
3.15k
  struct pcap_ng_sf *ps = p->priv;
1111
3.15k
  struct block_cursor cursor;
1112
3.15k
  int status;
1113
3.15k
  struct enhanced_packet_block *epbp;
1114
3.15k
  struct simple_packet_block *spbp;
1115
3.15k
  struct packet_block *pbp;
1116
3.15k
  bpf_u_int32 interface_id = 0xFFFFFFFF;
1117
3.15k
  struct interface_description_block *idbp;
1118
3.15k
  struct section_header_block *shbp;
1119
3.15k
  FILE *fp = p->rfile;
1120
3.15k
  uint64_t t, sec, frac;
1121
1122
  /*
1123
   * Look for an Enhanced Packet Block, a Simple Packet Block,
1124
   * or a Packet Block.
1125
   */
1126
6.78k
  for (;;) {
1127
    /*
1128
     * Read the block type and length; those are common
1129
     * to all blocks.
1130
     */
1131
6.78k
    status = read_block(fp, p, &cursor, p->errbuf);
1132
6.78k
    if (status == 0)
1133
355
      return (0);  /* EOF */
1134
6.42k
    if (status == -1)
1135
56
      return (-1);  /* error */
1136
6.37k
    switch (cursor.block_type) {
1137
1138
475
    case BT_EPB:
1139
      /*
1140
       * Get a pointer to the fixed-length portion of the
1141
       * EPB.
1142
       */
1143
475
      epbp = get_from_block_data(&cursor, sizeof(*epbp),
1144
475
          p->errbuf);
1145
475
      if (epbp == NULL)
1146
4
        return (-1);  /* error */
1147
1148
      /*
1149
       * Byte-swap it if necessary.
1150
       */
1151
471
      if (p->swapped) {
1152
        /* these were written in opposite byte order */
1153
226
        interface_id = PCAP_BSWAP_32(epbp->interface_id);
1154
226
        hdr->caplen = PCAP_BSWAP_32(epbp->caplen);
1155
226
        hdr->len = PCAP_BSWAP_32(epbp->len);
1156
226
        t = ((uint64_t)PCAP_BSWAP_32(epbp->timestamp_high)) << 32 |
1157
226
            PCAP_BSWAP_32(epbp->timestamp_low);
1158
245
      } else {
1159
245
        interface_id = epbp->interface_id;
1160
245
        hdr->caplen = epbp->caplen;
1161
245
        hdr->len = epbp->len;
1162
245
        t = ((uint64_t)epbp->timestamp_high) << 32 |
1163
245
            epbp->timestamp_low;
1164
245
      }
1165
471
      goto found;
1166
1167
1.59k
    case BT_SPB:
1168
      /*
1169
       * Get a pointer to the fixed-length portion of the
1170
       * SPB.
1171
       */
1172
1.59k
      spbp = get_from_block_data(&cursor, sizeof(*spbp),
1173
1.59k
          p->errbuf);
1174
1.59k
      if (spbp == NULL)
1175
1
        return (-1);  /* error */
1176
1177
      /*
1178
       * SPB packets are assumed to have arrived on
1179
       * the first interface.
1180
       */
1181
1.59k
      interface_id = 0;
1182
1183
      /*
1184
       * Byte-swap it if necessary.
1185
       */
1186
1.59k
      if (p->swapped) {
1187
        /* these were written in opposite byte order */
1188
910
        hdr->len = PCAP_BSWAP_32(spbp->len);
1189
910
      } else
1190
687
        hdr->len = spbp->len;
1191
1192
      /*
1193
       * The SPB doesn't give the captured length;
1194
       * it's the minimum of the snapshot length
1195
       * and the packet length.
1196
       */
1197
1.59k
      hdr->caplen = hdr->len;
1198
1.59k
      if (hdr->caplen > (bpf_u_int32)p->snapshot)
1199
556
        hdr->caplen = p->snapshot;
1200
1.59k
      t = 0;  /* no time stamps */
1201
1.59k
      goto found;
1202
1203
470
    case BT_PB:
1204
      /*
1205
       * Get a pointer to the fixed-length portion of the
1206
       * PB.
1207
       */
1208
470
      pbp = get_from_block_data(&cursor, sizeof(*pbp),
1209
470
          p->errbuf);
1210
470
      if (pbp == NULL)
1211
4
        return (-1);  /* error */
1212
1213
      /*
1214
       * Byte-swap it if necessary.
1215
       */
1216
466
      if (p->swapped) {
1217
        /* these were written in opposite byte order */
1218
205
        interface_id = PCAP_BSWAP_16(pbp->interface_id);
1219
205
        hdr->caplen = PCAP_BSWAP_32(pbp->caplen);
1220
205
        hdr->len = PCAP_BSWAP_32(pbp->len);
1221
205
        t = ((uint64_t)PCAP_BSWAP_32(pbp->timestamp_high)) << 32 |
1222
205
            PCAP_BSWAP_32(pbp->timestamp_low);
1223
261
      } else {
1224
261
        interface_id = pbp->interface_id;
1225
261
        hdr->caplen = pbp->caplen;
1226
261
        hdr->len = pbp->len;
1227
261
        t = ((uint64_t)pbp->timestamp_high) << 32 |
1228
261
            pbp->timestamp_low;
1229
261
      }
1230
466
      goto found;
1231
1232
2.93k
    case BT_IDB:
1233
      /*
1234
       * Interface Description Block.  Get a pointer
1235
       * to its fixed-length portion.
1236
       */
1237
2.93k
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1238
2.93k
          p->errbuf);
1239
2.93k
      if (idbp == NULL)
1240
1
        return (-1);  /* error */
1241
1242
      /*
1243
       * Byte-swap it if necessary.
1244
       */
1245
2.92k
      if (p->swapped) {
1246
922
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1247
922
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1248
922
      }
1249
1250
      /*
1251
       * If the link-layer type or snapshot length
1252
       * differ from the ones for the first IDB we
1253
       * saw, quit.
1254
       *
1255
       * XXX - just discard packets from those
1256
       * interfaces?
1257
       */
1258
2.92k
      if (p->linktype != idbp->linktype) {
1259
2
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1260
2
            "an interface has a type %u different from the type of the first interface",
1261
2
            idbp->linktype);
1262
2
        return (-1);
1263
2
      }
1264
1265
      /*
1266
       * Check against the *adjusted* value of this IDB's
1267
       * snapshot length.
1268
       */
1269
2.92k
      if ((bpf_u_int32)p->snapshot !=
1270
2.92k
          pcapint_adjust_snapshot(p->linktype, idbp->snaplen)) {
1271
54
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1272
54
            "an interface has a snapshot length %u different from the snapshot length of the first interface",
1273
54
            idbp->snaplen);
1274
54
        return (-1);
1275
54
      }
1276
1277
      /*
1278
       * Try to add this interface.
1279
       */
1280
2.87k
      if (!add_interface(p, idbp, &cursor, p->errbuf))
1281
2
        return (-1);
1282
2.87k
      break;
1283
1284
2.87k
    case BT_SHB:
1285
      /*
1286
       * Section Header Block.  Get a pointer
1287
       * to its fixed-length portion.
1288
       */
1289
559
      shbp = get_from_block_data(&cursor, sizeof(*shbp),
1290
559
          p->errbuf);
1291
559
      if (shbp == NULL)
1292
3
        return (-1);  /* error */
1293
1294
      /*
1295
       * Assume the byte order of this section is
1296
       * the same as that of the previous section.
1297
       * We'll check for that later.
1298
       */
1299
556
      if (p->swapped) {
1300
231
        shbp->byte_order_magic =
1301
231
            PCAP_BSWAP_32(shbp->byte_order_magic);
1302
231
        shbp->major_version =
1303
231
            PCAP_BSWAP_16(shbp->major_version);
1304
231
      }
1305
1306
      /*
1307
       * Make sure the byte order doesn't change;
1308
       * pcap_is_swapped() shouldn't change its
1309
       * return value in the middle of reading a capture.
1310
       */
1311
556
      switch (shbp->byte_order_magic) {
1312
1313
431
      case BYTE_ORDER_MAGIC:
1314
        /*
1315
         * OK.
1316
         */
1317
431
        break;
1318
1319
1
      case PCAP_BSWAP_32(BYTE_ORDER_MAGIC):
1320
        /*
1321
         * Byte order changes.
1322
         */
1323
1
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1324
1
            "the file has sections with different byte orders");
1325
1
        return (-1);
1326
1327
124
      default:
1328
        /*
1329
         * Not a valid SHB.
1330
         */
1331
124
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1332
124
            "the file has a section with a bad byte order magic field");
1333
124
        return (-1);
1334
556
      }
1335
1336
      /*
1337
       * Make sure the major version is the version
1338
       * we handle.
1339
       */
1340
431
      if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
1341
17
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1342
17
            "unknown pcapng savefile major version number %u",
1343
17
            shbp->major_version);
1344
17
        return (-1);
1345
17
      }
1346
1347
      /*
1348
       * Reset the interface count; this section should
1349
       * have its own set of IDBs.  If any of them
1350
       * don't have the same interface type, snapshot
1351
       * length, or resolution as the first interface
1352
       * we saw, we'll fail.  (And if we don't see
1353
       * any IDBs, we'll fail when we see a packet
1354
       * block.)
1355
       */
1356
414
      ps->ifcount = 0;
1357
414
      break;
1358
1359
340
    default:
1360
      /*
1361
       * Not a packet block, IDB, or SHB; ignore it.
1362
       */
1363
340
      break;
1364
6.37k
    }
1365
6.37k
  }
1366
1367
2.53k
found:
1368
  /*
1369
   * Is the interface ID an interface we know?
1370
   */
1371
2.53k
  if (interface_id >= ps->ifcount) {
1372
    /*
1373
     * Yes.  Fail.
1374
     */
1375
60
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1376
60
        "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
1377
60
        interface_id);
1378
60
    return (-1);
1379
60
  }
1380
1381
2.47k
  if (hdr->caplen > (bpf_u_int32)p->snapshot) {
1382
37
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1383
37
        "invalid packet capture length %u, bigger than "
1384
37
        "snaplen of %d", hdr->caplen, p->snapshot);
1385
37
    return (-1);
1386
37
  }
1387
1388
  /*
1389
   * Convert the time stamp to seconds and fractions of a second,
1390
   * with the fractions being in units of the file-supplied resolution.
1391
   */
1392
2.43k
  sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
1393
2.43k
  frac = t % ps->ifaces[interface_id].tsresol;
1394
1395
  /*
1396
   * Convert the fractions from units of the file-supplied resolution
1397
   * to units of the user-requested resolution.
1398
   */
1399
2.43k
  switch (ps->ifaces[interface_id].scale_type) {
1400
1401
1.13k
  case PASS_THROUGH:
1402
    /*
1403
     * The interface resolution is what the user wants,
1404
     * so we're done.
1405
     */
1406
1.13k
    break;
1407
1408
613
  case SCALE_UP_DEC:
1409
    /*
1410
     * The interface resolution is less than what the user
1411
     * wants; scale the fractional part up to the units of
1412
     * the resolution the user requested by multiplying by
1413
     * the quotient of the user-requested resolution and the
1414
     * file-supplied resolution.
1415
     *
1416
     * Those resolutions are both powers of 10, and the user-
1417
     * requested resolution is greater than the file-supplied
1418
     * resolution, so the quotient in question is an integer.
1419
     * We've calculated that quotient already, so we just
1420
     * multiply by it.
1421
     */
1422
613
    frac *= ps->ifaces[interface_id].scale_factor;
1423
613
    break;
1424
1425
224
  case SCALE_UP_BIN:
1426
    /*
1427
     * The interface resolution is less than what the user
1428
     * wants; scale the fractional part up to the units of
1429
     * the resolution the user requested by multiplying by
1430
     * the quotient of the user-requested resolution and the
1431
     * file-supplied resolution.
1432
     *
1433
     * The file-supplied resolution is a power of 2, so the
1434
     * quotient is not an integer, so, in order to do this
1435
     * entirely with integer arithmetic, we multiply by the
1436
     * user-requested resolution and divide by the file-
1437
     * supplied resolution.
1438
     *
1439
     * XXX - Is there something clever we could do here,
1440
     * given that we know that the file-supplied resolution
1441
     * is a power of 2?  Doing a multiplication followed by
1442
     * a division runs the risk of overflowing, and involves
1443
     * two non-simple arithmetic operations.
1444
     */
1445
224
    frac *= ps->user_tsresol;
1446
224
    frac /= ps->ifaces[interface_id].tsresol;
1447
224
    break;
1448
1449
199
  case SCALE_DOWN_DEC:
1450
    /*
1451
     * The interface resolution is greater than what the user
1452
     * wants; scale the fractional part up to the units of
1453
     * the resolution the user requested by multiplying by
1454
     * the quotient of the user-requested resolution and the
1455
     * file-supplied resolution.
1456
     *
1457
     * Those resolutions are both powers of 10, and the user-
1458
     * requested resolution is less than the file-supplied
1459
     * resolution, so the quotient in question isn't an
1460
     * integer, but its reciprocal is, and we can just divide
1461
     * by the reciprocal of the quotient.  We've calculated
1462
     * the reciprocal of that quotient already, so we must
1463
     * divide by it.
1464
     */
1465
199
    frac /= ps->ifaces[interface_id].scale_factor;
1466
199
    break;
1467
1468
1469
262
  case SCALE_DOWN_BIN:
1470
    /*
1471
     * The interface resolution is greater than what the user
1472
     * wants; convert the fractional part to units of the
1473
     * resolution the user requested by multiplying by the
1474
     * quotient of the user-requested resolution and the
1475
     * file-supplied resolution.  We do that by multiplying
1476
     * by the user-requested resolution and dividing by the
1477
     * file-supplied resolution, as the quotient might not
1478
     * fit in an integer.
1479
     *
1480
     * The file-supplied resolution is a power of 2, so the
1481
     * quotient is not an integer, and neither is its
1482
     * reciprocal, so, in order to do this entirely with
1483
     * integer arithmetic, we multiply by the user-requested
1484
     * resolution and divide by the file-supplied resolution.
1485
     *
1486
     * XXX - Is there something clever we could do here,
1487
     * given that we know that the file-supplied resolution
1488
     * is a power of 2?  Doing a multiplication followed by
1489
     * a division runs the risk of overflowing, and involves
1490
     * two non-simple arithmetic operations.
1491
     */
1492
262
    frac *= ps->user_tsresol;
1493
262
    frac /= ps->ifaces[interface_id].tsresol;
1494
262
    break;
1495
2.43k
  }
1496
#ifdef _WIN32
1497
  /*
1498
   * tv_sec and tv_usec in the Windows struct timeval are both
1499
   * longs.
1500
   */
1501
  hdr->ts.tv_sec = (long)sec;
1502
  hdr->ts.tv_usec = (long)frac;
1503
#else
1504
  /*
1505
   * tv_sec in the UN*X struct timeval is a time_t; tv_usec is
1506
   * suseconds_t in UN*Xes that work the way the current Single
1507
   * UNIX Standard specify - but not all older UN*Xes necessarily
1508
   * support that type, so just cast to int.
1509
   */
1510
2.43k
  hdr->ts.tv_sec = (time_t)sec;
1511
2.43k
  hdr->ts.tv_usec = (int)frac;
1512
2.43k
#endif
1513
1514
  /*
1515
   * Get a pointer to the packet data.
1516
   */
1517
2.43k
  *data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
1518
2.43k
  if (*data == NULL)
1519
100
    return (-1);
1520
1521
2.33k
  pcapint_post_process(p->linktype, p->swapped, hdr, *data);
1522
1523
2.33k
  return (1);
1524
2.43k
}