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