blob: 51325e28226054caa594250f19ee8a1906d869a2 [file] [log] [blame]
JP Abgrall53f17a92014-02-12 14:02:41 -08001/*
2 * Copyright (c) 1998-2007 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
JP Abgrall53f17a92014-02-12 14:02:41 -080015 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16 *
17 * Expansion and refactoring by Rick Jones <rick.jones2@hp.com>
18 */
19
Elliott Hughese2e3bd12017-05-15 10:59:29 -070020/* \summary: sFlow protocol printer */
21
Elliott Hughes820eced2021-08-20 18:00:50 -070022/* specification: https://sflow.org/developers/specifications.php */
Elliott Hughese2e3bd12017-05-15 10:59:29 -070023
JP Abgrall53f17a92014-02-12 14:02:41 -080024#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070025#include <config.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080026#endif
27
Elliott Hughes820eced2021-08-20 18:00:50 -070028#include "netdissect-stdinc.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080029
Elliott Hughes820eced2021-08-20 18:00:50 -070030#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070031#include "netdissect.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080032#include "extract.h"
33#include "addrtoname.h"
34
Elliott Hughes892a68b2015-10-19 14:43:53 -070035/*
JP Abgrall53f17a92014-02-12 14:02:41 -080036 * sFlow datagram
Elliott Hughes892a68b2015-10-19 14:43:53 -070037 *
JP Abgrall53f17a92014-02-12 14:02:41 -080038 * 0 1 2 3
39 * 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
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Sflow version (2,4,5) |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | IP version (1 for IPv4 | 2 for IPv6) |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | IP Address AGENT (4 or 16 bytes) |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Sub agent ID |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | Datagram sequence number |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Switch uptime in ms |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | num samples in datagram |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Elliott Hughes892a68b2015-10-19 14:43:53 -070055 *
JP Abgrall53f17a92014-02-12 14:02:41 -080056 */
57
58struct sflow_datagram_t {
Elliott Hughes820eced2021-08-20 18:00:50 -070059 nd_uint32_t version;
60 nd_uint32_t ip_version;
61 nd_ipv4 agent;
62 nd_uint32_t agent_id;
63 nd_uint32_t seqnum;
64 nd_uint32_t uptime;
65 nd_uint32_t samples;
JP Abgrall53f17a92014-02-12 14:02:41 -080066};
67
68struct sflow_sample_header {
Elliott Hughes820eced2021-08-20 18:00:50 -070069 nd_uint32_t format;
70 nd_uint32_t len;
JP Abgrall53f17a92014-02-12 14:02:41 -080071};
72
73#define SFLOW_FLOW_SAMPLE 1
74#define SFLOW_COUNTER_SAMPLE 2
75#define SFLOW_EXPANDED_FLOW_SAMPLE 3
76#define SFLOW_EXPANDED_COUNTER_SAMPLE 4
77
78static const struct tok sflow_format_values[] = {
79 { SFLOW_FLOW_SAMPLE, "flow sample" },
80 { SFLOW_COUNTER_SAMPLE, "counter sample" },
81 { SFLOW_EXPANDED_FLOW_SAMPLE, "expanded flow sample" },
82 { SFLOW_EXPANDED_COUNTER_SAMPLE, "expanded counter sample" },
83 { 0, NULL}
84};
85
86struct sflow_flow_sample_t {
Elliott Hughes820eced2021-08-20 18:00:50 -070087 nd_uint32_t seqnum;
88 nd_uint8_t type;
89 nd_uint24_t index;
90 nd_uint32_t rate;
91 nd_uint32_t pool;
92 nd_uint32_t drops;
93 nd_uint32_t in_interface;
94 nd_uint32_t out_interface;
95 nd_uint32_t records;
JP Abgrall53f17a92014-02-12 14:02:41 -080096
97};
98
99struct sflow_expanded_flow_sample_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700100 nd_uint32_t seqnum;
101 nd_uint32_t type;
102 nd_uint32_t index;
103 nd_uint32_t rate;
104 nd_uint32_t pool;
105 nd_uint32_t drops;
106 nd_uint32_t in_interface_format;
107 nd_uint32_t in_interface_value;
108 nd_uint32_t out_interface_format;
109 nd_uint32_t out_interface_value;
110 nd_uint32_t records;
JP Abgrall53f17a92014-02-12 14:02:41 -0800111};
112
113#define SFLOW_FLOW_RAW_PACKET 1
114#define SFLOW_FLOW_ETHERNET_FRAME 2
115#define SFLOW_FLOW_IPV4_DATA 3
116#define SFLOW_FLOW_IPV6_DATA 4
117#define SFLOW_FLOW_EXTENDED_SWITCH_DATA 1001
118#define SFLOW_FLOW_EXTENDED_ROUTER_DATA 1002
119#define SFLOW_FLOW_EXTENDED_GATEWAY_DATA 1003
120#define SFLOW_FLOW_EXTENDED_USER_DATA 1004
121#define SFLOW_FLOW_EXTENDED_URL_DATA 1005
122#define SFLOW_FLOW_EXTENDED_MPLS_DATA 1006
123#define SFLOW_FLOW_EXTENDED_NAT_DATA 1007
124#define SFLOW_FLOW_EXTENDED_MPLS_TUNNEL 1008
125#define SFLOW_FLOW_EXTENDED_MPLS_VC 1009
126#define SFLOW_FLOW_EXTENDED_MPLS_FEC 1010
127#define SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC 1011
128#define SFLOW_FLOW_EXTENDED_VLAN_TUNNEL 1012
129
130static const struct tok sflow_flow_type_values[] = {
131 { SFLOW_FLOW_RAW_PACKET, "Raw packet"},
132 { SFLOW_FLOW_ETHERNET_FRAME, "Ethernet frame"},
133 { SFLOW_FLOW_IPV4_DATA, "IPv4 Data"},
134 { SFLOW_FLOW_IPV6_DATA, "IPv6 Data"},
135 { SFLOW_FLOW_EXTENDED_SWITCH_DATA, "Extended Switch data"},
136 { SFLOW_FLOW_EXTENDED_ROUTER_DATA, "Extended Router data"},
137 { SFLOW_FLOW_EXTENDED_GATEWAY_DATA, "Extended Gateway data"},
138 { SFLOW_FLOW_EXTENDED_USER_DATA, "Extended User data"},
139 { SFLOW_FLOW_EXTENDED_URL_DATA, "Extended URL data"},
140 { SFLOW_FLOW_EXTENDED_MPLS_DATA, "Extended MPLS data"},
141 { SFLOW_FLOW_EXTENDED_NAT_DATA, "Extended NAT data"},
142 { SFLOW_FLOW_EXTENDED_MPLS_TUNNEL, "Extended MPLS tunnel"},
143 { SFLOW_FLOW_EXTENDED_MPLS_VC, "Extended MPLS VC"},
144 { SFLOW_FLOW_EXTENDED_MPLS_FEC, "Extended MPLS FEC"},
145 { SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC, "Extended MPLS LVP FEC"},
146 { SFLOW_FLOW_EXTENDED_VLAN_TUNNEL, "Extended VLAN Tunnel"},
147 { 0, NULL}
148};
149
150#define SFLOW_HEADER_PROTOCOL_ETHERNET 1
151#define SFLOW_HEADER_PROTOCOL_IPV4 11
152#define SFLOW_HEADER_PROTOCOL_IPV6 12
153
154static const struct tok sflow_flow_raw_protocol_values[] = {
155 { SFLOW_HEADER_PROTOCOL_ETHERNET, "Ethernet"},
156 { SFLOW_HEADER_PROTOCOL_IPV4, "IPv4"},
157 { SFLOW_HEADER_PROTOCOL_IPV6, "IPv6"},
158 { 0, NULL}
159};
Elliott Hughes892a68b2015-10-19 14:43:53 -0700160
JP Abgrall53f17a92014-02-12 14:02:41 -0800161struct sflow_expanded_flow_raw_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700162 nd_uint32_t protocol;
163 nd_uint32_t length;
164 nd_uint32_t stripped_bytes;
165 nd_uint32_t header_size;
JP Abgrall53f17a92014-02-12 14:02:41 -0800166};
167
168struct sflow_ethernet_frame_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700169 nd_uint32_t length;
170 nd_byte src_mac[8];
171 nd_byte dst_mac[8];
172 nd_uint32_t type;
JP Abgrall53f17a92014-02-12 14:02:41 -0800173};
174
175struct sflow_extended_switch_data_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700176 nd_uint32_t src_vlan;
177 nd_uint32_t src_pri;
178 nd_uint32_t dst_vlan;
179 nd_uint32_t dst_pri;
JP Abgrall53f17a92014-02-12 14:02:41 -0800180};
181
182struct sflow_counter_record_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700183 nd_uint32_t format;
184 nd_uint32_t length;
JP Abgrall53f17a92014-02-12 14:02:41 -0800185};
186
187struct sflow_flow_record_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700188 nd_uint32_t format;
189 nd_uint32_t length;
JP Abgrall53f17a92014-02-12 14:02:41 -0800190};
191
192struct sflow_counter_sample_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700193 nd_uint32_t seqnum;
194 nd_uint8_t type;
195 nd_uint24_t index;
196 nd_uint32_t records;
JP Abgrall53f17a92014-02-12 14:02:41 -0800197};
198
199struct sflow_expanded_counter_sample_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700200 nd_uint32_t seqnum;
201 nd_uint32_t type;
202 nd_uint32_t index;
203 nd_uint32_t records;
JP Abgrall53f17a92014-02-12 14:02:41 -0800204};
205
206#define SFLOW_COUNTER_GENERIC 1
207#define SFLOW_COUNTER_ETHERNET 2
208#define SFLOW_COUNTER_TOKEN_RING 3
209#define SFLOW_COUNTER_BASEVG 4
210#define SFLOW_COUNTER_VLAN 5
211#define SFLOW_COUNTER_PROCESSOR 1001
212
213static const struct tok sflow_counter_type_values[] = {
214 { SFLOW_COUNTER_GENERIC, "Generic counter"},
215 { SFLOW_COUNTER_ETHERNET, "Ethernet counter"},
216 { SFLOW_COUNTER_TOKEN_RING, "Token ring counter"},
217 { SFLOW_COUNTER_BASEVG, "100 BaseVG counter"},
218 { SFLOW_COUNTER_VLAN, "Vlan counter"},
219 { SFLOW_COUNTER_PROCESSOR, "Processor counter"},
220 { 0, NULL}
221};
222
223#define SFLOW_IFACE_DIRECTION_UNKNOWN 0
224#define SFLOW_IFACE_DIRECTION_FULLDUPLEX 1
225#define SFLOW_IFACE_DIRECTION_HALFDUPLEX 2
226#define SFLOW_IFACE_DIRECTION_IN 3
227#define SFLOW_IFACE_DIRECTION_OUT 4
228
229static const struct tok sflow_iface_direction_values[] = {
230 { SFLOW_IFACE_DIRECTION_UNKNOWN, "unknown"},
231 { SFLOW_IFACE_DIRECTION_FULLDUPLEX, "full-duplex"},
232 { SFLOW_IFACE_DIRECTION_HALFDUPLEX, "half-duplex"},
233 { SFLOW_IFACE_DIRECTION_IN, "in"},
234 { SFLOW_IFACE_DIRECTION_OUT, "out"},
235 { 0, NULL}
Elliott Hughes892a68b2015-10-19 14:43:53 -0700236};
JP Abgrall53f17a92014-02-12 14:02:41 -0800237
238struct sflow_generic_counter_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700239 nd_uint32_t ifindex;
240 nd_uint32_t iftype;
241 nd_uint64_t ifspeed;
242 nd_uint32_t ifdirection;
243 nd_uint32_t ifstatus;
244 nd_uint64_t ifinoctets;
245 nd_uint32_t ifinunicastpkts;
246 nd_uint32_t ifinmulticastpkts;
247 nd_uint32_t ifinbroadcastpkts;
248 nd_uint32_t ifindiscards;
249 nd_uint32_t ifinerrors;
250 nd_uint32_t ifinunkownprotos;
251 nd_uint64_t ifoutoctets;
252 nd_uint32_t ifoutunicastpkts;
253 nd_uint32_t ifoutmulticastpkts;
254 nd_uint32_t ifoutbroadcastpkts;
255 nd_uint32_t ifoutdiscards;
256 nd_uint32_t ifouterrors;
257 nd_uint32_t ifpromiscmode;
JP Abgrall53f17a92014-02-12 14:02:41 -0800258};
259
260struct sflow_ethernet_counter_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700261 nd_uint32_t alignerrors;
262 nd_uint32_t fcserrors;
263 nd_uint32_t single_collision_frames;
264 nd_uint32_t multiple_collision_frames;
265 nd_uint32_t test_errors;
266 nd_uint32_t deferred_transmissions;
267 nd_uint32_t late_collisions;
268 nd_uint32_t excessive_collisions;
269 nd_uint32_t mac_transmit_errors;
270 nd_uint32_t carrier_sense_errors;
271 nd_uint32_t frame_too_longs;
272 nd_uint32_t mac_receive_errors;
273 nd_uint32_t symbol_errors;
JP Abgrall53f17a92014-02-12 14:02:41 -0800274};
275
276struct sflow_100basevg_counter_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700277 nd_uint32_t in_highpriority_frames;
278 nd_uint64_t in_highpriority_octets;
279 nd_uint32_t in_normpriority_frames;
280 nd_uint64_t in_normpriority_octets;
281 nd_uint32_t in_ipmerrors;
282 nd_uint32_t in_oversized;
283 nd_uint32_t in_data_errors;
284 nd_uint32_t in_null_addressed_frames;
285 nd_uint32_t out_highpriority_frames;
286 nd_uint64_t out_highpriority_octets;
287 nd_uint32_t transitioninto_frames;
288 nd_uint64_t hc_in_highpriority_octets;
289 nd_uint64_t hc_in_normpriority_octets;
290 nd_uint64_t hc_out_highpriority_octets;
JP Abgrall53f17a92014-02-12 14:02:41 -0800291};
292
293struct sflow_vlan_counter_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700294 nd_uint32_t vlan_id;
295 nd_uint64_t octets;
296 nd_uint32_t unicast_pkt;
297 nd_uint32_t multicast_pkt;
298 nd_uint32_t broadcast_pkt;
299 nd_uint32_t discards;
JP Abgrall53f17a92014-02-12 14:02:41 -0800300};
301
302static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700303print_sflow_counter_generic(netdissect_options *ndo,
304 const u_char *pointer, u_int len)
305{
JP Abgrall53f17a92014-02-12 14:02:41 -0800306 const struct sflow_generic_counter_t *sflow_gen_counter;
307
308 if (len < sizeof(struct sflow_generic_counter_t))
309 return 1;
310
JP Abgrall53f17a92014-02-12 14:02:41 -0800311 sflow_gen_counter = (const struct sflow_generic_counter_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700312 ND_PRINT("\n\t ifindex %u, iftype %u, ifspeed %" PRIu64 ", ifdirection %u (%s)",
313 GET_BE_U_4(sflow_gen_counter->ifindex),
314 GET_BE_U_4(sflow_gen_counter->iftype),
315 GET_BE_U_8(sflow_gen_counter->ifspeed),
316 GET_BE_U_4(sflow_gen_counter->ifdirection),
JP Abgrall53f17a92014-02-12 14:02:41 -0800317 tok2str(sflow_iface_direction_values, "Unknown",
Elliott Hughes820eced2021-08-20 18:00:50 -0700318 GET_BE_U_4(sflow_gen_counter->ifdirection)));
319 ND_PRINT("\n\t ifstatus %u, adminstatus: %s, operstatus: %s",
320 GET_BE_U_4(sflow_gen_counter->ifstatus),
321 GET_BE_U_4(sflow_gen_counter->ifstatus)&1 ? "up" : "down",
322 (GET_BE_U_4(sflow_gen_counter->ifstatus)>>1)&1 ? "up" : "down");
323 ND_PRINT("\n\t In octets %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800324 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
Elliott Hughes820eced2021-08-20 18:00:50 -0700325 GET_BE_U_8(sflow_gen_counter->ifinoctets),
326 GET_BE_U_4(sflow_gen_counter->ifinunicastpkts),
327 GET_BE_U_4(sflow_gen_counter->ifinmulticastpkts),
328 GET_BE_U_4(sflow_gen_counter->ifinbroadcastpkts),
329 GET_BE_U_4(sflow_gen_counter->ifindiscards));
330 ND_PRINT("\n\t In errors %u, unknown protos %u",
331 GET_BE_U_4(sflow_gen_counter->ifinerrors),
332 GET_BE_U_4(sflow_gen_counter->ifinunkownprotos));
333 ND_PRINT("\n\t Out octets %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800334 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
Elliott Hughes820eced2021-08-20 18:00:50 -0700335 GET_BE_U_8(sflow_gen_counter->ifoutoctets),
336 GET_BE_U_4(sflow_gen_counter->ifoutunicastpkts),
337 GET_BE_U_4(sflow_gen_counter->ifoutmulticastpkts),
338 GET_BE_U_4(sflow_gen_counter->ifoutbroadcastpkts),
339 GET_BE_U_4(sflow_gen_counter->ifoutdiscards));
340 ND_PRINT("\n\t Out errors %u, promisc mode %u",
341 GET_BE_U_4(sflow_gen_counter->ifouterrors),
342 GET_BE_U_4(sflow_gen_counter->ifpromiscmode));
JP Abgrall53f17a92014-02-12 14:02:41 -0800343
344 return 0;
345}
346
347static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700348print_sflow_counter_ethernet(netdissect_options *ndo,
349 const u_char *pointer, u_int len)
350{
JP Abgrall53f17a92014-02-12 14:02:41 -0800351 const struct sflow_ethernet_counter_t *sflow_eth_counter;
352
353 if (len < sizeof(struct sflow_ethernet_counter_t))
354 return 1;
355
356 sflow_eth_counter = (const struct sflow_ethernet_counter_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700357 ND_PRINT("\n\t align errors %u, fcs errors %u, single collision %u, multiple collision %u, test error %u",
358 GET_BE_U_4(sflow_eth_counter->alignerrors),
359 GET_BE_U_4(sflow_eth_counter->fcserrors),
360 GET_BE_U_4(sflow_eth_counter->single_collision_frames),
361 GET_BE_U_4(sflow_eth_counter->multiple_collision_frames),
362 GET_BE_U_4(sflow_eth_counter->test_errors));
363 ND_PRINT("\n\t deferred %u, late collision %u, excessive collision %u, mac trans error %u",
364 GET_BE_U_4(sflow_eth_counter->deferred_transmissions),
365 GET_BE_U_4(sflow_eth_counter->late_collisions),
366 GET_BE_U_4(sflow_eth_counter->excessive_collisions),
367 GET_BE_U_4(sflow_eth_counter->mac_transmit_errors));
368 ND_PRINT("\n\t carrier error %u, frames too long %u, mac receive errors %u, symbol errors %u",
369 GET_BE_U_4(sflow_eth_counter->carrier_sense_errors),
370 GET_BE_U_4(sflow_eth_counter->frame_too_longs),
371 GET_BE_U_4(sflow_eth_counter->mac_receive_errors),
372 GET_BE_U_4(sflow_eth_counter->symbol_errors));
JP Abgrall53f17a92014-02-12 14:02:41 -0800373
374 return 0;
375}
376
377static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700378print_sflow_counter_token_ring(netdissect_options *ndo _U_,
379 const u_char *pointer _U_, u_int len _U_)
380{
JP Abgrall53f17a92014-02-12 14:02:41 -0800381 return 0;
382}
383
384static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700385print_sflow_counter_basevg(netdissect_options *ndo,
386 const u_char *pointer, u_int len)
387{
JP Abgrall53f17a92014-02-12 14:02:41 -0800388 const struct sflow_100basevg_counter_t *sflow_100basevg_counter;
389
390 if (len < sizeof(struct sflow_100basevg_counter_t))
391 return 1;
392
393 sflow_100basevg_counter = (const struct sflow_100basevg_counter_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700394 ND_PRINT("\n\t in high prio frames %u, in high prio octets %" PRIu64,
395 GET_BE_U_4(sflow_100basevg_counter->in_highpriority_frames),
396 GET_BE_U_8(sflow_100basevg_counter->in_highpriority_octets));
397 ND_PRINT("\n\t in norm prio frames %u, in norm prio octets %" PRIu64,
398 GET_BE_U_4(sflow_100basevg_counter->in_normpriority_frames),
399 GET_BE_U_8(sflow_100basevg_counter->in_normpriority_octets));
400 ND_PRINT("\n\t in ipm errors %u, oversized %u, in data errors %u, null addressed frames %u",
401 GET_BE_U_4(sflow_100basevg_counter->in_ipmerrors),
402 GET_BE_U_4(sflow_100basevg_counter->in_oversized),
403 GET_BE_U_4(sflow_100basevg_counter->in_data_errors),
404 GET_BE_U_4(sflow_100basevg_counter->in_null_addressed_frames));
405 ND_PRINT("\n\t out high prio frames %u, out high prio octets %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800406 ", trans into frames %u",
Elliott Hughes820eced2021-08-20 18:00:50 -0700407 GET_BE_U_4(sflow_100basevg_counter->out_highpriority_frames),
408 GET_BE_U_8(sflow_100basevg_counter->out_highpriority_octets),
409 GET_BE_U_4(sflow_100basevg_counter->transitioninto_frames));
410 ND_PRINT("\n\t in hc high prio octets %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800411 ", in hc norm prio octets %" PRIu64
412 ", out hc high prio octets %" PRIu64,
Elliott Hughes820eced2021-08-20 18:00:50 -0700413 GET_BE_U_8(sflow_100basevg_counter->hc_in_highpriority_octets),
414 GET_BE_U_8(sflow_100basevg_counter->hc_in_normpriority_octets),
415 GET_BE_U_8(sflow_100basevg_counter->hc_out_highpriority_octets));
JP Abgrall53f17a92014-02-12 14:02:41 -0800416
417 return 0;
418}
419
420static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700421print_sflow_counter_vlan(netdissect_options *ndo,
422 const u_char *pointer, u_int len)
423{
JP Abgrall53f17a92014-02-12 14:02:41 -0800424 const struct sflow_vlan_counter_t *sflow_vlan_counter;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700425
JP Abgrall53f17a92014-02-12 14:02:41 -0800426 if (len < sizeof(struct sflow_vlan_counter_t))
427 return 1;
428
429 sflow_vlan_counter = (const struct sflow_vlan_counter_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700430 ND_PRINT("\n\t vlan_id %u, octets %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800431 ", unicast_pkt %u, multicast_pkt %u, broadcast_pkt %u, discards %u",
Elliott Hughes820eced2021-08-20 18:00:50 -0700432 GET_BE_U_4(sflow_vlan_counter->vlan_id),
433 GET_BE_U_8(sflow_vlan_counter->octets),
434 GET_BE_U_4(sflow_vlan_counter->unicast_pkt),
435 GET_BE_U_4(sflow_vlan_counter->multicast_pkt),
436 GET_BE_U_4(sflow_vlan_counter->broadcast_pkt),
437 GET_BE_U_4(sflow_vlan_counter->discards));
JP Abgrall53f17a92014-02-12 14:02:41 -0800438
439 return 0;
440}
441
442struct sflow_processor_counter_t {
Elliott Hughes820eced2021-08-20 18:00:50 -0700443 nd_uint32_t five_sec_util;
444 nd_uint32_t one_min_util;
445 nd_uint32_t five_min_util;
446 nd_uint64_t total_memory;
447 nd_uint64_t free_memory;
JP Abgrall53f17a92014-02-12 14:02:41 -0800448};
449
450static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700451print_sflow_counter_processor(netdissect_options *ndo,
452 const u_char *pointer, u_int len)
453{
JP Abgrall53f17a92014-02-12 14:02:41 -0800454 const struct sflow_processor_counter_t *sflow_processor_counter;
455
456 if (len < sizeof(struct sflow_processor_counter_t))
457 return 1;
458
459 sflow_processor_counter = (const struct sflow_processor_counter_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700460 ND_PRINT("\n\t 5sec %u, 1min %u, 5min %u, total_mem %" PRIu64
JP Abgrall53f17a92014-02-12 14:02:41 -0800461 ", total_mem %" PRIu64,
Elliott Hughes820eced2021-08-20 18:00:50 -0700462 GET_BE_U_4(sflow_processor_counter->five_sec_util),
463 GET_BE_U_4(sflow_processor_counter->one_min_util),
464 GET_BE_U_4(sflow_processor_counter->five_min_util),
465 GET_BE_U_8(sflow_processor_counter->total_memory),
466 GET_BE_U_8(sflow_processor_counter->free_memory));
JP Abgrall53f17a92014-02-12 14:02:41 -0800467
468 return 0;
469}
470
471static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700472sflow_print_counter_records(netdissect_options *ndo,
473 const u_char *pointer, u_int len, u_int records)
474{
JP Abgrall53f17a92014-02-12 14:02:41 -0800475 u_int nrecords;
476 const u_char *tptr;
477 u_int tlen;
478 u_int counter_type;
479 u_int counter_len;
480 u_int enterprise;
481 const struct sflow_counter_record_t *sflow_counter_record;
482
483 nrecords = records;
484 tptr = pointer;
485 tlen = len;
486
487 while (nrecords > 0) {
488 /* do we have the "header?" */
489 if (tlen < sizeof(struct sflow_counter_record_t))
490 return 1;
491 sflow_counter_record = (const struct sflow_counter_record_t *)tptr;
492
Elliott Hughes820eced2021-08-20 18:00:50 -0700493 enterprise = GET_BE_U_4(sflow_counter_record->format);
JP Abgrall53f17a92014-02-12 14:02:41 -0800494 counter_type = enterprise & 0x0FFF;
495 enterprise = enterprise >> 20;
Elliott Hughes820eced2021-08-20 18:00:50 -0700496 counter_len = GET_BE_U_4(sflow_counter_record->length);
497 ND_PRINT("\n\t enterprise %u, %s (%u) length %u",
JP Abgrall53f17a92014-02-12 14:02:41 -0800498 enterprise,
499 (enterprise == 0) ? tok2str(sflow_counter_type_values,"Unknown",counter_type) : "Unknown",
500 counter_type,
Elliott Hughes820eced2021-08-20 18:00:50 -0700501 counter_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800502
503 tptr += sizeof(struct sflow_counter_record_t);
504 tlen -= sizeof(struct sflow_counter_record_t);
505
506 if (tlen < counter_len)
507 return 1;
508 if (enterprise == 0) {
509 switch (counter_type) {
510 case SFLOW_COUNTER_GENERIC:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700511 if (print_sflow_counter_generic(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800512 return 1;
513 break;
514 case SFLOW_COUNTER_ETHERNET:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700515 if (print_sflow_counter_ethernet(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800516 return 1;
517 break;
518 case SFLOW_COUNTER_TOKEN_RING:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700519 if (print_sflow_counter_token_ring(ndo, tptr,tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800520 return 1;
521 break;
522 case SFLOW_COUNTER_BASEVG:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700523 if (print_sflow_counter_basevg(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800524 return 1;
525 break;
526 case SFLOW_COUNTER_VLAN:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700527 if (print_sflow_counter_vlan(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800528 return 1;
529 break;
530 case SFLOW_COUNTER_PROCESSOR:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700531 if (print_sflow_counter_processor(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800532 return 1;
533 break;
534 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700535 if (ndo->ndo_vflag <= 1)
536 print_unknown_data(ndo, tptr, "\n\t\t", counter_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800537 break;
538 }
539 }
540 tptr += counter_len;
541 tlen -= counter_len;
542 nrecords--;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700543
JP Abgrall53f17a92014-02-12 14:02:41 -0800544 }
545
546 return 0;
547}
548
JP Abgrall53f17a92014-02-12 14:02:41 -0800549static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700550sflow_print_counter_sample(netdissect_options *ndo,
551 const u_char *pointer, u_int len)
552{
JP Abgrall53f17a92014-02-12 14:02:41 -0800553 const struct sflow_counter_sample_t *sflow_counter_sample;
554 u_int nrecords;
JP Abgrall53f17a92014-02-12 14:02:41 -0800555
JP Abgrall53f17a92014-02-12 14:02:41 -0800556 if (len < sizeof(struct sflow_counter_sample_t))
557 return 1;
558
559 sflow_counter_sample = (const struct sflow_counter_sample_t *)pointer;
560
Elliott Hughes820eced2021-08-20 18:00:50 -0700561 nrecords = GET_BE_U_4(sflow_counter_sample->records);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700562
Elliott Hughes820eced2021-08-20 18:00:50 -0700563 ND_PRINT(" seqnum %u, type %u, idx %u, records %u",
564 GET_BE_U_4(sflow_counter_sample->seqnum),
565 GET_U_1(sflow_counter_sample->type),
566 GET_BE_U_3(sflow_counter_sample->index),
567 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800568
Elliott Hughes892a68b2015-10-19 14:43:53 -0700569 return sflow_print_counter_records(ndo, pointer + sizeof(struct sflow_counter_sample_t),
JP Abgrall53f17a92014-02-12 14:02:41 -0800570 len - sizeof(struct sflow_counter_sample_t),
571 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800572}
573
574static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700575sflow_print_expanded_counter_sample(netdissect_options *ndo,
576 const u_char *pointer, u_int len)
577{
JP Abgrall53f17a92014-02-12 14:02:41 -0800578 const struct sflow_expanded_counter_sample_t *sflow_expanded_counter_sample;
579 u_int nrecords;
580
581
582 if (len < sizeof(struct sflow_expanded_counter_sample_t))
583 return 1;
584
585 sflow_expanded_counter_sample = (const struct sflow_expanded_counter_sample_t *)pointer;
586
Elliott Hughes820eced2021-08-20 18:00:50 -0700587 nrecords = GET_BE_U_4(sflow_expanded_counter_sample->records);
JP Abgrall53f17a92014-02-12 14:02:41 -0800588
Elliott Hughes820eced2021-08-20 18:00:50 -0700589 ND_PRINT(" seqnum %u, type %u, idx %u, records %u",
590 GET_BE_U_4(sflow_expanded_counter_sample->seqnum),
591 GET_BE_U_4(sflow_expanded_counter_sample->type),
592 GET_BE_U_4(sflow_expanded_counter_sample->index),
593 nrecords);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700594
595 return sflow_print_counter_records(ndo, pointer + sizeof(struct sflow_expanded_counter_sample_t),
JP Abgrall53f17a92014-02-12 14:02:41 -0800596 len - sizeof(struct sflow_expanded_counter_sample_t),
597 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800598}
599
600static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700601print_sflow_raw_packet(netdissect_options *ndo,
602 const u_char *pointer, u_int len)
603{
JP Abgrall53f17a92014-02-12 14:02:41 -0800604 const struct sflow_expanded_flow_raw_t *sflow_flow_raw;
605
606 if (len < sizeof(struct sflow_expanded_flow_raw_t))
607 return 1;
608
609 sflow_flow_raw = (const struct sflow_expanded_flow_raw_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700610 ND_PRINT("\n\t protocol %s (%u), length %u, stripped bytes %u, header_size %u",
611 tok2str(sflow_flow_raw_protocol_values,"Unknown",GET_BE_U_4(sflow_flow_raw->protocol)),
612 GET_BE_U_4(sflow_flow_raw->protocol),
613 GET_BE_U_4(sflow_flow_raw->length),
614 GET_BE_U_4(sflow_flow_raw->stripped_bytes),
615 GET_BE_U_4(sflow_flow_raw->header_size));
JP Abgrall53f17a92014-02-12 14:02:41 -0800616
617 /* QUESTION - should we attempt to print the raw header itself?
Elliott Hughes820eced2021-08-20 18:00:50 -0700618 assuming of course there is enough data present to do so... */
JP Abgrall53f17a92014-02-12 14:02:41 -0800619
620 return 0;
621}
622
623static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700624print_sflow_ethernet_frame(netdissect_options *ndo,
625 const u_char *pointer, u_int len)
626{
JP Abgrall53f17a92014-02-12 14:02:41 -0800627 const struct sflow_ethernet_frame_t *sflow_ethernet_frame;
628
629 if (len < sizeof(struct sflow_ethernet_frame_t))
630 return 1;
631
632 sflow_ethernet_frame = (const struct sflow_ethernet_frame_t *)pointer;
633
Elliott Hughes820eced2021-08-20 18:00:50 -0700634 ND_PRINT("\n\t frame len %u, type %u",
635 GET_BE_U_4(sflow_ethernet_frame->length),
636 GET_BE_U_4(sflow_ethernet_frame->type));
JP Abgrall53f17a92014-02-12 14:02:41 -0800637
638 return 0;
639}
640
641static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700642print_sflow_extended_switch_data(netdissect_options *ndo,
643 const u_char *pointer, u_int len)
644{
JP Abgrall53f17a92014-02-12 14:02:41 -0800645 const struct sflow_extended_switch_data_t *sflow_extended_sw_data;
646
647 if (len < sizeof(struct sflow_extended_switch_data_t))
648 return 1;
649
650 sflow_extended_sw_data = (const struct sflow_extended_switch_data_t *)pointer;
Elliott Hughes820eced2021-08-20 18:00:50 -0700651 ND_PRINT("\n\t src vlan %u, src pri %u, dst vlan %u, dst pri %u",
652 GET_BE_U_4(sflow_extended_sw_data->src_vlan),
653 GET_BE_U_4(sflow_extended_sw_data->src_pri),
654 GET_BE_U_4(sflow_extended_sw_data->dst_vlan),
655 GET_BE_U_4(sflow_extended_sw_data->dst_pri));
JP Abgrall53f17a92014-02-12 14:02:41 -0800656
657 return 0;
658}
659
660static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700661sflow_print_flow_records(netdissect_options *ndo,
662 const u_char *pointer, u_int len, u_int records)
663{
JP Abgrall53f17a92014-02-12 14:02:41 -0800664 u_int nrecords;
665 const u_char *tptr;
666 u_int tlen;
667 u_int flow_type;
668 u_int enterprise;
669 u_int flow_len;
670 const struct sflow_flow_record_t *sflow_flow_record;
671
672 nrecords = records;
673 tptr = pointer;
674 tlen = len;
675
676 while (nrecords > 0) {
677 /* do we have the "header?" */
678 if (tlen < sizeof(struct sflow_flow_record_t))
679 return 1;
680
681 sflow_flow_record = (const struct sflow_flow_record_t *)tptr;
682
683 /* so, the funky encoding means we cannot blythly mask-off
684 bits, we must also check the enterprise. */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700685
Elliott Hughes820eced2021-08-20 18:00:50 -0700686 enterprise = GET_BE_U_4(sflow_flow_record->format);
JP Abgrall53f17a92014-02-12 14:02:41 -0800687 flow_type = enterprise & 0x0FFF;
688 enterprise = enterprise >> 12;
Elliott Hughes820eced2021-08-20 18:00:50 -0700689 flow_len = GET_BE_U_4(sflow_flow_record->length);
690 ND_PRINT("\n\t enterprise %u %s (%u) length %u",
JP Abgrall53f17a92014-02-12 14:02:41 -0800691 enterprise,
692 (enterprise == 0) ? tok2str(sflow_flow_type_values,"Unknown",flow_type) : "Unknown",
693 flow_type,
Elliott Hughes820eced2021-08-20 18:00:50 -0700694 flow_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800695
696 tptr += sizeof(struct sflow_flow_record_t);
697 tlen -= sizeof(struct sflow_flow_record_t);
698
699 if (tlen < flow_len)
700 return 1;
701
702 if (enterprise == 0) {
703 switch (flow_type) {
704 case SFLOW_FLOW_RAW_PACKET:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700705 if (print_sflow_raw_packet(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800706 return 1;
707 break;
708 case SFLOW_FLOW_EXTENDED_SWITCH_DATA:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700709 if (print_sflow_extended_switch_data(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800710 return 1;
711 break;
712 case SFLOW_FLOW_ETHERNET_FRAME:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700713 if (print_sflow_ethernet_frame(ndo, tptr, tlen))
JP Abgrall53f17a92014-02-12 14:02:41 -0800714 return 1;
715 break;
716 /* FIXME these need a decoder */
717 case SFLOW_FLOW_IPV4_DATA:
718 case SFLOW_FLOW_IPV6_DATA:
719 case SFLOW_FLOW_EXTENDED_ROUTER_DATA:
720 case SFLOW_FLOW_EXTENDED_GATEWAY_DATA:
721 case SFLOW_FLOW_EXTENDED_USER_DATA:
722 case SFLOW_FLOW_EXTENDED_URL_DATA:
723 case SFLOW_FLOW_EXTENDED_MPLS_DATA:
724 case SFLOW_FLOW_EXTENDED_NAT_DATA:
725 case SFLOW_FLOW_EXTENDED_MPLS_TUNNEL:
726 case SFLOW_FLOW_EXTENDED_MPLS_VC:
727 case SFLOW_FLOW_EXTENDED_MPLS_FEC:
728 case SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC:
729 case SFLOW_FLOW_EXTENDED_VLAN_TUNNEL:
730 break;
731 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700732 if (ndo->ndo_vflag <= 1)
733 print_unknown_data(ndo, tptr, "\n\t\t", flow_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800734 break;
735 }
736 }
737 tptr += flow_len;
738 tlen -= flow_len;
739 nrecords--;
740
741 }
742
743 return 0;
744}
745
746static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700747sflow_print_flow_sample(netdissect_options *ndo,
748 const u_char *pointer, u_int len)
749{
JP Abgrall53f17a92014-02-12 14:02:41 -0800750 const struct sflow_flow_sample_t *sflow_flow_sample;
751 u_int nrecords;
JP Abgrall53f17a92014-02-12 14:02:41 -0800752
753 if (len < sizeof(struct sflow_flow_sample_t))
754 return 1;
755
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700756 sflow_flow_sample = (const struct sflow_flow_sample_t *)pointer;
JP Abgrall53f17a92014-02-12 14:02:41 -0800757
Elliott Hughes820eced2021-08-20 18:00:50 -0700758 nrecords = GET_BE_U_4(sflow_flow_sample->records);
JP Abgrall53f17a92014-02-12 14:02:41 -0800759
Elliott Hughes820eced2021-08-20 18:00:50 -0700760 ND_PRINT(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, input %u output %u records %u",
761 GET_BE_U_4(sflow_flow_sample->seqnum),
762 GET_U_1(sflow_flow_sample->type),
763 GET_BE_U_3(sflow_flow_sample->index),
764 GET_BE_U_4(sflow_flow_sample->rate),
765 GET_BE_U_4(sflow_flow_sample->pool),
766 GET_BE_U_4(sflow_flow_sample->drops),
767 GET_BE_U_4(sflow_flow_sample->in_interface),
768 GET_BE_U_4(sflow_flow_sample->out_interface),
769 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800770
Elliott Hughes892a68b2015-10-19 14:43:53 -0700771 return sflow_print_flow_records(ndo, pointer + sizeof(struct sflow_flow_sample_t),
JP Abgrall53f17a92014-02-12 14:02:41 -0800772 len - sizeof(struct sflow_flow_sample_t),
773 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800774}
775
776static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700777sflow_print_expanded_flow_sample(netdissect_options *ndo,
778 const u_char *pointer, u_int len)
779{
JP Abgrall53f17a92014-02-12 14:02:41 -0800780 const struct sflow_expanded_flow_sample_t *sflow_expanded_flow_sample;
781 u_int nrecords;
782
783 if (len < sizeof(struct sflow_expanded_flow_sample_t))
784 return 1;
785
786 sflow_expanded_flow_sample = (const struct sflow_expanded_flow_sample_t *)pointer;
787
Elliott Hughes820eced2021-08-20 18:00:50 -0700788 nrecords = GET_BE_U_4(sflow_expanded_flow_sample->records);
JP Abgrall53f17a92014-02-12 14:02:41 -0800789
Elliott Hughes820eced2021-08-20 18:00:50 -0700790 ND_PRINT(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, records %u",
791 GET_BE_U_4(sflow_expanded_flow_sample->seqnum),
792 GET_BE_U_4(sflow_expanded_flow_sample->type),
793 GET_BE_U_4(sflow_expanded_flow_sample->index),
794 GET_BE_U_4(sflow_expanded_flow_sample->rate),
795 GET_BE_U_4(sflow_expanded_flow_sample->pool),
796 GET_BE_U_4(sflow_expanded_flow_sample->drops),
797 nrecords);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700798
799 return sflow_print_flow_records(ndo, pointer + sizeof(struct sflow_expanded_flow_sample_t),
JP Abgrall53f17a92014-02-12 14:02:41 -0800800 len - sizeof(struct sflow_expanded_flow_sample_t),
801 nrecords);
JP Abgrall53f17a92014-02-12 14:02:41 -0800802}
803
804void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700805sflow_print(netdissect_options *ndo,
806 const u_char *pptr, u_int len)
807{
JP Abgrall53f17a92014-02-12 14:02:41 -0800808 const struct sflow_datagram_t *sflow_datagram;
809 const struct sflow_sample_header *sflow_sample;
810
811 const u_char *tptr;
812 u_int tlen;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700813 uint32_t sflow_sample_type, sflow_sample_len;
814 uint32_t nsamples;
JP Abgrall53f17a92014-02-12 14:02:41 -0800815
Elliott Hughes820eced2021-08-20 18:00:50 -0700816 ndo->ndo_protocol = "sflow";
JP Abgrall53f17a92014-02-12 14:02:41 -0800817 tptr = pptr;
818 tlen = len;
819 sflow_datagram = (const struct sflow_datagram_t *)pptr;
Elliott Hughes820eced2021-08-20 18:00:50 -0700820 if (len < sizeof(struct sflow_datagram_t)) {
821 ND_PRINT("sFlowv%u", GET_BE_U_4(sflow_datagram->version));
822 ND_PRINT(" [length %u < %zu]", len, sizeof(struct sflow_datagram_t));
823 nd_print_invalid(ndo);
824 return;
825 }
826 ND_TCHECK_SIZE(sflow_datagram);
JP Abgrall53f17a92014-02-12 14:02:41 -0800827
828 /*
829 * Sanity checking of the header.
830 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700831 if (GET_BE_U_4(sflow_datagram->version) != 5) {
832 ND_PRINT("sFlow version %u packet not supported",
833 GET_BE_U_4(sflow_datagram->version));
JP Abgrall53f17a92014-02-12 14:02:41 -0800834 return;
835 }
836
Elliott Hughes892a68b2015-10-19 14:43:53 -0700837 if (ndo->ndo_vflag < 1) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700838 ND_PRINT("sFlowv%u, %s agent %s, agent-id %u, length %u",
839 GET_BE_U_4(sflow_datagram->version),
840 GET_BE_U_4(sflow_datagram->ip_version) == 1 ? "IPv4" : "IPv6",
841 GET_IPADDR_STRING(sflow_datagram->agent),
842 GET_BE_U_4(sflow_datagram->agent_id),
843 len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800844 return;
845 }
846
847 /* ok they seem to want to know everything - lets fully decode it */
Elliott Hughes820eced2021-08-20 18:00:50 -0700848 nsamples=GET_BE_U_4(sflow_datagram->samples);
849 ND_PRINT("sFlowv%u, %s agent %s, agent-id %u, seqnum %u, uptime %u, samples %u, length %u",
850 GET_BE_U_4(sflow_datagram->version),
851 GET_BE_U_4(sflow_datagram->ip_version) == 1 ? "IPv4" : "IPv6",
852 GET_IPADDR_STRING(sflow_datagram->agent),
853 GET_BE_U_4(sflow_datagram->agent_id),
854 GET_BE_U_4(sflow_datagram->seqnum),
855 GET_BE_U_4(sflow_datagram->uptime),
JP Abgrall53f17a92014-02-12 14:02:41 -0800856 nsamples,
Elliott Hughes820eced2021-08-20 18:00:50 -0700857 len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800858
859 /* skip Common header */
Elliott Hughes820eced2021-08-20 18:00:50 -0700860 tptr += sizeof(struct sflow_datagram_t);
861 tlen -= sizeof(struct sflow_datagram_t);
JP Abgrall53f17a92014-02-12 14:02:41 -0800862
863 while (nsamples > 0 && tlen > 0) {
864 sflow_sample = (const struct sflow_sample_header *)tptr;
JP Abgrall53f17a92014-02-12 14:02:41 -0800865
Elliott Hughes820eced2021-08-20 18:00:50 -0700866 sflow_sample_type = (GET_BE_U_4(sflow_sample->format)&0x0FFF);
867 sflow_sample_len = GET_BE_U_4(sflow_sample->len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800868
869 if (tlen < sizeof(struct sflow_sample_header))
Elliott Hughes820eced2021-08-20 18:00:50 -0700870 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800871
872 tptr += sizeof(struct sflow_sample_header);
873 tlen -= sizeof(struct sflow_sample_header);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700874
Elliott Hughes820eced2021-08-20 18:00:50 -0700875 ND_PRINT("\n\t%s (%u), length %u,",
JP Abgrall53f17a92014-02-12 14:02:41 -0800876 tok2str(sflow_format_values, "Unknown", sflow_sample_type),
877 sflow_sample_type,
Elliott Hughes820eced2021-08-20 18:00:50 -0700878 sflow_sample_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800879
880 /* basic sanity check */
881 if (sflow_sample_type == 0 || sflow_sample_len ==0) {
882 return;
883 }
884
885 if (tlen < sflow_sample_len)
Elliott Hughes820eced2021-08-20 18:00:50 -0700886 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800887
888 /* did we capture enough for fully decoding the sample ? */
Elliott Hughes820eced2021-08-20 18:00:50 -0700889 ND_TCHECK_LEN(tptr, sflow_sample_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800890
891 switch(sflow_sample_type) {
892 case SFLOW_FLOW_SAMPLE:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700893 if (sflow_print_flow_sample(ndo, tptr, tlen))
Elliott Hughes820eced2021-08-20 18:00:50 -0700894 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800895 break;
896
897 case SFLOW_COUNTER_SAMPLE:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700898 if (sflow_print_counter_sample(ndo, tptr,tlen))
Elliott Hughes820eced2021-08-20 18:00:50 -0700899 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800900 break;
901
902 case SFLOW_EXPANDED_FLOW_SAMPLE:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700903 if (sflow_print_expanded_flow_sample(ndo, tptr, tlen))
Elliott Hughes820eced2021-08-20 18:00:50 -0700904 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800905 break;
906
907 case SFLOW_EXPANDED_COUNTER_SAMPLE:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700908 if (sflow_print_expanded_counter_sample(ndo, tptr,tlen))
Elliott Hughes820eced2021-08-20 18:00:50 -0700909 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800910 break;
911
912 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700913 if (ndo->ndo_vflag <= 1)
914 print_unknown_data(ndo, tptr, "\n\t ", sflow_sample_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800915 break;
916 }
917 tptr += sflow_sample_len;
918 tlen -= sflow_sample_len;
919 nsamples--;
920 }
921 return;
922
Elliott Hughes820eced2021-08-20 18:00:50 -0700923invalid:
924 nd_print_invalid(ndo);
925 ND_TCHECK_LEN(tptr, tlen);
JP Abgrall53f17a92014-02-12 14:02:41 -0800926}