]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmonitor.c
ip: allow to specify mode for sit tunnels
[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 ]\n");
33 fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
34 fprintf(stderr, " neigh | netconf\n");
35 fprintf(stderr, "FILE := file FILENAME\n");
36 exit(-1);
37 }
38
39
40 static int accept_msg(const struct sockaddr_nl *who,
41 struct nlmsghdr *n, void *arg)
42 {
43 FILE *fp = (FILE*)arg;
44
45 if (timestamp)
46 print_timestamp(fp);
47
48 if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
49 struct rtmsg *r = NLMSG_DATA(n);
50 int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
51
52 if (len < 0) {
53 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
54 return -1;
55 }
56
57 if (r->rtm_family == RTNL_FAMILY_IPMR ||
58 r->rtm_family == RTNL_FAMILY_IP6MR) {
59 if (prefix_banner)
60 fprintf(fp, "[MROUTE]");
61 print_mroute(who, n, arg);
62 return 0;
63 } else {
64 if (prefix_banner)
65 fprintf(fp, "[ROUTE]");
66 print_route(who, n, arg);
67 return 0;
68 }
69 }
70 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
71 ll_remember_index(who, n, NULL);
72 if (prefix_banner)
73 fprintf(fp, "[LINK]");
74 print_linkinfo(who, n, arg);
75 return 0;
76 }
77 if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
78 if (prefix_banner)
79 fprintf(fp, "[ADDR]");
80 print_addrinfo(who, n, arg);
81 return 0;
82 }
83 if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
84 if (prefix_banner)
85 fprintf(fp, "[ADDRLABEL]");
86 print_addrlabel(who, n, arg);
87 return 0;
88 }
89 if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH ||
90 n->nlmsg_type == RTM_GETNEIGH) {
91 if (prefix_banner)
92 fprintf(fp, "[NEIGH]");
93 print_neigh(who, n, arg);
94 return 0;
95 }
96 if (n->nlmsg_type == RTM_NEWPREFIX) {
97 if (prefix_banner)
98 fprintf(fp, "[PREFIX]");
99 print_prefix(who, n, arg);
100 return 0;
101 }
102 if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
103 if (prefix_banner)
104 fprintf(fp, "[RULE]");
105 print_rule(who, n, arg);
106 return 0;
107 }
108 if (n->nlmsg_type == RTM_NEWNETCONF) {
109 if (prefix_banner)
110 fprintf(fp, "[NETCONF]");
111 print_netconf(who, n, arg);
112 return 0;
113 }
114 if (n->nlmsg_type == 15) {
115 char *tstr;
116 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
117 long usecs = ((__u32*)NLMSG_DATA(n))[1];
118 tstr = asctime(localtime(&secs));
119 tstr[strlen(tstr)-1] = 0;
120 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
121 return 0;
122 }
123 if (n->nlmsg_type == RTM_NEWQDISC ||
124 n->nlmsg_type == RTM_DELQDISC ||
125 n->nlmsg_type == RTM_NEWTCLASS ||
126 n->nlmsg_type == RTM_DELTCLASS ||
127 n->nlmsg_type == RTM_NEWTFILTER ||
128 n->nlmsg_type == RTM_DELTFILTER)
129 return 0;
130 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
131 n->nlmsg_type != NLMSG_DONE) {
132 fprintf(fp, "Unknown message: %08x %08x %08x\n",
133 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
134 }
135 return 0;
136 }
137
138 int do_ipmonitor(int argc, char **argv)
139 {
140 char *file = NULL;
141 unsigned groups = ~RTMGRP_TC;
142 int llink=0;
143 int laddr=0;
144 int lroute=0;
145 int lmroute=0;
146 int lprefix=0;
147 int lneigh=0;
148 int lnetconf=0;
149
150 rtnl_close(&rth);
151 ipaddr_reset_filter(1);
152 iproute_reset_filter();
153 ipmroute_reset_filter();
154 ipneigh_reset_filter();
155
156 while (argc > 0) {
157 if (matches(*argv, "file") == 0) {
158 NEXT_ARG();
159 file = *argv;
160 } else if (matches(*argv, "link") == 0) {
161 llink=1;
162 groups = 0;
163 } else if (matches(*argv, "address") == 0) {
164 laddr=1;
165 groups = 0;
166 } else if (matches(*argv, "route") == 0) {
167 lroute=1;
168 groups = 0;
169 } else if (matches(*argv, "mroute") == 0) {
170 lmroute=1;
171 groups = 0;
172 } else if (matches(*argv, "prefix") == 0) {
173 lprefix=1;
174 groups = 0;
175 } else if (matches(*argv, "neigh") == 0) {
176 lneigh = 1;
177 groups = 0;
178 } else if (matches(*argv, "netconf") == 0) {
179 lnetconf = 1;
180 groups = 0;
181 } else if (strcmp(*argv, "all") == 0) {
182 groups = ~RTMGRP_TC;
183 prefix_banner=1;
184 } else if (matches(*argv, "help") == 0) {
185 usage();
186 } else {
187 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
188 exit(-1);
189 }
190 argc--; argv++;
191 }
192
193 if (llink)
194 groups |= nl_mgrp(RTNLGRP_LINK);
195 if (laddr) {
196 if (!preferred_family || preferred_family == AF_INET)
197 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
198 if (!preferred_family || preferred_family == AF_INET6)
199 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
200 }
201 if (lroute) {
202 if (!preferred_family || preferred_family == AF_INET)
203 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
204 if (!preferred_family || preferred_family == AF_INET6)
205 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
206 }
207 if (lmroute) {
208 if (!preferred_family || preferred_family == AF_INET)
209 groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE);
210 if (!preferred_family || preferred_family == AF_INET6)
211 groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE);
212 }
213 if (lprefix) {
214 if (!preferred_family || preferred_family == AF_INET6)
215 groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
216 }
217 if (lneigh) {
218 groups |= nl_mgrp(RTNLGRP_NEIGH);
219 }
220 if (lnetconf) {
221 if (!preferred_family || preferred_family == AF_INET)
222 groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF);
223 if (!preferred_family || preferred_family == AF_INET6)
224 groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
225 }
226 if (file) {
227 FILE *fp;
228 fp = fopen(file, "r");
229 if (fp == NULL) {
230 perror("Cannot fopen");
231 exit(-1);
232 }
233 return rtnl_from_file(fp, accept_msg, stdout);
234 }
235
236 if (rtnl_open(&rth, groups) < 0)
237 exit(1);
238 ll_init_map(&rth);
239
240 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
241 exit(2);
242
243 return 0;
244 }