1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetic was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 40
62 rtnl_dumpit_func dumpit;
68 static DEFINE_MUTEX(rtnl_mutex);
72 mutex_lock(&rtnl_mutex);
74 EXPORT_SYMBOL(rtnl_lock);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex);
80 EXPORT_SYMBOL(rtnl_lock_killable);
82 static struct sk_buff *defer_kfree_skb_list;
83 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
90 EXPORT_SYMBOL(rtnl_kfree_skbs);
92 void __rtnl_unlock(void)
94 struct sk_buff *head = defer_kfree_skb_list;
96 defer_kfree_skb_list = NULL;
98 mutex_unlock(&rtnl_mutex);
101 struct sk_buff *next = head->next;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex);
120 EXPORT_SYMBOL(rtnl_trylock);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex);
126 EXPORT_SYMBOL(rtnl_is_locked);
128 bool refcount_dec_and_rtnl_lock(refcount_t *r)
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
144 static inline int rtm_msgindex(int msgtype)
146 int msgindex = msgtype - RTM_BASE;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
158 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
160 struct rtnl_link __rcu **tab;
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
169 return rcu_dereference_rtnl(tab[msgtype]);
172 static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
183 msgindex = rtm_msgindex(msgtype);
186 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
196 old = rtnl_dereference(tab[msgindex]);
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
207 WARN_ON(link->owner && link->owner != owner);
210 WARN_ON(doit && link->doit && link->doit != doit);
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
215 link->dumpit = dumpit;
217 link->flags |= flags;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
249 EXPORT_SYMBOL_GPL(rtnl_register_module);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol, int msgtype,
268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol, int msgtype)
289 struct rtnl_link __rcu **tab;
290 struct rtnl_link *link;
293 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
294 msgindex = rtm_msgindex(msgtype);
297 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
303 link = rtnl_dereference(tab[msgindex]);
304 rcu_assign_pointer(tab[msgindex], NULL);
307 kfree_rcu(link, rcu);
311 EXPORT_SYMBOL_GPL(rtnl_unregister);
314 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
315 * @protocol : Protocol family or PF_UNSPEC
317 * Identical to calling rtnl_unregster() for all registered message types
318 * of a certain protocol family.
320 void rtnl_unregister_all(int protocol)
322 struct rtnl_link __rcu **tab;
323 struct rtnl_link *link;
326 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
329 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
334 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
335 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
336 link = rtnl_dereference(tab[msgindex]);
340 rcu_assign_pointer(tab[msgindex], NULL);
341 kfree_rcu(link, rcu);
349 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
351 static LIST_HEAD(link_ops);
353 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
355 const struct rtnl_link_ops *ops;
357 list_for_each_entry(ops, &link_ops, list) {
358 if (!strcmp(ops->kind, kind))
365 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
366 * @ops: struct rtnl_link_ops * to register
368 * The caller must hold the rtnl_mutex. This function should be used
369 * by drivers that create devices during module initialization. It
370 * must be called before registering the devices.
372 * Returns 0 on success or a negative error code.
374 int __rtnl_link_register(struct rtnl_link_ops *ops)
376 if (rtnl_link_ops_get(ops->kind))
379 /* The check for alloc/setup is here because if ops
380 * does not have that filled up, it is not possible
381 * to use the ops for creating device. So do not
382 * fill up dellink as well. That disables rtnl_dellink.
384 if ((ops->alloc || ops->setup) && !ops->dellink)
385 ops->dellink = unregister_netdevice_queue;
387 list_add_tail(&ops->list, &link_ops);
390 EXPORT_SYMBOL_GPL(__rtnl_link_register);
393 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
394 * @ops: struct rtnl_link_ops * to register
396 * Returns 0 on success or a negative error code.
398 int rtnl_link_register(struct rtnl_link_ops *ops)
402 /* Sanity-check max sizes to avoid stack buffer overflow. */
403 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
404 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
408 err = __rtnl_link_register(ops);
412 EXPORT_SYMBOL_GPL(rtnl_link_register);
414 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
416 struct net_device *dev;
417 LIST_HEAD(list_kill);
419 for_each_netdev(net, dev) {
420 if (dev->rtnl_link_ops == ops)
421 ops->dellink(dev, &list_kill);
423 unregister_netdevice_many(&list_kill);
427 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
428 * @ops: struct rtnl_link_ops * to unregister
430 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
431 * integrity (hold pernet_ops_rwsem for writing to close the race
432 * with setup_net() and cleanup_net()).
434 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
439 __rtnl_kill_links(net, ops);
441 list_del(&ops->list);
443 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
445 /* Return with the rtnl_lock held when there are no network
446 * devices unregistering in any network namespace.
448 static void rtnl_lock_unregistering_all(void)
452 DEFINE_WAIT_FUNC(wait, woken_wake_function);
454 add_wait_queue(&netdev_unregistering_wq, &wait);
456 unregistering = false;
458 /* We held write locked pernet_ops_rwsem, and parallel
459 * setup_net() and cleanup_net() are not possible.
462 if (net->dev_unreg_count > 0) {
463 unregistering = true;
471 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
473 remove_wait_queue(&netdev_unregistering_wq, &wait);
477 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
478 * @ops: struct rtnl_link_ops * to unregister
480 void rtnl_link_unregister(struct rtnl_link_ops *ops)
482 /* Close the race with setup_net() and cleanup_net() */
483 down_write(&pernet_ops_rwsem);
484 rtnl_lock_unregistering_all();
485 __rtnl_link_unregister(ops);
487 up_write(&pernet_ops_rwsem);
489 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
491 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
493 struct net_device *master_dev;
494 const struct rtnl_link_ops *ops;
499 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
503 ops = master_dev->rtnl_link_ops;
504 if (!ops || !ops->get_slave_size)
506 /* IFLA_INFO_SLAVE_DATA + nested data */
507 size = nla_total_size(sizeof(struct nlattr)) +
508 ops->get_slave_size(master_dev, dev);
515 static size_t rtnl_link_get_size(const struct net_device *dev)
517 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
523 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
524 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
527 /* IFLA_INFO_DATA + nested data */
528 size += nla_total_size(sizeof(struct nlattr)) +
531 if (ops->get_xstats_size)
532 /* IFLA_INFO_XSTATS */
533 size += nla_total_size(ops->get_xstats_size(dev));
535 size += rtnl_link_get_slave_info_data_size(dev);
540 static LIST_HEAD(rtnl_af_ops);
542 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
544 const struct rtnl_af_ops *ops;
548 list_for_each_entry(ops, &rtnl_af_ops, list) {
549 if (ops->family == family)
557 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
558 * @ops: struct rtnl_af_ops * to register
560 * Returns 0 on success or a negative error code.
562 void rtnl_af_register(struct rtnl_af_ops *ops)
565 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
568 EXPORT_SYMBOL_GPL(rtnl_af_register);
571 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
572 * @ops: struct rtnl_af_ops * to unregister
574 void rtnl_af_unregister(struct rtnl_af_ops *ops)
577 list_del_rcu(&ops->list);
582 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
584 static size_t rtnl_link_get_af_size(const struct net_device *dev,
587 struct rtnl_af_ops *af_ops;
591 size = nla_total_size(sizeof(struct nlattr));
594 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
595 if (af_ops->get_link_af_size) {
596 /* AF_* + nested data */
597 size += nla_total_size(sizeof(struct nlattr)) +
598 af_ops->get_link_af_size(dev, ext_filter_mask);
606 static bool rtnl_have_link_slave_info(const struct net_device *dev)
608 struct net_device *master_dev;
613 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
614 if (master_dev && master_dev->rtnl_link_ops)
620 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
621 const struct net_device *dev)
623 struct net_device *master_dev;
624 const struct rtnl_link_ops *ops;
625 struct nlattr *slave_data;
628 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
631 ops = master_dev->rtnl_link_ops;
634 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
636 if (ops->fill_slave_info) {
637 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
640 err = ops->fill_slave_info(skb, master_dev, dev);
642 goto err_cancel_slave_data;
643 nla_nest_end(skb, slave_data);
647 err_cancel_slave_data:
648 nla_nest_cancel(skb, slave_data);
652 static int rtnl_link_info_fill(struct sk_buff *skb,
653 const struct net_device *dev)
655 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
661 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
663 if (ops->fill_xstats) {
664 err = ops->fill_xstats(skb, dev);
668 if (ops->fill_info) {
669 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
672 err = ops->fill_info(skb, dev);
674 goto err_cancel_data;
675 nla_nest_end(skb, data);
680 nla_nest_cancel(skb, data);
684 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
686 struct nlattr *linkinfo;
689 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
690 if (linkinfo == NULL)
693 err = rtnl_link_info_fill(skb, dev);
695 goto err_cancel_link;
697 err = rtnl_link_slave_info_fill(skb, dev);
699 goto err_cancel_link;
701 nla_nest_end(skb, linkinfo);
705 nla_nest_cancel(skb, linkinfo);
710 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
712 struct sock *rtnl = net->rtnl;
714 return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
717 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
719 struct sock *rtnl = net->rtnl;
721 return nlmsg_unicast(rtnl, skb, pid);
723 EXPORT_SYMBOL(rtnl_unicast);
725 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
726 struct nlmsghdr *nlh, gfp_t flags)
728 struct sock *rtnl = net->rtnl;
730 nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
732 EXPORT_SYMBOL(rtnl_notify);
734 void rtnl_set_sk_err(struct net *net, u32 group, int error)
736 struct sock *rtnl = net->rtnl;
738 netlink_set_err(rtnl, 0, group, error);
740 EXPORT_SYMBOL(rtnl_set_sk_err);
742 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
747 /* nothing is dumped for dst_default_metrics, so just skip the loop */
748 if (metrics == dst_default_metrics.metrics)
751 mx = nla_nest_start_noflag(skb, RTA_METRICS);
755 for (i = 0; i < RTAX_MAX; i++) {
757 if (i == RTAX_CC_ALGO - 1) {
758 char tmp[TCP_CA_NAME_MAX], *name;
760 name = tcp_ca_get_name_by_key(metrics[i], tmp);
763 if (nla_put_string(skb, i + 1, name))
764 goto nla_put_failure;
765 } else if (i == RTAX_FEATURES - 1) {
766 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
770 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
771 if (nla_put_u32(skb, i + 1, user_features))
772 goto nla_put_failure;
774 if (nla_put_u32(skb, i + 1, metrics[i]))
775 goto nla_put_failure;
782 nla_nest_cancel(skb, mx);
786 return nla_nest_end(skb, mx);
789 nla_nest_cancel(skb, mx);
792 EXPORT_SYMBOL(rtnetlink_put_metrics);
794 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
795 long expires, u32 error)
797 struct rta_cacheinfo ci = {
803 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
804 ci.rta_used = dst->__use;
805 ci.rta_clntref = atomic_read(&dst->__refcnt);
810 clock = jiffies_to_clock_t(abs(expires));
811 clock = min_t(unsigned long, clock, INT_MAX);
812 ci.rta_expires = (expires > 0) ? clock : -clock;
814 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
816 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
818 static void set_operstate(struct net_device *dev, unsigned char transition)
820 unsigned char operstate = dev->operstate;
822 switch (transition) {
824 if ((operstate == IF_OPER_DORMANT ||
825 operstate == IF_OPER_TESTING ||
826 operstate == IF_OPER_UNKNOWN) &&
827 !netif_dormant(dev) && !netif_testing(dev))
828 operstate = IF_OPER_UP;
831 case IF_OPER_TESTING:
832 if (operstate == IF_OPER_UP ||
833 operstate == IF_OPER_UNKNOWN)
834 operstate = IF_OPER_TESTING;
837 case IF_OPER_DORMANT:
838 if (operstate == IF_OPER_UP ||
839 operstate == IF_OPER_UNKNOWN)
840 operstate = IF_OPER_DORMANT;
844 if (dev->operstate != operstate) {
845 write_lock(&dev_base_lock);
846 dev->operstate = operstate;
847 write_unlock(&dev_base_lock);
848 netdev_state_change(dev);
852 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
854 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
858 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
859 const struct ifinfomsg *ifm)
861 unsigned int flags = ifm->ifi_flags;
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 flags = (flags & ifm->ifi_change) |
866 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
871 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
872 const struct rtnl_link_stats64 *b)
874 a->rx_packets = b->rx_packets;
875 a->tx_packets = b->tx_packets;
876 a->rx_bytes = b->rx_bytes;
877 a->tx_bytes = b->tx_bytes;
878 a->rx_errors = b->rx_errors;
879 a->tx_errors = b->tx_errors;
880 a->rx_dropped = b->rx_dropped;
881 a->tx_dropped = b->tx_dropped;
883 a->multicast = b->multicast;
884 a->collisions = b->collisions;
886 a->rx_length_errors = b->rx_length_errors;
887 a->rx_over_errors = b->rx_over_errors;
888 a->rx_crc_errors = b->rx_crc_errors;
889 a->rx_frame_errors = b->rx_frame_errors;
890 a->rx_fifo_errors = b->rx_fifo_errors;
891 a->rx_missed_errors = b->rx_missed_errors;
893 a->tx_aborted_errors = b->tx_aborted_errors;
894 a->tx_carrier_errors = b->tx_carrier_errors;
895 a->tx_fifo_errors = b->tx_fifo_errors;
896 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897 a->tx_window_errors = b->tx_window_errors;
899 a->rx_compressed = b->rx_compressed;
900 a->tx_compressed = b->tx_compressed;
902 a->rx_nohandler = b->rx_nohandler;
906 static inline int rtnl_vfinfo_size(const struct net_device *dev,
909 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
910 int num_vfs = dev_num_vf(dev->dev.parent);
911 size_t size = nla_total_size(0);
914 nla_total_size(sizeof(struct ifla_vf_mac)) +
915 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
922 nla_total_size(sizeof(struct ifla_vf_rate)) +
923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
925 nla_total_size(0) + /* nest IFLA_VF_STATS */
926 /* IFLA_VF_STATS_RX_PACKETS */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_TX_PACKETS */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_RX_BYTES */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_TX_BYTES */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_BROADCAST */
935 nla_total_size_64bit(sizeof(__u64)) +
936 /* IFLA_VF_STATS_MULTICAST */
937 nla_total_size_64bit(sizeof(__u64)) +
938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
942 nla_total_size(sizeof(struct ifla_vf_trust)));
948 static size_t rtnl_port_size(const struct net_device *dev,
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
970 return port_self_size;
973 static size_t rtnl_xdp_size(void)
975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
976 nla_total_size(1) + /* XDP_ATTACHED */
977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
983 static size_t rtnl_prop_list_size(const struct net_device *dev)
985 struct netdev_name_node *name_node;
988 if (list_empty(&dev->name_node->list))
990 size = nla_total_size(0);
991 list_for_each_entry(name_node, &dev->name_node->list, list)
992 size += nla_total_size(ALTIFNAMSIZ);
996 static size_t rtnl_proto_down_size(const struct net_device *dev)
998 size_t size = nla_total_size(1);
1000 if (dev->proto_down_reason)
1001 size += nla_total_size(0) + nla_total_size(4);
1006 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1007 u32 ext_filter_mask)
1009 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1010 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1011 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1012 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1013 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1014 + nla_total_size(sizeof(struct rtnl_link_stats))
1015 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1016 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1017 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1018 + nla_total_size(4) /* IFLA_TXQLEN */
1019 + nla_total_size(4) /* IFLA_WEIGHT */
1020 + nla_total_size(4) /* IFLA_MTU */
1021 + nla_total_size(4) /* IFLA_LINK */
1022 + nla_total_size(4) /* IFLA_MASTER */
1023 + nla_total_size(1) /* IFLA_CARRIER */
1024 + nla_total_size(4) /* IFLA_PROMISCUITY */
1025 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1026 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1027 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1028 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1029 + nla_total_size(1) /* IFLA_OPERSTATE */
1030 + nla_total_size(1) /* IFLA_LINKMODE */
1031 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1032 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1033 + nla_total_size(4) /* IFLA_GROUP */
1034 + nla_total_size(ext_filter_mask
1035 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1036 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1037 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1038 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1039 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1040 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1041 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1042 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1043 + rtnl_xdp_size() /* IFLA_XDP */
1044 + nla_total_size(4) /* IFLA_EVENT */
1045 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1046 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1047 + rtnl_proto_down_size(dev) /* proto down */
1048 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1049 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1050 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1051 + nla_total_size(4) /* IFLA_MIN_MTU */
1052 + nla_total_size(4) /* IFLA_MAX_MTU */
1053 + rtnl_prop_list_size(dev)
1054 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1058 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1060 struct nlattr *vf_ports;
1061 struct nlattr *vf_port;
1065 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1069 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1070 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1072 goto nla_put_failure;
1073 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1074 goto nla_put_failure;
1075 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1076 if (err == -EMSGSIZE)
1077 goto nla_put_failure;
1079 nla_nest_cancel(skb, vf_port);
1082 nla_nest_end(skb, vf_port);
1085 nla_nest_end(skb, vf_ports);
1090 nla_nest_cancel(skb, vf_ports);
1094 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1096 struct nlattr *port_self;
1099 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1103 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1105 nla_nest_cancel(skb, port_self);
1106 return (err == -EMSGSIZE) ? err : 0;
1109 nla_nest_end(skb, port_self);
1114 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1115 u32 ext_filter_mask)
1119 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1120 !(ext_filter_mask & RTEXT_FILTER_VF))
1123 err = rtnl_port_self_fill(skb, dev);
1127 if (dev_num_vf(dev->dev.parent)) {
1128 err = rtnl_vf_ports_fill(skb, dev);
1136 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1139 struct netdev_phys_item_id ppid;
1141 err = dev_get_phys_port_id(dev, &ppid);
1143 if (err == -EOPNOTSUPP)
1148 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1154 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1156 char name[IFNAMSIZ];
1159 err = dev_get_phys_port_name(dev, name, sizeof(name));
1161 if (err == -EOPNOTSUPP)
1166 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1172 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1174 struct netdev_phys_item_id ppid = { };
1177 err = dev_get_port_parent_id(dev, &ppid, false);
1179 if (err == -EOPNOTSUPP)
1184 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1190 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1191 struct net_device *dev)
1193 struct rtnl_link_stats64 *sp;
1194 struct nlattr *attr;
1196 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1197 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1201 sp = nla_data(attr);
1202 dev_get_stats(dev, sp);
1204 attr = nla_reserve(skb, IFLA_STATS,
1205 sizeof(struct rtnl_link_stats));
1209 copy_rtnl_link_stats(nla_data(attr), sp);
1214 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1215 struct net_device *dev,
1217 struct nlattr *vfinfo)
1219 struct ifla_vf_rss_query_en vf_rss_query_en;
1220 struct nlattr *vf, *vfstats, *vfvlanlist;
1221 struct ifla_vf_link_state vf_linkstate;
1222 struct ifla_vf_vlan_info vf_vlan_info;
1223 struct ifla_vf_spoofchk vf_spoofchk;
1224 struct ifla_vf_tx_rate vf_tx_rate;
1225 struct ifla_vf_stats vf_stats;
1226 struct ifla_vf_trust vf_trust;
1227 struct ifla_vf_vlan vf_vlan;
1228 struct ifla_vf_rate vf_rate;
1229 struct ifla_vf_mac vf_mac;
1230 struct ifla_vf_broadcast vf_broadcast;
1231 struct ifla_vf_info ivi;
1232 struct ifla_vf_guid node_guid;
1233 struct ifla_vf_guid port_guid;
1235 memset(&ivi, 0, sizeof(ivi));
1237 /* Not all SR-IOV capable drivers support the
1238 * spoofcheck and "RSS query enable" query. Preset to
1239 * -1 so the user space tool can detect that the driver
1240 * didn't report anything.
1243 ivi.rss_query_en = -1;
1245 /* The default value for VF link state is "auto"
1246 * IFLA_VF_LINK_STATE_AUTO which equals zero
1249 /* VLAN Protocol by default is 802.1Q */
1250 ivi.vlan_proto = htons(ETH_P_8021Q);
1251 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1254 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1255 memset(&node_guid, 0, sizeof(node_guid));
1256 memset(&port_guid, 0, sizeof(port_guid));
1265 vf_rss_query_en.vf =
1268 port_guid.vf = ivi.vf;
1270 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1271 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1272 vf_vlan.vlan = ivi.vlan;
1273 vf_vlan.qos = ivi.qos;
1274 vf_vlan_info.vlan = ivi.vlan;
1275 vf_vlan_info.qos = ivi.qos;
1276 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1277 vf_tx_rate.rate = ivi.max_tx_rate;
1278 vf_rate.min_tx_rate = ivi.min_tx_rate;
1279 vf_rate.max_tx_rate = ivi.max_tx_rate;
1280 vf_spoofchk.setting = ivi.spoofchk;
1281 vf_linkstate.link_state = ivi.linkstate;
1282 vf_rss_query_en.setting = ivi.rss_query_en;
1283 vf_trust.setting = ivi.trusted;
1284 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1286 goto nla_put_vfinfo_failure;
1287 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1288 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1289 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1290 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1292 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1294 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1296 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1298 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1299 sizeof(vf_rss_query_en),
1300 &vf_rss_query_en) ||
1301 nla_put(skb, IFLA_VF_TRUST,
1302 sizeof(vf_trust), &vf_trust))
1303 goto nla_put_vf_failure;
1305 if (dev->netdev_ops->ndo_get_vf_guid &&
1306 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1308 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1310 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1312 goto nla_put_vf_failure;
1314 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1316 goto nla_put_vf_failure;
1317 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1319 nla_nest_cancel(skb, vfvlanlist);
1320 goto nla_put_vf_failure;
1322 nla_nest_end(skb, vfvlanlist);
1323 memset(&vf_stats, 0, sizeof(vf_stats));
1324 if (dev->netdev_ops->ndo_get_vf_stats)
1325 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1327 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1329 goto nla_put_vf_failure;
1330 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1331 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1332 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1333 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1334 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1335 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1336 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1337 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1338 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1339 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1340 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1341 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1342 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1343 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1344 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1345 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1346 nla_nest_cancel(skb, vfstats);
1347 goto nla_put_vf_failure;
1349 nla_nest_end(skb, vfstats);
1350 nla_nest_end(skb, vf);
1354 nla_nest_cancel(skb, vf);
1355 nla_put_vfinfo_failure:
1356 nla_nest_cancel(skb, vfinfo);
1360 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1361 struct net_device *dev,
1362 u32 ext_filter_mask)
1364 struct nlattr *vfinfo;
1367 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1370 num_vfs = dev_num_vf(dev->dev.parent);
1371 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1374 if (!dev->netdev_ops->ndo_get_vf_config)
1377 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1381 for (i = 0; i < num_vfs; i++) {
1382 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1386 nla_nest_end(skb, vfinfo);
1390 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1392 struct rtnl_link_ifmap map;
1394 memset(&map, 0, sizeof(map));
1395 map.mem_start = dev->mem_start;
1396 map.mem_end = dev->mem_end;
1397 map.base_addr = dev->base_addr;
1400 map.port = dev->if_port;
1402 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1408 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1410 const struct bpf_prog *generic_xdp_prog;
1414 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1415 if (!generic_xdp_prog)
1417 return generic_xdp_prog->aux->id;
1420 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1422 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1425 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1427 return dev_xdp_prog_id(dev, XDP_MODE_HW);
1430 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1431 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1432 u32 (*get_prog_id)(struct net_device *dev))
1437 curr_id = get_prog_id(dev);
1442 err = nla_put_u32(skb, attr, curr_id);
1446 if (*mode != XDP_ATTACHED_NONE)
1447 *mode = XDP_ATTACHED_MULTI;
1454 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1461 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1466 mode = XDP_ATTACHED_NONE;
1467 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1468 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1471 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1472 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1475 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1476 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1480 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1484 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1485 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1490 nla_nest_end(skb, xdp);
1494 nla_nest_cancel(skb, xdp);
1498 static u32 rtnl_get_event(unsigned long event)
1500 u32 rtnl_event_type = IFLA_EVENT_NONE;
1504 rtnl_event_type = IFLA_EVENT_REBOOT;
1506 case NETDEV_FEAT_CHANGE:
1507 rtnl_event_type = IFLA_EVENT_FEATURES;
1509 case NETDEV_BONDING_FAILOVER:
1510 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1512 case NETDEV_NOTIFY_PEERS:
1513 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1515 case NETDEV_RESEND_IGMP:
1516 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1518 case NETDEV_CHANGEINFODATA:
1519 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1525 return rtnl_event_type;
1528 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1530 const struct net_device *upper_dev;
1535 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1537 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1543 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1546 int ifindex = dev_get_iflink(dev);
1548 if (force || dev->ifindex != ifindex)
1549 return nla_put_u32(skb, IFLA_LINK, ifindex);
1554 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1555 struct net_device *dev)
1560 ret = dev_get_alias(dev, buf, sizeof(buf));
1561 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1564 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1565 const struct net_device *dev,
1566 struct net *src_net, gfp_t gfp)
1568 bool put_iflink = false;
1570 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1571 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1573 if (!net_eq(dev_net(dev), link_net)) {
1574 int id = peernet2id_alloc(src_net, link_net, gfp);
1576 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1583 return nla_put_iflink(skb, dev, put_iflink);
1586 static int rtnl_fill_link_af(struct sk_buff *skb,
1587 const struct net_device *dev,
1588 u32 ext_filter_mask)
1590 const struct rtnl_af_ops *af_ops;
1591 struct nlattr *af_spec;
1593 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1597 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1601 if (!af_ops->fill_link_af)
1604 af = nla_nest_start_noflag(skb, af_ops->family);
1608 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1610 * Caller may return ENODATA to indicate that there
1611 * was no data to be dumped. This is not an error, it
1612 * means we should trim the attribute header and
1615 if (err == -ENODATA)
1616 nla_nest_cancel(skb, af);
1620 nla_nest_end(skb, af);
1623 nla_nest_end(skb, af_spec);
1627 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1628 const struct net_device *dev)
1630 struct netdev_name_node *name_node;
1633 list_for_each_entry(name_node, &dev->name_node->list, list) {
1634 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1641 static int rtnl_fill_prop_list(struct sk_buff *skb,
1642 const struct net_device *dev)
1644 struct nlattr *prop_list;
1647 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1651 ret = rtnl_fill_alt_ifnames(skb, dev);
1655 nla_nest_end(skb, prop_list);
1659 nla_nest_cancel(skb, prop_list);
1663 static int rtnl_fill_proto_down(struct sk_buff *skb,
1664 const struct net_device *dev)
1669 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1670 goto nla_put_failure;
1672 preason = dev->proto_down_reason;
1676 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1680 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1681 nla_nest_cancel(skb, pr);
1682 goto nla_put_failure;
1685 nla_nest_end(skb, pr);
1692 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1693 struct net_device *dev, struct net *src_net,
1694 int type, u32 pid, u32 seq, u32 change,
1695 unsigned int flags, u32 ext_filter_mask,
1696 u32 event, int *new_nsid, int new_ifindex,
1697 int tgt_netnsid, gfp_t gfp)
1699 struct ifinfomsg *ifm;
1700 struct nlmsghdr *nlh;
1701 struct Qdisc *qdisc;
1704 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1708 ifm = nlmsg_data(nlh);
1709 ifm->ifi_family = AF_UNSPEC;
1711 ifm->ifi_type = dev->type;
1712 ifm->ifi_index = dev->ifindex;
1713 ifm->ifi_flags = dev_get_flags(dev);
1714 ifm->ifi_change = change;
1716 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1717 goto nla_put_failure;
1719 qdisc = rtnl_dereference(dev->qdisc);
1720 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1721 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1722 nla_put_u8(skb, IFLA_OPERSTATE,
1723 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1724 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1725 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1726 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1727 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1728 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1729 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1730 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1731 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1732 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1734 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1736 put_master_ifindex(skb, dev) ||
1737 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1739 nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1740 nla_put_ifalias(skb, dev) ||
1741 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1742 atomic_read(&dev->carrier_up_count) +
1743 atomic_read(&dev->carrier_down_count)) ||
1744 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1745 atomic_read(&dev->carrier_up_count)) ||
1746 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1747 atomic_read(&dev->carrier_down_count)))
1748 goto nla_put_failure;
1750 if (rtnl_fill_proto_down(skb, dev))
1751 goto nla_put_failure;
1753 if (event != IFLA_EVENT_NONE) {
1754 if (nla_put_u32(skb, IFLA_EVENT, event))
1755 goto nla_put_failure;
1758 if (rtnl_fill_link_ifmap(skb, dev))
1759 goto nla_put_failure;
1761 if (dev->addr_len) {
1762 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1763 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1764 goto nla_put_failure;
1767 if (rtnl_phys_port_id_fill(skb, dev))
1768 goto nla_put_failure;
1770 if (rtnl_phys_port_name_fill(skb, dev))
1771 goto nla_put_failure;
1773 if (rtnl_phys_switch_id_fill(skb, dev))
1774 goto nla_put_failure;
1776 if (rtnl_fill_stats(skb, dev))
1777 goto nla_put_failure;
1779 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1780 goto nla_put_failure;
1782 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1783 goto nla_put_failure;
1785 if (rtnl_xdp_fill(skb, dev))
1786 goto nla_put_failure;
1788 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1789 if (rtnl_link_fill(skb, dev) < 0)
1790 goto nla_put_failure;
1793 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1794 goto nla_put_failure;
1797 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1798 goto nla_put_failure;
1800 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1801 goto nla_put_failure;
1803 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1804 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1805 goto nla_put_failure;
1808 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1809 goto nla_put_failure_rcu;
1812 if (rtnl_fill_prop_list(skb, dev))
1813 goto nla_put_failure;
1815 if (dev->dev.parent &&
1816 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1817 dev_name(dev->dev.parent)))
1818 goto nla_put_failure;
1820 if (dev->dev.parent && dev->dev.parent->bus &&
1821 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1822 dev->dev.parent->bus->name))
1823 goto nla_put_failure;
1825 nlmsg_end(skb, nlh);
1828 nla_put_failure_rcu:
1831 nlmsg_cancel(skb, nlh);
1835 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1836 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1837 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1838 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1839 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1840 [IFLA_MTU] = { .type = NLA_U32 },
1841 [IFLA_LINK] = { .type = NLA_U32 },
1842 [IFLA_MASTER] = { .type = NLA_U32 },
1843 [IFLA_CARRIER] = { .type = NLA_U8 },
1844 [IFLA_TXQLEN] = { .type = NLA_U32 },
1845 [IFLA_WEIGHT] = { .type = NLA_U32 },
1846 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1847 [IFLA_LINKMODE] = { .type = NLA_U8 },
1848 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1849 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1850 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1851 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1852 * allow 0-length string (needed to remove an alias).
1854 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1855 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1856 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1857 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1858 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1859 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1860 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1861 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1862 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1863 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1864 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1865 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1866 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1867 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1868 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1869 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1870 [IFLA_XDP] = { .type = NLA_NESTED },
1871 [IFLA_EVENT] = { .type = NLA_U32 },
1872 [IFLA_GROUP] = { .type = NLA_U32 },
1873 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1874 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1875 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1876 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1877 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1878 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1879 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1880 .len = ALTIFNAMSIZ - 1 },
1881 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1882 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1883 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
1884 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
1887 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1888 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1889 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1890 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1891 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1894 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1895 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1896 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1897 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1898 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1899 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1900 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1901 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1902 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1903 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1904 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1905 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1906 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1907 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1910 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1911 [IFLA_PORT_VF] = { .type = NLA_U32 },
1912 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1913 .len = PORT_PROFILE_MAX },
1914 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1915 .len = PORT_UUID_MAX },
1916 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1917 .len = PORT_UUID_MAX },
1918 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1919 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1921 /* Unused, but we need to keep it here since user space could
1922 * fill it. It's also broken with regard to NLA_BINARY use in
1923 * combination with structs.
1925 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1926 .len = sizeof(struct ifla_port_vsi) },
1929 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1930 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1931 [IFLA_XDP_FD] = { .type = NLA_S32 },
1932 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
1933 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1934 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1935 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1938 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1940 const struct rtnl_link_ops *ops = NULL;
1941 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1943 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1946 if (linfo[IFLA_INFO_KIND]) {
1947 char kind[MODULE_NAME_LEN];
1949 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1950 ops = rtnl_link_ops_get(kind);
1956 static bool link_master_filtered(struct net_device *dev, int master_idx)
1958 struct net_device *master;
1963 master = netdev_master_upper_dev_get(dev);
1965 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
1966 * another invalid value for ifindex to denote "no master".
1968 if (master_idx == -1)
1971 if (!master || master->ifindex != master_idx)
1977 static bool link_kind_filtered(const struct net_device *dev,
1978 const struct rtnl_link_ops *kind_ops)
1980 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1986 static bool link_dump_filtered(struct net_device *dev,
1988 const struct rtnl_link_ops *kind_ops)
1990 if (link_master_filtered(dev, master_idx) ||
1991 link_kind_filtered(dev, kind_ops))
1998 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1999 * @sk: netlink socket
2000 * @netnsid: network namespace identifier
2002 * Returns the network namespace identified by netnsid on success or an error
2003 * pointer on failure.
2005 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2009 net = get_net_ns_by_id(sock_net(sk), netnsid);
2011 return ERR_PTR(-EINVAL);
2013 /* For now, the caller is required to have CAP_NET_ADMIN in
2014 * the user namespace owning the target net ns.
2016 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2018 return ERR_PTR(-EACCES);
2022 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2024 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2025 bool strict_check, struct nlattr **tb,
2026 struct netlink_ext_ack *extack)
2031 struct ifinfomsg *ifm;
2033 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2034 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2038 ifm = nlmsg_data(nlh);
2039 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2041 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2044 if (ifm->ifi_index) {
2045 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2049 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2050 IFLA_MAX, ifla_policy,
2054 /* A hack to preserve kernel<->userspace interface.
2055 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2056 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2057 * what iproute2 < v3.9.0 used.
2058 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2059 * attribute, its netlink message is shorter than struct ifinfomsg.
2061 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2062 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2064 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2068 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2070 struct netlink_ext_ack *extack = cb->extack;
2071 const struct nlmsghdr *nlh = cb->nlh;
2072 struct net *net = sock_net(skb->sk);
2073 struct net *tgt_net = net;
2076 struct net_device *dev;
2077 struct hlist_head *head;
2078 struct nlattr *tb[IFLA_MAX+1];
2079 u32 ext_filter_mask = 0;
2080 const struct rtnl_link_ops *kind_ops = NULL;
2081 unsigned int flags = NLM_F_MULTI;
2087 s_idx = cb->args[1];
2089 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2091 if (cb->strict_check)
2097 for (i = 0; i <= IFLA_MAX; ++i) {
2101 /* new attributes should only be added with strict checking */
2103 case IFLA_TARGET_NETNSID:
2104 netnsid = nla_get_s32(tb[i]);
2105 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2106 if (IS_ERR(tgt_net)) {
2107 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2108 return PTR_ERR(tgt_net);
2112 ext_filter_mask = nla_get_u32(tb[i]);
2115 master_idx = nla_get_u32(tb[i]);
2118 kind_ops = linkinfo_to_kind_ops(tb[i]);
2121 if (cb->strict_check) {
2122 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2128 if (master_idx || kind_ops)
2129 flags |= NLM_F_DUMP_FILTERED;
2132 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2134 head = &tgt_net->dev_index_head[h];
2135 hlist_for_each_entry(dev, head, index_hlist) {
2136 if (link_dump_filtered(dev, master_idx, kind_ops))
2140 err = rtnl_fill_ifinfo(skb, dev, net,
2142 NETLINK_CB(cb->skb).portid,
2143 nlh->nlmsg_seq, 0, flags,
2144 ext_filter_mask, 0, NULL, 0,
2145 netnsid, GFP_KERNEL);
2148 if (likely(skb->len))
2162 cb->seq = tgt_net->dev_base_seq;
2163 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2170 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2171 struct netlink_ext_ack *exterr)
2173 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2176 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2178 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2181 /* Examine the link attributes and figure out which
2182 * network namespace we are talking about.
2184 if (tb[IFLA_NET_NS_PID])
2185 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2186 else if (tb[IFLA_NET_NS_FD])
2187 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2189 net = get_net(src_net);
2192 EXPORT_SYMBOL(rtnl_link_get_net);
2194 /* Figure out which network namespace we are talking about by
2195 * examining the link attributes in the following order:
2197 * 1. IFLA_NET_NS_PID
2199 * 3. IFLA_TARGET_NETNSID
2201 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2202 struct nlattr *tb[])
2206 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2207 return rtnl_link_get_net(src_net, tb);
2209 if (!tb[IFLA_TARGET_NETNSID])
2210 return get_net(src_net);
2212 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2214 return ERR_PTR(-EINVAL);
2219 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2220 struct net *src_net,
2221 struct nlattr *tb[], int cap)
2225 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2229 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2231 return ERR_PTR(-EPERM);
2237 /* Verify that rtnetlink requests do not pass additional properties
2238 * potentially referring to different network namespaces.
2240 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2241 struct netlink_ext_ack *extack,
2245 if (netns_id_only) {
2246 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2249 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2253 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2256 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2259 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2265 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2269 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2270 struct netlink_ext_ack *extack)
2273 if (tb[IFLA_ADDRESS] &&
2274 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2277 if (tb[IFLA_BROADCAST] &&
2278 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2282 if (tb[IFLA_AF_SPEC]) {
2286 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2287 const struct rtnl_af_ops *af_ops;
2289 af_ops = rtnl_af_lookup(nla_type(af));
2291 return -EAFNOSUPPORT;
2293 if (!af_ops->set_link_af)
2296 if (af_ops->validate_link_af) {
2297 err = af_ops->validate_link_af(dev, af, extack);
2307 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2310 const struct net_device_ops *ops = dev->netdev_ops;
2312 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2315 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2317 if (dev->type != ARPHRD_INFINIBAND)
2320 return handle_infiniband_guid(dev, ivt, guid_type);
2323 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2325 const struct net_device_ops *ops = dev->netdev_ops;
2328 if (tb[IFLA_VF_MAC]) {
2329 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2331 if (ivm->vf >= INT_MAX)
2334 if (ops->ndo_set_vf_mac)
2335 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2341 if (tb[IFLA_VF_VLAN]) {
2342 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2344 if (ivv->vf >= INT_MAX)
2347 if (ops->ndo_set_vf_vlan)
2348 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2350 htons(ETH_P_8021Q));
2355 if (tb[IFLA_VF_VLAN_LIST]) {
2356 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2357 struct nlattr *attr;
2361 if (!ops->ndo_set_vf_vlan)
2364 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2365 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2366 nla_len(attr) < NLA_HDRLEN) {
2369 if (len >= MAX_VLAN_LIST_LEN)
2371 ivvl[len] = nla_data(attr);
2378 if (ivvl[0]->vf >= INT_MAX)
2380 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2381 ivvl[0]->qos, ivvl[0]->vlan_proto);
2386 if (tb[IFLA_VF_TX_RATE]) {
2387 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2388 struct ifla_vf_info ivf;
2390 if (ivt->vf >= INT_MAX)
2393 if (ops->ndo_get_vf_config)
2394 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2399 if (ops->ndo_set_vf_rate)
2400 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2407 if (tb[IFLA_VF_RATE]) {
2408 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2410 if (ivt->vf >= INT_MAX)
2413 if (ops->ndo_set_vf_rate)
2414 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2421 if (tb[IFLA_VF_SPOOFCHK]) {
2422 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2424 if (ivs->vf >= INT_MAX)
2427 if (ops->ndo_set_vf_spoofchk)
2428 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2434 if (tb[IFLA_VF_LINK_STATE]) {
2435 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2437 if (ivl->vf >= INT_MAX)
2440 if (ops->ndo_set_vf_link_state)
2441 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2447 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2448 struct ifla_vf_rss_query_en *ivrssq_en;
2451 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2452 if (ivrssq_en->vf >= INT_MAX)
2454 if (ops->ndo_set_vf_rss_query_en)
2455 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2456 ivrssq_en->setting);
2461 if (tb[IFLA_VF_TRUST]) {
2462 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2464 if (ivt->vf >= INT_MAX)
2467 if (ops->ndo_set_vf_trust)
2468 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2473 if (tb[IFLA_VF_IB_NODE_GUID]) {
2474 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2476 if (ivt->vf >= INT_MAX)
2478 if (!ops->ndo_set_vf_guid)
2480 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2483 if (tb[IFLA_VF_IB_PORT_GUID]) {
2484 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2486 if (ivt->vf >= INT_MAX)
2488 if (!ops->ndo_set_vf_guid)
2491 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2497 static int do_set_master(struct net_device *dev, int ifindex,
2498 struct netlink_ext_ack *extack)
2500 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2501 const struct net_device_ops *ops;
2505 if (upper_dev->ifindex == ifindex)
2507 ops = upper_dev->netdev_ops;
2508 if (ops->ndo_del_slave) {
2509 err = ops->ndo_del_slave(upper_dev, dev);
2518 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2521 ops = upper_dev->netdev_ops;
2522 if (ops->ndo_add_slave) {
2523 err = ops->ndo_add_slave(upper_dev, dev, extack);
2533 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2534 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2535 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2538 static int do_set_proto_down(struct net_device *dev,
2539 struct nlattr *nl_proto_down,
2540 struct nlattr *nl_proto_down_reason,
2541 struct netlink_ext_ack *extack)
2543 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2544 const struct net_device_ops *ops = dev->netdev_ops;
2545 unsigned long mask = 0;
2550 if (!ops->ndo_change_proto_down) {
2551 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2555 if (nl_proto_down_reason) {
2556 err = nla_parse_nested_deprecated(pdreason,
2557 IFLA_PROTO_DOWN_REASON_MAX,
2558 nl_proto_down_reason,
2559 ifla_proto_down_reason_policy,
2564 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2565 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2569 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2571 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2572 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2574 dev_change_proto_down_reason(dev, mask, value);
2577 if (nl_proto_down) {
2578 proto_down = nla_get_u8(nl_proto_down);
2580 /* Don't turn off protodown if there are active reasons */
2581 if (!proto_down && dev->proto_down_reason) {
2582 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2585 err = dev_change_proto_down(dev,
2594 #define DO_SETLINK_MODIFIED 0x01
2595 /* notify flag means notify + modified. */
2596 #define DO_SETLINK_NOTIFY 0x03
2597 static int do_setlink(const struct sk_buff *skb,
2598 struct net_device *dev, struct ifinfomsg *ifm,
2599 struct netlink_ext_ack *extack,
2600 struct nlattr **tb, char *ifname, int status)
2602 const struct net_device_ops *ops = dev->netdev_ops;
2605 err = validate_linkmsg(dev, tb, extack);
2609 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2610 const char *pat = ifname && ifname[0] ? ifname : NULL;
2614 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2621 if (tb[IFLA_NEW_IFINDEX])
2622 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2626 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2630 status |= DO_SETLINK_MODIFIED;
2634 struct rtnl_link_ifmap *u_map;
2637 if (!ops->ndo_set_config) {
2642 if (!netif_device_present(dev)) {
2647 u_map = nla_data(tb[IFLA_MAP]);
2648 k_map.mem_start = (unsigned long) u_map->mem_start;
2649 k_map.mem_end = (unsigned long) u_map->mem_end;
2650 k_map.base_addr = (unsigned short) u_map->base_addr;
2651 k_map.irq = (unsigned char) u_map->irq;
2652 k_map.dma = (unsigned char) u_map->dma;
2653 k_map.port = (unsigned char) u_map->port;
2655 err = ops->ndo_set_config(dev, &k_map);
2659 status |= DO_SETLINK_NOTIFY;
2662 if (tb[IFLA_ADDRESS]) {
2663 struct sockaddr *sa;
2666 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2668 sa = kmalloc(len, GFP_KERNEL);
2673 sa->sa_family = dev->type;
2674 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2676 err = dev_set_mac_address_user(dev, sa, extack);
2680 status |= DO_SETLINK_MODIFIED;
2684 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2687 status |= DO_SETLINK_MODIFIED;
2690 if (tb[IFLA_GROUP]) {
2691 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2692 status |= DO_SETLINK_NOTIFY;
2696 * Interface selected by interface index but interface
2697 * name provided implies that a name change has been
2700 if (ifm->ifi_index > 0 && ifname[0]) {
2701 err = dev_change_name(dev, ifname);
2704 status |= DO_SETLINK_MODIFIED;
2707 if (tb[IFLA_IFALIAS]) {
2708 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2709 nla_len(tb[IFLA_IFALIAS]));
2712 status |= DO_SETLINK_NOTIFY;
2715 if (tb[IFLA_BROADCAST]) {
2716 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2717 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2720 if (ifm->ifi_flags || ifm->ifi_change) {
2721 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2727 if (tb[IFLA_MASTER]) {
2728 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2731 status |= DO_SETLINK_MODIFIED;
2734 if (tb[IFLA_CARRIER]) {
2735 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2738 status |= DO_SETLINK_MODIFIED;
2741 if (tb[IFLA_TXQLEN]) {
2742 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2744 err = dev_change_tx_queue_len(dev, value);
2747 status |= DO_SETLINK_MODIFIED;
2750 if (tb[IFLA_GSO_MAX_SIZE]) {
2751 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2753 if (max_size > GSO_MAX_SIZE) {
2758 if (dev->gso_max_size ^ max_size) {
2759 netif_set_gso_max_size(dev, max_size);
2760 status |= DO_SETLINK_MODIFIED;
2764 if (tb[IFLA_GSO_MAX_SEGS]) {
2765 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2767 if (max_segs > GSO_MAX_SEGS) {
2772 if (dev->gso_max_segs ^ max_segs) {
2773 dev->gso_max_segs = max_segs;
2774 status |= DO_SETLINK_MODIFIED;
2778 if (tb[IFLA_OPERSTATE])
2779 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2781 if (tb[IFLA_LINKMODE]) {
2782 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2784 write_lock(&dev_base_lock);
2785 if (dev->link_mode ^ value)
2786 status |= DO_SETLINK_NOTIFY;
2787 dev->link_mode = value;
2788 write_unlock(&dev_base_lock);
2791 if (tb[IFLA_VFINFO_LIST]) {
2792 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2793 struct nlattr *attr;
2796 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2797 if (nla_type(attr) != IFLA_VF_INFO ||
2798 nla_len(attr) < NLA_HDRLEN) {
2802 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2808 err = do_setvfinfo(dev, vfinfo);
2811 status |= DO_SETLINK_NOTIFY;
2816 if (tb[IFLA_VF_PORTS]) {
2817 struct nlattr *port[IFLA_PORT_MAX+1];
2818 struct nlattr *attr;
2823 if (!ops->ndo_set_vf_port)
2826 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2827 if (nla_type(attr) != IFLA_VF_PORT ||
2828 nla_len(attr) < NLA_HDRLEN) {
2832 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2838 if (!port[IFLA_PORT_VF]) {
2842 vf = nla_get_u32(port[IFLA_PORT_VF]);
2843 err = ops->ndo_set_vf_port(dev, vf, port);
2846 status |= DO_SETLINK_NOTIFY;
2851 if (tb[IFLA_PORT_SELF]) {
2852 struct nlattr *port[IFLA_PORT_MAX+1];
2854 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2856 ifla_port_policy, NULL);
2861 if (ops->ndo_set_vf_port)
2862 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2865 status |= DO_SETLINK_NOTIFY;
2868 if (tb[IFLA_AF_SPEC]) {
2872 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2873 const struct rtnl_af_ops *af_ops;
2875 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2877 err = af_ops->set_link_af(dev, af, extack);
2881 status |= DO_SETLINK_NOTIFY;
2886 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2887 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2888 tb[IFLA_PROTO_DOWN_REASON], extack);
2891 status |= DO_SETLINK_NOTIFY;
2895 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2898 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2900 ifla_xdp_policy, NULL);
2904 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2909 if (xdp[IFLA_XDP_FLAGS]) {
2910 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2911 if (xdp_flags & ~XDP_FLAGS_MASK) {
2915 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2921 if (xdp[IFLA_XDP_FD]) {
2922 int expected_fd = -1;
2924 if (xdp_flags & XDP_FLAGS_REPLACE) {
2925 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2930 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2933 err = dev_change_xdp_fd(dev, extack,
2934 nla_get_s32(xdp[IFLA_XDP_FD]),
2939 status |= DO_SETLINK_NOTIFY;
2944 if (status & DO_SETLINK_MODIFIED) {
2945 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2946 netdev_state_change(dev);
2949 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",
2956 static struct net_device *rtnl_dev_get(struct net *net,
2957 struct nlattr *ifname_attr,
2958 struct nlattr *altifname_attr,
2961 char buffer[ALTIFNAMSIZ];
2966 nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
2967 else if (altifname_attr)
2968 nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
2973 return __dev_get_by_name(net, ifname);
2976 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2977 struct netlink_ext_ack *extack)
2979 struct net *net = sock_net(skb->sk);
2980 struct ifinfomsg *ifm;
2981 struct net_device *dev;
2983 struct nlattr *tb[IFLA_MAX+1];
2984 char ifname[IFNAMSIZ];
2986 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2987 ifla_policy, extack);
2991 err = rtnl_ensure_unique_netns(tb, extack, false);
2995 if (tb[IFLA_IFNAME])
2996 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3001 ifm = nlmsg_data(nlh);
3002 if (ifm->ifi_index > 0)
3003 dev = __dev_get_by_index(net, ifm->ifi_index);
3004 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3005 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3014 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
3019 static int rtnl_group_dellink(const struct net *net, int group)
3021 struct net_device *dev, *aux;
3022 LIST_HEAD(list_kill);
3028 for_each_netdev(net, dev) {
3029 if (dev->group == group) {
3030 const struct rtnl_link_ops *ops;
3033 ops = dev->rtnl_link_ops;
3034 if (!ops || !ops->dellink)
3042 for_each_netdev_safe(net, dev, aux) {
3043 if (dev->group == group) {
3044 const struct rtnl_link_ops *ops;
3046 ops = dev->rtnl_link_ops;
3047 ops->dellink(dev, &list_kill);
3050 unregister_netdevice_many(&list_kill);
3055 int rtnl_delete_link(struct net_device *dev)
3057 const struct rtnl_link_ops *ops;
3058 LIST_HEAD(list_kill);
3060 ops = dev->rtnl_link_ops;
3061 if (!ops || !ops->dellink)
3064 ops->dellink(dev, &list_kill);
3065 unregister_netdevice_many(&list_kill);
3069 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3071 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3072 struct netlink_ext_ack *extack)
3074 struct net *net = sock_net(skb->sk);
3075 struct net *tgt_net = net;
3076 struct net_device *dev = NULL;
3077 struct ifinfomsg *ifm;
3078 struct nlattr *tb[IFLA_MAX+1];
3082 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3083 ifla_policy, extack);
3087 err = rtnl_ensure_unique_netns(tb, extack, true);
3091 if (tb[IFLA_TARGET_NETNSID]) {
3092 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3093 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3094 if (IS_ERR(tgt_net))
3095 return PTR_ERR(tgt_net);
3099 ifm = nlmsg_data(nlh);
3100 if (ifm->ifi_index > 0)
3101 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3102 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3103 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3104 tb[IFLA_ALT_IFNAME], NULL);
3105 else if (tb[IFLA_GROUP])
3106 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3111 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3117 err = rtnl_delete_link(dev);
3126 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3128 unsigned int old_flags;
3131 old_flags = dev->flags;
3132 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3133 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3139 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3140 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3142 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3143 __dev_notify_flags(dev, old_flags, ~0U);
3147 EXPORT_SYMBOL(rtnl_configure_link);
3149 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3150 unsigned char name_assign_type,
3151 const struct rtnl_link_ops *ops,
3152 struct nlattr *tb[],
3153 struct netlink_ext_ack *extack)
3155 struct net_device *dev;
3156 unsigned int num_tx_queues = 1;
3157 unsigned int num_rx_queues = 1;
3159 if (tb[IFLA_NUM_TX_QUEUES])
3160 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3161 else if (ops->get_num_tx_queues)
3162 num_tx_queues = ops->get_num_tx_queues();
3164 if (tb[IFLA_NUM_RX_QUEUES])
3165 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3166 else if (ops->get_num_rx_queues)
3167 num_rx_queues = ops->get_num_rx_queues();
3169 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3170 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3171 return ERR_PTR(-EINVAL);
3174 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3175 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3176 return ERR_PTR(-EINVAL);
3180 dev = ops->alloc(tb, ifname, name_assign_type,
3181 num_tx_queues, num_rx_queues);
3185 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3186 name_assign_type, ops->setup,
3187 num_tx_queues, num_rx_queues);
3191 return ERR_PTR(-ENOMEM);
3193 dev_net_set(dev, net);
3194 dev->rtnl_link_ops = ops;
3195 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3198 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3201 err = dev_validate_mtu(dev, mtu, extack);
3204 return ERR_PTR(err);
3208 if (tb[IFLA_ADDRESS]) {
3209 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3210 nla_len(tb[IFLA_ADDRESS]));
3211 dev->addr_assign_type = NET_ADDR_SET;
3213 if (tb[IFLA_BROADCAST])
3214 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3215 nla_len(tb[IFLA_BROADCAST]));
3216 if (tb[IFLA_TXQLEN])
3217 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3218 if (tb[IFLA_OPERSTATE])
3219 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3220 if (tb[IFLA_LINKMODE])
3221 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3223 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3224 if (tb[IFLA_GSO_MAX_SIZE])
3225 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3226 if (tb[IFLA_GSO_MAX_SEGS])
3227 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3231 EXPORT_SYMBOL(rtnl_create_link);
3233 static int rtnl_group_changelink(const struct sk_buff *skb,
3234 struct net *net, int group,
3235 struct ifinfomsg *ifm,
3236 struct netlink_ext_ack *extack,
3239 struct net_device *dev, *aux;
3242 for_each_netdev_safe(net, dev, aux) {
3243 if (dev->group == group) {
3244 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3253 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3254 struct nlattr **attr, struct netlink_ext_ack *extack)
3256 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3257 unsigned char name_assign_type = NET_NAME_USER;
3258 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3259 const struct rtnl_link_ops *m_ops;
3260 struct net_device *master_dev;
3261 struct net *net = sock_net(skb->sk);
3262 const struct rtnl_link_ops *ops;
3263 struct nlattr *tb[IFLA_MAX + 1];
3264 struct net *dest_net, *link_net;
3265 struct nlattr **slave_data;
3266 char kind[MODULE_NAME_LEN];
3267 struct net_device *dev;
3268 struct ifinfomsg *ifm;
3269 char ifname[IFNAMSIZ];
3270 struct nlattr **data;
3273 #ifdef CONFIG_MODULES
3276 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3277 ifla_policy, extack);
3281 err = rtnl_ensure_unique_netns(tb, extack, false);
3285 if (tb[IFLA_IFNAME])
3286 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3290 ifm = nlmsg_data(nlh);
3291 if (ifm->ifi_index > 0)
3292 dev = __dev_get_by_index(net, ifm->ifi_index);
3293 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3294 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3301 master_dev = netdev_master_upper_dev_get(dev);
3303 m_ops = master_dev->rtnl_link_ops;
3306 err = validate_linkmsg(dev, tb, extack);
3310 if (tb[IFLA_LINKINFO]) {
3311 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3313 ifla_info_policy, NULL);
3317 memset(linkinfo, 0, sizeof(linkinfo));
3319 if (linkinfo[IFLA_INFO_KIND]) {
3320 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3321 ops = rtnl_link_ops_get(kind);
3329 if (ops->maxtype > RTNL_MAX_TYPE)
3332 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3333 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3334 linkinfo[IFLA_INFO_DATA],
3335 ops->policy, extack);
3340 if (ops->validate) {
3341 err = ops->validate(tb, data, extack);
3349 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3352 if (m_ops->slave_maxtype &&
3353 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3354 err = nla_parse_nested_deprecated(slave_attr,
3355 m_ops->slave_maxtype,
3356 linkinfo[IFLA_INFO_SLAVE_DATA],
3357 m_ops->slave_policy,
3361 slave_data = slave_attr;
3368 if (nlh->nlmsg_flags & NLM_F_EXCL)
3370 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3373 if (linkinfo[IFLA_INFO_DATA]) {
3374 if (!ops || ops != dev->rtnl_link_ops ||
3378 err = ops->changelink(dev, tb, data, extack);
3381 status |= DO_SETLINK_NOTIFY;
3384 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3385 if (!m_ops || !m_ops->slave_changelink)
3388 err = m_ops->slave_changelink(master_dev, dev, tb,
3389 slave_data, extack);
3392 status |= DO_SETLINK_NOTIFY;
3395 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3398 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3399 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3400 return rtnl_group_changelink(skb, net,
3401 nla_get_u32(tb[IFLA_GROUP]),
3406 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3410 #ifdef CONFIG_MODULES
3413 request_module("rtnl-link-%s", kind);
3415 ops = rtnl_link_ops_get(kind);
3420 NL_SET_ERR_MSG(extack, "Unknown device type");
3424 if (!ops->alloc && !ops->setup)
3428 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3429 name_assign_type = NET_NAME_ENUM;
3432 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3433 if (IS_ERR(dest_net))
3434 return PTR_ERR(dest_net);
3436 if (tb[IFLA_LINK_NETNSID]) {
3437 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3439 link_net = get_net_ns_by_id(dest_net, id);
3441 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3446 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3452 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3453 name_assign_type, ops, tb, extack);
3459 dev->ifindex = ifm->ifi_index;
3462 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3464 err = register_netdevice(dev);
3470 err = rtnl_configure_link(dev, ifm);
3472 goto out_unregister;
3474 err = dev_change_net_namespace(dev, dest_net, ifname);
3476 goto out_unregister;
3478 if (tb[IFLA_MASTER]) {
3479 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3481 goto out_unregister;
3490 LIST_HEAD(list_kill);
3492 ops->dellink(dev, &list_kill);
3493 unregister_netdevice_many(&list_kill);
3495 unregister_netdevice(dev);
3500 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3501 struct netlink_ext_ack *extack)
3503 struct nlattr **attr;
3506 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3510 ret = __rtnl_newlink(skb, nlh, attr, extack);
3515 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3516 const struct nlmsghdr *nlh,
3518 struct netlink_ext_ack *extack)
3520 struct ifinfomsg *ifm;
3523 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3524 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3528 if (!netlink_strict_get_check(skb))
3529 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3530 ifla_policy, extack);
3532 ifm = nlmsg_data(nlh);
3533 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3535 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3539 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3540 ifla_policy, extack);
3544 for (i = 0; i <= IFLA_MAX; i++) {
3550 case IFLA_ALT_IFNAME:
3552 case IFLA_TARGET_NETNSID:
3555 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3563 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3564 struct netlink_ext_ack *extack)
3566 struct net *net = sock_net(skb->sk);
3567 struct net *tgt_net = net;
3568 struct ifinfomsg *ifm;
3569 struct nlattr *tb[IFLA_MAX+1];
3570 struct net_device *dev = NULL;
3571 struct sk_buff *nskb;
3574 u32 ext_filter_mask = 0;
3576 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3580 err = rtnl_ensure_unique_netns(tb, extack, true);
3584 if (tb[IFLA_TARGET_NETNSID]) {
3585 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3586 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3587 if (IS_ERR(tgt_net))
3588 return PTR_ERR(tgt_net);
3591 if (tb[IFLA_EXT_MASK])
3592 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3595 ifm = nlmsg_data(nlh);
3596 if (ifm->ifi_index > 0)
3597 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3598 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3599 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3600 tb[IFLA_ALT_IFNAME], NULL);
3609 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3613 err = rtnl_fill_ifinfo(nskb, dev, net,
3614 RTM_NEWLINK, NETLINK_CB(skb).portid,
3615 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3616 0, NULL, 0, netnsid, GFP_KERNEL);
3618 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3619 WARN_ON(err == -EMSGSIZE);
3622 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3630 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3631 bool *changed, struct netlink_ext_ack *extack)
3637 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3641 if (cmd == RTM_NEWLINKPROP) {
3642 size = rtnl_prop_list_size(dev);
3643 size += nla_total_size(ALTIFNAMSIZ);
3644 if (size >= U16_MAX) {
3645 NL_SET_ERR_MSG(extack,
3646 "effective property list too long");
3651 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3655 if (cmd == RTM_NEWLINKPROP) {
3656 err = netdev_name_node_alt_create(dev, alt_ifname);
3659 } else if (cmd == RTM_DELLINKPROP) {
3660 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3672 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3673 struct netlink_ext_ack *extack)
3675 struct net *net = sock_net(skb->sk);
3676 struct nlattr *tb[IFLA_MAX + 1];
3677 struct net_device *dev;
3678 struct ifinfomsg *ifm;
3679 bool changed = false;
3680 struct nlattr *attr;
3683 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3687 err = rtnl_ensure_unique_netns(tb, extack, true);
3691 ifm = nlmsg_data(nlh);
3692 if (ifm->ifi_index > 0)
3693 dev = __dev_get_by_index(net, ifm->ifi_index);
3694 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3695 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3696 tb[IFLA_ALT_IFNAME], NULL);
3703 if (!tb[IFLA_PROP_LIST])
3706 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3707 switch (nla_type(attr)) {
3708 case IFLA_ALT_IFNAME:
3709 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3717 netdev_state_change(dev);
3721 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3722 struct netlink_ext_ack *extack)
3724 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3727 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3728 struct netlink_ext_ack *extack)
3730 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3733 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3735 struct net *net = sock_net(skb->sk);
3736 size_t min_ifinfo_dump_size = 0;
3737 struct nlattr *tb[IFLA_MAX+1];
3738 u32 ext_filter_mask = 0;
3739 struct net_device *dev;
3742 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3743 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3744 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3746 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3747 if (tb[IFLA_EXT_MASK])
3748 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3751 if (!ext_filter_mask)
3752 return NLMSG_GOODSIZE;
3754 * traverse the list of net devices and compute the minimum
3755 * buffer size based upon the filter mask.
3758 for_each_netdev_rcu(net, dev) {
3759 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3760 if_nlmsg_size(dev, ext_filter_mask));
3764 return nlmsg_total_size(min_ifinfo_dump_size);
3767 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3770 int s_idx = cb->family;
3771 int type = cb->nlh->nlmsg_type - RTM_BASE;
3777 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3778 struct rtnl_link __rcu **tab;
3779 struct rtnl_link *link;
3780 rtnl_dumpit_func dumpit;
3782 if (idx < s_idx || idx == PF_PACKET)
3785 if (type < 0 || type >= RTM_NR_MSGTYPES)
3788 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3792 link = rcu_dereference_rtnl(tab[type]);
3796 dumpit = link->dumpit;
3801 memset(&cb->args[0], 0, sizeof(cb->args));
3805 ret = dumpit(skb, cb);
3811 return skb->len ? : ret;
3814 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3815 unsigned int change,
3816 u32 event, gfp_t flags, int *new_nsid,
3819 struct net *net = dev_net(dev);
3820 struct sk_buff *skb;
3822 size_t if_info_size;
3824 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3828 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3829 type, 0, 0, change, 0, 0, event,
3830 new_nsid, new_ifindex, -1, flags);
3832 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3833 WARN_ON(err == -EMSGSIZE);
3840 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3844 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3846 struct net *net = dev_net(dev);
3848 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3851 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3852 unsigned int change, u32 event,
3853 gfp_t flags, int *new_nsid, int new_ifindex)
3855 struct sk_buff *skb;
3857 if (dev->reg_state != NETREG_REGISTERED)
3860 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3863 rtmsg_ifinfo_send(skb, dev, flags);
3866 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3869 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3873 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3874 gfp_t flags, int *new_nsid, int new_ifindex)
3876 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3877 new_nsid, new_ifindex);
3880 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3881 struct net_device *dev,
3882 u8 *addr, u16 vid, u32 pid, u32 seq,
3883 int type, unsigned int flags,
3884 int nlflags, u16 ndm_state)
3886 struct nlmsghdr *nlh;
3889 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3893 ndm = nlmsg_data(nlh);
3894 ndm->ndm_family = AF_BRIDGE;
3897 ndm->ndm_flags = flags;
3899 ndm->ndm_ifindex = dev->ifindex;
3900 ndm->ndm_state = ndm_state;
3902 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3903 goto nla_put_failure;
3905 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3906 goto nla_put_failure;
3908 nlmsg_end(skb, nlh);
3912 nlmsg_cancel(skb, nlh);
3916 static inline size_t rtnl_fdb_nlmsg_size(void)
3918 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3919 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3920 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3924 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3927 struct net *net = dev_net(dev);
3928 struct sk_buff *skb;
3931 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3935 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3936 0, 0, type, NTF_SELF, 0, ndm_state);
3942 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3945 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3949 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3951 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3952 struct nlattr *tb[],
3953 struct net_device *dev,
3954 const unsigned char *addr, u16 vid,
3959 /* If aging addresses are supported device will need to
3960 * implement its own handler for this.
3962 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3963 netdev_info(dev, "default FDB implementation only supports local addresses\n");
3968 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
3972 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3973 err = dev_uc_add_excl(dev, addr);
3974 else if (is_multicast_ether_addr(addr))
3975 err = dev_mc_add_excl(dev, addr);
3977 /* Only return duplicate errors if NLM_F_EXCL is set */
3978 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3983 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3985 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3986 struct netlink_ext_ack *extack)
3991 if (nla_len(vlan_attr) != sizeof(u16)) {
3992 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3996 vid = nla_get_u16(vlan_attr);
3998 if (!vid || vid >= VLAN_VID_MASK) {
3999 NL_SET_ERR_MSG(extack, "invalid vlan id");
4007 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4008 struct netlink_ext_ack *extack)
4010 struct net *net = sock_net(skb->sk);
4012 struct nlattr *tb[NDA_MAX+1];
4013 struct net_device *dev;
4018 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4023 ndm = nlmsg_data(nlh);
4024 if (ndm->ndm_ifindex == 0) {
4025 NL_SET_ERR_MSG(extack, "invalid ifindex");
4029 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4031 NL_SET_ERR_MSG(extack, "unknown ifindex");
4035 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4036 NL_SET_ERR_MSG(extack, "invalid address");
4040 if (dev->type != ARPHRD_ETHER) {
4041 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4045 addr = nla_data(tb[NDA_LLADDR]);
4047 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4053 /* Support fdb on master device the net/bridge default case */
4054 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4055 netif_is_bridge_port(dev)) {
4056 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4057 const struct net_device_ops *ops = br_dev->netdev_ops;
4059 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4060 nlh->nlmsg_flags, extack);
4064 ndm->ndm_flags &= ~NTF_MASTER;
4067 /* Embedded bridge, macvlan, and any other device support */
4068 if ((ndm->ndm_flags & NTF_SELF)) {
4069 if (dev->netdev_ops->ndo_fdb_add)
4070 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4075 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4079 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4081 ndm->ndm_flags &= ~NTF_SELF;
4089 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4091 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4092 struct nlattr *tb[],
4093 struct net_device *dev,
4094 const unsigned char *addr, u16 vid)
4098 /* If aging addresses are supported device will need to
4099 * implement its own handler for this.
4101 if (!(ndm->ndm_state & NUD_PERMANENT)) {
4102 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4106 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4107 err = dev_uc_del(dev, addr);
4108 else if (is_multicast_ether_addr(addr))
4109 err = dev_mc_del(dev, addr);
4113 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4115 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4116 struct netlink_ext_ack *extack)
4118 struct net *net = sock_net(skb->sk);
4120 struct nlattr *tb[NDA_MAX+1];
4121 struct net_device *dev;
4126 if (!netlink_capable(skb, CAP_NET_ADMIN))
4129 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4134 ndm = nlmsg_data(nlh);
4135 if (ndm->ndm_ifindex == 0) {
4136 NL_SET_ERR_MSG(extack, "invalid ifindex");
4140 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4142 NL_SET_ERR_MSG(extack, "unknown ifindex");
4146 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4147 NL_SET_ERR_MSG(extack, "invalid address");
4151 if (dev->type != ARPHRD_ETHER) {
4152 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4156 addr = nla_data(tb[NDA_LLADDR]);
4158 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4164 /* Support fdb on master device the net/bridge default case */
4165 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4166 netif_is_bridge_port(dev)) {
4167 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4168 const struct net_device_ops *ops = br_dev->netdev_ops;
4170 if (ops->ndo_fdb_del)
4171 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4176 ndm->ndm_flags &= ~NTF_MASTER;
4179 /* Embedded bridge, macvlan, and any other device support */
4180 if (ndm->ndm_flags & NTF_SELF) {
4181 if (dev->netdev_ops->ndo_fdb_del)
4182 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4185 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4188 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4190 ndm->ndm_flags &= ~NTF_SELF;
4197 static int nlmsg_populate_fdb(struct sk_buff *skb,
4198 struct netlink_callback *cb,
4199 struct net_device *dev,
4201 struct netdev_hw_addr_list *list)
4203 struct netdev_hw_addr *ha;
4207 portid = NETLINK_CB(cb->skb).portid;
4208 seq = cb->nlh->nlmsg_seq;
4210 list_for_each_entry(ha, &list->list, list) {
4211 if (*idx < cb->args[2])
4214 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4216 RTM_NEWNEIGH, NTF_SELF,
4217 NLM_F_MULTI, NUD_PERMANENT);
4227 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4228 * @skb: socket buffer to store message in
4229 * @cb: netlink callback
4231 * @filter_dev: ignored
4232 * @idx: the number of FDB table entries dumped is added to *@idx
4234 * Default netdevice operation to dump the existing unicast address list.
4235 * Returns number of addresses from list put in skb.
4237 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4238 struct netlink_callback *cb,
4239 struct net_device *dev,
4240 struct net_device *filter_dev,
4245 if (dev->type != ARPHRD_ETHER)
4248 netif_addr_lock_bh(dev);
4249 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4252 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4254 netif_addr_unlock_bh(dev);
4257 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4259 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4260 int *br_idx, int *brport_idx,
4261 struct netlink_ext_ack *extack)
4263 struct nlattr *tb[NDA_MAX + 1];
4267 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4268 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4272 ndm = nlmsg_data(nlh);
4273 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4274 ndm->ndm_flags || ndm->ndm_type) {
4275 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4279 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4280 NDA_MAX, NULL, extack);
4284 *brport_idx = ndm->ndm_ifindex;
4285 for (i = 0; i <= NDA_MAX; ++i) {
4291 if (nla_len(tb[i]) != sizeof(u32)) {
4292 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4295 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4298 if (nla_len(tb[i]) != sizeof(u32)) {
4299 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4302 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4305 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4313 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4314 int *br_idx, int *brport_idx,
4315 struct netlink_ext_ack *extack)
4317 struct nlattr *tb[IFLA_MAX+1];
4320 /* A hack to preserve kernel<->userspace interface.
4321 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4322 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4323 * So, check for ndmsg with an optional u32 attribute (not used here).
4324 * Fortunately these sizes don't conflict with the size of ifinfomsg
4325 * with an optional attribute.
4327 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4328 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4329 nla_attr_size(sizeof(u32)))) {
4330 struct ifinfomsg *ifm;
4332 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4333 tb, IFLA_MAX, ifla_policy,
4337 } else if (err == 0) {
4338 if (tb[IFLA_MASTER])
4339 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4342 ifm = nlmsg_data(nlh);
4343 *brport_idx = ifm->ifi_index;
4348 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4350 struct net_device *dev;
4351 struct net_device *br_dev = NULL;
4352 const struct net_device_ops *ops = NULL;
4353 const struct net_device_ops *cops = NULL;
4354 struct net *net = sock_net(skb->sk);
4355 struct hlist_head *head;
4363 if (cb->strict_check)
4364 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4367 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4373 br_dev = __dev_get_by_index(net, br_idx);
4377 ops = br_dev->netdev_ops;
4381 s_idx = cb->args[1];
4383 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4385 head = &net->dev_index_head[h];
4386 hlist_for_each_entry(dev, head, index_hlist) {
4388 if (brport_idx && (dev->ifindex != brport_idx))
4391 if (!br_idx) { /* user did not specify a specific bridge */
4392 if (netif_is_bridge_port(dev)) {
4393 br_dev = netdev_master_upper_dev_get(dev);
4394 cops = br_dev->netdev_ops;
4397 if (dev != br_dev &&
4398 !netif_is_bridge_port(dev))
4401 if (br_dev != netdev_master_upper_dev_get(dev) &&
4402 !(dev->priv_flags & IFF_EBRIDGE))
4410 if (netif_is_bridge_port(dev)) {
4411 if (cops && cops->ndo_fdb_dump) {
4412 err = cops->ndo_fdb_dump(skb, cb,
4415 if (err == -EMSGSIZE)
4420 if (dev->netdev_ops->ndo_fdb_dump)
4421 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4425 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4427 if (err == -EMSGSIZE)
4432 /* reset fdb offset to 0 for rest of the interfaces */
4448 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4449 struct nlattr **tb, u8 *ndm_flags,
4450 int *br_idx, int *brport_idx, u8 **addr,
4451 u16 *vid, struct netlink_ext_ack *extack)
4456 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4457 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4461 ndm = nlmsg_data(nlh);
4462 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4464 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4468 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4469 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4473 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4474 NDA_MAX, nda_policy, extack);
4478 *ndm_flags = ndm->ndm_flags;
4479 *brport_idx = ndm->ndm_ifindex;
4480 for (i = 0; i <= NDA_MAX; ++i) {
4486 *br_idx = nla_get_u32(tb[i]);
4489 if (nla_len(tb[i]) != ETH_ALEN) {
4490 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4493 *addr = nla_data(tb[i]);
4496 err = fdb_vid_parse(tb[i], vid, extack);
4503 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4511 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4512 struct netlink_ext_ack *extack)
4514 struct net_device *dev = NULL, *br_dev = NULL;
4515 const struct net_device_ops *ops = NULL;
4516 struct net *net = sock_net(in_skb->sk);
4517 struct nlattr *tb[NDA_MAX + 1];
4518 struct sk_buff *skb;
4526 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4527 &brport_idx, &addr, &vid, extack);
4532 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4537 dev = __dev_get_by_index(net, brport_idx);
4539 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4546 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4550 br_dev = __dev_get_by_index(net, br_idx);
4552 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4555 ops = br_dev->netdev_ops;
4559 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4560 if (!netif_is_bridge_port(dev)) {
4561 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4564 br_dev = netdev_master_upper_dev_get(dev);
4566 NL_SET_ERR_MSG(extack, "Master of device not found");
4569 ops = br_dev->netdev_ops;
4571 if (!(ndm_flags & NTF_SELF)) {
4572 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4575 ops = dev->netdev_ops;
4579 if (!br_dev && !dev) {
4580 NL_SET_ERR_MSG(extack, "No device specified");
4584 if (!ops || !ops->ndo_fdb_get) {
4585 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4589 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4595 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4596 NETLINK_CB(in_skb).portid,
4597 nlh->nlmsg_seq, extack);
4601 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4607 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4608 unsigned int attrnum, unsigned int flag)
4611 return nla_put_u8(skb, attrnum, !!(flags & flag));
4615 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4616 struct net_device *dev, u16 mode,
4617 u32 flags, u32 mask, int nlflags,
4619 int (*vlan_fill)(struct sk_buff *skb,
4620 struct net_device *dev,
4623 struct nlmsghdr *nlh;
4624 struct ifinfomsg *ifm;
4625 struct nlattr *br_afspec;
4626 struct nlattr *protinfo;
4627 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4628 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4631 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4635 ifm = nlmsg_data(nlh);
4636 ifm->ifi_family = AF_BRIDGE;
4638 ifm->ifi_type = dev->type;
4639 ifm->ifi_index = dev->ifindex;
4640 ifm->ifi_flags = dev_get_flags(dev);
4641 ifm->ifi_change = 0;
4644 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4645 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4646 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4648 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4650 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4651 (dev->ifindex != dev_get_iflink(dev) &&
4652 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4653 goto nla_put_failure;
4655 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4657 goto nla_put_failure;
4659 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4660 nla_nest_cancel(skb, br_afspec);
4661 goto nla_put_failure;
4664 if (mode != BRIDGE_MODE_UNDEF) {
4665 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4666 nla_nest_cancel(skb, br_afspec);
4667 goto nla_put_failure;
4671 err = vlan_fill(skb, dev, filter_mask);
4673 nla_nest_cancel(skb, br_afspec);
4674 goto nla_put_failure;
4677 nla_nest_end(skb, br_afspec);
4679 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4681 goto nla_put_failure;
4683 if (brport_nla_put_flag(skb, flags, mask,
4684 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4685 brport_nla_put_flag(skb, flags, mask,
4686 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4687 brport_nla_put_flag(skb, flags, mask,
4688 IFLA_BRPORT_FAST_LEAVE,
4689 BR_MULTICAST_FAST_LEAVE) ||
4690 brport_nla_put_flag(skb, flags, mask,
4691 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4692 brport_nla_put_flag(skb, flags, mask,
4693 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4694 brport_nla_put_flag(skb, flags, mask,
4695 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4696 brport_nla_put_flag(skb, flags, mask,
4697 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4698 brport_nla_put_flag(skb, flags, mask,
4699 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4700 brport_nla_put_flag(skb, flags, mask,
4701 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4702 brport_nla_put_flag(skb, flags, mask,
4703 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4704 nla_nest_cancel(skb, protinfo);
4705 goto nla_put_failure;
4708 nla_nest_end(skb, protinfo);
4710 nlmsg_end(skb, nlh);
4713 nlmsg_cancel(skb, nlh);
4714 return err ? err : -EMSGSIZE;
4716 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4718 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4719 bool strict_check, u32 *filter_mask,
4720 struct netlink_ext_ack *extack)
4722 struct nlattr *tb[IFLA_MAX+1];
4726 struct ifinfomsg *ifm;
4728 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4729 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4733 ifm = nlmsg_data(nlh);
4734 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4735 ifm->ifi_change || ifm->ifi_index) {
4736 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4740 err = nlmsg_parse_deprecated_strict(nlh,
4741 sizeof(struct ifinfomsg),
4742 tb, IFLA_MAX, ifla_policy,
4745 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4746 tb, IFLA_MAX, ifla_policy,
4752 /* new attributes should only be added with strict checking */
4753 for (i = 0; i <= IFLA_MAX; ++i) {
4759 *filter_mask = nla_get_u32(tb[i]);
4763 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4772 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4774 const struct nlmsghdr *nlh = cb->nlh;
4775 struct net *net = sock_net(skb->sk);
4776 struct net_device *dev;
4778 u32 portid = NETLINK_CB(cb->skb).portid;
4779 u32 seq = nlh->nlmsg_seq;
4780 u32 filter_mask = 0;
4783 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4785 if (err < 0 && cb->strict_check)
4789 for_each_netdev_rcu(net, dev) {
4790 const struct net_device_ops *ops = dev->netdev_ops;
4791 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4793 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4794 if (idx >= cb->args[0]) {
4795 err = br_dev->netdev_ops->ndo_bridge_getlink(
4796 skb, portid, seq, dev,
4797 filter_mask, NLM_F_MULTI);
4798 if (err < 0 && err != -EOPNOTSUPP) {
4799 if (likely(skb->len))
4808 if (ops->ndo_bridge_getlink) {
4809 if (idx >= cb->args[0]) {
4810 err = ops->ndo_bridge_getlink(skb, portid,
4814 if (err < 0 && err != -EOPNOTSUPP) {
4815 if (likely(skb->len))
4832 static inline size_t bridge_nlmsg_size(void)
4834 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4835 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4836 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4837 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4838 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4839 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4840 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4841 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4842 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4843 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4844 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4847 static int rtnl_bridge_notify(struct net_device *dev)
4849 struct net *net = dev_net(dev);
4850 struct sk_buff *skb;
4851 int err = -EOPNOTSUPP;
4853 if (!dev->netdev_ops->ndo_bridge_getlink)
4856 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4862 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4866 /* Notification info is only filled for bridge ports, not the bridge
4867 * device itself. Therefore, a zero notification length is valid and
4868 * should not result in an error.
4873 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4876 WARN_ON(err == -EMSGSIZE);
4879 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4883 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4884 struct netlink_ext_ack *extack)
4886 struct net *net = sock_net(skb->sk);
4887 struct ifinfomsg *ifm;
4888 struct net_device *dev;
4889 struct nlattr *br_spec, *attr = NULL;
4890 int rem, err = -EOPNOTSUPP;
4892 bool have_flags = false;
4894 if (nlmsg_len(nlh) < sizeof(*ifm))
4897 ifm = nlmsg_data(nlh);
4898 if (ifm->ifi_family != AF_BRIDGE)
4899 return -EPFNOSUPPORT;
4901 dev = __dev_get_by_index(net, ifm->ifi_index);
4903 NL_SET_ERR_MSG(extack, "unknown ifindex");
4907 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4909 nla_for_each_nested(attr, br_spec, rem) {
4910 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4911 if (nla_len(attr) < sizeof(flags))
4915 flags = nla_get_u16(attr);
4921 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4922 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4924 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4929 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4934 flags &= ~BRIDGE_FLAGS_MASTER;
4937 if ((flags & BRIDGE_FLAGS_SELF)) {
4938 if (!dev->netdev_ops->ndo_bridge_setlink)
4941 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4945 flags &= ~BRIDGE_FLAGS_SELF;
4947 /* Generate event to notify upper layer of bridge
4950 err = rtnl_bridge_notify(dev);
4955 memcpy(nla_data(attr), &flags, sizeof(flags));
4960 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4961 struct netlink_ext_ack *extack)
4963 struct net *net = sock_net(skb->sk);
4964 struct ifinfomsg *ifm;
4965 struct net_device *dev;
4966 struct nlattr *br_spec, *attr = NULL;
4967 int rem, err = -EOPNOTSUPP;
4969 bool have_flags = false;
4971 if (nlmsg_len(nlh) < sizeof(*ifm))
4974 ifm = nlmsg_data(nlh);
4975 if (ifm->ifi_family != AF_BRIDGE)
4976 return -EPFNOSUPPORT;
4978 dev = __dev_get_by_index(net, ifm->ifi_index);
4980 NL_SET_ERR_MSG(extack, "unknown ifindex");
4984 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4986 nla_for_each_nested(attr, br_spec, rem) {
4987 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4988 if (nla_len(attr) < sizeof(flags))
4992 flags = nla_get_u16(attr);
4998 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4999 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5001 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5006 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5010 flags &= ~BRIDGE_FLAGS_MASTER;
5013 if ((flags & BRIDGE_FLAGS_SELF)) {
5014 if (!dev->netdev_ops->ndo_bridge_dellink)
5017 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5021 flags &= ~BRIDGE_FLAGS_SELF;
5023 /* Generate event to notify upper layer of bridge
5026 err = rtnl_bridge_notify(dev);
5031 memcpy(nla_data(attr), &flags, sizeof(flags));
5036 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5038 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5039 (!idxattr || idxattr == attrid);
5042 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5043 static int rtnl_get_offload_stats_attr_size(int attr_id)
5046 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5047 return sizeof(struct rtnl_link_stats64);
5053 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5056 struct nlattr *attr = NULL;
5061 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5062 dev->netdev_ops->ndo_get_offload_stats))
5065 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5066 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5067 if (attr_id < *prividx)
5070 size = rtnl_get_offload_stats_attr_size(attr_id);
5074 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5077 attr = nla_reserve_64bit(skb, attr_id, size,
5078 IFLA_OFFLOAD_XSTATS_UNSPEC);
5080 goto nla_put_failure;
5082 attr_data = nla_data(attr);
5083 memset(attr_data, 0, size);
5084 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5087 goto get_offload_stats_failure;
5098 get_offload_stats_failure:
5103 static int rtnl_get_offload_stats_size(const struct net_device *dev)
5109 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5110 dev->netdev_ops->ndo_get_offload_stats))
5113 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5114 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5115 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5117 size = rtnl_get_offload_stats_attr_size(attr_id);
5118 nla_size += nla_total_size_64bit(size);
5122 nla_size += nla_total_size(0);
5127 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5128 int type, u32 pid, u32 seq, u32 change,
5129 unsigned int flags, unsigned int filter_mask,
5130 int *idxattr, int *prividx)
5132 struct if_stats_msg *ifsm;
5133 struct nlmsghdr *nlh;
5134 struct nlattr *attr;
5135 int s_prividx = *prividx;
5140 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5144 ifsm = nlmsg_data(nlh);
5145 ifsm->family = PF_UNSPEC;
5148 ifsm->ifindex = dev->ifindex;
5149 ifsm->filter_mask = filter_mask;
5151 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5152 struct rtnl_link_stats64 *sp;
5154 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5155 sizeof(struct rtnl_link_stats64),
5158 goto nla_put_failure;
5160 sp = nla_data(attr);
5161 dev_get_stats(dev, sp);
5164 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5165 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5167 if (ops && ops->fill_linkxstats) {
5168 *idxattr = IFLA_STATS_LINK_XSTATS;
5169 attr = nla_nest_start_noflag(skb,
5170 IFLA_STATS_LINK_XSTATS);
5172 goto nla_put_failure;
5174 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5175 nla_nest_end(skb, attr);
5177 goto nla_put_failure;
5182 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5184 const struct rtnl_link_ops *ops = NULL;
5185 const struct net_device *master;
5187 master = netdev_master_upper_dev_get(dev);
5189 ops = master->rtnl_link_ops;
5190 if (ops && ops->fill_linkxstats) {
5191 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5192 attr = nla_nest_start_noflag(skb,
5193 IFLA_STATS_LINK_XSTATS_SLAVE);
5195 goto nla_put_failure;
5197 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5198 nla_nest_end(skb, attr);
5200 goto nla_put_failure;
5205 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5207 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5208 attr = nla_nest_start_noflag(skb,
5209 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5211 goto nla_put_failure;
5213 err = rtnl_get_offload_stats(skb, dev, prividx);
5214 if (err == -ENODATA)
5215 nla_nest_cancel(skb, attr);
5217 nla_nest_end(skb, attr);
5219 if (err && err != -ENODATA)
5220 goto nla_put_failure;
5224 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5225 struct rtnl_af_ops *af_ops;
5227 *idxattr = IFLA_STATS_AF_SPEC;
5228 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5230 goto nla_put_failure;
5233 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5234 if (af_ops->fill_stats_af) {
5238 af = nla_nest_start_noflag(skb,
5242 goto nla_put_failure;
5244 err = af_ops->fill_stats_af(skb, dev);
5246 if (err == -ENODATA) {
5247 nla_nest_cancel(skb, af);
5248 } else if (err < 0) {
5250 goto nla_put_failure;
5253 nla_nest_end(skb, af);
5258 nla_nest_end(skb, attr);
5263 nlmsg_end(skb, nlh);
5268 /* not a multi message or no progress mean a real error */
5269 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5270 nlmsg_cancel(skb, nlh);
5272 nlmsg_end(skb, nlh);
5277 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5280 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5282 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5283 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5285 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5286 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5287 int attr = IFLA_STATS_LINK_XSTATS;
5289 if (ops && ops->get_linkxstats_size) {
5290 size += nla_total_size(ops->get_linkxstats_size(dev,
5292 /* for IFLA_STATS_LINK_XSTATS */
5293 size += nla_total_size(0);
5297 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5298 struct net_device *_dev = (struct net_device *)dev;
5299 const struct rtnl_link_ops *ops = NULL;
5300 const struct net_device *master;
5302 /* netdev_master_upper_dev_get can't take const */
5303 master = netdev_master_upper_dev_get(_dev);
5305 ops = master->rtnl_link_ops;
5306 if (ops && ops->get_linkxstats_size) {
5307 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5309 size += nla_total_size(ops->get_linkxstats_size(dev,
5311 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5312 size += nla_total_size(0);
5316 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5317 size += rtnl_get_offload_stats_size(dev);
5319 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5320 struct rtnl_af_ops *af_ops;
5322 /* for IFLA_STATS_AF_SPEC */
5323 size += nla_total_size(0);
5326 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5327 if (af_ops->get_stats_af_size) {
5328 size += nla_total_size(
5329 af_ops->get_stats_af_size(dev));
5332 size += nla_total_size(0);
5341 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5342 bool is_dump, struct netlink_ext_ack *extack)
5344 struct if_stats_msg *ifsm;
5346 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5347 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5354 ifsm = nlmsg_data(nlh);
5356 /* only requests using strict checks can pass data to influence
5357 * the dump. The legacy exception is filter_mask.
5359 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5360 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5363 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5364 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5367 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5368 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5375 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5376 struct netlink_ext_ack *extack)
5378 struct net *net = sock_net(skb->sk);
5379 struct net_device *dev = NULL;
5380 int idxattr = 0, prividx = 0;
5381 struct if_stats_msg *ifsm;
5382 struct sk_buff *nskb;
5386 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5391 ifsm = nlmsg_data(nlh);
5392 if (ifsm->ifindex > 0)
5393 dev = __dev_get_by_index(net, ifsm->ifindex);
5400 filter_mask = ifsm->filter_mask;
5404 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5408 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5409 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5410 0, filter_mask, &idxattr, &prividx);
5412 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5413 WARN_ON(err == -EMSGSIZE);
5416 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5422 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5424 struct netlink_ext_ack *extack = cb->extack;
5425 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5426 struct net *net = sock_net(skb->sk);
5427 unsigned int flags = NLM_F_MULTI;
5428 struct if_stats_msg *ifsm;
5429 struct hlist_head *head;
5430 struct net_device *dev;
5431 u32 filter_mask = 0;
5435 s_idx = cb->args[1];
5436 s_idxattr = cb->args[2];
5437 s_prividx = cb->args[3];
5439 cb->seq = net->dev_base_seq;
5441 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5445 ifsm = nlmsg_data(cb->nlh);
5446 filter_mask = ifsm->filter_mask;
5448 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5452 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5454 head = &net->dev_index_head[h];
5455 hlist_for_each_entry(dev, head, index_hlist) {
5458 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5459 NETLINK_CB(cb->skb).portid,
5460 cb->nlh->nlmsg_seq, 0,
5462 &s_idxattr, &s_prividx);
5463 /* If we ran out of room on the first message,
5466 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5472 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5478 cb->args[3] = s_prividx;
5479 cb->args[2] = s_idxattr;
5486 /* Process one rtnetlink message. */
5488 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5489 struct netlink_ext_ack *extack)
5491 struct net *net = sock_net(skb->sk);
5492 struct rtnl_link *link;
5493 struct module *owner;
5494 int err = -EOPNOTSUPP;
5495 rtnl_doit_func doit;
5501 type = nlh->nlmsg_type;
5507 /* All the messages must have at least 1 byte length */
5508 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5511 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5514 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5518 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5520 rtnl_dumpit_func dumpit;
5521 u32 min_dump_alloc = 0;
5523 link = rtnl_get_link(family, type);
5524 if (!link || !link->dumpit) {
5526 link = rtnl_get_link(family, type);
5527 if (!link || !link->dumpit)
5530 owner = link->owner;
5531 dumpit = link->dumpit;
5533 if (type == RTM_GETLINK - RTM_BASE)
5534 min_dump_alloc = rtnl_calcit(skb, nlh);
5537 /* need to do this before rcu_read_unlock() */
5538 if (!try_module_get(owner))
5539 err = -EPROTONOSUPPORT;
5545 struct netlink_dump_control c = {
5547 .min_dump_alloc = min_dump_alloc,
5550 err = netlink_dump_start(rtnl, skb, nlh, &c);
5551 /* netlink_dump_start() will keep a reference on
5552 * module if dump is still in progress.
5559 link = rtnl_get_link(family, type);
5560 if (!link || !link->doit) {
5562 link = rtnl_get_link(PF_UNSPEC, type);
5563 if (!link || !link->doit)
5567 owner = link->owner;
5568 if (!try_module_get(owner)) {
5569 err = -EPROTONOSUPPORT;
5573 flags = link->flags;
5574 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5578 err = doit(skb, nlh, extack);
5585 link = rtnl_get_link(family, type);
5586 if (link && link->doit)
5587 err = link->doit(skb, nlh, extack);
5603 static void rtnetlink_rcv(struct sk_buff *skb)
5605 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5608 static int rtnetlink_bind(struct net *net, int group)
5611 case RTNLGRP_IPV4_MROUTE_R:
5612 case RTNLGRP_IPV6_MROUTE_R:
5613 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5620 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5622 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5626 case NETDEV_CHANGEMTU:
5627 case NETDEV_CHANGEADDR:
5628 case NETDEV_CHANGENAME:
5629 case NETDEV_FEAT_CHANGE:
5630 case NETDEV_BONDING_FAILOVER:
5631 case NETDEV_POST_TYPE_CHANGE:
5632 case NETDEV_NOTIFY_PEERS:
5633 case NETDEV_CHANGEUPPER:
5634 case NETDEV_RESEND_IGMP:
5635 case NETDEV_CHANGEINFODATA:
5636 case NETDEV_CHANGELOWERSTATE:
5637 case NETDEV_CHANGE_TX_QUEUE_LEN:
5638 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5639 GFP_KERNEL, NULL, 0);
5647 static struct notifier_block rtnetlink_dev_notifier = {
5648 .notifier_call = rtnetlink_event,
5652 static int __net_init rtnetlink_net_init(struct net *net)
5655 struct netlink_kernel_cfg cfg = {
5656 .groups = RTNLGRP_MAX,
5657 .input = rtnetlink_rcv,
5658 .cb_mutex = &rtnl_mutex,
5659 .flags = NL_CFG_F_NONROOT_RECV,
5660 .bind = rtnetlink_bind,
5663 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5670 static void __net_exit rtnetlink_net_exit(struct net *net)
5672 netlink_kernel_release(net->rtnl);
5676 static struct pernet_operations rtnetlink_net_ops = {
5677 .init = rtnetlink_net_init,
5678 .exit = rtnetlink_net_exit,
5681 void __init rtnetlink_init(void)
5683 if (register_pernet_subsys(&rtnetlink_net_ops))
5684 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5686 register_netdevice_notifier(&rtnetlink_dev_notifier);
5688 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5689 rtnl_dump_ifinfo, 0);
5690 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5691 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5692 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5694 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5695 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5696 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5698 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5699 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5701 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5702 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5703 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5705 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5706 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5707 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5709 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,