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.

421 lines
12 KiB

  1. From cc919f7013d2d76c8bef0b9c562c1faf98a095a3 Mon Sep 17 00:00:00 2001
  2. From: Ander Juaristi <a@juaristi.eus>
  3. Date: Fri, 26 Apr 2019 09:58:07 +0200
  4. Subject: IPFIX: Introduce template record support
  5. This commit adds the ability to send template records
  6. to the remote collector.
  7. In addition, it also introduces a new
  8. configuration parameter 'send_template', which tells when template
  9. records should be sent. It accepts the following string values:
  10. - "once": Send the template record only the first time (might be coalesced
  11. with data records).
  12. - "always": Send the template record always, with every data record that is sent
  13. to the collector (multiple data records might be sent together).
  14. - "never": Assume the collector knows the schema already. Do not send template records.
  15. If omitted, the default value for 'send_template' is "once".
  16. Signed-off-by: Ander Juaristi <a@juaristi.eus>
  17. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
  18. ---
  19. include/ulogd/ipfix_protocol.h | 1 +
  20. output/ipfix/ipfix.c | 97 +++++++++++++++++++++++++++++++++++++--
  21. output/ipfix/ipfix.h | 22 ++++-----
  22. output/ipfix/ulogd_output_IPFIX.c | 56 ++++++++++++----------
  23. 4 files changed, 139 insertions(+), 37 deletions(-)
  24. --- a/include/ulogd/ipfix_protocol.h
  25. +++ b/include/ulogd/ipfix_protocol.h
  26. @@ -129,6 +129,7 @@ enum {
  27. /* reserved */
  28. IPFIX_fragmentOffsetIPv4 = 88,
  29. /* reserved */
  30. + IPFIX_applicationId = 95,
  31. IPFIX_bgpNextAdjacentAsNumber = 128,
  32. IPFIX_bgpPrevAdjacentAsNumber = 129,
  33. IPFIX_exporterIPv4Address = 130,
  34. --- a/output/ipfix/ipfix.c
  35. +++ b/output/ipfix/ipfix.c
  36. @@ -2,6 +2,7 @@
  37. * ipfix.c
  38. *
  39. * Holger Eitzenberger, 2009.
  40. + * Ander Juaristi, 2019
  41. */
  42. /* These forward declarations are needed since ulogd.h doesn't like to be the first */
  43. @@ -13,25 +14,107 @@
  44. #include <ulogd/ulogd.h>
  45. #include <ulogd/common.h>
  46. +#include <ulogd/ipfix_protocol.h>
  47. -struct ipfix_msg *ipfix_msg_alloc(size_t len, uint32_t oid)
  48. +struct ipfix_templ_elem {
  49. + uint16_t id;
  50. + uint16_t len;
  51. +};
  52. +
  53. +struct ipfix_templ {
  54. + unsigned int num_templ_elements;
  55. + struct ipfix_templ_elem templ_elements[];
  56. +};
  57. +
  58. +/* Template fields modeled after vy_ipfix_data */
  59. +static const struct ipfix_templ template = {
  60. + .num_templ_elements = 10,
  61. + .templ_elements = {
  62. + {
  63. + .id = IPFIX_sourceIPv4Address,
  64. + .len = sizeof(uint32_t)
  65. + },
  66. + {
  67. + .id = IPFIX_destinationIPv4Address,
  68. + .len = sizeof(uint32_t)
  69. + },
  70. + {
  71. + .id = IPFIX_packetTotalCount,
  72. + .len = sizeof(uint32_t)
  73. + },
  74. + {
  75. + .id = IPFIX_octetTotalCount,
  76. + .len = sizeof(uint32_t)
  77. + },
  78. + {
  79. + .id = IPFIX_flowStartSeconds,
  80. + .len = sizeof(uint32_t)
  81. + },
  82. + {
  83. + .id = IPFIX_flowEndSeconds,
  84. + .len = sizeof(uint32_t)
  85. + },
  86. + {
  87. + .id = IPFIX_sourceTransportPort,
  88. + .len = sizeof(uint16_t)
  89. + },
  90. + {
  91. + .id = IPFIX_destinationTransportPort,
  92. + .len = sizeof(uint16_t)
  93. + },
  94. + {
  95. + .id = IPFIX_protocolIdentifier,
  96. + .len = sizeof(uint8_t)
  97. + },
  98. + {
  99. + .id = IPFIX_applicationId,
  100. + .len = sizeof(uint32_t)
  101. + }
  102. + }
  103. +};
  104. +
  105. +struct ipfix_msg *ipfix_msg_alloc(size_t len, uint32_t oid, int tid)
  106. {
  107. struct ipfix_msg *msg;
  108. struct ipfix_hdr *hdr;
  109. + struct ipfix_templ_hdr *templ_hdr;
  110. + struct ipfix_templ_elem *elem;
  111. + unsigned int i = 0;
  112. - if (len < IPFIX_HDRLEN + IPFIX_SET_HDRLEN)
  113. + if ((tid > 0 && len < IPFIX_HDRLEN + IPFIX_TEMPL_HDRLEN(template.num_templ_elements) + IPFIX_SET_HDRLEN) ||
  114. + (len < IPFIX_HDRLEN + IPFIX_SET_HDRLEN))
  115. return NULL;
  116. msg = malloc(sizeof(struct ipfix_msg) + len);
  117. memset(msg, 0, sizeof(struct ipfix_msg));
  118. - msg->tail = msg->data + IPFIX_HDRLEN;
  119. + msg->tid = tid;
  120. msg->end = msg->data + len;
  121. + msg->tail = msg->data + IPFIX_HDRLEN;
  122. + if (tid > 0)
  123. + msg->tail += IPFIX_TEMPL_HDRLEN(template.num_templ_elements);
  124. + /* Initialize message header */
  125. hdr = ipfix_msg_hdr(msg);
  126. memset(hdr, 0, IPFIX_HDRLEN);
  127. hdr->version = htons(IPFIX_VERSION);
  128. hdr->oid = htonl(oid);
  129. + if (tid > 0) {
  130. + /* Initialize template record header */
  131. + templ_hdr = ipfix_msg_templ_hdr(msg);
  132. + templ_hdr->sid = htons(2);
  133. + templ_hdr->tid = htons(tid);
  134. + templ_hdr->len = htons(IPFIX_TEMPL_HDRLEN(template.num_templ_elements));
  135. + templ_hdr->cnt = htons(template.num_templ_elements);
  136. +
  137. + while (i < template.num_templ_elements) {
  138. + elem = (struct ipfix_templ_elem *) &templ_hdr->data[i * 4];
  139. + elem->id = htons(template.templ_elements[i].id);
  140. + elem->len = htons(template.templ_elements[i].len);
  141. + i++;
  142. + }
  143. + }
  144. +
  145. return msg;
  146. }
  147. @@ -47,6 +130,14 @@ void ipfix_msg_free(struct ipfix_msg *ms
  148. free(msg);
  149. }
  150. +struct ipfix_templ_hdr *ipfix_msg_templ_hdr(const struct ipfix_msg *msg)
  151. +{
  152. + if (msg->tid > 0)
  153. + return (struct ipfix_templ_hdr *) (msg->data + IPFIX_HDRLEN);
  154. +
  155. + return NULL;
  156. +}
  157. +
  158. struct ipfix_hdr *ipfix_msg_hdr(const struct ipfix_msg *msg)
  159. {
  160. return (struct ipfix_hdr *)msg->data;
  161. --- a/output/ipfix/ipfix.h
  162. +++ b/output/ipfix/ipfix.h
  163. @@ -2,6 +2,7 @@
  164. * ipfix.h
  165. *
  166. * Holger Eitzenberger <holger@eitzenberger.org>, 2009.
  167. + * Ander Juaristi <a@juaristi.eus>, 2019
  168. */
  169. #ifndef IPFIX_H
  170. #define IPFIX_H
  171. @@ -20,17 +21,21 @@ struct ipfix_hdr {
  172. uint8_t data[];
  173. } __packed;
  174. -#define IPFIX_HDRLEN sizeof(struct ipfix_hdr)
  175. +#define IPFIX_HDRLEN sizeof(struct ipfix_hdr)
  176. /*
  177. * IDs 0-255 are reserved for Template Sets. IDs of Data Sets are > 255.
  178. */
  179. struct ipfix_templ_hdr {
  180. - uint16_t id;
  181. + uint16_t sid;
  182. + uint16_t len;
  183. + uint16_t tid;
  184. uint16_t cnt;
  185. uint8_t data[];
  186. } __packed;
  187. +#define IPFIX_TEMPL_HDRLEN(nfields) sizeof(struct ipfix_templ_hdr) + (sizeof(uint16_t) * 2 * nfields)
  188. +
  189. struct ipfix_set_hdr {
  190. #define IPFIX_SET_TEMPL 2
  191. #define IPFIX_SET_OPT_TEMPL 3
  192. @@ -46,6 +51,7 @@ struct ipfix_msg {
  193. uint8_t *tail;
  194. uint8_t *end;
  195. unsigned nrecs;
  196. + int tid;
  197. struct ipfix_set_hdr *last_set;
  198. uint8_t data[];
  199. };
  200. @@ -53,18 +59,14 @@ struct ipfix_msg {
  201. struct vy_ipfix_data {
  202. struct in_addr saddr;
  203. struct in_addr daddr;
  204. - uint16_t ifi_in;
  205. - uint16_t ifi_out;
  206. uint32_t packets;
  207. uint32_t bytes;
  208. uint32_t start; /* Unix time */
  209. uint32_t end; /* Unix time */
  210. uint16_t sport;
  211. uint16_t dport;
  212. - uint32_t aid; /* Application ID */
  213. uint8_t l4_proto;
  214. - uint8_t dscp;
  215. - uint16_t __padding;
  216. + uint32_t aid; /* Application ID */
  217. } __packed;
  218. #define VY_IPFIX_SID 256
  219. @@ -73,13 +75,11 @@ struct vy_ipfix_data {
  220. #define VY_IPFIX_PKT_LEN (IPFIX_HDRLEN + IPFIX_SET_HDRLEN \
  221. + VY_IPFIX_FLOWS * sizeof(struct vy_ipfix_data))
  222. -/* template management */
  223. -size_t ipfix_rec_len(uint16_t);
  224. -
  225. /* message handling */
  226. -struct ipfix_msg *ipfix_msg_alloc(size_t, uint32_t);
  227. +struct ipfix_msg *ipfix_msg_alloc(size_t, uint32_t, int);
  228. void ipfix_msg_free(struct ipfix_msg *);
  229. struct ipfix_hdr *ipfix_msg_hdr(const struct ipfix_msg *);
  230. +struct ipfix_templ_hdr *ipfix_msg_templ_hdr(const struct ipfix_msg *);
  231. size_t ipfix_msg_len(const struct ipfix_msg *);
  232. void *ipfix_msg_data(struct ipfix_msg *);
  233. struct ipfix_set_hdr *ipfix_msg_add_set(struct ipfix_msg *, uint16_t);
  234. --- a/output/ipfix/ulogd_output_IPFIX.c
  235. +++ b/output/ipfix/ulogd_output_IPFIX.c
  236. @@ -3,6 +3,9 @@
  237. *
  238. * ulogd IPFIX Exporter plugin.
  239. *
  240. + * (C) 2009 by Holger Eitzenberger <holger@eitzenberger.org>, Astaro AG
  241. + * (C) 2019 by Ander Juaristi <a@juaristi.eus>
  242. + *
  243. * This program is distributed in the hope that it will be useful,
  244. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  245. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  246. @@ -11,8 +14,6 @@
  247. * You should have received a copy of the GNU General Public License
  248. * along with this program; if not, write to the Free Software
  249. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  250. - *
  251. - * Holger Eitzenberger <holger@eitzenberger.org> Astaro AG 2009
  252. */
  253. #include <unistd.h>
  254. #include <time.h>
  255. @@ -28,6 +29,7 @@
  256. #define DEFAULT_MTU 512 /* RFC 5101, 10.3.3 */
  257. #define DEFAULT_PORT 4739 /* RFC 5101, 10.3.4 */
  258. #define DEFAULT_SPORT 4740
  259. +#define DEFAULT_SEND_TEMPLATE "once"
  260. enum {
  261. OID_CE = 0,
  262. @@ -35,16 +37,18 @@ enum {
  263. PORT_CE,
  264. PROTO_CE,
  265. MTU_CE,
  266. + SEND_TEMPLATE_CE
  267. };
  268. -#define oid_ce(x) (x->ces[OID_CE])
  269. -#define host_ce(x) (x->ces[HOST_CE])
  270. -#define port_ce(x) (x->ces[PORT_CE])
  271. -#define proto_ce(x) (x->ces[PROTO_CE])
  272. -#define mtu_ce(x) (x->ces[MTU_CE])
  273. +#define oid_ce(x) (x->ces[OID_CE])
  274. +#define host_ce(x) (x->ces[HOST_CE])
  275. +#define port_ce(x) (x->ces[PORT_CE])
  276. +#define proto_ce(x) (x->ces[PROTO_CE])
  277. +#define mtu_ce(x) (x->ces[MTU_CE])
  278. +#define send_template_ce(x) (x->ces[SEND_TEMPLATE_CE])
  279. static const struct config_keyset ipfix_kset = {
  280. - .num_ces = 5,
  281. + .num_ces = 6,
  282. .ces = {
  283. {
  284. .key = "oid",
  285. @@ -70,20 +74,21 @@ static const struct config_keyset ipfix_
  286. .key = "mtu",
  287. .type = CONFIG_TYPE_INT,
  288. .u.value = DEFAULT_MTU
  289. + },
  290. + {
  291. + .key = "send_template",
  292. + .type = CONFIG_TYPE_STRING,
  293. + .u.string = DEFAULT_SEND_TEMPLATE
  294. }
  295. }
  296. };
  297. -struct ipfix_templ {
  298. - struct ipfix_templ *next;
  299. -};
  300. -
  301. struct ipfix_priv {
  302. struct ulogd_fd ufd;
  303. uint32_t seqno;
  304. struct ipfix_msg *msg; /* current message */
  305. struct llist_head list;
  306. - struct ipfix_templ *templates;
  307. + int tid;
  308. int proto;
  309. struct ulogd_timer timer;
  310. struct sockaddr_in sa;
  311. @@ -258,8 +263,8 @@ static void ipfix_timer_cb(struct ulogd_
  312. static int ipfix_configure(struct ulogd_pluginstance *pi, struct ulogd_pluginstance_stack *stack)
  313. {
  314. struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  315. + char *host, *proto, *send_template;
  316. int oid, port, mtu, ret;
  317. - char *host, *proto;
  318. char addr[16];
  319. ret = config_parse_file(pi->id, pi->config_kset);
  320. @@ -271,6 +276,7 @@ static int ipfix_configure(struct ulogd_
  321. port = port_ce(pi->config_kset).u.value;
  322. proto = proto_ce(pi->config_kset).u.string;
  323. mtu = mtu_ce(pi->config_kset).u.value;
  324. + send_template = send_template_ce(pi->config_kset).u.string;
  325. if (!oid) {
  326. ulogd_log(ULOGD_FATAL, "invalid Observation ID\n");
  327. @@ -303,6 +309,8 @@ static int ipfix_configure(struct ulogd_
  328. ulogd_init_timer(&priv->timer, pi, ipfix_timer_cb);
  329. + priv->tid = (strcmp(send_template, "never") ? VY_IPFIX_SID : -1);
  330. +
  331. ulogd_log(ULOGD_INFO, "using IPFIX Collector at %s:%d (MTU %d)\n",
  332. inet_ntop(AF_INET, &priv->sa.sin_addr, addr, sizeof(addr)),
  333. port, mtu);
  334. @@ -410,25 +418,30 @@ static int ipfix_stop(struct ulogd_plugi
  335. static int ipfix_interp(struct ulogd_pluginstance *pi)
  336. {
  337. struct ipfix_priv *priv = (struct ipfix_priv *) &pi->private;
  338. + char saddr[16], daddr[16], *send_template;
  339. struct vy_ipfix_data *data;
  340. int oid, mtu, ret;
  341. - char addr[16];
  342. if (!(GET_FLAGS(pi->input.keys, InIpSaddr) & ULOGD_RETF_VALID))
  343. return ULOGD_IRET_OK;
  344. oid = oid_ce(pi->config_kset).u.value;
  345. mtu = mtu_ce(pi->config_kset).u.value;
  346. + send_template = send_template_ce(pi->config_kset).u.string;
  347. again:
  348. if (!priv->msg) {
  349. - priv->msg = ipfix_msg_alloc(mtu, oid);
  350. + priv->msg = ipfix_msg_alloc(mtu, oid, priv->tid);
  351. if (!priv->msg) {
  352. /* just drop this flow */
  353. ulogd_log(ULOGD_ERROR, "out of memory, dropping flow\n");
  354. return ULOGD_IRET_OK;
  355. }
  356. ipfix_msg_add_set(priv->msg, VY_IPFIX_SID);
  357. +
  358. + /* template sent - do not send it again the next time */
  359. + if (priv->tid == VY_IPFIX_SID && strcmp(send_template, "once") == 0)
  360. + priv->tid = -1;
  361. }
  362. data = ipfix_msg_add_data(priv->msg, sizeof(struct vy_ipfix_data));
  363. @@ -439,8 +452,6 @@ again:
  364. goto again;
  365. }
  366. - data->ifi_in = data->ifi_out = 0;
  367. -
  368. data->saddr.s_addr = ikey_get_u32(&pi->input.keys[InIpSaddr]);
  369. data->daddr.s_addr = ikey_get_u32(&pi->input.keys[InIpDaddr]);
  370. @@ -462,13 +473,12 @@ again:
  371. data->aid = htonl(ikey_get_u32(&pi->input.keys[InCtMark]));
  372. data->l4_proto = ikey_get_u8(&pi->input.keys[InIpProto]);
  373. - data->__padding = 0;
  374. ulogd_log(ULOGD_DEBUG, "Got new packet (packets = %u, bytes = %u, flow = (%u, %u), saddr = %s, daddr = %s, sport = %u, dport = %u)\n",
  375. - ntohl(data->packets), ntohl(data->bytes), ntohl(data->start), ntohl(data->end),
  376. - inet_ntop(AF_INET, &data->saddr.s_addr, addr, sizeof(addr)),
  377. - inet_ntop(AF_INET, &data->daddr.s_addr, addr, sizeof(addr)),
  378. - ntohs(data->sport), ntohs(data->dport));
  379. + ntohl(data->packets), ntohl(data->bytes), ntohl(data->start), ntohl(data->end),
  380. + inet_ntop(AF_INET, &data->saddr.s_addr, saddr, sizeof(saddr)),
  381. + inet_ntop(AF_INET, &data->daddr.s_addr, daddr, sizeof(daddr)),
  382. + ntohs(data->sport), ntohs(data->dport));
  383. if ((ret = send_msgs(pi)) < 0)
  384. return ret;