Merge tag 'mmc-v5.17-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[linux-2.6-microblaze.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_INIT_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_INIT_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(4) /* IFLA_GRO_MAX_SIZE */
1030                + nla_total_size(1) /* IFLA_OPERSTATE */
1031                + nla_total_size(1) /* IFLA_LINKMODE */
1032                + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1033                + nla_total_size(4) /* IFLA_LINK_NETNSID */
1034                + nla_total_size(4) /* IFLA_GROUP */
1035                + nla_total_size(ext_filter_mask
1036                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1037                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1038                + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1039                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1040                + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1041                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1042                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1043                + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1044                + rtnl_xdp_size() /* IFLA_XDP */
1045                + nla_total_size(4)  /* IFLA_EVENT */
1046                + nla_total_size(4)  /* IFLA_NEW_NETNSID */
1047                + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1048                + rtnl_proto_down_size(dev)  /* proto down */
1049                + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1050                + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1051                + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
1052                + nla_total_size(4)  /* IFLA_MIN_MTU */
1053                + nla_total_size(4)  /* IFLA_MAX_MTU */
1054                + rtnl_prop_list_size(dev)
1055                + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1056                + 0;
1057 }
1058
1059 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1060 {
1061         struct nlattr *vf_ports;
1062         struct nlattr *vf_port;
1063         int vf;
1064         int err;
1065
1066         vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1067         if (!vf_ports)
1068                 return -EMSGSIZE;
1069
1070         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1071                 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1072                 if (!vf_port)
1073                         goto nla_put_failure;
1074                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1075                         goto nla_put_failure;
1076                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1077                 if (err == -EMSGSIZE)
1078                         goto nla_put_failure;
1079                 if (err) {
1080                         nla_nest_cancel(skb, vf_port);
1081                         continue;
1082                 }
1083                 nla_nest_end(skb, vf_port);
1084         }
1085
1086         nla_nest_end(skb, vf_ports);
1087
1088         return 0;
1089
1090 nla_put_failure:
1091         nla_nest_cancel(skb, vf_ports);
1092         return -EMSGSIZE;
1093 }
1094
1095 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1096 {
1097         struct nlattr *port_self;
1098         int err;
1099
1100         port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1101         if (!port_self)
1102                 return -EMSGSIZE;
1103
1104         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1105         if (err) {
1106                 nla_nest_cancel(skb, port_self);
1107                 return (err == -EMSGSIZE) ? err : 0;
1108         }
1109
1110         nla_nest_end(skb, port_self);
1111
1112         return 0;
1113 }
1114
1115 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1116                           u32 ext_filter_mask)
1117 {
1118         int err;
1119
1120         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1121             !(ext_filter_mask & RTEXT_FILTER_VF))
1122                 return 0;
1123
1124         err = rtnl_port_self_fill(skb, dev);
1125         if (err)
1126                 return err;
1127
1128         if (dev_num_vf(dev->dev.parent)) {
1129                 err = rtnl_vf_ports_fill(skb, dev);
1130                 if (err)
1131                         return err;
1132         }
1133
1134         return 0;
1135 }
1136
1137 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1138 {
1139         int err;
1140         struct netdev_phys_item_id ppid;
1141
1142         err = dev_get_phys_port_id(dev, &ppid);
1143         if (err) {
1144                 if (err == -EOPNOTSUPP)
1145                         return 0;
1146                 return err;
1147         }
1148
1149         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1150                 return -EMSGSIZE;
1151
1152         return 0;
1153 }
1154
1155 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1156 {
1157         char name[IFNAMSIZ];
1158         int err;
1159
1160         err = dev_get_phys_port_name(dev, name, sizeof(name));
1161         if (err) {
1162                 if (err == -EOPNOTSUPP)
1163                         return 0;
1164                 return err;
1165         }
1166
1167         if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1168                 return -EMSGSIZE;
1169
1170         return 0;
1171 }
1172
1173 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1174 {
1175         struct netdev_phys_item_id ppid = { };
1176         int err;
1177
1178         err = dev_get_port_parent_id(dev, &ppid, false);
1179         if (err) {
1180                 if (err == -EOPNOTSUPP)
1181                         return 0;
1182                 return err;
1183         }
1184
1185         if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1186                 return -EMSGSIZE;
1187
1188         return 0;
1189 }
1190
1191 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1192                                               struct net_device *dev)
1193 {
1194         struct rtnl_link_stats64 *sp;
1195         struct nlattr *attr;
1196
1197         attr = nla_reserve_64bit(skb, IFLA_STATS64,
1198                                  sizeof(struct rtnl_link_stats64), IFLA_PAD);
1199         if (!attr)
1200                 return -EMSGSIZE;
1201
1202         sp = nla_data(attr);
1203         dev_get_stats(dev, sp);
1204
1205         attr = nla_reserve(skb, IFLA_STATS,
1206                            sizeof(struct rtnl_link_stats));
1207         if (!attr)
1208                 return -EMSGSIZE;
1209
1210         copy_rtnl_link_stats(nla_data(attr), sp);
1211
1212         return 0;
1213 }
1214
1215 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1216                                                struct net_device *dev,
1217                                                int vfs_num,
1218                                                struct nlattr *vfinfo)
1219 {
1220         struct ifla_vf_rss_query_en vf_rss_query_en;
1221         struct nlattr *vf, *vfstats, *vfvlanlist;
1222         struct ifla_vf_link_state vf_linkstate;
1223         struct ifla_vf_vlan_info vf_vlan_info;
1224         struct ifla_vf_spoofchk vf_spoofchk;
1225         struct ifla_vf_tx_rate vf_tx_rate;
1226         struct ifla_vf_stats vf_stats;
1227         struct ifla_vf_trust vf_trust;
1228         struct ifla_vf_vlan vf_vlan;
1229         struct ifla_vf_rate vf_rate;
1230         struct ifla_vf_mac vf_mac;
1231         struct ifla_vf_broadcast vf_broadcast;
1232         struct ifla_vf_info ivi;
1233         struct ifla_vf_guid node_guid;
1234         struct ifla_vf_guid port_guid;
1235
1236         memset(&ivi, 0, sizeof(ivi));
1237
1238         /* Not all SR-IOV capable drivers support the
1239          * spoofcheck and "RSS query enable" query.  Preset to
1240          * -1 so the user space tool can detect that the driver
1241          * didn't report anything.
1242          */
1243         ivi.spoofchk = -1;
1244         ivi.rss_query_en = -1;
1245         ivi.trusted = -1;
1246         /* The default value for VF link state is "auto"
1247          * IFLA_VF_LINK_STATE_AUTO which equals zero
1248          */
1249         ivi.linkstate = 0;
1250         /* VLAN Protocol by default is 802.1Q */
1251         ivi.vlan_proto = htons(ETH_P_8021Q);
1252         if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1253                 return 0;
1254
1255         memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1256         memset(&node_guid, 0, sizeof(node_guid));
1257         memset(&port_guid, 0, sizeof(port_guid));
1258
1259         vf_mac.vf =
1260                 vf_vlan.vf =
1261                 vf_vlan_info.vf =
1262                 vf_rate.vf =
1263                 vf_tx_rate.vf =
1264                 vf_spoofchk.vf =
1265                 vf_linkstate.vf =
1266                 vf_rss_query_en.vf =
1267                 vf_trust.vf =
1268                 node_guid.vf =
1269                 port_guid.vf = ivi.vf;
1270
1271         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1272         memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1273         vf_vlan.vlan = ivi.vlan;
1274         vf_vlan.qos = ivi.qos;
1275         vf_vlan_info.vlan = ivi.vlan;
1276         vf_vlan_info.qos = ivi.qos;
1277         vf_vlan_info.vlan_proto = ivi.vlan_proto;
1278         vf_tx_rate.rate = ivi.max_tx_rate;
1279         vf_rate.min_tx_rate = ivi.min_tx_rate;
1280         vf_rate.max_tx_rate = ivi.max_tx_rate;
1281         vf_spoofchk.setting = ivi.spoofchk;
1282         vf_linkstate.link_state = ivi.linkstate;
1283         vf_rss_query_en.setting = ivi.rss_query_en;
1284         vf_trust.setting = ivi.trusted;
1285         vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1286         if (!vf)
1287                 goto nla_put_vfinfo_failure;
1288         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1289             nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1290             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1291             nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1292                     &vf_rate) ||
1293             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1294                     &vf_tx_rate) ||
1295             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1296                     &vf_spoofchk) ||
1297             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1298                     &vf_linkstate) ||
1299             nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1300                     sizeof(vf_rss_query_en),
1301                     &vf_rss_query_en) ||
1302             nla_put(skb, IFLA_VF_TRUST,
1303                     sizeof(vf_trust), &vf_trust))
1304                 goto nla_put_vf_failure;
1305
1306         if (dev->netdev_ops->ndo_get_vf_guid &&
1307             !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1308                                               &port_guid)) {
1309                 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1310                             &node_guid) ||
1311                     nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1312                             &port_guid))
1313                         goto nla_put_vf_failure;
1314         }
1315         vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1316         if (!vfvlanlist)
1317                 goto nla_put_vf_failure;
1318         if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1319                     &vf_vlan_info)) {
1320                 nla_nest_cancel(skb, vfvlanlist);
1321                 goto nla_put_vf_failure;
1322         }
1323         nla_nest_end(skb, vfvlanlist);
1324         memset(&vf_stats, 0, sizeof(vf_stats));
1325         if (dev->netdev_ops->ndo_get_vf_stats)
1326                 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1327                                                 &vf_stats);
1328         vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1329         if (!vfstats)
1330                 goto nla_put_vf_failure;
1331         if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1332                               vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1333             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1334                               vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1335             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1336                               vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1337             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1338                               vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1339             nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1340                               vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1341             nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1342                               vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1343             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1344                               vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1345             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1346                               vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1347                 nla_nest_cancel(skb, vfstats);
1348                 goto nla_put_vf_failure;
1349         }
1350         nla_nest_end(skb, vfstats);
1351         nla_nest_end(skb, vf);
1352         return 0;
1353
1354 nla_put_vf_failure:
1355         nla_nest_cancel(skb, vf);
1356 nla_put_vfinfo_failure:
1357         nla_nest_cancel(skb, vfinfo);
1358         return -EMSGSIZE;
1359 }
1360
1361 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1362                                            struct net_device *dev,
1363                                            u32 ext_filter_mask)
1364 {
1365         struct nlattr *vfinfo;
1366         int i, num_vfs;
1367
1368         if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1369                 return 0;
1370
1371         num_vfs = dev_num_vf(dev->dev.parent);
1372         if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1373                 return -EMSGSIZE;
1374
1375         if (!dev->netdev_ops->ndo_get_vf_config)
1376                 return 0;
1377
1378         vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1379         if (!vfinfo)
1380                 return -EMSGSIZE;
1381
1382         for (i = 0; i < num_vfs; i++) {
1383                 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1384                         return -EMSGSIZE;
1385         }
1386
1387         nla_nest_end(skb, vfinfo);
1388         return 0;
1389 }
1390
1391 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1392 {
1393         struct rtnl_link_ifmap map;
1394
1395         memset(&map, 0, sizeof(map));
1396         map.mem_start   = dev->mem_start;
1397         map.mem_end     = dev->mem_end;
1398         map.base_addr   = dev->base_addr;
1399         map.irq         = dev->irq;
1400         map.dma         = dev->dma;
1401         map.port        = dev->if_port;
1402
1403         if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1404                 return -EMSGSIZE;
1405
1406         return 0;
1407 }
1408
1409 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1410 {
1411         const struct bpf_prog *generic_xdp_prog;
1412
1413         ASSERT_RTNL();
1414
1415         generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1416         if (!generic_xdp_prog)
1417                 return 0;
1418         return generic_xdp_prog->aux->id;
1419 }
1420
1421 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1422 {
1423         return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1424 }
1425
1426 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1427 {
1428         return dev_xdp_prog_id(dev, XDP_MODE_HW);
1429 }
1430
1431 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1432                                u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1433                                u32 (*get_prog_id)(struct net_device *dev))
1434 {
1435         u32 curr_id;
1436         int err;
1437
1438         curr_id = get_prog_id(dev);
1439         if (!curr_id)
1440                 return 0;
1441
1442         *prog_id = curr_id;
1443         err = nla_put_u32(skb, attr, curr_id);
1444         if (err)
1445                 return err;
1446
1447         if (*mode != XDP_ATTACHED_NONE)
1448                 *mode = XDP_ATTACHED_MULTI;
1449         else
1450                 *mode = tgt_mode;
1451
1452         return 0;
1453 }
1454
1455 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1456 {
1457         struct nlattr *xdp;
1458         u32 prog_id;
1459         int err;
1460         u8 mode;
1461
1462         xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1463         if (!xdp)
1464                 return -EMSGSIZE;
1465
1466         prog_id = 0;
1467         mode = XDP_ATTACHED_NONE;
1468         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1469                                   IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1470         if (err)
1471                 goto err_cancel;
1472         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1473                                   IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1474         if (err)
1475                 goto err_cancel;
1476         err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1477                                   IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1478         if (err)
1479                 goto err_cancel;
1480
1481         err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1482         if (err)
1483                 goto err_cancel;
1484
1485         if (prog_id && mode != XDP_ATTACHED_MULTI) {
1486                 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1487                 if (err)
1488                         goto err_cancel;
1489         }
1490
1491         nla_nest_end(skb, xdp);
1492         return 0;
1493
1494 err_cancel:
1495         nla_nest_cancel(skb, xdp);
1496         return err;
1497 }
1498
1499 static u32 rtnl_get_event(unsigned long event)
1500 {
1501         u32 rtnl_event_type = IFLA_EVENT_NONE;
1502
1503         switch (event) {
1504         case NETDEV_REBOOT:
1505                 rtnl_event_type = IFLA_EVENT_REBOOT;
1506                 break;
1507         case NETDEV_FEAT_CHANGE:
1508                 rtnl_event_type = IFLA_EVENT_FEATURES;
1509                 break;
1510         case NETDEV_BONDING_FAILOVER:
1511                 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1512                 break;
1513         case NETDEV_NOTIFY_PEERS:
1514                 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1515                 break;
1516         case NETDEV_RESEND_IGMP:
1517                 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1518                 break;
1519         case NETDEV_CHANGEINFODATA:
1520                 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1521                 break;
1522         default:
1523                 break;
1524         }
1525
1526         return rtnl_event_type;
1527 }
1528
1529 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1530 {
1531         const struct net_device *upper_dev;
1532         int ret = 0;
1533
1534         rcu_read_lock();
1535
1536         upper_dev = netdev_master_upper_dev_get_rcu(dev);
1537         if (upper_dev)
1538                 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1539
1540         rcu_read_unlock();
1541         return ret;
1542 }
1543
1544 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1545                           bool force)
1546 {
1547         int ifindex = dev_get_iflink(dev);
1548
1549         if (force || dev->ifindex != ifindex)
1550                 return nla_put_u32(skb, IFLA_LINK, ifindex);
1551
1552         return 0;
1553 }
1554
1555 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1556                                               struct net_device *dev)
1557 {
1558         char buf[IFALIASZ];
1559         int ret;
1560
1561         ret = dev_get_alias(dev, buf, sizeof(buf));
1562         return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1563 }
1564
1565 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1566                                   const struct net_device *dev,
1567                                   struct net *src_net, gfp_t gfp)
1568 {
1569         bool put_iflink = false;
1570
1571         if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1572                 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1573
1574                 if (!net_eq(dev_net(dev), link_net)) {
1575                         int id = peernet2id_alloc(src_net, link_net, gfp);
1576
1577                         if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1578                                 return -EMSGSIZE;
1579
1580                         put_iflink = true;
1581                 }
1582         }
1583
1584         return nla_put_iflink(skb, dev, put_iflink);
1585 }
1586
1587 static int rtnl_fill_link_af(struct sk_buff *skb,
1588                              const struct net_device *dev,
1589                              u32 ext_filter_mask)
1590 {
1591         const struct rtnl_af_ops *af_ops;
1592         struct nlattr *af_spec;
1593
1594         af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1595         if (!af_spec)
1596                 return -EMSGSIZE;
1597
1598         list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1599                 struct nlattr *af;
1600                 int err;
1601
1602                 if (!af_ops->fill_link_af)
1603                         continue;
1604
1605                 af = nla_nest_start_noflag(skb, af_ops->family);
1606                 if (!af)
1607                         return -EMSGSIZE;
1608
1609                 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1610                 /*
1611                  * Caller may return ENODATA to indicate that there
1612                  * was no data to be dumped. This is not an error, it
1613                  * means we should trim the attribute header and
1614                  * continue.
1615                  */
1616                 if (err == -ENODATA)
1617                         nla_nest_cancel(skb, af);
1618                 else if (err < 0)
1619                         return -EMSGSIZE;
1620
1621                 nla_nest_end(skb, af);
1622         }
1623
1624         nla_nest_end(skb, af_spec);
1625         return 0;
1626 }
1627
1628 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1629                                  const struct net_device *dev)
1630 {
1631         struct netdev_name_node *name_node;
1632         int count = 0;
1633
1634         list_for_each_entry(name_node, &dev->name_node->list, list) {
1635                 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1636                         return -EMSGSIZE;
1637                 count++;
1638         }
1639         return count;
1640 }
1641
1642 static int rtnl_fill_prop_list(struct sk_buff *skb,
1643                                const struct net_device *dev)
1644 {
1645         struct nlattr *prop_list;
1646         int ret;
1647
1648         prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1649         if (!prop_list)
1650                 return -EMSGSIZE;
1651
1652         ret = rtnl_fill_alt_ifnames(skb, dev);
1653         if (ret <= 0)
1654                 goto nest_cancel;
1655
1656         nla_nest_end(skb, prop_list);
1657         return 0;
1658
1659 nest_cancel:
1660         nla_nest_cancel(skb, prop_list);
1661         return ret;
1662 }
1663
1664 static int rtnl_fill_proto_down(struct sk_buff *skb,
1665                                 const struct net_device *dev)
1666 {
1667         struct nlattr *pr;
1668         u32 preason;
1669
1670         if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1671                 goto nla_put_failure;
1672
1673         preason = dev->proto_down_reason;
1674         if (!preason)
1675                 return 0;
1676
1677         pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1678         if (!pr)
1679                 return -EMSGSIZE;
1680
1681         if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1682                 nla_nest_cancel(skb, pr);
1683                 goto nla_put_failure;
1684         }
1685
1686         nla_nest_end(skb, pr);
1687         return 0;
1688
1689 nla_put_failure:
1690         return -EMSGSIZE;
1691 }
1692
1693 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1694                             struct net_device *dev, struct net *src_net,
1695                             int type, u32 pid, u32 seq, u32 change,
1696                             unsigned int flags, u32 ext_filter_mask,
1697                             u32 event, int *new_nsid, int new_ifindex,
1698                             int tgt_netnsid, gfp_t gfp)
1699 {
1700         struct ifinfomsg *ifm;
1701         struct nlmsghdr *nlh;
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         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1720             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1721             nla_put_u8(skb, IFLA_OPERSTATE,
1722                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1723             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1724             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1725             nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1726             nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1727             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1728             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1729             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1730             nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1731             nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1732             nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_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             (dev->qdisc &&
1739              nla_put_string(skb, IFLA_QDISC, dev->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         [IFLA_GRO_MAX_SIZE]     = { .type = NLA_U32 },
1886 };
1887
1888 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1889         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1890         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1891         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
1892         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
1893 };
1894
1895 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1896         [IFLA_VF_MAC]           = { .len = sizeof(struct ifla_vf_mac) },
1897         [IFLA_VF_BROADCAST]     = { .type = NLA_REJECT },
1898         [IFLA_VF_VLAN]          = { .len = sizeof(struct ifla_vf_vlan) },
1899         [IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1900         [IFLA_VF_TX_RATE]       = { .len = sizeof(struct ifla_vf_tx_rate) },
1901         [IFLA_VF_SPOOFCHK]      = { .len = sizeof(struct ifla_vf_spoofchk) },
1902         [IFLA_VF_RATE]          = { .len = sizeof(struct ifla_vf_rate) },
1903         [IFLA_VF_LINK_STATE]    = { .len = sizeof(struct ifla_vf_link_state) },
1904         [IFLA_VF_RSS_QUERY_EN]  = { .len = sizeof(struct ifla_vf_rss_query_en) },
1905         [IFLA_VF_STATS]         = { .type = NLA_NESTED },
1906         [IFLA_VF_TRUST]         = { .len = sizeof(struct ifla_vf_trust) },
1907         [IFLA_VF_IB_NODE_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1908         [IFLA_VF_IB_PORT_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1909 };
1910
1911 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1912         [IFLA_PORT_VF]          = { .type = NLA_U32 },
1913         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
1914                                     .len = PORT_PROFILE_MAX },
1915         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1916                                       .len = PORT_UUID_MAX },
1917         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
1918                                     .len = PORT_UUID_MAX },
1919         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
1920         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
1921
1922         /* Unused, but we need to keep it here since user space could
1923          * fill it. It's also broken with regard to NLA_BINARY use in
1924          * combination with structs.
1925          */
1926         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
1927                                     .len = sizeof(struct ifla_port_vsi) },
1928 };
1929
1930 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1931         [IFLA_XDP_UNSPEC]       = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1932         [IFLA_XDP_FD]           = { .type = NLA_S32 },
1933         [IFLA_XDP_EXPECTED_FD]  = { .type = NLA_S32 },
1934         [IFLA_XDP_ATTACHED]     = { .type = NLA_U8 },
1935         [IFLA_XDP_FLAGS]        = { .type = NLA_U32 },
1936         [IFLA_XDP_PROG_ID]      = { .type = NLA_U32 },
1937 };
1938
1939 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1940 {
1941         const struct rtnl_link_ops *ops = NULL;
1942         struct nlattr *linfo[IFLA_INFO_MAX + 1];
1943
1944         if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1945                 return NULL;
1946
1947         if (linfo[IFLA_INFO_KIND]) {
1948                 char kind[MODULE_NAME_LEN];
1949
1950                 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1951                 ops = rtnl_link_ops_get(kind);
1952         }
1953
1954         return ops;
1955 }
1956
1957 static bool link_master_filtered(struct net_device *dev, int master_idx)
1958 {
1959         struct net_device *master;
1960
1961         if (!master_idx)
1962                 return false;
1963
1964         master = netdev_master_upper_dev_get(dev);
1965
1966         /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
1967          * another invalid value for ifindex to denote "no master".
1968          */
1969         if (master_idx == -1)
1970                 return !!master;
1971
1972         if (!master || master->ifindex != master_idx)
1973                 return true;
1974
1975         return false;
1976 }
1977
1978 static bool link_kind_filtered(const struct net_device *dev,
1979                                const struct rtnl_link_ops *kind_ops)
1980 {
1981         if (kind_ops && dev->rtnl_link_ops != kind_ops)
1982                 return true;
1983
1984         return false;
1985 }
1986
1987 static bool link_dump_filtered(struct net_device *dev,
1988                                int master_idx,
1989                                const struct rtnl_link_ops *kind_ops)
1990 {
1991         if (link_master_filtered(dev, master_idx) ||
1992             link_kind_filtered(dev, kind_ops))
1993                 return true;
1994
1995         return false;
1996 }
1997
1998 /**
1999  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2000  * @sk: netlink socket
2001  * @netnsid: network namespace identifier
2002  *
2003  * Returns the network namespace identified by netnsid on success or an error
2004  * pointer on failure.
2005  */
2006 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2007 {
2008         struct net *net;
2009
2010         net = get_net_ns_by_id(sock_net(sk), netnsid);
2011         if (!net)
2012                 return ERR_PTR(-EINVAL);
2013
2014         /* For now, the caller is required to have CAP_NET_ADMIN in
2015          * the user namespace owning the target net ns.
2016          */
2017         if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2018                 put_net(net);
2019                 return ERR_PTR(-EACCES);
2020         }
2021         return net;
2022 }
2023 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2024
2025 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2026                                       bool strict_check, struct nlattr **tb,
2027                                       struct netlink_ext_ack *extack)
2028 {
2029         int hdrlen;
2030
2031         if (strict_check) {
2032                 struct ifinfomsg *ifm;
2033
2034                 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2035                         NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2036                         return -EINVAL;
2037                 }
2038
2039                 ifm = nlmsg_data(nlh);
2040                 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2041                     ifm->ifi_change) {
2042                         NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2043                         return -EINVAL;
2044                 }
2045                 if (ifm->ifi_index) {
2046                         NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2047                         return -EINVAL;
2048                 }
2049
2050                 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2051                                                      IFLA_MAX, ifla_policy,
2052                                                      extack);
2053         }
2054
2055         /* A hack to preserve kernel<->userspace interface.
2056          * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2057          * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2058          * what iproute2 < v3.9.0 used.
2059          * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2060          * attribute, its netlink message is shorter than struct ifinfomsg.
2061          */
2062         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2063                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2064
2065         return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2066                                       extack);
2067 }
2068
2069 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2070 {
2071         struct netlink_ext_ack *extack = cb->extack;
2072         const struct nlmsghdr *nlh = cb->nlh;
2073         struct net *net = sock_net(skb->sk);
2074         struct net *tgt_net = net;
2075         int h, s_h;
2076         int idx = 0, s_idx;
2077         struct net_device *dev;
2078         struct hlist_head *head;
2079         struct nlattr *tb[IFLA_MAX+1];
2080         u32 ext_filter_mask = 0;
2081         const struct rtnl_link_ops *kind_ops = NULL;
2082         unsigned int flags = NLM_F_MULTI;
2083         int master_idx = 0;
2084         int netnsid = -1;
2085         int err, i;
2086
2087         s_h = cb->args[0];
2088         s_idx = cb->args[1];
2089
2090         err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2091         if (err < 0) {
2092                 if (cb->strict_check)
2093                         return err;
2094
2095                 goto walk_entries;
2096         }
2097
2098         for (i = 0; i <= IFLA_MAX; ++i) {
2099                 if (!tb[i])
2100                         continue;
2101
2102                 /* new attributes should only be added with strict checking */
2103                 switch (i) {
2104                 case IFLA_TARGET_NETNSID:
2105                         netnsid = nla_get_s32(tb[i]);
2106                         tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2107                         if (IS_ERR(tgt_net)) {
2108                                 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2109                                 return PTR_ERR(tgt_net);
2110                         }
2111                         break;
2112                 case IFLA_EXT_MASK:
2113                         ext_filter_mask = nla_get_u32(tb[i]);
2114                         break;
2115                 case IFLA_MASTER:
2116                         master_idx = nla_get_u32(tb[i]);
2117                         break;
2118                 case IFLA_LINKINFO:
2119                         kind_ops = linkinfo_to_kind_ops(tb[i]);
2120                         break;
2121                 default:
2122                         if (cb->strict_check) {
2123                                 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2124                                 return -EINVAL;
2125                         }
2126                 }
2127         }
2128
2129         if (master_idx || kind_ops)
2130                 flags |= NLM_F_DUMP_FILTERED;
2131
2132 walk_entries:
2133         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2134                 idx = 0;
2135                 head = &tgt_net->dev_index_head[h];
2136                 hlist_for_each_entry(dev, head, index_hlist) {
2137                         if (link_dump_filtered(dev, master_idx, kind_ops))
2138                                 goto cont;
2139                         if (idx < s_idx)
2140                                 goto cont;
2141                         err = rtnl_fill_ifinfo(skb, dev, net,
2142                                                RTM_NEWLINK,
2143                                                NETLINK_CB(cb->skb).portid,
2144                                                nlh->nlmsg_seq, 0, flags,
2145                                                ext_filter_mask, 0, NULL, 0,
2146                                                netnsid, GFP_KERNEL);
2147
2148                         if (err < 0) {
2149                                 if (likely(skb->len))
2150                                         goto out;
2151
2152                                 goto out_err;
2153                         }
2154 cont:
2155                         idx++;
2156                 }
2157         }
2158 out:
2159         err = skb->len;
2160 out_err:
2161         cb->args[1] = idx;
2162         cb->args[0] = h;
2163         cb->seq = tgt_net->dev_base_seq;
2164         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2165         if (netnsid >= 0)
2166                 put_net(tgt_net);
2167
2168         return err;
2169 }
2170
2171 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2172                         struct netlink_ext_ack *exterr)
2173 {
2174         return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2175                                     exterr);
2176 }
2177 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2178
2179 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2180 {
2181         struct net *net;
2182         /* Examine the link attributes and figure out which
2183          * network namespace we are talking about.
2184          */
2185         if (tb[IFLA_NET_NS_PID])
2186                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2187         else if (tb[IFLA_NET_NS_FD])
2188                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2189         else
2190                 net = get_net(src_net);
2191         return net;
2192 }
2193 EXPORT_SYMBOL(rtnl_link_get_net);
2194
2195 /* Figure out which network namespace we are talking about by
2196  * examining the link attributes in the following order:
2197  *
2198  * 1. IFLA_NET_NS_PID
2199  * 2. IFLA_NET_NS_FD
2200  * 3. IFLA_TARGET_NETNSID
2201  */
2202 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2203                                                struct nlattr *tb[])
2204 {
2205         struct net *net;
2206
2207         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2208                 return rtnl_link_get_net(src_net, tb);
2209
2210         if (!tb[IFLA_TARGET_NETNSID])
2211                 return get_net(src_net);
2212
2213         net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2214         if (!net)
2215                 return ERR_PTR(-EINVAL);
2216
2217         return net;
2218 }
2219
2220 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2221                                              struct net *src_net,
2222                                              struct nlattr *tb[], int cap)
2223 {
2224         struct net *net;
2225
2226         net = rtnl_link_get_net_by_nlattr(src_net, tb);
2227         if (IS_ERR(net))
2228                 return net;
2229
2230         if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2231                 put_net(net);
2232                 return ERR_PTR(-EPERM);
2233         }
2234
2235         return net;
2236 }
2237
2238 /* Verify that rtnetlink requests do not pass additional properties
2239  * potentially referring to different network namespaces.
2240  */
2241 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2242                                     struct netlink_ext_ack *extack,
2243                                     bool netns_id_only)
2244 {
2245
2246         if (netns_id_only) {
2247                 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2248                         return 0;
2249
2250                 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2251                 return -EOPNOTSUPP;
2252         }
2253
2254         if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2255                 goto invalid_attr;
2256
2257         if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2258                 goto invalid_attr;
2259
2260         if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2261                 goto invalid_attr;
2262
2263         return 0;
2264
2265 invalid_attr:
2266         NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2267         return -EINVAL;
2268 }
2269
2270 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2271                             struct netlink_ext_ack *extack)
2272 {
2273         if (dev) {
2274                 if (tb[IFLA_ADDRESS] &&
2275                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2276                         return -EINVAL;
2277
2278                 if (tb[IFLA_BROADCAST] &&
2279                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2280                         return -EINVAL;
2281         }
2282
2283         if (tb[IFLA_AF_SPEC]) {
2284                 struct nlattr *af;
2285                 int rem, err;
2286
2287                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2288                         const struct rtnl_af_ops *af_ops;
2289
2290                         af_ops = rtnl_af_lookup(nla_type(af));
2291                         if (!af_ops)
2292                                 return -EAFNOSUPPORT;
2293
2294                         if (!af_ops->set_link_af)
2295                                 return -EOPNOTSUPP;
2296
2297                         if (af_ops->validate_link_af) {
2298                                 err = af_ops->validate_link_af(dev, af, extack);
2299                                 if (err < 0)
2300                                         return err;
2301                         }
2302                 }
2303         }
2304
2305         if (tb[IFLA_GRO_MAX_SIZE]) {
2306                 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2307
2308                 if (gro_max_size > GRO_MAX_SIZE) {
2309                         NL_SET_ERR_MSG(extack, "too big gro_max_size");
2310                         return -EINVAL;
2311                 }
2312         }
2313         return 0;
2314 }
2315
2316 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2317                                   int guid_type)
2318 {
2319         const struct net_device_ops *ops = dev->netdev_ops;
2320
2321         return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2322 }
2323
2324 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2325 {
2326         if (dev->type != ARPHRD_INFINIBAND)
2327                 return -EOPNOTSUPP;
2328
2329         return handle_infiniband_guid(dev, ivt, guid_type);
2330 }
2331
2332 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2333 {
2334         const struct net_device_ops *ops = dev->netdev_ops;
2335         int err = -EINVAL;
2336
2337         if (tb[IFLA_VF_MAC]) {
2338                 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2339
2340                 if (ivm->vf >= INT_MAX)
2341                         return -EINVAL;
2342                 err = -EOPNOTSUPP;
2343                 if (ops->ndo_set_vf_mac)
2344                         err = ops->ndo_set_vf_mac(dev, ivm->vf,
2345                                                   ivm->mac);
2346                 if (err < 0)
2347                         return err;
2348         }
2349
2350         if (tb[IFLA_VF_VLAN]) {
2351                 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2352
2353                 if (ivv->vf >= INT_MAX)
2354                         return -EINVAL;
2355                 err = -EOPNOTSUPP;
2356                 if (ops->ndo_set_vf_vlan)
2357                         err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2358                                                    ivv->qos,
2359                                                    htons(ETH_P_8021Q));
2360                 if (err < 0)
2361                         return err;
2362         }
2363
2364         if (tb[IFLA_VF_VLAN_LIST]) {
2365                 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2366                 struct nlattr *attr;
2367                 int rem, len = 0;
2368
2369                 err = -EOPNOTSUPP;
2370                 if (!ops->ndo_set_vf_vlan)
2371                         return err;
2372
2373                 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2374                         if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2375                             nla_len(attr) < NLA_HDRLEN) {
2376                                 return -EINVAL;
2377                         }
2378                         if (len >= MAX_VLAN_LIST_LEN)
2379                                 return -EOPNOTSUPP;
2380                         ivvl[len] = nla_data(attr);
2381
2382                         len++;
2383                 }
2384                 if (len == 0)
2385                         return -EINVAL;
2386
2387                 if (ivvl[0]->vf >= INT_MAX)
2388                         return -EINVAL;
2389                 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2390                                            ivvl[0]->qos, ivvl[0]->vlan_proto);
2391                 if (err < 0)
2392                         return err;
2393         }
2394
2395         if (tb[IFLA_VF_TX_RATE]) {
2396                 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2397                 struct ifla_vf_info ivf;
2398
2399                 if (ivt->vf >= INT_MAX)
2400                         return -EINVAL;
2401                 err = -EOPNOTSUPP;
2402                 if (ops->ndo_get_vf_config)
2403                         err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2404                 if (err < 0)
2405                         return err;
2406
2407                 err = -EOPNOTSUPP;
2408                 if (ops->ndo_set_vf_rate)
2409                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
2410                                                    ivf.min_tx_rate,
2411                                                    ivt->rate);
2412                 if (err < 0)
2413                         return err;
2414         }
2415
2416         if (tb[IFLA_VF_RATE]) {
2417                 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2418
2419                 if (ivt->vf >= INT_MAX)
2420                         return -EINVAL;
2421                 err = -EOPNOTSUPP;
2422                 if (ops->ndo_set_vf_rate)
2423                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
2424                                                    ivt->min_tx_rate,
2425                                                    ivt->max_tx_rate);
2426                 if (err < 0)
2427                         return err;
2428         }
2429
2430         if (tb[IFLA_VF_SPOOFCHK]) {
2431                 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2432
2433                 if (ivs->vf >= INT_MAX)
2434                         return -EINVAL;
2435                 err = -EOPNOTSUPP;
2436                 if (ops->ndo_set_vf_spoofchk)
2437                         err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2438                                                        ivs->setting);
2439                 if (err < 0)
2440                         return err;
2441         }
2442
2443         if (tb[IFLA_VF_LINK_STATE]) {
2444                 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2445
2446                 if (ivl->vf >= INT_MAX)
2447                         return -EINVAL;
2448                 err = -EOPNOTSUPP;
2449                 if (ops->ndo_set_vf_link_state)
2450                         err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2451                                                          ivl->link_state);
2452                 if (err < 0)
2453                         return err;
2454         }
2455
2456         if (tb[IFLA_VF_RSS_QUERY_EN]) {
2457                 struct ifla_vf_rss_query_en *ivrssq_en;
2458
2459                 err = -EOPNOTSUPP;
2460                 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2461                 if (ivrssq_en->vf >= INT_MAX)
2462                         return -EINVAL;
2463                 if (ops->ndo_set_vf_rss_query_en)
2464                         err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2465                                                            ivrssq_en->setting);
2466                 if (err < 0)
2467                         return err;
2468         }
2469
2470         if (tb[IFLA_VF_TRUST]) {
2471                 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2472
2473                 if (ivt->vf >= INT_MAX)
2474                         return -EINVAL;
2475                 err = -EOPNOTSUPP;
2476                 if (ops->ndo_set_vf_trust)
2477                         err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2478                 if (err < 0)
2479                         return err;
2480         }
2481
2482         if (tb[IFLA_VF_IB_NODE_GUID]) {
2483                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2484
2485                 if (ivt->vf >= INT_MAX)
2486                         return -EINVAL;
2487                 if (!ops->ndo_set_vf_guid)
2488                         return -EOPNOTSUPP;
2489                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2490         }
2491
2492         if (tb[IFLA_VF_IB_PORT_GUID]) {
2493                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2494
2495                 if (ivt->vf >= INT_MAX)
2496                         return -EINVAL;
2497                 if (!ops->ndo_set_vf_guid)
2498                         return -EOPNOTSUPP;
2499
2500                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2501         }
2502
2503         return err;
2504 }
2505
2506 static int do_set_master(struct net_device *dev, int ifindex,
2507                          struct netlink_ext_ack *extack)
2508 {
2509         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2510         const struct net_device_ops *ops;
2511         int err;
2512
2513         if (upper_dev) {
2514                 if (upper_dev->ifindex == ifindex)
2515                         return 0;
2516                 ops = upper_dev->netdev_ops;
2517                 if (ops->ndo_del_slave) {
2518                         err = ops->ndo_del_slave(upper_dev, dev);
2519                         if (err)
2520                                 return err;
2521                 } else {
2522                         return -EOPNOTSUPP;
2523                 }
2524         }
2525
2526         if (ifindex) {
2527                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2528                 if (!upper_dev)
2529                         return -EINVAL;
2530                 ops = upper_dev->netdev_ops;
2531                 if (ops->ndo_add_slave) {
2532                         err = ops->ndo_add_slave(upper_dev, dev, extack);
2533                         if (err)
2534                                 return err;
2535                 } else {
2536                         return -EOPNOTSUPP;
2537                 }
2538         }
2539         return 0;
2540 }
2541
2542 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2543         [IFLA_PROTO_DOWN_REASON_MASK]   = { .type = NLA_U32 },
2544         [IFLA_PROTO_DOWN_REASON_VALUE]  = { .type = NLA_U32 },
2545 };
2546
2547 static int do_set_proto_down(struct net_device *dev,
2548                              struct nlattr *nl_proto_down,
2549                              struct nlattr *nl_proto_down_reason,
2550                              struct netlink_ext_ack *extack)
2551 {
2552         struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2553         unsigned long mask = 0;
2554         u32 value;
2555         bool proto_down;
2556         int err;
2557
2558         if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2559                 NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
2560                 return -EOPNOTSUPP;
2561         }
2562
2563         if (nl_proto_down_reason) {
2564                 err = nla_parse_nested_deprecated(pdreason,
2565                                                   IFLA_PROTO_DOWN_REASON_MAX,
2566                                                   nl_proto_down_reason,
2567                                                   ifla_proto_down_reason_policy,
2568                                                   NULL);
2569                 if (err < 0)
2570                         return err;
2571
2572                 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2573                         NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2574                         return -EINVAL;
2575                 }
2576
2577                 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2578
2579                 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2580                         mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2581
2582                 dev_change_proto_down_reason(dev, mask, value);
2583         }
2584
2585         if (nl_proto_down) {
2586                 proto_down = nla_get_u8(nl_proto_down);
2587
2588                 /* Don't turn off protodown if there are active reasons */
2589                 if (!proto_down && dev->proto_down_reason) {
2590                         NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2591                         return -EBUSY;
2592                 }
2593                 err = dev_change_proto_down(dev,
2594                                             proto_down);
2595                 if (err)
2596                         return err;
2597         }
2598
2599         return 0;
2600 }
2601
2602 #define DO_SETLINK_MODIFIED     0x01
2603 /* notify flag means notify + modified. */
2604 #define DO_SETLINK_NOTIFY       0x03
2605 static int do_setlink(const struct sk_buff *skb,
2606                       struct net_device *dev, struct ifinfomsg *ifm,
2607                       struct netlink_ext_ack *extack,
2608                       struct nlattr **tb, char *ifname, int status)
2609 {
2610         const struct net_device_ops *ops = dev->netdev_ops;
2611         int err;
2612
2613         err = validate_linkmsg(dev, tb, extack);
2614         if (err < 0)
2615                 return err;
2616
2617         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2618                 const char *pat = ifname && ifname[0] ? ifname : NULL;
2619                 struct net *net;
2620                 int new_ifindex;
2621
2622                 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2623                                                 tb, CAP_NET_ADMIN);
2624                 if (IS_ERR(net)) {
2625                         err = PTR_ERR(net);
2626                         goto errout;
2627                 }
2628
2629                 if (tb[IFLA_NEW_IFINDEX])
2630                         new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2631                 else
2632                         new_ifindex = 0;
2633
2634                 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2635                 put_net(net);
2636                 if (err)
2637                         goto errout;
2638                 status |= DO_SETLINK_MODIFIED;
2639         }
2640
2641         if (tb[IFLA_MAP]) {
2642                 struct rtnl_link_ifmap *u_map;
2643                 struct ifmap k_map;
2644
2645                 if (!ops->ndo_set_config) {
2646                         err = -EOPNOTSUPP;
2647                         goto errout;
2648                 }
2649
2650                 if (!netif_device_present(dev)) {
2651                         err = -ENODEV;
2652                         goto errout;
2653                 }
2654
2655                 u_map = nla_data(tb[IFLA_MAP]);
2656                 k_map.mem_start = (unsigned long) u_map->mem_start;
2657                 k_map.mem_end = (unsigned long) u_map->mem_end;
2658                 k_map.base_addr = (unsigned short) u_map->base_addr;
2659                 k_map.irq = (unsigned char) u_map->irq;
2660                 k_map.dma = (unsigned char) u_map->dma;
2661                 k_map.port = (unsigned char) u_map->port;
2662
2663                 err = ops->ndo_set_config(dev, &k_map);
2664                 if (err < 0)
2665                         goto errout;
2666
2667                 status |= DO_SETLINK_NOTIFY;
2668         }
2669
2670         if (tb[IFLA_ADDRESS]) {
2671                 struct sockaddr *sa;
2672                 int len;
2673
2674                 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2675                                                   sizeof(*sa));
2676                 sa = kmalloc(len, GFP_KERNEL);
2677                 if (!sa) {
2678                         err = -ENOMEM;
2679                         goto errout;
2680                 }
2681                 sa->sa_family = dev->type;
2682                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2683                        dev->addr_len);
2684                 err = dev_set_mac_address_user(dev, sa, extack);
2685                 kfree(sa);
2686                 if (err)
2687                         goto errout;
2688                 status |= DO_SETLINK_MODIFIED;
2689         }
2690
2691         if (tb[IFLA_MTU]) {
2692                 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2693                 if (err < 0)
2694                         goto errout;
2695                 status |= DO_SETLINK_MODIFIED;
2696         }
2697
2698         if (tb[IFLA_GROUP]) {
2699                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2700                 status |= DO_SETLINK_NOTIFY;
2701         }
2702
2703         /*
2704          * Interface selected by interface index but interface
2705          * name provided implies that a name change has been
2706          * requested.
2707          */
2708         if (ifm->ifi_index > 0 && ifname[0]) {
2709                 err = dev_change_name(dev, ifname);
2710                 if (err < 0)
2711                         goto errout;
2712                 status |= DO_SETLINK_MODIFIED;
2713         }
2714
2715         if (tb[IFLA_IFALIAS]) {
2716                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2717                                     nla_len(tb[IFLA_IFALIAS]));
2718                 if (err < 0)
2719                         goto errout;
2720                 status |= DO_SETLINK_NOTIFY;
2721         }
2722
2723         if (tb[IFLA_BROADCAST]) {
2724                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2725                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2726         }
2727
2728         if (ifm->ifi_flags || ifm->ifi_change) {
2729                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2730                                        extack);
2731                 if (err < 0)
2732                         goto errout;
2733         }
2734
2735         if (tb[IFLA_MASTER]) {
2736                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2737                 if (err)
2738                         goto errout;
2739                 status |= DO_SETLINK_MODIFIED;
2740         }
2741
2742         if (tb[IFLA_CARRIER]) {
2743                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2744                 if (err)
2745                         goto errout;
2746                 status |= DO_SETLINK_MODIFIED;
2747         }
2748
2749         if (tb[IFLA_TXQLEN]) {
2750                 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2751
2752                 err = dev_change_tx_queue_len(dev, value);
2753                 if (err)
2754                         goto errout;
2755                 status |= DO_SETLINK_MODIFIED;
2756         }
2757
2758         if (tb[IFLA_GSO_MAX_SIZE]) {
2759                 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2760
2761                 if (max_size > GSO_MAX_SIZE) {
2762                         err = -EINVAL;
2763                         goto errout;
2764                 }
2765
2766                 if (dev->gso_max_size ^ max_size) {
2767                         netif_set_gso_max_size(dev, max_size);
2768                         status |= DO_SETLINK_MODIFIED;
2769                 }
2770         }
2771
2772         if (tb[IFLA_GSO_MAX_SEGS]) {
2773                 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2774
2775                 if (max_segs > GSO_MAX_SEGS) {
2776                         err = -EINVAL;
2777                         goto errout;
2778                 }
2779
2780                 if (dev->gso_max_segs ^ max_segs) {
2781                         netif_set_gso_max_segs(dev, max_segs);
2782                         status |= DO_SETLINK_MODIFIED;
2783                 }
2784         }
2785
2786         if (tb[IFLA_GRO_MAX_SIZE]) {
2787                 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2788
2789                 if (dev->gro_max_size ^ gro_max_size) {
2790                         netif_set_gro_max_size(dev, gro_max_size);
2791                         status |= DO_SETLINK_MODIFIED;
2792                 }
2793         }
2794
2795         if (tb[IFLA_OPERSTATE])
2796                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2797
2798         if (tb[IFLA_LINKMODE]) {
2799                 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2800
2801                 write_lock(&dev_base_lock);
2802                 if (dev->link_mode ^ value)
2803                         status |= DO_SETLINK_NOTIFY;
2804                 dev->link_mode = value;
2805                 write_unlock(&dev_base_lock);
2806         }
2807
2808         if (tb[IFLA_VFINFO_LIST]) {
2809                 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2810                 struct nlattr *attr;
2811                 int rem;
2812
2813                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2814                         if (nla_type(attr) != IFLA_VF_INFO ||
2815                             nla_len(attr) < NLA_HDRLEN) {
2816                                 err = -EINVAL;
2817                                 goto errout;
2818                         }
2819                         err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2820                                                           attr,
2821                                                           ifla_vf_policy,
2822                                                           NULL);
2823                         if (err < 0)
2824                                 goto errout;
2825                         err = do_setvfinfo(dev, vfinfo);
2826                         if (err < 0)
2827                                 goto errout;
2828                         status |= DO_SETLINK_NOTIFY;
2829                 }
2830         }
2831         err = 0;
2832
2833         if (tb[IFLA_VF_PORTS]) {
2834                 struct nlattr *port[IFLA_PORT_MAX+1];
2835                 struct nlattr *attr;
2836                 int vf;
2837                 int rem;
2838
2839                 err = -EOPNOTSUPP;
2840                 if (!ops->ndo_set_vf_port)
2841                         goto errout;
2842
2843                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2844                         if (nla_type(attr) != IFLA_VF_PORT ||
2845                             nla_len(attr) < NLA_HDRLEN) {
2846                                 err = -EINVAL;
2847                                 goto errout;
2848                         }
2849                         err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2850                                                           attr,
2851                                                           ifla_port_policy,
2852                                                           NULL);
2853                         if (err < 0)
2854                                 goto errout;
2855                         if (!port[IFLA_PORT_VF]) {
2856                                 err = -EOPNOTSUPP;
2857                                 goto errout;
2858                         }
2859                         vf = nla_get_u32(port[IFLA_PORT_VF]);
2860                         err = ops->ndo_set_vf_port(dev, vf, port);
2861                         if (err < 0)
2862                                 goto errout;
2863                         status |= DO_SETLINK_NOTIFY;
2864                 }
2865         }
2866         err = 0;
2867
2868         if (tb[IFLA_PORT_SELF]) {
2869                 struct nlattr *port[IFLA_PORT_MAX+1];
2870
2871                 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2872                                                   tb[IFLA_PORT_SELF],
2873                                                   ifla_port_policy, NULL);
2874                 if (err < 0)
2875                         goto errout;
2876
2877                 err = -EOPNOTSUPP;
2878                 if (ops->ndo_set_vf_port)
2879                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2880                 if (err < 0)
2881                         goto errout;
2882                 status |= DO_SETLINK_NOTIFY;
2883         }
2884
2885         if (tb[IFLA_AF_SPEC]) {
2886                 struct nlattr *af;
2887                 int rem;
2888
2889                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2890                         const struct rtnl_af_ops *af_ops;
2891
2892                         BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2893
2894                         err = af_ops->set_link_af(dev, af, extack);
2895                         if (err < 0)
2896                                 goto errout;
2897
2898                         status |= DO_SETLINK_NOTIFY;
2899                 }
2900         }
2901         err = 0;
2902
2903         if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2904                 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2905                                         tb[IFLA_PROTO_DOWN_REASON], extack);
2906                 if (err)
2907                         goto errout;
2908                 status |= DO_SETLINK_NOTIFY;
2909         }
2910
2911         if (tb[IFLA_XDP]) {
2912                 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2913                 u32 xdp_flags = 0;
2914
2915                 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2916                                                   tb[IFLA_XDP],
2917                                                   ifla_xdp_policy, NULL);
2918                 if (err < 0)
2919                         goto errout;
2920
2921                 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2922                         err = -EINVAL;
2923                         goto errout;
2924                 }
2925
2926                 if (xdp[IFLA_XDP_FLAGS]) {
2927                         xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2928                         if (xdp_flags & ~XDP_FLAGS_MASK) {
2929                                 err = -EINVAL;
2930                                 goto errout;
2931                         }
2932                         if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2933                                 err = -EINVAL;
2934                                 goto errout;
2935                         }
2936                 }
2937
2938                 if (xdp[IFLA_XDP_FD]) {
2939                         int expected_fd = -1;
2940
2941                         if (xdp_flags & XDP_FLAGS_REPLACE) {
2942                                 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2943                                         err = -EINVAL;
2944                                         goto errout;
2945                                 }
2946                                 expected_fd =
2947                                         nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2948                         }
2949
2950                         err = dev_change_xdp_fd(dev, extack,
2951                                                 nla_get_s32(xdp[IFLA_XDP_FD]),
2952                                                 expected_fd,
2953                                                 xdp_flags);
2954                         if (err)
2955                                 goto errout;
2956                         status |= DO_SETLINK_NOTIFY;
2957                 }
2958         }
2959
2960 errout:
2961         if (status & DO_SETLINK_MODIFIED) {
2962                 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2963                         netdev_state_change(dev);
2964
2965                 if (err < 0)
2966                         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",
2967                                              dev->name);
2968         }
2969
2970         return err;
2971 }
2972
2973 static struct net_device *rtnl_dev_get(struct net *net,
2974                                        struct nlattr *ifname_attr,
2975                                        struct nlattr *altifname_attr,
2976                                        char *ifname)
2977 {
2978         char buffer[ALTIFNAMSIZ];
2979
2980         if (!ifname) {
2981                 ifname = buffer;
2982                 if (ifname_attr)
2983                         nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
2984                 else if (altifname_attr)
2985                         nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
2986                 else
2987                         return NULL;
2988         }
2989
2990         return __dev_get_by_name(net, ifname);
2991 }
2992
2993 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2994                         struct netlink_ext_ack *extack)
2995 {
2996         struct net *net = sock_net(skb->sk);
2997         struct ifinfomsg *ifm;
2998         struct net_device *dev;
2999         int err;
3000         struct nlattr *tb[IFLA_MAX+1];
3001         char ifname[IFNAMSIZ];
3002
3003         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3004                                      ifla_policy, extack);
3005         if (err < 0)
3006                 goto errout;
3007
3008         err = rtnl_ensure_unique_netns(tb, extack, false);
3009         if (err < 0)
3010                 goto errout;
3011
3012         if (tb[IFLA_IFNAME])
3013                 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3014         else
3015                 ifname[0] = '\0';
3016
3017         err = -EINVAL;
3018         ifm = nlmsg_data(nlh);
3019         if (ifm->ifi_index > 0)
3020                 dev = __dev_get_by_index(net, ifm->ifi_index);
3021         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3022                 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3023         else
3024                 goto errout;
3025
3026         if (dev == NULL) {
3027                 err = -ENODEV;
3028                 goto errout;
3029         }
3030
3031         err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
3032 errout:
3033         return err;
3034 }
3035
3036 static int rtnl_group_dellink(const struct net *net, int group)
3037 {
3038         struct net_device *dev, *aux;
3039         LIST_HEAD(list_kill);
3040         bool found = false;
3041
3042         if (!group)
3043                 return -EPERM;
3044
3045         for_each_netdev(net, dev) {
3046                 if (dev->group == group) {
3047                         const struct rtnl_link_ops *ops;
3048
3049                         found = true;
3050                         ops = dev->rtnl_link_ops;
3051                         if (!ops || !ops->dellink)
3052                                 return -EOPNOTSUPP;
3053                 }
3054         }
3055
3056         if (!found)
3057                 return -ENODEV;
3058
3059         for_each_netdev_safe(net, dev, aux) {
3060                 if (dev->group == group) {
3061                         const struct rtnl_link_ops *ops;
3062
3063                         ops = dev->rtnl_link_ops;
3064                         ops->dellink(dev, &list_kill);
3065                 }
3066         }
3067         unregister_netdevice_many(&list_kill);
3068
3069         return 0;
3070 }
3071
3072 int rtnl_delete_link(struct net_device *dev)
3073 {
3074         const struct rtnl_link_ops *ops;
3075         LIST_HEAD(list_kill);
3076
3077         ops = dev->rtnl_link_ops;
3078         if (!ops || !ops->dellink)
3079                 return -EOPNOTSUPP;
3080
3081         ops->dellink(dev, &list_kill);
3082         unregister_netdevice_many(&list_kill);
3083
3084         return 0;
3085 }
3086 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3087
3088 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3089                         struct netlink_ext_ack *extack)
3090 {
3091         struct net *net = sock_net(skb->sk);
3092         struct net *tgt_net = net;
3093         struct net_device *dev = NULL;
3094         struct ifinfomsg *ifm;
3095         struct nlattr *tb[IFLA_MAX+1];
3096         int err;
3097         int netnsid = -1;
3098
3099         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3100                                      ifla_policy, extack);
3101         if (err < 0)
3102                 return err;
3103
3104         err = rtnl_ensure_unique_netns(tb, extack, true);
3105         if (err < 0)
3106                 return err;
3107
3108         if (tb[IFLA_TARGET_NETNSID]) {
3109                 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3110                 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3111                 if (IS_ERR(tgt_net))
3112                         return PTR_ERR(tgt_net);
3113         }
3114
3115         err = -EINVAL;
3116         ifm = nlmsg_data(nlh);
3117         if (ifm->ifi_index > 0)
3118                 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3119         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3120                 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3121                                    tb[IFLA_ALT_IFNAME], NULL);
3122         else if (tb[IFLA_GROUP])
3123                 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3124         else
3125                 goto out;
3126
3127         if (!dev) {
3128                 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3129                         err = -ENODEV;
3130
3131                 goto out;
3132         }
3133
3134         err = rtnl_delete_link(dev);
3135
3136 out:
3137         if (netnsid >= 0)
3138                 put_net(tgt_net);
3139
3140         return err;
3141 }
3142
3143 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3144 {
3145         unsigned int old_flags;
3146         int err;
3147
3148         old_flags = dev->flags;
3149         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3150                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3151                                          NULL);
3152                 if (err < 0)
3153                         return err;
3154         }
3155
3156         if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3157                 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3158         } else {
3159                 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3160                 __dev_notify_flags(dev, old_flags, ~0U);
3161         }
3162         return 0;
3163 }
3164 EXPORT_SYMBOL(rtnl_configure_link);
3165
3166 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3167                                     unsigned char name_assign_type,
3168                                     const struct rtnl_link_ops *ops,
3169                                     struct nlattr *tb[],
3170                                     struct netlink_ext_ack *extack)
3171 {
3172         struct net_device *dev;
3173         unsigned int num_tx_queues = 1;
3174         unsigned int num_rx_queues = 1;
3175
3176         if (tb[IFLA_NUM_TX_QUEUES])
3177                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3178         else if (ops->get_num_tx_queues)
3179                 num_tx_queues = ops->get_num_tx_queues();
3180
3181         if (tb[IFLA_NUM_RX_QUEUES])
3182                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3183         else if (ops->get_num_rx_queues)
3184                 num_rx_queues = ops->get_num_rx_queues();
3185
3186         if (num_tx_queues < 1 || num_tx_queues > 4096) {
3187                 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3188                 return ERR_PTR(-EINVAL);
3189         }
3190
3191         if (num_rx_queues < 1 || num_rx_queues > 4096) {
3192                 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3193                 return ERR_PTR(-EINVAL);
3194         }
3195
3196         if (ops->alloc) {
3197                 dev = ops->alloc(tb, ifname, name_assign_type,
3198                                  num_tx_queues, num_rx_queues);
3199                 if (IS_ERR(dev))
3200                         return dev;
3201         } else {
3202                 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3203                                        name_assign_type, ops->setup,
3204                                        num_tx_queues, num_rx_queues);
3205         }
3206
3207         if (!dev)
3208                 return ERR_PTR(-ENOMEM);
3209
3210         dev_net_set(dev, net);
3211         dev->rtnl_link_ops = ops;
3212         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3213
3214         if (tb[IFLA_MTU]) {
3215                 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3216                 int err;
3217
3218                 err = dev_validate_mtu(dev, mtu, extack);
3219                 if (err) {
3220                         free_netdev(dev);
3221                         return ERR_PTR(err);
3222                 }
3223                 dev->mtu = mtu;
3224         }
3225         if (tb[IFLA_ADDRESS]) {
3226                 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3227                                nla_len(tb[IFLA_ADDRESS]));
3228                 dev->addr_assign_type = NET_ADDR_SET;
3229         }
3230         if (tb[IFLA_BROADCAST])
3231                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3232                                 nla_len(tb[IFLA_BROADCAST]));
3233         if (tb[IFLA_TXQLEN])
3234                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3235         if (tb[IFLA_OPERSTATE])
3236                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3237         if (tb[IFLA_LINKMODE])
3238                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3239         if (tb[IFLA_GROUP])
3240                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3241         if (tb[IFLA_GSO_MAX_SIZE])
3242                 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3243         if (tb[IFLA_GSO_MAX_SEGS])
3244                 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3245         if (tb[IFLA_GRO_MAX_SIZE])
3246                 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3247
3248         return dev;
3249 }
3250 EXPORT_SYMBOL(rtnl_create_link);
3251
3252 static int rtnl_group_changelink(const struct sk_buff *skb,
3253                 struct net *net, int group,
3254                 struct ifinfomsg *ifm,
3255                 struct netlink_ext_ack *extack,
3256                 struct nlattr **tb)
3257 {
3258         struct net_device *dev, *aux;
3259         int err;
3260
3261         for_each_netdev_safe(net, dev, aux) {
3262                 if (dev->group == group) {
3263                         err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3264                         if (err < 0)
3265                                 return err;
3266                 }
3267         }
3268
3269         return 0;
3270 }
3271
3272 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3273                           struct nlattr **attr, struct netlink_ext_ack *extack)
3274 {
3275         struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3276         unsigned char name_assign_type = NET_NAME_USER;
3277         struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3278         const struct rtnl_link_ops *m_ops;
3279         struct net_device *master_dev;
3280         struct net *net = sock_net(skb->sk);
3281         const struct rtnl_link_ops *ops;
3282         struct nlattr *tb[IFLA_MAX + 1];
3283         struct net *dest_net, *link_net;
3284         struct nlattr **slave_data;
3285         char kind[MODULE_NAME_LEN];
3286         struct net_device *dev;
3287         struct ifinfomsg *ifm;
3288         char ifname[IFNAMSIZ];
3289         struct nlattr **data;
3290         int err;
3291
3292 #ifdef CONFIG_MODULES
3293 replay:
3294 #endif
3295         err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3296                                      ifla_policy, extack);
3297         if (err < 0)
3298                 return err;
3299
3300         err = rtnl_ensure_unique_netns(tb, extack, false);
3301         if (err < 0)
3302                 return err;
3303
3304         if (tb[IFLA_IFNAME])
3305                 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3306         else
3307                 ifname[0] = '\0';
3308
3309         ifm = nlmsg_data(nlh);
3310         if (ifm->ifi_index > 0)
3311                 dev = __dev_get_by_index(net, ifm->ifi_index);
3312         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3313                 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3314         else
3315                 dev = NULL;
3316
3317         master_dev = NULL;
3318         m_ops = NULL;
3319         if (dev) {
3320                 master_dev = netdev_master_upper_dev_get(dev);
3321                 if (master_dev)
3322                         m_ops = master_dev->rtnl_link_ops;
3323         }
3324
3325         err = validate_linkmsg(dev, tb, extack);
3326         if (err < 0)
3327                 return err;
3328
3329         if (tb[IFLA_LINKINFO]) {
3330                 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3331                                                   tb[IFLA_LINKINFO],
3332                                                   ifla_info_policy, NULL);
3333                 if (err < 0)
3334                         return err;
3335         } else
3336                 memset(linkinfo, 0, sizeof(linkinfo));
3337
3338         if (linkinfo[IFLA_INFO_KIND]) {
3339                 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3340                 ops = rtnl_link_ops_get(kind);
3341         } else {
3342                 kind[0] = '\0';
3343                 ops = NULL;
3344         }
3345
3346         data = NULL;
3347         if (ops) {
3348                 if (ops->maxtype > RTNL_MAX_TYPE)
3349                         return -EINVAL;
3350
3351                 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3352                         err = nla_parse_nested_deprecated(attr, ops->maxtype,
3353                                                           linkinfo[IFLA_INFO_DATA],
3354                                                           ops->policy, extack);
3355                         if (err < 0)
3356                                 return err;
3357                         data = attr;
3358                 }
3359                 if (ops->validate) {
3360                         err = ops->validate(tb, data, extack);
3361                         if (err < 0)
3362                                 return err;
3363                 }
3364         }
3365
3366         slave_data = NULL;
3367         if (m_ops) {
3368                 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3369                         return -EINVAL;
3370
3371                 if (m_ops->slave_maxtype &&
3372                     linkinfo[IFLA_INFO_SLAVE_DATA]) {
3373                         err = nla_parse_nested_deprecated(slave_attr,
3374                                                           m_ops->slave_maxtype,
3375                                                           linkinfo[IFLA_INFO_SLAVE_DATA],
3376                                                           m_ops->slave_policy,
3377                                                           extack);
3378                         if (err < 0)
3379                                 return err;
3380                         slave_data = slave_attr;
3381                 }
3382         }
3383
3384         if (dev) {
3385                 int status = 0;
3386
3387                 if (nlh->nlmsg_flags & NLM_F_EXCL)
3388                         return -EEXIST;
3389                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3390                         return -EOPNOTSUPP;
3391
3392                 if (linkinfo[IFLA_INFO_DATA]) {
3393                         if (!ops || ops != dev->rtnl_link_ops ||
3394                             !ops->changelink)
3395                                 return -EOPNOTSUPP;
3396
3397                         err = ops->changelink(dev, tb, data, extack);
3398                         if (err < 0)
3399                                 return err;
3400                         status |= DO_SETLINK_NOTIFY;
3401                 }
3402
3403                 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3404                         if (!m_ops || !m_ops->slave_changelink)
3405                                 return -EOPNOTSUPP;
3406
3407                         err = m_ops->slave_changelink(master_dev, dev, tb,
3408                                                       slave_data, extack);
3409                         if (err < 0)
3410                                 return err;
3411                         status |= DO_SETLINK_NOTIFY;
3412                 }
3413
3414                 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3415         }
3416
3417         if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3418                 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3419                         return rtnl_group_changelink(skb, net,
3420                                                 nla_get_u32(tb[IFLA_GROUP]),
3421                                                 ifm, extack, tb);
3422                 return -ENODEV;
3423         }
3424
3425         if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3426                 return -EOPNOTSUPP;
3427
3428         if (!ops) {
3429 #ifdef CONFIG_MODULES
3430                 if (kind[0]) {
3431                         __rtnl_unlock();
3432                         request_module("rtnl-link-%s", kind);
3433                         rtnl_lock();
3434                         ops = rtnl_link_ops_get(kind);
3435                         if (ops)
3436                                 goto replay;
3437                 }
3438 #endif
3439                 NL_SET_ERR_MSG(extack, "Unknown device type");
3440                 return -EOPNOTSUPP;
3441         }
3442
3443         if (!ops->alloc && !ops->setup)
3444                 return -EOPNOTSUPP;
3445
3446         if (!ifname[0]) {
3447                 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3448                 name_assign_type = NET_NAME_ENUM;
3449         }
3450
3451         dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3452         if (IS_ERR(dest_net))
3453                 return PTR_ERR(dest_net);
3454
3455         if (tb[IFLA_LINK_NETNSID]) {
3456                 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3457
3458                 link_net = get_net_ns_by_id(dest_net, id);
3459                 if (!link_net) {
3460                         NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3461                         err =  -EINVAL;
3462                         goto out;
3463                 }
3464                 err = -EPERM;
3465                 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3466                         goto out;
3467         } else {
3468                 link_net = NULL;
3469         }
3470
3471         dev = rtnl_create_link(link_net ? : dest_net, ifname,
3472                                name_assign_type, ops, tb, extack);
3473         if (IS_ERR(dev)) {
3474                 err = PTR_ERR(dev);
3475                 goto out;
3476         }
3477
3478         dev->ifindex = ifm->ifi_index;
3479
3480         if (ops->newlink)
3481                 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3482         else
3483                 err = register_netdevice(dev);
3484         if (err < 0) {
3485                 free_netdev(dev);
3486                 goto out;
3487         }
3488
3489         err = rtnl_configure_link(dev, ifm);
3490         if (err < 0)
3491                 goto out_unregister;
3492         if (link_net) {
3493                 err = dev_change_net_namespace(dev, dest_net, ifname);
3494                 if (err < 0)
3495                         goto out_unregister;
3496         }
3497         if (tb[IFLA_MASTER]) {
3498                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3499                 if (err)
3500                         goto out_unregister;
3501         }
3502 out:
3503         if (link_net)
3504                 put_net(link_net);
3505         put_net(dest_net);
3506         return err;
3507 out_unregister:
3508         if (ops->newlink) {
3509                 LIST_HEAD(list_kill);
3510
3511                 ops->dellink(dev, &list_kill);
3512                 unregister_netdevice_many(&list_kill);
3513         } else {
3514                 unregister_netdevice(dev);
3515         }
3516         goto out;
3517 }
3518
3519 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3520                         struct netlink_ext_ack *extack)
3521 {
3522         struct nlattr **attr;
3523         int ret;
3524
3525         attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3526         if (!attr)
3527                 return -ENOMEM;
3528
3529         ret = __rtnl_newlink(skb, nlh, attr, extack);
3530         kfree(attr);
3531         return ret;
3532 }
3533
3534 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3535                                   const struct nlmsghdr *nlh,
3536                                   struct nlattr **tb,
3537                                   struct netlink_ext_ack *extack)
3538 {
3539         struct ifinfomsg *ifm;
3540         int i, err;
3541
3542         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3543                 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3544                 return -EINVAL;
3545         }
3546
3547         if (!netlink_strict_get_check(skb))
3548                 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3549                                               ifla_policy, extack);
3550
3551         ifm = nlmsg_data(nlh);
3552         if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3553             ifm->ifi_change) {
3554                 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3555                 return -EINVAL;
3556         }
3557
3558         err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3559                                             ifla_policy, extack);
3560         if (err)
3561                 return err;
3562
3563         for (i = 0; i <= IFLA_MAX; i++) {
3564                 if (!tb[i])
3565                         continue;
3566
3567                 switch (i) {
3568                 case IFLA_IFNAME:
3569                 case IFLA_ALT_IFNAME:
3570                 case IFLA_EXT_MASK:
3571                 case IFLA_TARGET_NETNSID:
3572                         break;
3573                 default:
3574                         NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3575                         return -EINVAL;
3576                 }
3577         }
3578
3579         return 0;
3580 }
3581
3582 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3583                         struct netlink_ext_ack *extack)
3584 {
3585         struct net *net = sock_net(skb->sk);
3586         struct net *tgt_net = net;
3587         struct ifinfomsg *ifm;
3588         struct nlattr *tb[IFLA_MAX+1];
3589         struct net_device *dev = NULL;
3590         struct sk_buff *nskb;
3591         int netnsid = -1;
3592         int err;
3593         u32 ext_filter_mask = 0;
3594
3595         err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3596         if (err < 0)
3597                 return err;
3598
3599         err = rtnl_ensure_unique_netns(tb, extack, true);
3600         if (err < 0)
3601                 return err;
3602
3603         if (tb[IFLA_TARGET_NETNSID]) {
3604                 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3605                 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3606                 if (IS_ERR(tgt_net))
3607                         return PTR_ERR(tgt_net);
3608         }
3609
3610         if (tb[IFLA_EXT_MASK])
3611                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3612
3613         err = -EINVAL;
3614         ifm = nlmsg_data(nlh);
3615         if (ifm->ifi_index > 0)
3616                 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3617         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3618                 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3619                                    tb[IFLA_ALT_IFNAME], NULL);
3620         else
3621                 goto out;
3622
3623         err = -ENODEV;
3624         if (dev == NULL)
3625                 goto out;
3626
3627         err = -ENOBUFS;
3628         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3629         if (nskb == NULL)
3630                 goto out;
3631
3632         err = rtnl_fill_ifinfo(nskb, dev, net,
3633                                RTM_NEWLINK, NETLINK_CB(skb).portid,
3634                                nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3635                                0, NULL, 0, netnsid, GFP_KERNEL);
3636         if (err < 0) {
3637                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3638                 WARN_ON(err == -EMSGSIZE);
3639                 kfree_skb(nskb);
3640         } else
3641                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3642 out:
3643         if (netnsid >= 0)
3644                 put_net(tgt_net);
3645
3646         return err;
3647 }
3648
3649 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3650                            bool *changed, struct netlink_ext_ack *extack)
3651 {
3652         char *alt_ifname;
3653         int err;
3654
3655         err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3656         if (err)
3657                 return err;
3658
3659         alt_ifname = nla_strdup(attr, GFP_KERNEL);
3660         if (!alt_ifname)
3661                 return -ENOMEM;
3662
3663         if (cmd == RTM_NEWLINKPROP) {
3664                 err = netdev_name_node_alt_create(dev, alt_ifname);
3665                 if (!err)
3666                         alt_ifname = NULL;
3667         } else if (cmd == RTM_DELLINKPROP) {
3668                 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3669         } else {
3670                 WARN_ON_ONCE(1);
3671                 err = -EINVAL;
3672         }
3673
3674         kfree(alt_ifname);
3675         if (!err)
3676                 *changed = true;
3677         return err;
3678 }
3679
3680 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3681                          struct netlink_ext_ack *extack)
3682 {
3683         struct net *net = sock_net(skb->sk);
3684         struct nlattr *tb[IFLA_MAX + 1];
3685         struct net_device *dev;
3686         struct ifinfomsg *ifm;
3687         bool changed = false;
3688         struct nlattr *attr;
3689         int err, rem;
3690
3691         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3692         if (err)
3693                 return err;
3694
3695         err = rtnl_ensure_unique_netns(tb, extack, true);
3696         if (err)
3697                 return err;
3698
3699         ifm = nlmsg_data(nlh);
3700         if (ifm->ifi_index > 0)
3701                 dev = __dev_get_by_index(net, ifm->ifi_index);
3702         else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3703                 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3704                                    tb[IFLA_ALT_IFNAME], NULL);
3705         else
3706                 return -EINVAL;
3707
3708         if (!dev)
3709                 return -ENODEV;
3710
3711         if (!tb[IFLA_PROP_LIST])
3712                 return 0;
3713
3714         nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3715                 switch (nla_type(attr)) {
3716                 case IFLA_ALT_IFNAME:
3717                         err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3718                         if (err)
3719                                 return err;
3720                         break;
3721                 }
3722         }
3723
3724         if (changed)
3725                 netdev_state_change(dev);
3726         return 0;
3727 }
3728
3729 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3730                             struct netlink_ext_ack *extack)
3731 {
3732         return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3733 }
3734
3735 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3736                             struct netlink_ext_ack *extack)
3737 {
3738         return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3739 }
3740
3741 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3742 {
3743         struct net *net = sock_net(skb->sk);
3744         size_t min_ifinfo_dump_size = 0;
3745         struct nlattr *tb[IFLA_MAX+1];
3746         u32 ext_filter_mask = 0;
3747         struct net_device *dev;
3748         int hdrlen;
3749
3750         /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3751         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3752                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3753
3754         if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3755                 if (tb[IFLA_EXT_MASK])
3756                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3757         }
3758
3759         if (!ext_filter_mask)
3760                 return NLMSG_GOODSIZE;
3761         /*
3762          * traverse the list of net devices and compute the minimum
3763          * buffer size based upon the filter mask.
3764          */
3765         rcu_read_lock();
3766         for_each_netdev_rcu(net, dev) {
3767                 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3768                                            if_nlmsg_size(dev, ext_filter_mask));
3769         }
3770         rcu_read_unlock();
3771
3772         return nlmsg_total_size(min_ifinfo_dump_size);
3773 }
3774
3775 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3776 {
3777         int idx;
3778         int s_idx = cb->family;
3779         int type = cb->nlh->nlmsg_type - RTM_BASE;
3780         int ret = 0;
3781
3782         if (s_idx == 0)
3783                 s_idx = 1;
3784
3785         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3786                 struct rtnl_link __rcu **tab;
3787                 struct rtnl_link *link;
3788                 rtnl_dumpit_func dumpit;
3789
3790                 if (idx < s_idx || idx == PF_PACKET)
3791                         continue;
3792
3793                 if (type < 0 || type >= RTM_NR_MSGTYPES)
3794                         continue;
3795
3796                 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3797                 if (!tab)
3798                         continue;
3799
3800                 link = rcu_dereference_rtnl(tab[type]);
3801                 if (!link)
3802                         continue;
3803
3804                 dumpit = link->dumpit;
3805                 if (!dumpit)
3806                         continue;
3807
3808                 if (idx > s_idx) {
3809                         memset(&cb->args[0], 0, sizeof(cb->args));
3810                         cb->prev_seq = 0;
3811                         cb->seq = 0;
3812                 }
3813                 ret = dumpit(skb, cb);
3814                 if (ret)
3815                         break;
3816         }
3817         cb->family = idx;
3818
3819         return skb->len ? : ret;
3820 }
3821
3822 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3823                                        unsigned int change,
3824                                        u32 event, gfp_t flags, int *new_nsid,
3825                                        int new_ifindex)
3826 {
3827         struct net *net = dev_net(dev);
3828         struct sk_buff *skb;
3829         int err = -ENOBUFS;
3830
3831         skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
3832         if (skb == NULL)
3833                 goto errout;
3834
3835         err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3836                                type, 0, 0, change, 0, 0, event,
3837                                new_nsid, new_ifindex, -1, flags);
3838         if (err < 0) {
3839                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3840                 WARN_ON(err == -EMSGSIZE);
3841                 kfree_skb(skb);
3842                 goto errout;
3843         }
3844         return skb;
3845 errout:
3846         if (err < 0)
3847                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3848         return NULL;
3849 }
3850
3851 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3852 {
3853         struct net *net = dev_net(dev);
3854
3855         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3856 }
3857
3858 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3859                                unsigned int change, u32 event,
3860                                gfp_t flags, int *new_nsid, int new_ifindex)
3861 {
3862         struct sk_buff *skb;
3863
3864         if (dev->reg_state != NETREG_REGISTERED)
3865                 return;
3866
3867         skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3868                                      new_ifindex);
3869         if (skb)
3870                 rtmsg_ifinfo_send(skb, dev, flags);
3871 }
3872
3873 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3874                   gfp_t flags)
3875 {
3876         rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3877                            NULL, 0);
3878 }
3879
3880 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3881                          gfp_t flags, int *new_nsid, int new_ifindex)
3882 {
3883         rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3884                            new_nsid, new_ifindex);
3885 }
3886
3887 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3888                                    struct net_device *dev,
3889                                    u8 *addr, u16 vid, u32 pid, u32 seq,
3890                                    int type, unsigned int flags,
3891                                    int nlflags, u16 ndm_state)
3892 {
3893         struct nlmsghdr *nlh;
3894         struct ndmsg *ndm;
3895
3896         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3897         if (!nlh)
3898                 return -EMSGSIZE;
3899
3900         ndm = nlmsg_data(nlh);
3901         ndm->ndm_family  = AF_BRIDGE;
3902         ndm->ndm_pad1    = 0;
3903         ndm->ndm_pad2    = 0;
3904         ndm->ndm_flags   = flags;
3905         ndm->ndm_type    = 0;
3906         ndm->ndm_ifindex = dev->ifindex;
3907         ndm->ndm_state   = ndm_state;
3908
3909         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3910                 goto nla_put_failure;
3911         if (vid)
3912                 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3913                         goto nla_put_failure;
3914
3915         nlmsg_end(skb, nlh);
3916         return 0;
3917
3918 nla_put_failure:
3919         nlmsg_cancel(skb, nlh);
3920         return -EMSGSIZE;
3921 }
3922
3923 static inline size_t rtnl_fdb_nlmsg_size(void)
3924 {
3925         return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3926                nla_total_size(ETH_ALEN) +       /* NDA_LLADDR */
3927                nla_total_size(sizeof(u16)) +    /* NDA_VLAN */
3928                0;
3929 }
3930
3931 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3932                             u16 ndm_state)
3933 {
3934         struct net *net = dev_net(dev);
3935         struct sk_buff *skb;
3936         int err = -ENOBUFS;
3937
3938         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3939         if (!skb)
3940                 goto errout;
3941
3942         err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3943                                       0, 0, type, NTF_SELF, 0, ndm_state);
3944         if (err < 0) {
3945                 kfree_skb(skb);
3946                 goto errout;
3947         }
3948
3949         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3950         return;
3951 errout:
3952         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3953 }
3954
3955 /*
3956  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3957  */
3958 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3959                      struct nlattr *tb[],
3960                      struct net_device *dev,
3961                      const unsigned char *addr, u16 vid,
3962                      u16 flags)
3963 {
3964         int err = -EINVAL;
3965
3966         /* If aging addresses are supported device will need to
3967          * implement its own handler for this.
3968          */
3969         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3970                 netdev_info(dev, "default FDB implementation only supports local addresses\n");
3971                 return err;
3972         }
3973
3974         if (vid) {
3975                 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
3976                 return err;
3977         }
3978
3979         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3980                 err = dev_uc_add_excl(dev, addr);
3981         else if (is_multicast_ether_addr(addr))
3982                 err = dev_mc_add_excl(dev, addr);
3983
3984         /* Only return duplicate errors if NLM_F_EXCL is set */
3985         if (err == -EEXIST && !(flags & NLM_F_EXCL))
3986                 err = 0;
3987
3988         return err;
3989 }
3990 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3991
3992 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3993                          struct netlink_ext_ack *extack)
3994 {
3995         u16 vid = 0;
3996
3997         if (vlan_attr) {
3998                 if (nla_len(vlan_attr) != sizeof(u16)) {
3999                         NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4000                         return -EINVAL;
4001                 }
4002
4003                 vid = nla_get_u16(vlan_attr);
4004
4005                 if (!vid || vid >= VLAN_VID_MASK) {
4006                         NL_SET_ERR_MSG(extack, "invalid vlan id");
4007                         return -EINVAL;
4008                 }
4009         }
4010         *p_vid = vid;
4011         return 0;
4012 }
4013
4014 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4015                         struct netlink_ext_ack *extack)
4016 {
4017         struct net *net = sock_net(skb->sk);
4018         struct ndmsg *ndm;
4019         struct nlattr *tb[NDA_MAX+1];
4020         struct net_device *dev;
4021         u8 *addr;
4022         u16 vid;
4023         int err;
4024
4025         err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4026                                      extack);
4027         if (err < 0)
4028                 return err;
4029
4030         ndm = nlmsg_data(nlh);
4031         if (ndm->ndm_ifindex == 0) {
4032                 NL_SET_ERR_MSG(extack, "invalid ifindex");
4033                 return -EINVAL;
4034         }
4035
4036         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4037         if (dev == NULL) {
4038                 NL_SET_ERR_MSG(extack, "unknown ifindex");
4039                 return -ENODEV;
4040         }
4041
4042         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4043                 NL_SET_ERR_MSG(extack, "invalid address");
4044                 return -EINVAL;
4045         }
4046
4047         if (dev->type != ARPHRD_ETHER) {
4048                 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4049                 return -EINVAL;
4050         }
4051
4052         addr = nla_data(tb[NDA_LLADDR]);
4053
4054         err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4055         if (err)
4056                 return err;
4057
4058         err = -EOPNOTSUPP;
4059
4060         /* Support fdb on master device the net/bridge default case */
4061         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4062             netif_is_bridge_port(dev)) {
4063                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4064                 const struct net_device_ops *ops = br_dev->netdev_ops;
4065
4066                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4067                                        nlh->nlmsg_flags, extack);
4068                 if (err)
4069                         goto out;
4070                 else
4071                         ndm->ndm_flags &= ~NTF_MASTER;
4072         }
4073
4074         /* Embedded bridge, macvlan, and any other device support */
4075         if ((ndm->ndm_flags & NTF_SELF)) {
4076                 if (dev->netdev_ops->ndo_fdb_add)
4077                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4078                                                            vid,
4079                                                            nlh->nlmsg_flags,
4080                                                            extack);
4081                 else
4082                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4083                                                nlh->nlmsg_flags);
4084
4085                 if (!err) {
4086                         rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4087                                         ndm->ndm_state);
4088                         ndm->ndm_flags &= ~NTF_SELF;
4089                 }
4090         }
4091 out:
4092         return err;
4093 }
4094
4095 /*
4096  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4097  */
4098 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4099                      struct nlattr *tb[],
4100                      struct net_device *dev,
4101                      const unsigned char *addr, u16 vid)
4102 {
4103         int err = -EINVAL;
4104
4105         /* If aging addresses are supported device will need to
4106          * implement its own handler for this.
4107          */
4108         if (!(ndm->ndm_state & NUD_PERMANENT)) {
4109                 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4110                 return err;
4111         }
4112
4113         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4114                 err = dev_uc_del(dev, addr);
4115         else if (is_multicast_ether_addr(addr))
4116                 err = dev_mc_del(dev, addr);
4117
4118         return err;
4119 }
4120 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4121
4122 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4123                         struct netlink_ext_ack *extack)
4124 {
4125         struct net *net = sock_net(skb->sk);
4126         struct ndmsg *ndm;
4127         struct nlattr *tb[NDA_MAX+1];
4128         struct net_device *dev;
4129         __u8 *addr;
4130         int err;
4131         u16 vid;
4132
4133         if (!netlink_capable(skb, CAP_NET_ADMIN))
4134                 return -EPERM;
4135
4136         err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4137                                      extack);
4138         if (err < 0)
4139                 return err;
4140
4141         ndm = nlmsg_data(nlh);
4142         if (ndm->ndm_ifindex == 0) {
4143                 NL_SET_ERR_MSG(extack, "invalid ifindex");
4144                 return -EINVAL;
4145         }
4146
4147         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4148         if (dev == NULL) {
4149                 NL_SET_ERR_MSG(extack, "unknown ifindex");
4150                 return -ENODEV;
4151         }
4152
4153         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4154                 NL_SET_ERR_MSG(extack, "invalid address");
4155                 return -EINVAL;
4156         }
4157
4158         if (dev->type != ARPHRD_ETHER) {
4159                 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4160                 return -EINVAL;
4161         }
4162
4163         addr = nla_data(tb[NDA_LLADDR]);
4164
4165         err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4166         if (err)
4167                 return err;
4168
4169         err = -EOPNOTSUPP;
4170
4171         /* Support fdb on master device the net/bridge default case */
4172         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4173             netif_is_bridge_port(dev)) {
4174                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4175                 const struct net_device_ops *ops = br_dev->netdev_ops;
4176
4177                 if (ops->ndo_fdb_del)
4178                         err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4179
4180                 if (err)
4181                         goto out;
4182                 else
4183                         ndm->ndm_flags &= ~NTF_MASTER;
4184         }
4185
4186         /* Embedded bridge, macvlan, and any other device support */
4187         if (ndm->ndm_flags & NTF_SELF) {
4188                 if (dev->netdev_ops->ndo_fdb_del)
4189                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4190                                                            vid);
4191                 else
4192                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4193
4194                 if (!err) {
4195                         rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4196                                         ndm->ndm_state);
4197                         ndm->ndm_flags &= ~NTF_SELF;
4198                 }
4199         }
4200 out:
4201         return err;
4202 }
4203
4204 static int nlmsg_populate_fdb(struct sk_buff *skb,
4205                               struct netlink_callback *cb,
4206                               struct net_device *dev,
4207                               int *idx,
4208                               struct netdev_hw_addr_list *list)
4209 {
4210         struct netdev_hw_addr *ha;
4211         int err;
4212         u32 portid, seq;
4213
4214         portid = NETLINK_CB(cb->skb).portid;
4215         seq = cb->nlh->nlmsg_seq;
4216
4217         list_for_each_entry(ha, &list->list, list) {
4218                 if (*idx < cb->args[2])
4219                         goto skip;
4220
4221                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4222                                               portid, seq,
4223                                               RTM_NEWNEIGH, NTF_SELF,
4224                                               NLM_F_MULTI, NUD_PERMANENT);
4225                 if (err < 0)
4226                         return err;
4227 skip:
4228                 *idx += 1;
4229         }
4230         return 0;
4231 }
4232
4233 /**
4234  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4235  * @skb: socket buffer to store message in
4236  * @cb: netlink callback
4237  * @dev: netdevice
4238  * @filter_dev: ignored
4239  * @idx: the number of FDB table entries dumped is added to *@idx
4240  *
4241  * Default netdevice operation to dump the existing unicast address list.
4242  * Returns number of addresses from list put in skb.
4243  */
4244 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4245                       struct netlink_callback *cb,
4246                       struct net_device *dev,
4247                       struct net_device *filter_dev,
4248                       int *idx)
4249 {
4250         int err;
4251
4252         if (dev->type != ARPHRD_ETHER)
4253                 return -EINVAL;
4254
4255         netif_addr_lock_bh(dev);
4256         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4257         if (err)
4258                 goto out;
4259         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4260 out:
4261         netif_addr_unlock_bh(dev);
4262         return err;
4263 }
4264 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4265
4266 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4267                                  int *br_idx, int *brport_idx,
4268                                  struct netlink_ext_ack *extack)
4269 {
4270         struct nlattr *tb[NDA_MAX + 1];
4271         struct ndmsg *ndm;
4272         int err, i;
4273
4274         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4275                 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4276                 return -EINVAL;
4277         }
4278
4279         ndm = nlmsg_data(nlh);
4280         if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
4281             ndm->ndm_flags || ndm->ndm_type) {
4282                 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4283                 return -EINVAL;
4284         }
4285
4286         err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4287                                             NDA_MAX, NULL, extack);
4288         if (err < 0)
4289                 return err;
4290
4291         *brport_idx = ndm->ndm_ifindex;
4292         for (i = 0; i <= NDA_MAX; ++i) {
4293                 if (!tb[i])
4294                         continue;
4295
4296                 switch (i) {
4297                 case NDA_IFINDEX:
4298                         if (nla_len(tb[i]) != sizeof(u32)) {
4299                                 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4300                                 return -EINVAL;
4301                         }
4302                         *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4303                         break;
4304                 case NDA_MASTER:
4305                         if (nla_len(tb[i]) != sizeof(u32)) {
4306                                 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4307                                 return -EINVAL;
4308                         }
4309                         *br_idx = nla_get_u32(tb[NDA_MASTER]);
4310                         break;
4311                 default:
4312                         NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4313                         return -EINVAL;
4314                 }
4315         }
4316
4317         return 0;
4318 }
4319
4320 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4321                                  int *br_idx, int *brport_idx,
4322                                  struct netlink_ext_ack *extack)
4323 {
4324         struct nlattr *tb[IFLA_MAX+1];
4325         int err;
4326
4327         /* A hack to preserve kernel<->userspace interface.
4328          * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4329          * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4330          * So, check for ndmsg with an optional u32 attribute (not used here).
4331          * Fortunately these sizes don't conflict with the size of ifinfomsg
4332          * with an optional attribute.
4333          */
4334         if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4335             (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4336              nla_attr_size(sizeof(u32)))) {
4337                 struct ifinfomsg *ifm;
4338
4339                 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4340                                              tb, IFLA_MAX, ifla_policy,
4341                                              extack);
4342                 if (err < 0) {
4343                         return -EINVAL;
4344                 } else if (err == 0) {
4345                         if (tb[IFLA_MASTER])
4346                                 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4347                 }
4348
4349                 ifm = nlmsg_data(nlh);
4350                 *brport_idx = ifm->ifi_index;
4351         }
4352         return 0;
4353 }
4354
4355 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4356 {
4357         struct net_device *dev;
4358         struct net_device *br_dev = NULL;
4359         const struct net_device_ops *ops = NULL;
4360         const struct net_device_ops *cops = NULL;
4361         struct net *net = sock_net(skb->sk);
4362         struct hlist_head *head;
4363         int brport_idx = 0;
4364         int br_idx = 0;
4365         int h, s_h;
4366         int idx = 0, s_idx;
4367         int err = 0;
4368         int fidx = 0;
4369
4370         if (cb->strict_check)
4371                 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4372                                             cb->extack);
4373         else
4374                 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4375                                             cb->extack);
4376         if (err < 0)
4377                 return err;
4378
4379         if (br_idx) {
4380                 br_dev = __dev_get_by_index(net, br_idx);
4381                 if (!br_dev)
4382                         return -ENODEV;
4383
4384                 ops = br_dev->netdev_ops;
4385         }
4386
4387         s_h = cb->args[0];
4388         s_idx = cb->args[1];
4389
4390         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4391                 idx = 0;
4392                 head = &net->dev_index_head[h];
4393                 hlist_for_each_entry(dev, head, index_hlist) {
4394
4395                         if (brport_idx && (dev->ifindex != brport_idx))
4396                                 continue;
4397
4398                         if (!br_idx) { /* user did not specify a specific bridge */
4399                                 if (netif_is_bridge_port(dev)) {
4400                                         br_dev = netdev_master_upper_dev_get(dev);
4401                                         cops = br_dev->netdev_ops;
4402                                 }
4403                         } else {
4404                                 if (dev != br_dev &&
4405                                     !netif_is_bridge_port(dev))
4406                                         continue;
4407
4408                                 if (br_dev != netdev_master_upper_dev_get(dev) &&
4409                                     !netif_is_bridge_master(dev))
4410                                         continue;
4411                                 cops = ops;
4412                         }
4413
4414                         if (idx < s_idx)
4415                                 goto cont;
4416
4417                         if (netif_is_bridge_port(dev)) {
4418                                 if (cops && cops->ndo_fdb_dump) {
4419                                         err = cops->ndo_fdb_dump(skb, cb,
4420                                                                 br_dev, dev,
4421                                                                 &fidx);
4422                                         if (err == -EMSGSIZE)
4423                                                 goto out;
4424                                 }
4425                         }
4426
4427                         if (dev->netdev_ops->ndo_fdb_dump)
4428                                 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4429                                                                     dev, NULL,
4430                                                                     &fidx);
4431                         else
4432                                 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4433                                                         &fidx);
4434                         if (err == -EMSGSIZE)
4435                                 goto out;
4436
4437                         cops = NULL;
4438
4439                         /* reset fdb offset to 0 for rest of the interfaces */
4440                         cb->args[2] = 0;
4441                         fidx = 0;
4442 cont:
4443                         idx++;
4444                 }
4445         }
4446
4447 out:
4448         cb->args[0] = h;
4449         cb->args[1] = idx;
4450         cb->args[2] = fidx;
4451
4452         return skb->len;
4453 }
4454
4455 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4456                                 struct nlattr **tb, u8 *ndm_flags,
4457                                 int *br_idx, int *brport_idx, u8 **addr,
4458                                 u16 *vid, struct netlink_ext_ack *extack)
4459 {
4460         struct ndmsg *ndm;
4461         int err, i;
4462
4463         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4464                 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4465                 return -EINVAL;
4466         }
4467
4468         ndm = nlmsg_data(nlh);
4469         if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
4470             ndm->ndm_type) {
4471                 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4472                 return -EINVAL;
4473         }
4474
4475         if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4476                 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4477                 return -EINVAL;
4478         }
4479
4480         err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4481                                             NDA_MAX, nda_policy, extack);
4482         if (err < 0)
4483                 return err;
4484
4485         *ndm_flags = ndm->ndm_flags;
4486         *brport_idx = ndm->ndm_ifindex;
4487         for (i = 0; i <= NDA_MAX; ++i) {
4488                 if (!tb[i])
4489                         continue;
4490
4491                 switch (i) {
4492                 case NDA_MASTER:
4493                         *br_idx = nla_get_u32(tb[i]);
4494                         break;
4495                 case NDA_LLADDR:
4496                         if (nla_len(tb[i]) != ETH_ALEN) {
4497                                 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4498                                 return -EINVAL;
4499                         }
4500                         *addr = nla_data(tb[i]);
4501                         break;
4502                 case NDA_VLAN:
4503                         err = fdb_vid_parse(tb[i], vid, extack);
4504                         if (err)
4505                                 return err;
4506                         break;
4507                 case NDA_VNI:
4508                         break;
4509                 default:
4510                         NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4511                         return -EINVAL;
4512                 }
4513         }
4514
4515         return 0;
4516 }
4517
4518 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4519                         struct netlink_ext_ack *extack)
4520 {
4521         struct net_device *dev = NULL, *br_dev = NULL;
4522         const struct net_device_ops *ops = NULL;
4523         struct net *net = sock_net(in_skb->sk);
4524         struct nlattr *tb[NDA_MAX + 1];
4525         struct sk_buff *skb;
4526         int brport_idx = 0;
4527         u8 ndm_flags = 0;
4528         int br_idx = 0;
4529         u8 *addr = NULL;
4530         u16 vid = 0;
4531         int err;
4532
4533         err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4534                                    &brport_idx, &addr, &vid, extack);
4535         if (err < 0)
4536                 return err;
4537
4538         if (!addr) {
4539                 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4540                 return -EINVAL;
4541         }
4542
4543         if (brport_idx) {
4544                 dev = __dev_get_by_index(net, brport_idx);
4545                 if (!dev) {
4546                         NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4547                         return -ENODEV;
4548                 }
4549         }
4550
4551         if (br_idx) {
4552                 if (dev) {
4553                         NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4554                         return -EINVAL;
4555                 }
4556
4557                 br_dev = __dev_get_by_index(net, br_idx);
4558                 if (!br_dev) {
4559                         NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4560                         return -EINVAL;
4561                 }
4562                 ops = br_dev->netdev_ops;
4563         }
4564
4565         if (dev) {
4566                 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4567                         if (!netif_is_bridge_port(dev)) {
4568                                 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4569                                 return -EINVAL;
4570                         }
4571                         br_dev = netdev_master_upper_dev_get(dev);
4572                         if (!br_dev) {
4573                                 NL_SET_ERR_MSG(extack, "Master of device not found");
4574                                 return -EINVAL;
4575                         }
4576                         ops = br_dev->netdev_ops;
4577                 } else {
4578                         if (!(ndm_flags & NTF_SELF)) {
4579                                 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4580                                 return -EINVAL;
4581                         }
4582                         ops = dev->netdev_ops;
4583                 }
4584         }
4585
4586         if (!br_dev && !dev) {
4587                 NL_SET_ERR_MSG(extack, "No device specified");
4588                 return -ENODEV;
4589         }
4590
4591         if (!ops || !ops->ndo_fdb_get) {
4592                 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4593                 return -EOPNOTSUPP;
4594         }
4595
4596         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4597         if (!skb)
4598                 return -ENOBUFS;
4599
4600         if (br_dev)
4601                 dev = br_dev;
4602         err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4603                                NETLINK_CB(in_skb).portid,
4604                                nlh->nlmsg_seq, extack);
4605         if (err)
4606                 goto out;
4607
4608         return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4609 out:
4610         kfree_skb(skb);
4611         return err;
4612 }
4613
4614 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4615                                unsigned int attrnum, unsigned int flag)
4616 {
4617         if (mask & flag)
4618                 return nla_put_u8(skb, attrnum, !!(flags & flag));
4619         return 0;
4620 }
4621
4622 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4623                             struct net_device *dev, u16 mode,
4624                             u32 flags, u32 mask, int nlflags,
4625                             u32 filter_mask,
4626                             int (*vlan_fill)(struct sk_buff *skb,
4627                                              struct net_device *dev,
4628                                              u32 filter_mask))
4629 {
4630         struct nlmsghdr *nlh;
4631         struct ifinfomsg *ifm;
4632         struct nlattr *br_afspec;
4633         struct nlattr *protinfo;
4634         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4635         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4636         int err = 0;
4637
4638         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4639         if (nlh == NULL)
4640                 return -EMSGSIZE;
4641
4642         ifm = nlmsg_data(nlh);
4643         ifm->ifi_family = AF_BRIDGE;
4644         ifm->__ifi_pad = 0;
4645         ifm->ifi_type = dev->type;
4646         ifm->ifi_index = dev->ifindex;
4647         ifm->ifi_flags = dev_get_flags(dev);
4648         ifm->ifi_change = 0;
4649
4650
4651         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4652             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4653             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4654             (br_dev &&
4655              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4656             (dev->addr_len &&
4657              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4658             (dev->ifindex != dev_get_iflink(dev) &&
4659              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4660                 goto nla_put_failure;
4661
4662         br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4663         if (!br_afspec)
4664                 goto nla_put_failure;
4665
4666         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4667                 nla_nest_cancel(skb, br_afspec);
4668                 goto nla_put_failure;
4669         }
4670
4671         if (mode != BRIDGE_MODE_UNDEF) {
4672                 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4673                         nla_nest_cancel(skb, br_afspec);
4674                         goto nla_put_failure;
4675                 }
4676         }
4677         if (vlan_fill) {
4678                 err = vlan_fill(skb, dev, filter_mask);
4679                 if (err) {
4680                         nla_nest_cancel(skb, br_afspec);
4681                         goto nla_put_failure;
4682                 }
4683         }
4684         nla_nest_end(skb, br_afspec);
4685
4686         protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4687         if (!protinfo)
4688                 goto nla_put_failure;
4689
4690         if (brport_nla_put_flag(skb, flags, mask,
4691                                 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4692             brport_nla_put_flag(skb, flags, mask,
4693                                 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4694             brport_nla_put_flag(skb, flags, mask,
4695                                 IFLA_BRPORT_FAST_LEAVE,
4696                                 BR_MULTICAST_FAST_LEAVE) ||
4697             brport_nla_put_flag(skb, flags, mask,
4698                                 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4699             brport_nla_put_flag(skb, flags, mask,
4700                                 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4701             brport_nla_put_flag(skb, flags, mask,
4702                                 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4703             brport_nla_put_flag(skb, flags, mask,
4704                                 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4705             brport_nla_put_flag(skb, flags, mask,
4706                                 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4707             brport_nla_put_flag(skb, flags, mask,
4708                                 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4709             brport_nla_put_flag(skb, flags, mask,
4710                                 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4711                 nla_nest_cancel(skb, protinfo);
4712                 goto nla_put_failure;
4713         }
4714
4715         nla_nest_end(skb, protinfo);
4716
4717         nlmsg_end(skb, nlh);
4718         return 0;
4719 nla_put_failure:
4720         nlmsg_cancel(skb, nlh);
4721         return err ? err : -EMSGSIZE;
4722 }
4723 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4724
4725 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4726                                     bool strict_check, u32 *filter_mask,
4727                                     struct netlink_ext_ack *extack)
4728 {
4729         struct nlattr *tb[IFLA_MAX+1];
4730         int err, i;
4731
4732         if (strict_check) {
4733                 struct ifinfomsg *ifm;
4734
4735                 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4736                         NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4737                         return -EINVAL;
4738                 }
4739
4740                 ifm = nlmsg_data(nlh);
4741                 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4742                     ifm->ifi_change || ifm->ifi_index) {
4743                         NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4744                         return -EINVAL;
4745                 }
4746
4747                 err = nlmsg_parse_deprecated_strict(nlh,
4748                                                     sizeof(struct ifinfomsg),
4749                                                     tb, IFLA_MAX, ifla_policy,
4750                                                     extack);
4751         } else {
4752                 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4753                                              tb, IFLA_MAX, ifla_policy,
4754                                              extack);
4755         }
4756         if (err < 0)
4757                 return err;
4758
4759         /* new attributes should only be added with strict checking */
4760         for (i = 0; i <= IFLA_MAX; ++i) {
4761                 if (!tb[i])
4762                         continue;
4763
4764                 switch (i) {
4765                 case IFLA_EXT_MASK:
4766                         *filter_mask = nla_get_u32(tb[i]);
4767                         break;
4768                 default:
4769                         if (strict_check) {
4770                                 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4771                                 return -EINVAL;
4772                         }
4773                 }
4774         }
4775
4776         return 0;
4777 }
4778
4779 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4780 {
4781         const struct nlmsghdr *nlh = cb->nlh;
4782         struct net *net = sock_net(skb->sk);
4783         struct net_device *dev;
4784         int idx = 0;
4785         u32 portid = NETLINK_CB(cb->skb).portid;
4786         u32 seq = nlh->nlmsg_seq;
4787         u32 filter_mask = 0;
4788         int err;
4789
4790         err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4791                                        cb->extack);
4792         if (err < 0 && cb->strict_check)
4793                 return err;
4794
4795         rcu_read_lock();
4796         for_each_netdev_rcu(net, dev) {
4797                 const struct net_device_ops *ops = dev->netdev_ops;
4798                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4799
4800                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4801                         if (idx >= cb->args[0]) {
4802                                 err = br_dev->netdev_ops->ndo_bridge_getlink(
4803                                                 skb, portid, seq, dev,
4804                                                 filter_mask, NLM_F_MULTI);
4805                                 if (err < 0 && err != -EOPNOTSUPP) {
4806                                         if (likely(skb->len))
4807                                                 break;
4808
4809                                         goto out_err;
4810                                 }
4811                         }
4812                         idx++;
4813                 }
4814
4815                 if (ops->ndo_bridge_getlink) {
4816                         if (idx >= cb->args[0]) {
4817                                 err = ops->ndo_bridge_getlink(skb, portid,
4818                                                               seq, dev,
4819                                                               filter_mask,
4820                                                               NLM_F_MULTI);
4821                                 if (err < 0 && err != -EOPNOTSUPP) {
4822                                         if (likely(skb->len))
4823                                                 break;
4824
4825                                         goto out_err;
4826                                 }
4827                         }
4828                         idx++;
4829                 }
4830         }
4831         err = skb->len;
4832 out_err:
4833         rcu_read_unlock();
4834         cb->args[0] = idx;
4835
4836         return err;
4837 }
4838
4839 static inline size_t bridge_nlmsg_size(void)
4840 {
4841         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4842                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
4843                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
4844                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
4845                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
4846                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
4847                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
4848                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
4849                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4850                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
4851                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
4852 }
4853
4854 static int rtnl_bridge_notify(struct net_device *dev)
4855 {
4856         struct net *net = dev_net(dev);
4857         struct sk_buff *skb;
4858         int err = -EOPNOTSUPP;
4859
4860         if (!dev->netdev_ops->ndo_bridge_getlink)
4861                 return 0;
4862
4863         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4864         if (!skb) {
4865                 err = -ENOMEM;
4866                 goto errout;
4867         }
4868
4869         err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4870         if (err < 0)
4871                 goto errout;
4872
4873         /* Notification info is only filled for bridge ports, not the bridge
4874          * device itself. Therefore, a zero notification length is valid and
4875          * should not result in an error.
4876          */
4877         if (!skb->len)
4878                 goto errout;
4879
4880         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4881         return 0;
4882 errout:
4883         WARN_ON(err == -EMSGSIZE);
4884         kfree_skb(skb);
4885         if (err)
4886                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4887         return err;
4888 }
4889
4890 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4891                                struct netlink_ext_ack *extack)
4892 {
4893         struct net *net = sock_net(skb->sk);
4894         struct ifinfomsg *ifm;
4895         struct net_device *dev;
4896         struct nlattr *br_spec, *attr = NULL;
4897         int rem, err = -EOPNOTSUPP;
4898         u16 flags = 0;
4899         bool have_flags = false;
4900
4901         if (nlmsg_len(nlh) < sizeof(*ifm))
4902                 return -EINVAL;
4903
4904         ifm = nlmsg_data(nlh);
4905         if (ifm->ifi_family != AF_BRIDGE)
4906                 return -EPFNOSUPPORT;
4907
4908         dev = __dev_get_by_index(net, ifm->ifi_index);
4909         if (!dev) {
4910                 NL_SET_ERR_MSG(extack, "unknown ifindex");
4911                 return -ENODEV;
4912         }
4913
4914         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4915         if (br_spec) {
4916                 nla_for_each_nested(attr, br_spec, rem) {
4917                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4918                                 if (nla_len(attr) < sizeof(flags))
4919                                         return -EINVAL;
4920
4921                                 have_flags = true;
4922                                 flags = nla_get_u16(attr);
4923                                 break;
4924                         }
4925                 }
4926         }
4927
4928         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4929                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4930
4931                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4932                         err = -EOPNOTSUPP;
4933                         goto out;
4934                 }
4935
4936                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4937                                                              extack);
4938                 if (err)
4939                         goto out;
4940
4941                 flags &= ~BRIDGE_FLAGS_MASTER;
4942         }
4943
4944         if ((flags & BRIDGE_FLAGS_SELF)) {
4945                 if (!dev->netdev_ops->ndo_bridge_setlink)
4946                         err = -EOPNOTSUPP;
4947                 else
4948                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4949                                                                   flags,
4950                                                                   extack);
4951                 if (!err) {
4952                         flags &= ~BRIDGE_FLAGS_SELF;
4953
4954                         /* Generate event to notify upper layer of bridge
4955                          * change
4956                          */
4957                         err = rtnl_bridge_notify(dev);
4958                 }
4959         }
4960
4961         if (have_flags)
4962                 memcpy(nla_data(attr), &flags, sizeof(flags));
4963 out:
4964         return err;
4965 }
4966
4967 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4968                                struct netlink_ext_ack *extack)
4969 {
4970         struct net *net = sock_net(skb->sk);
4971         struct ifinfomsg *ifm;
4972         struct net_device *dev;
4973         struct nlattr *br_spec, *attr = NULL;
4974         int rem, err = -EOPNOTSUPP;
4975         u16 flags = 0;
4976         bool have_flags = false;
4977
4978         if (nlmsg_len(nlh) < sizeof(*ifm))
4979                 return -EINVAL;
4980
4981         ifm = nlmsg_data(nlh);
4982         if (ifm->ifi_family != AF_BRIDGE)
4983                 return -EPFNOSUPPORT;
4984
4985         dev = __dev_get_by_index(net, ifm->ifi_index);
4986         if (!dev) {
4987                 NL_SET_ERR_MSG(extack, "unknown ifindex");
4988                 return -ENODEV;
4989         }
4990
4991         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4992         if (br_spec) {
4993                 nla_for_each_nested(attr, br_spec, rem) {
4994                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4995                                 if (nla_len(attr) < sizeof(flags))
4996                                         return -EINVAL;
4997
4998                                 have_flags = true;
4999                                 flags = nla_get_u16(attr);
5000                                 break;
5001                         }
5002                 }
5003         }
5004
5005         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5006                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5007
5008                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5009                         err = -EOPNOTSUPP;
5010                         goto out;
5011                 }
5012
5013                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5014                 if (err)
5015                         goto out;
5016
5017                 flags &= ~BRIDGE_FLAGS_MASTER;
5018         }
5019
5020         if ((flags & BRIDGE_FLAGS_SELF)) {
5021                 if (!dev->netdev_ops->ndo_bridge_dellink)
5022                         err = -EOPNOTSUPP;
5023                 else
5024                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5025                                                                   flags);
5026
5027                 if (!err) {
5028                         flags &= ~BRIDGE_FLAGS_SELF;
5029
5030                         /* Generate event to notify upper layer of bridge
5031                          * change
5032                          */
5033                         err = rtnl_bridge_notify(dev);
5034                 }
5035         }
5036
5037         if (have_flags)
5038                 memcpy(nla_data(attr), &flags, sizeof(flags));
5039 out:
5040         return err;
5041 }
5042
5043 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5044 {
5045         return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5046                (!idxattr || idxattr == attrid);
5047 }
5048
5049 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5050 static int rtnl_get_offload_stats_attr_size(int attr_id)
5051 {
5052         switch (attr_id) {
5053         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5054                 return sizeof(struct rtnl_link_stats64);
5055         }
5056
5057         return 0;
5058 }
5059
5060 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5061                                   int *prividx)
5062 {
5063         struct nlattr *attr = NULL;
5064         int attr_id, size;
5065         void *attr_data;
5066         int err;
5067
5068         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5069               dev->netdev_ops->ndo_get_offload_stats))
5070                 return -ENODATA;
5071
5072         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5073              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5074                 if (attr_id < *prividx)
5075                         continue;
5076
5077                 size = rtnl_get_offload_stats_attr_size(attr_id);
5078                 if (!size)
5079                         continue;
5080
5081                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5082                         continue;
5083
5084                 attr = nla_reserve_64bit(skb, attr_id, size,
5085                                          IFLA_OFFLOAD_XSTATS_UNSPEC);
5086                 if (!attr)
5087                         goto nla_put_failure;
5088
5089                 attr_data = nla_data(attr);
5090                 memset(attr_data, 0, size);
5091                 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5092                                                              attr_data);
5093                 if (err)
5094                         goto get_offload_stats_failure;
5095         }
5096
5097         if (!attr)
5098                 return -ENODATA;
5099
5100         *prividx = 0;
5101         return 0;
5102
5103 nla_put_failure:
5104         err = -EMSGSIZE;
5105 get_offload_stats_failure:
5106         *prividx = attr_id;
5107         return err;
5108 }
5109
5110 static int rtnl_get_offload_stats_size(const struct net_device *dev)
5111 {
5112         int nla_size = 0;
5113         int attr_id;
5114         int size;
5115
5116         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5117               dev->netdev_ops->ndo_get_offload_stats))
5118                 return 0;
5119
5120         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5121              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5122                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5123                         continue;
5124                 size = rtnl_get_offload_stats_attr_size(attr_id);
5125                 nla_size += nla_total_size_64bit(size);
5126         }
5127
5128         if (nla_size != 0)
5129                 nla_size += nla_total_size(0);
5130
5131         return nla_size;
5132 }
5133
5134 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5135                                int type, u32 pid, u32 seq, u32 change,
5136                                unsigned int flags, unsigned int filter_mask,
5137                                int *idxattr, int *prividx)
5138 {
5139         struct if_stats_msg *ifsm;
5140         struct nlmsghdr *nlh;
5141         struct nlattr *attr;
5142         int s_prividx = *prividx;
5143         int err;
5144
5145         ASSERT_RTNL();
5146
5147         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5148         if (!nlh)
5149                 return -EMSGSIZE;
5150
5151         ifsm = nlmsg_data(nlh);
5152         ifsm->family = PF_UNSPEC;
5153         ifsm->pad1 = 0;
5154         ifsm->pad2 = 0;
5155         ifsm->ifindex = dev->ifindex;
5156         ifsm->filter_mask = filter_mask;
5157
5158         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5159                 struct rtnl_link_stats64 *sp;
5160
5161                 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5162                                          sizeof(struct rtnl_link_stats64),
5163                                          IFLA_STATS_UNSPEC);
5164                 if (!attr)
5165                         goto nla_put_failure;
5166
5167                 sp = nla_data(attr);
5168                 dev_get_stats(dev, sp);
5169         }
5170
5171         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5172                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5173
5174                 if (ops && ops->fill_linkxstats) {
5175                         *idxattr = IFLA_STATS_LINK_XSTATS;
5176                         attr = nla_nest_start_noflag(skb,
5177                                                      IFLA_STATS_LINK_XSTATS);
5178                         if (!attr)
5179                                 goto nla_put_failure;
5180
5181                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5182                         nla_nest_end(skb, attr);
5183                         if (err)
5184                                 goto nla_put_failure;
5185                         *idxattr = 0;
5186                 }
5187         }
5188
5189         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5190                              *idxattr)) {
5191                 const struct rtnl_link_ops *ops = NULL;
5192                 const struct net_device *master;
5193
5194                 master = netdev_master_upper_dev_get(dev);
5195                 if (master)
5196                         ops = master->rtnl_link_ops;
5197                 if (ops && ops->fill_linkxstats) {
5198                         *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5199                         attr = nla_nest_start_noflag(skb,
5200                                                      IFLA_STATS_LINK_XSTATS_SLAVE);
5201                         if (!attr)
5202                                 goto nla_put_failure;
5203
5204                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5205                         nla_nest_end(skb, attr);
5206                         if (err)
5207                                 goto nla_put_failure;
5208                         *idxattr = 0;
5209                 }
5210         }
5211
5212         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5213                              *idxattr)) {
5214                 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5215                 attr = nla_nest_start_noflag(skb,
5216                                              IFLA_STATS_LINK_OFFLOAD_XSTATS);
5217                 if (!attr)
5218                         goto nla_put_failure;
5219
5220                 err = rtnl_get_offload_stats(skb, dev, prividx);
5221                 if (err == -ENODATA)
5222                         nla_nest_cancel(skb, attr);
5223                 else
5224                         nla_nest_end(skb, attr);
5225
5226                 if (err && err != -ENODATA)
5227                         goto nla_put_failure;
5228                 *idxattr = 0;
5229         }
5230
5231         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5232                 struct rtnl_af_ops *af_ops;
5233
5234                 *idxattr = IFLA_STATS_AF_SPEC;
5235                 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5236                 if (!attr)
5237                         goto nla_put_failure;
5238
5239                 rcu_read_lock();
5240                 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5241                         if (af_ops->fill_stats_af) {
5242                                 struct nlattr *af;
5243                                 int err;
5244
5245                                 af = nla_nest_start_noflag(skb,
5246                                                            af_ops->family);
5247                                 if (!af) {
5248                                         rcu_read_unlock();
5249                                         goto nla_put_failure;
5250                                 }
5251                                 err = af_ops->fill_stats_af(skb, dev);
5252
5253                                 if (err == -ENODATA) {
5254                                         nla_nest_cancel(skb, af);
5255                                 } else if (err < 0) {
5256                                         rcu_read_unlock();
5257                                         goto nla_put_failure;
5258                                 }
5259
5260                                 nla_nest_end(skb, af);
5261                         }
5262                 }
5263                 rcu_read_unlock();
5264
5265                 nla_nest_end(skb, attr);
5266
5267                 *idxattr = 0;
5268         }
5269
5270         nlmsg_end(skb, nlh);
5271
5272         return 0;
5273
5274 nla_put_failure:
5275         /* not a multi message or no progress mean a real error */
5276         if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5277                 nlmsg_cancel(skb, nlh);
5278         else
5279                 nlmsg_end(skb, nlh);
5280
5281         return -EMSGSIZE;
5282 }
5283
5284 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5285                                   u32 filter_mask)
5286 {
5287         size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5288
5289         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5290                 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5291
5292         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5293                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5294                 int attr = IFLA_STATS_LINK_XSTATS;
5295
5296                 if (ops && ops->get_linkxstats_size) {
5297                         size += nla_total_size(ops->get_linkxstats_size(dev,
5298                                                                         attr));
5299                         /* for IFLA_STATS_LINK_XSTATS */
5300                         size += nla_total_size(0);
5301                 }
5302         }
5303
5304         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5305                 struct net_device *_dev = (struct net_device *)dev;
5306                 const struct rtnl_link_ops *ops = NULL;
5307                 const struct net_device *master;
5308
5309                 /* netdev_master_upper_dev_get can't take const */
5310                 master = netdev_master_upper_dev_get(_dev);
5311                 if (master)
5312                         ops = master->rtnl_link_ops;
5313                 if (ops && ops->get_linkxstats_size) {
5314                         int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5315
5316                         size += nla_total_size(ops->get_linkxstats_size(dev,
5317                                                                         attr));
5318                         /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5319                         size += nla_total_size(0);
5320                 }
5321         }
5322
5323         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5324                 size += rtnl_get_offload_stats_size(dev);
5325
5326         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5327                 struct rtnl_af_ops *af_ops;
5328
5329                 /* for IFLA_STATS_AF_SPEC */
5330                 size += nla_total_size(0);
5331
5332                 rcu_read_lock();
5333                 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5334                         if (af_ops->get_stats_af_size) {
5335                                 size += nla_total_size(
5336                                         af_ops->get_stats_af_size(dev));
5337
5338                                 /* for AF_* */
5339                                 size += nla_total_size(0);
5340                         }
5341                 }
5342                 rcu_read_unlock();
5343         }
5344
5345         return size;
5346 }
5347
5348 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5349                                 bool is_dump, struct netlink_ext_ack *extack)
5350 {
5351         struct if_stats_msg *ifsm;
5352
5353         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5354                 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5355                 return -EINVAL;
5356         }
5357
5358         if (!strict_check)
5359                 return 0;
5360
5361         ifsm = nlmsg_data(nlh);
5362
5363         /* only requests using strict checks can pass data to influence
5364          * the dump. The legacy exception is filter_mask.
5365          */
5366         if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5367                 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5368                 return -EINVAL;
5369         }
5370         if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5371                 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5372                 return -EINVAL;
5373         }
5374         if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5375                 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5376                 return -EINVAL;
5377         }
5378
5379         return 0;
5380 }
5381
5382 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5383                           struct netlink_ext_ack *extack)
5384 {
5385         struct net *net = sock_net(skb->sk);
5386         struct net_device *dev = NULL;
5387         int idxattr = 0, prividx = 0;
5388         struct if_stats_msg *ifsm;
5389         struct sk_buff *nskb;
5390         u32 filter_mask;
5391         int err;
5392
5393         err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5394                                    false, extack);
5395         if (err)
5396                 return err;
5397
5398         ifsm = nlmsg_data(nlh);
5399         if (ifsm->ifindex > 0)
5400                 dev = __dev_get_by_index(net, ifsm->ifindex);
5401         else
5402                 return -EINVAL;
5403
5404         if (!dev)
5405                 return -ENODEV;
5406
5407         filter_mask = ifsm->filter_mask;
5408         if (!filter_mask)
5409                 return -EINVAL;
5410
5411         nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5412         if (!nskb)
5413                 return -ENOBUFS;
5414
5415         err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5416                                   NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5417                                   0, filter_mask, &idxattr, &prividx);
5418         if (err < 0) {
5419                 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5420                 WARN_ON(err == -EMSGSIZE);
5421                 kfree_skb(nskb);
5422         } else {
5423                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5424         }
5425
5426         return err;
5427 }
5428
5429 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5430 {
5431         struct netlink_ext_ack *extack = cb->extack;
5432         int h, s_h, err, s_idx, s_idxattr, s_prividx;
5433         struct net *net = sock_net(skb->sk);
5434         unsigned int flags = NLM_F_MULTI;
5435         struct if_stats_msg *ifsm;
5436         struct hlist_head *head;
5437         struct net_device *dev;
5438         u32 filter_mask = 0;
5439         int idx = 0;
5440
5441         s_h = cb->args[0];
5442         s_idx = cb->args[1];
5443         s_idxattr = cb->args[2];
5444         s_prividx = cb->args[3];
5445
5446         cb->seq = net->dev_base_seq;
5447
5448         err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5449         if (err)
5450                 return err;
5451
5452         ifsm = nlmsg_data(cb->nlh);
5453         filter_mask = ifsm->filter_mask;
5454         if (!filter_mask) {
5455                 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5456                 return -EINVAL;
5457         }
5458
5459         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5460                 idx = 0;
5461                 head = &net->dev_index_head[h];
5462                 hlist_for_each_entry(dev, head, index_hlist) {
5463                         if (idx < s_idx)
5464                                 goto cont;
5465                         err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5466                                                   NETLINK_CB(cb->skb).portid,
5467                                                   cb->nlh->nlmsg_seq, 0,
5468                                                   flags, filter_mask,
5469                                                   &s_idxattr, &s_prividx);
5470                         /* If we ran out of room on the first message,
5471                          * we're in trouble
5472                          */
5473                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5474
5475                         if (err < 0)
5476                                 goto out;
5477                         s_prividx = 0;
5478                         s_idxattr = 0;
5479                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5480 cont:
5481                         idx++;
5482                 }
5483         }
5484 out:
5485         cb->args[3] = s_prividx;
5486         cb->args[2] = s_idxattr;
5487         cb->args[1] = idx;
5488         cb->args[0] = h;
5489
5490         return skb->len;
5491 }
5492
5493 /* Process one rtnetlink message. */
5494
5495 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5496                              struct netlink_ext_ack *extack)
5497 {
5498         struct net *net = sock_net(skb->sk);
5499         struct rtnl_link *link;
5500         struct module *owner;
5501         int err = -EOPNOTSUPP;
5502         rtnl_doit_func doit;
5503         unsigned int flags;
5504         int kind;
5505         int family;
5506         int type;
5507
5508         type = nlh->nlmsg_type;
5509         if (type > RTM_MAX)
5510                 return -EOPNOTSUPP;
5511
5512         type -= RTM_BASE;
5513
5514         /* All the messages must have at least 1 byte length */
5515         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5516                 return 0;
5517
5518         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5519         kind = type&3;
5520
5521         if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5522                 return -EPERM;
5523
5524         rcu_read_lock();
5525         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5526                 struct sock *rtnl;
5527                 rtnl_dumpit_func dumpit;
5528                 u32 min_dump_alloc = 0;
5529
5530                 link = rtnl_get_link(family, type);
5531                 if (!link || !link->dumpit) {
5532                         family = PF_UNSPEC;
5533                         link = rtnl_get_link(family, type);
5534                         if (!link || !link->dumpit)
5535                                 goto err_unlock;
5536                 }
5537                 owner = link->owner;
5538                 dumpit = link->dumpit;
5539
5540                 if (type == RTM_GETLINK - RTM_BASE)
5541                         min_dump_alloc = rtnl_calcit(skb, nlh);
5542
5543                 err = 0;
5544                 /* need to do this before rcu_read_unlock() */
5545                 if (!try_module_get(owner))
5546                         err = -EPROTONOSUPPORT;
5547
5548                 rcu_read_unlock();
5549
5550                 rtnl = net->rtnl;
5551                 if (err == 0) {
5552                         struct netlink_dump_control c = {
5553                                 .dump           = dumpit,
5554                                 .min_dump_alloc = min_dump_alloc,
5555                                 .module         = owner,
5556                         };
5557                         err = netlink_dump_start(rtnl, skb, nlh, &c);
5558                         /* netlink_dump_start() will keep a reference on
5559                          * module if dump is still in progress.
5560                          */
5561                         module_put(owner);
5562                 }
5563                 return err;
5564         }
5565
5566         link = rtnl_get_link(family, type);
5567         if (!link || !link->doit) {
5568                 family = PF_UNSPEC;
5569                 link = rtnl_get_link(PF_UNSPEC, type);
5570                 if (!link || !link->doit)
5571                         goto out_unlock;
5572         }
5573
5574         owner = link->owner;
5575         if (!try_module_get(owner)) {
5576                 err = -EPROTONOSUPPORT;
5577                 goto out_unlock;
5578         }
5579
5580         flags = link->flags;
5581         if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5582                 doit = link->doit;
5583                 rcu_read_unlock();
5584                 if (doit)
5585                         err = doit(skb, nlh, extack);
5586                 module_put(owner);
5587                 return err;
5588         }
5589         rcu_read_unlock();
5590
5591         rtnl_lock();
5592         link = rtnl_get_link(family, type);
5593         if (link && link->doit)
5594                 err = link->doit(skb, nlh, extack);
5595         rtnl_unlock();
5596
5597         module_put(owner);
5598
5599         return err;
5600
5601 out_unlock:
5602         rcu_read_unlock();
5603         return err;
5604
5605 err_unlock:
5606         rcu_read_unlock();
5607         return -EOPNOTSUPP;
5608 }
5609
5610 static void rtnetlink_rcv(struct sk_buff *skb)
5611 {
5612         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5613 }
5614
5615 static int rtnetlink_bind(struct net *net, int group)
5616 {
5617         switch (group) {
5618         case RTNLGRP_IPV4_MROUTE_R:
5619         case RTNLGRP_IPV6_MROUTE_R:
5620                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5621                         return -EPERM;
5622                 break;
5623         }
5624         return 0;
5625 }
5626
5627 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5628 {
5629         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5630
5631         switch (event) {
5632         case NETDEV_REBOOT:
5633         case NETDEV_CHANGEMTU:
5634         case NETDEV_CHANGEADDR:
5635         case NETDEV_CHANGENAME:
5636         case NETDEV_FEAT_CHANGE:
5637         case NETDEV_BONDING_FAILOVER:
5638         case NETDEV_POST_TYPE_CHANGE:
5639         case NETDEV_NOTIFY_PEERS:
5640         case NETDEV_CHANGEUPPER:
5641         case NETDEV_RESEND_IGMP:
5642         case NETDEV_CHANGEINFODATA:
5643         case NETDEV_CHANGELOWERSTATE:
5644         case NETDEV_CHANGE_TX_QUEUE_LEN:
5645                 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5646                                    GFP_KERNEL, NULL, 0);
5647                 break;
5648         default:
5649                 break;
5650         }
5651         return NOTIFY_DONE;
5652 }
5653
5654 static struct notifier_block rtnetlink_dev_notifier = {
5655         .notifier_call  = rtnetlink_event,
5656 };
5657
5658
5659 static int __net_init rtnetlink_net_init(struct net *net)
5660 {
5661         struct sock *sk;
5662         struct netlink_kernel_cfg cfg = {
5663                 .groups         = RTNLGRP_MAX,
5664                 .input          = rtnetlink_rcv,
5665                 .cb_mutex       = &rtnl_mutex,
5666                 .flags          = NL_CFG_F_NONROOT_RECV,
5667                 .bind           = rtnetlink_bind,
5668         };
5669
5670         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5671         if (!sk)
5672                 return -ENOMEM;
5673         net->rtnl = sk;
5674         return 0;
5675 }
5676
5677 static void __net_exit rtnetlink_net_exit(struct net *net)
5678 {
5679         netlink_kernel_release(net->rtnl);
5680         net->rtnl = NULL;
5681 }
5682
5683 static struct pernet_operations rtnetlink_net_ops = {
5684         .init = rtnetlink_net_init,
5685         .exit = rtnetlink_net_exit,
5686 };
5687
5688 void __init rtnetlink_init(void)
5689 {
5690         if (register_pernet_subsys(&rtnetlink_net_ops))
5691                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5692
5693         register_netdevice_notifier(&rtnetlink_dev_notifier);
5694
5695         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5696                       rtnl_dump_ifinfo, 0);
5697         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5698         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5699         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5700
5701         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5702         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5703         rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5704
5705         rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5706         rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5707
5708         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5709         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5710         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5711
5712         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5713         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5714         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5715
5716         rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
5717                       0);
5718 }