]>
Commit | Line | Data |
---|---|---|
6054c1eb | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
d04bc300 SH |
2 | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <unistd.h> | |
6 | #include <time.h> | |
7 | #include <sys/socket.h> | |
8 | #include <sys/time.h> | |
9 | #include <netinet/in.h> | |
10 | #include <linux/if.h> | |
11 | #include <linux/if_bridge.h> | |
12 | #include <string.h> | |
64108901 | 13 | #include <stdbool.h> |
d04bc300 | 14 | |
f32e4977 | 15 | #include "json_print.h" |
64108901 | 16 | #include "libnetlink.h" |
d04bc300 SH |
17 | #include "utils.h" |
18 | #include "br_common.h" | |
19 | ||
9dca899b | 20 | static unsigned int filter_index; |
64108901 | 21 | |
d04bc300 SH |
22 | static const char *port_states[] = { |
23 | [BR_STATE_DISABLED] = "disabled", | |
24 | [BR_STATE_LISTENING] = "listening", | |
25 | [BR_STATE_LEARNING] = "learning", | |
26 | [BR_STATE_FORWARDING] = "forwarding", | |
27 | [BR_STATE_BLOCKING] = "blocking", | |
28 | }; | |
29 | ||
f32e4977 SH |
30 | static const char *hw_mode[] = { |
31 | "VEB", "VEPA" | |
32 | }; | |
33 | ||
34 | static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown) | |
d04bc300 | 35 | { |
f32e4977 | 36 | open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<"); |
d04bc300 | 37 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
f32e4977 SH |
38 | print_string(PRINT_ANY, NULL, |
39 | flags ? "%s," : "%s", "NO-CARRIER"); | |
d04bc300 | 40 | flags &= ~IFF_RUNNING; |
f32e4977 SH |
41 | |
42 | #define _PF(f) if (flags&IFF_##f) { \ | |
43 | flags &= ~IFF_##f ; \ | |
44 | print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); } | |
d04bc300 SH |
45 | _PF(LOOPBACK); |
46 | _PF(BROADCAST); | |
47 | _PF(POINTOPOINT); | |
48 | _PF(MULTICAST); | |
49 | _PF(NOARP); | |
50 | _PF(ALLMULTI); | |
51 | _PF(PROMISC); | |
52 | _PF(MASTER); | |
53 | _PF(SLAVE); | |
54 | _PF(DEBUG); | |
55 | _PF(DYNAMIC); | |
56 | _PF(AUTOMEDIA); | |
57 | _PF(PORTSEL); | |
58 | _PF(NOTRAILERS); | |
59 | _PF(UP); | |
60 | _PF(LOWER_UP); | |
61 | _PF(DORMANT); | |
62 | _PF(ECHO); | |
63 | #undef _PF | |
df4b043f | 64 | if (flags) |
f32e4977 SH |
65 | print_hex(PRINT_ANY, NULL, "%x", flags); |
66 | if (mdown) | |
67 | print_string(PRINT_ANY, NULL, ",%s", "M-DOWN"); | |
68 | close_json_array(PRINT_ANY, "> "); | |
d04bc300 SH |
69 | } |
70 | ||
f32e4977 SH |
71 | static void print_portstate(__u8 state) |
72 | { | |
73 | if (state <= BR_STATE_BLOCKING) | |
74 | print_string(PRINT_ANY, "state", | |
75 | "state %s ", port_states[state]); | |
76 | else | |
77 | print_uint(PRINT_ANY, "state", | |
78 | "state (%d) ", state); | |
79 | } | |
b1b7ce0f | 80 | |
f32e4977 | 81 | static void print_onoff(FILE *fp, const char *flag, __u8 val) |
d04bc300 | 82 | { |
f32e4977 SH |
83 | if (is_json_context()) |
84 | print_bool(PRINT_JSON, flag, NULL, val); | |
d04bc300 | 85 | else |
f32e4977 | 86 | fprintf(fp, "%s %s ", flag, val ? "on" : "off"); |
d04bc300 SH |
87 | } |
88 | ||
f32e4977 | 89 | static void print_hwmode(__u16 mode) |
b1b7ce0f | 90 | { |
f32e4977 SH |
91 | if (mode >= ARRAY_SIZE(hw_mode)) |
92 | print_0xhex(PRINT_ANY, "hwmode", | |
93 | "hwmode %#hx ", mode); | |
b1b7ce0f | 94 | else |
f32e4977 SH |
95 | print_string(PRINT_ANY, "hwmode", |
96 | "hwmode %s ", hw_mode[mode]); | |
b1b7ce0f VY |
97 | } |
98 | ||
f32e4977 | 99 | static void print_protinfo(FILE *fp, struct rtattr *attr) |
b1b7ce0f | 100 | { |
f32e4977 SH |
101 | if (attr->rta_type & NLA_F_NESTED) { |
102 | struct rtattr *prtb[IFLA_BRPORT_MAX + 1]; | |
103 | ||
104 | parse_rtattr_nested(prtb, IFLA_BRPORT_MAX, attr); | |
105 | ||
106 | if (prtb[IFLA_BRPORT_STATE]) | |
107 | print_portstate(rta_getattr_u8(prtb[IFLA_BRPORT_STATE])); | |
108 | ||
109 | if (prtb[IFLA_BRPORT_PRIORITY]) | |
110 | print_uint(PRINT_ANY, "priority", | |
111 | "priority %u ", | |
112 | rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY])); | |
113 | ||
114 | if (prtb[IFLA_BRPORT_COST]) | |
115 | print_uint(PRINT_ANY, "cost", | |
116 | "cost %u ", | |
117 | rta_getattr_u32(prtb[IFLA_BRPORT_COST])); | |
118 | ||
119 | if (!show_details) | |
120 | return; | |
121 | ||
122 | if (!is_json_context()) | |
123 | fprintf(fp, "%s ", _SL_); | |
124 | ||
125 | if (prtb[IFLA_BRPORT_MODE]) | |
126 | print_onoff(fp, "hairpin", | |
127 | rta_getattr_u8(prtb[IFLA_BRPORT_MODE])); | |
128 | if (prtb[IFLA_BRPORT_GUARD]) | |
129 | print_onoff(fp, "guard", | |
130 | rta_getattr_u8(prtb[IFLA_BRPORT_GUARD])); | |
131 | if (prtb[IFLA_BRPORT_PROTECT]) | |
132 | print_onoff(fp, "root_block", | |
133 | rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT])); | |
134 | if (prtb[IFLA_BRPORT_FAST_LEAVE]) | |
135 | print_onoff(fp, "fastleave", | |
136 | rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE])); | |
137 | if (prtb[IFLA_BRPORT_LEARNING]) | |
138 | print_onoff(fp, "learning", | |
139 | rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING])); | |
140 | if (prtb[IFLA_BRPORT_LEARNING_SYNC]) | |
141 | print_onoff(fp, "learning_sync", | |
142 | rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC])); | |
143 | if (prtb[IFLA_BRPORT_UNICAST_FLOOD]) | |
144 | print_onoff(fp, "flood", | |
145 | rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD])); | |
146 | if (prtb[IFLA_BRPORT_MCAST_FLOOD]) | |
147 | print_onoff(fp, "mcast_flood", | |
148 | rta_getattr_u8(prtb[IFLA_BRPORT_MCAST_FLOOD])); | |
149 | if (prtb[IFLA_BRPORT_NEIGH_SUPPRESS]) | |
150 | print_onoff(fp, "neigh_suppress", | |
151 | rta_getattr_u8(prtb[IFLA_BRPORT_NEIGH_SUPPRESS])); | |
152 | if (prtb[IFLA_BRPORT_VLAN_TUNNEL]) | |
153 | print_onoff(fp, "vlan_tunnel", | |
154 | rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL])); | |
d13d52d0 NA |
155 | |
156 | if (prtb[IFLA_BRPORT_BACKUP_PORT]) { | |
157 | int ifidx; | |
158 | ||
159 | ifidx = rta_getattr_u32(prtb[IFLA_BRPORT_BACKUP_PORT]); | |
160 | print_string(PRINT_ANY, | |
161 | "backup_port", "backup_port %s ", | |
162 | ll_index_to_name(ifidx)); | |
163 | } | |
164 | ||
05001bcf NA |
165 | if (prtb[IFLA_BRPORT_ISOLATED]) |
166 | print_onoff(fp, "isolated", | |
167 | rta_getattr_u8(prtb[IFLA_BRPORT_ISOLATED])); | |
f32e4977 SH |
168 | } else |
169 | print_portstate(rta_getattr_u8(attr)); | |
b1b7ce0f VY |
170 | } |
171 | ||
f32e4977 SH |
172 | |
173 | /* | |
174 | * This is reported by HW devices that have some bridging | |
175 | * capabilities. | |
176 | */ | |
0f362674 | 177 | static void print_af_spec(struct rtattr *attr, int ifindex) |
b1b7ce0f | 178 | { |
f32e4977 SH |
179 | struct rtattr *aftb[IFLA_BRIDGE_MAX+1]; |
180 | ||
181 | parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, attr); | |
182 | ||
183 | if (aftb[IFLA_BRIDGE_MODE]) | |
184 | print_hwmode(rta_getattr_u16(aftb[IFLA_BRIDGE_MODE])); | |
185 | ||
186 | if (!show_details) | |
187 | return; | |
188 | ||
189 | if (aftb[IFLA_BRIDGE_VLAN_INFO]) | |
0f362674 | 190 | print_vlan_info(aftb[IFLA_BRIDGE_VLAN_INFO], ifindex); |
b1b7ce0f VY |
191 | } |
192 | ||
d04bc300 SH |
193 | int print_linkinfo(const struct sockaddr_nl *who, |
194 | struct nlmsghdr *n, void *arg) | |
195 | { | |
196 | FILE *fp = arg; | |
d04bc300 | 197 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
df4b043f | 198 | struct rtattr *tb[IFLA_MAX+1]; |
f32e4977 | 199 | unsigned int m_flag = 0; |
fcac9665 SP |
200 | int len = n->nlmsg_len; |
201 | const char *name; | |
d04bc300 SH |
202 | |
203 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
204 | if (len < 0) { | |
205 | fprintf(stderr, "Message too short!\n"); | |
206 | return -1; | |
df4b043f | 207 | } |
d04bc300 SH |
208 | |
209 | if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC)) | |
210 | return 0; | |
211 | ||
64108901 VY |
212 | if (filter_index && filter_index != ifi->ifi_index) |
213 | return 0; | |
214 | ||
b1b7ce0f | 215 | parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED); |
d04bc300 | 216 | |
fcac9665 SP |
217 | name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); |
218 | if (!name) | |
d04bc300 | 219 | return -1; |
d04bc300 | 220 | |
f32e4977 | 221 | open_json_object(NULL); |
d04bc300 | 222 | if (n->nlmsg_type == RTM_DELLINK) |
f32e4977 | 223 | print_bool(PRINT_ANY, "deleted", "Deleted ", true); |
d04bc300 | 224 | |
f32e4977 | 225 | print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index); |
4328b687 | 226 | m_flag = print_name_and_link("%s: ", name, tb); |
f32e4977 | 227 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
d04bc300 SH |
228 | |
229 | if (tb[IFLA_MTU]) | |
f32e4977 SH |
230 | print_int(PRINT_ANY, |
231 | "mtu", "mtu %u ", | |
232 | rta_getattr_u32(tb[IFLA_MTU])); | |
d04bc300 | 233 | |
45d3a6ef SP |
234 | if (tb[IFLA_MASTER]) { |
235 | int master = rta_getattr_u32(tb[IFLA_MASTER]); | |
236 | ||
f32e4977 SH |
237 | print_string(PRINT_ANY, "master", "master %s ", |
238 | ll_index_to_name(master)); | |
45d3a6ef | 239 | } |
d04bc300 | 240 | |
f32e4977 SH |
241 | if (tb[IFLA_PROTINFO]) |
242 | print_protinfo(fp, tb[IFLA_PROTINFO]); | |
d04bc300 | 243 | |
f32e4977 | 244 | if (tb[IFLA_AF_SPEC]) |
0f362674 | 245 | print_af_spec(tb[IFLA_AF_SPEC], ifi->ifi_index); |
d04bc300 | 246 | |
f32e4977 SH |
247 | print_string(PRINT_FP, NULL, "%s", "\n"); |
248 | close_json_object(); | |
d04bc300 SH |
249 | fflush(fp); |
250 | return 0; | |
251 | } | |
64108901 VY |
252 | |
253 | static void usage(void) | |
254 | { | |
255 | fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n"); | |
256 | fprintf(stderr, " [ guard {on | off} ]\n"); | |
df4b043f | 257 | fprintf(stderr, " [ hairpin {on | off} ]\n"); |
64108901 VY |
258 | fprintf(stderr, " [ fastleave {on | off} ]\n"); |
259 | fprintf(stderr, " [ root_block {on | off} ]\n"); | |
f0f4ab60 | 260 | fprintf(stderr, " [ learning {on | off} ]\n"); |
674bb438 | 261 | fprintf(stderr, " [ learning_sync {on | off} ]\n"); |
f0f4ab60 | 262 | fprintf(stderr, " [ flood {on | off} ]\n"); |
9208b4e7 | 263 | fprintf(stderr, " [ mcast_flood {on | off} ]\n"); |
8cfde5c9 RP |
264 | fprintf(stderr, " [ neigh_suppress {on | off} ]\n"); |
265 | fprintf(stderr, " [ vlan_tunnel {on | off} ]\n"); | |
05001bcf | 266 | fprintf(stderr, " [ isolated {on | off} ]\n"); |
64108901 | 267 | fprintf(stderr, " [ hwmode {vepa | veb} ]\n"); |
d13d52d0 | 268 | fprintf(stderr, " [ backup_port DEVICE ] [ nobackup_port ]\n"); |
22a98f51 | 269 | fprintf(stderr, " [ self ] [ master ]\n"); |
64108901 VY |
270 | fprintf(stderr, " bridge link show [dev DEV]\n"); |
271 | exit(-1); | |
272 | } | |
273 | ||
274 | static bool on_off(char *arg, __s8 *attr, char *val) | |
275 | { | |
276 | if (strcmp(val, "on") == 0) | |
277 | *attr = 1; | |
278 | else if (strcmp(val, "off") == 0) | |
279 | *attr = 0; | |
280 | else { | |
281 | fprintf(stderr, | |
282 | "Error: argument of \"%s\" must be \"on\" or \"off\"\n", | |
283 | arg); | |
284 | return false; | |
285 | } | |
286 | ||
287 | return true; | |
288 | } | |
289 | ||
290 | static int brlink_modify(int argc, char **argv) | |
291 | { | |
292 | struct { | |
293 | struct nlmsghdr n; | |
294 | struct ifinfomsg ifm; | |
295 | char buf[512]; | |
d17b136f PS |
296 | } req = { |
297 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
298 | .n.nlmsg_flags = NLM_F_REQUEST, | |
299 | .n.nlmsg_type = RTM_SETLINK, | |
300 | .ifm.ifi_family = PF_BRIDGE, | |
301 | }; | |
64108901 | 302 | char *d = NULL; |
d13d52d0 | 303 | int backup_port_idx = -1; |
41973a47 | 304 | __s8 neigh_suppress = -1; |
f0f4ab60 | 305 | __s8 learning = -1; |
674bb438 | 306 | __s8 learning_sync = -1; |
f0f4ab60 | 307 | __s8 flood = -1; |
8cfde5c9 | 308 | __s8 vlan_tunnel = -1; |
9208b4e7 | 309 | __s8 mcast_flood = -1; |
05001bcf | 310 | __s8 isolated = -1; |
64108901 VY |
311 | __s8 hairpin = -1; |
312 | __s8 bpdu_guard = -1; | |
313 | __s8 fast_leave = -1; | |
b1b7ce0f | 314 | __s8 root_block = -1; |
64108901 VY |
315 | __u32 cost = 0; |
316 | __s16 priority = -1; | |
317 | __s8 state = -1; | |
318 | __s16 mode = -1; | |
6fdb4658 | 319 | __u16 flags = 0; |
64108901 VY |
320 | struct rtattr *nest; |
321 | ||
64108901 VY |
322 | while (argc > 0) { |
323 | if (strcmp(*argv, "dev") == 0) { | |
324 | NEXT_ARG(); | |
325 | d = *argv; | |
326 | } else if (strcmp(*argv, "guard") == 0) { | |
327 | NEXT_ARG(); | |
328 | if (!on_off("guard", &bpdu_guard, *argv)) | |
42ecedd4 | 329 | return -1; |
64108901 VY |
330 | } else if (strcmp(*argv, "hairpin") == 0) { |
331 | NEXT_ARG(); | |
ef367178 | 332 | if (!on_off("hairpin", &hairpin, *argv)) |
42ecedd4 | 333 | return -1; |
64108901 VY |
334 | } else if (strcmp(*argv, "fastleave") == 0) { |
335 | NEXT_ARG(); | |
336 | if (!on_off("fastleave", &fast_leave, *argv)) | |
42ecedd4 | 337 | return -1; |
b1b7ce0f VY |
338 | } else if (strcmp(*argv, "root_block") == 0) { |
339 | NEXT_ARG(); | |
340 | if (!on_off("root_block", &root_block, *argv)) | |
42ecedd4 | 341 | return -1; |
f0f4ab60 VY |
342 | } else if (strcmp(*argv, "learning") == 0) { |
343 | NEXT_ARG(); | |
344 | if (!on_off("learning", &learning, *argv)) | |
42ecedd4 | 345 | return -1; |
674bb438 SF |
346 | } else if (strcmp(*argv, "learning_sync") == 0) { |
347 | NEXT_ARG(); | |
348 | if (!on_off("learning_sync", &learning_sync, *argv)) | |
42ecedd4 | 349 | return -1; |
f0f4ab60 VY |
350 | } else if (strcmp(*argv, "flood") == 0) { |
351 | NEXT_ARG(); | |
352 | if (!on_off("flood", &flood, *argv)) | |
42ecedd4 | 353 | return -1; |
9208b4e7 NA |
354 | } else if (strcmp(*argv, "mcast_flood") == 0) { |
355 | NEXT_ARG(); | |
356 | if (!on_off("mcast_flood", &mcast_flood, *argv)) | |
357 | return -1; | |
a40d0827 | 358 | } else if (strcmp(*argv, "cost") == 0) { |
64108901 VY |
359 | NEXT_ARG(); |
360 | cost = atoi(*argv); | |
a40d0827 | 361 | } else if (strcmp(*argv, "priority") == 0) { |
64108901 VY |
362 | NEXT_ARG(); |
363 | priority = atoi(*argv); | |
a40d0827 | 364 | } else if (strcmp(*argv, "state") == 0) { |
64108901 | 365 | NEXT_ARG(); |
6b8c871d | 366 | char *endptr; |
62000e51 | 367 | size_t nstates = ARRAY_SIZE(port_states); |
df4b043f | 368 | |
6b8c871d AP |
369 | state = strtol(*argv, &endptr, 10); |
370 | if (!(**argv != '\0' && *endptr == '\0')) { | |
371 | for (state = 0; state < nstates; state++) | |
372 | if (strcmp(port_states[state], *argv) == 0) | |
373 | break; | |
374 | if (state == nstates) { | |
375 | fprintf(stderr, | |
376 | "Error: invalid STP port state\n"); | |
42ecedd4 | 377 | return -1; |
6b8c871d AP |
378 | } |
379 | } | |
a40d0827 | 380 | } else if (strcmp(*argv, "hwmode") == 0) { |
64108901 | 381 | NEXT_ARG(); |
a40d0827 | 382 | flags = BRIDGE_FLAGS_SELF; |
64108901 VY |
383 | if (strcmp(*argv, "vepa") == 0) |
384 | mode = BRIDGE_MODE_VEPA; | |
385 | else if (strcmp(*argv, "veb") == 0) | |
386 | mode = BRIDGE_MODE_VEB; | |
387 | else { | |
388 | fprintf(stderr, | |
df4b043f | 389 | "Mode argument must be \"vepa\" or \"veb\".\n"); |
42ecedd4 | 390 | return -1; |
64108901 | 391 | } |
6fdb4658 | 392 | } else if (strcmp(*argv, "self") == 0) { |
22a98f51 RP |
393 | flags |= BRIDGE_FLAGS_SELF; |
394 | } else if (strcmp(*argv, "master") == 0) { | |
395 | flags |= BRIDGE_FLAGS_MASTER; | |
41973a47 RP |
396 | } else if (strcmp(*argv, "neigh_suppress") == 0) { |
397 | NEXT_ARG(); | |
398 | if (!on_off("neigh_suppress", &neigh_suppress, | |
399 | *argv)) | |
400 | return -1; | |
8cfde5c9 RP |
401 | } else if (strcmp(*argv, "vlan_tunnel") == 0) { |
402 | NEXT_ARG(); | |
403 | if (!on_off("vlan_tunnel", &vlan_tunnel, | |
404 | *argv)) | |
405 | return -1; | |
05001bcf NA |
406 | } else if (strcmp(*argv, "isolated") == 0) { |
407 | NEXT_ARG(); | |
408 | if (!on_off("isolated", &isolated, *argv)) | |
409 | return -1; | |
d13d52d0 NA |
410 | } else if (strcmp(*argv, "backup_port") == 0) { |
411 | NEXT_ARG(); | |
412 | backup_port_idx = ll_name_to_index(*argv); | |
413 | if (!backup_port_idx) { | |
414 | fprintf(stderr, "Error: device %s does not exist\n", | |
415 | *argv); | |
416 | return -1; | |
417 | } | |
418 | } else if (strcmp(*argv, "nobackup_port") == 0) { | |
419 | backup_port_idx = 0; | |
64108901 VY |
420 | } else { |
421 | usage(); | |
422 | } | |
423 | argc--; argv++; | |
424 | } | |
425 | if (d == NULL) { | |
426 | fprintf(stderr, "Device is a required argument.\n"); | |
42ecedd4 | 427 | return -1; |
64108901 VY |
428 | } |
429 | ||
430 | ||
431 | req.ifm.ifi_index = ll_name_to_index(d); | |
432 | if (req.ifm.ifi_index == 0) { | |
433 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", d); | |
42ecedd4 | 434 | return -1; |
64108901 VY |
435 | } |
436 | ||
437 | /* Nested PROTINFO attribute. Contains: port flags, cost, priority and | |
438 | * state. | |
439 | */ | |
440 | nest = addattr_nest(&req.n, sizeof(req), | |
441 | IFLA_PROTINFO | NLA_F_NESTED); | |
442 | /* Flags first */ | |
443 | if (bpdu_guard >= 0) | |
444 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard); | |
445 | if (hairpin >= 0) | |
446 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin); | |
447 | if (fast_leave >= 0) | |
448 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE, | |
449 | fast_leave); | |
b1b7ce0f VY |
450 | if (root_block >= 0) |
451 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block); | |
f0f4ab60 VY |
452 | if (flood >= 0) |
453 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood); | |
9208b4e7 NA |
454 | if (mcast_flood >= 0) |
455 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MCAST_FLOOD, | |
456 | mcast_flood); | |
f0f4ab60 VY |
457 | if (learning >= 0) |
458 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning); | |
674bb438 SF |
459 | if (learning_sync >= 0) |
460 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING_SYNC, | |
461 | learning_sync); | |
64108901 VY |
462 | |
463 | if (cost > 0) | |
464 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost); | |
465 | ||
466 | if (priority >= 0) | |
467 | addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority); | |
468 | ||
469 | if (state >= 0) | |
470 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state); | |
471 | ||
41973a47 RP |
472 | if (neigh_suppress != -1) |
473 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_NEIGH_SUPPRESS, | |
474 | neigh_suppress); | |
8cfde5c9 RP |
475 | if (vlan_tunnel != -1) |
476 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_VLAN_TUNNEL, | |
477 | vlan_tunnel); | |
05001bcf NA |
478 | if (isolated != -1) |
479 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_ISOLATED, isolated); | |
41973a47 | 480 | |
d13d52d0 NA |
481 | if (backup_port_idx != -1) |
482 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_BACKUP_PORT, | |
483 | backup_port_idx); | |
484 | ||
64108901 VY |
485 | addattr_nest_end(&req.n, nest); |
486 | ||
6fdb4658 RP |
487 | /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that |
488 | * designates master or self operation and IFLA_BRIDGE_MODE | |
489 | * for hw 'vepa' or 'veb' operation modes. The hwmodes are | |
490 | * only valid in 'self' mode on some devices so far. | |
64108901 | 491 | */ |
6fdb4658 | 492 | if (mode >= 0 || flags > 0) { |
64108901 VY |
493 | nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC); |
494 | ||
6fdb4658 RP |
495 | if (flags > 0) |
496 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags); | |
64108901 VY |
497 | |
498 | if (mode >= 0) | |
499 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode); | |
500 | ||
501 | addattr_nest_end(&req.n, nest); | |
502 | } | |
503 | ||
86bf43c7 | 504 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
42ecedd4 | 505 | return -1; |
64108901 VY |
506 | |
507 | return 0; | |
508 | } | |
509 | ||
510 | static int brlink_show(int argc, char **argv) | |
511 | { | |
512 | char *filter_dev = NULL; | |
513 | ||
514 | while (argc > 0) { | |
515 | if (strcmp(*argv, "dev") == 0) { | |
516 | NEXT_ARG(); | |
517 | if (filter_dev) | |
518 | duparg("dev", *argv); | |
519 | filter_dev = *argv; | |
520 | } | |
521 | argc--; argv++; | |
522 | } | |
523 | ||
524 | if (filter_dev) { | |
fe99adbc SP |
525 | filter_index = ll_name_to_index(filter_dev); |
526 | if (!filter_index) | |
527 | return nodev(filter_dev); | |
64108901 VY |
528 | } |
529 | ||
fab9a18a | 530 | if (show_details) { |
31ae2912 | 531 | if (rtnl_linkdump_req_filter(&rth, PF_BRIDGE, |
fab9a18a RM |
532 | (compress_vlans ? |
533 | RTEXT_FILTER_BRVLAN_COMPRESSED : | |
534 | RTEXT_FILTER_BRVLAN)) < 0) { | |
535 | perror("Cannon send dump request"); | |
536 | exit(1); | |
537 | } | |
538 | } else { | |
31ae2912 | 539 | if (rtnl_linkdump_req(&rth, PF_BRIDGE) < 0) { |
fab9a18a RM |
540 | perror("Cannon send dump request"); |
541 | exit(1); | |
542 | } | |
64108901 VY |
543 | } |
544 | ||
f32e4977 | 545 | new_json_obj(json); |
64108901 VY |
546 | if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) { |
547 | fprintf(stderr, "Dump terminated\n"); | |
548 | exit(1); | |
549 | } | |
f32e4977 SH |
550 | |
551 | delete_json_obj(); | |
552 | fflush(stdout); | |
64108901 VY |
553 | return 0; |
554 | } | |
555 | ||
556 | int do_link(int argc, char **argv) | |
557 | { | |
558 | ll_init_map(&rth); | |
559 | if (argc > 0) { | |
560 | if (matches(*argv, "set") == 0 || | |
561 | matches(*argv, "change") == 0) | |
562 | return brlink_modify(argc-1, argv+1); | |
563 | if (matches(*argv, "show") == 0 || | |
564 | matches(*argv, "lst") == 0 || | |
565 | matches(*argv, "list") == 0) | |
566 | return brlink_show(argc-1, argv+1); | |
567 | if (matches(*argv, "help") == 0) | |
568 | usage(); | |
569 | } else | |
570 | return brlink_show(0, NULL); | |
571 | ||
572 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv); | |
573 | exit(-1); | |
574 | } |