blob: 5d6922a23ae667532dbb284f9bf699ae24408b1d [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 Dichtele34d3dc2012-12-14 10:08:17 -080018#include <inttypes.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000019#include <sys/ioctl.h>
20#include <sys/socket.h>
osdl.org!shemminger03156412004-06-09 22:58:42 +000021#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <string.h>
24
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025#include <linux/netdevice.h>
26#include <linux/if.h>
27#include <linux/if_arp.h>
28#include <linux/sockios.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080030#include <rt_names.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000031#include "utils.h"
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080032#include "ip_common.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000033
34static void usage(void) __attribute__((noreturn));
35
36static void usage(void)
37{
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080038 fprintf(stderr, "Usage: ip mroute show [ [ to ] PREFIX ] [ from PREFIX ] [ iif DEVICE ]\n");
Nicolas Dichtel2a898322012-12-14 09:56:47 -080039 fprintf(stderr, " [ table TABLE_ID ]\n");
40 fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000041#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
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070047struct rtfilter {
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080048 int tb;
49 int af;
50 int iif;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000051 inet_prefix mdst;
52 inet_prefix msrc;
53} filter;
54
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080055int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000056{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070057 FILE *fp = (FILE *)arg;
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080058 struct rtmsg *r = NLMSG_DATA(n);
59 int len = n->nlmsg_len;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070060 struct rtattr *tb[RTA_MAX+1];
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080061 char obuf[256];
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070062
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080063 SPRINT_BUF(b1);
64 __u32 table;
65 int iif = 0;
66 int family;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000067
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080068 if ((n->nlmsg_type != RTM_NEWROUTE &&
Nicolas Dichtel505f9182015-04-22 10:27:07 +020069 n->nlmsg_type != RTM_DELROUTE)) {
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080070 fprintf(stderr, "Not a multicast route: %08x %08x %08x\n",
71 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
72 return 0;
Thomas Jarosch67ef60a2011-10-03 05:24:08 +000073 }
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080074 len -= NLMSG_LENGTH(sizeof(*r));
75 if (len < 0) {
76 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
77 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000078 }
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080079 if (r->rtm_type != RTN_MULTICAST) {
80 fprintf(stderr, "Not a multicast route (type: %s)\n",
81 rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
82 return 0;
Thomas Jarosche588a7d2011-10-03 05:24:18 +000083 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000084
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080085 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
86 table = rtm_get_table(r, tb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000087
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080088 if (filter.tb > 0 && filter.tb != table)
89 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000090
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080091 if (tb[RTA_IIF])
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070092 iif = *(int *)RTA_DATA(tb[RTA_IIF]);
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080093 if (filter.iif && filter.iif != iif)
94 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000095
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -080096 if (filter.af && filter.af != r->rtm_family)
97 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000098
Mazhar Rana45b01c42015-06-11 21:37:36 +053099 if (tb[RTA_DST] && filter.mdst.bitlen > 0) {
Phil Sutterd17b1362016-07-18 16:48:42 +0200100 inet_prefix dst = { .family = r->rtm_family };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101
Mazhar Rana45b01c42015-06-11 21:37:36 +0530102 memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), RTA_PAYLOAD(tb[RTA_DST]));
103 if (inet_addr_match(&dst, &filter.mdst, filter.mdst.bitlen))
104 return 0;
105 }
106
107 if (tb[RTA_SRC] && filter.msrc.bitlen > 0) {
Phil Sutterd17b1362016-07-18 16:48:42 +0200108 inet_prefix src = { .family = r->rtm_family };
Mazhar Rana45b01c42015-06-11 21:37:36 +0530109
Mazhar Rana45b01c42015-06-11 21:37:36 +0530110 memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), RTA_PAYLOAD(tb[RTA_SRC]));
111 if (inet_addr_match(&src, &filter.msrc, filter.msrc.bitlen))
112 return 0;
113 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800115 family = r->rtm_family == RTNL_FAMILY_IPMR ? AF_INET : AF_INET6;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800117 if (n->nlmsg_type == RTM_DELROUTE)
118 fprintf(fp, "Deleted ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000119
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800120 if (tb[RTA_SRC])
121 len = snprintf(obuf, sizeof(obuf),
Phil Sutter7faf1582016-03-22 19:35:18 +0100122 "(%s, ", rt_addr_n2a_rta(family, tb[RTA_SRC]));
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800123 else
124 len = sprintf(obuf, "(unknown, ");
125 if (tb[RTA_DST])
126 snprintf(obuf + len, sizeof(obuf) - len,
Phil Sutter7faf1582016-03-22 19:35:18 +0100127 "%s)", rt_addr_n2a_rta(family, tb[RTA_DST]));
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800128 else
129 snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800131 fprintf(fp, "%-32s Iif: ", obuf);
132 if (iif)
133 fprintf(fp, "%-10s ", ll_index_to_name(iif));
134 else
135 fprintf(fp, "unresolved ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800137 if (tb[RTA_MULTIPATH]) {
138 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
139 int first = 1;
140
141 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
142
143 for (;;) {
144 if (len < sizeof(*nh))
145 break;
146 if (nh->rtnh_len > len)
147 break;
148
149 if (first) {
150 fprintf(fp, "Oifs: ");
151 first = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000152 }
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800153 fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex));
154 if (nh->rtnh_hops > 1)
155 fprintf(fp, "(ttl %d) ", nh->rtnh_hops);
156 else
157 fprintf(fp, " ");
158 len -= NLMSG_ALIGN(nh->rtnh_len);
159 nh = RTNH_NEXT(nh);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000161 }
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800162 if (show_stats && tb[RTA_MFC_STATS]) {
163 struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]);
164
165 fprintf(fp, "%s %"PRIu64" packets, %"PRIu64" bytes", _SL_,
166 (uint64_t)mfcs->mfcs_packets,
167 (uint64_t)mfcs->mfcs_bytes);
168 if (mfcs->mfcs_wrong_if)
169 fprintf(fp, ", %"PRIu64" arrived on wrong iif.",
170 (uint64_t)mfcs->mfcs_wrong_if);
171 }
172 fprintf(fp, "\n");
173 fflush(fp);
174 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175}
176
vadimk093b7642014-10-20 12:25:17 +0300177void ipmroute_reset_filter(int ifindex)
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800178{
179 memset(&filter, 0, sizeof(filter));
180 filter.mdst.bitlen = -1;
181 filter.msrc.bitlen = -1;
vadimk093b7642014-10-20 12:25:17 +0300182 filter.iif = ifindex;
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800183}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000184
185static int mroute_list(int argc, char **argv)
186{
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800187 char *id = NULL;
188 int family;
189
vadimk093b7642014-10-20 12:25:17 +0300190 ipmroute_reset_filter(0);
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800191 if (preferred_family == AF_UNSPEC)
192 family = AF_INET;
193 else
194 family = AF_INET6;
195 if (family == AF_INET) {
196 filter.af = RTNL_FAMILY_IPMR;
197 filter.tb = RT_TABLE_DEFAULT; /* for backward compatibility */
198 } else
199 filter.af = RTNL_FAMILY_IP6MR;
200
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 while (argc > 0) {
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800202 if (matches(*argv, "table") == 0) {
203 __u32 tid;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700204
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000205 NEXT_ARG();
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800206 if (rtnl_rttable_a2n(&tid, *argv)) {
207 if (strcmp(*argv, "all") == 0) {
208 filter.tb = 0;
209 } else if (strcmp(*argv, "help") == 0) {
210 usage();
211 } else {
212 invarg("table id value is invalid\n", *argv);
213 }
214 } else
215 filter.tb = tid;
216 } else if (strcmp(*argv, "iif") == 0) {
217 NEXT_ARG();
218 id = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000219 } else if (matches(*argv, "from") == 0) {
220 NEXT_ARG();
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800221 get_prefix(&filter.msrc, *argv, family);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000222 } else {
223 if (strcmp(*argv, "to") == 0) {
224 NEXT_ARG();
225 }
226 if (matches(*argv, "help") == 0)
227 usage();
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800228 get_prefix(&filter.mdst, *argv, family);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229 }
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800230 argc--; argv++;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000231 }
232
Nicolas Dichtele34d3dc2012-12-14 10:08:17 -0800233 ll_init_map(&rth);
234
235 if (id) {
236 int idx;
237
238 if ((idx = ll_name_to_index(id)) == 0) {
239 fprintf(stderr, "Cannot find device \"%s\"\n", id);
240 return -1;
241 }
242 filter.iif = idx;
243 }
244
245 if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) {
246 perror("Cannot send dump request");
247 return 1;
248 }
249
250 if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
251 fprintf(stderr, "Dump terminated\n");
252 exit(1);
253 }
254
255 exit(0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000256}
257
258int do_multiroute(int argc, char **argv)
259{
260 if (argc < 1)
261 return mroute_list(0, NULL);
262#if 0
263 if (matches(*argv, "add") == 0)
264 return mroute_modify(RTM_NEWADDR, argc-1, argv+1);
265 if (matches(*argv, "delete") == 0)
266 return mroute_modify(RTM_DELADDR, argc-1, argv+1);
267 if (matches(*argv, "get") == 0)
268 return mroute_get(argc-1, argv+1);
269#endif
270 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
271 || matches(*argv, "lst") == 0)
272 return mroute_list(argc-1, argv+1);
273 if (matches(*argv, "help") == 0)
274 usage();
275 fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
276 exit(-1);
277}