]>
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 | { | |
8ae66094 | 33 | fprintf(stderr, "Usage: bridge fdb { add | del } ADDR dev DEV {self|master} [ temp ]\n" |
5f409678 DS |
34 | " [ dst IPADDR] [ vlan VID ]\n" |
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 "); | |
d04bc300 | 146 | |
0849e60a | 147 | fprintf(fp, "%s\n", state_n2a(r->ndm_state)); |
d04bc300 SH |
148 | return 0; |
149 | } | |
150 | ||
151 | static int fdb_show(int argc, char **argv) | |
152 | { | |
153 | char *filter_dev = NULL; | |
0849e60a | 154 | |
d04bc300 SH |
155 | while (argc > 0) { |
156 | if (strcmp(*argv, "dev") == 0) { | |
157 | NEXT_ARG(); | |
158 | if (filter_dev) | |
159 | duparg("dev", *argv); | |
160 | filter_dev = *argv; | |
161 | } | |
162 | argc--; argv++; | |
163 | } | |
164 | ||
165 | if (filter_dev) { | |
0849e60a SH |
166 | filter_index = if_nametoindex(filter_dev); |
167 | if (filter_index == 0) { | |
168 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
169 | filter_dev); | |
d04bc300 SH |
170 | return -1; |
171 | } | |
172 | } | |
173 | ||
174 | if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETNEIGH) < 0) { | |
175 | perror("Cannot send dump request"); | |
176 | exit(1); | |
177 | } | |
d04bc300 | 178 | |
0849e60a | 179 | if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) { |
d04bc300 SH |
180 | fprintf(stderr, "Dump terminated\n"); |
181 | exit(1); | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | static int fdb_modify(int cmd, int flags, int argc, char **argv) | |
188 | { | |
189 | struct { | |
190 | struct nlmsghdr n; | |
191 | struct ndmsg ndm; | |
192 | char buf[256]; | |
193 | } req; | |
194 | char *addr = NULL; | |
195 | char *d = NULL; | |
196 | char abuf[ETH_ALEN]; | |
0849e60a SH |
197 | int dst_ok = 0; |
198 | inet_prefix dst; | |
5f409678 DS |
199 | unsigned long port = 0; |
200 | unsigned long vni = ~0; | |
201 | unsigned int via = 0; | |
202 | char *endptr; | |
fd08839c | 203 | short vid = -1; |
d04bc300 SH |
204 | |
205 | memset(&req, 0, sizeof(req)); | |
206 | ||
207 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
208 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
209 | req.n.nlmsg_type = cmd; | |
210 | req.ndm.ndm_family = PF_BRIDGE; | |
211 | req.ndm.ndm_state = NUD_NOARP; | |
212 | ||
213 | while (argc > 0) { | |
214 | if (strcmp(*argv, "dev") == 0) { | |
215 | NEXT_ARG(); | |
216 | d = *argv; | |
0849e60a SH |
217 | } else if (strcmp(*argv, "dst") == 0) { |
218 | NEXT_ARG(); | |
219 | if (dst_ok) | |
220 | duparg2("dst", *argv); | |
221 | get_addr(&dst, *argv, preferred_family); | |
222 | dst_ok = 1; | |
5f409678 DS |
223 | } else if (strcmp(*argv, "port") == 0) { |
224 | ||
225 | NEXT_ARG(); | |
226 | port = strtoul(*argv, &endptr, 0); | |
227 | if (endptr && *endptr) { | |
228 | struct servent *pse; | |
229 | ||
230 | pse = getservbyname(*argv, "udp"); | |
231 | if (!pse) | |
232 | invarg("invalid port\n", *argv); | |
233 | port = ntohs(pse->s_port); | |
234 | } else if (port > 0xffff) | |
235 | invarg("invalid port\n", *argv); | |
236 | } else if (strcmp(*argv, "vni") == 0) { | |
237 | NEXT_ARG(); | |
238 | vni = strtoul(*argv, &endptr, 0); | |
239 | if ((endptr && *endptr) || | |
240 | (vni >> 24) || vni == ULONG_MAX) | |
241 | invarg("invalid VNI\n", *argv); | |
242 | } else if (strcmp(*argv, "via") == 0) { | |
243 | NEXT_ARG(); | |
244 | via = if_nametoindex(*argv); | |
245 | if (via == 0) | |
246 | invarg("invalid device\n", *argv); | |
dc6a6a25 JF |
247 | } else if (strcmp(*argv, "self") == 0) { |
248 | req.ndm.ndm_flags |= NTF_SELF; | |
0849e60a | 249 | } else if (matches(*argv, "master") == 0) { |
dc6a6a25 | 250 | req.ndm.ndm_flags |= NTF_MASTER; |
38df7ac9 | 251 | } else if (matches(*argv, "local") == 0|| |
0849e60a SH |
252 | matches(*argv, "permanent") == 0) { |
253 | req.ndm.ndm_state |= NUD_PERMANENT; | |
254 | } else if (matches(*argv, "temp") == 0) { | |
255 | req.ndm.ndm_state |= NUD_REACHABLE; | |
fd08839c VY |
256 | } else if (matches(*argv, "vlan") == 0) { |
257 | if (vid >= 0) | |
258 | duparg2("vlan", *argv); | |
259 | NEXT_ARG(); | |
260 | vid = atoi(*argv); | |
d04bc300 SH |
261 | } else { |
262 | if (strcmp(*argv, "to") == 0) { | |
263 | NEXT_ARG(); | |
264 | } | |
0849e60a SH |
265 | if (matches(*argv, "help") == 0) |
266 | usage(); | |
d04bc300 SH |
267 | if (addr) |
268 | duparg2("to", *argv); | |
269 | addr = *argv; | |
270 | } | |
271 | argc--; argv++; | |
272 | } | |
273 | ||
274 | if (d == NULL || addr == NULL) { | |
275 | fprintf(stderr, "Device and address are required arguments.\n"); | |
276 | exit(-1); | |
277 | } | |
278 | ||
0849e60a SH |
279 | /* Assume self */ |
280 | if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER))) | |
281 | req.ndm.ndm_flags |= NTF_SELF; | |
282 | ||
283 | /* Assume permanent */ | |
284 | if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE))) | |
285 | req.ndm.ndm_state |= NUD_PERMANENT; | |
286 | ||
287 | if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
d04bc300 SH |
288 | abuf, abuf+1, abuf+2, |
289 | abuf+3, abuf+4, abuf+5) != 6) { | |
290 | fprintf(stderr, "Invalid mac address %s\n", addr); | |
291 | exit(-1); | |
292 | } | |
293 | ||
294 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN); | |
0849e60a SH |
295 | if (dst_ok) |
296 | addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen); | |
d04bc300 | 297 | |
fd08839c | 298 | if (vid >= 0) |
8ae66094 | 299 | addattr16(&req.n, sizeof(req), NDA_VLAN, vid); |
fd08839c | 300 | |
5f409678 DS |
301 | if (port) { |
302 | unsigned short dport; | |
303 | ||
304 | dport = htons((unsigned short)port); | |
305 | addattr16(&req.n, sizeof(req), NDA_PORT, dport); | |
306 | } | |
307 | if (vni != ~0) | |
308 | addattr32(&req.n, sizeof(req), NDA_VNI, vni); | |
309 | if (via) | |
310 | addattr32(&req.n, sizeof(req), NDA_IFINDEX, via); | |
311 | ||
d04bc300 SH |
312 | req.ndm.ndm_ifindex = ll_name_to_index(d); |
313 | if (req.ndm.ndm_ifindex == 0) { | |
314 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
315 | return -1; | |
316 | } | |
317 | ||
318 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) | |
319 | exit(2); | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | int do_fdb(int argc, char **argv) | |
325 | { | |
326 | ll_init_map(&rth); | |
327 | ||
328 | if (argc > 0) { | |
329 | if (matches(*argv, "add") == 0) | |
330 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
5f409678 DS |
331 | if (matches(*argv, "append") == 0) |
332 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_APPEND, argc-1, argv+1); | |
d04bc300 SH |
333 | if (matches(*argv, "delete") == 0) |
334 | return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
335 | if (matches(*argv, "show") == 0 || | |
336 | matches(*argv, "lst") == 0 || | |
337 | matches(*argv, "list") == 0) | |
338 | return fdb_show(argc-1, argv+1); | |
339 | if (matches(*argv, "help") == 0) | |
340 | usage(); | |
341 | } else | |
342 | return fdb_show(0, NULL); | |
343 | ||
083b46bb | 344 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv); |
d04bc300 SH |
345 | exit(-1); |
346 | } |