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