1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
| #!/bin/bash
if [ "$(id -u)" -ne 0 ]; then echo "This script must be run as root" >&2 exit 1 fi
install_dependencies() { echo "Installing dependencies..." if command -v apt-get &> /dev/null; then apt-get update apt-get install -y build-essential linux-headers-$(uname -r) iptables dnsutils elif command -v yum &> /dev/null; then yum install -y kernel-devel gcc make iptables bind-utils elif command -v dnf &> /dev/null; then dnf install -y kernel-devel gcc make iptables bind-utils elif command -v pacman &> /dev/null; then pacman -Sy --noconfirm base-devel linux-headers iptables bind-tools else echo "Unsupported package manager. Please install build-essential, linux headers, and iptables manually." exit 1 fi }
resolve_domain() { local domain="$1" local resolved_ip echo "Resolving domain $domain to IP address..." if command -v dig &> /dev/null; then resolved_ip=$(dig +short "$domain" | grep -v "\.$" | head -n 1) elif command -v host &> /dev/null; then resolved_ip=$(host "$domain" | grep "has address" | head -n 1 | awk '{print $NF}') elif command -v nslookup &> /dev/null; then resolved_ip=$(nslookup "$domain" | grep -A2 "Name:" | grep "Address:" | head -n 1 | awk '{print $2}') else echo "No DNS resolution tools found. Please install dig, host, or nslookup." exit 1 fi if [ -z "$resolved_ip" ]; then echo "Failed to resolve domain $domain to IP address." exit 1 fi echo "Domain $domain resolved to IP: $resolved_ip" echo "$resolved_ip" }
update_tcp_none_module() { echo "Updating tcp_none module with unlimited cwnd..." if lsmod | grep -q "tcp_none"; then active_connections=$(ss -ti | grep none) if [ -n "$active_connections" ]; then echo "Active connections found using tcp_none module:" echo "$active_connections" echo "These connections will be terminated to update the module." read -p "Press Enter to continue or Ctrl+C to abort..." </dev/tty
echo "Finding and terminating processes with active connections to $TARGET_IP..." TARGET_CONNS=$(ss -tnp | grep "$TARGET_IP") if [ -n "$TARGET_CONNS" ]; then PIDS=$(echo "$TARGET_CONNS" | grep -oP 'pid=\K\d+' | sort -u) if [ -n "$PIDS" ]; then for PID in $PIDS; do echo "Terminating process $PID" kill -9 $PID 2>/dev/null || echo "Could not kill process $PID" done fi fi echo "Adding firewall rules to reset connections to $TARGET_IP..." iptables -F iptables -A INPUT -p tcp -s $TARGET_IP -j REJECT --reject-with tcp-reset iptables -A OUTPUT -p tcp -d $TARGET_IP -j REJECT --reject-with tcp-reset echo "Enabling aggressive TCP connection cleanup..." echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle 2>/dev/null || true echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse 2>/dev/null || true echo 1 > /proc/sys/net/ipv4/tcp_abort_on_overflow 2>/dev/null || true echo 10 > /proc/sys/net/ipv4/tcp_fin_timeout 2>/dev/null || true echo "Directly targeting connections using 'none' congestion control..." ss -tn state established | grep "$TARGET_IP" | awk '{print $4}' | while read local_addr; do if [[ "$local_addr" == *:* ]]; then local_ip=$(echo $local_addr | cut -d: -f1) local_port=$(echo $local_addr | cut -d: -f2) echo "Closing connection on local port $local_port" iptables -A INPUT -p tcp --dport $local_port -j REJECT --reject-with tcp-reset iptables -A OUTPUT -p tcp --sport $local_port -j REJECT --reject-with tcp-reset fi done echo "Waiting for connections to terminate..." sleep 5 iptables -F fi
echo "Attempting to unload tcp_none module..." rmmod tcp_none 2>/dev/null if lsmod | grep -q "tcp_none"; then echo "Module still in use. Trying system-wide connection reset..." echo "WARNING: This will reset ALL TCP connections on the system!" read -p "Continue? [y/N] " confirm </dev/tty if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then DEFAULT_INTERFACE=$(ip route | grep default | head -n 1 | awk '{print $5}') if [ -n "$DEFAULT_INTERFACE" ]; then echo "Temporarily disabling interface $DEFAULT_INTERFACE..." ip link set $DEFAULT_INTERFACE down sleep 2 ip link set $DEFAULT_INTERFACE up sleep 2 fi rmmod tcp_none 2>/dev/null fi if lsmod | grep -q "tcp_none"; then echo "Could not unload module. You may need to reboot the system." echo "Continuing anyway to try compiling the new module..." else echo "Successfully unloaded tcp_none module." fi else echo "Successfully unloaded tcp_none module." fi fi MODULE_DIR=$(mktemp -d) cd "$MODULE_DIR" echo "Creating updated tcp_none module with unlimited cwnd..." cat > Makefile << 'EOF' obj-m += tcp_none.o
all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean EOF
cat > tcp_none.c << 'EOF'
static struct tcp_congestion_ops tcp_none;
static void tcp_none_init(struct sock *sk) { /* Set congestion window to maximum possible value */ struct tcp_sock *tp = tcp_sk(sk); tp->snd_cwnd = UINT_MAX / 2; /* Use a very large value (near max of u32) */ tp->snd_cwnd_clamp = UINT_MAX / 2; /* Also set the clamp to a very high value */ }
static void tcp_none_cong_avoid(struct sock *sk, u32 ack, u32 acked) { /* Always keep the congestion window maximized */ struct tcp_sock *tp = tcp_sk(sk); /* If cwnd somehow gets reduced, set it back to maximum */ if (tp->snd_cwnd < UINT_MAX / 2) { tp->snd_cwnd = UINT_MAX / 2; } }
static u32 tcp_none_ssthresh(struct sock *sk) { /* Return maximum value */ return TCP_INFINITE_SSTHRESH; }
static u32 tcp_none_undo_cwnd(struct sock *sk) { /* Return maximum value instead of current CWND */ return UINT_MAX / 2; }
static void tcp_none_cwnd_event(struct sock *sk, enum tcp_ca_event event) { /* Force cwnd to stay at maximum regardless of events */ struct tcp_sock *tp = tcp_sk(sk); tp->snd_cwnd = UINT_MAX / 2; }
static void tcp_none_pkts_acked(struct sock *sk, const struct ack_sample *sample) { /* Do nothing */ }
static struct tcp_congestion_ops tcp_none = { .init = tcp_none_init, .ssthresh = tcp_none_ssthresh, .cong_avoid = tcp_none_cong_avoid, .undo_cwnd = tcp_none_undo_cwnd, .cwnd_event = tcp_none_cwnd_event, .pkts_acked = tcp_none_pkts_acked,
.owner = THIS_MODULE, .name = "none", };
static int __init tcp_none_register(void) { printk(KERN_INFO "TCP None Congestion Control loaded with unlimited cwnd\n"); return tcp_register_congestion_control(&tcp_none); }
static void __exit tcp_none_unregister(void) { printk(KERN_INFO "TCP None Congestion Control unloaded\n"); tcp_unregister_congestion_control(&tcp_none); }
module_init(tcp_none_register); module_exit(tcp_none_unregister);
MODULE_AUTHOR("Kernel Module Generator"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("TCP Congestion Control with No Algorithm and Unlimited CWND"); EOF
echo "Compiling updated kernel module..." make
echo "Loading updated kernel module..." insmod tcp_none.ko
if ! grep -q "none" /proc/sys/net/ipv4/tcp_available_congestion_control; then echo "Failed to load 'none' congestion control module." exit 1 fi
echo "Successfully updated and loaded 'none' congestion control module with unlimited cwnd." echo "Updating routes to use new module..." if ip route help 2>&1 | grep -q "congctl"; then ip route change default via "$DEFAULT_GATEWAY" dev "$DEFAULT_INTERFACE" table nocongestion congctl none else echo "congctl option not supported on this system" ip route change default via "$DEFAULT_GATEWAY" dev "$DEFAULT_INTERFACE" table nocongestion echo "Using alternative method to set congestion control" echo "none" > /proc/sys/net/ipv4/tcp_congestion_control fi cd - >/dev/null }
if [ "$1" == "--update-module" ]; then if [ -f /var/lib/no-congestion-target ]; then TARGET_IP=$(cat /var/lib/no-congestion-target) echo "Updating module for existing target: $TARGET_IP" DEFAULT_ROUTE=$(ip route | grep default | head -n 1) DEFAULT_INTERFACE=$(echo "$DEFAULT_ROUTE" | awk '{print $5}') DEFAULT_GATEWAY=$(echo "$DEFAULT_ROUTE" | awk '{print $3}') update_tcp_none_module exit 0 else echo "No target IP found. Please run the script with a target IP first." exit 1 fi fi
if [ $# -ne 1 ]; then echo "Usage: $0 <target_ip_or_domain>" echo " or: $0 --update-module" exit 1 fi
if [[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then TARGET_IP="$1" echo "Using IP address: $TARGET_IP" else DOMAIN="$1" TARGET_IP=$(resolve_domain "$DOMAIN") echo "Using domain $DOMAIN with resolved IP: $TARGET_IP" fi
echo "Setting up no congestion control for connections to $TARGET_IP"
install_dependencies
if ! grep -q "none" /proc/sys/net/ipv4/tcp_available_congestion_control; then echo "'none' congestion control is not available. Creating kernel module..." MODULE_DIR=$(mktemp -d) cd "$MODULE_DIR" cat > Makefile << 'EOF' obj-m += tcp_none.o
all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean EOF
cat > tcp_none.c << 'EOF'
static struct tcp_congestion_ops tcp_none;
static void tcp_none_init(struct sock *sk) { /* Set congestion window to maximum possible value */ struct tcp_sock *tp = tcp_sk(sk); tp->snd_cwnd = UINT_MAX / 2; /* Use a very large value (near max of u32) */ tp->snd_cwnd_clamp = UINT_MAX / 2; /* Also set the clamp to a very high value */ }
static void tcp_none_cong_avoid(struct sock *sk, u32 ack, u32 acked) { /* Always keep the congestion window maximized */ struct tcp_sock *tp = tcp_sk(sk); /* If cwnd somehow gets reduced, set it back to maximum */ if (tp->snd_cwnd < UINT_MAX / 2) { tp->snd_cwnd = UINT_MAX / 2; } }
static u32 tcp_none_ssthresh(struct sock *sk) { /* Return maximum value */ return TCP_INFINITE_SSTHRESH; }
static u32 tcp_none_undo_cwnd(struct sock *sk) { /* Return maximum value instead of current CWND */ return UINT_MAX / 2; }
static void tcp_none_cwnd_event(struct sock *sk, enum tcp_ca_event event) { /* Force cwnd to stay at maximum regardless of events */ struct tcp_sock *tp = tcp_sk(sk); tp->snd_cwnd = UINT_MAX / 2; }
static void tcp_none_pkts_acked(struct sock *sk, const struct ack_sample *sample) { /* Do nothing */ }
static struct tcp_congestion_ops tcp_none = { .init = tcp_none_init, .ssthresh = tcp_none_ssthresh, .cong_avoid = tcp_none_cong_avoid, .undo_cwnd = tcp_none_undo_cwnd, .cwnd_event = tcp_none_cwnd_event, .pkts_acked = tcp_none_pkts_acked,
.owner = THIS_MODULE, .name = "none", };
static int __init tcp_none_register(void) { printk(KERN_INFO "TCP None Congestion Control loaded with unlimited cwnd\n"); return tcp_register_congestion_control(&tcp_none); }
static void __exit tcp_none_unregister(void) { printk(KERN_INFO "TCP None Congestion Control unloaded\n"); tcp_unregister_congestion_control(&tcp_none); }
module_init(tcp_none_register); module_exit(tcp_none_unregister);
MODULE_AUTHOR("Kernel Module Generator"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("TCP Congestion Control with No Algorithm and Unlimited CWND"); EOF
echo "Compiling kernel module..." make
echo "Loading kernel module..." insmod tcp_none.ko
if ! grep -q "none" /proc/sys/net/ipv4/tcp_available_congestion_control; then echo "Failed to load 'none' congestion control module." exit 1 fi
echo "Successfully created and loaded 'none' congestion control module." fi
echo "Setting up iptables to mark packets to $TARGET_IP..."
iptables -t mangle -D OUTPUT -p tcp -d "$TARGET_IP" -j MARK --set-mark 1 2>/dev/null || true iptables -t mangle -D INPUT -p tcp -s "$TARGET_IP" -j CONNMARK --set-mark 1 2>/dev/null || true iptables -t mangle -D OUTPUT -p tcp -m connmark --mark 1 -j MARK --set-mark 1 2>/dev/null || true iptables -t mangle -F
iptables -t mangle -A OUTPUT -p tcp -d "$TARGET_IP" -j MARK --set-mark 1
iptables -t mangle -A INPUT -p tcp -s "$TARGET_IP" -j CONNMARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp -m connmark --mark 1 -j MARK --set-mark 1
ip rule del fwmark 1 table nocongestion 2>/dev/null || true ip route del table nocongestion 2>/dev/null || true
grep -q "^200 nocongestion" /etc/iproute2/rt_tables || echo "200 nocongestion" >> /etc/iproute2/rt_tables
DEFAULT_ROUTE=$(ip route | grep default | head -n 1) DEFAULT_INTERFACE=$(echo "$DEFAULT_ROUTE" | awk '{print $5}') DEFAULT_GATEWAY=$(echo "$DEFAULT_ROUTE" | awk '{print $3}')
echo "Setting up routing for marked packets..."
ip route del default table nocongestion 2>/dev/null || true
ip route add default via "$DEFAULT_GATEWAY" dev "$DEFAULT_INTERFACE" table nocongestion 2>/dev/null || echo "Route already exists, continuing..."
ip rule show | grep -q "fwmark 1 lookup nocongestion" || ip rule add fwmark 1 table nocongestion
echo "Setting congestion control algorithm to none specifically for $TARGET_IP..." if grep -q "none" /proc/sys/net/ipv4/tcp_available_congestion_control; then ip route change default via "$DEFAULT_GATEWAY" dev "$DEFAULT_INTERFACE" table nocongestion congctl none echo "Congestion control set to 'none' only for connections to $TARGET_IP" echo "Global congestion control remains: $(cat /proc/sys/net/ipv4/tcp_congestion_control)" else echo "WARNING: 'none' congestion control is not available." echo "Using the default congestion control algorithm." fi
cat > /tmp/disable_nagle.c << 'EOF'
// Override connect function to disable Nagle's algorithm int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { int (*original_connect)(int, const struct sockaddr *, socklen_t); original_connect = dlsym(RTLD_NEXT, "connect"); int result = original_connect(sockfd, addr, addrlen); // Disable Nagle's algorithm by setting TCP_NODELAY int flag = 1; setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); return result; } EOF
echo "Compiling TCP_NODELAY library..." gcc -shared -fPIC -o /tmp/disable_nagle.so /tmp/disable_nagle.c -ldl mv /tmp/disable_nagle.so /usr/local/lib/
cat > /usr/local/bin/enforce_no_congestion.sh << EOF #!/bin/bash
# Continuously enforce congestion control settings for $TARGET_IP echo "Starting congestion control enforcement service for $TARGET_IP"
# Function to check and set congestion control enforce_cc() { # Verify route settings are in place if ! ip route show table nocongestion | grep -q "congctl none"; then echo "Resetting route congestion control to none" ip route change default via "$DEFAULT_GATEWAY" dev "$DEFAULT_INTERFACE" table nocongestion congctl none fi # Verify iptables rules are in place if ! iptables -t mangle -C OUTPUT -p tcp -d "$TARGET_IP" -j MARK --set-mark 1 2>/dev/null; then echo "Restoring outbound iptables mark rule" iptables -t mangle -A OUTPUT -p tcp -d "$TARGET_IP" -j MARK --set-mark 1 fi # Verify incoming connection mark rule if ! iptables -t mangle -C INPUT -p tcp -s "$TARGET_IP" -j CONNMARK --set-mark 1 2>/dev/null; then echo "Restoring incoming connection mark rule" iptables -t mangle -A INPUT -p tcp -s "$TARGET_IP" -j CONNMARK --set-mark 1 fi # Verify connection mark transfer rule if ! iptables -t mangle -C OUTPUT -p tcp -m connmark --mark 1 -j MARK --set-mark 1 2>/dev/null; then echo "Restoring connection mark transfer rule" iptables -t mangle -A OUTPUT -p tcp -m connmark --mark 1 -j MARK --set-mark 1 fi # Find active connections to target IP and disable Nagle connections=\$(ss -tnp | grep "$TARGET_IP" | grep -v LISTEN) if [ -n "\$connections" ]; then echo "Found active connections to $TARGET_IP" echo "\$connections" | while read -r conn; do pid=\$(echo "\$conn" | sed -n 's/.*pid=\([0-9]*\).*/\1/p') if [ -n "\$pid" ]; then echo "Connection found with PID: \$pid" # Use strace to call setsockopt on the process if command -v strace >/dev/null 2>&1; then # Install strace if not available if ! command -v strace >/dev/null 2>&1; then apt-get update && apt-get install -y strace fi # Find all TCP sockets associated with this PID for fd in /proc/\$pid/fd/*; do if [ -S "\$fd" ]; then echo "Setting TCP_NODELAY on socket \$fd" fi done fi fi done fi }
# Main loop while true; do # Enforce route-specific congestion control enforce_cc # Sleep briefly sleep 2 done EOF chmod +x /usr/local/bin/enforce_no_congestion.sh
cat > /etc/systemd/system/enforce-no-congestion.service << EOF [Unit] Description=Enforce no congestion control for target IP After=network.target
[Service] Type=simple ExecStart=/usr/local/bin/enforce_no_congestion.sh Restart=always
[Install] WantedBy=multi-user.target EOF
systemctl daemon-reload systemctl enable enforce-no-congestion systemctl start enforce-no-congestion
rm -f /etc/sysctl.d/99-no-congestion.conf
echo "Verifying setup:" echo "1. Checking if 'none' congestion control is available:" cat /proc/sys/net/ipv4/tcp_available_congestion_control echo "2. Checking current congestion control algorithm:" cat /proc/sys/net/ipv4/tcp_congestion_control echo "3. Verifying iptables rules:" echo " - Outbound packets to target IP:" iptables -t mangle -L OUTPUT -v | grep "$TARGET_IP" echo " - Incoming packets from target IP:" iptables -t mangle -L INPUT -v | grep "$TARGET_IP" echo " - Responses to incoming connections:" iptables -t mangle -L OUTPUT -v | grep "mark match 0x1"
echo "" echo "Configuration complete!" echo "TCP connections to AND from $TARGET_IP will now bypass congestion control and Nagle's algorithm." if [ -n "$DOMAIN" ]; then echo "Domain name $DOMAIN (resolved to $TARGET_IP) has been configured." fi echo "" echo "To run a specific application with Nagle's algorithm disabled, use:" echo "LD_PRELOAD=/usr/local/lib/disable_nagle.so your_application" echo "" echo "To verify connections are using 'none' congestion control:" echo "ss -ti | grep -A 5 $TARGET_IP" echo "" echo "NOTE: It may take a few seconds for existing connections to switch to 'none'." echo "New connections should use 'none' immediately."
echo "$TARGET_IP" > /var/lib/no-congestion-target if [ -n "$DOMAIN" ]; then echo "$DOMAIN" > /var/lib/no-congestion-domain fi
|