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