1 /*- 2 * Copyright (c) 2007-2009, Centre for Advanced Internet Architectures 3 * Swinburne University of Technology, Melbourne, Australia 4 * (CRICOS number 00111D). 5 * Copyright (c) 2009-2010, The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * Portions of this software were developed at the Centre for Advanced 9 * Internet Architectures, Swinburne University of Technology, Melbourne, 10 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /****************************************************** 35 * Statistical Information For TCP Research (SIFTR) 36 * 37 * A FreeBSD kernel module that adds very basic intrumentation to the 38 * TCP stack, allowing internal stats to be recorded to a log file 39 * for experimental, debugging and performance analysis purposes. 40 * 41 * SIFTR was first released in 2007 by James Healy and Lawrence Stewart whilst 42 * working on the NewTCP research project at Swinburne University's Centre for 43 * Advanced Internet Architectures, Melbourne, Australia, which was made 44 * possible in part by a grant from the Cisco University Research Program Fund 45 * at Community Foundation Silicon Valley. More details are available at: 46 * http://caia.swin.edu.au/urp/newtcp/ 47 * 48 * Work on SIFTR v1.2.x was sponsored by the FreeBSD Foundation as part of 49 * the "Enhancing the FreeBSD TCP Implementation" project 2008-2009. 50 * More details are available at: 51 * http://www.freebsdfoundation.org/ 52 * http://caia.swin.edu.au/freebsd/etcp09/ 53 * 54 * Lawrence Stewart is the current maintainer, and all contact regarding 55 * SIFTR should be directed to him via email: lastewart@swin.edu.au 56 * 57 * Initial release date: June 2007 58 * Most recent update: September 2010 59 ******************************************************/ 60 61 #include <sys/cdefs.h> 62 __FBSDID("$FreeBSD$"); 63 64 #include <sys/param.h> 65 #include <sys/alq.h> 66 #include <sys/errno.h> 67 #include <sys/hash.h> 68 #include <sys/kernel.h> 69 #include <sys/kthread.h> 70 #include <sys/lock.h> 71 #include <sys/mbuf.h> 72 #include <sys/module.h> 73 #include <sys/mutex.h> 74 #include <sys/pcpu.h> 75 #include <sys/proc.h> 76 #include <sys/sbuf.h> 77 #include <sys/smp.h> 78 #include <sys/socket.h> 79 #include <sys/socketvar.h> 80 #include <sys/sysctl.h> 81 #include <sys/unistd.h> 82 83 #include <net/if.h> 84 #include <net/pfil.h> 85 86 #include <netinet/in.h> 87 #include <netinet/in_pcb.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/in_var.h> 90 #include <netinet/ip.h> 91 #include <netinet/tcp_var.h> 92 93 #ifdef SIFTR_IPV6 94 #include <netinet/ip6.h> 95 #include <netinet6/in6_pcb.h> 96 #endif /* SIFTR_IPV6 */ 97 98 #include <machine/in_cksum.h> 99 100 /* 101 * Three digit version number refers to X.Y.Z where: 102 * X is the major version number 103 * Y is bumped to mark backwards incompatible changes 104 * Z is bumped to mark backwards compatible changes 105 */ 106 #define V_MAJOR 1 107 #define V_BACKBREAK 2 108 #define V_BACKCOMPAT 4 109 #define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT)) 110 #define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \ 111 __XSTRING(V_BACKCOMPAT) 112 113 #define HOOK 0 114 #define UNHOOK 1 115 #define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536 116 #define SYS_NAME "FreeBSD" 117 #define PACKET_TAG_SIFTR 100 118 #define PACKET_COOKIE_SIFTR 21749576 119 #define SIFTR_LOG_FILE_MODE 0644 120 #define SIFTR_DISABLE 0 121 #define SIFTR_ENABLE 1 122 123 /* 124 * Hard upper limit on the length of log messages. Bump this up if you add new 125 * data fields such that the line length could exceed the below value. 126 */ 127 #define MAX_LOG_MSG_LEN 200 128 /* XXX: Make this a sysctl tunable. */ 129 #define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN) 130 131 /* 132 * 1 byte for IP version 133 * IPv4: src/dst IP (4+4) + src/dst port (2+2) = 12 bytes 134 * IPv6: src/dst IP (16+16) + src/dst port (2+2) = 36 bytes 135 */ 136 #ifdef SIFTR_IPV6 137 #define FLOW_KEY_LEN 37 138 #else 139 #define FLOW_KEY_LEN 13 140 #endif 141 142 #ifdef SIFTR_IPV6 143 #define SIFTR_IPMODE 6 144 #else 145 #define SIFTR_IPMODE 4 146 #endif 147 148 /* useful macros */ 149 #define CAST_PTR_INT(X) (*((int*)(X))) 150 151 #define UPPER_SHORT(X) (((X) & 0xFFFF0000) >> 16) 152 #define LOWER_SHORT(X) ((X) & 0x0000FFFF) 153 154 #define FIRST_OCTET(X) (((X) & 0xFF000000) >> 24) 155 #define SECOND_OCTET(X) (((X) & 0x00FF0000) >> 16) 156 #define THIRD_OCTET(X) (((X) & 0x0000FF00) >> 8) 157 #define FOURTH_OCTET(X) ((X) & 0x000000FF) 158 159 MALLOC_DECLARE(M_SIFTR); 160 MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR"); 161 162 MALLOC_DECLARE(M_SIFTR_PKTNODE); 163 MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode", "SIFTR pkt_node struct"); 164 165 MALLOC_DECLARE(M_SIFTR_HASHNODE); 166 MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode", "SIFTR flow_hash_node struct"); 167 168 /* Used as links in the pkt manager queue. */ 169 struct pkt_node { 170 /* Timestamp of pkt as noted in the pfil hook. */ 171 struct timeval tval; 172 /* Direction pkt is travelling; either PFIL_IN or PFIL_OUT. */ 173 uint8_t direction; 174 /* IP version pkt_node relates to; either INP_IPV4 or INP_IPV6. */ 175 uint8_t ipver; 176 /* Hash of the pkt which triggered the log message. */ 177 uint32_t hash; 178 /* Local/foreign IP address. */ 179 #ifdef SIFTR_IPV6 180 uint32_t ip_laddr[4]; 181 uint32_t ip_faddr[4]; 182 #else 183 uint8_t ip_laddr[4]; 184 uint8_t ip_faddr[4]; 185 #endif 186 /* Local TCP port. */ 187 uint16_t tcp_localport; 188 /* Foreign TCP port. */ 189 uint16_t tcp_foreignport; 190 /* Congestion Window (bytes). */ 191 u_long snd_cwnd; 192 /* Sending Window (bytes). */ 193 u_long snd_wnd; 194 /* Receive Window (bytes). */ 195 u_long rcv_wnd; 196 /* Unused (was: Bandwidth Controlled Window (bytes)). */ 197 u_long snd_bwnd; 198 /* Slow Start Threshold (bytes). */ 199 u_long snd_ssthresh; 200 /* Current state of the TCP FSM. */ 201 int conn_state; 202 /* Max Segment Size (bytes). */ 203 u_int max_seg_size; 204 /* 205 * Smoothed RTT stored as found in the TCP control block 206 * in units of (TCP_RTT_SCALE*hz). 207 */ 208 int smoothed_rtt; 209 /* Is SACK enabled? */ 210 u_char sack_enabled; 211 /* Window scaling for snd window. */ 212 u_char snd_scale; 213 /* Window scaling for recv window. */ 214 u_char rcv_scale; 215 /* TCP control block flags. */ 216 u_int flags; 217 /* Retransmit timeout length. */ 218 int rxt_length; 219 /* Size of the TCP send buffer in bytes. */ 220 u_int snd_buf_hiwater; 221 /* Current num bytes in the send socket buffer. */ 222 u_int snd_buf_cc; 223 /* Size of the TCP receive buffer in bytes. */ 224 u_int rcv_buf_hiwater; 225 /* Current num bytes in the receive socket buffer. */ 226 u_int rcv_buf_cc; 227 /* Number of bytes inflight that we are waiting on ACKs for. */ 228 u_int sent_inflight_bytes; 229 /* Number of segments currently in the reassembly queue. */ 230 int t_segqlen; 231 /* Link to next pkt_node in the list. */ 232 STAILQ_ENTRY(pkt_node) nodes; 233 }; 234 235 struct flow_hash_node 236 { 237 uint16_t counter; 238 uint8_t key[FLOW_KEY_LEN]; 239 LIST_ENTRY(flow_hash_node) nodes; 240 }; 241 242 struct siftr_stats 243 { 244 /* # TCP pkts seen by the SIFTR PFIL hooks, including any skipped. */ 245 uint64_t n_in; 246 uint64_t n_out; 247 /* # pkts skipped due to failed malloc calls. */ 248 uint32_t nskip_in_malloc; 249 uint32_t nskip_out_malloc; 250 /* # pkts skipped due to failed mtx acquisition. */ 251 uint32_t nskip_in_mtx; 252 uint32_t nskip_out_mtx; 253 /* # pkts skipped due to failed inpcb lookups. */ 254 uint32_t nskip_in_inpcb; 255 uint32_t nskip_out_inpcb; 256 /* # pkts skipped due to failed tcpcb lookups. */ 257 uint32_t nskip_in_tcpcb; 258 uint32_t nskip_out_tcpcb; 259 /* # pkts skipped due to stack reinjection. */ 260 uint32_t nskip_in_dejavu; 261 uint32_t nskip_out_dejavu; 262 }; 263 264 static DPCPU_DEFINE(struct siftr_stats, ss); 265 266 static volatile unsigned int siftr_exit_pkt_manager_thread = 0; 267 static unsigned int siftr_enabled = 0; 268 static unsigned int siftr_pkts_per_log = 1; 269 static unsigned int siftr_generate_hashes = 0; 270 /* static unsigned int siftr_binary_log = 0; */ 271 static char siftr_logfile[PATH_MAX] = "/var/log/siftr.log"; 272 static u_long siftr_hashmask; 273 STAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue); 274 LIST_HEAD(listhead, flow_hash_node) *counter_hash; 275 static int wait_for_pkt; 276 static struct alq *siftr_alq = NULL; 277 static struct mtx siftr_pkt_queue_mtx; 278 static struct mtx siftr_pkt_mgr_mtx; 279 static struct thread *siftr_pkt_manager_thr = NULL; 280 /* 281 * pfil.h defines PFIL_IN as 1 and PFIL_OUT as 2, 282 * which we use as an index into this array. 283 */ 284 static char direction[3] = {'\0', 'i','o'}; 285 286 /* Required function prototypes. */ 287 static int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS); 288 static int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS); 289 290 291 /* Declare the net.inet.siftr sysctl tree and populate it. */ 292 293 SYSCTL_DECL(_net_inet_siftr); 294 295 SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW, NULL, 296 "siftr related settings"); 297 298 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled, CTLTYPE_UINT|CTLFLAG_RW, 299 &siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU", 300 "switch siftr module operations on/off"); 301 302 SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile, CTLTYPE_STRING|CTLFLAG_RW, 303 &siftr_logfile, sizeof(siftr_logfile), &siftr_sysctl_logfile_name_handler, 304 "A", "file to save siftr log messages to"); 305 306 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW, 307 &siftr_pkts_per_log, 1, 308 "number of packets between generating a log message"); 309 310 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW, 311 &siftr_generate_hashes, 0, 312 "enable packet hash generation"); 313 314 /* XXX: TODO 315 SYSCTL_UINT(_net_inet_siftr, OID_AUTO, binary, CTLFLAG_RW, 316 &siftr_binary_log, 0, 317 "write log files in binary instead of ascii"); 318 */ 319 320 321 /* Begin functions. */ 322 323 static void 324 siftr_process_pkt(struct pkt_node * pkt_node) 325 { 326 struct flow_hash_node *hash_node; 327 struct listhead *counter_list; 328 struct siftr_stats *ss; 329 struct ale *log_buf; 330 uint8_t key[FLOW_KEY_LEN]; 331 uint8_t found_match, key_offset; 332 333 hash_node = NULL; 334 ss = DPCPU_PTR(ss); 335 found_match = 0; 336 key_offset = 1; 337 338 /* 339 * Create the key that will be used to create a hash index 340 * into our hash table. Our key consists of: 341 * ipversion, localip, localport, foreignip, foreignport 342 */ 343 key[0] = pkt_node->ipver; 344 memcpy(key + key_offset, &pkt_node->ip_laddr, 345 sizeof(pkt_node->ip_laddr)); 346 key_offset += sizeof(pkt_node->ip_laddr); 347 memcpy(key + key_offset, &pkt_node->tcp_localport, 348 sizeof(pkt_node->tcp_localport)); 349 key_offset += sizeof(pkt_node->tcp_localport); 350 memcpy(key + key_offset, &pkt_node->ip_faddr, 351 sizeof(pkt_node->ip_faddr)); 352 key_offset += sizeof(pkt_node->ip_faddr); 353 memcpy(key + key_offset, &pkt_node->tcp_foreignport, 354 sizeof(pkt_node->tcp_foreignport)); 355 356 counter_list = counter_hash + 357 (hash32_buf(key, sizeof(key), 0) & siftr_hashmask); 358 359 /* 360 * If the list is not empty i.e. the hash index has 361 * been used by another flow previously. 362 */ 363 if (LIST_FIRST(counter_list) != NULL) { 364 /* 365 * Loop through the hash nodes in the list. 366 * There should normally only be 1 hash node in the list, 367 * except if there have been collisions at the hash index 368 * computed by hash32_buf(). 369 */ 370 LIST_FOREACH(hash_node, counter_list, nodes) { 371 /* 372 * Check if the key for the pkt we are currently 373 * processing is the same as the key stored in the 374 * hash node we are currently processing. 375 * If they are the same, then we've found the 376 * hash node that stores the counter for the flow 377 * the pkt belongs to. 378 */ 379 if (memcmp(hash_node->key, key, sizeof(key)) == 0) { 380 found_match = 1; 381 break; 382 } 383 } 384 } 385 386 /* If this flow hash hasn't been seen before or we have a collision. */ 387 if (hash_node == NULL || !found_match) { 388 /* Create a new hash node to store the flow's counter. */ 389 hash_node = malloc(sizeof(struct flow_hash_node), 390 M_SIFTR_HASHNODE, M_WAITOK); 391 392 if (hash_node != NULL) { 393 /* Initialise our new hash node list entry. */ 394 hash_node->counter = 0; 395 memcpy(hash_node->key, key, sizeof(key)); 396 LIST_INSERT_HEAD(counter_list, hash_node, nodes); 397 } else { 398 /* Malloc failed. */ 399 if (pkt_node->direction == PFIL_IN) 400 ss->nskip_in_malloc++; 401 else 402 ss->nskip_out_malloc++; 403 404 return; 405 } 406 } else if (siftr_pkts_per_log > 1) { 407 /* 408 * Taking the remainder of the counter divided 409 * by the current value of siftr_pkts_per_log 410 * and storing that in counter provides a neat 411 * way to modulate the frequency of log 412 * messages being written to the log file. 413 */ 414 hash_node->counter = (hash_node->counter + 1) % 415 siftr_pkts_per_log; 416 417 /* 418 * If we have not seen enough packets since the last time 419 * we wrote a log message for this connection, return. 420 */ 421 if (hash_node->counter > 0) 422 return; 423 } 424 425 log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN, ALQ_WAITOK); 426 427 if (log_buf == NULL) 428 return; /* Should only happen if the ALQ is shutting down. */ 429 430 #ifdef SIFTR_IPV6 431 pkt_node->ip_laddr[3] = ntohl(pkt_node->ip_laddr[3]); 432 pkt_node->ip_faddr[3] = ntohl(pkt_node->ip_faddr[3]); 433 434 if (pkt_node->ipver == INP_IPV6) { /* IPv6 packet */ 435 pkt_node->ip_laddr[0] = ntohl(pkt_node->ip_laddr[0]); 436 pkt_node->ip_laddr[1] = ntohl(pkt_node->ip_laddr[1]); 437 pkt_node->ip_laddr[2] = ntohl(pkt_node->ip_laddr[2]); 438 pkt_node->ip_faddr[0] = ntohl(pkt_node->ip_faddr[0]); 439 pkt_node->ip_faddr[1] = ntohl(pkt_node->ip_faddr[1]); 440 pkt_node->ip_faddr[2] = ntohl(pkt_node->ip_faddr[2]); 441 442 /* Construct an IPv6 log message. */ 443 log_buf->ae_bytesused = snprintf(log_buf->ae_data, 444 MAX_LOG_MSG_LEN, 445 "%c,0x%08x,%zd.%06ld,%x:%x:%x:%x:%x:%x:%x:%x,%u,%x:%x:%x:" 446 "%x:%x:%x:%x:%x,%u,%ld,%ld,%ld,%ld,%ld,%u,%u,%u,%u,%u,%u," 447 "%u,%d,%u,%u,%u,%u,%u,%u\n", 448 direction[pkt_node->direction], 449 pkt_node->hash, 450 pkt_node->tval.tv_sec, 451 pkt_node->tval.tv_usec, 452 UPPER_SHORT(pkt_node->ip_laddr[0]), 453 LOWER_SHORT(pkt_node->ip_laddr[0]), 454 UPPER_SHORT(pkt_node->ip_laddr[1]), 455 LOWER_SHORT(pkt_node->ip_laddr[1]), 456 UPPER_SHORT(pkt_node->ip_laddr[2]), 457 LOWER_SHORT(pkt_node->ip_laddr[2]), 458 UPPER_SHORT(pkt_node->ip_laddr[3]), 459 LOWER_SHORT(pkt_node->ip_laddr[3]), 460 ntohs(pkt_node->tcp_localport), 461 UPPER_SHORT(pkt_node->ip_faddr[0]), 462 LOWER_SHORT(pkt_node->ip_faddr[0]), 463 UPPER_SHORT(pkt_node->ip_faddr[1]), 464 LOWER_SHORT(pkt_node->ip_faddr[1]), 465 UPPER_SHORT(pkt_node->ip_faddr[2]), 466 LOWER_SHORT(pkt_node->ip_faddr[2]), 467 UPPER_SHORT(pkt_node->ip_faddr[3]), 468 LOWER_SHORT(pkt_node->ip_faddr[3]), 469 ntohs(pkt_node->tcp_foreignport), 470 pkt_node->snd_ssthresh, 471 pkt_node->snd_cwnd, 472 pkt_node->snd_bwnd, 473 pkt_node->snd_wnd, 474 pkt_node->rcv_wnd, 475 pkt_node->snd_scale, 476 pkt_node->rcv_scale, 477 pkt_node->conn_state, 478 pkt_node->max_seg_size, 479 pkt_node->smoothed_rtt, 480 pkt_node->sack_enabled, 481 pkt_node->flags, 482 pkt_node->rxt_length, 483 pkt_node->snd_buf_hiwater, 484 pkt_node->snd_buf_cc, 485 pkt_node->rcv_buf_hiwater, 486 pkt_node->rcv_buf_cc, 487 pkt_node->sent_inflight_bytes, 488 pkt_node->t_segqlen); 489 } else { /* IPv4 packet */ 490 pkt_node->ip_laddr[0] = FIRST_OCTET(pkt_node->ip_laddr[3]); 491 pkt_node->ip_laddr[1] = SECOND_OCTET(pkt_node->ip_laddr[3]); 492 pkt_node->ip_laddr[2] = THIRD_OCTET(pkt_node->ip_laddr[3]); 493 pkt_node->ip_laddr[3] = FOURTH_OCTET(pkt_node->ip_laddr[3]); 494 pkt_node->ip_faddr[0] = FIRST_OCTET(pkt_node->ip_faddr[3]); 495 pkt_node->ip_faddr[1] = SECOND_OCTET(pkt_node->ip_faddr[3]); 496 pkt_node->ip_faddr[2] = THIRD_OCTET(pkt_node->ip_faddr[3]); 497 pkt_node->ip_faddr[3] = FOURTH_OCTET(pkt_node->ip_faddr[3]); 498 #endif /* SIFTR_IPV6 */ 499 500 /* Construct an IPv4 log message. */ 501 log_buf->ae_bytesused = snprintf(log_buf->ae_data, 502 MAX_LOG_MSG_LEN, 503 "%c,0x%08x,%jd.%06ld,%u.%u.%u.%u,%u,%u.%u.%u.%u,%u,%ld,%ld," 504 "%ld,%ld,%ld,%u,%u,%u,%u,%u,%u,%u,%d,%u,%u,%u,%u,%u,%u\n", 505 direction[pkt_node->direction], 506 pkt_node->hash, 507 (intmax_t)pkt_node->tval.tv_sec, 508 pkt_node->tval.tv_usec, 509 pkt_node->ip_laddr[0], 510 pkt_node->ip_laddr[1], 511 pkt_node->ip_laddr[2], 512 pkt_node->ip_laddr[3], 513 ntohs(pkt_node->tcp_localport), 514 pkt_node->ip_faddr[0], 515 pkt_node->ip_faddr[1], 516 pkt_node->ip_faddr[2], 517 pkt_node->ip_faddr[3], 518 ntohs(pkt_node->tcp_foreignport), 519 pkt_node->snd_ssthresh, 520 pkt_node->snd_cwnd, 521 pkt_node->snd_bwnd, 522 pkt_node->snd_wnd, 523 pkt_node->rcv_wnd, 524 pkt_node->snd_scale, 525 pkt_node->rcv_scale, 526 pkt_node->conn_state, 527 pkt_node->max_seg_size, 528 pkt_node->smoothed_rtt, 529 pkt_node->sack_enabled, 530 pkt_node->flags, 531 pkt_node->rxt_length, 532 pkt_node->snd_buf_hiwater, 533 pkt_node->snd_buf_cc, 534 pkt_node->rcv_buf_hiwater, 535 pkt_node->rcv_buf_cc, 536 pkt_node->sent_inflight_bytes, 537 pkt_node->t_segqlen); 538 #ifdef SIFTR_IPV6 539 } 540 #endif 541 542 alq_post_flags(siftr_alq, log_buf, 0); 543 } 544 545 546 static void 547 siftr_pkt_manager_thread(void *arg) 548 { 549 STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue = 550 STAILQ_HEAD_INITIALIZER(tmp_pkt_queue); 551 struct pkt_node *pkt_node, *pkt_node_temp; 552 uint8_t draining; 553 554 draining = 2; 555 556 mtx_lock(&siftr_pkt_mgr_mtx); 557 558 /* draining == 0 when queue has been flushed and it's safe to exit. */ 559 while (draining) { 560 /* 561 * Sleep until we are signalled to wake because thread has 562 * been told to exit or until 1 tick has passed. 563 */ 564 mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait", 565 1); 566 567 /* Gain exclusive access to the pkt_node queue. */ 568 mtx_lock(&siftr_pkt_queue_mtx); 569 570 /* 571 * Move pkt_queue to tmp_pkt_queue, which leaves 572 * pkt_queue empty and ready to receive more pkt_nodes. 573 */ 574 STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue); 575 576 /* 577 * We've finished making changes to the list. Unlock it 578 * so the pfil hooks can continue queuing pkt_nodes. 579 */ 580 mtx_unlock(&siftr_pkt_queue_mtx); 581 582 /* 583 * We can't hold a mutex whilst calling siftr_process_pkt 584 * because ALQ might sleep waiting for buffer space. 585 */ 586 mtx_unlock(&siftr_pkt_mgr_mtx); 587 588 /* Flush all pkt_nodes to the log file. */ 589 STAILQ_FOREACH_SAFE(pkt_node, &tmp_pkt_queue, nodes, 590 pkt_node_temp) { 591 siftr_process_pkt(pkt_node); 592 STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes); 593 free(pkt_node, M_SIFTR_PKTNODE); 594 } 595 596 KASSERT(STAILQ_EMPTY(&tmp_pkt_queue), 597 ("SIFTR tmp_pkt_queue not empty after flush")); 598 599 mtx_lock(&siftr_pkt_mgr_mtx); 600 601 /* 602 * If siftr_exit_pkt_manager_thread gets set during the window 603 * where we are draining the tmp_pkt_queue above, there might 604 * still be pkts in pkt_queue that need to be drained. 605 * Allow one further iteration to occur after 606 * siftr_exit_pkt_manager_thread has been set to ensure 607 * pkt_queue is completely empty before we kill the thread. 608 * 609 * siftr_exit_pkt_manager_thread is set only after the pfil 610 * hooks have been removed, so only 1 extra iteration 611 * is needed to drain the queue. 612 */ 613 if (siftr_exit_pkt_manager_thread) 614 draining--; 615 } 616 617 mtx_unlock(&siftr_pkt_mgr_mtx); 618 619 /* Calls wakeup on this thread's struct thread ptr. */ 620 kthread_exit(); 621 } 622 623 624 static uint32_t 625 hash_pkt(struct mbuf *m, uint32_t offset) 626 { 627 uint32_t hash; 628 629 hash = 0; 630 631 while (m != NULL && offset > m->m_len) { 632 /* 633 * The IP packet payload does not start in this mbuf, so 634 * need to figure out which mbuf it starts in and what offset 635 * into the mbuf's data region the payload starts at. 636 */ 637 offset -= m->m_len; 638 m = m->m_next; 639 } 640 641 while (m != NULL) { 642 /* Ensure there is data in the mbuf */ 643 if ((m->m_len - offset) > 0) 644 hash = hash32_buf(m->m_data + offset, 645 m->m_len - offset, hash); 646 647 m = m->m_next; 648 offset = 0; 649 } 650 651 return (hash); 652 } 653 654 655 /* 656 * Check if a given mbuf has the SIFTR mbuf tag. If it does, log the fact that 657 * it's a reinjected packet and return. If it doesn't, tag the mbuf and return. 658 * Return value >0 means the caller should skip processing this mbuf. 659 */ 660 static inline int 661 siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss) 662 { 663 if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL) 664 != NULL) { 665 if (dir == PFIL_IN) 666 ss->nskip_in_dejavu++; 667 else 668 ss->nskip_out_dejavu++; 669 670 return (1); 671 } else { 672 struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR, 673 PACKET_TAG_SIFTR, 0, M_NOWAIT); 674 if (tag == NULL) { 675 if (dir == PFIL_IN) 676 ss->nskip_in_malloc++; 677 else 678 ss->nskip_out_malloc++; 679 680 return (1); 681 } 682 683 m_tag_prepend(m, tag); 684 } 685 686 return (0); 687 } 688 689 690 /* 691 * Look up an inpcb for a packet. Return the inpcb pointer if found, or NULL 692 * otherwise. 693 */ 694 static inline struct inpcb * 695 siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport, 696 uint16_t dport, int dir, struct siftr_stats *ss) 697 { 698 struct inpcb *inp; 699 700 /* We need the tcbinfo lock. */ 701 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 702 INP_INFO_RLOCK(&V_tcbinfo); 703 704 if (dir == PFIL_IN) 705 inp = (ipver == INP_IPV4 ? 706 in_pcblookup_hash(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst, 707 dport, 0, m->m_pkthdr.rcvif) 708 : 709 #ifdef SIFTR_IPV6 710 in6_pcblookup_hash(&V_tcbinfo, 711 &((struct ip6_hdr *)ip)->ip6_src, sport, 712 &((struct ip6_hdr *)ip)->ip6_dst, dport, 0, 713 m->m_pkthdr.rcvif) 714 #else 715 NULL 716 #endif 717 ); 718 719 else 720 inp = (ipver == INP_IPV4 ? 721 in_pcblookup_hash(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src, 722 sport, 0, m->m_pkthdr.rcvif) 723 : 724 #ifdef SIFTR_IPV6 725 in6_pcblookup_hash(&V_tcbinfo, 726 &((struct ip6_hdr *)ip)->ip6_dst, dport, 727 &((struct ip6_hdr *)ip)->ip6_src, sport, 0, 728 m->m_pkthdr.rcvif) 729 #else 730 NULL 731 #endif 732 ); 733 734 /* If we can't find the inpcb, bail. */ 735 if (inp == NULL) { 736 if (dir == PFIL_IN) 737 ss->nskip_in_inpcb++; 738 else 739 ss->nskip_out_inpcb++; 740 741 INP_INFO_RUNLOCK(&V_tcbinfo); 742 } else { 743 /* Acquire the inpcb lock. */ 744 INP_UNLOCK_ASSERT(inp); 745 INP_RLOCK(inp); 746 INP_INFO_RUNLOCK(&V_tcbinfo); 747 } 748 749 return (inp); 750 } 751 752 753 static inline void 754 siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp, 755 int ipver, int dir, int inp_locally_locked) 756 { 757 #ifdef SIFTR_IPV6 758 if (ipver == INP_IPV4) { 759 pn->ip_laddr[3] = inp->inp_laddr.s_addr; 760 pn->ip_faddr[3] = inp->inp_faddr.s_addr; 761 #else 762 *((uint32_t *)pn->ip_laddr) = inp->inp_laddr.s_addr; 763 *((uint32_t *)pn->ip_faddr) = inp->inp_faddr.s_addr; 764 #endif 765 #ifdef SIFTR_IPV6 766 } else { 767 pn->ip_laddr[0] = inp->in6p_laddr.s6_addr32[0]; 768 pn->ip_laddr[1] = inp->in6p_laddr.s6_addr32[1]; 769 pn->ip_laddr[2] = inp->in6p_laddr.s6_addr32[2]; 770 pn->ip_laddr[3] = inp->in6p_laddr.s6_addr32[3]; 771 pn->ip_faddr[0] = inp->in6p_faddr.s6_addr32[0]; 772 pn->ip_faddr[1] = inp->in6p_faddr.s6_addr32[1]; 773 pn->ip_faddr[2] = inp->in6p_faddr.s6_addr32[2]; 774 pn->ip_faddr[3] = inp->in6p_faddr.s6_addr32[3]; 775 } 776 #endif 777 pn->tcp_localport = inp->inp_lport; 778 pn->tcp_foreignport = inp->inp_fport; 779 pn->snd_cwnd = tp->snd_cwnd; 780 pn->snd_wnd = tp->snd_wnd; 781 pn->rcv_wnd = tp->rcv_wnd; 782 pn->snd_bwnd = 0; /* Unused, kept for compat. */ 783 pn->snd_ssthresh = tp->snd_ssthresh; 784 pn->snd_scale = tp->snd_scale; 785 pn->rcv_scale = tp->rcv_scale; 786 pn->conn_state = tp->t_state; 787 pn->max_seg_size = tp->t_maxseg; 788 pn->smoothed_rtt = tp->t_srtt; 789 pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0; 790 pn->flags = tp->t_flags; 791 pn->rxt_length = tp->t_rxtcur; 792 pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat; 793 pn->snd_buf_cc = inp->inp_socket->so_snd.sb_cc; 794 pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat; 795 pn->rcv_buf_cc = inp->inp_socket->so_rcv.sb_cc; 796 pn->sent_inflight_bytes = tp->snd_max - tp->snd_una; 797 pn->t_segqlen = tp->t_segqlen; 798 799 /* We've finished accessing the tcb so release the lock. */ 800 if (inp_locally_locked) 801 INP_RUNLOCK(inp); 802 803 pn->ipver = ipver; 804 pn->direction = dir; 805 806 /* 807 * Significantly more accurate than using getmicrotime(), but slower! 808 * Gives true microsecond resolution at the expense of a hit to 809 * maximum pps throughput processing when SIFTR is loaded and enabled. 810 */ 811 microtime(&pn->tval); 812 } 813 814 815 /* 816 * pfil hook that is called for each IPv4 packet making its way through the 817 * stack in either direction. 818 * The pfil subsystem holds a non-sleepable mutex somewhere when 819 * calling our hook function, so we can't sleep at all. 820 * It's very important to use the M_NOWAIT flag with all function calls 821 * that support it so that they won't sleep, otherwise you get a panic. 822 */ 823 static int 824 siftr_chkpkt(void *arg, struct mbuf **m, struct ifnet *ifp, int dir, 825 struct inpcb *inp) 826 { 827 struct pkt_node *pn; 828 struct ip *ip; 829 struct tcphdr *th; 830 struct tcpcb *tp; 831 struct siftr_stats *ss; 832 unsigned int ip_hl; 833 int inp_locally_locked; 834 835 inp_locally_locked = 0; 836 ss = DPCPU_PTR(ss); 837 838 /* 839 * m_pullup is not required here because ip_{input|output} 840 * already do the heavy lifting for us. 841 */ 842 843 ip = mtod(*m, struct ip *); 844 845 /* Only continue processing if the packet is TCP. */ 846 if (ip->ip_p != IPPROTO_TCP) 847 goto ret; 848 849 /* 850 * If a kernel subsystem reinjects packets into the stack, our pfil 851 * hook will be called multiple times for the same packet. 852 * Make sure we only process unique packets. 853 */ 854 if (siftr_chkreinject(*m, dir, ss)) 855 goto ret; 856 857 if (dir == PFIL_IN) 858 ss->n_in++; 859 else 860 ss->n_out++; 861 862 /* 863 * Create a tcphdr struct starting at the correct offset 864 * in the IP packet. ip->ip_hl gives the ip header length 865 * in 4-byte words, so multiply it to get the size in bytes. 866 */ 867 ip_hl = (ip->ip_hl << 2); 868 th = (struct tcphdr *)((caddr_t)ip + ip_hl); 869 870 /* 871 * If the pfil hooks don't provide a pointer to the 872 * inpcb, we need to find it ourselves and lock it. 873 */ 874 if (!inp) { 875 /* Find the corresponding inpcb for this pkt. */ 876 inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport, 877 th->th_dport, dir, ss); 878 879 if (inp == NULL) 880 goto ret; 881 else 882 inp_locally_locked = 1; 883 } 884 885 INP_LOCK_ASSERT(inp); 886 887 /* Find the TCP control block that corresponds with this packet */ 888 tp = intotcpcb(inp); 889 890 /* 891 * If we can't find the TCP control block (happens occasionaly for a 892 * packet sent during the shutdown phase of a TCP connection), 893 * or we're in the timewait state, bail 894 */ 895 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) { 896 if (dir == PFIL_IN) 897 ss->nskip_in_tcpcb++; 898 else 899 ss->nskip_out_tcpcb++; 900 901 goto inp_unlock; 902 } 903 904 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 905 906 if (pn == NULL) { 907 if (dir == PFIL_IN) 908 ss->nskip_in_malloc++; 909 else 910 ss->nskip_out_malloc++; 911 912 goto inp_unlock; 913 } 914 915 siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked); 916 917 if (siftr_generate_hashes) { 918 if ((*m)->m_pkthdr.csum_flags & CSUM_TCP) { 919 /* 920 * For outbound packets, the TCP checksum isn't 921 * calculated yet. This is a problem for our packet 922 * hashing as the receiver will calc a different hash 923 * to ours if we don't include the correct TCP checksum 924 * in the bytes being hashed. To work around this 925 * problem, we manually calc the TCP checksum here in 926 * software. We unset the CSUM_TCP flag so the lower 927 * layers don't recalc it. 928 */ 929 (*m)->m_pkthdr.csum_flags &= ~CSUM_TCP; 930 931 /* 932 * Calculate the TCP checksum in software and assign 933 * to correct TCP header field, which will follow the 934 * packet mbuf down the stack. The trick here is that 935 * tcp_output() sets th->th_sum to the checksum of the 936 * pseudo header for us already. Because of the nature 937 * of the checksumming algorithm, we can sum over the 938 * entire IP payload (i.e. TCP header and data), which 939 * will include the already calculated pseduo header 940 * checksum, thus giving us the complete TCP checksum. 941 * 942 * To put it in simple terms, if checksum(1,2,3,4)=10, 943 * then checksum(1,2,3,4,5) == checksum(10,5). 944 * This property is what allows us to "cheat" and 945 * checksum only the IP payload which has the TCP 946 * th_sum field populated with the pseudo header's 947 * checksum, and not need to futz around checksumming 948 * pseudo header bytes and TCP header/data in one hit. 949 * Refer to RFC 1071 for more info. 950 * 951 * NB: in_cksum_skip(struct mbuf *m, int len, int skip) 952 * in_cksum_skip 2nd argument is NOT the number of 953 * bytes to read from the mbuf at "skip" bytes offset 954 * from the start of the mbuf (very counter intuitive!). 955 * The number of bytes to read is calculated internally 956 * by the function as len-skip i.e. to sum over the IP 957 * payload (TCP header + data) bytes, it is INCORRECT 958 * to call the function like this: 959 * in_cksum_skip(at, ip->ip_len - offset, offset) 960 * Rather, it should be called like this: 961 * in_cksum_skip(at, ip->ip_len, offset) 962 * which means read "ip->ip_len - offset" bytes from 963 * the mbuf cluster "at" at offset "offset" bytes from 964 * the beginning of the "at" mbuf's data pointer. 965 */ 966 th->th_sum = in_cksum_skip(*m, ip->ip_len, ip_hl); 967 } 968 969 /* 970 * XXX: Having to calculate the checksum in software and then 971 * hash over all bytes is really inefficient. Would be nice to 972 * find a way to create the hash and checksum in the same pass 973 * over the bytes. 974 */ 975 pn->hash = hash_pkt(*m, ip_hl); 976 } 977 978 mtx_lock(&siftr_pkt_queue_mtx); 979 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 980 mtx_unlock(&siftr_pkt_queue_mtx); 981 goto ret; 982 983 inp_unlock: 984 if (inp_locally_locked) 985 INP_RUNLOCK(inp); 986 987 ret: 988 /* Returning 0 ensures pfil will not discard the pkt */ 989 return (0); 990 } 991 992 993 #ifdef SIFTR_IPV6 994 static int 995 siftr_chkpkt6(void *arg, struct mbuf **m, struct ifnet *ifp, int dir, 996 struct inpcb *inp) 997 { 998 struct pkt_node *pn; 999 struct ip6_hdr *ip6; 1000 struct tcphdr *th; 1001 struct tcpcb *tp; 1002 struct siftr_stats *ss; 1003 unsigned int ip6_hl; 1004 int inp_locally_locked; 1005 1006 inp_locally_locked = 0; 1007 ss = DPCPU_PTR(ss); 1008 1009 /* 1010 * m_pullup is not required here because ip6_{input|output} 1011 * already do the heavy lifting for us. 1012 */ 1013 1014 ip6 = mtod(*m, struct ip6_hdr *); 1015 1016 /* 1017 * Only continue processing if the packet is TCP 1018 * XXX: We should follow the next header fields 1019 * as shown on Pg 6 RFC 2460, but right now we'll 1020 * only check pkts that have no extension headers. 1021 */ 1022 if (ip6->ip6_nxt != IPPROTO_TCP) 1023 goto ret6; 1024 1025 /* 1026 * If a kernel subsystem reinjects packets into the stack, our pfil 1027 * hook will be called multiple times for the same packet. 1028 * Make sure we only process unique packets. 1029 */ 1030 if (siftr_chkreinject(*m, dir, ss)) 1031 goto ret6; 1032 1033 if (dir == PFIL_IN) 1034 ss->n_in++; 1035 else 1036 ss->n_out++; 1037 1038 ip6_hl = sizeof(struct ip6_hdr); 1039 1040 /* 1041 * Create a tcphdr struct starting at the correct offset 1042 * in the ipv6 packet. ip->ip_hl gives the ip header length 1043 * in 4-byte words, so multiply it to get the size in bytes. 1044 */ 1045 th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl); 1046 1047 /* 1048 * For inbound packets, the pfil hooks don't provide a pointer to the 1049 * inpcb, so we need to find it ourselves and lock it. 1050 */ 1051 if (!inp) { 1052 /* Find the corresponding inpcb for this pkt. */ 1053 inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m, 1054 th->th_sport, th->th_dport, dir, ss); 1055 1056 if (inp == NULL) 1057 goto ret6; 1058 else 1059 inp_locally_locked = 1; 1060 } 1061 1062 /* Find the TCP control block that corresponds with this packet. */ 1063 tp = intotcpcb(inp); 1064 1065 /* 1066 * If we can't find the TCP control block (happens occasionaly for a 1067 * packet sent during the shutdown phase of a TCP connection), 1068 * or we're in the timewait state, bail. 1069 */ 1070 if (tp == NULL || inp->inp_flags & INP_TIMEWAIT) { 1071 if (dir == PFIL_IN) 1072 ss->nskip_in_tcpcb++; 1073 else 1074 ss->nskip_out_tcpcb++; 1075 1076 goto inp_unlock6; 1077 } 1078 1079 pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO); 1080 1081 if (pn == NULL) { 1082 if (dir == PFIL_IN) 1083 ss->nskip_in_malloc++; 1084 else 1085 ss->nskip_out_malloc++; 1086 1087 goto inp_unlock6; 1088 } 1089 1090 siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked); 1091 1092 /* XXX: Figure out how to generate hashes for IPv6 packets. */ 1093 1094 mtx_lock(&siftr_pkt_queue_mtx); 1095 STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes); 1096 mtx_unlock(&siftr_pkt_queue_mtx); 1097 goto ret6; 1098 1099 inp_unlock6: 1100 if (inp_locally_locked) 1101 INP_RUNLOCK(inp); 1102 1103 ret6: 1104 /* Returning 0 ensures pfil will not discard the pkt. */ 1105 return (0); 1106 } 1107 #endif /* #ifdef SIFTR_IPV6 */ 1108 1109 1110 static int 1111 siftr_pfil(int action) 1112 { 1113 struct pfil_head *pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 1114 #ifdef SIFTR_IPV6 1115 struct pfil_head *pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 1116 #endif 1117 1118 if (action == HOOK) { 1119 pfil_add_hook(siftr_chkpkt, NULL, 1120 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1121 #ifdef SIFTR_IPV6 1122 pfil_add_hook(siftr_chkpkt6, NULL, 1123 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1124 #endif 1125 } else if (action == UNHOOK) { 1126 pfil_remove_hook(siftr_chkpkt, NULL, 1127 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet); 1128 #ifdef SIFTR_IPV6 1129 pfil_remove_hook(siftr_chkpkt6, NULL, 1130 PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh_inet6); 1131 #endif 1132 } 1133 1134 return (0); 1135 } 1136 1137 1138 static int 1139 siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS) 1140 { 1141 struct alq *new_alq; 1142 int error; 1143 1144 if (req->newptr == NULL) 1145 goto skip; 1146 1147 /* If old filename and new filename are different. */ 1148 if (strncmp(siftr_logfile, (char *)req->newptr, PATH_MAX)) { 1149 1150 error = alq_open(&new_alq, req->newptr, curthread->td_ucred, 1151 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1152 1153 /* Bail if unable to create new alq. */ 1154 if (error) 1155 return (1); 1156 1157 /* 1158 * If disabled, siftr_alq == NULL so we simply close 1159 * the alq as we've proved it can be opened. 1160 * If enabled, close the existing alq and switch the old 1161 * for the new. 1162 */ 1163 if (siftr_alq == NULL) 1164 alq_close(new_alq); 1165 else { 1166 alq_close(siftr_alq); 1167 siftr_alq = new_alq; 1168 } 1169 } 1170 1171 skip: 1172 return (sysctl_handle_string(oidp, arg1, arg2, req)); 1173 } 1174 1175 1176 static int 1177 siftr_manage_ops(uint8_t action) 1178 { 1179 struct siftr_stats totalss; 1180 struct timeval tval; 1181 struct flow_hash_node *counter, *tmp_counter; 1182 struct sbuf *s; 1183 int i, key_index, ret, error; 1184 uint32_t bytes_to_write, total_skipped_pkts; 1185 uint16_t lport, fport; 1186 uint8_t *key, ipver; 1187 1188 #ifdef SIFTR_IPV6 1189 uint32_t laddr[4]; 1190 uint32_t faddr[4]; 1191 #else 1192 uint8_t laddr[4]; 1193 uint8_t faddr[4]; 1194 #endif 1195 1196 error = 0; 1197 total_skipped_pkts = 0; 1198 1199 /* Init an autosizing sbuf that initially holds 200 chars. */ 1200 if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL) 1201 return (-1); 1202 1203 if (action == SIFTR_ENABLE) { 1204 /* 1205 * Create our alq 1206 * XXX: We should abort if alq_open fails! 1207 */ 1208 alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred, 1209 SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0); 1210 1211 STAILQ_INIT(&pkt_queue); 1212 1213 DPCPU_ZERO(ss); 1214 1215 siftr_exit_pkt_manager_thread = 0; 1216 1217 ret = kthread_add(&siftr_pkt_manager_thread, NULL, NULL, 1218 &siftr_pkt_manager_thr, RFNOWAIT, 0, 1219 "siftr_pkt_manager_thr"); 1220 1221 siftr_pfil(HOOK); 1222 1223 microtime(&tval); 1224 1225 sbuf_printf(s, 1226 "enable_time_secs=%jd\tenable_time_usecs=%06ld\t" 1227 "siftrver=%s\thz=%u\ttcp_rtt_scale=%u\tsysname=%s\t" 1228 "sysver=%u\tipmode=%u\n", 1229 (intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR, hz, 1230 TCP_RTT_SCALE, SYS_NAME, __FreeBSD_version, SIFTR_IPMODE); 1231 1232 sbuf_finish(s); 1233 alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK); 1234 1235 } else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) { 1236 /* 1237 * Remove the pfil hook functions. All threads currently in 1238 * the hook functions are allowed to exit before siftr_pfil() 1239 * returns. 1240 */ 1241 siftr_pfil(UNHOOK); 1242 1243 /* This will block until the pkt manager thread unlocks it. */ 1244 mtx_lock(&siftr_pkt_mgr_mtx); 1245 1246 /* Tell the pkt manager thread that it should exit now. */ 1247 siftr_exit_pkt_manager_thread = 1; 1248 1249 /* 1250 * Wake the pkt_manager thread so it realises that 1251 * siftr_exit_pkt_manager_thread == 1 and exits gracefully. 1252 * The wakeup won't be delivered until we unlock 1253 * siftr_pkt_mgr_mtx so this isn't racy. 1254 */ 1255 wakeup(&wait_for_pkt); 1256 1257 /* Wait for the pkt_manager thread to exit. */ 1258 mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT, 1259 "thrwait", 0); 1260 1261 siftr_pkt_manager_thr = NULL; 1262 mtx_unlock(&siftr_pkt_mgr_mtx); 1263 1264 totalss.n_in = DPCPU_VARSUM(ss, n_in); 1265 totalss.n_out = DPCPU_VARSUM(ss, n_out); 1266 totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc); 1267 totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc); 1268 totalss.nskip_in_mtx = DPCPU_VARSUM(ss, nskip_in_mtx); 1269 totalss.nskip_out_mtx = DPCPU_VARSUM(ss, nskip_out_mtx); 1270 totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb); 1271 totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb); 1272 totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb); 1273 totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb); 1274 1275 total_skipped_pkts = totalss.nskip_in_malloc + 1276 totalss.nskip_out_malloc + totalss.nskip_in_mtx + 1277 totalss.nskip_out_mtx + totalss.nskip_in_tcpcb + 1278 totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb + 1279 totalss.nskip_out_inpcb; 1280 1281 microtime(&tval); 1282 1283 sbuf_printf(s, 1284 "disable_time_secs=%jd\tdisable_time_usecs=%06ld\t" 1285 "num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t" 1286 "total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t" 1287 "num_outbound_skipped_pkts_malloc=%u\t" 1288 "num_inbound_skipped_pkts_mtx=%u\t" 1289 "num_outbound_skipped_pkts_mtx=%u\t" 1290 "num_inbound_skipped_pkts_tcpcb=%u\t" 1291 "num_outbound_skipped_pkts_tcpcb=%u\t" 1292 "num_inbound_skipped_pkts_inpcb=%u\t" 1293 "num_outbound_skipped_pkts_inpcb=%u\t" 1294 "total_skipped_tcp_pkts=%u\tflow_list=", 1295 (intmax_t)tval.tv_sec, 1296 tval.tv_usec, 1297 (uintmax_t)totalss.n_in, 1298 (uintmax_t)totalss.n_out, 1299 (uintmax_t)(totalss.n_in + totalss.n_out), 1300 totalss.nskip_in_malloc, 1301 totalss.nskip_out_malloc, 1302 totalss.nskip_in_mtx, 1303 totalss.nskip_out_mtx, 1304 totalss.nskip_in_tcpcb, 1305 totalss.nskip_out_tcpcb, 1306 totalss.nskip_in_inpcb, 1307 totalss.nskip_out_inpcb, 1308 total_skipped_pkts); 1309 1310 /* 1311 * Iterate over the flow hash, printing a summary of each 1312 * flow seen and freeing any malloc'd memory. 1313 * The hash consists of an array of LISTs (man 3 queue). 1314 */ 1315 for (i = 0; i < siftr_hashmask; i++) { 1316 LIST_FOREACH_SAFE(counter, counter_hash + i, nodes, 1317 tmp_counter) { 1318 key = counter->key; 1319 key_index = 1; 1320 1321 ipver = key[0]; 1322 1323 memcpy(laddr, key + key_index, sizeof(laddr)); 1324 key_index += sizeof(laddr); 1325 memcpy(&lport, key + key_index, sizeof(lport)); 1326 key_index += sizeof(lport); 1327 memcpy(faddr, key + key_index, sizeof(faddr)); 1328 key_index += sizeof(faddr); 1329 memcpy(&fport, key + key_index, sizeof(fport)); 1330 1331 #ifdef SIFTR_IPV6 1332 laddr[3] = ntohl(laddr[3]); 1333 faddr[3] = ntohl(faddr[3]); 1334 1335 if (ipver == INP_IPV6) { 1336 laddr[0] = ntohl(laddr[0]); 1337 laddr[1] = ntohl(laddr[1]); 1338 laddr[2] = ntohl(laddr[2]); 1339 faddr[0] = ntohl(faddr[0]); 1340 faddr[1] = ntohl(faddr[1]); 1341 faddr[2] = ntohl(faddr[2]); 1342 1343 sbuf_printf(s, 1344 "%x:%x:%x:%x:%x:%x:%x:%x;%u-" 1345 "%x:%x:%x:%x:%x:%x:%x:%x;%u,", 1346 UPPER_SHORT(laddr[0]), 1347 LOWER_SHORT(laddr[0]), 1348 UPPER_SHORT(laddr[1]), 1349 LOWER_SHORT(laddr[1]), 1350 UPPER_SHORT(laddr[2]), 1351 LOWER_SHORT(laddr[2]), 1352 UPPER_SHORT(laddr[3]), 1353 LOWER_SHORT(laddr[3]), 1354 ntohs(lport), 1355 UPPER_SHORT(faddr[0]), 1356 LOWER_SHORT(faddr[0]), 1357 UPPER_SHORT(faddr[1]), 1358 LOWER_SHORT(faddr[1]), 1359 UPPER_SHORT(faddr[2]), 1360 LOWER_SHORT(faddr[2]), 1361 UPPER_SHORT(faddr[3]), 1362 LOWER_SHORT(faddr[3]), 1363 ntohs(fport)); 1364 } else { 1365 laddr[0] = FIRST_OCTET(laddr[3]); 1366 laddr[1] = SECOND_OCTET(laddr[3]); 1367 laddr[2] = THIRD_OCTET(laddr[3]); 1368 laddr[3] = FOURTH_OCTET(laddr[3]); 1369 faddr[0] = FIRST_OCTET(faddr[3]); 1370 faddr[1] = SECOND_OCTET(faddr[3]); 1371 faddr[2] = THIRD_OCTET(faddr[3]); 1372 faddr[3] = FOURTH_OCTET(faddr[3]); 1373 #endif 1374 sbuf_printf(s, 1375 "%u.%u.%u.%u;%u-%u.%u.%u.%u;%u,", 1376 laddr[0], 1377 laddr[1], 1378 laddr[2], 1379 laddr[3], 1380 ntohs(lport), 1381 faddr[0], 1382 faddr[1], 1383 faddr[2], 1384 faddr[3], 1385 ntohs(fport)); 1386 #ifdef SIFTR_IPV6 1387 } 1388 #endif 1389 1390 free(counter, M_SIFTR_HASHNODE); 1391 } 1392 1393 LIST_INIT(counter_hash + i); 1394 } 1395 1396 sbuf_printf(s, "\n"); 1397 sbuf_finish(s); 1398 1399 i = 0; 1400 do { 1401 bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i); 1402 alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK); 1403 i += bytes_to_write; 1404 } while (i < sbuf_len(s)); 1405 1406 alq_close(siftr_alq); 1407 siftr_alq = NULL; 1408 } 1409 1410 sbuf_delete(s); 1411 1412 /* 1413 * XXX: Should be using ret to check if any functions fail 1414 * and set error appropriately 1415 */ 1416 1417 return (error); 1418 } 1419 1420 1421 static int 1422 siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS) 1423 { 1424 if (req->newptr == NULL) 1425 goto skip; 1426 1427 /* If the value passed in isn't 0 or 1, return an error. */ 1428 if (CAST_PTR_INT(req->newptr) != 0 && CAST_PTR_INT(req->newptr) != 1) 1429 return (1); 1430 1431 /* If we are changing state (0 to 1 or 1 to 0). */ 1432 if (CAST_PTR_INT(req->newptr) != siftr_enabled ) 1433 if (siftr_manage_ops(CAST_PTR_INT(req->newptr))) { 1434 siftr_manage_ops(SIFTR_DISABLE); 1435 return (1); 1436 } 1437 1438 skip: 1439 return (sysctl_handle_int(oidp, arg1, arg2, req)); 1440 } 1441 1442 1443 static void 1444 siftr_shutdown_handler(void *arg) 1445 { 1446 siftr_manage_ops(SIFTR_DISABLE); 1447 } 1448 1449 1450 /* 1451 * Module is being unloaded or machine is shutting down. Take care of cleanup. 1452 */ 1453 static int 1454 deinit_siftr(void) 1455 { 1456 /* Cleanup. */ 1457 siftr_manage_ops(SIFTR_DISABLE); 1458 hashdestroy(counter_hash, M_SIFTR, siftr_hashmask); 1459 mtx_destroy(&siftr_pkt_queue_mtx); 1460 mtx_destroy(&siftr_pkt_mgr_mtx); 1461 1462 return (0); 1463 } 1464 1465 1466 /* 1467 * Module has just been loaded into the kernel. 1468 */ 1469 static int 1470 init_siftr(void) 1471 { 1472 EVENTHANDLER_REGISTER(shutdown_pre_sync, siftr_shutdown_handler, NULL, 1473 SHUTDOWN_PRI_FIRST); 1474 1475 /* Initialise our flow counter hash table. */ 1476 counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR, 1477 &siftr_hashmask); 1478 1479 mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF); 1480 mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF); 1481 1482 /* Print message to the user's current terminal. */ 1483 uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n" 1484 " http://caia.swin.edu.au/urp/newtcp\n\n", 1485 MODVERSION_STR); 1486 1487 return (0); 1488 } 1489 1490 1491 /* 1492 * This is the function that is called to load and unload the module. 1493 * When the module is loaded, this function is called once with 1494 * "what" == MOD_LOAD 1495 * When the module is unloaded, this function is called twice with 1496 * "what" = MOD_QUIESCE first, followed by "what" = MOD_UNLOAD second 1497 * When the system is shut down e.g. CTRL-ALT-DEL or using the shutdown command, 1498 * this function is called once with "what" = MOD_SHUTDOWN 1499 * When the system is shut down, the handler isn't called until the very end 1500 * of the shutdown sequence i.e. after the disks have been synced. 1501 */ 1502 static int 1503 siftr_load_handler(module_t mod, int what, void *arg) 1504 { 1505 int ret; 1506 1507 switch (what) { 1508 case MOD_LOAD: 1509 ret = init_siftr(); 1510 break; 1511 1512 case MOD_QUIESCE: 1513 case MOD_SHUTDOWN: 1514 ret = deinit_siftr(); 1515 break; 1516 1517 case MOD_UNLOAD: 1518 ret = 0; 1519 break; 1520 1521 default: 1522 ret = EINVAL; 1523 break; 1524 } 1525 1526 return (ret); 1527 } 1528 1529 1530 static moduledata_t siftr_mod = { 1531 .name = "siftr", 1532 .evhand = siftr_load_handler, 1533 }; 1534 1535 /* 1536 * Param 1: name of the kernel module 1537 * Param 2: moduledata_t struct containing info about the kernel module 1538 * and the execution entry point for the module 1539 * Param 3: From sysinit_sub_id enumeration in /usr/include/sys/kernel.h 1540 * Defines the module initialisation order 1541 * Param 4: From sysinit_elem_order enumeration in /usr/include/sys/kernel.h 1542 * Defines the initialisation order of this kld relative to others 1543 * within the same subsystem as defined by param 3 1544 */ 1545 DECLARE_MODULE(siftr, siftr_mod, SI_SUB_SMP, SI_ORDER_ANY); 1546 MODULE_DEPEND(siftr, alq, 1, 1, 1); 1547 MODULE_VERSION(siftr, MODVERSION); 1548