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