xref: /freebsd/contrib/tcpdump/print-ipfc.c (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*
2  * Copyright (c) 1991, 1992, 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 
22 /* \summary: IP over Fibre Channel printer */
23 
24 /* specification: RFC 2625 */
25 
26 #include <config.h>
27 
28 #include "netdissect-stdinc.h"
29 
30 #include <string.h>
31 
32 #define ND_LONGJMP_FROM_TCHECK
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 
36 
37 struct ipfc_header {
38 	nd_byte ipfc_dhost[2+MAC_ADDR_LEN];
39 	nd_byte ipfc_shost[2+MAC_ADDR_LEN];
40 };
41 
42 #define IPFC_HDRLEN 16
43 
44 /* Extract src, dst addresses */
45 static void
46 extract_ipfc_addrs(const struct ipfc_header *ipfcp, char *ipfcsrc,
47 		   char *ipfcdst)
48 {
49 	/*
50 	 * We assume that, as per RFC 2625, the lower 48 bits of the
51 	 * source and destination addresses are MAC addresses.
52 	 */
53 	memcpy(ipfcdst, (const char *)&ipfcp->ipfc_dhost[2], MAC_ADDR_LEN);
54 	memcpy(ipfcsrc, (const char *)&ipfcp->ipfc_shost[2], MAC_ADDR_LEN);
55 }
56 
57 /*
58  * Print the Network_Header
59  */
60 static void
61 ipfc_hdr_print(netdissect_options *ndo,
62 	       const struct ipfc_header *ipfcp _U_, u_int length,
63 	       const u_char *ipfcsrc, const u_char *ipfcdst)
64 {
65 	const char *srcname, *dstname;
66 
67 	srcname = etheraddr_string(ndo, ipfcsrc);
68 	dstname = etheraddr_string(ndo, ipfcdst);
69 
70 	/*
71 	 * XXX - should we show the upper 16 bits of the addresses?
72 	 * Do so only if "vflag" is set?
73 	 * Section 3.3 "FC Port and Node Network Addresses" says that
74 	 *
75 	 *    In this specification, both the Source and Destination
76 	 *    4-bit NAA identifiers SHALL be set to binary '0001'
77 	 *    indicating that an IEEE 48-bit MAC address is contained
78 	 *    in the lower 48 bits of the network address fields. The
79 	 *    high order 12 bits in the network address fields SHALL
80 	 *    be set to 0x0000.
81 	 *
82 	 * so, for captures following this specification, the upper 16
83 	 * bits should be 0x1000, followed by a MAC address.
84 	 */
85 	ND_PRINT("%s > %s, length %u: ", srcname, dstname, length);
86 }
87 
88 static u_int
89 ipfc_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
90 {
91 	const struct ipfc_header *ipfcp = (const struct ipfc_header *)p;
92 	nd_mac_addr srcmac, dstmac;
93 	struct lladdr_info src, dst;
94 	int llc_hdrlen;
95 
96 	ndo->ndo_protocol = "ipfc";
97 	ND_TCHECK_LEN(p, IPFC_HDRLEN);
98 	/*
99 	 * Get the network addresses into a canonical form
100 	 */
101 	extract_ipfc_addrs(ipfcp, (char *)srcmac, (char *)dstmac);
102 
103 	if (ndo->ndo_eflag)
104 		ipfc_hdr_print(ndo, ipfcp, length, srcmac, dstmac);
105 
106 	src.addr = srcmac;
107 	src.addr_string = etheraddr_string;
108 	dst.addr = dstmac;
109 	dst.addr_string = etheraddr_string;
110 
111 	/* Skip over Network_Header */
112 	length -= IPFC_HDRLEN;
113 	p += IPFC_HDRLEN;
114 	caplen -= IPFC_HDRLEN;
115 
116 	/* Try to print the LLC-layer header & higher layers */
117 	llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
118 	if (llc_hdrlen < 0) {
119 		/*
120 		 * Some kinds of LLC packet we cannot
121 		 * handle intelligently
122 		 */
123 		if (!ndo->ndo_suppress_default_print)
124 			ND_DEFAULTPRINT(p, caplen);
125 		llc_hdrlen = -llc_hdrlen;
126 	}
127 	return (IPFC_HDRLEN + llc_hdrlen);
128 }
129 
130 /*
131  * This is the top level routine of the printer.  'p' points
132  * to the Network_Header of the packet, 'h->ts' is the timestamp,
133  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
134  * is the number of bytes actually captured.
135  */
136 void
137 ipfc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
138 {
139 	ndo->ndo_protocol = "ipfc";
140 	ndo->ndo_ll_hdr_len += ipfc_print(ndo, p, h->len, h->caplen);
141 }
142