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