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