2 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $SourceForge: flowctl.c,v 1.15 2004/08/31 20:24:58 glebius Exp $
31 static const char rcs_id[] =
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/queue.h>
41 #include <netinet/in.h>
43 #include <arpa/inet.h>
53 #include <netgraph/netflow/ng_netflow.h>
55 #define CISCO_SH_FLOW_HEADER "SrcIf SrcIPaddress " \
56 "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
57 #define CISCO_SH_FLOW "%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
59 /* human-readable IPv4 header */
60 #define CISCO_SH_FLOW_HHEADER "SrcIf SrcIPaddress " \
61 "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
62 #define CISCO_SH_FLOW_H "%-13s %-15s %-13s %-15s %5u %8d %8d %8lu\n"
64 #define CISCO_SH_FLOW6_HEADER "SrcIf SrcIPaddress " \
65 "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
66 #define CISCO_SH_FLOW6 "%-13s %-30s %-13s %-30s %2u %4.4x %4.4x %6lu\n"
68 /* Human-readable IPv6 headers */
69 #define CISCO_SH_FLOW6_HHEADER "SrcIf SrcIPaddress " \
70 "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
71 #define CISCO_SH_FLOW6_H "%-13s %-36s %-13s %-36s %5u %8d %8d %8lu\n"
73 #define CISCO_SH_VERB_FLOW_HEADER "SrcIf SrcIPaddress " \
74 "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
75 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
77 #define CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
78 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
80 #define CISCO_SH_VERB_FLOW6_HEADER "SrcIf SrcIPaddress " \
81 "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
82 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
84 #define CISCO_SH_VERB_FLOW6 "%-14s %-30s %-14s %-30s %2u %3x %4x %6lu\n" \
85 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-30s %9u %8u\n\n"
87 static void flow_cache_print(struct ngnf_show_header *resp);
88 static void flow_cache_print_verbose(struct ngnf_show_header *resp);
91 static void flow_cache_print6(struct ngnf_show_header *resp);
92 static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
94 static void ctl_show(int, char **);
95 #if defined(INET) || defined(INET6)
96 static void do_show(int, void (*func)(struct ngnf_show_header *));
98 static void help(void);
99 static void execute_command(int, char **);
103 void (*cmd_func)(int argc, char **argv);
106 struct ip_ctl_cmd cmds[] = {
115 main(int argc, char **argv)
118 char sname[NG_NODESIZ];
119 int rcvbuf = SORCVBUF_SIZE;
122 while ((c = getopt(argc, argv, "d:")) != -1) {
124 case 'd': /* set libnetgraph debug level. */
125 NgSetDebug(atoi(optarg));
133 if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
138 /* create control socket. */
139 snprintf(sname, sizeof(sname), "flowctl%i", getpid());
141 if (NgMkSockNode(sname, &cs, NULL) == -1)
142 err(1, "NgMkSockNode");
144 /* set receive buffer size */
145 if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
146 err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
148 /* parse and execute command */
149 execute_command(argc, argv);
157 execute_command(int argc, char **argv)
164 for (i = 0; cmds[i].cmd_name != NULL; i++)
165 if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
167 errx(1, "ambiguous command: %s", argv[0]);
171 errx(1, "bad command: %s", argv[0]);
174 (*cmds[cindex].cmd_func)(argc, argv);
178 ctl_show(int argc, char **argv)
180 int ipv4, ipv6, verbose = 0;
182 ipv4 = feature_present("inet");
183 ipv6 = feature_present("inet6");
185 if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
190 if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
196 if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
199 if (argc > 0 && !strncmp(argv[0], "human", strlen(argv[0])))
205 do_show(4, &flow_cache_print_verbose);
207 do_show(4, &flow_cache_print);
214 do_show(6, &flow_cache_print6_verbose);
216 do_show(6, &flow_cache_print6);
221 #if defined(INET) || defined(INET6)
223 do_show(int version, void (*func)(struct ngnf_show_header *))
225 char buf[SORCVBUF_SIZE];
226 struct ng_mesg *ng_mesg;
227 struct ngnf_show_header req, *resp;
230 ng_mesg = (struct ng_mesg *)buf;
231 req.version = version;
232 req.hash_id = req.list_id = 0;
235 /* request set of accounting records */
236 token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
237 NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
239 err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
242 nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
244 err(1, "NgRecvMsg() failed");
246 if (ng_mesg->header.token != token)
247 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
249 resp = (struct ngnf_show_header *)ng_mesg->data;
250 if ((ng_mesg->header.arglen < (sizeof(*resp))) ||
251 (ng_mesg->header.arglen < (sizeof(*resp) +
252 (resp->nentries * sizeof(struct flow_entry_data)))))
253 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
257 if (resp->hash_id != 0)
258 req.hash_id = resp->hash_id;
261 req.list_id = resp->list_id;
268 flow_cache_print(struct ngnf_show_header *resp)
270 struct flow_entry_data *fle;
271 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
272 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
275 if (resp->version != 4)
276 errx(EX_SOFTWARE, "%s: version mismatch: %u",
277 __func__, resp->version);
279 if (resp->nentries > 0)
280 printf(human ? CISCO_SH_FLOW_HHEADER : CISCO_SH_FLOW_HEADER);
282 fle = (struct flow_entry_data *)(resp + 1);
283 for (i = 0; i < resp->nentries; i++, fle++) {
284 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
285 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
286 printf(human ? CISCO_SH_FLOW_H : CISCO_SH_FLOW,
287 if_indextoname(fle->fle_i_ifx, src_if),
289 if_indextoname(fle->fle_o_ifx, dst_if),
292 ntohs(fle->r.r_sport),
293 ntohs(fle->r.r_dport),
302 flow_cache_print6(struct ngnf_show_header *resp)
304 struct flow6_entry_data *fle6;
305 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
306 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
309 if (resp->version != 6)
310 errx(EX_SOFTWARE, "%s: version mismatch: %u",
311 __func__, resp->version);
313 if (resp->nentries > 0)
314 printf(human ? CISCO_SH_FLOW6_HHEADER : CISCO_SH_FLOW6_HEADER);
316 fle6 = (struct flow6_entry_data *)(resp + 1);
317 for (i = 0; i < resp->nentries; i++, fle6++) {
318 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
319 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
320 printf(human ? CISCO_SH_FLOW6_H : CISCO_SH_FLOW6,
321 if_indextoname(fle6->fle_i_ifx, src_if),
323 if_indextoname(fle6->fle_o_ifx, dst_if),
326 ntohs(fle6->r.r_sport),
327 ntohs(fle6->r.r_dport),
336 flow_cache_print_verbose(struct ngnf_show_header *resp)
338 struct flow_entry_data *fle;
339 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
340 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
343 if (resp->version != 4)
344 errx(EX_SOFTWARE, "%s: version mismatch: %u",
345 __func__, resp->version);
347 printf(CISCO_SH_VERB_FLOW_HEADER);
349 fle = (struct flow_entry_data *)(resp + 1);
350 for (i = 0; i < resp->nentries; i++, fle++) {
351 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
352 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
353 inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
354 printf(CISCO_SH_VERB_FLOW,
355 if_indextoname(fle->fle_i_ifx, src_if),
357 if_indextoname(fle->fle_o_ifx, dst_if),
363 ntohs(fle->r.r_sport),
366 ntohs(fle->r.r_dport),
370 (u_int)(fle->bytes / fle->packets),
379 flow_cache_print6_verbose(struct ngnf_show_header *resp)
381 struct flow6_entry_data *fle6;
382 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN], next6[INET6_ADDRSTRLEN];
383 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
386 if (resp->version != 6)
387 errx(EX_SOFTWARE, "%s: version mismatch: %u",
388 __func__, resp->version);
390 printf(CISCO_SH_VERB_FLOW6_HEADER);
392 fle6 = (struct flow6_entry_data *)(resp + 1);
393 for (i = 0; i < resp->nentries; i++, fle6++) {
394 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
395 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
396 inet_ntop(AF_INET6, &fle6->n.next_hop6, next6, sizeof(next6));
397 printf(CISCO_SH_VERB_FLOW6,
398 if_indextoname(fle6->fle_i_ifx, src_if),
400 if_indextoname(fle6->fle_o_ifx, dst_if),
406 ntohs(fle6->r.r_sport),
409 ntohs(fle6->r.r_dport),
413 (u_int)(fle6->bytes / fle6->packets),
422 extern char *__progname;
424 fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);