Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Bridge netlink control interface |
| 3 | * |
| 4 | * Authors: |
| 5 | * Stephen Hemminger <shemminger@osdl.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 15 | #include <linux/etherdevice.h> |
Thomas Graf | 32fe21c | 2007-03-22 11:59:03 -0700 | [diff] [blame] | 16 | #include <net/rtnetlink.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 17 | #include <net/net_namespace.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 18 | #include <net/sock.h> |
Roopa Prabhu | 68e331c | 2015-01-29 22:40:14 -0800 | [diff] [blame] | 19 | #include <net/switchdev.h> |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 20 | #include <uapi/linux/if_bridge.h> |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 21 | |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 22 | #include "br_private.h" |
Vitalii Demianets | b03b6dd | 2011-11-25 00:16:37 +0000 | [diff] [blame] | 23 | #include "br_private_stp.h" |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 24 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 25 | static int br_get_num_vlan_infos(const struct net_port_vlans *pv, |
| 26 | u32 filter_mask) |
| 27 | { |
| 28 | u16 vid_range_start = 0, vid_range_end = 0; |
| 29 | u16 vid_range_flags = 0; |
| 30 | u16 pvid, vid, flags; |
| 31 | int num_vlans = 0; |
| 32 | |
| 33 | if (filter_mask & RTEXT_FILTER_BRVLAN) |
| 34 | return pv->num_vlans; |
| 35 | |
| 36 | if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) |
| 37 | return 0; |
| 38 | |
| 39 | /* Count number of vlan info's |
| 40 | */ |
| 41 | pvid = br_get_pvid(pv); |
| 42 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
| 43 | flags = 0; |
| 44 | if (vid == pvid) |
| 45 | flags |= BRIDGE_VLAN_INFO_PVID; |
| 46 | |
| 47 | if (test_bit(vid, pv->untagged_bitmap)) |
| 48 | flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 49 | |
| 50 | if (vid_range_start == 0) { |
| 51 | goto initvars; |
| 52 | } else if ((vid - vid_range_end) == 1 && |
| 53 | flags == vid_range_flags) { |
| 54 | vid_range_end = vid; |
| 55 | continue; |
| 56 | } else { |
| 57 | if ((vid_range_end - vid_range_start) > 0) |
| 58 | num_vlans += 2; |
| 59 | else |
| 60 | num_vlans += 1; |
| 61 | } |
| 62 | initvars: |
| 63 | vid_range_start = vid; |
| 64 | vid_range_end = vid; |
| 65 | vid_range_flags = flags; |
| 66 | } |
| 67 | |
| 68 | if (vid_range_start != 0) { |
| 69 | if ((vid_range_end - vid_range_start) > 0) |
| 70 | num_vlans += 2; |
| 71 | else |
| 72 | num_vlans += 1; |
| 73 | } |
| 74 | |
| 75 | return num_vlans; |
| 76 | } |
| 77 | |
| 78 | static size_t br_get_link_af_size_filtered(const struct net_device *dev, |
| 79 | u32 filter_mask) |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 80 | { |
| 81 | struct net_port_vlans *pv; |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 82 | int num_vlan_infos; |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 83 | |
Johannes Berg | 2f56f6b | 2015-03-03 16:02:16 +0100 | [diff] [blame] | 84 | rcu_read_lock(); |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 85 | if (br_port_exists(dev)) |
Johannes Berg | 2f56f6b | 2015-03-03 16:02:16 +0100 | [diff] [blame] | 86 | pv = nbp_get_vlan_info(br_port_get_rcu(dev)); |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 87 | else if (dev->priv_flags & IFF_EBRIDGE) |
| 88 | pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev)); |
| 89 | else |
Johannes Berg | 2f56f6b | 2015-03-03 16:02:16 +0100 | [diff] [blame] | 90 | pv = NULL; |
| 91 | if (pv) |
| 92 | num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask); |
| 93 | else |
| 94 | num_vlan_infos = 0; |
| 95 | rcu_read_unlock(); |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 96 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 97 | if (!num_vlan_infos) |
| 98 | return 0; |
| 99 | |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 100 | /* Each VLAN is returned in bridge_vlan_info along with flags */ |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 101 | return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info)); |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 104 | static inline size_t br_port_info_size(void) |
| 105 | { |
| 106 | return nla_total_size(1) /* IFLA_BRPORT_STATE */ |
| 107 | + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */ |
| 108 | + nla_total_size(4) /* IFLA_BRPORT_COST */ |
| 109 | + nla_total_size(1) /* IFLA_BRPORT_MODE */ |
stephen hemminger | a2e01a6 | 2012-11-13 07:53:07 +0000 | [diff] [blame] | 110 | + nla_total_size(1) /* IFLA_BRPORT_GUARD */ |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 111 | + nla_total_size(1) /* IFLA_BRPORT_PROTECT */ |
stephen hemminger | 3da889b | 2013-03-11 13:52:17 +0000 | [diff] [blame] | 112 | + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */ |
Vlad Yasevich | 9ba1889 | 2013-06-05 10:08:00 -0400 | [diff] [blame] | 113 | + nla_total_size(1) /* IFLA_BRPORT_LEARNING */ |
Vlad Yasevich | 867a594 | 2013-06-05 10:08:01 -0400 | [diff] [blame] | 114 | + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */ |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 115 | + 0; |
| 116 | } |
| 117 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 118 | static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 119 | { |
| 120 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 121 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 122 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 123 | + nla_total_size(4) /* IFLA_MASTER */ |
| 124 | + nla_total_size(4) /* IFLA_MTU */ |
| 125 | + nla_total_size(4) /* IFLA_LINK */ |
| 126 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Roopa Prabhu | b7853d7 | 2015-02-21 20:21:51 -0800 | [diff] [blame] | 127 | + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */ |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 128 | + nla_total_size(br_get_link_af_size_filtered(dev, |
| 129 | filter_mask)); /* IFLA_AF_SPEC */ |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int br_port_fill_attrs(struct sk_buff *skb, |
| 133 | const struct net_bridge_port *p) |
| 134 | { |
| 135 | u8 mode = !!(p->flags & BR_HAIRPIN_MODE); |
| 136 | |
| 137 | if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) || |
| 138 | nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) || |
| 139 | nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) || |
stephen hemminger | a2e01a6 | 2012-11-13 07:53:07 +0000 | [diff] [blame] | 140 | nla_put_u8(skb, IFLA_BRPORT_MODE, mode) || |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 141 | nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) || |
David S. Miller | c2d3bab | 2012-12-05 16:24:45 -0500 | [diff] [blame] | 142 | nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) || |
Vlad Yasevich | 9ba1889 | 2013-06-05 10:08:00 -0400 | [diff] [blame] | 143 | nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) || |
Vlad Yasevich | 867a594 | 2013-06-05 10:08:01 -0400 | [diff] [blame] | 144 | nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) || |
Kyeyoon Park | 9585011 | 2014-10-23 14:49:17 -0700 | [diff] [blame] | 145 | nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) || |
Jouni Malinen | 842a9ae | 2015-03-04 12:54:21 +0200 | [diff] [blame] | 146 | nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) || |
| 147 | nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI, |
| 148 | !!(p->flags & BR_PROXYARP_WIFI))) |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 149 | return -EMSGSIZE; |
| 150 | |
| 151 | return 0; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 154 | static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start, |
| 155 | u16 vid_end, u16 flags) |
| 156 | { |
| 157 | struct bridge_vlan_info vinfo; |
| 158 | |
| 159 | if ((vid_end - vid_start) > 0) { |
| 160 | /* add range to skb */ |
| 161 | vinfo.vid = vid_start; |
| 162 | vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 163 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, |
| 164 | sizeof(vinfo), &vinfo)) |
| 165 | goto nla_put_failure; |
| 166 | |
| 167 | vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 168 | |
| 169 | vinfo.vid = vid_end; |
| 170 | vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END; |
| 171 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, |
| 172 | sizeof(vinfo), &vinfo)) |
| 173 | goto nla_put_failure; |
| 174 | } else { |
| 175 | vinfo.vid = vid_start; |
| 176 | vinfo.flags = flags; |
| 177 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, |
| 178 | sizeof(vinfo), &vinfo)) |
| 179 | goto nla_put_failure; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | nla_put_failure: |
| 185 | return -EMSGSIZE; |
| 186 | } |
| 187 | |
| 188 | static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb, |
| 189 | const struct net_port_vlans *pv) |
| 190 | { |
| 191 | u16 vid_range_start = 0, vid_range_end = 0; |
Roopa Prabhu | 0fe6de4 | 2015-01-12 16:25:28 -0800 | [diff] [blame] | 192 | u16 vid_range_flags = 0; |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 193 | u16 pvid, vid, flags; |
| 194 | int err = 0; |
| 195 | |
| 196 | /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan |
| 197 | * and mark vlan info with begin and end flags |
| 198 | * if vlaninfo represents a range |
| 199 | */ |
| 200 | pvid = br_get_pvid(pv); |
| 201 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
| 202 | flags = 0; |
| 203 | if (vid == pvid) |
| 204 | flags |= BRIDGE_VLAN_INFO_PVID; |
| 205 | |
| 206 | if (test_bit(vid, pv->untagged_bitmap)) |
| 207 | flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 208 | |
| 209 | if (vid_range_start == 0) { |
| 210 | goto initvars; |
| 211 | } else if ((vid - vid_range_end) == 1 && |
| 212 | flags == vid_range_flags) { |
| 213 | vid_range_end = vid; |
| 214 | continue; |
| 215 | } else { |
| 216 | err = br_fill_ifvlaninfo_range(skb, vid_range_start, |
| 217 | vid_range_end, |
| 218 | vid_range_flags); |
| 219 | if (err) |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | initvars: |
| 224 | vid_range_start = vid; |
| 225 | vid_range_end = vid; |
| 226 | vid_range_flags = flags; |
| 227 | } |
| 228 | |
Roopa Prabhu | 0fe6de4 | 2015-01-12 16:25:28 -0800 | [diff] [blame] | 229 | if (vid_range_start != 0) { |
| 230 | /* Call it once more to send any left over vlans */ |
| 231 | err = br_fill_ifvlaninfo_range(skb, vid_range_start, |
| 232 | vid_range_end, |
| 233 | vid_range_flags); |
| 234 | if (err) |
| 235 | return err; |
| 236 | } |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int br_fill_ifvlaninfo(struct sk_buff *skb, |
| 242 | const struct net_port_vlans *pv) |
| 243 | { |
| 244 | struct bridge_vlan_info vinfo; |
| 245 | u16 pvid, vid; |
| 246 | |
| 247 | pvid = br_get_pvid(pv); |
| 248 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
| 249 | vinfo.vid = vid; |
| 250 | vinfo.flags = 0; |
| 251 | if (vid == pvid) |
| 252 | vinfo.flags |= BRIDGE_VLAN_INFO_PVID; |
| 253 | |
| 254 | if (test_bit(vid, pv->untagged_bitmap)) |
| 255 | vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 256 | |
| 257 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, |
| 258 | sizeof(vinfo), &vinfo)) |
| 259 | goto nla_put_failure; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | |
| 264 | nla_put_failure: |
| 265 | return -EMSGSIZE; |
| 266 | } |
| 267 | |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Create one netlink message for one interface |
| 270 | * Contains port and master info as well as carrier and bridge state. |
| 271 | */ |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 272 | static int br_fill_ifinfo(struct sk_buff *skb, |
| 273 | const struct net_bridge_port *port, |
| 274 | u32 pid, u32 seq, int event, unsigned int flags, |
| 275 | u32 filter_mask, const struct net_device *dev) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 276 | { |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 277 | const struct net_bridge *br; |
Thomas Graf | 7468596 | 2006-11-20 16:20:22 -0800 | [diff] [blame] | 278 | struct ifinfomsg *hdr; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 279 | struct nlmsghdr *nlh; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 280 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 281 | |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 282 | if (port) |
| 283 | br = port->br; |
| 284 | else |
| 285 | br = netdev_priv(dev); |
| 286 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 287 | br_debug(br, "br_fill_info event %d port %s master %s\n", |
| 288 | event, dev->name, br->dev->name); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 289 | |
Thomas Graf | 7468596 | 2006-11-20 16:20:22 -0800 | [diff] [blame] | 290 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); |
| 291 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 292 | return -EMSGSIZE; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 293 | |
Thomas Graf | 7468596 | 2006-11-20 16:20:22 -0800 | [diff] [blame] | 294 | hdr = nlmsg_data(nlh); |
| 295 | hdr->ifi_family = AF_BRIDGE; |
| 296 | hdr->__ifi_pad = 0; |
| 297 | hdr->ifi_type = dev->type; |
| 298 | hdr->ifi_index = dev->ifindex; |
| 299 | hdr->ifi_flags = dev_get_flags(dev); |
| 300 | hdr->ifi_change = 0; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 301 | |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 302 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 303 | nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) || |
| 304 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 305 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
| 306 | (dev->addr_len && |
| 307 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 308 | (dev->ifindex != dev_get_iflink(dev) && |
| 309 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 310 | goto nla_put_failure; |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 311 | |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 312 | if (event == RTM_NEWLINK && port) { |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 313 | struct nlattr *nest |
| 314 | = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
| 315 | |
| 316 | if (nest == NULL || br_port_fill_attrs(skb, port) < 0) |
| 317 | goto nla_put_failure; |
| 318 | nla_nest_end(skb, nest); |
| 319 | } |
| 320 | |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 321 | /* Check if the VID information is requested */ |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 322 | if ((filter_mask & RTEXT_FILTER_BRVLAN) || |
| 323 | (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 324 | const struct net_port_vlans *pv; |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 325 | struct nlattr *af; |
| 326 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 327 | |
| 328 | if (port) |
| 329 | pv = nbp_get_vlan_info(port); |
| 330 | else |
| 331 | pv = br_get_vlan_info(br); |
| 332 | |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 333 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 334 | goto done; |
| 335 | |
| 336 | af = nla_nest_start(skb, IFLA_AF_SPEC); |
| 337 | if (!af) |
| 338 | goto nla_put_failure; |
| 339 | |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 340 | if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) |
| 341 | err = br_fill_ifvlaninfo_compressed(skb, pv); |
| 342 | else |
| 343 | err = br_fill_ifvlaninfo(skb, pv); |
| 344 | if (err) |
| 345 | goto nla_put_failure; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 346 | nla_nest_end(skb, af); |
| 347 | } |
| 348 | |
| 349 | done: |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 350 | nlmsg_end(skb, nlh); |
| 351 | return 0; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 352 | |
Thomas Graf | 7468596 | 2006-11-20 16:20:22 -0800 | [diff] [blame] | 353 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 354 | nlmsg_cancel(skb, nlh); |
| 355 | return -EMSGSIZE; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Notify listeners of a change in port information |
| 360 | */ |
| 361 | void br_ifinfo_notify(int event, struct net_bridge_port *port) |
| 362 | { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 363 | struct net *net; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 364 | struct sk_buff *skb; |
Thomas Graf | 280a306 | 2006-08-15 00:36:28 -0700 | [diff] [blame] | 365 | int err = -ENOBUFS; |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 366 | u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 367 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 368 | if (!port) |
| 369 | return; |
| 370 | |
| 371 | net = dev_net(port->dev); |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 372 | br_debug(port->br, "port %u(%s) event %d\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 373 | (unsigned int)port->port_no, port->dev->name, event); |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 374 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 375 | skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC); |
Thomas Graf | 280a306 | 2006-08-15 00:36:28 -0700 | [diff] [blame] | 376 | if (skb == NULL) |
| 377 | goto errout; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 378 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 379 | err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 380 | if (err < 0) { |
| 381 | /* -EMSGSIZE implies BUG in br_nlmsg_size() */ |
| 382 | WARN_ON(err == -EMSGSIZE); |
| 383 | kfree_skb(skb); |
| 384 | goto errout; |
| 385 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 386 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 387 | return; |
Thomas Graf | 280a306 | 2006-08-15 00:36:28 -0700 | [diff] [blame] | 388 | errout: |
tanxiaojun | 87e823b | 2013-12-19 13:28:10 +0800 | [diff] [blame] | 389 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 392 | |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Dump information about all ports, in response to GETLINK |
| 395 | */ |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 396 | int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 397 | struct net_device *dev, u32 filter_mask, int nlflags) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 398 | { |
Hong Zhiguo | 1fb1754 | 2013-09-14 22:42:27 +0800 | [diff] [blame] | 399 | struct net_bridge_port *port = br_port_get_rtnl(dev); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 400 | |
Roopa Prabhu | 36cd0ff | 2015-01-10 07:31:14 -0800 | [diff] [blame] | 401 | if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) && |
| 402 | !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) |
Dan Carpenter | 1b846f9 | 2015-01-21 12:22:35 +0300 | [diff] [blame] | 403 | return 0; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 404 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 405 | return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags, |
Dan Carpenter | 1b846f9 | 2015-01-21 12:22:35 +0300 | [diff] [blame] | 406 | filter_mask, dev); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 409 | static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p, |
| 410 | int cmd, struct bridge_vlan_info *vinfo) |
| 411 | { |
| 412 | int err = 0; |
| 413 | |
| 414 | switch (cmd) { |
| 415 | case RTM_SETLINK: |
| 416 | if (p) { |
| 417 | err = nbp_vlan_add(p, vinfo->vid, vinfo->flags); |
| 418 | if (err) |
| 419 | break; |
| 420 | |
| 421 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) |
| 422 | err = br_vlan_add(p->br, vinfo->vid, |
| 423 | vinfo->flags); |
| 424 | } else { |
| 425 | err = br_vlan_add(br, vinfo->vid, vinfo->flags); |
| 426 | } |
| 427 | break; |
| 428 | |
| 429 | case RTM_DELLINK: |
| 430 | if (p) { |
| 431 | nbp_vlan_delete(p, vinfo->vid); |
| 432 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) |
| 433 | br_vlan_delete(p->br, vinfo->vid); |
| 434 | } else { |
| 435 | br_vlan_delete(br, vinfo->vid); |
| 436 | } |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | return err; |
| 441 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 442 | |
| 443 | static int br_afspec(struct net_bridge *br, |
| 444 | struct net_bridge_port *p, |
| 445 | struct nlattr *af_spec, |
| 446 | int cmd) |
| 447 | { |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 448 | struct bridge_vlan_info *vinfo_start = NULL; |
| 449 | struct bridge_vlan_info *vinfo = NULL; |
| 450 | struct nlattr *attr; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 451 | int err = 0; |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 452 | int rem; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 453 | |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 454 | nla_for_each_nested(attr, af_spec, rem) { |
| 455 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 456 | continue; |
| 457 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 458 | return -EINVAL; |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 459 | vinfo = nla_data(attr); |
Nikolay Aleksandrov | 462e1ea | 2015-07-02 05:48:17 -0700 | [diff] [blame] | 460 | if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK) |
| 461 | return -EINVAL; |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 462 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
| 463 | if (vinfo_start) |
| 464 | return -EINVAL; |
| 465 | vinfo_start = vinfo; |
| 466 | continue; |
| 467 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 468 | |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 469 | if (vinfo_start) { |
| 470 | struct bridge_vlan_info tmp_vinfo; |
| 471 | int v; |
| 472 | |
| 473 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | if (vinfo->vid <= vinfo_start->vid) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | memcpy(&tmp_vinfo, vinfo_start, |
| 480 | sizeof(struct bridge_vlan_info)); |
| 481 | |
| 482 | for (v = vinfo_start->vid; v <= vinfo->vid; v++) { |
| 483 | tmp_vinfo.vid = v; |
| 484 | err = br_vlan_info(br, p, cmd, &tmp_vinfo); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 485 | if (err) |
| 486 | break; |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 487 | } |
| 488 | vinfo_start = NULL; |
| 489 | } else { |
| 490 | err = br_vlan_info(br, p, cmd, vinfo); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 491 | } |
Roopa Prabhu | bdced7e | 2015-01-10 07:31:12 -0800 | [diff] [blame] | 492 | if (err) |
| 493 | break; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | return err; |
| 497 | } |
| 498 | |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 499 | static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = { |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 500 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 501 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 502 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 503 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
stephen hemminger | a2e01a6 | 2012-11-13 07:53:07 +0000 | [diff] [blame] | 504 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 505 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
Thomas Graf | 6f705d8 | 2014-11-26 13:42:19 +0100 | [diff] [blame] | 506 | [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 }, |
Vlad Yasevich | 9ba1889 | 2013-06-05 10:08:00 -0400 | [diff] [blame] | 507 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
Vlad Yasevich | 867a594 | 2013-06-05 10:08:01 -0400 | [diff] [blame] | 508 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | /* Change the state of the port and notify spanning tree */ |
| 512 | static int br_set_port_state(struct net_bridge_port *p, u8 state) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 513 | { |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 514 | if (state > BR_STATE_BLOCKING) |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 515 | return -EINVAL; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 516 | |
| 517 | /* if kernel STP is running, don't allow changes */ |
Stephen Hemminger | 9cde070 | 2007-03-21 14:22:44 -0700 | [diff] [blame] | 518 | if (p->br->stp_enabled == BR_KERNEL_STP) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 519 | return -EBUSY; |
| 520 | |
stephen hemminger | 576eb62 | 2012-12-28 18:15:22 +0000 | [diff] [blame] | 521 | /* if device is not up, change is not allowed |
| 522 | * if link is not present, only allowable state is disabled |
| 523 | */ |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 524 | if (!netif_running(p->dev) || |
stephen hemminger | 576eb62 | 2012-12-28 18:15:22 +0000 | [diff] [blame] | 525 | (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED)) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 526 | return -ENETDOWN; |
| 527 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 528 | br_set_state(p, state); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 529 | br_log_state(p); |
Vitalii Demianets | b03b6dd | 2011-11-25 00:16:37 +0000 | [diff] [blame] | 530 | br_port_state_selection(p->br); |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 534 | /* Set/clear or port flags based on attribute */ |
| 535 | static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[], |
| 536 | int attrtype, unsigned long mask) |
| 537 | { |
| 538 | if (tb[attrtype]) { |
| 539 | u8 flag = nla_get_u8(tb[attrtype]); |
| 540 | if (flag) |
| 541 | p->flags |= mask; |
| 542 | else |
| 543 | p->flags &= ~mask; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* Process bridge protocol info on port */ |
| 548 | static int br_setport(struct net_bridge_port *p, struct nlattr *tb[]) |
| 549 | { |
| 550 | int err; |
Vlad Yasevich | e028e4b | 2014-05-16 09:59:16 -0400 | [diff] [blame] | 551 | unsigned long old_flags = p->flags; |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 552 | |
| 553 | br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE); |
stephen hemminger | a2e01a6 | 2012-11-13 07:53:07 +0000 | [diff] [blame] | 554 | br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD); |
David S. Miller | c2d3bab | 2012-12-05 16:24:45 -0500 | [diff] [blame] | 555 | br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE); |
Vlad Yasevich | 3d84fa9 | 2013-03-15 06:39:12 +0000 | [diff] [blame] | 556 | br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK); |
Vlad Yasevich | 9ba1889 | 2013-06-05 10:08:00 -0400 | [diff] [blame] | 557 | br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING); |
Vlad Yasevich | 867a594 | 2013-06-05 10:08:01 -0400 | [diff] [blame] | 558 | br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD); |
Kyeyoon Park | 9585011 | 2014-10-23 14:49:17 -0700 | [diff] [blame] | 559 | br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP); |
Jouni Malinen | 842a9ae | 2015-03-04 12:54:21 +0200 | [diff] [blame] | 560 | br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI); |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 561 | |
| 562 | if (tb[IFLA_BRPORT_COST]) { |
| 563 | err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); |
| 564 | if (err) |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | if (tb[IFLA_BRPORT_PRIORITY]) { |
| 569 | err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY])); |
| 570 | if (err) |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | if (tb[IFLA_BRPORT_STATE]) { |
| 575 | err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE])); |
| 576 | if (err) |
| 577 | return err; |
| 578 | } |
Vlad Yasevich | e028e4b | 2014-05-16 09:59:16 -0400 | [diff] [blame] | 579 | |
| 580 | br_port_flags_change(p, old_flags ^ p->flags); |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* Change state and parameters on port. */ |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 585 | int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags) |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 586 | { |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 587 | struct nlattr *protinfo; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 588 | struct nlattr *afspec; |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 589 | struct net_bridge_port *p; |
Dan Carpenter | 2062cc2 | 2012-12-07 01:10:46 +0000 | [diff] [blame] | 590 | struct nlattr *tb[IFLA_BRPORT_MAX + 1]; |
Scott Feldman | 41c498b | 2015-05-10 09:47:59 -0700 | [diff] [blame] | 591 | int err = 0; |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 592 | |
Hong zhi guo | c60ee67 | 2013-03-28 06:21:22 +0000 | [diff] [blame] | 593 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO); |
| 594 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 595 | if (!protinfo && !afspec) |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | |
| 598 | p = br_port_get_rtnl(dev); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 599 | /* We want to accept dev as bridge itself if the AF_SPEC |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 600 | * is set to see if someone is setting vlan info on the bridge |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 601 | */ |
Hong zhi guo | 7b99a99 | 2013-03-24 03:26:47 +0000 | [diff] [blame] | 602 | if (!p && !afspec) |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 603 | return -EINVAL; |
| 604 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 605 | if (p && protinfo) { |
| 606 | if (protinfo->nla_type & NLA_F_NESTED) { |
| 607 | err = nla_parse_nested(tb, IFLA_BRPORT_MAX, |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 608 | protinfo, br_port_policy); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 609 | if (err) |
| 610 | return err; |
| 611 | |
| 612 | spin_lock_bh(&p->br->lock); |
| 613 | err = br_setport(p, tb); |
| 614 | spin_unlock_bh(&p->br->lock); |
| 615 | } else { |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 616 | /* Binary compatibility with old RSTP */ |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 617 | if (nla_len(protinfo) < sizeof(u8)) |
| 618 | return -EINVAL; |
| 619 | |
| 620 | spin_lock_bh(&p->br->lock); |
| 621 | err = br_set_port_state(p, nla_get_u8(protinfo)); |
| 622 | spin_unlock_bh(&p->br->lock); |
| 623 | } |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 624 | if (err) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 625 | goto out; |
| 626 | } |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 627 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 628 | if (afspec) { |
| 629 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, |
| 630 | afspec, RTM_SETLINK); |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | if (err == 0) |
| 634 | br_ifinfo_notify(RTM_NEWLINK, p); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 635 | out: |
stephen hemminger | 25c71c7 | 2012-11-13 07:53:05 +0000 | [diff] [blame] | 636 | return err; |
| 637 | } |
| 638 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 639 | /* Delete port information */ |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 640 | int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 641 | { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 642 | struct nlattr *afspec; |
| 643 | struct net_bridge_port *p; |
Scott Feldman | 8508025 | 2015-05-10 09:48:03 -0700 | [diff] [blame] | 644 | int err = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 645 | |
Hong zhi guo | c60ee67 | 2013-03-28 06:21:22 +0000 | [diff] [blame] | 646 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 647 | if (!afspec) |
| 648 | return 0; |
| 649 | |
| 650 | p = br_port_get_rtnl(dev); |
| 651 | /* We want to accept dev as bridge itself as well */ |
| 652 | if (!p && !(dev->priv_flags & IFF_EBRIDGE)) |
| 653 | return -EINVAL; |
| 654 | |
| 655 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, |
| 656 | afspec, RTM_DELLINK); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 657 | if (err == 0) |
| 658 | /* Send RTM_NEWLINK because userspace |
| 659 | * expects RTM_NEWLINK for vlan dels |
| 660 | */ |
| 661 | br_ifinfo_notify(RTM_NEWLINK, p); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 662 | |
| 663 | return err; |
| 664 | } |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 665 | static int br_validate(struct nlattr *tb[], struct nlattr *data[]) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 666 | { |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 667 | if (tb[IFLA_ADDRESS]) { |
| 668 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 669 | return -EINVAL; |
| 670 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 671 | return -EADDRNOTAVAIL; |
| 672 | } |
Thomas Graf | 32fe21c | 2007-03-22 11:59:03 -0700 | [diff] [blame] | 673 | |
| 674 | return 0; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Toshiaki Makita | 30313a3 | 2014-04-25 17:01:18 +0900 | [diff] [blame] | 677 | static int br_dev_newlink(struct net *src_net, struct net_device *dev, |
| 678 | struct nlattr *tb[], struct nlattr *data[]) |
| 679 | { |
| 680 | struct net_bridge *br = netdev_priv(dev); |
| 681 | |
| 682 | if (tb[IFLA_ADDRESS]) { |
| 683 | spin_lock_bh(&br->lock); |
| 684 | br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS])); |
| 685 | spin_unlock_bh(&br->lock); |
| 686 | } |
| 687 | |
| 688 | return register_netdevice(dev); |
| 689 | } |
| 690 | |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 691 | static int br_port_slave_changelink(struct net_device *brdev, |
| 692 | struct net_device *dev, |
| 693 | struct nlattr *tb[], |
| 694 | struct nlattr *data[]) |
| 695 | { |
Nikolay Aleksandrov | 963ad94 | 2015-07-22 13:03:40 +0200 | [diff] [blame] | 696 | struct net_bridge *br = netdev_priv(brdev); |
| 697 | int ret; |
| 698 | |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 699 | if (!data) |
| 700 | return 0; |
Nikolay Aleksandrov | 963ad94 | 2015-07-22 13:03:40 +0200 | [diff] [blame] | 701 | |
| 702 | spin_lock_bh(&br->lock); |
| 703 | ret = br_setport(br_port_get_rtnl(dev), data); |
| 704 | spin_unlock_bh(&br->lock); |
| 705 | |
| 706 | return ret; |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Jiri Pirko | ced8283 | 2014-09-05 15:51:29 +0200 | [diff] [blame] | 709 | static int br_port_fill_slave_info(struct sk_buff *skb, |
| 710 | const struct net_device *brdev, |
| 711 | const struct net_device *dev) |
| 712 | { |
| 713 | return br_port_fill_attrs(skb, br_port_get_rtnl(dev)); |
| 714 | } |
| 715 | |
| 716 | static size_t br_port_get_slave_size(const struct net_device *brdev, |
| 717 | const struct net_device *dev) |
| 718 | { |
| 719 | return br_port_info_size(); |
| 720 | } |
| 721 | |
Jiri Pirko | 1332351 | 2014-09-05 15:51:32 +0200 | [diff] [blame] | 722 | static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = { |
| 723 | [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 }, |
| 724 | [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 }, |
| 725 | [IFLA_BR_MAX_AGE] = { .type = NLA_U32 }, |
Jörg Thalheim | af61576 | 2015-03-18 10:06:58 +0100 | [diff] [blame] | 726 | [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 }, |
| 727 | [IFLA_BR_STP_STATE] = { .type = NLA_U32 }, |
| 728 | [IFLA_BR_PRIORITY] = { .type = NLA_U16 }, |
Jiri Pirko | 1332351 | 2014-09-05 15:51:32 +0200 | [diff] [blame] | 729 | }; |
| 730 | |
| 731 | static int br_changelink(struct net_device *brdev, struct nlattr *tb[], |
| 732 | struct nlattr *data[]) |
| 733 | { |
| 734 | struct net_bridge *br = netdev_priv(brdev); |
| 735 | int err; |
| 736 | |
| 737 | if (!data) |
| 738 | return 0; |
| 739 | |
| 740 | if (data[IFLA_BR_FORWARD_DELAY]) { |
| 741 | err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY])); |
| 742 | if (err) |
| 743 | return err; |
| 744 | } |
| 745 | |
| 746 | if (data[IFLA_BR_HELLO_TIME]) { |
| 747 | err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME])); |
| 748 | if (err) |
| 749 | return err; |
| 750 | } |
| 751 | |
| 752 | if (data[IFLA_BR_MAX_AGE]) { |
| 753 | err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE])); |
| 754 | if (err) |
| 755 | return err; |
| 756 | } |
| 757 | |
Jörg Thalheim | af61576 | 2015-03-18 10:06:58 +0100 | [diff] [blame] | 758 | if (data[IFLA_BR_AGEING_TIME]) { |
| 759 | u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]); |
| 760 | |
| 761 | br->ageing_time = clock_t_to_jiffies(ageing_time); |
| 762 | } |
| 763 | |
| 764 | if (data[IFLA_BR_STP_STATE]) { |
| 765 | u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]); |
| 766 | |
| 767 | br_stp_set_enabled(br, stp_enabled); |
| 768 | } |
| 769 | |
| 770 | if (data[IFLA_BR_PRIORITY]) { |
| 771 | u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]); |
| 772 | |
| 773 | br_stp_set_bridge_priority(br, priority); |
| 774 | } |
| 775 | |
Jiri Pirko | 1332351 | 2014-09-05 15:51:32 +0200 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Jiri Pirko | e5c3ea5 | 2014-09-05 15:51:31 +0200 | [diff] [blame] | 779 | static size_t br_get_size(const struct net_device *brdev) |
| 780 | { |
| 781 | return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */ |
| 782 | nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */ |
| 783 | nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */ |
Jörg Thalheim | af61576 | 2015-03-18 10:06:58 +0100 | [diff] [blame] | 784 | nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */ |
| 785 | nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */ |
| 786 | nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */ |
Jiri Pirko | e5c3ea5 | 2014-09-05 15:51:31 +0200 | [diff] [blame] | 787 | 0; |
| 788 | } |
| 789 | |
| 790 | static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev) |
| 791 | { |
| 792 | struct net_bridge *br = netdev_priv(brdev); |
| 793 | u32 forward_delay = jiffies_to_clock_t(br->forward_delay); |
| 794 | u32 hello_time = jiffies_to_clock_t(br->hello_time); |
| 795 | u32 age_time = jiffies_to_clock_t(br->max_age); |
Jörg Thalheim | af61576 | 2015-03-18 10:06:58 +0100 | [diff] [blame] | 796 | u32 ageing_time = jiffies_to_clock_t(br->ageing_time); |
| 797 | u32 stp_enabled = br->stp_enabled; |
| 798 | u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]; |
Jiri Pirko | e5c3ea5 | 2014-09-05 15:51:31 +0200 | [diff] [blame] | 799 | |
| 800 | if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) || |
| 801 | nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) || |
Jörg Thalheim | af61576 | 2015-03-18 10:06:58 +0100 | [diff] [blame] | 802 | nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) || |
| 803 | nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) || |
| 804 | nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) || |
| 805 | nla_put_u16(skb, IFLA_BR_PRIORITY, priority)) |
Jiri Pirko | e5c3ea5 | 2014-09-05 15:51:31 +0200 | [diff] [blame] | 806 | return -EMSGSIZE; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Roopa Prabhu | fed0a15 | 2015-02-25 23:55:40 -0800 | [diff] [blame] | 811 | static size_t br_get_link_af_size(const struct net_device *dev) |
| 812 | { |
| 813 | struct net_port_vlans *pv; |
| 814 | |
| 815 | if (br_port_exists(dev)) |
| 816 | pv = nbp_get_vlan_info(br_port_get_rtnl(dev)); |
| 817 | else if (dev->priv_flags & IFF_EBRIDGE) |
| 818 | pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev)); |
| 819 | else |
| 820 | return 0; |
| 821 | |
| 822 | if (!pv) |
| 823 | return 0; |
| 824 | |
| 825 | /* Each VLAN is returned in bridge_vlan_info along with flags */ |
| 826 | return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info)); |
| 827 | } |
| 828 | |
Daniel Borkmann | 207895f | 2015-01-29 12:15:03 +0100 | [diff] [blame] | 829 | static struct rtnl_af_ops br_af_ops __read_mostly = { |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 830 | .family = AF_BRIDGE, |
| 831 | .get_link_af_size = br_get_link_af_size, |
| 832 | }; |
| 833 | |
stephen hemminger | 149ddd8 | 2012-06-26 05:48:45 +0000 | [diff] [blame] | 834 | struct rtnl_link_ops br_link_ops __read_mostly = { |
Jiri Pirko | ced8283 | 2014-09-05 15:51:29 +0200 | [diff] [blame] | 835 | .kind = "bridge", |
| 836 | .priv_size = sizeof(struct net_bridge), |
| 837 | .setup = br_dev_setup, |
Jiri Pirko | 1332351 | 2014-09-05 15:51:32 +0200 | [diff] [blame] | 838 | .maxtype = IFLA_BRPORT_MAX, |
| 839 | .policy = br_policy, |
Jiri Pirko | ced8283 | 2014-09-05 15:51:29 +0200 | [diff] [blame] | 840 | .validate = br_validate, |
| 841 | .newlink = br_dev_newlink, |
Jiri Pirko | 1332351 | 2014-09-05 15:51:32 +0200 | [diff] [blame] | 842 | .changelink = br_changelink, |
Jiri Pirko | ced8283 | 2014-09-05 15:51:29 +0200 | [diff] [blame] | 843 | .dellink = br_dev_delete, |
Jiri Pirko | e5c3ea5 | 2014-09-05 15:51:31 +0200 | [diff] [blame] | 844 | .get_size = br_get_size, |
| 845 | .fill_info = br_fill_info, |
Jiri Pirko | 3ac636b | 2014-09-05 15:51:30 +0200 | [diff] [blame] | 846 | |
| 847 | .slave_maxtype = IFLA_BRPORT_MAX, |
| 848 | .slave_policy = br_port_policy, |
| 849 | .slave_changelink = br_port_slave_changelink, |
Jiri Pirko | ced8283 | 2014-09-05 15:51:29 +0200 | [diff] [blame] | 850 | .get_slave_size = br_port_get_slave_size, |
| 851 | .fill_slave_info = br_port_fill_slave_info, |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | int __init br_netlink_init(void) |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 855 | { |
Vlad Yasevich | 3ec8e9f | 2013-01-02 09:41:25 +0000 | [diff] [blame] | 856 | int err; |
| 857 | |
| 858 | br_mdb_init(); |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 859 | rtnl_af_register(&br_af_ops); |
Vlad Yasevich | 3ec8e9f | 2013-01-02 09:41:25 +0000 | [diff] [blame] | 860 | |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 861 | err = rtnl_link_register(&br_link_ops); |
| 862 | if (err) |
| 863 | goto out_af; |
| 864 | |
Vlad Yasevich | 3ec8e9f | 2013-01-02 09:41:25 +0000 | [diff] [blame] | 865 | return 0; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 866 | |
| 867 | out_af: |
| 868 | rtnl_af_unregister(&br_af_ops); |
Vlad Yasevich | 3ec8e9f | 2013-01-02 09:41:25 +0000 | [diff] [blame] | 869 | br_mdb_uninit(); |
| 870 | return err; |
Stephen Hemminger | 11dc1f3 | 2006-05-25 16:00:12 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Pablo Neira Ayuso | 34666d4 | 2014-09-18 11:29:03 +0200 | [diff] [blame] | 873 | void br_netlink_fini(void) |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 874 | { |
Vlad Yasevich | 3ec8e9f | 2013-01-02 09:41:25 +0000 | [diff] [blame] | 875 | br_mdb_uninit(); |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 876 | rtnl_af_unregister(&br_af_ops); |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 877 | rtnl_link_unregister(&br_link_ops); |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 878 | } |