xref: /dflybsd-src/usr.sbin/tcpdrop/tcpdrop.c (revision da0f7859d0ec9815fa217d4beeb6727df195e13e)
1*da0f7859SSepherosa Ziehau /* $OpenBSD: tcpdrop.c,v 1.4 2004/05/22 23:55:22 deraadt Exp $ */
2*da0f7859SSepherosa Ziehau 
3*da0f7859SSepherosa Ziehau /*-
4*da0f7859SSepherosa Ziehau  * Copyright (c) 2009 Juli Mallett <jmallett@FreeBSD.org>
5*da0f7859SSepherosa Ziehau  * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
6*da0f7859SSepherosa Ziehau  *
7*da0f7859SSepherosa Ziehau  * Permission to use, copy, modify, and distribute this software for any
8*da0f7859SSepherosa Ziehau  * purpose with or without fee is hereby granted, provided that the above
9*da0f7859SSepherosa Ziehau  * copyright notice and this permission notice appear in all copies.
10*da0f7859SSepherosa Ziehau  *
11*da0f7859SSepherosa Ziehau  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*da0f7859SSepherosa Ziehau  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*da0f7859SSepherosa Ziehau  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*da0f7859SSepherosa Ziehau  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*da0f7859SSepherosa Ziehau  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*da0f7859SSepherosa Ziehau  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*da0f7859SSepherosa Ziehau  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*da0f7859SSepherosa Ziehau  */
19*da0f7859SSepherosa Ziehau 
20*da0f7859SSepherosa Ziehau #include <sys/cdefs.h>
21*da0f7859SSepherosa Ziehau __FBSDID("$FreeBSD$");
22*da0f7859SSepherosa Ziehau 
23*da0f7859SSepherosa Ziehau #include <sys/param.h>
24*da0f7859SSepherosa Ziehau #include <sys/types.h>
25*da0f7859SSepherosa Ziehau #include <sys/socket.h>
26*da0f7859SSepherosa Ziehau #include <sys/socketvar.h>
27*da0f7859SSepherosa Ziehau #include <sys/sysctl.h>
28*da0f7859SSepherosa Ziehau 
29*da0f7859SSepherosa Ziehau #include <netinet/in.h>
30*da0f7859SSepherosa Ziehau #include <netinet/in_pcb.h>
31*da0f7859SSepherosa Ziehau #define TCPSTATES
32*da0f7859SSepherosa Ziehau #include <netinet/tcp_fsm.h>
33*da0f7859SSepherosa Ziehau #include <netinet/tcp_var.h>
34*da0f7859SSepherosa Ziehau 
35*da0f7859SSepherosa Ziehau #include <err.h>
36*da0f7859SSepherosa Ziehau #include <netdb.h>
37*da0f7859SSepherosa Ziehau #include <stdbool.h>
38*da0f7859SSepherosa Ziehau #include <stdio.h>
39*da0f7859SSepherosa Ziehau #include <stdlib.h>
40*da0f7859SSepherosa Ziehau #include <string.h>
41*da0f7859SSepherosa Ziehau #include <unistd.h>
42*da0f7859SSepherosa Ziehau 
43*da0f7859SSepherosa Ziehau #define	TCPDROP_FOREIGN		0
44*da0f7859SSepherosa Ziehau #define	TCPDROP_LOCAL		1
45*da0f7859SSepherosa Ziehau 
46*da0f7859SSepherosa Ziehau struct host_service {
47*da0f7859SSepherosa Ziehau 	char hs_host[NI_MAXHOST];
48*da0f7859SSepherosa Ziehau 	char hs_service[NI_MAXSERV];
49*da0f7859SSepherosa Ziehau };
50*da0f7859SSepherosa Ziehau 
51*da0f7859SSepherosa Ziehau static bool tcpdrop_list_commands = false;
52*da0f7859SSepherosa Ziehau 
53*da0f7859SSepherosa Ziehau static char *findport(const char *);
54*da0f7859SSepherosa Ziehau static struct xtcpcb *tcpcblist(const char *, size_t *);
55*da0f7859SSepherosa Ziehau static void sockinfo(const struct sockaddr *, struct host_service *);
56*da0f7859SSepherosa Ziehau static bool tcpdrop(const struct sockaddr *, const struct sockaddr *);
57*da0f7859SSepherosa Ziehau static bool tcpdropall(void);
58*da0f7859SSepherosa Ziehau static bool tcpdropbyname(const char *, const char *, const char *,
59*da0f7859SSepherosa Ziehau     const char *);
60*da0f7859SSepherosa Ziehau static bool tcpdropconn(const struct in_conninfo *);
61*da0f7859SSepherosa Ziehau static void usage(void);
62*da0f7859SSepherosa Ziehau 
63*da0f7859SSepherosa Ziehau /*
64*da0f7859SSepherosa Ziehau  * Drop a tcp connection.
65*da0f7859SSepherosa Ziehau  */
66*da0f7859SSepherosa Ziehau int
main(int argc,char * argv[])67*da0f7859SSepherosa Ziehau main(int argc, char *argv[])
68*da0f7859SSepherosa Ziehau {
69*da0f7859SSepherosa Ziehau 	char *lport, *fport;
70*da0f7859SSepherosa Ziehau 	bool dropall;
71*da0f7859SSepherosa Ziehau 	int ch;
72*da0f7859SSepherosa Ziehau 
73*da0f7859SSepherosa Ziehau 	dropall = false;
74*da0f7859SSepherosa Ziehau 
75*da0f7859SSepherosa Ziehau 	while ((ch = getopt(argc, argv, "al")) != -1) {
76*da0f7859SSepherosa Ziehau 		switch (ch) {
77*da0f7859SSepherosa Ziehau 		case 'a':
78*da0f7859SSepherosa Ziehau 			dropall = true;
79*da0f7859SSepherosa Ziehau 			break;
80*da0f7859SSepherosa Ziehau 		case 'l':
81*da0f7859SSepherosa Ziehau 			tcpdrop_list_commands = true;
82*da0f7859SSepherosa Ziehau 			break;
83*da0f7859SSepherosa Ziehau 		default:
84*da0f7859SSepherosa Ziehau 			usage();
85*da0f7859SSepherosa Ziehau 		}
86*da0f7859SSepherosa Ziehau 	}
87*da0f7859SSepherosa Ziehau 	argc -= optind;
88*da0f7859SSepherosa Ziehau 	argv += optind;
89*da0f7859SSepherosa Ziehau 
90*da0f7859SSepherosa Ziehau 	if (dropall) {
91*da0f7859SSepherosa Ziehau 		if (argc != 0)
92*da0f7859SSepherosa Ziehau 			usage();
93*da0f7859SSepherosa Ziehau 		if (!tcpdropall())
94*da0f7859SSepherosa Ziehau 			exit(1);
95*da0f7859SSepherosa Ziehau 		exit(0);
96*da0f7859SSepherosa Ziehau 	}
97*da0f7859SSepherosa Ziehau 
98*da0f7859SSepherosa Ziehau 	if ((argc != 2 && argc != 4) || tcpdrop_list_commands)
99*da0f7859SSepherosa Ziehau 		usage();
100*da0f7859SSepherosa Ziehau 
101*da0f7859SSepherosa Ziehau 	if (argc == 2) {
102*da0f7859SSepherosa Ziehau 		lport = findport(argv[0]);
103*da0f7859SSepherosa Ziehau 		fport = findport(argv[1]);
104*da0f7859SSepherosa Ziehau 		if (lport == NULL || lport[1] == '\0' || fport == NULL ||
105*da0f7859SSepherosa Ziehau 		    fport[1] == '\0')
106*da0f7859SSepherosa Ziehau 			usage();
107*da0f7859SSepherosa Ziehau 		*lport++ = '\0';
108*da0f7859SSepherosa Ziehau 		*fport++ = '\0';
109*da0f7859SSepherosa Ziehau 		if (!tcpdropbyname(argv[0], lport, argv[1], fport))
110*da0f7859SSepherosa Ziehau 			exit(1);
111*da0f7859SSepherosa Ziehau 	} else if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3]))
112*da0f7859SSepherosa Ziehau 		exit(1);
113*da0f7859SSepherosa Ziehau 
114*da0f7859SSepherosa Ziehau 	exit(0);
115*da0f7859SSepherosa Ziehau }
116*da0f7859SSepherosa Ziehau 
117*da0f7859SSepherosa Ziehau static char *
findport(const char * arg)118*da0f7859SSepherosa Ziehau findport(const char *arg)
119*da0f7859SSepherosa Ziehau {
120*da0f7859SSepherosa Ziehau 	char *dot, *colon;
121*da0f7859SSepherosa Ziehau 
122*da0f7859SSepherosa Ziehau 	/* A strrspn() or strrpbrk() would be nice. */
123*da0f7859SSepherosa Ziehau 	dot = strrchr(arg, '.');
124*da0f7859SSepherosa Ziehau 	colon = strrchr(arg, ':');
125*da0f7859SSepherosa Ziehau 	if (dot == NULL)
126*da0f7859SSepherosa Ziehau 		return (colon);
127*da0f7859SSepherosa Ziehau 	if (colon == NULL)
128*da0f7859SSepherosa Ziehau 		return (dot);
129*da0f7859SSepherosa Ziehau 	if (dot < colon)
130*da0f7859SSepherosa Ziehau 		return (colon);
131*da0f7859SSepherosa Ziehau 	else
132*da0f7859SSepherosa Ziehau 		return (dot);
133*da0f7859SSepherosa Ziehau }
134*da0f7859SSepherosa Ziehau 
135*da0f7859SSepherosa Ziehau static struct xtcpcb *
tcpcblist(const char * name,size_t * len0)136*da0f7859SSepherosa Ziehau tcpcblist(const char *name, size_t *len0)
137*da0f7859SSepherosa Ziehau {
138*da0f7859SSepherosa Ziehau 	struct xtcpcb *tcp;
139*da0f7859SSepherosa Ziehau 	size_t len;
140*da0f7859SSepherosa Ziehau 	int rv;
141*da0f7859SSepherosa Ziehau 
142*da0f7859SSepherosa Ziehau 	len = 0;
143*da0f7859SSepherosa Ziehau 	rv = sysctlbyname(name, NULL, &len, NULL, 0);
144*da0f7859SSepherosa Ziehau 	if (rv == -1)
145*da0f7859SSepherosa Ziehau 		err(1, "sysctlbyname %s", name);
146*da0f7859SSepherosa Ziehau 
147*da0f7859SSepherosa Ziehau 	if (len == 0)
148*da0f7859SSepherosa Ziehau 		errx(1, "%s is empty", name);
149*da0f7859SSepherosa Ziehau 
150*da0f7859SSepherosa Ziehau 	tcp = malloc(len);
151*da0f7859SSepherosa Ziehau 	if (tcp == NULL)
152*da0f7859SSepherosa Ziehau 		errx(1, "malloc failed");
153*da0f7859SSepherosa Ziehau 
154*da0f7859SSepherosa Ziehau 	rv = sysctlbyname(name, tcp, &len, NULL, 0);
155*da0f7859SSepherosa Ziehau 	if (rv == -1)
156*da0f7859SSepherosa Ziehau 		err(1, "sysctlbyname %s", name);
157*da0f7859SSepherosa Ziehau 
158*da0f7859SSepherosa Ziehau 	*len0 = len;
159*da0f7859SSepherosa Ziehau 	return (tcp);
160*da0f7859SSepherosa Ziehau }
161*da0f7859SSepherosa Ziehau 
162*da0f7859SSepherosa Ziehau static void
sockinfo(const struct sockaddr * sa,struct host_service * hs)163*da0f7859SSepherosa Ziehau sockinfo(const struct sockaddr *sa, struct host_service *hs)
164*da0f7859SSepherosa Ziehau {
165*da0f7859SSepherosa Ziehau 	static const int flags = NI_NUMERICHOST | NI_NUMERICSERV;
166*da0f7859SSepherosa Ziehau 	int rv;
167*da0f7859SSepherosa Ziehau 
168*da0f7859SSepherosa Ziehau 	rv = getnameinfo(sa, sa->sa_len, hs->hs_host, sizeof hs->hs_host,
169*da0f7859SSepherosa Ziehau 	    hs->hs_service, sizeof hs->hs_service, flags);
170*da0f7859SSepherosa Ziehau 	if (rv == -1)
171*da0f7859SSepherosa Ziehau 		err(1, "getnameinfo");
172*da0f7859SSepherosa Ziehau }
173*da0f7859SSepherosa Ziehau 
174*da0f7859SSepherosa Ziehau static bool
tcpdrop(const struct sockaddr * lsa,const struct sockaddr * fsa)175*da0f7859SSepherosa Ziehau tcpdrop(const struct sockaddr *lsa, const struct sockaddr *fsa)
176*da0f7859SSepherosa Ziehau {
177*da0f7859SSepherosa Ziehau 	struct host_service local, foreign;
178*da0f7859SSepherosa Ziehau 	struct sockaddr_storage addrs[2];
179*da0f7859SSepherosa Ziehau 	int rv;
180*da0f7859SSepherosa Ziehau 
181*da0f7859SSepherosa Ziehau 	memcpy(&addrs[TCPDROP_FOREIGN], fsa, fsa->sa_len);
182*da0f7859SSepherosa Ziehau 	memcpy(&addrs[TCPDROP_LOCAL], lsa, lsa->sa_len);
183*da0f7859SSepherosa Ziehau 
184*da0f7859SSepherosa Ziehau 	sockinfo(lsa, &local);
185*da0f7859SSepherosa Ziehau 	sockinfo(fsa, &foreign);
186*da0f7859SSepherosa Ziehau 
187*da0f7859SSepherosa Ziehau 	if (tcpdrop_list_commands) {
188*da0f7859SSepherosa Ziehau 		printf("tcpdrop %s %s %s %s\n", local.hs_host, local.hs_service,
189*da0f7859SSepherosa Ziehau 		    foreign.hs_host, foreign.hs_service);
190*da0f7859SSepherosa Ziehau 		return (true);
191*da0f7859SSepherosa Ziehau 	}
192*da0f7859SSepherosa Ziehau 
193*da0f7859SSepherosa Ziehau 	rv = sysctlbyname("net.inet.tcp.drop", NULL, NULL, &addrs,
194*da0f7859SSepherosa Ziehau 	    sizeof addrs);
195*da0f7859SSepherosa Ziehau 	if (rv == -1) {
196*da0f7859SSepherosa Ziehau 		warn("%s %s %s %s", local.hs_host, local.hs_service,
197*da0f7859SSepherosa Ziehau 		    foreign.hs_host, foreign.hs_service);
198*da0f7859SSepherosa Ziehau 		return (false);
199*da0f7859SSepherosa Ziehau 	}
200*da0f7859SSepherosa Ziehau 	printf("%s %s %s %s: dropped\n", local.hs_host, local.hs_service,
201*da0f7859SSepherosa Ziehau 	    foreign.hs_host, foreign.hs_service);
202*da0f7859SSepherosa Ziehau 	return (true);
203*da0f7859SSepherosa Ziehau }
204*da0f7859SSepherosa Ziehau 
205*da0f7859SSepherosa Ziehau static bool
tcpdropall(void)206*da0f7859SSepherosa Ziehau tcpdropall(void)
207*da0f7859SSepherosa Ziehau {
208*da0f7859SSepherosa Ziehau 	struct xtcpcb *tcp;
209*da0f7859SSepherosa Ziehau 	size_t len;
210*da0f7859SSepherosa Ziehau 	bool ok;
211*da0f7859SSepherosa Ziehau 	int cnt, i;
212*da0f7859SSepherosa Ziehau 
213*da0f7859SSepherosa Ziehau 	ok = true;
214*da0f7859SSepherosa Ziehau 
215*da0f7859SSepherosa Ziehau 	tcp = tcpcblist("net.inet.tcp.pcblist", &len);
216*da0f7859SSepherosa Ziehau 	cnt = len / sizeof(*tcp);
217*da0f7859SSepherosa Ziehau 
218*da0f7859SSepherosa Ziehau 	for (i = 0; i < cnt; ++i) {
219*da0f7859SSepherosa Ziehau 		struct xtcpcb *xpcb;
220*da0f7859SSepherosa Ziehau 		struct tcpcb *tp;
221*da0f7859SSepherosa Ziehau 		struct inpcb *inp;
222*da0f7859SSepherosa Ziehau 
223*da0f7859SSepherosa Ziehau 		xpcb = &tcp[i];
224*da0f7859SSepherosa Ziehau 		if (xpcb->xt_len != sizeof(*xpcb))
225*da0f7859SSepherosa Ziehau 			break;
226*da0f7859SSepherosa Ziehau 
227*da0f7859SSepherosa Ziehau 		tp = &xpcb->xt_tp;
228*da0f7859SSepherosa Ziehau 		inp = &xpcb->xt_inp;
229*da0f7859SSepherosa Ziehau 
230*da0f7859SSepherosa Ziehau 		/*
231*da0f7859SSepherosa Ziehau 		 * XXX
232*da0f7859SSepherosa Ziehau 		 * Check protocol, support just v4 or v6, etc.
233*da0f7859SSepherosa Ziehau 		 */
234*da0f7859SSepherosa Ziehau 
235*da0f7859SSepherosa Ziehau 		/* Skip listening sockets.  */
236*da0f7859SSepherosa Ziehau 		if (tp->t_state == TCPS_LISTEN)
237*da0f7859SSepherosa Ziehau 			continue;
238*da0f7859SSepherosa Ziehau 
239*da0f7859SSepherosa Ziehau 		if (!tcpdropconn(&inp->inp_inc))
240*da0f7859SSepherosa Ziehau 			ok = false;
241*da0f7859SSepherosa Ziehau 	}
242*da0f7859SSepherosa Ziehau 	free(tcp);
243*da0f7859SSepherosa Ziehau 
244*da0f7859SSepherosa Ziehau 	return (ok);
245*da0f7859SSepherosa Ziehau }
246*da0f7859SSepherosa Ziehau 
247*da0f7859SSepherosa Ziehau static bool
tcpdropbyname(const char * lhost,const char * lport,const char * fhost,const char * fport)248*da0f7859SSepherosa Ziehau tcpdropbyname(const char *lhost, const char *lport, const char *fhost,
249*da0f7859SSepherosa Ziehau     const char *fport)
250*da0f7859SSepherosa Ziehau {
251*da0f7859SSepherosa Ziehau 	static const struct addrinfo hints = {
252*da0f7859SSepherosa Ziehau 		/*
253*da0f7859SSepherosa Ziehau 		 * Look for streams in all domains.
254*da0f7859SSepherosa Ziehau 		 */
255*da0f7859SSepherosa Ziehau 		.ai_family = AF_UNSPEC,
256*da0f7859SSepherosa Ziehau 		.ai_socktype = SOCK_STREAM,
257*da0f7859SSepherosa Ziehau 	};
258*da0f7859SSepherosa Ziehau 	struct addrinfo *ail, *local, *aif, *foreign;
259*da0f7859SSepherosa Ziehau 	int error;
260*da0f7859SSepherosa Ziehau 	bool ok, infamily;
261*da0f7859SSepherosa Ziehau 
262*da0f7859SSepherosa Ziehau 	error = getaddrinfo(lhost, lport, &hints, &local);
263*da0f7859SSepherosa Ziehau 	if (error != 0)
264*da0f7859SSepherosa Ziehau 		errx(1, "getaddrinfo: %s port %s: %s", lhost, lport,
265*da0f7859SSepherosa Ziehau 		    gai_strerror(error));
266*da0f7859SSepherosa Ziehau 
267*da0f7859SSepherosa Ziehau 	error = getaddrinfo(fhost, fport, &hints, &foreign);
268*da0f7859SSepherosa Ziehau 	if (error != 0) {
269*da0f7859SSepherosa Ziehau 		freeaddrinfo(local); /* XXX gratuitous */
270*da0f7859SSepherosa Ziehau 		errx(1, "getaddrinfo: %s port %s: %s", fhost, fport,
271*da0f7859SSepherosa Ziehau 		    gai_strerror(error));
272*da0f7859SSepherosa Ziehau 	}
273*da0f7859SSepherosa Ziehau 
274*da0f7859SSepherosa Ziehau 	ok = true;
275*da0f7859SSepherosa Ziehau 	infamily = false;
276*da0f7859SSepherosa Ziehau 
277*da0f7859SSepherosa Ziehau 	/*
278*da0f7859SSepherosa Ziehau 	 * Try every combination of local and foreign address pairs.
279*da0f7859SSepherosa Ziehau 	 */
280*da0f7859SSepherosa Ziehau 	for (ail = local; ail != NULL; ail = ail->ai_next) {
281*da0f7859SSepherosa Ziehau 		for (aif = foreign; aif != NULL; aif = aif->ai_next) {
282*da0f7859SSepherosa Ziehau 			if (ail->ai_family != aif->ai_family)
283*da0f7859SSepherosa Ziehau 				continue;
284*da0f7859SSepherosa Ziehau 			infamily = true;
285*da0f7859SSepherosa Ziehau 			if (!tcpdrop(ail->ai_addr, aif->ai_addr))
286*da0f7859SSepherosa Ziehau 				ok = false;
287*da0f7859SSepherosa Ziehau 		}
288*da0f7859SSepherosa Ziehau 	}
289*da0f7859SSepherosa Ziehau 
290*da0f7859SSepherosa Ziehau 	if (!infamily) {
291*da0f7859SSepherosa Ziehau 		warnx("%s %s %s %s: different address families", lhost, lport,
292*da0f7859SSepherosa Ziehau 		    fhost, fport);
293*da0f7859SSepherosa Ziehau 		ok = false;
294*da0f7859SSepherosa Ziehau 	}
295*da0f7859SSepherosa Ziehau 
296*da0f7859SSepherosa Ziehau 	freeaddrinfo(local);
297*da0f7859SSepherosa Ziehau 	freeaddrinfo(foreign);
298*da0f7859SSepherosa Ziehau 
299*da0f7859SSepherosa Ziehau 	return (ok);
300*da0f7859SSepherosa Ziehau }
301*da0f7859SSepherosa Ziehau 
302*da0f7859SSepherosa Ziehau static bool
tcpdropconn(const struct in_conninfo * inc)303*da0f7859SSepherosa Ziehau tcpdropconn(const struct in_conninfo *inc)
304*da0f7859SSepherosa Ziehau {
305*da0f7859SSepherosa Ziehau 	struct sockaddr *local, *foreign;
306*da0f7859SSepherosa Ziehau 	struct sockaddr_in6 sin6[2];
307*da0f7859SSepherosa Ziehau 	struct sockaddr_in sin4[2];
308*da0f7859SSepherosa Ziehau 
309*da0f7859SSepherosa Ziehau 	if (inc->inc_isipv6) {
310*da0f7859SSepherosa Ziehau 		memset(sin6, 0, sizeof sin6);
311*da0f7859SSepherosa Ziehau 
312*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_LOCAL].sin6_len = sizeof sin6[TCPDROP_LOCAL];
313*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_LOCAL].sin6_family = AF_INET6;
314*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_LOCAL].sin6_port = inc->inc_lport;
315*da0f7859SSepherosa Ziehau 		memcpy(&sin6[TCPDROP_LOCAL].sin6_addr, &inc->inc6_laddr,
316*da0f7859SSepherosa Ziehau 		    sizeof inc->inc6_laddr);
317*da0f7859SSepherosa Ziehau 		local = (struct sockaddr *)&sin6[TCPDROP_LOCAL];
318*da0f7859SSepherosa Ziehau 
319*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_FOREIGN].sin6_len = sizeof sin6[TCPDROP_FOREIGN];
320*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_FOREIGN].sin6_family = AF_INET6;
321*da0f7859SSepherosa Ziehau 		sin6[TCPDROP_FOREIGN].sin6_port = inc->inc_fport;
322*da0f7859SSepherosa Ziehau 		memcpy(&sin6[TCPDROP_FOREIGN].sin6_addr, &inc->inc6_faddr,
323*da0f7859SSepherosa Ziehau 		    sizeof inc->inc6_faddr);
324*da0f7859SSepherosa Ziehau 		foreign = (struct sockaddr *)&sin6[TCPDROP_FOREIGN];
325*da0f7859SSepherosa Ziehau 	} else {
326*da0f7859SSepherosa Ziehau 		memset(&sin4[TCPDROP_LOCAL], 0, sizeof sin4[TCPDROP_LOCAL]);
327*da0f7859SSepherosa Ziehau 
328*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_LOCAL].sin_len = sizeof sin4[TCPDROP_LOCAL];
329*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_LOCAL].sin_family = AF_INET;
330*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_LOCAL].sin_port = inc->inc_lport;
331*da0f7859SSepherosa Ziehau 		memcpy(&sin4[TCPDROP_LOCAL].sin_addr, &inc->inc_laddr,
332*da0f7859SSepherosa Ziehau 		    sizeof inc->inc_laddr);
333*da0f7859SSepherosa Ziehau 		local = (struct sockaddr *)&sin4[TCPDROP_LOCAL];
334*da0f7859SSepherosa Ziehau 
335*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_FOREIGN].sin_len = sizeof sin4[TCPDROP_FOREIGN];
336*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_FOREIGN].sin_family = AF_INET;
337*da0f7859SSepherosa Ziehau 		sin4[TCPDROP_FOREIGN].sin_port = inc->inc_fport;
338*da0f7859SSepherosa Ziehau 		memcpy(&sin4[TCPDROP_FOREIGN].sin_addr, &inc->inc_faddr,
339*da0f7859SSepherosa Ziehau 		    sizeof inc->inc_faddr);
340*da0f7859SSepherosa Ziehau 		foreign = (struct sockaddr *)&sin4[TCPDROP_FOREIGN];
341*da0f7859SSepherosa Ziehau 	}
342*da0f7859SSepherosa Ziehau 
343*da0f7859SSepherosa Ziehau 	return (tcpdrop(local, foreign));
344*da0f7859SSepherosa Ziehau }
345*da0f7859SSepherosa Ziehau 
346*da0f7859SSepherosa Ziehau static void
usage(void)347*da0f7859SSepherosa Ziehau usage(void)
348*da0f7859SSepherosa Ziehau {
349*da0f7859SSepherosa Ziehau 	fprintf(stderr,
350*da0f7859SSepherosa Ziehau "usage: tcpdrop local-address local-port foreign-address foreign-port\n"
351*da0f7859SSepherosa Ziehau "       tcpdrop local-address:local-port foreign-address:foreign-port\n"
352*da0f7859SSepherosa Ziehau "       tcpdrop local-address.local-port foreign-address.foreign-port\n"
353*da0f7859SSepherosa Ziehau "       tcpdrop [-l] -a\n");
354*da0f7859SSepherosa Ziehau 	exit(1);
355*da0f7859SSepherosa Ziehau }
356