1/* $NetBSD: tcpdrop.c,v 1.2 2007/06/25 23:58:42 christos Exp $ */ 2 3/* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Herb Hasler and Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35/* $OpenBSD: tcpdrop.c,v 1.5 2006/01/03 01:46:27 stevesk Exp $ */ 36 37/* 38 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org> 39 * 40 * Permission to use, copy, modify, and distribute this software for any 41 * purpose with or without fee is hereby granted, provided that the above 42 * copyright notice and this permission notice appear in all copies. 43 * 44 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 45 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 46 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 47 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 48 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 49 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 50 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 51 */ 52 53#include <sys/param.h> 54#include <sys/socket.h> 55#include <sys/sysctl.h> 56 57#include <netinet/in.h> 58#include <netinet/tcp.h> 59#include <netinet/ip_var.h> 60#include <netinet/tcp_timer.h> 61#include <netinet/tcp_var.h> 62 63#include <assert.h> 64#include <err.h> 65#include <stdio.h> 66#include <string.h> 67#include <stdlib.h> 68#include <netdb.h> 69 70struct hpinfo { 71 char host[NI_MAXHOST]; 72 char serv[NI_MAXSERV]; 73}; 74 75static struct addrinfo * 76egetaddrinfo(const char *host, const char *serv) 77{ 78 static const struct addrinfo hints = { 79 .ai_family = AF_UNSPEC, 80 .ai_socktype = SOCK_STREAM, 81 }; 82 struct addrinfo *ai; 83 int gaierr; 84 85 if ((gaierr = getaddrinfo(host, serv, &hints, &ai)) != 0) 86 errx(1, "%s port %s: %s", host, serv, gai_strerror(gaierr)); 87 return ai; 88} 89 90static void 91egetnameinfo(const struct addrinfo *ai, struct hpinfo *hp) 92{ 93 int gaierr; 94 95 if ((gaierr = getnameinfo(ai->ai_addr, ai->ai_addrlen, 96 hp->host, sizeof(hp->host), hp->serv, sizeof(hp->serv), 97 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) 98 errx(1, "getnameinfo: %s", gai_strerror(gaierr)); 99} 100 101/* 102 * Drop a tcp connection. 103 */ 104int 105main(int argc, char **argv) 106{ 107 int mib[] = { CTL_NET, 0, IPPROTO_TCP, TCPCTL_DROP }; 108 struct addrinfo *ail, *aif, *laddr, *faddr; 109 struct sockaddr_storage sa[2]; 110 struct hpinfo fhp, lhp; 111 int rval = 0; 112 113 setprogname(argv[0]); 114 115 if (argc != 5) { 116 (void)fprintf(stderr, "Usage: %s laddr lport faddr fport\n", 117 getprogname()); 118 return 1; 119 } 120 121 laddr = egetaddrinfo(argv[1], argv[2]); 122 faddr = egetaddrinfo(argv[3], argv[4]); 123 124 for (ail = laddr; ail; ail = ail->ai_next) { 125 for (aif = faddr; aif; aif = aif->ai_next) { 126 127 if (ail->ai_family != aif->ai_family) 128 continue; 129 130 egetnameinfo(ail, &lhp); 131 egetnameinfo(aif, &fhp); 132 133 (void)memset(sa, 0, sizeof(sa)); 134 135 assert(aif->ai_addrlen <= sizeof(*sa)); 136 assert(ail->ai_addrlen <= sizeof(*sa)); 137 138 (void)memcpy(&sa[0], aif->ai_addr, aif->ai_addrlen); 139 (void)memcpy(&sa[1], ail->ai_addr, ail->ai_addrlen); 140 141 switch (ail->ai_family) { 142 case AF_INET: 143 mib[1] = PF_INET; 144 break; 145 case AF_INET6: 146 mib[1] = PF_INET6; 147 break; 148 default: 149 warnx("Unsupported socket address family %d", 150 ail->ai_family); 151 continue; 152 } 153 154 if (sysctl(mib, sizeof(mib) / sizeof(int), NULL, 155 NULL, sa, sizeof(sa)) == -1) { 156 rval = 1; 157 warn("%s:%s, %s:%s", 158 lhp.host, lhp.serv, fhp.host, fhp.serv); 159 } else 160 (void)printf("%s:%s %s:%s dropped\n", 161 lhp.host, lhp.serv, fhp.host, fhp.serv); 162 163 } 164 } 165 freeaddrinfo(laddr); 166 freeaddrinfo(faddr); 167 return rval; 168} 169