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