Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
/*	$NetBSD: uipc_mbufdebug.c,v 1.7 2018/10/18 05:44:19 msaitoh Exp $	*/

/*
 * Copyright (C) 2017 Internet Initiative Japan Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_mbufdebug.c,v 1.7 2018/10/18 05:44:19 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/ppp_defs.h>
#include <net/if_arp.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/if_inarp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>

#define EXAMINE_HEX_LIMIT 128
#define EXAMINE_HEX_COL 4

/* mbuf operations without change of mbuf chain */
static int m_peek_data(const struct mbuf *, int, int, void *);
static unsigned int m_peek_len(const struct mbuf *, const char *);

/* utility */
static char *str_ethaddr(const uint8_t *);
static char *str_ipaddr(const struct in_addr *);
static char *str_ip6addr(const struct in6_addr *);
static const char *str_ipproto(const uint8_t);

/* header structure for some protocol */
struct pppoehdr {
	uint8_t vertype;
	uint8_t code;
	uint16_t session;
	uint16_t plen;
} __attribute__((__packed__));

struct pppoetag {
	uint16_t tag;
	uint16_t len;
} __attribute__((__packed__));

#define PPPOE_TAG_EOL 0x0000
#define PPPOE_CODE_PADI		0x09	/* Active Discovery Initiation */
#define	PPPOE_CODE_PADO		0x07	/* Active Discovery Offer */
#define	PPPOE_CODE_PADR		0x19	/* Active Discovery Request */
#define	PPPOE_CODE_PADS		0x65	/* Active Discovery Session confirmation */
#define	PPPOE_CODE_PADT		0xA7	/* Active Discovery Terminate */

struct ppp_header {
	uint8_t address;
	uint8_t control;
	uint16_t protocol;
} __attribute__((__packed__));

#define CISCO_MULTICAST		0x8f	/* Cisco multicast address */
#define CISCO_UNICAST		0x0f	/* Cisco unicast address */
#define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */

#ifndef NELEMS
#define NELEMS(elem) ((sizeof(elem))/(sizeof((elem)[0])))
#endif

static int
m_peek_data(const struct mbuf *m, int off, int len, void *vp)
{
	unsigned int count;
	char *cp = vp;

	if (off < 0 || len < 0)
		return -1;

	while (off > 0) {
		if (m == 0)
			return -1;
		if (off < m->m_len)
			break;
		off -= m->m_len;
		m = m->m_next;
	}
	while (len > 0) {
		if (m == 0)
			return -1;
		count = uimin(m->m_len - off, len);
		memcpy(cp, mtod(m, char *) + off, count);
		len -= count;
		cp += count;
		off = 0;
		m = m->m_next;
	}

	return 0;
}

static unsigned int
m_peek_len(const struct mbuf *m, const char *modif)
{
	const struct mbuf *m0;
	unsigned int pktlen;
	bool opt_c = false;
	unsigned char ch;

	while (modif && (ch = *(modif++)) != '\0') {
		switch (ch) {
		case 'c':
			opt_c = true;
			break;
		}
	}

	if (opt_c == true)
		return m->m_len;

	if ((m->m_flags & M_PKTHDR) != 0)
		return m->m_pkthdr.len;

	pktlen = 0;
	for (m0 = m; m0 != NULL; m0 = m0->m_next)
		pktlen += m0->m_len;

	return pktlen;
}

static char *
str_ethaddr(const uint8_t *ap)
{
	static char buf[3 * ETHER_ADDR_LEN];

	return ether_snprintf(buf, sizeof(buf), ap);
}

static char *
str_ipaddr(const struct in_addr *ap)
{
	static char buf[INET_ADDRSTRLEN];

	return IN_PRINT(buf, ap);
}

static char *
str_ip6addr(const struct in6_addr *ap)
{
	static char buf[INET6_ADDRSTRLEN];

	return IN6_PRINT(buf, ap);
}

static const char *
str_ipproto(const uint8_t proto)
{

	switch (proto) {
	case IPPROTO_HOPOPTS:
		return ("IPv6 Hop-by-Hop");
		break;
	case IPPROTO_TCP:
		return("TCP");
		break;
	case IPPROTO_UDP:
		return("UDP");
		break;
	case IPPROTO_ICMP:
		return("ICMP");
		break;
	case IPPROTO_IGMP:
		return("IGMP");
		break;
	case IPPROTO_ESP:
		return("ESP");
		break;
	case IPPROTO_AH:
		return("AH");
		break;
	case IPPROTO_IPV6_ICMP:
		return("ICMP6");
	default:
		return("unknown");
		break;
	}

	/* not reached */
	return NULL;
}

void
m_examine_ether(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	struct ether_header eh;
	unsigned int pktlen;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(eh)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(eh));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(eh), (void *)(&eh)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(eh);

	(*pr)("ETHER: DST = %s\n", str_ethaddr(eh.ether_dhost));
	(*pr)("ETHER: SRC = %s\n", str_ethaddr(eh.ether_shost));

	(*pr)("ETHER: TYPE = 0x%04x(", ntohs(eh.ether_type));
	switch (ntohs(eh.ether_type)) {
	case ETHERTYPE_PPPOE:
		(*pr)("PPPoE)\n");
		return m_examine_pppoe(m, off, modif, pr);
		break;
	case ETHERTYPE_ARP:
		(*pr)("ARP)\n");
		return m_examine_arp(m, off, modif, pr);
		break;
	case ETHERTYPE_IP:
		(*pr)("IPv4)\n");
		return m_examine_ip(m, off, modif, pr);
		break;
	case ETHERTYPE_IPV6:
		(*pr)("IPv6)\n");
		return m_examine_ip6(m, off, modif, pr);
		break;
	default:
		(*pr)("unknown)\n");
		break;
	}

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_pppoe(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	struct pppoehdr ph;
	struct pppoetag pt;
	unsigned int pktlen;
	uint8_t vt;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(ph)) {
		(*pr)("%s: too short mbuf chain (%u, %u)\n", __func__,
		    pktlen, sizeof(ph));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(ph), (void *)(&ph)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(ph);

	while (off + sizeof(pt) > pktlen) {
		if (m_peek_data(m, off, sizeof(pt), (void *)(&pt)) < 0) {
			(*pr)("%s: cannot read header\n", __func__);
			return m_examine_hex(m, off, modif, pr);
		}
		off += sizeof(pt);

		if (ntohs(pt.tag) == PPPOE_TAG_EOL)
			break;
		off += ntohs(pt.len);
	}

	vt = ph.vertype;

	(*pr)("PPPoE: Version = %u\n", ((vt >> 4) & 0xff));
	(*pr)("PPPoE: Type = %u\n", (vt & 0xff));
	(*pr)("PPPoE: Code = %u(", ph.code);
	switch (ph.code) {
	case 0:
		(*pr)("DATA");
		break;
	case PPPOE_CODE_PADI:
		(*pr)("PADI");
		break;
	case PPPOE_CODE_PADO:
		(*pr)("PADO");
		break;
	case PPPOE_CODE_PADS:
		(*pr)("PADS");
		break;
	case PPPOE_CODE_PADT:
		(*pr)("PADT");
		break;
	default:
		(*pr)("unknown");
		break;
	}
	(*pr)(")\n");

	(*pr)("PPPoE: Session = 0x%04x\n", ntohs(ph.session));
	(*pr)("PPPoE: Payload Length = %u\n", ntohs(ph.plen));

	switch (ph.code) {
	case PPPOE_CODE_PADI:
	case PPPOE_CODE_PADO:
	case PPPOE_CODE_PADS:
	case PPPOE_CODE_PADT:
		(*pr)("No parser for PPPoE control frame.\n");
		return m_examine_hex(m, off, modif, pr);
		break;
	}

	if (ph.code != 0) {
		(*pr)("Unknown PPPoE code.\n");
		return m_examine_hex(m, off, modif, pr);
	}

	return m_examine_ppp(m, off, modif, pr);
}

void
m_examine_ppp(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	struct ppp_header h;
	unsigned int pktlen;
	uint16_t protocol;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(h)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(h));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(h), (void *)(&h)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(h);

	protocol = ntohs(h.protocol);

	(*pr)("SPPP: Address = %d(", h.address);
	switch (h.address) {
	case PPP_ALLSTATIONS:
		(*pr)("ALLSTATIONS)\n");
		(*pr)("SPPP: Protocol = %d(", protocol);
		switch (protocol) {
		case PPP_LCP:
			(*pr)("LCP)\n");
			break;
		case PPP_PAP:
			(*pr)("PAP)\n");
			break;
		case PPP_CHAP:
			(*pr)("CHAP)\n");
			break;
		case PPP_IPCP:
			(*pr)("IPCP)\n");
			break;
		case PPP_IPV6CP:
			(*pr)("IPV6CP)\n");
			break;
		case PPP_IP:
			(*pr)("IP)\n");
			return m_examine_ip(m, off, modif, pr);
			break;
		case PPP_IPV6:
			(*pr)("IPv6)\n");
			return m_examine_ip6(m, off, modif, pr);
			break;
		default:
			(*pr)("unknown)\n");
			break;
		}
		break;
	case CISCO_MULTICAST:
	case CISCO_UNICAST:
		if (h.address == CISCO_MULTICAST)
			(*pr)("MULTICAST)\n");
		else
			(*pr)("UNICAST)\n");

		(*pr)("SPPP: Protocol = %d(", protocol);
		switch (protocol) {
		case CISCO_KEEPALIVE:
			(*pr)("Keepalive)\n");
			break;
		case ETHERTYPE_IP:
			(*pr)("IP)\n");
			return m_examine_ip(m, off, modif, pr);
			break;
		case ETHERTYPE_IPV6:
			(*pr)("IPv6)\n");
			return m_examine_ip6(m, off, modif, pr);
			break;
		default:
			(*pr)("unknown)\n");
			break;
		}
		break;
	default:
		(*pr)("unknown)\n", h.address);
		break;
	}

	(*pr)("No parser for address %d, protocol %d\n", h.address, protocol);
	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_arp(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct arphdr ar;
	uint16_t hrd, op;
	struct in_addr isaddr, itaddr;
	uint8_t esaddr[ETHER_ADDR_LEN], etaddr[ETHER_ADDR_LEN];

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(ar)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(ar));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(ar), (void *)(&ar)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(ar);

	hrd = ntohs(ar.ar_hrd);
	(*pr)("ARP: AddressType = %u(", hrd);
	switch (hrd) {
	case ARPHRD_ETHER:
		(*pr)("ETHER)\n");
		break;
	case ARPHRD_IEEE802:
		(*pr)("IEEE802)\n");
		break;
	default:
		(*pr)("unknown)\n");
		return m_examine_hex(m, off, modif, pr);
		break;
	}
	(*pr)("ARP: Protocol Address Format = %u\n", ntohs(ar.ar_pro));
	(*pr)("ARP: Protocol Address Length = %u\n", ar.ar_pln);
	(*pr)("ARP: H/W Address Length = %u\n", ar.ar_hln);
	op = ntohs(ar.ar_op);
	(*pr)("ARP: Operation = %u(", op);
	switch (op) {
	case ARPOP_REQUEST:
		(*pr)("REQUEST)\n");
		break;
	case ARPOP_REPLY:
		(*pr)("REPLY)\n");
		break;
	case ARPOP_REVREQUEST:
		(*pr)("REVREQUEST)\n");
		break;
	case ARPOP_REVREPLY:
		(*pr)("REVREPLY)\n");
		break;
	case ARPOP_INVREQUEST:
		(*pr)("INVREQUEST)\n");
		break;
	case ARPOP_INVREPLY:
		(*pr)("INVREPLY)\n");
		break;
	}

	if (ar.ar_hln == 0 || ar.ar_pln == 0 ||
	    ar.ar_hln != sizeof(esaddr) || ar.ar_pln != sizeof(isaddr)) {
		(*pr)("Cannot parse.\n");
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(esaddr), (void *)(esaddr)) < 0) {
		(*pr)("Cannot read payload\n");
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(esaddr);
	(*pr)("ARP: Ether Src = %s\n", str_ethaddr(esaddr));

	if (m_peek_data(m, off, sizeof(isaddr), (void *)(&isaddr)) < 0) {
		(*pr)("Cannot read payload\n");
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(isaddr);
	(*pr)("ARP: IP Src = %s\n", str_ipaddr(&isaddr));

	if (m_peek_data(m, off, sizeof(etaddr), (void *)(etaddr)) < 0) {
		(*pr)("Cannot read payload\n");
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(etaddr);
	(*pr)("ARP: Ether Tgt = %s\n", str_ethaddr(etaddr));

	if (m_peek_data(m, off, sizeof(itaddr), (void *)(&itaddr)) < 0) {
		(*pr)("Cannot read payload\n");
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(itaddr);
	(*pr)("ARP: IP Tgt = %s\n", str_ipaddr(&itaddr));

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_ip(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct ip ip;
	uint16_t offset;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(ip)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(ip));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(ip), (void *)(&ip)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(ip);

	(*pr)("IP: Version = %u\n", ip.ip_v);
	(*pr)("IP: Header Length = %u\n", (ip.ip_hl << 2));
	(*pr)("IP: ToS = 0x%02x\n", ip.ip_tos);
	(*pr)("IP: Packet Length = %u\n", ntohs(ip.ip_len));
	(*pr)("IP: ID = %u\n", ntohs(ip.ip_id));
	offset = ntohs(ip.ip_off);
	(*pr)("IP: Offset = %u\n", (offset & IP_OFFMASK));
	if (offset & IP_RF)
		(*pr)("IP: Flag 0x%04x (reserved)\n", IP_RF);
	if (offset & IP_EF)
		(*pr)("IP: Flag 0x%04x (evil flag)\n", IP_EF);
	if (offset & IP_DF)
		(*pr)("IP: Flag 0x%04x (don't fragment)\n", IP_DF);
	if (offset & IP_MF)
		(*pr)("IP: Flag 0x%04x (more fragment)\n", IP_MF);
	(*pr)("IP: TTL = %u\n", ip.ip_ttl);
	(*pr)("IP: protocol = %u(%s)\n", ip.ip_p, str_ipproto(ip.ip_p));
	(*pr)("IP: checksum = 0x%04x\n", ntohs(ip.ip_sum));
	(*pr)("IP: Src = %s\n", str_ipaddr(&ip.ip_src));
	(*pr)("IP: Dst = %s\n", str_ipaddr(&ip.ip_dst));

	switch (ip.ip_p) {
	case IPPROTO_ICMP:
		return m_examine_icmp(m, off, modif, pr);
		break;
	case IPPROTO_TCP:
		return m_examine_tcp(m, off, modif, pr);
		break;
	case IPPROTO_UDP:
		return m_examine_udp(m, off, modif, pr);
		break;
	default:
		break;
	}

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_icmp(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct icmp icmphdr;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(icmphdr)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(icmphdr));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(icmphdr), (void *)(&icmphdr)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(icmphdr);

	(*pr)("ICMP: Type = %u(", icmphdr.icmp_type);
	switch (icmphdr.icmp_type) {
	case ICMP_ECHOREPLY:
		(*pr)("Echo Reply)\n");
		break;
	case ICMP_UNREACH:
		(*pr)("Destination Unreachable)\n");
		break;
	case ICMP_SOURCEQUENCH:
		(*pr)("Source Quench)\n");
		break;
	case ICMP_REDIRECT:
		(*pr)("Redirect)\n");
		break;
	case ICMP_TIMXCEED:
		(*pr)("Time Exceeded)\n");
		break;
	default:
		(*pr)("unknown)\n");
		break;
	}
	(*pr)("ICMP: Code = %d\n", icmphdr.icmp_code);

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_ip6(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct ip6_hdr ip6;
	struct ip6_hbh hbh;
	int hbhlen;
	uint32_t flow;
	uint8_t vfc;
	uint8_t nxt;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(ip6)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(ip6));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(ip6), (void *)(&ip6)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(ip6);

	vfc = ip6.ip6_vfc;
	(*pr)("IPv6: Version = %u\n", (vfc & IPV6_VERSION_MASK) >> 4);
	flow = ntohl(ip6.ip6_flow);
	(*pr)("IPv6: Flow INFO = 0x%07x\n", flow & IPV6_FLOWINFO_MASK);
	(*pr)("IPv6: Payload Length = %u\n", ntohs(ip6.ip6_plen));
	nxt = ip6.ip6_nxt;
	(*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
	(*pr)("IPv6: Hop Limit = %u\n", ip6.ip6_hlim);
	(*pr)("IPv6: Src = %s\n", str_ip6addr(&ip6.ip6_src));
	(*pr)("IPv6: Dst = %s\n", str_ip6addr(&ip6.ip6_dst));

	/* Strip Hop-by-Hop options */
	if (nxt == IPPROTO_HOPOPTS) {
		if (m_peek_data(m, off, sizeof(hbh), (void *)(&hbh)) < 0) {
			(*pr)("Cannot read option\n");
			return m_examine_hex(m, off, modif, pr);
		}
		hbhlen = (hbh.ip6h_len + 1) << 3;
		nxt = hbh.ip6h_nxt;
		off += hbhlen;

		(*pr)("IPv6: Stripped Hop-by-Hop\n");
		(*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
	}

	switch (nxt) {
	case IPPROTO_IPV6_ICMP:
		return m_examine_icmp6(m, off, modif, pr);
		break;
	case IPPROTO_TCP:
		return m_examine_tcp(m, off, modif, pr);
		break;
	case IPPROTO_UDP:
		return m_examine_udp(m, off, modif, pr);
		break;
	default:
		break;
	}

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_icmp6(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct icmp6_hdr icmp6;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(icmp6)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(icmp6));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(icmp6), (void *)(&icmp6)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(icmp6);

	(*pr)("ICMP6: Type = %u(", icmp6.icmp6_type);
	switch (icmp6.icmp6_type) {
	case ICMP6_DST_UNREACH:
		(*pr)("Destination Unreachable)\n");
		break;
	case ICMP6_PACKET_TOO_BIG:
		(*pr)("Packet Too Big)\n");
		break;
	case ICMP6_TIME_EXCEEDED:
		(*pr)("Time Exceeded)\n");
		break;
	case ICMP6_PARAM_PROB:
		(*pr)("Parameter Problem)\n");
		break;
	case ICMP6_ECHO_REQUEST:
		(*pr)("Echo Request)\n");
		break;
	case ICMP6_ECHO_REPLY:
		(*pr)("Echo Reply)\n");
		break;

	case MLD_LISTENER_QUERY:
		(*pr)("MLD Listener Query)\n");
		break;
	case MLD_LISTENER_REPORT:
		(*pr)("MLD Listener Report)\n");
		break;
	case MLD_LISTENER_DONE:
		(*pr)("MLD Listener Done)\n");
		break;

	case ND_ROUTER_SOLICIT:
		(*pr)("Router Solicitation)\n");
		break;
	case ND_ROUTER_ADVERT:
		(*pr)("Router Advertizement)\n");
		break;
	case ND_NEIGHBOR_SOLICIT:
		(*pr)("Neighbor Solicitation)\n");
		break;
	case ND_NEIGHBOR_ADVERT:
		(*pr)("Neighbor Advertizement)\n");
		break;
	case ND_REDIRECT:
		(*pr)("Redirect)\n");
		break;

	default:
		(*pr)("unknown)\n");
		break;
	}
	(*pr)("ICMP6: Code = %u\n", icmp6.icmp6_code);

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_tcp(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct tcphdr tcp;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(tcp)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(tcp));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(tcp), (void *)(&tcp)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(tcp);

	(*pr)("TCP: Src = %u\n", ntohs(tcp.th_sport));
	(*pr)("TCP: Dst = %u\n", ntohs(tcp.th_dport));
	(*pr)("TCP: Seq. = %u\n", ntohl(tcp.th_seq));
	(*pr)("TCP: Ack. = %u\n", ntohl(tcp.th_ack));
	(*pr)("TCP: Header Length = %u\n", tcp.th_off << 2);
	if (tcp.th_flags) {
		(*pr)("TCP: Flags 0x%02x : ", tcp.th_flags);
		if (tcp.th_flags & TH_FIN)
			(*pr)("FIN ");
		if (tcp.th_flags & TH_SYN)
			(*pr)("SYN ");
		if (tcp.th_flags & TH_RST)
			(*pr)("RST ");
		if (tcp.th_flags & TH_PUSH)
			(*pr)("PUSH ");
		if (tcp.th_flags & TH_URG)
			(*pr)("URG ");
		if (tcp.th_flags & TH_ECE)
			(*pr)("ECE ");
		if (tcp.th_flags & TH_CWR)
			(*pr)("CWR ");
		(*pr)("\n");
	}
	(*pr)("TCP: Windows Size = %u\n", ntohs(tcp.th_win));
	(*pr)("TCP: checksum = 0x%04x\n", ntohs(tcp.th_sum));
	(*pr)("TCP: Urgent Pointer = %u\n", ntohs(tcp.th_urp));

	int len;
	len = (tcp.th_off << 2) - sizeof(struct tcphdr);
	if (len > 0) {
		uint8_t *bufp, *op, opt, optlen;

		bufp = malloc(len, M_TEMP, M_DONTWAIT);
		if ((bufp == NULL) || (m_peek_data(m, off, len, bufp) < 0)) {
			(*pr)("%s: cannot read TCP option\n", __func__);
			if (bufp != NULL)
				free(bufp, M_TEMP);
			return m_examine_hex(m, off, modif, pr);
		}
		off += len;
		op = bufp;

		while (len > 0) {
			opt = op[0];
			if (opt == TCPOPT_EOL)
				break;
			if (opt == TCPOPT_NOP) {
				(*pr)("TCP: OPTION: NOP\n");
				op++;
				len--;
				continue;
			}
			if (opt == TCPOPT_PAD) {
				(*pr)("TCP: OPTION: PAD\n");
				op++;
				len--;
				continue;
			}
			optlen = op[1];
			if (optlen == 0)
				break;

			if (opt == TCPOPT_MAXSEG && optlen == TCPOLEN_MAXSEG) {
				uint16_t mss;

				bcopy(op + 2, &mss, sizeof(mss));
				(*pr)("TCP: OPTION: MSS = %d\n",
				    ntohs(mss));

				op += optlen;
				len -= optlen;
				continue;
			} else if (opt == TCPOPT_WINDOW
			    && optlen == TCPOLEN_WINDOW) {
				(*pr)("TCP: OPTION: wscale = %d\n", op[2]);
				op += optlen;
				len -= optlen;
				continue;
			} else if (opt == TCPOPT_SACK_PERMITTED
			    && optlen == TCPOLEN_SACK_PERMITTED) {
				(*pr)("TCP: OPTION: SACK OK\n");
				op += optlen;
				len -= optlen;
				continue;
			} else if (opt == TCPOPT_TIMESTAMP
			    && optlen == TCPOLEN_TIMESTAMP) {
				uint32_t ts_val, ts_ecr;

				memcpy(&ts_val, op + 2, sizeof(ts_val));
				memcpy(&ts_ecr, op + 6, sizeof(ts_ecr));
				(*pr)("TCP: OPTION: TIMESTAMP = %u, "
				    "ECR = %u\n",
				    ntohl(ts_val), ntohl(ts_ecr));
				op += optlen;
				len -= optlen;
				continue;
			} else {
				(*pr)("TCP: OPTION: unknown (%d, len = %d)\n",
				    opt, optlen);
				op += optlen;
				len -= optlen;
				continue;
			}
		}
		free(bufp, M_TEMP);
	}

	if (off < pktlen)
		m_examine_hex(m, off, modif, pr);

	return;
}

void
m_examine_udp(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	struct udphdr udp;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen < sizeof(udp)) {
		(*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
		    pktlen, sizeof(udp));
		return m_examine_hex(m, off, modif, pr);
	}

	if (m_peek_data(m, off, sizeof(udp), (void *)(&udp)) < 0) {
		(*pr)("%s: cannot read header\n", __func__);
		return m_examine_hex(m, off, modif, pr);
	}
	off += sizeof(udp);

	(*pr)("UDP: Src = %u\n", ntohs(udp.uh_sport));
	(*pr)("UDP: Dst = %u\n", ntohs(udp.uh_dport));
	(*pr)("UDP: Length = %u\n", ntohs(udp.uh_ulen));

	return m_examine_hex(m, off, modif, pr);
}

void
m_examine_hex(const struct mbuf *m, int off, const char *modif,
    void (*pr)(const char *, ...))
{
	unsigned int pktlen;
	int newline = 0;
	uint8_t v;

	pktlen = m_peek_len(m, modif) - off;
	if (pktlen > EXAMINE_HEX_LIMIT)
		pktlen = EXAMINE_HEX_LIMIT;

	if (pktlen == 0)
		return;

	(*pr)("offset %04d: ", off);
	while (pktlen > 0) {
		if (m_peek_data(m, off, sizeof(v), (void *)(&v)) < 0)
			break;
		pktlen --;
		off++;
		newline++;

		(*pr)("%02x", v);
		if (pktlen == 0)
			break;

		if ((newline % EXAMINE_HEX_COL) == 0) {
			(*pr)("\n");
			(*pr)("offset %04d: ", off);
		} else
			(*pr)(" ");
	}
	(*pr)("\n");
}

void
m_examine(const struct mbuf *m, int af, const char *modif,
    void (*pr)(const char *, ...))
{
	if (m == NULL)
		return;

	if (pr == NULL)
		return;

	switch (af) {
	case AF_UNSPEC:
		return m_examine_hex(m, 0, modif, pr);
		break;
	case AF_ETHER:
		return m_examine_ether(m, 0, modif, pr);
		break;
	case AF_ARP:
		return m_examine_arp(m, 0, modif, pr);
		break;
	case AF_INET:
		return m_examine_ip(m, 0, modif, pr);
		break;
	case AF_INET6:
		return m_examine_ip6(m, 0, modif, pr);
		break;
	default:
		(*pr)("No parser for AF %d\n", af);
		return m_examine_hex(m, 0, modif, pr);
		break;
	}

	/* not reached */
	return;
}