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