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