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