Home | History | Annotate | Line # | Download | only in dist
print-arcnet.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
      3  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8  1.1  christos  * distributions including binary code include the above copyright notice and
      9  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11  1.1  christos  * features or use of this software display the following acknowledgement:
     12  1.1  christos  * ``This product includes software developed by the University of California,
     13  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14  1.1  christos  * the University nor the names of its contributors may be used to endorse
     15  1.1  christos  * or promote products derived from this software without specific prior
     16  1.1  christos  * written permission.
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20  1.1  christos  *
     21  1.1  christos  * From: NetBSD: print-arcnet.c,v 1.2 2000/04/24 13:02:28 itojun Exp
     22  1.1  christos  */
     23  1.1  christos #ifndef lint
     24  1.1  christos static const char rcsid[] _U_ =
     25  1.1  christos     "@(#) Header: /tcpdump/master/tcpdump/print-arcnet.c,v 1.20 2005-04-06 21:32:38 mcr Exp (LBL)";
     26  1.1  christos #endif
     27  1.1  christos 
     28  1.1  christos #ifdef HAVE_CONFIG_H
     29  1.1  christos #include "config.h"
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos #include <tcpdump-stdinc.h>
     33  1.1  christos 
     34  1.1  christos #include <stdio.h>
     35  1.1  christos #include <pcap.h>
     36  1.1  christos 
     37  1.1  christos #include "interface.h"
     38  1.1  christos #include "extract.h"
     39  1.1  christos #include "arcnet.h"
     40  1.1  christos 
     41  1.1  christos static int arcnet_encap_print(u_char arctype, const u_char *p,
     42  1.1  christos     u_int length, u_int caplen);
     43  1.1  christos 
     44  1.1  christos struct tok arctypemap[] = {
     45  1.1  christos 	{ ARCTYPE_IP_OLD,	"oldip" },
     46  1.1  christos 	{ ARCTYPE_ARP_OLD,	"oldarp" },
     47  1.1  christos 	{ ARCTYPE_IP,		"ip" },
     48  1.1  christos 	{ ARCTYPE_ARP,		"arp" },
     49  1.1  christos 	{ ARCTYPE_REVARP,	"rarp" },
     50  1.1  christos 	{ ARCTYPE_ATALK,	"atalk" },
     51  1.1  christos 	{ ARCTYPE_BANIAN,	"banyan" },
     52  1.1  christos 	{ ARCTYPE_IPX,		"ipx" },
     53  1.1  christos 	{ ARCTYPE_INET6,	"ipv6" },
     54  1.1  christos 	{ ARCTYPE_DIAGNOSE,	"diag" },
     55  1.1  christos 	{ 0, 0 }
     56  1.1  christos };
     57  1.1  christos 
     58  1.1  christos static inline void
     59  1.1  christos arcnet_print(const u_char *bp, u_int length, int phds, int flag, u_int seqid)
     60  1.1  christos {
     61  1.1  christos 	const struct arc_header *ap;
     62  1.1  christos 	const char *arctypename;
     63  1.1  christos 
     64  1.1  christos 
     65  1.1  christos 	ap = (const struct arc_header *)bp;
     66  1.1  christos 
     67  1.1  christos 
     68  1.1  christos 	if (qflag) {
     69  1.1  christos 		(void)printf("%02x %02x %d: ",
     70  1.1  christos 			     ap->arc_shost,
     71  1.1  christos 			     ap->arc_dhost,
     72  1.1  christos 			     length);
     73  1.1  christos 		return;
     74  1.1  christos 	}
     75  1.1  christos 
     76  1.1  christos 	arctypename = tok2str(arctypemap, "%02x", ap->arc_type);
     77  1.1  christos 
     78  1.1  christos 	if (!phds) {
     79  1.1  christos 		(void)printf("%02x %02x %s %d: ",
     80  1.1  christos 			     ap->arc_shost, ap->arc_dhost, arctypename,
     81  1.1  christos 			     length);
     82  1.1  christos 			     return;
     83  1.1  christos 	}
     84  1.1  christos 
     85  1.1  christos 	if (flag == 0) {
     86  1.1  christos 		(void)printf("%02x %02x %s seqid %04x %d: ",
     87  1.1  christos 			ap->arc_shost, ap->arc_dhost, arctypename, seqid,
     88  1.1  christos 			length);
     89  1.1  christos 			return;
     90  1.1  christos 	}
     91  1.1  christos 
     92  1.1  christos 	if (flag & 1)
     93  1.1  christos 		(void)printf("%02x %02x %s seqid %04x "
     94  1.1  christos 			"(first of %d fragments) %d: ",
     95  1.1  christos 			ap->arc_shost, ap->arc_dhost, arctypename, seqid,
     96  1.1  christos 			(flag + 3) / 2, length);
     97  1.1  christos 	else
     98  1.1  christos 		(void)printf("%02x %02x %s seqid %04x "
     99  1.1  christos 			"(fragment %d) %d: ",
    100  1.1  christos 			ap->arc_shost, ap->arc_dhost, arctypename, seqid,
    101  1.1  christos 			flag/2 + 1, length);
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos /*
    105  1.1  christos  * This is the top level routine of the printer.  'p' points
    106  1.1  christos  * to the ARCNET header of the packet, 'h->ts' is the timestamp,
    107  1.1  christos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
    108  1.1  christos  * is the number of bytes actually captured.
    109  1.1  christos  */
    110  1.1  christos u_int
    111  1.1  christos arcnet_if_print(const struct pcap_pkthdr *h, const u_char *p)
    112  1.1  christos {
    113  1.1  christos 	u_int caplen = h->caplen;
    114  1.1  christos 	u_int length = h->len;
    115  1.1  christos 	const struct arc_header *ap;
    116  1.1  christos 
    117  1.1  christos 	int phds, flag = 0, archdrlen = 0;
    118  1.1  christos 	u_int seqid = 0;
    119  1.1  christos 	u_char arc_type;
    120  1.1  christos 
    121  1.1  christos 	if (caplen < ARC_HDRLEN) {
    122  1.1  christos 		printf("[|arcnet]");
    123  1.1  christos 		return (caplen);
    124  1.1  christos 	}
    125  1.1  christos 
    126  1.1  christos 	ap = (const struct arc_header *)p;
    127  1.1  christos 	arc_type = ap->arc_type;
    128  1.1  christos 
    129  1.1  christos 	switch (arc_type) {
    130  1.1  christos 	default:
    131  1.1  christos 		phds = 1;
    132  1.1  christos 		break;
    133  1.1  christos 	case ARCTYPE_IP_OLD:
    134  1.1  christos 	case ARCTYPE_ARP_OLD:
    135  1.1  christos 	case ARCTYPE_DIAGNOSE:
    136  1.1  christos 		phds = 0;
    137  1.1  christos 		archdrlen = ARC_HDRLEN;
    138  1.1  christos 		break;
    139  1.1  christos 	}
    140  1.1  christos 
    141  1.1  christos 	if (phds) {
    142  1.1  christos 		if (caplen < ARC_HDRNEWLEN) {
    143  1.1  christos 			arcnet_print(p, length, 0, 0, 0);
    144  1.1  christos 			printf("[|phds]");
    145  1.1  christos 			return (caplen);
    146  1.1  christos 		}
    147  1.1  christos 
    148  1.1  christos 		if (ap->arc_flag == 0xff) {
    149  1.1  christos 			if (caplen < ARC_HDRNEWLEN_EXC) {
    150  1.1  christos 				arcnet_print(p, length, 0, 0, 0);
    151  1.1  christos 				printf("[|phds extended]");
    152  1.1  christos 				return (caplen);
    153  1.1  christos 			}
    154  1.1  christos 			flag = ap->arc_flag2;
    155  1.1  christos 			seqid = EXTRACT_16BITS(&ap->arc_seqid2);
    156  1.1  christos 			archdrlen = ARC_HDRNEWLEN_EXC;
    157  1.1  christos 		} else {
    158  1.1  christos 			flag = ap->arc_flag;
    159  1.1  christos 			seqid = EXTRACT_16BITS(&ap->arc_seqid);
    160  1.1  christos 			archdrlen = ARC_HDRNEWLEN;
    161  1.1  christos 		}
    162  1.1  christos 	}
    163  1.1  christos 
    164  1.1  christos 
    165  1.1  christos 	if (eflag)
    166  1.1  christos 		arcnet_print(p, length, phds, flag, seqid);
    167  1.1  christos 
    168  1.1  christos 	/*
    169  1.1  christos 	 * Go past the ARCNET header.
    170  1.1  christos 	 */
    171  1.1  christos 	length -= archdrlen;
    172  1.1  christos 	caplen -= archdrlen;
    173  1.1  christos 	p += archdrlen;
    174  1.1  christos 
    175  1.1  christos 	if (phds && flag && (flag & 1) == 0) {
    176  1.1  christos 		/*
    177  1.1  christos 		 * This is a middle fragment.
    178  1.1  christos 		 */
    179  1.1  christos 		return (archdrlen);
    180  1.1  christos 	}
    181  1.1  christos 
    182  1.1  christos 	if (!arcnet_encap_print(arc_type, p, length, caplen))
    183  1.1  christos 		default_print(p, caplen);
    184  1.1  christos 
    185  1.1  christos 	return (archdrlen);
    186  1.1  christos }
    187  1.1  christos 
    188  1.1  christos /*
    189  1.1  christos  * This is the top level routine of the printer.  'p' points
    190  1.1  christos  * to the ARCNET header of the packet, 'h->ts' is the timestamp,
    191  1.1  christos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
    192  1.1  christos  * is the number of bytes actually captured.  It is quite similar
    193  1.1  christos  * to the non-Linux style printer except that Linux doesn't ever
    194  1.1  christos  * supply packets that look like exception frames, it always supplies
    195  1.1  christos  * reassembled packets rather than raw frames, and headers have an
    196  1.1  christos  * extra "offset" field between the src/dest and packet type.
    197  1.1  christos  */
    198  1.1  christos u_int
    199  1.1  christos arcnet_linux_if_print(const struct pcap_pkthdr *h, const u_char *p)
    200  1.1  christos {
    201  1.1  christos 	u_int caplen = h->caplen;
    202  1.1  christos 	u_int length = h->len;
    203  1.1  christos 	const struct arc_linux_header *ap;
    204  1.1  christos 
    205  1.1  christos 	int archdrlen = 0;
    206  1.1  christos 	u_char arc_type;
    207  1.1  christos 
    208  1.1  christos 	if (caplen < ARC_LINUX_HDRLEN) {
    209  1.1  christos 		printf("[|arcnet]");
    210  1.1  christos 		return (caplen);
    211  1.1  christos 	}
    212  1.1  christos 
    213  1.1  christos 	ap = (const struct arc_linux_header *)p;
    214  1.1  christos 	arc_type = ap->arc_type;
    215  1.1  christos 
    216  1.1  christos 	switch (arc_type) {
    217  1.1  christos 	default:
    218  1.1  christos 		archdrlen = ARC_LINUX_HDRNEWLEN;
    219  1.1  christos 		if (caplen < ARC_LINUX_HDRNEWLEN) {
    220  1.1  christos 			printf("[|arcnet]");
    221  1.1  christos 			return (caplen);
    222  1.1  christos 		}
    223  1.1  christos 		break;
    224  1.1  christos 	case ARCTYPE_IP_OLD:
    225  1.1  christos 	case ARCTYPE_ARP_OLD:
    226  1.1  christos 	case ARCTYPE_DIAGNOSE:
    227  1.1  christos 		archdrlen = ARC_LINUX_HDRLEN;
    228  1.1  christos 		break;
    229  1.1  christos 	}
    230  1.1  christos 
    231  1.1  christos 	if (eflag)
    232  1.1  christos 		arcnet_print(p, length, 0, 0, 0);
    233  1.1  christos 
    234  1.1  christos 	/*
    235  1.1  christos 	 * Go past the ARCNET header.
    236  1.1  christos 	 */
    237  1.1  christos 	length -= archdrlen;
    238  1.1  christos 	caplen -= archdrlen;
    239  1.1  christos 	p += archdrlen;
    240  1.1  christos 
    241  1.1  christos 	if (!arcnet_encap_print(arc_type, p, length, caplen))
    242  1.1  christos 		default_print(p, caplen);
    243  1.1  christos 
    244  1.1  christos 	return (archdrlen);
    245  1.1  christos }
    246  1.1  christos 
    247  1.1  christos /*
    248  1.1  christos  * Prints the packet encapsulated in an ARCnet data field,
    249  1.1  christos  * given the ARCnet system code.
    250  1.1  christos  *
    251  1.1  christos  * Returns non-zero if it can do so, zero if the system code is unknown.
    252  1.1  christos  */
    253  1.1  christos 
    254  1.1  christos 
    255  1.1  christos static int
    256  1.1  christos arcnet_encap_print(u_char arctype, const u_char *p,
    257  1.1  christos     u_int length, u_int caplen)
    258  1.1  christos {
    259  1.1  christos 	switch (arctype) {
    260  1.1  christos 
    261  1.1  christos 	case ARCTYPE_IP_OLD:
    262  1.1  christos 	case ARCTYPE_IP:
    263  1.1  christos 	        ip_print(gndo, p, length);
    264  1.1  christos 		return (1);
    265  1.1  christos 
    266  1.1  christos #ifdef INET6
    267  1.1  christos 	case ARCTYPE_INET6:
    268  1.1  christos 		ip6_print(p, length);
    269  1.1  christos 		return (1);
    270  1.1  christos #endif /*INET6*/
    271  1.1  christos 
    272  1.1  christos 	case ARCTYPE_ARP_OLD:
    273  1.1  christos 	case ARCTYPE_ARP:
    274  1.1  christos 	case ARCTYPE_REVARP:
    275  1.1  christos 	  arp_print(gndo, p, length, caplen);
    276  1.1  christos 		return (1);
    277  1.1  christos 
    278  1.1  christos 	case ARCTYPE_ATALK:	/* XXX was this ever used? */
    279  1.1  christos 		if (vflag)
    280  1.1  christos 			fputs("et1 ", stdout);
    281  1.1  christos 		atalk_print(p, length);
    282  1.1  christos 		return (1);
    283  1.1  christos 
    284  1.1  christos 	case ARCTYPE_IPX:
    285  1.1  christos 		ipx_print(p, length);
    286  1.1  christos 		return (1);
    287  1.1  christos 
    288  1.1  christos 	default:
    289  1.1  christos 		return (0);
    290  1.1  christos 	}
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos /*
    294  1.1  christos  * Local Variables:
    295  1.1  christos  * c-style: bsd
    296  1.1  christos  * End:
    297  1.1  christos  */
    298  1.1  christos 
    299