]>
git.proxmox.com Git - mirror_iproute2.git/blob - bridge/fdb.c
2 * Get/set/delete fdb table with netlink
4 * TODO: merge/replace this with ip neighbour
6 * Authors: Stephen Hemminger <shemminger@vyatta.com>
15 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <linux/if_bridge.h>
20 #include <linux/if_ether.h>
21 #include <linux/neighbour.h>
25 #include "libnetlink.h"
26 #include "br_common.h"
30 static unsigned int filter_index
;
32 static void usage(void)
34 fprintf(stderr
, "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV {self|master} [ temp ]\n"
35 " [router] [ dst IPADDR] [ vlan VID ]\n"
36 " [ port PORT] [ vni VNI ] [via DEV]\n");
37 fprintf(stderr
, " bridge fdb {show} [ br BRDEV ] [ brport DEV ]\n");
41 static const char *state_n2a(unsigned s
)
45 if (s
& NUD_PERMANENT
)
54 if (s
& NUD_REACHABLE
)
57 sprintf(buf
, "state=%#x", s
);
61 int print_fdb(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
64 struct ndmsg
*r
= NLMSG_DATA(n
);
65 int len
= n
->nlmsg_len
;
66 struct rtattr
* tb
[NDA_MAX
+1];
68 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
) {
69 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
70 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
75 len
-= NLMSG_LENGTH(sizeof(*r
));
77 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
81 if (r
->ndm_family
!= AF_BRIDGE
)
84 if (filter_index
&& filter_index
!= r
->ndm_ifindex
)
87 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
),
88 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
90 if (n
->nlmsg_type
== RTM_DELNEIGH
)
91 fprintf(fp
, "Deleted ");
96 ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
97 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
98 ll_index_to_type(r
->ndm_ifindex
),
102 if (!filter_index
&& r
->ndm_ifindex
)
103 fprintf(fp
, "dev %s ", ll_index_to_name(r
->ndm_ifindex
));
107 int family
= AF_INET
;
109 if (RTA_PAYLOAD(tb
[NDA_DST
]) == sizeof(struct in6_addr
))
112 fprintf(fp
, "dst %s ",
114 RTA_PAYLOAD(tb
[NDA_DST
]),
115 RTA_DATA(tb
[NDA_DST
]),
116 abuf
, sizeof(abuf
)));
120 __u16 vid
= rta_getattr_u16(tb
[NDA_VLAN
]);
121 fprintf(fp
, "vlan %hu ", vid
);
125 fprintf(fp
, "port %d ", ntohs(rta_getattr_u16(tb
[NDA_PORT
])));
127 fprintf(fp
, "vni %d ", rta_getattr_u32(tb
[NDA_VNI
]));
128 if (tb
[NDA_IFINDEX
]) {
129 unsigned int ifindex
= rta_getattr_u32(tb
[NDA_IFINDEX
]);
132 char ifname
[IF_NAMESIZE
];
134 if (!tb
[NDA_LINK_NETNSID
] &&
135 if_indextoname(ifindex
, ifname
))
136 fprintf(fp
, "via %s ", ifname
);
138 fprintf(fp
, "via ifindex %u ", ifindex
);
141 if (tb
[NDA_LINK_NETNSID
])
142 fprintf(fp
, "link-netnsid %d ",
143 rta_getattr_u32(tb
[NDA_LINK_NETNSID
]));
145 if (show_stats
&& tb
[NDA_CACHEINFO
]) {
146 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
147 int hz
= get_user_hz();
149 fprintf(fp
, "used %d/%d ", ci
->ndm_used
/hz
,
152 if (r
->ndm_flags
& NTF_SELF
)
153 fprintf(fp
, "self ");
155 fprintf(fp
, "master %s ",
156 ll_index_to_name(rta_getattr_u32(tb
[NDA_MASTER
])));
157 else if (r
->ndm_flags
& NTF_MASTER
)
158 fprintf(fp
, "master ");
159 if (r
->ndm_flags
& NTF_ROUTER
)
160 fprintf(fp
, "router ");
161 if (r
->ndm_flags
& NTF_EXT_LEARNED
)
162 fprintf(fp
, "offload ");
164 fprintf(fp
, "%s\n", state_n2a(r
->ndm_state
));
168 static int fdb_show(int argc
, char **argv
)
172 struct ifinfomsg ifm
;
176 char *filter_dev
= NULL
;
178 int msg_size
= sizeof(struct ifinfomsg
);
180 memset(&req
, 0, sizeof(req
));
181 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
182 req
.ifm
.ifi_family
= PF_BRIDGE
;
185 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
188 } else if (strcmp(*argv
, "br") == 0) {
192 if (matches(*argv
, "help") == 0)
199 int br_ifindex
= ll_name_to_index(br
);
200 if (br_ifindex
== 0) {
201 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
204 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, br_ifindex
);
205 msg_size
+= RTA_LENGTH(4);
208 /*we'll keep around filter_dev for older kernels */
210 filter_index
= if_nametoindex(filter_dev
);
211 if (filter_index
== 0) {
212 fprintf(stderr
, "Cannot find device \"%s\"\n",
216 req
.ifm
.ifi_index
= filter_index
;
219 if (rtnl_dump_request(&rth
, RTM_GETNEIGH
, &req
.ifm
, msg_size
) < 0) {
220 perror("Cannot send dump request");
224 if (rtnl_dump_filter(&rth
, print_fdb
, stdout
) < 0) {
225 fprintf(stderr
, "Dump terminated\n");
232 static int fdb_modify(int cmd
, int flags
, int argc
, char **argv
)
244 unsigned long port
= 0;
245 unsigned long vni
= ~0;
246 unsigned int via
= 0;
250 memset(&req
, 0, sizeof(req
));
252 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
253 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
254 req
.n
.nlmsg_type
= cmd
;
255 req
.ndm
.ndm_family
= PF_BRIDGE
;
256 req
.ndm
.ndm_state
= NUD_NOARP
;
259 if (strcmp(*argv
, "dev") == 0) {
262 } else if (strcmp(*argv
, "dst") == 0) {
265 duparg2("dst", *argv
);
266 get_addr(&dst
, *argv
, preferred_family
);
268 } else if (strcmp(*argv
, "port") == 0) {
271 port
= strtoul(*argv
, &endptr
, 0);
272 if (endptr
&& *endptr
) {
275 pse
= getservbyname(*argv
, "udp");
277 invarg("invalid port\n", *argv
);
278 port
= ntohs(pse
->s_port
);
279 } else if (port
> 0xffff)
280 invarg("invalid port\n", *argv
);
281 } else if (strcmp(*argv
, "vni") == 0) {
283 vni
= strtoul(*argv
, &endptr
, 0);
284 if ((endptr
&& *endptr
) ||
285 (vni
>> 24) || vni
== ULONG_MAX
)
286 invarg("invalid VNI\n", *argv
);
287 } else if (strcmp(*argv
, "via") == 0) {
289 via
= if_nametoindex(*argv
);
291 invarg("invalid device\n", *argv
);
292 } else if (strcmp(*argv
, "self") == 0) {
293 req
.ndm
.ndm_flags
|= NTF_SELF
;
294 } else if (matches(*argv
, "master") == 0) {
295 req
.ndm
.ndm_flags
|= NTF_MASTER
;
296 } else if (matches(*argv
, "router") == 0) {
297 req
.ndm
.ndm_flags
|= NTF_ROUTER
;
298 } else if (matches(*argv
, "local") == 0||
299 matches(*argv
, "permanent") == 0) {
300 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
301 } else if (matches(*argv
, "temp") == 0) {
302 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
303 } else if (matches(*argv
, "vlan") == 0) {
305 duparg2("vlan", *argv
);
309 if (strcmp(*argv
, "to") == 0) {
312 if (matches(*argv
, "help") == 0)
315 duparg2("to", *argv
);
321 if (d
== NULL
|| addr
== NULL
) {
322 fprintf(stderr
, "Device and address are required arguments.\n");
327 if (!(req
.ndm
.ndm_flags
&(NTF_SELF
|NTF_MASTER
)))
328 req
.ndm
.ndm_flags
|= NTF_SELF
;
330 /* Assume permanent */
331 if (!(req
.ndm
.ndm_state
&(NUD_PERMANENT
|NUD_REACHABLE
)))
332 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
334 if (sscanf(addr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
335 abuf
, abuf
+1, abuf
+2,
336 abuf
+3, abuf
+4, abuf
+5) != 6) {
337 fprintf(stderr
, "Invalid mac address %s\n", addr
);
341 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
343 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
);
346 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
349 unsigned short dport
;
351 dport
= htons((unsigned short)port
);
352 addattr16(&req
.n
, sizeof(req
), NDA_PORT
, dport
);
355 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
357 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, via
);
359 req
.ndm
.ndm_ifindex
= ll_name_to_index(d
);
360 if (req
.ndm
.ndm_ifindex
== 0) {
361 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
365 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
371 int do_fdb(int argc
, char **argv
)
376 if (matches(*argv
, "add") == 0)
377 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
378 if (matches(*argv
, "append") == 0)
379 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_APPEND
, argc
-1, argv
+1);
380 if (matches(*argv
, "replace") == 0)
381 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
382 if (matches(*argv
, "delete") == 0)
383 return fdb_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
384 if (matches(*argv
, "show") == 0 ||
385 matches(*argv
, "lst") == 0 ||
386 matches(*argv
, "list") == 0)
387 return fdb_show(argc
-1, argv
+1);
388 if (matches(*argv
, "help") == 0)
391 return fdb_show(0, NULL
);
393 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv
);