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