]>
Commit | Line | Data |
---|---|---|
8c39db39 JP |
1 | /* |
2 | * iplink_bridge_slave.c Bridge slave device support | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@resnulli.us> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <sys/socket.h> | |
14 | #include <netinet/in.h> | |
15 | #include <linux/if_link.h> | |
16 | #include <linux/if_bridge.h> | |
17 | ||
18 | #include "rt_names.h" | |
19 | #include "utils.h" | |
20 | #include "ip_common.h" | |
21 | ||
a560d850 | 22 | static void print_explain(FILE *f) |
8c39db39 | 23 | { |
a560d850 | 24 | fprintf(f, |
d1b41236 HL |
25 | "Usage: ... bridge_slave [ fdb_flush ]\n" |
26 | " [ state STATE ]\n" | |
27 | " [ priority PRIO ]\n" | |
28 | " [ cost COST ]\n" | |
8c39db39 | 29 | " [ guard {on | off} ]\n" |
56f5daac | 30 | " [ hairpin {on | off} ]\n" |
8c39db39 JP |
31 | " [ fastleave {on | off} ]\n" |
32 | " [ root_block {on | off} ]\n" | |
33 | " [ learning {on | off} ]\n" | |
34 | " [ flood {on | off} ]\n" | |
f6e615de | 35 | " [ proxy_arp {on | off} ]\n" |
38b31a78 | 36 | " [ proxy_arp_wifi {on | off} ]\n" |
10759a90 | 37 | " [ mcast_router MULTICAST_ROUTER ]\n" |
478a8e59 | 38 | " [ mcast_fast_leave {on | off} ]\n" |
9208b4e7 | 39 | " [ mcast_flood {on | off} ]\n" |
fdbdd356 | 40 | " [ group_fwd_mask MASK ]\n" |
8c39db39 JP |
41 | ); |
42 | } | |
43 | ||
a560d850 ZS |
44 | static void explain(void) |
45 | { | |
46 | print_explain(stderr); | |
47 | } | |
48 | ||
8c39db39 JP |
49 | static const char *port_states[] = { |
50 | [BR_STATE_DISABLED] = "disabled", | |
51 | [BR_STATE_LISTENING] = "listening", | |
52 | [BR_STATE_LEARNING] = "learning", | |
53 | [BR_STATE_FORWARDING] = "forwarding", | |
54 | [BR_STATE_BLOCKING] = "blocking", | |
55 | }; | |
56 | ||
fdbdd356 NA |
57 | static const char *fwd_mask_tbl[16] = { |
58 | [0] = "stp", | |
59 | [2] = "lacp", | |
60 | [14] = "lldp" | |
61 | }; | |
62 | ||
8c39db39 JP |
63 | static void print_portstate(FILE *f, __u8 state) |
64 | { | |
65 | if (state <= BR_STATE_BLOCKING) | |
165a7039 JF |
66 | print_string(PRINT_ANY, |
67 | "state", | |
68 | "state %s ", | |
69 | port_states[state]); | |
8c39db39 | 70 | else |
165a7039 | 71 | print_int(PRINT_ANY, "state_index", "state (%d) ", state); |
8c39db39 JP |
72 | } |
73 | ||
165a7039 | 74 | static void _print_onoff(FILE *f, char *json_flag, char *flag, __u8 val) |
8c39db39 | 75 | { |
165a7039 JF |
76 | if (is_json_context()) |
77 | print_bool(PRINT_JSON, flag, NULL, val); | |
78 | else | |
79 | fprintf(f, "%s %s ", flag, val ? "on" : "off"); | |
80 | } | |
81 | ||
82 | static void _print_hex(FILE *f, | |
83 | const char *json_attr, | |
84 | const char *attr, | |
85 | __u16 val) | |
86 | { | |
87 | if (is_json_context()) { | |
88 | SPRINT_BUF(b1); | |
89 | ||
90 | snprintf(b1, sizeof(b1), "0x%x", val); | |
91 | print_string(PRINT_JSON, json_attr, NULL, b1); | |
92 | } else { | |
93 | fprintf(f, "%s 0x%x ", attr, val); | |
94 | } | |
95 | } | |
96 | ||
97 | static void _print_timer(FILE *f, const char *attr, struct rtattr *timer) | |
98 | { | |
99 | struct timeval tv; | |
100 | ||
101 | __jiffies_to_tv(&tv, rta_getattr_u64(timer)); | |
102 | if (is_json_context()) { | |
103 | json_writer_t *jw = get_json_writer(); | |
104 | ||
105 | jsonw_name(jw, attr); | |
106 | jsonw_printf(jw, "%i.%.2i", | |
107 | (int)tv.tv_sec, (int)tv.tv_usec / 10000); | |
108 | } else { | |
109 | fprintf(f, "%s %4i.%.2i ", attr, (int)tv.tv_sec, | |
110 | (int)tv.tv_usec / 10000); | |
111 | } | |
8c39db39 JP |
112 | } |
113 | ||
fdbdd356 NA |
114 | static void _bitmask2str(__u16 bitmask, char *dst, size_t dst_size, |
115 | const char **tbl) | |
116 | { | |
117 | int len, i; | |
118 | ||
119 | for (i = 0, len = 0; bitmask; i++, bitmask >>= 1) { | |
120 | if (bitmask & 0x1) { | |
121 | if (tbl[i]) | |
122 | len += snprintf(dst + len, dst_size - len, "%s,", | |
123 | tbl[i]); | |
124 | else | |
125 | len += snprintf(dst + len, dst_size - len, "0x%x,", | |
126 | (1 << i)); | |
127 | } | |
128 | } | |
129 | ||
130 | if (!len) | |
131 | snprintf(dst, dst_size, "0x0"); | |
132 | else | |
133 | dst[len - 1] = 0; | |
134 | } | |
135 | ||
8c39db39 JP |
136 | static void bridge_slave_print_opt(struct link_util *lu, FILE *f, |
137 | struct rtattr *tb[]) | |
138 | { | |
139 | if (!tb) | |
140 | return; | |
141 | ||
142 | if (tb[IFLA_BRPORT_STATE]) | |
143 | print_portstate(f, rta_getattr_u8(tb[IFLA_BRPORT_STATE])); | |
144 | ||
145 | if (tb[IFLA_BRPORT_PRIORITY]) | |
165a7039 JF |
146 | print_int(PRINT_ANY, |
147 | "priority", | |
148 | "priority %d ", | |
149 | rta_getattr_u16(tb[IFLA_BRPORT_PRIORITY])); | |
8c39db39 JP |
150 | |
151 | if (tb[IFLA_BRPORT_COST]) | |
165a7039 JF |
152 | print_int(PRINT_ANY, |
153 | "cost", | |
154 | "cost %d ", | |
155 | rta_getattr_u32(tb[IFLA_BRPORT_COST])); | |
8c39db39 JP |
156 | |
157 | if (tb[IFLA_BRPORT_MODE]) | |
165a7039 JF |
158 | _print_onoff(f, "mode", "hairpin", |
159 | rta_getattr_u8(tb[IFLA_BRPORT_MODE])); | |
8c39db39 JP |
160 | |
161 | if (tb[IFLA_BRPORT_GUARD]) | |
165a7039 JF |
162 | _print_onoff(f, "guard", "guard", |
163 | rta_getattr_u8(tb[IFLA_BRPORT_GUARD])); | |
8c39db39 JP |
164 | |
165 | if (tb[IFLA_BRPORT_PROTECT]) | |
165a7039 JF |
166 | _print_onoff(f, "protect", "root_block", |
167 | rta_getattr_u8(tb[IFLA_BRPORT_PROTECT])); | |
8c39db39 JP |
168 | |
169 | if (tb[IFLA_BRPORT_FAST_LEAVE]) | |
165a7039 JF |
170 | _print_onoff(f, "fast_leave", "fastleave", |
171 | rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE])); | |
8c39db39 JP |
172 | |
173 | if (tb[IFLA_BRPORT_LEARNING]) | |
165a7039 JF |
174 | _print_onoff(f, "learning", "learning", |
175 | rta_getattr_u8(tb[IFLA_BRPORT_LEARNING])); | |
8c39db39 JP |
176 | |
177 | if (tb[IFLA_BRPORT_UNICAST_FLOOD]) | |
165a7039 JF |
178 | _print_onoff(f, "unicast_flood", "flood", |
179 | rta_getattr_u8(tb[IFLA_BRPORT_UNICAST_FLOOD])); | |
3069539f NA |
180 | |
181 | if (tb[IFLA_BRPORT_ID]) | |
165a7039 JF |
182 | _print_hex(f, "id", "port_id", |
183 | rta_getattr_u16(tb[IFLA_BRPORT_ID])); | |
3069539f NA |
184 | |
185 | if (tb[IFLA_BRPORT_NO]) | |
165a7039 JF |
186 | _print_hex(f, "no", "port_no", |
187 | rta_getattr_u16(tb[IFLA_BRPORT_NO])); | |
3069539f NA |
188 | |
189 | if (tb[IFLA_BRPORT_DESIGNATED_PORT]) | |
165a7039 JF |
190 | print_uint(PRINT_ANY, |
191 | "designated_port", | |
192 | "designated_port %u ", | |
193 | rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_PORT])); | |
3069539f NA |
194 | |
195 | if (tb[IFLA_BRPORT_DESIGNATED_COST]) | |
165a7039 JF |
196 | print_uint(PRINT_ANY, |
197 | "designated_cost", | |
198 | "designated_cost %u ", | |
199 | rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_COST])); | |
3069539f NA |
200 | |
201 | if (tb[IFLA_BRPORT_BRIDGE_ID]) { | |
202 | char bridge_id[32]; | |
203 | ||
204 | br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_BRIDGE_ID]), | |
205 | bridge_id, sizeof(bridge_id)); | |
165a7039 JF |
206 | print_string(PRINT_ANY, |
207 | "bridge_id", | |
208 | "designated_bridge %s ", | |
209 | bridge_id); | |
3069539f NA |
210 | } |
211 | ||
212 | if (tb[IFLA_BRPORT_ROOT_ID]) { | |
213 | char root_id[32]; | |
214 | ||
215 | br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_ROOT_ID]), | |
216 | root_id, sizeof(root_id)); | |
165a7039 JF |
217 | print_string(PRINT_ANY, |
218 | "root_id", | |
219 | "designated_root %s ", root_id); | |
3069539f NA |
220 | } |
221 | ||
165a7039 JF |
222 | if (tb[IFLA_BRPORT_HOLD_TIMER]) |
223 | _print_timer(f, "hold_timer", tb[IFLA_BRPORT_HOLD_TIMER]); | |
3069539f | 224 | |
165a7039 JF |
225 | if (tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]) |
226 | _print_timer(f, "message_age_timer", | |
227 | tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]); | |
3069539f | 228 | |
165a7039 JF |
229 | if (tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]) |
230 | _print_timer(f, "forward_delay_timer", | |
231 | tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]); | |
3069539f NA |
232 | |
233 | if (tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK]) | |
165a7039 JF |
234 | print_uint(PRINT_ANY, |
235 | "topology_change_ack", | |
236 | "topology_change_ack %u ", | |
237 | rta_getattr_u8(tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK])); | |
3069539f NA |
238 | |
239 | if (tb[IFLA_BRPORT_CONFIG_PENDING]) | |
165a7039 JF |
240 | print_uint(PRINT_ANY, |
241 | "config_pending", | |
242 | "config_pending %u ", | |
243 | rta_getattr_u8(tb[IFLA_BRPORT_CONFIG_PENDING])); | |
244 | ||
f6e615de | 245 | if (tb[IFLA_BRPORT_PROXYARP]) |
165a7039 JF |
246 | _print_onoff(f, "proxyarp", "proxy_arp", |
247 | rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP])); | |
38b31a78 NA |
248 | |
249 | if (tb[IFLA_BRPORT_PROXYARP_WIFI]) | |
165a7039 JF |
250 | _print_onoff(f, "proxyarp_wifi", "proxy_arp_wifi", |
251 | rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP_WIFI])); | |
10759a90 NA |
252 | |
253 | if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) | |
165a7039 JF |
254 | print_uint(PRINT_ANY, |
255 | "multicast_router", | |
256 | "mcast_router %u ", | |
257 | rta_getattr_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER])); | |
478a8e59 NA |
258 | |
259 | if (tb[IFLA_BRPORT_FAST_LEAVE]) | |
165a7039 JF |
260 | // not printing any json here because |
261 | // we already printed fast_leave before | |
262 | print_string(PRINT_FP, | |
263 | NULL, | |
264 | "mcast_fast_leave %s ", | |
265 | rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]) ? "on" : "off"); | |
9208b4e7 NA |
266 | |
267 | if (tb[IFLA_BRPORT_MCAST_FLOOD]) | |
165a7039 JF |
268 | _print_onoff(f, "mcast_flood", "mcast_flood", |
269 | rta_getattr_u8(tb[IFLA_BRPORT_MCAST_FLOOD])); | |
41973a47 RP |
270 | |
271 | if (tb[IFLA_BRPORT_NEIGH_SUPPRESS]) | |
272 | _print_onoff(f, "neigh_suppress", "neigh_suppress", | |
273 | rta_getattr_u8(tb[IFLA_BRPORT_NEIGH_SUPPRESS])); | |
fdbdd356 NA |
274 | |
275 | if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) { | |
276 | char convbuf[256]; | |
277 | __u16 fwd_mask; | |
278 | ||
279 | fwd_mask = rta_getattr_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]); | |
280 | _print_hex(f, "group_fwd_mask", "group_fwd_mask", fwd_mask); | |
281 | _bitmask2str(fwd_mask, convbuf, sizeof(convbuf), fwd_mask_tbl); | |
282 | print_string(PRINT_ANY, "group_fwd_mask_str", | |
283 | "group_fwd_mask_str %s ", convbuf); | |
284 | } | |
8c39db39 JP |
285 | } |
286 | ||
287 | static void bridge_slave_parse_on_off(char *arg_name, char *arg_val, | |
288 | struct nlmsghdr *n, int type) | |
289 | { | |
290 | __u8 val; | |
291 | ||
292 | if (strcmp(arg_val, "on") == 0) | |
293 | val = 1; | |
294 | else if (strcmp(arg_val, "off") == 0) | |
295 | val = 0; | |
296 | else | |
297 | invarg("should be \"on\" or \"off\"", arg_name); | |
298 | ||
299 | addattr8(n, 1024, type, val); | |
300 | } | |
301 | ||
302 | static int bridge_slave_parse_opt(struct link_util *lu, int argc, char **argv, | |
303 | struct nlmsghdr *n) | |
304 | { | |
305 | __u8 state; | |
306 | __u16 priority; | |
307 | __u32 cost; | |
308 | ||
309 | while (argc > 0) { | |
d1b41236 HL |
310 | if (matches(*argv, "fdb_flush") == 0) { |
311 | addattr(n, 1024, IFLA_BRPORT_FLUSH); | |
312 | } else if (matches(*argv, "state") == 0) { | |
8c39db39 JP |
313 | NEXT_ARG(); |
314 | if (get_u8(&state, *argv, 0)) | |
315 | invarg("state is invalid", *argv); | |
316 | addattr8(n, 1024, IFLA_BRPORT_STATE, state); | |
317 | } else if (matches(*argv, "priority") == 0) { | |
318 | NEXT_ARG(); | |
319 | if (get_u16(&priority, *argv, 0)) | |
320 | invarg("priority is invalid", *argv); | |
321 | addattr16(n, 1024, IFLA_BRPORT_PRIORITY, priority); | |
322 | } else if (matches(*argv, "cost") == 0) { | |
323 | NEXT_ARG(); | |
324 | if (get_u32(&cost, *argv, 0)) | |
325 | invarg("cost is invalid", *argv); | |
326 | addattr32(n, 1024, IFLA_BRPORT_COST, cost); | |
327 | } else if (matches(*argv, "hairpin") == 0) { | |
328 | NEXT_ARG(); | |
329 | bridge_slave_parse_on_off("hairpin", *argv, n, | |
330 | IFLA_BRPORT_MODE); | |
331 | } else if (matches(*argv, "guard") == 0) { | |
332 | NEXT_ARG(); | |
333 | bridge_slave_parse_on_off("guard", *argv, n, | |
334 | IFLA_BRPORT_GUARD); | |
335 | } else if (matches(*argv, "root_block") == 0) { | |
336 | NEXT_ARG(); | |
337 | bridge_slave_parse_on_off("root_block", *argv, n, | |
338 | IFLA_BRPORT_PROTECT); | |
339 | } else if (matches(*argv, "fastleave") == 0) { | |
340 | NEXT_ARG(); | |
341 | bridge_slave_parse_on_off("fastleave", *argv, n, | |
342 | IFLA_BRPORT_FAST_LEAVE); | |
343 | } else if (matches(*argv, "learning") == 0) { | |
344 | NEXT_ARG(); | |
345 | bridge_slave_parse_on_off("learning", *argv, n, | |
346 | IFLA_BRPORT_LEARNING); | |
347 | } else if (matches(*argv, "flood") == 0) { | |
348 | NEXT_ARG(); | |
349 | bridge_slave_parse_on_off("flood", *argv, n, | |
350 | IFLA_BRPORT_UNICAST_FLOOD); | |
9208b4e7 NA |
351 | } else if (matches(*argv, "mcast_flood") == 0) { |
352 | NEXT_ARG(); | |
353 | bridge_slave_parse_on_off("mcast_flood", *argv, n, | |
354 | IFLA_BRPORT_MCAST_FLOOD); | |
f6e615de NA |
355 | } else if (matches(*argv, "proxy_arp") == 0) { |
356 | NEXT_ARG(); | |
357 | bridge_slave_parse_on_off("proxy_arp", *argv, n, | |
358 | IFLA_BRPORT_PROXYARP); | |
38b31a78 NA |
359 | } else if (matches(*argv, "proxy_arp_wifi") == 0) { |
360 | NEXT_ARG(); | |
361 | bridge_slave_parse_on_off("proxy_arp_wifi", *argv, n, | |
362 | IFLA_BRPORT_PROXYARP_WIFI); | |
10759a90 NA |
363 | } else if (matches(*argv, "mcast_router") == 0) { |
364 | __u8 mcast_router; | |
365 | ||
366 | NEXT_ARG(); | |
367 | if (get_u8(&mcast_router, *argv, 0)) | |
368 | invarg("invalid mcast_router", *argv); | |
369 | addattr8(n, 1024, IFLA_BRPORT_MULTICAST_ROUTER, | |
370 | mcast_router); | |
478a8e59 NA |
371 | } else if (matches(*argv, "mcast_fast_leave") == 0) { |
372 | NEXT_ARG(); | |
373 | bridge_slave_parse_on_off("mcast_fast_leave", *argv, n, | |
374 | IFLA_BRPORT_FAST_LEAVE); | |
41973a47 RP |
375 | } else if (matches(*argv, "neigh_suppress") == 0) { |
376 | NEXT_ARG(); | |
377 | bridge_slave_parse_on_off("neigh_suppress", *argv, n, | |
378 | IFLA_BRPORT_NEIGH_SUPPRESS); | |
fdbdd356 NA |
379 | } else if (matches(*argv, "group_fwd_mask") == 0) { |
380 | __u16 mask; | |
381 | ||
382 | NEXT_ARG(); | |
383 | if (get_u16(&mask, *argv, 0)) | |
384 | invarg("invalid group_fwd_mask", *argv); | |
385 | addattr16(n, 1024, IFLA_BRPORT_GROUP_FWD_MASK, mask); | |
8c39db39 JP |
386 | } else if (matches(*argv, "help") == 0) { |
387 | explain(); | |
388 | return -1; | |
389 | } else { | |
390 | fprintf(stderr, "bridge_slave: unknown option \"%s\"?\n", | |
391 | *argv); | |
392 | explain(); | |
393 | return -1; | |
394 | } | |
395 | argc--, argv++; | |
396 | } | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
a560d850 ZS |
401 | static void bridge_slave_print_help(struct link_util *lu, int argc, char **argv, |
402 | FILE *f) | |
403 | { | |
404 | print_explain(f); | |
405 | } | |
406 | ||
8c39db39 | 407 | struct link_util bridge_slave_link_util = { |
22a84711 | 408 | .id = "bridge_slave", |
8c39db39 JP |
409 | .maxattr = IFLA_BRPORT_MAX, |
410 | .print_opt = bridge_slave_print_opt, | |
411 | .parse_opt = bridge_slave_parse_opt, | |
a560d850 | 412 | .print_help = bridge_slave_print_help, |
217264a0 NA |
413 | .parse_ifla_xstats = bridge_parse_xstats, |
414 | .print_ifla_xstats = bridge_print_xstats, | |
8c39db39 | 415 | }; |