]>
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 (if_indextoname(ifindex
, ifname
))
135 fprintf(fp
, "via %s ", ifname
);
137 fprintf(fp
, "via ifindex %u ", ifindex
);
141 if (show_stats
&& tb
[NDA_CACHEINFO
]) {
142 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
143 int hz
= get_user_hz();
145 fprintf(fp
, "used %d/%d ", ci
->ndm_used
/hz
,
148 if (r
->ndm_flags
& NTF_SELF
)
149 fprintf(fp
, "self ");
151 fprintf(fp
, "master %s ",
152 ll_index_to_name(rta_getattr_u32(tb
[NDA_MASTER
])));
153 else if (r
->ndm_flags
& NTF_MASTER
)
154 fprintf(fp
, "master ");
155 if (r
->ndm_flags
& NTF_ROUTER
)
156 fprintf(fp
, "router ");
157 if (r
->ndm_flags
& NTF_EXT_LEARNED
)
158 fprintf(fp
, "external ");
160 fprintf(fp
, "%s\n", state_n2a(r
->ndm_state
));
164 static int fdb_show(int argc
, char **argv
)
168 struct ifinfomsg ifm
;
172 char *filter_dev
= NULL
;
174 int msg_size
= sizeof(struct ifinfomsg
);
176 memset(&req
, 0, sizeof(req
));
177 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
178 req
.ifm
.ifi_family
= PF_BRIDGE
;
181 if ((strcmp(*argv
, "brport") == 0) || strcmp(*argv
, "dev") == 0) {
184 } else if (strcmp(*argv
, "br") == 0) {
188 if (matches(*argv
, "help") == 0)
195 int br_ifindex
= ll_name_to_index(br
);
196 if (br_ifindex
== 0) {
197 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", br
);
200 addattr32(&req
.n
, sizeof(req
), IFLA_MASTER
, br_ifindex
);
201 msg_size
+= RTA_LENGTH(4);
204 /*we'll keep around filter_dev for older kernels */
206 filter_index
= if_nametoindex(filter_dev
);
207 if (filter_index
== 0) {
208 fprintf(stderr
, "Cannot find device \"%s\"\n",
212 req
.ifm
.ifi_index
= filter_index
;
215 if (rtnl_dump_request(&rth
, RTM_GETNEIGH
, &req
.ifm
, msg_size
) < 0) {
216 perror("Cannot send dump request");
220 if (rtnl_dump_filter(&rth
, print_fdb
, stdout
) < 0) {
221 fprintf(stderr
, "Dump terminated\n");
228 static int fdb_modify(int cmd
, int flags
, int argc
, char **argv
)
240 unsigned long port
= 0;
241 unsigned long vni
= ~0;
242 unsigned int via
= 0;
246 memset(&req
, 0, sizeof(req
));
248 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
249 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
250 req
.n
.nlmsg_type
= cmd
;
251 req
.ndm
.ndm_family
= PF_BRIDGE
;
252 req
.ndm
.ndm_state
= NUD_NOARP
;
255 if (strcmp(*argv
, "dev") == 0) {
258 } else if (strcmp(*argv
, "dst") == 0) {
261 duparg2("dst", *argv
);
262 get_addr(&dst
, *argv
, preferred_family
);
264 } else if (strcmp(*argv
, "port") == 0) {
267 port
= strtoul(*argv
, &endptr
, 0);
268 if (endptr
&& *endptr
) {
271 pse
= getservbyname(*argv
, "udp");
273 invarg("invalid port\n", *argv
);
274 port
= ntohs(pse
->s_port
);
275 } else if (port
> 0xffff)
276 invarg("invalid port\n", *argv
);
277 } else if (strcmp(*argv
, "vni") == 0) {
279 vni
= strtoul(*argv
, &endptr
, 0);
280 if ((endptr
&& *endptr
) ||
281 (vni
>> 24) || vni
== ULONG_MAX
)
282 invarg("invalid VNI\n", *argv
);
283 } else if (strcmp(*argv
, "via") == 0) {
285 via
= if_nametoindex(*argv
);
287 invarg("invalid device\n", *argv
);
288 } else if (strcmp(*argv
, "self") == 0) {
289 req
.ndm
.ndm_flags
|= NTF_SELF
;
290 } else if (matches(*argv
, "master") == 0) {
291 req
.ndm
.ndm_flags
|= NTF_MASTER
;
292 } else if (matches(*argv
, "router") == 0) {
293 req
.ndm
.ndm_flags
|= NTF_ROUTER
;
294 } else if (matches(*argv
, "local") == 0||
295 matches(*argv
, "permanent") == 0) {
296 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
297 } else if (matches(*argv
, "temp") == 0) {
298 req
.ndm
.ndm_state
|= NUD_REACHABLE
;
299 } else if (matches(*argv
, "vlan") == 0) {
301 duparg2("vlan", *argv
);
305 if (strcmp(*argv
, "to") == 0) {
308 if (matches(*argv
, "help") == 0)
311 duparg2("to", *argv
);
317 if (d
== NULL
|| addr
== NULL
) {
318 fprintf(stderr
, "Device and address are required arguments.\n");
323 if (!(req
.ndm
.ndm_flags
&(NTF_SELF
|NTF_MASTER
)))
324 req
.ndm
.ndm_flags
|= NTF_SELF
;
326 /* Assume permanent */
327 if (!(req
.ndm
.ndm_state
&(NUD_PERMANENT
|NUD_REACHABLE
)))
328 req
.ndm
.ndm_state
|= NUD_PERMANENT
;
330 if (sscanf(addr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
331 abuf
, abuf
+1, abuf
+2,
332 abuf
+3, abuf
+4, abuf
+5) != 6) {
333 fprintf(stderr
, "Invalid mac address %s\n", addr
);
337 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, abuf
, ETH_ALEN
);
339 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
);
342 addattr16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
345 unsigned short dport
;
347 dport
= htons((unsigned short)port
);
348 addattr16(&req
.n
, sizeof(req
), NDA_PORT
, dport
);
351 addattr32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
353 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, via
);
355 req
.ndm
.ndm_ifindex
= ll_name_to_index(d
);
356 if (req
.ndm
.ndm_ifindex
== 0) {
357 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
361 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
367 int do_fdb(int argc
, char **argv
)
372 if (matches(*argv
, "add") == 0)
373 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
374 if (matches(*argv
, "append") == 0)
375 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_APPEND
, argc
-1, argv
+1);
376 if (matches(*argv
, "replace") == 0)
377 return fdb_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
378 if (matches(*argv
, "delete") == 0)
379 return fdb_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
380 if (matches(*argv
, "show") == 0 ||
381 matches(*argv
, "lst") == 0 ||
382 matches(*argv
, "list") == 0)
383 return fdb_show(argc
-1, argv
+1);
384 if (matches(*argv
, "help") == 0)
387 return fdb_show(0, NULL
);
389 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv
);