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