]>
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", | |
90c5c969 | 93 | "hwmode %#llx ", 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])); | |
b5a754b1 TJ |
149 | if (prtb[IFLA_BRPORT_MCAST_TO_UCAST]) |
150 | print_onoff(fp, "mcast_to_unicast", | |
151 | rta_getattr_u8(prtb[IFLA_BRPORT_MCAST_TO_UCAST])); | |
f32e4977 SH |
152 | if (prtb[IFLA_BRPORT_NEIGH_SUPPRESS]) |
153 | print_onoff(fp, "neigh_suppress", | |
154 | rta_getattr_u8(prtb[IFLA_BRPORT_NEIGH_SUPPRESS])); | |
155 | if (prtb[IFLA_BRPORT_VLAN_TUNNEL]) | |
156 | print_onoff(fp, "vlan_tunnel", | |
157 | rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL])); | |
d13d52d0 NA |
158 | |
159 | if (prtb[IFLA_BRPORT_BACKUP_PORT]) { | |
160 | int ifidx; | |
161 | ||
162 | ifidx = rta_getattr_u32(prtb[IFLA_BRPORT_BACKUP_PORT]); | |
163 | print_string(PRINT_ANY, | |
164 | "backup_port", "backup_port %s ", | |
165 | ll_index_to_name(ifidx)); | |
166 | } | |
167 | ||
05001bcf NA |
168 | if (prtb[IFLA_BRPORT_ISOLATED]) |
169 | print_onoff(fp, "isolated", | |
170 | rta_getattr_u8(prtb[IFLA_BRPORT_ISOLATED])); | |
f32e4977 SH |
171 | } else |
172 | print_portstate(rta_getattr_u8(attr)); | |
b1b7ce0f VY |
173 | } |
174 | ||
f32e4977 SH |
175 | |
176 | /* | |
177 | * This is reported by HW devices that have some bridging | |
178 | * capabilities. | |
179 | */ | |
0f362674 | 180 | static void print_af_spec(struct rtattr *attr, int ifindex) |
b1b7ce0f | 181 | { |
f32e4977 SH |
182 | struct rtattr *aftb[IFLA_BRIDGE_MAX+1]; |
183 | ||
184 | parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, attr); | |
185 | ||
186 | if (aftb[IFLA_BRIDGE_MODE]) | |
187 | print_hwmode(rta_getattr_u16(aftb[IFLA_BRIDGE_MODE])); | |
188 | ||
189 | if (!show_details) | |
190 | return; | |
191 | ||
192 | if (aftb[IFLA_BRIDGE_VLAN_INFO]) | |
0f362674 | 193 | print_vlan_info(aftb[IFLA_BRIDGE_VLAN_INFO], ifindex); |
b1b7ce0f VY |
194 | } |
195 | ||
cd554f2c | 196 | int print_linkinfo(struct nlmsghdr *n, void *arg) |
d04bc300 SH |
197 | { |
198 | FILE *fp = arg; | |
d04bc300 | 199 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
df4b043f | 200 | struct rtattr *tb[IFLA_MAX+1]; |
f32e4977 | 201 | unsigned int m_flag = 0; |
fcac9665 SP |
202 | int len = n->nlmsg_len; |
203 | const char *name; | |
d04bc300 SH |
204 | |
205 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
206 | if (len < 0) { | |
207 | fprintf(stderr, "Message too short!\n"); | |
208 | return -1; | |
df4b043f | 209 | } |
d04bc300 SH |
210 | |
211 | if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC)) | |
212 | return 0; | |
213 | ||
64108901 VY |
214 | if (filter_index && filter_index != ifi->ifi_index) |
215 | return 0; | |
216 | ||
b1b7ce0f | 217 | parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED); |
d04bc300 | 218 | |
fcac9665 SP |
219 | name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); |
220 | if (!name) | |
d04bc300 | 221 | return -1; |
d04bc300 | 222 | |
f32e4977 | 223 | open_json_object(NULL); |
d04bc300 | 224 | if (n->nlmsg_type == RTM_DELLINK) |
f32e4977 | 225 | print_bool(PRINT_ANY, "deleted", "Deleted ", true); |
d04bc300 | 226 | |
f32e4977 | 227 | print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index); |
4328b687 | 228 | m_flag = print_name_and_link("%s: ", name, tb); |
f32e4977 | 229 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
d04bc300 SH |
230 | |
231 | if (tb[IFLA_MTU]) | |
f32e4977 SH |
232 | print_int(PRINT_ANY, |
233 | "mtu", "mtu %u ", | |
234 | rta_getattr_u32(tb[IFLA_MTU])); | |
d04bc300 | 235 | |
45d3a6ef SP |
236 | if (tb[IFLA_MASTER]) { |
237 | int master = rta_getattr_u32(tb[IFLA_MASTER]); | |
238 | ||
f32e4977 SH |
239 | print_string(PRINT_ANY, "master", "master %s ", |
240 | ll_index_to_name(master)); | |
45d3a6ef | 241 | } |
d04bc300 | 242 | |
f32e4977 SH |
243 | if (tb[IFLA_PROTINFO]) |
244 | print_protinfo(fp, tb[IFLA_PROTINFO]); | |
d04bc300 | 245 | |
f32e4977 | 246 | if (tb[IFLA_AF_SPEC]) |
0f362674 | 247 | print_af_spec(tb[IFLA_AF_SPEC], ifi->ifi_index); |
d04bc300 | 248 | |
f32e4977 SH |
249 | print_string(PRINT_FP, NULL, "%s", "\n"); |
250 | close_json_object(); | |
d04bc300 SH |
251 | fflush(fp); |
252 | return 0; | |
253 | } | |
64108901 VY |
254 | |
255 | static void usage(void) | |
256 | { | |
8589eb4e MC |
257 | fprintf(stderr, |
258 | "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n" | |
259 | " [ guard {on | off} ]\n" | |
260 | " [ hairpin {on | off} ]\n" | |
261 | " [ fastleave {on | off} ]\n" | |
262 | " [ root_block {on | off} ]\n" | |
263 | " [ learning {on | off} ]\n" | |
264 | " [ learning_sync {on | off} ]\n" | |
265 | " [ flood {on | off} ]\n" | |
266 | " [ mcast_flood {on | off} ]\n" | |
267 | " [ mcast_to_unicast {on | off} ]\n" | |
268 | " [ neigh_suppress {on | off} ]\n" | |
269 | " [ vlan_tunnel {on | off} ]\n" | |
270 | " [ isolated {on | off} ]\n" | |
271 | " [ hwmode {vepa | veb} ]\n" | |
272 | " [ backup_port DEVICE ] [ nobackup_port ]\n" | |
273 | " [ self ] [ master ]\n" | |
274 | " bridge link show [dev DEV]\n"); | |
64108901 VY |
275 | exit(-1); |
276 | } | |
277 | ||
278 | static bool on_off(char *arg, __s8 *attr, char *val) | |
279 | { | |
280 | if (strcmp(val, "on") == 0) | |
281 | *attr = 1; | |
282 | else if (strcmp(val, "off") == 0) | |
283 | *attr = 0; | |
284 | else { | |
285 | fprintf(stderr, | |
286 | "Error: argument of \"%s\" must be \"on\" or \"off\"\n", | |
287 | arg); | |
288 | return false; | |
289 | } | |
290 | ||
291 | return true; | |
292 | } | |
293 | ||
294 | static int brlink_modify(int argc, char **argv) | |
295 | { | |
296 | struct { | |
297 | struct nlmsghdr n; | |
298 | struct ifinfomsg ifm; | |
299 | char buf[512]; | |
d17b136f PS |
300 | } req = { |
301 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
302 | .n.nlmsg_flags = NLM_F_REQUEST, | |
303 | .n.nlmsg_type = RTM_SETLINK, | |
304 | .ifm.ifi_family = PF_BRIDGE, | |
305 | }; | |
64108901 | 306 | char *d = NULL; |
d13d52d0 | 307 | int backup_port_idx = -1; |
41973a47 | 308 | __s8 neigh_suppress = -1; |
f0f4ab60 | 309 | __s8 learning = -1; |
674bb438 | 310 | __s8 learning_sync = -1; |
f0f4ab60 | 311 | __s8 flood = -1; |
8cfde5c9 | 312 | __s8 vlan_tunnel = -1; |
9208b4e7 | 313 | __s8 mcast_flood = -1; |
b5a754b1 | 314 | __s8 mcast_to_unicast = -1; |
05001bcf | 315 | __s8 isolated = -1; |
64108901 VY |
316 | __s8 hairpin = -1; |
317 | __s8 bpdu_guard = -1; | |
318 | __s8 fast_leave = -1; | |
b1b7ce0f | 319 | __s8 root_block = -1; |
64108901 VY |
320 | __u32 cost = 0; |
321 | __s16 priority = -1; | |
322 | __s8 state = -1; | |
323 | __s16 mode = -1; | |
6fdb4658 | 324 | __u16 flags = 0; |
64108901 VY |
325 | struct rtattr *nest; |
326 | ||
64108901 VY |
327 | while (argc > 0) { |
328 | if (strcmp(*argv, "dev") == 0) { | |
329 | NEXT_ARG(); | |
330 | d = *argv; | |
331 | } else if (strcmp(*argv, "guard") == 0) { | |
332 | NEXT_ARG(); | |
333 | if (!on_off("guard", &bpdu_guard, *argv)) | |
42ecedd4 | 334 | return -1; |
64108901 VY |
335 | } else if (strcmp(*argv, "hairpin") == 0) { |
336 | NEXT_ARG(); | |
ef367178 | 337 | if (!on_off("hairpin", &hairpin, *argv)) |
42ecedd4 | 338 | return -1; |
64108901 VY |
339 | } else if (strcmp(*argv, "fastleave") == 0) { |
340 | NEXT_ARG(); | |
341 | if (!on_off("fastleave", &fast_leave, *argv)) | |
42ecedd4 | 342 | return -1; |
b1b7ce0f VY |
343 | } else if (strcmp(*argv, "root_block") == 0) { |
344 | NEXT_ARG(); | |
345 | if (!on_off("root_block", &root_block, *argv)) | |
42ecedd4 | 346 | return -1; |
f0f4ab60 VY |
347 | } else if (strcmp(*argv, "learning") == 0) { |
348 | NEXT_ARG(); | |
349 | if (!on_off("learning", &learning, *argv)) | |
42ecedd4 | 350 | return -1; |
674bb438 SF |
351 | } else if (strcmp(*argv, "learning_sync") == 0) { |
352 | NEXT_ARG(); | |
353 | if (!on_off("learning_sync", &learning_sync, *argv)) | |
42ecedd4 | 354 | return -1; |
f0f4ab60 VY |
355 | } else if (strcmp(*argv, "flood") == 0) { |
356 | NEXT_ARG(); | |
357 | if (!on_off("flood", &flood, *argv)) | |
42ecedd4 | 358 | return -1; |
9208b4e7 NA |
359 | } else if (strcmp(*argv, "mcast_flood") == 0) { |
360 | NEXT_ARG(); | |
361 | if (!on_off("mcast_flood", &mcast_flood, *argv)) | |
362 | return -1; | |
b5a754b1 TJ |
363 | } else if (strcmp(*argv, "mcast_to_unicast") == 0) { |
364 | NEXT_ARG(); | |
365 | if (!on_off("mcast_to_unicast", &mcast_to_unicast, *argv)) | |
366 | return -1; | |
a40d0827 | 367 | } else if (strcmp(*argv, "cost") == 0) { |
64108901 VY |
368 | NEXT_ARG(); |
369 | cost = atoi(*argv); | |
a40d0827 | 370 | } else if (strcmp(*argv, "priority") == 0) { |
64108901 VY |
371 | NEXT_ARG(); |
372 | priority = atoi(*argv); | |
a40d0827 | 373 | } else if (strcmp(*argv, "state") == 0) { |
64108901 | 374 | NEXT_ARG(); |
6b8c871d | 375 | char *endptr; |
62000e51 | 376 | size_t nstates = ARRAY_SIZE(port_states); |
df4b043f | 377 | |
6b8c871d AP |
378 | state = strtol(*argv, &endptr, 10); |
379 | if (!(**argv != '\0' && *endptr == '\0')) { | |
380 | for (state = 0; state < nstates; state++) | |
381 | if (strcmp(port_states[state], *argv) == 0) | |
382 | break; | |
383 | if (state == nstates) { | |
384 | fprintf(stderr, | |
385 | "Error: invalid STP port state\n"); | |
42ecedd4 | 386 | return -1; |
6b8c871d AP |
387 | } |
388 | } | |
a40d0827 | 389 | } else if (strcmp(*argv, "hwmode") == 0) { |
64108901 | 390 | NEXT_ARG(); |
a40d0827 | 391 | flags = BRIDGE_FLAGS_SELF; |
64108901 VY |
392 | if (strcmp(*argv, "vepa") == 0) |
393 | mode = BRIDGE_MODE_VEPA; | |
394 | else if (strcmp(*argv, "veb") == 0) | |
395 | mode = BRIDGE_MODE_VEB; | |
396 | else { | |
397 | fprintf(stderr, | |
df4b043f | 398 | "Mode argument must be \"vepa\" or \"veb\".\n"); |
42ecedd4 | 399 | return -1; |
64108901 | 400 | } |
6fdb4658 | 401 | } else if (strcmp(*argv, "self") == 0) { |
22a98f51 RP |
402 | flags |= BRIDGE_FLAGS_SELF; |
403 | } else if (strcmp(*argv, "master") == 0) { | |
404 | flags |= BRIDGE_FLAGS_MASTER; | |
41973a47 RP |
405 | } else if (strcmp(*argv, "neigh_suppress") == 0) { |
406 | NEXT_ARG(); | |
407 | if (!on_off("neigh_suppress", &neigh_suppress, | |
408 | *argv)) | |
409 | return -1; | |
8cfde5c9 RP |
410 | } else if (strcmp(*argv, "vlan_tunnel") == 0) { |
411 | NEXT_ARG(); | |
412 | if (!on_off("vlan_tunnel", &vlan_tunnel, | |
413 | *argv)) | |
414 | return -1; | |
05001bcf NA |
415 | } else if (strcmp(*argv, "isolated") == 0) { |
416 | NEXT_ARG(); | |
417 | if (!on_off("isolated", &isolated, *argv)) | |
418 | return -1; | |
d13d52d0 NA |
419 | } else if (strcmp(*argv, "backup_port") == 0) { |
420 | NEXT_ARG(); | |
421 | backup_port_idx = ll_name_to_index(*argv); | |
422 | if (!backup_port_idx) { | |
423 | fprintf(stderr, "Error: device %s does not exist\n", | |
424 | *argv); | |
425 | return -1; | |
426 | } | |
427 | } else if (strcmp(*argv, "nobackup_port") == 0) { | |
428 | backup_port_idx = 0; | |
64108901 VY |
429 | } else { |
430 | usage(); | |
431 | } | |
432 | argc--; argv++; | |
433 | } | |
434 | if (d == NULL) { | |
435 | fprintf(stderr, "Device is a required argument.\n"); | |
42ecedd4 | 436 | return -1; |
64108901 VY |
437 | } |
438 | ||
439 | ||
440 | req.ifm.ifi_index = ll_name_to_index(d); | |
441 | if (req.ifm.ifi_index == 0) { | |
442 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", d); | |
42ecedd4 | 443 | return -1; |
64108901 VY |
444 | } |
445 | ||
446 | /* Nested PROTINFO attribute. Contains: port flags, cost, priority and | |
447 | * state. | |
448 | */ | |
449 | nest = addattr_nest(&req.n, sizeof(req), | |
450 | IFLA_PROTINFO | NLA_F_NESTED); | |
451 | /* Flags first */ | |
452 | if (bpdu_guard >= 0) | |
453 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard); | |
454 | if (hairpin >= 0) | |
455 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin); | |
456 | if (fast_leave >= 0) | |
457 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE, | |
458 | fast_leave); | |
b1b7ce0f VY |
459 | if (root_block >= 0) |
460 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block); | |
f0f4ab60 VY |
461 | if (flood >= 0) |
462 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood); | |
9208b4e7 NA |
463 | if (mcast_flood >= 0) |
464 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MCAST_FLOOD, | |
465 | mcast_flood); | |
b5a754b1 TJ |
466 | if (mcast_to_unicast >= 0) |
467 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MCAST_TO_UCAST, | |
468 | mcast_to_unicast); | |
f0f4ab60 VY |
469 | if (learning >= 0) |
470 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning); | |
674bb438 SF |
471 | if (learning_sync >= 0) |
472 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING_SYNC, | |
473 | learning_sync); | |
64108901 VY |
474 | |
475 | if (cost > 0) | |
476 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost); | |
477 | ||
478 | if (priority >= 0) | |
479 | addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority); | |
480 | ||
481 | if (state >= 0) | |
482 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state); | |
483 | ||
41973a47 RP |
484 | if (neigh_suppress != -1) |
485 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_NEIGH_SUPPRESS, | |
486 | neigh_suppress); | |
8cfde5c9 RP |
487 | if (vlan_tunnel != -1) |
488 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_VLAN_TUNNEL, | |
489 | vlan_tunnel); | |
05001bcf NA |
490 | if (isolated != -1) |
491 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_ISOLATED, isolated); | |
41973a47 | 492 | |
d13d52d0 NA |
493 | if (backup_port_idx != -1) |
494 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_BACKUP_PORT, | |
495 | backup_port_idx); | |
496 | ||
64108901 VY |
497 | addattr_nest_end(&req.n, nest); |
498 | ||
6fdb4658 RP |
499 | /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that |
500 | * designates master or self operation and IFLA_BRIDGE_MODE | |
501 | * for hw 'vepa' or 'veb' operation modes. The hwmodes are | |
502 | * only valid in 'self' mode on some devices so far. | |
64108901 | 503 | */ |
6fdb4658 | 504 | if (mode >= 0 || flags > 0) { |
64108901 VY |
505 | nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC); |
506 | ||
6fdb4658 RP |
507 | if (flags > 0) |
508 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags); | |
64108901 VY |
509 | |
510 | if (mode >= 0) | |
511 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode); | |
512 | ||
513 | addattr_nest_end(&req.n, nest); | |
514 | } | |
515 | ||
86bf43c7 | 516 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
42ecedd4 | 517 | return -1; |
64108901 VY |
518 | |
519 | return 0; | |
520 | } | |
521 | ||
522 | static int brlink_show(int argc, char **argv) | |
523 | { | |
524 | char *filter_dev = NULL; | |
525 | ||
526 | while (argc > 0) { | |
527 | if (strcmp(*argv, "dev") == 0) { | |
528 | NEXT_ARG(); | |
529 | if (filter_dev) | |
530 | duparg("dev", *argv); | |
531 | filter_dev = *argv; | |
532 | } | |
533 | argc--; argv++; | |
534 | } | |
535 | ||
536 | if (filter_dev) { | |
fe99adbc SP |
537 | filter_index = ll_name_to_index(filter_dev); |
538 | if (!filter_index) | |
539 | return nodev(filter_dev); | |
64108901 VY |
540 | } |
541 | ||
fab9a18a | 542 | if (show_details) { |
31ae2912 | 543 | if (rtnl_linkdump_req_filter(&rth, PF_BRIDGE, |
fab9a18a RM |
544 | (compress_vlans ? |
545 | RTEXT_FILTER_BRVLAN_COMPRESSED : | |
546 | RTEXT_FILTER_BRVLAN)) < 0) { | |
547 | perror("Cannon send dump request"); | |
548 | exit(1); | |
549 | } | |
550 | } else { | |
31ae2912 | 551 | if (rtnl_linkdump_req(&rth, PF_BRIDGE) < 0) { |
fab9a18a RM |
552 | perror("Cannon send dump request"); |
553 | exit(1); | |
554 | } | |
64108901 VY |
555 | } |
556 | ||
f32e4977 | 557 | new_json_obj(json); |
64108901 VY |
558 | if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) { |
559 | fprintf(stderr, "Dump terminated\n"); | |
560 | exit(1); | |
561 | } | |
f32e4977 SH |
562 | |
563 | delete_json_obj(); | |
564 | fflush(stdout); | |
64108901 VY |
565 | return 0; |
566 | } | |
567 | ||
568 | int do_link(int argc, char **argv) | |
569 | { | |
570 | ll_init_map(&rth); | |
571 | if (argc > 0) { | |
572 | if (matches(*argv, "set") == 0 || | |
573 | matches(*argv, "change") == 0) | |
574 | return brlink_modify(argc-1, argv+1); | |
575 | if (matches(*argv, "show") == 0 || | |
576 | matches(*argv, "lst") == 0 || | |
577 | matches(*argv, "list") == 0) | |
578 | return brlink_show(argc-1, argv+1); | |
579 | if (matches(*argv, "help") == 0) | |
580 | usage(); | |
581 | } else | |
582 | return brlink_show(0, NULL); | |
583 | ||
584 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv); | |
585 | exit(-1); | |
586 | } |