linux/net/bridge/br_netlink.c
<<
>>
Prefs
   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
  25static 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                }
  61initvars:
  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
  77static 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
  95static 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
 124static 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
 161static 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
 175static 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
 243static 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
 271nla_put_failure:
 272        return -EMSGSIZE;
 273}
 274
 275static 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
 312initvars:
 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
 330static 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
 357nla_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 */
 365static 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
 408                nest = nla_nest_start(skb, IFLA_PROTINFO);
 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_noflag(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
 450done:
 451        nlmsg_end(skb, nlh);
 452        return 0;
 453
 454nla_put_failure:
 455        nlmsg_cancel(skb, nlh);
 456        return -EMSGSIZE;
 457}
 458
 459/* Notify listeners of a change in bridge or port information */
 460void 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;
 497errout:
 498        rtnl_set_sk_err(net, RTNLGRP_LINK, err);
 499}
 500
 501/*
 502 * Dump information about all ports, in response to GETLINK
 503 */
 504int 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
 517static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
 518                        int cmd, struct bridge_vlan_info *vinfo, bool *changed,
 519                        struct netlink_ext_ack *extack)
 520{
 521        bool curr_change;
 522        int err = 0;
 523
 524        switch (cmd) {
 525        case RTM_SETLINK:
 526                if (p) {
 527                        /* if the MASTER flag is set this will act on the global
 528                         * per-VLAN entry as well
 529                         */
 530                        err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
 531                                           &curr_change, extack);
 532                } else {
 533                        vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
 534                        err = br_vlan_add(br, vinfo->vid, vinfo->flags,
 535                                          &curr_change, extack);
 536                }
 537                if (curr_change)
 538                        *changed = true;
 539                break;
 540
 541        case RTM_DELLINK:
 542                if (p) {
 543                        if (!nbp_vlan_delete(p, vinfo->vid))
 544                                *changed = true;
 545
 546                        if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
 547                            !br_vlan_delete(p->br, vinfo->vid))
 548                                *changed = true;
 549                } else if (!br_vlan_delete(br, vinfo->vid)) {
 550                        *changed = true;
 551                }
 552                break;
 553        }
 554
 555        return err;
 556}
 557
 558static int br_process_vlan_info(struct net_bridge *br,
 559                                struct net_bridge_port *p, int cmd,
 560                                struct bridge_vlan_info *vinfo_curr,
 561                                struct bridge_vlan_info **vinfo_last,
 562                                bool *changed,
 563                                struct netlink_ext_ack *extack)
 564{
 565        if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
 566                return -EINVAL;
 567
 568        if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
 569                /* check if we are already processing a range */
 570                if (*vinfo_last)
 571                        return -EINVAL;
 572                *vinfo_last = vinfo_curr;
 573                /* don't allow range of pvids */
 574                if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
 575                        return -EINVAL;
 576                return 0;
 577        }
 578
 579        if (*vinfo_last) {
 580                struct bridge_vlan_info tmp_vinfo;
 581                int v, err;
 582
 583                if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
 584                        return -EINVAL;
 585
 586                if (vinfo_curr->vid <= (*vinfo_last)->vid)
 587                        return -EINVAL;
 588
 589                memcpy(&tmp_vinfo, *vinfo_last,
 590                       sizeof(struct bridge_vlan_info));
 591                for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
 592                        tmp_vinfo.vid = v;
 593                        err = br_vlan_info(br, p, cmd, &tmp_vinfo, changed,
 594                                           extack);
 595                        if (err)
 596                                break;
 597                }
 598                *vinfo_last = NULL;
 599
 600                return err;
 601        }
 602
 603        return br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
 604}
 605
 606static int br_afspec(struct net_bridge *br,
 607                     struct net_bridge_port *p,
 608                     struct nlattr *af_spec,
 609                     int cmd, bool *changed,
 610                     struct netlink_ext_ack *extack)
 611{
 612        struct bridge_vlan_info *vinfo_curr = NULL;
 613        struct bridge_vlan_info *vinfo_last = NULL;
 614        struct nlattr *attr;
 615        struct vtunnel_info tinfo_last = {};
 616        struct vtunnel_info tinfo_curr = {};
 617        int err = 0, rem;
 618
 619        nla_for_each_nested(attr, af_spec, rem) {
 620                err = 0;
 621                switch (nla_type(attr)) {
 622                case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
 623                        if (!p || !(p->flags & BR_VLAN_TUNNEL))
 624                                return -EINVAL;
 625                        err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
 626                        if (err)
 627                                return err;
 628                        err = br_process_vlan_tunnel_info(br, p, cmd,
 629                                                          &tinfo_curr,
 630                                                          &tinfo_last,
 631                                                          changed);
 632                        if (err)
 633                                return err;
 634                        break;
 635                case IFLA_BRIDGE_VLAN_INFO:
 636                        if (nla_len(attr) != sizeof(struct bridge_vlan_info))
 637                                return -EINVAL;
 638                        vinfo_curr = nla_data(attr);
 639                        err = br_process_vlan_info(br, p, cmd, vinfo_curr,
 640                                                   &vinfo_last, changed,
 641                                                   extack);
 642                        if (err)
 643                                return err;
 644                        break;
 645                }
 646        }
 647
 648        return err;
 649}
 650
 651static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
 652        [IFLA_BRPORT_STATE]     = { .type = NLA_U8 },
 653        [IFLA_BRPORT_COST]      = { .type = NLA_U32 },
 654        [IFLA_BRPORT_PRIORITY]  = { .type = NLA_U16 },
 655        [IFLA_BRPORT_MODE]      = { .type = NLA_U8 },
 656        [IFLA_BRPORT_GUARD]     = { .type = NLA_U8 },
 657        [IFLA_BRPORT_PROTECT]   = { .type = NLA_U8 },
 658        [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
 659        [IFLA_BRPORT_LEARNING]  = { .type = NLA_U8 },
 660        [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
 661        [IFLA_BRPORT_PROXYARP]  = { .type = NLA_U8 },
 662        [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
 663        [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
 664        [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
 665        [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
 666        [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
 667        [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
 668        [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
 669        [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
 670        [IFLA_BRPORT_ISOLATED]  = { .type = NLA_U8 },
 671};
 672
 673/* Change the state of the port and notify spanning tree */
 674static int br_set_port_state(struct net_bridge_port *p, u8 state)
 675{
 676        if (state > BR_STATE_BLOCKING)
 677                return -EINVAL;
 678
 679        /* if kernel STP is running, don't allow changes */
 680        if (p->br->stp_enabled == BR_KERNEL_STP)
 681                return -EBUSY;
 682
 683        /* if device is not up, change is not allowed
 684         * if link is not present, only allowable state is disabled
 685         */
 686        if (!netif_running(p->dev) ||
 687            (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
 688                return -ENETDOWN;
 689
 690        br_set_state(p, state);
 691        br_port_state_selection(p->br);
 692        return 0;
 693}
 694
 695/* Set/clear or port flags based on attribute */
 696static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
 697                            int attrtype, unsigned long mask)
 698{
 699        unsigned long flags;
 700        int err;
 701
 702        if (!tb[attrtype])
 703                return 0;
 704
 705        if (nla_get_u8(tb[attrtype]))
 706                flags = p->flags | mask;
 707        else
 708                flags = p->flags & ~mask;
 709
 710        err = br_switchdev_set_port_flag(p, flags, mask);
 711        if (err)
 712                return err;
 713
 714        p->flags = flags;
 715        return 0;
 716}
 717
 718/* Process bridge protocol info on port */
 719static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
 720{
 721        unsigned long old_flags = p->flags;
 722        bool br_vlan_tunnel_old = false;
 723        int err;
 724
 725        err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
 726        if (err)
 727                return err;
 728
 729        err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
 730        if (err)
 731                return err;
 732
 733        err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
 734        if (err)
 735                return err;
 736
 737        err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
 738        if (err)
 739                return err;
 740
 741        err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
 742        if (err)
 743                return err;
 744
 745        err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
 746        if (err)
 747                return err;
 748
 749        err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
 750        if (err)
 751                return err;
 752
 753        err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
 754        if (err)
 755                return err;
 756
 757        err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
 758        if (err)
 759                return err;
 760
 761        err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
 762        if (err)
 763                return err;
 764
 765        err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
 766        if (err)
 767                return err;
 768
 769        br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
 770        err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
 771        if (err)
 772                return err;
 773
 774        if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
 775                nbp_vlan_tunnel_info_flush(p);
 776
 777        if (tb[IFLA_BRPORT_COST]) {
 778                err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
 779                if (err)
 780                        return err;
 781        }
 782
 783        if (tb[IFLA_BRPORT_PRIORITY]) {
 784                err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
 785                if (err)
 786                        return err;
 787        }
 788
 789        if (tb[IFLA_BRPORT_STATE]) {
 790                err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
 791                if (err)
 792                        return err;
 793        }
 794
 795        if (tb[IFLA_BRPORT_FLUSH])
 796                br_fdb_delete_by_port(p->br, p, 0, 0);
 797
 798#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
 799        if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
 800                u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
 801
 802                err = br_multicast_set_port_router(p, mcast_router);
 803                if (err)
 804                        return err;
 805        }
 806#endif
 807
 808        if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
 809                u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
 810
 811                if (fwd_mask & BR_GROUPFWD_MACPAUSE)
 812                        return -EINVAL;
 813                p->group_fwd_mask = fwd_mask;
 814        }
 815
 816        err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
 817                               BR_NEIGH_SUPPRESS);
 818        if (err)
 819                return err;
 820
 821        err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
 822        if (err)
 823                return err;
 824
 825        br_port_flags_change(p, old_flags ^ p->flags);
 826        return 0;
 827}
 828
 829/* Change state and parameters on port. */
 830int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
 831               struct netlink_ext_ack *extack)
 832{
 833        struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
 834        struct nlattr *tb[IFLA_BRPORT_MAX + 1];
 835        struct net_bridge_port *p;
 836        struct nlattr *protinfo;
 837        struct nlattr *afspec;
 838        bool changed = false;
 839        int err = 0;
 840
 841        protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
 842        afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
 843        if (!protinfo && !afspec)
 844                return 0;
 845
 846        p = br_port_get_rtnl(dev);
 847        /* We want to accept dev as bridge itself if the AF_SPEC
 848         * is set to see if someone is setting vlan info on the bridge
 849         */
 850        if (!p && !afspec)
 851                return -EINVAL;
 852
 853        if (p && protinfo) {
 854                if (protinfo->nla_type & NLA_F_NESTED) {
 855                        err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
 856                                                          protinfo,
 857                                                          br_port_policy,
 858                                                          NULL);
 859                        if (err)
 860                                return err;
 861
 862                        spin_lock_bh(&p->br->lock);
 863                        err = br_setport(p, tb);
 864                        spin_unlock_bh(&p->br->lock);
 865                } else {
 866                        /* Binary compatibility with old RSTP */
 867                        if (nla_len(protinfo) < sizeof(u8))
 868                                return -EINVAL;
 869
 870                        spin_lock_bh(&p->br->lock);
 871                        err = br_set_port_state(p, nla_get_u8(protinfo));
 872                        spin_unlock_bh(&p->br->lock);
 873                }
 874                if (err)
 875                        goto out;
 876                changed = true;
 877        }
 878
 879        if (afspec)
 880                err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
 881
 882        if (changed)
 883                br_ifinfo_notify(RTM_NEWLINK, br, p);
 884out:
 885        return err;
 886}
 887
 888/* Delete port information */
 889int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
 890{
 891        struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
 892        struct net_bridge_port *p;
 893        struct nlattr *afspec;
 894        bool changed = false;
 895        int err = 0;
 896
 897        afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
 898        if (!afspec)
 899                return 0;
 900
 901        p = br_port_get_rtnl(dev);
 902        /* We want to accept dev as bridge itself as well */
 903        if (!p && !(dev->priv_flags & IFF_EBRIDGE))
 904                return -EINVAL;
 905
 906        err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
 907        if (changed)
 908                /* Send RTM_NEWLINK because userspace
 909                 * expects RTM_NEWLINK for vlan dels
 910                 */
 911                br_ifinfo_notify(RTM_NEWLINK, br, p);
 912
 913        return err;
 914}
 915
 916static int br_validate(struct nlattr *tb[], struct nlattr *data[],
 917                       struct netlink_ext_ack *extack)
 918{
 919        if (tb[IFLA_ADDRESS]) {
 920                if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
 921                        return -EINVAL;
 922                if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
 923                        return -EADDRNOTAVAIL;
 924        }
 925
 926        if (!data)
 927                return 0;
 928
 929#ifdef CONFIG_BRIDGE_VLAN_FILTERING
 930        if (data[IFLA_BR_VLAN_PROTOCOL]) {
 931                switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
 932                case htons(ETH_P_8021Q):
 933                case htons(ETH_P_8021AD):
 934                        break;
 935                default:
 936                        return -EPROTONOSUPPORT;
 937                }
 938        }
 939
 940        if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
 941                __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
 942
 943                if (defpvid >= VLAN_VID_MASK)
 944                        return -EINVAL;
 945        }
 946#endif
 947
 948        return 0;
 949}
 950
 951static int br_port_slave_changelink(struct net_device *brdev,
 952                                    struct net_device *dev,
 953                                    struct nlattr *tb[],
 954                                    struct nlattr *data[],
 955                                    struct netlink_ext_ack *extack)
 956{
 957        struct net_bridge *br = netdev_priv(brdev);
 958        int ret;
 959
 960        if (!data)
 961                return 0;
 962
 963        spin_lock_bh(&br->lock);
 964        ret = br_setport(br_port_get_rtnl(dev), data);
 965        spin_unlock_bh(&br->lock);
 966
 967        return ret;
 968}
 969
 970static int br_port_fill_slave_info(struct sk_buff *skb,
 971                                   const struct net_device *brdev,
 972                                   const struct net_device *dev)
 973{
 974        return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
 975}
 976
 977static size_t br_port_get_slave_size(const struct net_device *brdev,
 978                                     const struct net_device *dev)
 979{
 980        return br_port_info_size();
 981}
 982
 983static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
 984        [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
 985        [IFLA_BR_HELLO_TIME]    = { .type = NLA_U32 },
 986        [IFLA_BR_MAX_AGE]       = { .type = NLA_U32 },
 987        [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
 988        [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
 989        [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
 990        [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
 991        [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
 992        [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
 993        [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
 994                                 .len  = ETH_ALEN },
 995        [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
 996        [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
 997        [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
 998        [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
 999        [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1000        [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1001        [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1002        [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1003        [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1004        [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1005        [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1006        [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1007        [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1008        [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1009        [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1010        [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1011        [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1012        [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1013        [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1014        [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1015        [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1016        [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1017        [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1018};
1019
1020static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1021                         struct nlattr *data[],
1022                         struct netlink_ext_ack *extack)
1023{
1024        struct net_bridge *br = netdev_priv(brdev);
1025        int err;
1026
1027        if (!data)
1028                return 0;
1029
1030        if (data[IFLA_BR_FORWARD_DELAY]) {
1031                err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1032                if (err)
1033                        return err;
1034        }
1035
1036        if (data[IFLA_BR_HELLO_TIME]) {
1037                err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1038                if (err)
1039                        return err;
1040        }
1041
1042        if (data[IFLA_BR_MAX_AGE]) {
1043                err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1044                if (err)
1045                        return err;
1046        }
1047
1048        if (data[IFLA_BR_AGEING_TIME]) {
1049                err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1050                if (err)
1051                        return err;
1052        }
1053
1054        if (data[IFLA_BR_STP_STATE]) {
1055                u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1056
1057                br_stp_set_enabled(br, stp_enabled);
1058        }
1059
1060        if (data[IFLA_BR_PRIORITY]) {
1061                u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1062
1063                br_stp_set_bridge_priority(br, priority);
1064        }
1065
1066        if (data[IFLA_BR_VLAN_FILTERING]) {
1067                u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1068
1069                err = __br_vlan_filter_toggle(br, vlan_filter);
1070                if (err)
1071                        return err;
1072        }
1073
1074#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1075        if (data[IFLA_BR_VLAN_PROTOCOL]) {
1076                __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1077
1078                err = __br_vlan_set_proto(br, vlan_proto);
1079                if (err)
1080                        return err;
1081        }
1082
1083        if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1084                __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1085
1086                err = __br_vlan_set_default_pvid(br, defpvid, extack);
1087                if (err)
1088                        return err;
1089        }
1090
1091        if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1092                __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1093
1094                err = br_vlan_set_stats(br, vlan_stats);
1095                if (err)
1096                        return err;
1097        }
1098
1099        if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1100                __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1101
1102                err = br_vlan_set_stats_per_port(br, per_port);
1103                if (err)
1104                        return err;
1105        }
1106#endif
1107
1108        if (data[IFLA_BR_GROUP_FWD_MASK]) {
1109                u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1110
1111                if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1112                        return -EINVAL;
1113                br->group_fwd_mask = fwd_mask;
1114        }
1115
1116        if (data[IFLA_BR_GROUP_ADDR]) {
1117                u8 new_addr[ETH_ALEN];
1118
1119                if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1120                        return -EINVAL;
1121                memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1122                if (!is_link_local_ether_addr(new_addr))
1123                        return -EINVAL;
1124                if (new_addr[5] == 1 ||         /* 802.3x Pause address */
1125                    new_addr[5] == 2 ||         /* 802.3ad Slow protocols */
1126                    new_addr[5] == 3)           /* 802.1X PAE address */
1127                        return -EINVAL;
1128                spin_lock_bh(&br->lock);
1129                memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1130                spin_unlock_bh(&br->lock);
1131                br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1132                br_recalculate_fwd_mask(br);
1133        }
1134
1135        if (data[IFLA_BR_FDB_FLUSH])
1136                br_fdb_flush(br);
1137
1138#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1139        if (data[IFLA_BR_MCAST_ROUTER]) {
1140                u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1141
1142                err = br_multicast_set_router(br, multicast_router);
1143                if (err)
1144                        return err;
1145        }
1146
1147        if (data[IFLA_BR_MCAST_SNOOPING]) {
1148                u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1149
1150                err = br_multicast_toggle(br, mcast_snooping);
1151                if (err)
1152                        return err;
1153        }
1154
1155        if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1156                u8 val;
1157
1158                val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1159                br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1160        }
1161
1162        if (data[IFLA_BR_MCAST_QUERIER]) {
1163                u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1164
1165                err = br_multicast_set_querier(br, mcast_querier);
1166                if (err)
1167                        return err;
1168        }
1169
1170        if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1171                u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1172
1173                br->hash_elasticity = val;
1174        }
1175
1176        if (data[IFLA_BR_MCAST_HASH_MAX]) {
1177                u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1178
1179                err = br_multicast_set_hash_max(br, hash_max);
1180                if (err)
1181                        return err;
1182        }
1183
1184        if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1185                u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1186
1187                br->multicast_last_member_count = val;
1188        }
1189
1190        if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1191                u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1192
1193                br->multicast_startup_query_count = val;
1194        }
1195
1196        if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1197                u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1198
1199                br->multicast_last_member_interval = clock_t_to_jiffies(val);
1200        }
1201
1202        if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1203                u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1204
1205                br->multicast_membership_interval = clock_t_to_jiffies(val);
1206        }
1207
1208        if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1209                u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1210
1211                br->multicast_querier_interval = clock_t_to_jiffies(val);
1212        }
1213
1214        if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1215                u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1216
1217                br->multicast_query_interval = clock_t_to_jiffies(val);
1218        }
1219
1220        if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1221                u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1222
1223                br->multicast_query_response_interval = clock_t_to_jiffies(val);
1224        }
1225
1226        if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1227                u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1228
1229                br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1230        }
1231
1232        if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1233                __u8 mcast_stats;
1234
1235                mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1236                br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1237        }
1238
1239        if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1240                __u8 igmp_version;
1241
1242                igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1243                err = br_multicast_set_igmp_version(br, igmp_version);
1244                if (err)
1245                        return err;
1246        }
1247
1248#if IS_ENABLED(CONFIG_IPV6)
1249        if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1250                __u8 mld_version;
1251
1252                mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1253                err = br_multicast_set_mld_version(br, mld_version);
1254                if (err)
1255                        return err;
1256        }
1257#endif
1258#endif
1259#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1260        if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1261                u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1262
1263                br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1264        }
1265
1266        if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1267                u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1268
1269                br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1270        }
1271
1272        if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1273                u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1274
1275                br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1276        }
1277#endif
1278
1279        return 0;
1280}
1281
1282static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1283                          struct nlattr *tb[], struct nlattr *data[],
1284                          struct netlink_ext_ack *extack)
1285{
1286        struct net_bridge *br = netdev_priv(dev);
1287        int err;
1288
1289        err = register_netdevice(dev);
1290        if (err)
1291                return err;
1292
1293        if (tb[IFLA_ADDRESS]) {
1294                spin_lock_bh(&br->lock);
1295                br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1296                spin_unlock_bh(&br->lock);
1297        }
1298
1299        err = br_changelink(dev, tb, data, extack);
1300        if (err)
1301                br_dev_delete(dev, NULL);
1302
1303        return err;
1304}
1305
1306static size_t br_get_size(const struct net_device *brdev)
1307{
1308        return nla_total_size(sizeof(u32)) +    /* IFLA_BR_FORWARD_DELAY  */
1309               nla_total_size(sizeof(u32)) +    /* IFLA_BR_HELLO_TIME */
1310               nla_total_size(sizeof(u32)) +    /* IFLA_BR_MAX_AGE */
1311               nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1312               nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1313               nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1314               nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1315#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1316               nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1317               nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1318               nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_ENABLED */
1319               nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_PER_PORT */
1320#endif
1321               nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1322               nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1323               nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1324               nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1325               nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1326               nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1327               nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1328               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1329               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1330               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1331               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1332               nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1333#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1334               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1335               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1336               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1337               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1338               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_STATS_ENABLED */
1339               nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1340               nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1341               nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1342               nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1343               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1344               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1345               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1346               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1347               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1348               nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1349               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_IGMP_VERSION */
1350               nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_MLD_VERSION */
1351#endif
1352#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1353               nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1354               nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1355               nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1356#endif
1357               0;
1358}
1359
1360static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1361{
1362        struct net_bridge *br = netdev_priv(brdev);
1363        u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1364        u32 hello_time = jiffies_to_clock_t(br->hello_time);
1365        u32 age_time = jiffies_to_clock_t(br->max_age);
1366        u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1367        u32 stp_enabled = br->stp_enabled;
1368        u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1369        u8 vlan_enabled = br_vlan_enabled(br->dev);
1370        u64 clockval;
1371
1372        clockval = br_timer_value(&br->hello_timer);
1373        if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1374                return -EMSGSIZE;
1375        clockval = br_timer_value(&br->tcn_timer);
1376        if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1377                return -EMSGSIZE;
1378        clockval = br_timer_value(&br->topology_change_timer);
1379        if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1380                              IFLA_BR_PAD))
1381                return -EMSGSIZE;
1382        clockval = br_timer_value(&br->gc_work.timer);
1383        if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1384                return -EMSGSIZE;
1385
1386        if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1387            nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1388            nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1389            nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1390            nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1391            nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1392            nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1393            nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1394            nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1395                    &br->bridge_id) ||
1396            nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1397                    &br->designated_root) ||
1398            nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1399            nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1400            nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1401            nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1402                       br->topology_change_detected) ||
1403            nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1404                return -EMSGSIZE;
1405
1406#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1407        if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1408            nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1409            nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1410                       br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1411            nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1412                       br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1413                return -EMSGSIZE;
1414#endif
1415#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1416        if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1417            nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1418                       br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1419            nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1420                       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1421            nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1422                       br_opt_get(br, BROPT_MULTICAST_QUERIER)) ||
1423            nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1424                       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1425            nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1426                        br->hash_elasticity) ||
1427            nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1428            nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1429                        br->multicast_last_member_count) ||
1430            nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1431                        br->multicast_startup_query_count) ||
1432            nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1433                       br->multicast_igmp_version))
1434                return -EMSGSIZE;
1435#if IS_ENABLED(CONFIG_IPV6)
1436        if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1437                       br->multicast_mld_version))
1438                return -EMSGSIZE;
1439#endif
1440        clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1441        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1442                              IFLA_BR_PAD))
1443                return -EMSGSIZE;
1444        clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1445        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1446                              IFLA_BR_PAD))
1447                return -EMSGSIZE;
1448        clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1449        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1450                              IFLA_BR_PAD))
1451                return -EMSGSIZE;
1452        clockval = jiffies_to_clock_t(br->multicast_query_interval);
1453        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1454                              IFLA_BR_PAD))
1455                return -EMSGSIZE;
1456        clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1457        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1458                              IFLA_BR_PAD))
1459                return -EMSGSIZE;
1460        clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1461        if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1462                              IFLA_BR_PAD))
1463                return -EMSGSIZE;
1464#endif
1465#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1466        if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1467                       br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1468            nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1469                       br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1470            nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1471                       br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1472                return -EMSGSIZE;
1473#endif
1474
1475        return 0;
1476}
1477
1478static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1479{
1480        struct net_bridge_port *p = NULL;
1481        struct net_bridge_vlan_group *vg;
1482        struct net_bridge_vlan *v;
1483        struct net_bridge *br;
1484        int numvls = 0;
1485
1486        switch (attr) {
1487        case IFLA_STATS_LINK_XSTATS:
1488                br = netdev_priv(dev);
1489                vg = br_vlan_group(br);
1490                break;
1491        case IFLA_STATS_LINK_XSTATS_SLAVE:
1492                p = br_port_get_rtnl(dev);
1493                if (!p)
1494                        return 0;
1495                br = p->br;
1496                vg = nbp_vlan_group(p);
1497                break;
1498        default:
1499                return 0;
1500        }
1501
1502        if (vg) {
1503                /* we need to count all, even placeholder entries */
1504                list_for_each_entry(v, &vg->vlan_list, vlist)
1505                        numvls++;
1506        }
1507
1508        return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1509               nla_total_size(sizeof(struct br_mcast_stats)) +
1510               nla_total_size(0);
1511}
1512
1513static int br_fill_linkxstats(struct sk_buff *skb,
1514                              const struct net_device *dev,
1515                              int *prividx, int attr)
1516{
1517        struct nlattr *nla __maybe_unused;
1518        struct net_bridge_port *p = NULL;
1519        struct net_bridge_vlan_group *vg;
1520        struct net_bridge_vlan *v;
1521        struct net_bridge *br;
1522        struct nlattr *nest;
1523        int vl_idx = 0;
1524
1525        switch (attr) {
1526        case IFLA_STATS_LINK_XSTATS:
1527                br = netdev_priv(dev);
1528                vg = br_vlan_group(br);
1529                break;
1530        case IFLA_STATS_LINK_XSTATS_SLAVE:
1531                p = br_port_get_rtnl(dev);
1532                if (!p)
1533                        return 0;
1534                br = p->br;
1535                vg = nbp_vlan_group(p);
1536                break;
1537        default:
1538                return -EINVAL;
1539        }
1540
1541        nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1542        if (!nest)
1543                return -EMSGSIZE;
1544
1545        if (vg) {
1546                u16 pvid;
1547
1548                pvid = br_get_pvid(vg);
1549                list_for_each_entry(v, &vg->vlan_list, vlist) {
1550                        struct bridge_vlan_xstats vxi;
1551                        struct br_vlan_stats stats;
1552
1553                        if (++vl_idx < *prividx)
1554                                continue;
1555                        memset(&vxi, 0, sizeof(vxi));
1556                        vxi.vid = v->vid;
1557                        vxi.flags = v->flags;
1558                        if (v->vid == pvid)
1559                                vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1560                        br_vlan_get_stats(v, &stats);
1561                        vxi.rx_bytes = stats.rx_bytes;
1562                        vxi.rx_packets = stats.rx_packets;
1563                        vxi.tx_bytes = stats.tx_bytes;
1564                        vxi.tx_packets = stats.tx_packets;
1565
1566                        if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1567                                goto nla_put_failure;
1568                }
1569        }
1570
1571#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1572        if (++vl_idx >= *prividx) {
1573                nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1574                                        sizeof(struct br_mcast_stats),
1575                                        BRIDGE_XSTATS_PAD);
1576                if (!nla)
1577                        goto nla_put_failure;
1578                br_multicast_get_stats(br, p, nla_data(nla));
1579        }
1580#endif
1581        nla_nest_end(skb, nest);
1582        *prividx = 0;
1583
1584        return 0;
1585
1586nla_put_failure:
1587        nla_nest_end(skb, nest);
1588        *prividx = vl_idx;
1589
1590        return -EMSGSIZE;
1591}
1592
1593static struct rtnl_af_ops br_af_ops __read_mostly = {
1594        .family                 = AF_BRIDGE,
1595        .get_link_af_size       = br_get_link_af_size_filtered,
1596};
1597
1598struct rtnl_link_ops br_link_ops __read_mostly = {
1599        .kind                   = "bridge",
1600        .priv_size              = sizeof(struct net_bridge),
1601        .setup                  = br_dev_setup,
1602        .maxtype                = IFLA_BR_MAX,
1603        .policy                 = br_policy,
1604        .validate               = br_validate,
1605        .newlink                = br_dev_newlink,
1606        .changelink             = br_changelink,
1607        .dellink                = br_dev_delete,
1608        .get_size               = br_get_size,
1609        .fill_info              = br_fill_info,
1610        .fill_linkxstats        = br_fill_linkxstats,
1611        .get_linkxstats_size    = br_get_linkxstats_size,
1612
1613        .slave_maxtype          = IFLA_BRPORT_MAX,
1614        .slave_policy           = br_port_policy,
1615        .slave_changelink       = br_port_slave_changelink,
1616        .get_slave_size         = br_port_get_slave_size,
1617        .fill_slave_info        = br_port_fill_slave_info,
1618};
1619
1620int __init br_netlink_init(void)
1621{
1622        int err;
1623
1624        br_mdb_init();
1625        rtnl_af_register(&br_af_ops);
1626
1627        err = rtnl_link_register(&br_link_ops);
1628        if (err)
1629                goto out_af;
1630
1631        return 0;
1632
1633out_af:
1634        rtnl_af_unregister(&br_af_ops);
1635        br_mdb_uninit();
1636        return err;
1637}
1638
1639void br_netlink_fini(void)
1640{
1641        br_mdb_uninit();
1642        rtnl_af_unregister(&br_af_ops);
1643        rtnl_link_unregister(&br_link_ops);
1644}
1645