]>
git.proxmox.com Git - mirror_iproute2.git/blob - bridge/fdb.c
1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Get/set/delete fdb table with netlink
5 * TODO: merge/replace this with ip neighbour
7 * Authors: Stephen Hemminger <shemminger@vyatta.com>
16 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_ether.h>
22 #include <linux/neighbour.h>
27 #include "json_print.h"
28 #include "libnetlink.h"
29 #include "br_common.h"
33 static unsigned int filter_index
, filter_dynamic
, filter_master
,
34 filter_state
, filter_vlan
;
36 static void usage(void)
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");
51 static const char *state_n2a(unsigned int s
)
55 if (s
& NUD_PERMANENT
)
64 if (s
& NUD_REACHABLE
)
67 if (is_json_context())
68 sprintf(buf
, "%#x", s
);
70 sprintf(buf
, "state=%#x", s
);
74 static int state_a2n(unsigned int *s
, const char *arg
)
76 if (matches(arg
, "permanent") == 0)
78 else if (matches(arg
, "static") == 0 || matches(arg
, "temp") == 0)
80 else if (matches(arg
, "stale") == 0)
82 else if (matches(arg
, "reachable") == 0 || matches(arg
, "dynamic") == 0)
84 else if (strcmp(arg
, "all") == 0)
86 else if (get_unsigned(s
, arg
, 0))
92 static void fdb_print_flags(FILE *fp
, unsigned int flags
)
94 open_json_array(PRINT_JSON
,
95 is_json_context() ? "flags" : "");
98 print_string(PRINT_ANY
, NULL
, "%s ", "self");
100 if (flags
& NTF_ROUTER
)
101 print_string(PRINT_ANY
, NULL
, "%s ", "router");
103 if (flags
& NTF_EXT_LEARNED
)
104 print_string(PRINT_ANY
, NULL
, "%s ", "extern_learn");
106 if (flags
& NTF_OFFLOADED
)
107 print_string(PRINT_ANY
, NULL
, "%s ", "offload");
109 if (flags
& NTF_MASTER
)
110 print_string(PRINT_ANY
, NULL
, "%s ", "master");
112 if (flags
& NTF_STICKY
)
113 print_string(PRINT_ANY
, NULL
, "%s ", "sticky");
115 close_json_array(PRINT_JSON
, NULL
);
118 static void fdb_print_stats(FILE *fp
, const struct nda_cacheinfo
*ci
)
125 if (is_json_context()) {
126 print_uint(PRINT_JSON
, "used", NULL
,
128 print_uint(PRINT_JSON
, "updated", NULL
,
129 ci
->ndm_updated
/ hz
);
131 fprintf(fp
, "used %d/%d ", ci
->ndm_used
/ hz
,
132 ci
->ndm_updated
/ hz
);
137 int print_fdb(struct nlmsghdr
*n
, void *arg
)
140 struct ndmsg
*r
= NLMSG_DATA(n
);
141 int len
= n
->nlmsg_len
;
142 struct rtattr
*tb
[NDA_MAX
+1];
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
);
151 len
-= NLMSG_LENGTH(sizeof(*r
));
153 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
157 if (r
->ndm_family
!= AF_BRIDGE
)
160 if (filter_index
&& filter_index
!= r
->ndm_ifindex
)
163 if (filter_state
&& !(r
->ndm_state
& filter_state
))
166 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
),
167 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
170 vid
= rta_getattr_u16(tb
[NDA_VLAN
]);
172 if (filter_vlan
&& filter_vlan
!= vid
)
175 if (filter_dynamic
&& (r
->ndm_state
& NUD_PERMANENT
))
178 open_json_object(NULL
);
179 if (n
->nlmsg_type
== RTM_DELNEIGH
)
180 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
182 if (tb
[NDA_LLADDR
]) {
186 lladdr
= ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
187 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
188 ll_index_to_type(r
->ndm_ifindex
),
191 print_color_string(PRINT_ANY
, COLOR_MAC
,
192 "mac", "%s ", lladdr
);
195 if (!filter_index
&& r
->ndm_ifindex
)
196 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
198 ll_index_to_name(r
->ndm_ifindex
));
201 int family
= AF_INET
;
204 if (RTA_PAYLOAD(tb
[NDA_DST
]) == sizeof(struct in6_addr
))
207 dst
= format_host(family
,
208 RTA_PAYLOAD(tb
[NDA_DST
]),
209 RTA_DATA(tb
[NDA_DST
]));
211 print_color_string(PRINT_ANY
,
212 ifa_family_color(family
),
213 "dst", "dst %s ", dst
);
217 print_uint(PRINT_ANY
,
218 "vlan", "vlan %hu ", vid
);
221 print_uint(PRINT_ANY
,
223 rta_getattr_be16(tb
[NDA_PORT
]));
226 print_uint(PRINT_ANY
,
228 rta_getattr_u32(tb
[NDA_VNI
]));
231 print_uint(PRINT_ANY
,
232 "src_vni", "src_vni %u ",
233 rta_getattr_u32(tb
[NDA_SRC_VNI
]));
235 if (tb
[NDA_IFINDEX
]) {
236 unsigned int ifindex
= rta_getattr_u32(tb
[NDA_IFINDEX
]);
238 if (tb
[NDA_LINK_NETNSID
])
239 print_uint(PRINT_ANY
,
240 "viaIfIndex", "via ifindex %u ",
243 print_string(PRINT_ANY
,
245 ll_index_to_name(ifindex
));
249 print_uint(PRINT_ANY
, "nhid", "nhid %u ",
250 rta_getattr_u32(tb
[NDA_NH_ID
]));
252 if (tb
[NDA_LINK_NETNSID
])
253 print_uint(PRINT_ANY
,
254 "linkNetNsId", "link-netnsid %d ",
255 rta_getattr_u32(tb
[NDA_LINK_NETNSID
]));
257 if (show_stats
&& tb
[NDA_CACHEINFO
])
258 fdb_print_stats(fp
, RTA_DATA(tb
[NDA_CACHEINFO
]));
260 fdb_print_flags(fp
, r
->ndm_flags
);
264 print_string(PRINT_ANY
, "master", "master %s ",
265 ll_index_to_name(rta_getattr_u32(tb
[NDA_MASTER
])));
267 print_string(PRINT_ANY
, "state", "%s\n",
268 state_n2a(r
->ndm_state
));
274 static int fdb_linkdump_filter(struct nlmsghdr
*nlh
, int reqlen
)
279 struct ifinfomsg
*ifm
= NLMSG_DATA(nlh
);
281 ifm
->ifi_index
= filter_index
;
285 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter_master
);
293 static int fdb_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
298 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
300 ndm
->ndm_ifindex
= filter_index
;
304 err
= addattr32(nlh
, reqlen
, NDA_MASTER
, filter_master
);
312 static int fdb_show(int argc
, char **argv
)
314 char *filter_dev
= NULL
;
319 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
322 } else if (strcmp(*argv
, "br") == 0) {
325 } else if (strcmp(*argv
, "vlan") == 0) {
328 duparg("vlan", *argv
);
329 filter_vlan
= atoi(*argv
);
330 } else if (strcmp(*argv
, "state") == 0) {
334 if (state_a2n(&state
, *argv
))
335 invarg("invalid state", *argv
);
336 filter_state
|= state
;
337 } else if (strcmp(*argv
, "dynamic") == 0) {
340 if (matches(*argv
, "help") == 0)
347 int br_ifindex
= ll_name_to_index(br
);
349 if (br_ifindex
== 0) {
350 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
353 filter_master
= br_ifindex
;
356 /*we'll keep around filter_dev for older kernels */
358 filter_index
= ll_name_to_index(filter_dev
);
360 return nodev(filter_dev
);
363 if (rth
.flags
& RTNL_HANDLE_F_STRICT_CHK
)
364 rc
= rtnl_neighdump_req(&rth
, PF_BRIDGE
, fdb_dump_filter
);
366 rc
= rtnl_fdb_linkdump_req_filter_fn(&rth
, fdb_linkdump_filter
);
368 perror("Cannot send dump request");
373 if (rtnl_dump_filter(&rth
, print_fdb
, stdout
) < 0) {
374 fprintf(stderr
, "Dump terminated\n");
383 static int fdb_modify(int cmd
, int flags
, int argc
, char **argv
)
390 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
391 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
393 .ndm
.ndm_family
= PF_BRIDGE
,
394 .ndm
.ndm_state
= NUD_NOARP
,
401 unsigned long port
= 0;
402 unsigned long vni
= ~0;
403 unsigned long src_vni
= ~0;
404 unsigned int via
= 0;
410 if (strcmp(*argv
, "dev") == 0) {
413 } else if (strcmp(*argv
, "dst") == 0) {
416 duparg2("dst", *argv
);
417 get_addr(&dst
, *argv
, preferred_family
);
419 } else if (strcmp(*argv
, "nhid") == 0) {
421 if (get_u32(&nhid
, *argv
, 0))
422 invarg("\"id\" value is invalid\n", *argv
);
423 } else if (strcmp(*argv
, "port") == 0) {
426 port
= strtoul(*argv
, &endptr
, 0);
427 if (endptr
&& *endptr
) {
430 pse
= getservbyname(*argv
, "udp");
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) {
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) {
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) {
450 via
= ll_name_to_index(*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) {
470 duparg2("vlan", *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
;
480 if (strcmp(*argv
, "to") == 0)
483 if (matches(*argv
, "help") == 0)
486 duparg2("to", *argv
);
492 if (d
== NULL
|| addr
== NULL
) {
493 fprintf(stderr
, "Device and address are required arguments.\n");
497 if (nhid
&& (dst_ok
|| port
|| vni
!= ~0)) {
498 fprintf(stderr
, "dst, port, vni are mutually exclusive with nhid\n");
503 if (!(req
.ndm
.ndm_flags
&(NTF_SELF
|NTF_MASTER
)))
504 req
.ndm
.ndm_flags
|= NTF_SELF
;
506 /* Assume permanent */
507 if (!(req
.ndm
.ndm_state
&(NUD_PERMANENT
|NUD_REACHABLE
)))
508 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
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
);
517 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
519 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
);
522 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
524 addattr32(&req
.n
, sizeof(req
), NDA_NH_ID
, nhid
);
527 unsigned short dport
;
529 dport
= htons((unsigned short)port
);
530 addattr16(&req
.n
, sizeof(req
), NDA_PORT
, dport
);
533 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
535 addattr32(&req
.n
, sizeof(req
), NDA_SRC_VNI
, src_vni
);
537 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, via
);
539 req
.ndm
.ndm_ifindex
= ll_name_to_index(d
);
540 if (!req
.ndm
.ndm_ifindex
)
543 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
549 static int fdb_get(int argc
, char **argv
)
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
,
561 char *d
= NULL
, *br
= NULL
;
562 struct nlmsghdr
*answer
;
563 unsigned long vni
= ~0;
571 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
574 } else if (strcmp(*argv
, "br") == 0) {
577 } else if (strcmp(*argv
, "dev") == 0) {
580 } else if (strcmp(*argv
, "vni") == 0) {
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) {
592 duparg2("vlan", *argv
);
595 } else if (matches(*argv
, "dynamic") == 0) {
598 if (strcmp(*argv
, "to") == 0)
601 if (matches(*argv
, "help") == 0)
604 duparg2("to", *argv
);
610 if ((d
== NULL
&& br
== NULL
) || addr
== NULL
) {
611 fprintf(stderr
, "Device or master and address are required arguments.\n");
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
);
622 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
625 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vlan
);
628 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
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
);
639 br_ifindex
= ll_name_to_index(br
);
641 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
644 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, br_ifindex
);
647 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0)
651 * Initialize a json_writer and open an array object
652 * if -json was specified.
655 if (print_fdb(answer
, stdout
) < 0) {
656 fprintf(stderr
, "An error :-)\n");
664 int do_fdb(int argc
, char **argv
)
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)
686 return fdb_show(0, NULL
);
688 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv
);