xref: /freebsd/usr.sbin/tcpdrop/tcpdrop.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */
2 
3 /*-
4  * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21 
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/sysctl.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp_var.h>
27 
28 #include <err.h>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 /*
35  * Drop a tcp connection.
36  */
37 int
38 main(int argc, char *argv[])
39 {
40 	struct addrinfo hints, *ail, *aif, *laddr, *faddr;
41 	/* addrs[0] is a foreign socket, addrs[1] is a local one. */
42 	struct sockaddr_storage addrs[2];
43 	int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_DROP };
44 	int gaierr, rval = 0;
45 	char fhbuf[NI_MAXHOST], fsbuf[NI_MAXSERV], lhbuf[NI_MAXHOST],
46 	    lsbuf[NI_MAXSERV];
47 
48 	if (argc != 5)
49 		errx(1, "usage: tcpdrop laddr lport faddr fport\n");
50 	memset(&hints, 0, sizeof(hints));
51 	hints.ai_family = AF_UNSPEC;
52 	hints.ai_socktype = SOCK_STREAM;
53 	if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &laddr)) != 0)
54 		errx(1, "%s port %s: %s", argv[1], argv[2],
55 		    gai_strerror(gaierr));
56 	if ((gaierr = getaddrinfo(argv[3], argv[4], &hints, &faddr)) != 0) {
57 		freeaddrinfo(laddr);
58 		errx(1, "%s port %s: %s", argv[3], argv[4],
59 		    gai_strerror(gaierr));
60 	}
61 	for (ail = laddr; ail; ail = ail->ai_next) {
62 		for (aif = faddr; aif; aif = aif->ai_next) {
63 			if (ail->ai_family != aif->ai_family)
64 				continue;
65 			memcpy(&addrs[0], aif->ai_addr, aif->ai_addrlen);
66 			memcpy(&addrs[1], ail->ai_addr, ail->ai_addrlen);
67 			if (getnameinfo(aif->ai_addr, aif->ai_addrlen,
68 			    fhbuf, sizeof(fhbuf),
69 			    fsbuf, sizeof(fsbuf),
70 			    NI_NUMERICHOST | NI_NUMERICSERV) == -1)
71 				err(1, "getnameinfo");
72 			if (getnameinfo(ail->ai_addr, ail->ai_addrlen,
73 			    lhbuf, sizeof(lhbuf),
74 			    lsbuf, sizeof(lsbuf),
75 			    NI_NUMERICHOST | NI_NUMERICSERV) == -1)
76 				err(1, "getnameinfo");
77 			if (sysctl(mib, sizeof (mib) / sizeof (int), NULL,
78 			    NULL, &addrs, sizeof(addrs)) == -1) {
79 				rval = 1;
80 				warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf);
81 			} else
82 				printf("%s %s %s %s: dropped\n",
83 				    lhbuf, lsbuf, fhbuf, fsbuf);
84 		}
85 	}
86 	freeaddrinfo(laddr);
87 	freeaddrinfo(faddr);
88 	exit(rval);
89 }
90