blob: b347040ccf91de08473c2418162e6d778d06c8a9 [file] [log] [blame]
Stephen Hemmingerd04bc302012-08-01 15:23:49 -07001
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>
Vlad Yasevich64108902013-03-15 10:01:28 -070012#include <stdbool.h>
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070013
Vlad Yasevich64108902013-03-15 10:01:28 -070014#include "libnetlink.h"
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070015#include "utils.h"
16#include "br_common.h"
17
Andreas Henriksson9dca8992014-06-04 19:40:37 +020018static unsigned int filter_index;
Vlad Yasevich64108902013-03-15 10:01:28 -070019
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070020static 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
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070028extern char *if_indextoname(unsigned int __ifindex, char *__ifname);
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070029
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070030static void print_link_flags(FILE *fp, unsigned int flags)
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070031{
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) { \
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070037 flags &= ~IFF_##f ; \
38 fprintf(fp, #f "%s", flags ? "," : ""); }
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070039 _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
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070058 if (flags)
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070059 fprintf(fp, "%x", flags);
60 fprintf(fp, "> ");
61}
62
63static const char *oper_states[] = {
Stephen Hemminger38df7ac2012-10-29 17:48:55 -070064 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070065 "TESTING", "DORMANT", "UP"
66};
67
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -070068static const char *hw_mode[] = {"VEB", "VEPA"};
69
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070070static void print_operstate(FILE *f, __u8 state)
71{
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070072 if (state >= ARRAY_SIZE(oper_states))
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070073 fprintf(f, "state %#x ", state);
74 else
75 fprintf(f, "state %s ", oper_states[state]);
76}
77
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -070078static 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{
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -070093 if (mode >= ARRAY_SIZE(hw_mode))
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -070094 fprintf(f, "hwmode %#hx ", mode);
95 else
96 fprintf(f, "hwmode %s ", hw_mode[mode]);
97}
98
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070099int 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);
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700105 struct rtattr *tb[IFLA_MAX+1];
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700106 char b1[IFNAMSIZ];
107
108 len -= NLMSG_LENGTH(sizeof(*ifi));
109 if (len < 0) {
110 fprintf(stderr, "Message too short!\n");
111 return -1;
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700112 }
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700113
114 if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
115 return 0;
116
Vlad Yasevich64108902013-03-15 10:01:28 -0700117 if (filter_index && filter_index != ifi->ifi_index)
118 return 0;
119
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700120 parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700121
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,
Stephen Hemminger1465db12012-10-29 17:52:45 -0700131 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700132
Stephen Hemminger38df7ac2012-10-29 17:48:55 -0700133 if (tb[IFLA_OPERSTATE])
Stephen Hemminger1465db12012-10-29 17:52:45 -0700134 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
Stephen Hemminger38df7ac2012-10-29 17:48:55 -0700135
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700136 if (tb[IFLA_LINK]) {
137 SPRINT_BUF(b1);
Stephen Hemminger1465db12012-10-29 17:52:45 -0700138 int iflink = rta_getattr_u32(tb[IFLA_LINK]);
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700139
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700140 if (iflink == 0)
141 fprintf(fp, "@NONE: ");
Stephen Hemminger1465db12012-10-29 17:52:45 -0700142 else
Stephen Hemminger38df7ac2012-10-29 17:48:55 -0700143 fprintf(fp, "@%s: ",
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700144 if_indextoname(iflink, b1));
Stephen Hemminger1465db12012-10-29 17:52:45 -0700145 } else
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700146 fprintf(fp, ": ");
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700147
148 print_link_flags(fp, ifi->ifi_flags);
149
150 if (tb[IFLA_MTU])
Stephen Hemminger1465db12012-10-29 17:52:45 -0700151 fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU]));
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700152
Stephen Hemminger1465db12012-10-29 17:52:45 -0700153 if (tb[IFLA_MASTER])
Stephen Hemminger38df7ac2012-10-29 17:48:55 -0700154 fprintf(fp, "master %s ",
Stephen Hemminger1465db12012-10-29 17:52:45 -0700155 if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1));
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700156
157 if (tb[IFLA_PROTINFO]) {
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700158 if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) {
159 struct rtattr *prtb[IFLA_BRPORT_MAX+1];
160
161 parse_rtattr_nested(prtb, IFLA_BRPORT_MAX,
162 tb[IFLA_PROTINFO]);
163
164 if (prtb[IFLA_BRPORT_STATE])
165 print_portstate(fp,
166 rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
167 if (prtb[IFLA_BRPORT_PRIORITY])
168 fprintf(fp, "priority %hu ",
169 rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
170 if (prtb[IFLA_BRPORT_COST])
171 fprintf(fp, "cost %u ",
172 rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
Stephen Hemminger4cd20da2013-03-16 10:18:50 -0700173
174 if (show_details) {
175 fprintf(fp, "%s ", _SL_);
176
177 if (prtb[IFLA_BRPORT_MODE])
178 print_onoff(fp, "hairpin",
179 rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
180 if (prtb[IFLA_BRPORT_GUARD])
181 print_onoff(fp, "guard",
182 rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
183 if (prtb[IFLA_BRPORT_PROTECT])
184 print_onoff(fp, "root_block",
185 rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
186 if (prtb[IFLA_BRPORT_FAST_LEAVE])
187 print_onoff(fp, "fastleave",
188 rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400189 if (prtb[IFLA_BRPORT_LEARNING])
190 print_onoff(fp, "learning",
191 rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING]));
Scott Feldman674bb432014-12-29 12:20:07 -0800192 if (prtb[IFLA_BRPORT_LEARNING_SYNC])
193 print_onoff(fp, "learning_sync",
194 rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC]));
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400195 if (prtb[IFLA_BRPORT_UNICAST_FLOOD])
196 print_onoff(fp, "flood",
197 rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD]));
Stephen Hemminger4cd20da2013-03-16 10:18:50 -0700198 }
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700199 } else
200 print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO]));
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700201 }
202
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700203 if (tb[IFLA_AF_SPEC]) {
204 /* This is reported by HW devices that have some bridging
205 * capabilities.
206 */
207 struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
208
209 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
210
John Fastabenda40d0822013-05-29 06:20:53 +0000211 if (aftb[IFLA_BRIDGE_MODE])
212 print_hwmode(fp, rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700213 }
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700214
215 fprintf(fp, "\n");
216 fflush(fp);
217 return 0;
218}
Vlad Yasevich64108902013-03-15 10:01:28 -0700219
220static void usage(void)
221{
222 fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
223 fprintf(stderr, " [ guard {on | off} ]\n");
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700224 fprintf(stderr, " [ hairpin {on | off} ]\n");
Vlad Yasevich64108902013-03-15 10:01:28 -0700225 fprintf(stderr, " [ fastleave {on | off} ]\n");
226 fprintf(stderr, " [ root_block {on | off} ]\n");
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400227 fprintf(stderr, " [ learning {on | off} ]\n");
Scott Feldman674bb432014-12-29 12:20:07 -0800228 fprintf(stderr, " [ learning_sync {on | off} ]\n");
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400229 fprintf(stderr, " [ flood {on | off} ]\n");
Vlad Yasevich64108902013-03-15 10:01:28 -0700230 fprintf(stderr, " [ hwmode {vepa | veb} ]\n");
Roopa Prabhu22a98f52015-02-26 00:12:59 -0800231 fprintf(stderr, " [ self ] [ master ]\n");
Vlad Yasevich64108902013-03-15 10:01:28 -0700232 fprintf(stderr, " bridge link show [dev DEV]\n");
233 exit(-1);
234}
235
236static bool on_off(char *arg, __s8 *attr, char *val)
237{
238 if (strcmp(val, "on") == 0)
239 *attr = 1;
240 else if (strcmp(val, "off") == 0)
241 *attr = 0;
242 else {
243 fprintf(stderr,
244 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
245 arg);
246 return false;
247 }
248
249 return true;
250}
251
252static int brlink_modify(int argc, char **argv)
253{
254 struct {
255 struct nlmsghdr n;
256 struct ifinfomsg ifm;
257 char buf[512];
258 } req;
259 char *d = NULL;
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400260 __s8 learning = -1;
Scott Feldman674bb432014-12-29 12:20:07 -0800261 __s8 learning_sync = -1;
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400262 __s8 flood = -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700263 __s8 hairpin = -1;
264 __s8 bpdu_guard = -1;
265 __s8 fast_leave = -1;
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700266 __s8 root_block = -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700267 __u32 cost = 0;
268 __s16 priority = -1;
269 __s8 state = -1;
270 __s16 mode = -1;
Roopa Prabhu6fdb4652014-12-06 00:21:01 -0800271 __u16 flags = 0;
Vlad Yasevich64108902013-03-15 10:01:28 -0700272 struct rtattr *nest;
273
274 memset(&req, 0, sizeof(req));
275
276 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
277 req.n.nlmsg_flags = NLM_F_REQUEST;
278 req.n.nlmsg_type = RTM_SETLINK;
279 req.ifm.ifi_family = PF_BRIDGE;
280
281 while (argc > 0) {
282 if (strcmp(*argv, "dev") == 0) {
283 NEXT_ARG();
284 d = *argv;
285 } else if (strcmp(*argv, "guard") == 0) {
286 NEXT_ARG();
287 if (!on_off("guard", &bpdu_guard, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700288 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700289 } else if (strcmp(*argv, "hairpin") == 0) {
290 NEXT_ARG();
291 if (!on_off("hairping", &hairpin, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700292 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700293 } else if (strcmp(*argv, "fastleave") == 0) {
294 NEXT_ARG();
295 if (!on_off("fastleave", &fast_leave, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700296 return -1;
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700297 } else if (strcmp(*argv, "root_block") == 0) {
298 NEXT_ARG();
299 if (!on_off("root_block", &root_block, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700300 return -1;
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400301 } else if (strcmp(*argv, "learning") == 0) {
302 NEXT_ARG();
303 if (!on_off("learning", &learning, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700304 return -1;
Scott Feldman674bb432014-12-29 12:20:07 -0800305 } else if (strcmp(*argv, "learning_sync") == 0) {
306 NEXT_ARG();
307 if (!on_off("learning_sync", &learning_sync, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700308 return -1;
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400309 } else if (strcmp(*argv, "flood") == 0) {
310 NEXT_ARG();
311 if (!on_off("flood", &flood, *argv))
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700312 return -1;
John Fastabenda40d0822013-05-29 06:20:53 +0000313 } else if (strcmp(*argv, "cost") == 0) {
Vlad Yasevich64108902013-03-15 10:01:28 -0700314 NEXT_ARG();
315 cost = atoi(*argv);
John Fastabenda40d0822013-05-29 06:20:53 +0000316 } else if (strcmp(*argv, "priority") == 0) {
Vlad Yasevich64108902013-03-15 10:01:28 -0700317 NEXT_ARG();
318 priority = atoi(*argv);
John Fastabenda40d0822013-05-29 06:20:53 +0000319 } else if (strcmp(*argv, "state") == 0) {
Vlad Yasevich64108902013-03-15 10:01:28 -0700320 NEXT_ARG();
Alex Pilon6b8c8712015-02-19 14:27:46 -0500321 char *endptr;
Phil Sutter62000e52016-06-28 18:42:15 +0200322 size_t nstates = ARRAY_SIZE(port_states);
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700323
Alex Pilon6b8c8712015-02-19 14:27:46 -0500324 state = strtol(*argv, &endptr, 10);
325 if (!(**argv != '\0' && *endptr == '\0')) {
326 for (state = 0; state < nstates; state++)
327 if (strcmp(port_states[state], *argv) == 0)
328 break;
329 if (state == nstates) {
330 fprintf(stderr,
331 "Error: invalid STP port state\n");
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700332 return -1;
Alex Pilon6b8c8712015-02-19 14:27:46 -0500333 }
334 }
John Fastabenda40d0822013-05-29 06:20:53 +0000335 } else if (strcmp(*argv, "hwmode") == 0) {
Vlad Yasevich64108902013-03-15 10:01:28 -0700336 NEXT_ARG();
John Fastabenda40d0822013-05-29 06:20:53 +0000337 flags = BRIDGE_FLAGS_SELF;
Vlad Yasevich64108902013-03-15 10:01:28 -0700338 if (strcmp(*argv, "vepa") == 0)
339 mode = BRIDGE_MODE_VEPA;
340 else if (strcmp(*argv, "veb") == 0)
341 mode = BRIDGE_MODE_VEB;
342 else {
343 fprintf(stderr,
Stephen Hemmingerdf4b0432016-03-21 11:56:01 -0700344 "Mode argument must be \"vepa\" or \"veb\".\n");
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700345 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700346 }
Roopa Prabhu6fdb4652014-12-06 00:21:01 -0800347 } else if (strcmp(*argv, "self") == 0) {
Roopa Prabhu22a98f52015-02-26 00:12:59 -0800348 flags |= BRIDGE_FLAGS_SELF;
349 } else if (strcmp(*argv, "master") == 0) {
350 flags |= BRIDGE_FLAGS_MASTER;
Vlad Yasevich64108902013-03-15 10:01:28 -0700351 } else {
352 usage();
353 }
354 argc--; argv++;
355 }
356 if (d == NULL) {
357 fprintf(stderr, "Device is a required argument.\n");
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700358 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700359 }
360
361
362 req.ifm.ifi_index = ll_name_to_index(d);
363 if (req.ifm.ifi_index == 0) {
364 fprintf(stderr, "Cannot find bridge device \"%s\"\n", d);
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700365 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700366 }
367
368 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
369 * state.
370 */
371 nest = addattr_nest(&req.n, sizeof(req),
372 IFLA_PROTINFO | NLA_F_NESTED);
373 /* Flags first */
374 if (bpdu_guard >= 0)
375 addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard);
376 if (hairpin >= 0)
377 addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin);
378 if (fast_leave >= 0)
379 addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE,
380 fast_leave);
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700381 if (root_block >= 0)
382 addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block);
Vlad Yasevichf0f4ab62014-05-21 09:53:43 -0400383 if (flood >= 0)
384 addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood);
385 if (learning >= 0)
386 addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning);
Scott Feldman674bb432014-12-29 12:20:07 -0800387 if (learning_sync >= 0)
388 addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING_SYNC,
389 learning_sync);
Vlad Yasevich64108902013-03-15 10:01:28 -0700390
391 if (cost > 0)
392 addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost);
393
394 if (priority >= 0)
395 addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority);
396
397 if (state >= 0)
398 addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state);
399
400 addattr_nest_end(&req.n, nest);
401
Roopa Prabhu6fdb4652014-12-06 00:21:01 -0800402 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
403 * designates master or self operation and IFLA_BRIDGE_MODE
404 * for hw 'vepa' or 'veb' operation modes. The hwmodes are
405 * only valid in 'self' mode on some devices so far.
Vlad Yasevich64108902013-03-15 10:01:28 -0700406 */
Roopa Prabhu6fdb4652014-12-06 00:21:01 -0800407 if (mode >= 0 || flags > 0) {
Vlad Yasevich64108902013-03-15 10:01:28 -0700408 nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
409
Roopa Prabhu6fdb4652014-12-06 00:21:01 -0800410 if (flags > 0)
411 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags);
Vlad Yasevich64108902013-03-15 10:01:28 -0700412
413 if (mode >= 0)
414 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode);
415
416 addattr_nest_end(&req.n, nest);
417 }
418
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700419 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
Roopa Prabhu42ecedd2015-03-17 19:26:32 -0700420 return -1;
Vlad Yasevich64108902013-03-15 10:01:28 -0700421
422 return 0;
423}
424
425static int brlink_show(int argc, char **argv)
426{
427 char *filter_dev = NULL;
428
429 while (argc > 0) {
430 if (strcmp(*argv, "dev") == 0) {
431 NEXT_ARG();
432 if (filter_dev)
433 duparg("dev", *argv);
434 filter_dev = *argv;
435 }
436 argc--; argv++;
437 }
438
439 if (filter_dev) {
440 if ((filter_index = ll_name_to_index(filter_dev)) == 0) {
441 fprintf(stderr, "Cannot find device \"%s\"\n",
442 filter_dev);
443 return -1;
444 }
445 }
446
447 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) {
448 perror("Cannon send dump request");
449 exit(1);
450 }
451
452 if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
453 fprintf(stderr, "Dump terminated\n");
454 exit(1);
455 }
456 return 0;
457}
458
459int do_link(int argc, char **argv)
460{
461 ll_init_map(&rth);
462 if (argc > 0) {
463 if (matches(*argv, "set") == 0 ||
464 matches(*argv, "change") == 0)
465 return brlink_modify(argc-1, argv+1);
466 if (matches(*argv, "show") == 0 ||
467 matches(*argv, "lst") == 0 ||
468 matches(*argv, "list") == 0)
469 return brlink_show(argc-1, argv+1);
470 if (matches(*argv, "help") == 0)
471 usage();
472 } else
473 return brlink_show(0, NULL);
474
475 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv);
476 exit(-1);
477}