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