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