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