blob: cdaeb6f99dd7f447fa764970b77cb2dc71821cb3 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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));
28
29static void usage(void)
30{
31 fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ]\n");
32 exit(-1);
33}
34
35
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +000036int accept_msg(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +000037 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038{
39 FILE *fp = (FILE*)arg;
40
41 if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
42 print_route(who, n, arg);
43 return 0;
44 }
45 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
46 ll_remember_index(who, n, NULL);
47 print_linkinfo(who, n, arg);
48 return 0;
49 }
50 if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
51 print_addrinfo(who, n, arg);
52 return 0;
53 }
54 if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH) {
55 print_neigh(who, n, arg);
56 return 0;
57 }
net[shemminger]!shemminger1cb54e52005-01-17 23:30:18 +000058 if (n->nlmsg_type == RTM_NEWPREFIX) {
59 print_prefix(who, n, arg);
60 return 0;
61 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 if (n->nlmsg_type == 15) {
63 char *tstr;
64 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
65 long usecs = ((__u32*)NLMSG_DATA(n))[1];
66 tstr = asctime(localtime(&secs));
67 tstr[strlen(tstr)-1] = 0;
68 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
69 return 0;
70 }
71 if (n->nlmsg_type == RTM_NEWQDISC ||
72 n->nlmsg_type == RTM_DELQDISC ||
73 n->nlmsg_type == RTM_NEWTCLASS ||
74 n->nlmsg_type == RTM_DELTCLASS ||
75 n->nlmsg_type == RTM_NEWTFILTER ||
76 n->nlmsg_type == RTM_DELTFILTER)
77 return 0;
78 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
79 n->nlmsg_type != NLMSG_DONE) {
80 fprintf(fp, "Unknown message: %08x %08x %08x\n",
81 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
82 }
83 return 0;
84}
85
86int do_ipmonitor(int argc, char **argv)
87{
88 struct rtnl_handle rth;
89 char *file = NULL;
90 unsigned groups = ~RTMGRP_TC;
91 int llink=0;
92 int laddr=0;
93 int lroute=0;
net[shemminger]!shemminger1cb54e52005-01-17 23:30:18 +000094 int lprefix=0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000095
96 ipaddr_reset_filter(1);
97 iproute_reset_filter();
98 ipneigh_reset_filter();
99
100 while (argc > 0) {
101 if (matches(*argv, "file") == 0) {
102 NEXT_ARG();
103 file = *argv;
104 } else if (matches(*argv, "link") == 0) {
105 llink=1;
106 groups = 0;
107 } else if (matches(*argv, "address") == 0) {
108 laddr=1;
109 groups = 0;
110 } else if (matches(*argv, "route") == 0) {
111 lroute=1;
112 groups = 0;
net[shemminger]!shemminger1cb54e52005-01-17 23:30:18 +0000113 } else if (matches(*argv, "prefix") == 0) {
114 lprefix=1;
115 groups = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116 } else if (strcmp(*argv, "all") == 0) {
117 groups = ~RTMGRP_TC;
118 } else if (matches(*argv, "help") == 0) {
119 usage();
120 } else {
121 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
122 exit(-1);
123 }
124 argc--; argv++;
125 }
126
127 if (llink)
128 groups |= RTMGRP_LINK;
129 if (laddr) {
130 if (!preferred_family || preferred_family == AF_INET)
131 groups |= RTMGRP_IPV4_IFADDR;
132 if (!preferred_family || preferred_family == AF_INET6)
133 groups |= RTMGRP_IPV6_IFADDR;
134 }
135 if (lroute) {
136 if (!preferred_family || preferred_family == AF_INET)
137 groups |= RTMGRP_IPV4_ROUTE;
138 if (!preferred_family || preferred_family == AF_INET6)
139 groups |= RTMGRP_IPV6_ROUTE;
140 }
net[shemminger]!shemminger1cb54e52005-01-17 23:30:18 +0000141 if (lprefix) {
142 if (!preferred_family || preferred_family == AF_INET6)
143 groups |= RTMGRP_IPV6_PREFIX;
144 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000145
146 if (file) {
147 FILE *fp;
148 fp = fopen(file, "r");
149 if (fp == NULL) {
150 perror("Cannot fopen");
151 exit(-1);
152 }
153 return rtnl_from_file(fp, accept_msg, (void*)stdout);
154 }
155
156 if (rtnl_open(&rth, groups) < 0)
157 exit(1);
158
159 ll_init_map(&rth);
160
161 if (rtnl_listen(&rth, accept_msg, (void*)stdout) < 0)
162 exit(2);
163
164 exit(0);
165}