xref: /freebsd/usr.sbin/flowctl/flowctl.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
5  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $SourceForge: flowctl.c,v 1.15 2004/08/31 20:24:58 glebius Exp $
30  */
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <sys/queue.h>
36 
37 #include <net/if.h>
38 #include <netinet/in.h>
39 
40 #include <arpa/inet.h>
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48 
49 #include <netgraph.h>
50 #include <netgraph/netflow/ng_netflow.h>
51 
52 #define	CISCO_SH_FLOW_HEADER	"SrcIf         SrcIPaddress    " \
53 "DstIf         DstIPaddress    Pr SrcP DstP  Pkts\n"
54 #define	CISCO_SH_FLOW	"%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
55 
56 /* human-readable IPv4 header */
57 #define	CISCO_SH_FLOW_HHEADER	"SrcIf         SrcIPaddress    " \
58 "DstIf         DstIPaddress    Proto  SrcPort  DstPort     Pkts\n"
59 #define	CISCO_SH_FLOW_H	"%-13s %-15s %-13s %-15s %5u %8d %8d %8lu\n"
60 
61 #define	CISCO_SH_FLOW6_HEADER	"SrcIf         SrcIPaddress                   " \
62 "DstIf         DstIPaddress                   Pr SrcP DstP  Pkts\n"
63 #define	CISCO_SH_FLOW6		"%-13s %-30s %-13s %-30s %2u %4.4x %4.4x %6lu\n"
64 
65 /* Human-readable IPv6 headers */
66 #define	CISCO_SH_FLOW6_HHEADER	"SrcIf         SrcIPaddress                         " \
67 "DstIf         DstIPaddress                         Proto  SrcPort  DstPort     Pkts\n"
68 #define	CISCO_SH_FLOW6_H	"%-13s %-36s %-13s %-36s %5u %8d %8d %8lu\n"
69 
70 #define	CISCO_SH_VERB_FLOW_HEADER "SrcIf          SrcIPaddress    " \
71 "DstIf          DstIPaddress    Pr TOS Flgs  Pkts\n" \
72 "Port Msk AS                    Port Msk AS    NextHop              B/Pk  Active\n"
73 
74 #define	CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
75 	"%4.4x /%-2u %-5u                 %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
76 
77 #define	CISCO_SH_VERB_FLOW6_HEADER "SrcIf          SrcIPaddress                   " \
78 "DstIf          DstIPaddress                   Pr TOS Flgs  Pkts\n" \
79 "Port Msk AS                    Port Msk AS    NextHop                             B/Pk  Active\n"
80 
81 #define	CISCO_SH_VERB_FLOW6 "%-14s %-30s %-14s %-30s %2u %3x %4x %6lu\n" \
82 	"%4.4x /%-2u %-5u                 %4.4x /%-2u %-5u %-30s %9u %8u\n\n"
83 #ifdef INET
84 static void flow_cache_print(struct ngnf_show_header *resp);
85 static void flow_cache_print_verbose(struct ngnf_show_header *resp);
86 #endif
87 #ifdef INET6
88 static void flow_cache_print6(struct ngnf_show_header *resp);
89 static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
90 #endif
91 static void ctl_show(int, char **);
92 #if defined(INET) || defined(INET6)
93 static void do_show(int, void (*func)(struct ngnf_show_header *));
94 #endif
95 static void help(void);
96 static void execute_command(int, char **);
97 
98 struct ip_ctl_cmd {
99 	char	*cmd_name;
100 	void	(*cmd_func)(int argc, char **argv);
101 };
102 
103 struct ip_ctl_cmd cmds[] = {
104     {"show",	ctl_show},
105     {NULL,	NULL},
106 };
107 
108 int	cs, human = 0;
109 char	*ng_path;
110 
111 int
112 main(int argc, char **argv)
113 {
114 	int c;
115 	char sname[NG_NODESIZ];
116 	int rcvbuf = SORCVBUF_SIZE;
117 
118 	/* parse options */
119 	while ((c = getopt(argc, argv, "d:")) != -1) {
120 		switch (c) {
121 		case 'd':	/* set libnetgraph debug level. */
122 			NgSetDebug(atoi(optarg));
123 			break;
124 		}
125 	}
126 
127 	argc -= optind;
128 	argv += optind;
129 	ng_path = argv[0];
130 	if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
131 		help();
132 	argc--;
133 	argv++;
134 
135 	/* create control socket. */
136 	snprintf(sname, sizeof(sname), "flowctl%i", getpid());
137 
138 	if (NgMkSockNode(sname, &cs, NULL) == -1)
139 		err(1, "NgMkSockNode");
140 
141 	/* set receive buffer size */
142 	if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
143 		err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
144 
145 	/* parse and execute command */
146 	execute_command(argc, argv);
147 
148 	close(cs);
149 
150 	exit(0);
151 }
152 
153 static void
154 execute_command(int argc, char **argv)
155 {
156 	int cindex = -1;
157 	int i;
158 
159 	if (!argc)
160 		help();
161 	for (i = 0; cmds[i].cmd_name != NULL; i++)
162 		if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
163 			if (cindex != -1)
164 				errx(1, "ambiguous command: %s", argv[0]);
165 			cindex = i;
166 		}
167 	if (cindex == -1)
168 		errx(1, "bad command: %s", argv[0]);
169 	argc--;
170 	argv++;
171 	(*cmds[cindex].cmd_func)(argc, argv);
172 }
173 
174 static void
175 ctl_show(int argc, char **argv)
176 {
177 	int ipv4, ipv6, verbose = 0;
178 
179 	ipv4 = feature_present("inet");
180 	ipv6 = feature_present("inet6");
181 
182 	if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
183 		ipv6 = 0;
184 		argc--;
185 		argv++;
186 	}
187 	if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
188 		ipv4 = 0;
189 		argc--;
190 		argv++;
191 	}
192 
193 	if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
194 		verbose = 1;
195 
196 	if (argc > 0 && !strncmp(argv[0], "human", strlen(argv[0])))
197 		human = 1;
198 
199 #ifdef INET
200 	if (ipv4) {
201 		if (verbose)
202 			do_show(4, &flow_cache_print_verbose);
203 		else
204 			do_show(4, &flow_cache_print);
205 	}
206 #endif
207 
208 #ifdef INET6
209 	if (ipv6) {
210 		if (verbose)
211 			do_show(6, &flow_cache_print6_verbose);
212 		else
213 			do_show(6, &flow_cache_print6);
214 	}
215 #endif
216 }
217 
218 #if defined(INET) || defined(INET6)
219 static void
220 do_show(int version, void (*func)(struct ngnf_show_header *))
221 {
222 	char buf[SORCVBUF_SIZE];
223 	struct ng_mesg *ng_mesg;
224 	struct ngnf_show_header req, *resp;
225 	int token, nread;
226 
227 	ng_mesg = (struct ng_mesg *)buf;
228 	req.version = version;
229 	req.hash_id = req.list_id = 0;
230 
231 	for (;;) {
232 		/* request set of accounting records */
233 		token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
234 		    NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
235 		if (token == -1)
236 			err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
237 
238 		/* read reply */
239 		nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
240 		if (nread == -1)
241 			err(1, "NgRecvMsg() failed");
242 
243 		if (ng_mesg->header.token != token)
244 			err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
245 
246 		resp = (struct ngnf_show_header *)ng_mesg->data;
247 		if ((ng_mesg->header.arglen < (sizeof(*resp))) ||
248 		    (ng_mesg->header.arglen < (sizeof(*resp) +
249 		    (resp->nentries * sizeof(struct flow_entry_data)))))
250 			err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
251 
252 		(*func)(resp);
253 
254 		if (resp->hash_id != 0)
255 			req.hash_id = resp->hash_id;
256 		else
257 			break;
258 		req.list_id = resp->list_id;
259 	}
260 }
261 #endif
262 
263 #ifdef INET
264 static void
265 flow_cache_print(struct ngnf_show_header *resp)
266 {
267 	struct flow_entry_data *fle;
268 	char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
269 	char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
270 	int i;
271 
272 	if (resp->version != 4)
273 		errx(EX_SOFTWARE, "%s: version mismatch: %u",
274 		    __func__, resp->version);
275 
276 	if (resp->nentries > 0)
277 		printf(human ? CISCO_SH_FLOW_HHEADER : CISCO_SH_FLOW_HEADER);
278 
279 	fle = (struct flow_entry_data *)(resp + 1);
280 	for (i = 0; i < resp->nentries; i++, fle++) {
281 		inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
282 		inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
283 		printf(human ? CISCO_SH_FLOW_H : CISCO_SH_FLOW,
284 			if_indextoname(fle->fle_i_ifx, src_if),
285 			src,
286 			if_indextoname(fle->fle_o_ifx, dst_if),
287 			dst,
288 			fle->r.r_ip_p,
289 			ntohs(fle->r.r_sport),
290 			ntohs(fle->r.r_dport),
291 			fle->packets);
292 
293 	}
294 }
295 #endif
296 
297 #ifdef INET6
298 static void
299 flow_cache_print6(struct ngnf_show_header *resp)
300 {
301 	struct flow6_entry_data *fle6;
302 	char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
303 	char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
304 	int i;
305 
306 	if (resp->version != 6)
307 		errx(EX_SOFTWARE, "%s: version mismatch: %u",
308 		    __func__, resp->version);
309 
310 	if (resp->nentries > 0)
311 		printf(human ? CISCO_SH_FLOW6_HHEADER : CISCO_SH_FLOW6_HEADER);
312 
313 	fle6 = (struct flow6_entry_data *)(resp + 1);
314 	for (i = 0; i < resp->nentries; i++, fle6++) {
315 		inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
316 		inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
317 		printf(human ? CISCO_SH_FLOW6_H : CISCO_SH_FLOW6,
318 			if_indextoname(fle6->fle_i_ifx, src_if),
319 			src6,
320 			if_indextoname(fle6->fle_o_ifx, dst_if),
321 			dst6,
322 			fle6->r.r_ip_p,
323 			ntohs(fle6->r.r_sport),
324 			ntohs(fle6->r.r_dport),
325 			fle6->packets);
326 
327 	}
328 }
329 #endif
330 
331 #ifdef INET
332 static void
333 flow_cache_print_verbose(struct ngnf_show_header *resp)
334 {
335 	struct flow_entry_data *fle;
336 	char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
337 	char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
338 	int i;
339 
340 	if (resp->version != 4)
341 		errx(EX_SOFTWARE, "%s: version mismatch: %u",
342 		    __func__, resp->version);
343 
344 	printf(CISCO_SH_VERB_FLOW_HEADER);
345 
346 	fle = (struct flow_entry_data *)(resp + 1);
347 	for (i = 0; i < resp->nentries; i++, fle++) {
348 		inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
349 		inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
350 		inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
351 		printf(CISCO_SH_VERB_FLOW,
352 			if_indextoname(fle->fle_i_ifx, src_if),
353 			src,
354 			if_indextoname(fle->fle_o_ifx, dst_if),
355 			dst,
356 			fle->r.r_ip_p,
357 			fle->r.r_tos,
358 			fle->tcp_flags,
359 			fle->packets,
360 			ntohs(fle->r.r_sport),
361 			fle->src_mask,
362 			0,
363 			ntohs(fle->r.r_dport),
364 			fle->dst_mask,
365 			0,
366 			next,
367 			(u_int)(fle->bytes / fle->packets),
368 			0);
369 
370 	}
371 }
372 #endif
373 
374 #ifdef INET6
375 static void
376 flow_cache_print6_verbose(struct ngnf_show_header *resp)
377 {
378 	struct flow6_entry_data *fle6;
379 	char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN], next6[INET6_ADDRSTRLEN];
380 	char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
381 	int i;
382 
383 	if (resp->version != 6)
384 		errx(EX_SOFTWARE, "%s: version mismatch: %u",
385 		    __func__, resp->version);
386 
387 	printf(CISCO_SH_VERB_FLOW6_HEADER);
388 
389 	fle6 = (struct flow6_entry_data *)(resp + 1);
390 	for (i = 0; i < resp->nentries; i++, fle6++) {
391 		inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
392 		inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
393 		inet_ntop(AF_INET6, &fle6->n.next_hop6, next6, sizeof(next6));
394 		printf(CISCO_SH_VERB_FLOW6,
395 			if_indextoname(fle6->fle_i_ifx, src_if),
396 			src6,
397 			if_indextoname(fle6->fle_o_ifx, dst_if),
398 			dst6,
399 			fle6->r.r_ip_p,
400 			fle6->r.r_tos,
401 			fle6->tcp_flags,
402 			fle6->packets,
403 			ntohs(fle6->r.r_sport),
404 			fle6->src_mask,
405 			0,
406 			ntohs(fle6->r.r_dport),
407 			fle6->dst_mask,
408 			0,
409 			next6,
410 			(u_int)(fle6->bytes / fle6->packets),
411 			0);
412 	}
413 }
414 #endif
415 
416 static void
417 help(void)
418 {
419 	extern char *__progname;
420 
421 	fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);
422 	exit (0);
423 }
424