Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Aleksandrov | 70dfb0b | 2016-02-09 00:14:19 +0100 | [diff] [blame] | 16 | #include <netinet/ether.h> |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 17 | |
Toshiaki Makita | 1eea5c4 | 2015-08-31 18:48:46 +0900 | [diff] [blame] | 18 | #include "rt_names.h" |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 19 | #include "utils.h" |
| 20 | #include "ip_common.h" |
| 21 | |
Zhang Shengju | 43367ef | 2015-08-12 06:03:23 +0000 | [diff] [blame] | 22 | static void print_explain(FILE *f) |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 23 | { |
Zhang Shengju | 43367ef | 2015-08-12 06:03:23 +0000 | [diff] [blame] | 24 | fprintf(f, |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 25 | "Usage: ... bridge [ forward_delay FORWARD_DELAY ]\n" |
| 26 | " [ hello_time HELLO_TIME ]\n" |
| 27 | " [ max_age MAX_AGE ]\n" |
Nikolay Aleksandrov | 6c99fb6 | 2015-06-16 13:38:47 +0300 | [diff] [blame] | 28 | " [ ageing_time AGEING_TIME ]\n" |
Nikolay Aleksandrov | dab0496 | 2015-06-16 13:38:48 +0300 | [diff] [blame] | 29 | " [ stp_state STP_STATE ]\n" |
Nikolay Aleksandrov | b0197a0 | 2015-06-16 13:38:49 +0300 | [diff] [blame] | 30 | " [ priority PRIORITY ]\n" |
Nikolay Aleksandrov | 8caaf33 | 2016-02-09 00:14:22 +0100 | [diff] [blame] | 31 | " [ group_fwd_mask MASK ]\n" |
Nikolay Aleksandrov | 0a61aa3 | 2016-02-09 00:14:23 +0100 | [diff] [blame^] | 32 | " [ group_address ADDRESS ]\n" |
Nikolay Aleksandrov | e4d456f | 2015-08-12 09:19:06 -0700 | [diff] [blame] | 33 | " [ vlan_filtering VLAN_FILTERING ]\n" |
Toshiaki Makita | 1eea5c4 | 2015-08-31 18:48:46 +0900 | [diff] [blame] | 34 | " [ vlan_protocol VLAN_PROTOCOL ]\n" |
| 35 | "\n" |
| 36 | "Where: VLAN_PROTOCOL := { 802.1Q | 802.1ad }\n" |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 37 | ); |
| 38 | } |
| 39 | |
Zhang Shengju | 43367ef | 2015-08-12 06:03:23 +0000 | [diff] [blame] | 40 | static void explain(void) |
| 41 | { |
| 42 | print_explain(stderr); |
| 43 | } |
| 44 | |
Nikolay Aleksandrov | 70dfb0b | 2016-02-09 00:14:19 +0100 | [diff] [blame] | 45 | static void br_dump_bridge_id(const struct ifla_bridge_id *id, char *buf, |
| 46 | size_t len) |
| 47 | { |
| 48 | char eaddr[32]; |
| 49 | |
| 50 | ether_ntoa_r((const struct ether_addr *)id->addr, eaddr); |
| 51 | snprintf(buf, len, "%.2x%.2x.%s", id->prio[0], id->prio[1], eaddr); |
| 52 | } |
| 53 | |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 54 | static int bridge_parse_opt(struct link_util *lu, int argc, char **argv, |
| 55 | struct nlmsghdr *n) |
| 56 | { |
| 57 | __u32 val; |
| 58 | |
| 59 | while (argc > 0) { |
| 60 | if (matches(*argv, "forward_delay") == 0) { |
| 61 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 62 | if (get_u32(&val, *argv, 0)) |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 63 | invarg("invalid forward_delay", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 64 | |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 65 | addattr32(n, 1024, IFLA_BR_FORWARD_DELAY, val); |
| 66 | } else if (matches(*argv, "hello_time") == 0) { |
| 67 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 68 | if (get_u32(&val, *argv, 0)) |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 69 | invarg("invalid hello_time", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 70 | |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 71 | addattr32(n, 1024, IFLA_BR_HELLO_TIME, val); |
| 72 | } else if (matches(*argv, "max_age") == 0) { |
| 73 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 74 | if (get_u32(&val, *argv, 0)) |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 75 | invarg("invalid max_age", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 76 | |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 77 | addattr32(n, 1024, IFLA_BR_MAX_AGE, val); |
Nikolay Aleksandrov | 6c99fb6 | 2015-06-16 13:38:47 +0300 | [diff] [blame] | 78 | } else if (matches(*argv, "ageing_time") == 0) { |
| 79 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 80 | if (get_u32(&val, *argv, 0)) |
Nikolay Aleksandrov | 6c99fb6 | 2015-06-16 13:38:47 +0300 | [diff] [blame] | 81 | invarg("invalid ageing_time", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 82 | |
Nikolay Aleksandrov | 6c99fb6 | 2015-06-16 13:38:47 +0300 | [diff] [blame] | 83 | addattr32(n, 1024, IFLA_BR_AGEING_TIME, val); |
Nikolay Aleksandrov | dab0496 | 2015-06-16 13:38:48 +0300 | [diff] [blame] | 84 | } else if (matches(*argv, "stp_state") == 0) { |
| 85 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 86 | if (get_u32(&val, *argv, 0)) |
Nikolay Aleksandrov | dab0496 | 2015-06-16 13:38:48 +0300 | [diff] [blame] | 87 | invarg("invalid stp_state", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 88 | |
Nikolay Aleksandrov | dab0496 | 2015-06-16 13:38:48 +0300 | [diff] [blame] | 89 | addattr32(n, 1024, IFLA_BR_STP_STATE, val); |
Nikolay Aleksandrov | b0197a0 | 2015-06-16 13:38:49 +0300 | [diff] [blame] | 90 | } else if (matches(*argv, "priority") == 0) { |
| 91 | __u16 prio; |
| 92 | |
| 93 | NEXT_ARG(); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 94 | if (get_u16(&prio, *argv, 0)) |
Nikolay Aleksandrov | b0197a0 | 2015-06-16 13:38:49 +0300 | [diff] [blame] | 95 | invarg("invalid priority", *argv); |
Zhang Shengju | 6a9ce30 | 2015-08-13 07:48:15 +0000 | [diff] [blame] | 96 | |
Nikolay Aleksandrov | b0197a0 | 2015-06-16 13:38:49 +0300 | [diff] [blame] | 97 | addattr16(n, 1024, IFLA_BR_PRIORITY, prio); |
Nikolay Aleksandrov | e4d456f | 2015-08-12 09:19:06 -0700 | [diff] [blame] | 98 | } else if (matches(*argv, "vlan_filtering") == 0) { |
| 99 | __u8 vlan_filter; |
| 100 | |
| 101 | NEXT_ARG(); |
| 102 | if (get_u8(&vlan_filter, *argv, 0)) { |
| 103 | invarg("invalid vlan_filtering", *argv); |
| 104 | return -1; |
| 105 | } |
| 106 | addattr8(n, 1024, IFLA_BR_VLAN_FILTERING, vlan_filter); |
Toshiaki Makita | 1eea5c4 | 2015-08-31 18:48:46 +0900 | [diff] [blame] | 107 | } else if (matches(*argv, "vlan_protocol") == 0) { |
| 108 | __u16 vlan_proto; |
| 109 | |
| 110 | NEXT_ARG(); |
| 111 | if (ll_proto_a2n(&vlan_proto, *argv)) { |
| 112 | invarg("invalid vlan_protocol", *argv); |
| 113 | return -1; |
| 114 | } |
| 115 | addattr16(n, 1024, IFLA_BR_VLAN_PROTOCOL, vlan_proto); |
Nikolay Aleksandrov | 8caaf33 | 2016-02-09 00:14:22 +0100 | [diff] [blame] | 116 | } else if (matches(*argv, "group_fwd_mask") == 0) { |
| 117 | __u16 fwd_mask; |
| 118 | |
| 119 | NEXT_ARG(); |
| 120 | if (get_u16(&fwd_mask, *argv, 0)) |
| 121 | invarg("invalid group_fwd_mask", *argv); |
| 122 | |
| 123 | addattr16(n, 1024, IFLA_BR_GROUP_FWD_MASK, fwd_mask); |
Nikolay Aleksandrov | 0a61aa3 | 2016-02-09 00:14:23 +0100 | [diff] [blame^] | 124 | } else if (matches(*argv, "group_address") == 0) { |
| 125 | char llabuf[32]; |
| 126 | int len; |
| 127 | |
| 128 | NEXT_ARG(); |
| 129 | len = ll_addr_a2n(llabuf, sizeof(llabuf), *argv); |
| 130 | if (len < 0) |
| 131 | return -1; |
| 132 | addattr_l(n, 1024, IFLA_BR_GROUP_ADDR, llabuf, len); |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 133 | } else if (matches(*argv, "help") == 0) { |
| 134 | explain(); |
| 135 | return -1; |
| 136 | } else { |
| 137 | fprintf(stderr, "bridge: unknown command \"%s\"?\n", *argv); |
| 138 | explain(); |
| 139 | return -1; |
| 140 | } |
| 141 | argc--, argv++; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static void bridge_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 148 | { |
| 149 | if (!tb) |
| 150 | return; |
| 151 | |
| 152 | if (tb[IFLA_BR_FORWARD_DELAY]) |
| 153 | fprintf(f, "forward_delay %u ", |
| 154 | rta_getattr_u32(tb[IFLA_BR_FORWARD_DELAY])); |
| 155 | |
| 156 | if (tb[IFLA_BR_HELLO_TIME]) |
| 157 | fprintf(f, "hello_time %u ", |
| 158 | rta_getattr_u32(tb[IFLA_BR_HELLO_TIME])); |
| 159 | |
| 160 | if (tb[IFLA_BR_MAX_AGE]) |
| 161 | fprintf(f, "max_age %u ", |
| 162 | rta_getattr_u32(tb[IFLA_BR_MAX_AGE])); |
Nikolay Aleksandrov | fdba051 | 2015-08-12 09:11:30 -0700 | [diff] [blame] | 163 | |
| 164 | if (tb[IFLA_BR_AGEING_TIME]) |
| 165 | fprintf(f, "ageing_time %u ", |
| 166 | rta_getattr_u32(tb[IFLA_BR_AGEING_TIME])); |
| 167 | |
| 168 | if (tb[IFLA_BR_STP_STATE]) |
| 169 | fprintf(f, "stp_state %u ", |
| 170 | rta_getattr_u32(tb[IFLA_BR_STP_STATE])); |
| 171 | |
| 172 | if (tb[IFLA_BR_PRIORITY]) |
| 173 | fprintf(f, "priority %u ", |
| 174 | rta_getattr_u16(tb[IFLA_BR_PRIORITY])); |
Nikolay Aleksandrov | e4d456f | 2015-08-12 09:19:06 -0700 | [diff] [blame] | 175 | |
| 176 | if (tb[IFLA_BR_VLAN_FILTERING]) |
| 177 | fprintf(f, "vlan_filtering %u ", |
| 178 | rta_getattr_u8(tb[IFLA_BR_VLAN_FILTERING])); |
Toshiaki Makita | 1eea5c4 | 2015-08-31 18:48:46 +0900 | [diff] [blame] | 179 | |
| 180 | if (tb[IFLA_BR_VLAN_PROTOCOL]) { |
| 181 | SPRINT_BUF(b1); |
| 182 | |
| 183 | fprintf(f, "vlan_protocol %s ", |
| 184 | ll_proto_n2a(rta_getattr_u16(tb[IFLA_BR_VLAN_PROTOCOL]), |
| 185 | b1, sizeof(b1))); |
| 186 | } |
Nikolay Aleksandrov | 70dfb0b | 2016-02-09 00:14:19 +0100 | [diff] [blame] | 187 | |
| 188 | if (tb[IFLA_BR_BRIDGE_ID]) { |
| 189 | char bridge_id[32]; |
| 190 | |
| 191 | br_dump_bridge_id(RTA_DATA(tb[IFLA_BR_BRIDGE_ID]), bridge_id, |
| 192 | sizeof(bridge_id)); |
| 193 | fprintf(f, "bridge_id %s ", bridge_id); |
| 194 | } |
| 195 | |
| 196 | if (tb[IFLA_BR_ROOT_ID]) { |
| 197 | char root_id[32]; |
| 198 | |
| 199 | br_dump_bridge_id(RTA_DATA(tb[IFLA_BR_BRIDGE_ID]), root_id, |
| 200 | sizeof(root_id)); |
| 201 | fprintf(f, "designated_root %s ", root_id); |
| 202 | } |
Nikolay Aleksandrov | 4e3bbc6 | 2016-02-09 00:14:20 +0100 | [diff] [blame] | 203 | |
| 204 | if (tb[IFLA_BR_ROOT_PORT]) |
| 205 | fprintf(f, "root_port %u ", |
| 206 | rta_getattr_u16(tb[IFLA_BR_ROOT_PORT])); |
| 207 | |
| 208 | if (tb[IFLA_BR_ROOT_PATH_COST]) |
| 209 | fprintf(f, "root_path_cost %u ", |
| 210 | rta_getattr_u32(tb[IFLA_BR_ROOT_PATH_COST])); |
| 211 | |
| 212 | if (tb[IFLA_BR_TOPOLOGY_CHANGE]) |
| 213 | fprintf(f, "topology_change %u ", |
| 214 | rta_getattr_u8(tb[IFLA_BR_TOPOLOGY_CHANGE])); |
| 215 | |
| 216 | if (tb[IFLA_BR_TOPOLOGY_CHANGE_DETECTED]) |
| 217 | fprintf(f, "topology_change_detected %u ", |
| 218 | rta_getattr_u8(tb[IFLA_BR_TOPOLOGY_CHANGE_DETECTED])); |
Nikolay Aleksandrov | 8c0f7a1 | 2016-02-09 00:14:21 +0100 | [diff] [blame] | 219 | |
| 220 | if (tb[IFLA_BR_HELLO_TIMER]) { |
| 221 | struct timeval tv; |
| 222 | |
| 223 | __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_HELLO_TIMER])); |
| 224 | fprintf(f, "hello_timer %4i.%.2i ", (int)tv.tv_sec, |
| 225 | (int)tv.tv_usec/10000); |
| 226 | } |
| 227 | |
| 228 | if (tb[IFLA_BR_TCN_TIMER]) { |
| 229 | struct timeval tv; |
| 230 | |
| 231 | __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_TCN_TIMER])); |
| 232 | fprintf(f, "tcn_timer %4i.%.2i ", (int)tv.tv_sec, |
| 233 | (int)tv.tv_usec/10000); |
| 234 | } |
| 235 | |
| 236 | if (tb[IFLA_BR_TOPOLOGY_CHANGE_TIMER]) { |
| 237 | unsigned long jiffies; |
| 238 | struct timeval tv; |
| 239 | |
| 240 | jiffies = rta_getattr_u64(tb[IFLA_BR_TOPOLOGY_CHANGE_TIMER]); |
| 241 | __jiffies_to_tv(&tv, jiffies); |
| 242 | fprintf(f, "topology_change_timer %4i.%.2i ", (int)tv.tv_sec, |
| 243 | (int)tv.tv_usec/10000); |
| 244 | } |
| 245 | |
| 246 | if (tb[IFLA_BR_GC_TIMER]) { |
| 247 | struct timeval tv; |
| 248 | |
| 249 | __jiffies_to_tv(&tv, rta_getattr_u64(tb[IFLA_BR_GC_TIMER])); |
| 250 | fprintf(f, "gc_timer %4i.%.2i ", (int)tv.tv_sec, |
| 251 | (int)tv.tv_usec/10000); |
| 252 | } |
Nikolay Aleksandrov | 8caaf33 | 2016-02-09 00:14:22 +0100 | [diff] [blame] | 253 | |
| 254 | if (tb[IFLA_BR_GROUP_FWD_MASK]) |
| 255 | fprintf(f, "group_fwd_mask %#x ", |
| 256 | rta_getattr_u16(tb[IFLA_BR_GROUP_FWD_MASK])); |
Nikolay Aleksandrov | 0a61aa3 | 2016-02-09 00:14:23 +0100 | [diff] [blame^] | 257 | |
| 258 | if (tb[IFLA_BR_GROUP_ADDR]) { |
| 259 | SPRINT_BUF(mac); |
| 260 | |
| 261 | fprintf(f, "group_address %s ", |
| 262 | ll_addr_n2a(RTA_DATA(tb[IFLA_BR_GROUP_ADDR]), |
| 263 | RTA_PAYLOAD(tb[IFLA_BR_GROUP_ADDR]), |
| 264 | 1 /*ARPHDR_ETHER*/, mac, sizeof(mac))); |
| 265 | } |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Zhang Shengju | 43367ef | 2015-08-12 06:03:23 +0000 | [diff] [blame] | 268 | static void bridge_print_help(struct link_util *lu, int argc, char **argv, |
| 269 | FILE *f) |
| 270 | { |
| 271 | print_explain(f); |
| 272 | } |
| 273 | |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 274 | struct link_util bridge_link_util = { |
| 275 | .id = "bridge", |
| 276 | .maxattr = IFLA_BR_MAX, |
| 277 | .parse_opt = bridge_parse_opt, |
| 278 | .print_opt = bridge_print_opt, |
Zhang Shengju | 43367ef | 2015-08-12 06:03:23 +0000 | [diff] [blame] | 279 | .print_help = bridge_print_help, |
Jiri Pirko | 28d84b4 | 2014-09-28 16:33:29 -0700 | [diff] [blame] | 280 | }; |