Coverage Report

Created: 2026-11-12 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
263
#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.06k
#define BT_SHB      0x0A0D0D0A
87
1.24k
#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.20k
#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.15k
#define PCAP_NG_VERSION_MINOR 0
108
109
/*
110
 * Interface Description Block.
111
 */
112
4.02k
#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.08k
#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
410
#define IF_TSOFFSET 14  /* time stamp offset for this interface */
137
138
/*
139
 * Enhanced Packet Block.
140
 */
141
473
#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.33k
#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
432
#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.15k
#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
823
  (sizeof (struct block_header) + \
247
823
   sizeof (struct enhanced_packet_block) + \
248
823
   (max_snaplen) + 131072 + \
249
823
   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.5k
{
259
17.5k
  size_t amt_read;
260
261
17.5k
  amt_read = fread(buf, 1, bytes_to_read, fp);
262
17.5k
  if (amt_read != bytes_to_read) {
263
525
    if (ferror(fp)) {
264
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
265
0
          errno, "error reading dump file");
266
525
    } else {
267
525
      if (amt_read == 0 && !fail_on_eof)
268
362
        return (0);  /* EOF */
269
163
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
270
163
          "truncated pcapng dump file; tried to read %zu bytes, only got %zu",
271
163
          bytes_to_read, amt_read);
272
163
    }
273
163
    return (-1);
274
525
  }
275
16.9k
  return (1);
276
17.5k
}
277
278
static int
279
read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
280
8.41k
{
281
8.41k
  struct pcap_ng_sf *ps;
282
8.41k
  int status;
283
8.41k
  struct block_header bhdr;
284
8.41k
  struct block_trailer *btrlr;
285
8.41k
  u_char *bdata;
286
8.41k
  size_t data_remaining;
287
288
8.41k
  ps = p->priv;
289
290
8.41k
  status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
291
8.41k
  if (status <= 0)
292
404
    return (status);  /* error or EOF */
293
294
8.00k
  if (p->swapped) {
295
2.96k
    bhdr.block_type = PCAP_BSWAP_32(bhdr.block_type);
296
2.96k
    bhdr.total_length = PCAP_BSWAP_32(bhdr.total_length);
297
2.96k
  }
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.00k
  if (bhdr.total_length < sizeof(struct block_header) +
304
8.00k
      sizeof(struct block_trailer)) {
305
5
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
306
5
        "block in pcapng dump file has a length of %u < %zu",
307
5
        bhdr.total_length,
308
5
        sizeof(struct block_header) + sizeof(struct block_trailer));
309
5
    return (-1);
310
5
  }
311
312
  /*
313
   * Is the block total length a multiple of 4?
314
   */
315
8.00k
  if ((bhdr.total_length % 4) != 0) {
316
    /*
317
     * No.  Report that as an error.
318
     */
319
16
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
320
16
        "block in pcapng dump file has a length of %u that is not a multiple of 4",
321
16
        bhdr.total_length);
322
16
    return (-1);
323
16
  }
324
325
  /*
326
   * Is the buffer big enough?
327
   */
328
7.98k
  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
117
    if (bhdr.total_length > ps->max_blocksize) {
334
49
      snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcapng block size %u > maximum %u", bhdr.total_length,
335
49
          ps->max_blocksize);
336
49
      return (-1);
337
49
    }
338
68
    if (!pcapint_grow_buffer(p, bhdr.total_length, errbuf))
339
0
      return (-1);
340
68
  }
341
342
  /*
343
   * Copy the stuff we've read to the buffer, and read the rest
344
   * of the block.
345
   */
346
7.93k
  memcpy(p->buffer, &bhdr, sizeof(bhdr));
347
7.93k
  bdata = p->buffer + sizeof(bhdr);
348
7.93k
  data_remaining = bhdr.total_length - sizeof(bhdr);
349
7.93k
  if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
350
55
    return (-1);
351
352
  /*
353
   * Get the block size from the trailer.
354
   */
355
7.88k
  btrlr = (struct block_trailer *)(bdata + data_remaining - sizeof (struct block_trailer));
356
7.88k
  if (p->swapped)
357
2.92k
    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.88k
  if (bhdr.total_length != btrlr->total_length) {
364
    /*
365
     * No.
366
     */
367
56
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
368
56
        "block total length in header %u and trailer %u don't match",
369
56
        bhdr.total_length, btrlr->total_length);
370
56
    return (-1);
371
56
  }
372
373
  /*
374
   * Initialize the cursor.
375
   */
376
7.82k
  cursor->data = bdata;
377
7.82k
  cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
378
7.82k
  cursor->block_type = bhdr.block_type;
379
7.82k
  return (1);
380
7.88k
}
381
382
static void *
383
get_from_block_data(struct block_cursor *cursor, size_t chunk_size,
384
    char *errbuf)
385
14.4k
{
386
14.4k
  void *data;
387
388
  /*
389
   * Make sure we have the specified amount of data remaining in
390
   * the block data.
391
   */
392
14.4k
  if (cursor->data_remaining < chunk_size) {
393
117
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
394
117
        "block of type %u in pcapng dump file is too short",
395
117
        cursor->block_type);
396
117
    return (NULL);
397
117
  }
398
399
  /*
400
   * Return the current pointer, and skip past the chunk.
401
   */
402
14.2k
  data = cursor->data;
403
14.2k
  cursor->data += chunk_size;
404
14.2k
  cursor->data_remaining -= chunk_size;
405
14.2k
  return (data);
406
14.4k
}
407
408
static struct option_header *
409
get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
410
2.73k
{
411
2.73k
  struct option_header *opthdr;
412
413
2.73k
  opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
414
2.73k
  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.73k
  if (p->swapped) {
425
962
    opthdr->option_code = PCAP_BSWAP_16(opthdr->option_code);
426
962
    opthdr->option_length = PCAP_BSWAP_16(opthdr->option_length);
427
962
  }
428
429
2.73k
  return (opthdr);
430
2.73k
}
431
432
static void *
433
get_optvalue_from_block_data(struct block_cursor *cursor,
434
    struct option_header *opthdr, char *errbuf)
435
2.73k
{
436
2.73k
  size_t padded_option_len;
437
2.73k
  void *optvalue;
438
439
  /* Pad option length to 4-byte boundary */
440
2.73k
  padded_option_len = opthdr->option_length;
441
2.73k
  padded_option_len = ((padded_option_len + 3)/4)*4;
442
443
2.73k
  optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
444
2.73k
  if (optvalue == NULL) {
445
    /*
446
     * Option value is cut short.
447
     */
448
27
    return (NULL);
449
27
  }
450
451
2.70k
  return (optvalue);
452
2.73k
}
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.96k
{
458
3.96k
  struct option_header *opthdr;
459
3.96k
  void *optvalue;
460
3.96k
  int saw_tsresol, saw_tsoffset;
461
3.96k
  uint8_t tsresol_opt;
462
3.96k
  u_int i;
463
464
3.96k
  saw_tsresol = 0;
465
3.96k
  saw_tsoffset = 0;
466
6.36k
  while (cursor->data_remaining != 0) {
467
    /*
468
     * Get the option header.
469
     */
470
2.73k
    opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
471
2.73k
    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.73k
    optvalue = get_optvalue_from_block_data(cursor, opthdr,
482
2.73k
        errbuf);
483
2.73k
    if (optvalue == NULL) {
484
      /*
485
       * Option value is cut short.
486
       */
487
27
      return (-1);
488
27
    }
489
490
2.70k
    switch (opthdr->option_code) {
491
492
263
    case OPT_ENDOFOPT:
493
263
      if (opthdr->option_length != 0) {
494
9
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
495
9
            "Interface Description Block has opt_endofopt option with length %u != 0",
496
9
            opthdr->option_length);
497
9
        return (-1);
498
9
      }
499
254
      goto done;
500
501
1.08k
    case IF_TSRESOL:
502
1.08k
      if (opthdr->option_length != 1) {
503
14
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
504
14
            "Interface Description Block has if_tsresol option with length %u != 1",
505
14
            opthdr->option_length);
506
14
        return (-1);
507
14
      }
508
1.06k
      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.06k
      saw_tsresol = 1;
514
1.06k
      memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
515
1.06k
      if (tsresol_opt & 0x80) {
516
        /*
517
         * Resolution is negative power of 2.
518
         */
519
600
        uint8_t tsresol_shift = (tsresol_opt & 0x7F);
520
521
600
        if (tsresol_shift > 63) {
522
          /*
523
           * Resolution is too high; 2^-{res}
524
           * won't fit in a 64-bit value.
525
           */
526
6
          snprintf(errbuf, PCAP_ERRBUF_SIZE,
527
6
              "Interface Description Block if_tsresol option resolution 2^-%u is too high",
528
6
              tsresol_shift);
529
6
          return (-1);
530
6
        }
531
594
        *is_binary = 1;
532
594
        *tsresol = ((uint64_t)1) << tsresol_shift;
533
594
      } else {
534
        /*
535
         * Resolution is negative power of 10.
536
         */
537
468
        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
464
        *is_binary = 0;
552
464
        *tsresol = 1;
553
4.19k
        for (i = 0; i < tsresol_opt; i++)
554
3.72k
          *tsresol *= 10;
555
464
      }
556
1.05k
      break;
557
558
1.05k
    case IF_TSOFFSET:
559
410
      if (opthdr->option_length != 8) {
560
15
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
561
15
            "Interface Description Block has if_tsoffset option with length %u != 8",
562
15
            opthdr->option_length);
563
15
        return (-1);
564
15
      }
565
395
      if (saw_tsoffset) {
566
2
        snprintf(errbuf, PCAP_ERRBUF_SIZE,
567
2
            "Interface Description Block has more than one if_tsoffset option");
568
2
        return (-1);
569
2
      }
570
393
      saw_tsoffset = 1;
571
393
      memcpy(tsoffset, optvalue, sizeof(*tsoffset));
572
393
      if (p->swapped)
573
198
        *tsoffset = PCAP_BSWAP_64(*tsoffset);
574
393
      break;
575
576
949
    default:
577
949
      break;
578
2.70k
    }
579
2.70k
  }
580
581
3.88k
done:
582
3.88k
  return (0);
583
3.96k
}
584
585
static int
586
add_interface(pcap_t *p, struct interface_description_block *idbp,
587
    struct block_cursor *cursor, char *errbuf)
588
3.96k
{
589
3.96k
  struct pcap_ng_sf *ps;
590
3.96k
  bpf_u_int32 new_ifcount;
591
3.96k
  bpf_u_int32 new_if_index;
592
3.96k
  uint64_t tsresol;
593
3.96k
  int64_t tsoffset;
594
3.96k
  int is_binary;
595
596
3.96k
  ps = p->priv;
597
598
  /*
599
   * Count this interface.
600
   */
601
3.96k
  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.96k
  new_if_index = ps->ifcount;
608
609
  /*
610
   * Grow the array of per-interface information as necessary.
611
   */
612
3.96k
  if (new_ifcount > ps->ifaces_size) {
613
    /*
614
     * We need to grow the array.
615
     */
616
1.28k
    bpf_u_int32 new_ifaces_size;
617
1.28k
    struct pcap_ng_if *new_ifaces;
618
619
1.28k
    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
867
      new_ifaces_size = 1;
637
867
      new_ifaces = malloc(sizeof (struct pcap_ng_if));
638
867
    } 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
417
      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
417
      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
417
      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
417
      new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
689
417
    }
690
1.28k
    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.28k
    ps->ifaces_size = new_ifaces_size;
701
1.28k
    ps->ifaces = new_ifaces;
702
1.28k
  }
703
704
3.96k
  ps->ifaces[new_if_index].snaplen = idbp->snaplen;
705
706
  /*
707
   * Set the default time stamp resolution and offset.
708
   */
709
3.96k
  tsresol = 1000000;  /* microsecond resolution */
710
3.96k
  is_binary = 0;    /* which is a power of 10 */
711
3.96k
  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.96k
  if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
718
3.96k
      errbuf) == -1)
719
78
    return (0);
720
721
3.88k
  ps->ifaces[new_if_index].tsresol = tsresol;
722
3.88k
  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.88k
  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.83k
    ps->ifaces[new_if_index].scale_type = PASS_THROUGH;
734
2.83k
  } 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
564
    if (is_binary)
740
336
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_BIN;
741
228
    else {
742
      /*
743
       * Calculate the scale factor.
744
       */
745
228
      ps->ifaces[new_if_index].scale_factor = tsresol/ps->user_tsresol;
746
228
      ps->ifaces[new_if_index].scale_type = SCALE_DOWN_DEC;
747
228
    }
748
564
  } else {
749
    /*
750
     * The resolution is less than what the user wants,
751
     * so we have to scale the timestamps up.
752
     */
753
492
    if (is_binary)
754
257
      ps->ifaces[new_if_index].scale_type = SCALE_UP_BIN;
755
235
    else {
756
      /*
757
       * Calculate the scale factor.
758
       */
759
235
      ps->ifaces[new_if_index].scale_factor = ps->user_tsresol/tsresol;
760
235
      ps->ifaces[new_if_index].scale_type = SCALE_UP_DEC;
761
235
    }
762
492
  }
763
764
  /*
765
   * We've succeeded, so update the interface count.
766
   */
767
3.88k
  ps->ifcount = new_ifcount;
768
3.88k
  return (1);
769
3.96k
}
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.50k
{
779
1.50k
  bpf_u_int32 magic_int;
780
1.50k
  size_t amt_read;
781
1.50k
  bpf_u_int32 total_length;
782
1.50k
  bpf_u_int32 byte_order_magic;
783
1.50k
  struct block_header *bhdrp;
784
1.50k
  struct section_header_block *shbp;
785
1.50k
  pcap_t *p;
786
1.50k
  int swapped = 0;
787
1.50k
  struct pcap_ng_sf *ps;
788
1.50k
  int status;
789
1.50k
  struct block_cursor cursor;
790
1.50k
  struct interface_description_block *idbp;
791
792
  /*
793
   * Assume no read errors.
794
   */
795
1.50k
  *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.50k
  memcpy(&magic_int, magic, sizeof(magic_int));
802
1.50k
  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
208
    return (NULL);  /* nope */
813
208
  }
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.29k
  amt_read = fread(&total_length, 1, sizeof(total_length), fp);
827
1.29k
  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.29k
  amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
842
1.29k
  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.29k
  if (byte_order_magic != BYTE_ORDER_MAGIC) {
857
486
    byte_order_magic = PCAP_BSWAP_32(byte_order_magic);
858
486
    if (byte_order_magic != BYTE_ORDER_MAGIC) {
859
      /*
860
       * Not a pcapng file.
861
       */
862
93
      return (NULL);
863
93
    }
864
393
    swapped = 1;
865
393
    total_length = PCAP_BSWAP_32(total_length);
866
393
  }
867
868
  /*
869
   * Check the sanity of the total length.
870
   */
871
1.20k
  if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer) ||
872
1.19k
            (total_length > BT_SHB_INSANE_MAX)) {
873
44
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
874
44
        "Section Header Block in pcapng dump file has invalid length %zu < _%u_ < %u (BT_SHB_INSANE_MAX)",
875
44
        sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer),
876
44
        total_length,
877
44
        BT_SHB_INSANE_MAX);
878
879
44
    *err = 1;
880
44
    return (NULL);
881
44
  }
882
883
  /*
884
   * OK, this is a good pcapng file.
885
   * Allocate a pcap_t for it.
886
   */
887
1.15k
  p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_ng_sf);
888
1.15k
  if (p == NULL) {
889
    /* Allocation failed. */
890
0
    *err = 1;
891
0
    return (NULL);
892
0
  }
893
1.15k
  p->swapped = swapped;
894
1.15k
  ps = p->priv;
895
896
  /*
897
   * What precision does the user want?
898
   */
899
1.15k
  switch (precision) {
900
901
1.15k
  case PCAP_TSTAMP_PRECISION_MICRO:
902
1.15k
    ps->user_tsresol = 1000000;
903
1.15k
    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.15k
  }
916
917
1.15k
  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.15k
  p->bufsize = 2048;
936
1.15k
  if (p->bufsize < total_length)
937
53
    p->bufsize = total_length;
938
1.15k
  p->buffer = malloc(p->bufsize);
939
1.15k
  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.15k
  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.15k
  bhdrp = (struct block_header *)p->buffer;
952
1.15k
  shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header));
953
1.15k
  bhdrp->block_type = magic_int;
954
1.15k
  bhdrp->total_length = total_length;
955
1.15k
  shbp->byte_order_magic = byte_order_magic;
956
1.15k
  if (read_bytes(fp,
957
1.15k
      p->buffer + (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
958
1.15k
      total_length - (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
959
1.15k
      1, errbuf) == -1)
960
66
    goto fail;
961
962
1.09k
  if (p->swapped) {
963
    /*
964
     * Byte-swap the fields we've read.
965
     */
966
360
    shbp->major_version = PCAP_BSWAP_16(shbp->major_version);
967
360
    shbp->minor_version = PCAP_BSWAP_16(shbp->minor_version);
968
969
    /*
970
     * XXX - we don't care about the section length.
971
     */
972
360
  }
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.09k
  if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
988
1.07k
         (shbp->minor_version == PCAP_NG_VERSION_MINOR ||
989
49
          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.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.54k
  for (;;) {
1007
    /*
1008
     * Read the next block.
1009
     */
1010
1.54k
    status = read_block(fp, p, &cursor, errbuf);
1011
1.54k
    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.52k
    if (status == -1)
1018
166
      goto fail;  /* error */
1019
1.36k
    switch (cursor.block_type) {
1020
1021
869
    case BT_IDB:
1022
      /*
1023
       * Get a pointer to the fixed-length portion of the
1024
       * IDB.
1025
       */
1026
869
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1027
869
          errbuf);
1028
869
      if (idbp == NULL)
1029
2
        goto fail;  /* error */
1030
1031
      /*
1032
       * Byte-swap it if necessary.
1033
       */
1034
867
      if (p->swapped) {
1035
282
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1036
282
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1037
282
      }
1038
1039
      /*
1040
       * Try to add this interface.
1041
       */
1042
867
      if (!add_interface(p, idbp, &cursor, errbuf))
1043
73
        goto fail;
1044
1045
794
      goto done;
1046
1047
794
    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
488
    default:
1060
      /*
1061
       * Just ignore it.
1062
       */
1063
488
      break;
1064
1.36k
    }
1065
1.36k
  }
1066
1067
794
done:
1068
794
  p->linktype = linktype_to_dlt(idbp->linktype);
1069
794
  p->snapshot = pcapint_adjust_snapshot(p->linktype, idbp->snaplen);
1070
794
  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
794
  if (MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
1078
29
    ps->max_blocksize = MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype));
1079
1080
794
  p->next_packet_op = pcap_ng_next_packet;
1081
794
  p->cleanup_op = pcap_ng_cleanup;
1082
1083
794
  return (p);
1084
1085
364
fail:
1086
364
  free(ps->ifaces);
1087
364
  free(p->buffer);
1088
364
  free(p);
1089
364
  *err = 1;
1090
364
  return (NULL);
1091
1.05k
}
1092
1093
static void
1094
pcap_ng_cleanup(pcap_t *p)
1095
794
{
1096
794
  struct pcap_ng_sf *ps = p->priv;
1097
1098
794
  free(ps->ifaces);
1099
794
  pcapint_sf_cleanup(p);
1100
794
}
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
2.85k
{
1110
2.85k
  struct pcap_ng_sf *ps = p->priv;
1111
2.85k
  struct block_cursor cursor;
1112
2.85k
  int status;
1113
2.85k
  struct enhanced_packet_block *epbp;
1114
2.85k
  struct simple_packet_block *spbp;
1115
2.85k
  struct packet_block *pbp;
1116
2.85k
  bpf_u_int32 interface_id = 0xFFFFFFFF;
1117
2.85k
  struct interface_description_block *idbp;
1118
2.85k
  struct section_header_block *shbp;
1119
2.85k
  FILE *fp = p->rfile;
1120
2.85k
  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.86k
  for (;;) {
1127
    /*
1128
     * Read the block type and length; those are common
1129
     * to all blocks.
1130
     */
1131
6.86k
    status = read_block(fp, p, &cursor, p->errbuf);
1132
6.86k
    if (status == 0)
1133
346
      return (0);  /* EOF */
1134
6.52k
    if (status == -1)
1135
57
      return (-1);  /* error */
1136
6.46k
    switch (cursor.block_type) {
1137
1138
472
    case BT_EPB:
1139
      /*
1140
       * Get a pointer to the fixed-length portion of the
1141
       * EPB.
1142
       */
1143
472
      epbp = get_from_block_data(&cursor, sizeof(*epbp),
1144
472
          p->errbuf);
1145
472
      if (epbp == NULL)
1146
4
        return (-1);  /* error */
1147
1148
      /*
1149
       * Byte-swap it if necessary.
1150
       */
1151
468
      if (p->swapped) {
1152
        /* these were written in opposite byte order */
1153
255
        interface_id = PCAP_BSWAP_32(epbp->interface_id);
1154
255
        hdr->caplen = PCAP_BSWAP_32(epbp->caplen);
1155
255
        hdr->len = PCAP_BSWAP_32(epbp->len);
1156
255
        t = ((uint64_t)PCAP_BSWAP_32(epbp->timestamp_high)) << 32 |
1157
255
            PCAP_BSWAP_32(epbp->timestamp_low);
1158
255
      } else {
1159
213
        interface_id = epbp->interface_id;
1160
213
        hdr->caplen = epbp->caplen;
1161
213
        hdr->len = epbp->len;
1162
213
        t = ((uint64_t)epbp->timestamp_high) << 32 |
1163
213
            epbp->timestamp_low;
1164
213
      }
1165
468
      goto found;
1166
1167
1.33k
    case BT_SPB:
1168
      /*
1169
       * Get a pointer to the fixed-length portion of the
1170
       * SPB.
1171
       */
1172
1.33k
      spbp = get_from_block_data(&cursor, sizeof(*spbp),
1173
1.33k
          p->errbuf);
1174
1.33k
      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.33k
      interface_id = 0;
1182
1183
      /*
1184
       * Byte-swap it if necessary.
1185
       */
1186
1.33k
      if (p->swapped) {
1187
        /* these were written in opposite byte order */
1188
314
        hdr->len = PCAP_BSWAP_32(spbp->len);
1189
314
      } else
1190
1.02k
        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.33k
      hdr->caplen = hdr->len;
1198
1.33k
      if (hdr->caplen > (bpf_u_int32)p->snapshot)
1199
241
        hdr->caplen = p->snapshot;
1200
1.33k
      t = 0;  /* no time stamps */
1201
1.33k
      goto found;
1202
1203
429
    case BT_PB:
1204
      /*
1205
       * Get a pointer to the fixed-length portion of the
1206
       * PB.
1207
       */
1208
429
      pbp = get_from_block_data(&cursor, sizeof(*pbp),
1209
429
          p->errbuf);
1210
429
      if (pbp == NULL)
1211
4
        return (-1);  /* error */
1212
1213
      /*
1214
       * Byte-swap it if necessary.
1215
       */
1216
425
      if (p->swapped) {
1217
        /* these were written in opposite byte order */
1218
208
        interface_id = PCAP_BSWAP_16(pbp->interface_id);
1219
208
        hdr->caplen = PCAP_BSWAP_32(pbp->caplen);
1220
208
        hdr->len = PCAP_BSWAP_32(pbp->len);
1221
208
        t = ((uint64_t)PCAP_BSWAP_32(pbp->timestamp_high)) << 32 |
1222
208
            PCAP_BSWAP_32(pbp->timestamp_low);
1223
217
      } else {
1224
217
        interface_id = pbp->interface_id;
1225
217
        hdr->caplen = pbp->caplen;
1226
217
        hdr->len = pbp->len;
1227
217
        t = ((uint64_t)pbp->timestamp_high) << 32 |
1228
217
            pbp->timestamp_low;
1229
217
      }
1230
425
      goto found;
1231
1232
3.15k
    case BT_IDB:
1233
      /*
1234
       * Interface Description Block.  Get a pointer
1235
       * to its fixed-length portion.
1236
       */
1237
3.15k
      idbp = get_from_block_data(&cursor, sizeof(*idbp),
1238
3.15k
          p->errbuf);
1239
3.15k
      if (idbp == NULL)
1240
1
        return (-1);  /* error */
1241
1242
      /*
1243
       * Byte-swap it if necessary.
1244
       */
1245
3.15k
      if (p->swapped) {
1246
1.24k
        idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
1247
1.24k
        idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
1248
1.24k
      }
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
3.15k
      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
3.15k
      if ((bpf_u_int32)p->snapshot !=
1270
3.15k
          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
3.09k
      if (!add_interface(p, idbp, &cursor, p->errbuf))
1281
5
        return (-1);
1282
3.09k
      break;
1283
1284
3.09k
    case BT_SHB:
1285
      /*
1286
       * Section Header Block.  Get a pointer
1287
       * to its fixed-length portion.
1288
       */
1289
558
      shbp = get_from_block_data(&cursor, sizeof(*shbp),
1290
558
          p->errbuf);
1291
558
      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
555
      if (p->swapped) {
1300
218
        shbp->byte_order_magic =
1301
218
            PCAP_BSWAP_32(shbp->byte_order_magic);
1302
218
        shbp->major_version =
1303
218
            PCAP_BSWAP_16(shbp->major_version);
1304
218
      }
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
555
      switch (shbp->byte_order_magic) {
1312
1313
422
      case BYTE_ORDER_MAGIC:
1314
        /*
1315
         * OK.
1316
         */
1317
422
        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
132
      default:
1328
        /*
1329
         * Not a valid SHB.
1330
         */
1331
132
        snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1332
132
            "the file has a section with a bad byte order magic field");
1333
132
        return (-1);
1334
555
      }
1335
1336
      /*
1337
       * Make sure the major version is the version
1338
       * we handle.
1339
       */
1340
422
      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
406
      ps->ifcount = 0;
1357
406
      break;
1358
1359
518
    default:
1360
      /*
1361
       * Not a packet block, IDB, or SHB; ignore it.
1362
       */
1363
518
      break;
1364
6.46k
    }
1365
6.46k
  }
1366
1367
2.22k
found:
1368
  /*
1369
   * Is the interface ID an interface we know?
1370
   */
1371
2.22k
  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.17k
  if (hdr->caplen > (bpf_u_int32)p->snapshot) {
1382
36
    snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1383
36
        "invalid packet capture length %u, bigger than "
1384
36
        "snaplen of %d", hdr->caplen, p->snapshot);
1385
36
    return (-1);
1386
36
  }
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.13k
  sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
1393
2.13k
  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.13k
  switch (ps->ifaces[interface_id].scale_type) {
1400
1401
808
  case PASS_THROUGH:
1402
    /*
1403
     * The interface resolution is what the user wants,
1404
     * so we're done.
1405
     */
1406
808
    break;
1407
1408
251
  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
251
    frac *= ps->ifaces[interface_id].scale_factor;
1423
251
    break;
1424
1425
239
  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
239
    frac *= ps->user_tsresol;
1446
239
    frac /= ps->ifaces[interface_id].tsresol;
1447
239
    break;
1448
1449
322
  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
322
    frac /= ps->ifaces[interface_id].scale_factor;
1466
322
    break;
1467
1468
1469
514
  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
514
    frac *= ps->user_tsresol;
1493
514
    frac /= ps->ifaces[interface_id].tsresol;
1494
514
    break;
1495
2.13k
  }
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.13k
  hdr->ts.tv_sec = (time_t)sec;
1511
2.13k
  hdr->ts.tv_usec = (int)frac;
1512
2.13k
#endif
1513
1514
  /*
1515
   * Get a pointer to the packet data.
1516
   */
1517
2.13k
  *data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
1518
2.13k
  if (*data == NULL)
1519
75
    return (-1);
1520
1521
2.05k
  pcapint_post_process(p->linktype, p->swapped, hdr, *data);
1522
1523
2.05k
  return (1);
1524
2.13k
}