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