1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetic 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>
59 #define RTNL_MAX_TYPE 50
60 #define RTNL_SLAVE_MAX_TYPE 40
64 rtnl_dumpit_func dumpit;
70 static DEFINE_MUTEX(rtnl_mutex);
74 mutex_lock(&rtnl_mutex);
76 EXPORT_SYMBOL(rtnl_lock);
78 int rtnl_lock_killable(void)
80 return mutex_lock_killable(&rtnl_mutex);
82 EXPORT_SYMBOL(rtnl_lock_killable);
84 static struct sk_buff *defer_kfree_skb_list;
85 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
88 tail->next = defer_kfree_skb_list;
89 defer_kfree_skb_list = head;
92 EXPORT_SYMBOL(rtnl_kfree_skbs);
94 void __rtnl_unlock(void)
96 struct sk_buff *head = defer_kfree_skb_list;
98 defer_kfree_skb_list = NULL;
100 /* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
101 * is used. In some places, e.g. in cfg80211, we have code that will do
108 * and because netdev_run_todo() acquires the RTNL for items on the list
109 * we could cause a situation such as this:
112 * unregister_netdevice()
120 * // list not empty now
121 * // because of thread 2
123 * while (!list_empty(...))
128 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
129 * it's not used in cases where something is added to do the list.
131 WARN_ON(!list_empty(&net_todo_list));
133 mutex_unlock(&rtnl_mutex);
136 struct sk_buff *next = head->next;
144 void rtnl_unlock(void)
146 /* This fellow will unlock it for us. */
149 EXPORT_SYMBOL(rtnl_unlock);
151 int rtnl_trylock(void)
153 return mutex_trylock(&rtnl_mutex);
155 EXPORT_SYMBOL(rtnl_trylock);
157 int rtnl_is_locked(void)
159 return mutex_is_locked(&rtnl_mutex);
161 EXPORT_SYMBOL(rtnl_is_locked);
163 bool refcount_dec_and_rtnl_lock(refcount_t *r)
165 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
167 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
169 #ifdef CONFIG_PROVE_LOCKING
170 bool lockdep_rtnl_is_held(void)
172 return lockdep_is_held(&rtnl_mutex);
174 EXPORT_SYMBOL(lockdep_rtnl_is_held);
175 #endif /* #ifdef CONFIG_PROVE_LOCKING */
177 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
179 static inline int rtm_msgindex(int msgtype)
181 int msgindex = msgtype - RTM_BASE;
184 * msgindex < 0 implies someone tried to register a netlink
185 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
186 * the message type has not been added to linux/rtnetlink.h
188 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
193 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
195 struct rtnl_link __rcu **tab;
197 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
198 protocol = PF_UNSPEC;
200 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
202 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
204 return rcu_dereference_rtnl(tab[msgtype]);
207 static int rtnl_register_internal(struct module *owner,
208 int protocol, int msgtype,
209 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
212 struct rtnl_link *link, *old;
213 struct rtnl_link __rcu **tab;
217 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
218 msgindex = rtm_msgindex(msgtype);
221 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
223 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
227 /* ensures we see the 0 stores */
228 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
231 old = rtnl_dereference(tab[msgindex]);
233 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
237 link = kzalloc(sizeof(*link), GFP_KERNEL);
242 WARN_ON(link->owner && link->owner != owner);
245 WARN_ON(doit && link->doit && link->doit != doit);
248 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
250 link->dumpit = dumpit;
252 WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
253 (flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
254 link->flags |= flags;
256 /* publish protocol:msgtype */
257 rcu_assign_pointer(tab[msgindex], link);
267 * rtnl_register_module - Register a rtnetlink message type
269 * @owner: module registering the hook (THIS_MODULE)
270 * @protocol: Protocol family or PF_UNSPEC
271 * @msgtype: rtnetlink message type
272 * @doit: Function pointer called for each request message
273 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
274 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
276 * Like rtnl_register, but for use by removable modules.
278 int rtnl_register_module(struct module *owner,
279 int protocol, int msgtype,
280 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
283 return rtnl_register_internal(owner, protocol, msgtype,
284 doit, dumpit, flags);
286 EXPORT_SYMBOL_GPL(rtnl_register_module);
289 * rtnl_register - Register a rtnetlink message type
290 * @protocol: Protocol family or PF_UNSPEC
291 * @msgtype: rtnetlink message type
292 * @doit: Function pointer called for each request message
293 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
294 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
296 * Registers the specified function pointers (at least one of them has
297 * to be non-NULL) to be called whenever a request message for the
298 * specified protocol family and message type is received.
300 * The special protocol family PF_UNSPEC may be used to define fallback
301 * function pointers for the case when no entry for the specific protocol
304 void rtnl_register(int protocol, int msgtype,
305 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
310 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
313 pr_err("Unable to register rtnetlink message handler, "
314 "protocol = %d, message type = %d\n", protocol, msgtype);
318 * rtnl_unregister - Unregister a rtnetlink message type
319 * @protocol: Protocol family or PF_UNSPEC
320 * @msgtype: rtnetlink message type
322 * Returns 0 on success or a negative error code.
324 int rtnl_unregister(int protocol, int msgtype)
326 struct rtnl_link __rcu **tab;
327 struct rtnl_link *link;
330 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
331 msgindex = rtm_msgindex(msgtype);
334 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
340 link = rtnl_dereference(tab[msgindex]);
341 RCU_INIT_POINTER(tab[msgindex], NULL);
344 kfree_rcu(link, rcu);
348 EXPORT_SYMBOL_GPL(rtnl_unregister);
351 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
352 * @protocol : Protocol family or PF_UNSPEC
354 * Identical to calling rtnl_unregster() for all registered message types
355 * of a certain protocol family.
357 void rtnl_unregister_all(int protocol)
359 struct rtnl_link __rcu **tab;
360 struct rtnl_link *link;
363 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
366 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
371 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
372 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
373 link = rtnl_dereference(tab[msgindex]);
377 RCU_INIT_POINTER(tab[msgindex], NULL);
378 kfree_rcu(link, rcu);
386 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
388 static LIST_HEAD(link_ops);
390 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
392 const struct rtnl_link_ops *ops;
394 list_for_each_entry(ops, &link_ops, list) {
395 if (!strcmp(ops->kind, kind))
402 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
403 * @ops: struct rtnl_link_ops * to register
405 * The caller must hold the rtnl_mutex. This function should be used
406 * by drivers that create devices during module initialization. It
407 * must be called before registering the devices.
409 * Returns 0 on success or a negative error code.
411 int __rtnl_link_register(struct rtnl_link_ops *ops)
413 if (rtnl_link_ops_get(ops->kind))
416 /* The check for alloc/setup is here because if ops
417 * does not have that filled up, it is not possible
418 * to use the ops for creating device. So do not
419 * fill up dellink as well. That disables rtnl_dellink.
421 if ((ops->alloc || ops->setup) && !ops->dellink)
422 ops->dellink = unregister_netdevice_queue;
424 list_add_tail(&ops->list, &link_ops);
427 EXPORT_SYMBOL_GPL(__rtnl_link_register);
430 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
431 * @ops: struct rtnl_link_ops * to register
433 * Returns 0 on success or a negative error code.
435 int rtnl_link_register(struct rtnl_link_ops *ops)
439 /* Sanity-check max sizes to avoid stack buffer overflow. */
440 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
441 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
445 err = __rtnl_link_register(ops);
449 EXPORT_SYMBOL_GPL(rtnl_link_register);
451 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
453 struct net_device *dev;
454 LIST_HEAD(list_kill);
456 for_each_netdev(net, dev) {
457 if (dev->rtnl_link_ops == ops)
458 ops->dellink(dev, &list_kill);
460 unregister_netdevice_many(&list_kill);
464 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
465 * @ops: struct rtnl_link_ops * to unregister
467 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
468 * integrity (hold pernet_ops_rwsem for writing to close the race
469 * with setup_net() and cleanup_net()).
471 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
476 __rtnl_kill_links(net, ops);
478 list_del(&ops->list);
480 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
482 /* Return with the rtnl_lock held when there are no network
483 * devices unregistering in any network namespace.
485 static void rtnl_lock_unregistering_all(void)
489 DEFINE_WAIT_FUNC(wait, woken_wake_function);
491 add_wait_queue(&netdev_unregistering_wq, &wait);
493 unregistering = false;
495 /* We held write locked pernet_ops_rwsem, and parallel
496 * setup_net() and cleanup_net() are not possible.
499 if (atomic_read(&net->dev_unreg_count) > 0) {
500 unregistering = true;
508 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
510 remove_wait_queue(&netdev_unregistering_wq, &wait);
514 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
515 * @ops: struct rtnl_link_ops * to unregister
517 void rtnl_link_unregister(struct rtnl_link_ops *ops)
519 /* Close the race with setup_net() and cleanup_net() */
520 down_write(&pernet_ops_rwsem);
521 rtnl_lock_unregistering_all();
522 __rtnl_link_unregister(ops);
524 up_write(&pernet_ops_rwsem);
526 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
528 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
530 struct net_device *master_dev;
531 const struct rtnl_link_ops *ops;
536 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
540 ops = master_dev->rtnl_link_ops;
541 if (!ops || !ops->get_slave_size)
543 /* IFLA_INFO_SLAVE_DATA + nested data */
544 size = nla_total_size(sizeof(struct nlattr)) +
545 ops->get_slave_size(master_dev, dev);
552 static size_t rtnl_link_get_size(const struct net_device *dev)
554 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
560 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
561 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
564 /* IFLA_INFO_DATA + nested data */
565 size += nla_total_size(sizeof(struct nlattr)) +
568 if (ops->get_xstats_size)
569 /* IFLA_INFO_XSTATS */
570 size += nla_total_size(ops->get_xstats_size(dev));
572 size += rtnl_link_get_slave_info_data_size(dev);
577 static LIST_HEAD(rtnl_af_ops);
579 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
581 const struct rtnl_af_ops *ops;
585 list_for_each_entry(ops, &rtnl_af_ops, list) {
586 if (ops->family == family)
594 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
595 * @ops: struct rtnl_af_ops * to register
597 * Returns 0 on success or a negative error code.
599 void rtnl_af_register(struct rtnl_af_ops *ops)
602 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
605 EXPORT_SYMBOL_GPL(rtnl_af_register);
608 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
609 * @ops: struct rtnl_af_ops * to unregister
611 void rtnl_af_unregister(struct rtnl_af_ops *ops)
614 list_del_rcu(&ops->list);
619 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
621 static size_t rtnl_link_get_af_size(const struct net_device *dev,
624 struct rtnl_af_ops *af_ops;
628 size = nla_total_size(sizeof(struct nlattr));
631 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
632 if (af_ops->get_link_af_size) {
633 /* AF_* + nested data */
634 size += nla_total_size(sizeof(struct nlattr)) +
635 af_ops->get_link_af_size(dev, ext_filter_mask);
643 static bool rtnl_have_link_slave_info(const struct net_device *dev)
645 struct net_device *master_dev;
650 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
651 if (master_dev && master_dev->rtnl_link_ops)
657 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
658 const struct net_device *dev)
660 struct net_device *master_dev;
661 const struct rtnl_link_ops *ops;
662 struct nlattr *slave_data;
665 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
668 ops = master_dev->rtnl_link_ops;
671 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
673 if (ops->fill_slave_info) {
674 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
677 err = ops->fill_slave_info(skb, master_dev, dev);
679 goto err_cancel_slave_data;
680 nla_nest_end(skb, slave_data);
684 err_cancel_slave_data:
685 nla_nest_cancel(skb, slave_data);
689 static int rtnl_link_info_fill(struct sk_buff *skb,
690 const struct net_device *dev)
692 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
698 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
700 if (ops->fill_xstats) {
701 err = ops->fill_xstats(skb, dev);
705 if (ops->fill_info) {
706 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
709 err = ops->fill_info(skb, dev);
711 goto err_cancel_data;
712 nla_nest_end(skb, data);
717 nla_nest_cancel(skb, data);
721 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
723 struct nlattr *linkinfo;
726 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
727 if (linkinfo == NULL)
730 err = rtnl_link_info_fill(skb, dev);
732 goto err_cancel_link;
734 err = rtnl_link_slave_info_fill(skb, dev);
736 goto err_cancel_link;
738 nla_nest_end(skb, linkinfo);
742 nla_nest_cancel(skb, linkinfo);
747 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
749 struct sock *rtnl = net->rtnl;
751 return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
754 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
756 struct sock *rtnl = net->rtnl;
758 return nlmsg_unicast(rtnl, skb, pid);
760 EXPORT_SYMBOL(rtnl_unicast);
762 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
763 struct nlmsghdr *nlh, gfp_t flags)
765 struct sock *rtnl = net->rtnl;
767 nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
769 EXPORT_SYMBOL(rtnl_notify);
771 void rtnl_set_sk_err(struct net *net, u32 group, int error)
773 struct sock *rtnl = net->rtnl;
775 netlink_set_err(rtnl, 0, group, error);
777 EXPORT_SYMBOL(rtnl_set_sk_err);
779 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
784 /* nothing is dumped for dst_default_metrics, so just skip the loop */
785 if (metrics == dst_default_metrics.metrics)
788 mx = nla_nest_start_noflag(skb, RTA_METRICS);
792 for (i = 0; i < RTAX_MAX; i++) {
794 if (i == RTAX_CC_ALGO - 1) {
795 char tmp[TCP_CA_NAME_MAX], *name;
797 name = tcp_ca_get_name_by_key(metrics[i], tmp);
800 if (nla_put_string(skb, i + 1, name))
801 goto nla_put_failure;
802 } else if (i == RTAX_FEATURES - 1) {
803 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
807 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
808 if (nla_put_u32(skb, i + 1, user_features))
809 goto nla_put_failure;
811 if (nla_put_u32(skb, i + 1, metrics[i]))
812 goto nla_put_failure;
819 nla_nest_cancel(skb, mx);
823 return nla_nest_end(skb, mx);
826 nla_nest_cancel(skb, mx);
829 EXPORT_SYMBOL(rtnetlink_put_metrics);
831 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
832 long expires, u32 error)
834 struct rta_cacheinfo ci = {
840 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
841 ci.rta_used = dst->__use;
842 ci.rta_clntref = atomic_read(&dst->__refcnt);
847 clock = jiffies_to_clock_t(abs(expires));
848 clock = min_t(unsigned long, clock, INT_MAX);
849 ci.rta_expires = (expires > 0) ? clock : -clock;
851 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
853 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
855 static void set_operstate(struct net_device *dev, unsigned char transition)
857 unsigned char operstate = dev->operstate;
859 switch (transition) {
861 if ((operstate == IF_OPER_DORMANT ||
862 operstate == IF_OPER_TESTING ||
863 operstate == IF_OPER_UNKNOWN) &&
864 !netif_dormant(dev) && !netif_testing(dev))
865 operstate = IF_OPER_UP;
868 case IF_OPER_TESTING:
869 if (netif_oper_up(dev))
870 operstate = IF_OPER_TESTING;
873 case IF_OPER_DORMANT:
874 if (netif_oper_up(dev))
875 operstate = IF_OPER_DORMANT;
879 if (dev->operstate != operstate) {
880 write_lock(&dev_base_lock);
881 dev->operstate = operstate;
882 write_unlock(&dev_base_lock);
883 netdev_state_change(dev);
887 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
889 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
890 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
893 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
894 const struct ifinfomsg *ifm)
896 unsigned int flags = ifm->ifi_flags;
898 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
900 flags = (flags & ifm->ifi_change) |
901 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
906 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
907 const struct rtnl_link_stats64 *b)
909 a->rx_packets = b->rx_packets;
910 a->tx_packets = b->tx_packets;
911 a->rx_bytes = b->rx_bytes;
912 a->tx_bytes = b->tx_bytes;
913 a->rx_errors = b->rx_errors;
914 a->tx_errors = b->tx_errors;
915 a->rx_dropped = b->rx_dropped;
916 a->tx_dropped = b->tx_dropped;
918 a->multicast = b->multicast;
919 a->collisions = b->collisions;
921 a->rx_length_errors = b->rx_length_errors;
922 a->rx_over_errors = b->rx_over_errors;
923 a->rx_crc_errors = b->rx_crc_errors;
924 a->rx_frame_errors = b->rx_frame_errors;
925 a->rx_fifo_errors = b->rx_fifo_errors;
926 a->rx_missed_errors = b->rx_missed_errors;
928 a->tx_aborted_errors = b->tx_aborted_errors;
929 a->tx_carrier_errors = b->tx_carrier_errors;
930 a->tx_fifo_errors = b->tx_fifo_errors;
931 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
932 a->tx_window_errors = b->tx_window_errors;
934 a->rx_compressed = b->rx_compressed;
935 a->tx_compressed = b->tx_compressed;
937 a->rx_nohandler = b->rx_nohandler;
941 static inline int rtnl_vfinfo_size(const struct net_device *dev,
944 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
945 int num_vfs = dev_num_vf(dev->dev.parent);
946 size_t size = nla_total_size(0);
949 nla_total_size(sizeof(struct ifla_vf_mac)) +
950 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
951 nla_total_size(sizeof(struct ifla_vf_vlan)) +
952 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
953 nla_total_size(MAX_VLAN_LIST_LEN *
954 sizeof(struct ifla_vf_vlan_info)) +
955 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
956 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
957 nla_total_size(sizeof(struct ifla_vf_rate)) +
958 nla_total_size(sizeof(struct ifla_vf_link_state)) +
959 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
960 nla_total_size(0) + /* nest IFLA_VF_STATS */
961 /* IFLA_VF_STATS_RX_PACKETS */
962 nla_total_size_64bit(sizeof(__u64)) +
963 /* IFLA_VF_STATS_TX_PACKETS */
964 nla_total_size_64bit(sizeof(__u64)) +
965 /* IFLA_VF_STATS_RX_BYTES */
966 nla_total_size_64bit(sizeof(__u64)) +
967 /* IFLA_VF_STATS_TX_BYTES */
968 nla_total_size_64bit(sizeof(__u64)) +
969 /* IFLA_VF_STATS_BROADCAST */
970 nla_total_size_64bit(sizeof(__u64)) +
971 /* IFLA_VF_STATS_MULTICAST */
972 nla_total_size_64bit(sizeof(__u64)) +
973 /* IFLA_VF_STATS_RX_DROPPED */
974 nla_total_size_64bit(sizeof(__u64)) +
975 /* IFLA_VF_STATS_TX_DROPPED */
976 nla_total_size_64bit(sizeof(__u64)) +
977 nla_total_size(sizeof(struct ifla_vf_trust)));
983 static size_t rtnl_port_size(const struct net_device *dev,
986 size_t port_size = nla_total_size(4) /* PORT_VF */
987 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
988 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
989 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
990 + nla_total_size(1) /* PROT_VDP_REQUEST */
991 + nla_total_size(2); /* PORT_VDP_RESPONSE */
992 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
993 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
995 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
998 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
999 !(ext_filter_mask & RTEXT_FILTER_VF))
1001 if (dev_num_vf(dev->dev.parent))
1002 return port_self_size + vf_ports_size +
1003 vf_port_size * dev_num_vf(dev->dev.parent);
1005 return port_self_size;
1008 static size_t rtnl_xdp_size(void)
1010 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
1011 nla_total_size(1) + /* XDP_ATTACHED */
1012 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
1013 nla_total_size(4); /* XDP_<mode>_PROG_ID */
1018 static size_t rtnl_prop_list_size(const struct net_device *dev)
1020 struct netdev_name_node *name_node;
1023 if (list_empty(&dev->name_node->list))
1025 size = nla_total_size(0);
1026 list_for_each_entry(name_node, &dev->name_node->list, list)
1027 size += nla_total_size(ALTIFNAMSIZ);
1031 static size_t rtnl_proto_down_size(const struct net_device *dev)
1033 size_t size = nla_total_size(1);
1035 if (dev->proto_down_reason)
1036 size += nla_total_size(0) + nla_total_size(4);
1041 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1042 u32 ext_filter_mask)
1044 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1045 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1046 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1047 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1048 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1049 + nla_total_size(sizeof(struct rtnl_link_stats))
1050 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1051 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1052 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1053 + nla_total_size(4) /* IFLA_TXQLEN */
1054 + nla_total_size(4) /* IFLA_WEIGHT */
1055 + nla_total_size(4) /* IFLA_MTU */
1056 + nla_total_size(4) /* IFLA_LINK */
1057 + nla_total_size(4) /* IFLA_MASTER */
1058 + nla_total_size(1) /* IFLA_CARRIER */
1059 + nla_total_size(4) /* IFLA_PROMISCUITY */
1060 + nla_total_size(4) /* IFLA_ALLMULTI */
1061 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1062 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1063 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1064 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1065 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1066 + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
1067 + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1068 + nla_total_size(1) /* IFLA_OPERSTATE */
1069 + nla_total_size(1) /* IFLA_LINKMODE */
1070 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1071 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1072 + nla_total_size(4) /* IFLA_GROUP */
1073 + nla_total_size(ext_filter_mask
1074 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1075 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1076 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1077 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1078 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1079 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1080 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1081 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1082 + rtnl_xdp_size() /* IFLA_XDP */
1083 + nla_total_size(4) /* IFLA_EVENT */
1084 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1085 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1086 + rtnl_proto_down_size(dev) /* proto down */
1087 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1088 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1089 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1090 + nla_total_size(4) /* IFLA_MIN_MTU */
1091 + nla_total_size(4) /* IFLA_MAX_MTU */
1092 + rtnl_prop_list_size(dev)
1093 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1097 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1099 struct nlattr *vf_ports;
1100 struct nlattr *vf_port;
1104 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1108 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1109 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1111 goto nla_put_failure;
1112 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1113 goto nla_put_failure;
1114 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1115 if (err == -EMSGSIZE)
1116 goto nla_put_failure;
1118 nla_nest_cancel(skb, vf_port);
1121 nla_nest_end(skb, vf_port);
1124 nla_nest_end(skb, vf_ports);
1129 nla_nest_cancel(skb, vf_ports);
1133 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1135 struct nlattr *port_self;
1138 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1142 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1144 nla_nest_cancel(skb, port_self);
1145 return (err == -EMSGSIZE) ? err : 0;
1148 nla_nest_end(skb, port_self);
1153 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1154 u32 ext_filter_mask)
1158 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1159 !(ext_filter_mask & RTEXT_FILTER_VF))
1162 err = rtnl_port_self_fill(skb, dev);
1166 if (dev_num_vf(dev->dev.parent)) {
1167 err = rtnl_vf_ports_fill(skb, dev);
1175 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1178 struct netdev_phys_item_id ppid;
1180 err = dev_get_phys_port_id(dev, &ppid);
1182 if (err == -EOPNOTSUPP)
1187 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1193 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1195 char name[IFNAMSIZ];
1198 err = dev_get_phys_port_name(dev, name, sizeof(name));
1200 if (err == -EOPNOTSUPP)
1205 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1211 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1213 struct netdev_phys_item_id ppid = { };
1216 err = dev_get_port_parent_id(dev, &ppid, false);
1218 if (err == -EOPNOTSUPP)
1223 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1229 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1230 struct net_device *dev)
1232 struct rtnl_link_stats64 *sp;
1233 struct nlattr *attr;
1235 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1236 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1240 sp = nla_data(attr);
1241 dev_get_stats(dev, sp);
1243 attr = nla_reserve(skb, IFLA_STATS,
1244 sizeof(struct rtnl_link_stats));
1248 copy_rtnl_link_stats(nla_data(attr), sp);
1253 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1254 struct net_device *dev,
1256 struct nlattr *vfinfo)
1258 struct ifla_vf_rss_query_en vf_rss_query_en;
1259 struct nlattr *vf, *vfstats, *vfvlanlist;
1260 struct ifla_vf_link_state vf_linkstate;
1261 struct ifla_vf_vlan_info vf_vlan_info;
1262 struct ifla_vf_spoofchk vf_spoofchk;
1263 struct ifla_vf_tx_rate vf_tx_rate;
1264 struct ifla_vf_stats vf_stats;
1265 struct ifla_vf_trust vf_trust;
1266 struct ifla_vf_vlan vf_vlan;
1267 struct ifla_vf_rate vf_rate;
1268 struct ifla_vf_mac vf_mac;
1269 struct ifla_vf_broadcast vf_broadcast;
1270 struct ifla_vf_info ivi;
1271 struct ifla_vf_guid node_guid;
1272 struct ifla_vf_guid port_guid;
1274 memset(&ivi, 0, sizeof(ivi));
1276 /* Not all SR-IOV capable drivers support the
1277 * spoofcheck and "RSS query enable" query. Preset to
1278 * -1 so the user space tool can detect that the driver
1279 * didn't report anything.
1282 ivi.rss_query_en = -1;
1284 /* The default value for VF link state is "auto"
1285 * IFLA_VF_LINK_STATE_AUTO which equals zero
1288 /* VLAN Protocol by default is 802.1Q */
1289 ivi.vlan_proto = htons(ETH_P_8021Q);
1290 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1293 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1294 memset(&node_guid, 0, sizeof(node_guid));
1295 memset(&port_guid, 0, sizeof(port_guid));
1304 vf_rss_query_en.vf =
1307 port_guid.vf = ivi.vf;
1309 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1310 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1311 vf_vlan.vlan = ivi.vlan;
1312 vf_vlan.qos = ivi.qos;
1313 vf_vlan_info.vlan = ivi.vlan;
1314 vf_vlan_info.qos = ivi.qos;
1315 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1316 vf_tx_rate.rate = ivi.max_tx_rate;
1317 vf_rate.min_tx_rate = ivi.min_tx_rate;
1318 vf_rate.max_tx_rate = ivi.max_tx_rate;
1319 vf_spoofchk.setting = ivi.spoofchk;
1320 vf_linkstate.link_state = ivi.linkstate;
1321 vf_rss_query_en.setting = ivi.rss_query_en;
1322 vf_trust.setting = ivi.trusted;
1323 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1325 goto nla_put_vfinfo_failure;
1326 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1327 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1328 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1329 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1331 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1333 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1335 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1337 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1338 sizeof(vf_rss_query_en),
1339 &vf_rss_query_en) ||
1340 nla_put(skb, IFLA_VF_TRUST,
1341 sizeof(vf_trust), &vf_trust))
1342 goto nla_put_vf_failure;
1344 if (dev->netdev_ops->ndo_get_vf_guid &&
1345 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1347 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1349 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1351 goto nla_put_vf_failure;
1353 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1355 goto nla_put_vf_failure;
1356 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1358 nla_nest_cancel(skb, vfvlanlist);
1359 goto nla_put_vf_failure;
1361 nla_nest_end(skb, vfvlanlist);
1362 memset(&vf_stats, 0, sizeof(vf_stats));
1363 if (dev->netdev_ops->ndo_get_vf_stats)
1364 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1366 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1368 goto nla_put_vf_failure;
1369 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1370 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1371 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1372 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1373 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1374 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1375 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1376 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1377 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1378 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1379 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1380 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1381 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1382 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1383 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1384 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1385 nla_nest_cancel(skb, vfstats);
1386 goto nla_put_vf_failure;
1388 nla_nest_end(skb, vfstats);
1389 nla_nest_end(skb, vf);
1393 nla_nest_cancel(skb, vf);
1394 nla_put_vfinfo_failure:
1395 nla_nest_cancel(skb, vfinfo);
1399 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1400 struct net_device *dev,
1401 u32 ext_filter_mask)
1403 struct nlattr *vfinfo;
1406 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1409 num_vfs = dev_num_vf(dev->dev.parent);
1410 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1413 if (!dev->netdev_ops->ndo_get_vf_config)
1416 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1420 for (i = 0; i < num_vfs; i++) {
1421 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1425 nla_nest_end(skb, vfinfo);
1429 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1431 struct rtnl_link_ifmap map;
1433 memset(&map, 0, sizeof(map));
1434 map.mem_start = dev->mem_start;
1435 map.mem_end = dev->mem_end;
1436 map.base_addr = dev->base_addr;
1439 map.port = dev->if_port;
1441 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1447 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1449 const struct bpf_prog *generic_xdp_prog;
1453 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1454 if (!generic_xdp_prog)
1456 return generic_xdp_prog->aux->id;
1459 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1461 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1464 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1466 return dev_xdp_prog_id(dev, XDP_MODE_HW);
1469 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1470 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1471 u32 (*get_prog_id)(struct net_device *dev))
1476 curr_id = get_prog_id(dev);
1481 err = nla_put_u32(skb, attr, curr_id);
1485 if (*mode != XDP_ATTACHED_NONE)
1486 *mode = XDP_ATTACHED_MULTI;
1493 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1500 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1505 mode = XDP_ATTACHED_NONE;
1506 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1507 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1510 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1511 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1514 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1515 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1519 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1523 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1524 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1529 nla_nest_end(skb, xdp);
1533 nla_nest_cancel(skb, xdp);
1537 static u32 rtnl_get_event(unsigned long event)
1539 u32 rtnl_event_type = IFLA_EVENT_NONE;
1543 rtnl_event_type = IFLA_EVENT_REBOOT;
1545 case NETDEV_FEAT_CHANGE:
1546 rtnl_event_type = IFLA_EVENT_FEATURES;
1548 case NETDEV_BONDING_FAILOVER:
1549 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1551 case NETDEV_NOTIFY_PEERS:
1552 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1554 case NETDEV_RESEND_IGMP:
1555 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1557 case NETDEV_CHANGEINFODATA:
1558 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1564 return rtnl_event_type;
1567 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1569 const struct net_device *upper_dev;
1574 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1576 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1582 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1585 int ifindex = dev_get_iflink(dev);
1587 if (force || dev->ifindex != ifindex)
1588 return nla_put_u32(skb, IFLA_LINK, ifindex);
1593 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1594 struct net_device *dev)
1599 ret = dev_get_alias(dev, buf, sizeof(buf));
1600 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1603 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1604 const struct net_device *dev,
1605 struct net *src_net, gfp_t gfp)
1607 bool put_iflink = false;
1609 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1610 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1612 if (!net_eq(dev_net(dev), link_net)) {
1613 int id = peernet2id_alloc(src_net, link_net, gfp);
1615 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1622 return nla_put_iflink(skb, dev, put_iflink);
1625 static int rtnl_fill_link_af(struct sk_buff *skb,
1626 const struct net_device *dev,
1627 u32 ext_filter_mask)
1629 const struct rtnl_af_ops *af_ops;
1630 struct nlattr *af_spec;
1632 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1636 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1640 if (!af_ops->fill_link_af)
1643 af = nla_nest_start_noflag(skb, af_ops->family);
1647 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1649 * Caller may return ENODATA to indicate that there
1650 * was no data to be dumped. This is not an error, it
1651 * means we should trim the attribute header and
1654 if (err == -ENODATA)
1655 nla_nest_cancel(skb, af);
1659 nla_nest_end(skb, af);
1662 nla_nest_end(skb, af_spec);
1666 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1667 const struct net_device *dev)
1669 struct netdev_name_node *name_node;
1672 list_for_each_entry(name_node, &dev->name_node->list, list) {
1673 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1680 static int rtnl_fill_prop_list(struct sk_buff *skb,
1681 const struct net_device *dev)
1683 struct nlattr *prop_list;
1686 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1690 ret = rtnl_fill_alt_ifnames(skb, dev);
1694 nla_nest_end(skb, prop_list);
1698 nla_nest_cancel(skb, prop_list);
1702 static int rtnl_fill_proto_down(struct sk_buff *skb,
1703 const struct net_device *dev)
1708 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1709 goto nla_put_failure;
1711 preason = dev->proto_down_reason;
1715 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1719 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1720 nla_nest_cancel(skb, pr);
1721 goto nla_put_failure;
1724 nla_nest_end(skb, pr);
1731 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1732 struct net_device *dev, struct net *src_net,
1733 int type, u32 pid, u32 seq, u32 change,
1734 unsigned int flags, u32 ext_filter_mask,
1735 u32 event, int *new_nsid, int new_ifindex,
1736 int tgt_netnsid, gfp_t gfp)
1738 struct ifinfomsg *ifm;
1739 struct nlmsghdr *nlh;
1740 struct Qdisc *qdisc;
1743 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1747 ifm = nlmsg_data(nlh);
1748 ifm->ifi_family = AF_UNSPEC;
1750 ifm->ifi_type = dev->type;
1751 ifm->ifi_index = dev->ifindex;
1752 ifm->ifi_flags = dev_get_flags(dev);
1753 ifm->ifi_change = change;
1755 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1756 goto nla_put_failure;
1758 qdisc = rtnl_dereference(dev->qdisc);
1759 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1760 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1761 nla_put_u8(skb, IFLA_OPERSTATE,
1762 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1763 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1764 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1765 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1766 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1767 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1768 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1769 nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
1770 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1771 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1772 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1773 nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
1774 nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
1775 nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
1777 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1779 put_master_ifindex(skb, dev) ||
1780 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1782 nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1783 nla_put_ifalias(skb, dev) ||
1784 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1785 atomic_read(&dev->carrier_up_count) +
1786 atomic_read(&dev->carrier_down_count)) ||
1787 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1788 atomic_read(&dev->carrier_up_count)) ||
1789 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1790 atomic_read(&dev->carrier_down_count)))
1791 goto nla_put_failure;
1793 if (rtnl_fill_proto_down(skb, dev))
1794 goto nla_put_failure;
1796 if (event != IFLA_EVENT_NONE) {
1797 if (nla_put_u32(skb, IFLA_EVENT, event))
1798 goto nla_put_failure;
1801 if (rtnl_fill_link_ifmap(skb, dev))
1802 goto nla_put_failure;
1804 if (dev->addr_len) {
1805 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1806 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1807 goto nla_put_failure;
1810 if (rtnl_phys_port_id_fill(skb, dev))
1811 goto nla_put_failure;
1813 if (rtnl_phys_port_name_fill(skb, dev))
1814 goto nla_put_failure;
1816 if (rtnl_phys_switch_id_fill(skb, dev))
1817 goto nla_put_failure;
1819 if (rtnl_fill_stats(skb, dev))
1820 goto nla_put_failure;
1822 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1823 goto nla_put_failure;
1825 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1826 goto nla_put_failure;
1828 if (rtnl_xdp_fill(skb, dev))
1829 goto nla_put_failure;
1831 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1832 if (rtnl_link_fill(skb, dev) < 0)
1833 goto nla_put_failure;
1836 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1837 goto nla_put_failure;
1840 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1841 goto nla_put_failure;
1843 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1844 goto nla_put_failure;
1846 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1847 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1848 goto nla_put_failure;
1851 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1852 goto nla_put_failure_rcu;
1855 if (rtnl_fill_prop_list(skb, dev))
1856 goto nla_put_failure;
1858 if (dev->dev.parent &&
1859 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1860 dev_name(dev->dev.parent)))
1861 goto nla_put_failure;
1863 if (dev->dev.parent && dev->dev.parent->bus &&
1864 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1865 dev->dev.parent->bus->name))
1866 goto nla_put_failure;
1868 nlmsg_end(skb, nlh);
1871 nla_put_failure_rcu:
1874 nlmsg_cancel(skb, nlh);
1878 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1879 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1880 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1881 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1882 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1883 [IFLA_MTU] = { .type = NLA_U32 },
1884 [IFLA_LINK] = { .type = NLA_U32 },
1885 [IFLA_MASTER] = { .type = NLA_U32 },
1886 [IFLA_CARRIER] = { .type = NLA_U8 },
1887 [IFLA_TXQLEN] = { .type = NLA_U32 },
1888 [IFLA_WEIGHT] = { .type = NLA_U32 },
1889 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1890 [IFLA_LINKMODE] = { .type = NLA_U8 },
1891 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1892 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1893 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1894 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1895 * allow 0-length string (needed to remove an alias).
1897 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1898 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1899 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1900 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1901 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1902 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1903 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1904 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1905 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1906 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1907 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1908 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1909 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1910 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1911 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1912 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1913 [IFLA_XDP] = { .type = NLA_NESTED },
1914 [IFLA_EVENT] = { .type = NLA_U32 },
1915 [IFLA_GROUP] = { .type = NLA_U32 },
1916 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1917 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1918 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1919 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1920 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1921 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1922 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1923 .len = ALTIFNAMSIZ - 1 },
1924 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1925 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1926 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
1927 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
1928 [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 },
1929 [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT },
1930 [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT },
1931 [IFLA_ALLMULTI] = { .type = NLA_REJECT },
1934 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1935 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1936 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1937 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1938 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1941 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1942 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1943 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1944 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1945 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1946 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1947 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1948 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1949 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1950 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1951 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1952 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1953 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1954 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1957 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1958 [IFLA_PORT_VF] = { .type = NLA_U32 },
1959 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1960 .len = PORT_PROFILE_MAX },
1961 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1962 .len = PORT_UUID_MAX },
1963 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1964 .len = PORT_UUID_MAX },
1965 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1966 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1968 /* Unused, but we need to keep it here since user space could
1969 * fill it. It's also broken with regard to NLA_BINARY use in
1970 * combination with structs.
1972 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1973 .len = sizeof(struct ifla_port_vsi) },
1976 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1977 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1978 [IFLA_XDP_FD] = { .type = NLA_S32 },
1979 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
1980 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1981 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1982 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1985 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1987 const struct rtnl_link_ops *ops = NULL;
1988 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1990 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1993 if (linfo[IFLA_INFO_KIND]) {
1994 char kind[MODULE_NAME_LEN];
1996 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1997 ops = rtnl_link_ops_get(kind);
2003 static bool link_master_filtered(struct net_device *dev, int master_idx)
2005 struct net_device *master;
2010 master = netdev_master_upper_dev_get(dev);
2012 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2013 * another invalid value for ifindex to denote "no master".
2015 if (master_idx == -1)
2018 if (!master || master->ifindex != master_idx)
2024 static bool link_kind_filtered(const struct net_device *dev,
2025 const struct rtnl_link_ops *kind_ops)
2027 if (kind_ops && dev->rtnl_link_ops != kind_ops)
2033 static bool link_dump_filtered(struct net_device *dev,
2035 const struct rtnl_link_ops *kind_ops)
2037 if (link_master_filtered(dev, master_idx) ||
2038 link_kind_filtered(dev, kind_ops))
2045 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2046 * @sk: netlink socket
2047 * @netnsid: network namespace identifier
2049 * Returns the network namespace identified by netnsid on success or an error
2050 * pointer on failure.
2052 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2056 net = get_net_ns_by_id(sock_net(sk), netnsid);
2058 return ERR_PTR(-EINVAL);
2060 /* For now, the caller is required to have CAP_NET_ADMIN in
2061 * the user namespace owning the target net ns.
2063 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2065 return ERR_PTR(-EACCES);
2069 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2071 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2072 bool strict_check, struct nlattr **tb,
2073 struct netlink_ext_ack *extack)
2078 struct ifinfomsg *ifm;
2080 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2081 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2085 ifm = nlmsg_data(nlh);
2086 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2088 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2091 if (ifm->ifi_index) {
2092 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2096 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2097 IFLA_MAX, ifla_policy,
2101 /* A hack to preserve kernel<->userspace interface.
2102 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2103 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2104 * what iproute2 < v3.9.0 used.
2105 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2106 * attribute, its netlink message is shorter than struct ifinfomsg.
2108 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2109 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2111 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2115 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2117 struct netlink_ext_ack *extack = cb->extack;
2118 const struct nlmsghdr *nlh = cb->nlh;
2119 struct net *net = sock_net(skb->sk);
2120 struct net *tgt_net = net;
2123 struct net_device *dev;
2124 struct hlist_head *head;
2125 struct nlattr *tb[IFLA_MAX+1];
2126 u32 ext_filter_mask = 0;
2127 const struct rtnl_link_ops *kind_ops = NULL;
2128 unsigned int flags = NLM_F_MULTI;
2134 s_idx = cb->args[1];
2136 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2138 if (cb->strict_check)
2144 for (i = 0; i <= IFLA_MAX; ++i) {
2148 /* new attributes should only be added with strict checking */
2150 case IFLA_TARGET_NETNSID:
2151 netnsid = nla_get_s32(tb[i]);
2152 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2153 if (IS_ERR(tgt_net)) {
2154 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2155 return PTR_ERR(tgt_net);
2159 ext_filter_mask = nla_get_u32(tb[i]);
2162 master_idx = nla_get_u32(tb[i]);
2165 kind_ops = linkinfo_to_kind_ops(tb[i]);
2168 if (cb->strict_check) {
2169 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2175 if (master_idx || kind_ops)
2176 flags |= NLM_F_DUMP_FILTERED;
2179 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2181 head = &tgt_net->dev_index_head[h];
2182 hlist_for_each_entry(dev, head, index_hlist) {
2183 if (link_dump_filtered(dev, master_idx, kind_ops))
2187 err = rtnl_fill_ifinfo(skb, dev, net,
2189 NETLINK_CB(cb->skb).portid,
2190 nlh->nlmsg_seq, 0, flags,
2191 ext_filter_mask, 0, NULL, 0,
2192 netnsid, GFP_KERNEL);
2195 if (likely(skb->len))
2209 cb->seq = tgt_net->dev_base_seq;
2210 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2217 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2218 struct netlink_ext_ack *exterr)
2220 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2223 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2225 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2228 /* Examine the link attributes and figure out which
2229 * network namespace we are talking about.
2231 if (tb[IFLA_NET_NS_PID])
2232 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2233 else if (tb[IFLA_NET_NS_FD])
2234 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2236 net = get_net(src_net);
2239 EXPORT_SYMBOL(rtnl_link_get_net);
2241 /* Figure out which network namespace we are talking about by
2242 * examining the link attributes in the following order:
2244 * 1. IFLA_NET_NS_PID
2246 * 3. IFLA_TARGET_NETNSID
2248 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2249 struct nlattr *tb[])
2253 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2254 return rtnl_link_get_net(src_net, tb);
2256 if (!tb[IFLA_TARGET_NETNSID])
2257 return get_net(src_net);
2259 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2261 return ERR_PTR(-EINVAL);
2266 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2267 struct net *src_net,
2268 struct nlattr *tb[], int cap)
2272 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2276 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2278 return ERR_PTR(-EPERM);
2284 /* Verify that rtnetlink requests do not pass additional properties
2285 * potentially referring to different network namespaces.
2287 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2288 struct netlink_ext_ack *extack,
2292 if (netns_id_only) {
2293 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2296 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2300 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2303 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2306 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2312 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2316 static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2319 const struct net_device_ops *ops = dev->netdev_ops;
2321 if (!ops->ndo_set_vf_rate)
2323 if (max_tx_rate && max_tx_rate < min_tx_rate)
2326 return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2329 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2330 struct netlink_ext_ack *extack)
2333 if (tb[IFLA_ADDRESS] &&
2334 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2337 if (tb[IFLA_BROADCAST] &&
2338 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2342 if (tb[IFLA_AF_SPEC]) {
2346 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2347 const struct rtnl_af_ops *af_ops;
2349 af_ops = rtnl_af_lookup(nla_type(af));
2351 return -EAFNOSUPPORT;
2353 if (!af_ops->set_link_af)
2356 if (af_ops->validate_link_af) {
2357 err = af_ops->validate_link_af(dev, af, extack);
2367 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2370 const struct net_device_ops *ops = dev->netdev_ops;
2372 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2375 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2377 if (dev->type != ARPHRD_INFINIBAND)
2380 return handle_infiniband_guid(dev, ivt, guid_type);
2383 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2385 const struct net_device_ops *ops = dev->netdev_ops;
2388 if (tb[IFLA_VF_MAC]) {
2389 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2391 if (ivm->vf >= INT_MAX)
2394 if (ops->ndo_set_vf_mac)
2395 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2401 if (tb[IFLA_VF_VLAN]) {
2402 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2404 if (ivv->vf >= INT_MAX)
2407 if (ops->ndo_set_vf_vlan)
2408 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2410 htons(ETH_P_8021Q));
2415 if (tb[IFLA_VF_VLAN_LIST]) {
2416 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2417 struct nlattr *attr;
2421 if (!ops->ndo_set_vf_vlan)
2424 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2425 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2426 nla_len(attr) < NLA_HDRLEN) {
2429 if (len >= MAX_VLAN_LIST_LEN)
2431 ivvl[len] = nla_data(attr);
2438 if (ivvl[0]->vf >= INT_MAX)
2440 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2441 ivvl[0]->qos, ivvl[0]->vlan_proto);
2446 if (tb[IFLA_VF_TX_RATE]) {
2447 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2448 struct ifla_vf_info ivf;
2450 if (ivt->vf >= INT_MAX)
2453 if (ops->ndo_get_vf_config)
2454 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2458 err = rtnl_set_vf_rate(dev, ivt->vf,
2459 ivf.min_tx_rate, ivt->rate);
2464 if (tb[IFLA_VF_RATE]) {
2465 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2467 if (ivt->vf >= INT_MAX)
2470 err = rtnl_set_vf_rate(dev, ivt->vf,
2471 ivt->min_tx_rate, ivt->max_tx_rate);
2476 if (tb[IFLA_VF_SPOOFCHK]) {
2477 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2479 if (ivs->vf >= INT_MAX)
2482 if (ops->ndo_set_vf_spoofchk)
2483 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2489 if (tb[IFLA_VF_LINK_STATE]) {
2490 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2492 if (ivl->vf >= INT_MAX)
2495 if (ops->ndo_set_vf_link_state)
2496 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2502 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2503 struct ifla_vf_rss_query_en *ivrssq_en;
2506 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2507 if (ivrssq_en->vf >= INT_MAX)
2509 if (ops->ndo_set_vf_rss_query_en)
2510 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2511 ivrssq_en->setting);
2516 if (tb[IFLA_VF_TRUST]) {
2517 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2519 if (ivt->vf >= INT_MAX)
2522 if (ops->ndo_set_vf_trust)
2523 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2528 if (tb[IFLA_VF_IB_NODE_GUID]) {
2529 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2531 if (ivt->vf >= INT_MAX)
2533 if (!ops->ndo_set_vf_guid)
2535 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2538 if (tb[IFLA_VF_IB_PORT_GUID]) {
2539 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2541 if (ivt->vf >= INT_MAX)
2543 if (!ops->ndo_set_vf_guid)
2546 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2552 static int do_set_master(struct net_device *dev, int ifindex,
2553 struct netlink_ext_ack *extack)
2555 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2556 const struct net_device_ops *ops;
2560 if (upper_dev->ifindex == ifindex)
2562 ops = upper_dev->netdev_ops;
2563 if (ops->ndo_del_slave) {
2564 err = ops->ndo_del_slave(upper_dev, dev);
2573 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2576 ops = upper_dev->netdev_ops;
2577 if (ops->ndo_add_slave) {
2578 err = ops->ndo_add_slave(upper_dev, dev, extack);
2588 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2589 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2590 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2593 static int do_set_proto_down(struct net_device *dev,
2594 struct nlattr *nl_proto_down,
2595 struct nlattr *nl_proto_down_reason,
2596 struct netlink_ext_ack *extack)
2598 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2599 unsigned long mask = 0;
2604 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2605 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2609 if (nl_proto_down_reason) {
2610 err = nla_parse_nested_deprecated(pdreason,
2611 IFLA_PROTO_DOWN_REASON_MAX,
2612 nl_proto_down_reason,
2613 ifla_proto_down_reason_policy,
2618 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2619 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2623 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2625 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2626 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2628 dev_change_proto_down_reason(dev, mask, value);
2631 if (nl_proto_down) {
2632 proto_down = nla_get_u8(nl_proto_down);
2634 /* Don't turn off protodown if there are active reasons */
2635 if (!proto_down && dev->proto_down_reason) {
2636 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2639 err = dev_change_proto_down(dev,
2648 #define DO_SETLINK_MODIFIED 0x01
2649 /* notify flag means notify + modified. */
2650 #define DO_SETLINK_NOTIFY 0x03
2651 static int do_setlink(const struct sk_buff *skb,
2652 struct net_device *dev, struct ifinfomsg *ifm,
2653 struct netlink_ext_ack *extack,
2654 struct nlattr **tb, int status)
2656 const struct net_device_ops *ops = dev->netdev_ops;
2657 char ifname[IFNAMSIZ];
2660 err = validate_linkmsg(dev, tb, extack);
2664 if (tb[IFLA_IFNAME])
2665 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2669 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2670 const char *pat = ifname[0] ? ifname : NULL;
2674 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2681 if (tb[IFLA_NEW_IFINDEX])
2682 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2686 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2690 status |= DO_SETLINK_MODIFIED;
2694 struct rtnl_link_ifmap *u_map;
2697 if (!ops->ndo_set_config) {
2702 if (!netif_device_present(dev)) {
2707 u_map = nla_data(tb[IFLA_MAP]);
2708 k_map.mem_start = (unsigned long) u_map->mem_start;
2709 k_map.mem_end = (unsigned long) u_map->mem_end;
2710 k_map.base_addr = (unsigned short) u_map->base_addr;
2711 k_map.irq = (unsigned char) u_map->irq;
2712 k_map.dma = (unsigned char) u_map->dma;
2713 k_map.port = (unsigned char) u_map->port;
2715 err = ops->ndo_set_config(dev, &k_map);
2719 status |= DO_SETLINK_NOTIFY;
2722 if (tb[IFLA_ADDRESS]) {
2723 struct sockaddr *sa;
2726 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2728 sa = kmalloc(len, GFP_KERNEL);
2733 sa->sa_family = dev->type;
2734 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2736 err = dev_set_mac_address_user(dev, sa, extack);
2740 status |= DO_SETLINK_MODIFIED;
2744 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2747 status |= DO_SETLINK_MODIFIED;
2750 if (tb[IFLA_GROUP]) {
2751 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2752 status |= DO_SETLINK_NOTIFY;
2756 * Interface selected by interface index but interface
2757 * name provided implies that a name change has been
2760 if (ifm->ifi_index > 0 && ifname[0]) {
2761 err = dev_change_name(dev, ifname);
2764 status |= DO_SETLINK_MODIFIED;
2767 if (tb[IFLA_IFALIAS]) {
2768 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2769 nla_len(tb[IFLA_IFALIAS]));
2772 status |= DO_SETLINK_NOTIFY;
2775 if (tb[IFLA_BROADCAST]) {
2776 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2777 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2780 if (tb[IFLA_MASTER]) {
2781 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2784 status |= DO_SETLINK_MODIFIED;
2787 if (ifm->ifi_flags || ifm->ifi_change) {
2788 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2794 if (tb[IFLA_CARRIER]) {
2795 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2798 status |= DO_SETLINK_MODIFIED;
2801 if (tb[IFLA_TXQLEN]) {
2802 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2804 err = dev_change_tx_queue_len(dev, value);
2807 status |= DO_SETLINK_MODIFIED;
2810 if (tb[IFLA_GSO_MAX_SIZE]) {
2811 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2813 if (max_size > dev->tso_max_size) {
2818 if (dev->gso_max_size ^ max_size) {
2819 netif_set_gso_max_size(dev, max_size);
2820 status |= DO_SETLINK_MODIFIED;
2824 if (tb[IFLA_GSO_MAX_SEGS]) {
2825 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2827 if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) {
2832 if (dev->gso_max_segs ^ max_segs) {
2833 netif_set_gso_max_segs(dev, max_segs);
2834 status |= DO_SETLINK_MODIFIED;
2838 if (tb[IFLA_GRO_MAX_SIZE]) {
2839 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2841 if (dev->gro_max_size ^ gro_max_size) {
2842 netif_set_gro_max_size(dev, gro_max_size);
2843 status |= DO_SETLINK_MODIFIED;
2847 if (tb[IFLA_OPERSTATE])
2848 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2850 if (tb[IFLA_LINKMODE]) {
2851 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2853 write_lock(&dev_base_lock);
2854 if (dev->link_mode ^ value)
2855 status |= DO_SETLINK_NOTIFY;
2856 dev->link_mode = value;
2857 write_unlock(&dev_base_lock);
2860 if (tb[IFLA_VFINFO_LIST]) {
2861 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2862 struct nlattr *attr;
2865 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2866 if (nla_type(attr) != IFLA_VF_INFO ||
2867 nla_len(attr) < NLA_HDRLEN) {
2871 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2877 err = do_setvfinfo(dev, vfinfo);
2880 status |= DO_SETLINK_NOTIFY;
2885 if (tb[IFLA_VF_PORTS]) {
2886 struct nlattr *port[IFLA_PORT_MAX+1];
2887 struct nlattr *attr;
2892 if (!ops->ndo_set_vf_port)
2895 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2896 if (nla_type(attr) != IFLA_VF_PORT ||
2897 nla_len(attr) < NLA_HDRLEN) {
2901 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2907 if (!port[IFLA_PORT_VF]) {
2911 vf = nla_get_u32(port[IFLA_PORT_VF]);
2912 err = ops->ndo_set_vf_port(dev, vf, port);
2915 status |= DO_SETLINK_NOTIFY;
2920 if (tb[IFLA_PORT_SELF]) {
2921 struct nlattr *port[IFLA_PORT_MAX+1];
2923 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2925 ifla_port_policy, NULL);
2930 if (ops->ndo_set_vf_port)
2931 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2934 status |= DO_SETLINK_NOTIFY;
2937 if (tb[IFLA_AF_SPEC]) {
2941 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2942 const struct rtnl_af_ops *af_ops;
2944 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2946 err = af_ops->set_link_af(dev, af, extack);
2950 status |= DO_SETLINK_NOTIFY;
2955 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2956 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2957 tb[IFLA_PROTO_DOWN_REASON], extack);
2960 status |= DO_SETLINK_NOTIFY;
2964 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2967 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2969 ifla_xdp_policy, NULL);
2973 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2978 if (xdp[IFLA_XDP_FLAGS]) {
2979 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2980 if (xdp_flags & ~XDP_FLAGS_MASK) {
2984 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2990 if (xdp[IFLA_XDP_FD]) {
2991 int expected_fd = -1;
2993 if (xdp_flags & XDP_FLAGS_REPLACE) {
2994 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2999 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
3002 err = dev_change_xdp_fd(dev, extack,
3003 nla_get_s32(xdp[IFLA_XDP_FD]),
3008 status |= DO_SETLINK_NOTIFY;
3013 if (status & DO_SETLINK_MODIFIED) {
3014 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3015 netdev_state_change(dev);
3018 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",
3025 static struct net_device *rtnl_dev_get(struct net *net,
3026 struct nlattr *tb[])
3028 char ifname[ALTIFNAMSIZ];
3030 if (tb[IFLA_IFNAME])
3031 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3032 else if (tb[IFLA_ALT_IFNAME])
3033 nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3037 return __dev_get_by_name(net, ifname);
3040 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3041 struct netlink_ext_ack *extack)
3043 struct net *net = sock_net(skb->sk);
3044 struct ifinfomsg *ifm;
3045 struct net_device *dev;
3047 struct nlattr *tb[IFLA_MAX+1];
3049 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3050 ifla_policy, extack);
3054 err = rtnl_ensure_unique_netns(tb, extack, false);
3059 ifm = nlmsg_data(nlh);
3060 if (ifm->ifi_index > 0)
3061 dev = __dev_get_by_index(net, ifm->ifi_index);
3062 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3063 dev = rtnl_dev_get(net, tb);
3072 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3077 static int rtnl_group_dellink(const struct net *net, int group)
3079 struct net_device *dev, *aux;
3080 LIST_HEAD(list_kill);
3086 for_each_netdev(net, dev) {
3087 if (dev->group == group) {
3088 const struct rtnl_link_ops *ops;
3091 ops = dev->rtnl_link_ops;
3092 if (!ops || !ops->dellink)
3100 for_each_netdev_safe(net, dev, aux) {
3101 if (dev->group == group) {
3102 const struct rtnl_link_ops *ops;
3104 ops = dev->rtnl_link_ops;
3105 ops->dellink(dev, &list_kill);
3108 unregister_netdevice_many(&list_kill);
3113 int rtnl_delete_link(struct net_device *dev)
3115 const struct rtnl_link_ops *ops;
3116 LIST_HEAD(list_kill);
3118 ops = dev->rtnl_link_ops;
3119 if (!ops || !ops->dellink)
3122 ops->dellink(dev, &list_kill);
3123 unregister_netdevice_many(&list_kill);
3127 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3129 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3130 struct netlink_ext_ack *extack)
3132 struct net *net = sock_net(skb->sk);
3133 struct net *tgt_net = net;
3134 struct net_device *dev = NULL;
3135 struct ifinfomsg *ifm;
3136 struct nlattr *tb[IFLA_MAX+1];
3140 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3141 ifla_policy, extack);
3145 err = rtnl_ensure_unique_netns(tb, extack, true);
3149 if (tb[IFLA_TARGET_NETNSID]) {
3150 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3151 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3152 if (IS_ERR(tgt_net))
3153 return PTR_ERR(tgt_net);
3157 ifm = nlmsg_data(nlh);
3158 if (ifm->ifi_index > 0)
3159 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3160 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3161 dev = rtnl_dev_get(net, tb);
3162 else if (tb[IFLA_GROUP])
3163 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3168 if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3174 err = rtnl_delete_link(dev);
3183 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3185 unsigned int old_flags;
3188 old_flags = dev->flags;
3189 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3190 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3196 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3197 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3199 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3200 __dev_notify_flags(dev, old_flags, ~0U);
3204 EXPORT_SYMBOL(rtnl_configure_link);
3206 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3207 unsigned char name_assign_type,
3208 const struct rtnl_link_ops *ops,
3209 struct nlattr *tb[],
3210 struct netlink_ext_ack *extack)
3212 struct net_device *dev;
3213 unsigned int num_tx_queues = 1;
3214 unsigned int num_rx_queues = 1;
3216 if (tb[IFLA_NUM_TX_QUEUES])
3217 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3218 else if (ops->get_num_tx_queues)
3219 num_tx_queues = ops->get_num_tx_queues();
3221 if (tb[IFLA_NUM_RX_QUEUES])
3222 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3223 else if (ops->get_num_rx_queues)
3224 num_rx_queues = ops->get_num_rx_queues();
3226 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3227 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3228 return ERR_PTR(-EINVAL);
3231 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3232 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3233 return ERR_PTR(-EINVAL);
3237 dev = ops->alloc(tb, ifname, name_assign_type,
3238 num_tx_queues, num_rx_queues);
3242 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3243 name_assign_type, ops->setup,
3244 num_tx_queues, num_rx_queues);
3248 return ERR_PTR(-ENOMEM);
3250 dev_net_set(dev, net);
3251 dev->rtnl_link_ops = ops;
3252 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3255 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3258 err = dev_validate_mtu(dev, mtu, extack);
3261 return ERR_PTR(err);
3265 if (tb[IFLA_ADDRESS]) {
3266 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3267 nla_len(tb[IFLA_ADDRESS]));
3268 dev->addr_assign_type = NET_ADDR_SET;
3270 if (tb[IFLA_BROADCAST])
3271 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3272 nla_len(tb[IFLA_BROADCAST]));
3273 if (tb[IFLA_TXQLEN])
3274 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3275 if (tb[IFLA_OPERSTATE])
3276 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3277 if (tb[IFLA_LINKMODE])
3278 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3280 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3281 if (tb[IFLA_GSO_MAX_SIZE])
3282 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3283 if (tb[IFLA_GSO_MAX_SEGS])
3284 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3285 if (tb[IFLA_GRO_MAX_SIZE])
3286 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3290 EXPORT_SYMBOL(rtnl_create_link);
3292 static int rtnl_group_changelink(const struct sk_buff *skb,
3293 struct net *net, int group,
3294 struct ifinfomsg *ifm,
3295 struct netlink_ext_ack *extack,
3298 struct net_device *dev, *aux;
3301 for_each_netdev_safe(net, dev, aux) {
3302 if (dev->group == group) {
3303 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3312 static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3313 const struct rtnl_link_ops *ops,
3314 struct nlattr **tb, struct nlattr **data,
3315 struct netlink_ext_ack *extack)
3317 unsigned char name_assign_type = NET_NAME_USER;
3318 struct net *net = sock_net(skb->sk);
3319 struct net *dest_net, *link_net;
3320 struct net_device *dev;
3321 char ifname[IFNAMSIZ];
3324 if (!ops->alloc && !ops->setup)
3327 if (tb[IFLA_IFNAME]) {
3328 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3330 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3331 name_assign_type = NET_NAME_ENUM;
3334 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3335 if (IS_ERR(dest_net))
3336 return PTR_ERR(dest_net);
3338 if (tb[IFLA_LINK_NETNSID]) {
3339 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3341 link_net = get_net_ns_by_id(dest_net, id);
3343 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3348 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3354 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3355 name_assign_type, ops, tb, extack);
3361 dev->ifindex = ifm->ifi_index;
3364 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3366 err = register_netdevice(dev);
3372 err = rtnl_configure_link(dev, ifm);
3374 goto out_unregister;
3376 err = dev_change_net_namespace(dev, dest_net, ifname);
3378 goto out_unregister;
3380 if (tb[IFLA_MASTER]) {
3381 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3383 goto out_unregister;
3392 LIST_HEAD(list_kill);
3394 ops->dellink(dev, &list_kill);
3395 unregister_netdevice_many(&list_kill);
3397 unregister_netdevice(dev);
3402 struct rtnl_newlink_tbs {
3403 struct nlattr *tb[IFLA_MAX + 1];
3404 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3405 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3408 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3409 struct rtnl_newlink_tbs *tbs,
3410 struct netlink_ext_ack *extack)
3412 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3413 struct nlattr ** const tb = tbs->tb;
3414 const struct rtnl_link_ops *m_ops;
3415 struct net_device *master_dev;
3416 struct net *net = sock_net(skb->sk);
3417 const struct rtnl_link_ops *ops;
3418 struct nlattr **slave_data;
3419 char kind[MODULE_NAME_LEN];
3420 struct net_device *dev;
3421 struct ifinfomsg *ifm;
3422 struct nlattr **data;
3423 bool link_specified;
3426 #ifdef CONFIG_MODULES
3429 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3430 ifla_policy, extack);
3434 err = rtnl_ensure_unique_netns(tb, extack, false);
3438 ifm = nlmsg_data(nlh);
3439 if (ifm->ifi_index > 0) {
3440 link_specified = true;
3441 dev = __dev_get_by_index(net, ifm->ifi_index);
3442 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3443 link_specified = true;
3444 dev = rtnl_dev_get(net, tb);
3446 link_specified = false;
3453 master_dev = netdev_master_upper_dev_get(dev);
3455 m_ops = master_dev->rtnl_link_ops;
3458 err = validate_linkmsg(dev, tb, extack);
3462 if (tb[IFLA_LINKINFO]) {
3463 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3465 ifla_info_policy, NULL);
3469 memset(linkinfo, 0, sizeof(linkinfo));
3471 if (linkinfo[IFLA_INFO_KIND]) {
3472 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3473 ops = rtnl_link_ops_get(kind);
3481 if (ops->maxtype > RTNL_MAX_TYPE)
3484 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3485 err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
3486 linkinfo[IFLA_INFO_DATA],
3487 ops->policy, extack);
3492 if (ops->validate) {
3493 err = ops->validate(tb, data, extack);
3501 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3504 if (m_ops->slave_maxtype &&
3505 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3506 err = nla_parse_nested_deprecated(tbs->slave_attr,
3507 m_ops->slave_maxtype,
3508 linkinfo[IFLA_INFO_SLAVE_DATA],
3509 m_ops->slave_policy,
3513 slave_data = tbs->slave_attr;
3520 if (nlh->nlmsg_flags & NLM_F_EXCL)
3522 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3525 if (linkinfo[IFLA_INFO_DATA]) {
3526 if (!ops || ops != dev->rtnl_link_ops ||
3530 err = ops->changelink(dev, tb, data, extack);
3533 status |= DO_SETLINK_NOTIFY;
3536 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3537 if (!m_ops || !m_ops->slave_changelink)
3540 err = m_ops->slave_changelink(master_dev, dev, tb,
3541 slave_data, extack);
3544 status |= DO_SETLINK_NOTIFY;
3547 return do_setlink(skb, dev, ifm, extack, tb, status);
3550 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3551 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3552 * or it's for a group
3557 return rtnl_group_changelink(skb, net,
3558 nla_get_u32(tb[IFLA_GROUP]),
3563 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3567 #ifdef CONFIG_MODULES
3570 request_module("rtnl-link-%s", kind);
3572 ops = rtnl_link_ops_get(kind);
3577 NL_SET_ERR_MSG(extack, "Unknown device type");
3581 return rtnl_newlink_create(skb, ifm, ops, tb, data, extack);
3584 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3585 struct netlink_ext_ack *extack)
3587 struct rtnl_newlink_tbs *tbs;
3590 tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3594 ret = __rtnl_newlink(skb, nlh, tbs, extack);
3599 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3600 const struct nlmsghdr *nlh,
3602 struct netlink_ext_ack *extack)
3604 struct ifinfomsg *ifm;
3607 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3608 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3612 if (!netlink_strict_get_check(skb))
3613 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3614 ifla_policy, extack);
3616 ifm = nlmsg_data(nlh);
3617 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3619 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3623 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3624 ifla_policy, extack);
3628 for (i = 0; i <= IFLA_MAX; i++) {
3634 case IFLA_ALT_IFNAME:
3636 case IFLA_TARGET_NETNSID:
3639 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3647 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3648 struct netlink_ext_ack *extack)
3650 struct net *net = sock_net(skb->sk);
3651 struct net *tgt_net = net;
3652 struct ifinfomsg *ifm;
3653 struct nlattr *tb[IFLA_MAX+1];
3654 struct net_device *dev = NULL;
3655 struct sk_buff *nskb;
3658 u32 ext_filter_mask = 0;
3660 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3664 err = rtnl_ensure_unique_netns(tb, extack, true);
3668 if (tb[IFLA_TARGET_NETNSID]) {
3669 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3670 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3671 if (IS_ERR(tgt_net))
3672 return PTR_ERR(tgt_net);
3675 if (tb[IFLA_EXT_MASK])
3676 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3679 ifm = nlmsg_data(nlh);
3680 if (ifm->ifi_index > 0)
3681 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3682 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3683 dev = rtnl_dev_get(tgt_net, tb);
3692 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3696 err = rtnl_fill_ifinfo(nskb, dev, net,
3697 RTM_NEWLINK, NETLINK_CB(skb).portid,
3698 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3699 0, NULL, 0, netnsid, GFP_KERNEL);
3701 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3702 WARN_ON(err == -EMSGSIZE);
3705 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3713 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3714 bool *changed, struct netlink_ext_ack *extack)
3720 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3724 if (cmd == RTM_NEWLINKPROP) {
3725 size = rtnl_prop_list_size(dev);
3726 size += nla_total_size(ALTIFNAMSIZ);
3727 if (size >= U16_MAX) {
3728 NL_SET_ERR_MSG(extack,
3729 "effective property list too long");
3734 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3738 if (cmd == RTM_NEWLINKPROP) {
3739 err = netdev_name_node_alt_create(dev, alt_ifname);
3742 } else if (cmd == RTM_DELLINKPROP) {
3743 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3755 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3756 struct netlink_ext_ack *extack)
3758 struct net *net = sock_net(skb->sk);
3759 struct nlattr *tb[IFLA_MAX + 1];
3760 struct net_device *dev;
3761 struct ifinfomsg *ifm;
3762 bool changed = false;
3763 struct nlattr *attr;
3766 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3770 err = rtnl_ensure_unique_netns(tb, extack, true);
3774 ifm = nlmsg_data(nlh);
3775 if (ifm->ifi_index > 0)
3776 dev = __dev_get_by_index(net, ifm->ifi_index);
3777 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3778 dev = rtnl_dev_get(net, tb);
3785 if (!tb[IFLA_PROP_LIST])
3788 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3789 switch (nla_type(attr)) {
3790 case IFLA_ALT_IFNAME:
3791 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3799 netdev_state_change(dev);
3803 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3804 struct netlink_ext_ack *extack)
3806 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3809 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3810 struct netlink_ext_ack *extack)
3812 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3815 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3817 struct net *net = sock_net(skb->sk);
3818 size_t min_ifinfo_dump_size = 0;
3819 struct nlattr *tb[IFLA_MAX+1];
3820 u32 ext_filter_mask = 0;
3821 struct net_device *dev;
3824 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3825 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3826 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3828 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3829 if (tb[IFLA_EXT_MASK])
3830 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3833 if (!ext_filter_mask)
3834 return NLMSG_GOODSIZE;
3836 * traverse the list of net devices and compute the minimum
3837 * buffer size based upon the filter mask.
3840 for_each_netdev_rcu(net, dev) {
3841 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3842 if_nlmsg_size(dev, ext_filter_mask));
3846 return nlmsg_total_size(min_ifinfo_dump_size);
3849 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3852 int s_idx = cb->family;
3853 int type = cb->nlh->nlmsg_type - RTM_BASE;
3859 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3860 struct rtnl_link __rcu **tab;
3861 struct rtnl_link *link;
3862 rtnl_dumpit_func dumpit;
3864 if (idx < s_idx || idx == PF_PACKET)
3867 if (type < 0 || type >= RTM_NR_MSGTYPES)
3870 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3874 link = rcu_dereference_rtnl(tab[type]);
3878 dumpit = link->dumpit;
3883 memset(&cb->args[0], 0, sizeof(cb->args));
3887 ret = dumpit(skb, cb);
3893 return skb->len ? : ret;
3896 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3897 unsigned int change,
3898 u32 event, gfp_t flags, int *new_nsid,
3901 struct net *net = dev_net(dev);
3902 struct sk_buff *skb;
3905 skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
3909 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3910 type, 0, 0, change, 0, 0, event,
3911 new_nsid, new_ifindex, -1, flags);
3913 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3914 WARN_ON(err == -EMSGSIZE);
3921 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3925 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3927 struct net *net = dev_net(dev);
3929 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3932 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3933 unsigned int change, u32 event,
3934 gfp_t flags, int *new_nsid, int new_ifindex)
3936 struct sk_buff *skb;
3938 if (dev->reg_state != NETREG_REGISTERED)
3941 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3944 rtmsg_ifinfo_send(skb, dev, flags);
3947 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3950 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3954 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3955 gfp_t flags, int *new_nsid, int new_ifindex)
3957 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3958 new_nsid, new_ifindex);
3961 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3962 struct net_device *dev,
3963 u8 *addr, u16 vid, u32 pid, u32 seq,
3964 int type, unsigned int flags,
3965 int nlflags, u16 ndm_state)
3967 struct nlmsghdr *nlh;
3970 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3974 ndm = nlmsg_data(nlh);
3975 ndm->ndm_family = AF_BRIDGE;
3978 ndm->ndm_flags = flags;
3980 ndm->ndm_ifindex = dev->ifindex;
3981 ndm->ndm_state = ndm_state;
3983 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3984 goto nla_put_failure;
3986 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3987 goto nla_put_failure;
3989 nlmsg_end(skb, nlh);
3993 nlmsg_cancel(skb, nlh);
3997 static inline size_t rtnl_fdb_nlmsg_size(void)
3999 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4000 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
4001 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
4005 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4008 struct net *net = dev_net(dev);
4009 struct sk_buff *skb;
4012 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
4016 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4017 0, 0, type, NTF_SELF, 0, ndm_state);
4023 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
4026 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
4030 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4032 int ndo_dflt_fdb_add(struct ndmsg *ndm,
4033 struct nlattr *tb[],
4034 struct net_device *dev,
4035 const unsigned char *addr, u16 vid,
4040 /* If aging addresses are supported device will need to
4041 * implement its own handler for this.
4043 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4044 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4049 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
4053 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4054 err = dev_uc_add_excl(dev, addr);
4055 else if (is_multicast_ether_addr(addr))
4056 err = dev_mc_add_excl(dev, addr);
4058 /* Only return duplicate errors if NLM_F_EXCL is set */
4059 if (err == -EEXIST && !(flags & NLM_F_EXCL))
4064 EXPORT_SYMBOL(ndo_dflt_fdb_add);
4066 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4067 struct netlink_ext_ack *extack)
4072 if (nla_len(vlan_attr) != sizeof(u16)) {
4073 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4077 vid = nla_get_u16(vlan_attr);
4079 if (!vid || vid >= VLAN_VID_MASK) {
4080 NL_SET_ERR_MSG(extack, "invalid vlan id");
4088 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4089 struct netlink_ext_ack *extack)
4091 struct net *net = sock_net(skb->sk);
4093 struct nlattr *tb[NDA_MAX+1];
4094 struct net_device *dev;
4099 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4104 ndm = nlmsg_data(nlh);
4105 if (ndm->ndm_ifindex == 0) {
4106 NL_SET_ERR_MSG(extack, "invalid ifindex");
4110 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4112 NL_SET_ERR_MSG(extack, "unknown ifindex");
4116 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4117 NL_SET_ERR_MSG(extack, "invalid address");
4121 if (dev->type != ARPHRD_ETHER) {
4122 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4126 addr = nla_data(tb[NDA_LLADDR]);
4128 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4134 /* Support fdb on master device the net/bridge default case */
4135 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4136 netif_is_bridge_port(dev)) {
4137 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4138 const struct net_device_ops *ops = br_dev->netdev_ops;
4140 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4141 nlh->nlmsg_flags, extack);
4145 ndm->ndm_flags &= ~NTF_MASTER;
4148 /* Embedded bridge, macvlan, and any other device support */
4149 if ((ndm->ndm_flags & NTF_SELF)) {
4150 if (dev->netdev_ops->ndo_fdb_add)
4151 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4156 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4160 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4162 ndm->ndm_flags &= ~NTF_SELF;
4170 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4172 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4173 struct nlattr *tb[],
4174 struct net_device *dev,
4175 const unsigned char *addr, u16 vid)
4179 /* If aging addresses are supported device will need to
4180 * implement its own handler for this.
4182 if (!(ndm->ndm_state & NUD_PERMANENT)) {
4183 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4187 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4188 err = dev_uc_del(dev, addr);
4189 else if (is_multicast_ether_addr(addr))
4190 err = dev_mc_del(dev, addr);
4194 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4196 static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
4197 [NDA_VLAN] = { .type = NLA_U16 },
4198 [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
4199 [NDA_NDM_STATE_MASK] = { .type = NLA_U16 },
4200 [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 },
4203 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4204 struct netlink_ext_ack *extack)
4206 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
4207 struct net *net = sock_net(skb->sk);
4208 const struct net_device_ops *ops;
4210 struct nlattr *tb[NDA_MAX+1];
4211 struct net_device *dev;
4216 if (!netlink_capable(skb, CAP_NET_ADMIN))
4220 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
4223 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
4224 fdb_del_bulk_policy, extack);
4229 ndm = nlmsg_data(nlh);
4230 if (ndm->ndm_ifindex == 0) {
4231 NL_SET_ERR_MSG(extack, "invalid ifindex");
4235 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4237 NL_SET_ERR_MSG(extack, "unknown ifindex");
4242 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4243 NL_SET_ERR_MSG(extack, "invalid address");
4246 addr = nla_data(tb[NDA_LLADDR]);
4249 if (dev->type != ARPHRD_ETHER) {
4250 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4254 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4260 /* Support fdb on master device the net/bridge default case */
4261 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4262 netif_is_bridge_port(dev)) {
4263 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4265 ops = br_dev->netdev_ops;
4267 if (ops->ndo_fdb_del)
4268 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4270 if (ops->ndo_fdb_del_bulk)
4271 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4278 ndm->ndm_flags &= ~NTF_MASTER;
4281 /* Embedded bridge, macvlan, and any other device support */
4282 if (ndm->ndm_flags & NTF_SELF) {
4283 ops = dev->netdev_ops;
4285 if (ops->ndo_fdb_del)
4286 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4288 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4290 /* in case err was cleared by NTF_MASTER call */
4292 if (ops->ndo_fdb_del_bulk)
4293 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4299 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4301 ndm->ndm_flags &= ~NTF_SELF;
4308 static int nlmsg_populate_fdb(struct sk_buff *skb,
4309 struct netlink_callback *cb,
4310 struct net_device *dev,
4312 struct netdev_hw_addr_list *list)
4314 struct netdev_hw_addr *ha;
4318 portid = NETLINK_CB(cb->skb).portid;
4319 seq = cb->nlh->nlmsg_seq;
4321 list_for_each_entry(ha, &list->list, list) {
4322 if (*idx < cb->args[2])
4325 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4327 RTM_NEWNEIGH, NTF_SELF,
4328 NLM_F_MULTI, NUD_PERMANENT);
4338 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4339 * @skb: socket buffer to store message in
4340 * @cb: netlink callback
4342 * @filter_dev: ignored
4343 * @idx: the number of FDB table entries dumped is added to *@idx
4345 * Default netdevice operation to dump the existing unicast address list.
4346 * Returns number of addresses from list put in skb.
4348 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4349 struct netlink_callback *cb,
4350 struct net_device *dev,
4351 struct net_device *filter_dev,
4356 if (dev->type != ARPHRD_ETHER)
4359 netif_addr_lock_bh(dev);
4360 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4363 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4365 netif_addr_unlock_bh(dev);
4368 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4370 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4371 int *br_idx, int *brport_idx,
4372 struct netlink_ext_ack *extack)
4374 struct nlattr *tb[NDA_MAX + 1];
4378 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4379 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4383 ndm = nlmsg_data(nlh);
4384 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4385 ndm->ndm_flags || ndm->ndm_type) {
4386 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4390 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4391 NDA_MAX, NULL, extack);
4395 *brport_idx = ndm->ndm_ifindex;
4396 for (i = 0; i <= NDA_MAX; ++i) {
4402 if (nla_len(tb[i]) != sizeof(u32)) {
4403 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4406 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4409 if (nla_len(tb[i]) != sizeof(u32)) {
4410 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4413 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4416 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4424 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4425 int *br_idx, int *brport_idx,
4426 struct netlink_ext_ack *extack)
4428 struct nlattr *tb[IFLA_MAX+1];
4431 /* A hack to preserve kernel<->userspace interface.
4432 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4433 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4434 * So, check for ndmsg with an optional u32 attribute (not used here).
4435 * Fortunately these sizes don't conflict with the size of ifinfomsg
4436 * with an optional attribute.
4438 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4439 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4440 nla_attr_size(sizeof(u32)))) {
4441 struct ifinfomsg *ifm;
4443 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4444 tb, IFLA_MAX, ifla_policy,
4448 } else if (err == 0) {
4449 if (tb[IFLA_MASTER])
4450 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4453 ifm = nlmsg_data(nlh);
4454 *brport_idx = ifm->ifi_index;
4459 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4461 struct net_device *dev;
4462 struct net_device *br_dev = NULL;
4463 const struct net_device_ops *ops = NULL;
4464 const struct net_device_ops *cops = NULL;
4465 struct net *net = sock_net(skb->sk);
4466 struct hlist_head *head;
4474 if (cb->strict_check)
4475 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4478 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4484 br_dev = __dev_get_by_index(net, br_idx);
4488 ops = br_dev->netdev_ops;
4492 s_idx = cb->args[1];
4494 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4496 head = &net->dev_index_head[h];
4497 hlist_for_each_entry(dev, head, index_hlist) {
4499 if (brport_idx && (dev->ifindex != brport_idx))
4502 if (!br_idx) { /* user did not specify a specific bridge */
4503 if (netif_is_bridge_port(dev)) {
4504 br_dev = netdev_master_upper_dev_get(dev);
4505 cops = br_dev->netdev_ops;
4508 if (dev != br_dev &&
4509 !netif_is_bridge_port(dev))
4512 if (br_dev != netdev_master_upper_dev_get(dev) &&
4513 !netif_is_bridge_master(dev))
4521 if (netif_is_bridge_port(dev)) {
4522 if (cops && cops->ndo_fdb_dump) {
4523 err = cops->ndo_fdb_dump(skb, cb,
4526 if (err == -EMSGSIZE)
4531 if (dev->netdev_ops->ndo_fdb_dump)
4532 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4536 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4538 if (err == -EMSGSIZE)
4543 /* reset fdb offset to 0 for rest of the interfaces */
4559 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4560 struct nlattr **tb, u8 *ndm_flags,
4561 int *br_idx, int *brport_idx, u8 **addr,
4562 u16 *vid, struct netlink_ext_ack *extack)
4567 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4568 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4572 ndm = nlmsg_data(nlh);
4573 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4575 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4579 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4580 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4584 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4585 NDA_MAX, nda_policy, extack);
4589 *ndm_flags = ndm->ndm_flags;
4590 *brport_idx = ndm->ndm_ifindex;
4591 for (i = 0; i <= NDA_MAX; ++i) {
4597 *br_idx = nla_get_u32(tb[i]);
4600 if (nla_len(tb[i]) != ETH_ALEN) {
4601 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4604 *addr = nla_data(tb[i]);
4607 err = fdb_vid_parse(tb[i], vid, extack);
4614 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4622 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4623 struct netlink_ext_ack *extack)
4625 struct net_device *dev = NULL, *br_dev = NULL;
4626 const struct net_device_ops *ops = NULL;
4627 struct net *net = sock_net(in_skb->sk);
4628 struct nlattr *tb[NDA_MAX + 1];
4629 struct sk_buff *skb;
4637 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4638 &brport_idx, &addr, &vid, extack);
4643 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4648 dev = __dev_get_by_index(net, brport_idx);
4650 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4657 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4661 br_dev = __dev_get_by_index(net, br_idx);
4663 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4666 ops = br_dev->netdev_ops;
4670 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4671 if (!netif_is_bridge_port(dev)) {
4672 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4675 br_dev = netdev_master_upper_dev_get(dev);
4677 NL_SET_ERR_MSG(extack, "Master of device not found");
4680 ops = br_dev->netdev_ops;
4682 if (!(ndm_flags & NTF_SELF)) {
4683 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4686 ops = dev->netdev_ops;
4690 if (!br_dev && !dev) {
4691 NL_SET_ERR_MSG(extack, "No device specified");
4695 if (!ops || !ops->ndo_fdb_get) {
4696 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4700 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4706 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4707 NETLINK_CB(in_skb).portid,
4708 nlh->nlmsg_seq, extack);
4712 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4718 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4719 unsigned int attrnum, unsigned int flag)
4722 return nla_put_u8(skb, attrnum, !!(flags & flag));
4726 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4727 struct net_device *dev, u16 mode,
4728 u32 flags, u32 mask, int nlflags,
4730 int (*vlan_fill)(struct sk_buff *skb,
4731 struct net_device *dev,
4734 struct nlmsghdr *nlh;
4735 struct ifinfomsg *ifm;
4736 struct nlattr *br_afspec;
4737 struct nlattr *protinfo;
4738 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4739 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4742 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4746 ifm = nlmsg_data(nlh);
4747 ifm->ifi_family = AF_BRIDGE;
4749 ifm->ifi_type = dev->type;
4750 ifm->ifi_index = dev->ifindex;
4751 ifm->ifi_flags = dev_get_flags(dev);
4752 ifm->ifi_change = 0;
4755 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4756 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4757 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4759 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4761 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4762 (dev->ifindex != dev_get_iflink(dev) &&
4763 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4764 goto nla_put_failure;
4766 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4768 goto nla_put_failure;
4770 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4771 nla_nest_cancel(skb, br_afspec);
4772 goto nla_put_failure;
4775 if (mode != BRIDGE_MODE_UNDEF) {
4776 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4777 nla_nest_cancel(skb, br_afspec);
4778 goto nla_put_failure;
4782 err = vlan_fill(skb, dev, filter_mask);
4784 nla_nest_cancel(skb, br_afspec);
4785 goto nla_put_failure;
4788 nla_nest_end(skb, br_afspec);
4790 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4792 goto nla_put_failure;
4794 if (brport_nla_put_flag(skb, flags, mask,
4795 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4796 brport_nla_put_flag(skb, flags, mask,
4797 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4798 brport_nla_put_flag(skb, flags, mask,
4799 IFLA_BRPORT_FAST_LEAVE,
4800 BR_MULTICAST_FAST_LEAVE) ||
4801 brport_nla_put_flag(skb, flags, mask,
4802 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4803 brport_nla_put_flag(skb, flags, mask,
4804 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4805 brport_nla_put_flag(skb, flags, mask,
4806 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4807 brport_nla_put_flag(skb, flags, mask,
4808 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4809 brport_nla_put_flag(skb, flags, mask,
4810 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4811 brport_nla_put_flag(skb, flags, mask,
4812 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4813 brport_nla_put_flag(skb, flags, mask,
4814 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4815 nla_nest_cancel(skb, protinfo);
4816 goto nla_put_failure;
4819 nla_nest_end(skb, protinfo);
4821 nlmsg_end(skb, nlh);
4824 nlmsg_cancel(skb, nlh);
4825 return err ? err : -EMSGSIZE;
4827 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4829 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4830 bool strict_check, u32 *filter_mask,
4831 struct netlink_ext_ack *extack)
4833 struct nlattr *tb[IFLA_MAX+1];
4837 struct ifinfomsg *ifm;
4839 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4840 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4844 ifm = nlmsg_data(nlh);
4845 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4846 ifm->ifi_change || ifm->ifi_index) {
4847 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4851 err = nlmsg_parse_deprecated_strict(nlh,
4852 sizeof(struct ifinfomsg),
4853 tb, IFLA_MAX, ifla_policy,
4856 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4857 tb, IFLA_MAX, ifla_policy,
4863 /* new attributes should only be added with strict checking */
4864 for (i = 0; i <= IFLA_MAX; ++i) {
4870 *filter_mask = nla_get_u32(tb[i]);
4874 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4883 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4885 const struct nlmsghdr *nlh = cb->nlh;
4886 struct net *net = sock_net(skb->sk);
4887 struct net_device *dev;
4889 u32 portid = NETLINK_CB(cb->skb).portid;
4890 u32 seq = nlh->nlmsg_seq;
4891 u32 filter_mask = 0;
4894 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4896 if (err < 0 && cb->strict_check)
4900 for_each_netdev_rcu(net, dev) {
4901 const struct net_device_ops *ops = dev->netdev_ops;
4902 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4904 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4905 if (idx >= cb->args[0]) {
4906 err = br_dev->netdev_ops->ndo_bridge_getlink(
4907 skb, portid, seq, dev,
4908 filter_mask, NLM_F_MULTI);
4909 if (err < 0 && err != -EOPNOTSUPP) {
4910 if (likely(skb->len))
4919 if (ops->ndo_bridge_getlink) {
4920 if (idx >= cb->args[0]) {
4921 err = ops->ndo_bridge_getlink(skb, portid,
4925 if (err < 0 && err != -EOPNOTSUPP) {
4926 if (likely(skb->len))
4943 static inline size_t bridge_nlmsg_size(void)
4945 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4946 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4947 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4948 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4949 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4950 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4951 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4952 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4953 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4954 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4955 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4958 static int rtnl_bridge_notify(struct net_device *dev)
4960 struct net *net = dev_net(dev);
4961 struct sk_buff *skb;
4962 int err = -EOPNOTSUPP;
4964 if (!dev->netdev_ops->ndo_bridge_getlink)
4967 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4973 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4977 /* Notification info is only filled for bridge ports, not the bridge
4978 * device itself. Therefore, a zero notification length is valid and
4979 * should not result in an error.
4984 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4987 WARN_ON(err == -EMSGSIZE);
4990 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4994 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4995 struct netlink_ext_ack *extack)
4997 struct net *net = sock_net(skb->sk);
4998 struct ifinfomsg *ifm;
4999 struct net_device *dev;
5000 struct nlattr *br_spec, *attr = NULL;
5001 int rem, err = -EOPNOTSUPP;
5003 bool have_flags = false;
5005 if (nlmsg_len(nlh) < sizeof(*ifm))
5008 ifm = nlmsg_data(nlh);
5009 if (ifm->ifi_family != AF_BRIDGE)
5010 return -EPFNOSUPPORT;
5012 dev = __dev_get_by_index(net, ifm->ifi_index);
5014 NL_SET_ERR_MSG(extack, "unknown ifindex");
5018 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5020 nla_for_each_nested(attr, br_spec, rem) {
5021 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5022 if (nla_len(attr) < sizeof(flags))
5026 flags = nla_get_u16(attr);
5032 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5033 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5035 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
5040 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
5045 flags &= ~BRIDGE_FLAGS_MASTER;
5048 if ((flags & BRIDGE_FLAGS_SELF)) {
5049 if (!dev->netdev_ops->ndo_bridge_setlink)
5052 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
5056 flags &= ~BRIDGE_FLAGS_SELF;
5058 /* Generate event to notify upper layer of bridge
5061 err = rtnl_bridge_notify(dev);
5066 memcpy(nla_data(attr), &flags, sizeof(flags));
5071 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5072 struct netlink_ext_ack *extack)
5074 struct net *net = sock_net(skb->sk);
5075 struct ifinfomsg *ifm;
5076 struct net_device *dev;
5077 struct nlattr *br_spec, *attr = NULL;
5078 int rem, err = -EOPNOTSUPP;
5080 bool have_flags = false;
5082 if (nlmsg_len(nlh) < sizeof(*ifm))
5085 ifm = nlmsg_data(nlh);
5086 if (ifm->ifi_family != AF_BRIDGE)
5087 return -EPFNOSUPPORT;
5089 dev = __dev_get_by_index(net, ifm->ifi_index);
5091 NL_SET_ERR_MSG(extack, "unknown ifindex");
5095 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5097 nla_for_each_nested(attr, br_spec, rem) {
5098 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5099 if (nla_len(attr) < sizeof(flags))
5103 flags = nla_get_u16(attr);
5109 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5110 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5112 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5117 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5121 flags &= ~BRIDGE_FLAGS_MASTER;
5124 if ((flags & BRIDGE_FLAGS_SELF)) {
5125 if (!dev->netdev_ops->ndo_bridge_dellink)
5128 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5132 flags &= ~BRIDGE_FLAGS_SELF;
5134 /* Generate event to notify upper layer of bridge
5137 err = rtnl_bridge_notify(dev);
5142 memcpy(nla_data(attr), &flags, sizeof(flags));
5147 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5149 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5150 (!idxattr || idxattr == attrid);
5154 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
5156 return dev->netdev_ops &&
5157 dev->netdev_ops->ndo_has_offload_stats &&
5158 dev->netdev_ops->ndo_get_offload_stats &&
5159 dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
5163 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5165 return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5166 sizeof(struct rtnl_link_stats64) : 0;
5170 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5171 struct sk_buff *skb)
5173 unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5174 struct nlattr *attr = NULL;
5181 attr = nla_reserve_64bit(skb, attr_id, size,
5182 IFLA_OFFLOAD_XSTATS_UNSPEC);
5186 attr_data = nla_data(attr);
5187 memset(attr_data, 0, size);
5189 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5197 rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
5198 enum netdev_offload_xstats_type type)
5200 bool enabled = netdev_offload_xstats_enabled(dev, type);
5202 return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
5205 struct rtnl_offload_xstats_request_used {
5211 rtnl_offload_xstats_get_stats(struct net_device *dev,
5212 enum netdev_offload_xstats_type type,
5213 struct rtnl_offload_xstats_request_used *ru,
5214 struct rtnl_hw_stats64 *stats,
5215 struct netlink_ext_ack *extack)
5221 request = netdev_offload_xstats_enabled(dev, type);
5227 err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
5233 ru->request = request;
5240 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
5241 struct rtnl_offload_xstats_request_used *ru)
5243 struct nlattr *nest;
5245 nest = nla_nest_start(skb, attr_id);
5249 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
5250 goto nla_put_failure;
5252 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
5253 goto nla_put_failure;
5255 nla_nest_end(skb, nest);
5259 nla_nest_cancel(skb, nest);
5264 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
5265 struct netlink_ext_ack *extack)
5267 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5268 struct rtnl_offload_xstats_request_used ru_l3;
5269 struct nlattr *nest;
5272 err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
5276 nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5280 if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
5281 IFLA_OFFLOAD_XSTATS_L3_STATS,
5283 goto nla_put_failure;
5285 nla_nest_end(skb, nest);
5289 nla_nest_cancel(skb, nest);
5293 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
5294 int *prividx, u32 off_filter_mask,
5295 struct netlink_ext_ack *extack)
5297 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5298 int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
5299 int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5300 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5301 bool have_data = false;
5304 if (*prividx <= attr_id_cpu_hit &&
5306 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5307 err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5310 } else if (err != -ENODATA) {
5311 *prividx = attr_id_cpu_hit;
5316 if (*prividx <= attr_id_hw_s_info &&
5317 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
5318 *prividx = attr_id_hw_s_info;
5320 err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
5328 if (*prividx <= attr_id_l3_stats &&
5329 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
5330 unsigned int size_l3;
5331 struct nlattr *attr;
5333 *prividx = attr_id_l3_stats;
5335 size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5338 attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
5339 IFLA_OFFLOAD_XSTATS_UNSPEC);
5343 err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
5344 nla_data(attr), extack);
5361 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
5362 enum netdev_offload_xstats_type type)
5364 bool enabled = netdev_offload_xstats_enabled(dev, type);
5366 return nla_total_size(0) +
5367 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
5368 nla_total_size(sizeof(u8)) +
5369 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5370 (enabled ? nla_total_size(sizeof(u8)) : 0) +
5375 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
5377 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5379 return nla_total_size(0) +
5380 /* IFLA_OFFLOAD_XSTATS_L3_STATS */
5381 rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
5385 static int rtnl_offload_xstats_get_size(const struct net_device *dev,
5386 u32 off_filter_mask)
5388 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5389 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5393 if (off_filter_mask &
5394 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5395 size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
5396 nla_size += nla_total_size_64bit(size);
5399 if (off_filter_mask &
5400 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
5401 nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
5403 if (off_filter_mask &
5404 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
5405 size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5406 nla_size += nla_total_size_64bit(size);
5410 nla_size += nla_total_size(0);
5415 struct rtnl_stats_dump_filters {
5416 /* mask[0] filters outer attributes. Then individual nests have their
5417 * filtering mask at the index of the nested attribute.
5419 u32 mask[IFLA_STATS_MAX + 1];
5422 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5423 int type, u32 pid, u32 seq, u32 change,
5425 const struct rtnl_stats_dump_filters *filters,
5426 int *idxattr, int *prividx,
5427 struct netlink_ext_ack *extack)
5429 unsigned int filter_mask = filters->mask[0];
5430 struct if_stats_msg *ifsm;
5431 struct nlmsghdr *nlh;
5432 struct nlattr *attr;
5433 int s_prividx = *prividx;
5438 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5442 ifsm = nlmsg_data(nlh);
5443 ifsm->family = PF_UNSPEC;
5446 ifsm->ifindex = dev->ifindex;
5447 ifsm->filter_mask = filter_mask;
5449 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5450 struct rtnl_link_stats64 *sp;
5452 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5453 sizeof(struct rtnl_link_stats64),
5457 goto nla_put_failure;
5460 sp = nla_data(attr);
5461 dev_get_stats(dev, sp);
5464 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5465 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5467 if (ops && ops->fill_linkxstats) {
5468 *idxattr = IFLA_STATS_LINK_XSTATS;
5469 attr = nla_nest_start_noflag(skb,
5470 IFLA_STATS_LINK_XSTATS);
5473 goto nla_put_failure;
5476 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5477 nla_nest_end(skb, attr);
5479 goto nla_put_failure;
5484 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5486 const struct rtnl_link_ops *ops = NULL;
5487 const struct net_device *master;
5489 master = netdev_master_upper_dev_get(dev);
5491 ops = master->rtnl_link_ops;
5492 if (ops && ops->fill_linkxstats) {
5493 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5494 attr = nla_nest_start_noflag(skb,
5495 IFLA_STATS_LINK_XSTATS_SLAVE);
5498 goto nla_put_failure;
5501 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5502 nla_nest_end(skb, attr);
5504 goto nla_put_failure;
5509 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5511 u32 off_filter_mask;
5513 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5514 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5515 attr = nla_nest_start_noflag(skb,
5516 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5519 goto nla_put_failure;
5522 err = rtnl_offload_xstats_fill(skb, dev, prividx,
5523 off_filter_mask, extack);
5524 if (err == -ENODATA)
5525 nla_nest_cancel(skb, attr);
5527 nla_nest_end(skb, attr);
5529 if (err && err != -ENODATA)
5530 goto nla_put_failure;
5534 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5535 struct rtnl_af_ops *af_ops;
5537 *idxattr = IFLA_STATS_AF_SPEC;
5538 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5541 goto nla_put_failure;
5545 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5546 if (af_ops->fill_stats_af) {
5549 af = nla_nest_start_noflag(skb,
5554 goto nla_put_failure;
5556 err = af_ops->fill_stats_af(skb, dev);
5558 if (err == -ENODATA) {
5559 nla_nest_cancel(skb, af);
5560 } else if (err < 0) {
5562 goto nla_put_failure;
5565 nla_nest_end(skb, af);
5570 nla_nest_end(skb, attr);
5575 nlmsg_end(skb, nlh);
5580 /* not a multi message or no progress mean a real error */
5581 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5582 nlmsg_cancel(skb, nlh);
5584 nlmsg_end(skb, nlh);
5589 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5590 const struct rtnl_stats_dump_filters *filters)
5592 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5593 unsigned int filter_mask = filters->mask[0];
5595 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5596 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5598 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5599 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5600 int attr = IFLA_STATS_LINK_XSTATS;
5602 if (ops && ops->get_linkxstats_size) {
5603 size += nla_total_size(ops->get_linkxstats_size(dev,
5605 /* for IFLA_STATS_LINK_XSTATS */
5606 size += nla_total_size(0);
5610 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5611 struct net_device *_dev = (struct net_device *)dev;
5612 const struct rtnl_link_ops *ops = NULL;
5613 const struct net_device *master;
5615 /* netdev_master_upper_dev_get can't take const */
5616 master = netdev_master_upper_dev_get(_dev);
5618 ops = master->rtnl_link_ops;
5619 if (ops && ops->get_linkxstats_size) {
5620 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5622 size += nla_total_size(ops->get_linkxstats_size(dev,
5624 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5625 size += nla_total_size(0);
5629 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
5630 u32 off_filter_mask;
5632 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5633 size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
5636 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5637 struct rtnl_af_ops *af_ops;
5639 /* for IFLA_STATS_AF_SPEC */
5640 size += nla_total_size(0);
5643 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5644 if (af_ops->get_stats_af_size) {
5645 size += nla_total_size(
5646 af_ops->get_stats_af_size(dev));
5649 size += nla_total_size(0);
5658 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
5660 static const struct nla_policy
5661 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
5662 [IFLA_STATS_LINK_OFFLOAD_XSTATS] =
5663 NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
5666 static const struct nla_policy
5667 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
5668 [IFLA_STATS_GET_FILTERS] =
5669 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
5672 static const struct nla_policy
5673 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
5674 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
5677 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
5678 struct rtnl_stats_dump_filters *filters,
5679 struct netlink_ext_ack *extack)
5681 struct nlattr *tb[IFLA_STATS_MAX + 1];
5685 err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
5686 rtnl_stats_get_policy_filters, extack);
5690 for (at = 1; at <= IFLA_STATS_MAX; at++) {
5692 if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
5693 NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
5696 filters->mask[at] = nla_get_u32(tb[at]);
5703 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
5705 struct rtnl_stats_dump_filters *filters,
5706 struct netlink_ext_ack *extack)
5708 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5712 filters->mask[0] = filter_mask;
5713 for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
5714 filters->mask[i] = -1U;
5716 err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
5717 IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
5721 if (tb[IFLA_STATS_GET_FILTERS]) {
5722 err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
5731 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5732 bool is_dump, struct netlink_ext_ack *extack)
5734 struct if_stats_msg *ifsm;
5736 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5737 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5744 ifsm = nlmsg_data(nlh);
5746 /* only requests using strict checks can pass data to influence
5747 * the dump. The legacy exception is filter_mask.
5749 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5750 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5753 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5754 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5761 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5762 struct netlink_ext_ack *extack)
5764 struct rtnl_stats_dump_filters filters;
5765 struct net *net = sock_net(skb->sk);
5766 struct net_device *dev = NULL;
5767 int idxattr = 0, prividx = 0;
5768 struct if_stats_msg *ifsm;
5769 struct sk_buff *nskb;
5772 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5777 ifsm = nlmsg_data(nlh);
5778 if (ifsm->ifindex > 0)
5779 dev = __dev_get_by_index(net, ifsm->ifindex);
5786 if (!ifsm->filter_mask) {
5787 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
5791 err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
5795 nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
5799 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5800 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5801 0, &filters, &idxattr, &prividx, extack);
5803 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5804 WARN_ON(err == -EMSGSIZE);
5807 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5813 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5815 struct netlink_ext_ack *extack = cb->extack;
5816 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5817 struct rtnl_stats_dump_filters filters;
5818 struct net *net = sock_net(skb->sk);
5819 unsigned int flags = NLM_F_MULTI;
5820 struct if_stats_msg *ifsm;
5821 struct hlist_head *head;
5822 struct net_device *dev;
5826 s_idx = cb->args[1];
5827 s_idxattr = cb->args[2];
5828 s_prividx = cb->args[3];
5830 cb->seq = net->dev_base_seq;
5832 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5836 ifsm = nlmsg_data(cb->nlh);
5837 if (!ifsm->filter_mask) {
5838 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5842 err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
5847 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5849 head = &net->dev_index_head[h];
5850 hlist_for_each_entry(dev, head, index_hlist) {
5853 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5854 NETLINK_CB(cb->skb).portid,
5855 cb->nlh->nlmsg_seq, 0,
5857 &s_idxattr, &s_prividx,
5859 /* If we ran out of room on the first message,
5862 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5868 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5874 cb->args[3] = s_prividx;
5875 cb->args[2] = s_idxattr;
5882 void rtnl_offload_xstats_notify(struct net_device *dev)
5884 struct rtnl_stats_dump_filters response_filters = {};
5885 struct net *net = dev_net(dev);
5886 int idxattr = 0, prividx = 0;
5887 struct sk_buff *skb;
5892 response_filters.mask[0] |=
5893 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
5894 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
5895 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5897 skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
5902 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
5903 &response_filters, &idxattr, &prividx, NULL);
5909 rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
5913 rtnl_set_sk_err(net, RTNLGRP_STATS, err);
5915 EXPORT_SYMBOL(rtnl_offload_xstats_notify);
5917 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
5918 struct netlink_ext_ack *extack)
5920 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5921 struct rtnl_stats_dump_filters response_filters = {};
5922 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5923 struct net *net = sock_net(skb->sk);
5924 struct net_device *dev = NULL;
5925 struct if_stats_msg *ifsm;
5926 bool notify = false;
5929 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5934 ifsm = nlmsg_data(nlh);
5935 if (ifsm->family != AF_UNSPEC) {
5936 NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
5940 if (ifsm->ifindex > 0)
5941 dev = __dev_get_by_index(net, ifsm->ifindex);
5948 if (ifsm->filter_mask) {
5949 NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
5953 err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
5954 ifla_stats_set_policy, extack);
5958 if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
5959 u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
5962 err = netdev_offload_xstats_enable(dev, t_l3, extack);
5964 err = netdev_offload_xstats_disable(dev, t_l3);
5968 else if (err != -EALREADY)
5971 response_filters.mask[0] |=
5972 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
5973 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
5974 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5978 rtnl_offload_xstats_notify(dev);
5983 /* Process one rtnetlink message. */
5985 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5986 struct netlink_ext_ack *extack)
5988 struct net *net = sock_net(skb->sk);
5989 struct rtnl_link *link;
5990 enum rtnl_kinds kind;
5991 struct module *owner;
5992 int err = -EOPNOTSUPP;
5993 rtnl_doit_func doit;
5998 type = nlh->nlmsg_type;
6004 /* All the messages must have at least 1 byte length */
6005 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
6008 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
6009 kind = rtnl_msgtype_kind(type);
6011 if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
6015 if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
6017 rtnl_dumpit_func dumpit;
6018 u32 min_dump_alloc = 0;
6020 link = rtnl_get_link(family, type);
6021 if (!link || !link->dumpit) {
6023 link = rtnl_get_link(family, type);
6024 if (!link || !link->dumpit)
6027 owner = link->owner;
6028 dumpit = link->dumpit;
6030 if (type == RTM_GETLINK - RTM_BASE)
6031 min_dump_alloc = rtnl_calcit(skb, nlh);
6034 /* need to do this before rcu_read_unlock() */
6035 if (!try_module_get(owner))
6036 err = -EPROTONOSUPPORT;
6042 struct netlink_dump_control c = {
6044 .min_dump_alloc = min_dump_alloc,
6047 err = netlink_dump_start(rtnl, skb, nlh, &c);
6048 /* netlink_dump_start() will keep a reference on
6049 * module if dump is still in progress.
6056 link = rtnl_get_link(family, type);
6057 if (!link || !link->doit) {
6059 link = rtnl_get_link(PF_UNSPEC, type);
6060 if (!link || !link->doit)
6064 owner = link->owner;
6065 if (!try_module_get(owner)) {
6066 err = -EPROTONOSUPPORT;
6070 flags = link->flags;
6071 if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6072 !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6073 NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6078 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6082 err = doit(skb, nlh, extack);
6089 link = rtnl_get_link(family, type);
6090 if (link && link->doit)
6091 err = link->doit(skb, nlh, extack);
6107 static void rtnetlink_rcv(struct sk_buff *skb)
6109 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
6112 static int rtnetlink_bind(struct net *net, int group)
6115 case RTNLGRP_IPV4_MROUTE_R:
6116 case RTNLGRP_IPV6_MROUTE_R:
6117 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
6124 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
6126 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6130 case NETDEV_CHANGEMTU:
6131 case NETDEV_CHANGEADDR:
6132 case NETDEV_CHANGENAME:
6133 case NETDEV_FEAT_CHANGE:
6134 case NETDEV_BONDING_FAILOVER:
6135 case NETDEV_POST_TYPE_CHANGE:
6136 case NETDEV_NOTIFY_PEERS:
6137 case NETDEV_CHANGEUPPER:
6138 case NETDEV_RESEND_IGMP:
6139 case NETDEV_CHANGEINFODATA:
6140 case NETDEV_CHANGELOWERSTATE:
6141 case NETDEV_CHANGE_TX_QUEUE_LEN:
6142 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6143 GFP_KERNEL, NULL, 0);
6151 static struct notifier_block rtnetlink_dev_notifier = {
6152 .notifier_call = rtnetlink_event,
6156 static int __net_init rtnetlink_net_init(struct net *net)
6159 struct netlink_kernel_cfg cfg = {
6160 .groups = RTNLGRP_MAX,
6161 .input = rtnetlink_rcv,
6162 .cb_mutex = &rtnl_mutex,
6163 .flags = NL_CFG_F_NONROOT_RECV,
6164 .bind = rtnetlink_bind,
6167 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
6174 static void __net_exit rtnetlink_net_exit(struct net *net)
6176 netlink_kernel_release(net->rtnl);
6180 static struct pernet_operations rtnetlink_net_ops = {
6181 .init = rtnetlink_net_init,
6182 .exit = rtnetlink_net_exit,
6185 void __init rtnetlink_init(void)
6187 if (register_pernet_subsys(&rtnetlink_net_ops))
6188 panic("rtnetlink_init: cannot initialize rtnetlink\n");
6190 register_netdevice_notifier(&rtnetlink_dev_notifier);
6192 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6193 rtnl_dump_ifinfo, 0);
6194 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6195 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6196 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6198 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6199 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6200 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6202 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
6203 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
6205 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6206 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
6207 RTNL_FLAG_BULK_DEL_SUPPORTED);
6208 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6210 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6211 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6212 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6214 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6216 rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);