tcpdrop.c revision 173223
1141381Smaxim/* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */ 2141381Smaxim 3141381Smaxim/*- 4141381Smaxim * Copyright (c) 2004 Markus Friedl <markus@openbsd.org> 5141381Smaxim * 6141381Smaxim * Permission to use, copy, modify, and distribute this software for any 7141381Smaxim * purpose with or without fee is hereby granted, provided that the above 8141381Smaxim * copyright notice and this permission notice appear in all copies. 9141381Smaxim * 10141381Smaxim * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11141381Smaxim * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12141381Smaxim * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13141381Smaxim * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14141381Smaxim * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15141381Smaxim * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16141381Smaxim * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17141381Smaxim */ 18141381Smaxim 19141381Smaxim#include <sys/cdefs.h> 20141381Smaxim__FBSDID("$FreeBSD: head/usr.sbin/tcpdrop/tcpdrop.c 173223 2007-10-31 13:49:20Z ru $"); 21141381Smaxim 22141381Smaxim#include <sys/types.h> 23141381Smaxim#include <sys/socket.h> 24141381Smaxim#include <sys/sysctl.h> 25141381Smaxim#include <netinet/in.h> 26141381Smaxim#include <netinet/tcp_var.h> 27141381Smaxim 28141381Smaxim#include <err.h> 29141381Smaxim#include <netdb.h> 30141381Smaxim#include <stdio.h> 31141381Smaxim#include <stdlib.h> 32141381Smaxim#include <string.h> 33141381Smaxim 34141381Smaxim/* 35141381Smaxim * Drop a tcp connection. 36141381Smaxim */ 37141381Smaximint 38141381Smaximmain(int argc, char *argv[]) 39141381Smaxim{ 40141381Smaxim struct addrinfo hints, *ail, *aif, *laddr, *faddr; 41141886Smaxim /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 42141886Smaxim struct sockaddr_storage addrs[2]; 43141381Smaxim int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_DROP }; 44141381Smaxim int gaierr, rval = 0; 45141381Smaxim char fhbuf[NI_MAXHOST], fsbuf[NI_MAXSERV], lhbuf[NI_MAXHOST], 46141381Smaxim lsbuf[NI_MAXSERV]; 47141381Smaxim 48173223Sru if (argc != 5) { 49173223Sru fprintf(stderr, "usage: tcpdrop laddr lport faddr fport\n"); 50173223Sru exit(1); 51173223Sru } 52141381Smaxim memset(&hints, 0, sizeof(hints)); 53141381Smaxim hints.ai_family = AF_UNSPEC; 54141381Smaxim hints.ai_socktype = SOCK_STREAM; 55141381Smaxim if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &laddr)) != 0) 56141381Smaxim errx(1, "%s port %s: %s", argv[1], argv[2], 57141381Smaxim gai_strerror(gaierr)); 58141381Smaxim if ((gaierr = getaddrinfo(argv[3], argv[4], &hints, &faddr)) != 0) { 59141381Smaxim freeaddrinfo(laddr); 60141381Smaxim errx(1, "%s port %s: %s", argv[3], argv[4], 61141381Smaxim gai_strerror(gaierr)); 62141381Smaxim } 63141381Smaxim for (ail = laddr; ail; ail = ail->ai_next) { 64141381Smaxim for (aif = faddr; aif; aif = aif->ai_next) { 65141381Smaxim if (ail->ai_family != aif->ai_family) 66141381Smaxim continue; 67141886Smaxim memcpy(&addrs[0], aif->ai_addr, aif->ai_addrlen); 68141886Smaxim memcpy(&addrs[1], ail->ai_addr, ail->ai_addrlen); 69141381Smaxim if (getnameinfo(aif->ai_addr, aif->ai_addrlen, 70141381Smaxim fhbuf, sizeof(fhbuf), 71141381Smaxim fsbuf, sizeof(fsbuf), 72141381Smaxim NI_NUMERICHOST | NI_NUMERICSERV) == -1) 73141381Smaxim err(1, "getnameinfo"); 74141381Smaxim if (getnameinfo(ail->ai_addr, ail->ai_addrlen, 75141381Smaxim lhbuf, sizeof(lhbuf), 76141381Smaxim lsbuf, sizeof(lsbuf), 77141381Smaxim NI_NUMERICHOST | NI_NUMERICSERV) == -1) 78141381Smaxim err(1, "getnameinfo"); 79141381Smaxim if (sysctl(mib, sizeof (mib) / sizeof (int), NULL, 80141886Smaxim NULL, &addrs, sizeof(addrs)) == -1) { 81141381Smaxim rval = 1; 82141381Smaxim warn("%s %s %s %s", lhbuf, lsbuf, fhbuf, fsbuf); 83141381Smaxim } else 84141381Smaxim printf("%s %s %s %s: dropped\n", 85141381Smaxim lhbuf, lsbuf, fhbuf, fsbuf); 86141381Smaxim } 87141381Smaxim } 88141381Smaxim freeaddrinfo(laddr); 89141381Smaxim freeaddrinfo(faddr); 90141381Smaxim exit(rval); 91141381Smaxim} 92