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