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