netflow9.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright 2002 Damien Miller <djm@mindrot.org> All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* $Id: netflow9.c,v 1.9 2006/03/16 08:23:13 djm Exp $ */
  25. #include "common.h"
  26. #include "log.h"
  27. #include "treetype.h"
  28. #include "softflowd.h"
  29. RCSID("$Id: netflow9.c,v 1.9 2006/03/16 08:23:13 djm Exp $");
  30. /* Netflow v.9 */
  31. struct NF9_HEADER {
  32. u_int16_t version, flows;
  33. u_int32_t uptime_ms, time_sec;
  34. u_int32_t package_sequence, source_id;
  35. } __packed;
  36. struct NF9_FLOWSET_HEADER_COMMON {
  37. u_int16_t flowset_id, length;
  38. } __packed;
  39. struct NF9_TEMPLATE_FLOWSET_HEADER {
  40. struct NF9_FLOWSET_HEADER_COMMON c;
  41. u_int16_t template_id, count;
  42. } __packed;
  43. struct NF9_TEMPLATE_FLOWSET_RECORD {
  44. u_int16_t type, length;
  45. } __packed;
  46. struct NF9_DATA_FLOWSET_HEADER {
  47. struct NF9_FLOWSET_HEADER_COMMON c;
  48. } __packed;
  49. #define NF9_TEMPLATE_FLOWSET_ID 0
  50. #define NF9_OPTIONS_FLOWSET_ID 1
  51. #define NF9_MIN_RECORD_FLOWSET_ID 256
  52. /* Flowset record types the we care about */
  53. #define NF9_IN_BYTES 1
  54. #define NF9_IN_PACKETS 2
  55. /* ... */
  56. #define NF9_IN_PROTOCOL 4
  57. /* ... */
  58. #define NF9_TCP_FLAGS 6
  59. #define NF9_L4_SRC_PORT 7
  60. #define NF9_IPV4_SRC_ADDR 8
  61. /* ... */
  62. #define NF9_L4_DST_PORT 11
  63. #define NF9_IPV4_DST_ADDR 12
  64. /* ... */
  65. #define NF9_LAST_SWITCHED 21
  66. #define NF9_FIRST_SWITCHED 22
  67. /* ... */
  68. #define NF9_IPV6_SRC_ADDR 27
  69. #define NF9_IPV6_DST_ADDR 28
  70. /* ... */
  71. #define NF9_IP_PROTOCOL_VERSION 60
  72. /* Stuff pertaining to the templates that softflowd uses */
  73. #define NF9_SOFTFLOWD_TEMPLATE_NRECORDS 11
  74. struct NF9_SOFTFLOWD_TEMPLATE {
  75. struct NF9_TEMPLATE_FLOWSET_HEADER h;
  76. struct NF9_TEMPLATE_FLOWSET_RECORD r[NF9_SOFTFLOWD_TEMPLATE_NRECORDS];
  77. } __packed;
  78. /* softflowd data flowset types */
  79. struct NF9_SOFTFLOWD_DATA_COMMON {
  80. u_int32_t first_switched, last_switched;
  81. u_int32_t bytes, packets;
  82. u_int16_t src_port, dst_port;
  83. u_int8_t protocol, tcp_flags, ipproto;
  84. } __packed;
  85. struct NF9_SOFTFLOWD_DATA_V4 {
  86. u_int32_t src_addr, dst_addr;
  87. struct NF9_SOFTFLOWD_DATA_COMMON c;
  88. } __packed;
  89. struct NF9_SOFTFLOWD_DATA_V6 {
  90. u_int8_t src_addr[16], dst_addr[16];
  91. struct NF9_SOFTFLOWD_DATA_COMMON c;
  92. } __packed;
  93. /* Local data: templates and counters */
  94. #define NF9_SOFTFLOWD_MAX_PACKET_SIZE 512
  95. #define NF9_SOFTFLOWD_V4_TEMPLATE_ID 1024
  96. #define NF9_SOFTFLOWD_V6_TEMPLATE_ID 2048
  97. #define NF9_DEFAULT_TEMPLATE_INTERVAL 16
  98. static struct NF9_SOFTFLOWD_TEMPLATE v4_template;
  99. static struct NF9_SOFTFLOWD_TEMPLATE v6_template;
  100. static int nf9_pkts_until_template = -1;
  101. static void
  102. nf9_init_template(void)
  103. {
  104. bzero(&v4_template, sizeof(v4_template));
  105. v4_template.h.c.flowset_id = htons(0);
  106. v4_template.h.c.length = htons(sizeof(v4_template));
  107. v4_template.h.template_id = htons(NF9_SOFTFLOWD_V4_TEMPLATE_ID);
  108. v4_template.h.count = htons(NF9_SOFTFLOWD_TEMPLATE_NRECORDS);
  109. v4_template.r[0].type = htons(NF9_IPV4_SRC_ADDR);
  110. v4_template.r[0].length = htons(4);
  111. v4_template.r[1].type = htons(NF9_IPV4_DST_ADDR);
  112. v4_template.r[1].length = htons(4);
  113. v4_template.r[2].type = htons(NF9_LAST_SWITCHED);
  114. v4_template.r[2].length = htons(4);
  115. v4_template.r[3].type = htons(NF9_FIRST_SWITCHED);
  116. v4_template.r[3].length = htons(4);
  117. v4_template.r[4].type = htons(NF9_IN_BYTES);
  118. v4_template.r[4].length = htons(4);
  119. v4_template.r[5].type = htons(NF9_IN_PACKETS);
  120. v4_template.r[5].length = htons(4);
  121. v4_template.r[6].type = htons(NF9_L4_SRC_PORT);
  122. v4_template.r[6].length = htons(2);
  123. v4_template.r[7].type = htons(NF9_L4_DST_PORT);
  124. v4_template.r[7].length = htons(2);
  125. v4_template.r[8].type = htons(NF9_IN_PROTOCOL);
  126. v4_template.r[8].length = htons(1);
  127. v4_template.r[9].type = htons(NF9_TCP_FLAGS);
  128. v4_template.r[9].length = htons(1);
  129. v4_template.r[10].type = htons(NF9_IP_PROTOCOL_VERSION);
  130. v4_template.r[10].length = htons(1);
  131. bzero(&v6_template, sizeof(v6_template));
  132. v6_template.h.c.flowset_id = htons(0);
  133. v6_template.h.c.length = htons(sizeof(v6_template));
  134. v6_template.h.template_id = htons(NF9_SOFTFLOWD_V6_TEMPLATE_ID);
  135. v6_template.h.count = htons(NF9_SOFTFLOWD_TEMPLATE_NRECORDS);
  136. v6_template.r[0].type = htons(NF9_IPV6_SRC_ADDR);
  137. v6_template.r[0].length = htons(16);
  138. v6_template.r[1].type = htons(NF9_IPV6_DST_ADDR);
  139. v6_template.r[1].length = htons(16);
  140. v6_template.r[2].type = htons(NF9_LAST_SWITCHED);
  141. v6_template.r[2].length = htons(4);
  142. v6_template.r[3].type = htons(NF9_FIRST_SWITCHED);
  143. v6_template.r[3].length = htons(4);
  144. v6_template.r[4].type = htons(NF9_IN_BYTES);
  145. v6_template.r[4].length = htons(4);
  146. v6_template.r[5].type = htons(NF9_IN_PACKETS);
  147. v6_template.r[5].length = htons(4);
  148. v6_template.r[6].type = htons(NF9_L4_SRC_PORT);
  149. v6_template.r[6].length = htons(2);
  150. v6_template.r[7].type = htons(NF9_L4_DST_PORT);
  151. v6_template.r[7].length = htons(2);
  152. v6_template.r[8].type = htons(NF9_IN_PROTOCOL);
  153. v6_template.r[8].length = htons(1);
  154. v6_template.r[9].type = htons(NF9_TCP_FLAGS);
  155. v6_template.r[9].length = htons(1);
  156. v6_template.r[10].type = htons(NF9_IP_PROTOCOL_VERSION);
  157. v6_template.r[10].length = htons(1);
  158. }
  159. static int
  160. nf_flow_to_flowset(const struct FLOW *flow, u_char *packet, u_int len,
  161. const struct timeval *system_boot_time, u_int *len_used)
  162. {
  163. union {
  164. struct NF9_SOFTFLOWD_DATA_V4 d4;
  165. struct NF9_SOFTFLOWD_DATA_V6 d6;
  166. } d[2];
  167. struct NF9_SOFTFLOWD_DATA_COMMON *dc[2];
  168. u_int freclen, ret_len, nflows;
  169. bzero(d, sizeof(d));
  170. *len_used = nflows = ret_len = 0;
  171. switch (flow->af) {
  172. case AF_INET:
  173. freclen = sizeof(struct NF9_SOFTFLOWD_DATA_V4);
  174. memcpy(&d[0].d4.src_addr, &flow->addr[0].v4, 4);
  175. memcpy(&d[0].d4.dst_addr, &flow->addr[1].v4, 4);
  176. memcpy(&d[1].d4.src_addr, &flow->addr[1].v4, 4);
  177. memcpy(&d[1].d4.dst_addr, &flow->addr[0].v4, 4);
  178. dc[0] = &d[0].d4.c;
  179. dc[1] = &d[1].d4.c;
  180. dc[0]->ipproto = dc[1]->ipproto = 4;
  181. break;
  182. case AF_INET6:
  183. freclen = sizeof(struct NF9_SOFTFLOWD_DATA_V6);
  184. memcpy(&d[0].d6.src_addr, &flow->addr[0].v6, 16);
  185. memcpy(&d[0].d6.dst_addr, &flow->addr[1].v6, 16);
  186. memcpy(&d[1].d6.src_addr, &flow->addr[1].v6, 16);
  187. memcpy(&d[1].d6.dst_addr, &flow->addr[0].v6, 16);
  188. dc[0] = &d[0].d6.c;
  189. dc[1] = &d[1].d6.c;
  190. dc[0]->ipproto = dc[1]->ipproto = 6;
  191. break;
  192. default:
  193. return (-1);
  194. }
  195. dc[0]->first_switched = dc[1]->first_switched =
  196. htonl(timeval_sub_ms(&flow->flow_start, system_boot_time));
  197. dc[0]->last_switched = dc[1]->last_switched =
  198. htonl(timeval_sub_ms(&flow->flow_last, system_boot_time));
  199. dc[0]->bytes = htonl(flow->octets[0]);
  200. dc[1]->bytes = htonl(flow->octets[1]);
  201. dc[0]->packets = htonl(flow->packets[0]);
  202. dc[1]->packets = htonl(flow->packets[1]);
  203. dc[0]->src_port = dc[1]->dst_port = flow->port[0];
  204. dc[1]->src_port = dc[0]->dst_port = flow->port[1];
  205. dc[0]->protocol = dc[1]->protocol = flow->protocol;
  206. dc[0]->tcp_flags = flow->tcp_flags[0];
  207. dc[1]->tcp_flags = flow->tcp_flags[1];
  208. if (flow->octets[0] > 0) {
  209. if (ret_len + freclen > len)
  210. return (-1);
  211. memcpy(packet + ret_len, &d[0], freclen);
  212. ret_len += freclen;
  213. nflows++;
  214. }
  215. if (flow->octets[1] > 0) {
  216. if (ret_len + freclen > len)
  217. return (-1);
  218. memcpy(packet + ret_len, &d[1], freclen);
  219. ret_len += freclen;
  220. nflows++;
  221. }
  222. *len_used = ret_len;
  223. return (nflows);
  224. }
  225. /*
  226. * Given an array of expired flows, send netflow v9 report packets
  227. * Returns number of packets sent or -1 on error
  228. */
  229. int
  230. send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock,
  231. u_int64_t *flows_exported, struct timeval *system_boot_time,
  232. int verbose_flag)
  233. {
  234. struct NF9_HEADER *nf9;
  235. struct NF9_DATA_FLOWSET_HEADER *dh;
  236. struct timeval now;
  237. u_int offset, last_af, i, j, num_packets, inc, last_valid;
  238. socklen_t errsz;
  239. int err, r;
  240. u_char packet[NF9_SOFTFLOWD_MAX_PACKET_SIZE];
  241. gettimeofday(&now, NULL);
  242. if (nf9_pkts_until_template == -1) {
  243. nf9_init_template();
  244. nf9_pkts_until_template = 0;
  245. }
  246. last_valid = num_packets = 0;
  247. for (j = 0; j < num_flows;) {
  248. bzero(packet, sizeof(packet));
  249. nf9 = (struct NF9_HEADER *)packet;
  250. nf9->version = htons(9);
  251. nf9->flows = 0; /* Filled as we go, htons at end */
  252. nf9->uptime_ms = htonl(timeval_sub_ms(&now, system_boot_time));
  253. nf9->time_sec = htonl(time(NULL));
  254. nf9->package_sequence = htonl(*flows_exported + j);
  255. nf9->source_id = 0;
  256. offset = sizeof(*nf9);
  257. /* Refresh template headers if we need to */
  258. if (nf9_pkts_until_template <= 0) {
  259. memcpy(packet + offset, &v4_template,
  260. sizeof(v4_template));
  261. offset += sizeof(v4_template);
  262. memcpy(packet + offset, &v6_template,
  263. sizeof(v6_template));
  264. offset += sizeof(v6_template);
  265. nf9_pkts_until_template = NF9_DEFAULT_TEMPLATE_INTERVAL;
  266. }
  267. dh = NULL;
  268. last_af = 0;
  269. for (i = 0; i + j < num_flows; i++) {
  270. if (dh == NULL || flows[i + j]->af != last_af) {
  271. if (dh != NULL) {
  272. if (offset % 4 != 0) {
  273. /* Pad to multiple of 4 */
  274. dh->c.length += 4 - (offset % 4);
  275. offset += 4 - (offset % 4);
  276. }
  277. /* Finalise last header */
  278. dh->c.length = htons(dh->c.length);
  279. }
  280. if (offset + sizeof(*dh) > sizeof(packet)) {
  281. /* Mark header is finished */
  282. dh = NULL;
  283. break;
  284. }
  285. dh = (struct NF9_DATA_FLOWSET_HEADER *)
  286. (packet + offset);
  287. dh->c.flowset_id =
  288. (flows[i + j]->af == AF_INET) ?
  289. v4_template.h.template_id :
  290. v6_template.h.template_id;
  291. last_af = flows[i + j]->af;
  292. last_valid = offset;
  293. dh->c.length = sizeof(*dh); /* Filled as we go */
  294. offset += sizeof(*dh);
  295. }
  296. r = nf_flow_to_flowset(flows[i + j], packet + offset,
  297. sizeof(packet) - offset, system_boot_time, &inc);
  298. if (r <= 0) {
  299. /* yank off data header, if we had to go back */
  300. if (last_valid)
  301. offset = last_valid;
  302. break;
  303. }
  304. offset += inc;
  305. dh->c.length += inc;
  306. nf9->flows += r;
  307. last_valid = 0; /* Don't clobber this header now */
  308. if (verbose_flag) {
  309. logit(LOG_DEBUG, "Flow %d/%d: "
  310. "r %d offset %d type %04x len %d(0x%04x) "
  311. "flows %d", r, i, j, offset,
  312. dh->c.flowset_id, dh->c.length,
  313. dh->c.length, nf9->flows);
  314. }
  315. }
  316. /* Don't finish header if it has already been done */
  317. if (dh != NULL) {
  318. if (offset % 4 != 0) {
  319. /* Pad to multiple of 4 */
  320. dh->c.length += 4 - (offset % 4);
  321. offset += 4 - (offset % 4);
  322. }
  323. /* Finalise last header */
  324. dh->c.length = htons(dh->c.length);
  325. }
  326. nf9->flows = htons(nf9->flows);
  327. if (verbose_flag)
  328. logit(LOG_DEBUG, "Sending flow packet len = %d", offset);
  329. errsz = sizeof(err);
  330. /* Clear ICMP errors */
  331. getsockopt(nfsock, SOL_SOCKET, SO_ERROR, &err, &errsz);
  332. if (send(nfsock, packet, (size_t)offset, 0) == -1)
  333. return (-1);
  334. num_packets++;
  335. nf9_pkts_until_template--;
  336. j += i;
  337. }
  338. *flows_exported += j;
  339. return (num_packets);
  340. }
  341. void
  342. netflow9_resend_template(void)
  343. {
  344. if (nf9_pkts_until_template > 0)
  345. nf9_pkts_until_template = 0;
  346. }