Coverage Report

Created: 2026-12-01 06:07

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
285
#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.08k
#define BT_SHB      0x0A0D0D0A
87
1.22k
#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.14k
#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.60k
#define PCAP_NG_VERSION_MAJOR 1
107
2.14k
#define PCAP_NG_VERSION_MINOR 0
108
109
/*
110
 * Interface Description Block.
111
 */
112
3.56k
#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
901
#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
409
#define IF_TSOFFSET 14  /* time stamp offset for this interface */
137
138
/*
139
 * Enhanced Packet Block.
140
 */
141
474
#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.51k
#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
431
#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.14k
#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
832
  (sizeof (struct block_header) + \
247
832
   sizeof (struct enhanced_packet_block) + \
248
832
   (max_snaplen) + 131072 + \
249
832
   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
16.7k
{
259
16.7k
  size_t amt_read;
260
261
16.7k
  amt_read = fread(buf, 1, bytes_to_read, fp);
262
16.7k
  if (amt_read != bytes_to_read) {
263
504
    if (ferror(fp)) {
264
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
265
0
          errno, "error reading dump file");
266
504
    } else {
267
504
      if (amt_read == 0 && !fail_on_eof)
268
365
        return (0);  /* EOF */
269
139
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
270
139
          "truncated pcapng dump file; tried to read %zu bytes, only got %zu",
271
139
          bytes_to_read, amt_read);
272
139
    }
273
139
    return (-1);
274
504
  }
275
16.2k
  return (1);
276
16.7k
}
277
278
static int
279
read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
280
8.02k
{
281
8.02k
  struct pcap_ng_sf *ps;
282
8.02k
  int status;
283
8.02k
  struct block_header bhdr;
284
8.02k
  struct block_trailer *btrlr;
285
8.02k
  u_char *bdata;
286
8.02k
  size_t data_remaining;
287
288
8.02k
  ps = p->priv;
289
290
8.02k
  status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
291
8.02k
  if (status <= 0)
292
399
    return (status);  /* error or EOF */
293
294
7.62k
  if (p->swapped) {
295
2.44k
    bhdr.block_type = PCAP_BSWAP_32(bhdr.block_type);
296
2.44k
    bhdr.total_length = PCAP_BSWAP_32(bhdr.total_length);
297
2.44k
  }
298
299
  /*
300
   * Is this block "too small" - i.e., is it shorter than a block
301
   * header plus a block trailer?
302
   */
303
7.62k
  if (bhdr.total_length < sizeof(struct block_header) +
304
7.62k
      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
7.61k
  if ((bhdr.total_length % 4) != 0) {
316
    /*
317
     * No.  Report that as an error.
318
     */
319
8
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
320
8
        "block in pcapng dump file has a length of %u that is not a multiple of 4",
321
8
        bhdr.total_length);
322
8
    return (-1);
323
8
  }
324
325
  /*
326
   * Is the buffer big enough?
327
   */
328
7.60k
  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
135
    if (bhdr.total_length > ps->max_blocksize) {
334
69
      snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcapng block size %u > maximum %u", bhdr.total_length,
335
69
          ps->max_blocksize);
336
69
      return (-1);
337
69
    }
338
66
    if (!pcapint_grow_buffer(p, bhdr.total_length, errbuf))
339
0
      return (-1);
340
66
  }
341
342
  /*
343
   * Copy the stuff we've read to the buffer, and read the rest
344
   * of the block.
345
   */
346
7.53k
  memcpy(p->buffer, &bhdr, sizeof(bhdr));
347
7.53k
  bdata = p->buffer + sizeof(bhdr);
348
7.53k
  data_remaining = bhdr.total_length - sizeof(bhdr);
349
7.53k
  if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
350
47
    return (-1);
351
352
  /*
353
   * Get the block size from the trailer.
354
   */
355
7.49k
  btrlr = (struct block_trailer *)(bdata + data_remaining - sizeof (struct block_trailer));
356
7.49k
  if (p->swapped)
357
2.40k
    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
7.49k
  if (bhdr.total_length != btrlr->total_length) {
364
    /*
365
     * No.
366
     */
367
44
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
368
44
        "block total length in header %u and trailer %u don't match",
369
44
        bhdr.total_length, btrlr->total_length);
370
44
    return (-1);
371
44
  }
372
373
  /*
374
   * Initialize the cursor.
375
   */
376
7.44k
  cursor->data = bdata;
377
7.44k
  cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
378
7.44k
  cursor->block_type = bhdr.block_type;
379
7.44k
  return (1);
380
7.49k
}
381
382
static void *
383
get_from_block_data(struct block_cursor *cursor, size_t chunk_size,
384
    char *errbuf)
385
13.9k
{
386
13.9k
  void *data;
387
388
  /*
389
   * Make sure we have the specified amount of data remaining in
390
   * the block data.
391
   */
392
13.9k
  if (cursor->data_remaining < chunk_size) {
393
118
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
394
118
        "block of type %u in pcapng dump file is too short",
395
118
        cursor->block_type);
396
118
    return (NULL);
397
118
  }
398
399
  /*
400
   * Return the current pointer, and skip past the chunk.
401
   */
402
13.7k
  data = cursor->data;
403
13.7k
  cursor->data += chunk_size;
404
13.7k
  cursor->data_remaining -= chunk_size;
405
13.7k
  return (data);
406
13.9k
}
407
408
static struct option_header *
409
get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
410
2.51k
{
411
2.51k
  struct option_header *opthdr;
412
413
2.51k
  opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
414
2.51k
  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.51k
  if (p->swapped) {
425
953
    opthdr->option_code = PCAP_BSWAP_16(opthdr->option_code);
426
953
    opthdr->option_length = PCAP_BSWAP_16(opthdr->option_length);
427
953
  }
428
429
2.51k
  return (opthdr);
430
2.51k
}
431
432
static void *
433
get_optvalue_from_block_data(struct block_cursor *cursor,
434
    struct option_header *opthdr, char *errbuf)
435
2.51k
{
436
2.51k
  size_t padded_option_len;
437
2.51k
  void *optvalue;
438
439
  /* Pad option length to 4-byte boundary */
440
2.51k
  padded_option_len = opthdr->option_length;
441
2.51k
  padded_option_len = ((padded_option_len + 3)/4)*4;
442
443
2.51k
  optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
444
2.51k
  if (optvalue == NULL) {
445
    /*
446
     * Option value is cut short.
447
     */
448
26
    return (NULL);
449
26
  }
450
451
2.48k
  return (optvalue);
452
2.51k
}
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.52k
{
458
3.52k
  struct option_header *opthdr;
459
3.52k
  void *optvalue;
460
3.52k
  int saw_tsresol, saw_tsoffset;
461
3.52k
  uint8_t tsresol_opt;
462
3.52k
  u_int i;
463
464
3.52k
  saw_tsresol = 0;
465
3.52k
  saw_tsoffset = 0;
466
5.70k
  while (cursor->data_remaining != 0) {
467
    /*
468
     * Get the option header.
469
     */
470
2.51k
    opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
471
2.51k
    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.51k
    optvalue = get_optvalue_from_block_data(cursor, opthdr,
482
2.51k
        errbuf);
483
2.51k
    if (optvalue == NULL) {
484
      /*
485
       * Option value is cut short.
486
       */
487
26
      return (-1);
488
26
    }
489
490
2.48k
    switch (opthdr->option_code) {
491
492
285
    case OPT_ENDOFOPT:
493
285
      if (opthdr->option_length != 0) {
494
10
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
495
10
            "Interface Description Block has opt_endofopt option with length %u != 0",
496
10
            opthdr->option_length);
497
10
        return (-1);
498
10
      }
499
275
      goto done;
500
501
901
    case IF_TSRESOL:
502
901
      if (opthdr->option_length != 1) {
503
3
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
504
3
            "Interface Description Block has if_tsresol option with length %u != 1",
505
3
            opthdr->option_length);
506
3
        return (-1);
507
3
      }
508
898
      if (saw_tsresol) {
509
2
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
510
2
            "Interface Description Block has more than one if_tsresol option");
511
2
        return (-1);
512
2
      }
513
896
      saw_tsresol = 1;
514
896
      memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
515
896
      if (tsresol_opt & 0x80) {
516
        /*
517
         * Resolution is negative power of 2.
518
         */
519
461
        uint8_t tsresol_shift = (tsresol_opt & 0x7F);
520
521
461
        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
454
        *is_binary = 1;
532
454
        *tsresol = ((uint64_t)1) << tsresol_shift;
533
454
      } else {
534
        /*
535
         * Resolution is negative power of 10.
536
         */
537
435
        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
4
          snprintf(errbuf, PCAP_ERRBUF_SIZE,
547
4
              "Interface Description Block if_tsresol option resolution 10^-%u is too high",
548
4
              tsresol_opt);
549
4
          return (-1);
550
4
        }
551
431
        *is_binary = 0;
552
431
        *tsresol = 1;
553
3.96k
        for (i = 0; i < tsresol_opt; i++)
554
3.53k
          *tsresol *= 10;
555
431
      }
556
885
      break;
557
558
885
    case IF_TSOFFSET:
559
409
      if (opthdr->option_length != 8) {
560
16
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
561
16
            "Interface Description Block has if_tsoffset option with length %u != 8",
562
16
            opthdr->option_length);
563
16
        return (-1);
564
16
      }
565
393
      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
392
      saw_tsoffset = 1;
571
392
      memcpy(tsoffset, optvalue, sizeof(*tsoffset));
572
392
      if (p->swapped)
573
195
        *tsoffset = PCAP_BSWAP_64(*tsoffset);
574
392
      break;
575
576
894
    default:
577
894
      break;
578
2.48k
    }
579
2.48k
  }
580
581
3.46k
done:
582
3.46k
  return (0);
583
3.52k
}
584
585
static int
586
add_interface(pcap_t *p, struct interface_description_block *idbp,
587
    struct block_cursor *cursor, char *errbuf)
588
3.52k
{
589
3.52k
  struct pcap_ng_sf *ps;
590
3.52k
  bpf_u_int32 new_ifcount;
591
3.52k
  bpf_u_int32 new_if_index;
592
3.52k
  uint64_t tsresol;
593
3.52k
  int64_t tsoffset;
594
3.52k
  int is_binary;
595
596
3.52k
  ps = p->priv;
597
598
  /*
599
   * Count this interface.
600
   */
601
3.52k
  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.52k
  new_if_index = ps->ifcount;
608
609
  /*
610
   * Grow the array of per-interface information as necessary.
611
   */
612
3.52k
  if (new_ifcount > ps->ifaces_size) {
613
    /*
614
     * We need to grow the array.
615
     */
616
1.23k
    bpf_u_int32 new_ifaces_size;
617
1.23k
    struct pcap_ng_if *new_ifaces;
618
619
1.23k
    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
864
      new_ifaces_size = 1;
637
864
      new_ifaces = malloc(sizeof (struct pcap_ng_if));
638
864
    } 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
369
      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
369
      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
369
      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
369
      new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
689
369
    }
690
1.23k
    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.23k
    ps->ifaces_size = new_ifaces_size;
701
1.23k
    ps->ifaces = new_ifaces;
702
1.23k
  }
703
704
3.52k
  ps->ifaces[new_if_index].snaplen = idbp->snaplen;
705
706
  /*
707
   * Set the default time stamp resolution and offset.
708
   */
709
3.52k
  tsresol = 1000000;  /* microsecond resolution */
710
3.52k
  is_binary = 0;    /* which is a power of 10 */
711
3.52k
  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.52k
  if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
718
3.52k
      errbuf) == -1)
719
69
    return (0);
720
721
3.46k
  ps->ifaces[new_if_index].tsresol = tsresol;
722
3.46k
  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.46k
  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.57k
    ps->ifaces[new_if_index].scale_type = PASS_THROUGH;
734
2.57k
  } 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
461
    if (is_binary)
740
244
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_BIN;
741
217
    else {
742
      /*
743
       * Calculate the scale factor.
744
       */
745
217
      ps->ifaces[new_if_index].scale_factor = tsresol/ps->user_tsresol;
746
217
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_DEC;
747
217
    }
748
461
  } else {
749
    /*
750
     * The resolution is less than what the user wants,
751
     * so we have to scale the timestamps up.
752
     */
753
420
    if (is_binary)
754
209
      ps->ifaces[new_if_index].scale_type = SCALE_UP_BIN;
755
211
    else {
756
      /*
757
       * Calculate the scale factor.
758
       */
759
211
      ps->ifaces[new_if_index].scale_factor = ps->user_tsresol/tsresol;
760
211
      ps->ifaces[new_if_index].scale_type = SCALE_UP_DEC;
761
211
    }
762
420
  }
763
764
  /*
765
   * We've succeeded, so update the interface count.
766
   */
767
3.46k
  ps->ifcount = new_ifcount;
768
3.46k
  return (1);
769
3.52k
}
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.48k
{
779
1.48k
  bpf_u_int32 magic_int;
780
1.48k
  size_t amt_read;
781
1.48k
  bpf_u_int32 total_length;
782
1.48k
  bpf_u_int32 byte_order_magic;
783
1.48k
  struct block_header *bhdrp;
784
1.48k
  struct section_header_block *shbp;
785
1.48k
  pcap_t *p;
786
1.48k
  int swapped = 0;
787
1.48k
  struct pcap_ng_sf *ps;
788
1.48k
  int status;
789
1.48k
  struct block_cursor cursor;
790
1.48k
  struct interface_description_block *idbp;
791
792
  /*
793
   * Assume no read errors.
794
   */
795
1.48k
  *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.48k
  memcpy(&magic_int, magic, sizeof(magic_int));
802
1.48k
  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
218
    return (NULL);  /* nope */
813
218
  }
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.26k
  amt_read = fread(&total_length, 1, sizeof(total_length), fp);
827
1.26k
  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.26k
  amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
842
1.26k
  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.26k
  if (byte_order_magic != BYTE_ORDER_MAGIC) {
857
452
    byte_order_magic = PCAP_BSWAP_32(byte_order_magic);
858
452
    if (byte_order_magic != BYTE_ORDER_MAGIC) {
859
      /*
860
       * Not a pcapng file.
861
       */
862
84
      return (NULL);
863
84
    }
864
368
    swapped = 1;
865
368
    total_length = PCAP_BSWAP_32(total_length);
866
368
  }
867
868
  /*
869
   * Check the sanity of the total length.
870
   */
871
1.18k
  if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer) ||
872
1.18k
            (total_length > BT_SHB_INSANE_MAX)) {
873
39
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
874
39
        "Section Header Block in pcapng dump file has invalid length %zu < _%u_ < %u (BT_SHB_INSANE_MAX)",
875
39
        sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer),
876
39
        total_length,
877
39
        BT_SHB_INSANE_MAX);
878
879
39
    *err = 1;
880
39
    return (NULL);
881
39
  }
882
883
  /*
884
   * OK, this is a good pcapng file.
885
   * Allocate a pcap_t for it.
886
   */
887
1.14k
  p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_ng_sf);
888
1.14k
  if (p == NULL) {
889
    /* Allocation failed. */
890
0
    *err = 1;
891
0
    return (NULL);
892
0
  }
893
1.14k
  p->swapped = swapped;
894
1.14k
  ps = p->priv;
895
896
  /*
897
   * What precision does the user want?
898
   */
899
1.14k
  switch (precision) {
900
901
1.14k
  case PCAP_TSTAMP_PRECISION_MICRO:
902
1.14k
    ps->user_tsresol = 1000000;
903
1.14k
    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.14k
  }
916
917
1.14k
  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.14k
  p->bufsize = 2048;
936
1.14k
  if (p->bufsize < total_length)
937
48
    p->bufsize = total_length;
938
1.14k
  p->buffer = malloc(p->bufsize);
939
1.14k
  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.14k
  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.14k
  bhdrp = (struct block_header *)p->buffer;
952
1.14k
  shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header));
953
1.14k
  bhdrp->block_type = magic_int;
954
1.14k
  bhdrp->total_length = total_length;
955
1.14k
  shbp->byte_order_magic = byte_order_magic;
956
1.14k
  if (read_bytes(fp,
957
1.14k
      p->buffer + (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
958
1.14k
      total_length - (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
959
1.14k
      1, errbuf) == -1)
960
58
    goto fail;
961
962
1.08k
  if (p->swapped) {
963
    /*
964
     * Byte-swap the fields we've read.
965
     */
966
350
    shbp->major_version = PCAP_BSWAP_16(shbp->major_version);
967
350
    shbp->minor_version = PCAP_BSWAP_16(shbp->minor_version);
968
969
    /*
970
     * XXX - we don't care about the section length.
971
     */
972
350
  }
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.08k
  if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
988
1.07k
         (shbp->minor_version == PCAP_NG_VERSION_MINOR ||
989
36
          shbp->minor_version == 2))) {
990
35
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
991
35
        "unsupported pcapng savefile version %u.%u",
992
35
        shbp->major_version, shbp->minor_version);
993
35
    goto fail;
994
35
  }
995
1.05k
  p->version_major = shbp->major_version;
996
1.05k
  p->version_minor = shbp->minor_version;
997
998
  /*
999
   * Save the time stamp resolution the user requested.
1000
   */
1001
1.05k
  p->opt.tstamp_precision = precision;
1002
1003
  /*
1004
   * Now start looking for an Interface Description Block.
1005
   */
1006
1.59k
  for (;;) {
1007
    /*
1008
     * Read the next block.
1009
     */
1010
1.59k
    status = read_block(fp, p, &cursor, errbuf);
1011
1.59k
    if (status == 0) {
1012
      /* EOF - no IDB in this file */
1013
19
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
1014
19
          "the capture file has no Interface Description Blocks");
1015
19
      goto fail;
1016
19
    }
1017
1.57k
    if (status == -1)
1018
166
      goto fail;  /* error */
1019
1.41k
    switch (cursor.block_type) {
1020
1021
865
    case BT_IDB:
1022
      /*
1023
       * Get a pointer to the fixed-length portion of the
1024
       * IDB.
1025
       */
1026
865
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1027
865
          errbuf);
1028
865
      if (idbp == NULL)
1029
1
        goto fail;  /* error */
1030
1031
      /*
1032
       * Byte-swap it if necessary.
1033
       */
1034
864
      if (p->swapped) {
1035
270
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1036
270
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1037
270
      }
1038
1039
      /*
1040
       * Try to add this interface.
1041
       */
1042
864
      if (!add_interface(p, idbp, &cursor, errbuf))
1043
66
        goto fail;
1044
1045
798
      goto done;
1046
1047
798
    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
544
    default:
1060
      /*
1061
       * Just ignore it.
1062
       */
1063
544
      break;
1064
1.41k
    }
1065
1.41k
  }
1066
1067
798
done:
1068
798
  p->linktype = linktype_to_dlt(idbp->linktype);
1069
798
  p->snapshot = pcapint_adjust_snapshot(p->linktype, idbp->snaplen);
1070
798
  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
798
  if (MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
1078
34
    ps->max_blocksize = MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype));
1079
1080
798
  p->next_packet_op = pcap_ng_next_packet;
1081
798
  p->cleanup_op = pcap_ng_cleanup;
1082
1083
798
  return (p);
1084
1085
348
fail:
1086
348
  free(ps->ifaces);
1087
348
  free(p->buffer);
1088
348
  free(p);
1089
348
  *err = 1;
1090
348
  return (NULL);
1091
1.05k
}
1092
1093
static void
1094
pcap_ng_cleanup(pcap_t *p)
1095
798
{
1096
798
  struct pcap_ng_sf *ps = p->priv;
1097
1098
798
  free(ps->ifaces);
1099
798
  pcapint_sf_cleanup(p);
1100
798
}
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.02k
{
1110
3.02k
  struct pcap_ng_sf *ps = p->priv;
1111
3.02k
  struct block_cursor cursor;
1112
3.02k
  int status;
1113
3.02k
  struct enhanced_packet_block *epbp;
1114
3.02k
  struct simple_packet_block *spbp;
1115
3.02k
  struct packet_block *pbp;
1116
3.02k
  bpf_u_int32 interface_id = 0xFFFFFFFF;
1117
3.02k
  struct interface_description_block *idbp;
1118
3.02k
  struct section_header_block *shbp;
1119
3.02k
  FILE *fp = p->rfile;
1120
3.02k
  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.42k
  for (;;) {
1127
    /*
1128
     * Read the block type and length; those are common
1129
     * to all blocks.
1130
     */
1131
6.42k
    status = read_block(fp, p, &cursor, p->errbuf);
1132
6.42k
    if (status == 0)
1133
346
      return (0);  /* EOF */
1134
6.07k
    if (status == -1)
1135
43
      return (-1);  /* error */
1136
6.03k
    switch (cursor.block_type) {
1137
1138
473
    case BT_EPB:
1139
      /*
1140
       * Get a pointer to the fixed-length portion of the
1141
       * EPB.
1142
       */
1143
473
      epbp = get_from_block_data(&cursor, sizeof(*epbp),
1144
473
          p->errbuf);
1145
473
      if (epbp == NULL)
1146
4
        return (-1);  /* error */
1147
1148
      /*
1149
       * Byte-swap it if necessary.
1150
       */
1151
469
      if (p->swapped) {
1152
        /* these were written in opposite byte order */
1153
243
        interface_id = PCAP_BSWAP_32(epbp->interface_id);
1154
243
        hdr->caplen = PCAP_BSWAP_32(epbp->caplen);
1155
243
        hdr->len = PCAP_BSWAP_32(epbp->len);
1156
243
        t = ((uint64_t)PCAP_BSWAP_32(epbp->timestamp_high)) << 32 |
1157
243
            PCAP_BSWAP_32(epbp->timestamp_low);
1158
243
      } else {
1159
226
        interface_id = epbp->interface_id;
1160
226
        hdr->caplen = epbp->caplen;
1161
226
        hdr->len = epbp->len;
1162
226
        t = ((uint64_t)epbp->timestamp_high) << 32 |
1163
226
            epbp->timestamp_low;
1164
226
      }
1165
469
      goto found;
1166
1167
1.51k
    case BT_SPB:
1168
      /*
1169
       * Get a pointer to the fixed-length portion of the
1170
       * SPB.
1171
       */
1172
1.51k
      spbp = get_from_block_data(&cursor, sizeof(*spbp),
1173
1.51k
          p->errbuf);
1174
1.51k
      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.50k
      interface_id = 0;
1182
1183
      /*
1184
       * Byte-swap it if necessary.
1185
       */
1186
1.50k
      if (p->swapped) {
1187
        /* these were written in opposite byte order */
1188
320
        hdr->len = PCAP_BSWAP_32(spbp->len);
1189
320
      } else
1190
1.18k
        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.50k
      hdr->caplen = hdr->len;
1198
1.50k
      if (hdr->caplen > (bpf_u_int32)p->snapshot)
1199
244
        hdr->caplen = p->snapshot;
1200
1.50k
      t = 0;  /* no time stamps */
1201
1.50k
      goto found;
1202
1203
428
    case BT_PB:
1204
      /*
1205
       * Get a pointer to the fixed-length portion of the
1206
       * PB.
1207
       */
1208
428
      pbp = get_from_block_data(&cursor, sizeof(*pbp),
1209
428
          p->errbuf);
1210
428
      if (pbp == NULL)
1211
4
        return (-1);  /* error */
1212
1213
      /*
1214
       * Byte-swap it if necessary.
1215
       */
1216
424
      if (p->swapped) {
1217
        /* these were written in opposite byte order */
1218
201
        interface_id = PCAP_BSWAP_16(pbp->interface_id);
1219
201
        hdr->caplen = PCAP_BSWAP_32(pbp->caplen);
1220
201
        hdr->len = PCAP_BSWAP_32(pbp->len);
1221
201
        t = ((uint64_t)PCAP_BSWAP_32(pbp->timestamp_high)) << 32 |
1222
201
            PCAP_BSWAP_32(pbp->timestamp_low);
1223
223
      } else {
1224
223
        interface_id = pbp->interface_id;
1225
223
        hdr->caplen = pbp->caplen;
1226
223
        hdr->len = pbp->len;
1227
223
        t = ((uint64_t)pbp->timestamp_high) << 32 |
1228
223
            pbp->timestamp_low;
1229
223
      }
1230
424
      goto found;
1231
1232
2.69k
    case BT_IDB:
1233
      /*
1234
       * Interface Description Block.  Get a pointer
1235
       * to its fixed-length portion.
1236
       */
1237
2.69k
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1238
2.69k
          p->errbuf);
1239
2.69k
      if (idbp == NULL)
1240
1
        return (-1);  /* error */
1241
1242
      /*
1243
       * Byte-swap it if necessary.
1244
       */
1245
2.69k
      if (p->swapped) {
1246
819
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1247
819
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1248
819
      }
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.69k
      if (p->linktype != idbp->linktype) {
1259
1
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1260
1
            "an interface has a type %u different from the type of the first interface",
1261
1
            idbp->linktype);
1262
1
        return (-1);
1263
1
      }
1264
1265
      /*
1266
       * Check against the *adjusted* value of this IDB's
1267
       * snapshot length.
1268
       */
1269
2.69k
      if ((bpf_u_int32)p->snapshot !=
1270
2.69k
          pcapint_adjust_snapshot(p->linktype, idbp->snaplen)) {
1271
30
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1272
30
            "an interface has a snapshot length %u different from the snapshot length of the first interface",
1273
30
            idbp->snaplen);
1274
30
        return (-1);
1275
30
      }
1276
1277
      /*
1278
       * Try to add this interface.
1279
       */
1280
2.66k
      if (!add_interface(p, idbp, &cursor, p->errbuf))
1281
3
        return (-1);
1282
2.66k
      break;
1283
1284
2.66k
    case BT_SHB:
1285
      /*
1286
       * Section Header Block.  Get a pointer
1287
       * to its fixed-length portion.
1288
       */
1289
597
      shbp = get_from_block_data(&cursor, sizeof(*shbp),
1290
597
          p->errbuf);
1291
597
      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
594
      if (p->swapped) {
1300
263
        shbp->byte_order_magic =
1301
263
            PCAP_BSWAP_32(shbp->byte_order_magic);
1302
263
        shbp->major_version =
1303
263
            PCAP_BSWAP_16(shbp->major_version);
1304
263
      }
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
594
      switch (shbp->byte_order_magic) {
1312
1313
424
      case BYTE_ORDER_MAGIC:
1314
        /*
1315
         * OK.
1316
         */
1317
424
        break;
1318
1319
2
      case PCAP_BSWAP_32(BYTE_ORDER_MAGIC):
1320
        /*
1321
         * Byte order changes.
1322
         */
1323
2
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1324
2
            "the file has sections with different byte orders");
1325
2
        return (-1);
1326
1327
168
      default:
1328
        /*
1329
         * Not a valid SHB.
1330
         */
1331
168
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1332
168
            "the file has a section with a bad byte order magic field");
1333
168
        return (-1);
1334
594
      }
1335
1336
      /*
1337
       * Make sure the major version is the version
1338
       * we handle.
1339
       */
1340
424
      if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
1341
16
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1342
16
            "unknown pcapng savefile major version number %u",
1343
16
            shbp->major_version);
1344
16
        return (-1);
1345
16
      }
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
408
      ps->ifcount = 0;
1357
408
      break;
1358
1359
331
    default:
1360
      /*
1361
       * Not a packet block, IDB, or SHB; ignore it.
1362
       */
1363
331
      break;
1364
6.03k
    }
1365
6.03k
  }
1366
1367
2.40k
found:
1368
  /*
1369
   * Is the interface ID an interface we know?
1370
   */
1371
2.40k
  if (interface_id >= ps->ifcount) {
1372
    /*
1373
     * Yes.  Fail.
1374
     */
1375
58
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1376
58
        "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
1377
58
        interface_id);
1378
58
    return (-1);
1379
58
  }
1380
1381
2.34k
  if (hdr->caplen > (bpf_u_int32)p->snapshot) {
1382
40
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1383
40
        "invalid packet capture length %u, bigger than "
1384
40
        "snaplen of %d", hdr->caplen, p->snapshot);
1385
40
    return (-1);
1386
40
  }
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.30k
  sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
1393
2.30k
  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.30k
  switch (ps->ifaces[interface_id].scale_type) {
1400
1401
905
  case PASS_THROUGH:
1402
    /*
1403
     * The interface resolution is what the user wants,
1404
     * so we're done.
1405
     */
1406
905
    break;
1407
1408
450
  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
450
    frac *= ps->ifaces[interface_id].scale_factor;
1423
450
    break;
1424
1425
294
  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
294
    frac *= ps->user_tsresol;
1446
294
    frac /= ps->ifaces[interface_id].tsresol;
1447
294
    break;
1448
1449
194
  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
194
    frac /= ps->ifaces[interface_id].scale_factor;
1466
194
    break;
1467
1468
1469
461
  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
461
    frac *= ps->user_tsresol;
1493
461
    frac /= ps->ifaces[interface_id].tsresol;
1494
461
    break;
1495
2.30k
  }
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.30k
  hdr->ts.tv_sec = (time_t)sec;
1511
2.30k
  hdr->ts.tv_usec = (int)frac;
1512
2.30k
#endif
1513
1514
  /*
1515
   * Get a pointer to the packet data.
1516
   */
1517
2.30k
  *data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
1518
2.30k
  if (*data == NULL)
1519
78
    return (-1);
1520
1521
2.22k
  pcapint_post_process(p->linktype, p->swapped, hdr, *data);
1522
1523
2.22k
  return (1);
1524
2.30k
}