1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Bridge netlink control interface 4 * 5 * Authors: 6 * Stephen Hemminger <shemminger@osdl.org> 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/slab.h> 11 #include <linux/etherdevice.h> 12 #include <net/rtnetlink.h> 13 #include <net/net_namespace.h> 14 #include <net/sock.h> 15 #include <uapi/linux/if_bridge.h> 16 17 #include "br_private.h" 18 #include "br_private_stp.h" 19 #include "br_private_cfm.h" 20 #include "br_private_tunnel.h" 21 #include "br_private_mcast_eht.h" 22 23 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg, 24 u32 filter_mask) 25 { 26 struct net_bridge_vlan *v; 27 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0; 28 u16 flags, pvid; 29 int num_vlans = 0; 30 31 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) 32 return 0; 33 34 pvid = br_get_pvid(vg); 35 /* Count number of vlan infos */ 36 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) { 37 flags = 0; 38 /* only a context, bridge vlan not activated */ 39 if (!br_vlan_should_use(v)) 40 continue; 41 if (v->vid == pvid) 42 flags |= BRIDGE_VLAN_INFO_PVID; 43 44 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) 45 flags |= BRIDGE_VLAN_INFO_UNTAGGED; 46 47 if (vid_range_start == 0) { 48 goto initvars; 49 } else if ((v->vid - vid_range_end) == 1 && 50 flags == vid_range_flags) { 51 vid_range_end = v->vid; 52 continue; 53 } else { 54 if ((vid_range_end - vid_range_start) > 0) 55 num_vlans += 2; 56 else 57 num_vlans += 1; 58 } 59 initvars: 60 vid_range_start = v->vid; 61 vid_range_end = v->vid; 62 vid_range_flags = flags; 63 } 64 65 if (vid_range_start != 0) { 66 if ((vid_range_end - vid_range_start) > 0) 67 num_vlans += 2; 68 else 69 num_vlans += 1; 70 } 71 72 return num_vlans; 73 } 74 75 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg, 76 u32 filter_mask) 77 { 78 int num_vlans; 79 80 if (!vg) 81 return 0; 82 83 if (filter_mask & RTEXT_FILTER_BRVLAN) 84 return vg->num_vlans; 85 86 rcu_read_lock(); 87 num_vlans = __get_num_vlan_infos(vg, filter_mask); 88 rcu_read_unlock(); 89 90 return num_vlans; 91 } 92 93 static size_t br_get_link_af_size_filtered(const struct net_device *dev, 94 u32 filter_mask) 95 { 96 struct net_bridge_vlan_group *vg = NULL; 97 struct net_bridge_port *p = NULL; 98 struct net_bridge *br = NULL; 99 u32 num_cfm_peer_mep_infos; 100 u32 num_cfm_mep_infos; 101 size_t vinfo_sz = 0; 102 int num_vlan_infos; 103 104 rcu_read_lock(); 105 if (netif_is_bridge_port(dev)) { 106 p = br_port_get_check_rcu(dev); 107 if (p) 108 vg = nbp_vlan_group_rcu(p); 109 } else if (netif_is_bridge_master(dev)) { 110 br = netdev_priv(dev); 111 vg = br_vlan_group_rcu(br); 112 } 113 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask); 114 rcu_read_unlock(); 115 116 if (p && (p->flags & BR_VLAN_TUNNEL)) 117 vinfo_sz += br_get_vlan_tunnel_info_size(vg); 118 119 /* Each VLAN is returned in bridge_vlan_info along with flags */ 120 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info)); 121 122 if (p && vg && (filter_mask & RTEXT_FILTER_MST)) 123 vinfo_sz += br_mst_info_size(vg); 124 125 if (!(filter_mask & RTEXT_FILTER_CFM_STATUS)) 126 return vinfo_sz; 127 128 if (!br) 129 return vinfo_sz; 130 131 /* CFM status info must be added */ 132 br_cfm_mep_count(br, &num_cfm_mep_infos); 133 br_cfm_peer_mep_count(br, &num_cfm_peer_mep_infos); 134 135 vinfo_sz += nla_total_size(0); /* IFLA_BRIDGE_CFM */ 136 /* For each status struct the MEP instance (u32) is added */ 137 /* MEP instance (u32) + br_cfm_mep_status */ 138 vinfo_sz += num_cfm_mep_infos * 139 /*IFLA_BRIDGE_CFM_MEP_STATUS_INSTANCE */ 140 (nla_total_size(sizeof(u32)) 141 /* IFLA_BRIDGE_CFM_MEP_STATUS_OPCODE_UNEXP_SEEN */ 142 + nla_total_size(sizeof(u32)) 143 /* IFLA_BRIDGE_CFM_MEP_STATUS_VERSION_UNEXP_SEEN */ 144 + nla_total_size(sizeof(u32)) 145 /* IFLA_BRIDGE_CFM_MEP_STATUS_RX_LEVEL_LOW_SEEN */ 146 + nla_total_size(sizeof(u32))); 147 /* MEP instance (u32) + br_cfm_cc_peer_status */ 148 vinfo_sz += num_cfm_peer_mep_infos * 149 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE */ 150 (nla_total_size(sizeof(u32)) 151 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID */ 152 + nla_total_size(sizeof(u32)) 153 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT */ 154 + nla_total_size(sizeof(u32)) 155 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_RDI */ 156 + nla_total_size(sizeof(u32)) 157 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PORT_TLV_VALUE */ 158 + nla_total_size(sizeof(u8)) 159 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_IF_TLV_VALUE */ 160 + nla_total_size(sizeof(u8)) 161 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEEN */ 162 + nla_total_size(sizeof(u32)) 163 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_TLV_SEEN */ 164 + nla_total_size(sizeof(u32)) 165 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEQ_UNEXP_SEEN */ 166 + nla_total_size(sizeof(u32))); 167 168 return vinfo_sz; 169 } 170 171 static inline size_t br_port_info_size(void) 172 { 173 return nla_total_size(1) /* IFLA_BRPORT_STATE */ 174 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */ 175 + nla_total_size(4) /* IFLA_BRPORT_COST */ 176 + nla_total_size(1) /* IFLA_BRPORT_MODE */ 177 + nla_total_size(1) /* IFLA_BRPORT_GUARD */ 178 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */ 179 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */ 180 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */ 181 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */ 182 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */ 183 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */ 184 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */ 185 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */ 186 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */ 187 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */ 188 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */ 189 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */ 190 + nla_total_size(1) /* IFLA_BRPORT_LOCKED */ 191 + nla_total_size(1) /* IFLA_BRPORT_MAB */ 192 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */ 193 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */ 194 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */ 195 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */ 196 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */ 197 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */ 198 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */ 199 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */ 200 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */ 201 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */ 202 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */ 203 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 204 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */ 205 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_N_GROUPS */ 206 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_MAX_GROUPS */ 207 #endif 208 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */ 209 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_RING_OPEN */ 210 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_IN_OPEN */ 211 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT */ 212 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_CNT */ 213 + 0; 214 } 215 216 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask) 217 { 218 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 219 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 220 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 221 + nla_total_size(4) /* IFLA_MASTER */ 222 + nla_total_size(4) /* IFLA_MTU */ 223 + nla_total_size(4) /* IFLA_LINK */ 224 + nla_total_size(1) /* IFLA_OPERSTATE */ 225 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */ 226 + nla_total_size(br_get_link_af_size_filtered(dev, 227 filter_mask)) /* IFLA_AF_SPEC */ 228 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */ 229 } 230 231 static int br_port_fill_attrs(struct sk_buff *skb, 232 const struct net_bridge_port *p) 233 { 234 u8 mode = !!(p->flags & BR_HAIRPIN_MODE); 235 struct net_bridge_port *backup_p; 236 u64 timerval; 237 238 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) || 239 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) || 240 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) || 241 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) || 242 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) || 243 nla_put_u8(skb, IFLA_BRPORT_PROTECT, 244 !!(p->flags & BR_ROOT_BLOCK)) || 245 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, 246 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) || 247 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST, 248 !!(p->flags & BR_MULTICAST_TO_UNICAST)) || 249 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) || 250 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, 251 !!(p->flags & BR_FLOOD)) || 252 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD, 253 !!(p->flags & BR_MCAST_FLOOD)) || 254 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD, 255 !!(p->flags & BR_BCAST_FLOOD)) || 256 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) || 257 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI, 258 !!(p->flags & BR_PROXYARP_WIFI)) || 259 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id), 260 &p->designated_root) || 261 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id), 262 &p->designated_bridge) || 263 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) || 264 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) || 265 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) || 266 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) || 267 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK, 268 p->topology_change_ack) || 269 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) || 270 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags & 271 BR_VLAN_TUNNEL)) || 272 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) || 273 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS, 274 !!(p->flags & BR_NEIGH_SUPPRESS)) || 275 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags & 276 BR_MRP_LOST_CONT)) || 277 nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN, 278 !!(p->flags & BR_MRP_LOST_IN_CONT)) || 279 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)) || 280 nla_put_u8(skb, IFLA_BRPORT_LOCKED, !!(p->flags & BR_PORT_LOCKED)) || 281 nla_put_u8(skb, IFLA_BRPORT_MAB, !!(p->flags & BR_PORT_MAB))) 282 return -EMSGSIZE; 283 284 timerval = br_timer_value(&p->message_age_timer); 285 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval, 286 IFLA_BRPORT_PAD)) 287 return -EMSGSIZE; 288 timerval = br_timer_value(&p->forward_delay_timer); 289 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval, 290 IFLA_BRPORT_PAD)) 291 return -EMSGSIZE; 292 timerval = br_timer_value(&p->hold_timer); 293 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval, 294 IFLA_BRPORT_PAD)) 295 return -EMSGSIZE; 296 297 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 298 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER, 299 p->multicast_ctx.multicast_router) || 300 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT, 301 p->multicast_eht_hosts_limit) || 302 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_CNT, 303 p->multicast_eht_hosts_cnt) || 304 nla_put_u32(skb, IFLA_BRPORT_MCAST_N_GROUPS, 305 br_multicast_ngroups_get(&p->multicast_ctx)) || 306 nla_put_u32(skb, IFLA_BRPORT_MCAST_MAX_GROUPS, 307 br_multicast_ngroups_get_max(&p->multicast_ctx))) 308 return -EMSGSIZE; 309 #endif 310 311 /* we might be called only with br->lock */ 312 rcu_read_lock(); 313 backup_p = rcu_dereference(p->backup_port); 314 if (backup_p) 315 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT, 316 backup_p->dev->ifindex); 317 rcu_read_unlock(); 318 319 return 0; 320 } 321 322 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start, 323 u16 vid_end, u16 flags) 324 { 325 struct bridge_vlan_info vinfo; 326 327 if ((vid_end - vid_start) > 0) { 328 /* add range to skb */ 329 vinfo.vid = vid_start; 330 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN; 331 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, 332 sizeof(vinfo), &vinfo)) 333 goto nla_put_failure; 334 335 vinfo.vid = vid_end; 336 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END; 337 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, 338 sizeof(vinfo), &vinfo)) 339 goto nla_put_failure; 340 } else { 341 vinfo.vid = vid_start; 342 vinfo.flags = flags; 343 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, 344 sizeof(vinfo), &vinfo)) 345 goto nla_put_failure; 346 } 347 348 return 0; 349 350 nla_put_failure: 351 return -EMSGSIZE; 352 } 353 354 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb, 355 struct net_bridge_vlan_group *vg) 356 { 357 struct net_bridge_vlan *v; 358 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0; 359 u16 flags, pvid; 360 int err = 0; 361 362 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan 363 * and mark vlan info with begin and end flags 364 * if vlaninfo represents a range 365 */ 366 pvid = br_get_pvid(vg); 367 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) { 368 flags = 0; 369 if (!br_vlan_should_use(v)) 370 continue; 371 if (v->vid == pvid) 372 flags |= BRIDGE_VLAN_INFO_PVID; 373 374 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) 375 flags |= BRIDGE_VLAN_INFO_UNTAGGED; 376 377 if (vid_range_start == 0) { 378 goto initvars; 379 } else if ((v->vid - vid_range_end) == 1 && 380 flags == vid_range_flags) { 381 vid_range_end = v->vid; 382 continue; 383 } else { 384 err = br_fill_ifvlaninfo_range(skb, vid_range_start, 385 vid_range_end, 386 vid_range_flags); 387 if (err) 388 return err; 389 } 390 391 initvars: 392 vid_range_start = v->vid; 393 vid_range_end = v->vid; 394 vid_range_flags = flags; 395 } 396 397 if (vid_range_start != 0) { 398 /* Call it once more to send any left over vlans */ 399 err = br_fill_ifvlaninfo_range(skb, vid_range_start, 400 vid_range_end, 401 vid_range_flags); 402 if (err) 403 return err; 404 } 405 406 return 0; 407 } 408 409 static int br_fill_ifvlaninfo(struct sk_buff *skb, 410 struct net_bridge_vlan_group *vg) 411 { 412 struct bridge_vlan_info vinfo; 413 struct net_bridge_vlan *v; 414 u16 pvid; 415 416 pvid = br_get_pvid(vg); 417 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) { 418 if (!br_vlan_should_use(v)) 419 continue; 420 421 vinfo.vid = v->vid; 422 vinfo.flags = 0; 423 if (v->vid == pvid) 424 vinfo.flags |= BRIDGE_VLAN_INFO_PVID; 425 426 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) 427 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED; 428 429 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, 430 sizeof(vinfo), &vinfo)) 431 goto nla_put_failure; 432 } 433 434 return 0; 435 436 nla_put_failure: 437 return -EMSGSIZE; 438 } 439 440 /* 441 * Create one netlink message for one interface 442 * Contains port and master info as well as carrier and bridge state. 443 */ 444 static int br_fill_ifinfo(struct sk_buff *skb, 445 const struct net_bridge_port *port, 446 u32 pid, u32 seq, int event, unsigned int flags, 447 u32 filter_mask, const struct net_device *dev, 448 bool getlink) 449 { 450 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 451 struct nlattr *af = NULL; 452 struct net_bridge *br; 453 struct ifinfomsg *hdr; 454 struct nlmsghdr *nlh; 455 456 if (port) 457 br = port->br; 458 else 459 br = netdev_priv(dev); 460 461 br_debug(br, "br_fill_info event %d port %s master %s\n", 462 event, dev->name, br->dev->name); 463 464 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 465 if (nlh == NULL) 466 return -EMSGSIZE; 467 468 hdr = nlmsg_data(nlh); 469 hdr->ifi_family = AF_BRIDGE; 470 hdr->__ifi_pad = 0; 471 hdr->ifi_type = dev->type; 472 hdr->ifi_index = dev->ifindex; 473 hdr->ifi_flags = dev_get_flags(dev); 474 hdr->ifi_change = 0; 475 476 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 477 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) || 478 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 479 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 480 (dev->addr_len && 481 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 482 (dev->ifindex != dev_get_iflink(dev) && 483 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 484 goto nla_put_failure; 485 486 if (event == RTM_NEWLINK && port) { 487 struct nlattr *nest; 488 489 nest = nla_nest_start(skb, IFLA_PROTINFO); 490 if (nest == NULL || br_port_fill_attrs(skb, port) < 0) 491 goto nla_put_failure; 492 nla_nest_end(skb, nest); 493 } 494 495 if (filter_mask & (RTEXT_FILTER_BRVLAN | 496 RTEXT_FILTER_BRVLAN_COMPRESSED | 497 RTEXT_FILTER_MRP | 498 RTEXT_FILTER_CFM_CONFIG | 499 RTEXT_FILTER_CFM_STATUS | 500 RTEXT_FILTER_MST)) { 501 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 502 if (!af) 503 goto nla_put_failure; 504 } 505 506 /* Check if the VID information is requested */ 507 if ((filter_mask & RTEXT_FILTER_BRVLAN) || 508 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { 509 struct net_bridge_vlan_group *vg; 510 int err; 511 512 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */ 513 rcu_read_lock(); 514 if (port) 515 vg = nbp_vlan_group_rcu(port); 516 else 517 vg = br_vlan_group_rcu(br); 518 519 if (!vg || !vg->num_vlans) { 520 rcu_read_unlock(); 521 goto done; 522 } 523 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) 524 err = br_fill_ifvlaninfo_compressed(skb, vg); 525 else 526 err = br_fill_ifvlaninfo(skb, vg); 527 528 if (port && (port->flags & BR_VLAN_TUNNEL)) 529 err = br_fill_vlan_tunnel_info(skb, vg); 530 rcu_read_unlock(); 531 if (err) 532 goto nla_put_failure; 533 } 534 535 if (filter_mask & RTEXT_FILTER_MRP) { 536 int err; 537 538 if (!br_mrp_enabled(br) || port) 539 goto done; 540 541 rcu_read_lock(); 542 err = br_mrp_fill_info(skb, br); 543 rcu_read_unlock(); 544 545 if (err) 546 goto nla_put_failure; 547 } 548 549 if (filter_mask & (RTEXT_FILTER_CFM_CONFIG | RTEXT_FILTER_CFM_STATUS)) { 550 struct nlattr *cfm_nest = NULL; 551 int err; 552 553 if (!br_cfm_created(br) || port) 554 goto done; 555 556 cfm_nest = nla_nest_start(skb, IFLA_BRIDGE_CFM); 557 if (!cfm_nest) 558 goto nla_put_failure; 559 560 if (filter_mask & RTEXT_FILTER_CFM_CONFIG) { 561 rcu_read_lock(); 562 err = br_cfm_config_fill_info(skb, br); 563 rcu_read_unlock(); 564 if (err) 565 goto nla_put_failure; 566 } 567 568 if (filter_mask & RTEXT_FILTER_CFM_STATUS) { 569 rcu_read_lock(); 570 err = br_cfm_status_fill_info(skb, br, getlink); 571 rcu_read_unlock(); 572 if (err) 573 goto nla_put_failure; 574 } 575 576 nla_nest_end(skb, cfm_nest); 577 } 578 579 if ((filter_mask & RTEXT_FILTER_MST) && 580 br_opt_get(br, BROPT_MST_ENABLED) && port) { 581 const struct net_bridge_vlan_group *vg = nbp_vlan_group(port); 582 struct nlattr *mst_nest; 583 int err; 584 585 if (!vg || !vg->num_vlans) 586 goto done; 587 588 mst_nest = nla_nest_start(skb, IFLA_BRIDGE_MST); 589 if (!mst_nest) 590 goto nla_put_failure; 591 592 err = br_mst_fill_info(skb, vg); 593 if (err) 594 goto nla_put_failure; 595 596 nla_nest_end(skb, mst_nest); 597 } 598 599 done: 600 if (af) { 601 if (nlmsg_get_pos(skb) - (void *)af > nla_attr_size(0)) 602 nla_nest_end(skb, af); 603 else 604 nla_nest_cancel(skb, af); 605 } 606 607 nlmsg_end(skb, nlh); 608 return 0; 609 610 nla_put_failure: 611 nlmsg_cancel(skb, nlh); 612 return -EMSGSIZE; 613 } 614 615 void br_info_notify(int event, const struct net_bridge *br, 616 const struct net_bridge_port *port, u32 filter) 617 { 618 struct net_device *dev; 619 struct sk_buff *skb; 620 int err = -ENOBUFS; 621 struct net *net; 622 u16 port_no = 0; 623 624 if (WARN_ON(!port && !br)) 625 return; 626 627 if (port) { 628 dev = port->dev; 629 br = port->br; 630 port_no = port->port_no; 631 } else { 632 dev = br->dev; 633 } 634 635 net = dev_net(dev); 636 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event); 637 638 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC); 639 if (skb == NULL) 640 goto errout; 641 642 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false); 643 if (err < 0) { 644 /* -EMSGSIZE implies BUG in br_nlmsg_size() */ 645 WARN_ON(err == -EMSGSIZE); 646 kfree_skb(skb); 647 goto errout; 648 } 649 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 650 return; 651 errout: 652 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 653 } 654 655 /* Notify listeners of a change in bridge or port information */ 656 void br_ifinfo_notify(int event, const struct net_bridge *br, 657 const struct net_bridge_port *port) 658 { 659 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED; 660 661 return br_info_notify(event, br, port, filter); 662 } 663 664 /* 665 * Dump information about all ports, in response to GETLINK 666 */ 667 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, 668 struct net_device *dev, u32 filter_mask, int nlflags) 669 { 670 struct net_bridge_port *port = br_port_get_rtnl(dev); 671 672 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) && 673 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) && 674 !(filter_mask & RTEXT_FILTER_MRP) && 675 !(filter_mask & RTEXT_FILTER_CFM_CONFIG) && 676 !(filter_mask & RTEXT_FILTER_CFM_STATUS)) 677 return 0; 678 679 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags, 680 filter_mask, dev, true); 681 } 682 683 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p, 684 int cmd, struct bridge_vlan_info *vinfo, bool *changed, 685 struct netlink_ext_ack *extack) 686 { 687 bool curr_change; 688 int err = 0; 689 690 switch (cmd) { 691 case RTM_SETLINK: 692 if (p) { 693 /* if the MASTER flag is set this will act on the global 694 * per-VLAN entry as well 695 */ 696 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags, 697 &curr_change, extack); 698 } else { 699 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY; 700 err = br_vlan_add(br, vinfo->vid, vinfo->flags, 701 &curr_change, extack); 702 } 703 if (curr_change) 704 *changed = true; 705 break; 706 707 case RTM_DELLINK: 708 if (p) { 709 if (!nbp_vlan_delete(p, vinfo->vid)) 710 *changed = true; 711 712 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) && 713 !br_vlan_delete(p->br, vinfo->vid)) 714 *changed = true; 715 } else if (!br_vlan_delete(br, vinfo->vid)) { 716 *changed = true; 717 } 718 break; 719 } 720 721 return err; 722 } 723 724 int br_process_vlan_info(struct net_bridge *br, 725 struct net_bridge_port *p, int cmd, 726 struct bridge_vlan_info *vinfo_curr, 727 struct bridge_vlan_info **vinfo_last, 728 bool *changed, 729 struct netlink_ext_ack *extack) 730 { 731 int err, rtm_cmd; 732 733 if (!br_vlan_valid_id(vinfo_curr->vid, extack)) 734 return -EINVAL; 735 736 /* needed for vlan-only NEWVLAN/DELVLAN notifications */ 737 rtm_cmd = br_afspec_cmd_to_rtm(cmd); 738 739 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { 740 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack)) 741 return -EINVAL; 742 *vinfo_last = vinfo_curr; 743 return 0; 744 } 745 746 if (*vinfo_last) { 747 struct bridge_vlan_info tmp_vinfo; 748 int v, v_change_start = 0; 749 750 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack)) 751 return -EINVAL; 752 753 memcpy(&tmp_vinfo, *vinfo_last, 754 sizeof(struct bridge_vlan_info)); 755 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) { 756 bool curr_change = false; 757 758 tmp_vinfo.vid = v; 759 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change, 760 extack); 761 if (err) 762 break; 763 if (curr_change) { 764 *changed = curr_change; 765 if (!v_change_start) 766 v_change_start = v; 767 } else { 768 /* nothing to notify yet */ 769 if (!v_change_start) 770 continue; 771 br_vlan_notify(br, p, v_change_start, 772 v - 1, rtm_cmd); 773 v_change_start = 0; 774 } 775 cond_resched(); 776 } 777 /* v_change_start is set only if the last/whole range changed */ 778 if (v_change_start) 779 br_vlan_notify(br, p, v_change_start, 780 v - 1, rtm_cmd); 781 782 *vinfo_last = NULL; 783 784 return err; 785 } 786 787 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack); 788 if (*changed) 789 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd); 790 791 return err; 792 } 793 794 static int br_afspec(struct net_bridge *br, 795 struct net_bridge_port *p, 796 struct nlattr *af_spec, 797 int cmd, bool *changed, 798 struct netlink_ext_ack *extack) 799 { 800 struct bridge_vlan_info *vinfo_curr = NULL; 801 struct bridge_vlan_info *vinfo_last = NULL; 802 struct nlattr *attr; 803 struct vtunnel_info tinfo_last = {}; 804 struct vtunnel_info tinfo_curr = {}; 805 int err = 0, rem; 806 807 nla_for_each_nested(attr, af_spec, rem) { 808 err = 0; 809 switch (nla_type(attr)) { 810 case IFLA_BRIDGE_VLAN_TUNNEL_INFO: 811 if (!p || !(p->flags & BR_VLAN_TUNNEL)) 812 return -EINVAL; 813 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr); 814 if (err) 815 return err; 816 err = br_process_vlan_tunnel_info(br, p, cmd, 817 &tinfo_curr, 818 &tinfo_last, 819 changed); 820 if (err) 821 return err; 822 break; 823 case IFLA_BRIDGE_VLAN_INFO: 824 if (nla_len(attr) != sizeof(struct bridge_vlan_info)) 825 return -EINVAL; 826 vinfo_curr = nla_data(attr); 827 err = br_process_vlan_info(br, p, cmd, vinfo_curr, 828 &vinfo_last, changed, 829 extack); 830 if (err) 831 return err; 832 break; 833 case IFLA_BRIDGE_MRP: 834 err = br_mrp_parse(br, p, attr, cmd, extack); 835 if (err) 836 return err; 837 break; 838 case IFLA_BRIDGE_CFM: 839 err = br_cfm_parse(br, p, attr, cmd, extack); 840 if (err) 841 return err; 842 break; 843 case IFLA_BRIDGE_MST: 844 if (!p) { 845 NL_SET_ERR_MSG(extack, 846 "MST states can only be set on bridge ports"); 847 return -EINVAL; 848 } 849 850 if (cmd != RTM_SETLINK) { 851 NL_SET_ERR_MSG(extack, 852 "MST states can only be set through RTM_SETLINK"); 853 return -EINVAL; 854 } 855 856 err = br_mst_process(p, attr, extack); 857 if (err) 858 return err; 859 break; 860 } 861 } 862 863 return err; 864 } 865 866 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = { 867 [IFLA_BRPORT_UNSPEC] = { .strict_start_type = 868 IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT + 1 }, 869 [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, 870 [IFLA_BRPORT_COST] = { .type = NLA_U32 }, 871 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, 872 [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, 873 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, 874 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, 875 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 }, 876 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, 877 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, 878 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 }, 879 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 }, 880 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 }, 881 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 }, 882 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 }, 883 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 }, 884 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 }, 885 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 }, 886 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 }, 887 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 }, 888 [IFLA_BRPORT_LOCKED] = { .type = NLA_U8 }, 889 [IFLA_BRPORT_MAB] = { .type = NLA_U8 }, 890 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 }, 891 [IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 }, 892 [IFLA_BRPORT_MCAST_N_GROUPS] = { .type = NLA_REJECT }, 893 [IFLA_BRPORT_MCAST_MAX_GROUPS] = { .type = NLA_U32 }, 894 }; 895 896 /* Change the state of the port and notify spanning tree */ 897 static int br_set_port_state(struct net_bridge_port *p, u8 state) 898 { 899 if (state > BR_STATE_BLOCKING) 900 return -EINVAL; 901 902 /* if kernel STP is running, don't allow changes */ 903 if (p->br->stp_enabled == BR_KERNEL_STP) 904 return -EBUSY; 905 906 /* if device is not up, change is not allowed 907 * if link is not present, only allowable state is disabled 908 */ 909 if (!netif_running(p->dev) || 910 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED)) 911 return -ENETDOWN; 912 913 br_set_state(p, state); 914 br_port_state_selection(p->br); 915 return 0; 916 } 917 918 /* Set/clear or port flags based on attribute */ 919 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[], 920 int attrtype, unsigned long mask) 921 { 922 if (!tb[attrtype]) 923 return; 924 925 if (nla_get_u8(tb[attrtype])) 926 p->flags |= mask; 927 else 928 p->flags &= ~mask; 929 } 930 931 /* Process bridge protocol info on port */ 932 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[], 933 struct netlink_ext_ack *extack) 934 { 935 unsigned long old_flags, changed_mask; 936 bool br_vlan_tunnel_old; 937 int err; 938 939 old_flags = p->flags; 940 br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false; 941 942 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE); 943 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD); 944 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, 945 BR_MULTICAST_FAST_LEAVE); 946 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK); 947 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING); 948 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD); 949 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD); 950 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, 951 BR_MULTICAST_TO_UNICAST); 952 br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD); 953 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP); 954 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI); 955 br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL); 956 br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS); 957 br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED); 958 br_set_port_flag(p, tb, IFLA_BRPORT_LOCKED, BR_PORT_LOCKED); 959 br_set_port_flag(p, tb, IFLA_BRPORT_MAB, BR_PORT_MAB); 960 961 if ((p->flags & BR_PORT_MAB) && 962 (!(p->flags & BR_PORT_LOCKED) || !(p->flags & BR_LEARNING))) { 963 NL_SET_ERR_MSG(extack, "Bridge port must be locked and have learning enabled when MAB is enabled"); 964 p->flags = old_flags; 965 return -EINVAL; 966 } else if (!(p->flags & BR_PORT_MAB) && (old_flags & BR_PORT_MAB)) { 967 struct net_bridge_fdb_flush_desc desc = { 968 .flags = BIT(BR_FDB_LOCKED), 969 .flags_mask = BIT(BR_FDB_LOCKED), 970 .port_ifindex = p->dev->ifindex, 971 }; 972 973 br_fdb_flush(p->br, &desc); 974 } 975 976 changed_mask = old_flags ^ p->flags; 977 978 err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack); 979 if (err) { 980 p->flags = old_flags; 981 return err; 982 } 983 984 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL)) 985 nbp_vlan_tunnel_info_flush(p); 986 987 br_port_flags_change(p, changed_mask); 988 989 if (tb[IFLA_BRPORT_COST]) { 990 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); 991 if (err) 992 return err; 993 } 994 995 if (tb[IFLA_BRPORT_PRIORITY]) { 996 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY])); 997 if (err) 998 return err; 999 } 1000 1001 if (tb[IFLA_BRPORT_STATE]) { 1002 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE])); 1003 if (err) 1004 return err; 1005 } 1006 1007 if (tb[IFLA_BRPORT_FLUSH]) 1008 br_fdb_delete_by_port(p->br, p, 0, 0); 1009 1010 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 1011 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) { 1012 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]); 1013 1014 err = br_multicast_set_port_router(&p->multicast_ctx, 1015 mcast_router); 1016 if (err) 1017 return err; 1018 } 1019 1020 if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) { 1021 u32 hlimit; 1022 1023 hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]); 1024 err = br_multicast_eht_set_hosts_limit(p, hlimit); 1025 if (err) 1026 return err; 1027 } 1028 1029 if (tb[IFLA_BRPORT_MCAST_MAX_GROUPS]) { 1030 u32 max_groups; 1031 1032 max_groups = nla_get_u32(tb[IFLA_BRPORT_MCAST_MAX_GROUPS]); 1033 br_multicast_ngroups_set_max(&p->multicast_ctx, max_groups); 1034 } 1035 #endif 1036 1037 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) { 1038 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]); 1039 1040 if (fwd_mask & BR_GROUPFWD_MACPAUSE) 1041 return -EINVAL; 1042 p->group_fwd_mask = fwd_mask; 1043 } 1044 1045 if (tb[IFLA_BRPORT_BACKUP_PORT]) { 1046 struct net_device *backup_dev = NULL; 1047 u32 backup_ifindex; 1048 1049 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]); 1050 if (backup_ifindex) { 1051 backup_dev = __dev_get_by_index(dev_net(p->dev), 1052 backup_ifindex); 1053 if (!backup_dev) 1054 return -ENOENT; 1055 } 1056 1057 err = nbp_backup_change(p, backup_dev); 1058 if (err) 1059 return err; 1060 } 1061 1062 return 0; 1063 } 1064 1065 /* Change state and parameters on port. */ 1066 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags, 1067 struct netlink_ext_ack *extack) 1068 { 1069 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev); 1070 struct nlattr *tb[IFLA_BRPORT_MAX + 1]; 1071 struct net_bridge_port *p; 1072 struct nlattr *protinfo; 1073 struct nlattr *afspec; 1074 bool changed = false; 1075 int err = 0; 1076 1077 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO); 1078 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 1079 if (!protinfo && !afspec) 1080 return 0; 1081 1082 p = br_port_get_rtnl(dev); 1083 /* We want to accept dev as bridge itself if the AF_SPEC 1084 * is set to see if someone is setting vlan info on the bridge 1085 */ 1086 if (!p && !afspec) 1087 return -EINVAL; 1088 1089 if (p && protinfo) { 1090 if (protinfo->nla_type & NLA_F_NESTED) { 1091 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX, 1092 protinfo, 1093 br_port_policy, 1094 NULL); 1095 if (err) 1096 return err; 1097 1098 spin_lock_bh(&p->br->lock); 1099 err = br_setport(p, tb, extack); 1100 spin_unlock_bh(&p->br->lock); 1101 } else { 1102 /* Binary compatibility with old RSTP */ 1103 if (nla_len(protinfo) < sizeof(u8)) 1104 return -EINVAL; 1105 1106 spin_lock_bh(&p->br->lock); 1107 err = br_set_port_state(p, nla_get_u8(protinfo)); 1108 spin_unlock_bh(&p->br->lock); 1109 } 1110 if (err) 1111 goto out; 1112 changed = true; 1113 } 1114 1115 if (afspec) 1116 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack); 1117 1118 if (changed) 1119 br_ifinfo_notify(RTM_NEWLINK, br, p); 1120 out: 1121 return err; 1122 } 1123 1124 /* Delete port information */ 1125 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags) 1126 { 1127 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev); 1128 struct net_bridge_port *p; 1129 struct nlattr *afspec; 1130 bool changed = false; 1131 int err = 0; 1132 1133 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 1134 if (!afspec) 1135 return 0; 1136 1137 p = br_port_get_rtnl(dev); 1138 /* We want to accept dev as bridge itself as well */ 1139 if (!p && !netif_is_bridge_master(dev)) 1140 return -EINVAL; 1141 1142 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL); 1143 if (changed) 1144 /* Send RTM_NEWLINK because userspace 1145 * expects RTM_NEWLINK for vlan dels 1146 */ 1147 br_ifinfo_notify(RTM_NEWLINK, br, p); 1148 1149 return err; 1150 } 1151 1152 static int br_validate(struct nlattr *tb[], struct nlattr *data[], 1153 struct netlink_ext_ack *extack) 1154 { 1155 if (tb[IFLA_ADDRESS]) { 1156 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1157 return -EINVAL; 1158 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1159 return -EADDRNOTAVAIL; 1160 } 1161 1162 if (!data) 1163 return 0; 1164 1165 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1166 if (data[IFLA_BR_VLAN_PROTOCOL] && 1167 !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]))) 1168 return -EPROTONOSUPPORT; 1169 1170 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) { 1171 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]); 1172 1173 if (defpvid >= VLAN_VID_MASK) 1174 return -EINVAL; 1175 } 1176 #endif 1177 1178 return 0; 1179 } 1180 1181 static int br_port_slave_changelink(struct net_device *brdev, 1182 struct net_device *dev, 1183 struct nlattr *tb[], 1184 struct nlattr *data[], 1185 struct netlink_ext_ack *extack) 1186 { 1187 struct net_bridge *br = netdev_priv(brdev); 1188 int ret; 1189 1190 if (!data) 1191 return 0; 1192 1193 spin_lock_bh(&br->lock); 1194 ret = br_setport(br_port_get_rtnl(dev), data, extack); 1195 spin_unlock_bh(&br->lock); 1196 1197 return ret; 1198 } 1199 1200 static int br_port_fill_slave_info(struct sk_buff *skb, 1201 const struct net_device *brdev, 1202 const struct net_device *dev) 1203 { 1204 return br_port_fill_attrs(skb, br_port_get_rtnl(dev)); 1205 } 1206 1207 static size_t br_port_get_slave_size(const struct net_device *brdev, 1208 const struct net_device *dev) 1209 { 1210 return br_port_info_size(); 1211 } 1212 1213 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = { 1214 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 }, 1215 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 }, 1216 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 }, 1217 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 }, 1218 [IFLA_BR_STP_STATE] = { .type = NLA_U32 }, 1219 [IFLA_BR_PRIORITY] = { .type = NLA_U16 }, 1220 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 }, 1221 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 }, 1222 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 }, 1223 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY, 1224 .len = ETH_ALEN }, 1225 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 }, 1226 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 }, 1227 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 }, 1228 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 }, 1229 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 }, 1230 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 }, 1231 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 }, 1232 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 }, 1233 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 }, 1234 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 }, 1235 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 }, 1236 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 }, 1237 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 }, 1238 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 }, 1239 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 }, 1240 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 }, 1241 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 }, 1242 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 }, 1243 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 }, 1244 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 }, 1245 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 }, 1246 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 }, 1247 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 }, 1248 [IFLA_BR_MULTI_BOOLOPT] = 1249 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)), 1250 }; 1251 1252 static int br_changelink(struct net_device *brdev, struct nlattr *tb[], 1253 struct nlattr *data[], 1254 struct netlink_ext_ack *extack) 1255 { 1256 struct net_bridge *br = netdev_priv(brdev); 1257 int err; 1258 1259 if (!data) 1260 return 0; 1261 1262 if (data[IFLA_BR_FORWARD_DELAY]) { 1263 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY])); 1264 if (err) 1265 return err; 1266 } 1267 1268 if (data[IFLA_BR_HELLO_TIME]) { 1269 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME])); 1270 if (err) 1271 return err; 1272 } 1273 1274 if (data[IFLA_BR_MAX_AGE]) { 1275 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE])); 1276 if (err) 1277 return err; 1278 } 1279 1280 if (data[IFLA_BR_AGEING_TIME]) { 1281 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME])); 1282 if (err) 1283 return err; 1284 } 1285 1286 if (data[IFLA_BR_STP_STATE]) { 1287 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]); 1288 1289 err = br_stp_set_enabled(br, stp_enabled, extack); 1290 if (err) 1291 return err; 1292 } 1293 1294 if (data[IFLA_BR_PRIORITY]) { 1295 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]); 1296 1297 br_stp_set_bridge_priority(br, priority); 1298 } 1299 1300 if (data[IFLA_BR_VLAN_FILTERING]) { 1301 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]); 1302 1303 err = br_vlan_filter_toggle(br, vlan_filter, extack); 1304 if (err) 1305 return err; 1306 } 1307 1308 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1309 if (data[IFLA_BR_VLAN_PROTOCOL]) { 1310 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]); 1311 1312 err = __br_vlan_set_proto(br, vlan_proto, extack); 1313 if (err) 1314 return err; 1315 } 1316 1317 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) { 1318 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]); 1319 1320 err = __br_vlan_set_default_pvid(br, defpvid, extack); 1321 if (err) 1322 return err; 1323 } 1324 1325 if (data[IFLA_BR_VLAN_STATS_ENABLED]) { 1326 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]); 1327 1328 err = br_vlan_set_stats(br, vlan_stats); 1329 if (err) 1330 return err; 1331 } 1332 1333 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) { 1334 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]); 1335 1336 err = br_vlan_set_stats_per_port(br, per_port); 1337 if (err) 1338 return err; 1339 } 1340 #endif 1341 1342 if (data[IFLA_BR_GROUP_FWD_MASK]) { 1343 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]); 1344 1345 if (fwd_mask & BR_GROUPFWD_RESTRICTED) 1346 return -EINVAL; 1347 br->group_fwd_mask = fwd_mask; 1348 } 1349 1350 if (data[IFLA_BR_GROUP_ADDR]) { 1351 u8 new_addr[ETH_ALEN]; 1352 1353 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN) 1354 return -EINVAL; 1355 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN); 1356 if (!is_link_local_ether_addr(new_addr)) 1357 return -EINVAL; 1358 if (new_addr[5] == 1 || /* 802.3x Pause address */ 1359 new_addr[5] == 2 || /* 802.3ad Slow protocols */ 1360 new_addr[5] == 3) /* 802.1X PAE address */ 1361 return -EINVAL; 1362 spin_lock_bh(&br->lock); 1363 memcpy(br->group_addr, new_addr, sizeof(br->group_addr)); 1364 spin_unlock_bh(&br->lock); 1365 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true); 1366 br_recalculate_fwd_mask(br); 1367 } 1368 1369 if (data[IFLA_BR_FDB_FLUSH]) { 1370 struct net_bridge_fdb_flush_desc desc = { 1371 .flags_mask = BIT(BR_FDB_STATIC) 1372 }; 1373 1374 br_fdb_flush(br, &desc); 1375 } 1376 1377 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 1378 if (data[IFLA_BR_MCAST_ROUTER]) { 1379 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]); 1380 1381 err = br_multicast_set_router(&br->multicast_ctx, 1382 multicast_router); 1383 if (err) 1384 return err; 1385 } 1386 1387 if (data[IFLA_BR_MCAST_SNOOPING]) { 1388 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]); 1389 1390 err = br_multicast_toggle(br, mcast_snooping, extack); 1391 if (err) 1392 return err; 1393 } 1394 1395 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) { 1396 u8 val; 1397 1398 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]); 1399 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val); 1400 } 1401 1402 if (data[IFLA_BR_MCAST_QUERIER]) { 1403 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]); 1404 1405 err = br_multicast_set_querier(&br->multicast_ctx, 1406 mcast_querier); 1407 if (err) 1408 return err; 1409 } 1410 1411 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) 1412 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n", 1413 RHT_ELASTICITY); 1414 1415 if (data[IFLA_BR_MCAST_HASH_MAX]) 1416 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]); 1417 1418 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) { 1419 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]); 1420 1421 br->multicast_ctx.multicast_last_member_count = val; 1422 } 1423 1424 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) { 1425 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]); 1426 1427 br->multicast_ctx.multicast_startup_query_count = val; 1428 } 1429 1430 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) { 1431 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]); 1432 1433 br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val); 1434 } 1435 1436 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) { 1437 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]); 1438 1439 br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val); 1440 } 1441 1442 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) { 1443 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]); 1444 1445 br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val); 1446 } 1447 1448 if (data[IFLA_BR_MCAST_QUERY_INTVL]) { 1449 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]); 1450 1451 br_multicast_set_query_intvl(&br->multicast_ctx, val); 1452 } 1453 1454 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) { 1455 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]); 1456 1457 br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val); 1458 } 1459 1460 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) { 1461 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]); 1462 1463 br_multicast_set_startup_query_intvl(&br->multicast_ctx, val); 1464 } 1465 1466 if (data[IFLA_BR_MCAST_STATS_ENABLED]) { 1467 __u8 mcast_stats; 1468 1469 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]); 1470 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats); 1471 } 1472 1473 if (data[IFLA_BR_MCAST_IGMP_VERSION]) { 1474 __u8 igmp_version; 1475 1476 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]); 1477 err = br_multicast_set_igmp_version(&br->multicast_ctx, 1478 igmp_version); 1479 if (err) 1480 return err; 1481 } 1482 1483 #if IS_ENABLED(CONFIG_IPV6) 1484 if (data[IFLA_BR_MCAST_MLD_VERSION]) { 1485 __u8 mld_version; 1486 1487 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]); 1488 err = br_multicast_set_mld_version(&br->multicast_ctx, 1489 mld_version); 1490 if (err) 1491 return err; 1492 } 1493 #endif 1494 #endif 1495 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1496 if (data[IFLA_BR_NF_CALL_IPTABLES]) { 1497 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]); 1498 1499 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val); 1500 } 1501 1502 if (data[IFLA_BR_NF_CALL_IP6TABLES]) { 1503 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]); 1504 1505 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val); 1506 } 1507 1508 if (data[IFLA_BR_NF_CALL_ARPTABLES]) { 1509 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]); 1510 1511 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val); 1512 } 1513 #endif 1514 1515 if (data[IFLA_BR_MULTI_BOOLOPT]) { 1516 struct br_boolopt_multi *bm; 1517 1518 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]); 1519 err = br_boolopt_multi_toggle(br, bm, extack); 1520 if (err) 1521 return err; 1522 } 1523 1524 return 0; 1525 } 1526 1527 static int br_dev_newlink(struct net *src_net, struct net_device *dev, 1528 struct nlattr *tb[], struct nlattr *data[], 1529 struct netlink_ext_ack *extack) 1530 { 1531 struct net_bridge *br = netdev_priv(dev); 1532 int err; 1533 1534 err = register_netdevice(dev); 1535 if (err) 1536 return err; 1537 1538 if (tb[IFLA_ADDRESS]) { 1539 spin_lock_bh(&br->lock); 1540 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS])); 1541 spin_unlock_bh(&br->lock); 1542 } 1543 1544 err = br_changelink(dev, tb, data, extack); 1545 if (err) 1546 br_dev_delete(dev, NULL); 1547 1548 return err; 1549 } 1550 1551 static size_t br_get_size(const struct net_device *brdev) 1552 { 1553 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */ 1554 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */ 1555 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */ 1556 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */ 1557 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */ 1558 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */ 1559 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */ 1560 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1561 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */ 1562 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */ 1563 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */ 1564 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */ 1565 #endif 1566 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */ 1567 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */ 1568 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */ 1569 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */ 1570 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */ 1571 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */ 1572 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */ 1573 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */ 1574 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */ 1575 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */ 1576 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */ 1577 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */ 1578 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 1579 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */ 1580 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */ 1581 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */ 1582 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */ 1583 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */ 1584 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */ 1585 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */ 1586 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */ 1587 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */ 1588 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */ 1589 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */ 1590 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */ 1591 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */ 1592 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */ 1593 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */ 1594 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */ 1595 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */ 1596 br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */ 1597 #endif 1598 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1599 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */ 1600 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */ 1601 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */ 1602 #endif 1603 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */ 1604 0; 1605 } 1606 1607 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev) 1608 { 1609 struct net_bridge *br = netdev_priv(brdev); 1610 u32 forward_delay = jiffies_to_clock_t(br->forward_delay); 1611 u32 hello_time = jiffies_to_clock_t(br->hello_time); 1612 u32 age_time = jiffies_to_clock_t(br->max_age); 1613 u32 ageing_time = jiffies_to_clock_t(br->ageing_time); 1614 u32 stp_enabled = br->stp_enabled; 1615 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]; 1616 u8 vlan_enabled = br_vlan_enabled(br->dev); 1617 struct br_boolopt_multi bm; 1618 u64 clockval; 1619 1620 clockval = br_timer_value(&br->hello_timer); 1621 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD)) 1622 return -EMSGSIZE; 1623 clockval = br_timer_value(&br->tcn_timer); 1624 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD)) 1625 return -EMSGSIZE; 1626 clockval = br_timer_value(&br->topology_change_timer); 1627 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval, 1628 IFLA_BR_PAD)) 1629 return -EMSGSIZE; 1630 clockval = br_timer_value(&br->gc_work.timer); 1631 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD)) 1632 return -EMSGSIZE; 1633 1634 br_boolopt_multi_get(br, &bm); 1635 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) || 1636 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) || 1637 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) || 1638 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) || 1639 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) || 1640 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) || 1641 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) || 1642 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) || 1643 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id), 1644 &br->bridge_id) || 1645 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id), 1646 &br->designated_root) || 1647 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) || 1648 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) || 1649 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) || 1650 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED, 1651 br->topology_change_detected) || 1652 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) || 1653 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm)) 1654 return -EMSGSIZE; 1655 1656 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1657 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) || 1658 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) || 1659 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, 1660 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) || 1661 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT, 1662 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT))) 1663 return -EMSGSIZE; 1664 #endif 1665 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 1666 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, 1667 br->multicast_ctx.multicast_router) || 1668 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, 1669 br_opt_get(br, BROPT_MULTICAST_ENABLED)) || 1670 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR, 1671 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) || 1672 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, 1673 br->multicast_ctx.multicast_querier) || 1674 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED, 1675 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) || 1676 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) || 1677 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) || 1678 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT, 1679 br->multicast_ctx.multicast_last_member_count) || 1680 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT, 1681 br->multicast_ctx.multicast_startup_query_count) || 1682 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION, 1683 br->multicast_ctx.multicast_igmp_version) || 1684 br_multicast_dump_querier_state(skb, &br->multicast_ctx, 1685 IFLA_BR_MCAST_QUERIER_STATE)) 1686 return -EMSGSIZE; 1687 #if IS_ENABLED(CONFIG_IPV6) 1688 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION, 1689 br->multicast_ctx.multicast_mld_version)) 1690 return -EMSGSIZE; 1691 #endif 1692 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval); 1693 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval, 1694 IFLA_BR_PAD)) 1695 return -EMSGSIZE; 1696 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval); 1697 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval, 1698 IFLA_BR_PAD)) 1699 return -EMSGSIZE; 1700 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval); 1701 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval, 1702 IFLA_BR_PAD)) 1703 return -EMSGSIZE; 1704 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval); 1705 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval, 1706 IFLA_BR_PAD)) 1707 return -EMSGSIZE; 1708 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval); 1709 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval, 1710 IFLA_BR_PAD)) 1711 return -EMSGSIZE; 1712 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval); 1713 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval, 1714 IFLA_BR_PAD)) 1715 return -EMSGSIZE; 1716 #endif 1717 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1718 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES, 1719 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) || 1720 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES, 1721 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) || 1722 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES, 1723 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0)) 1724 return -EMSGSIZE; 1725 #endif 1726 1727 return 0; 1728 } 1729 1730 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr) 1731 { 1732 struct net_bridge_port *p = NULL; 1733 struct net_bridge_vlan_group *vg; 1734 struct net_bridge_vlan *v; 1735 struct net_bridge *br; 1736 int numvls = 0; 1737 1738 switch (attr) { 1739 case IFLA_STATS_LINK_XSTATS: 1740 br = netdev_priv(dev); 1741 vg = br_vlan_group(br); 1742 break; 1743 case IFLA_STATS_LINK_XSTATS_SLAVE: 1744 p = br_port_get_rtnl(dev); 1745 if (!p) 1746 return 0; 1747 vg = nbp_vlan_group(p); 1748 break; 1749 default: 1750 return 0; 1751 } 1752 1753 if (vg) { 1754 /* we need to count all, even placeholder entries */ 1755 list_for_each_entry(v, &vg->vlan_list, vlist) 1756 numvls++; 1757 } 1758 1759 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) + 1760 nla_total_size_64bit(sizeof(struct br_mcast_stats)) + 1761 (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) + 1762 nla_total_size(0); 1763 } 1764 1765 static int br_fill_linkxstats(struct sk_buff *skb, 1766 const struct net_device *dev, 1767 int *prividx, int attr) 1768 { 1769 struct nlattr *nla __maybe_unused; 1770 struct net_bridge_port *p = NULL; 1771 struct net_bridge_vlan_group *vg; 1772 struct net_bridge_vlan *v; 1773 struct net_bridge *br; 1774 struct nlattr *nest; 1775 int vl_idx = 0; 1776 1777 switch (attr) { 1778 case IFLA_STATS_LINK_XSTATS: 1779 br = netdev_priv(dev); 1780 vg = br_vlan_group(br); 1781 break; 1782 case IFLA_STATS_LINK_XSTATS_SLAVE: 1783 p = br_port_get_rtnl(dev); 1784 if (!p) 1785 return 0; 1786 br = p->br; 1787 vg = nbp_vlan_group(p); 1788 break; 1789 default: 1790 return -EINVAL; 1791 } 1792 1793 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE); 1794 if (!nest) 1795 return -EMSGSIZE; 1796 1797 if (vg) { 1798 u16 pvid; 1799 1800 pvid = br_get_pvid(vg); 1801 list_for_each_entry(v, &vg->vlan_list, vlist) { 1802 struct bridge_vlan_xstats vxi; 1803 struct pcpu_sw_netstats stats; 1804 1805 if (++vl_idx < *prividx) 1806 continue; 1807 memset(&vxi, 0, sizeof(vxi)); 1808 vxi.vid = v->vid; 1809 vxi.flags = v->flags; 1810 if (v->vid == pvid) 1811 vxi.flags |= BRIDGE_VLAN_INFO_PVID; 1812 br_vlan_get_stats(v, &stats); 1813 vxi.rx_bytes = u64_stats_read(&stats.rx_bytes); 1814 vxi.rx_packets = u64_stats_read(&stats.rx_packets); 1815 vxi.tx_bytes = u64_stats_read(&stats.tx_bytes); 1816 vxi.tx_packets = u64_stats_read(&stats.tx_packets); 1817 1818 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi)) 1819 goto nla_put_failure; 1820 } 1821 } 1822 1823 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 1824 if (++vl_idx >= *prividx) { 1825 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST, 1826 sizeof(struct br_mcast_stats), 1827 BRIDGE_XSTATS_PAD); 1828 if (!nla) 1829 goto nla_put_failure; 1830 br_multicast_get_stats(br, p, nla_data(nla)); 1831 } 1832 #endif 1833 1834 if (p) { 1835 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP, 1836 sizeof(p->stp_xstats), 1837 BRIDGE_XSTATS_PAD); 1838 if (!nla) 1839 goto nla_put_failure; 1840 1841 spin_lock_bh(&br->lock); 1842 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats)); 1843 spin_unlock_bh(&br->lock); 1844 } 1845 1846 nla_nest_end(skb, nest); 1847 *prividx = 0; 1848 1849 return 0; 1850 1851 nla_put_failure: 1852 nla_nest_end(skb, nest); 1853 *prividx = vl_idx; 1854 1855 return -EMSGSIZE; 1856 } 1857 1858 static struct rtnl_af_ops br_af_ops __read_mostly = { 1859 .family = AF_BRIDGE, 1860 .get_link_af_size = br_get_link_af_size_filtered, 1861 }; 1862 1863 struct rtnl_link_ops br_link_ops __read_mostly = { 1864 .kind = "bridge", 1865 .priv_size = sizeof(struct net_bridge), 1866 .setup = br_dev_setup, 1867 .maxtype = IFLA_BR_MAX, 1868 .policy = br_policy, 1869 .validate = br_validate, 1870 .newlink = br_dev_newlink, 1871 .changelink = br_changelink, 1872 .dellink = br_dev_delete, 1873 .get_size = br_get_size, 1874 .fill_info = br_fill_info, 1875 .fill_linkxstats = br_fill_linkxstats, 1876 .get_linkxstats_size = br_get_linkxstats_size, 1877 1878 .slave_maxtype = IFLA_BRPORT_MAX, 1879 .slave_policy = br_port_policy, 1880 .slave_changelink = br_port_slave_changelink, 1881 .get_slave_size = br_port_get_slave_size, 1882 .fill_slave_info = br_port_fill_slave_info, 1883 }; 1884 1885 int __init br_netlink_init(void) 1886 { 1887 int err; 1888 1889 br_mdb_init(); 1890 br_vlan_rtnl_init(); 1891 rtnl_af_register(&br_af_ops); 1892 1893 err = rtnl_link_register(&br_link_ops); 1894 if (err) 1895 goto out_af; 1896 1897 return 0; 1898 1899 out_af: 1900 rtnl_af_unregister(&br_af_ops); 1901 br_mdb_uninit(); 1902 return err; 1903 } 1904 1905 void br_netlink_fini(void) 1906 { 1907 br_mdb_uninit(); 1908 br_vlan_rtnl_uninit(); 1909 rtnl_af_unregister(&br_af_ops); 1910 rtnl_link_unregister(&br_link_ops); 1911 } 1912