]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmonitor.c
ip: Add label option to ip monitor
[mirror_iproute2.git] / ip / ipmonitor.c
1 /*
2 * ipmonitor.c "ip monitor".
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "utils.h"
25 #include "ip_common.h"
26
27 static void usage(void) __attribute__((noreturn));
28 int prefix_banner;
29
30 static void usage(void)
31 {
32 fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ] [ label ]\n");
33 fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
34 fprintf(stderr, " neigh | netconf\n");
35 fprintf(stderr, "FILE := file FILENAME\n");
36 exit(-1);
37 }
38
39
40 static int accept_msg(const struct sockaddr_nl *who,
41 struct nlmsghdr *n, void *arg)
42 {
43 FILE *fp = (FILE*)arg;
44
45 if (timestamp)
46 print_timestamp(fp);
47
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));
51
52 if (len < 0) {
53 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
54 return -1;
55 }
56
57 if (r->rtm_family == RTNL_FAMILY_IPMR ||
58 r->rtm_family == RTNL_FAMILY_IP6MR) {
59 if (prefix_banner)
60 fprintf(fp, "[MROUTE]");
61 print_mroute(who, n, arg);
62 return 0;
63 } else {
64 if (prefix_banner)
65 fprintf(fp, "[ROUTE]");
66 print_route(who, n, arg);
67 return 0;
68 }
69 }
70 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
71 ll_remember_index(who, n, NULL);
72 if (prefix_banner)
73 fprintf(fp, "[LINK]");
74 print_linkinfo(who, n, arg);
75 return 0;
76 }
77 if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
78 if (prefix_banner)
79 fprintf(fp, "[ADDR]");
80 print_addrinfo(who, n, arg);
81 return 0;
82 }
83 if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
84 if (prefix_banner)
85 fprintf(fp, "[ADDRLABEL]");
86 print_addrlabel(who, n, arg);
87 return 0;
88 }
89 if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH ||
90 n->nlmsg_type == RTM_GETNEIGH) {
91 if (prefix_banner)
92 fprintf(fp, "[NEIGH]");
93 print_neigh(who, n, arg);
94 return 0;
95 }
96 if (n->nlmsg_type == RTM_NEWPREFIX) {
97 if (prefix_banner)
98 fprintf(fp, "[PREFIX]");
99 print_prefix(who, n, arg);
100 return 0;
101 }
102 if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
103 if (prefix_banner)
104 fprintf(fp, "[RULE]");
105 print_rule(who, n, arg);
106 return 0;
107 }
108 if (n->nlmsg_type == RTM_NEWNETCONF) {
109 if (prefix_banner)
110 fprintf(fp, "[NETCONF]");
111 print_netconf(who, n, arg);
112 return 0;
113 }
114 if (n->nlmsg_type == 15) {
115 char *tstr;
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);
121 return 0;
122 }
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)
129 return 0;
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);
134 }
135 return 0;
136 }
137
138 int do_ipmonitor(int argc, char **argv)
139 {
140 char *file = NULL;
141 unsigned groups = ~RTMGRP_TC;
142 int llink=0;
143 int laddr=0;
144 int lroute=0;
145 int lmroute=0;
146 int lprefix=0;
147 int lneigh=0;
148 int lnetconf=0;
149
150 rtnl_close(&rth);
151 ipaddr_reset_filter(1);
152 iproute_reset_filter();
153 ipmroute_reset_filter();
154 ipneigh_reset_filter();
155
156 while (argc > 0) {
157 if (matches(*argv, "file") == 0) {
158 NEXT_ARG();
159 file = *argv;
160 } else if (matches(*argv, "label") == 0) {
161 prefix_banner = 1;
162 } else if (matches(*argv, "link") == 0) {
163 llink=1;
164 groups = 0;
165 } else if (matches(*argv, "address") == 0) {
166 laddr=1;
167 groups = 0;
168 } else if (matches(*argv, "route") == 0) {
169 lroute=1;
170 groups = 0;
171 } else if (matches(*argv, "mroute") == 0) {
172 lmroute=1;
173 groups = 0;
174 } else if (matches(*argv, "prefix") == 0) {
175 lprefix=1;
176 groups = 0;
177 } else if (matches(*argv, "neigh") == 0) {
178 lneigh = 1;
179 groups = 0;
180 } else if (matches(*argv, "netconf") == 0) {
181 lnetconf = 1;
182 groups = 0;
183 } else if (strcmp(*argv, "all") == 0) {
184 groups = ~RTMGRP_TC;
185 prefix_banner=1;
186 } else if (matches(*argv, "help") == 0) {
187 usage();
188 } else {
189 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
190 exit(-1);
191 }
192 argc--; argv++;
193 }
194
195 if (llink)
196 groups |= nl_mgrp(RTNLGRP_LINK);
197 if (laddr) {
198 if (!preferred_family || preferred_family == AF_INET)
199 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
200 if (!preferred_family || preferred_family == AF_INET6)
201 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
202 }
203 if (lroute) {
204 if (!preferred_family || preferred_family == AF_INET)
205 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
206 if (!preferred_family || preferred_family == AF_INET6)
207 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
208 }
209 if (lmroute) {
210 if (!preferred_family || preferred_family == AF_INET)
211 groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE);
212 if (!preferred_family || preferred_family == AF_INET6)
213 groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE);
214 }
215 if (lprefix) {
216 if (!preferred_family || preferred_family == AF_INET6)
217 groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
218 }
219 if (lneigh) {
220 groups |= nl_mgrp(RTNLGRP_NEIGH);
221 }
222 if (lnetconf) {
223 if (!preferred_family || preferred_family == AF_INET)
224 groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF);
225 if (!preferred_family || preferred_family == AF_INET6)
226 groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
227 }
228 if (file) {
229 FILE *fp;
230 fp = fopen(file, "r");
231 if (fp == NULL) {
232 perror("Cannot fopen");
233 exit(-1);
234 }
235 return rtnl_from_file(fp, accept_msg, stdout);
236 }
237
238 if (rtnl_open(&rth, groups) < 0)
239 exit(1);
240 ll_init_map(&rth);
241
242 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
243 exit(2);
244
245 return 0;
246 }