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>
56 #include <net/devlink.h>
57 #if IS_ENABLED(CONFIG_IPV6)
58 #include <net/addrconf.h>
63 #define RTNL_MAX_TYPE 50
64 #define RTNL_SLAVE_MAX_TYPE 44
68 rtnl_dumpit_func dumpit;
74 static DEFINE_MUTEX(rtnl_mutex);
78 mutex_lock(&rtnl_mutex);
80 EXPORT_SYMBOL(rtnl_lock);
82 int rtnl_lock_killable(void)
84 return mutex_lock_killable(&rtnl_mutex);
86 EXPORT_SYMBOL(rtnl_lock_killable);
88 static struct sk_buff *defer_kfree_skb_list;
89 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
92 tail->next = defer_kfree_skb_list;
93 defer_kfree_skb_list = head;
96 EXPORT_SYMBOL(rtnl_kfree_skbs);
98 void __rtnl_unlock(void)
100 struct sk_buff *head = defer_kfree_skb_list;
102 defer_kfree_skb_list = NULL;
104 /* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
105 * is used. In some places, e.g. in cfg80211, we have code that will do
112 * and because netdev_run_todo() acquires the RTNL for items on the list
113 * we could cause a situation such as this:
116 * unregister_netdevice()
124 * // list not empty now
125 * // because of thread 2
127 * while (!list_empty(...))
132 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
133 * it's not used in cases where something is added to do the list.
135 WARN_ON(!list_empty(&net_todo_list));
137 mutex_unlock(&rtnl_mutex);
140 struct sk_buff *next = head->next;
148 void rtnl_unlock(void)
150 /* This fellow will unlock it for us. */
153 EXPORT_SYMBOL(rtnl_unlock);
155 int rtnl_trylock(void)
157 return mutex_trylock(&rtnl_mutex);
159 EXPORT_SYMBOL(rtnl_trylock);
161 int rtnl_is_locked(void)
163 return mutex_is_locked(&rtnl_mutex);
165 EXPORT_SYMBOL(rtnl_is_locked);
167 bool refcount_dec_and_rtnl_lock(refcount_t *r)
169 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
171 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
173 #ifdef CONFIG_PROVE_LOCKING
174 bool lockdep_rtnl_is_held(void)
176 return lockdep_is_held(&rtnl_mutex);
178 EXPORT_SYMBOL(lockdep_rtnl_is_held);
179 #endif /* #ifdef CONFIG_PROVE_LOCKING */
181 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
183 static inline int rtm_msgindex(int msgtype)
185 int msgindex = msgtype - RTM_BASE;
188 * msgindex < 0 implies someone tried to register a netlink
189 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
190 * the message type has not been added to linux/rtnetlink.h
192 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
197 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
199 struct rtnl_link __rcu **tab;
201 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
202 protocol = PF_UNSPEC;
204 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
206 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
208 return rcu_dereference_rtnl(tab[msgtype]);
211 static int rtnl_register_internal(struct module *owner,
212 int protocol, int msgtype,
213 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
216 struct rtnl_link *link, *old;
217 struct rtnl_link __rcu **tab;
221 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
222 msgindex = rtm_msgindex(msgtype);
225 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
227 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
231 /* ensures we see the 0 stores */
232 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
235 old = rtnl_dereference(tab[msgindex]);
237 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
241 link = kzalloc(sizeof(*link), GFP_KERNEL);
246 WARN_ON(link->owner && link->owner != owner);
249 WARN_ON(doit && link->doit && link->doit != doit);
252 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
254 link->dumpit = dumpit;
256 WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
257 (flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
258 link->flags |= flags;
260 /* publish protocol:msgtype */
261 rcu_assign_pointer(tab[msgindex], link);
271 * rtnl_register_module - Register a rtnetlink message type
273 * @owner: module registering the hook (THIS_MODULE)
274 * @protocol: Protocol family or PF_UNSPEC
275 * @msgtype: rtnetlink message type
276 * @doit: Function pointer called for each request message
277 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
278 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
280 * Like rtnl_register, but for use by removable modules.
282 int rtnl_register_module(struct module *owner,
283 int protocol, int msgtype,
284 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
287 return rtnl_register_internal(owner, protocol, msgtype,
288 doit, dumpit, flags);
290 EXPORT_SYMBOL_GPL(rtnl_register_module);
293 * rtnl_register - Register a rtnetlink message type
294 * @protocol: Protocol family or PF_UNSPEC
295 * @msgtype: rtnetlink message type
296 * @doit: Function pointer called for each request message
297 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
298 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
300 * Registers the specified function pointers (at least one of them has
301 * to be non-NULL) to be called whenever a request message for the
302 * specified protocol family and message type is received.
304 * The special protocol family PF_UNSPEC may be used to define fallback
305 * function pointers for the case when no entry for the specific protocol
308 void rtnl_register(int protocol, int msgtype,
309 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
314 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
317 pr_err("Unable to register rtnetlink message handler, "
318 "protocol = %d, message type = %d\n", protocol, msgtype);
322 * rtnl_unregister - Unregister a rtnetlink message type
323 * @protocol: Protocol family or PF_UNSPEC
324 * @msgtype: rtnetlink message type
326 * Returns 0 on success or a negative error code.
328 int rtnl_unregister(int protocol, int msgtype)
330 struct rtnl_link __rcu **tab;
331 struct rtnl_link *link;
334 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
335 msgindex = rtm_msgindex(msgtype);
338 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
344 link = rtnl_dereference(tab[msgindex]);
345 RCU_INIT_POINTER(tab[msgindex], NULL);
348 kfree_rcu(link, rcu);
352 EXPORT_SYMBOL_GPL(rtnl_unregister);
355 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
356 * @protocol : Protocol family or PF_UNSPEC
358 * Identical to calling rtnl_unregster() for all registered message types
359 * of a certain protocol family.
361 void rtnl_unregister_all(int protocol)
363 struct rtnl_link __rcu **tab;
364 struct rtnl_link *link;
367 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
370 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
375 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
376 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
377 link = rtnl_dereference(tab[msgindex]);
381 RCU_INIT_POINTER(tab[msgindex], NULL);
382 kfree_rcu(link, rcu);
390 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
392 static LIST_HEAD(link_ops);
394 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
396 const struct rtnl_link_ops *ops;
398 list_for_each_entry(ops, &link_ops, list) {
399 if (!strcmp(ops->kind, kind))
406 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
407 * @ops: struct rtnl_link_ops * to register
409 * The caller must hold the rtnl_mutex. This function should be used
410 * by drivers that create devices during module initialization. It
411 * must be called before registering the devices.
413 * Returns 0 on success or a negative error code.
415 int __rtnl_link_register(struct rtnl_link_ops *ops)
417 if (rtnl_link_ops_get(ops->kind))
420 /* The check for alloc/setup is here because if ops
421 * does not have that filled up, it is not possible
422 * to use the ops for creating device. So do not
423 * fill up dellink as well. That disables rtnl_dellink.
425 if ((ops->alloc || ops->setup) && !ops->dellink)
426 ops->dellink = unregister_netdevice_queue;
428 list_add_tail(&ops->list, &link_ops);
431 EXPORT_SYMBOL_GPL(__rtnl_link_register);
434 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
435 * @ops: struct rtnl_link_ops * to register
437 * Returns 0 on success or a negative error code.
439 int rtnl_link_register(struct rtnl_link_ops *ops)
443 /* Sanity-check max sizes to avoid stack buffer overflow. */
444 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
445 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
449 err = __rtnl_link_register(ops);
453 EXPORT_SYMBOL_GPL(rtnl_link_register);
455 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
457 struct net_device *dev;
458 LIST_HEAD(list_kill);
460 for_each_netdev(net, dev) {
461 if (dev->rtnl_link_ops == ops)
462 ops->dellink(dev, &list_kill);
464 unregister_netdevice_many(&list_kill);
468 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
469 * @ops: struct rtnl_link_ops * to unregister
471 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
472 * integrity (hold pernet_ops_rwsem for writing to close the race
473 * with setup_net() and cleanup_net()).
475 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
480 __rtnl_kill_links(net, ops);
482 list_del(&ops->list);
484 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
486 /* Return with the rtnl_lock held when there are no network
487 * devices unregistering in any network namespace.
489 static void rtnl_lock_unregistering_all(void)
493 DEFINE_WAIT_FUNC(wait, woken_wake_function);
495 add_wait_queue(&netdev_unregistering_wq, &wait);
497 unregistering = false;
499 /* We held write locked pernet_ops_rwsem, and parallel
500 * setup_net() and cleanup_net() are not possible.
503 if (atomic_read(&net->dev_unreg_count) > 0) {
504 unregistering = true;
512 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
514 remove_wait_queue(&netdev_unregistering_wq, &wait);
518 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
519 * @ops: struct rtnl_link_ops * to unregister
521 void rtnl_link_unregister(struct rtnl_link_ops *ops)
523 /* Close the race with setup_net() and cleanup_net() */
524 down_write(&pernet_ops_rwsem);
525 rtnl_lock_unregistering_all();
526 __rtnl_link_unregister(ops);
528 up_write(&pernet_ops_rwsem);
530 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
532 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
534 struct net_device *master_dev;
535 const struct rtnl_link_ops *ops;
540 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
544 ops = master_dev->rtnl_link_ops;
545 if (!ops || !ops->get_slave_size)
547 /* IFLA_INFO_SLAVE_DATA + nested data */
548 size = nla_total_size(sizeof(struct nlattr)) +
549 ops->get_slave_size(master_dev, dev);
556 static size_t rtnl_link_get_size(const struct net_device *dev)
558 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
564 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
565 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
568 /* IFLA_INFO_DATA + nested data */
569 size += nla_total_size(sizeof(struct nlattr)) +
572 if (ops->get_xstats_size)
573 /* IFLA_INFO_XSTATS */
574 size += nla_total_size(ops->get_xstats_size(dev));
576 size += rtnl_link_get_slave_info_data_size(dev);
581 static LIST_HEAD(rtnl_af_ops);
583 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
585 const struct rtnl_af_ops *ops;
589 list_for_each_entry(ops, &rtnl_af_ops, list) {
590 if (ops->family == family)
598 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
599 * @ops: struct rtnl_af_ops * to register
601 * Returns 0 on success or a negative error code.
603 void rtnl_af_register(struct rtnl_af_ops *ops)
606 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
609 EXPORT_SYMBOL_GPL(rtnl_af_register);
612 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
613 * @ops: struct rtnl_af_ops * to unregister
615 void rtnl_af_unregister(struct rtnl_af_ops *ops)
618 list_del_rcu(&ops->list);
623 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
625 static size_t rtnl_link_get_af_size(const struct net_device *dev,
628 struct rtnl_af_ops *af_ops;
632 size = nla_total_size(sizeof(struct nlattr));
635 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
636 if (af_ops->get_link_af_size) {
637 /* AF_* + nested data */
638 size += nla_total_size(sizeof(struct nlattr)) +
639 af_ops->get_link_af_size(dev, ext_filter_mask);
647 static bool rtnl_have_link_slave_info(const struct net_device *dev)
649 struct net_device *master_dev;
654 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
655 if (master_dev && master_dev->rtnl_link_ops)
661 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
662 const struct net_device *dev)
664 struct net_device *master_dev;
665 const struct rtnl_link_ops *ops;
666 struct nlattr *slave_data;
669 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
672 ops = master_dev->rtnl_link_ops;
675 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
677 if (ops->fill_slave_info) {
678 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
681 err = ops->fill_slave_info(skb, master_dev, dev);
683 goto err_cancel_slave_data;
684 nla_nest_end(skb, slave_data);
688 err_cancel_slave_data:
689 nla_nest_cancel(skb, slave_data);
693 static int rtnl_link_info_fill(struct sk_buff *skb,
694 const struct net_device *dev)
696 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
702 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
704 if (ops->fill_xstats) {
705 err = ops->fill_xstats(skb, dev);
709 if (ops->fill_info) {
710 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
713 err = ops->fill_info(skb, dev);
715 goto err_cancel_data;
716 nla_nest_end(skb, data);
721 nla_nest_cancel(skb, data);
725 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
727 struct nlattr *linkinfo;
730 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
731 if (linkinfo == NULL)
734 err = rtnl_link_info_fill(skb, dev);
736 goto err_cancel_link;
738 err = rtnl_link_slave_info_fill(skb, dev);
740 goto err_cancel_link;
742 nla_nest_end(skb, linkinfo);
746 nla_nest_cancel(skb, linkinfo);
751 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
753 struct sock *rtnl = net->rtnl;
755 return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
758 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
760 struct sock *rtnl = net->rtnl;
762 return nlmsg_unicast(rtnl, skb, pid);
764 EXPORT_SYMBOL(rtnl_unicast);
766 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
767 const struct nlmsghdr *nlh, gfp_t flags)
769 struct sock *rtnl = net->rtnl;
771 nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
773 EXPORT_SYMBOL(rtnl_notify);
775 void rtnl_set_sk_err(struct net *net, u32 group, int error)
777 struct sock *rtnl = net->rtnl;
779 netlink_set_err(rtnl, 0, group, error);
781 EXPORT_SYMBOL(rtnl_set_sk_err);
783 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
788 /* nothing is dumped for dst_default_metrics, so just skip the loop */
789 if (metrics == dst_default_metrics.metrics)
792 mx = nla_nest_start_noflag(skb, RTA_METRICS);
796 for (i = 0; i < RTAX_MAX; i++) {
798 if (i == RTAX_CC_ALGO - 1) {
799 char tmp[TCP_CA_NAME_MAX], *name;
801 name = tcp_ca_get_name_by_key(metrics[i], tmp);
804 if (nla_put_string(skb, i + 1, name))
805 goto nla_put_failure;
806 } else if (i == RTAX_FEATURES - 1) {
807 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
811 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
812 if (nla_put_u32(skb, i + 1, user_features))
813 goto nla_put_failure;
815 if (nla_put_u32(skb, i + 1, metrics[i]))
816 goto nla_put_failure;
823 nla_nest_cancel(skb, mx);
827 return nla_nest_end(skb, mx);
830 nla_nest_cancel(skb, mx);
833 EXPORT_SYMBOL(rtnetlink_put_metrics);
835 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
836 long expires, u32 error)
838 struct rta_cacheinfo ci = {
844 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
845 ci.rta_used = dst->__use;
846 ci.rta_clntref = rcuref_read(&dst->__rcuref);
851 clock = jiffies_to_clock_t(abs(expires));
852 clock = min_t(unsigned long, clock, INT_MAX);
853 ci.rta_expires = (expires > 0) ? clock : -clock;
855 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
857 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
859 static void set_operstate(struct net_device *dev, unsigned char transition)
861 unsigned char operstate = dev->operstate;
863 switch (transition) {
865 if ((operstate == IF_OPER_DORMANT ||
866 operstate == IF_OPER_TESTING ||
867 operstate == IF_OPER_UNKNOWN) &&
868 !netif_dormant(dev) && !netif_testing(dev))
869 operstate = IF_OPER_UP;
872 case IF_OPER_TESTING:
873 if (netif_oper_up(dev))
874 operstate = IF_OPER_TESTING;
877 case IF_OPER_DORMANT:
878 if (netif_oper_up(dev))
879 operstate = IF_OPER_DORMANT;
883 if (dev->operstate != operstate) {
884 write_lock(&dev_base_lock);
885 dev->operstate = operstate;
886 write_unlock(&dev_base_lock);
887 netdev_state_change(dev);
891 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
893 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
894 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
897 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
898 const struct ifinfomsg *ifm)
900 unsigned int flags = ifm->ifi_flags;
902 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
904 flags = (flags & ifm->ifi_change) |
905 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
910 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
911 const struct rtnl_link_stats64 *b)
913 a->rx_packets = b->rx_packets;
914 a->tx_packets = b->tx_packets;
915 a->rx_bytes = b->rx_bytes;
916 a->tx_bytes = b->tx_bytes;
917 a->rx_errors = b->rx_errors;
918 a->tx_errors = b->tx_errors;
919 a->rx_dropped = b->rx_dropped;
920 a->tx_dropped = b->tx_dropped;
922 a->multicast = b->multicast;
923 a->collisions = b->collisions;
925 a->rx_length_errors = b->rx_length_errors;
926 a->rx_over_errors = b->rx_over_errors;
927 a->rx_crc_errors = b->rx_crc_errors;
928 a->rx_frame_errors = b->rx_frame_errors;
929 a->rx_fifo_errors = b->rx_fifo_errors;
930 a->rx_missed_errors = b->rx_missed_errors;
932 a->tx_aborted_errors = b->tx_aborted_errors;
933 a->tx_carrier_errors = b->tx_carrier_errors;
934 a->tx_fifo_errors = b->tx_fifo_errors;
935 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
936 a->tx_window_errors = b->tx_window_errors;
938 a->rx_compressed = b->rx_compressed;
939 a->tx_compressed = b->tx_compressed;
941 a->rx_nohandler = b->rx_nohandler;
945 static inline int rtnl_vfinfo_size(const struct net_device *dev,
948 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
949 int num_vfs = dev_num_vf(dev->dev.parent);
950 size_t size = nla_total_size(0);
953 nla_total_size(sizeof(struct ifla_vf_mac)) +
954 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
955 nla_total_size(sizeof(struct ifla_vf_vlan)) +
956 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
957 nla_total_size(MAX_VLAN_LIST_LEN *
958 sizeof(struct ifla_vf_vlan_info)) +
959 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
960 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
961 nla_total_size(sizeof(struct ifla_vf_rate)) +
962 nla_total_size(sizeof(struct ifla_vf_link_state)) +
963 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
964 nla_total_size(sizeof(struct ifla_vf_trust)));
965 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
967 (nla_total_size(0) + /* nest IFLA_VF_STATS */
968 /* IFLA_VF_STATS_RX_PACKETS */
969 nla_total_size_64bit(sizeof(__u64)) +
970 /* IFLA_VF_STATS_TX_PACKETS */
971 nla_total_size_64bit(sizeof(__u64)) +
972 /* IFLA_VF_STATS_RX_BYTES */
973 nla_total_size_64bit(sizeof(__u64)) +
974 /* IFLA_VF_STATS_TX_BYTES */
975 nla_total_size_64bit(sizeof(__u64)) +
976 /* IFLA_VF_STATS_BROADCAST */
977 nla_total_size_64bit(sizeof(__u64)) +
978 /* IFLA_VF_STATS_MULTICAST */
979 nla_total_size_64bit(sizeof(__u64)) +
980 /* IFLA_VF_STATS_RX_DROPPED */
981 nla_total_size_64bit(sizeof(__u64)) +
982 /* IFLA_VF_STATS_TX_DROPPED */
983 nla_total_size_64bit(sizeof(__u64)));
990 static size_t rtnl_port_size(const struct net_device *dev,
993 size_t port_size = nla_total_size(4) /* PORT_VF */
994 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
995 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
996 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
997 + nla_total_size(1) /* PROT_VDP_REQUEST */
998 + nla_total_size(2); /* PORT_VDP_RESPONSE */
999 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
1000 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
1002 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
1005 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1006 !(ext_filter_mask & RTEXT_FILTER_VF))
1008 if (dev_num_vf(dev->dev.parent))
1009 return port_self_size + vf_ports_size +
1010 vf_port_size * dev_num_vf(dev->dev.parent);
1012 return port_self_size;
1015 static size_t rtnl_xdp_size(void)
1017 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
1018 nla_total_size(1) + /* XDP_ATTACHED */
1019 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
1020 nla_total_size(4); /* XDP_<mode>_PROG_ID */
1025 static size_t rtnl_prop_list_size(const struct net_device *dev)
1027 struct netdev_name_node *name_node;
1030 if (list_empty(&dev->name_node->list))
1032 size = nla_total_size(0);
1033 list_for_each_entry(name_node, &dev->name_node->list, list)
1034 size += nla_total_size(ALTIFNAMSIZ);
1038 static size_t rtnl_proto_down_size(const struct net_device *dev)
1040 size_t size = nla_total_size(1);
1042 if (dev->proto_down_reason)
1043 size += nla_total_size(0) + nla_total_size(4);
1048 static size_t rtnl_devlink_port_size(const struct net_device *dev)
1050 size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
1052 if (dev->devlink_port)
1053 size += devlink_nl_port_handle_size(dev->devlink_port);
1058 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1059 u32 ext_filter_mask)
1061 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1062 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1063 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1064 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1065 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1066 + nla_total_size(sizeof(struct rtnl_link_stats))
1067 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1068 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1069 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1070 + nla_total_size(4) /* IFLA_TXQLEN */
1071 + nla_total_size(4) /* IFLA_WEIGHT */
1072 + nla_total_size(4) /* IFLA_MTU */
1073 + nla_total_size(4) /* IFLA_LINK */
1074 + nla_total_size(4) /* IFLA_MASTER */
1075 + nla_total_size(1) /* IFLA_CARRIER */
1076 + nla_total_size(4) /* IFLA_PROMISCUITY */
1077 + nla_total_size(4) /* IFLA_ALLMULTI */
1078 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1079 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1080 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1081 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1082 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1083 + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
1084 + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
1085 + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
1086 + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1087 + nla_total_size(1) /* IFLA_OPERSTATE */
1088 + nla_total_size(1) /* IFLA_LINKMODE */
1089 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1090 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1091 + nla_total_size(4) /* IFLA_GROUP */
1092 + nla_total_size(ext_filter_mask
1093 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1094 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1095 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1096 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1097 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1098 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1099 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1100 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1101 + rtnl_xdp_size() /* IFLA_XDP */
1102 + nla_total_size(4) /* IFLA_EVENT */
1103 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1104 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1105 + rtnl_proto_down_size(dev) /* proto down */
1106 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1107 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1108 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1109 + nla_total_size(4) /* IFLA_MIN_MTU */
1110 + nla_total_size(4) /* IFLA_MAX_MTU */
1111 + rtnl_prop_list_size(dev)
1112 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1113 + rtnl_devlink_port_size(dev)
1117 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1119 struct nlattr *vf_ports;
1120 struct nlattr *vf_port;
1124 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1128 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1129 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1131 goto nla_put_failure;
1132 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1133 goto nla_put_failure;
1134 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1135 if (err == -EMSGSIZE)
1136 goto nla_put_failure;
1138 nla_nest_cancel(skb, vf_port);
1141 nla_nest_end(skb, vf_port);
1144 nla_nest_end(skb, vf_ports);
1149 nla_nest_cancel(skb, vf_ports);
1153 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1155 struct nlattr *port_self;
1158 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1162 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1164 nla_nest_cancel(skb, port_self);
1165 return (err == -EMSGSIZE) ? err : 0;
1168 nla_nest_end(skb, port_self);
1173 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1174 u32 ext_filter_mask)
1178 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1179 !(ext_filter_mask & RTEXT_FILTER_VF))
1182 err = rtnl_port_self_fill(skb, dev);
1186 if (dev_num_vf(dev->dev.parent)) {
1187 err = rtnl_vf_ports_fill(skb, dev);
1195 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1198 struct netdev_phys_item_id ppid;
1200 err = dev_get_phys_port_id(dev, &ppid);
1202 if (err == -EOPNOTSUPP)
1207 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1213 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1215 char name[IFNAMSIZ];
1218 err = dev_get_phys_port_name(dev, name, sizeof(name));
1220 if (err == -EOPNOTSUPP)
1225 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1231 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1233 struct netdev_phys_item_id ppid = { };
1236 err = dev_get_port_parent_id(dev, &ppid, false);
1238 if (err == -EOPNOTSUPP)
1243 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1249 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1250 struct net_device *dev)
1252 struct rtnl_link_stats64 *sp;
1253 struct nlattr *attr;
1255 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1256 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1260 sp = nla_data(attr);
1261 dev_get_stats(dev, sp);
1263 attr = nla_reserve(skb, IFLA_STATS,
1264 sizeof(struct rtnl_link_stats));
1268 copy_rtnl_link_stats(nla_data(attr), sp);
1273 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1274 struct net_device *dev,
1276 u32 ext_filter_mask)
1278 struct ifla_vf_rss_query_en vf_rss_query_en;
1279 struct nlattr *vf, *vfstats, *vfvlanlist;
1280 struct ifla_vf_link_state vf_linkstate;
1281 struct ifla_vf_vlan_info vf_vlan_info;
1282 struct ifla_vf_spoofchk vf_spoofchk;
1283 struct ifla_vf_tx_rate vf_tx_rate;
1284 struct ifla_vf_stats vf_stats;
1285 struct ifla_vf_trust vf_trust;
1286 struct ifla_vf_vlan vf_vlan;
1287 struct ifla_vf_rate vf_rate;
1288 struct ifla_vf_mac vf_mac;
1289 struct ifla_vf_broadcast vf_broadcast;
1290 struct ifla_vf_info ivi;
1291 struct ifla_vf_guid node_guid;
1292 struct ifla_vf_guid port_guid;
1294 memset(&ivi, 0, sizeof(ivi));
1296 /* Not all SR-IOV capable drivers support the
1297 * spoofcheck and "RSS query enable" query. Preset to
1298 * -1 so the user space tool can detect that the driver
1299 * didn't report anything.
1302 ivi.rss_query_en = -1;
1304 /* The default value for VF link state is "auto"
1305 * IFLA_VF_LINK_STATE_AUTO which equals zero
1308 /* VLAN Protocol by default is 802.1Q */
1309 ivi.vlan_proto = htons(ETH_P_8021Q);
1310 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1313 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1314 memset(&node_guid, 0, sizeof(node_guid));
1315 memset(&port_guid, 0, sizeof(port_guid));
1324 vf_rss_query_en.vf =
1327 port_guid.vf = ivi.vf;
1329 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1330 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1331 vf_vlan.vlan = ivi.vlan;
1332 vf_vlan.qos = ivi.qos;
1333 vf_vlan_info.vlan = ivi.vlan;
1334 vf_vlan_info.qos = ivi.qos;
1335 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1336 vf_tx_rate.rate = ivi.max_tx_rate;
1337 vf_rate.min_tx_rate = ivi.min_tx_rate;
1338 vf_rate.max_tx_rate = ivi.max_tx_rate;
1339 vf_spoofchk.setting = ivi.spoofchk;
1340 vf_linkstate.link_state = ivi.linkstate;
1341 vf_rss_query_en.setting = ivi.rss_query_en;
1342 vf_trust.setting = ivi.trusted;
1343 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1346 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1347 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1348 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1349 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1351 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1353 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1355 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1357 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1358 sizeof(vf_rss_query_en),
1359 &vf_rss_query_en) ||
1360 nla_put(skb, IFLA_VF_TRUST,
1361 sizeof(vf_trust), &vf_trust))
1362 goto nla_put_vf_failure;
1364 if (dev->netdev_ops->ndo_get_vf_guid &&
1365 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1367 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1369 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1371 goto nla_put_vf_failure;
1373 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1375 goto nla_put_vf_failure;
1376 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1378 nla_nest_cancel(skb, vfvlanlist);
1379 goto nla_put_vf_failure;
1381 nla_nest_end(skb, vfvlanlist);
1382 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
1383 memset(&vf_stats, 0, sizeof(vf_stats));
1384 if (dev->netdev_ops->ndo_get_vf_stats)
1385 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1387 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1389 goto nla_put_vf_failure;
1390 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1391 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1392 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1393 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1394 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1395 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1396 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1397 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1398 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1399 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1400 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1401 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1402 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1403 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1404 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1405 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1406 nla_nest_cancel(skb, vfstats);
1407 goto nla_put_vf_failure;
1409 nla_nest_end(skb, vfstats);
1411 nla_nest_end(skb, vf);
1415 nla_nest_cancel(skb, vf);
1419 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1420 struct net_device *dev,
1421 u32 ext_filter_mask)
1423 struct nlattr *vfinfo;
1426 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1429 num_vfs = dev_num_vf(dev->dev.parent);
1430 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1433 if (!dev->netdev_ops->ndo_get_vf_config)
1436 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1440 for (i = 0; i < num_vfs; i++) {
1441 if (rtnl_fill_vfinfo(skb, dev, i, ext_filter_mask)) {
1442 nla_nest_cancel(skb, vfinfo);
1447 nla_nest_end(skb, vfinfo);
1451 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1453 struct rtnl_link_ifmap map;
1455 memset(&map, 0, sizeof(map));
1456 map.mem_start = dev->mem_start;
1457 map.mem_end = dev->mem_end;
1458 map.base_addr = dev->base_addr;
1461 map.port = dev->if_port;
1463 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1469 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1471 const struct bpf_prog *generic_xdp_prog;
1475 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1476 if (!generic_xdp_prog)
1478 return generic_xdp_prog->aux->id;
1481 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1483 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1486 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1488 return dev_xdp_prog_id(dev, XDP_MODE_HW);
1491 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1492 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1493 u32 (*get_prog_id)(struct net_device *dev))
1498 curr_id = get_prog_id(dev);
1503 err = nla_put_u32(skb, attr, curr_id);
1507 if (*mode != XDP_ATTACHED_NONE)
1508 *mode = XDP_ATTACHED_MULTI;
1515 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1522 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1527 mode = XDP_ATTACHED_NONE;
1528 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1529 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1532 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1533 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1536 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1537 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1541 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1545 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1546 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1551 nla_nest_end(skb, xdp);
1555 nla_nest_cancel(skb, xdp);
1559 static u32 rtnl_get_event(unsigned long event)
1561 u32 rtnl_event_type = IFLA_EVENT_NONE;
1565 rtnl_event_type = IFLA_EVENT_REBOOT;
1567 case NETDEV_FEAT_CHANGE:
1568 rtnl_event_type = IFLA_EVENT_FEATURES;
1570 case NETDEV_BONDING_FAILOVER:
1571 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1573 case NETDEV_NOTIFY_PEERS:
1574 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1576 case NETDEV_RESEND_IGMP:
1577 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1579 case NETDEV_CHANGEINFODATA:
1580 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1586 return rtnl_event_type;
1589 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1591 const struct net_device *upper_dev;
1596 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1598 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1604 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1607 int ifindex = dev_get_iflink(dev);
1609 if (force || dev->ifindex != ifindex)
1610 return nla_put_u32(skb, IFLA_LINK, ifindex);
1615 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1616 struct net_device *dev)
1621 ret = dev_get_alias(dev, buf, sizeof(buf));
1622 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1625 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1626 const struct net_device *dev,
1627 struct net *src_net, gfp_t gfp)
1629 bool put_iflink = false;
1631 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1632 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1634 if (!net_eq(dev_net(dev), link_net)) {
1635 int id = peernet2id_alloc(src_net, link_net, gfp);
1637 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1644 return nla_put_iflink(skb, dev, put_iflink);
1647 static int rtnl_fill_link_af(struct sk_buff *skb,
1648 const struct net_device *dev,
1649 u32 ext_filter_mask)
1651 const struct rtnl_af_ops *af_ops;
1652 struct nlattr *af_spec;
1654 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1658 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1662 if (!af_ops->fill_link_af)
1665 af = nla_nest_start_noflag(skb, af_ops->family);
1669 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1671 * Caller may return ENODATA to indicate that there
1672 * was no data to be dumped. This is not an error, it
1673 * means we should trim the attribute header and
1676 if (err == -ENODATA)
1677 nla_nest_cancel(skb, af);
1681 nla_nest_end(skb, af);
1684 nla_nest_end(skb, af_spec);
1688 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1689 const struct net_device *dev)
1691 struct netdev_name_node *name_node;
1694 list_for_each_entry(name_node, &dev->name_node->list, list) {
1695 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1702 static int rtnl_fill_prop_list(struct sk_buff *skb,
1703 const struct net_device *dev)
1705 struct nlattr *prop_list;
1708 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1712 ret = rtnl_fill_alt_ifnames(skb, dev);
1716 nla_nest_end(skb, prop_list);
1720 nla_nest_cancel(skb, prop_list);
1724 static int rtnl_fill_proto_down(struct sk_buff *skb,
1725 const struct net_device *dev)
1730 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1731 goto nla_put_failure;
1733 preason = dev->proto_down_reason;
1737 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1741 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1742 nla_nest_cancel(skb, pr);
1743 goto nla_put_failure;
1746 nla_nest_end(skb, pr);
1753 static int rtnl_fill_devlink_port(struct sk_buff *skb,
1754 const struct net_device *dev)
1756 struct nlattr *devlink_port_nest;
1759 devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
1760 if (!devlink_port_nest)
1763 if (dev->devlink_port) {
1764 ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
1769 nla_nest_end(skb, devlink_port_nest);
1773 nla_nest_cancel(skb, devlink_port_nest);
1777 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1778 struct net_device *dev, struct net *src_net,
1779 int type, u32 pid, u32 seq, u32 change,
1780 unsigned int flags, u32 ext_filter_mask,
1781 u32 event, int *new_nsid, int new_ifindex,
1782 int tgt_netnsid, gfp_t gfp)
1784 struct ifinfomsg *ifm;
1785 struct nlmsghdr *nlh;
1786 struct Qdisc *qdisc;
1789 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1793 ifm = nlmsg_data(nlh);
1794 ifm->ifi_family = AF_UNSPEC;
1796 ifm->ifi_type = dev->type;
1797 ifm->ifi_index = dev->ifindex;
1798 ifm->ifi_flags = dev_get_flags(dev);
1799 ifm->ifi_change = change;
1801 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1802 goto nla_put_failure;
1804 qdisc = rtnl_dereference(dev->qdisc);
1805 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1806 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1807 nla_put_u8(skb, IFLA_OPERSTATE,
1808 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1809 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1810 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1811 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1812 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1813 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1814 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1815 nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
1816 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1817 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1818 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1819 nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
1820 nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) ||
1821 nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) ||
1822 nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
1823 nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
1825 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1827 put_master_ifindex(skb, dev) ||
1828 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1830 nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1831 nla_put_ifalias(skb, dev) ||
1832 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1833 atomic_read(&dev->carrier_up_count) +
1834 atomic_read(&dev->carrier_down_count)) ||
1835 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1836 atomic_read(&dev->carrier_up_count)) ||
1837 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1838 atomic_read(&dev->carrier_down_count)))
1839 goto nla_put_failure;
1841 if (rtnl_fill_proto_down(skb, dev))
1842 goto nla_put_failure;
1844 if (event != IFLA_EVENT_NONE) {
1845 if (nla_put_u32(skb, IFLA_EVENT, event))
1846 goto nla_put_failure;
1849 if (rtnl_fill_link_ifmap(skb, dev))
1850 goto nla_put_failure;
1852 if (dev->addr_len) {
1853 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1854 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1855 goto nla_put_failure;
1858 if (rtnl_phys_port_id_fill(skb, dev))
1859 goto nla_put_failure;
1861 if (rtnl_phys_port_name_fill(skb, dev))
1862 goto nla_put_failure;
1864 if (rtnl_phys_switch_id_fill(skb, dev))
1865 goto nla_put_failure;
1867 if (rtnl_fill_stats(skb, dev))
1868 goto nla_put_failure;
1870 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1871 goto nla_put_failure;
1873 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1874 goto nla_put_failure;
1876 if (rtnl_xdp_fill(skb, dev))
1877 goto nla_put_failure;
1879 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1880 if (rtnl_link_fill(skb, dev) < 0)
1881 goto nla_put_failure;
1884 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1885 goto nla_put_failure;
1888 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1889 goto nla_put_failure;
1891 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1892 goto nla_put_failure;
1894 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1895 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1896 goto nla_put_failure;
1899 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1900 goto nla_put_failure_rcu;
1903 if (rtnl_fill_prop_list(skb, dev))
1904 goto nla_put_failure;
1906 if (dev->dev.parent &&
1907 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1908 dev_name(dev->dev.parent)))
1909 goto nla_put_failure;
1911 if (dev->dev.parent && dev->dev.parent->bus &&
1912 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1913 dev->dev.parent->bus->name))
1914 goto nla_put_failure;
1916 if (rtnl_fill_devlink_port(skb, dev))
1917 goto nla_put_failure;
1919 nlmsg_end(skb, nlh);
1922 nla_put_failure_rcu:
1925 nlmsg_cancel(skb, nlh);
1929 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1930 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1931 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1932 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1933 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1934 [IFLA_MTU] = { .type = NLA_U32 },
1935 [IFLA_LINK] = { .type = NLA_U32 },
1936 [IFLA_MASTER] = { .type = NLA_U32 },
1937 [IFLA_CARRIER] = { .type = NLA_U8 },
1938 [IFLA_TXQLEN] = { .type = NLA_U32 },
1939 [IFLA_WEIGHT] = { .type = NLA_U32 },
1940 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1941 [IFLA_LINKMODE] = { .type = NLA_U8 },
1942 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1943 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1944 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1945 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1946 * allow 0-length string (needed to remove an alias).
1948 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1949 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1950 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1951 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1952 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1953 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1954 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1955 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1956 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1957 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1958 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1959 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1960 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1961 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1962 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1963 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1964 [IFLA_XDP] = { .type = NLA_NESTED },
1965 [IFLA_EVENT] = { .type = NLA_U32 },
1966 [IFLA_GROUP] = { .type = NLA_U32 },
1967 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1968 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1969 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1970 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1971 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1972 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1973 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1974 .len = ALTIFNAMSIZ - 1 },
1975 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1976 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1977 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
1978 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
1979 [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 },
1980 [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT },
1981 [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT },
1982 [IFLA_ALLMULTI] = { .type = NLA_REJECT },
1983 [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 },
1984 [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 },
1987 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1988 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1989 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1990 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1991 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1994 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1995 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1996 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1997 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1998 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1999 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
2000 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
2001 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
2002 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
2003 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
2004 [IFLA_VF_STATS] = { .type = NLA_NESTED },
2005 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
2006 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
2007 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
2010 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
2011 [IFLA_PORT_VF] = { .type = NLA_U32 },
2012 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
2013 .len = PORT_PROFILE_MAX },
2014 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
2015 .len = PORT_UUID_MAX },
2016 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
2017 .len = PORT_UUID_MAX },
2018 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
2019 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
2021 /* Unused, but we need to keep it here since user space could
2022 * fill it. It's also broken with regard to NLA_BINARY use in
2023 * combination with structs.
2025 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
2026 .len = sizeof(struct ifla_port_vsi) },
2029 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
2030 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
2031 [IFLA_XDP_FD] = { .type = NLA_S32 },
2032 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
2033 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
2034 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
2035 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
2038 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
2040 const struct rtnl_link_ops *ops = NULL;
2041 struct nlattr *linfo[IFLA_INFO_MAX + 1];
2043 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
2046 if (linfo[IFLA_INFO_KIND]) {
2047 char kind[MODULE_NAME_LEN];
2049 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2050 ops = rtnl_link_ops_get(kind);
2056 static bool link_master_filtered(struct net_device *dev, int master_idx)
2058 struct net_device *master;
2063 master = netdev_master_upper_dev_get(dev);
2065 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2066 * another invalid value for ifindex to denote "no master".
2068 if (master_idx == -1)
2071 if (!master || master->ifindex != master_idx)
2077 static bool link_kind_filtered(const struct net_device *dev,
2078 const struct rtnl_link_ops *kind_ops)
2080 if (kind_ops && dev->rtnl_link_ops != kind_ops)
2086 static bool link_dump_filtered(struct net_device *dev,
2088 const struct rtnl_link_ops *kind_ops)
2090 if (link_master_filtered(dev, master_idx) ||
2091 link_kind_filtered(dev, kind_ops))
2098 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2099 * @sk: netlink socket
2100 * @netnsid: network namespace identifier
2102 * Returns the network namespace identified by netnsid on success or an error
2103 * pointer on failure.
2105 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2109 net = get_net_ns_by_id(sock_net(sk), netnsid);
2111 return ERR_PTR(-EINVAL);
2113 /* For now, the caller is required to have CAP_NET_ADMIN in
2114 * the user namespace owning the target net ns.
2116 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2118 return ERR_PTR(-EACCES);
2122 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2124 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2125 bool strict_check, struct nlattr **tb,
2126 struct netlink_ext_ack *extack)
2131 struct ifinfomsg *ifm;
2133 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2134 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2138 ifm = nlmsg_data(nlh);
2139 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2141 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2144 if (ifm->ifi_index) {
2145 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2149 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2150 IFLA_MAX, ifla_policy,
2154 /* A hack to preserve kernel<->userspace interface.
2155 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2156 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2157 * what iproute2 < v3.9.0 used.
2158 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2159 * attribute, its netlink message is shorter than struct ifinfomsg.
2161 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2162 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2164 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2168 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2170 struct netlink_ext_ack *extack = cb->extack;
2171 const struct nlmsghdr *nlh = cb->nlh;
2172 struct net *net = sock_net(skb->sk);
2173 struct net *tgt_net = net;
2176 struct net_device *dev;
2177 struct hlist_head *head;
2178 struct nlattr *tb[IFLA_MAX+1];
2179 u32 ext_filter_mask = 0;
2180 const struct rtnl_link_ops *kind_ops = NULL;
2181 unsigned int flags = NLM_F_MULTI;
2187 s_idx = cb->args[1];
2189 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2191 if (cb->strict_check)
2197 for (i = 0; i <= IFLA_MAX; ++i) {
2201 /* new attributes should only be added with strict checking */
2203 case IFLA_TARGET_NETNSID:
2204 netnsid = nla_get_s32(tb[i]);
2205 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2206 if (IS_ERR(tgt_net)) {
2207 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2208 return PTR_ERR(tgt_net);
2212 ext_filter_mask = nla_get_u32(tb[i]);
2215 master_idx = nla_get_u32(tb[i]);
2218 kind_ops = linkinfo_to_kind_ops(tb[i]);
2221 if (cb->strict_check) {
2222 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2228 if (master_idx || kind_ops)
2229 flags |= NLM_F_DUMP_FILTERED;
2232 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2234 head = &tgt_net->dev_index_head[h];
2235 hlist_for_each_entry(dev, head, index_hlist) {
2236 if (link_dump_filtered(dev, master_idx, kind_ops))
2240 err = rtnl_fill_ifinfo(skb, dev, net,
2242 NETLINK_CB(cb->skb).portid,
2243 nlh->nlmsg_seq, 0, flags,
2244 ext_filter_mask, 0, NULL, 0,
2245 netnsid, GFP_KERNEL);
2248 if (likely(skb->len))
2262 cb->seq = tgt_net->dev_base_seq;
2263 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2270 int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
2271 struct netlink_ext_ack *exterr)
2273 const struct ifinfomsg *ifmp;
2274 const struct nlattr *attrs;
2277 ifmp = nla_data(nla_peer);
2278 attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg);
2279 len = nla_len(nla_peer) - sizeof(struct ifinfomsg);
2281 if (ifmp->ifi_index < 0) {
2282 NL_SET_ERR_MSG_ATTR(exterr, nla_peer,
2283 "ifindex can't be negative");
2287 return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy,
2290 EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg);
2292 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2295 /* Examine the link attributes and figure out which
2296 * network namespace we are talking about.
2298 if (tb[IFLA_NET_NS_PID])
2299 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2300 else if (tb[IFLA_NET_NS_FD])
2301 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2303 net = get_net(src_net);
2306 EXPORT_SYMBOL(rtnl_link_get_net);
2308 /* Figure out which network namespace we are talking about by
2309 * examining the link attributes in the following order:
2311 * 1. IFLA_NET_NS_PID
2313 * 3. IFLA_TARGET_NETNSID
2315 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2316 struct nlattr *tb[])
2320 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2321 return rtnl_link_get_net(src_net, tb);
2323 if (!tb[IFLA_TARGET_NETNSID])
2324 return get_net(src_net);
2326 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2328 return ERR_PTR(-EINVAL);
2333 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2334 struct net *src_net,
2335 struct nlattr *tb[], int cap)
2339 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2343 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2345 return ERR_PTR(-EPERM);
2351 /* Verify that rtnetlink requests do not pass additional properties
2352 * potentially referring to different network namespaces.
2354 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2355 struct netlink_ext_ack *extack,
2359 if (netns_id_only) {
2360 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2363 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2367 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2370 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2373 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2379 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2383 static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2386 const struct net_device_ops *ops = dev->netdev_ops;
2388 if (!ops->ndo_set_vf_rate)
2390 if (max_tx_rate && max_tx_rate < min_tx_rate)
2393 return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2396 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2397 struct netlink_ext_ack *extack)
2399 if (tb[IFLA_ADDRESS] &&
2400 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2403 if (tb[IFLA_BROADCAST] &&
2404 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2407 if (tb[IFLA_GSO_MAX_SIZE] &&
2408 nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) {
2409 NL_SET_ERR_MSG(extack, "too big gso_max_size");
2413 if (tb[IFLA_GSO_MAX_SEGS] &&
2414 (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS ||
2415 nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) {
2416 NL_SET_ERR_MSG(extack, "too big gso_max_segs");
2420 if (tb[IFLA_GRO_MAX_SIZE] &&
2421 nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) {
2422 NL_SET_ERR_MSG(extack, "too big gro_max_size");
2426 if (tb[IFLA_GSO_IPV4_MAX_SIZE] &&
2427 nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) {
2428 NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size");
2432 if (tb[IFLA_GRO_IPV4_MAX_SIZE] &&
2433 nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) {
2434 NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size");
2438 if (tb[IFLA_AF_SPEC]) {
2442 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2443 const struct rtnl_af_ops *af_ops;
2445 af_ops = rtnl_af_lookup(nla_type(af));
2447 return -EAFNOSUPPORT;
2449 if (!af_ops->set_link_af)
2452 if (af_ops->validate_link_af) {
2453 err = af_ops->validate_link_af(dev, af, extack);
2463 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2466 const struct net_device_ops *ops = dev->netdev_ops;
2468 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2471 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2473 if (dev->type != ARPHRD_INFINIBAND)
2476 return handle_infiniband_guid(dev, ivt, guid_type);
2479 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2481 const struct net_device_ops *ops = dev->netdev_ops;
2484 if (tb[IFLA_VF_MAC]) {
2485 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2487 if (ivm->vf >= INT_MAX)
2490 if (ops->ndo_set_vf_mac)
2491 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2497 if (tb[IFLA_VF_VLAN]) {
2498 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2500 if (ivv->vf >= INT_MAX)
2503 if (ops->ndo_set_vf_vlan)
2504 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2506 htons(ETH_P_8021Q));
2511 if (tb[IFLA_VF_VLAN_LIST]) {
2512 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2513 struct nlattr *attr;
2517 if (!ops->ndo_set_vf_vlan)
2520 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2521 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2522 nla_len(attr) < NLA_HDRLEN) {
2525 if (len >= MAX_VLAN_LIST_LEN)
2527 ivvl[len] = nla_data(attr);
2534 if (ivvl[0]->vf >= INT_MAX)
2536 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2537 ivvl[0]->qos, ivvl[0]->vlan_proto);
2542 if (tb[IFLA_VF_TX_RATE]) {
2543 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2544 struct ifla_vf_info ivf;
2546 if (ivt->vf >= INT_MAX)
2549 if (ops->ndo_get_vf_config)
2550 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2554 err = rtnl_set_vf_rate(dev, ivt->vf,
2555 ivf.min_tx_rate, ivt->rate);
2560 if (tb[IFLA_VF_RATE]) {
2561 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2563 if (ivt->vf >= INT_MAX)
2566 err = rtnl_set_vf_rate(dev, ivt->vf,
2567 ivt->min_tx_rate, ivt->max_tx_rate);
2572 if (tb[IFLA_VF_SPOOFCHK]) {
2573 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2575 if (ivs->vf >= INT_MAX)
2578 if (ops->ndo_set_vf_spoofchk)
2579 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2585 if (tb[IFLA_VF_LINK_STATE]) {
2586 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2588 if (ivl->vf >= INT_MAX)
2591 if (ops->ndo_set_vf_link_state)
2592 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2598 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2599 struct ifla_vf_rss_query_en *ivrssq_en;
2602 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2603 if (ivrssq_en->vf >= INT_MAX)
2605 if (ops->ndo_set_vf_rss_query_en)
2606 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2607 ivrssq_en->setting);
2612 if (tb[IFLA_VF_TRUST]) {
2613 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2615 if (ivt->vf >= INT_MAX)
2618 if (ops->ndo_set_vf_trust)
2619 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2624 if (tb[IFLA_VF_IB_NODE_GUID]) {
2625 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2627 if (ivt->vf >= INT_MAX)
2629 if (!ops->ndo_set_vf_guid)
2631 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2634 if (tb[IFLA_VF_IB_PORT_GUID]) {
2635 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2637 if (ivt->vf >= INT_MAX)
2639 if (!ops->ndo_set_vf_guid)
2642 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2648 static int do_set_master(struct net_device *dev, int ifindex,
2649 struct netlink_ext_ack *extack)
2651 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2652 const struct net_device_ops *ops;
2656 if (upper_dev->ifindex == ifindex)
2658 ops = upper_dev->netdev_ops;
2659 if (ops->ndo_del_slave) {
2660 err = ops->ndo_del_slave(upper_dev, dev);
2669 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2672 ops = upper_dev->netdev_ops;
2673 if (ops->ndo_add_slave) {
2674 err = ops->ndo_add_slave(upper_dev, dev, extack);
2684 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2685 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2686 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2689 static int do_set_proto_down(struct net_device *dev,
2690 struct nlattr *nl_proto_down,
2691 struct nlattr *nl_proto_down_reason,
2692 struct netlink_ext_ack *extack)
2694 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2695 unsigned long mask = 0;
2700 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2701 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2705 if (nl_proto_down_reason) {
2706 err = nla_parse_nested_deprecated(pdreason,
2707 IFLA_PROTO_DOWN_REASON_MAX,
2708 nl_proto_down_reason,
2709 ifla_proto_down_reason_policy,
2714 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2715 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2719 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2721 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2722 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2724 dev_change_proto_down_reason(dev, mask, value);
2727 if (nl_proto_down) {
2728 proto_down = nla_get_u8(nl_proto_down);
2730 /* Don't turn off protodown if there are active reasons */
2731 if (!proto_down && dev->proto_down_reason) {
2732 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2735 err = dev_change_proto_down(dev,
2744 #define DO_SETLINK_MODIFIED 0x01
2745 /* notify flag means notify + modified. */
2746 #define DO_SETLINK_NOTIFY 0x03
2747 static int do_setlink(const struct sk_buff *skb,
2748 struct net_device *dev, struct ifinfomsg *ifm,
2749 struct netlink_ext_ack *extack,
2750 struct nlattr **tb, int status)
2752 const struct net_device_ops *ops = dev->netdev_ops;
2753 char ifname[IFNAMSIZ];
2756 if (tb[IFLA_IFNAME])
2757 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2761 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2762 const char *pat = ifname[0] ? ifname : NULL;
2766 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2773 if (tb[IFLA_NEW_IFINDEX])
2774 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2778 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2782 status |= DO_SETLINK_MODIFIED;
2786 struct rtnl_link_ifmap *u_map;
2789 if (!ops->ndo_set_config) {
2794 if (!netif_device_present(dev)) {
2799 u_map = nla_data(tb[IFLA_MAP]);
2800 k_map.mem_start = (unsigned long) u_map->mem_start;
2801 k_map.mem_end = (unsigned long) u_map->mem_end;
2802 k_map.base_addr = (unsigned short) u_map->base_addr;
2803 k_map.irq = (unsigned char) u_map->irq;
2804 k_map.dma = (unsigned char) u_map->dma;
2805 k_map.port = (unsigned char) u_map->port;
2807 err = ops->ndo_set_config(dev, &k_map);
2811 status |= DO_SETLINK_NOTIFY;
2814 if (tb[IFLA_ADDRESS]) {
2815 struct sockaddr *sa;
2818 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2820 sa = kmalloc(len, GFP_KERNEL);
2825 sa->sa_family = dev->type;
2826 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2828 err = dev_set_mac_address_user(dev, sa, extack);
2832 status |= DO_SETLINK_MODIFIED;
2836 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2839 status |= DO_SETLINK_MODIFIED;
2842 if (tb[IFLA_GROUP]) {
2843 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2844 status |= DO_SETLINK_NOTIFY;
2848 * Interface selected by interface index but interface
2849 * name provided implies that a name change has been
2852 if (ifm->ifi_index > 0 && ifname[0]) {
2853 err = dev_change_name(dev, ifname);
2856 status |= DO_SETLINK_MODIFIED;
2859 if (tb[IFLA_IFALIAS]) {
2860 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2861 nla_len(tb[IFLA_IFALIAS]));
2864 status |= DO_SETLINK_NOTIFY;
2867 if (tb[IFLA_BROADCAST]) {
2868 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2869 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2872 if (tb[IFLA_MASTER]) {
2873 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2876 status |= DO_SETLINK_MODIFIED;
2879 if (ifm->ifi_flags || ifm->ifi_change) {
2880 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2886 if (tb[IFLA_CARRIER]) {
2887 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2890 status |= DO_SETLINK_MODIFIED;
2893 if (tb[IFLA_TXQLEN]) {
2894 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2896 err = dev_change_tx_queue_len(dev, value);
2899 status |= DO_SETLINK_MODIFIED;
2902 if (tb[IFLA_GSO_MAX_SIZE]) {
2903 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2905 if (dev->gso_max_size ^ max_size) {
2906 netif_set_gso_max_size(dev, max_size);
2907 status |= DO_SETLINK_MODIFIED;
2911 if (tb[IFLA_GSO_MAX_SEGS]) {
2912 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2914 if (dev->gso_max_segs ^ max_segs) {
2915 netif_set_gso_max_segs(dev, max_segs);
2916 status |= DO_SETLINK_MODIFIED;
2920 if (tb[IFLA_GRO_MAX_SIZE]) {
2921 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2923 if (dev->gro_max_size ^ gro_max_size) {
2924 netif_set_gro_max_size(dev, gro_max_size);
2925 status |= DO_SETLINK_MODIFIED;
2929 if (tb[IFLA_GSO_IPV4_MAX_SIZE]) {
2930 u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]);
2932 if (dev->gso_ipv4_max_size ^ max_size) {
2933 netif_set_gso_ipv4_max_size(dev, max_size);
2934 status |= DO_SETLINK_MODIFIED;
2938 if (tb[IFLA_GRO_IPV4_MAX_SIZE]) {
2939 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]);
2941 if (dev->gro_ipv4_max_size ^ gro_max_size) {
2942 netif_set_gro_ipv4_max_size(dev, gro_max_size);
2943 status |= DO_SETLINK_MODIFIED;
2947 if (tb[IFLA_OPERSTATE])
2948 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2950 if (tb[IFLA_LINKMODE]) {
2951 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2953 write_lock(&dev_base_lock);
2954 if (dev->link_mode ^ value)
2955 status |= DO_SETLINK_NOTIFY;
2956 dev->link_mode = value;
2957 write_unlock(&dev_base_lock);
2960 if (tb[IFLA_VFINFO_LIST]) {
2961 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2962 struct nlattr *attr;
2965 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2966 if (nla_type(attr) != IFLA_VF_INFO ||
2967 nla_len(attr) < NLA_HDRLEN) {
2971 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2977 err = do_setvfinfo(dev, vfinfo);
2980 status |= DO_SETLINK_NOTIFY;
2985 if (tb[IFLA_VF_PORTS]) {
2986 struct nlattr *port[IFLA_PORT_MAX+1];
2987 struct nlattr *attr;
2992 if (!ops->ndo_set_vf_port)
2995 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2996 if (nla_type(attr) != IFLA_VF_PORT ||
2997 nla_len(attr) < NLA_HDRLEN) {
3001 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
3007 if (!port[IFLA_PORT_VF]) {
3011 vf = nla_get_u32(port[IFLA_PORT_VF]);
3012 err = ops->ndo_set_vf_port(dev, vf, port);
3015 status |= DO_SETLINK_NOTIFY;
3020 if (tb[IFLA_PORT_SELF]) {
3021 struct nlattr *port[IFLA_PORT_MAX+1];
3023 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
3025 ifla_port_policy, NULL);
3030 if (ops->ndo_set_vf_port)
3031 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
3034 status |= DO_SETLINK_NOTIFY;
3037 if (tb[IFLA_AF_SPEC]) {
3041 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
3042 const struct rtnl_af_ops *af_ops;
3044 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
3046 err = af_ops->set_link_af(dev, af, extack);
3050 status |= DO_SETLINK_NOTIFY;
3055 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
3056 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
3057 tb[IFLA_PROTO_DOWN_REASON], extack);
3060 status |= DO_SETLINK_NOTIFY;
3064 struct nlattr *xdp[IFLA_XDP_MAX + 1];
3067 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
3069 ifla_xdp_policy, NULL);
3073 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
3078 if (xdp[IFLA_XDP_FLAGS]) {
3079 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
3080 if (xdp_flags & ~XDP_FLAGS_MASK) {
3084 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
3090 if (xdp[IFLA_XDP_FD]) {
3091 int expected_fd = -1;
3093 if (xdp_flags & XDP_FLAGS_REPLACE) {
3094 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
3099 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
3102 err = dev_change_xdp_fd(dev, extack,
3103 nla_get_s32(xdp[IFLA_XDP_FD]),
3108 status |= DO_SETLINK_NOTIFY;
3113 if (status & DO_SETLINK_MODIFIED) {
3114 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3115 netdev_state_change(dev);
3118 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",
3125 static struct net_device *rtnl_dev_get(struct net *net,
3126 struct nlattr *tb[])
3128 char ifname[ALTIFNAMSIZ];
3130 if (tb[IFLA_IFNAME])
3131 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3132 else if (tb[IFLA_ALT_IFNAME])
3133 nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3137 return __dev_get_by_name(net, ifname);
3140 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3141 struct netlink_ext_ack *extack)
3143 struct net *net = sock_net(skb->sk);
3144 struct ifinfomsg *ifm;
3145 struct net_device *dev;
3147 struct nlattr *tb[IFLA_MAX+1];
3149 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3150 ifla_policy, extack);
3154 err = rtnl_ensure_unique_netns(tb, extack, false);
3159 ifm = nlmsg_data(nlh);
3160 if (ifm->ifi_index > 0)
3161 dev = __dev_get_by_index(net, ifm->ifi_index);
3162 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3163 dev = rtnl_dev_get(net, tb);
3172 err = validate_linkmsg(dev, tb, extack);
3176 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3181 static int rtnl_group_dellink(const struct net *net, int group)
3183 struct net_device *dev, *aux;
3184 LIST_HEAD(list_kill);
3190 for_each_netdev(net, dev) {
3191 if (dev->group == group) {
3192 const struct rtnl_link_ops *ops;
3195 ops = dev->rtnl_link_ops;
3196 if (!ops || !ops->dellink)
3204 for_each_netdev_safe(net, dev, aux) {
3205 if (dev->group == group) {
3206 const struct rtnl_link_ops *ops;
3208 ops = dev->rtnl_link_ops;
3209 ops->dellink(dev, &list_kill);
3212 unregister_netdevice_many(&list_kill);
3217 int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
3219 const struct rtnl_link_ops *ops;
3220 LIST_HEAD(list_kill);
3222 ops = dev->rtnl_link_ops;
3223 if (!ops || !ops->dellink)
3226 ops->dellink(dev, &list_kill);
3227 unregister_netdevice_many_notify(&list_kill, portid, nlh);
3231 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3233 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3234 struct netlink_ext_ack *extack)
3236 struct net *net = sock_net(skb->sk);
3237 u32 portid = NETLINK_CB(skb).portid;
3238 struct net *tgt_net = net;
3239 struct net_device *dev = NULL;
3240 struct ifinfomsg *ifm;
3241 struct nlattr *tb[IFLA_MAX+1];
3245 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3246 ifla_policy, extack);
3250 err = rtnl_ensure_unique_netns(tb, extack, true);
3254 if (tb[IFLA_TARGET_NETNSID]) {
3255 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3256 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3257 if (IS_ERR(tgt_net))
3258 return PTR_ERR(tgt_net);
3262 ifm = nlmsg_data(nlh);
3263 if (ifm->ifi_index > 0)
3264 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3265 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3266 dev = rtnl_dev_get(net, tb);
3267 else if (tb[IFLA_GROUP])
3268 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3273 if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3279 err = rtnl_delete_link(dev, portid, nlh);
3288 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
3289 u32 portid, const struct nlmsghdr *nlh)
3291 unsigned int old_flags;
3294 old_flags = dev->flags;
3295 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3296 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3302 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3303 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh);
3305 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3306 __dev_notify_flags(dev, old_flags, ~0U, portid, nlh);
3310 EXPORT_SYMBOL(rtnl_configure_link);
3312 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3313 unsigned char name_assign_type,
3314 const struct rtnl_link_ops *ops,
3315 struct nlattr *tb[],
3316 struct netlink_ext_ack *extack)
3318 struct net_device *dev;
3319 unsigned int num_tx_queues = 1;
3320 unsigned int num_rx_queues = 1;
3323 if (tb[IFLA_NUM_TX_QUEUES])
3324 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3325 else if (ops->get_num_tx_queues)
3326 num_tx_queues = ops->get_num_tx_queues();
3328 if (tb[IFLA_NUM_RX_QUEUES])
3329 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3330 else if (ops->get_num_rx_queues)
3331 num_rx_queues = ops->get_num_rx_queues();
3333 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3334 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3335 return ERR_PTR(-EINVAL);
3338 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3339 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3340 return ERR_PTR(-EINVAL);
3344 dev = ops->alloc(tb, ifname, name_assign_type,
3345 num_tx_queues, num_rx_queues);
3349 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3350 name_assign_type, ops->setup,
3351 num_tx_queues, num_rx_queues);
3355 return ERR_PTR(-ENOMEM);
3357 err = validate_linkmsg(dev, tb, extack);
3360 return ERR_PTR(err);
3363 dev_net_set(dev, net);
3364 dev->rtnl_link_ops = ops;
3365 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3368 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3370 err = dev_validate_mtu(dev, mtu, extack);
3373 return ERR_PTR(err);
3377 if (tb[IFLA_ADDRESS]) {
3378 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3379 nla_len(tb[IFLA_ADDRESS]));
3380 dev->addr_assign_type = NET_ADDR_SET;
3382 if (tb[IFLA_BROADCAST])
3383 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3384 nla_len(tb[IFLA_BROADCAST]));
3385 if (tb[IFLA_TXQLEN])
3386 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3387 if (tb[IFLA_OPERSTATE])
3388 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3389 if (tb[IFLA_LINKMODE])
3390 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3392 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3393 if (tb[IFLA_GSO_MAX_SIZE])
3394 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3395 if (tb[IFLA_GSO_MAX_SEGS])
3396 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3397 if (tb[IFLA_GRO_MAX_SIZE])
3398 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3399 if (tb[IFLA_GSO_IPV4_MAX_SIZE])
3400 netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
3401 if (tb[IFLA_GRO_IPV4_MAX_SIZE])
3402 netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
3406 EXPORT_SYMBOL(rtnl_create_link);
3408 static int rtnl_group_changelink(const struct sk_buff *skb,
3409 struct net *net, int group,
3410 struct ifinfomsg *ifm,
3411 struct netlink_ext_ack *extack,
3414 struct net_device *dev, *aux;
3417 for_each_netdev_safe(net, dev, aux) {
3418 if (dev->group == group) {
3419 err = validate_linkmsg(dev, tb, extack);
3422 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3431 static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3432 const struct rtnl_link_ops *ops,
3433 const struct nlmsghdr *nlh,
3434 struct nlattr **tb, struct nlattr **data,
3435 struct netlink_ext_ack *extack)
3437 unsigned char name_assign_type = NET_NAME_USER;
3438 struct net *net = sock_net(skb->sk);
3439 u32 portid = NETLINK_CB(skb).portid;
3440 struct net *dest_net, *link_net;
3441 struct net_device *dev;
3442 char ifname[IFNAMSIZ];
3445 if (!ops->alloc && !ops->setup)
3448 if (tb[IFLA_IFNAME]) {
3449 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3451 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3452 name_assign_type = NET_NAME_ENUM;
3455 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3456 if (IS_ERR(dest_net))
3457 return PTR_ERR(dest_net);
3459 if (tb[IFLA_LINK_NETNSID]) {
3460 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3462 link_net = get_net_ns_by_id(dest_net, id);
3464 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3469 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3475 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3476 name_assign_type, ops, tb, extack);
3482 dev->ifindex = ifm->ifi_index;
3485 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3487 err = register_netdevice(dev);
3493 err = rtnl_configure_link(dev, ifm, portid, nlh);
3495 goto out_unregister;
3497 err = dev_change_net_namespace(dev, dest_net, ifname);
3499 goto out_unregister;
3501 if (tb[IFLA_MASTER]) {
3502 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3504 goto out_unregister;
3513 LIST_HEAD(list_kill);
3515 ops->dellink(dev, &list_kill);
3516 unregister_netdevice_many(&list_kill);
3518 unregister_netdevice(dev);
3523 struct rtnl_newlink_tbs {
3524 struct nlattr *tb[IFLA_MAX + 1];
3525 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3526 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3529 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3530 struct rtnl_newlink_tbs *tbs,
3531 struct netlink_ext_ack *extack)
3533 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3534 struct nlattr ** const tb = tbs->tb;
3535 const struct rtnl_link_ops *m_ops;
3536 struct net_device *master_dev;
3537 struct net *net = sock_net(skb->sk);
3538 const struct rtnl_link_ops *ops;
3539 struct nlattr **slave_data;
3540 char kind[MODULE_NAME_LEN];
3541 struct net_device *dev;
3542 struct ifinfomsg *ifm;
3543 struct nlattr **data;
3544 bool link_specified;
3547 #ifdef CONFIG_MODULES
3550 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3551 ifla_policy, extack);
3555 err = rtnl_ensure_unique_netns(tb, extack, false);
3559 ifm = nlmsg_data(nlh);
3560 if (ifm->ifi_index > 0) {
3561 link_specified = true;
3562 dev = __dev_get_by_index(net, ifm->ifi_index);
3563 } else if (ifm->ifi_index < 0) {
3564 NL_SET_ERR_MSG(extack, "ifindex can't be negative");
3566 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3567 link_specified = true;
3568 dev = rtnl_dev_get(net, tb);
3570 link_specified = false;
3577 master_dev = netdev_master_upper_dev_get(dev);
3579 m_ops = master_dev->rtnl_link_ops;
3582 if (tb[IFLA_LINKINFO]) {
3583 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3585 ifla_info_policy, NULL);
3589 memset(linkinfo, 0, sizeof(linkinfo));
3591 if (linkinfo[IFLA_INFO_KIND]) {
3592 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3593 ops = rtnl_link_ops_get(kind);
3601 if (ops->maxtype > RTNL_MAX_TYPE)
3604 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3605 err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
3606 linkinfo[IFLA_INFO_DATA],
3607 ops->policy, extack);
3612 if (ops->validate) {
3613 err = ops->validate(tb, data, extack);
3621 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3624 if (m_ops->slave_maxtype &&
3625 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3626 err = nla_parse_nested_deprecated(tbs->slave_attr,
3627 m_ops->slave_maxtype,
3628 linkinfo[IFLA_INFO_SLAVE_DATA],
3629 m_ops->slave_policy,
3633 slave_data = tbs->slave_attr;
3640 if (nlh->nlmsg_flags & NLM_F_EXCL)
3642 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3645 err = validate_linkmsg(dev, tb, extack);
3649 if (linkinfo[IFLA_INFO_DATA]) {
3650 if (!ops || ops != dev->rtnl_link_ops ||
3654 err = ops->changelink(dev, tb, data, extack);
3657 status |= DO_SETLINK_NOTIFY;
3660 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3661 if (!m_ops || !m_ops->slave_changelink)
3664 err = m_ops->slave_changelink(master_dev, dev, tb,
3665 slave_data, extack);
3668 status |= DO_SETLINK_NOTIFY;
3671 return do_setlink(skb, dev, ifm, extack, tb, status);
3674 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3675 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3676 * or it's for a group
3681 return rtnl_group_changelink(skb, net,
3682 nla_get_u32(tb[IFLA_GROUP]),
3687 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3691 #ifdef CONFIG_MODULES
3694 request_module("rtnl-link-%s", kind);
3696 ops = rtnl_link_ops_get(kind);
3701 NL_SET_ERR_MSG(extack, "Unknown device type");
3705 return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
3708 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3709 struct netlink_ext_ack *extack)
3711 struct rtnl_newlink_tbs *tbs;
3714 tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3718 ret = __rtnl_newlink(skb, nlh, tbs, extack);
3723 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3724 const struct nlmsghdr *nlh,
3726 struct netlink_ext_ack *extack)
3728 struct ifinfomsg *ifm;
3731 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3732 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3736 if (!netlink_strict_get_check(skb))
3737 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3738 ifla_policy, extack);
3740 ifm = nlmsg_data(nlh);
3741 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3743 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3747 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3748 ifla_policy, extack);
3752 for (i = 0; i <= IFLA_MAX; i++) {
3758 case IFLA_ALT_IFNAME:
3760 case IFLA_TARGET_NETNSID:
3763 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3771 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3772 struct netlink_ext_ack *extack)
3774 struct net *net = sock_net(skb->sk);
3775 struct net *tgt_net = net;
3776 struct ifinfomsg *ifm;
3777 struct nlattr *tb[IFLA_MAX+1];
3778 struct net_device *dev = NULL;
3779 struct sk_buff *nskb;
3782 u32 ext_filter_mask = 0;
3784 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3788 err = rtnl_ensure_unique_netns(tb, extack, true);
3792 if (tb[IFLA_TARGET_NETNSID]) {
3793 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3794 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3795 if (IS_ERR(tgt_net))
3796 return PTR_ERR(tgt_net);
3799 if (tb[IFLA_EXT_MASK])
3800 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3803 ifm = nlmsg_data(nlh);
3804 if (ifm->ifi_index > 0)
3805 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3806 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3807 dev = rtnl_dev_get(tgt_net, tb);
3816 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3820 err = rtnl_fill_ifinfo(nskb, dev, net,
3821 RTM_NEWLINK, NETLINK_CB(skb).portid,
3822 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3823 0, NULL, 0, netnsid, GFP_KERNEL);
3825 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3826 WARN_ON(err == -EMSGSIZE);
3829 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3837 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3838 bool *changed, struct netlink_ext_ack *extack)
3844 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3848 if (cmd == RTM_NEWLINKPROP) {
3849 size = rtnl_prop_list_size(dev);
3850 size += nla_total_size(ALTIFNAMSIZ);
3851 if (size >= U16_MAX) {
3852 NL_SET_ERR_MSG(extack,
3853 "effective property list too long");
3858 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3862 if (cmd == RTM_NEWLINKPROP) {
3863 err = netdev_name_node_alt_create(dev, alt_ifname);
3866 } else if (cmd == RTM_DELLINKPROP) {
3867 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3879 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3880 struct netlink_ext_ack *extack)
3882 struct net *net = sock_net(skb->sk);
3883 struct nlattr *tb[IFLA_MAX + 1];
3884 struct net_device *dev;
3885 struct ifinfomsg *ifm;
3886 bool changed = false;
3887 struct nlattr *attr;
3890 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3894 err = rtnl_ensure_unique_netns(tb, extack, true);
3898 ifm = nlmsg_data(nlh);
3899 if (ifm->ifi_index > 0)
3900 dev = __dev_get_by_index(net, ifm->ifi_index);
3901 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3902 dev = rtnl_dev_get(net, tb);
3909 if (!tb[IFLA_PROP_LIST])
3912 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3913 switch (nla_type(attr)) {
3914 case IFLA_ALT_IFNAME:
3915 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3923 netdev_state_change(dev);
3927 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3928 struct netlink_ext_ack *extack)
3930 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3933 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3934 struct netlink_ext_ack *extack)
3936 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3939 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3941 struct net *net = sock_net(skb->sk);
3942 size_t min_ifinfo_dump_size = 0;
3943 struct nlattr *tb[IFLA_MAX+1];
3944 u32 ext_filter_mask = 0;
3945 struct net_device *dev;
3948 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3949 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3950 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3952 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3953 if (tb[IFLA_EXT_MASK])
3954 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3957 if (!ext_filter_mask)
3958 return NLMSG_GOODSIZE;
3960 * traverse the list of net devices and compute the minimum
3961 * buffer size based upon the filter mask.
3964 for_each_netdev_rcu(net, dev) {
3965 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3966 if_nlmsg_size(dev, ext_filter_mask));
3970 return nlmsg_total_size(min_ifinfo_dump_size);
3973 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3976 int s_idx = cb->family;
3977 int type = cb->nlh->nlmsg_type - RTM_BASE;
3983 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3984 struct rtnl_link __rcu **tab;
3985 struct rtnl_link *link;
3986 rtnl_dumpit_func dumpit;
3988 if (idx < s_idx || idx == PF_PACKET)
3991 if (type < 0 || type >= RTM_NR_MSGTYPES)
3994 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3998 link = rcu_dereference_rtnl(tab[type]);
4002 dumpit = link->dumpit;
4007 memset(&cb->args[0], 0, sizeof(cb->args));
4011 ret = dumpit(skb, cb);
4017 return skb->len ? : ret;
4020 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
4021 unsigned int change,
4022 u32 event, gfp_t flags, int *new_nsid,
4023 int new_ifindex, u32 portid,
4024 const struct nlmsghdr *nlh)
4026 struct net *net = dev_net(dev);
4027 struct sk_buff *skb;
4031 skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
4035 if (nlmsg_report(nlh))
4036 seq = nlmsg_seq(nlh);
4040 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
4041 type, portid, seq, change, 0, 0, event,
4042 new_nsid, new_ifindex, -1, flags);
4044 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
4045 WARN_ON(err == -EMSGSIZE);
4052 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4056 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags,
4057 u32 portid, const struct nlmsghdr *nlh)
4059 struct net *net = dev_net(dev);
4061 rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags);
4064 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
4065 unsigned int change, u32 event,
4066 gfp_t flags, int *new_nsid, int new_ifindex,
4067 u32 portid, const struct nlmsghdr *nlh)
4069 struct sk_buff *skb;
4071 if (dev->reg_state != NETREG_REGISTERED)
4074 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
4075 new_ifindex, portid, nlh);
4077 rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
4080 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
4081 gfp_t flags, u32 portid, const struct nlmsghdr *nlh)
4083 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4084 NULL, 0, portid, nlh);
4087 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
4088 gfp_t flags, int *new_nsid, int new_ifindex)
4090 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4091 new_nsid, new_ifindex, 0, NULL);
4094 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
4095 struct net_device *dev,
4096 u8 *addr, u16 vid, u32 pid, u32 seq,
4097 int type, unsigned int flags,
4098 int nlflags, u16 ndm_state)
4100 struct nlmsghdr *nlh;
4103 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4107 ndm = nlmsg_data(nlh);
4108 ndm->ndm_family = AF_BRIDGE;
4111 ndm->ndm_flags = flags;
4113 ndm->ndm_ifindex = dev->ifindex;
4114 ndm->ndm_state = ndm_state;
4116 if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
4117 goto nla_put_failure;
4119 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
4120 goto nla_put_failure;
4122 nlmsg_end(skb, nlh);
4126 nlmsg_cancel(skb, nlh);
4130 static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
4132 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4133 nla_total_size(dev->addr_len) + /* NDA_LLADDR */
4134 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
4138 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4141 struct net *net = dev_net(dev);
4142 struct sk_buff *skb;
4145 skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
4149 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4150 0, 0, type, NTF_SELF, 0, ndm_state);
4156 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
4159 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
4163 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4165 int ndo_dflt_fdb_add(struct ndmsg *ndm,
4166 struct nlattr *tb[],
4167 struct net_device *dev,
4168 const unsigned char *addr, u16 vid,
4173 /* If aging addresses are supported device will need to
4174 * implement its own handler for this.
4176 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4177 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4181 if (tb[NDA_FLAGS_EXT]) {
4182 netdev_info(dev, "invalid flags given to default FDB implementation\n");
4187 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
4191 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4192 err = dev_uc_add_excl(dev, addr);
4193 else if (is_multicast_ether_addr(addr))
4194 err = dev_mc_add_excl(dev, addr);
4196 /* Only return duplicate errors if NLM_F_EXCL is set */
4197 if (err == -EEXIST && !(flags & NLM_F_EXCL))
4202 EXPORT_SYMBOL(ndo_dflt_fdb_add);
4204 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4205 struct netlink_ext_ack *extack)
4210 if (nla_len(vlan_attr) != sizeof(u16)) {
4211 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4215 vid = nla_get_u16(vlan_attr);
4217 if (!vid || vid >= VLAN_VID_MASK) {
4218 NL_SET_ERR_MSG(extack, "invalid vlan id");
4226 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4227 struct netlink_ext_ack *extack)
4229 struct net *net = sock_net(skb->sk);
4231 struct nlattr *tb[NDA_MAX+1];
4232 struct net_device *dev;
4237 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4242 ndm = nlmsg_data(nlh);
4243 if (ndm->ndm_ifindex == 0) {
4244 NL_SET_ERR_MSG(extack, "invalid ifindex");
4248 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4250 NL_SET_ERR_MSG(extack, "unknown ifindex");
4254 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4255 NL_SET_ERR_MSG(extack, "invalid address");
4259 if (dev->type != ARPHRD_ETHER) {
4260 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4264 addr = nla_data(tb[NDA_LLADDR]);
4266 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4272 /* Support fdb on master device the net/bridge default case */
4273 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4274 netif_is_bridge_port(dev)) {
4275 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4276 const struct net_device_ops *ops = br_dev->netdev_ops;
4278 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4279 nlh->nlmsg_flags, extack);
4283 ndm->ndm_flags &= ~NTF_MASTER;
4286 /* Embedded bridge, macvlan, and any other device support */
4287 if ((ndm->ndm_flags & NTF_SELF)) {
4288 if (dev->netdev_ops->ndo_fdb_add)
4289 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4294 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4298 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4300 ndm->ndm_flags &= ~NTF_SELF;
4308 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4310 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4311 struct nlattr *tb[],
4312 struct net_device *dev,
4313 const unsigned char *addr, u16 vid)
4317 /* If aging addresses are supported device will need to
4318 * implement its own handler for this.
4320 if (!(ndm->ndm_state & NUD_PERMANENT)) {
4321 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4325 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4326 err = dev_uc_del(dev, addr);
4327 else if (is_multicast_ether_addr(addr))
4328 err = dev_mc_del(dev, addr);
4332 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4334 static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
4335 [NDA_VLAN] = { .type = NLA_U16 },
4336 [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
4337 [NDA_NDM_STATE_MASK] = { .type = NLA_U16 },
4338 [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 },
4341 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4342 struct netlink_ext_ack *extack)
4344 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
4345 struct net *net = sock_net(skb->sk);
4346 const struct net_device_ops *ops;
4348 struct nlattr *tb[NDA_MAX+1];
4349 struct net_device *dev;
4354 if (!netlink_capable(skb, CAP_NET_ADMIN))
4358 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
4361 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
4362 fdb_del_bulk_policy, extack);
4367 ndm = nlmsg_data(nlh);
4368 if (ndm->ndm_ifindex == 0) {
4369 NL_SET_ERR_MSG(extack, "invalid ifindex");
4373 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4375 NL_SET_ERR_MSG(extack, "unknown ifindex");
4380 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4381 NL_SET_ERR_MSG(extack, "invalid address");
4384 addr = nla_data(tb[NDA_LLADDR]);
4387 if (dev->type != ARPHRD_ETHER) {
4388 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4392 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4398 /* Support fdb on master device the net/bridge default case */
4399 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4400 netif_is_bridge_port(dev)) {
4401 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4403 ops = br_dev->netdev_ops;
4405 if (ops->ndo_fdb_del)
4406 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4408 if (ops->ndo_fdb_del_bulk)
4409 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4416 ndm->ndm_flags &= ~NTF_MASTER;
4419 /* Embedded bridge, macvlan, and any other device support */
4420 if (ndm->ndm_flags & NTF_SELF) {
4421 ops = dev->netdev_ops;
4423 if (ops->ndo_fdb_del)
4424 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4426 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4428 /* in case err was cleared by NTF_MASTER call */
4430 if (ops->ndo_fdb_del_bulk)
4431 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4437 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4439 ndm->ndm_flags &= ~NTF_SELF;
4446 static int nlmsg_populate_fdb(struct sk_buff *skb,
4447 struct netlink_callback *cb,
4448 struct net_device *dev,
4450 struct netdev_hw_addr_list *list)
4452 struct netdev_hw_addr *ha;
4456 portid = NETLINK_CB(cb->skb).portid;
4457 seq = cb->nlh->nlmsg_seq;
4459 list_for_each_entry(ha, &list->list, list) {
4460 if (*idx < cb->args[2])
4463 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4465 RTM_NEWNEIGH, NTF_SELF,
4466 NLM_F_MULTI, NUD_PERMANENT);
4476 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4477 * @skb: socket buffer to store message in
4478 * @cb: netlink callback
4480 * @filter_dev: ignored
4481 * @idx: the number of FDB table entries dumped is added to *@idx
4483 * Default netdevice operation to dump the existing unicast address list.
4484 * Returns number of addresses from list put in skb.
4486 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4487 struct netlink_callback *cb,
4488 struct net_device *dev,
4489 struct net_device *filter_dev,
4494 if (dev->type != ARPHRD_ETHER)
4497 netif_addr_lock_bh(dev);
4498 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4501 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4503 netif_addr_unlock_bh(dev);
4506 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4508 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4509 int *br_idx, int *brport_idx,
4510 struct netlink_ext_ack *extack)
4512 struct nlattr *tb[NDA_MAX + 1];
4516 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4517 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4521 ndm = nlmsg_data(nlh);
4522 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4523 ndm->ndm_flags || ndm->ndm_type) {
4524 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4528 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4529 NDA_MAX, NULL, extack);
4533 *brport_idx = ndm->ndm_ifindex;
4534 for (i = 0; i <= NDA_MAX; ++i) {
4540 if (nla_len(tb[i]) != sizeof(u32)) {
4541 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4544 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4547 if (nla_len(tb[i]) != sizeof(u32)) {
4548 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4551 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4554 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4562 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4563 int *br_idx, int *brport_idx,
4564 struct netlink_ext_ack *extack)
4566 struct nlattr *tb[IFLA_MAX+1];
4569 /* A hack to preserve kernel<->userspace interface.
4570 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4571 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4572 * So, check for ndmsg with an optional u32 attribute (not used here).
4573 * Fortunately these sizes don't conflict with the size of ifinfomsg
4574 * with an optional attribute.
4576 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4577 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4578 nla_attr_size(sizeof(u32)))) {
4579 struct ifinfomsg *ifm;
4581 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4582 tb, IFLA_MAX, ifla_policy,
4586 } else if (err == 0) {
4587 if (tb[IFLA_MASTER])
4588 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4591 ifm = nlmsg_data(nlh);
4592 *brport_idx = ifm->ifi_index;
4597 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4599 struct net_device *dev;
4600 struct net_device *br_dev = NULL;
4601 const struct net_device_ops *ops = NULL;
4602 const struct net_device_ops *cops = NULL;
4603 struct net *net = sock_net(skb->sk);
4604 struct hlist_head *head;
4612 if (cb->strict_check)
4613 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4616 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4622 br_dev = __dev_get_by_index(net, br_idx);
4626 ops = br_dev->netdev_ops;
4630 s_idx = cb->args[1];
4632 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4634 head = &net->dev_index_head[h];
4635 hlist_for_each_entry(dev, head, index_hlist) {
4637 if (brport_idx && (dev->ifindex != brport_idx))
4640 if (!br_idx) { /* user did not specify a specific bridge */
4641 if (netif_is_bridge_port(dev)) {
4642 br_dev = netdev_master_upper_dev_get(dev);
4643 cops = br_dev->netdev_ops;
4646 if (dev != br_dev &&
4647 !netif_is_bridge_port(dev))
4650 if (br_dev != netdev_master_upper_dev_get(dev) &&
4651 !netif_is_bridge_master(dev))
4659 if (netif_is_bridge_port(dev)) {
4660 if (cops && cops->ndo_fdb_dump) {
4661 err = cops->ndo_fdb_dump(skb, cb,
4664 if (err == -EMSGSIZE)
4669 if (dev->netdev_ops->ndo_fdb_dump)
4670 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4674 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4676 if (err == -EMSGSIZE)
4681 /* reset fdb offset to 0 for rest of the interfaces */
4697 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4698 struct nlattr **tb, u8 *ndm_flags,
4699 int *br_idx, int *brport_idx, u8 **addr,
4700 u16 *vid, struct netlink_ext_ack *extack)
4705 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4706 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4710 ndm = nlmsg_data(nlh);
4711 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4713 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4717 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4718 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4722 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4723 NDA_MAX, nda_policy, extack);
4727 *ndm_flags = ndm->ndm_flags;
4728 *brport_idx = ndm->ndm_ifindex;
4729 for (i = 0; i <= NDA_MAX; ++i) {
4735 *br_idx = nla_get_u32(tb[i]);
4738 if (nla_len(tb[i]) != ETH_ALEN) {
4739 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4742 *addr = nla_data(tb[i]);
4745 err = fdb_vid_parse(tb[i], vid, extack);
4752 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4760 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4761 struct netlink_ext_ack *extack)
4763 struct net_device *dev = NULL, *br_dev = NULL;
4764 const struct net_device_ops *ops = NULL;
4765 struct net *net = sock_net(in_skb->sk);
4766 struct nlattr *tb[NDA_MAX + 1];
4767 struct sk_buff *skb;
4775 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4776 &brport_idx, &addr, &vid, extack);
4781 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4786 dev = __dev_get_by_index(net, brport_idx);
4788 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4795 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4799 br_dev = __dev_get_by_index(net, br_idx);
4801 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4804 ops = br_dev->netdev_ops;
4808 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4809 if (!netif_is_bridge_port(dev)) {
4810 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4813 br_dev = netdev_master_upper_dev_get(dev);
4815 NL_SET_ERR_MSG(extack, "Master of device not found");
4818 ops = br_dev->netdev_ops;
4820 if (!(ndm_flags & NTF_SELF)) {
4821 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4824 ops = dev->netdev_ops;
4828 if (!br_dev && !dev) {
4829 NL_SET_ERR_MSG(extack, "No device specified");
4833 if (!ops || !ops->ndo_fdb_get) {
4834 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4838 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4844 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4845 NETLINK_CB(in_skb).portid,
4846 nlh->nlmsg_seq, extack);
4850 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4856 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4857 unsigned int attrnum, unsigned int flag)
4860 return nla_put_u8(skb, attrnum, !!(flags & flag));
4864 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4865 struct net_device *dev, u16 mode,
4866 u32 flags, u32 mask, int nlflags,
4868 int (*vlan_fill)(struct sk_buff *skb,
4869 struct net_device *dev,
4872 struct nlmsghdr *nlh;
4873 struct ifinfomsg *ifm;
4874 struct nlattr *br_afspec;
4875 struct nlattr *protinfo;
4876 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4877 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4880 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4884 ifm = nlmsg_data(nlh);
4885 ifm->ifi_family = AF_BRIDGE;
4887 ifm->ifi_type = dev->type;
4888 ifm->ifi_index = dev->ifindex;
4889 ifm->ifi_flags = dev_get_flags(dev);
4890 ifm->ifi_change = 0;
4893 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4894 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4895 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4897 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4899 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4900 (dev->ifindex != dev_get_iflink(dev) &&
4901 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4902 goto nla_put_failure;
4904 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4906 goto nla_put_failure;
4908 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4909 nla_nest_cancel(skb, br_afspec);
4910 goto nla_put_failure;
4913 if (mode != BRIDGE_MODE_UNDEF) {
4914 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4915 nla_nest_cancel(skb, br_afspec);
4916 goto nla_put_failure;
4920 err = vlan_fill(skb, dev, filter_mask);
4922 nla_nest_cancel(skb, br_afspec);
4923 goto nla_put_failure;
4926 nla_nest_end(skb, br_afspec);
4928 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4930 goto nla_put_failure;
4932 if (brport_nla_put_flag(skb, flags, mask,
4933 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4934 brport_nla_put_flag(skb, flags, mask,
4935 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4936 brport_nla_put_flag(skb, flags, mask,
4937 IFLA_BRPORT_FAST_LEAVE,
4938 BR_MULTICAST_FAST_LEAVE) ||
4939 brport_nla_put_flag(skb, flags, mask,
4940 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4941 brport_nla_put_flag(skb, flags, mask,
4942 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4943 brport_nla_put_flag(skb, flags, mask,
4944 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4945 brport_nla_put_flag(skb, flags, mask,
4946 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4947 brport_nla_put_flag(skb, flags, mask,
4948 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4949 brport_nla_put_flag(skb, flags, mask,
4950 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4951 brport_nla_put_flag(skb, flags, mask,
4952 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4953 nla_nest_cancel(skb, protinfo);
4954 goto nla_put_failure;
4957 nla_nest_end(skb, protinfo);
4959 nlmsg_end(skb, nlh);
4962 nlmsg_cancel(skb, nlh);
4963 return err ? err : -EMSGSIZE;
4965 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4967 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4968 bool strict_check, u32 *filter_mask,
4969 struct netlink_ext_ack *extack)
4971 struct nlattr *tb[IFLA_MAX+1];
4975 struct ifinfomsg *ifm;
4977 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4978 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4982 ifm = nlmsg_data(nlh);
4983 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4984 ifm->ifi_change || ifm->ifi_index) {
4985 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4989 err = nlmsg_parse_deprecated_strict(nlh,
4990 sizeof(struct ifinfomsg),
4991 tb, IFLA_MAX, ifla_policy,
4994 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4995 tb, IFLA_MAX, ifla_policy,
5001 /* new attributes should only be added with strict checking */
5002 for (i = 0; i <= IFLA_MAX; ++i) {
5008 *filter_mask = nla_get_u32(tb[i]);
5012 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
5021 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
5023 const struct nlmsghdr *nlh = cb->nlh;
5024 struct net *net = sock_net(skb->sk);
5025 struct net_device *dev;
5027 u32 portid = NETLINK_CB(cb->skb).portid;
5028 u32 seq = nlh->nlmsg_seq;
5029 u32 filter_mask = 0;
5032 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
5034 if (err < 0 && cb->strict_check)
5038 for_each_netdev_rcu(net, dev) {
5039 const struct net_device_ops *ops = dev->netdev_ops;
5040 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5042 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
5043 if (idx >= cb->args[0]) {
5044 err = br_dev->netdev_ops->ndo_bridge_getlink(
5045 skb, portid, seq, dev,
5046 filter_mask, NLM_F_MULTI);
5047 if (err < 0 && err != -EOPNOTSUPP) {
5048 if (likely(skb->len))
5057 if (ops->ndo_bridge_getlink) {
5058 if (idx >= cb->args[0]) {
5059 err = ops->ndo_bridge_getlink(skb, portid,
5063 if (err < 0 && err != -EOPNOTSUPP) {
5064 if (likely(skb->len))
5081 static inline size_t bridge_nlmsg_size(void)
5083 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5084 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5085 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5086 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
5087 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
5088 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
5089 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
5090 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
5091 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
5092 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
5093 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
5096 static int rtnl_bridge_notify(struct net_device *dev)
5098 struct net *net = dev_net(dev);
5099 struct sk_buff *skb;
5100 int err = -EOPNOTSUPP;
5102 if (!dev->netdev_ops->ndo_bridge_getlink)
5105 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
5111 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5115 /* Notification info is only filled for bridge ports, not the bridge
5116 * device itself. Therefore, a zero notification length is valid and
5117 * should not result in an error.
5122 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
5125 WARN_ON(err == -EMSGSIZE);
5128 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
5132 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
5133 struct netlink_ext_ack *extack)
5135 struct net *net = sock_net(skb->sk);
5136 struct ifinfomsg *ifm;
5137 struct net_device *dev;
5138 struct nlattr *br_spec, *attr = NULL;
5139 int rem, err = -EOPNOTSUPP;
5141 bool have_flags = false;
5143 if (nlmsg_len(nlh) < sizeof(*ifm))
5146 ifm = nlmsg_data(nlh);
5147 if (ifm->ifi_family != AF_BRIDGE)
5148 return -EPFNOSUPPORT;
5150 dev = __dev_get_by_index(net, ifm->ifi_index);
5152 NL_SET_ERR_MSG(extack, "unknown ifindex");
5156 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5158 nla_for_each_nested(attr, br_spec, rem) {
5159 if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
5160 if (nla_len(attr) < sizeof(flags))
5164 flags = nla_get_u16(attr);
5167 if (nla_type(attr) == IFLA_BRIDGE_MODE) {
5168 if (nla_len(attr) < sizeof(u16))
5174 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5175 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5177 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
5182 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
5187 flags &= ~BRIDGE_FLAGS_MASTER;
5190 if ((flags & BRIDGE_FLAGS_SELF)) {
5191 if (!dev->netdev_ops->ndo_bridge_setlink)
5194 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
5198 flags &= ~BRIDGE_FLAGS_SELF;
5200 /* Generate event to notify upper layer of bridge
5203 err = rtnl_bridge_notify(dev);
5208 memcpy(nla_data(attr), &flags, sizeof(flags));
5213 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5214 struct netlink_ext_ack *extack)
5216 struct net *net = sock_net(skb->sk);
5217 struct ifinfomsg *ifm;
5218 struct net_device *dev;
5219 struct nlattr *br_spec, *attr = NULL;
5220 int rem, err = -EOPNOTSUPP;
5222 bool have_flags = false;
5224 if (nlmsg_len(nlh) < sizeof(*ifm))
5227 ifm = nlmsg_data(nlh);
5228 if (ifm->ifi_family != AF_BRIDGE)
5229 return -EPFNOSUPPORT;
5231 dev = __dev_get_by_index(net, ifm->ifi_index);
5233 NL_SET_ERR_MSG(extack, "unknown ifindex");
5237 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5239 nla_for_each_nested(attr, br_spec, rem) {
5240 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5241 if (nla_len(attr) < sizeof(flags))
5245 flags = nla_get_u16(attr);
5251 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5252 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5254 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5259 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5263 flags &= ~BRIDGE_FLAGS_MASTER;
5266 if ((flags & BRIDGE_FLAGS_SELF)) {
5267 if (!dev->netdev_ops->ndo_bridge_dellink)
5270 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5274 flags &= ~BRIDGE_FLAGS_SELF;
5276 /* Generate event to notify upper layer of bridge
5279 err = rtnl_bridge_notify(dev);
5284 memcpy(nla_data(attr), &flags, sizeof(flags));
5289 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5291 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5292 (!idxattr || idxattr == attrid);
5296 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
5298 return dev->netdev_ops &&
5299 dev->netdev_ops->ndo_has_offload_stats &&
5300 dev->netdev_ops->ndo_get_offload_stats &&
5301 dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
5305 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5307 return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5308 sizeof(struct rtnl_link_stats64) : 0;
5312 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5313 struct sk_buff *skb)
5315 unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5316 struct nlattr *attr = NULL;
5323 attr = nla_reserve_64bit(skb, attr_id, size,
5324 IFLA_OFFLOAD_XSTATS_UNSPEC);
5328 attr_data = nla_data(attr);
5329 memset(attr_data, 0, size);
5331 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5339 rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
5340 enum netdev_offload_xstats_type type)
5342 bool enabled = netdev_offload_xstats_enabled(dev, type);
5344 return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
5347 struct rtnl_offload_xstats_request_used {
5353 rtnl_offload_xstats_get_stats(struct net_device *dev,
5354 enum netdev_offload_xstats_type type,
5355 struct rtnl_offload_xstats_request_used *ru,
5356 struct rtnl_hw_stats64 *stats,
5357 struct netlink_ext_ack *extack)
5363 request = netdev_offload_xstats_enabled(dev, type);
5369 err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
5375 ru->request = request;
5382 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
5383 struct rtnl_offload_xstats_request_used *ru)
5385 struct nlattr *nest;
5387 nest = nla_nest_start(skb, attr_id);
5391 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
5392 goto nla_put_failure;
5394 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
5395 goto nla_put_failure;
5397 nla_nest_end(skb, nest);
5401 nla_nest_cancel(skb, nest);
5406 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
5407 struct netlink_ext_ack *extack)
5409 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5410 struct rtnl_offload_xstats_request_used ru_l3;
5411 struct nlattr *nest;
5414 err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
5418 nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5422 if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
5423 IFLA_OFFLOAD_XSTATS_L3_STATS,
5425 goto nla_put_failure;
5427 nla_nest_end(skb, nest);
5431 nla_nest_cancel(skb, nest);
5435 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
5436 int *prividx, u32 off_filter_mask,
5437 struct netlink_ext_ack *extack)
5439 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5440 int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
5441 int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5442 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5443 bool have_data = false;
5446 if (*prividx <= attr_id_cpu_hit &&
5448 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5449 err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5452 } else if (err != -ENODATA) {
5453 *prividx = attr_id_cpu_hit;
5458 if (*prividx <= attr_id_hw_s_info &&
5459 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
5460 *prividx = attr_id_hw_s_info;
5462 err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
5470 if (*prividx <= attr_id_l3_stats &&
5471 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
5472 unsigned int size_l3;
5473 struct nlattr *attr;
5475 *prividx = attr_id_l3_stats;
5477 size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5480 attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
5481 IFLA_OFFLOAD_XSTATS_UNSPEC);
5485 err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
5486 nla_data(attr), extack);
5503 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
5504 enum netdev_offload_xstats_type type)
5506 bool enabled = netdev_offload_xstats_enabled(dev, type);
5508 return nla_total_size(0) +
5509 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
5510 nla_total_size(sizeof(u8)) +
5511 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5512 (enabled ? nla_total_size(sizeof(u8)) : 0) +
5517 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
5519 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5521 return nla_total_size(0) +
5522 /* IFLA_OFFLOAD_XSTATS_L3_STATS */
5523 rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
5527 static int rtnl_offload_xstats_get_size(const struct net_device *dev,
5528 u32 off_filter_mask)
5530 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5531 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5535 if (off_filter_mask &
5536 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5537 size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
5538 nla_size += nla_total_size_64bit(size);
5541 if (off_filter_mask &
5542 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
5543 nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
5545 if (off_filter_mask &
5546 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
5547 size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5548 nla_size += nla_total_size_64bit(size);
5552 nla_size += nla_total_size(0);
5557 struct rtnl_stats_dump_filters {
5558 /* mask[0] filters outer attributes. Then individual nests have their
5559 * filtering mask at the index of the nested attribute.
5561 u32 mask[IFLA_STATS_MAX + 1];
5564 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5565 int type, u32 pid, u32 seq, u32 change,
5567 const struct rtnl_stats_dump_filters *filters,
5568 int *idxattr, int *prividx,
5569 struct netlink_ext_ack *extack)
5571 unsigned int filter_mask = filters->mask[0];
5572 struct if_stats_msg *ifsm;
5573 struct nlmsghdr *nlh;
5574 struct nlattr *attr;
5575 int s_prividx = *prividx;
5580 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5584 ifsm = nlmsg_data(nlh);
5585 ifsm->family = PF_UNSPEC;
5588 ifsm->ifindex = dev->ifindex;
5589 ifsm->filter_mask = filter_mask;
5591 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5592 struct rtnl_link_stats64 *sp;
5594 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5595 sizeof(struct rtnl_link_stats64),
5599 goto nla_put_failure;
5602 sp = nla_data(attr);
5603 dev_get_stats(dev, sp);
5606 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5607 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5609 if (ops && ops->fill_linkxstats) {
5610 *idxattr = IFLA_STATS_LINK_XSTATS;
5611 attr = nla_nest_start_noflag(skb,
5612 IFLA_STATS_LINK_XSTATS);
5615 goto nla_put_failure;
5618 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5619 nla_nest_end(skb, attr);
5621 goto nla_put_failure;
5626 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5628 const struct rtnl_link_ops *ops = NULL;
5629 const struct net_device *master;
5631 master = netdev_master_upper_dev_get(dev);
5633 ops = master->rtnl_link_ops;
5634 if (ops && ops->fill_linkxstats) {
5635 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5636 attr = nla_nest_start_noflag(skb,
5637 IFLA_STATS_LINK_XSTATS_SLAVE);
5640 goto nla_put_failure;
5643 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5644 nla_nest_end(skb, attr);
5646 goto nla_put_failure;
5651 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5653 u32 off_filter_mask;
5655 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5656 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5657 attr = nla_nest_start_noflag(skb,
5658 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5661 goto nla_put_failure;
5664 err = rtnl_offload_xstats_fill(skb, dev, prividx,
5665 off_filter_mask, extack);
5666 if (err == -ENODATA)
5667 nla_nest_cancel(skb, attr);
5669 nla_nest_end(skb, attr);
5671 if (err && err != -ENODATA)
5672 goto nla_put_failure;
5676 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5677 struct rtnl_af_ops *af_ops;
5679 *idxattr = IFLA_STATS_AF_SPEC;
5680 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5683 goto nla_put_failure;
5687 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5688 if (af_ops->fill_stats_af) {
5691 af = nla_nest_start_noflag(skb,
5696 goto nla_put_failure;
5698 err = af_ops->fill_stats_af(skb, dev);
5700 if (err == -ENODATA) {
5701 nla_nest_cancel(skb, af);
5702 } else if (err < 0) {
5704 goto nla_put_failure;
5707 nla_nest_end(skb, af);
5712 nla_nest_end(skb, attr);
5717 nlmsg_end(skb, nlh);
5722 /* not a multi message or no progress mean a real error */
5723 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5724 nlmsg_cancel(skb, nlh);
5726 nlmsg_end(skb, nlh);
5731 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5732 const struct rtnl_stats_dump_filters *filters)
5734 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5735 unsigned int filter_mask = filters->mask[0];
5737 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5738 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5740 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5741 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5742 int attr = IFLA_STATS_LINK_XSTATS;
5744 if (ops && ops->get_linkxstats_size) {
5745 size += nla_total_size(ops->get_linkxstats_size(dev,
5747 /* for IFLA_STATS_LINK_XSTATS */
5748 size += nla_total_size(0);
5752 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5753 struct net_device *_dev = (struct net_device *)dev;
5754 const struct rtnl_link_ops *ops = NULL;
5755 const struct net_device *master;
5757 /* netdev_master_upper_dev_get can't take const */
5758 master = netdev_master_upper_dev_get(_dev);
5760 ops = master->rtnl_link_ops;
5761 if (ops && ops->get_linkxstats_size) {
5762 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5764 size += nla_total_size(ops->get_linkxstats_size(dev,
5766 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5767 size += nla_total_size(0);
5771 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
5772 u32 off_filter_mask;
5774 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5775 size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
5778 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5779 struct rtnl_af_ops *af_ops;
5781 /* for IFLA_STATS_AF_SPEC */
5782 size += nla_total_size(0);
5785 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5786 if (af_ops->get_stats_af_size) {
5787 size += nla_total_size(
5788 af_ops->get_stats_af_size(dev));
5791 size += nla_total_size(0);
5800 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
5802 static const struct nla_policy
5803 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
5804 [IFLA_STATS_LINK_OFFLOAD_XSTATS] =
5805 NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
5808 static const struct nla_policy
5809 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
5810 [IFLA_STATS_GET_FILTERS] =
5811 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
5814 static const struct nla_policy
5815 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
5816 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
5819 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
5820 struct rtnl_stats_dump_filters *filters,
5821 struct netlink_ext_ack *extack)
5823 struct nlattr *tb[IFLA_STATS_MAX + 1];
5827 err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
5828 rtnl_stats_get_policy_filters, extack);
5832 for (at = 1; at <= IFLA_STATS_MAX; at++) {
5834 if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
5835 NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
5838 filters->mask[at] = nla_get_u32(tb[at]);
5845 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
5847 struct rtnl_stats_dump_filters *filters,
5848 struct netlink_ext_ack *extack)
5850 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5854 filters->mask[0] = filter_mask;
5855 for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
5856 filters->mask[i] = -1U;
5858 err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
5859 IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
5863 if (tb[IFLA_STATS_GET_FILTERS]) {
5864 err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
5873 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5874 bool is_dump, struct netlink_ext_ack *extack)
5876 struct if_stats_msg *ifsm;
5878 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5879 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5886 ifsm = nlmsg_data(nlh);
5888 /* only requests using strict checks can pass data to influence
5889 * the dump. The legacy exception is filter_mask.
5891 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5892 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5895 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5896 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5903 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5904 struct netlink_ext_ack *extack)
5906 struct rtnl_stats_dump_filters filters;
5907 struct net *net = sock_net(skb->sk);
5908 struct net_device *dev = NULL;
5909 int idxattr = 0, prividx = 0;
5910 struct if_stats_msg *ifsm;
5911 struct sk_buff *nskb;
5914 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5919 ifsm = nlmsg_data(nlh);
5920 if (ifsm->ifindex > 0)
5921 dev = __dev_get_by_index(net, ifsm->ifindex);
5928 if (!ifsm->filter_mask) {
5929 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
5933 err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
5937 nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
5941 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5942 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5943 0, &filters, &idxattr, &prividx, extack);
5945 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5946 WARN_ON(err == -EMSGSIZE);
5949 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5955 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5957 struct netlink_ext_ack *extack = cb->extack;
5958 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5959 struct rtnl_stats_dump_filters filters;
5960 struct net *net = sock_net(skb->sk);
5961 unsigned int flags = NLM_F_MULTI;
5962 struct if_stats_msg *ifsm;
5963 struct hlist_head *head;
5964 struct net_device *dev;
5968 s_idx = cb->args[1];
5969 s_idxattr = cb->args[2];
5970 s_prividx = cb->args[3];
5972 cb->seq = net->dev_base_seq;
5974 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5978 ifsm = nlmsg_data(cb->nlh);
5979 if (!ifsm->filter_mask) {
5980 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5984 err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
5989 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5991 head = &net->dev_index_head[h];
5992 hlist_for_each_entry(dev, head, index_hlist) {
5995 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5996 NETLINK_CB(cb->skb).portid,
5997 cb->nlh->nlmsg_seq, 0,
5999 &s_idxattr, &s_prividx,
6001 /* If we ran out of room on the first message,
6004 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
6010 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6016 cb->args[3] = s_prividx;
6017 cb->args[2] = s_idxattr;
6024 void rtnl_offload_xstats_notify(struct net_device *dev)
6026 struct rtnl_stats_dump_filters response_filters = {};
6027 struct net *net = dev_net(dev);
6028 int idxattr = 0, prividx = 0;
6029 struct sk_buff *skb;
6034 response_filters.mask[0] |=
6035 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
6036 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
6037 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
6039 skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
6044 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
6045 &response_filters, &idxattr, &prividx, NULL);
6051 rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
6055 rtnl_set_sk_err(net, RTNLGRP_STATS, err);
6057 EXPORT_SYMBOL(rtnl_offload_xstats_notify);
6059 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
6060 struct netlink_ext_ack *extack)
6062 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
6063 struct rtnl_stats_dump_filters response_filters = {};
6064 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
6065 struct net *net = sock_net(skb->sk);
6066 struct net_device *dev = NULL;
6067 struct if_stats_msg *ifsm;
6068 bool notify = false;
6071 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
6076 ifsm = nlmsg_data(nlh);
6077 if (ifsm->family != AF_UNSPEC) {
6078 NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
6082 if (ifsm->ifindex > 0)
6083 dev = __dev_get_by_index(net, ifsm->ifindex);
6090 if (ifsm->filter_mask) {
6091 NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
6095 err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
6096 ifla_stats_set_policy, extack);
6100 if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
6101 u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
6104 err = netdev_offload_xstats_enable(dev, t_l3, extack);
6106 err = netdev_offload_xstats_disable(dev, t_l3);
6110 else if (err != -EALREADY)
6113 response_filters.mask[0] |=
6114 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
6115 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
6116 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
6120 rtnl_offload_xstats_notify(dev);
6125 static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh,
6126 struct netlink_ext_ack *extack)
6128 struct br_port_msg *bpm;
6130 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
6131 NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request");
6135 bpm = nlmsg_data(nlh);
6137 NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request");
6140 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
6141 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
6148 struct rtnl_mdb_dump_ctx {
6152 static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
6154 struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx;
6155 struct net *net = sock_net(skb->sk);
6156 struct net_device *dev;
6160 NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx);
6162 if (cb->strict_check) {
6163 err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack);
6171 for_each_netdev(net, dev) {
6174 if (!dev->netdev_ops->ndo_mdb_dump)
6177 err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb);
6178 if (err == -EMSGSIZE)
6180 /* Moving on to next device, reset markers and sequence
6181 * counters since they are all maintained per-device.
6183 memset(cb->ctx, 0, sizeof(cb->ctx));
6195 static int rtnl_validate_mdb_entry(const struct nlattr *attr,
6196 struct netlink_ext_ack *extack)
6198 struct br_mdb_entry *entry = nla_data(attr);
6200 if (nla_len(attr) != sizeof(struct br_mdb_entry)) {
6201 NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length");
6205 if (entry->ifindex == 0) {
6206 NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed");
6210 if (entry->addr.proto == htons(ETH_P_IP)) {
6211 if (!ipv4_is_multicast(entry->addr.u.ip4) &&
6212 !ipv4_is_zeronet(entry->addr.u.ip4)) {
6213 NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0");
6216 if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
6217 NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast");
6220 #if IS_ENABLED(CONFIG_IPV6)
6221 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
6222 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
6223 NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes");
6227 } else if (entry->addr.proto == 0) {
6229 if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
6230 NL_SET_ERR_MSG(extack, "L2 entry group is not multicast");
6234 NL_SET_ERR_MSG(extack, "Unknown entry protocol");
6238 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
6239 NL_SET_ERR_MSG(extack, "Unknown entry state");
6242 if (entry->vid >= VLAN_VID_MASK) {
6243 NL_SET_ERR_MSG(extack, "Invalid entry VLAN id");
6250 static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = {
6251 [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 },
6252 [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
6253 rtnl_validate_mdb_entry,
6254 sizeof(struct br_mdb_entry)),
6255 [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED },
6258 static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
6259 struct netlink_ext_ack *extack)
6261 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6262 struct net *net = sock_net(skb->sk);
6263 struct br_port_msg *bpm;
6264 struct net_device *dev;
6267 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6268 MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6272 bpm = nlmsg_data(nlh);
6273 if (!bpm->ifindex) {
6274 NL_SET_ERR_MSG(extack, "Invalid ifindex");
6278 dev = __dev_get_by_index(net, bpm->ifindex);
6280 NL_SET_ERR_MSG(extack, "Device doesn't exist");
6284 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6285 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6289 if (!dev->netdev_ops->ndo_mdb_add) {
6290 NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6294 return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack);
6297 static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
6298 struct netlink_ext_ack *extack)
6300 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6301 struct net *net = sock_net(skb->sk);
6302 struct br_port_msg *bpm;
6303 struct net_device *dev;
6306 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6307 MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6311 bpm = nlmsg_data(nlh);
6312 if (!bpm->ifindex) {
6313 NL_SET_ERR_MSG(extack, "Invalid ifindex");
6317 dev = __dev_get_by_index(net, bpm->ifindex);
6319 NL_SET_ERR_MSG(extack, "Device doesn't exist");
6323 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6324 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6328 if (!dev->netdev_ops->ndo_mdb_del) {
6329 NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6333 return dev->netdev_ops->ndo_mdb_del(dev, tb, extack);
6336 /* Process one rtnetlink message. */
6338 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
6339 struct netlink_ext_ack *extack)
6341 struct net *net = sock_net(skb->sk);
6342 struct rtnl_link *link;
6343 enum rtnl_kinds kind;
6344 struct module *owner;
6345 int err = -EOPNOTSUPP;
6346 rtnl_doit_func doit;
6351 type = nlh->nlmsg_type;
6357 /* All the messages must have at least 1 byte length */
6358 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
6361 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
6362 kind = rtnl_msgtype_kind(type);
6364 if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
6368 if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
6370 rtnl_dumpit_func dumpit;
6371 u32 min_dump_alloc = 0;
6373 link = rtnl_get_link(family, type);
6374 if (!link || !link->dumpit) {
6376 link = rtnl_get_link(family, type);
6377 if (!link || !link->dumpit)
6380 owner = link->owner;
6381 dumpit = link->dumpit;
6383 if (type == RTM_GETLINK - RTM_BASE)
6384 min_dump_alloc = rtnl_calcit(skb, nlh);
6387 /* need to do this before rcu_read_unlock() */
6388 if (!try_module_get(owner))
6389 err = -EPROTONOSUPPORT;
6395 struct netlink_dump_control c = {
6397 .min_dump_alloc = min_dump_alloc,
6400 err = netlink_dump_start(rtnl, skb, nlh, &c);
6401 /* netlink_dump_start() will keep a reference on
6402 * module if dump is still in progress.
6409 link = rtnl_get_link(family, type);
6410 if (!link || !link->doit) {
6412 link = rtnl_get_link(PF_UNSPEC, type);
6413 if (!link || !link->doit)
6417 owner = link->owner;
6418 if (!try_module_get(owner)) {
6419 err = -EPROTONOSUPPORT;
6423 flags = link->flags;
6424 if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6425 !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6426 NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6431 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6435 err = doit(skb, nlh, extack);
6442 link = rtnl_get_link(family, type);
6443 if (link && link->doit)
6444 err = link->doit(skb, nlh, extack);
6460 static void rtnetlink_rcv(struct sk_buff *skb)
6462 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
6465 static int rtnetlink_bind(struct net *net, int group)
6468 case RTNLGRP_IPV4_MROUTE_R:
6469 case RTNLGRP_IPV6_MROUTE_R:
6470 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
6477 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
6479 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6483 case NETDEV_CHANGEMTU:
6484 case NETDEV_CHANGEADDR:
6485 case NETDEV_CHANGENAME:
6486 case NETDEV_FEAT_CHANGE:
6487 case NETDEV_BONDING_FAILOVER:
6488 case NETDEV_POST_TYPE_CHANGE:
6489 case NETDEV_NOTIFY_PEERS:
6490 case NETDEV_CHANGEUPPER:
6491 case NETDEV_RESEND_IGMP:
6492 case NETDEV_CHANGEINFODATA:
6493 case NETDEV_CHANGELOWERSTATE:
6494 case NETDEV_CHANGE_TX_QUEUE_LEN:
6495 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6496 GFP_KERNEL, NULL, 0, 0, NULL);
6504 static struct notifier_block rtnetlink_dev_notifier = {
6505 .notifier_call = rtnetlink_event,
6509 static int __net_init rtnetlink_net_init(struct net *net)
6512 struct netlink_kernel_cfg cfg = {
6513 .groups = RTNLGRP_MAX,
6514 .input = rtnetlink_rcv,
6515 .cb_mutex = &rtnl_mutex,
6516 .flags = NL_CFG_F_NONROOT_RECV,
6517 .bind = rtnetlink_bind,
6520 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
6527 static void __net_exit rtnetlink_net_exit(struct net *net)
6529 netlink_kernel_release(net->rtnl);
6533 static struct pernet_operations rtnetlink_net_ops = {
6534 .init = rtnetlink_net_init,
6535 .exit = rtnetlink_net_exit,
6538 void __init rtnetlink_init(void)
6540 if (register_pernet_subsys(&rtnetlink_net_ops))
6541 panic("rtnetlink_init: cannot initialize rtnetlink\n");
6543 register_netdevice_notifier(&rtnetlink_dev_notifier);
6545 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6546 rtnl_dump_ifinfo, 0);
6547 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6548 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6549 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6551 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6552 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6553 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6555 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
6556 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
6558 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6559 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
6560 RTNL_FLAG_BULK_DEL_SUPPORTED);
6561 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6563 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6564 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6565 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6567 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6569 rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6571 rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0);
6572 rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
6573 rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0);