2 * ipmonitor.c "ip monitor".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 #include "ip_common.h"
27 static void usage(void) __attribute__((noreturn));
30 static void usage(void)
32 fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ]\n");
37 int accept_msg(const struct sockaddr_nl *who,
38 struct nlmsghdr *n, void *arg)
40 FILE *fp = (FILE*)arg;
45 if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
47 fprintf(fp, "[ROUTE]");
48 print_route(who, n, arg);
51 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
52 ll_remember_index(who, n, NULL);
54 fprintf(fp, "[LINK]");
55 print_linkinfo(who, n, arg);
58 if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
60 fprintf(fp, "[ADDR]");
61 print_addrinfo(who, n, arg);
64 if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
66 fprintf(fp, "[ADDRLABEL]");
67 print_addrlabel(who, n, arg);
70 if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH) {
72 fprintf(fp, "[NEIGH]");
73 print_neigh(who, n, arg);
76 if (n->nlmsg_type == RTM_NEWPREFIX) {
78 fprintf(fp, "[PREFIX]");
79 print_prefix(who, n, arg);
82 if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
84 fprintf(fp, "[RULE]");
85 print_rule(who, n, arg);
88 if (n->nlmsg_type == 15) {
90 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
91 long usecs = ((__u32*)NLMSG_DATA(n))[1];
92 tstr = asctime(localtime(&secs));
93 tstr[strlen(tstr)-1] = 0;
94 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
97 if (n->nlmsg_type == RTM_NEWQDISC ||
98 n->nlmsg_type == RTM_DELQDISC ||
99 n->nlmsg_type == RTM_NEWTCLASS ||
100 n->nlmsg_type == RTM_DELTCLASS ||
101 n->nlmsg_type == RTM_NEWTFILTER ||
102 n->nlmsg_type == RTM_DELTFILTER)
104 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
105 n->nlmsg_type != NLMSG_DONE) {
106 fprintf(fp, "Unknown message: %08x %08x %08x\n",
107 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
112 int do_ipmonitor(int argc, char **argv)
115 unsigned groups = ~RTMGRP_TC;
123 ipaddr_reset_filter(1);
124 iproute_reset_filter();
125 ipneigh_reset_filter();
128 if (matches(*argv, "file") == 0) {
131 } else if (matches(*argv, "link") == 0) {
134 } else if (matches(*argv, "address") == 0) {
137 } else if (matches(*argv, "route") == 0) {
140 } else if (matches(*argv, "prefix") == 0) {
143 } else if (matches(*argv, "neigh") == 0) {
146 } else if (strcmp(*argv, "all") == 0) {
149 } else if (matches(*argv, "help") == 0) {
152 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
159 groups |= nl_mgrp(RTNLGRP_LINK);
161 if (!preferred_family || preferred_family == AF_INET)
162 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
163 if (!preferred_family || preferred_family == AF_INET6)
164 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
167 if (!preferred_family || preferred_family == AF_INET)
168 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
169 if (!preferred_family || preferred_family == AF_INET6)
170 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
173 if (!preferred_family || preferred_family == AF_INET6)
174 groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
177 groups |= nl_mgrp(RTNLGRP_NEIGH);
181 fp = fopen(file, "r");
183 perror("Cannot fopen");
186 return rtnl_from_file(fp, accept_msg, stdout);
189 if (rtnl_open(&rth, groups) < 0)
193 if (rtnl_listen(&rth, accept_msg, stdout) < 0)