]> git.proxmox.com Git - mirror_iproute2.git/blame - bridge/fdb.c
bridge: Update bridge man pages to include vlan command
[mirror_iproute2.git] / bridge / fdb.c
CommitLineData
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
28int filter_index;
29
30static void usage(void)
31{
fd08839c 32 fprintf(stderr, "Usage: bridge fdb { add | del } ADDR dev DEV {self|master} [ temp ] [ dst IPADDR] [ vlan VID ]\n");
90698170 33 fprintf(stderr, " bridge fdb {show} [ dev DEV ]\n");
d04bc300
SH
34 exit(-1);
35}
36
37static 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
57int 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
fd08839c
VY
110 if (tb[NDA_VLAN]) {
111 __u16 vid = rta_getattr_u16(tb[NDA_VLAN]);
112 fprintf(fp, "vlan %hu ", vid);
113 }
114
d04bc300
SH
115 if (show_stats && tb[NDA_CACHEINFO]) {
116 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
0849e60a 117 int hz = get_user_hz();
d04bc300 118
0849e60a
SH
119 fprintf(fp, " used %d/%d", ci->ndm_used/hz,
120 ci->ndm_updated/hz);
d04bc300 121 }
0849e60a
SH
122 if (r->ndm_flags & NTF_SELF)
123 fprintf(fp, "self ");
124 if (r->ndm_flags & NTF_MASTER)
125 fprintf(fp, "master ");
d04bc300 126
0849e60a 127 fprintf(fp, "%s\n", state_n2a(r->ndm_state));
d04bc300
SH
128 return 0;
129}
130
131static int fdb_show(int argc, char **argv)
132{
133 char *filter_dev = NULL;
0849e60a 134
d04bc300
SH
135 while (argc > 0) {
136 if (strcmp(*argv, "dev") == 0) {
137 NEXT_ARG();
138 if (filter_dev)
139 duparg("dev", *argv);
140 filter_dev = *argv;
141 }
142 argc--; argv++;
143 }
144
145 if (filter_dev) {
0849e60a
SH
146 filter_index = if_nametoindex(filter_dev);
147 if (filter_index == 0) {
148 fprintf(stderr, "Cannot find device \"%s\"\n",
149 filter_dev);
d04bc300
SH
150 return -1;
151 }
152 }
153
154 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETNEIGH) < 0) {
155 perror("Cannot send dump request");
156 exit(1);
157 }
d04bc300 158
0849e60a 159 if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) {
d04bc300
SH
160 fprintf(stderr, "Dump terminated\n");
161 exit(1);
162 }
163
164 return 0;
165}
166
167static int fdb_modify(int cmd, int flags, int argc, char **argv)
168{
169 struct {
170 struct nlmsghdr n;
171 struct ndmsg ndm;
172 char buf[256];
173 } req;
174 char *addr = NULL;
175 char *d = NULL;
176 char abuf[ETH_ALEN];
0849e60a
SH
177 int dst_ok = 0;
178 inet_prefix dst;
fd08839c 179 short vid = -1;
d04bc300
SH
180
181 memset(&req, 0, sizeof(req));
182
183 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
184 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
185 req.n.nlmsg_type = cmd;
186 req.ndm.ndm_family = PF_BRIDGE;
187 req.ndm.ndm_state = NUD_NOARP;
188
189 while (argc > 0) {
190 if (strcmp(*argv, "dev") == 0) {
191 NEXT_ARG();
192 d = *argv;
0849e60a
SH
193 } else if (strcmp(*argv, "dst") == 0) {
194 NEXT_ARG();
195 if (dst_ok)
196 duparg2("dst", *argv);
197 get_addr(&dst, *argv, preferred_family);
198 dst_ok = 1;
dc6a6a25
JF
199 } else if (strcmp(*argv, "self") == 0) {
200 req.ndm.ndm_flags |= NTF_SELF;
0849e60a 201 } else if (matches(*argv, "master") == 0) {
dc6a6a25 202 req.ndm.ndm_flags |= NTF_MASTER;
38df7ac9 203 } else if (matches(*argv, "local") == 0||
0849e60a
SH
204 matches(*argv, "permanent") == 0) {
205 req.ndm.ndm_state |= NUD_PERMANENT;
206 } else if (matches(*argv, "temp") == 0) {
207 req.ndm.ndm_state |= NUD_REACHABLE;
fd08839c
VY
208 } else if (matches(*argv, "vlan") == 0) {
209 if (vid >= 0)
210 duparg2("vlan", *argv);
211 NEXT_ARG();
212 vid = atoi(*argv);
d04bc300
SH
213 } else {
214 if (strcmp(*argv, "to") == 0) {
215 NEXT_ARG();
216 }
0849e60a
SH
217 if (matches(*argv, "help") == 0)
218 usage();
d04bc300
SH
219 if (addr)
220 duparg2("to", *argv);
221 addr = *argv;
222 }
223 argc--; argv++;
224 }
225
226 if (d == NULL || addr == NULL) {
227 fprintf(stderr, "Device and address are required arguments.\n");
228 exit(-1);
229 }
230
0849e60a
SH
231 /* Assume self */
232 if (!(req.ndm.ndm_flags&(NTF_SELF|NTF_MASTER)))
233 req.ndm.ndm_flags |= NTF_SELF;
234
235 /* Assume permanent */
236 if (!(req.ndm.ndm_state&(NUD_PERMANENT|NUD_REACHABLE)))
237 req.ndm.ndm_state |= NUD_PERMANENT;
238
239 if (sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
d04bc300
SH
240 abuf, abuf+1, abuf+2,
241 abuf+3, abuf+4, abuf+5) != 6) {
242 fprintf(stderr, "Invalid mac address %s\n", addr);
243 exit(-1);
244 }
245
246 addattr_l(&req.n, sizeof(req), NDA_LLADDR, abuf, ETH_ALEN);
0849e60a
SH
247 if (dst_ok)
248 addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen);
d04bc300 249
fd08839c
VY
250 if (vid >= 0)
251 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
252
d04bc300
SH
253 req.ndm.ndm_ifindex = ll_name_to_index(d);
254 if (req.ndm.ndm_ifindex == 0) {
255 fprintf(stderr, "Cannot find device \"%s\"\n", d);
256 return -1;
257 }
258
259 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
260 exit(2);
261
262 return 0;
263}
264
265int do_fdb(int argc, char **argv)
266{
267 ll_init_map(&rth);
268
269 if (argc > 0) {
270 if (matches(*argv, "add") == 0)
271 return fdb_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
d04bc300
SH
272 if (matches(*argv, "delete") == 0)
273 return fdb_modify(RTM_DELNEIGH, 0, argc-1, argv+1);
274 if (matches(*argv, "show") == 0 ||
275 matches(*argv, "lst") == 0 ||
276 matches(*argv, "list") == 0)
277 return fdb_show(argc-1, argv+1);
278 if (matches(*argv, "help") == 0)
279 usage();
280 } else
281 return fdb_show(0, NULL);
282
083b46bb 283 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge fdb help\".\n", *argv);
d04bc300
SH
284 exit(-1);
285}