]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmonitor.c
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 ] [ FILE ]\n");
33 fprintf(stderr
, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
34 fprintf(stderr
, " neigh | netconf\n");
35 fprintf(stderr
, "FILE := file FILENAME\n");
40 static int accept_msg(const struct sockaddr_nl
*who
,
41 struct nlmsghdr
*n
, void *arg
)
43 FILE *fp
= (FILE*)arg
;
48 if (n
->nlmsg_type
== RTM_NEWROUTE
|| n
->nlmsg_type
== RTM_DELROUTE
) {
49 struct rtmsg
*r
= NLMSG_DATA(n
);
50 int len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
53 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
57 if (r
->rtm_family
== RTNL_FAMILY_IPMR
||
58 r
->rtm_family
== RTNL_FAMILY_IP6MR
) {
60 fprintf(fp
, "[MROUTE]");
61 print_mroute(who
, n
, arg
);
65 fprintf(fp
, "[ROUTE]");
66 print_route(who
, n
, arg
);
70 if (n
->nlmsg_type
== RTM_NEWLINK
|| n
->nlmsg_type
== RTM_DELLINK
) {
71 ll_remember_index(who
, n
, NULL
);
73 fprintf(fp
, "[LINK]");
74 print_linkinfo(who
, n
, arg
);
77 if (n
->nlmsg_type
== RTM_NEWADDR
|| n
->nlmsg_type
== RTM_DELADDR
) {
79 fprintf(fp
, "[ADDR]");
80 print_addrinfo(who
, n
, arg
);
83 if (n
->nlmsg_type
== RTM_NEWADDRLABEL
|| n
->nlmsg_type
== RTM_DELADDRLABEL
) {
85 fprintf(fp
, "[ADDRLABEL]");
86 print_addrlabel(who
, n
, arg
);
89 if (n
->nlmsg_type
== RTM_NEWNEIGH
|| n
->nlmsg_type
== RTM_DELNEIGH
||
90 n
->nlmsg_type
== RTM_GETNEIGH
) {
92 fprintf(fp
, "[NEIGH]");
93 print_neigh(who
, n
, arg
);
96 if (n
->nlmsg_type
== RTM_NEWPREFIX
) {
98 fprintf(fp
, "[PREFIX]");
99 print_prefix(who
, n
, arg
);
102 if (n
->nlmsg_type
== RTM_NEWRULE
|| n
->nlmsg_type
== RTM_DELRULE
) {
104 fprintf(fp
, "[RULE]");
105 print_rule(who
, n
, arg
);
108 if (n
->nlmsg_type
== RTM_NEWNETCONF
) {
110 fprintf(fp
, "[NETCONF]");
111 print_netconf(who
, n
, arg
);
114 if (n
->nlmsg_type
== 15) {
116 time_t secs
= ((__u32
*)NLMSG_DATA(n
))[0];
117 long usecs
= ((__u32
*)NLMSG_DATA(n
))[1];
118 tstr
= asctime(localtime(&secs
));
119 tstr
[strlen(tstr
)-1] = 0;
120 fprintf(fp
, "Timestamp: %s %lu us\n", tstr
, usecs
);
123 if (n
->nlmsg_type
== RTM_NEWQDISC
||
124 n
->nlmsg_type
== RTM_DELQDISC
||
125 n
->nlmsg_type
== RTM_NEWTCLASS
||
126 n
->nlmsg_type
== RTM_DELTCLASS
||
127 n
->nlmsg_type
== RTM_NEWTFILTER
||
128 n
->nlmsg_type
== RTM_DELTFILTER
)
130 if (n
->nlmsg_type
!= NLMSG_ERROR
&& n
->nlmsg_type
!= NLMSG_NOOP
&&
131 n
->nlmsg_type
!= NLMSG_DONE
) {
132 fprintf(fp
, "Unknown message: %08x %08x %08x\n",
133 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
138 int do_ipmonitor(int argc
, char **argv
)
141 unsigned groups
= ~RTMGRP_TC
;
151 ipaddr_reset_filter(1);
152 iproute_reset_filter();
153 ipmroute_reset_filter();
154 ipneigh_reset_filter();
157 if (matches(*argv
, "file") == 0) {
160 } else if (matches(*argv
, "link") == 0) {
163 } else if (matches(*argv
, "address") == 0) {
166 } else if (matches(*argv
, "route") == 0) {
169 } else if (matches(*argv
, "mroute") == 0) {
172 } else if (matches(*argv
, "prefix") == 0) {
175 } else if (matches(*argv
, "neigh") == 0) {
178 } else if (matches(*argv
, "netconf") == 0) {
181 } else if (strcmp(*argv
, "all") == 0) {
184 } else if (matches(*argv
, "help") == 0) {
187 fprintf(stderr
, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv
);
194 groups
|= nl_mgrp(RTNLGRP_LINK
);
196 if (!preferred_family
|| preferred_family
== AF_INET
)
197 groups
|= nl_mgrp(RTNLGRP_IPV4_IFADDR
);
198 if (!preferred_family
|| preferred_family
== AF_INET6
)
199 groups
|= nl_mgrp(RTNLGRP_IPV6_IFADDR
);
202 if (!preferred_family
|| preferred_family
== AF_INET
)
203 groups
|= nl_mgrp(RTNLGRP_IPV4_ROUTE
);
204 if (!preferred_family
|| preferred_family
== AF_INET6
)
205 groups
|= nl_mgrp(RTNLGRP_IPV6_ROUTE
);
208 if (!preferred_family
|| preferred_family
== AF_INET
)
209 groups
|= nl_mgrp(RTNLGRP_IPV4_MROUTE
);
210 if (!preferred_family
|| preferred_family
== AF_INET6
)
211 groups
|= nl_mgrp(RTNLGRP_IPV6_MROUTE
);
214 if (!preferred_family
|| preferred_family
== AF_INET6
)
215 groups
|= nl_mgrp(RTNLGRP_IPV6_PREFIX
);
218 groups
|= nl_mgrp(RTNLGRP_NEIGH
);
221 if (!preferred_family
|| preferred_family
== AF_INET
)
222 groups
|= nl_mgrp(RTNLGRP_IPV4_NETCONF
);
223 if (!preferred_family
|| preferred_family
== AF_INET6
)
224 groups
|= nl_mgrp(RTNLGRP_IPV6_NETCONF
);
228 fp
= fopen(file
, "r");
230 perror("Cannot fopen");
233 return rtnl_from_file(fp
, accept_msg
, stdout
);
236 if (rtnl_open(&rth
, groups
) < 0)
240 if (rtnl_listen(&rth
, accept_msg
, stdout
) < 0)