]>
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_vlan
, filter_state
, filter_master
;
35 static void usage(void)
38 "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV\n"
39 " [ self ] [ master ] [ use ] [ router ] [ extern_learn ]\n"
40 " [ sticky ] [ local | static | dynamic ] [ dst IPADDR ]\n"
41 " [ vlan VID ] [ port PORT] [ vni VNI ] [ via DEV ]\n"
43 " bridge fdb [ show [ br BRDEV ] [ brport DEV ] [ vlan VID ] [ state STATE ] ]\n");
47 static const char *state_n2a(unsigned int s
)
51 if (s
& NUD_PERMANENT
)
60 if (s
& NUD_REACHABLE
)
63 sprintf(buf
, "state=%#x", s
);
67 static int state_a2n(unsigned int *s
, const char *arg
)
69 if (matches(arg
, "permanent") == 0)
71 else if (matches(arg
, "static") == 0 || matches(arg
, "temp") == 0)
73 else if (matches(arg
, "stale") == 0)
75 else if (matches(arg
, "reachable") == 0 || matches(arg
, "dynamic") == 0)
77 else if (strcmp(arg
, "all") == 0)
79 else if (get_unsigned(s
, arg
, 0))
85 static void fdb_print_flags(FILE *fp
, unsigned int flags
)
87 open_json_array(PRINT_JSON
,
88 is_json_context() ? "flags" : "");
91 print_string(PRINT_ANY
, NULL
, "%s ", "self");
93 if (flags
& NTF_ROUTER
)
94 print_string(PRINT_ANY
, NULL
, "%s ", "router");
96 if (flags
& NTF_EXT_LEARNED
)
97 print_string(PRINT_ANY
, NULL
, "%s ", "extern_learn");
99 if (flags
& NTF_OFFLOADED
)
100 print_string(PRINT_ANY
, NULL
, "%s ", "offload");
102 if (flags
& NTF_MASTER
)
103 print_string(PRINT_ANY
, NULL
, "%s ", "master");
105 if (flags
& NTF_STICKY
)
106 print_string(PRINT_ANY
, NULL
, "%s ", "sticky");
108 close_json_array(PRINT_JSON
, NULL
);
111 static void fdb_print_stats(FILE *fp
, const struct nda_cacheinfo
*ci
)
118 if (is_json_context()) {
119 print_uint(PRINT_JSON
, "used", NULL
,
121 print_uint(PRINT_JSON
, "updated", NULL
,
122 ci
->ndm_updated
/ hz
);
124 fprintf(fp
, "used %d/%d ", ci
->ndm_used
/ hz
,
125 ci
->ndm_updated
/ hz
);
130 int print_fdb(struct nlmsghdr
*n
, void *arg
)
133 struct ndmsg
*r
= NLMSG_DATA(n
);
134 int len
= n
->nlmsg_len
;
135 struct rtattr
*tb
[NDA_MAX
+1];
138 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
) {
139 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
140 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
144 len
-= NLMSG_LENGTH(sizeof(*r
));
146 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
150 if (r
->ndm_family
!= AF_BRIDGE
)
153 if (filter_index
&& filter_index
!= r
->ndm_ifindex
)
156 if (filter_state
&& !(r
->ndm_state
& filter_state
))
159 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
),
160 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
163 vid
= rta_getattr_u16(tb
[NDA_VLAN
]);
165 if (filter_vlan
&& filter_vlan
!= vid
)
168 open_json_object(NULL
);
169 if (n
->nlmsg_type
== RTM_DELNEIGH
)
170 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
172 if (tb
[NDA_LLADDR
]) {
176 lladdr
= ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
177 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
178 ll_index_to_type(r
->ndm_ifindex
),
181 print_color_string(PRINT_ANY
, COLOR_MAC
,
182 "mac", "%s ", lladdr
);
185 if (!filter_index
&& r
->ndm_ifindex
)
186 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
188 ll_index_to_name(r
->ndm_ifindex
));
191 int family
= AF_INET
;
194 if (RTA_PAYLOAD(tb
[NDA_DST
]) == sizeof(struct in6_addr
))
197 dst
= format_host(family
,
198 RTA_PAYLOAD(tb
[NDA_DST
]),
199 RTA_DATA(tb
[NDA_DST
]));
201 print_color_string(PRINT_ANY
,
202 ifa_family_color(family
),
203 "dst", "dst %s ", dst
);
207 print_uint(PRINT_ANY
,
208 "vlan", "vlan %hu ", vid
);
211 print_uint(PRINT_ANY
,
213 rta_getattr_be16(tb
[NDA_PORT
]));
216 print_uint(PRINT_ANY
,
218 rta_getattr_u32(tb
[NDA_VNI
]));
221 print_uint(PRINT_ANY
,
222 "src_vni", "src_vni %u ",
223 rta_getattr_u32(tb
[NDA_SRC_VNI
]));
225 if (tb
[NDA_IFINDEX
]) {
226 unsigned int ifindex
= rta_getattr_u32(tb
[NDA_IFINDEX
]);
228 if (tb
[NDA_LINK_NETNSID
])
229 print_uint(PRINT_ANY
,
230 "viaIfIndex", "via ifindex %u ",
233 print_string(PRINT_ANY
,
235 ll_index_to_name(ifindex
));
238 if (tb
[NDA_LINK_NETNSID
])
239 print_uint(PRINT_ANY
,
240 "linkNetNsId", "link-netnsid %d ",
241 rta_getattr_u32(tb
[NDA_LINK_NETNSID
]));
243 if (show_stats
&& tb
[NDA_CACHEINFO
])
244 fdb_print_stats(fp
, RTA_DATA(tb
[NDA_CACHEINFO
]));
246 fdb_print_flags(fp
, r
->ndm_flags
);
250 print_string(PRINT_ANY
, "master", "master %s ",
251 ll_index_to_name(rta_getattr_u32(tb
[NDA_MASTER
])));
253 print_string(PRINT_ANY
, "state", "%s\n",
254 state_n2a(r
->ndm_state
));
260 static int fdb_linkdump_filter(struct nlmsghdr
*nlh
, int reqlen
)
265 struct ifinfomsg
*ifm
= NLMSG_DATA(nlh
);
267 ifm
->ifi_index
= filter_index
;
271 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter_master
);
279 static int fdb_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
284 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
286 ndm
->ndm_ifindex
= filter_index
;
290 err
= addattr32(nlh
, reqlen
, NDA_MASTER
, filter_master
);
298 static int fdb_show(int argc
, char **argv
)
300 char *filter_dev
= NULL
;
305 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
308 } else if (strcmp(*argv
, "br") == 0) {
311 } else if (strcmp(*argv
, "vlan") == 0) {
314 duparg("vlan", *argv
);
315 filter_vlan
= atoi(*argv
);
316 } else if (strcmp(*argv
, "state") == 0) {
320 if (state_a2n(&state
, *argv
))
321 invarg("invalid state", *argv
);
322 filter_state
|= state
;
324 if (matches(*argv
, "help") == 0)
331 int br_ifindex
= ll_name_to_index(br
);
333 if (br_ifindex
== 0) {
334 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
337 filter_master
= br_ifindex
;
340 /*we'll keep around filter_dev for older kernels */
342 filter_index
= ll_name_to_index(filter_dev
);
344 return nodev(filter_dev
);
347 if (rth
.flags
& RTNL_HANDLE_F_STRICT_CHK
)
348 rc
= rtnl_neighdump_req(&rth
, PF_BRIDGE
, fdb_dump_filter
);
350 rc
= rtnl_fdb_linkdump_req_filter_fn(&rth
, fdb_linkdump_filter
);
352 perror("Cannot send dump request");
357 if (rtnl_dump_filter(&rth
, print_fdb
, stdout
) < 0) {
358 fprintf(stderr
, "Dump terminated\n");
367 static int fdb_modify(int cmd
, int flags
, int argc
, char **argv
)
374 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
375 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
377 .ndm
.ndm_family
= PF_BRIDGE
,
378 .ndm
.ndm_state
= NUD_NOARP
,
385 unsigned long port
= 0;
386 unsigned long vni
= ~0;
387 unsigned long src_vni
= ~0;
388 unsigned int via
= 0;
393 if (strcmp(*argv
, "dev") == 0) {
396 } else if (strcmp(*argv
, "dst") == 0) {
399 duparg2("dst", *argv
);
400 get_addr(&dst
, *argv
, preferred_family
);
402 } else if (strcmp(*argv
, "port") == 0) {
405 port
= strtoul(*argv
, &endptr
, 0);
406 if (endptr
&& *endptr
) {
409 pse
= getservbyname(*argv
, "udp");
411 invarg("invalid port\n", *argv
);
412 port
= ntohs(pse
->s_port
);
413 } else if (port
> 0xffff)
414 invarg("invalid port\n", *argv
);
415 } else if (strcmp(*argv
, "vni") == 0) {
417 vni
= strtoul(*argv
, &endptr
, 0);
418 if ((endptr
&& *endptr
) ||
419 (vni
>> 24) || vni
== ULONG_MAX
)
420 invarg("invalid VNI\n", *argv
);
421 } else if (strcmp(*argv
, "src_vni") == 0) {
423 src_vni
= strtoul(*argv
, &endptr
, 0);
424 if ((endptr
&& *endptr
) ||
425 (src_vni
>> 24) || src_vni
== ULONG_MAX
)
426 invarg("invalid src VNI\n", *argv
);
427 } else if (strcmp(*argv
, "via") == 0) {
429 via
= ll_name_to_index(*argv
);
432 } else if (strcmp(*argv
, "self") == 0) {
433 req
.ndm
.ndm_flags
|= NTF_SELF
;
434 } else if (matches(*argv
, "master") == 0) {
435 req
.ndm
.ndm_flags
|= NTF_MASTER
;
436 } else if (matches(*argv
, "router") == 0) {
437 req
.ndm
.ndm_flags
|= NTF_ROUTER
;
438 } else if (matches(*argv
, "local") == 0 ||
439 matches(*argv
, "permanent") == 0) {
440 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
441 } else if (matches(*argv
, "temp") == 0 ||
442 matches(*argv
, "static") == 0) {
443 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
444 } else if (matches(*argv
, "dynamic") == 0) {
445 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
446 req
.ndm
.ndm_state
&= ~NUD_NOARP
;
447 } else if (matches(*argv
, "vlan") == 0) {
449 duparg2("vlan", *argv
);
452 } else if (matches(*argv
, "use") == 0) {
453 req
.ndm
.ndm_flags
|= NTF_USE
;
454 } else if (matches(*argv
, "extern_learn") == 0) {
455 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
456 } else if (matches(*argv
, "sticky") == 0) {
457 req
.ndm
.ndm_flags
|= NTF_STICKY
;
459 if (strcmp(*argv
, "to") == 0)
462 if (matches(*argv
, "help") == 0)
465 duparg2("to", *argv
);
471 if (d
== NULL
|| addr
== NULL
) {
472 fprintf(stderr
, "Device and address are required arguments.\n");
477 if (!(req
.ndm
.ndm_flags
&(NTF_SELF
|NTF_MASTER
)))
478 req
.ndm
.ndm_flags
|= NTF_SELF
;
480 /* Assume permanent */
481 if (!(req
.ndm
.ndm_state
&(NUD_PERMANENT
|NUD_REACHABLE
)))
482 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
484 if (sscanf(addr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
485 abuf
, abuf
+1, abuf
+2,
486 abuf
+3, abuf
+4, abuf
+5) != 6) {
487 fprintf(stderr
, "Invalid mac address %s\n", addr
);
491 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
493 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
);
496 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
499 unsigned short dport
;
501 dport
= htons((unsigned short)port
);
502 addattr16(&req
.n
, sizeof(req
), NDA_PORT
, dport
);
505 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
507 addattr32(&req
.n
, sizeof(req
), NDA_SRC_VNI
, src_vni
);
509 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, via
);
511 req
.ndm
.ndm_ifindex
= ll_name_to_index(d
);
512 if (!req
.ndm
.ndm_ifindex
)
515 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
521 int do_fdb(int argc
, char **argv
)
526 if (matches(*argv
, "add") == 0)
527 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
528 if (matches(*argv
, "append") == 0)
529 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_APPEND
, argc
-1, argv
+1);
530 if (matches(*argv
, "replace") == 0)
531 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
532 if (matches(*argv
, "delete") == 0)
533 return fdb_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
534 if (matches(*argv
, "show") == 0 ||
535 matches(*argv
, "lst") == 0 ||
536 matches(*argv
, "list") == 0)
537 return fdb_show(argc
-1, argv
+1);
538 if (matches(*argv
, "help") == 0)
541 return fdb_show(0, NULL
);
543 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv
);