xref: /aosp_15_r20/external/tcpdump/print-ipfc.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997
3*05b00f60SXin Li  *	The Regents of the University of California.  All rights reserved.
4*05b00f60SXin Li  *
5*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
6*05b00f60SXin Li  * modification, are permitted provided that: (1) source code distributions
7*05b00f60SXin Li  * retain the above copyright notice and this paragraph in its entirety, (2)
8*05b00f60SXin Li  * distributions including binary code include the above copyright notice and
9*05b00f60SXin Li  * this paragraph in its entirety in the documentation or other materials
10*05b00f60SXin Li  * provided with the distribution, and (3) all advertising materials mentioning
11*05b00f60SXin Li  * features or use of this software display the following acknowledgement:
12*05b00f60SXin Li  * ``This product includes software developed by the University of California,
13*05b00f60SXin Li  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*05b00f60SXin Li  * the University nor the names of its contributors may be used to endorse
15*05b00f60SXin Li  * or promote products derived from this software without specific prior
16*05b00f60SXin Li  * written permission.
17*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*05b00f60SXin Li  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*05b00f60SXin Li  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*05b00f60SXin Li  */
21*05b00f60SXin Li 
22*05b00f60SXin Li /* \summary: IP over Fibre Channel printer */
23*05b00f60SXin Li 
24*05b00f60SXin Li /* specification: RFC 2625 */
25*05b00f60SXin Li 
26*05b00f60SXin Li #ifdef HAVE_CONFIG_H
27*05b00f60SXin Li #include <config.h>
28*05b00f60SXin Li #endif
29*05b00f60SXin Li 
30*05b00f60SXin Li #include "netdissect-stdinc.h"
31*05b00f60SXin Li 
32*05b00f60SXin Li #include <string.h>
33*05b00f60SXin Li 
34*05b00f60SXin Li #define ND_LONGJMP_FROM_TCHECK
35*05b00f60SXin Li #include "netdissect.h"
36*05b00f60SXin Li #include "addrtoname.h"
37*05b00f60SXin Li 
38*05b00f60SXin Li 
39*05b00f60SXin Li struct ipfc_header {
40*05b00f60SXin Li 	nd_byte ipfc_dhost[2+MAC_ADDR_LEN];
41*05b00f60SXin Li 	nd_byte ipfc_shost[2+MAC_ADDR_LEN];
42*05b00f60SXin Li };
43*05b00f60SXin Li 
44*05b00f60SXin Li #define IPFC_HDRLEN 16
45*05b00f60SXin Li 
46*05b00f60SXin Li /* Extract src, dst addresses */
47*05b00f60SXin Li static void
extract_ipfc_addrs(const struct ipfc_header * ipfcp,char * ipfcsrc,char * ipfcdst)48*05b00f60SXin Li extract_ipfc_addrs(const struct ipfc_header *ipfcp, char *ipfcsrc,
49*05b00f60SXin Li 		   char *ipfcdst)
50*05b00f60SXin Li {
51*05b00f60SXin Li 	/*
52*05b00f60SXin Li 	 * We assume that, as per RFC 2625, the lower 48 bits of the
53*05b00f60SXin Li 	 * source and destination addresses are MAC addresses.
54*05b00f60SXin Li 	 */
55*05b00f60SXin Li 	memcpy(ipfcdst, (const char *)&ipfcp->ipfc_dhost[2], MAC_ADDR_LEN);
56*05b00f60SXin Li 	memcpy(ipfcsrc, (const char *)&ipfcp->ipfc_shost[2], MAC_ADDR_LEN);
57*05b00f60SXin Li }
58*05b00f60SXin Li 
59*05b00f60SXin Li /*
60*05b00f60SXin Li  * Print the Network_Header
61*05b00f60SXin Li  */
62*05b00f60SXin Li static void
ipfc_hdr_print(netdissect_options * ndo,const struct ipfc_header * ipfcp _U_,u_int length,const u_char * ipfcsrc,const u_char * ipfcdst)63*05b00f60SXin Li ipfc_hdr_print(netdissect_options *ndo,
64*05b00f60SXin Li 	       const struct ipfc_header *ipfcp _U_, u_int length,
65*05b00f60SXin Li 	       const u_char *ipfcsrc, const u_char *ipfcdst)
66*05b00f60SXin Li {
67*05b00f60SXin Li 	const char *srcname, *dstname;
68*05b00f60SXin Li 
69*05b00f60SXin Li 	srcname = etheraddr_string(ndo, ipfcsrc);
70*05b00f60SXin Li 	dstname = etheraddr_string(ndo, ipfcdst);
71*05b00f60SXin Li 
72*05b00f60SXin Li 	/*
73*05b00f60SXin Li 	 * XXX - should we show the upper 16 bits of the addresses?
74*05b00f60SXin Li 	 * Do so only if "vflag" is set?
75*05b00f60SXin Li 	 * Section 3.3 "FC Port and Node Network Addresses" says that
76*05b00f60SXin Li 	 *
77*05b00f60SXin Li 	 *    In this specification, both the Source and Destination
78*05b00f60SXin Li 	 *    4-bit NAA identifiers SHALL be set to binary '0001'
79*05b00f60SXin Li 	 *    indicating that an IEEE 48-bit MAC address is contained
80*05b00f60SXin Li 	 *    in the lower 48 bits of the network address fields. The
81*05b00f60SXin Li 	 *    high order 12 bits in the network address fields SHALL
82*05b00f60SXin Li 	 *    be set to 0x0000.
83*05b00f60SXin Li 	 *
84*05b00f60SXin Li 	 * so, for captures following this specification, the upper 16
85*05b00f60SXin Li 	 * bits should be 0x1000, followed by a MAC address.
86*05b00f60SXin Li 	 */
87*05b00f60SXin Li 	ND_PRINT("%s > %s, length %u: ", srcname, dstname, length);
88*05b00f60SXin Li }
89*05b00f60SXin Li 
90*05b00f60SXin Li static u_int
ipfc_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen)91*05b00f60SXin Li ipfc_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
92*05b00f60SXin Li {
93*05b00f60SXin Li 	const struct ipfc_header *ipfcp = (const struct ipfc_header *)p;
94*05b00f60SXin Li 	nd_mac_addr srcmac, dstmac;
95*05b00f60SXin Li 	struct lladdr_info src, dst;
96*05b00f60SXin Li 	int llc_hdrlen;
97*05b00f60SXin Li 
98*05b00f60SXin Li 	ndo->ndo_protocol = "ipfc";
99*05b00f60SXin Li 	ND_TCHECK_LEN(p, IPFC_HDRLEN);
100*05b00f60SXin Li 	/*
101*05b00f60SXin Li 	 * Get the network addresses into a canonical form
102*05b00f60SXin Li 	 */
103*05b00f60SXin Li 	extract_ipfc_addrs(ipfcp, (char *)srcmac, (char *)dstmac);
104*05b00f60SXin Li 
105*05b00f60SXin Li 	if (ndo->ndo_eflag)
106*05b00f60SXin Li 		ipfc_hdr_print(ndo, ipfcp, length, srcmac, dstmac);
107*05b00f60SXin Li 
108*05b00f60SXin Li 	src.addr = srcmac;
109*05b00f60SXin Li 	src.addr_string = etheraddr_string;
110*05b00f60SXin Li 	dst.addr = dstmac;
111*05b00f60SXin Li 	dst.addr_string = etheraddr_string;
112*05b00f60SXin Li 
113*05b00f60SXin Li 	/* Skip over Network_Header */
114*05b00f60SXin Li 	length -= IPFC_HDRLEN;
115*05b00f60SXin Li 	p += IPFC_HDRLEN;
116*05b00f60SXin Li 	caplen -= IPFC_HDRLEN;
117*05b00f60SXin Li 
118*05b00f60SXin Li 	/* Try to print the LLC-layer header & higher layers */
119*05b00f60SXin Li 	llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
120*05b00f60SXin Li 	if (llc_hdrlen < 0) {
121*05b00f60SXin Li 		/*
122*05b00f60SXin Li 		 * Some kinds of LLC packet we cannot
123*05b00f60SXin Li 		 * handle intelligently
124*05b00f60SXin Li 		 */
125*05b00f60SXin Li 		if (!ndo->ndo_suppress_default_print)
126*05b00f60SXin Li 			ND_DEFAULTPRINT(p, caplen);
127*05b00f60SXin Li 		llc_hdrlen = -llc_hdrlen;
128*05b00f60SXin Li 	}
129*05b00f60SXin Li 	return (IPFC_HDRLEN + llc_hdrlen);
130*05b00f60SXin Li }
131*05b00f60SXin Li 
132*05b00f60SXin Li /*
133*05b00f60SXin Li  * This is the top level routine of the printer.  'p' points
134*05b00f60SXin Li  * to the Network_Header of the packet, 'h->ts' is the timestamp,
135*05b00f60SXin Li  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
136*05b00f60SXin Li  * is the number of bytes actually captured.
137*05b00f60SXin Li  */
138*05b00f60SXin Li void
ipfc_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)139*05b00f60SXin Li ipfc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
140*05b00f60SXin Li {
141*05b00f60SXin Li 	ndo->ndo_protocol = "ipfc";
142*05b00f60SXin Li 	ndo->ndo_ll_hdr_len += ipfc_print(ndo, p, h->len, h->caplen);
143*05b00f60SXin Li }
144