2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
29 * $SourceForge: flowctl.c,v 1.15 2004/08/31 20:24:58 glebius Exp $
33 static const char rcs_id[] =
37 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
55 #include <netgraph/netflow/ng_netflow.h>
57 #define CISCO_SH_FLOW_HEADER "SrcIf SrcIPaddress " \
58 "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
59 #define CISCO_SH_FLOW "%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
61 /* human-readable IPv4 header */
62 #define CISCO_SH_FLOW_HHEADER "SrcIf SrcIPaddress " \
63 "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
64 #define CISCO_SH_FLOW_H "%-13s %-15s %-13s %-15s %5u %8d %8d %8lu\n"
66 #define CISCO_SH_FLOW6_HEADER "SrcIf SrcIPaddress " \
67 "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
68 #define CISCO_SH_FLOW6 "%-13s %-30s %-13s %-30s %2u %4.4x %4.4x %6lu\n"
70 /* Human-readable IPv6 headers */
71 #define CISCO_SH_FLOW6_HHEADER "SrcIf SrcIPaddress " \
72 "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
73 #define CISCO_SH_FLOW6_H "%-13s %-36s %-13s %-36s %5u %8d %8d %8lu\n"
75 #define CISCO_SH_VERB_FLOW_HEADER "SrcIf SrcIPaddress " \
76 "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
77 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
79 #define CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
80 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
82 #define CISCO_SH_VERB_FLOW6_HEADER "SrcIf SrcIPaddress " \
83 "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
84 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
86 #define CISCO_SH_VERB_FLOW6 "%-14s %-30s %-14s %-30s %2u %3x %4x %6lu\n" \
87 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-30s %9u %8u\n\n"
89 static void flow_cache_print(struct ngnf_show_header *resp);
90 static void flow_cache_print_verbose(struct ngnf_show_header *resp);
93 static void flow_cache_print6(struct ngnf_show_header *resp);
94 static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
96 static void ctl_show(int, char **);
97 #if defined(INET) || defined(INET6)
98 static void do_show(int, void (*func)(struct ngnf_show_header *));
100 static void help(void);
101 static void execute_command(int, char **);
105 void (*cmd_func)(int argc, char **argv);
108 struct ip_ctl_cmd cmds[] = {
117 main(int argc, char **argv)
120 char sname[NG_NODESIZ];
121 int rcvbuf = SORCVBUF_SIZE;
124 while ((c = getopt(argc, argv, "d:")) != -1) {
126 case 'd': /* set libnetgraph debug level. */
127 NgSetDebug(atoi(optarg));
135 if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
140 /* create control socket. */
141 snprintf(sname, sizeof(sname), "flowctl%i", getpid());
143 if (NgMkSockNode(sname, &cs, NULL) == -1)
144 err(1, "NgMkSockNode");
146 /* set receive buffer size */
147 if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
148 err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
150 /* parse and execute command */
151 execute_command(argc, argv);
159 execute_command(int argc, char **argv)
166 for (i = 0; cmds[i].cmd_name != NULL; i++)
167 if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
169 errx(1, "ambiguous command: %s", argv[0]);
173 errx(1, "bad command: %s", argv[0]);
176 (*cmds[cindex].cmd_func)(argc, argv);
180 ctl_show(int argc, char **argv)
182 int ipv4, ipv6, verbose = 0;
184 ipv4 = feature_present("inet");
185 ipv6 = feature_present("inet6");
187 if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
192 if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
198 if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
201 if (argc > 0 && !strncmp(argv[0], "human", strlen(argv[0])))
207 do_show(4, &flow_cache_print_verbose);
209 do_show(4, &flow_cache_print);
216 do_show(6, &flow_cache_print6_verbose);
218 do_show(6, &flow_cache_print6);
223 #if defined(INET) || defined(INET6)
225 do_show(int version, void (*func)(struct ngnf_show_header *))
227 char buf[SORCVBUF_SIZE];
228 struct ng_mesg *ng_mesg;
229 struct ngnf_show_header req, *resp;
232 ng_mesg = (struct ng_mesg *)buf;
233 req.version = version;
234 req.hash_id = req.list_id = 0;
237 /* request set of accounting records */
238 token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
239 NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
241 err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
244 nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
246 err(1, "NgRecvMsg() failed");
248 if (ng_mesg->header.token != token)
249 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
251 resp = (struct ngnf_show_header *)ng_mesg->data;
252 if ((ng_mesg->header.arglen < (sizeof(*resp))) ||
253 (ng_mesg->header.arglen < (sizeof(*resp) +
254 (resp->nentries * sizeof(struct flow_entry_data)))))
255 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
259 if (resp->hash_id != 0)
260 req.hash_id = resp->hash_id;
263 req.list_id = resp->list_id;
270 flow_cache_print(struct ngnf_show_header *resp)
272 struct flow_entry_data *fle;
273 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
274 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
277 if (resp->version != 4)
278 errx(EX_SOFTWARE, "%s: version mismatch: %u",
279 __func__, resp->version);
281 if (resp->nentries > 0)
282 printf(human ? CISCO_SH_FLOW_HHEADER : CISCO_SH_FLOW_HEADER);
284 fle = (struct flow_entry_data *)(resp + 1);
285 for (i = 0; i < resp->nentries; i++, fle++) {
286 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
287 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
288 printf(human ? CISCO_SH_FLOW_H : CISCO_SH_FLOW,
289 if_indextoname(fle->fle_i_ifx, src_if),
291 if_indextoname(fle->fle_o_ifx, dst_if),
294 ntohs(fle->r.r_sport),
295 ntohs(fle->r.r_dport),
304 flow_cache_print6(struct ngnf_show_header *resp)
306 struct flow6_entry_data *fle6;
307 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
308 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
311 if (resp->version != 6)
312 errx(EX_SOFTWARE, "%s: version mismatch: %u",
313 __func__, resp->version);
315 if (resp->nentries > 0)
316 printf(human ? CISCO_SH_FLOW6_HHEADER : CISCO_SH_FLOW6_HEADER);
318 fle6 = (struct flow6_entry_data *)(resp + 1);
319 for (i = 0; i < resp->nentries; i++, fle6++) {
320 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
321 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
322 printf(human ? CISCO_SH_FLOW6_H : CISCO_SH_FLOW6,
323 if_indextoname(fle6->fle_i_ifx, src_if),
325 if_indextoname(fle6->fle_o_ifx, dst_if),
328 ntohs(fle6->r.r_sport),
329 ntohs(fle6->r.r_dport),
338 flow_cache_print_verbose(struct ngnf_show_header *resp)
340 struct flow_entry_data *fle;
341 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
342 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
345 if (resp->version != 4)
346 errx(EX_SOFTWARE, "%s: version mismatch: %u",
347 __func__, resp->version);
349 printf(CISCO_SH_VERB_FLOW_HEADER);
351 fle = (struct flow_entry_data *)(resp + 1);
352 for (i = 0; i < resp->nentries; i++, fle++) {
353 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
354 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
355 inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
356 printf(CISCO_SH_VERB_FLOW,
357 if_indextoname(fle->fle_i_ifx, src_if),
359 if_indextoname(fle->fle_o_ifx, dst_if),
365 ntohs(fle->r.r_sport),
368 ntohs(fle->r.r_dport),
372 (u_int)(fle->bytes / fle->packets),
381 flow_cache_print6_verbose(struct ngnf_show_header *resp)
383 struct flow6_entry_data *fle6;
384 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN], next6[INET6_ADDRSTRLEN];
385 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
388 if (resp->version != 6)
389 errx(EX_SOFTWARE, "%s: version mismatch: %u",
390 __func__, resp->version);
392 printf(CISCO_SH_VERB_FLOW6_HEADER);
394 fle6 = (struct flow6_entry_data *)(resp + 1);
395 for (i = 0; i < resp->nentries; i++, fle6++) {
396 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
397 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
398 inet_ntop(AF_INET6, &fle6->n.next_hop6, next6, sizeof(next6));
399 printf(CISCO_SH_VERB_FLOW6,
400 if_indextoname(fle6->fle_i_ifx, src_if),
402 if_indextoname(fle6->fle_o_ifx, dst_if),
408 ntohs(fle6->r.r_sport),
411 ntohs(fle6->r.r_dport),
415 (u_int)(fle6->bytes / fle6->packets),
424 extern char *__progname;
426 fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);