LCOV - code coverage report
Current view: top level - net/core - rtnetlink.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 761 3084 24.7 %
Date: 2021-04-22 12:43:58 Functions: 63 143 44.1 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * INET         An implementation of the TCP/IP protocol suite for the LINUX
       4             :  *              operating system.  INET is implemented using the  BSD Socket
       5             :  *              interface as the means of communication with the user level.
       6             :  *
       7             :  *              Routing netlink socket interface: protocol independent part.
       8             :  *
       9             :  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
      10             :  *
      11             :  *      Fixes:
      12             :  *      Vitaly E. Lavrov                RTA_OK arithmetics was wrong.
      13             :  */
      14             : 
      15             : #include <linux/bitops.h>
      16             : #include <linux/errno.h>
      17             : #include <linux/module.h>
      18             : #include <linux/types.h>
      19             : #include <linux/socket.h>
      20             : #include <linux/kernel.h>
      21             : #include <linux/timer.h>
      22             : #include <linux/string.h>
      23             : #include <linux/sockios.h>
      24             : #include <linux/net.h>
      25             : #include <linux/fcntl.h>
      26             : #include <linux/mm.h>
      27             : #include <linux/slab.h>
      28             : #include <linux/interrupt.h>
      29             : #include <linux/capability.h>
      30             : #include <linux/skbuff.h>
      31             : #include <linux/init.h>
      32             : #include <linux/security.h>
      33             : #include <linux/mutex.h>
      34             : #include <linux/if_addr.h>
      35             : #include <linux/if_bridge.h>
      36             : #include <linux/if_vlan.h>
      37             : #include <linux/pci.h>
      38             : #include <linux/etherdevice.h>
      39             : #include <linux/bpf.h>
      40             : 
      41             : #include <linux/uaccess.h>
      42             : 
      43             : #include <linux/inet.h>
      44             : #include <linux/netdevice.h>
      45             : #include <net/ip.h>
      46             : #include <net/protocol.h>
      47             : #include <net/arp.h>
      48             : #include <net/route.h>
      49             : #include <net/udp.h>
      50             : #include <net/tcp.h>
      51             : #include <net/sock.h>
      52             : #include <net/pkt_sched.h>
      53             : #include <net/fib_rules.h>
      54             : #include <net/rtnetlink.h>
      55             : #include <net/net_namespace.h>
      56             : 
      57             : #define RTNL_MAX_TYPE           50
      58             : #define RTNL_SLAVE_MAX_TYPE     40
      59             : 
      60             : struct rtnl_link {
      61             :         rtnl_doit_func          doit;
      62             :         rtnl_dumpit_func        dumpit;
      63             :         struct module           *owner;
      64             :         unsigned int            flags;
      65             :         struct rcu_head         rcu;
      66             : };
      67             : 
      68             : static DEFINE_MUTEX(rtnl_mutex);
      69             : 
      70          67 : void rtnl_lock(void)
      71             : {
      72          19 :         mutex_lock(&rtnl_mutex);
      73          19 : }
      74             : EXPORT_SYMBOL(rtnl_lock);
      75             : 
      76           2 : int rtnl_lock_killable(void)
      77             : {
      78           2 :         return mutex_lock_killable(&rtnl_mutex);
      79             : }
      80             : EXPORT_SYMBOL(rtnl_lock_killable);
      81             : 
      82             : static struct sk_buff *defer_kfree_skb_list;
      83           0 : void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
      84             : {
      85           0 :         if (head && tail) {
      86           0 :                 tail->next = defer_kfree_skb_list;
      87           0 :                 defer_kfree_skb_list = head;
      88             :         }
      89           0 : }
      90             : EXPORT_SYMBOL(rtnl_kfree_skbs);
      91             : 
      92          69 : void __rtnl_unlock(void)
      93             : {
      94          69 :         struct sk_buff *head = defer_kfree_skb_list;
      95             : 
      96          69 :         defer_kfree_skb_list = NULL;
      97             : 
      98          69 :         mutex_unlock(&rtnl_mutex);
      99             : 
     100          69 :         while (head) {
     101           0 :                 struct sk_buff *next = head->next;
     102             : 
     103           0 :                 kfree_skb(head);
     104           0 :                 cond_resched();
     105           0 :                 head = next;
     106             :         }
     107          69 : }
     108             : 
     109          69 : void rtnl_unlock(void)
     110             : {
     111             :         /* This fellow will unlock it for us. */
     112          21 :         netdev_run_todo();
     113          21 : }
     114             : EXPORT_SYMBOL(rtnl_unlock);
     115             : 
     116           0 : int rtnl_trylock(void)
     117             : {
     118           0 :         return mutex_trylock(&rtnl_mutex);
     119             : }
     120             : EXPORT_SYMBOL(rtnl_trylock);
     121             : 
     122          89 : int rtnl_is_locked(void)
     123             : {
     124          57 :         return mutex_is_locked(&rtnl_mutex);
     125             : }
     126             : EXPORT_SYMBOL(rtnl_is_locked);
     127             : 
     128           0 : bool refcount_dec_and_rtnl_lock(refcount_t *r)
     129             : {
     130           0 :         return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
     131             : }
     132             : EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
     133             : 
     134             : #ifdef CONFIG_PROVE_LOCKING
     135        4627 : bool lockdep_rtnl_is_held(void)
     136             : {
     137        4627 :         return lockdep_is_held(&rtnl_mutex);
     138             : }
     139             : EXPORT_SYMBOL(lockdep_rtnl_is_held);
     140             : #endif /* #ifdef CONFIG_PROVE_LOCKING */
     141             : 
     142             : static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
     143             : 
     144          38 : static inline int rtm_msgindex(int msgtype)
     145             : {
     146          38 :         int msgindex = msgtype - RTM_BASE;
     147             : 
     148             :         /*
     149             :          * msgindex < 0 implies someone tried to register a netlink
     150             :          * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
     151             :          * the message type has not been added to linux/rtnetlink.h
     152             :          */
     153          38 :         BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
     154             : 
     155          38 :         return msgindex;
     156             : }
     157             : 
     158          53 : static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
     159             : {
     160          53 :         struct rtnl_link __rcu **tab;
     161             : 
     162          53 :         if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
     163           0 :                 protocol = PF_UNSPEC;
     164             : 
     165         106 :         tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
     166          53 :         if (!tab)
     167           0 :                 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
     168             : 
     169         106 :         return rcu_dereference_rtnl(tab[msgtype]);
     170             : }
     171             : 
     172          38 : static int rtnl_register_internal(struct module *owner,
     173             :                                   int protocol, int msgtype,
     174             :                                   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
     175             :                                   unsigned int flags)
     176             : {
     177          38 :         struct rtnl_link *link, *old;
     178          38 :         struct rtnl_link __rcu **tab;
     179          38 :         int msgindex;
     180          38 :         int ret = -ENOBUFS;
     181             : 
     182          38 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     183          38 :         msgindex = rtm_msgindex(msgtype);
     184             : 
     185          38 :         rtnl_lock();
     186          76 :         tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
     187          38 :         if (tab == NULL) {
     188           4 :                 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
     189           4 :                 if (!tab)
     190           0 :                         goto unlock;
     191             : 
     192             :                 /* ensures we see the 0 stores */
     193           4 :                 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
     194             :         }
     195             : 
     196          76 :         old = rtnl_dereference(tab[msgindex]);
     197          38 :         if (old) {
     198           1 :                 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
     199           1 :                 if (!link)
     200           0 :                         goto unlock;
     201             :         } else {
     202          37 :                 link = kzalloc(sizeof(*link), GFP_KERNEL);
     203          37 :                 if (!link)
     204           0 :                         goto unlock;
     205             :         }
     206             : 
     207          76 :         WARN_ON(link->owner && link->owner != owner);
     208          38 :         link->owner = owner;
     209             : 
     210          76 :         WARN_ON(doit && link->doit && link->doit != doit);
     211          38 :         if (doit)
     212          29 :                 link->doit = doit;
     213          76 :         WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
     214          38 :         if (dumpit)
     215          16 :                 link->dumpit = dumpit;
     216             : 
     217          38 :         link->flags |= flags;
     218             : 
     219             :         /* publish protocol:msgtype */
     220          38 :         rcu_assign_pointer(tab[msgindex], link);
     221          38 :         ret = 0;
     222          38 :         if (old)
     223           1 :                 kfree_rcu(old, rcu);
     224          37 : unlock:
     225          38 :         rtnl_unlock();
     226          38 :         return ret;
     227             : }
     228             : 
     229             : /**
     230             :  * rtnl_register_module - Register a rtnetlink message type
     231             :  *
     232             :  * @owner: module registering the hook (THIS_MODULE)
     233             :  * @protocol: Protocol family or PF_UNSPEC
     234             :  * @msgtype: rtnetlink message type
     235             :  * @doit: Function pointer called for each request message
     236             :  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
     237             :  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
     238             :  *
     239             :  * Like rtnl_register, but for use by removable modules.
     240             :  */
     241           0 : int rtnl_register_module(struct module *owner,
     242             :                          int protocol, int msgtype,
     243             :                          rtnl_doit_func doit, rtnl_dumpit_func dumpit,
     244             :                          unsigned int flags)
     245             : {
     246           0 :         return rtnl_register_internal(owner, protocol, msgtype,
     247             :                                       doit, dumpit, flags);
     248             : }
     249             : EXPORT_SYMBOL_GPL(rtnl_register_module);
     250             : 
     251             : /**
     252             :  * rtnl_register - Register a rtnetlink message type
     253             :  * @protocol: Protocol family or PF_UNSPEC
     254             :  * @msgtype: rtnetlink message type
     255             :  * @doit: Function pointer called for each request message
     256             :  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
     257             :  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
     258             :  *
     259             :  * Registers the specified function pointers (at least one of them has
     260             :  * to be non-NULL) to be called whenever a request message for the
     261             :  * specified protocol family and message type is received.
     262             :  *
     263             :  * The special protocol family PF_UNSPEC may be used to define fallback
     264             :  * function pointers for the case when no entry for the specific protocol
     265             :  * family exists.
     266             :  */
     267          38 : void rtnl_register(int protocol, int msgtype,
     268             :                    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
     269             :                    unsigned int flags)
     270             : {
     271          38 :         int err;
     272             : 
     273          38 :         err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
     274             :                                      flags);
     275          38 :         if (err)
     276           0 :                 pr_err("Unable to register rtnetlink message handler, "
     277             :                        "protocol = %d, message type = %d\n", protocol, msgtype);
     278          38 : }
     279             : 
     280             : /**
     281             :  * rtnl_unregister - Unregister a rtnetlink message type
     282             :  * @protocol: Protocol family or PF_UNSPEC
     283             :  * @msgtype: rtnetlink message type
     284             :  *
     285             :  * Returns 0 on success or a negative error code.
     286             :  */
     287           0 : int rtnl_unregister(int protocol, int msgtype)
     288             : {
     289           0 :         struct rtnl_link __rcu **tab;
     290           0 :         struct rtnl_link *link;
     291           0 :         int msgindex;
     292             : 
     293           0 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     294           0 :         msgindex = rtm_msgindex(msgtype);
     295             : 
     296           0 :         rtnl_lock();
     297           0 :         tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
     298           0 :         if (!tab) {
     299           0 :                 rtnl_unlock();
     300           0 :                 return -ENOENT;
     301             :         }
     302             : 
     303           0 :         link = rtnl_dereference(tab[msgindex]);
     304           0 :         rcu_assign_pointer(tab[msgindex], NULL);
     305           0 :         rtnl_unlock();
     306             : 
     307           0 :         kfree_rcu(link, rcu);
     308             : 
     309             :         return 0;
     310             : }
     311             : EXPORT_SYMBOL_GPL(rtnl_unregister);
     312             : 
     313             : /**
     314             :  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
     315             :  * @protocol : Protocol family or PF_UNSPEC
     316             :  *
     317             :  * Identical to calling rtnl_unregster() for all registered message types
     318             :  * of a certain protocol family.
     319             :  */
     320           0 : void rtnl_unregister_all(int protocol)
     321             : {
     322           0 :         struct rtnl_link __rcu **tab;
     323           0 :         struct rtnl_link *link;
     324           0 :         int msgindex;
     325             : 
     326           0 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     327             : 
     328           0 :         rtnl_lock();
     329           0 :         tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
     330           0 :         if (!tab) {
     331           0 :                 rtnl_unlock();
     332           0 :                 return;
     333             :         }
     334           0 :         RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
     335           0 :         for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
     336           0 :                 link = rtnl_dereference(tab[msgindex]);
     337           0 :                 if (!link)
     338           0 :                         continue;
     339             : 
     340           0 :                 rcu_assign_pointer(tab[msgindex], NULL);
     341           0 :                 kfree_rcu(link, rcu);
     342             :         }
     343           0 :         rtnl_unlock();
     344             : 
     345           0 :         synchronize_net();
     346             : 
     347           0 :         kfree(tab);
     348             : }
     349             : EXPORT_SYMBOL_GPL(rtnl_unregister_all);
     350             : 
     351             : static LIST_HEAD(link_ops);
     352             : 
     353           0 : static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
     354             : {
     355           0 :         const struct rtnl_link_ops *ops;
     356             : 
     357           0 :         list_for_each_entry(ops, &link_ops, list) {
     358           0 :                 if (!strcmp(ops->kind, kind))
     359           0 :                         return ops;
     360             :         }
     361             :         return NULL;
     362             : }
     363             : 
     364             : /**
     365             :  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
     366             :  * @ops: struct rtnl_link_ops * to register
     367             :  *
     368             :  * The caller must hold the rtnl_mutex. This function should be used
     369             :  * by drivers that create devices during module initialization. It
     370             :  * must be called before registering the devices.
     371             :  *
     372             :  * Returns 0 on success or a negative error code.
     373             :  */
     374           0 : int __rtnl_link_register(struct rtnl_link_ops *ops)
     375             : {
     376           0 :         if (rtnl_link_ops_get(ops->kind))
     377             :                 return -EEXIST;
     378             : 
     379             :         /* The check for setup is here because if ops
     380             :          * does not have that filled up, it is not possible
     381             :          * to use the ops for creating device. So do not
     382             :          * fill up dellink as well. That disables rtnl_dellink.
     383             :          */
     384           0 :         if (ops->setup && !ops->dellink)
     385           0 :                 ops->dellink = unregister_netdevice_queue;
     386             : 
     387           0 :         list_add_tail(&ops->list, &link_ops);
     388           0 :         return 0;
     389             : }
     390             : EXPORT_SYMBOL_GPL(__rtnl_link_register);
     391             : 
     392             : /**
     393             :  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
     394             :  * @ops: struct rtnl_link_ops * to register
     395             :  *
     396             :  * Returns 0 on success or a negative error code.
     397             :  */
     398           0 : int rtnl_link_register(struct rtnl_link_ops *ops)
     399             : {
     400           0 :         int err;
     401             : 
     402             :         /* Sanity-check max sizes to avoid stack buffer overflow. */
     403           0 :         if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
     404             :                     ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
     405             :                 return -EINVAL;
     406             : 
     407           0 :         rtnl_lock();
     408           0 :         err = __rtnl_link_register(ops);
     409           0 :         rtnl_unlock();
     410           0 :         return err;
     411             : }
     412             : EXPORT_SYMBOL_GPL(rtnl_link_register);
     413             : 
     414           0 : static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
     415             : {
     416           0 :         struct net_device *dev;
     417           0 :         LIST_HEAD(list_kill);
     418             : 
     419           0 :         for_each_netdev(net, dev) {
     420           0 :                 if (dev->rtnl_link_ops == ops)
     421           0 :                         ops->dellink(dev, &list_kill);
     422             :         }
     423           0 :         unregister_netdevice_many(&list_kill);
     424           0 : }
     425             : 
     426             : /**
     427             :  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
     428             :  * @ops: struct rtnl_link_ops * to unregister
     429             :  *
     430             :  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
     431             :  * integrity (hold pernet_ops_rwsem for writing to close the race
     432             :  * with setup_net() and cleanup_net()).
     433             :  */
     434           0 : void __rtnl_link_unregister(struct rtnl_link_ops *ops)
     435             : {
     436           0 :         struct net *net;
     437             : 
     438           0 :         for_each_net(net) {
     439           0 :                 __rtnl_kill_links(net, ops);
     440             :         }
     441           0 :         list_del(&ops->list);
     442           0 : }
     443             : EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
     444             : 
     445             : /* Return with the rtnl_lock held when there are no network
     446             :  * devices unregistering in any network namespace.
     447             :  */
     448           0 : static void rtnl_lock_unregistering_all(void)
     449             : {
     450           0 :         struct net *net;
     451           0 :         bool unregistering;
     452           0 :         DEFINE_WAIT_FUNC(wait, woken_wake_function);
     453             : 
     454           0 :         add_wait_queue(&netdev_unregistering_wq, &wait);
     455           0 :         for (;;) {
     456           0 :                 unregistering = false;
     457           0 :                 rtnl_lock();
     458             :                 /* We held write locked pernet_ops_rwsem, and parallel
     459             :                  * setup_net() and cleanup_net() are not possible.
     460             :                  */
     461           0 :                 for_each_net(net) {
     462           0 :                         if (net->dev_unreg_count > 0) {
     463             :                                 unregistering = true;
     464             :                                 break;
     465             :                         }
     466             :                 }
     467           0 :                 if (!unregistering)
     468             :                         break;
     469           0 :                 __rtnl_unlock();
     470             : 
     471           0 :                 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
     472             :         }
     473           0 :         remove_wait_queue(&netdev_unregistering_wq, &wait);
     474           0 : }
     475             : 
     476             : /**
     477             :  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
     478             :  * @ops: struct rtnl_link_ops * to unregister
     479             :  */
     480           0 : void rtnl_link_unregister(struct rtnl_link_ops *ops)
     481             : {
     482             :         /* Close the race with setup_net() and cleanup_net() */
     483           0 :         down_write(&pernet_ops_rwsem);
     484           0 :         rtnl_lock_unregistering_all();
     485           0 :         __rtnl_link_unregister(ops);
     486           0 :         rtnl_unlock();
     487           0 :         up_write(&pernet_ops_rwsem);
     488           0 : }
     489             : EXPORT_SYMBOL_GPL(rtnl_link_unregister);
     490             : 
     491           0 : static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
     492             : {
     493           0 :         struct net_device *master_dev;
     494           0 :         const struct rtnl_link_ops *ops;
     495           0 :         size_t size = 0;
     496             : 
     497           0 :         rcu_read_lock();
     498             : 
     499           0 :         master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
     500           0 :         if (!master_dev)
     501           0 :                 goto out;
     502             : 
     503           0 :         ops = master_dev->rtnl_link_ops;
     504           0 :         if (!ops || !ops->get_slave_size)
     505           0 :                 goto out;
     506             :         /* IFLA_INFO_SLAVE_DATA + nested data */
     507           0 :         size = nla_total_size(sizeof(struct nlattr)) +
     508           0 :                ops->get_slave_size(master_dev, dev);
     509             : 
     510           0 : out:
     511           0 :         rcu_read_unlock();
     512           0 :         return size;
     513             : }
     514             : 
     515           4 : static size_t rtnl_link_get_size(const struct net_device *dev)
     516             : {
     517           4 :         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
     518           4 :         size_t size;
     519             : 
     520           4 :         if (!ops)
     521             :                 return 0;
     522             : 
     523           0 :         size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
     524           0 :                nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
     525             : 
     526           0 :         if (ops->get_size)
     527             :                 /* IFLA_INFO_DATA + nested data */
     528           0 :                 size += nla_total_size(sizeof(struct nlattr)) +
     529           0 :                         ops->get_size(dev);
     530             : 
     531           0 :         if (ops->get_xstats_size)
     532             :                 /* IFLA_INFO_XSTATS */
     533           0 :                 size += nla_total_size(ops->get_xstats_size(dev));
     534             : 
     535           0 :         size += rtnl_link_get_slave_info_data_size(dev);
     536             : 
     537           0 :         return size;
     538             : }
     539             : 
     540             : static LIST_HEAD(rtnl_af_ops);
     541             : 
     542           0 : static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
     543             : {
     544           0 :         const struct rtnl_af_ops *ops;
     545             : 
     546           0 :         list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
     547           0 :                 if (ops->family == family)
     548             :                         return ops;
     549             :         }
     550             : 
     551             :         return NULL;
     552             : }
     553             : 
     554             : /**
     555             :  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
     556             :  * @ops: struct rtnl_af_ops * to register
     557             :  *
     558             :  * Returns 0 on success or a negative error code.
     559             :  */
     560           1 : void rtnl_af_register(struct rtnl_af_ops *ops)
     561             : {
     562           1 :         rtnl_lock();
     563           1 :         list_add_tail_rcu(&ops->list, &rtnl_af_ops);
     564           1 :         rtnl_unlock();
     565           1 : }
     566             : EXPORT_SYMBOL_GPL(rtnl_af_register);
     567             : 
     568             : /**
     569             :  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
     570             :  * @ops: struct rtnl_af_ops * to unregister
     571             :  */
     572           0 : void rtnl_af_unregister(struct rtnl_af_ops *ops)
     573             : {
     574           0 :         rtnl_lock();
     575           0 :         list_del_rcu(&ops->list);
     576           0 :         rtnl_unlock();
     577             : 
     578           0 :         synchronize_rcu();
     579           0 : }
     580             : EXPORT_SYMBOL_GPL(rtnl_af_unregister);
     581             : 
     582           4 : static size_t rtnl_link_get_af_size(const struct net_device *dev,
     583             :                                     u32 ext_filter_mask)
     584             : {
     585           4 :         struct rtnl_af_ops *af_ops;
     586           4 :         size_t size;
     587             : 
     588             :         /* IFLA_AF_SPEC */
     589           4 :         size = nla_total_size(sizeof(struct nlattr));
     590             : 
     591           4 :         rcu_read_lock();
     592           7 :         list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
     593           3 :                 if (af_ops->get_link_af_size) {
     594             :                         /* AF_* + nested data */
     595           3 :                         size += nla_total_size(sizeof(struct nlattr)) +
     596           3 :                                 af_ops->get_link_af_size(dev, ext_filter_mask);
     597             :                 }
     598             :         }
     599           4 :         rcu_read_unlock();
     600             : 
     601           4 :         return size;
     602             : }
     603             : 
     604          16 : static bool rtnl_have_link_slave_info(const struct net_device *dev)
     605             : {
     606          16 :         struct net_device *master_dev;
     607          16 :         bool ret = false;
     608             : 
     609          16 :         rcu_read_lock();
     610             : 
     611          16 :         master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
     612          16 :         if (master_dev && master_dev->rtnl_link_ops)
     613           0 :                 ret = true;
     614          16 :         rcu_read_unlock();
     615          16 :         return ret;
     616             : }
     617             : 
     618           0 : static int rtnl_link_slave_info_fill(struct sk_buff *skb,
     619             :                                      const struct net_device *dev)
     620             : {
     621           0 :         struct net_device *master_dev;
     622           0 :         const struct rtnl_link_ops *ops;
     623           0 :         struct nlattr *slave_data;
     624           0 :         int err;
     625             : 
     626           0 :         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
     627           0 :         if (!master_dev)
     628             :                 return 0;
     629           0 :         ops = master_dev->rtnl_link_ops;
     630           0 :         if (!ops)
     631             :                 return 0;
     632           0 :         if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
     633             :                 return -EMSGSIZE;
     634           0 :         if (ops->fill_slave_info) {
     635           0 :                 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
     636           0 :                 if (!slave_data)
     637             :                         return -EMSGSIZE;
     638           0 :                 err = ops->fill_slave_info(skb, master_dev, dev);
     639           0 :                 if (err < 0)
     640           0 :                         goto err_cancel_slave_data;
     641           0 :                 nla_nest_end(skb, slave_data);
     642             :         }
     643             :         return 0;
     644             : 
     645           0 : err_cancel_slave_data:
     646           0 :         nla_nest_cancel(skb, slave_data);
     647           0 :         return err;
     648             : }
     649             : 
     650           0 : static int rtnl_link_info_fill(struct sk_buff *skb,
     651             :                                const struct net_device *dev)
     652             : {
     653           0 :         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
     654           0 :         struct nlattr *data;
     655           0 :         int err;
     656             : 
     657           0 :         if (!ops)
     658             :                 return 0;
     659           0 :         if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
     660             :                 return -EMSGSIZE;
     661           0 :         if (ops->fill_xstats) {
     662           0 :                 err = ops->fill_xstats(skb, dev);
     663           0 :                 if (err < 0)
     664             :                         return err;
     665             :         }
     666           0 :         if (ops->fill_info) {
     667           0 :                 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
     668           0 :                 if (data == NULL)
     669             :                         return -EMSGSIZE;
     670           0 :                 err = ops->fill_info(skb, dev);
     671           0 :                 if (err < 0)
     672           0 :                         goto err_cancel_data;
     673           0 :                 nla_nest_end(skb, data);
     674             :         }
     675             :         return 0;
     676             : 
     677           0 : err_cancel_data:
     678           0 :         nla_nest_cancel(skb, data);
     679           0 :         return err;
     680             : }
     681             : 
     682           0 : static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
     683             : {
     684           0 :         struct nlattr *linkinfo;
     685           0 :         int err = -EMSGSIZE;
     686             : 
     687           0 :         linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
     688           0 :         if (linkinfo == NULL)
     689           0 :                 goto out;
     690             : 
     691           0 :         err = rtnl_link_info_fill(skb, dev);
     692           0 :         if (err < 0)
     693           0 :                 goto err_cancel_link;
     694             : 
     695           0 :         err = rtnl_link_slave_info_fill(skb, dev);
     696           0 :         if (err < 0)
     697           0 :                 goto err_cancel_link;
     698             : 
     699           0 :         nla_nest_end(skb, linkinfo);
     700           0 :         return 0;
     701             : 
     702           0 : err_cancel_link:
     703           0 :         nla_nest_cancel(skb, linkinfo);
     704             : out:
     705             :         return err;
     706             : }
     707             : 
     708           0 : int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
     709             : {
     710           0 :         struct sock *rtnl = net->rtnl;
     711           0 :         int err = 0;
     712             : 
     713           0 :         NETLINK_CB(skb).dst_group = group;
     714           0 :         if (echo)
     715           0 :                 refcount_inc(&skb->users);
     716           0 :         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
     717           0 :         if (echo)
     718           0 :                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
     719           0 :         return err;
     720             : }
     721             : 
     722           0 : int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
     723             : {
     724           0 :         struct sock *rtnl = net->rtnl;
     725             : 
     726           0 :         return nlmsg_unicast(rtnl, skb, pid);
     727             : }
     728             : EXPORT_SYMBOL(rtnl_unicast);
     729             : 
     730          27 : void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
     731             :                  struct nlmsghdr *nlh, gfp_t flags)
     732             : {
     733          27 :         struct sock *rtnl = net->rtnl;
     734          27 :         int report = 0;
     735             : 
     736          23 :         if (nlh)
     737           4 :                 report = nlmsg_report(nlh);
     738             : 
     739          23 :         nlmsg_notify(rtnl, skb, pid, group, report, flags);
     740           0 : }
     741             : EXPORT_SYMBOL(rtnl_notify);
     742             : 
     743           0 : void rtnl_set_sk_err(struct net *net, u32 group, int error)
     744             : {
     745           0 :         struct sock *rtnl = net->rtnl;
     746             : 
     747           0 :         netlink_set_err(rtnl, 0, group, error);
     748           0 : }
     749             : EXPORT_SYMBOL(rtnl_set_sk_err);
     750             : 
     751          19 : int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
     752             : {
     753          19 :         struct nlattr *mx;
     754          19 :         int i, valid = 0;
     755             : 
     756             :         /* nothing is dumped for dst_default_metrics, so just skip the loop */
     757          19 :         if (metrics == dst_default_metrics.metrics)
     758             :                 return 0;
     759             : 
     760           0 :         mx = nla_nest_start_noflag(skb, RTA_METRICS);
     761           0 :         if (mx == NULL)
     762             :                 return -ENOBUFS;
     763             : 
     764           0 :         for (i = 0; i < RTAX_MAX; i++) {
     765           0 :                 if (metrics[i]) {
     766           0 :                         if (i == RTAX_CC_ALGO - 1) {
     767           0 :                                 char tmp[TCP_CA_NAME_MAX], *name;
     768             : 
     769           0 :                                 name = tcp_ca_get_name_by_key(metrics[i], tmp);
     770           0 :                                 if (!name)
     771           0 :                                         continue;
     772           0 :                                 if (nla_put_string(skb, i + 1, name))
     773           0 :                                         goto nla_put_failure;
     774           0 :                         } else if (i == RTAX_FEATURES - 1) {
     775           0 :                                 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
     776             : 
     777           0 :                                 if (!user_features)
     778           0 :                                         continue;
     779           0 :                                 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
     780           0 :                                 if (nla_put_u32(skb, i + 1, user_features))
     781           0 :                                         goto nla_put_failure;
     782             :                         } else {
     783           0 :                                 if (nla_put_u32(skb, i + 1, metrics[i]))
     784           0 :                                         goto nla_put_failure;
     785             :                         }
     786           0 :                         valid++;
     787             :                 }
     788             :         }
     789             : 
     790           0 :         if (!valid) {
     791           0 :                 nla_nest_cancel(skb, mx);
     792           0 :                 return 0;
     793             :         }
     794             : 
     795           0 :         return nla_nest_end(skb, mx);
     796             : 
     797           0 : nla_put_failure:
     798           0 :         nla_nest_cancel(skb, mx);
     799           0 :         return -EMSGSIZE;
     800             : }
     801             : EXPORT_SYMBOL(rtnetlink_put_metrics);
     802             : 
     803           0 : int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
     804             :                        long expires, u32 error)
     805             : {
     806           0 :         struct rta_cacheinfo ci = {
     807             :                 .rta_error = error,
     808             :                 .rta_id =  id,
     809             :         };
     810             : 
     811           0 :         if (dst) {
     812           0 :                 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
     813           0 :                 ci.rta_used = dst->__use;
     814           0 :                 ci.rta_clntref = atomic_read(&dst->__refcnt);
     815             :         }
     816           0 :         if (expires) {
     817           0 :                 unsigned long clock;
     818             : 
     819           0 :                 clock = jiffies_to_clock_t(abs(expires));
     820           0 :                 clock = min_t(unsigned long, clock, INT_MAX);
     821           0 :                 ci.rta_expires = (expires > 0) ? clock : -clock;
     822             :         }
     823           0 :         return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
     824             : }
     825             : EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
     826             : 
     827           0 : static void set_operstate(struct net_device *dev, unsigned char transition)
     828             : {
     829           0 :         unsigned char operstate = dev->operstate;
     830             : 
     831           0 :         switch (transition) {
     832           0 :         case IF_OPER_UP:
     833           0 :                 if ((operstate == IF_OPER_DORMANT ||
     834           0 :                      operstate == IF_OPER_TESTING ||
     835           0 :                      operstate == IF_OPER_UNKNOWN) &&
     836           0 :                     !netif_dormant(dev) && !netif_testing(dev))
     837           0 :                         operstate = IF_OPER_UP;
     838             :                 break;
     839             : 
     840           0 :         case IF_OPER_TESTING:
     841           0 :                 if (operstate == IF_OPER_UP ||
     842           0 :                     operstate == IF_OPER_UNKNOWN)
     843           0 :                         operstate = IF_OPER_TESTING;
     844             :                 break;
     845             : 
     846           0 :         case IF_OPER_DORMANT:
     847           0 :                 if (operstate == IF_OPER_UP ||
     848           0 :                     operstate == IF_OPER_UNKNOWN)
     849           0 :                         operstate = IF_OPER_DORMANT;
     850             :                 break;
     851             :         }
     852             : 
     853           0 :         if (dev->operstate != operstate) {
     854           0 :                 write_lock_bh(&dev_base_lock);
     855           0 :                 dev->operstate = operstate;
     856           0 :                 write_unlock_bh(&dev_base_lock);
     857           0 :                 netdev_state_change(dev);
     858             :         }
     859           0 : }
     860             : 
     861           3 : static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
     862             : {
     863           3 :         return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
     864           3 :                (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
     865             : }
     866             : 
     867           3 : static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
     868             :                                            const struct ifinfomsg *ifm)
     869             : {
     870           3 :         unsigned int flags = ifm->ifi_flags;
     871             : 
     872             :         /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
     873           3 :         if (ifm->ifi_change)
     874           3 :                 flags = (flags & ifm->ifi_change) |
     875           3 :                         (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
     876             : 
     877           3 :         return flags;
     878             : }
     879             : 
     880          16 : static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
     881             :                                  const struct rtnl_link_stats64 *b)
     882             : {
     883          16 :         a->rx_packets = b->rx_packets;
     884          16 :         a->tx_packets = b->tx_packets;
     885          16 :         a->rx_bytes = b->rx_bytes;
     886          16 :         a->tx_bytes = b->tx_bytes;
     887          16 :         a->rx_errors = b->rx_errors;
     888          16 :         a->tx_errors = b->tx_errors;
     889          16 :         a->rx_dropped = b->rx_dropped;
     890          16 :         a->tx_dropped = b->tx_dropped;
     891             : 
     892          16 :         a->multicast = b->multicast;
     893          16 :         a->collisions = b->collisions;
     894             : 
     895          16 :         a->rx_length_errors = b->rx_length_errors;
     896          16 :         a->rx_over_errors = b->rx_over_errors;
     897          16 :         a->rx_crc_errors = b->rx_crc_errors;
     898          16 :         a->rx_frame_errors = b->rx_frame_errors;
     899          16 :         a->rx_fifo_errors = b->rx_fifo_errors;
     900          16 :         a->rx_missed_errors = b->rx_missed_errors;
     901             : 
     902          16 :         a->tx_aborted_errors = b->tx_aborted_errors;
     903          16 :         a->tx_carrier_errors = b->tx_carrier_errors;
     904          16 :         a->tx_fifo_errors = b->tx_fifo_errors;
     905          16 :         a->tx_heartbeat_errors = b->tx_heartbeat_errors;
     906          16 :         a->tx_window_errors = b->tx_window_errors;
     907             : 
     908          16 :         a->rx_compressed = b->rx_compressed;
     909          16 :         a->tx_compressed = b->tx_compressed;
     910             : 
     911          16 :         a->rx_nohandler = b->rx_nohandler;
     912          16 : }
     913             : 
     914             : /* All VF info */
     915           4 : static inline int rtnl_vfinfo_size(const struct net_device *dev,
     916             :                                    u32 ext_filter_mask)
     917             : {
     918           4 :         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
     919           0 :                 int num_vfs = dev_num_vf(dev->dev.parent);
     920           0 :                 size_t size = nla_total_size(0);
     921           0 :                 size += num_vfs *
     922           0 :                         (nla_total_size(0) +
     923           0 :                          nla_total_size(sizeof(struct ifla_vf_mac)) +
     924           0 :                          nla_total_size(sizeof(struct ifla_vf_broadcast)) +
     925           0 :                          nla_total_size(sizeof(struct ifla_vf_vlan)) +
     926           0 :                          nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
     927           0 :                          nla_total_size(MAX_VLAN_LIST_LEN *
     928           0 :                                         sizeof(struct ifla_vf_vlan_info)) +
     929           0 :                          nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
     930           0 :                          nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
     931           0 :                          nla_total_size(sizeof(struct ifla_vf_rate)) +
     932           0 :                          nla_total_size(sizeof(struct ifla_vf_link_state)) +
     933           0 :                          nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
     934           0 :                          nla_total_size(0) + /* nest IFLA_VF_STATS */
     935             :                          /* IFLA_VF_STATS_RX_PACKETS */
     936           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     937             :                          /* IFLA_VF_STATS_TX_PACKETS */
     938           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     939             :                          /* IFLA_VF_STATS_RX_BYTES */
     940           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     941             :                          /* IFLA_VF_STATS_TX_BYTES */
     942           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     943             :                          /* IFLA_VF_STATS_BROADCAST */
     944           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     945             :                          /* IFLA_VF_STATS_MULTICAST */
     946           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     947             :                          /* IFLA_VF_STATS_RX_DROPPED */
     948           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     949             :                          /* IFLA_VF_STATS_TX_DROPPED */
     950           0 :                          nla_total_size_64bit(sizeof(__u64)) +
     951           0 :                          nla_total_size(sizeof(struct ifla_vf_trust)));
     952           0 :                 return size;
     953             :         } else
     954             :                 return 0;
     955             : }
     956             : 
     957           4 : static size_t rtnl_port_size(const struct net_device *dev,
     958             :                              u32 ext_filter_mask)
     959             : {
     960           4 :         size_t port_size = nla_total_size(4)            /* PORT_VF */
     961           4 :                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
     962           4 :                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
     963           4 :                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
     964           4 :                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
     965           4 :                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
     966           4 :         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
     967           4 :         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
     968             :                 + port_size;
     969           4 :         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
     970             :                 + port_size;
     971             : 
     972           4 :         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
     973           0 :             !(ext_filter_mask & RTEXT_FILTER_VF))
     974             :                 return 0;
     975           0 :         if (dev_num_vf(dev->dev.parent))
     976           0 :                 return port_self_size + vf_ports_size +
     977           0 :                         vf_port_size * dev_num_vf(dev->dev.parent);
     978             :         else
     979             :                 return port_self_size;
     980             : }
     981             : 
     982           4 : static size_t rtnl_xdp_size(void)
     983             : {
     984           4 :         size_t xdp_size = nla_total_size(0) +   /* nest IFLA_XDP */
     985           4 :                           nla_total_size(1) +   /* XDP_ATTACHED */
     986           4 :                           nla_total_size(4) +   /* XDP_PROG_ID (or 1st mode) */
     987           4 :                           nla_total_size(4);    /* XDP_<mode>_PROG_ID */
     988             : 
     989           4 :         return xdp_size;
     990             : }
     991             : 
     992           4 : static size_t rtnl_prop_list_size(const struct net_device *dev)
     993             : {
     994           4 :         struct netdev_name_node *name_node;
     995           4 :         size_t size;
     996             : 
     997           4 :         if (list_empty(&dev->name_node->list))
     998             :                 return 0;
     999           0 :         size = nla_total_size(0);
    1000           0 :         list_for_each_entry(name_node, &dev->name_node->list, list)
    1001           0 :                 size += nla_total_size(ALTIFNAMSIZ);
    1002             :         return size;
    1003             : }
    1004             : 
    1005           4 : static size_t rtnl_proto_down_size(const struct net_device *dev)
    1006             : {
    1007           4 :         size_t size = nla_total_size(1);
    1008             : 
    1009           4 :         if (dev->proto_down_reason)
    1010           0 :                 size += nla_total_size(0) + nla_total_size(4);
    1011             : 
    1012           4 :         return size;
    1013             : }
    1014             : 
    1015           4 : static noinline size_t if_nlmsg_size(const struct net_device *dev,
    1016             :                                      u32 ext_filter_mask)
    1017             : {
    1018           4 :         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
    1019           4 :                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
    1020           4 :                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
    1021           4 :                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
    1022           4 :                + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
    1023           4 :                + nla_total_size(sizeof(struct rtnl_link_stats))
    1024           4 :                + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
    1025           4 :                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
    1026           4 :                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
    1027           4 :                + nla_total_size(4) /* IFLA_TXQLEN */
    1028           4 :                + nla_total_size(4) /* IFLA_WEIGHT */
    1029           4 :                + nla_total_size(4) /* IFLA_MTU */
    1030           4 :                + nla_total_size(4) /* IFLA_LINK */
    1031           4 :                + nla_total_size(4) /* IFLA_MASTER */
    1032           4 :                + nla_total_size(1) /* IFLA_CARRIER */
    1033           4 :                + nla_total_size(4) /* IFLA_PROMISCUITY */
    1034           4 :                + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
    1035           4 :                + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
    1036           4 :                + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
    1037           4 :                + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
    1038           4 :                + nla_total_size(1) /* IFLA_OPERSTATE */
    1039           4 :                + nla_total_size(1) /* IFLA_LINKMODE */
    1040           4 :                + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
    1041           4 :                + nla_total_size(4) /* IFLA_LINK_NETNSID */
    1042           4 :                + nla_total_size(4) /* IFLA_GROUP */
    1043           4 :                + nla_total_size(ext_filter_mask
    1044           4 :                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
    1045           4 :                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
    1046           4 :                + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
    1047           4 :                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
    1048           4 :                + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
    1049           4 :                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
    1050           4 :                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
    1051           4 :                + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
    1052           4 :                + rtnl_xdp_size() /* IFLA_XDP */
    1053           4 :                + nla_total_size(4)  /* IFLA_EVENT */
    1054           4 :                + nla_total_size(4)  /* IFLA_NEW_NETNSID */
    1055           4 :                + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
    1056           4 :                + rtnl_proto_down_size(dev)  /* proto down */
    1057           4 :                + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
    1058           4 :                + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
    1059           4 :                + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
    1060           4 :                + nla_total_size(4)  /* IFLA_MIN_MTU */
    1061           4 :                + nla_total_size(4)  /* IFLA_MAX_MTU */
    1062           4 :                + rtnl_prop_list_size(dev)
    1063           4 :                + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
    1064           4 :                + 0;
    1065             : }
    1066             : 
    1067           0 : static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
    1068             : {
    1069           0 :         struct nlattr *vf_ports;
    1070           0 :         struct nlattr *vf_port;
    1071           0 :         int vf;
    1072           0 :         int err;
    1073             : 
    1074           0 :         vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
    1075           0 :         if (!vf_ports)
    1076             :                 return -EMSGSIZE;
    1077             : 
    1078           0 :         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
    1079           0 :                 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
    1080           0 :                 if (!vf_port)
    1081           0 :                         goto nla_put_failure;
    1082           0 :                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
    1083           0 :                         goto nla_put_failure;
    1084           0 :                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
    1085           0 :                 if (err == -EMSGSIZE)
    1086           0 :                         goto nla_put_failure;
    1087           0 :                 if (err) {
    1088           0 :                         nla_nest_cancel(skb, vf_port);
    1089           0 :                         continue;
    1090             :                 }
    1091           0 :                 nla_nest_end(skb, vf_port);
    1092             :         }
    1093             : 
    1094           0 :         nla_nest_end(skb, vf_ports);
    1095             : 
    1096           0 :         return 0;
    1097             : 
    1098           0 : nla_put_failure:
    1099           0 :         nla_nest_cancel(skb, vf_ports);
    1100           0 :         return -EMSGSIZE;
    1101             : }
    1102             : 
    1103           0 : static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
    1104             : {
    1105           0 :         struct nlattr *port_self;
    1106           0 :         int err;
    1107             : 
    1108           0 :         port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
    1109           0 :         if (!port_self)
    1110             :                 return -EMSGSIZE;
    1111             : 
    1112           0 :         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
    1113           0 :         if (err) {
    1114           0 :                 nla_nest_cancel(skb, port_self);
    1115           0 :                 return (err == -EMSGSIZE) ? err : 0;
    1116             :         }
    1117             : 
    1118           0 :         nla_nest_end(skb, port_self);
    1119             : 
    1120           0 :         return 0;
    1121             : }
    1122             : 
    1123          16 : static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
    1124             :                           u32 ext_filter_mask)
    1125             : {
    1126          16 :         int err;
    1127             : 
    1128          16 :         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
    1129           0 :             !(ext_filter_mask & RTEXT_FILTER_VF))
    1130             :                 return 0;
    1131             : 
    1132           0 :         err = rtnl_port_self_fill(skb, dev);
    1133           0 :         if (err)
    1134             :                 return err;
    1135             : 
    1136           0 :         if (dev_num_vf(dev->dev.parent)) {
    1137           0 :                 err = rtnl_vf_ports_fill(skb, dev);
    1138           0 :                 if (err)
    1139           0 :                         return err;
    1140             :         }
    1141             : 
    1142             :         return 0;
    1143             : }
    1144             : 
    1145          16 : static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
    1146             : {
    1147          16 :         int err;
    1148          16 :         struct netdev_phys_item_id ppid;
    1149             : 
    1150          16 :         err = dev_get_phys_port_id(dev, &ppid);
    1151          16 :         if (err) {
    1152          16 :                 if (err == -EOPNOTSUPP)
    1153             :                         return 0;
    1154           0 :                 return err;
    1155             :         }
    1156             : 
    1157           0 :         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
    1158           0 :                 return -EMSGSIZE;
    1159             : 
    1160             :         return 0;
    1161             : }
    1162             : 
    1163          16 : static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
    1164             : {
    1165          16 :         char name[IFNAMSIZ];
    1166          16 :         int err;
    1167             : 
    1168          16 :         err = dev_get_phys_port_name(dev, name, sizeof(name));
    1169          16 :         if (err) {
    1170          16 :                 if (err == -EOPNOTSUPP)
    1171             :                         return 0;
    1172           0 :                 return err;
    1173             :         }
    1174             : 
    1175           0 :         if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
    1176           0 :                 return -EMSGSIZE;
    1177             : 
    1178             :         return 0;
    1179             : }
    1180             : 
    1181          16 : static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
    1182             : {
    1183          16 :         struct netdev_phys_item_id ppid = { };
    1184          16 :         int err;
    1185             : 
    1186          16 :         err = dev_get_port_parent_id(dev, &ppid, false);
    1187          16 :         if (err) {
    1188          16 :                 if (err == -EOPNOTSUPP)
    1189             :                         return 0;
    1190           0 :                 return err;
    1191             :         }
    1192             : 
    1193           0 :         if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
    1194           0 :                 return -EMSGSIZE;
    1195             : 
    1196             :         return 0;
    1197             : }
    1198             : 
    1199          16 : static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
    1200             :                                               struct net_device *dev)
    1201             : {
    1202          16 :         struct rtnl_link_stats64 *sp;
    1203          16 :         struct nlattr *attr;
    1204             : 
    1205          16 :         attr = nla_reserve_64bit(skb, IFLA_STATS64,
    1206             :                                  sizeof(struct rtnl_link_stats64), IFLA_PAD);
    1207          16 :         if (!attr)
    1208             :                 return -EMSGSIZE;
    1209             : 
    1210          16 :         sp = nla_data(attr);
    1211          16 :         dev_get_stats(dev, sp);
    1212             : 
    1213          16 :         attr = nla_reserve(skb, IFLA_STATS,
    1214             :                            sizeof(struct rtnl_link_stats));
    1215          16 :         if (!attr)
    1216             :                 return -EMSGSIZE;
    1217             : 
    1218          16 :         copy_rtnl_link_stats(nla_data(attr), sp);
    1219             : 
    1220          16 :         return 0;
    1221             : }
    1222             : 
    1223           0 : static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
    1224             :                                                struct net_device *dev,
    1225             :                                                int vfs_num,
    1226             :                                                struct nlattr *vfinfo)
    1227             : {
    1228           0 :         struct ifla_vf_rss_query_en vf_rss_query_en;
    1229           0 :         struct nlattr *vf, *vfstats, *vfvlanlist;
    1230           0 :         struct ifla_vf_link_state vf_linkstate;
    1231           0 :         struct ifla_vf_vlan_info vf_vlan_info;
    1232           0 :         struct ifla_vf_spoofchk vf_spoofchk;
    1233           0 :         struct ifla_vf_tx_rate vf_tx_rate;
    1234           0 :         struct ifla_vf_stats vf_stats;
    1235           0 :         struct ifla_vf_trust vf_trust;
    1236           0 :         struct ifla_vf_vlan vf_vlan;
    1237           0 :         struct ifla_vf_rate vf_rate;
    1238           0 :         struct ifla_vf_mac vf_mac;
    1239           0 :         struct ifla_vf_broadcast vf_broadcast;
    1240           0 :         struct ifla_vf_info ivi;
    1241           0 :         struct ifla_vf_guid node_guid;
    1242           0 :         struct ifla_vf_guid port_guid;
    1243             : 
    1244           0 :         memset(&ivi, 0, sizeof(ivi));
    1245             : 
    1246             :         /* Not all SR-IOV capable drivers support the
    1247             :          * spoofcheck and "RSS query enable" query.  Preset to
    1248             :          * -1 so the user space tool can detect that the driver
    1249             :          * didn't report anything.
    1250             :          */
    1251           0 :         ivi.spoofchk = -1;
    1252           0 :         ivi.rss_query_en = -1;
    1253           0 :         ivi.trusted = -1;
    1254             :         /* The default value for VF link state is "auto"
    1255             :          * IFLA_VF_LINK_STATE_AUTO which equals zero
    1256             :          */
    1257           0 :         ivi.linkstate = 0;
    1258             :         /* VLAN Protocol by default is 802.1Q */
    1259           0 :         ivi.vlan_proto = htons(ETH_P_8021Q);
    1260           0 :         if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
    1261             :                 return 0;
    1262             : 
    1263           0 :         memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
    1264           0 :         memset(&node_guid, 0, sizeof(node_guid));
    1265           0 :         memset(&port_guid, 0, sizeof(port_guid));
    1266             : 
    1267           0 :         vf_mac.vf =
    1268           0 :                 vf_vlan.vf =
    1269           0 :                 vf_vlan_info.vf =
    1270           0 :                 vf_rate.vf =
    1271           0 :                 vf_tx_rate.vf =
    1272           0 :                 vf_spoofchk.vf =
    1273           0 :                 vf_linkstate.vf =
    1274           0 :                 vf_rss_query_en.vf =
    1275           0 :                 vf_trust.vf =
    1276           0 :                 node_guid.vf =
    1277           0 :                 port_guid.vf = ivi.vf;
    1278             : 
    1279           0 :         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
    1280           0 :         memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
    1281           0 :         vf_vlan.vlan = ivi.vlan;
    1282           0 :         vf_vlan.qos = ivi.qos;
    1283           0 :         vf_vlan_info.vlan = ivi.vlan;
    1284           0 :         vf_vlan_info.qos = ivi.qos;
    1285           0 :         vf_vlan_info.vlan_proto = ivi.vlan_proto;
    1286           0 :         vf_tx_rate.rate = ivi.max_tx_rate;
    1287           0 :         vf_rate.min_tx_rate = ivi.min_tx_rate;
    1288           0 :         vf_rate.max_tx_rate = ivi.max_tx_rate;
    1289           0 :         vf_spoofchk.setting = ivi.spoofchk;
    1290           0 :         vf_linkstate.link_state = ivi.linkstate;
    1291           0 :         vf_rss_query_en.setting = ivi.rss_query_en;
    1292           0 :         vf_trust.setting = ivi.trusted;
    1293           0 :         vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
    1294           0 :         if (!vf)
    1295           0 :                 goto nla_put_vfinfo_failure;
    1296           0 :         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
    1297           0 :             nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
    1298           0 :             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
    1299           0 :             nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
    1300           0 :                     &vf_rate) ||
    1301           0 :             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
    1302           0 :                     &vf_tx_rate) ||
    1303           0 :             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
    1304           0 :                     &vf_spoofchk) ||
    1305           0 :             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
    1306           0 :                     &vf_linkstate) ||
    1307           0 :             nla_put(skb, IFLA_VF_RSS_QUERY_EN,
    1308             :                     sizeof(vf_rss_query_en),
    1309           0 :                     &vf_rss_query_en) ||
    1310           0 :             nla_put(skb, IFLA_VF_TRUST,
    1311             :                     sizeof(vf_trust), &vf_trust))
    1312           0 :                 goto nla_put_vf_failure;
    1313             : 
    1314           0 :         if (dev->netdev_ops->ndo_get_vf_guid &&
    1315           0 :             !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
    1316             :                                               &port_guid)) {
    1317           0 :                 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
    1318           0 :                             &node_guid) ||
    1319           0 :                     nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
    1320             :                             &port_guid))
    1321           0 :                         goto nla_put_vf_failure;
    1322             :         }
    1323           0 :         vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
    1324           0 :         if (!vfvlanlist)
    1325           0 :                 goto nla_put_vf_failure;
    1326           0 :         if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
    1327             :                     &vf_vlan_info)) {
    1328           0 :                 nla_nest_cancel(skb, vfvlanlist);
    1329           0 :                 goto nla_put_vf_failure;
    1330             :         }
    1331           0 :         nla_nest_end(skb, vfvlanlist);
    1332           0 :         memset(&vf_stats, 0, sizeof(vf_stats));
    1333           0 :         if (dev->netdev_ops->ndo_get_vf_stats)
    1334           0 :                 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
    1335             :                                                 &vf_stats);
    1336           0 :         vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
    1337           0 :         if (!vfstats)
    1338           0 :                 goto nla_put_vf_failure;
    1339           0 :         if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
    1340           0 :                               vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
    1341           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
    1342           0 :                               vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
    1343           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
    1344           0 :                               vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
    1345           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
    1346           0 :                               vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
    1347           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
    1348           0 :                               vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
    1349           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
    1350           0 :                               vf_stats.multicast, IFLA_VF_STATS_PAD) ||
    1351           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
    1352           0 :                               vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
    1353           0 :             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
    1354             :                               vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
    1355           0 :                 nla_nest_cancel(skb, vfstats);
    1356           0 :                 goto nla_put_vf_failure;
    1357             :         }
    1358           0 :         nla_nest_end(skb, vfstats);
    1359           0 :         nla_nest_end(skb, vf);
    1360           0 :         return 0;
    1361             : 
    1362           0 : nla_put_vf_failure:
    1363           0 :         nla_nest_cancel(skb, vf);
    1364           0 : nla_put_vfinfo_failure:
    1365           0 :         nla_nest_cancel(skb, vfinfo);
    1366           0 :         return -EMSGSIZE;
    1367             : }
    1368             : 
    1369          16 : static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
    1370             :                                            struct net_device *dev,
    1371             :                                            u32 ext_filter_mask)
    1372             : {
    1373          16 :         struct nlattr *vfinfo;
    1374          16 :         int i, num_vfs;
    1375             : 
    1376          16 :         if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
    1377             :                 return 0;
    1378             : 
    1379           0 :         num_vfs = dev_num_vf(dev->dev.parent);
    1380           0 :         if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
    1381             :                 return -EMSGSIZE;
    1382             : 
    1383           0 :         if (!dev->netdev_ops->ndo_get_vf_config)
    1384             :                 return 0;
    1385             : 
    1386           0 :         vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
    1387           0 :         if (!vfinfo)
    1388             :                 return -EMSGSIZE;
    1389             : 
    1390           0 :         for (i = 0; i < num_vfs; i++) {
    1391           0 :                 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
    1392             :                         return -EMSGSIZE;
    1393             :         }
    1394             : 
    1395           0 :         nla_nest_end(skb, vfinfo);
    1396           0 :         return 0;
    1397             : }
    1398             : 
    1399          16 : static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
    1400             : {
    1401          16 :         struct rtnl_link_ifmap map;
    1402             : 
    1403          16 :         memset(&map, 0, sizeof(map));
    1404          16 :         map.mem_start   = dev->mem_start;
    1405          16 :         map.mem_end     = dev->mem_end;
    1406          16 :         map.base_addr   = dev->base_addr;
    1407          16 :         map.irq         = dev->irq;
    1408          16 :         map.dma         = dev->dma;
    1409          16 :         map.port        = dev->if_port;
    1410             : 
    1411          16 :         if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
    1412           0 :                 return -EMSGSIZE;
    1413             : 
    1414             :         return 0;
    1415             : }
    1416             : 
    1417          16 : static u32 rtnl_xdp_prog_skb(struct net_device *dev)
    1418             : {
    1419          16 :         const struct bpf_prog *generic_xdp_prog;
    1420             : 
    1421          16 :         ASSERT_RTNL();
    1422             : 
    1423          32 :         generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
    1424          16 :         if (!generic_xdp_prog)
    1425             :                 return 0;
    1426           0 :         return generic_xdp_prog->aux->id;
    1427             : }
    1428             : 
    1429          16 : static u32 rtnl_xdp_prog_drv(struct net_device *dev)
    1430             : {
    1431          16 :         return dev_xdp_prog_id(dev, XDP_MODE_DRV);
    1432             : }
    1433             : 
    1434          16 : static u32 rtnl_xdp_prog_hw(struct net_device *dev)
    1435             : {
    1436          16 :         return dev_xdp_prog_id(dev, XDP_MODE_HW);
    1437             : }
    1438             : 
    1439          48 : static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
    1440             :                                u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
    1441             :                                u32 (*get_prog_id)(struct net_device *dev))
    1442             : {
    1443          48 :         u32 curr_id;
    1444          48 :         int err;
    1445             : 
    1446          48 :         curr_id = get_prog_id(dev);
    1447          48 :         if (!curr_id)
    1448             :                 return 0;
    1449             : 
    1450           0 :         *prog_id = curr_id;
    1451           0 :         err = nla_put_u32(skb, attr, curr_id);
    1452           0 :         if (err)
    1453             :                 return err;
    1454             : 
    1455           0 :         if (*mode != XDP_ATTACHED_NONE)
    1456           0 :                 *mode = XDP_ATTACHED_MULTI;
    1457             :         else
    1458           0 :                 *mode = tgt_mode;
    1459             : 
    1460             :         return 0;
    1461             : }
    1462             : 
    1463          16 : static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
    1464             : {
    1465          16 :         struct nlattr *xdp;
    1466          16 :         u32 prog_id;
    1467          16 :         int err;
    1468          16 :         u8 mode;
    1469             : 
    1470          16 :         xdp = nla_nest_start_noflag(skb, IFLA_XDP);
    1471          16 :         if (!xdp)
    1472             :                 return -EMSGSIZE;
    1473             : 
    1474          16 :         prog_id = 0;
    1475          16 :         mode = XDP_ATTACHED_NONE;
    1476          16 :         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
    1477             :                                   IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
    1478          16 :         if (err)
    1479           0 :                 goto err_cancel;
    1480          16 :         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
    1481             :                                   IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
    1482          16 :         if (err)
    1483           0 :                 goto err_cancel;
    1484          16 :         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
    1485             :                                   IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
    1486          16 :         if (err)
    1487           0 :                 goto err_cancel;
    1488             : 
    1489          16 :         err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
    1490          16 :         if (err)
    1491           0 :                 goto err_cancel;
    1492             : 
    1493          16 :         if (prog_id && mode != XDP_ATTACHED_MULTI) {
    1494           0 :                 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
    1495           0 :                 if (err)
    1496           0 :                         goto err_cancel;
    1497             :         }
    1498             : 
    1499          16 :         nla_nest_end(skb, xdp);
    1500          16 :         return 0;
    1501             : 
    1502           0 : err_cancel:
    1503           0 :         nla_nest_cancel(skb, xdp);
    1504           0 :         return err;
    1505             : }
    1506             : 
    1507           4 : static u32 rtnl_get_event(unsigned long event)
    1508             : {
    1509           4 :         u32 rtnl_event_type = IFLA_EVENT_NONE;
    1510             : 
    1511           4 :         switch (event) {
    1512             :         case NETDEV_REBOOT:
    1513             :                 rtnl_event_type = IFLA_EVENT_REBOOT;
    1514             :                 break;
    1515             :         case NETDEV_FEAT_CHANGE:
    1516             :                 rtnl_event_type = IFLA_EVENT_FEATURES;
    1517             :                 break;
    1518             :         case NETDEV_BONDING_FAILOVER:
    1519             :                 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
    1520             :                 break;
    1521             :         case NETDEV_NOTIFY_PEERS:
    1522             :                 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
    1523             :                 break;
    1524             :         case NETDEV_RESEND_IGMP:
    1525             :                 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
    1526             :                 break;
    1527             :         case NETDEV_CHANGEINFODATA:
    1528             :                 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
    1529             :                 break;
    1530             :         default:
    1531             :                 break;
    1532             :         }
    1533             : 
    1534           4 :         return rtnl_event_type;
    1535             : }
    1536             : 
    1537          16 : static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
    1538             : {
    1539          16 :         const struct net_device *upper_dev;
    1540          16 :         int ret = 0;
    1541             : 
    1542          16 :         rcu_read_lock();
    1543             : 
    1544          16 :         upper_dev = netdev_master_upper_dev_get_rcu(dev);
    1545          16 :         if (upper_dev)
    1546           0 :                 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
    1547             : 
    1548          16 :         rcu_read_unlock();
    1549          16 :         return ret;
    1550             : }
    1551             : 
    1552          16 : static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
    1553             :                           bool force)
    1554             : {
    1555          16 :         int ifindex = dev_get_iflink(dev);
    1556             : 
    1557          16 :         if (force || dev->ifindex != ifindex)
    1558           0 :                 return nla_put_u32(skb, IFLA_LINK, ifindex);
    1559             : 
    1560             :         return 0;
    1561             : }
    1562             : 
    1563          16 : static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
    1564             :                                               struct net_device *dev)
    1565             : {
    1566          16 :         char buf[IFALIASZ];
    1567          16 :         int ret;
    1568             : 
    1569          16 :         ret = dev_get_alias(dev, buf, sizeof(buf));
    1570          16 :         return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
    1571             : }
    1572             : 
    1573          16 : static int rtnl_fill_link_netnsid(struct sk_buff *skb,
    1574             :                                   const struct net_device *dev,
    1575             :                                   struct net *src_net, gfp_t gfp)
    1576             : {
    1577          16 :         bool put_iflink = false;
    1578             : 
    1579          16 :         if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
    1580           0 :                 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
    1581             : 
    1582           0 :                 if (!net_eq(dev_net(dev), link_net)) {
    1583             :                         int id = peernet2id_alloc(src_net, link_net, gfp);
    1584             : 
    1585             :                         if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
    1586             :                                 return -EMSGSIZE;
    1587             : 
    1588             :                         put_iflink = true;
    1589             :                 }
    1590             :         }
    1591             : 
    1592          16 :         return nla_put_iflink(skb, dev, put_iflink);
    1593             : }
    1594             : 
    1595          16 : static int rtnl_fill_link_af(struct sk_buff *skb,
    1596             :                              const struct net_device *dev,
    1597             :                              u32 ext_filter_mask)
    1598             : {
    1599          16 :         const struct rtnl_af_ops *af_ops;
    1600          16 :         struct nlattr *af_spec;
    1601             : 
    1602          16 :         af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
    1603          16 :         if (!af_spec)
    1604             :                 return -EMSGSIZE;
    1605             : 
    1606          31 :         list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
    1607          15 :                 struct nlattr *af;
    1608          15 :                 int err;
    1609             : 
    1610          15 :                 if (!af_ops->fill_link_af)
    1611           0 :                         continue;
    1612             : 
    1613          15 :                 af = nla_nest_start_noflag(skb, af_ops->family);
    1614          15 :                 if (!af)
    1615             :                         return -EMSGSIZE;
    1616             : 
    1617          15 :                 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
    1618             :                 /*
    1619             :                  * Caller may return ENODATA to indicate that there
    1620             :                  * was no data to be dumped. This is not an error, it
    1621             :                  * means we should trim the attribute header and
    1622             :                  * continue.
    1623             :                  */
    1624          15 :                 if (err == -ENODATA)
    1625           0 :                         nla_nest_cancel(skb, af);
    1626          15 :                 else if (err < 0)
    1627             :                         return -EMSGSIZE;
    1628             : 
    1629          15 :                 nla_nest_end(skb, af);
    1630             :         }
    1631             : 
    1632          16 :         nla_nest_end(skb, af_spec);
    1633          16 :         return 0;
    1634             : }
    1635             : 
    1636          16 : static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
    1637             :                                  const struct net_device *dev)
    1638             : {
    1639          16 :         struct netdev_name_node *name_node;
    1640          16 :         int count = 0;
    1641             : 
    1642          16 :         list_for_each_entry(name_node, &dev->name_node->list, list) {
    1643           0 :                 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
    1644             :                         return -EMSGSIZE;
    1645           0 :                 count++;
    1646             :         }
    1647             :         return count;
    1648             : }
    1649             : 
    1650          16 : static int rtnl_fill_prop_list(struct sk_buff *skb,
    1651             :                                const struct net_device *dev)
    1652             : {
    1653          16 :         struct nlattr *prop_list;
    1654          16 :         int ret;
    1655             : 
    1656          16 :         prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
    1657          16 :         if (!prop_list)
    1658             :                 return -EMSGSIZE;
    1659             : 
    1660          16 :         ret = rtnl_fill_alt_ifnames(skb, dev);
    1661          16 :         if (ret <= 0)
    1662          16 :                 goto nest_cancel;
    1663             : 
    1664           0 :         nla_nest_end(skb, prop_list);
    1665           0 :         return 0;
    1666             : 
    1667          16 : nest_cancel:
    1668          16 :         nla_nest_cancel(skb, prop_list);
    1669          16 :         return ret;
    1670             : }
    1671             : 
    1672          16 : static int rtnl_fill_proto_down(struct sk_buff *skb,
    1673             :                                 const struct net_device *dev)
    1674             : {
    1675          16 :         struct nlattr *pr;
    1676          16 :         u32 preason;
    1677             : 
    1678          16 :         if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
    1679           0 :                 goto nla_put_failure;
    1680             : 
    1681          16 :         preason = dev->proto_down_reason;
    1682          16 :         if (!preason)
    1683             :                 return 0;
    1684             : 
    1685           0 :         pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
    1686           0 :         if (!pr)
    1687             :                 return -EMSGSIZE;
    1688             : 
    1689           0 :         if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
    1690           0 :                 nla_nest_cancel(skb, pr);
    1691           0 :                 goto nla_put_failure;
    1692             :         }
    1693             : 
    1694           0 :         nla_nest_end(skb, pr);
    1695           0 :         return 0;
    1696             : 
    1697             : nla_put_failure:
    1698             :         return -EMSGSIZE;
    1699             : }
    1700             : 
    1701          16 : static int rtnl_fill_ifinfo(struct sk_buff *skb,
    1702             :                             struct net_device *dev, struct net *src_net,
    1703             :                             int type, u32 pid, u32 seq, u32 change,
    1704             :                             unsigned int flags, u32 ext_filter_mask,
    1705             :                             u32 event, int *new_nsid, int new_ifindex,
    1706             :                             int tgt_netnsid, gfp_t gfp)
    1707             : {
    1708          16 :         struct ifinfomsg *ifm;
    1709          16 :         struct nlmsghdr *nlh;
    1710             : 
    1711          16 :         ASSERT_RTNL();
    1712          16 :         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
    1713          16 :         if (nlh == NULL)
    1714             :                 return -EMSGSIZE;
    1715             : 
    1716          16 :         ifm = nlmsg_data(nlh);
    1717          16 :         ifm->ifi_family = AF_UNSPEC;
    1718          16 :         ifm->__ifi_pad = 0;
    1719          16 :         ifm->ifi_type = dev->type;
    1720          16 :         ifm->ifi_index = dev->ifindex;
    1721          16 :         ifm->ifi_flags = dev_get_flags(dev);
    1722          16 :         ifm->ifi_change = change;
    1723             : 
    1724          16 :         if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
    1725           0 :                 goto nla_put_failure;
    1726             : 
    1727          16 :         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
    1728          16 :             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
    1729          16 :             nla_put_u8(skb, IFLA_OPERSTATE,
    1730          16 :                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
    1731          16 :             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
    1732          16 :             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
    1733          16 :             nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
    1734          16 :             nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
    1735          16 :             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
    1736          16 :             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
    1737          16 :             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
    1738          16 :             nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
    1739          16 :             nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
    1740             : #ifdef CONFIG_RPS
    1741          32 :             nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
    1742             : #endif
    1743          16 :             put_master_ifindex(skb, dev) ||
    1744          16 :             nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
    1745          32 :             (dev->qdisc &&
    1746          32 :              nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
    1747          16 :             nla_put_ifalias(skb, dev) ||
    1748          16 :             nla_put_u32(skb, IFLA_CARRIER_CHANGES,
    1749          16 :                         atomic_read(&dev->carrier_up_count) +
    1750          16 :                         atomic_read(&dev->carrier_down_count)) ||
    1751          16 :             nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
    1752          16 :                         atomic_read(&dev->carrier_up_count)) ||
    1753          16 :             nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
    1754          16 :                         atomic_read(&dev->carrier_down_count)))
    1755           0 :                 goto nla_put_failure;
    1756             : 
    1757          16 :         if (rtnl_fill_proto_down(skb, dev))
    1758           0 :                 goto nla_put_failure;
    1759             : 
    1760          16 :         if (event != IFLA_EVENT_NONE) {
    1761           0 :                 if (nla_put_u32(skb, IFLA_EVENT, event))
    1762           0 :                         goto nla_put_failure;
    1763             :         }
    1764             : 
    1765          16 :         if (rtnl_fill_link_ifmap(skb, dev))
    1766           0 :                 goto nla_put_failure;
    1767             : 
    1768          16 :         if (dev->addr_len) {
    1769          32 :                 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
    1770          16 :                     nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
    1771           0 :                         goto nla_put_failure;
    1772             :         }
    1773             : 
    1774          16 :         if (rtnl_phys_port_id_fill(skb, dev))
    1775           0 :                 goto nla_put_failure;
    1776             : 
    1777          16 :         if (rtnl_phys_port_name_fill(skb, dev))
    1778           0 :                 goto nla_put_failure;
    1779             : 
    1780          16 :         if (rtnl_phys_switch_id_fill(skb, dev))
    1781           0 :                 goto nla_put_failure;
    1782             : 
    1783          16 :         if (rtnl_fill_stats(skb, dev))
    1784           0 :                 goto nla_put_failure;
    1785             : 
    1786          16 :         if (rtnl_fill_vf(skb, dev, ext_filter_mask))
    1787           0 :                 goto nla_put_failure;
    1788             : 
    1789          16 :         if (rtnl_port_fill(skb, dev, ext_filter_mask))
    1790           0 :                 goto nla_put_failure;
    1791             : 
    1792          16 :         if (rtnl_xdp_fill(skb, dev))
    1793           0 :                 goto nla_put_failure;
    1794             : 
    1795          16 :         if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
    1796           0 :                 if (rtnl_link_fill(skb, dev) < 0)
    1797           0 :                         goto nla_put_failure;
    1798             :         }
    1799             : 
    1800          16 :         if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
    1801           0 :                 goto nla_put_failure;
    1802             : 
    1803          16 :         if (new_nsid &&
    1804           0 :             nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
    1805           0 :                 goto nla_put_failure;
    1806          16 :         if (new_ifindex &&
    1807           0 :             nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
    1808           0 :                 goto nla_put_failure;
    1809             : 
    1810          24 :         if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
    1811           8 :             nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
    1812           0 :                 goto nla_put_failure;
    1813             : 
    1814          16 :         rcu_read_lock();
    1815          16 :         if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
    1816           0 :                 goto nla_put_failure_rcu;
    1817          16 :         rcu_read_unlock();
    1818             : 
    1819          16 :         if (rtnl_fill_prop_list(skb, dev))
    1820           0 :                 goto nla_put_failure;
    1821             : 
    1822          16 :         nlmsg_end(skb, nlh);
    1823          16 :         return 0;
    1824             : 
    1825           0 : nla_put_failure_rcu:
    1826           0 :         rcu_read_unlock();
    1827           0 : nla_put_failure:
    1828           0 :         nlmsg_cancel(skb, nlh);
    1829           0 :         return -EMSGSIZE;
    1830             : }
    1831             : 
    1832             : static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
    1833             :         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
    1834             :         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
    1835             :         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
    1836             :         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
    1837             :         [IFLA_MTU]              = { .type = NLA_U32 },
    1838             :         [IFLA_LINK]             = { .type = NLA_U32 },
    1839             :         [IFLA_MASTER]           = { .type = NLA_U32 },
    1840             :         [IFLA_CARRIER]          = { .type = NLA_U8 },
    1841             :         [IFLA_TXQLEN]           = { .type = NLA_U32 },
    1842             :         [IFLA_WEIGHT]           = { .type = NLA_U32 },
    1843             :         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
    1844             :         [IFLA_LINKMODE]         = { .type = NLA_U8 },
    1845             :         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
    1846             :         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
    1847             :         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
    1848             :         /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
    1849             :          * allow 0-length string (needed to remove an alias).
    1850             :          */
    1851             :         [IFLA_IFALIAS]          = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
    1852             :         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
    1853             :         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
    1854             :         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
    1855             :         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
    1856             :         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
    1857             :         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
    1858             :         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
    1859             :         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
    1860             :         [IFLA_GSO_MAX_SEGS]     = { .type = NLA_U32 },
    1861             :         [IFLA_GSO_MAX_SIZE]     = { .type = NLA_U32 },
    1862             :         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
    1863             :         [IFLA_CARRIER_CHANGES]  = { .type = NLA_U32 },  /* ignored */
    1864             :         [IFLA_PHYS_SWITCH_ID]   = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
    1865             :         [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
    1866             :         [IFLA_PROTO_DOWN]       = { .type = NLA_U8 },
    1867             :         [IFLA_XDP]              = { .type = NLA_NESTED },
    1868             :         [IFLA_EVENT]            = { .type = NLA_U32 },
    1869             :         [IFLA_GROUP]            = { .type = NLA_U32 },
    1870             :         [IFLA_TARGET_NETNSID]   = { .type = NLA_S32 },
    1871             :         [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
    1872             :         [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
    1873             :         [IFLA_MIN_MTU]          = { .type = NLA_U32 },
    1874             :         [IFLA_MAX_MTU]          = { .type = NLA_U32 },
    1875             :         [IFLA_PROP_LIST]        = { .type = NLA_NESTED },
    1876             :         [IFLA_ALT_IFNAME]       = { .type = NLA_STRING,
    1877             :                                     .len = ALTIFNAMSIZ - 1 },
    1878             :         [IFLA_PERM_ADDRESS]     = { .type = NLA_REJECT },
    1879             :         [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
    1880             : };
    1881             : 
    1882             : static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
    1883             :         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
    1884             :         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
    1885             :         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
    1886             :         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
    1887             : };
    1888             : 
    1889             : static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
    1890             :         [IFLA_VF_MAC]           = { .len = sizeof(struct ifla_vf_mac) },
    1891             :         [IFLA_VF_BROADCAST]     = { .type = NLA_REJECT },
    1892             :         [IFLA_VF_VLAN]          = { .len = sizeof(struct ifla_vf_vlan) },
    1893             :         [IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
    1894             :         [IFLA_VF_TX_RATE]       = { .len = sizeof(struct ifla_vf_tx_rate) },
    1895             :         [IFLA_VF_SPOOFCHK]      = { .len = sizeof(struct ifla_vf_spoofchk) },
    1896             :         [IFLA_VF_RATE]          = { .len = sizeof(struct ifla_vf_rate) },
    1897             :         [IFLA_VF_LINK_STATE]    = { .len = sizeof(struct ifla_vf_link_state) },
    1898             :         [IFLA_VF_RSS_QUERY_EN]  = { .len = sizeof(struct ifla_vf_rss_query_en) },
    1899             :         [IFLA_VF_STATS]         = { .type = NLA_NESTED },
    1900             :         [IFLA_VF_TRUST]         = { .len = sizeof(struct ifla_vf_trust) },
    1901             :         [IFLA_VF_IB_NODE_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
    1902             :         [IFLA_VF_IB_PORT_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
    1903             : };
    1904             : 
    1905             : static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
    1906             :         [IFLA_PORT_VF]          = { .type = NLA_U32 },
    1907             :         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
    1908             :                                     .len = PORT_PROFILE_MAX },
    1909             :         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
    1910             :                                       .len = PORT_UUID_MAX },
    1911             :         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
    1912             :                                     .len = PORT_UUID_MAX },
    1913             :         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
    1914             :         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
    1915             : 
    1916             :         /* Unused, but we need to keep it here since user space could
    1917             :          * fill it. It's also broken with regard to NLA_BINARY use in
    1918             :          * combination with structs.
    1919             :          */
    1920             :         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
    1921             :                                     .len = sizeof(struct ifla_port_vsi) },
    1922             : };
    1923             : 
    1924             : static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
    1925             :         [IFLA_XDP_UNSPEC]       = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
    1926             :         [IFLA_XDP_FD]           = { .type = NLA_S32 },
    1927             :         [IFLA_XDP_EXPECTED_FD]  = { .type = NLA_S32 },
    1928             :         [IFLA_XDP_ATTACHED]     = { .type = NLA_U8 },
    1929             :         [IFLA_XDP_FLAGS]        = { .type = NLA_U32 },
    1930             :         [IFLA_XDP_PROG_ID]      = { .type = NLA_U32 },
    1931             : };
    1932             : 
    1933           0 : static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
    1934             : {
    1935           0 :         const struct rtnl_link_ops *ops = NULL;
    1936           0 :         struct nlattr *linfo[IFLA_INFO_MAX + 1];
    1937             : 
    1938           0 :         if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
    1939             :                 return NULL;
    1940             : 
    1941           0 :         if (linfo[IFLA_INFO_KIND]) {
    1942           0 :                 char kind[MODULE_NAME_LEN];
    1943             : 
    1944           0 :                 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
    1945           0 :                 ops = rtnl_link_ops_get(kind);
    1946             :         }
    1947             : 
    1948             :         return ops;
    1949             : }
    1950             : 
    1951          12 : static bool link_master_filtered(struct net_device *dev, int master_idx)
    1952             : {
    1953          12 :         struct net_device *master;
    1954             : 
    1955          12 :         if (!master_idx)
    1956             :                 return false;
    1957             : 
    1958           0 :         master = netdev_master_upper_dev_get(dev);
    1959           0 :         if (!master || master->ifindex != master_idx)
    1960           0 :                 return true;
    1961             : 
    1962             :         return false;
    1963             : }
    1964             : 
    1965          12 : static bool link_kind_filtered(const struct net_device *dev,
    1966             :                                const struct rtnl_link_ops *kind_ops)
    1967             : {
    1968          12 :         if (kind_ops && dev->rtnl_link_ops != kind_ops)
    1969             :                 return true;
    1970             : 
    1971             :         return false;
    1972             : }
    1973             : 
    1974          12 : static bool link_dump_filtered(struct net_device *dev,
    1975             :                                int master_idx,
    1976             :                                const struct rtnl_link_ops *kind_ops)
    1977             : {
    1978          12 :         if (link_master_filtered(dev, master_idx) ||
    1979          12 :             link_kind_filtered(dev, kind_ops))
    1980           0 :                 return true;
    1981             : 
    1982             :         return false;
    1983             : }
    1984             : 
    1985             : /**
    1986             :  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
    1987             :  * @sk: netlink socket
    1988             :  * @netnsid: network namespace identifier
    1989             :  *
    1990             :  * Returns the network namespace identified by netnsid on success or an error
    1991             :  * pointer on failure.
    1992             :  */
    1993           0 : struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
    1994             : {
    1995           0 :         struct net *net;
    1996             : 
    1997           0 :         net = get_net_ns_by_id(sock_net(sk), netnsid);
    1998           0 :         if (!net)
    1999           0 :                 return ERR_PTR(-EINVAL);
    2000             : 
    2001             :         /* For now, the caller is required to have CAP_NET_ADMIN in
    2002             :          * the user namespace owning the target net ns.
    2003             :          */
    2004           0 :         if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
    2005           0 :                 put_net(net);
    2006           0 :                 return ERR_PTR(-EACCES);
    2007             :         }
    2008             :         return net;
    2009             : }
    2010             : EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
    2011             : 
    2012          12 : static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
    2013             :                                       bool strict_check, struct nlattr **tb,
    2014             :                                       struct netlink_ext_ack *extack)
    2015             : {
    2016          12 :         int hdrlen;
    2017             : 
    2018          12 :         if (strict_check) {
    2019           0 :                 struct ifinfomsg *ifm;
    2020             : 
    2021           0 :                 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
    2022           0 :                         NL_SET_ERR_MSG(extack, "Invalid header for link dump");
    2023           0 :                         return -EINVAL;
    2024             :                 }
    2025             : 
    2026           0 :                 ifm = nlmsg_data(nlh);
    2027           0 :                 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
    2028           0 :                     ifm->ifi_change) {
    2029           0 :                         NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
    2030           0 :                         return -EINVAL;
    2031             :                 }
    2032           0 :                 if (ifm->ifi_index) {
    2033           0 :                         NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
    2034           0 :                         return -EINVAL;
    2035             :                 }
    2036             : 
    2037           0 :                 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
    2038             :                                                      IFLA_MAX, ifla_policy,
    2039             :                                                      extack);
    2040             :         }
    2041             : 
    2042             :         /* A hack to preserve kernel<->userspace interface.
    2043             :          * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
    2044             :          * However, before Linux v3.9 the code here assumed rtgenmsg and that's
    2045             :          * what iproute2 < v3.9.0 used.
    2046             :          * We can detect the old iproute2. Even including the IFLA_EXT_MASK
    2047             :          * attribute, its netlink message is shorter than struct ifinfomsg.
    2048             :          */
    2049          12 :         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
    2050             :                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
    2051             : 
    2052          12 :         return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
    2053             :                                       extack);
    2054             : }
    2055             : 
    2056          12 : static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
    2057             : {
    2058          12 :         struct netlink_ext_ack *extack = cb->extack;
    2059          12 :         const struct nlmsghdr *nlh = cb->nlh;
    2060          12 :         struct net *net = sock_net(skb->sk);
    2061          12 :         struct net *tgt_net = net;
    2062          12 :         int h, s_h;
    2063          12 :         int idx = 0, s_idx;
    2064          12 :         struct net_device *dev;
    2065          12 :         struct hlist_head *head;
    2066          12 :         struct nlattr *tb[IFLA_MAX+1];
    2067          12 :         u32 ext_filter_mask = 0;
    2068          12 :         const struct rtnl_link_ops *kind_ops = NULL;
    2069          12 :         unsigned int flags = NLM_F_MULTI;
    2070          12 :         int master_idx = 0;
    2071          12 :         int netnsid = -1;
    2072          12 :         int err, i;
    2073             : 
    2074          12 :         s_h = cb->args[0];
    2075          12 :         s_idx = cb->args[1];
    2076             : 
    2077          12 :         err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
    2078          12 :         if (err < 0) {
    2079           0 :                 if (cb->strict_check)
    2080             :                         return err;
    2081             : 
    2082           0 :                 goto walk_entries;
    2083             :         }
    2084             : 
    2085         684 :         for (i = 0; i <= IFLA_MAX; ++i) {
    2086         672 :                 if (!tb[i])
    2087         672 :                         continue;
    2088             : 
    2089             :                 /* new attributes should only be added with strict checking */
    2090           0 :                 switch (i) {
    2091           0 :                 case IFLA_TARGET_NETNSID:
    2092           0 :                         netnsid = nla_get_s32(tb[i]);
    2093           0 :                         tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
    2094           0 :                         if (IS_ERR(tgt_net)) {
    2095           0 :                                 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
    2096           0 :                                 return PTR_ERR(tgt_net);
    2097             :                         }
    2098             :                         break;
    2099           0 :                 case IFLA_EXT_MASK:
    2100           0 :                         ext_filter_mask = nla_get_u32(tb[i]);
    2101           0 :                         break;
    2102           0 :                 case IFLA_MASTER:
    2103           0 :                         master_idx = nla_get_u32(tb[i]);
    2104           0 :                         break;
    2105           0 :                 case IFLA_LINKINFO:
    2106           0 :                         kind_ops = linkinfo_to_kind_ops(tb[i]);
    2107           0 :                         break;
    2108           0 :                 default:
    2109           0 :                         if (cb->strict_check) {
    2110           0 :                                 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
    2111           0 :                                 return -EINVAL;
    2112             :                         }
    2113             :                 }
    2114             :         }
    2115             : 
    2116          12 :         if (master_idx || kind_ops)
    2117           0 :                 flags |= NLM_F_DUMP_FILTERED;
    2118             : 
    2119          12 : walk_entries:
    2120        1548 :         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
    2121        1536 :                 idx = 0;
    2122        1536 :                 head = &tgt_net->dev_index_head[h];
    2123        3084 :                 hlist_for_each_entry(dev, head, index_hlist) {
    2124          12 :                         if (link_dump_filtered(dev, master_idx, kind_ops))
    2125           0 :                                 goto cont;
    2126          12 :                         if (idx < s_idx)
    2127           0 :                                 goto cont;
    2128          24 :                         err = rtnl_fill_ifinfo(skb, dev, net,
    2129             :                                                RTM_NEWLINK,
    2130          12 :                                                NETLINK_CB(cb->skb).portid,
    2131             :                                                nlh->nlmsg_seq, 0, flags,
    2132             :                                                ext_filter_mask, 0, NULL, 0,
    2133             :                                                netnsid, GFP_KERNEL);
    2134             : 
    2135          12 :                         if (err < 0) {
    2136           0 :                                 if (likely(skb->len))
    2137           0 :                                         goto out;
    2138             : 
    2139           0 :                                 goto out_err;
    2140             :                         }
    2141          12 : cont:
    2142          12 :                         idx++;
    2143             :                 }
    2144             :         }
    2145          12 : out:
    2146          12 :         err = skb->len;
    2147          12 : out_err:
    2148          12 :         cb->args[1] = idx;
    2149          12 :         cb->args[0] = h;
    2150          12 :         cb->seq = tgt_net->dev_base_seq;
    2151          12 :         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
    2152          12 :         if (netnsid >= 0)
    2153          12 :                 put_net(tgt_net);
    2154             : 
    2155          12 :         return err;
    2156             : }
    2157             : 
    2158           0 : int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
    2159             :                         struct netlink_ext_ack *exterr)
    2160             : {
    2161           0 :         return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
    2162             :                                     exterr);
    2163             : }
    2164             : EXPORT_SYMBOL(rtnl_nla_parse_ifla);
    2165             : 
    2166           0 : struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
    2167             : {
    2168           0 :         struct net *net;
    2169             :         /* Examine the link attributes and figure out which
    2170             :          * network namespace we are talking about.
    2171             :          */
    2172           0 :         if (tb[IFLA_NET_NS_PID])
    2173           0 :                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
    2174           0 :         else if (tb[IFLA_NET_NS_FD])
    2175           0 :                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
    2176             :         else
    2177           0 :                 net = get_net(src_net);
    2178           0 :         return net;
    2179             : }
    2180             : EXPORT_SYMBOL(rtnl_link_get_net);
    2181             : 
    2182             : /* Figure out which network namespace we are talking about by
    2183             :  * examining the link attributes in the following order:
    2184             :  *
    2185             :  * 1. IFLA_NET_NS_PID
    2186             :  * 2. IFLA_NET_NS_FD
    2187             :  * 3. IFLA_TARGET_NETNSID
    2188             :  */
    2189           0 : static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
    2190             :                                                struct nlattr *tb[])
    2191             : {
    2192           0 :         struct net *net;
    2193             : 
    2194           0 :         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
    2195           0 :                 return rtnl_link_get_net(src_net, tb);
    2196             : 
    2197           0 :         if (!tb[IFLA_TARGET_NETNSID])
    2198           0 :                 return get_net(src_net);
    2199             : 
    2200           0 :         net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
    2201           0 :         if (!net)
    2202           0 :                 return ERR_PTR(-EINVAL);
    2203             : 
    2204             :         return net;
    2205             : }
    2206             : 
    2207           0 : static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
    2208             :                                              struct net *src_net,
    2209             :                                              struct nlattr *tb[], int cap)
    2210             : {
    2211           0 :         struct net *net;
    2212             : 
    2213           0 :         net = rtnl_link_get_net_by_nlattr(src_net, tb);
    2214           0 :         if (IS_ERR(net))
    2215             :                 return net;
    2216             : 
    2217           0 :         if (!netlink_ns_capable(skb, net->user_ns, cap)) {
    2218           0 :                 put_net(net);
    2219           0 :                 return ERR_PTR(-EPERM);
    2220             :         }
    2221             : 
    2222             :         return net;
    2223             : }
    2224             : 
    2225             : /* Verify that rtnetlink requests do not pass additional properties
    2226             :  * potentially referring to different network namespaces.
    2227             :  */
    2228           5 : static int rtnl_ensure_unique_netns(struct nlattr *tb[],
    2229             :                                     struct netlink_ext_ack *extack,
    2230             :                                     bool netns_id_only)
    2231             : {
    2232             : 
    2233           5 :         if (netns_id_only) {
    2234           0 :                 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
    2235             :                         return 0;
    2236             : 
    2237           0 :                 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
    2238           0 :                 return -EOPNOTSUPP;
    2239             :         }
    2240             : 
    2241           5 :         if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
    2242           0 :                 goto invalid_attr;
    2243             : 
    2244           5 :         if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
    2245           0 :                 goto invalid_attr;
    2246             : 
    2247           5 :         if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
    2248           0 :                 goto invalid_attr;
    2249             : 
    2250             :         return 0;
    2251             : 
    2252           0 : invalid_attr:
    2253           0 :         NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
    2254             :         return -EINVAL;
    2255             : }
    2256             : 
    2257           7 : static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
    2258             : {
    2259           7 :         if (dev) {
    2260           5 :                 if (tb[IFLA_ADDRESS] &&
    2261           0 :                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
    2262             :                         return -EINVAL;
    2263             : 
    2264           5 :                 if (tb[IFLA_BROADCAST] &&
    2265           0 :                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
    2266             :                         return -EINVAL;
    2267             :         }
    2268             : 
    2269           7 :         if (tb[IFLA_AF_SPEC]) {
    2270           0 :                 struct nlattr *af;
    2271           0 :                 int rem, err;
    2272             : 
    2273           0 :                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
    2274           0 :                         const struct rtnl_af_ops *af_ops;
    2275             : 
    2276           0 :                         rcu_read_lock();
    2277           0 :                         af_ops = rtnl_af_lookup(nla_type(af));
    2278           0 :                         if (!af_ops) {
    2279           0 :                                 rcu_read_unlock();
    2280           0 :                                 return -EAFNOSUPPORT;
    2281             :                         }
    2282             : 
    2283           0 :                         if (!af_ops->set_link_af) {
    2284           0 :                                 rcu_read_unlock();
    2285           0 :                                 return -EOPNOTSUPP;
    2286             :                         }
    2287             : 
    2288           0 :                         if (af_ops->validate_link_af) {
    2289           0 :                                 err = af_ops->validate_link_af(dev, af);
    2290           0 :                                 if (err < 0) {
    2291           0 :                                         rcu_read_unlock();
    2292           0 :                                         return err;
    2293             :                                 }
    2294             :                         }
    2295             : 
    2296           0 :                         rcu_read_unlock();
    2297             :                 }
    2298             :         }
    2299             : 
    2300             :         return 0;
    2301             : }
    2302             : 
    2303           0 : static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
    2304             :                                   int guid_type)
    2305             : {
    2306           0 :         const struct net_device_ops *ops = dev->netdev_ops;
    2307             : 
    2308           0 :         return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
    2309             : }
    2310             : 
    2311           0 : static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
    2312             : {
    2313           0 :         if (dev->type != ARPHRD_INFINIBAND)
    2314             :                 return -EOPNOTSUPP;
    2315             : 
    2316           0 :         return handle_infiniband_guid(dev, ivt, guid_type);
    2317             : }
    2318             : 
    2319           0 : static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
    2320             : {
    2321           0 :         const struct net_device_ops *ops = dev->netdev_ops;
    2322           0 :         int err = -EINVAL;
    2323             : 
    2324           0 :         if (tb[IFLA_VF_MAC]) {
    2325           0 :                 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
    2326             : 
    2327           0 :                 if (ivm->vf >= INT_MAX)
    2328             :                         return -EINVAL;
    2329           0 :                 err = -EOPNOTSUPP;
    2330           0 :                 if (ops->ndo_set_vf_mac)
    2331           0 :                         err = ops->ndo_set_vf_mac(dev, ivm->vf,
    2332           0 :                                                   ivm->mac);
    2333           0 :                 if (err < 0)
    2334           0 :                         return err;
    2335             :         }
    2336             : 
    2337           0 :         if (tb[IFLA_VF_VLAN]) {
    2338           0 :                 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
    2339             : 
    2340           0 :                 if (ivv->vf >= INT_MAX)
    2341             :                         return -EINVAL;
    2342           0 :                 err = -EOPNOTSUPP;
    2343           0 :                 if (ops->ndo_set_vf_vlan)
    2344           0 :                         err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
    2345           0 :                                                    ivv->qos,
    2346             :                                                    htons(ETH_P_8021Q));
    2347           0 :                 if (err < 0)
    2348           0 :                         return err;
    2349             :         }
    2350             : 
    2351           0 :         if (tb[IFLA_VF_VLAN_LIST]) {
    2352           0 :                 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
    2353           0 :                 struct nlattr *attr;
    2354           0 :                 int rem, len = 0;
    2355             : 
    2356           0 :                 err = -EOPNOTSUPP;
    2357           0 :                 if (!ops->ndo_set_vf_vlan)
    2358           0 :                         return err;
    2359             : 
    2360           0 :                 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
    2361           0 :                         if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
    2362           0 :                             nla_len(attr) < NLA_HDRLEN) {
    2363             :                                 return -EINVAL;
    2364             :                         }
    2365           0 :                         if (len >= MAX_VLAN_LIST_LEN)
    2366             :                                 return -EOPNOTSUPP;
    2367           0 :                         ivvl[len] = nla_data(attr);
    2368             : 
    2369           0 :                         len++;
    2370             :                 }
    2371           0 :                 if (len == 0)
    2372             :                         return -EINVAL;
    2373             : 
    2374           0 :                 if (ivvl[0]->vf >= INT_MAX)
    2375             :                         return -EINVAL;
    2376           0 :                 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
    2377           0 :                                            ivvl[0]->qos, ivvl[0]->vlan_proto);
    2378           0 :                 if (err < 0)
    2379             :                         return err;
    2380             :         }
    2381             : 
    2382           0 :         if (tb[IFLA_VF_TX_RATE]) {
    2383           0 :                 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
    2384           0 :                 struct ifla_vf_info ivf;
    2385             : 
    2386           0 :                 if (ivt->vf >= INT_MAX)
    2387           0 :                         return -EINVAL;
    2388           0 :                 err = -EOPNOTSUPP;
    2389           0 :                 if (ops->ndo_get_vf_config)
    2390           0 :                         err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
    2391           0 :                 if (err < 0)
    2392           0 :                         return err;
    2393             : 
    2394           0 :                 err = -EOPNOTSUPP;
    2395           0 :                 if (ops->ndo_set_vf_rate)
    2396           0 :                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
    2397           0 :                                                    ivf.min_tx_rate,
    2398           0 :                                                    ivt->rate);
    2399           0 :                 if (err < 0)
    2400           0 :                         return err;
    2401             :         }
    2402             : 
    2403           0 :         if (tb[IFLA_VF_RATE]) {
    2404           0 :                 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
    2405             : 
    2406           0 :                 if (ivt->vf >= INT_MAX)
    2407             :                         return -EINVAL;
    2408           0 :                 err = -EOPNOTSUPP;
    2409           0 :                 if (ops->ndo_set_vf_rate)
    2410           0 :                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
    2411           0 :                                                    ivt->min_tx_rate,
    2412           0 :                                                    ivt->max_tx_rate);
    2413           0 :                 if (err < 0)
    2414           0 :                         return err;
    2415             :         }
    2416             : 
    2417           0 :         if (tb[IFLA_VF_SPOOFCHK]) {
    2418           0 :                 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
    2419             : 
    2420           0 :                 if (ivs->vf >= INT_MAX)
    2421             :                         return -EINVAL;
    2422           0 :                 err = -EOPNOTSUPP;
    2423           0 :                 if (ops->ndo_set_vf_spoofchk)
    2424           0 :                         err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
    2425           0 :                                                        ivs->setting);
    2426           0 :                 if (err < 0)
    2427           0 :                         return err;
    2428             :         }
    2429             : 
    2430           0 :         if (tb[IFLA_VF_LINK_STATE]) {
    2431           0 :                 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
    2432             : 
    2433           0 :                 if (ivl->vf >= INT_MAX)
    2434             :                         return -EINVAL;
    2435           0 :                 err = -EOPNOTSUPP;
    2436           0 :                 if (ops->ndo_set_vf_link_state)
    2437           0 :                         err = ops->ndo_set_vf_link_state(dev, ivl->vf,
    2438           0 :                                                          ivl->link_state);
    2439           0 :                 if (err < 0)
    2440           0 :                         return err;
    2441             :         }
    2442             : 
    2443           0 :         if (tb[IFLA_VF_RSS_QUERY_EN]) {
    2444           0 :                 struct ifla_vf_rss_query_en *ivrssq_en;
    2445             : 
    2446           0 :                 err = -EOPNOTSUPP;
    2447           0 :                 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
    2448           0 :                 if (ivrssq_en->vf >= INT_MAX)
    2449             :                         return -EINVAL;
    2450           0 :                 if (ops->ndo_set_vf_rss_query_en)
    2451           0 :                         err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
    2452           0 :                                                            ivrssq_en->setting);
    2453           0 :                 if (err < 0)
    2454           0 :                         return err;
    2455             :         }
    2456             : 
    2457           0 :         if (tb[IFLA_VF_TRUST]) {
    2458           0 :                 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
    2459             : 
    2460           0 :                 if (ivt->vf >= INT_MAX)
    2461             :                         return -EINVAL;
    2462           0 :                 err = -EOPNOTSUPP;
    2463           0 :                 if (ops->ndo_set_vf_trust)
    2464           0 :                         err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
    2465           0 :                 if (err < 0)
    2466           0 :                         return err;
    2467             :         }
    2468             : 
    2469           0 :         if (tb[IFLA_VF_IB_NODE_GUID]) {
    2470           0 :                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
    2471             : 
    2472           0 :                 if (ivt->vf >= INT_MAX)
    2473             :                         return -EINVAL;
    2474           0 :                 if (!ops->ndo_set_vf_guid)
    2475             :                         return -EOPNOTSUPP;
    2476           0 :                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
    2477             :         }
    2478             : 
    2479           0 :         if (tb[IFLA_VF_IB_PORT_GUID]) {
    2480           0 :                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
    2481             : 
    2482           0 :                 if (ivt->vf >= INT_MAX)
    2483             :                         return -EINVAL;
    2484           0 :                 if (!ops->ndo_set_vf_guid)
    2485             :                         return -EOPNOTSUPP;
    2486             : 
    2487           0 :                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
    2488             :         }
    2489             : 
    2490             :         return err;
    2491             : }
    2492             : 
    2493           0 : static int do_set_master(struct net_device *dev, int ifindex,
    2494             :                          struct netlink_ext_ack *extack)
    2495             : {
    2496           0 :         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
    2497           0 :         const struct net_device_ops *ops;
    2498           0 :         int err;
    2499             : 
    2500           0 :         if (upper_dev) {
    2501           0 :                 if (upper_dev->ifindex == ifindex)
    2502             :                         return 0;
    2503           0 :                 ops = upper_dev->netdev_ops;
    2504           0 :                 if (ops->ndo_del_slave) {
    2505           0 :                         err = ops->ndo_del_slave(upper_dev, dev);
    2506           0 :                         if (err)
    2507             :                                 return err;
    2508             :                 } else {
    2509             :                         return -EOPNOTSUPP;
    2510             :                 }
    2511             :         }
    2512             : 
    2513           0 :         if (ifindex) {
    2514           0 :                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
    2515           0 :                 if (!upper_dev)
    2516             :                         return -EINVAL;
    2517           0 :                 ops = upper_dev->netdev_ops;
    2518           0 :                 if (ops->ndo_add_slave) {
    2519           0 :                         err = ops->ndo_add_slave(upper_dev, dev, extack);
    2520           0 :                         if (err)
    2521           0 :                                 return err;
    2522             :                 } else {
    2523             :                         return -EOPNOTSUPP;
    2524             :                 }
    2525             :         }
    2526             :         return 0;
    2527             : }
    2528             : 
    2529             : static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
    2530             :         [IFLA_PROTO_DOWN_REASON_MASK]   = { .type = NLA_U32 },
    2531             :         [IFLA_PROTO_DOWN_REASON_VALUE]  = { .type = NLA_U32 },
    2532             : };
    2533             : 
    2534           0 : static int do_set_proto_down(struct net_device *dev,
    2535             :                              struct nlattr *nl_proto_down,
    2536             :                              struct nlattr *nl_proto_down_reason,
    2537             :                              struct netlink_ext_ack *extack)
    2538             : {
    2539           0 :         struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
    2540           0 :         const struct net_device_ops *ops = dev->netdev_ops;
    2541           0 :         unsigned long mask = 0;
    2542           0 :         u32 value;
    2543           0 :         bool proto_down;
    2544           0 :         int err;
    2545             : 
    2546           0 :         if (!ops->ndo_change_proto_down) {
    2547           0 :                 NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
    2548           0 :                 return -EOPNOTSUPP;
    2549             :         }
    2550             : 
    2551           0 :         if (nl_proto_down_reason) {
    2552           0 :                 err = nla_parse_nested_deprecated(pdreason,
    2553             :                                                   IFLA_PROTO_DOWN_REASON_MAX,
    2554             :                                                   nl_proto_down_reason,
    2555             :                                                   ifla_proto_down_reason_policy,
    2556             :                                                   NULL);
    2557           0 :                 if (err < 0)
    2558             :                         return err;
    2559             : 
    2560           0 :                 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
    2561           0 :                         NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
    2562           0 :                         return -EINVAL;
    2563             :                 }
    2564             : 
    2565           0 :                 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
    2566             : 
    2567           0 :                 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
    2568           0 :                         mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
    2569             : 
    2570           0 :                 dev_change_proto_down_reason(dev, mask, value);
    2571             :         }
    2572             : 
    2573           0 :         if (nl_proto_down) {
    2574           0 :                 proto_down = nla_get_u8(nl_proto_down);
    2575             : 
    2576             :                 /* Dont turn off protodown if there are active reasons */
    2577           0 :                 if (!proto_down && dev->proto_down_reason) {
    2578           0 :                         NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
    2579           0 :                         return -EBUSY;
    2580             :                 }
    2581           0 :                 err = dev_change_proto_down(dev,
    2582             :                                             proto_down);
    2583           0 :                 if (err)
    2584           0 :                         return err;
    2585             :         }
    2586             : 
    2587             :         return 0;
    2588             : }
    2589             : 
    2590             : #define DO_SETLINK_MODIFIED     0x01
    2591             : /* notify flag means notify + modified. */
    2592             : #define DO_SETLINK_NOTIFY       0x03
    2593           3 : static int do_setlink(const struct sk_buff *skb,
    2594             :                       struct net_device *dev, struct ifinfomsg *ifm,
    2595             :                       struct netlink_ext_ack *extack,
    2596             :                       struct nlattr **tb, char *ifname, int status)
    2597             : {
    2598           3 :         const struct net_device_ops *ops = dev->netdev_ops;
    2599           3 :         int err;
    2600             : 
    2601           3 :         err = validate_linkmsg(dev, tb);
    2602           3 :         if (err < 0)
    2603             :                 return err;
    2604             : 
    2605           3 :         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
    2606           0 :                 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
    2607             :                                                             tb, CAP_NET_ADMIN);
    2608           0 :                 if (IS_ERR(net)) {
    2609           0 :                         err = PTR_ERR(net);
    2610           0 :                         goto errout;
    2611             :                 }
    2612             : 
    2613           0 :                 err = dev_change_net_namespace(dev, net, ifname);
    2614           0 :                 put_net(net);
    2615           0 :                 if (err)
    2616           0 :                         goto errout;
    2617           0 :                 status |= DO_SETLINK_MODIFIED;
    2618             :         }
    2619             : 
    2620           3 :         if (tb[IFLA_MAP]) {
    2621           0 :                 struct rtnl_link_ifmap *u_map;
    2622           0 :                 struct ifmap k_map;
    2623             : 
    2624           0 :                 if (!ops->ndo_set_config) {
    2625           0 :                         err = -EOPNOTSUPP;
    2626           0 :                         goto errout;
    2627             :                 }
    2628             : 
    2629           0 :                 if (!netif_device_present(dev)) {
    2630           0 :                         err = -ENODEV;
    2631           0 :                         goto errout;
    2632             :                 }
    2633             : 
    2634           0 :                 u_map = nla_data(tb[IFLA_MAP]);
    2635           0 :                 k_map.mem_start = (unsigned long) u_map->mem_start;
    2636           0 :                 k_map.mem_end = (unsigned long) u_map->mem_end;
    2637           0 :                 k_map.base_addr = (unsigned short) u_map->base_addr;
    2638           0 :                 k_map.irq = (unsigned char) u_map->irq;
    2639           0 :                 k_map.dma = (unsigned char) u_map->dma;
    2640           0 :                 k_map.port = (unsigned char) u_map->port;
    2641             : 
    2642           0 :                 err = ops->ndo_set_config(dev, &k_map);
    2643           0 :                 if (err < 0)
    2644           0 :                         goto errout;
    2645             : 
    2646           0 :                 status |= DO_SETLINK_NOTIFY;
    2647             :         }
    2648             : 
    2649           3 :         if (tb[IFLA_ADDRESS]) {
    2650           0 :                 struct sockaddr *sa;
    2651           0 :                 int len;
    2652             : 
    2653           0 :                 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
    2654             :                                                   sizeof(*sa));
    2655           0 :                 sa = kmalloc(len, GFP_KERNEL);
    2656           0 :                 if (!sa) {
    2657           0 :                         err = -ENOMEM;
    2658           0 :                         goto errout;
    2659             :                 }
    2660           0 :                 sa->sa_family = dev->type;
    2661           0 :                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
    2662           0 :                        dev->addr_len);
    2663           0 :                 err = dev_set_mac_address_user(dev, sa, extack);
    2664           0 :                 kfree(sa);
    2665           0 :                 if (err)
    2666           0 :                         goto errout;
    2667           0 :                 status |= DO_SETLINK_MODIFIED;
    2668             :         }
    2669             : 
    2670           3 :         if (tb[IFLA_MTU]) {
    2671           0 :                 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
    2672           0 :                 if (err < 0)
    2673           0 :                         goto errout;
    2674           0 :                 status |= DO_SETLINK_MODIFIED;
    2675             :         }
    2676             : 
    2677           3 :         if (tb[IFLA_GROUP]) {
    2678           0 :                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
    2679           0 :                 status |= DO_SETLINK_NOTIFY;
    2680             :         }
    2681             : 
    2682             :         /*
    2683             :          * Interface selected by interface index but interface
    2684             :          * name provided implies that a name change has been
    2685             :          * requested.
    2686             :          */
    2687           3 :         if (ifm->ifi_index > 0 && ifname[0]) {
    2688           0 :                 err = dev_change_name(dev, ifname);
    2689           0 :                 if (err < 0)
    2690           0 :                         goto errout;
    2691           0 :                 status |= DO_SETLINK_MODIFIED;
    2692             :         }
    2693             : 
    2694           3 :         if (tb[IFLA_IFALIAS]) {
    2695           0 :                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
    2696           0 :                                     nla_len(tb[IFLA_IFALIAS]));
    2697           0 :                 if (err < 0)
    2698           0 :                         goto errout;
    2699           0 :                 status |= DO_SETLINK_NOTIFY;
    2700             :         }
    2701             : 
    2702           3 :         if (tb[IFLA_BROADCAST]) {
    2703           0 :                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
    2704           0 :                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
    2705             :         }
    2706             : 
    2707           3 :         if (ifm->ifi_flags || ifm->ifi_change) {
    2708           6 :                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
    2709             :                                        extack);
    2710           3 :                 if (err < 0)
    2711           0 :                         goto errout;
    2712             :         }
    2713             : 
    2714           3 :         if (tb[IFLA_MASTER]) {
    2715           0 :                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
    2716           0 :                 if (err)
    2717           0 :                         goto errout;
    2718           0 :                 status |= DO_SETLINK_MODIFIED;
    2719             :         }
    2720             : 
    2721           3 :         if (tb[IFLA_CARRIER]) {
    2722           0 :                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
    2723           0 :                 if (err)
    2724           0 :                         goto errout;
    2725           0 :                 status |= DO_SETLINK_MODIFIED;
    2726             :         }
    2727             : 
    2728           3 :         if (tb[IFLA_TXQLEN]) {
    2729           0 :                 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
    2730             : 
    2731           0 :                 err = dev_change_tx_queue_len(dev, value);
    2732           0 :                 if (err)
    2733           0 :                         goto errout;
    2734           0 :                 status |= DO_SETLINK_MODIFIED;
    2735             :         }
    2736             : 
    2737           3 :         if (tb[IFLA_GSO_MAX_SIZE]) {
    2738           0 :                 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
    2739             : 
    2740           0 :                 if (max_size > GSO_MAX_SIZE) {
    2741           0 :                         err = -EINVAL;
    2742           0 :                         goto errout;
    2743             :                 }
    2744             : 
    2745           0 :                 if (dev->gso_max_size ^ max_size) {
    2746           0 :                         netif_set_gso_max_size(dev, max_size);
    2747           0 :                         status |= DO_SETLINK_MODIFIED;
    2748             :                 }
    2749             :         }
    2750             : 
    2751           3 :         if (tb[IFLA_GSO_MAX_SEGS]) {
    2752           0 :                 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
    2753             : 
    2754           0 :                 if (max_segs > GSO_MAX_SEGS) {
    2755           0 :                         err = -EINVAL;
    2756           0 :                         goto errout;
    2757             :                 }
    2758             : 
    2759           0 :                 if (dev->gso_max_segs ^ max_segs) {
    2760           0 :                         dev->gso_max_segs = max_segs;
    2761           0 :                         status |= DO_SETLINK_MODIFIED;
    2762             :                 }
    2763             :         }
    2764             : 
    2765           3 :         if (tb[IFLA_OPERSTATE])
    2766           0 :                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
    2767             : 
    2768           3 :         if (tb[IFLA_LINKMODE]) {
    2769           0 :                 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
    2770             : 
    2771           0 :                 write_lock_bh(&dev_base_lock);
    2772           0 :                 if (dev->link_mode ^ value)
    2773           0 :                         status |= DO_SETLINK_NOTIFY;
    2774           0 :                 dev->link_mode = value;
    2775           0 :                 write_unlock_bh(&dev_base_lock);
    2776             :         }
    2777             : 
    2778           3 :         if (tb[IFLA_VFINFO_LIST]) {
    2779           0 :                 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
    2780           0 :                 struct nlattr *attr;
    2781           0 :                 int rem;
    2782             : 
    2783           0 :                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
    2784           0 :                         if (nla_type(attr) != IFLA_VF_INFO ||
    2785           0 :                             nla_len(attr) < NLA_HDRLEN) {
    2786           0 :                                 err = -EINVAL;
    2787           0 :                                 goto errout;
    2788             :                         }
    2789           0 :                         err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
    2790             :                                                           attr,
    2791             :                                                           ifla_vf_policy,
    2792             :                                                           NULL);
    2793           0 :                         if (err < 0)
    2794           0 :                                 goto errout;
    2795           0 :                         err = do_setvfinfo(dev, vfinfo);
    2796           0 :                         if (err < 0)
    2797           0 :                                 goto errout;
    2798           0 :                         status |= DO_SETLINK_NOTIFY;
    2799             :                 }
    2800             :         }
    2801           3 :         err = 0;
    2802             : 
    2803           3 :         if (tb[IFLA_VF_PORTS]) {
    2804           0 :                 struct nlattr *port[IFLA_PORT_MAX+1];
    2805           0 :                 struct nlattr *attr;
    2806           0 :                 int vf;
    2807           0 :                 int rem;
    2808             : 
    2809           0 :                 err = -EOPNOTSUPP;
    2810           0 :                 if (!ops->ndo_set_vf_port)
    2811           0 :                         goto errout;
    2812             : 
    2813           0 :                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
    2814           0 :                         if (nla_type(attr) != IFLA_VF_PORT ||
    2815           0 :                             nla_len(attr) < NLA_HDRLEN) {
    2816           0 :                                 err = -EINVAL;
    2817           0 :                                 goto errout;
    2818             :                         }
    2819           0 :                         err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
    2820             :                                                           attr,
    2821             :                                                           ifla_port_policy,
    2822             :                                                           NULL);
    2823           0 :                         if (err < 0)
    2824           0 :                                 goto errout;
    2825           0 :                         if (!port[IFLA_PORT_VF]) {
    2826           0 :                                 err = -EOPNOTSUPP;
    2827           0 :                                 goto errout;
    2828             :                         }
    2829           0 :                         vf = nla_get_u32(port[IFLA_PORT_VF]);
    2830           0 :                         err = ops->ndo_set_vf_port(dev, vf, port);
    2831           0 :                         if (err < 0)
    2832           0 :                                 goto errout;
    2833           0 :                         status |= DO_SETLINK_NOTIFY;
    2834             :                 }
    2835             :         }
    2836           3 :         err = 0;
    2837             : 
    2838           3 :         if (tb[IFLA_PORT_SELF]) {
    2839           0 :                 struct nlattr *port[IFLA_PORT_MAX+1];
    2840             : 
    2841           0 :                 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
    2842             :                                                   tb[IFLA_PORT_SELF],
    2843             :                                                   ifla_port_policy, NULL);
    2844           0 :                 if (err < 0)
    2845           0 :                         goto errout;
    2846             : 
    2847           0 :                 err = -EOPNOTSUPP;
    2848           0 :                 if (ops->ndo_set_vf_port)
    2849           0 :                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
    2850           0 :                 if (err < 0)
    2851           0 :                         goto errout;
    2852           0 :                 status |= DO_SETLINK_NOTIFY;
    2853             :         }
    2854             : 
    2855           3 :         if (tb[IFLA_AF_SPEC]) {
    2856           0 :                 struct nlattr *af;
    2857           0 :                 int rem;
    2858             : 
    2859           0 :                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
    2860           0 :                         const struct rtnl_af_ops *af_ops;
    2861             : 
    2862           0 :                         rcu_read_lock();
    2863             : 
    2864           0 :                         BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
    2865             : 
    2866           0 :                         err = af_ops->set_link_af(dev, af);
    2867           0 :                         if (err < 0) {
    2868           0 :                                 rcu_read_unlock();
    2869           0 :                                 goto errout;
    2870             :                         }
    2871             : 
    2872           0 :                         rcu_read_unlock();
    2873           0 :                         status |= DO_SETLINK_NOTIFY;
    2874             :                 }
    2875             :         }
    2876           3 :         err = 0;
    2877             : 
    2878           3 :         if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
    2879           0 :                 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
    2880             :                                         tb[IFLA_PROTO_DOWN_REASON], extack);
    2881           0 :                 if (err)
    2882           0 :                         goto errout;
    2883           0 :                 status |= DO_SETLINK_NOTIFY;
    2884             :         }
    2885             : 
    2886           3 :         if (tb[IFLA_XDP]) {
    2887           0 :                 struct nlattr *xdp[IFLA_XDP_MAX + 1];
    2888           0 :                 u32 xdp_flags = 0;
    2889             : 
    2890           0 :                 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
    2891             :                                                   tb[IFLA_XDP],
    2892             :                                                   ifla_xdp_policy, NULL);
    2893           0 :                 if (err < 0)
    2894           0 :                         goto errout;
    2895             : 
    2896           0 :                 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
    2897           0 :                         err = -EINVAL;
    2898           0 :                         goto errout;
    2899             :                 }
    2900             : 
    2901           0 :                 if (xdp[IFLA_XDP_FLAGS]) {
    2902           0 :                         xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
    2903           0 :                         if (xdp_flags & ~XDP_FLAGS_MASK) {
    2904           0 :                                 err = -EINVAL;
    2905           0 :                                 goto errout;
    2906             :                         }
    2907           0 :                         if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
    2908           0 :                                 err = -EINVAL;
    2909           0 :                                 goto errout;
    2910             :                         }
    2911             :                 }
    2912             : 
    2913           0 :                 if (xdp[IFLA_XDP_FD]) {
    2914           0 :                         int expected_fd = -1;
    2915             : 
    2916           0 :                         if (xdp_flags & XDP_FLAGS_REPLACE) {
    2917           0 :                                 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
    2918           0 :                                         err = -EINVAL;
    2919           0 :                                         goto errout;
    2920             :                                 }
    2921           0 :                                 expected_fd =
    2922           0 :                                         nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
    2923             :                         }
    2924             : 
    2925           0 :                         err = dev_change_xdp_fd(dev, extack,
    2926           0 :                                                 nla_get_s32(xdp[IFLA_XDP_FD]),
    2927             :                                                 expected_fd,
    2928             :                                                 xdp_flags);
    2929           0 :                         if (err)
    2930           0 :                                 goto errout;
    2931           0 :                         status |= DO_SETLINK_NOTIFY;
    2932             :                 }
    2933             :         }
    2934             : 
    2935           3 : errout:
    2936           3 :         if (status & DO_SETLINK_MODIFIED) {
    2937           0 :                 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
    2938           0 :                         netdev_state_change(dev);
    2939             : 
    2940           0 :                 if (err < 0)
    2941           0 :                         net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
    2942             :                                              dev->name);
    2943             :         }
    2944             : 
    2945             :         return err;
    2946             : }
    2947             : 
    2948           0 : static struct net_device *rtnl_dev_get(struct net *net,
    2949             :                                        struct nlattr *ifname_attr,
    2950             :                                        struct nlattr *altifname_attr,
    2951             :                                        char *ifname)
    2952             : {
    2953           0 :         char buffer[ALTIFNAMSIZ];
    2954             : 
    2955           0 :         if (!ifname) {
    2956           0 :                 ifname = buffer;
    2957           0 :                 if (ifname_attr)
    2958           0 :                         nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
    2959           0 :                 else if (altifname_attr)
    2960           0 :                         nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
    2961             :                 else
    2962             :                         return NULL;
    2963             :         }
    2964             : 
    2965           0 :         return __dev_get_by_name(net, ifname);
    2966             : }
    2967             : 
    2968           1 : static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
    2969             :                         struct netlink_ext_ack *extack)
    2970             : {
    2971           1 :         struct net *net = sock_net(skb->sk);
    2972           1 :         struct ifinfomsg *ifm;
    2973           1 :         struct net_device *dev;
    2974           1 :         int err;
    2975           1 :         struct nlattr *tb[IFLA_MAX+1];
    2976           1 :         char ifname[IFNAMSIZ];
    2977             : 
    2978           1 :         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
    2979             :                                      ifla_policy, extack);
    2980           1 :         if (err < 0)
    2981           0 :                 goto errout;
    2982             : 
    2983           1 :         err = rtnl_ensure_unique_netns(tb, extack, false);
    2984           1 :         if (err < 0)
    2985           0 :                 goto errout;
    2986             : 
    2987           1 :         if (tb[IFLA_IFNAME])
    2988           0 :                 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    2989             :         else
    2990           1 :                 ifname[0] = '\0';
    2991             : 
    2992           1 :         err = -EINVAL;
    2993           1 :         ifm = nlmsg_data(nlh);
    2994           1 :         if (ifm->ifi_index > 0)
    2995           1 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    2996           0 :         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
    2997           0 :                 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
    2998             :         else
    2999           0 :                 goto errout;
    3000             : 
    3001           1 :         if (dev == NULL) {
    3002           0 :                 err = -ENODEV;
    3003           0 :                 goto errout;
    3004             :         }
    3005             : 
    3006           1 :         err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
    3007           1 : errout:
    3008           1 :         return err;
    3009             : }
    3010             : 
    3011           0 : static int rtnl_group_dellink(const struct net *net, int group)
    3012             : {
    3013           0 :         struct net_device *dev, *aux;
    3014           0 :         LIST_HEAD(list_kill);
    3015           0 :         bool found = false;
    3016             : 
    3017           0 :         if (!group)
    3018             :                 return -EPERM;
    3019             : 
    3020           0 :         for_each_netdev(net, dev) {
    3021           0 :                 if (dev->group == group) {
    3022           0 :                         const struct rtnl_link_ops *ops;
    3023             : 
    3024           0 :                         found = true;
    3025           0 :                         ops = dev->rtnl_link_ops;
    3026           0 :                         if (!ops || !ops->dellink)
    3027             :                                 return -EOPNOTSUPP;
    3028             :                 }
    3029             :         }
    3030             : 
    3031           0 :         if (!found)
    3032             :                 return -ENODEV;
    3033             : 
    3034           0 :         for_each_netdev_safe(net, dev, aux) {
    3035           0 :                 if (dev->group == group) {
    3036           0 :                         const struct rtnl_link_ops *ops;
    3037             : 
    3038           0 :                         ops = dev->rtnl_link_ops;
    3039           0 :                         ops->dellink(dev, &list_kill);
    3040             :                 }
    3041             :         }
    3042           0 :         unregister_netdevice_many(&list_kill);
    3043             : 
    3044           0 :         return 0;
    3045             : }
    3046             : 
    3047           0 : int rtnl_delete_link(struct net_device *dev)
    3048             : {
    3049           0 :         const struct rtnl_link_ops *ops;
    3050           0 :         LIST_HEAD(list_kill);
    3051             : 
    3052           0 :         ops = dev->rtnl_link_ops;
    3053           0 :         if (!ops || !ops->dellink)
    3054             :                 return -EOPNOTSUPP;
    3055             : 
    3056           0 :         ops->dellink(dev, &list_kill);
    3057           0 :         unregister_netdevice_many(&list_kill);
    3058             : 
    3059           0 :         return 0;
    3060             : }
    3061             : EXPORT_SYMBOL_GPL(rtnl_delete_link);
    3062             : 
    3063           0 : static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
    3064             :                         struct netlink_ext_ack *extack)
    3065             : {
    3066           0 :         struct net *net = sock_net(skb->sk);
    3067           0 :         struct net *tgt_net = net;
    3068           0 :         struct net_device *dev = NULL;
    3069           0 :         struct ifinfomsg *ifm;
    3070           0 :         struct nlattr *tb[IFLA_MAX+1];
    3071           0 :         int err;
    3072           0 :         int netnsid = -1;
    3073             : 
    3074           0 :         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
    3075             :                                      ifla_policy, extack);
    3076           0 :         if (err < 0)
    3077             :                 return err;
    3078             : 
    3079           0 :         err = rtnl_ensure_unique_netns(tb, extack, true);
    3080           0 :         if (err < 0)
    3081             :                 return err;
    3082             : 
    3083           0 :         if (tb[IFLA_TARGET_NETNSID]) {
    3084           0 :                 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
    3085           0 :                 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
    3086           0 :                 if (IS_ERR(tgt_net))
    3087           0 :                         return PTR_ERR(tgt_net);
    3088             :         }
    3089             : 
    3090           0 :         err = -EINVAL;
    3091           0 :         ifm = nlmsg_data(nlh);
    3092           0 :         if (ifm->ifi_index > 0)
    3093           0 :                 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
    3094           0 :         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
    3095           0 :                 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
    3096             :                                    tb[IFLA_ALT_IFNAME], NULL);
    3097           0 :         else if (tb[IFLA_GROUP])
    3098           0 :                 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
    3099             :         else
    3100           0 :                 goto out;
    3101             : 
    3102           0 :         if (!dev) {
    3103           0 :                 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
    3104           0 :                         err = -ENODEV;
    3105             : 
    3106           0 :                 goto out;
    3107             :         }
    3108             : 
    3109           0 :         err = rtnl_delete_link(dev);
    3110             : 
    3111             : out:
    3112             :         if (netnsid >= 0)
    3113           0 :                 put_net(tgt_net);
    3114             : 
    3115             :         return err;
    3116             : }
    3117             : 
    3118           0 : int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
    3119             : {
    3120           0 :         unsigned int old_flags;
    3121           0 :         int err;
    3122             : 
    3123           0 :         old_flags = dev->flags;
    3124           0 :         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
    3125           0 :                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
    3126             :                                          NULL);
    3127           0 :                 if (err < 0)
    3128             :                         return err;
    3129             :         }
    3130             : 
    3131           0 :         if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
    3132           0 :                 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
    3133             :         } else {
    3134           0 :                 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
    3135           0 :                 __dev_notify_flags(dev, old_flags, ~0U);
    3136             :         }
    3137             :         return 0;
    3138             : }
    3139             : EXPORT_SYMBOL(rtnl_configure_link);
    3140             : 
    3141           0 : struct net_device *rtnl_create_link(struct net *net, const char *ifname,
    3142             :                                     unsigned char name_assign_type,
    3143             :                                     const struct rtnl_link_ops *ops,
    3144             :                                     struct nlattr *tb[],
    3145             :                                     struct netlink_ext_ack *extack)
    3146             : {
    3147           0 :         struct net_device *dev;
    3148           0 :         unsigned int num_tx_queues = 1;
    3149           0 :         unsigned int num_rx_queues = 1;
    3150             : 
    3151           0 :         if (tb[IFLA_NUM_TX_QUEUES])
    3152           0 :                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
    3153           0 :         else if (ops->get_num_tx_queues)
    3154           0 :                 num_tx_queues = ops->get_num_tx_queues();
    3155             : 
    3156           0 :         if (tb[IFLA_NUM_RX_QUEUES])
    3157           0 :                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
    3158           0 :         else if (ops->get_num_rx_queues)
    3159           0 :                 num_rx_queues = ops->get_num_rx_queues();
    3160             : 
    3161           0 :         if (num_tx_queues < 1 || num_tx_queues > 4096) {
    3162           0 :                 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
    3163           0 :                 return ERR_PTR(-EINVAL);
    3164             :         }
    3165             : 
    3166           0 :         if (num_rx_queues < 1 || num_rx_queues > 4096) {
    3167           0 :                 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
    3168           0 :                 return ERR_PTR(-EINVAL);
    3169             :         }
    3170             : 
    3171           0 :         dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
    3172             :                                ops->setup, num_tx_queues, num_rx_queues);
    3173           0 :         if (!dev)
    3174           0 :                 return ERR_PTR(-ENOMEM);
    3175             : 
    3176           0 :         dev_net_set(dev, net);
    3177           0 :         dev->rtnl_link_ops = ops;
    3178           0 :         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
    3179             : 
    3180           0 :         if (tb[IFLA_MTU]) {
    3181           0 :                 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
    3182           0 :                 int err;
    3183             : 
    3184           0 :                 err = dev_validate_mtu(dev, mtu, extack);
    3185           0 :                 if (err) {
    3186           0 :                         free_netdev(dev);
    3187           0 :                         return ERR_PTR(err);
    3188             :                 }
    3189           0 :                 dev->mtu = mtu;
    3190             :         }
    3191           0 :         if (tb[IFLA_ADDRESS]) {
    3192           0 :                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
    3193           0 :                                 nla_len(tb[IFLA_ADDRESS]));
    3194           0 :                 dev->addr_assign_type = NET_ADDR_SET;
    3195             :         }
    3196           0 :         if (tb[IFLA_BROADCAST])
    3197           0 :                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
    3198           0 :                                 nla_len(tb[IFLA_BROADCAST]));
    3199           0 :         if (tb[IFLA_TXQLEN])
    3200           0 :                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
    3201           0 :         if (tb[IFLA_OPERSTATE])
    3202           0 :                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
    3203           0 :         if (tb[IFLA_LINKMODE])
    3204           0 :                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
    3205           0 :         if (tb[IFLA_GROUP])
    3206           0 :                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
    3207           0 :         if (tb[IFLA_GSO_MAX_SIZE])
    3208           0 :                 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
    3209           0 :         if (tb[IFLA_GSO_MAX_SEGS])
    3210           0 :                 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
    3211             : 
    3212             :         return dev;
    3213             : }
    3214             : EXPORT_SYMBOL(rtnl_create_link);
    3215             : 
    3216           0 : static int rtnl_group_changelink(const struct sk_buff *skb,
    3217             :                 struct net *net, int group,
    3218             :                 struct ifinfomsg *ifm,
    3219             :                 struct netlink_ext_ack *extack,
    3220             :                 struct nlattr **tb)
    3221             : {
    3222           0 :         struct net_device *dev, *aux;
    3223           0 :         int err;
    3224             : 
    3225           0 :         for_each_netdev_safe(net, dev, aux) {
    3226           0 :                 if (dev->group == group) {
    3227           0 :                         err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
    3228           0 :                         if (err < 0)
    3229           0 :                                 return err;
    3230             :                 }
    3231             :         }
    3232             : 
    3233             :         return 0;
    3234             : }
    3235             : 
    3236           4 : static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
    3237             :                           struct nlattr **attr, struct netlink_ext_ack *extack)
    3238             : {
    3239           4 :         struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
    3240           4 :         unsigned char name_assign_type = NET_NAME_USER;
    3241           4 :         struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
    3242           4 :         const struct rtnl_link_ops *m_ops = NULL;
    3243           4 :         struct net_device *master_dev = NULL;
    3244           4 :         struct net *net = sock_net(skb->sk);
    3245           4 :         const struct rtnl_link_ops *ops;
    3246           4 :         struct nlattr *tb[IFLA_MAX + 1];
    3247           4 :         struct net *dest_net, *link_net;
    3248           4 :         struct nlattr **slave_data;
    3249           4 :         char kind[MODULE_NAME_LEN];
    3250           4 :         struct net_device *dev;
    3251           4 :         struct ifinfomsg *ifm;
    3252           4 :         char ifname[IFNAMSIZ];
    3253           4 :         struct nlattr **data;
    3254           4 :         int err;
    3255             : 
    3256             : #ifdef CONFIG_MODULES
    3257             : replay:
    3258             : #endif
    3259           4 :         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
    3260             :                                      ifla_policy, extack);
    3261           4 :         if (err < 0)
    3262             :                 return err;
    3263             : 
    3264           4 :         err = rtnl_ensure_unique_netns(tb, extack, false);
    3265           4 :         if (err < 0)
    3266             :                 return err;
    3267             : 
    3268           4 :         if (tb[IFLA_IFNAME])
    3269           0 :                 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    3270             :         else
    3271           4 :                 ifname[0] = '\0';
    3272             : 
    3273           4 :         ifm = nlmsg_data(nlh);
    3274           4 :         if (ifm->ifi_index > 0)
    3275           2 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    3276           2 :         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
    3277           0 :                 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
    3278             :         else
    3279             :                 dev = NULL;
    3280             : 
    3281           2 :         if (dev) {
    3282           2 :                 master_dev = netdev_master_upper_dev_get(dev);
    3283           2 :                 if (master_dev)
    3284           0 :                         m_ops = master_dev->rtnl_link_ops;
    3285             :         }
    3286             : 
    3287           4 :         err = validate_linkmsg(dev, tb);
    3288           4 :         if (err < 0)
    3289             :                 return err;
    3290             : 
    3291           4 :         if (tb[IFLA_LINKINFO]) {
    3292           0 :                 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
    3293             :                                                   tb[IFLA_LINKINFO],
    3294             :                                                   ifla_info_policy, NULL);
    3295           0 :                 if (err < 0)
    3296             :                         return err;
    3297             :         } else
    3298           4 :                 memset(linkinfo, 0, sizeof(linkinfo));
    3299             : 
    3300           4 :         if (linkinfo[IFLA_INFO_KIND]) {
    3301           0 :                 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
    3302           0 :                 ops = rtnl_link_ops_get(kind);
    3303             :         } else {
    3304           4 :                 kind[0] = '\0';
    3305           4 :                 ops = NULL;
    3306             :         }
    3307             : 
    3308           4 :         data = NULL;
    3309           4 :         if (ops) {
    3310           0 :                 if (ops->maxtype > RTNL_MAX_TYPE)
    3311             :                         return -EINVAL;
    3312             : 
    3313           0 :                 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
    3314           0 :                         err = nla_parse_nested_deprecated(attr, ops->maxtype,
    3315             :                                                           linkinfo[IFLA_INFO_DATA],
    3316             :                                                           ops->policy, extack);
    3317           0 :                         if (err < 0)
    3318             :                                 return err;
    3319             :                         data = attr;
    3320             :                 }
    3321           0 :                 if (ops->validate) {
    3322           0 :                         err = ops->validate(tb, data, extack);
    3323           0 :                         if (err < 0)
    3324             :                                 return err;
    3325             :                 }
    3326             :         }
    3327             : 
    3328           4 :         slave_data = NULL;
    3329           4 :         if (m_ops) {
    3330           0 :                 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
    3331             :                         return -EINVAL;
    3332             : 
    3333           0 :                 if (m_ops->slave_maxtype &&
    3334           0 :                     linkinfo[IFLA_INFO_SLAVE_DATA]) {
    3335           0 :                         err = nla_parse_nested_deprecated(slave_attr,
    3336             :                                                           m_ops->slave_maxtype,
    3337             :                                                           linkinfo[IFLA_INFO_SLAVE_DATA],
    3338             :                                                           m_ops->slave_policy,
    3339             :                                                           extack);
    3340           0 :                         if (err < 0)
    3341             :                                 return err;
    3342             :                         slave_data = slave_attr;
    3343             :                 }
    3344             :         }
    3345             : 
    3346           4 :         if (dev) {
    3347           2 :                 int status = 0;
    3348             : 
    3349           2 :                 if (nlh->nlmsg_flags & NLM_F_EXCL)
    3350             :                         return -EEXIST;
    3351           2 :                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
    3352             :                         return -EOPNOTSUPP;
    3353             : 
    3354           2 :                 if (linkinfo[IFLA_INFO_DATA]) {
    3355           0 :                         if (!ops || ops != dev->rtnl_link_ops ||
    3356           0 :                             !ops->changelink)
    3357             :                                 return -EOPNOTSUPP;
    3358             : 
    3359           0 :                         err = ops->changelink(dev, tb, data, extack);
    3360           0 :                         if (err < 0)
    3361             :                                 return err;
    3362             :                         status |= DO_SETLINK_NOTIFY;
    3363             :                 }
    3364             : 
    3365           2 :                 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
    3366           0 :                         if (!m_ops || !m_ops->slave_changelink)
    3367             :                                 return -EOPNOTSUPP;
    3368             : 
    3369           0 :                         err = m_ops->slave_changelink(master_dev, dev, tb,
    3370             :                                                       slave_data, extack);
    3371           0 :                         if (err < 0)
    3372             :                                 return err;
    3373             :                         status |= DO_SETLINK_NOTIFY;
    3374             :                 }
    3375             : 
    3376           2 :                 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
    3377             :         }
    3378             : 
    3379           2 :         if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
    3380           2 :                 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
    3381           0 :                         return rtnl_group_changelink(skb, net,
    3382           0 :                                                 nla_get_u32(tb[IFLA_GROUP]),
    3383             :                                                 ifm, extack, tb);
    3384             :                 return -ENODEV;
    3385             :         }
    3386             : 
    3387           0 :         if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
    3388             :                 return -EOPNOTSUPP;
    3389             : 
    3390           0 :         if (!ops) {
    3391             : #ifdef CONFIG_MODULES
    3392             :                 if (kind[0]) {
    3393             :                         __rtnl_unlock();
    3394             :                         request_module("rtnl-link-%s", kind);
    3395             :                         rtnl_lock();
    3396             :                         ops = rtnl_link_ops_get(kind);
    3397             :                         if (ops)
    3398             :                                 goto replay;
    3399             :                 }
    3400             : #endif
    3401           0 :                 NL_SET_ERR_MSG(extack, "Unknown device type");
    3402           0 :                 return -EOPNOTSUPP;
    3403             :         }
    3404             : 
    3405           0 :         if (!ops->setup)
    3406             :                 return -EOPNOTSUPP;
    3407             : 
    3408           0 :         if (!ifname[0]) {
    3409           0 :                 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
    3410           0 :                 name_assign_type = NET_NAME_ENUM;
    3411             :         }
    3412             : 
    3413           0 :         dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
    3414           0 :         if (IS_ERR(dest_net))
    3415           0 :                 return PTR_ERR(dest_net);
    3416             : 
    3417           0 :         if (tb[IFLA_LINK_NETNSID]) {
    3418           0 :                 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
    3419             : 
    3420           0 :                 link_net = get_net_ns_by_id(dest_net, id);
    3421           0 :                 if (!link_net) {
    3422           0 :                         NL_SET_ERR_MSG(extack, "Unknown network namespace id");
    3423           0 :                         err =  -EINVAL;
    3424           0 :                         goto out;
    3425             :                 }
    3426           0 :                 err = -EPERM;
    3427           0 :                 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
    3428           0 :                         goto out;
    3429             :         } else {
    3430             :                 link_net = NULL;
    3431             :         }
    3432             : 
    3433           0 :         dev = rtnl_create_link(link_net ? : dest_net, ifname,
    3434             :                                name_assign_type, ops, tb, extack);
    3435           0 :         if (IS_ERR(dev)) {
    3436           0 :                 err = PTR_ERR(dev);
    3437           0 :                 goto out;
    3438             :         }
    3439             : 
    3440           0 :         dev->ifindex = ifm->ifi_index;
    3441             : 
    3442           0 :         if (ops->newlink)
    3443           0 :                 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
    3444             :         else
    3445           0 :                 err = register_netdevice(dev);
    3446           0 :         if (err < 0) {
    3447           0 :                 free_netdev(dev);
    3448           0 :                 goto out;
    3449             :         }
    3450             : 
    3451           0 :         err = rtnl_configure_link(dev, ifm);
    3452           0 :         if (err < 0)
    3453           0 :                 goto out_unregister;
    3454           0 :         if (link_net) {
    3455           0 :                 err = dev_change_net_namespace(dev, dest_net, ifname);
    3456           0 :                 if (err < 0)
    3457           0 :                         goto out_unregister;
    3458             :         }
    3459           0 :         if (tb[IFLA_MASTER]) {
    3460           0 :                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
    3461           0 :                 if (err)
    3462           0 :                         goto out_unregister;
    3463             :         }
    3464           0 : out:
    3465             :         if (link_net)
    3466           4 :                 put_net(link_net);
    3467           4 :         put_net(dest_net);
    3468             :         return err;
    3469           0 : out_unregister:
    3470           0 :         if (ops->newlink) {
    3471           0 :                 LIST_HEAD(list_kill);
    3472             : 
    3473           0 :                 ops->dellink(dev, &list_kill);
    3474           0 :                 unregister_netdevice_many(&list_kill);
    3475             :         } else {
    3476           0 :                 unregister_netdevice(dev);
    3477             :         }
    3478           0 :         goto out;
    3479             : }
    3480             : 
    3481           4 : static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
    3482             :                         struct netlink_ext_ack *extack)
    3483             : {
    3484           4 :         struct nlattr **attr;
    3485           4 :         int ret;
    3486             : 
    3487           4 :         attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
    3488           4 :         if (!attr)
    3489             :                 return -ENOMEM;
    3490             : 
    3491           4 :         ret = __rtnl_newlink(skb, nlh, attr, extack);
    3492           4 :         kfree(attr);
    3493           4 :         return ret;
    3494             : }
    3495             : 
    3496           0 : static int rtnl_valid_getlink_req(struct sk_buff *skb,
    3497             :                                   const struct nlmsghdr *nlh,
    3498             :                                   struct nlattr **tb,
    3499             :                                   struct netlink_ext_ack *extack)
    3500             : {
    3501           0 :         struct ifinfomsg *ifm;
    3502           0 :         int i, err;
    3503             : 
    3504           0 :         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
    3505           0 :                 NL_SET_ERR_MSG(extack, "Invalid header for get link");
    3506           0 :                 return -EINVAL;
    3507             :         }
    3508             : 
    3509           0 :         if (!netlink_strict_get_check(skb))
    3510           0 :                 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
    3511             :                                               ifla_policy, extack);
    3512             : 
    3513           0 :         ifm = nlmsg_data(nlh);
    3514           0 :         if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
    3515           0 :             ifm->ifi_change) {
    3516           0 :                 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
    3517           0 :                 return -EINVAL;
    3518             :         }
    3519             : 
    3520           0 :         err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
    3521             :                                             ifla_policy, extack);
    3522           0 :         if (err)
    3523             :                 return err;
    3524             : 
    3525           0 :         for (i = 0; i <= IFLA_MAX; i++) {
    3526           0 :                 if (!tb[i])
    3527           0 :                         continue;
    3528             : 
    3529           0 :                 switch (i) {
    3530             :                 case IFLA_IFNAME:
    3531             :                 case IFLA_ALT_IFNAME:
    3532             :                 case IFLA_EXT_MASK:
    3533             :                 case IFLA_TARGET_NETNSID:
    3534             :                         break;
    3535           0 :                 default:
    3536           0 :                         NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
    3537             :                         return -EINVAL;
    3538             :                 }
    3539             :         }
    3540             : 
    3541             :         return 0;
    3542             : }
    3543             : 
    3544           0 : static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
    3545             :                         struct netlink_ext_ack *extack)
    3546             : {
    3547           0 :         struct net *net = sock_net(skb->sk);
    3548           0 :         struct net *tgt_net = net;
    3549           0 :         struct ifinfomsg *ifm;
    3550           0 :         struct nlattr *tb[IFLA_MAX+1];
    3551           0 :         struct net_device *dev = NULL;
    3552           0 :         struct sk_buff *nskb;
    3553           0 :         int netnsid = -1;
    3554           0 :         int err;
    3555           0 :         u32 ext_filter_mask = 0;
    3556             : 
    3557           0 :         err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
    3558           0 :         if (err < 0)
    3559             :                 return err;
    3560             : 
    3561           0 :         err = rtnl_ensure_unique_netns(tb, extack, true);
    3562           0 :         if (err < 0)
    3563             :                 return err;
    3564             : 
    3565           0 :         if (tb[IFLA_TARGET_NETNSID]) {
    3566           0 :                 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
    3567           0 :                 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
    3568           0 :                 if (IS_ERR(tgt_net))
    3569           0 :                         return PTR_ERR(tgt_net);
    3570             :         }
    3571             : 
    3572           0 :         if (tb[IFLA_EXT_MASK])
    3573           0 :                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
    3574             : 
    3575           0 :         err = -EINVAL;
    3576           0 :         ifm = nlmsg_data(nlh);
    3577           0 :         if (ifm->ifi_index > 0)
    3578           0 :                 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
    3579           0 :         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
    3580           0 :                 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
    3581             :                                    tb[IFLA_ALT_IFNAME], NULL);
    3582             :         else
    3583           0 :                 goto out;
    3584             : 
    3585           0 :         err = -ENODEV;
    3586           0 :         if (dev == NULL)
    3587           0 :                 goto out;
    3588             : 
    3589           0 :         err = -ENOBUFS;
    3590           0 :         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
    3591           0 :         if (nskb == NULL)
    3592           0 :                 goto out;
    3593             : 
    3594           0 :         err = rtnl_fill_ifinfo(nskb, dev, net,
    3595             :                                RTM_NEWLINK, NETLINK_CB(skb).portid,
    3596             :                                nlh->nlmsg_seq, 0, 0, ext_filter_mask,
    3597             :                                0, NULL, 0, netnsid, GFP_KERNEL);
    3598           0 :         if (err < 0) {
    3599             :                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
    3600           0 :                 WARN_ON(err == -EMSGSIZE);
    3601           0 :                 kfree_skb(nskb);
    3602             :         } else
    3603           0 :                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
    3604             : out:
    3605             :         if (netnsid >= 0)
    3606           0 :                 put_net(tgt_net);
    3607             : 
    3608             :         return err;
    3609             : }
    3610             : 
    3611           0 : static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
    3612             :                            bool *changed, struct netlink_ext_ack *extack)
    3613             : {
    3614           0 :         char *alt_ifname;
    3615           0 :         int err;
    3616             : 
    3617           0 :         err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
    3618           0 :         if (err)
    3619             :                 return err;
    3620             : 
    3621           0 :         alt_ifname = nla_strdup(attr, GFP_KERNEL);
    3622           0 :         if (!alt_ifname)
    3623             :                 return -ENOMEM;
    3624             : 
    3625           0 :         if (cmd == RTM_NEWLINKPROP) {
    3626           0 :                 err = netdev_name_node_alt_create(dev, alt_ifname);
    3627           0 :                 if (!err)
    3628           0 :                         alt_ifname = NULL;
    3629           0 :         } else if (cmd == RTM_DELLINKPROP) {
    3630           0 :                 err = netdev_name_node_alt_destroy(dev, alt_ifname);
    3631             :         } else {
    3632           0 :                 WARN_ON_ONCE(1);
    3633           0 :                 err = -EINVAL;
    3634             :         }
    3635             : 
    3636           0 :         kfree(alt_ifname);
    3637           0 :         if (!err)
    3638           0 :                 *changed = true;
    3639             :         return err;
    3640             : }
    3641             : 
    3642           0 : static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
    3643             :                          struct netlink_ext_ack *extack)
    3644             : {
    3645           0 :         struct net *net = sock_net(skb->sk);
    3646           0 :         struct nlattr *tb[IFLA_MAX + 1];
    3647           0 :         struct net_device *dev;
    3648           0 :         struct ifinfomsg *ifm;
    3649           0 :         bool changed = false;
    3650           0 :         struct nlattr *attr;
    3651           0 :         int err, rem;
    3652             : 
    3653           0 :         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
    3654           0 :         if (err)
    3655             :                 return err;
    3656             : 
    3657           0 :         err = rtnl_ensure_unique_netns(tb, extack, true);
    3658           0 :         if (err)
    3659             :                 return err;
    3660             : 
    3661           0 :         ifm = nlmsg_data(nlh);
    3662           0 :         if (ifm->ifi_index > 0)
    3663           0 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    3664           0 :         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
    3665           0 :                 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
    3666             :                                    tb[IFLA_ALT_IFNAME], NULL);
    3667             :         else
    3668             :                 return -EINVAL;
    3669             : 
    3670           0 :         if (!dev)
    3671             :                 return -ENODEV;
    3672             : 
    3673           0 :         if (!tb[IFLA_PROP_LIST])
    3674             :                 return 0;
    3675             : 
    3676           0 :         nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
    3677           0 :                 switch (nla_type(attr)) {
    3678           0 :                 case IFLA_ALT_IFNAME:
    3679           0 :                         err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
    3680           0 :                         if (err)
    3681           0 :                                 return err;
    3682             :                         break;
    3683             :                 }
    3684           0 :         }
    3685             : 
    3686           0 :         if (changed)
    3687           0 :                 netdev_state_change(dev);
    3688             :         return 0;
    3689             : }
    3690             : 
    3691           0 : static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
    3692             :                             struct netlink_ext_ack *extack)
    3693             : {
    3694           0 :         return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
    3695             : }
    3696             : 
    3697           0 : static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
    3698             :                             struct netlink_ext_ack *extack)
    3699             : {
    3700           0 :         return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
    3701             : }
    3702             : 
    3703           6 : static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
    3704             : {
    3705           6 :         struct net *net = sock_net(skb->sk);
    3706           6 :         size_t min_ifinfo_dump_size = 0;
    3707           6 :         struct nlattr *tb[IFLA_MAX+1];
    3708           6 :         u32 ext_filter_mask = 0;
    3709           6 :         struct net_device *dev;
    3710           6 :         int hdrlen;
    3711             : 
    3712             :         /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
    3713           6 :         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
    3714             :                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
    3715             : 
    3716           6 :         if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
    3717           6 :                 if (tb[IFLA_EXT_MASK])
    3718           0 :                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
    3719             :         }
    3720             : 
    3721           0 :         if (!ext_filter_mask)
    3722             :                 return NLMSG_GOODSIZE;
    3723             :         /*
    3724             :          * traverse the list of net devices and compute the minimum
    3725             :          * buffer size based upon the filter mask.
    3726             :          */
    3727           0 :         rcu_read_lock();
    3728           0 :         for_each_netdev_rcu(net, dev) {
    3729           0 :                 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
    3730             :                                            if_nlmsg_size(dev, ext_filter_mask));
    3731             :         }
    3732           0 :         rcu_read_unlock();
    3733             : 
    3734           0 :         return nlmsg_total_size(min_ifinfo_dump_size);
    3735             : }
    3736             : 
    3737          52 : static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
    3738             : {
    3739          52 :         int idx;
    3740          52 :         int s_idx = cb->family;
    3741          52 :         int type = cb->nlh->nlmsg_type - RTM_BASE;
    3742          52 :         int ret = 0;
    3743             : 
    3744          52 :         if (s_idx == 0)
    3745          26 :                 s_idx = 1;
    3746             : 
    3747        3432 :         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
    3748        3406 :                 struct rtnl_link __rcu **tab;
    3749        3406 :                 struct rtnl_link *link;
    3750        3406 :                 rtnl_dumpit_func dumpit;
    3751             : 
    3752        3406 :                 if (idx < s_idx || idx == PF_PACKET)
    3753          52 :                         continue;
    3754             : 
    3755        3354 :                 if (type < 0 || type >= RTM_NR_MSGTYPES)
    3756           0 :                         continue;
    3757             : 
    3758        6708 :                 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
    3759        3354 :                 if (!tab)
    3760        3250 :                         continue;
    3761             : 
    3762         208 :                 link = rcu_dereference_rtnl(tab[type]);
    3763         104 :                 if (!link)
    3764          52 :                         continue;
    3765             : 
    3766          52 :                 dumpit = link->dumpit;
    3767          52 :                 if (!dumpit)
    3768           0 :                         continue;
    3769             : 
    3770          52 :                 if (idx > s_idx) {
    3771          26 :                         memset(&cb->args[0], 0, sizeof(cb->args));
    3772          26 :                         cb->prev_seq = 0;
    3773          26 :                         cb->seq = 0;
    3774             :                 }
    3775          52 :                 ret = dumpit(skb, cb);
    3776          52 :                 if (ret)
    3777             :                         break;
    3778             :         }
    3779          52 :         cb->family = idx;
    3780             : 
    3781          52 :         return skb->len ? : ret;
    3782             : }
    3783             : 
    3784           4 : struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
    3785             :                                        unsigned int change,
    3786             :                                        u32 event, gfp_t flags, int *new_nsid,
    3787             :                                        int new_ifindex)
    3788             : {
    3789           4 :         struct net *net = dev_net(dev);
    3790           4 :         struct sk_buff *skb;
    3791           4 :         int err = -ENOBUFS;
    3792           4 :         size_t if_info_size;
    3793             : 
    3794           4 :         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
    3795           4 :         if (skb == NULL)
    3796           0 :                 goto errout;
    3797             : 
    3798           4 :         err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
    3799             :                                type, 0, 0, change, 0, 0, event,
    3800             :                                new_nsid, new_ifindex, -1, flags);
    3801           4 :         if (err < 0) {
    3802             :                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
    3803           0 :                 WARN_ON(err == -EMSGSIZE);
    3804           0 :                 kfree_skb(skb);
    3805           0 :                 goto errout;
    3806             :         }
    3807             :         return skb;
    3808           0 : errout:
    3809           0 :         if (err < 0)
    3810           0 :                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
    3811           0 :         return NULL;
    3812             : }
    3813             : 
    3814           4 : void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
    3815             : {
    3816           4 :         struct net *net = dev_net(dev);
    3817             : 
    3818           4 :         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
    3819           4 : }
    3820             : 
    3821           4 : static void rtmsg_ifinfo_event(int type, struct net_device *dev,
    3822             :                                unsigned int change, u32 event,
    3823             :                                gfp_t flags, int *new_nsid, int new_ifindex)
    3824             : {
    3825           4 :         struct sk_buff *skb;
    3826             : 
    3827           4 :         if (dev->reg_state != NETREG_REGISTERED)
    3828             :                 return;
    3829             : 
    3830           4 :         skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
    3831             :                                      new_ifindex);
    3832           4 :         if (skb)
    3833           4 :                 rtmsg_ifinfo_send(skb, dev, flags);
    3834             : }
    3835             : 
    3836           4 : void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
    3837             :                   gfp_t flags)
    3838             : {
    3839           4 :         rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
    3840             :                            NULL, 0);
    3841           4 : }
    3842             : 
    3843           0 : void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
    3844             :                          gfp_t flags, int *new_nsid, int new_ifindex)
    3845             : {
    3846           0 :         rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
    3847             :                            new_nsid, new_ifindex);
    3848           0 : }
    3849             : 
    3850           0 : static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
    3851             :                                    struct net_device *dev,
    3852             :                                    u8 *addr, u16 vid, u32 pid, u32 seq,
    3853             :                                    int type, unsigned int flags,
    3854             :                                    int nlflags, u16 ndm_state)
    3855             : {
    3856           0 :         struct nlmsghdr *nlh;
    3857           0 :         struct ndmsg *ndm;
    3858             : 
    3859           0 :         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
    3860           0 :         if (!nlh)
    3861             :                 return -EMSGSIZE;
    3862             : 
    3863           0 :         ndm = nlmsg_data(nlh);
    3864           0 :         ndm->ndm_family  = AF_BRIDGE;
    3865           0 :         ndm->ndm_pad1         = 0;
    3866           0 :         ndm->ndm_pad2    = 0;
    3867           0 :         ndm->ndm_flags        = flags;
    3868           0 :         ndm->ndm_type         = 0;
    3869           0 :         ndm->ndm_ifindex = dev->ifindex;
    3870           0 :         ndm->ndm_state   = ndm_state;
    3871             : 
    3872           0 :         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
    3873           0 :                 goto nla_put_failure;
    3874           0 :         if (vid)
    3875           0 :                 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
    3876           0 :                         goto nla_put_failure;
    3877             : 
    3878           0 :         nlmsg_end(skb, nlh);
    3879           0 :         return 0;
    3880             : 
    3881           0 : nla_put_failure:
    3882           0 :         nlmsg_cancel(skb, nlh);
    3883           0 :         return -EMSGSIZE;
    3884             : }
    3885             : 
    3886           0 : static inline size_t rtnl_fdb_nlmsg_size(void)
    3887             : {
    3888           0 :         return NLMSG_ALIGN(sizeof(struct ndmsg)) +
    3889           0 :                nla_total_size(ETH_ALEN) +       /* NDA_LLADDR */
    3890           0 :                nla_total_size(sizeof(u16)) +    /* NDA_VLAN */
    3891             :                0;
    3892             : }
    3893             : 
    3894           0 : static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
    3895             :                             u16 ndm_state)
    3896             : {
    3897           0 :         struct net *net = dev_net(dev);
    3898           0 :         struct sk_buff *skb;
    3899           0 :         int err = -ENOBUFS;
    3900             : 
    3901           0 :         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
    3902           0 :         if (!skb)
    3903           0 :                 goto errout;
    3904             : 
    3905           0 :         err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
    3906             :                                       0, 0, type, NTF_SELF, 0, ndm_state);
    3907           0 :         if (err < 0) {
    3908           0 :                 kfree_skb(skb);
    3909           0 :                 goto errout;
    3910             :         }
    3911             : 
    3912           0 :         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
    3913             :         return;
    3914           0 : errout:
    3915           0 :         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
    3916             : }
    3917             : 
    3918             : /*
    3919             :  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
    3920             :  */
    3921           0 : int ndo_dflt_fdb_add(struct ndmsg *ndm,
    3922             :                      struct nlattr *tb[],
    3923             :                      struct net_device *dev,
    3924             :                      const unsigned char *addr, u16 vid,
    3925             :                      u16 flags)
    3926             : {
    3927           0 :         int err = -EINVAL;
    3928             : 
    3929             :         /* If aging addresses are supported device will need to
    3930             :          * implement its own handler for this.
    3931             :          */
    3932           0 :         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
    3933           0 :                 pr_info("%s: FDB only supports static addresses\n", dev->name);
    3934           0 :                 return err;
    3935             :         }
    3936             : 
    3937           0 :         if (vid) {
    3938           0 :                 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
    3939           0 :                 return err;
    3940             :         }
    3941             : 
    3942           0 :         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
    3943           0 :                 err = dev_uc_add_excl(dev, addr);
    3944           0 :         else if (is_multicast_ether_addr(addr))
    3945           0 :                 err = dev_mc_add_excl(dev, addr);
    3946             : 
    3947             :         /* Only return duplicate errors if NLM_F_EXCL is set */
    3948           0 :         if (err == -EEXIST && !(flags & NLM_F_EXCL))
    3949           0 :                 err = 0;
    3950             : 
    3951             :         return err;
    3952             : }
    3953             : EXPORT_SYMBOL(ndo_dflt_fdb_add);
    3954             : 
    3955           0 : static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
    3956             :                          struct netlink_ext_ack *extack)
    3957             : {
    3958           0 :         u16 vid = 0;
    3959             : 
    3960           0 :         if (vlan_attr) {
    3961           0 :                 if (nla_len(vlan_attr) != sizeof(u16)) {
    3962           0 :                         NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
    3963           0 :                         return -EINVAL;
    3964             :                 }
    3965             : 
    3966           0 :                 vid = nla_get_u16(vlan_attr);
    3967             : 
    3968           0 :                 if (!vid || vid >= VLAN_VID_MASK) {
    3969           0 :                         NL_SET_ERR_MSG(extack, "invalid vlan id");
    3970           0 :                         return -EINVAL;
    3971             :                 }
    3972             :         }
    3973           0 :         *p_vid = vid;
    3974           0 :         return 0;
    3975             : }
    3976             : 
    3977           0 : static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
    3978             :                         struct netlink_ext_ack *extack)
    3979             : {
    3980           0 :         struct net *net = sock_net(skb->sk);
    3981           0 :         struct ndmsg *ndm;
    3982           0 :         struct nlattr *tb[NDA_MAX+1];
    3983           0 :         struct net_device *dev;
    3984           0 :         u8 *addr;
    3985           0 :         u16 vid;
    3986           0 :         int err;
    3987             : 
    3988           0 :         err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
    3989             :                                      extack);
    3990           0 :         if (err < 0)
    3991             :                 return err;
    3992             : 
    3993           0 :         ndm = nlmsg_data(nlh);
    3994           0 :         if (ndm->ndm_ifindex == 0) {
    3995           0 :                 NL_SET_ERR_MSG(extack, "invalid ifindex");
    3996           0 :                 return -EINVAL;
    3997             :         }
    3998             : 
    3999           0 :         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
    4000           0 :         if (dev == NULL) {
    4001           0 :                 NL_SET_ERR_MSG(extack, "unknown ifindex");
    4002           0 :                 return -ENODEV;
    4003             :         }
    4004             : 
    4005           0 :         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
    4006           0 :                 NL_SET_ERR_MSG(extack, "invalid address");
    4007           0 :                 return -EINVAL;
    4008             :         }
    4009             : 
    4010           0 :         if (dev->type != ARPHRD_ETHER) {
    4011           0 :                 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
    4012           0 :                 return -EINVAL;
    4013             :         }
    4014             : 
    4015           0 :         addr = nla_data(tb[NDA_LLADDR]);
    4016             : 
    4017           0 :         err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
    4018           0 :         if (err)
    4019             :                 return err;
    4020             : 
    4021           0 :         err = -EOPNOTSUPP;
    4022             : 
    4023             :         /* Support fdb on master device the net/bridge default case */
    4024           0 :         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
    4025           0 :             netif_is_bridge_port(dev)) {
    4026           0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4027           0 :                 const struct net_device_ops *ops = br_dev->netdev_ops;
    4028             : 
    4029           0 :                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
    4030           0 :                                        nlh->nlmsg_flags, extack);
    4031           0 :                 if (err)
    4032           0 :                         goto out;
    4033             :                 else
    4034           0 :                         ndm->ndm_flags &= ~NTF_MASTER;
    4035             :         }
    4036             : 
    4037             :         /* Embedded bridge, macvlan, and any other device support */
    4038           0 :         if ((ndm->ndm_flags & NTF_SELF)) {
    4039           0 :                 if (dev->netdev_ops->ndo_fdb_add)
    4040           0 :                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
    4041             :                                                            vid,
    4042           0 :                                                            nlh->nlmsg_flags,
    4043             :                                                            extack);
    4044             :                 else
    4045           0 :                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
    4046           0 :                                                nlh->nlmsg_flags);
    4047             : 
    4048           0 :                 if (!err) {
    4049           0 :                         rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
    4050           0 :                                         ndm->ndm_state);
    4051           0 :                         ndm->ndm_flags &= ~NTF_SELF;
    4052             :                 }
    4053             :         }
    4054           0 : out:
    4055             :         return err;
    4056             : }
    4057             : 
    4058             : /*
    4059             :  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
    4060             :  */
    4061           0 : int ndo_dflt_fdb_del(struct ndmsg *ndm,
    4062             :                      struct nlattr *tb[],
    4063             :                      struct net_device *dev,
    4064             :                      const unsigned char *addr, u16 vid)
    4065             : {
    4066           0 :         int err = -EINVAL;
    4067             : 
    4068             :         /* If aging addresses are supported device will need to
    4069             :          * implement its own handler for this.
    4070             :          */
    4071           0 :         if (!(ndm->ndm_state & NUD_PERMANENT)) {
    4072           0 :                 pr_info("%s: FDB only supports static addresses\n", dev->name);
    4073           0 :                 return err;
    4074             :         }
    4075             : 
    4076           0 :         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
    4077           0 :                 err = dev_uc_del(dev, addr);
    4078           0 :         else if (is_multicast_ether_addr(addr))
    4079           0 :                 err = dev_mc_del(dev, addr);
    4080             : 
    4081             :         return err;
    4082             : }
    4083             : EXPORT_SYMBOL(ndo_dflt_fdb_del);
    4084             : 
    4085           0 : static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
    4086             :                         struct netlink_ext_ack *extack)
    4087             : {
    4088           0 :         struct net *net = sock_net(skb->sk);
    4089           0 :         struct ndmsg *ndm;
    4090           0 :         struct nlattr *tb[NDA_MAX+1];
    4091           0 :         struct net_device *dev;
    4092           0 :         __u8 *addr;
    4093           0 :         int err;
    4094           0 :         u16 vid;
    4095             : 
    4096           0 :         if (!netlink_capable(skb, CAP_NET_ADMIN))
    4097             :                 return -EPERM;
    4098             : 
    4099           0 :         err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
    4100             :                                      extack);
    4101           0 :         if (err < 0)
    4102             :                 return err;
    4103             : 
    4104           0 :         ndm = nlmsg_data(nlh);
    4105           0 :         if (ndm->ndm_ifindex == 0) {
    4106           0 :                 NL_SET_ERR_MSG(extack, "invalid ifindex");
    4107           0 :                 return -EINVAL;
    4108             :         }
    4109             : 
    4110           0 :         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
    4111           0 :         if (dev == NULL) {
    4112           0 :                 NL_SET_ERR_MSG(extack, "unknown ifindex");
    4113           0 :                 return -ENODEV;
    4114             :         }
    4115             : 
    4116           0 :         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
    4117           0 :                 NL_SET_ERR_MSG(extack, "invalid address");
    4118           0 :                 return -EINVAL;
    4119             :         }
    4120             : 
    4121           0 :         if (dev->type != ARPHRD_ETHER) {
    4122           0 :                 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
    4123           0 :                 return -EINVAL;
    4124             :         }
    4125             : 
    4126           0 :         addr = nla_data(tb[NDA_LLADDR]);
    4127             : 
    4128           0 :         err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
    4129           0 :         if (err)
    4130             :                 return err;
    4131             : 
    4132           0 :         err = -EOPNOTSUPP;
    4133             : 
    4134             :         /* Support fdb on master device the net/bridge default case */
    4135           0 :         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
    4136           0 :             netif_is_bridge_port(dev)) {
    4137           0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4138           0 :                 const struct net_device_ops *ops = br_dev->netdev_ops;
    4139             : 
    4140           0 :                 if (ops->ndo_fdb_del)
    4141           0 :                         err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
    4142             : 
    4143           0 :                 if (err)
    4144           0 :                         goto out;
    4145             :                 else
    4146           0 :                         ndm->ndm_flags &= ~NTF_MASTER;
    4147             :         }
    4148             : 
    4149             :         /* Embedded bridge, macvlan, and any other device support */
    4150           0 :         if (ndm->ndm_flags & NTF_SELF) {
    4151           0 :                 if (dev->netdev_ops->ndo_fdb_del)
    4152           0 :                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
    4153             :                                                            vid);
    4154             :                 else
    4155           0 :                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
    4156             : 
    4157           0 :                 if (!err) {
    4158           0 :                         rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
    4159           0 :                                         ndm->ndm_state);
    4160           0 :                         ndm->ndm_flags &= ~NTF_SELF;
    4161             :                 }
    4162             :         }
    4163           0 : out:
    4164             :         return err;
    4165             : }
    4166             : 
    4167           0 : static int nlmsg_populate_fdb(struct sk_buff *skb,
    4168             :                               struct netlink_callback *cb,
    4169             :                               struct net_device *dev,
    4170             :                               int *idx,
    4171             :                               struct netdev_hw_addr_list *list)
    4172             : {
    4173           0 :         struct netdev_hw_addr *ha;
    4174           0 :         int err;
    4175           0 :         u32 portid, seq;
    4176             : 
    4177           0 :         portid = NETLINK_CB(cb->skb).portid;
    4178           0 :         seq = cb->nlh->nlmsg_seq;
    4179             : 
    4180           0 :         list_for_each_entry(ha, &list->list, list) {
    4181           0 :                 if (*idx < cb->args[2])
    4182           0 :                         goto skip;
    4183             : 
    4184           0 :                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
    4185             :                                               portid, seq,
    4186             :                                               RTM_NEWNEIGH, NTF_SELF,
    4187             :                                               NLM_F_MULTI, NUD_PERMANENT);
    4188           0 :                 if (err < 0)
    4189           0 :                         return err;
    4190           0 : skip:
    4191           0 :                 *idx += 1;
    4192             :         }
    4193             :         return 0;
    4194             : }
    4195             : 
    4196             : /**
    4197             :  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
    4198             :  * @skb: socket buffer to store message in
    4199             :  * @cb: netlink callback
    4200             :  * @dev: netdevice
    4201             :  * @filter_dev: ignored
    4202             :  * @idx: the number of FDB table entries dumped is added to *@idx
    4203             :  *
    4204             :  * Default netdevice operation to dump the existing unicast address list.
    4205             :  * Returns number of addresses from list put in skb.
    4206             :  */
    4207           0 : int ndo_dflt_fdb_dump(struct sk_buff *skb,
    4208             :                       struct netlink_callback *cb,
    4209             :                       struct net_device *dev,
    4210             :                       struct net_device *filter_dev,
    4211             :                       int *idx)
    4212             : {
    4213           0 :         int err;
    4214             : 
    4215           0 :         if (dev->type != ARPHRD_ETHER)
    4216             :                 return -EINVAL;
    4217             : 
    4218           0 :         netif_addr_lock_bh(dev);
    4219           0 :         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
    4220           0 :         if (err)
    4221           0 :                 goto out;
    4222           0 :         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
    4223           0 : out:
    4224           0 :         netif_addr_unlock_bh(dev);
    4225           0 :         return err;
    4226             : }
    4227             : EXPORT_SYMBOL(ndo_dflt_fdb_dump);
    4228             : 
    4229           0 : static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
    4230             :                                  int *br_idx, int *brport_idx,
    4231             :                                  struct netlink_ext_ack *extack)
    4232             : {
    4233           0 :         struct nlattr *tb[NDA_MAX + 1];
    4234           0 :         struct ndmsg *ndm;
    4235           0 :         int err, i;
    4236             : 
    4237           0 :         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
    4238           0 :                 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
    4239           0 :                 return -EINVAL;
    4240             :         }
    4241             : 
    4242           0 :         ndm = nlmsg_data(nlh);
    4243           0 :         if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
    4244           0 :             ndm->ndm_flags || ndm->ndm_type) {
    4245           0 :                 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
    4246           0 :                 return -EINVAL;
    4247             :         }
    4248             : 
    4249           0 :         err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
    4250             :                                             NDA_MAX, NULL, extack);
    4251           0 :         if (err < 0)
    4252             :                 return err;
    4253             : 
    4254           0 :         *brport_idx = ndm->ndm_ifindex;
    4255           0 :         for (i = 0; i <= NDA_MAX; ++i) {
    4256           0 :                 if (!tb[i])
    4257           0 :                         continue;
    4258             : 
    4259           0 :                 switch (i) {
    4260           0 :                 case NDA_IFINDEX:
    4261           0 :                         if (nla_len(tb[i]) != sizeof(u32)) {
    4262           0 :                                 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
    4263           0 :                                 return -EINVAL;
    4264             :                         }
    4265           0 :                         *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
    4266           0 :                         break;
    4267           0 :                 case NDA_MASTER:
    4268           0 :                         if (nla_len(tb[i]) != sizeof(u32)) {
    4269           0 :                                 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
    4270           0 :                                 return -EINVAL;
    4271             :                         }
    4272           0 :                         *br_idx = nla_get_u32(tb[NDA_MASTER]);
    4273           0 :                         break;
    4274           0 :                 default:
    4275           0 :                         NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
    4276             :                         return -EINVAL;
    4277             :                 }
    4278             :         }
    4279             : 
    4280             :         return 0;
    4281             : }
    4282             : 
    4283           0 : static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
    4284             :                                  int *br_idx, int *brport_idx,
    4285             :                                  struct netlink_ext_ack *extack)
    4286             : {
    4287           0 :         struct nlattr *tb[IFLA_MAX+1];
    4288           0 :         int err;
    4289             : 
    4290             :         /* A hack to preserve kernel<->userspace interface.
    4291             :          * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
    4292             :          * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
    4293             :          * So, check for ndmsg with an optional u32 attribute (not used here).
    4294             :          * Fortunately these sizes don't conflict with the size of ifinfomsg
    4295             :          * with an optional attribute.
    4296             :          */
    4297           0 :         if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
    4298           0 :             (nlmsg_len(nlh) != sizeof(struct ndmsg) +
    4299           0 :              nla_attr_size(sizeof(u32)))) {
    4300           0 :                 struct ifinfomsg *ifm;
    4301             : 
    4302           0 :                 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
    4303             :                                              tb, IFLA_MAX, ifla_policy,
    4304             :                                              extack);
    4305           0 :                 if (err < 0) {
    4306             :                         return -EINVAL;
    4307           0 :                 } else if (err == 0) {
    4308           0 :                         if (tb[IFLA_MASTER])
    4309           0 :                                 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
    4310             :                 }
    4311             : 
    4312           0 :                 ifm = nlmsg_data(nlh);
    4313           0 :                 *brport_idx = ifm->ifi_index;
    4314             :         }
    4315             :         return 0;
    4316             : }
    4317             : 
    4318           0 : static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
    4319             : {
    4320           0 :         struct net_device *dev;
    4321           0 :         struct net_device *br_dev = NULL;
    4322           0 :         const struct net_device_ops *ops = NULL;
    4323           0 :         const struct net_device_ops *cops = NULL;
    4324           0 :         struct net *net = sock_net(skb->sk);
    4325           0 :         struct hlist_head *head;
    4326           0 :         int brport_idx = 0;
    4327           0 :         int br_idx = 0;
    4328           0 :         int h, s_h;
    4329           0 :         int idx = 0, s_idx;
    4330           0 :         int err = 0;
    4331           0 :         int fidx = 0;
    4332             : 
    4333           0 :         if (cb->strict_check)
    4334           0 :                 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
    4335             :                                             cb->extack);
    4336             :         else
    4337           0 :                 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
    4338             :                                             cb->extack);
    4339           0 :         if (err < 0)
    4340             :                 return err;
    4341             : 
    4342           0 :         if (br_idx) {
    4343           0 :                 br_dev = __dev_get_by_index(net, br_idx);
    4344           0 :                 if (!br_dev)
    4345             :                         return -ENODEV;
    4346             : 
    4347           0 :                 ops = br_dev->netdev_ops;
    4348             :         }
    4349             : 
    4350           0 :         s_h = cb->args[0];
    4351           0 :         s_idx = cb->args[1];
    4352             : 
    4353           0 :         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
    4354           0 :                 idx = 0;
    4355           0 :                 head = &net->dev_index_head[h];
    4356           0 :                 hlist_for_each_entry(dev, head, index_hlist) {
    4357             : 
    4358           0 :                         if (brport_idx && (dev->ifindex != brport_idx))
    4359           0 :                                 continue;
    4360             : 
    4361           0 :                         if (!br_idx) { /* user did not specify a specific bridge */
    4362           0 :                                 if (netif_is_bridge_port(dev)) {
    4363           0 :                                         br_dev = netdev_master_upper_dev_get(dev);
    4364           0 :                                         cops = br_dev->netdev_ops;
    4365             :                                 }
    4366             :                         } else {
    4367           0 :                                 if (dev != br_dev &&
    4368           0 :                                     !netif_is_bridge_port(dev))
    4369           0 :                                         continue;
    4370             : 
    4371           0 :                                 if (br_dev != netdev_master_upper_dev_get(dev) &&
    4372           0 :                                     !(dev->priv_flags & IFF_EBRIDGE))
    4373           0 :                                         continue;
    4374             :                                 cops = ops;
    4375             :                         }
    4376             : 
    4377           0 :                         if (idx < s_idx)
    4378           0 :                                 goto cont;
    4379             : 
    4380           0 :                         if (netif_is_bridge_port(dev)) {
    4381           0 :                                 if (cops && cops->ndo_fdb_dump) {
    4382           0 :                                         err = cops->ndo_fdb_dump(skb, cb,
    4383             :                                                                 br_dev, dev,
    4384             :                                                                 &fidx);
    4385           0 :                                         if (err == -EMSGSIZE)
    4386           0 :                                                 goto out;
    4387             :                                 }
    4388             :                         }
    4389             : 
    4390           0 :                         if (dev->netdev_ops->ndo_fdb_dump)
    4391           0 :                                 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
    4392             :                                                                     dev, NULL,
    4393             :                                                                     &fidx);
    4394             :                         else
    4395           0 :                                 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
    4396             :                                                         &fidx);
    4397           0 :                         if (err == -EMSGSIZE)
    4398           0 :                                 goto out;
    4399             : 
    4400           0 :                         cops = NULL;
    4401             : 
    4402             :                         /* reset fdb offset to 0 for rest of the interfaces */
    4403           0 :                         cb->args[2] = 0;
    4404           0 :                         fidx = 0;
    4405           0 : cont:
    4406           0 :                         idx++;
    4407             :                 }
    4408             :         }
    4409             : 
    4410           0 : out:
    4411           0 :         cb->args[0] = h;
    4412           0 :         cb->args[1] = idx;
    4413           0 :         cb->args[2] = fidx;
    4414             : 
    4415           0 :         return skb->len;
    4416             : }
    4417             : 
    4418           0 : static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
    4419             :                                 struct nlattr **tb, u8 *ndm_flags,
    4420             :                                 int *br_idx, int *brport_idx, u8 **addr,
    4421             :                                 u16 *vid, struct netlink_ext_ack *extack)
    4422             : {
    4423           0 :         struct ndmsg *ndm;
    4424           0 :         int err, i;
    4425             : 
    4426           0 :         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
    4427           0 :                 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
    4428           0 :                 return -EINVAL;
    4429             :         }
    4430             : 
    4431           0 :         ndm = nlmsg_data(nlh);
    4432           0 :         if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
    4433             :             ndm->ndm_type) {
    4434           0 :                 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
    4435           0 :                 return -EINVAL;
    4436             :         }
    4437             : 
    4438           0 :         if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
    4439           0 :                 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
    4440           0 :                 return -EINVAL;
    4441             :         }
    4442             : 
    4443           0 :         err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
    4444             :                                             NDA_MAX, nda_policy, extack);
    4445           0 :         if (err < 0)
    4446             :                 return err;
    4447             : 
    4448           0 :         *ndm_flags = ndm->ndm_flags;
    4449           0 :         *brport_idx = ndm->ndm_ifindex;
    4450           0 :         for (i = 0; i <= NDA_MAX; ++i) {
    4451           0 :                 if (!tb[i])
    4452           0 :                         continue;
    4453             : 
    4454           0 :                 switch (i) {
    4455           0 :                 case NDA_MASTER:
    4456           0 :                         *br_idx = nla_get_u32(tb[i]);
    4457           0 :                         break;
    4458           0 :                 case NDA_LLADDR:
    4459           0 :                         if (nla_len(tb[i]) != ETH_ALEN) {
    4460           0 :                                 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
    4461           0 :                                 return -EINVAL;
    4462             :                         }
    4463           0 :                         *addr = nla_data(tb[i]);
    4464           0 :                         break;
    4465           0 :                 case NDA_VLAN:
    4466           0 :                         err = fdb_vid_parse(tb[i], vid, extack);
    4467           0 :                         if (err)
    4468           0 :                                 return err;
    4469             :                         break;
    4470             :                 case NDA_VNI:
    4471             :                         break;
    4472           0 :                 default:
    4473           0 :                         NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
    4474             :                         return -EINVAL;
    4475             :                 }
    4476             :         }
    4477             : 
    4478             :         return 0;
    4479             : }
    4480             : 
    4481           0 : static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
    4482             :                         struct netlink_ext_ack *extack)
    4483             : {
    4484           0 :         struct net_device *dev = NULL, *br_dev = NULL;
    4485           0 :         const struct net_device_ops *ops = NULL;
    4486           0 :         struct net *net = sock_net(in_skb->sk);
    4487           0 :         struct nlattr *tb[NDA_MAX + 1];
    4488           0 :         struct sk_buff *skb;
    4489           0 :         int brport_idx = 0;
    4490           0 :         u8 ndm_flags = 0;
    4491           0 :         int br_idx = 0;
    4492           0 :         u8 *addr = NULL;
    4493           0 :         u16 vid = 0;
    4494           0 :         int err;
    4495             : 
    4496           0 :         err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
    4497             :                                    &brport_idx, &addr, &vid, extack);
    4498           0 :         if (err < 0)
    4499             :                 return err;
    4500             : 
    4501           0 :         if (!addr) {
    4502           0 :                 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
    4503           0 :                 return -EINVAL;
    4504             :         }
    4505             : 
    4506           0 :         if (brport_idx) {
    4507           0 :                 dev = __dev_get_by_index(net, brport_idx);
    4508           0 :                 if (!dev) {
    4509           0 :                         NL_SET_ERR_MSG(extack, "Unknown device ifindex");
    4510           0 :                         return -ENODEV;
    4511             :                 }
    4512             :         }
    4513             : 
    4514           0 :         if (br_idx) {
    4515           0 :                 if (dev) {
    4516           0 :                         NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
    4517           0 :                         return -EINVAL;
    4518             :                 }
    4519             : 
    4520           0 :                 br_dev = __dev_get_by_index(net, br_idx);
    4521           0 :                 if (!br_dev) {
    4522           0 :                         NL_SET_ERR_MSG(extack, "Invalid master ifindex");
    4523           0 :                         return -EINVAL;
    4524             :                 }
    4525           0 :                 ops = br_dev->netdev_ops;
    4526             :         }
    4527             : 
    4528           0 :         if (dev) {
    4529           0 :                 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
    4530           0 :                         if (!netif_is_bridge_port(dev)) {
    4531           0 :                                 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
    4532           0 :                                 return -EINVAL;
    4533             :                         }
    4534           0 :                         br_dev = netdev_master_upper_dev_get(dev);
    4535           0 :                         if (!br_dev) {
    4536           0 :                                 NL_SET_ERR_MSG(extack, "Master of device not found");
    4537           0 :                                 return -EINVAL;
    4538             :                         }
    4539           0 :                         ops = br_dev->netdev_ops;
    4540             :                 } else {
    4541           0 :                         if (!(ndm_flags & NTF_SELF)) {
    4542           0 :                                 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
    4543           0 :                                 return -EINVAL;
    4544             :                         }
    4545           0 :                         ops = dev->netdev_ops;
    4546             :                 }
    4547             :         }
    4548             : 
    4549           0 :         if (!br_dev && !dev) {
    4550           0 :                 NL_SET_ERR_MSG(extack, "No device specified");
    4551           0 :                 return -ENODEV;
    4552             :         }
    4553             : 
    4554           0 :         if (!ops || !ops->ndo_fdb_get) {
    4555           0 :                 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
    4556           0 :                 return -EOPNOTSUPP;
    4557             :         }
    4558             : 
    4559           0 :         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
    4560           0 :         if (!skb)
    4561             :                 return -ENOBUFS;
    4562             : 
    4563           0 :         if (br_dev)
    4564           0 :                 dev = br_dev;
    4565           0 :         err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
    4566             :                                NETLINK_CB(in_skb).portid,
    4567             :                                nlh->nlmsg_seq, extack);
    4568           0 :         if (err)
    4569           0 :                 goto out;
    4570             : 
    4571           0 :         return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
    4572           0 : out:
    4573           0 :         kfree_skb(skb);
    4574           0 :         return err;
    4575             : }
    4576             : 
    4577           0 : static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
    4578             :                                unsigned int attrnum, unsigned int flag)
    4579             : {
    4580           0 :         if (mask & flag)
    4581           0 :                 return nla_put_u8(skb, attrnum, !!(flags & flag));
    4582             :         return 0;
    4583             : }
    4584             : 
    4585           0 : int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
    4586             :                             struct net_device *dev, u16 mode,
    4587             :                             u32 flags, u32 mask, int nlflags,
    4588             :                             u32 filter_mask,
    4589             :                             int (*vlan_fill)(struct sk_buff *skb,
    4590             :                                              struct net_device *dev,
    4591             :                                              u32 filter_mask))
    4592             : {
    4593           0 :         struct nlmsghdr *nlh;
    4594           0 :         struct ifinfomsg *ifm;
    4595           0 :         struct nlattr *br_afspec;
    4596           0 :         struct nlattr *protinfo;
    4597           0 :         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
    4598           0 :         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4599           0 :         int err = 0;
    4600             : 
    4601           0 :         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
    4602           0 :         if (nlh == NULL)
    4603             :                 return -EMSGSIZE;
    4604             : 
    4605           0 :         ifm = nlmsg_data(nlh);
    4606           0 :         ifm->ifi_family = AF_BRIDGE;
    4607           0 :         ifm->__ifi_pad = 0;
    4608           0 :         ifm->ifi_type = dev->type;
    4609           0 :         ifm->ifi_index = dev->ifindex;
    4610           0 :         ifm->ifi_flags = dev_get_flags(dev);
    4611           0 :         ifm->ifi_change = 0;
    4612             : 
    4613             : 
    4614           0 :         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
    4615           0 :             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
    4616           0 :             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
    4617           0 :             (br_dev &&
    4618           0 :              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
    4619           0 :             (dev->addr_len &&
    4620           0 :              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
    4621           0 :             (dev->ifindex != dev_get_iflink(dev) &&
    4622           0 :              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
    4623           0 :                 goto nla_put_failure;
    4624             : 
    4625           0 :         br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
    4626           0 :         if (!br_afspec)
    4627           0 :                 goto nla_put_failure;
    4628             : 
    4629           0 :         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
    4630           0 :                 nla_nest_cancel(skb, br_afspec);
    4631           0 :                 goto nla_put_failure;
    4632             :         }
    4633             : 
    4634           0 :         if (mode != BRIDGE_MODE_UNDEF) {
    4635           0 :                 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
    4636           0 :                         nla_nest_cancel(skb, br_afspec);
    4637           0 :                         goto nla_put_failure;
    4638             :                 }
    4639             :         }
    4640           0 :         if (vlan_fill) {
    4641           0 :                 err = vlan_fill(skb, dev, filter_mask);
    4642           0 :                 if (err) {
    4643           0 :                         nla_nest_cancel(skb, br_afspec);
    4644           0 :                         goto nla_put_failure;
    4645             :                 }
    4646             :         }
    4647           0 :         nla_nest_end(skb, br_afspec);
    4648             : 
    4649           0 :         protinfo = nla_nest_start(skb, IFLA_PROTINFO);
    4650           0 :         if (!protinfo)
    4651           0 :                 goto nla_put_failure;
    4652             : 
    4653           0 :         if (brport_nla_put_flag(skb, flags, mask,
    4654           0 :                                 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
    4655           0 :             brport_nla_put_flag(skb, flags, mask,
    4656           0 :                                 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
    4657           0 :             brport_nla_put_flag(skb, flags, mask,
    4658             :                                 IFLA_BRPORT_FAST_LEAVE,
    4659           0 :                                 BR_MULTICAST_FAST_LEAVE) ||
    4660           0 :             brport_nla_put_flag(skb, flags, mask,
    4661           0 :                                 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
    4662           0 :             brport_nla_put_flag(skb, flags, mask,
    4663           0 :                                 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
    4664           0 :             brport_nla_put_flag(skb, flags, mask,
    4665           0 :                                 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
    4666           0 :             brport_nla_put_flag(skb, flags, mask,
    4667           0 :                                 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
    4668           0 :             brport_nla_put_flag(skb, flags, mask,
    4669           0 :                                 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
    4670           0 :             brport_nla_put_flag(skb, flags, mask,
    4671           0 :                                 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
    4672           0 :             brport_nla_put_flag(skb, flags, mask,
    4673             :                                 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
    4674           0 :                 nla_nest_cancel(skb, protinfo);
    4675           0 :                 goto nla_put_failure;
    4676             :         }
    4677             : 
    4678           0 :         nla_nest_end(skb, protinfo);
    4679             : 
    4680           0 :         nlmsg_end(skb, nlh);
    4681           0 :         return 0;
    4682           0 : nla_put_failure:
    4683           0 :         nlmsg_cancel(skb, nlh);
    4684           0 :         return err ? err : -EMSGSIZE;
    4685             : }
    4686             : EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
    4687             : 
    4688           0 : static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
    4689             :                                     bool strict_check, u32 *filter_mask,
    4690             :                                     struct netlink_ext_ack *extack)
    4691             : {
    4692           0 :         struct nlattr *tb[IFLA_MAX+1];
    4693           0 :         int err, i;
    4694             : 
    4695           0 :         if (strict_check) {
    4696           0 :                 struct ifinfomsg *ifm;
    4697             : 
    4698           0 :                 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
    4699           0 :                         NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
    4700           0 :                         return -EINVAL;
    4701             :                 }
    4702             : 
    4703           0 :                 ifm = nlmsg_data(nlh);
    4704           0 :                 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
    4705           0 :                     ifm->ifi_change || ifm->ifi_index) {
    4706           0 :                         NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
    4707           0 :                         return -EINVAL;
    4708             :                 }
    4709             : 
    4710           0 :                 err = nlmsg_parse_deprecated_strict(nlh,
    4711             :                                                     sizeof(struct ifinfomsg),
    4712             :                                                     tb, IFLA_MAX, ifla_policy,
    4713             :                                                     extack);
    4714             :         } else {
    4715           0 :                 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
    4716             :                                              tb, IFLA_MAX, ifla_policy,
    4717             :                                              extack);
    4718             :         }
    4719           0 :         if (err < 0)
    4720             :                 return err;
    4721             : 
    4722             :         /* new attributes should only be added with strict checking */
    4723           0 :         for (i = 0; i <= IFLA_MAX; ++i) {
    4724           0 :                 if (!tb[i])
    4725           0 :                         continue;
    4726             : 
    4727           0 :                 switch (i) {
    4728           0 :                 case IFLA_EXT_MASK:
    4729           0 :                         *filter_mask = nla_get_u32(tb[i]);
    4730           0 :                         break;
    4731           0 :                 default:
    4732           0 :                         if (strict_check) {
    4733           0 :                                 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
    4734           0 :                                 return -EINVAL;
    4735             :                         }
    4736             :                 }
    4737             :         }
    4738             : 
    4739             :         return 0;
    4740             : }
    4741             : 
    4742           0 : static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
    4743             : {
    4744           0 :         const struct nlmsghdr *nlh = cb->nlh;
    4745           0 :         struct net *net = sock_net(skb->sk);
    4746           0 :         struct net_device *dev;
    4747           0 :         int idx = 0;
    4748           0 :         u32 portid = NETLINK_CB(cb->skb).portid;
    4749           0 :         u32 seq = nlh->nlmsg_seq;
    4750           0 :         u32 filter_mask = 0;
    4751           0 :         int err;
    4752             : 
    4753           0 :         err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
    4754             :                                        cb->extack);
    4755           0 :         if (err < 0 && cb->strict_check)
    4756             :                 return err;
    4757             : 
    4758           0 :         rcu_read_lock();
    4759           0 :         for_each_netdev_rcu(net, dev) {
    4760           0 :                 const struct net_device_ops *ops = dev->netdev_ops;
    4761           0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4762             : 
    4763           0 :                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
    4764           0 :                         if (idx >= cb->args[0]) {
    4765           0 :                                 err = br_dev->netdev_ops->ndo_bridge_getlink(
    4766             :                                                 skb, portid, seq, dev,
    4767             :                                                 filter_mask, NLM_F_MULTI);
    4768           0 :                                 if (err < 0 && err != -EOPNOTSUPP) {
    4769           0 :                                         if (likely(skb->len))
    4770             :                                                 break;
    4771             : 
    4772           0 :                                         goto out_err;
    4773             :                                 }
    4774             :                         }
    4775           0 :                         idx++;
    4776             :                 }
    4777             : 
    4778           0 :                 if (ops->ndo_bridge_getlink) {
    4779           0 :                         if (idx >= cb->args[0]) {
    4780           0 :                                 err = ops->ndo_bridge_getlink(skb, portid,
    4781             :                                                               seq, dev,
    4782             :                                                               filter_mask,
    4783             :                                                               NLM_F_MULTI);
    4784           0 :                                 if (err < 0 && err != -EOPNOTSUPP) {
    4785           0 :                                         if (likely(skb->len))
    4786             :                                                 break;
    4787             : 
    4788           0 :                                         goto out_err;
    4789             :                                 }
    4790             :                         }
    4791           0 :                         idx++;
    4792             :                 }
    4793             :         }
    4794           0 :         err = skb->len;
    4795           0 : out_err:
    4796           0 :         rcu_read_unlock();
    4797           0 :         cb->args[0] = idx;
    4798             : 
    4799           0 :         return err;
    4800             : }
    4801             : 
    4802           0 : static inline size_t bridge_nlmsg_size(void)
    4803             : {
    4804           0 :         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
    4805           0 :                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
    4806           0 :                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
    4807           0 :                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
    4808           0 :                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
    4809           0 :                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
    4810           0 :                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
    4811           0 :                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
    4812           0 :                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
    4813           0 :                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
    4814           0 :                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
    4815             : }
    4816             : 
    4817           0 : static int rtnl_bridge_notify(struct net_device *dev)
    4818             : {
    4819           0 :         struct net *net = dev_net(dev);
    4820           0 :         struct sk_buff *skb;
    4821           0 :         int err = -EOPNOTSUPP;
    4822             : 
    4823           0 :         if (!dev->netdev_ops->ndo_bridge_getlink)
    4824             :                 return 0;
    4825             : 
    4826           0 :         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
    4827           0 :         if (!skb) {
    4828           0 :                 err = -ENOMEM;
    4829           0 :                 goto errout;
    4830             :         }
    4831             : 
    4832           0 :         err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
    4833           0 :         if (err < 0)
    4834           0 :                 goto errout;
    4835             : 
    4836           0 :         if (!skb->len)
    4837           0 :                 goto errout;
    4838             : 
    4839           0 :         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
    4840           0 :         return 0;
    4841           0 : errout:
    4842           0 :         WARN_ON(err == -EMSGSIZE);
    4843           0 :         kfree_skb(skb);
    4844           0 :         if (err)
    4845           0 :                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
    4846             :         return err;
    4847             : }
    4848             : 
    4849           0 : static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
    4850             :                                struct netlink_ext_ack *extack)
    4851             : {
    4852           0 :         struct net *net = sock_net(skb->sk);
    4853           0 :         struct ifinfomsg *ifm;
    4854           0 :         struct net_device *dev;
    4855           0 :         struct nlattr *br_spec, *attr = NULL;
    4856           0 :         int rem, err = -EOPNOTSUPP;
    4857           0 :         u16 flags = 0;
    4858           0 :         bool have_flags = false;
    4859             : 
    4860           0 :         if (nlmsg_len(nlh) < sizeof(*ifm))
    4861             :                 return -EINVAL;
    4862             : 
    4863           0 :         ifm = nlmsg_data(nlh);
    4864           0 :         if (ifm->ifi_family != AF_BRIDGE)
    4865             :                 return -EPFNOSUPPORT;
    4866             : 
    4867           0 :         dev = __dev_get_by_index(net, ifm->ifi_index);
    4868           0 :         if (!dev) {
    4869           0 :                 NL_SET_ERR_MSG(extack, "unknown ifindex");
    4870           0 :                 return -ENODEV;
    4871             :         }
    4872             : 
    4873           0 :         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
    4874           0 :         if (br_spec) {
    4875           0 :                 nla_for_each_nested(attr, br_spec, rem) {
    4876           0 :                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
    4877           0 :                                 if (nla_len(attr) < sizeof(flags))
    4878             :                                         return -EINVAL;
    4879             : 
    4880           0 :                                 have_flags = true;
    4881           0 :                                 flags = nla_get_u16(attr);
    4882           0 :                                 break;
    4883             :                         }
    4884             :                 }
    4885             :         }
    4886             : 
    4887           0 :         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
    4888           0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4889             : 
    4890           0 :                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
    4891           0 :                         err = -EOPNOTSUPP;
    4892           0 :                         goto out;
    4893             :                 }
    4894             : 
    4895           0 :                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
    4896             :                                                              extack);
    4897           0 :                 if (err)
    4898           0 :                         goto out;
    4899             : 
    4900           0 :                 flags &= ~BRIDGE_FLAGS_MASTER;
    4901             :         }
    4902             : 
    4903           0 :         if ((flags & BRIDGE_FLAGS_SELF)) {
    4904           0 :                 if (!dev->netdev_ops->ndo_bridge_setlink)
    4905             :                         err = -EOPNOTSUPP;
    4906             :                 else
    4907           0 :                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
    4908             :                                                                   flags,
    4909             :                                                                   extack);
    4910           0 :                 if (!err) {
    4911           0 :                         flags &= ~BRIDGE_FLAGS_SELF;
    4912             : 
    4913             :                         /* Generate event to notify upper layer of bridge
    4914             :                          * change
    4915             :                          */
    4916           0 :                         err = rtnl_bridge_notify(dev);
    4917             :                 }
    4918             :         }
    4919             : 
    4920           0 :         if (have_flags)
    4921           0 :                 memcpy(nla_data(attr), &flags, sizeof(flags));
    4922           0 : out:
    4923             :         return err;
    4924             : }
    4925             : 
    4926           0 : static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
    4927             :                                struct netlink_ext_ack *extack)
    4928             : {
    4929           0 :         struct net *net = sock_net(skb->sk);
    4930           0 :         struct ifinfomsg *ifm;
    4931           0 :         struct net_device *dev;
    4932           0 :         struct nlattr *br_spec, *attr = NULL;
    4933           0 :         int rem, err = -EOPNOTSUPP;
    4934           0 :         u16 flags = 0;
    4935           0 :         bool have_flags = false;
    4936             : 
    4937           0 :         if (nlmsg_len(nlh) < sizeof(*ifm))
    4938             :                 return -EINVAL;
    4939             : 
    4940           0 :         ifm = nlmsg_data(nlh);
    4941           0 :         if (ifm->ifi_family != AF_BRIDGE)
    4942             :                 return -EPFNOSUPPORT;
    4943             : 
    4944           0 :         dev = __dev_get_by_index(net, ifm->ifi_index);
    4945           0 :         if (!dev) {
    4946           0 :                 NL_SET_ERR_MSG(extack, "unknown ifindex");
    4947           0 :                 return -ENODEV;
    4948             :         }
    4949             : 
    4950           0 :         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
    4951           0 :         if (br_spec) {
    4952           0 :                 nla_for_each_nested(attr, br_spec, rem) {
    4953           0 :                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
    4954           0 :                                 if (nla_len(attr) < sizeof(flags))
    4955             :                                         return -EINVAL;
    4956             : 
    4957           0 :                                 have_flags = true;
    4958           0 :                                 flags = nla_get_u16(attr);
    4959           0 :                                 break;
    4960             :                         }
    4961             :                 }
    4962             :         }
    4963             : 
    4964           0 :         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
    4965           0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    4966             : 
    4967           0 :                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
    4968           0 :                         err = -EOPNOTSUPP;
    4969           0 :                         goto out;
    4970             :                 }
    4971             : 
    4972           0 :                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
    4973           0 :                 if (err)
    4974           0 :                         goto out;
    4975             : 
    4976           0 :                 flags &= ~BRIDGE_FLAGS_MASTER;
    4977             :         }
    4978             : 
    4979           0 :         if ((flags & BRIDGE_FLAGS_SELF)) {
    4980           0 :                 if (!dev->netdev_ops->ndo_bridge_dellink)
    4981             :                         err = -EOPNOTSUPP;
    4982             :                 else
    4983           0 :                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
    4984             :                                                                   flags);
    4985             : 
    4986           0 :                 if (!err) {
    4987           0 :                         flags &= ~BRIDGE_FLAGS_SELF;
    4988             : 
    4989             :                         /* Generate event to notify upper layer of bridge
    4990             :                          * change
    4991             :                          */
    4992           0 :                         err = rtnl_bridge_notify(dev);
    4993             :                 }
    4994             :         }
    4995             : 
    4996           0 :         if (have_flags)
    4997           0 :                 memcpy(nla_data(attr), &flags, sizeof(flags));
    4998           0 : out:
    4999             :         return err;
    5000             : }
    5001             : 
    5002           0 : static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
    5003             : {
    5004           0 :         return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
    5005           0 :                (!idxattr || idxattr == attrid);
    5006             : }
    5007             : 
    5008             : #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
    5009           0 : static int rtnl_get_offload_stats_attr_size(int attr_id)
    5010             : {
    5011           0 :         switch (attr_id) {
    5012             :         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
    5013             :                 return sizeof(struct rtnl_link_stats64);
    5014             :         }
    5015             : 
    5016             :         return 0;
    5017             : }
    5018             : 
    5019           0 : static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
    5020             :                                   int *prividx)
    5021             : {
    5022           0 :         struct nlattr *attr = NULL;
    5023           0 :         int attr_id, size;
    5024           0 :         void *attr_data;
    5025           0 :         int err;
    5026             : 
    5027           0 :         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
    5028           0 :               dev->netdev_ops->ndo_get_offload_stats))
    5029             :                 return -ENODATA;
    5030             : 
    5031           0 :         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
    5032           0 :              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
    5033           0 :                 if (attr_id < *prividx)
    5034           0 :                         continue;
    5035             : 
    5036           0 :                 size = rtnl_get_offload_stats_attr_size(attr_id);
    5037           0 :                 if (!size)
    5038             :                         continue;
    5039             : 
    5040           0 :                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
    5041           0 :                         continue;
    5042             : 
    5043           0 :                 attr = nla_reserve_64bit(skb, attr_id, size,
    5044             :                                          IFLA_OFFLOAD_XSTATS_UNSPEC);
    5045           0 :                 if (!attr)
    5046           0 :                         goto nla_put_failure;
    5047             : 
    5048           0 :                 attr_data = nla_data(attr);
    5049           0 :                 memset(attr_data, 0, size);
    5050           0 :                 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
    5051             :                                                              attr_data);
    5052           0 :                 if (err)
    5053           0 :                         goto get_offload_stats_failure;
    5054             :         }
    5055             : 
    5056           0 :         if (!attr)
    5057             :                 return -ENODATA;
    5058             : 
    5059           0 :         *prividx = 0;
    5060           0 :         return 0;
    5061             : 
    5062           0 : nla_put_failure:
    5063           0 :         err = -EMSGSIZE;
    5064           0 : get_offload_stats_failure:
    5065           0 :         *prividx = attr_id;
    5066           0 :         return err;
    5067             : }
    5068             : 
    5069           0 : static int rtnl_get_offload_stats_size(const struct net_device *dev)
    5070             : {
    5071           0 :         int nla_size = 0;
    5072           0 :         int attr_id;
    5073           0 :         int size;
    5074             : 
    5075           0 :         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
    5076           0 :               dev->netdev_ops->ndo_get_offload_stats))
    5077             :                 return 0;
    5078             : 
    5079           0 :         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
    5080           0 :              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
    5081           0 :                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
    5082           0 :                         continue;
    5083           0 :                 size = rtnl_get_offload_stats_attr_size(attr_id);
    5084           0 :                 nla_size += nla_total_size_64bit(size);
    5085             :         }
    5086             : 
    5087           0 :         if (nla_size != 0)
    5088           0 :                 nla_size += nla_total_size(0);
    5089             : 
    5090             :         return nla_size;
    5091             : }
    5092             : 
    5093           0 : static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
    5094             :                                int type, u32 pid, u32 seq, u32 change,
    5095             :                                unsigned int flags, unsigned int filter_mask,
    5096             :                                int *idxattr, int *prividx)
    5097             : {
    5098           0 :         struct if_stats_msg *ifsm;
    5099           0 :         struct nlmsghdr *nlh;
    5100           0 :         struct nlattr *attr;
    5101           0 :         int s_prividx = *prividx;
    5102           0 :         int err;
    5103             : 
    5104           0 :         ASSERT_RTNL();
    5105             : 
    5106           0 :         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
    5107           0 :         if (!nlh)
    5108             :                 return -EMSGSIZE;
    5109             : 
    5110           0 :         ifsm = nlmsg_data(nlh);
    5111           0 :         ifsm->family = PF_UNSPEC;
    5112           0 :         ifsm->pad1 = 0;
    5113           0 :         ifsm->pad2 = 0;
    5114           0 :         ifsm->ifindex = dev->ifindex;
    5115           0 :         ifsm->filter_mask = filter_mask;
    5116             : 
    5117           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
    5118           0 :                 struct rtnl_link_stats64 *sp;
    5119             : 
    5120           0 :                 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
    5121             :                                          sizeof(struct rtnl_link_stats64),
    5122             :                                          IFLA_STATS_UNSPEC);
    5123           0 :                 if (!attr)
    5124           0 :                         goto nla_put_failure;
    5125             : 
    5126           0 :                 sp = nla_data(attr);
    5127           0 :                 dev_get_stats(dev, sp);
    5128             :         }
    5129             : 
    5130           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
    5131           0 :                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
    5132             : 
    5133           0 :                 if (ops && ops->fill_linkxstats) {
    5134           0 :                         *idxattr = IFLA_STATS_LINK_XSTATS;
    5135           0 :                         attr = nla_nest_start_noflag(skb,
    5136             :                                                      IFLA_STATS_LINK_XSTATS);
    5137           0 :                         if (!attr)
    5138           0 :                                 goto nla_put_failure;
    5139             : 
    5140           0 :                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
    5141           0 :                         nla_nest_end(skb, attr);
    5142           0 :                         if (err)
    5143           0 :                                 goto nla_put_failure;
    5144           0 :                         *idxattr = 0;
    5145             :                 }
    5146             :         }
    5147             : 
    5148           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
    5149             :                              *idxattr)) {
    5150           0 :                 const struct rtnl_link_ops *ops = NULL;
    5151           0 :                 const struct net_device *master;
    5152             : 
    5153           0 :                 master = netdev_master_upper_dev_get(dev);
    5154           0 :                 if (master)
    5155           0 :                         ops = master->rtnl_link_ops;
    5156           0 :                 if (ops && ops->fill_linkxstats) {
    5157           0 :                         *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
    5158           0 :                         attr = nla_nest_start_noflag(skb,
    5159             :                                                      IFLA_STATS_LINK_XSTATS_SLAVE);
    5160           0 :                         if (!attr)
    5161           0 :                                 goto nla_put_failure;
    5162             : 
    5163           0 :                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
    5164           0 :                         nla_nest_end(skb, attr);
    5165           0 :                         if (err)
    5166           0 :                                 goto nla_put_failure;
    5167           0 :                         *idxattr = 0;
    5168             :                 }
    5169             :         }
    5170             : 
    5171           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
    5172             :                              *idxattr)) {
    5173           0 :                 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
    5174           0 :                 attr = nla_nest_start_noflag(skb,
    5175             :                                              IFLA_STATS_LINK_OFFLOAD_XSTATS);
    5176           0 :                 if (!attr)
    5177           0 :                         goto nla_put_failure;
    5178             : 
    5179           0 :                 err = rtnl_get_offload_stats(skb, dev, prividx);
    5180           0 :                 if (err == -ENODATA)
    5181           0 :                         nla_nest_cancel(skb, attr);
    5182             :                 else
    5183           0 :                         nla_nest_end(skb, attr);
    5184             : 
    5185           0 :                 if (err && err != -ENODATA)
    5186           0 :                         goto nla_put_failure;
    5187           0 :                 *idxattr = 0;
    5188             :         }
    5189             : 
    5190           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
    5191           0 :                 struct rtnl_af_ops *af_ops;
    5192             : 
    5193           0 :                 *idxattr = IFLA_STATS_AF_SPEC;
    5194           0 :                 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
    5195           0 :                 if (!attr)
    5196           0 :                         goto nla_put_failure;
    5197             : 
    5198           0 :                 rcu_read_lock();
    5199           0 :                 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
    5200           0 :                         if (af_ops->fill_stats_af) {
    5201           0 :                                 struct nlattr *af;
    5202           0 :                                 int err;
    5203             : 
    5204           0 :                                 af = nla_nest_start_noflag(skb,
    5205             :                                                            af_ops->family);
    5206           0 :                                 if (!af) {
    5207           0 :                                         rcu_read_unlock();
    5208           0 :                                         goto nla_put_failure;
    5209             :                                 }
    5210           0 :                                 err = af_ops->fill_stats_af(skb, dev);
    5211             : 
    5212           0 :                                 if (err == -ENODATA) {
    5213           0 :                                         nla_nest_cancel(skb, af);
    5214           0 :                                 } else if (err < 0) {
    5215           0 :                                         rcu_read_unlock();
    5216           0 :                                         goto nla_put_failure;
    5217             :                                 }
    5218             : 
    5219           0 :                                 nla_nest_end(skb, af);
    5220             :                         }
    5221             :                 }
    5222           0 :                 rcu_read_unlock();
    5223             : 
    5224           0 :                 nla_nest_end(skb, attr);
    5225             : 
    5226           0 :                 *idxattr = 0;
    5227             :         }
    5228             : 
    5229           0 :         nlmsg_end(skb, nlh);
    5230             : 
    5231           0 :         return 0;
    5232             : 
    5233           0 : nla_put_failure:
    5234             :         /* not a multi message or no progress mean a real error */
    5235           0 :         if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
    5236           0 :                 nlmsg_cancel(skb, nlh);
    5237             :         else
    5238           0 :                 nlmsg_end(skb, nlh);
    5239             : 
    5240             :         return -EMSGSIZE;
    5241             : }
    5242             : 
    5243           0 : static size_t if_nlmsg_stats_size(const struct net_device *dev,
    5244             :                                   u32 filter_mask)
    5245             : {
    5246           0 :         size_t size = 0;
    5247             : 
    5248           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
    5249           0 :                 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
    5250             : 
    5251           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
    5252           0 :                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
    5253           0 :                 int attr = IFLA_STATS_LINK_XSTATS;
    5254             : 
    5255           0 :                 if (ops && ops->get_linkxstats_size) {
    5256           0 :                         size += nla_total_size(ops->get_linkxstats_size(dev,
    5257             :                                                                         attr));
    5258             :                         /* for IFLA_STATS_LINK_XSTATS */
    5259           0 :                         size += nla_total_size(0);
    5260             :                 }
    5261             :         }
    5262             : 
    5263           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
    5264           0 :                 struct net_device *_dev = (struct net_device *)dev;
    5265           0 :                 const struct rtnl_link_ops *ops = NULL;
    5266           0 :                 const struct net_device *master;
    5267             : 
    5268             :                 /* netdev_master_upper_dev_get can't take const */
    5269           0 :                 master = netdev_master_upper_dev_get(_dev);
    5270           0 :                 if (master)
    5271           0 :                         ops = master->rtnl_link_ops;
    5272           0 :                 if (ops && ops->get_linkxstats_size) {
    5273           0 :                         int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
    5274             : 
    5275           0 :                         size += nla_total_size(ops->get_linkxstats_size(dev,
    5276             :                                                                         attr));
    5277             :                         /* for IFLA_STATS_LINK_XSTATS_SLAVE */
    5278           0 :                         size += nla_total_size(0);
    5279             :                 }
    5280             :         }
    5281             : 
    5282           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
    5283           0 :                 size += rtnl_get_offload_stats_size(dev);
    5284             : 
    5285           0 :         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
    5286           0 :                 struct rtnl_af_ops *af_ops;
    5287             : 
    5288             :                 /* for IFLA_STATS_AF_SPEC */
    5289           0 :                 size += nla_total_size(0);
    5290             : 
    5291           0 :                 rcu_read_lock();
    5292           0 :                 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
    5293           0 :                         if (af_ops->get_stats_af_size) {
    5294           0 :                                 size += nla_total_size(
    5295           0 :                                         af_ops->get_stats_af_size(dev));
    5296             : 
    5297             :                                 /* for AF_* */
    5298           0 :                                 size += nla_total_size(0);
    5299             :                         }
    5300             :                 }
    5301           0 :                 rcu_read_unlock();
    5302             :         }
    5303             : 
    5304           0 :         return size;
    5305             : }
    5306             : 
    5307           0 : static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
    5308             :                                 bool is_dump, struct netlink_ext_ack *extack)
    5309             : {
    5310           0 :         struct if_stats_msg *ifsm;
    5311             : 
    5312           0 :         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
    5313           0 :                 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
    5314           0 :                 return -EINVAL;
    5315             :         }
    5316             : 
    5317           0 :         if (!strict_check)
    5318             :                 return 0;
    5319             : 
    5320           0 :         ifsm = nlmsg_data(nlh);
    5321             : 
    5322             :         /* only requests using strict checks can pass data to influence
    5323             :          * the dump. The legacy exception is filter_mask.
    5324             :          */
    5325           0 :         if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
    5326           0 :                 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
    5327           0 :                 return -EINVAL;
    5328             :         }
    5329           0 :         if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
    5330           0 :                 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
    5331           0 :                 return -EINVAL;
    5332             :         }
    5333           0 :         if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
    5334           0 :                 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
    5335           0 :                 return -EINVAL;
    5336             :         }
    5337             : 
    5338             :         return 0;
    5339             : }
    5340             : 
    5341           0 : static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
    5342             :                           struct netlink_ext_ack *extack)
    5343             : {
    5344           0 :         struct net *net = sock_net(skb->sk);
    5345           0 :         struct net_device *dev = NULL;
    5346           0 :         int idxattr = 0, prividx = 0;
    5347           0 :         struct if_stats_msg *ifsm;
    5348           0 :         struct sk_buff *nskb;
    5349           0 :         u32 filter_mask;
    5350           0 :         int err;
    5351             : 
    5352           0 :         err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
    5353             :                                    false, extack);
    5354           0 :         if (err)
    5355             :                 return err;
    5356             : 
    5357           0 :         ifsm = nlmsg_data(nlh);
    5358           0 :         if (ifsm->ifindex > 0)
    5359           0 :                 dev = __dev_get_by_index(net, ifsm->ifindex);
    5360             :         else
    5361             :                 return -EINVAL;
    5362             : 
    5363           0 :         if (!dev)
    5364             :                 return -ENODEV;
    5365             : 
    5366           0 :         filter_mask = ifsm->filter_mask;
    5367           0 :         if (!filter_mask)
    5368             :                 return -EINVAL;
    5369             : 
    5370           0 :         nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
    5371           0 :         if (!nskb)
    5372             :                 return -ENOBUFS;
    5373             : 
    5374           0 :         err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
    5375             :                                   NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
    5376             :                                   0, filter_mask, &idxattr, &prividx);
    5377           0 :         if (err < 0) {
    5378             :                 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
    5379           0 :                 WARN_ON(err == -EMSGSIZE);
    5380           0 :                 kfree_skb(nskb);
    5381             :         } else {
    5382           0 :                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
    5383             :         }
    5384             : 
    5385             :         return err;
    5386             : }
    5387             : 
    5388           0 : static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
    5389             : {
    5390           0 :         struct netlink_ext_ack *extack = cb->extack;
    5391           0 :         int h, s_h, err, s_idx, s_idxattr, s_prividx;
    5392           0 :         struct net *net = sock_net(skb->sk);
    5393           0 :         unsigned int flags = NLM_F_MULTI;
    5394           0 :         struct if_stats_msg *ifsm;
    5395           0 :         struct hlist_head *head;
    5396           0 :         struct net_device *dev;
    5397           0 :         u32 filter_mask = 0;
    5398           0 :         int idx = 0;
    5399             : 
    5400           0 :         s_h = cb->args[0];
    5401           0 :         s_idx = cb->args[1];
    5402           0 :         s_idxattr = cb->args[2];
    5403           0 :         s_prividx = cb->args[3];
    5404             : 
    5405           0 :         cb->seq = net->dev_base_seq;
    5406             : 
    5407           0 :         err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
    5408           0 :         if (err)
    5409             :                 return err;
    5410             : 
    5411           0 :         ifsm = nlmsg_data(cb->nlh);
    5412           0 :         filter_mask = ifsm->filter_mask;
    5413           0 :         if (!filter_mask) {
    5414           0 :                 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
    5415           0 :                 return -EINVAL;
    5416             :         }
    5417             : 
    5418           0 :         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
    5419           0 :                 idx = 0;
    5420           0 :                 head = &net->dev_index_head[h];
    5421           0 :                 hlist_for_each_entry(dev, head, index_hlist) {
    5422           0 :                         if (idx < s_idx)
    5423           0 :                                 goto cont;
    5424           0 :                         err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
    5425           0 :                                                   NETLINK_CB(cb->skb).portid,
    5426           0 :                                                   cb->nlh->nlmsg_seq, 0,
    5427             :                                                   flags, filter_mask,
    5428             :                                                   &s_idxattr, &s_prividx);
    5429             :                         /* If we ran out of room on the first message,
    5430             :                          * we're in trouble
    5431             :                          */
    5432           0 :                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
    5433             : 
    5434           0 :                         if (err < 0)
    5435           0 :                                 goto out;
    5436           0 :                         s_prividx = 0;
    5437           0 :                         s_idxattr = 0;
    5438           0 :                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
    5439           0 : cont:
    5440           0 :                         idx++;
    5441             :                 }
    5442             :         }
    5443           0 : out:
    5444           0 :         cb->args[3] = s_prividx;
    5445           0 :         cb->args[2] = s_idxattr;
    5446           0 :         cb->args[1] = idx;
    5447           0 :         cb->args[0] = h;
    5448             : 
    5449           0 :         return skb->len;
    5450             : }
    5451             : 
    5452             : /* Process one rtnetlink message. */
    5453             : 
    5454          43 : static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
    5455             :                              struct netlink_ext_ack *extack)
    5456             : {
    5457          43 :         struct net *net = sock_net(skb->sk);
    5458          43 :         struct rtnl_link *link;
    5459          43 :         struct module *owner;
    5460          43 :         int err = -EOPNOTSUPP;
    5461          43 :         rtnl_doit_func doit;
    5462          43 :         unsigned int flags;
    5463          43 :         int kind;
    5464          43 :         int family;
    5465          43 :         int type;
    5466             : 
    5467          43 :         type = nlh->nlmsg_type;
    5468          43 :         if (type > RTM_MAX)
    5469             :                 return -EOPNOTSUPP;
    5470             : 
    5471          43 :         type -= RTM_BASE;
    5472             : 
    5473             :         /* All the messages must have at least 1 byte length */
    5474          43 :         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
    5475             :                 return 0;
    5476             : 
    5477          43 :         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
    5478          43 :         kind = type&3;
    5479             : 
    5480          43 :         if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
    5481             :                 return -EPERM;
    5482             : 
    5483          43 :         rcu_read_lock();
    5484          43 :         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
    5485          33 :                 struct sock *rtnl;
    5486          33 :                 rtnl_dumpit_func dumpit;
    5487          33 :                 u32 min_dump_alloc = 0;
    5488             : 
    5489          33 :                 link = rtnl_get_link(family, type);
    5490          33 :                 if (!link || !link->dumpit) {
    5491           0 :                         family = PF_UNSPEC;
    5492           0 :                         link = rtnl_get_link(family, type);
    5493           0 :                         if (!link || !link->dumpit)
    5494           0 :                                 goto err_unlock;
    5495             :                 }
    5496          33 :                 owner = link->owner;
    5497          33 :                 dumpit = link->dumpit;
    5498             : 
    5499          33 :                 if (type == RTM_GETLINK - RTM_BASE)
    5500           6 :                         min_dump_alloc = rtnl_calcit(skb, nlh);
    5501             : 
    5502          33 :                 err = 0;
    5503             :                 /* need to do this before rcu_read_unlock() */
    5504          33 :                 if (!try_module_get(owner))
    5505             :                         err = -EPROTONOSUPPORT;
    5506             : 
    5507          33 :                 rcu_read_unlock();
    5508             : 
    5509          33 :                 rtnl = net->rtnl;
    5510          33 :                 if (err == 0) {
    5511          33 :                         struct netlink_dump_control c = {
    5512             :                                 .dump           = dumpit,
    5513             :                                 .min_dump_alloc = min_dump_alloc,
    5514             :                                 .module         = owner,
    5515             :                         };
    5516          33 :                         err = netlink_dump_start(rtnl, skb, nlh, &c);
    5517             :                         /* netlink_dump_start() will keep a reference on
    5518             :                          * module if dump is still in progress.
    5519             :                          */
    5520          33 :                         module_put(owner);
    5521             :                 }
    5522          33 :                 return err;
    5523             :         }
    5524             : 
    5525          10 :         link = rtnl_get_link(family, type);
    5526          10 :         if (!link || !link->doit) {
    5527           1 :                 family = PF_UNSPEC;
    5528           1 :                 link = rtnl_get_link(PF_UNSPEC, type);
    5529           1 :                 if (!link || !link->doit)
    5530           1 :                         goto out_unlock;
    5531             :         }
    5532             : 
    5533           9 :         owner = link->owner;
    5534           9 :         if (!try_module_get(owner)) {
    5535             :                 err = -EPROTONOSUPPORT;
    5536             :                 goto out_unlock;
    5537             :         }
    5538             : 
    5539           9 :         flags = link->flags;
    5540           9 :         if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
    5541           0 :                 doit = link->doit;
    5542           0 :                 rcu_read_unlock();
    5543           0 :                 if (doit)
    5544           0 :                         err = doit(skb, nlh, extack);
    5545           0 :                 module_put(owner);
    5546           0 :                 return err;
    5547             :         }
    5548           9 :         rcu_read_unlock();
    5549             : 
    5550           9 :         rtnl_lock();
    5551           9 :         link = rtnl_get_link(family, type);
    5552           9 :         if (link && link->doit)
    5553           9 :                 err = link->doit(skb, nlh, extack);
    5554           9 :         rtnl_unlock();
    5555             : 
    5556           9 :         module_put(owner);
    5557             : 
    5558           9 :         return err;
    5559             : 
    5560           1 : out_unlock:
    5561           1 :         rcu_read_unlock();
    5562           1 :         return err;
    5563             : 
    5564           0 : err_unlock:
    5565           0 :         rcu_read_unlock();
    5566           0 :         return -EOPNOTSUPP;
    5567             : }
    5568             : 
    5569          43 : static void rtnetlink_rcv(struct sk_buff *skb)
    5570             : {
    5571          43 :         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
    5572          43 : }
    5573             : 
    5574           0 : static int rtnetlink_bind(struct net *net, int group)
    5575             : {
    5576           0 :         switch (group) {
    5577           0 :         case RTNLGRP_IPV4_MROUTE_R:
    5578             :         case RTNLGRP_IPV6_MROUTE_R:
    5579           0 :                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    5580           0 :                         return -EPERM;
    5581             :                 break;
    5582             :         }
    5583             :         return 0;
    5584             : }
    5585             : 
    5586           8 : static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
    5587             : {
    5588           8 :         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
    5589             : 
    5590           8 :         switch (event) {
    5591             :         case NETDEV_REBOOT:
    5592             :         case NETDEV_CHANGEMTU:
    5593             :         case NETDEV_CHANGEADDR:
    5594             :         case NETDEV_CHANGENAME:
    5595             :         case NETDEV_FEAT_CHANGE:
    5596             :         case NETDEV_BONDING_FAILOVER:
    5597             :         case NETDEV_POST_TYPE_CHANGE:
    5598             :         case NETDEV_NOTIFY_PEERS:
    5599             :         case NETDEV_CHANGEUPPER:
    5600             :         case NETDEV_RESEND_IGMP:
    5601             :         case NETDEV_CHANGEINFODATA:
    5602             :         case NETDEV_CHANGELOWERSTATE:
    5603             :         case NETDEV_CHANGE_TX_QUEUE_LEN:
    5604           0 :                 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
    5605             :                                    GFP_KERNEL, NULL, 0);
    5606           0 :                 break;
    5607             :         default:
    5608             :                 break;
    5609             :         }
    5610           8 :         return NOTIFY_DONE;
    5611             : }
    5612             : 
    5613             : static struct notifier_block rtnetlink_dev_notifier = {
    5614             :         .notifier_call  = rtnetlink_event,
    5615             : };
    5616             : 
    5617             : 
    5618           1 : static int __net_init rtnetlink_net_init(struct net *net)
    5619             : {
    5620           1 :         struct sock *sk;
    5621           1 :         struct netlink_kernel_cfg cfg = {
    5622             :                 .groups         = RTNLGRP_MAX,
    5623             :                 .input          = rtnetlink_rcv,
    5624             :                 .cb_mutex       = &rtnl_mutex,
    5625             :                 .flags          = NL_CFG_F_NONROOT_RECV,
    5626             :                 .bind           = rtnetlink_bind,
    5627             :         };
    5628             : 
    5629           1 :         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
    5630           1 :         if (!sk)
    5631             :                 return -ENOMEM;
    5632           1 :         net->rtnl = sk;
    5633           1 :         return 0;
    5634             : }
    5635             : 
    5636           0 : static void __net_exit rtnetlink_net_exit(struct net *net)
    5637             : {
    5638           0 :         netlink_kernel_release(net->rtnl);
    5639           0 :         net->rtnl = NULL;
    5640           0 : }
    5641             : 
    5642             : static struct pernet_operations rtnetlink_net_ops = {
    5643             :         .init = rtnetlink_net_init,
    5644             :         .exit = rtnetlink_net_exit,
    5645             : };
    5646             : 
    5647           1 : void __init rtnetlink_init(void)
    5648             : {
    5649           1 :         if (register_pernet_subsys(&rtnetlink_net_ops))
    5650           0 :                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
    5651             : 
    5652           1 :         register_netdevice_notifier(&rtnetlink_dev_notifier);
    5653             : 
    5654           1 :         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
    5655             :                       rtnl_dump_ifinfo, 0);
    5656           1 :         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
    5657           1 :         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
    5658           1 :         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
    5659             : 
    5660           1 :         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
    5661           1 :         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
    5662           1 :         rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
    5663             : 
    5664           1 :         rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
    5665           1 :         rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
    5666             : 
    5667           1 :         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
    5668           1 :         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
    5669           1 :         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
    5670             : 
    5671           1 :         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
    5672           1 :         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
    5673           1 :         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
    5674             : 
    5675           1 :         rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
    5676             :                       0);
    5677           1 : }

Generated by: LCOV version 1.14