]>
Commit | Line | Data |
---|---|---|
d04bc300 SH |
1 | /* |
2 | * Get/set/delete fdb table with netlink | |
3 | * | |
0849e60a SH |
4 | * TODO: merge/replace this with ip neighbour |
5 | * | |
d04bc300 SH |
6 | * Authors: Stephen Hemminger <shemminger@vyatta.com> |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <unistd.h> | |
5f409678 | 12 | #include <netdb.h> |
d04bc300 SH |
13 | #include <time.h> |
14 | #include <fcntl.h> | |
15 | #include <sys/socket.h> | |
16 | #include <sys/time.h> | |
17 | #include <net/if.h> | |
18 | #include <netinet/in.h> | |
19 | #include <linux/if_bridge.h> | |
20 | #include <linux/if_ether.h> | |
21 | #include <linux/neighbour.h> | |
22 | #include <string.h> | |
dd9cc0ee | 23 | #include <limits.h> |
b239c56e AK |
24 | #include <json_writer.h> |
25 | #include <stdbool.h> | |
d04bc300 SH |
26 | |
27 | #include "libnetlink.h" | |
28 | #include "br_common.h" | |
0849e60a | 29 | #include "rt_names.h" |
d04bc300 SH |
30 | #include "utils.h" |
31 | ||
7f10090b | 32 | static unsigned int filter_index, filter_vlan, filter_state; |
d04bc300 | 33 | |
b239c56e AK |
34 | json_writer_t *jw_global; |
35 | ||
d04bc300 SH |
36 | static void usage(void) |
37 | { | |
bd5bbad4 | 38 | fprintf(stderr, "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV\n" |
cd8df30b | 39 | " [ self ] [ master ] [ use ] [ router ]\n" |
c6d0cfb5 | 40 | " [ local | static | dynamic ] [ dst IPADDR ] [ vlan VID ]\n" |
df4b043f | 41 | " [ port PORT] [ vni VNI ] [ via DEV ]\n"); |
7f10090b | 42 | fprintf(stderr, " bridge fdb [ show [ br BRDEV ] [ brport DEV ] [ vlan VID ] [ state STATE ] ]\n"); |
d04bc300 SH |
43 | exit(-1); |
44 | } | |
45 | ||
df4b043f | 46 | static const char *state_n2a(unsigned int s) |
d04bc300 SH |
47 | { |
48 | static char buf[32]; | |
49 | ||
0849e60a SH |
50 | if (s & NUD_PERMANENT) |
51 | return "permanent"; | |
d04bc300 SH |
52 | |
53 | if (s & NUD_NOARP) | |
54 | return "static"; | |
55 | ||
56 | if (s & NUD_STALE) | |
57 | return "stale"; | |
0849e60a | 58 | |
d04bc300 SH |
59 | if (s & NUD_REACHABLE) |
60 | return ""; | |
61 | ||
62 | sprintf(buf, "state=%#x", s); | |
63 | return buf; | |
64 | } | |
65 | ||
7f10090b NA |
66 | static int state_a2n(unsigned int *s, const char *arg) |
67 | { | |
68 | if (matches(arg, "permanent") == 0) | |
69 | *s = NUD_PERMANENT; | |
70 | else if (matches(arg, "static") == 0 || matches(arg, "temp") == 0) | |
71 | *s = NUD_NOARP; | |
72 | else if (matches(arg, "stale") == 0) | |
73 | *s = NUD_STALE; | |
74 | else if (matches(arg, "reachable") == 0 || matches(arg, "dynamic") == 0) | |
75 | *s = NUD_REACHABLE; | |
76 | else if (strcmp(arg, "all") == 0) | |
77 | *s = ~0; | |
78 | else if (get_unsigned(s, arg, 0)) | |
79 | return -1; | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
b239c56e AK |
84 | static void start_json_fdb_flags_array(bool *fdb_flags) |
85 | { | |
86 | if (*fdb_flags) | |
87 | return; | |
88 | jsonw_name(jw_global, "flags"); | |
89 | jsonw_start_array(jw_global); | |
90 | *fdb_flags = true; | |
91 | } | |
92 | ||
d04bc300 SH |
93 | int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
94 | { | |
0849e60a | 95 | FILE *fp = arg; |
d04bc300 SH |
96 | struct ndmsg *r = NLMSG_DATA(n); |
97 | int len = n->nlmsg_len; | |
df4b043f | 98 | struct rtattr *tb[NDA_MAX+1]; |
ae6eb907 | 99 | __u16 vid = 0; |
b239c56e AK |
100 | bool fdb_flags = false; |
101 | const char *state_s; | |
0849e60a SH |
102 | |
103 | if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) { | |
104 | fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", | |
105 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
0849e60a SH |
106 | return 0; |
107 | } | |
d04bc300 SH |
108 | |
109 | len -= NLMSG_LENGTH(sizeof(*r)); | |
110 | if (len < 0) { | |
111 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
112 | return -1; | |
113 | } | |
114 | ||
115 | if (r->ndm_family != AF_BRIDGE) | |
116 | return 0; | |
117 | ||
118 | if (filter_index && filter_index != r->ndm_ifindex) | |
119 | return 0; | |
120 | ||
7f10090b NA |
121 | if (filter_state && !(r->ndm_state & filter_state)) |
122 | return 0; | |
123 | ||
d04bc300 SH |
124 | parse_rtattr(tb, NDA_MAX, NDA_RTA(r), |
125 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
126 | ||
ae6eb907 NA |
127 | if (tb[NDA_VLAN]) |
128 | vid = rta_getattr_u16(tb[NDA_VLAN]); | |
129 | ||
130 | if (filter_vlan && filter_vlan != vid) | |
131 | return 0; | |
132 | ||
afd3921e | 133 | if (jw_global) { |
134 | jsonw_pretty(jw_global, 1); | |
135 | jsonw_start_object(jw_global); | |
136 | } | |
137 | ||
b239c56e AK |
138 | if (n->nlmsg_type == RTM_DELNEIGH) { |
139 | if (jw_global) | |
140 | jsonw_string_field(jw_global, "opCode", "deleted"); | |
141 | else | |
142 | fprintf(fp, "Deleted "); | |
143 | } | |
0849e60a SH |
144 | |
145 | if (tb[NDA_LLADDR]) { | |
146 | SPRINT_BUF(b1); | |
b239c56e AK |
147 | ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), |
148 | RTA_PAYLOAD(tb[NDA_LLADDR]), | |
149 | ll_index_to_type(r->ndm_ifindex), | |
150 | b1, sizeof(b1)); | |
151 | if (jw_global) | |
152 | jsonw_string_field(jw_global, "mac", b1); | |
153 | else | |
154 | fprintf(fp, "%s ", b1); | |
d04bc300 | 155 | } |
38df7ac9 | 156 | |
b239c56e AK |
157 | if (!filter_index && r->ndm_ifindex) { |
158 | if (jw_global) | |
159 | jsonw_string_field(jw_global, "dev", | |
160 | ll_index_to_name(r->ndm_ifindex)); | |
161 | else | |
162 | fprintf(fp, "dev %s ", | |
163 | ll_index_to_name(r->ndm_ifindex)); | |
164 | } | |
0849e60a SH |
165 | |
166 | if (tb[NDA_DST]) { | |
9e763fa5 | 167 | int family = AF_INET; |
b239c56e | 168 | const char *abuf_s; |
9e763fa5 MR |
169 | |
170 | if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr)) | |
171 | family = AF_INET6; | |
172 | ||
b239c56e AK |
173 | abuf_s = format_host(family, |
174 | RTA_PAYLOAD(tb[NDA_DST]), | |
175 | RTA_DATA(tb[NDA_DST])); | |
176 | if (jw_global) | |
177 | jsonw_string_field(jw_global, "dst", abuf_s); | |
178 | else | |
179 | fprintf(fp, "dst %s ", abuf_s); | |
180 | } | |
181 | ||
182 | if (vid) { | |
183 | if (jw_global) | |
184 | jsonw_uint_field(jw_global, "vlan", vid); | |
185 | else | |
186 | fprintf(fp, "vlan %hu ", vid); | |
187 | } | |
188 | ||
189 | if (tb[NDA_PORT]) { | |
190 | if (jw_global) | |
191 | jsonw_uint_field(jw_global, "port", | |
aab0f610 | 192 | rta_getattr_be16(tb[NDA_PORT])); |
b239c56e AK |
193 | else |
194 | fprintf(fp, "port %d ", | |
aab0f610 | 195 | rta_getattr_be16(tb[NDA_PORT])); |
0849e60a | 196 | } |
38df7ac9 | 197 | |
b239c56e AK |
198 | if (tb[NDA_VNI]) { |
199 | if (jw_global) | |
200 | jsonw_uint_field(jw_global, "vni", | |
201 | rta_getattr_u32(tb[NDA_VNI])); | |
202 | else | |
203 | fprintf(fp, "vni %d ", | |
204 | rta_getattr_u32(tb[NDA_VNI])); | |
205 | } | |
fd08839c | 206 | |
5f409678 DS |
207 | if (tb[NDA_IFINDEX]) { |
208 | unsigned int ifindex = rta_getattr_u32(tb[NDA_IFINDEX]); | |
209 | ||
210 | if (ifindex) { | |
211 | char ifname[IF_NAMESIZE]; | |
212 | ||
a4797670 | 213 | if (!tb[NDA_LINK_NETNSID] && |
b239c56e AK |
214 | if_indextoname(ifindex, ifname)) { |
215 | if (jw_global) | |
216 | jsonw_string_field(jw_global, "viaIf", | |
217 | ifname); | |
218 | else | |
219 | fprintf(fp, "via %s ", ifname); | |
220 | } else { | |
221 | if (jw_global) | |
222 | jsonw_uint_field(jw_global, "viaIfIndex", | |
223 | ifindex); | |
224 | else | |
225 | fprintf(fp, "via ifindex %u ", ifindex); | |
226 | } | |
5f409678 DS |
227 | } |
228 | } | |
b239c56e AK |
229 | |
230 | if (tb[NDA_LINK_NETNSID]) { | |
231 | if (jw_global) | |
232 | jsonw_uint_field(jw_global, "linkNetNsId", | |
233 | rta_getattr_u32(tb[NDA_LINK_NETNSID])); | |
234 | else | |
235 | fprintf(fp, "link-netnsid %d ", | |
236 | rta_getattr_u32(tb[NDA_LINK_NETNSID])); | |
237 | } | |
5f409678 | 238 | |
d04bc300 SH |
239 | if (show_stats && tb[NDA_CACHEINFO]) { |
240 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); | |
0849e60a | 241 | int hz = get_user_hz(); |
d04bc300 | 242 | |
b239c56e AK |
243 | if (jw_global) { |
244 | jsonw_uint_field(jw_global, "used", | |
245 | ci->ndm_used/hz); | |
246 | jsonw_uint_field(jw_global, "updated", | |
247 | ci->ndm_updated/hz); | |
248 | } else { | |
249 | fprintf(fp, "used %d/%d ", ci->ndm_used/hz, | |
250 | ci->ndm_updated/hz); | |
251 | } | |
252 | } | |
253 | ||
254 | if (jw_global) { | |
255 | if (r->ndm_flags & NTF_SELF) { | |
256 | start_json_fdb_flags_array(&fdb_flags); | |
257 | jsonw_string(jw_global, "self"); | |
258 | } | |
259 | if (r->ndm_flags & NTF_ROUTER) { | |
260 | start_json_fdb_flags_array(&fdb_flags); | |
261 | jsonw_string(jw_global, "router"); | |
262 | } | |
263 | if (r->ndm_flags & NTF_EXT_LEARNED) { | |
8a38e44f AS |
264 | start_json_fdb_flags_array(&fdb_flags); |
265 | jsonw_string(jw_global, "extern_learn"); | |
266 | } | |
267 | if (r->ndm_flags & NTF_OFFLOADED) { | |
b239c56e AK |
268 | start_json_fdb_flags_array(&fdb_flags); |
269 | jsonw_string(jw_global, "offload"); | |
270 | } | |
271 | if (r->ndm_flags & NTF_MASTER) | |
272 | jsonw_string(jw_global, "master"); | |
273 | if (fdb_flags) | |
274 | jsonw_end_array(jw_global); | |
275 | ||
276 | if (tb[NDA_MASTER]) | |
277 | jsonw_string_field(jw_global, | |
278 | "master", | |
279 | ll_index_to_name(rta_getattr_u32(tb[NDA_MASTER]))); | |
280 | ||
281 | } else { | |
282 | if (r->ndm_flags & NTF_SELF) | |
283 | fprintf(fp, "self "); | |
284 | if (r->ndm_flags & NTF_ROUTER) | |
285 | fprintf(fp, "router "); | |
286 | if (r->ndm_flags & NTF_EXT_LEARNED) | |
8a38e44f AS |
287 | fprintf(fp, "extern_learn "); |
288 | if (r->ndm_flags & NTF_OFFLOADED) | |
b239c56e AK |
289 | fprintf(fp, "offload "); |
290 | if (tb[NDA_MASTER]) { | |
291 | fprintf(fp, "master %s ", | |
292 | ll_index_to_name(rta_getattr_u32(tb[NDA_MASTER]))); | |
293 | } else if (r->ndm_flags & NTF_MASTER) { | |
294 | fprintf(fp, "master "); | |
295 | } | |
296 | } | |
297 | ||
298 | state_s = state_n2a(r->ndm_state); | |
299 | if (jw_global) { | |
300 | if (state_s[0]) | |
301 | jsonw_string_field(jw_global, "state", state_s); | |
302 | ||
303 | jsonw_end_object(jw_global); | |
304 | } else { | |
305 | fprintf(fp, "%s\n", state_s); | |
306 | ||
307 | fflush(fp); | |
d04bc300 | 308 | } |
4d45bf3b | 309 | |
d04bc300 SH |
310 | return 0; |
311 | } | |
312 | ||
313 | static int fdb_show(int argc, char **argv) | |
314 | { | |
aeb14eb0 | 315 | struct { |
df4b043f | 316 | struct nlmsghdr n; |
aeb14eb0 | 317 | struct ifinfomsg ifm; |
df4b043f | 318 | char buf[256]; |
d17b136f PS |
319 | } req = { |
320 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
321 | .ifm.ifi_family = PF_BRIDGE, | |
322 | }; | |
aeb14eb0 | 323 | |
d04bc300 | 324 | char *filter_dev = NULL; |
aeb14eb0 JHS |
325 | char *br = NULL; |
326 | int msg_size = sizeof(struct ifinfomsg); | |
327 | ||
d04bc300 | 328 | while (argc > 0) { |
aeb14eb0 | 329 | if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) { |
d04bc300 | 330 | NEXT_ARG(); |
d04bc300 | 331 | filter_dev = *argv; |
aeb14eb0 JHS |
332 | } else if (strcmp(*argv, "br") == 0) { |
333 | NEXT_ARG(); | |
334 | br = *argv; | |
ae6eb907 NA |
335 | } else if (strcmp(*argv, "vlan") == 0) { |
336 | NEXT_ARG(); | |
337 | if (filter_vlan) | |
338 | duparg("vlan", *argv); | |
339 | filter_vlan = atoi(*argv); | |
7f10090b NA |
340 | } else if (strcmp(*argv, "state") == 0) { |
341 | unsigned int state; | |
342 | ||
343 | NEXT_ARG(); | |
344 | if (state_a2n(&state, *argv)) | |
345 | invarg("invalid state", *argv); | |
346 | filter_state |= state; | |
aeb14eb0 JHS |
347 | } else { |
348 | if (matches(*argv, "help") == 0) | |
349 | usage(); | |
d04bc300 SH |
350 | } |
351 | argc--; argv++; | |
352 | } | |
353 | ||
aeb14eb0 JHS |
354 | if (br) { |
355 | int br_ifindex = ll_name_to_index(br); | |
df4b043f | 356 | |
aeb14eb0 JHS |
357 | if (br_ifindex == 0) { |
358 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", br); | |
359 | return -1; | |
360 | } | |
361 | addattr32(&req.n, sizeof(req), IFLA_MASTER, br_ifindex); | |
362 | msg_size += RTA_LENGTH(4); | |
363 | } | |
364 | ||
365 | /*we'll keep around filter_dev for older kernels */ | |
d04bc300 | 366 | if (filter_dev) { |
0849e60a SH |
367 | filter_index = if_nametoindex(filter_dev); |
368 | if (filter_index == 0) { | |
369 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
370 | filter_dev); | |
d04bc300 SH |
371 | return -1; |
372 | } | |
aeb14eb0 | 373 | req.ifm.ifi_index = filter_index; |
d04bc300 SH |
374 | } |
375 | ||
aeb14eb0 | 376 | if (rtnl_dump_request(&rth, RTM_GETNEIGH, &req.ifm, msg_size) < 0) { |
d04bc300 SH |
377 | perror("Cannot send dump request"); |
378 | exit(1); | |
379 | } | |
d04bc300 | 380 | |
b239c56e AK |
381 | if (json_output) { |
382 | jw_global = jsonw_new(stdout); | |
383 | if (!jw_global) { | |
384 | fprintf(stderr, "Error allocation json object\n"); | |
385 | exit(1); | |
386 | } | |
387 | jsonw_start_array(jw_global); | |
388 | } | |
0849e60a | 389 | if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) { |
d04bc300 SH |
390 | fprintf(stderr, "Dump terminated\n"); |
391 | exit(1); | |
392 | } | |
b239c56e AK |
393 | if (jw_global) { |
394 | jsonw_end_array(jw_global); | |
395 | jsonw_destroy(&jw_global); | |
396 | } | |
d04bc300 SH |
397 | |
398 | return 0; | |
399 | } | |
400 | ||
401 | static int fdb_modify(int cmd, int flags, int argc, char **argv) | |
402 | { | |
403 | struct { | |
df4b043f SH |
404 | struct nlmsghdr n; |
405 | struct ndmsg ndm; | |
406 | char buf[256]; | |
d17b136f PS |
407 | } req = { |
408 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)), | |
409 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
410 | .n.nlmsg_type = cmd, | |
411 | .ndm.ndm_family = PF_BRIDGE, | |
412 | .ndm.ndm_state = NUD_NOARP, | |
413 | }; | |
d04bc300 SH |
414 | char *addr = NULL; |
415 | char *d = NULL; | |
416 | char abuf[ETH_ALEN]; | |
0849e60a SH |
417 | int dst_ok = 0; |
418 | inet_prefix dst; | |
5f409678 DS |
419 | unsigned long port = 0; |
420 | unsigned long vni = ~0; | |
421 | unsigned int via = 0; | |
422 | char *endptr; | |
fd08839c | 423 | short vid = -1; |
d04bc300 | 424 | |
d04bc300 SH |
425 | while (argc > 0) { |
426 | if (strcmp(*argv, "dev") == 0) { | |
427 | NEXT_ARG(); | |
428 | d = *argv; | |
0849e60a SH |
429 | } else if (strcmp(*argv, "dst") == 0) { |
430 | NEXT_ARG(); | |
431 | if (dst_ok) | |
432 | duparg2("dst", *argv); | |
433 | get_addr(&dst, *argv, preferred_family); | |
434 | dst_ok = 1; | |
5f409678 DS |
435 | } else if (strcmp(*argv, "port") == 0) { |
436 | ||
437 | NEXT_ARG(); | |
438 | port = strtoul(*argv, &endptr, 0); | |
439 | if (endptr && *endptr) { | |
440 | struct servent *pse; | |
441 | ||
442 | pse = getservbyname(*argv, "udp"); | |
443 | if (!pse) | |
444 | invarg("invalid port\n", *argv); | |
445 | port = ntohs(pse->s_port); | |
446 | } else if (port > 0xffff) | |
447 | invarg("invalid port\n", *argv); | |
448 | } else if (strcmp(*argv, "vni") == 0) { | |
449 | NEXT_ARG(); | |
450 | vni = strtoul(*argv, &endptr, 0); | |
451 | if ((endptr && *endptr) || | |
452 | (vni >> 24) || vni == ULONG_MAX) | |
453 | invarg("invalid VNI\n", *argv); | |
454 | } else if (strcmp(*argv, "via") == 0) { | |
455 | NEXT_ARG(); | |
456 | via = if_nametoindex(*argv); | |
457 | if (via == 0) | |
458 | invarg("invalid device\n", *argv); | |
dc6a6a25 JF |
459 | } else if (strcmp(*argv, "self") == 0) { |
460 | req.ndm.ndm_flags |= NTF_SELF; | |
0849e60a | 461 | } else if (matches(*argv, "master") == 0) { |
dc6a6a25 | 462 | req.ndm.ndm_flags |= NTF_MASTER; |
5b8a1d4a DS |
463 | } else if (matches(*argv, "router") == 0) { |
464 | req.ndm.ndm_flags |= NTF_ROUTER; | |
e9e9365b | 465 | } else if (matches(*argv, "local") == 0 || |
0849e60a SH |
466 | matches(*argv, "permanent") == 0) { |
467 | req.ndm.ndm_state |= NUD_PERMANENT; | |
a1987cd1 RP |
468 | } else if (matches(*argv, "temp") == 0 || |
469 | matches(*argv, "static") == 0) { | |
0849e60a | 470 | req.ndm.ndm_state |= NUD_REACHABLE; |
c6d0cfb5 RP |
471 | } else if (matches(*argv, "dynamic") == 0) { |
472 | req.ndm.ndm_state |= NUD_REACHABLE; | |
473 | req.ndm.ndm_state &= ~NUD_NOARP; | |
fd08839c VY |
474 | } else if (matches(*argv, "vlan") == 0) { |
475 | if (vid >= 0) | |
476 | duparg2("vlan", *argv); | |
477 | NEXT_ARG(); | |
478 | vid = atoi(*argv); | |
cd8df30b RP |
479 | } else if (matches(*argv, "use") == 0) { |
480 | req.ndm.ndm_flags |= NTF_USE; | |
d04bc300 SH |
481 | } else { |
482 | if (strcmp(*argv, "to") == 0) { | |
483 | NEXT_ARG(); | |
484 | } | |
0849e60a SH |
485 | if (matches(*argv, "help") == 0) |
486 | usage(); | |
d04bc300 SH |
487 | if (addr) |
488 | duparg2("to", *argv); | |
489 | addr = *argv; | |
490 | } | |
491 | argc--; argv++; | |
492 | } | |
493 | ||
494 | if (d == NULL || addr == NULL) { | |
495 | fprintf(stderr, "Device and address are required arguments.\n"); | |
42ecedd4 | 496 | return -1; |
d04bc300 SH |
497 | } |
498 | ||
0849e60a SH |
499 | /* Assume self */ |
500 | if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER))) | |
501 | req.ndm.ndm_flags |= NTF_SELF; | |
502 | ||
503 | /* Assume permanent */ | |
504 | if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE))) | |
505 | req.ndm.ndm_state |= NUD_PERMANENT; | |
506 | ||
507 | if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
d04bc300 SH |
508 | abuf, abuf+1, abuf+2, |
509 | abuf+3, abuf+4, abuf+5) != 6) { | |
510 | fprintf(stderr, "Invalid mac address %s\n", addr); | |
42ecedd4 | 511 | return -1; |
d04bc300 SH |
512 | } |
513 | ||
514 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN); | |
0849e60a SH |
515 | if (dst_ok) |
516 | addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen); | |
d04bc300 | 517 | |
fd08839c | 518 | if (vid >= 0) |
8ae66094 | 519 | addattr16(&req.n, sizeof(req), NDA_VLAN, vid); |
fd08839c | 520 | |
5f409678 DS |
521 | if (port) { |
522 | unsigned short dport; | |
523 | ||
524 | dport = htons((unsigned short)port); | |
525 | addattr16(&req.n, sizeof(req), NDA_PORT, dport); | |
526 | } | |
527 | if (vni != ~0) | |
528 | addattr32(&req.n, sizeof(req), NDA_VNI, vni); | |
529 | if (via) | |
530 | addattr32(&req.n, sizeof(req), NDA_IFINDEX, via); | |
531 | ||
d04bc300 SH |
532 | req.ndm.ndm_ifindex = ll_name_to_index(d); |
533 | if (req.ndm.ndm_ifindex == 0) { | |
534 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
535 | return -1; | |
536 | } | |
537 | ||
c079e121 | 538 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
42ecedd4 | 539 | return -1; |
d04bc300 SH |
540 | |
541 | return 0; | |
542 | } | |
543 | ||
544 | int do_fdb(int argc, char **argv) | |
545 | { | |
546 | ll_init_map(&rth); | |
547 | ||
548 | if (argc > 0) { | |
549 | if (matches(*argv, "add") == 0) | |
550 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
5f409678 DS |
551 | if (matches(*argv, "append") == 0) |
552 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_APPEND, argc-1, argv+1); | |
5464049b TR |
553 | if (matches(*argv, "replace") == 0) |
554 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
d04bc300 SH |
555 | if (matches(*argv, "delete") == 0) |
556 | return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
557 | if (matches(*argv, "show") == 0 || | |
558 | matches(*argv, "lst") == 0 || | |
559 | matches(*argv, "list") == 0) | |
560 | return fdb_show(argc-1, argv+1); | |
561 | if (matches(*argv, "help") == 0) | |
562 | usage(); | |
563 | } else | |
564 | return fdb_show(0, NULL); | |
565 | ||
083b46bb | 566 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv); |
d04bc300 SH |
567 | exit(-1); |
568 | } |