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 43
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(0) + /* nest IFLA_VF_STATS */
965 /* IFLA_VF_STATS_RX_PACKETS */
966 nla_total_size_64bit(sizeof(__u64)) +
967 /* IFLA_VF_STATS_TX_PACKETS */
968 nla_total_size_64bit(sizeof(__u64)) +
969 /* IFLA_VF_STATS_RX_BYTES */
970 nla_total_size_64bit(sizeof(__u64)) +
971 /* IFLA_VF_STATS_TX_BYTES */
972 nla_total_size_64bit(sizeof(__u64)) +
973 /* IFLA_VF_STATS_BROADCAST */
974 nla_total_size_64bit(sizeof(__u64)) +
975 /* IFLA_VF_STATS_MULTICAST */
976 nla_total_size_64bit(sizeof(__u64)) +
977 /* IFLA_VF_STATS_RX_DROPPED */
978 nla_total_size_64bit(sizeof(__u64)) +
979 /* IFLA_VF_STATS_TX_DROPPED */
980 nla_total_size_64bit(sizeof(__u64)) +
981 nla_total_size(sizeof(struct ifla_vf_trust)));
987 static size_t rtnl_port_size(const struct net_device *dev,
990 size_t port_size = nla_total_size(4) /* PORT_VF */
991 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
992 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
993 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
994 + nla_total_size(1) /* PROT_VDP_REQUEST */
995 + nla_total_size(2); /* PORT_VDP_RESPONSE */
996 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
997 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
999 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
1002 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1003 !(ext_filter_mask & RTEXT_FILTER_VF))
1005 if (dev_num_vf(dev->dev.parent))
1006 return port_self_size + vf_ports_size +
1007 vf_port_size * dev_num_vf(dev->dev.parent);
1009 return port_self_size;
1012 static size_t rtnl_xdp_size(void)
1014 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
1015 nla_total_size(1) + /* XDP_ATTACHED */
1016 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
1017 nla_total_size(4); /* XDP_<mode>_PROG_ID */
1022 static size_t rtnl_prop_list_size(const struct net_device *dev)
1024 struct netdev_name_node *name_node;
1027 if (list_empty(&dev->name_node->list))
1029 size = nla_total_size(0);
1030 list_for_each_entry(name_node, &dev->name_node->list, list)
1031 size += nla_total_size(ALTIFNAMSIZ);
1035 static size_t rtnl_proto_down_size(const struct net_device *dev)
1037 size_t size = nla_total_size(1);
1039 if (dev->proto_down_reason)
1040 size += nla_total_size(0) + nla_total_size(4);
1045 static size_t rtnl_devlink_port_size(const struct net_device *dev)
1047 size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
1049 if (dev->devlink_port)
1050 size += devlink_nl_port_handle_size(dev->devlink_port);
1055 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1056 u32 ext_filter_mask)
1058 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1059 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1060 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1061 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1062 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1063 + nla_total_size(sizeof(struct rtnl_link_stats))
1064 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1065 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1066 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1067 + nla_total_size(4) /* IFLA_TXQLEN */
1068 + nla_total_size(4) /* IFLA_WEIGHT */
1069 + nla_total_size(4) /* IFLA_MTU */
1070 + nla_total_size(4) /* IFLA_LINK */
1071 + nla_total_size(4) /* IFLA_MASTER */
1072 + nla_total_size(1) /* IFLA_CARRIER */
1073 + nla_total_size(4) /* IFLA_PROMISCUITY */
1074 + nla_total_size(4) /* IFLA_ALLMULTI */
1075 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1076 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1077 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1078 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1079 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1080 + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
1081 + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
1082 + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
1083 + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1084 + nla_total_size(1) /* IFLA_OPERSTATE */
1085 + nla_total_size(1) /* IFLA_LINKMODE */
1086 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1087 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1088 + nla_total_size(4) /* IFLA_GROUP */
1089 + nla_total_size(ext_filter_mask
1090 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1091 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1092 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1093 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1094 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1095 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1096 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1097 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1098 + rtnl_xdp_size() /* IFLA_XDP */
1099 + nla_total_size(4) /* IFLA_EVENT */
1100 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1101 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1102 + rtnl_proto_down_size(dev) /* proto down */
1103 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1104 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1105 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1106 + nla_total_size(4) /* IFLA_MIN_MTU */
1107 + nla_total_size(4) /* IFLA_MAX_MTU */
1108 + rtnl_prop_list_size(dev)
1109 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1110 + rtnl_devlink_port_size(dev)
1114 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1116 struct nlattr *vf_ports;
1117 struct nlattr *vf_port;
1121 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1125 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1126 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1128 goto nla_put_failure;
1129 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1130 goto nla_put_failure;
1131 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1132 if (err == -EMSGSIZE)
1133 goto nla_put_failure;
1135 nla_nest_cancel(skb, vf_port);
1138 nla_nest_end(skb, vf_port);
1141 nla_nest_end(skb, vf_ports);
1146 nla_nest_cancel(skb, vf_ports);
1150 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1152 struct nlattr *port_self;
1155 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1159 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1161 nla_nest_cancel(skb, port_self);
1162 return (err == -EMSGSIZE) ? err : 0;
1165 nla_nest_end(skb, port_self);
1170 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1171 u32 ext_filter_mask)
1175 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1176 !(ext_filter_mask & RTEXT_FILTER_VF))
1179 err = rtnl_port_self_fill(skb, dev);
1183 if (dev_num_vf(dev->dev.parent)) {
1184 err = rtnl_vf_ports_fill(skb, dev);
1192 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1195 struct netdev_phys_item_id ppid;
1197 err = dev_get_phys_port_id(dev, &ppid);
1199 if (err == -EOPNOTSUPP)
1204 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1210 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1212 char name[IFNAMSIZ];
1215 err = dev_get_phys_port_name(dev, name, sizeof(name));
1217 if (err == -EOPNOTSUPP)
1222 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1228 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1230 struct netdev_phys_item_id ppid = { };
1233 err = dev_get_port_parent_id(dev, &ppid, false);
1235 if (err == -EOPNOTSUPP)
1240 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1246 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1247 struct net_device *dev)
1249 struct rtnl_link_stats64 *sp;
1250 struct nlattr *attr;
1252 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1253 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1257 sp = nla_data(attr);
1258 dev_get_stats(dev, sp);
1260 attr = nla_reserve(skb, IFLA_STATS,
1261 sizeof(struct rtnl_link_stats));
1265 copy_rtnl_link_stats(nla_data(attr), sp);
1270 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1271 struct net_device *dev,
1273 struct nlattr *vfinfo)
1275 struct ifla_vf_rss_query_en vf_rss_query_en;
1276 struct nlattr *vf, *vfstats, *vfvlanlist;
1277 struct ifla_vf_link_state vf_linkstate;
1278 struct ifla_vf_vlan_info vf_vlan_info;
1279 struct ifla_vf_spoofchk vf_spoofchk;
1280 struct ifla_vf_tx_rate vf_tx_rate;
1281 struct ifla_vf_stats vf_stats;
1282 struct ifla_vf_trust vf_trust;
1283 struct ifla_vf_vlan vf_vlan;
1284 struct ifla_vf_rate vf_rate;
1285 struct ifla_vf_mac vf_mac;
1286 struct ifla_vf_broadcast vf_broadcast;
1287 struct ifla_vf_info ivi;
1288 struct ifla_vf_guid node_guid;
1289 struct ifla_vf_guid port_guid;
1291 memset(&ivi, 0, sizeof(ivi));
1293 /* Not all SR-IOV capable drivers support the
1294 * spoofcheck and "RSS query enable" query. Preset to
1295 * -1 so the user space tool can detect that the driver
1296 * didn't report anything.
1299 ivi.rss_query_en = -1;
1301 /* The default value for VF link state is "auto"
1302 * IFLA_VF_LINK_STATE_AUTO which equals zero
1305 /* VLAN Protocol by default is 802.1Q */
1306 ivi.vlan_proto = htons(ETH_P_8021Q);
1307 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1310 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1311 memset(&node_guid, 0, sizeof(node_guid));
1312 memset(&port_guid, 0, sizeof(port_guid));
1321 vf_rss_query_en.vf =
1324 port_guid.vf = ivi.vf;
1326 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1327 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1328 vf_vlan.vlan = ivi.vlan;
1329 vf_vlan.qos = ivi.qos;
1330 vf_vlan_info.vlan = ivi.vlan;
1331 vf_vlan_info.qos = ivi.qos;
1332 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1333 vf_tx_rate.rate = ivi.max_tx_rate;
1334 vf_rate.min_tx_rate = ivi.min_tx_rate;
1335 vf_rate.max_tx_rate = ivi.max_tx_rate;
1336 vf_spoofchk.setting = ivi.spoofchk;
1337 vf_linkstate.link_state = ivi.linkstate;
1338 vf_rss_query_en.setting = ivi.rss_query_en;
1339 vf_trust.setting = ivi.trusted;
1340 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1342 goto nla_put_vfinfo_failure;
1343 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1344 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1345 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1346 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1348 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1350 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1352 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1354 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1355 sizeof(vf_rss_query_en),
1356 &vf_rss_query_en) ||
1357 nla_put(skb, IFLA_VF_TRUST,
1358 sizeof(vf_trust), &vf_trust))
1359 goto nla_put_vf_failure;
1361 if (dev->netdev_ops->ndo_get_vf_guid &&
1362 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1364 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1366 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1368 goto nla_put_vf_failure;
1370 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1372 goto nla_put_vf_failure;
1373 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1375 nla_nest_cancel(skb, vfvlanlist);
1376 goto nla_put_vf_failure;
1378 nla_nest_end(skb, vfvlanlist);
1379 memset(&vf_stats, 0, sizeof(vf_stats));
1380 if (dev->netdev_ops->ndo_get_vf_stats)
1381 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1383 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1385 goto nla_put_vf_failure;
1386 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1387 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1388 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1389 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1390 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1391 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1392 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1393 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1394 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1395 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1396 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1397 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1398 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1399 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1400 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1401 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1402 nla_nest_cancel(skb, vfstats);
1403 goto nla_put_vf_failure;
1405 nla_nest_end(skb, vfstats);
1406 nla_nest_end(skb, vf);
1410 nla_nest_cancel(skb, vf);
1411 nla_put_vfinfo_failure:
1412 nla_nest_cancel(skb, vfinfo);
1416 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1417 struct net_device *dev,
1418 u32 ext_filter_mask)
1420 struct nlattr *vfinfo;
1423 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1426 num_vfs = dev_num_vf(dev->dev.parent);
1427 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1430 if (!dev->netdev_ops->ndo_get_vf_config)
1433 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1437 for (i = 0; i < num_vfs; i++) {
1438 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1442 nla_nest_end(skb, vfinfo);
1446 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1448 struct rtnl_link_ifmap map;
1450 memset(&map, 0, sizeof(map));
1451 map.mem_start = dev->mem_start;
1452 map.mem_end = dev->mem_end;
1453 map.base_addr = dev->base_addr;
1456 map.port = dev->if_port;
1458 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1464 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1466 const struct bpf_prog *generic_xdp_prog;
1470 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1471 if (!generic_xdp_prog)
1473 return generic_xdp_prog->aux->id;
1476 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1478 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1481 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1483 return dev_xdp_prog_id(dev, XDP_MODE_HW);
1486 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1487 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1488 u32 (*get_prog_id)(struct net_device *dev))
1493 curr_id = get_prog_id(dev);
1498 err = nla_put_u32(skb, attr, curr_id);
1502 if (*mode != XDP_ATTACHED_NONE)
1503 *mode = XDP_ATTACHED_MULTI;
1510 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1517 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1522 mode = XDP_ATTACHED_NONE;
1523 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1524 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1527 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1528 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1531 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1532 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1536 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1540 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1541 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1546 nla_nest_end(skb, xdp);
1550 nla_nest_cancel(skb, xdp);
1554 static u32 rtnl_get_event(unsigned long event)
1556 u32 rtnl_event_type = IFLA_EVENT_NONE;
1560 rtnl_event_type = IFLA_EVENT_REBOOT;
1562 case NETDEV_FEAT_CHANGE:
1563 rtnl_event_type = IFLA_EVENT_FEATURES;
1565 case NETDEV_BONDING_FAILOVER:
1566 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1568 case NETDEV_NOTIFY_PEERS:
1569 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1571 case NETDEV_RESEND_IGMP:
1572 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1574 case NETDEV_CHANGEINFODATA:
1575 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1581 return rtnl_event_type;
1584 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1586 const struct net_device *upper_dev;
1591 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1593 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1599 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1602 int ifindex = dev_get_iflink(dev);
1604 if (force || dev->ifindex != ifindex)
1605 return nla_put_u32(skb, IFLA_LINK, ifindex);
1610 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1611 struct net_device *dev)
1616 ret = dev_get_alias(dev, buf, sizeof(buf));
1617 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1620 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1621 const struct net_device *dev,
1622 struct net *src_net, gfp_t gfp)
1624 bool put_iflink = false;
1626 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1627 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1629 if (!net_eq(dev_net(dev), link_net)) {
1630 int id = peernet2id_alloc(src_net, link_net, gfp);
1632 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1639 return nla_put_iflink(skb, dev, put_iflink);
1642 static int rtnl_fill_link_af(struct sk_buff *skb,
1643 const struct net_device *dev,
1644 u32 ext_filter_mask)
1646 const struct rtnl_af_ops *af_ops;
1647 struct nlattr *af_spec;
1649 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1653 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1657 if (!af_ops->fill_link_af)
1660 af = nla_nest_start_noflag(skb, af_ops->family);
1664 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1666 * Caller may return ENODATA to indicate that there
1667 * was no data to be dumped. This is not an error, it
1668 * means we should trim the attribute header and
1671 if (err == -ENODATA)
1672 nla_nest_cancel(skb, af);
1676 nla_nest_end(skb, af);
1679 nla_nest_end(skb, af_spec);
1683 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1684 const struct net_device *dev)
1686 struct netdev_name_node *name_node;
1689 list_for_each_entry(name_node, &dev->name_node->list, list) {
1690 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1697 static int rtnl_fill_prop_list(struct sk_buff *skb,
1698 const struct net_device *dev)
1700 struct nlattr *prop_list;
1703 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1707 ret = rtnl_fill_alt_ifnames(skb, dev);
1711 nla_nest_end(skb, prop_list);
1715 nla_nest_cancel(skb, prop_list);
1719 static int rtnl_fill_proto_down(struct sk_buff *skb,
1720 const struct net_device *dev)
1725 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1726 goto nla_put_failure;
1728 preason = dev->proto_down_reason;
1732 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1736 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1737 nla_nest_cancel(skb, pr);
1738 goto nla_put_failure;
1741 nla_nest_end(skb, pr);
1748 static int rtnl_fill_devlink_port(struct sk_buff *skb,
1749 const struct net_device *dev)
1751 struct nlattr *devlink_port_nest;
1754 devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
1755 if (!devlink_port_nest)
1758 if (dev->devlink_port) {
1759 ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
1764 nla_nest_end(skb, devlink_port_nest);
1768 nla_nest_cancel(skb, devlink_port_nest);
1772 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1773 struct net_device *dev, struct net *src_net,
1774 int type, u32 pid, u32 seq, u32 change,
1775 unsigned int flags, u32 ext_filter_mask,
1776 u32 event, int *new_nsid, int new_ifindex,
1777 int tgt_netnsid, gfp_t gfp)
1779 struct ifinfomsg *ifm;
1780 struct nlmsghdr *nlh;
1781 struct Qdisc *qdisc;
1784 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1788 ifm = nlmsg_data(nlh);
1789 ifm->ifi_family = AF_UNSPEC;
1791 ifm->ifi_type = dev->type;
1792 ifm->ifi_index = dev->ifindex;
1793 ifm->ifi_flags = dev_get_flags(dev);
1794 ifm->ifi_change = change;
1796 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1797 goto nla_put_failure;
1799 qdisc = rtnl_dereference(dev->qdisc);
1800 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1801 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1802 nla_put_u8(skb, IFLA_OPERSTATE,
1803 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1804 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1805 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1806 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1807 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1808 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1809 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1810 nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
1811 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1812 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1813 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1814 nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
1815 nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) ||
1816 nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) ||
1817 nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
1818 nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
1820 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1822 put_master_ifindex(skb, dev) ||
1823 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1825 nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1826 nla_put_ifalias(skb, dev) ||
1827 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1828 atomic_read(&dev->carrier_up_count) +
1829 atomic_read(&dev->carrier_down_count)) ||
1830 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1831 atomic_read(&dev->carrier_up_count)) ||
1832 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1833 atomic_read(&dev->carrier_down_count)))
1834 goto nla_put_failure;
1836 if (rtnl_fill_proto_down(skb, dev))
1837 goto nla_put_failure;
1839 if (event != IFLA_EVENT_NONE) {
1840 if (nla_put_u32(skb, IFLA_EVENT, event))
1841 goto nla_put_failure;
1844 if (rtnl_fill_link_ifmap(skb, dev))
1845 goto nla_put_failure;
1847 if (dev->addr_len) {
1848 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1849 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1850 goto nla_put_failure;
1853 if (rtnl_phys_port_id_fill(skb, dev))
1854 goto nla_put_failure;
1856 if (rtnl_phys_port_name_fill(skb, dev))
1857 goto nla_put_failure;
1859 if (rtnl_phys_switch_id_fill(skb, dev))
1860 goto nla_put_failure;
1862 if (rtnl_fill_stats(skb, dev))
1863 goto nla_put_failure;
1865 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1866 goto nla_put_failure;
1868 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1869 goto nla_put_failure;
1871 if (rtnl_xdp_fill(skb, dev))
1872 goto nla_put_failure;
1874 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1875 if (rtnl_link_fill(skb, dev) < 0)
1876 goto nla_put_failure;
1879 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1880 goto nla_put_failure;
1883 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1884 goto nla_put_failure;
1886 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1887 goto nla_put_failure;
1889 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1890 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1891 goto nla_put_failure;
1894 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1895 goto nla_put_failure_rcu;
1898 if (rtnl_fill_prop_list(skb, dev))
1899 goto nla_put_failure;
1901 if (dev->dev.parent &&
1902 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1903 dev_name(dev->dev.parent)))
1904 goto nla_put_failure;
1906 if (dev->dev.parent && dev->dev.parent->bus &&
1907 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1908 dev->dev.parent->bus->name))
1909 goto nla_put_failure;
1911 if (rtnl_fill_devlink_port(skb, dev))
1912 goto nla_put_failure;
1914 nlmsg_end(skb, nlh);
1917 nla_put_failure_rcu:
1920 nlmsg_cancel(skb, nlh);
1924 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1925 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1926 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1927 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1928 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1929 [IFLA_MTU] = { .type = NLA_U32 },
1930 [IFLA_LINK] = { .type = NLA_U32 },
1931 [IFLA_MASTER] = { .type = NLA_U32 },
1932 [IFLA_CARRIER] = { .type = NLA_U8 },
1933 [IFLA_TXQLEN] = { .type = NLA_U32 },
1934 [IFLA_WEIGHT] = { .type = NLA_U32 },
1935 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1936 [IFLA_LINKMODE] = { .type = NLA_U8 },
1937 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1938 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1939 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1940 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1941 * allow 0-length string (needed to remove an alias).
1943 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1944 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1945 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1946 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1947 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1948 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1949 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1950 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1951 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1952 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1953 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1954 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1955 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1956 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1957 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1958 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1959 [IFLA_XDP] = { .type = NLA_NESTED },
1960 [IFLA_EVENT] = { .type = NLA_U32 },
1961 [IFLA_GROUP] = { .type = NLA_U32 },
1962 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1963 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1964 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1965 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1966 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1967 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1968 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1969 .len = ALTIFNAMSIZ - 1 },
1970 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1971 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1972 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
1973 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
1974 [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 },
1975 [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT },
1976 [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT },
1977 [IFLA_ALLMULTI] = { .type = NLA_REJECT },
1978 [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 },
1979 [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 },
1982 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1983 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1984 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1985 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1986 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1989 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1990 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1991 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1992 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1993 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1994 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1995 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1996 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1997 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1998 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1999 [IFLA_VF_STATS] = { .type = NLA_NESTED },
2000 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
2001 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
2002 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
2005 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
2006 [IFLA_PORT_VF] = { .type = NLA_U32 },
2007 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
2008 .len = PORT_PROFILE_MAX },
2009 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
2010 .len = PORT_UUID_MAX },
2011 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
2012 .len = PORT_UUID_MAX },
2013 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
2014 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
2016 /* Unused, but we need to keep it here since user space could
2017 * fill it. It's also broken with regard to NLA_BINARY use in
2018 * combination with structs.
2020 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
2021 .len = sizeof(struct ifla_port_vsi) },
2024 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
2025 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
2026 [IFLA_XDP_FD] = { .type = NLA_S32 },
2027 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
2028 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
2029 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
2030 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
2033 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
2035 const struct rtnl_link_ops *ops = NULL;
2036 struct nlattr *linfo[IFLA_INFO_MAX + 1];
2038 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
2041 if (linfo[IFLA_INFO_KIND]) {
2042 char kind[MODULE_NAME_LEN];
2044 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2045 ops = rtnl_link_ops_get(kind);
2051 static bool link_master_filtered(struct net_device *dev, int master_idx)
2053 struct net_device *master;
2058 master = netdev_master_upper_dev_get(dev);
2060 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2061 * another invalid value for ifindex to denote "no master".
2063 if (master_idx == -1)
2066 if (!master || master->ifindex != master_idx)
2072 static bool link_kind_filtered(const struct net_device *dev,
2073 const struct rtnl_link_ops *kind_ops)
2075 if (kind_ops && dev->rtnl_link_ops != kind_ops)
2081 static bool link_dump_filtered(struct net_device *dev,
2083 const struct rtnl_link_ops *kind_ops)
2085 if (link_master_filtered(dev, master_idx) ||
2086 link_kind_filtered(dev, kind_ops))
2093 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2094 * @sk: netlink socket
2095 * @netnsid: network namespace identifier
2097 * Returns the network namespace identified by netnsid on success or an error
2098 * pointer on failure.
2100 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2104 net = get_net_ns_by_id(sock_net(sk), netnsid);
2106 return ERR_PTR(-EINVAL);
2108 /* For now, the caller is required to have CAP_NET_ADMIN in
2109 * the user namespace owning the target net ns.
2111 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2113 return ERR_PTR(-EACCES);
2117 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2119 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2120 bool strict_check, struct nlattr **tb,
2121 struct netlink_ext_ack *extack)
2126 struct ifinfomsg *ifm;
2128 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2129 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2133 ifm = nlmsg_data(nlh);
2134 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2136 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2139 if (ifm->ifi_index) {
2140 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2144 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2145 IFLA_MAX, ifla_policy,
2149 /* A hack to preserve kernel<->userspace interface.
2150 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2151 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2152 * what iproute2 < v3.9.0 used.
2153 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2154 * attribute, its netlink message is shorter than struct ifinfomsg.
2156 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2157 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2159 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2163 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2165 struct netlink_ext_ack *extack = cb->extack;
2166 const struct nlmsghdr *nlh = cb->nlh;
2167 struct net *net = sock_net(skb->sk);
2168 struct net *tgt_net = net;
2171 struct net_device *dev;
2172 struct hlist_head *head;
2173 struct nlattr *tb[IFLA_MAX+1];
2174 u32 ext_filter_mask = 0;
2175 const struct rtnl_link_ops *kind_ops = NULL;
2176 unsigned int flags = NLM_F_MULTI;
2182 s_idx = cb->args[1];
2184 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2186 if (cb->strict_check)
2192 for (i = 0; i <= IFLA_MAX; ++i) {
2196 /* new attributes should only be added with strict checking */
2198 case IFLA_TARGET_NETNSID:
2199 netnsid = nla_get_s32(tb[i]);
2200 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2201 if (IS_ERR(tgt_net)) {
2202 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2203 return PTR_ERR(tgt_net);
2207 ext_filter_mask = nla_get_u32(tb[i]);
2210 master_idx = nla_get_u32(tb[i]);
2213 kind_ops = linkinfo_to_kind_ops(tb[i]);
2216 if (cb->strict_check) {
2217 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2223 if (master_idx || kind_ops)
2224 flags |= NLM_F_DUMP_FILTERED;
2227 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2229 head = &tgt_net->dev_index_head[h];
2230 hlist_for_each_entry(dev, head, index_hlist) {
2231 if (link_dump_filtered(dev, master_idx, kind_ops))
2235 err = rtnl_fill_ifinfo(skb, dev, net,
2237 NETLINK_CB(cb->skb).portid,
2238 nlh->nlmsg_seq, 0, flags,
2239 ext_filter_mask, 0, NULL, 0,
2240 netnsid, GFP_KERNEL);
2243 if (likely(skb->len))
2257 cb->seq = tgt_net->dev_base_seq;
2258 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2265 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2266 struct netlink_ext_ack *exterr)
2268 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2271 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2273 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2276 /* Examine the link attributes and figure out which
2277 * network namespace we are talking about.
2279 if (tb[IFLA_NET_NS_PID])
2280 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2281 else if (tb[IFLA_NET_NS_FD])
2282 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2284 net = get_net(src_net);
2287 EXPORT_SYMBOL(rtnl_link_get_net);
2289 /* Figure out which network namespace we are talking about by
2290 * examining the link attributes in the following order:
2292 * 1. IFLA_NET_NS_PID
2294 * 3. IFLA_TARGET_NETNSID
2296 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2297 struct nlattr *tb[])
2301 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2302 return rtnl_link_get_net(src_net, tb);
2304 if (!tb[IFLA_TARGET_NETNSID])
2305 return get_net(src_net);
2307 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2309 return ERR_PTR(-EINVAL);
2314 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2315 struct net *src_net,
2316 struct nlattr *tb[], int cap)
2320 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2324 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2326 return ERR_PTR(-EPERM);
2332 /* Verify that rtnetlink requests do not pass additional properties
2333 * potentially referring to different network namespaces.
2335 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2336 struct netlink_ext_ack *extack,
2340 if (netns_id_only) {
2341 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2344 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2348 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2351 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2354 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2360 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2364 static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2367 const struct net_device_ops *ops = dev->netdev_ops;
2369 if (!ops->ndo_set_vf_rate)
2371 if (max_tx_rate && max_tx_rate < min_tx_rate)
2374 return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2377 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2378 struct netlink_ext_ack *extack)
2381 if (tb[IFLA_ADDRESS] &&
2382 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2385 if (tb[IFLA_BROADCAST] &&
2386 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2390 if (tb[IFLA_AF_SPEC]) {
2394 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2395 const struct rtnl_af_ops *af_ops;
2397 af_ops = rtnl_af_lookup(nla_type(af));
2399 return -EAFNOSUPPORT;
2401 if (!af_ops->set_link_af)
2404 if (af_ops->validate_link_af) {
2405 err = af_ops->validate_link_af(dev, af, extack);
2415 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2418 const struct net_device_ops *ops = dev->netdev_ops;
2420 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2423 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2425 if (dev->type != ARPHRD_INFINIBAND)
2428 return handle_infiniband_guid(dev, ivt, guid_type);
2431 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2433 const struct net_device_ops *ops = dev->netdev_ops;
2436 if (tb[IFLA_VF_MAC]) {
2437 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2439 if (ivm->vf >= INT_MAX)
2442 if (ops->ndo_set_vf_mac)
2443 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2449 if (tb[IFLA_VF_VLAN]) {
2450 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2452 if (ivv->vf >= INT_MAX)
2455 if (ops->ndo_set_vf_vlan)
2456 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2458 htons(ETH_P_8021Q));
2463 if (tb[IFLA_VF_VLAN_LIST]) {
2464 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2465 struct nlattr *attr;
2469 if (!ops->ndo_set_vf_vlan)
2472 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2473 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2474 nla_len(attr) < NLA_HDRLEN) {
2477 if (len >= MAX_VLAN_LIST_LEN)
2479 ivvl[len] = nla_data(attr);
2486 if (ivvl[0]->vf >= INT_MAX)
2488 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2489 ivvl[0]->qos, ivvl[0]->vlan_proto);
2494 if (tb[IFLA_VF_TX_RATE]) {
2495 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2496 struct ifla_vf_info ivf;
2498 if (ivt->vf >= INT_MAX)
2501 if (ops->ndo_get_vf_config)
2502 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2506 err = rtnl_set_vf_rate(dev, ivt->vf,
2507 ivf.min_tx_rate, ivt->rate);
2512 if (tb[IFLA_VF_RATE]) {
2513 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2515 if (ivt->vf >= INT_MAX)
2518 err = rtnl_set_vf_rate(dev, ivt->vf,
2519 ivt->min_tx_rate, ivt->max_tx_rate);
2524 if (tb[IFLA_VF_SPOOFCHK]) {
2525 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2527 if (ivs->vf >= INT_MAX)
2530 if (ops->ndo_set_vf_spoofchk)
2531 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2537 if (tb[IFLA_VF_LINK_STATE]) {
2538 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2540 if (ivl->vf >= INT_MAX)
2543 if (ops->ndo_set_vf_link_state)
2544 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2550 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2551 struct ifla_vf_rss_query_en *ivrssq_en;
2554 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2555 if (ivrssq_en->vf >= INT_MAX)
2557 if (ops->ndo_set_vf_rss_query_en)
2558 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2559 ivrssq_en->setting);
2564 if (tb[IFLA_VF_TRUST]) {
2565 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2567 if (ivt->vf >= INT_MAX)
2570 if (ops->ndo_set_vf_trust)
2571 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2576 if (tb[IFLA_VF_IB_NODE_GUID]) {
2577 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2579 if (ivt->vf >= INT_MAX)
2581 if (!ops->ndo_set_vf_guid)
2583 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2586 if (tb[IFLA_VF_IB_PORT_GUID]) {
2587 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2589 if (ivt->vf >= INT_MAX)
2591 if (!ops->ndo_set_vf_guid)
2594 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2600 static int do_set_master(struct net_device *dev, int ifindex,
2601 struct netlink_ext_ack *extack)
2603 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2604 const struct net_device_ops *ops;
2608 if (upper_dev->ifindex == ifindex)
2610 ops = upper_dev->netdev_ops;
2611 if (ops->ndo_del_slave) {
2612 err = ops->ndo_del_slave(upper_dev, dev);
2621 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2624 ops = upper_dev->netdev_ops;
2625 if (ops->ndo_add_slave) {
2626 err = ops->ndo_add_slave(upper_dev, dev, extack);
2636 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2637 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2638 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2641 static int do_set_proto_down(struct net_device *dev,
2642 struct nlattr *nl_proto_down,
2643 struct nlattr *nl_proto_down_reason,
2644 struct netlink_ext_ack *extack)
2646 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2647 unsigned long mask = 0;
2652 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2653 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2657 if (nl_proto_down_reason) {
2658 err = nla_parse_nested_deprecated(pdreason,
2659 IFLA_PROTO_DOWN_REASON_MAX,
2660 nl_proto_down_reason,
2661 ifla_proto_down_reason_policy,
2666 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2667 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2671 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2673 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2674 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2676 dev_change_proto_down_reason(dev, mask, value);
2679 if (nl_proto_down) {
2680 proto_down = nla_get_u8(nl_proto_down);
2682 /* Don't turn off protodown if there are active reasons */
2683 if (!proto_down && dev->proto_down_reason) {
2684 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2687 err = dev_change_proto_down(dev,
2696 #define DO_SETLINK_MODIFIED 0x01
2697 /* notify flag means notify + modified. */
2698 #define DO_SETLINK_NOTIFY 0x03
2699 static int do_setlink(const struct sk_buff *skb,
2700 struct net_device *dev, struct ifinfomsg *ifm,
2701 struct netlink_ext_ack *extack,
2702 struct nlattr **tb, int status)
2704 const struct net_device_ops *ops = dev->netdev_ops;
2705 char ifname[IFNAMSIZ];
2708 err = validate_linkmsg(dev, tb, extack);
2712 if (tb[IFLA_IFNAME])
2713 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2717 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2718 const char *pat = ifname[0] ? ifname : NULL;
2722 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2729 if (tb[IFLA_NEW_IFINDEX])
2730 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2734 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2738 status |= DO_SETLINK_MODIFIED;
2742 struct rtnl_link_ifmap *u_map;
2745 if (!ops->ndo_set_config) {
2750 if (!netif_device_present(dev)) {
2755 u_map = nla_data(tb[IFLA_MAP]);
2756 k_map.mem_start = (unsigned long) u_map->mem_start;
2757 k_map.mem_end = (unsigned long) u_map->mem_end;
2758 k_map.base_addr = (unsigned short) u_map->base_addr;
2759 k_map.irq = (unsigned char) u_map->irq;
2760 k_map.dma = (unsigned char) u_map->dma;
2761 k_map.port = (unsigned char) u_map->port;
2763 err = ops->ndo_set_config(dev, &k_map);
2767 status |= DO_SETLINK_NOTIFY;
2770 if (tb[IFLA_ADDRESS]) {
2771 struct sockaddr *sa;
2774 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2776 sa = kmalloc(len, GFP_KERNEL);
2781 sa->sa_family = dev->type;
2782 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2784 err = dev_set_mac_address_user(dev, sa, extack);
2788 status |= DO_SETLINK_MODIFIED;
2792 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2795 status |= DO_SETLINK_MODIFIED;
2798 if (tb[IFLA_GROUP]) {
2799 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2800 status |= DO_SETLINK_NOTIFY;
2804 * Interface selected by interface index but interface
2805 * name provided implies that a name change has been
2808 if (ifm->ifi_index > 0 && ifname[0]) {
2809 err = dev_change_name(dev, ifname);
2812 status |= DO_SETLINK_MODIFIED;
2815 if (tb[IFLA_IFALIAS]) {
2816 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2817 nla_len(tb[IFLA_IFALIAS]));
2820 status |= DO_SETLINK_NOTIFY;
2823 if (tb[IFLA_BROADCAST]) {
2824 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2825 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2828 if (tb[IFLA_MASTER]) {
2829 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2832 status |= DO_SETLINK_MODIFIED;
2835 if (ifm->ifi_flags || ifm->ifi_change) {
2836 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2842 if (tb[IFLA_CARRIER]) {
2843 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2846 status |= DO_SETLINK_MODIFIED;
2849 if (tb[IFLA_TXQLEN]) {
2850 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2852 err = dev_change_tx_queue_len(dev, value);
2855 status |= DO_SETLINK_MODIFIED;
2858 if (tb[IFLA_GSO_MAX_SIZE]) {
2859 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2861 if (max_size > dev->tso_max_size) {
2866 if (dev->gso_max_size ^ max_size) {
2867 netif_set_gso_max_size(dev, max_size);
2868 status |= DO_SETLINK_MODIFIED;
2872 if (tb[IFLA_GSO_MAX_SEGS]) {
2873 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2875 if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) {
2880 if (dev->gso_max_segs ^ max_segs) {
2881 netif_set_gso_max_segs(dev, max_segs);
2882 status |= DO_SETLINK_MODIFIED;
2886 if (tb[IFLA_GRO_MAX_SIZE]) {
2887 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2889 if (dev->gro_max_size ^ gro_max_size) {
2890 netif_set_gro_max_size(dev, gro_max_size);
2891 status |= DO_SETLINK_MODIFIED;
2895 if (tb[IFLA_GSO_IPV4_MAX_SIZE]) {
2896 u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]);
2898 if (max_size > dev->tso_max_size) {
2903 if (dev->gso_ipv4_max_size ^ max_size) {
2904 netif_set_gso_ipv4_max_size(dev, max_size);
2905 status |= DO_SETLINK_MODIFIED;
2909 if (tb[IFLA_GRO_IPV4_MAX_SIZE]) {
2910 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]);
2912 if (dev->gro_ipv4_max_size ^ gro_max_size) {
2913 netif_set_gro_ipv4_max_size(dev, gro_max_size);
2914 status |= DO_SETLINK_MODIFIED;
2918 if (tb[IFLA_OPERSTATE])
2919 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2921 if (tb[IFLA_LINKMODE]) {
2922 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2924 write_lock(&dev_base_lock);
2925 if (dev->link_mode ^ value)
2926 status |= DO_SETLINK_NOTIFY;
2927 dev->link_mode = value;
2928 write_unlock(&dev_base_lock);
2931 if (tb[IFLA_VFINFO_LIST]) {
2932 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2933 struct nlattr *attr;
2936 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2937 if (nla_type(attr) != IFLA_VF_INFO ||
2938 nla_len(attr) < NLA_HDRLEN) {
2942 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2948 err = do_setvfinfo(dev, vfinfo);
2951 status |= DO_SETLINK_NOTIFY;
2956 if (tb[IFLA_VF_PORTS]) {
2957 struct nlattr *port[IFLA_PORT_MAX+1];
2958 struct nlattr *attr;
2963 if (!ops->ndo_set_vf_port)
2966 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2967 if (nla_type(attr) != IFLA_VF_PORT ||
2968 nla_len(attr) < NLA_HDRLEN) {
2972 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2978 if (!port[IFLA_PORT_VF]) {
2982 vf = nla_get_u32(port[IFLA_PORT_VF]);
2983 err = ops->ndo_set_vf_port(dev, vf, port);
2986 status |= DO_SETLINK_NOTIFY;
2991 if (tb[IFLA_PORT_SELF]) {
2992 struct nlattr *port[IFLA_PORT_MAX+1];
2994 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2996 ifla_port_policy, NULL);
3001 if (ops->ndo_set_vf_port)
3002 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
3005 status |= DO_SETLINK_NOTIFY;
3008 if (tb[IFLA_AF_SPEC]) {
3012 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
3013 const struct rtnl_af_ops *af_ops;
3015 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
3017 err = af_ops->set_link_af(dev, af, extack);
3021 status |= DO_SETLINK_NOTIFY;
3026 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
3027 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
3028 tb[IFLA_PROTO_DOWN_REASON], extack);
3031 status |= DO_SETLINK_NOTIFY;
3035 struct nlattr *xdp[IFLA_XDP_MAX + 1];
3038 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
3040 ifla_xdp_policy, NULL);
3044 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
3049 if (xdp[IFLA_XDP_FLAGS]) {
3050 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
3051 if (xdp_flags & ~XDP_FLAGS_MASK) {
3055 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
3061 if (xdp[IFLA_XDP_FD]) {
3062 int expected_fd = -1;
3064 if (xdp_flags & XDP_FLAGS_REPLACE) {
3065 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
3070 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
3073 err = dev_change_xdp_fd(dev, extack,
3074 nla_get_s32(xdp[IFLA_XDP_FD]),
3079 status |= DO_SETLINK_NOTIFY;
3084 if (status & DO_SETLINK_MODIFIED) {
3085 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3086 netdev_state_change(dev);
3089 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",
3096 static struct net_device *rtnl_dev_get(struct net *net,
3097 struct nlattr *tb[])
3099 char ifname[ALTIFNAMSIZ];
3101 if (tb[IFLA_IFNAME])
3102 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3103 else if (tb[IFLA_ALT_IFNAME])
3104 nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3108 return __dev_get_by_name(net, ifname);
3111 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3112 struct netlink_ext_ack *extack)
3114 struct net *net = sock_net(skb->sk);
3115 struct ifinfomsg *ifm;
3116 struct net_device *dev;
3118 struct nlattr *tb[IFLA_MAX+1];
3120 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3121 ifla_policy, extack);
3125 err = rtnl_ensure_unique_netns(tb, extack, false);
3130 ifm = nlmsg_data(nlh);
3131 if (ifm->ifi_index > 0)
3132 dev = __dev_get_by_index(net, ifm->ifi_index);
3133 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3134 dev = rtnl_dev_get(net, tb);
3143 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3148 static int rtnl_group_dellink(const struct net *net, int group)
3150 struct net_device *dev, *aux;
3151 LIST_HEAD(list_kill);
3157 for_each_netdev(net, dev) {
3158 if (dev->group == group) {
3159 const struct rtnl_link_ops *ops;
3162 ops = dev->rtnl_link_ops;
3163 if (!ops || !ops->dellink)
3171 for_each_netdev_safe(net, dev, aux) {
3172 if (dev->group == group) {
3173 const struct rtnl_link_ops *ops;
3175 ops = dev->rtnl_link_ops;
3176 ops->dellink(dev, &list_kill);
3179 unregister_netdevice_many(&list_kill);
3184 int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
3186 const struct rtnl_link_ops *ops;
3187 LIST_HEAD(list_kill);
3189 ops = dev->rtnl_link_ops;
3190 if (!ops || !ops->dellink)
3193 ops->dellink(dev, &list_kill);
3194 unregister_netdevice_many_notify(&list_kill, portid, nlh);
3198 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3200 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3201 struct netlink_ext_ack *extack)
3203 struct net *net = sock_net(skb->sk);
3204 u32 portid = NETLINK_CB(skb).portid;
3205 struct net *tgt_net = net;
3206 struct net_device *dev = NULL;
3207 struct ifinfomsg *ifm;
3208 struct nlattr *tb[IFLA_MAX+1];
3212 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3213 ifla_policy, extack);
3217 err = rtnl_ensure_unique_netns(tb, extack, true);
3221 if (tb[IFLA_TARGET_NETNSID]) {
3222 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3223 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3224 if (IS_ERR(tgt_net))
3225 return PTR_ERR(tgt_net);
3229 ifm = nlmsg_data(nlh);
3230 if (ifm->ifi_index > 0)
3231 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3232 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3233 dev = rtnl_dev_get(net, tb);
3234 else if (tb[IFLA_GROUP])
3235 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3240 if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3246 err = rtnl_delete_link(dev, portid, nlh);
3255 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
3256 u32 portid, const struct nlmsghdr *nlh)
3258 unsigned int old_flags;
3261 old_flags = dev->flags;
3262 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3263 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3269 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3270 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh);
3272 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3273 __dev_notify_flags(dev, old_flags, ~0U, portid, nlh);
3277 EXPORT_SYMBOL(rtnl_configure_link);
3279 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3280 unsigned char name_assign_type,
3281 const struct rtnl_link_ops *ops,
3282 struct nlattr *tb[],
3283 struct netlink_ext_ack *extack)
3285 struct net_device *dev;
3286 unsigned int num_tx_queues = 1;
3287 unsigned int num_rx_queues = 1;
3289 if (tb[IFLA_NUM_TX_QUEUES])
3290 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3291 else if (ops->get_num_tx_queues)
3292 num_tx_queues = ops->get_num_tx_queues();
3294 if (tb[IFLA_NUM_RX_QUEUES])
3295 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3296 else if (ops->get_num_rx_queues)
3297 num_rx_queues = ops->get_num_rx_queues();
3299 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3300 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3301 return ERR_PTR(-EINVAL);
3304 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3305 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3306 return ERR_PTR(-EINVAL);
3310 dev = ops->alloc(tb, ifname, name_assign_type,
3311 num_tx_queues, num_rx_queues);
3315 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3316 name_assign_type, ops->setup,
3317 num_tx_queues, num_rx_queues);
3321 return ERR_PTR(-ENOMEM);
3323 dev_net_set(dev, net);
3324 dev->rtnl_link_ops = ops;
3325 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3328 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3331 err = dev_validate_mtu(dev, mtu, extack);
3334 return ERR_PTR(err);
3338 if (tb[IFLA_ADDRESS]) {
3339 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3340 nla_len(tb[IFLA_ADDRESS]));
3341 dev->addr_assign_type = NET_ADDR_SET;
3343 if (tb[IFLA_BROADCAST])
3344 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3345 nla_len(tb[IFLA_BROADCAST]));
3346 if (tb[IFLA_TXQLEN])
3347 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3348 if (tb[IFLA_OPERSTATE])
3349 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3350 if (tb[IFLA_LINKMODE])
3351 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3353 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3354 if (tb[IFLA_GSO_MAX_SIZE])
3355 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3356 if (tb[IFLA_GSO_MAX_SEGS])
3357 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3358 if (tb[IFLA_GRO_MAX_SIZE])
3359 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3360 if (tb[IFLA_GSO_IPV4_MAX_SIZE])
3361 netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
3362 if (tb[IFLA_GRO_IPV4_MAX_SIZE])
3363 netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
3367 EXPORT_SYMBOL(rtnl_create_link);
3369 static int rtnl_group_changelink(const struct sk_buff *skb,
3370 struct net *net, int group,
3371 struct ifinfomsg *ifm,
3372 struct netlink_ext_ack *extack,
3375 struct net_device *dev, *aux;
3378 for_each_netdev_safe(net, dev, aux) {
3379 if (dev->group == group) {
3380 err = do_setlink(skb, dev, ifm, extack, tb, 0);
3389 static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3390 const struct rtnl_link_ops *ops,
3391 const struct nlmsghdr *nlh,
3392 struct nlattr **tb, struct nlattr **data,
3393 struct netlink_ext_ack *extack)
3395 unsigned char name_assign_type = NET_NAME_USER;
3396 struct net *net = sock_net(skb->sk);
3397 u32 portid = NETLINK_CB(skb).portid;
3398 struct net *dest_net, *link_net;
3399 struct net_device *dev;
3400 char ifname[IFNAMSIZ];
3403 if (!ops->alloc && !ops->setup)
3406 if (tb[IFLA_IFNAME]) {
3407 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3409 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3410 name_assign_type = NET_NAME_ENUM;
3413 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3414 if (IS_ERR(dest_net))
3415 return PTR_ERR(dest_net);
3417 if (tb[IFLA_LINK_NETNSID]) {
3418 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3420 link_net = get_net_ns_by_id(dest_net, id);
3422 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3427 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3433 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3434 name_assign_type, ops, tb, extack);
3440 dev->ifindex = ifm->ifi_index;
3443 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3445 err = register_netdevice(dev);
3451 err = rtnl_configure_link(dev, ifm, portid, nlh);
3453 goto out_unregister;
3455 err = dev_change_net_namespace(dev, dest_net, ifname);
3457 goto out_unregister;
3459 if (tb[IFLA_MASTER]) {
3460 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3462 goto out_unregister;
3471 LIST_HEAD(list_kill);
3473 ops->dellink(dev, &list_kill);
3474 unregister_netdevice_many(&list_kill);
3476 unregister_netdevice(dev);
3481 struct rtnl_newlink_tbs {
3482 struct nlattr *tb[IFLA_MAX + 1];
3483 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3484 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3487 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3488 struct rtnl_newlink_tbs *tbs,
3489 struct netlink_ext_ack *extack)
3491 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3492 struct nlattr ** const tb = tbs->tb;
3493 const struct rtnl_link_ops *m_ops;
3494 struct net_device *master_dev;
3495 struct net *net = sock_net(skb->sk);
3496 const struct rtnl_link_ops *ops;
3497 struct nlattr **slave_data;
3498 char kind[MODULE_NAME_LEN];
3499 struct net_device *dev;
3500 struct ifinfomsg *ifm;
3501 struct nlattr **data;
3502 bool link_specified;
3505 #ifdef CONFIG_MODULES
3508 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3509 ifla_policy, extack);
3513 err = rtnl_ensure_unique_netns(tb, extack, false);
3517 ifm = nlmsg_data(nlh);
3518 if (ifm->ifi_index > 0) {
3519 link_specified = true;
3520 dev = __dev_get_by_index(net, ifm->ifi_index);
3521 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3522 link_specified = true;
3523 dev = rtnl_dev_get(net, tb);
3525 link_specified = false;
3532 master_dev = netdev_master_upper_dev_get(dev);
3534 m_ops = master_dev->rtnl_link_ops;
3537 err = validate_linkmsg(dev, tb, extack);
3541 if (tb[IFLA_LINKINFO]) {
3542 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3544 ifla_info_policy, NULL);
3548 memset(linkinfo, 0, sizeof(linkinfo));
3550 if (linkinfo[IFLA_INFO_KIND]) {
3551 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3552 ops = rtnl_link_ops_get(kind);
3560 if (ops->maxtype > RTNL_MAX_TYPE)
3563 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3564 err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
3565 linkinfo[IFLA_INFO_DATA],
3566 ops->policy, extack);
3571 if (ops->validate) {
3572 err = ops->validate(tb, data, extack);
3580 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3583 if (m_ops->slave_maxtype &&
3584 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3585 err = nla_parse_nested_deprecated(tbs->slave_attr,
3586 m_ops->slave_maxtype,
3587 linkinfo[IFLA_INFO_SLAVE_DATA],
3588 m_ops->slave_policy,
3592 slave_data = tbs->slave_attr;
3599 if (nlh->nlmsg_flags & NLM_F_EXCL)
3601 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3604 if (linkinfo[IFLA_INFO_DATA]) {
3605 if (!ops || ops != dev->rtnl_link_ops ||
3609 err = ops->changelink(dev, tb, data, extack);
3612 status |= DO_SETLINK_NOTIFY;
3615 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3616 if (!m_ops || !m_ops->slave_changelink)
3619 err = m_ops->slave_changelink(master_dev, dev, tb,
3620 slave_data, extack);
3623 status |= DO_SETLINK_NOTIFY;
3626 return do_setlink(skb, dev, ifm, extack, tb, status);
3629 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3630 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3631 * or it's for a group
3636 return rtnl_group_changelink(skb, net,
3637 nla_get_u32(tb[IFLA_GROUP]),
3642 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3646 #ifdef CONFIG_MODULES
3649 request_module("rtnl-link-%s", kind);
3651 ops = rtnl_link_ops_get(kind);
3656 NL_SET_ERR_MSG(extack, "Unknown device type");
3660 return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
3663 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3664 struct netlink_ext_ack *extack)
3666 struct rtnl_newlink_tbs *tbs;
3669 tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3673 ret = __rtnl_newlink(skb, nlh, tbs, extack);
3678 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3679 const struct nlmsghdr *nlh,
3681 struct netlink_ext_ack *extack)
3683 struct ifinfomsg *ifm;
3686 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3687 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3691 if (!netlink_strict_get_check(skb))
3692 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3693 ifla_policy, extack);
3695 ifm = nlmsg_data(nlh);
3696 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3698 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3702 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3703 ifla_policy, extack);
3707 for (i = 0; i <= IFLA_MAX; i++) {
3713 case IFLA_ALT_IFNAME:
3715 case IFLA_TARGET_NETNSID:
3718 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3726 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3727 struct netlink_ext_ack *extack)
3729 struct net *net = sock_net(skb->sk);
3730 struct net *tgt_net = net;
3731 struct ifinfomsg *ifm;
3732 struct nlattr *tb[IFLA_MAX+1];
3733 struct net_device *dev = NULL;
3734 struct sk_buff *nskb;
3737 u32 ext_filter_mask = 0;
3739 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3743 err = rtnl_ensure_unique_netns(tb, extack, true);
3747 if (tb[IFLA_TARGET_NETNSID]) {
3748 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3749 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3750 if (IS_ERR(tgt_net))
3751 return PTR_ERR(tgt_net);
3754 if (tb[IFLA_EXT_MASK])
3755 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3758 ifm = nlmsg_data(nlh);
3759 if (ifm->ifi_index > 0)
3760 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3761 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3762 dev = rtnl_dev_get(tgt_net, tb);
3771 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3775 err = rtnl_fill_ifinfo(nskb, dev, net,
3776 RTM_NEWLINK, NETLINK_CB(skb).portid,
3777 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3778 0, NULL, 0, netnsid, GFP_KERNEL);
3780 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3781 WARN_ON(err == -EMSGSIZE);
3784 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3792 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3793 bool *changed, struct netlink_ext_ack *extack)
3799 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3803 if (cmd == RTM_NEWLINKPROP) {
3804 size = rtnl_prop_list_size(dev);
3805 size += nla_total_size(ALTIFNAMSIZ);
3806 if (size >= U16_MAX) {
3807 NL_SET_ERR_MSG(extack,
3808 "effective property list too long");
3813 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3817 if (cmd == RTM_NEWLINKPROP) {
3818 err = netdev_name_node_alt_create(dev, alt_ifname);
3821 } else if (cmd == RTM_DELLINKPROP) {
3822 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3834 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3835 struct netlink_ext_ack *extack)
3837 struct net *net = sock_net(skb->sk);
3838 struct nlattr *tb[IFLA_MAX + 1];
3839 struct net_device *dev;
3840 struct ifinfomsg *ifm;
3841 bool changed = false;
3842 struct nlattr *attr;
3845 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3849 err = rtnl_ensure_unique_netns(tb, extack, true);
3853 ifm = nlmsg_data(nlh);
3854 if (ifm->ifi_index > 0)
3855 dev = __dev_get_by_index(net, ifm->ifi_index);
3856 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3857 dev = rtnl_dev_get(net, tb);
3864 if (!tb[IFLA_PROP_LIST])
3867 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3868 switch (nla_type(attr)) {
3869 case IFLA_ALT_IFNAME:
3870 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3878 netdev_state_change(dev);
3882 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3883 struct netlink_ext_ack *extack)
3885 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3888 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3889 struct netlink_ext_ack *extack)
3891 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3894 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3896 struct net *net = sock_net(skb->sk);
3897 size_t min_ifinfo_dump_size = 0;
3898 struct nlattr *tb[IFLA_MAX+1];
3899 u32 ext_filter_mask = 0;
3900 struct net_device *dev;
3903 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3904 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3905 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3907 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3908 if (tb[IFLA_EXT_MASK])
3909 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3912 if (!ext_filter_mask)
3913 return NLMSG_GOODSIZE;
3915 * traverse the list of net devices and compute the minimum
3916 * buffer size based upon the filter mask.
3919 for_each_netdev_rcu(net, dev) {
3920 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3921 if_nlmsg_size(dev, ext_filter_mask));
3925 return nlmsg_total_size(min_ifinfo_dump_size);
3928 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3931 int s_idx = cb->family;
3932 int type = cb->nlh->nlmsg_type - RTM_BASE;
3938 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3939 struct rtnl_link __rcu **tab;
3940 struct rtnl_link *link;
3941 rtnl_dumpit_func dumpit;
3943 if (idx < s_idx || idx == PF_PACKET)
3946 if (type < 0 || type >= RTM_NR_MSGTYPES)
3949 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3953 link = rcu_dereference_rtnl(tab[type]);
3957 dumpit = link->dumpit;
3962 memset(&cb->args[0], 0, sizeof(cb->args));
3966 ret = dumpit(skb, cb);
3972 return skb->len ? : ret;
3975 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3976 unsigned int change,
3977 u32 event, gfp_t flags, int *new_nsid,
3978 int new_ifindex, u32 portid,
3979 const struct nlmsghdr *nlh)
3981 struct net *net = dev_net(dev);
3982 struct sk_buff *skb;
3986 skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
3990 if (nlmsg_report(nlh))
3991 seq = nlmsg_seq(nlh);
3995 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3996 type, portid, seq, change, 0, 0, event,
3997 new_nsid, new_ifindex, -1, flags);
3999 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
4000 WARN_ON(err == -EMSGSIZE);
4007 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4011 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags,
4012 u32 portid, const struct nlmsghdr *nlh)
4014 struct net *net = dev_net(dev);
4016 rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags);
4019 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
4020 unsigned int change, u32 event,
4021 gfp_t flags, int *new_nsid, int new_ifindex,
4022 u32 portid, const struct nlmsghdr *nlh)
4024 struct sk_buff *skb;
4026 if (dev->reg_state != NETREG_REGISTERED)
4029 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
4030 new_ifindex, portid, nlh);
4032 rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
4035 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
4036 gfp_t flags, u32 portid, const struct nlmsghdr *nlh)
4038 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4039 NULL, 0, portid, nlh);
4042 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
4043 gfp_t flags, int *new_nsid, int new_ifindex)
4045 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4046 new_nsid, new_ifindex, 0, NULL);
4049 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
4050 struct net_device *dev,
4051 u8 *addr, u16 vid, u32 pid, u32 seq,
4052 int type, unsigned int flags,
4053 int nlflags, u16 ndm_state)
4055 struct nlmsghdr *nlh;
4058 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4062 ndm = nlmsg_data(nlh);
4063 ndm->ndm_family = AF_BRIDGE;
4066 ndm->ndm_flags = flags;
4068 ndm->ndm_ifindex = dev->ifindex;
4069 ndm->ndm_state = ndm_state;
4071 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
4072 goto nla_put_failure;
4074 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
4075 goto nla_put_failure;
4077 nlmsg_end(skb, nlh);
4081 nlmsg_cancel(skb, nlh);
4085 static inline size_t rtnl_fdb_nlmsg_size(void)
4087 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4088 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
4089 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
4093 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4096 struct net *net = dev_net(dev);
4097 struct sk_buff *skb;
4100 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
4104 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4105 0, 0, type, NTF_SELF, 0, ndm_state);
4111 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
4114 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
4118 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4120 int ndo_dflt_fdb_add(struct ndmsg *ndm,
4121 struct nlattr *tb[],
4122 struct net_device *dev,
4123 const unsigned char *addr, u16 vid,
4128 /* If aging addresses are supported device will need to
4129 * implement its own handler for this.
4131 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4132 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4136 if (tb[NDA_FLAGS_EXT]) {
4137 netdev_info(dev, "invalid flags given to default FDB implementation\n");
4142 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
4146 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4147 err = dev_uc_add_excl(dev, addr);
4148 else if (is_multicast_ether_addr(addr))
4149 err = dev_mc_add_excl(dev, addr);
4151 /* Only return duplicate errors if NLM_F_EXCL is set */
4152 if (err == -EEXIST && !(flags & NLM_F_EXCL))
4157 EXPORT_SYMBOL(ndo_dflt_fdb_add);
4159 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4160 struct netlink_ext_ack *extack)
4165 if (nla_len(vlan_attr) != sizeof(u16)) {
4166 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4170 vid = nla_get_u16(vlan_attr);
4172 if (!vid || vid >= VLAN_VID_MASK) {
4173 NL_SET_ERR_MSG(extack, "invalid vlan id");
4181 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4182 struct netlink_ext_ack *extack)
4184 struct net *net = sock_net(skb->sk);
4186 struct nlattr *tb[NDA_MAX+1];
4187 struct net_device *dev;
4192 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4197 ndm = nlmsg_data(nlh);
4198 if (ndm->ndm_ifindex == 0) {
4199 NL_SET_ERR_MSG(extack, "invalid ifindex");
4203 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4205 NL_SET_ERR_MSG(extack, "unknown ifindex");
4209 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4210 NL_SET_ERR_MSG(extack, "invalid address");
4214 if (dev->type != ARPHRD_ETHER) {
4215 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4219 addr = nla_data(tb[NDA_LLADDR]);
4221 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4227 /* Support fdb on master device the net/bridge default case */
4228 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4229 netif_is_bridge_port(dev)) {
4230 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4231 const struct net_device_ops *ops = br_dev->netdev_ops;
4233 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4234 nlh->nlmsg_flags, extack);
4238 ndm->ndm_flags &= ~NTF_MASTER;
4241 /* Embedded bridge, macvlan, and any other device support */
4242 if ((ndm->ndm_flags & NTF_SELF)) {
4243 if (dev->netdev_ops->ndo_fdb_add)
4244 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4249 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4253 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4255 ndm->ndm_flags &= ~NTF_SELF;
4263 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4265 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4266 struct nlattr *tb[],
4267 struct net_device *dev,
4268 const unsigned char *addr, u16 vid)
4272 /* If aging addresses are supported device will need to
4273 * implement its own handler for this.
4275 if (!(ndm->ndm_state & NUD_PERMANENT)) {
4276 netdev_info(dev, "default FDB implementation only supports local addresses\n");
4280 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4281 err = dev_uc_del(dev, addr);
4282 else if (is_multicast_ether_addr(addr))
4283 err = dev_mc_del(dev, addr);
4287 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4289 static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
4290 [NDA_VLAN] = { .type = NLA_U16 },
4291 [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
4292 [NDA_NDM_STATE_MASK] = { .type = NLA_U16 },
4293 [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 },
4296 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4297 struct netlink_ext_ack *extack)
4299 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
4300 struct net *net = sock_net(skb->sk);
4301 const struct net_device_ops *ops;
4303 struct nlattr *tb[NDA_MAX+1];
4304 struct net_device *dev;
4309 if (!netlink_capable(skb, CAP_NET_ADMIN))
4313 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
4316 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
4317 fdb_del_bulk_policy, extack);
4322 ndm = nlmsg_data(nlh);
4323 if (ndm->ndm_ifindex == 0) {
4324 NL_SET_ERR_MSG(extack, "invalid ifindex");
4328 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4330 NL_SET_ERR_MSG(extack, "unknown ifindex");
4335 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4336 NL_SET_ERR_MSG(extack, "invalid address");
4339 addr = nla_data(tb[NDA_LLADDR]);
4342 if (dev->type != ARPHRD_ETHER) {
4343 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4347 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4353 /* Support fdb on master device the net/bridge default case */
4354 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4355 netif_is_bridge_port(dev)) {
4356 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4358 ops = br_dev->netdev_ops;
4360 if (ops->ndo_fdb_del)
4361 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4363 if (ops->ndo_fdb_del_bulk)
4364 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4371 ndm->ndm_flags &= ~NTF_MASTER;
4374 /* Embedded bridge, macvlan, and any other device support */
4375 if (ndm->ndm_flags & NTF_SELF) {
4376 ops = dev->netdev_ops;
4378 if (ops->ndo_fdb_del)
4379 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4381 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4383 /* in case err was cleared by NTF_MASTER call */
4385 if (ops->ndo_fdb_del_bulk)
4386 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4392 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4394 ndm->ndm_flags &= ~NTF_SELF;
4401 static int nlmsg_populate_fdb(struct sk_buff *skb,
4402 struct netlink_callback *cb,
4403 struct net_device *dev,
4405 struct netdev_hw_addr_list *list)
4407 struct netdev_hw_addr *ha;
4411 portid = NETLINK_CB(cb->skb).portid;
4412 seq = cb->nlh->nlmsg_seq;
4414 list_for_each_entry(ha, &list->list, list) {
4415 if (*idx < cb->args[2])
4418 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4420 RTM_NEWNEIGH, NTF_SELF,
4421 NLM_F_MULTI, NUD_PERMANENT);
4431 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4432 * @skb: socket buffer to store message in
4433 * @cb: netlink callback
4435 * @filter_dev: ignored
4436 * @idx: the number of FDB table entries dumped is added to *@idx
4438 * Default netdevice operation to dump the existing unicast address list.
4439 * Returns number of addresses from list put in skb.
4441 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4442 struct netlink_callback *cb,
4443 struct net_device *dev,
4444 struct net_device *filter_dev,
4449 if (dev->type != ARPHRD_ETHER)
4452 netif_addr_lock_bh(dev);
4453 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4456 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4458 netif_addr_unlock_bh(dev);
4461 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4463 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4464 int *br_idx, int *brport_idx,
4465 struct netlink_ext_ack *extack)
4467 struct nlattr *tb[NDA_MAX + 1];
4471 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4472 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4476 ndm = nlmsg_data(nlh);
4477 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4478 ndm->ndm_flags || ndm->ndm_type) {
4479 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4483 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4484 NDA_MAX, NULL, extack);
4488 *brport_idx = ndm->ndm_ifindex;
4489 for (i = 0; i <= NDA_MAX; ++i) {
4495 if (nla_len(tb[i]) != sizeof(u32)) {
4496 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4499 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4502 if (nla_len(tb[i]) != sizeof(u32)) {
4503 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4506 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4509 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4517 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4518 int *br_idx, int *brport_idx,
4519 struct netlink_ext_ack *extack)
4521 struct nlattr *tb[IFLA_MAX+1];
4524 /* A hack to preserve kernel<->userspace interface.
4525 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4526 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4527 * So, check for ndmsg with an optional u32 attribute (not used here).
4528 * Fortunately these sizes don't conflict with the size of ifinfomsg
4529 * with an optional attribute.
4531 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4532 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4533 nla_attr_size(sizeof(u32)))) {
4534 struct ifinfomsg *ifm;
4536 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4537 tb, IFLA_MAX, ifla_policy,
4541 } else if (err == 0) {
4542 if (tb[IFLA_MASTER])
4543 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4546 ifm = nlmsg_data(nlh);
4547 *brport_idx = ifm->ifi_index;
4552 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4554 struct net_device *dev;
4555 struct net_device *br_dev = NULL;
4556 const struct net_device_ops *ops = NULL;
4557 const struct net_device_ops *cops = NULL;
4558 struct net *net = sock_net(skb->sk);
4559 struct hlist_head *head;
4567 if (cb->strict_check)
4568 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4571 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4577 br_dev = __dev_get_by_index(net, br_idx);
4581 ops = br_dev->netdev_ops;
4585 s_idx = cb->args[1];
4587 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4589 head = &net->dev_index_head[h];
4590 hlist_for_each_entry(dev, head, index_hlist) {
4592 if (brport_idx && (dev->ifindex != brport_idx))
4595 if (!br_idx) { /* user did not specify a specific bridge */
4596 if (netif_is_bridge_port(dev)) {
4597 br_dev = netdev_master_upper_dev_get(dev);
4598 cops = br_dev->netdev_ops;
4601 if (dev != br_dev &&
4602 !netif_is_bridge_port(dev))
4605 if (br_dev != netdev_master_upper_dev_get(dev) &&
4606 !netif_is_bridge_master(dev))
4614 if (netif_is_bridge_port(dev)) {
4615 if (cops && cops->ndo_fdb_dump) {
4616 err = cops->ndo_fdb_dump(skb, cb,
4619 if (err == -EMSGSIZE)
4624 if (dev->netdev_ops->ndo_fdb_dump)
4625 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4629 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4631 if (err == -EMSGSIZE)
4636 /* reset fdb offset to 0 for rest of the interfaces */
4652 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4653 struct nlattr **tb, u8 *ndm_flags,
4654 int *br_idx, int *brport_idx, u8 **addr,
4655 u16 *vid, struct netlink_ext_ack *extack)
4660 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4661 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4665 ndm = nlmsg_data(nlh);
4666 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4668 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4672 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4673 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4677 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4678 NDA_MAX, nda_policy, extack);
4682 *ndm_flags = ndm->ndm_flags;
4683 *brport_idx = ndm->ndm_ifindex;
4684 for (i = 0; i <= NDA_MAX; ++i) {
4690 *br_idx = nla_get_u32(tb[i]);
4693 if (nla_len(tb[i]) != ETH_ALEN) {
4694 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4697 *addr = nla_data(tb[i]);
4700 err = fdb_vid_parse(tb[i], vid, extack);
4707 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4715 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4716 struct netlink_ext_ack *extack)
4718 struct net_device *dev = NULL, *br_dev = NULL;
4719 const struct net_device_ops *ops = NULL;
4720 struct net *net = sock_net(in_skb->sk);
4721 struct nlattr *tb[NDA_MAX + 1];
4722 struct sk_buff *skb;
4730 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4731 &brport_idx, &addr, &vid, extack);
4736 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4741 dev = __dev_get_by_index(net, brport_idx);
4743 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4750 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4754 br_dev = __dev_get_by_index(net, br_idx);
4756 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4759 ops = br_dev->netdev_ops;
4763 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4764 if (!netif_is_bridge_port(dev)) {
4765 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4768 br_dev = netdev_master_upper_dev_get(dev);
4770 NL_SET_ERR_MSG(extack, "Master of device not found");
4773 ops = br_dev->netdev_ops;
4775 if (!(ndm_flags & NTF_SELF)) {
4776 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4779 ops = dev->netdev_ops;
4783 if (!br_dev && !dev) {
4784 NL_SET_ERR_MSG(extack, "No device specified");
4788 if (!ops || !ops->ndo_fdb_get) {
4789 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4793 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4799 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4800 NETLINK_CB(in_skb).portid,
4801 nlh->nlmsg_seq, extack);
4805 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4811 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4812 unsigned int attrnum, unsigned int flag)
4815 return nla_put_u8(skb, attrnum, !!(flags & flag));
4819 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4820 struct net_device *dev, u16 mode,
4821 u32 flags, u32 mask, int nlflags,
4823 int (*vlan_fill)(struct sk_buff *skb,
4824 struct net_device *dev,
4827 struct nlmsghdr *nlh;
4828 struct ifinfomsg *ifm;
4829 struct nlattr *br_afspec;
4830 struct nlattr *protinfo;
4831 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4832 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4835 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4839 ifm = nlmsg_data(nlh);
4840 ifm->ifi_family = AF_BRIDGE;
4842 ifm->ifi_type = dev->type;
4843 ifm->ifi_index = dev->ifindex;
4844 ifm->ifi_flags = dev_get_flags(dev);
4845 ifm->ifi_change = 0;
4848 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4849 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4850 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4852 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4854 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4855 (dev->ifindex != dev_get_iflink(dev) &&
4856 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4857 goto nla_put_failure;
4859 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4861 goto nla_put_failure;
4863 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4864 nla_nest_cancel(skb, br_afspec);
4865 goto nla_put_failure;
4868 if (mode != BRIDGE_MODE_UNDEF) {
4869 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4870 nla_nest_cancel(skb, br_afspec);
4871 goto nla_put_failure;
4875 err = vlan_fill(skb, dev, filter_mask);
4877 nla_nest_cancel(skb, br_afspec);
4878 goto nla_put_failure;
4881 nla_nest_end(skb, br_afspec);
4883 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4885 goto nla_put_failure;
4887 if (brport_nla_put_flag(skb, flags, mask,
4888 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4889 brport_nla_put_flag(skb, flags, mask,
4890 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4891 brport_nla_put_flag(skb, flags, mask,
4892 IFLA_BRPORT_FAST_LEAVE,
4893 BR_MULTICAST_FAST_LEAVE) ||
4894 brport_nla_put_flag(skb, flags, mask,
4895 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4896 brport_nla_put_flag(skb, flags, mask,
4897 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4898 brport_nla_put_flag(skb, flags, mask,
4899 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4900 brport_nla_put_flag(skb, flags, mask,
4901 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4902 brport_nla_put_flag(skb, flags, mask,
4903 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4904 brport_nla_put_flag(skb, flags, mask,
4905 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4906 brport_nla_put_flag(skb, flags, mask,
4907 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4908 nla_nest_cancel(skb, protinfo);
4909 goto nla_put_failure;
4912 nla_nest_end(skb, protinfo);
4914 nlmsg_end(skb, nlh);
4917 nlmsg_cancel(skb, nlh);
4918 return err ? err : -EMSGSIZE;
4920 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4922 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4923 bool strict_check, u32 *filter_mask,
4924 struct netlink_ext_ack *extack)
4926 struct nlattr *tb[IFLA_MAX+1];
4930 struct ifinfomsg *ifm;
4932 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4933 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4937 ifm = nlmsg_data(nlh);
4938 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4939 ifm->ifi_change || ifm->ifi_index) {
4940 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4944 err = nlmsg_parse_deprecated_strict(nlh,
4945 sizeof(struct ifinfomsg),
4946 tb, IFLA_MAX, ifla_policy,
4949 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4950 tb, IFLA_MAX, ifla_policy,
4956 /* new attributes should only be added with strict checking */
4957 for (i = 0; i <= IFLA_MAX; ++i) {
4963 *filter_mask = nla_get_u32(tb[i]);
4967 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4976 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4978 const struct nlmsghdr *nlh = cb->nlh;
4979 struct net *net = sock_net(skb->sk);
4980 struct net_device *dev;
4982 u32 portid = NETLINK_CB(cb->skb).portid;
4983 u32 seq = nlh->nlmsg_seq;
4984 u32 filter_mask = 0;
4987 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4989 if (err < 0 && cb->strict_check)
4993 for_each_netdev_rcu(net, dev) {
4994 const struct net_device_ops *ops = dev->netdev_ops;
4995 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4997 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4998 if (idx >= cb->args[0]) {
4999 err = br_dev->netdev_ops->ndo_bridge_getlink(
5000 skb, portid, seq, dev,
5001 filter_mask, NLM_F_MULTI);
5002 if (err < 0 && err != -EOPNOTSUPP) {
5003 if (likely(skb->len))
5012 if (ops->ndo_bridge_getlink) {
5013 if (idx >= cb->args[0]) {
5014 err = ops->ndo_bridge_getlink(skb, portid,
5018 if (err < 0 && err != -EOPNOTSUPP) {
5019 if (likely(skb->len))
5036 static inline size_t bridge_nlmsg_size(void)
5038 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5039 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5040 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5041 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
5042 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
5043 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
5044 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
5045 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
5046 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
5047 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
5048 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
5051 static int rtnl_bridge_notify(struct net_device *dev)
5053 struct net *net = dev_net(dev);
5054 struct sk_buff *skb;
5055 int err = -EOPNOTSUPP;
5057 if (!dev->netdev_ops->ndo_bridge_getlink)
5060 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
5066 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5070 /* Notification info is only filled for bridge ports, not the bridge
5071 * device itself. Therefore, a zero notification length is valid and
5072 * should not result in an error.
5077 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
5080 WARN_ON(err == -EMSGSIZE);
5083 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
5087 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
5088 struct netlink_ext_ack *extack)
5090 struct net *net = sock_net(skb->sk);
5091 struct ifinfomsg *ifm;
5092 struct net_device *dev;
5093 struct nlattr *br_spec, *attr = NULL;
5094 int rem, err = -EOPNOTSUPP;
5096 bool have_flags = false;
5098 if (nlmsg_len(nlh) < sizeof(*ifm))
5101 ifm = nlmsg_data(nlh);
5102 if (ifm->ifi_family != AF_BRIDGE)
5103 return -EPFNOSUPPORT;
5105 dev = __dev_get_by_index(net, ifm->ifi_index);
5107 NL_SET_ERR_MSG(extack, "unknown ifindex");
5111 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5113 nla_for_each_nested(attr, br_spec, rem) {
5114 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5115 if (nla_len(attr) < sizeof(flags))
5119 flags = nla_get_u16(attr);
5125 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5126 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5128 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
5133 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
5138 flags &= ~BRIDGE_FLAGS_MASTER;
5141 if ((flags & BRIDGE_FLAGS_SELF)) {
5142 if (!dev->netdev_ops->ndo_bridge_setlink)
5145 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
5149 flags &= ~BRIDGE_FLAGS_SELF;
5151 /* Generate event to notify upper layer of bridge
5154 err = rtnl_bridge_notify(dev);
5159 memcpy(nla_data(attr), &flags, sizeof(flags));
5164 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5165 struct netlink_ext_ack *extack)
5167 struct net *net = sock_net(skb->sk);
5168 struct ifinfomsg *ifm;
5169 struct net_device *dev;
5170 struct nlattr *br_spec, *attr = NULL;
5171 int rem, err = -EOPNOTSUPP;
5173 bool have_flags = false;
5175 if (nlmsg_len(nlh) < sizeof(*ifm))
5178 ifm = nlmsg_data(nlh);
5179 if (ifm->ifi_family != AF_BRIDGE)
5180 return -EPFNOSUPPORT;
5182 dev = __dev_get_by_index(net, ifm->ifi_index);
5184 NL_SET_ERR_MSG(extack, "unknown ifindex");
5188 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5190 nla_for_each_nested(attr, br_spec, rem) {
5191 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5192 if (nla_len(attr) < sizeof(flags))
5196 flags = nla_get_u16(attr);
5202 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5203 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5205 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5210 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5214 flags &= ~BRIDGE_FLAGS_MASTER;
5217 if ((flags & BRIDGE_FLAGS_SELF)) {
5218 if (!dev->netdev_ops->ndo_bridge_dellink)
5221 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5225 flags &= ~BRIDGE_FLAGS_SELF;
5227 /* Generate event to notify upper layer of bridge
5230 err = rtnl_bridge_notify(dev);
5235 memcpy(nla_data(attr), &flags, sizeof(flags));
5240 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5242 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5243 (!idxattr || idxattr == attrid);
5247 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
5249 return dev->netdev_ops &&
5250 dev->netdev_ops->ndo_has_offload_stats &&
5251 dev->netdev_ops->ndo_get_offload_stats &&
5252 dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
5256 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5258 return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5259 sizeof(struct rtnl_link_stats64) : 0;
5263 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5264 struct sk_buff *skb)
5266 unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5267 struct nlattr *attr = NULL;
5274 attr = nla_reserve_64bit(skb, attr_id, size,
5275 IFLA_OFFLOAD_XSTATS_UNSPEC);
5279 attr_data = nla_data(attr);
5280 memset(attr_data, 0, size);
5282 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5290 rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
5291 enum netdev_offload_xstats_type type)
5293 bool enabled = netdev_offload_xstats_enabled(dev, type);
5295 return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
5298 struct rtnl_offload_xstats_request_used {
5304 rtnl_offload_xstats_get_stats(struct net_device *dev,
5305 enum netdev_offload_xstats_type type,
5306 struct rtnl_offload_xstats_request_used *ru,
5307 struct rtnl_hw_stats64 *stats,
5308 struct netlink_ext_ack *extack)
5314 request = netdev_offload_xstats_enabled(dev, type);
5320 err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
5326 ru->request = request;
5333 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
5334 struct rtnl_offload_xstats_request_used *ru)
5336 struct nlattr *nest;
5338 nest = nla_nest_start(skb, attr_id);
5342 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
5343 goto nla_put_failure;
5345 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
5346 goto nla_put_failure;
5348 nla_nest_end(skb, nest);
5352 nla_nest_cancel(skb, nest);
5357 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
5358 struct netlink_ext_ack *extack)
5360 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5361 struct rtnl_offload_xstats_request_used ru_l3;
5362 struct nlattr *nest;
5365 err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
5369 nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5373 if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
5374 IFLA_OFFLOAD_XSTATS_L3_STATS,
5376 goto nla_put_failure;
5378 nla_nest_end(skb, nest);
5382 nla_nest_cancel(skb, nest);
5386 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
5387 int *prividx, u32 off_filter_mask,
5388 struct netlink_ext_ack *extack)
5390 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5391 int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
5392 int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5393 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5394 bool have_data = false;
5397 if (*prividx <= attr_id_cpu_hit &&
5399 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5400 err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5403 } else if (err != -ENODATA) {
5404 *prividx = attr_id_cpu_hit;
5409 if (*prividx <= attr_id_hw_s_info &&
5410 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
5411 *prividx = attr_id_hw_s_info;
5413 err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
5421 if (*prividx <= attr_id_l3_stats &&
5422 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
5423 unsigned int size_l3;
5424 struct nlattr *attr;
5426 *prividx = attr_id_l3_stats;
5428 size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5431 attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
5432 IFLA_OFFLOAD_XSTATS_UNSPEC);
5436 err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
5437 nla_data(attr), extack);
5454 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
5455 enum netdev_offload_xstats_type type)
5457 bool enabled = netdev_offload_xstats_enabled(dev, type);
5459 return nla_total_size(0) +
5460 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
5461 nla_total_size(sizeof(u8)) +
5462 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5463 (enabled ? nla_total_size(sizeof(u8)) : 0) +
5468 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
5470 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5472 return nla_total_size(0) +
5473 /* IFLA_OFFLOAD_XSTATS_L3_STATS */
5474 rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
5478 static int rtnl_offload_xstats_get_size(const struct net_device *dev,
5479 u32 off_filter_mask)
5481 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5482 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5486 if (off_filter_mask &
5487 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5488 size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
5489 nla_size += nla_total_size_64bit(size);
5492 if (off_filter_mask &
5493 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
5494 nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
5496 if (off_filter_mask &
5497 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
5498 size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5499 nla_size += nla_total_size_64bit(size);
5503 nla_size += nla_total_size(0);
5508 struct rtnl_stats_dump_filters {
5509 /* mask[0] filters outer attributes. Then individual nests have their
5510 * filtering mask at the index of the nested attribute.
5512 u32 mask[IFLA_STATS_MAX + 1];
5515 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5516 int type, u32 pid, u32 seq, u32 change,
5518 const struct rtnl_stats_dump_filters *filters,
5519 int *idxattr, int *prividx,
5520 struct netlink_ext_ack *extack)
5522 unsigned int filter_mask = filters->mask[0];
5523 struct if_stats_msg *ifsm;
5524 struct nlmsghdr *nlh;
5525 struct nlattr *attr;
5526 int s_prividx = *prividx;
5531 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5535 ifsm = nlmsg_data(nlh);
5536 ifsm->family = PF_UNSPEC;
5539 ifsm->ifindex = dev->ifindex;
5540 ifsm->filter_mask = filter_mask;
5542 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5543 struct rtnl_link_stats64 *sp;
5545 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5546 sizeof(struct rtnl_link_stats64),
5550 goto nla_put_failure;
5553 sp = nla_data(attr);
5554 dev_get_stats(dev, sp);
5557 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5558 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5560 if (ops && ops->fill_linkxstats) {
5561 *idxattr = IFLA_STATS_LINK_XSTATS;
5562 attr = nla_nest_start_noflag(skb,
5563 IFLA_STATS_LINK_XSTATS);
5566 goto nla_put_failure;
5569 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5570 nla_nest_end(skb, attr);
5572 goto nla_put_failure;
5577 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5579 const struct rtnl_link_ops *ops = NULL;
5580 const struct net_device *master;
5582 master = netdev_master_upper_dev_get(dev);
5584 ops = master->rtnl_link_ops;
5585 if (ops && ops->fill_linkxstats) {
5586 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5587 attr = nla_nest_start_noflag(skb,
5588 IFLA_STATS_LINK_XSTATS_SLAVE);
5591 goto nla_put_failure;
5594 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5595 nla_nest_end(skb, attr);
5597 goto nla_put_failure;
5602 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5604 u32 off_filter_mask;
5606 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5607 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5608 attr = nla_nest_start_noflag(skb,
5609 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5612 goto nla_put_failure;
5615 err = rtnl_offload_xstats_fill(skb, dev, prividx,
5616 off_filter_mask, extack);
5617 if (err == -ENODATA)
5618 nla_nest_cancel(skb, attr);
5620 nla_nest_end(skb, attr);
5622 if (err && err != -ENODATA)
5623 goto nla_put_failure;
5627 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5628 struct rtnl_af_ops *af_ops;
5630 *idxattr = IFLA_STATS_AF_SPEC;
5631 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5634 goto nla_put_failure;
5638 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5639 if (af_ops->fill_stats_af) {
5642 af = nla_nest_start_noflag(skb,
5647 goto nla_put_failure;
5649 err = af_ops->fill_stats_af(skb, dev);
5651 if (err == -ENODATA) {
5652 nla_nest_cancel(skb, af);
5653 } else if (err < 0) {
5655 goto nla_put_failure;
5658 nla_nest_end(skb, af);
5663 nla_nest_end(skb, attr);
5668 nlmsg_end(skb, nlh);
5673 /* not a multi message or no progress mean a real error */
5674 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5675 nlmsg_cancel(skb, nlh);
5677 nlmsg_end(skb, nlh);
5682 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5683 const struct rtnl_stats_dump_filters *filters)
5685 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5686 unsigned int filter_mask = filters->mask[0];
5688 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5689 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5691 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5692 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5693 int attr = IFLA_STATS_LINK_XSTATS;
5695 if (ops && ops->get_linkxstats_size) {
5696 size += nla_total_size(ops->get_linkxstats_size(dev,
5698 /* for IFLA_STATS_LINK_XSTATS */
5699 size += nla_total_size(0);
5703 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5704 struct net_device *_dev = (struct net_device *)dev;
5705 const struct rtnl_link_ops *ops = NULL;
5706 const struct net_device *master;
5708 /* netdev_master_upper_dev_get can't take const */
5709 master = netdev_master_upper_dev_get(_dev);
5711 ops = master->rtnl_link_ops;
5712 if (ops && ops->get_linkxstats_size) {
5713 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5715 size += nla_total_size(ops->get_linkxstats_size(dev,
5717 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5718 size += nla_total_size(0);
5722 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
5723 u32 off_filter_mask;
5725 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5726 size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
5729 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5730 struct rtnl_af_ops *af_ops;
5732 /* for IFLA_STATS_AF_SPEC */
5733 size += nla_total_size(0);
5736 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5737 if (af_ops->get_stats_af_size) {
5738 size += nla_total_size(
5739 af_ops->get_stats_af_size(dev));
5742 size += nla_total_size(0);
5751 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
5753 static const struct nla_policy
5754 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
5755 [IFLA_STATS_LINK_OFFLOAD_XSTATS] =
5756 NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
5759 static const struct nla_policy
5760 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
5761 [IFLA_STATS_GET_FILTERS] =
5762 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
5765 static const struct nla_policy
5766 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
5767 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
5770 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
5771 struct rtnl_stats_dump_filters *filters,
5772 struct netlink_ext_ack *extack)
5774 struct nlattr *tb[IFLA_STATS_MAX + 1];
5778 err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
5779 rtnl_stats_get_policy_filters, extack);
5783 for (at = 1; at <= IFLA_STATS_MAX; at++) {
5785 if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
5786 NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
5789 filters->mask[at] = nla_get_u32(tb[at]);
5796 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
5798 struct rtnl_stats_dump_filters *filters,
5799 struct netlink_ext_ack *extack)
5801 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5805 filters->mask[0] = filter_mask;
5806 for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
5807 filters->mask[i] = -1U;
5809 err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
5810 IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
5814 if (tb[IFLA_STATS_GET_FILTERS]) {
5815 err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
5824 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5825 bool is_dump, struct netlink_ext_ack *extack)
5827 struct if_stats_msg *ifsm;
5829 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5830 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5837 ifsm = nlmsg_data(nlh);
5839 /* only requests using strict checks can pass data to influence
5840 * the dump. The legacy exception is filter_mask.
5842 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5843 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5846 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5847 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5854 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5855 struct netlink_ext_ack *extack)
5857 struct rtnl_stats_dump_filters filters;
5858 struct net *net = sock_net(skb->sk);
5859 struct net_device *dev = NULL;
5860 int idxattr = 0, prividx = 0;
5861 struct if_stats_msg *ifsm;
5862 struct sk_buff *nskb;
5865 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5870 ifsm = nlmsg_data(nlh);
5871 if (ifsm->ifindex > 0)
5872 dev = __dev_get_by_index(net, ifsm->ifindex);
5879 if (!ifsm->filter_mask) {
5880 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
5884 err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
5888 nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
5892 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5893 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5894 0, &filters, &idxattr, &prividx, extack);
5896 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5897 WARN_ON(err == -EMSGSIZE);
5900 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5906 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5908 struct netlink_ext_ack *extack = cb->extack;
5909 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5910 struct rtnl_stats_dump_filters filters;
5911 struct net *net = sock_net(skb->sk);
5912 unsigned int flags = NLM_F_MULTI;
5913 struct if_stats_msg *ifsm;
5914 struct hlist_head *head;
5915 struct net_device *dev;
5919 s_idx = cb->args[1];
5920 s_idxattr = cb->args[2];
5921 s_prividx = cb->args[3];
5923 cb->seq = net->dev_base_seq;
5925 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5929 ifsm = nlmsg_data(cb->nlh);
5930 if (!ifsm->filter_mask) {
5931 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5935 err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
5940 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5942 head = &net->dev_index_head[h];
5943 hlist_for_each_entry(dev, head, index_hlist) {
5946 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5947 NETLINK_CB(cb->skb).portid,
5948 cb->nlh->nlmsg_seq, 0,
5950 &s_idxattr, &s_prividx,
5952 /* If we ran out of room on the first message,
5955 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5961 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5967 cb->args[3] = s_prividx;
5968 cb->args[2] = s_idxattr;
5975 void rtnl_offload_xstats_notify(struct net_device *dev)
5977 struct rtnl_stats_dump_filters response_filters = {};
5978 struct net *net = dev_net(dev);
5979 int idxattr = 0, prividx = 0;
5980 struct sk_buff *skb;
5985 response_filters.mask[0] |=
5986 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
5987 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
5988 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5990 skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
5995 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
5996 &response_filters, &idxattr, &prividx, NULL);
6002 rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
6006 rtnl_set_sk_err(net, RTNLGRP_STATS, err);
6008 EXPORT_SYMBOL(rtnl_offload_xstats_notify);
6010 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
6011 struct netlink_ext_ack *extack)
6013 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
6014 struct rtnl_stats_dump_filters response_filters = {};
6015 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
6016 struct net *net = sock_net(skb->sk);
6017 struct net_device *dev = NULL;
6018 struct if_stats_msg *ifsm;
6019 bool notify = false;
6022 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
6027 ifsm = nlmsg_data(nlh);
6028 if (ifsm->family != AF_UNSPEC) {
6029 NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
6033 if (ifsm->ifindex > 0)
6034 dev = __dev_get_by_index(net, ifsm->ifindex);
6041 if (ifsm->filter_mask) {
6042 NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
6046 err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
6047 ifla_stats_set_policy, extack);
6051 if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
6052 u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
6055 err = netdev_offload_xstats_enable(dev, t_l3, extack);
6057 err = netdev_offload_xstats_disable(dev, t_l3);
6061 else if (err != -EALREADY)
6064 response_filters.mask[0] |=
6065 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
6066 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
6067 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
6071 rtnl_offload_xstats_notify(dev);
6076 static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh,
6077 struct netlink_ext_ack *extack)
6079 struct br_port_msg *bpm;
6081 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
6082 NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request");
6086 bpm = nlmsg_data(nlh);
6088 NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request");
6091 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
6092 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
6099 struct rtnl_mdb_dump_ctx {
6103 static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
6105 struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx;
6106 struct net *net = sock_net(skb->sk);
6107 struct net_device *dev;
6111 NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx);
6113 if (cb->strict_check) {
6114 err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack);
6122 for_each_netdev(net, dev) {
6125 if (!dev->netdev_ops->ndo_mdb_dump)
6128 err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb);
6129 if (err == -EMSGSIZE)
6131 /* Moving on to next device, reset markers and sequence
6132 * counters since they are all maintained per-device.
6134 memset(cb->ctx, 0, sizeof(cb->ctx));
6146 static int rtnl_validate_mdb_entry(const struct nlattr *attr,
6147 struct netlink_ext_ack *extack)
6149 struct br_mdb_entry *entry = nla_data(attr);
6151 if (nla_len(attr) != sizeof(struct br_mdb_entry)) {
6152 NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length");
6156 if (entry->ifindex == 0) {
6157 NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed");
6161 if (entry->addr.proto == htons(ETH_P_IP)) {
6162 if (!ipv4_is_multicast(entry->addr.u.ip4) &&
6163 !ipv4_is_zeronet(entry->addr.u.ip4)) {
6164 NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0");
6167 if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
6168 NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast");
6171 #if IS_ENABLED(CONFIG_IPV6)
6172 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
6173 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
6174 NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes");
6178 } else if (entry->addr.proto == 0) {
6180 if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
6181 NL_SET_ERR_MSG(extack, "L2 entry group is not multicast");
6185 NL_SET_ERR_MSG(extack, "Unknown entry protocol");
6189 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
6190 NL_SET_ERR_MSG(extack, "Unknown entry state");
6193 if (entry->vid >= VLAN_VID_MASK) {
6194 NL_SET_ERR_MSG(extack, "Invalid entry VLAN id");
6201 static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = {
6202 [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 },
6203 [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
6204 rtnl_validate_mdb_entry,
6205 sizeof(struct br_mdb_entry)),
6206 [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED },
6209 static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
6210 struct netlink_ext_ack *extack)
6212 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6213 struct net *net = sock_net(skb->sk);
6214 struct br_port_msg *bpm;
6215 struct net_device *dev;
6218 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6219 MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6223 bpm = nlmsg_data(nlh);
6224 if (!bpm->ifindex) {
6225 NL_SET_ERR_MSG(extack, "Invalid ifindex");
6229 dev = __dev_get_by_index(net, bpm->ifindex);
6231 NL_SET_ERR_MSG(extack, "Device doesn't exist");
6235 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6236 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6240 if (!dev->netdev_ops->ndo_mdb_add) {
6241 NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6245 return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack);
6248 static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
6249 struct netlink_ext_ack *extack)
6251 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6252 struct net *net = sock_net(skb->sk);
6253 struct br_port_msg *bpm;
6254 struct net_device *dev;
6257 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6258 MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6262 bpm = nlmsg_data(nlh);
6263 if (!bpm->ifindex) {
6264 NL_SET_ERR_MSG(extack, "Invalid ifindex");
6268 dev = __dev_get_by_index(net, bpm->ifindex);
6270 NL_SET_ERR_MSG(extack, "Device doesn't exist");
6274 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6275 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6279 if (!dev->netdev_ops->ndo_mdb_del) {
6280 NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6284 return dev->netdev_ops->ndo_mdb_del(dev, tb, extack);
6287 /* Process one rtnetlink message. */
6289 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
6290 struct netlink_ext_ack *extack)
6292 struct net *net = sock_net(skb->sk);
6293 struct rtnl_link *link;
6294 enum rtnl_kinds kind;
6295 struct module *owner;
6296 int err = -EOPNOTSUPP;
6297 rtnl_doit_func doit;
6302 type = nlh->nlmsg_type;
6308 /* All the messages must have at least 1 byte length */
6309 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
6312 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
6313 kind = rtnl_msgtype_kind(type);
6315 if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
6319 if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
6321 rtnl_dumpit_func dumpit;
6322 u32 min_dump_alloc = 0;
6324 link = rtnl_get_link(family, type);
6325 if (!link || !link->dumpit) {
6327 link = rtnl_get_link(family, type);
6328 if (!link || !link->dumpit)
6331 owner = link->owner;
6332 dumpit = link->dumpit;
6334 if (type == RTM_GETLINK - RTM_BASE)
6335 min_dump_alloc = rtnl_calcit(skb, nlh);
6338 /* need to do this before rcu_read_unlock() */
6339 if (!try_module_get(owner))
6340 err = -EPROTONOSUPPORT;
6346 struct netlink_dump_control c = {
6348 .min_dump_alloc = min_dump_alloc,
6351 err = netlink_dump_start(rtnl, skb, nlh, &c);
6352 /* netlink_dump_start() will keep a reference on
6353 * module if dump is still in progress.
6360 link = rtnl_get_link(family, type);
6361 if (!link || !link->doit) {
6363 link = rtnl_get_link(PF_UNSPEC, type);
6364 if (!link || !link->doit)
6368 owner = link->owner;
6369 if (!try_module_get(owner)) {
6370 err = -EPROTONOSUPPORT;
6374 flags = link->flags;
6375 if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6376 !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6377 NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6382 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6386 err = doit(skb, nlh, extack);
6393 link = rtnl_get_link(family, type);
6394 if (link && link->doit)
6395 err = link->doit(skb, nlh, extack);
6411 static void rtnetlink_rcv(struct sk_buff *skb)
6413 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
6416 static int rtnetlink_bind(struct net *net, int group)
6419 case RTNLGRP_IPV4_MROUTE_R:
6420 case RTNLGRP_IPV6_MROUTE_R:
6421 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
6428 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
6430 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6434 case NETDEV_CHANGEMTU:
6435 case NETDEV_CHANGEADDR:
6436 case NETDEV_CHANGENAME:
6437 case NETDEV_FEAT_CHANGE:
6438 case NETDEV_BONDING_FAILOVER:
6439 case NETDEV_POST_TYPE_CHANGE:
6440 case NETDEV_NOTIFY_PEERS:
6441 case NETDEV_CHANGEUPPER:
6442 case NETDEV_RESEND_IGMP:
6443 case NETDEV_CHANGEINFODATA:
6444 case NETDEV_CHANGELOWERSTATE:
6445 case NETDEV_CHANGE_TX_QUEUE_LEN:
6446 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6447 GFP_KERNEL, NULL, 0, 0, NULL);
6455 static struct notifier_block rtnetlink_dev_notifier = {
6456 .notifier_call = rtnetlink_event,
6460 static int __net_init rtnetlink_net_init(struct net *net)
6463 struct netlink_kernel_cfg cfg = {
6464 .groups = RTNLGRP_MAX,
6465 .input = rtnetlink_rcv,
6466 .cb_mutex = &rtnl_mutex,
6467 .flags = NL_CFG_F_NONROOT_RECV,
6468 .bind = rtnetlink_bind,
6471 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
6478 static void __net_exit rtnetlink_net_exit(struct net *net)
6480 netlink_kernel_release(net->rtnl);
6484 static struct pernet_operations rtnetlink_net_ops = {
6485 .init = rtnetlink_net_init,
6486 .exit = rtnetlink_net_exit,
6489 void __init rtnetlink_init(void)
6491 if (register_pernet_subsys(&rtnetlink_net_ops))
6492 panic("rtnetlink_init: cannot initialize rtnetlink\n");
6494 register_netdevice_notifier(&rtnetlink_dev_notifier);
6496 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6497 rtnl_dump_ifinfo, 0);
6498 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6499 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6500 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6502 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6503 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6504 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6506 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
6507 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
6509 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6510 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
6511 RTNL_FLAG_BULK_DEL_SUPPORTED);
6512 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6514 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6515 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6516 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6518 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6520 rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6522 rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0);
6523 rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
6524 rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0);