1
2
3
4
5
6
7
8
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <netdb.h>
14#include <time.h>
15#include <fcntl.h>
16#include <sys/socket.h>
17#include <sys/time.h>
18#include <net/if.h>
19#include <netinet/in.h>
20#include <linux/if_bridge.h>
21#include <linux/if_ether.h>
22#include <linux/neighbour.h>
23#include <string.h>
24#include <limits.h>
25#include <stdbool.h>
26
27#include "json_print.h"
28#include "libnetlink.h"
29#include "br_common.h"
30#include "rt_names.h"
31#include "utils.h"
32
33static unsigned int filter_index, filter_dynamic, filter_master,
34 filter_state, filter_vlan;
35
36static void usage(void)
37{
38 fprintf(stderr,
39 "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV\n"
40 " [ self ] [ master ] [ use ] [ router ] [ extern_learn ]\n"
41 " [ sticky ] [ local | static | dynamic ] [ vlan VID ]\n"
42 " { [ dst IPADDR ] [ port PORT] [ vni VNI ] | [ nhid NHID ] }\n"
43 " [ via DEV ] [ src_vni VNI ]\n"
44 " bridge fdb [ show [ br BRDEV ] [ brport DEV ] [ vlan VID ]\n"
45 " [ state STATE ] [ dynamic ] ]\n"
46 " bridge fdb get [ to ] LLADDR [ br BRDEV ] { brport | dev } DEV\n"
47 " [ vlan VID ] [ vni VNI ] [ self ] [ master ] [ dynamic ]\n");
48 exit(-1);
49}
50
51static const char *state_n2a(unsigned int s)
52{
53 static char buf[32];
54
55 if (s & NUD_PERMANENT)
56 return "permanent";
57
58 if (s & NUD_NOARP)
59 return "static";
60
61 if (s & NUD_STALE)
62 return "stale";
63
64 if (s & NUD_REACHABLE)
65 return "";
66
67 if (is_json_context())
68 sprintf(buf, "%#x", s);
69 else
70 sprintf(buf, "state=%#x", s);
71 return buf;
72}
73
74static int state_a2n(unsigned int *s, const char *arg)
75{
76 if (matches(arg, "permanent") == 0)
77 *s = NUD_PERMANENT;
78 else if (matches(arg, "static") == 0 || matches(arg, "temp") == 0)
79 *s = NUD_NOARP;
80 else if (matches(arg, "stale") == 0)
81 *s = NUD_STALE;
82 else if (matches(arg, "reachable") == 0 || matches(arg, "dynamic") == 0)
83 *s = NUD_REACHABLE;
84 else if (strcmp(arg, "all") == 0)
85 *s = ~0;
86 else if (get_unsigned(s, arg, 0))
87 return -1;
88
89 return 0;
90}
91
92static void fdb_print_flags(FILE *fp, unsigned int flags)
93{
94 open_json_array(PRINT_JSON,
95 is_json_context() ? "flags" : "");
96
97 if (flags & NTF_SELF)
98 print_string(PRINT_ANY, NULL, "%s ", "self");
99
100 if (flags & NTF_ROUTER)
101 print_string(PRINT_ANY, NULL, "%s ", "router");
102
103 if (flags & NTF_EXT_LEARNED)
104 print_string(PRINT_ANY, NULL, "%s ", "extern_learn");
105
106 if (flags & NTF_OFFLOADED)
107 print_string(PRINT_ANY, NULL, "%s ", "offload");
108
109 if (flags & NTF_MASTER)
110 print_string(PRINT_ANY, NULL, "%s ", "master");
111
112 if (flags & NTF_STICKY)
113 print_string(PRINT_ANY, NULL, "%s ", "sticky");
114
115 close_json_array(PRINT_JSON, NULL);
116}
117
118static void fdb_print_stats(FILE *fp, const struct nda_cacheinfo *ci)
119{
120 static int hz;
121
122 if (!hz)
123 hz = get_user_hz();
124
125 if (is_json_context()) {
126 print_uint(PRINT_JSON, "used", NULL,
127 ci->ndm_used / hz);
128 print_uint(PRINT_JSON, "updated", NULL,
129 ci->ndm_updated / hz);
130 } else {
131 fprintf(fp, "used %d/%d ", ci->ndm_used / hz,
132 ci->ndm_updated / hz);
133
134 }
135}
136
137int print_fdb(struct nlmsghdr *n, void *arg)
138{
139 FILE *fp = arg;
140 struct ndmsg *r = NLMSG_DATA(n);
141 int len = n->nlmsg_len;
142 struct rtattr *tb[NDA_MAX+1];
143 __u16 vid = 0;
144
145 if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
146 fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
147 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
148 return 0;
149 }
150
151 len -= NLMSG_LENGTH(sizeof(*r));
152 if (len < 0) {
153 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
154 return -1;
155 }
156
157 if (r->ndm_family != AF_BRIDGE)
158 return 0;
159
160 if (filter_index && filter_index != r->ndm_ifindex)
161 return 0;
162
163 if (filter_state && !(r->ndm_state & filter_state))
164 return 0;
165
166 parse_rtattr(tb, NDA_MAX, NDA_RTA(r),
167 n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
168
169 if (tb[NDA_VLAN])
170 vid = rta_getattr_u16(tb[NDA_VLAN]);
171
172 if (filter_vlan && filter_vlan != vid)
173 return 0;
174
175 if (filter_dynamic && (r->ndm_state & NUD_PERMANENT))
176 return 0;
177
178 open_json_object(NULL);
179 if (n->nlmsg_type == RTM_DELNEIGH)
180 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
181
182 if (tb[NDA_LLADDR]) {
183 const char *lladdr;
184 SPRINT_BUF(b1);
185
186 lladdr = ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
187 RTA_PAYLOAD(tb[NDA_LLADDR]),
188 ll_index_to_type(r->ndm_ifindex),
189 b1, sizeof(b1));
190
191 print_color_string(PRINT_ANY, COLOR_MAC,
192 "mac", "%s ", lladdr);
193 }
194
195 if (!filter_index && r->ndm_ifindex)
196 print_color_string(PRINT_ANY, COLOR_IFNAME,
197 "ifname", "dev %s ",
198 ll_index_to_name(r->ndm_ifindex));
199
200 if (tb[NDA_DST]) {
201 int family = AF_INET;
202 const char *dst;
203
204 if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr))
205 family = AF_INET6;
206
207 dst = format_host(family,
208 RTA_PAYLOAD(tb[NDA_DST]),
209 RTA_DATA(tb[NDA_DST]));
210
211 print_color_string(PRINT_ANY,
212 ifa_family_color(family),
213 "dst", "dst %s ", dst);
214 }
215
216 if (vid)
217 print_uint(PRINT_ANY,
218 "vlan", "vlan %hu ", vid);
219
220 if (tb[NDA_PORT])
221 print_uint(PRINT_ANY,
222 "port", "port %u ",
223 rta_getattr_be16(tb[NDA_PORT]));
224
225 if (tb[NDA_VNI])
226 print_uint(PRINT_ANY,
227 "vni", "vni %u ",
228 rta_getattr_u32(tb[NDA_VNI]));
229
230 if (tb[NDA_SRC_VNI])
231 print_uint(PRINT_ANY,
232 "src_vni", "src_vni %u ",
233 rta_getattr_u32(tb[NDA_SRC_VNI]));
234
235 if (tb[NDA_IFINDEX]) {
236 unsigned int ifindex = rta_getattr_u32(tb[NDA_IFINDEX]);
237
238 if (tb[NDA_LINK_NETNSID])
239 print_uint(PRINT_ANY,
240 "viaIfIndex", "via ifindex %u ",
241 ifindex);
242 else
243 print_string(PRINT_ANY,
244 "viaIf", "via %s ",
245 ll_index_to_name(ifindex));
246 }
247
248 if (tb[NDA_NH_ID])
249 print_uint(PRINT_ANY, "nhid", "nhid %u ",
250 rta_getattr_u32(tb[NDA_NH_ID]));
251
252 if (tb[NDA_LINK_NETNSID])
253 print_uint(PRINT_ANY,
254 "linkNetNsId", "link-netnsid %d ",
255 rta_getattr_u32(tb[NDA_LINK_NETNSID]));
256
257 if (show_stats && tb[NDA_CACHEINFO])
258 fdb_print_stats(fp, RTA_DATA(tb[NDA_CACHEINFO]));
259
260 fdb_print_flags(fp, r->ndm_flags);
261
262
263 if (tb[NDA_MASTER])
264 print_string(PRINT_ANY, "master", "master %s ",
265 ll_index_to_name(rta_getattr_u32(tb[NDA_MASTER])));
266
267 print_string(PRINT_ANY, "state", "%s\n",
268 state_n2a(r->ndm_state));
269 close_json_object();
270 fflush(fp);
271 return 0;
272}
273
274static int fdb_linkdump_filter(struct nlmsghdr *nlh, int reqlen)
275{
276 int err;
277
278 if (filter_index) {
279 struct ifinfomsg *ifm = NLMSG_DATA(nlh);
280
281 ifm->ifi_index = filter_index;
282 }
283
284 if (filter_master) {
285 err = addattr32(nlh, reqlen, IFLA_MASTER, filter_master);
286 if (err)
287 return err;
288 }
289
290 return 0;
291}
292
293static int fdb_dump_filter(struct nlmsghdr *nlh, int reqlen)
294{
295 int err;
296
297 if (filter_index) {
298 struct ndmsg *ndm = NLMSG_DATA(nlh);
299
300 ndm->ndm_ifindex = filter_index;
301 }
302
303 if (filter_master) {
304 err = addattr32(nlh, reqlen, NDA_MASTER, filter_master);
305 if (err)
306 return err;
307 }
308
309 return 0;
310}
311
312static int fdb_show(int argc, char **argv)
313{
314 char *filter_dev = NULL;
315 char *br = NULL;
316 int rc;
317
318 while (argc > 0) {
319 if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) {
320 NEXT_ARG();
321 filter_dev = *argv;
322 } else if (strcmp(*argv, "br") == 0) {
323 NEXT_ARG();
324 br = *argv;
325 } else if (strcmp(*argv, "vlan") == 0) {
326 NEXT_ARG();
327 if (filter_vlan)
328 duparg("vlan", *argv);
329 filter_vlan = atoi(*argv);
330 } else if (strcmp(*argv, "state") == 0) {
331 unsigned int state;
332
333 NEXT_ARG();
334 if (state_a2n(&state, *argv))
335 invarg("invalid state", *argv);
336 filter_state |= state;
337 } else if (strcmp(*argv, "dynamic") == 0) {
338 filter_dynamic = 1;
339 } else {
340 if (matches(*argv, "help") == 0)
341 usage();
342 }
343 argc--; argv++;
344 }
345
346 if (br) {
347 int br_ifindex = ll_name_to_index(br);
348
349 if (br_ifindex == 0) {
350 fprintf(stderr, "Cannot find bridge device \"%s\"\n", br);
351 return -1;
352 }
353 filter_master = br_ifindex;
354 }
355
356
357 if (filter_dev) {
358 filter_index = ll_name_to_index(filter_dev);
359 if (!filter_index)
360 return nodev(filter_dev);
361 }
362
363 if (rth.flags & RTNL_HANDLE_F_STRICT_CHK)
364 rc = rtnl_neighdump_req(&rth, PF_BRIDGE, fdb_dump_filter);
365 else
366 rc = rtnl_fdb_linkdump_req_filter_fn(&rth, fdb_linkdump_filter);
367 if (rc < 0) {
368 perror("Cannot send dump request");
369 exit(1);
370 }
371
372 new_json_obj(json);
373 if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) {
374 fprintf(stderr, "Dump terminated\n");
375 exit(1);
376 }
377 delete_json_obj();
378 fflush(stdout);
379
380 return 0;
381}
382
383static int fdb_modify(int cmd, int flags, int argc, char **argv)
384{
385 struct {
386 struct nlmsghdr n;
387 struct ndmsg ndm;
388 char buf[256];
389 } req = {
390 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
391 .n.nlmsg_flags = NLM_F_REQUEST | flags,
392 .n.nlmsg_type = cmd,
393 .ndm.ndm_family = PF_BRIDGE,
394 .ndm.ndm_state = NUD_NOARP,
395 };
396 char *addr = NULL;
397 char *d = NULL;
398 char abuf[ETH_ALEN];
399 int dst_ok = 0;
400 inet_prefix dst;
401 unsigned long port = 0;
402 unsigned long vni = ~0;
403 unsigned long src_vni = ~0;
404 unsigned int via = 0;
405 char *endptr;
406 short vid = -1;
407 __u32 nhid = 0;
408
409 while (argc > 0) {
410 if (strcmp(*argv, "dev") == 0) {
411 NEXT_ARG();
412 d = *argv;
413 } else if (strcmp(*argv, "dst") == 0) {
414 NEXT_ARG();
415 if (dst_ok)
416 duparg2("dst", *argv);
417 get_addr(&dst, *argv, preferred_family);
418 dst_ok = 1;
419 } else if (strcmp(*argv, "nhid") == 0) {
420 NEXT_ARG();
421 if (get_u32(&nhid, *argv, 0))
422 invarg("\"id\" value is invalid\n", *argv);
423 } else if (strcmp(*argv, "port") == 0) {
424
425 NEXT_ARG();
426 port = strtoul(*argv, &endptr, 0);
427 if (endptr && *endptr) {
428 struct servent *pse;
429
430 pse = getservbyname(*argv, "udp");
431 if (!pse)
432 invarg("invalid port\n", *argv);
433 port = ntohs(pse->s_port);
434 } else if (port > 0xffff)
435 invarg("invalid port\n", *argv);
436 } else if (strcmp(*argv, "vni") == 0) {
437 NEXT_ARG();
438 vni = strtoul(*argv, &endptr, 0);
439 if ((endptr && *endptr) ||
440 (vni >> 24) || vni == ULONG_MAX)
441 invarg("invalid VNI\n", *argv);
442 } else if (strcmp(*argv, "src_vni") == 0) {
443 NEXT_ARG();
444 src_vni = strtoul(*argv, &endptr, 0);
445 if ((endptr && *endptr) ||
446 (src_vni >> 24) || src_vni == ULONG_MAX)
447 invarg("invalid src VNI\n", *argv);
448 } else if (strcmp(*argv, "via") == 0) {
449 NEXT_ARG();
450 via = ll_name_to_index(*argv);
451 if (!via)
452 exit(nodev(*argv));
453 } else if (strcmp(*argv, "self") == 0) {
454 req.ndm.ndm_flags |= NTF_SELF;
455 } else if (matches(*argv, "master") == 0) {
456 req.ndm.ndm_flags |= NTF_MASTER;
457 } else if (matches(*argv, "router") == 0) {
458 req.ndm.ndm_flags |= NTF_ROUTER;
459 } else if (matches(*argv, "local") == 0 ||
460 matches(*argv, "permanent") == 0) {
461 req.ndm.ndm_state |= NUD_PERMANENT;
462 } else if (matches(*argv, "temp") == 0 ||
463 matches(*argv, "static") == 0) {
464 req.ndm.ndm_state |= NUD_REACHABLE;
465 } else if (matches(*argv, "dynamic") == 0) {
466 req.ndm.ndm_state |= NUD_REACHABLE;
467 req.ndm.ndm_state &= ~NUD_NOARP;
468 } else if (matches(*argv, "vlan") == 0) {
469 if (vid >= 0)
470 duparg2("vlan", *argv);
471 NEXT_ARG();
472 vid = atoi(*argv);
473 } else if (matches(*argv, "use") == 0) {
474 req.ndm.ndm_flags |= NTF_USE;
475 } else if (matches(*argv, "extern_learn") == 0) {
476 req.ndm.ndm_flags |= NTF_EXT_LEARNED;
477 } else if (matches(*argv, "sticky") == 0) {
478 req.ndm.ndm_flags |= NTF_STICKY;
479 } else {
480 if (strcmp(*argv, "to") == 0)
481 NEXT_ARG();
482
483 if (matches(*argv, "help") == 0)
484 usage();
485 if (addr)
486 duparg2("to", *argv);
487 addr = *argv;
488 }
489 argc--; argv++;
490 }
491
492 if (d == NULL || addr == NULL) {
493 fprintf(stderr, "Device and address are required arguments.\n");
494 return -1;
495 }
496
497 if (nhid && (dst_ok || port || vni != ~0)) {
498 fprintf(stderr, "dst, port, vni are mutually exclusive with nhid\n");
499 return -1;
500 }
501
502
503 if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER)))
504 req.ndm.ndm_flags |= NTF_SELF;
505
506
507 if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE)))
508 req.ndm.ndm_state |= NUD_PERMANENT;
509
510 if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
511 abuf, abuf+1, abuf+2,
512 abuf+3, abuf+4, abuf+5) != 6) {
513 fprintf(stderr, "Invalid mac address %s\n", addr);
514 return -1;
515 }
516
517 addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN);
518 if (dst_ok)
519 addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen);
520
521 if (vid >= 0)
522 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
523 if (nhid > 0)
524 addattr32(&req.n, sizeof(req), NDA_NH_ID, nhid);
525
526 if (port) {
527 unsigned short dport;
528
529 dport = htons((unsigned short)port);
530 addattr16(&req.n, sizeof(req), NDA_PORT, dport);
531 }
532 if (vni != ~0)
533 addattr32(&req.n, sizeof(req), NDA_VNI, vni);
534 if (src_vni != ~0)
535 addattr32(&req.n, sizeof(req), NDA_SRC_VNI, src_vni);
536 if (via)
537 addattr32(&req.n, sizeof(req), NDA_IFINDEX, via);
538
539 req.ndm.ndm_ifindex = ll_name_to_index(d);
540 if (!req.ndm.ndm_ifindex)
541 return nodev(d);
542
543 if (rtnl_talk(&rth, &req.n, NULL) < 0)
544 return -1;
545
546 return 0;
547}
548
549static int fdb_get(int argc, char **argv)
550{
551 struct {
552 struct nlmsghdr n;
553 struct ndmsg ndm;
554 char buf[1024];
555 } req = {
556 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
557 .n.nlmsg_flags = NLM_F_REQUEST,
558 .n.nlmsg_type = RTM_GETNEIGH,
559 .ndm.ndm_family = AF_BRIDGE,
560 };
561 char *d = NULL, *br = NULL;
562 struct nlmsghdr *answer;
563 unsigned long vni = ~0;
564 char abuf[ETH_ALEN];
565 int br_ifindex = 0;
566 char *addr = NULL;
567 short vlan = -1;
568 char *endptr;
569
570 while (argc > 0) {
571 if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) {
572 NEXT_ARG();
573 d = *argv;
574 } else if (strcmp(*argv, "br") == 0) {
575 NEXT_ARG();
576 br = *argv;
577 } else if (strcmp(*argv, "dev") == 0) {
578 NEXT_ARG();
579 d = *argv;
580 } else if (strcmp(*argv, "vni") == 0) {
581 NEXT_ARG();
582 vni = strtoul(*argv, &endptr, 0);
583 if ((endptr && *endptr) ||
584 (vni >> 24) || vni == ULONG_MAX)
585 invarg("invalid VNI\n", *argv);
586 } else if (strcmp(*argv, "self") == 0) {
587 req.ndm.ndm_flags |= NTF_SELF;
588 } else if (matches(*argv, "master") == 0) {
589 req.ndm.ndm_flags |= NTF_MASTER;
590 } else if (matches(*argv, "vlan") == 0) {
591 if (vlan >= 0)
592 duparg2("vlan", *argv);
593 NEXT_ARG();
594 vlan = atoi(*argv);
595 } else if (matches(*argv, "dynamic") == 0) {
596 filter_dynamic = 1;
597 } else {
598 if (strcmp(*argv, "to") == 0)
599 NEXT_ARG();
600
601 if (matches(*argv, "help") == 0)
602 usage();
603 if (addr)
604 duparg2("to", *argv);
605 addr = *argv;
606 }
607 argc--; argv++;
608 }
609
610 if ((d == NULL && br == NULL) || addr == NULL) {
611 fprintf(stderr, "Device or master and address are required arguments.\n");
612 return -1;
613 }
614
615 if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
616 abuf, abuf+1, abuf+2,
617 abuf+3, abuf+4, abuf+5) != 6) {
618 fprintf(stderr, "Invalid mac address %s\n", addr);
619 return -1;
620 }
621
622 addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN);
623
624 if (vlan >= 0)
625 addattr16(&req.n, sizeof(req), NDA_VLAN, vlan);
626
627 if (vni != ~0)
628 addattr32(&req.n, sizeof(req), NDA_VNI, vni);
629
630 if (d) {
631 req.ndm.ndm_ifindex = ll_name_to_index(d);
632 if (!req.ndm.ndm_ifindex) {
633 fprintf(stderr, "Cannot find device \"%s\"\n", d);
634 return -1;
635 }
636 }
637
638 if (br) {
639 br_ifindex = ll_name_to_index(br);
640 if (!br_ifindex) {
641 fprintf(stderr, "Cannot find bridge device \"%s\"\n", br);
642 return -1;
643 }
644 addattr32(&req.n, sizeof(req), NDA_MASTER, br_ifindex);
645 }
646
647 if (rtnl_talk(&rth, &req.n, &answer) < 0)
648 return -2;
649
650
651
652
653
654 new_json_obj(json);
655 if (print_fdb(answer, stdout) < 0) {
656 fprintf(stderr, "An error :-)\n");
657 return -1;
658 }
659 delete_json_obj();
660
661 return 0;
662}
663
664int do_fdb(int argc, char **argv)
665{
666 ll_init_map(&rth);
667
668 if (argc > 0) {
669 if (matches(*argv, "add") == 0)
670 return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
671 if (matches(*argv, "append") == 0)
672 return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_APPEND, argc-1, argv+1);
673 if (matches(*argv, "replace") == 0)
674 return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
675 if (matches(*argv, "delete") == 0)
676 return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1);
677 if (matches(*argv, "get") == 0)
678 return fdb_get(argc-1, argv+1);
679 if (matches(*argv, "show") == 0 ||
680 matches(*argv, "lst") == 0 ||
681 matches(*argv, "list") == 0)
682 return fdb_show(argc-1, argv+1);
683 if (matches(*argv, "help") == 0)
684 usage();
685 } else
686 return fdb_show(0, NULL);
687
688 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv);
689 exit(-1);
690}
691