netflow9.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include "common.h"
  25. #include "log.h"
  26. #include "treetype.h"
  27. #include "softflowd.h"
  28. /* Netflow v.9 */
  29. struct NF9_HEADER {
  30. u_int16_t version, flows;
  31. u_int32_t uptime_ms, time_sec;
  32. u_int32_t package_sequence, source_id;
  33. } __packed;
  34. struct NF9_FLOWSET_HEADER_COMMON {
  35. u_int16_t flowset_id, length;
  36. } __packed;
  37. struct NF9_TEMPLATE_FLOWSET_HEADER {
  38. struct NF9_FLOWSET_HEADER_COMMON c;
  39. u_int16_t template_id, count;
  40. } __packed;
  41. struct NF9_OPTION_TEMPLATE_FLOWSET_HEADER {
  42. struct NF9_FLOWSET_HEADER_COMMON c;
  43. u_int16_t template_id, scope_length, option_length;
  44. } __packed;
  45. struct NF9_TEMPLATE_FLOWSET_RECORD {
  46. u_int16_t type, length;
  47. } __packed;
  48. struct NF9_DATA_FLOWSET_HEADER {
  49. struct NF9_FLOWSET_HEADER_COMMON c;
  50. } __packed;
  51. #define NF9_TEMPLATE_FLOWSET_ID 0
  52. #define NF9_OPTIONS_FLOWSET_ID 1
  53. #define NF9_MIN_RECORD_FLOWSET_ID 256
  54. /* Flowset record types the we care about */
  55. #define NF9_IN_BYTES 1
  56. #define NF9_IN_PACKETS 2
  57. /* ... */
  58. #define NF9_IN_PROTOCOL 4
  59. /* ... */
  60. #define NF9_TCP_FLAGS 6
  61. #define NF9_L4_SRC_PORT 7
  62. #define NF9_IPV4_SRC_ADDR 8
  63. /* ... */
  64. #define NF9_IF_INDEX_IN 10
  65. #define NF9_L4_DST_PORT 11
  66. #define NF9_IPV4_DST_ADDR 12
  67. /* ... */
  68. #define NF9_IF_INDEX_OUT 14
  69. /* ... */
  70. #define NF9_LAST_SWITCHED 21
  71. #define NF9_FIRST_SWITCHED 22
  72. /* ... */
  73. #define NF9_IPV6_SRC_ADDR 27
  74. #define NF9_IPV6_DST_ADDR 28
  75. /* ... */
  76. #define NF9_SAMPLING_INTERVAL 34
  77. #define NF9_SAMPLING_ALGORITHM 35
  78. /* ... */
  79. #define NF9_IP_PROTOCOL_VERSION 60
  80. /* Stuff pertaining to the templates that softflowd uses */
  81. #define NF9_SOFTFLOWD_TEMPLATE_NRECORDS 13
  82. struct NF9_SOFTFLOWD_TEMPLATE {
  83. struct NF9_TEMPLATE_FLOWSET_HEADER h;
  84. struct NF9_TEMPLATE_FLOWSET_RECORD r[NF9_SOFTFLOWD_TEMPLATE_NRECORDS];
  85. } __packed;
  86. #define NF9_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS 1
  87. #define NF9_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS 2
  88. struct NF9_SOFTFLOWD_OPTION_TEMPLATE {
  89. struct NF9_OPTION_TEMPLATE_FLOWSET_HEADER h;
  90. struct NF9_TEMPLATE_FLOWSET_RECORD s[NF9_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS];
  91. struct NF9_TEMPLATE_FLOWSET_RECORD r[NF9_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS];
  92. } __packed;
  93. /* softflowd data flowset types */
  94. struct NF9_SOFTFLOWD_DATA_COMMON {
  95. u_int32_t last_switched, first_switched;
  96. u_int32_t bytes, packets;
  97. u_int32_t if_index_in, if_index_out;
  98. u_int16_t src_port, dst_port;
  99. u_int8_t protocol, tcp_flags, ipproto;
  100. } __packed;
  101. struct NF9_SOFTFLOWD_DATA_V4 {
  102. u_int32_t src_addr, dst_addr;
  103. struct NF9_SOFTFLOWD_DATA_COMMON c;
  104. } __packed;
  105. struct NF9_SOFTFLOWD_DATA_V6 {
  106. u_int8_t src_addr[16], dst_addr[16];
  107. struct NF9_SOFTFLOWD_DATA_COMMON c;
  108. } __packed;
  109. struct NF9_SOFTFLOWD_OPTION_DATA {
  110. struct NF9_FLOWSET_HEADER_COMMON c;
  111. u_int32_t scope_ifidx;
  112. u_int32_t sampling_interval;
  113. u_int8_t sampling_algorithm;
  114. u_int8_t padding[3];
  115. } __packed;
  116. /* Local data: templates and counters */
  117. #define NF9_SOFTFLOWD_MAX_PACKET_SIZE 512
  118. #define NF9_SOFTFLOWD_V4_TEMPLATE_ID 1024
  119. #define NF9_SOFTFLOWD_V6_TEMPLATE_ID 2048
  120. #define NF9_SOFTFLOWD_OPTION_TEMPLATE_ID 256
  121. #define NF9_DEFAULT_TEMPLATE_INTERVAL 16
  122. /* ... */
  123. #define NF9_OPTION_SCOPE_SYSTEM 1
  124. #define NF9_OPTION_SCOPE_INTERFACE 2
  125. #define NF9_OPTION_SCOPE_LINECARD 3
  126. #define NF9_OPTION_SCOPE_CACHE 4
  127. #define NF9_OPTION_SCOPE_TEMPLATE 5
  128. /* ... */
  129. #define NF9_SAMPLING_ALGORITHM_DETERMINISTIC 1
  130. #define NF9_SAMPLING_ALGORITHM_RANDOM 2
  131. /* ... */
  132. static struct NF9_SOFTFLOWD_TEMPLATE v4_template;
  133. static struct NF9_SOFTFLOWD_TEMPLATE v6_template;
  134. static struct NF9_SOFTFLOWD_OPTION_TEMPLATE option_template;
  135. static struct NF9_SOFTFLOWD_OPTION_DATA option_data;
  136. static int nf9_pkts_until_template = -1;
  137. static void
  138. nf9_init_template(void)
  139. {
  140. bzero(&v4_template, sizeof(v4_template));
  141. v4_template.h.c.flowset_id = htons(NF9_TEMPLATE_FLOWSET_ID);
  142. v4_template.h.c.length = htons(sizeof(v4_template));
  143. v4_template.h.template_id = htons(NF9_SOFTFLOWD_V4_TEMPLATE_ID);
  144. v4_template.h.count = htons(NF9_SOFTFLOWD_TEMPLATE_NRECORDS);
  145. v4_template.r[0].type = htons(NF9_IPV4_SRC_ADDR);
  146. v4_template.r[0].length = htons(4);
  147. v4_template.r[1].type = htons(NF9_IPV4_DST_ADDR);
  148. v4_template.r[1].length = htons(4);
  149. v4_template.r[2].type = htons(NF9_LAST_SWITCHED);
  150. v4_template.r[2].length = htons(4);
  151. v4_template.r[3].type = htons(NF9_FIRST_SWITCHED);
  152. v4_template.r[3].length = htons(4);
  153. v4_template.r[4].type = htons(NF9_IN_BYTES);
  154. v4_template.r[4].length = htons(4);
  155. v4_template.r[5].type = htons(NF9_IN_PACKETS);
  156. v4_template.r[5].length = htons(4);
  157. v4_template.r[6].type = htons(NF9_IF_INDEX_IN);
  158. v4_template.r[6].length = htons(4);
  159. v4_template.r[7].type = htons(NF9_IF_INDEX_OUT);
  160. v4_template.r[7].length = htons(4);
  161. v4_template.r[8].type = htons(NF9_L4_SRC_PORT);
  162. v4_template.r[8].length = htons(2);
  163. v4_template.r[9].type = htons(NF9_L4_DST_PORT);
  164. v4_template.r[9].length = htons(2);
  165. v4_template.r[10].type = htons(NF9_IN_PROTOCOL);
  166. v4_template.r[10].length = htons(1);
  167. v4_template.r[11].type = htons(NF9_TCP_FLAGS);
  168. v4_template.r[11].length = htons(1);
  169. v4_template.r[12].type = htons(NF9_IP_PROTOCOL_VERSION);
  170. v4_template.r[12].length = htons(1);
  171. bzero(&v6_template, sizeof(v6_template));
  172. v6_template.h.c.flowset_id = htons(NF9_TEMPLATE_FLOWSET_ID);
  173. v6_template.h.c.length = htons(sizeof(v6_template));
  174. v6_template.h.template_id = htons(NF9_SOFTFLOWD_V6_TEMPLATE_ID);
  175. v6_template.h.count = htons(NF9_SOFTFLOWD_TEMPLATE_NRECORDS);
  176. v6_template.r[0].type = htons(NF9_IPV6_SRC_ADDR);
  177. v6_template.r[0].length = htons(16);
  178. v6_template.r[1].type = htons(NF9_IPV6_DST_ADDR);
  179. v6_template.r[1].length = htons(16);
  180. v6_template.r[2].type = htons(NF9_LAST_SWITCHED);
  181. v6_template.r[2].length = htons(4);
  182. v6_template.r[3].type = htons(NF9_FIRST_SWITCHED);
  183. v6_template.r[3].length = htons(4);
  184. v6_template.r[4].type = htons(NF9_IN_BYTES);
  185. v6_template.r[4].length = htons(4);
  186. v6_template.r[5].type = htons(NF9_IN_PACKETS);
  187. v6_template.r[5].length = htons(4);
  188. v6_template.r[6].type = htons(NF9_IF_INDEX_IN);
  189. v6_template.r[6].length = htons(4);
  190. v6_template.r[7].type = htons(NF9_IF_INDEX_OUT);
  191. v6_template.r[7].length = htons(4);
  192. v6_template.r[8].type = htons(NF9_L4_SRC_PORT);
  193. v6_template.r[8].length = htons(2);
  194. v6_template.r[9].type = htons(NF9_L4_DST_PORT);
  195. v6_template.r[9].length = htons(2);
  196. v6_template.r[10].type = htons(NF9_IN_PROTOCOL);
  197. v6_template.r[10].length = htons(1);
  198. v6_template.r[11].type = htons(NF9_TCP_FLAGS);
  199. v6_template.r[11].length = htons(1);
  200. v6_template.r[12].type = htons(NF9_IP_PROTOCOL_VERSION);
  201. v6_template.r[12].length = htons(1);
  202. }
  203. static void
  204. nf9_init_option(u_int16_t ifidx, struct OPTION *option) {
  205. bzero(&option_template, sizeof(option_template));
  206. option_template.h.c.flowset_id = htons(NF9_OPTIONS_FLOWSET_ID);
  207. option_template.h.c.length = htons(sizeof(option_template));
  208. option_template.h.template_id = htons(NF9_SOFTFLOWD_OPTION_TEMPLATE_ID);
  209. option_template.h.scope_length = htons(sizeof(option_template.s));
  210. option_template.h.option_length = htons(sizeof(option_template.r));
  211. option_template.s[0].type = htons(NF9_OPTION_SCOPE_INTERFACE);
  212. option_template.s[0].length = htons(sizeof(option_data.scope_ifidx));
  213. option_template.r[0].type = htons(NF9_SAMPLING_INTERVAL);
  214. option_template.r[0].length = htons(sizeof(option_data.sampling_interval));
  215. option_template.r[1].type = htons(NF9_SAMPLING_ALGORITHM);
  216. option_template.r[1].length = htons(sizeof(option_data.sampling_algorithm));
  217. bzero(&option_data, sizeof(option_data));
  218. option_data.c.flowset_id = htons(NF9_SOFTFLOWD_OPTION_TEMPLATE_ID);
  219. option_data.c.length = htons(sizeof(option_data));
  220. option_data.scope_ifidx = htonl(ifidx);
  221. option_data.sampling_interval = htonl(option->sample);
  222. option_data.sampling_algorithm = NF9_SAMPLING_ALGORITHM_DETERMINISTIC;
  223. }
  224. static int
  225. nf_flow_to_flowset(const struct FLOW *flow, u_char *packet, u_int len,
  226. u_int16_t ifidx, const struct timeval *system_boot_time, u_int *len_used)
  227. {
  228. union {
  229. struct NF9_SOFTFLOWD_DATA_V4 d4;
  230. struct NF9_SOFTFLOWD_DATA_V6 d6;
  231. } d[2];
  232. struct NF9_SOFTFLOWD_DATA_COMMON *dc[2];
  233. u_int freclen, ret_len, nflows;
  234. bzero(d, sizeof(d));
  235. *len_used = nflows = ret_len = 0;
  236. switch (flow->af) {
  237. case AF_INET:
  238. freclen = sizeof(struct NF9_SOFTFLOWD_DATA_V4);
  239. memcpy(&d[0].d4.src_addr, &flow->addr[0].v4, 4);
  240. memcpy(&d[0].d4.dst_addr, &flow->addr[1].v4, 4);
  241. memcpy(&d[1].d4.src_addr, &flow->addr[1].v4, 4);
  242. memcpy(&d[1].d4.dst_addr, &flow->addr[0].v4, 4);
  243. dc[0] = &d[0].d4.c;
  244. dc[1] = &d[1].d4.c;
  245. dc[0]->ipproto = dc[1]->ipproto = 4;
  246. break;
  247. case AF_INET6:
  248. freclen = sizeof(struct NF9_SOFTFLOWD_DATA_V6);
  249. memcpy(&d[0].d6.src_addr, &flow->addr[0].v6, 16);
  250. memcpy(&d[0].d6.dst_addr, &flow->addr[1].v6, 16);
  251. memcpy(&d[1].d6.src_addr, &flow->addr[1].v6, 16);
  252. memcpy(&d[1].d6.dst_addr, &flow->addr[0].v6, 16);
  253. dc[0] = &d[0].d6.c;
  254. dc[1] = &d[1].d6.c;
  255. dc[0]->ipproto = dc[1]->ipproto = 6;
  256. break;
  257. default:
  258. return (-1);
  259. }
  260. dc[0]->first_switched = dc[1]->first_switched =
  261. htonl(timeval_sub_ms(&flow->flow_start, system_boot_time));
  262. dc[0]->last_switched = dc[1]->last_switched =
  263. htonl(timeval_sub_ms(&flow->flow_last, system_boot_time));
  264. dc[0]->bytes = htonl(flow->octets[0]);
  265. dc[1]->bytes = htonl(flow->octets[1]);
  266. dc[0]->packets = htonl(flow->packets[0]);
  267. dc[1]->packets = htonl(flow->packets[1]);
  268. dc[0]->if_index_in = dc[0]->if_index_out = htonl(ifidx);
  269. dc[1]->if_index_in = dc[1]->if_index_out = htonl(ifidx);
  270. dc[0]->src_port = dc[1]->dst_port = flow->port[0];
  271. dc[1]->src_port = dc[0]->dst_port = flow->port[1];
  272. dc[0]->protocol = dc[1]->protocol = flow->protocol;
  273. dc[0]->tcp_flags = flow->tcp_flags[0];
  274. dc[1]->tcp_flags = flow->tcp_flags[1];
  275. if (flow->octets[0] > 0) {
  276. if (ret_len + freclen > len)
  277. return (-1);
  278. memcpy(packet + ret_len, &d[0], freclen);
  279. ret_len += freclen;
  280. nflows++;
  281. }
  282. if (flow->octets[1] > 0) {
  283. if (ret_len + freclen > len)
  284. return (-1);
  285. memcpy(packet + ret_len, &d[1], freclen);
  286. ret_len += freclen;
  287. nflows++;
  288. }
  289. *len_used = ret_len;
  290. return (nflows);
  291. }
  292. /*
  293. * Given an array of expired flows, send netflow v9 report packets
  294. * Returns number of packets sent or -1 on error
  295. */
  296. int
  297. send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock, u_int16_t ifidx,
  298. u_int64_t *flows_exported, struct timeval *system_boot_time,
  299. int verbose_flag, struct OPTION *option)
  300. {
  301. struct NF9_HEADER *nf9;
  302. struct NF9_DATA_FLOWSET_HEADER *dh;
  303. struct timeval now;
  304. u_int offset, last_af, i, j, num_packets, inc, last_valid;
  305. socklen_t errsz;
  306. int err, r;
  307. u_char packet[NF9_SOFTFLOWD_MAX_PACKET_SIZE];
  308. gettimeofday(&now, NULL);
  309. if (nf9_pkts_until_template == -1) {
  310. nf9_init_template();
  311. nf9_pkts_until_template = 0;
  312. if (option != NULL && option->sample > 1){
  313. nf9_init_option(ifidx, option);
  314. }
  315. }
  316. last_valid = num_packets = 0;
  317. for (j = 0; j < num_flows;) {
  318. bzero(packet, sizeof(packet));
  319. nf9 = (struct NF9_HEADER *)packet;
  320. nf9->version = htons(9);
  321. nf9->flows = 0; /* Filled as we go, htons at end */
  322. nf9->uptime_ms = htonl(timeval_sub_ms(&now, system_boot_time));
  323. nf9->time_sec = htonl(time(NULL));
  324. nf9->package_sequence = htonl(*flows_exported + j);
  325. nf9->source_id = 0;
  326. offset = sizeof(*nf9);
  327. /* Refresh template headers if we need to */
  328. if (nf9_pkts_until_template <= 0) {
  329. memcpy(packet + offset, &v4_template,
  330. sizeof(v4_template));
  331. offset += sizeof(v4_template);
  332. memcpy(packet + offset, &v6_template,
  333. sizeof(v6_template));
  334. offset += sizeof(v6_template);
  335. if (option != NULL && option->sample > 1){
  336. memcpy(packet + offset, &option_template,
  337. sizeof(option_template));
  338. offset += sizeof(option_template);
  339. memcpy(packet + offset, &option_data,
  340. sizeof(option_data));
  341. offset += sizeof(option_data);
  342. }
  343. nf9_pkts_until_template = NF9_DEFAULT_TEMPLATE_INTERVAL;
  344. }
  345. dh = NULL;
  346. last_af = 0;
  347. for (i = 0; i + j < num_flows; i++) {
  348. if (dh == NULL || flows[i + j]->af != last_af) {
  349. if (dh != NULL) {
  350. if (offset % 4 != 0) {
  351. /* Pad to multiple of 4 */
  352. dh->c.length += 4 - (offset % 4);
  353. offset += 4 - (offset % 4);
  354. }
  355. /* Finalise last header */
  356. dh->c.length = htons(dh->c.length);
  357. }
  358. if (offset + sizeof(*dh) > sizeof(packet)) {
  359. /* Mark header is finished */
  360. dh = NULL;
  361. break;
  362. }
  363. dh = (struct NF9_DATA_FLOWSET_HEADER *)
  364. (packet + offset);
  365. dh->c.flowset_id =
  366. (flows[i + j]->af == AF_INET) ?
  367. v4_template.h.template_id :
  368. v6_template.h.template_id;
  369. last_af = flows[i + j]->af;
  370. last_valid = offset;
  371. dh->c.length = sizeof(*dh); /* Filled as we go */
  372. offset += sizeof(*dh);
  373. }
  374. r = nf_flow_to_flowset(flows[i + j], packet + offset,
  375. sizeof(packet) - offset, ifidx, system_boot_time, &inc);
  376. if (r <= 0) {
  377. /* yank off data header, if we had to go back */
  378. if (last_valid)
  379. offset = last_valid;
  380. break;
  381. }
  382. offset += inc;
  383. dh->c.length += inc;
  384. nf9->flows += r;
  385. last_valid = 0; /* Don't clobber this header now */
  386. if (verbose_flag) {
  387. logit(LOG_DEBUG, "Flow %d/%d: "
  388. "r %d offset %d type %04x len %d(0x%04x) "
  389. "flows %d", r, i, j, offset,
  390. dh->c.flowset_id, dh->c.length,
  391. dh->c.length, nf9->flows);
  392. }
  393. }
  394. /* Don't finish header if it has already been done */
  395. if (dh != NULL) {
  396. if (offset % 4 != 0) {
  397. /* Pad to multiple of 4 */
  398. dh->c.length += 4 - (offset % 4);
  399. offset += 4 - (offset % 4);
  400. }
  401. /* Finalise last header */
  402. dh->c.length = htons(dh->c.length);
  403. }
  404. nf9->flows = htons(nf9->flows);
  405. if (verbose_flag)
  406. logit(LOG_DEBUG, "Sending flow packet len = %d", offset);
  407. errsz = sizeof(err);
  408. /* Clear ICMP errors */
  409. getsockopt(nfsock, SOL_SOCKET, SO_ERROR, &err, &errsz);
  410. if (send(nfsock, packet, (size_t)offset, 0) == -1)
  411. return (-1);
  412. num_packets++;
  413. nf9_pkts_until_template--;
  414. j += i;
  415. }
  416. *flows_exported += j;
  417. return (num_packets);
  418. }
  419. void
  420. netflow9_resend_template(void)
  421. {
  422. if (nf9_pkts_until_template > 0)
  423. nf9_pkts_until_template = 0;
  424. }