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