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.
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;
1222 memset(&ivi, 0, sizeof(ivi));
1224 /* Not all SR-IOV capable drivers support the
1225 * spoofcheck and "RSS query enable" query. Preset to
1226 * -1 so the user space tool can detect that the driver
1227 * didn't report anything.
1230 ivi.rss_query_en = -1;
1232 /* The default value for VF link state is "auto"
1233 * IFLA_VF_LINK_STATE_AUTO which equals zero
1236 /* VLAN Protocol by default is 802.1Q */
1237 ivi.vlan_proto = htons(ETH_P_8021Q);
1238 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1241 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1250 vf_rss_query_en.vf =
1251 vf_trust.vf = ivi.vf;
1253 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1254 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1255 vf_vlan.vlan = ivi.vlan;
1256 vf_vlan.qos = ivi.qos;
1257 vf_vlan_info.vlan = ivi.vlan;
1258 vf_vlan_info.qos = ivi.qos;
1259 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1260 vf_tx_rate.rate = ivi.max_tx_rate;
1261 vf_rate.min_tx_rate = ivi.min_tx_rate;
1262 vf_rate.max_tx_rate = ivi.max_tx_rate;
1263 vf_spoofchk.setting = ivi.spoofchk;
1264 vf_linkstate.link_state = ivi.linkstate;
1265 vf_rss_query_en.setting = ivi.rss_query_en;
1266 vf_trust.setting = ivi.trusted;
1267 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1269 goto nla_put_vfinfo_failure;
1270 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1271 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1272 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1273 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1275 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1277 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1279 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1281 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1282 sizeof(vf_rss_query_en),
1283 &vf_rss_query_en) ||
1284 nla_put(skb, IFLA_VF_TRUST,
1285 sizeof(vf_trust), &vf_trust))
1286 goto nla_put_vf_failure;
1287 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1289 goto nla_put_vf_failure;
1290 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1292 nla_nest_cancel(skb, vfvlanlist);
1293 goto nla_put_vf_failure;
1295 nla_nest_end(skb, vfvlanlist);
1296 memset(&vf_stats, 0, sizeof(vf_stats));
1297 if (dev->netdev_ops->ndo_get_vf_stats)
1298 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1300 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1302 goto nla_put_vf_failure;
1303 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1304 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1305 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1306 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1307 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1308 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1309 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1310 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1311 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1312 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1313 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1314 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1315 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1316 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1317 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1318 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1319 nla_nest_cancel(skb, vfstats);
1320 goto nla_put_vf_failure;
1322 nla_nest_end(skb, vfstats);
1323 nla_nest_end(skb, vf);
1327 nla_nest_cancel(skb, vf);
1328 nla_put_vfinfo_failure:
1329 nla_nest_cancel(skb, vfinfo);
1333 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1334 struct net_device *dev,
1335 u32 ext_filter_mask)
1337 struct nlattr *vfinfo;
1340 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1343 num_vfs = dev_num_vf(dev->dev.parent);
1344 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1347 if (!dev->netdev_ops->ndo_get_vf_config)
1350 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1354 for (i = 0; i < num_vfs; i++) {
1355 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1359 nla_nest_end(skb, vfinfo);
1363 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1365 struct rtnl_link_ifmap map;
1367 memset(&map, 0, sizeof(map));
1368 map.mem_start = dev->mem_start;
1369 map.mem_end = dev->mem_end;
1370 map.base_addr = dev->base_addr;
1373 map.port = dev->if_port;
1375 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1381 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1383 const struct bpf_prog *generic_xdp_prog;
1387 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1388 if (!generic_xdp_prog)
1390 return generic_xdp_prog->aux->id;
1393 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1395 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1398 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1400 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1404 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1405 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1406 u32 (*get_prog_id)(struct net_device *dev))
1411 curr_id = get_prog_id(dev);
1416 err = nla_put_u32(skb, attr, curr_id);
1420 if (*mode != XDP_ATTACHED_NONE)
1421 *mode = XDP_ATTACHED_MULTI;
1428 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1435 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1440 mode = XDP_ATTACHED_NONE;
1441 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1442 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1445 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1446 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1449 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1450 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1454 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1458 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1459 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1464 nla_nest_end(skb, xdp);
1468 nla_nest_cancel(skb, xdp);
1472 static u32 rtnl_get_event(unsigned long event)
1474 u32 rtnl_event_type = IFLA_EVENT_NONE;
1478 rtnl_event_type = IFLA_EVENT_REBOOT;
1480 case NETDEV_FEAT_CHANGE:
1481 rtnl_event_type = IFLA_EVENT_FEATURES;
1483 case NETDEV_BONDING_FAILOVER:
1484 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1486 case NETDEV_NOTIFY_PEERS:
1487 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1489 case NETDEV_RESEND_IGMP:
1490 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1492 case NETDEV_CHANGEINFODATA:
1493 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1499 return rtnl_event_type;
1502 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1504 const struct net_device *upper_dev;
1509 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1511 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1517 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1520 int ifindex = dev_get_iflink(dev);
1522 if (force || dev->ifindex != ifindex)
1523 return nla_put_u32(skb, IFLA_LINK, ifindex);
1528 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1529 struct net_device *dev)
1534 ret = dev_get_alias(dev, buf, sizeof(buf));
1535 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1538 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1539 const struct net_device *dev,
1540 struct net *src_net, gfp_t gfp)
1542 bool put_iflink = false;
1544 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1545 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1547 if (!net_eq(dev_net(dev), link_net)) {
1548 int id = peernet2id_alloc(src_net, link_net, gfp);
1550 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1557 return nla_put_iflink(skb, dev, put_iflink);
1560 static int rtnl_fill_link_af(struct sk_buff *skb,
1561 const struct net_device *dev,
1562 u32 ext_filter_mask)
1564 const struct rtnl_af_ops *af_ops;
1565 struct nlattr *af_spec;
1567 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1571 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1575 if (!af_ops->fill_link_af)
1578 af = nla_nest_start_noflag(skb, af_ops->family);
1582 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1584 * Caller may return ENODATA to indicate that there
1585 * was no data to be dumped. This is not an error, it
1586 * means we should trim the attribute header and
1589 if (err == -ENODATA)
1590 nla_nest_cancel(skb, af);
1594 nla_nest_end(skb, af);
1597 nla_nest_end(skb, af_spec);
1601 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1602 const struct net_device *dev)
1604 struct netdev_name_node *name_node;
1607 list_for_each_entry(name_node, &dev->name_node->list, list) {
1608 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1615 static int rtnl_fill_prop_list(struct sk_buff *skb,
1616 const struct net_device *dev)
1618 struct nlattr *prop_list;
1621 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1625 ret = rtnl_fill_alt_ifnames(skb, dev);
1629 nla_nest_end(skb, prop_list);
1633 nla_nest_cancel(skb, prop_list);
1637 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1638 struct net_device *dev, struct net *src_net,
1639 int type, u32 pid, u32 seq, u32 change,
1640 unsigned int flags, u32 ext_filter_mask,
1641 u32 event, int *new_nsid, int new_ifindex,
1642 int tgt_netnsid, gfp_t gfp)
1644 struct ifinfomsg *ifm;
1645 struct nlmsghdr *nlh;
1648 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1652 ifm = nlmsg_data(nlh);
1653 ifm->ifi_family = AF_UNSPEC;
1655 ifm->ifi_type = dev->type;
1656 ifm->ifi_index = dev->ifindex;
1657 ifm->ifi_flags = dev_get_flags(dev);
1658 ifm->ifi_change = change;
1660 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1661 goto nla_put_failure;
1663 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1664 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1665 nla_put_u8(skb, IFLA_OPERSTATE,
1666 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1667 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1668 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1669 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1670 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1671 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1672 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1673 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1674 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1675 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1677 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1679 put_master_ifindex(skb, dev) ||
1680 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1682 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1683 nla_put_ifalias(skb, dev) ||
1684 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1685 atomic_read(&dev->carrier_up_count) +
1686 atomic_read(&dev->carrier_down_count)) ||
1687 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1688 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1689 atomic_read(&dev->carrier_up_count)) ||
1690 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1691 atomic_read(&dev->carrier_down_count)))
1692 goto nla_put_failure;
1694 if (event != IFLA_EVENT_NONE) {
1695 if (nla_put_u32(skb, IFLA_EVENT, event))
1696 goto nla_put_failure;
1699 if (rtnl_fill_link_ifmap(skb, dev))
1700 goto nla_put_failure;
1702 if (dev->addr_len) {
1703 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1704 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1705 goto nla_put_failure;
1708 if (rtnl_phys_port_id_fill(skb, dev))
1709 goto nla_put_failure;
1711 if (rtnl_phys_port_name_fill(skb, dev))
1712 goto nla_put_failure;
1714 if (rtnl_phys_switch_id_fill(skb, dev))
1715 goto nla_put_failure;
1717 if (rtnl_fill_stats(skb, dev))
1718 goto nla_put_failure;
1720 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1721 goto nla_put_failure;
1723 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1724 goto nla_put_failure;
1726 if (rtnl_xdp_fill(skb, dev))
1727 goto nla_put_failure;
1729 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1730 if (rtnl_link_fill(skb, dev) < 0)
1731 goto nla_put_failure;
1734 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1735 goto nla_put_failure;
1738 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1739 goto nla_put_failure;
1741 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1742 goto nla_put_failure;
1746 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1747 goto nla_put_failure_rcu;
1750 if (rtnl_fill_prop_list(skb, dev))
1751 goto nla_put_failure;
1753 nlmsg_end(skb, nlh);
1756 nla_put_failure_rcu:
1759 nlmsg_cancel(skb, nlh);
1763 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1764 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1765 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1766 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1767 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1768 [IFLA_MTU] = { .type = NLA_U32 },
1769 [IFLA_LINK] = { .type = NLA_U32 },
1770 [IFLA_MASTER] = { .type = NLA_U32 },
1771 [IFLA_CARRIER] = { .type = NLA_U8 },
1772 [IFLA_TXQLEN] = { .type = NLA_U32 },
1773 [IFLA_WEIGHT] = { .type = NLA_U32 },
1774 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1775 [IFLA_LINKMODE] = { .type = NLA_U8 },
1776 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1777 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1778 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1779 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1780 * allow 0-length string (needed to remove an alias).
1782 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1783 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1784 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1785 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1786 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1787 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1788 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1789 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1790 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1791 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1792 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1793 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1794 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1795 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1796 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1797 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1798 [IFLA_XDP] = { .type = NLA_NESTED },
1799 [IFLA_EVENT] = { .type = NLA_U32 },
1800 [IFLA_GROUP] = { .type = NLA_U32 },
1801 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1802 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1803 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1804 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1805 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1806 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1807 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1808 .len = ALTIFNAMSIZ - 1 },
1811 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1812 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1813 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1814 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1815 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1818 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1819 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1820 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1821 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1822 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1823 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1824 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1825 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1826 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1827 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1828 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1829 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1830 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1831 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1834 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1835 [IFLA_PORT_VF] = { .type = NLA_U32 },
1836 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1837 .len = PORT_PROFILE_MAX },
1838 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1839 .len = PORT_UUID_MAX },
1840 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1841 .len = PORT_UUID_MAX },
1842 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1843 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1845 /* Unused, but we need to keep it here since user space could
1846 * fill it. It's also broken with regard to NLA_BINARY use in
1847 * combination with structs.
1849 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1850 .len = sizeof(struct ifla_port_vsi) },
1853 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1854 [IFLA_XDP_FD] = { .type = NLA_S32 },
1855 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1856 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1857 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1860 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1862 const struct rtnl_link_ops *ops = NULL;
1863 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1865 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1868 if (linfo[IFLA_INFO_KIND]) {
1869 char kind[MODULE_NAME_LEN];
1871 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1872 ops = rtnl_link_ops_get(kind);
1878 static bool link_master_filtered(struct net_device *dev, int master_idx)
1880 struct net_device *master;
1885 master = netdev_master_upper_dev_get(dev);
1886 if (!master || master->ifindex != master_idx)
1892 static bool link_kind_filtered(const struct net_device *dev,
1893 const struct rtnl_link_ops *kind_ops)
1895 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1901 static bool link_dump_filtered(struct net_device *dev,
1903 const struct rtnl_link_ops *kind_ops)
1905 if (link_master_filtered(dev, master_idx) ||
1906 link_kind_filtered(dev, kind_ops))
1913 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1914 * @sk: netlink socket
1915 * @netnsid: network namespace identifier
1917 * Returns the network namespace identified by netnsid on success or an error
1918 * pointer on failure.
1920 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1924 net = get_net_ns_by_id(sock_net(sk), netnsid);
1926 return ERR_PTR(-EINVAL);
1928 /* For now, the caller is required to have CAP_NET_ADMIN in
1929 * the user namespace owning the target net ns.
1931 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1933 return ERR_PTR(-EACCES);
1937 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1939 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1940 bool strict_check, struct nlattr **tb,
1941 struct netlink_ext_ack *extack)
1946 struct ifinfomsg *ifm;
1948 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1949 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1953 ifm = nlmsg_data(nlh);
1954 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1956 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1959 if (ifm->ifi_index) {
1960 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1964 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1965 IFLA_MAX, ifla_policy,
1969 /* A hack to preserve kernel<->userspace interface.
1970 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1971 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1972 * what iproute2 < v3.9.0 used.
1973 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1974 * attribute, its netlink message is shorter than struct ifinfomsg.
1976 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1977 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1979 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
1983 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1985 struct netlink_ext_ack *extack = cb->extack;
1986 const struct nlmsghdr *nlh = cb->nlh;
1987 struct net *net = sock_net(skb->sk);
1988 struct net *tgt_net = net;
1991 struct net_device *dev;
1992 struct hlist_head *head;
1993 struct nlattr *tb[IFLA_MAX+1];
1994 u32 ext_filter_mask = 0;
1995 const struct rtnl_link_ops *kind_ops = NULL;
1996 unsigned int flags = NLM_F_MULTI;
2002 s_idx = cb->args[1];
2004 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2006 if (cb->strict_check)
2012 for (i = 0; i <= IFLA_MAX; ++i) {
2016 /* new attributes should only be added with strict checking */
2018 case IFLA_TARGET_NETNSID:
2019 netnsid = nla_get_s32(tb[i]);
2020 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2021 if (IS_ERR(tgt_net)) {
2022 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2023 return PTR_ERR(tgt_net);
2027 ext_filter_mask = nla_get_u32(tb[i]);
2030 master_idx = nla_get_u32(tb[i]);
2033 kind_ops = linkinfo_to_kind_ops(tb[i]);
2036 if (cb->strict_check) {
2037 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2043 if (master_idx || kind_ops)
2044 flags |= NLM_F_DUMP_FILTERED;
2047 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2049 head = &tgt_net->dev_index_head[h];
2050 hlist_for_each_entry(dev, head, index_hlist) {
2051 if (link_dump_filtered(dev, master_idx, kind_ops))
2055 err = rtnl_fill_ifinfo(skb, dev, net,
2057 NETLINK_CB(cb->skb).portid,
2058 nlh->nlmsg_seq, 0, flags,
2059 ext_filter_mask, 0, NULL, 0,
2060 netnsid, GFP_KERNEL);
2063 if (likely(skb->len))
2077 cb->seq = net->dev_base_seq;
2078 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2085 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2086 struct netlink_ext_ack *exterr)
2088 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2091 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2093 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2096 /* Examine the link attributes and figure out which
2097 * network namespace we are talking about.
2099 if (tb[IFLA_NET_NS_PID])
2100 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2101 else if (tb[IFLA_NET_NS_FD])
2102 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2104 net = get_net(src_net);
2107 EXPORT_SYMBOL(rtnl_link_get_net);
2109 /* Figure out which network namespace we are talking about by
2110 * examining the link attributes in the following order:
2112 * 1. IFLA_NET_NS_PID
2114 * 3. IFLA_TARGET_NETNSID
2116 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2117 struct nlattr *tb[])
2121 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2122 return rtnl_link_get_net(src_net, tb);
2124 if (!tb[IFLA_TARGET_NETNSID])
2125 return get_net(src_net);
2127 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2129 return ERR_PTR(-EINVAL);
2134 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2135 struct net *src_net,
2136 struct nlattr *tb[], int cap)
2140 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2144 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2146 return ERR_PTR(-EPERM);
2152 /* Verify that rtnetlink requests do not pass additional properties
2153 * potentially referring to different network namespaces.
2155 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2156 struct netlink_ext_ack *extack,
2160 if (netns_id_only) {
2161 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2164 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2168 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2171 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2174 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2180 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2184 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2187 if (tb[IFLA_ADDRESS] &&
2188 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2191 if (tb[IFLA_BROADCAST] &&
2192 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2196 if (tb[IFLA_AF_SPEC]) {
2200 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2201 const struct rtnl_af_ops *af_ops;
2204 af_ops = rtnl_af_lookup(nla_type(af));
2207 return -EAFNOSUPPORT;
2210 if (!af_ops->set_link_af) {
2215 if (af_ops->validate_link_af) {
2216 err = af_ops->validate_link_af(dev, af);
2230 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2233 const struct net_device_ops *ops = dev->netdev_ops;
2235 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2238 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2240 if (dev->type != ARPHRD_INFINIBAND)
2243 return handle_infiniband_guid(dev, ivt, guid_type);
2246 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2248 const struct net_device_ops *ops = dev->netdev_ops;
2251 if (tb[IFLA_VF_MAC]) {
2252 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2255 if (ops->ndo_set_vf_mac)
2256 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2262 if (tb[IFLA_VF_VLAN]) {
2263 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2266 if (ops->ndo_set_vf_vlan)
2267 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2269 htons(ETH_P_8021Q));
2274 if (tb[IFLA_VF_VLAN_LIST]) {
2275 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2276 struct nlattr *attr;
2280 if (!ops->ndo_set_vf_vlan)
2283 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2284 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2285 nla_len(attr) < NLA_HDRLEN) {
2288 if (len >= MAX_VLAN_LIST_LEN)
2290 ivvl[len] = nla_data(attr);
2297 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2298 ivvl[0]->qos, ivvl[0]->vlan_proto);
2303 if (tb[IFLA_VF_TX_RATE]) {
2304 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2305 struct ifla_vf_info ivf;
2308 if (ops->ndo_get_vf_config)
2309 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2314 if (ops->ndo_set_vf_rate)
2315 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2322 if (tb[IFLA_VF_RATE]) {
2323 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2326 if (ops->ndo_set_vf_rate)
2327 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2334 if (tb[IFLA_VF_SPOOFCHK]) {
2335 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2338 if (ops->ndo_set_vf_spoofchk)
2339 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2345 if (tb[IFLA_VF_LINK_STATE]) {
2346 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2349 if (ops->ndo_set_vf_link_state)
2350 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2356 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2357 struct ifla_vf_rss_query_en *ivrssq_en;
2360 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2361 if (ops->ndo_set_vf_rss_query_en)
2362 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2363 ivrssq_en->setting);
2368 if (tb[IFLA_VF_TRUST]) {
2369 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2372 if (ops->ndo_set_vf_trust)
2373 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2378 if (tb[IFLA_VF_IB_NODE_GUID]) {
2379 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2381 if (!ops->ndo_set_vf_guid)
2384 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2387 if (tb[IFLA_VF_IB_PORT_GUID]) {
2388 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2390 if (!ops->ndo_set_vf_guid)
2393 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2399 static int do_set_master(struct net_device *dev, int ifindex,
2400 struct netlink_ext_ack *extack)
2402 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2403 const struct net_device_ops *ops;
2407 if (upper_dev->ifindex == ifindex)
2409 ops = upper_dev->netdev_ops;
2410 if (ops->ndo_del_slave) {
2411 err = ops->ndo_del_slave(upper_dev, dev);
2414 netdev_update_lockdep_key(dev);
2421 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2424 ops = upper_dev->netdev_ops;
2425 if (ops->ndo_add_slave) {
2426 err = ops->ndo_add_slave(upper_dev, dev, extack);
2436 #define DO_SETLINK_MODIFIED 0x01
2437 /* notify flag means notify + modified. */
2438 #define DO_SETLINK_NOTIFY 0x03
2439 static int do_setlink(const struct sk_buff *skb,
2440 struct net_device *dev, struct ifinfomsg *ifm,
2441 struct netlink_ext_ack *extack,
2442 struct nlattr **tb, char *ifname, int status)
2444 const struct net_device_ops *ops = dev->netdev_ops;
2447 err = validate_linkmsg(dev, tb);
2451 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2452 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2459 err = dev_change_net_namespace(dev, net, ifname);
2463 status |= DO_SETLINK_MODIFIED;
2467 struct rtnl_link_ifmap *u_map;
2470 if (!ops->ndo_set_config) {
2475 if (!netif_device_present(dev)) {
2480 u_map = nla_data(tb[IFLA_MAP]);
2481 k_map.mem_start = (unsigned long) u_map->mem_start;
2482 k_map.mem_end = (unsigned long) u_map->mem_end;
2483 k_map.base_addr = (unsigned short) u_map->base_addr;
2484 k_map.irq = (unsigned char) u_map->irq;
2485 k_map.dma = (unsigned char) u_map->dma;
2486 k_map.port = (unsigned char) u_map->port;
2488 err = ops->ndo_set_config(dev, &k_map);
2492 status |= DO_SETLINK_NOTIFY;
2495 if (tb[IFLA_ADDRESS]) {
2496 struct sockaddr *sa;
2499 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2501 sa = kmalloc(len, GFP_KERNEL);
2506 sa->sa_family = dev->type;
2507 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2509 err = dev_set_mac_address(dev, sa, extack);
2513 status |= DO_SETLINK_MODIFIED;
2517 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2520 status |= DO_SETLINK_MODIFIED;
2523 if (tb[IFLA_GROUP]) {
2524 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2525 status |= DO_SETLINK_NOTIFY;
2529 * Interface selected by interface index but interface
2530 * name provided implies that a name change has been
2533 if (ifm->ifi_index > 0 && ifname[0]) {
2534 err = dev_change_name(dev, ifname);
2537 status |= DO_SETLINK_MODIFIED;
2540 if (tb[IFLA_IFALIAS]) {
2541 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2542 nla_len(tb[IFLA_IFALIAS]));
2545 status |= DO_SETLINK_NOTIFY;
2548 if (tb[IFLA_BROADCAST]) {
2549 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2550 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2553 if (ifm->ifi_flags || ifm->ifi_change) {
2554 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2560 if (tb[IFLA_MASTER]) {
2561 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2564 status |= DO_SETLINK_MODIFIED;
2567 if (tb[IFLA_CARRIER]) {
2568 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2571 status |= DO_SETLINK_MODIFIED;
2574 if (tb[IFLA_TXQLEN]) {
2575 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2577 err = dev_change_tx_queue_len(dev, value);
2580 status |= DO_SETLINK_MODIFIED;
2583 if (tb[IFLA_GSO_MAX_SIZE]) {
2584 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2586 if (max_size > GSO_MAX_SIZE) {
2591 if (dev->gso_max_size ^ max_size) {
2592 netif_set_gso_max_size(dev, max_size);
2593 status |= DO_SETLINK_MODIFIED;
2597 if (tb[IFLA_GSO_MAX_SEGS]) {
2598 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2600 if (max_segs > GSO_MAX_SEGS) {
2605 if (dev->gso_max_segs ^ max_segs) {
2606 dev->gso_max_segs = max_segs;
2607 status |= DO_SETLINK_MODIFIED;
2611 if (tb[IFLA_OPERSTATE])
2612 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2614 if (tb[IFLA_LINKMODE]) {
2615 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2617 write_lock_bh(&dev_base_lock);
2618 if (dev->link_mode ^ value)
2619 status |= DO_SETLINK_NOTIFY;
2620 dev->link_mode = value;
2621 write_unlock_bh(&dev_base_lock);
2624 if (tb[IFLA_VFINFO_LIST]) {
2625 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2626 struct nlattr *attr;
2629 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2630 if (nla_type(attr) != IFLA_VF_INFO ||
2631 nla_len(attr) < NLA_HDRLEN) {
2635 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2641 err = do_setvfinfo(dev, vfinfo);
2644 status |= DO_SETLINK_NOTIFY;
2649 if (tb[IFLA_VF_PORTS]) {
2650 struct nlattr *port[IFLA_PORT_MAX+1];
2651 struct nlattr *attr;
2656 if (!ops->ndo_set_vf_port)
2659 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2660 if (nla_type(attr) != IFLA_VF_PORT ||
2661 nla_len(attr) < NLA_HDRLEN) {
2665 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2671 if (!port[IFLA_PORT_VF]) {
2675 vf = nla_get_u32(port[IFLA_PORT_VF]);
2676 err = ops->ndo_set_vf_port(dev, vf, port);
2679 status |= DO_SETLINK_NOTIFY;
2684 if (tb[IFLA_PORT_SELF]) {
2685 struct nlattr *port[IFLA_PORT_MAX+1];
2687 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2689 ifla_port_policy, NULL);
2694 if (ops->ndo_set_vf_port)
2695 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2698 status |= DO_SETLINK_NOTIFY;
2701 if (tb[IFLA_AF_SPEC]) {
2705 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2706 const struct rtnl_af_ops *af_ops;
2710 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2712 err = af_ops->set_link_af(dev, af);
2719 status |= DO_SETLINK_NOTIFY;
2724 if (tb[IFLA_PROTO_DOWN]) {
2725 err = dev_change_proto_down(dev,
2726 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2729 status |= DO_SETLINK_NOTIFY;
2733 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2736 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2738 ifla_xdp_policy, NULL);
2742 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2747 if (xdp[IFLA_XDP_FLAGS]) {
2748 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2749 if (xdp_flags & ~XDP_FLAGS_MASK) {
2753 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2759 if (xdp[IFLA_XDP_FD]) {
2760 err = dev_change_xdp_fd(dev, extack,
2761 nla_get_s32(xdp[IFLA_XDP_FD]),
2765 status |= DO_SETLINK_NOTIFY;
2770 if (status & DO_SETLINK_MODIFIED) {
2771 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2772 netdev_state_change(dev);
2775 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",
2782 static struct net_device *rtnl_dev_get(struct net *net,
2783 struct nlattr *ifname_attr,
2784 struct nlattr *altifname_attr,
2787 char buffer[ALTIFNAMSIZ];
2792 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2793 else if (altifname_attr)
2794 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2799 return __dev_get_by_name(net, ifname);
2802 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2803 struct netlink_ext_ack *extack)
2805 struct net *net = sock_net(skb->sk);
2806 struct ifinfomsg *ifm;
2807 struct net_device *dev;
2809 struct nlattr *tb[IFLA_MAX+1];
2810 char ifname[IFNAMSIZ];
2812 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2813 ifla_policy, extack);
2817 err = rtnl_ensure_unique_netns(tb, extack, false);
2821 if (tb[IFLA_IFNAME])
2822 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2827 ifm = nlmsg_data(nlh);
2828 if (ifm->ifi_index > 0)
2829 dev = __dev_get_by_index(net, ifm->ifi_index);
2830 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2831 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2840 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2845 static int rtnl_group_dellink(const struct net *net, int group)
2847 struct net_device *dev, *aux;
2848 LIST_HEAD(list_kill);
2854 for_each_netdev(net, dev) {
2855 if (dev->group == group) {
2856 const struct rtnl_link_ops *ops;
2859 ops = dev->rtnl_link_ops;
2860 if (!ops || !ops->dellink)
2868 for_each_netdev_safe(net, dev, aux) {
2869 if (dev->group == group) {
2870 const struct rtnl_link_ops *ops;
2872 ops = dev->rtnl_link_ops;
2873 ops->dellink(dev, &list_kill);
2876 unregister_netdevice_many(&list_kill);
2881 int rtnl_delete_link(struct net_device *dev)
2883 const struct rtnl_link_ops *ops;
2884 LIST_HEAD(list_kill);
2886 ops = dev->rtnl_link_ops;
2887 if (!ops || !ops->dellink)
2890 ops->dellink(dev, &list_kill);
2891 unregister_netdevice_many(&list_kill);
2895 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2897 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2898 struct netlink_ext_ack *extack)
2900 struct net *net = sock_net(skb->sk);
2901 struct net *tgt_net = net;
2902 struct net_device *dev = NULL;
2903 struct ifinfomsg *ifm;
2904 struct nlattr *tb[IFLA_MAX+1];
2908 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2909 ifla_policy, extack);
2913 err = rtnl_ensure_unique_netns(tb, extack, true);
2917 if (tb[IFLA_TARGET_NETNSID]) {
2918 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2919 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2920 if (IS_ERR(tgt_net))
2921 return PTR_ERR(tgt_net);
2925 ifm = nlmsg_data(nlh);
2926 if (ifm->ifi_index > 0)
2927 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2928 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2929 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2930 tb[IFLA_ALT_IFNAME], NULL);
2931 else if (tb[IFLA_GROUP])
2932 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2937 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2943 err = rtnl_delete_link(dev);
2952 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2954 unsigned int old_flags;
2957 old_flags = dev->flags;
2958 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2959 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2965 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2966 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2968 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2969 __dev_notify_flags(dev, old_flags, ~0U);
2973 EXPORT_SYMBOL(rtnl_configure_link);
2975 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2976 unsigned char name_assign_type,
2977 const struct rtnl_link_ops *ops,
2978 struct nlattr *tb[],
2979 struct netlink_ext_ack *extack)
2981 struct net_device *dev;
2982 unsigned int num_tx_queues = 1;
2983 unsigned int num_rx_queues = 1;
2985 if (tb[IFLA_NUM_TX_QUEUES])
2986 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2987 else if (ops->get_num_tx_queues)
2988 num_tx_queues = ops->get_num_tx_queues();
2990 if (tb[IFLA_NUM_RX_QUEUES])
2991 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2992 else if (ops->get_num_rx_queues)
2993 num_rx_queues = ops->get_num_rx_queues();
2995 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2996 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
2997 return ERR_PTR(-EINVAL);
3000 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3001 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3002 return ERR_PTR(-EINVAL);
3005 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3006 ops->setup, num_tx_queues, num_rx_queues);
3008 return ERR_PTR(-ENOMEM);
3010 dev_net_set(dev, net);
3011 dev->rtnl_link_ops = ops;
3012 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3015 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
3016 if (tb[IFLA_ADDRESS]) {
3017 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3018 nla_len(tb[IFLA_ADDRESS]));
3019 dev->addr_assign_type = NET_ADDR_SET;
3021 if (tb[IFLA_BROADCAST])
3022 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3023 nla_len(tb[IFLA_BROADCAST]));
3024 if (tb[IFLA_TXQLEN])
3025 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3026 if (tb[IFLA_OPERSTATE])
3027 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3028 if (tb[IFLA_LINKMODE])
3029 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3031 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3032 if (tb[IFLA_GSO_MAX_SIZE])
3033 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3034 if (tb[IFLA_GSO_MAX_SEGS])
3035 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3039 EXPORT_SYMBOL(rtnl_create_link);
3041 static int rtnl_group_changelink(const struct sk_buff *skb,
3042 struct net *net, int group,
3043 struct ifinfomsg *ifm,
3044 struct netlink_ext_ack *extack,
3047 struct net_device *dev, *aux;
3050 for_each_netdev_safe(net, dev, aux) {
3051 if (dev->group == group) {
3052 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3061 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3062 struct nlattr **attr, struct netlink_ext_ack *extack)
3064 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3065 unsigned char name_assign_type = NET_NAME_USER;
3066 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3067 const struct rtnl_link_ops *m_ops = NULL;
3068 struct net_device *master_dev = NULL;
3069 struct net *net = sock_net(skb->sk);
3070 const struct rtnl_link_ops *ops;
3071 struct nlattr *tb[IFLA_MAX + 1];
3072 struct net *dest_net, *link_net;
3073 struct nlattr **slave_data;
3074 char kind[MODULE_NAME_LEN];
3075 struct net_device *dev;
3076 struct ifinfomsg *ifm;
3077 char ifname[IFNAMSIZ];
3078 struct nlattr **data;
3081 #ifdef CONFIG_MODULES
3084 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3085 ifla_policy, extack);
3089 err = rtnl_ensure_unique_netns(tb, extack, false);
3093 if (tb[IFLA_IFNAME])
3094 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3098 ifm = nlmsg_data(nlh);
3099 if (ifm->ifi_index > 0)
3100 dev = __dev_get_by_index(net, ifm->ifi_index);
3101 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3102 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3107 master_dev = netdev_master_upper_dev_get(dev);
3109 m_ops = master_dev->rtnl_link_ops;
3112 err = validate_linkmsg(dev, tb);
3116 if (tb[IFLA_LINKINFO]) {
3117 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3119 ifla_info_policy, NULL);
3123 memset(linkinfo, 0, sizeof(linkinfo));
3125 if (linkinfo[IFLA_INFO_KIND]) {
3126 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3127 ops = rtnl_link_ops_get(kind);
3135 if (ops->maxtype > RTNL_MAX_TYPE)
3138 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3139 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3140 linkinfo[IFLA_INFO_DATA],
3141 ops->policy, extack);
3146 if (ops->validate) {
3147 err = ops->validate(tb, data, extack);
3155 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3158 if (m_ops->slave_maxtype &&
3159 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3160 err = nla_parse_nested_deprecated(slave_attr,
3161 m_ops->slave_maxtype,
3162 linkinfo[IFLA_INFO_SLAVE_DATA],
3163 m_ops->slave_policy,
3167 slave_data = slave_attr;
3174 if (nlh->nlmsg_flags & NLM_F_EXCL)
3176 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3179 if (linkinfo[IFLA_INFO_DATA]) {
3180 if (!ops || ops != dev->rtnl_link_ops ||
3184 err = ops->changelink(dev, tb, data, extack);
3187 status |= DO_SETLINK_NOTIFY;
3190 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3191 if (!m_ops || !m_ops->slave_changelink)
3194 err = m_ops->slave_changelink(master_dev, dev, tb,
3195 slave_data, extack);
3198 status |= DO_SETLINK_NOTIFY;
3201 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3204 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3205 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3206 return rtnl_group_changelink(skb, net,
3207 nla_get_u32(tb[IFLA_GROUP]),
3212 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3216 #ifdef CONFIG_MODULES
3219 request_module("rtnl-link-%s", kind);
3221 ops = rtnl_link_ops_get(kind);
3226 NL_SET_ERR_MSG(extack, "Unknown device type");
3234 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3235 name_assign_type = NET_NAME_ENUM;
3238 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3239 if (IS_ERR(dest_net))
3240 return PTR_ERR(dest_net);
3242 if (tb[IFLA_LINK_NETNSID]) {
3243 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3245 link_net = get_net_ns_by_id(dest_net, id);
3247 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3252 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3258 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3259 name_assign_type, ops, tb, extack);
3265 dev->ifindex = ifm->ifi_index;
3268 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3269 /* Drivers should call free_netdev() in ->destructor
3270 * and unregister it on failure after registration
3271 * so that device could be finally freed in rtnl_unlock.
3274 /* If device is not registered at all, free it now */
3275 if (dev->reg_state == NETREG_UNINITIALIZED)
3280 err = register_netdevice(dev);
3286 err = rtnl_configure_link(dev, ifm);
3288 goto out_unregister;
3290 err = dev_change_net_namespace(dev, dest_net, ifname);
3292 goto out_unregister;
3294 if (tb[IFLA_MASTER]) {
3295 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3297 goto out_unregister;
3306 LIST_HEAD(list_kill);
3308 ops->dellink(dev, &list_kill);
3309 unregister_netdevice_many(&list_kill);
3311 unregister_netdevice(dev);
3316 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3317 struct netlink_ext_ack *extack)
3319 struct nlattr **attr;
3322 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3326 ret = __rtnl_newlink(skb, nlh, attr, extack);
3331 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3332 const struct nlmsghdr *nlh,
3334 struct netlink_ext_ack *extack)
3336 struct ifinfomsg *ifm;
3339 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3340 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3344 if (!netlink_strict_get_check(skb))
3345 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3346 ifla_policy, extack);
3348 ifm = nlmsg_data(nlh);
3349 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3351 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3355 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3356 ifla_policy, extack);
3360 for (i = 0; i <= IFLA_MAX; i++) {
3366 case IFLA_ALT_IFNAME:
3368 case IFLA_TARGET_NETNSID:
3371 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3379 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3380 struct netlink_ext_ack *extack)
3382 struct net *net = sock_net(skb->sk);
3383 struct net *tgt_net = net;
3384 struct ifinfomsg *ifm;
3385 struct nlattr *tb[IFLA_MAX+1];
3386 struct net_device *dev = NULL;
3387 struct sk_buff *nskb;
3390 u32 ext_filter_mask = 0;
3392 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3396 err = rtnl_ensure_unique_netns(tb, extack, true);
3400 if (tb[IFLA_TARGET_NETNSID]) {
3401 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3402 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3403 if (IS_ERR(tgt_net))
3404 return PTR_ERR(tgt_net);
3407 if (tb[IFLA_EXT_MASK])
3408 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3411 ifm = nlmsg_data(nlh);
3412 if (ifm->ifi_index > 0)
3413 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3414 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3415 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3416 tb[IFLA_ALT_IFNAME], NULL);
3425 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3429 err = rtnl_fill_ifinfo(nskb, dev, net,
3430 RTM_NEWLINK, NETLINK_CB(skb).portid,
3431 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3432 0, NULL, 0, netnsid, GFP_KERNEL);
3434 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3435 WARN_ON(err == -EMSGSIZE);
3438 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3446 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3447 bool *changed, struct netlink_ext_ack *extack)
3452 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3456 alt_ifname = nla_data(attr);
3457 if (cmd == RTM_NEWLINKPROP) {
3458 alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
3461 err = netdev_name_node_alt_create(dev, alt_ifname);
3466 } else if (cmd == RTM_DELLINKPROP) {
3467 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3479 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3480 struct netlink_ext_ack *extack)
3482 struct net *net = sock_net(skb->sk);
3483 struct nlattr *tb[IFLA_MAX + 1];
3484 struct net_device *dev;
3485 struct ifinfomsg *ifm;
3486 bool changed = false;
3487 struct nlattr *attr;
3490 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3494 err = rtnl_ensure_unique_netns(tb, extack, true);
3498 ifm = nlmsg_data(nlh);
3499 if (ifm->ifi_index > 0)
3500 dev = __dev_get_by_index(net, ifm->ifi_index);
3501 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3502 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3503 tb[IFLA_ALT_IFNAME], NULL);
3510 if (!tb[IFLA_PROP_LIST])
3513 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3514 switch (nla_type(attr)) {
3515 case IFLA_ALT_IFNAME:
3516 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3524 netdev_state_change(dev);
3528 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3529 struct netlink_ext_ack *extack)
3531 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3534 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3535 struct netlink_ext_ack *extack)
3537 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3540 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3542 struct net *net = sock_net(skb->sk);
3543 struct net_device *dev;
3544 struct nlattr *tb[IFLA_MAX+1];
3545 u32 ext_filter_mask = 0;
3546 u16 min_ifinfo_dump_size = 0;
3549 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3550 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3551 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3553 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3554 if (tb[IFLA_EXT_MASK])
3555 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3558 if (!ext_filter_mask)
3559 return NLMSG_GOODSIZE;
3561 * traverse the list of net devices and compute the minimum
3562 * buffer size based upon the filter mask.
3565 for_each_netdev_rcu(net, dev) {
3566 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3572 return nlmsg_total_size(min_ifinfo_dump_size);
3575 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3578 int s_idx = cb->family;
3579 int type = cb->nlh->nlmsg_type - RTM_BASE;
3585 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3586 struct rtnl_link **tab;
3587 struct rtnl_link *link;
3588 rtnl_dumpit_func dumpit;
3590 if (idx < s_idx || idx == PF_PACKET)
3593 if (type < 0 || type >= RTM_NR_MSGTYPES)
3596 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3604 dumpit = link->dumpit;
3609 memset(&cb->args[0], 0, sizeof(cb->args));
3613 ret = dumpit(skb, cb);
3619 return skb->len ? : ret;
3622 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3623 unsigned int change,
3624 u32 event, gfp_t flags, int *new_nsid,
3627 struct net *net = dev_net(dev);
3628 struct sk_buff *skb;
3630 size_t if_info_size;
3632 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3636 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3637 type, 0, 0, change, 0, 0, event,
3638 new_nsid, new_ifindex, -1, flags);
3640 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3641 WARN_ON(err == -EMSGSIZE);
3648 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3652 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3654 struct net *net = dev_net(dev);
3656 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3659 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3660 unsigned int change, u32 event,
3661 gfp_t flags, int *new_nsid, int new_ifindex)
3663 struct sk_buff *skb;
3665 if (dev->reg_state != NETREG_REGISTERED)
3668 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3671 rtmsg_ifinfo_send(skb, dev, flags);
3674 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3677 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3681 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3682 gfp_t flags, int *new_nsid, int new_ifindex)
3684 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3685 new_nsid, new_ifindex);
3688 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3689 struct net_device *dev,
3690 u8 *addr, u16 vid, u32 pid, u32 seq,
3691 int type, unsigned int flags,
3692 int nlflags, u16 ndm_state)
3694 struct nlmsghdr *nlh;
3697 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3701 ndm = nlmsg_data(nlh);
3702 ndm->ndm_family = AF_BRIDGE;
3705 ndm->ndm_flags = flags;
3707 ndm->ndm_ifindex = dev->ifindex;
3708 ndm->ndm_state = ndm_state;
3710 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3711 goto nla_put_failure;
3713 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3714 goto nla_put_failure;
3716 nlmsg_end(skb, nlh);
3720 nlmsg_cancel(skb, nlh);
3724 static inline size_t rtnl_fdb_nlmsg_size(void)
3726 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3727 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3728 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3732 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3735 struct net *net = dev_net(dev);
3736 struct sk_buff *skb;
3739 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3743 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3744 0, 0, type, NTF_SELF, 0, ndm_state);
3750 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3753 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3757 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3759 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3760 struct nlattr *tb[],
3761 struct net_device *dev,
3762 const unsigned char *addr, u16 vid,
3767 /* If aging addresses are supported device will need to
3768 * implement its own handler for this.
3770 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3771 pr_info("%s: FDB only supports static addresses\n", dev->name);
3776 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3780 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3781 err = dev_uc_add_excl(dev, addr);
3782 else if (is_multicast_ether_addr(addr))
3783 err = dev_mc_add_excl(dev, addr);
3785 /* Only return duplicate errors if NLM_F_EXCL is set */
3786 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3791 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3793 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3794 struct netlink_ext_ack *extack)
3799 if (nla_len(vlan_attr) != sizeof(u16)) {
3800 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3804 vid = nla_get_u16(vlan_attr);
3806 if (!vid || vid >= VLAN_VID_MASK) {
3807 NL_SET_ERR_MSG(extack, "invalid vlan id");
3815 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3816 struct netlink_ext_ack *extack)
3818 struct net *net = sock_net(skb->sk);
3820 struct nlattr *tb[NDA_MAX+1];
3821 struct net_device *dev;
3826 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3831 ndm = nlmsg_data(nlh);
3832 if (ndm->ndm_ifindex == 0) {
3833 NL_SET_ERR_MSG(extack, "invalid ifindex");
3837 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3839 NL_SET_ERR_MSG(extack, "unknown ifindex");
3843 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3844 NL_SET_ERR_MSG(extack, "invalid address");
3848 if (dev->type != ARPHRD_ETHER) {
3849 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3853 addr = nla_data(tb[NDA_LLADDR]);
3855 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3861 /* Support fdb on master device the net/bridge default case */
3862 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3863 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3864 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3865 const struct net_device_ops *ops = br_dev->netdev_ops;
3867 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3868 nlh->nlmsg_flags, extack);
3872 ndm->ndm_flags &= ~NTF_MASTER;
3875 /* Embedded bridge, macvlan, and any other device support */
3876 if ((ndm->ndm_flags & NTF_SELF)) {
3877 if (dev->netdev_ops->ndo_fdb_add)
3878 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3883 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3887 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3889 ndm->ndm_flags &= ~NTF_SELF;
3897 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3899 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3900 struct nlattr *tb[],
3901 struct net_device *dev,
3902 const unsigned char *addr, u16 vid)
3906 /* If aging addresses are supported device will need to
3907 * implement its own handler for this.
3909 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3910 pr_info("%s: FDB only supports static addresses\n", dev->name);
3914 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3915 err = dev_uc_del(dev, addr);
3916 else if (is_multicast_ether_addr(addr))
3917 err = dev_mc_del(dev, addr);
3921 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3923 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3924 struct netlink_ext_ack *extack)
3926 struct net *net = sock_net(skb->sk);
3928 struct nlattr *tb[NDA_MAX+1];
3929 struct net_device *dev;
3934 if (!netlink_capable(skb, CAP_NET_ADMIN))
3937 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3942 ndm = nlmsg_data(nlh);
3943 if (ndm->ndm_ifindex == 0) {
3944 NL_SET_ERR_MSG(extack, "invalid ifindex");
3948 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3950 NL_SET_ERR_MSG(extack, "unknown ifindex");
3954 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3955 NL_SET_ERR_MSG(extack, "invalid address");
3959 if (dev->type != ARPHRD_ETHER) {
3960 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3964 addr = nla_data(tb[NDA_LLADDR]);
3966 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3972 /* Support fdb on master device the net/bridge default case */
3973 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3974 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3975 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3976 const struct net_device_ops *ops = br_dev->netdev_ops;
3978 if (ops->ndo_fdb_del)
3979 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3984 ndm->ndm_flags &= ~NTF_MASTER;
3987 /* Embedded bridge, macvlan, and any other device support */
3988 if (ndm->ndm_flags & NTF_SELF) {
3989 if (dev->netdev_ops->ndo_fdb_del)
3990 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3993 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3996 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3998 ndm->ndm_flags &= ~NTF_SELF;
4005 static int nlmsg_populate_fdb(struct sk_buff *skb,
4006 struct netlink_callback *cb,
4007 struct net_device *dev,
4009 struct netdev_hw_addr_list *list)
4011 struct netdev_hw_addr *ha;
4015 portid = NETLINK_CB(cb->skb).portid;
4016 seq = cb->nlh->nlmsg_seq;
4018 list_for_each_entry(ha, &list->list, list) {
4019 if (*idx < cb->args[2])
4022 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4024 RTM_NEWNEIGH, NTF_SELF,
4025 NLM_F_MULTI, NUD_PERMANENT);
4035 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4036 * @skb: socket buffer to store message in
4037 * @cb: netlink callback
4039 * @filter_dev: ignored
4040 * @idx: the number of FDB table entries dumped is added to *@idx
4042 * Default netdevice operation to dump the existing unicast address list.
4043 * Returns number of addresses from list put in skb.
4045 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4046 struct netlink_callback *cb,
4047 struct net_device *dev,
4048 struct net_device *filter_dev,
4053 if (dev->type != ARPHRD_ETHER)
4056 netif_addr_lock_bh(dev);
4057 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4060 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4062 netif_addr_unlock_bh(dev);
4065 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4067 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4068 int *br_idx, int *brport_idx,
4069 struct netlink_ext_ack *extack)
4071 struct nlattr *tb[NDA_MAX + 1];
4075 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4076 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4080 ndm = nlmsg_data(nlh);
4081 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4082 ndm->ndm_flags || ndm->ndm_type) {
4083 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4087 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4088 NDA_MAX, NULL, extack);
4092 *brport_idx = ndm->ndm_ifindex;
4093 for (i = 0; i <= NDA_MAX; ++i) {
4099 if (nla_len(tb[i]) != sizeof(u32)) {
4100 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4103 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4106 if (nla_len(tb[i]) != sizeof(u32)) {
4107 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4110 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4113 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4121 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4122 int *br_idx, int *brport_idx,
4123 struct netlink_ext_ack *extack)
4125 struct nlattr *tb[IFLA_MAX+1];
4128 /* A hack to preserve kernel<->userspace interface.
4129 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4130 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4131 * So, check for ndmsg with an optional u32 attribute (not used here).
4132 * Fortunately these sizes don't conflict with the size of ifinfomsg
4133 * with an optional attribute.
4135 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4136 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4137 nla_attr_size(sizeof(u32)))) {
4138 struct ifinfomsg *ifm;
4140 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4141 tb, IFLA_MAX, ifla_policy,
4145 } else if (err == 0) {
4146 if (tb[IFLA_MASTER])
4147 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4150 ifm = nlmsg_data(nlh);
4151 *brport_idx = ifm->ifi_index;
4156 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4158 struct net_device *dev;
4159 struct net_device *br_dev = NULL;
4160 const struct net_device_ops *ops = NULL;
4161 const struct net_device_ops *cops = NULL;
4162 struct net *net = sock_net(skb->sk);
4163 struct hlist_head *head;
4171 if (cb->strict_check)
4172 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4175 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4181 br_dev = __dev_get_by_index(net, br_idx);
4185 ops = br_dev->netdev_ops;
4189 s_idx = cb->args[1];
4191 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4193 head = &net->dev_index_head[h];
4194 hlist_for_each_entry(dev, head, index_hlist) {
4196 if (brport_idx && (dev->ifindex != brport_idx))
4199 if (!br_idx) { /* user did not specify a specific bridge */
4200 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4201 br_dev = netdev_master_upper_dev_get(dev);
4202 cops = br_dev->netdev_ops;
4205 if (dev != br_dev &&
4206 !(dev->priv_flags & IFF_BRIDGE_PORT))
4209 if (br_dev != netdev_master_upper_dev_get(dev) &&
4210 !(dev->priv_flags & IFF_EBRIDGE))
4218 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4219 if (cops && cops->ndo_fdb_dump) {
4220 err = cops->ndo_fdb_dump(skb, cb,
4223 if (err == -EMSGSIZE)
4228 if (dev->netdev_ops->ndo_fdb_dump)
4229 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4233 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4235 if (err == -EMSGSIZE)
4240 /* reset fdb offset to 0 for rest of the interfaces */
4256 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4257 struct nlattr **tb, u8 *ndm_flags,
4258 int *br_idx, int *brport_idx, u8 **addr,
4259 u16 *vid, struct netlink_ext_ack *extack)
4264 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4265 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4269 ndm = nlmsg_data(nlh);
4270 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4272 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4276 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4277 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4281 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4282 NDA_MAX, nda_policy, extack);
4286 *ndm_flags = ndm->ndm_flags;
4287 *brport_idx = ndm->ndm_ifindex;
4288 for (i = 0; i <= NDA_MAX; ++i) {
4294 *br_idx = nla_get_u32(tb[i]);
4297 if (nla_len(tb[i]) != ETH_ALEN) {
4298 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4301 *addr = nla_data(tb[i]);
4304 err = fdb_vid_parse(tb[i], vid, extack);
4311 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4319 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4320 struct netlink_ext_ack *extack)
4322 struct net_device *dev = NULL, *br_dev = NULL;
4323 const struct net_device_ops *ops = NULL;
4324 struct net *net = sock_net(in_skb->sk);
4325 struct nlattr *tb[NDA_MAX + 1];
4326 struct sk_buff *skb;
4334 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4335 &brport_idx, &addr, &vid, extack);
4340 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4345 dev = __dev_get_by_index(net, brport_idx);
4347 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4354 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4358 br_dev = __dev_get_by_index(net, br_idx);
4360 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4363 ops = br_dev->netdev_ops;
4367 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4368 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4369 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4372 br_dev = netdev_master_upper_dev_get(dev);
4374 NL_SET_ERR_MSG(extack, "Master of device not found");
4377 ops = br_dev->netdev_ops;
4379 if (!(ndm_flags & NTF_SELF)) {
4380 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4383 ops = dev->netdev_ops;
4387 if (!br_dev && !dev) {
4388 NL_SET_ERR_MSG(extack, "No device specified");
4392 if (!ops || !ops->ndo_fdb_get) {
4393 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4397 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4403 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4404 NETLINK_CB(in_skb).portid,
4405 nlh->nlmsg_seq, extack);
4409 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4415 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4416 unsigned int attrnum, unsigned int flag)
4419 return nla_put_u8(skb, attrnum, !!(flags & flag));
4423 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4424 struct net_device *dev, u16 mode,
4425 u32 flags, u32 mask, int nlflags,
4427 int (*vlan_fill)(struct sk_buff *skb,
4428 struct net_device *dev,
4431 struct nlmsghdr *nlh;
4432 struct ifinfomsg *ifm;
4433 struct nlattr *br_afspec;
4434 struct nlattr *protinfo;
4435 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4436 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4439 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4443 ifm = nlmsg_data(nlh);
4444 ifm->ifi_family = AF_BRIDGE;
4446 ifm->ifi_type = dev->type;
4447 ifm->ifi_index = dev->ifindex;
4448 ifm->ifi_flags = dev_get_flags(dev);
4449 ifm->ifi_change = 0;
4452 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4453 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4454 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4456 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4458 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4459 (dev->ifindex != dev_get_iflink(dev) &&
4460 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4461 goto nla_put_failure;
4463 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4465 goto nla_put_failure;
4467 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4468 nla_nest_cancel(skb, br_afspec);
4469 goto nla_put_failure;
4472 if (mode != BRIDGE_MODE_UNDEF) {
4473 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4474 nla_nest_cancel(skb, br_afspec);
4475 goto nla_put_failure;
4479 err = vlan_fill(skb, dev, filter_mask);
4481 nla_nest_cancel(skb, br_afspec);
4482 goto nla_put_failure;
4485 nla_nest_end(skb, br_afspec);
4487 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4489 goto nla_put_failure;
4491 if (brport_nla_put_flag(skb, flags, mask,
4492 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4493 brport_nla_put_flag(skb, flags, mask,
4494 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4495 brport_nla_put_flag(skb, flags, mask,
4496 IFLA_BRPORT_FAST_LEAVE,
4497 BR_MULTICAST_FAST_LEAVE) ||
4498 brport_nla_put_flag(skb, flags, mask,
4499 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4500 brport_nla_put_flag(skb, flags, mask,
4501 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4502 brport_nla_put_flag(skb, flags, mask,
4503 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4504 brport_nla_put_flag(skb, flags, mask,
4505 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4506 brport_nla_put_flag(skb, flags, mask,
4507 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4508 nla_nest_cancel(skb, protinfo);
4509 goto nla_put_failure;
4512 nla_nest_end(skb, protinfo);
4514 nlmsg_end(skb, nlh);
4517 nlmsg_cancel(skb, nlh);
4518 return err ? err : -EMSGSIZE;
4520 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4522 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4523 bool strict_check, u32 *filter_mask,
4524 struct netlink_ext_ack *extack)
4526 struct nlattr *tb[IFLA_MAX+1];
4530 struct ifinfomsg *ifm;
4532 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4533 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4537 ifm = nlmsg_data(nlh);
4538 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4539 ifm->ifi_change || ifm->ifi_index) {
4540 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4544 err = nlmsg_parse_deprecated_strict(nlh,
4545 sizeof(struct ifinfomsg),
4546 tb, IFLA_MAX, ifla_policy,
4549 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4550 tb, IFLA_MAX, ifla_policy,
4556 /* new attributes should only be added with strict checking */
4557 for (i = 0; i <= IFLA_MAX; ++i) {
4563 *filter_mask = nla_get_u32(tb[i]);
4567 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4576 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4578 const struct nlmsghdr *nlh = cb->nlh;
4579 struct net *net = sock_net(skb->sk);
4580 struct net_device *dev;
4582 u32 portid = NETLINK_CB(cb->skb).portid;
4583 u32 seq = nlh->nlmsg_seq;
4584 u32 filter_mask = 0;
4587 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4589 if (err < 0 && cb->strict_check)
4593 for_each_netdev_rcu(net, dev) {
4594 const struct net_device_ops *ops = dev->netdev_ops;
4595 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4597 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4598 if (idx >= cb->args[0]) {
4599 err = br_dev->netdev_ops->ndo_bridge_getlink(
4600 skb, portid, seq, dev,
4601 filter_mask, NLM_F_MULTI);
4602 if (err < 0 && err != -EOPNOTSUPP) {
4603 if (likely(skb->len))
4612 if (ops->ndo_bridge_getlink) {
4613 if (idx >= cb->args[0]) {
4614 err = ops->ndo_bridge_getlink(skb, portid,
4618 if (err < 0 && err != -EOPNOTSUPP) {
4619 if (likely(skb->len))
4636 static inline size_t bridge_nlmsg_size(void)
4638 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4639 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4640 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4641 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4642 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4643 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4644 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4645 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4646 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4647 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4648 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4651 static int rtnl_bridge_notify(struct net_device *dev)
4653 struct net *net = dev_net(dev);
4654 struct sk_buff *skb;
4655 int err = -EOPNOTSUPP;
4657 if (!dev->netdev_ops->ndo_bridge_getlink)
4660 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4666 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4673 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4676 WARN_ON(err == -EMSGSIZE);
4679 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4683 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4684 struct netlink_ext_ack *extack)
4686 struct net *net = sock_net(skb->sk);
4687 struct ifinfomsg *ifm;
4688 struct net_device *dev;
4689 struct nlattr *br_spec, *attr = NULL;
4690 int rem, err = -EOPNOTSUPP;
4692 bool have_flags = false;
4694 if (nlmsg_len(nlh) < sizeof(*ifm))
4697 ifm = nlmsg_data(nlh);
4698 if (ifm->ifi_family != AF_BRIDGE)
4699 return -EPFNOSUPPORT;
4701 dev = __dev_get_by_index(net, ifm->ifi_index);
4703 NL_SET_ERR_MSG(extack, "unknown ifindex");
4707 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4709 nla_for_each_nested(attr, br_spec, rem) {
4710 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4711 if (nla_len(attr) < sizeof(flags))
4715 flags = nla_get_u16(attr);
4721 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4722 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4724 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4729 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4734 flags &= ~BRIDGE_FLAGS_MASTER;
4737 if ((flags & BRIDGE_FLAGS_SELF)) {
4738 if (!dev->netdev_ops->ndo_bridge_setlink)
4741 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4745 flags &= ~BRIDGE_FLAGS_SELF;
4747 /* Generate event to notify upper layer of bridge
4750 err = rtnl_bridge_notify(dev);
4755 memcpy(nla_data(attr), &flags, sizeof(flags));
4760 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4761 struct netlink_ext_ack *extack)
4763 struct net *net = sock_net(skb->sk);
4764 struct ifinfomsg *ifm;
4765 struct net_device *dev;
4766 struct nlattr *br_spec, *attr = NULL;
4767 int rem, err = -EOPNOTSUPP;
4769 bool have_flags = false;
4771 if (nlmsg_len(nlh) < sizeof(*ifm))
4774 ifm = nlmsg_data(nlh);
4775 if (ifm->ifi_family != AF_BRIDGE)
4776 return -EPFNOSUPPORT;
4778 dev = __dev_get_by_index(net, ifm->ifi_index);
4780 NL_SET_ERR_MSG(extack, "unknown ifindex");
4784 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4786 nla_for_each_nested(attr, br_spec, rem) {
4787 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4788 if (nla_len(attr) < sizeof(flags))
4792 flags = nla_get_u16(attr);
4798 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4799 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4801 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4806 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4810 flags &= ~BRIDGE_FLAGS_MASTER;
4813 if ((flags & BRIDGE_FLAGS_SELF)) {
4814 if (!dev->netdev_ops->ndo_bridge_dellink)
4817 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4821 flags &= ~BRIDGE_FLAGS_SELF;
4823 /* Generate event to notify upper layer of bridge
4826 err = rtnl_bridge_notify(dev);
4831 memcpy(nla_data(attr), &flags, sizeof(flags));
4836 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4838 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4839 (!idxattr || idxattr == attrid);
4842 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4843 static int rtnl_get_offload_stats_attr_size(int attr_id)
4846 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4847 return sizeof(struct rtnl_link_stats64);
4853 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4856 struct nlattr *attr = NULL;
4861 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4862 dev->netdev_ops->ndo_get_offload_stats))
4865 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4866 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4867 if (attr_id < *prividx)
4870 size = rtnl_get_offload_stats_attr_size(attr_id);
4874 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4877 attr = nla_reserve_64bit(skb, attr_id, size,
4878 IFLA_OFFLOAD_XSTATS_UNSPEC);
4880 goto nla_put_failure;
4882 attr_data = nla_data(attr);
4883 memset(attr_data, 0, size);
4884 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4887 goto get_offload_stats_failure;
4898 get_offload_stats_failure:
4903 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4909 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4910 dev->netdev_ops->ndo_get_offload_stats))
4913 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4914 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4915 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4917 size = rtnl_get_offload_stats_attr_size(attr_id);
4918 nla_size += nla_total_size_64bit(size);
4922 nla_size += nla_total_size(0);
4927 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4928 int type, u32 pid, u32 seq, u32 change,
4929 unsigned int flags, unsigned int filter_mask,
4930 int *idxattr, int *prividx)
4932 struct if_stats_msg *ifsm;
4933 struct nlmsghdr *nlh;
4934 struct nlattr *attr;
4935 int s_prividx = *prividx;
4940 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4944 ifsm = nlmsg_data(nlh);
4945 ifsm->family = PF_UNSPEC;
4948 ifsm->ifindex = dev->ifindex;
4949 ifsm->filter_mask = filter_mask;
4951 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4952 struct rtnl_link_stats64 *sp;
4954 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4955 sizeof(struct rtnl_link_stats64),
4958 goto nla_put_failure;
4960 sp = nla_data(attr);
4961 dev_get_stats(dev, sp);
4964 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4965 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4967 if (ops && ops->fill_linkxstats) {
4968 *idxattr = IFLA_STATS_LINK_XSTATS;
4969 attr = nla_nest_start_noflag(skb,
4970 IFLA_STATS_LINK_XSTATS);
4972 goto nla_put_failure;
4974 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4975 nla_nest_end(skb, attr);
4977 goto nla_put_failure;
4982 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4984 const struct rtnl_link_ops *ops = NULL;
4985 const struct net_device *master;
4987 master = netdev_master_upper_dev_get(dev);
4989 ops = master->rtnl_link_ops;
4990 if (ops && ops->fill_linkxstats) {
4991 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4992 attr = nla_nest_start_noflag(skb,
4993 IFLA_STATS_LINK_XSTATS_SLAVE);
4995 goto nla_put_failure;
4997 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4998 nla_nest_end(skb, attr);
5000 goto nla_put_failure;
5005 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5007 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5008 attr = nla_nest_start_noflag(skb,
5009 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5011 goto nla_put_failure;
5013 err = rtnl_get_offload_stats(skb, dev, prividx);
5014 if (err == -ENODATA)
5015 nla_nest_cancel(skb, attr);
5017 nla_nest_end(skb, attr);
5019 if (err && err != -ENODATA)
5020 goto nla_put_failure;
5024 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5025 struct rtnl_af_ops *af_ops;
5027 *idxattr = IFLA_STATS_AF_SPEC;
5028 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5030 goto nla_put_failure;
5033 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5034 if (af_ops->fill_stats_af) {
5038 af = nla_nest_start_noflag(skb,
5042 goto nla_put_failure;
5044 err = af_ops->fill_stats_af(skb, dev);
5046 if (err == -ENODATA) {
5047 nla_nest_cancel(skb, af);
5048 } else if (err < 0) {
5050 goto nla_put_failure;
5053 nla_nest_end(skb, af);
5058 nla_nest_end(skb, attr);
5063 nlmsg_end(skb, nlh);
5068 /* not a multi message or no progress mean a real error */
5069 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5070 nlmsg_cancel(skb, nlh);
5072 nlmsg_end(skb, nlh);
5077 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5082 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5083 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5085 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5086 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5087 int attr = IFLA_STATS_LINK_XSTATS;
5089 if (ops && ops->get_linkxstats_size) {
5090 size += nla_total_size(ops->get_linkxstats_size(dev,
5092 /* for IFLA_STATS_LINK_XSTATS */
5093 size += nla_total_size(0);
5097 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5098 struct net_device *_dev = (struct net_device *)dev;
5099 const struct rtnl_link_ops *ops = NULL;
5100 const struct net_device *master;
5102 /* netdev_master_upper_dev_get can't take const */
5103 master = netdev_master_upper_dev_get(_dev);
5105 ops = master->rtnl_link_ops;
5106 if (ops && ops->get_linkxstats_size) {
5107 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5109 size += nla_total_size(ops->get_linkxstats_size(dev,
5111 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5112 size += nla_total_size(0);
5116 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5117 size += rtnl_get_offload_stats_size(dev);
5119 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5120 struct rtnl_af_ops *af_ops;
5122 /* for IFLA_STATS_AF_SPEC */
5123 size += nla_total_size(0);
5126 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5127 if (af_ops->get_stats_af_size) {
5128 size += nla_total_size(
5129 af_ops->get_stats_af_size(dev));
5132 size += nla_total_size(0);
5141 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5142 bool is_dump, struct netlink_ext_ack *extack)
5144 struct if_stats_msg *ifsm;
5146 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5147 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5154 ifsm = nlmsg_data(nlh);
5156 /* only requests using strict checks can pass data to influence
5157 * the dump. The legacy exception is filter_mask.
5159 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5160 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5163 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5164 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5167 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5168 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5175 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5176 struct netlink_ext_ack *extack)
5178 struct net *net = sock_net(skb->sk);
5179 struct net_device *dev = NULL;
5180 int idxattr = 0, prividx = 0;
5181 struct if_stats_msg *ifsm;
5182 struct sk_buff *nskb;
5186 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5191 ifsm = nlmsg_data(nlh);
5192 if (ifsm->ifindex > 0)
5193 dev = __dev_get_by_index(net, ifsm->ifindex);
5200 filter_mask = ifsm->filter_mask;
5204 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5208 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5209 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5210 0, filter_mask, &idxattr, &prividx);
5212 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5213 WARN_ON(err == -EMSGSIZE);
5216 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5222 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5224 struct netlink_ext_ack *extack = cb->extack;
5225 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5226 struct net *net = sock_net(skb->sk);
5227 unsigned int flags = NLM_F_MULTI;
5228 struct if_stats_msg *ifsm;
5229 struct hlist_head *head;
5230 struct net_device *dev;
5231 u32 filter_mask = 0;
5235 s_idx = cb->args[1];
5236 s_idxattr = cb->args[2];
5237 s_prividx = cb->args[3];
5239 cb->seq = net->dev_base_seq;
5241 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5245 ifsm = nlmsg_data(cb->nlh);
5246 filter_mask = ifsm->filter_mask;
5248 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5252 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5254 head = &net->dev_index_head[h];
5255 hlist_for_each_entry(dev, head, index_hlist) {
5258 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5259 NETLINK_CB(cb->skb).portid,
5260 cb->nlh->nlmsg_seq, 0,
5262 &s_idxattr, &s_prividx);
5263 /* If we ran out of room on the first message,
5266 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5272 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5278 cb->args[3] = s_prividx;
5279 cb->args[2] = s_idxattr;
5286 /* Process one rtnetlink message. */
5288 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5289 struct netlink_ext_ack *extack)
5291 struct net *net = sock_net(skb->sk);
5292 struct rtnl_link *link;
5293 struct module *owner;
5294 int err = -EOPNOTSUPP;
5295 rtnl_doit_func doit;
5301 type = nlh->nlmsg_type;
5307 /* All the messages must have at least 1 byte length */
5308 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5311 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5314 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5318 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5320 rtnl_dumpit_func dumpit;
5321 u16 min_dump_alloc = 0;
5323 link = rtnl_get_link(family, type);
5324 if (!link || !link->dumpit) {
5326 link = rtnl_get_link(family, type);
5327 if (!link || !link->dumpit)
5330 owner = link->owner;
5331 dumpit = link->dumpit;
5333 if (type == RTM_GETLINK - RTM_BASE)
5334 min_dump_alloc = rtnl_calcit(skb, nlh);
5337 /* need to do this before rcu_read_unlock() */
5338 if (!try_module_get(owner))
5339 err = -EPROTONOSUPPORT;
5345 struct netlink_dump_control c = {
5347 .min_dump_alloc = min_dump_alloc,
5350 err = netlink_dump_start(rtnl, skb, nlh, &c);
5351 /* netlink_dump_start() will keep a reference on
5352 * module if dump is still in progress.
5359 link = rtnl_get_link(family, type);
5360 if (!link || !link->doit) {
5362 link = rtnl_get_link(PF_UNSPEC, type);
5363 if (!link || !link->doit)
5367 owner = link->owner;
5368 if (!try_module_get(owner)) {
5369 err = -EPROTONOSUPPORT;
5373 flags = link->flags;
5374 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5378 err = doit(skb, nlh, extack);
5385 link = rtnl_get_link(family, type);
5386 if (link && link->doit)
5387 err = link->doit(skb, nlh, extack);
5403 static void rtnetlink_rcv(struct sk_buff *skb)
5405 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5408 static int rtnetlink_bind(struct net *net, int group)
5411 case RTNLGRP_IPV4_MROUTE_R:
5412 case RTNLGRP_IPV6_MROUTE_R:
5413 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5420 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5422 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5426 case NETDEV_CHANGEMTU:
5427 case NETDEV_CHANGEADDR:
5428 case NETDEV_CHANGENAME:
5429 case NETDEV_FEAT_CHANGE:
5430 case NETDEV_BONDING_FAILOVER:
5431 case NETDEV_POST_TYPE_CHANGE:
5432 case NETDEV_NOTIFY_PEERS:
5433 case NETDEV_CHANGEUPPER:
5434 case NETDEV_RESEND_IGMP:
5435 case NETDEV_CHANGEINFODATA:
5436 case NETDEV_CHANGELOWERSTATE:
5437 case NETDEV_CHANGE_TX_QUEUE_LEN:
5438 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5439 GFP_KERNEL, NULL, 0);
5447 static struct notifier_block rtnetlink_dev_notifier = {
5448 .notifier_call = rtnetlink_event,
5452 static int __net_init rtnetlink_net_init(struct net *net)
5455 struct netlink_kernel_cfg cfg = {
5456 .groups = RTNLGRP_MAX,
5457 .input = rtnetlink_rcv,
5458 .cb_mutex = &rtnl_mutex,
5459 .flags = NL_CFG_F_NONROOT_RECV,
5460 .bind = rtnetlink_bind,
5463 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5470 static void __net_exit rtnetlink_net_exit(struct net *net)
5472 netlink_kernel_release(net->rtnl);
5476 static struct pernet_operations rtnetlink_net_ops = {
5477 .init = rtnetlink_net_init,
5478 .exit = rtnetlink_net_exit,
5481 void __init rtnetlink_init(void)
5483 if (register_pernet_subsys(&rtnetlink_net_ops))
5484 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5486 register_netdevice_notifier(&rtnetlink_dev_notifier);
5488 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5489 rtnl_dump_ifinfo, 0);
5490 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5491 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5492 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5494 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5495 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5496 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5498 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5499 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5501 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5502 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5503 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5505 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5506 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5507 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5509 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,