]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipmroute.c
b4ed9f15fda5e8349fa6188c77d8f105888cabb0
[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 <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
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
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 !(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;
75 }
76 len -= NLMSG_LENGTH(sizeof(*r));
77 if (len < 0) {
78 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
79 return -1;
80 }
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;
85 }
86
87 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
88 table = rtm_get_table(r, tb);
89
90 if (filter.tb > 0 && filter.tb != table)
91 return 0;
92
93 if (tb[RTA_IIF])
94 iif = *(int*)RTA_DATA(tb[RTA_IIF]);
95 if (filter.iif && filter.iif != iif)
96 return 0;
97
98 if (filter.af && filter.af != r->rtm_family)
99 return 0;
100
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;
105
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;
110
111 family = r->rtm_family == RTNL_FAMILY_IPMR ? AF_INET : AF_INET6;
112
113 if (n->nlmsg_type == RTM_DELROUTE)
114 fprintf(fp, "Deleted ");
115
116 if (tb[RTA_SRC])
117 len = snprintf(obuf, sizeof(obuf),
118 "(%s, ", rt_addr_n2a(family,
119 RTA_DATA(tb[RTA_SRC]),
120 abuf, sizeof(abuf)));
121 else
122 len = sprintf(obuf, "(unknown, ");
123 if (tb[RTA_DST])
124 snprintf(obuf + len, sizeof(obuf) - len,
125 "%s)", rt_addr_n2a(family,
126 RTA_DATA(tb[RTA_DST]),
127 abuf, sizeof(abuf)));
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 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;
175 }
176
177 void ipmroute_reset_filter(int ifindex)
178 {
179 memset(&filter, 0, sizeof(filter));
180 filter.mdst.bitlen = -1;
181 filter.msrc.bitlen = -1;
182 filter.iif = ifindex;
183 }
184
185 static int mroute_list(int argc, char **argv)
186 {
187 char *id = NULL;
188 int family;
189
190 ipmroute_reset_filter(0);
191 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
201 while (argc > 0) {
202 if (matches(*argv, "table") == 0) {
203 __u32 tid;
204 NEXT_ARG();
205 if (rtnl_rttable_a2n(&tid, *argv)) {
206 if (strcmp(*argv, "all") == 0) {
207 filter.tb = 0;
208 } else if (strcmp(*argv, "help") == 0) {
209 usage();
210 } else {
211 invarg("table id value is invalid\n", *argv);
212 }
213 } else
214 filter.tb = tid;
215 } else if (strcmp(*argv, "iif") == 0) {
216 NEXT_ARG();
217 id = *argv;
218 } else if (matches(*argv, "from") == 0) {
219 NEXT_ARG();
220 get_prefix(&filter.msrc, *argv, family);
221 } else {
222 if (strcmp(*argv, "to") == 0) {
223 NEXT_ARG();
224 }
225 if (matches(*argv, "help") == 0)
226 usage();
227 get_prefix(&filter.mdst, *argv, family);
228 }
229 argc--; argv++;
230 }
231
232 ll_init_map(&rth);
233
234 if (id) {
235 int idx;
236
237 if ((idx = ll_name_to_index(id)) == 0) {
238 fprintf(stderr, "Cannot find device \"%s\"\n", id);
239 return -1;
240 }
241 filter.iif = idx;
242 }
243
244 if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) {
245 perror("Cannot send dump request");
246 return 1;
247 }
248
249 if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
250 fprintf(stderr, "Dump terminated\n");
251 exit(1);
252 }
253
254 exit(0);
255 }
256
257 int do_multiroute(int argc, char **argv)
258 {
259 if (argc < 1)
260 return mroute_list(0, NULL);
261 #if 0
262 if (matches(*argv, "add") == 0)
263 return mroute_modify(RTM_NEWADDR, argc-1, argv+1);
264 if (matches(*argv, "delete") == 0)
265 return mroute_modify(RTM_DELADDR, argc-1, argv+1);
266 if (matches(*argv, "get") == 0)
267 return mroute_get(argc-1, argv+1);
268 #endif
269 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
270 || matches(*argv, "lst") == 0)
271 return mroute_list(argc-1, argv+1);
272 if (matches(*argv, "help") == 0)
273 usage();
274 fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
275 exit(-1);
276 }