1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 Alexander V. Chernikov <melifaro@ipfw.ru> 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 AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_inet6.h" 33 #include "opt_route.h" 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/counter.h> 37 #include <sys/kernel.h> 38 #include <sys/ktr.h> 39 #include <sys/limits.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/syslog.h> 43 #include <sys/socket.h> 44 #include <vm/uma.h> 45 46 #include <net/if.h> 47 #include <net/route.h> 48 #include <net/ethernet.h> 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 #include <netinet/ip.h> 52 #include <netinet/ip6.h> 53 #include <netinet/tcp.h> 54 #include <netinet/udp.h> 55 56 #include <netgraph/ng_message.h> 57 #include <netgraph/netgraph.h> 58 59 #include <netgraph/netflow/netflow.h> 60 #include <netgraph/netflow/ng_netflow.h> 61 #include <netgraph/netflow/netflow_v9.h> 62 63 MALLOC_DECLARE(M_NETFLOW_GENERAL); 64 MALLOC_DEFINE(M_NETFLOW_GENERAL, "netflow_general", "plog, V9 templates data"); 65 66 /* 67 * Base V9 templates for L4+ IPv4/IPv6 protocols 68 */ 69 struct netflow_v9_template _netflow_v9_record_ipv4_tcp[] = 70 { 71 { NETFLOW_V9_FIELD_IPV4_SRC_ADDR, 4}, 72 { NETFLOW_V9_FIELD_IPV4_DST_ADDR, 4}, 73 { NETFLOW_V9_FIELD_IPV4_NEXT_HOP, 4}, 74 { NETFLOW_V9_FIELD_INPUT_SNMP, 2}, 75 { NETFLOW_V9_FIELD_OUTPUT_SNMP, 2}, 76 { NETFLOW_V9_FIELD_IN_PKTS, sizeof(CNTR)}, 77 { NETFLOW_V9_FIELD_IN_BYTES, sizeof(CNTR)}, 78 { NETFLOW_V9_FIELD_OUT_PKTS, sizeof(CNTR)}, 79 { NETFLOW_V9_FIELD_OUT_BYTES, sizeof(CNTR)}, 80 { NETFLOW_V9_FIELD_FIRST_SWITCHED, 4}, 81 { NETFLOW_V9_FIELD_LAST_SWITCHED, 4}, 82 { NETFLOW_V9_FIELD_L4_SRC_PORT, 2}, 83 { NETFLOW_V9_FIELD_L4_DST_PORT, 2}, 84 { NETFLOW_V9_FIELD_TCP_FLAGS, 1}, 85 { NETFLOW_V9_FIELD_PROTOCOL, 1}, 86 { NETFLOW_V9_FIELD_TOS, 1}, 87 { NETFLOW_V9_FIELD_SRC_AS, 4}, 88 { NETFLOW_V9_FIELD_DST_AS, 4}, 89 { NETFLOW_V9_FIELD_SRC_MASK, 1}, 90 { NETFLOW_V9_FIELD_DST_MASK, 1}, 91 {0, 0} 92 }; 93 94 struct netflow_v9_template _netflow_v9_record_ipv6_tcp[] = 95 { 96 { NETFLOW_V9_FIELD_IPV6_SRC_ADDR, 16}, 97 { NETFLOW_V9_FIELD_IPV6_DST_ADDR, 16}, 98 { NETFLOW_V9_FIELD_IPV6_NEXT_HOP, 16}, 99 { NETFLOW_V9_FIELD_INPUT_SNMP, 2}, 100 { NETFLOW_V9_FIELD_OUTPUT_SNMP, 2}, 101 { NETFLOW_V9_FIELD_IN_PKTS, sizeof(CNTR)}, 102 { NETFLOW_V9_FIELD_IN_BYTES, sizeof(CNTR)}, 103 { NETFLOW_V9_FIELD_OUT_PKTS, sizeof(CNTR)}, 104 { NETFLOW_V9_FIELD_OUT_BYTES, sizeof(CNTR)}, 105 { NETFLOW_V9_FIELD_FIRST_SWITCHED, 4}, 106 { NETFLOW_V9_FIELD_LAST_SWITCHED, 4}, 107 { NETFLOW_V9_FIELD_L4_SRC_PORT, 2}, 108 { NETFLOW_V9_FIELD_L4_DST_PORT, 2}, 109 { NETFLOW_V9_FIELD_TCP_FLAGS, 1}, 110 { NETFLOW_V9_FIELD_PROTOCOL, 1}, 111 { NETFLOW_V9_FIELD_TOS, 1}, 112 { NETFLOW_V9_FIELD_SRC_AS, 4}, 113 { NETFLOW_V9_FIELD_DST_AS, 4}, 114 { NETFLOW_V9_FIELD_SRC_MASK, 1}, 115 { NETFLOW_V9_FIELD_DST_MASK, 1}, 116 {0, 0} 117 }; 118 119 /* 120 * Pre-compiles flow exporter for all possible FlowSets 121 * so we can add flowset to packet via simple memcpy() 122 */ 123 static void 124 generate_v9_templates(priv_p priv) 125 { 126 uint16_t *p, *template_fields_cnt; 127 int cnt; 128 129 int flowset_size = sizeof(struct netflow_v9_flowset_header) + 130 _NETFLOW_V9_TEMPLATE_SIZE(_netflow_v9_record_ipv4_tcp) + /* netflow_v9_record_ipv4_tcp */ 131 _NETFLOW_V9_TEMPLATE_SIZE(_netflow_v9_record_ipv6_tcp); /* netflow_v9_record_ipv6_tcp */ 132 133 priv->v9_flowsets[0] = malloc(flowset_size, M_NETFLOW_GENERAL, M_WAITOK | M_ZERO); 134 135 if (flowset_size % 4) 136 flowset_size += 4 - (flowset_size % 4); /* Padding to 4-byte boundary */ 137 138 priv->flowsets_count = 1; 139 p = (uint16_t *)priv->v9_flowsets[0]; 140 *p++ = 0; /* Flowset ID, 0 is reserved for Template FlowSets */ 141 *p++ = htons(flowset_size); /* Total FlowSet length */ 142 143 /* 144 * Most common TCP/UDP IPv4 template, ID = 256 145 */ 146 *p++ = htons(NETFLOW_V9_MAX_RESERVED_FLOWSET + NETFLOW_V9_FLOW_V4_L4); 147 template_fields_cnt = p++; 148 for (cnt = 0; _netflow_v9_record_ipv4_tcp[cnt].field_id != 0; cnt++) { 149 *p++ = htons(_netflow_v9_record_ipv4_tcp[cnt].field_id); 150 *p++ = htons(_netflow_v9_record_ipv4_tcp[cnt].field_length); 151 } 152 *template_fields_cnt = htons(cnt); 153 154 /* 155 * TCP/UDP IPv6 template, ID = 257 156 */ 157 *p++ = htons(NETFLOW_V9_MAX_RESERVED_FLOWSET + NETFLOW_V9_FLOW_V6_L4); 158 template_fields_cnt = p++; 159 for (cnt = 0; _netflow_v9_record_ipv6_tcp[cnt].field_id != 0; cnt++) { 160 *p++ = htons(_netflow_v9_record_ipv6_tcp[cnt].field_id); 161 *p++ = htons(_netflow_v9_record_ipv6_tcp[cnt].field_length); 162 } 163 *template_fields_cnt = htons(cnt); 164 165 priv->flowset_records[0] = 2; 166 } 167 168 /* Closes current data flowset */ 169 static void inline 170 close_flowset(struct mbuf *m, struct netflow_v9_packet_opt *t) 171 { 172 struct mbuf *m_old; 173 uint32_t zero = 0; 174 int offset = 0; 175 uint16_t *flowset_length, len; 176 177 /* Hack to ensure we are not crossing mbuf boundary, length is uint16_t */ 178 m_old = m_getptr(m, t->flow_header + offsetof(struct netflow_v9_flowset_header, length), &offset); 179 flowset_length = (uint16_t *)(mtod(m_old, char *) + offset); 180 181 len = (uint16_t)(m_pktlen(m) - t->flow_header); 182 /* Align on 4-byte boundary (RFC 3954, Clause 5.3) */ 183 if (len % 4) { 184 if (m_append(m, 4 - (len % 4), (void *)&zero) != 1) 185 panic("ng_netflow: m_append() failed!"); 186 187 len += 4 - (len % 4); 188 } 189 190 *flowset_length = htons(len); 191 } 192 193 /* 194 * Non-static functions called from ng_netflow.c 195 */ 196 197 /* We have full datagram in fib data. Send it to export hook. */ 198 int 199 export9_send(priv_p priv, fib_export_p fe, item_p item, struct netflow_v9_packet_opt *t, int flags) 200 { 201 struct mbuf *m = NGI_M(item); 202 struct netflow_v9_export_dgram *dgram = mtod(m, 203 struct netflow_v9_export_dgram *); 204 struct netflow_v9_header *header = &dgram->header; 205 struct timespec ts; 206 int error = 0; 207 208 if (t == NULL) { 209 CTR0(KTR_NET, "export9_send(): V9 export packet without tag"); 210 NG_FREE_ITEM(item); 211 return (0); 212 } 213 214 /* Close flowset if not closed already */ 215 if (m_pktlen(m) != t->flow_header) 216 close_flowset(m, t); 217 218 /* Fill export header. */ 219 header->count = t->count; 220 header->sys_uptime = htonl(MILLIUPTIME(time_uptime)); 221 getnanotime(&ts); 222 header->unix_secs = htonl(ts.tv_sec); 223 header->seq_num = htonl(atomic_fetchadd_32(&fe->flow9_seq, 1)); 224 header->count = htons(t->count); 225 header->source_id = htonl(fe->domain_id); 226 227 if (priv->export9 != NULL) 228 NG_FWD_ITEM_HOOK_FLAGS(error, item, priv->export9, flags); 229 else 230 NG_FREE_ITEM(item); 231 232 free(t, M_NETFLOW_GENERAL); 233 234 return (error); 235 } 236 237 /* Add V9 record to dgram. */ 238 int 239 export9_add(item_p item, struct netflow_v9_packet_opt *t, struct flow_entry *fle) 240 { 241 size_t len = 0; 242 struct netflow_v9_flowset_header fsh; 243 struct netflow_v9_record_general rg; 244 struct mbuf *m = NGI_M(item); 245 uint16_t flow_type; 246 struct flow_entry_data *fed; 247 #ifdef INET6 248 struct flow6_entry_data *fed6; 249 #endif 250 if (t == NULL) { 251 CTR0(KTR_NET, "ng_netflow: V9 export packet without tag!"); 252 return (0); 253 } 254 255 /* Prepare flow record */ 256 fed = (struct flow_entry_data *)&fle->f; 257 #ifdef INET6 258 fed6 = (struct flow6_entry_data *)&fle->f; 259 #endif 260 /* We can use flow_type field since fle6 offset is equal to fle */ 261 flow_type = fed->r.flow_type; 262 263 switch (flow_type) { 264 case NETFLOW_V9_FLOW_V4_L4: 265 { 266 /* IPv4 TCP/UDP/[SCTP] */ 267 struct netflow_v9_record_ipv4_tcp *rec = &rg.rec.v4_tcp; 268 269 rec->src_addr = fed->r.r_src.s_addr; 270 rec->dst_addr = fed->r.r_dst.s_addr; 271 rec->next_hop = fed->next_hop.s_addr; 272 rec->i_ifx = htons(fed->fle_i_ifx); 273 rec->o_ifx = htons(fed->fle_o_ifx); 274 rec->i_packets = htonl(fed->packets); 275 rec->i_octets = htonl(fed->bytes); 276 rec->o_packets = htonl(0); 277 rec->o_octets = htonl(0); 278 rec->first = htonl(MILLIUPTIME(fed->first)); 279 rec->last = htonl(MILLIUPTIME(fed->last)); 280 rec->s_port = fed->r.r_sport; 281 rec->d_port = fed->r.r_dport; 282 rec->flags = fed->tcp_flags; 283 rec->prot = fed->r.r_ip_p; 284 rec->tos = fed->r.r_tos; 285 rec->dst_mask = fed->dst_mask; 286 rec->src_mask = fed->src_mask; 287 288 /* Not supported fields. */ 289 rec->src_as = rec->dst_as = 0; 290 291 len = sizeof(struct netflow_v9_record_ipv4_tcp); 292 break; 293 } 294 #ifdef INET6 295 case NETFLOW_V9_FLOW_V6_L4: 296 { 297 /* IPv6 TCP/UDP/[SCTP] */ 298 struct netflow_v9_record_ipv6_tcp *rec = &rg.rec.v6_tcp; 299 300 rec->src_addr = fed6->r.src.r_src6; 301 rec->dst_addr = fed6->r.dst.r_dst6; 302 rec->next_hop = fed6->n.next_hop6; 303 rec->i_ifx = htons(fed6->fle_i_ifx); 304 rec->o_ifx = htons(fed6->fle_o_ifx); 305 rec->i_packets = htonl(fed6->packets); 306 rec->i_octets = htonl(fed6->bytes); 307 rec->o_packets = htonl(0); 308 rec->o_octets = htonl(0); 309 rec->first = htonl(MILLIUPTIME(fed6->first)); 310 rec->last = htonl(MILLIUPTIME(fed6->last)); 311 rec->s_port = fed6->r.r_sport; 312 rec->d_port = fed6->r.r_dport; 313 rec->flags = fed6->tcp_flags; 314 rec->prot = fed6->r.r_ip_p; 315 rec->tos = fed6->r.r_tos; 316 rec->dst_mask = fed6->dst_mask; 317 rec->src_mask = fed6->src_mask; 318 319 /* Not supported fields. */ 320 rec->src_as = rec->dst_as = 0; 321 322 len = sizeof(struct netflow_v9_record_ipv6_tcp); 323 break; 324 } 325 #endif 326 default: 327 { 328 CTR1(KTR_NET, "export9_add(): Don't know what to do with %d flow type!", flow_type); 329 return (0); 330 } 331 } 332 333 /* Check if new records has the same template */ 334 if (flow_type != t->flow_type) { 335 /* close old flowset */ 336 if (t->flow_type != 0) 337 close_flowset(m, t); 338 339 t->flow_type = flow_type; 340 t->flow_header = m_pktlen(m); 341 342 /* Generate data flowset ID */ 343 fsh.id = htons(NETFLOW_V9_MAX_RESERVED_FLOWSET + flow_type); 344 fsh.length = 0; 345 346 /* m_append should not fail since all data is already allocated */ 347 if (m_append(m, sizeof(fsh), (void *)&fsh) != 1) 348 panic("ng_netflow: m_append() failed"); 349 350 } 351 352 if (m_append(m, len, (void *)&rg.rec) != 1) 353 panic("ng_netflow: m_append() failed"); 354 355 t->count++; 356 357 if (m_pktlen(m) + sizeof(struct netflow_v9_record_general) + sizeof(struct netflow_v9_flowset_header) >= _NETFLOW_V9_MAX_SIZE(t->mtu)) 358 return (1); /* end of datagram */ 359 return (0); 360 } 361 362 /* 363 * Detach export datagram from fib instance, if there is any. 364 * If there is no, allocate a new one. 365 */ 366 item_p 367 get_export9_dgram(priv_p priv, fib_export_p fe, struct netflow_v9_packet_opt **tt) 368 { 369 item_p item = NULL; 370 struct netflow_v9_packet_opt *t = NULL; 371 372 mtx_lock(&fe->export9_mtx); 373 if (fe->exp.item9 != NULL) { 374 item = fe->exp.item9; 375 fe->exp.item9 = NULL; 376 t = fe->exp.item9_opt; 377 fe->exp.item9_opt = NULL; 378 } 379 mtx_unlock(&fe->export9_mtx); 380 381 if (item == NULL) { 382 struct netflow_v9_export_dgram *dgram; 383 struct mbuf *m; 384 uint16_t mtu = priv->mtu; 385 386 /* Allocate entire packet at once, allowing easy m_append() calls */ 387 m = m_getm(NULL, mtu, M_NOWAIT, MT_DATA); 388 if (m == NULL) 389 return (NULL); 390 391 t = malloc(sizeof(struct netflow_v9_packet_opt), M_NETFLOW_GENERAL, M_NOWAIT | M_ZERO); 392 if (t == NULL) { 393 m_free(m); 394 return (NULL); 395 } 396 397 item = ng_package_data(m, NG_NOFLAGS); 398 if (item == NULL) { 399 free(t, M_NETFLOW_GENERAL); 400 return (NULL); 401 } 402 403 dgram = mtod(m, struct netflow_v9_export_dgram *); 404 dgram->header.count = 0; 405 dgram->header.version = htons(NETFLOW_V9); 406 /* Set mbuf current data length */ 407 m->m_len = m->m_pkthdr.len = sizeof(struct netflow_v9_header); 408 409 t->count = 0; 410 t->mtu = mtu; 411 t->flow_header = m->m_len; 412 413 /* 414 * Check if we need to insert templates into packet 415 */ 416 417 struct netflow_v9_flowset_header *fl; 418 419 if ((time_uptime >= priv->templ_time + fe->templ_last_ts) || 420 (fe->sent_packets >= priv->templ_packets + fe->templ_last_pkt)) { 421 fe->templ_last_ts = time_uptime; 422 fe->templ_last_pkt = fe->sent_packets; 423 424 fl = priv->v9_flowsets[0]; 425 m_append(m, ntohs(fl->length), (void *)fl); 426 t->flow_header = m->m_len; 427 t->count += priv->flowset_records[0]; 428 } 429 } 430 431 *tt = t; 432 return (item); 433 } 434 435 /* 436 * Re-attach incomplete datagram back to fib instance. 437 * If there is already another one, then send incomplete. 438 */ 439 void 440 return_export9_dgram(priv_p priv, fib_export_p fe, item_p item, struct netflow_v9_packet_opt *t, int flags) 441 { 442 /* 443 * It may happen on SMP, that some thread has already 444 * put its item there, in this case we bail out and 445 * send what we have to collector. 446 */ 447 mtx_lock(&fe->export9_mtx); 448 if (fe->exp.item9 == NULL) { 449 fe->exp.item9 = item; 450 fe->exp.item9_opt = t; 451 mtx_unlock(&fe->export9_mtx); 452 } else { 453 mtx_unlock(&fe->export9_mtx); 454 export9_send(priv, fe, item, t, flags); 455 } 456 } 457 458 /* Allocate memory and set up flow cache */ 459 void 460 ng_netflow_v9_cache_init(priv_p priv) 461 { 462 generate_v9_templates(priv); 463 464 priv->templ_time = NETFLOW_V9_MAX_TIME_TEMPL; 465 priv->templ_packets = NETFLOW_V9_MAX_PACKETS_TEMPL; 466 priv->mtu = BASE_MTU; 467 } 468 469 /* Free all flow cache memory. Called from ng_netflow_cache_flush() */ 470 void 471 ng_netflow_v9_cache_flush(priv_p priv) 472 { 473 int i; 474 475 /* Free flowsets*/ 476 for (i = 0; i < priv->flowsets_count; i++) 477 free(priv->v9_flowsets[i], M_NETFLOW_GENERAL); 478 } 479 480 /* Get a snapshot of NetFlow v9 settings */ 481 void 482 ng_netflow_copyv9info(priv_p priv, struct ng_netflow_v9info *i) 483 { 484 485 i->templ_time = priv->templ_time; 486 i->templ_packets = priv->templ_packets; 487 i->mtu = priv->mtu; 488 } 489