]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | static void usage(void) __attribute__((noreturn)); | |
4b6e07d8 | 27 | int prefix_banner; |
449b824a | 28 | int listen_all_nsid; |
aba5acdf SH |
29 | |
30 | static 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 | 39 | static 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 | 55 | static 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 SH |
60 | |
61 | if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) { | |
e34d3dcc ND |
62 | struct rtmsg *r = NLMSG_DATA(n); |
63 | int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)); | |
64 | ||
65 | if (len < 0) { | |
66 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
67 | return -1; | |
68 | } | |
69 | ||
2271779d | 70 | if (r->rtm_flags & RTM_F_CLONED) |
71 | return 0; | |
72 | ||
e34d3dcc ND |
73 | if (r->rtm_family == RTNL_FAMILY_IPMR || |
74 | r->rtm_family == RTNL_FAMILY_IP6MR) { | |
449b824a | 75 | print_headers(fp, "[MROUTE]", ctrl); |
e34d3dcc ND |
76 | print_mroute(who, n, arg); |
77 | return 0; | |
78 | } else { | |
449b824a | 79 | print_headers(fp, "[ROUTE]", ctrl); |
e34d3dcc ND |
80 | print_route(who, n, arg); |
81 | return 0; | |
82 | } | |
aba5acdf | 83 | } |
2271779d | 84 | |
aba5acdf SH |
85 | if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) { |
86 | ll_remember_index(who, n, NULL); | |
449b824a | 87 | print_headers(fp, "[LINK]", ctrl); |
aba5acdf SH |
88 | print_linkinfo(who, n, arg); |
89 | return 0; | |
90 | } | |
91 | if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) { | |
449b824a | 92 | print_headers(fp, "[ADDR]", ctrl); |
aba5acdf SH |
93 | print_addrinfo(who, n, arg); |
94 | return 0; | |
95 | } | |
4759758c | 96 | if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) { |
449b824a | 97 | print_headers(fp, "[ADDRLABEL]", ctrl); |
4759758c YH |
98 | print_addrlabel(who, n, arg); |
99 | return 0; | |
100 | } | |
1556e29d DS |
101 | if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH || |
102 | n->nlmsg_type == RTM_GETNEIGH) { | |
00185653 SH |
103 | if (preferred_family) { |
104 | struct ndmsg *r = NLMSG_DATA(n); | |
105 | ||
106 | if (r->ndm_family != preferred_family) | |
107 | return 0; | |
108 | } | |
109 | ||
449b824a | 110 | print_headers(fp, "[NEIGH]", ctrl); |
aba5acdf SH |
111 | print_neigh(who, n, arg); |
112 | return 0; | |
113 | } | |
1cb54e58 | 114 | if (n->nlmsg_type == RTM_NEWPREFIX) { |
449b824a | 115 | print_headers(fp, "[PREFIX]", ctrl); |
1cb54e58 | 116 | print_prefix(who, n, arg); |
117 | return 0; | |
118 | } | |
98bde989 | 119 | if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) { |
449b824a | 120 | print_headers(fp, "[RULE]", ctrl); |
98bde989 TG |
121 | print_rule(who, n, arg); |
122 | return 0; | |
123 | } | |
9d0efc10 | 124 | if (n->nlmsg_type == RTM_NEWNETCONF) { |
449b824a | 125 | print_headers(fp, "[NETCONF]", ctrl); |
0628cddd | 126 | print_netconf(who, ctrl, n, arg); |
9d0efc10 ND |
127 | return 0; |
128 | } | |
27b14f2e | 129 | if (n->nlmsg_type == NLMSG_TSTAMP) { |
ddb1129b | 130 | print_nlmsg_timestamp(fp, n); |
aba5acdf SH |
131 | return 0; |
132 | } | |
d652ccbf | 133 | if (n->nlmsg_type == RTM_NEWNSID || n->nlmsg_type == RTM_DELNSID) { |
449b824a | 134 | print_headers(fp, "[NSID]", ctrl); |
d652ccbf ND |
135 | print_nsid(who, n, arg); |
136 | return 0; | |
137 | } | |
aba5acdf SH |
138 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
139 | n->nlmsg_type != NLMSG_DONE) { | |
56f5daac SH |
140 | fprintf(fp, "Unknown message: type=0x%08x(%d) flags=0x%08x(%d)len=0x%08x(%d)\n", |
141 | n->nlmsg_type, n->nlmsg_type, | |
40aadf8b | 142 | n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len, |
143 | n->nlmsg_len); | |
aba5acdf SH |
144 | } |
145 | return 0; | |
146 | } | |
147 | ||
148 | int do_ipmonitor(int argc, char **argv) | |
149 | { | |
aba5acdf | 150 | char *file = NULL; |
56f5daac SH |
151 | unsigned int groups = 0; |
152 | int llink = 0; | |
153 | int laddr = 0; | |
154 | int lroute = 0; | |
155 | int lmroute = 0; | |
156 | int lprefix = 0; | |
157 | int lneigh = 0; | |
158 | int lnetconf = 0; | |
159 | int lrule = 0; | |
160 | int lnsid = 0; | |
161 | int ifindex = 0; | |
aba5acdf | 162 | |
6fcabac5 | 163 | groups |= nl_mgrp(RTNLGRP_LINK); |
164 | groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); | |
165 | groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); | |
166 | groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); | |
167 | groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); | |
dacc5d41 | 168 | groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE); |
6fcabac5 | 169 | groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE); |
170 | groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE); | |
171 | groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); | |
172 | groups |= nl_mgrp(RTNLGRP_NEIGH); | |
173 | groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF); | |
174 | groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF); | |
2dd5909d ND |
175 | groups |= nl_mgrp(RTNLGRP_IPV4_RULE); |
176 | groups |= nl_mgrp(RTNLGRP_IPV6_RULE); | |
d652ccbf | 177 | groups |= nl_mgrp(RTNLGRP_NSID); |
76f7d89d | 178 | groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF); |
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); | |
76f7d89d DA |
274 | if (!preferred_family || preferred_family == AF_MPLS) |
275 | groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF); | |
9d0efc10 | 276 | } |
2dd5909d ND |
277 | if (lrule) { |
278 | if (!preferred_family || preferred_family == AF_INET) | |
279 | groups |= nl_mgrp(RTNLGRP_IPV4_RULE); | |
280 | if (!preferred_family || preferred_family == AF_INET6) | |
281 | groups |= nl_mgrp(RTNLGRP_IPV6_RULE); | |
282 | } | |
d652ccbf ND |
283 | if (lnsid) { |
284 | groups |= nl_mgrp(RTNLGRP_NSID); | |
285 | } | |
aba5acdf SH |
286 | if (file) { |
287 | FILE *fp; | |
e49b51d6 SH |
288 | int err; |
289 | ||
aba5acdf SH |
290 | fp = fopen(file, "r"); |
291 | if (fp == NULL) { | |
292 | perror("Cannot fopen"); | |
293 | exit(-1); | |
294 | } | |
e49b51d6 SH |
295 | err = rtnl_from_file(fp, accept_msg, stdout); |
296 | fclose(fp); | |
297 | return err; | |
aba5acdf SH |
298 | } |
299 | ||
fc57a9df SH |
300 | if (rtnl_open(&rth, groups) < 0) |
301 | exit(1); | |
449b824a ND |
302 | if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0) |
303 | exit(1); | |
304 | ||
aba5acdf | 305 | ll_init_map(&rth); |
e29a8e05 | 306 | netns_nsid_socket_init(); |
d652ccbf | 307 | netns_map_init(); |
aba5acdf | 308 | |
fc57a9df | 309 | if (rtnl_listen(&rth, accept_msg, stdout) < 0) |
aba5acdf SH |
310 | exit(2); |
311 | ||
351efcde | 312 | return 0; |
aba5acdf | 313 | } |