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 | { |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 32 | fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ]" |
| 33 | "[ label ] [dev DEVICE]\n"); |
Nicolas Dichtel | cbe195d | 2012-12-17 17:41:27 +0100 | [diff] [blame] | 34 | fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n"); |
| 35 | fprintf(stderr, " neigh | netconf\n"); |
| 36 | fprintf(stderr, "FILE := file FILENAME\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 37 | exit(-1); |
| 38 | } |
| 39 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 40 | static int accept_msg(const struct sockaddr_nl *who, |
| 41 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | { |
| 43 | FILE *fp = (FILE*)arg; |
| 44 | |
| 45 | if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 46 | struct rtmsg *r = NLMSG_DATA(n); |
| 47 | int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)); |
| 48 | |
| 49 | if (len < 0) { |
| 50 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 51 | return -1; |
| 52 | } |
| 53 | |
vadimk | 2271779 | 2014-09-04 22:48:08 +0300 | [diff] [blame] | 54 | if (r->rtm_flags & RTM_F_CLONED) |
| 55 | return 0; |
| 56 | |
| 57 | if (timestamp) |
| 58 | print_timestamp(fp); |
| 59 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 60 | if (r->rtm_family == RTNL_FAMILY_IPMR || |
| 61 | r->rtm_family == RTNL_FAMILY_IP6MR) { |
| 62 | if (prefix_banner) |
| 63 | fprintf(fp, "[MROUTE]"); |
| 64 | print_mroute(who, n, arg); |
| 65 | return 0; |
| 66 | } else { |
| 67 | if (prefix_banner) |
| 68 | fprintf(fp, "[ROUTE]"); |
| 69 | print_route(who, n, arg); |
| 70 | return 0; |
| 71 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 72 | } |
vadimk | 2271779 | 2014-09-04 22:48:08 +0300 | [diff] [blame] | 73 | |
| 74 | if (timestamp) |
| 75 | print_timestamp(fp); |
| 76 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) { |
| 78 | ll_remember_index(who, n, NULL); |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 79 | if (prefix_banner) |
| 80 | fprintf(fp, "[LINK]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 81 | print_linkinfo(who, n, arg); |
| 82 | return 0; |
| 83 | } |
| 84 | if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 85 | if (prefix_banner) |
| 86 | fprintf(fp, "[ADDR]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 87 | print_addrinfo(who, n, arg); |
| 88 | return 0; |
| 89 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 90 | if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 91 | if (prefix_banner) |
| 92 | fprintf(fp, "[ADDRLABEL]"); |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 93 | print_addrlabel(who, n, arg); |
| 94 | return 0; |
| 95 | } |
David L Stevens | 1556e29 | 2012-12-12 10:02:19 -0800 | [diff] [blame] | 96 | if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH || |
| 97 | n->nlmsg_type == RTM_GETNEIGH) { |
Stephen Hemminger | 0018565 | 2013-08-29 12:18:52 -0700 | [diff] [blame] | 98 | if (preferred_family) { |
| 99 | struct ndmsg *r = NLMSG_DATA(n); |
| 100 | |
| 101 | if (r->ndm_family != preferred_family) |
| 102 | return 0; |
| 103 | } |
| 104 | |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 105 | if (prefix_banner) |
| 106 | fprintf(fp, "[NEIGH]"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 107 | print_neigh(who, n, arg); |
| 108 | return 0; |
| 109 | } |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 110 | if (n->nlmsg_type == RTM_NEWPREFIX) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 111 | if (prefix_banner) |
| 112 | fprintf(fp, "[PREFIX]"); |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 113 | print_prefix(who, n, arg); |
| 114 | return 0; |
| 115 | } |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 116 | if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 117 | if (prefix_banner) |
| 118 | fprintf(fp, "[RULE]"); |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 119 | print_rule(who, n, arg); |
| 120 | return 0; |
| 121 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 122 | if (n->nlmsg_type == RTM_NEWNETCONF) { |
| 123 | if (prefix_banner) |
| 124 | fprintf(fp, "[NETCONF]"); |
| 125 | print_netconf(who, n, arg); |
| 126 | return 0; |
| 127 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 128 | if (n->nlmsg_type == 15) { |
| 129 | char *tstr; |
| 130 | time_t secs = ((__u32*)NLMSG_DATA(n))[0]; |
| 131 | long usecs = ((__u32*)NLMSG_DATA(n))[1]; |
| 132 | tstr = asctime(localtime(&secs)); |
| 133 | tstr[strlen(tstr)-1] = 0; |
| 134 | fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); |
| 135 | return 0; |
| 136 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 137 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 138 | n->nlmsg_type != NLMSG_DONE) { |
vadimk | 40aadf8 | 2014-09-29 23:07:57 +0300 | [diff] [blame] | 139 | fprintf(fp, "Unknown message: type=0x%08x(%d) flags=0x%08x(%d)" |
| 140 | "len=0x%08x(%d)\n", n->nlmsg_type, n->nlmsg_type, |
| 141 | n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len, |
| 142 | n->nlmsg_len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int do_ipmonitor(int argc, char **argv) |
| 148 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | char *file = NULL; |
vadimk | 6fcabac | 2014-12-05 02:18:59 +0200 | [diff] [blame^] | 150 | unsigned groups = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 151 | int llink=0; |
| 152 | int laddr=0; |
| 153 | int lroute=0; |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 154 | int lmroute=0; |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 155 | int lprefix=0; |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 156 | int lneigh=0; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 157 | int lnetconf=0; |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 158 | int ifindex=0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | |
vadimk | 6fcabac | 2014-12-05 02:18:59 +0200 | [diff] [blame^] | 160 | groups |= nl_mgrp(RTNLGRP_LINK); |
| 161 | groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); |
| 162 | groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); |
| 163 | groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); |
| 164 | groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); |
| 165 | groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE); |
| 166 | groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE); |
| 167 | groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); |
| 168 | groups |= nl_mgrp(RTNLGRP_NEIGH); |
| 169 | groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF); |
| 170 | groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF); |
| 171 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 172 | rtnl_close(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | |
| 174 | while (argc > 0) { |
| 175 | if (matches(*argv, "file") == 0) { |
| 176 | NEXT_ARG(); |
| 177 | file = *argv; |
Martin Schwenke | 488c41d | 2013-08-19 15:43:30 +1000 | [diff] [blame] | 178 | } else if (matches(*argv, "label") == 0) { |
| 179 | prefix_banner = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 180 | } else if (matches(*argv, "link") == 0) { |
| 181 | llink=1; |
| 182 | groups = 0; |
| 183 | } else if (matches(*argv, "address") == 0) { |
| 184 | laddr=1; |
| 185 | groups = 0; |
| 186 | } else if (matches(*argv, "route") == 0) { |
| 187 | lroute=1; |
| 188 | groups = 0; |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 189 | } else if (matches(*argv, "mroute") == 0) { |
| 190 | lmroute=1; |
| 191 | groups = 0; |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 192 | } else if (matches(*argv, "prefix") == 0) { |
| 193 | lprefix=1; |
| 194 | groups = 0; |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 195 | } else if (matches(*argv, "neigh") == 0) { |
Andreas Henriksson | 6cdbf37 | 2009-03-14 17:37:35 +0100 | [diff] [blame] | 196 | lneigh = 1; |
| 197 | groups = 0; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 198 | } else if (matches(*argv, "netconf") == 0) { |
| 199 | lnetconf = 1; |
| 200 | groups = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | } else if (strcmp(*argv, "all") == 0) { |
Varun Chandramohan | 4b6e07d | 2009-03-12 13:42:43 +0530 | [diff] [blame] | 202 | prefix_banner=1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 203 | } else if (matches(*argv, "help") == 0) { |
| 204 | usage(); |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 205 | } else if (strcmp(*argv, "dev") == 0) { |
| 206 | NEXT_ARG(); |
| 207 | |
| 208 | ifindex = ll_name_to_index(*argv); |
| 209 | if (!ifindex) |
| 210 | invarg("Device does not exist\n", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 211 | } else { |
| 212 | fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv); |
| 213 | exit(-1); |
| 214 | } |
| 215 | argc--; argv++; |
| 216 | } |
| 217 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 218 | ipaddr_reset_filter(1, ifindex); |
| 219 | iproute_reset_filter(ifindex); |
| 220 | ipmroute_reset_filter(ifindex); |
| 221 | ipneigh_reset_filter(ifindex); |
| 222 | ipnetconf_reset_filter(ifindex); |
| 223 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 224 | if (llink) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 225 | groups |= nl_mgrp(RTNLGRP_LINK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 226 | if (laddr) { |
| 227 | if (!preferred_family || preferred_family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 228 | groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 229 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 230 | groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 231 | } |
| 232 | if (lroute) { |
| 233 | if (!preferred_family || preferred_family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 234 | groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 235 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 236 | groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 237 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 238 | if (lmroute) { |
| 239 | if (!preferred_family || preferred_family == AF_INET) |
| 240 | groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE); |
| 241 | if (!preferred_family || preferred_family == AF_INET6) |
| 242 | groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE); |
| 243 | } |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 244 | if (lprefix) { |
| 245 | if (!preferred_family || preferred_family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 246 | groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); |
net[shemminger]!shemminger | 1cb54e5 | 2005-01-17 23:30:18 +0000 | [diff] [blame] | 247 | } |
Varun Chandramohan | fb06332 | 2009-03-12 13:42:07 +0530 | [diff] [blame] | 248 | if (lneigh) { |
| 249 | groups |= nl_mgrp(RTNLGRP_NEIGH); |
| 250 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 251 | if (lnetconf) { |
| 252 | if (!preferred_family || preferred_family == AF_INET) |
| 253 | groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF); |
| 254 | if (!preferred_family || preferred_family == AF_INET6) |
| 255 | groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF); |
| 256 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 257 | if (file) { |
| 258 | FILE *fp; |
| 259 | fp = fopen(file, "r"); |
| 260 | if (fp == NULL) { |
| 261 | perror("Cannot fopen"); |
| 262 | exit(-1); |
| 263 | } |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 264 | return rtnl_from_file(fp, accept_msg, stdout); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 265 | } |
| 266 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 267 | if (rtnl_open(&rth, groups) < 0) |
| 268 | exit(1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 269 | ll_init_map(&rth); |
| 270 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 271 | if (rtnl_listen(&rth, accept_msg, stdout) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 272 | exit(2); |
| 273 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 274 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 275 | } |