]>
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> |
d04bc300 SH |
24 | |
25 | #include "libnetlink.h" | |
26 | #include "br_common.h" | |
0849e60a | 27 | #include "rt_names.h" |
d04bc300 SH |
28 | #include "utils.h" |
29 | ||
9dca899b | 30 | static unsigned int filter_index; |
d04bc300 SH |
31 | |
32 | static void usage(void) | |
33 | { | |
bd5bbad4 | 34 | fprintf(stderr, "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV\n" |
cd8df30b | 35 | " [ self ] [ master ] [ use ] [ router ]\n" |
c6d0cfb5 | 36 | " [ local | static | dynamic ] [ dst IPADDR ] [ vlan VID ]\n" |
df4b043f | 37 | " [ port PORT] [ vni VNI ] [ via DEV ]\n"); |
bd5bbad4 | 38 | fprintf(stderr, " bridge fdb [ show [ br BRDEV ] [ brport DEV ] ]\n"); |
d04bc300 SH |
39 | exit(-1); |
40 | } | |
41 | ||
df4b043f | 42 | static const char *state_n2a(unsigned int s) |
d04bc300 SH |
43 | { |
44 | static char buf[32]; | |
45 | ||
0849e60a SH |
46 | if (s & NUD_PERMANENT) |
47 | return "permanent"; | |
d04bc300 SH |
48 | |
49 | if (s & NUD_NOARP) | |
50 | return "static"; | |
51 | ||
52 | if (s & NUD_STALE) | |
53 | return "stale"; | |
0849e60a | 54 | |
d04bc300 SH |
55 | if (s & NUD_REACHABLE) |
56 | return ""; | |
57 | ||
58 | sprintf(buf, "state=%#x", s); | |
59 | return buf; | |
60 | } | |
61 | ||
d04bc300 SH |
62 | int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
63 | { | |
0849e60a | 64 | FILE *fp = arg; |
d04bc300 SH |
65 | struct ndmsg *r = NLMSG_DATA(n); |
66 | int len = n->nlmsg_len; | |
df4b043f | 67 | struct rtattr *tb[NDA_MAX+1]; |
0849e60a SH |
68 | |
69 | if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) { | |
70 | fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", | |
71 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
72 | ||
73 | return 0; | |
74 | } | |
d04bc300 SH |
75 | |
76 | len -= NLMSG_LENGTH(sizeof(*r)); | |
77 | if (len < 0) { | |
78 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
79 | return -1; | |
80 | } | |
81 | ||
82 | if (r->ndm_family != AF_BRIDGE) | |
83 | return 0; | |
84 | ||
85 | if (filter_index && filter_index != r->ndm_ifindex) | |
86 | return 0; | |
87 | ||
88 | parse_rtattr(tb, NDA_MAX, NDA_RTA(r), | |
89 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
90 | ||
91 | if (n->nlmsg_type == RTM_DELNEIGH) | |
0849e60a SH |
92 | fprintf(fp, "Deleted "); |
93 | ||
94 | if (tb[NDA_LLADDR]) { | |
95 | SPRINT_BUF(b1); | |
96 | fprintf(fp, "%s ", | |
97 | ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), | |
98 | RTA_PAYLOAD(tb[NDA_LLADDR]), | |
99 | ll_index_to_type(r->ndm_ifindex), | |
100 | b1, sizeof(b1))); | |
d04bc300 | 101 | } |
38df7ac9 | 102 | |
0849e60a SH |
103 | if (!filter_index && r->ndm_ifindex) |
104 | fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex)); | |
105 | ||
106 | if (tb[NDA_DST]) { | |
9e763fa5 MR |
107 | int family = AF_INET; |
108 | ||
109 | if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr)) | |
110 | family = AF_INET6; | |
111 | ||
0849e60a | 112 | fprintf(fp, "dst %s ", |
9e763fa5 | 113 | format_host(family, |
0849e60a | 114 | RTA_PAYLOAD(tb[NDA_DST]), |
a418e451 | 115 | RTA_DATA(tb[NDA_DST]))); |
0849e60a | 116 | } |
38df7ac9 | 117 | |
fd08839c VY |
118 | if (tb[NDA_VLAN]) { |
119 | __u16 vid = rta_getattr_u16(tb[NDA_VLAN]); | |
df4b043f | 120 | |
fd08839c VY |
121 | fprintf(fp, "vlan %hu ", vid); |
122 | } | |
123 | ||
5f409678 DS |
124 | if (tb[NDA_PORT]) |
125 | fprintf(fp, "port %d ", ntohs(rta_getattr_u16(tb[NDA_PORT]))); | |
126 | if (tb[NDA_VNI]) | |
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]); | |
130 | ||
131 | if (ifindex) { | |
132 | char ifname[IF_NAMESIZE]; | |
133 | ||
a4797670 ND |
134 | if (!tb[NDA_LINK_NETNSID] && |
135 | if_indextoname(ifindex, ifname)) | |
5f409678 DS |
136 | fprintf(fp, "via %s ", ifname); |
137 | else | |
138 | fprintf(fp, "via ifindex %u ", ifindex); | |
139 | } | |
140 | } | |
a4797670 ND |
141 | if (tb[NDA_LINK_NETNSID]) |
142 | fprintf(fp, "link-netnsid %d ", | |
143 | rta_getattr_u32(tb[NDA_LINK_NETNSID])); | |
5f409678 | 144 | |
d04bc300 SH |
145 | if (show_stats && tb[NDA_CACHEINFO]) { |
146 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); | |
0849e60a | 147 | int hz = get_user_hz(); |
d04bc300 | 148 | |
85c1807f | 149 | fprintf(fp, "used %d/%d ", ci->ndm_used/hz, |
0849e60a | 150 | ci->ndm_updated/hz); |
d04bc300 | 151 | } |
0849e60a SH |
152 | if (r->ndm_flags & NTF_SELF) |
153 | fprintf(fp, "self "); | |
cc273a51 RP |
154 | if (tb[NDA_MASTER]) |
155 | fprintf(fp, "master %s ", | |
156 | ll_index_to_name(rta_getattr_u32(tb[NDA_MASTER]))); | |
157 | else if (r->ndm_flags & NTF_MASTER) | |
0849e60a | 158 | fprintf(fp, "master "); |
5b8a1d4a DS |
159 | if (r->ndm_flags & NTF_ROUTER) |
160 | fprintf(fp, "router "); | |
28467b7f | 161 | if (r->ndm_flags & NTF_EXT_LEARNED) |
aa05b988 | 162 | fprintf(fp, "offload "); |
d04bc300 | 163 | |
0849e60a | 164 | fprintf(fp, "%s\n", state_n2a(r->ndm_state)); |
4d45bf3b WK |
165 | fflush(fp); |
166 | ||
d04bc300 SH |
167 | return 0; |
168 | } | |
169 | ||
170 | static int fdb_show(int argc, char **argv) | |
171 | { | |
aeb14eb0 | 172 | struct { |
df4b043f | 173 | struct nlmsghdr n; |
aeb14eb0 | 174 | struct ifinfomsg ifm; |
df4b043f | 175 | char buf[256]; |
aeb14eb0 JHS |
176 | } req; |
177 | ||
d04bc300 | 178 | char *filter_dev = NULL; |
aeb14eb0 JHS |
179 | char *br = NULL; |
180 | int msg_size = sizeof(struct ifinfomsg); | |
181 | ||
182 | memset(&req, 0, sizeof(req)); | |
183 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
184 | req.ifm.ifi_family = PF_BRIDGE; | |
0849e60a | 185 | |
d04bc300 | 186 | while (argc > 0) { |
aeb14eb0 | 187 | if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) { |
d04bc300 | 188 | NEXT_ARG(); |
d04bc300 | 189 | filter_dev = *argv; |
aeb14eb0 JHS |
190 | } else if (strcmp(*argv, "br") == 0) { |
191 | NEXT_ARG(); | |
192 | br = *argv; | |
193 | } else { | |
194 | if (matches(*argv, "help") == 0) | |
195 | usage(); | |
d04bc300 SH |
196 | } |
197 | argc--; argv++; | |
198 | } | |
199 | ||
aeb14eb0 JHS |
200 | if (br) { |
201 | int br_ifindex = ll_name_to_index(br); | |
df4b043f | 202 | |
aeb14eb0 JHS |
203 | if (br_ifindex == 0) { |
204 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", br); | |
205 | return -1; | |
206 | } | |
207 | addattr32(&req.n, sizeof(req), IFLA_MASTER, br_ifindex); | |
208 | msg_size += RTA_LENGTH(4); | |
209 | } | |
210 | ||
211 | /*we'll keep around filter_dev for older kernels */ | |
d04bc300 | 212 | if (filter_dev) { |
0849e60a SH |
213 | filter_index = if_nametoindex(filter_dev); |
214 | if (filter_index == 0) { | |
215 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
216 | filter_dev); | |
d04bc300 SH |
217 | return -1; |
218 | } | |
aeb14eb0 | 219 | req.ifm.ifi_index = filter_index; |
d04bc300 SH |
220 | } |
221 | ||
aeb14eb0 | 222 | if (rtnl_dump_request(&rth, RTM_GETNEIGH, &req.ifm, msg_size) < 0) { |
d04bc300 SH |
223 | perror("Cannot send dump request"); |
224 | exit(1); | |
225 | } | |
d04bc300 | 226 | |
0849e60a | 227 | if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) { |
d04bc300 SH |
228 | fprintf(stderr, "Dump terminated\n"); |
229 | exit(1); | |
230 | } | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | static int fdb_modify(int cmd, int flags, int argc, char **argv) | |
236 | { | |
237 | struct { | |
df4b043f SH |
238 | struct nlmsghdr n; |
239 | struct ndmsg ndm; | |
240 | char buf[256]; | |
d04bc300 SH |
241 | } req; |
242 | char *addr = NULL; | |
243 | char *d = NULL; | |
244 | char abuf[ETH_ALEN]; | |
0849e60a SH |
245 | int dst_ok = 0; |
246 | inet_prefix dst; | |
5f409678 DS |
247 | unsigned long port = 0; |
248 | unsigned long vni = ~0; | |
249 | unsigned int via = 0; | |
250 | char *endptr; | |
fd08839c | 251 | short vid = -1; |
d04bc300 SH |
252 | |
253 | memset(&req, 0, sizeof(req)); | |
254 | ||
255 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
256 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
257 | req.n.nlmsg_type = cmd; | |
258 | req.ndm.ndm_family = PF_BRIDGE; | |
259 | req.ndm.ndm_state = NUD_NOARP; | |
260 | ||
261 | while (argc > 0) { | |
262 | if (strcmp(*argv, "dev") == 0) { | |
263 | NEXT_ARG(); | |
264 | d = *argv; | |
0849e60a SH |
265 | } else if (strcmp(*argv, "dst") == 0) { |
266 | NEXT_ARG(); | |
267 | if (dst_ok) | |
268 | duparg2("dst", *argv); | |
269 | get_addr(&dst, *argv, preferred_family); | |
270 | dst_ok = 1; | |
5f409678 DS |
271 | } else if (strcmp(*argv, "port") == 0) { |
272 | ||
273 | NEXT_ARG(); | |
274 | port = strtoul(*argv, &endptr, 0); | |
275 | if (endptr && *endptr) { | |
276 | struct servent *pse; | |
277 | ||
278 | pse = getservbyname(*argv, "udp"); | |
279 | if (!pse) | |
280 | invarg("invalid port\n", *argv); | |
281 | port = ntohs(pse->s_port); | |
282 | } else if (port > 0xffff) | |
283 | invarg("invalid port\n", *argv); | |
284 | } else if (strcmp(*argv, "vni") == 0) { | |
285 | NEXT_ARG(); | |
286 | vni = strtoul(*argv, &endptr, 0); | |
287 | if ((endptr && *endptr) || | |
288 | (vni >> 24) || vni == ULONG_MAX) | |
289 | invarg("invalid VNI\n", *argv); | |
290 | } else if (strcmp(*argv, "via") == 0) { | |
291 | NEXT_ARG(); | |
292 | via = if_nametoindex(*argv); | |
293 | if (via == 0) | |
294 | invarg("invalid device\n", *argv); | |
dc6a6a25 JF |
295 | } else if (strcmp(*argv, "self") == 0) { |
296 | req.ndm.ndm_flags |= NTF_SELF; | |
0849e60a | 297 | } else if (matches(*argv, "master") == 0) { |
dc6a6a25 | 298 | req.ndm.ndm_flags |= NTF_MASTER; |
5b8a1d4a DS |
299 | } else if (matches(*argv, "router") == 0) { |
300 | req.ndm.ndm_flags |= NTF_ROUTER; | |
df4b043f | 301 | } else if (matches(*argv, "local") == 0 || |
0849e60a SH |
302 | matches(*argv, "permanent") == 0) { |
303 | req.ndm.ndm_state |= NUD_PERMANENT; | |
a1987cd1 RP |
304 | } else if (matches(*argv, "temp") == 0 || |
305 | matches(*argv, "static") == 0) { | |
0849e60a | 306 | req.ndm.ndm_state |= NUD_REACHABLE; |
c6d0cfb5 RP |
307 | } else if (matches(*argv, "dynamic") == 0) { |
308 | req.ndm.ndm_state |= NUD_REACHABLE; | |
309 | req.ndm.ndm_state &= ~NUD_NOARP; | |
fd08839c VY |
310 | } else if (matches(*argv, "vlan") == 0) { |
311 | if (vid >= 0) | |
312 | duparg2("vlan", *argv); | |
313 | NEXT_ARG(); | |
314 | vid = atoi(*argv); | |
cd8df30b RP |
315 | } else if (matches(*argv, "use") == 0) { |
316 | req.ndm.ndm_flags |= NTF_USE; | |
d04bc300 SH |
317 | } else { |
318 | if (strcmp(*argv, "to") == 0) { | |
319 | NEXT_ARG(); | |
320 | } | |
0849e60a SH |
321 | if (matches(*argv, "help") == 0) |
322 | usage(); | |
d04bc300 SH |
323 | if (addr) |
324 | duparg2("to", *argv); | |
325 | addr = *argv; | |
326 | } | |
327 | argc--; argv++; | |
328 | } | |
329 | ||
330 | if (d == NULL || addr == NULL) { | |
331 | fprintf(stderr, "Device and address are required arguments.\n"); | |
42ecedd4 | 332 | return -1; |
d04bc300 SH |
333 | } |
334 | ||
0849e60a SH |
335 | /* Assume self */ |
336 | if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER))) | |
337 | req.ndm.ndm_flags |= NTF_SELF; | |
338 | ||
339 | /* Assume permanent */ | |
340 | if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE))) | |
341 | req.ndm.ndm_state |= NUD_PERMANENT; | |
342 | ||
343 | if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
d04bc300 SH |
344 | abuf, abuf+1, abuf+2, |
345 | abuf+3, abuf+4, abuf+5) != 6) { | |
346 | fprintf(stderr, "Invalid mac address %s\n", addr); | |
42ecedd4 | 347 | return -1; |
d04bc300 SH |
348 | } |
349 | ||
350 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN); | |
0849e60a SH |
351 | if (dst_ok) |
352 | addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen); | |
d04bc300 | 353 | |
fd08839c | 354 | if (vid >= 0) |
8ae66094 | 355 | addattr16(&req.n, sizeof(req), NDA_VLAN, vid); |
fd08839c | 356 | |
5f409678 DS |
357 | if (port) { |
358 | unsigned short dport; | |
359 | ||
360 | dport = htons((unsigned short)port); | |
361 | addattr16(&req.n, sizeof(req), NDA_PORT, dport); | |
362 | } | |
363 | if (vni != ~0) | |
364 | addattr32(&req.n, sizeof(req), NDA_VNI, vni); | |
365 | if (via) | |
366 | addattr32(&req.n, sizeof(req), NDA_IFINDEX, via); | |
367 | ||
d04bc300 SH |
368 | req.ndm.ndm_ifindex = ll_name_to_index(d); |
369 | if (req.ndm.ndm_ifindex == 0) { | |
370 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
371 | return -1; | |
372 | } | |
373 | ||
c079e121 | 374 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
42ecedd4 | 375 | return -1; |
d04bc300 SH |
376 | |
377 | return 0; | |
378 | } | |
379 | ||
380 | int do_fdb(int argc, char **argv) | |
381 | { | |
382 | ll_init_map(&rth); | |
383 | ||
384 | if (argc > 0) { | |
385 | if (matches(*argv, "add") == 0) | |
386 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
5f409678 DS |
387 | if (matches(*argv, "append") == 0) |
388 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_APPEND, argc-1, argv+1); | |
5464049b TR |
389 | if (matches(*argv, "replace") == 0) |
390 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
d04bc300 SH |
391 | if (matches(*argv, "delete") == 0) |
392 | return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
393 | if (matches(*argv, "show") == 0 || | |
394 | matches(*argv, "lst") == 0 || | |
395 | matches(*argv, "list") == 0) | |
396 | return fdb_show(argc-1, argv+1); | |
397 | if (matches(*argv, "help") == 0) | |
398 | usage(); | |
399 | } else | |
400 | return fdb_show(0, NULL); | |
401 | ||
083b46bb | 402 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv); |
d04bc300 SH |
403 | exit(-1); |
404 | } |