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