]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/iplink_bridge_slave.c
bridge: add support for the multicast flood flag
[mirror_iproute2.git] / ip / iplink_bridge_slave.c
CommitLineData
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 22static void print_explain(FILE *f)
8c39db39 23{
a560d850 24 fprintf(f,
8c39db39
JP
25 "Usage: ... bridge_slave [ state STATE ] [ priority PRIO ] [cost COST ]\n"
26 " [ guard {on | off} ]\n"
56f5daac 27 " [ hairpin {on | off} ]\n"
8c39db39
JP
28 " [ fastleave {on | off} ]\n"
29 " [ root_block {on | off} ]\n"
30 " [ learning {on | off} ]\n"
31 " [ flood {on | off} ]\n"
f6e615de 32 " [ proxy_arp {on | off} ]\n"
38b31a78 33 " [ proxy_arp_wifi {on | off} ]\n"
10759a90 34 " [ mcast_router MULTICAST_ROUTER ]\n"
478a8e59 35 " [ mcast_fast_leave {on | off} ]\n"
9208b4e7 36 " [ mcast_flood {on | off} ]\n"
8c39db39
JP
37 );
38}
39
a560d850
ZS
40static void explain(void)
41{
42 print_explain(stderr);
43}
44
8c39db39
JP
45static const char *port_states[] = {
46 [BR_STATE_DISABLED] = "disabled",
47 [BR_STATE_LISTENING] = "listening",
48 [BR_STATE_LEARNING] = "learning",
49 [BR_STATE_FORWARDING] = "forwarding",
50 [BR_STATE_BLOCKING] = "blocking",
51};
52
53static void print_portstate(FILE *f, __u8 state)
54{
55 if (state <= BR_STATE_BLOCKING)
56 fprintf(f, "state %s ", port_states[state]);
57 else
58 fprintf(f, "state (%d) ", state);
59}
60
61static void print_onoff(FILE *f, char *flag, __u8 val)
62{
63 fprintf(f, "%s %s ", flag, val ? "on" : "off");
64}
65
66static void bridge_slave_print_opt(struct link_util *lu, FILE *f,
67 struct rtattr *tb[])
68{
69 if (!tb)
70 return;
71
72 if (tb[IFLA_BRPORT_STATE])
73 print_portstate(f, rta_getattr_u8(tb[IFLA_BRPORT_STATE]));
74
75 if (tb[IFLA_BRPORT_PRIORITY])
76 fprintf(f, "priority %d ",
77 rta_getattr_u16(tb[IFLA_BRPORT_PRIORITY]));
78
79 if (tb[IFLA_BRPORT_COST])
80 fprintf(f, "cost %d ",
81 rta_getattr_u32(tb[IFLA_BRPORT_COST]));
82
83 if (tb[IFLA_BRPORT_MODE])
84 print_onoff(f, "hairpin",
85 rta_getattr_u8(tb[IFLA_BRPORT_MODE]));
86
87 if (tb[IFLA_BRPORT_GUARD])
88 print_onoff(f, "guard",
89 rta_getattr_u8(tb[IFLA_BRPORT_GUARD]));
90
91 if (tb[IFLA_BRPORT_PROTECT])
92 print_onoff(f, "root_block",
93 rta_getattr_u8(tb[IFLA_BRPORT_PROTECT]));
94
95 if (tb[IFLA_BRPORT_FAST_LEAVE])
96 print_onoff(f, "fastleave",
97 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]));
98
99 if (tb[IFLA_BRPORT_LEARNING])
100 print_onoff(f, "learning",
101 rta_getattr_u8(tb[IFLA_BRPORT_LEARNING]));
102
103 if (tb[IFLA_BRPORT_UNICAST_FLOOD])
104 print_onoff(f, "flood",
105 rta_getattr_u8(tb[IFLA_BRPORT_UNICAST_FLOOD]));
3069539f
NA
106
107 if (tb[IFLA_BRPORT_ID])
108 fprintf(f, "port_id 0x%x ",
109 rta_getattr_u16(tb[IFLA_BRPORT_ID]));
110
111 if (tb[IFLA_BRPORT_NO])
112 fprintf(f, "port_no 0x%x ",
113 rta_getattr_u16(tb[IFLA_BRPORT_NO]));
114
115 if (tb[IFLA_BRPORT_DESIGNATED_PORT])
116 fprintf(f, "designated_port %u ",
117 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_PORT]));
118
119 if (tb[IFLA_BRPORT_DESIGNATED_COST])
120 fprintf(f, "designated_cost %u ",
121 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_COST]));
122
123 if (tb[IFLA_BRPORT_BRIDGE_ID]) {
124 char bridge_id[32];
125
126 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_BRIDGE_ID]),
127 bridge_id, sizeof(bridge_id));
128 fprintf(f, "designated_bridge %s ", bridge_id);
129 }
130
131 if (tb[IFLA_BRPORT_ROOT_ID]) {
132 char root_id[32];
133
134 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_ROOT_ID]),
135 root_id, sizeof(root_id));
136 fprintf(f, "designated_root %s ", root_id);
137 }
138
139 if (tb[IFLA_BRPORT_HOLD_TIMER]) {
140 struct timeval tv;
141 __u64 htimer;
142
143 htimer = rta_getattr_u64(tb[IFLA_BRPORT_HOLD_TIMER]);
144 __jiffies_to_tv(&tv, htimer);
145 fprintf(f, "hold_timer %4i.%.2i ", (int)tv.tv_sec,
146 (int)tv.tv_usec/10000);
147 }
148
149 if (tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]) {
150 struct timeval tv;
151 __u64 agetimer;
152
153 agetimer = rta_getattr_u64(tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]);
154 __jiffies_to_tv(&tv, agetimer);
155 fprintf(f, "message_age_timer %4i.%.2i ", (int)tv.tv_sec,
156 (int)tv.tv_usec/10000);
157 }
158
159 if (tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]) {
160 struct timeval tv;
161 __u64 fwdtimer;
162
163 fwdtimer = rta_getattr_u64(tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]);
164 __jiffies_to_tv(&tv, fwdtimer);
165 fprintf(f, "forward_delay_timer %4i.%.2i ", (int)tv.tv_sec,
166 (int)tv.tv_usec/10000);
167 }
168
169 if (tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK])
170 fprintf(f, "topology_change_ack %u ",
171 rta_getattr_u8(tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK]));
172
173 if (tb[IFLA_BRPORT_CONFIG_PENDING])
174 fprintf(f, "config_pending %u ",
175 rta_getattr_u8(tb[IFLA_BRPORT_CONFIG_PENDING]));
f6e615de
NA
176 if (tb[IFLA_BRPORT_PROXYARP])
177 print_onoff(f, "proxy_arp",
178 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP]));
38b31a78
NA
179
180 if (tb[IFLA_BRPORT_PROXYARP_WIFI])
181 print_onoff(f, "proxy_arp_wifi",
182 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP_WIFI]));
10759a90
NA
183
184 if (tb[IFLA_BRPORT_MULTICAST_ROUTER])
185 fprintf(f, "mcast_router %u ",
186 rta_getattr_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]));
478a8e59
NA
187
188 if (tb[IFLA_BRPORT_FAST_LEAVE])
189 print_onoff(f, "mcast_fast_leave",
190 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]));
9208b4e7
NA
191
192 if (tb[IFLA_BRPORT_MCAST_FLOOD])
193 print_onoff(f, "mcast_flood",
194 rta_getattr_u8(tb[IFLA_BRPORT_MCAST_FLOOD]));
8c39db39
JP
195}
196
197static void bridge_slave_parse_on_off(char *arg_name, char *arg_val,
198 struct nlmsghdr *n, int type)
199{
200 __u8 val;
201
202 if (strcmp(arg_val, "on") == 0)
203 val = 1;
204 else if (strcmp(arg_val, "off") == 0)
205 val = 0;
206 else
207 invarg("should be \"on\" or \"off\"", arg_name);
208
209 addattr8(n, 1024, type, val);
210}
211
212static int bridge_slave_parse_opt(struct link_util *lu, int argc, char **argv,
213 struct nlmsghdr *n)
214{
215 __u8 state;
216 __u16 priority;
217 __u32 cost;
218
219 while (argc > 0) {
220 if (matches(*argv, "state") == 0) {
221 NEXT_ARG();
222 if (get_u8(&state, *argv, 0))
223 invarg("state is invalid", *argv);
224 addattr8(n, 1024, IFLA_BRPORT_STATE, state);
225 } else if (matches(*argv, "priority") == 0) {
226 NEXT_ARG();
227 if (get_u16(&priority, *argv, 0))
228 invarg("priority is invalid", *argv);
229 addattr16(n, 1024, IFLA_BRPORT_PRIORITY, priority);
230 } else if (matches(*argv, "cost") == 0) {
231 NEXT_ARG();
232 if (get_u32(&cost, *argv, 0))
233 invarg("cost is invalid", *argv);
234 addattr32(n, 1024, IFLA_BRPORT_COST, cost);
235 } else if (matches(*argv, "hairpin") == 0) {
236 NEXT_ARG();
237 bridge_slave_parse_on_off("hairpin", *argv, n,
238 IFLA_BRPORT_MODE);
239 } else if (matches(*argv, "guard") == 0) {
240 NEXT_ARG();
241 bridge_slave_parse_on_off("guard", *argv, n,
242 IFLA_BRPORT_GUARD);
243 } else if (matches(*argv, "root_block") == 0) {
244 NEXT_ARG();
245 bridge_slave_parse_on_off("root_block", *argv, n,
246 IFLA_BRPORT_PROTECT);
247 } else if (matches(*argv, "fastleave") == 0) {
248 NEXT_ARG();
249 bridge_slave_parse_on_off("fastleave", *argv, n,
250 IFLA_BRPORT_FAST_LEAVE);
251 } else if (matches(*argv, "learning") == 0) {
252 NEXT_ARG();
253 bridge_slave_parse_on_off("learning", *argv, n,
254 IFLA_BRPORT_LEARNING);
255 } else if (matches(*argv, "flood") == 0) {
256 NEXT_ARG();
257 bridge_slave_parse_on_off("flood", *argv, n,
258 IFLA_BRPORT_UNICAST_FLOOD);
9208b4e7
NA
259 } else if (matches(*argv, "mcast_flood") == 0) {
260 NEXT_ARG();
261 bridge_slave_parse_on_off("mcast_flood", *argv, n,
262 IFLA_BRPORT_MCAST_FLOOD);
f6e615de
NA
263 } else if (matches(*argv, "proxy_arp") == 0) {
264 NEXT_ARG();
265 bridge_slave_parse_on_off("proxy_arp", *argv, n,
266 IFLA_BRPORT_PROXYARP);
38b31a78
NA
267 } else if (matches(*argv, "proxy_arp_wifi") == 0) {
268 NEXT_ARG();
269 bridge_slave_parse_on_off("proxy_arp_wifi", *argv, n,
270 IFLA_BRPORT_PROXYARP_WIFI);
10759a90
NA
271 } else if (matches(*argv, "mcast_router") == 0) {
272 __u8 mcast_router;
273
274 NEXT_ARG();
275 if (get_u8(&mcast_router, *argv, 0))
276 invarg("invalid mcast_router", *argv);
277 addattr8(n, 1024, IFLA_BRPORT_MULTICAST_ROUTER,
278 mcast_router);
478a8e59
NA
279 } else if (matches(*argv, "mcast_fast_leave") == 0) {
280 NEXT_ARG();
281 bridge_slave_parse_on_off("mcast_fast_leave", *argv, n,
282 IFLA_BRPORT_FAST_LEAVE);
8c39db39
JP
283 } else if (matches(*argv, "help") == 0) {
284 explain();
285 return -1;
286 } else {
287 fprintf(stderr, "bridge_slave: unknown option \"%s\"?\n",
288 *argv);
289 explain();
290 return -1;
291 }
292 argc--, argv++;
293 }
294
295 return 0;
296}
297
a560d850
ZS
298static void bridge_slave_print_help(struct link_util *lu, int argc, char **argv,
299 FILE *f)
300{
301 print_explain(f);
302}
303
8c39db39 304struct link_util bridge_slave_link_util = {
22a84711 305 .id = "bridge_slave",
8c39db39
JP
306 .maxattr = IFLA_BRPORT_MAX,
307 .print_opt = bridge_slave_print_opt,
308 .parse_opt = bridge_slave_parse_opt,
a560d850 309 .print_help = bridge_slave_print_help,
8c39db39 310};