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_UNKNOWN) &&
834 operstate = IF_OPER_UP;
837 case IF_OPER_DORMANT:
838 if (operstate == IF_OPER_UP ||
839 operstate == IF_OPER_UNKNOWN)
840 operstate = IF_OPER_DORMANT;
844 if (dev->operstate != operstate) {
845 write_lock_bh(&dev_base_lock);
846 dev->operstate = operstate;
847 write_unlock_bh(&dev_base_lock);
848 netdev_state_change(dev);
852 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
854 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
858 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
859 const struct ifinfomsg *ifm)
861 unsigned int flags = ifm->ifi_flags;
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 flags = (flags & ifm->ifi_change) |
866 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
871 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
872 const struct rtnl_link_stats64 *b)
874 a->rx_packets = b->rx_packets;
875 a->tx_packets = b->tx_packets;
876 a->rx_bytes = b->rx_bytes;
877 a->tx_bytes = b->tx_bytes;
878 a->rx_errors = b->rx_errors;
879 a->tx_errors = b->tx_errors;
880 a->rx_dropped = b->rx_dropped;
881 a->tx_dropped = b->tx_dropped;
883 a->multicast = b->multicast;
884 a->collisions = b->collisions;
886 a->rx_length_errors = b->rx_length_errors;
887 a->rx_over_errors = b->rx_over_errors;
888 a->rx_crc_errors = b->rx_crc_errors;
889 a->rx_frame_errors = b->rx_frame_errors;
890 a->rx_fifo_errors = b->rx_fifo_errors;
891 a->rx_missed_errors = b->rx_missed_errors;
893 a->tx_aborted_errors = b->tx_aborted_errors;
894 a->tx_carrier_errors = b->tx_carrier_errors;
895 a->tx_fifo_errors = b->tx_fifo_errors;
896 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897 a->tx_window_errors = b->tx_window_errors;
899 a->rx_compressed = b->rx_compressed;
900 a->tx_compressed = b->tx_compressed;
902 a->rx_nohandler = b->rx_nohandler;
906 static inline int rtnl_vfinfo_size(const struct net_device *dev,
909 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
910 int num_vfs = dev_num_vf(dev->dev.parent);
911 size_t size = nla_total_size(0);
914 nla_total_size(sizeof(struct ifla_vf_mac)) +
915 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
922 nla_total_size(sizeof(struct ifla_vf_rate)) +
923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
925 nla_total_size(0) + /* nest IFLA_VF_STATS */
926 /* IFLA_VF_STATS_RX_PACKETS */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_TX_PACKETS */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_RX_BYTES */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_TX_BYTES */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_BROADCAST */
935 nla_total_size_64bit(sizeof(__u64)) +
936 /* IFLA_VF_STATS_MULTICAST */
937 nla_total_size_64bit(sizeof(__u64)) +
938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
942 nla_total_size(sizeof(struct ifla_vf_trust)));
948 static size_t rtnl_port_size(const struct net_device *dev,
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
970 return port_self_size;
973 static size_t rtnl_xdp_size(void)
975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
976 nla_total_size(1) + /* XDP_ATTACHED */
977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
983 static size_t rtnl_prop_list_size(const struct net_device *dev)
985 struct netdev_name_node *name_node;
988 if (list_empty(&dev->name_node->list))
990 size = nla_total_size(0);
991 list_for_each_entry(name_node, &dev->name_node->list, list)
992 size += nla_total_size(ALTIFNAMSIZ);
996 static noinline size_t if_nlmsg_size(const struct net_device *dev,
999 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1000 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1001 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1002 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1003 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1004 + nla_total_size(sizeof(struct rtnl_link_stats))
1005 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1006 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1007 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1008 + nla_total_size(4) /* IFLA_TXQLEN */
1009 + nla_total_size(4) /* IFLA_WEIGHT */
1010 + nla_total_size(4) /* IFLA_MTU */
1011 + nla_total_size(4) /* IFLA_LINK */
1012 + nla_total_size(4) /* IFLA_MASTER */
1013 + nla_total_size(1) /* IFLA_CARRIER */
1014 + nla_total_size(4) /* IFLA_PROMISCUITY */
1015 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1016 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1017 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1018 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1019 + nla_total_size(1) /* IFLA_OPERSTATE */
1020 + nla_total_size(1) /* IFLA_LINKMODE */
1021 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1022 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1023 + nla_total_size(4) /* IFLA_GROUP */
1024 + nla_total_size(ext_filter_mask
1025 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1026 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1027 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1028 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1029 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1030 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1031 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1032 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1033 + rtnl_xdp_size() /* IFLA_XDP */
1034 + nla_total_size(4) /* IFLA_EVENT */
1035 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1036 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1037 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1038 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1039 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1040 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1041 + nla_total_size(4) /* IFLA_MIN_MTU */
1042 + nla_total_size(4) /* IFLA_MAX_MTU */
1043 + rtnl_prop_list_size(dev)
1044 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1048 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1050 struct nlattr *vf_ports;
1051 struct nlattr *vf_port;
1055 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1059 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1060 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1062 goto nla_put_failure;
1063 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1064 goto nla_put_failure;
1065 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1066 if (err == -EMSGSIZE)
1067 goto nla_put_failure;
1069 nla_nest_cancel(skb, vf_port);
1072 nla_nest_end(skb, vf_port);
1075 nla_nest_end(skb, vf_ports);
1080 nla_nest_cancel(skb, vf_ports);
1084 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1086 struct nlattr *port_self;
1089 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1093 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1095 nla_nest_cancel(skb, port_self);
1096 return (err == -EMSGSIZE) ? err : 0;
1099 nla_nest_end(skb, port_self);
1104 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1105 u32 ext_filter_mask)
1109 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1110 !(ext_filter_mask & RTEXT_FILTER_VF))
1113 err = rtnl_port_self_fill(skb, dev);
1117 if (dev_num_vf(dev->dev.parent)) {
1118 err = rtnl_vf_ports_fill(skb, dev);
1126 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1129 struct netdev_phys_item_id ppid;
1131 err = dev_get_phys_port_id(dev, &ppid);
1133 if (err == -EOPNOTSUPP)
1138 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1144 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1146 char name[IFNAMSIZ];
1149 err = dev_get_phys_port_name(dev, name, sizeof(name));
1151 if (err == -EOPNOTSUPP)
1156 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1162 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1164 struct netdev_phys_item_id ppid = { };
1167 err = dev_get_port_parent_id(dev, &ppid, false);
1169 if (err == -EOPNOTSUPP)
1174 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1180 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1181 struct net_device *dev)
1183 struct rtnl_link_stats64 *sp;
1184 struct nlattr *attr;
1186 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1187 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1191 sp = nla_data(attr);
1192 dev_get_stats(dev, sp);
1194 attr = nla_reserve(skb, IFLA_STATS,
1195 sizeof(struct rtnl_link_stats));
1199 copy_rtnl_link_stats(nla_data(attr), sp);
1204 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1205 struct net_device *dev,
1207 struct nlattr *vfinfo)
1209 struct ifla_vf_rss_query_en vf_rss_query_en;
1210 struct nlattr *vf, *vfstats, *vfvlanlist;
1211 struct ifla_vf_link_state vf_linkstate;
1212 struct ifla_vf_vlan_info vf_vlan_info;
1213 struct ifla_vf_spoofchk vf_spoofchk;
1214 struct ifla_vf_tx_rate vf_tx_rate;
1215 struct ifla_vf_stats vf_stats;
1216 struct ifla_vf_trust vf_trust;
1217 struct ifla_vf_vlan vf_vlan;
1218 struct ifla_vf_rate vf_rate;
1219 struct ifla_vf_mac vf_mac;
1220 struct ifla_vf_broadcast vf_broadcast;
1221 struct ifla_vf_info ivi;
1222 struct ifla_vf_guid node_guid;
1223 struct ifla_vf_guid port_guid;
1225 memset(&ivi, 0, sizeof(ivi));
1227 /* Not all SR-IOV capable drivers support the
1228 * spoofcheck and "RSS query enable" query. Preset to
1229 * -1 so the user space tool can detect that the driver
1230 * didn't report anything.
1233 ivi.rss_query_en = -1;
1235 /* The default value for VF link state is "auto"
1236 * IFLA_VF_LINK_STATE_AUTO which equals zero
1239 /* VLAN Protocol by default is 802.1Q */
1240 ivi.vlan_proto = htons(ETH_P_8021Q);
1241 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1244 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1245 memset(&node_guid, 0, sizeof(node_guid));
1246 memset(&port_guid, 0, sizeof(port_guid));
1255 vf_rss_query_en.vf =
1258 port_guid.vf = ivi.vf;
1260 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1261 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1262 vf_vlan.vlan = ivi.vlan;
1263 vf_vlan.qos = ivi.qos;
1264 vf_vlan_info.vlan = ivi.vlan;
1265 vf_vlan_info.qos = ivi.qos;
1266 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1267 vf_tx_rate.rate = ivi.max_tx_rate;
1268 vf_rate.min_tx_rate = ivi.min_tx_rate;
1269 vf_rate.max_tx_rate = ivi.max_tx_rate;
1270 vf_spoofchk.setting = ivi.spoofchk;
1271 vf_linkstate.link_state = ivi.linkstate;
1272 vf_rss_query_en.setting = ivi.rss_query_en;
1273 vf_trust.setting = ivi.trusted;
1274 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1276 goto nla_put_vfinfo_failure;
1277 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1278 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1279 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1280 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1282 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1284 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1286 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1288 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1289 sizeof(vf_rss_query_en),
1290 &vf_rss_query_en) ||
1291 nla_put(skb, IFLA_VF_TRUST,
1292 sizeof(vf_trust), &vf_trust))
1293 goto nla_put_vf_failure;
1295 if (dev->netdev_ops->ndo_get_vf_guid &&
1296 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1298 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1300 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1302 goto nla_put_vf_failure;
1304 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1306 goto nla_put_vf_failure;
1307 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1309 nla_nest_cancel(skb, vfvlanlist);
1310 goto nla_put_vf_failure;
1312 nla_nest_end(skb, vfvlanlist);
1313 memset(&vf_stats, 0, sizeof(vf_stats));
1314 if (dev->netdev_ops->ndo_get_vf_stats)
1315 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1317 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1319 goto nla_put_vf_failure;
1320 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1321 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1322 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1323 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1324 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1325 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1326 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1327 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1328 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1329 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1330 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1331 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1332 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1333 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1334 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1335 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1336 nla_nest_cancel(skb, vfstats);
1337 goto nla_put_vf_failure;
1339 nla_nest_end(skb, vfstats);
1340 nla_nest_end(skb, vf);
1344 nla_nest_cancel(skb, vf);
1345 nla_put_vfinfo_failure:
1346 nla_nest_cancel(skb, vfinfo);
1350 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1351 struct net_device *dev,
1352 u32 ext_filter_mask)
1354 struct nlattr *vfinfo;
1357 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1360 num_vfs = dev_num_vf(dev->dev.parent);
1361 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1364 if (!dev->netdev_ops->ndo_get_vf_config)
1367 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1371 for (i = 0; i < num_vfs; i++) {
1372 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1376 nla_nest_end(skb, vfinfo);
1380 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1382 struct rtnl_link_ifmap map;
1384 memset(&map, 0, sizeof(map));
1385 map.mem_start = dev->mem_start;
1386 map.mem_end = dev->mem_end;
1387 map.base_addr = dev->base_addr;
1390 map.port = dev->if_port;
1392 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1398 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1400 const struct bpf_prog *generic_xdp_prog;
1404 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1405 if (!generic_xdp_prog)
1407 return generic_xdp_prog->aux->id;
1410 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1412 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1415 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1417 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1421 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1422 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1423 u32 (*get_prog_id)(struct net_device *dev))
1428 curr_id = get_prog_id(dev);
1433 err = nla_put_u32(skb, attr, curr_id);
1437 if (*mode != XDP_ATTACHED_NONE)
1438 *mode = XDP_ATTACHED_MULTI;
1445 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1452 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1457 mode = XDP_ATTACHED_NONE;
1458 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1459 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1462 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1463 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1466 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1467 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1471 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1475 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1476 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1481 nla_nest_end(skb, xdp);
1485 nla_nest_cancel(skb, xdp);
1489 static u32 rtnl_get_event(unsigned long event)
1491 u32 rtnl_event_type = IFLA_EVENT_NONE;
1495 rtnl_event_type = IFLA_EVENT_REBOOT;
1497 case NETDEV_FEAT_CHANGE:
1498 rtnl_event_type = IFLA_EVENT_FEATURES;
1500 case NETDEV_BONDING_FAILOVER:
1501 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1503 case NETDEV_NOTIFY_PEERS:
1504 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1506 case NETDEV_RESEND_IGMP:
1507 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1509 case NETDEV_CHANGEINFODATA:
1510 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1516 return rtnl_event_type;
1519 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1521 const struct net_device *upper_dev;
1526 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1528 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1534 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1537 int ifindex = dev_get_iflink(dev);
1539 if (force || dev->ifindex != ifindex)
1540 return nla_put_u32(skb, IFLA_LINK, ifindex);
1545 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1546 struct net_device *dev)
1551 ret = dev_get_alias(dev, buf, sizeof(buf));
1552 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1555 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1556 const struct net_device *dev,
1557 struct net *src_net, gfp_t gfp)
1559 bool put_iflink = false;
1561 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1562 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1564 if (!net_eq(dev_net(dev), link_net)) {
1565 int id = peernet2id_alloc(src_net, link_net, gfp);
1567 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1574 return nla_put_iflink(skb, dev, put_iflink);
1577 static int rtnl_fill_link_af(struct sk_buff *skb,
1578 const struct net_device *dev,
1579 u32 ext_filter_mask)
1581 const struct rtnl_af_ops *af_ops;
1582 struct nlattr *af_spec;
1584 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1588 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1592 if (!af_ops->fill_link_af)
1595 af = nla_nest_start_noflag(skb, af_ops->family);
1599 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1601 * Caller may return ENODATA to indicate that there
1602 * was no data to be dumped. This is not an error, it
1603 * means we should trim the attribute header and
1606 if (err == -ENODATA)
1607 nla_nest_cancel(skb, af);
1611 nla_nest_end(skb, af);
1614 nla_nest_end(skb, af_spec);
1618 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1619 const struct net_device *dev)
1621 struct netdev_name_node *name_node;
1624 list_for_each_entry(name_node, &dev->name_node->list, list) {
1625 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1632 static int rtnl_fill_prop_list(struct sk_buff *skb,
1633 const struct net_device *dev)
1635 struct nlattr *prop_list;
1638 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1642 ret = rtnl_fill_alt_ifnames(skb, dev);
1646 nla_nest_end(skb, prop_list);
1650 nla_nest_cancel(skb, prop_list);
1654 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1655 struct net_device *dev, struct net *src_net,
1656 int type, u32 pid, u32 seq, u32 change,
1657 unsigned int flags, u32 ext_filter_mask,
1658 u32 event, int *new_nsid, int new_ifindex,
1659 int tgt_netnsid, gfp_t gfp)
1661 struct ifinfomsg *ifm;
1662 struct nlmsghdr *nlh;
1665 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1669 ifm = nlmsg_data(nlh);
1670 ifm->ifi_family = AF_UNSPEC;
1672 ifm->ifi_type = dev->type;
1673 ifm->ifi_index = dev->ifindex;
1674 ifm->ifi_flags = dev_get_flags(dev);
1675 ifm->ifi_change = change;
1677 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1678 goto nla_put_failure;
1680 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1681 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1682 nla_put_u8(skb, IFLA_OPERSTATE,
1683 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1684 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1685 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1686 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1687 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1688 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1689 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1690 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1691 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1692 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1694 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1696 put_master_ifindex(skb, dev) ||
1697 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1699 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1700 nla_put_ifalias(skb, dev) ||
1701 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1702 atomic_read(&dev->carrier_up_count) +
1703 atomic_read(&dev->carrier_down_count)) ||
1704 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1705 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1706 atomic_read(&dev->carrier_up_count)) ||
1707 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1708 atomic_read(&dev->carrier_down_count)))
1709 goto nla_put_failure;
1711 if (event != IFLA_EVENT_NONE) {
1712 if (nla_put_u32(skb, IFLA_EVENT, event))
1713 goto nla_put_failure;
1716 if (rtnl_fill_link_ifmap(skb, dev))
1717 goto nla_put_failure;
1719 if (dev->addr_len) {
1720 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1721 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1722 goto nla_put_failure;
1725 if (rtnl_phys_port_id_fill(skb, dev))
1726 goto nla_put_failure;
1728 if (rtnl_phys_port_name_fill(skb, dev))
1729 goto nla_put_failure;
1731 if (rtnl_phys_switch_id_fill(skb, dev))
1732 goto nla_put_failure;
1734 if (rtnl_fill_stats(skb, dev))
1735 goto nla_put_failure;
1737 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1738 goto nla_put_failure;
1740 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1741 goto nla_put_failure;
1743 if (rtnl_xdp_fill(skb, dev))
1744 goto nla_put_failure;
1746 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1747 if (rtnl_link_fill(skb, dev) < 0)
1748 goto nla_put_failure;
1751 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1752 goto nla_put_failure;
1755 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1756 goto nla_put_failure;
1758 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1759 goto nla_put_failure;
1761 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1762 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1763 goto nla_put_failure;
1766 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1767 goto nla_put_failure_rcu;
1770 if (rtnl_fill_prop_list(skb, dev))
1771 goto nla_put_failure;
1773 nlmsg_end(skb, nlh);
1776 nla_put_failure_rcu:
1779 nlmsg_cancel(skb, nlh);
1783 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1784 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1785 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1786 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1787 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1788 [IFLA_MTU] = { .type = NLA_U32 },
1789 [IFLA_LINK] = { .type = NLA_U32 },
1790 [IFLA_MASTER] = { .type = NLA_U32 },
1791 [IFLA_CARRIER] = { .type = NLA_U8 },
1792 [IFLA_TXQLEN] = { .type = NLA_U32 },
1793 [IFLA_WEIGHT] = { .type = NLA_U32 },
1794 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1795 [IFLA_LINKMODE] = { .type = NLA_U8 },
1796 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1797 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1798 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1799 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1800 * allow 0-length string (needed to remove an alias).
1802 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1803 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1804 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1805 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1806 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1807 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1808 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1809 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1810 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1811 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1812 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1813 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1814 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1815 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1816 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1817 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1818 [IFLA_XDP] = { .type = NLA_NESTED },
1819 [IFLA_EVENT] = { .type = NLA_U32 },
1820 [IFLA_GROUP] = { .type = NLA_U32 },
1821 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1822 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1823 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1824 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1825 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1826 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1827 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1828 .len = ALTIFNAMSIZ - 1 },
1829 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1832 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1833 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1834 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1835 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1836 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1839 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1840 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1841 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1842 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1843 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1844 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1845 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1846 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1847 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1848 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1849 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1850 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1851 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1852 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1855 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1856 [IFLA_PORT_VF] = { .type = NLA_U32 },
1857 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1858 .len = PORT_PROFILE_MAX },
1859 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1860 .len = PORT_UUID_MAX },
1861 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1862 .len = PORT_UUID_MAX },
1863 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1864 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1866 /* Unused, but we need to keep it here since user space could
1867 * fill it. It's also broken with regard to NLA_BINARY use in
1868 * combination with structs.
1870 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1871 .len = sizeof(struct ifla_port_vsi) },
1874 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1875 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1876 [IFLA_XDP_FD] = { .type = NLA_S32 },
1877 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
1878 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1879 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1880 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1883 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1885 const struct rtnl_link_ops *ops = NULL;
1886 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1888 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1891 if (linfo[IFLA_INFO_KIND]) {
1892 char kind[MODULE_NAME_LEN];
1894 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1895 ops = rtnl_link_ops_get(kind);
1901 static bool link_master_filtered(struct net_device *dev, int master_idx)
1903 struct net_device *master;
1908 master = netdev_master_upper_dev_get(dev);
1909 if (!master || master->ifindex != master_idx)
1915 static bool link_kind_filtered(const struct net_device *dev,
1916 const struct rtnl_link_ops *kind_ops)
1918 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1924 static bool link_dump_filtered(struct net_device *dev,
1926 const struct rtnl_link_ops *kind_ops)
1928 if (link_master_filtered(dev, master_idx) ||
1929 link_kind_filtered(dev, kind_ops))
1936 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1937 * @sk: netlink socket
1938 * @netnsid: network namespace identifier
1940 * Returns the network namespace identified by netnsid on success or an error
1941 * pointer on failure.
1943 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1947 net = get_net_ns_by_id(sock_net(sk), netnsid);
1949 return ERR_PTR(-EINVAL);
1951 /* For now, the caller is required to have CAP_NET_ADMIN in
1952 * the user namespace owning the target net ns.
1954 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1956 return ERR_PTR(-EACCES);
1960 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1962 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1963 bool strict_check, struct nlattr **tb,
1964 struct netlink_ext_ack *extack)
1969 struct ifinfomsg *ifm;
1971 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1972 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1976 ifm = nlmsg_data(nlh);
1977 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1979 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1982 if (ifm->ifi_index) {
1983 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1987 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1988 IFLA_MAX, ifla_policy,
1992 /* A hack to preserve kernel<->userspace interface.
1993 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1994 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1995 * what iproute2 < v3.9.0 used.
1996 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1997 * attribute, its netlink message is shorter than struct ifinfomsg.
1999 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2000 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2002 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2006 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2008 struct netlink_ext_ack *extack = cb->extack;
2009 const struct nlmsghdr *nlh = cb->nlh;
2010 struct net *net = sock_net(skb->sk);
2011 struct net *tgt_net = net;
2014 struct net_device *dev;
2015 struct hlist_head *head;
2016 struct nlattr *tb[IFLA_MAX+1];
2017 u32 ext_filter_mask = 0;
2018 const struct rtnl_link_ops *kind_ops = NULL;
2019 unsigned int flags = NLM_F_MULTI;
2025 s_idx = cb->args[1];
2027 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2029 if (cb->strict_check)
2035 for (i = 0; i <= IFLA_MAX; ++i) {
2039 /* new attributes should only be added with strict checking */
2041 case IFLA_TARGET_NETNSID:
2042 netnsid = nla_get_s32(tb[i]);
2043 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2044 if (IS_ERR(tgt_net)) {
2045 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2046 return PTR_ERR(tgt_net);
2050 ext_filter_mask = nla_get_u32(tb[i]);
2053 master_idx = nla_get_u32(tb[i]);
2056 kind_ops = linkinfo_to_kind_ops(tb[i]);
2059 if (cb->strict_check) {
2060 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2066 if (master_idx || kind_ops)
2067 flags |= NLM_F_DUMP_FILTERED;
2070 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2072 head = &tgt_net->dev_index_head[h];
2073 hlist_for_each_entry(dev, head, index_hlist) {
2074 if (link_dump_filtered(dev, master_idx, kind_ops))
2078 err = rtnl_fill_ifinfo(skb, dev, net,
2080 NETLINK_CB(cb->skb).portid,
2081 nlh->nlmsg_seq, 0, flags,
2082 ext_filter_mask, 0, NULL, 0,
2083 netnsid, GFP_KERNEL);
2086 if (likely(skb->len))
2100 cb->seq = net->dev_base_seq;
2101 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2108 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2109 struct netlink_ext_ack *exterr)
2111 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2114 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2116 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2119 /* Examine the link attributes and figure out which
2120 * network namespace we are talking about.
2122 if (tb[IFLA_NET_NS_PID])
2123 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2124 else if (tb[IFLA_NET_NS_FD])
2125 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2127 net = get_net(src_net);
2130 EXPORT_SYMBOL(rtnl_link_get_net);
2132 /* Figure out which network namespace we are talking about by
2133 * examining the link attributes in the following order:
2135 * 1. IFLA_NET_NS_PID
2137 * 3. IFLA_TARGET_NETNSID
2139 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2140 struct nlattr *tb[])
2144 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2145 return rtnl_link_get_net(src_net, tb);
2147 if (!tb[IFLA_TARGET_NETNSID])
2148 return get_net(src_net);
2150 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2152 return ERR_PTR(-EINVAL);
2157 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2158 struct net *src_net,
2159 struct nlattr *tb[], int cap)
2163 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2167 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2169 return ERR_PTR(-EPERM);
2175 /* Verify that rtnetlink requests do not pass additional properties
2176 * potentially referring to different network namespaces.
2178 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2179 struct netlink_ext_ack *extack,
2183 if (netns_id_only) {
2184 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2187 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2191 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2194 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2197 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2203 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2207 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2210 if (tb[IFLA_ADDRESS] &&
2211 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2214 if (tb[IFLA_BROADCAST] &&
2215 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2219 if (tb[IFLA_AF_SPEC]) {
2223 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2224 const struct rtnl_af_ops *af_ops;
2227 af_ops = rtnl_af_lookup(nla_type(af));
2230 return -EAFNOSUPPORT;
2233 if (!af_ops->set_link_af) {
2238 if (af_ops->validate_link_af) {
2239 err = af_ops->validate_link_af(dev, af);
2253 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2256 const struct net_device_ops *ops = dev->netdev_ops;
2258 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2261 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2263 if (dev->type != ARPHRD_INFINIBAND)
2266 return handle_infiniband_guid(dev, ivt, guid_type);
2269 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2271 const struct net_device_ops *ops = dev->netdev_ops;
2274 if (tb[IFLA_VF_MAC]) {
2275 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2277 if (ivm->vf >= INT_MAX)
2280 if (ops->ndo_set_vf_mac)
2281 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2287 if (tb[IFLA_VF_VLAN]) {
2288 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2290 if (ivv->vf >= INT_MAX)
2293 if (ops->ndo_set_vf_vlan)
2294 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2296 htons(ETH_P_8021Q));
2301 if (tb[IFLA_VF_VLAN_LIST]) {
2302 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2303 struct nlattr *attr;
2307 if (!ops->ndo_set_vf_vlan)
2310 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2311 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2312 nla_len(attr) < NLA_HDRLEN) {
2315 if (len >= MAX_VLAN_LIST_LEN)
2317 ivvl[len] = nla_data(attr);
2324 if (ivvl[0]->vf >= INT_MAX)
2326 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2327 ivvl[0]->qos, ivvl[0]->vlan_proto);
2332 if (tb[IFLA_VF_TX_RATE]) {
2333 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2334 struct ifla_vf_info ivf;
2336 if (ivt->vf >= INT_MAX)
2339 if (ops->ndo_get_vf_config)
2340 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2345 if (ops->ndo_set_vf_rate)
2346 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2353 if (tb[IFLA_VF_RATE]) {
2354 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2356 if (ivt->vf >= INT_MAX)
2359 if (ops->ndo_set_vf_rate)
2360 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2367 if (tb[IFLA_VF_SPOOFCHK]) {
2368 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2370 if (ivs->vf >= INT_MAX)
2373 if (ops->ndo_set_vf_spoofchk)
2374 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2380 if (tb[IFLA_VF_LINK_STATE]) {
2381 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2383 if (ivl->vf >= INT_MAX)
2386 if (ops->ndo_set_vf_link_state)
2387 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2393 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2394 struct ifla_vf_rss_query_en *ivrssq_en;
2397 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2398 if (ivrssq_en->vf >= INT_MAX)
2400 if (ops->ndo_set_vf_rss_query_en)
2401 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2402 ivrssq_en->setting);
2407 if (tb[IFLA_VF_TRUST]) {
2408 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2410 if (ivt->vf >= INT_MAX)
2413 if (ops->ndo_set_vf_trust)
2414 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2419 if (tb[IFLA_VF_IB_NODE_GUID]) {
2420 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2422 if (ivt->vf >= INT_MAX)
2424 if (!ops->ndo_set_vf_guid)
2426 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2429 if (tb[IFLA_VF_IB_PORT_GUID]) {
2430 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2432 if (ivt->vf >= INT_MAX)
2434 if (!ops->ndo_set_vf_guid)
2437 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2443 static int do_set_master(struct net_device *dev, int ifindex,
2444 struct netlink_ext_ack *extack)
2446 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2447 const struct net_device_ops *ops;
2451 if (upper_dev->ifindex == ifindex)
2453 ops = upper_dev->netdev_ops;
2454 if (ops->ndo_del_slave) {
2455 err = ops->ndo_del_slave(upper_dev, dev);
2458 netdev_update_lockdep_key(dev);
2465 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2468 ops = upper_dev->netdev_ops;
2469 if (ops->ndo_add_slave) {
2470 err = ops->ndo_add_slave(upper_dev, dev, extack);
2480 #define DO_SETLINK_MODIFIED 0x01
2481 /* notify flag means notify + modified. */
2482 #define DO_SETLINK_NOTIFY 0x03
2483 static int do_setlink(const struct sk_buff *skb,
2484 struct net_device *dev, struct ifinfomsg *ifm,
2485 struct netlink_ext_ack *extack,
2486 struct nlattr **tb, char *ifname, int status)
2488 const struct net_device_ops *ops = dev->netdev_ops;
2491 err = validate_linkmsg(dev, tb);
2495 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2496 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2503 err = dev_change_net_namespace(dev, net, ifname);
2507 status |= DO_SETLINK_MODIFIED;
2511 struct rtnl_link_ifmap *u_map;
2514 if (!ops->ndo_set_config) {
2519 if (!netif_device_present(dev)) {
2524 u_map = nla_data(tb[IFLA_MAP]);
2525 k_map.mem_start = (unsigned long) u_map->mem_start;
2526 k_map.mem_end = (unsigned long) u_map->mem_end;
2527 k_map.base_addr = (unsigned short) u_map->base_addr;
2528 k_map.irq = (unsigned char) u_map->irq;
2529 k_map.dma = (unsigned char) u_map->dma;
2530 k_map.port = (unsigned char) u_map->port;
2532 err = ops->ndo_set_config(dev, &k_map);
2536 status |= DO_SETLINK_NOTIFY;
2539 if (tb[IFLA_ADDRESS]) {
2540 struct sockaddr *sa;
2543 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2545 sa = kmalloc(len, GFP_KERNEL);
2550 sa->sa_family = dev->type;
2551 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2553 err = dev_set_mac_address(dev, sa, extack);
2557 status |= DO_SETLINK_MODIFIED;
2561 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2564 status |= DO_SETLINK_MODIFIED;
2567 if (tb[IFLA_GROUP]) {
2568 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2569 status |= DO_SETLINK_NOTIFY;
2573 * Interface selected by interface index but interface
2574 * name provided implies that a name change has been
2577 if (ifm->ifi_index > 0 && ifname[0]) {
2578 err = dev_change_name(dev, ifname);
2581 status |= DO_SETLINK_MODIFIED;
2584 if (tb[IFLA_IFALIAS]) {
2585 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2586 nla_len(tb[IFLA_IFALIAS]));
2589 status |= DO_SETLINK_NOTIFY;
2592 if (tb[IFLA_BROADCAST]) {
2593 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2594 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2597 if (ifm->ifi_flags || ifm->ifi_change) {
2598 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2604 if (tb[IFLA_MASTER]) {
2605 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2608 status |= DO_SETLINK_MODIFIED;
2611 if (tb[IFLA_CARRIER]) {
2612 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2615 status |= DO_SETLINK_MODIFIED;
2618 if (tb[IFLA_TXQLEN]) {
2619 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2621 err = dev_change_tx_queue_len(dev, value);
2624 status |= DO_SETLINK_MODIFIED;
2627 if (tb[IFLA_GSO_MAX_SIZE]) {
2628 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2630 if (max_size > GSO_MAX_SIZE) {
2635 if (dev->gso_max_size ^ max_size) {
2636 netif_set_gso_max_size(dev, max_size);
2637 status |= DO_SETLINK_MODIFIED;
2641 if (tb[IFLA_GSO_MAX_SEGS]) {
2642 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2644 if (max_segs > GSO_MAX_SEGS) {
2649 if (dev->gso_max_segs ^ max_segs) {
2650 dev->gso_max_segs = max_segs;
2651 status |= DO_SETLINK_MODIFIED;
2655 if (tb[IFLA_OPERSTATE])
2656 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2658 if (tb[IFLA_LINKMODE]) {
2659 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2661 write_lock_bh(&dev_base_lock);
2662 if (dev->link_mode ^ value)
2663 status |= DO_SETLINK_NOTIFY;
2664 dev->link_mode = value;
2665 write_unlock_bh(&dev_base_lock);
2668 if (tb[IFLA_VFINFO_LIST]) {
2669 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2670 struct nlattr *attr;
2673 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2674 if (nla_type(attr) != IFLA_VF_INFO ||
2675 nla_len(attr) < NLA_HDRLEN) {
2679 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2685 err = do_setvfinfo(dev, vfinfo);
2688 status |= DO_SETLINK_NOTIFY;
2693 if (tb[IFLA_VF_PORTS]) {
2694 struct nlattr *port[IFLA_PORT_MAX+1];
2695 struct nlattr *attr;
2700 if (!ops->ndo_set_vf_port)
2703 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2704 if (nla_type(attr) != IFLA_VF_PORT ||
2705 nla_len(attr) < NLA_HDRLEN) {
2709 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2715 if (!port[IFLA_PORT_VF]) {
2719 vf = nla_get_u32(port[IFLA_PORT_VF]);
2720 err = ops->ndo_set_vf_port(dev, vf, port);
2723 status |= DO_SETLINK_NOTIFY;
2728 if (tb[IFLA_PORT_SELF]) {
2729 struct nlattr *port[IFLA_PORT_MAX+1];
2731 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2733 ifla_port_policy, NULL);
2738 if (ops->ndo_set_vf_port)
2739 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2742 status |= DO_SETLINK_NOTIFY;
2745 if (tb[IFLA_AF_SPEC]) {
2749 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2750 const struct rtnl_af_ops *af_ops;
2754 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2756 err = af_ops->set_link_af(dev, af);
2763 status |= DO_SETLINK_NOTIFY;
2768 if (tb[IFLA_PROTO_DOWN]) {
2769 err = dev_change_proto_down(dev,
2770 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2773 status |= DO_SETLINK_NOTIFY;
2777 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2780 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2782 ifla_xdp_policy, NULL);
2786 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2791 if (xdp[IFLA_XDP_FLAGS]) {
2792 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2793 if (xdp_flags & ~XDP_FLAGS_MASK) {
2797 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2803 if (xdp[IFLA_XDP_FD]) {
2804 int expected_fd = -1;
2806 if (xdp_flags & XDP_FLAGS_REPLACE) {
2807 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2812 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2815 err = dev_change_xdp_fd(dev, extack,
2816 nla_get_s32(xdp[IFLA_XDP_FD]),
2821 status |= DO_SETLINK_NOTIFY;
2826 if (status & DO_SETLINK_MODIFIED) {
2827 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2828 netdev_state_change(dev);
2831 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",
2838 static struct net_device *rtnl_dev_get(struct net *net,
2839 struct nlattr *ifname_attr,
2840 struct nlattr *altifname_attr,
2843 char buffer[ALTIFNAMSIZ];
2848 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2849 else if (altifname_attr)
2850 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2855 return __dev_get_by_name(net, ifname);
2858 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2859 struct netlink_ext_ack *extack)
2861 struct net *net = sock_net(skb->sk);
2862 struct ifinfomsg *ifm;
2863 struct net_device *dev;
2865 struct nlattr *tb[IFLA_MAX+1];
2866 char ifname[IFNAMSIZ];
2868 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2869 ifla_policy, extack);
2873 err = rtnl_ensure_unique_netns(tb, extack, false);
2877 if (tb[IFLA_IFNAME])
2878 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2883 ifm = nlmsg_data(nlh);
2884 if (ifm->ifi_index > 0)
2885 dev = __dev_get_by_index(net, ifm->ifi_index);
2886 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2887 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2896 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2901 static int rtnl_group_dellink(const struct net *net, int group)
2903 struct net_device *dev, *aux;
2904 LIST_HEAD(list_kill);
2910 for_each_netdev(net, dev) {
2911 if (dev->group == group) {
2912 const struct rtnl_link_ops *ops;
2915 ops = dev->rtnl_link_ops;
2916 if (!ops || !ops->dellink)
2924 for_each_netdev_safe(net, dev, aux) {
2925 if (dev->group == group) {
2926 const struct rtnl_link_ops *ops;
2928 ops = dev->rtnl_link_ops;
2929 ops->dellink(dev, &list_kill);
2932 unregister_netdevice_many(&list_kill);
2937 int rtnl_delete_link(struct net_device *dev)
2939 const struct rtnl_link_ops *ops;
2940 LIST_HEAD(list_kill);
2942 ops = dev->rtnl_link_ops;
2943 if (!ops || !ops->dellink)
2946 ops->dellink(dev, &list_kill);
2947 unregister_netdevice_many(&list_kill);
2951 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2953 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2954 struct netlink_ext_ack *extack)
2956 struct net *net = sock_net(skb->sk);
2957 struct net *tgt_net = net;
2958 struct net_device *dev = NULL;
2959 struct ifinfomsg *ifm;
2960 struct nlattr *tb[IFLA_MAX+1];
2964 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2965 ifla_policy, extack);
2969 err = rtnl_ensure_unique_netns(tb, extack, true);
2973 if (tb[IFLA_TARGET_NETNSID]) {
2974 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2975 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2976 if (IS_ERR(tgt_net))
2977 return PTR_ERR(tgt_net);
2981 ifm = nlmsg_data(nlh);
2982 if (ifm->ifi_index > 0)
2983 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2984 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2985 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2986 tb[IFLA_ALT_IFNAME], NULL);
2987 else if (tb[IFLA_GROUP])
2988 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2993 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2999 err = rtnl_delete_link(dev);
3008 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3010 unsigned int old_flags;
3013 old_flags = dev->flags;
3014 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3015 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3021 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3022 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3024 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3025 __dev_notify_flags(dev, old_flags, ~0U);
3029 EXPORT_SYMBOL(rtnl_configure_link);
3031 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3032 unsigned char name_assign_type,
3033 const struct rtnl_link_ops *ops,
3034 struct nlattr *tb[],
3035 struct netlink_ext_ack *extack)
3037 struct net_device *dev;
3038 unsigned int num_tx_queues = 1;
3039 unsigned int num_rx_queues = 1;
3041 if (tb[IFLA_NUM_TX_QUEUES])
3042 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3043 else if (ops->get_num_tx_queues)
3044 num_tx_queues = ops->get_num_tx_queues();
3046 if (tb[IFLA_NUM_RX_QUEUES])
3047 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3048 else if (ops->get_num_rx_queues)
3049 num_rx_queues = ops->get_num_rx_queues();
3051 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3052 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3053 return ERR_PTR(-EINVAL);
3056 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3057 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3058 return ERR_PTR(-EINVAL);
3061 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3062 ops->setup, num_tx_queues, num_rx_queues);
3064 return ERR_PTR(-ENOMEM);
3066 dev_net_set(dev, net);
3067 dev->rtnl_link_ops = ops;
3068 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3071 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3074 err = dev_validate_mtu(dev, mtu, extack);
3077 return ERR_PTR(err);
3081 if (tb[IFLA_ADDRESS]) {
3082 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3083 nla_len(tb[IFLA_ADDRESS]));
3084 dev->addr_assign_type = NET_ADDR_SET;
3086 if (tb[IFLA_BROADCAST])
3087 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3088 nla_len(tb[IFLA_BROADCAST]));
3089 if (tb[IFLA_TXQLEN])
3090 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3091 if (tb[IFLA_OPERSTATE])
3092 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3093 if (tb[IFLA_LINKMODE])
3094 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3096 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3097 if (tb[IFLA_GSO_MAX_SIZE])
3098 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3099 if (tb[IFLA_GSO_MAX_SEGS])
3100 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3104 EXPORT_SYMBOL(rtnl_create_link);
3106 static int rtnl_group_changelink(const struct sk_buff *skb,
3107 struct net *net, int group,
3108 struct ifinfomsg *ifm,
3109 struct netlink_ext_ack *extack,
3112 struct net_device *dev, *aux;
3115 for_each_netdev_safe(net, dev, aux) {
3116 if (dev->group == group) {
3117 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3126 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3127 struct nlattr **attr, struct netlink_ext_ack *extack)
3129 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3130 unsigned char name_assign_type = NET_NAME_USER;
3131 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3132 const struct rtnl_link_ops *m_ops = NULL;
3133 struct net_device *master_dev = NULL;
3134 struct net *net = sock_net(skb->sk);
3135 const struct rtnl_link_ops *ops;
3136 struct nlattr *tb[IFLA_MAX + 1];
3137 struct net *dest_net, *link_net;
3138 struct nlattr **slave_data;
3139 char kind[MODULE_NAME_LEN];
3140 struct net_device *dev;
3141 struct ifinfomsg *ifm;
3142 char ifname[IFNAMSIZ];
3143 struct nlattr **data;
3146 #ifdef CONFIG_MODULES
3149 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3150 ifla_policy, extack);
3154 err = rtnl_ensure_unique_netns(tb, extack, false);
3158 if (tb[IFLA_IFNAME])
3159 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3163 ifm = nlmsg_data(nlh);
3164 if (ifm->ifi_index > 0)
3165 dev = __dev_get_by_index(net, ifm->ifi_index);
3166 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3167 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3172 master_dev = netdev_master_upper_dev_get(dev);
3174 m_ops = master_dev->rtnl_link_ops;
3177 err = validate_linkmsg(dev, tb);
3181 if (tb[IFLA_LINKINFO]) {
3182 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3184 ifla_info_policy, NULL);
3188 memset(linkinfo, 0, sizeof(linkinfo));
3190 if (linkinfo[IFLA_INFO_KIND]) {
3191 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3192 ops = rtnl_link_ops_get(kind);
3200 if (ops->maxtype > RTNL_MAX_TYPE)
3203 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3204 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3205 linkinfo[IFLA_INFO_DATA],
3206 ops->policy, extack);
3211 if (ops->validate) {
3212 err = ops->validate(tb, data, extack);
3220 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3223 if (m_ops->slave_maxtype &&
3224 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3225 err = nla_parse_nested_deprecated(slave_attr,
3226 m_ops->slave_maxtype,
3227 linkinfo[IFLA_INFO_SLAVE_DATA],
3228 m_ops->slave_policy,
3232 slave_data = slave_attr;
3239 if (nlh->nlmsg_flags & NLM_F_EXCL)
3241 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3244 if (linkinfo[IFLA_INFO_DATA]) {
3245 if (!ops || ops != dev->rtnl_link_ops ||
3249 err = ops->changelink(dev, tb, data, extack);
3252 status |= DO_SETLINK_NOTIFY;
3255 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3256 if (!m_ops || !m_ops->slave_changelink)
3259 err = m_ops->slave_changelink(master_dev, dev, tb,
3260 slave_data, extack);
3263 status |= DO_SETLINK_NOTIFY;
3266 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3269 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3270 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3271 return rtnl_group_changelink(skb, net,
3272 nla_get_u32(tb[IFLA_GROUP]),
3277 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3281 #ifdef CONFIG_MODULES
3284 request_module("rtnl-link-%s", kind);
3286 ops = rtnl_link_ops_get(kind);
3291 NL_SET_ERR_MSG(extack, "Unknown device type");
3299 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3300 name_assign_type = NET_NAME_ENUM;
3303 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3304 if (IS_ERR(dest_net))
3305 return PTR_ERR(dest_net);
3307 if (tb[IFLA_LINK_NETNSID]) {
3308 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3310 link_net = get_net_ns_by_id(dest_net, id);
3312 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3317 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3323 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3324 name_assign_type, ops, tb, extack);
3330 dev->ifindex = ifm->ifi_index;
3333 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3334 /* Drivers should call free_netdev() in ->destructor
3335 * and unregister it on failure after registration
3336 * so that device could be finally freed in rtnl_unlock.
3339 /* If device is not registered at all, free it now */
3340 if (dev->reg_state == NETREG_UNINITIALIZED)
3345 err = register_netdevice(dev);
3351 err = rtnl_configure_link(dev, ifm);
3353 goto out_unregister;
3355 err = dev_change_net_namespace(dev, dest_net, ifname);
3357 goto out_unregister;
3359 if (tb[IFLA_MASTER]) {
3360 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3362 goto out_unregister;
3371 LIST_HEAD(list_kill);
3373 ops->dellink(dev, &list_kill);
3374 unregister_netdevice_many(&list_kill);
3376 unregister_netdevice(dev);
3381 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3382 struct netlink_ext_ack *extack)
3384 struct nlattr **attr;
3387 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3391 ret = __rtnl_newlink(skb, nlh, attr, extack);
3396 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3397 const struct nlmsghdr *nlh,
3399 struct netlink_ext_ack *extack)
3401 struct ifinfomsg *ifm;
3404 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3405 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3409 if (!netlink_strict_get_check(skb))
3410 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3411 ifla_policy, extack);
3413 ifm = nlmsg_data(nlh);
3414 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3416 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3420 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3421 ifla_policy, extack);
3425 for (i = 0; i <= IFLA_MAX; i++) {
3431 case IFLA_ALT_IFNAME:
3433 case IFLA_TARGET_NETNSID:
3436 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3444 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3445 struct netlink_ext_ack *extack)
3447 struct net *net = sock_net(skb->sk);
3448 struct net *tgt_net = net;
3449 struct ifinfomsg *ifm;
3450 struct nlattr *tb[IFLA_MAX+1];
3451 struct net_device *dev = NULL;
3452 struct sk_buff *nskb;
3455 u32 ext_filter_mask = 0;
3457 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3461 err = rtnl_ensure_unique_netns(tb, extack, true);
3465 if (tb[IFLA_TARGET_NETNSID]) {
3466 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3467 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3468 if (IS_ERR(tgt_net))
3469 return PTR_ERR(tgt_net);
3472 if (tb[IFLA_EXT_MASK])
3473 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3476 ifm = nlmsg_data(nlh);
3477 if (ifm->ifi_index > 0)
3478 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3479 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3480 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3481 tb[IFLA_ALT_IFNAME], NULL);
3490 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3494 err = rtnl_fill_ifinfo(nskb, dev, net,
3495 RTM_NEWLINK, NETLINK_CB(skb).portid,
3496 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3497 0, NULL, 0, netnsid, GFP_KERNEL);
3499 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3500 WARN_ON(err == -EMSGSIZE);
3503 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3511 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3512 bool *changed, struct netlink_ext_ack *extack)
3517 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3521 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3525 if (cmd == RTM_NEWLINKPROP) {
3526 err = netdev_name_node_alt_create(dev, alt_ifname);
3529 } else if (cmd == RTM_DELLINKPROP) {
3530 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3542 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3543 struct netlink_ext_ack *extack)
3545 struct net *net = sock_net(skb->sk);
3546 struct nlattr *tb[IFLA_MAX + 1];
3547 struct net_device *dev;
3548 struct ifinfomsg *ifm;
3549 bool changed = false;
3550 struct nlattr *attr;
3553 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3557 err = rtnl_ensure_unique_netns(tb, extack, true);
3561 ifm = nlmsg_data(nlh);
3562 if (ifm->ifi_index > 0)
3563 dev = __dev_get_by_index(net, ifm->ifi_index);
3564 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3565 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3566 tb[IFLA_ALT_IFNAME], NULL);
3573 if (!tb[IFLA_PROP_LIST])
3576 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3577 switch (nla_type(attr)) {
3578 case IFLA_ALT_IFNAME:
3579 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3587 netdev_state_change(dev);
3591 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3592 struct netlink_ext_ack *extack)
3594 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3597 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3598 struct netlink_ext_ack *extack)
3600 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3603 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3605 struct net *net = sock_net(skb->sk);
3606 struct net_device *dev;
3607 struct nlattr *tb[IFLA_MAX+1];
3608 u32 ext_filter_mask = 0;
3609 u16 min_ifinfo_dump_size = 0;
3612 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3613 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3614 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3616 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3617 if (tb[IFLA_EXT_MASK])
3618 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3621 if (!ext_filter_mask)
3622 return NLMSG_GOODSIZE;
3624 * traverse the list of net devices and compute the minimum
3625 * buffer size based upon the filter mask.
3628 for_each_netdev_rcu(net, dev) {
3629 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3635 return nlmsg_total_size(min_ifinfo_dump_size);
3638 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3641 int s_idx = cb->family;
3642 int type = cb->nlh->nlmsg_type - RTM_BASE;
3648 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3649 struct rtnl_link **tab;
3650 struct rtnl_link *link;
3651 rtnl_dumpit_func dumpit;
3653 if (idx < s_idx || idx == PF_PACKET)
3656 if (type < 0 || type >= RTM_NR_MSGTYPES)
3659 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3667 dumpit = link->dumpit;
3672 memset(&cb->args[0], 0, sizeof(cb->args));
3676 ret = dumpit(skb, cb);
3682 return skb->len ? : ret;
3685 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3686 unsigned int change,
3687 u32 event, gfp_t flags, int *new_nsid,
3690 struct net *net = dev_net(dev);
3691 struct sk_buff *skb;
3693 size_t if_info_size;
3695 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3699 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3700 type, 0, 0, change, 0, 0, event,
3701 new_nsid, new_ifindex, -1, flags);
3703 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3704 WARN_ON(err == -EMSGSIZE);
3711 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3715 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3717 struct net *net = dev_net(dev);
3719 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3722 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3723 unsigned int change, u32 event,
3724 gfp_t flags, int *new_nsid, int new_ifindex)
3726 struct sk_buff *skb;
3728 if (dev->reg_state != NETREG_REGISTERED)
3731 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3734 rtmsg_ifinfo_send(skb, dev, flags);
3737 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3740 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3744 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3745 gfp_t flags, int *new_nsid, int new_ifindex)
3747 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3748 new_nsid, new_ifindex);
3751 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3752 struct net_device *dev,
3753 u8 *addr, u16 vid, u32 pid, u32 seq,
3754 int type, unsigned int flags,
3755 int nlflags, u16 ndm_state)
3757 struct nlmsghdr *nlh;
3760 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3764 ndm = nlmsg_data(nlh);
3765 ndm->ndm_family = AF_BRIDGE;
3768 ndm->ndm_flags = flags;
3770 ndm->ndm_ifindex = dev->ifindex;
3771 ndm->ndm_state = ndm_state;
3773 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3774 goto nla_put_failure;
3776 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3777 goto nla_put_failure;
3779 nlmsg_end(skb, nlh);
3783 nlmsg_cancel(skb, nlh);
3787 static inline size_t rtnl_fdb_nlmsg_size(void)
3789 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3790 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3791 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3795 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3798 struct net *net = dev_net(dev);
3799 struct sk_buff *skb;
3802 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3806 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3807 0, 0, type, NTF_SELF, 0, ndm_state);
3813 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3816 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3820 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3822 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3823 struct nlattr *tb[],
3824 struct net_device *dev,
3825 const unsigned char *addr, u16 vid,
3830 /* If aging addresses are supported device will need to
3831 * implement its own handler for this.
3833 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3834 pr_info("%s: FDB only supports static addresses\n", dev->name);
3839 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3843 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3844 err = dev_uc_add_excl(dev, addr);
3845 else if (is_multicast_ether_addr(addr))
3846 err = dev_mc_add_excl(dev, addr);
3848 /* Only return duplicate errors if NLM_F_EXCL is set */
3849 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3854 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3856 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3857 struct netlink_ext_ack *extack)
3862 if (nla_len(vlan_attr) != sizeof(u16)) {
3863 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3867 vid = nla_get_u16(vlan_attr);
3869 if (!vid || vid >= VLAN_VID_MASK) {
3870 NL_SET_ERR_MSG(extack, "invalid vlan id");
3878 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3879 struct netlink_ext_ack *extack)
3881 struct net *net = sock_net(skb->sk);
3883 struct nlattr *tb[NDA_MAX+1];
3884 struct net_device *dev;
3889 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3894 ndm = nlmsg_data(nlh);
3895 if (ndm->ndm_ifindex == 0) {
3896 NL_SET_ERR_MSG(extack, "invalid ifindex");
3900 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3902 NL_SET_ERR_MSG(extack, "unknown ifindex");
3906 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3907 NL_SET_ERR_MSG(extack, "invalid address");
3911 if (dev->type != ARPHRD_ETHER) {
3912 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3916 addr = nla_data(tb[NDA_LLADDR]);
3918 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3924 /* Support fdb on master device the net/bridge default case */
3925 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3926 netif_is_bridge_port(dev)) {
3927 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3928 const struct net_device_ops *ops = br_dev->netdev_ops;
3930 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3931 nlh->nlmsg_flags, extack);
3935 ndm->ndm_flags &= ~NTF_MASTER;
3938 /* Embedded bridge, macvlan, and any other device support */
3939 if ((ndm->ndm_flags & NTF_SELF)) {
3940 if (dev->netdev_ops->ndo_fdb_add)
3941 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3946 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3950 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3952 ndm->ndm_flags &= ~NTF_SELF;
3960 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3962 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3963 struct nlattr *tb[],
3964 struct net_device *dev,
3965 const unsigned char *addr, u16 vid)
3969 /* If aging addresses are supported device will need to
3970 * implement its own handler for this.
3972 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3973 pr_info("%s: FDB only supports static addresses\n", dev->name);
3977 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3978 err = dev_uc_del(dev, addr);
3979 else if (is_multicast_ether_addr(addr))
3980 err = dev_mc_del(dev, addr);
3984 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3986 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3987 struct netlink_ext_ack *extack)
3989 struct net *net = sock_net(skb->sk);
3991 struct nlattr *tb[NDA_MAX+1];
3992 struct net_device *dev;
3997 if (!netlink_capable(skb, CAP_NET_ADMIN))
4000 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4005 ndm = nlmsg_data(nlh);
4006 if (ndm->ndm_ifindex == 0) {
4007 NL_SET_ERR_MSG(extack, "invalid ifindex");
4011 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4013 NL_SET_ERR_MSG(extack, "unknown ifindex");
4017 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4018 NL_SET_ERR_MSG(extack, "invalid address");
4022 if (dev->type != ARPHRD_ETHER) {
4023 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4027 addr = nla_data(tb[NDA_LLADDR]);
4029 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4035 /* Support fdb on master device the net/bridge default case */
4036 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4037 netif_is_bridge_port(dev)) {
4038 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4039 const struct net_device_ops *ops = br_dev->netdev_ops;
4041 if (ops->ndo_fdb_del)
4042 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4047 ndm->ndm_flags &= ~NTF_MASTER;
4050 /* Embedded bridge, macvlan, and any other device support */
4051 if (ndm->ndm_flags & NTF_SELF) {
4052 if (dev->netdev_ops->ndo_fdb_del)
4053 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4056 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4059 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4061 ndm->ndm_flags &= ~NTF_SELF;
4068 static int nlmsg_populate_fdb(struct sk_buff *skb,
4069 struct netlink_callback *cb,
4070 struct net_device *dev,
4072 struct netdev_hw_addr_list *list)
4074 struct netdev_hw_addr *ha;
4078 portid = NETLINK_CB(cb->skb).portid;
4079 seq = cb->nlh->nlmsg_seq;
4081 list_for_each_entry(ha, &list->list, list) {
4082 if (*idx < cb->args[2])
4085 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4087 RTM_NEWNEIGH, NTF_SELF,
4088 NLM_F_MULTI, NUD_PERMANENT);
4098 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4099 * @skb: socket buffer to store message in
4100 * @cb: netlink callback
4102 * @filter_dev: ignored
4103 * @idx: the number of FDB table entries dumped is added to *@idx
4105 * Default netdevice operation to dump the existing unicast address list.
4106 * Returns number of addresses from list put in skb.
4108 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4109 struct netlink_callback *cb,
4110 struct net_device *dev,
4111 struct net_device *filter_dev,
4116 if (dev->type != ARPHRD_ETHER)
4119 netif_addr_lock_bh(dev);
4120 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4123 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4125 netif_addr_unlock_bh(dev);
4128 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4130 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4131 int *br_idx, int *brport_idx,
4132 struct netlink_ext_ack *extack)
4134 struct nlattr *tb[NDA_MAX + 1];
4138 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4139 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4143 ndm = nlmsg_data(nlh);
4144 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4145 ndm->ndm_flags || ndm->ndm_type) {
4146 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4150 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4151 NDA_MAX, NULL, extack);
4155 *brport_idx = ndm->ndm_ifindex;
4156 for (i = 0; i <= NDA_MAX; ++i) {
4162 if (nla_len(tb[i]) != sizeof(u32)) {
4163 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4166 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4169 if (nla_len(tb[i]) != sizeof(u32)) {
4170 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4173 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4176 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4184 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4185 int *br_idx, int *brport_idx,
4186 struct netlink_ext_ack *extack)
4188 struct nlattr *tb[IFLA_MAX+1];
4191 /* A hack to preserve kernel<->userspace interface.
4192 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4193 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4194 * So, check for ndmsg with an optional u32 attribute (not used here).
4195 * Fortunately these sizes don't conflict with the size of ifinfomsg
4196 * with an optional attribute.
4198 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4199 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4200 nla_attr_size(sizeof(u32)))) {
4201 struct ifinfomsg *ifm;
4203 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4204 tb, IFLA_MAX, ifla_policy,
4208 } else if (err == 0) {
4209 if (tb[IFLA_MASTER])
4210 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4213 ifm = nlmsg_data(nlh);
4214 *brport_idx = ifm->ifi_index;
4219 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4221 struct net_device *dev;
4222 struct net_device *br_dev = NULL;
4223 const struct net_device_ops *ops = NULL;
4224 const struct net_device_ops *cops = NULL;
4225 struct net *net = sock_net(skb->sk);
4226 struct hlist_head *head;
4234 if (cb->strict_check)
4235 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4238 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4244 br_dev = __dev_get_by_index(net, br_idx);
4248 ops = br_dev->netdev_ops;
4252 s_idx = cb->args[1];
4254 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4256 head = &net->dev_index_head[h];
4257 hlist_for_each_entry(dev, head, index_hlist) {
4259 if (brport_idx && (dev->ifindex != brport_idx))
4262 if (!br_idx) { /* user did not specify a specific bridge */
4263 if (netif_is_bridge_port(dev)) {
4264 br_dev = netdev_master_upper_dev_get(dev);
4265 cops = br_dev->netdev_ops;
4268 if (dev != br_dev &&
4269 !netif_is_bridge_port(dev))
4272 if (br_dev != netdev_master_upper_dev_get(dev) &&
4273 !(dev->priv_flags & IFF_EBRIDGE))
4281 if (netif_is_bridge_port(dev)) {
4282 if (cops && cops->ndo_fdb_dump) {
4283 err = cops->ndo_fdb_dump(skb, cb,
4286 if (err == -EMSGSIZE)
4291 if (dev->netdev_ops->ndo_fdb_dump)
4292 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4296 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4298 if (err == -EMSGSIZE)
4303 /* reset fdb offset to 0 for rest of the interfaces */
4319 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4320 struct nlattr **tb, u8 *ndm_flags,
4321 int *br_idx, int *brport_idx, u8 **addr,
4322 u16 *vid, struct netlink_ext_ack *extack)
4327 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4328 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4332 ndm = nlmsg_data(nlh);
4333 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4335 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4339 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4340 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4344 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4345 NDA_MAX, nda_policy, extack);
4349 *ndm_flags = ndm->ndm_flags;
4350 *brport_idx = ndm->ndm_ifindex;
4351 for (i = 0; i <= NDA_MAX; ++i) {
4357 *br_idx = nla_get_u32(tb[i]);
4360 if (nla_len(tb[i]) != ETH_ALEN) {
4361 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4364 *addr = nla_data(tb[i]);
4367 err = fdb_vid_parse(tb[i], vid, extack);
4374 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4382 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4383 struct netlink_ext_ack *extack)
4385 struct net_device *dev = NULL, *br_dev = NULL;
4386 const struct net_device_ops *ops = NULL;
4387 struct net *net = sock_net(in_skb->sk);
4388 struct nlattr *tb[NDA_MAX + 1];
4389 struct sk_buff *skb;
4397 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4398 &brport_idx, &addr, &vid, extack);
4403 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4408 dev = __dev_get_by_index(net, brport_idx);
4410 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4417 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4421 br_dev = __dev_get_by_index(net, br_idx);
4423 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4426 ops = br_dev->netdev_ops;
4430 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4431 if (!netif_is_bridge_port(dev)) {
4432 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4435 br_dev = netdev_master_upper_dev_get(dev);
4437 NL_SET_ERR_MSG(extack, "Master of device not found");
4440 ops = br_dev->netdev_ops;
4442 if (!(ndm_flags & NTF_SELF)) {
4443 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4446 ops = dev->netdev_ops;
4450 if (!br_dev && !dev) {
4451 NL_SET_ERR_MSG(extack, "No device specified");
4455 if (!ops || !ops->ndo_fdb_get) {
4456 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4460 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4466 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4467 NETLINK_CB(in_skb).portid,
4468 nlh->nlmsg_seq, extack);
4472 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4478 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4479 unsigned int attrnum, unsigned int flag)
4482 return nla_put_u8(skb, attrnum, !!(flags & flag));
4486 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4487 struct net_device *dev, u16 mode,
4488 u32 flags, u32 mask, int nlflags,
4490 int (*vlan_fill)(struct sk_buff *skb,
4491 struct net_device *dev,
4494 struct nlmsghdr *nlh;
4495 struct ifinfomsg *ifm;
4496 struct nlattr *br_afspec;
4497 struct nlattr *protinfo;
4498 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4499 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4502 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4506 ifm = nlmsg_data(nlh);
4507 ifm->ifi_family = AF_BRIDGE;
4509 ifm->ifi_type = dev->type;
4510 ifm->ifi_index = dev->ifindex;
4511 ifm->ifi_flags = dev_get_flags(dev);
4512 ifm->ifi_change = 0;
4515 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4516 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4517 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4519 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4521 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4522 (dev->ifindex != dev_get_iflink(dev) &&
4523 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4524 goto nla_put_failure;
4526 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4528 goto nla_put_failure;
4530 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4531 nla_nest_cancel(skb, br_afspec);
4532 goto nla_put_failure;
4535 if (mode != BRIDGE_MODE_UNDEF) {
4536 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4537 nla_nest_cancel(skb, br_afspec);
4538 goto nla_put_failure;
4542 err = vlan_fill(skb, dev, filter_mask);
4544 nla_nest_cancel(skb, br_afspec);
4545 goto nla_put_failure;
4548 nla_nest_end(skb, br_afspec);
4550 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4552 goto nla_put_failure;
4554 if (brport_nla_put_flag(skb, flags, mask,
4555 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4556 brport_nla_put_flag(skb, flags, mask,
4557 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4558 brport_nla_put_flag(skb, flags, mask,
4559 IFLA_BRPORT_FAST_LEAVE,
4560 BR_MULTICAST_FAST_LEAVE) ||
4561 brport_nla_put_flag(skb, flags, mask,
4562 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4563 brport_nla_put_flag(skb, flags, mask,
4564 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4565 brport_nla_put_flag(skb, flags, mask,
4566 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4567 brport_nla_put_flag(skb, flags, mask,
4568 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4569 brport_nla_put_flag(skb, flags, mask,
4570 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4571 brport_nla_put_flag(skb, flags, mask,
4572 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4573 brport_nla_put_flag(skb, flags, mask,
4574 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4575 nla_nest_cancel(skb, protinfo);
4576 goto nla_put_failure;
4579 nla_nest_end(skb, protinfo);
4581 nlmsg_end(skb, nlh);
4584 nlmsg_cancel(skb, nlh);
4585 return err ? err : -EMSGSIZE;
4587 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4589 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4590 bool strict_check, u32 *filter_mask,
4591 struct netlink_ext_ack *extack)
4593 struct nlattr *tb[IFLA_MAX+1];
4597 struct ifinfomsg *ifm;
4599 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4600 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4604 ifm = nlmsg_data(nlh);
4605 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4606 ifm->ifi_change || ifm->ifi_index) {
4607 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4611 err = nlmsg_parse_deprecated_strict(nlh,
4612 sizeof(struct ifinfomsg),
4613 tb, IFLA_MAX, ifla_policy,
4616 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4617 tb, IFLA_MAX, ifla_policy,
4623 /* new attributes should only be added with strict checking */
4624 for (i = 0; i <= IFLA_MAX; ++i) {
4630 *filter_mask = nla_get_u32(tb[i]);
4634 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4643 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4645 const struct nlmsghdr *nlh = cb->nlh;
4646 struct net *net = sock_net(skb->sk);
4647 struct net_device *dev;
4649 u32 portid = NETLINK_CB(cb->skb).portid;
4650 u32 seq = nlh->nlmsg_seq;
4651 u32 filter_mask = 0;
4654 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4656 if (err < 0 && cb->strict_check)
4660 for_each_netdev_rcu(net, dev) {
4661 const struct net_device_ops *ops = dev->netdev_ops;
4662 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4664 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4665 if (idx >= cb->args[0]) {
4666 err = br_dev->netdev_ops->ndo_bridge_getlink(
4667 skb, portid, seq, dev,
4668 filter_mask, NLM_F_MULTI);
4669 if (err < 0 && err != -EOPNOTSUPP) {
4670 if (likely(skb->len))
4679 if (ops->ndo_bridge_getlink) {
4680 if (idx >= cb->args[0]) {
4681 err = ops->ndo_bridge_getlink(skb, portid,
4685 if (err < 0 && err != -EOPNOTSUPP) {
4686 if (likely(skb->len))
4703 static inline size_t bridge_nlmsg_size(void)
4705 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4706 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4707 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4708 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4709 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4710 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4711 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4712 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4713 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4714 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4715 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4718 static int rtnl_bridge_notify(struct net_device *dev)
4720 struct net *net = dev_net(dev);
4721 struct sk_buff *skb;
4722 int err = -EOPNOTSUPP;
4724 if (!dev->netdev_ops->ndo_bridge_getlink)
4727 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4733 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4740 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4743 WARN_ON(err == -EMSGSIZE);
4746 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4750 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4751 struct netlink_ext_ack *extack)
4753 struct net *net = sock_net(skb->sk);
4754 struct ifinfomsg *ifm;
4755 struct net_device *dev;
4756 struct nlattr *br_spec, *attr = NULL;
4757 int rem, err = -EOPNOTSUPP;
4759 bool have_flags = false;
4761 if (nlmsg_len(nlh) < sizeof(*ifm))
4764 ifm = nlmsg_data(nlh);
4765 if (ifm->ifi_family != AF_BRIDGE)
4766 return -EPFNOSUPPORT;
4768 dev = __dev_get_by_index(net, ifm->ifi_index);
4770 NL_SET_ERR_MSG(extack, "unknown ifindex");
4774 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4776 nla_for_each_nested(attr, br_spec, rem) {
4777 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4778 if (nla_len(attr) < sizeof(flags))
4782 flags = nla_get_u16(attr);
4788 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4789 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4791 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4796 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4801 flags &= ~BRIDGE_FLAGS_MASTER;
4804 if ((flags & BRIDGE_FLAGS_SELF)) {
4805 if (!dev->netdev_ops->ndo_bridge_setlink)
4808 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4812 flags &= ~BRIDGE_FLAGS_SELF;
4814 /* Generate event to notify upper layer of bridge
4817 err = rtnl_bridge_notify(dev);
4822 memcpy(nla_data(attr), &flags, sizeof(flags));
4827 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4828 struct netlink_ext_ack *extack)
4830 struct net *net = sock_net(skb->sk);
4831 struct ifinfomsg *ifm;
4832 struct net_device *dev;
4833 struct nlattr *br_spec, *attr = NULL;
4834 int rem, err = -EOPNOTSUPP;
4836 bool have_flags = false;
4838 if (nlmsg_len(nlh) < sizeof(*ifm))
4841 ifm = nlmsg_data(nlh);
4842 if (ifm->ifi_family != AF_BRIDGE)
4843 return -EPFNOSUPPORT;
4845 dev = __dev_get_by_index(net, ifm->ifi_index);
4847 NL_SET_ERR_MSG(extack, "unknown ifindex");
4851 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4853 nla_for_each_nested(attr, br_spec, rem) {
4854 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4855 if (nla_len(attr) < sizeof(flags))
4859 flags = nla_get_u16(attr);
4865 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4866 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4868 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4873 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4877 flags &= ~BRIDGE_FLAGS_MASTER;
4880 if ((flags & BRIDGE_FLAGS_SELF)) {
4881 if (!dev->netdev_ops->ndo_bridge_dellink)
4884 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4888 flags &= ~BRIDGE_FLAGS_SELF;
4890 /* Generate event to notify upper layer of bridge
4893 err = rtnl_bridge_notify(dev);
4898 memcpy(nla_data(attr), &flags, sizeof(flags));
4903 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4905 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4906 (!idxattr || idxattr == attrid);
4909 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4910 static int rtnl_get_offload_stats_attr_size(int attr_id)
4913 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4914 return sizeof(struct rtnl_link_stats64);
4920 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4923 struct nlattr *attr = NULL;
4928 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4929 dev->netdev_ops->ndo_get_offload_stats))
4932 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4933 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4934 if (attr_id < *prividx)
4937 size = rtnl_get_offload_stats_attr_size(attr_id);
4941 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4944 attr = nla_reserve_64bit(skb, attr_id, size,
4945 IFLA_OFFLOAD_XSTATS_UNSPEC);
4947 goto nla_put_failure;
4949 attr_data = nla_data(attr);
4950 memset(attr_data, 0, size);
4951 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4954 goto get_offload_stats_failure;
4965 get_offload_stats_failure:
4970 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4976 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4977 dev->netdev_ops->ndo_get_offload_stats))
4980 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4981 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4982 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4984 size = rtnl_get_offload_stats_attr_size(attr_id);
4985 nla_size += nla_total_size_64bit(size);
4989 nla_size += nla_total_size(0);
4994 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4995 int type, u32 pid, u32 seq, u32 change,
4996 unsigned int flags, unsigned int filter_mask,
4997 int *idxattr, int *prividx)
4999 struct if_stats_msg *ifsm;
5000 struct nlmsghdr *nlh;
5001 struct nlattr *attr;
5002 int s_prividx = *prividx;
5007 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5011 ifsm = nlmsg_data(nlh);
5012 ifsm->family = PF_UNSPEC;
5015 ifsm->ifindex = dev->ifindex;
5016 ifsm->filter_mask = filter_mask;
5018 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5019 struct rtnl_link_stats64 *sp;
5021 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5022 sizeof(struct rtnl_link_stats64),
5025 goto nla_put_failure;
5027 sp = nla_data(attr);
5028 dev_get_stats(dev, sp);
5031 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5032 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5034 if (ops && ops->fill_linkxstats) {
5035 *idxattr = IFLA_STATS_LINK_XSTATS;
5036 attr = nla_nest_start_noflag(skb,
5037 IFLA_STATS_LINK_XSTATS);
5039 goto nla_put_failure;
5041 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5042 nla_nest_end(skb, attr);
5044 goto nla_put_failure;
5049 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5051 const struct rtnl_link_ops *ops = NULL;
5052 const struct net_device *master;
5054 master = netdev_master_upper_dev_get(dev);
5056 ops = master->rtnl_link_ops;
5057 if (ops && ops->fill_linkxstats) {
5058 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5059 attr = nla_nest_start_noflag(skb,
5060 IFLA_STATS_LINK_XSTATS_SLAVE);
5062 goto nla_put_failure;
5064 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5065 nla_nest_end(skb, attr);
5067 goto nla_put_failure;
5072 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5074 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5075 attr = nla_nest_start_noflag(skb,
5076 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5078 goto nla_put_failure;
5080 err = rtnl_get_offload_stats(skb, dev, prividx);
5081 if (err == -ENODATA)
5082 nla_nest_cancel(skb, attr);
5084 nla_nest_end(skb, attr);
5086 if (err && err != -ENODATA)
5087 goto nla_put_failure;
5091 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5092 struct rtnl_af_ops *af_ops;
5094 *idxattr = IFLA_STATS_AF_SPEC;
5095 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5097 goto nla_put_failure;
5100 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5101 if (af_ops->fill_stats_af) {
5105 af = nla_nest_start_noflag(skb,
5109 goto nla_put_failure;
5111 err = af_ops->fill_stats_af(skb, dev);
5113 if (err == -ENODATA) {
5114 nla_nest_cancel(skb, af);
5115 } else if (err < 0) {
5117 goto nla_put_failure;
5120 nla_nest_end(skb, af);
5125 nla_nest_end(skb, attr);
5130 nlmsg_end(skb, nlh);
5135 /* not a multi message or no progress mean a real error */
5136 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5137 nlmsg_cancel(skb, nlh);
5139 nlmsg_end(skb, nlh);
5144 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5149 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5150 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5152 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5153 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5154 int attr = IFLA_STATS_LINK_XSTATS;
5156 if (ops && ops->get_linkxstats_size) {
5157 size += nla_total_size(ops->get_linkxstats_size(dev,
5159 /* for IFLA_STATS_LINK_XSTATS */
5160 size += nla_total_size(0);
5164 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5165 struct net_device *_dev = (struct net_device *)dev;
5166 const struct rtnl_link_ops *ops = NULL;
5167 const struct net_device *master;
5169 /* netdev_master_upper_dev_get can't take const */
5170 master = netdev_master_upper_dev_get(_dev);
5172 ops = master->rtnl_link_ops;
5173 if (ops && ops->get_linkxstats_size) {
5174 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5176 size += nla_total_size(ops->get_linkxstats_size(dev,
5178 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5179 size += nla_total_size(0);
5183 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5184 size += rtnl_get_offload_stats_size(dev);
5186 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5187 struct rtnl_af_ops *af_ops;
5189 /* for IFLA_STATS_AF_SPEC */
5190 size += nla_total_size(0);
5193 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5194 if (af_ops->get_stats_af_size) {
5195 size += nla_total_size(
5196 af_ops->get_stats_af_size(dev));
5199 size += nla_total_size(0);
5208 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5209 bool is_dump, struct netlink_ext_ack *extack)
5211 struct if_stats_msg *ifsm;
5213 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5214 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5221 ifsm = nlmsg_data(nlh);
5223 /* only requests using strict checks can pass data to influence
5224 * the dump. The legacy exception is filter_mask.
5226 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5227 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5230 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5231 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5234 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5235 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5242 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5243 struct netlink_ext_ack *extack)
5245 struct net *net = sock_net(skb->sk);
5246 struct net_device *dev = NULL;
5247 int idxattr = 0, prividx = 0;
5248 struct if_stats_msg *ifsm;
5249 struct sk_buff *nskb;
5253 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5258 ifsm = nlmsg_data(nlh);
5259 if (ifsm->ifindex > 0)
5260 dev = __dev_get_by_index(net, ifsm->ifindex);
5267 filter_mask = ifsm->filter_mask;
5271 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5275 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5276 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5277 0, filter_mask, &idxattr, &prividx);
5279 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5280 WARN_ON(err == -EMSGSIZE);
5283 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5289 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5291 struct netlink_ext_ack *extack = cb->extack;
5292 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5293 struct net *net = sock_net(skb->sk);
5294 unsigned int flags = NLM_F_MULTI;
5295 struct if_stats_msg *ifsm;
5296 struct hlist_head *head;
5297 struct net_device *dev;
5298 u32 filter_mask = 0;
5302 s_idx = cb->args[1];
5303 s_idxattr = cb->args[2];
5304 s_prividx = cb->args[3];
5306 cb->seq = net->dev_base_seq;
5308 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5312 ifsm = nlmsg_data(cb->nlh);
5313 filter_mask = ifsm->filter_mask;
5315 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5319 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5321 head = &net->dev_index_head[h];
5322 hlist_for_each_entry(dev, head, index_hlist) {
5325 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5326 NETLINK_CB(cb->skb).portid,
5327 cb->nlh->nlmsg_seq, 0,
5329 &s_idxattr, &s_prividx);
5330 /* If we ran out of room on the first message,
5333 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5339 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5345 cb->args[3] = s_prividx;
5346 cb->args[2] = s_idxattr;
5353 /* Process one rtnetlink message. */
5355 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5356 struct netlink_ext_ack *extack)
5358 struct net *net = sock_net(skb->sk);
5359 struct rtnl_link *link;
5360 struct module *owner;
5361 int err = -EOPNOTSUPP;
5362 rtnl_doit_func doit;
5368 type = nlh->nlmsg_type;
5374 /* All the messages must have at least 1 byte length */
5375 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5378 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5381 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5385 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5387 rtnl_dumpit_func dumpit;
5388 u16 min_dump_alloc = 0;
5390 link = rtnl_get_link(family, type);
5391 if (!link || !link->dumpit) {
5393 link = rtnl_get_link(family, type);
5394 if (!link || !link->dumpit)
5397 owner = link->owner;
5398 dumpit = link->dumpit;
5400 if (type == RTM_GETLINK - RTM_BASE)
5401 min_dump_alloc = rtnl_calcit(skb, nlh);
5404 /* need to do this before rcu_read_unlock() */
5405 if (!try_module_get(owner))
5406 err = -EPROTONOSUPPORT;
5412 struct netlink_dump_control c = {
5414 .min_dump_alloc = min_dump_alloc,
5417 err = netlink_dump_start(rtnl, skb, nlh, &c);
5418 /* netlink_dump_start() will keep a reference on
5419 * module if dump is still in progress.
5426 link = rtnl_get_link(family, type);
5427 if (!link || !link->doit) {
5429 link = rtnl_get_link(PF_UNSPEC, type);
5430 if (!link || !link->doit)
5434 owner = link->owner;
5435 if (!try_module_get(owner)) {
5436 err = -EPROTONOSUPPORT;
5440 flags = link->flags;
5441 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5445 err = doit(skb, nlh, extack);
5452 link = rtnl_get_link(family, type);
5453 if (link && link->doit)
5454 err = link->doit(skb, nlh, extack);
5470 static void rtnetlink_rcv(struct sk_buff *skb)
5472 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5475 static int rtnetlink_bind(struct net *net, int group)
5478 case RTNLGRP_IPV4_MROUTE_R:
5479 case RTNLGRP_IPV6_MROUTE_R:
5480 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5487 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5489 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5493 case NETDEV_CHANGEMTU:
5494 case NETDEV_CHANGEADDR:
5495 case NETDEV_CHANGENAME:
5496 case NETDEV_FEAT_CHANGE:
5497 case NETDEV_BONDING_FAILOVER:
5498 case NETDEV_POST_TYPE_CHANGE:
5499 case NETDEV_NOTIFY_PEERS:
5500 case NETDEV_CHANGEUPPER:
5501 case NETDEV_RESEND_IGMP:
5502 case NETDEV_CHANGEINFODATA:
5503 case NETDEV_CHANGELOWERSTATE:
5504 case NETDEV_CHANGE_TX_QUEUE_LEN:
5505 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5506 GFP_KERNEL, NULL, 0);
5514 static struct notifier_block rtnetlink_dev_notifier = {
5515 .notifier_call = rtnetlink_event,
5519 static int __net_init rtnetlink_net_init(struct net *net)
5522 struct netlink_kernel_cfg cfg = {
5523 .groups = RTNLGRP_MAX,
5524 .input = rtnetlink_rcv,
5525 .cb_mutex = &rtnl_mutex,
5526 .flags = NL_CFG_F_NONROOT_RECV,
5527 .bind = rtnetlink_bind,
5530 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5537 static void __net_exit rtnetlink_net_exit(struct net *net)
5539 netlink_kernel_release(net->rtnl);
5543 static struct pernet_operations rtnetlink_net_ops = {
5544 .init = rtnetlink_net_init,
5545 .exit = rtnetlink_net_exit,
5548 void __init rtnetlink_init(void)
5550 if (register_pernet_subsys(&rtnetlink_net_ops))
5551 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5553 register_netdevice_notifier(&rtnetlink_dev_notifier);
5555 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5556 rtnl_dump_ifinfo, 0);
5557 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5558 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5559 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5561 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5562 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5563 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5565 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5566 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5568 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5569 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5570 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5572 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5573 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5574 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5576 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,