]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipmroute.c
ip: code cleanup
[mirror_iproute2.git] / ip / ipmroute.c
CommitLineData
aba5acdf
SH
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>
e34d3dcc 18#include <inttypes.h>
aba5acdf
SH
19#include <sys/ioctl.h>
20#include <sys/socket.h>
03156410
SH
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <string.h>
24
aba5acdf
SH
25#include <linux/netdevice.h>
26#include <linux/if.h>
27#include <linux/if_arp.h>
28#include <linux/sockios.h>
aba5acdf 29
e34d3dcc 30#include <rt_names.h>
aba5acdf 31#include "utils.h"
e34d3dcc 32#include "ip_common.h"
aba5acdf
SH
33
34static void usage(void) __attribute__((noreturn));
35
36static void usage(void)
37{
e34d3dcc 38 fprintf(stderr, "Usage: ip mroute show [ [ to ] PREFIX ] [ from PREFIX ] [ iif DEVICE ]\n");
2a898320
ND
39 fprintf(stderr, " [ table TABLE_ID ]\n");
40 fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n");
aba5acdf
SH
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
56f5daac 47struct rtfilter {
e34d3dcc
ND
48 int tb;
49 int af;
50 int iif;
aba5acdf
SH
51 inet_prefix mdst;
52 inet_prefix msrc;
53} filter;
54
e34d3dcc 55int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
aba5acdf 56{
56f5daac 57 FILE *fp = (FILE *)arg;
e34d3dcc
ND
58 struct rtmsg *r = NLMSG_DATA(n);
59 int len = n->nlmsg_len;
56f5daac 60 struct rtattr *tb[RTA_MAX+1];
e34d3dcc
ND
61 char abuf[256];
62 char obuf[256];
56f5daac 63
e34d3dcc
ND
64 SPRINT_BUF(b1);
65 __u32 table;
66 int iif = 0;
67 int family;
68
69 if ((n->nlmsg_type != RTM_NEWROUTE &&
505f9186 70 n->nlmsg_type != RTM_DELROUTE)) {
e34d3dcc
ND
71 fprintf(stderr, "Not a multicast route: %08x %08x %08x\n",
72 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
73 return 0;
67ef60a2 74 }
e34d3dcc
ND
75 len -= NLMSG_LENGTH(sizeof(*r));
76 if (len < 0) {
77 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
78 return -1;
aba5acdf 79 }
e34d3dcc
ND
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;
e588a7db 84 }
aba5acdf 85
e34d3dcc
ND
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])
56f5daac 93 iif = *(int *)RTA_DATA(tb[RTA_IIF]);
e34d3dcc
ND
94 if (filter.iif && filter.iif != iif)
95 return 0;
96
97 if (filter.af && filter.af != r->rtm_family)
98 return 0;
99
45b01c46
MR
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 }
e34d3dcc 109
45b01c46
MR
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 }
e34d3dcc
ND
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,
26dcdf3a 128 RTA_PAYLOAD(tb[RTA_SRC]),
e34d3dcc
ND
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,
656111b2 135 "%s)", rt_addr_n2a(family,
26dcdf3a 136 RTA_PAYLOAD(tb[RTA_DST]),
e34d3dcc
ND
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;
aba5acdf 163 }
e34d3dcc
ND
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);
aba5acdf 171 }
aba5acdf 172 }
e34d3dcc
ND
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;
aba5acdf
SH
186}
187
093b7646 188void ipmroute_reset_filter(int ifindex)
e34d3dcc
ND
189{
190 memset(&filter, 0, sizeof(filter));
191 filter.mdst.bitlen = -1;
192 filter.msrc.bitlen = -1;
093b7646 193 filter.iif = ifindex;
e34d3dcc 194}
aba5acdf
SH
195
196static int mroute_list(int argc, char **argv)
197{
e34d3dcc
ND
198 char *id = NULL;
199 int family;
200
093b7646 201 ipmroute_reset_filter(0);
e34d3dcc
ND
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
aba5acdf 212 while (argc > 0) {
e34d3dcc
ND
213 if (matches(*argv, "table") == 0) {
214 __u32 tid;
56f5daac 215
aba5acdf 216 NEXT_ARG();
e34d3dcc
ND
217 if (rtnl_rttable_a2n(&tid, *argv)) {
218 if (strcmp(*argv, "all") == 0) {
219 filter.tb = 0;
220 } else if (strcmp(*argv, "help") == 0) {
221 usage();
222 } else {
223 invarg("table id value is invalid\n", *argv);
224 }
225 } else
226 filter.tb = tid;
227 } else if (strcmp(*argv, "iif") == 0) {
228 NEXT_ARG();
229 id = *argv;
aba5acdf
SH
230 } else if (matches(*argv, "from") == 0) {
231 NEXT_ARG();
e34d3dcc 232 get_prefix(&filter.msrc, *argv, family);
aba5acdf
SH
233 } else {
234 if (strcmp(*argv, "to") == 0) {
235 NEXT_ARG();
236 }
237 if (matches(*argv, "help") == 0)
238 usage();
e34d3dcc 239 get_prefix(&filter.mdst, *argv, family);
aba5acdf 240 }
e34d3dcc 241 argc--; argv++;
aba5acdf
SH
242 }
243
e34d3dcc
ND
244 ll_init_map(&rth);
245
246 if (id) {
247 int idx;
248
249 if ((idx = ll_name_to_index(id)) == 0) {
250 fprintf(stderr, "Cannot find device \"%s\"\n", id);
251 return -1;
252 }
253 filter.iif = idx;
254 }
255
256 if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) {
257 perror("Cannot send dump request");
258 return 1;
259 }
260
261 if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) {
262 fprintf(stderr, "Dump terminated\n");
263 exit(1);
264 }
265
266 exit(0);
aba5acdf
SH
267}
268
269int do_multiroute(int argc, char **argv)
270{
271 if (argc < 1)
272 return mroute_list(0, NULL);
273#if 0
274 if (matches(*argv, "add") == 0)
275 return mroute_modify(RTM_NEWADDR, argc-1, argv+1);
276 if (matches(*argv, "delete") == 0)
277 return mroute_modify(RTM_DELADDR, argc-1, argv+1);
278 if (matches(*argv, "get") == 0)
279 return mroute_get(argc-1, argv+1);
280#endif
281 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
282 || matches(*argv, "lst") == 0)
283 return mroute_list(argc-1, argv+1);
284 if (matches(*argv, "help") == 0)
285 usage();
286 fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv);
287 exit(-1);
288}