Merge tag 'xtensa-20161116' of git://github.com/jcmvbkbc/linux-xtensa
[platform/kernel/linux-rpi.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 <asm/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 && dev_is_pci(dev->dev.parent) &&
841             (ext_filter_mask & RTEXT_FILTER_VF)) {
842                 int num_vfs = dev_num_vf(dev->dev.parent);
843                 size_t size = nla_total_size(sizeof(struct nlattr));
844                 size += nla_total_size(num_vfs * sizeof(struct nlattr));
845                 size += num_vfs *
846                         (nla_total_size(sizeof(struct ifla_vf_mac)) +
847                          nla_total_size(MAX_VLAN_LIST_LEN *
848                                         sizeof(struct nlattr)) +
849                          nla_total_size(MAX_VLAN_LIST_LEN *
850                                         sizeof(struct ifla_vf_vlan_info)) +
851                          nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
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                          /* IFLA_VF_STATS_RX_PACKETS */
856                          nla_total_size_64bit(sizeof(__u64)) +
857                          /* IFLA_VF_STATS_TX_PACKETS */
858                          nla_total_size_64bit(sizeof(__u64)) +
859                          /* IFLA_VF_STATS_RX_BYTES */
860                          nla_total_size_64bit(sizeof(__u64)) +
861                          /* IFLA_VF_STATS_TX_BYTES */
862                          nla_total_size_64bit(sizeof(__u64)) +
863                          /* IFLA_VF_STATS_BROADCAST */
864                          nla_total_size_64bit(sizeof(__u64)) +
865                          /* IFLA_VF_STATS_MULTICAST */
866                          nla_total_size_64bit(sizeof(__u64)) +
867                          nla_total_size(sizeof(struct ifla_vf_trust)));
868                 return size;
869         } else
870                 return 0;
871 }
872
873 static size_t rtnl_port_size(const struct net_device *dev,
874                              u32 ext_filter_mask)
875 {
876         size_t port_size = nla_total_size(4)            /* PORT_VF */
877                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
878                 + nla_total_size(sizeof(struct ifla_port_vsi))
879                                                         /* PORT_VSI_TYPE */
880                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
881                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
882                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
883                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
884         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
885         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
886                 + port_size;
887         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
888                 + port_size;
889
890         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
891             !(ext_filter_mask & RTEXT_FILTER_VF))
892                 return 0;
893         if (dev_num_vf(dev->dev.parent))
894                 return port_self_size + vf_ports_size +
895                         vf_port_size * dev_num_vf(dev->dev.parent);
896         else
897                 return port_self_size;
898 }
899
900 static size_t rtnl_xdp_size(const struct net_device *dev)
901 {
902         size_t xdp_size = nla_total_size(1);    /* XDP_ATTACHED */
903
904         if (!dev->netdev_ops->ndo_xdp)
905                 return 0;
906         else
907                 return xdp_size;
908 }
909
910 static noinline size_t if_nlmsg_size(const struct net_device *dev,
911                                      u32 ext_filter_mask)
912 {
913         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
914                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
915                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
916                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
917                + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
918                + nla_total_size(sizeof(struct rtnl_link_stats))
919                + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
920                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
921                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
922                + nla_total_size(4) /* IFLA_TXQLEN */
923                + nla_total_size(4) /* IFLA_WEIGHT */
924                + nla_total_size(4) /* IFLA_MTU */
925                + nla_total_size(4) /* IFLA_LINK */
926                + nla_total_size(4) /* IFLA_MASTER */
927                + nla_total_size(1) /* IFLA_CARRIER */
928                + nla_total_size(4) /* IFLA_PROMISCUITY */
929                + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
930                + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
931                + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
932                + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
933                + nla_total_size(1) /* IFLA_OPERSTATE */
934                + nla_total_size(1) /* IFLA_LINKMODE */
935                + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
936                + nla_total_size(4) /* IFLA_LINK_NETNSID */
937                + nla_total_size(ext_filter_mask
938                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
939                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
940                + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
941                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
942                + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
943                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
944                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
945                + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
946                + rtnl_xdp_size(dev) /* IFLA_XDP */
947                + nla_total_size(1); /* IFLA_PROTO_DOWN */
948
949 }
950
951 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
952 {
953         struct nlattr *vf_ports;
954         struct nlattr *vf_port;
955         int vf;
956         int err;
957
958         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
959         if (!vf_ports)
960                 return -EMSGSIZE;
961
962         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
963                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
964                 if (!vf_port)
965                         goto nla_put_failure;
966                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
967                         goto nla_put_failure;
968                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
969                 if (err == -EMSGSIZE)
970                         goto nla_put_failure;
971                 if (err) {
972                         nla_nest_cancel(skb, vf_port);
973                         continue;
974                 }
975                 nla_nest_end(skb, vf_port);
976         }
977
978         nla_nest_end(skb, vf_ports);
979
980         return 0;
981
982 nla_put_failure:
983         nla_nest_cancel(skb, vf_ports);
984         return -EMSGSIZE;
985 }
986
987 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
988 {
989         struct nlattr *port_self;
990         int err;
991
992         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
993         if (!port_self)
994                 return -EMSGSIZE;
995
996         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
997         if (err) {
998                 nla_nest_cancel(skb, port_self);
999                 return (err == -EMSGSIZE) ? err : 0;
1000         }
1001
1002         nla_nest_end(skb, port_self);
1003
1004         return 0;
1005 }
1006
1007 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1008                           u32 ext_filter_mask)
1009 {
1010         int err;
1011
1012         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1013             !(ext_filter_mask & RTEXT_FILTER_VF))
1014                 return 0;
1015
1016         err = rtnl_port_self_fill(skb, dev);
1017         if (err)
1018                 return err;
1019
1020         if (dev_num_vf(dev->dev.parent)) {
1021                 err = rtnl_vf_ports_fill(skb, dev);
1022                 if (err)
1023                         return err;
1024         }
1025
1026         return 0;
1027 }
1028
1029 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1030 {
1031         int err;
1032         struct netdev_phys_item_id ppid;
1033
1034         err = dev_get_phys_port_id(dev, &ppid);
1035         if (err) {
1036                 if (err == -EOPNOTSUPP)
1037                         return 0;
1038                 return err;
1039         }
1040
1041         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1042                 return -EMSGSIZE;
1043
1044         return 0;
1045 }
1046
1047 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1048 {
1049         char name[IFNAMSIZ];
1050         int err;
1051
1052         err = dev_get_phys_port_name(dev, name, sizeof(name));
1053         if (err) {
1054                 if (err == -EOPNOTSUPP)
1055                         return 0;
1056                 return err;
1057         }
1058
1059         if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1060                 return -EMSGSIZE;
1061
1062         return 0;
1063 }
1064
1065 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1066 {
1067         int err;
1068         struct switchdev_attr attr = {
1069                 .orig_dev = dev,
1070                 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1071                 .flags = SWITCHDEV_F_NO_RECURSE,
1072         };
1073
1074         err = switchdev_port_attr_get(dev, &attr);
1075         if (err) {
1076                 if (err == -EOPNOTSUPP)
1077                         return 0;
1078                 return err;
1079         }
1080
1081         if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1082                     attr.u.ppid.id))
1083                 return -EMSGSIZE;
1084
1085         return 0;
1086 }
1087
1088 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1089                                               struct net_device *dev)
1090 {
1091         struct rtnl_link_stats64 *sp;
1092         struct nlattr *attr;
1093
1094         attr = nla_reserve_64bit(skb, IFLA_STATS64,
1095                                  sizeof(struct rtnl_link_stats64), IFLA_PAD);
1096         if (!attr)
1097                 return -EMSGSIZE;
1098
1099         sp = nla_data(attr);
1100         dev_get_stats(dev, sp);
1101
1102         attr = nla_reserve(skb, IFLA_STATS,
1103                            sizeof(struct rtnl_link_stats));
1104         if (!attr)
1105                 return -EMSGSIZE;
1106
1107         copy_rtnl_link_stats(nla_data(attr), sp);
1108
1109         return 0;
1110 }
1111
1112 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1113                                                struct net_device *dev,
1114                                                int vfs_num,
1115                                                struct nlattr *vfinfo)
1116 {
1117         struct ifla_vf_rss_query_en vf_rss_query_en;
1118         struct nlattr *vf, *vfstats, *vfvlanlist;
1119         struct ifla_vf_link_state vf_linkstate;
1120         struct ifla_vf_vlan_info vf_vlan_info;
1121         struct ifla_vf_spoofchk vf_spoofchk;
1122         struct ifla_vf_tx_rate vf_tx_rate;
1123         struct ifla_vf_stats vf_stats;
1124         struct ifla_vf_trust vf_trust;
1125         struct ifla_vf_vlan vf_vlan;
1126         struct ifla_vf_rate vf_rate;
1127         struct ifla_vf_mac vf_mac;
1128         struct ifla_vf_info ivi;
1129
1130         /* Not all SR-IOV capable drivers support the
1131          * spoofcheck and "RSS query enable" query.  Preset to
1132          * -1 so the user space tool can detect that the driver
1133          * didn't report anything.
1134          */
1135         ivi.spoofchk = -1;
1136         ivi.rss_query_en = -1;
1137         ivi.trusted = -1;
1138         memset(ivi.mac, 0, sizeof(ivi.mac));
1139         /* The default value for VF link state is "auto"
1140          * IFLA_VF_LINK_STATE_AUTO which equals zero
1141          */
1142         ivi.linkstate = 0;
1143         /* VLAN Protocol by default is 802.1Q */
1144         ivi.vlan_proto = htons(ETH_P_8021Q);
1145         if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1146                 return 0;
1147
1148         memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1149
1150         vf_mac.vf =
1151                 vf_vlan.vf =
1152                 vf_vlan_info.vf =
1153                 vf_rate.vf =
1154                 vf_tx_rate.vf =
1155                 vf_spoofchk.vf =
1156                 vf_linkstate.vf =
1157                 vf_rss_query_en.vf =
1158                 vf_trust.vf = ivi.vf;
1159
1160         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1161         vf_vlan.vlan = ivi.vlan;
1162         vf_vlan.qos = ivi.qos;
1163         vf_vlan_info.vlan = ivi.vlan;
1164         vf_vlan_info.qos = ivi.qos;
1165         vf_vlan_info.vlan_proto = ivi.vlan_proto;
1166         vf_tx_rate.rate = ivi.max_tx_rate;
1167         vf_rate.min_tx_rate = ivi.min_tx_rate;
1168         vf_rate.max_tx_rate = ivi.max_tx_rate;
1169         vf_spoofchk.setting = ivi.spoofchk;
1170         vf_linkstate.link_state = ivi.linkstate;
1171         vf_rss_query_en.setting = ivi.rss_query_en;
1172         vf_trust.setting = ivi.trusted;
1173         vf = nla_nest_start(skb, IFLA_VF_INFO);
1174         if (!vf)
1175                 goto nla_put_vfinfo_failure;
1176         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1177             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1178             nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1179                     &vf_rate) ||
1180             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1181                     &vf_tx_rate) ||
1182             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1183                     &vf_spoofchk) ||
1184             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1185                     &vf_linkstate) ||
1186             nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1187                     sizeof(vf_rss_query_en),
1188                     &vf_rss_query_en) ||
1189             nla_put(skb, IFLA_VF_TRUST,
1190                     sizeof(vf_trust), &vf_trust))
1191                 goto nla_put_vf_failure;
1192         vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1193         if (!vfvlanlist)
1194                 goto nla_put_vf_failure;
1195         if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1196                     &vf_vlan_info)) {
1197                 nla_nest_cancel(skb, vfvlanlist);
1198                 goto nla_put_vf_failure;
1199         }
1200         nla_nest_end(skb, vfvlanlist);
1201         memset(&vf_stats, 0, sizeof(vf_stats));
1202         if (dev->netdev_ops->ndo_get_vf_stats)
1203                 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1204                                                 &vf_stats);
1205         vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1206         if (!vfstats)
1207                 goto nla_put_vf_failure;
1208         if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1209                               vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1210             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1211                               vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1212             nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1213                               vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1214             nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1215                               vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1216             nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1217                               vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1218             nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1219                               vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1220                 nla_nest_cancel(skb, vfstats);
1221                 goto nla_put_vf_failure;
1222         }
1223         nla_nest_end(skb, vfstats);
1224         nla_nest_end(skb, vf);
1225         return 0;
1226
1227 nla_put_vf_failure:
1228         nla_nest_cancel(skb, vf);
1229 nla_put_vfinfo_failure:
1230         nla_nest_cancel(skb, vfinfo);
1231         return -EMSGSIZE;
1232 }
1233
1234 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1235 {
1236         struct rtnl_link_ifmap map;
1237
1238         memset(&map, 0, sizeof(map));
1239         map.mem_start   = dev->mem_start;
1240         map.mem_end     = dev->mem_end;
1241         map.base_addr   = dev->base_addr;
1242         map.irq         = dev->irq;
1243         map.dma         = dev->dma;
1244         map.port        = dev->if_port;
1245
1246         if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1247                 return -EMSGSIZE;
1248
1249         return 0;
1250 }
1251
1252 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1253 {
1254         struct netdev_xdp xdp_op = {};
1255         struct nlattr *xdp;
1256         int err;
1257
1258         if (!dev->netdev_ops->ndo_xdp)
1259                 return 0;
1260         xdp = nla_nest_start(skb, IFLA_XDP);
1261         if (!xdp)
1262                 return -EMSGSIZE;
1263         xdp_op.command = XDP_QUERY_PROG;
1264         err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
1265         if (err)
1266                 goto err_cancel;
1267         err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
1268         if (err)
1269                 goto err_cancel;
1270
1271         nla_nest_end(skb, xdp);
1272         return 0;
1273
1274 err_cancel:
1275         nla_nest_cancel(skb, xdp);
1276         return err;
1277 }
1278
1279 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1280                             int type, u32 pid, u32 seq, u32 change,
1281                             unsigned int flags, u32 ext_filter_mask)
1282 {
1283         struct ifinfomsg *ifm;
1284         struct nlmsghdr *nlh;
1285         struct nlattr *af_spec;
1286         struct rtnl_af_ops *af_ops;
1287         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1288
1289         ASSERT_RTNL();
1290         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1291         if (nlh == NULL)
1292                 return -EMSGSIZE;
1293
1294         ifm = nlmsg_data(nlh);
1295         ifm->ifi_family = AF_UNSPEC;
1296         ifm->__ifi_pad = 0;
1297         ifm->ifi_type = dev->type;
1298         ifm->ifi_index = dev->ifindex;
1299         ifm->ifi_flags = dev_get_flags(dev);
1300         ifm->ifi_change = change;
1301
1302         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1303             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1304             nla_put_u8(skb, IFLA_OPERSTATE,
1305                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1306             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1307             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1308             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1309             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1310             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1311             nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1312             nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1313 #ifdef CONFIG_RPS
1314             nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1315 #endif
1316             (dev->ifindex != dev_get_iflink(dev) &&
1317              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1318             (upper_dev &&
1319              nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1320             nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1321             (dev->qdisc &&
1322              nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1323             (dev->ifalias &&
1324              nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1325             nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1326                         atomic_read(&dev->carrier_changes)) ||
1327             nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1328                 goto nla_put_failure;
1329
1330         if (rtnl_fill_link_ifmap(skb, dev))
1331                 goto nla_put_failure;
1332
1333         if (dev->addr_len) {
1334                 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1335                     nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1336                         goto nla_put_failure;
1337         }
1338
1339         if (rtnl_phys_port_id_fill(skb, dev))
1340                 goto nla_put_failure;
1341
1342         if (rtnl_phys_port_name_fill(skb, dev))
1343                 goto nla_put_failure;
1344
1345         if (rtnl_phys_switch_id_fill(skb, dev))
1346                 goto nla_put_failure;
1347
1348         if (rtnl_fill_stats(skb, dev))
1349                 goto nla_put_failure;
1350
1351         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1352             nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1353                 goto nla_put_failure;
1354
1355         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1356             ext_filter_mask & RTEXT_FILTER_VF) {
1357                 int i;
1358                 struct nlattr *vfinfo;
1359                 int num_vfs = dev_num_vf(dev->dev.parent);
1360
1361                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1362                 if (!vfinfo)
1363                         goto nla_put_failure;
1364                 for (i = 0; i < num_vfs; i++) {
1365                         if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1366                                 goto nla_put_failure;
1367                 }
1368
1369                 nla_nest_end(skb, vfinfo);
1370         }
1371
1372         if (rtnl_port_fill(skb, dev, ext_filter_mask))
1373                 goto nla_put_failure;
1374
1375         if (rtnl_xdp_fill(skb, dev))
1376                 goto nla_put_failure;
1377
1378         if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1379                 if (rtnl_link_fill(skb, dev) < 0)
1380                         goto nla_put_failure;
1381         }
1382
1383         if (dev->rtnl_link_ops &&
1384             dev->rtnl_link_ops->get_link_net) {
1385                 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1386
1387                 if (!net_eq(dev_net(dev), link_net)) {
1388                         int id = peernet2id_alloc(dev_net(dev), link_net);
1389
1390                         if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1391                                 goto nla_put_failure;
1392                 }
1393         }
1394
1395         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1396                 goto nla_put_failure;
1397
1398         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1399                 if (af_ops->fill_link_af) {
1400                         struct nlattr *af;
1401                         int err;
1402
1403                         if (!(af = nla_nest_start(skb, af_ops->family)))
1404                                 goto nla_put_failure;
1405
1406                         err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1407
1408                         /*
1409                          * Caller may return ENODATA to indicate that there
1410                          * was no data to be dumped. This is not an error, it
1411                          * means we should trim the attribute header and
1412                          * continue.
1413                          */
1414                         if (err == -ENODATA)
1415                                 nla_nest_cancel(skb, af);
1416                         else if (err < 0)
1417                                 goto nla_put_failure;
1418
1419                         nla_nest_end(skb, af);
1420                 }
1421         }
1422
1423         nla_nest_end(skb, af_spec);
1424
1425         nlmsg_end(skb, nlh);
1426         return 0;
1427
1428 nla_put_failure:
1429         nlmsg_cancel(skb, nlh);
1430         return -EMSGSIZE;
1431 }
1432
1433 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1434         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1435         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1436         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1437         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
1438         [IFLA_MTU]              = { .type = NLA_U32 },
1439         [IFLA_LINK]             = { .type = NLA_U32 },
1440         [IFLA_MASTER]           = { .type = NLA_U32 },
1441         [IFLA_CARRIER]          = { .type = NLA_U8 },
1442         [IFLA_TXQLEN]           = { .type = NLA_U32 },
1443         [IFLA_WEIGHT]           = { .type = NLA_U32 },
1444         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
1445         [IFLA_LINKMODE]         = { .type = NLA_U8 },
1446         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
1447         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
1448         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
1449         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
1450         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
1451         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
1452         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
1453         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
1454         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
1455         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
1456         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
1457         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
1458         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1459         [IFLA_CARRIER_CHANGES]  = { .type = NLA_U32 },  /* ignored */
1460         [IFLA_PHYS_SWITCH_ID]   = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1461         [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
1462         [IFLA_PROTO_DOWN]       = { .type = NLA_U8 },
1463         [IFLA_XDP]              = { .type = NLA_NESTED },
1464 };
1465
1466 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1467         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1468         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1469         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
1470         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
1471 };
1472
1473 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1474         [IFLA_VF_MAC]           = { .len = sizeof(struct ifla_vf_mac) },
1475         [IFLA_VF_VLAN]          = { .len = sizeof(struct ifla_vf_vlan) },
1476         [IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1477         [IFLA_VF_TX_RATE]       = { .len = sizeof(struct ifla_vf_tx_rate) },
1478         [IFLA_VF_SPOOFCHK]      = { .len = sizeof(struct ifla_vf_spoofchk) },
1479         [IFLA_VF_RATE]          = { .len = sizeof(struct ifla_vf_rate) },
1480         [IFLA_VF_LINK_STATE]    = { .len = sizeof(struct ifla_vf_link_state) },
1481         [IFLA_VF_RSS_QUERY_EN]  = { .len = sizeof(struct ifla_vf_rss_query_en) },
1482         [IFLA_VF_STATS]         = { .type = NLA_NESTED },
1483         [IFLA_VF_TRUST]         = { .len = sizeof(struct ifla_vf_trust) },
1484         [IFLA_VF_IB_NODE_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1485         [IFLA_VF_IB_PORT_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1486 };
1487
1488 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1489         [IFLA_PORT_VF]          = { .type = NLA_U32 },
1490         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
1491                                     .len = PORT_PROFILE_MAX },
1492         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
1493                                     .len = sizeof(struct ifla_port_vsi)},
1494         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1495                                       .len = PORT_UUID_MAX },
1496         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
1497                                     .len = PORT_UUID_MAX },
1498         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
1499         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
1500 };
1501
1502 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1503         [IFLA_XDP_FD]           = { .type = NLA_S32 },
1504         [IFLA_XDP_ATTACHED]     = { .type = NLA_U8 },
1505 };
1506
1507 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1508 {
1509         const struct rtnl_link_ops *ops = NULL;
1510         struct nlattr *linfo[IFLA_INFO_MAX + 1];
1511
1512         if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1513                 return NULL;
1514
1515         if (linfo[IFLA_INFO_KIND]) {
1516                 char kind[MODULE_NAME_LEN];
1517
1518                 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1519                 ops = rtnl_link_ops_get(kind);
1520         }
1521
1522         return ops;
1523 }
1524
1525 static bool link_master_filtered(struct net_device *dev, int master_idx)
1526 {
1527         struct net_device *master;
1528
1529         if (!master_idx)
1530                 return false;
1531
1532         master = netdev_master_upper_dev_get(dev);
1533         if (!master || master->ifindex != master_idx)
1534                 return true;
1535
1536         return false;
1537 }
1538
1539 static bool link_kind_filtered(const struct net_device *dev,
1540                                const struct rtnl_link_ops *kind_ops)
1541 {
1542         if (kind_ops && dev->rtnl_link_ops != kind_ops)
1543                 return true;
1544
1545         return false;
1546 }
1547
1548 static bool link_dump_filtered(struct net_device *dev,
1549                                int master_idx,
1550                                const struct rtnl_link_ops *kind_ops)
1551 {
1552         if (link_master_filtered(dev, master_idx) ||
1553             link_kind_filtered(dev, kind_ops))
1554                 return true;
1555
1556         return false;
1557 }
1558
1559 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1560 {
1561         struct net *net = sock_net(skb->sk);
1562         int h, s_h;
1563         int idx = 0, s_idx;
1564         struct net_device *dev;
1565         struct hlist_head *head;
1566         struct nlattr *tb[IFLA_MAX+1];
1567         u32 ext_filter_mask = 0;
1568         const struct rtnl_link_ops *kind_ops = NULL;
1569         unsigned int flags = NLM_F_MULTI;
1570         int master_idx = 0;
1571         int err;
1572         int hdrlen;
1573
1574         s_h = cb->args[0];
1575         s_idx = cb->args[1];
1576
1577         cb->seq = net->dev_base_seq;
1578
1579         /* A hack to preserve kernel<->userspace interface.
1580          * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1581          * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1582          * what iproute2 < v3.9.0 used.
1583          * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1584          * attribute, its netlink message is shorter than struct ifinfomsg.
1585          */
1586         hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1587                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1588
1589         if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1590
1591                 if (tb[IFLA_EXT_MASK])
1592                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1593
1594                 if (tb[IFLA_MASTER])
1595                         master_idx = nla_get_u32(tb[IFLA_MASTER]);
1596
1597                 if (tb[IFLA_LINKINFO])
1598                         kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1599
1600                 if (master_idx || kind_ops)
1601                         flags |= NLM_F_DUMP_FILTERED;
1602         }
1603
1604         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1605                 idx = 0;
1606                 head = &net->dev_index_head[h];
1607                 hlist_for_each_entry(dev, head, index_hlist) {
1608                         if (link_dump_filtered(dev, master_idx, kind_ops))
1609                                 continue;
1610                         if (idx < s_idx)
1611                                 goto cont;
1612                         err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1613                                                NETLINK_CB(cb->skb).portid,
1614                                                cb->nlh->nlmsg_seq, 0,
1615                                                flags,
1616                                                ext_filter_mask);
1617                         /* If we ran out of room on the first message,
1618                          * we're in trouble
1619                          */
1620                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1621
1622                         if (err < 0)
1623                                 goto out;
1624
1625                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1626 cont:
1627                         idx++;
1628                 }
1629         }
1630 out:
1631         cb->args[1] = idx;
1632         cb->args[0] = h;
1633
1634         return skb->len;
1635 }
1636
1637 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1638 {
1639         return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1640 }
1641 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1642
1643 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1644 {
1645         struct net *net;
1646         /* Examine the link attributes and figure out which
1647          * network namespace we are talking about.
1648          */
1649         if (tb[IFLA_NET_NS_PID])
1650                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1651         else if (tb[IFLA_NET_NS_FD])
1652                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1653         else
1654                 net = get_net(src_net);
1655         return net;
1656 }
1657 EXPORT_SYMBOL(rtnl_link_get_net);
1658
1659 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1660 {
1661         if (dev) {
1662                 if (tb[IFLA_ADDRESS] &&
1663                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1664                         return -EINVAL;
1665
1666                 if (tb[IFLA_BROADCAST] &&
1667                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1668                         return -EINVAL;
1669         }
1670
1671         if (tb[IFLA_AF_SPEC]) {
1672                 struct nlattr *af;
1673                 int rem, err;
1674
1675                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1676                         const struct rtnl_af_ops *af_ops;
1677
1678                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1679                                 return -EAFNOSUPPORT;
1680
1681                         if (!af_ops->set_link_af)
1682                                 return -EOPNOTSUPP;
1683
1684                         if (af_ops->validate_link_af) {
1685                                 err = af_ops->validate_link_af(dev, af);
1686                                 if (err < 0)
1687                                         return err;
1688                         }
1689                 }
1690         }
1691
1692         return 0;
1693 }
1694
1695 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1696                                   int guid_type)
1697 {
1698         const struct net_device_ops *ops = dev->netdev_ops;
1699
1700         return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1701 }
1702
1703 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1704 {
1705         if (dev->type != ARPHRD_INFINIBAND)
1706                 return -EOPNOTSUPP;
1707
1708         return handle_infiniband_guid(dev, ivt, guid_type);
1709 }
1710
1711 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1712 {
1713         const struct net_device_ops *ops = dev->netdev_ops;
1714         int err = -EINVAL;
1715
1716         if (tb[IFLA_VF_MAC]) {
1717                 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1718
1719                 err = -EOPNOTSUPP;
1720                 if (ops->ndo_set_vf_mac)
1721                         err = ops->ndo_set_vf_mac(dev, ivm->vf,
1722                                                   ivm->mac);
1723                 if (err < 0)
1724                         return err;
1725         }
1726
1727         if (tb[IFLA_VF_VLAN]) {
1728                 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1729
1730                 err = -EOPNOTSUPP;
1731                 if (ops->ndo_set_vf_vlan)
1732                         err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1733                                                    ivv->qos,
1734                                                    htons(ETH_P_8021Q));
1735                 if (err < 0)
1736                         return err;
1737         }
1738
1739         if (tb[IFLA_VF_VLAN_LIST]) {
1740                 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1741                 struct nlattr *attr;
1742                 int rem, len = 0;
1743
1744                 err = -EOPNOTSUPP;
1745                 if (!ops->ndo_set_vf_vlan)
1746                         return err;
1747
1748                 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1749                         if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1750                             nla_len(attr) < NLA_HDRLEN) {
1751                                 return -EINVAL;
1752                         }
1753                         if (len >= MAX_VLAN_LIST_LEN)
1754                                 return -EOPNOTSUPP;
1755                         ivvl[len] = nla_data(attr);
1756
1757                         len++;
1758                 }
1759                 if (len == 0)
1760                         return -EINVAL;
1761
1762                 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1763                                            ivvl[0]->qos, ivvl[0]->vlan_proto);
1764                 if (err < 0)
1765                         return err;
1766         }
1767
1768         if (tb[IFLA_VF_TX_RATE]) {
1769                 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1770                 struct ifla_vf_info ivf;
1771
1772                 err = -EOPNOTSUPP;
1773                 if (ops->ndo_get_vf_config)
1774                         err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1775                 if (err < 0)
1776                         return err;
1777
1778                 err = -EOPNOTSUPP;
1779                 if (ops->ndo_set_vf_rate)
1780                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1781                                                    ivf.min_tx_rate,
1782                                                    ivt->rate);
1783                 if (err < 0)
1784                         return err;
1785         }
1786
1787         if (tb[IFLA_VF_RATE]) {
1788                 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1789
1790                 err = -EOPNOTSUPP;
1791                 if (ops->ndo_set_vf_rate)
1792                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1793                                                    ivt->min_tx_rate,
1794                                                    ivt->max_tx_rate);
1795                 if (err < 0)
1796                         return err;
1797         }
1798
1799         if (tb[IFLA_VF_SPOOFCHK]) {
1800                 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1801
1802                 err = -EOPNOTSUPP;
1803                 if (ops->ndo_set_vf_spoofchk)
1804                         err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1805                                                        ivs->setting);
1806                 if (err < 0)
1807                         return err;
1808         }
1809
1810         if (tb[IFLA_VF_LINK_STATE]) {
1811                 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1812
1813                 err = -EOPNOTSUPP;
1814                 if (ops->ndo_set_vf_link_state)
1815                         err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1816                                                          ivl->link_state);
1817                 if (err < 0)
1818                         return err;
1819         }
1820
1821         if (tb[IFLA_VF_RSS_QUERY_EN]) {
1822                 struct ifla_vf_rss_query_en *ivrssq_en;
1823
1824                 err = -EOPNOTSUPP;
1825                 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1826                 if (ops->ndo_set_vf_rss_query_en)
1827                         err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1828                                                            ivrssq_en->setting);
1829                 if (err < 0)
1830                         return err;
1831         }
1832
1833         if (tb[IFLA_VF_TRUST]) {
1834                 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1835
1836                 err = -EOPNOTSUPP;
1837                 if (ops->ndo_set_vf_trust)
1838                         err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1839                 if (err < 0)
1840                         return err;
1841         }
1842
1843         if (tb[IFLA_VF_IB_NODE_GUID]) {
1844                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1845
1846                 if (!ops->ndo_set_vf_guid)
1847                         return -EOPNOTSUPP;
1848
1849                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1850         }
1851
1852         if (tb[IFLA_VF_IB_PORT_GUID]) {
1853                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1854
1855                 if (!ops->ndo_set_vf_guid)
1856                         return -EOPNOTSUPP;
1857
1858                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1859         }
1860
1861         return err;
1862 }
1863
1864 static int do_set_master(struct net_device *dev, int ifindex)
1865 {
1866         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1867         const struct net_device_ops *ops;
1868         int err;
1869
1870         if (upper_dev) {
1871                 if (upper_dev->ifindex == ifindex)
1872                         return 0;
1873                 ops = upper_dev->netdev_ops;
1874                 if (ops->ndo_del_slave) {
1875                         err = ops->ndo_del_slave(upper_dev, dev);
1876                         if (err)
1877                                 return err;
1878                 } else {
1879                         return -EOPNOTSUPP;
1880                 }
1881         }
1882
1883         if (ifindex) {
1884                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1885                 if (!upper_dev)
1886                         return -EINVAL;
1887                 ops = upper_dev->netdev_ops;
1888                 if (ops->ndo_add_slave) {
1889                         err = ops->ndo_add_slave(upper_dev, dev);
1890                         if (err)
1891                                 return err;
1892                 } else {
1893                         return -EOPNOTSUPP;
1894                 }
1895         }
1896         return 0;
1897 }
1898
1899 #define DO_SETLINK_MODIFIED     0x01
1900 /* notify flag means notify + modified. */
1901 #define DO_SETLINK_NOTIFY       0x03
1902 static int do_setlink(const struct sk_buff *skb,
1903                       struct net_device *dev, struct ifinfomsg *ifm,
1904                       struct nlattr **tb, char *ifname, int status)
1905 {
1906         const struct net_device_ops *ops = dev->netdev_ops;
1907         int err;
1908
1909         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1910                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1911                 if (IS_ERR(net)) {
1912                         err = PTR_ERR(net);
1913                         goto errout;
1914                 }
1915                 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1916                         put_net(net);
1917                         err = -EPERM;
1918                         goto errout;
1919                 }
1920                 err = dev_change_net_namespace(dev, net, ifname);
1921                 put_net(net);
1922                 if (err)
1923                         goto errout;
1924                 status |= DO_SETLINK_MODIFIED;
1925         }
1926
1927         if (tb[IFLA_MAP]) {
1928                 struct rtnl_link_ifmap *u_map;
1929                 struct ifmap k_map;
1930
1931                 if (!ops->ndo_set_config) {
1932                         err = -EOPNOTSUPP;
1933                         goto errout;
1934                 }
1935
1936                 if (!netif_device_present(dev)) {
1937                         err = -ENODEV;
1938                         goto errout;
1939                 }
1940
1941                 u_map = nla_data(tb[IFLA_MAP]);
1942                 k_map.mem_start = (unsigned long) u_map->mem_start;
1943                 k_map.mem_end = (unsigned long) u_map->mem_end;
1944                 k_map.base_addr = (unsigned short) u_map->base_addr;
1945                 k_map.irq = (unsigned char) u_map->irq;
1946                 k_map.dma = (unsigned char) u_map->dma;
1947                 k_map.port = (unsigned char) u_map->port;
1948
1949                 err = ops->ndo_set_config(dev, &k_map);
1950                 if (err < 0)
1951                         goto errout;
1952
1953                 status |= DO_SETLINK_NOTIFY;
1954         }
1955
1956         if (tb[IFLA_ADDRESS]) {
1957                 struct sockaddr *sa;
1958                 int len;
1959
1960                 len = sizeof(sa_family_t) + dev->addr_len;
1961                 sa = kmalloc(len, GFP_KERNEL);
1962                 if (!sa) {
1963                         err = -ENOMEM;
1964                         goto errout;
1965                 }
1966                 sa->sa_family = dev->type;
1967                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1968                        dev->addr_len);
1969                 err = dev_set_mac_address(dev, sa);
1970                 kfree(sa);
1971                 if (err)
1972                         goto errout;
1973                 status |= DO_SETLINK_MODIFIED;
1974         }
1975
1976         if (tb[IFLA_MTU]) {
1977                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1978                 if (err < 0)
1979                         goto errout;
1980                 status |= DO_SETLINK_MODIFIED;
1981         }
1982
1983         if (tb[IFLA_GROUP]) {
1984                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1985                 status |= DO_SETLINK_NOTIFY;
1986         }
1987
1988         /*
1989          * Interface selected by interface index but interface
1990          * name provided implies that a name change has been
1991          * requested.
1992          */
1993         if (ifm->ifi_index > 0 && ifname[0]) {
1994                 err = dev_change_name(dev, ifname);
1995                 if (err < 0)
1996                         goto errout;
1997                 status |= DO_SETLINK_MODIFIED;
1998         }
1999
2000         if (tb[IFLA_IFALIAS]) {
2001                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2002                                     nla_len(tb[IFLA_IFALIAS]));
2003                 if (err < 0)
2004                         goto errout;
2005                 status |= DO_SETLINK_NOTIFY;
2006         }
2007
2008         if (tb[IFLA_BROADCAST]) {
2009                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2010                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2011         }
2012
2013         if (ifm->ifi_flags || ifm->ifi_change) {
2014                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2015                 if (err < 0)
2016                         goto errout;
2017         }
2018
2019         if (tb[IFLA_MASTER]) {
2020                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2021                 if (err)
2022                         goto errout;
2023                 status |= DO_SETLINK_MODIFIED;
2024         }
2025
2026         if (tb[IFLA_CARRIER]) {
2027                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2028                 if (err)
2029                         goto errout;
2030                 status |= DO_SETLINK_MODIFIED;
2031         }
2032
2033         if (tb[IFLA_TXQLEN]) {
2034                 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
2035                 unsigned long orig_len = dev->tx_queue_len;
2036
2037                 if (dev->tx_queue_len ^ value) {
2038                         dev->tx_queue_len = value;
2039                         err = call_netdevice_notifiers(
2040                               NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2041                         err = notifier_to_errno(err);
2042                         if (err) {
2043                                 dev->tx_queue_len = orig_len;
2044                                 goto errout;
2045                         }
2046                         status |= DO_SETLINK_NOTIFY;
2047                 }
2048         }
2049
2050         if (tb[IFLA_OPERSTATE])
2051                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2052
2053         if (tb[IFLA_LINKMODE]) {
2054                 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2055
2056                 write_lock_bh(&dev_base_lock);
2057                 if (dev->link_mode ^ value)
2058                         status |= DO_SETLINK_NOTIFY;
2059                 dev->link_mode = value;
2060                 write_unlock_bh(&dev_base_lock);
2061         }
2062
2063         if (tb[IFLA_VFINFO_LIST]) {
2064                 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2065                 struct nlattr *attr;
2066                 int rem;
2067
2068                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2069                         if (nla_type(attr) != IFLA_VF_INFO ||
2070                             nla_len(attr) < NLA_HDRLEN) {
2071                                 err = -EINVAL;
2072                                 goto errout;
2073                         }
2074                         err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2075                                                ifla_vf_policy);
2076                         if (err < 0)
2077                                 goto errout;
2078                         err = do_setvfinfo(dev, vfinfo);
2079                         if (err < 0)
2080                                 goto errout;
2081                         status |= DO_SETLINK_NOTIFY;
2082                 }
2083         }
2084         err = 0;
2085
2086         if (tb[IFLA_VF_PORTS]) {
2087                 struct nlattr *port[IFLA_PORT_MAX+1];
2088                 struct nlattr *attr;
2089                 int vf;
2090                 int rem;
2091
2092                 err = -EOPNOTSUPP;
2093                 if (!ops->ndo_set_vf_port)
2094                         goto errout;
2095
2096                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2097                         if (nla_type(attr) != IFLA_VF_PORT ||
2098                             nla_len(attr) < NLA_HDRLEN) {
2099                                 err = -EINVAL;
2100                                 goto errout;
2101                         }
2102                         err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2103                                                ifla_port_policy);
2104                         if (err < 0)
2105                                 goto errout;
2106                         if (!port[IFLA_PORT_VF]) {
2107                                 err = -EOPNOTSUPP;
2108                                 goto errout;
2109                         }
2110                         vf = nla_get_u32(port[IFLA_PORT_VF]);
2111                         err = ops->ndo_set_vf_port(dev, vf, port);
2112                         if (err < 0)
2113                                 goto errout;
2114                         status |= DO_SETLINK_NOTIFY;
2115                 }
2116         }
2117         err = 0;
2118
2119         if (tb[IFLA_PORT_SELF]) {
2120                 struct nlattr *port[IFLA_PORT_MAX+1];
2121
2122                 err = nla_parse_nested(port, IFLA_PORT_MAX,
2123                         tb[IFLA_PORT_SELF], ifla_port_policy);
2124                 if (err < 0)
2125                         goto errout;
2126
2127                 err = -EOPNOTSUPP;
2128                 if (ops->ndo_set_vf_port)
2129                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2130                 if (err < 0)
2131                         goto errout;
2132                 status |= DO_SETLINK_NOTIFY;
2133         }
2134
2135         if (tb[IFLA_AF_SPEC]) {
2136                 struct nlattr *af;
2137                 int rem;
2138
2139                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2140                         const struct rtnl_af_ops *af_ops;
2141
2142                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2143                                 BUG();
2144
2145                         err = af_ops->set_link_af(dev, af);
2146                         if (err < 0)
2147                                 goto errout;
2148
2149                         status |= DO_SETLINK_NOTIFY;
2150                 }
2151         }
2152         err = 0;
2153
2154         if (tb[IFLA_PROTO_DOWN]) {
2155                 err = dev_change_proto_down(dev,
2156                                             nla_get_u8(tb[IFLA_PROTO_DOWN]));
2157                 if (err)
2158                         goto errout;
2159                 status |= DO_SETLINK_NOTIFY;
2160         }
2161
2162         if (tb[IFLA_XDP]) {
2163                 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2164
2165                 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2166                                        ifla_xdp_policy);
2167                 if (err < 0)
2168                         goto errout;
2169
2170                 if (xdp[IFLA_XDP_ATTACHED]) {
2171                         err = -EINVAL;
2172                         goto errout;
2173                 }
2174                 if (xdp[IFLA_XDP_FD]) {
2175                         err = dev_change_xdp_fd(dev,
2176                                                 nla_get_s32(xdp[IFLA_XDP_FD]));
2177                         if (err)
2178                                 goto errout;
2179                         status |= DO_SETLINK_NOTIFY;
2180                 }
2181         }
2182
2183 errout:
2184         if (status & DO_SETLINK_MODIFIED) {
2185                 if (status & DO_SETLINK_NOTIFY)
2186                         netdev_state_change(dev);
2187
2188                 if (err < 0)
2189                         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",
2190                                              dev->name);
2191         }
2192
2193         return err;
2194 }
2195
2196 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2197 {
2198         struct net *net = sock_net(skb->sk);
2199         struct ifinfomsg *ifm;
2200         struct net_device *dev;
2201         int err;
2202         struct nlattr *tb[IFLA_MAX+1];
2203         char ifname[IFNAMSIZ];
2204
2205         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2206         if (err < 0)
2207                 goto errout;
2208
2209         if (tb[IFLA_IFNAME])
2210                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2211         else
2212                 ifname[0] = '\0';
2213
2214         err = -EINVAL;
2215         ifm = nlmsg_data(nlh);
2216         if (ifm->ifi_index > 0)
2217                 dev = __dev_get_by_index(net, ifm->ifi_index);
2218         else if (tb[IFLA_IFNAME])
2219                 dev = __dev_get_by_name(net, ifname);
2220         else
2221                 goto errout;
2222
2223         if (dev == NULL) {
2224                 err = -ENODEV;
2225                 goto errout;
2226         }
2227
2228         err = validate_linkmsg(dev, tb);
2229         if (err < 0)
2230                 goto errout;
2231
2232         err = do_setlink(skb, dev, ifm, tb, ifname, 0);
2233 errout:
2234         return err;
2235 }
2236
2237 static int rtnl_group_dellink(const struct net *net, int group)
2238 {
2239         struct net_device *dev, *aux;
2240         LIST_HEAD(list_kill);
2241         bool found = false;
2242
2243         if (!group)
2244                 return -EPERM;
2245
2246         for_each_netdev(net, dev) {
2247                 if (dev->group == group) {
2248                         const struct rtnl_link_ops *ops;
2249
2250                         found = true;
2251                         ops = dev->rtnl_link_ops;
2252                         if (!ops || !ops->dellink)
2253                                 return -EOPNOTSUPP;
2254                 }
2255         }
2256
2257         if (!found)
2258                 return -ENODEV;
2259
2260         for_each_netdev_safe(net, dev, aux) {
2261                 if (dev->group == group) {
2262                         const struct rtnl_link_ops *ops;
2263
2264                         ops = dev->rtnl_link_ops;
2265                         ops->dellink(dev, &list_kill);
2266                 }
2267         }
2268         unregister_netdevice_many(&list_kill);
2269
2270         return 0;
2271 }
2272
2273 int rtnl_delete_link(struct net_device *dev)
2274 {
2275         const struct rtnl_link_ops *ops;
2276         LIST_HEAD(list_kill);
2277
2278         ops = dev->rtnl_link_ops;
2279         if (!ops || !ops->dellink)
2280                 return -EOPNOTSUPP;
2281
2282         ops->dellink(dev, &list_kill);
2283         unregister_netdevice_many(&list_kill);
2284
2285         return 0;
2286 }
2287 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2288
2289 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2290 {
2291         struct net *net = sock_net(skb->sk);
2292         struct net_device *dev;
2293         struct ifinfomsg *ifm;
2294         char ifname[IFNAMSIZ];
2295         struct nlattr *tb[IFLA_MAX+1];
2296         int err;
2297
2298         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2299         if (err < 0)
2300                 return err;
2301
2302         if (tb[IFLA_IFNAME])
2303                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2304
2305         ifm = nlmsg_data(nlh);
2306         if (ifm->ifi_index > 0)
2307                 dev = __dev_get_by_index(net, ifm->ifi_index);
2308         else if (tb[IFLA_IFNAME])
2309                 dev = __dev_get_by_name(net, ifname);
2310         else if (tb[IFLA_GROUP])
2311                 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2312         else
2313                 return -EINVAL;
2314
2315         if (!dev)
2316                 return -ENODEV;
2317
2318         return rtnl_delete_link(dev);
2319 }
2320
2321 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2322 {
2323         unsigned int old_flags;
2324         int err;
2325
2326         old_flags = dev->flags;
2327         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2328                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2329                 if (err < 0)
2330                         return err;
2331         }
2332
2333         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2334
2335         __dev_notify_flags(dev, old_flags, ~0U);
2336         return 0;
2337 }
2338 EXPORT_SYMBOL(rtnl_configure_link);
2339
2340 struct net_device *rtnl_create_link(struct net *net,
2341         const char *ifname, unsigned char name_assign_type,
2342         const struct rtnl_link_ops *ops, struct nlattr *tb[])
2343 {
2344         int err;
2345         struct net_device *dev;
2346         unsigned int num_tx_queues = 1;
2347         unsigned int num_rx_queues = 1;
2348
2349         if (tb[IFLA_NUM_TX_QUEUES])
2350                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2351         else if (ops->get_num_tx_queues)
2352                 num_tx_queues = ops->get_num_tx_queues();
2353
2354         if (tb[IFLA_NUM_RX_QUEUES])
2355                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2356         else if (ops->get_num_rx_queues)
2357                 num_rx_queues = ops->get_num_rx_queues();
2358
2359         err = -ENOMEM;
2360         dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2361                                ops->setup, num_tx_queues, num_rx_queues);
2362         if (!dev)
2363                 goto err;
2364
2365         dev_net_set(dev, net);
2366         dev->rtnl_link_ops = ops;
2367         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2368
2369         if (tb[IFLA_MTU])
2370                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2371         if (tb[IFLA_ADDRESS]) {
2372                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2373                                 nla_len(tb[IFLA_ADDRESS]));
2374                 dev->addr_assign_type = NET_ADDR_SET;
2375         }
2376         if (tb[IFLA_BROADCAST])
2377                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2378                                 nla_len(tb[IFLA_BROADCAST]));
2379         if (tb[IFLA_TXQLEN])
2380                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2381         if (tb[IFLA_OPERSTATE])
2382                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2383         if (tb[IFLA_LINKMODE])
2384                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2385         if (tb[IFLA_GROUP])
2386                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2387
2388         return dev;
2389
2390 err:
2391         return ERR_PTR(err);
2392 }
2393 EXPORT_SYMBOL(rtnl_create_link);
2394
2395 static int rtnl_group_changelink(const struct sk_buff *skb,
2396                 struct net *net, int group,
2397                 struct ifinfomsg *ifm,
2398                 struct nlattr **tb)
2399 {
2400         struct net_device *dev, *aux;
2401         int err;
2402
2403         for_each_netdev_safe(net, dev, aux) {
2404                 if (dev->group == group) {
2405                         err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2406                         if (err < 0)
2407                                 return err;
2408                 }
2409         }
2410
2411         return 0;
2412 }
2413
2414 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2415 {
2416         struct net *net = sock_net(skb->sk);
2417         const struct rtnl_link_ops *ops;
2418         const struct rtnl_link_ops *m_ops = NULL;
2419         struct net_device *dev;
2420         struct net_device *master_dev = NULL;
2421         struct ifinfomsg *ifm;
2422         char kind[MODULE_NAME_LEN];
2423         char ifname[IFNAMSIZ];
2424         struct nlattr *tb[IFLA_MAX+1];
2425         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2426         unsigned char name_assign_type = NET_NAME_USER;
2427         int err;
2428
2429 #ifdef CONFIG_MODULES
2430 replay:
2431 #endif
2432         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2433         if (err < 0)
2434                 return err;
2435
2436         if (tb[IFLA_IFNAME])
2437                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2438         else
2439                 ifname[0] = '\0';
2440
2441         ifm = nlmsg_data(nlh);
2442         if (ifm->ifi_index > 0)
2443                 dev = __dev_get_by_index(net, ifm->ifi_index);
2444         else {
2445                 if (ifname[0])
2446                         dev = __dev_get_by_name(net, ifname);
2447                 else
2448                         dev = NULL;
2449         }
2450
2451         if (dev) {
2452                 master_dev = netdev_master_upper_dev_get(dev);
2453                 if (master_dev)
2454                         m_ops = master_dev->rtnl_link_ops;
2455         }
2456
2457         err = validate_linkmsg(dev, tb);
2458         if (err < 0)
2459                 return err;
2460
2461         if (tb[IFLA_LINKINFO]) {
2462                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2463                                        tb[IFLA_LINKINFO], ifla_info_policy);
2464                 if (err < 0)
2465                         return err;
2466         } else
2467                 memset(linkinfo, 0, sizeof(linkinfo));
2468
2469         if (linkinfo[IFLA_INFO_KIND]) {
2470                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2471                 ops = rtnl_link_ops_get(kind);
2472         } else {
2473                 kind[0] = '\0';
2474                 ops = NULL;
2475         }
2476
2477         if (1) {
2478                 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2479                 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2480                 struct nlattr **data = NULL;
2481                 struct nlattr **slave_data = NULL;
2482                 struct net *dest_net, *link_net = NULL;
2483
2484                 if (ops) {
2485                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2486                                 err = nla_parse_nested(attr, ops->maxtype,
2487                                                        linkinfo[IFLA_INFO_DATA],
2488                                                        ops->policy);
2489                                 if (err < 0)
2490                                         return err;
2491                                 data = attr;
2492                         }
2493                         if (ops->validate) {
2494                                 err = ops->validate(tb, data);
2495                                 if (err < 0)
2496                                         return err;
2497                         }
2498                 }
2499
2500                 if (m_ops) {
2501                         if (m_ops->slave_maxtype &&
2502                             linkinfo[IFLA_INFO_SLAVE_DATA]) {
2503                                 err = nla_parse_nested(slave_attr,
2504                                                        m_ops->slave_maxtype,
2505                                                        linkinfo[IFLA_INFO_SLAVE_DATA],
2506                                                        m_ops->slave_policy);
2507                                 if (err < 0)
2508                                         return err;
2509                                 slave_data = slave_attr;
2510                         }
2511                         if (m_ops->slave_validate) {
2512                                 err = m_ops->slave_validate(tb, slave_data);
2513                                 if (err < 0)
2514                                         return err;
2515                         }
2516                 }
2517
2518                 if (dev) {
2519                         int status = 0;
2520
2521                         if (nlh->nlmsg_flags & NLM_F_EXCL)
2522                                 return -EEXIST;
2523                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
2524                                 return -EOPNOTSUPP;
2525
2526                         if (linkinfo[IFLA_INFO_DATA]) {
2527                                 if (!ops || ops != dev->rtnl_link_ops ||
2528                                     !ops->changelink)
2529                                         return -EOPNOTSUPP;
2530
2531                                 err = ops->changelink(dev, tb, data);
2532                                 if (err < 0)
2533                                         return err;
2534                                 status |= DO_SETLINK_NOTIFY;
2535                         }
2536
2537                         if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2538                                 if (!m_ops || !m_ops->slave_changelink)
2539                                         return -EOPNOTSUPP;
2540
2541                                 err = m_ops->slave_changelink(master_dev, dev,
2542                                                               tb, slave_data);
2543                                 if (err < 0)
2544                                         return err;
2545                                 status |= DO_SETLINK_NOTIFY;
2546                         }
2547
2548                         return do_setlink(skb, dev, ifm, tb, ifname, status);
2549                 }
2550
2551                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2552                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2553                                 return rtnl_group_changelink(skb, net,
2554                                                 nla_get_u32(tb[IFLA_GROUP]),
2555                                                 ifm, tb);
2556                         return -ENODEV;
2557                 }
2558
2559                 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2560                         return -EOPNOTSUPP;
2561
2562                 if (!ops) {
2563 #ifdef CONFIG_MODULES
2564                         if (kind[0]) {
2565                                 __rtnl_unlock();
2566                                 request_module("rtnl-link-%s", kind);
2567                                 rtnl_lock();
2568                                 ops = rtnl_link_ops_get(kind);
2569                                 if (ops)
2570                                         goto replay;
2571                         }
2572 #endif
2573                         return -EOPNOTSUPP;
2574                 }
2575
2576                 if (!ops->setup)
2577                         return -EOPNOTSUPP;
2578
2579                 if (!ifname[0]) {
2580                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2581                         name_assign_type = NET_NAME_ENUM;
2582                 }
2583
2584                 dest_net = rtnl_link_get_net(net, tb);
2585                 if (IS_ERR(dest_net))
2586                         return PTR_ERR(dest_net);
2587
2588                 err = -EPERM;
2589                 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2590                         goto out;
2591
2592                 if (tb[IFLA_LINK_NETNSID]) {
2593                         int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2594
2595                         link_net = get_net_ns_by_id(dest_net, id);
2596                         if (!link_net) {
2597                                 err =  -EINVAL;
2598                                 goto out;
2599                         }
2600                         err = -EPERM;
2601                         if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2602                                 goto out;
2603                 }
2604
2605                 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2606                                        name_assign_type, ops, tb);
2607                 if (IS_ERR(dev)) {
2608                         err = PTR_ERR(dev);
2609                         goto out;
2610                 }
2611
2612                 dev->ifindex = ifm->ifi_index;
2613
2614                 if (ops->newlink) {
2615                         err = ops->newlink(link_net ? : net, dev, tb, data);
2616                         /* Drivers should call free_netdev() in ->destructor
2617                          * and unregister it on failure after registration
2618                          * so that device could be finally freed in rtnl_unlock.
2619                          */
2620                         if (err < 0) {
2621                                 /* If device is not registered at all, free it now */
2622                                 if (dev->reg_state == NETREG_UNINITIALIZED)
2623                                         free_netdev(dev);
2624                                 goto out;
2625                         }
2626                 } else {
2627                         err = register_netdevice(dev);
2628                         if (err < 0) {
2629                                 free_netdev(dev);
2630                                 goto out;
2631                         }
2632                 }
2633                 err = rtnl_configure_link(dev, ifm);
2634                 if (err < 0)
2635                         goto out_unregister;
2636                 if (link_net) {
2637                         err = dev_change_net_namespace(dev, dest_net, ifname);
2638                         if (err < 0)
2639                                 goto out_unregister;
2640                 }
2641 out:
2642                 if (link_net)
2643                         put_net(link_net);
2644                 put_net(dest_net);
2645                 return err;
2646 out_unregister:
2647                 if (ops->newlink) {
2648                         LIST_HEAD(list_kill);
2649
2650                         ops->dellink(dev, &list_kill);
2651                         unregister_netdevice_many(&list_kill);
2652                 } else {
2653                         unregister_netdevice(dev);
2654                 }
2655                 goto out;
2656         }
2657 }
2658
2659 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2660 {
2661         struct net *net = sock_net(skb->sk);
2662         struct ifinfomsg *ifm;
2663         char ifname[IFNAMSIZ];
2664         struct nlattr *tb[IFLA_MAX+1];
2665         struct net_device *dev = NULL;
2666         struct sk_buff *nskb;
2667         int err;
2668         u32 ext_filter_mask = 0;
2669
2670         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2671         if (err < 0)
2672                 return err;
2673
2674         if (tb[IFLA_IFNAME])
2675                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2676
2677         if (tb[IFLA_EXT_MASK])
2678                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2679
2680         ifm = nlmsg_data(nlh);
2681         if (ifm->ifi_index > 0)
2682                 dev = __dev_get_by_index(net, ifm->ifi_index);
2683         else if (tb[IFLA_IFNAME])
2684                 dev = __dev_get_by_name(net, ifname);
2685         else
2686                 return -EINVAL;
2687
2688         if (dev == NULL)
2689                 return -ENODEV;
2690
2691         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2692         if (nskb == NULL)
2693                 return -ENOBUFS;
2694
2695         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2696                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2697         if (err < 0) {
2698                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2699                 WARN_ON(err == -EMSGSIZE);
2700                 kfree_skb(nskb);
2701         } else
2702                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2703
2704         return err;
2705 }
2706
2707 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2708 {
2709         struct net *net = sock_net(skb->sk);
2710         struct net_device *dev;
2711         struct nlattr *tb[IFLA_MAX+1];
2712         u32 ext_filter_mask = 0;
2713         u16 min_ifinfo_dump_size = 0;
2714         int hdrlen;
2715
2716         /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2717         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2718                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2719
2720         if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2721                 if (tb[IFLA_EXT_MASK])
2722                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2723         }
2724
2725         if (!ext_filter_mask)
2726                 return NLMSG_GOODSIZE;
2727         /*
2728          * traverse the list of net devices and compute the minimum
2729          * buffer size based upon the filter mask.
2730          */
2731         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2732                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2733                                              if_nlmsg_size(dev,
2734                                                            ext_filter_mask));
2735         }
2736
2737         return min_ifinfo_dump_size;
2738 }
2739
2740 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2741 {
2742         int idx;
2743         int s_idx = cb->family;
2744
2745         if (s_idx == 0)
2746                 s_idx = 1;
2747         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2748                 int type = cb->nlh->nlmsg_type-RTM_BASE;
2749                 if (idx < s_idx || idx == PF_PACKET)
2750                         continue;
2751                 if (rtnl_msg_handlers[idx] == NULL ||
2752                     rtnl_msg_handlers[idx][type].dumpit == NULL)
2753                         continue;
2754                 if (idx > s_idx) {
2755                         memset(&cb->args[0], 0, sizeof(cb->args));
2756                         cb->prev_seq = 0;
2757                         cb->seq = 0;
2758                 }
2759                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2760                         break;
2761         }
2762         cb->family = idx;
2763
2764         return skb->len;
2765 }
2766
2767 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2768                                        unsigned int change, gfp_t flags)
2769 {
2770         struct net *net = dev_net(dev);
2771         struct sk_buff *skb;
2772         int err = -ENOBUFS;
2773         size_t if_info_size;
2774
2775         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2776         if (skb == NULL)
2777                 goto errout;
2778
2779         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2780         if (err < 0) {
2781                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2782                 WARN_ON(err == -EMSGSIZE);
2783                 kfree_skb(skb);
2784                 goto errout;
2785         }
2786         return skb;
2787 errout:
2788         if (err < 0)
2789                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2790         return NULL;
2791 }
2792
2793 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2794 {
2795         struct net *net = dev_net(dev);
2796
2797         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2798 }
2799
2800 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2801                   gfp_t flags)
2802 {
2803         struct sk_buff *skb;
2804
2805         if (dev->reg_state != NETREG_REGISTERED)
2806                 return;
2807
2808         skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2809         if (skb)
2810                 rtmsg_ifinfo_send(skb, dev, flags);
2811 }
2812 EXPORT_SYMBOL(rtmsg_ifinfo);
2813
2814 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2815                                    struct net_device *dev,
2816                                    u8 *addr, u16 vid, u32 pid, u32 seq,
2817                                    int type, unsigned int flags,
2818                                    int nlflags, u16 ndm_state)
2819 {
2820         struct nlmsghdr *nlh;
2821         struct ndmsg *ndm;
2822
2823         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2824         if (!nlh)
2825                 return -EMSGSIZE;
2826
2827         ndm = nlmsg_data(nlh);
2828         ndm->ndm_family  = AF_BRIDGE;
2829         ndm->ndm_pad1    = 0;
2830         ndm->ndm_pad2    = 0;
2831         ndm->ndm_flags   = flags;
2832         ndm->ndm_type    = 0;
2833         ndm->ndm_ifindex = dev->ifindex;
2834         ndm->ndm_state   = ndm_state;
2835
2836         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2837                 goto nla_put_failure;
2838         if (vid)
2839                 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2840                         goto nla_put_failure;
2841
2842         nlmsg_end(skb, nlh);
2843         return 0;
2844
2845 nla_put_failure:
2846         nlmsg_cancel(skb, nlh);
2847         return -EMSGSIZE;
2848 }
2849
2850 static inline size_t rtnl_fdb_nlmsg_size(void)
2851 {
2852         return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2853 }
2854
2855 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2856                             u16 ndm_state)
2857 {
2858         struct net *net = dev_net(dev);
2859         struct sk_buff *skb;
2860         int err = -ENOBUFS;
2861
2862         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2863         if (!skb)
2864                 goto errout;
2865
2866         err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2867                                       0, 0, type, NTF_SELF, 0, ndm_state);
2868         if (err < 0) {
2869                 kfree_skb(skb);
2870                 goto errout;
2871         }
2872
2873         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2874         return;
2875 errout:
2876         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2877 }
2878
2879 /**
2880  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2881  */
2882 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2883                      struct nlattr *tb[],
2884                      struct net_device *dev,
2885                      const unsigned char *addr, u16 vid,
2886                      u16 flags)
2887 {
2888         int err = -EINVAL;
2889
2890         /* If aging addresses are supported device will need to
2891          * implement its own handler for this.
2892          */
2893         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2894                 pr_info("%s: FDB only supports static addresses\n", dev->name);
2895                 return err;
2896         }
2897
2898         if (vid) {
2899                 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2900                 return err;
2901         }
2902
2903         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2904                 err = dev_uc_add_excl(dev, addr);
2905         else if (is_multicast_ether_addr(addr))
2906                 err = dev_mc_add_excl(dev, addr);
2907
2908         /* Only return duplicate errors if NLM_F_EXCL is set */
2909         if (err == -EEXIST && !(flags & NLM_F_EXCL))
2910                 err = 0;
2911
2912         return err;
2913 }
2914 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2915
2916 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2917 {
2918         u16 vid = 0;
2919
2920         if (vlan_attr) {
2921                 if (nla_len(vlan_attr) != sizeof(u16)) {
2922                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2923                         return -EINVAL;
2924                 }
2925
2926                 vid = nla_get_u16(vlan_attr);
2927
2928                 if (!vid || vid >= VLAN_VID_MASK) {
2929                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2930                                 vid);
2931                         return -EINVAL;
2932                 }
2933         }
2934         *p_vid = vid;
2935         return 0;
2936 }
2937
2938 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2939 {
2940         struct net *net = sock_net(skb->sk);
2941         struct ndmsg *ndm;
2942         struct nlattr *tb[NDA_MAX+1];
2943         struct net_device *dev;
2944         u8 *addr;
2945         u16 vid;
2946         int err;
2947
2948         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2949         if (err < 0)
2950                 return err;
2951
2952         ndm = nlmsg_data(nlh);
2953         if (ndm->ndm_ifindex == 0) {
2954                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2955                 return -EINVAL;
2956         }
2957
2958         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2959         if (dev == NULL) {
2960                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2961                 return -ENODEV;
2962         }
2963
2964         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2965                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2966                 return -EINVAL;
2967         }
2968
2969         addr = nla_data(tb[NDA_LLADDR]);
2970
2971         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2972         if (err)
2973                 return err;
2974
2975         err = -EOPNOTSUPP;
2976
2977         /* Support fdb on master device the net/bridge default case */
2978         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2979             (dev->priv_flags & IFF_BRIDGE_PORT)) {
2980                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2981                 const struct net_device_ops *ops = br_dev->netdev_ops;
2982
2983                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2984                                        nlh->nlmsg_flags);
2985                 if (err)
2986                         goto out;
2987                 else
2988                         ndm->ndm_flags &= ~NTF_MASTER;
2989         }
2990
2991         /* Embedded bridge, macvlan, and any other device support */
2992         if ((ndm->ndm_flags & NTF_SELF)) {
2993                 if (dev->netdev_ops->ndo_fdb_add)
2994                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2995                                                            vid,
2996                                                            nlh->nlmsg_flags);
2997                 else
2998                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2999                                                nlh->nlmsg_flags);
3000
3001                 if (!err) {
3002                         rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3003                                         ndm->ndm_state);
3004                         ndm->ndm_flags &= ~NTF_SELF;
3005                 }
3006         }
3007 out:
3008         return err;
3009 }
3010
3011 /**
3012  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3013  */
3014 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3015                      struct nlattr *tb[],
3016                      struct net_device *dev,
3017                      const unsigned char *addr, u16 vid)
3018 {
3019         int err = -EINVAL;
3020
3021         /* If aging addresses are supported device will need to
3022          * implement its own handler for this.
3023          */
3024         if (!(ndm->ndm_state & NUD_PERMANENT)) {
3025                 pr_info("%s: FDB only supports static addresses\n", dev->name);
3026                 return err;
3027         }
3028
3029         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3030                 err = dev_uc_del(dev, addr);
3031         else if (is_multicast_ether_addr(addr))
3032                 err = dev_mc_del(dev, addr);
3033
3034         return err;
3035 }
3036 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3037
3038 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
3039 {
3040         struct net *net = sock_net(skb->sk);
3041         struct ndmsg *ndm;
3042         struct nlattr *tb[NDA_MAX+1];
3043         struct net_device *dev;
3044         int err = -EINVAL;
3045         __u8 *addr;
3046         u16 vid;
3047
3048         if (!netlink_capable(skb, CAP_NET_ADMIN))
3049                 return -EPERM;
3050
3051         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
3052         if (err < 0)
3053                 return err;
3054
3055         ndm = nlmsg_data(nlh);
3056         if (ndm->ndm_ifindex == 0) {
3057                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3058                 return -EINVAL;
3059         }
3060
3061         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3062         if (dev == NULL) {
3063                 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3064                 return -ENODEV;
3065         }
3066
3067         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3068                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3069                 return -EINVAL;
3070         }
3071
3072         addr = nla_data(tb[NDA_LLADDR]);
3073
3074         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3075         if (err)
3076                 return err;
3077
3078         err = -EOPNOTSUPP;
3079
3080         /* Support fdb on master device the net/bridge default case */
3081         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3082             (dev->priv_flags & IFF_BRIDGE_PORT)) {
3083                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3084                 const struct net_device_ops *ops = br_dev->netdev_ops;
3085
3086                 if (ops->ndo_fdb_del)
3087                         err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3088
3089                 if (err)
3090                         goto out;
3091                 else
3092                         ndm->ndm_flags &= ~NTF_MASTER;
3093         }
3094
3095         /* Embedded bridge, macvlan, and any other device support */
3096         if (ndm->ndm_flags & NTF_SELF) {
3097                 if (dev->netdev_ops->ndo_fdb_del)
3098                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3099                                                            vid);
3100                 else
3101                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3102
3103                 if (!err) {
3104                         rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3105                                         ndm->ndm_state);
3106                         ndm->ndm_flags &= ~NTF_SELF;
3107                 }
3108         }
3109 out:
3110         return err;
3111 }
3112
3113 static int nlmsg_populate_fdb(struct sk_buff *skb,
3114                               struct netlink_callback *cb,
3115                               struct net_device *dev,
3116                               int *idx,
3117                               struct netdev_hw_addr_list *list)
3118 {
3119         struct netdev_hw_addr *ha;
3120         int err;
3121         u32 portid, seq;
3122
3123         portid = NETLINK_CB(cb->skb).portid;
3124         seq = cb->nlh->nlmsg_seq;
3125
3126         list_for_each_entry(ha, &list->list, list) {
3127                 if (*idx < cb->args[2])
3128                         goto skip;
3129
3130                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3131                                               portid, seq,
3132                                               RTM_NEWNEIGH, NTF_SELF,
3133                                               NLM_F_MULTI, NUD_PERMANENT);
3134                 if (err < 0)
3135                         return err;
3136 skip:
3137                 *idx += 1;
3138         }
3139         return 0;
3140 }
3141
3142 /**
3143  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3144  * @nlh: netlink message header
3145  * @dev: netdevice
3146  *
3147  * Default netdevice operation to dump the existing unicast address list.
3148  * Returns number of addresses from list put in skb.
3149  */
3150 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3151                       struct netlink_callback *cb,
3152                       struct net_device *dev,
3153                       struct net_device *filter_dev,
3154                       int *idx)
3155 {
3156         int err;
3157
3158         netif_addr_lock_bh(dev);
3159         err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3160         if (err)
3161                 goto out;
3162         nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3163 out:
3164         netif_addr_unlock_bh(dev);
3165         return err;
3166 }
3167 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3168
3169 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3170 {
3171         struct net_device *dev;
3172         struct nlattr *tb[IFLA_MAX+1];
3173         struct net_device *br_dev = NULL;
3174         const struct net_device_ops *ops = NULL;
3175         const struct net_device_ops *cops = NULL;
3176         struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3177         struct net *net = sock_net(skb->sk);
3178         struct hlist_head *head;
3179         int brport_idx = 0;
3180         int br_idx = 0;
3181         int h, s_h;
3182         int idx = 0, s_idx;
3183         int err = 0;
3184         int fidx = 0;
3185
3186         if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3187                         ifla_policy) == 0) {
3188                 if (tb[IFLA_MASTER])
3189                         br_idx = nla_get_u32(tb[IFLA_MASTER]);
3190         }
3191
3192         brport_idx = ifm->ifi_index;
3193
3194         if (br_idx) {
3195                 br_dev = __dev_get_by_index(net, br_idx);
3196                 if (!br_dev)
3197                         return -ENODEV;
3198
3199                 ops = br_dev->netdev_ops;
3200         }
3201
3202         s_h = cb->args[0];
3203         s_idx = cb->args[1];
3204
3205         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3206                 idx = 0;
3207                 head = &net->dev_index_head[h];
3208                 hlist_for_each_entry(dev, head, index_hlist) {
3209
3210                         if (brport_idx && (dev->ifindex != brport_idx))
3211                                 continue;
3212
3213                         if (!br_idx) { /* user did not specify a specific bridge */
3214                                 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3215                                         br_dev = netdev_master_upper_dev_get(dev);
3216                                         cops = br_dev->netdev_ops;
3217                                 }
3218                         } else {
3219                                 if (dev != br_dev &&
3220                                     !(dev->priv_flags & IFF_BRIDGE_PORT))
3221                                         continue;
3222
3223                                 if (br_dev != netdev_master_upper_dev_get(dev) &&
3224                                     !(dev->priv_flags & IFF_EBRIDGE))
3225                                         continue;
3226                                 cops = ops;
3227                         }
3228
3229                         if (idx < s_idx)
3230                                 goto cont;
3231
3232                         if (dev->priv_flags & IFF_BRIDGE_PORT) {
3233                                 if (cops && cops->ndo_fdb_dump) {
3234                                         err = cops->ndo_fdb_dump(skb, cb,
3235                                                                 br_dev, dev,
3236                                                                 &fidx);
3237                                         if (err == -EMSGSIZE)
3238                                                 goto out;
3239                                 }
3240                         }
3241
3242                         if (dev->netdev_ops->ndo_fdb_dump)
3243                                 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3244                                                                     dev, NULL,
3245                                                                     &fidx);
3246                         else
3247                                 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3248                                                         &fidx);
3249                         if (err == -EMSGSIZE)
3250                                 goto out;
3251
3252                         cops = NULL;
3253
3254                         /* reset fdb offset to 0 for rest of the interfaces */
3255                         cb->args[2] = 0;
3256                         fidx = 0;
3257 cont:
3258                         idx++;
3259                 }
3260         }
3261
3262 out:
3263         cb->args[0] = h;
3264         cb->args[1] = idx;
3265         cb->args[2] = fidx;
3266
3267         return skb->len;
3268 }
3269
3270 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3271                                unsigned int attrnum, unsigned int flag)
3272 {
3273         if (mask & flag)
3274                 return nla_put_u8(skb, attrnum, !!(flags & flag));
3275         return 0;
3276 }
3277
3278 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3279                             struct net_device *dev, u16 mode,
3280                             u32 flags, u32 mask, int nlflags,
3281                             u32 filter_mask,
3282                             int (*vlan_fill)(struct sk_buff *skb,
3283                                              struct net_device *dev,
3284                                              u32 filter_mask))
3285 {
3286         struct nlmsghdr *nlh;
3287         struct ifinfomsg *ifm;
3288         struct nlattr *br_afspec;
3289         struct nlattr *protinfo;
3290         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3291         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3292         int err = 0;
3293
3294         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3295         if (nlh == NULL)
3296                 return -EMSGSIZE;
3297
3298         ifm = nlmsg_data(nlh);
3299         ifm->ifi_family = AF_BRIDGE;
3300         ifm->__ifi_pad = 0;
3301         ifm->ifi_type = dev->type;
3302         ifm->ifi_index = dev->ifindex;
3303         ifm->ifi_flags = dev_get_flags(dev);
3304         ifm->ifi_change = 0;
3305
3306
3307         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3308             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3309             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3310             (br_dev &&
3311              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3312             (dev->addr_len &&
3313              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3314             (dev->ifindex != dev_get_iflink(dev) &&
3315              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3316                 goto nla_put_failure;
3317
3318         br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3319         if (!br_afspec)
3320                 goto nla_put_failure;
3321
3322         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3323                 nla_nest_cancel(skb, br_afspec);
3324                 goto nla_put_failure;
3325         }
3326
3327         if (mode != BRIDGE_MODE_UNDEF) {
3328                 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3329                         nla_nest_cancel(skb, br_afspec);
3330                         goto nla_put_failure;
3331                 }
3332         }
3333         if (vlan_fill) {
3334                 err = vlan_fill(skb, dev, filter_mask);
3335                 if (err) {
3336                         nla_nest_cancel(skb, br_afspec);
3337                         goto nla_put_failure;
3338                 }
3339         }
3340         nla_nest_end(skb, br_afspec);
3341
3342         protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3343         if (!protinfo)
3344                 goto nla_put_failure;
3345
3346         if (brport_nla_put_flag(skb, flags, mask,
3347                                 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3348             brport_nla_put_flag(skb, flags, mask,
3349                                 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3350             brport_nla_put_flag(skb, flags, mask,
3351                                 IFLA_BRPORT_FAST_LEAVE,
3352                                 BR_MULTICAST_FAST_LEAVE) ||
3353             brport_nla_put_flag(skb, flags, mask,
3354                                 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3355             brport_nla_put_flag(skb, flags, mask,
3356                                 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3357             brport_nla_put_flag(skb, flags, mask,
3358                                 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3359             brport_nla_put_flag(skb, flags, mask,
3360                                 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3361             brport_nla_put_flag(skb, flags, mask,
3362                                 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3363                 nla_nest_cancel(skb, protinfo);
3364                 goto nla_put_failure;
3365         }
3366
3367         nla_nest_end(skb, protinfo);
3368
3369         nlmsg_end(skb, nlh);
3370         return 0;
3371 nla_put_failure:
3372         nlmsg_cancel(skb, nlh);
3373         return err ? err : -EMSGSIZE;
3374 }
3375 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3376
3377 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3378 {
3379         struct net *net = sock_net(skb->sk);
3380         struct net_device *dev;
3381         int idx = 0;
3382         u32 portid = NETLINK_CB(cb->skb).portid;
3383         u32 seq = cb->nlh->nlmsg_seq;
3384         u32 filter_mask = 0;
3385         int err;
3386
3387         if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3388                 struct nlattr *extfilt;
3389
3390                 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3391                                           IFLA_EXT_MASK);
3392                 if (extfilt) {
3393                         if (nla_len(extfilt) < sizeof(filter_mask))
3394                                 return -EINVAL;
3395
3396                         filter_mask = nla_get_u32(extfilt);
3397                 }
3398         }
3399
3400         rcu_read_lock();
3401         for_each_netdev_rcu(net, dev) {
3402                 const struct net_device_ops *ops = dev->netdev_ops;
3403                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3404
3405                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3406                         if (idx >= cb->args[0]) {
3407                                 err = br_dev->netdev_ops->ndo_bridge_getlink(
3408                                                 skb, portid, seq, dev,
3409                                                 filter_mask, NLM_F_MULTI);
3410                                 if (err < 0 && err != -EOPNOTSUPP)
3411                                         break;
3412                         }
3413                         idx++;
3414                 }
3415
3416                 if (ops->ndo_bridge_getlink) {
3417                         if (idx >= cb->args[0]) {
3418                                 err = ops->ndo_bridge_getlink(skb, portid,
3419                                                               seq, dev,
3420                                                               filter_mask,
3421                                                               NLM_F_MULTI);
3422                                 if (err < 0 && err != -EOPNOTSUPP)
3423                                         break;
3424                         }
3425                         idx++;
3426                 }
3427         }
3428         rcu_read_unlock();
3429         cb->args[0] = idx;
3430
3431         return skb->len;
3432 }
3433
3434 static inline size_t bridge_nlmsg_size(void)
3435 {
3436         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3437                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
3438                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
3439                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
3440                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
3441                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
3442                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
3443                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
3444                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3445                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
3446                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
3447 }
3448
3449 static int rtnl_bridge_notify(struct net_device *dev)
3450 {
3451         struct net *net = dev_net(dev);
3452         struct sk_buff *skb;
3453         int err = -EOPNOTSUPP;
3454
3455         if (!dev->netdev_ops->ndo_bridge_getlink)
3456                 return 0;
3457
3458         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3459         if (!skb) {
3460                 err = -ENOMEM;
3461                 goto errout;
3462         }
3463
3464         err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3465         if (err < 0)
3466                 goto errout;
3467
3468         if (!skb->len)
3469                 goto errout;
3470
3471         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3472         return 0;
3473 errout:
3474         WARN_ON(err == -EMSGSIZE);
3475         kfree_skb(skb);
3476         if (err)
3477                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3478         return err;
3479 }
3480
3481 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3482 {
3483         struct net *net = sock_net(skb->sk);
3484         struct ifinfomsg *ifm;
3485         struct net_device *dev;
3486         struct nlattr *br_spec, *attr = NULL;
3487         int rem, err = -EOPNOTSUPP;
3488         u16 flags = 0;
3489         bool have_flags = false;
3490
3491         if (nlmsg_len(nlh) < sizeof(*ifm))
3492                 return -EINVAL;
3493
3494         ifm = nlmsg_data(nlh);
3495         if (ifm->ifi_family != AF_BRIDGE)
3496                 return -EPFNOSUPPORT;
3497
3498         dev = __dev_get_by_index(net, ifm->ifi_index);
3499         if (!dev) {
3500                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3501                 return -ENODEV;
3502         }
3503
3504         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3505         if (br_spec) {
3506                 nla_for_each_nested(attr, br_spec, rem) {
3507                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3508                                 if (nla_len(attr) < sizeof(flags))
3509                                         return -EINVAL;
3510
3511                                 have_flags = true;
3512                                 flags = nla_get_u16(attr);
3513                                 break;
3514                         }
3515                 }
3516         }
3517
3518         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3519                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3520
3521                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3522                         err = -EOPNOTSUPP;
3523                         goto out;
3524                 }
3525
3526                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3527                 if (err)
3528                         goto out;
3529
3530                 flags &= ~BRIDGE_FLAGS_MASTER;
3531         }
3532
3533         if ((flags & BRIDGE_FLAGS_SELF)) {
3534                 if (!dev->netdev_ops->ndo_bridge_setlink)
3535                         err = -EOPNOTSUPP;
3536                 else
3537                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3538                                                                   flags);
3539                 if (!err) {
3540                         flags &= ~BRIDGE_FLAGS_SELF;
3541
3542                         /* Generate event to notify upper layer of bridge
3543                          * change
3544                          */
3545                         err = rtnl_bridge_notify(dev);
3546                 }
3547         }
3548
3549         if (have_flags)
3550                 memcpy(nla_data(attr), &flags, sizeof(flags));
3551 out:
3552         return err;
3553 }
3554
3555 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3556 {
3557         struct net *net = sock_net(skb->sk);
3558         struct ifinfomsg *ifm;
3559         struct net_device *dev;
3560         struct nlattr *br_spec, *attr = NULL;
3561         int rem, err = -EOPNOTSUPP;
3562         u16 flags = 0;
3563         bool have_flags = false;
3564
3565         if (nlmsg_len(nlh) < sizeof(*ifm))
3566                 return -EINVAL;
3567
3568         ifm = nlmsg_data(nlh);
3569         if (ifm->ifi_family != AF_BRIDGE)
3570                 return -EPFNOSUPPORT;
3571
3572         dev = __dev_get_by_index(net, ifm->ifi_index);
3573         if (!dev) {
3574                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3575                 return -ENODEV;
3576         }
3577
3578         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3579         if (br_spec) {
3580                 nla_for_each_nested(attr, br_spec, rem) {
3581                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3582                                 if (nla_len(attr) < sizeof(flags))
3583                                         return -EINVAL;
3584
3585                                 have_flags = true;
3586                                 flags = nla_get_u16(attr);
3587                                 break;
3588                         }
3589                 }
3590         }
3591
3592         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3593                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3594
3595                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3596                         err = -EOPNOTSUPP;
3597                         goto out;
3598                 }
3599
3600                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3601                 if (err)
3602                         goto out;
3603
3604                 flags &= ~BRIDGE_FLAGS_MASTER;
3605         }
3606
3607         if ((flags & BRIDGE_FLAGS_SELF)) {
3608                 if (!dev->netdev_ops->ndo_bridge_dellink)
3609                         err = -EOPNOTSUPP;
3610                 else
3611                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3612                                                                   flags);
3613
3614                 if (!err) {
3615                         flags &= ~BRIDGE_FLAGS_SELF;
3616
3617                         /* Generate event to notify upper layer of bridge
3618                          * change
3619                          */
3620                         err = rtnl_bridge_notify(dev);
3621                 }
3622         }
3623
3624         if (have_flags)
3625                 memcpy(nla_data(attr), &flags, sizeof(flags));
3626 out:
3627         return err;
3628 }
3629
3630 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3631 {
3632         return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3633                (!idxattr || idxattr == attrid);
3634 }
3635
3636 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3637 static int rtnl_get_offload_stats_attr_size(int attr_id)
3638 {
3639         switch (attr_id) {
3640         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3641                 return sizeof(struct rtnl_link_stats64);
3642         }
3643
3644         return 0;
3645 }
3646
3647 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3648                                   int *prividx)
3649 {
3650         struct nlattr *attr = NULL;
3651         int attr_id, size;
3652         void *attr_data;
3653         int err;
3654
3655         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3656               dev->netdev_ops->ndo_get_offload_stats))
3657                 return -ENODATA;
3658
3659         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3660              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3661                 if (attr_id < *prividx)
3662                         continue;
3663
3664                 size = rtnl_get_offload_stats_attr_size(attr_id);
3665                 if (!size)
3666                         continue;
3667
3668                 if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
3669                         continue;
3670
3671                 attr = nla_reserve_64bit(skb, attr_id, size,
3672                                          IFLA_OFFLOAD_XSTATS_UNSPEC);
3673                 if (!attr)
3674                         goto nla_put_failure;
3675
3676                 attr_data = nla_data(attr);
3677                 memset(attr_data, 0, size);
3678                 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3679                                                              attr_data);
3680                 if (err)
3681                         goto get_offload_stats_failure;
3682         }
3683
3684         if (!attr)
3685                 return -ENODATA;
3686
3687         *prividx = 0;
3688         return 0;
3689
3690 nla_put_failure:
3691         err = -EMSGSIZE;
3692 get_offload_stats_failure:
3693         *prividx = attr_id;
3694         return err;
3695 }
3696
3697 static int rtnl_get_offload_stats_size(const struct net_device *dev)
3698 {
3699         int nla_size = 0;
3700         int attr_id;
3701         int size;
3702
3703         if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3704               dev->netdev_ops->ndo_get_offload_stats))
3705                 return 0;
3706
3707         for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3708              attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3709                 if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
3710                         continue;
3711                 size = rtnl_get_offload_stats_attr_size(attr_id);
3712                 nla_size += nla_total_size_64bit(size);
3713         }
3714
3715         if (nla_size != 0)
3716                 nla_size += nla_total_size(0);
3717
3718         return nla_size;
3719 }
3720
3721 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3722                                int type, u32 pid, u32 seq, u32 change,
3723                                unsigned int flags, unsigned int filter_mask,
3724                                int *idxattr, int *prividx)
3725 {
3726         struct if_stats_msg *ifsm;
3727         struct nlmsghdr *nlh;
3728         struct nlattr *attr;
3729         int s_prividx = *prividx;
3730         int err;
3731
3732         ASSERT_RTNL();
3733
3734         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3735         if (!nlh)
3736                 return -EMSGSIZE;
3737
3738         ifsm = nlmsg_data(nlh);
3739         ifsm->ifindex = dev->ifindex;
3740         ifsm->filter_mask = filter_mask;
3741
3742         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3743                 struct rtnl_link_stats64 *sp;
3744
3745                 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3746                                          sizeof(struct rtnl_link_stats64),
3747                                          IFLA_STATS_UNSPEC);
3748                 if (!attr)
3749                         goto nla_put_failure;
3750
3751                 sp = nla_data(attr);
3752                 dev_get_stats(dev, sp);
3753         }
3754
3755         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3756                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3757
3758                 if (ops && ops->fill_linkxstats) {
3759                         *idxattr = IFLA_STATS_LINK_XSTATS;
3760                         attr = nla_nest_start(skb,
3761                                               IFLA_STATS_LINK_XSTATS);
3762                         if (!attr)
3763                                 goto nla_put_failure;
3764
3765                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3766                         nla_nest_end(skb, attr);
3767                         if (err)
3768                                 goto nla_put_failure;
3769                         *idxattr = 0;
3770                 }
3771         }
3772
3773         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3774                              *idxattr)) {
3775                 const struct rtnl_link_ops *ops = NULL;
3776                 const struct net_device *master;
3777
3778                 master = netdev_master_upper_dev_get(dev);
3779                 if (master)
3780                         ops = master->rtnl_link_ops;
3781                 if (ops && ops->fill_linkxstats) {
3782                         *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3783                         attr = nla_nest_start(skb,
3784                                               IFLA_STATS_LINK_XSTATS_SLAVE);
3785                         if (!attr)
3786                                 goto nla_put_failure;
3787
3788                         err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3789                         nla_nest_end(skb, attr);
3790                         if (err)
3791                                 goto nla_put_failure;
3792                         *idxattr = 0;
3793                 }
3794         }
3795
3796         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3797                              *idxattr)) {
3798                 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3799                 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3800                 if (!attr)
3801                         goto nla_put_failure;
3802
3803                 err = rtnl_get_offload_stats(skb, dev, prividx);
3804                 if (err == -ENODATA)
3805                         nla_nest_cancel(skb, attr);
3806                 else
3807                         nla_nest_end(skb, attr);
3808
3809                 if (err && err != -ENODATA)
3810                         goto nla_put_failure;
3811                 *idxattr = 0;
3812         }
3813
3814         nlmsg_end(skb, nlh);
3815
3816         return 0;
3817
3818 nla_put_failure:
3819         /* not a multi message or no progress mean a real error */
3820         if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3821                 nlmsg_cancel(skb, nlh);
3822         else
3823                 nlmsg_end(skb, nlh);
3824
3825         return -EMSGSIZE;
3826 }
3827
3828 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3829                                   u32 filter_mask)
3830 {
3831         size_t size = 0;
3832
3833         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3834                 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3835
3836         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3837                 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3838                 int attr = IFLA_STATS_LINK_XSTATS;
3839
3840                 if (ops && ops->get_linkxstats_size) {
3841                         size += nla_total_size(ops->get_linkxstats_size(dev,
3842                                                                         attr));
3843                         /* for IFLA_STATS_LINK_XSTATS */
3844                         size += nla_total_size(0);
3845                 }
3846         }
3847
3848         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
3849                 struct net_device *_dev = (struct net_device *)dev;
3850                 const struct rtnl_link_ops *ops = NULL;
3851                 const struct net_device *master;
3852
3853                 /* netdev_master_upper_dev_get can't take const */
3854                 master = netdev_master_upper_dev_get(_dev);
3855                 if (master)
3856                         ops = master->rtnl_link_ops;
3857                 if (ops && ops->get_linkxstats_size) {
3858                         int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
3859
3860                         size += nla_total_size(ops->get_linkxstats_size(dev,
3861                                                                         attr));
3862                         /* for IFLA_STATS_LINK_XSTATS_SLAVE */
3863                         size += nla_total_size(0);
3864                 }
3865         }
3866
3867         if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
3868                 size += rtnl_get_offload_stats_size(dev);
3869
3870         return size;
3871 }
3872
3873 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
3874 {
3875         struct net *net = sock_net(skb->sk);
3876         struct net_device *dev = NULL;
3877         int idxattr = 0, prividx = 0;
3878         struct if_stats_msg *ifsm;
3879         struct sk_buff *nskb;
3880         u32 filter_mask;
3881         int err;
3882
3883         ifsm = nlmsg_data(nlh);
3884         if (ifsm->ifindex > 0)
3885                 dev = __dev_get_by_index(net, ifsm->ifindex);
3886         else
3887                 return -EINVAL;
3888
3889         if (!dev)
3890                 return -ENODEV;
3891
3892         filter_mask = ifsm->filter_mask;
3893         if (!filter_mask)
3894                 return -EINVAL;
3895
3896         nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
3897         if (!nskb)
3898                 return -ENOBUFS;
3899
3900         err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
3901                                   NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
3902                                   0, filter_mask, &idxattr, &prividx);
3903         if (err < 0) {
3904                 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3905                 WARN_ON(err == -EMSGSIZE);
3906                 kfree_skb(nskb);
3907         } else {
3908                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3909         }
3910
3911         return err;
3912 }
3913
3914 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
3915 {
3916         int h, s_h, err, s_idx, s_idxattr, s_prividx;
3917         struct net *net = sock_net(skb->sk);
3918         unsigned int flags = NLM_F_MULTI;
3919         struct if_stats_msg *ifsm;
3920         struct hlist_head *head;
3921         struct net_device *dev;
3922         u32 filter_mask = 0;
3923         int idx = 0;
3924
3925         s_h = cb->args[0];
3926         s_idx = cb->args[1];
3927         s_idxattr = cb->args[2];
3928         s_prividx = cb->args[3];
3929
3930         cb->seq = net->dev_base_seq;
3931
3932         ifsm = nlmsg_data(cb->nlh);
3933         filter_mask = ifsm->filter_mask;
3934         if (!filter_mask)
3935                 return -EINVAL;
3936
3937         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3938                 idx = 0;
3939                 head = &net->dev_index_head[h];
3940                 hlist_for_each_entry(dev, head, index_hlist) {
3941                         if (idx < s_idx)
3942                                 goto cont;
3943                         err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
3944                                                   NETLINK_CB(cb->skb).portid,
3945                                                   cb->nlh->nlmsg_seq, 0,
3946                                                   flags, filter_mask,
3947                                                   &s_idxattr, &s_prividx);
3948                         /* If we ran out of room on the first message,
3949                          * we're in trouble
3950                          */
3951                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
3952
3953                         if (err < 0)
3954                                 goto out;
3955                         s_prividx = 0;
3956                         s_idxattr = 0;
3957                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3958 cont:
3959                         idx++;
3960                 }
3961         }
3962 out:
3963         cb->args[3] = s_prividx;
3964         cb->args[2] = s_idxattr;
3965         cb->args[1] = idx;
3966         cb->args[0] = h;
3967
3968         return skb->len;
3969 }
3970
3971 /* Process one rtnetlink message. */
3972
3973 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3974 {
3975         struct net *net = sock_net(skb->sk);
3976         rtnl_doit_func doit;
3977         int kind;
3978         int family;
3979         int type;
3980         int err;
3981
3982         type = nlh->nlmsg_type;
3983         if (type > RTM_MAX)
3984                 return -EOPNOTSUPP;
3985
3986         type -= RTM_BASE;
3987
3988         /* All the messages must have at least 1 byte length */
3989         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3990                 return 0;
3991
3992         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3993         kind = type&3;
3994
3995         if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3996                 return -EPERM;
3997
3998         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3999                 struct sock *rtnl;
4000                 rtnl_dumpit_func dumpit;
4001                 rtnl_calcit_func calcit;
4002                 u16 min_dump_alloc = 0;
4003
4004                 dumpit = rtnl_get_dumpit(family, type);
4005                 if (dumpit == NULL)
4006                         return -EOPNOTSUPP;
4007                 calcit = rtnl_get_calcit(family, type);
4008                 if (calcit)
4009                         min_dump_alloc = calcit(skb, nlh);
4010
4011                 __rtnl_unlock();
4012                 rtnl = net->rtnl;
4013                 {
4014                         struct netlink_dump_control c = {
4015                                 .dump           = dumpit,
4016                                 .min_dump_alloc = min_dump_alloc,
4017                         };
4018                         err = netlink_dump_start(rtnl, skb, nlh, &c);
4019                 }
4020                 rtnl_lock();
4021                 return err;
4022         }
4023
4024         doit = rtnl_get_doit(family, type);
4025         if (doit == NULL)
4026                 return -EOPNOTSUPP;
4027
4028         return doit(skb, nlh);
4029 }
4030
4031 static void rtnetlink_rcv(struct sk_buff *skb)
4032 {
4033         rtnl_lock();
4034         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4035         rtnl_unlock();
4036 }
4037
4038 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4039 {
4040         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4041
4042         switch (event) {
4043         case NETDEV_UP:
4044         case NETDEV_DOWN:
4045         case NETDEV_PRE_UP:
4046         case NETDEV_POST_INIT:
4047         case NETDEV_REGISTER:
4048         case NETDEV_CHANGE:
4049         case NETDEV_PRE_TYPE_CHANGE:
4050         case NETDEV_GOING_DOWN:
4051         case NETDEV_UNREGISTER:
4052         case NETDEV_UNREGISTER_FINAL:
4053         case NETDEV_RELEASE:
4054         case NETDEV_JOIN:
4055         case NETDEV_BONDING_INFO:
4056                 break;
4057         default:
4058                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
4059                 break;
4060         }
4061         return NOTIFY_DONE;
4062 }
4063
4064 static struct notifier_block rtnetlink_dev_notifier = {
4065         .notifier_call  = rtnetlink_event,
4066 };
4067
4068
4069 static int __net_init rtnetlink_net_init(struct net *net)
4070 {
4071         struct sock *sk;
4072         struct netlink_kernel_cfg cfg = {
4073                 .groups         = RTNLGRP_MAX,
4074                 .input          = rtnetlink_rcv,
4075                 .cb_mutex       = &rtnl_mutex,
4076                 .flags          = NL_CFG_F_NONROOT_RECV,
4077         };
4078
4079         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4080         if (!sk)
4081                 return -ENOMEM;
4082         net->rtnl = sk;
4083         return 0;
4084 }
4085
4086 static void __net_exit rtnetlink_net_exit(struct net *net)
4087 {
4088         netlink_kernel_release(net->rtnl);
4089         net->rtnl = NULL;
4090 }
4091
4092 static struct pernet_operations rtnetlink_net_ops = {
4093         .init = rtnetlink_net_init,
4094         .exit = rtnetlink_net_exit,
4095 };
4096
4097 void __init rtnetlink_init(void)
4098 {
4099         if (register_pernet_subsys(&rtnetlink_net_ops))
4100                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4101
4102         register_netdevice_notifier(&rtnetlink_dev_notifier);
4103
4104         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4105                       rtnl_dump_ifinfo, rtnl_calcit);
4106         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4107         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4108         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
4109
4110         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4111         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
4112
4113         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4114         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4115         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
4116
4117         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
4118         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
4119         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
4120
4121         rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4122                       NULL);
4123 }