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