Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
0004  * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
0005  * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/errno.h>
0010 #include <linux/netdevice.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/if_link.h>
0013 #include <linux/if_ether.h>
0014 #include <net/netlink.h>
0015 #include <net/rtnetlink.h>
0016 #include <net/bonding.h>
0017 #include <net/ipv6.h>
0018 
0019 static size_t bond_get_slave_size(const struct net_device *bond_dev,
0020                   const struct net_device *slave_dev)
0021 {
0022     return nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_STATE */
0023         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_SLAVE_MII_STATUS */
0024         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_SLAVE_LINK_FAILURE_COUNT */
0025         nla_total_size(MAX_ADDR_LEN) +  /* IFLA_BOND_SLAVE_PERM_HWADDR */
0026         nla_total_size(sizeof(u16)) +   /* IFLA_BOND_SLAVE_QUEUE_ID */
0027         nla_total_size(sizeof(u16)) +   /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
0028         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
0029         nla_total_size(sizeof(u16)) +   /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
0030         nla_total_size(sizeof(s32)) +   /* IFLA_BOND_SLAVE_PRIO */
0031         0;
0032 }
0033 
0034 static int bond_fill_slave_info(struct sk_buff *skb,
0035                 const struct net_device *bond_dev,
0036                 const struct net_device *slave_dev)
0037 {
0038     struct slave *slave = bond_slave_get_rtnl(slave_dev);
0039 
0040     if (nla_put_u8(skb, IFLA_BOND_SLAVE_STATE, bond_slave_state(slave)))
0041         goto nla_put_failure;
0042 
0043     if (nla_put_u8(skb, IFLA_BOND_SLAVE_MII_STATUS, slave->link))
0044         goto nla_put_failure;
0045 
0046     if (nla_put_u32(skb, IFLA_BOND_SLAVE_LINK_FAILURE_COUNT,
0047             slave->link_failure_count))
0048         goto nla_put_failure;
0049 
0050     if (nla_put(skb, IFLA_BOND_SLAVE_PERM_HWADDR,
0051             slave_dev->addr_len, slave->perm_hwaddr))
0052         goto nla_put_failure;
0053 
0054     if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
0055         goto nla_put_failure;
0056 
0057     if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
0058         goto nla_put_failure;
0059 
0060     if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
0061         const struct aggregator *agg;
0062         const struct port *ad_port;
0063 
0064         ad_port = &SLAVE_AD_INFO(slave)->port;
0065         agg = SLAVE_AD_INFO(slave)->port.aggregator;
0066         if (agg) {
0067             if (nla_put_u16(skb, IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
0068                     agg->aggregator_identifier))
0069                 goto nla_put_failure;
0070             if (nla_put_u8(skb,
0071                        IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
0072                        ad_port->actor_oper_port_state))
0073                 goto nla_put_failure;
0074             if (nla_put_u16(skb,
0075                     IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
0076                     ad_port->partner_oper.port_state))
0077                 goto nla_put_failure;
0078         }
0079     }
0080 
0081     return 0;
0082 
0083 nla_put_failure:
0084     return -EMSGSIZE;
0085 }
0086 
0087 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
0088     [IFLA_BOND_MODE]        = { .type = NLA_U8 },
0089     [IFLA_BOND_ACTIVE_SLAVE]    = { .type = NLA_U32 },
0090     [IFLA_BOND_MIIMON]      = { .type = NLA_U32 },
0091     [IFLA_BOND_UPDELAY]     = { .type = NLA_U32 },
0092     [IFLA_BOND_DOWNDELAY]       = { .type = NLA_U32 },
0093     [IFLA_BOND_USE_CARRIER]     = { .type = NLA_U8 },
0094     [IFLA_BOND_ARP_INTERVAL]    = { .type = NLA_U32 },
0095     [IFLA_BOND_ARP_IP_TARGET]   = { .type = NLA_NESTED },
0096     [IFLA_BOND_ARP_VALIDATE]    = { .type = NLA_U32 },
0097     [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
0098     [IFLA_BOND_PRIMARY]     = { .type = NLA_U32 },
0099     [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NLA_U8 },
0100     [IFLA_BOND_FAIL_OVER_MAC]   = { .type = NLA_U8 },
0101     [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NLA_U8 },
0102     [IFLA_BOND_RESEND_IGMP]     = { .type = NLA_U32 },
0103     [IFLA_BOND_NUM_PEER_NOTIF]  = { .type = NLA_U8 },
0104     [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NLA_U8 },
0105     [IFLA_BOND_MIN_LINKS]       = { .type = NLA_U32 },
0106     [IFLA_BOND_LP_INTERVAL]     = { .type = NLA_U32 },
0107     [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NLA_U32 },
0108     [IFLA_BOND_AD_LACP_ACTIVE]  = { .type = NLA_U8 },
0109     [IFLA_BOND_AD_LACP_RATE]    = { .type = NLA_U8 },
0110     [IFLA_BOND_AD_SELECT]       = { .type = NLA_U8 },
0111     [IFLA_BOND_AD_INFO]     = { .type = NLA_NESTED },
0112     [IFLA_BOND_AD_ACTOR_SYS_PRIO]   = { .type = NLA_U16 },
0113     [IFLA_BOND_AD_USER_PORT_KEY]    = { .type = NLA_U16 },
0114     [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NLA_BINARY,
0115                         .len  = ETH_ALEN },
0116     [IFLA_BOND_TLB_DYNAMIC_LB]  = { .type = NLA_U8 },
0117     [IFLA_BOND_PEER_NOTIF_DELAY]    = { .type = NLA_U32 },
0118     [IFLA_BOND_MISSED_MAX]      = { .type = NLA_U8 },
0119     [IFLA_BOND_NS_IP6_TARGET]   = { .type = NLA_NESTED },
0120 };
0121 
0122 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
0123     [IFLA_BOND_SLAVE_QUEUE_ID]  = { .type = NLA_U16 },
0124     [IFLA_BOND_SLAVE_PRIO]      = { .type = NLA_S32 },
0125 };
0126 
0127 static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
0128              struct netlink_ext_ack *extack)
0129 {
0130     if (tb[IFLA_ADDRESS]) {
0131         if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
0132             return -EINVAL;
0133         if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
0134             return -EADDRNOTAVAIL;
0135     }
0136     return 0;
0137 }
0138 
0139 static int bond_slave_changelink(struct net_device *bond_dev,
0140                  struct net_device *slave_dev,
0141                  struct nlattr *tb[], struct nlattr *data[],
0142                  struct netlink_ext_ack *extack)
0143 {
0144     struct bonding *bond = netdev_priv(bond_dev);
0145     struct bond_opt_value newval;
0146     int err;
0147 
0148     if (!data)
0149         return 0;
0150 
0151     if (data[IFLA_BOND_SLAVE_QUEUE_ID]) {
0152         u16 queue_id = nla_get_u16(data[IFLA_BOND_SLAVE_QUEUE_ID]);
0153         char queue_id_str[IFNAMSIZ + 7];
0154 
0155         /* queue_id option setting expects slave_name:queue_id */
0156         snprintf(queue_id_str, sizeof(queue_id_str), "%s:%u\n",
0157              slave_dev->name, queue_id);
0158         bond_opt_initstr(&newval, queue_id_str);
0159         err = __bond_opt_set(bond, BOND_OPT_QUEUE_ID, &newval,
0160                      data[IFLA_BOND_SLAVE_QUEUE_ID], extack);
0161         if (err)
0162             return err;
0163     }
0164 
0165     if (data[IFLA_BOND_SLAVE_PRIO]) {
0166         int prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
0167 
0168         bond_opt_slave_initval(&newval, &slave_dev, prio);
0169         err = __bond_opt_set(bond, BOND_OPT_PRIO, &newval,
0170                      data[IFLA_BOND_SLAVE_PRIO], extack);
0171         if (err)
0172             return err;
0173     }
0174 
0175     return 0;
0176 }
0177 
0178 static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[],
0179                struct nlattr *data[],
0180                struct netlink_ext_ack *extack)
0181 {
0182     struct bonding *bond = netdev_priv(bond_dev);
0183     struct bond_opt_value newval;
0184     int miimon = 0;
0185     int err;
0186 
0187     if (!data)
0188         return 0;
0189 
0190     if (data[IFLA_BOND_MODE]) {
0191         int mode = nla_get_u8(data[IFLA_BOND_MODE]);
0192 
0193         bond_opt_initval(&newval, mode);
0194         err = __bond_opt_set(bond, BOND_OPT_MODE, &newval,
0195                      data[IFLA_BOND_MODE], extack);
0196         if (err)
0197             return err;
0198     }
0199     if (data[IFLA_BOND_ACTIVE_SLAVE]) {
0200         int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
0201         struct net_device *slave_dev;
0202         char *active_slave = "";
0203 
0204         if (ifindex != 0) {
0205             slave_dev = __dev_get_by_index(dev_net(bond_dev),
0206                                ifindex);
0207             if (!slave_dev)
0208                 return -ENODEV;
0209             active_slave = slave_dev->name;
0210         }
0211         bond_opt_initstr(&newval, active_slave);
0212         err = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval,
0213                      data[IFLA_BOND_ACTIVE_SLAVE], extack);
0214         if (err)
0215             return err;
0216     }
0217     if (data[IFLA_BOND_MIIMON]) {
0218         miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
0219 
0220         bond_opt_initval(&newval, miimon);
0221         err = __bond_opt_set(bond, BOND_OPT_MIIMON, &newval,
0222                      data[IFLA_BOND_MIIMON], extack);
0223         if (err)
0224             return err;
0225     }
0226     if (data[IFLA_BOND_UPDELAY]) {
0227         int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
0228 
0229         bond_opt_initval(&newval, updelay);
0230         err = __bond_opt_set(bond, BOND_OPT_UPDELAY, &newval,
0231                      data[IFLA_BOND_UPDELAY], extack);
0232         if (err)
0233             return err;
0234     }
0235     if (data[IFLA_BOND_DOWNDELAY]) {
0236         int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
0237 
0238         bond_opt_initval(&newval, downdelay);
0239         err = __bond_opt_set(bond, BOND_OPT_DOWNDELAY, &newval,
0240                      data[IFLA_BOND_DOWNDELAY], extack);
0241         if (err)
0242             return err;
0243     }
0244     if (data[IFLA_BOND_PEER_NOTIF_DELAY]) {
0245         int delay = nla_get_u32(data[IFLA_BOND_PEER_NOTIF_DELAY]);
0246 
0247         bond_opt_initval(&newval, delay);
0248         err = __bond_opt_set(bond, BOND_OPT_PEER_NOTIF_DELAY, &newval,
0249                      data[IFLA_BOND_PEER_NOTIF_DELAY], extack);
0250         if (err)
0251             return err;
0252     }
0253     if (data[IFLA_BOND_USE_CARRIER]) {
0254         int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
0255 
0256         bond_opt_initval(&newval, use_carrier);
0257         err = __bond_opt_set(bond, BOND_OPT_USE_CARRIER, &newval,
0258                      data[IFLA_BOND_USE_CARRIER], extack);
0259         if (err)
0260             return err;
0261     }
0262     if (data[IFLA_BOND_ARP_INTERVAL]) {
0263         int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
0264 
0265         if (arp_interval && miimon) {
0266             NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
0267                         "ARP monitoring cannot be used with MII monitoring");
0268             return -EINVAL;
0269         }
0270 
0271         bond_opt_initval(&newval, arp_interval);
0272         err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval,
0273                      data[IFLA_BOND_ARP_INTERVAL], extack);
0274         if (err)
0275             return err;
0276     }
0277     if (data[IFLA_BOND_ARP_IP_TARGET]) {
0278         struct nlattr *attr;
0279         int i = 0, rem;
0280 
0281         bond_option_arp_ip_targets_clear(bond);
0282         nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
0283             __be32 target;
0284 
0285             if (nla_len(attr) < sizeof(target))
0286                 return -EINVAL;
0287 
0288             target = nla_get_be32(attr);
0289 
0290             bond_opt_initval(&newval, (__force u64)target);
0291             err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS,
0292                          &newval,
0293                          data[IFLA_BOND_ARP_IP_TARGET],
0294                          extack);
0295             if (err)
0296                 break;
0297             i++;
0298         }
0299         if (i == 0 && bond->params.arp_interval)
0300             netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
0301         if (err)
0302             return err;
0303     }
0304 #if IS_ENABLED(CONFIG_IPV6)
0305     if (data[IFLA_BOND_NS_IP6_TARGET]) {
0306         struct nlattr *attr;
0307         int i = 0, rem;
0308 
0309         bond_option_ns_ip6_targets_clear(bond);
0310         nla_for_each_nested(attr, data[IFLA_BOND_NS_IP6_TARGET], rem) {
0311             struct in6_addr addr6;
0312 
0313             if (nla_len(attr) < sizeof(addr6)) {
0314                 NL_SET_ERR_MSG(extack, "Invalid IPv6 address");
0315                 return -EINVAL;
0316             }
0317 
0318             addr6 = nla_get_in6_addr(attr);
0319 
0320             bond_opt_initextra(&newval, &addr6, sizeof(addr6));
0321             err = __bond_opt_set(bond, BOND_OPT_NS_TARGETS,
0322                          &newval,
0323                          data[IFLA_BOND_NS_IP6_TARGET],
0324                          extack);
0325             if (err)
0326                 break;
0327             i++;
0328         }
0329         if (i == 0 && bond->params.arp_interval)
0330             netdev_warn(bond->dev, "Removing last ns target with arp_interval on\n");
0331         if (err)
0332             return err;
0333     }
0334 #endif
0335     if (data[IFLA_BOND_ARP_VALIDATE]) {
0336         int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
0337 
0338         if (arp_validate && miimon) {
0339             NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
0340                         "ARP validating cannot be used with MII monitoring");
0341             return -EINVAL;
0342         }
0343 
0344         bond_opt_initval(&newval, arp_validate);
0345         err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval,
0346                      data[IFLA_BOND_ARP_VALIDATE], extack);
0347         if (err)
0348             return err;
0349     }
0350     if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
0351         int arp_all_targets =
0352             nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
0353 
0354         bond_opt_initval(&newval, arp_all_targets);
0355         err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval,
0356                      data[IFLA_BOND_ARP_ALL_TARGETS], extack);
0357         if (err)
0358             return err;
0359     }
0360     if (data[IFLA_BOND_PRIMARY]) {
0361         int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
0362         struct net_device *dev;
0363         char *primary = "";
0364 
0365         dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
0366         if (dev)
0367             primary = dev->name;
0368 
0369         bond_opt_initstr(&newval, primary);
0370         err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval,
0371                      data[IFLA_BOND_PRIMARY], extack);
0372         if (err)
0373             return err;
0374     }
0375     if (data[IFLA_BOND_PRIMARY_RESELECT]) {
0376         int primary_reselect =
0377             nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
0378 
0379         bond_opt_initval(&newval, primary_reselect);
0380         err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval,
0381                      data[IFLA_BOND_PRIMARY_RESELECT], extack);
0382         if (err)
0383             return err;
0384     }
0385     if (data[IFLA_BOND_FAIL_OVER_MAC]) {
0386         int fail_over_mac =
0387             nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
0388 
0389         bond_opt_initval(&newval, fail_over_mac);
0390         err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval,
0391                      data[IFLA_BOND_FAIL_OVER_MAC], extack);
0392         if (err)
0393             return err;
0394     }
0395     if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
0396         int xmit_hash_policy =
0397             nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
0398 
0399         bond_opt_initval(&newval, xmit_hash_policy);
0400         err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval,
0401                      data[IFLA_BOND_XMIT_HASH_POLICY], extack);
0402         if (err)
0403             return err;
0404     }
0405     if (data[IFLA_BOND_RESEND_IGMP]) {
0406         int resend_igmp =
0407             nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
0408 
0409         bond_opt_initval(&newval, resend_igmp);
0410         err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval,
0411                      data[IFLA_BOND_RESEND_IGMP], extack);
0412         if (err)
0413             return err;
0414     }
0415     if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
0416         int num_peer_notif =
0417             nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
0418 
0419         bond_opt_initval(&newval, num_peer_notif);
0420         err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval,
0421                      data[IFLA_BOND_NUM_PEER_NOTIF], extack);
0422         if (err)
0423             return err;
0424     }
0425     if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
0426         int all_slaves_active =
0427             nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
0428 
0429         bond_opt_initval(&newval, all_slaves_active);
0430         err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval,
0431                      data[IFLA_BOND_ALL_SLAVES_ACTIVE], extack);
0432         if (err)
0433             return err;
0434     }
0435     if (data[IFLA_BOND_MIN_LINKS]) {
0436         int min_links =
0437             nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
0438 
0439         bond_opt_initval(&newval, min_links);
0440         err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval,
0441                      data[IFLA_BOND_MIN_LINKS], extack);
0442         if (err)
0443             return err;
0444     }
0445     if (data[IFLA_BOND_LP_INTERVAL]) {
0446         int lp_interval =
0447             nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
0448 
0449         bond_opt_initval(&newval, lp_interval);
0450         err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval,
0451                      data[IFLA_BOND_LP_INTERVAL], extack);
0452         if (err)
0453             return err;
0454     }
0455     if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
0456         int packets_per_slave =
0457             nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
0458 
0459         bond_opt_initval(&newval, packets_per_slave);
0460         err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval,
0461                      data[IFLA_BOND_PACKETS_PER_SLAVE], extack);
0462         if (err)
0463             return err;
0464     }
0465 
0466     if (data[IFLA_BOND_AD_LACP_ACTIVE]) {
0467         int lacp_active = nla_get_u8(data[IFLA_BOND_AD_LACP_ACTIVE]);
0468 
0469         bond_opt_initval(&newval, lacp_active);
0470         err = __bond_opt_set(bond, BOND_OPT_LACP_ACTIVE, &newval,
0471                      data[IFLA_BOND_AD_LACP_ACTIVE], extack);
0472         if (err)
0473             return err;
0474     }
0475 
0476     if (data[IFLA_BOND_AD_LACP_RATE]) {
0477         int lacp_rate =
0478             nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
0479 
0480         bond_opt_initval(&newval, lacp_rate);
0481         err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval,
0482                      data[IFLA_BOND_AD_LACP_RATE], extack);
0483         if (err)
0484             return err;
0485     }
0486     if (data[IFLA_BOND_AD_SELECT]) {
0487         int ad_select =
0488             nla_get_u8(data[IFLA_BOND_AD_SELECT]);
0489 
0490         bond_opt_initval(&newval, ad_select);
0491         err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval,
0492                      data[IFLA_BOND_AD_SELECT], extack);
0493         if (err)
0494             return err;
0495     }
0496     if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) {
0497         int actor_sys_prio =
0498             nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]);
0499 
0500         bond_opt_initval(&newval, actor_sys_prio);
0501         err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval,
0502                      data[IFLA_BOND_AD_ACTOR_SYS_PRIO], extack);
0503         if (err)
0504             return err;
0505     }
0506     if (data[IFLA_BOND_AD_USER_PORT_KEY]) {
0507         int port_key =
0508             nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]);
0509 
0510         bond_opt_initval(&newval, port_key);
0511         err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval,
0512                      data[IFLA_BOND_AD_USER_PORT_KEY], extack);
0513         if (err)
0514             return err;
0515     }
0516     if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) {
0517         if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN)
0518             return -EINVAL;
0519 
0520         bond_opt_initval(&newval,
0521                  nla_get_u64(data[IFLA_BOND_AD_ACTOR_SYSTEM]));
0522         err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval,
0523                      data[IFLA_BOND_AD_ACTOR_SYSTEM], extack);
0524         if (err)
0525             return err;
0526     }
0527     if (data[IFLA_BOND_TLB_DYNAMIC_LB]) {
0528         int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]);
0529 
0530         bond_opt_initval(&newval, dynamic_lb);
0531         err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval,
0532                      data[IFLA_BOND_TLB_DYNAMIC_LB], extack);
0533         if (err)
0534             return err;
0535     }
0536 
0537     if (data[IFLA_BOND_MISSED_MAX]) {
0538         int missed_max = nla_get_u8(data[IFLA_BOND_MISSED_MAX]);
0539 
0540         bond_opt_initval(&newval, missed_max);
0541         err = __bond_opt_set(bond, BOND_OPT_MISSED_MAX, &newval,
0542                      data[IFLA_BOND_MISSED_MAX], extack);
0543         if (err)
0544             return err;
0545     }
0546 
0547     return 0;
0548 }
0549 
0550 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
0551             struct nlattr *tb[], struct nlattr *data[],
0552             struct netlink_ext_ack *extack)
0553 {
0554     int err;
0555 
0556     err = bond_changelink(bond_dev, tb, data, extack);
0557     if (err < 0)
0558         return err;
0559 
0560     err = register_netdevice(bond_dev);
0561     if (!err) {
0562         struct bonding *bond = netdev_priv(bond_dev);
0563 
0564         netif_carrier_off(bond_dev);
0565         bond_work_init_all(bond);
0566     }
0567 
0568     return err;
0569 }
0570 
0571 static size_t bond_get_size(const struct net_device *bond_dev)
0572 {
0573     return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
0574         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ACTIVE_SLAVE */
0575         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIIMON */
0576         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_UPDELAY */
0577         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_DOWNDELAY */
0578         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_USE_CARRIER */
0579         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_INTERVAL */
0580                         /* IFLA_BOND_ARP_IP_TARGET */
0581         nla_total_size(sizeof(struct nlattr)) +
0582         nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
0583         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_VALIDATE */
0584         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_ALL_TARGETS */
0585         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_PRIMARY */
0586         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_PRIMARY_RESELECT */
0587         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_FAIL_OVER_MAC */
0588         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_XMIT_HASH_POLICY */
0589         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_RESEND_IGMP */
0590         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_NUM_PEER_NOTIF */
0591         nla_total_size(sizeof(u8)) +   /* IFLA_BOND_ALL_SLAVES_ACTIVE */
0592         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIN_LINKS */
0593         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_LP_INTERVAL */
0594         nla_total_size(sizeof(u32)) +  /* IFLA_BOND_PACKETS_PER_SLAVE */
0595         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_LACP_ACTIVE */
0596         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_LACP_RATE */
0597         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_SELECT */
0598         nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
0599         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
0600         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
0601         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
0602         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
0603         nla_total_size(ETH_ALEN) +    /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
0604         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */
0605         nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */
0606         nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */
0607         nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */
0608         nla_total_size(sizeof(u32)) +   /* IFLA_BOND_PEER_NOTIF_DELAY */
0609         nla_total_size(sizeof(u8)) +    /* IFLA_BOND_MISSED_MAX */
0610                         /* IFLA_BOND_NS_IP6_TARGET */
0611         nla_total_size(sizeof(struct nlattr)) +
0612         nla_total_size(sizeof(struct in6_addr)) * BOND_MAX_NS_TARGETS +
0613         0;
0614 }
0615 
0616 static int bond_option_active_slave_get_ifindex(struct bonding *bond)
0617 {
0618     const struct net_device *slave;
0619     int ifindex;
0620 
0621     rcu_read_lock();
0622     slave = bond_option_active_slave_get_rcu(bond);
0623     ifindex = slave ? slave->ifindex : 0;
0624     rcu_read_unlock();
0625     return ifindex;
0626 }
0627 
0628 static int bond_fill_info(struct sk_buff *skb,
0629               const struct net_device *bond_dev)
0630 {
0631     struct bonding *bond = netdev_priv(bond_dev);
0632     unsigned int packets_per_slave;
0633     int ifindex, i, targets_added;
0634     struct nlattr *targets;
0635     struct slave *primary;
0636 
0637     if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond)))
0638         goto nla_put_failure;
0639 
0640     ifindex = bond_option_active_slave_get_ifindex(bond);
0641     if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex))
0642         goto nla_put_failure;
0643 
0644     if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
0645         goto nla_put_failure;
0646 
0647     if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
0648             bond->params.updelay * bond->params.miimon))
0649         goto nla_put_failure;
0650 
0651     if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
0652             bond->params.downdelay * bond->params.miimon))
0653         goto nla_put_failure;
0654 
0655     if (nla_put_u32(skb, IFLA_BOND_PEER_NOTIF_DELAY,
0656             bond->params.peer_notif_delay * bond->params.miimon))
0657         goto nla_put_failure;
0658 
0659     if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
0660         goto nla_put_failure;
0661 
0662     if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
0663         goto nla_put_failure;
0664 
0665     targets = nla_nest_start_noflag(skb, IFLA_BOND_ARP_IP_TARGET);
0666     if (!targets)
0667         goto nla_put_failure;
0668 
0669     targets_added = 0;
0670     for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
0671         if (bond->params.arp_targets[i]) {
0672             if (nla_put_be32(skb, i, bond->params.arp_targets[i]))
0673                 goto nla_put_failure;
0674             targets_added = 1;
0675         }
0676     }
0677 
0678     if (targets_added)
0679         nla_nest_end(skb, targets);
0680     else
0681         nla_nest_cancel(skb, targets);
0682 
0683     if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
0684         goto nla_put_failure;
0685 
0686     if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
0687             bond->params.arp_all_targets))
0688         goto nla_put_failure;
0689 
0690 #if IS_ENABLED(CONFIG_IPV6)
0691     targets = nla_nest_start(skb, IFLA_BOND_NS_IP6_TARGET);
0692     if (!targets)
0693         goto nla_put_failure;
0694 
0695     targets_added = 0;
0696     for (i = 0; i < BOND_MAX_NS_TARGETS; i++) {
0697         if (!ipv6_addr_any(&bond->params.ns_targets[i])) {
0698             if (nla_put_in6_addr(skb, i, &bond->params.ns_targets[i]))
0699                 goto nla_put_failure;
0700             targets_added = 1;
0701         }
0702     }
0703 
0704     if (targets_added)
0705         nla_nest_end(skb, targets);
0706     else
0707         nla_nest_cancel(skb, targets);
0708 #endif
0709 
0710     primary = rtnl_dereference(bond->primary_slave);
0711     if (primary &&
0712         nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex))
0713         goto nla_put_failure;
0714 
0715     if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
0716                bond->params.primary_reselect))
0717         goto nla_put_failure;
0718 
0719     if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
0720                bond->params.fail_over_mac))
0721         goto nla_put_failure;
0722 
0723     if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
0724                bond->params.xmit_policy))
0725         goto nla_put_failure;
0726 
0727     if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
0728             bond->params.resend_igmp))
0729         goto nla_put_failure;
0730 
0731     if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
0732                bond->params.num_peer_notif))
0733         goto nla_put_failure;
0734 
0735     if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
0736                bond->params.all_slaves_active))
0737         goto nla_put_failure;
0738 
0739     if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
0740             bond->params.min_links))
0741         goto nla_put_failure;
0742 
0743     if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
0744             bond->params.lp_interval))
0745         goto nla_put_failure;
0746 
0747     packets_per_slave = bond->params.packets_per_slave;
0748     if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
0749             packets_per_slave))
0750         goto nla_put_failure;
0751 
0752     if (nla_put_u8(skb, IFLA_BOND_AD_LACP_ACTIVE,
0753                bond->params.lacp_active))
0754         goto nla_put_failure;
0755 
0756     if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
0757                bond->params.lacp_fast))
0758         goto nla_put_failure;
0759 
0760     if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
0761                bond->params.ad_select))
0762         goto nla_put_failure;
0763 
0764     if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB,
0765                bond->params.tlb_dynamic_lb))
0766         goto nla_put_failure;
0767 
0768     if (nla_put_u8(skb, IFLA_BOND_MISSED_MAX,
0769                bond->params.missed_max))
0770         goto nla_put_failure;
0771 
0772     if (BOND_MODE(bond) == BOND_MODE_8023AD) {
0773         struct ad_info info;
0774 
0775         if (capable(CAP_NET_ADMIN)) {
0776             if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO,
0777                     bond->params.ad_actor_sys_prio))
0778                 goto nla_put_failure;
0779 
0780             if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY,
0781                     bond->params.ad_user_port_key))
0782                 goto nla_put_failure;
0783 
0784             if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM,
0785                     ETH_ALEN, &bond->params.ad_actor_system))
0786                 goto nla_put_failure;
0787         }
0788         if (!bond_3ad_get_active_agg_info(bond, &info)) {
0789             struct nlattr *nest;
0790 
0791             nest = nla_nest_start_noflag(skb, IFLA_BOND_AD_INFO);
0792             if (!nest)
0793                 goto nla_put_failure;
0794 
0795             if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
0796                     info.aggregator_id))
0797                 goto nla_put_failure;
0798             if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
0799                     info.ports))
0800                 goto nla_put_failure;
0801             if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
0802                     info.actor_key))
0803                 goto nla_put_failure;
0804             if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
0805                     info.partner_key))
0806                 goto nla_put_failure;
0807             if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
0808                     sizeof(info.partner_system),
0809                     &info.partner_system))
0810                 goto nla_put_failure;
0811 
0812             nla_nest_end(skb, nest);
0813         }
0814     }
0815 
0816     return 0;
0817 
0818 nla_put_failure:
0819     return -EMSGSIZE;
0820 }
0821 
0822 static size_t bond_get_linkxstats_size(const struct net_device *dev, int attr)
0823 {
0824     switch (attr) {
0825     case IFLA_STATS_LINK_XSTATS:
0826     case IFLA_STATS_LINK_XSTATS_SLAVE:
0827         break;
0828     default:
0829         return 0;
0830     }
0831 
0832     return bond_3ad_stats_size() + nla_total_size(0);
0833 }
0834 
0835 static int bond_fill_linkxstats(struct sk_buff *skb,
0836                 const struct net_device *dev,
0837                 int *prividx, int attr)
0838 {
0839     struct nlattr *nla __maybe_unused;
0840     struct slave *slave = NULL;
0841     struct nlattr *nest, *nest2;
0842     struct bonding *bond;
0843 
0844     switch (attr) {
0845     case IFLA_STATS_LINK_XSTATS:
0846         bond = netdev_priv(dev);
0847         break;
0848     case IFLA_STATS_LINK_XSTATS_SLAVE:
0849         slave = bond_slave_get_rtnl(dev);
0850         if (!slave)
0851             return 0;
0852         bond = slave->bond;
0853         break;
0854     default:
0855         return -EINVAL;
0856     }
0857 
0858     nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BOND);
0859     if (!nest)
0860         return -EMSGSIZE;
0861     if (BOND_MODE(bond) == BOND_MODE_8023AD) {
0862         struct bond_3ad_stats *stats;
0863 
0864         if (slave)
0865             stats = &SLAVE_AD_INFO(slave)->stats;
0866         else
0867             stats = &BOND_AD_INFO(bond).stats;
0868 
0869         nest2 = nla_nest_start_noflag(skb, BOND_XSTATS_3AD);
0870         if (!nest2) {
0871             nla_nest_end(skb, nest);
0872             return -EMSGSIZE;
0873         }
0874 
0875         if (bond_3ad_stats_fill(skb, stats)) {
0876             nla_nest_cancel(skb, nest2);
0877             nla_nest_end(skb, nest);
0878             return -EMSGSIZE;
0879         }
0880         nla_nest_end(skb, nest2);
0881     }
0882     nla_nest_end(skb, nest);
0883 
0884     return 0;
0885 }
0886 
0887 struct rtnl_link_ops bond_link_ops __read_mostly = {
0888     .kind           = "bond",
0889     .priv_size      = sizeof(struct bonding),
0890     .setup          = bond_setup,
0891     .maxtype        = IFLA_BOND_MAX,
0892     .policy         = bond_policy,
0893     .validate       = bond_validate,
0894     .newlink        = bond_newlink,
0895     .changelink     = bond_changelink,
0896     .get_size       = bond_get_size,
0897     .fill_info      = bond_fill_info,
0898     .get_num_tx_queues  = bond_get_num_tx_queues,
0899     .get_num_rx_queues  = bond_get_num_tx_queues, /* Use the same number
0900                                  as for TX queues */
0901     .fill_linkxstats        = bond_fill_linkxstats,
0902     .get_linkxstats_size    = bond_get_linkxstats_size,
0903     .slave_maxtype      = IFLA_BOND_SLAVE_MAX,
0904     .slave_policy       = bond_slave_policy,
0905     .slave_changelink   = bond_slave_changelink,
0906     .get_slave_size     = bond_get_slave_size,
0907     .fill_slave_info    = bond_fill_slave_info,
0908 };
0909 
0910 int __init bond_netlink_init(void)
0911 {
0912     return rtnl_link_register(&bond_link_ops);
0913 }
0914 
0915 void bond_netlink_fini(void)
0916 {
0917     rtnl_link_unregister(&bond_link_ops);
0918 }
0919 
0920 MODULE_ALIAS_RTNL_LINK("bond");