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 arithmetics 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 36
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 *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 **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]);
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_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 modifiy 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 modifiy 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 **tab, *link;
292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293 msgindex = rtm_msgindex(msgtype);
296 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 link = tab[msgindex];
303 rcu_assign_pointer(tab[msgindex], NULL);
306 kfree_rcu(link, rcu);
310 EXPORT_SYMBOL_GPL(rtnl_unregister);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol)
321 struct rtnl_link **tab, *link;
324 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
327 tab = rtnl_msg_handlers[protocol];
332 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
333 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334 link = tab[msgindex];
338 rcu_assign_pointer(tab[msgindex], NULL);
339 kfree_rcu(link, rcu);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
349 static LIST_HEAD(link_ops);
351 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
353 const struct rtnl_link_ops *ops;
355 list_for_each_entry(ops, &link_ops, list) {
356 if (!strcmp(ops->kind, kind))
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops *ops)
374 if (rtnl_link_ops_get(ops->kind))
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops->setup && !ops->dellink)
383 ops->dellink = unregister_netdevice_queue;
385 list_add_tail(&ops->list, &link_ops);
388 EXPORT_SYMBOL_GPL(__rtnl_link_register);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops *ops)
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
406 err = __rtnl_link_register(ops);
410 EXPORT_SYMBOL_GPL(rtnl_link_register);
412 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
414 struct net_device *dev;
415 LIST_HEAD(list_kill);
417 for_each_netdev(net, dev) {
418 if (dev->rtnl_link_ops == ops)
419 ops->dellink(dev, &list_kill);
421 unregister_netdevice_many(&list_kill);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
437 __rtnl_kill_links(net, ops);
439 list_del(&ops->list);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
450 DEFINE_WAIT_FUNC(wait, woken_wake_function);
452 add_wait_queue(&netdev_unregistering_wq, &wait);
454 unregistering = false;
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
460 if (net->dev_unreg_count > 0) {
461 unregistering = true;
469 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
471 remove_wait_queue(&netdev_unregistering_wq, &wait);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops *ops)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops);
485 up_write(&pernet_ops_rwsem);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
491 struct net_device *master_dev;
492 const struct rtnl_link_ops *ops;
497 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
501 ops = master_dev->rtnl_link_ops;
502 if (!ops || !ops->get_slave_size)
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size = nla_total_size(sizeof(struct nlattr)) +
506 ops->get_slave_size(master_dev, dev);
513 static size_t rtnl_link_get_size(const struct net_device *dev)
515 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
525 /* IFLA_INFO_DATA + nested data */
526 size += nla_total_size(sizeof(struct nlattr)) +
529 if (ops->get_xstats_size)
530 /* IFLA_INFO_XSTATS */
531 size += nla_total_size(ops->get_xstats_size(dev));
533 size += rtnl_link_get_slave_info_data_size(dev);
538 static LIST_HEAD(rtnl_af_ops);
540 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
542 const struct rtnl_af_ops *ops;
544 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
545 if (ops->family == family)
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops *ops)
561 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
564 EXPORT_SYMBOL_GPL(rtnl_af_register);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops *ops)
573 list_del_rcu(&ops->list);
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
580 static size_t rtnl_link_get_af_size(const struct net_device *dev,
583 struct rtnl_af_ops *af_ops;
587 size = nla_total_size(sizeof(struct nlattr));
590 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
591 if (af_ops->get_link_af_size) {
592 /* AF_* + nested data */
593 size += nla_total_size(sizeof(struct nlattr)) +
594 af_ops->get_link_af_size(dev, ext_filter_mask);
602 static bool rtnl_have_link_slave_info(const struct net_device *dev)
604 struct net_device *master_dev;
609 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
610 if (master_dev && master_dev->rtnl_link_ops)
616 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617 const struct net_device *dev)
619 struct net_device *master_dev;
620 const struct rtnl_link_ops *ops;
621 struct nlattr *slave_data;
624 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
627 ops = master_dev->rtnl_link_ops;
630 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
632 if (ops->fill_slave_info) {
633 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
636 err = ops->fill_slave_info(skb, master_dev, dev);
638 goto err_cancel_slave_data;
639 nla_nest_end(skb, slave_data);
643 err_cancel_slave_data:
644 nla_nest_cancel(skb, slave_data);
648 static int rtnl_link_info_fill(struct sk_buff *skb,
649 const struct net_device *dev)
651 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
657 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
659 if (ops->fill_xstats) {
660 err = ops->fill_xstats(skb, dev);
664 if (ops->fill_info) {
665 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
668 err = ops->fill_info(skb, dev);
670 goto err_cancel_data;
671 nla_nest_end(skb, data);
676 nla_nest_cancel(skb, data);
680 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
682 struct nlattr *linkinfo;
685 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
686 if (linkinfo == NULL)
689 err = rtnl_link_info_fill(skb, dev);
691 goto err_cancel_link;
693 err = rtnl_link_slave_info_fill(skb, dev);
695 goto err_cancel_link;
697 nla_nest_end(skb, linkinfo);
701 nla_nest_cancel(skb, linkinfo);
706 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
708 struct sock *rtnl = net->rtnl;
711 NETLINK_CB(skb).dst_group = group;
713 refcount_inc(&skb->users);
714 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
716 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
720 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
722 struct sock *rtnl = net->rtnl;
724 return nlmsg_unicast(rtnl, skb, pid);
726 EXPORT_SYMBOL(rtnl_unicast);
728 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
729 struct nlmsghdr *nlh, gfp_t flags)
731 struct sock *rtnl = net->rtnl;
735 report = nlmsg_report(nlh);
737 nlmsg_notify(rtnl, skb, pid, group, report, flags);
739 EXPORT_SYMBOL(rtnl_notify);
741 void rtnl_set_sk_err(struct net *net, u32 group, int error)
743 struct sock *rtnl = net->rtnl;
745 netlink_set_err(rtnl, 0, group, error);
747 EXPORT_SYMBOL(rtnl_set_sk_err);
749 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
754 /* nothing is dumped for dst_default_metrics, so just skip the loop */
755 if (metrics == dst_default_metrics.metrics)
758 mx = nla_nest_start_noflag(skb, RTA_METRICS);
762 for (i = 0; i < RTAX_MAX; i++) {
764 if (i == RTAX_CC_ALGO - 1) {
765 char tmp[TCP_CA_NAME_MAX], *name;
767 name = tcp_ca_get_name_by_key(metrics[i], tmp);
770 if (nla_put_string(skb, i + 1, name))
771 goto nla_put_failure;
772 } else if (i == RTAX_FEATURES - 1) {
773 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
777 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
778 if (nla_put_u32(skb, i + 1, user_features))
779 goto nla_put_failure;
781 if (nla_put_u32(skb, i + 1, metrics[i]))
782 goto nla_put_failure;
789 nla_nest_cancel(skb, mx);
793 return nla_nest_end(skb, mx);
796 nla_nest_cancel(skb, mx);
799 EXPORT_SYMBOL(rtnetlink_put_metrics);
801 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
802 long expires, u32 error)
804 struct rta_cacheinfo ci = {
810 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
811 ci.rta_used = dst->__use;
812 ci.rta_clntref = atomic_read(&dst->__refcnt);
817 clock = jiffies_to_clock_t(abs(expires));
818 clock = min_t(unsigned long, clock, INT_MAX);
819 ci.rta_expires = (expires > 0) ? clock : -clock;
821 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
823 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
825 static void set_operstate(struct net_device *dev, unsigned char transition)
827 unsigned char operstate = dev->operstate;
829 switch (transition) {
831 if ((operstate == IF_OPER_DORMANT ||
832 operstate == IF_OPER_TESTING ||
833 operstate == IF_OPER_UNKNOWN) &&
834 !netif_dormant(dev) && !netif_testing(dev))
835 operstate = IF_OPER_UP;
838 case IF_OPER_TESTING:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_TESTING;
844 case IF_OPER_DORMANT:
845 if (operstate == IF_OPER_UP ||
846 operstate == IF_OPER_UNKNOWN)
847 operstate = IF_OPER_DORMANT;
851 if (dev->operstate != operstate) {
852 write_lock_bh(&dev_base_lock);
853 dev->operstate = operstate;
854 write_unlock_bh(&dev_base_lock);
855 netdev_state_change(dev);
859 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
861 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
862 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
865 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
866 const struct ifinfomsg *ifm)
868 unsigned int flags = ifm->ifi_flags;
870 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
872 flags = (flags & ifm->ifi_change) |
873 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
878 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
879 const struct rtnl_link_stats64 *b)
881 a->rx_packets = b->rx_packets;
882 a->tx_packets = b->tx_packets;
883 a->rx_bytes = b->rx_bytes;
884 a->tx_bytes = b->tx_bytes;
885 a->rx_errors = b->rx_errors;
886 a->tx_errors = b->tx_errors;
887 a->rx_dropped = b->rx_dropped;
888 a->tx_dropped = b->tx_dropped;
890 a->multicast = b->multicast;
891 a->collisions = b->collisions;
893 a->rx_length_errors = b->rx_length_errors;
894 a->rx_over_errors = b->rx_over_errors;
895 a->rx_crc_errors = b->rx_crc_errors;
896 a->rx_frame_errors = b->rx_frame_errors;
897 a->rx_fifo_errors = b->rx_fifo_errors;
898 a->rx_missed_errors = b->rx_missed_errors;
900 a->tx_aborted_errors = b->tx_aborted_errors;
901 a->tx_carrier_errors = b->tx_carrier_errors;
902 a->tx_fifo_errors = b->tx_fifo_errors;
903 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
904 a->tx_window_errors = b->tx_window_errors;
906 a->rx_compressed = b->rx_compressed;
907 a->tx_compressed = b->tx_compressed;
909 a->rx_nohandler = b->rx_nohandler;
913 static inline int rtnl_vfinfo_size(const struct net_device *dev,
916 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
917 int num_vfs = dev_num_vf(dev->dev.parent);
918 size_t size = nla_total_size(0);
921 nla_total_size(sizeof(struct ifla_vf_mac)) +
922 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
923 nla_total_size(sizeof(struct ifla_vf_vlan)) +
924 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
925 nla_total_size(MAX_VLAN_LIST_LEN *
926 sizeof(struct ifla_vf_vlan_info)) +
927 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
928 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
929 nla_total_size(sizeof(struct ifla_vf_rate)) +
930 nla_total_size(sizeof(struct ifla_vf_link_state)) +
931 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
932 nla_total_size(0) + /* nest IFLA_VF_STATS */
933 /* IFLA_VF_STATS_RX_PACKETS */
934 nla_total_size_64bit(sizeof(__u64)) +
935 /* IFLA_VF_STATS_TX_PACKETS */
936 nla_total_size_64bit(sizeof(__u64)) +
937 /* IFLA_VF_STATS_RX_BYTES */
938 nla_total_size_64bit(sizeof(__u64)) +
939 /* IFLA_VF_STATS_TX_BYTES */
940 nla_total_size_64bit(sizeof(__u64)) +
941 /* IFLA_VF_STATS_BROADCAST */
942 nla_total_size_64bit(sizeof(__u64)) +
943 /* IFLA_VF_STATS_MULTICAST */
944 nla_total_size_64bit(sizeof(__u64)) +
945 /* IFLA_VF_STATS_RX_DROPPED */
946 nla_total_size_64bit(sizeof(__u64)) +
947 /* IFLA_VF_STATS_TX_DROPPED */
948 nla_total_size_64bit(sizeof(__u64)) +
949 nla_total_size(sizeof(struct ifla_vf_trust)));
955 static size_t rtnl_port_size(const struct net_device *dev,
958 size_t port_size = nla_total_size(4) /* PORT_VF */
959 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
960 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
961 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
962 + nla_total_size(1) /* PROT_VDP_REQUEST */
963 + nla_total_size(2); /* PORT_VDP_RESPONSE */
964 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
965 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
967 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
970 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
971 !(ext_filter_mask & RTEXT_FILTER_VF))
973 if (dev_num_vf(dev->dev.parent))
974 return port_self_size + vf_ports_size +
975 vf_port_size * dev_num_vf(dev->dev.parent);
977 return port_self_size;
980 static size_t rtnl_xdp_size(void)
982 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
983 nla_total_size(1) + /* XDP_ATTACHED */
984 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
985 nla_total_size(4); /* XDP_<mode>_PROG_ID */
990 static size_t rtnl_prop_list_size(const struct net_device *dev)
992 struct netdev_name_node *name_node;
995 if (list_empty(&dev->name_node->list))
997 size = nla_total_size(0);
998 list_for_each_entry(name_node, &dev->name_node->list, list)
999 size += nla_total_size(ALTIFNAMSIZ);
1003 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1004 u32 ext_filter_mask)
1006 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1007 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1008 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1009 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1010 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1011 + nla_total_size(sizeof(struct rtnl_link_stats))
1012 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1013 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1014 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1015 + nla_total_size(4) /* IFLA_TXQLEN */
1016 + nla_total_size(4) /* IFLA_WEIGHT */
1017 + nla_total_size(4) /* IFLA_MTU */
1018 + nla_total_size(4) /* IFLA_LINK */
1019 + nla_total_size(4) /* IFLA_MASTER */
1020 + nla_total_size(1) /* IFLA_CARRIER */
1021 + nla_total_size(4) /* IFLA_PROMISCUITY */
1022 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1023 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1024 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1025 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1026 + nla_total_size(1) /* IFLA_OPERSTATE */
1027 + nla_total_size(1) /* IFLA_LINKMODE */
1028 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1029 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1030 + nla_total_size(4) /* IFLA_GROUP */
1031 + nla_total_size(ext_filter_mask
1032 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1033 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1034 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1035 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1036 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1037 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1038 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1039 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1040 + rtnl_xdp_size() /* IFLA_XDP */
1041 + nla_total_size(4) /* IFLA_EVENT */
1042 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1043 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1044 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1045 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1046 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1047 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1048 + nla_total_size(4) /* IFLA_MIN_MTU */
1049 + nla_total_size(4) /* IFLA_MAX_MTU */
1050 + rtnl_prop_list_size(dev)
1051 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1055 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1057 struct nlattr *vf_ports;
1058 struct nlattr *vf_port;
1062 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1066 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1067 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1069 goto nla_put_failure;
1070 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1071 goto nla_put_failure;
1072 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1073 if (err == -EMSGSIZE)
1074 goto nla_put_failure;
1076 nla_nest_cancel(skb, vf_port);
1079 nla_nest_end(skb, vf_port);
1082 nla_nest_end(skb, vf_ports);
1087 nla_nest_cancel(skb, vf_ports);
1091 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1093 struct nlattr *port_self;
1096 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1100 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1102 nla_nest_cancel(skb, port_self);
1103 return (err == -EMSGSIZE) ? err : 0;
1106 nla_nest_end(skb, port_self);
1111 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1112 u32 ext_filter_mask)
1116 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1117 !(ext_filter_mask & RTEXT_FILTER_VF))
1120 err = rtnl_port_self_fill(skb, dev);
1124 if (dev_num_vf(dev->dev.parent)) {
1125 err = rtnl_vf_ports_fill(skb, dev);
1133 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1136 struct netdev_phys_item_id ppid;
1138 err = dev_get_phys_port_id(dev, &ppid);
1140 if (err == -EOPNOTSUPP)
1145 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1151 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1153 char name[IFNAMSIZ];
1156 err = dev_get_phys_port_name(dev, name, sizeof(name));
1158 if (err == -EOPNOTSUPP)
1163 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1169 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1171 struct netdev_phys_item_id ppid = { };
1174 err = dev_get_port_parent_id(dev, &ppid, false);
1176 if (err == -EOPNOTSUPP)
1181 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1187 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1188 struct net_device *dev)
1190 struct rtnl_link_stats64 *sp;
1191 struct nlattr *attr;
1193 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1194 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1198 sp = nla_data(attr);
1199 dev_get_stats(dev, sp);
1201 attr = nla_reserve(skb, IFLA_STATS,
1202 sizeof(struct rtnl_link_stats));
1206 copy_rtnl_link_stats(nla_data(attr), sp);
1211 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1212 struct net_device *dev,
1214 struct nlattr *vfinfo)
1216 struct ifla_vf_rss_query_en vf_rss_query_en;
1217 struct nlattr *vf, *vfstats, *vfvlanlist;
1218 struct ifla_vf_link_state vf_linkstate;
1219 struct ifla_vf_vlan_info vf_vlan_info;
1220 struct ifla_vf_spoofchk vf_spoofchk;
1221 struct ifla_vf_tx_rate vf_tx_rate;
1222 struct ifla_vf_stats vf_stats;
1223 struct ifla_vf_trust vf_trust;
1224 struct ifla_vf_vlan vf_vlan;
1225 struct ifla_vf_rate vf_rate;
1226 struct ifla_vf_mac vf_mac;
1227 struct ifla_vf_broadcast vf_broadcast;
1228 struct ifla_vf_info ivi;
1229 struct ifla_vf_guid node_guid;
1230 struct ifla_vf_guid port_guid;
1232 memset(&ivi, 0, sizeof(ivi));
1234 /* Not all SR-IOV capable drivers support the
1235 * spoofcheck and "RSS query enable" query. Preset to
1236 * -1 so the user space tool can detect that the driver
1237 * didn't report anything.
1240 ivi.rss_query_en = -1;
1242 /* The default value for VF link state is "auto"
1243 * IFLA_VF_LINK_STATE_AUTO which equals zero
1246 /* VLAN Protocol by default is 802.1Q */
1247 ivi.vlan_proto = htons(ETH_P_8021Q);
1248 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1251 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1252 memset(&node_guid, 0, sizeof(node_guid));
1253 memset(&port_guid, 0, sizeof(port_guid));
1262 vf_rss_query_en.vf =
1265 port_guid.vf = ivi.vf;
1267 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1268 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1269 vf_vlan.vlan = ivi.vlan;
1270 vf_vlan.qos = ivi.qos;
1271 vf_vlan_info.vlan = ivi.vlan;
1272 vf_vlan_info.qos = ivi.qos;
1273 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1274 vf_tx_rate.rate = ivi.max_tx_rate;
1275 vf_rate.min_tx_rate = ivi.min_tx_rate;
1276 vf_rate.max_tx_rate = ivi.max_tx_rate;
1277 vf_spoofchk.setting = ivi.spoofchk;
1278 vf_linkstate.link_state = ivi.linkstate;
1279 vf_rss_query_en.setting = ivi.rss_query_en;
1280 vf_trust.setting = ivi.trusted;
1281 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1283 goto nla_put_vfinfo_failure;
1284 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1285 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1286 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1287 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1289 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1291 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1293 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1295 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1296 sizeof(vf_rss_query_en),
1297 &vf_rss_query_en) ||
1298 nla_put(skb, IFLA_VF_TRUST,
1299 sizeof(vf_trust), &vf_trust))
1300 goto nla_put_vf_failure;
1302 if (dev->netdev_ops->ndo_get_vf_guid &&
1303 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1305 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1307 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1309 goto nla_put_vf_failure;
1311 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1313 goto nla_put_vf_failure;
1314 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1316 nla_nest_cancel(skb, vfvlanlist);
1317 goto nla_put_vf_failure;
1319 nla_nest_end(skb, vfvlanlist);
1320 memset(&vf_stats, 0, sizeof(vf_stats));
1321 if (dev->netdev_ops->ndo_get_vf_stats)
1322 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1324 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1326 goto nla_put_vf_failure;
1327 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1328 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1329 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1330 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1331 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1332 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1333 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1334 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1335 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1336 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1337 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1338 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1339 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1340 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1341 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1342 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1343 nla_nest_cancel(skb, vfstats);
1344 goto nla_put_vf_failure;
1346 nla_nest_end(skb, vfstats);
1347 nla_nest_end(skb, vf);
1351 nla_nest_cancel(skb, vf);
1352 nla_put_vfinfo_failure:
1353 nla_nest_cancel(skb, vfinfo);
1357 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1358 struct net_device *dev,
1359 u32 ext_filter_mask)
1361 struct nlattr *vfinfo;
1364 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1367 num_vfs = dev_num_vf(dev->dev.parent);
1368 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1371 if (!dev->netdev_ops->ndo_get_vf_config)
1374 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1378 for (i = 0; i < num_vfs; i++) {
1379 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1383 nla_nest_end(skb, vfinfo);
1387 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1389 struct rtnl_link_ifmap map;
1391 memset(&map, 0, sizeof(map));
1392 map.mem_start = dev->mem_start;
1393 map.mem_end = dev->mem_end;
1394 map.base_addr = dev->base_addr;
1397 map.port = dev->if_port;
1399 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1405 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1407 const struct bpf_prog *generic_xdp_prog;
1411 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1412 if (!generic_xdp_prog)
1414 return generic_xdp_prog->aux->id;
1417 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1419 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1422 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1424 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1428 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1429 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1430 u32 (*get_prog_id)(struct net_device *dev))
1435 curr_id = get_prog_id(dev);
1440 err = nla_put_u32(skb, attr, curr_id);
1444 if (*mode != XDP_ATTACHED_NONE)
1445 *mode = XDP_ATTACHED_MULTI;
1452 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1459 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1464 mode = XDP_ATTACHED_NONE;
1465 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1466 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1469 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1470 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1473 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1474 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1478 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1482 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1483 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1488 nla_nest_end(skb, xdp);
1492 nla_nest_cancel(skb, xdp);
1496 static u32 rtnl_get_event(unsigned long event)
1498 u32 rtnl_event_type = IFLA_EVENT_NONE;
1502 rtnl_event_type = IFLA_EVENT_REBOOT;
1504 case NETDEV_FEAT_CHANGE:
1505 rtnl_event_type = IFLA_EVENT_FEATURES;
1507 case NETDEV_BONDING_FAILOVER:
1508 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1510 case NETDEV_NOTIFY_PEERS:
1511 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1513 case NETDEV_RESEND_IGMP:
1514 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1516 case NETDEV_CHANGEINFODATA:
1517 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1523 return rtnl_event_type;
1526 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1528 const struct net_device *upper_dev;
1533 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1535 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1541 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1544 int ifindex = dev_get_iflink(dev);
1546 if (force || dev->ifindex != ifindex)
1547 return nla_put_u32(skb, IFLA_LINK, ifindex);
1552 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1553 struct net_device *dev)
1558 ret = dev_get_alias(dev, buf, sizeof(buf));
1559 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1562 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1563 const struct net_device *dev,
1564 struct net *src_net, gfp_t gfp)
1566 bool put_iflink = false;
1568 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1569 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1571 if (!net_eq(dev_net(dev), link_net)) {
1572 int id = peernet2id_alloc(src_net, link_net, gfp);
1574 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1581 return nla_put_iflink(skb, dev, put_iflink);
1584 static int rtnl_fill_link_af(struct sk_buff *skb,
1585 const struct net_device *dev,
1586 u32 ext_filter_mask)
1588 const struct rtnl_af_ops *af_ops;
1589 struct nlattr *af_spec;
1591 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1595 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1599 if (!af_ops->fill_link_af)
1602 af = nla_nest_start_noflag(skb, af_ops->family);
1606 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1608 * Caller may return ENODATA to indicate that there
1609 * was no data to be dumped. This is not an error, it
1610 * means we should trim the attribute header and
1613 if (err == -ENODATA)
1614 nla_nest_cancel(skb, af);
1618 nla_nest_end(skb, af);
1621 nla_nest_end(skb, af_spec);
1625 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1626 const struct net_device *dev)
1628 struct netdev_name_node *name_node;
1631 list_for_each_entry(name_node, &dev->name_node->list, list) {
1632 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1639 static int rtnl_fill_prop_list(struct sk_buff *skb,
1640 const struct net_device *dev)
1642 struct nlattr *prop_list;
1645 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1649 ret = rtnl_fill_alt_ifnames(skb, dev);
1653 nla_nest_end(skb, prop_list);
1657 nla_nest_cancel(skb, prop_list);
1661 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1662 struct net_device *dev, struct net *src_net,
1663 int type, u32 pid, u32 seq, u32 change,
1664 unsigned int flags, u32 ext_filter_mask,
1665 u32 event, int *new_nsid, int new_ifindex,
1666 int tgt_netnsid, gfp_t gfp)
1668 struct ifinfomsg *ifm;
1669 struct nlmsghdr *nlh;
1672 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1676 ifm = nlmsg_data(nlh);
1677 ifm->ifi_family = AF_UNSPEC;
1679 ifm->ifi_type = dev->type;
1680 ifm->ifi_index = dev->ifindex;
1681 ifm->ifi_flags = dev_get_flags(dev);
1682 ifm->ifi_change = change;
1684 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1685 goto nla_put_failure;
1687 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1688 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1689 nla_put_u8(skb, IFLA_OPERSTATE,
1690 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1691 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1692 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1693 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1694 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1695 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1696 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1697 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1698 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1699 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1701 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1703 put_master_ifindex(skb, dev) ||
1704 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1706 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1707 nla_put_ifalias(skb, dev) ||
1708 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1709 atomic_read(&dev->carrier_up_count) +
1710 atomic_read(&dev->carrier_down_count)) ||
1711 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1712 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1713 atomic_read(&dev->carrier_up_count)) ||
1714 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1715 atomic_read(&dev->carrier_down_count)))
1716 goto nla_put_failure;
1718 if (event != IFLA_EVENT_NONE) {
1719 if (nla_put_u32(skb, IFLA_EVENT, event))
1720 goto nla_put_failure;
1723 if (rtnl_fill_link_ifmap(skb, dev))
1724 goto nla_put_failure;
1726 if (dev->addr_len) {
1727 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1728 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1729 goto nla_put_failure;
1732 if (rtnl_phys_port_id_fill(skb, dev))
1733 goto nla_put_failure;
1735 if (rtnl_phys_port_name_fill(skb, dev))
1736 goto nla_put_failure;
1738 if (rtnl_phys_switch_id_fill(skb, dev))
1739 goto nla_put_failure;
1741 if (rtnl_fill_stats(skb, dev))
1742 goto nla_put_failure;
1744 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1745 goto nla_put_failure;
1747 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1748 goto nla_put_failure;
1750 if (rtnl_xdp_fill(skb, dev))
1751 goto nla_put_failure;
1753 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1754 if (rtnl_link_fill(skb, dev) < 0)
1755 goto nla_put_failure;
1758 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1759 goto nla_put_failure;
1762 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1763 goto nla_put_failure;
1765 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1766 goto nla_put_failure;
1768 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1769 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1770 goto nla_put_failure;
1773 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1774 goto nla_put_failure_rcu;
1777 if (rtnl_fill_prop_list(skb, dev))
1778 goto nla_put_failure;
1780 nlmsg_end(skb, nlh);
1783 nla_put_failure_rcu:
1786 nlmsg_cancel(skb, nlh);
1790 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1791 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1792 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1793 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1794 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1795 [IFLA_MTU] = { .type = NLA_U32 },
1796 [IFLA_LINK] = { .type = NLA_U32 },
1797 [IFLA_MASTER] = { .type = NLA_U32 },
1798 [IFLA_CARRIER] = { .type = NLA_U8 },
1799 [IFLA_TXQLEN] = { .type = NLA_U32 },
1800 [IFLA_WEIGHT] = { .type = NLA_U32 },
1801 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1802 [IFLA_LINKMODE] = { .type = NLA_U8 },
1803 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1804 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1805 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1806 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1807 * allow 0-length string (needed to remove an alias).
1809 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1810 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1811 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1812 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1813 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1814 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1815 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1816 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1817 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1818 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1819 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1820 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1821 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1822 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1823 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1824 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1825 [IFLA_XDP] = { .type = NLA_NESTED },
1826 [IFLA_EVENT] = { .type = NLA_U32 },
1827 [IFLA_GROUP] = { .type = NLA_U32 },
1828 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1829 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1830 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1831 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1832 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1833 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1834 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1835 .len = ALTIFNAMSIZ - 1 },
1836 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1839 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1840 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1841 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1842 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1843 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1846 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1847 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1848 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1849 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1850 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1851 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1852 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1853 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1854 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1855 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1856 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1857 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1858 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1859 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1862 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1863 [IFLA_PORT_VF] = { .type = NLA_U32 },
1864 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1865 .len = PORT_PROFILE_MAX },
1866 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1867 .len = PORT_UUID_MAX },
1868 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1869 .len = PORT_UUID_MAX },
1870 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1871 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1873 /* Unused, but we need to keep it here since user space could
1874 * fill it. It's also broken with regard to NLA_BINARY use in
1875 * combination with structs.
1877 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1878 .len = sizeof(struct ifla_port_vsi) },
1881 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1882 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1883 [IFLA_XDP_FD] = { .type = NLA_S32 },
1884 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
1885 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1886 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1887 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1890 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1892 const struct rtnl_link_ops *ops = NULL;
1893 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1895 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1898 if (linfo[IFLA_INFO_KIND]) {
1899 char kind[MODULE_NAME_LEN];
1901 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1902 ops = rtnl_link_ops_get(kind);
1908 static bool link_master_filtered(struct net_device *dev, int master_idx)
1910 struct net_device *master;
1915 master = netdev_master_upper_dev_get(dev);
1916 if (!master || master->ifindex != master_idx)
1922 static bool link_kind_filtered(const struct net_device *dev,
1923 const struct rtnl_link_ops *kind_ops)
1925 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1931 static bool link_dump_filtered(struct net_device *dev,
1933 const struct rtnl_link_ops *kind_ops)
1935 if (link_master_filtered(dev, master_idx) ||
1936 link_kind_filtered(dev, kind_ops))
1943 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1944 * @sk: netlink socket
1945 * @netnsid: network namespace identifier
1947 * Returns the network namespace identified by netnsid on success or an error
1948 * pointer on failure.
1950 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1954 net = get_net_ns_by_id(sock_net(sk), netnsid);
1956 return ERR_PTR(-EINVAL);
1958 /* For now, the caller is required to have CAP_NET_ADMIN in
1959 * the user namespace owning the target net ns.
1961 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1963 return ERR_PTR(-EACCES);
1967 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1969 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1970 bool strict_check, struct nlattr **tb,
1971 struct netlink_ext_ack *extack)
1976 struct ifinfomsg *ifm;
1978 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1979 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1983 ifm = nlmsg_data(nlh);
1984 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1986 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1989 if (ifm->ifi_index) {
1990 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1994 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1995 IFLA_MAX, ifla_policy,
1999 /* A hack to preserve kernel<->userspace interface.
2000 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2001 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2002 * what iproute2 < v3.9.0 used.
2003 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2004 * attribute, its netlink message is shorter than struct ifinfomsg.
2006 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2007 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2009 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2013 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2015 struct netlink_ext_ack *extack = cb->extack;
2016 const struct nlmsghdr *nlh = cb->nlh;
2017 struct net *net = sock_net(skb->sk);
2018 struct net *tgt_net = net;
2021 struct net_device *dev;
2022 struct hlist_head *head;
2023 struct nlattr *tb[IFLA_MAX+1];
2024 u32 ext_filter_mask = 0;
2025 const struct rtnl_link_ops *kind_ops = NULL;
2026 unsigned int flags = NLM_F_MULTI;
2032 s_idx = cb->args[1];
2034 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2036 if (cb->strict_check)
2042 for (i = 0; i <= IFLA_MAX; ++i) {
2046 /* new attributes should only be added with strict checking */
2048 case IFLA_TARGET_NETNSID:
2049 netnsid = nla_get_s32(tb[i]);
2050 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2051 if (IS_ERR(tgt_net)) {
2052 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2053 return PTR_ERR(tgt_net);
2057 ext_filter_mask = nla_get_u32(tb[i]);
2060 master_idx = nla_get_u32(tb[i]);
2063 kind_ops = linkinfo_to_kind_ops(tb[i]);
2066 if (cb->strict_check) {
2067 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2073 if (master_idx || kind_ops)
2074 flags |= NLM_F_DUMP_FILTERED;
2077 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2079 head = &tgt_net->dev_index_head[h];
2080 hlist_for_each_entry(dev, head, index_hlist) {
2081 if (link_dump_filtered(dev, master_idx, kind_ops))
2085 err = rtnl_fill_ifinfo(skb, dev, net,
2087 NETLINK_CB(cb->skb).portid,
2088 nlh->nlmsg_seq, 0, flags,
2089 ext_filter_mask, 0, NULL, 0,
2090 netnsid, GFP_KERNEL);
2093 if (likely(skb->len))
2107 cb->seq = net->dev_base_seq;
2108 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2115 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2116 struct netlink_ext_ack *exterr)
2118 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2121 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2123 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2126 /* Examine the link attributes and figure out which
2127 * network namespace we are talking about.
2129 if (tb[IFLA_NET_NS_PID])
2130 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2131 else if (tb[IFLA_NET_NS_FD])
2132 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2134 net = get_net(src_net);
2137 EXPORT_SYMBOL(rtnl_link_get_net);
2139 /* Figure out which network namespace we are talking about by
2140 * examining the link attributes in the following order:
2142 * 1. IFLA_NET_NS_PID
2144 * 3. IFLA_TARGET_NETNSID
2146 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2147 struct nlattr *tb[])
2151 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2152 return rtnl_link_get_net(src_net, tb);
2154 if (!tb[IFLA_TARGET_NETNSID])
2155 return get_net(src_net);
2157 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2159 return ERR_PTR(-EINVAL);
2164 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2165 struct net *src_net,
2166 struct nlattr *tb[], int cap)
2170 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2174 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2176 return ERR_PTR(-EPERM);
2182 /* Verify that rtnetlink requests do not pass additional properties
2183 * potentially referring to different network namespaces.
2185 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2186 struct netlink_ext_ack *extack,
2190 if (netns_id_only) {
2191 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2194 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2198 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2201 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2204 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2210 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2214 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2217 if (tb[IFLA_ADDRESS] &&
2218 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2221 if (tb[IFLA_BROADCAST] &&
2222 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2226 if (tb[IFLA_AF_SPEC]) {
2230 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2231 const struct rtnl_af_ops *af_ops;
2234 af_ops = rtnl_af_lookup(nla_type(af));
2237 return -EAFNOSUPPORT;
2240 if (!af_ops->set_link_af) {
2245 if (af_ops->validate_link_af) {
2246 err = af_ops->validate_link_af(dev, af);
2260 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2263 const struct net_device_ops *ops = dev->netdev_ops;
2265 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2268 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2270 if (dev->type != ARPHRD_INFINIBAND)
2273 return handle_infiniband_guid(dev, ivt, guid_type);
2276 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2278 const struct net_device_ops *ops = dev->netdev_ops;
2281 if (tb[IFLA_VF_MAC]) {
2282 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2284 if (ivm->vf >= INT_MAX)
2287 if (ops->ndo_set_vf_mac)
2288 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2294 if (tb[IFLA_VF_VLAN]) {
2295 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2297 if (ivv->vf >= INT_MAX)
2300 if (ops->ndo_set_vf_vlan)
2301 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2303 htons(ETH_P_8021Q));
2308 if (tb[IFLA_VF_VLAN_LIST]) {
2309 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2310 struct nlattr *attr;
2314 if (!ops->ndo_set_vf_vlan)
2317 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2318 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2319 nla_len(attr) < NLA_HDRLEN) {
2322 if (len >= MAX_VLAN_LIST_LEN)
2324 ivvl[len] = nla_data(attr);
2331 if (ivvl[0]->vf >= INT_MAX)
2333 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2334 ivvl[0]->qos, ivvl[0]->vlan_proto);
2339 if (tb[IFLA_VF_TX_RATE]) {
2340 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2341 struct ifla_vf_info ivf;
2343 if (ivt->vf >= INT_MAX)
2346 if (ops->ndo_get_vf_config)
2347 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2352 if (ops->ndo_set_vf_rate)
2353 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2360 if (tb[IFLA_VF_RATE]) {
2361 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2363 if (ivt->vf >= INT_MAX)
2366 if (ops->ndo_set_vf_rate)
2367 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2374 if (tb[IFLA_VF_SPOOFCHK]) {
2375 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2377 if (ivs->vf >= INT_MAX)
2380 if (ops->ndo_set_vf_spoofchk)
2381 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2387 if (tb[IFLA_VF_LINK_STATE]) {
2388 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2390 if (ivl->vf >= INT_MAX)
2393 if (ops->ndo_set_vf_link_state)
2394 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2400 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2401 struct ifla_vf_rss_query_en *ivrssq_en;
2404 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2405 if (ivrssq_en->vf >= INT_MAX)
2407 if (ops->ndo_set_vf_rss_query_en)
2408 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2409 ivrssq_en->setting);
2414 if (tb[IFLA_VF_TRUST]) {
2415 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2417 if (ivt->vf >= INT_MAX)
2420 if (ops->ndo_set_vf_trust)
2421 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2426 if (tb[IFLA_VF_IB_NODE_GUID]) {
2427 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2429 if (ivt->vf >= INT_MAX)
2431 if (!ops->ndo_set_vf_guid)
2433 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2436 if (tb[IFLA_VF_IB_PORT_GUID]) {
2437 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2439 if (ivt->vf >= INT_MAX)
2441 if (!ops->ndo_set_vf_guid)
2444 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2450 static int do_set_master(struct net_device *dev, int ifindex,
2451 struct netlink_ext_ack *extack)
2453 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2454 const struct net_device_ops *ops;
2458 if (upper_dev->ifindex == ifindex)
2460 ops = upper_dev->netdev_ops;
2461 if (ops->ndo_del_slave) {
2462 err = ops->ndo_del_slave(upper_dev, dev);
2471 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2474 ops = upper_dev->netdev_ops;
2475 if (ops->ndo_add_slave) {
2476 err = ops->ndo_add_slave(upper_dev, dev, extack);
2486 #define DO_SETLINK_MODIFIED 0x01
2487 /* notify flag means notify + modified. */
2488 #define DO_SETLINK_NOTIFY 0x03
2489 static int do_setlink(const struct sk_buff *skb,
2490 struct net_device *dev, struct ifinfomsg *ifm,
2491 struct netlink_ext_ack *extack,
2492 struct nlattr **tb, char *ifname, int status)
2494 const struct net_device_ops *ops = dev->netdev_ops;
2497 err = validate_linkmsg(dev, tb);
2501 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2502 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2509 err = dev_change_net_namespace(dev, net, ifname);
2513 status |= DO_SETLINK_MODIFIED;
2517 struct rtnl_link_ifmap *u_map;
2520 if (!ops->ndo_set_config) {
2525 if (!netif_device_present(dev)) {
2530 u_map = nla_data(tb[IFLA_MAP]);
2531 k_map.mem_start = (unsigned long) u_map->mem_start;
2532 k_map.mem_end = (unsigned long) u_map->mem_end;
2533 k_map.base_addr = (unsigned short) u_map->base_addr;
2534 k_map.irq = (unsigned char) u_map->irq;
2535 k_map.dma = (unsigned char) u_map->dma;
2536 k_map.port = (unsigned char) u_map->port;
2538 err = ops->ndo_set_config(dev, &k_map);
2542 status |= DO_SETLINK_NOTIFY;
2545 if (tb[IFLA_ADDRESS]) {
2546 struct sockaddr *sa;
2549 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2551 sa = kmalloc(len, GFP_KERNEL);
2556 sa->sa_family = dev->type;
2557 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2559 err = dev_set_mac_address(dev, sa, extack);
2563 status |= DO_SETLINK_MODIFIED;
2567 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2570 status |= DO_SETLINK_MODIFIED;
2573 if (tb[IFLA_GROUP]) {
2574 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2575 status |= DO_SETLINK_NOTIFY;
2579 * Interface selected by interface index but interface
2580 * name provided implies that a name change has been
2583 if (ifm->ifi_index > 0 && ifname[0]) {
2584 err = dev_change_name(dev, ifname);
2587 status |= DO_SETLINK_MODIFIED;
2590 if (tb[IFLA_IFALIAS]) {
2591 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2592 nla_len(tb[IFLA_IFALIAS]));
2595 status |= DO_SETLINK_NOTIFY;
2598 if (tb[IFLA_BROADCAST]) {
2599 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2600 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2603 if (ifm->ifi_flags || ifm->ifi_change) {
2604 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2610 if (tb[IFLA_MASTER]) {
2611 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2614 status |= DO_SETLINK_MODIFIED;
2617 if (tb[IFLA_CARRIER]) {
2618 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2621 status |= DO_SETLINK_MODIFIED;
2624 if (tb[IFLA_TXQLEN]) {
2625 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2627 err = dev_change_tx_queue_len(dev, value);
2630 status |= DO_SETLINK_MODIFIED;
2633 if (tb[IFLA_GSO_MAX_SIZE]) {
2634 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2636 if (max_size > GSO_MAX_SIZE) {
2641 if (dev->gso_max_size ^ max_size) {
2642 netif_set_gso_max_size(dev, max_size);
2643 status |= DO_SETLINK_MODIFIED;
2647 if (tb[IFLA_GSO_MAX_SEGS]) {
2648 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2650 if (max_segs > GSO_MAX_SEGS) {
2655 if (dev->gso_max_segs ^ max_segs) {
2656 dev->gso_max_segs = max_segs;
2657 status |= DO_SETLINK_MODIFIED;
2661 if (tb[IFLA_OPERSTATE])
2662 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2664 if (tb[IFLA_LINKMODE]) {
2665 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2667 write_lock_bh(&dev_base_lock);
2668 if (dev->link_mode ^ value)
2669 status |= DO_SETLINK_NOTIFY;
2670 dev->link_mode = value;
2671 write_unlock_bh(&dev_base_lock);
2674 if (tb[IFLA_VFINFO_LIST]) {
2675 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2676 struct nlattr *attr;
2679 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2680 if (nla_type(attr) != IFLA_VF_INFO ||
2681 nla_len(attr) < NLA_HDRLEN) {
2685 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2691 err = do_setvfinfo(dev, vfinfo);
2694 status |= DO_SETLINK_NOTIFY;
2699 if (tb[IFLA_VF_PORTS]) {
2700 struct nlattr *port[IFLA_PORT_MAX+1];
2701 struct nlattr *attr;
2706 if (!ops->ndo_set_vf_port)
2709 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2710 if (nla_type(attr) != IFLA_VF_PORT ||
2711 nla_len(attr) < NLA_HDRLEN) {
2715 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2721 if (!port[IFLA_PORT_VF]) {
2725 vf = nla_get_u32(port[IFLA_PORT_VF]);
2726 err = ops->ndo_set_vf_port(dev, vf, port);
2729 status |= DO_SETLINK_NOTIFY;
2734 if (tb[IFLA_PORT_SELF]) {
2735 struct nlattr *port[IFLA_PORT_MAX+1];
2737 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2739 ifla_port_policy, NULL);
2744 if (ops->ndo_set_vf_port)
2745 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2748 status |= DO_SETLINK_NOTIFY;
2751 if (tb[IFLA_AF_SPEC]) {
2755 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2756 const struct rtnl_af_ops *af_ops;
2760 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2762 err = af_ops->set_link_af(dev, af);
2769 status |= DO_SETLINK_NOTIFY;
2774 if (tb[IFLA_PROTO_DOWN]) {
2775 err = dev_change_proto_down(dev,
2776 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2779 status |= DO_SETLINK_NOTIFY;
2783 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2786 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2788 ifla_xdp_policy, NULL);
2792 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2797 if (xdp[IFLA_XDP_FLAGS]) {
2798 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2799 if (xdp_flags & ~XDP_FLAGS_MASK) {
2803 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2809 if (xdp[IFLA_XDP_FD]) {
2810 int expected_fd = -1;
2812 if (xdp_flags & XDP_FLAGS_REPLACE) {
2813 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2818 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2821 err = dev_change_xdp_fd(dev, extack,
2822 nla_get_s32(xdp[IFLA_XDP_FD]),
2827 status |= DO_SETLINK_NOTIFY;
2832 if (status & DO_SETLINK_MODIFIED) {
2833 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2834 netdev_state_change(dev);
2837 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",
2844 static struct net_device *rtnl_dev_get(struct net *net,
2845 struct nlattr *ifname_attr,
2846 struct nlattr *altifname_attr,
2849 char buffer[ALTIFNAMSIZ];
2854 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2855 else if (altifname_attr)
2856 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2861 return __dev_get_by_name(net, ifname);
2864 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2865 struct netlink_ext_ack *extack)
2867 struct net *net = sock_net(skb->sk);
2868 struct ifinfomsg *ifm;
2869 struct net_device *dev;
2871 struct nlattr *tb[IFLA_MAX+1];
2872 char ifname[IFNAMSIZ];
2874 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2875 ifla_policy, extack);
2879 err = rtnl_ensure_unique_netns(tb, extack, false);
2883 if (tb[IFLA_IFNAME])
2884 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2889 ifm = nlmsg_data(nlh);
2890 if (ifm->ifi_index > 0)
2891 dev = __dev_get_by_index(net, ifm->ifi_index);
2892 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2893 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2902 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2907 static int rtnl_group_dellink(const struct net *net, int group)
2909 struct net_device *dev, *aux;
2910 LIST_HEAD(list_kill);
2916 for_each_netdev(net, dev) {
2917 if (dev->group == group) {
2918 const struct rtnl_link_ops *ops;
2921 ops = dev->rtnl_link_ops;
2922 if (!ops || !ops->dellink)
2930 for_each_netdev_safe(net, dev, aux) {
2931 if (dev->group == group) {
2932 const struct rtnl_link_ops *ops;
2934 ops = dev->rtnl_link_ops;
2935 ops->dellink(dev, &list_kill);
2938 unregister_netdevice_many(&list_kill);
2943 int rtnl_delete_link(struct net_device *dev)
2945 const struct rtnl_link_ops *ops;
2946 LIST_HEAD(list_kill);
2948 ops = dev->rtnl_link_ops;
2949 if (!ops || !ops->dellink)
2952 ops->dellink(dev, &list_kill);
2953 unregister_netdevice_many(&list_kill);
2957 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2959 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2960 struct netlink_ext_ack *extack)
2962 struct net *net = sock_net(skb->sk);
2963 struct net *tgt_net = net;
2964 struct net_device *dev = NULL;
2965 struct ifinfomsg *ifm;
2966 struct nlattr *tb[IFLA_MAX+1];
2970 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2971 ifla_policy, extack);
2975 err = rtnl_ensure_unique_netns(tb, extack, true);
2979 if (tb[IFLA_TARGET_NETNSID]) {
2980 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2981 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2982 if (IS_ERR(tgt_net))
2983 return PTR_ERR(tgt_net);
2987 ifm = nlmsg_data(nlh);
2988 if (ifm->ifi_index > 0)
2989 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2990 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2991 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2992 tb[IFLA_ALT_IFNAME], NULL);
2993 else if (tb[IFLA_GROUP])
2994 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2999 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3005 err = rtnl_delete_link(dev);
3014 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3016 unsigned int old_flags;
3019 old_flags = dev->flags;
3020 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3021 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3027 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3028 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3030 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3031 __dev_notify_flags(dev, old_flags, ~0U);
3035 EXPORT_SYMBOL(rtnl_configure_link);
3037 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3038 unsigned char name_assign_type,
3039 const struct rtnl_link_ops *ops,
3040 struct nlattr *tb[],
3041 struct netlink_ext_ack *extack)
3043 struct net_device *dev;
3044 unsigned int num_tx_queues = 1;
3045 unsigned int num_rx_queues = 1;
3047 if (tb[IFLA_NUM_TX_QUEUES])
3048 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3049 else if (ops->get_num_tx_queues)
3050 num_tx_queues = ops->get_num_tx_queues();
3052 if (tb[IFLA_NUM_RX_QUEUES])
3053 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3054 else if (ops->get_num_rx_queues)
3055 num_rx_queues = ops->get_num_rx_queues();
3057 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3058 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3059 return ERR_PTR(-EINVAL);
3062 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3063 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3064 return ERR_PTR(-EINVAL);
3067 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3068 ops->setup, num_tx_queues, num_rx_queues);
3070 return ERR_PTR(-ENOMEM);
3072 dev_net_set(dev, net);
3073 dev->rtnl_link_ops = ops;
3074 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3077 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3080 err = dev_validate_mtu(dev, mtu, extack);
3083 return ERR_PTR(err);
3087 if (tb[IFLA_ADDRESS]) {
3088 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3089 nla_len(tb[IFLA_ADDRESS]));
3090 dev->addr_assign_type = NET_ADDR_SET;
3092 if (tb[IFLA_BROADCAST])
3093 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3094 nla_len(tb[IFLA_BROADCAST]));
3095 if (tb[IFLA_TXQLEN])
3096 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3097 if (tb[IFLA_OPERSTATE])
3098 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3099 if (tb[IFLA_LINKMODE])
3100 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3102 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3103 if (tb[IFLA_GSO_MAX_SIZE])
3104 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3105 if (tb[IFLA_GSO_MAX_SEGS])
3106 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3110 EXPORT_SYMBOL(rtnl_create_link);
3112 static int rtnl_group_changelink(const struct sk_buff *skb,
3113 struct net *net, int group,
3114 struct ifinfomsg *ifm,
3115 struct netlink_ext_ack *extack,
3118 struct net_device *dev, *aux;
3121 for_each_netdev_safe(net, dev, aux) {
3122 if (dev->group == group) {
3123 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3132 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3133 struct nlattr **attr, struct netlink_ext_ack *extack)
3135 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3136 unsigned char name_assign_type = NET_NAME_USER;
3137 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3138 const struct rtnl_link_ops *m_ops = NULL;
3139 struct net_device *master_dev = NULL;
3140 struct net *net = sock_net(skb->sk);
3141 const struct rtnl_link_ops *ops;
3142 struct nlattr *tb[IFLA_MAX + 1];
3143 struct net *dest_net, *link_net;
3144 struct nlattr **slave_data;
3145 char kind[MODULE_NAME_LEN];
3146 struct net_device *dev;
3147 struct ifinfomsg *ifm;
3148 char ifname[IFNAMSIZ];
3149 struct nlattr **data;
3152 #ifdef CONFIG_MODULES
3155 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3156 ifla_policy, extack);
3160 err = rtnl_ensure_unique_netns(tb, extack, false);
3164 if (tb[IFLA_IFNAME])
3165 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3169 ifm = nlmsg_data(nlh);
3170 if (ifm->ifi_index > 0)
3171 dev = __dev_get_by_index(net, ifm->ifi_index);
3172 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3173 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3178 master_dev = netdev_master_upper_dev_get(dev);
3180 m_ops = master_dev->rtnl_link_ops;
3183 err = validate_linkmsg(dev, tb);
3187 if (tb[IFLA_LINKINFO]) {
3188 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3190 ifla_info_policy, NULL);
3194 memset(linkinfo, 0, sizeof(linkinfo));
3196 if (linkinfo[IFLA_INFO_KIND]) {
3197 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3198 ops = rtnl_link_ops_get(kind);
3206 if (ops->maxtype > RTNL_MAX_TYPE)
3209 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3210 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3211 linkinfo[IFLA_INFO_DATA],
3212 ops->policy, extack);
3217 if (ops->validate) {
3218 err = ops->validate(tb, data, extack);
3226 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3229 if (m_ops->slave_maxtype &&
3230 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3231 err = nla_parse_nested_deprecated(slave_attr,
3232 m_ops->slave_maxtype,
3233 linkinfo[IFLA_INFO_SLAVE_DATA],
3234 m_ops->slave_policy,
3238 slave_data = slave_attr;
3245 if (nlh->nlmsg_flags & NLM_F_EXCL)
3247 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3250 if (linkinfo[IFLA_INFO_DATA]) {
3251 if (!ops || ops != dev->rtnl_link_ops ||
3255 err = ops->changelink(dev, tb, data, extack);
3258 status |= DO_SETLINK_NOTIFY;
3261 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3262 if (!m_ops || !m_ops->slave_changelink)
3265 err = m_ops->slave_changelink(master_dev, dev, tb,
3266 slave_data, extack);
3269 status |= DO_SETLINK_NOTIFY;
3272 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3275 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3276 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3277 return rtnl_group_changelink(skb, net,
3278 nla_get_u32(tb[IFLA_GROUP]),
3283 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3287 #ifdef CONFIG_MODULES
3290 request_module("rtnl-link-%s", kind);
3292 ops = rtnl_link_ops_get(kind);
3297 NL_SET_ERR_MSG(extack, "Unknown device type");
3305 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3306 name_assign_type = NET_NAME_ENUM;
3309 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3310 if (IS_ERR(dest_net))
3311 return PTR_ERR(dest_net);
3313 if (tb[IFLA_LINK_NETNSID]) {
3314 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3316 link_net = get_net_ns_by_id(dest_net, id);
3318 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3323 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3329 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3330 name_assign_type, ops, tb, extack);
3336 dev->ifindex = ifm->ifi_index;
3339 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3340 /* Drivers should call free_netdev() in ->destructor
3341 * and unregister it on failure after registration
3342 * so that device could be finally freed in rtnl_unlock.
3345 /* If device is not registered at all, free it now */
3346 if (dev->reg_state == NETREG_UNINITIALIZED)
3351 err = register_netdevice(dev);
3357 err = rtnl_configure_link(dev, ifm);
3359 goto out_unregister;
3361 err = dev_change_net_namespace(dev, dest_net, ifname);
3363 goto out_unregister;
3365 if (tb[IFLA_MASTER]) {
3366 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3368 goto out_unregister;
3377 LIST_HEAD(list_kill);
3379 ops->dellink(dev, &list_kill);
3380 unregister_netdevice_many(&list_kill);
3382 unregister_netdevice(dev);
3387 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3388 struct netlink_ext_ack *extack)
3390 struct nlattr **attr;
3393 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3397 ret = __rtnl_newlink(skb, nlh, attr, extack);
3402 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3403 const struct nlmsghdr *nlh,
3405 struct netlink_ext_ack *extack)
3407 struct ifinfomsg *ifm;
3410 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3411 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3415 if (!netlink_strict_get_check(skb))
3416 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3417 ifla_policy, extack);
3419 ifm = nlmsg_data(nlh);
3420 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3422 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3426 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3427 ifla_policy, extack);
3431 for (i = 0; i <= IFLA_MAX; i++) {
3437 case IFLA_ALT_IFNAME:
3439 case IFLA_TARGET_NETNSID:
3442 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3450 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3451 struct netlink_ext_ack *extack)
3453 struct net *net = sock_net(skb->sk);
3454 struct net *tgt_net = net;
3455 struct ifinfomsg *ifm;
3456 struct nlattr *tb[IFLA_MAX+1];
3457 struct net_device *dev = NULL;
3458 struct sk_buff *nskb;
3461 u32 ext_filter_mask = 0;
3463 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3467 err = rtnl_ensure_unique_netns(tb, extack, true);
3471 if (tb[IFLA_TARGET_NETNSID]) {
3472 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3473 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3474 if (IS_ERR(tgt_net))
3475 return PTR_ERR(tgt_net);
3478 if (tb[IFLA_EXT_MASK])
3479 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3482 ifm = nlmsg_data(nlh);
3483 if (ifm->ifi_index > 0)
3484 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3485 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3486 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3487 tb[IFLA_ALT_IFNAME], NULL);
3496 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3500 err = rtnl_fill_ifinfo(nskb, dev, net,
3501 RTM_NEWLINK, NETLINK_CB(skb).portid,
3502 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3503 0, NULL, 0, netnsid, GFP_KERNEL);
3505 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3506 WARN_ON(err == -EMSGSIZE);
3509 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3517 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3518 bool *changed, struct netlink_ext_ack *extack)
3523 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3527 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3531 if (cmd == RTM_NEWLINKPROP) {
3532 err = netdev_name_node_alt_create(dev, alt_ifname);
3535 } else if (cmd == RTM_DELLINKPROP) {
3536 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3548 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3549 struct netlink_ext_ack *extack)
3551 struct net *net = sock_net(skb->sk);
3552 struct nlattr *tb[IFLA_MAX + 1];
3553 struct net_device *dev;
3554 struct ifinfomsg *ifm;
3555 bool changed = false;
3556 struct nlattr *attr;
3559 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3563 err = rtnl_ensure_unique_netns(tb, extack, true);
3567 ifm = nlmsg_data(nlh);
3568 if (ifm->ifi_index > 0)
3569 dev = __dev_get_by_index(net, ifm->ifi_index);
3570 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3571 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3572 tb[IFLA_ALT_IFNAME], NULL);
3579 if (!tb[IFLA_PROP_LIST])
3582 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3583 switch (nla_type(attr)) {
3584 case IFLA_ALT_IFNAME:
3585 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3593 netdev_state_change(dev);
3597 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3598 struct netlink_ext_ack *extack)
3600 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3603 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3604 struct netlink_ext_ack *extack)
3606 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3609 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3611 struct net *net = sock_net(skb->sk);
3612 struct net_device *dev;
3613 struct nlattr *tb[IFLA_MAX+1];
3614 u32 ext_filter_mask = 0;
3615 u16 min_ifinfo_dump_size = 0;
3618 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3619 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3620 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3622 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3623 if (tb[IFLA_EXT_MASK])
3624 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3627 if (!ext_filter_mask)
3628 return NLMSG_GOODSIZE;
3630 * traverse the list of net devices and compute the minimum
3631 * buffer size based upon the filter mask.
3634 for_each_netdev_rcu(net, dev) {
3635 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3641 return nlmsg_total_size(min_ifinfo_dump_size);
3644 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3647 int s_idx = cb->family;
3648 int type = cb->nlh->nlmsg_type - RTM_BASE;
3654 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3655 struct rtnl_link **tab;
3656 struct rtnl_link *link;
3657 rtnl_dumpit_func dumpit;
3659 if (idx < s_idx || idx == PF_PACKET)
3662 if (type < 0 || type >= RTM_NR_MSGTYPES)
3665 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3673 dumpit = link->dumpit;
3678 memset(&cb->args[0], 0, sizeof(cb->args));
3682 ret = dumpit(skb, cb);
3688 return skb->len ? : ret;
3691 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3692 unsigned int change,
3693 u32 event, gfp_t flags, int *new_nsid,
3696 struct net *net = dev_net(dev);
3697 struct sk_buff *skb;
3699 size_t if_info_size;
3701 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3705 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3706 type, 0, 0, change, 0, 0, event,
3707 new_nsid, new_ifindex, -1, flags);
3709 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3710 WARN_ON(err == -EMSGSIZE);
3717 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3721 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3723 struct net *net = dev_net(dev);
3725 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3728 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3729 unsigned int change, u32 event,
3730 gfp_t flags, int *new_nsid, int new_ifindex)
3732 struct sk_buff *skb;
3734 if (dev->reg_state != NETREG_REGISTERED)
3737 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3740 rtmsg_ifinfo_send(skb, dev, flags);
3743 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3746 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3750 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3751 gfp_t flags, int *new_nsid, int new_ifindex)
3753 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3754 new_nsid, new_ifindex);
3757 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3758 struct net_device *dev,
3759 u8 *addr, u16 vid, u32 pid, u32 seq,
3760 int type, unsigned int flags,
3761 int nlflags, u16 ndm_state)
3763 struct nlmsghdr *nlh;
3766 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3770 ndm = nlmsg_data(nlh);
3771 ndm->ndm_family = AF_BRIDGE;
3774 ndm->ndm_flags = flags;
3776 ndm->ndm_ifindex = dev->ifindex;
3777 ndm->ndm_state = ndm_state;
3779 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3780 goto nla_put_failure;
3782 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3783 goto nla_put_failure;
3785 nlmsg_end(skb, nlh);
3789 nlmsg_cancel(skb, nlh);
3793 static inline size_t rtnl_fdb_nlmsg_size(void)
3795 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3796 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3797 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3801 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3804 struct net *net = dev_net(dev);
3805 struct sk_buff *skb;
3808 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3812 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3813 0, 0, type, NTF_SELF, 0, ndm_state);
3819 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3822 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3826 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3828 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3829 struct nlattr *tb[],
3830 struct net_device *dev,
3831 const unsigned char *addr, u16 vid,
3836 /* If aging addresses are supported device will need to
3837 * implement its own handler for this.
3839 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3840 pr_info("%s: FDB only supports static addresses\n", dev->name);
3845 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3849 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3850 err = dev_uc_add_excl(dev, addr);
3851 else if (is_multicast_ether_addr(addr))
3852 err = dev_mc_add_excl(dev, addr);
3854 /* Only return duplicate errors if NLM_F_EXCL is set */
3855 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3860 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3862 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3863 struct netlink_ext_ack *extack)
3868 if (nla_len(vlan_attr) != sizeof(u16)) {
3869 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3873 vid = nla_get_u16(vlan_attr);
3875 if (!vid || vid >= VLAN_VID_MASK) {
3876 NL_SET_ERR_MSG(extack, "invalid vlan id");
3884 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3885 struct netlink_ext_ack *extack)
3887 struct net *net = sock_net(skb->sk);
3889 struct nlattr *tb[NDA_MAX+1];
3890 struct net_device *dev;
3895 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3900 ndm = nlmsg_data(nlh);
3901 if (ndm->ndm_ifindex == 0) {
3902 NL_SET_ERR_MSG(extack, "invalid ifindex");
3906 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3908 NL_SET_ERR_MSG(extack, "unknown ifindex");
3912 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3913 NL_SET_ERR_MSG(extack, "invalid address");
3917 if (dev->type != ARPHRD_ETHER) {
3918 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3922 addr = nla_data(tb[NDA_LLADDR]);
3924 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3930 /* Support fdb on master device the net/bridge default case */
3931 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3932 netif_is_bridge_port(dev)) {
3933 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3934 const struct net_device_ops *ops = br_dev->netdev_ops;
3936 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3937 nlh->nlmsg_flags, extack);
3941 ndm->ndm_flags &= ~NTF_MASTER;
3944 /* Embedded bridge, macvlan, and any other device support */
3945 if ((ndm->ndm_flags & NTF_SELF)) {
3946 if (dev->netdev_ops->ndo_fdb_add)
3947 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3952 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3956 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3958 ndm->ndm_flags &= ~NTF_SELF;
3966 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3968 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3969 struct nlattr *tb[],
3970 struct net_device *dev,
3971 const unsigned char *addr, u16 vid)
3975 /* If aging addresses are supported device will need to
3976 * implement its own handler for this.
3978 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3979 pr_info("%s: FDB only supports static addresses\n", dev->name);
3983 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3984 err = dev_uc_del(dev, addr);
3985 else if (is_multicast_ether_addr(addr))
3986 err = dev_mc_del(dev, addr);
3990 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3992 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3993 struct netlink_ext_ack *extack)
3995 struct net *net = sock_net(skb->sk);
3997 struct nlattr *tb[NDA_MAX+1];
3998 struct net_device *dev;
4003 if (!netlink_capable(skb, CAP_NET_ADMIN))
4006 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4011 ndm = nlmsg_data(nlh);
4012 if (ndm->ndm_ifindex == 0) {
4013 NL_SET_ERR_MSG(extack, "invalid ifindex");
4017 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4019 NL_SET_ERR_MSG(extack, "unknown ifindex");
4023 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4024 NL_SET_ERR_MSG(extack, "invalid address");
4028 if (dev->type != ARPHRD_ETHER) {
4029 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4033 addr = nla_data(tb[NDA_LLADDR]);
4035 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4041 /* Support fdb on master device the net/bridge default case */
4042 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4043 netif_is_bridge_port(dev)) {
4044 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4045 const struct net_device_ops *ops = br_dev->netdev_ops;
4047 if (ops->ndo_fdb_del)
4048 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4053 ndm->ndm_flags &= ~NTF_MASTER;
4056 /* Embedded bridge, macvlan, and any other device support */
4057 if (ndm->ndm_flags & NTF_SELF) {
4058 if (dev->netdev_ops->ndo_fdb_del)
4059 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4062 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4065 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4067 ndm->ndm_flags &= ~NTF_SELF;
4074 static int nlmsg_populate_fdb(struct sk_buff *skb,
4075 struct netlink_callback *cb,
4076 struct net_device *dev,
4078 struct netdev_hw_addr_list *list)
4080 struct netdev_hw_addr *ha;
4084 portid = NETLINK_CB(cb->skb).portid;
4085 seq = cb->nlh->nlmsg_seq;
4087 list_for_each_entry(ha, &list->list, list) {
4088 if (*idx < cb->args[2])
4091 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4093 RTM_NEWNEIGH, NTF_SELF,
4094 NLM_F_MULTI, NUD_PERMANENT);
4104 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4105 * @skb: socket buffer to store message in
4106 * @cb: netlink callback
4108 * @filter_dev: ignored
4109 * @idx: the number of FDB table entries dumped is added to *@idx
4111 * Default netdevice operation to dump the existing unicast address list.
4112 * Returns number of addresses from list put in skb.
4114 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4115 struct netlink_callback *cb,
4116 struct net_device *dev,
4117 struct net_device *filter_dev,
4122 if (dev->type != ARPHRD_ETHER)
4125 netif_addr_lock_bh(dev);
4126 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4129 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4131 netif_addr_unlock_bh(dev);
4134 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4136 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4137 int *br_idx, int *brport_idx,
4138 struct netlink_ext_ack *extack)
4140 struct nlattr *tb[NDA_MAX + 1];
4144 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4145 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4149 ndm = nlmsg_data(nlh);
4150 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4151 ndm->ndm_flags || ndm->ndm_type) {
4152 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4156 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4157 NDA_MAX, NULL, extack);
4161 *brport_idx = ndm->ndm_ifindex;
4162 for (i = 0; i <= NDA_MAX; ++i) {
4168 if (nla_len(tb[i]) != sizeof(u32)) {
4169 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4172 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4175 if (nla_len(tb[i]) != sizeof(u32)) {
4176 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4179 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4182 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4190 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4191 int *br_idx, int *brport_idx,
4192 struct netlink_ext_ack *extack)
4194 struct nlattr *tb[IFLA_MAX+1];
4197 /* A hack to preserve kernel<->userspace interface.
4198 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4199 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4200 * So, check for ndmsg with an optional u32 attribute (not used here).
4201 * Fortunately these sizes don't conflict with the size of ifinfomsg
4202 * with an optional attribute.
4204 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4205 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4206 nla_attr_size(sizeof(u32)))) {
4207 struct ifinfomsg *ifm;
4209 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4210 tb, IFLA_MAX, ifla_policy,
4214 } else if (err == 0) {
4215 if (tb[IFLA_MASTER])
4216 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4219 ifm = nlmsg_data(nlh);
4220 *brport_idx = ifm->ifi_index;
4225 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4227 struct net_device *dev;
4228 struct net_device *br_dev = NULL;
4229 const struct net_device_ops *ops = NULL;
4230 const struct net_device_ops *cops = NULL;
4231 struct net *net = sock_net(skb->sk);
4232 struct hlist_head *head;
4240 if (cb->strict_check)
4241 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4244 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4250 br_dev = __dev_get_by_index(net, br_idx);
4254 ops = br_dev->netdev_ops;
4258 s_idx = cb->args[1];
4260 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4262 head = &net->dev_index_head[h];
4263 hlist_for_each_entry(dev, head, index_hlist) {
4265 if (brport_idx && (dev->ifindex != brport_idx))
4268 if (!br_idx) { /* user did not specify a specific bridge */
4269 if (netif_is_bridge_port(dev)) {
4270 br_dev = netdev_master_upper_dev_get(dev);
4271 cops = br_dev->netdev_ops;
4274 if (dev != br_dev &&
4275 !netif_is_bridge_port(dev))
4278 if (br_dev != netdev_master_upper_dev_get(dev) &&
4279 !(dev->priv_flags & IFF_EBRIDGE))
4287 if (netif_is_bridge_port(dev)) {
4288 if (cops && cops->ndo_fdb_dump) {
4289 err = cops->ndo_fdb_dump(skb, cb,
4292 if (err == -EMSGSIZE)
4297 if (dev->netdev_ops->ndo_fdb_dump)
4298 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4302 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4304 if (err == -EMSGSIZE)
4309 /* reset fdb offset to 0 for rest of the interfaces */
4325 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4326 struct nlattr **tb, u8 *ndm_flags,
4327 int *br_idx, int *brport_idx, u8 **addr,
4328 u16 *vid, struct netlink_ext_ack *extack)
4333 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4334 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4338 ndm = nlmsg_data(nlh);
4339 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4341 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4345 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4346 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4350 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4351 NDA_MAX, nda_policy, extack);
4355 *ndm_flags = ndm->ndm_flags;
4356 *brport_idx = ndm->ndm_ifindex;
4357 for (i = 0; i <= NDA_MAX; ++i) {
4363 *br_idx = nla_get_u32(tb[i]);
4366 if (nla_len(tb[i]) != ETH_ALEN) {
4367 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4370 *addr = nla_data(tb[i]);
4373 err = fdb_vid_parse(tb[i], vid, extack);
4380 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4388 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4389 struct netlink_ext_ack *extack)
4391 struct net_device *dev = NULL, *br_dev = NULL;
4392 const struct net_device_ops *ops = NULL;
4393 struct net *net = sock_net(in_skb->sk);
4394 struct nlattr *tb[NDA_MAX + 1];
4395 struct sk_buff *skb;
4403 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4404 &brport_idx, &addr, &vid, extack);
4409 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4414 dev = __dev_get_by_index(net, brport_idx);
4416 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4423 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4427 br_dev = __dev_get_by_index(net, br_idx);
4429 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4432 ops = br_dev->netdev_ops;
4436 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4437 if (!netif_is_bridge_port(dev)) {
4438 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4441 br_dev = netdev_master_upper_dev_get(dev);
4443 NL_SET_ERR_MSG(extack, "Master of device not found");
4446 ops = br_dev->netdev_ops;
4448 if (!(ndm_flags & NTF_SELF)) {
4449 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4452 ops = dev->netdev_ops;
4456 if (!br_dev && !dev) {
4457 NL_SET_ERR_MSG(extack, "No device specified");
4461 if (!ops || !ops->ndo_fdb_get) {
4462 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4466 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4472 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4473 NETLINK_CB(in_skb).portid,
4474 nlh->nlmsg_seq, extack);
4478 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4484 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4485 unsigned int attrnum, unsigned int flag)
4488 return nla_put_u8(skb, attrnum, !!(flags & flag));
4492 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4493 struct net_device *dev, u16 mode,
4494 u32 flags, u32 mask, int nlflags,
4496 int (*vlan_fill)(struct sk_buff *skb,
4497 struct net_device *dev,
4500 struct nlmsghdr *nlh;
4501 struct ifinfomsg *ifm;
4502 struct nlattr *br_afspec;
4503 struct nlattr *protinfo;
4504 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4505 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4508 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4512 ifm = nlmsg_data(nlh);
4513 ifm->ifi_family = AF_BRIDGE;
4515 ifm->ifi_type = dev->type;
4516 ifm->ifi_index = dev->ifindex;
4517 ifm->ifi_flags = dev_get_flags(dev);
4518 ifm->ifi_change = 0;
4521 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4522 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4523 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4525 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4527 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4528 (dev->ifindex != dev_get_iflink(dev) &&
4529 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4530 goto nla_put_failure;
4532 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4534 goto nla_put_failure;
4536 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4537 nla_nest_cancel(skb, br_afspec);
4538 goto nla_put_failure;
4541 if (mode != BRIDGE_MODE_UNDEF) {
4542 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4543 nla_nest_cancel(skb, br_afspec);
4544 goto nla_put_failure;
4548 err = vlan_fill(skb, dev, filter_mask);
4550 nla_nest_cancel(skb, br_afspec);
4551 goto nla_put_failure;
4554 nla_nest_end(skb, br_afspec);
4556 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4558 goto nla_put_failure;
4560 if (brport_nla_put_flag(skb, flags, mask,
4561 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4562 brport_nla_put_flag(skb, flags, mask,
4563 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4564 brport_nla_put_flag(skb, flags, mask,
4565 IFLA_BRPORT_FAST_LEAVE,
4566 BR_MULTICAST_FAST_LEAVE) ||
4567 brport_nla_put_flag(skb, flags, mask,
4568 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4569 brport_nla_put_flag(skb, flags, mask,
4570 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4571 brport_nla_put_flag(skb, flags, mask,
4572 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4573 brport_nla_put_flag(skb, flags, mask,
4574 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4575 brport_nla_put_flag(skb, flags, mask,
4576 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4577 brport_nla_put_flag(skb, flags, mask,
4578 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4579 brport_nla_put_flag(skb, flags, mask,
4580 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4581 nla_nest_cancel(skb, protinfo);
4582 goto nla_put_failure;
4585 nla_nest_end(skb, protinfo);
4587 nlmsg_end(skb, nlh);
4590 nlmsg_cancel(skb, nlh);
4591 return err ? err : -EMSGSIZE;
4593 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4595 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4596 bool strict_check, u32 *filter_mask,
4597 struct netlink_ext_ack *extack)
4599 struct nlattr *tb[IFLA_MAX+1];
4603 struct ifinfomsg *ifm;
4605 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4606 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4610 ifm = nlmsg_data(nlh);
4611 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4612 ifm->ifi_change || ifm->ifi_index) {
4613 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4617 err = nlmsg_parse_deprecated_strict(nlh,
4618 sizeof(struct ifinfomsg),
4619 tb, IFLA_MAX, ifla_policy,
4622 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4623 tb, IFLA_MAX, ifla_policy,
4629 /* new attributes should only be added with strict checking */
4630 for (i = 0; i <= IFLA_MAX; ++i) {
4636 *filter_mask = nla_get_u32(tb[i]);
4640 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4649 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4651 const struct nlmsghdr *nlh = cb->nlh;
4652 struct net *net = sock_net(skb->sk);
4653 struct net_device *dev;
4655 u32 portid = NETLINK_CB(cb->skb).portid;
4656 u32 seq = nlh->nlmsg_seq;
4657 u32 filter_mask = 0;
4660 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4662 if (err < 0 && cb->strict_check)
4666 for_each_netdev_rcu(net, dev) {
4667 const struct net_device_ops *ops = dev->netdev_ops;
4668 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4670 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4671 if (idx >= cb->args[0]) {
4672 err = br_dev->netdev_ops->ndo_bridge_getlink(
4673 skb, portid, seq, dev,
4674 filter_mask, NLM_F_MULTI);
4675 if (err < 0 && err != -EOPNOTSUPP) {
4676 if (likely(skb->len))
4685 if (ops->ndo_bridge_getlink) {
4686 if (idx >= cb->args[0]) {
4687 err = ops->ndo_bridge_getlink(skb, portid,
4691 if (err < 0 && err != -EOPNOTSUPP) {
4692 if (likely(skb->len))
4709 static inline size_t bridge_nlmsg_size(void)
4711 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4712 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4713 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4714 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4715 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4716 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4717 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4718 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4719 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4720 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4721 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4724 static int rtnl_bridge_notify(struct net_device *dev)
4726 struct net *net = dev_net(dev);
4727 struct sk_buff *skb;
4728 int err = -EOPNOTSUPP;
4730 if (!dev->netdev_ops->ndo_bridge_getlink)
4733 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4739 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4746 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4749 WARN_ON(err == -EMSGSIZE);
4752 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4756 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4757 struct netlink_ext_ack *extack)
4759 struct net *net = sock_net(skb->sk);
4760 struct ifinfomsg *ifm;
4761 struct net_device *dev;
4762 struct nlattr *br_spec, *attr = NULL;
4763 int rem, err = -EOPNOTSUPP;
4765 bool have_flags = false;
4767 if (nlmsg_len(nlh) < sizeof(*ifm))
4770 ifm = nlmsg_data(nlh);
4771 if (ifm->ifi_family != AF_BRIDGE)
4772 return -EPFNOSUPPORT;
4774 dev = __dev_get_by_index(net, ifm->ifi_index);
4776 NL_SET_ERR_MSG(extack, "unknown ifindex");
4780 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4782 nla_for_each_nested(attr, br_spec, rem) {
4783 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4784 if (nla_len(attr) < sizeof(flags))
4788 flags = nla_get_u16(attr);
4794 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4795 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4797 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4802 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4807 flags &= ~BRIDGE_FLAGS_MASTER;
4810 if ((flags & BRIDGE_FLAGS_SELF)) {
4811 if (!dev->netdev_ops->ndo_bridge_setlink)
4814 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4818 flags &= ~BRIDGE_FLAGS_SELF;
4820 /* Generate event to notify upper layer of bridge
4823 err = rtnl_bridge_notify(dev);
4828 memcpy(nla_data(attr), &flags, sizeof(flags));
4833 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4834 struct netlink_ext_ack *extack)
4836 struct net *net = sock_net(skb->sk);
4837 struct ifinfomsg *ifm;
4838 struct net_device *dev;
4839 struct nlattr *br_spec, *attr = NULL;
4840 int rem, err = -EOPNOTSUPP;
4842 bool have_flags = false;
4844 if (nlmsg_len(nlh) < sizeof(*ifm))
4847 ifm = nlmsg_data(nlh);
4848 if (ifm->ifi_family != AF_BRIDGE)
4849 return -EPFNOSUPPORT;
4851 dev = __dev_get_by_index(net, ifm->ifi_index);
4853 NL_SET_ERR_MSG(extack, "unknown ifindex");
4857 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4859 nla_for_each_nested(attr, br_spec, rem) {
4860 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4861 if (nla_len(attr) < sizeof(flags))
4865 flags = nla_get_u16(attr);
4871 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4872 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4874 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4879 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4883 flags &= ~BRIDGE_FLAGS_MASTER;
4886 if ((flags & BRIDGE_FLAGS_SELF)) {
4887 if (!dev->netdev_ops->ndo_bridge_dellink)
4890 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4894 flags &= ~BRIDGE_FLAGS_SELF;
4896 /* Generate event to notify upper layer of bridge
4899 err = rtnl_bridge_notify(dev);
4904 memcpy(nla_data(attr), &flags, sizeof(flags));
4909 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4911 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4912 (!idxattr || idxattr == attrid);
4915 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4916 static int rtnl_get_offload_stats_attr_size(int attr_id)
4919 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4920 return sizeof(struct rtnl_link_stats64);
4926 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4929 struct nlattr *attr = NULL;
4934 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4935 dev->netdev_ops->ndo_get_offload_stats))
4938 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4939 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4940 if (attr_id < *prividx)
4943 size = rtnl_get_offload_stats_attr_size(attr_id);
4947 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4950 attr = nla_reserve_64bit(skb, attr_id, size,
4951 IFLA_OFFLOAD_XSTATS_UNSPEC);
4953 goto nla_put_failure;
4955 attr_data = nla_data(attr);
4956 memset(attr_data, 0, size);
4957 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4960 goto get_offload_stats_failure;
4971 get_offload_stats_failure:
4976 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4982 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4983 dev->netdev_ops->ndo_get_offload_stats))
4986 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4987 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4988 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4990 size = rtnl_get_offload_stats_attr_size(attr_id);
4991 nla_size += nla_total_size_64bit(size);
4995 nla_size += nla_total_size(0);
5000 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5001 int type, u32 pid, u32 seq, u32 change,
5002 unsigned int flags, unsigned int filter_mask,
5003 int *idxattr, int *prividx)
5005 struct if_stats_msg *ifsm;
5006 struct nlmsghdr *nlh;
5007 struct nlattr *attr;
5008 int s_prividx = *prividx;
5013 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5017 ifsm = nlmsg_data(nlh);
5018 ifsm->family = PF_UNSPEC;
5021 ifsm->ifindex = dev->ifindex;
5022 ifsm->filter_mask = filter_mask;
5024 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5025 struct rtnl_link_stats64 *sp;
5027 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5028 sizeof(struct rtnl_link_stats64),
5031 goto nla_put_failure;
5033 sp = nla_data(attr);
5034 dev_get_stats(dev, sp);
5037 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5038 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5040 if (ops && ops->fill_linkxstats) {
5041 *idxattr = IFLA_STATS_LINK_XSTATS;
5042 attr = nla_nest_start_noflag(skb,
5043 IFLA_STATS_LINK_XSTATS);
5045 goto nla_put_failure;
5047 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5048 nla_nest_end(skb, attr);
5050 goto nla_put_failure;
5055 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5057 const struct rtnl_link_ops *ops = NULL;
5058 const struct net_device *master;
5060 master = netdev_master_upper_dev_get(dev);
5062 ops = master->rtnl_link_ops;
5063 if (ops && ops->fill_linkxstats) {
5064 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5065 attr = nla_nest_start_noflag(skb,
5066 IFLA_STATS_LINK_XSTATS_SLAVE);
5068 goto nla_put_failure;
5070 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5071 nla_nest_end(skb, attr);
5073 goto nla_put_failure;
5078 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5080 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5081 attr = nla_nest_start_noflag(skb,
5082 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5084 goto nla_put_failure;
5086 err = rtnl_get_offload_stats(skb, dev, prividx);
5087 if (err == -ENODATA)
5088 nla_nest_cancel(skb, attr);
5090 nla_nest_end(skb, attr);
5092 if (err && err != -ENODATA)
5093 goto nla_put_failure;
5097 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5098 struct rtnl_af_ops *af_ops;
5100 *idxattr = IFLA_STATS_AF_SPEC;
5101 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5103 goto nla_put_failure;
5106 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5107 if (af_ops->fill_stats_af) {
5111 af = nla_nest_start_noflag(skb,
5115 goto nla_put_failure;
5117 err = af_ops->fill_stats_af(skb, dev);
5119 if (err == -ENODATA) {
5120 nla_nest_cancel(skb, af);
5121 } else if (err < 0) {
5123 goto nla_put_failure;
5126 nla_nest_end(skb, af);
5131 nla_nest_end(skb, attr);
5136 nlmsg_end(skb, nlh);
5141 /* not a multi message or no progress mean a real error */
5142 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5143 nlmsg_cancel(skb, nlh);
5145 nlmsg_end(skb, nlh);
5150 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5155 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5156 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5158 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5159 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5160 int attr = IFLA_STATS_LINK_XSTATS;
5162 if (ops && ops->get_linkxstats_size) {
5163 size += nla_total_size(ops->get_linkxstats_size(dev,
5165 /* for IFLA_STATS_LINK_XSTATS */
5166 size += nla_total_size(0);
5170 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5171 struct net_device *_dev = (struct net_device *)dev;
5172 const struct rtnl_link_ops *ops = NULL;
5173 const struct net_device *master;
5175 /* netdev_master_upper_dev_get can't take const */
5176 master = netdev_master_upper_dev_get(_dev);
5178 ops = master->rtnl_link_ops;
5179 if (ops && ops->get_linkxstats_size) {
5180 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5182 size += nla_total_size(ops->get_linkxstats_size(dev,
5184 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5185 size += nla_total_size(0);
5189 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5190 size += rtnl_get_offload_stats_size(dev);
5192 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5193 struct rtnl_af_ops *af_ops;
5195 /* for IFLA_STATS_AF_SPEC */
5196 size += nla_total_size(0);
5199 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5200 if (af_ops->get_stats_af_size) {
5201 size += nla_total_size(
5202 af_ops->get_stats_af_size(dev));
5205 size += nla_total_size(0);
5214 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5215 bool is_dump, struct netlink_ext_ack *extack)
5217 struct if_stats_msg *ifsm;
5219 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5220 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5227 ifsm = nlmsg_data(nlh);
5229 /* only requests using strict checks can pass data to influence
5230 * the dump. The legacy exception is filter_mask.
5232 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5233 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5236 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5237 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5240 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5241 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5248 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5249 struct netlink_ext_ack *extack)
5251 struct net *net = sock_net(skb->sk);
5252 struct net_device *dev = NULL;
5253 int idxattr = 0, prividx = 0;
5254 struct if_stats_msg *ifsm;
5255 struct sk_buff *nskb;
5259 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5264 ifsm = nlmsg_data(nlh);
5265 if (ifsm->ifindex > 0)
5266 dev = __dev_get_by_index(net, ifsm->ifindex);
5273 filter_mask = ifsm->filter_mask;
5277 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5281 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5282 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5283 0, filter_mask, &idxattr, &prividx);
5285 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5286 WARN_ON(err == -EMSGSIZE);
5289 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5295 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5297 struct netlink_ext_ack *extack = cb->extack;
5298 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5299 struct net *net = sock_net(skb->sk);
5300 unsigned int flags = NLM_F_MULTI;
5301 struct if_stats_msg *ifsm;
5302 struct hlist_head *head;
5303 struct net_device *dev;
5304 u32 filter_mask = 0;
5308 s_idx = cb->args[1];
5309 s_idxattr = cb->args[2];
5310 s_prividx = cb->args[3];
5312 cb->seq = net->dev_base_seq;
5314 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5318 ifsm = nlmsg_data(cb->nlh);
5319 filter_mask = ifsm->filter_mask;
5321 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5325 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5327 head = &net->dev_index_head[h];
5328 hlist_for_each_entry(dev, head, index_hlist) {
5331 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5332 NETLINK_CB(cb->skb).portid,
5333 cb->nlh->nlmsg_seq, 0,
5335 &s_idxattr, &s_prividx);
5336 /* If we ran out of room on the first message,
5339 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5345 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5351 cb->args[3] = s_prividx;
5352 cb->args[2] = s_idxattr;
5359 /* Process one rtnetlink message. */
5361 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5362 struct netlink_ext_ack *extack)
5364 struct net *net = sock_net(skb->sk);
5365 struct rtnl_link *link;
5366 struct module *owner;
5367 int err = -EOPNOTSUPP;
5368 rtnl_doit_func doit;
5374 type = nlh->nlmsg_type;
5380 /* All the messages must have at least 1 byte length */
5381 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5384 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5387 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5391 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5393 rtnl_dumpit_func dumpit;
5394 u16 min_dump_alloc = 0;
5396 link = rtnl_get_link(family, type);
5397 if (!link || !link->dumpit) {
5399 link = rtnl_get_link(family, type);
5400 if (!link || !link->dumpit)
5403 owner = link->owner;
5404 dumpit = link->dumpit;
5406 if (type == RTM_GETLINK - RTM_BASE)
5407 min_dump_alloc = rtnl_calcit(skb, nlh);
5410 /* need to do this before rcu_read_unlock() */
5411 if (!try_module_get(owner))
5412 err = -EPROTONOSUPPORT;
5418 struct netlink_dump_control c = {
5420 .min_dump_alloc = min_dump_alloc,
5423 err = netlink_dump_start(rtnl, skb, nlh, &c);
5424 /* netlink_dump_start() will keep a reference on
5425 * module if dump is still in progress.
5432 link = rtnl_get_link(family, type);
5433 if (!link || !link->doit) {
5435 link = rtnl_get_link(PF_UNSPEC, type);
5436 if (!link || !link->doit)
5440 owner = link->owner;
5441 if (!try_module_get(owner)) {
5442 err = -EPROTONOSUPPORT;
5446 flags = link->flags;
5447 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5451 err = doit(skb, nlh, extack);
5458 link = rtnl_get_link(family, type);
5459 if (link && link->doit)
5460 err = link->doit(skb, nlh, extack);
5476 static void rtnetlink_rcv(struct sk_buff *skb)
5478 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5481 static int rtnetlink_bind(struct net *net, int group)
5484 case RTNLGRP_IPV4_MROUTE_R:
5485 case RTNLGRP_IPV6_MROUTE_R:
5486 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5493 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5495 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5499 case NETDEV_CHANGEMTU:
5500 case NETDEV_CHANGEADDR:
5501 case NETDEV_CHANGENAME:
5502 case NETDEV_FEAT_CHANGE:
5503 case NETDEV_BONDING_FAILOVER:
5504 case NETDEV_POST_TYPE_CHANGE:
5505 case NETDEV_NOTIFY_PEERS:
5506 case NETDEV_CHANGEUPPER:
5507 case NETDEV_RESEND_IGMP:
5508 case NETDEV_CHANGEINFODATA:
5509 case NETDEV_CHANGELOWERSTATE:
5510 case NETDEV_CHANGE_TX_QUEUE_LEN:
5511 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5512 GFP_KERNEL, NULL, 0);
5520 static struct notifier_block rtnetlink_dev_notifier = {
5521 .notifier_call = rtnetlink_event,
5525 static int __net_init rtnetlink_net_init(struct net *net)
5528 struct netlink_kernel_cfg cfg = {
5529 .groups = RTNLGRP_MAX,
5530 .input = rtnetlink_rcv,
5531 .cb_mutex = &rtnl_mutex,
5532 .flags = NL_CFG_F_NONROOT_RECV,
5533 .bind = rtnetlink_bind,
5536 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5543 static void __net_exit rtnetlink_net_exit(struct net *net)
5545 netlink_kernel_release(net->rtnl);
5549 static struct pernet_operations rtnetlink_net_ops = {
5550 .init = rtnetlink_net_init,
5551 .exit = rtnetlink_net_exit,
5554 void __init rtnetlink_init(void)
5556 if (register_pernet_subsys(&rtnetlink_net_ops))
5557 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5559 register_netdevice_notifier(&rtnetlink_dev_notifier);
5561 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5562 rtnl_dump_ifinfo, 0);
5563 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5564 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5565 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5567 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5568 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5569 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5571 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5572 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5574 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5575 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5576 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5578 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5579 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5580 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5582 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,