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