]> git.proxmox.com Git - mirror_iproute2.git/blob - bridge/mdb.c
man: macsec: fix macsec related typos
[mirror_iproute2.git] / bridge / mdb.c
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
27 static unsigned int filter_index, filter_vlan;
28
29 static void usage(void)
30 {
31 fprintf(stderr, "Usage: bridge mdb { add | del } dev DEV port PORT grp GROUP [permanent | temp] [vid VID]\n");
32 fprintf(stderr, " bridge mdb {show} [ dev DEV ] [ vid VID ]\n");
33 exit(-1);
34 }
35
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)
63 {
64 uint32_t *port_ifindex;
65 struct rtattr *i;
66 int rem;
67
68 if (!show_stats)
69 fprintf(f, "router ports on %s: ", ll_index_to_name(brifidx));
70
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);
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 }
83 }
84 if (!show_stats)
85 fprintf(f, "\n");
86 }
87
88 static void print_mdb_entry(FILE *f, int ifindex, struct br_mdb_entry *e,
89 struct nlmsghdr *n, struct rtattr **tb)
90 {
91 SPRINT_BUF(abuf);
92 const void *src;
93 int af;
94
95 if (filter_vlan && e->vid != filter_vlan)
96 return;
97 af = e->addr.proto == htons(ETH_P_IP) ? AF_INET : AF_INET6;
98 src = af == AF_INET ? (const void *)&e->addr.u.ip4 :
99 (const void *)&e->addr.u.ip6;
100 if (n->nlmsg_type == RTM_DELMDB)
101 fprintf(f, "Deleted ");
102 fprintf(f, "dev %s port %s grp %s %s %s", ll_index_to_name(ifindex),
103 ll_index_to_name(e->ifindex),
104 inet_ntop(af, src, abuf, sizeof(abuf)),
105 (e->state & MDB_PERMANENT) ? "permanent" : "temp",
106 (e->flags & MDB_FLAGS_OFFLOAD) ? "offload" : "");
107 if (e->vid)
108 fprintf(f, " vid %hu", e->vid);
109 if (show_stats && tb && tb[MDBA_MDB_EATTR_TIMER]) {
110 struct timeval tv;
111
112 __jiffies_to_tv(&tv, rta_getattr_u32(tb[MDBA_MDB_EATTR_TIMER]));
113 fprintf(f, "%4i.%.2i", (int)tv.tv_sec, (int)tv.tv_usec/10000);
114 }
115 fprintf(f, "\n");
116 }
117
118 static void br_print_mdb_entry(FILE *f, int ifindex, struct rtattr *attr,
119 struct nlmsghdr *n)
120 {
121 struct rtattr *etb[MDBA_MDB_EATTR_MAX + 1];
122 struct br_mdb_entry *e;
123 struct rtattr *i;
124 int rem;
125
126 rem = RTA_PAYLOAD(attr);
127 for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
128 e = RTA_DATA(i);
129 parse_rtattr(etb, MDBA_MDB_EATTR_MAX, MDB_RTA(RTA_DATA(i)),
130 RTA_PAYLOAD(i) - RTA_ALIGN(sizeof(*e)));
131 print_mdb_entry(f, ifindex, e, n, etb);
132 }
133 }
134
135 int print_mdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
136 {
137 FILE *fp = arg;
138 struct br_port_msg *r = NLMSG_DATA(n);
139 int len = n->nlmsg_len;
140 struct rtattr *tb[MDBA_MAX+1], *i;
141
142 if (n->nlmsg_type != RTM_GETMDB && n->nlmsg_type != RTM_NEWMDB && n->nlmsg_type != RTM_DELMDB) {
143 fprintf(stderr, "Not RTM_GETMDB, RTM_NEWMDB or RTM_DELMDB: %08x %08x %08x\n",
144 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
145
146 return 0;
147 }
148
149 len -= NLMSG_LENGTH(sizeof(*r));
150 if (len < 0) {
151 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
152 return -1;
153 }
154
155 if (filter_index && filter_index != r->ifindex)
156 return 0;
157
158 parse_rtattr(tb, MDBA_MAX, MDBA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
159
160 if (tb[MDBA_MDB]) {
161 int rem = RTA_PAYLOAD(tb[MDBA_MDB]);
162
163 for (i = RTA_DATA(tb[MDBA_MDB]); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
164 br_print_mdb_entry(fp, r->ifindex, i, n);
165 }
166
167 if (tb[MDBA_ROUTER]) {
168 if (n->nlmsg_type == RTM_GETMDB) {
169 if (show_details)
170 br_print_router_ports(fp, tb[MDBA_ROUTER],
171 r->ifindex);
172 } else {
173 uint32_t *port_ifindex;
174
175 i = RTA_DATA(tb[MDBA_ROUTER]);
176 port_ifindex = RTA_DATA(i);
177 if (n->nlmsg_type == RTM_DELMDB)
178 fprintf(fp, "Deleted ");
179 fprintf(fp, "router port dev %s master %s\n",
180 ll_index_to_name(*port_ifindex),
181 ll_index_to_name(r->ifindex));
182 }
183 }
184
185 fflush(fp);
186
187 return 0;
188 }
189
190 static int mdb_show(int argc, char **argv)
191 {
192 char *filter_dev = NULL;
193
194 while (argc > 0) {
195 if (strcmp(*argv, "dev") == 0) {
196 NEXT_ARG();
197 if (filter_dev)
198 duparg("dev", *argv);
199 filter_dev = *argv;
200 } else if (strcmp(*argv, "vid") == 0) {
201 NEXT_ARG();
202 if (filter_vlan)
203 duparg("vid", *argv);
204 filter_vlan = atoi(*argv);
205 }
206 argc--; argv++;
207 }
208
209 if (filter_dev) {
210 filter_index = if_nametoindex(filter_dev);
211 if (filter_index == 0) {
212 fprintf(stderr, "Cannot find device \"%s\"\n",
213 filter_dev);
214 return -1;
215 }
216 }
217
218 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETMDB) < 0) {
219 perror("Cannot send dump request");
220 return -1;
221 }
222
223 if (rtnl_dump_filter(&rth, print_mdb, stdout) < 0) {
224 fprintf(stderr, "Dump terminated\n");
225 return -1;
226 }
227
228 return 0;
229 }
230
231 static int mdb_modify(int cmd, int flags, int argc, char **argv)
232 {
233 struct {
234 struct nlmsghdr n;
235 struct br_port_msg bpm;
236 char buf[1024];
237 } req = {
238 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)),
239 .n.nlmsg_flags = NLM_F_REQUEST | flags,
240 .n.nlmsg_type = cmd,
241 .bpm.family = PF_BRIDGE,
242 };
243 struct br_mdb_entry entry = {};
244 char *d = NULL, *p = NULL, *grp = NULL;
245 short vid = 0;
246
247 while (argc > 0) {
248 if (strcmp(*argv, "dev") == 0) {
249 NEXT_ARG();
250 d = *argv;
251 } else if (strcmp(*argv, "grp") == 0) {
252 NEXT_ARG();
253 grp = *argv;
254 } else if (strcmp(*argv, "port") == 0) {
255 NEXT_ARG();
256 p = *argv;
257 } else if (strcmp(*argv, "permanent") == 0) {
258 if (cmd == RTM_NEWMDB)
259 entry.state |= MDB_PERMANENT;
260 } else if (strcmp(*argv, "temp") == 0) {
261 ;/* nothing */
262 } else if (strcmp(*argv, "vid") == 0) {
263 NEXT_ARG();
264 vid = atoi(*argv);
265 } else {
266 if (matches(*argv, "help") == 0)
267 usage();
268 }
269 argc--; argv++;
270 }
271
272 if (d == NULL || grp == NULL || p == NULL) {
273 fprintf(stderr, "Device, group address and port name are required arguments.\n");
274 return -1;
275 }
276
277 req.bpm.ifindex = ll_name_to_index(d);
278 if (req.bpm.ifindex == 0) {
279 fprintf(stderr, "Cannot find device \"%s\"\n", d);
280 return -1;
281 }
282
283 entry.ifindex = ll_name_to_index(p);
284 if (entry.ifindex == 0) {
285 fprintf(stderr, "Cannot find device \"%s\"\n", p);
286 return -1;
287 }
288
289 if (!inet_pton(AF_INET, grp, &entry.addr.u.ip4)) {
290 if (!inet_pton(AF_INET6, grp, &entry.addr.u.ip6)) {
291 fprintf(stderr, "Invalid address \"%s\"\n", grp);
292 return -1;
293 } else
294 entry.addr.proto = htons(ETH_P_IPV6);
295 } else
296 entry.addr.proto = htons(ETH_P_IP);
297
298 entry.vid = vid;
299 addattr_l(&req.n, sizeof(req), MDBA_SET_ENTRY, &entry, sizeof(entry));
300
301 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
302 return -1;
303
304 return 0;
305 }
306
307 int do_mdb(int argc, char **argv)
308 {
309 ll_init_map(&rth);
310
311 if (argc > 0) {
312 if (matches(*argv, "add") == 0)
313 return mdb_modify(RTM_NEWMDB, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
314 if (matches(*argv, "delete") == 0)
315 return mdb_modify(RTM_DELMDB, 0, argc-1, argv+1);
316
317 if (matches(*argv, "show") == 0 ||
318 matches(*argv, "lst") == 0 ||
319 matches(*argv, "list") == 0)
320 return mdb_show(argc-1, argv+1);
321 if (matches(*argv, "help") == 0)
322 usage();
323 } else
324 return mdb_show(0, NULL);
325
326 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge mdb help\".\n", *argv);
327 exit(-1);
328 }