blob: 9a8924c2ee8213b7cb8415b1ce6e8725cc2fafe1 [file] [log] [blame]
Jiri Pirko28d84b42014-09-28 16:33:29 -07001/*
2 * iplink_bridge.c Bridge 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 <stdlib.h>
14#include <string.h>
15#include <linux/if_link.h>
Nikolay Aleksandrov70dfb0b2016-02-09 00:14:19 +010016#include <netinet/ether.h>
Jiri Pirko28d84b42014-09-28 16:33:29 -070017
Toshiaki Makita1eea5c42015-08-31 18:48:46 +090018#include "rt_names.h"
Jiri Pirko28d84b42014-09-28 16:33:29 -070019#include "utils.h"
20#include "ip_common.h"
21
Zhang Shengju43367ef2015-08-12 06:03:23 +000022static void print_explain(FILE *f)
Jiri Pirko28d84b42014-09-28 16:33:29 -070023{
Zhang Shengju43367ef2015-08-12 06:03:23 +000024 fprintf(f,
Jiri Pirko28d84b42014-09-28 16:33:29 -070025 "Usage: ... bridge [ forward_delay FORWARD_DELAY ]\n"
26 " [ hello_time HELLO_TIME ]\n"
27 " [ max_age MAX_AGE ]\n"
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030028 " [ ageing_time AGEING_TIME ]\n"
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030029 " [ stp_state STP_STATE ]\n"
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030030 " [ priority PRIORITY ]\n"
Nikolay Aleksandrov8caaf332016-02-09 00:14:22 +010031 " [ group_fwd_mask MASK ]\n"
Nikolay Aleksandrov0a61aa32016-02-09 00:14:23 +010032 " [ group_address ADDRESS ]\n"
Nikolay Aleksandrove4d456f2015-08-12 09:19:06 -070033 " [ vlan_filtering VLAN_FILTERING ]\n"
Toshiaki Makita1eea5c42015-08-31 18:48:46 +090034 " [ vlan_protocol VLAN_PROTOCOL ]\n"
Nikolay Aleksandrov719832a2016-02-09 00:14:24 +010035 " [ vlan_default_pvid VLAN_DEFAULT_PVID ]\n"
Toshiaki Makita1eea5c42015-08-31 18:48:46 +090036 "\n"
37 "Where: VLAN_PROTOCOL := { 802.1Q | 802.1ad }\n"
Jiri Pirko28d84b42014-09-28 16:33:29 -070038 );
39}
40
Zhang Shengju43367ef2015-08-12 06:03:23 +000041static void explain(void)
42{
43 print_explain(stderr);
44}
45
Nikolay Aleksandrov70dfb0b2016-02-09 00:14:19 +010046static void br_dump_bridge_id(const struct ifla_bridge_id *id, char *buf,
47 size_t len)
48{
49 char eaddr[32];
50
51 ether_ntoa_r((const struct ether_addr *)id->addr, eaddr);
52 snprintf(buf, len, "%.2x%.2x.%s", id->prio[0], id->prio[1], eaddr);
53}
54
Jiri Pirko28d84b42014-09-28 16:33:29 -070055static int bridge_parse_opt(struct link_util *lu, int argc, char **argv,
56 struct nlmsghdr *n)
57{
58 __u32 val;
59
60 while (argc > 0) {
61 if (matches(*argv, "forward_delay") == 0) {
62 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000063 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070064 invarg("invalid forward_delay", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000065
Jiri Pirko28d84b42014-09-28 16:33:29 -070066 addattr32(n, 1024, IFLA_BR_FORWARD_DELAY, val);
67 } else if (matches(*argv, "hello_time") == 0) {
68 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000069 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070070 invarg("invalid hello_time", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000071
Jiri Pirko28d84b42014-09-28 16:33:29 -070072 addattr32(n, 1024, IFLA_BR_HELLO_TIME, val);
73 } else if (matches(*argv, "max_age") == 0) {
74 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000075 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070076 invarg("invalid max_age", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000077
Jiri Pirko28d84b42014-09-28 16:33:29 -070078 addattr32(n, 1024, IFLA_BR_MAX_AGE, val);
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030079 } else if (matches(*argv, "ageing_time") == 0) {
80 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000081 if (get_u32(&val, *argv, 0))
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030082 invarg("invalid ageing_time", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000083
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030084 addattr32(n, 1024, IFLA_BR_AGEING_TIME, val);
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030085 } else if (matches(*argv, "stp_state") == 0) {
86 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000087 if (get_u32(&val, *argv, 0))
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030088 invarg("invalid stp_state", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000089
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030090 addattr32(n, 1024, IFLA_BR_STP_STATE, val);
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030091 } else if (matches(*argv, "priority") == 0) {
92 __u16 prio;
93
94 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000095 if (get_u16(&prio, *argv, 0))
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030096 invarg("invalid priority", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000097
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030098 addattr16(n, 1024, IFLA_BR_PRIORITY, prio);
Nikolay Aleksandrove4d456f2015-08-12 09:19:06 -070099 } else if (matches(*argv, "vlan_filtering") == 0) {
100 __u8 vlan_filter;
101
102 NEXT_ARG();
103 if (get_u8(&vlan_filter, *argv, 0)) {
104 invarg("invalid vlan_filtering", *argv);
105 return -1;
106 }
107 addattr8(n, 1024, IFLA_BR_VLAN_FILTERING, vlan_filter);
Toshiaki Makita1eea5c42015-08-31 18:48:46 +0900108 } else if (matches(*argv, "vlan_protocol") == 0) {
109 __u16 vlan_proto;
110
111 NEXT_ARG();
112 if (ll_proto_a2n(&vlan_proto, *argv)) {
113 invarg("invalid vlan_protocol", *argv);
114 return -1;
115 }
116 addattr16(n, 1024, IFLA_BR_VLAN_PROTOCOL, vlan_proto);
Nikolay Aleksandrov8caaf332016-02-09 00:14:22 +0100117 } else if (matches(*argv, "group_fwd_mask") == 0) {
118 __u16 fwd_mask;
119
120 NEXT_ARG();
121 if (get_u16(&fwd_mask, *argv, 0))
122 invarg("invalid group_fwd_mask", *argv);
123
124 addattr16(n, 1024, IFLA_BR_GROUP_FWD_MASK, fwd_mask);
Nikolay Aleksandrov0a61aa32016-02-09 00:14:23 +0100125 } else if (matches(*argv, "group_address") == 0) {
126 char llabuf[32];
127 int len;
128
129 NEXT_ARG();
130 len = ll_addr_a2n(llabuf, sizeof(llabuf), *argv);
131 if (len < 0)
132 return -1;
133 addattr_l(n, 1024, IFLA_BR_GROUP_ADDR, llabuf, len);
Nikolay Aleksandrov719832a2016-02-09 00:14:24 +0100134 } else if (matches(*argv, "vlan_default_pvid") == 0) {
135 __u16 default_pvid;
136
137 NEXT_ARG();
138 if (get_u16(&default_pvid, *argv, 0))
139 invarg("invalid vlan_default_pvid", *argv);
140
141 addattr16(n, 1024, IFLA_BR_VLAN_DEFAULT_PVID,
142 default_pvid);
Jiri Pirko28d84b42014-09-28 16:33:29 -0700143 } else if (matches(*argv, "help") == 0) {
144 explain();
145 return -1;
146 } else {
147 fprintf(stderr, "bridge: unknown command \"%s\"?\n", *argv);
148 explain();
149 return -1;
150 }
151 argc--, argv++;
152 }
153
154 return 0;
155}
156
157static void bridge_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
158{
159 if (!tb)
160 return;
161
162 if (tb[IFLA_BR_FORWARD_DELAY])
163 fprintf(f, "forward_delay %u ",
164 rta_getattr_u32(tb[IFLA_BR_FORWARD_DELAY]));
165
166 if (tb[IFLA_BR_HELLO_TIME])
167 fprintf(f, "hello_time %u ",
168 rta_getattr_u32(tb[IFLA_BR_HELLO_TIME]));
169
170 if (tb[IFLA_BR_MAX_AGE])
171 fprintf(f, "max_age %u ",
172 rta_getattr_u32(tb[IFLA_BR_MAX_AGE]));
Nikolay Aleksandrovfdba0512015-08-12 09:11:30 -0700173
174 if (tb[IFLA_BR_AGEING_TIME])
175 fprintf(f, "ageing_time %u ",
176 rta_getattr_u32(tb[IFLA_BR_AGEING_TIME]));
177
178 if (tb[IFLA_BR_STP_STATE])
179 fprintf(f, "stp_state %u ",
180 rta_getattr_u32(tb[IFLA_BR_STP_STATE]));
181
182 if (tb[IFLA_BR_PRIORITY])
183 fprintf(f, "priority %u ",
184 rta_getattr_u16(tb[IFLA_BR_PRIORITY]));
Nikolay Aleksandrove4d456f2015-08-12 09:19:06 -0700185
186 if (tb[IFLA_BR_VLAN_FILTERING])
187 fprintf(f, "vlan_filtering %u ",
188 rta_getattr_u8(tb[IFLA_BR_VLAN_FILTERING]));
Toshiaki Makita1eea5c42015-08-31 18:48:46 +0900189
190 if (tb[IFLA_BR_VLAN_PROTOCOL]) {
191 SPRINT_BUF(b1);
192
193 fprintf(f, "vlan_protocol %s ",
194 ll_proto_n2a(rta_getattr_u16(tb[IFLA_BR_VLAN_PROTOCOL]),
195 b1, sizeof(b1)));
196 }
Nikolay Aleksandrov70dfb0b2016-02-09 00:14:19 +0100197
198 if (tb[IFLA_BR_BRIDGE_ID]) {
199 char bridge_id[32];
200
201 br_dump_bridge_id(RTA_DATA(tb[IFLA_BR_BRIDGE_ID]), bridge_id,
202 sizeof(bridge_id));
203 fprintf(f, "bridge_id %s ", bridge_id);
204 }
205
206 if (tb[IFLA_BR_ROOT_ID]) {
207 char root_id[32];
208
209 br_dump_bridge_id(RTA_DATA(tb[IFLA_BR_BRIDGE_ID]), root_id,
210 sizeof(root_id));
211 fprintf(f, "designated_root %s ", root_id);
212 }
Nikolay Aleksandrov4e3bbc62016-02-09 00:14:20 +0100213
214 if (tb[IFLA_BR_ROOT_PORT])
215 fprintf(f, "root_port %u ",
216 rta_getattr_u16(tb[IFLA_BR_ROOT_PORT]));
217
218 if (tb[IFLA_BR_ROOT_PATH_COST])
219 fprintf(f, "root_path_cost %u ",
220 rta_getattr_u32(tb[IFLA_BR_ROOT_PATH_COST]));
221
222 if (tb[IFLA_BR_TOPOLOGY_CHANGE])
223 fprintf(f, "topology_change %u ",
224 rta_getattr_u8(tb[IFLA_BR_TOPOLOGY_CHANGE]));
225
226 if (tb[IFLA_BR_TOPOLOGY_CHANGE_DETECTED])
227 fprintf(f, "topology_change_detected %u ",
228 rta_getattr_u8(tb[IFLA_BR_TOPOLOGY_CHANGE_DETECTED]));
Nikolay Aleksandrov8c0f7a12016-02-09 00:14:21 +0100229
230 if (tb[IFLA_BR_HELLO_TIMER]) {
231 struct timeval tv;
232
233 __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_HELLO_TIMER]));
234 fprintf(f, "hello_timer %4i.%.2i ", (int)tv.tv_sec,
235 (int)tv.tv_usec/10000);
236 }
237
238 if (tb[IFLA_BR_TCN_TIMER]) {
239 struct timeval tv;
240
241 __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_TCN_TIMER]));
242 fprintf(f, "tcn_timer %4i.%.2i ", (int)tv.tv_sec,
243 (int)tv.tv_usec/10000);
244 }
245
246 if (tb[IFLA_BR_TOPOLOGY_CHANGE_TIMER]) {
247 unsigned long jiffies;
248 struct timeval tv;
249
250 jiffies = rta_getattr_u64(tb[IFLA_BR_TOPOLOGY_CHANGE_TIMER]);
251 __jiffies_to_tv(&tv, jiffies);
252 fprintf(f, "topology_change_timer %4i.%.2i ", (int)tv.tv_sec,
253 (int)tv.tv_usec/10000);
254 }
255
256 if (tb[IFLA_BR_GC_TIMER]) {
257 struct timeval tv;
258
259 __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_GC_TIMER]));
260 fprintf(f, "gc_timer %4i.%.2i ", (int)tv.tv_sec,
261 (int)tv.tv_usec/10000);
262 }
Nikolay Aleksandrov8caaf332016-02-09 00:14:22 +0100263
Nikolay Aleksandrov719832a2016-02-09 00:14:24 +0100264 if (tb[IFLA_BR_VLAN_DEFAULT_PVID])
265 fprintf(f, "vlan_default_pvid %u ",
266 rta_getattr_u16(tb[IFLA_BR_VLAN_DEFAULT_PVID]));
267
Nikolay Aleksandrov8caaf332016-02-09 00:14:22 +0100268 if (tb[IFLA_BR_GROUP_FWD_MASK])
269 fprintf(f, "group_fwd_mask %#x ",
270 rta_getattr_u16(tb[IFLA_BR_GROUP_FWD_MASK]));
Nikolay Aleksandrov0a61aa32016-02-09 00:14:23 +0100271
272 if (tb[IFLA_BR_GROUP_ADDR]) {
273 SPRINT_BUF(mac);
274
275 fprintf(f, "group_address %s ",
276 ll_addr_n2a(RTA_DATA(tb[IFLA_BR_GROUP_ADDR]),
277 RTA_PAYLOAD(tb[IFLA_BR_GROUP_ADDR]),
278 1 /*ARPHDR_ETHER*/, mac, sizeof(mac)));
279 }
Jiri Pirko28d84b42014-09-28 16:33:29 -0700280}
281
Zhang Shengju43367ef2015-08-12 06:03:23 +0000282static void bridge_print_help(struct link_util *lu, int argc, char **argv,
283 FILE *f)
284{
285 print_explain(f);
286}
287
Jiri Pirko28d84b42014-09-28 16:33:29 -0700288struct link_util bridge_link_util = {
289 .id = "bridge",
290 .maxattr = IFLA_BR_MAX,
291 .parse_opt = bridge_parse_opt,
292 .print_opt = bridge_print_opt,
Zhang Shengju43367ef2015-08-12 06:03:23 +0000293 .print_help = bridge_print_help,
Jiri Pirko28d84b42014-09-28 16:33:29 -0700294};