1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
62 rtnl_dumpit_func dumpit;
68 static DEFINE_MUTEX(rtnl_mutex);
72 mutex_lock(&rtnl_mutex);
74 EXPORT_SYMBOL(rtnl_lock);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex);
80 EXPORT_SYMBOL(rtnl_lock_killable);
82 static struct sk_buff *defer_kfree_skb_list;
83 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
90 EXPORT_SYMBOL(rtnl_kfree_skbs);
92 void __rtnl_unlock(void)
94 struct sk_buff *head = defer_kfree_skb_list;
96 defer_kfree_skb_list = NULL;
98 mutex_unlock(&rtnl_mutex);
101 struct sk_buff *next = head->next;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex);
120 EXPORT_SYMBOL(rtnl_trylock);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex);
126 EXPORT_SYMBOL(rtnl_is_locked);
128 bool refcount_dec_and_rtnl_lock(refcount_t *r)
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
144 static inline int rtm_msgindex(int msgtype)
146 int msgindex = msgtype - RTM_BASE;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
158 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
160 struct rtnl_link **tab;
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
172 static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
183 msgindex = rtm_msgindex(msgtype);
186 tab = rtnl_msg_handlers[protocol];
188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
196 old = rtnl_dereference(tab[msgindex]);
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
207 WARN_ON(link->owner && link->owner != owner);
210 WARN_ON(doit && link->doit && link->doit != doit);
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
215 link->dumpit = dumpit;
217 link->flags |= flags;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
249 EXPORT_SYMBOL_GPL(rtnl_register_module);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol, int msgtype,
268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol, int msgtype)
289 struct rtnl_link **tab, *link;
292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293 msgindex = rtm_msgindex(msgtype);
296 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 link = tab[msgindex];
303 rcu_assign_pointer(tab[msgindex], NULL);
306 kfree_rcu(link, rcu);
310 EXPORT_SYMBOL_GPL(rtnl_unregister);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol)
321 struct rtnl_link **tab, *link;
324 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
327 tab = rtnl_msg_handlers[protocol];
332 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
333 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334 link = tab[msgindex];
338 rcu_assign_pointer(tab[msgindex], NULL);
339 kfree_rcu(link, rcu);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
349 static LIST_HEAD(link_ops);
351 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
353 const struct rtnl_link_ops *ops;
355 list_for_each_entry(ops, &link_ops, list) {
356 if (!strcmp(ops->kind, kind))
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops *ops)
374 if (rtnl_link_ops_get(ops->kind))
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops->setup && !ops->dellink)
383 ops->dellink = unregister_netdevice_queue;
385 list_add_tail(&ops->list, &link_ops);
388 EXPORT_SYMBOL_GPL(__rtnl_link_register);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops *ops)
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
406 err = __rtnl_link_register(ops);
410 EXPORT_SYMBOL_GPL(rtnl_link_register);
412 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
414 struct net_device *dev;
415 LIST_HEAD(list_kill);
417 for_each_netdev(net, dev) {
418 if (dev->rtnl_link_ops == ops)
419 ops->dellink(dev, &list_kill);
421 unregister_netdevice_many(&list_kill);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
437 __rtnl_kill_links(net, ops);
439 list_del(&ops->list);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
450 DEFINE_WAIT_FUNC(wait, woken_wake_function);
452 add_wait_queue(&netdev_unregistering_wq, &wait);
454 unregistering = false;
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
460 if (net->dev_unreg_count > 0) {
461 unregistering = true;
469 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
471 remove_wait_queue(&netdev_unregistering_wq, &wait);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops *ops)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops);
485 up_write(&pernet_ops_rwsem);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
491 struct net_device *master_dev;
492 const struct rtnl_link_ops *ops;
497 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
501 ops = master_dev->rtnl_link_ops;
502 if (!ops || !ops->get_slave_size)
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size = nla_total_size(sizeof(struct nlattr)) +
506 ops->get_slave_size(master_dev, dev);
513 static size_t rtnl_link_get_size(const struct net_device *dev)
515 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
525 /* IFLA_INFO_DATA + nested data */
526 size += nla_total_size(sizeof(struct nlattr)) +
529 if (ops->get_xstats_size)
530 /* IFLA_INFO_XSTATS */
531 size += nla_total_size(ops->get_xstats_size(dev));
533 size += rtnl_link_get_slave_info_data_size(dev);
538 static LIST_HEAD(rtnl_af_ops);
540 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
542 const struct rtnl_af_ops *ops;
544 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
545 if (ops->family == family)
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops *ops)
561 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
564 EXPORT_SYMBOL_GPL(rtnl_af_register);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops *ops)
573 list_del_rcu(&ops->list);
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
580 static size_t rtnl_link_get_af_size(const struct net_device *dev,
583 struct rtnl_af_ops *af_ops;
587 size = nla_total_size(sizeof(struct nlattr));
590 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
591 if (af_ops->get_link_af_size) {
592 /* AF_* + nested data */
593 size += nla_total_size(sizeof(struct nlattr)) +
594 af_ops->get_link_af_size(dev, ext_filter_mask);
602 static bool rtnl_have_link_slave_info(const struct net_device *dev)
604 struct net_device *master_dev;
609 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
610 if (master_dev && master_dev->rtnl_link_ops)
616 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617 const struct net_device *dev)
619 struct net_device *master_dev;
620 const struct rtnl_link_ops *ops;
621 struct nlattr *slave_data;
624 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
627 ops = master_dev->rtnl_link_ops;
630 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
632 if (ops->fill_slave_info) {
633 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
636 err = ops->fill_slave_info(skb, master_dev, dev);
638 goto err_cancel_slave_data;
639 nla_nest_end(skb, slave_data);
643 err_cancel_slave_data:
644 nla_nest_cancel(skb, slave_data);
648 static int rtnl_link_info_fill(struct sk_buff *skb,
649 const struct net_device *dev)
651 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
657 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
659 if (ops->fill_xstats) {
660 err = ops->fill_xstats(skb, dev);
664 if (ops->fill_info) {
665 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
668 err = ops->fill_info(skb, dev);
670 goto err_cancel_data;
671 nla_nest_end(skb, data);
676 nla_nest_cancel(skb, data);
680 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
682 struct nlattr *linkinfo;
685 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
686 if (linkinfo == NULL)
689 err = rtnl_link_info_fill(skb, dev);
691 goto err_cancel_link;
693 err = rtnl_link_slave_info_fill(skb, dev);
695 goto err_cancel_link;
697 nla_nest_end(skb, linkinfo);
701 nla_nest_cancel(skb, linkinfo);
706 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
708 struct sock *rtnl = net->rtnl;
711 NETLINK_CB(skb).dst_group = group;
713 refcount_inc(&skb->users);
714 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
716 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
720 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
722 struct sock *rtnl = net->rtnl;
724 return nlmsg_unicast(rtnl, skb, pid);
726 EXPORT_SYMBOL(rtnl_unicast);
728 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
729 struct nlmsghdr *nlh, gfp_t flags)
731 struct sock *rtnl = net->rtnl;
735 report = nlmsg_report(nlh);
737 nlmsg_notify(rtnl, skb, pid, group, report, flags);
739 EXPORT_SYMBOL(rtnl_notify);
741 void rtnl_set_sk_err(struct net *net, u32 group, int error)
743 struct sock *rtnl = net->rtnl;
745 netlink_set_err(rtnl, 0, group, error);
747 EXPORT_SYMBOL(rtnl_set_sk_err);
749 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
754 mx = nla_nest_start_noflag(skb, RTA_METRICS);
758 for (i = 0; i < RTAX_MAX; i++) {
760 if (i == RTAX_CC_ALGO - 1) {
761 char tmp[TCP_CA_NAME_MAX], *name;
763 name = tcp_ca_get_name_by_key(metrics[i], tmp);
766 if (nla_put_string(skb, i + 1, name))
767 goto nla_put_failure;
768 } else if (i == RTAX_FEATURES - 1) {
769 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
773 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
774 if (nla_put_u32(skb, i + 1, user_features))
775 goto nla_put_failure;
777 if (nla_put_u32(skb, i + 1, metrics[i]))
778 goto nla_put_failure;
785 nla_nest_cancel(skb, mx);
789 return nla_nest_end(skb, mx);
792 nla_nest_cancel(skb, mx);
795 EXPORT_SYMBOL(rtnetlink_put_metrics);
797 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
798 long expires, u32 error)
800 struct rta_cacheinfo ci = {
806 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
807 ci.rta_used = dst->__use;
808 ci.rta_clntref = atomic_read(&dst->__refcnt);
813 clock = jiffies_to_clock_t(abs(expires));
814 clock = min_t(unsigned long, clock, INT_MAX);
815 ci.rta_expires = (expires > 0) ? clock : -clock;
817 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
819 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
821 static void set_operstate(struct net_device *dev, unsigned char transition)
823 unsigned char operstate = dev->operstate;
825 switch (transition) {
827 if ((operstate == IF_OPER_DORMANT ||
828 operstate == IF_OPER_UNKNOWN) &&
830 operstate = IF_OPER_UP;
833 case IF_OPER_DORMANT:
834 if (operstate == IF_OPER_UP ||
835 operstate == IF_OPER_UNKNOWN)
836 operstate = IF_OPER_DORMANT;
840 if (dev->operstate != operstate) {
841 write_lock_bh(&dev_base_lock);
842 dev->operstate = operstate;
843 write_unlock_bh(&dev_base_lock);
844 netdev_state_change(dev);
848 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
850 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
851 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
854 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
855 const struct ifinfomsg *ifm)
857 unsigned int flags = ifm->ifi_flags;
859 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
861 flags = (flags & ifm->ifi_change) |
862 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
867 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
868 const struct rtnl_link_stats64 *b)
870 a->rx_packets = b->rx_packets;
871 a->tx_packets = b->tx_packets;
872 a->rx_bytes = b->rx_bytes;
873 a->tx_bytes = b->tx_bytes;
874 a->rx_errors = b->rx_errors;
875 a->tx_errors = b->tx_errors;
876 a->rx_dropped = b->rx_dropped;
877 a->tx_dropped = b->tx_dropped;
879 a->multicast = b->multicast;
880 a->collisions = b->collisions;
882 a->rx_length_errors = b->rx_length_errors;
883 a->rx_over_errors = b->rx_over_errors;
884 a->rx_crc_errors = b->rx_crc_errors;
885 a->rx_frame_errors = b->rx_frame_errors;
886 a->rx_fifo_errors = b->rx_fifo_errors;
887 a->rx_missed_errors = b->rx_missed_errors;
889 a->tx_aborted_errors = b->tx_aborted_errors;
890 a->tx_carrier_errors = b->tx_carrier_errors;
891 a->tx_fifo_errors = b->tx_fifo_errors;
892 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
893 a->tx_window_errors = b->tx_window_errors;
895 a->rx_compressed = b->rx_compressed;
896 a->tx_compressed = b->tx_compressed;
898 a->rx_nohandler = b->rx_nohandler;
902 static inline int rtnl_vfinfo_size(const struct net_device *dev,
905 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
906 int num_vfs = dev_num_vf(dev->dev.parent);
907 size_t size = nla_total_size(0);
910 nla_total_size(sizeof(struct ifla_vf_mac)) +
911 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
912 nla_total_size(sizeof(struct ifla_vf_vlan)) +
913 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
914 nla_total_size(MAX_VLAN_LIST_LEN *
915 sizeof(struct ifla_vf_vlan_info)) +
916 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
917 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
918 nla_total_size(sizeof(struct ifla_vf_rate)) +
919 nla_total_size(sizeof(struct ifla_vf_link_state)) +
920 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
921 nla_total_size(0) + /* nest IFLA_VF_STATS */
922 /* IFLA_VF_STATS_RX_PACKETS */
923 nla_total_size_64bit(sizeof(__u64)) +
924 /* IFLA_VF_STATS_TX_PACKETS */
925 nla_total_size_64bit(sizeof(__u64)) +
926 /* IFLA_VF_STATS_RX_BYTES */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_TX_BYTES */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_BROADCAST */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_MULTICAST */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_RX_DROPPED */
935 nla_total_size_64bit(sizeof(__u64)) +
936 /* IFLA_VF_STATS_TX_DROPPED */
937 nla_total_size_64bit(sizeof(__u64)) +
938 nla_total_size(sizeof(struct ifla_vf_trust)));
944 static size_t rtnl_port_size(const struct net_device *dev,
947 size_t port_size = nla_total_size(4) /* PORT_VF */
948 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
949 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
950 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
951 + nla_total_size(1) /* PROT_VDP_REQUEST */
952 + nla_total_size(2); /* PORT_VDP_RESPONSE */
953 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
954 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
956 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
959 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
960 !(ext_filter_mask & RTEXT_FILTER_VF))
962 if (dev_num_vf(dev->dev.parent))
963 return port_self_size + vf_ports_size +
964 vf_port_size * dev_num_vf(dev->dev.parent);
966 return port_self_size;
969 static size_t rtnl_xdp_size(void)
971 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
972 nla_total_size(1) + /* XDP_ATTACHED */
973 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
974 nla_total_size(4); /* XDP_<mode>_PROG_ID */
979 static noinline size_t if_nlmsg_size(const struct net_device *dev,
982 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
983 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
984 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
985 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
986 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
987 + nla_total_size(sizeof(struct rtnl_link_stats))
988 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
989 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
990 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
991 + nla_total_size(4) /* IFLA_TXQLEN */
992 + nla_total_size(4) /* IFLA_WEIGHT */
993 + nla_total_size(4) /* IFLA_MTU */
994 + nla_total_size(4) /* IFLA_LINK */
995 + nla_total_size(4) /* IFLA_MASTER */
996 + nla_total_size(1) /* IFLA_CARRIER */
997 + nla_total_size(4) /* IFLA_PROMISCUITY */
998 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
999 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1000 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1001 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1002 + nla_total_size(1) /* IFLA_OPERSTATE */
1003 + nla_total_size(1) /* IFLA_LINKMODE */
1004 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1005 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1006 + nla_total_size(4) /* IFLA_GROUP */
1007 + nla_total_size(ext_filter_mask
1008 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1009 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1010 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1011 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1012 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1013 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1014 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1015 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1016 + rtnl_xdp_size() /* IFLA_XDP */
1017 + nla_total_size(4) /* IFLA_EVENT */
1018 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1019 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1020 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1021 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1022 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1023 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1024 + nla_total_size(4) /* IFLA_MIN_MTU */
1025 + nla_total_size(4) /* IFLA_MAX_MTU */
1029 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1031 struct nlattr *vf_ports;
1032 struct nlattr *vf_port;
1036 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1040 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1041 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1043 goto nla_put_failure;
1044 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1045 goto nla_put_failure;
1046 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1047 if (err == -EMSGSIZE)
1048 goto nla_put_failure;
1050 nla_nest_cancel(skb, vf_port);
1053 nla_nest_end(skb, vf_port);
1056 nla_nest_end(skb, vf_ports);
1061 nla_nest_cancel(skb, vf_ports);
1065 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1067 struct nlattr *port_self;
1070 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1074 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1076 nla_nest_cancel(skb, port_self);
1077 return (err == -EMSGSIZE) ? err : 0;
1080 nla_nest_end(skb, port_self);
1085 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1086 u32 ext_filter_mask)
1090 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1091 !(ext_filter_mask & RTEXT_FILTER_VF))
1094 err = rtnl_port_self_fill(skb, dev);
1098 if (dev_num_vf(dev->dev.parent)) {
1099 err = rtnl_vf_ports_fill(skb, dev);
1107 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1110 struct netdev_phys_item_id ppid;
1112 err = dev_get_phys_port_id(dev, &ppid);
1114 if (err == -EOPNOTSUPP)
1119 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1125 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1127 char name[IFNAMSIZ];
1130 err = dev_get_phys_port_name(dev, name, sizeof(name));
1132 if (err == -EOPNOTSUPP)
1137 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1143 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1145 struct netdev_phys_item_id ppid = { };
1148 err = dev_get_port_parent_id(dev, &ppid, false);
1150 if (err == -EOPNOTSUPP)
1155 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1161 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1162 struct net_device *dev)
1164 struct rtnl_link_stats64 *sp;
1165 struct nlattr *attr;
1167 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1168 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1172 sp = nla_data(attr);
1173 dev_get_stats(dev, sp);
1175 attr = nla_reserve(skb, IFLA_STATS,
1176 sizeof(struct rtnl_link_stats));
1180 copy_rtnl_link_stats(nla_data(attr), sp);
1185 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1186 struct net_device *dev,
1188 struct nlattr *vfinfo)
1190 struct ifla_vf_rss_query_en vf_rss_query_en;
1191 struct nlattr *vf, *vfstats, *vfvlanlist;
1192 struct ifla_vf_link_state vf_linkstate;
1193 struct ifla_vf_vlan_info vf_vlan_info;
1194 struct ifla_vf_spoofchk vf_spoofchk;
1195 struct ifla_vf_tx_rate vf_tx_rate;
1196 struct ifla_vf_stats vf_stats;
1197 struct ifla_vf_trust vf_trust;
1198 struct ifla_vf_vlan vf_vlan;
1199 struct ifla_vf_rate vf_rate;
1200 struct ifla_vf_mac vf_mac;
1201 struct ifla_vf_broadcast vf_broadcast;
1202 struct ifla_vf_info ivi;
1204 memset(&ivi, 0, sizeof(ivi));
1206 /* Not all SR-IOV capable drivers support the
1207 * spoofcheck and "RSS query enable" query. Preset to
1208 * -1 so the user space tool can detect that the driver
1209 * didn't report anything.
1212 ivi.rss_query_en = -1;
1214 /* The default value for VF link state is "auto"
1215 * IFLA_VF_LINK_STATE_AUTO which equals zero
1218 /* VLAN Protocol by default is 802.1Q */
1219 ivi.vlan_proto = htons(ETH_P_8021Q);
1220 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1223 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1232 vf_rss_query_en.vf =
1233 vf_trust.vf = ivi.vf;
1235 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1236 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1237 vf_vlan.vlan = ivi.vlan;
1238 vf_vlan.qos = ivi.qos;
1239 vf_vlan_info.vlan = ivi.vlan;
1240 vf_vlan_info.qos = ivi.qos;
1241 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1242 vf_tx_rate.rate = ivi.max_tx_rate;
1243 vf_rate.min_tx_rate = ivi.min_tx_rate;
1244 vf_rate.max_tx_rate = ivi.max_tx_rate;
1245 vf_spoofchk.setting = ivi.spoofchk;
1246 vf_linkstate.link_state = ivi.linkstate;
1247 vf_rss_query_en.setting = ivi.rss_query_en;
1248 vf_trust.setting = ivi.trusted;
1249 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1251 goto nla_put_vfinfo_failure;
1252 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1253 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1254 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1255 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1257 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1259 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1261 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1263 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1264 sizeof(vf_rss_query_en),
1265 &vf_rss_query_en) ||
1266 nla_put(skb, IFLA_VF_TRUST,
1267 sizeof(vf_trust), &vf_trust))
1268 goto nla_put_vf_failure;
1269 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1271 goto nla_put_vf_failure;
1272 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1274 nla_nest_cancel(skb, vfvlanlist);
1275 goto nla_put_vf_failure;
1277 nla_nest_end(skb, vfvlanlist);
1278 memset(&vf_stats, 0, sizeof(vf_stats));
1279 if (dev->netdev_ops->ndo_get_vf_stats)
1280 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1282 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1284 goto nla_put_vf_failure;
1285 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1286 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1288 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1290 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1292 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1294 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1296 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1298 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1300 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1301 nla_nest_cancel(skb, vfstats);
1302 goto nla_put_vf_failure;
1304 nla_nest_end(skb, vfstats);
1305 nla_nest_end(skb, vf);
1309 nla_nest_cancel(skb, vf);
1310 nla_put_vfinfo_failure:
1311 nla_nest_cancel(skb, vfinfo);
1315 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1316 struct net_device *dev,
1317 u32 ext_filter_mask)
1319 struct nlattr *vfinfo;
1322 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1325 num_vfs = dev_num_vf(dev->dev.parent);
1326 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1329 if (!dev->netdev_ops->ndo_get_vf_config)
1332 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1336 for (i = 0; i < num_vfs; i++) {
1337 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1341 nla_nest_end(skb, vfinfo);
1345 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1347 struct rtnl_link_ifmap map;
1349 memset(&map, 0, sizeof(map));
1350 map.mem_start = dev->mem_start;
1351 map.mem_end = dev->mem_end;
1352 map.base_addr = dev->base_addr;
1355 map.port = dev->if_port;
1357 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1363 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1365 const struct bpf_prog *generic_xdp_prog;
1369 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1370 if (!generic_xdp_prog)
1372 return generic_xdp_prog->aux->id;
1375 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1377 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1380 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1382 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1386 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1387 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1388 u32 (*get_prog_id)(struct net_device *dev))
1393 curr_id = get_prog_id(dev);
1398 err = nla_put_u32(skb, attr, curr_id);
1402 if (*mode != XDP_ATTACHED_NONE)
1403 *mode = XDP_ATTACHED_MULTI;
1410 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1417 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1422 mode = XDP_ATTACHED_NONE;
1423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1424 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1428 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1431 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1432 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1436 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1440 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1441 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1446 nla_nest_end(skb, xdp);
1450 nla_nest_cancel(skb, xdp);
1454 static u32 rtnl_get_event(unsigned long event)
1456 u32 rtnl_event_type = IFLA_EVENT_NONE;
1460 rtnl_event_type = IFLA_EVENT_REBOOT;
1462 case NETDEV_FEAT_CHANGE:
1463 rtnl_event_type = IFLA_EVENT_FEATURES;
1465 case NETDEV_BONDING_FAILOVER:
1466 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1468 case NETDEV_NOTIFY_PEERS:
1469 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1471 case NETDEV_RESEND_IGMP:
1472 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1474 case NETDEV_CHANGEINFODATA:
1475 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1481 return rtnl_event_type;
1484 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1486 const struct net_device *upper_dev;
1491 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1493 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1499 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1502 int ifindex = dev_get_iflink(dev);
1504 if (force || dev->ifindex != ifindex)
1505 return nla_put_u32(skb, IFLA_LINK, ifindex);
1510 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1511 struct net_device *dev)
1516 ret = dev_get_alias(dev, buf, sizeof(buf));
1517 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1520 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1521 const struct net_device *dev,
1522 struct net *src_net)
1524 bool put_iflink = false;
1526 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1527 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1529 if (!net_eq(dev_net(dev), link_net)) {
1530 int id = peernet2id_alloc(src_net, link_net);
1532 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1539 return nla_put_iflink(skb, dev, put_iflink);
1542 static int rtnl_fill_link_af(struct sk_buff *skb,
1543 const struct net_device *dev,
1544 u32 ext_filter_mask)
1546 const struct rtnl_af_ops *af_ops;
1547 struct nlattr *af_spec;
1549 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1553 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1557 if (!af_ops->fill_link_af)
1560 af = nla_nest_start_noflag(skb, af_ops->family);
1564 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1566 * Caller may return ENODATA to indicate that there
1567 * was no data to be dumped. This is not an error, it
1568 * means we should trim the attribute header and
1571 if (err == -ENODATA)
1572 nla_nest_cancel(skb, af);
1576 nla_nest_end(skb, af);
1579 nla_nest_end(skb, af_spec);
1583 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1584 struct net_device *dev, struct net *src_net,
1585 int type, u32 pid, u32 seq, u32 change,
1586 unsigned int flags, u32 ext_filter_mask,
1587 u32 event, int *new_nsid, int new_ifindex,
1590 struct ifinfomsg *ifm;
1591 struct nlmsghdr *nlh;
1594 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1598 ifm = nlmsg_data(nlh);
1599 ifm->ifi_family = AF_UNSPEC;
1601 ifm->ifi_type = dev->type;
1602 ifm->ifi_index = dev->ifindex;
1603 ifm->ifi_flags = dev_get_flags(dev);
1604 ifm->ifi_change = change;
1606 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1607 goto nla_put_failure;
1609 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1610 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1611 nla_put_u8(skb, IFLA_OPERSTATE,
1612 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1613 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1614 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1615 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1616 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1617 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1618 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1619 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1620 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1621 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1623 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1625 put_master_ifindex(skb, dev) ||
1626 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1628 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1629 nla_put_ifalias(skb, dev) ||
1630 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1631 atomic_read(&dev->carrier_up_count) +
1632 atomic_read(&dev->carrier_down_count)) ||
1633 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1634 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1635 atomic_read(&dev->carrier_up_count)) ||
1636 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1637 atomic_read(&dev->carrier_down_count)))
1638 goto nla_put_failure;
1640 if (event != IFLA_EVENT_NONE) {
1641 if (nla_put_u32(skb, IFLA_EVENT, event))
1642 goto nla_put_failure;
1645 if (rtnl_fill_link_ifmap(skb, dev))
1646 goto nla_put_failure;
1648 if (dev->addr_len) {
1649 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1650 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1651 goto nla_put_failure;
1654 if (rtnl_phys_port_id_fill(skb, dev))
1655 goto nla_put_failure;
1657 if (rtnl_phys_port_name_fill(skb, dev))
1658 goto nla_put_failure;
1660 if (rtnl_phys_switch_id_fill(skb, dev))
1661 goto nla_put_failure;
1663 if (rtnl_fill_stats(skb, dev))
1664 goto nla_put_failure;
1666 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1667 goto nla_put_failure;
1669 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1670 goto nla_put_failure;
1672 if (rtnl_xdp_fill(skb, dev))
1673 goto nla_put_failure;
1675 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1676 if (rtnl_link_fill(skb, dev) < 0)
1677 goto nla_put_failure;
1680 if (rtnl_fill_link_netnsid(skb, dev, src_net))
1681 goto nla_put_failure;
1684 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1685 goto nla_put_failure;
1687 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1688 goto nla_put_failure;
1692 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1693 goto nla_put_failure_rcu;
1696 nlmsg_end(skb, nlh);
1699 nla_put_failure_rcu:
1702 nlmsg_cancel(skb, nlh);
1706 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1707 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1708 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1709 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1710 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1711 [IFLA_MTU] = { .type = NLA_U32 },
1712 [IFLA_LINK] = { .type = NLA_U32 },
1713 [IFLA_MASTER] = { .type = NLA_U32 },
1714 [IFLA_CARRIER] = { .type = NLA_U8 },
1715 [IFLA_TXQLEN] = { .type = NLA_U32 },
1716 [IFLA_WEIGHT] = { .type = NLA_U32 },
1717 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1718 [IFLA_LINKMODE] = { .type = NLA_U8 },
1719 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1720 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1721 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1722 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1723 * allow 0-length string (needed to remove an alias).
1725 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1726 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1727 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1728 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1729 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1730 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1731 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1732 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1733 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1734 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1735 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1736 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1737 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1738 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1739 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1740 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1741 [IFLA_XDP] = { .type = NLA_NESTED },
1742 [IFLA_EVENT] = { .type = NLA_U32 },
1743 [IFLA_GROUP] = { .type = NLA_U32 },
1744 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1745 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1746 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1747 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1748 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1751 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1752 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1753 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1754 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1758 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1759 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1760 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
1761 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1762 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1763 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1764 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1765 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1766 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1767 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1768 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1769 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1770 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1771 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1774 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1775 [IFLA_PORT_VF] = { .type = NLA_U32 },
1776 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1777 .len = PORT_PROFILE_MAX },
1778 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1779 .len = PORT_UUID_MAX },
1780 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1781 .len = PORT_UUID_MAX },
1782 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1783 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1785 /* Unused, but we need to keep it here since user space could
1786 * fill it. It's also broken with regard to NLA_BINARY use in
1787 * combination with structs.
1789 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1790 .len = sizeof(struct ifla_port_vsi) },
1793 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1794 [IFLA_XDP_FD] = { .type = NLA_S32 },
1795 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1796 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1797 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1800 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1802 const struct rtnl_link_ops *ops = NULL;
1803 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1805 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1808 if (linfo[IFLA_INFO_KIND]) {
1809 char kind[MODULE_NAME_LEN];
1811 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1812 ops = rtnl_link_ops_get(kind);
1818 static bool link_master_filtered(struct net_device *dev, int master_idx)
1820 struct net_device *master;
1825 master = netdev_master_upper_dev_get(dev);
1826 if (!master || master->ifindex != master_idx)
1832 static bool link_kind_filtered(const struct net_device *dev,
1833 const struct rtnl_link_ops *kind_ops)
1835 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1841 static bool link_dump_filtered(struct net_device *dev,
1843 const struct rtnl_link_ops *kind_ops)
1845 if (link_master_filtered(dev, master_idx) ||
1846 link_kind_filtered(dev, kind_ops))
1853 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1854 * @sk: netlink socket
1855 * @netnsid: network namespace identifier
1857 * Returns the network namespace identified by netnsid on success or an error
1858 * pointer on failure.
1860 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1864 net = get_net_ns_by_id(sock_net(sk), netnsid);
1866 return ERR_PTR(-EINVAL);
1868 /* For now, the caller is required to have CAP_NET_ADMIN in
1869 * the user namespace owning the target net ns.
1871 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1873 return ERR_PTR(-EACCES);
1877 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1879 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1880 bool strict_check, struct nlattr **tb,
1881 struct netlink_ext_ack *extack)
1886 struct ifinfomsg *ifm;
1888 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1889 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1893 ifm = nlmsg_data(nlh);
1894 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1896 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1899 if (ifm->ifi_index) {
1900 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1904 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1905 IFLA_MAX, ifla_policy,
1909 /* A hack to preserve kernel<->userspace interface.
1910 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1911 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1912 * what iproute2 < v3.9.0 used.
1913 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1914 * attribute, its netlink message is shorter than struct ifinfomsg.
1916 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1917 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1919 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
1923 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1925 struct netlink_ext_ack *extack = cb->extack;
1926 const struct nlmsghdr *nlh = cb->nlh;
1927 struct net *net = sock_net(skb->sk);
1928 struct net *tgt_net = net;
1931 struct net_device *dev;
1932 struct hlist_head *head;
1933 struct nlattr *tb[IFLA_MAX+1];
1934 u32 ext_filter_mask = 0;
1935 const struct rtnl_link_ops *kind_ops = NULL;
1936 unsigned int flags = NLM_F_MULTI;
1942 s_idx = cb->args[1];
1944 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1946 if (cb->strict_check)
1952 for (i = 0; i <= IFLA_MAX; ++i) {
1956 /* new attributes should only be added with strict checking */
1958 case IFLA_TARGET_NETNSID:
1959 netnsid = nla_get_s32(tb[i]);
1960 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1961 if (IS_ERR(tgt_net)) {
1962 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1963 return PTR_ERR(tgt_net);
1967 ext_filter_mask = nla_get_u32(tb[i]);
1970 master_idx = nla_get_u32(tb[i]);
1973 kind_ops = linkinfo_to_kind_ops(tb[i]);
1976 if (cb->strict_check) {
1977 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1983 if (master_idx || kind_ops)
1984 flags |= NLM_F_DUMP_FILTERED;
1987 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1989 head = &tgt_net->dev_index_head[h];
1990 hlist_for_each_entry(dev, head, index_hlist) {
1991 if (link_dump_filtered(dev, master_idx, kind_ops))
1995 err = rtnl_fill_ifinfo(skb, dev, net,
1997 NETLINK_CB(cb->skb).portid,
1998 nlh->nlmsg_seq, 0, flags,
1999 ext_filter_mask, 0, NULL, 0,
2003 if (likely(skb->len))
2017 cb->seq = net->dev_base_seq;
2018 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2025 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2026 struct netlink_ext_ack *exterr)
2028 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2031 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2033 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2036 /* Examine the link attributes and figure out which
2037 * network namespace we are talking about.
2039 if (tb[IFLA_NET_NS_PID])
2040 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2041 else if (tb[IFLA_NET_NS_FD])
2042 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2044 net = get_net(src_net);
2047 EXPORT_SYMBOL(rtnl_link_get_net);
2049 /* Figure out which network namespace we are talking about by
2050 * examining the link attributes in the following order:
2052 * 1. IFLA_NET_NS_PID
2054 * 3. IFLA_TARGET_NETNSID
2056 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2057 struct nlattr *tb[])
2061 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2062 return rtnl_link_get_net(src_net, tb);
2064 if (!tb[IFLA_TARGET_NETNSID])
2065 return get_net(src_net);
2067 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2069 return ERR_PTR(-EINVAL);
2074 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2075 struct net *src_net,
2076 struct nlattr *tb[], int cap)
2080 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2084 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2086 return ERR_PTR(-EPERM);
2092 /* Verify that rtnetlink requests do not pass additional properties
2093 * potentially referring to different network namespaces.
2095 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2096 struct netlink_ext_ack *extack,
2100 if (netns_id_only) {
2101 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2104 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2108 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2111 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2114 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2120 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2124 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2127 if (tb[IFLA_ADDRESS] &&
2128 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2131 if (tb[IFLA_BROADCAST] &&
2132 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2136 if (tb[IFLA_AF_SPEC]) {
2140 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2141 const struct rtnl_af_ops *af_ops;
2144 af_ops = rtnl_af_lookup(nla_type(af));
2147 return -EAFNOSUPPORT;
2150 if (!af_ops->set_link_af) {
2155 if (af_ops->validate_link_af) {
2156 err = af_ops->validate_link_af(dev, af);
2170 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2173 const struct net_device_ops *ops = dev->netdev_ops;
2175 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2178 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2180 if (dev->type != ARPHRD_INFINIBAND)
2183 return handle_infiniband_guid(dev, ivt, guid_type);
2186 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2188 const struct net_device_ops *ops = dev->netdev_ops;
2191 if (tb[IFLA_VF_MAC]) {
2192 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2195 if (ops->ndo_set_vf_mac)
2196 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2202 if (tb[IFLA_VF_VLAN]) {
2203 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2206 if (ops->ndo_set_vf_vlan)
2207 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2209 htons(ETH_P_8021Q));
2214 if (tb[IFLA_VF_VLAN_LIST]) {
2215 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2216 struct nlattr *attr;
2220 if (!ops->ndo_set_vf_vlan)
2223 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2224 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2225 nla_len(attr) < NLA_HDRLEN) {
2228 if (len >= MAX_VLAN_LIST_LEN)
2230 ivvl[len] = nla_data(attr);
2237 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2238 ivvl[0]->qos, ivvl[0]->vlan_proto);
2243 if (tb[IFLA_VF_TX_RATE]) {
2244 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2245 struct ifla_vf_info ivf;
2248 if (ops->ndo_get_vf_config)
2249 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2254 if (ops->ndo_set_vf_rate)
2255 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2262 if (tb[IFLA_VF_RATE]) {
2263 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2266 if (ops->ndo_set_vf_rate)
2267 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2274 if (tb[IFLA_VF_SPOOFCHK]) {
2275 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2278 if (ops->ndo_set_vf_spoofchk)
2279 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2285 if (tb[IFLA_VF_LINK_STATE]) {
2286 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2289 if (ops->ndo_set_vf_link_state)
2290 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2296 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2297 struct ifla_vf_rss_query_en *ivrssq_en;
2300 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2301 if (ops->ndo_set_vf_rss_query_en)
2302 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2303 ivrssq_en->setting);
2308 if (tb[IFLA_VF_TRUST]) {
2309 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2312 if (ops->ndo_set_vf_trust)
2313 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2318 if (tb[IFLA_VF_IB_NODE_GUID]) {
2319 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2321 if (!ops->ndo_set_vf_guid)
2324 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2327 if (tb[IFLA_VF_IB_PORT_GUID]) {
2328 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2330 if (!ops->ndo_set_vf_guid)
2333 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2339 static int do_set_master(struct net_device *dev, int ifindex,
2340 struct netlink_ext_ack *extack)
2342 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2343 const struct net_device_ops *ops;
2347 if (upper_dev->ifindex == ifindex)
2349 ops = upper_dev->netdev_ops;
2350 if (ops->ndo_del_slave) {
2351 err = ops->ndo_del_slave(upper_dev, dev);
2360 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2363 ops = upper_dev->netdev_ops;
2364 if (ops->ndo_add_slave) {
2365 err = ops->ndo_add_slave(upper_dev, dev, extack);
2375 #define DO_SETLINK_MODIFIED 0x01
2376 /* notify flag means notify + modified. */
2377 #define DO_SETLINK_NOTIFY 0x03
2378 static int do_setlink(const struct sk_buff *skb,
2379 struct net_device *dev, struct ifinfomsg *ifm,
2380 struct netlink_ext_ack *extack,
2381 struct nlattr **tb, char *ifname, int status)
2383 const struct net_device_ops *ops = dev->netdev_ops;
2386 err = validate_linkmsg(dev, tb);
2390 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2391 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2398 err = dev_change_net_namespace(dev, net, ifname);
2402 status |= DO_SETLINK_MODIFIED;
2406 struct rtnl_link_ifmap *u_map;
2409 if (!ops->ndo_set_config) {
2414 if (!netif_device_present(dev)) {
2419 u_map = nla_data(tb[IFLA_MAP]);
2420 k_map.mem_start = (unsigned long) u_map->mem_start;
2421 k_map.mem_end = (unsigned long) u_map->mem_end;
2422 k_map.base_addr = (unsigned short) u_map->base_addr;
2423 k_map.irq = (unsigned char) u_map->irq;
2424 k_map.dma = (unsigned char) u_map->dma;
2425 k_map.port = (unsigned char) u_map->port;
2427 err = ops->ndo_set_config(dev, &k_map);
2431 status |= DO_SETLINK_NOTIFY;
2434 if (tb[IFLA_ADDRESS]) {
2435 struct sockaddr *sa;
2438 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2440 sa = kmalloc(len, GFP_KERNEL);
2445 sa->sa_family = dev->type;
2446 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2448 err = dev_set_mac_address(dev, sa, extack);
2452 status |= DO_SETLINK_MODIFIED;
2456 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2459 status |= DO_SETLINK_MODIFIED;
2462 if (tb[IFLA_GROUP]) {
2463 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2464 status |= DO_SETLINK_NOTIFY;
2468 * Interface selected by interface index but interface
2469 * name provided implies that a name change has been
2472 if (ifm->ifi_index > 0 && ifname[0]) {
2473 err = dev_change_name(dev, ifname);
2476 status |= DO_SETLINK_MODIFIED;
2479 if (tb[IFLA_IFALIAS]) {
2480 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2481 nla_len(tb[IFLA_IFALIAS]));
2484 status |= DO_SETLINK_NOTIFY;
2487 if (tb[IFLA_BROADCAST]) {
2488 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2489 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2492 if (ifm->ifi_flags || ifm->ifi_change) {
2493 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2499 if (tb[IFLA_MASTER]) {
2500 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2503 status |= DO_SETLINK_MODIFIED;
2506 if (tb[IFLA_CARRIER]) {
2507 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2510 status |= DO_SETLINK_MODIFIED;
2513 if (tb[IFLA_TXQLEN]) {
2514 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2516 err = dev_change_tx_queue_len(dev, value);
2519 status |= DO_SETLINK_MODIFIED;
2522 if (tb[IFLA_GSO_MAX_SIZE]) {
2523 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2525 if (max_size > GSO_MAX_SIZE) {
2530 if (dev->gso_max_size ^ max_size) {
2531 netif_set_gso_max_size(dev, max_size);
2532 status |= DO_SETLINK_MODIFIED;
2536 if (tb[IFLA_GSO_MAX_SEGS]) {
2537 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2539 if (max_segs > GSO_MAX_SEGS) {
2544 if (dev->gso_max_segs ^ max_segs) {
2545 dev->gso_max_segs = max_segs;
2546 status |= DO_SETLINK_MODIFIED;
2550 if (tb[IFLA_OPERSTATE])
2551 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2553 if (tb[IFLA_LINKMODE]) {
2554 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2556 write_lock_bh(&dev_base_lock);
2557 if (dev->link_mode ^ value)
2558 status |= DO_SETLINK_NOTIFY;
2559 dev->link_mode = value;
2560 write_unlock_bh(&dev_base_lock);
2563 if (tb[IFLA_VFINFO_LIST]) {
2564 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2565 struct nlattr *attr;
2568 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2569 if (nla_type(attr) != IFLA_VF_INFO ||
2570 nla_len(attr) < NLA_HDRLEN) {
2574 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2580 err = do_setvfinfo(dev, vfinfo);
2583 status |= DO_SETLINK_NOTIFY;
2588 if (tb[IFLA_VF_PORTS]) {
2589 struct nlattr *port[IFLA_PORT_MAX+1];
2590 struct nlattr *attr;
2595 if (!ops->ndo_set_vf_port)
2598 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2599 if (nla_type(attr) != IFLA_VF_PORT ||
2600 nla_len(attr) < NLA_HDRLEN) {
2604 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2610 if (!port[IFLA_PORT_VF]) {
2614 vf = nla_get_u32(port[IFLA_PORT_VF]);
2615 err = ops->ndo_set_vf_port(dev, vf, port);
2618 status |= DO_SETLINK_NOTIFY;
2623 if (tb[IFLA_PORT_SELF]) {
2624 struct nlattr *port[IFLA_PORT_MAX+1];
2626 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2628 ifla_port_policy, NULL);
2633 if (ops->ndo_set_vf_port)
2634 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2637 status |= DO_SETLINK_NOTIFY;
2640 if (tb[IFLA_AF_SPEC]) {
2644 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2645 const struct rtnl_af_ops *af_ops;
2649 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2651 err = af_ops->set_link_af(dev, af);
2658 status |= DO_SETLINK_NOTIFY;
2663 if (tb[IFLA_PROTO_DOWN]) {
2664 err = dev_change_proto_down(dev,
2665 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2668 status |= DO_SETLINK_NOTIFY;
2672 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2675 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2677 ifla_xdp_policy, NULL);
2681 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2686 if (xdp[IFLA_XDP_FLAGS]) {
2687 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2688 if (xdp_flags & ~XDP_FLAGS_MASK) {
2692 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2698 if (xdp[IFLA_XDP_FD]) {
2699 err = dev_change_xdp_fd(dev, extack,
2700 nla_get_s32(xdp[IFLA_XDP_FD]),
2704 status |= DO_SETLINK_NOTIFY;
2709 if (status & DO_SETLINK_MODIFIED) {
2710 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2711 netdev_state_change(dev);
2714 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",
2721 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2722 struct netlink_ext_ack *extack)
2724 struct net *net = sock_net(skb->sk);
2725 struct ifinfomsg *ifm;
2726 struct net_device *dev;
2728 struct nlattr *tb[IFLA_MAX+1];
2729 char ifname[IFNAMSIZ];
2731 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2732 ifla_policy, extack);
2736 err = rtnl_ensure_unique_netns(tb, extack, false);
2740 if (tb[IFLA_IFNAME])
2741 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2746 ifm = nlmsg_data(nlh);
2747 if (ifm->ifi_index > 0)
2748 dev = __dev_get_by_index(net, ifm->ifi_index);
2749 else if (tb[IFLA_IFNAME])
2750 dev = __dev_get_by_name(net, ifname);
2759 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2764 static int rtnl_group_dellink(const struct net *net, int group)
2766 struct net_device *dev, *aux;
2767 LIST_HEAD(list_kill);
2773 for_each_netdev(net, dev) {
2774 if (dev->group == group) {
2775 const struct rtnl_link_ops *ops;
2778 ops = dev->rtnl_link_ops;
2779 if (!ops || !ops->dellink)
2787 for_each_netdev_safe(net, dev, aux) {
2788 if (dev->group == group) {
2789 const struct rtnl_link_ops *ops;
2791 ops = dev->rtnl_link_ops;
2792 ops->dellink(dev, &list_kill);
2795 unregister_netdevice_many(&list_kill);
2800 int rtnl_delete_link(struct net_device *dev)
2802 const struct rtnl_link_ops *ops;
2803 LIST_HEAD(list_kill);
2805 ops = dev->rtnl_link_ops;
2806 if (!ops || !ops->dellink)
2809 ops->dellink(dev, &list_kill);
2810 unregister_netdevice_many(&list_kill);
2814 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2816 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2817 struct netlink_ext_ack *extack)
2819 struct net *net = sock_net(skb->sk);
2820 struct net *tgt_net = net;
2821 struct net_device *dev = NULL;
2822 struct ifinfomsg *ifm;
2823 char ifname[IFNAMSIZ];
2824 struct nlattr *tb[IFLA_MAX+1];
2828 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2829 ifla_policy, extack);
2833 err = rtnl_ensure_unique_netns(tb, extack, true);
2837 if (tb[IFLA_IFNAME])
2838 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2840 if (tb[IFLA_TARGET_NETNSID]) {
2841 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2842 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2843 if (IS_ERR(tgt_net))
2844 return PTR_ERR(tgt_net);
2848 ifm = nlmsg_data(nlh);
2849 if (ifm->ifi_index > 0)
2850 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2851 else if (tb[IFLA_IFNAME])
2852 dev = __dev_get_by_name(tgt_net, ifname);
2853 else if (tb[IFLA_GROUP])
2854 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2859 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2865 err = rtnl_delete_link(dev);
2874 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2876 unsigned int old_flags;
2879 old_flags = dev->flags;
2880 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2881 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2887 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2888 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2890 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2891 __dev_notify_flags(dev, old_flags, ~0U);
2895 EXPORT_SYMBOL(rtnl_configure_link);
2897 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2898 unsigned char name_assign_type,
2899 const struct rtnl_link_ops *ops,
2900 struct nlattr *tb[],
2901 struct netlink_ext_ack *extack)
2903 struct net_device *dev;
2904 unsigned int num_tx_queues = 1;
2905 unsigned int num_rx_queues = 1;
2907 if (tb[IFLA_NUM_TX_QUEUES])
2908 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2909 else if (ops->get_num_tx_queues)
2910 num_tx_queues = ops->get_num_tx_queues();
2912 if (tb[IFLA_NUM_RX_QUEUES])
2913 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2914 else if (ops->get_num_rx_queues)
2915 num_rx_queues = ops->get_num_rx_queues();
2917 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2918 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
2919 return ERR_PTR(-EINVAL);
2922 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2923 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
2924 return ERR_PTR(-EINVAL);
2927 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2928 ops->setup, num_tx_queues, num_rx_queues);
2930 return ERR_PTR(-ENOMEM);
2932 dev_net_set(dev, net);
2933 dev->rtnl_link_ops = ops;
2934 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2937 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2938 if (tb[IFLA_ADDRESS]) {
2939 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2940 nla_len(tb[IFLA_ADDRESS]));
2941 dev->addr_assign_type = NET_ADDR_SET;
2943 if (tb[IFLA_BROADCAST])
2944 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2945 nla_len(tb[IFLA_BROADCAST]));
2946 if (tb[IFLA_TXQLEN])
2947 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2948 if (tb[IFLA_OPERSTATE])
2949 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2950 if (tb[IFLA_LINKMODE])
2951 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2953 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2954 if (tb[IFLA_GSO_MAX_SIZE])
2955 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2956 if (tb[IFLA_GSO_MAX_SEGS])
2957 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2961 EXPORT_SYMBOL(rtnl_create_link);
2963 static int rtnl_group_changelink(const struct sk_buff *skb,
2964 struct net *net, int group,
2965 struct ifinfomsg *ifm,
2966 struct netlink_ext_ack *extack,
2969 struct net_device *dev, *aux;
2972 for_each_netdev_safe(net, dev, aux) {
2973 if (dev->group == group) {
2974 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2983 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2984 struct nlattr **attr, struct netlink_ext_ack *extack)
2986 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
2987 unsigned char name_assign_type = NET_NAME_USER;
2988 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
2989 const struct rtnl_link_ops *m_ops = NULL;
2990 struct net_device *master_dev = NULL;
2991 struct net *net = sock_net(skb->sk);
2992 const struct rtnl_link_ops *ops;
2993 struct nlattr *tb[IFLA_MAX + 1];
2994 struct net *dest_net, *link_net;
2995 struct nlattr **slave_data;
2996 char kind[MODULE_NAME_LEN];
2997 struct net_device *dev;
2998 struct ifinfomsg *ifm;
2999 char ifname[IFNAMSIZ];
3000 struct nlattr **data;
3003 #ifdef CONFIG_MODULES
3006 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3007 ifla_policy, extack);
3011 err = rtnl_ensure_unique_netns(tb, extack, false);
3015 if (tb[IFLA_IFNAME])
3016 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3020 ifm = nlmsg_data(nlh);
3021 if (ifm->ifi_index > 0)
3022 dev = __dev_get_by_index(net, ifm->ifi_index);
3025 dev = __dev_get_by_name(net, ifname);
3031 master_dev = netdev_master_upper_dev_get(dev);
3033 m_ops = master_dev->rtnl_link_ops;
3036 err = validate_linkmsg(dev, tb);
3040 if (tb[IFLA_LINKINFO]) {
3041 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3043 ifla_info_policy, NULL);
3047 memset(linkinfo, 0, sizeof(linkinfo));
3049 if (linkinfo[IFLA_INFO_KIND]) {
3050 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3051 ops = rtnl_link_ops_get(kind);
3059 if (ops->maxtype > RTNL_MAX_TYPE)
3062 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3063 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3064 linkinfo[IFLA_INFO_DATA],
3065 ops->policy, extack);
3070 if (ops->validate) {
3071 err = ops->validate(tb, data, extack);
3079 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3082 if (m_ops->slave_maxtype &&
3083 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3084 err = nla_parse_nested_deprecated(slave_attr,
3085 m_ops->slave_maxtype,
3086 linkinfo[IFLA_INFO_SLAVE_DATA],
3087 m_ops->slave_policy,
3091 slave_data = slave_attr;
3098 if (nlh->nlmsg_flags & NLM_F_EXCL)
3100 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3103 if (linkinfo[IFLA_INFO_DATA]) {
3104 if (!ops || ops != dev->rtnl_link_ops ||
3108 err = ops->changelink(dev, tb, data, extack);
3111 status |= DO_SETLINK_NOTIFY;
3114 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3115 if (!m_ops || !m_ops->slave_changelink)
3118 err = m_ops->slave_changelink(master_dev, dev, tb,
3119 slave_data, extack);
3122 status |= DO_SETLINK_NOTIFY;
3125 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3128 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3129 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3130 return rtnl_group_changelink(skb, net,
3131 nla_get_u32(tb[IFLA_GROUP]),
3136 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3140 #ifdef CONFIG_MODULES
3143 request_module("rtnl-link-%s", kind);
3145 ops = rtnl_link_ops_get(kind);
3150 NL_SET_ERR_MSG(extack, "Unknown device type");
3158 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3159 name_assign_type = NET_NAME_ENUM;
3162 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3163 if (IS_ERR(dest_net))
3164 return PTR_ERR(dest_net);
3166 if (tb[IFLA_LINK_NETNSID]) {
3167 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3169 link_net = get_net_ns_by_id(dest_net, id);
3171 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3176 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3182 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3183 name_assign_type, ops, tb, extack);
3189 dev->ifindex = ifm->ifi_index;
3192 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3193 /* Drivers should call free_netdev() in ->destructor
3194 * and unregister it on failure after registration
3195 * so that device could be finally freed in rtnl_unlock.
3198 /* If device is not registered at all, free it now */
3199 if (dev->reg_state == NETREG_UNINITIALIZED)
3204 err = register_netdevice(dev);
3210 err = rtnl_configure_link(dev, ifm);
3212 goto out_unregister;
3214 err = dev_change_net_namespace(dev, dest_net, ifname);
3216 goto out_unregister;
3218 if (tb[IFLA_MASTER]) {
3219 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3221 goto out_unregister;
3230 LIST_HEAD(list_kill);
3232 ops->dellink(dev, &list_kill);
3233 unregister_netdevice_many(&list_kill);
3235 unregister_netdevice(dev);
3240 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3241 struct netlink_ext_ack *extack)
3243 struct nlattr **attr;
3246 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3250 ret = __rtnl_newlink(skb, nlh, attr, extack);
3255 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3256 const struct nlmsghdr *nlh,
3258 struct netlink_ext_ack *extack)
3260 struct ifinfomsg *ifm;
3263 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3264 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3268 if (!netlink_strict_get_check(skb))
3269 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3270 ifla_policy, extack);
3272 ifm = nlmsg_data(nlh);
3273 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3275 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3279 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3280 ifla_policy, extack);
3284 for (i = 0; i <= IFLA_MAX; i++) {
3291 case IFLA_TARGET_NETNSID:
3294 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3302 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3303 struct netlink_ext_ack *extack)
3305 struct net *net = sock_net(skb->sk);
3306 struct net *tgt_net = net;
3307 struct ifinfomsg *ifm;
3308 char ifname[IFNAMSIZ];
3309 struct nlattr *tb[IFLA_MAX+1];
3310 struct net_device *dev = NULL;
3311 struct sk_buff *nskb;
3314 u32 ext_filter_mask = 0;
3316 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3320 err = rtnl_ensure_unique_netns(tb, extack, true);
3324 if (tb[IFLA_TARGET_NETNSID]) {
3325 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3326 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3327 if (IS_ERR(tgt_net))
3328 return PTR_ERR(tgt_net);
3331 if (tb[IFLA_IFNAME])
3332 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3334 if (tb[IFLA_EXT_MASK])
3335 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3338 ifm = nlmsg_data(nlh);
3339 if (ifm->ifi_index > 0)
3340 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3341 else if (tb[IFLA_IFNAME])
3342 dev = __dev_get_by_name(tgt_net, ifname);
3351 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3355 err = rtnl_fill_ifinfo(nskb, dev, net,
3356 RTM_NEWLINK, NETLINK_CB(skb).portid,
3357 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3358 0, NULL, 0, netnsid);
3360 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3361 WARN_ON(err == -EMSGSIZE);
3364 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3372 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3374 struct net *net = sock_net(skb->sk);
3375 struct net_device *dev;
3376 struct nlattr *tb[IFLA_MAX+1];
3377 u32 ext_filter_mask = 0;
3378 u16 min_ifinfo_dump_size = 0;
3381 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3382 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3383 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3385 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3386 if (tb[IFLA_EXT_MASK])
3387 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3390 if (!ext_filter_mask)
3391 return NLMSG_GOODSIZE;
3393 * traverse the list of net devices and compute the minimum
3394 * buffer size based upon the filter mask.
3397 for_each_netdev_rcu(net, dev) {
3398 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3404 return nlmsg_total_size(min_ifinfo_dump_size);
3407 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3410 int s_idx = cb->family;
3411 int type = cb->nlh->nlmsg_type - RTM_BASE;
3417 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3418 struct rtnl_link **tab;
3419 struct rtnl_link *link;
3420 rtnl_dumpit_func dumpit;
3422 if (idx < s_idx || idx == PF_PACKET)
3425 if (type < 0 || type >= RTM_NR_MSGTYPES)
3428 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3436 dumpit = link->dumpit;
3441 memset(&cb->args[0], 0, sizeof(cb->args));
3445 ret = dumpit(skb, cb);
3451 return skb->len ? : ret;
3454 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3455 unsigned int change,
3456 u32 event, gfp_t flags, int *new_nsid,
3459 struct net *net = dev_net(dev);
3460 struct sk_buff *skb;
3462 size_t if_info_size;
3464 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3468 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3469 type, 0, 0, change, 0, 0, event,
3470 new_nsid, new_ifindex, -1);
3472 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3473 WARN_ON(err == -EMSGSIZE);
3480 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3484 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3486 struct net *net = dev_net(dev);
3488 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3491 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3492 unsigned int change, u32 event,
3493 gfp_t flags, int *new_nsid, int new_ifindex)
3495 struct sk_buff *skb;
3497 if (dev->reg_state != NETREG_REGISTERED)
3500 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3503 rtmsg_ifinfo_send(skb, dev, flags);
3506 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3509 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3513 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3514 gfp_t flags, int *new_nsid, int new_ifindex)
3516 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3517 new_nsid, new_ifindex);
3520 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3521 struct net_device *dev,
3522 u8 *addr, u16 vid, u32 pid, u32 seq,
3523 int type, unsigned int flags,
3524 int nlflags, u16 ndm_state)
3526 struct nlmsghdr *nlh;
3529 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3533 ndm = nlmsg_data(nlh);
3534 ndm->ndm_family = AF_BRIDGE;
3537 ndm->ndm_flags = flags;
3539 ndm->ndm_ifindex = dev->ifindex;
3540 ndm->ndm_state = ndm_state;
3542 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3543 goto nla_put_failure;
3545 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3546 goto nla_put_failure;
3548 nlmsg_end(skb, nlh);
3552 nlmsg_cancel(skb, nlh);
3556 static inline size_t rtnl_fdb_nlmsg_size(void)
3558 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3559 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3560 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3564 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3567 struct net *net = dev_net(dev);
3568 struct sk_buff *skb;
3571 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3575 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3576 0, 0, type, NTF_SELF, 0, ndm_state);
3582 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3585 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3589 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3591 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3592 struct nlattr *tb[],
3593 struct net_device *dev,
3594 const unsigned char *addr, u16 vid,
3599 /* If aging addresses are supported device will need to
3600 * implement its own handler for this.
3602 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3603 pr_info("%s: FDB only supports static addresses\n", dev->name);
3608 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3612 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3613 err = dev_uc_add_excl(dev, addr);
3614 else if (is_multicast_ether_addr(addr))
3615 err = dev_mc_add_excl(dev, addr);
3617 /* Only return duplicate errors if NLM_F_EXCL is set */
3618 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3623 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3625 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3626 struct netlink_ext_ack *extack)
3631 if (nla_len(vlan_attr) != sizeof(u16)) {
3632 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3636 vid = nla_get_u16(vlan_attr);
3638 if (!vid || vid >= VLAN_VID_MASK) {
3639 NL_SET_ERR_MSG(extack, "invalid vlan id");
3647 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3648 struct netlink_ext_ack *extack)
3650 struct net *net = sock_net(skb->sk);
3652 struct nlattr *tb[NDA_MAX+1];
3653 struct net_device *dev;
3658 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3663 ndm = nlmsg_data(nlh);
3664 if (ndm->ndm_ifindex == 0) {
3665 NL_SET_ERR_MSG(extack, "invalid ifindex");
3669 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3671 NL_SET_ERR_MSG(extack, "unknown ifindex");
3675 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3676 NL_SET_ERR_MSG(extack, "invalid address");
3680 if (dev->type != ARPHRD_ETHER) {
3681 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3685 addr = nla_data(tb[NDA_LLADDR]);
3687 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3693 /* Support fdb on master device the net/bridge default case */
3694 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3695 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3696 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3697 const struct net_device_ops *ops = br_dev->netdev_ops;
3699 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3700 nlh->nlmsg_flags, extack);
3704 ndm->ndm_flags &= ~NTF_MASTER;
3707 /* Embedded bridge, macvlan, and any other device support */
3708 if ((ndm->ndm_flags & NTF_SELF)) {
3709 if (dev->netdev_ops->ndo_fdb_add)
3710 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3715 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3719 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3721 ndm->ndm_flags &= ~NTF_SELF;
3729 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3731 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3732 struct nlattr *tb[],
3733 struct net_device *dev,
3734 const unsigned char *addr, u16 vid)
3738 /* If aging addresses are supported device will need to
3739 * implement its own handler for this.
3741 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3742 pr_info("%s: FDB only supports static addresses\n", dev->name);
3746 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3747 err = dev_uc_del(dev, addr);
3748 else if (is_multicast_ether_addr(addr))
3749 err = dev_mc_del(dev, addr);
3753 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3755 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3756 struct netlink_ext_ack *extack)
3758 struct net *net = sock_net(skb->sk);
3760 struct nlattr *tb[NDA_MAX+1];
3761 struct net_device *dev;
3766 if (!netlink_capable(skb, CAP_NET_ADMIN))
3769 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3774 ndm = nlmsg_data(nlh);
3775 if (ndm->ndm_ifindex == 0) {
3776 NL_SET_ERR_MSG(extack, "invalid ifindex");
3780 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3782 NL_SET_ERR_MSG(extack, "unknown ifindex");
3786 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3787 NL_SET_ERR_MSG(extack, "invalid address");
3791 if (dev->type != ARPHRD_ETHER) {
3792 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3796 addr = nla_data(tb[NDA_LLADDR]);
3798 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3804 /* Support fdb on master device the net/bridge default case */
3805 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3806 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3807 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3808 const struct net_device_ops *ops = br_dev->netdev_ops;
3810 if (ops->ndo_fdb_del)
3811 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3816 ndm->ndm_flags &= ~NTF_MASTER;
3819 /* Embedded bridge, macvlan, and any other device support */
3820 if (ndm->ndm_flags & NTF_SELF) {
3821 if (dev->netdev_ops->ndo_fdb_del)
3822 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3825 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3828 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3830 ndm->ndm_flags &= ~NTF_SELF;
3837 static int nlmsg_populate_fdb(struct sk_buff *skb,
3838 struct netlink_callback *cb,
3839 struct net_device *dev,
3841 struct netdev_hw_addr_list *list)
3843 struct netdev_hw_addr *ha;
3847 portid = NETLINK_CB(cb->skb).portid;
3848 seq = cb->nlh->nlmsg_seq;
3850 list_for_each_entry(ha, &list->list, list) {
3851 if (*idx < cb->args[2])
3854 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3856 RTM_NEWNEIGH, NTF_SELF,
3857 NLM_F_MULTI, NUD_PERMANENT);
3867 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3868 * @skb: socket buffer to store message in
3869 * @cb: netlink callback
3871 * @filter_dev: ignored
3872 * @idx: the number of FDB table entries dumped is added to *@idx
3874 * Default netdevice operation to dump the existing unicast address list.
3875 * Returns number of addresses from list put in skb.
3877 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3878 struct netlink_callback *cb,
3879 struct net_device *dev,
3880 struct net_device *filter_dev,
3885 if (dev->type != ARPHRD_ETHER)
3888 netif_addr_lock_bh(dev);
3889 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3892 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3894 netif_addr_unlock_bh(dev);
3897 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3899 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3900 int *br_idx, int *brport_idx,
3901 struct netlink_ext_ack *extack)
3903 struct nlattr *tb[NDA_MAX + 1];
3907 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3908 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3912 ndm = nlmsg_data(nlh);
3913 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3914 ndm->ndm_flags || ndm->ndm_type) {
3915 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3919 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
3920 NDA_MAX, NULL, extack);
3924 *brport_idx = ndm->ndm_ifindex;
3925 for (i = 0; i <= NDA_MAX; ++i) {
3931 if (nla_len(tb[i]) != sizeof(u32)) {
3932 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3935 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3938 if (nla_len(tb[i]) != sizeof(u32)) {
3939 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3942 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3945 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3953 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3954 int *br_idx, int *brport_idx,
3955 struct netlink_ext_ack *extack)
3957 struct nlattr *tb[IFLA_MAX+1];
3960 /* A hack to preserve kernel<->userspace interface.
3961 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3962 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3963 * So, check for ndmsg with an optional u32 attribute (not used here).
3964 * Fortunately these sizes don't conflict with the size of ifinfomsg
3965 * with an optional attribute.
3967 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3968 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3969 nla_attr_size(sizeof(u32)))) {
3970 struct ifinfomsg *ifm;
3972 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
3973 tb, IFLA_MAX, ifla_policy,
3977 } else if (err == 0) {
3978 if (tb[IFLA_MASTER])
3979 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3982 ifm = nlmsg_data(nlh);
3983 *brport_idx = ifm->ifi_index;
3988 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3990 struct net_device *dev;
3991 struct net_device *br_dev = NULL;
3992 const struct net_device_ops *ops = NULL;
3993 const struct net_device_ops *cops = NULL;
3994 struct net *net = sock_net(skb->sk);
3995 struct hlist_head *head;
4003 if (cb->strict_check)
4004 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4007 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4013 br_dev = __dev_get_by_index(net, br_idx);
4017 ops = br_dev->netdev_ops;
4021 s_idx = cb->args[1];
4023 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4025 head = &net->dev_index_head[h];
4026 hlist_for_each_entry(dev, head, index_hlist) {
4028 if (brport_idx && (dev->ifindex != brport_idx))
4031 if (!br_idx) { /* user did not specify a specific bridge */
4032 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4033 br_dev = netdev_master_upper_dev_get(dev);
4034 cops = br_dev->netdev_ops;
4037 if (dev != br_dev &&
4038 !(dev->priv_flags & IFF_BRIDGE_PORT))
4041 if (br_dev != netdev_master_upper_dev_get(dev) &&
4042 !(dev->priv_flags & IFF_EBRIDGE))
4050 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4051 if (cops && cops->ndo_fdb_dump) {
4052 err = cops->ndo_fdb_dump(skb, cb,
4055 if (err == -EMSGSIZE)
4060 if (dev->netdev_ops->ndo_fdb_dump)
4061 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4065 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4067 if (err == -EMSGSIZE)
4072 /* reset fdb offset to 0 for rest of the interfaces */
4088 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4089 struct nlattr **tb, u8 *ndm_flags,
4090 int *br_idx, int *brport_idx, u8 **addr,
4091 u16 *vid, struct netlink_ext_ack *extack)
4096 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4097 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4101 ndm = nlmsg_data(nlh);
4102 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4104 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4108 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4109 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4113 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4114 NDA_MAX, nda_policy, extack);
4118 *ndm_flags = ndm->ndm_flags;
4119 *brport_idx = ndm->ndm_ifindex;
4120 for (i = 0; i <= NDA_MAX; ++i) {
4126 *br_idx = nla_get_u32(tb[i]);
4129 if (nla_len(tb[i]) != ETH_ALEN) {
4130 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4133 *addr = nla_data(tb[i]);
4136 err = fdb_vid_parse(tb[i], vid, extack);
4143 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4151 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4152 struct netlink_ext_ack *extack)
4154 struct net_device *dev = NULL, *br_dev = NULL;
4155 const struct net_device_ops *ops = NULL;
4156 struct net *net = sock_net(in_skb->sk);
4157 struct nlattr *tb[NDA_MAX + 1];
4158 struct sk_buff *skb;
4166 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4167 &brport_idx, &addr, &vid, extack);
4172 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4177 dev = __dev_get_by_index(net, brport_idx);
4179 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4186 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4190 br_dev = __dev_get_by_index(net, br_idx);
4192 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4195 ops = br_dev->netdev_ops;
4199 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4200 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4201 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4204 br_dev = netdev_master_upper_dev_get(dev);
4206 NL_SET_ERR_MSG(extack, "Master of device not found");
4209 ops = br_dev->netdev_ops;
4211 if (!(ndm_flags & NTF_SELF)) {
4212 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4215 ops = dev->netdev_ops;
4219 if (!br_dev && !dev) {
4220 NL_SET_ERR_MSG(extack, "No device specified");
4224 if (!ops || !ops->ndo_fdb_get) {
4225 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4229 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4235 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4236 NETLINK_CB(in_skb).portid,
4237 nlh->nlmsg_seq, extack);
4241 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4247 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4248 unsigned int attrnum, unsigned int flag)
4251 return nla_put_u8(skb, attrnum, !!(flags & flag));
4255 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4256 struct net_device *dev, u16 mode,
4257 u32 flags, u32 mask, int nlflags,
4259 int (*vlan_fill)(struct sk_buff *skb,
4260 struct net_device *dev,
4263 struct nlmsghdr *nlh;
4264 struct ifinfomsg *ifm;
4265 struct nlattr *br_afspec;
4266 struct nlattr *protinfo;
4267 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4268 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4271 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4275 ifm = nlmsg_data(nlh);
4276 ifm->ifi_family = AF_BRIDGE;
4278 ifm->ifi_type = dev->type;
4279 ifm->ifi_index = dev->ifindex;
4280 ifm->ifi_flags = dev_get_flags(dev);
4281 ifm->ifi_change = 0;
4284 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4285 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4286 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4288 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4290 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4291 (dev->ifindex != dev_get_iflink(dev) &&
4292 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4293 goto nla_put_failure;
4295 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4297 goto nla_put_failure;
4299 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4300 nla_nest_cancel(skb, br_afspec);
4301 goto nla_put_failure;
4304 if (mode != BRIDGE_MODE_UNDEF) {
4305 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4306 nla_nest_cancel(skb, br_afspec);
4307 goto nla_put_failure;
4311 err = vlan_fill(skb, dev, filter_mask);
4313 nla_nest_cancel(skb, br_afspec);
4314 goto nla_put_failure;
4317 nla_nest_end(skb, br_afspec);
4319 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4321 goto nla_put_failure;
4323 if (brport_nla_put_flag(skb, flags, mask,
4324 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4325 brport_nla_put_flag(skb, flags, mask,
4326 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4327 brport_nla_put_flag(skb, flags, mask,
4328 IFLA_BRPORT_FAST_LEAVE,
4329 BR_MULTICAST_FAST_LEAVE) ||
4330 brport_nla_put_flag(skb, flags, mask,
4331 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4332 brport_nla_put_flag(skb, flags, mask,
4333 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4334 brport_nla_put_flag(skb, flags, mask,
4335 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4336 brport_nla_put_flag(skb, flags, mask,
4337 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4338 brport_nla_put_flag(skb, flags, mask,
4339 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4340 nla_nest_cancel(skb, protinfo);
4341 goto nla_put_failure;
4344 nla_nest_end(skb, protinfo);
4346 nlmsg_end(skb, nlh);
4349 nlmsg_cancel(skb, nlh);
4350 return err ? err : -EMSGSIZE;
4352 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4354 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4355 bool strict_check, u32 *filter_mask,
4356 struct netlink_ext_ack *extack)
4358 struct nlattr *tb[IFLA_MAX+1];
4362 struct ifinfomsg *ifm;
4364 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4365 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4369 ifm = nlmsg_data(nlh);
4370 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4371 ifm->ifi_change || ifm->ifi_index) {
4372 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4376 err = nlmsg_parse_deprecated_strict(nlh,
4377 sizeof(struct ifinfomsg),
4378 tb, IFLA_MAX, ifla_policy,
4381 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4382 tb, IFLA_MAX, ifla_policy,
4388 /* new attributes should only be added with strict checking */
4389 for (i = 0; i <= IFLA_MAX; ++i) {
4395 *filter_mask = nla_get_u32(tb[i]);
4399 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4408 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4410 const struct nlmsghdr *nlh = cb->nlh;
4411 struct net *net = sock_net(skb->sk);
4412 struct net_device *dev;
4414 u32 portid = NETLINK_CB(cb->skb).portid;
4415 u32 seq = nlh->nlmsg_seq;
4416 u32 filter_mask = 0;
4419 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4421 if (err < 0 && cb->strict_check)
4425 for_each_netdev_rcu(net, dev) {
4426 const struct net_device_ops *ops = dev->netdev_ops;
4427 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4429 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4430 if (idx >= cb->args[0]) {
4431 err = br_dev->netdev_ops->ndo_bridge_getlink(
4432 skb, portid, seq, dev,
4433 filter_mask, NLM_F_MULTI);
4434 if (err < 0 && err != -EOPNOTSUPP) {
4435 if (likely(skb->len))
4444 if (ops->ndo_bridge_getlink) {
4445 if (idx >= cb->args[0]) {
4446 err = ops->ndo_bridge_getlink(skb, portid,
4450 if (err < 0 && err != -EOPNOTSUPP) {
4451 if (likely(skb->len))
4468 static inline size_t bridge_nlmsg_size(void)
4470 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4471 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4472 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4473 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4474 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4475 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4476 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4477 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4478 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4479 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4480 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4483 static int rtnl_bridge_notify(struct net_device *dev)
4485 struct net *net = dev_net(dev);
4486 struct sk_buff *skb;
4487 int err = -EOPNOTSUPP;
4489 if (!dev->netdev_ops->ndo_bridge_getlink)
4492 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4498 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4505 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4508 WARN_ON(err == -EMSGSIZE);
4511 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4515 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4516 struct netlink_ext_ack *extack)
4518 struct net *net = sock_net(skb->sk);
4519 struct ifinfomsg *ifm;
4520 struct net_device *dev;
4521 struct nlattr *br_spec, *attr = NULL;
4522 int rem, err = -EOPNOTSUPP;
4524 bool have_flags = false;
4526 if (nlmsg_len(nlh) < sizeof(*ifm))
4529 ifm = nlmsg_data(nlh);
4530 if (ifm->ifi_family != AF_BRIDGE)
4531 return -EPFNOSUPPORT;
4533 dev = __dev_get_by_index(net, ifm->ifi_index);
4535 NL_SET_ERR_MSG(extack, "unknown ifindex");
4539 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4541 nla_for_each_nested(attr, br_spec, rem) {
4542 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4543 if (nla_len(attr) < sizeof(flags))
4547 flags = nla_get_u16(attr);
4553 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4554 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4556 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4561 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4566 flags &= ~BRIDGE_FLAGS_MASTER;
4569 if ((flags & BRIDGE_FLAGS_SELF)) {
4570 if (!dev->netdev_ops->ndo_bridge_setlink)
4573 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4577 flags &= ~BRIDGE_FLAGS_SELF;
4579 /* Generate event to notify upper layer of bridge
4582 err = rtnl_bridge_notify(dev);
4587 memcpy(nla_data(attr), &flags, sizeof(flags));
4592 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4593 struct netlink_ext_ack *extack)
4595 struct net *net = sock_net(skb->sk);
4596 struct ifinfomsg *ifm;
4597 struct net_device *dev;
4598 struct nlattr *br_spec, *attr = NULL;
4599 int rem, err = -EOPNOTSUPP;
4601 bool have_flags = false;
4603 if (nlmsg_len(nlh) < sizeof(*ifm))
4606 ifm = nlmsg_data(nlh);
4607 if (ifm->ifi_family != AF_BRIDGE)
4608 return -EPFNOSUPPORT;
4610 dev = __dev_get_by_index(net, ifm->ifi_index);
4612 NL_SET_ERR_MSG(extack, "unknown ifindex");
4616 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4618 nla_for_each_nested(attr, br_spec, rem) {
4619 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4620 if (nla_len(attr) < sizeof(flags))
4624 flags = nla_get_u16(attr);
4630 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4631 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4633 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4638 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4642 flags &= ~BRIDGE_FLAGS_MASTER;
4645 if ((flags & BRIDGE_FLAGS_SELF)) {
4646 if (!dev->netdev_ops->ndo_bridge_dellink)
4649 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4653 flags &= ~BRIDGE_FLAGS_SELF;
4655 /* Generate event to notify upper layer of bridge
4658 err = rtnl_bridge_notify(dev);
4663 memcpy(nla_data(attr), &flags, sizeof(flags));
4668 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4670 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4671 (!idxattr || idxattr == attrid);
4674 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4675 static int rtnl_get_offload_stats_attr_size(int attr_id)
4678 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4679 return sizeof(struct rtnl_link_stats64);
4685 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4688 struct nlattr *attr = NULL;
4693 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4694 dev->netdev_ops->ndo_get_offload_stats))
4697 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4698 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4699 if (attr_id < *prividx)
4702 size = rtnl_get_offload_stats_attr_size(attr_id);
4706 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4709 attr = nla_reserve_64bit(skb, attr_id, size,
4710 IFLA_OFFLOAD_XSTATS_UNSPEC);
4712 goto nla_put_failure;
4714 attr_data = nla_data(attr);
4715 memset(attr_data, 0, size);
4716 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4719 goto get_offload_stats_failure;
4730 get_offload_stats_failure:
4735 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4741 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4742 dev->netdev_ops->ndo_get_offload_stats))
4745 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4746 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4747 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4749 size = rtnl_get_offload_stats_attr_size(attr_id);
4750 nla_size += nla_total_size_64bit(size);
4754 nla_size += nla_total_size(0);
4759 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4760 int type, u32 pid, u32 seq, u32 change,
4761 unsigned int flags, unsigned int filter_mask,
4762 int *idxattr, int *prividx)
4764 struct if_stats_msg *ifsm;
4765 struct nlmsghdr *nlh;
4766 struct nlattr *attr;
4767 int s_prividx = *prividx;
4772 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4776 ifsm = nlmsg_data(nlh);
4777 ifsm->family = PF_UNSPEC;
4780 ifsm->ifindex = dev->ifindex;
4781 ifsm->filter_mask = filter_mask;
4783 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4784 struct rtnl_link_stats64 *sp;
4786 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4787 sizeof(struct rtnl_link_stats64),
4790 goto nla_put_failure;
4792 sp = nla_data(attr);
4793 dev_get_stats(dev, sp);
4796 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4797 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4799 if (ops && ops->fill_linkxstats) {
4800 *idxattr = IFLA_STATS_LINK_XSTATS;
4801 attr = nla_nest_start_noflag(skb,
4802 IFLA_STATS_LINK_XSTATS);
4804 goto nla_put_failure;
4806 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4807 nla_nest_end(skb, attr);
4809 goto nla_put_failure;
4814 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4816 const struct rtnl_link_ops *ops = NULL;
4817 const struct net_device *master;
4819 master = netdev_master_upper_dev_get(dev);
4821 ops = master->rtnl_link_ops;
4822 if (ops && ops->fill_linkxstats) {
4823 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4824 attr = nla_nest_start_noflag(skb,
4825 IFLA_STATS_LINK_XSTATS_SLAVE);
4827 goto nla_put_failure;
4829 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4830 nla_nest_end(skb, attr);
4832 goto nla_put_failure;
4837 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4839 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4840 attr = nla_nest_start_noflag(skb,
4841 IFLA_STATS_LINK_OFFLOAD_XSTATS);
4843 goto nla_put_failure;
4845 err = rtnl_get_offload_stats(skb, dev, prividx);
4846 if (err == -ENODATA)
4847 nla_nest_cancel(skb, attr);
4849 nla_nest_end(skb, attr);
4851 if (err && err != -ENODATA)
4852 goto nla_put_failure;
4856 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4857 struct rtnl_af_ops *af_ops;
4859 *idxattr = IFLA_STATS_AF_SPEC;
4860 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
4862 goto nla_put_failure;
4865 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4866 if (af_ops->fill_stats_af) {
4870 af = nla_nest_start_noflag(skb,
4874 goto nla_put_failure;
4876 err = af_ops->fill_stats_af(skb, dev);
4878 if (err == -ENODATA) {
4879 nla_nest_cancel(skb, af);
4880 } else if (err < 0) {
4882 goto nla_put_failure;
4885 nla_nest_end(skb, af);
4890 nla_nest_end(skb, attr);
4895 nlmsg_end(skb, nlh);
4900 /* not a multi message or no progress mean a real error */
4901 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4902 nlmsg_cancel(skb, nlh);
4904 nlmsg_end(skb, nlh);
4909 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4914 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4915 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4917 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4918 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4919 int attr = IFLA_STATS_LINK_XSTATS;
4921 if (ops && ops->get_linkxstats_size) {
4922 size += nla_total_size(ops->get_linkxstats_size(dev,
4924 /* for IFLA_STATS_LINK_XSTATS */
4925 size += nla_total_size(0);
4929 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4930 struct net_device *_dev = (struct net_device *)dev;
4931 const struct rtnl_link_ops *ops = NULL;
4932 const struct net_device *master;
4934 /* netdev_master_upper_dev_get can't take const */
4935 master = netdev_master_upper_dev_get(_dev);
4937 ops = master->rtnl_link_ops;
4938 if (ops && ops->get_linkxstats_size) {
4939 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4941 size += nla_total_size(ops->get_linkxstats_size(dev,
4943 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4944 size += nla_total_size(0);
4948 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4949 size += rtnl_get_offload_stats_size(dev);
4951 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4952 struct rtnl_af_ops *af_ops;
4954 /* for IFLA_STATS_AF_SPEC */
4955 size += nla_total_size(0);
4958 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4959 if (af_ops->get_stats_af_size) {
4960 size += nla_total_size(
4961 af_ops->get_stats_af_size(dev));
4964 size += nla_total_size(0);
4973 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
4974 bool is_dump, struct netlink_ext_ack *extack)
4976 struct if_stats_msg *ifsm;
4978 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
4979 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
4986 ifsm = nlmsg_data(nlh);
4988 /* only requests using strict checks can pass data to influence
4989 * the dump. The legacy exception is filter_mask.
4991 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
4992 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4995 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
4996 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4999 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5000 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5007 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5008 struct netlink_ext_ack *extack)
5010 struct net *net = sock_net(skb->sk);
5011 struct net_device *dev = NULL;
5012 int idxattr = 0, prividx = 0;
5013 struct if_stats_msg *ifsm;
5014 struct sk_buff *nskb;
5018 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5023 ifsm = nlmsg_data(nlh);
5024 if (ifsm->ifindex > 0)
5025 dev = __dev_get_by_index(net, ifsm->ifindex);
5032 filter_mask = ifsm->filter_mask;
5036 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5040 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5041 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5042 0, filter_mask, &idxattr, &prividx);
5044 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5045 WARN_ON(err == -EMSGSIZE);
5048 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5054 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5056 struct netlink_ext_ack *extack = cb->extack;
5057 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5058 struct net *net = sock_net(skb->sk);
5059 unsigned int flags = NLM_F_MULTI;
5060 struct if_stats_msg *ifsm;
5061 struct hlist_head *head;
5062 struct net_device *dev;
5063 u32 filter_mask = 0;
5067 s_idx = cb->args[1];
5068 s_idxattr = cb->args[2];
5069 s_prividx = cb->args[3];
5071 cb->seq = net->dev_base_seq;
5073 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5077 ifsm = nlmsg_data(cb->nlh);
5078 filter_mask = ifsm->filter_mask;
5080 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5084 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5086 head = &net->dev_index_head[h];
5087 hlist_for_each_entry(dev, head, index_hlist) {
5090 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5091 NETLINK_CB(cb->skb).portid,
5092 cb->nlh->nlmsg_seq, 0,
5094 &s_idxattr, &s_prividx);
5095 /* If we ran out of room on the first message,
5098 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5104 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5110 cb->args[3] = s_prividx;
5111 cb->args[2] = s_idxattr;
5118 /* Process one rtnetlink message. */
5120 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5121 struct netlink_ext_ack *extack)
5123 struct net *net = sock_net(skb->sk);
5124 struct rtnl_link *link;
5125 struct module *owner;
5126 int err = -EOPNOTSUPP;
5127 rtnl_doit_func doit;
5133 type = nlh->nlmsg_type;
5139 /* All the messages must have at least 1 byte length */
5140 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5143 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5146 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5150 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5152 rtnl_dumpit_func dumpit;
5153 u16 min_dump_alloc = 0;
5155 link = rtnl_get_link(family, type);
5156 if (!link || !link->dumpit) {
5158 link = rtnl_get_link(family, type);
5159 if (!link || !link->dumpit)
5162 owner = link->owner;
5163 dumpit = link->dumpit;
5165 if (type == RTM_GETLINK - RTM_BASE)
5166 min_dump_alloc = rtnl_calcit(skb, nlh);
5169 /* need to do this before rcu_read_unlock() */
5170 if (!try_module_get(owner))
5171 err = -EPROTONOSUPPORT;
5177 struct netlink_dump_control c = {
5179 .min_dump_alloc = min_dump_alloc,
5182 err = netlink_dump_start(rtnl, skb, nlh, &c);
5183 /* netlink_dump_start() will keep a reference on
5184 * module if dump is still in progress.
5191 link = rtnl_get_link(family, type);
5192 if (!link || !link->doit) {
5194 link = rtnl_get_link(PF_UNSPEC, type);
5195 if (!link || !link->doit)
5199 owner = link->owner;
5200 if (!try_module_get(owner)) {
5201 err = -EPROTONOSUPPORT;
5205 flags = link->flags;
5206 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5210 err = doit(skb, nlh, extack);
5217 link = rtnl_get_link(family, type);
5218 if (link && link->doit)
5219 err = link->doit(skb, nlh, extack);
5235 static void rtnetlink_rcv(struct sk_buff *skb)
5237 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5240 static int rtnetlink_bind(struct net *net, int group)
5243 case RTNLGRP_IPV4_MROUTE_R:
5244 case RTNLGRP_IPV6_MROUTE_R:
5245 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5252 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5254 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5258 case NETDEV_CHANGEMTU:
5259 case NETDEV_CHANGEADDR:
5260 case NETDEV_CHANGENAME:
5261 case NETDEV_FEAT_CHANGE:
5262 case NETDEV_BONDING_FAILOVER:
5263 case NETDEV_POST_TYPE_CHANGE:
5264 case NETDEV_NOTIFY_PEERS:
5265 case NETDEV_CHANGEUPPER:
5266 case NETDEV_RESEND_IGMP:
5267 case NETDEV_CHANGEINFODATA:
5268 case NETDEV_CHANGELOWERSTATE:
5269 case NETDEV_CHANGE_TX_QUEUE_LEN:
5270 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5271 GFP_KERNEL, NULL, 0);
5279 static struct notifier_block rtnetlink_dev_notifier = {
5280 .notifier_call = rtnetlink_event,
5284 static int __net_init rtnetlink_net_init(struct net *net)
5287 struct netlink_kernel_cfg cfg = {
5288 .groups = RTNLGRP_MAX,
5289 .input = rtnetlink_rcv,
5290 .cb_mutex = &rtnl_mutex,
5291 .flags = NL_CFG_F_NONROOT_RECV,
5292 .bind = rtnetlink_bind,
5295 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5302 static void __net_exit rtnetlink_net_exit(struct net *net)
5304 netlink_kernel_release(net->rtnl);
5308 static struct pernet_operations rtnetlink_net_ops = {
5309 .init = rtnetlink_net_init,
5310 .exit = rtnetlink_net_exit,
5313 void __init rtnetlink_init(void)
5315 if (register_pernet_subsys(&rtnetlink_net_ops))
5316 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5318 register_netdevice_notifier(&rtnetlink_dev_notifier);
5320 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5321 rtnl_dump_ifinfo, 0);
5322 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5323 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5324 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5326 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5327 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5328 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5330 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5331 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5332 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5334 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5335 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5336 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5338 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,