xref: /freebsd/contrib/tcpdump/print-ipoib.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3  *	1997, 2000, 2011, 2012
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that: (1) source code distributions
8  * retain the above copyright notice and this paragraph in its entirety, (2)
9  * distributions including binary code include the above copyright notice and
10  * this paragraph in its entirety in the documentation or other materials
11  * provided with the distribution, and (3) all advertising materials mentioning
12  * features or use of this software display the following acknowledgement:
13  * ``This product includes software developed by the University of California,
14  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15  * the University nor the names of its contributors may be used to endorse
16  * or promote products derived from this software without specific prior
17  * written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22 /*
23  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /* \summary: IP-over-InfiniBand (IPoIB) printer */
27 
28 #include <config.h>
29 
30 #include "netdissect-stdinc.h"
31 
32 #include "netdissect.h"
33 #include "extract.h"
34 #include "addrtoname.h"
35 
36 
37 extern const struct tok ethertype_values[];
38 
39 #define	IPOIB_HDRLEN	44
40 
41 static inline void
ipoib_hdr_print(netdissect_options * ndo,const u_char * bp,u_int length)42 ipoib_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
43 {
44 	uint16_t ether_type;
45 
46 	ether_type = GET_BE_U_2(bp + 40);
47 	if (!ndo->ndo_qflag) {
48 		ND_PRINT(", ethertype %s (0x%04x)",
49 			     tok2str(ethertype_values,"Unknown", ether_type),
50 			     ether_type);
51 	} else {
52 		ND_PRINT(", ethertype %s",
53 			     tok2str(ethertype_values,"Unknown", ether_type));
54 	}
55 
56 	ND_PRINT(", length %u: ", length);
57 }
58 
59 /*
60  * Print an InfiniBand frame.
61  * This might be encapsulated within another frame; we might be passed
62  * a pointer to a function that can print header information for that
63  * frame's protocol, and an argument to pass to that function.
64  */
65 static void
ipoib_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen,void (* print_encap_header)(const u_char *),const u_char * encap_header_arg)66 ipoib_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen,
67     void (*print_encap_header)(const u_char *), const u_char *encap_header_arg)
68 {
69 	const u_char *orig_hdr = p;
70 	u_int orig_length;
71 	u_short ether_type;
72 
73 	if (caplen < IPOIB_HDRLEN) {
74 		nd_print_trunc(ndo);
75 		ndo->ndo_ll_hdr_len += caplen;
76 		return;
77 	}
78 
79 	if (length < IPOIB_HDRLEN) {
80 		nd_print_trunc(ndo);
81 		ndo->ndo_ll_hdr_len += length;
82 		return;
83 	}
84 
85 	if (ndo->ndo_eflag) {
86 		nd_print_protocol_caps(ndo);
87 		if (print_encap_header != NULL)
88 			(*print_encap_header)(encap_header_arg);
89 		ipoib_hdr_print(ndo, p, length);
90 	}
91 	orig_length = length;
92 
93 	ndo->ndo_ll_hdr_len += IPOIB_HDRLEN;
94 	length -= IPOIB_HDRLEN;
95 	caplen -= IPOIB_HDRLEN;
96 	ether_type = GET_BE_U_2(p + 40);
97 	p += IPOIB_HDRLEN;
98 
99 	if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
100 		/* ether_type not known, print raw packet */
101 		if (!ndo->ndo_eflag) {
102 			if (print_encap_header != NULL)
103 				(*print_encap_header)(encap_header_arg);
104 			ipoib_hdr_print(ndo, orig_hdr , orig_length);
105 		}
106 
107 		if (!ndo->ndo_suppress_default_print)
108 			ND_DEFAULTPRINT(p, caplen);
109 	}
110 }
111 
112 /*
113  * This is the top level routine of the printer.  'p' points
114  * to the ether header of the packet, 'h->ts' is the timestamp,
115  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
116  * is the number of bytes actually captured.
117  */
118 void
ipoib_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)119 ipoib_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
120 {
121 	ndo->ndo_protocol = "ipoib";
122 	ipoib_print(ndo, p, h->len, h->caplen, NULL, NULL);
123 }
124