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 char *findport(const char *);
54 static struct xinpgen *getxpcblist(const char *);
55 static void sockinfo(const struct sockaddr *, struct host_service *);
56 static bool tcpdrop(const struct sockaddr *, const struct sockaddr *);
57 static bool tcpdropall(const char *, int);
58 static bool tcpdropbyname(const char *, const char *, const char *,
60 static bool tcpdropconn(const struct in_conninfo *);
61 static void usage(void);
64 * Drop a tcp connection.
67 main(int argc, char *argv[])
69 char stack[TCP_FUNCTION_NAME_LEN_MAX];
71 bool dropall, dropallstack;
79 while ((ch = getopt(argc, argv, "alS:s:")) != -1) {
85 tcpdrop_list_commands = true;
89 strlcpy(stack, optarg, sizeof(stack));
93 for (state = 0; state < TCP_NSTATES; state++) {
94 if (strcmp(tcpstates[state], optarg) == 0)
105 if (state == TCP_NSTATES ||
106 state == TCPS_CLOSED ||
107 state == TCPS_LISTEN)
109 if (dropall && dropallstack)
111 if (dropall || dropallstack) {
114 if (!tcpdropall(stack, state))
119 if ((argc != 2 && argc != 4) || tcpdrop_list_commands)
123 lport = findport(argv[0]);
124 fport = findport(argv[1]);
125 if (lport == NULL || lport[1] == '\0' || fport == NULL ||
130 if (!tcpdropbyname(argv[0], lport, argv[1], fport))
132 } else if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
139 findport(const char *arg)
143 /* A strrspn() or strrpbrk() would be nice. */
144 dot = strrchr(arg, '.');
145 colon = strrchr(arg, ':');
156 static struct xinpgen *
157 getxpcblist(const char *name)
159 struct xinpgen *xinp;
164 rv = sysctlbyname(name, NULL, &len, NULL, 0);
166 err(1, "sysctlbyname %s", name);
169 errx(1, "%s is empty", name);
173 errx(1, "malloc failed");
175 rv = sysctlbyname(name, xinp, &len, NULL, 0);
177 err(1, "sysctlbyname %s", name);
183 sockinfo(const struct sockaddr *sa, struct host_service *hs)
185 static const int flags = NI_NUMERICHOST | NI_NUMERICSERV;
188 rv = getnameinfo(sa, sa->sa_len, hs->hs_host, sizeof hs->hs_host,
189 hs->hs_service, sizeof hs->hs_service, flags);
191 err(1, "getnameinfo");
195 tcpdrop(const struct sockaddr *lsa, const struct sockaddr *fsa)
197 struct host_service local, foreign;
198 struct sockaddr_storage addrs[2];
201 memcpy(&addrs[TCPDROP_FOREIGN], fsa, fsa->sa_len);
202 memcpy(&addrs[TCPDROP_LOCAL], lsa, lsa->sa_len);
204 sockinfo(lsa, &local);
205 sockinfo(fsa, &foreign);
207 if (tcpdrop_list_commands) {
208 printf("tcpdrop %s %s %s %s\n", local.hs_host, local.hs_service,
209 foreign.hs_host, foreign.hs_service);
213 rv = sysctlbyname("net.inet.tcp.drop", NULL, NULL, &addrs,
216 warn("%s %s %s %s", local.hs_host, local.hs_service,
217 foreign.hs_host, foreign.hs_service);
220 printf("%s %s %s %s: dropped\n", local.hs_host, local.hs_service,
221 foreign.hs_host, foreign.hs_service);
226 tcpdropall(const char *stack, int state)
228 struct xinpgen *head, *xinp;
235 head = getxpcblist("net.inet.tcp.pcblist");
237 #define XINP_NEXT(xinp) \
238 ((struct xinpgen *)(uintptr_t)((uintptr_t)(xinp) + (xinp)->xig_len))
240 for (xinp = XINP_NEXT(head); xinp->xig_len > sizeof *xinp;
241 xinp = XINP_NEXT(xinp)) {
242 xtp = (struct xtcpcb *)xinp;
247 * Check protocol, support just v4 or v6, etc.
250 /* Ignore PCBs which were freed during copyout. */
251 if (xip->inp_gencnt > head->xig_gen)
254 /* Skip listening sockets. */
255 if (xtp->t_state == TCPS_LISTEN)
258 /* If requested, skip sockets not having the requested state. */
259 if ((state != -1) && (xtp->t_state != state))
262 /* If requested, skip sockets not having the requested stack. */
263 if (stack[0] != '\0' &&
264 strncmp(xtp->xt_stack, stack, TCP_FUNCTION_NAME_LEN_MAX))
267 if (!tcpdropconn(&xip->inp_inc))
276 tcpdropbyname(const char *lhost, const char *lport, const char *fhost,
279 static const struct addrinfo hints = {
281 * Look for streams in all domains.
283 .ai_family = AF_UNSPEC,
284 .ai_socktype = SOCK_STREAM,
286 struct addrinfo *ail, *local, *aif, *foreign;
290 error = getaddrinfo(lhost, lport, &hints, &local);
292 errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
293 gai_strerror(error));
295 error = getaddrinfo(fhost, fport, &hints, &foreign);
297 freeaddrinfo(local); /* XXX gratuitous */
298 errx(1, "getaddrinfo: %s port %s: %s", fhost, fport,
299 gai_strerror(error));
306 * Try every combination of local and foreign address pairs.
308 for (ail = local; ail != NULL; ail = ail->ai_next) {
309 for (aif = foreign; aif != NULL; aif = aif->ai_next) {
310 if (ail->ai_family != aif->ai_family)
313 if (!tcpdrop(ail->ai_addr, aif->ai_addr))
319 warnx("%s %s %s %s: different address families", lhost, lport,
325 freeaddrinfo(foreign);
331 tcpdropconn(const struct in_conninfo *inc)
333 struct sockaddr *local, *foreign;
334 struct sockaddr_in6 sin6[2];
335 struct sockaddr_in sin4[2];
337 if ((inc->inc_flags & INC_ISIPV6) != 0) {
338 memset(sin6, 0, sizeof sin6);
340 sin6[TCPDROP_LOCAL].sin6_len = sizeof sin6[TCPDROP_LOCAL];
341 sin6[TCPDROP_LOCAL].sin6_family = AF_INET6;
342 sin6[TCPDROP_LOCAL].sin6_port = inc->inc_lport;
343 memcpy(&sin6[TCPDROP_LOCAL].sin6_addr, &inc->inc6_laddr,
344 sizeof inc->inc6_laddr);
345 local = (struct sockaddr *)&sin6[TCPDROP_LOCAL];
347 sin6[TCPDROP_FOREIGN].sin6_len = sizeof sin6[TCPDROP_FOREIGN];
348 sin6[TCPDROP_FOREIGN].sin6_family = AF_INET6;
349 sin6[TCPDROP_FOREIGN].sin6_port = inc->inc_fport;
350 memcpy(&sin6[TCPDROP_FOREIGN].sin6_addr, &inc->inc6_faddr,
351 sizeof inc->inc6_faddr);
352 foreign = (struct sockaddr *)&sin6[TCPDROP_FOREIGN];
354 memset(sin4, 0, sizeof sin4);
356 sin4[TCPDROP_LOCAL].sin_len = sizeof sin4[TCPDROP_LOCAL];
357 sin4[TCPDROP_LOCAL].sin_family = AF_INET;
358 sin4[TCPDROP_LOCAL].sin_port = inc->inc_lport;
359 memcpy(&sin4[TCPDROP_LOCAL].sin_addr, &inc->inc_laddr,
360 sizeof inc->inc_laddr);
361 local = (struct sockaddr *)&sin4[TCPDROP_LOCAL];
363 sin4[TCPDROP_FOREIGN].sin_len = sizeof sin4[TCPDROP_FOREIGN];
364 sin4[TCPDROP_FOREIGN].sin_family = AF_INET;
365 sin4[TCPDROP_FOREIGN].sin_port = inc->inc_fport;
366 memcpy(&sin4[TCPDROP_FOREIGN].sin_addr, &inc->inc_faddr,
367 sizeof inc->inc_faddr);
368 foreign = (struct sockaddr *)&sin4[TCPDROP_FOREIGN];
371 return (tcpdrop(local, foreign));
378 "usage: tcpdrop local-address local-port foreign-address foreign-port\n"
379 " tcpdrop local-address:local-port foreign-address:foreign-port\n"
380 " tcpdrop local-address.local-port foreign-address.foreign-port\n"
382 " tcpdrop [-l] -S stack\n"
383 " tcpdrop [-l] -s state\n"
384 " tcpdrop [-l] -S stack -s state\n");