siftr.c revision 213162
1209662Slstewart/*- 2209662Slstewart * Copyright (c) 2007-2009, Centre for Advanced Internet Architectures 3209662Slstewart * Swinburne University of Technology, Melbourne, Australia 4209662Slstewart * (CRICOS number 00111D). 5209662Slstewart * Copyright (c) 2009-2010, The FreeBSD Foundation 6209662Slstewart * All rights reserved. 7209662Slstewart * 8209662Slstewart * Portions of this software were developed at the Centre for Advanced 9209662Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne, 10209662Slstewart * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. 11209662Slstewart * 12209662Slstewart * Redistribution and use in source and binary forms, with or without 13209662Slstewart * modification, are permitted provided that the following conditions 14209662Slstewart * are met: 15209662Slstewart * 1. Redistributions of source code must retain the above copyright 16209662Slstewart * notice, this list of conditions and the following disclaimer. 17209662Slstewart * 2. Redistributions in binary form must reproduce the above copyright 18209662Slstewart * notice, this list of conditions and the following disclaimer in the 19209662Slstewart * documentation and/or other materials provided with the distribution. 20209662Slstewart * 21209662Slstewart * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 22209662Slstewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23209662Slstewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24209662Slstewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 25209662Slstewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26209662Slstewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27209662Slstewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28209662Slstewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29209662Slstewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30209662Slstewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31209662Slstewart * SUCH DAMAGE. 32209662Slstewart */ 33209662Slstewart 34209662Slstewart/****************************************************** 35209662Slstewart * Statistical Information For TCP Research (SIFTR) 36209662Slstewart * 37209662Slstewart * A FreeBSD kernel module that adds very basic intrumentation to the 38209662Slstewart * TCP stack, allowing internal stats to be recorded to a log file 39209662Slstewart * for experimental, debugging and performance analysis purposes. 40209662Slstewart * 41209662Slstewart * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst 42209662Slstewart * working on the NewTCP research project at Swinburne University's Centre for 43209662Slstewart * Advanced Internet Architectures, Melbourne, Australia, which was made 44209662Slstewart * possible in part by a grant from the Cisco University Research Program Fund 45209662Slstewart * at Community Foundation Silicon Valley. 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: head/sys/netinet/siftr.c 213162 2010-09-25 09:16:46Z lstewart $"); 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 159209662SlstewartMALLOC_DECLARE(M_SIFTR); 160209662SlstewartMALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR"); 161209662Slstewart 162209662SlstewartMALLOC_DECLARE(M_SIFTR_PKTNODE); 163209662SlstewartMALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode", "SIFTR pkt_node struct"); 164209662Slstewart 165209662SlstewartMALLOC_DECLARE(M_SIFTR_HASHNODE); 166209662SlstewartMALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode", "SIFTR flow_hash_node struct"); 167209662Slstewart 168209662Slstewart/* Used as links in the pkt manager queue. */ 169209662Slstewartstruct pkt_node { 170209662Slstewart /* Timestamp of pkt as noted in the pfil hook. */ 171209662Slstewart struct timeval tval; 172209662Slstewart /* Direction pkt is travelling; either PFIL_IN or PFIL_OUT. */ 173209662Slstewart uint8_t direction; 174209662Slstewart /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */ 175209662Slstewart uint8_t ipver; 176209662Slstewart /* Hash of the pkt which triggered the log message. */ 177209662Slstewart uint32_t hash; 178209662Slstewart /* Local/foreign IP address. */ 179209662Slstewart#ifdef SIFTR_IPV6 180209662Slstewart uint32_t ip_laddr[4]; 181209662Slstewart uint32_t ip_faddr[4]; 182209662Slstewart#else 183209662Slstewart uint8_t ip_laddr[4]; 184209662Slstewart uint8_t ip_faddr[4]; 185209662Slstewart#endif 186209662Slstewart /* Local TCP port. */ 187209662Slstewart uint16_t tcp_localport; 188209662Slstewart /* Foreign TCP port. */ 189209662Slstewart uint16_t tcp_foreignport; 190209662Slstewart /* Congestion Window (bytes). */ 191209662Slstewart u_long snd_cwnd; 192209662Slstewart /* Sending Window (bytes). */ 193209662Slstewart u_long snd_wnd; 194209662Slstewart /* Receive Window (bytes). */ 195209662Slstewart u_long rcv_wnd; 196212765Sandre /* Unused (was: Bandwidth Controlled Window (bytes)). */ 197209662Slstewart u_long snd_bwnd; 198209662Slstewart /* Slow Start Threshold (bytes). */ 199209662Slstewart u_long snd_ssthresh; 200209662Slstewart /* Current state of the TCP FSM. */ 201209662Slstewart int conn_state; 202209662Slstewart /* Max Segment Size (bytes). */ 203209662Slstewart u_int max_seg_size; 204209662Slstewart /* 205209662Slstewart * Smoothed RTT stored as found in the TCP control block 206209662Slstewart * in units of (TCP_RTT_SCALE*hz). 207209662Slstewart */ 208209662Slstewart int smoothed_rtt; 209209662Slstewart /* Is SACK enabled? */ 210209662Slstewart u_char sack_enabled; 211209662Slstewart /* Window scaling for snd window. */ 212209662Slstewart u_char snd_scale; 213209662Slstewart /* Window scaling for recv window. */ 214209662Slstewart u_char rcv_scale; 215209662Slstewart /* TCP control block flags. */ 216209662Slstewart u_int flags; 217209662Slstewart /* Retransmit timeout length. */ 218209662Slstewart int rxt_length; 219209662Slstewart /* Size of the TCP send buffer in bytes. */ 220209662Slstewart u_int snd_buf_hiwater; 221209662Slstewart /* Current num bytes in the send socket buffer. */ 222209662Slstewart u_int snd_buf_cc; 223209662Slstewart /* Size of the TCP receive buffer in bytes. */ 224209662Slstewart u_int rcv_buf_hiwater; 225209662Slstewart /* Current num bytes in the receive socket buffer. */ 226209662Slstewart u_int rcv_buf_cc; 227209662Slstewart /* Number of bytes inflight that we are waiting on ACKs for. */ 228209662Slstewart u_int sent_inflight_bytes; 229213162Slstewart /* Number of segments currently in the reassembly queue. */ 230213162Slstewart int t_segqlen; 231209662Slstewart /* Link to next pkt_node in the list. */ 232209662Slstewart STAILQ_ENTRY(pkt_node) nodes; 233209662Slstewart}; 234209662Slstewart 235209662Slstewartstruct flow_hash_node 236209662Slstewart{ 237209662Slstewart uint16_t counter; 238209662Slstewart uint8_t key[FLOW_KEY_LEN]; 239209662Slstewart LIST_ENTRY(flow_hash_node) nodes; 240209662Slstewart}; 241209662Slstewart 242209662Slstewartstruct siftr_stats 243209662Slstewart{ 244209662Slstewart /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */ 245209662Slstewart uint64_t n_in; 246209662Slstewart uint64_t n_out; 247209662Slstewart /* # pkts skipped due to failed malloc calls. */ 248209662Slstewart uint32_t nskip_in_malloc; 249209662Slstewart uint32_t nskip_out_malloc; 250209662Slstewart /* # pkts skipped due to failed mtx acquisition. */ 251209662Slstewart uint32_t nskip_in_mtx; 252209662Slstewart uint32_t nskip_out_mtx; 253209662Slstewart /* # pkts skipped due to failed inpcb lookups. */ 254209662Slstewart uint32_t nskip_in_inpcb; 255209662Slstewart uint32_t nskip_out_inpcb; 256209662Slstewart /* # pkts skipped due to failed tcpcb lookups. */ 257209662Slstewart uint32_t nskip_in_tcpcb; 258209662Slstewart uint32_t nskip_out_tcpcb; 259209662Slstewart /* # pkts skipped due to stack reinjection. */ 260209662Slstewart uint32_t nskip_in_dejavu; 261209662Slstewart uint32_t nskip_out_dejavu; 262209662Slstewart}; 263209662Slstewart 264209662Slstewartstatic DPCPU_DEFINE(struct siftr_stats, ss); 265209662Slstewart 266209662Slstewartstatic volatile unsigned int siftr_exit_pkt_manager_thread = 0; 267209662Slstewartstatic unsigned int siftr_enabled = 0; 268209662Slstewartstatic unsigned int siftr_pkts_per_log = 1; 269209662Slstewartstatic unsigned int siftr_generate_hashes = 0; 270209662Slstewart/* static unsigned int siftr_binary_log = 0; */ 271209662Slstewartstatic char siftr_logfile[PATH_MAX] = "/var/log/siftr.log"; 272209662Slstewartstatic u_long siftr_hashmask; 273209662SlstewartSTAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue); 274209662SlstewartLIST_HEAD(listhead, flow_hash_node) *counter_hash; 275209662Slstewartstatic int wait_for_pkt; 276209662Slstewartstatic struct alq *siftr_alq = NULL; 277209662Slstewartstatic struct mtx siftr_pkt_queue_mtx; 278209662Slstewartstatic struct mtx siftr_pkt_mgr_mtx; 279209662Slstewartstatic struct thread *siftr_pkt_manager_thr = NULL; 280209662Slstewart/* 281209662Slstewart * pfil.h defines PFIL_IN as 1 and PFIL_OUT as 2, 282209662Slstewart * which we use as an index into this array. 283209662Slstewart */ 284209662Slstewartstatic char direction[3] = {'\0', 'i','o'}; 285209662Slstewart 286209662Slstewart/* Required function prototypes. */ 287209662Slstewartstatic int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS); 288209662Slstewartstatic int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS); 289209662Slstewart 290209662Slstewart 291209662Slstewart/* Declare the net.inet.siftr sysctl tree and populate it. */ 292209662Slstewart 293209662SlstewartSYSCTL_DECL(_net_inet_siftr); 294209662Slstewart 295209662SlstewartSYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW, NULL, 296209662Slstewart "siftr related settings"); 297209662Slstewart 298209662SlstewartSYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled, CTLTYPE_UINT|CTLFLAG_RW, 299209662Slstewart &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU", 300209662Slstewart "switch siftr module operations on/off"); 301209662Slstewart 302209662SlstewartSYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile, CTLTYPE_STRING|CTLFLAG_RW, 303209662Slstewart &siftr_logfile, sizeof(siftr_logfile), &siftr_sysctl_logfile_name_handler, 304209662Slstewart "A", "file to save siftr log messages to"); 305209662Slstewart 306209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW, 307209662Slstewart &siftr_pkts_per_log, 1, 308209662Slstewart "number of packets between generating a log message"); 309209662Slstewart 310209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW, 311209662Slstewart &siftr_generate_hashes, 0, 312209662Slstewart "enable packet hash generation"); 313209662Slstewart 314209662Slstewart/* XXX: TODO 315209662SlstewartSYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW, 316209662Slstewart &siftr_binary_log, 0, 317209662Slstewart "write log files in binary instead of ascii"); 318209662Slstewart*/ 319209662Slstewart 320209662Slstewart 321209662Slstewart/* Begin functions. */ 322209662Slstewart 323209662Slstewartstatic void 324209662Slstewartsiftr_process_pkt(struct pkt_node * pkt_node) 325209662Slstewart{ 326209662Slstewart struct flow_hash_node *hash_node; 327209662Slstewart struct listhead *counter_list; 328209662Slstewart struct siftr_stats *ss; 329209662Slstewart struct ale *log_buf; 330209662Slstewart uint8_t key[FLOW_KEY_LEN]; 331209662Slstewart uint8_t found_match, key_offset; 332209662Slstewart 333209662Slstewart hash_node = NULL; 334209662Slstewart ss = DPCPU_PTR(ss); 335209662Slstewart found_match = 0; 336209662Slstewart key_offset = 1; 337209662Slstewart 338209662Slstewart /* 339209662Slstewart * Create the key that will be used to create a hash index 340209662Slstewart * into our hash table. Our key consists of: 341209662Slstewart * ipversion, localip, localport, foreignip, foreignport 342209662Slstewart */ 343209662Slstewart key[0] = pkt_node->ipver; 344209662Slstewart memcpy(key + key_offset, &pkt_node->ip_laddr, 345209662Slstewart sizeof(pkt_node->ip_laddr)); 346209662Slstewart key_offset += sizeof(pkt_node->ip_laddr); 347209662Slstewart memcpy(key + key_offset, &pkt_node->tcp_localport, 348209662Slstewart sizeof(pkt_node->tcp_localport)); 349209662Slstewart key_offset += sizeof(pkt_node->tcp_localport); 350209662Slstewart memcpy(key + key_offset, &pkt_node->ip_faddr, 351209662Slstewart sizeof(pkt_node->ip_faddr)); 352209662Slstewart key_offset += sizeof(pkt_node->ip_faddr); 353209662Slstewart memcpy(key + key_offset, &pkt_node->tcp_foreignport, 354209662Slstewart sizeof(pkt_node->tcp_foreignport)); 355209662Slstewart 356209662Slstewart counter_list = counter_hash + 357209662Slstewart (hash32_buf(key, sizeof(key), 0) & siftr_hashmask); 358209662Slstewart 359209662Slstewart /* 360209662Slstewart * If the list is not empty i.e. the hash index has 361209662Slstewart * been used by another flow previously. 362209662Slstewart */ 363209662Slstewart if (LIST_FIRST(counter_list) != NULL) { 364209662Slstewart /* 365209662Slstewart * Loop through the hash nodes in the list. 366209662Slstewart * There should normally only be 1 hash node in the list, 367209662Slstewart * except if there have been collisions at the hash index 368209662Slstewart * computed by hash32_buf(). 369209662Slstewart */ 370209662Slstewart LIST_FOREACH(hash_node, counter_list, nodes) { 371209662Slstewart /* 372209662Slstewart * Check if the key for the pkt we are currently 373209662Slstewart * processing is the same as the key stored in the 374209662Slstewart * hash node we are currently processing. 375209662Slstewart * If they are the same, then we've found the 376209662Slstewart * hash node that stores the counter for the flow 377209662Slstewart * the pkt belongs to. 378209662Slstewart */ 379209662Slstewart if (memcmp(hash_node->key, key, sizeof(key)) == 0) { 380209662Slstewart found_match = 1; 381209662Slstewart break; 382209662Slstewart } 383209662Slstewart } 384209662Slstewart } 385209662Slstewart 386209662Slstewart /* If this flow hash hasn't been seen before or we have a collision. */ 387209662Slstewart if (hash_node == NULL || !found_match) { 388209662Slstewart /* Create a new hash node to store the flow's counter. */ 389209662Slstewart hash_node = malloc(sizeof(struct flow_hash_node), 390209662Slstewart M_SIFTR_HASHNODE, M_WAITOK); 391209662Slstewart 392209662Slstewart if (hash_node != NULL) { 393209662Slstewart /* Initialise our new hash node list entry. */ 394209662Slstewart hash_node->counter = 0; 395209662Slstewart memcpy(hash_node->key, key, sizeof(key)); 396209662Slstewart LIST_INSERT_HEAD(counter_list, hash_node, nodes); 397209662Slstewart } else { 398209662Slstewart /* Malloc failed. */ 399209662Slstewart if (pkt_node->direction == PFIL_IN) 400209662Slstewart ss->nskip_in_malloc++; 401209662Slstewart else 402209662Slstewart ss->nskip_out_malloc++; 403209662Slstewart 404209662Slstewart return; 405209662Slstewart } 406209662Slstewart } else if (siftr_pkts_per_log > 1) { 407209662Slstewart /* 408209662Slstewart * Taking the remainder of the counter divided 409209662Slstewart * by the current value of siftr_pkts_per_log 410209662Slstewart * and storing that in counter provides a neat 411209662Slstewart * way to modulate the frequency of log 412209662Slstewart * messages being written to the log file. 413209662Slstewart */ 414209662Slstewart hash_node->counter = (hash_node->counter + 1) % 415209662Slstewart siftr_pkts_per_log; 416209662Slstewart 417209662Slstewart /* 418209662Slstewart * If we have not seen enough packets since the last time 419209662Slstewart * we wrote a log message for this connection, return. 420209662Slstewart */ 421209662Slstewart if (hash_node->counter > 0) 422209662Slstewart return; 423209662Slstewart } 424209662Slstewart 425209662Slstewart log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK); 426209662Slstewart 427209662Slstewart if (log_buf == NULL) 428209662Slstewart return; /* Should only happen if the ALQ is shutting down. */ 429209662Slstewart 430209662Slstewart#ifdef SIFTR_IPV6 431209662Slstewart pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]); 432209662Slstewart pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]); 433209662Slstewart 434209662Slstewart if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */ 435209662Slstewart pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]); 436209662Slstewart pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]); 437209662Slstewart pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]); 438209662Slstewart pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]); 439209662Slstewart pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]); 440209662Slstewart pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]); 441209662Slstewart 442209662Slstewart /* Construct an IPv6 log message. */ 443209662Slstewart log_buf->ae_bytesused = snprintf(log_buf->ae_data, 444209662Slstewart MAX_LOG_MSG_LEN, 445209662Slstewart "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:" 446209662Slstewart "%x:%x:%x:%x:%x,%u,%ld,%ld,%ld,%ld,%ld,%u,%u,%u,%u,%u,%u," 447213162Slstewart "%u,%d,%u,%u,%u,%u,%u,%u\n", 448209662Slstewart direction[pkt_node->direction], 449209662Slstewart pkt_node->hash, 450209662Slstewart pkt_node->tval.tv_sec, 451209662Slstewart pkt_node->tval.tv_usec, 452209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[0]), 453209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[0]), 454209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[1]), 455209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[1]), 456209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[2]), 457209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[2]), 458209662Slstewart UPPER_SHORT(pkt_node->ip_laddr[3]), 459209662Slstewart LOWER_SHORT(pkt_node->ip_laddr[3]), 460209662Slstewart ntohs(pkt_node->tcp_localport), 461209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[0]), 462209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[0]), 463209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[1]), 464209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[1]), 465209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[2]), 466209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[2]), 467209662Slstewart UPPER_SHORT(pkt_node->ip_faddr[3]), 468209662Slstewart LOWER_SHORT(pkt_node->ip_faddr[3]), 469209662Slstewart ntohs(pkt_node->tcp_foreignport), 470209662Slstewart pkt_node->snd_ssthresh, 471209662Slstewart pkt_node->snd_cwnd, 472209662Slstewart pkt_node->snd_bwnd, 473209662Slstewart pkt_node->snd_wnd, 474209662Slstewart pkt_node->rcv_wnd, 475209662Slstewart pkt_node->snd_scale, 476209662Slstewart pkt_node->rcv_scale, 477209662Slstewart pkt_node->conn_state, 478209662Slstewart pkt_node->max_seg_size, 479209662Slstewart pkt_node->smoothed_rtt, 480209662Slstewart pkt_node->sack_enabled, 481209662Slstewart pkt_node->flags, 482209662Slstewart pkt_node->rxt_length, 483209662Slstewart pkt_node->snd_buf_hiwater, 484209662Slstewart pkt_node->snd_buf_cc, 485209662Slstewart pkt_node->rcv_buf_hiwater, 486209662Slstewart pkt_node->rcv_buf_cc, 487213162Slstewart pkt_node->sent_inflight_bytes, 488213162Slstewart pkt_node->t_segqlen); 489209662Slstewart } else { /* IPv4 packet */ 490209662Slstewart pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]); 491209662Slstewart pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]); 492209662Slstewart pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]); 493209662Slstewart pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]); 494209662Slstewart pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]); 495209662Slstewart pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]); 496209662Slstewart pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]); 497209662Slstewart pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]); 498209662Slstewart#endif /* SIFTR_IPV6 */ 499209662Slstewart 500209662Slstewart /* Construct an IPv4 log message. */ 501209662Slstewart log_buf->ae_bytesused = snprintf(log_buf->ae_data, 502209662Slstewart MAX_LOG_MSG_LEN, 503209662Slstewart "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%ld,%ld," 504213162Slstewart "%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u\n", 505209662Slstewart direction[pkt_node->direction], 506209662Slstewart pkt_node->hash, 507209662Slstewart (intmax_t)pkt_node->tval.tv_sec, 508209662Slstewart pkt_node->tval.tv_usec, 509209662Slstewart pkt_node->ip_laddr[0], 510209662Slstewart pkt_node->ip_laddr[1], 511209662Slstewart pkt_node->ip_laddr[2], 512209662Slstewart pkt_node->ip_laddr[3], 513209662Slstewart ntohs(pkt_node->tcp_localport), 514209662Slstewart pkt_node->ip_faddr[0], 515209662Slstewart pkt_node->ip_faddr[1], 516209662Slstewart pkt_node->ip_faddr[2], 517209662Slstewart pkt_node->ip_faddr[3], 518209662Slstewart ntohs(pkt_node->tcp_foreignport), 519209662Slstewart pkt_node->snd_ssthresh, 520209662Slstewart pkt_node->snd_cwnd, 521209662Slstewart pkt_node->snd_bwnd, 522209662Slstewart pkt_node->snd_wnd, 523209662Slstewart pkt_node->rcv_wnd, 524209662Slstewart pkt_node->snd_scale, 525209662Slstewart pkt_node->rcv_scale, 526209662Slstewart pkt_node->conn_state, 527209662Slstewart pkt_node->max_seg_size, 528209662Slstewart pkt_node->smoothed_rtt, 529209662Slstewart pkt_node->sack_enabled, 530209662Slstewart pkt_node->flags, 531209662Slstewart pkt_node->rxt_length, 532209662Slstewart pkt_node->snd_buf_hiwater, 533209662Slstewart pkt_node->snd_buf_cc, 534209662Slstewart pkt_node->rcv_buf_hiwater, 535209662Slstewart pkt_node->rcv_buf_cc, 536213162Slstewart pkt_node->sent_inflight_bytes, 537213162Slstewart pkt_node->t_segqlen); 538209662Slstewart#ifdef SIFTR_IPV6 539209662Slstewart } 540209662Slstewart#endif 541209662Slstewart 542209662Slstewart alq_post_flags(siftr_alq, log_buf, 0); 543209662Slstewart} 544209662Slstewart 545209662Slstewart 546209662Slstewartstatic void 547209662Slstewartsiftr_pkt_manager_thread(void *arg) 548209662Slstewart{ 549209662Slstewart STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue = 550209662Slstewart STAILQ_HEAD_INITIALIZER(tmp_pkt_queue); 551209662Slstewart struct pkt_node *pkt_node, *pkt_node_temp; 552209662Slstewart uint8_t draining; 553209662Slstewart 554209662Slstewart draining = 2; 555209662Slstewart 556209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 557209662Slstewart 558209662Slstewart /* draining == 0 when queue has been flushed and it's safe to exit. */ 559209662Slstewart while (draining) { 560209662Slstewart /* 561209662Slstewart * Sleep until we are signalled to wake because thread has 562209662Slstewart * been told to exit or until 1 tick has passed. 563209662Slstewart */ 564209662Slstewart mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait", 565209662Slstewart 1); 566209662Slstewart 567209662Slstewart /* Gain exclusive access to the pkt_node queue. */ 568209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 569209662Slstewart 570209662Slstewart /* 571209662Slstewart * Move pkt_queue to tmp_pkt_queue, which leaves 572209662Slstewart * pkt_queue empty and ready to receive more pkt_nodes. 573209662Slstewart */ 574209662Slstewart STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue); 575209662Slstewart 576209662Slstewart /* 577209662Slstewart * We've finished making changes to the list. Unlock it 578209662Slstewart * so the pfil hooks can continue queuing pkt_nodes. 579209662Slstewart */ 580209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 581209662Slstewart 582209662Slstewart /* 583209662Slstewart * We can't hold a mutex whilst calling siftr_process_pkt 584209662Slstewart * because ALQ might sleep waiting for buffer space. 585209662Slstewart */ 586209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 587209662Slstewart 588209662Slstewart /* Flush all pkt_nodes to the log file. */ 589209662Slstewart STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes, 590209662Slstewart pkt_node_temp) { 591209662Slstewart siftr_process_pkt(pkt_node); 592209662Slstewart STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes); 593209662Slstewart free(pkt_node, M_SIFTR_PKTNODE); 594209662Slstewart } 595209662Slstewart 596209662Slstewart KASSERT(STAILQ_EMPTY(&tmp_pkt_queue), 597209662Slstewart ("SIFTR tmp_pkt_queue not empty after flush")); 598209662Slstewart 599209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 600209662Slstewart 601209662Slstewart /* 602209662Slstewart * If siftr_exit_pkt_manager_thread gets set during the window 603209662Slstewart * where we are draining the tmp_pkt_queue above, there might 604209662Slstewart * still be pkts in pkt_queue that need to be drained. 605209662Slstewart * Allow one further iteration to occur after 606209662Slstewart * siftr_exit_pkt_manager_thread has been set to ensure 607209662Slstewart * pkt_queue is completely empty before we kill the thread. 608209662Slstewart * 609209662Slstewart * siftr_exit_pkt_manager_thread is set only after the pfil 610209662Slstewart * hooks have been removed, so only 1 extra iteration 611209662Slstewart * is needed to drain the queue. 612209662Slstewart */ 613209662Slstewart if (siftr_exit_pkt_manager_thread) 614209662Slstewart draining--; 615209662Slstewart } 616209662Slstewart 617209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 618209662Slstewart 619209662Slstewart /* Calls wakeup on this thread's struct thread ptr. */ 620209662Slstewart kthread_exit(); 621209662Slstewart} 622209662Slstewart 623209662Slstewart 624209662Slstewartstatic uint32_t 625209662Slstewarthash_pkt(struct mbuf *m, uint32_t offset) 626209662Slstewart{ 627209662Slstewart uint32_t hash; 628209662Slstewart 629209662Slstewart hash = 0; 630209662Slstewart 631209662Slstewart while (m != NULL && offset > m->m_len) { 632209662Slstewart /* 633209662Slstewart * The IP packet payload does not start in this mbuf, so 634209662Slstewart * need to figure out which mbuf it starts in and what offset 635209662Slstewart * into the mbuf's data region the payload starts at. 636209662Slstewart */ 637209662Slstewart offset -= m->m_len; 638209662Slstewart m = m->m_next; 639209662Slstewart } 640209662Slstewart 641209662Slstewart while (m != NULL) { 642209662Slstewart /* Ensure there is data in the mbuf */ 643209662Slstewart if ((m->m_len - offset) > 0) 644209662Slstewart hash = hash32_buf(m->m_data + offset, 645209662Slstewart m->m_len - offset, hash); 646209662Slstewart 647209662Slstewart m = m->m_next; 648209662Slstewart offset = 0; 649209662Slstewart } 650209662Slstewart 651209662Slstewart return (hash); 652209662Slstewart} 653209662Slstewart 654209662Slstewart 655209662Slstewart/* 656209662Slstewart * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that 657209662Slstewart * it's a reinjected packet and return. If it doesn't, tag the mbuf and return. 658209662Slstewart * Return value >0 means the caller should skip processing this mbuf. 659209662Slstewart */ 660209662Slstewartstatic inline int 661209662Slstewartsiftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss) 662209662Slstewart{ 663209662Slstewart if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL) 664209662Slstewart != NULL) { 665209662Slstewart if (dir == PFIL_IN) 666209662Slstewart ss->nskip_in_dejavu++; 667209662Slstewart else 668209662Slstewart ss->nskip_out_dejavu++; 669209662Slstewart 670209662Slstewart return (1); 671209662Slstewart } else { 672209662Slstewart struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR, 673209662Slstewart PACKET_TAG_SIFTR, 0, M_NOWAIT); 674209662Slstewart if (tag == NULL) { 675209662Slstewart if (dir == PFIL_IN) 676209662Slstewart ss->nskip_in_malloc++; 677209662Slstewart else 678209662Slstewart ss->nskip_out_malloc++; 679209662Slstewart 680209662Slstewart return (1); 681209662Slstewart } 682209662Slstewart 683209662Slstewart m_tag_prepend(m, tag); 684209662Slstewart } 685209662Slstewart 686209662Slstewart return (0); 687209662Slstewart} 688209662Slstewart 689209662Slstewart 690209662Slstewart/* 691209662Slstewart * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL 692209662Slstewart * otherwise. 693209662Slstewart */ 694209662Slstewartstatic inline struct inpcb * 695209662Slstewartsiftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport, 696209662Slstewart uint16_t dport, int dir, struct siftr_stats *ss) 697209662Slstewart{ 698209662Slstewart struct inpcb *inp; 699209662Slstewart 700209662Slstewart /* We need the tcbinfo lock. */ 701209662Slstewart INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 702209662Slstewart INP_INFO_RLOCK(&V_tcbinfo); 703209662Slstewart 704209662Slstewart if (dir == PFIL_IN) 705209662Slstewart inp = (ipver == INP_IPV4 ? 706209662Slstewart in_pcblookup_hash(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst, 707209662Slstewart dport, 0, m->m_pkthdr.rcvif) 708209662Slstewart : 709209662Slstewart#ifdef SIFTR_IPV6 710209662Slstewart in6_pcblookup_hash(&V_tcbinfo, 711209662Slstewart &((struct ip6_hdr *)ip)->ip6_src, sport, 712209662Slstewart &((struct ip6_hdr *)ip)->ip6_dst, dport, 0, 713209662Slstewart m->m_pkthdr.rcvif) 714209662Slstewart#else 715209662Slstewart NULL 716209662Slstewart#endif 717209662Slstewart ); 718209662Slstewart 719209662Slstewart else 720209662Slstewart inp = (ipver == INP_IPV4 ? 721209662Slstewart in_pcblookup_hash(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src, 722209662Slstewart sport, 0, m->m_pkthdr.rcvif) 723209662Slstewart : 724209662Slstewart#ifdef SIFTR_IPV6 725209662Slstewart in6_pcblookup_hash(&V_tcbinfo, 726209662Slstewart &((struct ip6_hdr *)ip)->ip6_dst, dport, 727209662Slstewart &((struct ip6_hdr *)ip)->ip6_src, sport, 0, 728209662Slstewart m->m_pkthdr.rcvif) 729209662Slstewart#else 730209662Slstewart NULL 731209662Slstewart#endif 732209662Slstewart ); 733209662Slstewart 734209662Slstewart /* If we can't find the inpcb, bail. */ 735209662Slstewart if (inp == NULL) { 736209662Slstewart if (dir == PFIL_IN) 737209662Slstewart ss->nskip_in_inpcb++; 738209662Slstewart else 739209662Slstewart ss->nskip_out_inpcb++; 740209662Slstewart 741209662Slstewart INP_INFO_RUNLOCK(&V_tcbinfo); 742209662Slstewart } else { 743209662Slstewart /* Acquire the inpcb lock. */ 744209662Slstewart INP_UNLOCK_ASSERT(inp); 745209662Slstewart INP_RLOCK(inp); 746209662Slstewart INP_INFO_RUNLOCK(&V_tcbinfo); 747209662Slstewart } 748209662Slstewart 749209662Slstewart return (inp); 750209662Slstewart} 751209662Slstewart 752209662Slstewart 753210203Slstewartstatic inline void 754210203Slstewartsiftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp, 755210203Slstewart int ipver, int dir, int inp_locally_locked) 756210203Slstewart{ 757210203Slstewart#ifdef SIFTR_IPV6 758210203Slstewart if (ipver == INP_IPV4) { 759210203Slstewart pn->ip_laddr[3] = inp->inp_laddr.s_addr; 760210203Slstewart pn->ip_faddr[3] = inp->inp_faddr.s_addr; 761210203Slstewart#else 762210203Slstewart *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr; 763210203Slstewart *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr; 764210203Slstewart#endif 765210203Slstewart#ifdef SIFTR_IPV6 766210203Slstewart } else { 767210203Slstewart pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0]; 768210203Slstewart pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1]; 769210203Slstewart pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2]; 770210203Slstewart pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3]; 771210203Slstewart pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0]; 772210203Slstewart pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1]; 773210203Slstewart pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2]; 774210203Slstewart pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3]; 775210203Slstewart } 776210203Slstewart#endif 777210203Slstewart pn->tcp_localport = inp->inp_lport; 778210203Slstewart pn->tcp_foreignport = inp->inp_fport; 779210203Slstewart pn->snd_cwnd = tp->snd_cwnd; 780210203Slstewart pn->snd_wnd = tp->snd_wnd; 781210203Slstewart pn->rcv_wnd = tp->rcv_wnd; 782212765Sandre pn->snd_bwnd = 0; /* Unused, kept for compat. */ 783210203Slstewart pn->snd_ssthresh = tp->snd_ssthresh; 784210203Slstewart pn->snd_scale = tp->snd_scale; 785210203Slstewart pn->rcv_scale = tp->rcv_scale; 786210203Slstewart pn->conn_state = tp->t_state; 787210203Slstewart pn->max_seg_size = tp->t_maxseg; 788210203Slstewart pn->smoothed_rtt = tp->t_srtt; 789210203Slstewart pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0; 790210203Slstewart pn->flags = tp->t_flags; 791210203Slstewart pn->rxt_length = tp->t_rxtcur; 792210203Slstewart pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat; 793210203Slstewart pn->snd_buf_cc = inp->inp_socket->so_snd.sb_cc; 794210203Slstewart pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat; 795210203Slstewart pn->rcv_buf_cc = inp->inp_socket->so_rcv.sb_cc; 796210203Slstewart pn->sent_inflight_bytes = tp->snd_max - tp->snd_una; 797213162Slstewart pn->t_segqlen = tp->t_segqlen; 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 */ 966209662Slstewart th->th_sum = in_cksum_skip(*m, ip->ip_len, ip_hl); 967209662Slstewart } 968209662Slstewart 969209662Slstewart /* 970209662Slstewart * XXX: Having to calculate the checksum in software and then 971209662Slstewart * hash over all bytes is really inefficient. Would be nice to 972209662Slstewart * find a way to create the hash and checksum in the same pass 973209662Slstewart * over the bytes. 974209662Slstewart */ 975210203Slstewart pn->hash = hash_pkt(*m, ip_hl); 976209662Slstewart } 977209662Slstewart 978209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 979210203Slstewart STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 980209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 981209662Slstewart goto ret; 982209662Slstewart 983209662Slstewartinp_unlock: 984209662Slstewart if (inp_locally_locked) 985209662Slstewart INP_RUNLOCK(inp); 986209662Slstewart 987209662Slstewartret: 988209662Slstewart /* Returning 0 ensures pfil will not discard the pkt */ 989209662Slstewart return (0); 990209662Slstewart} 991209662Slstewart 992209662Slstewart 993209662Slstewart#ifdef SIFTR_IPV6 994209662Slstewartstatic int 995209662Slstewartsiftr_chkpkt6(void *arg, struct mbuf **m, struct ifnet *ifp, int dir, 996209662Slstewart struct inpcb *inp) 997209662Slstewart{ 998210203Slstewart struct pkt_node *pn; 999209662Slstewart struct ip6_hdr *ip6; 1000209662Slstewart struct tcphdr *th; 1001209662Slstewart struct tcpcb *tp; 1002209662Slstewart struct siftr_stats *ss; 1003209662Slstewart unsigned int ip6_hl; 1004210203Slstewart int inp_locally_locked; 1005209662Slstewart 1006209662Slstewart inp_locally_locked = 0; 1007209662Slstewart ss = DPCPU_PTR(ss); 1008209662Slstewart 1009209662Slstewart /* 1010209662Slstewart * m_pullup is not required here because ip6_{input|output} 1011209662Slstewart * already do the heavy lifting for us. 1012209662Slstewart */ 1013209662Slstewart 1014209662Slstewart ip6 = mtod(*m, struct ip6_hdr *); 1015209662Slstewart 1016209662Slstewart /* 1017209662Slstewart * Only continue processing if the packet is TCP 1018209662Slstewart * XXX: We should follow the next header fields 1019209662Slstewart * as shown on Pg 6 RFC 2460, but right now we'll 1020209662Slstewart * only check pkts that have no extension headers. 1021209662Slstewart */ 1022209662Slstewart if (ip6->ip6_nxt != IPPROTO_TCP) 1023209662Slstewart goto ret6; 1024209662Slstewart 1025209662Slstewart /* 1026209662Slstewart * If a kernel subsystem reinjects packets into the stack, our pfil 1027209662Slstewart * hook will be called multiple times for the same packet. 1028209662Slstewart * Make sure we only process unique packets. 1029209662Slstewart */ 1030209662Slstewart if (siftr_chkreinject(*m, dir, ss)) 1031209662Slstewart goto ret6; 1032209662Slstewart 1033209662Slstewart if (dir == PFIL_IN) 1034209662Slstewart ss->n_in++; 1035209662Slstewart else 1036209662Slstewart ss->n_out++; 1037209662Slstewart 1038209662Slstewart ip6_hl = sizeof(struct ip6_hdr); 1039209662Slstewart 1040209662Slstewart /* 1041209662Slstewart * Create a tcphdr struct starting at the correct offset 1042209662Slstewart * in the ipv6 packet. ip->ip_hl gives the ip header length 1043209662Slstewart * in 4-byte words, so multiply it to get the size in bytes. 1044209662Slstewart */ 1045209662Slstewart th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl); 1046209662Slstewart 1047209662Slstewart /* 1048209662Slstewart * For inbound packets, the pfil hooks don't provide a pointer to the 1049209662Slstewart * inpcb, so we need to find it ourselves and lock it. 1050209662Slstewart */ 1051209662Slstewart if (!inp) { 1052209662Slstewart /* Find the corresponding inpcb for this pkt. */ 1053209662Slstewart inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m, 1054209662Slstewart th->th_sport, th->th_dport, dir, ss); 1055209662Slstewart 1056209662Slstewart if (inp == NULL) 1057209662Slstewart goto ret6; 1058209662Slstewart else 1059209662Slstewart inp_locally_locked = 1; 1060209662Slstewart } 1061209662Slstewart 1062209662Slstewart /* Find the TCP control block that corresponds with this packet. */ 1063209662Slstewart tp = intotcpcb(inp); 1064209662Slstewart 1065209662Slstewart /* 1066209662Slstewart * If we can't find the TCP control block (happens occasionaly for a 1067209662Slstewart * packet sent during the shutdown phase of a TCP connection), 1068209662Slstewart * or we're in the timewait state, bail. 1069209662Slstewart */ 1070209662Slstewart if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) { 1071209662Slstewart if (dir == PFIL_IN) 1072209662Slstewart ss->nskip_in_tcpcb++; 1073209662Slstewart else 1074209662Slstewart ss->nskip_out_tcpcb++; 1075209662Slstewart 1076209662Slstewart goto inp_unlock6; 1077209662Slstewart } 1078209662Slstewart 1079210203Slstewart pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 1080209662Slstewart 1081210203Slstewart if (pn == NULL) { 1082210203Slstewart if (dir == PFIL_IN) 1083210203Slstewart ss->nskip_in_malloc++; 1084210203Slstewart else 1085210203Slstewart ss->nskip_out_malloc++; 1086209662Slstewart 1087210203Slstewart goto inp_unlock6; 1088210203Slstewart } 1089209662Slstewart 1090210203Slstewart siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked); 1091209662Slstewart 1092210203Slstewart /* XXX: Figure out how to generate hashes for IPv6 packets. */ 1093209662Slstewart 1094209662Slstewart mtx_lock(&siftr_pkt_queue_mtx); 1095210203Slstewart STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 1096209662Slstewart mtx_unlock(&siftr_pkt_queue_mtx); 1097209662Slstewart goto ret6; 1098209662Slstewart 1099209662Slstewartinp_unlock6: 1100209662Slstewart if (inp_locally_locked) 1101209662Slstewart INP_RUNLOCK(inp); 1102209662Slstewart 1103209662Slstewartret6: 1104209662Slstewart /* Returning 0 ensures pfil will not discard the pkt. */ 1105209662Slstewart return (0); 1106209662Slstewart} 1107209662Slstewart#endif /* #ifdef SIFTR_IPV6 */ 1108209662Slstewart 1109209662Slstewart 1110209662Slstewartstatic int 1111209662Slstewartsiftr_pfil(int action) 1112209662Slstewart{ 1113209662Slstewart struct pfil_head *pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 1114209662Slstewart#ifdef SIFTR_IPV6 1115209662Slstewart struct pfil_head *pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 1116209662Slstewart#endif 1117209662Slstewart 1118209662Slstewart if (action == HOOK) { 1119209662Slstewart pfil_add_hook(siftr_chkpkt, NULL, 1120209662Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1121209662Slstewart#ifdef SIFTR_IPV6 1122209662Slstewart pfil_add_hook(siftr_chkpkt6, NULL, 1123209662Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1124209662Slstewart#endif 1125209662Slstewart } else if (action == UNHOOK) { 1126209662Slstewart pfil_remove_hook(siftr_chkpkt, NULL, 1127209662Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1128209662Slstewart#ifdef SIFTR_IPV6 1129209662Slstewart pfil_remove_hook(siftr_chkpkt6, NULL, 1130209662Slstewart PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1131209662Slstewart#endif 1132209662Slstewart } 1133209662Slstewart 1134209662Slstewart return (0); 1135209662Slstewart} 1136209662Slstewart 1137209662Slstewart 1138209662Slstewartstatic int 1139209662Slstewartsiftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS) 1140209662Slstewart{ 1141209662Slstewart struct alq *new_alq; 1142209662Slstewart int error; 1143209662Slstewart 1144209662Slstewart if (req->newptr == NULL) 1145209662Slstewart goto skip; 1146209662Slstewart 1147209662Slstewart /* If old filename and new filename are different. */ 1148209662Slstewart if (strncmp(siftr_logfile, (char *)req->newptr, PATH_MAX)) { 1149209662Slstewart 1150209662Slstewart error = alq_open(&new_alq, req->newptr, curthread->td_ucred, 1151209662Slstewart SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1152209662Slstewart 1153209662Slstewart /* Bail if unable to create new alq. */ 1154209662Slstewart if (error) 1155209662Slstewart return (1); 1156209662Slstewart 1157209662Slstewart /* 1158209662Slstewart * If disabled, siftr_alq == NULL so we simply close 1159209662Slstewart * the alq as we've proved it can be opened. 1160209662Slstewart * If enabled, close the existing alq and switch the old 1161209662Slstewart * for the new. 1162209662Slstewart */ 1163209662Slstewart if (siftr_alq == NULL) 1164209662Slstewart alq_close(new_alq); 1165209662Slstewart else { 1166209662Slstewart alq_close(siftr_alq); 1167209662Slstewart siftr_alq = new_alq; 1168209662Slstewart } 1169209662Slstewart } 1170209662Slstewart 1171209662Slstewartskip: 1172209662Slstewart return (sysctl_handle_string(oidp, arg1, arg2, req)); 1173209662Slstewart} 1174209662Slstewart 1175209662Slstewart 1176209662Slstewartstatic int 1177209662Slstewartsiftr_manage_ops(uint8_t action) 1178209662Slstewart{ 1179209662Slstewart struct siftr_stats totalss; 1180209662Slstewart struct timeval tval; 1181209662Slstewart struct flow_hash_node *counter, *tmp_counter; 1182209662Slstewart struct sbuf *s; 1183209662Slstewart int i, key_index, ret, error; 1184209662Slstewart uint32_t bytes_to_write, total_skipped_pkts; 1185209662Slstewart uint16_t lport, fport; 1186209662Slstewart uint8_t *key, ipver; 1187209662Slstewart 1188209662Slstewart#ifdef SIFTR_IPV6 1189209662Slstewart uint32_t laddr[4]; 1190209662Slstewart uint32_t faddr[4]; 1191209662Slstewart#else 1192209662Slstewart uint8_t laddr[4]; 1193209662Slstewart uint8_t faddr[4]; 1194209662Slstewart#endif 1195209662Slstewart 1196209662Slstewart error = 0; 1197209662Slstewart total_skipped_pkts = 0; 1198209662Slstewart 1199209662Slstewart /* Init an autosizing sbuf that initially holds 200 chars. */ 1200209662Slstewart if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL) 1201209662Slstewart return (-1); 1202209662Slstewart 1203209662Slstewart if (action == SIFTR_ENABLE) { 1204209662Slstewart /* 1205209662Slstewart * Create our alq 1206209662Slstewart * XXX: We should abort if alq_open fails! 1207209662Slstewart */ 1208209662Slstewart alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred, 1209209662Slstewart SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1210209662Slstewart 1211209662Slstewart STAILQ_INIT(&pkt_queue); 1212209662Slstewart 1213209982Slstewart DPCPU_ZERO(ss); 1214209982Slstewart 1215209662Slstewart siftr_exit_pkt_manager_thread = 0; 1216209662Slstewart 1217209662Slstewart ret = kthread_add(&siftr_pkt_manager_thread, NULL, NULL, 1218209662Slstewart &siftr_pkt_manager_thr, RFNOWAIT, 0, 1219209662Slstewart "siftr_pkt_manager_thr"); 1220209662Slstewart 1221209662Slstewart siftr_pfil(HOOK); 1222209662Slstewart 1223209662Slstewart microtime(&tval); 1224209662Slstewart 1225209662Slstewart sbuf_printf(s, 1226209662Slstewart "enable_time_secs=%jd\tenable_time_usecs=%06ld\t" 1227209662Slstewart "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t" 1228209662Slstewart "sysver=%u\tipmode=%u\n", 1229209662Slstewart (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz, 1230209662Slstewart TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE); 1231209662Slstewart 1232209662Slstewart sbuf_finish(s); 1233209662Slstewart alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK); 1234209662Slstewart 1235209662Slstewart } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) { 1236209662Slstewart /* 1237209662Slstewart * Remove the pfil hook functions. All threads currently in 1238209662Slstewart * the hook functions are allowed to exit before siftr_pfil() 1239209662Slstewart * returns. 1240209662Slstewart */ 1241209662Slstewart siftr_pfil(UNHOOK); 1242209662Slstewart 1243209662Slstewart /* This will block until the pkt manager thread unlocks it. */ 1244209662Slstewart mtx_lock(&siftr_pkt_mgr_mtx); 1245209662Slstewart 1246209662Slstewart /* Tell the pkt manager thread that it should exit now. */ 1247209662Slstewart siftr_exit_pkt_manager_thread = 1; 1248209662Slstewart 1249209662Slstewart /* 1250209662Slstewart * Wake the pkt_manager thread so it realises that 1251209662Slstewart * siftr_exit_pkt_manager_thread == 1 and exits gracefully. 1252209662Slstewart * The wakeup won't be delivered until we unlock 1253209662Slstewart * siftr_pkt_mgr_mtx so this isn't racy. 1254209662Slstewart */ 1255209662Slstewart wakeup(&wait_for_pkt); 1256209662Slstewart 1257209662Slstewart /* Wait for the pkt_manager thread to exit. */ 1258209662Slstewart mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT, 1259209662Slstewart "thrwait", 0); 1260209662Slstewart 1261209662Slstewart siftr_pkt_manager_thr = NULL; 1262209662Slstewart mtx_unlock(&siftr_pkt_mgr_mtx); 1263209662Slstewart 1264209980Slstewart totalss.n_in = DPCPU_VARSUM(ss, n_in); 1265209980Slstewart totalss.n_out = DPCPU_VARSUM(ss, n_out); 1266209980Slstewart totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc); 1267209980Slstewart totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc); 1268209980Slstewart totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx); 1269209980Slstewart totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx); 1270209980Slstewart totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb); 1271209980Slstewart totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb); 1272209980Slstewart totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb); 1273209980Slstewart totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb); 1274209662Slstewart 1275209662Slstewart total_skipped_pkts = totalss.nskip_in_malloc + 1276209662Slstewart totalss.nskip_out_malloc + totalss.nskip_in_mtx + 1277209662Slstewart totalss.nskip_out_mtx + totalss.nskip_in_tcpcb + 1278209662Slstewart totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb + 1279209662Slstewart totalss.nskip_out_inpcb; 1280209662Slstewart 1281209662Slstewart microtime(&tval); 1282209662Slstewart 1283209662Slstewart sbuf_printf(s, 1284209662Slstewart "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t" 1285209662Slstewart "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t" 1286209662Slstewart "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t" 1287209662Slstewart "num_outbound_skipped_pkts_malloc=%u\t" 1288209662Slstewart "num_inbound_skipped_pkts_mtx=%u\t" 1289209662Slstewart "num_outbound_skipped_pkts_mtx=%u\t" 1290209662Slstewart "num_inbound_skipped_pkts_tcpcb=%u\t" 1291209662Slstewart "num_outbound_skipped_pkts_tcpcb=%u\t" 1292209662Slstewart "num_inbound_skipped_pkts_inpcb=%u\t" 1293209662Slstewart "num_outbound_skipped_pkts_inpcb=%u\t" 1294209662Slstewart "total_skipped_tcp_pkts=%u\tflow_list=", 1295209662Slstewart (intmax_t)tval.tv_sec, 1296209662Slstewart tval.tv_usec, 1297209662Slstewart (uintmax_t)totalss.n_in, 1298209662Slstewart (uintmax_t)totalss.n_out, 1299209662Slstewart (uintmax_t)(totalss.n_in + totalss.n_out), 1300209662Slstewart totalss.nskip_in_malloc, 1301209662Slstewart totalss.nskip_out_malloc, 1302209662Slstewart totalss.nskip_in_mtx, 1303209662Slstewart totalss.nskip_out_mtx, 1304209662Slstewart totalss.nskip_in_tcpcb, 1305209662Slstewart totalss.nskip_out_tcpcb, 1306209662Slstewart totalss.nskip_in_inpcb, 1307209662Slstewart totalss.nskip_out_inpcb, 1308209662Slstewart total_skipped_pkts); 1309209662Slstewart 1310209662Slstewart /* 1311209662Slstewart * Iterate over the flow hash, printing a summary of each 1312209662Slstewart * flow seen and freeing any malloc'd memory. 1313209662Slstewart * The hash consists of an array of LISTs (man 3 queue). 1314209662Slstewart */ 1315209662Slstewart for (i = 0; i < siftr_hashmask; i++) { 1316209662Slstewart LIST_FOREACH_SAFE(counter, counter_hash + i, nodes, 1317209662Slstewart tmp_counter) { 1318209662Slstewart key = counter->key; 1319209662Slstewart key_index = 1; 1320209662Slstewart 1321209662Slstewart ipver = key[0]; 1322209662Slstewart 1323209662Slstewart memcpy(laddr, key + key_index, sizeof(laddr)); 1324209662Slstewart key_index += sizeof(laddr); 1325209662Slstewart memcpy(&lport, key + key_index, sizeof(lport)); 1326209662Slstewart key_index += sizeof(lport); 1327209662Slstewart memcpy(faddr, key + key_index, sizeof(faddr)); 1328209662Slstewart key_index += sizeof(faddr); 1329209662Slstewart memcpy(&fport, key + key_index, sizeof(fport)); 1330209662Slstewart 1331209662Slstewart#ifdef SIFTR_IPV6 1332209662Slstewart laddr[3] = ntohl(laddr[3]); 1333209662Slstewart faddr[3] = ntohl(faddr[3]); 1334209662Slstewart 1335209662Slstewart if (ipver == INP_IPV6) { 1336209662Slstewart laddr[0] = ntohl(laddr[0]); 1337209662Slstewart laddr[1] = ntohl(laddr[1]); 1338209662Slstewart laddr[2] = ntohl(laddr[2]); 1339209662Slstewart faddr[0] = ntohl(faddr[0]); 1340209662Slstewart faddr[1] = ntohl(faddr[1]); 1341209662Slstewart faddr[2] = ntohl(faddr[2]); 1342209662Slstewart 1343209662Slstewart sbuf_printf(s, 1344209662Slstewart "%x:%x:%x:%x:%x:%x:%x:%x;%u-" 1345209662Slstewart "%x:%x:%x:%x:%x:%x:%x:%x;%u,", 1346209662Slstewart UPPER_SHORT(laddr[0]), 1347209662Slstewart LOWER_SHORT(laddr[0]), 1348209662Slstewart UPPER_SHORT(laddr[1]), 1349209662Slstewart LOWER_SHORT(laddr[1]), 1350209662Slstewart UPPER_SHORT(laddr[2]), 1351209662Slstewart LOWER_SHORT(laddr[2]), 1352209662Slstewart UPPER_SHORT(laddr[3]), 1353209662Slstewart LOWER_SHORT(laddr[3]), 1354209662Slstewart ntohs(lport), 1355209662Slstewart UPPER_SHORT(faddr[0]), 1356209662Slstewart LOWER_SHORT(faddr[0]), 1357209662Slstewart UPPER_SHORT(faddr[1]), 1358209662Slstewart LOWER_SHORT(faddr[1]), 1359209662Slstewart UPPER_SHORT(faddr[2]), 1360209662Slstewart LOWER_SHORT(faddr[2]), 1361209662Slstewart UPPER_SHORT(faddr[3]), 1362209662Slstewart LOWER_SHORT(faddr[3]), 1363209662Slstewart ntohs(fport)); 1364209662Slstewart } else { 1365209662Slstewart laddr[0] = FIRST_OCTET(laddr[3]); 1366209662Slstewart laddr[1] = SECOND_OCTET(laddr[3]); 1367209662Slstewart laddr[2] = THIRD_OCTET(laddr[3]); 1368209662Slstewart laddr[3] = FOURTH_OCTET(laddr[3]); 1369209662Slstewart faddr[0] = FIRST_OCTET(faddr[3]); 1370209662Slstewart faddr[1] = SECOND_OCTET(faddr[3]); 1371209662Slstewart faddr[2] = THIRD_OCTET(faddr[3]); 1372209662Slstewart faddr[3] = FOURTH_OCTET(faddr[3]); 1373209662Slstewart#endif 1374209662Slstewart sbuf_printf(s, 1375209662Slstewart "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,", 1376209662Slstewart laddr[0], 1377209662Slstewart laddr[1], 1378209662Slstewart laddr[2], 1379209662Slstewart laddr[3], 1380209662Slstewart ntohs(lport), 1381209662Slstewart faddr[0], 1382209662Slstewart faddr[1], 1383209662Slstewart faddr[2], 1384209662Slstewart faddr[3], 1385209662Slstewart ntohs(fport)); 1386209662Slstewart#ifdef SIFTR_IPV6 1387209662Slstewart } 1388209662Slstewart#endif 1389209662Slstewart 1390209662Slstewart free(counter, M_SIFTR_HASHNODE); 1391209662Slstewart } 1392209662Slstewart 1393209662Slstewart LIST_INIT(counter_hash + i); 1394209662Slstewart } 1395209662Slstewart 1396209662Slstewart sbuf_printf(s, "\n"); 1397209662Slstewart sbuf_finish(s); 1398209662Slstewart 1399209662Slstewart i = 0; 1400209662Slstewart do { 1401209662Slstewart bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i); 1402209662Slstewart alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK); 1403209662Slstewart i += bytes_to_write; 1404209662Slstewart } while (i < sbuf_len(s)); 1405209662Slstewart 1406209662Slstewart alq_close(siftr_alq); 1407209662Slstewart siftr_alq = NULL; 1408209662Slstewart } 1409209662Slstewart 1410209662Slstewart sbuf_delete(s); 1411209662Slstewart 1412209662Slstewart /* 1413209662Slstewart * XXX: Should be using ret to check if any functions fail 1414209662Slstewart * and set error appropriately 1415209662Slstewart */ 1416209662Slstewart 1417209662Slstewart return (error); 1418209662Slstewart} 1419209662Slstewart 1420209662Slstewart 1421209662Slstewartstatic int 1422209662Slstewartsiftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS) 1423209662Slstewart{ 1424209662Slstewart if (req->newptr == NULL) 1425209662Slstewart goto skip; 1426209662Slstewart 1427209662Slstewart /* If the value passed in isn't 0 or 1, return an error. */ 1428209662Slstewart if (CAST_PTR_INT(req->newptr) != 0 && CAST_PTR_INT(req->newptr) != 1) 1429209662Slstewart return (1); 1430209662Slstewart 1431209662Slstewart /* If we are changing state (0 to 1 or 1 to 0). */ 1432209662Slstewart if (CAST_PTR_INT(req->newptr) != siftr_enabled ) 1433209662Slstewart if (siftr_manage_ops(CAST_PTR_INT(req->newptr))) { 1434209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1435209662Slstewart return (1); 1436209662Slstewart } 1437209662Slstewart 1438209662Slstewartskip: 1439209662Slstewart return (sysctl_handle_int(oidp, arg1, arg2, req)); 1440209662Slstewart} 1441209662Slstewart 1442209662Slstewart 1443209662Slstewartstatic void 1444209662Slstewartsiftr_shutdown_handler(void *arg) 1445209662Slstewart{ 1446209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1447209662Slstewart} 1448209662Slstewart 1449209662Slstewart 1450209662Slstewart/* 1451209662Slstewart * Module is being unloaded or machine is shutting down. Take care of cleanup. 1452209662Slstewart */ 1453209662Slstewartstatic int 1454209662Slstewartdeinit_siftr(void) 1455209662Slstewart{ 1456209662Slstewart /* Cleanup. */ 1457209662Slstewart siftr_manage_ops(SIFTR_DISABLE); 1458209662Slstewart hashdestroy(counter_hash, M_SIFTR, siftr_hashmask); 1459209662Slstewart mtx_destroy(&siftr_pkt_queue_mtx); 1460209662Slstewart mtx_destroy(&siftr_pkt_mgr_mtx); 1461209662Slstewart 1462209662Slstewart return (0); 1463209662Slstewart} 1464209662Slstewart 1465209662Slstewart 1466209662Slstewart/* 1467209662Slstewart * Module has just been loaded into the kernel. 1468209662Slstewart */ 1469209662Slstewartstatic int 1470209662Slstewartinit_siftr(void) 1471209662Slstewart{ 1472209662Slstewart EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL, 1473209662Slstewart SHUTDOWN_PRI_FIRST); 1474209662Slstewart 1475209662Slstewart /* Initialise our flow counter hash table. */ 1476209662Slstewart counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR, 1477209662Slstewart &siftr_hashmask); 1478209662Slstewart 1479209662Slstewart mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF); 1480209662Slstewart mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF); 1481209662Slstewart 1482209662Slstewart /* Print message to the user's current terminal. */ 1483209662Slstewart uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n" 1484209662Slstewart " http://caia.swin.edu.au/urp/newtcp\n\n", 1485209662Slstewart MODVERSION_STR); 1486209662Slstewart 1487209662Slstewart return (0); 1488209662Slstewart} 1489209662Slstewart 1490209662Slstewart 1491209662Slstewart/* 1492209662Slstewart * This is the function that is called to load and unload the module. 1493209662Slstewart * When the module is loaded, this function is called once with 1494209662Slstewart * "what" == MOD_LOAD 1495209662Slstewart * When the module is unloaded, this function is called twice with 1496209662Slstewart * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second 1497209662Slstewart * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command, 1498209662Slstewart * this function is called once with "what" = MOD_SHUTDOWN 1499209662Slstewart * When the system is shut down, the handler isn't called until the very end 1500209662Slstewart * of the shutdown sequence i.e. after the disks have been synced. 1501209662Slstewart */ 1502209662Slstewartstatic int 1503209662Slstewartsiftr_load_handler(module_t mod, int what, void *arg) 1504209662Slstewart{ 1505209662Slstewart int ret; 1506209662Slstewart 1507209662Slstewart switch (what) { 1508209662Slstewart case MOD_LOAD: 1509209662Slstewart ret = init_siftr(); 1510209662Slstewart break; 1511209662Slstewart 1512209662Slstewart case MOD_QUIESCE: 1513209662Slstewart case MOD_SHUTDOWN: 1514209662Slstewart ret = deinit_siftr(); 1515209662Slstewart break; 1516209662Slstewart 1517209662Slstewart case MOD_UNLOAD: 1518209662Slstewart ret = 0; 1519209662Slstewart break; 1520209662Slstewart 1521209662Slstewart default: 1522209662Slstewart ret = EINVAL; 1523209662Slstewart break; 1524209662Slstewart } 1525209662Slstewart 1526209662Slstewart return (ret); 1527209662Slstewart} 1528209662Slstewart 1529209662Slstewart 1530209662Slstewartstatic moduledata_t siftr_mod = { 1531209662Slstewart .name = "siftr", 1532209662Slstewart .evhand = siftr_load_handler, 1533209662Slstewart}; 1534209662Slstewart 1535209662Slstewart/* 1536209662Slstewart * Param 1: name of the kernel module 1537209662Slstewart * Param 2: moduledata_t struct containing info about the kernel module 1538209662Slstewart * and the execution entry point for the module 1539209662Slstewart * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h 1540209662Slstewart * Defines the module initialisation order 1541209662Slstewart * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h 1542209662Slstewart * Defines the initialisation order of this kld relative to others 1543209662Slstewart * within the same subsystem as defined by param 3 1544209662Slstewart */ 1545209662SlstewartDECLARE_MODULE(siftr, siftr_mod, SI_SUB_SMP, SI_ORDER_ANY); 1546209662SlstewartMODULE_DEPEND(siftr, alq, 1, 1, 1); 1547209662SlstewartMODULE_VERSION(siftr, MODVERSION); 1548