LCOV - code coverage report
Current view: top level - net/core - rtnetlink.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 251 983 25.5 %
Date: 2014-04-16 Functions: 22 72 30.6 %
Branches: 164 909 18.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * INET         An implementation of the TCP/IP protocol suite for the LINUX
       3                 :            :  *              operating system.  INET is implemented using the  BSD Socket
       4                 :            :  *              interface as the means of communication with the user level.
       5                 :            :  *
       6                 :            :  *              Routing netlink socket interface: protocol independent part.
       7                 :            :  *
       8                 :            :  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
       9                 :            :  *
      10                 :            :  *              This program is free software; you can redistribute it and/or
      11                 :            :  *              modify it under the terms of the GNU General Public License
      12                 :            :  *              as published by the Free Software Foundation; either version
      13                 :            :  *              2 of the License, or (at your option) any later version.
      14                 :            :  *
      15                 :            :  *      Fixes:
      16                 :            :  *      Vitaly E. Lavrov                RTA_OK arithmetics was wrong.
      17                 :            :  */
      18                 :            : 
      19                 :            : #include <linux/errno.h>
      20                 :            : #include <linux/module.h>
      21                 :            : #include <linux/types.h>
      22                 :            : #include <linux/socket.h>
      23                 :            : #include <linux/kernel.h>
      24                 :            : #include <linux/timer.h>
      25                 :            : #include <linux/string.h>
      26                 :            : #include <linux/sockios.h>
      27                 :            : #include <linux/net.h>
      28                 :            : #include <linux/fcntl.h>
      29                 :            : #include <linux/mm.h>
      30                 :            : #include <linux/slab.h>
      31                 :            : #include <linux/interrupt.h>
      32                 :            : #include <linux/capability.h>
      33                 :            : #include <linux/skbuff.h>
      34                 :            : #include <linux/init.h>
      35                 :            : #include <linux/security.h>
      36                 :            : #include <linux/mutex.h>
      37                 :            : #include <linux/if_addr.h>
      38                 :            : #include <linux/if_bridge.h>
      39                 :            : #include <linux/pci.h>
      40                 :            : #include <linux/etherdevice.h>
      41                 :            : 
      42                 :            : #include <asm/uaccess.h>
      43                 :            : 
      44                 :            : #include <linux/inet.h>
      45                 :            : #include <linux/netdevice.h>
      46                 :            : #include <net/ip.h>
      47                 :            : #include <net/protocol.h>
      48                 :            : #include <net/arp.h>
      49                 :            : #include <net/route.h>
      50                 :            : #include <net/udp.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                 :            : struct rtnl_link {
      58                 :            :         rtnl_doit_func          doit;
      59                 :            :         rtnl_dumpit_func        dumpit;
      60                 :            :         rtnl_calcit_func        calcit;
      61                 :            : };
      62                 :            : 
      63                 :            : static DEFINE_MUTEX(rtnl_mutex);
      64                 :            : 
      65                 :          0 : void rtnl_lock(void)
      66                 :            : {
      67                 :       2496 :         mutex_lock(&rtnl_mutex);
      68                 :        276 : }
      69                 :            : EXPORT_SYMBOL(rtnl_lock);
      70                 :            : 
      71                 :          0 : void __rtnl_unlock(void)
      72                 :            : {
      73                 :       2496 :         mutex_unlock(&rtnl_mutex);
      74                 :       1387 : }
      75                 :            : 
      76                 :          0 : void rtnl_unlock(void)
      77                 :            : {
      78                 :            :         /* This fellow will unlock it for us. */
      79                 :       1387 :         netdev_run_todo();
      80                 :        276 : }
      81                 :            : EXPORT_SYMBOL(rtnl_unlock);
      82                 :            : 
      83                 :          0 : int rtnl_trylock(void)
      84                 :            : {
      85                 :          0 :         return mutex_trylock(&rtnl_mutex);
      86                 :            : }
      87                 :            : EXPORT_SYMBOL(rtnl_trylock);
      88                 :            : 
      89                 :          0 : int rtnl_is_locked(void)
      90                 :            : {
      91                 :       3604 :         return mutex_is_locked(&rtnl_mutex);
      92                 :            : }
      93                 :            : EXPORT_SYMBOL(rtnl_is_locked);
      94                 :            : 
      95                 :            : #ifdef CONFIG_PROVE_LOCKING
      96                 :            : int lockdep_rtnl_is_held(void)
      97                 :            : {
      98                 :            :         return lockdep_is_held(&rtnl_mutex);
      99                 :            : }
     100                 :            : EXPORT_SYMBOL(lockdep_rtnl_is_held);
     101                 :            : #endif /* #ifdef CONFIG_PROVE_LOCKING */
     102                 :            : 
     103                 :            : static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
     104                 :            : 
     105                 :            : static inline int rtm_msgindex(int msgtype)
     106                 :            : {
     107                 :          0 :         int msgindex = msgtype - RTM_BASE;
     108                 :            : 
     109                 :            :         /*
     110                 :            :          * msgindex < 0 implies someone tried to register a netlink
     111                 :            :          * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
     112                 :            :          * the message type has not been added to linux/rtnetlink.h
     113                 :            :          */
     114 [ #  # ][ #  # ]:          0 :         BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
     115                 :            : 
     116                 :            :         return msgindex;
     117                 :            : }
     118                 :            : 
     119                 :            : static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
     120                 :            : {
     121                 :            :         struct rtnl_link *tab;
     122                 :            : 
     123         [ +  - ]:          2 :         if (protocol <= RTNL_FAMILY_MAX)
     124                 :          2 :                 tab = rtnl_msg_handlers[protocol];
     125                 :            :         else
     126                 :            :                 tab = NULL;
     127                 :            : 
     128 [ +  - ][ -  + ]:          2 :         if (tab == NULL || tab[msgindex].doit == NULL)
     129                 :          0 :                 tab = rtnl_msg_handlers[PF_UNSPEC];
     130                 :            : 
     131                 :          2 :         return tab[msgindex].doit;
     132                 :            : }
     133                 :            : 
     134                 :            : static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
     135                 :            : {
     136                 :            :         struct rtnl_link *tab;
     137                 :            : 
     138         [ +  - ]:       1109 :         if (protocol <= RTNL_FAMILY_MAX)
     139                 :       1109 :                 tab = rtnl_msg_handlers[protocol];
     140                 :            :         else
     141                 :            :                 tab = NULL;
     142                 :            : 
     143 [ +  - ][ -  + ]:       1109 :         if (tab == NULL || tab[msgindex].dumpit == NULL)
     144                 :          0 :                 tab = rtnl_msg_handlers[PF_UNSPEC];
     145                 :            : 
     146                 :       1109 :         return tab[msgindex].dumpit;
     147                 :            : }
     148                 :            : 
     149                 :            : static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
     150                 :            : {
     151                 :            :         struct rtnl_link *tab;
     152                 :            : 
     153         [ +  - ]:       1109 :         if (protocol <= RTNL_FAMILY_MAX)
     154                 :       1109 :                 tab = rtnl_msg_handlers[protocol];
     155                 :            :         else
     156                 :            :                 tab = NULL;
     157                 :            : 
     158 [ +  - ][ +  + ]:       1109 :         if (tab == NULL || tab[msgindex].calcit == NULL)
     159                 :        554 :                 tab = rtnl_msg_handlers[PF_UNSPEC];
     160                 :            : 
     161                 :       1109 :         return tab[msgindex].calcit;
     162                 :            : }
     163                 :            : 
     164                 :            : /**
     165                 :            :  * __rtnl_register - Register a rtnetlink message type
     166                 :            :  * @protocol: Protocol family or PF_UNSPEC
     167                 :            :  * @msgtype: rtnetlink message type
     168                 :            :  * @doit: Function pointer called for each request message
     169                 :            :  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
     170                 :            :  * @calcit: Function pointer to calc size of dump message
     171                 :            :  *
     172                 :            :  * Registers the specified function pointers (at least one of them has
     173                 :            :  * to be non-NULL) to be called whenever a request message for the
     174                 :            :  * specified protocol family and message type is received.
     175                 :            :  *
     176                 :            :  * The special protocol family PF_UNSPEC may be used to define fallback
     177                 :            :  * function pointers for the case when no entry for the specific protocol
     178                 :            :  * family exists.
     179                 :            :  *
     180                 :            :  * Returns 0 on success or a negative error code.
     181                 :            :  */
     182                 :          0 : int __rtnl_register(int protocol, int msgtype,
     183                 :            :                     rtnl_doit_func doit, rtnl_dumpit_func dumpit,
     184                 :            :                     rtnl_calcit_func calcit)
     185                 :            : {
     186                 :            :         struct rtnl_link *tab;
     187                 :            :         int msgindex;
     188                 :            : 
     189         [ #  # ]:          0 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     190                 :            :         msgindex = rtm_msgindex(msgtype);
     191                 :            : 
     192                 :          0 :         tab = rtnl_msg_handlers[protocol];
     193         [ #  # ]:          0 :         if (tab == NULL) {
     194                 :            :                 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
     195         [ #  # ]:          0 :                 if (tab == NULL)
     196                 :            :                         return -ENOBUFS;
     197                 :            : 
     198                 :          0 :                 rtnl_msg_handlers[protocol] = tab;
     199                 :            :         }
     200                 :            : 
     201         [ #  # ]:          0 :         if (doit)
     202                 :          0 :                 tab[msgindex].doit = doit;
     203                 :            : 
     204         [ #  # ]:          0 :         if (dumpit)
     205                 :          0 :                 tab[msgindex].dumpit = dumpit;
     206                 :            : 
     207         [ #  # ]:          0 :         if (calcit)
     208                 :          0 :                 tab[msgindex].calcit = calcit;
     209                 :            : 
     210                 :            :         return 0;
     211                 :            : }
     212                 :            : EXPORT_SYMBOL_GPL(__rtnl_register);
     213                 :            : 
     214                 :            : /**
     215                 :            :  * rtnl_register - Register a rtnetlink message type
     216                 :            :  *
     217                 :            :  * Identical to __rtnl_register() but panics on failure. This is useful
     218                 :            :  * as failure of this function is very unlikely, it can only happen due
     219                 :            :  * to lack of memory when allocating the chain to store all message
     220                 :            :  * handlers for a protocol. Meant for use in init functions where lack
     221                 :            :  * of memory implies no sense in continuing.
     222                 :            :  */
     223                 :          0 : void rtnl_register(int protocol, int msgtype,
     224                 :            :                    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
     225                 :            :                    rtnl_calcit_func calcit)
     226                 :            : {
     227         [ #  # ]:          0 :         if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
     228                 :          0 :                 panic("Unable to register rtnetlink message handler, "
     229                 :            :                       "protocol = %d, message type = %d\n",
     230                 :            :                       protocol, msgtype);
     231                 :          0 : }
     232                 :            : EXPORT_SYMBOL_GPL(rtnl_register);
     233                 :            : 
     234                 :            : /**
     235                 :            :  * rtnl_unregister - Unregister a rtnetlink message type
     236                 :            :  * @protocol: Protocol family or PF_UNSPEC
     237                 :            :  * @msgtype: rtnetlink message type
     238                 :            :  *
     239                 :            :  * Returns 0 on success or a negative error code.
     240                 :            :  */
     241                 :          0 : int rtnl_unregister(int protocol, int msgtype)
     242                 :            : {
     243                 :            :         int msgindex;
     244                 :            : 
     245         [ #  # ]:          0 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     246                 :            :         msgindex = rtm_msgindex(msgtype);
     247                 :            : 
     248         [ #  # ]:          0 :         if (rtnl_msg_handlers[protocol] == NULL)
     249                 :            :                 return -ENOENT;
     250                 :            : 
     251                 :          0 :         rtnl_msg_handlers[protocol][msgindex].doit = NULL;
     252                 :          0 :         rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
     253                 :            : 
     254                 :          0 :         return 0;
     255                 :            : }
     256                 :            : EXPORT_SYMBOL_GPL(rtnl_unregister);
     257                 :            : 
     258                 :            : /**
     259                 :            :  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
     260                 :            :  * @protocol : Protocol family or PF_UNSPEC
     261                 :            :  *
     262                 :            :  * Identical to calling rtnl_unregster() for all registered message types
     263                 :            :  * of a certain protocol family.
     264                 :            :  */
     265                 :          0 : void rtnl_unregister_all(int protocol)
     266                 :            : {
     267         [ #  # ]:          0 :         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
     268                 :            : 
     269                 :          0 :         kfree(rtnl_msg_handlers[protocol]);
     270                 :          0 :         rtnl_msg_handlers[protocol] = NULL;
     271                 :          0 : }
     272                 :            : EXPORT_SYMBOL_GPL(rtnl_unregister_all);
     273                 :            : 
     274                 :            : static LIST_HEAD(link_ops);
     275                 :            : 
     276                 :          0 : static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
     277                 :            : {
     278                 :            :         const struct rtnl_link_ops *ops;
     279                 :            : 
     280         [ #  # ]:          0 :         list_for_each_entry(ops, &link_ops, list) {
     281         [ #  # ]:          0 :                 if (!strcmp(ops->kind, kind))
     282                 :            :                         return ops;
     283                 :            :         }
     284                 :            :         return NULL;
     285                 :            : }
     286                 :            : 
     287                 :            : /**
     288                 :            :  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
     289                 :            :  * @ops: struct rtnl_link_ops * to register
     290                 :            :  *
     291                 :            :  * The caller must hold the rtnl_mutex. This function should be used
     292                 :            :  * by drivers that create devices during module initialization. It
     293                 :            :  * must be called before registering the devices.
     294                 :            :  *
     295                 :            :  * Returns 0 on success or a negative error code.
     296                 :            :  */
     297                 :          0 : int __rtnl_link_register(struct rtnl_link_ops *ops)
     298                 :            : {
     299         [ #  # ]:          0 :         if (rtnl_link_ops_get(ops->kind))
     300                 :            :                 return -EEXIST;
     301                 :            : 
     302         [ #  # ]:          0 :         if (!ops->dellink)
     303                 :          0 :                 ops->dellink = unregister_netdevice_queue;
     304                 :            : 
     305                 :          0 :         list_add_tail(&ops->list, &link_ops);
     306                 :          0 :         return 0;
     307                 :            : }
     308                 :            : EXPORT_SYMBOL_GPL(__rtnl_link_register);
     309                 :            : 
     310                 :            : /**
     311                 :            :  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
     312                 :            :  * @ops: struct rtnl_link_ops * to register
     313                 :            :  *
     314                 :            :  * Returns 0 on success or a negative error code.
     315                 :            :  */
     316                 :          0 : int rtnl_link_register(struct rtnl_link_ops *ops)
     317                 :            : {
     318                 :            :         int err;
     319                 :            : 
     320                 :            :         rtnl_lock();
     321                 :          0 :         err = __rtnl_link_register(ops);
     322                 :            :         rtnl_unlock();
     323                 :          0 :         return err;
     324                 :            : }
     325                 :            : EXPORT_SYMBOL_GPL(rtnl_link_register);
     326                 :            : 
     327                 :          0 : static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
     328                 :            : {
     329                 :            :         struct net_device *dev;
     330                 :          0 :         LIST_HEAD(list_kill);
     331                 :            : 
     332         [ #  # ]:          0 :         for_each_netdev(net, dev) {
     333         [ #  # ]:          0 :                 if (dev->rtnl_link_ops == ops)
     334                 :          0 :                         ops->dellink(dev, &list_kill);
     335                 :            :         }
     336                 :          0 :         unregister_netdevice_many(&list_kill);
     337                 :          0 : }
     338                 :            : 
     339                 :            : /**
     340                 :            :  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
     341                 :            :  * @ops: struct rtnl_link_ops * to unregister
     342                 :            :  *
     343                 :            :  * The caller must hold the rtnl_mutex.
     344                 :            :  */
     345                 :          0 : void __rtnl_link_unregister(struct rtnl_link_ops *ops)
     346                 :            : {
     347                 :            :         struct net *net;
     348                 :            : 
     349         [ #  # ]:          0 :         for_each_net(net) {
     350                 :          0 :                 __rtnl_kill_links(net, ops);
     351                 :            :         }
     352                 :            :         list_del(&ops->list);
     353                 :          0 : }
     354                 :            : EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
     355                 :            : 
     356                 :            : /**
     357                 :            :  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
     358                 :            :  * @ops: struct rtnl_link_ops * to unregister
     359                 :            :  */
     360                 :          0 : void rtnl_link_unregister(struct rtnl_link_ops *ops)
     361                 :            : {
     362                 :            :         rtnl_lock();
     363                 :          0 :         __rtnl_link_unregister(ops);
     364                 :            :         rtnl_unlock();
     365                 :          0 : }
     366                 :            : EXPORT_SYMBOL_GPL(rtnl_link_unregister);
     367                 :            : 
     368                 :          0 : static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
     369                 :            : {
     370                 :            :         struct net_device *master_dev;
     371                 :            :         const struct rtnl_link_ops *ops;
     372                 :            : 
     373                 :          0 :         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
     374         [ #  # ]:          0 :         if (!master_dev)
     375                 :            :                 return 0;
     376                 :          0 :         ops = master_dev->rtnl_link_ops;
     377 [ #  # ][ #  # ]:          0 :         if (!ops || !ops->get_slave_size)
     378                 :            :                 return 0;
     379                 :            :         /* IFLA_INFO_SLAVE_DATA + nested data */
     380                 :          0 :         return nla_total_size(sizeof(struct nlattr)) +
     381                 :          0 :                ops->get_slave_size(master_dev, dev);
     382                 :            : }
     383                 :            : 
     384                 :          0 : static size_t rtnl_link_get_size(const struct net_device *dev)
     385                 :            : {
     386                 :          0 :         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
     387                 :            :         size_t size;
     388                 :            : 
     389         [ #  # ]:          0 :         if (!ops)
     390                 :            :                 return 0;
     391                 :            : 
     392                 :          0 :         size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
     393                 :          0 :                nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
     394                 :            : 
     395         [ #  # ]:          0 :         if (ops->get_size)
     396                 :            :                 /* IFLA_INFO_DATA + nested data */
     397                 :          0 :                 size += nla_total_size(sizeof(struct nlattr)) +
     398                 :          0 :                         ops->get_size(dev);
     399                 :            : 
     400         [ #  # ]:          0 :         if (ops->get_xstats_size)
     401                 :            :                 /* IFLA_INFO_XSTATS */
     402                 :          0 :                 size += nla_total_size(ops->get_xstats_size(dev));
     403                 :            : 
     404                 :          0 :         size += rtnl_link_get_slave_info_data_size(dev);
     405                 :            : 
     406                 :          0 :         return size;
     407                 :            : }
     408                 :            : 
     409                 :            : static LIST_HEAD(rtnl_af_ops);
     410                 :            : 
     411                 :            : static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
     412                 :            : {
     413                 :            :         const struct rtnl_af_ops *ops;
     414                 :            : 
     415 [ #  # ][ #  # ]:          0 :         list_for_each_entry(ops, &rtnl_af_ops, list) {
     416 [ #  # ][ #  # ]:          0 :                 if (ops->family == family)
     417                 :            :                         return ops;
     418                 :            :         }
     419                 :            : 
     420                 :            :         return NULL;
     421                 :            : }
     422                 :            : 
     423                 :            : /**
     424                 :            :  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
     425                 :            :  * @ops: struct rtnl_af_ops * to register
     426                 :            :  *
     427                 :            :  * Returns 0 on success or a negative error code.
     428                 :            :  */
     429                 :          0 : void rtnl_af_register(struct rtnl_af_ops *ops)
     430                 :            : {
     431                 :            :         rtnl_lock();
     432                 :          0 :         list_add_tail(&ops->list, &rtnl_af_ops);
     433                 :            :         rtnl_unlock();
     434                 :          0 : }
     435                 :            : EXPORT_SYMBOL_GPL(rtnl_af_register);
     436                 :            : 
     437                 :            : /**
     438                 :            :  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
     439                 :            :  * @ops: struct rtnl_af_ops * to unregister
     440                 :            :  *
     441                 :            :  * The caller must hold the rtnl_mutex.
     442                 :            :  */
     443                 :          0 : void __rtnl_af_unregister(struct rtnl_af_ops *ops)
     444                 :            : {
     445                 :            :         list_del(&ops->list);
     446                 :          0 : }
     447                 :            : EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
     448                 :            : 
     449                 :            : /**
     450                 :            :  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
     451                 :            :  * @ops: struct rtnl_af_ops * to unregister
     452                 :            :  */
     453                 :          0 : void rtnl_af_unregister(struct rtnl_af_ops *ops)
     454                 :            : {
     455                 :            :         rtnl_lock();
     456                 :            :         __rtnl_af_unregister(ops);
     457                 :            :         rtnl_unlock();
     458                 :          0 : }
     459                 :            : EXPORT_SYMBOL_GPL(rtnl_af_unregister);
     460                 :            : 
     461                 :            : static size_t rtnl_link_get_af_size(const struct net_device *dev)
     462                 :            : {
     463                 :            :         struct rtnl_af_ops *af_ops;
     464                 :            :         size_t size;
     465                 :            : 
     466                 :            :         /* IFLA_AF_SPEC */
     467                 :            :         size = nla_total_size(sizeof(struct nlattr));
     468                 :            : 
     469         [ #  # ]:          0 :         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
     470         [ #  # ]:          0 :                 if (af_ops->get_link_af_size) {
     471                 :            :                         /* AF_* + nested data */
     472                 :          0 :                         size += nla_total_size(sizeof(struct nlattr)) +
     473                 :          0 :                                 af_ops->get_link_af_size(dev);
     474                 :            :                 }
     475                 :            :         }
     476                 :            : 
     477                 :            :         return size;
     478                 :            : }
     479                 :            : 
     480                 :            : static bool rtnl_have_link_slave_info(const struct net_device *dev)
     481                 :            : {
     482                 :            :         struct net_device *master_dev;
     483                 :            : 
     484                 :       1110 :         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
     485 [ -  + ][ #  # ]:       1110 :         if (master_dev && master_dev->rtnl_link_ops)
     486                 :            :                 return true;
     487                 :            :         return false;
     488                 :            : }
     489                 :            : 
     490                 :          0 : static int rtnl_link_slave_info_fill(struct sk_buff *skb,
     491                 :            :                                      const struct net_device *dev)
     492                 :            : {
     493                 :            :         struct net_device *master_dev;
     494                 :            :         const struct rtnl_link_ops *ops;
     495                 :            :         struct nlattr *slave_data;
     496                 :            :         int err;
     497                 :            : 
     498                 :        555 :         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
     499         [ -  + ]:        555 :         if (!master_dev)
     500                 :            :                 return 0;
     501                 :          0 :         ops = master_dev->rtnl_link_ops;
     502         [ #  # ]:          0 :         if (!ops)
     503                 :            :                 return 0;
     504         [ #  # ]:          0 :         if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
     505                 :            :                 return -EMSGSIZE;
     506         [ #  # ]:          0 :         if (ops->fill_slave_info) {
     507                 :            :                 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
     508         [ #  # ]:          0 :                 if (!slave_data)
     509                 :            :                         return -EMSGSIZE;
     510                 :          0 :                 err = ops->fill_slave_info(skb, master_dev, dev);
     511         [ #  # ]:          0 :                 if (err < 0)
     512                 :            :                         goto err_cancel_slave_data;
     513                 :            :                 nla_nest_end(skb, slave_data);
     514                 :            :         }
     515                 :            :         return 0;
     516                 :            : 
     517                 :            : err_cancel_slave_data:
     518                 :            :         nla_nest_cancel(skb, slave_data);
     519                 :          0 :         return err;
     520                 :            : }
     521                 :            : 
     522                 :          0 : static int rtnl_link_info_fill(struct sk_buff *skb,
     523                 :            :                                const struct net_device *dev)
     524                 :            : {
     525                 :        555 :         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
     526                 :            :         struct nlattr *data;
     527                 :            :         int err;
     528                 :            : 
     529         [ +  - ]:        555 :         if (!ops)
     530                 :            :                 return 0;
     531         [ +  - ]:        555 :         if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
     532                 :            :                 return -EMSGSIZE;
     533         [ -  + ]:        555 :         if (ops->fill_xstats) {
     534                 :          0 :                 err = ops->fill_xstats(skb, dev);
     535         [ #  # ]:          0 :                 if (err < 0)
     536                 :            :                         return err;
     537                 :            :         }
     538         [ +  - ]:        555 :         if (ops->fill_info) {
     539                 :            :                 data = nla_nest_start(skb, IFLA_INFO_DATA);
     540         [ +  - ]:        555 :                 if (data == NULL)
     541                 :            :                         return -EMSGSIZE;
     542                 :        555 :                 err = ops->fill_info(skb, dev);
     543         [ +  - ]:        555 :                 if (err < 0)
     544                 :            :                         goto err_cancel_data;
     545                 :            :                 nla_nest_end(skb, data);
     546                 :            :         }
     547                 :            :         return 0;
     548                 :            : 
     549                 :            : err_cancel_data:
     550                 :            :         nla_nest_cancel(skb, data);
     551                 :          0 :         return err;
     552                 :            : }
     553                 :            : 
     554                 :          0 : static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
     555                 :            : {
     556                 :            :         struct nlattr *linkinfo;
     557                 :            :         int err = -EMSGSIZE;
     558                 :            : 
     559                 :            :         linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
     560         [ +  - ]:        555 :         if (linkinfo == NULL)
     561                 :            :                 goto out;
     562                 :            : 
     563                 :        555 :         err = rtnl_link_info_fill(skb, dev);
     564         [ +  - ]:        555 :         if (err < 0)
     565                 :            :                 goto err_cancel_link;
     566                 :            : 
     567                 :        555 :         err = rtnl_link_slave_info_fill(skb, dev);
     568         [ +  - ]:        555 :         if (err < 0)
     569                 :            :                 goto err_cancel_link;
     570                 :            : 
     571                 :            :         nla_nest_end(skb, linkinfo);
     572                 :        555 :         return 0;
     573                 :            : 
     574                 :            : err_cancel_link:
     575                 :            :         nla_nest_cancel(skb, linkinfo);
     576                 :            : out:
     577                 :          0 :         return err;
     578                 :            : }
     579                 :            : 
     580                 :          0 : int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
     581                 :            : {
     582                 :          0 :         struct sock *rtnl = net->rtnl;
     583                 :            :         int err = 0;
     584                 :            : 
     585                 :          0 :         NETLINK_CB(skb).dst_group = group;
     586         [ #  # ]:          0 :         if (echo)
     587                 :          0 :                 atomic_inc(&skb->users);
     588                 :          0 :         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
     589         [ #  # ]:          0 :         if (echo)
     590                 :          0 :                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
     591                 :          0 :         return err;
     592                 :            : }
     593                 :            : 
     594                 :          0 : int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
     595                 :            : {
     596                 :          0 :         struct sock *rtnl = net->rtnl;
     597                 :            : 
     598                 :          0 :         return nlmsg_unicast(rtnl, skb, pid);
     599                 :            : }
     600                 :            : EXPORT_SYMBOL(rtnl_unicast);
     601                 :            : 
     602                 :          0 : void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
     603                 :          0 :                  struct nlmsghdr *nlh, gfp_t flags)
     604                 :            : {
     605                 :        159 :         struct sock *rtnl = net->rtnl;
     606                 :            :         int report = 0;
     607                 :            : 
     608         [ -  + ]:        159 :         if (nlh)
     609                 :            :                 report = nlmsg_report(nlh);
     610                 :            : 
     611                 :        159 :         nlmsg_notify(rtnl, skb, pid, group, report, flags);
     612                 :        159 : }
     613                 :            : EXPORT_SYMBOL(rtnl_notify);
     614                 :            : 
     615                 :          0 : void rtnl_set_sk_err(struct net *net, u32 group, int error)
     616                 :            : {
     617                 :          0 :         struct sock *rtnl = net->rtnl;
     618                 :            : 
     619                 :          0 :         netlink_set_err(rtnl, 0, group, error);
     620                 :          0 : }
     621                 :            : EXPORT_SYMBOL(rtnl_set_sk_err);
     622                 :            : 
     623                 :          0 : int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
     624                 :            : {
     625                 :            :         struct nlattr *mx;
     626                 :            :         int i, valid = 0;
     627                 :            : 
     628                 :            :         mx = nla_nest_start(skb, RTA_METRICS);
     629            [ + ]:         10 :         if (mx == NULL)
     630                 :            :                 return -ENOBUFS;
     631                 :            : 
     632         [ +  + ]:        160 :         for (i = 0; i < RTAX_MAX; i++) {
     633         [ -  + ]:        150 :                 if (metrics[i]) {
     634                 :          0 :                         valid++;
     635         [ #  # ]:          0 :                         if (nla_put_u32(skb, i+1, metrics[i]))
     636                 :            :                                 goto nla_put_failure;
     637                 :            :                 }
     638                 :            :         }
     639                 :            : 
     640         [ +  - ]:         10 :         if (!valid) {
     641                 :            :                 nla_nest_cancel(skb, mx);
     642                 :            :                 return 0;
     643                 :            :         }
     644                 :            : 
     645                 :          0 :         return nla_nest_end(skb, mx);
     646                 :            : 
     647                 :            : nla_put_failure:
     648                 :            :         nla_nest_cancel(skb, mx);
     649                 :            :         return -EMSGSIZE;
     650                 :            : }
     651                 :            : EXPORT_SYMBOL(rtnetlink_put_metrics);
     652                 :            : 
     653                 :          0 : int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
     654                 :            :                        long expires, u32 error)
     655                 :            : {
     656                 :         40 :         struct rta_cacheinfo ci = {
     657                 :         10 :                 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
     658                 :         10 :                 .rta_used = dst->__use,
     659                 :         10 :                 .rta_clntref = atomic_read(&(dst->__refcnt)),
     660                 :            :                 .rta_error = error,
     661                 :            :                 .rta_id =  id,
     662                 :            :         };
     663                 :            : 
     664         [ -  + ]:         10 :         if (expires) {
     665                 :            :                 unsigned long clock;
     666                 :            : 
     667                 :          0 :                 clock = jiffies_to_clock_t(abs(expires));
     668                 :          0 :                 clock = min_t(unsigned long, clock, INT_MAX);
     669         [ #  # ]:         10 :                 ci.rta_expires = (expires > 0) ? clock : -clock;
     670                 :            :         }
     671                 :         10 :         return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
     672                 :            : }
     673                 :            : EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
     674                 :            : 
     675                 :          0 : static void set_operstate(struct net_device *dev, unsigned char transition)
     676                 :            : {
     677                 :          0 :         unsigned char operstate = dev->operstate;
     678                 :            : 
     679      [ #  #  # ]:          0 :         switch (transition) {
     680                 :            :         case IF_OPER_UP:
     681         [ #  # ]:          0 :                 if ((operstate == IF_OPER_DORMANT ||
     682         [ #  # ]:          0 :                      operstate == IF_OPER_UNKNOWN) &&
     683                 :            :                     !netif_dormant(dev))
     684                 :            :                         operstate = IF_OPER_UP;
     685                 :            :                 break;
     686                 :            : 
     687                 :            :         case IF_OPER_DORMANT:
     688         [ #  # ]:          0 :                 if (operstate == IF_OPER_UP ||
     689                 :          0 :                     operstate == IF_OPER_UNKNOWN)
     690                 :            :                         operstate = IF_OPER_DORMANT;
     691                 :            :                 break;
     692                 :            :         }
     693                 :            : 
     694         [ #  # ]:          0 :         if (dev->operstate != operstate) {
     695                 :          0 :                 write_lock_bh(&dev_base_lock);
     696                 :          0 :                 dev->operstate = operstate;
     697                 :          0 :                 write_unlock_bh(&dev_base_lock);
     698                 :          0 :                 netdev_state_change(dev);
     699                 :            :         }
     700                 :          0 : }
     701                 :            : 
     702                 :            : static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
     703                 :            : {
     704                 :          1 :         return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
     705                 :            :                (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
     706                 :            : }
     707                 :            : 
     708                 :          1 : static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
     709                 :            :                                            const struct ifinfomsg *ifm)
     710                 :            : {
     711                 :            :         unsigned int flags = ifm->ifi_flags;
     712                 :            : 
     713                 :            :         /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
     714 [ #  # ][ +  - ]:          1 :         if (ifm->ifi_change)
     715                 :          1 :                 flags = (flags & ifm->ifi_change) |
     716                 :          1 :                         (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
     717                 :            : 
     718                 :            :         return flags;
     719                 :            : }
     720                 :            : 
     721                 :          0 : static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
     722                 :            :                                  const struct rtnl_link_stats64 *b)
     723                 :            : {
     724                 :       1665 :         a->rx_packets = b->rx_packets;
     725                 :       1665 :         a->tx_packets = b->tx_packets;
     726                 :       1665 :         a->rx_bytes = b->rx_bytes;
     727                 :       1665 :         a->tx_bytes = b->tx_bytes;
     728                 :       1665 :         a->rx_errors = b->rx_errors;
     729                 :       1665 :         a->tx_errors = b->tx_errors;
     730                 :       1665 :         a->rx_dropped = b->rx_dropped;
     731                 :       1665 :         a->tx_dropped = b->tx_dropped;
     732                 :            : 
     733                 :       1665 :         a->multicast = b->multicast;
     734                 :       1665 :         a->collisions = b->collisions;
     735                 :            : 
     736                 :       1665 :         a->rx_length_errors = b->rx_length_errors;
     737                 :       1665 :         a->rx_over_errors = b->rx_over_errors;
     738                 :       1665 :         a->rx_crc_errors = b->rx_crc_errors;
     739                 :       1665 :         a->rx_frame_errors = b->rx_frame_errors;
     740                 :       1665 :         a->rx_fifo_errors = b->rx_fifo_errors;
     741                 :       1665 :         a->rx_missed_errors = b->rx_missed_errors;
     742                 :            : 
     743                 :       1665 :         a->tx_aborted_errors = b->tx_aborted_errors;
     744                 :       1665 :         a->tx_carrier_errors = b->tx_carrier_errors;
     745                 :       1665 :         a->tx_fifo_errors = b->tx_fifo_errors;
     746                 :       1665 :         a->tx_heartbeat_errors = b->tx_heartbeat_errors;
     747                 :       1665 :         a->tx_window_errors = b->tx_window_errors;
     748                 :            : 
     749                 :       1665 :         a->rx_compressed = b->rx_compressed;
     750                 :       1665 :         a->tx_compressed = b->tx_compressed;
     751                 :       1665 : }
     752                 :            : 
     753                 :            : static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
     754                 :            : {
     755                 :       1665 :         memcpy(v, b, sizeof(*b));
     756                 :            : }
     757                 :            : 
     758                 :            : /* All VF info */
     759                 :            : static inline int rtnl_vfinfo_size(const struct net_device *dev,
     760                 :            :                                    u32 ext_filter_mask)
     761                 :            : {
     762                 :            :         if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
     763                 :            :             (ext_filter_mask & RTEXT_FILTER_VF)) {
     764                 :            :                 int num_vfs = dev_num_vf(dev->dev.parent);
     765                 :            :                 size_t size = nla_total_size(sizeof(struct nlattr));
     766                 :            :                 size += nla_total_size(num_vfs * sizeof(struct nlattr));
     767                 :            :                 size += num_vfs *
     768                 :            :                         (nla_total_size(sizeof(struct ifla_vf_mac)) +
     769                 :            :                          nla_total_size(sizeof(struct ifla_vf_vlan)) +
     770                 :            :                          nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
     771                 :            :                          nla_total_size(sizeof(struct ifla_vf_spoofchk)));
     772                 :            :                 return size;
     773                 :            :         } else
     774                 :            :                 return 0;
     775                 :            : }
     776                 :            : 
     777                 :            : static size_t rtnl_port_size(const struct net_device *dev)
     778                 :            : {
     779                 :            :         size_t port_size = nla_total_size(4)            /* PORT_VF */
     780                 :            :                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
     781                 :            :                 + nla_total_size(sizeof(struct ifla_port_vsi))
     782                 :            :                                                         /* PORT_VSI_TYPE */
     783                 :            :                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
     784                 :            :                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
     785                 :            :                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
     786                 :            :                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
     787                 :            :         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
     788                 :            :         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
     789                 :            :                 + port_size;
     790                 :            :         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
     791                 :            :                 + port_size;
     792                 :            : 
     793 [ #  # ][ #  # ]:          0 :         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
     794                 :            :                 return 0;
     795                 :            :         if (dev_num_vf(dev->dev.parent))
     796                 :            :                 return port_self_size + vf_ports_size +
     797                 :            :                         vf_port_size * dev_num_vf(dev->dev.parent);
     798                 :            :         else
     799                 :            :                 return port_self_size;
     800                 :            : }
     801                 :            : 
     802                 :          0 : static noinline size_t if_nlmsg_size(const struct net_device *dev,
     803                 :            :                                      u32 ext_filter_mask)
     804                 :            : {
     805                 :          0 :         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
     806                 :            :                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
     807                 :            :                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
     808                 :            :                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
     809                 :            :                + nla_total_size(sizeof(struct rtnl_link_ifmap))
     810                 :            :                + nla_total_size(sizeof(struct rtnl_link_stats))
     811                 :            :                + nla_total_size(sizeof(struct rtnl_link_stats64))
     812                 :            :                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
     813                 :            :                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
     814                 :            :                + nla_total_size(4) /* IFLA_TXQLEN */
     815                 :            :                + nla_total_size(4) /* IFLA_WEIGHT */
     816                 :            :                + nla_total_size(4) /* IFLA_MTU */
     817                 :            :                + nla_total_size(4) /* IFLA_LINK */
     818                 :            :                + nla_total_size(4) /* IFLA_MASTER */
     819                 :            :                + nla_total_size(1) /* IFLA_CARRIER */
     820                 :            :                + nla_total_size(4) /* IFLA_PROMISCUITY */
     821                 :            :                + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
     822                 :            :                + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
     823                 :            :                + nla_total_size(1) /* IFLA_OPERSTATE */
     824                 :            :                + nla_total_size(1) /* IFLA_LINKMODE */
     825         [ #  # ]:          0 :                + nla_total_size(ext_filter_mask
     826                 :          0 :                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
     827                 :            :                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
     828                 :          0 :                + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
     829                 :          0 :                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
     830                 :          0 :                + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
     831                 :            :                + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
     832                 :            : }
     833                 :            : 
     834                 :            : static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
     835                 :            : {
     836                 :            :         struct nlattr *vf_ports;
     837                 :            :         struct nlattr *vf_port;
     838                 :            :         int vf;
     839                 :            :         int err;
     840                 :            : 
     841                 :            :         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
     842                 :            :         if (!vf_ports)
     843                 :            :                 return -EMSGSIZE;
     844                 :            : 
     845                 :            :         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
     846                 :            :                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
     847                 :            :                 if (!vf_port)
     848                 :            :                         goto nla_put_failure;
     849                 :            :                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
     850                 :            :                         goto nla_put_failure;
     851                 :            :                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
     852                 :            :                 if (err == -EMSGSIZE)
     853                 :            :                         goto nla_put_failure;
     854                 :            :                 if (err) {
     855                 :            :                         nla_nest_cancel(skb, vf_port);
     856                 :            :                         continue;
     857                 :            :                 }
     858                 :            :                 nla_nest_end(skb, vf_port);
     859                 :            :         }
     860                 :            : 
     861                 :            :         nla_nest_end(skb, vf_ports);
     862                 :            : 
     863                 :            :         return 0;
     864                 :            : 
     865                 :            : nla_put_failure:
     866                 :            :         nla_nest_cancel(skb, vf_ports);
     867                 :            :         return -EMSGSIZE;
     868                 :            : }
     869                 :            : 
     870                 :          0 : static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
     871                 :            : {
     872                 :            :         struct nlattr *port_self;
     873                 :            :         int err;
     874                 :            : 
     875                 :            :         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
     876         [ #  # ]:          0 :         if (!port_self)
     877                 :            :                 return -EMSGSIZE;
     878                 :            : 
     879                 :          0 :         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
     880         [ #  # ]:          0 :         if (err) {
     881                 :            :                 nla_nest_cancel(skb, port_self);
     882         [ #  # ]:          0 :                 return (err == -EMSGSIZE) ? err : 0;
     883                 :            :         }
     884                 :            : 
     885                 :            :         nla_nest_end(skb, port_self);
     886                 :            : 
     887                 :          0 :         return 0;
     888                 :            : }
     889                 :            : 
     890                 :          0 : static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
     891                 :            : {
     892                 :            :         int err;
     893                 :            : 
     894 [ -  + ][ #  # ]:       1665 :         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
     895                 :            :                 return 0;
     896                 :            : 
     897                 :          0 :         err = rtnl_port_self_fill(skb, dev);
     898         [ #  # ]:          0 :         if (err)
     899                 :          0 :                 return err;
     900                 :            : 
     901                 :            :         if (dev_num_vf(dev->dev.parent)) {
     902                 :            :                 err = rtnl_vf_ports_fill(skb, dev);
     903                 :            :                 if (err)
     904                 :            :                         return err;
     905                 :            :         }
     906                 :            : 
     907                 :            :         return 0;
     908                 :            : }
     909                 :            : 
     910                 :          0 : static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
     911                 :            : {
     912                 :            :         int err;
     913                 :            :         struct netdev_phys_port_id ppid;
     914                 :            : 
     915                 :       1665 :         err = dev_get_phys_port_id(dev, &ppid);
     916         [ +  - ]:       1665 :         if (err) {
     917         [ -  + ]:       1665 :                 if (err == -EOPNOTSUPP)
     918                 :            :                         return 0;
     919                 :          0 :                 return err;
     920                 :            :         }
     921                 :            : 
     922         [ #  # ]:          0 :         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
     923                 :            :                 return -EMSGSIZE;
     924                 :            : 
     925                 :          0 :         return 0;
     926                 :            : }
     927                 :            : 
     928                 :          0 : static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
     929                 :            :                             int type, u32 pid, u32 seq, u32 change,
     930                 :            :                             unsigned int flags, u32 ext_filter_mask)
     931                 :            : {
     932                 :            :         struct ifinfomsg *ifm;
     933                 :            :         struct nlmsghdr *nlh;
     934                 :            :         struct rtnl_link_stats64 temp;
     935                 :            :         const struct rtnl_link_stats64 *stats;
     936                 :            :         struct nlattr *attr, *af_spec;
     937                 :            :         struct rtnl_af_ops *af_ops;
     938                 :       1665 :         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
     939                 :            : 
     940         [ -  + ]:       1665 :         ASSERT_RTNL();
     941                 :       1665 :         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
     942         [ +  - ]:       1665 :         if (nlh == NULL)
     943                 :            :                 return -EMSGSIZE;
     944                 :            : 
     945                 :            :         ifm = nlmsg_data(nlh);
     946                 :       1665 :         ifm->ifi_family = AF_UNSPEC;
     947                 :       1665 :         ifm->__ifi_pad = 0;
     948                 :       1665 :         ifm->ifi_type = dev->type;
     949                 :       1665 :         ifm->ifi_index = dev->ifindex;
     950                 :       1665 :         ifm->ifi_flags = dev_get_flags(dev);
     951                 :       1665 :         ifm->ifi_change = change;
     952                 :            : 
     953   [ +  -  +  - ]:       3330 :         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
     954         [ +  - ]:       1665 :             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
     955         [ +  + ]:       1665 :             nla_put_u8(skb, IFLA_OPERSTATE,
     956         [ +  - ]:       1665 :                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
     957         [ +  - ]:       1665 :             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
     958         [ +  - ]:       1665 :             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
     959         [ +  - ]:       1665 :             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
     960         [ +  - ]:       1665 :             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
     961         [ +  - ]:       1665 :             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
     962                 :            : #ifdef CONFIG_RPS
     963         [ -  + ]:       1665 :             nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
     964                 :            : #endif
     965         [ #  # ]:          0 :             (dev->ifindex != dev->iflink &&
     966         [ -  - ]:       1665 :              nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
     967         [ #  # ]:          0 :             (upper_dev &&
     968         [ +  - ]:       1665 :              nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
     969         [ +  - ]:       1665 :             nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
     970         [ +  - ]:       1665 :             (dev->qdisc &&
     971         [ -  + ]:       1665 :              nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
     972         [ #  # ]:          0 :             (dev->ifalias &&
     973                 :            :              nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
     974                 :            :                 goto nla_put_failure;
     975                 :            : 
     976                 :            :         if (1) {
     977                 :       9990 :                 struct rtnl_link_ifmap map = {
     978                 :       1665 :                         .mem_start   = dev->mem_start,
     979                 :       1665 :                         .mem_end     = dev->mem_end,
     980                 :       1665 :                         .base_addr   = dev->base_addr,
     981                 :       1665 :                         .irq         = dev->irq,
     982                 :       1665 :                         .dma         = dev->dma,
     983                 :       1665 :                         .port        = dev->if_port,
     984                 :            :                 };
     985         [ +  - ]:       1665 :                 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
     986                 :            :                         goto nla_put_failure;
     987                 :            :         }
     988                 :            : 
     989         [ +  - ]:       1665 :         if (dev->addr_len) {
     990   [ +  -  +  - ]:       3330 :                 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
     991                 :       1665 :                     nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
     992                 :            :                         goto nla_put_failure;
     993                 :            :         }
     994                 :            : 
     995         [ +  - ]:       1665 :         if (rtnl_phys_port_id_fill(skb, dev))
     996                 :            :                 goto nla_put_failure;
     997                 :            : 
     998                 :       1665 :         attr = nla_reserve(skb, IFLA_STATS,
     999                 :            :                         sizeof(struct rtnl_link_stats));
    1000         [ +  - ]:       1665 :         if (attr == NULL)
    1001                 :            :                 goto nla_put_failure;
    1002                 :            : 
    1003                 :       1665 :         stats = dev_get_stats(dev, &temp);
    1004                 :       1665 :         copy_rtnl_link_stats(nla_data(attr), stats);
    1005                 :            : 
    1006                 :       1665 :         attr = nla_reserve(skb, IFLA_STATS64,
    1007                 :            :                         sizeof(struct rtnl_link_stats64));
    1008         [ +  - ]:       1665 :         if (attr == NULL)
    1009                 :            :                 goto nla_put_failure;
    1010                 :       1665 :         copy_rtnl_link_stats64(nla_data(attr), stats);
    1011                 :            : 
    1012         [ +  + ]:       1665 :         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
           [ -  +  #  # ]
    1013                 :            :             nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
    1014                 :            :                 goto nla_put_failure;
    1015                 :            : 
    1016 [ -  + ][ #  # ]:       1665 :         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
    1017         [ #  # ]:          0 :             && (ext_filter_mask & RTEXT_FILTER_VF)) {
    1018                 :            :                 int i;
    1019                 :            : 
    1020                 :            :                 struct nlattr *vfinfo, *vf;
    1021                 :            :                 int num_vfs = dev_num_vf(dev->dev.parent);
    1022                 :            : 
    1023                 :            :                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
    1024         [ #  # ]:          0 :                 if (!vfinfo)
    1025                 :            :                         goto nla_put_failure;
    1026                 :            :                 for (i = 0; i < num_vfs; i++) {
    1027                 :            :                         struct ifla_vf_info ivi;
    1028                 :            :                         struct ifla_vf_mac vf_mac;
    1029                 :            :                         struct ifla_vf_vlan vf_vlan;
    1030                 :            :                         struct ifla_vf_tx_rate vf_tx_rate;
    1031                 :            :                         struct ifla_vf_spoofchk vf_spoofchk;
    1032                 :            :                         struct ifla_vf_link_state vf_linkstate;
    1033                 :            : 
    1034                 :            :                         /*
    1035                 :            :                          * Not all SR-IOV capable drivers support the
    1036                 :            :                          * spoofcheck query.  Preset to -1 so the user
    1037                 :            :                          * space tool can detect that the driver didn't
    1038                 :            :                          * report anything.
    1039                 :            :                          */
    1040                 :            :                         ivi.spoofchk = -1;
    1041                 :            :                         memset(ivi.mac, 0, sizeof(ivi.mac));
    1042                 :            :                         /* The default value for VF link state is "auto"
    1043                 :            :                          * IFLA_VF_LINK_STATE_AUTO which equals zero
    1044                 :            :                          */
    1045                 :            :                         ivi.linkstate = 0;
    1046                 :            :                         if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
    1047                 :            :                                 break;
    1048                 :            :                         vf_mac.vf =
    1049                 :            :                                 vf_vlan.vf =
    1050                 :            :                                 vf_tx_rate.vf =
    1051                 :            :                                 vf_spoofchk.vf =
    1052                 :            :                                 vf_linkstate.vf = ivi.vf;
    1053                 :            : 
    1054                 :            :                         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
    1055                 :            :                         vf_vlan.vlan = ivi.vlan;
    1056                 :            :                         vf_vlan.qos = ivi.qos;
    1057                 :            :                         vf_tx_rate.rate = ivi.tx_rate;
    1058                 :            :                         vf_spoofchk.setting = ivi.spoofchk;
    1059                 :            :                         vf_linkstate.link_state = ivi.linkstate;
    1060                 :            :                         vf = nla_nest_start(skb, IFLA_VF_INFO);
    1061                 :            :                         if (!vf) {
    1062                 :            :                                 nla_nest_cancel(skb, vfinfo);
    1063                 :            :                                 goto nla_put_failure;
    1064                 :            :                         }
    1065                 :            :                         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
    1066                 :            :                             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
    1067                 :            :                             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
    1068                 :            :                                     &vf_tx_rate) ||
    1069                 :            :                             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
    1070                 :            :                                     &vf_spoofchk) ||
    1071                 :            :                             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
    1072                 :            :                                     &vf_linkstate))
    1073                 :            :                                 goto nla_put_failure;
    1074                 :            :                         nla_nest_end(skb, vf);
    1075                 :            :                 }
    1076                 :            :                 nla_nest_end(skb, vfinfo);
    1077                 :            :         }
    1078                 :            : 
    1079         [ +  - ]:       1665 :         if (rtnl_port_fill(skb, dev))
    1080                 :            :                 goto nla_put_failure;
    1081                 :            : 
    1082 [ +  + ][ -  + ]:       2775 :         if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
    1083         [ +  - ]:        555 :                 if (rtnl_link_fill(skb, dev) < 0)
    1084                 :            :                         goto nla_put_failure;
    1085                 :            :         }
    1086                 :            : 
    1087         [ +  - ]:       1665 :         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
    1088                 :            :                 goto nla_put_failure;
    1089                 :            : 
    1090         [ +  + ]:       4995 :         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
    1091         [ +  - ]:       3330 :                 if (af_ops->fill_link_af) {
    1092                 :            :                         struct nlattr *af;
    1093                 :            :                         int err;
    1094                 :            : 
    1095         [ +  - ]:       3330 :                         if (!(af = nla_nest_start(skb, af_ops->family)))
    1096                 :            :                                 goto nla_put_failure;
    1097                 :            : 
    1098                 :       3330 :                         err = af_ops->fill_link_af(skb, dev);
    1099                 :            : 
    1100                 :            :                         /*
    1101                 :            :                          * Caller may return ENODATA to indicate that there
    1102                 :            :                          * was no data to be dumped. This is not an error, it
    1103                 :            :                          * means we should trim the attribute header and
    1104                 :            :                          * continue.
    1105                 :            :                          */
    1106         [ -  + ]:       3330 :                         if (err == -ENODATA)
    1107                 :            :                                 nla_nest_cancel(skb, af);
    1108         [ +  - ]:       3330 :                         else if (err < 0)
    1109                 :            :                                 goto nla_put_failure;
    1110                 :            : 
    1111                 :            :                         nla_nest_end(skb, af);
    1112                 :            :                 }
    1113                 :            :         }
    1114                 :            : 
    1115                 :            :         nla_nest_end(skb, af_spec);
    1116                 :            : 
    1117                 :       1665 :         return nlmsg_end(skb, nlh);
    1118                 :            : 
    1119                 :            : nla_put_failure:
    1120                 :            :         nlmsg_cancel(skb, nlh);
    1121                 :            :         return -EMSGSIZE;
    1122                 :            : }
    1123                 :            : 
    1124                 :          0 : static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
    1125                 :            : {
    1126                 :            :         struct net *net = sock_net(skb->sk);
    1127                 :            :         int h, s_h;
    1128                 :            :         int idx = 0, s_idx;
    1129                 :            :         struct net_device *dev;
    1130                 :            :         struct hlist_head *head;
    1131                 :            :         struct nlattr *tb[IFLA_MAX+1];
    1132                 :            :         u32 ext_filter_mask = 0;
    1133                 :            : 
    1134                 :       1110 :         s_h = cb->args[0];
    1135                 :       1110 :         s_idx = cb->args[1];
    1136                 :            : 
    1137                 :            :         rcu_read_lock();
    1138                 :       1110 :         cb->seq = net->dev_base_seq;
    1139                 :            : 
    1140         [ -  + ]:       1110 :         if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
    1141                 :            :                         ifla_policy) >= 0) {
    1142                 :            : 
    1143         [ #  # ]:          0 :                 if (tb[IFLA_EXT_MASK])
    1144                 :            :                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
    1145                 :            :         }
    1146                 :            : 
    1147         [ +  + ]:     143190 :         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
    1148                 :            :                 idx = 0;
    1149                 :     142080 :                 head = &net->dev_index_head[h];
    1150 [ +  + ][ -  + ]:     144855 :                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
                 [ +  + ]
    1151         [ +  + ]:       1665 :                         if (idx < s_idx)
    1152                 :            :                                 goto cont;
    1153         [ +  - ]:        555 :                         if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
    1154                 :        555 :                                              NETLINK_CB(cb->skb).portid,
    1155                 :        555 :                                              cb->nlh->nlmsg_seq, 0,
    1156                 :            :                                              NLM_F_MULTI,
    1157                 :            :                                              ext_filter_mask) <= 0)
    1158                 :            :                                 goto out;
    1159                 :            : 
    1160                 :            :                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
    1161                 :            : cont:
    1162                 :       1665 :                         idx++;
    1163                 :            :                 }
    1164                 :            :         }
    1165                 :            : out:
    1166                 :            :         rcu_read_unlock();
    1167                 :       1110 :         cb->args[1] = idx;
    1168                 :       1110 :         cb->args[0] = h;
    1169                 :            : 
    1170                 :       1110 :         return skb->len;
    1171                 :            : }
    1172                 :            : 
    1173                 :            : const struct nla_policy ifla_policy[IFLA_MAX+1] = {
    1174                 :            :         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
    1175                 :            :         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
    1176                 :            :         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
    1177                 :            :         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
    1178                 :            :         [IFLA_MTU]              = { .type = NLA_U32 },
    1179                 :            :         [IFLA_LINK]             = { .type = NLA_U32 },
    1180                 :            :         [IFLA_MASTER]           = { .type = NLA_U32 },
    1181                 :            :         [IFLA_CARRIER]          = { .type = NLA_U8 },
    1182                 :            :         [IFLA_TXQLEN]           = { .type = NLA_U32 },
    1183                 :            :         [IFLA_WEIGHT]           = { .type = NLA_U32 },
    1184                 :            :         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
    1185                 :            :         [IFLA_LINKMODE]         = { .type = NLA_U8 },
    1186                 :            :         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
    1187                 :            :         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
    1188                 :            :         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
    1189                 :            :         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
    1190                 :            :         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
    1191                 :            :         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
    1192                 :            :         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
    1193                 :            :         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
    1194                 :            :         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
    1195                 :            :         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
    1196                 :            :         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
    1197                 :            :         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
    1198                 :            :         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
    1199                 :            : };
    1200                 :            : EXPORT_SYMBOL(ifla_policy);
    1201                 :            : 
    1202                 :            : static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
    1203                 :            :         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
    1204                 :            :         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
    1205                 :            :         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
    1206                 :            :         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
    1207                 :            : };
    1208                 :            : 
    1209                 :            : static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
    1210                 :            :         [IFLA_VF_INFO]          = { .type = NLA_NESTED },
    1211                 :            : };
    1212                 :            : 
    1213                 :            : static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
    1214                 :            :         [IFLA_VF_MAC]           = { .type = NLA_BINARY,
    1215                 :            :                                     .len = sizeof(struct ifla_vf_mac) },
    1216                 :            :         [IFLA_VF_VLAN]          = { .type = NLA_BINARY,
    1217                 :            :                                     .len = sizeof(struct ifla_vf_vlan) },
    1218                 :            :         [IFLA_VF_TX_RATE]       = { .type = NLA_BINARY,
    1219                 :            :                                     .len = sizeof(struct ifla_vf_tx_rate) },
    1220                 :            :         [IFLA_VF_SPOOFCHK]      = { .type = NLA_BINARY,
    1221                 :            :                                     .len = sizeof(struct ifla_vf_spoofchk) },
    1222                 :            : };
    1223                 :            : 
    1224                 :            : static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
    1225                 :            :         [IFLA_PORT_VF]          = { .type = NLA_U32 },
    1226                 :            :         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
    1227                 :            :                                     .len = PORT_PROFILE_MAX },
    1228                 :            :         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
    1229                 :            :                                     .len = sizeof(struct ifla_port_vsi)},
    1230                 :            :         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
    1231                 :            :                                       .len = PORT_UUID_MAX },
    1232                 :            :         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
    1233                 :            :                                     .len = PORT_UUID_MAX },
    1234                 :            :         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
    1235                 :            :         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
    1236                 :            : };
    1237                 :            : 
    1238                 :          0 : struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
    1239                 :            : {
    1240                 :            :         struct net *net;
    1241                 :            :         /* Examine the link attributes and figure out which
    1242                 :            :          * network namespace we are talking about.
    1243                 :            :          */
    1244         [ #  # ]:          0 :         if (tb[IFLA_NET_NS_PID])
    1245                 :          0 :                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
    1246         [ #  # ]:          0 :         else if (tb[IFLA_NET_NS_FD])
    1247                 :          0 :                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
    1248                 :            :         else
    1249                 :            :                 net = get_net(src_net);
    1250                 :          0 :         return net;
    1251                 :            : }
    1252                 :            : EXPORT_SYMBOL(rtnl_link_get_net);
    1253                 :            : 
    1254                 :          0 : static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
    1255                 :            : {
    1256         [ +  + ]:          2 :         if (dev) {
    1257 [ -  + ][ #  # ]:          1 :                 if (tb[IFLA_ADDRESS] &&
    1258                 :          0 :                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
    1259                 :            :                         return -EINVAL;
    1260                 :            : 
    1261 [ -  + ][ #  # ]:          1 :                 if (tb[IFLA_BROADCAST] &&
    1262                 :          0 :                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
    1263                 :            :                         return -EINVAL;
    1264                 :            :         }
    1265                 :            : 
    1266         [ -  + ]:          2 :         if (tb[IFLA_AF_SPEC]) {
    1267                 :          0 :                 struct nlattr *af;
    1268                 :            :                 int rem, err;
    1269                 :            : 
    1270         [ #  # ]:          0 :                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
    1271                 :            :                         const struct rtnl_af_ops *af_ops;
    1272                 :            : 
    1273         [ #  # ]:          0 :                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
    1274                 :            :                                 return -EAFNOSUPPORT;
    1275                 :            : 
    1276         [ #  # ]:          0 :                         if (!af_ops->set_link_af)
    1277                 :            :                                 return -EOPNOTSUPP;
    1278                 :            : 
    1279         [ #  # ]:          0 :                         if (af_ops->validate_link_af) {
    1280                 :          0 :                                 err = af_ops->validate_link_af(dev, af);
    1281         [ #  # ]:          0 :                                 if (err < 0)
    1282                 :            :                                         return err;
    1283                 :            :                         }
    1284                 :            :                 }
    1285                 :            :         }
    1286                 :            : 
    1287                 :            :         return 0;
    1288                 :            : }
    1289                 :            : 
    1290                 :          0 : static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
    1291                 :            : {
    1292                 :            :         int rem, err = -EINVAL;
    1293                 :          0 :         struct nlattr *vf;
    1294                 :          0 :         const struct net_device_ops *ops = dev->netdev_ops;
    1295                 :            : 
    1296         [ #  # ]:          0 :         nla_for_each_nested(vf, attr, rem) {
    1297   [ #  #  #  #  :          0 :                 switch (nla_type(vf)) {
                   #  # ]
    1298                 :            :                 case IFLA_VF_MAC: {
    1299                 :            :                         struct ifla_vf_mac *ivm;
    1300                 :            :                         ivm = nla_data(vf);
    1301                 :            :                         err = -EOPNOTSUPP;
    1302         [ #  # ]:          0 :                         if (ops->ndo_set_vf_mac)
    1303                 :          0 :                                 err = ops->ndo_set_vf_mac(dev, ivm->vf,
    1304                 :          0 :                                                           ivm->mac);
    1305                 :            :                         break;
    1306                 :            :                 }
    1307                 :            :                 case IFLA_VF_VLAN: {
    1308                 :            :                         struct ifla_vf_vlan *ivv;
    1309                 :            :                         ivv = nla_data(vf);
    1310                 :            :                         err = -EOPNOTSUPP;
    1311         [ #  # ]:          0 :                         if (ops->ndo_set_vf_vlan)
    1312                 :          0 :                                 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
    1313                 :          0 :                                                            ivv->vlan,
    1314                 :          0 :                                                            ivv->qos);
    1315                 :            :                         break;
    1316                 :            :                 }
    1317                 :            :                 case IFLA_VF_TX_RATE: {
    1318                 :            :                         struct ifla_vf_tx_rate *ivt;
    1319                 :            :                         ivt = nla_data(vf);
    1320                 :            :                         err = -EOPNOTSUPP;
    1321         [ #  # ]:          0 :                         if (ops->ndo_set_vf_tx_rate)
    1322                 :          0 :                                 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
    1323                 :          0 :                                                               ivt->rate);
    1324                 :            :                         break;
    1325                 :            :                 }
    1326                 :            :                 case IFLA_VF_SPOOFCHK: {
    1327                 :            :                         struct ifla_vf_spoofchk *ivs;
    1328                 :            :                         ivs = nla_data(vf);
    1329                 :            :                         err = -EOPNOTSUPP;
    1330         [ #  # ]:          0 :                         if (ops->ndo_set_vf_spoofchk)
    1331                 :          0 :                                 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
    1332                 :          0 :                                                                ivs->setting);
    1333                 :            :                         break;
    1334                 :            :                 }
    1335                 :            :                 case IFLA_VF_LINK_STATE: {
    1336                 :            :                         struct ifla_vf_link_state *ivl;
    1337                 :            :                         ivl = nla_data(vf);
    1338                 :            :                         err = -EOPNOTSUPP;
    1339         [ #  # ]:          0 :                         if (ops->ndo_set_vf_link_state)
    1340                 :          0 :                                 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
    1341                 :          0 :                                                                  ivl->link_state);
    1342                 :            :                         break;
    1343                 :            :                 }
    1344                 :            :                 default:
    1345                 :            :                         err = -EINVAL;
    1346                 :            :                         break;
    1347                 :            :                 }
    1348         [ #  # ]:          0 :                 if (err)
    1349                 :            :                         break;
    1350                 :            :         }
    1351                 :          0 :         return err;
    1352                 :            : }
    1353                 :            : 
    1354                 :          0 : static int do_set_master(struct net_device *dev, int ifindex)
    1355                 :            : {
    1356                 :          0 :         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
    1357                 :            :         const struct net_device_ops *ops;
    1358                 :            :         int err;
    1359                 :            : 
    1360         [ #  # ]:          0 :         if (upper_dev) {
    1361         [ #  # ]:          0 :                 if (upper_dev->ifindex == ifindex)
    1362                 :            :                         return 0;
    1363                 :          0 :                 ops = upper_dev->netdev_ops;
    1364         [ #  # ]:          0 :                 if (ops->ndo_del_slave) {
    1365                 :          0 :                         err = ops->ndo_del_slave(upper_dev, dev);
    1366         [ #  # ]:          0 :                         if (err)
    1367                 :            :                                 return err;
    1368                 :            :                 } else {
    1369                 :            :                         return -EOPNOTSUPP;
    1370                 :            :                 }
    1371                 :            :         }
    1372                 :            : 
    1373         [ #  # ]:          0 :         if (ifindex) {
    1374                 :          0 :                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
    1375         [ #  # ]:          0 :                 if (!upper_dev)
    1376                 :            :                         return -EINVAL;
    1377                 :          0 :                 ops = upper_dev->netdev_ops;
    1378         [ #  # ]:          0 :                 if (ops->ndo_add_slave) {
    1379                 :          0 :                         err = ops->ndo_add_slave(upper_dev, dev);
    1380         [ #  # ]:          0 :                         if (err)
    1381                 :          0 :                                 return err;
    1382                 :            :                 } else {
    1383                 :            :                         return -EOPNOTSUPP;
    1384                 :            :                 }
    1385                 :            :         }
    1386                 :            :         return 0;
    1387                 :            : }
    1388                 :            : 
    1389                 :          0 : static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
    1390                 :            :                       struct nlattr **tb, char *ifname, int modified)
    1391                 :            : {
    1392                 :          1 :         const struct net_device_ops *ops = dev->netdev_ops;
    1393                 :            :         int err;
    1394                 :            : 
    1395    [ +  - ][ + ]:          1 :         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
    1396                 :          0 :                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
    1397         [ #  # ]:          0 :                 if (IS_ERR(net)) {
    1398                 :            :                         err = PTR_ERR(net);
    1399                 :          0 :                         goto errout;
    1400                 :            :                 }
    1401         [ #  # ]:          0 :                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
    1402                 :            :                         err = -EPERM;
    1403                 :            :                         goto errout;
    1404                 :            :                 }
    1405                 :          0 :                 err = dev_change_net_namespace(dev, net, ifname);
    1406                 :            :                 put_net(net);
    1407         [ #  # ]:          0 :                 if (err)
    1408                 :            :                         goto errout;
    1409                 :            :                 modified = 1;
    1410                 :            :         }
    1411                 :            : 
    1412         [ -  + ]:          2 :         if (tb[IFLA_MAP]) {
    1413                 :            :                 struct rtnl_link_ifmap *u_map;
    1414                 :            :                 struct ifmap k_map;
    1415                 :            : 
    1416         [ #  # ]:          0 :                 if (!ops->ndo_set_config) {
    1417                 :            :                         err = -EOPNOTSUPP;
    1418                 :          0 :                         goto errout;
    1419                 :            :                 }
    1420                 :            : 
    1421         [ #  # ]:          0 :                 if (!netif_device_present(dev)) {
    1422                 :            :                         err = -ENODEV;
    1423                 :            :                         goto errout;
    1424                 :            :                 }
    1425                 :            : 
    1426                 :            :                 u_map = nla_data(tb[IFLA_MAP]);
    1427                 :          0 :                 k_map.mem_start = (unsigned long) u_map->mem_start;
    1428                 :          0 :                 k_map.mem_end = (unsigned long) u_map->mem_end;
    1429                 :          0 :                 k_map.base_addr = (unsigned short) u_map->base_addr;
    1430                 :          0 :                 k_map.irq = (unsigned char) u_map->irq;
    1431                 :          0 :                 k_map.dma = (unsigned char) u_map->dma;
    1432                 :          0 :                 k_map.port = (unsigned char) u_map->port;
    1433                 :            : 
    1434                 :          0 :                 err = ops->ndo_set_config(dev, &k_map);
    1435         [ #  # ]:          0 :                 if (err < 0)
    1436                 :            :                         goto errout;
    1437                 :            : 
    1438                 :            :                 modified = 1;
    1439                 :            :         }
    1440                 :            : 
    1441         [ -  + ]:          2 :         if (tb[IFLA_ADDRESS]) {
    1442                 :            :                 struct sockaddr *sa;
    1443                 :            :                 int len;
    1444                 :            : 
    1445                 :          0 :                 len = sizeof(sa_family_t) + dev->addr_len;
    1446                 :            :                 sa = kmalloc(len, GFP_KERNEL);
    1447         [ #  # ]:          0 :                 if (!sa) {
    1448                 :            :                         err = -ENOMEM;
    1449                 :            :                         goto errout;
    1450                 :            :                 }
    1451                 :          0 :                 sa->sa_family = dev->type;
    1452                 :          0 :                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
    1453                 :          0 :                        dev->addr_len);
    1454                 :          0 :                 err = dev_set_mac_address(dev, sa);
    1455                 :          0 :                 kfree(sa);
    1456         [ #  # ]:          0 :                 if (err)
    1457                 :            :                         goto errout;
    1458                 :            :                 modified = 1;
    1459                 :            :         }
    1460                 :            : 
    1461         [ -  + ]:          2 :         if (tb[IFLA_MTU]) {
    1462                 :          0 :                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
    1463         [ #  # ]:          0 :                 if (err < 0)
    1464                 :            :                         goto errout;
    1465                 :            :                 modified = 1;
    1466                 :            :         }
    1467                 :            : 
    1468         [ -  + ]:          2 :         if (tb[IFLA_GROUP]) {
    1469                 :          0 :                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
    1470                 :            :                 modified = 1;
    1471                 :            :         }
    1472                 :            : 
    1473                 :            :         /*
    1474                 :            :          * Interface selected by interface index but interface
    1475                 :            :          * name provided implies that a name change has been
    1476                 :            :          * requested.
    1477                 :            :          */
    1478 [ +  + ][ -  + ]:          2 :         if (ifm->ifi_index > 0 && ifname[0]) {
    1479                 :          0 :                 err = dev_change_name(dev, ifname);
    1480         [ #  # ]:          0 :                 if (err < 0)
    1481                 :            :                         goto errout;
    1482                 :            :                 modified = 1;
    1483                 :            :         }
    1484                 :            : 
    1485         [ -  + ]:          2 :         if (tb[IFLA_IFALIAS]) {
    1486                 :          0 :                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
    1487                 :            :                                     nla_len(tb[IFLA_IFALIAS]));
    1488         [ #  # ]:          0 :                 if (err < 0)
    1489                 :            :                         goto errout;
    1490                 :            :                 modified = 1;
    1491                 :            :         }
    1492                 :            : 
    1493         [ -  + ]:          1 :         if (tb[IFLA_BROADCAST]) {
    1494                 :          0 :                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
    1495                 :          0 :                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
    1496                 :            :         }
    1497                 :            : 
    1498 [ -  + ][ #  # ]:          1 :         if (ifm->ifi_flags || ifm->ifi_change) {
    1499                 :          1 :                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
    1500         [ +  - ]:          1 :                 if (err < 0)
    1501                 :            :                         goto errout;
    1502                 :            :         }
    1503                 :            : 
    1504         [ -  + ]:          1 :         if (tb[IFLA_MASTER]) {
    1505                 :          0 :                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
    1506         [ #  # ]:          0 :                 if (err)
    1507                 :            :                         goto errout;
    1508                 :            :                 modified = 1;
    1509                 :            :         }
    1510                 :            : 
    1511         [ -  + ]:          1 :         if (tb[IFLA_CARRIER]) {
    1512                 :          0 :                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
    1513         [ #  # ]:          0 :                 if (err)
    1514                 :            :                         goto errout;
    1515                 :            :                 modified = 1;
    1516                 :            :         }
    1517                 :            : 
    1518         [ -  + ]:          1 :         if (tb[IFLA_TXQLEN])
    1519                 :          0 :                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
    1520                 :            : 
    1521         [ -  + ]:          1 :         if (tb[IFLA_OPERSTATE])
    1522                 :          0 :                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
    1523                 :            : 
    1524         [ -  + ]:          1 :         if (tb[IFLA_LINKMODE]) {
    1525                 :          0 :                 write_lock_bh(&dev_base_lock);
    1526                 :          0 :                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
    1527                 :          0 :                 write_unlock_bh(&dev_base_lock);
    1528                 :            :         }
    1529                 :            : 
    1530         [ -  + ]:          1 :         if (tb[IFLA_VFINFO_LIST]) {
    1531                 :          0 :                 struct nlattr *attr;
    1532                 :            :                 int rem;
    1533         [ #  # ]:          0 :                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
    1534         [ #  # ]:          0 :                         if (nla_type(attr) != IFLA_VF_INFO) {
    1535                 :            :                                 err = -EINVAL;
    1536                 :            :                                 goto errout;
    1537                 :            :                         }
    1538                 :          0 :                         err = do_setvfinfo(dev, attr);
    1539         [ #  # ]:          0 :                         if (err < 0)
    1540                 :            :                                 goto errout;
    1541                 :            :                         modified = 1;
    1542                 :            :                 }
    1543                 :            :         }
    1544                 :            :         err = 0;
    1545                 :            : 
    1546         [ -  + ]:          1 :         if (tb[IFLA_VF_PORTS]) {
    1547                 :            :                 struct nlattr *port[IFLA_PORT_MAX+1];
    1548                 :          0 :                 struct nlattr *attr;
    1549                 :            :                 int vf;
    1550                 :            :                 int rem;
    1551                 :            : 
    1552                 :            :                 err = -EOPNOTSUPP;
    1553         [ #  # ]:          0 :                 if (!ops->ndo_set_vf_port)
    1554                 :            :                         goto errout;
    1555                 :            : 
    1556         [ #  # ]:          0 :                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
    1557         [ #  # ]:          0 :                         if (nla_type(attr) != IFLA_VF_PORT)
    1558                 :          0 :                                 continue;
    1559                 :            :                         err = nla_parse_nested(port, IFLA_PORT_MAX,
    1560                 :            :                                 attr, ifla_port_policy);
    1561         [ #  # ]:          0 :                         if (err < 0)
    1562                 :            :                                 goto errout;
    1563         [ #  # ]:          0 :                         if (!port[IFLA_PORT_VF]) {
    1564                 :            :                                 err = -EOPNOTSUPP;
    1565                 :            :                                 goto errout;
    1566                 :            :                         }
    1567                 :          0 :                         vf = nla_get_u32(port[IFLA_PORT_VF]);
    1568                 :          0 :                         err = ops->ndo_set_vf_port(dev, vf, port);
    1569         [ #  # ]:          0 :                         if (err < 0)
    1570                 :            :                                 goto errout;
    1571                 :            :                         modified = 1;
    1572                 :            :                 }
    1573                 :            :         }
    1574                 :            :         err = 0;
    1575                 :            : 
    1576         [ -  + ]:          1 :         if (tb[IFLA_PORT_SELF]) {
    1577                 :            :                 struct nlattr *port[IFLA_PORT_MAX+1];
    1578                 :            : 
    1579                 :            :                 err = nla_parse_nested(port, IFLA_PORT_MAX,
    1580                 :            :                         tb[IFLA_PORT_SELF], ifla_port_policy);
    1581         [ #  # ]:          0 :                 if (err < 0)
    1582                 :            :                         goto errout;
    1583                 :            : 
    1584                 :            :                 err = -EOPNOTSUPP;
    1585         [ #  # ]:          0 :                 if (ops->ndo_set_vf_port)
    1586                 :          0 :                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
    1587         [ #  # ]:          0 :                 if (err < 0)
    1588                 :            :                         goto errout;
    1589                 :            :                 modified = 1;
    1590                 :            :         }
    1591                 :            : 
    1592         [ -  + ]:          1 :         if (tb[IFLA_AF_SPEC]) {
    1593                 :          0 :                 struct nlattr *af;
    1594                 :            :                 int rem;
    1595                 :            : 
    1596         [ #  # ]:          0 :                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
    1597                 :            :                         const struct rtnl_af_ops *af_ops;
    1598                 :            : 
    1599         [ #  # ]:          0 :                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
    1600                 :          0 :                                 BUG();
    1601                 :            : 
    1602                 :          0 :                         err = af_ops->set_link_af(dev, af);
    1603         [ #  # ]:          0 :                         if (err < 0)
    1604                 :            :                                 goto errout;
    1605                 :            : 
    1606                 :            :                         modified = 1;
    1607                 :            :                 }
    1608                 :            :         }
    1609                 :            :         err = 0;
    1610                 :            : 
    1611                 :            : errout:
    1612         [ -  + ]:          1 :         if (err < 0 && modified)
    1613         [ #  # ]:          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",
    1614                 :            :                                      dev->name);
    1615                 :            : 
    1616                 :          1 :         return err;
    1617                 :            : }
    1618                 :            : 
    1619                 :          0 : static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
    1620                 :            : {
    1621                 :            :         struct net *net = sock_net(skb->sk);
    1622                 :            :         struct ifinfomsg *ifm;
    1623                 :            :         struct net_device *dev;
    1624                 :            :         int err;
    1625                 :            :         struct nlattr *tb[IFLA_MAX+1];
    1626                 :            :         char ifname[IFNAMSIZ];
    1627                 :            : 
    1628                 :            :         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
    1629         [ #  # ]:          0 :         if (err < 0)
    1630                 :            :                 goto errout;
    1631                 :            : 
    1632         [ #  # ]:          0 :         if (tb[IFLA_IFNAME])
    1633                 :          0 :                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    1634                 :            :         else
    1635                 :          0 :                 ifname[0] = '\0';
    1636                 :            : 
    1637                 :            :         err = -EINVAL;
    1638                 :          0 :         ifm = nlmsg_data(nlh);
    1639         [ #  # ]:          0 :         if (ifm->ifi_index > 0)
    1640                 :          0 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    1641         [ #  # ]:          0 :         else if (tb[IFLA_IFNAME])
    1642                 :          0 :                 dev = __dev_get_by_name(net, ifname);
    1643                 :            :         else
    1644                 :            :                 goto errout;
    1645                 :            : 
    1646         [ #  # ]:          0 :         if (dev == NULL) {
    1647                 :            :                 err = -ENODEV;
    1648                 :            :                 goto errout;
    1649                 :            :         }
    1650                 :            : 
    1651                 :          0 :         err = validate_linkmsg(dev, tb);
    1652         [ #  # ]:          0 :         if (err < 0)
    1653                 :            :                 goto errout;
    1654                 :            : 
    1655                 :          0 :         err = do_setlink(dev, ifm, tb, ifname, 0);
    1656                 :            : errout:
    1657                 :          0 :         return err;
    1658                 :            : }
    1659                 :            : 
    1660                 :          0 : static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
    1661                 :            : {
    1662                 :            :         struct net *net = sock_net(skb->sk);
    1663                 :            :         const struct rtnl_link_ops *ops;
    1664                 :            :         struct net_device *dev;
    1665                 :            :         struct ifinfomsg *ifm;
    1666                 :            :         char ifname[IFNAMSIZ];
    1667                 :            :         struct nlattr *tb[IFLA_MAX+1];
    1668                 :            :         int err;
    1669                 :          0 :         LIST_HEAD(list_kill);
    1670                 :            : 
    1671                 :            :         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
    1672         [ #  # ]:          0 :         if (err < 0)
    1673                 :            :                 return err;
    1674                 :            : 
    1675         [ #  # ]:          0 :         if (tb[IFLA_IFNAME])
    1676                 :          0 :                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    1677                 :            : 
    1678                 :            :         ifm = nlmsg_data(nlh);
    1679         [ #  # ]:          0 :         if (ifm->ifi_index > 0)
    1680                 :          0 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    1681         [ #  # ]:          0 :         else if (tb[IFLA_IFNAME])
    1682                 :          0 :                 dev = __dev_get_by_name(net, ifname);
    1683                 :            :         else
    1684                 :            :                 return -EINVAL;
    1685                 :            : 
    1686         [ #  # ]:          0 :         if (!dev)
    1687                 :            :                 return -ENODEV;
    1688                 :            : 
    1689                 :          0 :         ops = dev->rtnl_link_ops;
    1690         [ #  # ]:          0 :         if (!ops)
    1691                 :            :                 return -EOPNOTSUPP;
    1692                 :            : 
    1693                 :          0 :         ops->dellink(dev, &list_kill);
    1694                 :          0 :         unregister_netdevice_many(&list_kill);
    1695                 :            :         list_del(&list_kill);
    1696                 :          0 :         return 0;
    1697                 :            : }
    1698                 :            : 
    1699                 :          0 : int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
    1700                 :            : {
    1701                 :            :         unsigned int old_flags;
    1702                 :            :         int err;
    1703                 :            : 
    1704                 :          0 :         old_flags = dev->flags;
    1705 [ #  # ][ #  # ]:          0 :         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
                 [ #  # ]
    1706                 :          0 :                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
    1707         [ #  # ]:          0 :                 if (err < 0)
    1708                 :            :                         return err;
    1709                 :            :         }
    1710                 :            : 
    1711                 :          0 :         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
    1712                 :            : 
    1713                 :          0 :         __dev_notify_flags(dev, old_flags, ~0U);
    1714                 :          0 :         return 0;
    1715                 :            : }
    1716                 :            : EXPORT_SYMBOL(rtnl_configure_link);
    1717                 :            : 
    1718                 :          0 : struct net_device *rtnl_create_link(struct net *net,
    1719                 :            :         char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
    1720                 :            : {
    1721                 :            :         int err;
    1722                 :            :         struct net_device *dev;
    1723                 :            :         unsigned int num_tx_queues = 1;
    1724                 :            :         unsigned int num_rx_queues = 1;
    1725                 :            : 
    1726         [ #  # ]:          0 :         if (tb[IFLA_NUM_TX_QUEUES])
    1727                 :            :                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
    1728         [ #  # ]:          0 :         else if (ops->get_num_tx_queues)
    1729                 :          0 :                 num_tx_queues = ops->get_num_tx_queues();
    1730                 :            : 
    1731         [ #  # ]:          0 :         if (tb[IFLA_NUM_RX_QUEUES])
    1732                 :            :                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
    1733         [ #  # ]:          0 :         else if (ops->get_num_rx_queues)
    1734                 :          0 :                 num_rx_queues = ops->get_num_rx_queues();
    1735                 :            : 
    1736                 :            :         err = -ENOMEM;
    1737                 :          0 :         dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
    1738                 :            :                                num_tx_queues, num_rx_queues);
    1739         [ #  # ]:          0 :         if (!dev)
    1740                 :            :                 goto err;
    1741                 :            : 
    1742                 :            :         dev_net_set(dev, net);
    1743                 :          0 :         dev->rtnl_link_ops = ops;
    1744                 :          0 :         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
    1745                 :            : 
    1746         [ #  # ]:          0 :         if (tb[IFLA_MTU])
    1747                 :          0 :                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
    1748         [ #  # ]:          0 :         if (tb[IFLA_ADDRESS]) {
    1749                 :          0 :                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
    1750                 :            :                                 nla_len(tb[IFLA_ADDRESS]));
    1751                 :          0 :                 dev->addr_assign_type = NET_ADDR_SET;
    1752                 :            :         }
    1753         [ #  # ]:          0 :         if (tb[IFLA_BROADCAST])
    1754                 :          0 :                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
    1755                 :            :                                 nla_len(tb[IFLA_BROADCAST]));
    1756         [ #  # ]:          0 :         if (tb[IFLA_TXQLEN])
    1757                 :          0 :                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
    1758         [ #  # ]:          0 :         if (tb[IFLA_OPERSTATE])
    1759                 :          0 :                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
    1760         [ #  # ]:          0 :         if (tb[IFLA_LINKMODE])
    1761                 :          0 :                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
    1762         [ #  # ]:          0 :         if (tb[IFLA_GROUP])
    1763                 :          0 :                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
    1764                 :            : 
    1765                 :          0 :         return dev;
    1766                 :            : 
    1767                 :            : err:
    1768                 :            :         return ERR_PTR(err);
    1769                 :            : }
    1770                 :            : EXPORT_SYMBOL(rtnl_create_link);
    1771                 :            : 
    1772                 :          0 : static int rtnl_group_changelink(struct net *net, int group,
    1773                 :            :                 struct ifinfomsg *ifm,
    1774                 :            :                 struct nlattr **tb)
    1775                 :            : {
    1776                 :            :         struct net_device *dev;
    1777                 :            :         int err;
    1778                 :            : 
    1779         [ #  # ]:          0 :         for_each_netdev(net, dev) {
    1780         [ #  # ]:          0 :                 if (dev->group == group) {
    1781                 :          0 :                         err = do_setlink(dev, ifm, tb, NULL, 0);
    1782         [ #  # ]:          0 :                         if (err < 0)
    1783                 :            :                                 return err;
    1784                 :            :                 }
    1785                 :            :         }
    1786                 :            : 
    1787                 :            :         return 0;
    1788                 :            : }
    1789                 :            : 
    1790                 :          2 : static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
    1791                 :            : {
    1792                 :            :         struct net *net = sock_net(skb->sk);
    1793                 :            :         const struct rtnl_link_ops *ops;
    1794                 :            :         const struct rtnl_link_ops *m_ops = NULL;
    1795                 :            :         struct net_device *dev;
    1796                 :            :         struct net_device *master_dev = NULL;
    1797                 :            :         struct ifinfomsg *ifm;
    1798                 :            :         char kind[MODULE_NAME_LEN];
    1799                 :            :         char ifname[IFNAMSIZ];
    1800                 :            :         struct nlattr *tb[IFLA_MAX+1];
    1801                 :            :         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
    1802                 :            :         int err;
    1803                 :            : 
    1804                 :            : #ifdef CONFIG_MODULES
    1805                 :            : replay:
    1806                 :            : #endif
    1807                 :            :         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
    1808         [ +  - ]:          2 :         if (err < 0)
    1809                 :          2 :                 return err;
    1810                 :            : 
    1811         [ -  + ]:          2 :         if (tb[IFLA_IFNAME])
    1812                 :          0 :                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    1813                 :            :         else
    1814                 :          2 :                 ifname[0] = '\0';
    1815                 :            : 
    1816                 :          2 :         ifm = nlmsg_data(nlh);
    1817         [ +  + ]:          2 :         if (ifm->ifi_index > 0)
    1818                 :          1 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    1819                 :            :         else {
    1820         [ -  + ]:          1 :                 if (ifname[0])
    1821                 :          0 :                         dev = __dev_get_by_name(net, ifname);
    1822                 :            :                 else
    1823                 :            :                         dev = NULL;
    1824                 :            :         }
    1825                 :            : 
    1826         [ +  + ]:          2 :         if (dev) {
    1827                 :          1 :                 master_dev = netdev_master_upper_dev_get(dev);
    1828         [ -  + ]:          1 :                 if (master_dev)
    1829                 :          0 :                         m_ops = master_dev->rtnl_link_ops;
    1830                 :            :         }
    1831                 :            : 
    1832                 :          2 :         err = validate_linkmsg(dev, tb);
    1833         [ +  - ]:          2 :         if (err < 0)
    1834                 :            :                 return err;
    1835                 :            : 
    1836         [ -  + ]:          2 :         if (tb[IFLA_LINKINFO]) {
    1837                 :            :                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
    1838                 :            :                                        tb[IFLA_LINKINFO], ifla_info_policy);
    1839         [ #  # ]:          0 :                 if (err < 0)
    1840                 :            :                         return err;
    1841                 :            :         } else
    1842                 :          2 :                 memset(linkinfo, 0, sizeof(linkinfo));
    1843                 :            : 
    1844         [ -  + ]:          2 :         if (linkinfo[IFLA_INFO_KIND]) {
    1845                 :          0 :                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
    1846                 :          0 :                 ops = rtnl_link_ops_get(kind);
    1847                 :            :         } else {
    1848                 :          2 :                 kind[0] = '\0';
    1849                 :            :                 ops = NULL;
    1850                 :            :         }
    1851                 :            : 
    1852                 :            :         if (1) {
    1853         [ -  + ]:          4 :                 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
    1854         [ -  + ]:          4 :                 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
    1855                 :            :                 struct nlattr **data = NULL;
    1856                 :            :                 struct nlattr **slave_data = NULL;
    1857                 :            :                 struct net *dest_net;
    1858                 :            : 
    1859         [ -  + ]:          2 :                 if (ops) {
    1860 [ #  # ][ #  # ]:          0 :                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
    1861                 :          0 :                                 err = nla_parse_nested(attr, ops->maxtype,
    1862                 :            :                                                        linkinfo[IFLA_INFO_DATA],
    1863                 :            :                                                        ops->policy);
    1864         [ #  # ]:          0 :                                 if (err < 0)
    1865                 :          2 :                                         return err;
    1866                 :            :                                 data = attr;
    1867                 :            :                         }
    1868         [ #  # ]:          0 :                         if (ops->validate) {
    1869                 :          0 :                                 err = ops->validate(tb, data);
    1870         [ #  # ]:          0 :                                 if (err < 0)
    1871                 :            :                                         return err;
    1872                 :            :                         }
    1873                 :            :                 }
    1874                 :            : 
    1875         [ -  + ]:          2 :                 if (m_ops) {
    1876 [ #  # ][ #  # ]:          0 :                         if (m_ops->slave_maxtype &&
    1877                 :          0 :                             linkinfo[IFLA_INFO_SLAVE_DATA]) {
    1878                 :          0 :                                 err = nla_parse_nested(slave_attr,
    1879                 :            :                                                        m_ops->slave_maxtype,
    1880                 :            :                                                        linkinfo[IFLA_INFO_SLAVE_DATA],
    1881                 :            :                                                        m_ops->slave_policy);
    1882         [ #  # ]:          0 :                                 if (err < 0)
    1883                 :            :                                         return err;
    1884                 :            :                                 slave_data = slave_attr;
    1885                 :            :                         }
    1886         [ #  # ]:          0 :                         if (m_ops->slave_validate) {
    1887                 :          0 :                                 err = m_ops->slave_validate(tb, slave_data);
    1888         [ #  # ]:          0 :                                 if (err < 0)
    1889                 :            :                                         return err;
    1890                 :            :                         }
    1891                 :            :                 }
    1892                 :            : 
    1893         [ +  + ]:          2 :                 if (dev) {
    1894                 :            :                         int modified = 0;
    1895                 :            : 
    1896         [ +  - ]:          1 :                         if (nlh->nlmsg_flags & NLM_F_EXCL)
    1897                 :            :                                 return -EEXIST;
    1898         [ +  - ]:          1 :                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
    1899                 :            :                                 return -EOPNOTSUPP;
    1900                 :            : 
    1901         [ -  + ]:          1 :                         if (linkinfo[IFLA_INFO_DATA]) {
    1902 [ #  # ][ #  # ]:          0 :                                 if (!ops || ops != dev->rtnl_link_ops ||
                 [ #  # ]
    1903                 :          0 :                                     !ops->changelink)
    1904                 :            :                                         return -EOPNOTSUPP;
    1905                 :            : 
    1906                 :          0 :                                 err = ops->changelink(dev, tb, data);
    1907         [ #  # ]:          0 :                                 if (err < 0)
    1908                 :            :                                         return err;
    1909                 :            :                                 modified = 1;
    1910                 :            :                         }
    1911                 :            : 
    1912         [ -  + ]:          1 :                         if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
    1913 [ #  # ][ #  # ]:          0 :                                 if (!m_ops || !m_ops->slave_changelink)
    1914                 :            :                                         return -EOPNOTSUPP;
    1915                 :            : 
    1916                 :          0 :                                 err = m_ops->slave_changelink(master_dev, dev,
    1917                 :            :                                                               tb, slave_data);
    1918         [ #  # ]:          0 :                                 if (err < 0)
    1919                 :            :                                         return err;
    1920                 :            :                                 modified = 1;
    1921                 :            :                         }
    1922                 :            : 
    1923                 :          1 :                         return do_setlink(dev, ifm, tb, ifname, modified);
    1924                 :            :                 }
    1925                 :            : 
    1926         [ +  - ]:          1 :                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
    1927 [ +  - ][ -  + ]:          1 :                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
    1928                 :          0 :                                 return rtnl_group_changelink(net,
    1929                 :            :                                                 nla_get_u32(tb[IFLA_GROUP]),
    1930                 :            :                                                 ifm, tb);
    1931                 :            :                         return -ENODEV;
    1932                 :            :                 }
    1933                 :            : 
    1934 [ #  # ][ #  # ]:          0 :                 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
                 [ #  # ]
    1935                 :            :                         return -EOPNOTSUPP;
    1936                 :            : 
    1937         [ #  # ]:          0 :                 if (!ops) {
    1938                 :            : #ifdef CONFIG_MODULES
    1939         [ #  # ]:          0 :                         if (kind[0]) {
    1940                 :            :                                 __rtnl_unlock();
    1941                 :          0 :                                 request_module("rtnl-link-%s", kind);
    1942                 :            :                                 rtnl_lock();
    1943                 :          0 :                                 ops = rtnl_link_ops_get(kind);
    1944         [ #  # ]:          0 :                                 if (ops)
    1945                 :            :                                         goto replay;
    1946                 :            :                         }
    1947                 :            : #endif
    1948                 :            :                         return -EOPNOTSUPP;
    1949                 :            :                 }
    1950                 :            : 
    1951         [ #  # ]:          0 :                 if (!ifname[0])
    1952                 :          0 :                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
    1953                 :            : 
    1954                 :          0 :                 dest_net = rtnl_link_get_net(net, tb);
    1955         [ #  # ]:          0 :                 if (IS_ERR(dest_net))
    1956                 :          0 :                         return PTR_ERR(dest_net);
    1957                 :            : 
    1958                 :          0 :                 dev = rtnl_create_link(dest_net, ifname, ops, tb);
    1959         [ #  # ]:          0 :                 if (IS_ERR(dev)) {
    1960                 :            :                         err = PTR_ERR(dev);
    1961                 :          0 :                         goto out;
    1962                 :            :                 }
    1963                 :            : 
    1964                 :          0 :                 dev->ifindex = ifm->ifi_index;
    1965                 :            : 
    1966         [ #  # ]:          0 :                 if (ops->newlink) {
    1967                 :          0 :                         err = ops->newlink(net, dev, tb, data);
    1968                 :            :                         /* Drivers should call free_netdev() in ->destructor
    1969                 :            :                          * and unregister it on failure so that device could be
    1970                 :            :                          * finally freed in rtnl_unlock.
    1971                 :            :                          */
    1972         [ #  # ]:          0 :                         if (err < 0)
    1973                 :            :                                 goto out;
    1974                 :            :                 } else {
    1975                 :          0 :                         err = register_netdevice(dev);
    1976         [ #  # ]:          0 :                         if (err < 0) {
    1977                 :          0 :                                 free_netdev(dev);
    1978                 :          0 :                                 goto out;
    1979                 :            :                         }
    1980                 :            :                 }
    1981                 :          0 :                 err = rtnl_configure_link(dev, ifm);
    1982         [ #  # ]:          0 :                 if (err < 0)
    1983                 :            :                         unregister_netdevice(dev);
    1984                 :            : out:
    1985                 :            :                 put_net(dest_net);
    1986                 :          0 :                 return err;
    1987                 :            :         }
    1988                 :            : }
    1989                 :            : 
    1990                 :          0 : static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
    1991                 :            : {
    1992                 :            :         struct net *net = sock_net(skb->sk);
    1993                 :            :         struct ifinfomsg *ifm;
    1994                 :            :         char ifname[IFNAMSIZ];
    1995                 :            :         struct nlattr *tb[IFLA_MAX+1];
    1996                 :            :         struct net_device *dev = NULL;
    1997                 :            :         struct sk_buff *nskb;
    1998                 :            :         int err;
    1999                 :            :         u32 ext_filter_mask = 0;
    2000                 :            : 
    2001                 :            :         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
    2002         [ #  # ]:          0 :         if (err < 0)
    2003                 :            :                 return err;
    2004                 :            : 
    2005         [ #  # ]:          0 :         if (tb[IFLA_IFNAME])
    2006                 :          0 :                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
    2007                 :            : 
    2008         [ #  # ]:          0 :         if (tb[IFLA_EXT_MASK])
    2009                 :            :                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
    2010                 :            : 
    2011                 :            :         ifm = nlmsg_data(nlh);
    2012         [ #  # ]:          0 :         if (ifm->ifi_index > 0)
    2013                 :          0 :                 dev = __dev_get_by_index(net, ifm->ifi_index);
    2014         [ #  # ]:          0 :         else if (tb[IFLA_IFNAME])
    2015                 :          0 :                 dev = __dev_get_by_name(net, ifname);
    2016                 :            :         else
    2017                 :            :                 return -EINVAL;
    2018                 :            : 
    2019         [ #  # ]:          0 :         if (dev == NULL)
    2020                 :            :                 return -ENODEV;
    2021                 :            : 
    2022                 :          0 :         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
    2023         [ #  # ]:          0 :         if (nskb == NULL)
    2024                 :            :                 return -ENOBUFS;
    2025                 :            : 
    2026                 :          0 :         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
    2027                 :            :                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
    2028         [ #  # ]:          0 :         if (err < 0) {
    2029                 :            :                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
    2030         [ #  # ]:          0 :                 WARN_ON(err == -EMSGSIZE);
    2031                 :          0 :                 kfree_skb(nskb);
    2032                 :            :         } else
    2033                 :          0 :                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
    2034                 :            : 
    2035                 :          0 :         return err;
    2036                 :            : }
    2037                 :            : 
    2038                 :          0 : static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
    2039                 :            : {
    2040                 :            :         struct net *net = sock_net(skb->sk);
    2041                 :            :         struct net_device *dev;
    2042                 :            :         struct nlattr *tb[IFLA_MAX+1];
    2043                 :            :         u32 ext_filter_mask = 0;
    2044                 :            :         u16 min_ifinfo_dump_size = 0;
    2045                 :            : 
    2046         [ -  + ]:       1110 :         if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
    2047                 :            :                         ifla_policy) >= 0) {
    2048         [ #  # ]:          0 :                 if (tb[IFLA_EXT_MASK])
    2049                 :            :                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
    2050                 :            :         }
    2051                 :            : 
    2052         [ -  + ]:        555 :         if (!ext_filter_mask)
    2053                 :            :                 return NLMSG_GOODSIZE;
    2054                 :            :         /*
    2055                 :            :          * traverse the list of net devices and compute the minimum
    2056                 :            :          * buffer size based upon the filter mask.
    2057                 :            :          */
    2058         [ #  # ]:          0 :         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
    2059                 :          0 :                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
    2060                 :            :                                              if_nlmsg_size(dev,
    2061                 :            :                                                            ext_filter_mask));
    2062                 :            :         }
    2063                 :            : 
    2064                 :            :         return min_ifinfo_dump_size;
    2065                 :            : }
    2066                 :            : 
    2067                 :          0 : static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
    2068                 :            : {
    2069                 :            :         int idx;
    2070                 :       1662 :         int s_idx = cb->family;
    2071                 :            : 
    2072         [ +  + ]:       1662 :         if (s_idx == 0)
    2073                 :            :                 s_idx = 1;
    2074         [ +  + ]:      78668 :         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
    2075                 :      78114 :                 int type = cb->nlh->nlmsg_type-RTM_BASE;
    2076         [ +  + ]:      78114 :                 if (idx < s_idx || idx == PF_PACKET)
    2077                 :       6094 :                         continue;
    2078 [ +  + ][ +  + ]:      72020 :                 if (rtnl_msg_handlers[idx] == NULL ||
    2079                 :       2770 :                     rtnl_msg_handlers[idx][type].dumpit == NULL)
    2080                 :      69804 :                         continue;
    2081         [ +  + ]:       2216 :                 if (idx > s_idx) {
    2082                 :       1108 :                         memset(&cb->args[0], 0, sizeof(cb->args));
    2083                 :       1108 :                         cb->prev_seq = 0;
    2084                 :       1108 :                         cb->seq = 0;
    2085                 :            :                 }
    2086         [ +  + ]:       2216 :                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
    2087                 :            :                         break;
    2088                 :            :         }
    2089                 :          0 :         cb->family = idx;
    2090                 :            : 
    2091                 :          0 :         return skb->len;
    2092                 :            : }
    2093                 :            : 
    2094                 :          0 : void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
    2095                 :            :                   gfp_t flags)
    2096                 :            : {
    2097                 :            :         struct net *net = dev_net(dev);
    2098                 :            :         struct sk_buff *skb;
    2099                 :            :         int err = -ENOBUFS;
    2100                 :            :         size_t if_info_size;
    2101                 :            : 
    2102                 :          0 :         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
    2103         [ #  # ]:          0 :         if (skb == NULL)
    2104                 :            :                 goto errout;
    2105                 :            : 
    2106                 :          0 :         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
    2107         [ #  # ]:          0 :         if (err < 0) {
    2108                 :            :                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
    2109         [ #  # ]:          0 :                 WARN_ON(err == -EMSGSIZE);
    2110                 :          0 :                 kfree_skb(skb);
    2111                 :          0 :                 goto errout;
    2112                 :            :         }
    2113                 :            :         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
    2114                 :          0 :         return;
    2115                 :            : errout:
    2116         [ #  # ]:          0 :         if (err < 0)
    2117                 :            :                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
    2118                 :            : }
    2119                 :            : EXPORT_SYMBOL(rtmsg_ifinfo);
    2120                 :            : 
    2121                 :          0 : static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
    2122                 :            :                                    struct net_device *dev,
    2123                 :            :                                    u8 *addr, u32 pid, u32 seq,
    2124                 :            :                                    int type, unsigned int flags,
    2125                 :            :                                    int nlflags)
    2126                 :            : {
    2127                 :            :         struct nlmsghdr *nlh;
    2128                 :            :         struct ndmsg *ndm;
    2129                 :            : 
    2130                 :            :         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
    2131         [ #  # ]:          0 :         if (!nlh)
    2132                 :            :                 return -EMSGSIZE;
    2133                 :            : 
    2134                 :            :         ndm = nlmsg_data(nlh);
    2135                 :          0 :         ndm->ndm_family  = AF_BRIDGE;
    2136                 :          0 :         ndm->ndm_pad1         = 0;
    2137                 :          0 :         ndm->ndm_pad2    = 0;
    2138                 :          0 :         ndm->ndm_flags        = flags;
    2139                 :          0 :         ndm->ndm_type         = 0;
    2140                 :          0 :         ndm->ndm_ifindex = dev->ifindex;
    2141                 :          0 :         ndm->ndm_state   = NUD_PERMANENT;
    2142                 :            : 
    2143         [ #  # ]:          0 :         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
    2144                 :            :                 goto nla_put_failure;
    2145                 :            : 
    2146                 :            :         return nlmsg_end(skb, nlh);
    2147                 :            : 
    2148                 :            : nla_put_failure:
    2149                 :            :         nlmsg_cancel(skb, nlh);
    2150                 :            :         return -EMSGSIZE;
    2151                 :            : }
    2152                 :            : 
    2153                 :            : static inline size_t rtnl_fdb_nlmsg_size(void)
    2154                 :            : {
    2155                 :            :         return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
    2156                 :            : }
    2157                 :            : 
    2158                 :          0 : static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
    2159                 :            : {
    2160                 :            :         struct net *net = dev_net(dev);
    2161                 :            :         struct sk_buff *skb;
    2162                 :            :         int err = -ENOBUFS;
    2163                 :            : 
    2164                 :            :         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
    2165         [ #  # ]:          0 :         if (!skb)
    2166                 :            :                 goto errout;
    2167                 :            : 
    2168                 :          0 :         err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
    2169         [ #  # ]:          0 :         if (err < 0) {
    2170                 :          0 :                 kfree_skb(skb);
    2171                 :          0 :                 goto errout;
    2172                 :            :         }
    2173                 :            : 
    2174                 :            :         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
    2175                 :          0 :         return;
    2176                 :            : errout:
    2177                 :            :         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
    2178                 :            : }
    2179                 :            : 
    2180                 :            : /**
    2181                 :            :  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
    2182                 :            :  */
    2183                 :          0 : int ndo_dflt_fdb_add(struct ndmsg *ndm,
    2184                 :            :                      struct nlattr *tb[],
    2185                 :            :                      struct net_device *dev,
    2186                 :          0 :                      const unsigned char *addr,
    2187                 :            :                      u16 flags)
    2188                 :            : {
    2189                 :            :         int err = -EINVAL;
    2190                 :            : 
    2191                 :            :         /* If aging addresses are supported device will need to
    2192                 :            :          * implement its own handler for this.
    2193                 :            :          */
    2194 [ #  # ][ #  # ]:          0 :         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
    2195                 :          0 :                 pr_info("%s: FDB only supports static addresses\n", dev->name);
    2196                 :          0 :                 return err;
    2197                 :            :         }
    2198                 :            : 
    2199 [ #  # ][ #  # ]:          0 :         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
    2200                 :          0 :                 err = dev_uc_add_excl(dev, addr);
    2201         [ #  # ]:          0 :         else if (is_multicast_ether_addr(addr))
    2202                 :          0 :                 err = dev_mc_add_excl(dev, addr);
    2203                 :            : 
    2204                 :            :         /* Only return duplicate errors if NLM_F_EXCL is set */
    2205 [ #  # ][ #  # ]:          0 :         if (err == -EEXIST && !(flags & NLM_F_EXCL))
    2206                 :            :                 err = 0;
    2207                 :            : 
    2208                 :          0 :         return err;
    2209                 :            : }
    2210                 :            : EXPORT_SYMBOL(ndo_dflt_fdb_add);
    2211                 :            : 
    2212                 :          0 : static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
    2213                 :            : {
    2214                 :            :         struct net *net = sock_net(skb->sk);
    2215                 :            :         struct ndmsg *ndm;
    2216                 :            :         struct nlattr *tb[NDA_MAX+1];
    2217                 :            :         struct net_device *dev;
    2218                 :            :         u8 *addr;
    2219                 :            :         int err;
    2220                 :            : 
    2221                 :            :         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
    2222         [ #  # ]:          0 :         if (err < 0)
    2223                 :            :                 return err;
    2224                 :            : 
    2225                 :          0 :         ndm = nlmsg_data(nlh);
    2226         [ #  # ]:          0 :         if (ndm->ndm_ifindex == 0) {
    2227                 :          0 :                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
    2228                 :          0 :                 return -EINVAL;
    2229                 :            :         }
    2230                 :            : 
    2231                 :          0 :         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
    2232         [ #  # ]:          0 :         if (dev == NULL) {
    2233                 :          0 :                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
    2234                 :          0 :                 return -ENODEV;
    2235                 :            :         }
    2236                 :            : 
    2237 [ #  # ][ #  # ]:          0 :         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
    2238                 :          0 :                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
    2239                 :          0 :                 return -EINVAL;
    2240                 :            :         }
    2241                 :            : 
    2242                 :          0 :         addr = nla_data(tb[NDA_LLADDR]);
    2243                 :            : 
    2244                 :            :         err = -EOPNOTSUPP;
    2245                 :            : 
    2246                 :            :         /* Support fdb on master device the net/bridge default case */
    2247 [ #  # ][ #  # ]:          0 :         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
                 [ #  # ]
    2248                 :          0 :             (dev->priv_flags & IFF_BRIDGE_PORT)) {
    2249                 :          0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2250                 :          0 :                 const struct net_device_ops *ops = br_dev->netdev_ops;
    2251                 :            : 
    2252                 :          0 :                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
    2253         [ #  # ]:          0 :                 if (err)
    2254                 :            :                         goto out;
    2255                 :            :                 else
    2256                 :          0 :                         ndm->ndm_flags &= ~NTF_MASTER;
    2257                 :            :         }
    2258                 :            : 
    2259                 :            :         /* Embedded bridge, macvlan, and any other device support */
    2260         [ #  # ]:          0 :         if ((ndm->ndm_flags & NTF_SELF)) {
    2261         [ #  # ]:          0 :                 if (dev->netdev_ops->ndo_fdb_add)
    2262                 :          0 :                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
    2263                 :            :                                                            nlh->nlmsg_flags);
    2264                 :            :                 else
    2265                 :          0 :                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
    2266                 :            :                                                nlh->nlmsg_flags);
    2267                 :            : 
    2268         [ #  # ]:          0 :                 if (!err) {
    2269                 :          0 :                         rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
    2270                 :          0 :                         ndm->ndm_flags &= ~NTF_SELF;
    2271                 :            :                 }
    2272                 :            :         }
    2273                 :            : out:
    2274                 :          0 :         return err;
    2275                 :            : }
    2276                 :            : 
    2277                 :            : /**
    2278                 :            :  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
    2279                 :            :  */
    2280                 :          0 : int ndo_dflt_fdb_del(struct ndmsg *ndm,
    2281                 :            :                      struct nlattr *tb[],
    2282                 :            :                      struct net_device *dev,
    2283                 :          0 :                      const unsigned char *addr)
    2284                 :            : {
    2285                 :            :         int err = -EOPNOTSUPP;
    2286                 :            : 
    2287                 :            :         /* If aging addresses are supported device will need to
    2288                 :            :          * implement its own handler for this.
    2289                 :            :          */
    2290         [ #  # ]:          0 :         if (!(ndm->ndm_state & NUD_PERMANENT)) {
    2291                 :          0 :                 pr_info("%s: FDB only supports static addresses\n", dev->name);
    2292                 :          0 :                 return -EINVAL;
    2293                 :            :         }
    2294                 :            : 
    2295 [ #  # ][ #  # ]:          0 :         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
    2296                 :          0 :                 err = dev_uc_del(dev, addr);
    2297         [ #  # ]:          0 :         else if (is_multicast_ether_addr(addr))
    2298                 :          0 :                 err = dev_mc_del(dev, addr);
    2299                 :            :         else
    2300                 :            :                 err = -EINVAL;
    2301                 :            : 
    2302                 :          0 :         return err;
    2303                 :            : }
    2304                 :            : EXPORT_SYMBOL(ndo_dflt_fdb_del);
    2305                 :            : 
    2306                 :          0 : static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
    2307                 :            : {
    2308                 :            :         struct net *net = sock_net(skb->sk);
    2309                 :            :         struct ndmsg *ndm;
    2310                 :            :         struct nlattr *tb[NDA_MAX+1];
    2311                 :            :         struct net_device *dev;
    2312                 :            :         int err = -EINVAL;
    2313                 :            :         __u8 *addr;
    2314                 :            : 
    2315         [ #  # ]:          0 :         if (!capable(CAP_NET_ADMIN))
    2316                 :            :                 return -EPERM;
    2317                 :            : 
    2318                 :            :         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
    2319         [ #  # ]:          0 :         if (err < 0)
    2320                 :            :                 return err;
    2321                 :            : 
    2322                 :          0 :         ndm = nlmsg_data(nlh);
    2323         [ #  # ]:          0 :         if (ndm->ndm_ifindex == 0) {
    2324                 :          0 :                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
    2325                 :          0 :                 return -EINVAL;
    2326                 :            :         }
    2327                 :            : 
    2328                 :          0 :         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
    2329         [ #  # ]:          0 :         if (dev == NULL) {
    2330                 :          0 :                 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
    2331                 :          0 :                 return -ENODEV;
    2332                 :            :         }
    2333                 :            : 
    2334 [ #  # ][ #  # ]:          0 :         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
    2335                 :          0 :                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
    2336                 :          0 :                 return -EINVAL;
    2337                 :            :         }
    2338                 :            : 
    2339                 :          0 :         addr = nla_data(tb[NDA_LLADDR]);
    2340                 :            : 
    2341                 :            :         err = -EOPNOTSUPP;
    2342                 :            : 
    2343                 :            :         /* Support fdb on master device the net/bridge default case */
    2344 [ #  # ][ #  # ]:          0 :         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
                 [ #  # ]
    2345                 :          0 :             (dev->priv_flags & IFF_BRIDGE_PORT)) {
    2346                 :          0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2347                 :          0 :                 const struct net_device_ops *ops = br_dev->netdev_ops;
    2348                 :            : 
    2349         [ #  # ]:          0 :                 if (ops->ndo_fdb_del)
    2350                 :          0 :                         err = ops->ndo_fdb_del(ndm, tb, dev, addr);
    2351                 :            : 
    2352         [ #  # ]:          0 :                 if (err)
    2353                 :            :                         goto out;
    2354                 :            :                 else
    2355                 :          0 :                         ndm->ndm_flags &= ~NTF_MASTER;
    2356                 :            :         }
    2357                 :            : 
    2358                 :            :         /* Embedded bridge, macvlan, and any other device support */
    2359         [ #  # ]:          0 :         if (ndm->ndm_flags & NTF_SELF) {
    2360         [ #  # ]:          0 :                 if (dev->netdev_ops->ndo_fdb_del)
    2361                 :          0 :                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
    2362                 :            :                 else
    2363                 :          0 :                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
    2364                 :            : 
    2365         [ #  # ]:          0 :                 if (!err) {
    2366                 :          0 :                         rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
    2367                 :          0 :                         ndm->ndm_flags &= ~NTF_SELF;
    2368                 :            :                 }
    2369                 :            :         }
    2370                 :            : out:
    2371                 :          0 :         return err;
    2372                 :            : }
    2373                 :            : 
    2374                 :          0 : static int nlmsg_populate_fdb(struct sk_buff *skb,
    2375                 :            :                               struct netlink_callback *cb,
    2376                 :            :                               struct net_device *dev,
    2377                 :            :                               int *idx,
    2378                 :            :                               struct netdev_hw_addr_list *list)
    2379                 :            : {
    2380                 :            :         struct netdev_hw_addr *ha;
    2381                 :            :         int err;
    2382                 :            :         u32 portid, seq;
    2383                 :            : 
    2384                 :          0 :         portid = NETLINK_CB(cb->skb).portid;
    2385                 :          0 :         seq = cb->nlh->nlmsg_seq;
    2386                 :            : 
    2387         [ #  # ]:          0 :         list_for_each_entry(ha, &list->list, list) {
    2388         [ #  # ]:          0 :                 if (*idx < cb->args[0])
    2389                 :            :                         goto skip;
    2390                 :            : 
    2391                 :          0 :                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
    2392                 :            :                                               portid, seq,
    2393                 :            :                                               RTM_NEWNEIGH, NTF_SELF,
    2394                 :            :                                               NLM_F_MULTI);
    2395         [ #  # ]:          0 :                 if (err < 0)
    2396                 :            :                         return err;
    2397                 :            : skip:
    2398                 :          0 :                 *idx += 1;
    2399                 :            :         }
    2400                 :            :         return 0;
    2401                 :            : }
    2402                 :            : 
    2403                 :            : /**
    2404                 :            :  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
    2405                 :            :  * @nlh: netlink message header
    2406                 :            :  * @dev: netdevice
    2407                 :            :  *
    2408                 :            :  * Default netdevice operation to dump the existing unicast address list.
    2409                 :            :  * Returns number of addresses from list put in skb.
    2410                 :            :  */
    2411                 :          0 : int ndo_dflt_fdb_dump(struct sk_buff *skb,
    2412                 :            :                       struct netlink_callback *cb,
    2413                 :            :                       struct net_device *dev,
    2414                 :            :                       int idx)
    2415                 :            : {
    2416                 :            :         int err;
    2417                 :            : 
    2418                 :            :         netif_addr_lock_bh(dev);
    2419                 :          0 :         err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
    2420         [ #  # ]:          0 :         if (err)
    2421                 :            :                 goto out;
    2422                 :          0 :         nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
    2423                 :            : out:
    2424                 :            :         netif_addr_unlock_bh(dev);
    2425                 :          0 :         return idx;
    2426                 :            : }
    2427                 :            : EXPORT_SYMBOL(ndo_dflt_fdb_dump);
    2428                 :            : 
    2429                 :          0 : static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
    2430                 :            : {
    2431                 :            :         int idx = 0;
    2432                 :            :         struct net *net = sock_net(skb->sk);
    2433                 :            :         struct net_device *dev;
    2434                 :            : 
    2435                 :            :         rcu_read_lock();
    2436         [ #  # ]:          0 :         for_each_netdev_rcu(net, dev) {
    2437         [ #  # ]:          0 :                 if (dev->priv_flags & IFF_BRIDGE_PORT) {
    2438                 :            :                         struct net_device *br_dev;
    2439                 :            :                         const struct net_device_ops *ops;
    2440                 :            : 
    2441                 :          0 :                         br_dev = netdev_master_upper_dev_get(dev);
    2442                 :          0 :                         ops = br_dev->netdev_ops;
    2443         [ #  # ]:          0 :                         if (ops->ndo_fdb_dump)
    2444                 :          0 :                                 idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
    2445                 :            :                 }
    2446                 :            : 
    2447         [ #  # ]:          0 :                 if (dev->netdev_ops->ndo_fdb_dump)
    2448                 :          0 :                         idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
    2449                 :            :                 else
    2450                 :          0 :                         idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
    2451                 :            :         }
    2452                 :            :         rcu_read_unlock();
    2453                 :            : 
    2454                 :          0 :         cb->args[0] = idx;
    2455                 :          0 :         return skb->len;
    2456                 :            : }
    2457                 :            : 
    2458                 :          0 : int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
    2459                 :            :                             struct net_device *dev, u16 mode)
    2460                 :            : {
    2461                 :            :         struct nlmsghdr *nlh;
    2462                 :            :         struct ifinfomsg *ifm;
    2463                 :            :         struct nlattr *br_afspec;
    2464         [ #  # ]:          0 :         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
    2465                 :          0 :         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2466                 :            : 
    2467                 :            :         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
    2468         [ #  # ]:          0 :         if (nlh == NULL)
    2469                 :            :                 return -EMSGSIZE;
    2470                 :            : 
    2471                 :            :         ifm = nlmsg_data(nlh);
    2472                 :          0 :         ifm->ifi_family = AF_BRIDGE;
    2473                 :          0 :         ifm->__ifi_pad = 0;
    2474                 :          0 :         ifm->ifi_type = dev->type;
    2475                 :          0 :         ifm->ifi_index = dev->ifindex;
    2476                 :          0 :         ifm->ifi_flags = dev_get_flags(dev);
    2477                 :          0 :         ifm->ifi_change = 0;
    2478                 :            : 
    2479                 :            : 
    2480   [ #  #  #  # ]:          0 :         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
    2481         [ #  # ]:          0 :             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
    2482         [ #  # ]:          0 :             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
    2483         [ #  # ]:          0 :             (br_dev &&
    2484         [ #  # ]:          0 :              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
    2485         [ #  # ]:          0 :             (dev->addr_len &&
    2486         [ #  # ]:          0 :              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
    2487         [ #  # ]:          0 :             (dev->ifindex != dev->iflink &&
    2488                 :          0 :              nla_put_u32(skb, IFLA_LINK, dev->iflink)))
    2489                 :            :                 goto nla_put_failure;
    2490                 :            : 
    2491                 :            :         br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
    2492         [ #  # ]:          0 :         if (!br_afspec)
    2493                 :            :                 goto nla_put_failure;
    2494                 :            : 
    2495   [ #  #  #  # ]:          0 :         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
    2496                 :            :             nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
    2497                 :            :                 nla_nest_cancel(skb, br_afspec);
    2498                 :            :                 goto nla_put_failure;
    2499                 :            :         }
    2500                 :            :         nla_nest_end(skb, br_afspec);
    2501                 :            : 
    2502                 :          0 :         return nlmsg_end(skb, nlh);
    2503                 :            : nla_put_failure:
    2504                 :            :         nlmsg_cancel(skb, nlh);
    2505                 :            :         return -EMSGSIZE;
    2506                 :            : }
    2507                 :            : EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
    2508                 :            : 
    2509                 :          0 : static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
    2510                 :            : {
    2511                 :            :         struct net *net = sock_net(skb->sk);
    2512                 :            :         struct net_device *dev;
    2513                 :            :         int idx = 0;
    2514                 :          0 :         u32 portid = NETLINK_CB(cb->skb).portid;
    2515                 :          0 :         u32 seq = cb->nlh->nlmsg_seq;
    2516                 :            :         struct nlattr *extfilt;
    2517                 :            :         u32 filter_mask = 0;
    2518                 :            : 
    2519                 :            :         extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
    2520                 :            :                                   IFLA_EXT_MASK);
    2521         [ #  # ]:          0 :         if (extfilt)
    2522                 :            :                 filter_mask = nla_get_u32(extfilt);
    2523                 :            : 
    2524                 :            :         rcu_read_lock();
    2525         [ #  # ]:          0 :         for_each_netdev_rcu(net, dev) {
    2526                 :          0 :                 const struct net_device_ops *ops = dev->netdev_ops;
    2527                 :          0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2528                 :            : 
    2529 [ #  # ][ #  # ]:          0 :                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
    2530   [ #  #  #  # ]:          0 :                         if (idx >= cb->args[0] &&
    2531                 :          0 :                             br_dev->netdev_ops->ndo_bridge_getlink(
    2532                 :            :                                     skb, portid, seq, dev, filter_mask) < 0)
    2533                 :            :                                 break;
    2534                 :          0 :                         idx++;
    2535                 :            :                 }
    2536                 :            : 
    2537         [ #  # ]:          0 :                 if (ops->ndo_bridge_getlink) {
    2538   [ #  #  #  # ]:          0 :                         if (idx >= cb->args[0] &&
    2539                 :          0 :                             ops->ndo_bridge_getlink(skb, portid, seq, dev,
    2540                 :            :                                                     filter_mask) < 0)
    2541                 :            :                                 break;
    2542                 :          0 :                         idx++;
    2543                 :            :                 }
    2544                 :            :         }
    2545                 :            :         rcu_read_unlock();
    2546                 :          0 :         cb->args[0] = idx;
    2547                 :            : 
    2548                 :          0 :         return skb->len;
    2549                 :            : }
    2550                 :            : 
    2551                 :            : static inline size_t bridge_nlmsg_size(void)
    2552                 :            : {
    2553                 :            :         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
    2554                 :            :                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
    2555                 :            :                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
    2556                 :            :                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
    2557                 :            :                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
    2558                 :            :                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
    2559                 :            :                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
    2560                 :            :                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
    2561                 :            :                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
    2562                 :            :                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
    2563                 :            :                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
    2564                 :            : }
    2565                 :            : 
    2566                 :          0 : static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
    2567                 :            : {
    2568                 :            :         struct net *net = dev_net(dev);
    2569                 :          0 :         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2570                 :            :         struct sk_buff *skb;
    2571                 :            :         int err = -EOPNOTSUPP;
    2572                 :            : 
    2573                 :            :         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
    2574         [ #  # ]:          0 :         if (!skb) {
    2575                 :            :                 err = -ENOMEM;
    2576                 :            :                 goto errout;
    2577                 :            :         }
    2578                 :            : 
    2579 [ #  # ][ #  # ]:          0 :         if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
                 [ #  # ]
    2580         [ #  # ]:          0 :             br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
    2581                 :          0 :                 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
    2582         [ #  # ]:          0 :                 if (err < 0)
    2583                 :            :                         goto errout;
    2584                 :            :         }
    2585                 :            : 
    2586 [ #  # ][ #  # ]:          0 :         if ((flags & BRIDGE_FLAGS_SELF) &&
    2587                 :          0 :             dev->netdev_ops->ndo_bridge_getlink) {
    2588                 :          0 :                 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
    2589         [ #  # ]:          0 :                 if (err < 0)
    2590                 :            :                         goto errout;
    2591                 :            :         }
    2592                 :            : 
    2593                 :            :         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
    2594                 :          0 :         return 0;
    2595                 :            : errout:
    2596         [ #  # ]:          0 :         WARN_ON(err == -EMSGSIZE);
    2597                 :          0 :         kfree_skb(skb);
    2598                 :            :         rtnl_set_sk_err(net, RTNLGRP_LINK, err);
    2599                 :          0 :         return err;
    2600                 :            : }
    2601                 :            : 
    2602                 :          0 : static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
    2603                 :            : {
    2604                 :            :         struct net *net = sock_net(skb->sk);
    2605                 :            :         struct ifinfomsg *ifm;
    2606                 :            :         struct net_device *dev;
    2607                 :          0 :         struct nlattr *br_spec, *attr = NULL;
    2608                 :            :         int rem, err = -EOPNOTSUPP;
    2609                 :          0 :         u16 oflags, flags = 0;
    2610                 :            :         bool have_flags = false;
    2611                 :            : 
    2612         [ #  # ]:          0 :         if (nlmsg_len(nlh) < sizeof(*ifm))
    2613                 :            :                 return -EINVAL;
    2614                 :            : 
    2615                 :            :         ifm = nlmsg_data(nlh);
    2616         [ #  # ]:          0 :         if (ifm->ifi_family != AF_BRIDGE)
    2617                 :            :                 return -EPFNOSUPPORT;
    2618                 :            : 
    2619                 :          0 :         dev = __dev_get_by_index(net, ifm->ifi_index);
    2620         [ #  # ]:          0 :         if (!dev) {
    2621                 :          0 :                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
    2622                 :          0 :                 return -ENODEV;
    2623                 :            :         }
    2624                 :            : 
    2625                 :            :         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
    2626         [ #  # ]:          0 :         if (br_spec) {
    2627         [ #  # ]:          0 :                 nla_for_each_nested(attr, br_spec, rem) {
    2628         [ #  # ]:          0 :                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
    2629                 :            :                                 have_flags = true;
    2630                 :          0 :                                 flags = nla_get_u16(attr);
    2631                 :          0 :                                 break;
    2632                 :            :                         }
    2633                 :            :                 }
    2634                 :            :         }
    2635                 :            : 
    2636                 :          0 :         oflags = flags;
    2637                 :            : 
    2638 [ #  # ][ #  # ]:          0 :         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
    2639                 :          0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2640                 :            : 
    2641 [ #  # ][ #  # ]:          0 :                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
    2642                 :            :                         err = -EOPNOTSUPP;
    2643                 :            :                         goto out;
    2644                 :            :                 }
    2645                 :            : 
    2646                 :          0 :                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
    2647         [ #  # ]:          0 :                 if (err)
    2648                 :            :                         goto out;
    2649                 :            : 
    2650                 :          0 :                 flags &= ~BRIDGE_FLAGS_MASTER;
    2651                 :            :         }
    2652                 :            : 
    2653         [ #  # ]:          0 :         if ((flags & BRIDGE_FLAGS_SELF)) {
    2654         [ #  # ]:          0 :                 if (!dev->netdev_ops->ndo_bridge_setlink)
    2655                 :            :                         err = -EOPNOTSUPP;
    2656                 :            :                 else
    2657                 :          0 :                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
    2658                 :            : 
    2659         [ #  # ]:          0 :                 if (!err)
    2660                 :          0 :                         flags &= ~BRIDGE_FLAGS_SELF;
    2661                 :            :         }
    2662                 :            : 
    2663         [ #  # ]:          0 :         if (have_flags)
    2664                 :          0 :                 memcpy(nla_data(attr), &flags, sizeof(flags));
    2665                 :            :         /* Generate event to notify upper layer of bridge change */
    2666         [ #  # ]:          0 :         if (!err)
    2667                 :          0 :                 err = rtnl_bridge_notify(dev, oflags);
    2668                 :            : out:
    2669                 :          0 :         return err;
    2670                 :            : }
    2671                 :            : 
    2672                 :          0 : static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
    2673                 :            : {
    2674                 :            :         struct net *net = sock_net(skb->sk);
    2675                 :            :         struct ifinfomsg *ifm;
    2676                 :            :         struct net_device *dev;
    2677                 :          0 :         struct nlattr *br_spec, *attr = NULL;
    2678                 :            :         int rem, err = -EOPNOTSUPP;
    2679                 :          0 :         u16 oflags, flags = 0;
    2680                 :            :         bool have_flags = false;
    2681                 :            : 
    2682         [ #  # ]:          0 :         if (nlmsg_len(nlh) < sizeof(*ifm))
    2683                 :            :                 return -EINVAL;
    2684                 :            : 
    2685                 :            :         ifm = nlmsg_data(nlh);
    2686         [ #  # ]:          0 :         if (ifm->ifi_family != AF_BRIDGE)
    2687                 :            :                 return -EPFNOSUPPORT;
    2688                 :            : 
    2689                 :          0 :         dev = __dev_get_by_index(net, ifm->ifi_index);
    2690         [ #  # ]:          0 :         if (!dev) {
    2691                 :          0 :                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
    2692                 :          0 :                 return -ENODEV;
    2693                 :            :         }
    2694                 :            : 
    2695                 :            :         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
    2696         [ #  # ]:          0 :         if (br_spec) {
    2697         [ #  # ]:          0 :                 nla_for_each_nested(attr, br_spec, rem) {
    2698         [ #  # ]:          0 :                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
    2699                 :            :                                 have_flags = true;
    2700                 :          0 :                                 flags = nla_get_u16(attr);
    2701                 :          0 :                                 break;
    2702                 :            :                         }
    2703                 :            :                 }
    2704                 :            :         }
    2705                 :            : 
    2706                 :          0 :         oflags = flags;
    2707                 :            : 
    2708 [ #  # ][ #  # ]:          0 :         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
    2709                 :          0 :                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
    2710                 :            : 
    2711 [ #  # ][ #  # ]:          0 :                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
    2712                 :            :                         err = -EOPNOTSUPP;
    2713                 :            :                         goto out;
    2714                 :            :                 }
    2715                 :            : 
    2716                 :          0 :                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
    2717         [ #  # ]:          0 :                 if (err)
    2718                 :            :                         goto out;
    2719                 :            : 
    2720                 :          0 :                 flags &= ~BRIDGE_FLAGS_MASTER;
    2721                 :            :         }
    2722                 :            : 
    2723         [ #  # ]:          0 :         if ((flags & BRIDGE_FLAGS_SELF)) {
    2724         [ #  # ]:          0 :                 if (!dev->netdev_ops->ndo_bridge_dellink)
    2725                 :            :                         err = -EOPNOTSUPP;
    2726                 :            :                 else
    2727                 :          0 :                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
    2728                 :            : 
    2729         [ #  # ]:          0 :                 if (!err)
    2730                 :          0 :                         flags &= ~BRIDGE_FLAGS_SELF;
    2731                 :            :         }
    2732                 :            : 
    2733         [ #  # ]:          0 :         if (have_flags)
    2734                 :          0 :                 memcpy(nla_data(attr), &flags, sizeof(flags));
    2735                 :            :         /* Generate event to notify upper layer of bridge change */
    2736         [ #  # ]:          0 :         if (!err)
    2737                 :          0 :                 err = rtnl_bridge_notify(dev, oflags);
    2738                 :            : out:
    2739                 :          0 :         return err;
    2740                 :            : }
    2741                 :            : 
    2742                 :            : /* Process one rtnetlink message. */
    2743                 :            : 
    2744                 :          0 : static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
    2745                 :            : {
    2746                 :            :         struct net *net = sock_net(skb->sk);
    2747                 :            :         rtnl_doit_func doit;
    2748                 :            :         int sz_idx, kind;
    2749                 :            :         int family;
    2750                 :            :         int type;
    2751                 :            :         int err;
    2752                 :            : 
    2753                 :       1111 :         type = nlh->nlmsg_type;
    2754         [ +  - ]:       1111 :         if (type > RTM_MAX)
    2755                 :            :                 return -EOPNOTSUPP;
    2756                 :            : 
    2757                 :       1111 :         type -= RTM_BASE;
    2758                 :            : 
    2759                 :            :         /* All the messages must have at least 1 byte length */
    2760         [ +  - ]:       1111 :         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
    2761                 :            :                 return 0;
    2762                 :            : 
    2763                 :       1111 :         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
    2764                 :            :         sz_idx = type>>2;
    2765                 :       1111 :         kind = type&3;
    2766                 :            : 
    2767 [ +  + ][ +  - ]:       1111 :         if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
    2768                 :            :                 return -EPERM;
    2769                 :            : 
    2770 [ +  + ][ +  - ]:       1111 :         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
    2771                 :            :                 struct sock *rtnl;
    2772                 :            :                 rtnl_dumpit_func dumpit;
    2773                 :            :                 rtnl_calcit_func calcit;
    2774                 :            :                 u16 min_dump_alloc = 0;
    2775                 :            : 
    2776                 :            :                 dumpit = rtnl_get_dumpit(family, type);
    2777         [ +  - ]:       1109 :                 if (dumpit == NULL)
    2778                 :            :                         return -EOPNOTSUPP;
    2779                 :            :                 calcit = rtnl_get_calcit(family, type);
    2780         [ +  + ]:       1109 :                 if (calcit)
    2781                 :        555 :                         min_dump_alloc = calcit(skb, nlh);
    2782                 :            : 
    2783                 :            :                 __rtnl_unlock();
    2784                 :       1109 :                 rtnl = net->rtnl;
    2785                 :            :                 {
    2786                 :       1109 :                         struct netlink_dump_control c = {
    2787                 :            :                                 .dump           = dumpit,
    2788                 :            :                                 .min_dump_alloc = min_dump_alloc,
    2789                 :            :                         };
    2790                 :            :                         err = netlink_dump_start(rtnl, skb, nlh, &c);
    2791                 :            :                 }
    2792                 :            :                 rtnl_lock();
    2793                 :       1109 :                 return err;
    2794                 :            :         }
    2795                 :            : 
    2796                 :            :         doit = rtnl_get_doit(family, type);
    2797         [ +  - ]:          2 :         if (doit == NULL)
    2798                 :            :                 return -EOPNOTSUPP;
    2799                 :            : 
    2800                 :          2 :         return doit(skb, nlh);
    2801                 :            : }
    2802                 :            : 
    2803                 :          0 : static void rtnetlink_rcv(struct sk_buff *skb)
    2804                 :            : {
    2805                 :            :         rtnl_lock();
    2806                 :       1111 :         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
    2807                 :            :         rtnl_unlock();
    2808                 :       1111 : }
    2809                 :            : 
    2810                 :          0 : static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
    2811                 :            : {
    2812                 :            :         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
    2813                 :            : 
    2814                 :            :         switch (event) {
    2815                 :            :         case NETDEV_UP:
    2816                 :            :         case NETDEV_DOWN:
    2817                 :            :         case NETDEV_PRE_UP:
    2818                 :            :         case NETDEV_POST_INIT:
    2819                 :            :         case NETDEV_REGISTER:
    2820                 :            :         case NETDEV_CHANGE:
    2821                 :            :         case NETDEV_PRE_TYPE_CHANGE:
    2822                 :            :         case NETDEV_GOING_DOWN:
    2823                 :            :         case NETDEV_UNREGISTER:
    2824                 :            :         case NETDEV_UNREGISTER_FINAL:
    2825                 :            :         case NETDEV_RELEASE:
    2826                 :            :         case NETDEV_JOIN:
    2827                 :            :                 break;
    2828                 :            :         default:
    2829                 :          0 :                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
    2830                 :          0 :                 break;
    2831                 :            :         }
    2832                 :          0 :         return NOTIFY_DONE;
    2833                 :            : }
    2834                 :            : 
    2835                 :            : static struct notifier_block rtnetlink_dev_notifier = {
    2836                 :            :         .notifier_call  = rtnetlink_event,
    2837                 :            : };
    2838                 :            : 
    2839                 :            : 
    2840                 :          0 : static int __net_init rtnetlink_net_init(struct net *net)
    2841                 :            : {
    2842                 :            :         struct sock *sk;
    2843                 :          0 :         struct netlink_kernel_cfg cfg = {
    2844                 :            :                 .groups         = RTNLGRP_MAX,
    2845                 :            :                 .input          = rtnetlink_rcv,
    2846                 :            :                 .cb_mutex       = &rtnl_mutex,
    2847                 :            :                 .flags          = NL_CFG_F_NONROOT_RECV,
    2848                 :            :         };
    2849                 :            : 
    2850                 :            :         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
    2851         [ #  # ]:          0 :         if (!sk)
    2852                 :            :                 return -ENOMEM;
    2853                 :          0 :         net->rtnl = sk;
    2854                 :          0 :         return 0;
    2855                 :            : }
    2856                 :            : 
    2857                 :          0 : static void __net_exit rtnetlink_net_exit(struct net *net)
    2858                 :            : {
    2859                 :          0 :         netlink_kernel_release(net->rtnl);
    2860                 :          0 :         net->rtnl = NULL;
    2861                 :          0 : }
    2862                 :            : 
    2863                 :            : static struct pernet_operations rtnetlink_net_ops = {
    2864                 :            :         .init = rtnetlink_net_init,
    2865                 :            :         .exit = rtnetlink_net_exit,
    2866                 :            : };
    2867                 :            : 
    2868                 :          0 : void __init rtnetlink_init(void)
    2869                 :            : {
    2870         [ #  # ]:          0 :         if (register_pernet_subsys(&rtnetlink_net_ops))
    2871                 :          0 :                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
    2872                 :            : 
    2873                 :          0 :         register_netdevice_notifier(&rtnetlink_dev_notifier);
    2874                 :            : 
    2875                 :          0 :         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
    2876                 :            :                       rtnl_dump_ifinfo, rtnl_calcit);
    2877                 :          0 :         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
    2878                 :          0 :         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
    2879                 :          0 :         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
    2880                 :            : 
    2881                 :          0 :         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
    2882                 :          0 :         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
    2883                 :            : 
    2884                 :          0 :         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
    2885                 :          0 :         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
    2886                 :          0 :         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
    2887                 :            : 
    2888                 :          0 :         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
    2889                 :          0 :         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
    2890                 :          0 :         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
    2891                 :          0 : }
    2892                 :            : 

Generated by: LCOV version 1.9