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