xhci: prevent bus suspend if a roothub port detected a over-current condition
[linux/fpc-iii.git] / net / core / rtnetlink.c
blobf51973f458e44bd72ba64c581f68ee480d476f85
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.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
45 #include <linux/uaccess.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
50 #include <net/ip.h>
51 #include <net/protocol.h>
52 #include <net/arp.h>
53 #include <net/route.h>
54 #include <net/udp.h>
55 #include <net/tcp.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
62 #define RTNL_MAX_TYPE 48
63 #define RTNL_SLAVE_MAX_TYPE 36
65 struct rtnl_link {
66 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
68 struct module *owner;
69 unsigned int flags;
70 struct rcu_head rcu;
73 static DEFINE_MUTEX(rtnl_mutex);
75 void rtnl_lock(void)
77 mutex_lock(&rtnl_mutex);
79 EXPORT_SYMBOL(rtnl_lock);
81 int rtnl_lock_killable(void)
83 return mutex_lock_killable(&rtnl_mutex);
85 EXPORT_SYMBOL(rtnl_lock_killable);
87 static struct sk_buff *defer_kfree_skb_list;
88 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
95 EXPORT_SYMBOL(rtnl_kfree_skbs);
97 void __rtnl_unlock(void)
99 struct sk_buff *head = defer_kfree_skb_list;
101 defer_kfree_skb_list = NULL;
103 mutex_unlock(&rtnl_mutex);
105 while (head) {
106 struct sk_buff *next = head->next;
108 kfree_skb(head);
109 cond_resched();
110 head = next;
114 void rtnl_unlock(void)
116 /* This fellow will unlock it for us. */
117 netdev_run_todo();
119 EXPORT_SYMBOL(rtnl_unlock);
121 int rtnl_trylock(void)
123 return mutex_trylock(&rtnl_mutex);
125 EXPORT_SYMBOL(rtnl_trylock);
127 int rtnl_is_locked(void)
129 return mutex_is_locked(&rtnl_mutex);
131 EXPORT_SYMBOL(rtnl_is_locked);
133 #ifdef CONFIG_PROVE_LOCKING
134 bool lockdep_rtnl_is_held(void)
136 return lockdep_is_held(&rtnl_mutex);
138 EXPORT_SYMBOL(lockdep_rtnl_is_held);
139 #endif /* #ifdef CONFIG_PROVE_LOCKING */
141 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
143 static inline int rtm_msgindex(int msgtype)
145 int msgindex = msgtype - RTM_BASE;
148 * msgindex < 0 implies someone tried to register a netlink
149 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
150 * the message type has not been added to linux/rtnetlink.h
152 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
154 return msgindex;
157 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
159 struct rtnl_link **tab;
161 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
162 protocol = PF_UNSPEC;
164 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
165 if (!tab)
166 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
168 return tab[msgtype];
171 static int rtnl_register_internal(struct module *owner,
172 int protocol, int msgtype,
173 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
174 unsigned int flags)
176 struct rtnl_link *link, *old;
177 struct rtnl_link __rcu **tab;
178 int msgindex;
179 int ret = -ENOBUFS;
181 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
182 msgindex = rtm_msgindex(msgtype);
184 rtnl_lock();
185 tab = rtnl_msg_handlers[protocol];
186 if (tab == NULL) {
187 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
188 if (!tab)
189 goto unlock;
191 /* ensures we see the 0 stores */
192 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
195 old = rtnl_dereference(tab[msgindex]);
196 if (old) {
197 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
198 if (!link)
199 goto unlock;
200 } else {
201 link = kzalloc(sizeof(*link), GFP_KERNEL);
202 if (!link)
203 goto unlock;
206 WARN_ON(link->owner && link->owner != owner);
207 link->owner = owner;
209 WARN_ON(doit && link->doit && link->doit != doit);
210 if (doit)
211 link->doit = doit;
212 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
213 if (dumpit)
214 link->dumpit = dumpit;
216 link->flags |= flags;
218 /* publish protocol:msgtype */
219 rcu_assign_pointer(tab[msgindex], link);
220 ret = 0;
221 if (old)
222 kfree_rcu(old, rcu);
223 unlock:
224 rtnl_unlock();
225 return ret;
229 * rtnl_register_module - Register a rtnetlink message type
231 * @owner: module registering the hook (THIS_MODULE)
232 * @protocol: Protocol family or PF_UNSPEC
233 * @msgtype: rtnetlink message type
234 * @doit: Function pointer called for each request message
235 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
236 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
238 * Like rtnl_register, but for use by removable modules.
240 int rtnl_register_module(struct module *owner,
241 int protocol, int msgtype,
242 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
243 unsigned int flags)
245 return rtnl_register_internal(owner, protocol, msgtype,
246 doit, dumpit, flags);
248 EXPORT_SYMBOL_GPL(rtnl_register_module);
251 * rtnl_register - Register a rtnetlink message type
252 * @protocol: Protocol family or PF_UNSPEC
253 * @msgtype: rtnetlink message type
254 * @doit: Function pointer called for each request message
255 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
256 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
258 * Registers the specified function pointers (at least one of them has
259 * to be non-NULL) to be called whenever a request message for the
260 * specified protocol family and message type is received.
262 * The special protocol family PF_UNSPEC may be used to define fallback
263 * function pointers for the case when no entry for the specific protocol
264 * family exists.
266 void rtnl_register(int protocol, int msgtype,
267 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
268 unsigned int flags)
270 int err;
272 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
273 flags);
274 if (err)
275 pr_err("Unable to register rtnetlink message handler, "
276 "protocol = %d, message type = %d\n", protocol, msgtype);
280 * rtnl_unregister - Unregister a rtnetlink message type
281 * @protocol: Protocol family or PF_UNSPEC
282 * @msgtype: rtnetlink message type
284 * Returns 0 on success or a negative error code.
286 int rtnl_unregister(int protocol, int msgtype)
288 struct rtnl_link **tab, *link;
289 int msgindex;
291 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
292 msgindex = rtm_msgindex(msgtype);
294 rtnl_lock();
295 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
296 if (!tab) {
297 rtnl_unlock();
298 return -ENOENT;
301 link = tab[msgindex];
302 rcu_assign_pointer(tab[msgindex], NULL);
303 rtnl_unlock();
305 kfree_rcu(link, rcu);
307 return 0;
309 EXPORT_SYMBOL_GPL(rtnl_unregister);
312 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
313 * @protocol : Protocol family or PF_UNSPEC
315 * Identical to calling rtnl_unregster() for all registered message types
316 * of a certain protocol family.
318 void rtnl_unregister_all(int protocol)
320 struct rtnl_link **tab, *link;
321 int msgindex;
323 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
325 rtnl_lock();
326 tab = rtnl_msg_handlers[protocol];
327 if (!tab) {
328 rtnl_unlock();
329 return;
331 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
332 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
333 link = tab[msgindex];
334 if (!link)
335 continue;
337 rcu_assign_pointer(tab[msgindex], NULL);
338 kfree_rcu(link, rcu);
340 rtnl_unlock();
342 synchronize_net();
344 kfree(tab);
346 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
348 static LIST_HEAD(link_ops);
350 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
352 const struct rtnl_link_ops *ops;
354 list_for_each_entry(ops, &link_ops, list) {
355 if (!strcmp(ops->kind, kind))
356 return ops;
358 return NULL;
362 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
363 * @ops: struct rtnl_link_ops * to register
365 * The caller must hold the rtnl_mutex. This function should be used
366 * by drivers that create devices during module initialization. It
367 * must be called before registering the devices.
369 * Returns 0 on success or a negative error code.
371 int __rtnl_link_register(struct rtnl_link_ops *ops)
373 if (rtnl_link_ops_get(ops->kind))
374 return -EEXIST;
376 /* The check for setup is here because if ops
377 * does not have that filled up, it is not possible
378 * to use the ops for creating device. So do not
379 * fill up dellink as well. That disables rtnl_dellink.
381 if (ops->setup && !ops->dellink)
382 ops->dellink = unregister_netdevice_queue;
384 list_add_tail(&ops->list, &link_ops);
385 return 0;
387 EXPORT_SYMBOL_GPL(__rtnl_link_register);
390 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
391 * @ops: struct rtnl_link_ops * to register
393 * Returns 0 on success or a negative error code.
395 int rtnl_link_register(struct rtnl_link_ops *ops)
397 int err;
399 /* Sanity-check max sizes to avoid stack buffer overflow. */
400 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
401 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
402 return -EINVAL;
404 rtnl_lock();
405 err = __rtnl_link_register(ops);
406 rtnl_unlock();
407 return err;
409 EXPORT_SYMBOL_GPL(rtnl_link_register);
411 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
413 struct net_device *dev;
414 LIST_HEAD(list_kill);
416 for_each_netdev(net, dev) {
417 if (dev->rtnl_link_ops == ops)
418 ops->dellink(dev, &list_kill);
420 unregister_netdevice_many(&list_kill);
424 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
425 * @ops: struct rtnl_link_ops * to unregister
427 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
428 * integrity (hold pernet_ops_rwsem for writing to close the race
429 * with setup_net() and cleanup_net()).
431 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
433 struct net *net;
435 for_each_net(net) {
436 __rtnl_kill_links(net, ops);
438 list_del(&ops->list);
440 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
442 /* Return with the rtnl_lock held when there are no network
443 * devices unregistering in any network namespace.
445 static void rtnl_lock_unregistering_all(void)
447 struct net *net;
448 bool unregistering;
449 DEFINE_WAIT_FUNC(wait, woken_wake_function);
451 add_wait_queue(&netdev_unregistering_wq, &wait);
452 for (;;) {
453 unregistering = false;
454 rtnl_lock();
455 /* We held write locked pernet_ops_rwsem, and parallel
456 * setup_net() and cleanup_net() are not possible.
458 for_each_net(net) {
459 if (net->dev_unreg_count > 0) {
460 unregistering = true;
461 break;
464 if (!unregistering)
465 break;
466 __rtnl_unlock();
468 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
470 remove_wait_queue(&netdev_unregistering_wq, &wait);
474 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
475 * @ops: struct rtnl_link_ops * to unregister
477 void rtnl_link_unregister(struct rtnl_link_ops *ops)
479 /* Close the race with setup_net() and cleanup_net() */
480 down_write(&pernet_ops_rwsem);
481 rtnl_lock_unregistering_all();
482 __rtnl_link_unregister(ops);
483 rtnl_unlock();
484 up_write(&pernet_ops_rwsem);
486 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
488 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
490 struct net_device *master_dev;
491 const struct rtnl_link_ops *ops;
492 size_t size = 0;
494 rcu_read_lock();
496 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
497 if (!master_dev)
498 goto out;
500 ops = master_dev->rtnl_link_ops;
501 if (!ops || !ops->get_slave_size)
502 goto out;
503 /* IFLA_INFO_SLAVE_DATA + nested data */
504 size = nla_total_size(sizeof(struct nlattr)) +
505 ops->get_slave_size(master_dev, dev);
507 out:
508 rcu_read_unlock();
509 return size;
512 static size_t rtnl_link_get_size(const struct net_device *dev)
514 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
515 size_t size;
517 if (!ops)
518 return 0;
520 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
521 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
523 if (ops->get_size)
524 /* IFLA_INFO_DATA + nested data */
525 size += nla_total_size(sizeof(struct nlattr)) +
526 ops->get_size(dev);
528 if (ops->get_xstats_size)
529 /* IFLA_INFO_XSTATS */
530 size += nla_total_size(ops->get_xstats_size(dev));
532 size += rtnl_link_get_slave_info_data_size(dev);
534 return size;
537 static LIST_HEAD(rtnl_af_ops);
539 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
541 const struct rtnl_af_ops *ops;
543 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
544 if (ops->family == family)
545 return ops;
548 return NULL;
552 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
553 * @ops: struct rtnl_af_ops * to register
555 * Returns 0 on success or a negative error code.
557 void rtnl_af_register(struct rtnl_af_ops *ops)
559 rtnl_lock();
560 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
561 rtnl_unlock();
563 EXPORT_SYMBOL_GPL(rtnl_af_register);
566 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
567 * @ops: struct rtnl_af_ops * to unregister
569 void rtnl_af_unregister(struct rtnl_af_ops *ops)
571 rtnl_lock();
572 list_del_rcu(&ops->list);
573 rtnl_unlock();
575 synchronize_rcu();
577 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
579 static size_t rtnl_link_get_af_size(const struct net_device *dev,
580 u32 ext_filter_mask)
582 struct rtnl_af_ops *af_ops;
583 size_t size;
585 /* IFLA_AF_SPEC */
586 size = nla_total_size(sizeof(struct nlattr));
588 rcu_read_lock();
589 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
590 if (af_ops->get_link_af_size) {
591 /* AF_* + nested data */
592 size += nla_total_size(sizeof(struct nlattr)) +
593 af_ops->get_link_af_size(dev, ext_filter_mask);
596 rcu_read_unlock();
598 return size;
601 static bool rtnl_have_link_slave_info(const struct net_device *dev)
603 struct net_device *master_dev;
604 bool ret = false;
606 rcu_read_lock();
608 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
609 if (master_dev && master_dev->rtnl_link_ops)
610 ret = true;
611 rcu_read_unlock();
612 return ret;
615 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
616 const struct net_device *dev)
618 struct net_device *master_dev;
619 const struct rtnl_link_ops *ops;
620 struct nlattr *slave_data;
621 int err;
623 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
624 if (!master_dev)
625 return 0;
626 ops = master_dev->rtnl_link_ops;
627 if (!ops)
628 return 0;
629 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
630 return -EMSGSIZE;
631 if (ops->fill_slave_info) {
632 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
633 if (!slave_data)
634 return -EMSGSIZE;
635 err = ops->fill_slave_info(skb, master_dev, dev);
636 if (err < 0)
637 goto err_cancel_slave_data;
638 nla_nest_end(skb, slave_data);
640 return 0;
642 err_cancel_slave_data:
643 nla_nest_cancel(skb, slave_data);
644 return err;
647 static int rtnl_link_info_fill(struct sk_buff *skb,
648 const struct net_device *dev)
650 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
651 struct nlattr *data;
652 int err;
654 if (!ops)
655 return 0;
656 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
657 return -EMSGSIZE;
658 if (ops->fill_xstats) {
659 err = ops->fill_xstats(skb, dev);
660 if (err < 0)
661 return err;
663 if (ops->fill_info) {
664 data = nla_nest_start(skb, IFLA_INFO_DATA);
665 if (data == NULL)
666 return -EMSGSIZE;
667 err = ops->fill_info(skb, dev);
668 if (err < 0)
669 goto err_cancel_data;
670 nla_nest_end(skb, data);
672 return 0;
674 err_cancel_data:
675 nla_nest_cancel(skb, data);
676 return err;
679 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
681 struct nlattr *linkinfo;
682 int err = -EMSGSIZE;
684 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
685 if (linkinfo == NULL)
686 goto out;
688 err = rtnl_link_info_fill(skb, dev);
689 if (err < 0)
690 goto err_cancel_link;
692 err = rtnl_link_slave_info_fill(skb, dev);
693 if (err < 0)
694 goto err_cancel_link;
696 nla_nest_end(skb, linkinfo);
697 return 0;
699 err_cancel_link:
700 nla_nest_cancel(skb, linkinfo);
701 out:
702 return err;
705 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
707 struct sock *rtnl = net->rtnl;
708 int err = 0;
710 NETLINK_CB(skb).dst_group = group;
711 if (echo)
712 refcount_inc(&skb->users);
713 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
714 if (echo)
715 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
716 return err;
719 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
721 struct sock *rtnl = net->rtnl;
723 return nlmsg_unicast(rtnl, skb, pid);
725 EXPORT_SYMBOL(rtnl_unicast);
727 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
728 struct nlmsghdr *nlh, gfp_t flags)
730 struct sock *rtnl = net->rtnl;
731 int report = 0;
733 if (nlh)
734 report = nlmsg_report(nlh);
736 nlmsg_notify(rtnl, skb, pid, group, report, flags);
738 EXPORT_SYMBOL(rtnl_notify);
740 void rtnl_set_sk_err(struct net *net, u32 group, int error)
742 struct sock *rtnl = net->rtnl;
744 netlink_set_err(rtnl, 0, group, error);
746 EXPORT_SYMBOL(rtnl_set_sk_err);
748 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
750 struct nlattr *mx;
751 int i, valid = 0;
753 mx = nla_nest_start(skb, RTA_METRICS);
754 if (mx == NULL)
755 return -ENOBUFS;
757 for (i = 0; i < RTAX_MAX; i++) {
758 if (metrics[i]) {
759 if (i == RTAX_CC_ALGO - 1) {
760 char tmp[TCP_CA_NAME_MAX], *name;
762 name = tcp_ca_get_name_by_key(metrics[i], tmp);
763 if (!name)
764 continue;
765 if (nla_put_string(skb, i + 1, name))
766 goto nla_put_failure;
767 } else if (i == RTAX_FEATURES - 1) {
768 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
770 if (!user_features)
771 continue;
772 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
773 if (nla_put_u32(skb, i + 1, user_features))
774 goto nla_put_failure;
775 } else {
776 if (nla_put_u32(skb, i + 1, metrics[i]))
777 goto nla_put_failure;
779 valid++;
783 if (!valid) {
784 nla_nest_cancel(skb, mx);
785 return 0;
788 return nla_nest_end(skb, mx);
790 nla_put_failure:
791 nla_nest_cancel(skb, mx);
792 return -EMSGSIZE;
794 EXPORT_SYMBOL(rtnetlink_put_metrics);
796 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
797 long expires, u32 error)
799 struct rta_cacheinfo ci = {
800 .rta_error = error,
801 .rta_id = id,
804 if (dst) {
805 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
806 ci.rta_used = dst->__use;
807 ci.rta_clntref = atomic_read(&dst->__refcnt);
809 if (expires) {
810 unsigned long clock;
812 clock = jiffies_to_clock_t(abs(expires));
813 clock = min_t(unsigned long, clock, INT_MAX);
814 ci.rta_expires = (expires > 0) ? clock : -clock;
816 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
818 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
820 static void set_operstate(struct net_device *dev, unsigned char transition)
822 unsigned char operstate = dev->operstate;
824 switch (transition) {
825 case IF_OPER_UP:
826 if ((operstate == IF_OPER_DORMANT ||
827 operstate == IF_OPER_UNKNOWN) &&
828 !netif_dormant(dev))
829 operstate = IF_OPER_UP;
830 break;
832 case IF_OPER_DORMANT:
833 if (operstate == IF_OPER_UP ||
834 operstate == IF_OPER_UNKNOWN)
835 operstate = IF_OPER_DORMANT;
836 break;
839 if (dev->operstate != operstate) {
840 write_lock_bh(&dev_base_lock);
841 dev->operstate = operstate;
842 write_unlock_bh(&dev_base_lock);
843 netdev_state_change(dev);
847 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
849 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
850 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
853 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
854 const struct ifinfomsg *ifm)
856 unsigned int flags = ifm->ifi_flags;
858 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
859 if (ifm->ifi_change)
860 flags = (flags & ifm->ifi_change) |
861 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
863 return flags;
866 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
867 const struct rtnl_link_stats64 *b)
869 a->rx_packets = b->rx_packets;
870 a->tx_packets = b->tx_packets;
871 a->rx_bytes = b->rx_bytes;
872 a->tx_bytes = b->tx_bytes;
873 a->rx_errors = b->rx_errors;
874 a->tx_errors = b->tx_errors;
875 a->rx_dropped = b->rx_dropped;
876 a->tx_dropped = b->tx_dropped;
878 a->multicast = b->multicast;
879 a->collisions = b->collisions;
881 a->rx_length_errors = b->rx_length_errors;
882 a->rx_over_errors = b->rx_over_errors;
883 a->rx_crc_errors = b->rx_crc_errors;
884 a->rx_frame_errors = b->rx_frame_errors;
885 a->rx_fifo_errors = b->rx_fifo_errors;
886 a->rx_missed_errors = b->rx_missed_errors;
888 a->tx_aborted_errors = b->tx_aborted_errors;
889 a->tx_carrier_errors = b->tx_carrier_errors;
890 a->tx_fifo_errors = b->tx_fifo_errors;
891 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
892 a->tx_window_errors = b->tx_window_errors;
894 a->rx_compressed = b->rx_compressed;
895 a->tx_compressed = b->tx_compressed;
897 a->rx_nohandler = b->rx_nohandler;
900 /* All VF info */
901 static inline int rtnl_vfinfo_size(const struct net_device *dev,
902 u32 ext_filter_mask)
904 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
905 int num_vfs = dev_num_vf(dev->dev.parent);
906 size_t size = nla_total_size(0);
907 size += num_vfs *
908 (nla_total_size(0) +
909 nla_total_size(sizeof(struct ifla_vf_mac)) +
910 nla_total_size(sizeof(struct ifla_vf_vlan)) +
911 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
912 nla_total_size(MAX_VLAN_LIST_LEN *
913 sizeof(struct ifla_vf_vlan_info)) +
914 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
915 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
916 nla_total_size(sizeof(struct ifla_vf_rate)) +
917 nla_total_size(sizeof(struct ifla_vf_link_state)) +
918 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
919 nla_total_size(0) + /* nest IFLA_VF_STATS */
920 /* IFLA_VF_STATS_RX_PACKETS */
921 nla_total_size_64bit(sizeof(__u64)) +
922 /* IFLA_VF_STATS_TX_PACKETS */
923 nla_total_size_64bit(sizeof(__u64)) +
924 /* IFLA_VF_STATS_RX_BYTES */
925 nla_total_size_64bit(sizeof(__u64)) +
926 /* IFLA_VF_STATS_TX_BYTES */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_BROADCAST */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_MULTICAST */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_RX_DROPPED */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_TX_DROPPED */
935 nla_total_size_64bit(sizeof(__u64)) +
936 nla_total_size(sizeof(struct ifla_vf_trust)));
937 return size;
938 } else
939 return 0;
942 static size_t rtnl_port_size(const struct net_device *dev,
943 u32 ext_filter_mask)
945 size_t port_size = nla_total_size(4) /* PORT_VF */
946 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
947 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
948 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
949 + nla_total_size(1) /* PROT_VDP_REQUEST */
950 + nla_total_size(2); /* PORT_VDP_RESPONSE */
951 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
952 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
953 + port_size;
954 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
955 + port_size;
957 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
958 !(ext_filter_mask & RTEXT_FILTER_VF))
959 return 0;
960 if (dev_num_vf(dev->dev.parent))
961 return port_self_size + vf_ports_size +
962 vf_port_size * dev_num_vf(dev->dev.parent);
963 else
964 return port_self_size;
967 static size_t rtnl_xdp_size(void)
969 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
970 nla_total_size(1) + /* XDP_ATTACHED */
971 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
972 nla_total_size(4); /* XDP_<mode>_PROG_ID */
974 return xdp_size;
977 static noinline size_t if_nlmsg_size(const struct net_device *dev,
978 u32 ext_filter_mask)
980 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
981 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
982 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
983 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
984 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
985 + nla_total_size(sizeof(struct rtnl_link_stats))
986 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
987 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
988 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
989 + nla_total_size(4) /* IFLA_TXQLEN */
990 + nla_total_size(4) /* IFLA_WEIGHT */
991 + nla_total_size(4) /* IFLA_MTU */
992 + nla_total_size(4) /* IFLA_LINK */
993 + nla_total_size(4) /* IFLA_MASTER */
994 + nla_total_size(1) /* IFLA_CARRIER */
995 + nla_total_size(4) /* IFLA_PROMISCUITY */
996 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
997 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
998 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1000 + nla_total_size(1) /* IFLA_OPERSTATE */
1001 + nla_total_size(1) /* IFLA_LINKMODE */
1002 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1003 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1004 + nla_total_size(4) /* IFLA_GROUP */
1005 + nla_total_size(ext_filter_mask
1006 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1007 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1008 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1009 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1010 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1011 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1013 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1014 + rtnl_xdp_size() /* IFLA_XDP */
1015 + nla_total_size(4) /* IFLA_EVENT */
1016 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1017 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1018 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1019 + nla_total_size(4) /* IFLA_IF_NETNSID */
1020 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1021 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1022 + nla_total_size(4) /* IFLA_MIN_MTU */
1023 + nla_total_size(4) /* IFLA_MAX_MTU */
1024 + 0;
1027 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1029 struct nlattr *vf_ports;
1030 struct nlattr *vf_port;
1031 int vf;
1032 int err;
1034 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1035 if (!vf_ports)
1036 return -EMSGSIZE;
1038 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1039 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
1040 if (!vf_port)
1041 goto nla_put_failure;
1042 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1043 goto nla_put_failure;
1044 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1045 if (err == -EMSGSIZE)
1046 goto nla_put_failure;
1047 if (err) {
1048 nla_nest_cancel(skb, vf_port);
1049 continue;
1051 nla_nest_end(skb, vf_port);
1054 nla_nest_end(skb, vf_ports);
1056 return 0;
1058 nla_put_failure:
1059 nla_nest_cancel(skb, vf_ports);
1060 return -EMSGSIZE;
1063 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1065 struct nlattr *port_self;
1066 int err;
1068 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1069 if (!port_self)
1070 return -EMSGSIZE;
1072 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1073 if (err) {
1074 nla_nest_cancel(skb, port_self);
1075 return (err == -EMSGSIZE) ? err : 0;
1078 nla_nest_end(skb, port_self);
1080 return 0;
1083 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1084 u32 ext_filter_mask)
1086 int err;
1088 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1089 !(ext_filter_mask & RTEXT_FILTER_VF))
1090 return 0;
1092 err = rtnl_port_self_fill(skb, dev);
1093 if (err)
1094 return err;
1096 if (dev_num_vf(dev->dev.parent)) {
1097 err = rtnl_vf_ports_fill(skb, dev);
1098 if (err)
1099 return err;
1102 return 0;
1105 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1107 int err;
1108 struct netdev_phys_item_id ppid;
1110 err = dev_get_phys_port_id(dev, &ppid);
1111 if (err) {
1112 if (err == -EOPNOTSUPP)
1113 return 0;
1114 return err;
1117 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1118 return -EMSGSIZE;
1120 return 0;
1123 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1125 char name[IFNAMSIZ];
1126 int err;
1128 err = dev_get_phys_port_name(dev, name, sizeof(name));
1129 if (err) {
1130 if (err == -EOPNOTSUPP)
1131 return 0;
1132 return err;
1135 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1136 return -EMSGSIZE;
1138 return 0;
1141 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1143 int err;
1144 struct switchdev_attr attr = {
1145 .orig_dev = dev,
1146 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1147 .flags = SWITCHDEV_F_NO_RECURSE,
1150 err = switchdev_port_attr_get(dev, &attr);
1151 if (err) {
1152 if (err == -EOPNOTSUPP)
1153 return 0;
1154 return err;
1157 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1158 attr.u.ppid.id))
1159 return -EMSGSIZE;
1161 return 0;
1164 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1165 struct net_device *dev)
1167 struct rtnl_link_stats64 *sp;
1168 struct nlattr *attr;
1170 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1171 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1172 if (!attr)
1173 return -EMSGSIZE;
1175 sp = nla_data(attr);
1176 dev_get_stats(dev, sp);
1178 attr = nla_reserve(skb, IFLA_STATS,
1179 sizeof(struct rtnl_link_stats));
1180 if (!attr)
1181 return -EMSGSIZE;
1183 copy_rtnl_link_stats(nla_data(attr), sp);
1185 return 0;
1188 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1189 struct net_device *dev,
1190 int vfs_num,
1191 struct nlattr *vfinfo)
1193 struct ifla_vf_rss_query_en vf_rss_query_en;
1194 struct nlattr *vf, *vfstats, *vfvlanlist;
1195 struct ifla_vf_link_state vf_linkstate;
1196 struct ifla_vf_vlan_info vf_vlan_info;
1197 struct ifla_vf_spoofchk vf_spoofchk;
1198 struct ifla_vf_tx_rate vf_tx_rate;
1199 struct ifla_vf_stats vf_stats;
1200 struct ifla_vf_trust vf_trust;
1201 struct ifla_vf_vlan vf_vlan;
1202 struct ifla_vf_rate vf_rate;
1203 struct ifla_vf_mac vf_mac;
1204 struct ifla_vf_info ivi;
1206 memset(&ivi, 0, sizeof(ivi));
1208 /* Not all SR-IOV capable drivers support the
1209 * spoofcheck and "RSS query enable" query. Preset to
1210 * -1 so the user space tool can detect that the driver
1211 * didn't report anything.
1213 ivi.spoofchk = -1;
1214 ivi.rss_query_en = -1;
1215 ivi.trusted = -1;
1216 /* The default value for VF link state is "auto"
1217 * IFLA_VF_LINK_STATE_AUTO which equals zero
1219 ivi.linkstate = 0;
1220 /* VLAN Protocol by default is 802.1Q */
1221 ivi.vlan_proto = htons(ETH_P_8021Q);
1222 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1223 return 0;
1225 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1227 vf_mac.vf =
1228 vf_vlan.vf =
1229 vf_vlan_info.vf =
1230 vf_rate.vf =
1231 vf_tx_rate.vf =
1232 vf_spoofchk.vf =
1233 vf_linkstate.vf =
1234 vf_rss_query_en.vf =
1235 vf_trust.vf = ivi.vf;
1237 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1238 vf_vlan.vlan = ivi.vlan;
1239 vf_vlan.qos = ivi.qos;
1240 vf_vlan_info.vlan = ivi.vlan;
1241 vf_vlan_info.qos = ivi.qos;
1242 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1243 vf_tx_rate.rate = ivi.max_tx_rate;
1244 vf_rate.min_tx_rate = ivi.min_tx_rate;
1245 vf_rate.max_tx_rate = ivi.max_tx_rate;
1246 vf_spoofchk.setting = ivi.spoofchk;
1247 vf_linkstate.link_state = ivi.linkstate;
1248 vf_rss_query_en.setting = ivi.rss_query_en;
1249 vf_trust.setting = ivi.trusted;
1250 vf = nla_nest_start(skb, IFLA_VF_INFO);
1251 if (!vf)
1252 goto nla_put_vfinfo_failure;
1253 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1254 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1255 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1256 &vf_rate) ||
1257 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1258 &vf_tx_rate) ||
1259 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1260 &vf_spoofchk) ||
1261 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1262 &vf_linkstate) ||
1263 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1264 sizeof(vf_rss_query_en),
1265 &vf_rss_query_en) ||
1266 nla_put(skb, IFLA_VF_TRUST,
1267 sizeof(vf_trust), &vf_trust))
1268 goto nla_put_vf_failure;
1269 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1270 if (!vfvlanlist)
1271 goto nla_put_vf_failure;
1272 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1273 &vf_vlan_info)) {
1274 nla_nest_cancel(skb, vfvlanlist);
1275 goto nla_put_vf_failure;
1277 nla_nest_end(skb, vfvlanlist);
1278 memset(&vf_stats, 0, sizeof(vf_stats));
1279 if (dev->netdev_ops->ndo_get_vf_stats)
1280 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1281 &vf_stats);
1282 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1283 if (!vfstats)
1284 goto nla_put_vf_failure;
1285 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1286 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1288 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1290 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1292 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1294 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1296 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1298 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1300 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1301 nla_nest_cancel(skb, vfstats);
1302 goto nla_put_vf_failure;
1304 nla_nest_end(skb, vfstats);
1305 nla_nest_end(skb, vf);
1306 return 0;
1308 nla_put_vf_failure:
1309 nla_nest_cancel(skb, vf);
1310 nla_put_vfinfo_failure:
1311 nla_nest_cancel(skb, vfinfo);
1312 return -EMSGSIZE;
1315 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1316 struct net_device *dev,
1317 u32 ext_filter_mask)
1319 struct nlattr *vfinfo;
1320 int i, num_vfs;
1322 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1323 return 0;
1325 num_vfs = dev_num_vf(dev->dev.parent);
1326 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1327 return -EMSGSIZE;
1329 if (!dev->netdev_ops->ndo_get_vf_config)
1330 return 0;
1332 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1333 if (!vfinfo)
1334 return -EMSGSIZE;
1336 for (i = 0; i < num_vfs; i++) {
1337 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1338 return -EMSGSIZE;
1341 nla_nest_end(skb, vfinfo);
1342 return 0;
1345 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1347 struct rtnl_link_ifmap map;
1349 memset(&map, 0, sizeof(map));
1350 map.mem_start = dev->mem_start;
1351 map.mem_end = dev->mem_end;
1352 map.base_addr = dev->base_addr;
1353 map.irq = dev->irq;
1354 map.dma = dev->dma;
1355 map.port = dev->if_port;
1357 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1358 return -EMSGSIZE;
1360 return 0;
1363 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1365 const struct bpf_prog *generic_xdp_prog;
1367 ASSERT_RTNL();
1369 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1370 if (!generic_xdp_prog)
1371 return 0;
1372 return generic_xdp_prog->aux->id;
1375 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1377 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1380 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1382 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1383 XDP_QUERY_PROG_HW);
1386 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1387 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1388 u32 (*get_prog_id)(struct net_device *dev))
1390 u32 curr_id;
1391 int err;
1393 curr_id = get_prog_id(dev);
1394 if (!curr_id)
1395 return 0;
1397 *prog_id = curr_id;
1398 err = nla_put_u32(skb, attr, curr_id);
1399 if (err)
1400 return err;
1402 if (*mode != XDP_ATTACHED_NONE)
1403 *mode = XDP_ATTACHED_MULTI;
1404 else
1405 *mode = tgt_mode;
1407 return 0;
1410 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1412 struct nlattr *xdp;
1413 u32 prog_id;
1414 int err;
1415 u8 mode;
1417 xdp = nla_nest_start(skb, IFLA_XDP);
1418 if (!xdp)
1419 return -EMSGSIZE;
1421 prog_id = 0;
1422 mode = XDP_ATTACHED_NONE;
1423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1424 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1425 if (err)
1426 goto err_cancel;
1427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1428 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1429 if (err)
1430 goto err_cancel;
1431 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1432 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1433 if (err)
1434 goto err_cancel;
1436 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1437 if (err)
1438 goto err_cancel;
1440 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1441 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1442 if (err)
1443 goto err_cancel;
1446 nla_nest_end(skb, xdp);
1447 return 0;
1449 err_cancel:
1450 nla_nest_cancel(skb, xdp);
1451 return err;
1454 static u32 rtnl_get_event(unsigned long event)
1456 u32 rtnl_event_type = IFLA_EVENT_NONE;
1458 switch (event) {
1459 case NETDEV_REBOOT:
1460 rtnl_event_type = IFLA_EVENT_REBOOT;
1461 break;
1462 case NETDEV_FEAT_CHANGE:
1463 rtnl_event_type = IFLA_EVENT_FEATURES;
1464 break;
1465 case NETDEV_BONDING_FAILOVER:
1466 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1467 break;
1468 case NETDEV_NOTIFY_PEERS:
1469 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1470 break;
1471 case NETDEV_RESEND_IGMP:
1472 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1473 break;
1474 case NETDEV_CHANGEINFODATA:
1475 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1476 break;
1477 default:
1478 break;
1481 return rtnl_event_type;
1484 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1486 const struct net_device *upper_dev;
1487 int ret = 0;
1489 rcu_read_lock();
1491 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1492 if (upper_dev)
1493 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1495 rcu_read_unlock();
1496 return ret;
1499 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1500 bool force)
1502 int ifindex = dev_get_iflink(dev);
1504 if (force || dev->ifindex != ifindex)
1505 return nla_put_u32(skb, IFLA_LINK, ifindex);
1507 return 0;
1510 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1511 struct net_device *dev)
1513 char buf[IFALIASZ];
1514 int ret;
1516 ret = dev_get_alias(dev, buf, sizeof(buf));
1517 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1520 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1521 const struct net_device *dev,
1522 struct net *src_net, gfp_t gfp)
1524 bool put_iflink = false;
1526 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1527 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1529 if (!net_eq(dev_net(dev), link_net)) {
1530 int id = peernet2id_alloc(src_net, link_net, gfp);
1532 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1533 return -EMSGSIZE;
1535 put_iflink = true;
1539 return nla_put_iflink(skb, dev, put_iflink);
1542 static int rtnl_fill_link_af(struct sk_buff *skb,
1543 const struct net_device *dev,
1544 u32 ext_filter_mask)
1546 const struct rtnl_af_ops *af_ops;
1547 struct nlattr *af_spec;
1549 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1550 if (!af_spec)
1551 return -EMSGSIZE;
1553 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1554 struct nlattr *af;
1555 int err;
1557 if (!af_ops->fill_link_af)
1558 continue;
1560 af = nla_nest_start(skb, af_ops->family);
1561 if (!af)
1562 return -EMSGSIZE;
1564 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1566 * Caller may return ENODATA to indicate that there
1567 * was no data to be dumped. This is not an error, it
1568 * means we should trim the attribute header and
1569 * continue.
1571 if (err == -ENODATA)
1572 nla_nest_cancel(skb, af);
1573 else if (err < 0)
1574 return -EMSGSIZE;
1576 nla_nest_end(skb, af);
1579 nla_nest_end(skb, af_spec);
1580 return 0;
1583 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1584 struct net_device *dev, struct net *src_net,
1585 int type, u32 pid, u32 seq, u32 change,
1586 unsigned int flags, u32 ext_filter_mask,
1587 u32 event, int *new_nsid, int new_ifindex,
1588 int tgt_netnsid, gfp_t gfp)
1590 struct ifinfomsg *ifm;
1591 struct nlmsghdr *nlh;
1593 ASSERT_RTNL();
1594 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1595 if (nlh == NULL)
1596 return -EMSGSIZE;
1598 ifm = nlmsg_data(nlh);
1599 ifm->ifi_family = AF_UNSPEC;
1600 ifm->__ifi_pad = 0;
1601 ifm->ifi_type = dev->type;
1602 ifm->ifi_index = dev->ifindex;
1603 ifm->ifi_flags = dev_get_flags(dev);
1604 ifm->ifi_change = change;
1606 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1607 goto nla_put_failure;
1609 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1610 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1611 nla_put_u8(skb, IFLA_OPERSTATE,
1612 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1613 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1614 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1615 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1616 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1617 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1618 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1619 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1620 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1621 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1622 #ifdef CONFIG_RPS
1623 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1624 #endif
1625 put_master_ifindex(skb, dev) ||
1626 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1627 (dev->qdisc &&
1628 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1629 nla_put_ifalias(skb, dev) ||
1630 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1631 atomic_read(&dev->carrier_up_count) +
1632 atomic_read(&dev->carrier_down_count)) ||
1633 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1634 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1635 atomic_read(&dev->carrier_up_count)) ||
1636 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1637 atomic_read(&dev->carrier_down_count)))
1638 goto nla_put_failure;
1640 if (event != IFLA_EVENT_NONE) {
1641 if (nla_put_u32(skb, IFLA_EVENT, event))
1642 goto nla_put_failure;
1645 if (rtnl_fill_link_ifmap(skb, dev))
1646 goto nla_put_failure;
1648 if (dev->addr_len) {
1649 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1650 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1651 goto nla_put_failure;
1654 if (rtnl_phys_port_id_fill(skb, dev))
1655 goto nla_put_failure;
1657 if (rtnl_phys_port_name_fill(skb, dev))
1658 goto nla_put_failure;
1660 if (rtnl_phys_switch_id_fill(skb, dev))
1661 goto nla_put_failure;
1663 if (rtnl_fill_stats(skb, dev))
1664 goto nla_put_failure;
1666 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1667 goto nla_put_failure;
1669 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1670 goto nla_put_failure;
1672 if (rtnl_xdp_fill(skb, dev))
1673 goto nla_put_failure;
1675 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1676 if (rtnl_link_fill(skb, dev) < 0)
1677 goto nla_put_failure;
1680 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1681 goto nla_put_failure;
1683 if (new_nsid &&
1684 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1685 goto nla_put_failure;
1686 if (new_ifindex &&
1687 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1688 goto nla_put_failure;
1691 rcu_read_lock();
1692 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1693 goto nla_put_failure_rcu;
1694 rcu_read_unlock();
1696 nlmsg_end(skb, nlh);
1697 return 0;
1699 nla_put_failure_rcu:
1700 rcu_read_unlock();
1701 nla_put_failure:
1702 nlmsg_cancel(skb, nlh);
1703 return -EMSGSIZE;
1706 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1707 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1708 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1709 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1710 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1711 [IFLA_MTU] = { .type = NLA_U32 },
1712 [IFLA_LINK] = { .type = NLA_U32 },
1713 [IFLA_MASTER] = { .type = NLA_U32 },
1714 [IFLA_CARRIER] = { .type = NLA_U8 },
1715 [IFLA_TXQLEN] = { .type = NLA_U32 },
1716 [IFLA_WEIGHT] = { .type = NLA_U32 },
1717 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1718 [IFLA_LINKMODE] = { .type = NLA_U8 },
1719 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1720 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1721 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1722 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1723 * allow 0-length string (needed to remove an alias).
1725 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1726 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1727 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1728 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1729 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1730 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1731 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1732 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1733 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1734 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1735 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1736 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1737 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1738 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1739 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1740 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1741 [IFLA_XDP] = { .type = NLA_NESTED },
1742 [IFLA_EVENT] = { .type = NLA_U32 },
1743 [IFLA_GROUP] = { .type = NLA_U32 },
1744 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
1745 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1746 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1747 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1748 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1751 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1752 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1753 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1754 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1758 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1759 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1760 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1761 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1762 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1763 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1764 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1765 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1766 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1767 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1768 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1769 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1770 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1773 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1774 [IFLA_PORT_VF] = { .type = NLA_U32 },
1775 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1776 .len = PORT_PROFILE_MAX },
1777 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1778 .len = PORT_UUID_MAX },
1779 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1780 .len = PORT_UUID_MAX },
1781 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1782 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1784 /* Unused, but we need to keep it here since user space could
1785 * fill it. It's also broken with regard to NLA_BINARY use in
1786 * combination with structs.
1788 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1789 .len = sizeof(struct ifla_port_vsi) },
1792 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1793 [IFLA_XDP_FD] = { .type = NLA_S32 },
1794 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1795 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1796 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1799 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1801 const struct rtnl_link_ops *ops = NULL;
1802 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1804 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1805 ifla_info_policy, NULL) < 0)
1806 return NULL;
1808 if (linfo[IFLA_INFO_KIND]) {
1809 char kind[MODULE_NAME_LEN];
1811 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1812 ops = rtnl_link_ops_get(kind);
1815 return ops;
1818 static bool link_master_filtered(struct net_device *dev, int master_idx)
1820 struct net_device *master;
1822 if (!master_idx)
1823 return false;
1825 master = netdev_master_upper_dev_get(dev);
1826 if (!master || master->ifindex != master_idx)
1827 return true;
1829 return false;
1832 static bool link_kind_filtered(const struct net_device *dev,
1833 const struct rtnl_link_ops *kind_ops)
1835 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1836 return true;
1838 return false;
1841 static bool link_dump_filtered(struct net_device *dev,
1842 int master_idx,
1843 const struct rtnl_link_ops *kind_ops)
1845 if (link_master_filtered(dev, master_idx) ||
1846 link_kind_filtered(dev, kind_ops))
1847 return true;
1849 return false;
1852 static struct net *get_target_net(struct sock *sk, int netnsid)
1854 struct net *net;
1856 net = get_net_ns_by_id(sock_net(sk), netnsid);
1857 if (!net)
1858 return ERR_PTR(-EINVAL);
1860 /* For now, the caller is required to have CAP_NET_ADMIN in
1861 * the user namespace owning the target net ns.
1863 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1864 put_net(net);
1865 return ERR_PTR(-EACCES);
1867 return net;
1870 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1872 struct net *net = sock_net(skb->sk);
1873 struct net *tgt_net = net;
1874 int h, s_h;
1875 int idx = 0, s_idx;
1876 struct net_device *dev;
1877 struct hlist_head *head;
1878 struct nlattr *tb[IFLA_MAX+1];
1879 u32 ext_filter_mask = 0;
1880 const struct rtnl_link_ops *kind_ops = NULL;
1881 unsigned int flags = NLM_F_MULTI;
1882 int master_idx = 0;
1883 int netnsid = -1;
1884 int err;
1885 int hdrlen;
1887 s_h = cb->args[0];
1888 s_idx = cb->args[1];
1890 /* A hack to preserve kernel<->userspace interface.
1891 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1892 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1893 * what iproute2 < v3.9.0 used.
1894 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1895 * attribute, its netlink message is shorter than struct ifinfomsg.
1897 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1898 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1900 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1901 ifla_policy, NULL) >= 0) {
1902 if (tb[IFLA_IF_NETNSID]) {
1903 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1904 tgt_net = get_target_net(skb->sk, netnsid);
1905 if (IS_ERR(tgt_net))
1906 return PTR_ERR(tgt_net);
1909 if (tb[IFLA_EXT_MASK])
1910 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1912 if (tb[IFLA_MASTER])
1913 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1915 if (tb[IFLA_LINKINFO])
1916 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1918 if (master_idx || kind_ops)
1919 flags |= NLM_F_DUMP_FILTERED;
1922 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1923 idx = 0;
1924 head = &tgt_net->dev_index_head[h];
1925 hlist_for_each_entry(dev, head, index_hlist) {
1926 if (link_dump_filtered(dev, master_idx, kind_ops))
1927 goto cont;
1928 if (idx < s_idx)
1929 goto cont;
1930 err = rtnl_fill_ifinfo(skb, dev, net,
1931 RTM_NEWLINK,
1932 NETLINK_CB(cb->skb).portid,
1933 cb->nlh->nlmsg_seq, 0,
1934 flags,
1935 ext_filter_mask, 0, NULL, 0,
1936 netnsid, GFP_KERNEL);
1938 if (err < 0) {
1939 if (likely(skb->len))
1940 goto out;
1942 goto out_err;
1944 cont:
1945 idx++;
1948 out:
1949 err = skb->len;
1950 out_err:
1951 cb->args[1] = idx;
1952 cb->args[0] = h;
1953 cb->seq = net->dev_base_seq;
1954 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1955 if (netnsid >= 0)
1956 put_net(tgt_net);
1958 return err;
1961 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1962 struct netlink_ext_ack *exterr)
1964 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1966 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1968 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1970 struct net *net;
1971 /* Examine the link attributes and figure out which
1972 * network namespace we are talking about.
1974 if (tb[IFLA_NET_NS_PID])
1975 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1976 else if (tb[IFLA_NET_NS_FD])
1977 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1978 else
1979 net = get_net(src_net);
1980 return net;
1982 EXPORT_SYMBOL(rtnl_link_get_net);
1984 /* Figure out which network namespace we are talking about by
1985 * examining the link attributes in the following order:
1987 * 1. IFLA_NET_NS_PID
1988 * 2. IFLA_NET_NS_FD
1989 * 3. IFLA_IF_NETNSID
1991 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1992 struct nlattr *tb[])
1994 struct net *net;
1996 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
1997 return rtnl_link_get_net(src_net, tb);
1999 if (!tb[IFLA_IF_NETNSID])
2000 return get_net(src_net);
2002 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
2003 if (!net)
2004 return ERR_PTR(-EINVAL);
2006 return net;
2009 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2010 struct net *src_net,
2011 struct nlattr *tb[], int cap)
2013 struct net *net;
2015 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2016 if (IS_ERR(net))
2017 return net;
2019 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2020 put_net(net);
2021 return ERR_PTR(-EPERM);
2024 return net;
2027 /* Verify that rtnetlink requests do not pass additional properties
2028 * potentially referring to different network namespaces.
2030 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2031 struct netlink_ext_ack *extack,
2032 bool netns_id_only)
2035 if (netns_id_only) {
2036 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2037 return 0;
2039 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2040 return -EOPNOTSUPP;
2043 if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2044 goto invalid_attr;
2046 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD]))
2047 goto invalid_attr;
2049 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID]))
2050 goto invalid_attr;
2052 return 0;
2054 invalid_attr:
2055 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2056 return -EINVAL;
2059 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2061 if (dev) {
2062 if (tb[IFLA_ADDRESS] &&
2063 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2064 return -EINVAL;
2066 if (tb[IFLA_BROADCAST] &&
2067 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2068 return -EINVAL;
2071 if (tb[IFLA_AF_SPEC]) {
2072 struct nlattr *af;
2073 int rem, err;
2075 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2076 const struct rtnl_af_ops *af_ops;
2078 rcu_read_lock();
2079 af_ops = rtnl_af_lookup(nla_type(af));
2080 if (!af_ops) {
2081 rcu_read_unlock();
2082 return -EAFNOSUPPORT;
2085 if (!af_ops->set_link_af) {
2086 rcu_read_unlock();
2087 return -EOPNOTSUPP;
2090 if (af_ops->validate_link_af) {
2091 err = af_ops->validate_link_af(dev, af);
2092 if (err < 0) {
2093 rcu_read_unlock();
2094 return err;
2098 rcu_read_unlock();
2102 return 0;
2105 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2106 int guid_type)
2108 const struct net_device_ops *ops = dev->netdev_ops;
2110 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2113 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2115 if (dev->type != ARPHRD_INFINIBAND)
2116 return -EOPNOTSUPP;
2118 return handle_infiniband_guid(dev, ivt, guid_type);
2121 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2123 const struct net_device_ops *ops = dev->netdev_ops;
2124 int err = -EINVAL;
2126 if (tb[IFLA_VF_MAC]) {
2127 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2129 if (ivm->vf >= INT_MAX)
2130 return -EINVAL;
2131 err = -EOPNOTSUPP;
2132 if (ops->ndo_set_vf_mac)
2133 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2134 ivm->mac);
2135 if (err < 0)
2136 return err;
2139 if (tb[IFLA_VF_VLAN]) {
2140 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2142 if (ivv->vf >= INT_MAX)
2143 return -EINVAL;
2144 err = -EOPNOTSUPP;
2145 if (ops->ndo_set_vf_vlan)
2146 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2147 ivv->qos,
2148 htons(ETH_P_8021Q));
2149 if (err < 0)
2150 return err;
2153 if (tb[IFLA_VF_VLAN_LIST]) {
2154 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2155 struct nlattr *attr;
2156 int rem, len = 0;
2158 err = -EOPNOTSUPP;
2159 if (!ops->ndo_set_vf_vlan)
2160 return err;
2162 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2163 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2164 nla_len(attr) < NLA_HDRLEN) {
2165 return -EINVAL;
2167 if (len >= MAX_VLAN_LIST_LEN)
2168 return -EOPNOTSUPP;
2169 ivvl[len] = nla_data(attr);
2171 len++;
2173 if (len == 0)
2174 return -EINVAL;
2176 if (ivvl[0]->vf >= INT_MAX)
2177 return -EINVAL;
2178 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2179 ivvl[0]->qos, ivvl[0]->vlan_proto);
2180 if (err < 0)
2181 return err;
2184 if (tb[IFLA_VF_TX_RATE]) {
2185 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2186 struct ifla_vf_info ivf;
2188 if (ivt->vf >= INT_MAX)
2189 return -EINVAL;
2190 err = -EOPNOTSUPP;
2191 if (ops->ndo_get_vf_config)
2192 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2193 if (err < 0)
2194 return err;
2196 err = -EOPNOTSUPP;
2197 if (ops->ndo_set_vf_rate)
2198 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2199 ivf.min_tx_rate,
2200 ivt->rate);
2201 if (err < 0)
2202 return err;
2205 if (tb[IFLA_VF_RATE]) {
2206 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2208 if (ivt->vf >= INT_MAX)
2209 return -EINVAL;
2210 err = -EOPNOTSUPP;
2211 if (ops->ndo_set_vf_rate)
2212 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2213 ivt->min_tx_rate,
2214 ivt->max_tx_rate);
2215 if (err < 0)
2216 return err;
2219 if (tb[IFLA_VF_SPOOFCHK]) {
2220 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2222 if (ivs->vf >= INT_MAX)
2223 return -EINVAL;
2224 err = -EOPNOTSUPP;
2225 if (ops->ndo_set_vf_spoofchk)
2226 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2227 ivs->setting);
2228 if (err < 0)
2229 return err;
2232 if (tb[IFLA_VF_LINK_STATE]) {
2233 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2235 if (ivl->vf >= INT_MAX)
2236 return -EINVAL;
2237 err = -EOPNOTSUPP;
2238 if (ops->ndo_set_vf_link_state)
2239 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2240 ivl->link_state);
2241 if (err < 0)
2242 return err;
2245 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2246 struct ifla_vf_rss_query_en *ivrssq_en;
2248 err = -EOPNOTSUPP;
2249 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2250 if (ivrssq_en->vf >= INT_MAX)
2251 return -EINVAL;
2252 if (ops->ndo_set_vf_rss_query_en)
2253 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2254 ivrssq_en->setting);
2255 if (err < 0)
2256 return err;
2259 if (tb[IFLA_VF_TRUST]) {
2260 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2262 if (ivt->vf >= INT_MAX)
2263 return -EINVAL;
2264 err = -EOPNOTSUPP;
2265 if (ops->ndo_set_vf_trust)
2266 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2267 if (err < 0)
2268 return err;
2271 if (tb[IFLA_VF_IB_NODE_GUID]) {
2272 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2274 if (ivt->vf >= INT_MAX)
2275 return -EINVAL;
2276 if (!ops->ndo_set_vf_guid)
2277 return -EOPNOTSUPP;
2278 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2281 if (tb[IFLA_VF_IB_PORT_GUID]) {
2282 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2284 if (ivt->vf >= INT_MAX)
2285 return -EINVAL;
2286 if (!ops->ndo_set_vf_guid)
2287 return -EOPNOTSUPP;
2289 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2292 return err;
2295 static int do_set_master(struct net_device *dev, int ifindex,
2296 struct netlink_ext_ack *extack)
2298 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2299 const struct net_device_ops *ops;
2300 int err;
2302 if (upper_dev) {
2303 if (upper_dev->ifindex == ifindex)
2304 return 0;
2305 ops = upper_dev->netdev_ops;
2306 if (ops->ndo_del_slave) {
2307 err = ops->ndo_del_slave(upper_dev, dev);
2308 if (err)
2309 return err;
2310 } else {
2311 return -EOPNOTSUPP;
2315 if (ifindex) {
2316 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2317 if (!upper_dev)
2318 return -EINVAL;
2319 ops = upper_dev->netdev_ops;
2320 if (ops->ndo_add_slave) {
2321 err = ops->ndo_add_slave(upper_dev, dev, extack);
2322 if (err)
2323 return err;
2324 } else {
2325 return -EOPNOTSUPP;
2328 return 0;
2331 #define DO_SETLINK_MODIFIED 0x01
2332 /* notify flag means notify + modified. */
2333 #define DO_SETLINK_NOTIFY 0x03
2334 static int do_setlink(const struct sk_buff *skb,
2335 struct net_device *dev, struct ifinfomsg *ifm,
2336 struct netlink_ext_ack *extack,
2337 struct nlattr **tb, char *ifname, int status)
2339 const struct net_device_ops *ops = dev->netdev_ops;
2340 int err;
2342 err = validate_linkmsg(dev, tb);
2343 if (err < 0)
2344 return err;
2346 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2347 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2348 tb, CAP_NET_ADMIN);
2349 if (IS_ERR(net)) {
2350 err = PTR_ERR(net);
2351 goto errout;
2354 err = dev_change_net_namespace(dev, net, ifname);
2355 put_net(net);
2356 if (err)
2357 goto errout;
2358 status |= DO_SETLINK_MODIFIED;
2361 if (tb[IFLA_MAP]) {
2362 struct rtnl_link_ifmap *u_map;
2363 struct ifmap k_map;
2365 if (!ops->ndo_set_config) {
2366 err = -EOPNOTSUPP;
2367 goto errout;
2370 if (!netif_device_present(dev)) {
2371 err = -ENODEV;
2372 goto errout;
2375 u_map = nla_data(tb[IFLA_MAP]);
2376 k_map.mem_start = (unsigned long) u_map->mem_start;
2377 k_map.mem_end = (unsigned long) u_map->mem_end;
2378 k_map.base_addr = (unsigned short) u_map->base_addr;
2379 k_map.irq = (unsigned char) u_map->irq;
2380 k_map.dma = (unsigned char) u_map->dma;
2381 k_map.port = (unsigned char) u_map->port;
2383 err = ops->ndo_set_config(dev, &k_map);
2384 if (err < 0)
2385 goto errout;
2387 status |= DO_SETLINK_NOTIFY;
2390 if (tb[IFLA_ADDRESS]) {
2391 struct sockaddr *sa;
2392 int len;
2394 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2395 sizeof(*sa));
2396 sa = kmalloc(len, GFP_KERNEL);
2397 if (!sa) {
2398 err = -ENOMEM;
2399 goto errout;
2401 sa->sa_family = dev->type;
2402 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2403 dev->addr_len);
2404 err = dev_set_mac_address(dev, sa);
2405 kfree(sa);
2406 if (err)
2407 goto errout;
2408 status |= DO_SETLINK_MODIFIED;
2411 if (tb[IFLA_MTU]) {
2412 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2413 if (err < 0)
2414 goto errout;
2415 status |= DO_SETLINK_MODIFIED;
2418 if (tb[IFLA_GROUP]) {
2419 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2420 status |= DO_SETLINK_NOTIFY;
2424 * Interface selected by interface index but interface
2425 * name provided implies that a name change has been
2426 * requested.
2428 if (ifm->ifi_index > 0 && ifname[0]) {
2429 err = dev_change_name(dev, ifname);
2430 if (err < 0)
2431 goto errout;
2432 status |= DO_SETLINK_MODIFIED;
2435 if (tb[IFLA_IFALIAS]) {
2436 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2437 nla_len(tb[IFLA_IFALIAS]));
2438 if (err < 0)
2439 goto errout;
2440 status |= DO_SETLINK_NOTIFY;
2443 if (tb[IFLA_BROADCAST]) {
2444 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2445 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2448 if (ifm->ifi_flags || ifm->ifi_change) {
2449 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2450 if (err < 0)
2451 goto errout;
2454 if (tb[IFLA_MASTER]) {
2455 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2456 if (err)
2457 goto errout;
2458 status |= DO_SETLINK_MODIFIED;
2461 if (tb[IFLA_CARRIER]) {
2462 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2463 if (err)
2464 goto errout;
2465 status |= DO_SETLINK_MODIFIED;
2468 if (tb[IFLA_TXQLEN]) {
2469 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2471 err = dev_change_tx_queue_len(dev, value);
2472 if (err)
2473 goto errout;
2474 status |= DO_SETLINK_MODIFIED;
2477 if (tb[IFLA_GSO_MAX_SIZE]) {
2478 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2480 if (max_size > GSO_MAX_SIZE) {
2481 err = -EINVAL;
2482 goto errout;
2485 if (dev->gso_max_size ^ max_size) {
2486 netif_set_gso_max_size(dev, max_size);
2487 status |= DO_SETLINK_MODIFIED;
2491 if (tb[IFLA_GSO_MAX_SEGS]) {
2492 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2494 if (max_segs > GSO_MAX_SEGS) {
2495 err = -EINVAL;
2496 goto errout;
2499 if (dev->gso_max_segs ^ max_segs) {
2500 dev->gso_max_segs = max_segs;
2501 status |= DO_SETLINK_MODIFIED;
2505 if (tb[IFLA_OPERSTATE])
2506 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2508 if (tb[IFLA_LINKMODE]) {
2509 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2511 write_lock_bh(&dev_base_lock);
2512 if (dev->link_mode ^ value)
2513 status |= DO_SETLINK_NOTIFY;
2514 dev->link_mode = value;
2515 write_unlock_bh(&dev_base_lock);
2518 if (tb[IFLA_VFINFO_LIST]) {
2519 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2520 struct nlattr *attr;
2521 int rem;
2523 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2524 if (nla_type(attr) != IFLA_VF_INFO ||
2525 nla_len(attr) < NLA_HDRLEN) {
2526 err = -EINVAL;
2527 goto errout;
2529 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2530 ifla_vf_policy, NULL);
2531 if (err < 0)
2532 goto errout;
2533 err = do_setvfinfo(dev, vfinfo);
2534 if (err < 0)
2535 goto errout;
2536 status |= DO_SETLINK_NOTIFY;
2539 err = 0;
2541 if (tb[IFLA_VF_PORTS]) {
2542 struct nlattr *port[IFLA_PORT_MAX+1];
2543 struct nlattr *attr;
2544 int vf;
2545 int rem;
2547 err = -EOPNOTSUPP;
2548 if (!ops->ndo_set_vf_port)
2549 goto errout;
2551 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2552 if (nla_type(attr) != IFLA_VF_PORT ||
2553 nla_len(attr) < NLA_HDRLEN) {
2554 err = -EINVAL;
2555 goto errout;
2557 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2558 ifla_port_policy, NULL);
2559 if (err < 0)
2560 goto errout;
2561 if (!port[IFLA_PORT_VF]) {
2562 err = -EOPNOTSUPP;
2563 goto errout;
2565 vf = nla_get_u32(port[IFLA_PORT_VF]);
2566 err = ops->ndo_set_vf_port(dev, vf, port);
2567 if (err < 0)
2568 goto errout;
2569 status |= DO_SETLINK_NOTIFY;
2572 err = 0;
2574 if (tb[IFLA_PORT_SELF]) {
2575 struct nlattr *port[IFLA_PORT_MAX+1];
2577 err = nla_parse_nested(port, IFLA_PORT_MAX,
2578 tb[IFLA_PORT_SELF], ifla_port_policy,
2579 NULL);
2580 if (err < 0)
2581 goto errout;
2583 err = -EOPNOTSUPP;
2584 if (ops->ndo_set_vf_port)
2585 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2586 if (err < 0)
2587 goto errout;
2588 status |= DO_SETLINK_NOTIFY;
2591 if (tb[IFLA_AF_SPEC]) {
2592 struct nlattr *af;
2593 int rem;
2595 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2596 const struct rtnl_af_ops *af_ops;
2598 rcu_read_lock();
2600 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2602 err = af_ops->set_link_af(dev, af);
2603 if (err < 0) {
2604 rcu_read_unlock();
2605 goto errout;
2608 rcu_read_unlock();
2609 status |= DO_SETLINK_NOTIFY;
2612 err = 0;
2614 if (tb[IFLA_PROTO_DOWN]) {
2615 err = dev_change_proto_down(dev,
2616 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2617 if (err)
2618 goto errout;
2619 status |= DO_SETLINK_NOTIFY;
2622 if (tb[IFLA_XDP]) {
2623 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2624 u32 xdp_flags = 0;
2626 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2627 ifla_xdp_policy, NULL);
2628 if (err < 0)
2629 goto errout;
2631 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2632 err = -EINVAL;
2633 goto errout;
2636 if (xdp[IFLA_XDP_FLAGS]) {
2637 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2638 if (xdp_flags & ~XDP_FLAGS_MASK) {
2639 err = -EINVAL;
2640 goto errout;
2642 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2643 err = -EINVAL;
2644 goto errout;
2648 if (xdp[IFLA_XDP_FD]) {
2649 err = dev_change_xdp_fd(dev, extack,
2650 nla_get_s32(xdp[IFLA_XDP_FD]),
2651 xdp_flags);
2652 if (err)
2653 goto errout;
2654 status |= DO_SETLINK_NOTIFY;
2658 errout:
2659 if (status & DO_SETLINK_MODIFIED) {
2660 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2661 netdev_state_change(dev);
2663 if (err < 0)
2664 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",
2665 dev->name);
2668 return err;
2671 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2672 struct netlink_ext_ack *extack)
2674 struct net *net = sock_net(skb->sk);
2675 struct ifinfomsg *ifm;
2676 struct net_device *dev;
2677 int err;
2678 struct nlattr *tb[IFLA_MAX+1];
2679 char ifname[IFNAMSIZ];
2681 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2682 extack);
2683 if (err < 0)
2684 goto errout;
2686 err = rtnl_ensure_unique_netns(tb, extack, false);
2687 if (err < 0)
2688 goto errout;
2690 if (tb[IFLA_IFNAME])
2691 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2692 else
2693 ifname[0] = '\0';
2695 err = -EINVAL;
2696 ifm = nlmsg_data(nlh);
2697 if (ifm->ifi_index > 0)
2698 dev = __dev_get_by_index(net, ifm->ifi_index);
2699 else if (tb[IFLA_IFNAME])
2700 dev = __dev_get_by_name(net, ifname);
2701 else
2702 goto errout;
2704 if (dev == NULL) {
2705 err = -ENODEV;
2706 goto errout;
2709 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2710 errout:
2711 return err;
2714 static int rtnl_group_dellink(const struct net *net, int group)
2716 struct net_device *dev, *aux;
2717 LIST_HEAD(list_kill);
2718 bool found = false;
2720 if (!group)
2721 return -EPERM;
2723 for_each_netdev(net, dev) {
2724 if (dev->group == group) {
2725 const struct rtnl_link_ops *ops;
2727 found = true;
2728 ops = dev->rtnl_link_ops;
2729 if (!ops || !ops->dellink)
2730 return -EOPNOTSUPP;
2734 if (!found)
2735 return -ENODEV;
2737 for_each_netdev_safe(net, dev, aux) {
2738 if (dev->group == group) {
2739 const struct rtnl_link_ops *ops;
2741 ops = dev->rtnl_link_ops;
2742 ops->dellink(dev, &list_kill);
2745 unregister_netdevice_many(&list_kill);
2747 return 0;
2750 int rtnl_delete_link(struct net_device *dev)
2752 const struct rtnl_link_ops *ops;
2753 LIST_HEAD(list_kill);
2755 ops = dev->rtnl_link_ops;
2756 if (!ops || !ops->dellink)
2757 return -EOPNOTSUPP;
2759 ops->dellink(dev, &list_kill);
2760 unregister_netdevice_many(&list_kill);
2762 return 0;
2764 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2766 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2767 struct netlink_ext_ack *extack)
2769 struct net *net = sock_net(skb->sk);
2770 struct net *tgt_net = net;
2771 struct net_device *dev = NULL;
2772 struct ifinfomsg *ifm;
2773 char ifname[IFNAMSIZ];
2774 struct nlattr *tb[IFLA_MAX+1];
2775 int err;
2776 int netnsid = -1;
2778 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2779 if (err < 0)
2780 return err;
2782 err = rtnl_ensure_unique_netns(tb, extack, true);
2783 if (err < 0)
2784 return err;
2786 if (tb[IFLA_IFNAME])
2787 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2789 if (tb[IFLA_IF_NETNSID]) {
2790 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2791 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
2792 if (IS_ERR(tgt_net))
2793 return PTR_ERR(tgt_net);
2796 err = -EINVAL;
2797 ifm = nlmsg_data(nlh);
2798 if (ifm->ifi_index > 0)
2799 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2800 else if (tb[IFLA_IFNAME])
2801 dev = __dev_get_by_name(tgt_net, ifname);
2802 else if (tb[IFLA_GROUP])
2803 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2804 else
2805 goto out;
2807 if (!dev) {
2808 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2809 err = -ENODEV;
2811 goto out;
2814 err = rtnl_delete_link(dev);
2816 out:
2817 if (netnsid >= 0)
2818 put_net(tgt_net);
2820 return err;
2823 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2825 unsigned int old_flags;
2826 int err;
2828 old_flags = dev->flags;
2829 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2830 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2831 if (err < 0)
2832 return err;
2835 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2836 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2837 } else {
2838 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2839 __dev_notify_flags(dev, old_flags, ~0U);
2841 return 0;
2843 EXPORT_SYMBOL(rtnl_configure_link);
2845 struct net_device *rtnl_create_link(struct net *net,
2846 const char *ifname, unsigned char name_assign_type,
2847 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2849 struct net_device *dev;
2850 unsigned int num_tx_queues = 1;
2851 unsigned int num_rx_queues = 1;
2853 if (tb[IFLA_NUM_TX_QUEUES])
2854 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2855 else if (ops->get_num_tx_queues)
2856 num_tx_queues = ops->get_num_tx_queues();
2858 if (tb[IFLA_NUM_RX_QUEUES])
2859 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2860 else if (ops->get_num_rx_queues)
2861 num_rx_queues = ops->get_num_rx_queues();
2863 if (num_tx_queues < 1 || num_tx_queues > 4096)
2864 return ERR_PTR(-EINVAL);
2866 if (num_rx_queues < 1 || num_rx_queues > 4096)
2867 return ERR_PTR(-EINVAL);
2869 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2870 ops->setup, num_tx_queues, num_rx_queues);
2871 if (!dev)
2872 return ERR_PTR(-ENOMEM);
2874 dev_net_set(dev, net);
2875 dev->rtnl_link_ops = ops;
2876 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2878 if (tb[IFLA_MTU]) {
2879 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
2880 int err;
2882 err = dev_validate_mtu(dev, mtu, NULL);
2883 if (err) {
2884 free_netdev(dev);
2885 return ERR_PTR(err);
2887 dev->mtu = mtu;
2889 if (tb[IFLA_ADDRESS]) {
2890 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2891 nla_len(tb[IFLA_ADDRESS]));
2892 dev->addr_assign_type = NET_ADDR_SET;
2894 if (tb[IFLA_BROADCAST])
2895 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2896 nla_len(tb[IFLA_BROADCAST]));
2897 if (tb[IFLA_TXQLEN])
2898 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2899 if (tb[IFLA_OPERSTATE])
2900 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2901 if (tb[IFLA_LINKMODE])
2902 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2903 if (tb[IFLA_GROUP])
2904 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2905 if (tb[IFLA_GSO_MAX_SIZE])
2906 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2907 if (tb[IFLA_GSO_MAX_SEGS])
2908 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2910 return dev;
2912 EXPORT_SYMBOL(rtnl_create_link);
2914 static int rtnl_group_changelink(const struct sk_buff *skb,
2915 struct net *net, int group,
2916 struct ifinfomsg *ifm,
2917 struct netlink_ext_ack *extack,
2918 struct nlattr **tb)
2920 struct net_device *dev, *aux;
2921 int err;
2923 for_each_netdev_safe(net, dev, aux) {
2924 if (dev->group == group) {
2925 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2926 if (err < 0)
2927 return err;
2931 return 0;
2934 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2935 struct netlink_ext_ack *extack)
2937 struct net *net = sock_net(skb->sk);
2938 const struct rtnl_link_ops *ops;
2939 const struct rtnl_link_ops *m_ops = NULL;
2940 struct net_device *dev;
2941 struct net_device *master_dev = NULL;
2942 struct ifinfomsg *ifm;
2943 char kind[MODULE_NAME_LEN];
2944 char ifname[IFNAMSIZ];
2945 struct nlattr *tb[IFLA_MAX+1];
2946 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2947 unsigned char name_assign_type = NET_NAME_USER;
2948 int err;
2950 #ifdef CONFIG_MODULES
2951 replay:
2952 #endif
2953 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2954 if (err < 0)
2955 return err;
2957 err = rtnl_ensure_unique_netns(tb, extack, false);
2958 if (err < 0)
2959 return err;
2961 if (tb[IFLA_IFNAME])
2962 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2963 else
2964 ifname[0] = '\0';
2966 ifm = nlmsg_data(nlh);
2967 if (ifm->ifi_index > 0)
2968 dev = __dev_get_by_index(net, ifm->ifi_index);
2969 else {
2970 if (ifname[0])
2971 dev = __dev_get_by_name(net, ifname);
2972 else
2973 dev = NULL;
2976 if (dev) {
2977 master_dev = netdev_master_upper_dev_get(dev);
2978 if (master_dev)
2979 m_ops = master_dev->rtnl_link_ops;
2982 err = validate_linkmsg(dev, tb);
2983 if (err < 0)
2984 return err;
2986 if (tb[IFLA_LINKINFO]) {
2987 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2988 tb[IFLA_LINKINFO], ifla_info_policy,
2989 NULL);
2990 if (err < 0)
2991 return err;
2992 } else
2993 memset(linkinfo, 0, sizeof(linkinfo));
2995 if (linkinfo[IFLA_INFO_KIND]) {
2996 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2997 ops = rtnl_link_ops_get(kind);
2998 } else {
2999 kind[0] = '\0';
3000 ops = NULL;
3003 if (1) {
3004 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3005 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3006 struct nlattr **data = NULL;
3007 struct nlattr **slave_data = NULL;
3008 struct net *dest_net, *link_net = NULL;
3010 if (ops) {
3011 if (ops->maxtype > RTNL_MAX_TYPE)
3012 return -EINVAL;
3014 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3015 err = nla_parse_nested(attr, ops->maxtype,
3016 linkinfo[IFLA_INFO_DATA],
3017 ops->policy, NULL);
3018 if (err < 0)
3019 return err;
3020 data = attr;
3022 if (ops->validate) {
3023 err = ops->validate(tb, data, extack);
3024 if (err < 0)
3025 return err;
3029 if (m_ops) {
3030 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3031 return -EINVAL;
3033 if (m_ops->slave_maxtype &&
3034 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3035 err = nla_parse_nested(slave_attr,
3036 m_ops->slave_maxtype,
3037 linkinfo[IFLA_INFO_SLAVE_DATA],
3038 m_ops->slave_policy,
3039 NULL);
3040 if (err < 0)
3041 return err;
3042 slave_data = slave_attr;
3046 if (dev) {
3047 int status = 0;
3049 if (nlh->nlmsg_flags & NLM_F_EXCL)
3050 return -EEXIST;
3051 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3052 return -EOPNOTSUPP;
3054 if (linkinfo[IFLA_INFO_DATA]) {
3055 if (!ops || ops != dev->rtnl_link_ops ||
3056 !ops->changelink)
3057 return -EOPNOTSUPP;
3059 err = ops->changelink(dev, tb, data, extack);
3060 if (err < 0)
3061 return err;
3062 status |= DO_SETLINK_NOTIFY;
3065 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3066 if (!m_ops || !m_ops->slave_changelink)
3067 return -EOPNOTSUPP;
3069 err = m_ops->slave_changelink(master_dev, dev,
3070 tb, slave_data,
3071 extack);
3072 if (err < 0)
3073 return err;
3074 status |= DO_SETLINK_NOTIFY;
3077 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3078 status);
3081 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3082 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3083 return rtnl_group_changelink(skb, net,
3084 nla_get_u32(tb[IFLA_GROUP]),
3085 ifm, extack, tb);
3086 return -ENODEV;
3089 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3090 return -EOPNOTSUPP;
3092 if (!ops) {
3093 #ifdef CONFIG_MODULES
3094 if (kind[0]) {
3095 __rtnl_unlock();
3096 request_module("rtnl-link-%s", kind);
3097 rtnl_lock();
3098 ops = rtnl_link_ops_get(kind);
3099 if (ops)
3100 goto replay;
3102 #endif
3103 return -EOPNOTSUPP;
3106 if (!ops->setup)
3107 return -EOPNOTSUPP;
3109 if (!ifname[0]) {
3110 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3111 name_assign_type = NET_NAME_ENUM;
3114 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3115 if (IS_ERR(dest_net))
3116 return PTR_ERR(dest_net);
3118 if (tb[IFLA_LINK_NETNSID]) {
3119 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3121 link_net = get_net_ns_by_id(dest_net, id);
3122 if (!link_net) {
3123 err = -EINVAL;
3124 goto out;
3126 err = -EPERM;
3127 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3128 goto out;
3131 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3132 name_assign_type, ops, tb);
3133 if (IS_ERR(dev)) {
3134 err = PTR_ERR(dev);
3135 goto out;
3138 dev->ifindex = ifm->ifi_index;
3140 if (ops->newlink) {
3141 err = ops->newlink(link_net ? : net, dev, tb, data,
3142 extack);
3143 /* Drivers should call free_netdev() in ->destructor
3144 * and unregister it on failure after registration
3145 * so that device could be finally freed in rtnl_unlock.
3147 if (err < 0) {
3148 /* If device is not registered at all, free it now */
3149 if (dev->reg_state == NETREG_UNINITIALIZED)
3150 free_netdev(dev);
3151 goto out;
3153 } else {
3154 err = register_netdevice(dev);
3155 if (err < 0) {
3156 free_netdev(dev);
3157 goto out;
3160 err = rtnl_configure_link(dev, ifm);
3161 if (err < 0)
3162 goto out_unregister;
3163 if (link_net) {
3164 err = dev_change_net_namespace(dev, dest_net, ifname);
3165 if (err < 0)
3166 goto out_unregister;
3168 if (tb[IFLA_MASTER]) {
3169 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3170 extack);
3171 if (err)
3172 goto out_unregister;
3174 out:
3175 if (link_net)
3176 put_net(link_net);
3177 put_net(dest_net);
3178 return err;
3179 out_unregister:
3180 if (ops->newlink) {
3181 LIST_HEAD(list_kill);
3183 ops->dellink(dev, &list_kill);
3184 unregister_netdevice_many(&list_kill);
3185 } else {
3186 unregister_netdevice(dev);
3188 goto out;
3192 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3193 struct netlink_ext_ack *extack)
3195 struct net *net = sock_net(skb->sk);
3196 struct net *tgt_net = net;
3197 struct ifinfomsg *ifm;
3198 char ifname[IFNAMSIZ];
3199 struct nlattr *tb[IFLA_MAX+1];
3200 struct net_device *dev = NULL;
3201 struct sk_buff *nskb;
3202 int netnsid = -1;
3203 int err;
3204 u32 ext_filter_mask = 0;
3206 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3207 if (err < 0)
3208 return err;
3210 err = rtnl_ensure_unique_netns(tb, extack, true);
3211 if (err < 0)
3212 return err;
3214 if (tb[IFLA_IF_NETNSID]) {
3215 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
3216 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
3217 if (IS_ERR(tgt_net))
3218 return PTR_ERR(tgt_net);
3221 if (tb[IFLA_IFNAME])
3222 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3224 if (tb[IFLA_EXT_MASK])
3225 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3227 err = -EINVAL;
3228 ifm = nlmsg_data(nlh);
3229 if (ifm->ifi_index > 0)
3230 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3231 else if (tb[IFLA_IFNAME])
3232 dev = __dev_get_by_name(tgt_net, ifname);
3233 else
3234 goto out;
3236 err = -ENODEV;
3237 if (dev == NULL)
3238 goto out;
3240 err = -ENOBUFS;
3241 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3242 if (nskb == NULL)
3243 goto out;
3245 err = rtnl_fill_ifinfo(nskb, dev, net,
3246 RTM_NEWLINK, NETLINK_CB(skb).portid,
3247 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3248 0, NULL, 0, netnsid, GFP_KERNEL);
3249 if (err < 0) {
3250 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3251 WARN_ON(err == -EMSGSIZE);
3252 kfree_skb(nskb);
3253 } else
3254 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3255 out:
3256 if (netnsid >= 0)
3257 put_net(tgt_net);
3259 return err;
3262 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3264 struct net *net = sock_net(skb->sk);
3265 struct net_device *dev;
3266 struct nlattr *tb[IFLA_MAX+1];
3267 u32 ext_filter_mask = 0;
3268 u16 min_ifinfo_dump_size = 0;
3269 int hdrlen;
3271 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3272 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3273 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3275 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3276 if (tb[IFLA_EXT_MASK])
3277 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3280 if (!ext_filter_mask)
3281 return NLMSG_GOODSIZE;
3283 * traverse the list of net devices and compute the minimum
3284 * buffer size based upon the filter mask.
3286 rcu_read_lock();
3287 for_each_netdev_rcu(net, dev) {
3288 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3289 if_nlmsg_size(dev,
3290 ext_filter_mask));
3292 rcu_read_unlock();
3294 return nlmsg_total_size(min_ifinfo_dump_size);
3297 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3299 int idx;
3300 int s_idx = cb->family;
3301 int type = cb->nlh->nlmsg_type - RTM_BASE;
3303 if (s_idx == 0)
3304 s_idx = 1;
3306 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3307 struct rtnl_link **tab;
3308 struct rtnl_link *link;
3309 rtnl_dumpit_func dumpit;
3311 if (idx < s_idx || idx == PF_PACKET)
3312 continue;
3314 if (type < 0 || type >= RTM_NR_MSGTYPES)
3315 continue;
3317 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3318 if (!tab)
3319 continue;
3321 link = tab[type];
3322 if (!link)
3323 continue;
3325 dumpit = link->dumpit;
3326 if (!dumpit)
3327 continue;
3329 if (idx > s_idx) {
3330 memset(&cb->args[0], 0, sizeof(cb->args));
3331 cb->prev_seq = 0;
3332 cb->seq = 0;
3334 if (dumpit(skb, cb))
3335 break;
3337 cb->family = idx;
3339 return skb->len;
3342 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3343 unsigned int change,
3344 u32 event, gfp_t flags, int *new_nsid,
3345 int new_ifindex)
3347 struct net *net = dev_net(dev);
3348 struct sk_buff *skb;
3349 int err = -ENOBUFS;
3350 size_t if_info_size;
3352 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3353 if (skb == NULL)
3354 goto errout;
3356 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3357 type, 0, 0, change, 0, 0, event,
3358 new_nsid, new_ifindex, -1, flags);
3359 if (err < 0) {
3360 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3361 WARN_ON(err == -EMSGSIZE);
3362 kfree_skb(skb);
3363 goto errout;
3365 return skb;
3366 errout:
3367 if (err < 0)
3368 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3369 return NULL;
3372 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3374 struct net *net = dev_net(dev);
3376 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3379 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3380 unsigned int change, u32 event,
3381 gfp_t flags, int *new_nsid, int new_ifindex)
3383 struct sk_buff *skb;
3385 if (dev->reg_state != NETREG_REGISTERED)
3386 return;
3388 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3389 new_ifindex);
3390 if (skb)
3391 rtmsg_ifinfo_send(skb, dev, flags);
3394 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3395 gfp_t flags)
3397 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3398 NULL, 0);
3401 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3402 gfp_t flags, int *new_nsid, int new_ifindex)
3404 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3405 new_nsid, new_ifindex);
3408 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3409 struct net_device *dev,
3410 u8 *addr, u16 vid, u32 pid, u32 seq,
3411 int type, unsigned int flags,
3412 int nlflags, u16 ndm_state)
3414 struct nlmsghdr *nlh;
3415 struct ndmsg *ndm;
3417 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3418 if (!nlh)
3419 return -EMSGSIZE;
3421 ndm = nlmsg_data(nlh);
3422 ndm->ndm_family = AF_BRIDGE;
3423 ndm->ndm_pad1 = 0;
3424 ndm->ndm_pad2 = 0;
3425 ndm->ndm_flags = flags;
3426 ndm->ndm_type = 0;
3427 ndm->ndm_ifindex = dev->ifindex;
3428 ndm->ndm_state = ndm_state;
3430 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3431 goto nla_put_failure;
3432 if (vid)
3433 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3434 goto nla_put_failure;
3436 nlmsg_end(skb, nlh);
3437 return 0;
3439 nla_put_failure:
3440 nlmsg_cancel(skb, nlh);
3441 return -EMSGSIZE;
3444 static inline size_t rtnl_fdb_nlmsg_size(void)
3446 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3447 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3448 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3452 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3453 u16 ndm_state)
3455 struct net *net = dev_net(dev);
3456 struct sk_buff *skb;
3457 int err = -ENOBUFS;
3459 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3460 if (!skb)
3461 goto errout;
3463 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3464 0, 0, type, NTF_SELF, 0, ndm_state);
3465 if (err < 0) {
3466 kfree_skb(skb);
3467 goto errout;
3470 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3471 return;
3472 errout:
3473 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3477 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3479 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3480 struct nlattr *tb[],
3481 struct net_device *dev,
3482 const unsigned char *addr, u16 vid,
3483 u16 flags)
3485 int err = -EINVAL;
3487 /* If aging addresses are supported device will need to
3488 * implement its own handler for this.
3490 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3491 pr_info("%s: FDB only supports static addresses\n", dev->name);
3492 return err;
3495 if (vid) {
3496 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3497 return err;
3500 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3501 err = dev_uc_add_excl(dev, addr);
3502 else if (is_multicast_ether_addr(addr))
3503 err = dev_mc_add_excl(dev, addr);
3505 /* Only return duplicate errors if NLM_F_EXCL is set */
3506 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3507 err = 0;
3509 return err;
3511 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3513 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3514 struct netlink_ext_ack *extack)
3516 u16 vid = 0;
3518 if (vlan_attr) {
3519 if (nla_len(vlan_attr) != sizeof(u16)) {
3520 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3521 return -EINVAL;
3524 vid = nla_get_u16(vlan_attr);
3526 if (!vid || vid >= VLAN_VID_MASK) {
3527 NL_SET_ERR_MSG(extack, "invalid vlan id");
3528 return -EINVAL;
3531 *p_vid = vid;
3532 return 0;
3535 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3536 struct netlink_ext_ack *extack)
3538 struct net *net = sock_net(skb->sk);
3539 struct ndmsg *ndm;
3540 struct nlattr *tb[NDA_MAX+1];
3541 struct net_device *dev;
3542 u8 *addr;
3543 u16 vid;
3544 int err;
3546 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3547 if (err < 0)
3548 return err;
3550 ndm = nlmsg_data(nlh);
3551 if (ndm->ndm_ifindex == 0) {
3552 NL_SET_ERR_MSG(extack, "invalid ifindex");
3553 return -EINVAL;
3556 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3557 if (dev == NULL) {
3558 NL_SET_ERR_MSG(extack, "unknown ifindex");
3559 return -ENODEV;
3562 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3563 NL_SET_ERR_MSG(extack, "invalid address");
3564 return -EINVAL;
3567 if (dev->type != ARPHRD_ETHER) {
3568 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3569 return -EINVAL;
3572 addr = nla_data(tb[NDA_LLADDR]);
3574 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3575 if (err)
3576 return err;
3578 err = -EOPNOTSUPP;
3580 /* Support fdb on master device the net/bridge default case */
3581 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3582 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3583 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3584 const struct net_device_ops *ops = br_dev->netdev_ops;
3586 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3587 nlh->nlmsg_flags);
3588 if (err)
3589 goto out;
3590 else
3591 ndm->ndm_flags &= ~NTF_MASTER;
3594 /* Embedded bridge, macvlan, and any other device support */
3595 if ((ndm->ndm_flags & NTF_SELF)) {
3596 if (dev->netdev_ops->ndo_fdb_add)
3597 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3598 vid,
3599 nlh->nlmsg_flags);
3600 else
3601 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3602 nlh->nlmsg_flags);
3604 if (!err) {
3605 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3606 ndm->ndm_state);
3607 ndm->ndm_flags &= ~NTF_SELF;
3610 out:
3611 return err;
3615 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3617 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3618 struct nlattr *tb[],
3619 struct net_device *dev,
3620 const unsigned char *addr, u16 vid)
3622 int err = -EINVAL;
3624 /* If aging addresses are supported device will need to
3625 * implement its own handler for this.
3627 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3628 pr_info("%s: FDB only supports static addresses\n", dev->name);
3629 return err;
3632 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3633 err = dev_uc_del(dev, addr);
3634 else if (is_multicast_ether_addr(addr))
3635 err = dev_mc_del(dev, addr);
3637 return err;
3639 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3641 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3642 struct netlink_ext_ack *extack)
3644 struct net *net = sock_net(skb->sk);
3645 struct ndmsg *ndm;
3646 struct nlattr *tb[NDA_MAX+1];
3647 struct net_device *dev;
3648 int err = -EINVAL;
3649 __u8 *addr;
3650 u16 vid;
3652 if (!netlink_capable(skb, CAP_NET_ADMIN))
3653 return -EPERM;
3655 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3656 if (err < 0)
3657 return err;
3659 ndm = nlmsg_data(nlh);
3660 if (ndm->ndm_ifindex == 0) {
3661 NL_SET_ERR_MSG(extack, "invalid ifindex");
3662 return -EINVAL;
3665 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3666 if (dev == NULL) {
3667 NL_SET_ERR_MSG(extack, "unknown ifindex");
3668 return -ENODEV;
3671 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3672 NL_SET_ERR_MSG(extack, "invalid address");
3673 return -EINVAL;
3676 if (dev->type != ARPHRD_ETHER) {
3677 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3678 return -EINVAL;
3681 addr = nla_data(tb[NDA_LLADDR]);
3683 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3684 if (err)
3685 return err;
3687 err = -EOPNOTSUPP;
3689 /* Support fdb on master device the net/bridge default case */
3690 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3691 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3692 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3693 const struct net_device_ops *ops = br_dev->netdev_ops;
3695 if (ops->ndo_fdb_del)
3696 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3698 if (err)
3699 goto out;
3700 else
3701 ndm->ndm_flags &= ~NTF_MASTER;
3704 /* Embedded bridge, macvlan, and any other device support */
3705 if (ndm->ndm_flags & NTF_SELF) {
3706 if (dev->netdev_ops->ndo_fdb_del)
3707 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3708 vid);
3709 else
3710 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3712 if (!err) {
3713 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3714 ndm->ndm_state);
3715 ndm->ndm_flags &= ~NTF_SELF;
3718 out:
3719 return err;
3722 static int nlmsg_populate_fdb(struct sk_buff *skb,
3723 struct netlink_callback *cb,
3724 struct net_device *dev,
3725 int *idx,
3726 struct netdev_hw_addr_list *list)
3728 struct netdev_hw_addr *ha;
3729 int err;
3730 u32 portid, seq;
3732 portid = NETLINK_CB(cb->skb).portid;
3733 seq = cb->nlh->nlmsg_seq;
3735 list_for_each_entry(ha, &list->list, list) {
3736 if (*idx < cb->args[2])
3737 goto skip;
3739 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3740 portid, seq,
3741 RTM_NEWNEIGH, NTF_SELF,
3742 NLM_F_MULTI, NUD_PERMANENT);
3743 if (err < 0)
3744 return err;
3745 skip:
3746 *idx += 1;
3748 return 0;
3752 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3753 * @nlh: netlink message header
3754 * @dev: netdevice
3756 * Default netdevice operation to dump the existing unicast address list.
3757 * Returns number of addresses from list put in skb.
3759 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3760 struct netlink_callback *cb,
3761 struct net_device *dev,
3762 struct net_device *filter_dev,
3763 int *idx)
3765 int err;
3767 if (dev->type != ARPHRD_ETHER)
3768 return -EINVAL;
3770 netif_addr_lock_bh(dev);
3771 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3772 if (err)
3773 goto out;
3774 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3775 out:
3776 netif_addr_unlock_bh(dev);
3777 return err;
3779 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3781 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3783 struct net_device *dev;
3784 struct nlattr *tb[IFLA_MAX+1];
3785 struct net_device *br_dev = NULL;
3786 const struct net_device_ops *ops = NULL;
3787 const struct net_device_ops *cops = NULL;
3788 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3789 struct net *net = sock_net(skb->sk);
3790 struct hlist_head *head;
3791 int brport_idx = 0;
3792 int br_idx = 0;
3793 int h, s_h;
3794 int idx = 0, s_idx;
3795 int err = 0;
3796 int fidx = 0;
3798 /* A hack to preserve kernel<->userspace interface.
3799 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3800 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3801 * So, check for ndmsg with an optional u32 attribute (not used here).
3802 * Fortunately these sizes don't conflict with the size of ifinfomsg
3803 * with an optional attribute.
3805 if (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) &&
3806 (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) +
3807 nla_attr_size(sizeof(u32)))) {
3808 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3809 IFLA_MAX, ifla_policy, NULL);
3810 if (err < 0) {
3811 return -EINVAL;
3812 } else if (err == 0) {
3813 if (tb[IFLA_MASTER])
3814 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3817 brport_idx = ifm->ifi_index;
3820 if (br_idx) {
3821 br_dev = __dev_get_by_index(net, br_idx);
3822 if (!br_dev)
3823 return -ENODEV;
3825 ops = br_dev->netdev_ops;
3828 s_h = cb->args[0];
3829 s_idx = cb->args[1];
3831 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3832 idx = 0;
3833 head = &net->dev_index_head[h];
3834 hlist_for_each_entry(dev, head, index_hlist) {
3836 if (brport_idx && (dev->ifindex != brport_idx))
3837 continue;
3839 if (!br_idx) { /* user did not specify a specific bridge */
3840 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3841 br_dev = netdev_master_upper_dev_get(dev);
3842 cops = br_dev->netdev_ops;
3844 } else {
3845 if (dev != br_dev &&
3846 !(dev->priv_flags & IFF_BRIDGE_PORT))
3847 continue;
3849 if (br_dev != netdev_master_upper_dev_get(dev) &&
3850 !(dev->priv_flags & IFF_EBRIDGE))
3851 continue;
3852 cops = ops;
3855 if (idx < s_idx)
3856 goto cont;
3858 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3859 if (cops && cops->ndo_fdb_dump) {
3860 err = cops->ndo_fdb_dump(skb, cb,
3861 br_dev, dev,
3862 &fidx);
3863 if (err == -EMSGSIZE)
3864 goto out;
3868 if (dev->netdev_ops->ndo_fdb_dump)
3869 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3870 dev, NULL,
3871 &fidx);
3872 else
3873 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3874 &fidx);
3875 if (err == -EMSGSIZE)
3876 goto out;
3878 cops = NULL;
3880 /* reset fdb offset to 0 for rest of the interfaces */
3881 cb->args[2] = 0;
3882 fidx = 0;
3883 cont:
3884 idx++;
3888 out:
3889 cb->args[0] = h;
3890 cb->args[1] = idx;
3891 cb->args[2] = fidx;
3893 return skb->len;
3896 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3897 unsigned int attrnum, unsigned int flag)
3899 if (mask & flag)
3900 return nla_put_u8(skb, attrnum, !!(flags & flag));
3901 return 0;
3904 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3905 struct net_device *dev, u16 mode,
3906 u32 flags, u32 mask, int nlflags,
3907 u32 filter_mask,
3908 int (*vlan_fill)(struct sk_buff *skb,
3909 struct net_device *dev,
3910 u32 filter_mask))
3912 struct nlmsghdr *nlh;
3913 struct ifinfomsg *ifm;
3914 struct nlattr *br_afspec;
3915 struct nlattr *protinfo;
3916 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3917 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3918 int err = 0;
3920 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3921 if (nlh == NULL)
3922 return -EMSGSIZE;
3924 ifm = nlmsg_data(nlh);
3925 ifm->ifi_family = AF_BRIDGE;
3926 ifm->__ifi_pad = 0;
3927 ifm->ifi_type = dev->type;
3928 ifm->ifi_index = dev->ifindex;
3929 ifm->ifi_flags = dev_get_flags(dev);
3930 ifm->ifi_change = 0;
3933 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3934 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3935 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3936 (br_dev &&
3937 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3938 (dev->addr_len &&
3939 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3940 (dev->ifindex != dev_get_iflink(dev) &&
3941 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3942 goto nla_put_failure;
3944 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3945 if (!br_afspec)
3946 goto nla_put_failure;
3948 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3949 nla_nest_cancel(skb, br_afspec);
3950 goto nla_put_failure;
3953 if (mode != BRIDGE_MODE_UNDEF) {
3954 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3955 nla_nest_cancel(skb, br_afspec);
3956 goto nla_put_failure;
3959 if (vlan_fill) {
3960 err = vlan_fill(skb, dev, filter_mask);
3961 if (err) {
3962 nla_nest_cancel(skb, br_afspec);
3963 goto nla_put_failure;
3966 nla_nest_end(skb, br_afspec);
3968 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3969 if (!protinfo)
3970 goto nla_put_failure;
3972 if (brport_nla_put_flag(skb, flags, mask,
3973 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3974 brport_nla_put_flag(skb, flags, mask,
3975 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3976 brport_nla_put_flag(skb, flags, mask,
3977 IFLA_BRPORT_FAST_LEAVE,
3978 BR_MULTICAST_FAST_LEAVE) ||
3979 brport_nla_put_flag(skb, flags, mask,
3980 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3981 brport_nla_put_flag(skb, flags, mask,
3982 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3983 brport_nla_put_flag(skb, flags, mask,
3984 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3985 brport_nla_put_flag(skb, flags, mask,
3986 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3987 brport_nla_put_flag(skb, flags, mask,
3988 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3989 nla_nest_cancel(skb, protinfo);
3990 goto nla_put_failure;
3993 nla_nest_end(skb, protinfo);
3995 nlmsg_end(skb, nlh);
3996 return 0;
3997 nla_put_failure:
3998 nlmsg_cancel(skb, nlh);
3999 return err ? err : -EMSGSIZE;
4001 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4003 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4005 struct net *net = sock_net(skb->sk);
4006 struct net_device *dev;
4007 int idx = 0;
4008 u32 portid = NETLINK_CB(cb->skb).portid;
4009 u32 seq = cb->nlh->nlmsg_seq;
4010 u32 filter_mask = 0;
4011 int err;
4013 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
4014 struct nlattr *extfilt;
4016 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
4017 IFLA_EXT_MASK);
4018 if (extfilt) {
4019 if (nla_len(extfilt) < sizeof(filter_mask))
4020 return -EINVAL;
4022 filter_mask = nla_get_u32(extfilt);
4026 rcu_read_lock();
4027 for_each_netdev_rcu(net, dev) {
4028 const struct net_device_ops *ops = dev->netdev_ops;
4029 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4031 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4032 if (idx >= cb->args[0]) {
4033 err = br_dev->netdev_ops->ndo_bridge_getlink(
4034 skb, portid, seq, dev,
4035 filter_mask, NLM_F_MULTI);
4036 if (err < 0 && err != -EOPNOTSUPP) {
4037 if (likely(skb->len))
4038 break;
4040 goto out_err;
4043 idx++;
4046 if (ops->ndo_bridge_getlink) {
4047 if (idx >= cb->args[0]) {
4048 err = ops->ndo_bridge_getlink(skb, portid,
4049 seq, dev,
4050 filter_mask,
4051 NLM_F_MULTI);
4052 if (err < 0 && err != -EOPNOTSUPP) {
4053 if (likely(skb->len))
4054 break;
4056 goto out_err;
4059 idx++;
4062 err = skb->len;
4063 out_err:
4064 rcu_read_unlock();
4065 cb->args[0] = idx;
4067 return err;
4070 static inline size_t bridge_nlmsg_size(void)
4072 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4073 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4074 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4075 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4076 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4077 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4078 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4079 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4080 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4081 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4082 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4085 static int rtnl_bridge_notify(struct net_device *dev)
4087 struct net *net = dev_net(dev);
4088 struct sk_buff *skb;
4089 int err = -EOPNOTSUPP;
4091 if (!dev->netdev_ops->ndo_bridge_getlink)
4092 return 0;
4094 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4095 if (!skb) {
4096 err = -ENOMEM;
4097 goto errout;
4100 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4101 if (err < 0)
4102 goto errout;
4104 if (!skb->len)
4105 goto errout;
4107 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4108 return 0;
4109 errout:
4110 WARN_ON(err == -EMSGSIZE);
4111 kfree_skb(skb);
4112 if (err)
4113 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4114 return err;
4117 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4118 struct netlink_ext_ack *extack)
4120 struct net *net = sock_net(skb->sk);
4121 struct ifinfomsg *ifm;
4122 struct net_device *dev;
4123 struct nlattr *br_spec, *attr = NULL;
4124 int rem, err = -EOPNOTSUPP;
4125 u16 flags = 0;
4126 bool have_flags = false;
4128 if (nlmsg_len(nlh) < sizeof(*ifm))
4129 return -EINVAL;
4131 ifm = nlmsg_data(nlh);
4132 if (ifm->ifi_family != AF_BRIDGE)
4133 return -EPFNOSUPPORT;
4135 dev = __dev_get_by_index(net, ifm->ifi_index);
4136 if (!dev) {
4137 NL_SET_ERR_MSG(extack, "unknown ifindex");
4138 return -ENODEV;
4141 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4142 if (br_spec) {
4143 nla_for_each_nested(attr, br_spec, rem) {
4144 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4145 if (nla_len(attr) < sizeof(flags))
4146 return -EINVAL;
4148 have_flags = true;
4149 flags = nla_get_u16(attr);
4150 break;
4155 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4156 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4158 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4159 err = -EOPNOTSUPP;
4160 goto out;
4163 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
4164 if (err)
4165 goto out;
4167 flags &= ~BRIDGE_FLAGS_MASTER;
4170 if ((flags & BRIDGE_FLAGS_SELF)) {
4171 if (!dev->netdev_ops->ndo_bridge_setlink)
4172 err = -EOPNOTSUPP;
4173 else
4174 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4175 flags);
4176 if (!err) {
4177 flags &= ~BRIDGE_FLAGS_SELF;
4179 /* Generate event to notify upper layer of bridge
4180 * change
4182 err = rtnl_bridge_notify(dev);
4186 if (have_flags)
4187 memcpy(nla_data(attr), &flags, sizeof(flags));
4188 out:
4189 return err;
4192 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4193 struct netlink_ext_ack *extack)
4195 struct net *net = sock_net(skb->sk);
4196 struct ifinfomsg *ifm;
4197 struct net_device *dev;
4198 struct nlattr *br_spec, *attr = NULL;
4199 int rem, err = -EOPNOTSUPP;
4200 u16 flags = 0;
4201 bool have_flags = false;
4203 if (nlmsg_len(nlh) < sizeof(*ifm))
4204 return -EINVAL;
4206 ifm = nlmsg_data(nlh);
4207 if (ifm->ifi_family != AF_BRIDGE)
4208 return -EPFNOSUPPORT;
4210 dev = __dev_get_by_index(net, ifm->ifi_index);
4211 if (!dev) {
4212 NL_SET_ERR_MSG(extack, "unknown ifindex");
4213 return -ENODEV;
4216 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4217 if (br_spec) {
4218 nla_for_each_nested(attr, br_spec, rem) {
4219 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4220 if (nla_len(attr) < sizeof(flags))
4221 return -EINVAL;
4223 have_flags = true;
4224 flags = nla_get_u16(attr);
4225 break;
4230 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4231 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4233 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4234 err = -EOPNOTSUPP;
4235 goto out;
4238 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4239 if (err)
4240 goto out;
4242 flags &= ~BRIDGE_FLAGS_MASTER;
4245 if ((flags & BRIDGE_FLAGS_SELF)) {
4246 if (!dev->netdev_ops->ndo_bridge_dellink)
4247 err = -EOPNOTSUPP;
4248 else
4249 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4250 flags);
4252 if (!err) {
4253 flags &= ~BRIDGE_FLAGS_SELF;
4255 /* Generate event to notify upper layer of bridge
4256 * change
4258 err = rtnl_bridge_notify(dev);
4262 if (have_flags)
4263 memcpy(nla_data(attr), &flags, sizeof(flags));
4264 out:
4265 return err;
4268 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4270 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4271 (!idxattr || idxattr == attrid);
4274 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4275 static int rtnl_get_offload_stats_attr_size(int attr_id)
4277 switch (attr_id) {
4278 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4279 return sizeof(struct rtnl_link_stats64);
4282 return 0;
4285 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4286 int *prividx)
4288 struct nlattr *attr = NULL;
4289 int attr_id, size;
4290 void *attr_data;
4291 int err;
4293 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4294 dev->netdev_ops->ndo_get_offload_stats))
4295 return -ENODATA;
4297 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4298 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4299 if (attr_id < *prividx)
4300 continue;
4302 size = rtnl_get_offload_stats_attr_size(attr_id);
4303 if (!size)
4304 continue;
4306 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4307 continue;
4309 attr = nla_reserve_64bit(skb, attr_id, size,
4310 IFLA_OFFLOAD_XSTATS_UNSPEC);
4311 if (!attr)
4312 goto nla_put_failure;
4314 attr_data = nla_data(attr);
4315 memset(attr_data, 0, size);
4316 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4317 attr_data);
4318 if (err)
4319 goto get_offload_stats_failure;
4322 if (!attr)
4323 return -ENODATA;
4325 *prividx = 0;
4326 return 0;
4328 nla_put_failure:
4329 err = -EMSGSIZE;
4330 get_offload_stats_failure:
4331 *prividx = attr_id;
4332 return err;
4335 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4337 int nla_size = 0;
4338 int attr_id;
4339 int size;
4341 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4342 dev->netdev_ops->ndo_get_offload_stats))
4343 return 0;
4345 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4346 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4347 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4348 continue;
4349 size = rtnl_get_offload_stats_attr_size(attr_id);
4350 nla_size += nla_total_size_64bit(size);
4353 if (nla_size != 0)
4354 nla_size += nla_total_size(0);
4356 return nla_size;
4359 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4360 int type, u32 pid, u32 seq, u32 change,
4361 unsigned int flags, unsigned int filter_mask,
4362 int *idxattr, int *prividx)
4364 struct if_stats_msg *ifsm;
4365 struct nlmsghdr *nlh;
4366 struct nlattr *attr;
4367 int s_prividx = *prividx;
4368 int err;
4370 ASSERT_RTNL();
4372 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4373 if (!nlh)
4374 return -EMSGSIZE;
4376 ifsm = nlmsg_data(nlh);
4377 ifsm->family = PF_UNSPEC;
4378 ifsm->pad1 = 0;
4379 ifsm->pad2 = 0;
4380 ifsm->ifindex = dev->ifindex;
4381 ifsm->filter_mask = filter_mask;
4383 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4384 struct rtnl_link_stats64 *sp;
4386 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4387 sizeof(struct rtnl_link_stats64),
4388 IFLA_STATS_UNSPEC);
4389 if (!attr)
4390 goto nla_put_failure;
4392 sp = nla_data(attr);
4393 dev_get_stats(dev, sp);
4396 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4397 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4399 if (ops && ops->fill_linkxstats) {
4400 *idxattr = IFLA_STATS_LINK_XSTATS;
4401 attr = nla_nest_start(skb,
4402 IFLA_STATS_LINK_XSTATS);
4403 if (!attr)
4404 goto nla_put_failure;
4406 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4407 nla_nest_end(skb, attr);
4408 if (err)
4409 goto nla_put_failure;
4410 *idxattr = 0;
4414 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4415 *idxattr)) {
4416 const struct rtnl_link_ops *ops = NULL;
4417 const struct net_device *master;
4419 master = netdev_master_upper_dev_get(dev);
4420 if (master)
4421 ops = master->rtnl_link_ops;
4422 if (ops && ops->fill_linkxstats) {
4423 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4424 attr = nla_nest_start(skb,
4425 IFLA_STATS_LINK_XSTATS_SLAVE);
4426 if (!attr)
4427 goto nla_put_failure;
4429 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4430 nla_nest_end(skb, attr);
4431 if (err)
4432 goto nla_put_failure;
4433 *idxattr = 0;
4437 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4438 *idxattr)) {
4439 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4440 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4441 if (!attr)
4442 goto nla_put_failure;
4444 err = rtnl_get_offload_stats(skb, dev, prividx);
4445 if (err == -ENODATA)
4446 nla_nest_cancel(skb, attr);
4447 else
4448 nla_nest_end(skb, attr);
4450 if (err && err != -ENODATA)
4451 goto nla_put_failure;
4452 *idxattr = 0;
4455 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4456 struct rtnl_af_ops *af_ops;
4458 *idxattr = IFLA_STATS_AF_SPEC;
4459 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4460 if (!attr)
4461 goto nla_put_failure;
4463 rcu_read_lock();
4464 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4465 if (af_ops->fill_stats_af) {
4466 struct nlattr *af;
4467 int err;
4469 af = nla_nest_start(skb, af_ops->family);
4470 if (!af) {
4471 rcu_read_unlock();
4472 goto nla_put_failure;
4474 err = af_ops->fill_stats_af(skb, dev);
4476 if (err == -ENODATA) {
4477 nla_nest_cancel(skb, af);
4478 } else if (err < 0) {
4479 rcu_read_unlock();
4480 goto nla_put_failure;
4483 nla_nest_end(skb, af);
4486 rcu_read_unlock();
4488 nla_nest_end(skb, attr);
4490 *idxattr = 0;
4493 nlmsg_end(skb, nlh);
4495 return 0;
4497 nla_put_failure:
4498 /* not a multi message or no progress mean a real error */
4499 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4500 nlmsg_cancel(skb, nlh);
4501 else
4502 nlmsg_end(skb, nlh);
4504 return -EMSGSIZE;
4507 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4508 u32 filter_mask)
4510 size_t size = 0;
4512 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4513 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4515 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4516 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4517 int attr = IFLA_STATS_LINK_XSTATS;
4519 if (ops && ops->get_linkxstats_size) {
4520 size += nla_total_size(ops->get_linkxstats_size(dev,
4521 attr));
4522 /* for IFLA_STATS_LINK_XSTATS */
4523 size += nla_total_size(0);
4527 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4528 struct net_device *_dev = (struct net_device *)dev;
4529 const struct rtnl_link_ops *ops = NULL;
4530 const struct net_device *master;
4532 /* netdev_master_upper_dev_get can't take const */
4533 master = netdev_master_upper_dev_get(_dev);
4534 if (master)
4535 ops = master->rtnl_link_ops;
4536 if (ops && ops->get_linkxstats_size) {
4537 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4539 size += nla_total_size(ops->get_linkxstats_size(dev,
4540 attr));
4541 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4542 size += nla_total_size(0);
4546 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4547 size += rtnl_get_offload_stats_size(dev);
4549 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4550 struct rtnl_af_ops *af_ops;
4552 /* for IFLA_STATS_AF_SPEC */
4553 size += nla_total_size(0);
4555 rcu_read_lock();
4556 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4557 if (af_ops->get_stats_af_size) {
4558 size += nla_total_size(
4559 af_ops->get_stats_af_size(dev));
4561 /* for AF_* */
4562 size += nla_total_size(0);
4565 rcu_read_unlock();
4568 return size;
4571 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4572 struct netlink_ext_ack *extack)
4574 struct net *net = sock_net(skb->sk);
4575 struct net_device *dev = NULL;
4576 int idxattr = 0, prividx = 0;
4577 struct if_stats_msg *ifsm;
4578 struct sk_buff *nskb;
4579 u32 filter_mask;
4580 int err;
4582 if (nlmsg_len(nlh) < sizeof(*ifsm))
4583 return -EINVAL;
4585 ifsm = nlmsg_data(nlh);
4586 if (ifsm->ifindex > 0)
4587 dev = __dev_get_by_index(net, ifsm->ifindex);
4588 else
4589 return -EINVAL;
4591 if (!dev)
4592 return -ENODEV;
4594 filter_mask = ifsm->filter_mask;
4595 if (!filter_mask)
4596 return -EINVAL;
4598 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4599 if (!nskb)
4600 return -ENOBUFS;
4602 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4603 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4604 0, filter_mask, &idxattr, &prividx);
4605 if (err < 0) {
4606 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4607 WARN_ON(err == -EMSGSIZE);
4608 kfree_skb(nskb);
4609 } else {
4610 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4613 return err;
4616 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4618 int h, s_h, err, s_idx, s_idxattr, s_prividx;
4619 struct net *net = sock_net(skb->sk);
4620 unsigned int flags = NLM_F_MULTI;
4621 struct if_stats_msg *ifsm;
4622 struct hlist_head *head;
4623 struct net_device *dev;
4624 u32 filter_mask = 0;
4625 int idx = 0;
4627 s_h = cb->args[0];
4628 s_idx = cb->args[1];
4629 s_idxattr = cb->args[2];
4630 s_prividx = cb->args[3];
4632 cb->seq = net->dev_base_seq;
4634 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4635 return -EINVAL;
4637 ifsm = nlmsg_data(cb->nlh);
4638 filter_mask = ifsm->filter_mask;
4639 if (!filter_mask)
4640 return -EINVAL;
4642 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4643 idx = 0;
4644 head = &net->dev_index_head[h];
4645 hlist_for_each_entry(dev, head, index_hlist) {
4646 if (idx < s_idx)
4647 goto cont;
4648 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4649 NETLINK_CB(cb->skb).portid,
4650 cb->nlh->nlmsg_seq, 0,
4651 flags, filter_mask,
4652 &s_idxattr, &s_prividx);
4653 /* If we ran out of room on the first message,
4654 * we're in trouble
4656 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4658 if (err < 0)
4659 goto out;
4660 s_prividx = 0;
4661 s_idxattr = 0;
4662 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4663 cont:
4664 idx++;
4667 out:
4668 cb->args[3] = s_prividx;
4669 cb->args[2] = s_idxattr;
4670 cb->args[1] = idx;
4671 cb->args[0] = h;
4673 return skb->len;
4676 /* Process one rtnetlink message. */
4678 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4679 struct netlink_ext_ack *extack)
4681 struct net *net = sock_net(skb->sk);
4682 struct rtnl_link *link;
4683 struct module *owner;
4684 int err = -EOPNOTSUPP;
4685 rtnl_doit_func doit;
4686 unsigned int flags;
4687 int kind;
4688 int family;
4689 int type;
4691 type = nlh->nlmsg_type;
4692 if (type > RTM_MAX)
4693 return -EOPNOTSUPP;
4695 type -= RTM_BASE;
4697 /* All the messages must have at least 1 byte length */
4698 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4699 return 0;
4701 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4702 kind = type&3;
4704 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4705 return -EPERM;
4707 rcu_read_lock();
4708 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4709 struct sock *rtnl;
4710 rtnl_dumpit_func dumpit;
4711 u16 min_dump_alloc = 0;
4713 link = rtnl_get_link(family, type);
4714 if (!link || !link->dumpit) {
4715 family = PF_UNSPEC;
4716 link = rtnl_get_link(family, type);
4717 if (!link || !link->dumpit)
4718 goto err_unlock;
4720 owner = link->owner;
4721 dumpit = link->dumpit;
4723 if (type == RTM_GETLINK - RTM_BASE)
4724 min_dump_alloc = rtnl_calcit(skb, nlh);
4726 err = 0;
4727 /* need to do this before rcu_read_unlock() */
4728 if (!try_module_get(owner))
4729 err = -EPROTONOSUPPORT;
4731 rcu_read_unlock();
4733 rtnl = net->rtnl;
4734 if (err == 0) {
4735 struct netlink_dump_control c = {
4736 .dump = dumpit,
4737 .min_dump_alloc = min_dump_alloc,
4738 .module = owner,
4740 err = netlink_dump_start(rtnl, skb, nlh, &c);
4741 /* netlink_dump_start() will keep a reference on
4742 * module if dump is still in progress.
4744 module_put(owner);
4746 return err;
4749 link = rtnl_get_link(family, type);
4750 if (!link || !link->doit) {
4751 family = PF_UNSPEC;
4752 link = rtnl_get_link(PF_UNSPEC, type);
4753 if (!link || !link->doit)
4754 goto out_unlock;
4757 owner = link->owner;
4758 if (!try_module_get(owner)) {
4759 err = -EPROTONOSUPPORT;
4760 goto out_unlock;
4763 flags = link->flags;
4764 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4765 doit = link->doit;
4766 rcu_read_unlock();
4767 if (doit)
4768 err = doit(skb, nlh, extack);
4769 module_put(owner);
4770 return err;
4772 rcu_read_unlock();
4774 rtnl_lock();
4775 link = rtnl_get_link(family, type);
4776 if (link && link->doit)
4777 err = link->doit(skb, nlh, extack);
4778 rtnl_unlock();
4780 module_put(owner);
4782 return err;
4784 out_unlock:
4785 rcu_read_unlock();
4786 return err;
4788 err_unlock:
4789 rcu_read_unlock();
4790 return -EOPNOTSUPP;
4793 static void rtnetlink_rcv(struct sk_buff *skb)
4795 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4798 static int rtnetlink_bind(struct net *net, int group)
4800 switch (group) {
4801 case RTNLGRP_IPV4_MROUTE_R:
4802 case RTNLGRP_IPV6_MROUTE_R:
4803 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4804 return -EPERM;
4805 break;
4807 return 0;
4810 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4812 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4814 switch (event) {
4815 case NETDEV_REBOOT:
4816 case NETDEV_CHANGEMTU:
4817 case NETDEV_CHANGEADDR:
4818 case NETDEV_CHANGENAME:
4819 case NETDEV_FEAT_CHANGE:
4820 case NETDEV_BONDING_FAILOVER:
4821 case NETDEV_POST_TYPE_CHANGE:
4822 case NETDEV_NOTIFY_PEERS:
4823 case NETDEV_CHANGEUPPER:
4824 case NETDEV_RESEND_IGMP:
4825 case NETDEV_CHANGEINFODATA:
4826 case NETDEV_CHANGELOWERSTATE:
4827 case NETDEV_CHANGE_TX_QUEUE_LEN:
4828 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4829 GFP_KERNEL, NULL, 0);
4830 break;
4831 default:
4832 break;
4834 return NOTIFY_DONE;
4837 static struct notifier_block rtnetlink_dev_notifier = {
4838 .notifier_call = rtnetlink_event,
4842 static int __net_init rtnetlink_net_init(struct net *net)
4844 struct sock *sk;
4845 struct netlink_kernel_cfg cfg = {
4846 .groups = RTNLGRP_MAX,
4847 .input = rtnetlink_rcv,
4848 .cb_mutex = &rtnl_mutex,
4849 .flags = NL_CFG_F_NONROOT_RECV,
4850 .bind = rtnetlink_bind,
4853 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4854 if (!sk)
4855 return -ENOMEM;
4856 net->rtnl = sk;
4857 return 0;
4860 static void __net_exit rtnetlink_net_exit(struct net *net)
4862 netlink_kernel_release(net->rtnl);
4863 net->rtnl = NULL;
4866 static struct pernet_operations rtnetlink_net_ops = {
4867 .init = rtnetlink_net_init,
4868 .exit = rtnetlink_net_exit,
4871 void __init rtnetlink_init(void)
4873 if (register_pernet_subsys(&rtnetlink_net_ops))
4874 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4876 register_netdevice_notifier(&rtnetlink_dev_notifier);
4878 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4879 rtnl_dump_ifinfo, 0);
4880 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4881 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4882 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4884 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4885 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4886 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
4888 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4889 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4890 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4892 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4893 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4894 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
4896 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,