2 * SPDX-License-Identifier: BSD-2-Clause
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 $
32 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/queue.h>
38 #include <netinet/in.h>
40 #include <arpa/inet.h>
50 #include <netgraph/netflow/ng_netflow.h>
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"
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"
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"
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"
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"
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"
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"
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"
84 static void flow_cache_print(struct ngnf_show_header *resp);
85 static void flow_cache_print_verbose(struct ngnf_show_header *resp);
88 static void flow_cache_print6(struct ngnf_show_header *resp);
89 static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
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 *));
95 static void help(void);
96 static void execute_command(int, char **);
100 void (*cmd_func)(int argc, char **argv);
103 struct ip_ctl_cmd cmds[] = {
112 main(int argc, char **argv)
115 char sname[NG_NODESIZ];
116 int rcvbuf = SORCVBUF_SIZE;
119 while ((c = getopt(argc, argv, "d:")) != -1) {
121 case 'd': /* set libnetgraph debug level. */
122 NgSetDebug(atoi(optarg));
130 if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
135 /* create control socket. */
136 snprintf(sname, sizeof(sname), "flowctl%i", getpid());
138 if (NgMkSockNode(sname, &cs, NULL) == -1)
139 err(1, "NgMkSockNode");
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)");
145 /* parse and execute command */
146 execute_command(argc, argv);
154 execute_command(int argc, char **argv)
161 for (i = 0; cmds[i].cmd_name != NULL; i++)
162 if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
164 errx(1, "ambiguous command: %s", argv[0]);
168 errx(1, "bad command: %s", argv[0]);
171 (*cmds[cindex].cmd_func)(argc, argv);
175 ctl_show(int argc, char **argv)
177 int ipv4, ipv6, verbose = 0;
179 ipv4 = feature_present("inet");
180 ipv6 = feature_present("inet6");
182 if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
187 if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
193 if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
196 if (argc > 0 && !strncmp(argv[0], "human", strlen(argv[0])))
202 do_show(4, &flow_cache_print_verbose);
204 do_show(4, &flow_cache_print);
211 do_show(6, &flow_cache_print6_verbose);
213 do_show(6, &flow_cache_print6);
218 #if defined(INET) || defined(INET6)
220 do_show(int version, void (*func)(struct ngnf_show_header *))
222 char buf[SORCVBUF_SIZE];
223 struct ng_mesg *ng_mesg;
224 struct ngnf_show_header req, *resp;
227 ng_mesg = (struct ng_mesg *)buf;
228 req.version = version;
229 req.hash_id = req.list_id = 0;
232 /* request set of accounting records */
233 token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
234 NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
236 err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
239 nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
241 err(1, "NgRecvMsg() failed");
243 if (ng_mesg->header.token != token)
244 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
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");
254 if (resp->hash_id != 0)
255 req.hash_id = resp->hash_id;
258 req.list_id = resp->list_id;
265 flow_cache_print(struct ngnf_show_header *resp)
267 struct flow_entry_data *fle;
268 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
269 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
272 if (resp->version != 4)
273 errx(EX_SOFTWARE, "%s: version mismatch: %u",
274 __func__, resp->version);
276 if (resp->nentries > 0)
277 printf(human ? CISCO_SH_FLOW_HHEADER : CISCO_SH_FLOW_HEADER);
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),
286 if_indextoname(fle->fle_o_ifx, dst_if),
289 ntohs(fle->r.r_sport),
290 ntohs(fle->r.r_dport),
299 flow_cache_print6(struct ngnf_show_header *resp)
301 struct flow6_entry_data *fle6;
302 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
303 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
306 if (resp->version != 6)
307 errx(EX_SOFTWARE, "%s: version mismatch: %u",
308 __func__, resp->version);
310 if (resp->nentries > 0)
311 printf(human ? CISCO_SH_FLOW6_HHEADER : CISCO_SH_FLOW6_HEADER);
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),
320 if_indextoname(fle6->fle_o_ifx, dst_if),
323 ntohs(fle6->r.r_sport),
324 ntohs(fle6->r.r_dport),
333 flow_cache_print_verbose(struct ngnf_show_header *resp)
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];
340 if (resp->version != 4)
341 errx(EX_SOFTWARE, "%s: version mismatch: %u",
342 __func__, resp->version);
344 printf(CISCO_SH_VERB_FLOW_HEADER);
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),
354 if_indextoname(fle->fle_o_ifx, dst_if),
360 ntohs(fle->r.r_sport),
363 ntohs(fle->r.r_dport),
367 (u_int)(fle->bytes / fle->packets),
376 flow_cache_print6_verbose(struct ngnf_show_header *resp)
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];
383 if (resp->version != 6)
384 errx(EX_SOFTWARE, "%s: version mismatch: %u",
385 __func__, resp->version);
387 printf(CISCO_SH_VERB_FLOW6_HEADER);
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),
397 if_indextoname(fle6->fle_o_ifx, dst_if),
403 ntohs(fle6->r.r_sport),
406 ntohs(fle6->r.r_dport),
410 (u_int)(fle6->bytes / fle6->packets),
419 extern char *__progname;
421 fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);