• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	int tb;
49 	int af;
50 	int iif;
51 	inet_prefix mdst;
52 	inet_prefix msrc;
53 } filter;
54 
print_mroute(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)55 int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
56 {
57 	FILE *fp = (FILE *)arg;
58 	struct rtmsg *r = NLMSG_DATA(n);
59 	int len = n->nlmsg_len;
60 	struct rtattr *tb[RTA_MAX+1];
61 	char obuf[256];
62 
63 	SPRINT_BUF(b1);
64 	__u32 table;
65 	int iif = 0;
66 	int family;
67 
68 	if ((n->nlmsg_type != RTM_NEWROUTE &&
69 	     n->nlmsg_type != RTM_DELROUTE)) {
70 		fprintf(stderr, "Not a multicast route: %08x %08x %08x\n",
71 			n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
72 		return 0;
73 	}
74 	len -= NLMSG_LENGTH(sizeof(*r));
75 	if (len < 0) {
76 		fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
77 		return -1;
78 	}
79 	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;
83 	}
84 
85 	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
86 	table = rtm_get_table(r, tb);
87 
88 	if (filter.tb > 0 && filter.tb != table)
89 		return 0;
90 
91 	if (tb[RTA_IIF])
92 		iif = rta_getattr_u32(tb[RTA_IIF]);
93 	if (filter.iif && filter.iif != iif)
94 		return 0;
95 
96 	if (filter.af && filter.af != r->rtm_family)
97 		return 0;
98 
99 	if (tb[RTA_DST] && filter.mdst.bitlen > 0) {
100 		inet_prefix dst = { .family = r->rtm_family };
101 
102 		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) {
108 		inet_prefix src = { .family = r->rtm_family };
109 
110 		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 	}
114 
115 	family = get_real_family(r->rtm_type, r->rtm_family);
116 
117 	if (n->nlmsg_type == RTM_DELROUTE)
118 		fprintf(fp, "Deleted ");
119 
120 	if (tb[RTA_SRC])
121 		len = snprintf(obuf, sizeof(obuf),
122 			       "(%s, ", rt_addr_n2a_rta(family, tb[RTA_SRC]));
123 	else
124 		len = sprintf(obuf, "(unknown, ");
125 	if (tb[RTA_DST])
126 		snprintf(obuf + len, sizeof(obuf) - len,
127 			 "%s)", rt_addr_n2a_rta(family, tb[RTA_DST]));
128 	else
129 		snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
130 
131 	fprintf(fp, "%-32s Iif: ", obuf);
132 	if (iif)
133 		fprintf(fp, "%-10s ", ll_index_to_name(iif));
134 	else
135 		fprintf(fp, "unresolved ");
136 
137 	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;
152 			}
153 			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);
160 		}
161 	}
162 	fprintf(fp, " State: %s",
163 		r->rtm_flags & RTNH_F_UNRESOLVED ? "unresolved" : "resolved");
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 	if (show_stats && tb[RTA_EXPIRES]) {
175 		struct timeval tv;
176 
177 		__jiffies_to_tv(&tv, rta_getattr_u64(tb[RTA_EXPIRES]));
178 		fprintf(fp, ", Age %4i.%.2i", (int)tv.tv_sec,
179 			(int)tv.tv_usec/10000);
180 	}
181 
182 	if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
183 		fprintf(fp, " Table: %s",
184 			rtnl_rttable_n2a(table, b1, sizeof(b1)));
185 
186 	fprintf(fp, "\n");
187 	fflush(fp);
188 	return 0;
189 }
190 
ipmroute_reset_filter(int ifindex)191 void ipmroute_reset_filter(int ifindex)
192 {
193 	memset(&filter, 0, sizeof(filter));
194 	filter.mdst.bitlen = -1;
195 	filter.msrc.bitlen = -1;
196 	filter.iif = ifindex;
197 }
198 
mroute_list(int argc,char ** argv)199 static int mroute_list(int argc, char **argv)
200 {
201 	char *id = NULL;
202 	int family;
203 
204 	ipmroute_reset_filter(0);
205 	if (preferred_family == AF_UNSPEC)
206 		family = AF_INET;
207 	else
208 		family = AF_INET6;
209 	if (family == AF_INET) {
210 		filter.af = RTNL_FAMILY_IPMR;
211 		filter.tb = RT_TABLE_DEFAULT;  /* for backward compatibility */
212 	} else
213 		filter.af = RTNL_FAMILY_IP6MR;
214 
215 	while (argc > 0) {
216 		if (matches(*argv, "table") == 0) {
217 			__u32 tid;
218 
219 			NEXT_ARG();
220 			if (rtnl_rttable_a2n(&tid, *argv)) {
221 				if (strcmp(*argv, "all") == 0) {
222 					filter.tb = 0;
223 				} else if (strcmp(*argv, "help") == 0) {
224 					usage();
225 				} else {
226 					invarg("table id value is invalid\n", *argv);
227 				}
228 			} else
229 				filter.tb = tid;
230 		} else if (strcmp(*argv, "iif") == 0) {
231 			NEXT_ARG();
232 			id = *argv;
233 		} else if (matches(*argv, "from") == 0) {
234 			NEXT_ARG();
235 			get_prefix(&filter.msrc, *argv, family);
236 		} else {
237 			if (strcmp(*argv, "to") == 0) {
238 				NEXT_ARG();
239 			}
240 			if (matches(*argv, "help") == 0)
241 				usage();
242 			get_prefix(&filter.mdst, *argv, family);
243 		}
244 		argc--; argv++;
245 	}
246 
247 	ll_init_map(&rth);
248 
249 	if (id)  {
250 		int idx;
251 
252 		if ((idx = ll_name_to_index(id)) == 0) {
253 			fprintf(stderr, "Cannot find device \"%s\"\n", id);
254 			return -1;
255 		}
256 		filter.iif = idx;
257 	}
258 
259 	if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) {
260 		perror("Cannot send dump request");
261 		return 1;
262 	}
263 
264 	if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
265 		fprintf(stderr, "Dump terminated\n");
266 		exit(1);
267 	}
268 
269 	exit(0);
270 }
271 
do_multiroute(int argc,char ** argv)272 int do_multiroute(int argc, char **argv)
273 {
274 	if (argc < 1)
275 		return mroute_list(0, NULL);
276 #if 0
277 	if (matches(*argv, "add") == 0)
278 		return mroute_modify(RTM_NEWADDR, argc-1, argv+1);
279 	if (matches(*argv, "delete") == 0)
280 		return mroute_modify(RTM_DELADDR, argc-1, argv+1);
281 	if (matches(*argv, "get") == 0)
282 		return mroute_get(argc-1, argv+1);
283 #endif
284 	if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
285 	    || matches(*argv, "lst") == 0)
286 		return mroute_list(argc-1, argv+1);
287 	if (matches(*argv, "help") == 0)
288 		usage();
289 	fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
290 	exit(-1);
291 }
292