]> git.proxmox.com Git - mirror_iproute2.git/blame - bridge/link.c
ip link: use addattr_nest()/addattr_nest_end()
[mirror_iproute2.git] / bridge / link.c
CommitLineData
d04bc300
SH
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <time.h>
6#include <sys/socket.h>
7#include <sys/time.h>
8#include <netinet/in.h>
9#include <linux/if.h>
10#include <linux/if_bridge.h>
11#include <string.h>
64108901 12#include <stdbool.h>
d04bc300 13
64108901 14#include "libnetlink.h"
d04bc300
SH
15#include "utils.h"
16#include "br_common.h"
17
9dca899b 18static unsigned int filter_index;
64108901 19
d04bc300
SH
20static const char *port_states[] = {
21 [BR_STATE_DISABLED] = "disabled",
22 [BR_STATE_LISTENING] = "listening",
23 [BR_STATE_LEARNING] = "learning",
24 [BR_STATE_FORWARDING] = "forwarding",
25 [BR_STATE_BLOCKING] = "blocking",
26};
27
28extern char *if_indextoname (unsigned int __ifindex, char *__ifname);
29
30static void print_link_flags(FILE *fp, unsigned flags)
31{
32 fprintf(fp, "<");
33 if (flags & IFF_UP && !(flags & IFF_RUNNING))
34 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
35 flags &= ~IFF_RUNNING;
36#define _PF(f) if (flags&IFF_##f) { \
37 flags &= ~IFF_##f ; \
38 fprintf(fp, #f "%s", flags ? "," : ""); }
39 _PF(LOOPBACK);
40 _PF(BROADCAST);
41 _PF(POINTOPOINT);
42 _PF(MULTICAST);
43 _PF(NOARP);
44 _PF(ALLMULTI);
45 _PF(PROMISC);
46 _PF(MASTER);
47 _PF(SLAVE);
48 _PF(DEBUG);
49 _PF(DYNAMIC);
50 _PF(AUTOMEDIA);
51 _PF(PORTSEL);
52 _PF(NOTRAILERS);
53 _PF(UP);
54 _PF(LOWER_UP);
55 _PF(DORMANT);
56 _PF(ECHO);
57#undef _PF
58 if (flags)
59 fprintf(fp, "%x", flags);
60 fprintf(fp, "> ");
61}
62
63static const char *oper_states[] = {
38df7ac9 64 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
d04bc300
SH
65 "TESTING", "DORMANT", "UP"
66};
67
b1b7ce0f
VY
68static const char *hw_mode[] = {"VEB", "VEPA"};
69
d04bc300
SH
70static void print_operstate(FILE *f, __u8 state)
71{
72 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
73 fprintf(f, "state %#x ", state);
74 else
75 fprintf(f, "state %s ", oper_states[state]);
76}
77
b1b7ce0f
VY
78static void print_portstate(FILE *f, __u8 state)
79{
80 if (state <= BR_STATE_BLOCKING)
81 fprintf(f, "state %s ", port_states[state]);
82 else
83 fprintf(f, "state (%d) ", state);
84}
85
86static void print_onoff(FILE *f, char *flag, __u8 val)
87{
88 fprintf(f, "%s %s ", flag, val ? "on" : "off");
89}
90
91static void print_hwmode(FILE *f, __u16 mode)
92{
93 if (mode >= sizeof(hw_mode)/sizeof(hw_mode[0]))
94 fprintf(f, "hwmode %#hx ", mode);
95 else
96 fprintf(f, "hwmode %s ", hw_mode[mode]);
97}
98
d04bc300
SH
99int print_linkinfo(const struct sockaddr_nl *who,
100 struct nlmsghdr *n, void *arg)
101{
102 FILE *fp = arg;
103 int len = n->nlmsg_len;
104 struct ifinfomsg *ifi = NLMSG_DATA(n);
105 struct rtattr * tb[IFLA_MAX+1];
106 char b1[IFNAMSIZ];
107
108 len -= NLMSG_LENGTH(sizeof(*ifi));
109 if (len < 0) {
110 fprintf(stderr, "Message too short!\n");
111 return -1;
112 }
113
114 if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
115 return 0;
116
64108901
VY
117 if (filter_index && filter_index != ifi->ifi_index)
118 return 0;
119
b1b7ce0f 120 parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
d04bc300
SH
121
122 if (tb[IFLA_IFNAME] == NULL) {
123 fprintf(stderr, "BUG: nil ifname\n");
124 return -1;
125 }
126
127 if (n->nlmsg_type == RTM_DELLINK)
128 fprintf(fp, "Deleted ");
129
130 fprintf(fp, "%d: %s ", ifi->ifi_index,
1465db1a 131 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
d04bc300 132
38df7ac9 133 if (tb[IFLA_OPERSTATE])
1465db1a 134 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
38df7ac9 135
d04bc300
SH
136 if (tb[IFLA_LINK]) {
137 SPRINT_BUF(b1);
1465db1a 138 int iflink = rta_getattr_u32(tb[IFLA_LINK]);
d04bc300
SH
139 if (iflink == 0)
140 fprintf(fp, "@NONE: ");
1465db1a 141 else
38df7ac9 142 fprintf(fp, "@%s: ",
d04bc300 143 if_indextoname(iflink, b1));
1465db1a 144 } else
d04bc300 145 fprintf(fp, ": ");
d04bc300
SH
146
147 print_link_flags(fp, ifi->ifi_flags);
148
149 if (tb[IFLA_MTU])
1465db1a 150 fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU]));
d04bc300 151
1465db1a 152 if (tb[IFLA_MASTER])
38df7ac9 153 fprintf(fp, "master %s ",
1465db1a 154 if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1));
d04bc300
SH
155
156 if (tb[IFLA_PROTINFO]) {
b1b7ce0f
VY
157 if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) {
158 struct rtattr *prtb[IFLA_BRPORT_MAX+1];
159
160 parse_rtattr_nested(prtb, IFLA_BRPORT_MAX,
161 tb[IFLA_PROTINFO]);
162
163 if (prtb[IFLA_BRPORT_STATE])
164 print_portstate(fp,
165 rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
166 if (prtb[IFLA_BRPORT_PRIORITY])
167 fprintf(fp, "priority %hu ",
168 rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
169 if (prtb[IFLA_BRPORT_COST])
170 fprintf(fp, "cost %u ",
171 rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
4cd20da1
SH
172
173 if (show_details) {
174 fprintf(fp, "%s ", _SL_);
175
176 if (prtb[IFLA_BRPORT_MODE])
177 print_onoff(fp, "hairpin",
178 rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
179 if (prtb[IFLA_BRPORT_GUARD])
180 print_onoff(fp, "guard",
181 rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
182 if (prtb[IFLA_BRPORT_PROTECT])
183 print_onoff(fp, "root_block",
184 rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
185 if (prtb[IFLA_BRPORT_FAST_LEAVE])
186 print_onoff(fp, "fastleave",
187 rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
f0f4ab60
VY
188 if (prtb[IFLA_BRPORT_LEARNING])
189 print_onoff(fp, "learning",
190 rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING]));
191 if (prtb[IFLA_BRPORT_UNICAST_FLOOD])
192 print_onoff(fp, "flood",
193 rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD]));
4cd20da1 194 }
b1b7ce0f
VY
195 } else
196 print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO]));
d04bc300
SH
197 }
198
b1b7ce0f
VY
199 if (tb[IFLA_AF_SPEC]) {
200 /* This is reported by HW devices that have some bridging
201 * capabilities.
202 */
203 struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
204
205 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
206
a40d0827
JF
207 if (aftb[IFLA_BRIDGE_MODE])
208 print_hwmode(fp, rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
b1b7ce0f 209 }
d04bc300
SH
210
211 fprintf(fp, "\n");
212 fflush(fp);
213 return 0;
214}
64108901
VY
215
216static void usage(void)
217{
218 fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
219 fprintf(stderr, " [ guard {on | off} ]\n");
220 fprintf(stderr, " [ hairpin {on | off} ] \n");
221 fprintf(stderr, " [ fastleave {on | off} ]\n");
222 fprintf(stderr, " [ root_block {on | off} ]\n");
f0f4ab60
VY
223 fprintf(stderr, " [ learning {on | off} ]\n");
224 fprintf(stderr, " [ flood {on | off} ]\n");
64108901
VY
225 fprintf(stderr, " [ hwmode {vepa | veb} ]\n");
226 fprintf(stderr, " bridge link show [dev DEV]\n");
227 exit(-1);
228}
229
230static bool on_off(char *arg, __s8 *attr, char *val)
231{
232 if (strcmp(val, "on") == 0)
233 *attr = 1;
234 else if (strcmp(val, "off") == 0)
235 *attr = 0;
236 else {
237 fprintf(stderr,
238 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
239 arg);
240 return false;
241 }
242
243 return true;
244}
245
246static int brlink_modify(int argc, char **argv)
247{
248 struct {
249 struct nlmsghdr n;
250 struct ifinfomsg ifm;
251 char buf[512];
252 } req;
253 char *d = NULL;
f0f4ab60
VY
254 __s8 learning = -1;
255 __s8 flood = -1;
64108901
VY
256 __s8 hairpin = -1;
257 __s8 bpdu_guard = -1;
258 __s8 fast_leave = -1;
b1b7ce0f 259 __s8 root_block = -1;
64108901
VY
260 __u32 cost = 0;
261 __s16 priority = -1;
262 __s8 state = -1;
263 __s16 mode = -1;
264 __u16 flags = BRIDGE_FLAGS_MASTER;
265 struct rtattr *nest;
266
267 memset(&req, 0, sizeof(req));
268
269 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
270 req.n.nlmsg_flags = NLM_F_REQUEST;
271 req.n.nlmsg_type = RTM_SETLINK;
272 req.ifm.ifi_family = PF_BRIDGE;
273
274 while (argc > 0) {
275 if (strcmp(*argv, "dev") == 0) {
276 NEXT_ARG();
277 d = *argv;
278 } else if (strcmp(*argv, "guard") == 0) {
279 NEXT_ARG();
280 if (!on_off("guard", &bpdu_guard, *argv))
281 exit(-1);
282 } else if (strcmp(*argv, "hairpin") == 0) {
283 NEXT_ARG();
284 if (!on_off("hairping", &hairpin, *argv))
285 exit(-1);
286 } else if (strcmp(*argv, "fastleave") == 0) {
287 NEXT_ARG();
288 if (!on_off("fastleave", &fast_leave, *argv))
289 exit(-1);
b1b7ce0f
VY
290 } else if (strcmp(*argv, "root_block") == 0) {
291 NEXT_ARG();
292 if (!on_off("root_block", &root_block, *argv))
293 exit(-1);
f0f4ab60
VY
294 } else if (strcmp(*argv, "learning") == 0) {
295 NEXT_ARG();
296 if (!on_off("learning", &learning, *argv))
297 exit(-1);
298 } else if (strcmp(*argv, "flood") == 0) {
299 NEXT_ARG();
300 if (!on_off("flood", &flood, *argv))
301 exit(-1);
a40d0827 302 } else if (strcmp(*argv, "cost") == 0) {
64108901
VY
303 NEXT_ARG();
304 cost = atoi(*argv);
a40d0827 305 } else if (strcmp(*argv, "priority") == 0) {
64108901
VY
306 NEXT_ARG();
307 priority = atoi(*argv);
a40d0827 308 } else if (strcmp(*argv, "state") == 0) {
64108901
VY
309 NEXT_ARG();
310 state = atoi(*argv);
a40d0827 311 } else if (strcmp(*argv, "hwmode") == 0) {
64108901 312 NEXT_ARG();
a40d0827 313 flags = BRIDGE_FLAGS_SELF;
64108901
VY
314 if (strcmp(*argv, "vepa") == 0)
315 mode = BRIDGE_MODE_VEPA;
316 else if (strcmp(*argv, "veb") == 0)
317 mode = BRIDGE_MODE_VEB;
318 else {
319 fprintf(stderr,
320 "Mode argument must be \"vepa\" or "
321 "\"veb\".\n");
322 exit(-1);
323 }
324 } else {
325 usage();
326 }
327 argc--; argv++;
328 }
329 if (d == NULL) {
330 fprintf(stderr, "Device is a required argument.\n");
331 exit(-1);
332 }
333
334
335 req.ifm.ifi_index = ll_name_to_index(d);
336 if (req.ifm.ifi_index == 0) {
337 fprintf(stderr, "Cannot find bridge device \"%s\"\n", d);
338 exit(-1);
339 }
340
341 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
342 * state.
343 */
344 nest = addattr_nest(&req.n, sizeof(req),
345 IFLA_PROTINFO | NLA_F_NESTED);
346 /* Flags first */
347 if (bpdu_guard >= 0)
348 addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard);
349 if (hairpin >= 0)
350 addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin);
351 if (fast_leave >= 0)
352 addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE,
353 fast_leave);
b1b7ce0f
VY
354 if (root_block >= 0)
355 addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block);
f0f4ab60
VY
356 if (flood >= 0)
357 addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood);
358 if (learning >= 0)
359 addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning);
64108901
VY
360
361 if (cost > 0)
362 addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost);
363
364 if (priority >= 0)
365 addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority);
366
367 if (state >= 0)
368 addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state);
369
370 addattr_nest_end(&req.n, nest);
371
372 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
373 * designates master or self operation as well as 'vepa' or 'veb'
374 * operation modes. These are only valid in 'self' mode on some
375 * devices so far. Thus we only need to include the flags attribute
376 * if we are setting the hw mode.
377 */
378 if (mode >= 0) {
379 nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
380
381 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags);
382
383 if (mode >= 0)
384 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode);
385
386 addattr_nest_end(&req.n, nest);
387 }
388
389 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
390 exit(2);
391
392 return 0;
393}
394
395static int brlink_show(int argc, char **argv)
396{
397 char *filter_dev = NULL;
398
399 while (argc > 0) {
400 if (strcmp(*argv, "dev") == 0) {
401 NEXT_ARG();
402 if (filter_dev)
403 duparg("dev", *argv);
404 filter_dev = *argv;
405 }
406 argc--; argv++;
407 }
408
409 if (filter_dev) {
410 if ((filter_index = ll_name_to_index(filter_dev)) == 0) {
411 fprintf(stderr, "Cannot find device \"%s\"\n",
412 filter_dev);
413 return -1;
414 }
415 }
416
417 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) {
418 perror("Cannon send dump request");
419 exit(1);
420 }
421
422 if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
423 fprintf(stderr, "Dump terminated\n");
424 exit(1);
425 }
426 return 0;
427}
428
429int do_link(int argc, char **argv)
430{
431 ll_init_map(&rth);
432 if (argc > 0) {
433 if (matches(*argv, "set") == 0 ||
434 matches(*argv, "change") == 0)
435 return brlink_modify(argc-1, argv+1);
436 if (matches(*argv, "show") == 0 ||
437 matches(*argv, "lst") == 0 ||
438 matches(*argv, "list") == 0)
439 return brlink_show(argc-1, argv+1);
440 if (matches(*argv, "help") == 0)
441 usage();
442 } else
443 return brlink_show(0, NULL);
444
445 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv);
446 exit(-1);
447}