]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
Update ND_PRINT() as a variadic macro
[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 * http://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 struct in_addr src_ina;
69 struct in_addr dst_ina;
70 struct in_addr 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 struct in_addr src_ina;
101 struct in_addr dst_ina;
102 struct in_addr 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 struct in_addr 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 struct in_addr src_ina;
135 struct in_addr dst_ina;
136 struct in_addr 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 struct in_addr 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 = EXTRACT_BE_U_2(nh->version);
173 nrecs = EXTRACT_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 = EXTRACT_BE_U_4(nh->utc_sec);
181 #endif
182
183 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
184 EXTRACT_BE_U_4(nh->msys_uptime)/1000,
185 EXTRACT_BE_U_4(nh->msys_uptime)%1000,
186 EXTRACT_BE_U_4(nh->utc_sec), EXTRACT_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 EXTRACT_BE_U_4(nr->start_time)/1000,
202 EXTRACT_BE_U_4(nr->start_time)%1000,
203 EXTRACT_BE_U_4(nr->last_time)/1000,
204 EXTRACT_BE_U_4(nr->last_time)%1000);
205
206 asbuf[0] = buf[0] = '\0';
207 ND_PRINT("\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
208 EXTRACT_BE_U_2(nr->srcport));
209
210 ND_PRINT("> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
211 EXTRACT_BE_U_2(nr->dstport));
212
213 ND_PRINT(">> %s\n ", intoa(nr->nhop_ina.s_addr));
214
215 proto = EXTRACT_U_1(nr->proto);
216 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
217 ND_PRINT("%s ", p_name);
218 else
219 ND_PRINT("%u ", proto);
220
221 /* tcp flags for tcp only */
222 if (proto == IPPROTO_TCP) {
223 u_int flags;
224 flags = EXTRACT_U_1(nr->tcp_flags);
225 ND_PRINT("%s%s%s%s%s%s%s",
226 flags & TH_FIN ? "F" : "",
227 flags & TH_SYN ? "S" : "",
228 flags & TH_RST ? "R" : "",
229 flags & TH_PUSH ? "P" : "",
230 flags & TH_ACK ? "A" : "",
231 flags & TH_URG ? "U" : "",
232 flags ? " " : "");
233 }
234
235 buf[0]='\0';
236 ND_PRINT("tos %u, %u (%u octets) %s",
237 EXTRACT_U_1(nr->tos),
238 EXTRACT_BE_U_4(nr->packets),
239 EXTRACT_BE_U_4(nr->octets), buf);
240 }
241 return;
242
243 trunc:
244 ND_PRINT("[|cnfp]");
245 return;
246 }
247
248 static void
249 cnfp_v5_print(netdissect_options *ndo, const u_char *cp)
250 {
251 const struct nfhdr_v5 *nh;
252 const struct nfrec_v5 *nr;
253 const char *p_name;
254 uint8_t proto;
255 u_int nrecs, ver;
256 #if 0
257 time_t t;
258 #endif
259
260 nh = (const struct nfhdr_v5 *)cp;
261 ND_TCHECK_SIZE(nh);
262
263 ver = EXTRACT_BE_U_2(nh->version);
264 nrecs = EXTRACT_BE_U_4(nh->count);
265 #if 0
266 /*
267 * This is seconds since the UN*X epoch, and is followed by
268 * nanoseconds. XXX - format it, rather than just dumping the
269 * raw seconds-since-the-Epoch.
270 */
271 t = EXTRACT_BE_U_4(nh->utc_sec);
272 #endif
273
274 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
275 EXTRACT_BE_U_4(nh->msys_uptime)/1000,
276 EXTRACT_BE_U_4(nh->msys_uptime)%1000,
277 EXTRACT_BE_U_4(nh->utc_sec), EXTRACT_BE_U_4(nh->utc_nsec));
278
279 ND_PRINT("#%u, ", EXTRACT_BE_U_4(nh->sequence));
280 nr = (const struct nfrec_v5 *)&nh[1];
281
282 ND_PRINT("%2u recs", nrecs);
283
284 for (; nrecs != 0; nr++, nrecs--) {
285 char buf[20];
286 char asbuf[20];
287
288 /*
289 * Make sure we have the entire record.
290 */
291 ND_TCHECK_SIZE(nr);
292 ND_PRINT("\n started %u.%03u, last %u.%03u",
293 EXTRACT_BE_U_4(nr->start_time)/1000,
294 EXTRACT_BE_U_4(nr->start_time)%1000,
295 EXTRACT_BE_U_4(nr->last_time)/1000,
296 EXTRACT_BE_U_4(nr->last_time)%1000);
297
298 asbuf[0] = buf[0] = '\0';
299 snprintf(buf, sizeof(buf), "/%u", EXTRACT_U_1(nr->src_mask));
300 snprintf(asbuf, sizeof(asbuf), ":%u",
301 EXTRACT_BE_U_2(nr->src_as));
302 ND_PRINT("\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
303 EXTRACT_BE_U_2(nr->srcport));
304
305 snprintf(buf, sizeof(buf), "/%u", EXTRACT_U_1(nr->dst_mask));
306 snprintf(asbuf, sizeof(asbuf), ":%u",
307 EXTRACT_BE_U_2(nr->dst_as));
308 ND_PRINT("> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
309 EXTRACT_BE_U_2(nr->dstport));
310
311 ND_PRINT(">> %s\n ", intoa(nr->nhop_ina.s_addr));
312
313 proto = EXTRACT_U_1(nr->proto);
314 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
315 ND_PRINT("%s ", p_name);
316 else
317 ND_PRINT("%u ", proto);
318
319 /* tcp flags for tcp only */
320 if (proto == IPPROTO_TCP) {
321 u_int flags;
322 flags = EXTRACT_U_1(nr->tcp_flags);
323 ND_PRINT("%s%s%s%s%s%s%s",
324 flags & TH_FIN ? "F" : "",
325 flags & TH_SYN ? "S" : "",
326 flags & TH_RST ? "R" : "",
327 flags & TH_PUSH ? "P" : "",
328 flags & TH_ACK ? "A" : "",
329 flags & TH_URG ? "U" : "",
330 flags ? " " : "");
331 }
332
333 buf[0]='\0';
334 ND_PRINT("tos %u, %u (%u octets) %s",
335 EXTRACT_U_1(nr->tos),
336 EXTRACT_BE_U_4(nr->packets),
337 EXTRACT_BE_U_4(nr->octets), buf);
338 }
339 return;
340
341 trunc:
342 ND_PRINT("[|cnfp]");
343 return;
344 }
345
346 static void
347 cnfp_v6_print(netdissect_options *ndo, const u_char *cp)
348 {
349 const struct nfhdr_v6 *nh;
350 const struct nfrec_v6 *nr;
351 const char *p_name;
352 uint8_t proto;
353 u_int nrecs, ver;
354 #if 0
355 time_t t;
356 #endif
357
358 nh = (const struct nfhdr_v6 *)cp;
359 ND_TCHECK_SIZE(nh);
360
361 ver = EXTRACT_BE_U_2(nh->version);
362 nrecs = EXTRACT_BE_U_4(nh->count);
363 #if 0
364 /*
365 * This is seconds since the UN*X epoch, and is followed by
366 * nanoseconds. XXX - format it, rather than just dumping the
367 * raw seconds-since-the-Epoch.
368 */
369 t = EXTRACT_BE_U_4(nh->utc_sec);
370 #endif
371
372 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
373 EXTRACT_BE_U_4(nh->msys_uptime)/1000,
374 EXTRACT_BE_U_4(nh->msys_uptime)%1000,
375 EXTRACT_BE_U_4(nh->utc_sec), EXTRACT_BE_U_4(nh->utc_nsec));
376
377 ND_PRINT("#%u, ", EXTRACT_BE_U_4(nh->sequence));
378 nr = (const struct nfrec_v6 *)&nh[1];
379
380 ND_PRINT("%2u recs", nrecs);
381
382 for (; nrecs != 0; nr++, nrecs--) {
383 char buf[20];
384 char asbuf[20];
385
386 /*
387 * Make sure we have the entire record.
388 */
389 ND_TCHECK_SIZE(nr);
390 ND_PRINT("\n started %u.%03u, last %u.%03u",
391 EXTRACT_BE_U_4(nr->start_time)/1000,
392 EXTRACT_BE_U_4(nr->start_time)%1000,
393 EXTRACT_BE_U_4(nr->last_time)/1000,
394 EXTRACT_BE_U_4(nr->last_time)%1000);
395
396 asbuf[0] = buf[0] = '\0';
397 snprintf(buf, sizeof(buf), "/%u", EXTRACT_U_1(nr->src_mask));
398 snprintf(asbuf, sizeof(asbuf), ":%u",
399 EXTRACT_BE_U_2(nr->src_as));
400 ND_PRINT("\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
401 EXTRACT_BE_U_2(nr->srcport));
402
403 snprintf(buf, sizeof(buf), "/%u", EXTRACT_U_1(nr->dst_mask));
404 snprintf(asbuf, sizeof(asbuf), ":%u",
405 EXTRACT_BE_U_2(nr->dst_as));
406 ND_PRINT("> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
407 EXTRACT_BE_U_2(nr->dstport));
408
409 ND_PRINT(">> %s\n ", intoa(nr->nhop_ina.s_addr));
410
411 proto = EXTRACT_U_1(nr->proto);
412 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
413 ND_PRINT("%s ", p_name);
414 else
415 ND_PRINT("%u ", proto);
416
417 /* tcp flags for tcp only */
418 if (proto == IPPROTO_TCP) {
419 u_int flags;
420 flags = EXTRACT_U_1(nr->tcp_flags);
421 ND_PRINT("%s%s%s%s%s%s%s",
422 flags & TH_FIN ? "F" : "",
423 flags & TH_SYN ? "S" : "",
424 flags & TH_RST ? "R" : "",
425 flags & TH_PUSH ? "P" : "",
426 flags & TH_ACK ? "A" : "",
427 flags & TH_URG ? "U" : "",
428 flags ? " " : "");
429 }
430
431 buf[0]='\0';
432 snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
433 (EXTRACT_BE_U_2(nr->flags) >> 8) & 0xff,
434 (EXTRACT_BE_U_2(nr->flags)) & 0xff);
435 ND_PRINT("tos %u, %u (%u octets) %s",
436 EXTRACT_U_1(nr->tos),
437 EXTRACT_BE_U_4(nr->packets),
438 EXTRACT_BE_U_4(nr->octets), buf);
439 }
440 return;
441
442 trunc:
443 ND_PRINT("[|cnfp]");
444 return;
445 }
446
447 void
448 cnfp_print(netdissect_options *ndo, const u_char *cp)
449 {
450 int ver;
451
452 /*
453 * First 2 bytes are the version number.
454 */
455 ND_TCHECK_2(cp);
456 ver = EXTRACT_BE_U_2(cp);
457 switch (ver) {
458
459 case 1:
460 cnfp_v1_print(ndo, cp);
461 break;
462
463 case 5:
464 cnfp_v5_print(ndo, cp);
465 break;
466
467 case 6:
468 cnfp_v6_print(ndo, cp);
469 break;
470
471 default:
472 ND_PRINT("NetFlow v%x", ver);
473 break;
474 }
475 return;
476
477 trunc:
478 ND_PRINT("[|cnfp]");
479 return;
480 }