]>
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> | |
12 | #include <time.h> | |
13 | #include <fcntl.h> | |
14 | #include <sys/socket.h> | |
15 | #include <sys/time.h> | |
16 | #include <net/if.h> | |
17 | #include <netinet/in.h> | |
18 | #include <linux/if_bridge.h> | |
19 | #include <linux/if_ether.h> | |
20 | #include <linux/neighbour.h> | |
21 | #include <string.h> | |
22 | ||
23 | #include "libnetlink.h" | |
24 | #include "br_common.h" | |
0849e60a | 25 | #include "rt_names.h" |
d04bc300 SH |
26 | #include "utils.h" |
27 | ||
28 | int filter_index; | |
29 | ||
30 | static void usage(void) | |
31 | { | |
8ae66094 SH |
32 | fprintf(stderr, "Usage: bridge fdb { add | del } ADDR dev DEV {self|master} [ temp ]\n" |
33 | " [ dst IPADDR] [ vlan VID ]\n"); | |
90698170 | 34 | fprintf(stderr, " bridge fdb {show} [ dev DEV ]\n"); |
d04bc300 SH |
35 | exit(-1); |
36 | } | |
37 | ||
38 | static const char *state_n2a(unsigned s) | |
39 | { | |
40 | static char buf[32]; | |
41 | ||
0849e60a SH |
42 | if (s & NUD_PERMANENT) |
43 | return "permanent"; | |
d04bc300 SH |
44 | |
45 | if (s & NUD_NOARP) | |
46 | return "static"; | |
47 | ||
48 | if (s & NUD_STALE) | |
49 | return "stale"; | |
0849e60a | 50 | |
d04bc300 SH |
51 | if (s & NUD_REACHABLE) |
52 | return ""; | |
53 | ||
54 | sprintf(buf, "state=%#x", s); | |
55 | return buf; | |
56 | } | |
57 | ||
d04bc300 SH |
58 | int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
59 | { | |
0849e60a | 60 | FILE *fp = arg; |
d04bc300 SH |
61 | struct ndmsg *r = NLMSG_DATA(n); |
62 | int len = n->nlmsg_len; | |
63 | struct rtattr * tb[NDA_MAX+1]; | |
0849e60a SH |
64 | |
65 | if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) { | |
66 | fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", | |
67 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
68 | ||
69 | return 0; | |
70 | } | |
d04bc300 SH |
71 | |
72 | len -= NLMSG_LENGTH(sizeof(*r)); | |
73 | if (len < 0) { | |
74 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
75 | return -1; | |
76 | } | |
77 | ||
78 | if (r->ndm_family != AF_BRIDGE) | |
79 | return 0; | |
80 | ||
81 | if (filter_index && filter_index != r->ndm_ifindex) | |
82 | return 0; | |
83 | ||
84 | parse_rtattr(tb, NDA_MAX, NDA_RTA(r), | |
85 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); | |
86 | ||
87 | if (n->nlmsg_type == RTM_DELNEIGH) | |
0849e60a SH |
88 | fprintf(fp, "Deleted "); |
89 | ||
90 | if (tb[NDA_LLADDR]) { | |
91 | SPRINT_BUF(b1); | |
92 | fprintf(fp, "%s ", | |
93 | ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), | |
94 | RTA_PAYLOAD(tb[NDA_LLADDR]), | |
95 | ll_index_to_type(r->ndm_ifindex), | |
96 | b1, sizeof(b1))); | |
d04bc300 | 97 | } |
38df7ac9 | 98 | |
0849e60a SH |
99 | if (!filter_index && r->ndm_ifindex) |
100 | fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex)); | |
101 | ||
102 | if (tb[NDA_DST]) { | |
103 | SPRINT_BUF(abuf); | |
104 | fprintf(fp, "dst %s ", | |
105 | format_host(AF_INET, | |
106 | RTA_PAYLOAD(tb[NDA_DST]), | |
107 | RTA_DATA(tb[NDA_DST]), | |
108 | abuf, sizeof(abuf))); | |
109 | } | |
38df7ac9 | 110 | |
fd08839c VY |
111 | if (tb[NDA_VLAN]) { |
112 | __u16 vid = rta_getattr_u16(tb[NDA_VLAN]); | |
113 | fprintf(fp, "vlan %hu ", vid); | |
114 | } | |
115 | ||
d04bc300 SH |
116 | if (show_stats && tb[NDA_CACHEINFO]) { |
117 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); | |
0849e60a | 118 | int hz = get_user_hz(); |
d04bc300 | 119 | |
0849e60a SH |
120 | fprintf(fp, " used %d/%d", ci->ndm_used/hz, |
121 | ci->ndm_updated/hz); | |
d04bc300 | 122 | } |
0849e60a SH |
123 | if (r->ndm_flags & NTF_SELF) |
124 | fprintf(fp, "self "); | |
125 | if (r->ndm_flags & NTF_MASTER) | |
126 | fprintf(fp, "master "); | |
d04bc300 | 127 | |
0849e60a | 128 | fprintf(fp, "%s\n", state_n2a(r->ndm_state)); |
d04bc300 SH |
129 | return 0; |
130 | } | |
131 | ||
132 | static int fdb_show(int argc, char **argv) | |
133 | { | |
134 | char *filter_dev = NULL; | |
0849e60a | 135 | |
d04bc300 SH |
136 | while (argc > 0) { |
137 | if (strcmp(*argv, "dev") == 0) { | |
138 | NEXT_ARG(); | |
139 | if (filter_dev) | |
140 | duparg("dev", *argv); | |
141 | filter_dev = *argv; | |
142 | } | |
143 | argc--; argv++; | |
144 | } | |
145 | ||
146 | if (filter_dev) { | |
0849e60a SH |
147 | filter_index = if_nametoindex(filter_dev); |
148 | if (filter_index == 0) { | |
149 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
150 | filter_dev); | |
d04bc300 SH |
151 | return -1; |
152 | } | |
153 | } | |
154 | ||
155 | if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETNEIGH) < 0) { | |
156 | perror("Cannot send dump request"); | |
157 | exit(1); | |
158 | } | |
d04bc300 | 159 | |
0849e60a | 160 | if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) { |
d04bc300 SH |
161 | fprintf(stderr, "Dump terminated\n"); |
162 | exit(1); | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | static int fdb_modify(int cmd, int flags, int argc, char **argv) | |
169 | { | |
170 | struct { | |
171 | struct nlmsghdr n; | |
172 | struct ndmsg ndm; | |
173 | char buf[256]; | |
174 | } req; | |
175 | char *addr = NULL; | |
176 | char *d = NULL; | |
177 | char abuf[ETH_ALEN]; | |
0849e60a SH |
178 | int dst_ok = 0; |
179 | inet_prefix dst; | |
fd08839c | 180 | short vid = -1; |
d04bc300 SH |
181 | |
182 | memset(&req, 0, sizeof(req)); | |
183 | ||
184 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
185 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
186 | req.n.nlmsg_type = cmd; | |
187 | req.ndm.ndm_family = PF_BRIDGE; | |
188 | req.ndm.ndm_state = NUD_NOARP; | |
189 | ||
190 | while (argc > 0) { | |
191 | if (strcmp(*argv, "dev") == 0) { | |
192 | NEXT_ARG(); | |
193 | d = *argv; | |
0849e60a SH |
194 | } else if (strcmp(*argv, "dst") == 0) { |
195 | NEXT_ARG(); | |
196 | if (dst_ok) | |
197 | duparg2("dst", *argv); | |
198 | get_addr(&dst, *argv, preferred_family); | |
199 | dst_ok = 1; | |
dc6a6a25 JF |
200 | } else if (strcmp(*argv, "self") == 0) { |
201 | req.ndm.ndm_flags |= NTF_SELF; | |
0849e60a | 202 | } else if (matches(*argv, "master") == 0) { |
dc6a6a25 | 203 | req.ndm.ndm_flags |= NTF_MASTER; |
38df7ac9 | 204 | } else if (matches(*argv, "local") == 0|| |
0849e60a SH |
205 | matches(*argv, "permanent") == 0) { |
206 | req.ndm.ndm_state |= NUD_PERMANENT; | |
207 | } else if (matches(*argv, "temp") == 0) { | |
208 | req.ndm.ndm_state |= NUD_REACHABLE; | |
fd08839c VY |
209 | } else if (matches(*argv, "vlan") == 0) { |
210 | if (vid >= 0) | |
211 | duparg2("vlan", *argv); | |
212 | NEXT_ARG(); | |
213 | vid = atoi(*argv); | |
d04bc300 SH |
214 | } else { |
215 | if (strcmp(*argv, "to") == 0) { | |
216 | NEXT_ARG(); | |
217 | } | |
0849e60a SH |
218 | if (matches(*argv, "help") == 0) |
219 | usage(); | |
d04bc300 SH |
220 | if (addr) |
221 | duparg2("to", *argv); | |
222 | addr = *argv; | |
223 | } | |
224 | argc--; argv++; | |
225 | } | |
226 | ||
227 | if (d == NULL || addr == NULL) { | |
228 | fprintf(stderr, "Device and address are required arguments.\n"); | |
229 | exit(-1); | |
230 | } | |
231 | ||
0849e60a SH |
232 | /* Assume self */ |
233 | if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER))) | |
234 | req.ndm.ndm_flags |= NTF_SELF; | |
235 | ||
236 | /* Assume permanent */ | |
237 | if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE))) | |
238 | req.ndm.ndm_state |= NUD_PERMANENT; | |
239 | ||
240 | if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
d04bc300 SH |
241 | abuf, abuf+1, abuf+2, |
242 | abuf+3, abuf+4, abuf+5) != 6) { | |
243 | fprintf(stderr, "Invalid mac address %s\n", addr); | |
244 | exit(-1); | |
245 | } | |
246 | ||
247 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN); | |
0849e60a SH |
248 | if (dst_ok) |
249 | addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen); | |
d04bc300 | 250 | |
fd08839c | 251 | if (vid >= 0) |
8ae66094 | 252 | addattr16(&req.n, sizeof(req), NDA_VLAN, vid); |
fd08839c | 253 | |
d04bc300 SH |
254 | req.ndm.ndm_ifindex = ll_name_to_index(d); |
255 | if (req.ndm.ndm_ifindex == 0) { | |
256 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
257 | return -1; | |
258 | } | |
259 | ||
260 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) | |
261 | exit(2); | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
266 | int do_fdb(int argc, char **argv) | |
267 | { | |
268 | ll_init_map(&rth); | |
269 | ||
270 | if (argc > 0) { | |
271 | if (matches(*argv, "add") == 0) | |
272 | return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
d04bc300 SH |
273 | if (matches(*argv, "delete") == 0) |
274 | return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
275 | if (matches(*argv, "show") == 0 || | |
276 | matches(*argv, "lst") == 0 || | |
277 | matches(*argv, "list") == 0) | |
278 | return fdb_show(argc-1, argv+1); | |
279 | if (matches(*argv, "help") == 0) | |
280 | usage(); | |
281 | } else | |
282 | return fdb_show(0, NULL); | |
283 | ||
083b46bb | 284 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv); |
d04bc300 SH |
285 | exit(-1); |
286 | } |