Coverage Report

Created: 2026-11-12 06:10

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