osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ipmroute.c "ip mroute". |
| 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> |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 19 | #include <sys/ioctl.h> |
| 20 | #include <sys/socket.h> |
osdl.org!shemminger | 0315641 | 2004-06-09 22:58:42 +0000 | [diff] [blame] | 21 | #include <netinet/in.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #include <string.h> |
| 24 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/if.h> |
| 27 | #include <linux/if_arp.h> |
| 28 | #include <linux/sockios.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 30 | #include <rt_names.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 31 | #include "utils.h" |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 32 | #include "ip_common.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 33 | |
| 34 | static void usage(void) __attribute__((noreturn)); |
| 35 | |
| 36 | static void usage(void) |
| 37 | { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 38 | fprintf(stderr, "Usage: ip mroute show [ [ to ] PREFIX ] [ from PREFIX ] [ iif DEVICE ]\n"); |
Nicolas Dichtel | 2a89832 | 2012-12-14 09:56:47 -0800 | [diff] [blame] | 39 | fprintf(stderr, " [ table TABLE_ID ]\n"); |
| 40 | fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | #if 0 |
| 42 | fprintf(stderr, "Usage: ip mroute [ add | del ] DESTINATION from SOURCE [ iif DEVICE ] [ oif DEVICE ]\n"); |
| 43 | #endif |
| 44 | exit(-1); |
| 45 | } |
| 46 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 47 | struct rtfilter |
| 48 | { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 49 | int tb; |
| 50 | int af; |
| 51 | int iif; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | inet_prefix mdst; |
| 53 | inet_prefix msrc; |
| 54 | } filter; |
| 55 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 56 | int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 57 | { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 58 | FILE *fp = (FILE*)arg; |
| 59 | struct rtmsg *r = NLMSG_DATA(n); |
| 60 | int len = n->nlmsg_len; |
| 61 | struct rtattr * tb[RTA_MAX+1]; |
| 62 | char abuf[256]; |
| 63 | char obuf[256]; |
| 64 | SPRINT_BUF(b1); |
| 65 | __u32 table; |
| 66 | int iif = 0; |
| 67 | int family; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 68 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 69 | if ((n->nlmsg_type != RTM_NEWROUTE && |
| 70 | n->nlmsg_type != RTM_DELROUTE) || |
| 71 | !(n->nlmsg_flags & NLM_F_MULTI)) { |
| 72 | fprintf(stderr, "Not a multicast route: %08x %08x %08x\n", |
| 73 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 74 | return 0; |
Thomas Jarosch | 67ef60a | 2011-10-03 05:24:08 +0000 | [diff] [blame] | 75 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 76 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 77 | if (len < 0) { |
| 78 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 79 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 81 | if (r->rtm_type != RTN_MULTICAST) { |
| 82 | fprintf(stderr, "Not a multicast route (type: %s)\n", |
| 83 | rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); |
| 84 | return 0; |
Thomas Jarosch | e588a7d | 2011-10-03 05:24:18 +0000 | [diff] [blame] | 85 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 86 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 87 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 88 | table = rtm_get_table(r, tb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 89 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 90 | if (filter.tb > 0 && filter.tb != table) |
| 91 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 92 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 93 | if (tb[RTA_IIF]) |
| 94 | iif = *(int*)RTA_DATA(tb[RTA_IIF]); |
| 95 | if (filter.iif && filter.iif != iif) |
| 96 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 97 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 98 | if (filter.af && filter.af != r->rtm_family) |
| 99 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 101 | if (tb[RTA_DST] && |
| 102 | filter.mdst.bitlen > 0 && |
| 103 | inet_addr_match(RTA_DATA(tb[RTA_DST]), &filter.mdst, filter.mdst.bitlen)) |
| 104 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 105 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 106 | if (tb[RTA_SRC] && |
| 107 | filter.msrc.bitlen > 0 && |
| 108 | inet_addr_match(RTA_DATA(tb[RTA_SRC]), &filter.msrc, filter.msrc.bitlen)) |
| 109 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 110 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 111 | family = r->rtm_family == RTNL_FAMILY_IPMR ? AF_INET : AF_INET6; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 112 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 113 | if (n->nlmsg_type == RTM_DELROUTE) |
| 114 | fprintf(fp, "Deleted "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 115 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 116 | if (tb[RTA_SRC]) |
| 117 | len = snprintf(obuf, sizeof(obuf), |
| 118 | "(%s, ", rt_addr_n2a(family, |
Eric W. Biederman | 26dcdf3 | 2015-03-15 14:48:32 -0500 | [diff] [blame] | 119 | RTA_PAYLOAD(tb[RTA_SRC]), |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 120 | RTA_DATA(tb[RTA_SRC]), |
| 121 | abuf, sizeof(abuf))); |
| 122 | else |
| 123 | len = sprintf(obuf, "(unknown, "); |
| 124 | if (tb[RTA_DST]) |
| 125 | snprintf(obuf + len, sizeof(obuf) - len, |
Stephen Hemminger | 656111b | 2014-08-04 10:30:35 -0700 | [diff] [blame] | 126 | "%s)", rt_addr_n2a(family, |
Eric W. Biederman | 26dcdf3 | 2015-03-15 14:48:32 -0500 | [diff] [blame] | 127 | RTA_PAYLOAD(tb[RTA_DST]), |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 128 | RTA_DATA(tb[RTA_DST]), |
| 129 | abuf, sizeof(abuf))); |
| 130 | else |
| 131 | snprintf(obuf + len, sizeof(obuf) - len, "unknown) "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 133 | fprintf(fp, "%-32s Iif: ", obuf); |
| 134 | if (iif) |
| 135 | fprintf(fp, "%-10s ", ll_index_to_name(iif)); |
| 136 | else |
| 137 | fprintf(fp, "unresolved "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 138 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 139 | if (tb[RTA_MULTIPATH]) { |
| 140 | struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]); |
| 141 | int first = 1; |
| 142 | |
| 143 | len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); |
| 144 | |
| 145 | for (;;) { |
| 146 | if (len < sizeof(*nh)) |
| 147 | break; |
| 148 | if (nh->rtnh_len > len) |
| 149 | break; |
| 150 | |
| 151 | if (first) { |
| 152 | fprintf(fp, "Oifs: "); |
| 153 | first = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 154 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 155 | fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex)); |
| 156 | if (nh->rtnh_hops > 1) |
| 157 | fprintf(fp, "(ttl %d) ", nh->rtnh_hops); |
| 158 | else |
| 159 | fprintf(fp, " "); |
| 160 | len -= NLMSG_ALIGN(nh->rtnh_len); |
| 161 | nh = RTNH_NEXT(nh); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 162 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 163 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 164 | if (show_stats && tb[RTA_MFC_STATS]) { |
| 165 | struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]); |
| 166 | |
| 167 | fprintf(fp, "%s %"PRIu64" packets, %"PRIu64" bytes", _SL_, |
| 168 | (uint64_t)mfcs->mfcs_packets, |
| 169 | (uint64_t)mfcs->mfcs_bytes); |
| 170 | if (mfcs->mfcs_wrong_if) |
| 171 | fprintf(fp, ", %"PRIu64" arrived on wrong iif.", |
| 172 | (uint64_t)mfcs->mfcs_wrong_if); |
| 173 | } |
| 174 | fprintf(fp, "\n"); |
| 175 | fflush(fp); |
| 176 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 177 | } |
| 178 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 179 | void ipmroute_reset_filter(int ifindex) |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 180 | { |
| 181 | memset(&filter, 0, sizeof(filter)); |
| 182 | filter.mdst.bitlen = -1; |
| 183 | filter.msrc.bitlen = -1; |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 184 | filter.iif = ifindex; |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 185 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 186 | |
| 187 | static int mroute_list(int argc, char **argv) |
| 188 | { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 189 | char *id = NULL; |
| 190 | int family; |
| 191 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 192 | ipmroute_reset_filter(0); |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 193 | if (preferred_family == AF_UNSPEC) |
| 194 | family = AF_INET; |
| 195 | else |
| 196 | family = AF_INET6; |
| 197 | if (family == AF_INET) { |
| 198 | filter.af = RTNL_FAMILY_IPMR; |
| 199 | filter.tb = RT_TABLE_DEFAULT; /* for backward compatibility */ |
| 200 | } else |
| 201 | filter.af = RTNL_FAMILY_IP6MR; |
| 202 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 203 | while (argc > 0) { |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 204 | if (matches(*argv, "table") == 0) { |
| 205 | __u32 tid; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | NEXT_ARG(); |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 207 | if (rtnl_rttable_a2n(&tid, *argv)) { |
| 208 | if (strcmp(*argv, "all") == 0) { |
| 209 | filter.tb = 0; |
| 210 | } else if (strcmp(*argv, "help") == 0) { |
| 211 | usage(); |
| 212 | } else { |
| 213 | invarg("table id value is invalid\n", *argv); |
| 214 | } |
| 215 | } else |
| 216 | filter.tb = tid; |
| 217 | } else if (strcmp(*argv, "iif") == 0) { |
| 218 | NEXT_ARG(); |
| 219 | id = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 220 | } else if (matches(*argv, "from") == 0) { |
| 221 | NEXT_ARG(); |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 222 | get_prefix(&filter.msrc, *argv, family); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 223 | } else { |
| 224 | if (strcmp(*argv, "to") == 0) { |
| 225 | NEXT_ARG(); |
| 226 | } |
| 227 | if (matches(*argv, "help") == 0) |
| 228 | usage(); |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 229 | get_prefix(&filter.mdst, *argv, family); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 230 | } |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 231 | argc--; argv++; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Nicolas Dichtel | e34d3dc | 2012-12-14 10:08:17 -0800 | [diff] [blame] | 234 | ll_init_map(&rth); |
| 235 | |
| 236 | if (id) { |
| 237 | int idx; |
| 238 | |
| 239 | if ((idx = ll_name_to_index(id)) == 0) { |
| 240 | fprintf(stderr, "Cannot find device \"%s\"\n", id); |
| 241 | return -1; |
| 242 | } |
| 243 | filter.iif = idx; |
| 244 | } |
| 245 | |
| 246 | if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) { |
| 247 | perror("Cannot send dump request"); |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) { |
| 252 | fprintf(stderr, "Dump terminated\n"); |
| 253 | exit(1); |
| 254 | } |
| 255 | |
| 256 | exit(0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | int do_multiroute(int argc, char **argv) |
| 260 | { |
| 261 | if (argc < 1) |
| 262 | return mroute_list(0, NULL); |
| 263 | #if 0 |
| 264 | if (matches(*argv, "add") == 0) |
| 265 | return mroute_modify(RTM_NEWADDR, argc-1, argv+1); |
| 266 | if (matches(*argv, "delete") == 0) |
| 267 | return mroute_modify(RTM_DELADDR, argc-1, argv+1); |
| 268 | if (matches(*argv, "get") == 0) |
| 269 | return mroute_get(argc-1, argv+1); |
| 270 | #endif |
| 271 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 272 | || matches(*argv, "lst") == 0) |
| 273 | return mroute_list(argc-1, argv+1); |
| 274 | if (matches(*argv, "help") == 0) |
| 275 | usage(); |
| 276 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv); |
| 277 | exit(-1); |
| 278 | } |