1209662Slstewart/*- 2215153Slstewart * Copyright (c) 2007-2009 3215153Slstewart * Swinburne University of Technology, Melbourne, Australia. 4209662Slstewart * Copyright (c) 2009-2010, The FreeBSD Foundation 5209662Slstewart * All rights reserved. 6209662Slstewart * 7209662Slstewart * Portions of this software were developed at the Centre for Advanced 8209662Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne, 9209662Slstewart * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. 10209662Slstewart * 11209662Slstewart * Redistribution and use in source and binary forms, with or without 12209662Slstewart * modification, are permitted provided that the following conditions 13209662Slstewart * are met: 14209662Slstewart * 1. Redistributions of source code must retain the above copyright 15209662Slstewart * notice, this list of conditions and the following disclaimer. 16209662Slstewart * 2. Redistributions in binary form must reproduce the above copyright 17209662Slstewart * notice, this list of conditions and the following disclaimer in the 18209662Slstewart * documentation and/or other materials provided with the distribution. 19209662Slstewart * 20209662Slstewart * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21209662Slstewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22209662Slstewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23209662Slstewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24209662Slstewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25209662Slstewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26209662Slstewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27209662Slstewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28209662Slstewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29209662Slstewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30209662Slstewart * SUCH DAMAGE. 31209662Slstewart */ 32209662Slstewart 33209662Slstewart/****************************************************** 34209662Slstewart * Statistical Information For TCP Research (SIFTR) 35209662Slstewart * 36209662Slstewart * A FreeBSD kernel module that adds very basic intrumentation to the 37209662Slstewart * TCP stack, allowing internal stats to be recorded to a log file 38209662Slstewart * for experimental, debugging and performance analysis purposes. 39209662Slstewart * 40209662Slstewart * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst 41220560Slstewart * working on the NewTCP research project at Swinburne University of 42220560Slstewart * Technology's Centre for Advanced Internet Architectures, Melbourne, 43220560Slstewart * Australia, which was made possible in part by a grant from the Cisco 44220560Slstewart * University Research Program Fund at Community Foundation Silicon Valley. 45220560Slstewart * More details are available at: 46209662Slstewart * http://caia.swin.edu.au/urp/newtcp/ 47209662Slstewart * 48209662Slstewart * Work on SIFTR v1.2.x was sponsored by the FreeBSD Foundation as part of 49209662Slstewart * the "Enhancing the FreeBSD TCP Implementation" project 2008-2009. 50209662Slstewart * More details are available at: 51209662Slstewart * http://www.freebsdfoundation.org/ 52209662Slstewart * http://caia.swin.edu.au/freebsd/etcp09/ 53209662Slstewart * 54209662Slstewart * Lawrence Stewart is the current maintainer, and all contact regarding 55209662Slstewart * SIFTR should be directed to him via email: lastewart@swin.edu.au 56209662Slstewart * 57209662Slstewart * Initial release date: June 2007 58213162Slstewart * Most recent update: September 2010 59209662Slstewart ******************************************************/ 60209662Slstewart 61209662Slstewart#include <sys/cdefs.h> 62209662Slstewart__FBSDID("$FreeBSD$"); 63209662Slstewart 64209662Slstewart#include <sys/param.h> 65209662Slstewart#include <sys/alq.h> 66209662Slstewart#include <sys/errno.h> 67209662Slstewart#include <sys/hash.h> 68209662Slstewart#include <sys/kernel.h> 69209662Slstewart#include <sys/kthread.h> 70209662Slstewart#include <sys/lock.h> 71209662Slstewart#include <sys/mbuf.h> 72209662Slstewart#include <sys/module.h> 73209662Slstewart#include <sys/mutex.h> 74209662Slstewart#include <sys/pcpu.h> 75209662Slstewart#include <sys/proc.h> 76209662Slstewart#include <sys/sbuf.h> 77209662Slstewart#include <sys/smp.h> 78209662Slstewart#include <sys/socket.h> 79209662Slstewart#include <sys/socketvar.h> 80209662Slstewart#include <sys/sysctl.h> 81209662Slstewart#include <sys/unistd.h> 82209662Slstewart 83209662Slstewart#include <net/if.h> 84209662Slstewart#include <net/pfil.h> 85209662Slstewart 86209662Slstewart#include <netinet/in.h> 87209662Slstewart#include <netinet/in_pcb.h> 88209662Slstewart#include <netinet/in_systm.h> 89209662Slstewart#include <netinet/in_var.h> 90209662Slstewart#include <netinet/ip.h> 91209662Slstewart#include <netinet/tcp_var.h> 92209662Slstewart 93209662Slstewart#ifdef SIFTR_IPV6 94209662Slstewart#include <netinet/ip6.h> 95209662Slstewart#include <netinet6/in6_pcb.h> 96209662Slstewart#endif /* SIFTR_IPV6 */ 97209662Slstewart 98209662Slstewart#include <machine/in_cksum.h> 99209662Slstewart 100209662Slstewart/* 101209662Slstewart * Three digit version number refers to X.Y.Z where: 102209662Slstewart * X is the major version number 103209662Slstewart * Y is bumped to mark backwards incompatible changes 104209662Slstewart * Z is bumped to mark backwards compatible changes 105209662Slstewart */ 106209662Slstewart#define V_MAJOR 1 107209662Slstewart#define V_BACKBREAK 2 108213162Slstewart#define V_BACKCOMPAT 4 109209662Slstewart#define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT)) 110209662Slstewart#define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \ 111209662Slstewart __XSTRING(V_BACKCOMPAT) 112209662Slstewart 113209662Slstewart#define HOOK 0 114209662Slstewart#define UNHOOK 1 115209662Slstewart#define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536 116209662Slstewart#define SYS_NAME "FreeBSD" 117209662Slstewart#define PACKET_TAG_SIFTR 100 118209662Slstewart#define PACKET_COOKIE_SIFTR 21749576 119209662Slstewart#define SIFTR_LOG_FILE_MODE 0644 120209662Slstewart#define SIFTR_DISABLE 0 121209662Slstewart#define SIFTR_ENABLE 1 122209662Slstewart 123209662Slstewart/* 124209662Slstewart * Hard upper limit on the length of log messages. Bump this up if you add new 125209662Slstewart * data fields such that the line length could exceed the below value. 126209662Slstewart */ 127209662Slstewart#define MAX_LOG_MSG_LEN 200 128209662Slstewart/* XXX: Make this a sysctl tunable. */ 129209662Slstewart#define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN) 130209662Slstewart 131209662Slstewart/* 132209662Slstewart * 1 byte for IP version 133209662Slstewart * IPv4: src/dst IP (4+4) + src/dst port (2+2) = 12 bytes 134209662Slstewart * IPv6: src/dst IP (16+16) + src/dst port (2+2) = 36 bytes 135209662Slstewart */ 136209662Slstewart#ifdef SIFTR_IPV6 137209662Slstewart#define FLOW_KEY_LEN 37 138209662Slstewart#else 139209662Slstewart#define FLOW_KEY_LEN 13 140209662Slstewart#endif 141209662Slstewart 142209662Slstewart#ifdef SIFTR_IPV6 143209662Slstewart#define SIFTR_IPMODE 6 144209662Slstewart#else 145209662Slstewart#define SIFTR_IPMODE 4 146209662Slstewart#endif 147209662Slstewart 148209662Slstewart/* useful macros */ 149209662Slstewart#define CAST_PTR_INT(X) (*((int*)(X))) 150209662Slstewart 151209662Slstewart#define UPPER_SHORT(X) (((X) & 0xFFFF0000) >> 16) 152209662Slstewart#define LOWER_SHORT(X) ((X) & 0x0000FFFF) 153209662Slstewart 154209662Slstewart#define FIRST_OCTET(X) (((X) & 0xFF000000) >> 24) 155209662Slstewart#define SECOND_OCTET(X) (((X) & 0x00FF0000) >> 16) 156209662Slstewart#define THIRD_OCTET(X) (((X) & 0x0000FF00) >> 8) 157209662Slstewart#define FOURTH_OCTET(X) ((X) & 0x000000FF) 158209662Slstewart 159220592Spluknetstatic MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR"); 160220592Spluknetstatic MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode", 161220592Spluknet "SIFTR pkt_node struct"); 162220592Spluknetstatic MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode", 163220592Spluknet "SIFTR flow_hash_node struct"); 164209662Slstewart 165209662Slstewart/* Used as links in the pkt manager queue. */ 166209662Slstewartstruct pkt_node { 167209662Slstewart /* Timestamp of pkt as noted in the pfil hook. */ 168209662Slstewart struct timeval tval; 169209662Slstewart /* Direction pkt is travelling; either PFIL_IN or PFIL_OUT. */ 170209662Slstewart uint8_t direction; 171209662Slstewart /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */ 172209662Slstewart uint8_t ipver; 173209662Slstewart /* Hash of the pkt which triggered the log message. */ 174209662Slstewart uint32_t hash; 175209662Slstewart /* Local/foreign IP address. */ 176209662Slstewart#ifdef SIFTR_IPV6 177209662Slstewart uint32_t ip_laddr[4]; 178209662Slstewart uint32_t ip_faddr[4]; 179209662Slstewart#else 180209662Slstewart uint8_t ip_laddr[4]; 181209662Slstewart uint8_t ip_faddr[4]; 182209662Slstewart#endif 183209662Slstewart /* Local TCP port. */ 184209662Slstewart uint16_t tcp_localport; 185209662Slstewart /* Foreign TCP port. */ 186209662Slstewart uint16_t tcp_foreignport; 187209662Slstewart /* Congestion Window (bytes). */ 188209662Slstewart u_long snd_cwnd; 189209662Slstewart /* Sending Window (bytes). */ 190209662Slstewart u_long snd_wnd; 191209662Slstewart /* Receive Window (bytes). */ 192209662Slstewart u_long rcv_wnd; 193212765Sandre /* Unused (was: Bandwidth Controlled Window (bytes)). */ 194209662Slstewart u_long snd_bwnd; 195209662Slstewart /* Slow Start Threshold (bytes). */ 196209662Slstewart u_long snd_ssthresh; 197209662Slstewart /* Current state of the TCP FSM. */ 198209662Slstewart int conn_state; 199209662Slstewart /* Max Segment Size (bytes). */ 200209662Slstewart u_int max_seg_size; 201209662Slstewart /* 202209662Slstewart * Smoothed RTT stored as found in the TCP control block 203209662Slstewart * in units of (TCP_RTT_SCALE*hz). 204209662Slstewart */ 205209662Slstewart int smoothed_rtt; 206209662Slstewart /* Is SACK enabled? */ 207209662Slstewart u_char sack_enabled; 208209662Slstewart /* Window scaling for snd window. */ 209209662Slstewart u_char snd_scale; 210209662Slstewart /* Window scaling for recv window. */ 211209662Slstewart u_char rcv_scale; 212209662Slstewart /* TCP control block flags. */ 213209662Slstewart u_int flags; 214209662Slstewart /* Retransmit timeout length. */ 215209662Slstewart int rxt_length; 216209662Slstewart /* Size of the TCP send buffer in bytes. */ 217209662Slstewart u_int snd_buf_hiwater; 218209662Slstewart /* Current num bytes in the send socket buffer. */ 219209662Slstewart u_int snd_buf_cc; 220209662Slstewart /* Size of the TCP receive buffer in bytes. */ 221209662Slstewart u_int rcv_buf_hiwater; 222209662Slstewart /* Current num bytes in the receive socket buffer. */ 223209662Slstewart u_int rcv_buf_cc; 224209662Slstewart /* Number of bytes inflight that we are waiting on ACKs for. */ 225209662Slstewart u_int sent_inflight_bytes; 226213162Slstewart /* Number of segments currently in the reassembly queue. */ 227213162Slstewart int t_segqlen; 228281174Shiren /* Flowid for the connection. */ 229281174Shiren u_int flowid; 230281174Shiren /* Flow type for the connection. */ 231281174Shiren u_int flowtype; 232209662Slstewart /* Link to next pkt_node in the list. */ 233209662Slstewart STAILQ_ENTRY(pkt_node) nodes; 234209662Slstewart}; 235209662Slstewart 236209662Slstewartstruct flow_hash_node 237209662Slstewart{ 238209662Slstewart uint16_t counter; 239209662Slstewart uint8_t key[FLOW_KEY_LEN]; 240209662Slstewart LIST_ENTRY(flow_hash_node) nodes; 241209662Slstewart}; 242209662Slstewart 243209662Slstewartstruct siftr_stats 244209662Slstewart{ 245209662Slstewart /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */ 246209662Slstewart uint64_t n_in; 247209662Slstewart uint64_t n_out; 248209662Slstewart /* # pkts skipped due to failed malloc calls. */ 249209662Slstewart uint32_t nskip_in_malloc; 250209662Slstewart uint32_t nskip_out_malloc; 251209662Slstewart /* # pkts skipped due to failed mtx acquisition. */ 252209662Slstewart uint32_t nskip_in_mtx; 253209662Slstewart uint32_t nskip_out_mtx; 254209662Slstewart /* # pkts skipped due to failed inpcb lookups. */ 255209662Slstewart uint32_t nskip_in_inpcb; 256209662Slstewart uint32_t nskip_out_inpcb; 257209662Slstewart /* # pkts skipped due to failed tcpcb lookups. */ 258209662Slstewart uint32_t nskip_in_tcpcb; 259209662Slstewart uint32_t nskip_out_tcpcb; 260209662Slstewart /* # pkts skipped due to stack reinjection. */ 261209662Slstewart uint32_t nskip_in_dejavu; 262209662Slstewart uint32_t nskip_out_dejavu; 263209662Slstewart}; 264209662Slstewart 265215701Sdimstatic DPCPU_DEFINE(struct siftr_stats, ss); 266209662Slstewart 267209662Slstewartstatic volatile unsigned int siftr_exit_pkt_manager_thread = 0; 268209662Slstewartstatic unsigned int siftr_enabled = 0; 269209662Slstewartstatic unsigned int siftr_pkts_per_log = 1; 270209662Slstewartstatic unsigned int siftr_generate_hashes = 0; 271209662Slstewart/* static unsigned int siftr_binary_log = 0; */ 272209662Slstewartstatic char siftr_logfile[PATH_MAX] = "/var/log/siftr.log"; 273273847Shselaskystatic char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log"; 274209662Slstewartstatic u_long siftr_hashmask; 275209662SlstewartSTAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue); 276209662SlstewartLIST_HEAD(listhead, flow_hash_node) *counter_hash; 277209662Slstewartstatic int wait_for_pkt; 278209662Slstewartstatic struct alq *siftr_alq = NULL; 279209662Slstewartstatic struct mtx siftr_pkt_queue_mtx; 280209662Slstewartstatic struct mtx siftr_pkt_mgr_mtx; 281209662Slstewartstatic struct thread *siftr_pkt_manager_thr = NULL; 282209662Slstewart/* 283209662Slstewart * pfil.h defines PFIL_IN as 1 and PFIL_OUT as 2, 284209662Slstewart * which we use as an index into this array. 285209662Slstewart */ 286209662Slstewartstatic char direction[3] = {'\0', 'i','o'}; 287209662Slstewart 288209662Slstewart/* Required function prototypes. */ 289209662Slstewartstatic int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS); 290209662Slstewartstatic int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS); 291209662Slstewart 292209662Slstewart 293209662Slstewart/* Declare the net.inet.siftr sysctl tree and populate it. */ 294209662Slstewart 295209662SlstewartSYSCTL_DECL(_net_inet_siftr); 296209662Slstewart 297209662SlstewartSYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW, NULL, 298209662Slstewart "siftr related settings"); 299209662Slstewart 300209662SlstewartSYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled, CTLTYPE_UINT|CTLFLAG_RW, 301209662Slstewart &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU", 302209662Slstewart "switch siftr module operations on/off"); 303209662Slstewart 304209662SlstewartSYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile, CTLTYPE_STRING|CTLFLAG_RW, 305273847Shselasky &siftr_logfile_shadow, sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler, 306209662Slstewart "A", "file to save siftr log messages to"); 307209662Slstewart 308209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW, 309209662Slstewart &siftr_pkts_per_log, 1, 310209662Slstewart "number of packets between generating a log message"); 311209662Slstewart 312209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW, 313209662Slstewart &siftr_generate_hashes, 0, 314209662Slstewart "enable packet hash generation"); 315209662Slstewart 316209662Slstewart/* XXX: TODO 317209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW, 318209662Slstewart &siftr_binary_log, 0, 319209662Slstewart "write log files in binary instead of ascii"); 320209662Slstewart*/ 321209662Slstewart 322209662Slstewart 323209662Slstewart/* Begin functions. */ 324209662Slstewart 325209662Slstewartstatic void 326209662Slstewartsiftr_process_pkt(struct pkt_node * pkt_node) 327209662Slstewart{ 328209662Slstewart struct flow_hash_node *hash_node; 329209662Slstewart struct listhead *counter_list; 330209662Slstewart struct siftr_stats *ss; 331209662Slstewart struct ale *log_buf; 332209662Slstewart uint8_t key[FLOW_KEY_LEN]; 333209662Slstewart uint8_t found_match, key_offset; 334209662Slstewart 335209662Slstewart hash_node = NULL; 336209662Slstewart ss = DPCPU_PTR(ss); 337209662Slstewart found_match = 0; 338209662Slstewart key_offset = 1; 339209662Slstewart 340209662Slstewart /* 341209662Slstewart * Create the key that will be used to create a hash index 342209662Slstewart * into our hash table. Our key consists of: 343209662Slstewart * ipversion, localip, localport, foreignip, foreignport 344209662Slstewart */ 345209662Slstewart key[0] = pkt_node->ipver; 346209662Slstewart memcpy(key + key_offset, &pkt_node->ip_laddr, 347209662Slstewart sizeof(pkt_node->ip_laddr)); 348209662Slstewart key_offset += sizeof(pkt_node->ip_laddr); 349209662Slstewart memcpy(key + key_offset, &pkt_node->tcp_localport, 350209662Slstewart sizeof(pkt_node->tcp_localport)); 351209662Slstewart key_offset += sizeof(pkt_node->tcp_localport); 352209662Slstewart memcpy(key + key_offset, &pkt_node->ip_faddr, 353209662Slstewart sizeof(pkt_node->ip_faddr)); 354209662Slstewart key_offset += sizeof(pkt_node->ip_faddr); 355209662Slstewart memcpy(key + key_offset, &pkt_node->tcp_foreignport, 356209662Slstewart sizeof(pkt_node->tcp_foreignport)); 357209662Slstewart 358209662Slstewart counter_list = counter_hash + 359209662Slstewart (hash32_buf(key, sizeof(key), 0) & siftr_hashmask); 360209662Slstewart 361209662Slstewart /* 362209662Slstewart * If the list is not empty i.e. the hash index has 363209662Slstewart * been used by another flow previously. 364209662Slstewart */ 365209662Slstewart if (LIST_FIRST(counter_list) != NULL) { 366209662Slstewart /* 367209662Slstewart * Loop through the hash nodes in the list. 368209662Slstewart * There should normally only be 1 hash node in the list, 369209662Slstewart * except if there have been collisions at the hash index 370209662Slstewart * computed by hash32_buf(). 371209662Slstewart */ 372209662Slstewart LIST_FOREACH(hash_node, counter_list, nodes) { 373209662Slstewart /* 374209662Slstewart * Check if the key for the pkt we are currently 375209662Slstewart * processing is the same as the key stored in the 376209662Slstewart * hash node we are currently processing. 377209662Slstewart * If they are the same, then we've found the 378209662Slstewart * hash node that stores the counter for the flow 379209662Slstewart * the pkt belongs to. 380209662Slstewart */ 381209662Slstewart if (memcmp(hash_node->key, key, sizeof(key)) == 0) { 382209662Slstewart found_match = 1; 383209662Slstewart break; 384209662Slstewart } 385209662Slstewart } 386209662Slstewart } 387209662Slstewart 388209662Slstewart /* If this flow hash hasn't been seen before or we have a collision. */ 389209662Slstewart if (hash_node == NULL || !found_match) { 390209662Slstewart /* Create a new hash node to store the flow's counter. */ 391209662Slstewart hash_node = malloc(sizeof(struct flow_hash_node), 392209662Slstewart M_SIFTR_HASHNODE, M_WAITOK); 393209662Slstewart 394209662Slstewart if (hash_node != NULL) { 395209662Slstewart /* Initialise our new hash node list entry. */ 396209662Slstewart hash_node->counter = 0; 397209662Slstewart memcpy(hash_node->key, key, sizeof(key)); 398209662Slstewart LIST_INSERT_HEAD(counter_list, hash_node, nodes); 399209662Slstewart } else { 400209662Slstewart /* Malloc failed. */ 401209662Slstewart if (pkt_node->direction == PFIL_IN) 402209662Slstewart ss->nskip_in_malloc++; 403209662Slstewart else 404209662Slstewart ss->nskip_out_malloc++; 405209662Slstewart 406209662Slstewart return; 407209662Slstewart } 408209662Slstewart } else if (siftr_pkts_per_log > 1) { 409209662Slstewart /* 410209662Slstewart * Taking the remainder of the counter divided 411209662Slstewart * by the current value of siftr_pkts_per_log 412209662Slstewart * and storing that in counter provides a neat 413209662Slstewart * way to modulate the frequency of log 414209662Slstewart * messages being written to the log file. 415209662Slstewart */ 416209662Slstewart hash_node->counter = (hash_node->counter + 1) % 417209662Slstewart siftr_pkts_per_log; 418209662Slstewart 419209662Slstewart /* 420209662Slstewart * If we have not seen enough packets since the last time 421209662Slstewart * we wrote a log message for this connection, return. 422209662Slstewart */ 423209662Slstewart if (hash_node->counter > 0) 424209662Slstewart return; 425209662Slstewart } 426209662Slstewart 427209662Slstewart log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK); 428209662Slstewart 429209662Slstewart if (log_buf == NULL) 430209662Slstewart return; /* Should only happen if the ALQ is shutting down. */ 431209662Slstewart 432209662Slstewart#ifdef SIFTR_IPV6 433209662Slstewart pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]); 434209662Slstewart pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]); 435209662Slstewart 436209662Slstewart if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */ 437209662Slstewart pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]); 438209662Slstewart pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]); 439209662Slstewart pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]); 440209662Slstewart pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]); 441209662Slstewart pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]); 442209662Slstewart pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]); 443209662Slstewart 444209662Slstewart /* Construct an IPv6 log message. */ 445209662Slstewart log_buf->ae_bytesused = snprintf(log_buf->ae_data, 446209662Slstewart MAX_LOG_MSG_LEN, 447209662Slstewart "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:" 448209662Slstewart "%x:%x:%x:%x:%x,%u,%ld,%ld,%ld,%ld,%ld,%u,%u,%u,%u,%u,%u," 449281174Shiren "%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n", 450209662Slstewart direction[pkt_node->direction], 451209662Slstewart pkt_node->hash, 452209662Slstewart pkt_node->tval.tv_sec, 453209662Slstewart pkt_node->tval.tv_usec, 454209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[0]), 455209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[0]), 456209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[1]), 457209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[1]), 458209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[2]), 459209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[2]), 460209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[3]), 461209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[3]), 462209662Slstewart ntohs(pkt_node->tcp_localport), 463209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[0]), 464209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[0]), 465209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[1]), 466209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[1]), 467209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[2]), 468209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[2]), 469209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[3]), 470209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[3]), 471209662Slstewart ntohs(pkt_node->tcp_foreignport), 472209662Slstewart pkt_node->snd_ssthresh, 473209662Slstewart pkt_node->snd_cwnd, 474209662Slstewart pkt_node->snd_bwnd, 475209662Slstewart pkt_node->snd_wnd, 476209662Slstewart pkt_node->rcv_wnd, 477209662Slstewart pkt_node->snd_scale, 478209662Slstewart pkt_node->rcv_scale, 479209662Slstewart pkt_node->conn_state, 480209662Slstewart pkt_node->max_seg_size, 481209662Slstewart pkt_node->smoothed_rtt, 482209662Slstewart pkt_node->sack_enabled, 483209662Slstewart pkt_node->flags, 484209662Slstewart pkt_node->rxt_length, 485209662Slstewart pkt_node->snd_buf_hiwater, 486209662Slstewart pkt_node->snd_buf_cc, 487209662Slstewart pkt_node->rcv_buf_hiwater, 488209662Slstewart pkt_node->rcv_buf_cc, 489213162Slstewart pkt_node->sent_inflight_bytes, 490281174Shiren pkt_node->t_segqlen, 491281174Shiren pkt_node->flowid, 492281174Shiren pkt_node->flowtype); 493209662Slstewart } else { /* IPv4 packet */ 494209662Slstewart pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]); 495209662Slstewart pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]); 496209662Slstewart pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]); 497209662Slstewart pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]); 498209662Slstewart pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]); 499209662Slstewart pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]); 500209662Slstewart pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]); 501209662Slstewart pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]); 502209662Slstewart#endif /* SIFTR_IPV6 */ 503209662Slstewart 504209662Slstewart /* Construct an IPv4 log message. */ 505209662Slstewart log_buf->ae_bytesused = snprintf(log_buf->ae_data, 506209662Slstewart MAX_LOG_MSG_LEN, 507209662Slstewart "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%ld,%ld," 508281174Shiren "%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u,%u,%u\n", 509209662Slstewart direction[pkt_node->direction], 510209662Slstewart pkt_node->hash, 511209662Slstewart (intmax_t)pkt_node->tval.tv_sec, 512209662Slstewart pkt_node->tval.tv_usec, 513209662Slstewart pkt_node->ip_laddr[0], 514209662Slstewart pkt_node->ip_laddr[1], 515209662Slstewart pkt_node->ip_laddr[2], 516209662Slstewart pkt_node->ip_laddr[3], 517209662Slstewart ntohs(pkt_node->tcp_localport), 518209662Slstewart pkt_node->ip_faddr[0], 519209662Slstewart pkt_node->ip_faddr[1], 520209662Slstewart pkt_node->ip_faddr[2], 521209662Slstewart pkt_node->ip_faddr[3], 522209662Slstewart ntohs(pkt_node->tcp_foreignport), 523209662Slstewart pkt_node->snd_ssthresh, 524209662Slstewart pkt_node->snd_cwnd, 525209662Slstewart pkt_node->snd_bwnd, 526209662Slstewart pkt_node->snd_wnd, 527209662Slstewart pkt_node->rcv_wnd, 528209662Slstewart pkt_node->snd_scale, 529209662Slstewart pkt_node->rcv_scale, 530209662Slstewart pkt_node->conn_state, 531209662Slstewart pkt_node->max_seg_size, 532209662Slstewart pkt_node->smoothed_rtt, 533209662Slstewart pkt_node->sack_enabled, 534209662Slstewart pkt_node->flags, 535209662Slstewart pkt_node->rxt_length, 536209662Slstewart pkt_node->snd_buf_hiwater, 537209662Slstewart pkt_node->snd_buf_cc, 538209662Slstewart pkt_node->rcv_buf_hiwater, 539209662Slstewart pkt_node->rcv_buf_cc, 540213162Slstewart pkt_node->sent_inflight_bytes, 541281174Shiren pkt_node->t_segqlen, 542281174Shiren pkt_node->flowid, 543281174Shiren pkt_node->flowtype); 544209662Slstewart#ifdef SIFTR_IPV6 545209662Slstewart } 546209662Slstewart#endif 547209662Slstewart 548209662Slstewart alq_post_flags(siftr_alq, log_buf, 0); 549209662Slstewart} 550209662Slstewart 551209662Slstewart 552209662Slstewartstatic void 553209662Slstewartsiftr_pkt_manager_thread(void *arg) 554209662Slstewart{ 555209662Slstewart STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue = 556209662Slstewart STAILQ_HEAD_INITIALIZER(tmp_pkt_queue); 557209662Slstewart struct pkt_node *pkt_node, *pkt_node_temp; 558209662Slstewart uint8_t draining; 559209662Slstewart 560209662Slstewart draining = 2; 561209662Slstewart 562209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 563209662Slstewart 564209662Slstewart /* draining == 0 when queue has been flushed and it's safe to exit. */ 565209662Slstewart while (draining) { 566209662Slstewart /* 567209662Slstewart * Sleep until we are signalled to wake because thread has 568209662Slstewart * been told to exit or until 1 tick has passed. 569209662Slstewart */ 570209662Slstewart mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait", 571209662Slstewart 1); 572209662Slstewart 573209662Slstewart /* Gain exclusive access to the pkt_node queue. */ 574209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 575209662Slstewart 576209662Slstewart /* 577209662Slstewart * Move pkt_queue to tmp_pkt_queue, which leaves 578209662Slstewart * pkt_queue empty and ready to receive more pkt_nodes. 579209662Slstewart */ 580209662Slstewart STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue); 581209662Slstewart 582209662Slstewart /* 583209662Slstewart * We've finished making changes to the list. Unlock it 584209662Slstewart * so the pfil hooks can continue queuing pkt_nodes. 585209662Slstewart */ 586209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 587209662Slstewart 588209662Slstewart /* 589209662Slstewart * We can't hold a mutex whilst calling siftr_process_pkt 590209662Slstewart * because ALQ might sleep waiting for buffer space. 591209662Slstewart */ 592209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 593209662Slstewart 594209662Slstewart /* Flush all pkt_nodes to the log file. */ 595209662Slstewart STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes, 596209662Slstewart pkt_node_temp) { 597209662Slstewart siftr_process_pkt(pkt_node); 598209662Slstewart STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes); 599209662Slstewart free(pkt_node, M_SIFTR_PKTNODE); 600209662Slstewart } 601209662Slstewart 602209662Slstewart KASSERT(STAILQ_EMPTY(&tmp_pkt_queue), 603209662Slstewart ("SIFTR tmp_pkt_queue not empty after flush")); 604209662Slstewart 605209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 606209662Slstewart 607209662Slstewart /* 608209662Slstewart * If siftr_exit_pkt_manager_thread gets set during the window 609209662Slstewart * where we are draining the tmp_pkt_queue above, there might 610209662Slstewart * still be pkts in pkt_queue that need to be drained. 611209662Slstewart * Allow one further iteration to occur after 612209662Slstewart * siftr_exit_pkt_manager_thread has been set to ensure 613209662Slstewart * pkt_queue is completely empty before we kill the thread. 614209662Slstewart * 615209662Slstewart * siftr_exit_pkt_manager_thread is set only after the pfil 616209662Slstewart * hooks have been removed, so only 1 extra iteration 617209662Slstewart * is needed to drain the queue. 618209662Slstewart */ 619209662Slstewart if (siftr_exit_pkt_manager_thread) 620209662Slstewart draining--; 621209662Slstewart } 622209662Slstewart 623209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 624209662Slstewart 625209662Slstewart /* Calls wakeup on this thread's struct thread ptr. */ 626209662Slstewart kthread_exit(); 627209662Slstewart} 628209662Slstewart 629209662Slstewart 630209662Slstewartstatic uint32_t 631209662Slstewarthash_pkt(struct mbuf *m, uint32_t offset) 632209662Slstewart{ 633209662Slstewart uint32_t hash; 634209662Slstewart 635209662Slstewart hash = 0; 636209662Slstewart 637209662Slstewart while (m != NULL && offset > m->m_len) { 638209662Slstewart /* 639209662Slstewart * The IP packet payload does not start in this mbuf, so 640209662Slstewart * need to figure out which mbuf it starts in and what offset 641209662Slstewart * into the mbuf's data region the payload starts at. 642209662Slstewart */ 643209662Slstewart offset -= m->m_len; 644209662Slstewart m = m->m_next; 645209662Slstewart } 646209662Slstewart 647209662Slstewart while (m != NULL) { 648209662Slstewart /* Ensure there is data in the mbuf */ 649209662Slstewart if ((m->m_len - offset) > 0) 650209662Slstewart hash = hash32_buf(m->m_data + offset, 651209662Slstewart m->m_len - offset, hash); 652209662Slstewart 653209662Slstewart m = m->m_next; 654209662Slstewart offset = 0; 655209662Slstewart } 656209662Slstewart 657209662Slstewart return (hash); 658209662Slstewart} 659209662Slstewart 660209662Slstewart 661209662Slstewart/* 662209662Slstewart * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that 663209662Slstewart * it's a reinjected packet and return. If it doesn't, tag the mbuf and return. 664209662Slstewart * Return value >0 means the caller should skip processing this mbuf. 665209662Slstewart */ 666209662Slstewartstatic inline int 667209662Slstewartsiftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss) 668209662Slstewart{ 669209662Slstewart if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL) 670209662Slstewart != NULL) { 671209662Slstewart if (dir == PFIL_IN) 672209662Slstewart ss->nskip_in_dejavu++; 673209662Slstewart else 674209662Slstewart ss->nskip_out_dejavu++; 675209662Slstewart 676209662Slstewart return (1); 677209662Slstewart } else { 678209662Slstewart struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR, 679209662Slstewart PACKET_TAG_SIFTR, 0, M_NOWAIT); 680209662Slstewart if (tag == NULL) { 681209662Slstewart if (dir == PFIL_IN) 682209662Slstewart ss->nskip_in_malloc++; 683209662Slstewart else 684209662Slstewart ss->nskip_out_malloc++; 685209662Slstewart 686209662Slstewart return (1); 687209662Slstewart } 688209662Slstewart 689209662Slstewart m_tag_prepend(m, tag); 690209662Slstewart } 691209662Slstewart 692209662Slstewart return (0); 693209662Slstewart} 694209662Slstewart 695209662Slstewart 696209662Slstewart/* 697209662Slstewart * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL 698209662Slstewart * otherwise. 699209662Slstewart */ 700209662Slstewartstatic inline struct inpcb * 701209662Slstewartsiftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport, 702209662Slstewart uint16_t dport, int dir, struct siftr_stats *ss) 703209662Slstewart{ 704209662Slstewart struct inpcb *inp; 705209662Slstewart 706209662Slstewart /* We need the tcbinfo lock. */ 707209662Slstewart INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 708209662Slstewart 709209662Slstewart if (dir == PFIL_IN) 710209662Slstewart inp = (ipver == INP_IPV4 ? 711222488Srwatson in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst, 712222488Srwatson dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif) 713209662Slstewart : 714209662Slstewart#ifdef SIFTR_IPV6 715222488Srwatson in6_pcblookup(&V_tcbinfo, 716209662Slstewart &((struct ip6_hdr *)ip)->ip6_src, sport, 717222488Srwatson &((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB, 718209662Slstewart m->m_pkthdr.rcvif) 719209662Slstewart#else 720209662Slstewart NULL 721209662Slstewart#endif 722209662Slstewart ); 723209662Slstewart 724209662Slstewart else 725209662Slstewart inp = (ipver == INP_IPV4 ? 726222488Srwatson in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src, 727222488Srwatson sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif) 728209662Slstewart : 729209662Slstewart#ifdef SIFTR_IPV6 730222488Srwatson in6_pcblookup(&V_tcbinfo, 731209662Slstewart &((struct ip6_hdr *)ip)->ip6_dst, dport, 732222488Srwatson &((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB, 733209662Slstewart m->m_pkthdr.rcvif) 734209662Slstewart#else 735209662Slstewart NULL 736209662Slstewart#endif 737209662Slstewart ); 738209662Slstewart 739209662Slstewart /* If we can't find the inpcb, bail. */ 740209662Slstewart if (inp == NULL) { 741209662Slstewart if (dir == PFIL_IN) 742209662Slstewart ss->nskip_in_inpcb++; 743209662Slstewart else 744209662Slstewart ss->nskip_out_inpcb++; 745209662Slstewart } 746209662Slstewart 747209662Slstewart return (inp); 748209662Slstewart} 749209662Slstewart 750209662Slstewart 751210203Slstewartstatic inline void 752210203Slstewartsiftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp, 753210203Slstewart int ipver, int dir, int inp_locally_locked) 754210203Slstewart{ 755210203Slstewart#ifdef SIFTR_IPV6 756210203Slstewart if (ipver == INP_IPV4) { 757210203Slstewart pn->ip_laddr[3] = inp->inp_laddr.s_addr; 758210203Slstewart pn->ip_faddr[3] = inp->inp_faddr.s_addr; 759210203Slstewart#else 760210203Slstewart *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr; 761210203Slstewart *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr; 762210203Slstewart#endif 763210203Slstewart#ifdef SIFTR_IPV6 764210203Slstewart } else { 765210203Slstewart pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0]; 766210203Slstewart pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1]; 767210203Slstewart pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2]; 768210203Slstewart pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3]; 769210203Slstewart pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0]; 770210203Slstewart pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1]; 771210203Slstewart pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2]; 772210203Slstewart pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3]; 773210203Slstewart } 774210203Slstewart#endif 775210203Slstewart pn->tcp_localport = inp->inp_lport; 776210203Slstewart pn->tcp_foreignport = inp->inp_fport; 777210203Slstewart pn->snd_cwnd = tp->snd_cwnd; 778210203Slstewart pn->snd_wnd = tp->snd_wnd; 779210203Slstewart pn->rcv_wnd = tp->rcv_wnd; 780212765Sandre pn->snd_bwnd = 0; /* Unused, kept for compat. */ 781210203Slstewart pn->snd_ssthresh = tp->snd_ssthresh; 782210203Slstewart pn->snd_scale = tp->snd_scale; 783210203Slstewart pn->rcv_scale = tp->rcv_scale; 784210203Slstewart pn->conn_state = tp->t_state; 785210203Slstewart pn->max_seg_size = tp->t_maxseg; 786210203Slstewart pn->smoothed_rtt = tp->t_srtt; 787210203Slstewart pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0; 788210203Slstewart pn->flags = tp->t_flags; 789210203Slstewart pn->rxt_length = tp->t_rxtcur; 790210203Slstewart pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat; 791210203Slstewart pn->snd_buf_cc = inp->inp_socket->so_snd.sb_cc; 792210203Slstewart pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat; 793210203Slstewart pn->rcv_buf_cc = inp->inp_socket->so_rcv.sb_cc; 794210203Slstewart pn->sent_inflight_bytes = tp->snd_max - tp->snd_una; 795213162Slstewart pn->t_segqlen = tp->t_segqlen; 796281174Shiren pn->flowid = inp->inp_flowid; 797281174Shiren pn->flowtype = inp->inp_flowtype; 798210203Slstewart 799210203Slstewart /* We've finished accessing the tcb so release the lock. */ 800210203Slstewart if (inp_locally_locked) 801210203Slstewart INP_RUNLOCK(inp); 802210203Slstewart 803210203Slstewart pn->ipver = ipver; 804210203Slstewart pn->direction = dir; 805210203Slstewart 806210203Slstewart /* 807210203Slstewart * Significantly more accurate than using getmicrotime(), but slower! 808210203Slstewart * Gives true microsecond resolution at the expense of a hit to 809210203Slstewart * maximum pps throughput processing when SIFTR is loaded and enabled. 810210203Slstewart */ 811210203Slstewart microtime(&pn->tval); 812210203Slstewart} 813210203Slstewart 814210203Slstewart 815209662Slstewart/* 816209662Slstewart * pfil hook that is called for each IPv4 packet making its way through the 817209662Slstewart * stack in either direction. 818209662Slstewart * The pfil subsystem holds a non-sleepable mutex somewhere when 819209662Slstewart * calling our hook function, so we can't sleep at all. 820209662Slstewart * It's very important to use the M_NOWAIT flag with all function calls 821209662Slstewart * that support it so that they won't sleep, otherwise you get a panic. 822209662Slstewart */ 823209662Slstewartstatic int 824209662Slstewartsiftr_chkpkt(void *arg, struct mbuf **m, struct ifnet *ifp, int dir, 825209662Slstewart struct inpcb *inp) 826209662Slstewart{ 827210203Slstewart struct pkt_node *pn; 828209662Slstewart struct ip *ip; 829209662Slstewart struct tcphdr *th; 830209662Slstewart struct tcpcb *tp; 831209662Slstewart struct siftr_stats *ss; 832209662Slstewart unsigned int ip_hl; 833210203Slstewart int inp_locally_locked; 834209662Slstewart 835209662Slstewart inp_locally_locked = 0; 836209662Slstewart ss = DPCPU_PTR(ss); 837209662Slstewart 838209662Slstewart /* 839209662Slstewart * m_pullup is not required here because ip_{input|output} 840209662Slstewart * already do the heavy lifting for us. 841209662Slstewart */ 842209662Slstewart 843209662Slstewart ip = mtod(*m, struct ip *); 844209662Slstewart 845209662Slstewart /* Only continue processing if the packet is TCP. */ 846209662Slstewart if (ip->ip_p != IPPROTO_TCP) 847209662Slstewart goto ret; 848209662Slstewart 849209662Slstewart /* 850209662Slstewart * If a kernel subsystem reinjects packets into the stack, our pfil 851209662Slstewart * hook will be called multiple times for the same packet. 852209662Slstewart * Make sure we only process unique packets. 853209662Slstewart */ 854209662Slstewart if (siftr_chkreinject(*m, dir, ss)) 855209662Slstewart goto ret; 856209662Slstewart 857209662Slstewart if (dir == PFIL_IN) 858209662Slstewart ss->n_in++; 859209662Slstewart else 860209662Slstewart ss->n_out++; 861209662Slstewart 862209662Slstewart /* 863209662Slstewart * Create a tcphdr struct starting at the correct offset 864209662Slstewart * in the IP packet. ip->ip_hl gives the ip header length 865209662Slstewart * in 4-byte words, so multiply it to get the size in bytes. 866209662Slstewart */ 867209662Slstewart ip_hl = (ip->ip_hl << 2); 868209662Slstewart th = (struct tcphdr *)((caddr_t)ip + ip_hl); 869209662Slstewart 870209662Slstewart /* 871209662Slstewart * If the pfil hooks don't provide a pointer to the 872209662Slstewart * inpcb, we need to find it ourselves and lock it. 873209662Slstewart */ 874209662Slstewart if (!inp) { 875209662Slstewart /* Find the corresponding inpcb for this pkt. */ 876209662Slstewart inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport, 877209662Slstewart th->th_dport, dir, ss); 878209662Slstewart 879209662Slstewart if (inp == NULL) 880209662Slstewart goto ret; 881209662Slstewart else 882209662Slstewart inp_locally_locked = 1; 883209662Slstewart } 884209662Slstewart 885209662Slstewart INP_LOCK_ASSERT(inp); 886209662Slstewart 887209662Slstewart /* Find the TCP control block that corresponds with this packet */ 888209662Slstewart tp = intotcpcb(inp); 889209662Slstewart 890209662Slstewart /* 891209662Slstewart * If we can't find the TCP control block (happens occasionaly for a 892209662Slstewart * packet sent during the shutdown phase of a TCP connection), 893209662Slstewart * or we're in the timewait state, bail 894209662Slstewart */ 895209662Slstewart if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) { 896209662Slstewart if (dir == PFIL_IN) 897209662Slstewart ss->nskip_in_tcpcb++; 898209662Slstewart else 899209662Slstewart ss->nskip_out_tcpcb++; 900209662Slstewart 901209662Slstewart goto inp_unlock; 902209662Slstewart } 903209662Slstewart 904210203Slstewart pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 905209662Slstewart 906210203Slstewart if (pn == NULL) { 907210203Slstewart if (dir == PFIL_IN) 908210203Slstewart ss->nskip_in_malloc++; 909210203Slstewart else 910210203Slstewart ss->nskip_out_malloc++; 911209662Slstewart 912210203Slstewart goto inp_unlock; 913210203Slstewart } 914209662Slstewart 915210203Slstewart siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked); 916209662Slstewart 917209662Slstewart if (siftr_generate_hashes) { 918209662Slstewart if ((*m)->m_pkthdr.csum_flags & CSUM_TCP) { 919209662Slstewart /* 920209662Slstewart * For outbound packets, the TCP checksum isn't 921209662Slstewart * calculated yet. This is a problem for our packet 922209662Slstewart * hashing as the receiver will calc a different hash 923209662Slstewart * to ours if we don't include the correct TCP checksum 924209662Slstewart * in the bytes being hashed. To work around this 925209662Slstewart * problem, we manually calc the TCP checksum here in 926209662Slstewart * software. We unset the CSUM_TCP flag so the lower 927209662Slstewart * layers don't recalc it. 928209662Slstewart */ 929209662Slstewart (*m)->m_pkthdr.csum_flags &= ~CSUM_TCP; 930209662Slstewart 931209662Slstewart /* 932209662Slstewart * Calculate the TCP checksum in software and assign 933209662Slstewart * to correct TCP header field, which will follow the 934209662Slstewart * packet mbuf down the stack. The trick here is that 935209662Slstewart * tcp_output() sets th->th_sum to the checksum of the 936209662Slstewart * pseudo header for us already. Because of the nature 937209662Slstewart * of the checksumming algorithm, we can sum over the 938209662Slstewart * entire IP payload (i.e. TCP header and data), which 939209662Slstewart * will include the already calculated pseduo header 940209662Slstewart * checksum, thus giving us the complete TCP checksum. 941209662Slstewart * 942209662Slstewart * To put it in simple terms, if checksum(1,2,3,4)=10, 943209662Slstewart * then checksum(1,2,3,4,5) == checksum(10,5). 944209662Slstewart * This property is what allows us to "cheat" and 945209662Slstewart * checksum only the IP payload which has the TCP 946209662Slstewart * th_sum field populated with the pseudo header's 947209662Slstewart * checksum, and not need to futz around checksumming 948209662Slstewart * pseudo header bytes and TCP header/data in one hit. 949209662Slstewart * Refer to RFC 1071 for more info. 950209662Slstewart * 951209662Slstewart * NB: in_cksum_skip(struct mbuf *m, int len, int skip) 952209662Slstewart * in_cksum_skip 2nd argument is NOT the number of 953209662Slstewart * bytes to read from the mbuf at "skip" bytes offset 954209662Slstewart * from the start of the mbuf (very counter intuitive!). 955209662Slstewart * The number of bytes to read is calculated internally 956209662Slstewart * by the function as len-skip i.e. to sum over the IP 957209662Slstewart * payload (TCP header + data) bytes, it is INCORRECT 958209662Slstewart * to call the function like this: 959209662Slstewart * in_cksum_skip(at, ip->ip_len - offset, offset) 960209662Slstewart * Rather, it should be called like this: 961209662Slstewart * in_cksum_skip(at, ip->ip_len, offset) 962209662Slstewart * which means read "ip->ip_len - offset" bytes from 963209662Slstewart * the mbuf cluster "at" at offset "offset" bytes from 964209662Slstewart * the beginning of the "at" mbuf's data pointer. 965209662Slstewart */ 966241913Sglebius th->th_sum = in_cksum_skip(*m, ntohs(ip->ip_len), 967241913Sglebius ip_hl); 968209662Slstewart } 969209662Slstewart 970209662Slstewart /* 971209662Slstewart * XXX: Having to calculate the checksum in software and then 972209662Slstewart * hash over all bytes is really inefficient. Would be nice to 973209662Slstewart * find a way to create the hash and checksum in the same pass 974209662Slstewart * over the bytes. 975209662Slstewart */ 976210203Slstewart pn->hash = hash_pkt(*m, ip_hl); 977209662Slstewart } 978209662Slstewart 979209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 980210203Slstewart STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 981209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 982209662Slstewart goto ret; 983209662Slstewart 984209662Slstewartinp_unlock: 985209662Slstewart if (inp_locally_locked) 986209662Slstewart INP_RUNLOCK(inp); 987209662Slstewart 988209662Slstewartret: 989209662Slstewart /* Returning 0 ensures pfil will not discard the pkt */ 990209662Slstewart return (0); 991209662Slstewart} 992209662Slstewart 993209662Slstewart 994209662Slstewart#ifdef SIFTR_IPV6 995209662Slstewartstatic int 996209662Slstewartsiftr_chkpkt6(void *arg, struct mbuf **m, struct ifnet *ifp, int dir, 997209662Slstewart struct inpcb *inp) 998209662Slstewart{ 999210203Slstewart struct pkt_node *pn; 1000209662Slstewart struct ip6_hdr *ip6; 1001209662Slstewart struct tcphdr *th; 1002209662Slstewart struct tcpcb *tp; 1003209662Slstewart struct siftr_stats *ss; 1004209662Slstewart unsigned int ip6_hl; 1005210203Slstewart int inp_locally_locked; 1006209662Slstewart 1007209662Slstewart inp_locally_locked = 0; 1008209662Slstewart ss = DPCPU_PTR(ss); 1009209662Slstewart 1010209662Slstewart /* 1011209662Slstewart * m_pullup is not required here because ip6_{input|output} 1012209662Slstewart * already do the heavy lifting for us. 1013209662Slstewart */ 1014209662Slstewart 1015209662Slstewart ip6 = mtod(*m, struct ip6_hdr *); 1016209662Slstewart 1017209662Slstewart /* 1018209662Slstewart * Only continue processing if the packet is TCP 1019209662Slstewart * XXX: We should follow the next header fields 1020209662Slstewart * as shown on Pg 6 RFC 2460, but right now we'll 1021209662Slstewart * only check pkts that have no extension headers. 1022209662Slstewart */ 1023209662Slstewart if (ip6->ip6_nxt != IPPROTO_TCP) 1024209662Slstewart goto ret6; 1025209662Slstewart 1026209662Slstewart /* 1027209662Slstewart * If a kernel subsystem reinjects packets into the stack, our pfil 1028209662Slstewart * hook will be called multiple times for the same packet. 1029209662Slstewart * Make sure we only process unique packets. 1030209662Slstewart */ 1031209662Slstewart if (siftr_chkreinject(*m, dir, ss)) 1032209662Slstewart goto ret6; 1033209662Slstewart 1034209662Slstewart if (dir == PFIL_IN) 1035209662Slstewart ss->n_in++; 1036209662Slstewart else 1037209662Slstewart ss->n_out++; 1038209662Slstewart 1039209662Slstewart ip6_hl = sizeof(struct ip6_hdr); 1040209662Slstewart 1041209662Slstewart /* 1042209662Slstewart * Create a tcphdr struct starting at the correct offset 1043209662Slstewart * in the ipv6 packet. ip->ip_hl gives the ip header length 1044209662Slstewart * in 4-byte words, so multiply it to get the size in bytes. 1045209662Slstewart */ 1046209662Slstewart th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl); 1047209662Slstewart 1048209662Slstewart /* 1049209662Slstewart * For inbound packets, the pfil hooks don't provide a pointer to the 1050209662Slstewart * inpcb, so we need to find it ourselves and lock it. 1051209662Slstewart */ 1052209662Slstewart if (!inp) { 1053209662Slstewart /* Find the corresponding inpcb for this pkt. */ 1054209662Slstewart inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m, 1055209662Slstewart th->th_sport, th->th_dport, dir, ss); 1056209662Slstewart 1057209662Slstewart if (inp == NULL) 1058209662Slstewart goto ret6; 1059209662Slstewart else 1060209662Slstewart inp_locally_locked = 1; 1061209662Slstewart } 1062209662Slstewart 1063209662Slstewart /* Find the TCP control block that corresponds with this packet. */ 1064209662Slstewart tp = intotcpcb(inp); 1065209662Slstewart 1066209662Slstewart /* 1067209662Slstewart * If we can't find the TCP control block (happens occasionaly for a 1068209662Slstewart * packet sent during the shutdown phase of a TCP connection), 1069209662Slstewart * or we're in the timewait state, bail. 1070209662Slstewart */ 1071209662Slstewart if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) { 1072209662Slstewart if (dir == PFIL_IN) 1073209662Slstewart ss->nskip_in_tcpcb++; 1074209662Slstewart else 1075209662Slstewart ss->nskip_out_tcpcb++; 1076209662Slstewart 1077209662Slstewart goto inp_unlock6; 1078209662Slstewart } 1079209662Slstewart 1080210203Slstewart pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 1081209662Slstewart 1082210203Slstewart if (pn == NULL) { 1083210203Slstewart if (dir == PFIL_IN) 1084210203Slstewart ss->nskip_in_malloc++; 1085210203Slstewart else 1086210203Slstewart ss->nskip_out_malloc++; 1087209662Slstewart 1088210203Slstewart goto inp_unlock6; 1089210203Slstewart } 1090209662Slstewart 1091210203Slstewart siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked); 1092209662Slstewart 1093210203Slstewart /* XXX: Figure out how to generate hashes for IPv6 packets. */ 1094209662Slstewart 1095209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 1096210203Slstewart STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 1097209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 1098209662Slstewart goto ret6; 1099209662Slstewart 1100209662Slstewartinp_unlock6: 1101209662Slstewart if (inp_locally_locked) 1102209662Slstewart INP_RUNLOCK(inp); 1103209662Slstewart 1104209662Slstewartret6: 1105209662Slstewart /* Returning 0 ensures pfil will not discard the pkt. */ 1106209662Slstewart return (0); 1107209662Slstewart} 1108209662Slstewart#endif /* #ifdef SIFTR_IPV6 */ 1109209662Slstewart 1110209662Slstewart 1111209662Slstewartstatic int 1112209662Slstewartsiftr_pfil(int action) 1113209662Slstewart{ 1114215552Slstewart struct pfil_head *pfh_inet; 1115209662Slstewart#ifdef SIFTR_IPV6 1116215552Slstewart struct pfil_head *pfh_inet6; 1117209662Slstewart#endif 1118215552Slstewart VNET_ITERATOR_DECL(vnet_iter); 1119209662Slstewart 1120215552Slstewart VNET_LIST_RLOCK(); 1121215552Slstewart VNET_FOREACH(vnet_iter) { 1122215552Slstewart CURVNET_SET(vnet_iter); 1123215552Slstewart pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 1124209662Slstewart#ifdef SIFTR_IPV6 1125215552Slstewart pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 1126209662Slstewart#endif 1127215552Slstewart 1128215552Slstewart if (action == HOOK) { 1129215552Slstewart pfil_add_hook(siftr_chkpkt, NULL, 1130215552Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1131209662Slstewart#ifdef SIFTR_IPV6 1132215552Slstewart pfil_add_hook(siftr_chkpkt6, NULL, 1133215552Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1134209662Slstewart#endif 1135215552Slstewart } else if (action == UNHOOK) { 1136215552Slstewart pfil_remove_hook(siftr_chkpkt, NULL, 1137215552Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1138215552Slstewart#ifdef SIFTR_IPV6 1139215552Slstewart pfil_remove_hook(siftr_chkpkt6, NULL, 1140215552Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1141215552Slstewart#endif 1142215552Slstewart } 1143215552Slstewart CURVNET_RESTORE(); 1144209662Slstewart } 1145215552Slstewart VNET_LIST_RUNLOCK(); 1146209662Slstewart 1147209662Slstewart return (0); 1148209662Slstewart} 1149209662Slstewart 1150209662Slstewart 1151209662Slstewartstatic int 1152209662Slstewartsiftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS) 1153209662Slstewart{ 1154209662Slstewart struct alq *new_alq; 1155209662Slstewart int error; 1156209662Slstewart 1157273847Shselasky error = sysctl_handle_string(oidp, arg1, arg2, req); 1158209662Slstewart 1159273847Shselasky /* Check for error or same filename */ 1160273847Shselasky if (error != 0 || req->newptr == NULL || 1161273847Shselasky strncmp(siftr_logfile, arg1, arg2) == 0) 1162273847Shselasky goto done; 1163209662Slstewart 1164273847Shselasky /* Filname changed */ 1165273847Shselasky error = alq_open(&new_alq, arg1, curthread->td_ucred, 1166273847Shselasky SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1167273847Shselasky if (error != 0) 1168273847Shselasky goto done; 1169209662Slstewart 1170273847Shselasky /* 1171273847Shselasky * If disabled, siftr_alq == NULL so we simply close 1172273847Shselasky * the alq as we've proved it can be opened. 1173273847Shselasky * If enabled, close the existing alq and switch the old 1174273847Shselasky * for the new. 1175273847Shselasky */ 1176273847Shselasky if (siftr_alq == NULL) { 1177273847Shselasky alq_close(new_alq); 1178273847Shselasky } else { 1179273847Shselasky alq_close(siftr_alq); 1180273847Shselasky siftr_alq = new_alq; 1181209662Slstewart } 1182209662Slstewart 1183273847Shselasky /* Update filename upon success */ 1184273847Shselasky strlcpy(siftr_logfile, arg1, arg2); 1185273847Shselaskydone: 1186273847Shselasky return (error); 1187209662Slstewart} 1188209662Slstewart 1189209662Slstewartstatic int 1190209662Slstewartsiftr_manage_ops(uint8_t action) 1191209662Slstewart{ 1192209662Slstewart struct siftr_stats totalss; 1193209662Slstewart struct timeval tval; 1194209662Slstewart struct flow_hash_node *counter, *tmp_counter; 1195209662Slstewart struct sbuf *s; 1196209662Slstewart int i, key_index, ret, error; 1197209662Slstewart uint32_t bytes_to_write, total_skipped_pkts; 1198209662Slstewart uint16_t lport, fport; 1199209662Slstewart uint8_t *key, ipver; 1200209662Slstewart 1201209662Slstewart#ifdef SIFTR_IPV6 1202209662Slstewart uint32_t laddr[4]; 1203209662Slstewart uint32_t faddr[4]; 1204209662Slstewart#else 1205209662Slstewart uint8_t laddr[4]; 1206209662Slstewart uint8_t faddr[4]; 1207209662Slstewart#endif 1208209662Slstewart 1209209662Slstewart error = 0; 1210209662Slstewart total_skipped_pkts = 0; 1211209662Slstewart 1212209662Slstewart /* Init an autosizing sbuf that initially holds 200 chars. */ 1213209662Slstewart if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL) 1214209662Slstewart return (-1); 1215209662Slstewart 1216209662Slstewart if (action == SIFTR_ENABLE) { 1217209662Slstewart /* 1218209662Slstewart * Create our alq 1219209662Slstewart * XXX: We should abort if alq_open fails! 1220209662Slstewart */ 1221209662Slstewart alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred, 1222209662Slstewart SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1223209662Slstewart 1224209662Slstewart STAILQ_INIT(&pkt_queue); 1225209662Slstewart 1226209982Slstewart DPCPU_ZERO(ss); 1227209982Slstewart 1228209662Slstewart siftr_exit_pkt_manager_thread = 0; 1229209662Slstewart 1230209662Slstewart ret = kthread_add(&siftr_pkt_manager_thread, NULL, NULL, 1231209662Slstewart &siftr_pkt_manager_thr, RFNOWAIT, 0, 1232209662Slstewart "siftr_pkt_manager_thr"); 1233209662Slstewart 1234209662Slstewart siftr_pfil(HOOK); 1235209662Slstewart 1236209662Slstewart microtime(&tval); 1237209662Slstewart 1238209662Slstewart sbuf_printf(s, 1239209662Slstewart "enable_time_secs=%jd\tenable_time_usecs=%06ld\t" 1240209662Slstewart "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t" 1241209662Slstewart "sysver=%u\tipmode=%u\n", 1242209662Slstewart (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz, 1243209662Slstewart TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE); 1244209662Slstewart 1245209662Slstewart sbuf_finish(s); 1246209662Slstewart alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK); 1247209662Slstewart 1248209662Slstewart } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) { 1249209662Slstewart /* 1250209662Slstewart * Remove the pfil hook functions. All threads currently in 1251209662Slstewart * the hook functions are allowed to exit before siftr_pfil() 1252209662Slstewart * returns. 1253209662Slstewart */ 1254209662Slstewart siftr_pfil(UNHOOK); 1255209662Slstewart 1256209662Slstewart /* This will block until the pkt manager thread unlocks it. */ 1257209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 1258209662Slstewart 1259209662Slstewart /* Tell the pkt manager thread that it should exit now. */ 1260209662Slstewart siftr_exit_pkt_manager_thread = 1; 1261209662Slstewart 1262209662Slstewart /* 1263209662Slstewart * Wake the pkt_manager thread so it realises that 1264209662Slstewart * siftr_exit_pkt_manager_thread == 1 and exits gracefully. 1265209662Slstewart * The wakeup won't be delivered until we unlock 1266209662Slstewart * siftr_pkt_mgr_mtx so this isn't racy. 1267209662Slstewart */ 1268209662Slstewart wakeup(&wait_for_pkt); 1269209662Slstewart 1270209662Slstewart /* Wait for the pkt_manager thread to exit. */ 1271209662Slstewart mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT, 1272209662Slstewart "thrwait", 0); 1273209662Slstewart 1274209662Slstewart siftr_pkt_manager_thr = NULL; 1275209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 1276209662Slstewart 1277209980Slstewart totalss.n_in = DPCPU_VARSUM(ss, n_in); 1278209980Slstewart totalss.n_out = DPCPU_VARSUM(ss, n_out); 1279209980Slstewart totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc); 1280209980Slstewart totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc); 1281209980Slstewart totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx); 1282209980Slstewart totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx); 1283209980Slstewart totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb); 1284209980Slstewart totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb); 1285209980Slstewart totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb); 1286209980Slstewart totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb); 1287209662Slstewart 1288209662Slstewart total_skipped_pkts = totalss.nskip_in_malloc + 1289209662Slstewart totalss.nskip_out_malloc + totalss.nskip_in_mtx + 1290209662Slstewart totalss.nskip_out_mtx + totalss.nskip_in_tcpcb + 1291209662Slstewart totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb + 1292209662Slstewart totalss.nskip_out_inpcb; 1293209662Slstewart 1294209662Slstewart microtime(&tval); 1295209662Slstewart 1296209662Slstewart sbuf_printf(s, 1297209662Slstewart "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t" 1298209662Slstewart "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t" 1299209662Slstewart "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t" 1300209662Slstewart "num_outbound_skipped_pkts_malloc=%u\t" 1301209662Slstewart "num_inbound_skipped_pkts_mtx=%u\t" 1302209662Slstewart "num_outbound_skipped_pkts_mtx=%u\t" 1303209662Slstewart "num_inbound_skipped_pkts_tcpcb=%u\t" 1304209662Slstewart "num_outbound_skipped_pkts_tcpcb=%u\t" 1305209662Slstewart "num_inbound_skipped_pkts_inpcb=%u\t" 1306209662Slstewart "num_outbound_skipped_pkts_inpcb=%u\t" 1307209662Slstewart "total_skipped_tcp_pkts=%u\tflow_list=", 1308209662Slstewart (intmax_t)tval.tv_sec, 1309209662Slstewart tval.tv_usec, 1310209662Slstewart (uintmax_t)totalss.n_in, 1311209662Slstewart (uintmax_t)totalss.n_out, 1312209662Slstewart (uintmax_t)(totalss.n_in + totalss.n_out), 1313209662Slstewart totalss.nskip_in_malloc, 1314209662Slstewart totalss.nskip_out_malloc, 1315209662Slstewart totalss.nskip_in_mtx, 1316209662Slstewart totalss.nskip_out_mtx, 1317209662Slstewart totalss.nskip_in_tcpcb, 1318209662Slstewart totalss.nskip_out_tcpcb, 1319209662Slstewart totalss.nskip_in_inpcb, 1320209662Slstewart totalss.nskip_out_inpcb, 1321209662Slstewart total_skipped_pkts); 1322209662Slstewart 1323209662Slstewart /* 1324209662Slstewart * Iterate over the flow hash, printing a summary of each 1325209662Slstewart * flow seen and freeing any malloc'd memory. 1326209662Slstewart * The hash consists of an array of LISTs (man 3 queue). 1327209662Slstewart */ 1328247906Slstewart for (i = 0; i <= siftr_hashmask; i++) { 1329209662Slstewart LIST_FOREACH_SAFE(counter, counter_hash + i, nodes, 1330209662Slstewart tmp_counter) { 1331209662Slstewart key = counter->key; 1332209662Slstewart key_index = 1; 1333209662Slstewart 1334209662Slstewart ipver = key[0]; 1335209662Slstewart 1336209662Slstewart memcpy(laddr, key + key_index, sizeof(laddr)); 1337209662Slstewart key_index += sizeof(laddr); 1338209662Slstewart memcpy(&lport, key + key_index, sizeof(lport)); 1339209662Slstewart key_index += sizeof(lport); 1340209662Slstewart memcpy(faddr, key + key_index, sizeof(faddr)); 1341209662Slstewart key_index += sizeof(faddr); 1342209662Slstewart memcpy(&fport, key + key_index, sizeof(fport)); 1343209662Slstewart 1344209662Slstewart#ifdef SIFTR_IPV6 1345209662Slstewart laddr[3] = ntohl(laddr[3]); 1346209662Slstewart faddr[3] = ntohl(faddr[3]); 1347209662Slstewart 1348209662Slstewart if (ipver == INP_IPV6) { 1349209662Slstewart laddr[0] = ntohl(laddr[0]); 1350209662Slstewart laddr[1] = ntohl(laddr[1]); 1351209662Slstewart laddr[2] = ntohl(laddr[2]); 1352209662Slstewart faddr[0] = ntohl(faddr[0]); 1353209662Slstewart faddr[1] = ntohl(faddr[1]); 1354209662Slstewart faddr[2] = ntohl(faddr[2]); 1355209662Slstewart 1356209662Slstewart sbuf_printf(s, 1357209662Slstewart "%x:%x:%x:%x:%x:%x:%x:%x;%u-" 1358209662Slstewart "%x:%x:%x:%x:%x:%x:%x:%x;%u,", 1359209662Slstewart UPPER_SHORT(laddr[0]), 1360209662Slstewart LOWER_SHORT(laddr[0]), 1361209662Slstewart UPPER_SHORT(laddr[1]), 1362209662Slstewart LOWER_SHORT(laddr[1]), 1363209662Slstewart UPPER_SHORT(laddr[2]), 1364209662Slstewart LOWER_SHORT(laddr[2]), 1365209662Slstewart UPPER_SHORT(laddr[3]), 1366209662Slstewart LOWER_SHORT(laddr[3]), 1367209662Slstewart ntohs(lport), 1368209662Slstewart UPPER_SHORT(faddr[0]), 1369209662Slstewart LOWER_SHORT(faddr[0]), 1370209662Slstewart UPPER_SHORT(faddr[1]), 1371209662Slstewart LOWER_SHORT(faddr[1]), 1372209662Slstewart UPPER_SHORT(faddr[2]), 1373209662Slstewart LOWER_SHORT(faddr[2]), 1374209662Slstewart UPPER_SHORT(faddr[3]), 1375209662Slstewart LOWER_SHORT(faddr[3]), 1376209662Slstewart ntohs(fport)); 1377209662Slstewart } else { 1378209662Slstewart laddr[0] = FIRST_OCTET(laddr[3]); 1379209662Slstewart laddr[1] = SECOND_OCTET(laddr[3]); 1380209662Slstewart laddr[2] = THIRD_OCTET(laddr[3]); 1381209662Slstewart laddr[3] = FOURTH_OCTET(laddr[3]); 1382209662Slstewart faddr[0] = FIRST_OCTET(faddr[3]); 1383209662Slstewart faddr[1] = SECOND_OCTET(faddr[3]); 1384209662Slstewart faddr[2] = THIRD_OCTET(faddr[3]); 1385209662Slstewart faddr[3] = FOURTH_OCTET(faddr[3]); 1386209662Slstewart#endif 1387209662Slstewart sbuf_printf(s, 1388209662Slstewart "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,", 1389209662Slstewart laddr[0], 1390209662Slstewart laddr[1], 1391209662Slstewart laddr[2], 1392209662Slstewart laddr[3], 1393209662Slstewart ntohs(lport), 1394209662Slstewart faddr[0], 1395209662Slstewart faddr[1], 1396209662Slstewart faddr[2], 1397209662Slstewart faddr[3], 1398209662Slstewart ntohs(fport)); 1399209662Slstewart#ifdef SIFTR_IPV6 1400209662Slstewart } 1401209662Slstewart#endif 1402209662Slstewart 1403209662Slstewart free(counter, M_SIFTR_HASHNODE); 1404209662Slstewart } 1405209662Slstewart 1406209662Slstewart LIST_INIT(counter_hash + i); 1407209662Slstewart } 1408209662Slstewart 1409209662Slstewart sbuf_printf(s, "\n"); 1410209662Slstewart sbuf_finish(s); 1411209662Slstewart 1412209662Slstewart i = 0; 1413209662Slstewart do { 1414209662Slstewart bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i); 1415209662Slstewart alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK); 1416209662Slstewart i += bytes_to_write; 1417209662Slstewart } while (i < sbuf_len(s)); 1418209662Slstewart 1419209662Slstewart alq_close(siftr_alq); 1420209662Slstewart siftr_alq = NULL; 1421209662Slstewart } 1422209662Slstewart 1423209662Slstewart sbuf_delete(s); 1424209662Slstewart 1425209662Slstewart /* 1426209662Slstewart * XXX: Should be using ret to check if any functions fail 1427209662Slstewart * and set error appropriately 1428209662Slstewart */ 1429209662Slstewart 1430209662Slstewart return (error); 1431209662Slstewart} 1432209662Slstewart 1433209662Slstewart 1434209662Slstewartstatic int 1435209662Slstewartsiftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS) 1436209662Slstewart{ 1437209662Slstewart if (req->newptr == NULL) 1438209662Slstewart goto skip; 1439209662Slstewart 1440209662Slstewart /* If the value passed in isn't 0 or 1, return an error. */ 1441209662Slstewart if (CAST_PTR_INT(req->newptr) != 0 && CAST_PTR_INT(req->newptr) != 1) 1442209662Slstewart return (1); 1443209662Slstewart 1444209662Slstewart /* If we are changing state (0 to 1 or 1 to 0). */ 1445209662Slstewart if (CAST_PTR_INT(req->newptr) != siftr_enabled ) 1446209662Slstewart if (siftr_manage_ops(CAST_PTR_INT(req->newptr))) { 1447209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1448209662Slstewart return (1); 1449209662Slstewart } 1450209662Slstewart 1451209662Slstewartskip: 1452209662Slstewart return (sysctl_handle_int(oidp, arg1, arg2, req)); 1453209662Slstewart} 1454209662Slstewart 1455209662Slstewart 1456209662Slstewartstatic void 1457209662Slstewartsiftr_shutdown_handler(void *arg) 1458209662Slstewart{ 1459209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1460209662Slstewart} 1461209662Slstewart 1462209662Slstewart 1463209662Slstewart/* 1464209662Slstewart * Module is being unloaded or machine is shutting down. Take care of cleanup. 1465209662Slstewart */ 1466209662Slstewartstatic int 1467209662Slstewartdeinit_siftr(void) 1468209662Slstewart{ 1469209662Slstewart /* Cleanup. */ 1470209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1471209662Slstewart hashdestroy(counter_hash, M_SIFTR, siftr_hashmask); 1472209662Slstewart mtx_destroy(&siftr_pkt_queue_mtx); 1473209662Slstewart mtx_destroy(&siftr_pkt_mgr_mtx); 1474209662Slstewart 1475209662Slstewart return (0); 1476209662Slstewart} 1477209662Slstewart 1478209662Slstewart 1479209662Slstewart/* 1480209662Slstewart * Module has just been loaded into the kernel. 1481209662Slstewart */ 1482209662Slstewartstatic int 1483209662Slstewartinit_siftr(void) 1484209662Slstewart{ 1485209662Slstewart EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL, 1486209662Slstewart SHUTDOWN_PRI_FIRST); 1487209662Slstewart 1488209662Slstewart /* Initialise our flow counter hash table. */ 1489209662Slstewart counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR, 1490209662Slstewart &siftr_hashmask); 1491209662Slstewart 1492209662Slstewart mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF); 1493209662Slstewart mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF); 1494209662Slstewart 1495209662Slstewart /* Print message to the user's current terminal. */ 1496209662Slstewart uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n" 1497209662Slstewart " http://caia.swin.edu.au/urp/newtcp\n\n", 1498209662Slstewart MODVERSION_STR); 1499209662Slstewart 1500209662Slstewart return (0); 1501209662Slstewart} 1502209662Slstewart 1503209662Slstewart 1504209662Slstewart/* 1505209662Slstewart * This is the function that is called to load and unload the module. 1506209662Slstewart * When the module is loaded, this function is called once with 1507209662Slstewart * "what" == MOD_LOAD 1508209662Slstewart * When the module is unloaded, this function is called twice with 1509209662Slstewart * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second 1510209662Slstewart * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command, 1511209662Slstewart * this function is called once with "what" = MOD_SHUTDOWN 1512209662Slstewart * When the system is shut down, the handler isn't called until the very end 1513209662Slstewart * of the shutdown sequence i.e. after the disks have been synced. 1514209662Slstewart */ 1515209662Slstewartstatic int 1516209662Slstewartsiftr_load_handler(module_t mod, int what, void *arg) 1517209662Slstewart{ 1518209662Slstewart int ret; 1519209662Slstewart 1520209662Slstewart switch (what) { 1521209662Slstewart case MOD_LOAD: 1522209662Slstewart ret = init_siftr(); 1523209662Slstewart break; 1524209662Slstewart 1525209662Slstewart case MOD_QUIESCE: 1526209662Slstewart case MOD_SHUTDOWN: 1527209662Slstewart ret = deinit_siftr(); 1528209662Slstewart break; 1529209662Slstewart 1530209662Slstewart case MOD_UNLOAD: 1531209662Slstewart ret = 0; 1532209662Slstewart break; 1533209662Slstewart 1534209662Slstewart default: 1535209662Slstewart ret = EINVAL; 1536209662Slstewart break; 1537209662Slstewart } 1538209662Slstewart 1539209662Slstewart return (ret); 1540209662Slstewart} 1541209662Slstewart 1542209662Slstewart 1543209662Slstewartstatic moduledata_t siftr_mod = { 1544209662Slstewart .name = "siftr", 1545209662Slstewart .evhand = siftr_load_handler, 1546209662Slstewart}; 1547209662Slstewart 1548209662Slstewart/* 1549209662Slstewart * Param 1: name of the kernel module 1550209662Slstewart * Param 2: moduledata_t struct containing info about the kernel module 1551209662Slstewart * and the execution entry point for the module 1552209662Slstewart * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h 1553209662Slstewart * Defines the module initialisation order 1554209662Slstewart * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h 1555209662Slstewart * Defines the initialisation order of this kld relative to others 1556209662Slstewart * within the same subsystem as defined by param 3 1557209662Slstewart */ 1558209662SlstewartDECLARE_MODULE(siftr, siftr_mod, SI_SUB_SMP, SI_ORDER_ANY); 1559209662SlstewartMODULE_DEPEND(siftr, alq, 1, 1, 1); 1560209662SlstewartMODULE_VERSION(siftr, MODVERSION); 1561