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