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