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 DstIf DstIPaddress Pr SrcP DstP Pkts\n"
56 #define CISCO_SH_FLOW "%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
58 #define CISCO_SH_FLOW6_HEADER "SrcIf SrcIPaddress DstIf DstIPaddress Pr SrcP DstP Pkts\n"
59 #define CISCO_SH_FLOW6 "%-13s %-30s %-13s %-30s %2u %4.4x %4.4x %6lu\n"
61 #define CISCO_SH_VERB_FLOW_HEADER "SrcIf SrcIPaddress DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
62 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
64 #define CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
65 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
67 #define CISCO_SH_VERB_FLOW6_HEADER "SrcIf SrcIPaddress DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
68 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
70 #define CISCO_SH_VERB_FLOW6 "%-14s %-30s %-14s %-30s %2u %3x %4x %6lu\n" \
71 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-30s %9u %8u\n\n"
73 static void flow_cache_print(struct ngnf_show_header *resp);
74 static void flow_cache_print_verbose(struct ngnf_show_header *resp);
77 static void flow_cache_print6(struct ngnf_show_header *resp);
78 static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
80 static void ctl_show(int, char **);
81 #if defined(INET) || defined(INET6)
82 static void do_show(int, void (*func)(struct ngnf_show_header *));
84 static void help(void);
85 static void execute_command(int, char **);
89 void (*cmd_func)(int argc, char **argv);
92 struct ip_ctl_cmd cmds[] = {
101 main(int argc, char **argv)
104 char sname[NG_NODESIZ];
105 int rcvbuf = SORCVBUF_SIZE;
108 while ((c = getopt(argc, argv, "d:")) != -1) {
110 case 'd': /* set libnetgraph debug level. */
111 NgSetDebug(atoi(optarg));
119 if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
124 /* create control socket. */
125 snprintf(sname, sizeof(sname), "flowctl%i", getpid());
127 if (NgMkSockNode(sname, &cs, NULL) == -1)
128 err(1, "NgMkSockNode");
130 /* set receive buffer size */
131 if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
132 err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
134 /* parse and execute command */
135 execute_command(argc, argv);
143 execute_command(int argc, char **argv)
150 for (i = 0; cmds[i].cmd_name != NULL; i++)
151 if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
153 errx(1, "ambiguous command: %s", argv[0]);
157 errx(1, "bad command: %s", argv[0]);
160 (*cmds[cindex].cmd_func)(argc, argv);
164 ctl_show(int argc, char **argv)
166 int ipv4, ipv6, verbose = 0;
168 ipv4 = feature_present("inet");
169 ipv6 = feature_present("inet6");
171 if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
176 if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
182 if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
188 do_show(4, &flow_cache_print_verbose);
190 do_show(4, &flow_cache_print);
197 do_show(6, &flow_cache_print6_verbose);
199 do_show(6, &flow_cache_print6);
204 #if defined(INET) || defined(INET6)
206 do_show(int version, void (*func)(struct ngnf_show_header *))
208 struct ng_mesg *ng_mesg;
209 struct ngnf_show_header req, *resp;
212 ng_mesg = alloca(SORCVBUF_SIZE);
214 req.version = version;
215 req.hash_id = req.list_id = 0;
218 /* request set of accounting records */
219 token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
220 NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
222 err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
225 nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
227 err(1, "NgRecvMsg() failed");
229 if (ng_mesg->header.token != token)
230 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
232 resp = (struct ngnf_show_header *)ng_mesg->data;
233 if ((ng_mesg->header.arglen < (sizeof(*resp))) ||
234 (ng_mesg->header.arglen < (sizeof(*resp) +
235 (resp->nentries * sizeof(struct flow_entry_data)))))
236 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
240 if (resp->hash_id != 0)
241 req.hash_id = resp->hash_id;
244 req.list_id = resp->list_id;
251 flow_cache_print(struct ngnf_show_header *resp)
253 struct flow_entry_data *fle;
254 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
255 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
258 if (resp->version != 4)
259 errx(EX_SOFTWARE, "%s: version mismatch: %u",
260 __func__, resp->version);
262 printf(CISCO_SH_FLOW_HEADER);
264 fle = (struct flow_entry_data *)(resp + 1);
265 for (i = 0; i < resp->nentries; i++, fle++) {
266 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
267 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
268 printf(CISCO_SH_FLOW,
269 if_indextoname(fle->fle_i_ifx, src_if),
271 if_indextoname(fle->fle_o_ifx, dst_if),
274 ntohs(fle->r.r_sport),
275 ntohs(fle->r.r_dport),
284 flow_cache_print6(struct ngnf_show_header *resp)
286 struct flow6_entry_data *fle6;
287 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
288 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
291 if (resp->version != 6)
292 errx(EX_SOFTWARE, "%s: version mismatch: %u",
293 __func__, resp->version);
295 printf(CISCO_SH_FLOW6_HEADER);
297 fle6 = (struct flow6_entry_data *)(resp + 1);
298 for (i = 0; i < resp->nentries; i++, fle6++) {
299 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
300 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
301 printf(CISCO_SH_FLOW6,
302 if_indextoname(fle6->fle_i_ifx, src_if),
304 if_indextoname(fle6->fle_o_ifx, dst_if),
307 ntohs(fle6->r.r_sport),
308 ntohs(fle6->r.r_dport),
317 flow_cache_print_verbose(struct ngnf_show_header *resp)
319 struct flow_entry_data *fle;
320 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
321 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
324 if (resp->version != 4)
325 errx(EX_SOFTWARE, "%s: version mismatch: %u",
326 __func__, resp->version);
328 printf(CISCO_SH_VERB_FLOW_HEADER);
330 fle = (struct flow_entry_data *)(resp + 1);
331 for (i = 0; i < resp->nentries; i++, fle++) {
332 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
333 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
334 inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
335 printf(CISCO_SH_VERB_FLOW,
336 if_indextoname(fle->fle_i_ifx, src_if),
338 if_indextoname(fle->fle_o_ifx, dst_if),
344 ntohs(fle->r.r_sport),
347 ntohs(fle->r.r_dport),
351 (u_int)(fle->bytes / fle->packets),
360 flow_cache_print6_verbose(struct ngnf_show_header *resp)
362 struct flow6_entry_data *fle6;
363 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN], next6[INET6_ADDRSTRLEN];
364 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
367 if (resp->version != 6)
368 errx(EX_SOFTWARE, "%s: version mismatch: %u",
369 __func__, resp->version);
371 printf(CISCO_SH_VERB_FLOW6_HEADER);
373 fle6 = (struct flow6_entry_data *)(resp + 1);
374 for (i = 0; i < resp->nentries; i++, fle6++) {
375 inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
376 inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
377 inet_ntop(AF_INET6, &fle6->n.next_hop6, next6, sizeof(next6));
378 printf(CISCO_SH_VERB_FLOW6,
379 if_indextoname(fle6->fle_i_ifx, src_if),
381 if_indextoname(fle6->fle_o_ifx, dst_if),
387 ntohs(fle6->r.r_sport),
390 ntohs(fle6->r.r_dport),
394 (u_int)(fle6->bytes / fle6->packets),
403 extern char *__progname;
405 fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);