This patch adds tcpkill support to ngrep (new option -K).  It was
initially written by Florian Weimer <fw@deneb.enyo.de> in 2004, and
updated for libnet 1.1 by Romain Francoise <rfrancoise@debian.org>.

---
 LICENSE.txt |   29 +++++++++++++++++++
 Makefile.in |   10 ++++--
 ngrep.8     |    4 ++
 ngrep.c     |   15 +++++++++-
 tcpkill.c   |   88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tcpkill.h   |    7 ++++
 6 files changed, 148 insertions(+), 5 deletions(-)

--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -36,3 +36,32 @@
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+Portions were copied from tcpkill (part of dsniff), which has the
+following copyright:
+
+  Copyright (c) 1999, 2000 Dug Song <dugsong@monkey.org>
+  All rights reserved, all wrongs reversed.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+
+  1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+  3. The name of author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+  THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--- a/Makefile.in
+++ b/Makefile.in
@@ -7,16 +7,18 @@
 
 CC=@CC@
 
-CFLAGS=@CFLAGS@ -D@OS@ @DEFS@ @EXTRA_DEFINES@ 
+CFLAGS_LIBNET := $(shell libnet-config --defines ; libnet-config --cflags)
+CFLAGS=@CFLAGS@ -D@OS@ @DEFS@ @EXTRA_DEFINES@  $(CFLAGS_LIBNET)
 INCLUDES=-I@srcdir@ @PCAP_INCLUDE@ @EXTRA_INCLUDES@
 
 LDFLAGS=@LDFLAGS@ @PCAP_LINK@
-LIBS=-lpcap @EXTRA_LIBS@ 
+LIBNET := $(shell libnet-config --libs)
+LIBS=-lpcap @EXTRA_LIBS@ $(LIBNET)
 
 STRIPFLAG=@STRIPFLAG@
 
-SRC=ngrep.c 
-OBJS=ngrep.o 
+SRC=ngrep.c tcpkill.c
+OBJS=ngrep.o tcpkill.o
 TARGET=ngrep
 MANPAGE=ngrep.8
 
--- a/ngrep.8
+++ b/ngrep.8
@@ -189,6 +189,10 @@
 Change the non-printable character from the default ``.'' to the
 character specified.
 
+.IP "-K num"
+Kill matching TCP connections (like tcpkill).  The numeric argument
+controls how many RST segments are sent.
+
 .IP "\fI match expression\fP"
 A match expression is either an extended regular expression, or if the
 \fI-X\fP option is specified, a string signifying a hexadecimal value.
--- a/ngrep.c
+++ b/ngrep.c
@@ -110,6 +110,7 @@
 
 uint16_t snaplen = 65535, limitlen = 65535, promisc = 1, to = 100;
 uint16_t match_after = 0, keep_matching = 0, matches = 0, max_matches = 0;
+uint16_t tcpkill_active = 0;
 
 uint8_t  re_match_word = 0, re_ignore_case = 0, re_multiline_match = 1;
 uint8_t  show_empty = 0, show_hex = 0, show_proto = 0, quiet = 0;
@@ -199,7 +200,7 @@
 
     setlocale(LC_ALL, "");
 
-    while ((c = getopt(argc, argv, "LNhXViwqpevxlDtTRMs:n:c:d:A:I:O:S:P:F:W:")) != EOF) {
+    while ((c = getopt(argc, argv, "LNhXViwqpevxlDtTRMK:s:n:c:d:A:I:O:S:P:F:W:")) != EOF) {
         switch (c) {
             case 'W': {
                 if (!strcasecmp(optarg, "normal"))
@@ -314,6 +315,9 @@
             case 'N':
                 show_proto++;
                 break;
+            case 'K':
+                tcpkill_active = atoi(optarg);
+                break;
             case 'h':
                 usage(0);
             default:
@@ -353,6 +357,10 @@
             clean_exit(-1);
         }
 
+        if (tcpkill_active) {
+            tcpkill_init();
+        }
+
         if (pcap_lookupnet(dev, &net.s_addr, &mask.s_addr, pc_err) == -1) {
             perror(pc_err);
             memset(&net, 0, sizeof(net));
@@ -887,6 +895,10 @@
 
     if (pd_dump)
         pcap_dump((u_char*)pd_dump, h, p);
+
+    if (tcpkill_active) {
+        tcpkill_kill(h, p, link_offset, tcpkill_active);
+    }
 }
 
 int8_t re_match_func(unsigned char *data, uint32_t len) {
@@ -1256,6 +1268,7 @@
 #else
            "   -d  is use specified device instead of the pcap default\n"
 #endif
+           "   -K  is kill matching TCP connections\n"
            "");
 
     exit(e);
--- /dev/null
+++ b/tcpkill.c
@@ -0,0 +1,88 @@
+/*
+ * tcpkill.c
+ *
+ * Kill TCP connections already in progress.
+ *
+ * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
+ *
+ * $Id: tcpkill.c,v 1.17 2001/03/17 08:10:43 dugsong Exp $
+ */
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <libnet.h>
+#include <pcap.h>
+
+#include "tcpkill.h"
+
+libnet_t *l;
+
+void
+tcpkill_kill(const struct pcap_pkthdr *pcap, const u_char *pkt,
+	     unsigned pcap_off, unsigned kill_count)
+{
+	struct libnet_ipv4_hdr *ip;
+	struct libnet_tcp_hdr *tcp;
+	u_char ctext[64];
+	u_int32_t seq, win;
+	int i, len;
+
+	pkt += pcap_off;
+	len = pcap->caplen - pcap_off;
+
+	ip = (struct libnet_ipv4_hdr *)pkt;
+	if (ip->ip_p != IPPROTO_TCP)
+		return;
+
+	tcp = (struct libnet_tcp_hdr *)(pkt + (ip->ip_hl << 2));
+	if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST))
+		return;
+
+	seq = ntohl(tcp->th_ack);
+	win = ntohs(tcp->th_win);
+
+	snprintf(ctext, sizeof(ctext), "%s:%d > %s:%d:",
+		 libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
+		 ntohs(tcp->th_sport),
+		 libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
+		 ntohs(tcp->th_dport));
+
+	for (i = 0; i < kill_count; i++) {
+		seq += (i * win);
+
+		libnet_clear_packet(l);
+
+		libnet_build_tcp(ntohs(tcp->th_dport), ntohs(tcp->th_sport),
+				 seq, 0, TH_RST, 0, 0, 0, LIBNET_TCP_H,
+				 NULL, 0, l, 0);
+
+		libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H, 0,
+				  libnet_get_prand(LIBNET_PRu16), 0, 64,
+				  IPPROTO_TCP, 0, ip->ip_dst.s_addr,
+				  ip->ip_src.s_addr, NULL, 0, l, 0);
+
+		if (libnet_write(l) < 0)
+			warn("write");
+
+		fprintf(stderr, "%s R %u:%u(0) win 0\n", ctext, seq, seq);
+	}
+}
+
+void
+tcpkill_init(void)
+{
+	char *intf, ebuf[PCAP_ERRBUF_SIZE];
+	char libnet_ebuf[LIBNET_ERRBUF_SIZE];
+
+	if ((intf = pcap_lookupdev(ebuf)) == NULL)
+		errx(1, "%s", ebuf);
+
+	if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL)
+		errx(1, "couldn't initialize sending");
+
+	libnet_seed_prand(l);
+}
--- /dev/null
+++ b/tcpkill.h
@@ -0,0 +1,7 @@
+#ifndef TCPKILL_H
+#define TCPKILL_H
+
+void tcpkill_init(void);
+void tcpkill_kill(const struct pcap_pkthdr *pcap, const u_char *pkt, unsigned pcap_off, unsigned kill_count);
+
+#endif
