~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/rtnetlink.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php