1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
62 rtnl_dumpit_func dumpit;
68 static DEFINE_MUTEX(rtnl_mutex);
72 mutex_lock(&rtnl_mutex);
74 EXPORT_SYMBOL(rtnl_lock);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex);
80 EXPORT_SYMBOL(rtnl_lock_killable);
82 static struct sk_buff *defer_kfree_skb_list;
83 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
90 EXPORT_SYMBOL(rtnl_kfree_skbs);
92 void __rtnl_unlock(void)
94 struct sk_buff *head = defer_kfree_skb_list;
96 defer_kfree_skb_list = NULL;
98 mutex_unlock(&rtnl_mutex);
101 struct sk_buff *next = head->next;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex);
120 EXPORT_SYMBOL(rtnl_trylock);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex);
126 EXPORT_SYMBOL(rtnl_is_locked);
128 bool refcount_dec_and_rtnl_lock(refcount_t *r)
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
144 static inline int rtm_msgindex(int msgtype)
146 int msgindex = msgtype - RTM_BASE;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
158 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
160 struct rtnl_link **tab;
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
172 static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
183 msgindex = rtm_msgindex(msgtype);
186 tab = rtnl_msg_handlers[protocol];
188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
196 old = rtnl_dereference(tab[msgindex]);
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
207 WARN_ON(link->owner && link->owner != owner);
210 WARN_ON(doit && link->doit && link->doit != doit);
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
215 link->dumpit = dumpit;
217 link->flags |= flags;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
249 EXPORT_SYMBOL_GPL(rtnl_register_module);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol, int msgtype,
268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol, int msgtype)
289 struct rtnl_link **tab, *link;
292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293 msgindex = rtm_msgindex(msgtype);
296 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 link = tab[msgindex];
303 rcu_assign_pointer(tab[msgindex], NULL);
306 kfree_rcu(link, rcu);
310 EXPORT_SYMBOL_GPL(rtnl_unregister);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol)
321 struct rtnl_link **tab, *link;
324 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
327 tab = rtnl_msg_handlers[protocol];
332 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
333 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334 link = tab[msgindex];
338 rcu_assign_pointer(tab[msgindex], NULL);
339 kfree_rcu(link, rcu);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
349 static LIST_HEAD(link_ops);
351 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
353 const struct rtnl_link_ops *ops;
355 list_for_each_entry(ops, &link_ops, list) {
356 if (!strcmp(ops->kind, kind))
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops *ops)
374 if (rtnl_link_ops_get(ops->kind))
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops->setup && !ops->dellink)
383 ops->dellink = unregister_netdevice_queue;
385 list_add_tail(&ops->list, &link_ops);
388 EXPORT_SYMBOL_GPL(__rtnl_link_register);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops *ops)
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
406 err = __rtnl_link_register(ops);
410 EXPORT_SYMBOL_GPL(rtnl_link_register);
412 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
414 struct net_device *dev;
415 LIST_HEAD(list_kill);
417 for_each_netdev(net, dev) {
418 if (dev->rtnl_link_ops == ops)
419 ops->dellink(dev, &list_kill);
421 unregister_netdevice_many(&list_kill);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
437 __rtnl_kill_links(net, ops);
439 list_del(&ops->list);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
450 DEFINE_WAIT_FUNC(wait, woken_wake_function);
452 add_wait_queue(&netdev_unregistering_wq, &wait);
454 unregistering = false;
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
460 if (net->dev_unreg_count > 0) {
461 unregistering = true;
469 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
471 remove_wait_queue(&netdev_unregistering_wq, &wait);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops *ops)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops);
485 up_write(&pernet_ops_rwsem);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
491 struct net_device *master_dev;
492 const struct rtnl_link_ops *ops;
497 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
501 ops = master_dev->rtnl_link_ops;
502 if (!ops || !ops->get_slave_size)
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size = nla_total_size(sizeof(struct nlattr)) +
506 ops->get_slave_size(master_dev, dev);
513 static size_t rtnl_link_get_size(const struct net_device *dev)
515 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
525 /* IFLA_INFO_DATA + nested data */
526 size += nla_total_size(sizeof(struct nlattr)) +
529 if (ops->get_xstats_size)
530 /* IFLA_INFO_XSTATS */
531 size += nla_total_size(ops->get_xstats_size(dev));
533 size += rtnl_link_get_slave_info_data_size(dev);
538 static LIST_HEAD(rtnl_af_ops);
540 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
542 const struct rtnl_af_ops *ops;
544 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
545 if (ops->family == family)
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops *ops)
561 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
564 EXPORT_SYMBOL_GPL(rtnl_af_register);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops *ops)
573 list_del_rcu(&ops->list);
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
580 static size_t rtnl_link_get_af_size(const struct net_device *dev,
583 struct rtnl_af_ops *af_ops;
587 size = nla_total_size(sizeof(struct nlattr));
590 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
591 if (af_ops->get_link_af_size) {
592 /* AF_* + nested data */
593 size += nla_total_size(sizeof(struct nlattr)) +
594 af_ops->get_link_af_size(dev, ext_filter_mask);
602 static bool rtnl_have_link_slave_info(const struct net_device *dev)
604 struct net_device *master_dev;
609 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
610 if (master_dev && master_dev->rtnl_link_ops)
616 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617 const struct net_device *dev)
619 struct net_device *master_dev;
620 const struct rtnl_link_ops *ops;
621 struct nlattr *slave_data;
624 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
627 ops = master_dev->rtnl_link_ops;
630 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
632 if (ops->fill_slave_info) {
633 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
636 err = ops->fill_slave_info(skb, master_dev, dev);
638 goto err_cancel_slave_data;
639 nla_nest_end(skb, slave_data);
643 err_cancel_slave_data:
644 nla_nest_cancel(skb, slave_data);
648 static int rtnl_link_info_fill(struct sk_buff *skb,
649 const struct net_device *dev)
651 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
657 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
659 if (ops->fill_xstats) {
660 err = ops->fill_xstats(skb, dev);
664 if (ops->fill_info) {
665 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
668 err = ops->fill_info(skb, dev);
670 goto err_cancel_data;
671 nla_nest_end(skb, data);
676 nla_nest_cancel(skb, data);
680 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
682 struct nlattr *linkinfo;
685 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
686 if (linkinfo == NULL)
689 err = rtnl_link_info_fill(skb, dev);
691 goto err_cancel_link;
693 err = rtnl_link_slave_info_fill(skb, dev);
695 goto err_cancel_link;
697 nla_nest_end(skb, linkinfo);
701 nla_nest_cancel(skb, linkinfo);
706 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
708 struct sock *rtnl = net->rtnl;
711 NETLINK_CB(skb).dst_group = group;
713 refcount_inc(&skb->users);
714 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
716 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
720 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
722 struct sock *rtnl = net->rtnl;
724 return nlmsg_unicast(rtnl, skb, pid);
726 EXPORT_SYMBOL(rtnl_unicast);
728 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
729 struct nlmsghdr *nlh, gfp_t flags)
731 struct sock *rtnl = net->rtnl;
735 report = nlmsg_report(nlh);
737 nlmsg_notify(rtnl, skb, pid, group, report, flags);
739 EXPORT_SYMBOL(rtnl_notify);
741 void rtnl_set_sk_err(struct net *net, u32 group, int error)
743 struct sock *rtnl = net->rtnl;
745 netlink_set_err(rtnl, 0, group, error);
747 EXPORT_SYMBOL(rtnl_set_sk_err);
749 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
754 /* nothing is dumped for dst_default_metrics, so just skip the loop */
755 if (metrics == dst_default_metrics.metrics)
758 mx = nla_nest_start_noflag(skb, RTA_METRICS);
762 for (i = 0; i < RTAX_MAX; i++) {
764 if (i == RTAX_CC_ALGO - 1) {
765 char tmp[TCP_CA_NAME_MAX], *name;
767 name = tcp_ca_get_name_by_key(metrics[i], tmp);
770 if (nla_put_string(skb, i + 1, name))
771 goto nla_put_failure;
772 } else if (i == RTAX_FEATURES - 1) {
773 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
777 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
778 if (nla_put_u32(skb, i + 1, user_features))
779 goto nla_put_failure;
781 if (nla_put_u32(skb, i + 1, metrics[i]))
782 goto nla_put_failure;
789 nla_nest_cancel(skb, mx);
793 return nla_nest_end(skb, mx);
796 nla_nest_cancel(skb, mx);
799 EXPORT_SYMBOL(rtnetlink_put_metrics);
801 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
802 long expires, u32 error)
804 struct rta_cacheinfo ci = {
810 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
811 ci.rta_used = dst->__use;
812 ci.rta_clntref = atomic_read(&dst->__refcnt);
817 clock = jiffies_to_clock_t(abs(expires));
818 clock = min_t(unsigned long, clock, INT_MAX);
819 ci.rta_expires = (expires > 0) ? clock : -clock;
821 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
823 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
825 static void set_operstate(struct net_device *dev, unsigned char transition)
827 unsigned char operstate = dev->operstate;
829 switch (transition) {
831 if ((operstate == IF_OPER_DORMANT ||
832 operstate == IF_OPER_TESTING ||
833 operstate == IF_OPER_UNKNOWN) &&
834 !netif_dormant(dev) && !netif_testing(dev))
835 operstate = IF_OPER_UP;
838 case IF_OPER_TESTING:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_TESTING;
844 case IF_OPER_DORMANT:
845 if (operstate == IF_OPER_UP ||
846 operstate == IF_OPER_UNKNOWN)
847 operstate = IF_OPER_DORMANT;
851 if (dev->operstate != operstate) {
852 write_lock_bh(&dev_base_lock);
853 dev->operstate = operstate;
854 write_unlock_bh(&dev_base_lock);
855 netdev_state_change(dev);
859 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
861 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
862 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
865 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
866 const struct ifinfomsg *ifm)
868 unsigned int flags = ifm->ifi_flags;
870 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
872 flags = (flags & ifm->ifi_change) |
873 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
878 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
879 const struct rtnl_link_stats64 *b)
881 a->rx_packets = b->rx_packets;
882 a->tx_packets = b->tx_packets;
883 a->rx_bytes = b->rx_bytes;
884 a->tx_bytes = b->tx_bytes;
885 a->rx_errors = b->rx_errors;
886 a->tx_errors = b->tx_errors;
887 a->rx_dropped = b->rx_dropped;
888 a->tx_dropped = b->tx_dropped;
890 a->multicast = b->multicast;
891 a->collisions = b->collisions;
893 a->rx_length_errors = b->rx_length_errors;
894 a->rx_over_errors = b->rx_over_errors;
895 a->rx_crc_errors = b->rx_crc_errors;
896 a->rx_frame_errors = b->rx_frame_errors;
897 a->rx_fifo_errors = b->rx_fifo_errors;
898 a->rx_missed_errors = b->rx_missed_errors;
900 a->tx_aborted_errors = b->tx_aborted_errors;
901 a->tx_carrier_errors = b->tx_carrier_errors;
902 a->tx_fifo_errors = b->tx_fifo_errors;
903 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
904 a->tx_window_errors = b->tx_window_errors;
906 a->rx_compressed = b->rx_compressed;
907 a->tx_compressed = b->tx_compressed;
909 a->rx_nohandler = b->rx_nohandler;
913 static inline int rtnl_vfinfo_size(const struct net_device *dev,
916 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
917 int num_vfs = dev_num_vf(dev->dev.parent);
918 size_t size = nla_total_size(0);
921 nla_total_size(sizeof(struct ifla_vf_mac)) +
922 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
923 nla_total_size(sizeof(struct ifla_vf_vlan)) +
924 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
925 nla_total_size(MAX_VLAN_LIST_LEN *
926 sizeof(struct ifla_vf_vlan_info)) +
927 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
928 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
929 nla_total_size(sizeof(struct ifla_vf_rate)) +
930 nla_total_size(sizeof(struct ifla_vf_link_state)) +
931 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
932 nla_total_size(sizeof(struct ifla_vf_trust)));
933 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
935 (nla_total_size(0) + /* nest IFLA_VF_STATS */
936 /* IFLA_VF_STATS_RX_PACKETS */
937 nla_total_size_64bit(sizeof(__u64)) +
938 /* IFLA_VF_STATS_TX_PACKETS */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_RX_BYTES */
941 nla_total_size_64bit(sizeof(__u64)) +
942 /* IFLA_VF_STATS_TX_BYTES */
943 nla_total_size_64bit(sizeof(__u64)) +
944 /* IFLA_VF_STATS_BROADCAST */
945 nla_total_size_64bit(sizeof(__u64)) +
946 /* IFLA_VF_STATS_MULTICAST */
947 nla_total_size_64bit(sizeof(__u64)) +
948 /* IFLA_VF_STATS_RX_DROPPED */
949 nla_total_size_64bit(sizeof(__u64)) +
950 /* IFLA_VF_STATS_TX_DROPPED */
951 nla_total_size_64bit(sizeof(__u64)));
958 static size_t rtnl_port_size(const struct net_device *dev,
961 size_t port_size = nla_total_size(4) /* PORT_VF */
962 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
963 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
964 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
965 + nla_total_size(1) /* PROT_VDP_REQUEST */
966 + nla_total_size(2); /* PORT_VDP_RESPONSE */
967 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
968 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
970 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
973 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
974 !(ext_filter_mask & RTEXT_FILTER_VF))
976 if (dev_num_vf(dev->dev.parent))
977 return port_self_size + vf_ports_size +
978 vf_port_size * dev_num_vf(dev->dev.parent);
980 return port_self_size;
983 static size_t rtnl_xdp_size(void)
985 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
986 nla_total_size(1) + /* XDP_ATTACHED */
987 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
988 nla_total_size(4); /* XDP_<mode>_PROG_ID */
993 static size_t rtnl_prop_list_size(const struct net_device *dev)
995 struct netdev_name_node *name_node;
998 if (list_empty(&dev->name_node->list))
1000 size = nla_total_size(0);
1001 list_for_each_entry(name_node, &dev->name_node->list, list)
1002 size += nla_total_size(ALTIFNAMSIZ);
1006 static size_t rtnl_proto_down_size(const struct net_device *dev)
1008 size_t size = nla_total_size(1);
1010 if (dev->proto_down_reason)
1011 size += nla_total_size(0) + nla_total_size(4);
1016 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1017 u32 ext_filter_mask)
1019 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1020 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1021 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1022 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1023 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1024 + nla_total_size(sizeof(struct rtnl_link_stats))
1025 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1026 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1027 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1028 + nla_total_size(4) /* IFLA_TXQLEN */
1029 + nla_total_size(4) /* IFLA_WEIGHT */
1030 + nla_total_size(4) /* IFLA_MTU */
1031 + nla_total_size(4) /* IFLA_LINK */
1032 + nla_total_size(4) /* IFLA_MASTER */
1033 + nla_total_size(1) /* IFLA_CARRIER */
1034 + nla_total_size(4) /* IFLA_PROMISCUITY */
1035 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1036 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1037 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1038 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1039 + nla_total_size(1) /* IFLA_OPERSTATE */
1040 + nla_total_size(1) /* IFLA_LINKMODE */
1041 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1042 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1043 + nla_total_size(4) /* IFLA_GROUP */
1044 + nla_total_size(ext_filter_mask
1045 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1046 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1047 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1048 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1049 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1050 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1051 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1052 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1053 + rtnl_xdp_size() /* IFLA_XDP */
1054 + nla_total_size(4) /* IFLA_EVENT */
1055 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1056 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1057 + rtnl_proto_down_size(dev) /* proto down */
1058 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1059 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1060 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1061 + nla_total_size(4) /* IFLA_MIN_MTU */
1062 + nla_total_size(4) /* IFLA_MAX_MTU */
1063 + rtnl_prop_list_size(dev)
1064 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1068 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1070 struct nlattr *vf_ports;
1071 struct nlattr *vf_port;
1075 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1079 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1080 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1082 goto nla_put_failure;
1083 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1084 goto nla_put_failure;
1085 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1086 if (err == -EMSGSIZE)
1087 goto nla_put_failure;
1089 nla_nest_cancel(skb, vf_port);
1092 nla_nest_end(skb, vf_port);
1095 nla_nest_end(skb, vf_ports);
1100 nla_nest_cancel(skb, vf_ports);
1104 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1106 struct nlattr *port_self;
1109 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1113 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1115 nla_nest_cancel(skb, port_self);
1116 return (err == -EMSGSIZE) ? err : 0;
1119 nla_nest_end(skb, port_self);
1124 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1125 u32 ext_filter_mask)
1129 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1130 !(ext_filter_mask & RTEXT_FILTER_VF))
1133 err = rtnl_port_self_fill(skb, dev);
1137 if (dev_num_vf(dev->dev.parent)) {
1138 err = rtnl_vf_ports_fill(skb, dev);
1146 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1149 struct netdev_phys_item_id ppid;
1151 err = dev_get_phys_port_id(dev, &ppid);
1153 if (err == -EOPNOTSUPP)
1158 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1164 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1166 char name[IFNAMSIZ];
1169 err = dev_get_phys_port_name(dev, name, sizeof(name));
1171 if (err == -EOPNOTSUPP)
1176 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1182 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1184 struct netdev_phys_item_id ppid = { };
1187 err = dev_get_port_parent_id(dev, &ppid, false);
1189 if (err == -EOPNOTSUPP)
1194 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1200 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1201 struct net_device *dev)
1203 struct rtnl_link_stats64 *sp;
1204 struct nlattr *attr;
1206 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1207 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1211 sp = nla_data(attr);
1212 dev_get_stats(dev, sp);
1214 attr = nla_reserve(skb, IFLA_STATS,
1215 sizeof(struct rtnl_link_stats));
1219 copy_rtnl_link_stats(nla_data(attr), sp);
1224 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1225 struct net_device *dev,
1227 struct nlattr *vfinfo,
1228 u32 ext_filter_mask)
1230 struct ifla_vf_rss_query_en vf_rss_query_en;
1231 struct nlattr *vf, *vfstats, *vfvlanlist;
1232 struct ifla_vf_link_state vf_linkstate;
1233 struct ifla_vf_vlan_info vf_vlan_info;
1234 struct ifla_vf_spoofchk vf_spoofchk;
1235 struct ifla_vf_tx_rate vf_tx_rate;
1236 struct ifla_vf_stats vf_stats;
1237 struct ifla_vf_trust vf_trust;
1238 struct ifla_vf_vlan vf_vlan;
1239 struct ifla_vf_rate vf_rate;
1240 struct ifla_vf_mac vf_mac;
1241 struct ifla_vf_broadcast vf_broadcast;
1242 struct ifla_vf_info ivi;
1243 struct ifla_vf_guid node_guid;
1244 struct ifla_vf_guid port_guid;
1246 memset(&ivi, 0, sizeof(ivi));
1248 /* Not all SR-IOV capable drivers support the
1249 * spoofcheck and "RSS query enable" query. Preset to
1250 * -1 so the user space tool can detect that the driver
1251 * didn't report anything.
1254 ivi.rss_query_en = -1;
1256 /* The default value for VF link state is "auto"
1257 * IFLA_VF_LINK_STATE_AUTO which equals zero
1260 /* VLAN Protocol by default is 802.1Q */
1261 ivi.vlan_proto = htons(ETH_P_8021Q);
1262 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1265 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1266 memset(&node_guid, 0, sizeof(node_guid));
1267 memset(&port_guid, 0, sizeof(port_guid));
1276 vf_rss_query_en.vf =
1279 port_guid.vf = ivi.vf;
1281 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1282 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1283 vf_vlan.vlan = ivi.vlan;
1284 vf_vlan.qos = ivi.qos;
1285 vf_vlan_info.vlan = ivi.vlan;
1286 vf_vlan_info.qos = ivi.qos;
1287 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1288 vf_tx_rate.rate = ivi.max_tx_rate;
1289 vf_rate.min_tx_rate = ivi.min_tx_rate;
1290 vf_rate.max_tx_rate = ivi.max_tx_rate;
1291 vf_spoofchk.setting = ivi.spoofchk;
1292 vf_linkstate.link_state = ivi.linkstate;
1293 vf_rss_query_en.setting = ivi.rss_query_en;
1294 vf_trust.setting = ivi.trusted;
1295 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1297 goto nla_put_vfinfo_failure;
1298 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1299 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1300 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1301 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1303 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1305 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1307 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1309 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1310 sizeof(vf_rss_query_en),
1311 &vf_rss_query_en) ||
1312 nla_put(skb, IFLA_VF_TRUST,
1313 sizeof(vf_trust), &vf_trust))
1314 goto nla_put_vf_failure;
1316 if (dev->netdev_ops->ndo_get_vf_guid &&
1317 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1319 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1321 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1323 goto nla_put_vf_failure;
1325 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1327 goto nla_put_vf_failure;
1328 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1330 nla_nest_cancel(skb, vfvlanlist);
1331 goto nla_put_vf_failure;
1333 nla_nest_end(skb, vfvlanlist);
1334 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
1335 memset(&vf_stats, 0, sizeof(vf_stats));
1336 if (dev->netdev_ops->ndo_get_vf_stats)
1337 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1339 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1341 goto nla_put_vf_failure;
1342 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1343 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1344 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1345 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1346 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1347 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1348 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1349 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1350 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1351 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1352 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1353 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1354 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1355 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1356 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1357 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1358 nla_nest_cancel(skb, vfstats);
1359 goto nla_put_vf_failure;
1361 nla_nest_end(skb, vfstats);
1363 nla_nest_end(skb, vf);
1367 nla_nest_cancel(skb, vf);
1368 nla_put_vfinfo_failure:
1369 nla_nest_cancel(skb, vfinfo);
1373 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1374 struct net_device *dev,
1375 u32 ext_filter_mask)
1377 struct nlattr *vfinfo;
1380 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1383 num_vfs = dev_num_vf(dev->dev.parent);
1384 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1387 if (!dev->netdev_ops->ndo_get_vf_config)
1390 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1394 for (i = 0; i < num_vfs; i++) {
1395 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask))
1399 nla_nest_end(skb, vfinfo);
1403 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1405 struct rtnl_link_ifmap map;
1407 memset(&map, 0, sizeof(map));
1408 map.mem_start = dev->mem_start;
1409 map.mem_end = dev->mem_end;
1410 map.base_addr = dev->base_addr;
1413 map.port = dev->if_port;
1415 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1421 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1423 const struct bpf_prog *generic_xdp_prog;
1427 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1428 if (!generic_xdp_prog)
1430 return generic_xdp_prog->aux->id;
1433 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1435 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1438 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1440 return dev_xdp_prog_id(dev, XDP_MODE_HW);
1443 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1444 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1445 u32 (*get_prog_id)(struct net_device *dev))
1450 curr_id = get_prog_id(dev);
1455 err = nla_put_u32(skb, attr, curr_id);
1459 if (*mode != XDP_ATTACHED_NONE)
1460 *mode = XDP_ATTACHED_MULTI;
1467 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1474 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1479 mode = XDP_ATTACHED_NONE;
1480 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1481 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1484 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1485 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1488 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1489 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1493 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1497 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1498 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1503 nla_nest_end(skb, xdp);
1507 nla_nest_cancel(skb, xdp);
1511 static u32 rtnl_get_event(unsigned long event)
1513 u32 rtnl_event_type = IFLA_EVENT_NONE;
1517 rtnl_event_type = IFLA_EVENT_REBOOT;
1519 case NETDEV_FEAT_CHANGE:
1520 rtnl_event_type = IFLA_EVENT_FEATURES;
1522 case NETDEV_BONDING_FAILOVER:
1523 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1525 case NETDEV_NOTIFY_PEERS:
1526 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1528 case NETDEV_RESEND_IGMP:
1529 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1531 case NETDEV_CHANGEINFODATA:
1532 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1538 return rtnl_event_type;
1541 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1543 const struct net_device *upper_dev;
1548 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1550 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1556 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1559 int ifindex = dev_get_iflink(dev);
1561 if (force || dev->ifindex != ifindex)
1562 return nla_put_u32(skb, IFLA_LINK, ifindex);
1567 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1568 struct net_device *dev)
1573 ret = dev_get_alias(dev, buf, sizeof(buf));
1574 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1577 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1578 const struct net_device *dev,
1579 struct net *src_net, gfp_t gfp)
1581 bool put_iflink = false;
1583 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1584 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1586 if (!net_eq(dev_net(dev), link_net)) {
1587 int id = peernet2id_alloc(src_net, link_net, gfp);
1589 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1596 return nla_put_iflink(skb, dev, put_iflink);
1599 static int rtnl_fill_link_af(struct sk_buff *skb,
1600 const struct net_device *dev,
1601 u32 ext_filter_mask)
1603 const struct rtnl_af_ops *af_ops;
1604 struct nlattr *af_spec;
1606 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1610 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1614 if (!af_ops->fill_link_af)
1617 af = nla_nest_start_noflag(skb, af_ops->family);
1621 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1623 * Caller may return ENODATA to indicate that there
1624 * was no data to be dumped. This is not an error, it
1625 * means we should trim the attribute header and
1628 if (err == -ENODATA)
1629 nla_nest_cancel(skb, af);
1633 nla_nest_end(skb, af);
1636 nla_nest_end(skb, af_spec);
1640 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1641 const struct net_device *dev)
1643 struct netdev_name_node *name_node;
1646 list_for_each_entry(name_node, &dev->name_node->list, list) {
1647 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1654 static int rtnl_fill_prop_list(struct sk_buff *skb,
1655 const struct net_device *dev)
1657 struct nlattr *prop_list;
1660 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1664 ret = rtnl_fill_alt_ifnames(skb, dev);
1668 nla_nest_end(skb, prop_list);
1672 nla_nest_cancel(skb, prop_list);
1676 static int rtnl_fill_proto_down(struct sk_buff *skb,
1677 const struct net_device *dev)
1682 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1683 goto nla_put_failure;
1685 preason = dev->proto_down_reason;
1689 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1693 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1694 nla_nest_cancel(skb, pr);
1695 goto nla_put_failure;
1698 nla_nest_end(skb, pr);
1705 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1706 struct net_device *dev, struct net *src_net,
1707 int type, u32 pid, u32 seq, u32 change,
1708 unsigned int flags, u32 ext_filter_mask,
1709 u32 event, int *new_nsid, int new_ifindex,
1710 int tgt_netnsid, gfp_t gfp)
1712 struct ifinfomsg *ifm;
1713 struct nlmsghdr *nlh;
1714 struct Qdisc *qdisc;
1717 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1721 ifm = nlmsg_data(nlh);
1722 ifm->ifi_family = AF_UNSPEC;
1724 ifm->ifi_type = dev->type;
1725 ifm->ifi_index = dev->ifindex;
1726 ifm->ifi_flags = dev_get_flags(dev);
1727 ifm->ifi_change = change;
1729 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1730 goto nla_put_failure;
1732 qdisc = rtnl_dereference(dev->qdisc);
1733 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1734 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1735 nla_put_u8(skb, IFLA_OPERSTATE,
1736 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1737 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1738 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1739 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1740 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1741 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1742 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1743 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1744 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1745 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1747 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1749 put_master_ifindex(skb, dev) ||
1750 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1752 nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1753 nla_put_ifalias(skb, dev) ||
1754 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1755 atomic_read(&dev->carrier_up_count) +
1756 atomic_read(&dev->carrier_down_count)) ||
1757 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1758 atomic_read(&dev->carrier_up_count)) ||
1759 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1760 atomic_read(&dev->carrier_down_count)))
1761 goto nla_put_failure;
1763 if (rtnl_fill_proto_down(skb, dev))
1764 goto nla_put_failure;
1766 if (event != IFLA_EVENT_NONE) {
1767 if (nla_put_u32(skb, IFLA_EVENT, event))
1768 goto nla_put_failure;
1771 if (rtnl_fill_link_ifmap(skb, dev))
1772 goto nla_put_failure;
1774 if (dev->addr_len) {
1775 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1776 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1777 goto nla_put_failure;
1780 if (rtnl_phys_port_id_fill(skb, dev))
1781 goto nla_put_failure;
1783 if (rtnl_phys_port_name_fill(skb, dev))
1784 goto nla_put_failure;
1786 if (rtnl_phys_switch_id_fill(skb, dev))
1787 goto nla_put_failure;
1789 if (rtnl_fill_stats(skb, dev))
1790 goto nla_put_failure;
1792 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1793 goto nla_put_failure;
1795 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1796 goto nla_put_failure;
1798 if (rtnl_xdp_fill(skb, dev))
1799 goto nla_put_failure;
1801 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1802 if (rtnl_link_fill(skb, dev) < 0)
1803 goto nla_put_failure;
1806 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1807 goto nla_put_failure;
1810 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1811 goto nla_put_failure;
1813 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1814 goto nla_put_failure;
1816 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1817 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1818 goto nla_put_failure;
1821 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1822 goto nla_put_failure_rcu;
1825 if (rtnl_fill_prop_list(skb, dev))
1826 goto nla_put_failure;
1828 nlmsg_end(skb, nlh);
1831 nla_put_failure_rcu:
1834 nlmsg_cancel(skb, nlh);
1838 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1839 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1840 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1841 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1842 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1843 [IFLA_MTU] = { .type = NLA_U32 },
1844 [IFLA_LINK] = { .type = NLA_U32 },
1845 [IFLA_MASTER] = { .type = NLA_U32 },
1846 [IFLA_CARRIER] = { .type = NLA_U8 },
1847 [IFLA_TXQLEN] = { .type = NLA_U32 },
1848 [IFLA_WEIGHT] = { .type = NLA_U32 },
1849 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1850 [IFLA_LINKMODE] = { .type = NLA_U8 },
1851 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1852 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1853 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1854 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1855 * allow 0-length string (needed to remove an alias).
1857 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1858 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1859 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1860 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1861 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1862 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1863 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1864 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1865 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1866 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1867 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1868 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1869 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1870 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1871 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1872 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1873 [IFLA_XDP] = { .type = NLA_NESTED },
1874 [IFLA_EVENT] = { .type = NLA_U32 },
1875 [IFLA_GROUP] = { .type = NLA_U32 },
1876 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1877 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1878 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1879 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1880 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1881 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1882 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1883 .len = ALTIFNAMSIZ - 1 },
1884 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
1885 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1888 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1889 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1890 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1891 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1892 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1895 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1896 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1897 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1898 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1899 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1900 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1901 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1902 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1903 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1904 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1905 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1906 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1907 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1908 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1911 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1912 [IFLA_PORT_VF] = { .type = NLA_U32 },
1913 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1914 .len = PORT_PROFILE_MAX },
1915 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1916 .len = PORT_UUID_MAX },
1917 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1918 .len = PORT_UUID_MAX },
1919 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1920 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1922 /* Unused, but we need to keep it here since user space could
1923 * fill it. It's also broken with regard to NLA_BINARY use in
1924 * combination with structs.
1926 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1927 .len = sizeof(struct ifla_port_vsi) },
1930 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1931 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1932 [IFLA_XDP_FD] = { .type = NLA_S32 },
1933 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
1934 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1935 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1936 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1939 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1941 const struct rtnl_link_ops *ops = NULL;
1942 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1944 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1947 if (linfo[IFLA_INFO_KIND]) {
1948 char kind[MODULE_NAME_LEN];
1950 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1951 ops = rtnl_link_ops_get(kind);
1957 static bool link_master_filtered(struct net_device *dev, int master_idx)
1959 struct net_device *master;
1964 master = netdev_master_upper_dev_get(dev);
1965 if (!master || master->ifindex != master_idx)
1971 static bool link_kind_filtered(const struct net_device *dev,
1972 const struct rtnl_link_ops *kind_ops)
1974 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1980 static bool link_dump_filtered(struct net_device *dev,
1982 const struct rtnl_link_ops *kind_ops)
1984 if (link_master_filtered(dev, master_idx) ||
1985 link_kind_filtered(dev, kind_ops))
1992 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1993 * @sk: netlink socket
1994 * @netnsid: network namespace identifier
1996 * Returns the network namespace identified by netnsid on success or an error
1997 * pointer on failure.
1999 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2003 net = get_net_ns_by_id(sock_net(sk), netnsid);
2005 return ERR_PTR(-EINVAL);
2007 /* For now, the caller is required to have CAP_NET_ADMIN in
2008 * the user namespace owning the target net ns.
2010 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2012 return ERR_PTR(-EACCES);
2016 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2018 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2019 bool strict_check, struct nlattr **tb,
2020 struct netlink_ext_ack *extack)
2025 struct ifinfomsg *ifm;
2027 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2028 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2032 ifm = nlmsg_data(nlh);
2033 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2035 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2038 if (ifm->ifi_index) {
2039 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2043 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2044 IFLA_MAX, ifla_policy,
2048 /* A hack to preserve kernel<->userspace interface.
2049 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2050 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2051 * what iproute2 < v3.9.0 used.
2052 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2053 * attribute, its netlink message is shorter than struct ifinfomsg.
2055 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2056 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2058 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2062 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2064 struct netlink_ext_ack *extack = cb->extack;
2065 const struct nlmsghdr *nlh = cb->nlh;
2066 struct net *net = sock_net(skb->sk);
2067 struct net *tgt_net = net;
2070 struct net_device *dev;
2071 struct hlist_head *head;
2072 struct nlattr *tb[IFLA_MAX+1];
2073 u32 ext_filter_mask = 0;
2074 const struct rtnl_link_ops *kind_ops = NULL;
2075 unsigned int flags = NLM_F_MULTI;
2081 s_idx = cb->args[1];
2083 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2085 if (cb->strict_check)
2091 for (i = 0; i <= IFLA_MAX; ++i) {
2095 /* new attributes should only be added with strict checking */
2097 case IFLA_TARGET_NETNSID:
2098 netnsid = nla_get_s32(tb[i]);
2099 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2100 if (IS_ERR(tgt_net)) {
2101 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2102 return PTR_ERR(tgt_net);
2106 ext_filter_mask = nla_get_u32(tb[i]);
2109 master_idx = nla_get_u32(tb[i]);
2112 kind_ops = linkinfo_to_kind_ops(tb[i]);
2115 if (cb->strict_check) {
2116 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2122 if (master_idx || kind_ops)
2123 flags |= NLM_F_DUMP_FILTERED;
2126 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2128 head = &tgt_net->dev_index_head[h];
2129 hlist_for_each_entry(dev, head, index_hlist) {
2130 if (link_dump_filtered(dev, master_idx, kind_ops))
2134 err = rtnl_fill_ifinfo(skb, dev, net,
2136 NETLINK_CB(cb->skb).portid,
2137 nlh->nlmsg_seq, 0, flags,
2138 ext_filter_mask, 0, NULL, 0,
2139 netnsid, GFP_KERNEL);
2142 if (likely(skb->len))
2156 cb->seq = net->dev_base_seq;
2157 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2164 int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
2165 struct netlink_ext_ack *exterr)
2167 const struct ifinfomsg *ifmp;
2168 const struct nlattr *attrs;
2171 ifmp = nla_data(nla_peer);
2172 attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg);
2173 len = nla_len(nla_peer) - sizeof(struct ifinfomsg);
2175 if (ifmp->ifi_index < 0) {
2176 NL_SET_ERR_MSG_ATTR(exterr, nla_peer,
2177 "ifindex can't be negative");
2181 return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy,
2184 EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg);
2186 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2189 /* Examine the link attributes and figure out which
2190 * network namespace we are talking about.
2192 if (tb[IFLA_NET_NS_PID])
2193 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2194 else if (tb[IFLA_NET_NS_FD])
2195 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2197 net = get_net(src_net);
2200 EXPORT_SYMBOL(rtnl_link_get_net);
2202 /* Figure out which network namespace we are talking about by
2203 * examining the link attributes in the following order:
2205 * 1. IFLA_NET_NS_PID
2207 * 3. IFLA_TARGET_NETNSID
2209 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2210 struct nlattr *tb[])
2214 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2215 return rtnl_link_get_net(src_net, tb);
2217 if (!tb[IFLA_TARGET_NETNSID])
2218 return get_net(src_net);
2220 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2222 return ERR_PTR(-EINVAL);
2227 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2228 struct net *src_net,
2229 struct nlattr *tb[], int cap)
2233 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2237 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2239 return ERR_PTR(-EPERM);
2245 /* Verify that rtnetlink requests do not pass additional properties
2246 * potentially referring to different network namespaces.
2248 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2249 struct netlink_ext_ack *extack,
2253 if (netns_id_only) {
2254 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2257 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2261 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2264 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2267 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2273 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2277 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2280 if (tb[IFLA_ADDRESS] &&
2281 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2284 if (tb[IFLA_BROADCAST] &&
2285 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2289 if (tb[IFLA_AF_SPEC]) {
2293 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2294 const struct rtnl_af_ops *af_ops;
2297 af_ops = rtnl_af_lookup(nla_type(af));
2300 return -EAFNOSUPPORT;
2303 if (!af_ops->set_link_af) {
2308 if (af_ops->validate_link_af) {
2309 err = af_ops->validate_link_af(dev, af);
2323 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2326 const struct net_device_ops *ops = dev->netdev_ops;
2328 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2331 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2333 if (dev->type != ARPHRD_INFINIBAND)
2336 return handle_infiniband_guid(dev, ivt, guid_type);
2339 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2341 const struct net_device_ops *ops = dev->netdev_ops;
2344 if (tb[IFLA_VF_MAC]) {
2345 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2347 if (ivm->vf >= INT_MAX)
2350 if (ops->ndo_set_vf_mac)
2351 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2357 if (tb[IFLA_VF_VLAN]) {
2358 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2360 if (ivv->vf >= INT_MAX)
2363 if (ops->ndo_set_vf_vlan)
2364 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2366 htons(ETH_P_8021Q));
2371 if (tb[IFLA_VF_VLAN_LIST]) {
2372 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2373 struct nlattr *attr;
2377 if (!ops->ndo_set_vf_vlan)
2380 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2381 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2382 nla_len(attr) < sizeof(struct ifla_vf_vlan_info)) {
2385 if (len >= MAX_VLAN_LIST_LEN)
2387 ivvl[len] = nla_data(attr);
2394 if (ivvl[0]->vf >= INT_MAX)
2396 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2397 ivvl[0]->qos, ivvl[0]->vlan_proto);
2402 if (tb[IFLA_VF_TX_RATE]) {
2403 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2404 struct ifla_vf_info ivf;
2406 if (ivt->vf >= INT_MAX)
2409 if (ops->ndo_get_vf_config)
2410 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2415 if (ops->ndo_set_vf_rate)
2416 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2423 if (tb[IFLA_VF_RATE]) {
2424 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2426 if (ivt->vf >= INT_MAX)
2429 if (ops->ndo_set_vf_rate)
2430 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2437 if (tb[IFLA_VF_SPOOFCHK]) {
2438 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2440 if (ivs->vf >= INT_MAX)
2443 if (ops->ndo_set_vf_spoofchk)
2444 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2450 if (tb[IFLA_VF_LINK_STATE]) {
2451 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2453 if (ivl->vf >= INT_MAX)
2456 if (ops->ndo_set_vf_link_state)
2457 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2463 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2464 struct ifla_vf_rss_query_en *ivrssq_en;
2467 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2468 if (ivrssq_en->vf >= INT_MAX)
2470 if (ops->ndo_set_vf_rss_query_en)
2471 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2472 ivrssq_en->setting);
2477 if (tb[IFLA_VF_TRUST]) {
2478 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2480 if (ivt->vf >= INT_MAX)
2483 if (ops->ndo_set_vf_trust)
2484 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2489 if (tb[IFLA_VF_IB_NODE_GUID]) {
2490 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2492 if (ivt->vf >= INT_MAX)
2494 if (!ops->ndo_set_vf_guid)
2496 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2499 if (tb[IFLA_VF_IB_PORT_GUID]) {
2500 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2502 if (ivt->vf >= INT_MAX)
2504 if (!ops->ndo_set_vf_guid)
2507 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2513 static int do_set_master(struct net_device *dev, int ifindex,
2514 struct netlink_ext_ack *extack)
2516 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2517 const struct net_device_ops *ops;
2521 if (upper_dev->ifindex == ifindex)
2523 ops = upper_dev->netdev_ops;
2524 if (ops->ndo_del_slave) {
2525 err = ops->ndo_del_slave(upper_dev, dev);
2534 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2537 ops = upper_dev->netdev_ops;
2538 if (ops->ndo_add_slave) {
2539 err = ops->ndo_add_slave(upper_dev, dev, extack);
2549 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2550 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2551 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2554 static int do_set_proto_down(struct net_device *dev,
2555 struct nlattr *nl_proto_down,
2556 struct nlattr *nl_proto_down_reason,
2557 struct netlink_ext_ack *extack)
2559 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2560 const struct net_device_ops *ops = dev->netdev_ops;
2561 unsigned long mask = 0;
2566 if (!ops->ndo_change_proto_down) {
2567 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2571 if (nl_proto_down_reason) {
2572 err = nla_parse_nested_deprecated(pdreason,
2573 IFLA_PROTO_DOWN_REASON_MAX,
2574 nl_proto_down_reason,
2575 ifla_proto_down_reason_policy,
2580 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2581 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2585 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2587 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2588 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2590 dev_change_proto_down_reason(dev, mask, value);
2593 if (nl_proto_down) {
2594 proto_down = nla_get_u8(nl_proto_down);
2596 /* Dont turn off protodown if there are active reasons */
2597 if (!proto_down && dev->proto_down_reason) {
2598 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2601 err = dev_change_proto_down(dev,
2610 #define DO_SETLINK_MODIFIED 0x01
2611 /* notify flag means notify + modified. */
2612 #define DO_SETLINK_NOTIFY 0x03
2613 static int do_setlink(const struct sk_buff *skb,
2614 struct net_device *dev, struct ifinfomsg *ifm,
2615 struct netlink_ext_ack *extack,
2616 struct nlattr **tb, char *ifname, int status)
2618 const struct net_device_ops *ops = dev->netdev_ops;
2621 err = validate_linkmsg(dev, tb);
2625 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2626 const char *pat = ifname && ifname[0] ? ifname : NULL;
2627 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2634 err = dev_change_net_namespace(dev, net, pat);
2638 status |= DO_SETLINK_MODIFIED;
2642 struct rtnl_link_ifmap *u_map;
2645 if (!ops->ndo_set_config) {
2650 if (!netif_device_present(dev)) {
2655 u_map = nla_data(tb[IFLA_MAP]);
2656 k_map.mem_start = (unsigned long) u_map->mem_start;
2657 k_map.mem_end = (unsigned long) u_map->mem_end;
2658 k_map.base_addr = (unsigned short) u_map->base_addr;
2659 k_map.irq = (unsigned char) u_map->irq;
2660 k_map.dma = (unsigned char) u_map->dma;
2661 k_map.port = (unsigned char) u_map->port;
2663 err = ops->ndo_set_config(dev, &k_map);
2667 status |= DO_SETLINK_NOTIFY;
2670 if (tb[IFLA_ADDRESS]) {
2671 struct sockaddr *sa;
2674 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2676 sa = kmalloc(len, GFP_KERNEL);
2681 sa->sa_family = dev->type;
2682 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2684 err = dev_set_mac_address_user(dev, sa, extack);
2688 status |= DO_SETLINK_MODIFIED;
2692 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2695 status |= DO_SETLINK_MODIFIED;
2698 if (tb[IFLA_GROUP]) {
2699 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2700 status |= DO_SETLINK_NOTIFY;
2704 * Interface selected by interface index but interface
2705 * name provided implies that a name change has been
2708 if (ifm->ifi_index > 0 && ifname[0]) {
2709 err = dev_change_name(dev, ifname);
2712 status |= DO_SETLINK_MODIFIED;
2715 if (tb[IFLA_IFALIAS]) {
2716 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2717 nla_len(tb[IFLA_IFALIAS]));
2720 status |= DO_SETLINK_NOTIFY;
2723 if (tb[IFLA_BROADCAST]) {
2724 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2725 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2728 if (ifm->ifi_flags || ifm->ifi_change) {
2729 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2735 if (tb[IFLA_MASTER]) {
2736 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2739 status |= DO_SETLINK_MODIFIED;
2742 if (tb[IFLA_CARRIER]) {
2743 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2746 status |= DO_SETLINK_MODIFIED;
2749 if (tb[IFLA_TXQLEN]) {
2750 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2752 err = dev_change_tx_queue_len(dev, value);
2755 status |= DO_SETLINK_MODIFIED;
2758 if (tb[IFLA_GSO_MAX_SIZE]) {
2759 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2761 if (max_size > GSO_MAX_SIZE) {
2766 if (dev->gso_max_size ^ max_size) {
2767 netif_set_gso_max_size(dev, max_size);
2768 status |= DO_SETLINK_MODIFIED;
2772 if (tb[IFLA_GSO_MAX_SEGS]) {
2773 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2775 if (max_segs > GSO_MAX_SEGS) {
2780 if (dev->gso_max_segs ^ max_segs) {
2781 dev->gso_max_segs = max_segs;
2782 status |= DO_SETLINK_MODIFIED;
2786 if (tb[IFLA_OPERSTATE])
2787 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2789 if (tb[IFLA_LINKMODE]) {
2790 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2792 write_lock_bh(&dev_base_lock);
2793 if (dev->link_mode ^ value)
2794 status |= DO_SETLINK_NOTIFY;
2795 dev->link_mode = value;
2796 write_unlock_bh(&dev_base_lock);
2799 if (tb[IFLA_VFINFO_LIST]) {
2800 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2801 struct nlattr *attr;
2804 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2805 if (nla_type(attr) != IFLA_VF_INFO ||
2806 nla_len(attr) < NLA_HDRLEN) {
2810 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2816 err = do_setvfinfo(dev, vfinfo);
2819 status |= DO_SETLINK_NOTIFY;
2824 if (tb[IFLA_VF_PORTS]) {
2825 struct nlattr *port[IFLA_PORT_MAX+1];
2826 struct nlattr *attr;
2831 if (!ops->ndo_set_vf_port)
2834 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2835 if (nla_type(attr) != IFLA_VF_PORT ||
2836 nla_len(attr) < NLA_HDRLEN) {
2840 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2846 if (!port[IFLA_PORT_VF]) {
2850 vf = nla_get_u32(port[IFLA_PORT_VF]);
2851 err = ops->ndo_set_vf_port(dev, vf, port);
2854 status |= DO_SETLINK_NOTIFY;
2859 if (tb[IFLA_PORT_SELF]) {
2860 struct nlattr *port[IFLA_PORT_MAX+1];
2862 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2864 ifla_port_policy, NULL);
2869 if (ops->ndo_set_vf_port)
2870 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2873 status |= DO_SETLINK_NOTIFY;
2876 if (tb[IFLA_AF_SPEC]) {
2880 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2881 const struct rtnl_af_ops *af_ops;
2885 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2887 err = af_ops->set_link_af(dev, af);
2894 status |= DO_SETLINK_NOTIFY;
2899 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2900 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2901 tb[IFLA_PROTO_DOWN_REASON], extack);
2904 status |= DO_SETLINK_NOTIFY;
2908 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2911 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2913 ifla_xdp_policy, NULL);
2917 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2922 if (xdp[IFLA_XDP_FLAGS]) {
2923 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2924 if (xdp_flags & ~XDP_FLAGS_MASK) {
2928 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2934 if (xdp[IFLA_XDP_FD]) {
2935 int expected_fd = -1;
2937 if (xdp_flags & XDP_FLAGS_REPLACE) {
2938 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2943 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2946 err = dev_change_xdp_fd(dev, extack,
2947 nla_get_s32(xdp[IFLA_XDP_FD]),
2952 status |= DO_SETLINK_NOTIFY;
2957 if (status & DO_SETLINK_MODIFIED) {
2958 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2959 netdev_state_change(dev);
2962 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",
2969 static struct net_device *rtnl_dev_get(struct net *net,
2970 struct nlattr *ifname_attr,
2971 struct nlattr *altifname_attr,
2974 char buffer[ALTIFNAMSIZ];
2979 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2980 else if (altifname_attr)
2981 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2986 return __dev_get_by_name(net, ifname);
2989 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2990 struct netlink_ext_ack *extack)
2992 struct net *net = sock_net(skb->sk);
2993 struct ifinfomsg *ifm;
2994 struct net_device *dev;
2996 struct nlattr *tb[IFLA_MAX+1];
2997 char ifname[IFNAMSIZ];
2999 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3000 ifla_policy, extack);
3004 err = rtnl_ensure_unique_netns(tb, extack, false);
3008 if (tb[IFLA_IFNAME])
3009 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3014 ifm = nlmsg_data(nlh);
3015 if (ifm->ifi_index > 0)
3016 dev = __dev_get_by_index(net, ifm->ifi_index);
3017 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3018 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3027 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
3032 static int rtnl_group_dellink(const struct net *net, int group)
3034 struct net_device *dev, *aux;
3035 LIST_HEAD(list_kill);
3041 for_each_netdev(net, dev) {
3042 if (dev->group == group) {
3043 const struct rtnl_link_ops *ops;
3046 ops = dev->rtnl_link_ops;
3047 if (!ops || !ops->dellink)
3055 for_each_netdev_safe(net, dev, aux) {
3056 if (dev->group == group) {
3057 const struct rtnl_link_ops *ops;
3059 ops = dev->rtnl_link_ops;
3060 ops->dellink(dev, &list_kill);
3063 unregister_netdevice_many(&list_kill);
3068 int rtnl_delete_link(struct net_device *dev)
3070 const struct rtnl_link_ops *ops;
3071 LIST_HEAD(list_kill);
3073 ops = dev->rtnl_link_ops;
3074 if (!ops || !ops->dellink)
3077 ops->dellink(dev, &list_kill);
3078 unregister_netdevice_many(&list_kill);
3082 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3084 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3085 struct netlink_ext_ack *extack)
3087 struct net *net = sock_net(skb->sk);
3088 struct net *tgt_net = net;
3089 struct net_device *dev = NULL;
3090 struct ifinfomsg *ifm;
3091 struct nlattr *tb[IFLA_MAX+1];
3095 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3096 ifla_policy, extack);
3100 err = rtnl_ensure_unique_netns(tb, extack, true);
3104 if (tb[IFLA_TARGET_NETNSID]) {
3105 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3106 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3107 if (IS_ERR(tgt_net))
3108 return PTR_ERR(tgt_net);
3112 ifm = nlmsg_data(nlh);
3113 if (ifm->ifi_index > 0)
3114 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3115 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3116 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3117 tb[IFLA_ALT_IFNAME], NULL);
3118 else if (tb[IFLA_GROUP])
3119 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3124 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3130 err = rtnl_delete_link(dev);
3139 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3141 unsigned int old_flags;
3144 old_flags = dev->flags;
3145 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3146 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3152 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3153 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3155 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3156 __dev_notify_flags(dev, old_flags, ~0U);
3160 EXPORT_SYMBOL(rtnl_configure_link);
3162 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3163 unsigned char name_assign_type,
3164 const struct rtnl_link_ops *ops,
3165 struct nlattr *tb[],
3166 struct netlink_ext_ack *extack)
3168 struct net_device *dev;
3169 unsigned int num_tx_queues = 1;
3170 unsigned int num_rx_queues = 1;
3172 if (tb[IFLA_NUM_TX_QUEUES])
3173 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3174 else if (ops->get_num_tx_queues)
3175 num_tx_queues = ops->get_num_tx_queues();
3177 if (tb[IFLA_NUM_RX_QUEUES])
3178 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3179 else if (ops->get_num_rx_queues)
3180 num_rx_queues = ops->get_num_rx_queues();
3182 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3183 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3184 return ERR_PTR(-EINVAL);
3187 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3188 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3189 return ERR_PTR(-EINVAL);
3192 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3193 ops->setup, num_tx_queues, num_rx_queues);
3195 return ERR_PTR(-ENOMEM);
3197 dev_net_set(dev, net);
3198 dev->rtnl_link_ops = ops;
3199 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3202 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3205 err = dev_validate_mtu(dev, mtu, extack);
3208 return ERR_PTR(err);
3212 if (tb[IFLA_ADDRESS]) {
3213 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3214 nla_len(tb[IFLA_ADDRESS]));
3215 dev->addr_assign_type = NET_ADDR_SET;
3217 if (tb[IFLA_BROADCAST])
3218 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3219 nla_len(tb[IFLA_BROADCAST]));
3220 if (tb[IFLA_TXQLEN])
3221 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3222 if (tb[IFLA_OPERSTATE])
3223 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3224 if (tb[IFLA_LINKMODE])
3225 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3227 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3228 if (tb[IFLA_GSO_MAX_SIZE])
3229 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3230 if (tb[IFLA_GSO_MAX_SEGS])
3231 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3235 EXPORT_SYMBOL(rtnl_create_link);
3237 static int rtnl_group_changelink(const struct sk_buff *skb,
3238 struct net *net, int group,
3239 struct ifinfomsg *ifm,
3240 struct netlink_ext_ack *extack,
3243 struct net_device *dev, *aux;
3246 for_each_netdev_safe(net, dev, aux) {
3247 if (dev->group == group) {
3248 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3257 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3258 struct nlattr **attr, struct netlink_ext_ack *extack)
3260 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3261 unsigned char name_assign_type = NET_NAME_USER;
3262 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3263 const struct rtnl_link_ops *m_ops;
3264 struct net_device *master_dev;
3265 struct net *net = sock_net(skb->sk);
3266 const struct rtnl_link_ops *ops;
3267 struct nlattr *tb[IFLA_MAX + 1];
3268 struct net *dest_net, *link_net;
3269 struct nlattr **slave_data;
3270 char kind[MODULE_NAME_LEN];
3271 struct net_device *dev;
3272 struct ifinfomsg *ifm;
3273 char ifname[IFNAMSIZ];
3274 struct nlattr **data;
3275 bool link_specified;
3278 #ifdef CONFIG_MODULES
3281 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3282 ifla_policy, extack);
3286 err = rtnl_ensure_unique_netns(tb, extack, false);
3290 if (tb[IFLA_IFNAME])
3291 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3295 ifm = nlmsg_data(nlh);
3296 if (ifm->ifi_index > 0) {
3297 link_specified = true;
3298 dev = __dev_get_by_index(net, ifm->ifi_index);
3299 } else if (ifm->ifi_index < 0) {
3300 NL_SET_ERR_MSG(extack, "ifindex can't be negative");
3302 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3303 link_specified = true;
3304 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
3306 link_specified = false;
3313 master_dev = netdev_master_upper_dev_get(dev);
3315 m_ops = master_dev->rtnl_link_ops;
3318 err = validate_linkmsg(dev, tb);
3322 if (tb[IFLA_LINKINFO]) {
3323 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3325 ifla_info_policy, NULL);
3329 memset(linkinfo, 0, sizeof(linkinfo));
3331 if (linkinfo[IFLA_INFO_KIND]) {
3332 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3333 ops = rtnl_link_ops_get(kind);
3341 if (ops->maxtype > RTNL_MAX_TYPE)
3344 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3345 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3346 linkinfo[IFLA_INFO_DATA],
3347 ops->policy, extack);
3352 if (ops->validate) {
3353 err = ops->validate(tb, data, extack);
3361 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3364 if (m_ops->slave_maxtype &&
3365 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3366 err = nla_parse_nested_deprecated(slave_attr,
3367 m_ops->slave_maxtype,
3368 linkinfo[IFLA_INFO_SLAVE_DATA],
3369 m_ops->slave_policy,
3373 slave_data = slave_attr;
3380 if (nlh->nlmsg_flags & NLM_F_EXCL)
3382 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3385 if (linkinfo[IFLA_INFO_DATA]) {
3386 if (!ops || ops != dev->rtnl_link_ops ||
3390 err = ops->changelink(dev, tb, data, extack);
3393 status |= DO_SETLINK_NOTIFY;
3396 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3397 if (!m_ops || !m_ops->slave_changelink)
3400 err = m_ops->slave_changelink(master_dev, dev, tb,
3401 slave_data, extack);
3404 status |= DO_SETLINK_NOTIFY;
3407 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3410 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3411 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3412 * or it's for a group
3417 return rtnl_group_changelink(skb, net,
3418 nla_get_u32(tb[IFLA_GROUP]),
3423 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3427 #ifdef CONFIG_MODULES
3430 request_module("rtnl-link-%s", kind);
3432 ops = rtnl_link_ops_get(kind);
3437 NL_SET_ERR_MSG(extack, "Unknown device type");
3445 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3446 name_assign_type = NET_NAME_ENUM;
3449 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3450 if (IS_ERR(dest_net))
3451 return PTR_ERR(dest_net);
3453 if (tb[IFLA_LINK_NETNSID]) {
3454 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3456 link_net = get_net_ns_by_id(dest_net, id);
3458 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3463 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3469 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3470 name_assign_type, ops, tb, extack);
3476 dev->ifindex = ifm->ifi_index;
3479 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3481 err = register_netdevice(dev);
3487 err = rtnl_configure_link(dev, ifm);
3489 goto out_unregister;
3491 err = dev_change_net_namespace(dev, dest_net, ifname);
3493 goto out_unregister;
3495 if (tb[IFLA_MASTER]) {
3496 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3498 goto out_unregister;
3507 LIST_HEAD(list_kill);
3509 ops->dellink(dev, &list_kill);
3510 unregister_netdevice_many(&list_kill);
3512 unregister_netdevice(dev);
3517 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3518 struct netlink_ext_ack *extack)
3520 struct nlattr **attr;
3523 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3527 ret = __rtnl_newlink(skb, nlh, attr, extack);
3532 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3533 const struct nlmsghdr *nlh,
3535 struct netlink_ext_ack *extack)
3537 struct ifinfomsg *ifm;
3540 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3541 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3545 if (!netlink_strict_get_check(skb))
3546 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3547 ifla_policy, extack);
3549 ifm = nlmsg_data(nlh);
3550 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3552 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3556 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3557 ifla_policy, extack);
3561 for (i = 0; i <= IFLA_MAX; i++) {
3567 case IFLA_ALT_IFNAME:
3569 case IFLA_TARGET_NETNSID:
3572 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3580 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3581 struct netlink_ext_ack *extack)
3583 struct net *net = sock_net(skb->sk);
3584 struct net *tgt_net = net;
3585 struct ifinfomsg *ifm;
3586 struct nlattr *tb[IFLA_MAX+1];
3587 struct net_device *dev = NULL;
3588 struct sk_buff *nskb;
3591 u32 ext_filter_mask = 0;
3593 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3597 err = rtnl_ensure_unique_netns(tb, extack, true);
3601 if (tb[IFLA_TARGET_NETNSID]) {
3602 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3603 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3604 if (IS_ERR(tgt_net))
3605 return PTR_ERR(tgt_net);
3608 if (tb[IFLA_EXT_MASK])
3609 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3612 ifm = nlmsg_data(nlh);
3613 if (ifm->ifi_index > 0)
3614 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3615 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3616 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3617 tb[IFLA_ALT_IFNAME], NULL);
3626 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3630 err = rtnl_fill_ifinfo(nskb, dev, net,
3631 RTM_NEWLINK, NETLINK_CB(skb).portid,
3632 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3633 0, NULL, 0, netnsid, GFP_KERNEL);
3635 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3636 WARN_ON(err == -EMSGSIZE);
3639 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3647 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3648 bool *changed, struct netlink_ext_ack *extack)
3654 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3658 if (cmd == RTM_NEWLINKPROP) {
3659 size = rtnl_prop_list_size(dev);
3660 size += nla_total_size(ALTIFNAMSIZ);
3661 if (size >= U16_MAX) {
3662 NL_SET_ERR_MSG(extack,
3663 "effective property list too long");
3668 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3672 if (cmd == RTM_NEWLINKPROP) {
3673 err = netdev_name_node_alt_create(dev, alt_ifname);
3676 } else if (cmd == RTM_DELLINKPROP) {
3677 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3689 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3690 struct netlink_ext_ack *extack)
3692 struct net *net = sock_net(skb->sk);
3693 struct nlattr *tb[IFLA_MAX + 1];
3694 struct net_device *dev;
3695 struct ifinfomsg *ifm;
3696 bool changed = false;
3697 struct nlattr *attr;
3700 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3704 err = rtnl_ensure_unique_netns(tb, extack, true);
3708 ifm = nlmsg_data(nlh);
3709 if (ifm->ifi_index > 0)
3710 dev = __dev_get_by_index(net, ifm->ifi_index);
3711 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3712 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3713 tb[IFLA_ALT_IFNAME], NULL);
3720 if (!tb[IFLA_PROP_LIST])
3723 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3724 switch (nla_type(attr)) {
3725 case IFLA_ALT_IFNAME:
3726 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3734 netdev_state_change(dev);
3738 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3739 struct netlink_ext_ack *extack)
3741 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3744 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3745 struct netlink_ext_ack *extack)
3747 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3750 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3752 struct net *net = sock_net(skb->sk);
3753 size_t min_ifinfo_dump_size = 0;
3754 struct nlattr *tb[IFLA_MAX+1];
3755 u32 ext_filter_mask = 0;
3756 struct net_device *dev;
3759 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3760 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3761 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3763 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3764 if (tb[IFLA_EXT_MASK])
3765 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3768 if (!ext_filter_mask)
3769 return NLMSG_GOODSIZE;
3771 * traverse the list of net devices and compute the minimum
3772 * buffer size based upon the filter mask.
3775 for_each_netdev_rcu(net, dev) {
3776 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3777 if_nlmsg_size(dev, ext_filter_mask));
3781 return nlmsg_total_size(min_ifinfo_dump_size);
3784 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3787 int s_idx = cb->family;
3788 int type = cb->nlh->nlmsg_type - RTM_BASE;
3794 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3795 struct rtnl_link **tab;
3796 struct rtnl_link *link;
3797 rtnl_dumpit_func dumpit;
3799 if (idx < s_idx || idx == PF_PACKET)
3802 if (type < 0 || type >= RTM_NR_MSGTYPES)
3805 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3813 dumpit = link->dumpit;
3818 memset(&cb->args[0], 0, sizeof(cb->args));
3822 ret = dumpit(skb, cb);
3828 return skb->len ? : ret;
3831 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3832 unsigned int change,
3833 u32 event, gfp_t flags, int *new_nsid,
3836 struct net *net = dev_net(dev);
3837 struct sk_buff *skb;
3839 size_t if_info_size;
3841 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3845 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3846 type, 0, 0, change, 0, 0, event,
3847 new_nsid, new_ifindex, -1, flags);
3849 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3850 WARN_ON(err == -EMSGSIZE);
3857 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3861 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3863 struct net *net = dev_net(dev);
3865 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3868 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3869 unsigned int change, u32 event,
3870 gfp_t flags, int *new_nsid, int new_ifindex)
3872 struct sk_buff *skb;
3874 if (dev->reg_state != NETREG_REGISTERED)
3877 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3880 rtmsg_ifinfo_send(skb, dev, flags);
3883 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3886 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3890 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3891 gfp_t flags, int *new_nsid, int new_ifindex)
3893 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3894 new_nsid, new_ifindex);
3897 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3898 struct net_device *dev,
3899 u8 *addr, u16 vid, u32 pid, u32 seq,
3900 int type, unsigned int flags,
3901 int nlflags, u16 ndm_state)
3903 struct nlmsghdr *nlh;
3906 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3910 ndm = nlmsg_data(nlh);
3911 ndm->ndm_family = AF_BRIDGE;
3914 ndm->ndm_flags = flags;
3916 ndm->ndm_ifindex = dev->ifindex;
3917 ndm->ndm_state = ndm_state;
3919 if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
3920 goto nla_put_failure;
3922 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3923 goto nla_put_failure;
3925 nlmsg_end(skb, nlh);
3929 nlmsg_cancel(skb, nlh);
3933 static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
3935 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3936 nla_total_size(dev->addr_len) + /* NDA_LLADDR */
3937 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3941 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3944 struct net *net = dev_net(dev);
3945 struct sk_buff *skb;
3948 skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
3952 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3953 0, 0, type, NTF_SELF, 0, ndm_state);
3959 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3962 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3966 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3968 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3969 struct nlattr *tb[],
3970 struct net_device *dev,
3971 const unsigned char *addr, u16 vid,
3976 /* If aging addresses are supported device will need to
3977 * implement its own handler for this.
3979 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3980 pr_info("%s: FDB only supports static addresses\n", dev->name);
3985 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3989 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3990 err = dev_uc_add_excl(dev, addr);
3991 else if (is_multicast_ether_addr(addr))
3992 err = dev_mc_add_excl(dev, addr);
3994 /* Only return duplicate errors if NLM_F_EXCL is set */
3995 if (err == -EEXIST && !(flags & NLM_F_EXCL))
4000 EXPORT_SYMBOL(ndo_dflt_fdb_add);
4002 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4003 struct netlink_ext_ack *extack)
4008 if (nla_len(vlan_attr) != sizeof(u16)) {
4009 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4013 vid = nla_get_u16(vlan_attr);
4015 if (!vid || vid >= VLAN_VID_MASK) {
4016 NL_SET_ERR_MSG(extack, "invalid vlan id");
4024 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4025 struct netlink_ext_ack *extack)
4027 struct net *net = sock_net(skb->sk);
4029 struct nlattr *tb[NDA_MAX+1];
4030 struct net_device *dev;
4035 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4040 ndm = nlmsg_data(nlh);
4041 if (ndm->ndm_ifindex == 0) {
4042 NL_SET_ERR_MSG(extack, "invalid ifindex");
4046 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4048 NL_SET_ERR_MSG(extack, "unknown ifindex");
4052 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4053 NL_SET_ERR_MSG(extack, "invalid address");
4057 if (dev->type != ARPHRD_ETHER) {
4058 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4062 addr = nla_data(tb[NDA_LLADDR]);
4064 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4070 /* Support fdb on master device the net/bridge default case */
4071 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4072 netif_is_bridge_port(dev)) {
4073 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4074 const struct net_device_ops *ops = br_dev->netdev_ops;
4076 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4077 nlh->nlmsg_flags, extack);
4081 ndm->ndm_flags &= ~NTF_MASTER;
4084 /* Embedded bridge, macvlan, and any other device support */
4085 if ((ndm->ndm_flags & NTF_SELF)) {
4086 if (dev->netdev_ops->ndo_fdb_add)
4087 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4092 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4096 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4098 ndm->ndm_flags &= ~NTF_SELF;
4106 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4108 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4109 struct nlattr *tb[],
4110 struct net_device *dev,
4111 const unsigned char *addr, u16 vid)
4115 /* If aging addresses are supported device will need to
4116 * implement its own handler for this.
4118 if (!(ndm->ndm_state & NUD_PERMANENT)) {
4119 pr_info("%s: FDB only supports static addresses\n", dev->name);
4123 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4124 err = dev_uc_del(dev, addr);
4125 else if (is_multicast_ether_addr(addr))
4126 err = dev_mc_del(dev, addr);
4130 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4132 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4133 struct netlink_ext_ack *extack)
4135 struct net *net = sock_net(skb->sk);
4137 struct nlattr *tb[NDA_MAX+1];
4138 struct net_device *dev;
4143 if (!netlink_capable(skb, CAP_NET_ADMIN))
4146 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4151 ndm = nlmsg_data(nlh);
4152 if (ndm->ndm_ifindex == 0) {
4153 NL_SET_ERR_MSG(extack, "invalid ifindex");
4157 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4159 NL_SET_ERR_MSG(extack, "unknown ifindex");
4163 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4164 NL_SET_ERR_MSG(extack, "invalid address");
4168 if (dev->type != ARPHRD_ETHER) {
4169 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4173 addr = nla_data(tb[NDA_LLADDR]);
4175 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4181 /* Support fdb on master device the net/bridge default case */
4182 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4183 netif_is_bridge_port(dev)) {
4184 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4185 const struct net_device_ops *ops = br_dev->netdev_ops;
4187 if (ops->ndo_fdb_del)
4188 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4193 ndm->ndm_flags &= ~NTF_MASTER;
4196 /* Embedded bridge, macvlan, and any other device support */
4197 if (ndm->ndm_flags & NTF_SELF) {
4198 if (dev->netdev_ops->ndo_fdb_del)
4199 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4202 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4205 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4207 ndm->ndm_flags &= ~NTF_SELF;
4214 static int nlmsg_populate_fdb(struct sk_buff *skb,
4215 struct netlink_callback *cb,
4216 struct net_device *dev,
4218 struct netdev_hw_addr_list *list)
4220 struct netdev_hw_addr *ha;
4224 portid = NETLINK_CB(cb->skb).portid;
4225 seq = cb->nlh->nlmsg_seq;
4227 list_for_each_entry(ha, &list->list, list) {
4228 if (*idx < cb->args[2])
4231 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4233 RTM_NEWNEIGH, NTF_SELF,
4234 NLM_F_MULTI, NUD_PERMANENT);
4244 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4245 * @skb: socket buffer to store message in
4246 * @cb: netlink callback
4248 * @filter_dev: ignored
4249 * @idx: the number of FDB table entries dumped is added to *@idx
4251 * Default netdevice operation to dump the existing unicast address list.
4252 * Returns number of addresses from list put in skb.
4254 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4255 struct netlink_callback *cb,
4256 struct net_device *dev,
4257 struct net_device *filter_dev,
4262 if (dev->type != ARPHRD_ETHER)
4265 netif_addr_lock_bh(dev);
4266 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4269 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4271 netif_addr_unlock_bh(dev);
4274 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4276 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4277 int *br_idx, int *brport_idx,
4278 struct netlink_ext_ack *extack)
4280 struct nlattr *tb[NDA_MAX + 1];
4284 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4285 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4289 ndm = nlmsg_data(nlh);
4290 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4291 ndm->ndm_flags || ndm->ndm_type) {
4292 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4296 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4297 NDA_MAX, NULL, extack);
4301 *brport_idx = ndm->ndm_ifindex;
4302 for (i = 0; i <= NDA_MAX; ++i) {
4308 if (nla_len(tb[i]) != sizeof(u32)) {
4309 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4312 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4315 if (nla_len(tb[i]) != sizeof(u32)) {
4316 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4319 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4322 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4330 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4331 int *br_idx, int *brport_idx,
4332 struct netlink_ext_ack *extack)
4334 struct nlattr *tb[IFLA_MAX+1];
4337 /* A hack to preserve kernel<->userspace interface.
4338 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4339 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4340 * So, check for ndmsg with an optional u32 attribute (not used here).
4341 * Fortunately these sizes don't conflict with the size of ifinfomsg
4342 * with an optional attribute.
4344 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4345 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4346 nla_attr_size(sizeof(u32)))) {
4347 struct ifinfomsg *ifm;
4349 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4350 tb, IFLA_MAX, ifla_policy,
4354 } else if (err == 0) {
4355 if (tb[IFLA_MASTER])
4356 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4359 ifm = nlmsg_data(nlh);
4360 *brport_idx = ifm->ifi_index;
4365 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4367 struct net_device *dev;
4368 struct net_device *br_dev = NULL;
4369 const struct net_device_ops *ops = NULL;
4370 const struct net_device_ops *cops = NULL;
4371 struct net *net = sock_net(skb->sk);
4372 struct hlist_head *head;
4380 if (cb->strict_check)
4381 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4384 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4390 br_dev = __dev_get_by_index(net, br_idx);
4394 ops = br_dev->netdev_ops;
4398 s_idx = cb->args[1];
4400 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4402 head = &net->dev_index_head[h];
4403 hlist_for_each_entry(dev, head, index_hlist) {
4405 if (brport_idx && (dev->ifindex != brport_idx))
4408 if (!br_idx) { /* user did not specify a specific bridge */
4409 if (netif_is_bridge_port(dev)) {
4410 br_dev = netdev_master_upper_dev_get(dev);
4411 cops = br_dev->netdev_ops;
4414 if (dev != br_dev &&
4415 !netif_is_bridge_port(dev))
4418 if (br_dev != netdev_master_upper_dev_get(dev) &&
4419 !(dev->priv_flags & IFF_EBRIDGE))
4427 if (netif_is_bridge_port(dev)) {
4428 if (cops && cops->ndo_fdb_dump) {
4429 err = cops->ndo_fdb_dump(skb, cb,
4432 if (err == -EMSGSIZE)
4437 if (dev->netdev_ops->ndo_fdb_dump)
4438 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4442 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4444 if (err == -EMSGSIZE)
4449 /* reset fdb offset to 0 for rest of the interfaces */
4465 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4466 struct nlattr **tb, u8 *ndm_flags,
4467 int *br_idx, int *brport_idx, u8 **addr,
4468 u16 *vid, struct netlink_ext_ack *extack)
4473 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4474 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4478 ndm = nlmsg_data(nlh);
4479 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4481 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4485 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4486 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4490 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4491 NDA_MAX, nda_policy, extack);
4495 *ndm_flags = ndm->ndm_flags;
4496 *brport_idx = ndm->ndm_ifindex;
4497 for (i = 0; i <= NDA_MAX; ++i) {
4503 *br_idx = nla_get_u32(tb[i]);
4506 if (nla_len(tb[i]) != ETH_ALEN) {
4507 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4510 *addr = nla_data(tb[i]);
4513 err = fdb_vid_parse(tb[i], vid, extack);
4520 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4528 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4529 struct netlink_ext_ack *extack)
4531 struct net_device *dev = NULL, *br_dev = NULL;
4532 const struct net_device_ops *ops = NULL;
4533 struct net *net = sock_net(in_skb->sk);
4534 struct nlattr *tb[NDA_MAX + 1];
4535 struct sk_buff *skb;
4543 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4544 &brport_idx, &addr, &vid, extack);
4549 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4554 dev = __dev_get_by_index(net, brport_idx);
4556 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4563 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4567 br_dev = __dev_get_by_index(net, br_idx);
4569 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4572 ops = br_dev->netdev_ops;
4576 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4577 if (!netif_is_bridge_port(dev)) {
4578 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4581 br_dev = netdev_master_upper_dev_get(dev);
4583 NL_SET_ERR_MSG(extack, "Master of device not found");
4586 ops = br_dev->netdev_ops;
4588 if (!(ndm_flags & NTF_SELF)) {
4589 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4592 ops = dev->netdev_ops;
4596 if (!br_dev && !dev) {
4597 NL_SET_ERR_MSG(extack, "No device specified");
4601 if (!ops || !ops->ndo_fdb_get) {
4602 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4606 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4612 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4613 NETLINK_CB(in_skb).portid,
4614 nlh->nlmsg_seq, extack);
4618 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4624 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4625 unsigned int attrnum, unsigned int flag)
4628 return nla_put_u8(skb, attrnum, !!(flags & flag));
4632 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4633 struct net_device *dev, u16 mode,
4634 u32 flags, u32 mask, int nlflags,
4636 int (*vlan_fill)(struct sk_buff *skb,
4637 struct net_device *dev,
4640 struct nlmsghdr *nlh;
4641 struct ifinfomsg *ifm;
4642 struct nlattr *br_afspec;
4643 struct nlattr *protinfo;
4644 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4645 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4648 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4652 ifm = nlmsg_data(nlh);
4653 ifm->ifi_family = AF_BRIDGE;
4655 ifm->ifi_type = dev->type;
4656 ifm->ifi_index = dev->ifindex;
4657 ifm->ifi_flags = dev_get_flags(dev);
4658 ifm->ifi_change = 0;
4661 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4662 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4663 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4665 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4667 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4668 (dev->ifindex != dev_get_iflink(dev) &&
4669 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4670 goto nla_put_failure;
4672 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4674 goto nla_put_failure;
4676 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4677 nla_nest_cancel(skb, br_afspec);
4678 goto nla_put_failure;
4681 if (mode != BRIDGE_MODE_UNDEF) {
4682 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4683 nla_nest_cancel(skb, br_afspec);
4684 goto nla_put_failure;
4688 err = vlan_fill(skb, dev, filter_mask);
4690 nla_nest_cancel(skb, br_afspec);
4691 goto nla_put_failure;
4694 nla_nest_end(skb, br_afspec);
4696 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4698 goto nla_put_failure;
4700 if (brport_nla_put_flag(skb, flags, mask,
4701 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4702 brport_nla_put_flag(skb, flags, mask,
4703 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4704 brport_nla_put_flag(skb, flags, mask,
4705 IFLA_BRPORT_FAST_LEAVE,
4706 BR_MULTICAST_FAST_LEAVE) ||
4707 brport_nla_put_flag(skb, flags, mask,
4708 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4709 brport_nla_put_flag(skb, flags, mask,
4710 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4711 brport_nla_put_flag(skb, flags, mask,
4712 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4713 brport_nla_put_flag(skb, flags, mask,
4714 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4715 brport_nla_put_flag(skb, flags, mask,
4716 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4717 brport_nla_put_flag(skb, flags, mask,
4718 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4719 brport_nla_put_flag(skb, flags, mask,
4720 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4721 nla_nest_cancel(skb, protinfo);
4722 goto nla_put_failure;
4725 nla_nest_end(skb, protinfo);
4727 nlmsg_end(skb, nlh);
4730 nlmsg_cancel(skb, nlh);
4731 return err ? err : -EMSGSIZE;
4733 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4735 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4736 bool strict_check, u32 *filter_mask,
4737 struct netlink_ext_ack *extack)
4739 struct nlattr *tb[IFLA_MAX+1];
4743 struct ifinfomsg *ifm;
4745 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4746 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4750 ifm = nlmsg_data(nlh);
4751 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4752 ifm->ifi_change || ifm->ifi_index) {
4753 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4757 err = nlmsg_parse_deprecated_strict(nlh,
4758 sizeof(struct ifinfomsg),
4759 tb, IFLA_MAX, ifla_policy,
4762 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4763 tb, IFLA_MAX, ifla_policy,
4769 /* new attributes should only be added with strict checking */
4770 for (i = 0; i <= IFLA_MAX; ++i) {
4776 *filter_mask = nla_get_u32(tb[i]);
4780 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4789 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4791 const struct nlmsghdr *nlh = cb->nlh;
4792 struct net *net = sock_net(skb->sk);
4793 struct net_device *dev;
4795 u32 portid = NETLINK_CB(cb->skb).portid;
4796 u32 seq = nlh->nlmsg_seq;
4797 u32 filter_mask = 0;
4800 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4802 if (err < 0 && cb->strict_check)
4806 for_each_netdev_rcu(net, dev) {
4807 const struct net_device_ops *ops = dev->netdev_ops;
4808 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4810 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4811 if (idx >= cb->args[0]) {
4812 err = br_dev->netdev_ops->ndo_bridge_getlink(
4813 skb, portid, seq, dev,
4814 filter_mask, NLM_F_MULTI);
4815 if (err < 0 && err != -EOPNOTSUPP) {
4816 if (likely(skb->len))
4825 if (ops->ndo_bridge_getlink) {
4826 if (idx >= cb->args[0]) {
4827 err = ops->ndo_bridge_getlink(skb, portid,
4831 if (err < 0 && err != -EOPNOTSUPP) {
4832 if (likely(skb->len))
4849 static inline size_t bridge_nlmsg_size(void)
4851 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4852 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4853 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4854 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4855 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4856 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4857 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4858 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4859 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4860 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4861 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4864 static int rtnl_bridge_notify(struct net_device *dev)
4866 struct net *net = dev_net(dev);
4867 struct sk_buff *skb;
4868 int err = -EOPNOTSUPP;
4870 if (!dev->netdev_ops->ndo_bridge_getlink)
4873 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4879 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4883 /* Notification info is only filled for bridge ports, not the bridge
4884 * device itself. Therefore, a zero notification length is valid and
4885 * should not result in an error.
4890 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4893 WARN_ON(err == -EMSGSIZE);
4896 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4900 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4901 struct netlink_ext_ack *extack)
4903 struct net *net = sock_net(skb->sk);
4904 struct ifinfomsg *ifm;
4905 struct net_device *dev;
4906 struct nlattr *br_spec, *attr, *br_flags_attr = NULL;
4907 int rem, err = -EOPNOTSUPP;
4910 if (nlmsg_len(nlh) < sizeof(*ifm))
4913 ifm = nlmsg_data(nlh);
4914 if (ifm->ifi_family != AF_BRIDGE)
4915 return -EPFNOSUPPORT;
4917 dev = __dev_get_by_index(net, ifm->ifi_index);
4919 NL_SET_ERR_MSG(extack, "unknown ifindex");
4923 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4925 nla_for_each_nested(attr, br_spec, rem) {
4926 if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !br_flags_attr) {
4927 if (nla_len(attr) < sizeof(flags))
4930 br_flags_attr = attr;
4931 flags = nla_get_u16(attr);
4934 if (nla_type(attr) == IFLA_BRIDGE_MODE) {
4935 if (nla_len(attr) < sizeof(u16))
4941 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4942 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4944 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4949 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4954 flags &= ~BRIDGE_FLAGS_MASTER;
4957 if ((flags & BRIDGE_FLAGS_SELF)) {
4958 if (!dev->netdev_ops->ndo_bridge_setlink)
4961 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4965 flags &= ~BRIDGE_FLAGS_SELF;
4967 /* Generate event to notify upper layer of bridge
4970 err = rtnl_bridge_notify(dev);
4975 memcpy(nla_data(br_flags_attr), &flags, sizeof(flags));
4980 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4981 struct netlink_ext_ack *extack)
4983 struct net *net = sock_net(skb->sk);
4984 struct ifinfomsg *ifm;
4985 struct net_device *dev;
4986 struct nlattr *br_spec, *attr = NULL;
4987 int rem, err = -EOPNOTSUPP;
4989 bool have_flags = false;
4991 if (nlmsg_len(nlh) < sizeof(*ifm))
4994 ifm = nlmsg_data(nlh);
4995 if (ifm->ifi_family != AF_BRIDGE)
4996 return -EPFNOSUPPORT;
4998 dev = __dev_get_by_index(net, ifm->ifi_index);
5000 NL_SET_ERR_MSG(extack, "unknown ifindex");
5004 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5006 nla_for_each_nested(attr, br_spec, rem) {
5007 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5008 if (nla_len(attr) < sizeof(flags))
5012 flags = nla_get_u16(attr);
5018 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5019 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5021 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5026 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5030 flags &= ~BRIDGE_FLAGS_MASTER;
5033 if ((flags & BRIDGE_FLAGS_SELF)) {
5034 if (!dev->netdev_ops->ndo_bridge_dellink)
5037 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5041 flags &= ~BRIDGE_FLAGS_SELF;
5043 /* Generate event to notify upper layer of bridge
5046 err = rtnl_bridge_notify(dev);
5051 memcpy(nla_data(attr), &flags, sizeof(flags));
5056 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5058 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5059 (!idxattr || idxattr == attrid);
5062 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5063 static int rtnl_get_offload_stats_attr_size(int attr_id)
5066 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5067 return sizeof(struct rtnl_link_stats64);
5073 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5076 struct nlattr *attr = NULL;
5081 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5082 dev->netdev_ops->ndo_get_offload_stats))
5085 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5086 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5087 if (attr_id < *prividx)
5090 size = rtnl_get_offload_stats_attr_size(attr_id);
5094 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5097 attr = nla_reserve_64bit(skb, attr_id, size,
5098 IFLA_OFFLOAD_XSTATS_UNSPEC);
5100 goto nla_put_failure;
5102 attr_data = nla_data(attr);
5103 memset(attr_data, 0, size);
5104 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5107 goto get_offload_stats_failure;
5118 get_offload_stats_failure:
5123 static int rtnl_get_offload_stats_size(const struct net_device *dev)
5129 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5130 dev->netdev_ops->ndo_get_offload_stats))
5133 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5134 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5135 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
5137 size = rtnl_get_offload_stats_attr_size(attr_id);
5138 nla_size += nla_total_size_64bit(size);
5142 nla_size += nla_total_size(0);
5147 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5148 int type, u32 pid, u32 seq, u32 change,
5149 unsigned int flags, unsigned int filter_mask,
5150 int *idxattr, int *prividx)
5152 struct if_stats_msg *ifsm;
5153 struct nlmsghdr *nlh;
5154 struct nlattr *attr;
5155 int s_prividx = *prividx;
5160 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5164 ifsm = nlmsg_data(nlh);
5165 ifsm->family = PF_UNSPEC;
5168 ifsm->ifindex = dev->ifindex;
5169 ifsm->filter_mask = filter_mask;
5171 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5172 struct rtnl_link_stats64 *sp;
5174 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5175 sizeof(struct rtnl_link_stats64),
5178 goto nla_put_failure;
5180 sp = nla_data(attr);
5181 dev_get_stats(dev, sp);
5184 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5185 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5187 if (ops && ops->fill_linkxstats) {
5188 *idxattr = IFLA_STATS_LINK_XSTATS;
5189 attr = nla_nest_start_noflag(skb,
5190 IFLA_STATS_LINK_XSTATS);
5192 goto nla_put_failure;
5194 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5195 nla_nest_end(skb, attr);
5197 goto nla_put_failure;
5202 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5204 const struct rtnl_link_ops *ops = NULL;
5205 const struct net_device *master;
5207 master = netdev_master_upper_dev_get(dev);
5209 ops = master->rtnl_link_ops;
5210 if (ops && ops->fill_linkxstats) {
5211 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5212 attr = nla_nest_start_noflag(skb,
5213 IFLA_STATS_LINK_XSTATS_SLAVE);
5215 goto nla_put_failure;
5217 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5218 nla_nest_end(skb, attr);
5220 goto nla_put_failure;
5225 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5227 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5228 attr = nla_nest_start_noflag(skb,
5229 IFLA_STATS_LINK_OFFLOAD_XSTATS);
5231 goto nla_put_failure;
5233 err = rtnl_get_offload_stats(skb, dev, prividx);
5234 if (err == -ENODATA)
5235 nla_nest_cancel(skb, attr);
5237 nla_nest_end(skb, attr);
5239 if (err && err != -ENODATA)
5240 goto nla_put_failure;
5244 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5245 struct rtnl_af_ops *af_ops;
5247 *idxattr = IFLA_STATS_AF_SPEC;
5248 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5250 goto nla_put_failure;
5253 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5254 if (af_ops->fill_stats_af) {
5258 af = nla_nest_start_noflag(skb,
5262 goto nla_put_failure;
5264 err = af_ops->fill_stats_af(skb, dev);
5266 if (err == -ENODATA) {
5267 nla_nest_cancel(skb, af);
5268 } else if (err < 0) {
5270 goto nla_put_failure;
5273 nla_nest_end(skb, af);
5278 nla_nest_end(skb, attr);
5283 nlmsg_end(skb, nlh);
5288 /* not a multi message or no progress mean a real error */
5289 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5290 nlmsg_cancel(skb, nlh);
5292 nlmsg_end(skb, nlh);
5297 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5300 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5302 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5303 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5305 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5306 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5307 int attr = IFLA_STATS_LINK_XSTATS;
5309 if (ops && ops->get_linkxstats_size) {
5310 size += nla_total_size(ops->get_linkxstats_size(dev,
5312 /* for IFLA_STATS_LINK_XSTATS */
5313 size += nla_total_size(0);
5317 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5318 struct net_device *_dev = (struct net_device *)dev;
5319 const struct rtnl_link_ops *ops = NULL;
5320 const struct net_device *master;
5322 /* netdev_master_upper_dev_get can't take const */
5323 master = netdev_master_upper_dev_get(_dev);
5325 ops = master->rtnl_link_ops;
5326 if (ops && ops->get_linkxstats_size) {
5327 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5329 size += nla_total_size(ops->get_linkxstats_size(dev,
5331 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5332 size += nla_total_size(0);
5336 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5337 size += rtnl_get_offload_stats_size(dev);
5339 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5340 struct rtnl_af_ops *af_ops;
5342 /* for IFLA_STATS_AF_SPEC */
5343 size += nla_total_size(0);
5346 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5347 if (af_ops->get_stats_af_size) {
5348 size += nla_total_size(
5349 af_ops->get_stats_af_size(dev));
5352 size += nla_total_size(0);
5361 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5362 bool is_dump, struct netlink_ext_ack *extack)
5364 struct if_stats_msg *ifsm;
5366 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5367 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5374 ifsm = nlmsg_data(nlh);
5376 /* only requests using strict checks can pass data to influence
5377 * the dump. The legacy exception is filter_mask.
5379 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5380 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5383 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5384 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5387 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5388 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5395 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5396 struct netlink_ext_ack *extack)
5398 struct net *net = sock_net(skb->sk);
5399 struct net_device *dev = NULL;
5400 int idxattr = 0, prividx = 0;
5401 struct if_stats_msg *ifsm;
5402 struct sk_buff *nskb;
5406 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5411 ifsm = nlmsg_data(nlh);
5412 if (ifsm->ifindex > 0)
5413 dev = __dev_get_by_index(net, ifsm->ifindex);
5420 filter_mask = ifsm->filter_mask;
5424 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5428 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5429 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5430 0, filter_mask, &idxattr, &prividx);
5432 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5433 WARN_ON(err == -EMSGSIZE);
5436 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5442 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5444 struct netlink_ext_ack *extack = cb->extack;
5445 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5446 struct net *net = sock_net(skb->sk);
5447 unsigned int flags = NLM_F_MULTI;
5448 struct if_stats_msg *ifsm;
5449 struct hlist_head *head;
5450 struct net_device *dev;
5451 u32 filter_mask = 0;
5455 s_idx = cb->args[1];
5456 s_idxattr = cb->args[2];
5457 s_prividx = cb->args[3];
5459 cb->seq = net->dev_base_seq;
5461 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5465 ifsm = nlmsg_data(cb->nlh);
5466 filter_mask = ifsm->filter_mask;
5468 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5472 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5474 head = &net->dev_index_head[h];
5475 hlist_for_each_entry(dev, head, index_hlist) {
5478 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5479 NETLINK_CB(cb->skb).portid,
5480 cb->nlh->nlmsg_seq, 0,
5482 &s_idxattr, &s_prividx);
5483 /* If we ran out of room on the first message,
5486 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5492 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5498 cb->args[3] = s_prividx;
5499 cb->args[2] = s_idxattr;
5506 /* Process one rtnetlink message. */
5508 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5509 struct netlink_ext_ack *extack)
5511 struct net *net = sock_net(skb->sk);
5512 struct rtnl_link *link;
5513 struct module *owner;
5514 int err = -EOPNOTSUPP;
5515 rtnl_doit_func doit;
5521 type = nlh->nlmsg_type;
5527 /* All the messages must have at least 1 byte length */
5528 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5531 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5534 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5538 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5540 rtnl_dumpit_func dumpit;
5541 u32 min_dump_alloc = 0;
5543 link = rtnl_get_link(family, type);
5544 if (!link || !link->dumpit) {
5546 link = rtnl_get_link(family, type);
5547 if (!link || !link->dumpit)
5550 owner = link->owner;
5551 dumpit = link->dumpit;
5553 if (type == RTM_GETLINK - RTM_BASE)
5554 min_dump_alloc = rtnl_calcit(skb, nlh);
5557 /* need to do this before rcu_read_unlock() */
5558 if (!try_module_get(owner))
5559 err = -EPROTONOSUPPORT;
5565 struct netlink_dump_control c = {
5567 .min_dump_alloc = min_dump_alloc,
5570 err = netlink_dump_start(rtnl, skb, nlh, &c);
5571 /* netlink_dump_start() will keep a reference on
5572 * module if dump is still in progress.
5579 link = rtnl_get_link(family, type);
5580 if (!link || !link->doit) {
5582 link = rtnl_get_link(PF_UNSPEC, type);
5583 if (!link || !link->doit)
5587 owner = link->owner;
5588 if (!try_module_get(owner)) {
5589 err = -EPROTONOSUPPORT;
5593 flags = link->flags;
5594 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5598 err = doit(skb, nlh, extack);
5605 link = rtnl_get_link(family, type);
5606 if (link && link->doit)
5607 err = link->doit(skb, nlh, extack);
5623 static void rtnetlink_rcv(struct sk_buff *skb)
5625 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5628 static int rtnetlink_bind(struct net *net, int group)
5631 case RTNLGRP_IPV4_MROUTE_R:
5632 case RTNLGRP_IPV6_MROUTE_R:
5633 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5640 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5642 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5646 case NETDEV_CHANGEMTU:
5647 case NETDEV_CHANGEADDR:
5648 case NETDEV_CHANGENAME:
5649 case NETDEV_FEAT_CHANGE:
5650 case NETDEV_BONDING_FAILOVER:
5651 case NETDEV_POST_TYPE_CHANGE:
5652 case NETDEV_NOTIFY_PEERS:
5653 case NETDEV_CHANGEUPPER:
5654 case NETDEV_RESEND_IGMP:
5655 case NETDEV_CHANGEINFODATA:
5656 case NETDEV_CHANGELOWERSTATE:
5657 case NETDEV_CHANGE_TX_QUEUE_LEN:
5658 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5659 GFP_KERNEL, NULL, 0);
5667 static struct notifier_block rtnetlink_dev_notifier = {
5668 .notifier_call = rtnetlink_event,
5672 static int __net_init rtnetlink_net_init(struct net *net)
5675 struct netlink_kernel_cfg cfg = {
5676 .groups = RTNLGRP_MAX,
5677 .input = rtnetlink_rcv,
5678 .cb_mutex = &rtnl_mutex,
5679 .flags = NL_CFG_F_NONROOT_RECV,
5680 .bind = rtnetlink_bind,
5683 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5690 static void __net_exit rtnetlink_net_exit(struct net *net)
5692 netlink_kernel_release(net->rtnl);
5696 static struct pernet_operations rtnetlink_net_ops = {
5697 .init = rtnetlink_net_init,
5698 .exit = rtnetlink_net_exit,
5701 void __init rtnetlink_init(void)
5703 if (register_pernet_subsys(&rtnetlink_net_ops))
5704 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5706 register_netdevice_notifier(&rtnetlink_dev_notifier);
5708 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5709 rtnl_dump_ifinfo, 0);
5710 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5711 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5712 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5714 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5715 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5716 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5718 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5719 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5721 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5722 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5723 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5725 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5726 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5727 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5729 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,