You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

858 lines
21 KiB

  1. From 4f639231c83b09ea004c03e95c702b7750bf9930 Mon Sep 17 00:00:00 2001
  2. From: Ander Juaristi <a@juaristi.eus>
  3. Date: Fri, 26 Apr 2019 09:58:06 +0200
  4. Subject: IPFIX: Add IPFIX output plugin
  5. This patch adds an IPFIX output plugin to ulogd2. It generates NetFlow/IPFIX
  6. traces and sends them to a remote server (collector) via TCP or UDP.
  7. Based on original work by Holger Eitzenberger <holger@eitzenberger.org>.
  8. How to test this
  9. ----------------
  10. I am currently testing this with the NFCT input and Wireshark.
  11. Place the following in ulogd.conf:
  12. # this will print all flows on screen
  13. loglevel=1
  14. # load NFCT and IPFIX plugins
  15. plugin="/lib/ulogd/ulogd_inpflow_NFCT.so"
  16. plugin="/lib/ulogd/ulogd_output_IPFIX.so"
  17. stack=ct1:NFCT,ipfix1:IPFIX
  18. [ct1]
  19. netlink_socket_buffer_size=217088
  20. netlink_socket_buffer_maxsize=1085440
  21. accept_proto_filter=tcp,sctp
  22. [ipfix1]
  23. oid=1
  24. host="127.0.0.1"
  25. #port=4739
  26. #send_template="once"
  27. I am currently testing it by launching a plain NetCat listener on port
  28. 4739 (the default for IPFIX) and then running Wireshark and see that it
  29. dissects the IPFIX/NetFlow traffic correctly (obviously this relies on
  30. the Wireshark NetFlow dissector being correct).
  31. First:
  32. nc -vvvv -l 127.0.0.1 4739
  33. Then:
  34. sudo ulogd -vc ulogd.conf
  35. Signed-off-by: Ander Juaristi <a@juaristi.eus>
  36. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
  37. ---
  38. configure.ac | 2 +-
  39. include/ulogd/ulogd.h | 5 +
  40. input/flow/ulogd_inpflow_IPFIX.c | 2 -
  41. output/Makefile.am | 2 +-
  42. output/ipfix/Makefile.am | 7 +
  43. output/ipfix/ipfix.c | 141 ++++++++++
  44. output/ipfix/ipfix.h | 89 +++++++
  45. output/ipfix/ulogd_output_IPFIX.c | 503 +++++++++++++++++++++++++++++++++++
  46. output/ulogd_output_IPFIX.c | 546 --------------------------------------
  47. 9 files changed, 747 insertions(+), 550 deletions(-)
  48. delete mode 100644 input/flow/ulogd_inpflow_IPFIX.c
  49. create mode 100644 output/ipfix/Makefile.am
  50. create mode 100644 output/ipfix/ipfix.c
  51. create mode 100644 output/ipfix/ipfix.h
  52. create mode 100644 output/ipfix/ulogd_output_IPFIX.c
  53. delete mode 100644 output/ulogd_output_IPFIX.c
  54. --- a/configure.ac
  55. +++ b/configure.ac
  56. @@ -179,7 +179,7 @@ AC_CONFIG_FILES(include/Makefile include
  57. input/sum/Makefile \
  58. filter/Makefile filter/raw2packet/Makefile filter/packet2flow/Makefile \
  59. output/Makefile output/pcap/Makefile output/mysql/Makefile output/pgsql/Makefile output/sqlite3/Makefile \
  60. - output/dbi/Makefile \
  61. + output/dbi/Makefile output/ipfix/Makefile \
  62. src/Makefile Makefile Rules.make)
  63. AC_OUTPUT
  64. --- a/include/ulogd/ulogd.h
  65. +++ b/include/ulogd/ulogd.h
  66. @@ -28,6 +28,11 @@
  67. /* types without length */
  68. #define ULOGD_RET_NONE 0x0000
  69. +#define __packed __attribute__((packed))
  70. +#define __noreturn __attribute__((noreturn))
  71. +#define __cold __attribute__((cold))
  72. +
  73. +#define __packed __attribute__((packed))
  74. #define ULOGD_RET_INT8 0x0001
  75. #define ULOGD_RET_INT16 0x0002
  76. --- a/output/Makefile.am
  77. +++ b/output/Makefile.am
  78. @@ -2,7 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include ${
  79. ${LIBNETFILTER_CONNTRACK_CFLAGS} ${LIBNETFILTER_LOG_CFLAGS}
  80. AM_CFLAGS = ${regular_CFLAGS}
  81. -SUBDIRS= pcap mysql pgsql sqlite3 dbi
  82. +SUBDIRS= pcap mysql pgsql sqlite3 dbi ipfix
  83. pkglib_LTLIBRARIES = ulogd_output_LOGEMU.la ulogd_output_SYSLOG.la \
  84. ulogd_output_OPRINT.la ulogd_output_GPRINT.la \
  85. --- /dev/null
  86. +++ b/output/ipfix/Makefile.am
  87. @@ -0,0 +1,7 @@
  88. +AM_CPPFLAGS = -I$(top_srcdir)/include
  89. +AM_CFLAGS = $(regular_CFLAGS)
  90. +
  91. +pkglib_LTLIBRARIES = ulogd_output_IPFIX.la
  92. +
  93. +ulogd_output_IPFIX_la_SOURCES = ulogd_output_IPFIX.c ipfix.c
  94. +ulogd_output_IPFIX_la_LDFLAGS = -avoid-version -module
  95. --- /dev/null
  96. +++ b/output/ipfix/ipfix.c
  97. @@ -0,0 +1,141 @@
  98. +/*
  99. + * ipfix.c
  100. + *
  101. + * Holger Eitzenberger, 2009.
  102. + */
  103. +
  104. +/* These forward declarations are needed since ulogd.h doesn't like to be the first */
  105. +#include <ulogd/linuxlist.h>
  106. +
  107. +#define __packed __attribute__((packed))
  108. +
  109. +#include "ipfix.h"
  110. +
  111. +#include <ulogd/ulogd.h>
  112. +#include <ulogd/common.h>
  113. +
  114. +struct ipfix_msg *ipfix_msg_alloc(size_t len, uint32_t oid)
  115. +{
  116. + struct ipfix_msg *msg;
  117. + struct ipfix_hdr *hdr;
  118. +
  119. + if (len < IPFIX_HDRLEN + IPFIX_SET_HDRLEN)
  120. + return NULL;
  121. +
  122. + msg = malloc(sizeof(struct ipfix_msg) + len);
  123. + memset(msg, 0, sizeof(struct ipfix_msg));
  124. + msg->tail = msg->data + IPFIX_HDRLEN;
  125. + msg->end = msg->data + len;
  126. +
  127. + hdr = ipfix_msg_hdr(msg);
  128. + memset(hdr, 0, IPFIX_HDRLEN);
  129. + hdr->version = htons(IPFIX_VERSION);
  130. + hdr->oid = htonl(oid);
  131. +
  132. + return msg;
  133. +}
  134. +
  135. +void ipfix_msg_free(struct ipfix_msg *msg)
  136. +{
  137. + if (!msg)
  138. + return;
  139. +
  140. + if (msg->nrecs > 0)
  141. + ulogd_log(ULOGD_DEBUG, "%s: %d flows have been lost\n", __func__,
  142. + msg->nrecs);
  143. +
  144. + free(msg);
  145. +}
  146. +
  147. +struct ipfix_hdr *ipfix_msg_hdr(const struct ipfix_msg *msg)
  148. +{
  149. + return (struct ipfix_hdr *)msg->data;
  150. +}
  151. +
  152. +void *ipfix_msg_data(struct ipfix_msg *msg)
  153. +{
  154. + return msg->data;
  155. +}
  156. +
  157. +size_t ipfix_msg_len(const struct ipfix_msg *msg)
  158. +{
  159. + return msg->tail - msg->data;
  160. +}
  161. +
  162. +struct ipfix_set_hdr *ipfix_msg_add_set(struct ipfix_msg *msg, uint16_t sid)
  163. +{
  164. + struct ipfix_set_hdr *shdr;
  165. +
  166. + if (msg->end - msg->tail < (int) IPFIX_SET_HDRLEN)
  167. + return NULL;
  168. +
  169. + shdr = (struct ipfix_set_hdr *)msg->tail;
  170. + shdr->id = sid;
  171. + shdr->len = IPFIX_SET_HDRLEN;
  172. + msg->tail += IPFIX_SET_HDRLEN;
  173. + msg->last_set = shdr;
  174. + return shdr;
  175. +}
  176. +
  177. +struct ipfix_set_hdr *ipfix_msg_get_set(const struct ipfix_msg *msg)
  178. +{
  179. + return msg->last_set;
  180. +}
  181. +
  182. +/**
  183. + * Add data record to an IPFIX message. The data is accounted properly.
  184. + *
  185. + * @return pointer to data or %NULL if not that much space left.
  186. + */
  187. +void *ipfix_msg_add_data(struct ipfix_msg *msg, size_t len)
  188. +{
  189. + void *data;
  190. +
  191. + if (!msg->last_set) {
  192. + ulogd_log(ULOGD_FATAL, "msg->last_set is NULL\n");
  193. + return NULL;
  194. + }
  195. +
  196. + if ((ssize_t) len > msg->end - msg->tail)
  197. + return NULL;
  198. +
  199. + data = msg->tail;
  200. + msg->tail += len;
  201. + msg->nrecs++;
  202. + msg->last_set->len += len;
  203. +
  204. + return data;
  205. +}
  206. +
  207. +/* check and dump message */
  208. +int ipfix_dump_msg(const struct ipfix_msg *msg)
  209. +{
  210. + const struct ipfix_hdr *hdr = ipfix_msg_hdr(msg);
  211. + const struct ipfix_set_hdr *shdr = (struct ipfix_set_hdr *) hdr->data;
  212. +
  213. + if (ntohs(hdr->len) < IPFIX_HDRLEN) {
  214. + ulogd_log(ULOGD_FATAL, "Invalid IPFIX message header length\n");
  215. + return -1;
  216. + }
  217. + if (ipfix_msg_len(msg) != IPFIX_HDRLEN + ntohs(shdr->len)) {
  218. + ulogd_log(ULOGD_FATAL, "Invalid IPFIX message length\n");
  219. + return -1;
  220. + }
  221. +
  222. + ulogd_log(ULOGD_DEBUG, "msg: ver=%#x len=%#x t=%#x seq=%#x oid=%d\n",
  223. + ntohs(hdr->version), ntohs(hdr->len), htonl(hdr->time),
  224. + ntohl(hdr->seqno), ntohl(hdr->oid));
  225. +
  226. + return 0;
  227. +}
  228. +
  229. +/* template management */
  230. +size_t ipfix_rec_len(uint16_t sid)
  231. +{
  232. + if (sid != htons(VY_IPFIX_SID)) {
  233. + ulogd_log(ULOGD_FATAL, "Invalid SID\n");
  234. + return 0;
  235. + }
  236. +
  237. + return sizeof(struct vy_ipfix_data);
  238. +}
  239. --- /dev/null
  240. +++ b/output/ipfix/ipfix.h
  241. @@ -0,0 +1,89 @@
  242. +/*
  243. + * ipfix.h
  244. + *
  245. + * Holger Eitzenberger <holger@eitzenberger.org>, 2009.
  246. + */
  247. +#ifndef IPFIX_H
  248. +#define IPFIX_H
  249. +
  250. +#include <stdint.h>
  251. +#include <netinet/in.h>
  252. +
  253. +
  254. +struct ipfix_hdr {
  255. +#define IPFIX_VERSION 0xa
  256. + uint16_t version;
  257. + uint16_t len;
  258. + uint32_t time;
  259. + uint32_t seqno;
  260. + uint32_t oid; /* Observation Domain ID */
  261. + uint8_t data[];
  262. +} __packed;
  263. +
  264. +#define IPFIX_HDRLEN sizeof(struct ipfix_hdr)
  265. +
  266. +/*
  267. + * IDs 0-255 are reserved for Template Sets. IDs of Data Sets are > 255.
  268. + */
  269. +struct ipfix_templ_hdr {
  270. + uint16_t id;
  271. + uint16_t cnt;
  272. + uint8_t data[];
  273. +} __packed;
  274. +
  275. +struct ipfix_set_hdr {
  276. +#define IPFIX_SET_TEMPL 2
  277. +#define IPFIX_SET_OPT_TEMPL 3
  278. + uint16_t id;
  279. + uint16_t len;
  280. + uint8_t data[];
  281. +} __packed;
  282. +
  283. +#define IPFIX_SET_HDRLEN sizeof(struct ipfix_set_hdr)
  284. +
  285. +struct ipfix_msg {
  286. + struct llist_head link;
  287. + uint8_t *tail;
  288. + uint8_t *end;
  289. + unsigned nrecs;
  290. + struct ipfix_set_hdr *last_set;
  291. + uint8_t data[];
  292. +};
  293. +
  294. +struct vy_ipfix_data {
  295. + struct in_addr saddr;
  296. + struct in_addr daddr;
  297. + uint16_t ifi_in;
  298. + uint16_t ifi_out;
  299. + uint32_t packets;
  300. + uint32_t bytes;
  301. + uint32_t start; /* Unix time */
  302. + uint32_t end; /* Unix time */
  303. + uint16_t sport;
  304. + uint16_t dport;
  305. + uint32_t aid; /* Application ID */
  306. + uint8_t l4_proto;
  307. + uint8_t dscp;
  308. + uint16_t __padding;
  309. +} __packed;
  310. +
  311. +#define VY_IPFIX_SID 256
  312. +
  313. +#define VY_IPFIX_FLOWS 36
  314. +#define VY_IPFIX_PKT_LEN (IPFIX_HDRLEN + IPFIX_SET_HDRLEN \
  315. + + VY_IPFIX_FLOWS * sizeof(struct vy_ipfix_data))
  316. +
  317. +/* template management */
  318. +size_t ipfix_rec_len(uint16_t);
  319. +
  320. +/* message handling */
  321. +struct ipfix_msg *ipfix_msg_alloc(size_t, uint32_t);
  322. +void ipfix_msg_free(struct ipfix_msg *);
  323. +struct ipfix_hdr *ipfix_msg_hdr(const struct ipfix_msg *);
  324. +size_t ipfix_msg_len(const struct ipfix_msg *);
  325. +void *ipfix_msg_data(struct ipfix_msg *);
  326. +struct ipfix_set_hdr *ipfix_msg_add_set(struct ipfix_msg *, uint16_t);
  327. +void *ipfix_msg_add_data(struct ipfix_msg *, size_t);
  328. +int ipfix_dump_msg(const struct ipfix_msg *);
  329. +
  330. +#endif /* IPFIX_H */
  331. --- /dev/null
  332. +++ b/output/ipfix/ulogd_output_IPFIX.c
  333. @@ -0,0 +1,503 @@
  334. +/*
  335. + * ulogd_output_IPFIX.c
  336. + *
  337. + * ulogd IPFIX Exporter plugin.
  338. + *
  339. + * This program is distributed in the hope that it will be useful,
  340. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  341. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  342. + * GNU General Public License for more details.
  343. + *
  344. + * You should have received a copy of the GNU General Public License
  345. + * along with this program; if not, write to the Free Software
  346. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  347. + *
  348. + * Holger Eitzenberger <holger@eitzenberger.org> Astaro AG 2009
  349. + */
  350. +#include <unistd.h>
  351. +#include <time.h>
  352. +#include <sys/types.h>
  353. +#include <sys/socket.h>
  354. +#include <arpa/inet.h>
  355. +#include <netdb.h>
  356. +#include <ulogd/ulogd.h>
  357. +#include <ulogd/common.h>
  358. +
  359. +#include "ipfix.h"
  360. +
  361. +#define DEFAULT_MTU 512 /* RFC 5101, 10.3.3 */
  362. +#define DEFAULT_PORT 4739 /* RFC 5101, 10.3.4 */
  363. +#define DEFAULT_SPORT 4740
  364. +
  365. +enum {
  366. + OID_CE = 0,
  367. + HOST_CE,
  368. + PORT_CE,
  369. + PROTO_CE,
  370. + MTU_CE,
  371. +};
  372. +
  373. +#define oid_ce(x) (x->ces[OID_CE])
  374. +#define host_ce(x) (x->ces[HOST_CE])
  375. +#define port_ce(x) (x->ces[PORT_CE])
  376. +#define proto_ce(x) (x->ces[PROTO_CE])
  377. +#define mtu_ce(x) (x->ces[MTU_CE])
  378. +
  379. +static const struct config_keyset ipfix_kset = {
  380. + .num_ces = 5,
  381. + .ces = {
  382. + {
  383. + .key = "oid",
  384. + .type = CONFIG_TYPE_INT,
  385. + .u.value = 0
  386. + },
  387. + {
  388. + .key = "host",
  389. + .type = CONFIG_TYPE_STRING,
  390. + .u.string = ""
  391. + },
  392. + {
  393. + .key = "port",
  394. + .type = CONFIG_TYPE_INT,
  395. + .u.value = DEFAULT_PORT
  396. + },
  397. + {
  398. + .key = "proto",
  399. + .type = CONFIG_TYPE_STRING,
  400. + .u.string = "tcp"
  401. + },
  402. + {
  403. + .key = "mtu",
  404. + .type = CONFIG_TYPE_INT,
  405. + .u.value = DEFAULT_MTU
  406. + }
  407. + }
  408. +};
  409. +
  410. +struct ipfix_templ {
  411. + struct ipfix_templ *next;
  412. +};
  413. +
  414. +struct ipfix_priv {
  415. + struct ulogd_fd ufd;
  416. + uint32_t seqno;
  417. + struct ipfix_msg *msg; /* current message */
  418. + struct llist_head list;
  419. + struct ipfix_templ *templates;
  420. + int proto;
  421. + struct ulogd_timer timer;
  422. + struct sockaddr_in sa;
  423. +};
  424. +
  425. +enum {
  426. + InIpSaddr = 0,
  427. + InIpDaddr,
  428. + InRawInPktCount,
  429. + InRawInPktLen,
  430. + InRawOutPktCount,
  431. + InRawOutPktLen,
  432. + InFlowStartSec,
  433. + InFlowStartUsec,
  434. + InFlowEndSec,
  435. + InFlowEndUsec,
  436. + InL4SPort,
  437. + InL4DPort,
  438. + InIpProto,
  439. + InCtMark
  440. +};
  441. +
  442. +static struct ulogd_key ipfix_in_keys[] = {
  443. + [InIpSaddr] = {
  444. + .type = ULOGD_RET_IPADDR,
  445. + .name = "orig.ip.saddr"
  446. + },
  447. + [InIpDaddr] = {
  448. + .type = ULOGD_RET_IPADDR,
  449. + .name = "orig.ip.daddr"
  450. + },
  451. + [InRawInPktCount] = {
  452. + .type = ULOGD_RET_UINT64,
  453. + .name = "orig.raw.pktcount"
  454. + },
  455. + [InRawInPktLen] = {
  456. + .type = ULOGD_RET_UINT64,
  457. + .name = "orig.raw.pktlen"
  458. + },
  459. + [InRawOutPktCount] = {
  460. + .type = ULOGD_RET_UINT64,
  461. + .name = "reply.raw.pktcount"
  462. + },
  463. + [InRawOutPktLen] = {
  464. + .type = ULOGD_RET_UINT64,
  465. + .name = "reply.raw.pktlen"
  466. + },
  467. + [InFlowStartSec] = {
  468. + .type = ULOGD_RET_UINT32,
  469. + .name = "flow.start.sec"
  470. + },
  471. + [InFlowStartUsec] = {
  472. + .type = ULOGD_RET_UINT32,
  473. + .name = "flow.start.usec"
  474. + },
  475. + [InFlowEndSec] = {
  476. + .type = ULOGD_RET_UINT32,
  477. + .name = "flow.end.sec"
  478. + },
  479. + [InFlowEndUsec] = {
  480. + .type = ULOGD_RET_UINT32,
  481. + .name = "flow.end.usec"
  482. + },
  483. + [InL4SPort] = {
  484. + .type = ULOGD_RET_UINT16,
  485. + .name = "orig.l4.sport"
  486. + },
  487. + [InL4DPort] = {
  488. + .type = ULOGD_RET_UINT16,
  489. + .name = "orig.l4.dport"
  490. + },
  491. + [InIpProto] = {
  492. + .type = ULOGD_RET_UINT8,
  493. + .name = "orig.ip.protocol"
  494. + },
  495. + [InCtMark] = {
  496. + .type = ULOGD_RET_UINT32,
  497. + .name = "ct.mark"
  498. + }
  499. +};
  500. +
  501. +/* do some polishing and enqueue it */
  502. +static void enqueue_msg(struct ipfix_priv *priv, struct ipfix_msg *msg)
  503. +{
  504. + struct ipfix_hdr *hdr = ipfix_msg_data(msg);
  505. +
  506. + if (!msg)
  507. + return;
  508. +
  509. + hdr->time = htonl(time(NULL));
  510. + hdr->seqno = htonl(priv->seqno += msg->nrecs);
  511. + if (msg->last_set) {
  512. + msg->last_set->id = htons(msg->last_set->id);
  513. + msg->last_set->len = htons(msg->last_set->len);
  514. + msg->last_set = NULL;
  515. + }
  516. + hdr->len = htons(ipfix_msg_len(msg));
  517. +
  518. + llist_add(&msg->link, &priv->list);
  519. +}
  520. +
  521. +/**
  522. + * @return %ULOGD_IRET_OK or error value
  523. + */
  524. +static int send_msgs(struct ulogd_pluginstance *pi)
  525. +{
  526. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  527. + struct llist_head *curr, *tmp;
  528. + struct ipfix_msg *msg;
  529. + int ret = ULOGD_IRET_OK, sent;
  530. +
  531. + llist_for_each_prev(curr, &priv->list) {
  532. + msg = llist_entry(curr, struct ipfix_msg, link);
  533. +
  534. + sent = send(priv->ufd.fd, ipfix_msg_data(msg), ipfix_msg_len(msg), 0);
  535. + if (sent < 0) {
  536. + ulogd_log(ULOGD_ERROR, "send: %m\n");
  537. + ret = ULOGD_IRET_ERR;
  538. + goto done;
  539. + }
  540. +
  541. + /* TODO handle short send() for other protocols */
  542. + if ((size_t) sent < ipfix_msg_len(msg))
  543. + ulogd_log(ULOGD_ERROR, "short send: %d < %d\n",
  544. + sent, ipfix_msg_len(msg));
  545. + }
  546. +
  547. + llist_for_each_safe(curr, tmp, &priv->list) {
  548. + msg = llist_entry(curr, struct ipfix_msg, link);
  549. + llist_del(curr);
  550. + msg->nrecs = 0;
  551. + ipfix_msg_free(msg);
  552. + }
  553. +
  554. +done:
  555. + return ret;
  556. +}
  557. +
  558. +static int ipfix_ufd_cb(int fd, unsigned what, void *arg)
  559. +{
  560. + struct ulogd_pluginstance *pi = arg;
  561. + struct ipfix_priv *priv = (struct ipfix_priv *) pi->private;
  562. + ssize_t nread;
  563. + char buf[16];
  564. +
  565. + if (what & ULOGD_FD_READ) {
  566. + nread = recv(priv->ufd.fd, buf, sizeof(buf), MSG_DONTWAIT);
  567. + if (nread < 0) {
  568. + ulogd_log(ULOGD_ERROR, "recv: %m\n");
  569. + } else if (!nread) {
  570. + ulogd_log(ULOGD_INFO, "connection reset by peer\n");
  571. + ulogd_unregister_fd(&priv->ufd);
  572. + } else
  573. + ulogd_log(ULOGD_INFO, "unexpected data (%d bytes)\n", nread);
  574. + }
  575. +
  576. + return 0;
  577. +}
  578. +
  579. +static void ipfix_timer_cb(struct ulogd_timer *t, void *data)
  580. +{
  581. + struct ulogd_pluginstance *pi = data;
  582. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  583. +
  584. + if (priv->msg && priv->msg->nrecs > 0) {
  585. + enqueue_msg(priv, priv->msg);
  586. + priv->msg = NULL;
  587. + send_msgs(pi);
  588. + }
  589. +}
  590. +
  591. +static int ipfix_configure(struct ulogd_pluginstance *pi, struct ulogd_pluginstance_stack *stack)
  592. +{
  593. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  594. + int oid, port, mtu, ret;
  595. + char *host, *proto;
  596. + char addr[16];
  597. +
  598. + ret = config_parse_file(pi->id, pi->config_kset);
  599. + if (ret < 0)
  600. + return ret;
  601. +
  602. + oid = oid_ce(pi->config_kset).u.value;
  603. + host = host_ce(pi->config_kset).u.string;
  604. + port = port_ce(pi->config_kset).u.value;
  605. + proto = proto_ce(pi->config_kset).u.string;
  606. + mtu = mtu_ce(pi->config_kset).u.value;
  607. +
  608. + if (!oid) {
  609. + ulogd_log(ULOGD_FATAL, "invalid Observation ID\n");
  610. + return ULOGD_IRET_ERR;
  611. + }
  612. + if (!host || !strcmp(host, "")) {
  613. + ulogd_log(ULOGD_FATAL, "no destination host specified\n");
  614. + return ULOGD_IRET_ERR;
  615. + }
  616. +
  617. + if (!strcmp(proto, "udp")) {
  618. + priv->proto = IPPROTO_UDP;
  619. + } else if (!strcmp(proto, "tcp")) {
  620. + priv->proto = IPPROTO_TCP;
  621. + } else {
  622. + ulogd_log(ULOGD_FATAL, "unsupported protocol '%s'\n", proto);
  623. + return ULOGD_IRET_ERR;
  624. + }
  625. +
  626. + memset(&priv->sa, 0, sizeof(priv->sa));
  627. + priv->sa.sin_family = AF_INET;
  628. + priv->sa.sin_port = htons(port);
  629. + ret = inet_pton(AF_INET, host, &priv->sa.sin_addr);
  630. + if (ret <= 0) {
  631. + ulogd_log(ULOGD_FATAL, "inet_pton: %m\n");
  632. + return ULOGD_IRET_ERR;
  633. + }
  634. +
  635. + INIT_LLIST_HEAD(&priv->list);
  636. +
  637. + ulogd_init_timer(&priv->timer, pi, ipfix_timer_cb);
  638. +
  639. + ulogd_log(ULOGD_INFO, "using IPFIX Collector at %s:%d (MTU %d)\n",
  640. + inet_ntop(AF_INET, &priv->sa.sin_addr, addr, sizeof(addr)),
  641. + port, mtu);
  642. +
  643. + return ULOGD_IRET_OK;
  644. +}
  645. +
  646. +static int tcp_connect(struct ulogd_pluginstance *pi)
  647. +{
  648. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  649. + int ret = ULOGD_IRET_ERR;
  650. +
  651. + if ((priv->ufd.fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  652. + ulogd_log(ULOGD_FATAL, "socket: %m\n");
  653. + return ULOGD_IRET_ERR;
  654. + }
  655. +
  656. + if (connect(priv->ufd.fd, (struct sockaddr *) &priv->sa, sizeof(priv->sa)) < 0) {
  657. + ulogd_log(ULOGD_ERROR, "connect: %m\n");
  658. + ret = ULOGD_IRET_ERR;
  659. + goto err_close;
  660. + }
  661. +
  662. + return ULOGD_IRET_OK;
  663. +
  664. +err_close:
  665. + close(priv->ufd.fd);
  666. + return ret;
  667. +}
  668. +
  669. +static int udp_connect(struct ulogd_pluginstance *pi)
  670. +{
  671. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  672. +
  673. + if ((priv->ufd.fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  674. + ulogd_log(ULOGD_FATAL, "socket: %m\n");
  675. + return ULOGD_IRET_ERR;
  676. + }
  677. +
  678. + if (connect(priv->ufd.fd, (struct sockaddr *) &priv->sa, sizeof(priv->sa)) < 0) {
  679. + ulogd_log(ULOGD_ERROR, "connect: %m\n");
  680. + return ULOGD_IRET_ERR;
  681. + }
  682. +
  683. + return 0;
  684. +}
  685. +
  686. +static int ipfix_start(struct ulogd_pluginstance *pi)
  687. +{
  688. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  689. + char addr[16];
  690. + int port, ret;
  691. +
  692. + switch (priv->proto) {
  693. + case IPPROTO_UDP:
  694. + if ((ret = udp_connect(pi)) < 0)
  695. + return ret;
  696. + break;
  697. + case IPPROTO_TCP:
  698. + if ((ret = tcp_connect(pi)) < 0)
  699. + return ret;
  700. + break;
  701. +
  702. + default:
  703. + break;
  704. + }
  705. +
  706. + priv->seqno = 0;
  707. +
  708. + port = port_ce(pi->config_kset).u.value;
  709. + ulogd_log(ULOGD_INFO, "connected to %s:%d\n",
  710. + inet_ntop(AF_INET, &priv->sa.sin_addr, addr, sizeof(addr)),
  711. + port);
  712. +
  713. + /* Register the socket FD */
  714. + priv->ufd.when = ULOGD_FD_READ;
  715. + priv->ufd.cb = ipfix_ufd_cb;
  716. + priv->ufd.data = pi;
  717. +
  718. + if (ulogd_register_fd(&priv->ufd) < 0)
  719. + return ULOGD_IRET_ERR;
  720. +
  721. + /* Add a 1 second timer */
  722. + ulogd_add_timer(&priv->timer, 1);
  723. +
  724. + return ULOGD_IRET_OK;
  725. +}
  726. +
  727. +static int ipfix_stop(struct ulogd_pluginstance *pi)
  728. +{
  729. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  730. +
  731. + ulogd_unregister_fd(&priv->ufd);
  732. + close(priv->ufd.fd);
  733. + priv->ufd.fd = -1;
  734. +
  735. + ulogd_del_timer(&priv->timer);
  736. +
  737. + ipfix_msg_free(priv->msg);
  738. + priv->msg = NULL;
  739. +
  740. + return 0;
  741. +}
  742. +
  743. +static int ipfix_interp(struct ulogd_pluginstance *pi)
  744. +{
  745. + struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  746. + struct vy_ipfix_data *data;
  747. + int oid, mtu, ret;
  748. + char addr[16];
  749. +
  750. + if (!(GET_FLAGS(pi->input.keys, InIpSaddr) & ULOGD_RETF_VALID))
  751. + return ULOGD_IRET_OK;
  752. +
  753. + oid = oid_ce(pi->config_kset).u.value;
  754. + mtu = mtu_ce(pi->config_kset).u.value;
  755. +
  756. +again:
  757. + if (!priv->msg) {
  758. + priv->msg = ipfix_msg_alloc(mtu, oid);
  759. + if (!priv->msg) {
  760. + /* just drop this flow */
  761. + ulogd_log(ULOGD_ERROR, "out of memory, dropping flow\n");
  762. + return ULOGD_IRET_OK;
  763. + }
  764. + ipfix_msg_add_set(priv->msg, VY_IPFIX_SID);
  765. + }
  766. +
  767. + data = ipfix_msg_add_data(priv->msg, sizeof(struct vy_ipfix_data));
  768. + if (!data) {
  769. + enqueue_msg(priv, priv->msg);
  770. + priv->msg = NULL;
  771. + /* can't loop because the next will definitely succeed */
  772. + goto again;
  773. + }
  774. +
  775. + data->ifi_in = data->ifi_out = 0;
  776. +
  777. + data->saddr.s_addr = ikey_get_u32(&pi->input.keys[InIpSaddr]);
  778. + data->daddr.s_addr = ikey_get_u32(&pi->input.keys[InIpDaddr]);
  779. +
  780. + data->packets = htonl((uint32_t) (ikey_get_u64(&pi->input.keys[InRawInPktCount])
  781. + + ikey_get_u64(&pi->input.keys[InRawOutPktCount])));
  782. + data->bytes = htonl((uint32_t) (ikey_get_u64(&pi->input.keys[InRawInPktLen])
  783. + + ikey_get_u64(&pi->input.keys[InRawOutPktLen])));
  784. +
  785. + data->start = htonl(ikey_get_u32(&pi->input.keys[InFlowStartSec]));
  786. + data->end = htonl(ikey_get_u32(&pi->input.keys[InFlowEndSec]));
  787. +
  788. + if (GET_FLAGS(pi->input.keys, InL4SPort) & ULOGD_RETF_VALID) {
  789. + data->sport = htons(ikey_get_u16(&pi->input.keys[InL4SPort]));
  790. + data->dport = htons(ikey_get_u16(&pi->input.keys[InL4DPort]));
  791. + }
  792. +
  793. + data->aid = 0;
  794. + if (GET_FLAGS(pi->input.keys, InCtMark) & ULOGD_RETF_VALID)
  795. + data->aid = htonl(ikey_get_u32(&pi->input.keys[InCtMark]));
  796. +
  797. + data->l4_proto = ikey_get_u8(&pi->input.keys[InIpProto]);
  798. + data->__padding = 0;
  799. +
  800. + ulogd_log(ULOGD_DEBUG, "Got new packet (packets = %u, bytes = %u, flow = (%u, %u), saddr = %s, daddr = %s, sport = %u, dport = %u)\n",
  801. + ntohl(data->packets), ntohl(data->bytes), ntohl(data->start), ntohl(data->end),
  802. + inet_ntop(AF_INET, &data->saddr.s_addr, addr, sizeof(addr)),
  803. + inet_ntop(AF_INET, &data->daddr.s_addr, addr, sizeof(addr)),
  804. + ntohs(data->sport), ntohs(data->dport));
  805. +
  806. + if ((ret = send_msgs(pi)) < 0)
  807. + return ret;
  808. +
  809. + return ULOGD_IRET_OK;
  810. +}
  811. +
  812. +static struct ulogd_plugin ipfix_plugin = {
  813. + .name = "IPFIX",
  814. + .input = {
  815. + .keys = ipfix_in_keys,
  816. + .num_keys = ARRAY_SIZE(ipfix_in_keys),
  817. + .type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW | ULOGD_DTYPE_SUM
  818. + },
  819. + .output = {
  820. + .type = ULOGD_DTYPE_SINK
  821. + },
  822. + .config_kset = (struct config_keyset *) &ipfix_kset,
  823. + .priv_size = sizeof(struct ipfix_priv),
  824. + .configure = ipfix_configure,
  825. + .start = ipfix_start,
  826. + .stop = ipfix_stop,
  827. + .interp = ipfix_interp,
  828. + .version = VERSION,
  829. +};
  830. +
  831. +void __attribute__ ((constructor)) init(void);
  832. +
  833. +void init(void)
  834. +{
  835. + ulogd_register_plugin(&ipfix_plugin);
  836. +}