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 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/socketvar.h>
25 #include <sys/sysctl.h>
27 #include <netinet/in.h>
28 #include <netinet/in_pcb.h>
30 #include <netinet/tcp_fsm.h>
31 #include <netinet/tcp_var.h>
41 #define TCPDROP_FOREIGN 0
42 #define TCPDROP_LOCAL 1
45 char hs_host[NI_MAXHOST];
46 char hs_service[NI_MAXSERV];
49 static bool tcpdrop_list_commands = false;
51 static char *findport(const char *);
52 static struct xinpgen *getxpcblist(const char *);
53 static void sockinfo(const struct sockaddr *, struct host_service *);
54 static bool tcpdrop(const struct sockaddr *, const struct sockaddr *);
55 static bool tcpdropall(const char *, const char *, int);
56 static bool tcpdropbyname(const char *, const char *, const char *,
58 static bool tcpdropconn(const struct in_conninfo *);
59 static void usage(void) __dead2;
62 * Drop a tcp connection.
65 main(int argc, char *argv[])
67 char stack[TCP_FUNCTION_NAME_LEN_MAX];
68 char ca_name[TCP_CA_NAME_MAX];
70 bool dropall, dropspecific;
79 while ((ch = getopt(argc, argv, "aC:lS:s:")) != -1) {
86 strlcpy(ca_name, optarg, sizeof(ca_name));
89 tcpdrop_list_commands = true;
93 strlcpy(stack, optarg, sizeof(stack));
97 for (state = 0; state < TCP_NSTATES; state++) {
98 if (strcmp(tcpstates[state], optarg) == 0)
109 if (state == TCP_NSTATES ||
110 state == TCPS_CLOSED ||
111 state == TCPS_LISTEN)
113 if (dropall && dropspecific)
115 if (dropall || dropspecific) {
118 if (!tcpdropall(ca_name, stack, state))
123 if ((argc != 2 && argc != 4) || tcpdrop_list_commands)
127 lport = findport(argv[0]);
128 fport = findport(argv[1]);
129 if (lport == NULL || lport[1] == '\0' || fport == NULL ||
134 if (!tcpdropbyname(argv[0], lport, argv[1], fport))
136 } else if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
143 findport(const char *arg)
147 /* A strrspn() or strrpbrk() would be nice. */
148 dot = strrchr(arg, '.');
149 colon = strrchr(arg, ':');
160 static struct xinpgen *
161 getxpcblist(const char *name)
163 struct xinpgen *xinp;
168 rv = sysctlbyname(name, NULL, &len, NULL, 0);
170 err(1, "sysctlbyname %s", name);
173 errx(1, "%s is empty", name);
177 errx(1, "malloc failed");
179 rv = sysctlbyname(name, xinp, &len, NULL, 0);
181 err(1, "sysctlbyname %s", name);
187 sockinfo(const struct sockaddr *sa, struct host_service *hs)
189 static const int flags = NI_NUMERICHOST | NI_NUMERICSERV;
192 rv = getnameinfo(sa, sa->sa_len, hs->hs_host, sizeof hs->hs_host,
193 hs->hs_service, sizeof hs->hs_service, flags);
195 err(1, "getnameinfo");
199 tcpdrop(const struct sockaddr *lsa, const struct sockaddr *fsa)
201 struct host_service local, foreign;
202 struct sockaddr_storage addrs[2];
205 memcpy(&addrs[TCPDROP_FOREIGN], fsa, fsa->sa_len);
206 memcpy(&addrs[TCPDROP_LOCAL], lsa, lsa->sa_len);
208 sockinfo(lsa, &local);
209 sockinfo(fsa, &foreign);
211 if (tcpdrop_list_commands) {
212 printf("tcpdrop %s %s %s %s\n", local.hs_host, local.hs_service,
213 foreign.hs_host, foreign.hs_service);
217 rv = sysctlbyname("net.inet.tcp.drop", NULL, NULL, &addrs,
220 warn("%s %s %s %s", local.hs_host, local.hs_service,
221 foreign.hs_host, foreign.hs_service);
224 printf("%s %s %s %s: dropped\n", local.hs_host, local.hs_service,
225 foreign.hs_host, foreign.hs_service);
230 tcpdropall(const char *ca_name, const char *stack, int state)
232 struct xinpgen *head, *xinp;
239 head = getxpcblist("net.inet.tcp.pcblist");
241 #define XINP_NEXT(xinp) \
242 ((struct xinpgen *)(uintptr_t)((uintptr_t)(xinp) + (xinp)->xig_len))
244 for (xinp = XINP_NEXT(head); xinp->xig_len > sizeof *xinp;
245 xinp = XINP_NEXT(xinp)) {
246 xtp = (struct xtcpcb *)xinp;
251 * Check protocol, support just v4 or v6, etc.
254 /* Ignore PCBs which were freed during copyout. */
255 if (xip->inp_gencnt > head->xig_gen)
258 /* Skip listening sockets. */
259 if (xtp->t_state == TCPS_LISTEN)
262 /* If requested, skip sockets not having the requested state. */
263 if ((state != -1) && (xtp->t_state != state))
267 * If requested, skip sockets not having the requested
268 * congestion control algorithm.
270 if (ca_name[0] != '\0' &&
271 strncmp(xtp->xt_cc, ca_name, TCP_CA_NAME_MAX))
274 /* If requested, skip sockets not having the requested stack. */
275 if (stack[0] != '\0' &&
276 strncmp(xtp->xt_stack, stack, TCP_FUNCTION_NAME_LEN_MAX))
279 if (!tcpdropconn(&xip->inp_inc))
288 tcpdropbyname(const char *lhost, const char *lport, const char *fhost,
291 static const struct addrinfo hints = {
293 * Look for TCP streams in all domains.
295 .ai_family = AF_UNSPEC,
296 .ai_socktype = SOCK_STREAM,
297 .ai_protocol = IPPROTO_TCP,
299 struct addrinfo *ail, *local, *aif, *foreign;
303 error = getaddrinfo(lhost, lport, &hints, &local);
305 errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
306 gai_strerror(error));
308 error = getaddrinfo(fhost, fport, &hints, &foreign);
310 freeaddrinfo(local); /* XXX gratuitous */
311 errx(1, "getaddrinfo: %s port %s: %s", fhost, fport,
312 gai_strerror(error));
319 * Try every combination of local and foreign address pairs.
321 for (ail = local; ail != NULL; ail = ail->ai_next) {
322 for (aif = foreign; aif != NULL; aif = aif->ai_next) {
323 if (ail->ai_family != aif->ai_family)
326 if (!tcpdrop(ail->ai_addr, aif->ai_addr))
332 warnx("%s %s %s %s: different address families", lhost, lport,
338 freeaddrinfo(foreign);
344 tcpdropconn(const struct in_conninfo *inc)
346 struct sockaddr *local, *foreign;
347 struct sockaddr_in6 sin6[2];
348 struct sockaddr_in sin4[2];
350 if ((inc->inc_flags & INC_ISIPV6) != 0) {
351 memset(sin6, 0, sizeof sin6);
353 sin6[TCPDROP_LOCAL].sin6_len = sizeof sin6[TCPDROP_LOCAL];
354 sin6[TCPDROP_LOCAL].sin6_family = AF_INET6;
355 sin6[TCPDROP_LOCAL].sin6_port = inc->inc_lport;
356 memcpy(&sin6[TCPDROP_LOCAL].sin6_addr, &inc->inc6_laddr,
357 sizeof inc->inc6_laddr);
358 local = (struct sockaddr *)&sin6[TCPDROP_LOCAL];
360 sin6[TCPDROP_FOREIGN].sin6_len = sizeof sin6[TCPDROP_FOREIGN];
361 sin6[TCPDROP_FOREIGN].sin6_family = AF_INET6;
362 sin6[TCPDROP_FOREIGN].sin6_port = inc->inc_fport;
363 memcpy(&sin6[TCPDROP_FOREIGN].sin6_addr, &inc->inc6_faddr,
364 sizeof inc->inc6_faddr);
365 foreign = (struct sockaddr *)&sin6[TCPDROP_FOREIGN];
367 memset(sin4, 0, sizeof sin4);
369 sin4[TCPDROP_LOCAL].sin_len = sizeof sin4[TCPDROP_LOCAL];
370 sin4[TCPDROP_LOCAL].sin_family = AF_INET;
371 sin4[TCPDROP_LOCAL].sin_port = inc->inc_lport;
372 memcpy(&sin4[TCPDROP_LOCAL].sin_addr, &inc->inc_laddr,
373 sizeof inc->inc_laddr);
374 local = (struct sockaddr *)&sin4[TCPDROP_LOCAL];
376 sin4[TCPDROP_FOREIGN].sin_len = sizeof sin4[TCPDROP_FOREIGN];
377 sin4[TCPDROP_FOREIGN].sin_family = AF_INET;
378 sin4[TCPDROP_FOREIGN].sin_port = inc->inc_fport;
379 memcpy(&sin4[TCPDROP_FOREIGN].sin_addr, &inc->inc_faddr,
380 sizeof inc->inc_faddr);
381 foreign = (struct sockaddr *)&sin4[TCPDROP_FOREIGN];
384 return (tcpdrop(local, foreign));
391 "usage: tcpdrop local-address local-port foreign-address foreign-port\n"
392 " tcpdrop local-address:local-port foreign-address:foreign-port\n"
393 " tcpdrop local-address.local-port foreign-address.foreign-port\n"
395 " tcpdrop [-l] -C cc-algo [-S stack] [-s state]\n"
396 " tcpdrop [-l] [-C cc-algo] -S stack [-s state]\n"
397 " tcpdrop [-l] [-C cc-algo] [-S stack] -s state\n");