1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007-2009 5 * Swinburne University of Technology, Melbourne, Australia. 6 * Copyright (c) 2009-2010, The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * Portions of this software were developed at the Centre for Advanced 10 * Internet Architectures, Swinburne University of Technology, Melbourne, 11 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /****************************************************** 36 * Statistical Information For TCP Research (SIFTR) 37 * 38 * A FreeBSD kernel module that adds very basic intrumentation to the 39 * TCP stack, allowing internal stats to be recorded to a log file 40 * for experimental, debugging and performance analysis purposes. 41 * 42 * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst 43 * working on the NewTCP research project at Swinburne University of 44 * Technology's Centre for Advanced Internet Architectures, Melbourne, 45 * Australia, which was made possible in part by a grant from the Cisco 46 * University Research Program Fund at Community Foundation Silicon Valley. 47 * More details are available at: 48 * http://caia.swin.edu.au/urp/newtcp/ 49 * 50 * Work on SIFTR v1.2.x was sponsored by the FreeBSD Foundation as part of 51 * the "Enhancing the FreeBSD TCP Implementation" project 2008-2009. 52 * More details are available at: 53 * http://www.freebsdfoundation.org/ 54 * http://caia.swin.edu.au/freebsd/etcp09/ 55 * 56 * Lawrence Stewart is the current maintainer, and all contact regarding 57 * SIFTR should be directed to him via email: lastewart@swin.edu.au 58 * 59 * Initial release date: June 2007 60 * Most recent update: September 2010 61 ******************************************************/ 62 63 #include <sys/cdefs.h> 64 #include <sys/param.h> 65 #include <sys/alq.h> 66 #include <sys/errno.h> 67 #include <sys/eventhandler.h> 68 #include <sys/hash.h> 69 #include <sys/kernel.h> 70 #include <sys/kthread.h> 71 #include <sys/lock.h> 72 #include <sys/mbuf.h> 73 #include <sys/module.h> 74 #include <sys/mutex.h> 75 #include <sys/pcpu.h> 76 #include <sys/proc.h> 77 #include <sys/sbuf.h> 78 #include <sys/sdt.h> 79 #include <sys/smp.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/sysctl.h> 83 #include <sys/unistd.h> 84 85 #include <net/if.h> 86 #include <net/if_var.h> 87 #include <net/pfil.h> 88 #include <net/route.h> 89 90 #include <netinet/in.h> 91 #include <netinet/in_kdtrace.h> 92 #include <netinet/in_fib.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/in_var.h> 96 #include <netinet/ip.h> 97 #include <netinet/ip_var.h> 98 #include <netinet/tcp_var.h> 99 100 #ifdef SIFTR_IPV6 101 #include <netinet/ip6.h> 102 #include <netinet6/ip6_var.h> 103 #include <netinet6/in6_fib.h> 104 #include <netinet6/in6_pcb.h> 105 #endif /* SIFTR_IPV6 */ 106 107 #include <machine/in_cksum.h> 108 109 /* 110 * Three digit version number refers to X.Y.Z where: 111 * X is the major version number 112 * Y is bumped to mark backwards incompatible changes 113 * Z is bumped to mark backwards compatible changes 114 */ 115 #define V_MAJOR 1 116 #define V_BACKBREAK 3 117 #define V_BACKCOMPAT 0 118 #define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT)) 119 #define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \ 120 __XSTRING(V_BACKCOMPAT) 121 122 #define HOOK 0 123 #define UNHOOK 1 124 #define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536 125 #define SYS_NAME "FreeBSD" 126 #define PACKET_TAG_SIFTR 100 127 #define PACKET_COOKIE_SIFTR 21749576 128 #define SIFTR_LOG_FILE_MODE 0644 129 #define SIFTR_DISABLE 0 130 #define SIFTR_ENABLE 1 131 132 /* 133 * Hard upper limit on the length of log messages. Bump this up if you add new 134 * data fields such that the line length could exceed the below value. 135 */ 136 #define MAX_LOG_MSG_LEN 300 137 /* XXX: Make this a sysctl tunable. */ 138 #define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN) 139 140 #ifdef SIFTR_IPV6 141 #define SIFTR_IPMODE 6 142 #else 143 #define SIFTR_IPMODE 4 144 #endif 145 146 static MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR"); 147 static MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode", 148 "SIFTR pkt_node struct"); 149 static MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode", 150 "SIFTR flow_hash_node struct"); 151 152 /* Used as links in the pkt manager queue. */ 153 struct pkt_node { 154 /* Timestamp of pkt as noted in the pfil hook. */ 155 struct timeval tval; 156 /* Direction pkt is travelling. */ 157 enum { 158 DIR_IN = 0, 159 DIR_OUT = 1, 160 } direction; 161 /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */ 162 uint8_t ipver; 163 /* Local TCP port. */ 164 uint16_t lport; 165 /* Foreign TCP port. */ 166 uint16_t fport; 167 /* Local address. */ 168 union in_dependaddr laddr; 169 /* Foreign address. */ 170 union in_dependaddr faddr; 171 /* Congestion Window (bytes). */ 172 uint32_t snd_cwnd; 173 /* Sending Window (bytes). */ 174 uint32_t snd_wnd; 175 /* Receive Window (bytes). */ 176 uint32_t rcv_wnd; 177 /* More tcpcb flags storage */ 178 uint32_t t_flags2; 179 /* Slow Start Threshold (bytes). */ 180 uint32_t snd_ssthresh; 181 /* Current state of the TCP FSM. */ 182 int conn_state; 183 /* Max Segment Size (bytes). */ 184 uint32_t mss; 185 /* Smoothed RTT (usecs). */ 186 uint32_t srtt; 187 /* Is SACK enabled? */ 188 u_char sack_enabled; 189 /* Window scaling for snd window. */ 190 u_char snd_scale; 191 /* Window scaling for recv window. */ 192 u_char rcv_scale; 193 /* TCP control block flags. */ 194 u_int t_flags; 195 /* Retransmission timeout (usec). */ 196 uint32_t rto; 197 /* Size of the TCP send buffer in bytes. */ 198 u_int snd_buf_hiwater; 199 /* Current num bytes in the send socket buffer. */ 200 u_int snd_buf_cc; 201 /* Size of the TCP receive buffer in bytes. */ 202 u_int rcv_buf_hiwater; 203 /* Current num bytes in the receive socket buffer. */ 204 u_int rcv_buf_cc; 205 /* Number of bytes inflight that we are waiting on ACKs for. */ 206 u_int sent_inflight_bytes; 207 /* Number of segments currently in the reassembly queue. */ 208 int t_segqlen; 209 /* Flowid for the connection. */ 210 u_int flowid; 211 /* Flow type for the connection. */ 212 u_int flowtype; 213 /* Link to next pkt_node in the list. */ 214 STAILQ_ENTRY(pkt_node) nodes; 215 }; 216 217 struct flow_info 218 { 219 #ifdef SIFTR_IPV6 220 char laddr[INET6_ADDRSTRLEN]; /* local IP address */ 221 char faddr[INET6_ADDRSTRLEN]; /* foreign IP address */ 222 #else 223 char laddr[INET_ADDRSTRLEN]; /* local IP address */ 224 char faddr[INET_ADDRSTRLEN]; /* foreign IP address */ 225 #endif 226 uint16_t lport; /* local TCP port */ 227 uint16_t fport; /* foreign TCP port */ 228 uint32_t key; /* flowid of the connection */ 229 }; 230 231 struct flow_hash_node 232 { 233 uint16_t counter; 234 struct flow_info const_info; /* constant connection info */ 235 LIST_ENTRY(flow_hash_node) nodes; 236 }; 237 238 struct siftr_stats 239 { 240 /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */ 241 uint64_t n_in; 242 uint64_t n_out; 243 /* # pkts skipped due to failed malloc calls. */ 244 uint32_t nskip_in_malloc; 245 uint32_t nskip_out_malloc; 246 /* # pkts skipped due to failed inpcb lookups. */ 247 uint32_t nskip_in_inpcb; 248 uint32_t nskip_out_inpcb; 249 /* # pkts skipped due to failed tcpcb lookups. */ 250 uint32_t nskip_in_tcpcb; 251 uint32_t nskip_out_tcpcb; 252 /* # pkts skipped due to stack reinjection. */ 253 uint32_t nskip_in_dejavu; 254 uint32_t nskip_out_dejavu; 255 }; 256 257 DPCPU_DEFINE_STATIC(struct siftr_stats, ss); 258 259 static volatile unsigned int siftr_exit_pkt_manager_thread = 0; 260 static unsigned int siftr_enabled = 0; 261 static unsigned int siftr_pkts_per_log = 1; 262 static uint16_t siftr_port_filter = 0; 263 /* static unsigned int siftr_binary_log = 0; */ 264 static char siftr_logfile[PATH_MAX] = "/var/log/siftr.log"; 265 static char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log"; 266 static u_long siftr_hashmask; 267 STAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue); 268 LIST_HEAD(listhead, flow_hash_node) *counter_hash; 269 static int wait_for_pkt; 270 static struct alq *siftr_alq = NULL; 271 static struct mtx siftr_pkt_queue_mtx; 272 static struct mtx siftr_pkt_mgr_mtx; 273 static struct thread *siftr_pkt_manager_thr = NULL; 274 static char direction[2] = {'i','o'}; 275 276 /* Required function prototypes. */ 277 static int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS); 278 static int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS); 279 280 /* Declare the net.inet.siftr sysctl tree and populate it. */ 281 282 SYSCTL_DECL(_net_inet_siftr); 283 284 SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 285 "siftr related settings"); 286 287 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled, 288 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 289 &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU", 290 "switch siftr module operations on/off"); 291 292 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile, 293 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &siftr_logfile_shadow, 294 sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler, "A", 295 "file to save siftr log messages to"); 296 297 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW, 298 &siftr_pkts_per_log, 1, 299 "number of packets between generating a log message"); 300 301 SYSCTL_U16(_net_inet_siftr, OID_AUTO, port_filter, CTLFLAG_RW, 302 &siftr_port_filter, 0, 303 "enable packet filter on a TCP port"); 304 305 /* XXX: TODO 306 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW, 307 &siftr_binary_log, 0, 308 "write log files in binary instead of ascii"); 309 */ 310 311 /* Begin functions. */ 312 313 static inline struct flow_hash_node * 314 siftr_find_flow(struct listhead *counter_list, uint32_t id) 315 { 316 struct flow_hash_node *hash_node; 317 /* 318 * If the list is not empty i.e. the hash index has 319 * been used by another flow previously. 320 */ 321 if (LIST_FIRST(counter_list) != NULL) { 322 /* 323 * Loop through the hash nodes in the list. 324 * There should normally only be 1 hash node in the list. 325 */ 326 LIST_FOREACH(hash_node, counter_list, nodes) { 327 /* 328 * Check if the key for the pkt we are currently 329 * processing is the same as the key stored in the 330 * hash node we are currently processing. 331 * If they are the same, then we've found the 332 * hash node that stores the counter for the flow 333 * the pkt belongs to. 334 */ 335 if (hash_node->const_info.key == id) { 336 return hash_node; 337 } 338 } 339 } 340 341 return NULL; 342 } 343 344 static inline struct flow_hash_node * 345 siftr_new_hash_node(struct flow_info info, int dir, 346 struct siftr_stats *ss) 347 { 348 struct flow_hash_node *hash_node; 349 struct listhead *counter_list; 350 351 counter_list = counter_hash + (info.key & siftr_hashmask); 352 /* Create a new hash node to store the flow's constant info. */ 353 hash_node = malloc(sizeof(struct flow_hash_node), M_SIFTR_HASHNODE, 354 M_NOWAIT|M_ZERO); 355 356 if (hash_node != NULL) { 357 /* Initialise our new hash node list entry. */ 358 hash_node->counter = 0; 359 hash_node->const_info = info; 360 LIST_INSERT_HEAD(counter_list, hash_node, nodes); 361 return hash_node; 362 } else { 363 /* malloc failed */ 364 if (dir == DIR_IN) 365 ss->nskip_in_malloc++; 366 else 367 ss->nskip_out_malloc++; 368 369 return NULL; 370 } 371 } 372 373 static void 374 siftr_process_pkt(struct pkt_node * pkt_node) 375 { 376 struct flow_hash_node *hash_node; 377 struct listhead *counter_list; 378 struct ale *log_buf; 379 380 if (pkt_node->flowid == 0) { 381 panic("%s: flowid not available", __func__); 382 } 383 384 counter_list = counter_hash + (pkt_node->flowid & siftr_hashmask); 385 hash_node = siftr_find_flow(counter_list, pkt_node->flowid); 386 387 if (hash_node == NULL) { 388 return; 389 } else if (siftr_pkts_per_log > 1) { 390 /* 391 * Taking the remainder of the counter divided 392 * by the current value of siftr_pkts_per_log 393 * and storing that in counter provides a neat 394 * way to modulate the frequency of log 395 * messages being written to the log file. 396 */ 397 hash_node->counter = (hash_node->counter + 1) % 398 siftr_pkts_per_log; 399 /* 400 * If we have not seen enough packets since the last time 401 * we wrote a log message for this connection, return. 402 */ 403 if (hash_node->counter > 0) 404 return; 405 } 406 407 log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK); 408 409 if (log_buf == NULL) 410 return; /* Should only happen if the ALQ is shutting down. */ 411 412 /* Construct a log message. */ 413 log_buf->ae_bytesused = snprintf(log_buf->ae_data, MAX_LOG_MSG_LEN, 414 "%c,%jd.%06ld,%s,%hu,%s,%hu,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u," 415 "%u,%u,%u,%u,%u,%u,%u,%u\n", 416 direction[pkt_node->direction], 417 (intmax_t)pkt_node->tval.tv_sec, 418 pkt_node->tval.tv_usec, 419 hash_node->const_info.laddr, 420 hash_node->const_info.lport, 421 hash_node->const_info.faddr, 422 hash_node->const_info.fport, 423 pkt_node->snd_ssthresh, 424 pkt_node->snd_cwnd, 425 pkt_node->t_flags2, 426 pkt_node->snd_wnd, 427 pkt_node->rcv_wnd, 428 pkt_node->snd_scale, 429 pkt_node->rcv_scale, 430 pkt_node->conn_state, 431 pkt_node->mss, 432 pkt_node->srtt, 433 pkt_node->sack_enabled, 434 pkt_node->t_flags, 435 pkt_node->rto, 436 pkt_node->snd_buf_hiwater, 437 pkt_node->snd_buf_cc, 438 pkt_node->rcv_buf_hiwater, 439 pkt_node->rcv_buf_cc, 440 pkt_node->sent_inflight_bytes, 441 pkt_node->t_segqlen, 442 pkt_node->flowid, 443 pkt_node->flowtype); 444 445 alq_post_flags(siftr_alq, log_buf, 0); 446 } 447 448 static void 449 siftr_pkt_manager_thread(void *arg) 450 { 451 STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue = 452 STAILQ_HEAD_INITIALIZER(tmp_pkt_queue); 453 struct pkt_node *pkt_node, *pkt_node_temp; 454 uint8_t draining; 455 456 draining = 2; 457 458 mtx_lock(&siftr_pkt_mgr_mtx); 459 460 /* draining == 0 when queue has been flushed and it's safe to exit. */ 461 while (draining) { 462 /* 463 * Sleep until we are signalled to wake because thread has 464 * been told to exit or until 1 tick has passed. 465 */ 466 mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait", 467 1); 468 469 /* Gain exclusive access to the pkt_node queue. */ 470 mtx_lock(&siftr_pkt_queue_mtx); 471 472 /* 473 * Move pkt_queue to tmp_pkt_queue, which leaves 474 * pkt_queue empty and ready to receive more pkt_nodes. 475 */ 476 STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue); 477 478 /* 479 * We've finished making changes to the list. Unlock it 480 * so the pfil hooks can continue queuing pkt_nodes. 481 */ 482 mtx_unlock(&siftr_pkt_queue_mtx); 483 484 /* 485 * We can't hold a mutex whilst calling siftr_process_pkt 486 * because ALQ might sleep waiting for buffer space. 487 */ 488 mtx_unlock(&siftr_pkt_mgr_mtx); 489 490 /* Flush all pkt_nodes to the log file. */ 491 STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes, 492 pkt_node_temp) { 493 siftr_process_pkt(pkt_node); 494 STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes); 495 free(pkt_node, M_SIFTR_PKTNODE); 496 } 497 498 KASSERT(STAILQ_EMPTY(&tmp_pkt_queue), 499 ("SIFTR tmp_pkt_queue not empty after flush")); 500 501 mtx_lock(&siftr_pkt_mgr_mtx); 502 503 /* 504 * If siftr_exit_pkt_manager_thread gets set during the window 505 * where we are draining the tmp_pkt_queue above, there might 506 * still be pkts in pkt_queue that need to be drained. 507 * Allow one further iteration to occur after 508 * siftr_exit_pkt_manager_thread has been set to ensure 509 * pkt_queue is completely empty before we kill the thread. 510 * 511 * siftr_exit_pkt_manager_thread is set only after the pfil 512 * hooks have been removed, so only 1 extra iteration 513 * is needed to drain the queue. 514 */ 515 if (siftr_exit_pkt_manager_thread) 516 draining--; 517 } 518 519 mtx_unlock(&siftr_pkt_mgr_mtx); 520 521 /* Calls wakeup on this thread's struct thread ptr. */ 522 kthread_exit(); 523 } 524 525 /* 526 * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that 527 * it's a reinjected packet and return. If it doesn't, tag the mbuf and return. 528 * Return value >0 means the caller should skip processing this mbuf. 529 */ 530 static inline int 531 siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss) 532 { 533 if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL) 534 != NULL) { 535 if (dir == PFIL_IN) 536 ss->nskip_in_dejavu++; 537 else 538 ss->nskip_out_dejavu++; 539 540 return (1); 541 } else { 542 struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR, 543 PACKET_TAG_SIFTR, 0, M_NOWAIT); 544 if (tag == NULL) { 545 if (dir == PFIL_IN) 546 ss->nskip_in_malloc++; 547 else 548 ss->nskip_out_malloc++; 549 550 return (1); 551 } 552 553 m_tag_prepend(m, tag); 554 } 555 556 return (0); 557 } 558 559 /* 560 * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL 561 * otherwise. 562 */ 563 static inline struct inpcb * 564 siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport, 565 uint16_t dport, int dir, struct siftr_stats *ss) 566 { 567 struct inpcb *inp; 568 569 /* We need the tcbinfo lock. */ 570 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 571 572 if (dir == PFIL_IN) 573 inp = (ipver == INP_IPV4 ? 574 in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst, 575 dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif) 576 : 577 #ifdef SIFTR_IPV6 578 in6_pcblookup(&V_tcbinfo, 579 &((struct ip6_hdr *)ip)->ip6_src, sport, 580 &((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB, 581 m->m_pkthdr.rcvif) 582 #else 583 NULL 584 #endif 585 ); 586 587 else 588 inp = (ipver == INP_IPV4 ? 589 in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src, 590 sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif) 591 : 592 #ifdef SIFTR_IPV6 593 in6_pcblookup(&V_tcbinfo, 594 &((struct ip6_hdr *)ip)->ip6_dst, dport, 595 &((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB, 596 m->m_pkthdr.rcvif) 597 #else 598 NULL 599 #endif 600 ); 601 602 /* If we can't find the inpcb, bail. */ 603 if (inp == NULL) { 604 if (dir == PFIL_IN) 605 ss->nskip_in_inpcb++; 606 else 607 ss->nskip_out_inpcb++; 608 } 609 610 return (inp); 611 } 612 613 static inline uint32_t 614 siftr_get_flowid(struct inpcb *inp, int ipver, uint32_t *phashtype) 615 { 616 if (inp->inp_flowid == 0) { 617 #ifdef SIFTR_IPV6 618 if (ipver == INP_IPV6) { 619 return fib6_calc_packet_hash(&inp->in6p_laddr, 620 &inp->in6p_faddr, 621 inp->inp_lport, 622 inp->inp_fport, 623 IPPROTO_TCP, 624 phashtype); 625 } else 626 #endif 627 { 628 return fib4_calc_packet_hash(inp->inp_laddr, 629 inp->inp_faddr, 630 inp->inp_lport, 631 inp->inp_fport, 632 IPPROTO_TCP, 633 phashtype); 634 } 635 } else { 636 *phashtype = inp->inp_flowtype; 637 return inp->inp_flowid; 638 } 639 } 640 641 static inline void 642 siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp, 643 int ipver, int dir, int inp_locally_locked) 644 { 645 pn->ipver = ipver; 646 pn->lport = inp->inp_lport; 647 pn->fport = inp->inp_fport; 648 pn->laddr = inp->inp_inc.inc_ie.ie_dependladdr; 649 pn->faddr = inp->inp_inc.inc_ie.ie_dependfaddr; 650 pn->snd_cwnd = tp->snd_cwnd; 651 pn->snd_wnd = tp->snd_wnd; 652 pn->rcv_wnd = tp->rcv_wnd; 653 pn->t_flags2 = tp->t_flags2; 654 pn->snd_ssthresh = tp->snd_ssthresh; 655 pn->snd_scale = tp->snd_scale; 656 pn->rcv_scale = tp->rcv_scale; 657 pn->conn_state = tp->t_state; 658 pn->mss = tp->t_maxseg; 659 pn->srtt = ((uint64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 660 pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0; 661 pn->t_flags = tp->t_flags; 662 pn->rto = tp->t_rxtcur * tick; 663 pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat; 664 pn->snd_buf_cc = sbused(&inp->inp_socket->so_snd); 665 pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat; 666 pn->rcv_buf_cc = sbused(&inp->inp_socket->so_rcv); 667 pn->sent_inflight_bytes = tp->snd_max - tp->snd_una; 668 pn->t_segqlen = tp->t_segqlen; 669 670 /* We've finished accessing the tcb so release the lock. */ 671 if (inp_locally_locked) 672 INP_RUNLOCK(inp); 673 674 pn->direction = (dir == PFIL_IN ? DIR_IN : DIR_OUT); 675 676 /* 677 * Significantly more accurate than using getmicrotime(), but slower! 678 * Gives true microsecond resolution at the expense of a hit to 679 * maximum pps throughput processing when SIFTR is loaded and enabled. 680 */ 681 microtime(&pn->tval); 682 TCP_PROBE1(siftr, pn); 683 } 684 685 /* 686 * pfil hook that is called for each IPv4 packet making its way through the 687 * stack in either direction. 688 * The pfil subsystem holds a non-sleepable mutex somewhere when 689 * calling our hook function, so we can't sleep at all. 690 * It's very important to use the M_NOWAIT flag with all function calls 691 * that support it so that they won't sleep, otherwise you get a panic. 692 */ 693 static pfil_return_t 694 siftr_chkpkt(struct mbuf **m, struct ifnet *ifp, int flags, 695 void *ruleset __unused, struct inpcb *inp) 696 { 697 struct pkt_node *pn; 698 struct ip *ip; 699 struct tcphdr *th; 700 struct tcpcb *tp; 701 struct siftr_stats *ss; 702 unsigned int ip_hl; 703 int inp_locally_locked, dir; 704 uint32_t hash_id, hash_type; 705 struct listhead *counter_list; 706 struct flow_hash_node *hash_node; 707 708 inp_locally_locked = 0; 709 dir = PFIL_DIR(flags); 710 ss = DPCPU_PTR(ss); 711 712 /* 713 * m_pullup is not required here because ip_{input|output} 714 * already do the heavy lifting for us. 715 */ 716 717 ip = mtod(*m, struct ip *); 718 719 /* Only continue processing if the packet is TCP. */ 720 if (ip->ip_p != IPPROTO_TCP) 721 goto ret; 722 723 /* 724 * Create a tcphdr struct starting at the correct offset 725 * in the IP packet. ip->ip_hl gives the ip header length 726 * in 4-byte words, so multiply it to get the size in bytes. 727 */ 728 ip_hl = (ip->ip_hl << 2); 729 th = (struct tcphdr *)((caddr_t)ip + ip_hl); 730 731 /* 732 * Only pkts selected by the tcp port filter 733 * can be inserted into the pkt_queue 734 */ 735 if ((siftr_port_filter != 0) && 736 (siftr_port_filter != ntohs(th->th_sport)) && 737 (siftr_port_filter != ntohs(th->th_dport))) { 738 goto ret; 739 } 740 741 /* 742 * If a kernel subsystem reinjects packets into the stack, our pfil 743 * hook will be called multiple times for the same packet. 744 * Make sure we only process unique packets. 745 */ 746 if (siftr_chkreinject(*m, dir, ss)) 747 goto ret; 748 749 if (dir == PFIL_IN) 750 ss->n_in++; 751 else 752 ss->n_out++; 753 754 /* 755 * If the pfil hooks don't provide a pointer to the 756 * inpcb, we need to find it ourselves and lock it. 757 */ 758 if (!inp) { 759 /* Find the corresponding inpcb for this pkt. */ 760 inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport, 761 th->th_dport, dir, ss); 762 763 if (inp == NULL) 764 goto ret; 765 else 766 inp_locally_locked = 1; 767 } 768 769 INP_LOCK_ASSERT(inp); 770 771 /* Find the TCP control block that corresponds with this packet */ 772 tp = intotcpcb(inp); 773 774 /* 775 * If we can't find the TCP control block (happens occasionaly for a 776 * packet sent during the shutdown phase of a TCP connection), or the 777 * TCP control block has not initialized (happens during TCPS_SYN_SENT), 778 * bail. 779 */ 780 if (tp == NULL || tp->t_state < TCPS_ESTABLISHED) { 781 if (dir == PFIL_IN) 782 ss->nskip_in_tcpcb++; 783 else 784 ss->nskip_out_tcpcb++; 785 786 goto inp_unlock; 787 } 788 789 hash_id = siftr_get_flowid(inp, INP_IPV4, &hash_type); 790 counter_list = counter_hash + (hash_id & siftr_hashmask); 791 hash_node = siftr_find_flow(counter_list, hash_id); 792 793 /* If this flow hasn't been seen before, we create a new entry. */ 794 if (hash_node == NULL) { 795 struct flow_info info; 796 797 inet_ntoa_r(inp->inp_laddr, info.laddr); 798 inet_ntoa_r(inp->inp_faddr, info.faddr); 799 info.lport = ntohs(inp->inp_lport); 800 info.fport = ntohs(inp->inp_fport); 801 info.key = hash_id; 802 803 hash_node = siftr_new_hash_node(info, dir, ss); 804 } 805 806 if (hash_node == NULL) { 807 goto inp_unlock; 808 } 809 810 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 811 812 if (pn == NULL) { 813 if (dir == PFIL_IN) 814 ss->nskip_in_malloc++; 815 else 816 ss->nskip_out_malloc++; 817 818 goto inp_unlock; 819 } 820 821 pn->flowid = hash_id; 822 pn->flowtype = hash_type; 823 824 siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked); 825 826 mtx_lock(&siftr_pkt_queue_mtx); 827 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 828 mtx_unlock(&siftr_pkt_queue_mtx); 829 goto ret; 830 831 inp_unlock: 832 if (inp_locally_locked) 833 INP_RUNLOCK(inp); 834 835 ret: 836 return (PFIL_PASS); 837 } 838 839 #ifdef SIFTR_IPV6 840 static pfil_return_t 841 siftr_chkpkt6(struct mbuf **m, struct ifnet *ifp, int flags, 842 void *ruleset __unused, struct inpcb *inp) 843 { 844 struct pkt_node *pn; 845 struct ip6_hdr *ip6; 846 struct tcphdr *th; 847 struct tcpcb *tp; 848 struct siftr_stats *ss; 849 unsigned int ip6_hl; 850 int inp_locally_locked, dir; 851 uint32_t hash_id, hash_type; 852 struct listhead *counter_list; 853 struct flow_hash_node *hash_node; 854 855 inp_locally_locked = 0; 856 dir = PFIL_DIR(flags); 857 ss = DPCPU_PTR(ss); 858 859 /* 860 * m_pullup is not required here because ip6_{input|output} 861 * already do the heavy lifting for us. 862 */ 863 864 ip6 = mtod(*m, struct ip6_hdr *); 865 866 /* 867 * Only continue processing if the packet is TCP 868 * XXX: We should follow the next header fields 869 * as shown on Pg 6 RFC 2460, but right now we'll 870 * only check pkts that have no extension headers. 871 */ 872 if (ip6->ip6_nxt != IPPROTO_TCP) 873 goto ret6; 874 875 /* 876 * Create a tcphdr struct starting at the correct offset 877 * in the ipv6 packet. 878 */ 879 ip6_hl = sizeof(struct ip6_hdr); 880 th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl); 881 882 /* 883 * Only pkts selected by the tcp port filter 884 * can be inserted into the pkt_queue 885 */ 886 if ((siftr_port_filter != 0) && 887 (siftr_port_filter != ntohs(th->th_sport)) && 888 (siftr_port_filter != ntohs(th->th_dport))) { 889 goto ret6; 890 } 891 892 /* 893 * If a kernel subsystem reinjects packets into the stack, our pfil 894 * hook will be called multiple times for the same packet. 895 * Make sure we only process unique packets. 896 */ 897 if (siftr_chkreinject(*m, dir, ss)) 898 goto ret6; 899 900 if (dir == PFIL_IN) 901 ss->n_in++; 902 else 903 ss->n_out++; 904 905 /* 906 * For inbound packets, the pfil hooks don't provide a pointer to the 907 * inpcb, so we need to find it ourselves and lock it. 908 */ 909 if (!inp) { 910 /* Find the corresponding inpcb for this pkt. */ 911 inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m, 912 th->th_sport, th->th_dport, dir, ss); 913 914 if (inp == NULL) 915 goto ret6; 916 else 917 inp_locally_locked = 1; 918 } 919 920 /* Find the TCP control block that corresponds with this packet. */ 921 tp = intotcpcb(inp); 922 923 /* 924 * If we can't find the TCP control block (happens occasionaly for a 925 * packet sent during the shutdown phase of a TCP connection), or the 926 * TCP control block has not initialized (happens during TCPS_SYN_SENT), 927 * bail. 928 */ 929 if (tp == NULL || tp->t_state < TCPS_ESTABLISHED) { 930 if (dir == PFIL_IN) 931 ss->nskip_in_tcpcb++; 932 else 933 ss->nskip_out_tcpcb++; 934 935 goto inp_unlock6; 936 } 937 938 hash_id = siftr_get_flowid(inp, INP_IPV6, &hash_type); 939 counter_list = counter_hash + (hash_id & siftr_hashmask); 940 hash_node = siftr_find_flow(counter_list, hash_id); 941 942 /* If this flow hasn't been seen before, we create a new entry. */ 943 if (!hash_node) { 944 struct flow_info info; 945 946 ip6_sprintf(info.laddr, &inp->in6p_laddr); 947 ip6_sprintf(info.faddr, &inp->in6p_faddr); 948 info.lport = ntohs(inp->inp_lport); 949 info.fport = ntohs(inp->inp_fport); 950 info.key = hash_id; 951 952 hash_node = siftr_new_hash_node(info, dir, ss); 953 } 954 955 if (!hash_node) { 956 goto inp_unlock6; 957 } 958 959 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 960 961 if (pn == NULL) { 962 if (dir == PFIL_IN) 963 ss->nskip_in_malloc++; 964 else 965 ss->nskip_out_malloc++; 966 967 goto inp_unlock6; 968 } 969 970 pn->flowid = hash_id; 971 pn->flowtype = hash_type; 972 973 siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked); 974 975 mtx_lock(&siftr_pkt_queue_mtx); 976 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 977 mtx_unlock(&siftr_pkt_queue_mtx); 978 goto ret6; 979 980 inp_unlock6: 981 if (inp_locally_locked) 982 INP_RUNLOCK(inp); 983 984 ret6: 985 return (PFIL_PASS); 986 } 987 #endif /* #ifdef SIFTR_IPV6 */ 988 989 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet_hook); 990 #define V_siftr_inet_hook VNET(siftr_inet_hook) 991 #ifdef SIFTR_IPV6 992 VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet6_hook); 993 #define V_siftr_inet6_hook VNET(siftr_inet6_hook) 994 #endif 995 static int 996 siftr_pfil(int action) 997 { 998 struct pfil_hook_args pha = { 999 .pa_version = PFIL_VERSION, 1000 .pa_flags = PFIL_IN | PFIL_OUT, 1001 .pa_modname = "siftr", 1002 .pa_rulname = "default", 1003 }; 1004 struct pfil_link_args pla = { 1005 .pa_version = PFIL_VERSION, 1006 .pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR, 1007 }; 1008 1009 VNET_ITERATOR_DECL(vnet_iter); 1010 1011 VNET_LIST_RLOCK(); 1012 VNET_FOREACH(vnet_iter) { 1013 CURVNET_SET(vnet_iter); 1014 1015 if (action == HOOK) { 1016 pha.pa_mbuf_chk = siftr_chkpkt; 1017 pha.pa_type = PFIL_TYPE_IP4; 1018 V_siftr_inet_hook = pfil_add_hook(&pha); 1019 pla.pa_hook = V_siftr_inet_hook; 1020 pla.pa_head = V_inet_pfil_head; 1021 (void)pfil_link(&pla); 1022 #ifdef SIFTR_IPV6 1023 pha.pa_mbuf_chk = siftr_chkpkt6; 1024 pha.pa_type = PFIL_TYPE_IP6; 1025 V_siftr_inet6_hook = pfil_add_hook(&pha); 1026 pla.pa_hook = V_siftr_inet6_hook; 1027 pla.pa_head = V_inet6_pfil_head; 1028 (void)pfil_link(&pla); 1029 #endif 1030 } else if (action == UNHOOK) { 1031 pfil_remove_hook(V_siftr_inet_hook); 1032 #ifdef SIFTR_IPV6 1033 pfil_remove_hook(V_siftr_inet6_hook); 1034 #endif 1035 } 1036 CURVNET_RESTORE(); 1037 } 1038 VNET_LIST_RUNLOCK(); 1039 1040 return (0); 1041 } 1042 1043 static int 1044 siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS) 1045 { 1046 struct alq *new_alq; 1047 int error; 1048 1049 error = sysctl_handle_string(oidp, arg1, arg2, req); 1050 1051 /* Check for error or same filename */ 1052 if (error != 0 || req->newptr == NULL || 1053 strncmp(siftr_logfile, arg1, arg2) == 0) 1054 goto done; 1055 1056 /* file name changed */ 1057 error = alq_open(&new_alq, arg1, curthread->td_ucred, 1058 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1059 if (error != 0) 1060 goto done; 1061 1062 /* 1063 * If disabled, siftr_alq == NULL so we simply close 1064 * the alq as we've proved it can be opened. 1065 * If enabled, close the existing alq and switch the old 1066 * for the new. 1067 */ 1068 if (siftr_alq == NULL) { 1069 alq_close(new_alq); 1070 } else { 1071 alq_close(siftr_alq); 1072 siftr_alq = new_alq; 1073 } 1074 1075 /* Update filename upon success */ 1076 strlcpy(siftr_logfile, arg1, arg2); 1077 done: 1078 return (error); 1079 } 1080 1081 static int 1082 siftr_manage_ops(uint8_t action) 1083 { 1084 struct siftr_stats totalss; 1085 struct timeval tval; 1086 struct flow_hash_node *counter, *tmp_counter; 1087 struct sbuf *s; 1088 int i, error; 1089 uint32_t bytes_to_write, total_skipped_pkts; 1090 1091 error = 0; 1092 total_skipped_pkts = 0; 1093 1094 /* Init an autosizing sbuf that initially holds 200 chars. */ 1095 if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL) 1096 return (-1); 1097 1098 if (action == SIFTR_ENABLE && siftr_pkt_manager_thr == NULL) { 1099 /* 1100 * Create our alq 1101 * XXX: We should abort if alq_open fails! 1102 */ 1103 alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred, 1104 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1105 1106 STAILQ_INIT(&pkt_queue); 1107 1108 DPCPU_ZERO(ss); 1109 1110 siftr_exit_pkt_manager_thread = 0; 1111 1112 kthread_add(&siftr_pkt_manager_thread, NULL, NULL, 1113 &siftr_pkt_manager_thr, RFNOWAIT, 0, 1114 "siftr_pkt_manager_thr"); 1115 1116 siftr_pfil(HOOK); 1117 1118 microtime(&tval); 1119 1120 sbuf_printf(s, 1121 "enable_time_secs=%jd\tenable_time_usecs=%06ld\t" 1122 "siftrver=%s\tsysname=%s\tsysver=%u\tipmode=%u\n", 1123 (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, 1124 SYS_NAME, __FreeBSD_version, SIFTR_IPMODE); 1125 1126 sbuf_finish(s); 1127 alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK); 1128 1129 } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) { 1130 /* 1131 * Remove the pfil hook functions. All threads currently in 1132 * the hook functions are allowed to exit before siftr_pfil() 1133 * returns. 1134 */ 1135 siftr_pfil(UNHOOK); 1136 1137 /* This will block until the pkt manager thread unlocks it. */ 1138 mtx_lock(&siftr_pkt_mgr_mtx); 1139 1140 /* Tell the pkt manager thread that it should exit now. */ 1141 siftr_exit_pkt_manager_thread = 1; 1142 1143 /* 1144 * Wake the pkt_manager thread so it realises that 1145 * siftr_exit_pkt_manager_thread == 1 and exits gracefully. 1146 * The wakeup won't be delivered until we unlock 1147 * siftr_pkt_mgr_mtx so this isn't racy. 1148 */ 1149 wakeup(&wait_for_pkt); 1150 1151 /* Wait for the pkt_manager thread to exit. */ 1152 mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT, 1153 "thrwait", 0); 1154 1155 siftr_pkt_manager_thr = NULL; 1156 mtx_unlock(&siftr_pkt_mgr_mtx); 1157 1158 totalss.n_in = DPCPU_VARSUM(ss, n_in); 1159 totalss.n_out = DPCPU_VARSUM(ss, n_out); 1160 totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc); 1161 totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc); 1162 totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb); 1163 totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb); 1164 totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb); 1165 totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb); 1166 1167 total_skipped_pkts = totalss.nskip_in_malloc + 1168 totalss.nskip_out_malloc + totalss.nskip_in_tcpcb + 1169 totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb + 1170 totalss.nskip_out_inpcb; 1171 1172 microtime(&tval); 1173 1174 sbuf_printf(s, 1175 "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t" 1176 "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t" 1177 "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t" 1178 "num_outbound_skipped_pkts_malloc=%u\t" 1179 "num_inbound_skipped_pkts_tcpcb=%u\t" 1180 "num_outbound_skipped_pkts_tcpcb=%u\t" 1181 "num_inbound_skipped_pkts_inpcb=%u\t" 1182 "num_outbound_skipped_pkts_inpcb=%u\t" 1183 "total_skipped_tcp_pkts=%u\tflow_list=", 1184 (intmax_t)tval.tv_sec, 1185 tval.tv_usec, 1186 (uintmax_t)totalss.n_in, 1187 (uintmax_t)totalss.n_out, 1188 (uintmax_t)(totalss.n_in + totalss.n_out), 1189 totalss.nskip_in_malloc, 1190 totalss.nskip_out_malloc, 1191 totalss.nskip_in_tcpcb, 1192 totalss.nskip_out_tcpcb, 1193 totalss.nskip_in_inpcb, 1194 totalss.nskip_out_inpcb, 1195 total_skipped_pkts); 1196 1197 /* 1198 * Iterate over the flow hash, printing a summary of each 1199 * flow seen and freeing any malloc'd memory. 1200 * The hash consists of an array of LISTs (man 3 queue). 1201 */ 1202 for (i = 0; i <= siftr_hashmask; i++) { 1203 LIST_FOREACH_SAFE(counter, counter_hash + i, nodes, 1204 tmp_counter) { 1205 sbuf_printf(s, "%s;%hu-%s;%hu,", 1206 counter->const_info.laddr, 1207 counter->const_info.lport, 1208 counter->const_info.faddr, 1209 counter->const_info.fport); 1210 1211 free(counter, M_SIFTR_HASHNODE); 1212 } 1213 1214 LIST_INIT(counter_hash + i); 1215 } 1216 1217 sbuf_printf(s, "\n"); 1218 sbuf_finish(s); 1219 1220 i = 0; 1221 do { 1222 bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i); 1223 alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK); 1224 i += bytes_to_write; 1225 } while (i < sbuf_len(s)); 1226 1227 alq_close(siftr_alq); 1228 siftr_alq = NULL; 1229 } else 1230 error = EINVAL; 1231 1232 sbuf_delete(s); 1233 1234 /* 1235 * XXX: Should be using ret to check if any functions fail 1236 * and set error appropriately 1237 */ 1238 1239 return (error); 1240 } 1241 1242 static int 1243 siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS) 1244 { 1245 int error; 1246 uint32_t new; 1247 1248 new = siftr_enabled; 1249 error = sysctl_handle_int(oidp, &new, 0, req); 1250 if (error == 0 && req->newptr != NULL) { 1251 if (new > 1) 1252 return (EINVAL); 1253 else if (new != siftr_enabled) { 1254 if ((error = siftr_manage_ops(new)) == 0) { 1255 siftr_enabled = new; 1256 } else { 1257 siftr_manage_ops(SIFTR_DISABLE); 1258 } 1259 } 1260 } 1261 1262 return (error); 1263 } 1264 1265 static void 1266 siftr_shutdown_handler(void *arg) 1267 { 1268 if (siftr_enabled == 1) { 1269 siftr_manage_ops(SIFTR_DISABLE); 1270 } 1271 } 1272 1273 /* 1274 * Module is being unloaded or machine is shutting down. Take care of cleanup. 1275 */ 1276 static int 1277 deinit_siftr(void) 1278 { 1279 /* Cleanup. */ 1280 siftr_manage_ops(SIFTR_DISABLE); 1281 hashdestroy(counter_hash, M_SIFTR, siftr_hashmask); 1282 mtx_destroy(&siftr_pkt_queue_mtx); 1283 mtx_destroy(&siftr_pkt_mgr_mtx); 1284 1285 return (0); 1286 } 1287 1288 /* 1289 * Module has just been loaded into the kernel. 1290 */ 1291 static int 1292 init_siftr(void) 1293 { 1294 EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL, 1295 SHUTDOWN_PRI_FIRST); 1296 1297 /* Initialise our flow counter hash table. */ 1298 counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR, 1299 &siftr_hashmask); 1300 1301 mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF); 1302 mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF); 1303 1304 /* Print message to the user's current terminal. */ 1305 uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n" 1306 " http://caia.swin.edu.au/urp/newtcp\n\n", 1307 MODVERSION_STR); 1308 1309 return (0); 1310 } 1311 1312 /* 1313 * This is the function that is called to load and unload the module. 1314 * When the module is loaded, this function is called once with 1315 * "what" == MOD_LOAD 1316 * When the module is unloaded, this function is called twice with 1317 * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second 1318 * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command, 1319 * this function is called once with "what" = MOD_SHUTDOWN 1320 * When the system is shut down, the handler isn't called until the very end 1321 * of the shutdown sequence i.e. after the disks have been synced. 1322 */ 1323 static int 1324 siftr_load_handler(module_t mod, int what, void *arg) 1325 { 1326 int ret; 1327 1328 switch (what) { 1329 case MOD_LOAD: 1330 ret = init_siftr(); 1331 break; 1332 1333 case MOD_QUIESCE: 1334 case MOD_SHUTDOWN: 1335 ret = deinit_siftr(); 1336 break; 1337 1338 case MOD_UNLOAD: 1339 ret = 0; 1340 break; 1341 1342 default: 1343 ret = EINVAL; 1344 break; 1345 } 1346 1347 return (ret); 1348 } 1349 1350 static moduledata_t siftr_mod = { 1351 .name = "siftr", 1352 .evhand = siftr_load_handler, 1353 }; 1354 1355 /* 1356 * Param 1: name of the kernel module 1357 * Param 2: moduledata_t struct containing info about the kernel module 1358 * and the execution entry point for the module 1359 * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h 1360 * Defines the module initialisation order 1361 * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h 1362 * Defines the initialisation order of this kld relative to others 1363 * within the same subsystem as defined by param 3 1364 */ 1365 DECLARE_MODULE(siftr, siftr_mod, SI_SUB_LAST, SI_ORDER_ANY); 1366 MODULE_DEPEND(siftr, alq, 1, 1, 1); 1367 MODULE_VERSION(siftr, MODVERSION); 1368