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