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