1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/link.h>
8 #include <netlink/cli/mdb.h>
9
10 #include <linux/rtnetlink.h>
11
12 static const struct {
13 enum rtnetlink_groups gr_id;
14 const char* gr_name;
15 } known_groups[] = {
16 { RTNLGRP_LINK, "link" },
17 { RTNLGRP_NOTIFY, "notify" },
18 { RTNLGRP_NEIGH, "neigh" },
19 { RTNLGRP_TC, "tc" },
20 { RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
21 { RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
22 { RTNLGRP_IPV4_ROUTE, "ipv4-route" },
23 { RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
24 { RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
25 { RTNLGRP_IPV6_ROUTE, "ipv6-route" },
26 { RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
27 { RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
28 { RTNLGRP_DECnet_ROUTE, "decnet-route" },
29 { RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
30 { RTNLGRP_IPV4_NETCONF, "ipv4-netconf" },
31 { RTNLGRP_IPV6_NETCONF, "ipv6-netconf" },
32 { RTNLGRP_MPLS_NETCONF, "mpls-netconf" },
33 { RTNLGRP_MDB, "mdb" },
34 { RTNLGRP_NONE, NULL }
35 };
36
obj_input(struct nl_object * obj,void * arg)37 static void obj_input(struct nl_object *obj, void *arg)
38 {
39 nl_object_dump(obj, arg);
40 }
41
event_input(struct nl_msg * msg,void * arg)42 static int event_input(struct nl_msg *msg, void *arg)
43 {
44 if (nl_msg_parse(msg, &obj_input, arg) < 0)
45 fprintf(stderr, "<<EVENT>> Unknown message type\n");
46
47 /* Exit nl_recvmsgs_def() and return to the main select() */
48 return NL_STOP;
49 }
50
print_usage(void)51 static void print_usage(void)
52 {
53 int i;
54
55 printf(
56 "Usage: nl-monitor [OPTION] [<groups>]\n"
57 "\n"
58 "Options\n"
59 " -d, --debug=LEVEL Set libnl debug level { 0 - 7 }\n"
60 " -f, --format=TYPE Output format { brief | details | stats }\n"
61 " -h, --help Show this help.\n"
62 "\n"
63 );
64 printf("Known groups:");
65 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
66 printf(" %s", known_groups[i].gr_name);
67 printf("\n");
68 exit(0);
69 }
70
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73 struct nl_dump_params dp = {
74 .dp_type = NL_DUMP_STATS,
75 .dp_fd = stdout,
76 .dp_dump_msgtype = 1,
77 };
78
79 struct nl_sock *sock;
80 int err = 1;
81 int i, idx;
82
83 sock = nl_cli_alloc_socket();
84 nl_socket_disable_seq_check(sock);
85 nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, &dp);
86
87 for (;;) {
88 int c, optidx = 0;
89 static struct option long_opts[] = {
90 { "debug", 1, 0, 'd' },
91 { "format", 1, 0, 'f' },
92 { "help", 0, 0, 'h' },
93 { 0, 0, 0, 0 }
94 };
95
96 c = getopt_long(argc, argv, "d:f:h", long_opts, &optidx);
97 if (c == -1)
98 break;
99
100 switch (c) {
101 case 'd':
102 nl_debug = atoi(optarg);
103 break;
104 case 'f':
105 dp.dp_type = nl_cli_parse_dumptype(optarg);
106 break;
107 default:
108 print_usage();
109 break;
110 }
111 }
112
113 nl_cli_connect(sock, NETLINK_ROUTE);
114
115 for (idx = optind; argc > idx; idx++) {
116 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
117 if (!strcmp(argv[idx], known_groups[i].gr_name)) {
118
119 if ((err = nl_socket_add_membership(sock, known_groups[i].gr_id)) < 0) {
120 nl_cli_fatal(err, "%s: %s\n", argv[idx],
121 nl_geterror(err));
122 }
123
124 break;
125 }
126 }
127 if (known_groups[i].gr_id == RTNLGRP_NONE)
128 fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
129 }
130
131 nl_cli_link_alloc_cache(sock);
132
133 while (1) {
134 fd_set rfds;
135 int fd, retval;
136
137 fd = nl_socket_get_fd(sock);
138
139 FD_ZERO(&rfds);
140 FD_SET(fd, &rfds);
141 /* wait for an incoming message on the netlink socket */
142 retval = select(fd+1, &rfds, NULL, NULL, NULL);
143
144 if (retval) {
145 /* FD_ISSET(fd, &rfds) will be true */
146 nl_recvmsgs_default(sock);
147 }
148 }
149
150 return 0;
151 }
152