]>
Commit | Line | Data |
---|---|---|
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 | |
34 | static void usage(void) __attribute__((noreturn)); | |
35 | ||
36 | static 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 | ||
aba5acdf SH |
47 | struct rtfilter |
48 | { | |
e34d3dcc ND |
49 | int tb; |
50 | int af; | |
51 | int iif; | |
aba5acdf SH |
52 | inet_prefix mdst; |
53 | inet_prefix msrc; | |
54 | } filter; | |
55 | ||
e34d3dcc | 56 | int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
aba5acdf | 57 | { |
e34d3dcc ND |
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 && | |
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]) | |
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] && | |
101 | filter.mdst.bitlen > 0 && | |
102 | inet_addr_match(RTA_DATA(tb[RTA_DST]), &filter.mdst, filter.mdst.bitlen)) | |
103 | return 0; | |
104 | ||
105 | if (tb[RTA_SRC] && | |
106 | filter.msrc.bitlen > 0 && | |
107 | inet_addr_match(RTA_DATA(tb[RTA_SRC]), &filter.msrc, filter.msrc.bitlen)) | |
108 | return 0; | |
109 | ||
110 | family = r->rtm_family == RTNL_FAMILY_IPMR ? AF_INET : AF_INET6; | |
111 | ||
112 | if (n->nlmsg_type == RTM_DELROUTE) | |
113 | fprintf(fp, "Deleted "); | |
114 | ||
115 | if (tb[RTA_SRC]) | |
116 | len = snprintf(obuf, sizeof(obuf), | |
117 | "(%s, ", rt_addr_n2a(family, | |
26dcdf3a | 118 | RTA_PAYLOAD(tb[RTA_SRC]), |
e34d3dcc ND |
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, | |
656111b2 | 125 | "%s)", rt_addr_n2a(family, |
26dcdf3a | 126 | RTA_PAYLOAD(tb[RTA_DST]), |
e34d3dcc ND |
127 | RTA_DATA(tb[RTA_DST]), |
128 | abuf, sizeof(abuf))); | |
129 | else | |
130 | snprintf(obuf + len, sizeof(obuf) - len, "unknown) "); | |
131 | ||
132 | fprintf(fp, "%-32s Iif: ", obuf); | |
133 | if (iif) | |
134 | fprintf(fp, "%-10s ", ll_index_to_name(iif)); | |
135 | else | |
136 | fprintf(fp, "unresolved "); | |
137 | ||
138 | if (tb[RTA_MULTIPATH]) { | |
139 | struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]); | |
140 | int first = 1; | |
141 | ||
142 | len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); | |
143 | ||
144 | for (;;) { | |
145 | if (len < sizeof(*nh)) | |
146 | break; | |
147 | if (nh->rtnh_len > len) | |
148 | break; | |
149 | ||
150 | if (first) { | |
151 | fprintf(fp, "Oifs: "); | |
152 | first = 0; | |
aba5acdf | 153 | } |
e34d3dcc ND |
154 | fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex)); |
155 | if (nh->rtnh_hops > 1) | |
156 | fprintf(fp, "(ttl %d) ", nh->rtnh_hops); | |
157 | else | |
158 | fprintf(fp, " "); | |
159 | len -= NLMSG_ALIGN(nh->rtnh_len); | |
160 | nh = RTNH_NEXT(nh); | |
aba5acdf | 161 | } |
aba5acdf | 162 | } |
e34d3dcc ND |
163 | if (show_stats && tb[RTA_MFC_STATS]) { |
164 | struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]); | |
165 | ||
166 | fprintf(fp, "%s %"PRIu64" packets, %"PRIu64" bytes", _SL_, | |
167 | (uint64_t)mfcs->mfcs_packets, | |
168 | (uint64_t)mfcs->mfcs_bytes); | |
169 | if (mfcs->mfcs_wrong_if) | |
170 | fprintf(fp, ", %"PRIu64" arrived on wrong iif.", | |
171 | (uint64_t)mfcs->mfcs_wrong_if); | |
172 | } | |
173 | fprintf(fp, "\n"); | |
174 | fflush(fp); | |
175 | return 0; | |
aba5acdf SH |
176 | } |
177 | ||
093b7646 | 178 | void ipmroute_reset_filter(int ifindex) |
e34d3dcc ND |
179 | { |
180 | memset(&filter, 0, sizeof(filter)); | |
181 | filter.mdst.bitlen = -1; | |
182 | filter.msrc.bitlen = -1; | |
093b7646 | 183 | filter.iif = ifindex; |
e34d3dcc | 184 | } |
aba5acdf SH |
185 | |
186 | static int mroute_list(int argc, char **argv) | |
187 | { | |
e34d3dcc ND |
188 | char *id = NULL; |
189 | int family; | |
190 | ||
093b7646 | 191 | ipmroute_reset_filter(0); |
e34d3dcc ND |
192 | if (preferred_family == AF_UNSPEC) |
193 | family = AF_INET; | |
194 | else | |
195 | family = AF_INET6; | |
196 | if (family == AF_INET) { | |
197 | filter.af = RTNL_FAMILY_IPMR; | |
198 | filter.tb = RT_TABLE_DEFAULT; /* for backward compatibility */ | |
199 | } else | |
200 | filter.af = RTNL_FAMILY_IP6MR; | |
201 | ||
aba5acdf | 202 | while (argc > 0) { |
e34d3dcc ND |
203 | if (matches(*argv, "table") == 0) { |
204 | __u32 tid; | |
aba5acdf | 205 | NEXT_ARG(); |
e34d3dcc ND |
206 | if (rtnl_rttable_a2n(&tid, *argv)) { |
207 | if (strcmp(*argv, "all") == 0) { | |
208 | filter.tb = 0; | |
209 | } else if (strcmp(*argv, "help") == 0) { | |
210 | usage(); | |
211 | } else { | |
212 | invarg("table id value is invalid\n", *argv); | |
213 | } | |
214 | } else | |
215 | filter.tb = tid; | |
216 | } else if (strcmp(*argv, "iif") == 0) { | |
217 | NEXT_ARG(); | |
218 | id = *argv; | |
aba5acdf SH |
219 | } else if (matches(*argv, "from") == 0) { |
220 | NEXT_ARG(); | |
e34d3dcc | 221 | get_prefix(&filter.msrc, *argv, family); |
aba5acdf SH |
222 | } else { |
223 | if (strcmp(*argv, "to") == 0) { | |
224 | NEXT_ARG(); | |
225 | } | |
226 | if (matches(*argv, "help") == 0) | |
227 | usage(); | |
e34d3dcc | 228 | get_prefix(&filter.mdst, *argv, family); |
aba5acdf | 229 | } |
e34d3dcc | 230 | argc--; argv++; |
aba5acdf SH |
231 | } |
232 | ||
e34d3dcc ND |
233 | ll_init_map(&rth); |
234 | ||
235 | if (id) { | |
236 | int idx; | |
237 | ||
238 | if ((idx = ll_name_to_index(id)) == 0) { | |
239 | fprintf(stderr, "Cannot find device \"%s\"\n", id); | |
240 | return -1; | |
241 | } | |
242 | filter.iif = idx; | |
243 | } | |
244 | ||
245 | if (rtnl_wilddump_request(&rth, filter.af, RTM_GETROUTE) < 0) { | |
246 | perror("Cannot send dump request"); | |
247 | return 1; | |
248 | } | |
249 | ||
250 | if (rtnl_dump_filter(&rth, print_mroute, stdout) < 0) { | |
251 | fprintf(stderr, "Dump terminated\n"); | |
252 | exit(1); | |
253 | } | |
254 | ||
255 | exit(0); | |
aba5acdf SH |
256 | } |
257 | ||
258 | int do_multiroute(int argc, char **argv) | |
259 | { | |
260 | if (argc < 1) | |
261 | return mroute_list(0, NULL); | |
262 | #if 0 | |
263 | if (matches(*argv, "add") == 0) | |
264 | return mroute_modify(RTM_NEWADDR, argc-1, argv+1); | |
265 | if (matches(*argv, "delete") == 0) | |
266 | return mroute_modify(RTM_DELADDR, argc-1, argv+1); | |
267 | if (matches(*argv, "get") == 0) | |
268 | return mroute_get(argc-1, argv+1); | |
269 | #endif | |
270 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
271 | || matches(*argv, "lst") == 0) | |
272 | return mroute_list(argc-1, argv+1); | |
273 | if (matches(*argv, "help") == 0) | |
274 | usage(); | |
275 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip mroute help\".\n", *argv); | |
276 | exit(-1); | |
277 | } |