]>
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"
42 " bridge fdb [ show [ br BRDEV ] [ brport DEV ] [ vlan VID ] [ state STATE ] ]\n");
46 static const char *state_n2a(unsigned int s
)
50 if (s
& NUD_PERMANENT
)
59 if (s
& NUD_REACHABLE
)
62 sprintf(buf
, "state=%#x", s
);
66 static int state_a2n(unsigned int *s
, const char *arg
)
68 if (matches(arg
, "permanent") == 0)
70 else if (matches(arg
, "static") == 0 || matches(arg
, "temp") == 0)
72 else if (matches(arg
, "stale") == 0)
74 else if (matches(arg
, "reachable") == 0 || matches(arg
, "dynamic") == 0)
76 else if (strcmp(arg
, "all") == 0)
78 else if (get_unsigned(s
, arg
, 0))
84 static void fdb_print_flags(FILE *fp
, unsigned int flags
)
86 open_json_array(PRINT_JSON
,
87 is_json_context() ? "flags" : "");
90 print_string(PRINT_ANY
, NULL
, "%s ", "self");
92 if (flags
& NTF_ROUTER
)
93 print_string(PRINT_ANY
, NULL
, "%s ", "router");
95 if (flags
& NTF_EXT_LEARNED
)
96 print_string(PRINT_ANY
, NULL
, "%s ", "extern_learn");
98 if (flags
& NTF_OFFLOADED
)
99 print_string(PRINT_ANY
, NULL
, "%s ", "offload");
101 if (flags
& NTF_MASTER
)
102 print_string(PRINT_ANY
, NULL
, "%s ", "master");
104 if (flags
& NTF_STICKY
)
105 print_string(PRINT_ANY
, NULL
, "%s ", "sticky");
107 close_json_array(PRINT_JSON
, NULL
);
110 static void fdb_print_stats(FILE *fp
, const struct nda_cacheinfo
*ci
)
117 if (is_json_context()) {
118 print_uint(PRINT_JSON
, "used", NULL
,
120 print_uint(PRINT_JSON
, "updated", NULL
,
121 ci
->ndm_updated
/ hz
);
123 fprintf(fp
, "used %d/%d ", ci
->ndm_used
/ hz
,
124 ci
->ndm_updated
/ hz
);
129 int print_fdb(struct nlmsghdr
*n
, void *arg
)
132 struct ndmsg
*r
= NLMSG_DATA(n
);
133 int len
= n
->nlmsg_len
;
134 struct rtattr
*tb
[NDA_MAX
+1];
137 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
) {
138 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
139 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
143 len
-= NLMSG_LENGTH(sizeof(*r
));
145 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
149 if (r
->ndm_family
!= AF_BRIDGE
)
152 if (filter_index
&& filter_index
!= r
->ndm_ifindex
)
155 if (filter_state
&& !(r
->ndm_state
& filter_state
))
158 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
),
159 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
162 vid
= rta_getattr_u16(tb
[NDA_VLAN
]);
164 if (filter_vlan
&& filter_vlan
!= vid
)
167 open_json_object(NULL
);
168 if (n
->nlmsg_type
== RTM_DELNEIGH
)
169 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
171 if (tb
[NDA_LLADDR
]) {
175 lladdr
= ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
176 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
177 ll_index_to_type(r
->ndm_ifindex
),
180 print_color_string(PRINT_ANY
, COLOR_MAC
,
181 "mac", "%s ", lladdr
);
184 if (!filter_index
&& r
->ndm_ifindex
)
185 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
187 ll_index_to_name(r
->ndm_ifindex
));
190 int family
= AF_INET
;
193 if (RTA_PAYLOAD(tb
[NDA_DST
]) == sizeof(struct in6_addr
))
196 dst
= format_host(family
,
197 RTA_PAYLOAD(tb
[NDA_DST
]),
198 RTA_DATA(tb
[NDA_DST
]));
200 print_color_string(PRINT_ANY
,
201 ifa_family_color(family
),
202 "dst", "dst %s ", dst
);
206 print_uint(PRINT_ANY
,
207 "vlan", "vlan %hu ", vid
);
210 print_uint(PRINT_ANY
,
212 rta_getattr_be16(tb
[NDA_PORT
]));
215 print_uint(PRINT_ANY
,
217 rta_getattr_u32(tb
[NDA_VNI
]));
220 print_uint(PRINT_ANY
,
221 "src_vni", "src_vni %u ",
222 rta_getattr_u32(tb
[NDA_SRC_VNI
]));
224 if (tb
[NDA_IFINDEX
]) {
225 unsigned int ifindex
= rta_getattr_u32(tb
[NDA_IFINDEX
]);
227 if (tb
[NDA_LINK_NETNSID
])
228 print_uint(PRINT_ANY
,
229 "viaIfIndex", "via ifindex %u ",
232 print_string(PRINT_ANY
,
234 ll_index_to_name(ifindex
));
237 if (tb
[NDA_LINK_NETNSID
])
238 print_uint(PRINT_ANY
,
239 "linkNetNsId", "link-netnsid %d ",
240 rta_getattr_u32(tb
[NDA_LINK_NETNSID
]));
242 if (show_stats
&& tb
[NDA_CACHEINFO
])
243 fdb_print_stats(fp
, RTA_DATA(tb
[NDA_CACHEINFO
]));
245 fdb_print_flags(fp
, r
->ndm_flags
);
249 print_string(PRINT_ANY
, "master", "master %s ",
250 ll_index_to_name(rta_getattr_u32(tb
[NDA_MASTER
])));
252 print_string(PRINT_ANY
, "state", "%s\n",
253 state_n2a(r
->ndm_state
));
259 static int fdb_linkdump_filter(struct nlmsghdr
*nlh
, int reqlen
)
264 struct ifinfomsg
*ifm
= NLMSG_DATA(nlh
);
266 ifm
->ifi_index
= filter_index
;
270 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter_master
);
278 static int fdb_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
283 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
285 ndm
->ndm_ifindex
= filter_index
;
289 err
= addattr32(nlh
, reqlen
, NDA_MASTER
, filter_master
);
297 static int fdb_show(int argc
, char **argv
)
299 char *filter_dev
= NULL
;
304 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
307 } else if (strcmp(*argv
, "br") == 0) {
310 } else if (strcmp(*argv
, "vlan") == 0) {
313 duparg("vlan", *argv
);
314 filter_vlan
= atoi(*argv
);
315 } else if (strcmp(*argv
, "state") == 0) {
319 if (state_a2n(&state
, *argv
))
320 invarg("invalid state", *argv
);
321 filter_state
|= state
;
323 if (matches(*argv
, "help") == 0)
330 int br_ifindex
= ll_name_to_index(br
);
332 if (br_ifindex
== 0) {
333 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
336 filter_master
= br_ifindex
;
339 /*we'll keep around filter_dev for older kernels */
341 filter_index
= ll_name_to_index(filter_dev
);
343 return nodev(filter_dev
);
346 if (rth
.flags
& RTNL_HANDLE_F_STRICT_CHK
)
347 rc
= rtnl_neighdump_req(&rth
, PF_BRIDGE
, fdb_dump_filter
);
349 rc
= rtnl_linkdump_req_filter_fn(&rth
, PF_BRIDGE
,
350 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 int via
= 0;
392 if (strcmp(*argv
, "dev") == 0) {
395 } else if (strcmp(*argv
, "dst") == 0) {
398 duparg2("dst", *argv
);
399 get_addr(&dst
, *argv
, preferred_family
);
401 } else if (strcmp(*argv
, "port") == 0) {
404 port
= strtoul(*argv
, &endptr
, 0);
405 if (endptr
&& *endptr
) {
408 pse
= getservbyname(*argv
, "udp");
410 invarg("invalid port\n", *argv
);
411 port
= ntohs(pse
->s_port
);
412 } else if (port
> 0xffff)
413 invarg("invalid port\n", *argv
);
414 } else if (strcmp(*argv
, "vni") == 0) {
416 vni
= strtoul(*argv
, &endptr
, 0);
417 if ((endptr
&& *endptr
) ||
418 (vni
>> 24) || vni
== ULONG_MAX
)
419 invarg("invalid VNI\n", *argv
);
420 } else if (strcmp(*argv
, "via") == 0) {
422 via
= ll_name_to_index(*argv
);
425 } else if (strcmp(*argv
, "self") == 0) {
426 req
.ndm
.ndm_flags
|= NTF_SELF
;
427 } else if (matches(*argv
, "master") == 0) {
428 req
.ndm
.ndm_flags
|= NTF_MASTER
;
429 } else if (matches(*argv
, "router") == 0) {
430 req
.ndm
.ndm_flags
|= NTF_ROUTER
;
431 } else if (matches(*argv
, "local") == 0 ||
432 matches(*argv
, "permanent") == 0) {
433 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
434 } else if (matches(*argv
, "temp") == 0 ||
435 matches(*argv
, "static") == 0) {
436 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
437 } else if (matches(*argv
, "dynamic") == 0) {
438 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
439 req
.ndm
.ndm_state
&= ~NUD_NOARP
;
440 } else if (matches(*argv
, "vlan") == 0) {
442 duparg2("vlan", *argv
);
445 } else if (matches(*argv
, "use") == 0) {
446 req
.ndm
.ndm_flags
|= NTF_USE
;
447 } else if (matches(*argv
, "extern_learn") == 0) {
448 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
449 } else if (matches(*argv
, "sticky") == 0) {
450 req
.ndm
.ndm_flags
|= NTF_STICKY
;
452 if (strcmp(*argv
, "to") == 0)
455 if (matches(*argv
, "help") == 0)
458 duparg2("to", *argv
);
464 if (d
== NULL
|| addr
== NULL
) {
465 fprintf(stderr
, "Device and address are required arguments.\n");
470 if (!(req
.ndm
.ndm_flags
&(NTF_SELF
|NTF_MASTER
)))
471 req
.ndm
.ndm_flags
|= NTF_SELF
;
473 /* Assume permanent */
474 if (!(req
.ndm
.ndm_state
&(NUD_PERMANENT
|NUD_REACHABLE
)))
475 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
477 if (sscanf(addr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
478 abuf
, abuf
+1, abuf
+2,
479 abuf
+3, abuf
+4, abuf
+5) != 6) {
480 fprintf(stderr
, "Invalid mac address %s\n", addr
);
484 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
486 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
);
489 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
492 unsigned short dport
;
494 dport
= htons((unsigned short)port
);
495 addattr16(&req
.n
, sizeof(req
), NDA_PORT
, dport
);
498 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
500 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, via
);
502 req
.ndm
.ndm_ifindex
= ll_name_to_index(d
);
503 if (!req
.ndm
.ndm_ifindex
)
506 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
512 int do_fdb(int argc
, char **argv
)
517 if (matches(*argv
, "add") == 0)
518 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
519 if (matches(*argv
, "append") == 0)
520 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_APPEND
, argc
-1, argv
+1);
521 if (matches(*argv
, "replace") == 0)
522 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
523 if (matches(*argv
, "delete") == 0)
524 return fdb_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
525 if (matches(*argv
, "show") == 0 ||
526 matches(*argv
, "lst") == 0 ||
527 matches(*argv
, "list") == 0)
528 return fdb_show(argc
-1, argv
+1);
529 if (matches(*argv
, "help") == 0)
532 return fdb_show(0, NULL
);
534 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv
);