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)
1047 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1049 struct nlattr *vf_ports;
1050 struct nlattr *vf_port;
1054 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1058 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1059 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1061 goto nla_put_failure;
1062 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1063 goto nla_put_failure;
1064 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1065 if (err == -EMSGSIZE)
1066 goto nla_put_failure;
1068 nla_nest_cancel(skb, vf_port);
1071 nla_nest_end(skb, vf_port);
1074 nla_nest_end(skb, vf_ports);
1079 nla_nest_cancel(skb, vf_ports);
1083 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1085 struct nlattr *port_self;
1088 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1092 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1094 nla_nest_cancel(skb, port_self);
1095 return (err == -EMSGSIZE) ? err : 0;
1098 nla_nest_end(skb, port_self);
1103 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1104 u32 ext_filter_mask)
1108 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1109 !(ext_filter_mask & RTEXT_FILTER_VF))
1112 err = rtnl_port_self_fill(skb, dev);
1116 if (dev_num_vf(dev->dev.parent)) {
1117 err = rtnl_vf_ports_fill(skb, dev);
1125 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1128 struct netdev_phys_item_id ppid;
1130 err = dev_get_phys_port_id(dev, &ppid);
1132 if (err == -EOPNOTSUPP)
1137 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1143 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1145 char name[IFNAMSIZ];
1148 err = dev_get_phys_port_name(dev, name, sizeof(name));
1150 if (err == -EOPNOTSUPP)
1155 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1161 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1163 struct netdev_phys_item_id ppid = { };
1166 err = dev_get_port_parent_id(dev, &ppid, false);
1168 if (err == -EOPNOTSUPP)
1173 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1179 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1180 struct net_device *dev)
1182 struct rtnl_link_stats64 *sp;
1183 struct nlattr *attr;
1185 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1186 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1190 sp = nla_data(attr);
1191 dev_get_stats(dev, sp);
1193 attr = nla_reserve(skb, IFLA_STATS,
1194 sizeof(struct rtnl_link_stats));
1198 copy_rtnl_link_stats(nla_data(attr), sp);
1203 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1204 struct net_device *dev,
1206 struct nlattr *vfinfo)
1208 struct ifla_vf_rss_query_en vf_rss_query_en;
1209 struct nlattr *vf, *vfstats, *vfvlanlist;
1210 struct ifla_vf_link_state vf_linkstate;
1211 struct ifla_vf_vlan_info vf_vlan_info;
1212 struct ifla_vf_spoofchk vf_spoofchk;
1213 struct ifla_vf_tx_rate vf_tx_rate;
1214 struct ifla_vf_stats vf_stats;
1215 struct ifla_vf_trust vf_trust;
1216 struct ifla_vf_vlan vf_vlan;
1217 struct ifla_vf_rate vf_rate;
1218 struct ifla_vf_mac vf_mac;
1219 struct ifla_vf_broadcast vf_broadcast;
1220 struct ifla_vf_info ivi;
1221 struct ifla_vf_guid node_guid;
1222 struct ifla_vf_guid port_guid;
1224 memset(&ivi, 0, sizeof(ivi));
1226 /* Not all SR-IOV capable drivers support the
1227 * spoofcheck and "RSS query enable" query. Preset to
1228 * -1 so the user space tool can detect that the driver
1229 * didn't report anything.
1232 ivi.rss_query_en = -1;
1234 /* The default value for VF link state is "auto"
1235 * IFLA_VF_LINK_STATE_AUTO which equals zero
1238 /* VLAN Protocol by default is 802.1Q */
1239 ivi.vlan_proto = htons(ETH_P_8021Q);
1240 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1243 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1252 vf_rss_query_en.vf =
1255 port_guid.vf = ivi.vf;
1257 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1258 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1259 vf_vlan.vlan = ivi.vlan;
1260 vf_vlan.qos = ivi.qos;
1261 vf_vlan_info.vlan = ivi.vlan;
1262 vf_vlan_info.qos = ivi.qos;
1263 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1264 vf_tx_rate.rate = ivi.max_tx_rate;
1265 vf_rate.min_tx_rate = ivi.min_tx_rate;
1266 vf_rate.max_tx_rate = ivi.max_tx_rate;
1267 vf_spoofchk.setting = ivi.spoofchk;
1268 vf_linkstate.link_state = ivi.linkstate;
1269 vf_rss_query_en.setting = ivi.rss_query_en;
1270 vf_trust.setting = ivi.trusted;
1271 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1273 goto nla_put_vfinfo_failure;
1274 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1275 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1276 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1277 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1279 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1281 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1283 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1285 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1286 sizeof(vf_rss_query_en),
1287 &vf_rss_query_en) ||
1288 nla_put(skb, IFLA_VF_TRUST,
1289 sizeof(vf_trust), &vf_trust))
1290 goto nla_put_vf_failure;
1292 memset(&node_guid, 0, sizeof(node_guid));
1293 memset(&port_guid, 0, sizeof(port_guid));
1294 if (dev->netdev_ops->ndo_get_vf_guid &&
1295 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1297 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1299 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1301 goto nla_put_vf_failure;
1303 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1305 goto nla_put_vf_failure;
1306 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1308 nla_nest_cancel(skb, vfvlanlist);
1309 goto nla_put_vf_failure;
1311 nla_nest_end(skb, vfvlanlist);
1312 memset(&vf_stats, 0, sizeof(vf_stats));
1313 if (dev->netdev_ops->ndo_get_vf_stats)
1314 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1316 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1318 goto nla_put_vf_failure;
1319 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1320 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1321 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1322 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1323 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1324 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1325 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1326 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1327 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1328 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1329 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1330 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1331 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1332 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1333 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1334 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1335 nla_nest_cancel(skb, vfstats);
1336 goto nla_put_vf_failure;
1338 nla_nest_end(skb, vfstats);
1339 nla_nest_end(skb, vf);
1343 nla_nest_cancel(skb, vf);
1344 nla_put_vfinfo_failure:
1345 nla_nest_cancel(skb, vfinfo);
1349 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1350 struct net_device *dev,
1351 u32 ext_filter_mask)
1353 struct nlattr *vfinfo;
1356 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1359 num_vfs = dev_num_vf(dev->dev.parent);
1360 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1363 if (!dev->netdev_ops->ndo_get_vf_config)
1366 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1370 for (i = 0; i < num_vfs; i++) {
1371 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1375 nla_nest_end(skb, vfinfo);
1379 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1381 struct rtnl_link_ifmap map;
1383 memset(&map, 0, sizeof(map));
1384 map.mem_start = dev->mem_start;
1385 map.mem_end = dev->mem_end;
1386 map.base_addr = dev->base_addr;
1389 map.port = dev->if_port;
1391 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1397 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1399 const struct bpf_prog *generic_xdp_prog;
1403 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1404 if (!generic_xdp_prog)
1406 return generic_xdp_prog->aux->id;
1409 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1411 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1414 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1416 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1420 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1421 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1422 u32 (*get_prog_id)(struct net_device *dev))
1427 curr_id = get_prog_id(dev);
1432 err = nla_put_u32(skb, attr, curr_id);
1436 if (*mode != XDP_ATTACHED_NONE)
1437 *mode = XDP_ATTACHED_MULTI;
1444 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1451 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1456 mode = XDP_ATTACHED_NONE;
1457 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1458 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1461 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1462 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1465 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1466 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1470 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1474 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1475 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1480 nla_nest_end(skb, xdp);
1484 nla_nest_cancel(skb, xdp);
1488 static u32 rtnl_get_event(unsigned long event)
1490 u32 rtnl_event_type = IFLA_EVENT_NONE;
1494 rtnl_event_type = IFLA_EVENT_REBOOT;
1496 case NETDEV_FEAT_CHANGE:
1497 rtnl_event_type = IFLA_EVENT_FEATURES;
1499 case NETDEV_BONDING_FAILOVER:
1500 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1502 case NETDEV_NOTIFY_PEERS:
1503 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1505 case NETDEV_RESEND_IGMP:
1506 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1508 case NETDEV_CHANGEINFODATA:
1509 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1515 return rtnl_event_type;
1518 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1520 const struct net_device *upper_dev;
1525 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1527 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1533 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1536 int ifindex = dev_get_iflink(dev);
1538 if (force || dev->ifindex != ifindex)
1539 return nla_put_u32(skb, IFLA_LINK, ifindex);
1544 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1545 struct net_device *dev)
1550 ret = dev_get_alias(dev, buf, sizeof(buf));
1551 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1554 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1555 const struct net_device *dev,
1556 struct net *src_net, gfp_t gfp)
1558 bool put_iflink = false;
1560 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1561 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1563 if (!net_eq(dev_net(dev), link_net)) {
1564 int id = peernet2id_alloc(src_net, link_net, gfp);
1566 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1573 return nla_put_iflink(skb, dev, put_iflink);
1576 static int rtnl_fill_link_af(struct sk_buff *skb,
1577 const struct net_device *dev,
1578 u32 ext_filter_mask)
1580 const struct rtnl_af_ops *af_ops;
1581 struct nlattr *af_spec;
1583 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1587 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1591 if (!af_ops->fill_link_af)
1594 af = nla_nest_start_noflag(skb, af_ops->family);
1598 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1600 * Caller may return ENODATA to indicate that there
1601 * was no data to be dumped. This is not an error, it
1602 * means we should trim the attribute header and
1605 if (err == -ENODATA)
1606 nla_nest_cancel(skb, af);
1610 nla_nest_end(skb, af);
1613 nla_nest_end(skb, af_spec);
1617 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1618 const struct net_device *dev)
1620 struct netdev_name_node *name_node;
1623 list_for_each_entry(name_node, &dev->name_node->list, list) {
1624 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1631 static int rtnl_fill_prop_list(struct sk_buff *skb,
1632 const struct net_device *dev)
1634 struct nlattr *prop_list;
1637 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1641 ret = rtnl_fill_alt_ifnames(skb, dev);
1645 nla_nest_end(skb, prop_list);
1649 nla_nest_cancel(skb, prop_list);
1653 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1654 struct net_device *dev, struct net *src_net,
1655 int type, u32 pid, u32 seq, u32 change,
1656 unsigned int flags, u32 ext_filter_mask,
1657 u32 event, int *new_nsid, int new_ifindex,
1658 int tgt_netnsid, gfp_t gfp)
1660 struct ifinfomsg *ifm;
1661 struct nlmsghdr *nlh;
1664 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1668 ifm = nlmsg_data(nlh);
1669 ifm->ifi_family = AF_UNSPEC;
1671 ifm->ifi_type = dev->type;
1672 ifm->ifi_index = dev->ifindex;
1673 ifm->ifi_flags = dev_get_flags(dev);
1674 ifm->ifi_change = change;
1676 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1677 goto nla_put_failure;
1679 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1680 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1681 nla_put_u8(skb, IFLA_OPERSTATE,
1682 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1683 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1684 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1685 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1686 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1687 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1688 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1689 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1690 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1691 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1693 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1695 put_master_ifindex(skb, dev) ||
1696 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1698 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1699 nla_put_ifalias(skb, dev) ||
1700 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1701 atomic_read(&dev->carrier_up_count) +
1702 atomic_read(&dev->carrier_down_count)) ||
1703 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1704 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1705 atomic_read(&dev->carrier_up_count)) ||
1706 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1707 atomic_read(&dev->carrier_down_count)))
1708 goto nla_put_failure;
1710 if (event != IFLA_EVENT_NONE) {
1711 if (nla_put_u32(skb, IFLA_EVENT, event))
1712 goto nla_put_failure;
1715 if (rtnl_fill_link_ifmap(skb, dev))
1716 goto nla_put_failure;
1718 if (dev->addr_len) {
1719 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1720 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1721 goto nla_put_failure;
1724 if (rtnl_phys_port_id_fill(skb, dev))
1725 goto nla_put_failure;
1727 if (rtnl_phys_port_name_fill(skb, dev))
1728 goto nla_put_failure;
1730 if (rtnl_phys_switch_id_fill(skb, dev))
1731 goto nla_put_failure;
1733 if (rtnl_fill_stats(skb, dev))
1734 goto nla_put_failure;
1736 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1737 goto nla_put_failure;
1739 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1740 goto nla_put_failure;
1742 if (rtnl_xdp_fill(skb, dev))
1743 goto nla_put_failure;
1745 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1746 if (rtnl_link_fill(skb, dev) < 0)
1747 goto nla_put_failure;
1750 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1751 goto nla_put_failure;
1754 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1755 goto nla_put_failure;
1757 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1758 goto nla_put_failure;
1762 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1763 goto nla_put_failure_rcu;
1766 if (rtnl_fill_prop_list(skb, dev))
1767 goto nla_put_failure;
1769 nlmsg_end(skb, nlh);
1772 nla_put_failure_rcu:
1775 nlmsg_cancel(skb, nlh);
1779 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1780 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1781 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1782 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1783 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1784 [IFLA_MTU] = { .type = NLA_U32 },
1785 [IFLA_LINK] = { .type = NLA_U32 },
1786 [IFLA_MASTER] = { .type = NLA_U32 },
1787 [IFLA_CARRIER] = { .type = NLA_U8 },
1788 [IFLA_TXQLEN] = { .type = NLA_U32 },
1789 [IFLA_WEIGHT] = { .type = NLA_U32 },
1790 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1791 [IFLA_LINKMODE] = { .type = NLA_U8 },
1792 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1793 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1794 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1795 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1796 * allow 0-length string (needed to remove an alias).
1798 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1799 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1800 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1801 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1802 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1803 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1804 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1805 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1806 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1807 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1808 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1809 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1810 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1811 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1812 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1813 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1814 [IFLA_XDP] = { .type = NLA_NESTED },
1815 [IFLA_EVENT] = { .type = NLA_U32 },
1816 [IFLA_GROUP] = { .type = NLA_U32 },
1817 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1818 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1819 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1820 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1821 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1822 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1823 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1824 .len = ALTIFNAMSIZ - 1 },
1827 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1828 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1829 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1830 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1831 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1834 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1835 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1836 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1837 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1838 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1839 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1840 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1841 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1842 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1843 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1844 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1845 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1846 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1847 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1850 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1851 [IFLA_PORT_VF] = { .type = NLA_U32 },
1852 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1853 .len = PORT_PROFILE_MAX },
1854 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1855 .len = PORT_UUID_MAX },
1856 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1857 .len = PORT_UUID_MAX },
1858 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1859 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1861 /* Unused, but we need to keep it here since user space could
1862 * fill it. It's also broken with regard to NLA_BINARY use in
1863 * combination with structs.
1865 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1866 .len = sizeof(struct ifla_port_vsi) },
1869 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1870 [IFLA_XDP_FD] = { .type = NLA_S32 },
1871 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1872 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1873 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1876 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1878 const struct rtnl_link_ops *ops = NULL;
1879 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1881 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1884 if (linfo[IFLA_INFO_KIND]) {
1885 char kind[MODULE_NAME_LEN];
1887 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1888 ops = rtnl_link_ops_get(kind);
1894 static bool link_master_filtered(struct net_device *dev, int master_idx)
1896 struct net_device *master;
1901 master = netdev_master_upper_dev_get(dev);
1902 if (!master || master->ifindex != master_idx)
1908 static bool link_kind_filtered(const struct net_device *dev,
1909 const struct rtnl_link_ops *kind_ops)
1911 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1917 static bool link_dump_filtered(struct net_device *dev,
1919 const struct rtnl_link_ops *kind_ops)
1921 if (link_master_filtered(dev, master_idx) ||
1922 link_kind_filtered(dev, kind_ops))
1929 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1930 * @sk: netlink socket
1931 * @netnsid: network namespace identifier
1933 * Returns the network namespace identified by netnsid on success or an error
1934 * pointer on failure.
1936 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1940 net = get_net_ns_by_id(sock_net(sk), netnsid);
1942 return ERR_PTR(-EINVAL);
1944 /* For now, the caller is required to have CAP_NET_ADMIN in
1945 * the user namespace owning the target net ns.
1947 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1949 return ERR_PTR(-EACCES);
1953 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1955 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1956 bool strict_check, struct nlattr **tb,
1957 struct netlink_ext_ack *extack)
1962 struct ifinfomsg *ifm;
1964 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1965 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1969 ifm = nlmsg_data(nlh);
1970 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1972 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1975 if (ifm->ifi_index) {
1976 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1980 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1981 IFLA_MAX, ifla_policy,
1985 /* A hack to preserve kernel<->userspace interface.
1986 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1987 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1988 * what iproute2 < v3.9.0 used.
1989 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1990 * attribute, its netlink message is shorter than struct ifinfomsg.
1992 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1993 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1995 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
1999 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2001 struct netlink_ext_ack *extack = cb->extack;
2002 const struct nlmsghdr *nlh = cb->nlh;
2003 struct net *net = sock_net(skb->sk);
2004 struct net *tgt_net = net;
2007 struct net_device *dev;
2008 struct hlist_head *head;
2009 struct nlattr *tb[IFLA_MAX+1];
2010 u32 ext_filter_mask = 0;
2011 const struct rtnl_link_ops *kind_ops = NULL;
2012 unsigned int flags = NLM_F_MULTI;
2018 s_idx = cb->args[1];
2020 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2022 if (cb->strict_check)
2028 for (i = 0; i <= IFLA_MAX; ++i) {
2032 /* new attributes should only be added with strict checking */
2034 case IFLA_TARGET_NETNSID:
2035 netnsid = nla_get_s32(tb[i]);
2036 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2037 if (IS_ERR(tgt_net)) {
2038 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2039 return PTR_ERR(tgt_net);
2043 ext_filter_mask = nla_get_u32(tb[i]);
2046 master_idx = nla_get_u32(tb[i]);
2049 kind_ops = linkinfo_to_kind_ops(tb[i]);
2052 if (cb->strict_check) {
2053 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2059 if (master_idx || kind_ops)
2060 flags |= NLM_F_DUMP_FILTERED;
2063 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2065 head = &tgt_net->dev_index_head[h];
2066 hlist_for_each_entry(dev, head, index_hlist) {
2067 if (link_dump_filtered(dev, master_idx, kind_ops))
2071 err = rtnl_fill_ifinfo(skb, dev, net,
2073 NETLINK_CB(cb->skb).portid,
2074 nlh->nlmsg_seq, 0, flags,
2075 ext_filter_mask, 0, NULL, 0,
2076 netnsid, GFP_KERNEL);
2079 if (likely(skb->len))
2093 cb->seq = net->dev_base_seq;
2094 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2101 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2102 struct netlink_ext_ack *exterr)
2104 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2107 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2109 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2112 /* Examine the link attributes and figure out which
2113 * network namespace we are talking about.
2115 if (tb[IFLA_NET_NS_PID])
2116 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2117 else if (tb[IFLA_NET_NS_FD])
2118 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2120 net = get_net(src_net);
2123 EXPORT_SYMBOL(rtnl_link_get_net);
2125 /* Figure out which network namespace we are talking about by
2126 * examining the link attributes in the following order:
2128 * 1. IFLA_NET_NS_PID
2130 * 3. IFLA_TARGET_NETNSID
2132 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2133 struct nlattr *tb[])
2137 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2138 return rtnl_link_get_net(src_net, tb);
2140 if (!tb[IFLA_TARGET_NETNSID])
2141 return get_net(src_net);
2143 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2145 return ERR_PTR(-EINVAL);
2150 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2151 struct net *src_net,
2152 struct nlattr *tb[], int cap)
2156 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2160 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2162 return ERR_PTR(-EPERM);
2168 /* Verify that rtnetlink requests do not pass additional properties
2169 * potentially referring to different network namespaces.
2171 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2172 struct netlink_ext_ack *extack,
2176 if (netns_id_only) {
2177 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2180 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2184 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2187 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2190 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2196 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2200 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2203 if (tb[IFLA_ADDRESS] &&
2204 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2207 if (tb[IFLA_BROADCAST] &&
2208 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2212 if (tb[IFLA_AF_SPEC]) {
2216 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2217 const struct rtnl_af_ops *af_ops;
2220 af_ops = rtnl_af_lookup(nla_type(af));
2223 return -EAFNOSUPPORT;
2226 if (!af_ops->set_link_af) {
2231 if (af_ops->validate_link_af) {
2232 err = af_ops->validate_link_af(dev, af);
2246 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2249 const struct net_device_ops *ops = dev->netdev_ops;
2251 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2254 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2256 if (dev->type != ARPHRD_INFINIBAND)
2259 return handle_infiniband_guid(dev, ivt, guid_type);
2262 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2264 const struct net_device_ops *ops = dev->netdev_ops;
2267 if (tb[IFLA_VF_MAC]) {
2268 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2270 if (ivm->vf >= INT_MAX)
2273 if (ops->ndo_set_vf_mac)
2274 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2280 if (tb[IFLA_VF_VLAN]) {
2281 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2283 if (ivv->vf >= INT_MAX)
2286 if (ops->ndo_set_vf_vlan)
2287 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2289 htons(ETH_P_8021Q));
2294 if (tb[IFLA_VF_VLAN_LIST]) {
2295 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2296 struct nlattr *attr;
2300 if (!ops->ndo_set_vf_vlan)
2303 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2304 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2305 nla_len(attr) < NLA_HDRLEN) {
2308 if (len >= MAX_VLAN_LIST_LEN)
2310 ivvl[len] = nla_data(attr);
2317 if (ivvl[0]->vf >= INT_MAX)
2319 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2320 ivvl[0]->qos, ivvl[0]->vlan_proto);
2325 if (tb[IFLA_VF_TX_RATE]) {
2326 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2327 struct ifla_vf_info ivf;
2329 if (ivt->vf >= INT_MAX)
2332 if (ops->ndo_get_vf_config)
2333 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2338 if (ops->ndo_set_vf_rate)
2339 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2346 if (tb[IFLA_VF_RATE]) {
2347 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2349 if (ivt->vf >= INT_MAX)
2352 if (ops->ndo_set_vf_rate)
2353 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2360 if (tb[IFLA_VF_SPOOFCHK]) {
2361 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2363 if (ivs->vf >= INT_MAX)
2366 if (ops->ndo_set_vf_spoofchk)
2367 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2373 if (tb[IFLA_VF_LINK_STATE]) {
2374 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2376 if (ivl->vf >= INT_MAX)
2379 if (ops->ndo_set_vf_link_state)
2380 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2386 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2387 struct ifla_vf_rss_query_en *ivrssq_en;
2390 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2391 if (ivrssq_en->vf >= INT_MAX)
2393 if (ops->ndo_set_vf_rss_query_en)
2394 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2395 ivrssq_en->setting);
2400 if (tb[IFLA_VF_TRUST]) {
2401 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2403 if (ivt->vf >= INT_MAX)
2406 if (ops->ndo_set_vf_trust)
2407 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2412 if (tb[IFLA_VF_IB_NODE_GUID]) {
2413 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2415 if (ivt->vf >= INT_MAX)
2417 if (!ops->ndo_set_vf_guid)
2419 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2422 if (tb[IFLA_VF_IB_PORT_GUID]) {
2423 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2425 if (ivt->vf >= INT_MAX)
2427 if (!ops->ndo_set_vf_guid)
2430 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2436 static int do_set_master(struct net_device *dev, int ifindex,
2437 struct netlink_ext_ack *extack)
2439 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2440 const struct net_device_ops *ops;
2444 if (upper_dev->ifindex == ifindex)
2446 ops = upper_dev->netdev_ops;
2447 if (ops->ndo_del_slave) {
2448 err = ops->ndo_del_slave(upper_dev, dev);
2451 netdev_update_lockdep_key(dev);
2458 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2461 ops = upper_dev->netdev_ops;
2462 if (ops->ndo_add_slave) {
2463 err = ops->ndo_add_slave(upper_dev, dev, extack);
2473 #define DO_SETLINK_MODIFIED 0x01
2474 /* notify flag means notify + modified. */
2475 #define DO_SETLINK_NOTIFY 0x03
2476 static int do_setlink(const struct sk_buff *skb,
2477 struct net_device *dev, struct ifinfomsg *ifm,
2478 struct netlink_ext_ack *extack,
2479 struct nlattr **tb, char *ifname, int status)
2481 const struct net_device_ops *ops = dev->netdev_ops;
2484 err = validate_linkmsg(dev, tb);
2488 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2489 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2496 err = dev_change_net_namespace(dev, net, ifname);
2500 status |= DO_SETLINK_MODIFIED;
2504 struct rtnl_link_ifmap *u_map;
2507 if (!ops->ndo_set_config) {
2512 if (!netif_device_present(dev)) {
2517 u_map = nla_data(tb[IFLA_MAP]);
2518 k_map.mem_start = (unsigned long) u_map->mem_start;
2519 k_map.mem_end = (unsigned long) u_map->mem_end;
2520 k_map.base_addr = (unsigned short) u_map->base_addr;
2521 k_map.irq = (unsigned char) u_map->irq;
2522 k_map.dma = (unsigned char) u_map->dma;
2523 k_map.port = (unsigned char) u_map->port;
2525 err = ops->ndo_set_config(dev, &k_map);
2529 status |= DO_SETLINK_NOTIFY;
2532 if (tb[IFLA_ADDRESS]) {
2533 struct sockaddr *sa;
2536 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2538 sa = kmalloc(len, GFP_KERNEL);
2543 sa->sa_family = dev->type;
2544 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2546 err = dev_set_mac_address(dev, sa, extack);
2550 status |= DO_SETLINK_MODIFIED;
2554 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2557 status |= DO_SETLINK_MODIFIED;
2560 if (tb[IFLA_GROUP]) {
2561 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2562 status |= DO_SETLINK_NOTIFY;
2566 * Interface selected by interface index but interface
2567 * name provided implies that a name change has been
2570 if (ifm->ifi_index > 0 && ifname[0]) {
2571 err = dev_change_name(dev, ifname);
2574 status |= DO_SETLINK_MODIFIED;
2577 if (tb[IFLA_IFALIAS]) {
2578 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2579 nla_len(tb[IFLA_IFALIAS]));
2582 status |= DO_SETLINK_NOTIFY;
2585 if (tb[IFLA_BROADCAST]) {
2586 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2587 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2590 if (ifm->ifi_flags || ifm->ifi_change) {
2591 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2597 if (tb[IFLA_MASTER]) {
2598 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2601 status |= DO_SETLINK_MODIFIED;
2604 if (tb[IFLA_CARRIER]) {
2605 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2608 status |= DO_SETLINK_MODIFIED;
2611 if (tb[IFLA_TXQLEN]) {
2612 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2614 err = dev_change_tx_queue_len(dev, value);
2617 status |= DO_SETLINK_MODIFIED;
2620 if (tb[IFLA_GSO_MAX_SIZE]) {
2621 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2623 if (max_size > GSO_MAX_SIZE) {
2628 if (dev->gso_max_size ^ max_size) {
2629 netif_set_gso_max_size(dev, max_size);
2630 status |= DO_SETLINK_MODIFIED;
2634 if (tb[IFLA_GSO_MAX_SEGS]) {
2635 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2637 if (max_segs > GSO_MAX_SEGS) {
2642 if (dev->gso_max_segs ^ max_segs) {
2643 dev->gso_max_segs = max_segs;
2644 status |= DO_SETLINK_MODIFIED;
2648 if (tb[IFLA_OPERSTATE])
2649 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2651 if (tb[IFLA_LINKMODE]) {
2652 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2654 write_lock_bh(&dev_base_lock);
2655 if (dev->link_mode ^ value)
2656 status |= DO_SETLINK_NOTIFY;
2657 dev->link_mode = value;
2658 write_unlock_bh(&dev_base_lock);
2661 if (tb[IFLA_VFINFO_LIST]) {
2662 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2663 struct nlattr *attr;
2666 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2667 if (nla_type(attr) != IFLA_VF_INFO ||
2668 nla_len(attr) < NLA_HDRLEN) {
2672 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2678 err = do_setvfinfo(dev, vfinfo);
2681 status |= DO_SETLINK_NOTIFY;
2686 if (tb[IFLA_VF_PORTS]) {
2687 struct nlattr *port[IFLA_PORT_MAX+1];
2688 struct nlattr *attr;
2693 if (!ops->ndo_set_vf_port)
2696 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2697 if (nla_type(attr) != IFLA_VF_PORT ||
2698 nla_len(attr) < NLA_HDRLEN) {
2702 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2708 if (!port[IFLA_PORT_VF]) {
2712 vf = nla_get_u32(port[IFLA_PORT_VF]);
2713 err = ops->ndo_set_vf_port(dev, vf, port);
2716 status |= DO_SETLINK_NOTIFY;
2721 if (tb[IFLA_PORT_SELF]) {
2722 struct nlattr *port[IFLA_PORT_MAX+1];
2724 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2726 ifla_port_policy, NULL);
2731 if (ops->ndo_set_vf_port)
2732 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2735 status |= DO_SETLINK_NOTIFY;
2738 if (tb[IFLA_AF_SPEC]) {
2742 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2743 const struct rtnl_af_ops *af_ops;
2747 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2749 err = af_ops->set_link_af(dev, af);
2756 status |= DO_SETLINK_NOTIFY;
2761 if (tb[IFLA_PROTO_DOWN]) {
2762 err = dev_change_proto_down(dev,
2763 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2766 status |= DO_SETLINK_NOTIFY;
2770 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2773 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2775 ifla_xdp_policy, NULL);
2779 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2784 if (xdp[IFLA_XDP_FLAGS]) {
2785 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2786 if (xdp_flags & ~XDP_FLAGS_MASK) {
2790 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2796 if (xdp[IFLA_XDP_FD]) {
2797 err = dev_change_xdp_fd(dev, extack,
2798 nla_get_s32(xdp[IFLA_XDP_FD]),
2802 status |= DO_SETLINK_NOTIFY;
2807 if (status & DO_SETLINK_MODIFIED) {
2808 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2809 netdev_state_change(dev);
2812 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",
2819 static struct net_device *rtnl_dev_get(struct net *net,
2820 struct nlattr *ifname_attr,
2821 struct nlattr *altifname_attr,
2824 char buffer[ALTIFNAMSIZ];
2829 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2830 else if (altifname_attr)
2831 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2836 return __dev_get_by_name(net, ifname);
2839 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2840 struct netlink_ext_ack *extack)
2842 struct net *net = sock_net(skb->sk);
2843 struct ifinfomsg *ifm;
2844 struct net_device *dev;
2846 struct nlattr *tb[IFLA_MAX+1];
2847 char ifname[IFNAMSIZ];
2849 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2850 ifla_policy, extack);
2854 err = rtnl_ensure_unique_netns(tb, extack, false);
2858 if (tb[IFLA_IFNAME])
2859 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2864 ifm = nlmsg_data(nlh);
2865 if (ifm->ifi_index > 0)
2866 dev = __dev_get_by_index(net, ifm->ifi_index);
2867 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2868 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2877 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2882 static int rtnl_group_dellink(const struct net *net, int group)
2884 struct net_device *dev, *aux;
2885 LIST_HEAD(list_kill);
2891 for_each_netdev(net, dev) {
2892 if (dev->group == group) {
2893 const struct rtnl_link_ops *ops;
2896 ops = dev->rtnl_link_ops;
2897 if (!ops || !ops->dellink)
2905 for_each_netdev_safe(net, dev, aux) {
2906 if (dev->group == group) {
2907 const struct rtnl_link_ops *ops;
2909 ops = dev->rtnl_link_ops;
2910 ops->dellink(dev, &list_kill);
2913 unregister_netdevice_many(&list_kill);
2918 int rtnl_delete_link(struct net_device *dev)
2920 const struct rtnl_link_ops *ops;
2921 LIST_HEAD(list_kill);
2923 ops = dev->rtnl_link_ops;
2924 if (!ops || !ops->dellink)
2927 ops->dellink(dev, &list_kill);
2928 unregister_netdevice_many(&list_kill);
2932 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2934 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2935 struct netlink_ext_ack *extack)
2937 struct net *net = sock_net(skb->sk);
2938 struct net *tgt_net = net;
2939 struct net_device *dev = NULL;
2940 struct ifinfomsg *ifm;
2941 struct nlattr *tb[IFLA_MAX+1];
2945 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2946 ifla_policy, extack);
2950 err = rtnl_ensure_unique_netns(tb, extack, true);
2954 if (tb[IFLA_TARGET_NETNSID]) {
2955 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2956 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2957 if (IS_ERR(tgt_net))
2958 return PTR_ERR(tgt_net);
2962 ifm = nlmsg_data(nlh);
2963 if (ifm->ifi_index > 0)
2964 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2965 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2966 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2967 tb[IFLA_ALT_IFNAME], NULL);
2968 else if (tb[IFLA_GROUP])
2969 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2974 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2980 err = rtnl_delete_link(dev);
2989 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2991 unsigned int old_flags;
2994 old_flags = dev->flags;
2995 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2996 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3002 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3003 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3005 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3006 __dev_notify_flags(dev, old_flags, ~0U);
3010 EXPORT_SYMBOL(rtnl_configure_link);
3012 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3013 unsigned char name_assign_type,
3014 const struct rtnl_link_ops *ops,
3015 struct nlattr *tb[],
3016 struct netlink_ext_ack *extack)
3018 struct net_device *dev;
3019 unsigned int num_tx_queues = 1;
3020 unsigned int num_rx_queues = 1;
3022 if (tb[IFLA_NUM_TX_QUEUES])
3023 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3024 else if (ops->get_num_tx_queues)
3025 num_tx_queues = ops->get_num_tx_queues();
3027 if (tb[IFLA_NUM_RX_QUEUES])
3028 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3029 else if (ops->get_num_rx_queues)
3030 num_rx_queues = ops->get_num_rx_queues();
3032 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3033 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3034 return ERR_PTR(-EINVAL);
3037 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3038 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3039 return ERR_PTR(-EINVAL);
3042 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3043 ops->setup, num_tx_queues, num_rx_queues);
3045 return ERR_PTR(-ENOMEM);
3047 dev_net_set(dev, net);
3048 dev->rtnl_link_ops = ops;
3049 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3052 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
3053 if (tb[IFLA_ADDRESS]) {
3054 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3055 nla_len(tb[IFLA_ADDRESS]));
3056 dev->addr_assign_type = NET_ADDR_SET;
3058 if (tb[IFLA_BROADCAST])
3059 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3060 nla_len(tb[IFLA_BROADCAST]));
3061 if (tb[IFLA_TXQLEN])
3062 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3063 if (tb[IFLA_OPERSTATE])
3064 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3065 if (tb[IFLA_LINKMODE])
3066 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3068 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3069 if (tb[IFLA_GSO_MAX_SIZE])
3070 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3071 if (tb[IFLA_GSO_MAX_SEGS])
3072 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3076 EXPORT_SYMBOL(rtnl_create_link);
3078 static int rtnl_group_changelink(const struct sk_buff *skb,
3079 struct net *net, int group,
3080 struct ifinfomsg *ifm,
3081 struct netlink_ext_ack *extack,
3084 struct net_device *dev, *aux;
3087 for_each_netdev_safe(net, dev, aux) {
3088 if (dev->group == group) {
3089 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3098 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3099 struct nlattr **attr, struct netlink_ext_ack *extack)
3101 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3102 unsigned char name_assign_type = NET_NAME_USER;
3103 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3104 const struct rtnl_link_ops *m_ops = NULL;
3105 struct net_device *master_dev = NULL;
3106 struct net *net = sock_net(skb->sk);
3107 const struct rtnl_link_ops *ops;
3108 struct nlattr *tb[IFLA_MAX + 1];
3109 struct net *dest_net, *link_net;
3110 struct nlattr **slave_data;
3111 char kind[MODULE_NAME_LEN];
3112 struct net_device *dev;
3113 struct ifinfomsg *ifm;
3114 char ifname[IFNAMSIZ];
3115 struct nlattr **data;
3118 #ifdef CONFIG_MODULES
3121 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3122 ifla_policy, extack);
3126 err = rtnl_ensure_unique_netns(tb, extack, false);
3130 if (tb[IFLA_IFNAME])
3131 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3135 ifm = nlmsg_data(nlh);
3136 if (ifm->ifi_index > 0)
3137 dev = __dev_get_by_index(net, ifm->ifi_index);
3138 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3139 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3144 master_dev = netdev_master_upper_dev_get(dev);
3146 m_ops = master_dev->rtnl_link_ops;
3149 err = validate_linkmsg(dev, tb);
3153 if (tb[IFLA_LINKINFO]) {
3154 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3156 ifla_info_policy, NULL);
3160 memset(linkinfo, 0, sizeof(linkinfo));
3162 if (linkinfo[IFLA_INFO_KIND]) {
3163 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3164 ops = rtnl_link_ops_get(kind);
3172 if (ops->maxtype > RTNL_MAX_TYPE)
3175 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3176 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3177 linkinfo[IFLA_INFO_DATA],
3178 ops->policy, extack);
3183 if (ops->validate) {
3184 err = ops->validate(tb, data, extack);
3192 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3195 if (m_ops->slave_maxtype &&
3196 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3197 err = nla_parse_nested_deprecated(slave_attr,
3198 m_ops->slave_maxtype,
3199 linkinfo[IFLA_INFO_SLAVE_DATA],
3200 m_ops->slave_policy,
3204 slave_data = slave_attr;
3211 if (nlh->nlmsg_flags & NLM_F_EXCL)
3213 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3216 if (linkinfo[IFLA_INFO_DATA]) {
3217 if (!ops || ops != dev->rtnl_link_ops ||
3221 err = ops->changelink(dev, tb, data, extack);
3224 status |= DO_SETLINK_NOTIFY;
3227 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3228 if (!m_ops || !m_ops->slave_changelink)
3231 err = m_ops->slave_changelink(master_dev, dev, tb,
3232 slave_data, extack);
3235 status |= DO_SETLINK_NOTIFY;
3238 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3241 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3242 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3243 return rtnl_group_changelink(skb, net,
3244 nla_get_u32(tb[IFLA_GROUP]),
3249 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3253 #ifdef CONFIG_MODULES
3256 request_module("rtnl-link-%s", kind);
3258 ops = rtnl_link_ops_get(kind);
3263 NL_SET_ERR_MSG(extack, "Unknown device type");
3271 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3272 name_assign_type = NET_NAME_ENUM;
3275 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3276 if (IS_ERR(dest_net))
3277 return PTR_ERR(dest_net);
3279 if (tb[IFLA_LINK_NETNSID]) {
3280 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3282 link_net = get_net_ns_by_id(dest_net, id);
3284 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3289 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3295 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3296 name_assign_type, ops, tb, extack);
3302 dev->ifindex = ifm->ifi_index;
3305 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3306 /* Drivers should call free_netdev() in ->destructor
3307 * and unregister it on failure after registration
3308 * so that device could be finally freed in rtnl_unlock.
3311 /* If device is not registered at all, free it now */
3312 if (dev->reg_state == NETREG_UNINITIALIZED)
3317 err = register_netdevice(dev);
3323 err = rtnl_configure_link(dev, ifm);
3325 goto out_unregister;
3327 err = dev_change_net_namespace(dev, dest_net, ifname);
3329 goto out_unregister;
3331 if (tb[IFLA_MASTER]) {
3332 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3334 goto out_unregister;
3343 LIST_HEAD(list_kill);
3345 ops->dellink(dev, &list_kill);
3346 unregister_netdevice_many(&list_kill);
3348 unregister_netdevice(dev);
3353 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3354 struct netlink_ext_ack *extack)
3356 struct nlattr **attr;
3359 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3363 ret = __rtnl_newlink(skb, nlh, attr, extack);
3368 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3369 const struct nlmsghdr *nlh,
3371 struct netlink_ext_ack *extack)
3373 struct ifinfomsg *ifm;
3376 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3377 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3381 if (!netlink_strict_get_check(skb))
3382 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3383 ifla_policy, extack);
3385 ifm = nlmsg_data(nlh);
3386 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3388 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3392 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3393 ifla_policy, extack);
3397 for (i = 0; i <= IFLA_MAX; i++) {
3403 case IFLA_ALT_IFNAME:
3405 case IFLA_TARGET_NETNSID:
3408 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3416 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3417 struct netlink_ext_ack *extack)
3419 struct net *net = sock_net(skb->sk);
3420 struct net *tgt_net = net;
3421 struct ifinfomsg *ifm;
3422 struct nlattr *tb[IFLA_MAX+1];
3423 struct net_device *dev = NULL;
3424 struct sk_buff *nskb;
3427 u32 ext_filter_mask = 0;
3429 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3433 err = rtnl_ensure_unique_netns(tb, extack, true);
3437 if (tb[IFLA_TARGET_NETNSID]) {
3438 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3439 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3440 if (IS_ERR(tgt_net))
3441 return PTR_ERR(tgt_net);
3444 if (tb[IFLA_EXT_MASK])
3445 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3448 ifm = nlmsg_data(nlh);
3449 if (ifm->ifi_index > 0)
3450 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3451 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3452 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3453 tb[IFLA_ALT_IFNAME], NULL);
3462 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3466 err = rtnl_fill_ifinfo(nskb, dev, net,
3467 RTM_NEWLINK, NETLINK_CB(skb).portid,
3468 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3469 0, NULL, 0, netnsid, GFP_KERNEL);
3471 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3472 WARN_ON(err == -EMSGSIZE);
3475 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3483 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3484 bool *changed, struct netlink_ext_ack *extack)
3489 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3493 alt_ifname = nla_data(attr);
3494 if (cmd == RTM_NEWLINKPROP) {
3495 alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
3498 err = netdev_name_node_alt_create(dev, alt_ifname);
3503 } else if (cmd == RTM_DELLINKPROP) {
3504 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3516 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3517 struct netlink_ext_ack *extack)
3519 struct net *net = sock_net(skb->sk);
3520 struct nlattr *tb[IFLA_MAX + 1];
3521 struct net_device *dev;
3522 struct ifinfomsg *ifm;
3523 bool changed = false;
3524 struct nlattr *attr;
3527 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3531 err = rtnl_ensure_unique_netns(tb, extack, true);
3535 ifm = nlmsg_data(nlh);
3536 if (ifm->ifi_index > 0)
3537 dev = __dev_get_by_index(net, ifm->ifi_index);
3538 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3539 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3540 tb[IFLA_ALT_IFNAME], NULL);
3547 if (!tb[IFLA_PROP_LIST])
3550 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3551 switch (nla_type(attr)) {
3552 case IFLA_ALT_IFNAME:
3553 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3561 netdev_state_change(dev);
3565 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3566 struct netlink_ext_ack *extack)
3568 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3571 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3572 struct netlink_ext_ack *extack)
3574 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3577 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3579 struct net *net = sock_net(skb->sk);
3580 struct net_device *dev;
3581 struct nlattr *tb[IFLA_MAX+1];
3582 u32 ext_filter_mask = 0;
3583 u16 min_ifinfo_dump_size = 0;
3586 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3587 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3588 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3590 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3591 if (tb[IFLA_EXT_MASK])
3592 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3595 if (!ext_filter_mask)
3596 return NLMSG_GOODSIZE;
3598 * traverse the list of net devices and compute the minimum
3599 * buffer size based upon the filter mask.
3602 for_each_netdev_rcu(net, dev) {
3603 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3609 return nlmsg_total_size(min_ifinfo_dump_size);
3612 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3615 int s_idx = cb->family;
3616 int type = cb->nlh->nlmsg_type - RTM_BASE;
3622 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3623 struct rtnl_link **tab;
3624 struct rtnl_link *link;
3625 rtnl_dumpit_func dumpit;
3627 if (idx < s_idx || idx == PF_PACKET)
3630 if (type < 0 || type >= RTM_NR_MSGTYPES)
3633 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3641 dumpit = link->dumpit;
3646 memset(&cb->args[0], 0, sizeof(cb->args));
3650 ret = dumpit(skb, cb);
3656 return skb->len ? : ret;
3659 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3660 unsigned int change,
3661 u32 event, gfp_t flags, int *new_nsid,
3664 struct net *net = dev_net(dev);
3665 struct sk_buff *skb;
3667 size_t if_info_size;
3669 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3673 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3674 type, 0, 0, change, 0, 0, event,
3675 new_nsid, new_ifindex, -1, flags);
3677 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3678 WARN_ON(err == -EMSGSIZE);
3685 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3689 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3691 struct net *net = dev_net(dev);
3693 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3696 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3697 unsigned int change, u32 event,
3698 gfp_t flags, int *new_nsid, int new_ifindex)
3700 struct sk_buff *skb;
3702 if (dev->reg_state != NETREG_REGISTERED)
3705 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3708 rtmsg_ifinfo_send(skb, dev, flags);
3711 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3714 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3718 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3719 gfp_t flags, int *new_nsid, int new_ifindex)
3721 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3722 new_nsid, new_ifindex);
3725 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3726 struct net_device *dev,
3727 u8 *addr, u16 vid, u32 pid, u32 seq,
3728 int type, unsigned int flags,
3729 int nlflags, u16 ndm_state)
3731 struct nlmsghdr *nlh;
3734 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3738 ndm = nlmsg_data(nlh);
3739 ndm->ndm_family = AF_BRIDGE;
3742 ndm->ndm_flags = flags;
3744 ndm->ndm_ifindex = dev->ifindex;
3745 ndm->ndm_state = ndm_state;
3747 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3748 goto nla_put_failure;
3750 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3751 goto nla_put_failure;
3753 nlmsg_end(skb, nlh);
3757 nlmsg_cancel(skb, nlh);
3761 static inline size_t rtnl_fdb_nlmsg_size(void)
3763 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3764 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3765 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3769 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3772 struct net *net = dev_net(dev);
3773 struct sk_buff *skb;
3776 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3780 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3781 0, 0, type, NTF_SELF, 0, ndm_state);
3787 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3790 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3794 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3796 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3797 struct nlattr *tb[],
3798 struct net_device *dev,
3799 const unsigned char *addr, u16 vid,
3804 /* If aging addresses are supported device will need to
3805 * implement its own handler for this.
3807 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3808 pr_info("%s: FDB only supports static addresses\n", dev->name);
3813 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3817 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3818 err = dev_uc_add_excl(dev, addr);
3819 else if (is_multicast_ether_addr(addr))
3820 err = dev_mc_add_excl(dev, addr);
3822 /* Only return duplicate errors if NLM_F_EXCL is set */
3823 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3828 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3830 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3831 struct netlink_ext_ack *extack)
3836 if (nla_len(vlan_attr) != sizeof(u16)) {
3837 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3841 vid = nla_get_u16(vlan_attr);
3843 if (!vid || vid >= VLAN_VID_MASK) {
3844 NL_SET_ERR_MSG(extack, "invalid vlan id");
3852 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3853 struct netlink_ext_ack *extack)
3855 struct net *net = sock_net(skb->sk);
3857 struct nlattr *tb[NDA_MAX+1];
3858 struct net_device *dev;
3863 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3868 ndm = nlmsg_data(nlh);
3869 if (ndm->ndm_ifindex == 0) {
3870 NL_SET_ERR_MSG(extack, "invalid ifindex");
3874 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3876 NL_SET_ERR_MSG(extack, "unknown ifindex");
3880 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3881 NL_SET_ERR_MSG(extack, "invalid address");
3885 if (dev->type != ARPHRD_ETHER) {
3886 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3890 addr = nla_data(tb[NDA_LLADDR]);
3892 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3898 /* Support fdb on master device the net/bridge default case */
3899 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3900 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3901 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3902 const struct net_device_ops *ops = br_dev->netdev_ops;
3904 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3905 nlh->nlmsg_flags, extack);
3909 ndm->ndm_flags &= ~NTF_MASTER;
3912 /* Embedded bridge, macvlan, and any other device support */
3913 if ((ndm->ndm_flags & NTF_SELF)) {
3914 if (dev->netdev_ops->ndo_fdb_add)
3915 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3920 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3924 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3926 ndm->ndm_flags &= ~NTF_SELF;
3934 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3936 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3937 struct nlattr *tb[],
3938 struct net_device *dev,
3939 const unsigned char *addr, u16 vid)
3943 /* If aging addresses are supported device will need to
3944 * implement its own handler for this.
3946 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3947 pr_info("%s: FDB only supports static addresses\n", dev->name);
3951 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3952 err = dev_uc_del(dev, addr);
3953 else if (is_multicast_ether_addr(addr))
3954 err = dev_mc_del(dev, addr);
3958 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3960 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3961 struct netlink_ext_ack *extack)
3963 struct net *net = sock_net(skb->sk);
3965 struct nlattr *tb[NDA_MAX+1];
3966 struct net_device *dev;
3971 if (!netlink_capable(skb, CAP_NET_ADMIN))
3974 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3979 ndm = nlmsg_data(nlh);
3980 if (ndm->ndm_ifindex == 0) {
3981 NL_SET_ERR_MSG(extack, "invalid ifindex");
3985 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3987 NL_SET_ERR_MSG(extack, "unknown ifindex");
3991 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3992 NL_SET_ERR_MSG(extack, "invalid address");
3996 if (dev->type != ARPHRD_ETHER) {
3997 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4001 addr = nla_data(tb[NDA_LLADDR]);
4003 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4009 /* Support fdb on master device the net/bridge default case */
4010 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4011 (dev->priv_flags & IFF_BRIDGE_PORT)) {
4012 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4013 const struct net_device_ops *ops = br_dev->netdev_ops;
4015 if (ops->ndo_fdb_del)
4016 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4021 ndm->ndm_flags &= ~NTF_MASTER;
4024 /* Embedded bridge, macvlan, and any other device support */
4025 if (ndm->ndm_flags & NTF_SELF) {
4026 if (dev->netdev_ops->ndo_fdb_del)
4027 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4030 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4033 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4035 ndm->ndm_flags &= ~NTF_SELF;
4042 static int nlmsg_populate_fdb(struct sk_buff *skb,
4043 struct netlink_callback *cb,
4044 struct net_device *dev,
4046 struct netdev_hw_addr_list *list)
4048 struct netdev_hw_addr *ha;
4052 portid = NETLINK_CB(cb->skb).portid;
4053 seq = cb->nlh->nlmsg_seq;
4055 list_for_each_entry(ha, &list->list, list) {
4056 if (*idx < cb->args[2])
4059 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4061 RTM_NEWNEIGH, NTF_SELF,
4062 NLM_F_MULTI, NUD_PERMANENT);
4072 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4073 * @skb: socket buffer to store message in
4074 * @cb: netlink callback
4076 * @filter_dev: ignored
4077 * @idx: the number of FDB table entries dumped is added to *@idx
4079 * Default netdevice operation to dump the existing unicast address list.
4080 * Returns number of addresses from list put in skb.
4082 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4083 struct netlink_callback *cb,
4084 struct net_device *dev,
4085 struct net_device *filter_dev,
4090 if (dev->type != ARPHRD_ETHER)
4093 netif_addr_lock_bh(dev);
4094 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4097 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4099 netif_addr_unlock_bh(dev);
4102 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4104 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4105 int *br_idx, int *brport_idx,
4106 struct netlink_ext_ack *extack)
4108 struct nlattr *tb[NDA_MAX + 1];
4112 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4113 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4117 ndm = nlmsg_data(nlh);
4118 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4119 ndm->ndm_flags || ndm->ndm_type) {
4120 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4124 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4125 NDA_MAX, NULL, extack);
4129 *brport_idx = ndm->ndm_ifindex;
4130 for (i = 0; i <= NDA_MAX; ++i) {
4136 if (nla_len(tb[i]) != sizeof(u32)) {
4137 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4140 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4143 if (nla_len(tb[i]) != sizeof(u32)) {
4144 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4147 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4150 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4158 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4159 int *br_idx, int *brport_idx,
4160 struct netlink_ext_ack *extack)
4162 struct nlattr *tb[IFLA_MAX+1];
4165 /* A hack to preserve kernel<->userspace interface.
4166 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4167 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4168 * So, check for ndmsg with an optional u32 attribute (not used here).
4169 * Fortunately these sizes don't conflict with the size of ifinfomsg
4170 * with an optional attribute.
4172 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4173 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4174 nla_attr_size(sizeof(u32)))) {
4175 struct ifinfomsg *ifm;
4177 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4178 tb, IFLA_MAX, ifla_policy,
4182 } else if (err == 0) {
4183 if (tb[IFLA_MASTER])
4184 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4187 ifm = nlmsg_data(nlh);
4188 *brport_idx = ifm->ifi_index;
4193 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4195 struct net_device *dev;
4196 struct net_device *br_dev = NULL;
4197 const struct net_device_ops *ops = NULL;
4198 const struct net_device_ops *cops = NULL;
4199 struct net *net = sock_net(skb->sk);
4200 struct hlist_head *head;
4208 if (cb->strict_check)
4209 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4212 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4218 br_dev = __dev_get_by_index(net, br_idx);
4222 ops = br_dev->netdev_ops;
4226 s_idx = cb->args[1];
4228 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4230 head = &net->dev_index_head[h];
4231 hlist_for_each_entry(dev, head, index_hlist) {
4233 if (brport_idx && (dev->ifindex != brport_idx))
4236 if (!br_idx) { /* user did not specify a specific bridge */
4237 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4238 br_dev = netdev_master_upper_dev_get(dev);
4239 cops = br_dev->netdev_ops;
4242 if (dev != br_dev &&
4243 !(dev->priv_flags & IFF_BRIDGE_PORT))
4246 if (br_dev != netdev_master_upper_dev_get(dev) &&
4247 !(dev->priv_flags & IFF_EBRIDGE))
4255 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4256 if (cops && cops->ndo_fdb_dump) {
4257 err = cops->ndo_fdb_dump(skb, cb,
4260 if (err == -EMSGSIZE)
4265 if (dev->netdev_ops->ndo_fdb_dump)
4266 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4270 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4272 if (err == -EMSGSIZE)
4277 /* reset fdb offset to 0 for rest of the interfaces */
4293 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4294 struct nlattr **tb, u8 *ndm_flags,
4295 int *br_idx, int *brport_idx, u8 **addr,
4296 u16 *vid, struct netlink_ext_ack *extack)
4301 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4302 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4306 ndm = nlmsg_data(nlh);
4307 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4309 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4313 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4314 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4318 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4319 NDA_MAX, nda_policy, extack);
4323 *ndm_flags = ndm->ndm_flags;
4324 *brport_idx = ndm->ndm_ifindex;
4325 for (i = 0; i <= NDA_MAX; ++i) {
4331 *br_idx = nla_get_u32(tb[i]);
4334 if (nla_len(tb[i]) != ETH_ALEN) {
4335 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4338 *addr = nla_data(tb[i]);
4341 err = fdb_vid_parse(tb[i], vid, extack);
4348 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4356 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4357 struct netlink_ext_ack *extack)
4359 struct net_device *dev = NULL, *br_dev = NULL;
4360 const struct net_device_ops *ops = NULL;
4361 struct net *net = sock_net(in_skb->sk);
4362 struct nlattr *tb[NDA_MAX + 1];
4363 struct sk_buff *skb;
4371 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4372 &brport_idx, &addr, &vid, extack);
4377 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4382 dev = __dev_get_by_index(net, brport_idx);
4384 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4391 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4395 br_dev = __dev_get_by_index(net, br_idx);
4397 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4400 ops = br_dev->netdev_ops;
4404 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4405 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4406 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4409 br_dev = netdev_master_upper_dev_get(dev);
4411 NL_SET_ERR_MSG(extack, "Master of device not found");
4414 ops = br_dev->netdev_ops;
4416 if (!(ndm_flags & NTF_SELF)) {
4417 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4420 ops = dev->netdev_ops;
4424 if (!br_dev && !dev) {
4425 NL_SET_ERR_MSG(extack, "No device specified");
4429 if (!ops || !ops->ndo_fdb_get) {
4430 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4434 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4440 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4441 NETLINK_CB(in_skb).portid,
4442 nlh->nlmsg_seq, extack);
4446 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4452 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4453 unsigned int attrnum, unsigned int flag)
4456 return nla_put_u8(skb, attrnum, !!(flags & flag));
4460 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4461 struct net_device *dev, u16 mode,
4462 u32 flags, u32 mask, int nlflags,
4464 int (*vlan_fill)(struct sk_buff *skb,
4465 struct net_device *dev,
4468 struct nlmsghdr *nlh;
4469 struct ifinfomsg *ifm;
4470 struct nlattr *br_afspec;
4471 struct nlattr *protinfo;
4472 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4473 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4476 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4480 ifm = nlmsg_data(nlh);
4481 ifm->ifi_family = AF_BRIDGE;
4483 ifm->ifi_type = dev->type;
4484 ifm->ifi_index = dev->ifindex;
4485 ifm->ifi_flags = dev_get_flags(dev);
4486 ifm->ifi_change = 0;
4489 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4490 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4491 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4493 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4495 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4496 (dev->ifindex != dev_get_iflink(dev) &&
4497 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4498 goto nla_put_failure;
4500 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4502 goto nla_put_failure;
4504 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4505 nla_nest_cancel(skb, br_afspec);
4506 goto nla_put_failure;
4509 if (mode != BRIDGE_MODE_UNDEF) {
4510 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4511 nla_nest_cancel(skb, br_afspec);
4512 goto nla_put_failure;
4516 err = vlan_fill(skb, dev, filter_mask);
4518 nla_nest_cancel(skb, br_afspec);
4519 goto nla_put_failure;
4522 nla_nest_end(skb, br_afspec);
4524 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4526 goto nla_put_failure;
4528 if (brport_nla_put_flag(skb, flags, mask,
4529 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4530 brport_nla_put_flag(skb, flags, mask,
4531 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4532 brport_nla_put_flag(skb, flags, mask,
4533 IFLA_BRPORT_FAST_LEAVE,
4534 BR_MULTICAST_FAST_LEAVE) ||
4535 brport_nla_put_flag(skb, flags, mask,
4536 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4537 brport_nla_put_flag(skb, flags, mask,
4538 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4539 brport_nla_put_flag(skb, flags, mask,
4540 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4541 brport_nla_put_flag(skb, flags, mask,
4542 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4543 brport_nla_put_flag(skb, flags, mask,
4544 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4545 nla_nest_cancel(skb, protinfo);
4546 goto nla_put_failure;
4549 nla_nest_end(skb, protinfo);
4551 nlmsg_end(skb, nlh);
4554 nlmsg_cancel(skb, nlh);
4555 return err ? err : -EMSGSIZE;
4557 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4559 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4560 bool strict_check, u32 *filter_mask,
4561 struct netlink_ext_ack *extack)
4563 struct nlattr *tb[IFLA_MAX+1];
4567 struct ifinfomsg *ifm;
4569 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4570 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4574 ifm = nlmsg_data(nlh);
4575 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4576 ifm->ifi_change || ifm->ifi_index) {
4577 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4581 err = nlmsg_parse_deprecated_strict(nlh,
4582 sizeof(struct ifinfomsg),
4583 tb, IFLA_MAX, ifla_policy,
4586 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4587 tb, IFLA_MAX, ifla_policy,
4593 /* new attributes should only be added with strict checking */
4594 for (i = 0; i <= IFLA_MAX; ++i) {
4600 *filter_mask = nla_get_u32(tb[i]);
4604 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4613 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4615 const struct nlmsghdr *nlh = cb->nlh;
4616 struct net *net = sock_net(skb->sk);
4617 struct net_device *dev;
4619 u32 portid = NETLINK_CB(cb->skb).portid;
4620 u32 seq = nlh->nlmsg_seq;
4621 u32 filter_mask = 0;
4624 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4626 if (err < 0 && cb->strict_check)
4630 for_each_netdev_rcu(net, dev) {
4631 const struct net_device_ops *ops = dev->netdev_ops;
4632 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4634 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4635 if (idx >= cb->args[0]) {
4636 err = br_dev->netdev_ops->ndo_bridge_getlink(
4637 skb, portid, seq, dev,
4638 filter_mask, NLM_F_MULTI);
4639 if (err < 0 && err != -EOPNOTSUPP) {
4640 if (likely(skb->len))
4649 if (ops->ndo_bridge_getlink) {
4650 if (idx >= cb->args[0]) {
4651 err = ops->ndo_bridge_getlink(skb, portid,
4655 if (err < 0 && err != -EOPNOTSUPP) {
4656 if (likely(skb->len))
4673 static inline size_t bridge_nlmsg_size(void)
4675 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4676 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4677 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4678 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4679 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4680 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4681 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4682 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4683 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4684 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4685 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4688 static int rtnl_bridge_notify(struct net_device *dev)
4690 struct net *net = dev_net(dev);
4691 struct sk_buff *skb;
4692 int err = -EOPNOTSUPP;
4694 if (!dev->netdev_ops->ndo_bridge_getlink)
4697 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4703 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4710 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4713 WARN_ON(err == -EMSGSIZE);
4716 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4720 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4721 struct netlink_ext_ack *extack)
4723 struct net *net = sock_net(skb->sk);
4724 struct ifinfomsg *ifm;
4725 struct net_device *dev;
4726 struct nlattr *br_spec, *attr = NULL;
4727 int rem, err = -EOPNOTSUPP;
4729 bool have_flags = false;
4731 if (nlmsg_len(nlh) < sizeof(*ifm))
4734 ifm = nlmsg_data(nlh);
4735 if (ifm->ifi_family != AF_BRIDGE)
4736 return -EPFNOSUPPORT;
4738 dev = __dev_get_by_index(net, ifm->ifi_index);
4740 NL_SET_ERR_MSG(extack, "unknown ifindex");
4744 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4746 nla_for_each_nested(attr, br_spec, rem) {
4747 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4748 if (nla_len(attr) < sizeof(flags))
4752 flags = nla_get_u16(attr);
4758 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4759 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4761 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4766 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4771 flags &= ~BRIDGE_FLAGS_MASTER;
4774 if ((flags & BRIDGE_FLAGS_SELF)) {
4775 if (!dev->netdev_ops->ndo_bridge_setlink)
4778 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4782 flags &= ~BRIDGE_FLAGS_SELF;
4784 /* Generate event to notify upper layer of bridge
4787 err = rtnl_bridge_notify(dev);
4792 memcpy(nla_data(attr), &flags, sizeof(flags));
4797 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4798 struct netlink_ext_ack *extack)
4800 struct net *net = sock_net(skb->sk);
4801 struct ifinfomsg *ifm;
4802 struct net_device *dev;
4803 struct nlattr *br_spec, *attr = NULL;
4804 int rem, err = -EOPNOTSUPP;
4806 bool have_flags = false;
4808 if (nlmsg_len(nlh) < sizeof(*ifm))
4811 ifm = nlmsg_data(nlh);
4812 if (ifm->ifi_family != AF_BRIDGE)
4813 return -EPFNOSUPPORT;
4815 dev = __dev_get_by_index(net, ifm->ifi_index);
4817 NL_SET_ERR_MSG(extack, "unknown ifindex");
4821 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4823 nla_for_each_nested(attr, br_spec, rem) {
4824 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4825 if (nla_len(attr) < sizeof(flags))
4829 flags = nla_get_u16(attr);
4835 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4836 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4838 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4843 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4847 flags &= ~BRIDGE_FLAGS_MASTER;
4850 if ((flags & BRIDGE_FLAGS_SELF)) {
4851 if (!dev->netdev_ops->ndo_bridge_dellink)
4854 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4858 flags &= ~BRIDGE_FLAGS_SELF;
4860 /* Generate event to notify upper layer of bridge
4863 err = rtnl_bridge_notify(dev);
4868 memcpy(nla_data(attr), &flags, sizeof(flags));
4873 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4875 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4876 (!idxattr || idxattr == attrid);
4879 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4880 static int rtnl_get_offload_stats_attr_size(int attr_id)
4883 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4884 return sizeof(struct rtnl_link_stats64);
4890 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4893 struct nlattr *attr = NULL;
4898 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4899 dev->netdev_ops->ndo_get_offload_stats))
4902 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4903 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4904 if (attr_id < *prividx)
4907 size = rtnl_get_offload_stats_attr_size(attr_id);
4911 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4914 attr = nla_reserve_64bit(skb, attr_id, size,
4915 IFLA_OFFLOAD_XSTATS_UNSPEC);
4917 goto nla_put_failure;
4919 attr_data = nla_data(attr);
4920 memset(attr_data, 0, size);
4921 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4924 goto get_offload_stats_failure;
4935 get_offload_stats_failure:
4940 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4946 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4947 dev->netdev_ops->ndo_get_offload_stats))
4950 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4951 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4952 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4954 size = rtnl_get_offload_stats_attr_size(attr_id);
4955 nla_size += nla_total_size_64bit(size);
4959 nla_size += nla_total_size(0);
4964 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4965 int type, u32 pid, u32 seq, u32 change,
4966 unsigned int flags, unsigned int filter_mask,
4967 int *idxattr, int *prividx)
4969 struct if_stats_msg *ifsm;
4970 struct nlmsghdr *nlh;
4971 struct nlattr *attr;
4972 int s_prividx = *prividx;
4977 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4981 ifsm = nlmsg_data(nlh);
4982 ifsm->family = PF_UNSPEC;
4985 ifsm->ifindex = dev->ifindex;
4986 ifsm->filter_mask = filter_mask;
4988 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4989 struct rtnl_link_stats64 *sp;
4991 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4992 sizeof(struct rtnl_link_stats64),
4995 goto nla_put_failure;
4997 sp = nla_data(attr);
4998 dev_get_stats(dev, sp);
5001 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5002 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5004 if (ops && ops->fill_linkxstats) {
5005 *idxattr = IFLA_STATS_LINK_XSTATS;
5006 attr = nla_nest_start_noflag(skb,
5007 IFLA_STATS_LINK_XSTATS);
5009 goto nla_put_failure;
5011 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5012 nla_nest_end(skb, attr);
5014 goto nla_put_failure;
5019 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5021 const struct rtnl_link_ops *ops = NULL;
5022 const struct net_device *master;
5024 master = netdev_master_upper_dev_get(dev);
5026 ops = master->rtnl_link_ops;
5027 if (ops && ops->fill_linkxstats) {
5028 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5029 attr = nla_nest_start_noflag(skb,
5030 IFLA_STATS_LINK_XSTATS_SLAVE);
5032 goto nla_put_failure;
5034 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5035 nla_nest_end(skb, attr);
5037 goto nla_put_failure;
5042 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5044 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5045 attr = nla_nest_start_noflag(skb,
5046 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5048 goto nla_put_failure;
5050 err = rtnl_get_offload_stats(skb, dev, prividx);
5051 if (err == -ENODATA)
5052 nla_nest_cancel(skb, attr);
5054 nla_nest_end(skb, attr);
5056 if (err && err != -ENODATA)
5057 goto nla_put_failure;
5061 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5062 struct rtnl_af_ops *af_ops;
5064 *idxattr = IFLA_STATS_AF_SPEC;
5065 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5067 goto nla_put_failure;
5070 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5071 if (af_ops->fill_stats_af) {
5075 af = nla_nest_start_noflag(skb,
5079 goto nla_put_failure;
5081 err = af_ops->fill_stats_af(skb, dev);
5083 if (err == -ENODATA) {
5084 nla_nest_cancel(skb, af);
5085 } else if (err < 0) {
5087 goto nla_put_failure;
5090 nla_nest_end(skb, af);
5095 nla_nest_end(skb, attr);
5100 nlmsg_end(skb, nlh);
5105 /* not a multi message or no progress mean a real error */
5106 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5107 nlmsg_cancel(skb, nlh);
5109 nlmsg_end(skb, nlh);
5114 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5119 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5120 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5122 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5123 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5124 int attr = IFLA_STATS_LINK_XSTATS;
5126 if (ops && ops->get_linkxstats_size) {
5127 size += nla_total_size(ops->get_linkxstats_size(dev,
5129 /* for IFLA_STATS_LINK_XSTATS */
5130 size += nla_total_size(0);
5134 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5135 struct net_device *_dev = (struct net_device *)dev;
5136 const struct rtnl_link_ops *ops = NULL;
5137 const struct net_device *master;
5139 /* netdev_master_upper_dev_get can't take const */
5140 master = netdev_master_upper_dev_get(_dev);
5142 ops = master->rtnl_link_ops;
5143 if (ops && ops->get_linkxstats_size) {
5144 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5146 size += nla_total_size(ops->get_linkxstats_size(dev,
5148 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5149 size += nla_total_size(0);
5153 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5154 size += rtnl_get_offload_stats_size(dev);
5156 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5157 struct rtnl_af_ops *af_ops;
5159 /* for IFLA_STATS_AF_SPEC */
5160 size += nla_total_size(0);
5163 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5164 if (af_ops->get_stats_af_size) {
5165 size += nla_total_size(
5166 af_ops->get_stats_af_size(dev));
5169 size += nla_total_size(0);
5178 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5179 bool is_dump, struct netlink_ext_ack *extack)
5181 struct if_stats_msg *ifsm;
5183 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5184 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5191 ifsm = nlmsg_data(nlh);
5193 /* only requests using strict checks can pass data to influence
5194 * the dump. The legacy exception is filter_mask.
5196 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5197 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5200 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5201 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5204 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5205 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5212 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5213 struct netlink_ext_ack *extack)
5215 struct net *net = sock_net(skb->sk);
5216 struct net_device *dev = NULL;
5217 int idxattr = 0, prividx = 0;
5218 struct if_stats_msg *ifsm;
5219 struct sk_buff *nskb;
5223 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5228 ifsm = nlmsg_data(nlh);
5229 if (ifsm->ifindex > 0)
5230 dev = __dev_get_by_index(net, ifsm->ifindex);
5237 filter_mask = ifsm->filter_mask;
5241 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5245 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5246 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5247 0, filter_mask, &idxattr, &prividx);
5249 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5250 WARN_ON(err == -EMSGSIZE);
5253 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5259 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5261 struct netlink_ext_ack *extack = cb->extack;
5262 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5263 struct net *net = sock_net(skb->sk);
5264 unsigned int flags = NLM_F_MULTI;
5265 struct if_stats_msg *ifsm;
5266 struct hlist_head *head;
5267 struct net_device *dev;
5268 u32 filter_mask = 0;
5272 s_idx = cb->args[1];
5273 s_idxattr = cb->args[2];
5274 s_prividx = cb->args[3];
5276 cb->seq = net->dev_base_seq;
5278 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5282 ifsm = nlmsg_data(cb->nlh);
5283 filter_mask = ifsm->filter_mask;
5285 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5289 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5291 head = &net->dev_index_head[h];
5292 hlist_for_each_entry(dev, head, index_hlist) {
5295 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5296 NETLINK_CB(cb->skb).portid,
5297 cb->nlh->nlmsg_seq, 0,
5299 &s_idxattr, &s_prividx);
5300 /* If we ran out of room on the first message,
5303 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5309 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5315 cb->args[3] = s_prividx;
5316 cb->args[2] = s_idxattr;
5323 /* Process one rtnetlink message. */
5325 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5326 struct netlink_ext_ack *extack)
5328 struct net *net = sock_net(skb->sk);
5329 struct rtnl_link *link;
5330 struct module *owner;
5331 int err = -EOPNOTSUPP;
5332 rtnl_doit_func doit;
5338 type = nlh->nlmsg_type;
5344 /* All the messages must have at least 1 byte length */
5345 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5348 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5351 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5355 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5357 rtnl_dumpit_func dumpit;
5358 u16 min_dump_alloc = 0;
5360 link = rtnl_get_link(family, type);
5361 if (!link || !link->dumpit) {
5363 link = rtnl_get_link(family, type);
5364 if (!link || !link->dumpit)
5367 owner = link->owner;
5368 dumpit = link->dumpit;
5370 if (type == RTM_GETLINK - RTM_BASE)
5371 min_dump_alloc = rtnl_calcit(skb, nlh);
5374 /* need to do this before rcu_read_unlock() */
5375 if (!try_module_get(owner))
5376 err = -EPROTONOSUPPORT;
5382 struct netlink_dump_control c = {
5384 .min_dump_alloc = min_dump_alloc,
5387 err = netlink_dump_start(rtnl, skb, nlh, &c);
5388 /* netlink_dump_start() will keep a reference on
5389 * module if dump is still in progress.
5396 link = rtnl_get_link(family, type);
5397 if (!link || !link->doit) {
5399 link = rtnl_get_link(PF_UNSPEC, type);
5400 if (!link || !link->doit)
5404 owner = link->owner;
5405 if (!try_module_get(owner)) {
5406 err = -EPROTONOSUPPORT;
5410 flags = link->flags;
5411 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5415 err = doit(skb, nlh, extack);
5422 link = rtnl_get_link(family, type);
5423 if (link && link->doit)
5424 err = link->doit(skb, nlh, extack);
5440 static void rtnetlink_rcv(struct sk_buff *skb)
5442 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5445 static int rtnetlink_bind(struct net *net, int group)
5448 case RTNLGRP_IPV4_MROUTE_R:
5449 case RTNLGRP_IPV6_MROUTE_R:
5450 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5457 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5459 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5463 case NETDEV_CHANGEMTU:
5464 case NETDEV_CHANGEADDR:
5465 case NETDEV_CHANGENAME:
5466 case NETDEV_FEAT_CHANGE:
5467 case NETDEV_BONDING_FAILOVER:
5468 case NETDEV_POST_TYPE_CHANGE:
5469 case NETDEV_NOTIFY_PEERS:
5470 case NETDEV_CHANGEUPPER:
5471 case NETDEV_RESEND_IGMP:
5472 case NETDEV_CHANGEINFODATA:
5473 case NETDEV_CHANGELOWERSTATE:
5474 case NETDEV_CHANGE_TX_QUEUE_LEN:
5475 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5476 GFP_KERNEL, NULL, 0);
5484 static struct notifier_block rtnetlink_dev_notifier = {
5485 .notifier_call = rtnetlink_event,
5489 static int __net_init rtnetlink_net_init(struct net *net)
5492 struct netlink_kernel_cfg cfg = {
5493 .groups = RTNLGRP_MAX,
5494 .input = rtnetlink_rcv,
5495 .cb_mutex = &rtnl_mutex,
5496 .flags = NL_CFG_F_NONROOT_RECV,
5497 .bind = rtnetlink_bind,
5500 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5507 static void __net_exit rtnetlink_net_exit(struct net *net)
5509 netlink_kernel_release(net->rtnl);
5513 static struct pernet_operations rtnetlink_net_ops = {
5514 .init = rtnetlink_net_init,
5515 .exit = rtnetlink_net_exit,
5518 void __init rtnetlink_init(void)
5520 if (register_pernet_subsys(&rtnetlink_net_ops))
5521 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5523 register_netdevice_notifier(&rtnetlink_dev_notifier);
5525 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5526 rtnl_dump_ifinfo, 0);
5527 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5528 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5529 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5531 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5532 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5533 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5535 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5536 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5538 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5539 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5540 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5542 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5543 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5544 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5546 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,