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