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