2 * iplink_bridge_slave.c Bridge slave device support
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <linux/if_link.h>
16 #include <linux/if_bridge.h>
20 #include "ip_common.h"
22 static void print_explain(FILE *f
)
25 "Usage: ... bridge_slave [ state STATE ] [ priority PRIO ] [cost COST ]\n"
26 " [ guard {on | off} ]\n"
27 " [ hairpin {on | off} ]\n"
28 " [ fastleave {on | off} ]\n"
29 " [ root_block {on | off} ]\n"
30 " [ learning {on | off} ]\n"
31 " [ flood {on | off} ]\n"
32 " [ proxy_arp {on | off} ]\n"
33 " [ proxy_arp_wifi {on | off} ]\n"
34 " [ mcast_router MULTICAST_ROUTER ]\n"
35 " [ mcast_fast_leave {on | off} ]\n"
36 " [ mcast_flood {on | off} ]\n"
40 static void explain(void)
42 print_explain(stderr
);
45 static 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",
53 static void print_portstate(FILE *f
, __u8 state
)
55 if (state
<= BR_STATE_BLOCKING
)
56 fprintf(f
, "state %s ", port_states
[state
]);
58 fprintf(f
, "state (%d) ", state
);
61 static void print_onoff(FILE *f
, char *flag
, __u8 val
)
63 fprintf(f
, "%s %s ", flag
, val
? "on" : "off");
66 static void bridge_slave_print_opt(struct link_util
*lu
, FILE *f
,
72 if (tb
[IFLA_BRPORT_STATE
])
73 print_portstate(f
, rta_getattr_u8(tb
[IFLA_BRPORT_STATE
]));
75 if (tb
[IFLA_BRPORT_PRIORITY
])
76 fprintf(f
, "priority %d ",
77 rta_getattr_u16(tb
[IFLA_BRPORT_PRIORITY
]));
79 if (tb
[IFLA_BRPORT_COST
])
80 fprintf(f
, "cost %d ",
81 rta_getattr_u32(tb
[IFLA_BRPORT_COST
]));
83 if (tb
[IFLA_BRPORT_MODE
])
84 print_onoff(f
, "hairpin",
85 rta_getattr_u8(tb
[IFLA_BRPORT_MODE
]));
87 if (tb
[IFLA_BRPORT_GUARD
])
88 print_onoff(f
, "guard",
89 rta_getattr_u8(tb
[IFLA_BRPORT_GUARD
]));
91 if (tb
[IFLA_BRPORT_PROTECT
])
92 print_onoff(f
, "root_block",
93 rta_getattr_u8(tb
[IFLA_BRPORT_PROTECT
]));
95 if (tb
[IFLA_BRPORT_FAST_LEAVE
])
96 print_onoff(f
, "fastleave",
97 rta_getattr_u8(tb
[IFLA_BRPORT_FAST_LEAVE
]));
99 if (tb
[IFLA_BRPORT_LEARNING
])
100 print_onoff(f
, "learning",
101 rta_getattr_u8(tb
[IFLA_BRPORT_LEARNING
]));
103 if (tb
[IFLA_BRPORT_UNICAST_FLOOD
])
104 print_onoff(f
, "flood",
105 rta_getattr_u8(tb
[IFLA_BRPORT_UNICAST_FLOOD
]));
107 if (tb
[IFLA_BRPORT_ID
])
108 fprintf(f
, "port_id 0x%x ",
109 rta_getattr_u16(tb
[IFLA_BRPORT_ID
]));
111 if (tb
[IFLA_BRPORT_NO
])
112 fprintf(f
, "port_no 0x%x ",
113 rta_getattr_u16(tb
[IFLA_BRPORT_NO
]));
115 if (tb
[IFLA_BRPORT_DESIGNATED_PORT
])
116 fprintf(f
, "designated_port %u ",
117 rta_getattr_u16(tb
[IFLA_BRPORT_DESIGNATED_PORT
]));
119 if (tb
[IFLA_BRPORT_DESIGNATED_COST
])
120 fprintf(f
, "designated_cost %u ",
121 rta_getattr_u16(tb
[IFLA_BRPORT_DESIGNATED_COST
]));
123 if (tb
[IFLA_BRPORT_BRIDGE_ID
]) {
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
);
131 if (tb
[IFLA_BRPORT_ROOT_ID
]) {
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
);
139 if (tb
[IFLA_BRPORT_HOLD_TIMER
]) {
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);
149 if (tb
[IFLA_BRPORT_MESSAGE_AGE_TIMER
]) {
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);
159 if (tb
[IFLA_BRPORT_FORWARD_DELAY_TIMER
]) {
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);
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
]));
173 if (tb
[IFLA_BRPORT_CONFIG_PENDING
])
174 fprintf(f
, "config_pending %u ",
175 rta_getattr_u8(tb
[IFLA_BRPORT_CONFIG_PENDING
]));
176 if (tb
[IFLA_BRPORT_PROXYARP
])
177 print_onoff(f
, "proxy_arp",
178 rta_getattr_u8(tb
[IFLA_BRPORT_PROXYARP
]));
180 if (tb
[IFLA_BRPORT_PROXYARP_WIFI
])
181 print_onoff(f
, "proxy_arp_wifi",
182 rta_getattr_u8(tb
[IFLA_BRPORT_PROXYARP_WIFI
]));
184 if (tb
[IFLA_BRPORT_MULTICAST_ROUTER
])
185 fprintf(f
, "mcast_router %u ",
186 rta_getattr_u8(tb
[IFLA_BRPORT_MULTICAST_ROUTER
]));
188 if (tb
[IFLA_BRPORT_FAST_LEAVE
])
189 print_onoff(f
, "mcast_fast_leave",
190 rta_getattr_u8(tb
[IFLA_BRPORT_FAST_LEAVE
]));
192 if (tb
[IFLA_BRPORT_MCAST_FLOOD
])
193 print_onoff(f
, "mcast_flood",
194 rta_getattr_u8(tb
[IFLA_BRPORT_MCAST_FLOOD
]));
197 static void bridge_slave_parse_on_off(char *arg_name
, char *arg_val
,
198 struct nlmsghdr
*n
, int type
)
202 if (strcmp(arg_val
, "on") == 0)
204 else if (strcmp(arg_val
, "off") == 0)
207 invarg("should be \"on\" or \"off\"", arg_name
);
209 addattr8(n
, 1024, type
, val
);
212 static int bridge_slave_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
220 if (matches(*argv
, "state") == 0) {
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) {
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) {
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) {
237 bridge_slave_parse_on_off("hairpin", *argv
, n
,
239 } else if (matches(*argv
, "guard") == 0) {
241 bridge_slave_parse_on_off("guard", *argv
, n
,
243 } else if (matches(*argv
, "root_block") == 0) {
245 bridge_slave_parse_on_off("root_block", *argv
, n
,
246 IFLA_BRPORT_PROTECT
);
247 } else if (matches(*argv
, "fastleave") == 0) {
249 bridge_slave_parse_on_off("fastleave", *argv
, n
,
250 IFLA_BRPORT_FAST_LEAVE
);
251 } else if (matches(*argv
, "learning") == 0) {
253 bridge_slave_parse_on_off("learning", *argv
, n
,
254 IFLA_BRPORT_LEARNING
);
255 } else if (matches(*argv
, "flood") == 0) {
257 bridge_slave_parse_on_off("flood", *argv
, n
,
258 IFLA_BRPORT_UNICAST_FLOOD
);
259 } else if (matches(*argv
, "mcast_flood") == 0) {
261 bridge_slave_parse_on_off("mcast_flood", *argv
, n
,
262 IFLA_BRPORT_MCAST_FLOOD
);
263 } else if (matches(*argv
, "proxy_arp") == 0) {
265 bridge_slave_parse_on_off("proxy_arp", *argv
, n
,
266 IFLA_BRPORT_PROXYARP
);
267 } else if (matches(*argv
, "proxy_arp_wifi") == 0) {
269 bridge_slave_parse_on_off("proxy_arp_wifi", *argv
, n
,
270 IFLA_BRPORT_PROXYARP_WIFI
);
271 } else if (matches(*argv
, "mcast_router") == 0) {
275 if (get_u8(&mcast_router
, *argv
, 0))
276 invarg("invalid mcast_router", *argv
);
277 addattr8(n
, 1024, IFLA_BRPORT_MULTICAST_ROUTER
,
279 } else if (matches(*argv
, "mcast_fast_leave") == 0) {
281 bridge_slave_parse_on_off("mcast_fast_leave", *argv
, n
,
282 IFLA_BRPORT_FAST_LEAVE
);
283 } else if (matches(*argv
, "help") == 0) {
287 fprintf(stderr
, "bridge_slave: unknown option \"%s\"?\n",
298 static void bridge_slave_print_help(struct link_util
*lu
, int argc
, char **argv
,
304 struct link_util bridge_slave_link_util
= {
305 .id
= "bridge_slave",
306 .maxattr
= IFLA_BRPORT_MAX
,
307 .print_opt
= bridge_slave_print_opt
,
308 .parse_opt
= bridge_slave_parse_opt
,
309 .print_help
= bridge_slave_print_help
,