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
print_explain(FILE * f)22 static void print_explain(FILE *f)
23 {
24 fprintf(f,
25 "Usage: ... bridge_slave [ fdb_flush ]\n"
26 " [ state STATE ]\n"
27 " [ priority PRIO ]\n"
28 " [ cost COST ]\n"
29 " [ guard {on | off} ]\n"
30 " [ hairpin {on | off} ]\n"
31 " [ fastleave {on | off} ]\n"
32 " [ root_block {on | off} ]\n"
33 " [ learning {on | off} ]\n"
34 " [ flood {on | off} ]\n"
35 " [ proxy_arp {on | off} ]\n"
36 " [ proxy_arp_wifi {on | off} ]\n"
37 " [ mcast_router MULTICAST_ROUTER ]\n"
38 " [ mcast_fast_leave {on | off} ]\n"
39 " [ mcast_flood {on | off} ]\n"
40 );
41 }
42
explain(void)43 static void explain(void)
44 {
45 print_explain(stderr);
46 }
47
48 static const char *port_states[] = {
49 [BR_STATE_DISABLED] = "disabled",
50 [BR_STATE_LISTENING] = "listening",
51 [BR_STATE_LEARNING] = "learning",
52 [BR_STATE_FORWARDING] = "forwarding",
53 [BR_STATE_BLOCKING] = "blocking",
54 };
55
print_portstate(FILE * f,__u8 state)56 static void print_portstate(FILE *f, __u8 state)
57 {
58 if (state <= BR_STATE_BLOCKING)
59 print_string(PRINT_ANY,
60 "state",
61 "state %s ",
62 port_states[state]);
63 else
64 print_int(PRINT_ANY, "state_index", "state (%d) ", state);
65 }
66
_print_onoff(FILE * f,char * json_flag,char * flag,__u8 val)67 static void _print_onoff(FILE *f, char *json_flag, char *flag, __u8 val)
68 {
69 if (is_json_context())
70 print_bool(PRINT_JSON, flag, NULL, val);
71 else
72 fprintf(f, "%s %s ", flag, val ? "on" : "off");
73 }
74
_print_hex(FILE * f,const char * json_attr,const char * attr,__u16 val)75 static void _print_hex(FILE *f,
76 const char *json_attr,
77 const char *attr,
78 __u16 val)
79 {
80 if (is_json_context()) {
81 SPRINT_BUF(b1);
82
83 snprintf(b1, sizeof(b1), "0x%x", val);
84 print_string(PRINT_JSON, json_attr, NULL, b1);
85 } else {
86 fprintf(f, "%s 0x%x ", attr, val);
87 }
88 }
89
_print_timer(FILE * f,const char * attr,struct rtattr * timer)90 static void _print_timer(FILE *f, const char *attr, struct rtattr *timer)
91 {
92 struct timeval tv;
93
94 __jiffies_to_tv(&tv, rta_getattr_u64(timer));
95 if (is_json_context()) {
96 json_writer_t *jw = get_json_writer();
97
98 jsonw_name(jw, attr);
99 jsonw_printf(jw, "%i.%.2i",
100 (int)tv.tv_sec, (int)tv.tv_usec / 10000);
101 } else {
102 fprintf(f, "%s %4i.%.2i ", attr, (int)tv.tv_sec,
103 (int)tv.tv_usec / 10000);
104 }
105 }
106
bridge_slave_print_opt(struct link_util * lu,FILE * f,struct rtattr * tb[])107 static void bridge_slave_print_opt(struct link_util *lu, FILE *f,
108 struct rtattr *tb[])
109 {
110 if (!tb)
111 return;
112
113 if (tb[IFLA_BRPORT_STATE])
114 print_portstate(f, rta_getattr_u8(tb[IFLA_BRPORT_STATE]));
115
116 if (tb[IFLA_BRPORT_PRIORITY])
117 print_int(PRINT_ANY,
118 "priority",
119 "priority %d ",
120 rta_getattr_u16(tb[IFLA_BRPORT_PRIORITY]));
121
122 if (tb[IFLA_BRPORT_COST])
123 print_int(PRINT_ANY,
124 "cost",
125 "cost %d ",
126 rta_getattr_u32(tb[IFLA_BRPORT_COST]));
127
128 if (tb[IFLA_BRPORT_MODE])
129 _print_onoff(f, "mode", "hairpin",
130 rta_getattr_u8(tb[IFLA_BRPORT_MODE]));
131
132 if (tb[IFLA_BRPORT_GUARD])
133 _print_onoff(f, "guard", "guard",
134 rta_getattr_u8(tb[IFLA_BRPORT_GUARD]));
135
136 if (tb[IFLA_BRPORT_PROTECT])
137 _print_onoff(f, "protect", "root_block",
138 rta_getattr_u8(tb[IFLA_BRPORT_PROTECT]));
139
140 if (tb[IFLA_BRPORT_FAST_LEAVE])
141 _print_onoff(f, "fast_leave", "fastleave",
142 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]));
143
144 if (tb[IFLA_BRPORT_LEARNING])
145 _print_onoff(f, "learning", "learning",
146 rta_getattr_u8(tb[IFLA_BRPORT_LEARNING]));
147
148 if (tb[IFLA_BRPORT_UNICAST_FLOOD])
149 _print_onoff(f, "unicast_flood", "flood",
150 rta_getattr_u8(tb[IFLA_BRPORT_UNICAST_FLOOD]));
151
152 if (tb[IFLA_BRPORT_ID])
153 _print_hex(f, "id", "port_id",
154 rta_getattr_u16(tb[IFLA_BRPORT_ID]));
155
156 if (tb[IFLA_BRPORT_NO])
157 _print_hex(f, "no", "port_no",
158 rta_getattr_u16(tb[IFLA_BRPORT_NO]));
159
160 if (tb[IFLA_BRPORT_DESIGNATED_PORT])
161 print_uint(PRINT_ANY,
162 "designated_port",
163 "designated_port %u ",
164 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_PORT]));
165
166 if (tb[IFLA_BRPORT_DESIGNATED_COST])
167 print_uint(PRINT_ANY,
168 "designated_cost",
169 "designated_cost %u ",
170 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_COST]));
171
172 if (tb[IFLA_BRPORT_BRIDGE_ID]) {
173 char bridge_id[32];
174
175 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_BRIDGE_ID]),
176 bridge_id, sizeof(bridge_id));
177 print_string(PRINT_ANY,
178 "bridge_id",
179 "designated_bridge %s ",
180 bridge_id);
181 }
182
183 if (tb[IFLA_BRPORT_ROOT_ID]) {
184 char root_id[32];
185
186 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_ROOT_ID]),
187 root_id, sizeof(root_id));
188 print_string(PRINT_ANY,
189 "root_id",
190 "designated_root %s ", root_id);
191 }
192
193 if (tb[IFLA_BRPORT_HOLD_TIMER])
194 _print_timer(f, "hold_timer", tb[IFLA_BRPORT_HOLD_TIMER]);
195
196 if (tb[IFLA_BRPORT_MESSAGE_AGE_TIMER])
197 _print_timer(f, "message_age_timer",
198 tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]);
199
200 if (tb[IFLA_BRPORT_FORWARD_DELAY_TIMER])
201 _print_timer(f, "forward_delay_timer",
202 tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]);
203
204 if (tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK])
205 print_uint(PRINT_ANY,
206 "topology_change_ack",
207 "topology_change_ack %u ",
208 rta_getattr_u8(tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK]));
209
210 if (tb[IFLA_BRPORT_CONFIG_PENDING])
211 print_uint(PRINT_ANY,
212 "config_pending",
213 "config_pending %u ",
214 rta_getattr_u8(tb[IFLA_BRPORT_CONFIG_PENDING]));
215
216 if (tb[IFLA_BRPORT_PROXYARP])
217 _print_onoff(f, "proxyarp", "proxy_arp",
218 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP]));
219
220 if (tb[IFLA_BRPORT_PROXYARP_WIFI])
221 _print_onoff(f, "proxyarp_wifi", "proxy_arp_wifi",
222 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP_WIFI]));
223
224 if (tb[IFLA_BRPORT_MULTICAST_ROUTER])
225 print_uint(PRINT_ANY,
226 "multicast_router",
227 "mcast_router %u ",
228 rta_getattr_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]));
229
230 if (tb[IFLA_BRPORT_FAST_LEAVE])
231 // not printing any json here because
232 // we already printed fast_leave before
233 print_string(PRINT_FP,
234 NULL,
235 "mcast_fast_leave %s ",
236 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]) ? "on" : "off");
237
238 if (tb[IFLA_BRPORT_MCAST_FLOOD])
239 _print_onoff(f, "mcast_flood", "mcast_flood",
240 rta_getattr_u8(tb[IFLA_BRPORT_MCAST_FLOOD]));
241 }
242
bridge_slave_parse_on_off(char * arg_name,char * arg_val,struct nlmsghdr * n,int type)243 static void bridge_slave_parse_on_off(char *arg_name, char *arg_val,
244 struct nlmsghdr *n, int type)
245 {
246 __u8 val;
247
248 if (strcmp(arg_val, "on") == 0)
249 val = 1;
250 else if (strcmp(arg_val, "off") == 0)
251 val = 0;
252 else
253 invarg("should be \"on\" or \"off\"", arg_name);
254
255 addattr8(n, 1024, type, val);
256 }
257
bridge_slave_parse_opt(struct link_util * lu,int argc,char ** argv,struct nlmsghdr * n)258 static int bridge_slave_parse_opt(struct link_util *lu, int argc, char **argv,
259 struct nlmsghdr *n)
260 {
261 __u8 state;
262 __u16 priority;
263 __u32 cost;
264
265 while (argc > 0) {
266 if (matches(*argv, "fdb_flush") == 0) {
267 addattr(n, 1024, IFLA_BRPORT_FLUSH);
268 } else if (matches(*argv, "state") == 0) {
269 NEXT_ARG();
270 if (get_u8(&state, *argv, 0))
271 invarg("state is invalid", *argv);
272 addattr8(n, 1024, IFLA_BRPORT_STATE, state);
273 } else if (matches(*argv, "priority") == 0) {
274 NEXT_ARG();
275 if (get_u16(&priority, *argv, 0))
276 invarg("priority is invalid", *argv);
277 addattr16(n, 1024, IFLA_BRPORT_PRIORITY, priority);
278 } else if (matches(*argv, "cost") == 0) {
279 NEXT_ARG();
280 if (get_u32(&cost, *argv, 0))
281 invarg("cost is invalid", *argv);
282 addattr32(n, 1024, IFLA_BRPORT_COST, cost);
283 } else if (matches(*argv, "hairpin") == 0) {
284 NEXT_ARG();
285 bridge_slave_parse_on_off("hairpin", *argv, n,
286 IFLA_BRPORT_MODE);
287 } else if (matches(*argv, "guard") == 0) {
288 NEXT_ARG();
289 bridge_slave_parse_on_off("guard", *argv, n,
290 IFLA_BRPORT_GUARD);
291 } else if (matches(*argv, "root_block") == 0) {
292 NEXT_ARG();
293 bridge_slave_parse_on_off("root_block", *argv, n,
294 IFLA_BRPORT_PROTECT);
295 } else if (matches(*argv, "fastleave") == 0) {
296 NEXT_ARG();
297 bridge_slave_parse_on_off("fastleave", *argv, n,
298 IFLA_BRPORT_FAST_LEAVE);
299 } else if (matches(*argv, "learning") == 0) {
300 NEXT_ARG();
301 bridge_slave_parse_on_off("learning", *argv, n,
302 IFLA_BRPORT_LEARNING);
303 } else if (matches(*argv, "flood") == 0) {
304 NEXT_ARG();
305 bridge_slave_parse_on_off("flood", *argv, n,
306 IFLA_BRPORT_UNICAST_FLOOD);
307 } else if (matches(*argv, "mcast_flood") == 0) {
308 NEXT_ARG();
309 bridge_slave_parse_on_off("mcast_flood", *argv, n,
310 IFLA_BRPORT_MCAST_FLOOD);
311 } else if (matches(*argv, "proxy_arp") == 0) {
312 NEXT_ARG();
313 bridge_slave_parse_on_off("proxy_arp", *argv, n,
314 IFLA_BRPORT_PROXYARP);
315 } else if (matches(*argv, "proxy_arp_wifi") == 0) {
316 NEXT_ARG();
317 bridge_slave_parse_on_off("proxy_arp_wifi", *argv, n,
318 IFLA_BRPORT_PROXYARP_WIFI);
319 } else if (matches(*argv, "mcast_router") == 0) {
320 __u8 mcast_router;
321
322 NEXT_ARG();
323 if (get_u8(&mcast_router, *argv, 0))
324 invarg("invalid mcast_router", *argv);
325 addattr8(n, 1024, IFLA_BRPORT_MULTICAST_ROUTER,
326 mcast_router);
327 } else if (matches(*argv, "mcast_fast_leave") == 0) {
328 NEXT_ARG();
329 bridge_slave_parse_on_off("mcast_fast_leave", *argv, n,
330 IFLA_BRPORT_FAST_LEAVE);
331 } else if (matches(*argv, "help") == 0) {
332 explain();
333 return -1;
334 } else {
335 fprintf(stderr, "bridge_slave: unknown option \"%s\"?\n",
336 *argv);
337 explain();
338 return -1;
339 }
340 argc--, argv++;
341 }
342
343 return 0;
344 }
345
bridge_slave_print_help(struct link_util * lu,int argc,char ** argv,FILE * f)346 static void bridge_slave_print_help(struct link_util *lu, int argc, char **argv,
347 FILE *f)
348 {
349 print_explain(f);
350 }
351
352 struct link_util bridge_slave_link_util = {
353 .id = "bridge_slave",
354 .maxattr = IFLA_BRPORT_MAX,
355 .print_opt = bridge_slave_print_opt,
356 .parse_opt = bridge_slave_parse_opt,
357 .print_help = bridge_slave_print_help,
358 .parse_ifla_xstats = bridge_parse_xstats,
359 .print_ifla_xstats = bridge_print_xstats,
360 };
361