15f99200fSGleb Smirnoff /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
49672556cSGleb Smirnoff * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
55f99200fSGleb Smirnoff * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
65f99200fSGleb Smirnoff * All rights reserved.
75f99200fSGleb Smirnoff *
85f99200fSGleb Smirnoff * Redistribution and use in source and binary forms, with or without
95f99200fSGleb Smirnoff * modification, are permitted provided that the following conditions
105f99200fSGleb Smirnoff * are met:
115f99200fSGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
125f99200fSGleb Smirnoff * notice, this list of conditions and the following disclaimer.
135f99200fSGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
145f99200fSGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
155f99200fSGleb Smirnoff * documentation and/or other materials provided with the distribution.
165f99200fSGleb Smirnoff *
175f99200fSGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
185f99200fSGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
195f99200fSGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205f99200fSGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
215f99200fSGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
225f99200fSGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
235f99200fSGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
245f99200fSGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
255f99200fSGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
265f99200fSGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
275f99200fSGleb Smirnoff * SUCH DAMAGE.
285f99200fSGleb Smirnoff *
295f99200fSGleb Smirnoff * $SourceForge: flowctl.c,v 1.15 2004/08/31 20:24:58 glebius Exp $
305f99200fSGleb Smirnoff */
315f99200fSGleb Smirnoff
325f99200fSGleb Smirnoff #include <sys/types.h>
335f99200fSGleb Smirnoff #include <sys/time.h>
345f99200fSGleb Smirnoff #include <sys/socket.h>
355f99200fSGleb Smirnoff #include <sys/queue.h>
365f99200fSGleb Smirnoff
375f99200fSGleb Smirnoff #include <net/if.h>
385f99200fSGleb Smirnoff #include <netinet/in.h>
395f99200fSGleb Smirnoff
405f99200fSGleb Smirnoff #include <arpa/inet.h>
415f99200fSGleb Smirnoff
425f99200fSGleb Smirnoff #include <err.h>
435f99200fSGleb Smirnoff #include <stdio.h>
445f99200fSGleb Smirnoff #include <stdlib.h>
455f99200fSGleb Smirnoff #include <string.h>
4637b425e9SGleb Smirnoff #include <sysexits.h>
475f99200fSGleb Smirnoff #include <unistd.h>
485f99200fSGleb Smirnoff
495f99200fSGleb Smirnoff #include <netgraph.h>
505f99200fSGleb Smirnoff #include <netgraph/netflow/ng_netflow.h>
515f99200fSGleb Smirnoff
523097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW_HEADER "SrcIf SrcIPaddress " \
533097b95aSAlexander V. Chernikov "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
545f99200fSGleb Smirnoff #define CISCO_SH_FLOW "%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
555f99200fSGleb Smirnoff
563097b95aSAlexander V. Chernikov /* human-readable IPv4 header */
573097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW_HHEADER "SrcIf SrcIPaddress " \
583097b95aSAlexander V. Chernikov "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
593097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW_H "%-13s %-15s %-13s %-15s %5u %8d %8d %8lu\n"
603097b95aSAlexander V. Chernikov
613097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW6_HEADER "SrcIf SrcIPaddress " \
623097b95aSAlexander V. Chernikov "DstIf DstIPaddress Pr SrcP DstP Pkts\n"
6337b425e9SGleb Smirnoff #define CISCO_SH_FLOW6 "%-13s %-30s %-13s %-30s %2u %4.4x %4.4x %6lu\n"
6437b425e9SGleb Smirnoff
653097b95aSAlexander V. Chernikov /* Human-readable IPv6 headers */
663097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW6_HHEADER "SrcIf SrcIPaddress " \
673097b95aSAlexander V. Chernikov "DstIf DstIPaddress Proto SrcPort DstPort Pkts\n"
683097b95aSAlexander V. Chernikov #define CISCO_SH_FLOW6_H "%-13s %-36s %-13s %-36s %5u %8d %8d %8lu\n"
693097b95aSAlexander V. Chernikov
703097b95aSAlexander V. Chernikov #define CISCO_SH_VERB_FLOW_HEADER "SrcIf SrcIPaddress " \
713097b95aSAlexander V. Chernikov "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
729672556cSGleb Smirnoff "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
739672556cSGleb Smirnoff
749672556cSGleb Smirnoff #define CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
759672556cSGleb Smirnoff "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
765f99200fSGleb Smirnoff
773097b95aSAlexander V. Chernikov #define CISCO_SH_VERB_FLOW6_HEADER "SrcIf SrcIPaddress " \
783097b95aSAlexander V. Chernikov "DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
7937b425e9SGleb Smirnoff "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
8037b425e9SGleb Smirnoff
8137b425e9SGleb Smirnoff #define CISCO_SH_VERB_FLOW6 "%-14s %-30s %-14s %-30s %2u %3x %4x %6lu\n" \
8237b425e9SGleb Smirnoff "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-30s %9u %8u\n\n"
836765aefcSSergey Kandaurov #ifdef INET
8437b425e9SGleb Smirnoff static void flow_cache_print(struct ngnf_show_header *resp);
8537b425e9SGleb Smirnoff static void flow_cache_print_verbose(struct ngnf_show_header *resp);
866765aefcSSergey Kandaurov #endif
876765aefcSSergey Kandaurov #ifdef INET6
886765aefcSSergey Kandaurov static void flow_cache_print6(struct ngnf_show_header *resp);
8937b425e9SGleb Smirnoff static void flow_cache_print6_verbose(struct ngnf_show_header *resp);
906765aefcSSergey Kandaurov #endif
9137b425e9SGleb Smirnoff static void ctl_show(int, char **);
926765aefcSSergey Kandaurov #if defined(INET) || defined(INET6)
9337b425e9SGleb Smirnoff static void do_show(int, void (*func)(struct ngnf_show_header *));
946765aefcSSergey Kandaurov #endif
955f99200fSGleb Smirnoff static void help(void);
965f99200fSGleb Smirnoff static void execute_command(int, char **);
975f99200fSGleb Smirnoff
985f99200fSGleb Smirnoff struct ip_ctl_cmd {
995f99200fSGleb Smirnoff char *cmd_name;
10037b425e9SGleb Smirnoff void (*cmd_func)(int argc, char **argv);
1015f99200fSGleb Smirnoff };
1025f99200fSGleb Smirnoff
1035f99200fSGleb Smirnoff struct ip_ctl_cmd cmds[] = {
104f0f77f69SGleb Smirnoff {"show", ctl_show},
105f0f77f69SGleb Smirnoff {NULL, NULL},
1065f99200fSGleb Smirnoff };
1075f99200fSGleb Smirnoff
1083097b95aSAlexander V. Chernikov int cs, human = 0;
10937b425e9SGleb Smirnoff char *ng_path;
1105f99200fSGleb Smirnoff
1115f99200fSGleb Smirnoff int
main(int argc,char ** argv)1125f99200fSGleb Smirnoff main(int argc, char **argv)
1135f99200fSGleb Smirnoff {
114c41ba5f0SGleb Smirnoff int c;
1155f99200fSGleb Smirnoff char sname[NG_NODESIZ];
1165f99200fSGleb Smirnoff int rcvbuf = SORCVBUF_SIZE;
1175f99200fSGleb Smirnoff
1185f99200fSGleb Smirnoff /* parse options */
1195f99200fSGleb Smirnoff while ((c = getopt(argc, argv, "d:")) != -1) {
1205f99200fSGleb Smirnoff switch (c) {
1215f99200fSGleb Smirnoff case 'd': /* set libnetgraph debug level. */
1225f99200fSGleb Smirnoff NgSetDebug(atoi(optarg));
1235f99200fSGleb Smirnoff break;
1245f99200fSGleb Smirnoff }
1255f99200fSGleb Smirnoff }
1265f99200fSGleb Smirnoff
1275f99200fSGleb Smirnoff argc -= optind;
1285f99200fSGleb Smirnoff argv += optind;
12937b425e9SGleb Smirnoff ng_path = argv[0];
13037b425e9SGleb Smirnoff if (ng_path == NULL || (strlen(ng_path) > NG_PATHSIZ))
1315f99200fSGleb Smirnoff help();
1325f99200fSGleb Smirnoff argc--;
1335f99200fSGleb Smirnoff argv++;
1345f99200fSGleb Smirnoff
1355f99200fSGleb Smirnoff /* create control socket. */
1365f99200fSGleb Smirnoff snprintf(sname, sizeof(sname), "flowctl%i", getpid());
1375f99200fSGleb Smirnoff
1385f99200fSGleb Smirnoff if (NgMkSockNode(sname, &cs, NULL) == -1)
1395f99200fSGleb Smirnoff err(1, "NgMkSockNode");
1405f99200fSGleb Smirnoff
1415f99200fSGleb Smirnoff /* set receive buffer size */
1425f99200fSGleb Smirnoff if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
1435f99200fSGleb Smirnoff err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
1445f99200fSGleb Smirnoff
1455f99200fSGleb Smirnoff /* parse and execute command */
1465f99200fSGleb Smirnoff execute_command(argc, argv);
1475f99200fSGleb Smirnoff
1485f99200fSGleb Smirnoff close(cs);
1495f99200fSGleb Smirnoff
1505f99200fSGleb Smirnoff exit(0);
1515f99200fSGleb Smirnoff }
1525f99200fSGleb Smirnoff
1535f99200fSGleb Smirnoff static void
execute_command(int argc,char ** argv)1545f99200fSGleb Smirnoff execute_command(int argc, char **argv)
1555f99200fSGleb Smirnoff {
1565f99200fSGleb Smirnoff int cindex = -1;
1575f99200fSGleb Smirnoff int i;
1585f99200fSGleb Smirnoff
1595f99200fSGleb Smirnoff if (!argc)
1605f99200fSGleb Smirnoff help();
1615f99200fSGleb Smirnoff for (i = 0; cmds[i].cmd_name != NULL; i++)
1625f99200fSGleb Smirnoff if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
1635f99200fSGleb Smirnoff if (cindex != -1)
1645f99200fSGleb Smirnoff errx(1, "ambiguous command: %s", argv[0]);
1655f99200fSGleb Smirnoff cindex = i;
1665f99200fSGleb Smirnoff }
1675f99200fSGleb Smirnoff if (cindex == -1)
1685f99200fSGleb Smirnoff errx(1, "bad command: %s", argv[0]);
1695f99200fSGleb Smirnoff argc--;
1705f99200fSGleb Smirnoff argv++;
171f0f77f69SGleb Smirnoff (*cmds[cindex].cmd_func)(argc, argv);
1725f99200fSGleb Smirnoff }
1735f99200fSGleb Smirnoff
17437b425e9SGleb Smirnoff static void
ctl_show(int argc,char ** argv)175f0f77f69SGleb Smirnoff ctl_show(int argc, char **argv)
1765f99200fSGleb Smirnoff {
1776765aefcSSergey Kandaurov int ipv4, ipv6, verbose = 0;
1786765aefcSSergey Kandaurov
1796765aefcSSergey Kandaurov ipv4 = feature_present("inet");
1806765aefcSSergey Kandaurov ipv6 = feature_present("inet6");
18137b425e9SGleb Smirnoff
18237b425e9SGleb Smirnoff if (argc > 0 && !strncmp(argv[0], "ipv4", 4)) {
18337b425e9SGleb Smirnoff ipv6 = 0;
18437b425e9SGleb Smirnoff argc--;
18537b425e9SGleb Smirnoff argv++;
18637b425e9SGleb Smirnoff }
18737b425e9SGleb Smirnoff if (argc > 0 && !strncmp(argv[0], "ipv6", 4)) {
18837b425e9SGleb Smirnoff ipv4 = 0;
18937b425e9SGleb Smirnoff argc--;
19037b425e9SGleb Smirnoff argv++;
19137b425e9SGleb Smirnoff }
1929672556cSGleb Smirnoff
1939672556cSGleb Smirnoff if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
1949672556cSGleb Smirnoff verbose = 1;
1955f99200fSGleb Smirnoff
1963097b95aSAlexander V. Chernikov if (argc > 0 && !strncmp(argv[0], "human", strlen(argv[0])))
1973097b95aSAlexander V. Chernikov human = 1;
1983097b95aSAlexander V. Chernikov
1996765aefcSSergey Kandaurov #ifdef INET
20037b425e9SGleb Smirnoff if (ipv4) {
20137b425e9SGleb Smirnoff if (verbose)
20237b425e9SGleb Smirnoff do_show(4, &flow_cache_print_verbose);
20337b425e9SGleb Smirnoff else
20437b425e9SGleb Smirnoff do_show(4, &flow_cache_print);
20537b425e9SGleb Smirnoff }
2066765aefcSSergey Kandaurov #endif
20737b425e9SGleb Smirnoff
2086765aefcSSergey Kandaurov #ifdef INET6
20937b425e9SGleb Smirnoff if (ipv6) {
21037b425e9SGleb Smirnoff if (verbose)
21137b425e9SGleb Smirnoff do_show(6, &flow_cache_print6_verbose);
21237b425e9SGleb Smirnoff else
21337b425e9SGleb Smirnoff do_show(6, &flow_cache_print6);
21437b425e9SGleb Smirnoff }
2156765aefcSSergey Kandaurov #endif
21637b425e9SGleb Smirnoff }
21737b425e9SGleb Smirnoff
2186765aefcSSergey Kandaurov #if defined(INET) || defined(INET6)
21937b425e9SGleb Smirnoff static void
do_show(int version,void (* func)(struct ngnf_show_header *))22037b425e9SGleb Smirnoff do_show(int version, void (*func)(struct ngnf_show_header *))
22137b425e9SGleb Smirnoff {
2222698a8a5SGleb Smirnoff char buf[SORCVBUF_SIZE];
2232698a8a5SGleb Smirnoff struct ng_mesg *ng_mesg;
22437b425e9SGleb Smirnoff struct ngnf_show_header req, *resp;
22537b425e9SGleb Smirnoff int token, nread;
22637b425e9SGleb Smirnoff
2272698a8a5SGleb Smirnoff ng_mesg = (struct ng_mesg *)buf;
22837b425e9SGleb Smirnoff req.version = version;
22937b425e9SGleb Smirnoff req.hash_id = req.list_id = 0;
2305f99200fSGleb Smirnoff
2315f99200fSGleb Smirnoff for (;;) {
2325f99200fSGleb Smirnoff /* request set of accounting records */
23337b425e9SGleb Smirnoff token = NgSendMsg(cs, ng_path, NGM_NETFLOW_COOKIE,
23437b425e9SGleb Smirnoff NGM_NETFLOW_SHOW, (void *)&req, sizeof(req));
2355f99200fSGleb Smirnoff if (token == -1)
2365f99200fSGleb Smirnoff err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
2375f99200fSGleb Smirnoff
2385f99200fSGleb Smirnoff /* read reply */
23937b425e9SGleb Smirnoff nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, NULL);
2405f99200fSGleb Smirnoff if (nread == -1)
2415f99200fSGleb Smirnoff err(1, "NgRecvMsg() failed");
2425f99200fSGleb Smirnoff
2435f99200fSGleb Smirnoff if (ng_mesg->header.token != token)
2445f99200fSGleb Smirnoff err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
2455f99200fSGleb Smirnoff
24637b425e9SGleb Smirnoff resp = (struct ngnf_show_header *)ng_mesg->data;
24737b425e9SGleb Smirnoff if ((ng_mesg->header.arglen < (sizeof(*resp))) ||
24837b425e9SGleb Smirnoff (ng_mesg->header.arglen < (sizeof(*resp) +
24937b425e9SGleb Smirnoff (resp->nentries * sizeof(struct flow_entry_data)))))
2505f99200fSGleb Smirnoff err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
2515f99200fSGleb Smirnoff
25237b425e9SGleb Smirnoff (*func)(resp);
2535f99200fSGleb Smirnoff
25437b425e9SGleb Smirnoff if (resp->hash_id != 0)
25537b425e9SGleb Smirnoff req.hash_id = resp->hash_id;
2565f99200fSGleb Smirnoff else
2575f99200fSGleb Smirnoff break;
25837b425e9SGleb Smirnoff req.list_id = resp->list_id;
25937b425e9SGleb Smirnoff }
2605f99200fSGleb Smirnoff }
2616765aefcSSergey Kandaurov #endif
2625f99200fSGleb Smirnoff
2636765aefcSSergey Kandaurov #ifdef INET
26437b425e9SGleb Smirnoff static void
flow_cache_print(struct ngnf_show_header * resp)26537b425e9SGleb Smirnoff flow_cache_print(struct ngnf_show_header *resp)
2665f99200fSGleb Smirnoff {
2675f99200fSGleb Smirnoff struct flow_entry_data *fle;
2685f99200fSGleb Smirnoff char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
2695f99200fSGleb Smirnoff char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
2705f99200fSGleb Smirnoff int i;
2715f99200fSGleb Smirnoff
27237b425e9SGleb Smirnoff if (resp->version != 4)
27337b425e9SGleb Smirnoff errx(EX_SOFTWARE, "%s: version mismatch: %u",
27437b425e9SGleb Smirnoff __func__, resp->version);
2755f99200fSGleb Smirnoff
2763097b95aSAlexander V. Chernikov if (resp->nentries > 0)
2773097b95aSAlexander V. Chernikov printf(human ? CISCO_SH_FLOW_HHEADER : CISCO_SH_FLOW_HEADER);
27837b425e9SGleb Smirnoff
27937b425e9SGleb Smirnoff fle = (struct flow_entry_data *)(resp + 1);
28037b425e9SGleb Smirnoff for (i = 0; i < resp->nentries; i++, fle++) {
2815f99200fSGleb Smirnoff inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
2825f99200fSGleb Smirnoff inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
2833097b95aSAlexander V. Chernikov printf(human ? CISCO_SH_FLOW_H : CISCO_SH_FLOW,
2845f99200fSGleb Smirnoff if_indextoname(fle->fle_i_ifx, src_if),
2855f99200fSGleb Smirnoff src,
2865f99200fSGleb Smirnoff if_indextoname(fle->fle_o_ifx, dst_if),
2875f99200fSGleb Smirnoff dst,
2885f99200fSGleb Smirnoff fle->r.r_ip_p,
2895f99200fSGleb Smirnoff ntohs(fle->r.r_sport),
2905f99200fSGleb Smirnoff ntohs(fle->r.r_dport),
2915f99200fSGleb Smirnoff fle->packets);
2925f99200fSGleb Smirnoff
2935f99200fSGleb Smirnoff }
2945f99200fSGleb Smirnoff }
2956765aefcSSergey Kandaurov #endif
2965f99200fSGleb Smirnoff
29737b425e9SGleb Smirnoff #ifdef INET6
29837b425e9SGleb Smirnoff static void
flow_cache_print6(struct ngnf_show_header * resp)29937b425e9SGleb Smirnoff flow_cache_print6(struct ngnf_show_header *resp)
30037b425e9SGleb Smirnoff {
30137b425e9SGleb Smirnoff struct flow6_entry_data *fle6;
30237b425e9SGleb Smirnoff char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
30337b425e9SGleb Smirnoff char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
30437b425e9SGleb Smirnoff int i;
30537b425e9SGleb Smirnoff
30637b425e9SGleb Smirnoff if (resp->version != 6)
30737b425e9SGleb Smirnoff errx(EX_SOFTWARE, "%s: version mismatch: %u",
30837b425e9SGleb Smirnoff __func__, resp->version);
30937b425e9SGleb Smirnoff
3103097b95aSAlexander V. Chernikov if (resp->nentries > 0)
3113097b95aSAlexander V. Chernikov printf(human ? CISCO_SH_FLOW6_HHEADER : CISCO_SH_FLOW6_HEADER);
31237b425e9SGleb Smirnoff
31337b425e9SGleb Smirnoff fle6 = (struct flow6_entry_data *)(resp + 1);
31437b425e9SGleb Smirnoff for (i = 0; i < resp->nentries; i++, fle6++) {
31537b425e9SGleb Smirnoff inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
31637b425e9SGleb Smirnoff inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
3173097b95aSAlexander V. Chernikov printf(human ? CISCO_SH_FLOW6_H : CISCO_SH_FLOW6,
31837b425e9SGleb Smirnoff if_indextoname(fle6->fle_i_ifx, src_if),
31937b425e9SGleb Smirnoff src6,
32037b425e9SGleb Smirnoff if_indextoname(fle6->fle_o_ifx, dst_if),
32137b425e9SGleb Smirnoff dst6,
32237b425e9SGleb Smirnoff fle6->r.r_ip_p,
32337b425e9SGleb Smirnoff ntohs(fle6->r.r_sport),
32437b425e9SGleb Smirnoff ntohs(fle6->r.r_dport),
32537b425e9SGleb Smirnoff fle6->packets);
32637b425e9SGleb Smirnoff
32737b425e9SGleb Smirnoff }
32837b425e9SGleb Smirnoff }
32937b425e9SGleb Smirnoff #endif
33037b425e9SGleb Smirnoff
3316765aefcSSergey Kandaurov #ifdef INET
33237b425e9SGleb Smirnoff static void
flow_cache_print_verbose(struct ngnf_show_header * resp)33337b425e9SGleb Smirnoff flow_cache_print_verbose(struct ngnf_show_header *resp)
3349672556cSGleb Smirnoff {
3359672556cSGleb Smirnoff struct flow_entry_data *fle;
3369672556cSGleb Smirnoff char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
3379672556cSGleb Smirnoff char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
3389672556cSGleb Smirnoff int i;
3399672556cSGleb Smirnoff
34037b425e9SGleb Smirnoff if (resp->version != 4)
34137b425e9SGleb Smirnoff errx(EX_SOFTWARE, "%s: version mismatch: %u",
34237b425e9SGleb Smirnoff __func__, resp->version);
3439672556cSGleb Smirnoff
34437b425e9SGleb Smirnoff printf(CISCO_SH_VERB_FLOW_HEADER);
34537b425e9SGleb Smirnoff
34637b425e9SGleb Smirnoff fle = (struct flow_entry_data *)(resp + 1);
34737b425e9SGleb Smirnoff for (i = 0; i < resp->nentries; i++, fle++) {
3489672556cSGleb Smirnoff inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
3499672556cSGleb Smirnoff inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
3509672556cSGleb Smirnoff inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
3519672556cSGleb Smirnoff printf(CISCO_SH_VERB_FLOW,
3529672556cSGleb Smirnoff if_indextoname(fle->fle_i_ifx, src_if),
3539672556cSGleb Smirnoff src,
3549672556cSGleb Smirnoff if_indextoname(fle->fle_o_ifx, dst_if),
3559672556cSGleb Smirnoff dst,
3569672556cSGleb Smirnoff fle->r.r_ip_p,
3579672556cSGleb Smirnoff fle->r.r_tos,
3589672556cSGleb Smirnoff fle->tcp_flags,
3599672556cSGleb Smirnoff fle->packets,
3609672556cSGleb Smirnoff ntohs(fle->r.r_sport),
3619672556cSGleb Smirnoff fle->src_mask,
3629672556cSGleb Smirnoff 0,
3639672556cSGleb Smirnoff ntohs(fle->r.r_dport),
3649672556cSGleb Smirnoff fle->dst_mask,
3659672556cSGleb Smirnoff 0,
3669672556cSGleb Smirnoff next,
3679672556cSGleb Smirnoff (u_int)(fle->bytes / fle->packets),
3689672556cSGleb Smirnoff 0);
3699672556cSGleb Smirnoff
3709672556cSGleb Smirnoff }
3719672556cSGleb Smirnoff }
3726765aefcSSergey Kandaurov #endif
3739672556cSGleb Smirnoff
37437b425e9SGleb Smirnoff #ifdef INET6
37537b425e9SGleb Smirnoff static void
flow_cache_print6_verbose(struct ngnf_show_header * resp)37637b425e9SGleb Smirnoff flow_cache_print6_verbose(struct ngnf_show_header *resp)
37737b425e9SGleb Smirnoff {
37837b425e9SGleb Smirnoff struct flow6_entry_data *fle6;
37937b425e9SGleb Smirnoff char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN], next6[INET6_ADDRSTRLEN];
38037b425e9SGleb Smirnoff char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
38137b425e9SGleb Smirnoff int i;
38237b425e9SGleb Smirnoff
38337b425e9SGleb Smirnoff if (resp->version != 6)
38437b425e9SGleb Smirnoff errx(EX_SOFTWARE, "%s: version mismatch: %u",
38537b425e9SGleb Smirnoff __func__, resp->version);
38637b425e9SGleb Smirnoff
38737b425e9SGleb Smirnoff printf(CISCO_SH_VERB_FLOW6_HEADER);
38837b425e9SGleb Smirnoff
38937b425e9SGleb Smirnoff fle6 = (struct flow6_entry_data *)(resp + 1);
39037b425e9SGleb Smirnoff for (i = 0; i < resp->nentries; i++, fle6++) {
39137b425e9SGleb Smirnoff inet_ntop(AF_INET6, &fle6->r.src.r_src6, src6, sizeof(src6));
39237b425e9SGleb Smirnoff inet_ntop(AF_INET6, &fle6->r.dst.r_dst6, dst6, sizeof(dst6));
39337b425e9SGleb Smirnoff inet_ntop(AF_INET6, &fle6->n.next_hop6, next6, sizeof(next6));
39437b425e9SGleb Smirnoff printf(CISCO_SH_VERB_FLOW6,
39537b425e9SGleb Smirnoff if_indextoname(fle6->fle_i_ifx, src_if),
39637b425e9SGleb Smirnoff src6,
39737b425e9SGleb Smirnoff if_indextoname(fle6->fle_o_ifx, dst_if),
39837b425e9SGleb Smirnoff dst6,
39937b425e9SGleb Smirnoff fle6->r.r_ip_p,
40037b425e9SGleb Smirnoff fle6->r.r_tos,
40137b425e9SGleb Smirnoff fle6->tcp_flags,
40237b425e9SGleb Smirnoff fle6->packets,
40337b425e9SGleb Smirnoff ntohs(fle6->r.r_sport),
40437b425e9SGleb Smirnoff fle6->src_mask,
40537b425e9SGleb Smirnoff 0,
40637b425e9SGleb Smirnoff ntohs(fle6->r.r_dport),
40737b425e9SGleb Smirnoff fle6->dst_mask,
40837b425e9SGleb Smirnoff 0,
40937b425e9SGleb Smirnoff next6,
41037b425e9SGleb Smirnoff (u_int)(fle6->bytes / fle6->packets),
41137b425e9SGleb Smirnoff 0);
41237b425e9SGleb Smirnoff }
41337b425e9SGleb Smirnoff }
41437b425e9SGleb Smirnoff #endif
41537b425e9SGleb Smirnoff
4165f99200fSGleb Smirnoff static void
help(void)4175f99200fSGleb Smirnoff help(void)
4185f99200fSGleb Smirnoff {
4195f99200fSGleb Smirnoff extern char *__progname;
4205f99200fSGleb Smirnoff
4215f99200fSGleb Smirnoff fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);
4225f99200fSGleb Smirnoff exit (0);
4235f99200fSGleb Smirnoff }
424