1 /* $OpenBSD: print-cnfp.c,v 1.5 2003/06/02 20:37:14 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* Cisco NetFlow protocol */ 29 30 #include <sys/types.h> 31 #include <sys/time.h> 32 #include <sys/socket.h> 33 #include <netdb.h> 34 35 #include <netinet/in.h> 36 #include <netinet/tcp.h> 37 #include <arpa/inet.h> 38 39 #include <stdio.h> 40 #include <string.h> 41 42 #include "interface.h" 43 44 struct nfhdr { 45 u_int32_t ver_cnt; /* version [15], and # of records */ 46 u_int32_t msys_uptime; 47 u_int32_t utc_sec; 48 u_int32_t utc_nsec; 49 u_int32_t sequence; /* v5 flow sequence number */ 50 u_int32_t reserved; /* v5 only */ 51 }; 52 53 struct nfrec { 54 struct in_addr src_ina; 55 struct in_addr dst_ina; 56 struct in_addr nhop_ina; 57 u_int32_t ifaces; /* src,dst ifaces */ 58 u_int32_t packets; 59 u_int32_t octets; 60 u_int32_t start_time; /* sys_uptime value */ 61 u_int32_t last_time; /* sys_uptime value */ 62 u_int32_t ports; /* src,dst ports */ 63 u_int32_t proto_tos; /* proto, tos, pad, flags(v5) */ 64 u_int32_t asses; /* v1: flags; v5: src,dst AS */ 65 u_int32_t masks; /* src,dst addr prefix */ 66 67 }; 68 69 void 70 cnfp_print(register const u_char *cp, u_int len, register const u_char *bp) 71 { 72 register const struct nfhdr *nh; 73 register const struct nfrec *nr; 74 register const struct ip *ip; 75 struct protoent *pent; 76 int nrecs, ver; 77 time_t t; 78 79 ip = (struct ip *)bp; 80 nh = (struct nfhdr *)cp; 81 82 if ((u_char *)(nh + 1) > snapend) 83 return; 84 85 nrecs = ntohl(nh->ver_cnt) & 0xffff; 86 ver = (ntohl(nh->ver_cnt) & 0xffff0000) >> 16; 87 t = ntohl(nh->utc_sec); 88 /* (p = ctime(&t))[24] = '\0'; */ 89 90 printf("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver, 91 ntohl(nh->msys_uptime)/1000, ntohl(nh->msys_uptime)%1000, 92 ntohl(nh->utc_sec), ntohl(nh->utc_nsec)); 93 94 if (ver == 5) { 95 printf("#%u, ", htonl(nh->sequence)); 96 nr = (struct nfrec *)&nh[1]; 97 snaplen -= 24; 98 } else { 99 nr = (struct nfrec *)&nh->sequence; 100 snaplen -= 16; 101 } 102 103 printf("%2u recs", nrecs); 104 105 for (; nrecs-- && (u_char *)(nr + 1) <= snapend; nr++) { 106 char buf[5]; 107 char asbuf[7]; 108 109 printf("\n started %u.%03u, last %u.%03u", 110 ntohl(nr->start_time)/1000, ntohl(nr->start_time)%1000, 111 ntohl(nr->last_time)/1000, ntohl(nr->last_time)%1000); 112 113 asbuf[0] = buf[0] = '\0'; 114 if (ver == 5) { 115 snprintf(buf, sizeof buf, "/%d", 116 (ntohl(nr->masks) >> 24) & 0xff); 117 snprintf(asbuf, sizeof asbuf, "%d:", 118 (ntohl(nr->asses) >> 16) & 0xffff); 119 } 120 printf("\n %s%s%s:%u ", inet_ntoa(nr->src_ina), buf, asbuf, 121 ntohl(nr->ports) >> 16); 122 123 if (ver == 5) { 124 snprintf(buf, sizeof buf, "/%d", 125 (ntohl(nr->masks) >> 16) & 0xff); 126 snprintf(asbuf, sizeof asbuf, "%d:", 127 ntohl(nr->asses) & 0xffff); 128 } 129 printf("> %s%s%s:%u ", inet_ntoa(nr->dst_ina), buf, asbuf, 130 ntohl(nr->ports) & 0xffff); 131 132 printf(">> %s\n ", inet_ntoa(nr->nhop_ina)); 133 134 pent = getprotobynumber((ntohl(nr->proto_tos) >> 8) & 0xff); 135 if (!pent || nflag) 136 printf("%u ", (ntohl(nr->proto_tos) >> 8) & 0xff); 137 else 138 printf("%s ", pent->p_name); 139 140 /* tcp flags for tcp only */ 141 if (pent && pent->p_proto == IPPROTO_TCP) { 142 int flags; 143 if (ver == 1) 144 flags = (ntohl(nr->asses) >> 24) & 0xff; 145 else 146 flags = (ntohl(nr->proto_tos) >> 16) & 0xff; 147 if (flags & TH_FIN) putchar('F'); 148 if (flags & TH_SYN) putchar('S'); 149 if (flags & TH_RST) putchar('R'); 150 if (flags & TH_PUSH) putchar('P'); 151 if (flags & TH_ACK) putchar('A'); 152 if (flags & TH_URG) putchar('U'); 153 if (flags) 154 putchar(' '); 155 } 156 printf("tos %u, %u (%u octets)", ntohl(nr->proto_tos) & 0xff, 157 ntohl(nr->packets), ntohl(nr->octets)); 158 159 160 } 161 162 } 163 164