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