]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipmonitor.c
Merge branch 'master' into iproute2-next
[mirror_iproute2.git] / ip / ipmonitor.c
CommitLineData
aba5acdf
SH
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>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21#include <time.h>
22
23#include "utils.h"
24#include "ip_common.h"
25
26static void usage(void) __attribute__((noreturn));
4b6e07d8 27int prefix_banner;
449b824a 28int listen_all_nsid;
aba5acdf
SH
29
30static void usage(void)
31{
56f5daac 32 fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ] [ label ] [all-nsid] [dev DEVICE]\n");
cbe195dc 33 fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
d652ccbf 34 fprintf(stderr, " neigh | netconf | rule | nsid\n");
cbe195dc 35 fprintf(stderr, "FILE := file FILENAME\n");
aba5acdf
SH
36 exit(-1);
37}
38
449b824a 39static void print_headers(FILE *fp, char *label, struct rtnl_ctrl_data *ctrl)
3b0006f8
ND
40{
41 if (timestamp)
42 print_timestamp(fp);
43
449b824a
ND
44 if (listen_all_nsid) {
45 if (ctrl == NULL || ctrl->nsid < 0)
46 fprintf(fp, "[nsid current]");
47 else
48 fprintf(fp, "[nsid %d]", ctrl->nsid);
49 }
50
3b0006f8
ND
51 if (prefix_banner)
52 fprintf(fp, "%s", label);
53}
54
d1f28cf1 55static int accept_msg(const struct sockaddr_nl *who,
0628cddd 56 struct rtnl_ctrl_data *ctrl,
d1f28cf1 57 struct nlmsghdr *n, void *arg)
aba5acdf 58{
56f5daac 59 FILE *fp = (FILE *)arg;
aba5acdf 60
40443f49
SH
61 switch (n->nlmsg_type) {
62 case RTM_NEWROUTE:
63 case RTM_DELROUTE: {
e34d3dcc
ND
64 struct rtmsg *r = NLMSG_DATA(n);
65 int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
66
67 if (len < 0) {
68 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
69 return -1;
70 }
71
2271779d 72 if (r->rtm_flags & RTM_F_CLONED)
73 return 0;
74
e34d3dcc
ND
75 if (r->rtm_family == RTNL_FAMILY_IPMR ||
76 r->rtm_family == RTNL_FAMILY_IP6MR) {
449b824a 77 print_headers(fp, "[MROUTE]", ctrl);
e34d3dcc
ND
78 print_mroute(who, n, arg);
79 return 0;
80 } else {
449b824a 81 print_headers(fp, "[ROUTE]", ctrl);
e34d3dcc
ND
82 print_route(who, n, arg);
83 return 0;
84 }
aba5acdf 85 }
2271779d 86
40443f49
SH
87 case RTM_NEWLINK:
88 case RTM_DELLINK:
aba5acdf 89 ll_remember_index(who, n, NULL);
449b824a 90 print_headers(fp, "[LINK]", ctrl);
aba5acdf
SH
91 print_linkinfo(who, n, arg);
92 return 0;
40443f49
SH
93
94 case RTM_NEWADDR:
95 case RTM_DELADDR:
449b824a 96 print_headers(fp, "[ADDR]", ctrl);
aba5acdf
SH
97 print_addrinfo(who, n, arg);
98 return 0;
40443f49
SH
99
100 case RTM_NEWADDRLABEL:
101 case RTM_DELADDRLABEL:
449b824a 102 print_headers(fp, "[ADDRLABEL]", ctrl);
4759758c
YH
103 print_addrlabel(who, n, arg);
104 return 0;
40443f49
SH
105
106 case RTM_NEWNEIGH:
107 case RTM_DELNEIGH:
108 case RTM_GETNEIGH:
00185653
SH
109 if (preferred_family) {
110 struct ndmsg *r = NLMSG_DATA(n);
111
112 if (r->ndm_family != preferred_family)
113 return 0;
114 }
115
449b824a 116 print_headers(fp, "[NEIGH]", ctrl);
aba5acdf
SH
117 print_neigh(who, n, arg);
118 return 0;
40443f49
SH
119
120 case RTM_NEWPREFIX:
449b824a 121 print_headers(fp, "[PREFIX]", ctrl);
1cb54e58 122 print_prefix(who, n, arg);
123 return 0;
40443f49
SH
124
125 case RTM_NEWRULE:
126 case RTM_DELRULE:
449b824a 127 print_headers(fp, "[RULE]", ctrl);
98bde989
TG
128 print_rule(who, n, arg);
129 return 0;
40443f49
SH
130
131 case NLMSG_TSTAMP:
132 print_nlmsg_timestamp(fp, n);
133 return 0;
134
135 case RTM_NEWNETCONF:
e09d21f6 136 case RTM_DELNETCONF:
449b824a 137 print_headers(fp, "[NETCONF]", ctrl);
0628cddd 138 print_netconf(who, ctrl, n, arg);
9d0efc10 139 return 0;
40443f49
SH
140
141 case RTM_DELNSID:
142 case RTM_NEWNSID:
449b824a 143 print_headers(fp, "[NSID]", ctrl);
d652ccbf
ND
144 print_nsid(who, n, arg);
145 return 0;
40443f49
SH
146
147 case NLMSG_ERROR:
148 case NLMSG_NOOP:
149 case NLMSG_DONE:
150 break; /* ignore */
151
152 default:
153 fprintf(stderr,
154 "Unknown message: type=0x%08x(%d) flags=0x%08x(%d) len=0x%08x(%d)\n",
56f5daac 155 n->nlmsg_type, n->nlmsg_type,
40aadf8b 156 n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len,
157 n->nlmsg_len);
aba5acdf
SH
158 }
159 return 0;
160}
161
162int do_ipmonitor(int argc, char **argv)
163{
aba5acdf 164 char *file = NULL;
56f5daac
SH
165 unsigned int groups = 0;
166 int llink = 0;
167 int laddr = 0;
168 int lroute = 0;
169 int lmroute = 0;
170 int lprefix = 0;
171 int lneigh = 0;
172 int lnetconf = 0;
173 int lrule = 0;
174 int lnsid = 0;
175 int ifindex = 0;
aba5acdf 176
6fcabac5 177 groups |= nl_mgrp(RTNLGRP_LINK);
178 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
179 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
180 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
181 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
dacc5d41 182 groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE);
6fcabac5 183 groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE);
184 groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE);
185 groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
186 groups |= nl_mgrp(RTNLGRP_NEIGH);
187 groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF);
188 groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
2dd5909d
ND
189 groups |= nl_mgrp(RTNLGRP_IPV4_RULE);
190 groups |= nl_mgrp(RTNLGRP_IPV6_RULE);
d652ccbf 191 groups |= nl_mgrp(RTNLGRP_NSID);
76f7d89d 192 groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF);
6fcabac5 193
fc57a9df 194 rtnl_close(&rth);
aba5acdf
SH
195
196 while (argc > 0) {
197 if (matches(*argv, "file") == 0) {
198 NEXT_ARG();
199 file = *argv;
488c41d2
MS
200 } else if (matches(*argv, "label") == 0) {
201 prefix_banner = 1;
aba5acdf 202 } else if (matches(*argv, "link") == 0) {
56f5daac 203 llink = 1;
aba5acdf
SH
204 groups = 0;
205 } else if (matches(*argv, "address") == 0) {
56f5daac 206 laddr = 1;
aba5acdf
SH
207 groups = 0;
208 } else if (matches(*argv, "route") == 0) {
56f5daac 209 lroute = 1;
aba5acdf 210 groups = 0;
e34d3dcc 211 } else if (matches(*argv, "mroute") == 0) {
56f5daac 212 lmroute = 1;
e34d3dcc 213 groups = 0;
1cb54e58 214 } else if (matches(*argv, "prefix") == 0) {
56f5daac 215 lprefix = 1;
1cb54e58 216 groups = 0;
fb063322 217 } else if (matches(*argv, "neigh") == 0) {
6cdbf370
AH
218 lneigh = 1;
219 groups = 0;
9d0efc10
ND
220 } else if (matches(*argv, "netconf") == 0) {
221 lnetconf = 1;
222 groups = 0;
2dd5909d
ND
223 } else if (matches(*argv, "rule") == 0) {
224 lrule = 1;
225 groups = 0;
d652ccbf
ND
226 } else if (matches(*argv, "nsid") == 0) {
227 lnsid = 1;
228 groups = 0;
aba5acdf 229 } else if (strcmp(*argv, "all") == 0) {
56f5daac 230 prefix_banner = 1;
a9390c92
RP
231 } else if (matches(*argv, "all-nsid") == 0) {
232 listen_all_nsid = 1;
aba5acdf
SH
233 } else if (matches(*argv, "help") == 0) {
234 usage();
093b7646 235 } else if (strcmp(*argv, "dev") == 0) {
236 NEXT_ARG();
237
238 ifindex = ll_name_to_index(*argv);
239 if (!ifindex)
240 invarg("Device does not exist\n", *argv);
aba5acdf
SH
241 } else {
242 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
243 exit(-1);
244 }
245 argc--; argv++;
246 }
247
093b7646 248 ipaddr_reset_filter(1, ifindex);
249 iproute_reset_filter(ifindex);
250 ipmroute_reset_filter(ifindex);
251 ipneigh_reset_filter(ifindex);
252 ipnetconf_reset_filter(ifindex);
253
aba5acdf 254 if (llink)
b64f58b0 255 groups |= nl_mgrp(RTNLGRP_LINK);
aba5acdf
SH
256 if (laddr) {
257 if (!preferred_family || preferred_family == AF_INET)
b64f58b0 258 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
aba5acdf 259 if (!preferred_family || preferred_family == AF_INET6)
b64f58b0 260 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
aba5acdf
SH
261 }
262 if (lroute) {
263 if (!preferred_family || preferred_family == AF_INET)
b64f58b0 264 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
aba5acdf 265 if (!preferred_family || preferred_family == AF_INET6)
b64f58b0 266 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
dacc5d41
EB
267 if (!preferred_family || preferred_family == AF_MPLS)
268 groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE);
aba5acdf 269 }
e34d3dcc
ND
270 if (lmroute) {
271 if (!preferred_family || preferred_family == AF_INET)
272 groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE);
273 if (!preferred_family || preferred_family == AF_INET6)
274 groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE);
275 }
1cb54e58 276 if (lprefix) {
277 if (!preferred_family || preferred_family == AF_INET6)
b64f58b0 278 groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
1cb54e58 279 }
fb063322
VC
280 if (lneigh) {
281 groups |= nl_mgrp(RTNLGRP_NEIGH);
282 }
9d0efc10
ND
283 if (lnetconf) {
284 if (!preferred_family || preferred_family == AF_INET)
285 groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF);
286 if (!preferred_family || preferred_family == AF_INET6)
287 groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
76f7d89d
DA
288 if (!preferred_family || preferred_family == AF_MPLS)
289 groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF);
9d0efc10 290 }
2dd5909d
ND
291 if (lrule) {
292 if (!preferred_family || preferred_family == AF_INET)
293 groups |= nl_mgrp(RTNLGRP_IPV4_RULE);
294 if (!preferred_family || preferred_family == AF_INET6)
295 groups |= nl_mgrp(RTNLGRP_IPV6_RULE);
296 }
d652ccbf
ND
297 if (lnsid) {
298 groups |= nl_mgrp(RTNLGRP_NSID);
299 }
aba5acdf
SH
300 if (file) {
301 FILE *fp;
e49b51d6
SH
302 int err;
303
aba5acdf
SH
304 fp = fopen(file, "r");
305 if (fp == NULL) {
306 perror("Cannot fopen");
307 exit(-1);
308 }
e49b51d6
SH
309 err = rtnl_from_file(fp, accept_msg, stdout);
310 fclose(fp);
311 return err;
aba5acdf
SH
312 }
313
fc57a9df
SH
314 if (rtnl_open(&rth, groups) < 0)
315 exit(1);
449b824a
ND
316 if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
317 exit(1);
318
aba5acdf 319 ll_init_map(&rth);
e29a8e05 320 netns_nsid_socket_init();
d652ccbf 321 netns_map_init();
aba5acdf 322
fc57a9df 323 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
aba5acdf
SH
324 exit(2);
325
351efcde 326 return 0;
aba5acdf 327}