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