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