1a5779b6eSRui Paulo /*
2a5779b6eSRui Paulo * Copyright (c) 1998-2007 The TCPDUMP project
3a5779b6eSRui Paulo *
4a5779b6eSRui Paulo * Redistribution and use in source and binary forms, with or without
5a5779b6eSRui Paulo * modification, are permitted provided that: (1) source code
6a5779b6eSRui Paulo * distributions retain the above copyright notice and this paragraph
7a5779b6eSRui Paulo * in its entirety, and (2) distributions including binary code include
8a5779b6eSRui Paulo * the above copyright notice and this paragraph in its entirety in
9a5779b6eSRui Paulo * the documentation or other materials provided with the distribution.
10a5779b6eSRui Paulo * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11a5779b6eSRui Paulo * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12a5779b6eSRui Paulo * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13a5779b6eSRui Paulo * FOR A PARTICULAR PURPOSE.
14a5779b6eSRui Paulo *
15a5779b6eSRui Paulo * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16cac3dcd5SXin LI *
17cac3dcd5SXin LI * Expansion and refactoring by Rick Jones <rick.jones2@hp.com>
18a5779b6eSRui Paulo */
19a5779b6eSRui Paulo
203340d773SGleb Smirnoff /* \summary: sFlow protocol printer */
213340d773SGleb Smirnoff
22ee67461eSJoseph Mingrone /* specification: https://sflow.org/developers/specifications.php */
233340d773SGleb Smirnoff
24ee67461eSJoseph Mingrone #include <config.h>
25a5779b6eSRui Paulo
26ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
27a5779b6eSRui Paulo
28ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
30a5779b6eSRui Paulo #include "extract.h"
31a5779b6eSRui Paulo #include "addrtoname.h"
32a5779b6eSRui Paulo
33a5779b6eSRui Paulo /*
34a5779b6eSRui Paulo * sFlow datagram
35a5779b6eSRui Paulo *
36a5779b6eSRui Paulo * 0 1 2 3
37a5779b6eSRui Paulo * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
38a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39a5779b6eSRui Paulo * | Sflow version (2,4,5) |
40a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41a5779b6eSRui Paulo * | IP version (1 for IPv4 | 2 for IPv6) |
42a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43a5779b6eSRui Paulo * | IP Address AGENT (4 or 16 bytes) |
44a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45a5779b6eSRui Paulo * | Sub agent ID |
46a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47a5779b6eSRui Paulo * | Datagram sequence number |
48a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49a5779b6eSRui Paulo * | Switch uptime in ms |
50a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51a5779b6eSRui Paulo * | num samples in datagram |
52a5779b6eSRui Paulo * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53a5779b6eSRui Paulo *
54a5779b6eSRui Paulo */
55a5779b6eSRui Paulo
56a5779b6eSRui Paulo struct sflow_datagram_t {
57ee67461eSJoseph Mingrone nd_uint32_t version;
58ee67461eSJoseph Mingrone nd_uint32_t ip_version;
59ee67461eSJoseph Mingrone nd_ipv4 agent;
60ee67461eSJoseph Mingrone nd_uint32_t agent_id;
61ee67461eSJoseph Mingrone nd_uint32_t seqnum;
62ee67461eSJoseph Mingrone nd_uint32_t uptime;
63ee67461eSJoseph Mingrone nd_uint32_t samples;
64ee67461eSJoseph Mingrone };
65ee67461eSJoseph Mingrone
66ee67461eSJoseph Mingrone struct sflow_v6_datagram_t {
67ee67461eSJoseph Mingrone nd_uint32_t version;
68ee67461eSJoseph Mingrone nd_uint32_t ip_version;
69ee67461eSJoseph Mingrone nd_ipv6 agent;
70ee67461eSJoseph Mingrone nd_uint32_t agent_id;
71ee67461eSJoseph Mingrone nd_uint32_t seqnum;
72ee67461eSJoseph Mingrone nd_uint32_t uptime;
73ee67461eSJoseph Mingrone nd_uint32_t samples;
74a5779b6eSRui Paulo };
75a5779b6eSRui Paulo
76a5779b6eSRui Paulo struct sflow_sample_header {
77ee67461eSJoseph Mingrone nd_uint32_t format;
78ee67461eSJoseph Mingrone nd_uint32_t len;
79a5779b6eSRui Paulo };
80a5779b6eSRui Paulo
81a5779b6eSRui Paulo #define SFLOW_FLOW_SAMPLE 1
82a5779b6eSRui Paulo #define SFLOW_COUNTER_SAMPLE 2
83a5779b6eSRui Paulo #define SFLOW_EXPANDED_FLOW_SAMPLE 3
84a5779b6eSRui Paulo #define SFLOW_EXPANDED_COUNTER_SAMPLE 4
85a5779b6eSRui Paulo
86a5779b6eSRui Paulo static const struct tok sflow_format_values[] = {
87a5779b6eSRui Paulo { SFLOW_FLOW_SAMPLE, "flow sample" },
88a5779b6eSRui Paulo { SFLOW_COUNTER_SAMPLE, "counter sample" },
89a5779b6eSRui Paulo { SFLOW_EXPANDED_FLOW_SAMPLE, "expanded flow sample" },
90a5779b6eSRui Paulo { SFLOW_EXPANDED_COUNTER_SAMPLE, "expanded counter sample" },
91a5779b6eSRui Paulo { 0, NULL}
92a5779b6eSRui Paulo };
93a5779b6eSRui Paulo
94cac3dcd5SXin LI struct sflow_flow_sample_t {
95ee67461eSJoseph Mingrone nd_uint32_t seqnum;
96ee67461eSJoseph Mingrone nd_uint8_t type;
97ee67461eSJoseph Mingrone nd_uint24_t index;
98ee67461eSJoseph Mingrone nd_uint32_t rate;
99ee67461eSJoseph Mingrone nd_uint32_t pool;
100ee67461eSJoseph Mingrone nd_uint32_t drops;
101ee67461eSJoseph Mingrone nd_uint32_t in_interface;
102ee67461eSJoseph Mingrone nd_uint32_t out_interface;
103ee67461eSJoseph Mingrone nd_uint32_t records;
104cac3dcd5SXin LI
105cac3dcd5SXin LI };
106cac3dcd5SXin LI
107a5779b6eSRui Paulo struct sflow_expanded_flow_sample_t {
108ee67461eSJoseph Mingrone nd_uint32_t seqnum;
109ee67461eSJoseph Mingrone nd_uint32_t type;
110ee67461eSJoseph Mingrone nd_uint32_t index;
111ee67461eSJoseph Mingrone nd_uint32_t rate;
112ee67461eSJoseph Mingrone nd_uint32_t pool;
113ee67461eSJoseph Mingrone nd_uint32_t drops;
114ee67461eSJoseph Mingrone nd_uint32_t in_interface_format;
115ee67461eSJoseph Mingrone nd_uint32_t in_interface_value;
116ee67461eSJoseph Mingrone nd_uint32_t out_interface_format;
117ee67461eSJoseph Mingrone nd_uint32_t out_interface_value;
118ee67461eSJoseph Mingrone nd_uint32_t records;
119a5779b6eSRui Paulo };
120a5779b6eSRui Paulo
121a5779b6eSRui Paulo #define SFLOW_FLOW_RAW_PACKET 1
122a5779b6eSRui Paulo #define SFLOW_FLOW_ETHERNET_FRAME 2
123a5779b6eSRui Paulo #define SFLOW_FLOW_IPV4_DATA 3
124a5779b6eSRui Paulo #define SFLOW_FLOW_IPV6_DATA 4
125a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_SWITCH_DATA 1001
126a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_ROUTER_DATA 1002
127a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_GATEWAY_DATA 1003
128a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_USER_DATA 1004
129a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_URL_DATA 1005
130a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_MPLS_DATA 1006
131a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_NAT_DATA 1007
132a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_MPLS_TUNNEL 1008
133a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_MPLS_VC 1009
134a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_MPLS_FEC 1010
135a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC 1011
136a5779b6eSRui Paulo #define SFLOW_FLOW_EXTENDED_VLAN_TUNNEL 1012
137a5779b6eSRui Paulo
138a5779b6eSRui Paulo static const struct tok sflow_flow_type_values[] = {
139a5779b6eSRui Paulo { SFLOW_FLOW_RAW_PACKET, "Raw packet"},
140a5779b6eSRui Paulo { SFLOW_FLOW_ETHERNET_FRAME, "Ethernet frame"},
141a5779b6eSRui Paulo { SFLOW_FLOW_IPV4_DATA, "IPv4 Data"},
142a5779b6eSRui Paulo { SFLOW_FLOW_IPV6_DATA, "IPv6 Data"},
143a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_SWITCH_DATA, "Extended Switch data"},
144a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_ROUTER_DATA, "Extended Router data"},
145a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_GATEWAY_DATA, "Extended Gateway data"},
146a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_USER_DATA, "Extended User data"},
147a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_URL_DATA, "Extended URL data"},
148a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_MPLS_DATA, "Extended MPLS data"},
149a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_NAT_DATA, "Extended NAT data"},
150a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_MPLS_TUNNEL, "Extended MPLS tunnel"},
151a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_MPLS_VC, "Extended MPLS VC"},
152a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_MPLS_FEC, "Extended MPLS FEC"},
153a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC, "Extended MPLS LVP FEC"},
154a5779b6eSRui Paulo { SFLOW_FLOW_EXTENDED_VLAN_TUNNEL, "Extended VLAN Tunnel"},
155a5779b6eSRui Paulo { 0, NULL}
156a5779b6eSRui Paulo };
157a5779b6eSRui Paulo
158a5779b6eSRui Paulo #define SFLOW_HEADER_PROTOCOL_ETHERNET 1
159a5779b6eSRui Paulo #define SFLOW_HEADER_PROTOCOL_IPV4 11
160a5779b6eSRui Paulo #define SFLOW_HEADER_PROTOCOL_IPV6 12
161a5779b6eSRui Paulo
162a5779b6eSRui Paulo static const struct tok sflow_flow_raw_protocol_values[] = {
163a5779b6eSRui Paulo { SFLOW_HEADER_PROTOCOL_ETHERNET, "Ethernet"},
164a5779b6eSRui Paulo { SFLOW_HEADER_PROTOCOL_IPV4, "IPv4"},
165a5779b6eSRui Paulo { SFLOW_HEADER_PROTOCOL_IPV6, "IPv6"},
166a5779b6eSRui Paulo { 0, NULL}
167a5779b6eSRui Paulo };
168a5779b6eSRui Paulo
169a5779b6eSRui Paulo struct sflow_expanded_flow_raw_t {
170ee67461eSJoseph Mingrone nd_uint32_t protocol;
171ee67461eSJoseph Mingrone nd_uint32_t length;
172ee67461eSJoseph Mingrone nd_uint32_t stripped_bytes;
173ee67461eSJoseph Mingrone nd_uint32_t header_size;
174a5779b6eSRui Paulo };
175a5779b6eSRui Paulo
176cac3dcd5SXin LI struct sflow_ethernet_frame_t {
177ee67461eSJoseph Mingrone nd_uint32_t length;
178ee67461eSJoseph Mingrone nd_byte src_mac[8];
179ee67461eSJoseph Mingrone nd_byte dst_mac[8];
180ee67461eSJoseph Mingrone nd_uint32_t type;
181cac3dcd5SXin LI };
182cac3dcd5SXin LI
183cac3dcd5SXin LI struct sflow_extended_switch_data_t {
184ee67461eSJoseph Mingrone nd_uint32_t src_vlan;
185ee67461eSJoseph Mingrone nd_uint32_t src_pri;
186ee67461eSJoseph Mingrone nd_uint32_t dst_vlan;
187ee67461eSJoseph Mingrone nd_uint32_t dst_pri;
188cac3dcd5SXin LI };
189cac3dcd5SXin LI
190cac3dcd5SXin LI struct sflow_counter_record_t {
191ee67461eSJoseph Mingrone nd_uint32_t format;
192ee67461eSJoseph Mingrone nd_uint32_t length;
193cac3dcd5SXin LI };
194cac3dcd5SXin LI
195cac3dcd5SXin LI struct sflow_flow_record_t {
196ee67461eSJoseph Mingrone nd_uint32_t format;
197ee67461eSJoseph Mingrone nd_uint32_t length;
198cac3dcd5SXin LI };
199cac3dcd5SXin LI
200cac3dcd5SXin LI struct sflow_counter_sample_t {
201ee67461eSJoseph Mingrone nd_uint32_t seqnum;
202ee67461eSJoseph Mingrone nd_uint8_t type;
203ee67461eSJoseph Mingrone nd_uint24_t index;
204ee67461eSJoseph Mingrone nd_uint32_t records;
205cac3dcd5SXin LI };
206cac3dcd5SXin LI
207a5779b6eSRui Paulo struct sflow_expanded_counter_sample_t {
208ee67461eSJoseph Mingrone nd_uint32_t seqnum;
209ee67461eSJoseph Mingrone nd_uint32_t type;
210ee67461eSJoseph Mingrone nd_uint32_t index;
211ee67461eSJoseph Mingrone nd_uint32_t records;
212a5779b6eSRui Paulo };
213a5779b6eSRui Paulo
214a5779b6eSRui Paulo #define SFLOW_COUNTER_GENERIC 1
215a5779b6eSRui Paulo #define SFLOW_COUNTER_ETHERNET 2
216a5779b6eSRui Paulo #define SFLOW_COUNTER_TOKEN_RING 3
217a5779b6eSRui Paulo #define SFLOW_COUNTER_BASEVG 4
218a5779b6eSRui Paulo #define SFLOW_COUNTER_VLAN 5
219a5779b6eSRui Paulo #define SFLOW_COUNTER_PROCESSOR 1001
220a5779b6eSRui Paulo
221a5779b6eSRui Paulo static const struct tok sflow_counter_type_values[] = {
222a5779b6eSRui Paulo { SFLOW_COUNTER_GENERIC, "Generic counter"},
223a5779b6eSRui Paulo { SFLOW_COUNTER_ETHERNET, "Ethernet counter"},
224a5779b6eSRui Paulo { SFLOW_COUNTER_TOKEN_RING, "Token ring counter"},
225a5779b6eSRui Paulo { SFLOW_COUNTER_BASEVG, "100 BaseVG counter"},
226a5779b6eSRui Paulo { SFLOW_COUNTER_VLAN, "Vlan counter"},
227a5779b6eSRui Paulo { SFLOW_COUNTER_PROCESSOR, "Processor counter"},
228a5779b6eSRui Paulo { 0, NULL}
229a5779b6eSRui Paulo };
230a5779b6eSRui Paulo
231a5779b6eSRui Paulo #define SFLOW_IFACE_DIRECTION_UNKNOWN 0
232a5779b6eSRui Paulo #define SFLOW_IFACE_DIRECTION_FULLDUPLEX 1
233a5779b6eSRui Paulo #define SFLOW_IFACE_DIRECTION_HALFDUPLEX 2
234a5779b6eSRui Paulo #define SFLOW_IFACE_DIRECTION_IN 3
235a5779b6eSRui Paulo #define SFLOW_IFACE_DIRECTION_OUT 4
236a5779b6eSRui Paulo
237a5779b6eSRui Paulo static const struct tok sflow_iface_direction_values[] = {
238a5779b6eSRui Paulo { SFLOW_IFACE_DIRECTION_UNKNOWN, "unknown"},
239a5779b6eSRui Paulo { SFLOW_IFACE_DIRECTION_FULLDUPLEX, "full-duplex"},
240a5779b6eSRui Paulo { SFLOW_IFACE_DIRECTION_HALFDUPLEX, "half-duplex"},
241a5779b6eSRui Paulo { SFLOW_IFACE_DIRECTION_IN, "in"},
242a5779b6eSRui Paulo { SFLOW_IFACE_DIRECTION_OUT, "out"},
243a5779b6eSRui Paulo { 0, NULL}
244a5779b6eSRui Paulo };
245a5779b6eSRui Paulo
246a5779b6eSRui Paulo struct sflow_generic_counter_t {
247ee67461eSJoseph Mingrone nd_uint32_t ifindex;
248ee67461eSJoseph Mingrone nd_uint32_t iftype;
249ee67461eSJoseph Mingrone nd_uint64_t ifspeed;
250ee67461eSJoseph Mingrone nd_uint32_t ifdirection;
251ee67461eSJoseph Mingrone nd_uint32_t ifstatus;
252ee67461eSJoseph Mingrone nd_uint64_t ifinoctets;
253ee67461eSJoseph Mingrone nd_uint32_t ifinunicastpkts;
254ee67461eSJoseph Mingrone nd_uint32_t ifinmulticastpkts;
255ee67461eSJoseph Mingrone nd_uint32_t ifinbroadcastpkts;
256ee67461eSJoseph Mingrone nd_uint32_t ifindiscards;
257ee67461eSJoseph Mingrone nd_uint32_t ifinerrors;
258ee67461eSJoseph Mingrone nd_uint32_t ifinunkownprotos;
259ee67461eSJoseph Mingrone nd_uint64_t ifoutoctets;
260ee67461eSJoseph Mingrone nd_uint32_t ifoutunicastpkts;
261ee67461eSJoseph Mingrone nd_uint32_t ifoutmulticastpkts;
262ee67461eSJoseph Mingrone nd_uint32_t ifoutbroadcastpkts;
263ee67461eSJoseph Mingrone nd_uint32_t ifoutdiscards;
264ee67461eSJoseph Mingrone nd_uint32_t ifouterrors;
265ee67461eSJoseph Mingrone nd_uint32_t ifpromiscmode;
266a5779b6eSRui Paulo };
267a5779b6eSRui Paulo
268a5779b6eSRui Paulo struct sflow_ethernet_counter_t {
269ee67461eSJoseph Mingrone nd_uint32_t alignerrors;
270ee67461eSJoseph Mingrone nd_uint32_t fcserrors;
271ee67461eSJoseph Mingrone nd_uint32_t single_collision_frames;
272ee67461eSJoseph Mingrone nd_uint32_t multiple_collision_frames;
273ee67461eSJoseph Mingrone nd_uint32_t test_errors;
274ee67461eSJoseph Mingrone nd_uint32_t deferred_transmissions;
275ee67461eSJoseph Mingrone nd_uint32_t late_collisions;
276ee67461eSJoseph Mingrone nd_uint32_t excessive_collisions;
277ee67461eSJoseph Mingrone nd_uint32_t mac_transmit_errors;
278ee67461eSJoseph Mingrone nd_uint32_t carrier_sense_errors;
279ee67461eSJoseph Mingrone nd_uint32_t frame_too_longs;
280ee67461eSJoseph Mingrone nd_uint32_t mac_receive_errors;
281ee67461eSJoseph Mingrone nd_uint32_t symbol_errors;
282a5779b6eSRui Paulo };
283a5779b6eSRui Paulo
284a5779b6eSRui Paulo struct sflow_100basevg_counter_t {
285ee67461eSJoseph Mingrone nd_uint32_t in_highpriority_frames;
286ee67461eSJoseph Mingrone nd_uint64_t in_highpriority_octets;
287ee67461eSJoseph Mingrone nd_uint32_t in_normpriority_frames;
288ee67461eSJoseph Mingrone nd_uint64_t in_normpriority_octets;
289ee67461eSJoseph Mingrone nd_uint32_t in_ipmerrors;
290ee67461eSJoseph Mingrone nd_uint32_t in_oversized;
291ee67461eSJoseph Mingrone nd_uint32_t in_data_errors;
292ee67461eSJoseph Mingrone nd_uint32_t in_null_addressed_frames;
293ee67461eSJoseph Mingrone nd_uint32_t out_highpriority_frames;
294ee67461eSJoseph Mingrone nd_uint64_t out_highpriority_octets;
295ee67461eSJoseph Mingrone nd_uint32_t transitioninto_frames;
296ee67461eSJoseph Mingrone nd_uint64_t hc_in_highpriority_octets;
297ee67461eSJoseph Mingrone nd_uint64_t hc_in_normpriority_octets;
298ee67461eSJoseph Mingrone nd_uint64_t hc_out_highpriority_octets;
299a5779b6eSRui Paulo };
300a5779b6eSRui Paulo
301a5779b6eSRui Paulo struct sflow_vlan_counter_t {
302ee67461eSJoseph Mingrone nd_uint32_t vlan_id;
303ee67461eSJoseph Mingrone nd_uint64_t octets;
304ee67461eSJoseph Mingrone nd_uint32_t unicast_pkt;
305ee67461eSJoseph Mingrone nd_uint32_t multicast_pkt;
306ee67461eSJoseph Mingrone nd_uint32_t broadcast_pkt;
307ee67461eSJoseph Mingrone nd_uint32_t discards;
308a5779b6eSRui Paulo };
309a5779b6eSRui Paulo
310cac3dcd5SXin LI static int
print_sflow_counter_generic(netdissect_options * ndo,const u_char * pointer,u_int len)3113c602fabSXin LI print_sflow_counter_generic(netdissect_options *ndo,
3128bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
3138bdc5a62SPatrick Kelsey {
314cac3dcd5SXin LI const struct sflow_generic_counter_t *sflow_gen_counter;
315cac3dcd5SXin LI
316cac3dcd5SXin LI if (len < sizeof(struct sflow_generic_counter_t))
317cac3dcd5SXin LI return 1;
318cac3dcd5SXin LI
319cac3dcd5SXin LI sflow_gen_counter = (const struct sflow_generic_counter_t *)pointer;
320ee67461eSJoseph Mingrone ND_PRINT("\n\t ifindex %u, iftype %u, ifspeed %" PRIu64 ", ifdirection %u (%s)",
321ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifindex),
322ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->iftype),
323ee67461eSJoseph Mingrone GET_BE_U_8(sflow_gen_counter->ifspeed),
324ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifdirection),
325cac3dcd5SXin LI tok2str(sflow_iface_direction_values, "Unknown",
326ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifdirection)));
327ee67461eSJoseph Mingrone ND_PRINT("\n\t ifstatus %u, adminstatus: %s, operstatus: %s",
328ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifstatus),
329ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifstatus)&1 ? "up" : "down",
330ee67461eSJoseph Mingrone (GET_BE_U_4(sflow_gen_counter->ifstatus)>>1)&1 ? "up" : "down");
331ee67461eSJoseph Mingrone ND_PRINT("\n\t In octets %" PRIu64
332cac3dcd5SXin LI ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
333ee67461eSJoseph Mingrone GET_BE_U_8(sflow_gen_counter->ifinoctets),
334ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifinunicastpkts),
335ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifinmulticastpkts),
336ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifinbroadcastpkts),
337ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifindiscards));
338ee67461eSJoseph Mingrone ND_PRINT("\n\t In errors %u, unknown protos %u",
339ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifinerrors),
340ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifinunkownprotos));
341ee67461eSJoseph Mingrone ND_PRINT("\n\t Out octets %" PRIu64
342cac3dcd5SXin LI ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
343ee67461eSJoseph Mingrone GET_BE_U_8(sflow_gen_counter->ifoutoctets),
344ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifoutunicastpkts),
345ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifoutmulticastpkts),
346ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifoutbroadcastpkts),
347ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifoutdiscards));
348ee67461eSJoseph Mingrone ND_PRINT("\n\t Out errors %u, promisc mode %u",
349ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifouterrors),
350ee67461eSJoseph Mingrone GET_BE_U_4(sflow_gen_counter->ifpromiscmode));
351cac3dcd5SXin LI
352cac3dcd5SXin LI return 0;
353cac3dcd5SXin LI }
354cac3dcd5SXin LI
355cac3dcd5SXin LI static int
print_sflow_counter_ethernet(netdissect_options * ndo,const u_char * pointer,u_int len)3563c602fabSXin LI print_sflow_counter_ethernet(netdissect_options *ndo,
3578bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
3588bdc5a62SPatrick Kelsey {
359cac3dcd5SXin LI const struct sflow_ethernet_counter_t *sflow_eth_counter;
360cac3dcd5SXin LI
361cac3dcd5SXin LI if (len < sizeof(struct sflow_ethernet_counter_t))
362cac3dcd5SXin LI return 1;
363cac3dcd5SXin LI
364cac3dcd5SXin LI sflow_eth_counter = (const struct sflow_ethernet_counter_t *)pointer;
365ee67461eSJoseph Mingrone ND_PRINT("\n\t align errors %u, fcs errors %u, single collision %u, multiple collision %u, test error %u",
366ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->alignerrors),
367ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->fcserrors),
368ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->single_collision_frames),
369ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->multiple_collision_frames),
370ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->test_errors));
371ee67461eSJoseph Mingrone ND_PRINT("\n\t deferred %u, late collision %u, excessive collision %u, mac trans error %u",
372ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->deferred_transmissions),
373ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->late_collisions),
374ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->excessive_collisions),
375ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->mac_transmit_errors));
376ee67461eSJoseph Mingrone ND_PRINT("\n\t carrier error %u, frames too long %u, mac receive errors %u, symbol errors %u",
377ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->carrier_sense_errors),
378ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->frame_too_longs),
379ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->mac_receive_errors),
380ee67461eSJoseph Mingrone GET_BE_U_4(sflow_eth_counter->symbol_errors));
381cac3dcd5SXin LI
382cac3dcd5SXin LI return 0;
383cac3dcd5SXin LI }
384cac3dcd5SXin LI
385cac3dcd5SXin LI static int
print_sflow_counter_token_ring(netdissect_options * ndo _U_,const u_char * pointer _U_,u_int len _U_)3863c602fabSXin LI print_sflow_counter_token_ring(netdissect_options *ndo _U_,
3878bdc5a62SPatrick Kelsey const u_char *pointer _U_, u_int len _U_)
3888bdc5a62SPatrick Kelsey {
389cac3dcd5SXin LI return 0;
390cac3dcd5SXin LI }
391cac3dcd5SXin LI
392cac3dcd5SXin LI static int
print_sflow_counter_basevg(netdissect_options * ndo,const u_char * pointer,u_int len)3933c602fabSXin LI print_sflow_counter_basevg(netdissect_options *ndo,
3948bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
3958bdc5a62SPatrick Kelsey {
396cac3dcd5SXin LI const struct sflow_100basevg_counter_t *sflow_100basevg_counter;
397cac3dcd5SXin LI
398cac3dcd5SXin LI if (len < sizeof(struct sflow_100basevg_counter_t))
399cac3dcd5SXin LI return 1;
400cac3dcd5SXin LI
401cac3dcd5SXin LI sflow_100basevg_counter = (const struct sflow_100basevg_counter_t *)pointer;
402ee67461eSJoseph Mingrone ND_PRINT("\n\t in high prio frames %u, in high prio octets %" PRIu64,
403ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_highpriority_frames),
404ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->in_highpriority_octets));
405ee67461eSJoseph Mingrone ND_PRINT("\n\t in norm prio frames %u, in norm prio octets %" PRIu64,
406ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_normpriority_frames),
407ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->in_normpriority_octets));
408ee67461eSJoseph Mingrone ND_PRINT("\n\t in ipm errors %u, oversized %u, in data errors %u, null addressed frames %u",
409ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_ipmerrors),
410ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_oversized),
411ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_data_errors),
412ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->in_null_addressed_frames));
413ee67461eSJoseph Mingrone ND_PRINT("\n\t out high prio frames %u, out high prio octets %" PRIu64
414cac3dcd5SXin LI ", trans into frames %u",
415ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->out_highpriority_frames),
416ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->out_highpriority_octets),
417ee67461eSJoseph Mingrone GET_BE_U_4(sflow_100basevg_counter->transitioninto_frames));
418ee67461eSJoseph Mingrone ND_PRINT("\n\t in hc high prio octets %" PRIu64
419cac3dcd5SXin LI ", in hc norm prio octets %" PRIu64
420cac3dcd5SXin LI ", out hc high prio octets %" PRIu64,
421ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->hc_in_highpriority_octets),
422ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->hc_in_normpriority_octets),
423ee67461eSJoseph Mingrone GET_BE_U_8(sflow_100basevg_counter->hc_out_highpriority_octets));
424cac3dcd5SXin LI
425cac3dcd5SXin LI return 0;
426cac3dcd5SXin LI }
427cac3dcd5SXin LI
428cac3dcd5SXin LI static int
print_sflow_counter_vlan(netdissect_options * ndo,const u_char * pointer,u_int len)4293c602fabSXin LI print_sflow_counter_vlan(netdissect_options *ndo,
4308bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
4318bdc5a62SPatrick Kelsey {
432cac3dcd5SXin LI const struct sflow_vlan_counter_t *sflow_vlan_counter;
433cac3dcd5SXin LI
434cac3dcd5SXin LI if (len < sizeof(struct sflow_vlan_counter_t))
435cac3dcd5SXin LI return 1;
436cac3dcd5SXin LI
437cac3dcd5SXin LI sflow_vlan_counter = (const struct sflow_vlan_counter_t *)pointer;
438ee67461eSJoseph Mingrone ND_PRINT("\n\t vlan_id %u, octets %" PRIu64
439cac3dcd5SXin LI ", unicast_pkt %u, multicast_pkt %u, broadcast_pkt %u, discards %u",
440ee67461eSJoseph Mingrone GET_BE_U_4(sflow_vlan_counter->vlan_id),
441ee67461eSJoseph Mingrone GET_BE_U_8(sflow_vlan_counter->octets),
442ee67461eSJoseph Mingrone GET_BE_U_4(sflow_vlan_counter->unicast_pkt),
443ee67461eSJoseph Mingrone GET_BE_U_4(sflow_vlan_counter->multicast_pkt),
444ee67461eSJoseph Mingrone GET_BE_U_4(sflow_vlan_counter->broadcast_pkt),
445ee67461eSJoseph Mingrone GET_BE_U_4(sflow_vlan_counter->discards));
446cac3dcd5SXin LI
447cac3dcd5SXin LI return 0;
448cac3dcd5SXin LI }
449cac3dcd5SXin LI
450cac3dcd5SXin LI struct sflow_processor_counter_t {
451ee67461eSJoseph Mingrone nd_uint32_t five_sec_util;
452ee67461eSJoseph Mingrone nd_uint32_t one_min_util;
453ee67461eSJoseph Mingrone nd_uint32_t five_min_util;
454ee67461eSJoseph Mingrone nd_uint64_t total_memory;
455ee67461eSJoseph Mingrone nd_uint64_t free_memory;
456cac3dcd5SXin LI };
457cac3dcd5SXin LI
458cac3dcd5SXin LI static int
print_sflow_counter_processor(netdissect_options * ndo,const u_char * pointer,u_int len)4593c602fabSXin LI print_sflow_counter_processor(netdissect_options *ndo,
4608bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
4618bdc5a62SPatrick Kelsey {
462cac3dcd5SXin LI const struct sflow_processor_counter_t *sflow_processor_counter;
463cac3dcd5SXin LI
464cac3dcd5SXin LI if (len < sizeof(struct sflow_processor_counter_t))
465cac3dcd5SXin LI return 1;
466cac3dcd5SXin LI
467cac3dcd5SXin LI sflow_processor_counter = (const struct sflow_processor_counter_t *)pointer;
468ee67461eSJoseph Mingrone ND_PRINT("\n\t 5sec %u, 1min %u, 5min %u, total_mem %" PRIu64
469cac3dcd5SXin LI ", total_mem %" PRIu64,
470ee67461eSJoseph Mingrone GET_BE_U_4(sflow_processor_counter->five_sec_util),
471ee67461eSJoseph Mingrone GET_BE_U_4(sflow_processor_counter->one_min_util),
472ee67461eSJoseph Mingrone GET_BE_U_4(sflow_processor_counter->five_min_util),
473ee67461eSJoseph Mingrone GET_BE_U_8(sflow_processor_counter->total_memory),
474ee67461eSJoseph Mingrone GET_BE_U_8(sflow_processor_counter->free_memory));
475cac3dcd5SXin LI
476cac3dcd5SXin LI return 0;
477cac3dcd5SXin LI }
478cac3dcd5SXin LI
479cac3dcd5SXin LI static int
sflow_print_counter_records(netdissect_options * ndo,const u_char * pointer,u_int len,u_int records)4803c602fabSXin LI sflow_print_counter_records(netdissect_options *ndo,
4818bdc5a62SPatrick Kelsey const u_char *pointer, u_int len, u_int records)
4828bdc5a62SPatrick Kelsey {
483cac3dcd5SXin LI u_int nrecords;
484cac3dcd5SXin LI const u_char *tptr;
485cac3dcd5SXin LI u_int tlen;
486cac3dcd5SXin LI u_int counter_type;
487cac3dcd5SXin LI u_int counter_len;
488cac3dcd5SXin LI u_int enterprise;
489cac3dcd5SXin LI const struct sflow_counter_record_t *sflow_counter_record;
490cac3dcd5SXin LI
491cac3dcd5SXin LI nrecords = records;
492cac3dcd5SXin LI tptr = pointer;
493cac3dcd5SXin LI tlen = len;
494cac3dcd5SXin LI
495cac3dcd5SXin LI while (nrecords > 0) {
496cac3dcd5SXin LI /* do we have the "header?" */
497cac3dcd5SXin LI if (tlen < sizeof(struct sflow_counter_record_t))
498cac3dcd5SXin LI return 1;
499cac3dcd5SXin LI sflow_counter_record = (const struct sflow_counter_record_t *)tptr;
500cac3dcd5SXin LI
501ee67461eSJoseph Mingrone enterprise = GET_BE_U_4(sflow_counter_record->format);
502cac3dcd5SXin LI counter_type = enterprise & 0x0FFF;
503cac3dcd5SXin LI enterprise = enterprise >> 20;
504ee67461eSJoseph Mingrone counter_len = GET_BE_U_4(sflow_counter_record->length);
505ee67461eSJoseph Mingrone ND_PRINT("\n\t enterprise %u, %s (%u) length %u",
506cac3dcd5SXin LI enterprise,
507cac3dcd5SXin LI (enterprise == 0) ? tok2str(sflow_counter_type_values,"Unknown",counter_type) : "Unknown",
508cac3dcd5SXin LI counter_type,
509ee67461eSJoseph Mingrone counter_len);
510cac3dcd5SXin LI
511cac3dcd5SXin LI tptr += sizeof(struct sflow_counter_record_t);
512cac3dcd5SXin LI tlen -= sizeof(struct sflow_counter_record_t);
513cac3dcd5SXin LI
514cac3dcd5SXin LI if (tlen < counter_len)
515cac3dcd5SXin LI return 1;
516cac3dcd5SXin LI if (enterprise == 0) {
517cac3dcd5SXin LI switch (counter_type) {
518cac3dcd5SXin LI case SFLOW_COUNTER_GENERIC:
5193c602fabSXin LI if (print_sflow_counter_generic(ndo, tptr, tlen))
520cac3dcd5SXin LI return 1;
521cac3dcd5SXin LI break;
522cac3dcd5SXin LI case SFLOW_COUNTER_ETHERNET:
5233c602fabSXin LI if (print_sflow_counter_ethernet(ndo, tptr, tlen))
524cac3dcd5SXin LI return 1;
525cac3dcd5SXin LI break;
526cac3dcd5SXin LI case SFLOW_COUNTER_TOKEN_RING:
5273c602fabSXin LI if (print_sflow_counter_token_ring(ndo, tptr,tlen))
528cac3dcd5SXin LI return 1;
529cac3dcd5SXin LI break;
530cac3dcd5SXin LI case SFLOW_COUNTER_BASEVG:
5313c602fabSXin LI if (print_sflow_counter_basevg(ndo, tptr, tlen))
532cac3dcd5SXin LI return 1;
533cac3dcd5SXin LI break;
534cac3dcd5SXin LI case SFLOW_COUNTER_VLAN:
5353c602fabSXin LI if (print_sflow_counter_vlan(ndo, tptr, tlen))
536cac3dcd5SXin LI return 1;
537cac3dcd5SXin LI break;
538cac3dcd5SXin LI case SFLOW_COUNTER_PROCESSOR:
5393c602fabSXin LI if (print_sflow_counter_processor(ndo, tptr, tlen))
540cac3dcd5SXin LI return 1;
541cac3dcd5SXin LI break;
542cac3dcd5SXin LI default:
5433c602fabSXin LI if (ndo->ndo_vflag <= 1)
5443c602fabSXin LI print_unknown_data(ndo, tptr, "\n\t\t", counter_len);
545cac3dcd5SXin LI break;
546cac3dcd5SXin LI }
547cac3dcd5SXin LI }
548cac3dcd5SXin LI tptr += counter_len;
549cac3dcd5SXin LI tlen -= counter_len;
550cac3dcd5SXin LI nrecords--;
551cac3dcd5SXin LI
552cac3dcd5SXin LI }
553cac3dcd5SXin LI
554cac3dcd5SXin LI return 0;
555cac3dcd5SXin LI }
556cac3dcd5SXin LI
557cac3dcd5SXin LI static int
sflow_print_counter_sample(netdissect_options * ndo,const u_char * pointer,u_int len)5583c602fabSXin LI sflow_print_counter_sample(netdissect_options *ndo,
5598bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
5608bdc5a62SPatrick Kelsey {
561cac3dcd5SXin LI const struct sflow_counter_sample_t *sflow_counter_sample;
562cac3dcd5SXin LI u_int nrecords;
563cac3dcd5SXin LI
564cac3dcd5SXin LI if (len < sizeof(struct sflow_counter_sample_t))
565cac3dcd5SXin LI return 1;
566cac3dcd5SXin LI
567cac3dcd5SXin LI sflow_counter_sample = (const struct sflow_counter_sample_t *)pointer;
568cac3dcd5SXin LI
569ee67461eSJoseph Mingrone nrecords = GET_BE_U_4(sflow_counter_sample->records);
570cac3dcd5SXin LI
571ee67461eSJoseph Mingrone ND_PRINT(" seqnum %u, type %u, idx %u, records %u",
572ee67461eSJoseph Mingrone GET_BE_U_4(sflow_counter_sample->seqnum),
573ee67461eSJoseph Mingrone GET_U_1(sflow_counter_sample->type),
574ee67461eSJoseph Mingrone GET_BE_U_3(sflow_counter_sample->index),
575ee67461eSJoseph Mingrone nrecords);
576cac3dcd5SXin LI
5773c602fabSXin LI return sflow_print_counter_records(ndo, pointer + sizeof(struct sflow_counter_sample_t),
578cac3dcd5SXin LI len - sizeof(struct sflow_counter_sample_t),
579cac3dcd5SXin LI nrecords);
580cac3dcd5SXin LI }
581cac3dcd5SXin LI
582cac3dcd5SXin LI static int
sflow_print_expanded_counter_sample(netdissect_options * ndo,const u_char * pointer,u_int len)5833c602fabSXin LI sflow_print_expanded_counter_sample(netdissect_options *ndo,
5848bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
5858bdc5a62SPatrick Kelsey {
586cac3dcd5SXin LI const struct sflow_expanded_counter_sample_t *sflow_expanded_counter_sample;
587cac3dcd5SXin LI u_int nrecords;
588cac3dcd5SXin LI
589cac3dcd5SXin LI
590cac3dcd5SXin LI if (len < sizeof(struct sflow_expanded_counter_sample_t))
591cac3dcd5SXin LI return 1;
592cac3dcd5SXin LI
593cac3dcd5SXin LI sflow_expanded_counter_sample = (const struct sflow_expanded_counter_sample_t *)pointer;
594cac3dcd5SXin LI
595ee67461eSJoseph Mingrone nrecords = GET_BE_U_4(sflow_expanded_counter_sample->records);
596cac3dcd5SXin LI
597ee67461eSJoseph Mingrone ND_PRINT(" seqnum %u, type %u, idx %u, records %u",
598ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_counter_sample->seqnum),
599ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_counter_sample->type),
600ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_counter_sample->index),
601ee67461eSJoseph Mingrone nrecords);
602cac3dcd5SXin LI
6033c602fabSXin LI return sflow_print_counter_records(ndo, pointer + sizeof(struct sflow_expanded_counter_sample_t),
604cac3dcd5SXin LI len - sizeof(struct sflow_expanded_counter_sample_t),
605cac3dcd5SXin LI nrecords);
606cac3dcd5SXin LI }
607cac3dcd5SXin LI
608cac3dcd5SXin LI static int
print_sflow_raw_packet(netdissect_options * ndo,const u_char * pointer,u_int len)6093c602fabSXin LI print_sflow_raw_packet(netdissect_options *ndo,
6108bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
6118bdc5a62SPatrick Kelsey {
612cac3dcd5SXin LI const struct sflow_expanded_flow_raw_t *sflow_flow_raw;
613cac3dcd5SXin LI
614cac3dcd5SXin LI if (len < sizeof(struct sflow_expanded_flow_raw_t))
615cac3dcd5SXin LI return 1;
616cac3dcd5SXin LI
617cac3dcd5SXin LI sflow_flow_raw = (const struct sflow_expanded_flow_raw_t *)pointer;
618ee67461eSJoseph Mingrone ND_PRINT("\n\t protocol %s (%u), length %u, stripped bytes %u, header_size %u",
619ee67461eSJoseph Mingrone tok2str(sflow_flow_raw_protocol_values,"Unknown",GET_BE_U_4(sflow_flow_raw->protocol)),
620ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_raw->protocol),
621ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_raw->length),
622ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_raw->stripped_bytes),
623ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_raw->header_size));
624cac3dcd5SXin LI
625cac3dcd5SXin LI /* QUESTION - should we attempt to print the raw header itself?
626ee67461eSJoseph Mingrone assuming of course there is enough data present to do so... */
627cac3dcd5SXin LI
628cac3dcd5SXin LI return 0;
629cac3dcd5SXin LI }
630cac3dcd5SXin LI
631cac3dcd5SXin LI static int
print_sflow_ethernet_frame(netdissect_options * ndo,const u_char * pointer,u_int len)6323c602fabSXin LI print_sflow_ethernet_frame(netdissect_options *ndo,
6338bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
6348bdc5a62SPatrick Kelsey {
635cac3dcd5SXin LI const struct sflow_ethernet_frame_t *sflow_ethernet_frame;
636cac3dcd5SXin LI
637cac3dcd5SXin LI if (len < sizeof(struct sflow_ethernet_frame_t))
638cac3dcd5SXin LI return 1;
639cac3dcd5SXin LI
640cac3dcd5SXin LI sflow_ethernet_frame = (const struct sflow_ethernet_frame_t *)pointer;
641cac3dcd5SXin LI
642ee67461eSJoseph Mingrone ND_PRINT("\n\t frame len %u, type %u",
643ee67461eSJoseph Mingrone GET_BE_U_4(sflow_ethernet_frame->length),
644ee67461eSJoseph Mingrone GET_BE_U_4(sflow_ethernet_frame->type));
645cac3dcd5SXin LI
646cac3dcd5SXin LI return 0;
647cac3dcd5SXin LI }
648cac3dcd5SXin LI
649cac3dcd5SXin LI static int
print_sflow_extended_switch_data(netdissect_options * ndo,const u_char * pointer,u_int len)6503c602fabSXin LI print_sflow_extended_switch_data(netdissect_options *ndo,
6518bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
6528bdc5a62SPatrick Kelsey {
653cac3dcd5SXin LI const struct sflow_extended_switch_data_t *sflow_extended_sw_data;
654cac3dcd5SXin LI
655cac3dcd5SXin LI if (len < sizeof(struct sflow_extended_switch_data_t))
656cac3dcd5SXin LI return 1;
657cac3dcd5SXin LI
658cac3dcd5SXin LI sflow_extended_sw_data = (const struct sflow_extended_switch_data_t *)pointer;
659ee67461eSJoseph Mingrone ND_PRINT("\n\t src vlan %u, src pri %u, dst vlan %u, dst pri %u",
660ee67461eSJoseph Mingrone GET_BE_U_4(sflow_extended_sw_data->src_vlan),
661ee67461eSJoseph Mingrone GET_BE_U_4(sflow_extended_sw_data->src_pri),
662ee67461eSJoseph Mingrone GET_BE_U_4(sflow_extended_sw_data->dst_vlan),
663ee67461eSJoseph Mingrone GET_BE_U_4(sflow_extended_sw_data->dst_pri));
664cac3dcd5SXin LI
665cac3dcd5SXin LI return 0;
666cac3dcd5SXin LI }
667cac3dcd5SXin LI
668cac3dcd5SXin LI static int
sflow_print_flow_records(netdissect_options * ndo,const u_char * pointer,u_int len,u_int records)6693c602fabSXin LI sflow_print_flow_records(netdissect_options *ndo,
6708bdc5a62SPatrick Kelsey const u_char *pointer, u_int len, u_int records)
6718bdc5a62SPatrick Kelsey {
672cac3dcd5SXin LI u_int nrecords;
673cac3dcd5SXin LI const u_char *tptr;
674cac3dcd5SXin LI u_int tlen;
675cac3dcd5SXin LI u_int flow_type;
676cac3dcd5SXin LI u_int enterprise;
677cac3dcd5SXin LI u_int flow_len;
678cac3dcd5SXin LI const struct sflow_flow_record_t *sflow_flow_record;
679cac3dcd5SXin LI
680cac3dcd5SXin LI nrecords = records;
681cac3dcd5SXin LI tptr = pointer;
682cac3dcd5SXin LI tlen = len;
683cac3dcd5SXin LI
684cac3dcd5SXin LI while (nrecords > 0) {
685cac3dcd5SXin LI /* do we have the "header?" */
686cac3dcd5SXin LI if (tlen < sizeof(struct sflow_flow_record_t))
687cac3dcd5SXin LI return 1;
688cac3dcd5SXin LI
689cac3dcd5SXin LI sflow_flow_record = (const struct sflow_flow_record_t *)tptr;
690cac3dcd5SXin LI
691*0a7e5f1fSJoseph Mingrone /* so, the funky encoding means we cannot blithely mask-off
692cac3dcd5SXin LI bits, we must also check the enterprise. */
693cac3dcd5SXin LI
694ee67461eSJoseph Mingrone enterprise = GET_BE_U_4(sflow_flow_record->format);
695cac3dcd5SXin LI flow_type = enterprise & 0x0FFF;
696cac3dcd5SXin LI enterprise = enterprise >> 12;
697ee67461eSJoseph Mingrone flow_len = GET_BE_U_4(sflow_flow_record->length);
698ee67461eSJoseph Mingrone ND_PRINT("\n\t enterprise %u %s (%u) length %u",
699cac3dcd5SXin LI enterprise,
700cac3dcd5SXin LI (enterprise == 0) ? tok2str(sflow_flow_type_values,"Unknown",flow_type) : "Unknown",
701cac3dcd5SXin LI flow_type,
702ee67461eSJoseph Mingrone flow_len);
703cac3dcd5SXin LI
704cac3dcd5SXin LI tptr += sizeof(struct sflow_flow_record_t);
705cac3dcd5SXin LI tlen -= sizeof(struct sflow_flow_record_t);
706cac3dcd5SXin LI
707cac3dcd5SXin LI if (tlen < flow_len)
708cac3dcd5SXin LI return 1;
709cac3dcd5SXin LI
710cac3dcd5SXin LI if (enterprise == 0) {
711cac3dcd5SXin LI switch (flow_type) {
712cac3dcd5SXin LI case SFLOW_FLOW_RAW_PACKET:
7133c602fabSXin LI if (print_sflow_raw_packet(ndo, tptr, tlen))
714cac3dcd5SXin LI return 1;
715cac3dcd5SXin LI break;
716cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_SWITCH_DATA:
7173c602fabSXin LI if (print_sflow_extended_switch_data(ndo, tptr, tlen))
718cac3dcd5SXin LI return 1;
719cac3dcd5SXin LI break;
720cac3dcd5SXin LI case SFLOW_FLOW_ETHERNET_FRAME:
7213c602fabSXin LI if (print_sflow_ethernet_frame(ndo, tptr, tlen))
722cac3dcd5SXin LI return 1;
723cac3dcd5SXin LI break;
724cac3dcd5SXin LI /* FIXME these need a decoder */
725cac3dcd5SXin LI case SFLOW_FLOW_IPV4_DATA:
726cac3dcd5SXin LI case SFLOW_FLOW_IPV6_DATA:
727cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_ROUTER_DATA:
728cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_GATEWAY_DATA:
729cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_USER_DATA:
730cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_URL_DATA:
731cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_MPLS_DATA:
732cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_NAT_DATA:
733cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_MPLS_TUNNEL:
734cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_MPLS_VC:
735cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_MPLS_FEC:
736cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC:
737cac3dcd5SXin LI case SFLOW_FLOW_EXTENDED_VLAN_TUNNEL:
738cac3dcd5SXin LI break;
739cac3dcd5SXin LI default:
7403c602fabSXin LI if (ndo->ndo_vflag <= 1)
7413c602fabSXin LI print_unknown_data(ndo, tptr, "\n\t\t", flow_len);
742cac3dcd5SXin LI break;
743cac3dcd5SXin LI }
744cac3dcd5SXin LI }
745cac3dcd5SXin LI tptr += flow_len;
746cac3dcd5SXin LI tlen -= flow_len;
747cac3dcd5SXin LI nrecords--;
748cac3dcd5SXin LI
749cac3dcd5SXin LI }
750cac3dcd5SXin LI
751cac3dcd5SXin LI return 0;
752cac3dcd5SXin LI }
753cac3dcd5SXin LI
754cac3dcd5SXin LI static int
sflow_print_flow_sample(netdissect_options * ndo,const u_char * pointer,u_int len)7553c602fabSXin LI sflow_print_flow_sample(netdissect_options *ndo,
7568bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
7578bdc5a62SPatrick Kelsey {
758cac3dcd5SXin LI const struct sflow_flow_sample_t *sflow_flow_sample;
759cac3dcd5SXin LI u_int nrecords;
760cac3dcd5SXin LI
761cac3dcd5SXin LI if (len < sizeof(struct sflow_flow_sample_t))
762cac3dcd5SXin LI return 1;
763cac3dcd5SXin LI
7643340d773SGleb Smirnoff sflow_flow_sample = (const struct sflow_flow_sample_t *)pointer;
765cac3dcd5SXin LI
766ee67461eSJoseph Mingrone nrecords = GET_BE_U_4(sflow_flow_sample->records);
767cac3dcd5SXin LI
768ee67461eSJoseph Mingrone ND_PRINT(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, input %u output %u records %u",
769ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->seqnum),
770ee67461eSJoseph Mingrone GET_U_1(sflow_flow_sample->type),
771ee67461eSJoseph Mingrone GET_BE_U_3(sflow_flow_sample->index),
772ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->rate),
773ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->pool),
774ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->drops),
775ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->in_interface),
776ee67461eSJoseph Mingrone GET_BE_U_4(sflow_flow_sample->out_interface),
777ee67461eSJoseph Mingrone nrecords);
778cac3dcd5SXin LI
7793c602fabSXin LI return sflow_print_flow_records(ndo, pointer + sizeof(struct sflow_flow_sample_t),
780cac3dcd5SXin LI len - sizeof(struct sflow_flow_sample_t),
781cac3dcd5SXin LI nrecords);
782cac3dcd5SXin LI }
783cac3dcd5SXin LI
784cac3dcd5SXin LI static int
sflow_print_expanded_flow_sample(netdissect_options * ndo,const u_char * pointer,u_int len)7853c602fabSXin LI sflow_print_expanded_flow_sample(netdissect_options *ndo,
7868bdc5a62SPatrick Kelsey const u_char *pointer, u_int len)
7878bdc5a62SPatrick Kelsey {
788cac3dcd5SXin LI const struct sflow_expanded_flow_sample_t *sflow_expanded_flow_sample;
789cac3dcd5SXin LI u_int nrecords;
790cac3dcd5SXin LI
791cac3dcd5SXin LI if (len < sizeof(struct sflow_expanded_flow_sample_t))
792cac3dcd5SXin LI return 1;
793cac3dcd5SXin LI
794cac3dcd5SXin LI sflow_expanded_flow_sample = (const struct sflow_expanded_flow_sample_t *)pointer;
795cac3dcd5SXin LI
796ee67461eSJoseph Mingrone nrecords = GET_BE_U_4(sflow_expanded_flow_sample->records);
797cac3dcd5SXin LI
798ee67461eSJoseph Mingrone ND_PRINT(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, records %u",
799ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->seqnum),
800ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->type),
801ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->index),
802ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->rate),
803ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->pool),
804ee67461eSJoseph Mingrone GET_BE_U_4(sflow_expanded_flow_sample->drops),
805ee67461eSJoseph Mingrone nrecords);
806cac3dcd5SXin LI
8073c602fabSXin LI return sflow_print_flow_records(ndo, pointer + sizeof(struct sflow_expanded_flow_sample_t),
808cac3dcd5SXin LI len - sizeof(struct sflow_expanded_flow_sample_t),
809cac3dcd5SXin LI nrecords);
810cac3dcd5SXin LI }
811cac3dcd5SXin LI
812a5779b6eSRui Paulo void
sflow_print(netdissect_options * ndo,const u_char * pptr,u_int len)8133c602fabSXin LI sflow_print(netdissect_options *ndo,
8148bdc5a62SPatrick Kelsey const u_char *pptr, u_int len)
8158bdc5a62SPatrick Kelsey {
816a5779b6eSRui Paulo const struct sflow_datagram_t *sflow_datagram;
817ee67461eSJoseph Mingrone const struct sflow_v6_datagram_t *sflow_v6_datagram;
818a5779b6eSRui Paulo const struct sflow_sample_header *sflow_sample;
819cac3dcd5SXin LI
820a5779b6eSRui Paulo const u_char *tptr;
821cac3dcd5SXin LI u_int tlen;
8223c602fabSXin LI uint32_t sflow_sample_type, sflow_sample_len;
8233c602fabSXin LI uint32_t nsamples;
824ee67461eSJoseph Mingrone uint32_t ip_version;
825cac3dcd5SXin LI
826ee67461eSJoseph Mingrone ndo->ndo_protocol = "sflow";
827a5779b6eSRui Paulo tptr = pptr;
828a5779b6eSRui Paulo tlen = len;
829a5779b6eSRui Paulo sflow_datagram = (const struct sflow_datagram_t *)pptr;
830ee67461eSJoseph Mingrone sflow_v6_datagram = (const struct sflow_v6_datagram_t *)pptr;
831ee67461eSJoseph Mingrone ip_version = GET_BE_U_4(sflow_datagram->ip_version);
832ee67461eSJoseph Mingrone
833ee67461eSJoseph Mingrone if ((len < sizeof(struct sflow_datagram_t) && (ip_version == 1)) ||
834ee67461eSJoseph Mingrone (len < sizeof(struct sflow_v6_datagram_t) && (ip_version == 2))) {
835ee67461eSJoseph Mingrone ND_PRINT("sFlowv%u", GET_BE_U_4(sflow_datagram->version));
836ee67461eSJoseph Mingrone ND_PRINT(" [length %u < %zu]", len, sizeof(struct sflow_datagram_t));
837ee67461eSJoseph Mingrone nd_print_invalid(ndo);
83839e421e8SCy Schubert return;
83939e421e8SCy Schubert }
840ee67461eSJoseph Mingrone ND_TCHECK_SIZE(sflow_datagram);
841a5779b6eSRui Paulo
842a5779b6eSRui Paulo /*
843a5779b6eSRui Paulo * Sanity checking of the header.
844a5779b6eSRui Paulo */
845ee67461eSJoseph Mingrone if (GET_BE_U_4(sflow_datagram->version) != 5) {
846ee67461eSJoseph Mingrone ND_PRINT("sFlow version %u packet not supported",
847ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->version));
848a5779b6eSRui Paulo return;
849a5779b6eSRui Paulo }
850a5779b6eSRui Paulo
8513c602fabSXin LI if (ndo->ndo_vflag < 1) {
852ee67461eSJoseph Mingrone ND_PRINT("sFlowv%u, %s agent %s, agent-id %u, length %u",
853ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->version),
854ee67461eSJoseph Mingrone ip_version == 1 ? "IPv4" : "IPv6",
855ee67461eSJoseph Mingrone ip_version == 1 ? GET_IPADDR_STRING(sflow_datagram->agent) :
856ee67461eSJoseph Mingrone GET_IP6ADDR_STRING( sflow_v6_datagram->agent),
857ee67461eSJoseph Mingrone ip_version == 1 ? GET_BE_U_4(sflow_datagram->agent_id) :
858ee67461eSJoseph Mingrone GET_BE_U_4(sflow_v6_datagram->agent_id),
859ee67461eSJoseph Mingrone len);
860a5779b6eSRui Paulo return;
861a5779b6eSRui Paulo }
862a5779b6eSRui Paulo
863a5779b6eSRui Paulo /* ok they seem to want to know everything - lets fully decode it */
864ee67461eSJoseph Mingrone if (ip_version == 1) {
865ee67461eSJoseph Mingrone nsamples=GET_BE_U_4(sflow_datagram->samples);
866ee67461eSJoseph Mingrone ND_PRINT("sFlowv%u, %s agent %s, agent-id %u, seqnum %u, uptime %u, samples %u, length %u",
867ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->version),
868ee67461eSJoseph Mingrone "IPv4",
869ee67461eSJoseph Mingrone GET_IPADDR_STRING(sflow_datagram->agent),
870ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->agent_id),
871ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->seqnum),
872ee67461eSJoseph Mingrone GET_BE_U_4(sflow_datagram->uptime),
873a5779b6eSRui Paulo nsamples,
874ee67461eSJoseph Mingrone len);
875a5779b6eSRui Paulo
876a5779b6eSRui Paulo /* skip Common header */
877*0a7e5f1fSJoseph Mingrone ND_ICHECK_ZU(tlen, <, sizeof(struct sflow_datagram_t));
878ee67461eSJoseph Mingrone tptr += sizeof(struct sflow_datagram_t);
879ee67461eSJoseph Mingrone tlen -= sizeof(struct sflow_datagram_t);
880ee67461eSJoseph Mingrone } else {
881ee67461eSJoseph Mingrone nsamples=GET_BE_U_4(sflow_v6_datagram->samples);
882ee67461eSJoseph Mingrone ND_PRINT("sFlowv%u, %s agent %s, agent-id %u, seqnum %u, uptime %u, samples %u, length %u",
883ee67461eSJoseph Mingrone GET_BE_U_4(sflow_v6_datagram->version),
884ee67461eSJoseph Mingrone "IPv6",
885ee67461eSJoseph Mingrone GET_IP6ADDR_STRING(sflow_v6_datagram->agent),
886ee67461eSJoseph Mingrone GET_BE_U_4(sflow_v6_datagram->agent_id),
887ee67461eSJoseph Mingrone GET_BE_U_4(sflow_v6_datagram->seqnum),
888ee67461eSJoseph Mingrone GET_BE_U_4(sflow_v6_datagram->uptime),
889ee67461eSJoseph Mingrone nsamples,
890ee67461eSJoseph Mingrone len);
89139e421e8SCy Schubert
892ee67461eSJoseph Mingrone /* skip Common header */
893*0a7e5f1fSJoseph Mingrone ND_ICHECK_ZU(tlen, <, sizeof(struct sflow_v6_datagram_t));
894ee67461eSJoseph Mingrone tptr += sizeof(struct sflow_v6_datagram_t);
895ee67461eSJoseph Mingrone tlen -= sizeof(struct sflow_v6_datagram_t);
896ee67461eSJoseph Mingrone }
897a5779b6eSRui Paulo while (nsamples > 0 && tlen > 0) {
898a5779b6eSRui Paulo sflow_sample = (const struct sflow_sample_header *)tptr;
899cac3dcd5SXin LI
900ee67461eSJoseph Mingrone sflow_sample_type = (GET_BE_U_4(sflow_sample->format)&0x0FFF);
901ee67461eSJoseph Mingrone sflow_sample_len = GET_BE_U_4(sflow_sample->len);
902a5779b6eSRui Paulo
903cac3dcd5SXin LI if (tlen < sizeof(struct sflow_sample_header))
904ee67461eSJoseph Mingrone goto invalid;
905cac3dcd5SXin LI
906a5779b6eSRui Paulo tptr += sizeof(struct sflow_sample_header);
907a5779b6eSRui Paulo tlen -= sizeof(struct sflow_sample_header);
908a5779b6eSRui Paulo
909ee67461eSJoseph Mingrone ND_PRINT("\n\t%s (%u), length %u,",
910a5779b6eSRui Paulo tok2str(sflow_format_values, "Unknown", sflow_sample_type),
911a5779b6eSRui Paulo sflow_sample_type,
912ee67461eSJoseph Mingrone sflow_sample_len);
913a5779b6eSRui Paulo
914a5779b6eSRui Paulo /* basic sanity check */
915a5779b6eSRui Paulo if (sflow_sample_type == 0 || sflow_sample_len ==0) {
916a5779b6eSRui Paulo return;
917a5779b6eSRui Paulo }
918a5779b6eSRui Paulo
919cac3dcd5SXin LI if (tlen < sflow_sample_len)
920ee67461eSJoseph Mingrone goto invalid;
921a5779b6eSRui Paulo
922cac3dcd5SXin LI /* did we capture enough for fully decoding the sample ? */
923ee67461eSJoseph Mingrone ND_TCHECK_LEN(tptr, sflow_sample_len);
924cac3dcd5SXin LI
925a5779b6eSRui Paulo switch(sflow_sample_type) {
926cac3dcd5SXin LI case SFLOW_FLOW_SAMPLE:
9273c602fabSXin LI if (sflow_print_flow_sample(ndo, tptr, tlen))
928ee67461eSJoseph Mingrone goto invalid;
929a5779b6eSRui Paulo break;
930a5779b6eSRui Paulo
931cac3dcd5SXin LI case SFLOW_COUNTER_SAMPLE:
9323c602fabSXin LI if (sflow_print_counter_sample(ndo, tptr,tlen))
933ee67461eSJoseph Mingrone goto invalid;
934a5779b6eSRui Paulo break;
935a5779b6eSRui Paulo
936a5779b6eSRui Paulo case SFLOW_EXPANDED_FLOW_SAMPLE:
9373c602fabSXin LI if (sflow_print_expanded_flow_sample(ndo, tptr, tlen))
938ee67461eSJoseph Mingrone goto invalid;
939a5779b6eSRui Paulo break;
940a5779b6eSRui Paulo
941a5779b6eSRui Paulo case SFLOW_EXPANDED_COUNTER_SAMPLE:
9423c602fabSXin LI if (sflow_print_expanded_counter_sample(ndo, tptr,tlen))
943ee67461eSJoseph Mingrone goto invalid;
944cac3dcd5SXin LI break;
945a5779b6eSRui Paulo
946a5779b6eSRui Paulo default:
9473c602fabSXin LI if (ndo->ndo_vflag <= 1)
9483c602fabSXin LI print_unknown_data(ndo, tptr, "\n\t ", sflow_sample_len);
949a5779b6eSRui Paulo break;
950a5779b6eSRui Paulo }
951a5779b6eSRui Paulo tptr += sflow_sample_len;
952a5779b6eSRui Paulo tlen -= sflow_sample_len;
953a5779b6eSRui Paulo nsamples--;
954a5779b6eSRui Paulo }
955a5779b6eSRui Paulo return;
956a5779b6eSRui Paulo
957ee67461eSJoseph Mingrone invalid:
958ee67461eSJoseph Mingrone nd_print_invalid(ndo);
959ee67461eSJoseph Mingrone ND_TCHECK_LEN(tptr, tlen);
960a5779b6eSRui Paulo }
961