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