print-cnfp.c 14 KB

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