osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rtmon.c RTnetlink listener. |
| 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 <sys/time.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "SNAPSHOT.h" |
| 24 | |
| 25 | #include "utils.h" |
| 26 | #include "libnetlink.h" |
| 27 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 28 | int resolve_hosts; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | static int init_phase = 1; |
| 30 | |
| 31 | static void write_stamp(FILE *fp) |
| 32 | { |
| 33 | char buf[128]; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 34 | struct nlmsghdr *n1 = (void *)buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 35 | struct timeval tv; |
| 36 | |
Vadim Kochan | 27b14f2 | 2015-01-13 20:14:23 +0200 | [diff] [blame] | 37 | n1->nlmsg_type = NLMSG_TSTAMP; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 38 | n1->nlmsg_flags = 0; |
| 39 | n1->nlmsg_seq = 0; |
| 40 | n1->nlmsg_pid = 0; |
| 41 | n1->nlmsg_len = NLMSG_LENGTH(4*2); |
| 42 | gettimeofday(&tv, NULL); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 43 | ((__u32 *)NLMSG_DATA(n1))[0] = tv.tv_sec; |
| 44 | ((__u32 *)NLMSG_DATA(n1))[1] = tv.tv_usec; |
| 45 | fwrite((void *)n1, 1, NLMSG_ALIGN(n1->nlmsg_len), fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 48 | static int dump_msg(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl, |
| 49 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 51 | FILE *fp = (FILE *)arg; |
| 52 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 53 | if (!init_phase) |
| 54 | write_stamp(fp); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 55 | fwrite((void *)n, 1, NLMSG_ALIGN(n->nlmsg_len), fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 56 | fflush(fp); |
| 57 | return 0; |
| 58 | } |
| 59 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 60 | static int dump_msg2(const struct sockaddr_nl *who, |
| 61 | struct nlmsghdr *n, void *arg) |
| 62 | { |
| 63 | return dump_msg(who, NULL, n, arg); |
| 64 | } |
| 65 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 66 | static void usage(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 67 | { |
| 68 | fprintf(stderr, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n"); |
| 69 | fprintf(stderr, "LISTofOBJECTS := [ link ] [ address ] [ route ]\n"); |
| 70 | exit(-1); |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | main(int argc, char **argv) |
| 75 | { |
| 76 | FILE *fp; |
| 77 | struct rtnl_handle rth; |
| 78 | int family = AF_UNSPEC; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 79 | unsigned int groups = ~0U; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | int llink = 0; |
| 81 | int laddr = 0; |
| 82 | int lroute = 0; |
| 83 | char *file = NULL; |
| 84 | |
| 85 | while (argc > 1) { |
| 86 | if (matches(argv[1], "-family") == 0) { |
| 87 | argc--; |
| 88 | argv++; |
| 89 | if (argc <= 1) |
| 90 | usage(); |
| 91 | if (strcmp(argv[1], "inet") == 0) |
| 92 | family = AF_INET; |
| 93 | else if (strcmp(argv[1], "inet6") == 0) |
| 94 | family = AF_INET6; |
| 95 | else if (strcmp(argv[1], "link") == 0) |
| 96 | family = AF_INET6; |
| 97 | else if (strcmp(argv[1], "help") == 0) |
| 98 | usage(); |
| 99 | else { |
| 100 | fprintf(stderr, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv[1]); |
| 101 | exit(-1); |
| 102 | } |
| 103 | } else if (strcmp(argv[1], "-4") == 0) { |
| 104 | family = AF_INET; |
| 105 | } else if (strcmp(argv[1], "-6") == 0) { |
| 106 | family = AF_INET6; |
| 107 | } else if (strcmp(argv[1], "-0") == 0) { |
| 108 | family = AF_PACKET; |
| 109 | } else if (matches(argv[1], "-Version") == 0) { |
| 110 | printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT); |
| 111 | exit(0); |
| 112 | } else if (matches(argv[1], "file") == 0) { |
| 113 | argc--; |
| 114 | argv++; |
| 115 | if (argc <= 1) |
| 116 | usage(); |
| 117 | file = argv[1]; |
| 118 | } else if (matches(argv[1], "link") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 119 | llink = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 120 | groups = 0; |
| 121 | } else if (matches(argv[1], "address") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 122 | laddr = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 123 | groups = 0; |
| 124 | } else if (matches(argv[1], "route") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 125 | lroute = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 126 | groups = 0; |
| 127 | } else if (strcmp(argv[1], "all") == 0) { |
| 128 | groups = ~0U; |
| 129 | } else if (matches(argv[1], "help") == 0) { |
| 130 | usage(); |
| 131 | } else { |
| 132 | fprintf(stderr, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv[1]); |
| 133 | exit(-1); |
| 134 | } |
| 135 | argc--; argv++; |
| 136 | } |
| 137 | |
| 138 | if (file == NULL) { |
| 139 | fprintf(stderr, "Not enough information: argument \"file\" is required\n"); |
| 140 | exit(-1); |
| 141 | } |
| 142 | if (llink) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 143 | groups |= nl_mgrp(RTNLGRP_LINK); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 144 | if (laddr) { |
| 145 | if (!family || family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 146 | groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | if (!family || family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 148 | groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | } |
| 150 | if (lroute) { |
| 151 | if (!family || family == AF_INET) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 152 | groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | if (!family || family == AF_INET6) |
jamal | b64f58b | 2007-02-25 11:55:19 -0500 | [diff] [blame] | 154 | groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | fp = fopen(file, "w"); |
| 158 | if (fp == NULL) { |
| 159 | perror("Cannot fopen"); |
| 160 | exit(-1); |
| 161 | } |
| 162 | |
| 163 | if (rtnl_open(&rth, groups) < 0) |
| 164 | exit(1); |
| 165 | |
| 166 | if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) { |
| 167 | perror("Cannot send dump request"); |
| 168 | exit(1); |
| 169 | } |
| 170 | |
| 171 | write_stamp(fp); |
| 172 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 173 | if (rtnl_dump_filter(&rth, dump_msg2, fp) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 174 | fprintf(stderr, "Dump terminated\n"); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | init_phase = 0; |
| 179 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 180 | if (rtnl_listen(&rth, dump_msg, (void *)fp) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 181 | exit(2); |
| 182 | |
| 183 | exit(0); |
| 184 | } |