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