]>
Commit | Line | Data |
---|---|---|
e06c7f7e CW |
1 | /* |
2 | * Get mdb table with netlink | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | #include <fcntl.h> | |
9 | #include <sys/socket.h> | |
10 | #include <net/if.h> | |
11 | #include <netinet/in.h> | |
12 | #include <linux/if_bridge.h> | |
13 | #include <linux/if_ether.h> | |
14 | #include <string.h> | |
15 | #include <arpa/inet.h> | |
16 | ||
17 | #include "libnetlink.h" | |
18 | #include "br_common.h" | |
19 | #include "rt_names.h" | |
20 | #include "utils.h" | |
21 | ||
22 | #ifndef MDBA_RTA | |
23 | #define MDBA_RTA(r) \ | |
24 | ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct br_port_msg)))) | |
25 | #endif | |
26 | ||
9dca899b | 27 | static unsigned int filter_index; |
e06c7f7e CW |
28 | |
29 | static void usage(void) | |
30 | { | |
6aac8617 | 31 | fprintf(stderr, "Usage: bridge mdb { add | del } dev DEV port PORT grp GROUP [permanent | temp] [vid VID]\n"); |
e06c7f7e CW |
32 | fprintf(stderr, " bridge mdb {show} [ dev DEV ]\n"); |
33 | exit(-1); | |
34 | } | |
35 | ||
ba037267 NA |
36 | static bool is_temp_mcast_rtr(__u8 type) |
37 | { | |
38 | return type == MDB_RTR_TYPE_TEMP_QUERY || type == MDB_RTR_TYPE_TEMP; | |
39 | } | |
40 | ||
41 | static void __print_router_port_stats(FILE *f, struct rtattr *pattr) | |
42 | { | |
43 | struct rtattr *tb[MDBA_ROUTER_PATTR_MAX + 1]; | |
44 | struct timeval tv; | |
45 | __u8 type; | |
46 | ||
47 | parse_rtattr(tb, MDBA_ROUTER_PATTR_MAX, MDB_RTR_RTA(RTA_DATA(pattr)), | |
48 | RTA_PAYLOAD(pattr) - RTA_ALIGN(sizeof(uint32_t))); | |
49 | if (tb[MDBA_ROUTER_PATTR_TIMER]) { | |
50 | __jiffies_to_tv(&tv, | |
51 | rta_getattr_u32(tb[MDBA_ROUTER_PATTR_TIMER])); | |
52 | fprintf(f, " %4i.%.2i", | |
53 | (int)tv.tv_sec, (int)tv.tv_usec/10000); | |
54 | } | |
55 | if (tb[MDBA_ROUTER_PATTR_TYPE]) { | |
56 | type = rta_getattr_u8(tb[MDBA_ROUTER_PATTR_TYPE]); | |
57 | fprintf(f, " %s", | |
58 | is_temp_mcast_rtr(type) ? "temp" : "permanent"); | |
59 | } | |
60 | } | |
61 | ||
62 | static void br_print_router_ports(FILE *f, struct rtattr *attr, __u32 brifidx) | |
e06c7f7e CW |
63 | { |
64 | uint32_t *port_ifindex; | |
65 | struct rtattr *i; | |
66 | int rem; | |
67 | ||
ba037267 NA |
68 | if (!show_stats) |
69 | fprintf(f, "router ports on %s: ", ll_index_to_name(brifidx)); | |
70 | ||
e06c7f7e CW |
71 | rem = RTA_PAYLOAD(attr); |
72 | for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
73 | port_ifindex = RTA_DATA(i); | |
ba037267 NA |
74 | if (show_stats) { |
75 | fprintf(f, "router ports on %s: %s", | |
76 | ll_index_to_name(brifidx), | |
77 | ll_index_to_name(*port_ifindex)); | |
78 | __print_router_port_stats(f, i); | |
79 | fprintf(f, "\n"); | |
80 | } else { | |
81 | fprintf(f, "%s ", ll_index_to_name(*port_ifindex)); | |
82 | } | |
e06c7f7e | 83 | } |
ba037267 NA |
84 | if (!show_stats) |
85 | fprintf(f, "\n"); | |
e06c7f7e CW |
86 | } |
87 | ||
90d73159 | 88 | static void print_mdb_entry(FILE *f, int ifindex, struct br_mdb_entry *e, |
05d4f64d | 89 | struct nlmsghdr *n, struct rtattr **tb) |
e06c7f7e CW |
90 | { |
91 | SPRINT_BUF(abuf); | |
6aac8617 NA |
92 | const void *src; |
93 | int af; | |
94 | ||
95 | af = e->addr.proto == htons(ETH_P_IP) ? AF_INET : AF_INET6; | |
96 | src = af == AF_INET ? (const void *)&e->addr.u.ip4 : | |
97 | (const void *)&e->addr.u.ip6; | |
90d73159 NA |
98 | if (n->nlmsg_type == RTM_DELMDB) |
99 | fprintf(f, "Deleted "); | |
29d61fb3 | 100 | fprintf(f, "dev %s port %s grp %s %s %s", ll_index_to_name(ifindex), |
6aac8617 NA |
101 | ll_index_to_name(e->ifindex), |
102 | inet_ntop(af, src, abuf, sizeof(abuf)), | |
29d61fb3 ER |
103 | (e->state & MDB_PERMANENT) ? "permanent" : "temp", |
104 | (e->flags & MDB_FLAGS_OFFLOAD) ? "offload" : ""); | |
6aac8617 NA |
105 | if (e->vid) |
106 | fprintf(f, " vid %hu", e->vid); | |
05d4f64d NA |
107 | if (show_stats && tb && tb[MDBA_MDB_EATTR_TIMER]) { |
108 | struct timeval tv; | |
109 | ||
110 | __jiffies_to_tv(&tv, rta_getattr_u32(tb[MDBA_MDB_EATTR_TIMER])); | |
111 | fprintf(f, "%4i.%.2i", (int)tv.tv_sec, (int)tv.tv_usec/10000); | |
112 | } | |
6aac8617 | 113 | fprintf(f, "\n"); |
e06c7f7e CW |
114 | } |
115 | ||
90d73159 NA |
116 | static void br_print_mdb_entry(FILE *f, int ifindex, struct rtattr *attr, |
117 | struct nlmsghdr *n) | |
e06c7f7e | 118 | { |
05d4f64d NA |
119 | struct rtattr *etb[MDBA_MDB_EATTR_MAX + 1]; |
120 | struct br_mdb_entry *e; | |
e06c7f7e CW |
121 | struct rtattr *i; |
122 | int rem; | |
e06c7f7e CW |
123 | |
124 | rem = RTA_PAYLOAD(attr); | |
125 | for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
126 | e = RTA_DATA(i); | |
05d4f64d NA |
127 | parse_rtattr(etb, MDBA_MDB_EATTR_MAX, MDB_RTA(RTA_DATA(i)), |
128 | RTA_PAYLOAD(i) - RTA_ALIGN(sizeof(*e))); | |
129 | print_mdb_entry(f, ifindex, e, n, etb); | |
e06c7f7e CW |
130 | } |
131 | } | |
132 | ||
133 | int print_mdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
134 | { | |
135 | FILE *fp = arg; | |
136 | struct br_port_msg *r = NLMSG_DATA(n); | |
137 | int len = n->nlmsg_len; | |
6b4867e6 | 138 | struct rtattr *tb[MDBA_MAX+1], *i; |
e06c7f7e | 139 | |
4a4ee616 CW |
140 | if (n->nlmsg_type != RTM_GETMDB && n->nlmsg_type != RTM_NEWMDB && n->nlmsg_type != RTM_DELMDB) { |
141 | fprintf(stderr, "Not RTM_GETMDB, RTM_NEWMDB or RTM_DELMDB: %08x %08x %08x\n", | |
e06c7f7e CW |
142 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | len -= NLMSG_LENGTH(sizeof(*r)); | |
148 | if (len < 0) { | |
149 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
150 | return -1; | |
151 | } | |
152 | ||
153 | if (filter_index && filter_index != r->ifindex) | |
154 | return 0; | |
155 | ||
156 | parse_rtattr(tb, MDBA_MAX, MDBA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
157 | ||
158 | if (tb[MDBA_MDB]) { | |
e06c7f7e CW |
159 | int rem = RTA_PAYLOAD(tb[MDBA_MDB]); |
160 | ||
161 | for (i = RTA_DATA(tb[MDBA_MDB]); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) | |
90d73159 | 162 | br_print_mdb_entry(fp, r->ifindex, i, n); |
e06c7f7e CW |
163 | } |
164 | ||
165 | if (tb[MDBA_ROUTER]) { | |
6b4867e6 | 166 | if (n->nlmsg_type == RTM_GETMDB) { |
ba037267 NA |
167 | if (show_details) |
168 | br_print_router_ports(fp, tb[MDBA_ROUTER], | |
169 | r->ifindex); | |
6b4867e6 NA |
170 | } else { |
171 | uint32_t *port_ifindex; | |
172 | ||
173 | i = RTA_DATA(tb[MDBA_ROUTER]); | |
174 | port_ifindex = RTA_DATA(i); | |
175 | if (n->nlmsg_type == RTM_DELMDB) | |
176 | fprintf(fp, "Deleted "); | |
177 | fprintf(fp, "router port dev %s master %s\n", | |
178 | ll_index_to_name(*port_ifindex), | |
179 | ll_index_to_name(r->ifindex)); | |
e06c7f7e CW |
180 | } |
181 | } | |
182 | ||
4d45bf3b WK |
183 | fflush(fp); |
184 | ||
e06c7f7e CW |
185 | return 0; |
186 | } | |
187 | ||
188 | static int mdb_show(int argc, char **argv) | |
189 | { | |
190 | char *filter_dev = NULL; | |
191 | ||
192 | while (argc > 0) { | |
193 | if (strcmp(*argv, "dev") == 0) { | |
194 | NEXT_ARG(); | |
195 | if (filter_dev) | |
196 | duparg("dev", *argv); | |
197 | filter_dev = *argv; | |
198 | } | |
199 | argc--; argv++; | |
200 | } | |
201 | ||
202 | if (filter_dev) { | |
203 | filter_index = if_nametoindex(filter_dev); | |
204 | if (filter_index == 0) { | |
205 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
206 | filter_dev); | |
207 | return -1; | |
208 | } | |
209 | } | |
210 | ||
211 | if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETMDB) < 0) { | |
212 | perror("Cannot send dump request"); | |
42ecedd4 | 213 | return -1; |
e06c7f7e CW |
214 | } |
215 | ||
216 | if (rtnl_dump_filter(&rth, print_mdb, stdout) < 0) { | |
217 | fprintf(stderr, "Dump terminated\n"); | |
42ecedd4 | 218 | return -1; |
e06c7f7e CW |
219 | } |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
9dca6767 CW |
224 | static int mdb_modify(int cmd, int flags, int argc, char **argv) |
225 | { | |
226 | struct { | |
227 | struct nlmsghdr n; | |
228 | struct br_port_msg bpm; | |
229 | char buf[1024]; | |
230 | } req; | |
231 | struct br_mdb_entry entry; | |
232 | char *d = NULL, *p = NULL, *grp = NULL; | |
6aac8617 | 233 | short vid = 0; |
9dca6767 CW |
234 | |
235 | memset(&req, 0, sizeof(req)); | |
236 | memset(&entry, 0, sizeof(entry)); | |
237 | ||
238 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)); | |
239 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
240 | req.n.nlmsg_type = cmd; | |
241 | req.bpm.family = PF_BRIDGE; | |
242 | ||
243 | while (argc > 0) { | |
244 | if (strcmp(*argv, "dev") == 0) { | |
245 | NEXT_ARG(); | |
246 | d = *argv; | |
247 | } else if (strcmp(*argv, "grp") == 0) { | |
248 | NEXT_ARG(); | |
249 | grp = *argv; | |
d8b75d1a CW |
250 | } else if (strcmp(*argv, "port") == 0) { |
251 | NEXT_ARG(); | |
252 | p = *argv; | |
253 | } else if (strcmp(*argv, "permanent") == 0) { | |
254 | if (cmd == RTM_NEWMDB) | |
255 | entry.state |= MDB_PERMANENT; | |
256 | } else if (strcmp(*argv, "temp") == 0) { | |
257 | ;/* nothing */ | |
6aac8617 NA |
258 | } else if (strcmp(*argv, "vid") == 0) { |
259 | NEXT_ARG(); | |
260 | vid = atoi(*argv); | |
9dca6767 | 261 | } else { |
9dca6767 CW |
262 | if (matches(*argv, "help") == 0) |
263 | usage(); | |
264 | } | |
265 | argc--; argv++; | |
266 | } | |
267 | ||
268 | if (d == NULL || grp == NULL || p == NULL) { | |
269 | fprintf(stderr, "Device, group address and port name are required arguments.\n"); | |
42ecedd4 | 270 | return -1; |
9dca6767 CW |
271 | } |
272 | ||
273 | req.bpm.ifindex = ll_name_to_index(d); | |
274 | if (req.bpm.ifindex == 0) { | |
275 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | entry.ifindex = ll_name_to_index(p); | |
280 | if (entry.ifindex == 0) { | |
281 | fprintf(stderr, "Cannot find device \"%s\"\n", p); | |
282 | return -1; | |
283 | } | |
284 | ||
285 | if (!inet_pton(AF_INET, grp, &entry.addr.u.ip4)) { | |
286 | if (!inet_pton(AF_INET6, grp, &entry.addr.u.ip6)) { | |
287 | fprintf(stderr, "Invalid address \"%s\"\n", grp); | |
288 | return -1; | |
289 | } else | |
290 | entry.addr.proto = htons(ETH_P_IPV6); | |
291 | } else | |
292 | entry.addr.proto = htons(ETH_P_IP); | |
293 | ||
6aac8617 | 294 | entry.vid = vid; |
9dca6767 CW |
295 | addattr_l(&req.n, sizeof(req), MDBA_SET_ENTRY, &entry, sizeof(entry)); |
296 | ||
c079e121 | 297 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
42ecedd4 | 298 | return -1; |
9dca6767 CW |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
e06c7f7e CW |
303 | int do_mdb(int argc, char **argv) |
304 | { | |
305 | ll_init_map(&rth); | |
306 | ||
307 | if (argc > 0) { | |
9dca6767 CW |
308 | if (matches(*argv, "add") == 0) |
309 | return mdb_modify(RTM_NEWMDB, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
310 | if (matches(*argv, "delete") == 0) | |
311 | return mdb_modify(RTM_DELMDB, 0, argc-1, argv+1); | |
312 | ||
e06c7f7e CW |
313 | if (matches(*argv, "show") == 0 || |
314 | matches(*argv, "lst") == 0 || | |
315 | matches(*argv, "list") == 0) | |
316 | return mdb_show(argc-1, argv+1); | |
317 | if (matches(*argv, "help") == 0) | |
318 | usage(); | |
319 | } else | |
320 | return mdb_show(0, NULL); | |
321 | ||
322 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge mdb help\".\n", *argv); | |
323 | exit(-1); | |
324 | } |