1 /* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */
4 * Copyright (c) 2009 Juli Mallett <jmallett@FreeBSD.org>
5 * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/socketvar.h>
27 #include <sys/sysctl.h>
29 #include <netinet/in.h>
30 #include <netinet/in_pcb.h>
32 #include <netinet/tcp_fsm.h>
33 #include <netinet/tcp_var.h>
43 #define TCPDROP_FOREIGN 0
44 #define TCPDROP_LOCAL 1
47 char hs_host[NI_MAXHOST];
48 char hs_service[NI_MAXSERV];
51 static bool tcpdrop_list_commands = false;
53 static struct xinpgen *getxpcblist(const char *);
54 static void sockinfo(const struct sockaddr *, struct host_service *);
55 static bool tcpdrop(const struct sockaddr *, const struct sockaddr *);
56 static bool tcpdropall(void);
57 static bool tcpdropbyname(const char *, const char *, const char *,
59 static bool tcpdropconn(const struct in_conninfo *);
60 static void usage(void);
63 * Drop a tcp connection.
66 main(int argc, char *argv[])
73 while ((ch = getopt(argc, argv, "al")) != -1) {
79 tcpdrop_list_commands = true;
96 if (argc != 4 || tcpdrop_list_commands)
99 if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
105 static struct xinpgen *
106 getxpcblist(const char *name)
108 struct xinpgen *xinp;
113 rv = sysctlbyname(name, NULL, &len, NULL, 0);
115 err(1, "sysctlbyname %s", name);
118 errx(1, "%s is empty", name);
122 errx(1, "malloc failed");
124 rv = sysctlbyname(name, xinp, &len, NULL, 0);
126 err(1, "sysctlbyname %s", name);
132 sockinfo(const struct sockaddr *sa, struct host_service *hs)
134 static const int flags = NI_NUMERICHOST | NI_NUMERICSERV;
137 rv = getnameinfo(sa, sa->sa_len, hs->hs_host, sizeof hs->hs_host,
138 hs->hs_service, sizeof hs->hs_service, flags);
140 err(1, "getnameinfo");
144 tcpdrop(const struct sockaddr *lsa, const struct sockaddr *fsa)
146 struct host_service local, foreign;
147 struct sockaddr_storage addrs[2];
150 memcpy(&addrs[TCPDROP_FOREIGN], fsa, fsa->sa_len);
151 memcpy(&addrs[TCPDROP_LOCAL], lsa, lsa->sa_len);
153 sockinfo(lsa, &local);
154 sockinfo(fsa, &foreign);
156 if (tcpdrop_list_commands) {
157 printf("tcpdrop %s %s %s %s\n", local.hs_host, local.hs_service,
158 foreign.hs_host, foreign.hs_service);
162 rv = sysctlbyname("net.inet.tcp.drop", NULL, NULL, &addrs,
165 warn("%s %s %s %s", local.hs_host, local.hs_service,
166 foreign.hs_host, foreign.hs_service);
169 printf("%s %s %s %s: dropped\n", local.hs_host, local.hs_service,
170 foreign.hs_host, foreign.hs_service);
177 struct xinpgen *head, *xinp;
185 head = getxpcblist("net.inet.tcp.pcblist");
187 #define XINP_NEXT(xinp) \
188 ((struct xinpgen *)((uintptr_t)(xinp) + (xinp)->xig_len))
190 for (xinp = XINP_NEXT(head); xinp->xig_len > sizeof *xinp;
191 xinp = XINP_NEXT(xinp)) {
192 xpcb = (struct xtcpcb *)xinp;
198 * Check protocol, support just v4 or v6, etc.
201 /* Ignore PCBs which were freed during copyout. */
202 if (inp->inp_gencnt > head->xig_gen)
205 /* Skip listening sockets. */
206 if (tp->t_state == TCPS_LISTEN)
209 if (!tcpdropconn(&inp->inp_inc))
218 tcpdropbyname(const char *lhost, const char *lport, const char *fhost,
221 static const struct addrinfo hints = {
223 * Look for streams in all domains.
225 .ai_family = AF_UNSPEC,
226 .ai_socktype = SOCK_STREAM,
228 struct addrinfo *ail, *local, *aif, *foreign;
232 error = getaddrinfo(lhost, lport, &hints, &local);
234 errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
235 gai_strerror(error));
237 error = getaddrinfo(fhost, fport, &hints, &foreign);
239 freeaddrinfo(local); /* XXX gratuitous */
240 errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
241 gai_strerror(error));
248 * Try every combination of local and foreign address pairs.
250 for (ail = local; ail != NULL; ail = ail->ai_next) {
251 for (aif = foreign; aif != NULL; aif = aif->ai_next) {
252 if (ail->ai_family != aif->ai_family)
255 if (!tcpdrop(ail->ai_addr, aif->ai_addr))
261 warnx("%s %s %s %s: different address families", lhost, lport,
267 freeaddrinfo(foreign);
273 tcpdropconn(const struct in_conninfo *inc)
275 struct sockaddr *local, *foreign;
276 struct sockaddr_in6 sin6[2];
277 struct sockaddr_in sin4[2];
279 if ((inc->inc_flags & INC_ISIPV6) != 0) {
280 memset(sin6, 0, sizeof sin6);
282 sin6[TCPDROP_LOCAL].sin6_len = sizeof sin6[TCPDROP_LOCAL];
283 sin6[TCPDROP_LOCAL].sin6_family = AF_INET6;
284 sin6[TCPDROP_LOCAL].sin6_port = inc->inc_lport;
285 memcpy(&sin6[TCPDROP_LOCAL].sin6_addr, &inc->inc6_laddr,
286 sizeof inc->inc6_laddr);
287 local = (struct sockaddr *)&sin6[TCPDROP_LOCAL];
289 sin6[TCPDROP_FOREIGN].sin6_len = sizeof sin6[TCPDROP_FOREIGN];
290 sin6[TCPDROP_FOREIGN].sin6_family = AF_INET6;
291 sin6[TCPDROP_FOREIGN].sin6_port = inc->inc_fport;
292 memcpy(&sin6[TCPDROP_FOREIGN].sin6_addr, &inc->inc6_faddr,
293 sizeof inc->inc6_faddr);
294 foreign = (struct sockaddr *)&sin6[TCPDROP_FOREIGN];
296 memset(&sin4[TCPDROP_LOCAL], 0, sizeof sin4[TCPDROP_LOCAL]);
298 sin4[TCPDROP_LOCAL].sin_len = sizeof sin4[TCPDROP_LOCAL];
299 sin4[TCPDROP_LOCAL].sin_family = AF_INET;
300 sin4[TCPDROP_LOCAL].sin_port = inc->inc_lport;
301 memcpy(&sin4[TCPDROP_LOCAL].sin_addr, &inc->inc_laddr,
302 sizeof inc->inc_laddr);
303 local = (struct sockaddr *)&sin4[TCPDROP_LOCAL];
305 sin4[TCPDROP_FOREIGN].sin_len = sizeof sin4[TCPDROP_FOREIGN];
306 sin4[TCPDROP_FOREIGN].sin_family = AF_INET;
307 sin4[TCPDROP_FOREIGN].sin_port = inc->inc_fport;
308 memcpy(&sin4[TCPDROP_FOREIGN].sin_addr, &inc->inc_faddr,
309 sizeof inc->inc_faddr);
310 foreign = (struct sockaddr *)&sin4[TCPDROP_FOREIGN];
313 return (tcpdrop(local, foreign));
320 "usage: tcpdrop local-address local-port foreign-address foreign-port\n"
321 " tcpdrop [-l] -a\n");