]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/core/rtnetlink.c
Merge tag 'platform-drivers-x86-v4.12-2' of git://git.infradead.org/linux-platform...
[karo-tx-linux.git] / net / core / rtnetlink.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *              This program is free software; you can redistribute it and/or
11  *              modify it under the terms of the GNU General Public License
12  *              as published by the Free Software Foundation; either version
13  *              2 of the License, or (at your option) any later version.
14  *
15  *      Fixes:
16  *      Vitaly E. Lavrov                RTA_OK arithmetics was wrong.
17  */
18
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
42
43 #include <linux/uaccess.h>
44
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <net/udp.h>
53 #include <net/tcp.h>
54 #include <net/sock.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
59
60 struct rtnl_link {
61         rtnl_doit_func          doit;
62         rtnl_dumpit_func        dumpit;
63         rtnl_calcit_func        calcit;
64 };
65
66 static DEFINE_MUTEX(rtnl_mutex);
67
68 void rtnl_lock(void)
69 {
70         mutex_lock(&rtnl_mutex);
71 }
72 EXPORT_SYMBOL(rtnl_lock);
73
74 static struct sk_buff *defer_kfree_skb_list;
75 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
76 {
77         if (head && tail) {
78                 tail->next = defer_kfree_skb_list;
79                 defer_kfree_skb_list = head;
80         }
81 }
82 EXPORT_SYMBOL(rtnl_kfree_skbs);
83
84 void __rtnl_unlock(void)
85 {
86         struct sk_buff *head = defer_kfree_skb_list;
87
88         defer_kfree_skb_list = NULL;
89
90         mutex_unlock(&rtnl_mutex);
91
92         while (head) {
93                 struct sk_buff *next = head->next;
94
95                 kfree_skb(head);
96                 cond_resched();
97                 head = next;
98         }
99 }
100
101 void rtnl_unlock(void)
102 {
103         /* This fellow will unlock it for us. */
104         netdev_run_todo();
105 }
106 EXPORT_SYMBOL(rtnl_unlock);
107
108 int rtnl_trylock(void)
109 {
110         return mutex_trylock(&rtnl_mutex);
111 }
112 EXPORT_SYMBOL(rtnl_trylock);
113
114 int rtnl_is_locked(void)
115 {
116         return mutex_is_locked(&rtnl_mutex);
117 }
118 EXPORT_SYMBOL(rtnl_is_locked);
119
120 #ifdef CONFIG_PROVE_LOCKING
121 bool lockdep_rtnl_is_held(void)
122 {
123         return lockdep_is_held(&rtnl_mutex);
124 }
125 EXPORT_SYMBOL(lockdep_rtnl_is_held);
126 #endif /* #ifdef CONFIG_PROVE_LOCKING */
127
128 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
129
130 static inline int rtm_msgindex(int msgtype)
131 {
132         int msgindex = msgtype - RTM_BASE;
133
134         /*
135          * msgindex < 0 implies someone tried to register a netlink
136          * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
137          * the message type has not been added to linux/rtnetlink.h
138          */
139         BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
140
141         return msgindex;
142 }
143
144 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
145 {
146         struct rtnl_link *tab;
147
148         if (protocol <= RTNL_FAMILY_MAX)
149                 tab = rtnl_msg_handlers[protocol];
150         else
151                 tab = NULL;
152
153         if (tab == NULL || tab[msgindex].doit == NULL)
154                 tab = rtnl_msg_handlers[PF_UNSPEC];
155
156         return tab[msgindex].doit;
157 }
158
159 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
160 {
161         struct rtnl_link *tab;
162
163         if (protocol <= RTNL_FAMILY_MAX)
164                 tab = rtnl_msg_handlers[protocol];
165         else
166                 tab = NULL;
167
168         if (tab == NULL || tab[msgindex].dumpit == NULL)
169                 tab = rtnl_msg_handlers[PF_UNSPEC];
170
171         return tab[msgindex].dumpit;
172 }
173
174 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
175 {
176         struct rtnl_link *tab;
177
178         if (protocol <= RTNL_FAMILY_MAX)
179                 tab = rtnl_msg_handlers[protocol];
180         else
181                 tab = NULL;
182
183         if (tab == NULL || tab[msgindex].calcit == NULL)
184                 tab = rtnl_msg_handlers[PF_UNSPEC];
185
186         return tab[msgindex].calcit;
187 }
188
189 /**
190  * __rtnl_register - Register a rtnetlink message type
191  * @protocol: Protocol family or PF_UNSPEC
192  * @msgtype: rtnetlink message type
193  * @doit: Function pointer called for each request message
194  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
195  * @calcit: Function pointer to calc size of dump message
196  *
197  * Registers the specified function pointers (at least one of them has
198  * to be non-NULL) to be called whenever a request message for the
199  * specified protocol family and message type is received.
200  *
201  * The special protocol family PF_UNSPEC may be used to define fallback
202  * function pointers for the case when no entry for the specific protocol
203  * family exists.
204  *
205  * Returns 0 on success or a negative error code.
206  */
207 int __rtnl_register(int protocol, int msgtype,
208                     rtnl_doit_func doit, rtnl_dumpit_func dumpit,
209                     rtnl_calcit_func calcit)
210 {
211         struct rtnl_link *tab;
212         int msgindex;
213
214         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
215         msgindex = rtm_msgindex(msgtype);
216
217         tab = rtnl_msg_handlers[protocol];
218         if (tab == NULL) {
219                 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
220                 if (tab == NULL)
221                         return -ENOBUFS;
222
223                 rtnl_msg_handlers[protocol] = tab;
224         }
225
226         if (doit)
227                 tab[msgindex].doit = doit;
228
229         if (dumpit)
230                 tab[msgindex].dumpit = dumpit;
231
232         if (calcit)
233                 tab[msgindex].calcit = calcit;
234
235         return 0;
236 }
237 EXPORT_SYMBOL_GPL(__rtnl_register);
238
239 /**
240  * rtnl_register - Register a rtnetlink message type
241  *
242  * Identical to __rtnl_register() but panics on failure. This is useful
243  * as failure of this function is very unlikely, it can only happen due
244  * to lack of memory when allocating the chain to store all message
245  * handlers for a protocol. Meant for use in init functions where lack
246  * of memory implies no sense in continuing.
247  */
248 void rtnl_register(int protocol, int msgtype,
249                    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
250                    rtnl_calcit_func calcit)
251 {
252         if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
253                 panic("Unable to register rtnetlink message handler, "
254                       "protocol = %d, message type = %d\n",
255                       protocol, msgtype);
256 }
257 EXPORT_SYMBOL_GPL(rtnl_register);
258
259 /**
260  * rtnl_unregister - Unregister a rtnetlink message type
261  * @protocol: Protocol family or PF_UNSPEC
262  * @msgtype: rtnetlink message type
263  *
264  * Returns 0 on success or a negative error code.
265  */
266 int rtnl_unregister(int protocol, int msgtype)
267 {
268         int msgindex;
269
270         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
271         msgindex = rtm_msgindex(msgtype);
272
273         if (rtnl_msg_handlers[protocol] == NULL)
274                 return -ENOENT;
275
276         rtnl_msg_handlers[protocol][msgindex].doit = NULL;
277         rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
278         rtnl_msg_handlers[protocol][msgindex].calcit = NULL;
279
280         return 0;
281 }
282 EXPORT_SYMBOL_GPL(rtnl_unregister);
283
284 /**
285  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
286  * @protocol : Protocol family or PF_UNSPEC
287  *
288  * Identical to calling rtnl_unregster() for all registered message types
289  * of a certain protocol family.
290  */
291 void rtnl_unregister_all(int protocol)
292 {
293         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
294
295         kfree(rtnl_msg_handlers[protocol]);
296         rtnl_msg_handlers[protocol] = NULL;
297 }
298 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
299
300 static LIST_HEAD(link_ops);
301
302 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
303 {
304         const struct rtnl_link_ops *ops;
305
306         list_for_each_entry(ops, &link_ops, list) {
307                 if (!strcmp(ops->kind, kind))
308                         return ops;
309         }
310         return NULL;
311 }
312
313 /**
314  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
315  * @ops: struct rtnl_link_ops * to register
316  *
317  * The caller must hold the rtnl_mutex. This function should be used
318  * by drivers that create devices during module initialization. It
319  * must be called before registering the devices.
320  *
321  * Returns 0 on success or a negative error code.
322  */
323 int __rtnl_link_register(struct rtnl_link_ops *ops)
324 {
325         if (rtnl_link_ops_get(ops->kind))
326                 return -EEXIST;
327
328         /* The check for setup is here because if ops
329          * does not have that filled up, it is not possible
330          * to use the ops for creating device. So do not
331          * fill up dellink as well. That disables rtnl_dellink.
332          */
333         if (ops->setup && !ops->dellink)
334                 ops->dellink = unregister_netdevice_queue;
335
336         list_add_tail(&ops->list, &link_ops);
337         return 0;
338 }
339 EXPORT_SYMBOL_GPL(__rtnl_link_register);
340
341 /**
342  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
343  * @ops: struct rtnl_link_ops * to register
344  *
345  * Returns 0 on success or a negative error code.
346  */
347 int rtnl_link_register(struct rtnl_link_ops *ops)
348 {
349         int err;
350
351         rtnl_lock();
352         err = __rtnl_link_register(ops);
353         rtnl_unlock();
354         return err;
355 }
356 EXPORT_SYMBOL_GPL(rtnl_link_register);
357
358 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
359 {
360         struct net_device *dev;
361         LIST_HEAD(list_kill);
362
363         for_each_netdev(net, dev) {
364                 if (dev->rtnl_link_ops == ops)
365                         ops->dellink(dev, &list_kill);
366         }
367         unregister_netdevice_many(&list_kill);
368 }
369
370 /**
371  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
372  * @ops: struct rtnl_link_ops * to unregister
373  *
374  * The caller must hold the rtnl_mutex.
375  */
376 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
377 {
378         struct net *net;
379
380         for_each_net(net) {
381                 __rtnl_kill_links(net, ops);
382         }
383         list_del(&ops->list);
384 }
385 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
386
387 /* Return with the rtnl_lock held when there are no network
388  * devices unregistering in any network namespace.
389  */
390 static void rtnl_lock_unregistering_all(void)
391 {
392         struct net *net;
393         bool unregistering;
394         DEFINE_WAIT_FUNC(wait, woken_wake_function);
395
396         add_wait_queue(&netdev_unregistering_wq, &wait);
397         for (;;) {
398                 unregistering = false;
399                 rtnl_lock();
400                 for_each_net(net) {
401                         if (net->dev_unreg_count > 0) {
402                                 unregistering = true;
403                                 break;
404                         }
405                 }
406                 if (!unregistering)
407                         break;
408                 __rtnl_unlock();
409
410                 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
411         }
412         remove_wait_queue(&netdev_unregistering_wq, &wait);
413 }
414
415 /**
416  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
417  * @ops: struct rtnl_link_ops * to unregister
418  */
419 void rtnl_link_unregister(struct rtnl_link_ops *ops)
420 {
421         /* Close the race with cleanup_net() */
422         mutex_lock(&net_mutex);
423         rtnl_lock_unregistering_all();
424         __rtnl_link_unregister(ops);
425         rtnl_unlock();
426         mutex_unlock(&net_mutex);
427 }
428 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
429
430 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
431 {
432         struct net_device *master_dev;
433         const struct rtnl_link_ops *ops;
434
435         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
436         if (!master_dev)
437                 return 0;
438         ops = master_dev->rtnl_link_ops;
439         if (!ops || !ops->get_slave_size)
440                 return 0;
441         /* IFLA_INFO_SLAVE_DATA + nested data */
442         return nla_total_size(sizeof(struct nlattr)) +
443                ops->get_slave_size(master_dev, dev);
444 }
445
446 static size_t rtnl_link_get_size(const struct net_device *dev)
447 {
448         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
449         size_t size;
450
451         if (!ops)
452                 return 0;
453
454         size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
455                nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
456
457         if (ops->get_size)
458                 /* IFLA_INFO_DATA + nested data */
459                 size += nla_total_size(sizeof(struct nlattr)) +
460                         ops->get_size(dev);
461
462         if (ops->get_xstats_size)
463                 /* IFLA_INFO_XSTATS */
464                 size += nla_total_size(ops->get_xstats_size(dev));
465
466         size += rtnl_link_get_slave_info_data_size(dev);
467
468         return size;
469 }
470
471 static LIST_HEAD(rtnl_af_ops);
472
473 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
474 {
475         const struct rtnl_af_ops *ops;
476
477         list_for_each_entry(ops, &rtnl_af_ops, list) {
478                 if (ops->family == family)
479                         return ops;
480         }
481
482         return NULL;
483 }
484
485 /**
486  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
487  * @ops: struct rtnl_af_ops * to register
488  *
489  * Returns 0 on success or a negative error code.
490  */
491 void rtnl_af_register(struct rtnl_af_ops *ops)
492 {
493         rtnl_lock();
494         list_add_tail(&ops->list, &rtnl_af_ops);
495         rtnl_unlock();
496 }
497 EXPORT_SYMBOL_GPL(rtnl_af_register);
498
499 /**
500  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
501  * @ops: struct rtnl_af_ops * to unregister
502  *
503  * The caller must hold the rtnl_mutex.
504  */
505 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
506 {
507         list_del(&ops->list);
508 }
509 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
510
511 /**
512  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
513  * @ops: struct rtnl_af_ops * to unregister
514  */
515 void rtnl_af_unregister(struct rtnl_af_ops *ops)
516 {
517         rtnl_lock();
518         __rtnl_af_unregister(ops);
519         rtnl_unlock();
520 }
521 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
522
523 static size_t rtnl_link_get_af_size(const struct net_device *dev,
524                                     u32 ext_filter_mask)
525 {
526         struct rtnl_af_ops *af_ops;
527         size_t size;
528
529         /* IFLA_AF_SPEC */
530         size = nla_total_size(sizeof(struct nlattr));
531
532         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
533                 if (af_ops->get_link_af_size) {
534                         /* AF_* + nested data */
535                         size += nla_total_size(sizeof(struct nlattr)) +
536                                 af_ops->get_link_af_size(dev, ext_filter_mask);
537                 }
538         }
539
540         return size;
541 }
542
543 static bool rtnl_have_link_slave_info(const struct net_device *dev)
544 {
545         struct net_device *master_dev;
546
547         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
548         if (master_dev && master_dev->rtnl_link_ops)
549                 return true;
550         return false;
551 }
552
553 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
554                                      const struct net_device *dev)
555 {
556         struct net_device *master_dev;
557         const struct rtnl_link_ops *ops;
558         struct nlattr *slave_data;
559         int err;
560
561         master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
562         if (!master_dev)
563                 return 0;
564         ops = master_dev->rtnl_link_ops;
565         if (!ops)
566                 return 0;
567         if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
568                 return -EMSGSIZE;
569         if (ops->fill_slave_info) {
570                 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
571                 if (!slave_data)
572                         return -EMSGSIZE;
573                 err = ops->fill_slave_info(skb, master_dev, dev);
574                 if (err < 0)
575                         goto err_cancel_slave_data;
576                 nla_nest_end(skb, slave_data);
577         }
578         return 0;
579
580 err_cancel_slave_data:
581         nla_nest_cancel(skb, slave_data);
582         return err;
583 }
584
585 static int rtnl_link_info_fill(struct sk_buff *skb,
586                                const struct net_device *dev)
587 {
588         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
589         struct nlattr *data;
590         int err;
591
592         if (!ops)
593                 return 0;
594         if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
595                 return -EMSGSIZE;
596         if (ops->fill_xstats) {
597                 err = ops->fill_xstats(skb, dev);
598                 if (err < 0)
599                         return err;
600         }
601         if (ops->fill_info) {
602                 data = nla_nest_start(skb, IFLA_INFO_DATA);
603                 if (data == NULL)
604                         return -EMSGSIZE;
605                 err = ops->fill_info(skb, dev);
606                 if (err < 0)
607                         goto err_cancel_data;
608                 nla_nest_end(skb, data);
609         }
610         return 0;
611
612 err_cancel_data:
613         nla_nest_cancel(skb, data);
614         return err;
615 }
616
617 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
618 {
619         struct nlattr *linkinfo;
620         int err = -EMSGSIZE;
621
622         linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
623         if (linkinfo == NULL)
624                 goto out;
625
626         err = rtnl_link_info_fill(skb, dev);
627         if (err < 0)
628                 goto err_cancel_link;
629
630         err = rtnl_link_slave_info_fill(skb, dev);
631         if (err < 0)
632                 goto err_cancel_link;
633
634         nla_nest_end(skb, linkinfo);
635         return 0;
636
637 err_cancel_link:
638         nla_nest_cancel(skb, linkinfo);
639 out:
640         return err;
641 }
642
643 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
644 {
645         struct sock *rtnl = net->rtnl;
646         int err = 0;
647
648         NETLINK_CB(skb).dst_group = group;
649         if (echo)
650                 atomic_inc(&skb->users);
651         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
652         if (echo)
653                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
654         return err;
655 }
656
657 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
658 {
659         struct sock *rtnl = net->rtnl;
660
661         return nlmsg_unicast(rtnl, skb, pid);
662 }
663 EXPORT_SYMBOL(rtnl_unicast);
664
665 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
666                  struct nlmsghdr *nlh, gfp_t flags)
667 {
668         struct sock *rtnl = net->rtnl;
669         int report = 0;
670
671         if (nlh)
672                 report = nlmsg_report(nlh);
673
674         nlmsg_notify(rtnl, skb, pid, group, report, flags);
675 }
676 EXPORT_SYMBOL(rtnl_notify);
677
678 void rtnl_set_sk_err(struct net *net, u32 group, int error)
679 {
680         struct sock *rtnl = net->rtnl;
681
682         netlink_set_err(rtnl, 0, group, error);
683 }
684 EXPORT_SYMBOL(rtnl_set_sk_err);
685
686 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
687 {
688         struct nlattr *mx;
689         int i, valid = 0;
690
691         mx = nla_nest_start(skb, RTA_METRICS);
692         if (mx == NULL)
693                 return -ENOBUFS;
694
695         for (i = 0; i < RTAX_MAX; i++) {
696                 if (metrics[i]) {
697                         if (i == RTAX_CC_ALGO - 1) {
698                                 char tmp[TCP_CA_NAME_MAX], *name;
699
700                                 name = tcp_ca_get_name_by_key(metrics[i], tmp);
701                                 if (!name)
702                                         continue;
703                                 if (nla_put_string(skb, i + 1, name))
704                                         goto nla_put_failure;
705                         } else if (i == RTAX_FEATURES - 1) {
706                                 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
707
708                                 if (!user_features)
709                                         continue;
710                                 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
711                                 if (nla_put_u32(skb, i + 1, user_features))
712                                         goto nla_put_failure;
713                         } else {
714                                 if (nla_put_u32(skb, i + 1, metrics[i]))
715                                         goto nla_put_failure;
716                         }
717                         valid++;
718                 }
719         }
720
721         if (!valid) {
722                 nla_nest_cancel(skb, mx);
723                 return 0;
724         }
725
726         return nla_nest_end(skb, mx);
727
728 nla_put_failure:
729         nla_nest_cancel(skb, mx);
730         return -EMSGSIZE;
731 }
732 EXPORT_SYMBOL(rtnetlink_put_metrics);
733
734 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
735                        long expires, u32 error)
736 {
737         struct rta_cacheinfo ci = {
738                 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
739                 .rta_used = dst->__use,
740                 .rta_clntref = atomic_read(&(dst->__refcnt)),
741                 .rta_error = error,
742                 .rta_id =  id,
743         };
744
745         if (expires) {
746                 unsigned long clock;
747
748                 clock = jiffies_to_clock_t(abs(expires));
749                 clock = min_t(unsigned long, clock, INT_MAX);
750                 ci.rta_expires = (expires > 0) ? clock : -clock;
751         }
752         return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
753 }
754 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
755
756 static void set_operstate(struct net_device *dev, unsigned char transition)
757 {
758         unsigned char operstate = dev->operstate;
759
760         switch (transition) {
761         case IF_OPER_UP:
762                 if ((operstate == IF_OPER_DORMANT ||
763                      operstate == IF_OPER_UNKNOWN) &&
764                     !netif_dormant(dev))
765                         operstate = IF_OPER_UP;
766                 break;
767
768         case IF_OPER_DORMANT:
769                 if (operstate == IF_OPER_UP ||
770                     operstate == IF_OPER_UNKNOWN)
771                         operstate = IF_OPER_DORMANT;
772                 break;
773         }
774
775         if (dev->operstate != operstate) {
776                 write_lock_bh(&dev_base_lock);
777                 dev->operstate = operstate;
778                 write_unlock_bh(&dev_base_lock);
779                 netdev_state_change(dev);
780         }
781 }
782
783 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
784 {
785         return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
786                (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
787 }
788
789 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
790                                            const struct ifinfomsg *ifm)
791 {
792         unsigned int flags = ifm->ifi_flags;
793
794         /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
795         if (ifm->ifi_change)
796                 flags = (flags & ifm->ifi_change) |
797                         (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
798
799         return flags;
800 }
801
802 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
803                                  const struct rtnl_link_stats64 *b)
804 {
805         a->rx_packets = b->rx_packets;
806         a->tx_packets = b->tx_packets;
807         a->rx_bytes = b->rx_bytes;
808         a->tx_bytes = b->tx_bytes;
809         a->rx_errors = b->rx_errors;
810         a->tx_errors = b->tx_errors;
811         a->rx_dropped = b->rx_dropped;
812         a->tx_dropped = b->tx_dropped;
813
814         a->multicast = b->multicast;
815         a->collisions = b->collisions;
816
817         a->rx_length_errors = b->rx_length_errors;
818         a->rx_over_errors = b->rx_over_errors;
819         a->rx_crc_errors = b->rx_crc_errors;
820         a->rx_frame_errors = b->rx_frame_errors;
821         a->rx_fifo_errors = b->rx_fifo_errors;
822         a->rx_missed_errors = b->rx_missed_errors;
823
824         a->tx_aborted_errors = b->tx_aborted_errors;
825         a->tx_carrier_errors = b->tx_carrier_errors;
826         a->tx_fifo_errors = b->tx_fifo_errors;
827         a->tx_heartbeat_errors = b->tx_heartbeat_errors;
828         a->tx_window_errors = b->tx_window_errors;
829
830         a->rx_compressed = b->rx_compressed;
831         a->tx_compressed = b->tx_compressed;
832
833         a->rx_nohandler = b->rx_nohandler;
834 }
835
836 /* All VF info */
837 static inline int rtnl_vfinfo_size(const struct net_device *dev,
838                                    u32 ext_filter_mask)
839 {
840         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
841                 int num_vfs = dev_num_vf(dev->dev.parent);
842                 size_t size = nla_total_size(0);
843                 size += num_vfs *
844                         (nla_total_size(0) +
845                          nla_total_size(sizeof(struct ifla_vf_mac)) +
846                          nla_total_size(sizeof(struct ifla_vf_vlan)) +
847                          nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
848                          nla_total_size(MAX_VLAN_LIST_LEN *
849                                         sizeof(struct ifla_vf_vlan_info)) +
850                          nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
851                          nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
852                          nla_total_size(sizeof(struct ifla_vf_rate)) +
853                          nla_total_size(sizeof(struct ifla_vf_link_state)) +
854                          nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
855                          nla_total_size(0) + /* nest IFLA_VF_STATS */
856                          /* IFLA_VF_STATS_RX_PACKETS */
857                          nla_total_size_64bit(sizeof(__u64)) +
858                          /* IFLA_VF_STATS_TX_PACKETS */
859                          nla_total_size_64bit(sizeof(__u64)) +
860                          /* IFLA_VF_STATS_RX_BYTES */
861                          nla_total_size_64bit(sizeof(__u64)) +
862                          /* IFLA_VF_STATS_TX_BYTES */
863                          nla_total_size_64bit(sizeof(__u64)) +
864                          /* IFLA_VF_STATS_BROADCAST */
865                          nla_total_size_64bit(sizeof(__u64)) +
866                          /* IFLA_VF_STATS_MULTICAST */
867                          nla_total_size_64bit(sizeof(__u64)) +
868                          nla_total_size(sizeof(struct ifla_vf_trust)));
869                 return size;
870         } else
871                 return 0;
872 }
873
874 static size_t rtnl_port_size(const struct net_device *dev,
875                              u32 ext_filter_mask)
876 {
877         size_t port_size = nla_total_size(4)            /* PORT_VF */
878                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
879                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
880                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
881                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
882                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
883         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
884         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
885                 + port_size;
886         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
887                 + port_size;
888
889         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
890             !(ext_filter_mask & RTEXT_FILTER_VF))
891                 return 0;
892         if (dev_num_vf(dev->dev.parent))
893                 return port_self_size + vf_ports_size +
894                         vf_port_size * dev_num_vf(dev->dev.parent);
895         else
896                 return port_self_size;
897 }
898
899 static size_t rtnl_xdp_size(void)
900 {
901         size_t xdp_size = nla_total_size(0) +   /* nest IFLA_XDP */
902                           nla_total_size(1);    /* XDP_ATTACHED */
903
904         return xdp_size;
905 }
906
907 static noinline size_t if_nlmsg_size(const struct net_device *dev,
908                                      u32 ext_filter_mask)
909 {
910         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
911                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
912                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
913                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
914                + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
915                + nla_total_size(sizeof(struct rtnl_link_stats))
916                + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
917                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
918                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
919                + nla_total_size(4) /* IFLA_TXQLEN */
920                + nla_total_size(4) /* IFLA_WEIGHT */
921                + nla_total_size(4) /* IFLA_MTU */
922                + nla_total_size(4) /* IFLA_LINK */
923                + nla_total_size(4) /* IFLA_MASTER */
924                + nla_total_size(1) /* IFLA_CARRIER */
925                + nla_total_size(4) /* IFLA_PROMISCUITY */
926                + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
927                + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
928                + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
929                + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
930                + nla_total_size(1) /* IFLA_OPERSTATE */
931                + nla_total_size(1) /* IFLA_LINKMODE */
932                + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
933                + nla_total_size(4) /* IFLA_LINK_NETNSID */
934                + nla_total_size(ext_filter_mask
935                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
936                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
937                + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
938                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
939                + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
940                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
941                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
942                + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
943                + rtnl_xdp_size() /* IFLA_XDP */
944                + nla_total_size(1); /* IFLA_PROTO_DOWN */
945
946 }
947
948 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
949 {
950         struct nlattr *vf_ports;
951         struct nlattr *vf_port;
952         int vf;
953         int err;
954
955         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
956         if (!vf_ports)
957                 return -EMSGSIZE;
958
959         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
960                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
961                 if (!vf_port)
962                         goto nla_put_failure;
963                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
964                         goto nla_put_failure;
965                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
966                 if (err == -EMSGSIZE)
967                         goto nla_put_failure;
968                 if (err) {
969                         nla_nest_cancel(skb, vf_port);
970                         continue;
971                 }
972                 nla_nest_end(skb, vf_port);
973         }
974
975         nla_nest_end(skb, vf_ports);
976
977         return 0;
978
979 nla_put_failure:
980         nla_nest_cancel(skb, vf_ports);
981         return -EMSGSIZE;
982 }
983
984 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
985 {
986         struct nlattr *port_self;
987         int err;
988
989         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
990         if (!port_self)
991                 return -EMSGSIZE;
992
993         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
994         if (err) {
995                 nla_nest_cancel(skb, port_self);
996                 return (err == -EMSGSIZE) ? err : 0;
997         }
998
999         nla_nest_end(skb, port_self);
1000
1001         return 0;
1002 }
1003
1004 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1005                           u32 ext_filter_mask)
1006 {
1007         int err;
1008
1009         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1010             !(ext_filter_mask & RTEXT_FILTER_VF))
1011                 return 0;
1012
1013         err = rtnl_port_self_fill(skb, dev);
1014         if (err)
1015                 return err;
1016
1017         if (dev_num_vf(dev->dev.parent)) {
1018                 err = rtnl_vf_ports_fill(skb, dev);
1019                 if (err)
1020                         return err;
1021         }
1022
1023         return 0;
1024 }
1025
1026 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1027 {
1028         int err;
1029         struct netdev_phys_item_id ppid;
1030
1031         err = dev_get_phys_port_id(dev, &ppid);
1032         if (err) {
1033                 if (err == -EOPNOTSUPP)
1034                         return 0;
1035                 return err;
1036         }
1037
1038         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1039                 return -EMSGSIZE;
1040
1041         return 0;
1042 }
1043
1044 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1045 {
1046         char name[IFNAMSIZ];
1047         int err;
1048
1049         err = dev_get_phys_port_name(dev, name, sizeof(name));
1050         if (err) {
1051                 if (err == -EOPNOTSUPP)
1052                         return 0;
1053                 return err;
1054         }
1055
1056         if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1057                 return -EMSGSIZE;
1058
1059         return 0;
1060 }
1061
1062 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1063 {
1064         int err;
1065         struct switchdev_attr attr = {
1066                 .orig_dev = dev,
1067                 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1068                 .flags = SWITCHDEV_F_NO_RECURSE,
1069         };
1070
1071         err = switchdev_port_attr_get(dev, &attr);
1072         if (err) {
1073                 if (err == -EOPNOTSUPP)
1074                         return 0;
1075                 return err;
1076         }
1077
1078         if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1079                     attr.u.ppid.id))
1080                 return -EMSGSIZE;
1081
1082         return 0;
1083 }
1084
1085 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1086                                               struct net_device *dev)
1087 {
1088         struct rtnl_link_stats64 *sp;
1089         struct nlattr *attr;
1090
1091         attr = nla_reserve_64bit(skb, IFLA_STATS64,
1092                                  sizeof(struct rtnl_link_stats64), IFLA_PAD);
1093         if (!attr)
1094                 return -EMSGSIZE;
1095
1096         sp = nla_data(attr);
1097         dev_get_stats(dev, sp);
1098
1099         attr = nla_reserve(skb, IFLA_STATS,
1100                            sizeof(struct rtnl_link_stats));
1101         if (!attr)
1102                 return -EMSGSIZE;
1103
1104         copy_rtnl_link_stats(nla_data(attr), sp);
1105
1106         return 0;
1107 }
1108
1109 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1110                                                struct net_device *dev,
1111                                                int vfs_num,
1112                                                struct nlattr *vfinfo)
1113 {
1114         struct ifla_vf_rss_query_en vf_rss_query_en;
1115         struct nlattr *vf, *vfstats, *vfvlanlist;
1116         struct ifla_vf_link_state vf_linkstate;
1117         struct ifla_vf_vlan_info vf_vlan_info;
1118         struct ifla_vf_spoofchk vf_spoofchk;
1119         struct ifla_vf_tx_rate vf_tx_rate;
1120         struct ifla_vf_stats vf_stats;
1121         struct ifla_vf_trust vf_trust;
1122         struct ifla_vf_vlan vf_vlan;
1123         struct ifla_vf_rate vf_rate;
1124         struct ifla_vf_mac vf_mac;
1125         struct ifla_vf_info ivi;
1126
1127         memset(&ivi, 0, sizeof(ivi));
1128
1129         /* Not all SR-IOV capable drivers support the
1130          * spoofcheck and "RSS query enable" query.  Preset to
1131          * -1 so the user space tool can detect that the driver
1132          * didn't report anything.
1133          */
1134         ivi.spoofchk = -1;
1135         ivi.rss_query_en = -1;
1136         ivi.trusted = -1;
1137         /* The default value for VF link state is "auto"
1138          * IFLA_VF_LINK_STATE_AUTO which equals zero
1139          */
1140         ivi.linkstate = 0;
1141         /* VLAN Protocol by default is 802.1Q */
1142         ivi.vlan_proto = htons(ETH_P_8021Q);
1143         if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1144                 return 0;
1145
1146         memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1147
1148         vf_mac.vf =
1149                 vf_vlan.vf =
1150                 vf_vlan_info.vf =
1151                 vf_rate.vf =
1152                 vf_tx_rate.vf =
1153                 vf_spoofchk.vf =
1154                 vf_linkstate.vf =
1155                 vf_rss_query_en.vf =
1156                 vf_trust.vf = ivi.vf;
1157
1158         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1159         vf_vlan.vlan = ivi.vlan;
1160         vf_vlan.qos = ivi.qos;
1161         vf_vlan_info.vlan = ivi.vlan;
1162         vf_vlan_info.qos = ivi.qos;
1163         vf_vlan_info.vlan_proto = ivi.vlan_proto;
1164         vf_tx_rate.rate = ivi.max_tx_rate;
1165         vf_rate.min_tx_rate = ivi.min_tx_rate;
1166         vf_rate.max_tx_rate = ivi.max_tx_rate;
1167         vf_spoofchk.setting = ivi.spoofchk;
1168         vf_linkstate.link_state = ivi.linkstate;
1169         vf_rss_query_en.setting = ivi.rss_query_en;
1170         vf_trust.setting = ivi.trusted;
1171         vf = nla_nest_start(skb, IFLA_VF_INFO);
1172         if (!vf)
1173                 goto nla_put_vfinfo_failure;
1174         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1175             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1176             nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1177                     &vf_rate) ||
1178             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1179                     &vf_tx_rate) ||
1180             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1181                     &vf_spoofchk) ||
1182             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1183                     &vf_linkstate) ||
1184             nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1185                     sizeof(vf_rss_query_en),
1186                     &vf_rss_query_en) ||
1187             nla_put(skb, IFLA_VF_TRUST,
1188                     sizeof(vf_trust), &vf_trust))
1189                 goto nla_put_vf_failure;
1190         vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1191         if (!vfvlanlist)
1192                 goto nla_put_vf_failure;
1193         if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1194                     &vf_vlan_info)) {
1195                 nla_nest_cancel(skb, vfvlanlist);
1196                 goto nla_put_vf_failure;
1197         }
1198         nla_nest_end(skb, vfvlanlist);
1199         memset(&vf_stats, 0, sizeof(vf_stats));
1200         if (dev->netdev_ops->ndo_get_vf_stats)
1201                 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1202                                                 &vf_stats);
1203         vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1204         if (!vfstats)
1205                 goto nla_put_vf_failure;
1206         if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1207                               vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1208             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1209                               vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1210             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1211                               vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1212             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1213                               vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1214             nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1215                               vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1216             nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1217                               vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1218                 nla_nest_cancel(skb, vfstats);
1219                 goto nla_put_vf_failure;
1220         }
1221         nla_nest_end(skb, vfstats);
1222         nla_nest_end(skb, vf);
1223         return 0;
1224
1225 nla_put_vf_failure:
1226         nla_nest_cancel(skb, vf);
1227 nla_put_vfinfo_failure:
1228         nla_nest_cancel(skb, vfinfo);
1229         return -EMSGSIZE;
1230 }
1231
1232 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1233 {
1234         struct rtnl_link_ifmap map;
1235
1236         memset(&map, 0, sizeof(map));
1237         map.mem_start   = dev->mem_start;
1238         map.mem_end     = dev->mem_end;
1239         map.base_addr   = dev->base_addr;
1240         map.irq         = dev->irq;
1241         map.dma         = dev->dma;
1242         map.port        = dev->if_port;
1243
1244         if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1245                 return -EMSGSIZE;
1246
1247         return 0;
1248 }
1249
1250 static u8 rtnl_xdp_attached_mode(struct net_device *dev)
1251 {
1252         const struct net_device_ops *ops = dev->netdev_ops;
1253
1254         ASSERT_RTNL();
1255
1256         if (rcu_access_pointer(dev->xdp_prog))
1257                 return XDP_ATTACHED_SKB;
1258         if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp))
1259                 return XDP_ATTACHED_DRV;
1260
1261         return XDP_ATTACHED_NONE;
1262 }
1263
1264 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1265 {
1266         struct nlattr *xdp;
1267         int err;
1268
1269         xdp = nla_nest_start(skb, IFLA_XDP);
1270         if (!xdp)
1271                 return -EMSGSIZE;
1272
1273         err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
1274                          rtnl_xdp_attached_mode(dev));
1275         if (err)
1276                 goto err_cancel;
1277
1278         nla_nest_end(skb, xdp);
1279         return 0;
1280
1281 err_cancel:
1282         nla_nest_cancel(skb, xdp);
1283         return err;
1284 }
1285
1286 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1287                             int type, u32 pid, u32 seq, u32 change,
1288                             unsigned int flags, u32 ext_filter_mask)
1289 {
1290         struct ifinfomsg *ifm;
1291         struct nlmsghdr *nlh;
1292         struct nlattr *af_spec;
1293         struct rtnl_af_ops *af_ops;
1294         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1295
1296         ASSERT_RTNL();
1297         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1298         if (nlh == NULL)
1299                 return -EMSGSIZE;
1300
1301         ifm = nlmsg_data(nlh);
1302         ifm->ifi_family = AF_UNSPEC;
1303         ifm->__ifi_pad = 0;
1304         ifm->ifi_type = dev->type;
1305         ifm->ifi_index = dev->ifindex;
1306         ifm->ifi_flags = dev_get_flags(dev);
1307         ifm->ifi_change = change;
1308
1309         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1310             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1311             nla_put_u8(skb, IFLA_OPERSTATE,
1312                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1313             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1314             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1315             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1316             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1317             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1318             nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1319             nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1320 #ifdef CONFIG_RPS
1321             nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1322 #endif
1323             (dev->ifindex != dev_get_iflink(dev) &&
1324              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1325             (upper_dev &&
1326              nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1327             nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1328             (dev->qdisc &&
1329              nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1330             (dev->ifalias &&
1331              nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1332             nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1333                         atomic_read(&dev->carrier_changes)) ||
1334             nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1335                 goto nla_put_failure;
1336
1337         if (rtnl_fill_link_ifmap(skb, dev))
1338                 goto nla_put_failure;
1339
1340         if (dev->addr_len) {
1341                 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1342                     nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1343                         goto nla_put_failure;
1344         }
1345
1346         if (rtnl_phys_port_id_fill(skb, dev))
1347                 goto nla_put_failure;
1348
1349         if (rtnl_phys_port_name_fill(skb, dev))
1350                 goto nla_put_failure;
1351
1352         if (rtnl_phys_switch_id_fill(skb, dev))
1353                 goto nla_put_failure;
1354
1355         if (rtnl_fill_stats(skb, dev))
1356                 goto nla_put_failure;
1357
1358         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1359             nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1360                 goto nla_put_failure;
1361
1362         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1363             ext_filter_mask & RTEXT_FILTER_VF) {
1364                 int i;
1365                 struct nlattr *vfinfo;
1366                 int num_vfs = dev_num_vf(dev->dev.parent);
1367
1368                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1369                 if (!vfinfo)
1370                         goto nla_put_failure;
1371                 for (i = 0; i < num_vfs; i++) {
1372                         if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1373                                 goto nla_put_failure;
1374                 }
1375
1376                 nla_nest_end(skb, vfinfo);
1377         }
1378
1379         if (rtnl_port_fill(skb, dev, ext_filter_mask))
1380                 goto nla_put_failure;
1381
1382         if (rtnl_xdp_fill(skb, dev))
1383                 goto nla_put_failure;
1384
1385         if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1386                 if (rtnl_link_fill(skb, dev) < 0)
1387                         goto nla_put_failure;
1388         }
1389
1390         if (dev->rtnl_link_ops &&
1391             dev->rtnl_link_ops->get_link_net) {
1392                 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1393
1394                 if (!net_eq(dev_net(dev), link_net)) {
1395                         int id = peernet2id_alloc(dev_net(dev), link_net);
1396
1397                         if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1398                                 goto nla_put_failure;
1399                 }
1400         }
1401
1402         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1403                 goto nla_put_failure;
1404
1405         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1406                 if (af_ops->fill_link_af) {
1407                         struct nlattr *af;
1408                         int err;
1409
1410                         if (!(af = nla_nest_start(skb, af_ops->family)))
1411                                 goto nla_put_failure;
1412
1413                         err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1414
1415                         /*
1416                          * Caller may return ENODATA to indicate that there
1417                          * was no data to be dumped. This is not an error, it
1418                          * means we should trim the attribute header and
1419                          * continue.
1420                          */
1421                         if (err == -ENODATA)
1422                                 nla_nest_cancel(skb, af);
1423                         else if (err < 0)
1424                                 goto nla_put_failure;
1425
1426                         nla_nest_end(skb, af);
1427                 }
1428         }
1429
1430         nla_nest_end(skb, af_spec);
1431
1432         nlmsg_end(skb, nlh);
1433         return 0;
1434
1435 nla_put_failure:
1436         nlmsg_cancel(skb, nlh);
1437         return -EMSGSIZE;
1438 }
1439
1440 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1441         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1442         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1443         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1444         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
1445         [IFLA_MTU]              = { .type = NLA_U32 },
1446         [IFLA_LINK]             = { .type = NLA_U32 },
1447         [IFLA_MASTER]           = { .type = NLA_U32 },
1448         [IFLA_CARRIER]          = { .type = NLA_U8 },
1449         [IFLA_TXQLEN]           = { .type = NLA_U32 },
1450         [IFLA_WEIGHT]           = { .type = NLA_U32 },
1451         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
1452         [IFLA_LINKMODE]         = { .type = NLA_U8 },
1453         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
1454         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
1455         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
1456         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
1457         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
1458         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
1459         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
1460         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
1461         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
1462         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
1463         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
1464         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
1465         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1466         [IFLA_CARRIER_CHANGES]  = { .type = NLA_U32 },  /* ignored */
1467         [IFLA_PHYS_SWITCH_ID]   = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1468         [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
1469         [IFLA_PROTO_DOWN]       = { .type = NLA_U8 },
1470         [IFLA_XDP]              = { .type = NLA_NESTED },
1471 };
1472
1473 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1474         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1475         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1476         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
1477         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
1478 };
1479
1480 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1481         [IFLA_VF_MAC]           = { .len = sizeof(struct ifla_vf_mac) },
1482         [IFLA_VF_VLAN]          = { .len = sizeof(struct ifla_vf_vlan) },
1483         [IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1484         [IFLA_VF_TX_RATE]       = { .len = sizeof(struct ifla_vf_tx_rate) },
1485         [IFLA_VF_SPOOFCHK]      = { .len = sizeof(struct ifla_vf_spoofchk) },
1486         [IFLA_VF_RATE]          = { .len = sizeof(struct ifla_vf_rate) },
1487         [IFLA_VF_LINK_STATE]    = { .len = sizeof(struct ifla_vf_link_state) },
1488         [IFLA_VF_RSS_QUERY_EN]  = { .len = sizeof(struct ifla_vf_rss_query_en) },
1489         [IFLA_VF_STATS]         = { .type = NLA_NESTED },
1490         [IFLA_VF_TRUST]         = { .len = sizeof(struct ifla_vf_trust) },
1491         [IFLA_VF_IB_NODE_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1492         [IFLA_VF_IB_PORT_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1493 };
1494
1495 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1496         [IFLA_PORT_VF]          = { .type = NLA_U32 },
1497         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
1498                                     .len = PORT_PROFILE_MAX },
1499         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1500                                       .len = PORT_UUID_MAX },
1501         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
1502                                     .len = PORT_UUID_MAX },
1503         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
1504         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
1505
1506         /* Unused, but we need to keep it here since user space could
1507          * fill it. It's also broken with regard to NLA_BINARY use in
1508          * combination with structs.
1509          */
1510         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
1511                                     .len = sizeof(struct ifla_port_vsi) },
1512 };
1513
1514 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1515         [IFLA_XDP_FD]           = { .type = NLA_S32 },
1516         [IFLA_XDP_ATTACHED]     = { .type = NLA_U8 },
1517         [IFLA_XDP_FLAGS]        = { .type = NLA_U32 },
1518 };
1519
1520 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1521 {
1522         const struct rtnl_link_ops *ops = NULL;
1523         struct nlattr *linfo[IFLA_INFO_MAX + 1];
1524
1525         if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1526                              ifla_info_policy, NULL) < 0)
1527                 return NULL;
1528
1529         if (linfo[IFLA_INFO_KIND]) {
1530                 char kind[MODULE_NAME_LEN];
1531
1532                 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1533                 ops = rtnl_link_ops_get(kind);
1534         }
1535
1536         return ops;
1537 }
1538
1539 static bool link_master_filtered(struct net_device *dev, int master_idx)
1540 {
1541         struct net_device *master;
1542
1543         if (!master_idx)
1544                 return false;
1545
1546         master = netdev_master_upper_dev_get(dev);
1547         if (!master || master->ifindex != master_idx)
1548                 return true;
1549
1550         return false;
1551 }
1552
1553 static bool link_kind_filtered(const struct net_device *dev,
1554                                const struct rtnl_link_ops *kind_ops)
1555 {
1556         if (kind_ops && dev->rtnl_link_ops != kind_ops)
1557                 return true;
1558
1559         return false;
1560 }
1561
1562 static bool link_dump_filtered(struct net_device *dev,
1563                                int master_idx,
1564                                const struct rtnl_link_ops *kind_ops)
1565 {
1566         if (link_master_filtered(dev, master_idx) ||
1567             link_kind_filtered(dev, kind_ops))
1568                 return true;
1569
1570         return false;
1571 }
1572
1573 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1574 {
1575         struct net *net = sock_net(skb->sk);
1576         int h, s_h;
1577         int idx = 0, s_idx;
1578         struct net_device *dev;
1579         struct hlist_head *head;
1580         struct nlattr *tb[IFLA_MAX+1];
1581         u32 ext_filter_mask = 0;
1582         const struct rtnl_link_ops *kind_ops = NULL;
1583         unsigned int flags = NLM_F_MULTI;
1584         int master_idx = 0;
1585         int err;
1586         int hdrlen;
1587
1588         s_h = cb->args[0];
1589         s_idx = cb->args[1];
1590
1591         cb->seq = net->dev_base_seq;
1592
1593         /* A hack to preserve kernel<->userspace interface.
1594          * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1595          * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1596          * what iproute2 < v3.9.0 used.
1597          * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1598          * attribute, its netlink message is shorter than struct ifinfomsg.
1599          */
1600         hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1601                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1602
1603         if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1604                         ifla_policy, NULL) >= 0) {
1605                 if (tb[IFLA_EXT_MASK])
1606                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1607
1608                 if (tb[IFLA_MASTER])
1609                         master_idx = nla_get_u32(tb[IFLA_MASTER]);
1610
1611                 if (tb[IFLA_LINKINFO])
1612                         kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1613
1614                 if (master_idx || kind_ops)
1615                         flags |= NLM_F_DUMP_FILTERED;
1616         }
1617
1618         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1619                 idx = 0;
1620                 head = &net->dev_index_head[h];
1621                 hlist_for_each_entry(dev, head, index_hlist) {
1622                         if (link_dump_filtered(dev, master_idx, kind_ops))
1623                                 goto cont;
1624                         if (idx < s_idx)
1625                                 goto cont;
1626                         err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1627                                                NETLINK_CB(cb->skb).portid,
1628                                                cb->nlh->nlmsg_seq, 0,
1629                                                flags,
1630                                                ext_filter_mask);
1631
1632                         if (err < 0) {
1633                                 if (likely(skb->len))
1634                                         goto out;
1635
1636                                 goto out_err;
1637                         }
1638
1639                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1640 cont:
1641                         idx++;
1642                 }
1643         }
1644 out:
1645         err = skb->len;
1646 out_err:
1647         cb->args[1] = idx;
1648         cb->args[0] = h;
1649
1650         return err;
1651 }
1652
1653 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1654                         struct netlink_ext_ack *exterr)
1655 {
1656         return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1657 }
1658 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1659
1660 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1661 {
1662         struct net *net;
1663         /* Examine the link attributes and figure out which
1664          * network namespace we are talking about.
1665          */
1666         if (tb[IFLA_NET_NS_PID])
1667                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1668         else if (tb[IFLA_NET_NS_FD])
1669                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1670         else
1671                 net = get_net(src_net);
1672         return net;
1673 }
1674 EXPORT_SYMBOL(rtnl_link_get_net);
1675
1676 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1677 {
1678         if (dev) {
1679                 if (tb[IFLA_ADDRESS] &&
1680                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1681                         return -EINVAL;
1682
1683                 if (tb[IFLA_BROADCAST] &&
1684                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1685                         return -EINVAL;
1686         }
1687
1688         if (tb[IFLA_AF_SPEC]) {
1689                 struct nlattr *af;
1690                 int rem, err;
1691
1692                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1693                         const struct rtnl_af_ops *af_ops;
1694
1695                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1696                                 return -EAFNOSUPPORT;
1697
1698                         if (!af_ops->set_link_af)
1699                                 return -EOPNOTSUPP;
1700
1701                         if (af_ops->validate_link_af) {
1702                                 err = af_ops->validate_link_af(dev, af);
1703                                 if (err < 0)
1704                                         return err;
1705                         }
1706                 }
1707         }
1708
1709         return 0;
1710 }
1711
1712 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1713                                   int guid_type)
1714 {
1715         const struct net_device_ops *ops = dev->netdev_ops;
1716
1717         return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1718 }
1719
1720 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1721 {
1722         if (dev->type != ARPHRD_INFINIBAND)
1723                 return -EOPNOTSUPP;
1724
1725         return handle_infiniband_guid(dev, ivt, guid_type);
1726 }
1727
1728 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1729 {
1730         const struct net_device_ops *ops = dev->netdev_ops;
1731         int err = -EINVAL;
1732
1733         if (tb[IFLA_VF_MAC]) {
1734                 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1735
1736                 err = -EOPNOTSUPP;
1737                 if (ops->ndo_set_vf_mac)
1738                         err = ops->ndo_set_vf_mac(dev, ivm->vf,
1739                                                   ivm->mac);
1740                 if (err < 0)
1741                         return err;
1742         }
1743
1744         if (tb[IFLA_VF_VLAN]) {
1745                 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1746
1747                 err = -EOPNOTSUPP;
1748                 if (ops->ndo_set_vf_vlan)
1749                         err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1750                                                    ivv->qos,
1751                                                    htons(ETH_P_8021Q));
1752                 if (err < 0)
1753                         return err;
1754         }
1755
1756         if (tb[IFLA_VF_VLAN_LIST]) {
1757                 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1758                 struct nlattr *attr;
1759                 int rem, len = 0;
1760
1761                 err = -EOPNOTSUPP;
1762                 if (!ops->ndo_set_vf_vlan)
1763                         return err;
1764
1765                 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1766                         if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1767                             nla_len(attr) < NLA_HDRLEN) {
1768                                 return -EINVAL;
1769                         }
1770                         if (len >= MAX_VLAN_LIST_LEN)
1771                                 return -EOPNOTSUPP;
1772                         ivvl[len] = nla_data(attr);
1773
1774                         len++;
1775                 }
1776                 if (len == 0)
1777                         return -EINVAL;
1778
1779                 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1780                                            ivvl[0]->qos, ivvl[0]->vlan_proto);
1781                 if (err < 0)
1782                         return err;
1783         }
1784
1785         if (tb[IFLA_VF_TX_RATE]) {
1786                 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1787                 struct ifla_vf_info ivf;
1788
1789                 err = -EOPNOTSUPP;
1790                 if (ops->ndo_get_vf_config)
1791                         err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1792                 if (err < 0)
1793                         return err;
1794
1795                 err = -EOPNOTSUPP;
1796                 if (ops->ndo_set_vf_rate)
1797                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1798                                                    ivf.min_tx_rate,
1799                                                    ivt->rate);
1800                 if (err < 0)
1801                         return err;
1802         }
1803
1804         if (tb[IFLA_VF_RATE]) {
1805                 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1806
1807                 err = -EOPNOTSUPP;
1808                 if (ops->ndo_set_vf_rate)
1809                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1810                                                    ivt->min_tx_rate,
1811                                                    ivt->max_tx_rate);
1812                 if (err < 0)
1813                         return err;
1814         }
1815
1816         if (tb[IFLA_VF_SPOOFCHK]) {
1817                 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1818
1819                 err = -EOPNOTSUPP;
1820                 if (ops->ndo_set_vf_spoofchk)
1821                         err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1822                                                        ivs->setting);
1823                 if (err < 0)
1824                         return err;
1825         }
1826
1827         if (tb[IFLA_VF_LINK_STATE]) {
1828                 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1829
1830                 err = -EOPNOTSUPP;
1831                 if (ops->ndo_set_vf_link_state)
1832                         err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1833                                                          ivl->link_state);
1834                 if (err < 0)
1835                         return err;
1836         }
1837
1838         if (tb[IFLA_VF_RSS_QUERY_EN]) {
1839                 struct ifla_vf_rss_query_en *ivrssq_en;
1840
1841                 err = -EOPNOTSUPP;
1842                 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1843                 if (ops->ndo_set_vf_rss_query_en)
1844                         err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1845                                                            ivrssq_en->setting);
1846                 if (err < 0)
1847                         return err;
1848         }
1849
1850         if (tb[IFLA_VF_TRUST]) {
1851                 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1852
1853                 err = -EOPNOTSUPP;
1854                 if (ops->ndo_set_vf_trust)
1855                         err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1856                 if (err < 0)
1857                         return err;
1858         }
1859
1860         if (tb[IFLA_VF_IB_NODE_GUID]) {
1861                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1862
1863                 if (!ops->ndo_set_vf_guid)
1864                         return -EOPNOTSUPP;
1865
1866                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1867         }
1868
1869         if (tb[IFLA_VF_IB_PORT_GUID]) {
1870                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1871
1872                 if (!ops->ndo_set_vf_guid)
1873                         return -EOPNOTSUPP;
1874
1875                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1876         }
1877
1878         return err;
1879 }
1880
1881 static int do_set_master(struct net_device *dev, int ifindex)
1882 {
1883         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1884         const struct net_device_ops *ops;
1885         int err;
1886
1887         if (upper_dev) {
1888                 if (upper_dev->ifindex == ifindex)
1889                         return 0;
1890                 ops = upper_dev->netdev_ops;
1891                 if (ops->ndo_del_slave) {
1892                         err = ops->ndo_del_slave(upper_dev, dev);
1893                         if (err)
1894                                 return err;
1895                 } else {
1896                         return -EOPNOTSUPP;
1897                 }
1898         }
1899
1900         if (ifindex) {
1901                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1902                 if (!upper_dev)
1903                         return -EINVAL;
1904                 ops = upper_dev->netdev_ops;
1905                 if (ops->ndo_add_slave) {
1906                         err = ops->ndo_add_slave(upper_dev, dev);
1907                         if (err)
1908                                 return err;
1909                 } else {
1910                         return -EOPNOTSUPP;
1911                 }
1912         }
1913         return 0;
1914 }
1915
1916 #define DO_SETLINK_MODIFIED     0x01
1917 /* notify flag means notify + modified. */
1918 #define DO_SETLINK_NOTIFY       0x03
1919 static int do_setlink(const struct sk_buff *skb,
1920                       struct net_device *dev, struct ifinfomsg *ifm,
1921                       struct netlink_ext_ack *extack,
1922                       struct nlattr **tb, char *ifname, int status)
1923 {
1924         const struct net_device_ops *ops = dev->netdev_ops;
1925         int err;
1926
1927         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1928                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1929                 if (IS_ERR(net)) {
1930                         err = PTR_ERR(net);
1931                         goto errout;
1932                 }
1933                 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1934                         put_net(net);
1935                         err = -EPERM;
1936                         goto errout;
1937                 }
1938                 err = dev_change_net_namespace(dev, net, ifname);
1939                 put_net(net);
1940                 if (err)
1941                         goto errout;
1942                 status |= DO_SETLINK_MODIFIED;
1943         }
1944
1945         if (tb[IFLA_MAP]) {
1946                 struct rtnl_link_ifmap *u_map;
1947                 struct ifmap k_map;
1948
1949                 if (!ops->ndo_set_config) {
1950                         err = -EOPNOTSUPP;
1951                         goto errout;
1952                 }
1953
1954                 if (!netif_device_present(dev)) {
1955                         err = -ENODEV;
1956                         goto errout;
1957                 }
1958
1959                 u_map = nla_data(tb[IFLA_MAP]);
1960                 k_map.mem_start = (unsigned long) u_map->mem_start;
1961                 k_map.mem_end = (unsigned long) u_map->mem_end;
1962                 k_map.base_addr = (unsigned short) u_map->base_addr;
1963                 k_map.irq = (unsigned char) u_map->irq;
1964                 k_map.dma = (unsigned char) u_map->dma;
1965                 k_map.port = (unsigned char) u_map->port;
1966
1967                 err = ops->ndo_set_config(dev, &k_map);
1968                 if (err < 0)
1969                         goto errout;
1970
1971                 status |= DO_SETLINK_NOTIFY;
1972         }
1973
1974         if (tb[IFLA_ADDRESS]) {
1975                 struct sockaddr *sa;
1976                 int len;
1977
1978                 len = sizeof(sa_family_t) + dev->addr_len;
1979                 sa = kmalloc(len, GFP_KERNEL);
1980                 if (!sa) {
1981                         err = -ENOMEM;
1982                         goto errout;
1983                 }
1984                 sa->sa_family = dev->type;
1985                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1986                        dev->addr_len);
1987                 err = dev_set_mac_address(dev, sa);
1988                 kfree(sa);
1989                 if (err)
1990                         goto errout;
1991                 status |= DO_SETLINK_MODIFIED;
1992         }
1993
1994         if (tb[IFLA_MTU]) {
1995                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1996                 if (err < 0)
1997                         goto errout;
1998                 status |= DO_SETLINK_MODIFIED;
1999         }
2000
2001         if (tb[IFLA_GROUP]) {
2002                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2003                 status |= DO_SETLINK_NOTIFY;
2004         }
2005
2006         /*
2007          * Interface selected by interface index but interface
2008          * name provided implies that a name change has been
2009          * requested.
2010          */
2011         if (ifm->ifi_index > 0 && ifname[0]) {
2012                 err = dev_change_name(dev, ifname);
2013                 if (err < 0)
2014                         goto errout;
2015                 status |= DO_SETLINK_MODIFIED;
2016         }
2017
2018         if (tb[IFLA_IFALIAS]) {
2019                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2020                                     nla_len(tb[IFLA_IFALIAS]));
2021                 if (err < 0)
2022                         goto errout;
2023                 status |= DO_SETLINK_NOTIFY;
2024         }
2025
2026         if (tb[IFLA_BROADCAST]) {
2027                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2028                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2029         }
2030
2031         if (ifm->ifi_flags || ifm->ifi_change) {
2032                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2033                 if (err < 0)
2034                         goto errout;
2035         }
2036
2037         if (tb[IFLA_MASTER]) {
2038                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2039                 if (err)
2040                         goto errout;
2041                 status |= DO_SETLINK_MODIFIED;
2042         }
2043
2044         if (tb[IFLA_CARRIER]) {
2045                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2046                 if (err)
2047                         goto errout;
2048                 status |= DO_SETLINK_MODIFIED;
2049         }
2050
2051         if (tb[IFLA_TXQLEN]) {
2052                 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
2053                 unsigned long orig_len = dev->tx_queue_len;
2054
2055                 if (dev->tx_queue_len ^ value) {
2056                         dev->tx_queue_len = value;
2057                         err = call_netdevice_notifiers(
2058                               NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2059                         err = notifier_to_errno(err);
2060                         if (err) {
2061                                 dev->tx_queue_len = orig_len;
2062                                 goto errout;
2063                         }
2064                         status |= DO_SETLINK_NOTIFY;
2065                 }
2066         }
2067
2068         if (tb[IFLA_OPERSTATE])
2069                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2070
2071         if (tb[IFLA_LINKMODE]) {
2072                 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2073
2074                 write_lock_bh(&dev_base_lock);
2075                 if (dev->link_mode ^ value)
2076                         status |= DO_SETLINK_NOTIFY;
2077                 dev->link_mode = value;
2078                 write_unlock_bh(&dev_base_lock);
2079         }
2080
2081         if (tb[IFLA_VFINFO_LIST]) {
2082                 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2083                 struct nlattr *attr;
2084                 int rem;
2085
2086                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2087                         if (nla_type(attr) != IFLA_VF_INFO ||
2088                             nla_len(attr) < NLA_HDRLEN) {
2089                                 err = -EINVAL;
2090                                 goto errout;
2091                         }
2092                         err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2093                                                ifla_vf_policy, NULL);
2094                         if (err < 0)
2095                                 goto errout;
2096                         err = do_setvfinfo(dev, vfinfo);
2097                         if (err < 0)
2098                                 goto errout;
2099                         status |= DO_SETLINK_NOTIFY;
2100                 }
2101         }
2102         err = 0;
2103
2104         if (tb[IFLA_VF_PORTS]) {
2105                 struct nlattr *port[IFLA_PORT_MAX+1];
2106                 struct nlattr *attr;
2107                 int vf;
2108                 int rem;
2109
2110                 err = -EOPNOTSUPP;
2111                 if (!ops->ndo_set_vf_port)
2112                         goto errout;
2113
2114                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2115                         if (nla_type(attr) != IFLA_VF_PORT ||
2116                             nla_len(attr) < NLA_HDRLEN) {
2117                                 err = -EINVAL;
2118                                 goto errout;
2119                         }
2120                         err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2121                                                ifla_port_policy, NULL);
2122                         if (err < 0)
2123                                 goto errout;
2124                         if (!port[IFLA_PORT_VF]) {
2125                                 err = -EOPNOTSUPP;
2126                                 goto errout;
2127                         }
2128                         vf = nla_get_u32(port[IFLA_PORT_VF]);
2129                         err = ops->ndo_set_vf_port(dev, vf, port);
2130                         if (err < 0)
2131                                 goto errout;
2132                         status |= DO_SETLINK_NOTIFY;
2133                 }
2134         }
2135         err = 0;
2136
2137         if (tb[IFLA_PORT_SELF]) {
2138                 struct nlattr *port[IFLA_PORT_MAX+1];
2139
2140                 err = nla_parse_nested(port, IFLA_PORT_MAX,
2141                                        tb[IFLA_PORT_SELF], ifla_port_policy,
2142                                        NULL);
2143                 if (err < 0)
2144                         goto errout;
2145
2146                 err = -EOPNOTSUPP;
2147                 if (ops->ndo_set_vf_port)
2148                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2149                 if (err < 0)
2150                         goto errout;
2151                 status |= DO_SETLINK_NOTIFY;
2152         }
2153
2154         if (tb[IFLA_AF_SPEC]) {
2155                 struct nlattr *af;
2156                 int rem;
2157
2158                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2159                         const struct rtnl_af_ops *af_ops;
2160
2161                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2162                                 BUG();
2163
2164                         err = af_ops->set_link_af(dev, af);
2165                         if (err < 0)
2166                                 goto errout;
2167
2168                         status |= DO_SETLINK_NOTIFY;
2169                 }
2170         }
2171         err = 0;
2172
2173         if (tb[IFLA_PROTO_DOWN]) {
2174                 err = dev_change_proto_down(dev,
2175                                             nla_get_u8(tb[IFLA_PROTO_DOWN]));
2176                 if (err)
2177                         goto errout;
2178                 status |= DO_SETLINK_NOTIFY;
2179         }
2180
2181         if (tb[IFLA_XDP]) {
2182                 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2183                 u32 xdp_flags = 0;
2184
2185                 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2186                                        ifla_xdp_policy, NULL);
2187                 if (err < 0)
2188                         goto errout;
2189
2190                 if (xdp[IFLA_XDP_ATTACHED]) {
2191                         err = -EINVAL;
2192                         goto errout;
2193                 }
2194
2195                 if (xdp[IFLA_XDP_FLAGS]) {
2196                         xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2197                         if (xdp_flags & ~XDP_FLAGS_MASK) {
2198                                 err = -EINVAL;
2199                                 goto errout;
2200                         }
2201                         if ((xdp_flags & XDP_FLAGS_SKB_MODE) &&
2202                             (xdp_flags & XDP_FLAGS_DRV_MODE)) {
2203                                 err = -EINVAL;
2204                                 goto errout;
2205                         }
2206                 }
2207
2208                 if (xdp[IFLA_XDP_FD]) {
2209                         err = dev_change_xdp_fd(dev, extack,
2210                                                 nla_get_s32(xdp[IFLA_XDP_FD]),
2211                                                 xdp_flags);
2212                         if (err)
2213                                 goto errout;
2214                         status |= DO_SETLINK_NOTIFY;
2215                 }
2216         }
2217
2218 errout:
2219         if (status & DO_SETLINK_MODIFIED) {
2220                 if (status & DO_SETLINK_NOTIFY)
2221                         netdev_state_change(dev);
2222
2223                 if (err < 0)
2224                         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",
2225                                              dev->name);
2226         }
2227
2228         return err;
2229 }
2230
2231 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2232                         struct netlink_ext_ack *extack)
2233 {
2234         struct net *net = sock_net(skb->sk);
2235         struct ifinfomsg *ifm;
2236         struct net_device *dev;
2237         int err;
2238         struct nlattr *tb[IFLA_MAX+1];
2239         char ifname[IFNAMSIZ];
2240
2241         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2242                           extack);
2243         if (err < 0)
2244                 goto errout;
2245
2246         if (tb[IFLA_IFNAME])
2247                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2248         else
2249                 ifname[0] = '\0';
2250
2251         err = -EINVAL;
2252         ifm = nlmsg_data(nlh);
2253         if (ifm->ifi_index > 0)
2254                 dev = __dev_get_by_index(net, ifm->ifi_index);
2255         else if (tb[IFLA_IFNAME])
2256                 dev = __dev_get_by_name(net, ifname);
2257         else
2258                 goto errout;
2259
2260         if (dev == NULL) {
2261                 err = -ENODEV;
2262                 goto errout;
2263         }
2264
2265         err = validate_linkmsg(dev, tb);
2266         if (err < 0)
2267                 goto errout;
2268
2269         err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2270 errout:
2271         return err;
2272 }
2273
2274 static int rtnl_group_dellink(const struct net *net, int group)
2275 {
2276         struct net_device *dev, *aux;
2277         LIST_HEAD(list_kill);
2278         bool found = false;
2279
2280         if (!group)
2281                 return -EPERM;
2282
2283         for_each_netdev(net, dev) {
2284                 if (dev->group == group) {
2285                         const struct rtnl_link_ops *ops;
2286
2287                         found = true;
2288                         ops = dev->rtnl_link_ops;
2289                         if (!ops || !ops->dellink)
2290                                 return -EOPNOTSUPP;
2291                 }
2292         }
2293
2294         if (!found)
2295                 return -ENODEV;
2296
2297         for_each_netdev_safe(net, dev, aux) {
2298                 if (dev->group == group) {
2299                         const struct rtnl_link_ops *ops;
2300
2301                         ops = dev->rtnl_link_ops;
2302                         ops->dellink(dev, &list_kill);
2303                 }
2304         }
2305         unregister_netdevice_many(&list_kill);
2306
2307         return 0;
2308 }
2309
2310 int rtnl_delete_link(struct net_device *dev)
2311 {
2312         const struct rtnl_link_ops *ops;
2313         LIST_HEAD(list_kill);
2314
2315         ops = dev->rtnl_link_ops;
2316         if (!ops || !ops->dellink)
2317                 return -EOPNOTSUPP;
2318
2319         ops->dellink(dev, &list_kill);
2320         unregister_netdevice_many(&list_kill);
2321
2322         return 0;
2323 }
2324 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2325
2326 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2327                         struct netlink_ext_ack *extack)
2328 {
2329         struct net *net = sock_net(skb->sk);
2330         struct net_device *dev;
2331         struct ifinfomsg *ifm;
2332         char ifname[IFNAMSIZ];
2333         struct nlattr *tb[IFLA_MAX+1];
2334         int err;
2335
2336         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2337         if (err < 0)
2338                 return err;
2339
2340         if (tb[IFLA_IFNAME])
2341                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2342
2343         ifm = nlmsg_data(nlh);
2344         if (ifm->ifi_index > 0)
2345                 dev = __dev_get_by_index(net, ifm->ifi_index);
2346         else if (tb[IFLA_IFNAME])
2347                 dev = __dev_get_by_name(net, ifname);
2348         else if (tb[IFLA_GROUP])
2349                 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2350         else
2351                 return -EINVAL;
2352
2353         if (!dev)
2354                 return -ENODEV;
2355
2356         return rtnl_delete_link(dev);
2357 }
2358
2359 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2360 {
2361         unsigned int old_flags;
2362         int err;
2363
2364         old_flags = dev->flags;
2365         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2366                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2367                 if (err < 0)
2368                         return err;
2369         }
2370
2371         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2372
2373         __dev_notify_flags(dev, old_flags, ~0U);
2374         return 0;
2375 }
2376 EXPORT_SYMBOL(rtnl_configure_link);
2377
2378 struct net_device *rtnl_create_link(struct net *net,
2379         const char *ifname, unsigned char name_assign_type,
2380         const struct rtnl_link_ops *ops, struct nlattr *tb[])
2381 {
2382         struct net_device *dev;
2383         unsigned int num_tx_queues = 1;
2384         unsigned int num_rx_queues = 1;
2385
2386         if (tb[IFLA_NUM_TX_QUEUES])
2387                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2388         else if (ops->get_num_tx_queues)
2389                 num_tx_queues = ops->get_num_tx_queues();
2390
2391         if (tb[IFLA_NUM_RX_QUEUES])
2392                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2393         else if (ops->get_num_rx_queues)
2394                 num_rx_queues = ops->get_num_rx_queues();
2395
2396         dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2397                                ops->setup, num_tx_queues, num_rx_queues);
2398         if (!dev)
2399                 return ERR_PTR(-ENOMEM);
2400
2401         dev_net_set(dev, net);
2402         dev->rtnl_link_ops = ops;
2403         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2404
2405         if (tb[IFLA_MTU])
2406                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2407         if (tb[IFLA_ADDRESS]) {
2408                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2409                                 nla_len(tb[IFLA_ADDRESS]));
2410                 dev->addr_assign_type = NET_ADDR_SET;
2411         }
2412         if (tb[IFLA_BROADCAST])
2413                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2414                                 nla_len(tb[IFLA_BROADCAST]));
2415         if (tb[IFLA_TXQLEN])
2416                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2417         if (tb[IFLA_OPERSTATE])
2418                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2419         if (tb[IFLA_LINKMODE])
2420                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2421         if (tb[IFLA_GROUP])
2422                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2423
2424         return dev;
2425 }
2426 EXPORT_SYMBOL(rtnl_create_link);
2427
2428 static int rtnl_group_changelink(const struct sk_buff *skb,
2429                 struct net *net, int group,
2430                 struct ifinfomsg *ifm,
2431                 struct netlink_ext_ack *extack,
2432                 struct nlattr **tb)
2433 {
2434         struct net_device *dev, *aux;
2435         int err;
2436
2437         for_each_netdev_safe(net, dev, aux) {
2438                 if (dev->group == group) {
2439                         err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2440                         if (err < 0)
2441                                 return err;
2442                 }
2443         }
2444
2445         return 0;
2446 }
2447
2448 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2449                         struct netlink_ext_ack *extack)
2450 {
2451         struct net *net = sock_net(skb->sk);
2452         const struct rtnl_link_ops *ops;
2453         const struct rtnl_link_ops *m_ops = NULL;
2454         struct net_device *dev;
2455         struct net_device *master_dev = NULL;
2456         struct ifinfomsg *ifm;
2457         char kind[MODULE_NAME_LEN];
2458         char ifname[IFNAMSIZ];
2459         struct nlattr *tb[IFLA_MAX+1];
2460         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2461         unsigned char name_assign_type = NET_NAME_USER;
2462         int err;
2463
2464 #ifdef CONFIG_MODULES
2465 replay:
2466 #endif
2467         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2468         if (err < 0)
2469                 return err;
2470
2471         if (tb[IFLA_IFNAME])
2472                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2473         else
2474                 ifname[0] = '\0';
2475
2476         ifm = nlmsg_data(nlh);
2477         if (ifm->ifi_index > 0)
2478                 dev = __dev_get_by_index(net, ifm->ifi_index);
2479         else {
2480                 if (ifname[0])
2481                         dev = __dev_get_by_name(net, ifname);
2482                 else
2483                         dev = NULL;
2484         }
2485
2486         if (dev) {
2487                 master_dev = netdev_master_upper_dev_get(dev);
2488                 if (master_dev)
2489                         m_ops = master_dev->rtnl_link_ops;
2490         }
2491
2492         err = validate_linkmsg(dev, tb);
2493         if (err < 0)
2494                 return err;
2495
2496         if (tb[IFLA_LINKINFO]) {
2497                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2498                                        tb[IFLA_LINKINFO], ifla_info_policy,
2499                                        NULL);
2500                 if (err < 0)
2501                         return err;
2502         } else
2503                 memset(linkinfo, 0, sizeof(linkinfo));
2504
2505         if (linkinfo[IFLA_INFO_KIND]) {
2506                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2507                 ops = rtnl_link_ops_get(kind);
2508         } else {
2509                 kind[0] = '\0';
2510                 ops = NULL;
2511         }
2512
2513         if (1) {
2514                 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2515                 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2516                 struct nlattr **data = NULL;
2517                 struct nlattr **slave_data = NULL;
2518                 struct net *dest_net, *link_net = NULL;
2519
2520                 if (ops) {
2521                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2522                                 err = nla_parse_nested(attr, ops->maxtype,
2523                                                        linkinfo[IFLA_INFO_DATA],
2524                                                        ops->policy, NULL);
2525                                 if (err < 0)
2526                                         return err;
2527                                 data = attr;
2528                         }
2529                         if (ops->validate) {
2530                                 err = ops->validate(tb, data);
2531                                 if (err < 0)
2532                                         return err;
2533                         }
2534                 }
2535
2536                 if (m_ops) {
2537                         if (m_ops->slave_maxtype &&
2538                             linkinfo[IFLA_INFO_SLAVE_DATA]) {
2539                                 err = nla_parse_nested(slave_attr,
2540                                                        m_ops->slave_maxtype,
2541                                                        linkinfo[IFLA_INFO_SLAVE_DATA],
2542                                                        m_ops->slave_policy,
2543                                                        NULL);
2544                                 if (err < 0)
2545                                         return err;
2546                                 slave_data = slave_attr;
2547                         }
2548                         if (m_ops->slave_validate) {
2549                                 err = m_ops->slave_validate(tb, slave_data);
2550                                 if (err < 0)
2551                                         return err;
2552                         }
2553                 }
2554
2555                 if (dev) {
2556                         int status = 0;
2557
2558                         if (nlh->nlmsg_flags & NLM_F_EXCL)
2559                                 return -EEXIST;
2560                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
2561                                 return -EOPNOTSUPP;
2562
2563                         if (linkinfo[IFLA_INFO_DATA]) {
2564                                 if (!ops || ops != dev->rtnl_link_ops ||
2565                                     !ops->changelink)
2566                                         return -EOPNOTSUPP;
2567
2568                                 err = ops->changelink(dev, tb, data);
2569                                 if (err < 0)
2570                                         return err;
2571                                 status |= DO_SETLINK_NOTIFY;
2572                         }
2573
2574                         if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2575                                 if (!m_ops || !m_ops->slave_changelink)
2576                                         return -EOPNOTSUPP;
2577
2578                                 err = m_ops->slave_changelink(master_dev, dev,
2579                                                               tb, slave_data);
2580                                 if (err < 0)
2581                                         return err;
2582                                 status |= DO_SETLINK_NOTIFY;
2583                         }
2584
2585                         return do_setlink(skb, dev, ifm, extack, tb, ifname,
2586                                           status);
2587                 }
2588
2589                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2590                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2591                                 return rtnl_group_changelink(skb, net,
2592                                                 nla_get_u32(tb[IFLA_GROUP]),
2593                                                 ifm, extack, tb);
2594                         return -ENODEV;
2595                 }
2596
2597                 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
2598                         return -EOPNOTSUPP;
2599
2600                 if (!ops) {
2601 #ifdef CONFIG_MODULES
2602                         if (kind[0]) {
2603                                 __rtnl_unlock();
2604                                 request_module("rtnl-link-%s", kind);
2605                                 rtnl_lock();
2606                                 ops = rtnl_link_ops_get(kind);
2607                                 if (ops)
2608                                         goto replay;
2609                         }
2610 #endif
2611                         return -EOPNOTSUPP;
2612                 }
2613
2614                 if (!ops->setup)
2615                         return -EOPNOTSUPP;
2616
2617                 if (!ifname[0]) {
2618                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2619                         name_assign_type = NET_NAME_ENUM;
2620                 }
2621
2622                 dest_net = rtnl_link_get_net(net, tb);
2623                 if (IS_ERR(dest_net))
2624                         return PTR_ERR(dest_net);
2625
2626                 err = -EPERM;
2627                 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2628                         goto out;
2629
2630                 if (tb[IFLA_LINK_NETNSID]) {
2631                         int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2632
2633                         link_net = get_net_ns_by_id(dest_net, id);
2634                         if (!link_net) {
2635                                 err =  -EINVAL;
2636                                 goto out;
2637                         }
2638                         err = -EPERM;
2639                         if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2640                                 goto out;
2641                 }
2642
2643                 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2644                                        name_assign_type, ops, tb);
2645                 if (IS_ERR(dev)) {
2646                         err = PTR_ERR(dev);
2647                         goto out;
2648                 }
2649
2650                 dev->ifindex = ifm->ifi_index;
2651
2652                 if (ops->newlink) {
2653                         err = ops->newlink(link_net ? : net, dev, tb, data);
2654                         /* Drivers should call free_netdev() in ->destructor
2655                          * and unregister it on failure after registration
2656                          * so that device could be finally freed in rtnl_unlock.
2657                          */
2658                         if (err < 0) {
2659                                 /* If device is not registered at all, free it now */
2660                                 if (dev->reg_state == NETREG_UNINITIALIZED)
2661                                         free_netdev(dev);
2662                                 goto out;
2663                         }
2664                 } else {
2665                         err = register_netdevice(dev);
2666                         if (err < 0) {
2667                                 free_netdev(dev);
2668                                 goto out;
2669                         }
2670                 }
2671                 err = rtnl_configure_link(dev, ifm);
2672                 if (err < 0)
2673                         goto out_unregister;
2674                 if (link_net) {
2675                         err = dev_change_net_namespace(dev, dest_net, ifname);
2676                         if (err < 0)
2677                                 goto out_unregister;
2678                 }
2679                 if (tb[IFLA_MASTER]) {
2680                         err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2681                         if (err)
2682                                 goto out_unregister;
2683                 }
2684 out:
2685                 if (link_net)
2686                         put_net(link_net);
2687                 put_net(dest_net);
2688                 return err;
2689 out_unregister:
2690                 if (ops->newlink) {
2691                         LIST_HEAD(list_kill);
2692
2693                         ops->dellink(dev, &list_kill);
2694                         unregister_netdevice_many(&list_kill);
2695                 } else {
2696                         unregister_netdevice(dev);
2697                 }
2698                 goto out;
2699         }
2700 }
2701
2702 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2703                         struct netlink_ext_ack *extack)
2704 {
2705         struct net *net = sock_net(skb->sk);
2706         struct ifinfomsg *ifm;
2707         char ifname[IFNAMSIZ];
2708         struct nlattr *tb[IFLA_MAX+1];
2709         struct net_device *dev = NULL;
2710         struct sk_buff *nskb;
2711         int err;
2712         u32 ext_filter_mask = 0;
2713
2714         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2715         if (err < 0)
2716                 return err;
2717
2718         if (tb[IFLA_IFNAME])
2719                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2720
2721         if (tb[IFLA_EXT_MASK])
2722                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2723
2724         ifm = nlmsg_data(nlh);
2725         if (ifm->ifi_index > 0)
2726                 dev = __dev_get_by_index(net, ifm->ifi_index);
2727         else if (tb[IFLA_IFNAME])
2728                 dev = __dev_get_by_name(net, ifname);
2729         else
2730                 return -EINVAL;
2731
2732         if (dev == NULL)
2733                 return -ENODEV;
2734
2735         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2736         if (nskb == NULL)
2737                 return -ENOBUFS;
2738
2739         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2740                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2741         if (err < 0) {
2742                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2743                 WARN_ON(err == -EMSGSIZE);
2744                 kfree_skb(nskb);
2745         } else
2746                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2747
2748         return err;
2749 }
2750
2751 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2752 {
2753         struct net *net = sock_net(skb->sk);
2754         struct net_device *dev;
2755         struct nlattr *tb[IFLA_MAX+1];
2756         u32 ext_filter_mask = 0;
2757         u16 min_ifinfo_dump_size = 0;
2758         int hdrlen;
2759
2760         /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2761         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2762                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2763
2764         if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
2765                 if (tb[IFLA_EXT_MASK])
2766                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2767         }
2768
2769         if (!ext_filter_mask)
2770                 return NLMSG_GOODSIZE;
2771         /*
2772          * traverse the list of net devices and compute the minimum
2773          * buffer size based upon the filter mask.
2774          */
2775         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2776                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2777                                              if_nlmsg_size(dev,
2778                                                            ext_filter_mask));
2779         }
2780
2781         return nlmsg_total_size(min_ifinfo_dump_size);
2782 }
2783
2784 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2785 {
2786         int idx;
2787         int s_idx = cb->family;
2788
2789         if (s_idx == 0)
2790                 s_idx = 1;
2791         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2792                 int type = cb->nlh->nlmsg_type-RTM_BASE;
2793                 if (idx < s_idx || idx == PF_PACKET)
2794                         continue;
2795                 if (rtnl_msg_handlers[idx] == NULL ||
2796                     rtnl_msg_handlers[idx][type].dumpit == NULL)
2797                         continue;
2798                 if (idx > s_idx) {
2799                         memset(&cb->args[0], 0, sizeof(cb->args));
2800                         cb->prev_seq = 0;
2801                         cb->seq = 0;
2802                 }
2803                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2804                         break;
2805         }
2806         cb->family = idx;
2807
2808         return skb->len;
2809 }
2810
2811 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2812                                        unsigned int change, gfp_t flags)
2813 {
2814         struct net *net = dev_net(dev);
2815         struct sk_buff *skb;
2816         int err = -ENOBUFS;
2817         size_t if_info_size;
2818
2819         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2820         if (skb == NULL)
2821                 goto errout;
2822
2823         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2824         if (err < 0) {
2825                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2826                 WARN_ON(err == -EMSGSIZE);
2827                 kfree_skb(skb);
2828                 goto errout;
2829         }
2830         return skb;
2831 errout:
2832         if (err < 0)
2833                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2834         return NULL;
2835 }
2836
2837 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2838 {
2839         struct net *net = dev_net(dev);
2840
2841         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2842 }
2843
2844 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2845                   gfp_t flags)
2846 {
2847         struct sk_buff *skb;
2848
2849         if (dev->reg_state != NETREG_REGISTERED)
2850                 return;
2851
2852         skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2853         if (skb)
2854                 rtmsg_ifinfo_send(skb, dev, flags);
2855 }
2856 EXPORT_SYMBOL(rtmsg_ifinfo);
2857
2858 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2859                                    struct net_device *dev,
2860                                    u8 *addr, u16 vid, u32 pid, u32 seq,
2861                                    int type, unsigned int flags,
2862                                    int nlflags, u16 ndm_state)
2863 {
2864         struct nlmsghdr *nlh;
2865         struct ndmsg *ndm;
2866
2867         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2868         if (!nlh)
2869                 return -EMSGSIZE;
2870
2871         ndm = nlmsg_data(nlh);
2872         ndm->ndm_family  = AF_BRIDGE;
2873         ndm->ndm_pad1    = 0;
2874         ndm->ndm_pad2    = 0;
2875         ndm->ndm_flags   = flags;
2876         ndm->ndm_type    = 0;
2877         ndm->ndm_ifindex = dev->ifindex;
2878         ndm->ndm_state   = ndm_state;
2879
2880         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2881                 goto nla_put_failure;
2882         if (vid)
2883                 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2884                         goto nla_put_failure;
2885
2886         nlmsg_end(skb, nlh);
2887         return 0;
2888
2889 nla_put_failure:
2890         nlmsg_cancel(skb, nlh);
2891         return -EMSGSIZE;
2892 }
2893
2894 static inline size_t rtnl_fdb_nlmsg_size(void)
2895 {
2896         return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2897                nla_total_size(ETH_ALEN) +       /* NDA_LLADDR */
2898                nla_total_size(sizeof(u16)) +    /* NDA_VLAN */
2899                0;
2900 }
2901
2902 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2903                             u16 ndm_state)
2904 {
2905         struct net *net = dev_net(dev);
2906         struct sk_buff *skb;
2907         int err = -ENOBUFS;
2908
2909         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2910         if (!skb)
2911                 goto errout;
2912
2913         err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2914                                       0, 0, type, NTF_SELF, 0, ndm_state);
2915         if (err < 0) {
2916                 kfree_skb(skb);
2917                 goto errout;
2918         }
2919
2920         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2921         return;
2922 errout:
2923         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2924 }
2925
2926 /**
2927  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2928  */
2929 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2930                      struct nlattr *tb[],
2931                      struct net_device *dev,
2932                      const unsigned char *addr, u16 vid,
2933                      u16 flags)
2934 {
2935         int err = -EINVAL;
2936
2937         /* If aging addresses are supported device will need to
2938          * implement its own handler for this.
2939          */
2940         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2941                 pr_info("%s: FDB only supports static addresses\n", dev->name);
2942                 return err;
2943         }
2944
2945         if (vid) {
2946                 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2947                 return err;
2948         }
2949
2950         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2951                 err = dev_uc_add_excl(dev, addr);
2952         else if (is_multicast_ether_addr(addr))
2953                 err = dev_mc_add_excl(dev, addr);
2954
2955         /* Only return duplicate errors if NLM_F_EXCL is set */
2956         if (err == -EEXIST && !(flags & NLM_F_EXCL))
2957                 err = 0;
2958
2959         return err;
2960 }
2961 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2962
2963 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2964 {
2965         u16 vid = 0;
2966
2967         if (vlan_attr) {
2968                 if (nla_len(vlan_attr) != sizeof(u16)) {
2969                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2970                         return -EINVAL;
2971                 }
2972
2973                 vid = nla_get_u16(vlan_attr);
2974
2975                 if (!vid || vid >= VLAN_VID_MASK) {
2976                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2977                                 vid);
2978                         return -EINVAL;
2979                 }
2980         }
2981         *p_vid = vid;
2982         return 0;
2983 }
2984
2985 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
2986                         struct netlink_ext_ack *extack)
2987 {
2988         struct net *net = sock_net(skb->sk);
2989         struct ndmsg *ndm;
2990         struct nlattr *tb[NDA_MAX+1];
2991         struct net_device *dev;
2992         u8 *addr;
2993         u16 vid;
2994         int err;
2995
2996         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
2997         if (err < 0)
2998                 return err;
2999
3000         ndm = nlmsg_data(nlh);
3001         if (ndm->ndm_ifindex == 0) {
3002                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3003                 return -EINVAL;
3004         }
3005
3006         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3007         if (dev == NULL) {
3008                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3009                 return -ENODEV;
3010         }
3011
3012         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3013                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3014                 return -EINVAL;
3015         }
3016
3017         addr = nla_data(tb[NDA_LLADDR]);
3018
3019         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3020         if (err)
3021                 return err;
3022
3023         err = -EOPNOTSUPP;
3024
3025         /* Support fdb on master device the net/bridge default case */
3026         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3027             (dev->priv_flags & IFF_BRIDGE_PORT)) {
3028                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3029                 const struct net_device_ops *ops = br_dev->netdev_ops;
3030
3031                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3032                                        nlh->nlmsg_flags);
3033                 if (err)
3034                         goto out;
3035                 else
3036                         ndm->ndm_flags &= ~NTF_MASTER;
3037         }
3038
3039         /* Embedded bridge, macvlan, and any other device support */
3040         if ((ndm->ndm_flags & NTF_SELF)) {
3041                 if (dev->netdev_ops->ndo_fdb_add)
3042                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3043                                                            vid,
3044                                                            nlh->nlmsg_flags);
3045                 else
3046                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3047                                                nlh->nlmsg_flags);
3048
3049                 if (!err) {
3050                         rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3051                                         ndm->ndm_state);
3052                         ndm->ndm_flags &= ~NTF_SELF;
3053                 }
3054         }
3055 out:
3056         return err;
3057 }
3058
3059 /**
3060  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3061  */
3062 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3063                      struct nlattr *tb[],
3064                      struct net_device *dev,
3065                      const unsigned char *addr, u16 vid)
3066 {
3067         int err = -EINVAL;
3068
3069         /* If aging addresses are supported device will need to
3070          * implement its own handler for this.
3071          */
3072         if (!(ndm->ndm_state & NUD_PERMANENT)) {
3073                 pr_info("%s: FDB only supports static addresses\n", dev->name);
3074                 return err;
3075         }
3076
3077         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3078                 err = dev_uc_del(dev, addr);
3079         else if (is_multicast_ether_addr(addr))
3080                 err = dev_mc_del(dev, addr);
3081
3082         return err;
3083 }
3084 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3085
3086 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3087                         struct netlink_ext_ack *extack)
3088 {
3089         struct net *net = sock_net(skb->sk);
3090         struct ndmsg *ndm;
3091         struct nlattr *tb[NDA_MAX+1];
3092         struct net_device *dev;
3093         int err = -EINVAL;
3094         __u8 *addr;
3095         u16 vid;
3096
3097         if (!netlink_capable(skb, CAP_NET_ADMIN))
3098                 return -EPERM;
3099
3100         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3101         if (err < 0)
3102                 return err;
3103
3104         ndm = nlmsg_data(nlh);
3105         if (ndm->ndm_ifindex == 0) {
3106                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3107                 return -EINVAL;
3108         }
3109
3110         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3111         if (dev == NULL) {
3112                 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3113                 return -ENODEV;
3114         }
3115
3116         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3117                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3118                 return -EINVAL;
3119         }
3120
3121         addr = nla_data(tb[NDA_LLADDR]);
3122
3123         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3124         if (err)
3125                 return err;
3126
3127         err = -EOPNOTSUPP;
3128
3129         /* Support fdb on master device the net/bridge default case */
3130         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3131             (dev->priv_flags & IFF_BRIDGE_PORT)) {
3132                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3133                 const struct net_device_ops *ops = br_dev->netdev_ops;
3134
3135                 if (ops->ndo_fdb_del)
3136                         err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3137
3138                 if (err)
3139                         goto out;
3140                 else
3141                         ndm->ndm_flags &= ~NTF_MASTER;
3142         }
3143
3144         /* Embedded bridge, macvlan, and any other device support */
3145         if (ndm->ndm_flags & NTF_SELF) {
3146                 if (dev->netdev_ops->ndo_fdb_del)
3147                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3148                                                            vid);
3149                 else
3150                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3151
3152                 if (!err) {
3153                         rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3154                                         ndm->ndm_state);
3155                         ndm->ndm_flags &= ~NTF_SELF;
3156                 }
3157         }
3158 out:
3159         return err;
3160 }
3161
3162 static int nlmsg_populate_fdb(struct sk_buff *skb,
3163                               struct netlink_callback *cb,
3164                               struct net_device *dev,
3165                               int *idx,
3166                               struct netdev_hw_addr_list *list)
3167 {
3168         struct netdev_hw_addr *ha;
3169         int err;
3170         u32 portid, seq;
3171
3172         portid = NETLINK_CB(cb->skb).portid;
3173         seq = cb->nlh->nlmsg_seq;
3174
3175         list_for_each_entry(ha, &list->list, list) {
3176                 if (*idx < cb->args[2])
3177                         goto skip;
3178
3179                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3180                                               portid, seq,
3181                                               RTM_NEWNEIGH, NTF_SELF,
3182                                               NLM_F_MULTI, NUD_PERMANENT);
3183                 if (err < 0)
3184                         return err;
3185 skip:
3186                 *idx += 1;
3187         }
3188         return 0;
3189 }
3190
3191 /**
3192  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3193  * @nlh: netlink message header
3194  * @dev: netdevice
3195  *
3196  * Default netdevice operation to dump the existing unicast address list.
3197  * Returns number of addresses from list put in skb.
3198  */
3199 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3200                       struct netlink_callback *cb,
3201                       struct net_device *dev,
3202                       struct net_device *filter_dev,
3203                       int *idx)
3204 {
3205         int err;
3206
3207         netif_addr_lock_bh(dev);
3208         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3209         if (err)
3210                 goto out;
3211         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3212 out:
3213         netif_addr_unlock_bh(dev);
3214         return err;
3215 }
3216 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3217
3218 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3219 {
3220         struct net_device *dev;
3221         struct nlattr *tb[IFLA_MAX+1];
3222         struct net_device *br_dev = NULL;
3223         const struct net_device_ops *ops = NULL;
3224         const struct net_device_ops *cops = NULL;
3225         struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3226         struct net *net = sock_net(skb->sk);
3227         struct hlist_head *head;
3228         int brport_idx = 0;
3229         int br_idx = 0;
3230         int h, s_h;
3231         int idx = 0, s_idx;
3232         int err = 0;
3233         int fidx = 0;
3234
3235         err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3236                           IFLA_MAX, ifla_policy, NULL);
3237         if (err < 0) {
3238                 return -EINVAL;
3239         } else if (err == 0) {
3240                 if (tb[IFLA_MASTER])
3241                         br_idx = nla_get_u32(tb[IFLA_MASTER]);
3242         }
3243
3244         brport_idx = ifm->ifi_index;
3245
3246         if (br_idx) {
3247                 br_dev = __dev_get_by_index(net, br_idx);
3248                 if (!br_dev)
3249                         return -ENODEV;
3250
3251                 ops = br_dev->netdev_ops;
3252         }
3253
3254         s_h = cb->args[0];
3255         s_idx = cb->args[1];
3256
3257         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3258                 idx = 0;
3259                 head = &net->dev_index_head[h];
3260                 hlist_for_each_entry(dev, head, index_hlist) {
3261
3262                         if (brport_idx && (dev->ifindex != brport_idx))
3263                                 continue;
3264
3265                         if (!br_idx) { /* user did not specify a specific bridge */
3266                                 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3267                                         br_dev = netdev_master_upper_dev_get(dev);
3268                                         cops = br_dev->netdev_ops;
3269                                 }
3270                         } else {
3271                                 if (dev != br_dev &&
3272                                     !(dev->priv_flags & IFF_BRIDGE_PORT))
3273                                         continue;
3274
3275                                 if (br_dev != netdev_master_upper_dev_get(dev) &&
3276                                     !(dev->priv_flags & IFF_EBRIDGE))
3277                                         continue;
3278                                 cops = ops;
3279                         }
3280
3281                         if (idx < s_idx)
3282                                 goto cont;
3283
3284                         if (dev->priv_flags & IFF_BRIDGE_PORT) {
3285                                 if (cops && cops->ndo_fdb_dump) {
3286                                         err = cops->ndo_fdb_dump(skb, cb,
3287                                                                 br_dev, dev,
3288                                                                 &fidx);
3289                                         if (err == -EMSGSIZE)
3290                                                 goto out;
3291                                 }
3292                         }
3293
3294                         if (dev->netdev_ops->ndo_fdb_dump)
3295                                 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3296                                                                     dev, NULL,
3297                                                                     &fidx);
3298                         else
3299                                 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3300                                                         &fidx);
3301                         if (err == -EMSGSIZE)
3302                                 goto out;
3303
3304                         cops = NULL;
3305
3306                         /* reset fdb offset to 0 for rest of the interfaces */
3307                         cb->args[2] = 0;
3308                         fidx = 0;
3309 cont:
3310                         idx++;
3311                 }
3312         }
3313
3314 out:
3315         cb->args[0] = h;
3316         cb->args[1] = idx;
3317         cb->args[2] = fidx;
3318
3319         return skb->len;
3320 }
3321
3322 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3323                                unsigned int attrnum, unsigned int flag)
3324 {
3325         if (mask & flag)
3326                 return nla_put_u8(skb, attrnum, !!(flags & flag));
3327         return 0;
3328 }
3329
3330 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3331                             struct net_device *dev, u16 mode,
3332                             u32 flags, u32 mask, int nlflags,
3333                             u32 filter_mask,
3334                             int (*vlan_fill)(struct sk_buff *skb,
3335                                              struct net_device *dev,
3336                                              u32 filter_mask))
3337 {
3338         struct nlmsghdr *nlh;
3339         struct ifinfomsg *ifm;
3340         struct nlattr *br_afspec;
3341         struct nlattr *protinfo;
3342         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3343         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3344         int err = 0;
3345
3346         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3347         if (nlh == NULL)
3348                 return -EMSGSIZE;
3349
3350         ifm = nlmsg_data(nlh);
3351         ifm->ifi_family = AF_BRIDGE;
3352         ifm->__ifi_pad = 0;
3353         ifm->ifi_type = dev->type;
3354         ifm->ifi_index = dev->ifindex;
3355         ifm->ifi_flags = dev_get_flags(dev);
3356         ifm->ifi_change = 0;
3357
3358
3359         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3360             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3361             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3362             (br_dev &&
3363              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3364             (dev->addr_len &&
3365              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3366             (dev->ifindex != dev_get_iflink(dev) &&
3367              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3368                 goto nla_put_failure;
3369
3370         br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3371         if (!br_afspec)
3372                 goto nla_put_failure;
3373
3374         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3375                 nla_nest_cancel(skb, br_afspec);
3376                 goto nla_put_failure;
3377         }
3378
3379         if (mode != BRIDGE_MODE_UNDEF) {
3380                 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3381                         nla_nest_cancel(skb, br_afspec);
3382                         goto nla_put_failure;
3383                 }
3384         }
3385         if (vlan_fill) {
3386                 err = vlan_fill(skb, dev, filter_mask);
3387                 if (err) {
3388                         nla_nest_cancel(skb, br_afspec);
3389                         goto nla_put_failure;
3390                 }
3391         }
3392         nla_nest_end(skb, br_afspec);
3393
3394         protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3395         if (!protinfo)
3396                 goto nla_put_failure;
3397
3398         if (brport_nla_put_flag(skb, flags, mask,
3399                                 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3400             brport_nla_put_flag(skb, flags, mask,
3401                                 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3402             brport_nla_put_flag(skb, flags, mask,
3403                                 IFLA_BRPORT_FAST_LEAVE,
3404                                 BR_MULTICAST_FAST_LEAVE) ||
3405             brport_nla_put_flag(skb, flags, mask,
3406                                 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3407             brport_nla_put_flag(skb, flags, mask,
3408                                 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3409             brport_nla_put_flag(skb, flags, mask,
3410                                 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3411             brport_nla_put_flag(skb, flags, mask,
3412                                 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3413             brport_nla_put_flag(skb, flags, mask,
3414                                 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3415                 nla_nest_cancel(skb, protinfo);
3416                 goto nla_put_failure;
3417         }
3418
3419         nla_nest_end(skb, protinfo);
3420
3421         nlmsg_end(skb, nlh);
3422         return 0;
3423 nla_put_failure:
3424         nlmsg_cancel(skb, nlh);
3425         return err ? err : -EMSGSIZE;
3426 }
3427 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3428
3429 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3430 {
3431         struct net *net = sock_net(skb->sk);
3432         struct net_device *dev;
3433         int idx = 0;
3434         u32 portid = NETLINK_CB(cb->skb).portid;
3435         u32 seq = cb->nlh->nlmsg_seq;
3436         u32 filter_mask = 0;
3437         int err;
3438
3439         if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3440                 struct nlattr *extfilt;
3441
3442                 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3443                                           IFLA_EXT_MASK);
3444                 if (extfilt) {
3445                         if (nla_len(extfilt) < sizeof(filter_mask))
3446                                 return -EINVAL;
3447
3448                         filter_mask = nla_get_u32(extfilt);
3449                 }
3450         }
3451
3452         rcu_read_lock();
3453         for_each_netdev_rcu(net, dev) {
3454                 const struct net_device_ops *ops = dev->netdev_ops;
3455                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3456
3457                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3458                         if (idx >= cb->args[0]) {
3459                                 err = br_dev->netdev_ops->ndo_bridge_getlink(
3460                                                 skb, portid, seq, dev,
3461                                                 filter_mask, NLM_F_MULTI);
3462                                 if (err < 0 && err != -EOPNOTSUPP) {
3463                                         if (likely(skb->len))
3464                                                 break;
3465
3466                                         goto out_err;
3467                                 }
3468                         }
3469                         idx++;
3470                 }
3471
3472                 if (ops->ndo_bridge_getlink) {
3473                         if (idx >= cb->args[0]) {
3474                                 err = ops->ndo_bridge_getlink(skb, portid,
3475                                                               seq, dev,
3476                                                               filter_mask,
3477                                                               NLM_F_MULTI);
3478                                 if (err < 0 && err != -EOPNOTSUPP) {
3479                                         if (likely(skb->len))
3480                                                 break;
3481
3482                                         goto out_err;
3483                                 }
3484                         }
3485                         idx++;
3486                 }
3487         }
3488         err = skb->len;
3489 out_err:
3490         rcu_read_unlock();
3491         cb->args[0] = idx;
3492
3493         return err;
3494 }
3495
3496 static inline size_t bridge_nlmsg_size(void)
3497 {
3498         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3499                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
3500                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
3501                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
3502                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
3503                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
3504                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
3505                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
3506                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3507                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
3508                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
3509 }
3510
3511 static int rtnl_bridge_notify(struct net_device *dev)
3512 {
3513         struct net *net = dev_net(dev);
3514         struct sk_buff *skb;
3515         int err = -EOPNOTSUPP;
3516
3517         if (!dev->netdev_ops->ndo_bridge_getlink)
3518                 return 0;
3519
3520         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3521         if (!skb) {
3522                 err = -ENOMEM;
3523                 goto errout;
3524         }
3525
3526         err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3527         if (err < 0)
3528                 goto errout;
3529
3530         if (!skb->len)
3531                 goto errout;
3532
3533         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3534         return 0;
3535 errout:
3536         WARN_ON(err == -EMSGSIZE);
3537         kfree_skb(skb);
3538         if (err)
3539                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3540         return err;
3541 }
3542
3543 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3544                                struct netlink_ext_ack *extack)
3545 {
3546         struct net *net = sock_net(skb->sk);
3547         struct ifinfomsg *ifm;
3548         struct net_device *dev;
3549         struct nlattr *br_spec, *attr = NULL;
3550         int rem, err = -EOPNOTSUPP;
3551         u16 flags = 0;
3552         bool have_flags = false;
3553
3554         if (nlmsg_len(nlh) < sizeof(*ifm))
3555                 return -EINVAL;
3556
3557         ifm = nlmsg_data(nlh);
3558         if (ifm->ifi_family != AF_BRIDGE)
3559                 return -EPFNOSUPPORT;
3560
3561         dev = __dev_get_by_index(net, ifm->ifi_index);
3562         if (!dev) {
3563                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3564                 return -ENODEV;
3565         }
3566
3567         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3568         if (br_spec) {
3569                 nla_for_each_nested(attr, br_spec, rem) {
3570                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3571                                 if (nla_len(attr) < sizeof(flags))
3572                                         return -EINVAL;
3573
3574                                 have_flags = true;
3575                                 flags = nla_get_u16(attr);
3576                                 break;
3577                         }
3578                 }
3579         }
3580
3581         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3582                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3583
3584                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3585                         err = -EOPNOTSUPP;
3586                         goto out;
3587                 }
3588
3589                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3590                 if (err)
3591                         goto out;
3592
3593                 flags &= ~BRIDGE_FLAGS_MASTER;
3594         }
3595
3596         if ((flags & BRIDGE_FLAGS_SELF)) {
3597                 if (!dev->netdev_ops->ndo_bridge_setlink)
3598                         err = -EOPNOTSUPP;
3599                 else
3600                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3601                                                                   flags);
3602                 if (!err) {
3603                         flags &= ~BRIDGE_FLAGS_SELF;
3604
3605                         /* Generate event to notify upper layer of bridge
3606                          * change
3607                          */
3608                         err = rtnl_bridge_notify(dev);
3609                 }
3610         }
3611
3612         if (have_flags)
3613                 memcpy(nla_data(attr), &flags, sizeof(flags));
3614 out:
3615         return err;
3616 }
3617
3618 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3619                                struct netlink_ext_ack *extack)
3620 {
3621         struct net *net = sock_net(skb->sk);
3622         struct ifinfomsg *ifm;
3623         struct net_device *dev;
3624         struct nlattr *br_spec, *attr = NULL;
3625         int rem, err = -EOPNOTSUPP;
3626         u16 flags = 0;
3627         bool have_flags = false;
3628
3629         if (nlmsg_len(nlh) < sizeof(*ifm))
3630                 return -EINVAL;
3631
3632         ifm = nlmsg_data(nlh);
3633         if (ifm->ifi_family != AF_BRIDGE)
3634                 return -EPFNOSUPPORT;
3635
3636         dev = __dev_get_by_index(net, ifm->ifi_index);
3637         if (!dev) {
3638                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3639                 return -ENODEV;
3640         }
3641
3642         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3643         if (br_spec) {
3644                 nla_for_each_nested(attr, br_spec, rem) {
3645                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3646                                 if (nla_len(attr) < sizeof(flags))
3647                                         return -EINVAL;
3648
3649                                 have_flags = true;
3650                                 flags = nla_get_u16(attr);
3651                                 break;
3652                         }
3653                 }
3654         }
3655
3656         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3657                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3658
3659                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3660                         err = -EOPNOTSUPP;
3661                         goto out;
3662                 }
3663
3664                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3665                 if (err)
3666                         goto out;
3667
3668                 flags &= ~BRIDGE_FLAGS_MASTER;
3669         }
3670
3671         if ((flags & BRIDGE_FLAGS_SELF)) {
3672                 if (!dev->netdev_ops->ndo_bridge_dellink)
3673                         err = -EOPNOTSUPP;
3674                 else
3675                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3676                                                                   flags);
3677
3678                 if (!err) {
3679                         flags &= ~BRIDGE_FLAGS_SELF;
3680
3681                         /* Generate event to notify upper layer of bridge
3682                          * change
3683                          */
3684                         err = rtnl_bridge_notify(dev);
3685                 }
3686         }
3687
3688         if (have_flags)
3689                 memcpy(nla_data(attr), &flags, sizeof(flags));
3690 out:
3691         return err;
3692 }
3693
3694 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3695 {
3696         return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3697                (!idxattr || idxattr == attrid);
3698 }
3699
3700 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3701 static int rtnl_get_offload_stats_attr_size(int attr_id)
3702 {
3703         switch (attr_id) {
3704         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3705                 return sizeof(struct rtnl_link_stats64);
3706         }
3707
3708         return 0;
3709 }
3710
3711 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3712                                   int *prividx)
3713 {
3714         struct nlattr *attr = NULL;
3715         int attr_id, size;
3716         void *attr_data;
3717         int err;
3718
3719         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3720               dev->netdev_ops->ndo_get_offload_stats))
3721                 return -ENODATA;
3722
3723         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3724              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3725                 if (attr_id < *prividx)
3726                         continue;
3727
3728                 size = rtnl_get_offload_stats_attr_size(attr_id);
3729                 if (!size)
3730                         continue;
3731
3732                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3733                         continue;
3734
3735                 attr = nla_reserve_64bit(skb, attr_id, size,
3736                                          IFLA_OFFLOAD_XSTATS_UNSPEC);
3737                 if (!attr)
3738                         goto nla_put_failure;
3739
3740                 attr_data = nla_data(attr);
3741                 memset(attr_data, 0, size);
3742                 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3743                                                              attr_data);
3744                 if (err)
3745                         goto get_offload_stats_failure;
3746         }
3747
3748         if (!attr)
3749                 return -ENODATA;
3750
3751         *prividx = 0;
3752         return 0;
3753
3754 nla_put_failure:
3755         err = -EMSGSIZE;
3756 get_offload_stats_failure:
3757         *prividx = attr_id;
3758         return err;
3759 }
3760
3761 static int rtnl_get_offload_stats_size(const struct net_device *dev)
3762 {
3763         int nla_size = 0;
3764         int attr_id;
3765         int size;
3766
3767         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3768               dev->netdev_ops->ndo_get_offload_stats))
3769                 return 0;
3770
3771         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3772              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3773                 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3774                         continue;
3775                 size = rtnl_get_offload_stats_attr_size(attr_id);
3776                 nla_size += nla_total_size_64bit(size);
3777         }
3778
3779         if (nla_size != 0)
3780                 nla_size += nla_total_size(0);
3781
3782         return nla_size;
3783 }
3784
3785 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3786                                int type, u32 pid, u32 seq, u32 change,
3787                                unsigned int flags, unsigned int filter_mask,
3788                                int *idxattr, int *prividx)
3789 {
3790         struct if_stats_msg *ifsm;
3791         struct nlmsghdr *nlh;
3792         struct nlattr *attr;
3793         int s_prividx = *prividx;
3794         int err;
3795
3796         ASSERT_RTNL();
3797
3798         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3799         if (!nlh)
3800                 return -EMSGSIZE;
3801
3802         ifsm = nlmsg_data(nlh);
3803         ifsm->ifindex = dev->ifindex;
3804         ifsm->filter_mask = filter_mask;
3805
3806         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3807                 struct rtnl_link_stats64 *sp;
3808
3809                 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3810                                          sizeof(struct rtnl_link_stats64),
3811                                          IFLA_STATS_UNSPEC);
3812                 if (!attr)
3813                         goto nla_put_failure;
3814
3815                 sp = nla_data(attr);
3816                 dev_get_stats(dev, sp);
3817         }
3818
3819         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3820                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3821
3822                 if (ops && ops->fill_linkxstats) {
3823                         *idxattr = IFLA_STATS_LINK_XSTATS;
3824                         attr = nla_nest_start(skb,
3825                                               IFLA_STATS_LINK_XSTATS);
3826                         if (!attr)
3827                                 goto nla_put_failure;
3828
3829                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3830                         nla_nest_end(skb, attr);
3831                         if (err)
3832                                 goto nla_put_failure;
3833                         *idxattr = 0;
3834                 }
3835         }
3836
3837         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3838                              *idxattr)) {
3839                 const struct rtnl_link_ops *ops = NULL;
3840                 const struct net_device *master;
3841
3842                 master = netdev_master_upper_dev_get(dev);
3843                 if (master)
3844                         ops = master->rtnl_link_ops;
3845                 if (ops && ops->fill_linkxstats) {
3846                         *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3847                         attr = nla_nest_start(skb,
3848                                               IFLA_STATS_LINK_XSTATS_SLAVE);
3849                         if (!attr)
3850                                 goto nla_put_failure;
3851
3852                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3853                         nla_nest_end(skb, attr);
3854                         if (err)
3855                                 goto nla_put_failure;
3856                         *idxattr = 0;
3857                 }
3858         }
3859
3860         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3861                              *idxattr)) {
3862                 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3863                 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3864                 if (!attr)
3865                         goto nla_put_failure;
3866
3867                 err = rtnl_get_offload_stats(skb, dev, prividx);
3868                 if (err == -ENODATA)
3869                         nla_nest_cancel(skb, attr);
3870                 else
3871                         nla_nest_end(skb, attr);
3872
3873                 if (err && err != -ENODATA)
3874                         goto nla_put_failure;
3875                 *idxattr = 0;
3876         }
3877
3878         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3879                 struct rtnl_af_ops *af_ops;
3880
3881                 *idxattr = IFLA_STATS_AF_SPEC;
3882                 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3883                 if (!attr)
3884                         goto nla_put_failure;
3885
3886                 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3887                         if (af_ops->fill_stats_af) {
3888                                 struct nlattr *af;
3889                                 int err;
3890
3891                                 af = nla_nest_start(skb, af_ops->family);
3892                                 if (!af)
3893                                         goto nla_put_failure;
3894
3895                                 err = af_ops->fill_stats_af(skb, dev);
3896
3897                                 if (err == -ENODATA)
3898                                         nla_nest_cancel(skb, af);
3899                                 else if (err < 0)
3900                                         goto nla_put_failure;
3901
3902                                 nla_nest_end(skb, af);
3903                         }
3904                 }
3905
3906                 nla_nest_end(skb, attr);
3907
3908                 *idxattr = 0;
3909         }
3910
3911         nlmsg_end(skb, nlh);
3912
3913         return 0;
3914
3915 nla_put_failure:
3916         /* not a multi message or no progress mean a real error */
3917         if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3918                 nlmsg_cancel(skb, nlh);
3919         else
3920                 nlmsg_end(skb, nlh);
3921
3922         return -EMSGSIZE;
3923 }
3924
3925 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3926                                   u32 filter_mask)
3927 {
3928         size_t size = 0;
3929
3930         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3931                 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3932
3933         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3934                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3935                 int attr = IFLA_STATS_LINK_XSTATS;
3936
3937                 if (ops && ops->get_linkxstats_size) {
3938                         size += nla_total_size(ops->get_linkxstats_size(dev,
3939                                                                         attr));
3940                         /* for IFLA_STATS_LINK_XSTATS */
3941                         size += nla_total_size(0);
3942                 }
3943         }
3944
3945         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
3946                 struct net_device *_dev = (struct net_device *)dev;
3947                 const struct rtnl_link_ops *ops = NULL;
3948                 const struct net_device *master;
3949
3950                 /* netdev_master_upper_dev_get can't take const */
3951                 master = netdev_master_upper_dev_get(_dev);
3952                 if (master)
3953                         ops = master->rtnl_link_ops;
3954                 if (ops && ops->get_linkxstats_size) {
3955                         int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
3956
3957                         size += nla_total_size(ops->get_linkxstats_size(dev,
3958                                                                         attr));
3959                         /* for IFLA_STATS_LINK_XSTATS_SLAVE */
3960                         size += nla_total_size(0);
3961                 }
3962         }
3963
3964         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
3965                 size += rtnl_get_offload_stats_size(dev);
3966
3967         if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
3968                 struct rtnl_af_ops *af_ops;
3969
3970                 /* for IFLA_STATS_AF_SPEC */
3971                 size += nla_total_size(0);
3972
3973                 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3974                         if (af_ops->get_stats_af_size) {
3975                                 size += nla_total_size(
3976                                         af_ops->get_stats_af_size(dev));
3977
3978                                 /* for AF_* */
3979                                 size += nla_total_size(0);
3980                         }
3981                 }
3982         }
3983
3984         return size;
3985 }
3986
3987 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
3988                           struct netlink_ext_ack *extack)
3989 {
3990         struct net *net = sock_net(skb->sk);
3991         struct net_device *dev = NULL;
3992         int idxattr = 0, prividx = 0;
3993         struct if_stats_msg *ifsm;
3994         struct sk_buff *nskb;
3995         u32 filter_mask;
3996         int err;
3997
3998         if (nlmsg_len(nlh) < sizeof(*ifsm))
3999                 return -EINVAL;
4000
4001         ifsm = nlmsg_data(nlh);
4002         if (ifsm->ifindex > 0)
4003                 dev = __dev_get_by_index(net, ifsm->ifindex);
4004         else
4005                 return -EINVAL;
4006
4007         if (!dev)
4008                 return -ENODEV;
4009
4010         filter_mask = ifsm->filter_mask;
4011         if (!filter_mask)
4012                 return -EINVAL;
4013
4014         nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4015         if (!nskb)
4016                 return -ENOBUFS;
4017
4018         err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4019                                   NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4020                                   0, filter_mask, &idxattr, &prividx);
4021         if (err < 0) {
4022                 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4023                 WARN_ON(err == -EMSGSIZE);
4024                 kfree_skb(nskb);
4025         } else {
4026                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4027         }
4028
4029         return err;
4030 }
4031
4032 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4033 {
4034         int h, s_h, err, s_idx, s_idxattr, s_prividx;
4035         struct net *net = sock_net(skb->sk);
4036         unsigned int flags = NLM_F_MULTI;
4037         struct if_stats_msg *ifsm;
4038         struct hlist_head *head;
4039         struct net_device *dev;
4040         u32 filter_mask = 0;
4041         int idx = 0;
4042
4043         s_h = cb->args[0];
4044         s_idx = cb->args[1];
4045         s_idxattr = cb->args[2];
4046         s_prividx = cb->args[3];
4047
4048         cb->seq = net->dev_base_seq;
4049
4050         if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4051                 return -EINVAL;
4052
4053         ifsm = nlmsg_data(cb->nlh);
4054         filter_mask = ifsm->filter_mask;
4055         if (!filter_mask)
4056                 return -EINVAL;
4057
4058         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4059                 idx = 0;
4060                 head = &net->dev_index_head[h];
4061                 hlist_for_each_entry(dev, head, index_hlist) {
4062                         if (idx < s_idx)
4063                                 goto cont;
4064                         err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4065                                                   NETLINK_CB(cb->skb).portid,
4066                                                   cb->nlh->nlmsg_seq, 0,
4067                                                   flags, filter_mask,
4068                                                   &s_idxattr, &s_prividx);
4069                         /* If we ran out of room on the first message,
4070                          * we're in trouble
4071                          */
4072                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4073
4074                         if (err < 0)
4075                                 goto out;
4076                         s_prividx = 0;
4077                         s_idxattr = 0;
4078                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4079 cont:
4080                         idx++;
4081                 }
4082         }
4083 out:
4084         cb->args[3] = s_prividx;
4085         cb->args[2] = s_idxattr;
4086         cb->args[1] = idx;
4087         cb->args[0] = h;
4088
4089         return skb->len;
4090 }
4091
4092 /* Process one rtnetlink message. */
4093
4094 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4095                              struct netlink_ext_ack *extack)
4096 {
4097         struct net *net = sock_net(skb->sk);
4098         rtnl_doit_func doit;
4099         int kind;
4100         int family;
4101         int type;
4102         int err;
4103
4104         type = nlh->nlmsg_type;
4105         if (type > RTM_MAX)
4106                 return -EOPNOTSUPP;
4107
4108         type -= RTM_BASE;
4109
4110         /* All the messages must have at least 1 byte length */
4111         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4112                 return 0;
4113
4114         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4115         kind = type&3;
4116
4117         if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4118                 return -EPERM;
4119
4120         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4121                 struct sock *rtnl;
4122                 rtnl_dumpit_func dumpit;
4123                 rtnl_calcit_func calcit;
4124                 u16 min_dump_alloc = 0;
4125
4126                 dumpit = rtnl_get_dumpit(family, type);
4127                 if (dumpit == NULL)
4128                         return -EOPNOTSUPP;
4129                 calcit = rtnl_get_calcit(family, type);
4130                 if (calcit)
4131                         min_dump_alloc = calcit(skb, nlh);
4132
4133                 __rtnl_unlock();
4134                 rtnl = net->rtnl;
4135                 {
4136                         struct netlink_dump_control c = {
4137                                 .dump           = dumpit,
4138                                 .min_dump_alloc = min_dump_alloc,
4139                         };
4140                         err = netlink_dump_start(rtnl, skb, nlh, &c);
4141                 }
4142                 rtnl_lock();
4143                 return err;
4144         }
4145
4146         doit = rtnl_get_doit(family, type);
4147         if (doit == NULL)
4148                 return -EOPNOTSUPP;
4149
4150         return doit(skb, nlh, extack);
4151 }
4152
4153 static void rtnetlink_rcv(struct sk_buff *skb)
4154 {
4155         rtnl_lock();
4156         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4157         rtnl_unlock();
4158 }
4159
4160 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4161 {
4162         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4163
4164         switch (event) {
4165         case NETDEV_REBOOT:
4166         case NETDEV_CHANGENAME:
4167         case NETDEV_FEAT_CHANGE:
4168         case NETDEV_BONDING_FAILOVER:
4169         case NETDEV_NOTIFY_PEERS:
4170         case NETDEV_RESEND_IGMP:
4171         case NETDEV_CHANGEINFODATA:
4172                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
4173                 break;
4174         default:
4175                 break;
4176         }
4177         return NOTIFY_DONE;
4178 }
4179
4180 static struct notifier_block rtnetlink_dev_notifier = {
4181         .notifier_call  = rtnetlink_event,
4182 };
4183
4184
4185 static int __net_init rtnetlink_net_init(struct net *net)
4186 {
4187         struct sock *sk;
4188         struct netlink_kernel_cfg cfg = {
4189                 .groups         = RTNLGRP_MAX,
4190                 .input          = rtnetlink_rcv,
4191                 .cb_mutex       = &rtnl_mutex,
4192                 .flags          = NL_CFG_F_NONROOT_RECV,
4193         };
4194
4195         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4196         if (!sk)
4197                 return -ENOMEM;
4198         net->rtnl = sk;
4199         return 0;
4200 }
4201
4202 static void __net_exit rtnetlink_net_exit(struct net *net)
4203 {
4204         netlink_kernel_release(net->rtnl);
4205         net->rtnl = NULL;
4206 }
4207
4208 static struct pernet_operations rtnetlink_net_ops = {
4209         .init = rtnetlink_net_init,
4210         .exit = rtnetlink_net_exit,
4211 };
4212
4213 void __init rtnetlink_init(void)
4214 {
4215         if (register_pernet_subsys(&rtnetlink_net_ops))
4216                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4217
4218         register_netdevice_notifier(&rtnetlink_dev_notifier);
4219
4220         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4221                       rtnl_dump_ifinfo, rtnl_calcit);
4222         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4223         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4224         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
4225
4226         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4227         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
4228         rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL);
4229
4230         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4231         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4232         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
4233
4234         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
4235         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
4236         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
4237
4238         rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4239                       NULL);
4240 }