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