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));
1253 vf_rss_query_en.vf =
1256 port_guid.vf = ivi.vf;
1258 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1259 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1260 vf_vlan.vlan = ivi.vlan;
1261 vf_vlan.qos = ivi.qos;
1262 vf_vlan_info.vlan = ivi.vlan;
1263 vf_vlan_info.qos = ivi.qos;
1264 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1265 vf_tx_rate.rate = ivi.max_tx_rate;
1266 vf_rate.min_tx_rate = ivi.min_tx_rate;
1267 vf_rate.max_tx_rate = ivi.max_tx_rate;
1268 vf_spoofchk.setting = ivi.spoofchk;
1269 vf_linkstate.link_state = ivi.linkstate;
1270 vf_rss_query_en.setting = ivi.rss_query_en;
1271 vf_trust.setting = ivi.trusted;
1272 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1274 goto nla_put_vfinfo_failure;
1275 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1276 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1277 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1278 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1280 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1282 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1284 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1286 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1287 sizeof(vf_rss_query_en),
1288 &vf_rss_query_en) ||
1289 nla_put(skb, IFLA_VF_TRUST,
1290 sizeof(vf_trust), &vf_trust))
1291 goto nla_put_vf_failure;
1293 memset(&node_guid, 0, sizeof(node_guid));
1294 memset(&port_guid, 0, sizeof(port_guid));
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_FD] = { .type = NLA_S32 },
1876 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1877 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1878 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1881 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1883 const struct rtnl_link_ops *ops = NULL;
1884 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1886 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1889 if (linfo[IFLA_INFO_KIND]) {
1890 char kind[MODULE_NAME_LEN];
1892 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1893 ops = rtnl_link_ops_get(kind);
1899 static bool link_master_filtered(struct net_device *dev, int master_idx)
1901 struct net_device *master;
1906 master = netdev_master_upper_dev_get(dev);
1907 if (!master || master->ifindex != master_idx)
1913 static bool link_kind_filtered(const struct net_device *dev,
1914 const struct rtnl_link_ops *kind_ops)
1916 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1922 static bool link_dump_filtered(struct net_device *dev,
1924 const struct rtnl_link_ops *kind_ops)
1926 if (link_master_filtered(dev, master_idx) ||
1927 link_kind_filtered(dev, kind_ops))
1934 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1935 * @sk: netlink socket
1936 * @netnsid: network namespace identifier
1938 * Returns the network namespace identified by netnsid on success or an error
1939 * pointer on failure.
1941 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1945 net = get_net_ns_by_id(sock_net(sk), netnsid);
1947 return ERR_PTR(-EINVAL);
1949 /* For now, the caller is required to have CAP_NET_ADMIN in
1950 * the user namespace owning the target net ns.
1952 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1954 return ERR_PTR(-EACCES);
1958 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1960 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1961 bool strict_check, struct nlattr **tb,
1962 struct netlink_ext_ack *extack)
1967 struct ifinfomsg *ifm;
1969 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1970 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1974 ifm = nlmsg_data(nlh);
1975 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1977 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1980 if (ifm->ifi_index) {
1981 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1985 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1986 IFLA_MAX, ifla_policy,
1990 /* A hack to preserve kernel<->userspace interface.
1991 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1992 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1993 * what iproute2 < v3.9.0 used.
1994 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1995 * attribute, its netlink message is shorter than struct ifinfomsg.
1997 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1998 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2000 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2004 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2006 struct netlink_ext_ack *extack = cb->extack;
2007 const struct nlmsghdr *nlh = cb->nlh;
2008 struct net *net = sock_net(skb->sk);
2009 struct net *tgt_net = net;
2012 struct net_device *dev;
2013 struct hlist_head *head;
2014 struct nlattr *tb[IFLA_MAX+1];
2015 u32 ext_filter_mask = 0;
2016 const struct rtnl_link_ops *kind_ops = NULL;
2017 unsigned int flags = NLM_F_MULTI;
2023 s_idx = cb->args[1];
2025 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2027 if (cb->strict_check)
2033 for (i = 0; i <= IFLA_MAX; ++i) {
2037 /* new attributes should only be added with strict checking */
2039 case IFLA_TARGET_NETNSID:
2040 netnsid = nla_get_s32(tb[i]);
2041 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2042 if (IS_ERR(tgt_net)) {
2043 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2044 return PTR_ERR(tgt_net);
2048 ext_filter_mask = nla_get_u32(tb[i]);
2051 master_idx = nla_get_u32(tb[i]);
2054 kind_ops = linkinfo_to_kind_ops(tb[i]);
2057 if (cb->strict_check) {
2058 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2064 if (master_idx || kind_ops)
2065 flags |= NLM_F_DUMP_FILTERED;
2068 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2070 head = &tgt_net->dev_index_head[h];
2071 hlist_for_each_entry(dev, head, index_hlist) {
2072 if (link_dump_filtered(dev, master_idx, kind_ops))
2076 err = rtnl_fill_ifinfo(skb, dev, net,
2078 NETLINK_CB(cb->skb).portid,
2079 nlh->nlmsg_seq, 0, flags,
2080 ext_filter_mask, 0, NULL, 0,
2081 netnsid, GFP_KERNEL);
2084 if (likely(skb->len))
2098 cb->seq = net->dev_base_seq;
2099 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2106 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2107 struct netlink_ext_ack *exterr)
2109 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2112 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2114 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2117 /* Examine the link attributes and figure out which
2118 * network namespace we are talking about.
2120 if (tb[IFLA_NET_NS_PID])
2121 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2122 else if (tb[IFLA_NET_NS_FD])
2123 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2125 net = get_net(src_net);
2128 EXPORT_SYMBOL(rtnl_link_get_net);
2130 /* Figure out which network namespace we are talking about by
2131 * examining the link attributes in the following order:
2133 * 1. IFLA_NET_NS_PID
2135 * 3. IFLA_TARGET_NETNSID
2137 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2138 struct nlattr *tb[])
2142 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2143 return rtnl_link_get_net(src_net, tb);
2145 if (!tb[IFLA_TARGET_NETNSID])
2146 return get_net(src_net);
2148 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2150 return ERR_PTR(-EINVAL);
2155 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2156 struct net *src_net,
2157 struct nlattr *tb[], int cap)
2161 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2165 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2167 return ERR_PTR(-EPERM);
2173 /* Verify that rtnetlink requests do not pass additional properties
2174 * potentially referring to different network namespaces.
2176 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2177 struct netlink_ext_ack *extack,
2181 if (netns_id_only) {
2182 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2185 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2189 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2192 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2195 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2201 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2205 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2208 if (tb[IFLA_ADDRESS] &&
2209 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2212 if (tb[IFLA_BROADCAST] &&
2213 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2217 if (tb[IFLA_AF_SPEC]) {
2221 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2222 const struct rtnl_af_ops *af_ops;
2225 af_ops = rtnl_af_lookup(nla_type(af));
2228 return -EAFNOSUPPORT;
2231 if (!af_ops->set_link_af) {
2236 if (af_ops->validate_link_af) {
2237 err = af_ops->validate_link_af(dev, af);
2251 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2254 const struct net_device_ops *ops = dev->netdev_ops;
2256 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2259 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2261 if (dev->type != ARPHRD_INFINIBAND)
2264 return handle_infiniband_guid(dev, ivt, guid_type);
2267 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2269 const struct net_device_ops *ops = dev->netdev_ops;
2272 if (tb[IFLA_VF_MAC]) {
2273 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2275 if (ivm->vf >= INT_MAX)
2278 if (ops->ndo_set_vf_mac)
2279 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2285 if (tb[IFLA_VF_VLAN]) {
2286 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2288 if (ivv->vf >= INT_MAX)
2291 if (ops->ndo_set_vf_vlan)
2292 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2294 htons(ETH_P_8021Q));
2299 if (tb[IFLA_VF_VLAN_LIST]) {
2300 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2301 struct nlattr *attr;
2305 if (!ops->ndo_set_vf_vlan)
2308 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2309 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2310 nla_len(attr) < NLA_HDRLEN) {
2313 if (len >= MAX_VLAN_LIST_LEN)
2315 ivvl[len] = nla_data(attr);
2322 if (ivvl[0]->vf >= INT_MAX)
2324 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2325 ivvl[0]->qos, ivvl[0]->vlan_proto);
2330 if (tb[IFLA_VF_TX_RATE]) {
2331 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2332 struct ifla_vf_info ivf;
2334 if (ivt->vf >= INT_MAX)
2337 if (ops->ndo_get_vf_config)
2338 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2343 if (ops->ndo_set_vf_rate)
2344 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2351 if (tb[IFLA_VF_RATE]) {
2352 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2354 if (ivt->vf >= INT_MAX)
2357 if (ops->ndo_set_vf_rate)
2358 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2365 if (tb[IFLA_VF_SPOOFCHK]) {
2366 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2368 if (ivs->vf >= INT_MAX)
2371 if (ops->ndo_set_vf_spoofchk)
2372 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2378 if (tb[IFLA_VF_LINK_STATE]) {
2379 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2381 if (ivl->vf >= INT_MAX)
2384 if (ops->ndo_set_vf_link_state)
2385 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2391 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2392 struct ifla_vf_rss_query_en *ivrssq_en;
2395 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2396 if (ivrssq_en->vf >= INT_MAX)
2398 if (ops->ndo_set_vf_rss_query_en)
2399 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2400 ivrssq_en->setting);
2405 if (tb[IFLA_VF_TRUST]) {
2406 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2408 if (ivt->vf >= INT_MAX)
2411 if (ops->ndo_set_vf_trust)
2412 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2417 if (tb[IFLA_VF_IB_NODE_GUID]) {
2418 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2420 if (ivt->vf >= INT_MAX)
2422 if (!ops->ndo_set_vf_guid)
2424 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2427 if (tb[IFLA_VF_IB_PORT_GUID]) {
2428 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2430 if (ivt->vf >= INT_MAX)
2432 if (!ops->ndo_set_vf_guid)
2435 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2441 static int do_set_master(struct net_device *dev, int ifindex,
2442 struct netlink_ext_ack *extack)
2444 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2445 const struct net_device_ops *ops;
2449 if (upper_dev->ifindex == ifindex)
2451 ops = upper_dev->netdev_ops;
2452 if (ops->ndo_del_slave) {
2453 err = ops->ndo_del_slave(upper_dev, dev);
2456 netdev_update_lockdep_key(dev);
2463 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2466 ops = upper_dev->netdev_ops;
2467 if (ops->ndo_add_slave) {
2468 err = ops->ndo_add_slave(upper_dev, dev, extack);
2478 #define DO_SETLINK_MODIFIED 0x01
2479 /* notify flag means notify + modified. */
2480 #define DO_SETLINK_NOTIFY 0x03
2481 static int do_setlink(const struct sk_buff *skb,
2482 struct net_device *dev, struct ifinfomsg *ifm,
2483 struct netlink_ext_ack *extack,
2484 struct nlattr **tb, char *ifname, int status)
2486 const struct net_device_ops *ops = dev->netdev_ops;
2489 err = validate_linkmsg(dev, tb);
2493 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2494 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2501 err = dev_change_net_namespace(dev, net, ifname);
2505 status |= DO_SETLINK_MODIFIED;
2509 struct rtnl_link_ifmap *u_map;
2512 if (!ops->ndo_set_config) {
2517 if (!netif_device_present(dev)) {
2522 u_map = nla_data(tb[IFLA_MAP]);
2523 k_map.mem_start = (unsigned long) u_map->mem_start;
2524 k_map.mem_end = (unsigned long) u_map->mem_end;
2525 k_map.base_addr = (unsigned short) u_map->base_addr;
2526 k_map.irq = (unsigned char) u_map->irq;
2527 k_map.dma = (unsigned char) u_map->dma;
2528 k_map.port = (unsigned char) u_map->port;
2530 err = ops->ndo_set_config(dev, &k_map);
2534 status |= DO_SETLINK_NOTIFY;
2537 if (tb[IFLA_ADDRESS]) {
2538 struct sockaddr *sa;
2541 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2543 sa = kmalloc(len, GFP_KERNEL);
2548 sa->sa_family = dev->type;
2549 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2551 err = dev_set_mac_address(dev, sa, extack);
2555 status |= DO_SETLINK_MODIFIED;
2559 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2562 status |= DO_SETLINK_MODIFIED;
2565 if (tb[IFLA_GROUP]) {
2566 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2567 status |= DO_SETLINK_NOTIFY;
2571 * Interface selected by interface index but interface
2572 * name provided implies that a name change has been
2575 if (ifm->ifi_index > 0 && ifname[0]) {
2576 err = dev_change_name(dev, ifname);
2579 status |= DO_SETLINK_MODIFIED;
2582 if (tb[IFLA_IFALIAS]) {
2583 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2584 nla_len(tb[IFLA_IFALIAS]));
2587 status |= DO_SETLINK_NOTIFY;
2590 if (tb[IFLA_BROADCAST]) {
2591 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2592 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2595 if (ifm->ifi_flags || ifm->ifi_change) {
2596 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2602 if (tb[IFLA_MASTER]) {
2603 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2606 status |= DO_SETLINK_MODIFIED;
2609 if (tb[IFLA_CARRIER]) {
2610 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2613 status |= DO_SETLINK_MODIFIED;
2616 if (tb[IFLA_TXQLEN]) {
2617 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2619 err = dev_change_tx_queue_len(dev, value);
2622 status |= DO_SETLINK_MODIFIED;
2625 if (tb[IFLA_GSO_MAX_SIZE]) {
2626 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2628 if (max_size > GSO_MAX_SIZE) {
2633 if (dev->gso_max_size ^ max_size) {
2634 netif_set_gso_max_size(dev, max_size);
2635 status |= DO_SETLINK_MODIFIED;
2639 if (tb[IFLA_GSO_MAX_SEGS]) {
2640 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2642 if (max_segs > GSO_MAX_SEGS) {
2647 if (dev->gso_max_segs ^ max_segs) {
2648 dev->gso_max_segs = max_segs;
2649 status |= DO_SETLINK_MODIFIED;
2653 if (tb[IFLA_OPERSTATE])
2654 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2656 if (tb[IFLA_LINKMODE]) {
2657 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2659 write_lock_bh(&dev_base_lock);
2660 if (dev->link_mode ^ value)
2661 status |= DO_SETLINK_NOTIFY;
2662 dev->link_mode = value;
2663 write_unlock_bh(&dev_base_lock);
2666 if (tb[IFLA_VFINFO_LIST]) {
2667 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2668 struct nlattr *attr;
2671 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2672 if (nla_type(attr) != IFLA_VF_INFO ||
2673 nla_len(attr) < NLA_HDRLEN) {
2677 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2683 err = do_setvfinfo(dev, vfinfo);
2686 status |= DO_SETLINK_NOTIFY;
2691 if (tb[IFLA_VF_PORTS]) {
2692 struct nlattr *port[IFLA_PORT_MAX+1];
2693 struct nlattr *attr;
2698 if (!ops->ndo_set_vf_port)
2701 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2702 if (nla_type(attr) != IFLA_VF_PORT ||
2703 nla_len(attr) < NLA_HDRLEN) {
2707 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2713 if (!port[IFLA_PORT_VF]) {
2717 vf = nla_get_u32(port[IFLA_PORT_VF]);
2718 err = ops->ndo_set_vf_port(dev, vf, port);
2721 status |= DO_SETLINK_NOTIFY;
2726 if (tb[IFLA_PORT_SELF]) {
2727 struct nlattr *port[IFLA_PORT_MAX+1];
2729 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2731 ifla_port_policy, NULL);
2736 if (ops->ndo_set_vf_port)
2737 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2740 status |= DO_SETLINK_NOTIFY;
2743 if (tb[IFLA_AF_SPEC]) {
2747 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2748 const struct rtnl_af_ops *af_ops;
2752 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2754 err = af_ops->set_link_af(dev, af);
2761 status |= DO_SETLINK_NOTIFY;
2766 if (tb[IFLA_PROTO_DOWN]) {
2767 err = dev_change_proto_down(dev,
2768 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2771 status |= DO_SETLINK_NOTIFY;
2775 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2778 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2780 ifla_xdp_policy, NULL);
2784 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2789 if (xdp[IFLA_XDP_FLAGS]) {
2790 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2791 if (xdp_flags & ~XDP_FLAGS_MASK) {
2795 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2801 if (xdp[IFLA_XDP_FD]) {
2802 err = dev_change_xdp_fd(dev, extack,
2803 nla_get_s32(xdp[IFLA_XDP_FD]),
2807 status |= DO_SETLINK_NOTIFY;
2812 if (status & DO_SETLINK_MODIFIED) {
2813 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2814 netdev_state_change(dev);
2817 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",
2824 static struct net_device *rtnl_dev_get(struct net *net,
2825 struct nlattr *ifname_attr,
2826 struct nlattr *altifname_attr,
2829 char buffer[ALTIFNAMSIZ];
2834 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2835 else if (altifname_attr)
2836 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2841 return __dev_get_by_name(net, ifname);
2844 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2845 struct netlink_ext_ack *extack)
2847 struct net *net = sock_net(skb->sk);
2848 struct ifinfomsg *ifm;
2849 struct net_device *dev;
2851 struct nlattr *tb[IFLA_MAX+1];
2852 char ifname[IFNAMSIZ];
2854 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2855 ifla_policy, extack);
2859 err = rtnl_ensure_unique_netns(tb, extack, false);
2863 if (tb[IFLA_IFNAME])
2864 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2869 ifm = nlmsg_data(nlh);
2870 if (ifm->ifi_index > 0)
2871 dev = __dev_get_by_index(net, ifm->ifi_index);
2872 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2873 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2882 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2887 static int rtnl_group_dellink(const struct net *net, int group)
2889 struct net_device *dev, *aux;
2890 LIST_HEAD(list_kill);
2896 for_each_netdev(net, dev) {
2897 if (dev->group == group) {
2898 const struct rtnl_link_ops *ops;
2901 ops = dev->rtnl_link_ops;
2902 if (!ops || !ops->dellink)
2910 for_each_netdev_safe(net, dev, aux) {
2911 if (dev->group == group) {
2912 const struct rtnl_link_ops *ops;
2914 ops = dev->rtnl_link_ops;
2915 ops->dellink(dev, &list_kill);
2918 unregister_netdevice_many(&list_kill);
2923 int rtnl_delete_link(struct net_device *dev)
2925 const struct rtnl_link_ops *ops;
2926 LIST_HEAD(list_kill);
2928 ops = dev->rtnl_link_ops;
2929 if (!ops || !ops->dellink)
2932 ops->dellink(dev, &list_kill);
2933 unregister_netdevice_many(&list_kill);
2937 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2939 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2940 struct netlink_ext_ack *extack)
2942 struct net *net = sock_net(skb->sk);
2943 struct net *tgt_net = net;
2944 struct net_device *dev = NULL;
2945 struct ifinfomsg *ifm;
2946 struct nlattr *tb[IFLA_MAX+1];
2950 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2951 ifla_policy, extack);
2955 err = rtnl_ensure_unique_netns(tb, extack, true);
2959 if (tb[IFLA_TARGET_NETNSID]) {
2960 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2961 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2962 if (IS_ERR(tgt_net))
2963 return PTR_ERR(tgt_net);
2967 ifm = nlmsg_data(nlh);
2968 if (ifm->ifi_index > 0)
2969 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2970 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2971 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2972 tb[IFLA_ALT_IFNAME], NULL);
2973 else if (tb[IFLA_GROUP])
2974 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2979 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2985 err = rtnl_delete_link(dev);
2994 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2996 unsigned int old_flags;
2999 old_flags = dev->flags;
3000 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3001 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3007 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3008 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3010 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3011 __dev_notify_flags(dev, old_flags, ~0U);
3015 EXPORT_SYMBOL(rtnl_configure_link);
3017 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3018 unsigned char name_assign_type,
3019 const struct rtnl_link_ops *ops,
3020 struct nlattr *tb[],
3021 struct netlink_ext_ack *extack)
3023 struct net_device *dev;
3024 unsigned int num_tx_queues = 1;
3025 unsigned int num_rx_queues = 1;
3027 if (tb[IFLA_NUM_TX_QUEUES])
3028 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3029 else if (ops->get_num_tx_queues)
3030 num_tx_queues = ops->get_num_tx_queues();
3032 if (tb[IFLA_NUM_RX_QUEUES])
3033 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3034 else if (ops->get_num_rx_queues)
3035 num_rx_queues = ops->get_num_rx_queues();
3037 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3038 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3039 return ERR_PTR(-EINVAL);
3042 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3043 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3044 return ERR_PTR(-EINVAL);
3047 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3048 ops->setup, num_tx_queues, num_rx_queues);
3050 return ERR_PTR(-ENOMEM);
3052 dev_net_set(dev, net);
3053 dev->rtnl_link_ops = ops;
3054 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3057 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
3058 if (tb[IFLA_ADDRESS]) {
3059 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3060 nla_len(tb[IFLA_ADDRESS]));
3061 dev->addr_assign_type = NET_ADDR_SET;
3063 if (tb[IFLA_BROADCAST])
3064 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3065 nla_len(tb[IFLA_BROADCAST]));
3066 if (tb[IFLA_TXQLEN])
3067 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3068 if (tb[IFLA_OPERSTATE])
3069 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3070 if (tb[IFLA_LINKMODE])
3071 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3073 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3074 if (tb[IFLA_GSO_MAX_SIZE])
3075 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3076 if (tb[IFLA_GSO_MAX_SEGS])
3077 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3081 EXPORT_SYMBOL(rtnl_create_link);
3083 static int rtnl_group_changelink(const struct sk_buff *skb,
3084 struct net *net, int group,
3085 struct ifinfomsg *ifm,
3086 struct netlink_ext_ack *extack,
3089 struct net_device *dev, *aux;
3092 for_each_netdev_safe(net, dev, aux) {
3093 if (dev->group == group) {
3094 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3103 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3104 struct nlattr **attr, struct netlink_ext_ack *extack)
3106 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3107 unsigned char name_assign_type = NET_NAME_USER;
3108 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3109 const struct rtnl_link_ops *m_ops = NULL;
3110 struct net_device *master_dev = NULL;
3111 struct net *net = sock_net(skb->sk);
3112 const struct rtnl_link_ops *ops;
3113 struct nlattr *tb[IFLA_MAX + 1];
3114 struct net *dest_net, *link_net;
3115 struct nlattr **slave_data;
3116 char kind[MODULE_NAME_LEN];
3117 struct net_device *dev;
3118 struct ifinfomsg *ifm;
3119 char ifname[IFNAMSIZ];
3120 struct nlattr **data;
3123 #ifdef CONFIG_MODULES
3126 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3127 ifla_policy, extack);
3131 err = rtnl_ensure_unique_netns(tb, extack, false);
3135 if (tb[IFLA_IFNAME])
3136 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3140 ifm = nlmsg_data(nlh);
3141 if (ifm->ifi_index > 0)
3142 dev = __dev_get_by_index(net, ifm->ifi_index);
3143 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3144 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3149 master_dev = netdev_master_upper_dev_get(dev);
3151 m_ops = master_dev->rtnl_link_ops;
3154 err = validate_linkmsg(dev, tb);
3158 if (tb[IFLA_LINKINFO]) {
3159 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3161 ifla_info_policy, NULL);
3165 memset(linkinfo, 0, sizeof(linkinfo));
3167 if (linkinfo[IFLA_INFO_KIND]) {
3168 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3169 ops = rtnl_link_ops_get(kind);
3177 if (ops->maxtype > RTNL_MAX_TYPE)
3180 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3181 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3182 linkinfo[IFLA_INFO_DATA],
3183 ops->policy, extack);
3188 if (ops->validate) {
3189 err = ops->validate(tb, data, extack);
3197 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3200 if (m_ops->slave_maxtype &&
3201 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3202 err = nla_parse_nested_deprecated(slave_attr,
3203 m_ops->slave_maxtype,
3204 linkinfo[IFLA_INFO_SLAVE_DATA],
3205 m_ops->slave_policy,
3209 slave_data = slave_attr;
3216 if (nlh->nlmsg_flags & NLM_F_EXCL)
3218 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3221 if (linkinfo[IFLA_INFO_DATA]) {
3222 if (!ops || ops != dev->rtnl_link_ops ||
3226 err = ops->changelink(dev, tb, data, extack);
3229 status |= DO_SETLINK_NOTIFY;
3232 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3233 if (!m_ops || !m_ops->slave_changelink)
3236 err = m_ops->slave_changelink(master_dev, dev, tb,
3237 slave_data, extack);
3240 status |= DO_SETLINK_NOTIFY;
3243 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3246 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3247 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3248 return rtnl_group_changelink(skb, net,
3249 nla_get_u32(tb[IFLA_GROUP]),
3254 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3258 #ifdef CONFIG_MODULES
3261 request_module("rtnl-link-%s", kind);
3263 ops = rtnl_link_ops_get(kind);
3268 NL_SET_ERR_MSG(extack, "Unknown device type");
3276 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3277 name_assign_type = NET_NAME_ENUM;
3280 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3281 if (IS_ERR(dest_net))
3282 return PTR_ERR(dest_net);
3284 if (tb[IFLA_LINK_NETNSID]) {
3285 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3287 link_net = get_net_ns_by_id(dest_net, id);
3289 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3294 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3300 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3301 name_assign_type, ops, tb, extack);
3307 dev->ifindex = ifm->ifi_index;
3310 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3311 /* Drivers should call free_netdev() in ->destructor
3312 * and unregister it on failure after registration
3313 * so that device could be finally freed in rtnl_unlock.
3316 /* If device is not registered at all, free it now */
3317 if (dev->reg_state == NETREG_UNINITIALIZED)
3322 err = register_netdevice(dev);
3328 err = rtnl_configure_link(dev, ifm);
3330 goto out_unregister;
3332 err = dev_change_net_namespace(dev, dest_net, ifname);
3334 goto out_unregister;
3336 if (tb[IFLA_MASTER]) {
3337 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3339 goto out_unregister;
3348 LIST_HEAD(list_kill);
3350 ops->dellink(dev, &list_kill);
3351 unregister_netdevice_many(&list_kill);
3353 unregister_netdevice(dev);
3358 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3359 struct netlink_ext_ack *extack)
3361 struct nlattr **attr;
3364 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3368 ret = __rtnl_newlink(skb, nlh, attr, extack);
3373 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3374 const struct nlmsghdr *nlh,
3376 struct netlink_ext_ack *extack)
3378 struct ifinfomsg *ifm;
3381 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3382 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3386 if (!netlink_strict_get_check(skb))
3387 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3388 ifla_policy, extack);
3390 ifm = nlmsg_data(nlh);
3391 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3393 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3397 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3398 ifla_policy, extack);
3402 for (i = 0; i <= IFLA_MAX; i++) {
3408 case IFLA_ALT_IFNAME:
3410 case IFLA_TARGET_NETNSID:
3413 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3421 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3422 struct netlink_ext_ack *extack)
3424 struct net *net = sock_net(skb->sk);
3425 struct net *tgt_net = net;
3426 struct ifinfomsg *ifm;
3427 struct nlattr *tb[IFLA_MAX+1];
3428 struct net_device *dev = NULL;
3429 struct sk_buff *nskb;
3432 u32 ext_filter_mask = 0;
3434 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3438 err = rtnl_ensure_unique_netns(tb, extack, true);
3442 if (tb[IFLA_TARGET_NETNSID]) {
3443 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3444 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3445 if (IS_ERR(tgt_net))
3446 return PTR_ERR(tgt_net);
3449 if (tb[IFLA_EXT_MASK])
3450 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3453 ifm = nlmsg_data(nlh);
3454 if (ifm->ifi_index > 0)
3455 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3456 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3457 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3458 tb[IFLA_ALT_IFNAME], NULL);
3467 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3471 err = rtnl_fill_ifinfo(nskb, dev, net,
3472 RTM_NEWLINK, NETLINK_CB(skb).portid,
3473 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3474 0, NULL, 0, netnsid, GFP_KERNEL);
3476 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3477 WARN_ON(err == -EMSGSIZE);
3480 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3488 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3489 bool *changed, struct netlink_ext_ack *extack)
3494 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3498 alt_ifname = nla_data(attr);
3499 if (cmd == RTM_NEWLINKPROP) {
3500 alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
3503 err = netdev_name_node_alt_create(dev, alt_ifname);
3508 } else if (cmd == RTM_DELLINKPROP) {
3509 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3521 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3522 struct netlink_ext_ack *extack)
3524 struct net *net = sock_net(skb->sk);
3525 struct nlattr *tb[IFLA_MAX + 1];
3526 struct net_device *dev;
3527 struct ifinfomsg *ifm;
3528 bool changed = false;
3529 struct nlattr *attr;
3532 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3536 err = rtnl_ensure_unique_netns(tb, extack, true);
3540 ifm = nlmsg_data(nlh);
3541 if (ifm->ifi_index > 0)
3542 dev = __dev_get_by_index(net, ifm->ifi_index);
3543 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3544 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3545 tb[IFLA_ALT_IFNAME], NULL);
3552 if (!tb[IFLA_PROP_LIST])
3555 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3556 switch (nla_type(attr)) {
3557 case IFLA_ALT_IFNAME:
3558 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3566 netdev_state_change(dev);
3570 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3571 struct netlink_ext_ack *extack)
3573 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3576 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3577 struct netlink_ext_ack *extack)
3579 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3582 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3584 struct net *net = sock_net(skb->sk);
3585 struct net_device *dev;
3586 struct nlattr *tb[IFLA_MAX+1];
3587 u32 ext_filter_mask = 0;
3588 u16 min_ifinfo_dump_size = 0;
3591 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3592 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3593 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3595 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3596 if (tb[IFLA_EXT_MASK])
3597 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3600 if (!ext_filter_mask)
3601 return NLMSG_GOODSIZE;
3603 * traverse the list of net devices and compute the minimum
3604 * buffer size based upon the filter mask.
3607 for_each_netdev_rcu(net, dev) {
3608 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3614 return nlmsg_total_size(min_ifinfo_dump_size);
3617 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3620 int s_idx = cb->family;
3621 int type = cb->nlh->nlmsg_type - RTM_BASE;
3627 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3628 struct rtnl_link **tab;
3629 struct rtnl_link *link;
3630 rtnl_dumpit_func dumpit;
3632 if (idx < s_idx || idx == PF_PACKET)
3635 if (type < 0 || type >= RTM_NR_MSGTYPES)
3638 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3646 dumpit = link->dumpit;
3651 memset(&cb->args[0], 0, sizeof(cb->args));
3655 ret = dumpit(skb, cb);
3661 return skb->len ? : ret;
3664 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3665 unsigned int change,
3666 u32 event, gfp_t flags, int *new_nsid,
3669 struct net *net = dev_net(dev);
3670 struct sk_buff *skb;
3672 size_t if_info_size;
3674 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3678 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3679 type, 0, 0, change, 0, 0, event,
3680 new_nsid, new_ifindex, -1, flags);
3682 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3683 WARN_ON(err == -EMSGSIZE);
3690 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3694 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3696 struct net *net = dev_net(dev);
3698 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3701 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3702 unsigned int change, u32 event,
3703 gfp_t flags, int *new_nsid, int new_ifindex)
3705 struct sk_buff *skb;
3707 if (dev->reg_state != NETREG_REGISTERED)
3710 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3713 rtmsg_ifinfo_send(skb, dev, flags);
3716 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3719 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3723 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3724 gfp_t flags, int *new_nsid, int new_ifindex)
3726 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3727 new_nsid, new_ifindex);
3730 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3731 struct net_device *dev,
3732 u8 *addr, u16 vid, u32 pid, u32 seq,
3733 int type, unsigned int flags,
3734 int nlflags, u16 ndm_state)
3736 struct nlmsghdr *nlh;
3739 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3743 ndm = nlmsg_data(nlh);
3744 ndm->ndm_family = AF_BRIDGE;
3747 ndm->ndm_flags = flags;
3749 ndm->ndm_ifindex = dev->ifindex;
3750 ndm->ndm_state = ndm_state;
3752 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3753 goto nla_put_failure;
3755 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3756 goto nla_put_failure;
3758 nlmsg_end(skb, nlh);
3762 nlmsg_cancel(skb, nlh);
3766 static inline size_t rtnl_fdb_nlmsg_size(void)
3768 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3769 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3770 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3774 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3777 struct net *net = dev_net(dev);
3778 struct sk_buff *skb;
3781 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3785 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3786 0, 0, type, NTF_SELF, 0, ndm_state);
3792 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3795 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3799 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3801 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3802 struct nlattr *tb[],
3803 struct net_device *dev,
3804 const unsigned char *addr, u16 vid,
3809 /* If aging addresses are supported device will need to
3810 * implement its own handler for this.
3812 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3813 pr_info("%s: FDB only supports static addresses\n", dev->name);
3818 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3822 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3823 err = dev_uc_add_excl(dev, addr);
3824 else if (is_multicast_ether_addr(addr))
3825 err = dev_mc_add_excl(dev, addr);
3827 /* Only return duplicate errors if NLM_F_EXCL is set */
3828 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3833 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3835 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3836 struct netlink_ext_ack *extack)
3841 if (nla_len(vlan_attr) != sizeof(u16)) {
3842 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3846 vid = nla_get_u16(vlan_attr);
3848 if (!vid || vid >= VLAN_VID_MASK) {
3849 NL_SET_ERR_MSG(extack, "invalid vlan id");
3857 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3858 struct netlink_ext_ack *extack)
3860 struct net *net = sock_net(skb->sk);
3862 struct nlattr *tb[NDA_MAX+1];
3863 struct net_device *dev;
3868 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3873 ndm = nlmsg_data(nlh);
3874 if (ndm->ndm_ifindex == 0) {
3875 NL_SET_ERR_MSG(extack, "invalid ifindex");
3879 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3881 NL_SET_ERR_MSG(extack, "unknown ifindex");
3885 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3886 NL_SET_ERR_MSG(extack, "invalid address");
3890 if (dev->type != ARPHRD_ETHER) {
3891 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3895 addr = nla_data(tb[NDA_LLADDR]);
3897 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3903 /* Support fdb on master device the net/bridge default case */
3904 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3905 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3906 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3907 const struct net_device_ops *ops = br_dev->netdev_ops;
3909 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3910 nlh->nlmsg_flags, extack);
3914 ndm->ndm_flags &= ~NTF_MASTER;
3917 /* Embedded bridge, macvlan, and any other device support */
3918 if ((ndm->ndm_flags & NTF_SELF)) {
3919 if (dev->netdev_ops->ndo_fdb_add)
3920 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3925 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3929 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3931 ndm->ndm_flags &= ~NTF_SELF;
3939 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3941 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3942 struct nlattr *tb[],
3943 struct net_device *dev,
3944 const unsigned char *addr, u16 vid)
3948 /* If aging addresses are supported device will need to
3949 * implement its own handler for this.
3951 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3952 pr_info("%s: FDB only supports static addresses\n", dev->name);
3956 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3957 err = dev_uc_del(dev, addr);
3958 else if (is_multicast_ether_addr(addr))
3959 err = dev_mc_del(dev, addr);
3963 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3965 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3966 struct netlink_ext_ack *extack)
3968 struct net *net = sock_net(skb->sk);
3970 struct nlattr *tb[NDA_MAX+1];
3971 struct net_device *dev;
3976 if (!netlink_capable(skb, CAP_NET_ADMIN))
3979 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3984 ndm = nlmsg_data(nlh);
3985 if (ndm->ndm_ifindex == 0) {
3986 NL_SET_ERR_MSG(extack, "invalid ifindex");
3990 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3992 NL_SET_ERR_MSG(extack, "unknown ifindex");
3996 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3997 NL_SET_ERR_MSG(extack, "invalid address");
4001 if (dev->type != ARPHRD_ETHER) {
4002 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4006 addr = nla_data(tb[NDA_LLADDR]);
4008 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4014 /* Support fdb on master device the net/bridge default case */
4015 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4016 (dev->priv_flags & IFF_BRIDGE_PORT)) {
4017 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4018 const struct net_device_ops *ops = br_dev->netdev_ops;
4020 if (ops->ndo_fdb_del)
4021 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4026 ndm->ndm_flags &= ~NTF_MASTER;
4029 /* Embedded bridge, macvlan, and any other device support */
4030 if (ndm->ndm_flags & NTF_SELF) {
4031 if (dev->netdev_ops->ndo_fdb_del)
4032 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4035 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4038 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4040 ndm->ndm_flags &= ~NTF_SELF;
4047 static int nlmsg_populate_fdb(struct sk_buff *skb,
4048 struct netlink_callback *cb,
4049 struct net_device *dev,
4051 struct netdev_hw_addr_list *list)
4053 struct netdev_hw_addr *ha;
4057 portid = NETLINK_CB(cb->skb).portid;
4058 seq = cb->nlh->nlmsg_seq;
4060 list_for_each_entry(ha, &list->list, list) {
4061 if (*idx < cb->args[2])
4064 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4066 RTM_NEWNEIGH, NTF_SELF,
4067 NLM_F_MULTI, NUD_PERMANENT);
4077 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4078 * @skb: socket buffer to store message in
4079 * @cb: netlink callback
4081 * @filter_dev: ignored
4082 * @idx: the number of FDB table entries dumped is added to *@idx
4084 * Default netdevice operation to dump the existing unicast address list.
4085 * Returns number of addresses from list put in skb.
4087 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4088 struct netlink_callback *cb,
4089 struct net_device *dev,
4090 struct net_device *filter_dev,
4095 if (dev->type != ARPHRD_ETHER)
4098 netif_addr_lock_bh(dev);
4099 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4102 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4104 netif_addr_unlock_bh(dev);
4107 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4109 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4110 int *br_idx, int *brport_idx,
4111 struct netlink_ext_ack *extack)
4113 struct nlattr *tb[NDA_MAX + 1];
4117 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4118 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4122 ndm = nlmsg_data(nlh);
4123 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4124 ndm->ndm_flags || ndm->ndm_type) {
4125 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4129 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4130 NDA_MAX, NULL, extack);
4134 *brport_idx = ndm->ndm_ifindex;
4135 for (i = 0; i <= NDA_MAX; ++i) {
4141 if (nla_len(tb[i]) != sizeof(u32)) {
4142 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4145 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4148 if (nla_len(tb[i]) != sizeof(u32)) {
4149 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4152 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4155 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4163 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4164 int *br_idx, int *brport_idx,
4165 struct netlink_ext_ack *extack)
4167 struct nlattr *tb[IFLA_MAX+1];
4170 /* A hack to preserve kernel<->userspace interface.
4171 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4172 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4173 * So, check for ndmsg with an optional u32 attribute (not used here).
4174 * Fortunately these sizes don't conflict with the size of ifinfomsg
4175 * with an optional attribute.
4177 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4178 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4179 nla_attr_size(sizeof(u32)))) {
4180 struct ifinfomsg *ifm;
4182 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4183 tb, IFLA_MAX, ifla_policy,
4187 } else if (err == 0) {
4188 if (tb[IFLA_MASTER])
4189 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4192 ifm = nlmsg_data(nlh);
4193 *brport_idx = ifm->ifi_index;
4198 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4200 struct net_device *dev;
4201 struct net_device *br_dev = NULL;
4202 const struct net_device_ops *ops = NULL;
4203 const struct net_device_ops *cops = NULL;
4204 struct net *net = sock_net(skb->sk);
4205 struct hlist_head *head;
4213 if (cb->strict_check)
4214 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4217 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4223 br_dev = __dev_get_by_index(net, br_idx);
4227 ops = br_dev->netdev_ops;
4231 s_idx = cb->args[1];
4233 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4235 head = &net->dev_index_head[h];
4236 hlist_for_each_entry(dev, head, index_hlist) {
4238 if (brport_idx && (dev->ifindex != brport_idx))
4241 if (!br_idx) { /* user did not specify a specific bridge */
4242 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4243 br_dev = netdev_master_upper_dev_get(dev);
4244 cops = br_dev->netdev_ops;
4247 if (dev != br_dev &&
4248 !(dev->priv_flags & IFF_BRIDGE_PORT))
4251 if (br_dev != netdev_master_upper_dev_get(dev) &&
4252 !(dev->priv_flags & IFF_EBRIDGE))
4260 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4261 if (cops && cops->ndo_fdb_dump) {
4262 err = cops->ndo_fdb_dump(skb, cb,
4265 if (err == -EMSGSIZE)
4270 if (dev->netdev_ops->ndo_fdb_dump)
4271 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4275 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4277 if (err == -EMSGSIZE)
4282 /* reset fdb offset to 0 for rest of the interfaces */
4298 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4299 struct nlattr **tb, u8 *ndm_flags,
4300 int *br_idx, int *brport_idx, u8 **addr,
4301 u16 *vid, struct netlink_ext_ack *extack)
4306 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4307 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4311 ndm = nlmsg_data(nlh);
4312 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4314 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4318 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4319 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4323 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4324 NDA_MAX, nda_policy, extack);
4328 *ndm_flags = ndm->ndm_flags;
4329 *brport_idx = ndm->ndm_ifindex;
4330 for (i = 0; i <= NDA_MAX; ++i) {
4336 *br_idx = nla_get_u32(tb[i]);
4339 if (nla_len(tb[i]) != ETH_ALEN) {
4340 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4343 *addr = nla_data(tb[i]);
4346 err = fdb_vid_parse(tb[i], vid, extack);
4353 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4361 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4362 struct netlink_ext_ack *extack)
4364 struct net_device *dev = NULL, *br_dev = NULL;
4365 const struct net_device_ops *ops = NULL;
4366 struct net *net = sock_net(in_skb->sk);
4367 struct nlattr *tb[NDA_MAX + 1];
4368 struct sk_buff *skb;
4376 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4377 &brport_idx, &addr, &vid, extack);
4382 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4387 dev = __dev_get_by_index(net, brport_idx);
4389 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4396 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4400 br_dev = __dev_get_by_index(net, br_idx);
4402 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4405 ops = br_dev->netdev_ops;
4409 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4410 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4411 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4414 br_dev = netdev_master_upper_dev_get(dev);
4416 NL_SET_ERR_MSG(extack, "Master of device not found");
4419 ops = br_dev->netdev_ops;
4421 if (!(ndm_flags & NTF_SELF)) {
4422 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4425 ops = dev->netdev_ops;
4429 if (!br_dev && !dev) {
4430 NL_SET_ERR_MSG(extack, "No device specified");
4434 if (!ops || !ops->ndo_fdb_get) {
4435 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4439 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4445 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4446 NETLINK_CB(in_skb).portid,
4447 nlh->nlmsg_seq, extack);
4451 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4457 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4458 unsigned int attrnum, unsigned int flag)
4461 return nla_put_u8(skb, attrnum, !!(flags & flag));
4465 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4466 struct net_device *dev, u16 mode,
4467 u32 flags, u32 mask, int nlflags,
4469 int (*vlan_fill)(struct sk_buff *skb,
4470 struct net_device *dev,
4473 struct nlmsghdr *nlh;
4474 struct ifinfomsg *ifm;
4475 struct nlattr *br_afspec;
4476 struct nlattr *protinfo;
4477 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4478 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4481 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4485 ifm = nlmsg_data(nlh);
4486 ifm->ifi_family = AF_BRIDGE;
4488 ifm->ifi_type = dev->type;
4489 ifm->ifi_index = dev->ifindex;
4490 ifm->ifi_flags = dev_get_flags(dev);
4491 ifm->ifi_change = 0;
4494 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4495 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4496 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4498 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4500 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4501 (dev->ifindex != dev_get_iflink(dev) &&
4502 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4503 goto nla_put_failure;
4505 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4507 goto nla_put_failure;
4509 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4510 nla_nest_cancel(skb, br_afspec);
4511 goto nla_put_failure;
4514 if (mode != BRIDGE_MODE_UNDEF) {
4515 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4516 nla_nest_cancel(skb, br_afspec);
4517 goto nla_put_failure;
4521 err = vlan_fill(skb, dev, filter_mask);
4523 nla_nest_cancel(skb, br_afspec);
4524 goto nla_put_failure;
4527 nla_nest_end(skb, br_afspec);
4529 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4531 goto nla_put_failure;
4533 if (brport_nla_put_flag(skb, flags, mask,
4534 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4535 brport_nla_put_flag(skb, flags, mask,
4536 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4537 brport_nla_put_flag(skb, flags, mask,
4538 IFLA_BRPORT_FAST_LEAVE,
4539 BR_MULTICAST_FAST_LEAVE) ||
4540 brport_nla_put_flag(skb, flags, mask,
4541 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4542 brport_nla_put_flag(skb, flags, mask,
4543 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4544 brport_nla_put_flag(skb, flags, mask,
4545 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4546 brport_nla_put_flag(skb, flags, mask,
4547 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4548 brport_nla_put_flag(skb, flags, mask,
4549 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4550 nla_nest_cancel(skb, protinfo);
4551 goto nla_put_failure;
4554 nla_nest_end(skb, protinfo);
4556 nlmsg_end(skb, nlh);
4559 nlmsg_cancel(skb, nlh);
4560 return err ? err : -EMSGSIZE;
4562 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4564 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4565 bool strict_check, u32 *filter_mask,
4566 struct netlink_ext_ack *extack)
4568 struct nlattr *tb[IFLA_MAX+1];
4572 struct ifinfomsg *ifm;
4574 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4575 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4579 ifm = nlmsg_data(nlh);
4580 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4581 ifm->ifi_change || ifm->ifi_index) {
4582 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4586 err = nlmsg_parse_deprecated_strict(nlh,
4587 sizeof(struct ifinfomsg),
4588 tb, IFLA_MAX, ifla_policy,
4591 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4592 tb, IFLA_MAX, ifla_policy,
4598 /* new attributes should only be added with strict checking */
4599 for (i = 0; i <= IFLA_MAX; ++i) {
4605 *filter_mask = nla_get_u32(tb[i]);
4609 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4618 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4620 const struct nlmsghdr *nlh = cb->nlh;
4621 struct net *net = sock_net(skb->sk);
4622 struct net_device *dev;
4624 u32 portid = NETLINK_CB(cb->skb).portid;
4625 u32 seq = nlh->nlmsg_seq;
4626 u32 filter_mask = 0;
4629 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4631 if (err < 0 && cb->strict_check)
4635 for_each_netdev_rcu(net, dev) {
4636 const struct net_device_ops *ops = dev->netdev_ops;
4637 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4639 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4640 if (idx >= cb->args[0]) {
4641 err = br_dev->netdev_ops->ndo_bridge_getlink(
4642 skb, portid, seq, dev,
4643 filter_mask, NLM_F_MULTI);
4644 if (err < 0 && err != -EOPNOTSUPP) {
4645 if (likely(skb->len))
4654 if (ops->ndo_bridge_getlink) {
4655 if (idx >= cb->args[0]) {
4656 err = ops->ndo_bridge_getlink(skb, portid,
4660 if (err < 0 && err != -EOPNOTSUPP) {
4661 if (likely(skb->len))
4678 static inline size_t bridge_nlmsg_size(void)
4680 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4681 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4682 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4683 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4684 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4685 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4686 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4687 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4688 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4689 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4690 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4693 static int rtnl_bridge_notify(struct net_device *dev)
4695 struct net *net = dev_net(dev);
4696 struct sk_buff *skb;
4697 int err = -EOPNOTSUPP;
4699 if (!dev->netdev_ops->ndo_bridge_getlink)
4702 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4708 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4715 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4718 WARN_ON(err == -EMSGSIZE);
4721 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4725 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4726 struct netlink_ext_ack *extack)
4728 struct net *net = sock_net(skb->sk);
4729 struct ifinfomsg *ifm;
4730 struct net_device *dev;
4731 struct nlattr *br_spec, *attr = NULL;
4732 int rem, err = -EOPNOTSUPP;
4734 bool have_flags = false;
4736 if (nlmsg_len(nlh) < sizeof(*ifm))
4739 ifm = nlmsg_data(nlh);
4740 if (ifm->ifi_family != AF_BRIDGE)
4741 return -EPFNOSUPPORT;
4743 dev = __dev_get_by_index(net, ifm->ifi_index);
4745 NL_SET_ERR_MSG(extack, "unknown ifindex");
4749 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4751 nla_for_each_nested(attr, br_spec, rem) {
4752 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4753 if (nla_len(attr) < sizeof(flags))
4757 flags = nla_get_u16(attr);
4763 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4764 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4766 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4771 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4776 flags &= ~BRIDGE_FLAGS_MASTER;
4779 if ((flags & BRIDGE_FLAGS_SELF)) {
4780 if (!dev->netdev_ops->ndo_bridge_setlink)
4783 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4787 flags &= ~BRIDGE_FLAGS_SELF;
4789 /* Generate event to notify upper layer of bridge
4792 err = rtnl_bridge_notify(dev);
4797 memcpy(nla_data(attr), &flags, sizeof(flags));
4802 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4803 struct netlink_ext_ack *extack)
4805 struct net *net = sock_net(skb->sk);
4806 struct ifinfomsg *ifm;
4807 struct net_device *dev;
4808 struct nlattr *br_spec, *attr = NULL;
4809 int rem, err = -EOPNOTSUPP;
4811 bool have_flags = false;
4813 if (nlmsg_len(nlh) < sizeof(*ifm))
4816 ifm = nlmsg_data(nlh);
4817 if (ifm->ifi_family != AF_BRIDGE)
4818 return -EPFNOSUPPORT;
4820 dev = __dev_get_by_index(net, ifm->ifi_index);
4822 NL_SET_ERR_MSG(extack, "unknown ifindex");
4826 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4828 nla_for_each_nested(attr, br_spec, rem) {
4829 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4830 if (nla_len(attr) < sizeof(flags))
4834 flags = nla_get_u16(attr);
4840 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4841 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4843 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4848 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4852 flags &= ~BRIDGE_FLAGS_MASTER;
4855 if ((flags & BRIDGE_FLAGS_SELF)) {
4856 if (!dev->netdev_ops->ndo_bridge_dellink)
4859 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4863 flags &= ~BRIDGE_FLAGS_SELF;
4865 /* Generate event to notify upper layer of bridge
4868 err = rtnl_bridge_notify(dev);
4873 memcpy(nla_data(attr), &flags, sizeof(flags));
4878 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4880 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4881 (!idxattr || idxattr == attrid);
4884 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4885 static int rtnl_get_offload_stats_attr_size(int attr_id)
4888 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4889 return sizeof(struct rtnl_link_stats64);
4895 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4898 struct nlattr *attr = NULL;
4903 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4904 dev->netdev_ops->ndo_get_offload_stats))
4907 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4908 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4909 if (attr_id < *prividx)
4912 size = rtnl_get_offload_stats_attr_size(attr_id);
4916 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4919 attr = nla_reserve_64bit(skb, attr_id, size,
4920 IFLA_OFFLOAD_XSTATS_UNSPEC);
4922 goto nla_put_failure;
4924 attr_data = nla_data(attr);
4925 memset(attr_data, 0, size);
4926 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4929 goto get_offload_stats_failure;
4940 get_offload_stats_failure:
4945 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4951 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4952 dev->netdev_ops->ndo_get_offload_stats))
4955 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4956 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4957 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4959 size = rtnl_get_offload_stats_attr_size(attr_id);
4960 nla_size += nla_total_size_64bit(size);
4964 nla_size += nla_total_size(0);
4969 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4970 int type, u32 pid, u32 seq, u32 change,
4971 unsigned int flags, unsigned int filter_mask,
4972 int *idxattr, int *prividx)
4974 struct if_stats_msg *ifsm;
4975 struct nlmsghdr *nlh;
4976 struct nlattr *attr;
4977 int s_prividx = *prividx;
4982 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4986 ifsm = nlmsg_data(nlh);
4987 ifsm->family = PF_UNSPEC;
4990 ifsm->ifindex = dev->ifindex;
4991 ifsm->filter_mask = filter_mask;
4993 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4994 struct rtnl_link_stats64 *sp;
4996 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4997 sizeof(struct rtnl_link_stats64),
5000 goto nla_put_failure;
5002 sp = nla_data(attr);
5003 dev_get_stats(dev, sp);
5006 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5007 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5009 if (ops && ops->fill_linkxstats) {
5010 *idxattr = IFLA_STATS_LINK_XSTATS;
5011 attr = nla_nest_start_noflag(skb,
5012 IFLA_STATS_LINK_XSTATS);
5014 goto nla_put_failure;
5016 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5017 nla_nest_end(skb, attr);
5019 goto nla_put_failure;
5024 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5026 const struct rtnl_link_ops *ops = NULL;
5027 const struct net_device *master;
5029 master = netdev_master_upper_dev_get(dev);
5031 ops = master->rtnl_link_ops;
5032 if (ops && ops->fill_linkxstats) {
5033 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5034 attr = nla_nest_start_noflag(skb,
5035 IFLA_STATS_LINK_XSTATS_SLAVE);
5037 goto nla_put_failure;
5039 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5040 nla_nest_end(skb, attr);
5042 goto nla_put_failure;
5047 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5049 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5050 attr = nla_nest_start_noflag(skb,
5051 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5053 goto nla_put_failure;
5055 err = rtnl_get_offload_stats(skb, dev, prividx);
5056 if (err == -ENODATA)
5057 nla_nest_cancel(skb, attr);
5059 nla_nest_end(skb, attr);
5061 if (err && err != -ENODATA)
5062 goto nla_put_failure;
5066 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5067 struct rtnl_af_ops *af_ops;
5069 *idxattr = IFLA_STATS_AF_SPEC;
5070 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5072 goto nla_put_failure;
5075 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5076 if (af_ops->fill_stats_af) {
5080 af = nla_nest_start_noflag(skb,
5084 goto nla_put_failure;
5086 err = af_ops->fill_stats_af(skb, dev);
5088 if (err == -ENODATA) {
5089 nla_nest_cancel(skb, af);
5090 } else if (err < 0) {
5092 goto nla_put_failure;
5095 nla_nest_end(skb, af);
5100 nla_nest_end(skb, attr);
5105 nlmsg_end(skb, nlh);
5110 /* not a multi message or no progress mean a real error */
5111 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5112 nlmsg_cancel(skb, nlh);
5114 nlmsg_end(skb, nlh);
5119 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5124 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5125 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5127 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5128 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5129 int attr = IFLA_STATS_LINK_XSTATS;
5131 if (ops && ops->get_linkxstats_size) {
5132 size += nla_total_size(ops->get_linkxstats_size(dev,
5134 /* for IFLA_STATS_LINK_XSTATS */
5135 size += nla_total_size(0);
5139 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5140 struct net_device *_dev = (struct net_device *)dev;
5141 const struct rtnl_link_ops *ops = NULL;
5142 const struct net_device *master;
5144 /* netdev_master_upper_dev_get can't take const */
5145 master = netdev_master_upper_dev_get(_dev);
5147 ops = master->rtnl_link_ops;
5148 if (ops && ops->get_linkxstats_size) {
5149 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5151 size += nla_total_size(ops->get_linkxstats_size(dev,
5153 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5154 size += nla_total_size(0);
5158 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5159 size += rtnl_get_offload_stats_size(dev);
5161 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5162 struct rtnl_af_ops *af_ops;
5164 /* for IFLA_STATS_AF_SPEC */
5165 size += nla_total_size(0);
5168 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5169 if (af_ops->get_stats_af_size) {
5170 size += nla_total_size(
5171 af_ops->get_stats_af_size(dev));
5174 size += nla_total_size(0);
5183 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5184 bool is_dump, struct netlink_ext_ack *extack)
5186 struct if_stats_msg *ifsm;
5188 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5189 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5196 ifsm = nlmsg_data(nlh);
5198 /* only requests using strict checks can pass data to influence
5199 * the dump. The legacy exception is filter_mask.
5201 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5202 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5205 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5206 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5209 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5210 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5217 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5218 struct netlink_ext_ack *extack)
5220 struct net *net = sock_net(skb->sk);
5221 struct net_device *dev = NULL;
5222 int idxattr = 0, prividx = 0;
5223 struct if_stats_msg *ifsm;
5224 struct sk_buff *nskb;
5228 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5233 ifsm = nlmsg_data(nlh);
5234 if (ifsm->ifindex > 0)
5235 dev = __dev_get_by_index(net, ifsm->ifindex);
5242 filter_mask = ifsm->filter_mask;
5246 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5250 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5251 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5252 0, filter_mask, &idxattr, &prividx);
5254 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5255 WARN_ON(err == -EMSGSIZE);
5258 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5264 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5266 struct netlink_ext_ack *extack = cb->extack;
5267 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5268 struct net *net = sock_net(skb->sk);
5269 unsigned int flags = NLM_F_MULTI;
5270 struct if_stats_msg *ifsm;
5271 struct hlist_head *head;
5272 struct net_device *dev;
5273 u32 filter_mask = 0;
5277 s_idx = cb->args[1];
5278 s_idxattr = cb->args[2];
5279 s_prividx = cb->args[3];
5281 cb->seq = net->dev_base_seq;
5283 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5287 ifsm = nlmsg_data(cb->nlh);
5288 filter_mask = ifsm->filter_mask;
5290 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5294 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5296 head = &net->dev_index_head[h];
5297 hlist_for_each_entry(dev, head, index_hlist) {
5300 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5301 NETLINK_CB(cb->skb).portid,
5302 cb->nlh->nlmsg_seq, 0,
5304 &s_idxattr, &s_prividx);
5305 /* If we ran out of room on the first message,
5308 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5314 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5320 cb->args[3] = s_prividx;
5321 cb->args[2] = s_idxattr;
5328 /* Process one rtnetlink message. */
5330 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5331 struct netlink_ext_ack *extack)
5333 struct net *net = sock_net(skb->sk);
5334 struct rtnl_link *link;
5335 struct module *owner;
5336 int err = -EOPNOTSUPP;
5337 rtnl_doit_func doit;
5343 type = nlh->nlmsg_type;
5349 /* All the messages must have at least 1 byte length */
5350 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5353 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5356 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5360 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5362 rtnl_dumpit_func dumpit;
5363 u16 min_dump_alloc = 0;
5365 link = rtnl_get_link(family, type);
5366 if (!link || !link->dumpit) {
5368 link = rtnl_get_link(family, type);
5369 if (!link || !link->dumpit)
5372 owner = link->owner;
5373 dumpit = link->dumpit;
5375 if (type == RTM_GETLINK - RTM_BASE)
5376 min_dump_alloc = rtnl_calcit(skb, nlh);
5379 /* need to do this before rcu_read_unlock() */
5380 if (!try_module_get(owner))
5381 err = -EPROTONOSUPPORT;
5387 struct netlink_dump_control c = {
5389 .min_dump_alloc = min_dump_alloc,
5392 err = netlink_dump_start(rtnl, skb, nlh, &c);
5393 /* netlink_dump_start() will keep a reference on
5394 * module if dump is still in progress.
5401 link = rtnl_get_link(family, type);
5402 if (!link || !link->doit) {
5404 link = rtnl_get_link(PF_UNSPEC, type);
5405 if (!link || !link->doit)
5409 owner = link->owner;
5410 if (!try_module_get(owner)) {
5411 err = -EPROTONOSUPPORT;
5415 flags = link->flags;
5416 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5420 err = doit(skb, nlh, extack);
5427 link = rtnl_get_link(family, type);
5428 if (link && link->doit)
5429 err = link->doit(skb, nlh, extack);
5445 static void rtnetlink_rcv(struct sk_buff *skb)
5447 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5450 static int rtnetlink_bind(struct net *net, int group)
5453 case RTNLGRP_IPV4_MROUTE_R:
5454 case RTNLGRP_IPV6_MROUTE_R:
5455 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5462 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5464 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5468 case NETDEV_CHANGEMTU:
5469 case NETDEV_CHANGEADDR:
5470 case NETDEV_CHANGENAME:
5471 case NETDEV_FEAT_CHANGE:
5472 case NETDEV_BONDING_FAILOVER:
5473 case NETDEV_POST_TYPE_CHANGE:
5474 case NETDEV_NOTIFY_PEERS:
5475 case NETDEV_CHANGEUPPER:
5476 case NETDEV_RESEND_IGMP:
5477 case NETDEV_CHANGEINFODATA:
5478 case NETDEV_CHANGELOWERSTATE:
5479 case NETDEV_CHANGE_TX_QUEUE_LEN:
5480 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5481 GFP_KERNEL, NULL, 0);
5489 static struct notifier_block rtnetlink_dev_notifier = {
5490 .notifier_call = rtnetlink_event,
5494 static int __net_init rtnetlink_net_init(struct net *net)
5497 struct netlink_kernel_cfg cfg = {
5498 .groups = RTNLGRP_MAX,
5499 .input = rtnetlink_rcv,
5500 .cb_mutex = &rtnl_mutex,
5501 .flags = NL_CFG_F_NONROOT_RECV,
5502 .bind = rtnetlink_bind,
5505 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5512 static void __net_exit rtnetlink_net_exit(struct net *net)
5514 netlink_kernel_release(net->rtnl);
5518 static struct pernet_operations rtnetlink_net_ops = {
5519 .init = rtnetlink_net_init,
5520 .exit = rtnetlink_net_exit,
5523 void __init rtnetlink_init(void)
5525 if (register_pernet_subsys(&rtnetlink_net_ops))
5526 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5528 register_netdevice_notifier(&rtnetlink_dev_notifier);
5530 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5531 rtnl_dump_ifinfo, 0);
5532 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5533 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5534 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5536 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5537 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5538 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5540 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5541 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5543 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5544 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5545 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5547 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5548 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5549 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5551 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,