osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)); |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 28 | int prefix_banner; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
| 30 | static void usage(void) |
| 31 | { |
| 32 | fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ]\n"); |
| 33 | exit(-1); |
| 34 | } |
| 35 | |
| 36 | |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 37 | int accept_msg(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 38 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | { |
| 40 | FILE *fp = (FILE*)arg; |
| 41 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 42 | if (timestamp) |
| 43 | print_timestamp(fp); |
| 44 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 46 | if (prefix_banner) |
| 47 | fprintf(fp, "[ROUTE]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 53 | if (prefix_banner) |
| 54 | fprintf(fp, "[LINK]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | print_linkinfo(who, n, arg); |
| 56 | return 0; |
| 57 | } |
| 58 | if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 59 | if (prefix_banner) |
| 60 | fprintf(fp, "[ADDR]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 61 | print_addrinfo(who, n, arg); |
| 62 | return 0; |
| 63 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 64 | if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 65 | if (prefix_banner) |
| 66 | fprintf(fp, "[ADDRLABEL]"); |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 67 | print_addrlabel(who, n, arg); |
| 68 | return 0; |
| 69 | } |
David L Stevens | 1556e29 | 2012-12-12 10:02:19 -0800 | [diff] [blame^] | 70 | if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH || |
| 71 | n->nlmsg_type == RTM_GETNEIGH) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 72 | if (prefix_banner) |
| 73 | fprintf(fp, "[NEIGH]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 74 | print_neigh(who, n, arg); |
| 75 | return 0; |
| 76 | } |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 77 | if (n->nlmsg_type == RTM_NEWPREFIX) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 78 | if (prefix_banner) |
| 79 | fprintf(fp, "[PREFIX]"); |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 80 | print_prefix(who, n, arg); |
| 81 | return 0; |
| 82 | } |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 83 | if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 84 | if (prefix_banner) |
| 85 | fprintf(fp, "[RULE]"); |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 86 | print_rule(who, n, arg); |
| 87 | return 0; |
| 88 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 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 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
| 119 | int do_ipmonitor(int argc, char **argv) |
| 120 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 121 | char *file = NULL; |
| 122 | unsigned groups = ~RTMGRP_TC; |
| 123 | int llink=0; |
| 124 | int laddr=0; |
| 125 | int lroute=0; |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 126 | int lprefix=0; |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 127 | int lneigh=0; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 128 | int lnetconf=0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 129 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 130 | rtnl_close(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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; |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 148 | } else if (matches(*argv, "prefix") == 0) { |
| 149 | lprefix=1; |
| 150 | groups = 0; |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 151 | } else if (matches(*argv, "neigh") == 0) { |
Andreas Henriksson | 6cdbf37 | 2009-03-14 17:37:35 +0100 | [diff] [blame] | 152 | lneigh = 1; |
| 153 | groups = 0; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 154 | } else if (matches(*argv, "netconf") == 0) { |
| 155 | lnetconf = 1; |
| 156 | groups = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 157 | } else if (strcmp(*argv, "all") == 0) { |
| 158 | groups = ~RTMGRP_TC; |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 159 | prefix_banner=1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 170 | groups |= nl_mgrp(RTNLGRP_LINK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 171 | if (laddr) { |
| 172 | if (!preferred_family || preferred_family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 173 | groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 174 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 175 | groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 176 | } |
| 177 | if (lroute) { |
| 178 | if (!preferred_family || preferred_family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 179 | groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 180 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 181 | groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 182 | } |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 183 | if (lprefix) { |
| 184 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 185 | groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 186 | } |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 187 | if (lneigh) { |
| 188 | groups |= nl_mgrp(RTNLGRP_NEIGH); |
| 189 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 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 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 196 | if (file) { |
| 197 | FILE *fp; |
| 198 | fp = fopen(file, "r"); |
| 199 | if (fp == NULL) { |
| 200 | perror("Cannot fopen"); |
| 201 | exit(-1); |
| 202 | } |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 203 | return rtnl_from_file(fp, accept_msg, stdout); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 204 | } |
| 205 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 206 | if (rtnl_open(&rth, groups) < 0) |
| 207 | exit(1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 208 | ll_init_map(&rth); |
| 209 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 210 | if (rtnl_listen(&rth, accept_msg, stdout) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 211 | exit(2); |
| 212 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 213 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 214 | } |