]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
OpenFlow 1.0: Simplify of10_vendor_message_print().
[tcpdump] / print-cnfp.c
1 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
2
3 /*
4 * Copyright (c) 1998 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Michael Shalayeff.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* \summary: Cisco NetFlow protocol printer */
34
35 /*
36 * Cisco NetFlow protocol
37 *
38 * See
39 *
40 * https://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include "netdissect-stdinc.h"
48
49 #include <stdio.h>
50 #include <string.h>
51
52 #include "netdissect.h"
53 #include "addrtoname.h"
54 #include "extract.h"
55
56 #include "tcp.h"
57 #include "ipproto.h"
58
59 struct nfhdr_v1 {
60 nd_uint16_t version; /* version number */
61 nd_uint16_t count; /* # of records */
62 nd_uint32_t msys_uptime;
63 nd_uint32_t utc_sec;
64 nd_uint32_t utc_nsec;
65 };
66
67 struct nfrec_v1 {
68 nd_ipv4 src_ina;
69 nd_ipv4 dst_ina;
70 nd_ipv4 nhop_ina;
71 nd_uint16_t input; /* SNMP index of input interface */
72 nd_uint16_t output; /* SNMP index of output interface */
73 nd_uint32_t packets; /* packets in the flow */
74 nd_uint32_t octets; /* layer 3 octets in the packets of the flow */
75 nd_uint32_t start_time; /* sys_uptime value at start of flow */
76 nd_uint32_t last_time; /* sys_uptime value when last packet of flow was received */
77 nd_uint16_t srcport; /* TCP/UDP source port or equivalent */
78 nd_uint16_t dstport; /* TCP/UDP source port or equivalent */
79 nd_byte pad1[2]; /* pad */
80 nd_uint8_t proto; /* IP protocol type */
81 nd_uint8_t tos; /* IP type of service */
82 nd_uint8_t tcp_flags; /* cumulative OR of TCP flags */
83 nd_byte pad[3]; /* padding */
84 nd_uint32_t reserved; /* unused */
85 };
86
87 struct nfhdr_v5 {
88 nd_uint16_t version; /* version number */
89 nd_uint16_t count; /* # of records */
90 nd_uint32_t msys_uptime;
91 nd_uint32_t utc_sec;
92 nd_uint32_t utc_nsec;
93 nd_uint32_t sequence; /* flow sequence number */
94 nd_uint8_t engine_type; /* type of flow-switching engine */
95 nd_uint8_t engine_id; /* slot number of the flow-switching engine */
96 nd_uint16_t sampling_interval; /* sampling mode and interval */
97 };
98
99 struct nfrec_v5 {
100 nd_ipv4 src_ina;
101 nd_ipv4 dst_ina;
102 nd_ipv4 nhop_ina;
103 nd_uint16_t input; /* SNMP index of input interface */
104 nd_uint16_t output; /* SNMP index of output interface */
105 nd_uint32_t packets; /* packets in the flow */
106 nd_uint32_t octets; /* layer 3 octets in the packets of the flow */
107 nd_uint32_t start_time; /* sys_uptime value at start of flow */
108 nd_uint32_t last_time; /* sys_uptime value when last packet of flow was received */
109 nd_uint16_t srcport; /* TCP/UDP source port or equivalent */
110 nd_uint16_t dstport; /* TCP/UDP source port or equivalent */
111 nd_byte pad1; /* pad */
112 nd_uint8_t tcp_flags; /* cumulative OR of TCP flags */
113 nd_uint8_t proto; /* IP protocol type */
114 nd_uint8_t tos; /* IP type of service */
115 nd_uint16_t src_as; /* AS number of the source */
116 nd_uint16_t dst_as; /* AS number of the destination */
117 nd_uint8_t src_mask; /* source address mask bits */
118 nd_uint8_t dst_mask; /* destination address prefix mask bits */
119 nd_byte pad2[2];
120 nd_ipv4 peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
121 };
122
123 struct nfhdr_v6 {
124 nd_uint16_t version; /* version number */
125 nd_uint16_t count; /* # of records */
126 nd_uint32_t msys_uptime;
127 nd_uint32_t utc_sec;
128 nd_uint32_t utc_nsec;
129 nd_uint32_t sequence; /* v5 flow sequence number */
130 nd_uint32_t reserved; /* v5 only */
131 };
132
133 struct nfrec_v6 {
134 nd_ipv4 src_ina;
135 nd_ipv4 dst_ina;
136 nd_ipv4 nhop_ina;
137 nd_uint16_t input; /* SNMP index of input interface */
138 nd_uint16_t output; /* SNMP index of output interface */
139 nd_uint32_t packets; /* packets in the flow */
140 nd_uint32_t octets; /* layer 3 octets in the packets of the flow */
141 nd_uint32_t start_time; /* sys_uptime value at start of flow */
142 nd_uint32_t last_time; /* sys_uptime value when last packet of flow was received */
143 nd_uint16_t srcport; /* TCP/UDP source port or equivalent */
144 nd_uint16_t dstport; /* TCP/UDP source port or equivalent */
145 nd_byte pad1; /* pad */
146 nd_uint8_t tcp_flags; /* cumulative OR of TCP flags */
147 nd_uint8_t proto; /* IP protocol type */
148 nd_uint8_t tos; /* IP type of service */
149 nd_uint16_t src_as; /* AS number of the source */
150 nd_uint16_t dst_as; /* AS number of the destination */
151 nd_uint8_t src_mask; /* source address mask bits */
152 nd_uint8_t dst_mask; /* destination address prefix mask bits */
153 nd_uint16_t flags;
154 nd_ipv4 peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
155 };
156
157 static void
158 cnfp_v1_print(netdissect_options *ndo, const u_char *cp)
159 {
160 const struct nfhdr_v1 *nh;
161 const struct nfrec_v1 *nr;
162 const char *p_name;
163 uint8_t proto;
164 u_int nrecs, ver;
165 #if 0
166 time_t t;
167 #endif
168
169 nh = (const struct nfhdr_v1 *)cp;
170 ND_TCHECK_SIZE(nh);
171
172 ver = GET_BE_U_2(nh->version);
173 nrecs = GET_BE_U_4(nh->count);
174 #if 0
175 /*
176 * This is seconds since the UN*X epoch, and is followed by
177 * nanoseconds. XXX - format it, rather than just dumping the
178 * raw seconds-since-the-Epoch.
179 */
180 t = GET_BE_U_4(nh->utc_sec);
181 #endif
182
183 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
184 GET_BE_U_4(nh->msys_uptime)/1000,
185 GET_BE_U_4(nh->msys_uptime)%1000,
186 GET_BE_U_4(nh->utc_sec), GET_BE_U_4(nh->utc_nsec));
187
188 nr = (const struct nfrec_v1 *)&nh[1];
189
190 ND_PRINT("%2u recs", nrecs);
191
192 for (; nrecs != 0; nr++, nrecs--) {
193 char buf[20];
194 char asbuf[20];
195
196 /*
197 * Make sure we have the entire record.
198 */
199 ND_TCHECK_SIZE(nr);
200 ND_PRINT("\n started %u.%03u, last %u.%03u",
201 GET_BE_U_4(nr->start_time)/1000,
202 GET_BE_U_4(nr->start_time)%1000,
203 GET_BE_U_4(nr->last_time)/1000,
204 GET_BE_U_4(nr->last_time)%1000);
205
206 asbuf[0] = buf[0] = '\0';
207 ND_PRINT("\n %s%s%s:%u ",
208 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->src_ina)),
209 buf, asbuf,
210 GET_BE_U_2(nr->srcport));
211
212 ND_PRINT("> %s%s%s:%u ",
213 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->dst_ina)),
214 buf, asbuf,
215 GET_BE_U_2(nr->dstport));
216
217 ND_PRINT(">> %s\n ",
218 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
219
220 proto = GET_U_1(nr->proto);
221 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
222 ND_PRINT("%s ", p_name);
223 else
224 ND_PRINT("%u ", proto);
225
226 /* tcp flags for tcp only */
227 if (proto == IPPROTO_TCP) {
228 u_int flags;
229 flags = GET_U_1(nr->tcp_flags);
230 ND_PRINT("%s%s%s%s%s%s%s",
231 flags & TH_FIN ? "F" : "",
232 flags & TH_SYN ? "S" : "",
233 flags & TH_RST ? "R" : "",
234 flags & TH_PUSH ? "P" : "",
235 flags & TH_ACK ? "A" : "",
236 flags & TH_URG ? "U" : "",
237 flags ? " " : "");
238 }
239
240 buf[0]='\0';
241 ND_PRINT("tos %u, %u (%u octets) %s",
242 GET_U_1(nr->tos),
243 GET_BE_U_4(nr->packets),
244 GET_BE_U_4(nr->octets), buf);
245 }
246 return;
247
248 trunc:
249 nd_print_trunc(ndo);
250 }
251
252 static void
253 cnfp_v5_print(netdissect_options *ndo, const u_char *cp)
254 {
255 const struct nfhdr_v5 *nh;
256 const struct nfrec_v5 *nr;
257 const char *p_name;
258 uint8_t proto;
259 u_int nrecs, ver;
260 #if 0
261 time_t t;
262 #endif
263
264 nh = (const struct nfhdr_v5 *)cp;
265 ND_TCHECK_SIZE(nh);
266
267 ver = GET_BE_U_2(nh->version);
268 nrecs = GET_BE_U_4(nh->count);
269 #if 0
270 /*
271 * This is seconds since the UN*X epoch, and is followed by
272 * nanoseconds. XXX - format it, rather than just dumping the
273 * raw seconds-since-the-Epoch.
274 */
275 t = GET_BE_U_4(nh->utc_sec);
276 #endif
277
278 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
279 GET_BE_U_4(nh->msys_uptime)/1000,
280 GET_BE_U_4(nh->msys_uptime)%1000,
281 GET_BE_U_4(nh->utc_sec), GET_BE_U_4(nh->utc_nsec));
282
283 ND_PRINT("#%u, ", GET_BE_U_4(nh->sequence));
284 nr = (const struct nfrec_v5 *)&nh[1];
285
286 ND_PRINT("%2u recs", nrecs);
287
288 for (; nrecs != 0; nr++, nrecs--) {
289 char buf[20];
290 char asbuf[20];
291
292 /*
293 * Make sure we have the entire record.
294 */
295 ND_TCHECK_SIZE(nr);
296 ND_PRINT("\n started %u.%03u, last %u.%03u",
297 GET_BE_U_4(nr->start_time)/1000,
298 GET_BE_U_4(nr->start_time)%1000,
299 GET_BE_U_4(nr->last_time)/1000,
300 GET_BE_U_4(nr->last_time)%1000);
301
302 asbuf[0] = buf[0] = '\0';
303 snprintf(buf, sizeof(buf), "/%u", GET_U_1(nr->src_mask));
304 snprintf(asbuf, sizeof(asbuf), ":%u",
305 GET_BE_U_2(nr->src_as));
306 ND_PRINT("\n %s%s%s:%u ",
307 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->src_ina)),
308 buf, asbuf,
309 GET_BE_U_2(nr->srcport));
310
311 snprintf(buf, sizeof(buf), "/%u", GET_U_1(nr->dst_mask));
312 snprintf(asbuf, sizeof(asbuf), ":%u",
313 GET_BE_U_2(nr->dst_as));
314 ND_PRINT("> %s%s%s:%u ",
315 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->dst_ina)),
316 buf, asbuf,
317 GET_BE_U_2(nr->dstport));
318
319 ND_PRINT(">> %s\n ",
320 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
321
322 proto = GET_U_1(nr->proto);
323 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
324 ND_PRINT("%s ", p_name);
325 else
326 ND_PRINT("%u ", proto);
327
328 /* tcp flags for tcp only */
329 if (proto == IPPROTO_TCP) {
330 u_int flags;
331 flags = GET_U_1(nr->tcp_flags);
332 ND_PRINT("%s%s%s%s%s%s%s",
333 flags & TH_FIN ? "F" : "",
334 flags & TH_SYN ? "S" : "",
335 flags & TH_RST ? "R" : "",
336 flags & TH_PUSH ? "P" : "",
337 flags & TH_ACK ? "A" : "",
338 flags & TH_URG ? "U" : "",
339 flags ? " " : "");
340 }
341
342 buf[0]='\0';
343 ND_PRINT("tos %u, %u (%u octets) %s",
344 GET_U_1(nr->tos),
345 GET_BE_U_4(nr->packets),
346 GET_BE_U_4(nr->octets), buf);
347 }
348 return;
349
350 trunc:
351 nd_print_trunc(ndo);
352 }
353
354 static void
355 cnfp_v6_print(netdissect_options *ndo, const u_char *cp)
356 {
357 const struct nfhdr_v6 *nh;
358 const struct nfrec_v6 *nr;
359 const char *p_name;
360 uint8_t proto;
361 u_int nrecs, ver;
362 #if 0
363 time_t t;
364 #endif
365
366 nh = (const struct nfhdr_v6 *)cp;
367 ND_TCHECK_SIZE(nh);
368
369 ver = GET_BE_U_2(nh->version);
370 nrecs = GET_BE_U_4(nh->count);
371 #if 0
372 /*
373 * This is seconds since the UN*X epoch, and is followed by
374 * nanoseconds. XXX - format it, rather than just dumping the
375 * raw seconds-since-the-Epoch.
376 */
377 t = GET_BE_U_4(nh->utc_sec);
378 #endif
379
380 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
381 GET_BE_U_4(nh->msys_uptime)/1000,
382 GET_BE_U_4(nh->msys_uptime)%1000,
383 GET_BE_U_4(nh->utc_sec), GET_BE_U_4(nh->utc_nsec));
384
385 ND_PRINT("#%u, ", GET_BE_U_4(nh->sequence));
386 nr = (const struct nfrec_v6 *)&nh[1];
387
388 ND_PRINT("%2u recs", nrecs);
389
390 for (; nrecs != 0; nr++, nrecs--) {
391 char buf[20];
392 char asbuf[20];
393
394 /*
395 * Make sure we have the entire record.
396 */
397 ND_TCHECK_SIZE(nr);
398 ND_PRINT("\n started %u.%03u, last %u.%03u",
399 GET_BE_U_4(nr->start_time)/1000,
400 GET_BE_U_4(nr->start_time)%1000,
401 GET_BE_U_4(nr->last_time)/1000,
402 GET_BE_U_4(nr->last_time)%1000);
403
404 asbuf[0] = buf[0] = '\0';
405 snprintf(buf, sizeof(buf), "/%u", GET_U_1(nr->src_mask));
406 snprintf(asbuf, sizeof(asbuf), ":%u",
407 GET_BE_U_2(nr->src_as));
408 ND_PRINT("\n %s%s%s:%u ",
409 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->src_ina)),
410 buf, asbuf,
411 GET_BE_U_2(nr->srcport));
412
413 snprintf(buf, sizeof(buf), "/%u", GET_U_1(nr->dst_mask));
414 snprintf(asbuf, sizeof(asbuf), ":%u",
415 GET_BE_U_2(nr->dst_as));
416 ND_PRINT("> %s%s%s:%u ",
417 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->dst_ina)),
418 buf, asbuf,
419 GET_BE_U_2(nr->dstport));
420
421 ND_PRINT(">> %s\n ",
422 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
423
424 proto = GET_U_1(nr->proto);
425 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
426 ND_PRINT("%s ", p_name);
427 else
428 ND_PRINT("%u ", proto);
429
430 /* tcp flags for tcp only */
431 if (proto == IPPROTO_TCP) {
432 u_int flags;
433 flags = GET_U_1(nr->tcp_flags);
434 ND_PRINT("%s%s%s%s%s%s%s",
435 flags & TH_FIN ? "F" : "",
436 flags & TH_SYN ? "S" : "",
437 flags & TH_RST ? "R" : "",
438 flags & TH_PUSH ? "P" : "",
439 flags & TH_ACK ? "A" : "",
440 flags & TH_URG ? "U" : "",
441 flags ? " " : "");
442 }
443
444 buf[0]='\0';
445 snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
446 (GET_BE_U_2(nr->flags) >> 8) & 0xff,
447 (GET_BE_U_2(nr->flags)) & 0xff);
448 ND_PRINT("tos %u, %u (%u octets) %s",
449 GET_U_1(nr->tos),
450 GET_BE_U_4(nr->packets),
451 GET_BE_U_4(nr->octets), buf);
452 }
453 return;
454
455 trunc:
456 nd_print_trunc(ndo);
457 }
458
459 void
460 cnfp_print(netdissect_options *ndo, const u_char *cp)
461 {
462 int ver;
463
464 /*
465 * First 2 bytes are the version number.
466 */
467 ndo->ndo_protocol = "cnfp";
468 ver = GET_BE_U_2(cp);
469 switch (ver) {
470
471 case 1:
472 cnfp_v1_print(ndo, cp);
473 break;
474
475 case 5:
476 cnfp_v5_print(ndo, cp);
477 break;
478
479 case 6:
480 cnfp_v6_print(ndo, cp);
481 break;
482
483 default:
484 ND_PRINT("NetFlow v%x", ver);
485 break;
486 }
487 }