1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetic was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
56 #include <net/devlink.h>
57 #if IS_ENABLED(CONFIG_IPV6)
58 #include <net/addrconf.h>
60 #include <linux/dpll.h>
64 #define RTNL_MAX_TYPE 50
65 #define RTNL_SLAVE_MAX_TYPE 44
69 rtnl_dumpit_func dumpit
;
75 static DEFINE_MUTEX(rtnl_mutex
);
79 mutex_lock(&rtnl_mutex
);
81 EXPORT_SYMBOL(rtnl_lock
);
83 int rtnl_lock_killable(void)
85 return mutex_lock_killable(&rtnl_mutex
);
87 EXPORT_SYMBOL(rtnl_lock_killable
);
89 static struct sk_buff
*defer_kfree_skb_list
;
90 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
93 tail
->next
= defer_kfree_skb_list
;
94 defer_kfree_skb_list
= head
;
97 EXPORT_SYMBOL(rtnl_kfree_skbs
);
99 void __rtnl_unlock(void)
101 struct sk_buff
*head
= defer_kfree_skb_list
;
103 defer_kfree_skb_list
= NULL
;
105 /* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
106 * is used. In some places, e.g. in cfg80211, we have code that will do
113 * and because netdev_run_todo() acquires the RTNL for items on the list
114 * we could cause a situation such as this:
117 * unregister_netdevice()
125 * // list not empty now
126 * // because of thread 2
128 * while (!list_empty(...))
133 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
134 * it's not used in cases where something is added to do the list.
136 WARN_ON(!list_empty(&net_todo_list
));
138 mutex_unlock(&rtnl_mutex
);
141 struct sk_buff
*next
= head
->next
;
149 void rtnl_unlock(void)
151 /* This fellow will unlock it for us. */
154 EXPORT_SYMBOL(rtnl_unlock
);
156 int rtnl_trylock(void)
158 return mutex_trylock(&rtnl_mutex
);
160 EXPORT_SYMBOL(rtnl_trylock
);
162 int rtnl_is_locked(void)
164 return mutex_is_locked(&rtnl_mutex
);
166 EXPORT_SYMBOL(rtnl_is_locked
);
168 bool refcount_dec_and_rtnl_lock(refcount_t
*r
)
170 return refcount_dec_and_mutex_lock(r
, &rtnl_mutex
);
172 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock
);
174 #ifdef CONFIG_PROVE_LOCKING
175 bool lockdep_rtnl_is_held(void)
177 return lockdep_is_held(&rtnl_mutex
);
179 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
180 #endif /* #ifdef CONFIG_PROVE_LOCKING */
182 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
183 void __rtnl_net_lock(struct net
*net
)
187 mutex_lock(&net
->rtnl_mutex
);
189 EXPORT_SYMBOL(__rtnl_net_lock
);
191 void __rtnl_net_unlock(struct net
*net
)
195 mutex_unlock(&net
->rtnl_mutex
);
197 EXPORT_SYMBOL(__rtnl_net_unlock
);
199 void rtnl_net_lock(struct net
*net
)
202 __rtnl_net_lock(net
);
204 EXPORT_SYMBOL(rtnl_net_lock
);
206 void rtnl_net_unlock(struct net
*net
)
208 __rtnl_net_unlock(net
);
211 EXPORT_SYMBOL(rtnl_net_unlock
);
213 int rtnl_net_trylock(struct net
*net
)
215 int ret
= rtnl_trylock();
218 __rtnl_net_lock(net
);
222 EXPORT_SYMBOL(rtnl_net_trylock
);
224 static int rtnl_net_cmp_locks(const struct net
*net_a
, const struct net
*net_b
)
226 if (net_eq(net_a
, net_b
))
229 /* always init_net first */
230 if (net_eq(net_a
, &init_net
))
233 if (net_eq(net_b
, &init_net
))
236 /* otherwise lock in ascending order */
237 return net_a
< net_b
? -1 : 1;
240 int rtnl_net_lock_cmp_fn(const struct lockdep_map
*a
, const struct lockdep_map
*b
)
242 const struct net
*net_a
, *net_b
;
244 net_a
= container_of(a
, struct net
, rtnl_mutex
.dep_map
);
245 net_b
= container_of(b
, struct net
, rtnl_mutex
.dep_map
);
247 return rtnl_net_cmp_locks(net_a
, net_b
);
250 bool rtnl_net_is_locked(struct net
*net
)
252 return rtnl_is_locked() && mutex_is_locked(&net
->rtnl_mutex
);
254 EXPORT_SYMBOL(rtnl_net_is_locked
);
256 bool lockdep_rtnl_net_is_held(struct net
*net
)
258 return lockdep_rtnl_is_held() && lockdep_is_held(&net
->rtnl_mutex
);
260 EXPORT_SYMBOL(lockdep_rtnl_net_is_held
);
262 static int rtnl_net_cmp_locks(const struct net
*net_a
, const struct net
*net_b
)
264 /* No need to swap */
270 /* ->newlink() needs to freeze 3 netns at most;
271 * 2 for the new device, 1 for its peer.
277 static void rtnl_nets_init(struct rtnl_nets
*rtnl_nets
)
279 memset(rtnl_nets
, 0, sizeof(*rtnl_nets
));
282 static void rtnl_nets_destroy(struct rtnl_nets
*rtnl_nets
)
286 for (i
= 0; i
< rtnl_nets
->len
; i
++) {
287 put_net(rtnl_nets
->net
[i
]);
288 rtnl_nets
->net
[i
] = NULL
;
295 * rtnl_nets_add - Add netns to be locked before ->newlink().
297 * @rtnl_nets: rtnl_nets pointer passed to ->get_peer_net().
298 * @net: netns pointer with an extra refcnt held.
300 * The extra refcnt is released in rtnl_nets_destroy().
302 static void rtnl_nets_add(struct rtnl_nets
*rtnl_nets
, struct net
*net
)
306 DEBUG_NET_WARN_ON_ONCE(rtnl_nets
->len
== ARRAY_SIZE(rtnl_nets
->net
));
308 for (i
= 0; i
< rtnl_nets
->len
; i
++) {
309 switch (rtnl_net_cmp_locks(rtnl_nets
->net
[i
], net
)) {
314 swap(rtnl_nets
->net
[i
], net
);
318 rtnl_nets
->net
[i
] = net
;
322 static void rtnl_nets_lock(struct rtnl_nets
*rtnl_nets
)
328 for (i
= 0; i
< rtnl_nets
->len
; i
++)
329 __rtnl_net_lock(rtnl_nets
->net
[i
]);
332 static void rtnl_nets_unlock(struct rtnl_nets
*rtnl_nets
)
336 for (i
= 0; i
< rtnl_nets
->len
; i
++)
337 __rtnl_net_unlock(rtnl_nets
->net
[i
]);
342 static struct rtnl_link __rcu
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
344 static inline int rtm_msgindex(int msgtype
)
346 int msgindex
= msgtype
- RTM_BASE
;
349 * msgindex < 0 implies someone tried to register a netlink
350 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
351 * the message type has not been added to linux/rtnetlink.h
353 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
358 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
360 struct rtnl_link __rcu
**tab
;
362 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
363 protocol
= PF_UNSPEC
;
365 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
367 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
369 return rcu_dereference_rtnl(tab
[msgtype
]);
372 static int rtnl_register_internal(struct module
*owner
,
373 int protocol
, int msgtype
,
374 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
377 struct rtnl_link
*link
, *old
;
378 struct rtnl_link __rcu
**tab
;
382 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
383 msgindex
= rtm_msgindex(msgtype
);
386 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
388 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
392 /* ensures we see the 0 stores */
393 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
396 old
= rtnl_dereference(tab
[msgindex
]);
398 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
402 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
407 WARN_ON(link
->owner
&& link
->owner
!= owner
);
410 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
413 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
415 link
->dumpit
= dumpit
;
417 WARN_ON(rtnl_msgtype_kind(msgtype
) != RTNL_KIND_DEL
&&
418 (flags
& RTNL_FLAG_BULK_DEL_SUPPORTED
));
419 link
->flags
|= flags
;
421 /* publish protocol:msgtype */
422 rcu_assign_pointer(tab
[msgindex
], link
);
432 * rtnl_unregister - Unregister a rtnetlink message type
433 * @protocol: Protocol family or PF_UNSPEC
434 * @msgtype: rtnetlink message type
436 * Returns 0 on success or a negative error code.
438 static int rtnl_unregister(int protocol
, int msgtype
)
440 struct rtnl_link __rcu
**tab
;
441 struct rtnl_link
*link
;
444 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
445 msgindex
= rtm_msgindex(msgtype
);
448 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
454 link
= rcu_replace_pointer_rtnl(tab
[msgindex
], NULL
);
457 kfree_rcu(link
, rcu
);
463 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
464 * @protocol : Protocol family or PF_UNSPEC
466 * Identical to calling rtnl_unregster() for all registered message types
467 * of a certain protocol family.
469 void rtnl_unregister_all(int protocol
)
471 struct rtnl_link __rcu
**tab
;
472 struct rtnl_link
*link
;
475 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
478 tab
= rcu_replace_pointer_rtnl(rtnl_msg_handlers
[protocol
], NULL
);
483 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
484 link
= rcu_replace_pointer_rtnl(tab
[msgindex
], NULL
);
485 kfree_rcu(link
, rcu
);
493 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
496 * __rtnl_register_many - Register rtnetlink message types
497 * @handlers: Array of struct rtnl_msg_handlers
498 * @n: The length of @handlers
500 * Registers the specified function pointers (at least one of them has
501 * to be non-NULL) to be called whenever a request message for the
502 * specified protocol family and message type is received.
504 * The special protocol family PF_UNSPEC may be used to define fallback
505 * function pointers for the case when no entry for the specific protocol
508 * When one element of @handlers fails to register,
509 * 1) built-in: panics.
510 * 2) modules : the previous successful registrations are unwinded
511 * and an error is returned.
513 * Use rtnl_register_many().
515 int __rtnl_register_many(const struct rtnl_msg_handler
*handlers
, int n
)
517 const struct rtnl_msg_handler
*handler
;
520 for (i
= 0, handler
= handlers
; i
< n
; i
++, handler
++) {
521 err
= rtnl_register_internal(handler
->owner
, handler
->protocol
,
522 handler
->msgtype
, handler
->doit
,
523 handler
->dumpit
, handler
->flags
);
526 panic("Unable to register rtnetlink message "
527 "handlers, %pS\n", handlers
);
529 __rtnl_unregister_many(handlers
, i
);
536 EXPORT_SYMBOL_GPL(__rtnl_register_many
);
538 void __rtnl_unregister_many(const struct rtnl_msg_handler
*handlers
, int n
)
540 const struct rtnl_msg_handler
*handler
;
543 for (i
= n
- 1, handler
= handlers
+ n
- 1; i
>= 0; i
--, handler
--)
544 rtnl_unregister(handler
->protocol
, handler
->msgtype
);
546 EXPORT_SYMBOL_GPL(__rtnl_unregister_many
);
548 static DEFINE_MUTEX(link_ops_mutex
);
549 static LIST_HEAD(link_ops
);
551 static struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
, int *srcu_index
)
553 struct rtnl_link_ops
*ops
;
557 list_for_each_entry_rcu(ops
, &link_ops
, list
) {
558 if (!strcmp(ops
->kind
, kind
)) {
559 *srcu_index
= srcu_read_lock(&ops
->srcu
);
571 static void rtnl_link_ops_put(struct rtnl_link_ops
*ops
, int srcu_index
)
573 srcu_read_unlock(&ops
->srcu
, srcu_index
);
577 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
578 * @ops: struct rtnl_link_ops * to register
580 * Returns 0 on success or a negative error code.
582 int rtnl_link_register(struct rtnl_link_ops
*ops
)
584 struct rtnl_link_ops
*tmp
;
587 /* Sanity-check max sizes to avoid stack buffer overflow. */
588 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
589 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
592 /* The check for alloc/setup is here because if ops
593 * does not have that filled up, it is not possible
594 * to use the ops for creating device. So do not
595 * fill up dellink as well. That disables rtnl_dellink.
597 if ((ops
->alloc
|| ops
->setup
) && !ops
->dellink
)
598 ops
->dellink
= unregister_netdevice_queue
;
600 err
= init_srcu_struct(&ops
->srcu
);
604 mutex_lock(&link_ops_mutex
);
606 list_for_each_entry(tmp
, &link_ops
, list
) {
607 if (!strcmp(ops
->kind
, tmp
->kind
)) {
613 list_add_tail_rcu(&ops
->list
, &link_ops
);
615 mutex_unlock(&link_ops_mutex
);
619 EXPORT_SYMBOL_GPL(rtnl_link_register
);
621 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
623 struct net_device
*dev
;
624 LIST_HEAD(list_kill
);
626 for_each_netdev(net
, dev
) {
627 if (dev
->rtnl_link_ops
== ops
)
628 ops
->dellink(dev
, &list_kill
);
630 unregister_netdevice_many(&list_kill
);
633 /* Return with the rtnl_lock held when there are no network
634 * devices unregistering in any network namespace.
636 static void rtnl_lock_unregistering_all(void)
638 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
640 add_wait_queue(&netdev_unregistering_wq
, &wait
);
643 /* We held write locked pernet_ops_rwsem, and parallel
644 * setup_net() and cleanup_net() are not possible.
646 if (!atomic_read(&dev_unreg_count
))
650 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
652 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
656 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
657 * @ops: struct rtnl_link_ops * to unregister
659 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
663 mutex_lock(&link_ops_mutex
);
664 list_del_rcu(&ops
->list
);
665 mutex_unlock(&link_ops_mutex
);
667 synchronize_srcu(&ops
->srcu
);
668 cleanup_srcu_struct(&ops
->srcu
);
670 /* Close the race with setup_net() and cleanup_net() */
671 down_write(&pernet_ops_rwsem
);
672 rtnl_lock_unregistering_all();
675 __rtnl_kill_links(net
, ops
);
678 up_write(&pernet_ops_rwsem
);
680 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
682 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
684 struct net_device
*master_dev
;
685 const struct rtnl_link_ops
*ops
;
690 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
694 ops
= master_dev
->rtnl_link_ops
;
695 if (!ops
|| !ops
->get_slave_size
)
697 /* IFLA_INFO_SLAVE_DATA + nested data */
698 size
= nla_total_size(sizeof(struct nlattr
)) +
699 ops
->get_slave_size(master_dev
, dev
);
706 static size_t rtnl_link_get_size(const struct net_device
*dev
)
708 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
714 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
715 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
718 /* IFLA_INFO_DATA + nested data */
719 size
+= nla_total_size(sizeof(struct nlattr
)) +
722 if (ops
->get_xstats_size
)
723 /* IFLA_INFO_XSTATS */
724 size
+= nla_total_size(ops
->get_xstats_size(dev
));
726 size
+= rtnl_link_get_slave_info_data_size(dev
);
731 static LIST_HEAD(rtnl_af_ops
);
733 static struct rtnl_af_ops
*rtnl_af_lookup(const int family
, int *srcu_index
)
735 struct rtnl_af_ops
*ops
;
741 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
742 if (ops
->family
== family
) {
743 *srcu_index
= srcu_read_lock(&ops
->srcu
);
755 static void rtnl_af_put(struct rtnl_af_ops
*ops
, int srcu_index
)
757 srcu_read_unlock(&ops
->srcu
, srcu_index
);
761 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
762 * @ops: struct rtnl_af_ops * to register
764 * Return: 0 on success or a negative error code.
766 int rtnl_af_register(struct rtnl_af_ops
*ops
)
768 int err
= init_srcu_struct(&ops
->srcu
);
774 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
779 EXPORT_SYMBOL_GPL(rtnl_af_register
);
782 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
783 * @ops: struct rtnl_af_ops * to unregister
785 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
788 list_del_rcu(&ops
->list
);
792 synchronize_srcu(&ops
->srcu
);
793 cleanup_srcu_struct(&ops
->srcu
);
795 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
797 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
800 struct rtnl_af_ops
*af_ops
;
804 size
= nla_total_size(sizeof(struct nlattr
));
807 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
808 if (af_ops
->get_link_af_size
) {
809 /* AF_* + nested data */
810 size
+= nla_total_size(sizeof(struct nlattr
)) +
811 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
819 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
821 struct net_device
*master_dev
;
826 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
827 if (master_dev
&& master_dev
->rtnl_link_ops
)
833 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
834 const struct net_device
*dev
)
836 struct net_device
*master_dev
;
837 const struct rtnl_link_ops
*ops
;
838 struct nlattr
*slave_data
;
841 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
844 ops
= master_dev
->rtnl_link_ops
;
847 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
849 if (ops
->fill_slave_info
) {
850 slave_data
= nla_nest_start_noflag(skb
, IFLA_INFO_SLAVE_DATA
);
853 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
855 goto err_cancel_slave_data
;
856 nla_nest_end(skb
, slave_data
);
860 err_cancel_slave_data
:
861 nla_nest_cancel(skb
, slave_data
);
865 static int rtnl_link_info_fill(struct sk_buff
*skb
,
866 const struct net_device
*dev
)
868 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
874 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
876 if (ops
->fill_xstats
) {
877 err
= ops
->fill_xstats(skb
, dev
);
881 if (ops
->fill_info
) {
882 data
= nla_nest_start_noflag(skb
, IFLA_INFO_DATA
);
885 err
= ops
->fill_info(skb
, dev
);
887 goto err_cancel_data
;
888 nla_nest_end(skb
, data
);
893 nla_nest_cancel(skb
, data
);
897 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
899 struct nlattr
*linkinfo
;
902 linkinfo
= nla_nest_start_noflag(skb
, IFLA_LINKINFO
);
903 if (linkinfo
== NULL
)
906 err
= rtnl_link_info_fill(skb
, dev
);
908 goto err_cancel_link
;
910 err
= rtnl_link_slave_info_fill(skb
, dev
);
912 goto err_cancel_link
;
914 nla_nest_end(skb
, linkinfo
);
918 nla_nest_cancel(skb
, linkinfo
);
923 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
925 struct sock
*rtnl
= net
->rtnl
;
927 return nlmsg_notify(rtnl
, skb
, pid
, group
, echo
, GFP_KERNEL
);
930 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
932 struct sock
*rtnl
= net
->rtnl
;
934 return nlmsg_unicast(rtnl
, skb
, pid
);
936 EXPORT_SYMBOL(rtnl_unicast
);
938 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
939 const struct nlmsghdr
*nlh
, gfp_t flags
)
941 struct sock
*rtnl
= net
->rtnl
;
943 nlmsg_notify(rtnl
, skb
, pid
, group
, nlmsg_report(nlh
), flags
);
945 EXPORT_SYMBOL(rtnl_notify
);
947 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
949 struct sock
*rtnl
= net
->rtnl
;
951 netlink_set_err(rtnl
, 0, group
, error
);
953 EXPORT_SYMBOL(rtnl_set_sk_err
);
955 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
960 /* nothing is dumped for dst_default_metrics, so just skip the loop */
961 if (metrics
== dst_default_metrics
.metrics
)
964 mx
= nla_nest_start_noflag(skb
, RTA_METRICS
);
968 for (i
= 0; i
< RTAX_MAX
; i
++) {
970 if (i
== RTAX_CC_ALGO
- 1) {
971 char tmp
[TCP_CA_NAME_MAX
], *name
;
973 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
976 if (nla_put_string(skb
, i
+ 1, name
))
977 goto nla_put_failure
;
978 } else if (i
== RTAX_FEATURES
- 1) {
979 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
983 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
984 if (nla_put_u32(skb
, i
+ 1, user_features
))
985 goto nla_put_failure
;
987 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
988 goto nla_put_failure
;
995 nla_nest_cancel(skb
, mx
);
999 return nla_nest_end(skb
, mx
);
1002 nla_nest_cancel(skb
, mx
);
1005 EXPORT_SYMBOL(rtnetlink_put_metrics
);
1007 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
1008 long expires
, u32 error
)
1010 struct rta_cacheinfo ci
= {
1016 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
1017 ci
.rta_used
= dst
->__use
;
1018 ci
.rta_clntref
= rcuref_read(&dst
->__rcuref
);
1021 unsigned long clock
;
1023 clock
= jiffies_to_clock_t(abs(expires
));
1024 clock
= min_t(unsigned long, clock
, INT_MAX
);
1025 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
1027 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
1029 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
1031 void netdev_set_operstate(struct net_device
*dev
, int newstate
)
1033 unsigned int old
= READ_ONCE(dev
->operstate
);
1036 if (old
== newstate
)
1038 } while (!try_cmpxchg(&dev
->operstate
, &old
, newstate
));
1040 netdev_state_change(dev
);
1042 EXPORT_SYMBOL(netdev_set_operstate
);
1044 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
1046 unsigned char operstate
= READ_ONCE(dev
->operstate
);
1048 switch (transition
) {
1050 if ((operstate
== IF_OPER_DORMANT
||
1051 operstate
== IF_OPER_TESTING
||
1052 operstate
== IF_OPER_UNKNOWN
) &&
1053 !netif_dormant(dev
) && !netif_testing(dev
))
1054 operstate
= IF_OPER_UP
;
1057 case IF_OPER_TESTING
:
1058 if (netif_oper_up(dev
))
1059 operstate
= IF_OPER_TESTING
;
1062 case IF_OPER_DORMANT
:
1063 if (netif_oper_up(dev
))
1064 operstate
= IF_OPER_DORMANT
;
1068 netdev_set_operstate(dev
, operstate
);
1071 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
1073 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
1074 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
1077 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
1078 const struct ifinfomsg
*ifm
)
1080 unsigned int flags
= ifm
->ifi_flags
;
1082 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
1083 if (ifm
->ifi_change
)
1084 flags
= (flags
& ifm
->ifi_change
) |
1085 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
1090 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
1091 const struct rtnl_link_stats64
*b
)
1093 a
->rx_packets
= b
->rx_packets
;
1094 a
->tx_packets
= b
->tx_packets
;
1095 a
->rx_bytes
= b
->rx_bytes
;
1096 a
->tx_bytes
= b
->tx_bytes
;
1097 a
->rx_errors
= b
->rx_errors
;
1098 a
->tx_errors
= b
->tx_errors
;
1099 a
->rx_dropped
= b
->rx_dropped
;
1100 a
->tx_dropped
= b
->tx_dropped
;
1102 a
->multicast
= b
->multicast
;
1103 a
->collisions
= b
->collisions
;
1105 a
->rx_length_errors
= b
->rx_length_errors
;
1106 a
->rx_over_errors
= b
->rx_over_errors
;
1107 a
->rx_crc_errors
= b
->rx_crc_errors
;
1108 a
->rx_frame_errors
= b
->rx_frame_errors
;
1109 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
1110 a
->rx_missed_errors
= b
->rx_missed_errors
;
1112 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
1113 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
1114 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
1115 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
1116 a
->tx_window_errors
= b
->tx_window_errors
;
1118 a
->rx_compressed
= b
->rx_compressed
;
1119 a
->tx_compressed
= b
->tx_compressed
;
1121 a
->rx_nohandler
= b
->rx_nohandler
;
1125 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
1126 u32 ext_filter_mask
)
1128 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
1129 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1130 size_t size
= nla_total_size(0);
1132 (nla_total_size(0) +
1133 nla_total_size(sizeof(struct ifla_vf_mac
)) +
1134 nla_total_size(sizeof(struct ifla_vf_broadcast
)) +
1135 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
1136 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
1137 nla_total_size(MAX_VLAN_LIST_LEN
*
1138 sizeof(struct ifla_vf_vlan_info
)) +
1139 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
1140 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
1141 nla_total_size(sizeof(struct ifla_vf_rate
)) +
1142 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
1143 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
1144 nla_total_size(sizeof(struct ifla_vf_trust
)));
1145 if (~ext_filter_mask
& RTEXT_FILTER_SKIP_STATS
) {
1147 (nla_total_size(0) + /* nest IFLA_VF_STATS */
1148 /* IFLA_VF_STATS_RX_PACKETS */
1149 nla_total_size_64bit(sizeof(__u64
)) +
1150 /* IFLA_VF_STATS_TX_PACKETS */
1151 nla_total_size_64bit(sizeof(__u64
)) +
1152 /* IFLA_VF_STATS_RX_BYTES */
1153 nla_total_size_64bit(sizeof(__u64
)) +
1154 /* IFLA_VF_STATS_TX_BYTES */
1155 nla_total_size_64bit(sizeof(__u64
)) +
1156 /* IFLA_VF_STATS_BROADCAST */
1157 nla_total_size_64bit(sizeof(__u64
)) +
1158 /* IFLA_VF_STATS_MULTICAST */
1159 nla_total_size_64bit(sizeof(__u64
)) +
1160 /* IFLA_VF_STATS_RX_DROPPED */
1161 nla_total_size_64bit(sizeof(__u64
)) +
1162 /* IFLA_VF_STATS_TX_DROPPED */
1163 nla_total_size_64bit(sizeof(__u64
)));
1170 static size_t rtnl_port_size(const struct net_device
*dev
,
1171 u32 ext_filter_mask
)
1173 size_t port_size
= nla_total_size(4) /* PORT_VF */
1174 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
1175 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
1176 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
1177 + nla_total_size(1) /* PROT_VDP_REQUEST */
1178 + nla_total_size(2); /* PORT_VDP_RESPONSE */
1179 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
1180 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
1182 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
1185 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1186 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1188 if (dev_num_vf(dev
->dev
.parent
))
1189 return port_self_size
+ vf_ports_size
+
1190 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
1192 return port_self_size
;
1195 static size_t rtnl_xdp_size(void)
1197 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
1198 nla_total_size(1) + /* XDP_ATTACHED */
1199 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
1200 nla_total_size(4); /* XDP_<mode>_PROG_ID */
1205 static size_t rtnl_prop_list_size(const struct net_device
*dev
)
1207 struct netdev_name_node
*name_node
;
1208 unsigned int cnt
= 0;
1211 list_for_each_entry_rcu(name_node
, &dev
->name_node
->list
, list
)
1218 return nla_total_size(0) + cnt
* nla_total_size(ALTIFNAMSIZ
);
1221 static size_t rtnl_proto_down_size(const struct net_device
*dev
)
1223 size_t size
= nla_total_size(1);
1225 /* Assume dev->proto_down_reason is not zero. */
1226 size
+= nla_total_size(0) + nla_total_size(4);
1231 static size_t rtnl_devlink_port_size(const struct net_device
*dev
)
1233 size_t size
= nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
1235 if (dev
->devlink_port
)
1236 size
+= devlink_nl_port_handle_size(dev
->devlink_port
);
1241 static size_t rtnl_dpll_pin_size(const struct net_device
*dev
)
1243 size_t size
= nla_total_size(0); /* nest IFLA_DPLL_PIN */
1245 size
+= dpll_netdev_pin_handle_size(dev
);
1250 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
1251 u32 ext_filter_mask
)
1253 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
1254 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
1255 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
1256 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
1257 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
1258 + nla_total_size(sizeof(struct rtnl_link_stats
))
1259 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
1260 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
1261 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
1262 + nla_total_size(4) /* IFLA_TXQLEN */
1263 + nla_total_size(4) /* IFLA_WEIGHT */
1264 + nla_total_size(4) /* IFLA_MTU */
1265 + nla_total_size(4) /* IFLA_LINK */
1266 + nla_total_size(4) /* IFLA_MASTER */
1267 + nla_total_size(1) /* IFLA_CARRIER */
1268 + nla_total_size(4) /* IFLA_PROMISCUITY */
1269 + nla_total_size(4) /* IFLA_ALLMULTI */
1270 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1271 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1272 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1273 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1274 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1275 + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
1276 + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
1277 + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
1278 + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1279 + nla_total_size(1) /* IFLA_OPERSTATE */
1280 + nla_total_size(1) /* IFLA_LINKMODE */
1281 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1282 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1283 + nla_total_size(4) /* IFLA_GROUP */
1284 + nla_total_size(ext_filter_mask
1285 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1286 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1287 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1288 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1289 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1290 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1291 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1292 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1293 + rtnl_xdp_size() /* IFLA_XDP */
1294 + nla_total_size(4) /* IFLA_EVENT */
1295 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1296 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1297 + rtnl_proto_down_size(dev
) /* proto down */
1298 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1299 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1300 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1301 + nla_total_size(4) /* IFLA_MIN_MTU */
1302 + nla_total_size(4) /* IFLA_MAX_MTU */
1303 + rtnl_prop_list_size(dev
)
1304 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_PERM_ADDRESS */
1305 + rtnl_devlink_port_size(dev
)
1306 + rtnl_dpll_pin_size(dev
)
1307 + nla_total_size(8) /* IFLA_MAX_PACING_OFFLOAD_HORIZON */
1311 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1313 struct nlattr
*vf_ports
;
1314 struct nlattr
*vf_port
;
1318 vf_ports
= nla_nest_start_noflag(skb
, IFLA_VF_PORTS
);
1322 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1323 vf_port
= nla_nest_start_noflag(skb
, IFLA_VF_PORT
);
1325 goto nla_put_failure
;
1326 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1327 goto nla_put_failure
;
1328 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1329 if (err
== -EMSGSIZE
)
1330 goto nla_put_failure
;
1332 nla_nest_cancel(skb
, vf_port
);
1335 nla_nest_end(skb
, vf_port
);
1338 nla_nest_end(skb
, vf_ports
);
1343 nla_nest_cancel(skb
, vf_ports
);
1347 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1349 struct nlattr
*port_self
;
1352 port_self
= nla_nest_start_noflag(skb
, IFLA_PORT_SELF
);
1356 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1358 nla_nest_cancel(skb
, port_self
);
1359 return (err
== -EMSGSIZE
) ? err
: 0;
1362 nla_nest_end(skb
, port_self
);
1367 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1368 u32 ext_filter_mask
)
1372 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1373 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1376 err
= rtnl_port_self_fill(skb
, dev
);
1380 if (dev_num_vf(dev
->dev
.parent
)) {
1381 err
= rtnl_vf_ports_fill(skb
, dev
);
1389 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1392 struct netdev_phys_item_id ppid
;
1394 err
= dev_get_phys_port_id(dev
, &ppid
);
1396 if (err
== -EOPNOTSUPP
)
1401 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1407 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1409 char name
[IFNAMSIZ
];
1412 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1414 if (err
== -EOPNOTSUPP
)
1419 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1425 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1427 struct netdev_phys_item_id ppid
= { };
1430 err
= dev_get_port_parent_id(dev
, &ppid
, false);
1432 if (err
== -EOPNOTSUPP
)
1437 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, ppid
.id_len
, ppid
.id
))
1443 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1444 struct net_device
*dev
)
1446 struct rtnl_link_stats64
*sp
;
1447 struct nlattr
*attr
;
1449 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1450 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1454 sp
= nla_data(attr
);
1455 dev_get_stats(dev
, sp
);
1457 attr
= nla_reserve(skb
, IFLA_STATS
,
1458 sizeof(struct rtnl_link_stats
));
1462 copy_rtnl_link_stats(nla_data(attr
), sp
);
1467 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1468 struct net_device
*dev
,
1470 u32 ext_filter_mask
)
1472 struct ifla_vf_rss_query_en vf_rss_query_en
;
1473 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1474 struct ifla_vf_link_state vf_linkstate
;
1475 struct ifla_vf_vlan_info vf_vlan_info
;
1476 struct ifla_vf_spoofchk vf_spoofchk
;
1477 struct ifla_vf_tx_rate vf_tx_rate
;
1478 struct ifla_vf_stats vf_stats
;
1479 struct ifla_vf_trust vf_trust
;
1480 struct ifla_vf_vlan vf_vlan
;
1481 struct ifla_vf_rate vf_rate
;
1482 struct ifla_vf_mac vf_mac
;
1483 struct ifla_vf_broadcast vf_broadcast
;
1484 struct ifla_vf_info ivi
;
1485 struct ifla_vf_guid node_guid
;
1486 struct ifla_vf_guid port_guid
;
1488 memset(&ivi
, 0, sizeof(ivi
));
1490 /* Not all SR-IOV capable drivers support the
1491 * spoofcheck and "RSS query enable" query. Preset to
1492 * -1 so the user space tool can detect that the driver
1493 * didn't report anything.
1496 ivi
.rss_query_en
= -1;
1498 /* The default value for VF link state is "auto"
1499 * IFLA_VF_LINK_STATE_AUTO which equals zero
1502 /* VLAN Protocol by default is 802.1Q */
1503 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1504 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1507 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1508 memset(&node_guid
, 0, sizeof(node_guid
));
1509 memset(&port_guid
, 0, sizeof(port_guid
));
1518 vf_rss_query_en
.vf
=
1521 port_guid
.vf
= ivi
.vf
;
1523 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1524 memcpy(vf_broadcast
.broadcast
, dev
->broadcast
, dev
->addr_len
);
1525 vf_vlan
.vlan
= ivi
.vlan
;
1526 vf_vlan
.qos
= ivi
.qos
;
1527 vf_vlan_info
.vlan
= ivi
.vlan
;
1528 vf_vlan_info
.qos
= ivi
.qos
;
1529 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1530 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1531 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1532 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1533 vf_spoofchk
.setting
= ivi
.spoofchk
;
1534 vf_linkstate
.link_state
= ivi
.linkstate
;
1535 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1536 vf_trust
.setting
= ivi
.trusted
;
1537 vf
= nla_nest_start_noflag(skb
, IFLA_VF_INFO
);
1540 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1541 nla_put(skb
, IFLA_VF_BROADCAST
, sizeof(vf_broadcast
), &vf_broadcast
) ||
1542 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1543 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1545 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1547 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1549 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1551 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1552 sizeof(vf_rss_query_en
),
1553 &vf_rss_query_en
) ||
1554 nla_put(skb
, IFLA_VF_TRUST
,
1555 sizeof(vf_trust
), &vf_trust
))
1556 goto nla_put_vf_failure
;
1558 if (dev
->netdev_ops
->ndo_get_vf_guid
&&
1559 !dev
->netdev_ops
->ndo_get_vf_guid(dev
, vfs_num
, &node_guid
,
1561 if (nla_put(skb
, IFLA_VF_IB_NODE_GUID
, sizeof(node_guid
),
1563 nla_put(skb
, IFLA_VF_IB_PORT_GUID
, sizeof(port_guid
),
1565 goto nla_put_vf_failure
;
1567 vfvlanlist
= nla_nest_start_noflag(skb
, IFLA_VF_VLAN_LIST
);
1569 goto nla_put_vf_failure
;
1570 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1572 nla_nest_cancel(skb
, vfvlanlist
);
1573 goto nla_put_vf_failure
;
1575 nla_nest_end(skb
, vfvlanlist
);
1576 if (~ext_filter_mask
& RTEXT_FILTER_SKIP_STATS
) {
1577 memset(&vf_stats
, 0, sizeof(vf_stats
));
1578 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1579 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1581 vfstats
= nla_nest_start_noflag(skb
, IFLA_VF_STATS
);
1583 goto nla_put_vf_failure
;
1584 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1585 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1586 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1587 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1588 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1589 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1590 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1591 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1592 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1593 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1594 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1595 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1596 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1597 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1598 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1599 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1600 nla_nest_cancel(skb
, vfstats
);
1601 goto nla_put_vf_failure
;
1603 nla_nest_end(skb
, vfstats
);
1605 nla_nest_end(skb
, vf
);
1609 nla_nest_cancel(skb
, vf
);
1613 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1614 struct net_device
*dev
,
1615 u32 ext_filter_mask
)
1617 struct nlattr
*vfinfo
;
1620 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1623 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1624 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1627 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1630 vfinfo
= nla_nest_start_noflag(skb
, IFLA_VFINFO_LIST
);
1634 for (i
= 0; i
< num_vfs
; i
++) {
1635 if (rtnl_fill_vfinfo(skb
, dev
, i
, ext_filter_mask
)) {
1636 nla_nest_cancel(skb
, vfinfo
);
1641 nla_nest_end(skb
, vfinfo
);
1645 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
,
1646 const struct net_device
*dev
)
1648 struct rtnl_link_ifmap map
;
1650 memset(&map
, 0, sizeof(map
));
1651 map
.mem_start
= READ_ONCE(dev
->mem_start
);
1652 map
.mem_end
= READ_ONCE(dev
->mem_end
);
1653 map
.base_addr
= READ_ONCE(dev
->base_addr
);
1654 map
.irq
= READ_ONCE(dev
->irq
);
1655 map
.dma
= READ_ONCE(dev
->dma
);
1656 map
.port
= READ_ONCE(dev
->if_port
);
1658 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1664 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1666 const struct bpf_prog
*generic_xdp_prog
;
1670 generic_xdp_prog
= rcu_dereference(dev
->xdp_prog
);
1671 if (generic_xdp_prog
)
1672 res
= generic_xdp_prog
->aux
->id
;
1678 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1680 return dev_xdp_prog_id(dev
, XDP_MODE_DRV
);
1683 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1685 return dev_xdp_prog_id(dev
, XDP_MODE_HW
);
1688 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1689 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1690 u32 (*get_prog_id
)(struct net_device
*dev
))
1695 curr_id
= get_prog_id(dev
);
1700 err
= nla_put_u32(skb
, attr
, curr_id
);
1704 if (*mode
!= XDP_ATTACHED_NONE
)
1705 *mode
= XDP_ATTACHED_MULTI
;
1712 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1719 xdp
= nla_nest_start_noflag(skb
, IFLA_XDP
);
1724 mode
= XDP_ATTACHED_NONE
;
1725 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1726 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1729 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1730 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1733 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1734 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1738 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1742 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1743 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1748 nla_nest_end(skb
, xdp
);
1752 nla_nest_cancel(skb
, xdp
);
1756 static u32
rtnl_get_event(unsigned long event
)
1758 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1762 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1764 case NETDEV_FEAT_CHANGE
:
1765 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1767 case NETDEV_BONDING_FAILOVER
:
1768 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1770 case NETDEV_NOTIFY_PEERS
:
1771 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1773 case NETDEV_RESEND_IGMP
:
1774 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1776 case NETDEV_CHANGEINFODATA
:
1777 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1783 return rtnl_event_type
;
1786 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1788 const struct net_device
*upper_dev
;
1793 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1795 ret
= nla_put_u32(skb
, IFLA_MASTER
,
1796 READ_ONCE(upper_dev
->ifindex
));
1802 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
,
1805 int iflink
= dev_get_iflink(dev
);
1807 if (force
|| READ_ONCE(dev
->ifindex
) != iflink
)
1808 return nla_put_u32(skb
, IFLA_LINK
, iflink
);
1813 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1814 struct net_device
*dev
)
1819 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1820 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1823 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1824 const struct net_device
*dev
,
1825 struct net
*src_net
, gfp_t gfp
)
1827 bool put_iflink
= false;
1829 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1830 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1832 if (!net_eq(dev_net(dev
), link_net
)) {
1833 int id
= peernet2id_alloc(src_net
, link_net
, gfp
);
1835 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1842 return nla_put_iflink(skb
, dev
, put_iflink
);
1845 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1846 const struct net_device
*dev
,
1847 u32 ext_filter_mask
)
1849 const struct rtnl_af_ops
*af_ops
;
1850 struct nlattr
*af_spec
;
1852 af_spec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
1856 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1860 if (!af_ops
->fill_link_af
)
1863 af
= nla_nest_start_noflag(skb
, af_ops
->family
);
1867 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1869 * Caller may return ENODATA to indicate that there
1870 * was no data to be dumped. This is not an error, it
1871 * means we should trim the attribute header and
1874 if (err
== -ENODATA
)
1875 nla_nest_cancel(skb
, af
);
1879 nla_nest_end(skb
, af
);
1882 nla_nest_end(skb
, af_spec
);
1886 static int rtnl_fill_alt_ifnames(struct sk_buff
*skb
,
1887 const struct net_device
*dev
)
1889 struct netdev_name_node
*name_node
;
1892 list_for_each_entry_rcu(name_node
, &dev
->name_node
->list
, list
) {
1893 if (nla_put_string(skb
, IFLA_ALT_IFNAME
, name_node
->name
))
1900 /* RCU protected. */
1901 static int rtnl_fill_prop_list(struct sk_buff
*skb
,
1902 const struct net_device
*dev
)
1904 struct nlattr
*prop_list
;
1907 prop_list
= nla_nest_start(skb
, IFLA_PROP_LIST
);
1911 ret
= rtnl_fill_alt_ifnames(skb
, dev
);
1915 nla_nest_end(skb
, prop_list
);
1919 nla_nest_cancel(skb
, prop_list
);
1923 static int rtnl_fill_proto_down(struct sk_buff
*skb
,
1924 const struct net_device
*dev
)
1929 if (nla_put_u8(skb
, IFLA_PROTO_DOWN
, READ_ONCE(dev
->proto_down
)))
1930 goto nla_put_failure
;
1932 preason
= READ_ONCE(dev
->proto_down_reason
);
1936 pr
= nla_nest_start(skb
, IFLA_PROTO_DOWN_REASON
);
1940 if (nla_put_u32(skb
, IFLA_PROTO_DOWN_REASON_VALUE
, preason
)) {
1941 nla_nest_cancel(skb
, pr
);
1942 goto nla_put_failure
;
1945 nla_nest_end(skb
, pr
);
1952 static int rtnl_fill_devlink_port(struct sk_buff
*skb
,
1953 const struct net_device
*dev
)
1955 struct nlattr
*devlink_port_nest
;
1958 devlink_port_nest
= nla_nest_start(skb
, IFLA_DEVLINK_PORT
);
1959 if (!devlink_port_nest
)
1962 if (dev
->devlink_port
) {
1963 ret
= devlink_nl_port_handle_fill(skb
, dev
->devlink_port
);
1968 nla_nest_end(skb
, devlink_port_nest
);
1972 nla_nest_cancel(skb
, devlink_port_nest
);
1976 static int rtnl_fill_dpll_pin(struct sk_buff
*skb
,
1977 const struct net_device
*dev
)
1979 struct nlattr
*dpll_pin_nest
;
1982 dpll_pin_nest
= nla_nest_start(skb
, IFLA_DPLL_PIN
);
1986 ret
= dpll_netdev_add_pin_handle(skb
, dev
);
1990 nla_nest_end(skb
, dpll_pin_nest
);
1994 nla_nest_cancel(skb
, dpll_pin_nest
);
1998 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1999 struct net_device
*dev
, struct net
*src_net
,
2000 int type
, u32 pid
, u32 seq
, u32 change
,
2001 unsigned int flags
, u32 ext_filter_mask
,
2002 u32 event
, int *new_nsid
, int new_ifindex
,
2003 int tgt_netnsid
, gfp_t gfp
)
2005 char devname
[IFNAMSIZ
];
2006 struct ifinfomsg
*ifm
;
2007 struct nlmsghdr
*nlh
;
2008 struct Qdisc
*qdisc
;
2011 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
2015 ifm
= nlmsg_data(nlh
);
2016 ifm
->ifi_family
= AF_UNSPEC
;
2018 ifm
->ifi_type
= READ_ONCE(dev
->type
);
2019 ifm
->ifi_index
= READ_ONCE(dev
->ifindex
);
2020 ifm
->ifi_flags
= dev_get_flags(dev
);
2021 ifm
->ifi_change
= change
;
2023 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_TARGET_NETNSID
, tgt_netnsid
))
2024 goto nla_put_failure
;
2026 netdev_copy_name(dev
, devname
);
2027 if (nla_put_string(skb
, IFLA_IFNAME
, devname
))
2028 goto nla_put_failure
;
2030 if (nla_put_u32(skb
, IFLA_TXQLEN
, READ_ONCE(dev
->tx_queue_len
)) ||
2031 nla_put_u8(skb
, IFLA_OPERSTATE
,
2032 netif_running(dev
) ? READ_ONCE(dev
->operstate
) :
2034 nla_put_u8(skb
, IFLA_LINKMODE
, READ_ONCE(dev
->link_mode
)) ||
2035 nla_put_u32(skb
, IFLA_MTU
, READ_ONCE(dev
->mtu
)) ||
2036 nla_put_u32(skb
, IFLA_MIN_MTU
, READ_ONCE(dev
->min_mtu
)) ||
2037 nla_put_u32(skb
, IFLA_MAX_MTU
, READ_ONCE(dev
->max_mtu
)) ||
2038 nla_put_u32(skb
, IFLA_GROUP
, READ_ONCE(dev
->group
)) ||
2039 nla_put_u32(skb
, IFLA_PROMISCUITY
, READ_ONCE(dev
->promiscuity
)) ||
2040 nla_put_u32(skb
, IFLA_ALLMULTI
, READ_ONCE(dev
->allmulti
)) ||
2041 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
,
2042 READ_ONCE(dev
->num_tx_queues
)) ||
2043 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
,
2044 READ_ONCE(dev
->gso_max_segs
)) ||
2045 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
,
2046 READ_ONCE(dev
->gso_max_size
)) ||
2047 nla_put_u32(skb
, IFLA_GRO_MAX_SIZE
,
2048 READ_ONCE(dev
->gro_max_size
)) ||
2049 nla_put_u32(skb
, IFLA_GSO_IPV4_MAX_SIZE
,
2050 READ_ONCE(dev
->gso_ipv4_max_size
)) ||
2051 nla_put_u32(skb
, IFLA_GRO_IPV4_MAX_SIZE
,
2052 READ_ONCE(dev
->gro_ipv4_max_size
)) ||
2053 nla_put_u32(skb
, IFLA_TSO_MAX_SIZE
,
2054 READ_ONCE(dev
->tso_max_size
)) ||
2055 nla_put_u32(skb
, IFLA_TSO_MAX_SEGS
,
2056 READ_ONCE(dev
->tso_max_segs
)) ||
2057 nla_put_uint(skb
, IFLA_MAX_PACING_OFFLOAD_HORIZON
,
2058 READ_ONCE(dev
->max_pacing_offload_horizon
)) ||
2060 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
,
2061 READ_ONCE(dev
->num_rx_queues
)) ||
2063 put_master_ifindex(skb
, dev
) ||
2064 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
2065 nla_put_ifalias(skb
, dev
) ||
2066 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
2067 atomic_read(&dev
->carrier_up_count
) +
2068 atomic_read(&dev
->carrier_down_count
)) ||
2069 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
2070 atomic_read(&dev
->carrier_up_count
)) ||
2071 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
2072 atomic_read(&dev
->carrier_down_count
)))
2073 goto nla_put_failure
;
2075 if (rtnl_fill_proto_down(skb
, dev
))
2076 goto nla_put_failure
;
2078 if (event
!= IFLA_EVENT_NONE
) {
2079 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
2080 goto nla_put_failure
;
2083 if (dev
->addr_len
) {
2084 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
2085 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
2086 goto nla_put_failure
;
2089 if (rtnl_phys_port_id_fill(skb
, dev
))
2090 goto nla_put_failure
;
2092 if (rtnl_phys_port_name_fill(skb
, dev
))
2093 goto nla_put_failure
;
2095 if (rtnl_phys_switch_id_fill(skb
, dev
))
2096 goto nla_put_failure
;
2098 if (rtnl_fill_stats(skb
, dev
))
2099 goto nla_put_failure
;
2101 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
2102 goto nla_put_failure
;
2104 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
2105 goto nla_put_failure
;
2107 if (rtnl_xdp_fill(skb
, dev
))
2108 goto nla_put_failure
;
2110 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
2111 if (rtnl_link_fill(skb
, dev
) < 0)
2112 goto nla_put_failure
;
2116 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
2117 goto nla_put_failure
;
2119 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
2120 goto nla_put_failure
;
2122 if (memchr_inv(dev
->perm_addr
, '\0', dev
->addr_len
) &&
2123 nla_put(skb
, IFLA_PERM_ADDRESS
, dev
->addr_len
, dev
->perm_addr
))
2124 goto nla_put_failure
;
2127 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
, GFP_ATOMIC
))
2128 goto nla_put_failure_rcu
;
2129 qdisc
= rcu_dereference(dev
->qdisc
);
2130 if (qdisc
&& nla_put_string(skb
, IFLA_QDISC
, qdisc
->ops
->id
))
2131 goto nla_put_failure_rcu
;
2132 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
2133 goto nla_put_failure_rcu
;
2134 if (rtnl_fill_link_ifmap(skb
, dev
))
2135 goto nla_put_failure_rcu
;
2136 if (rtnl_fill_prop_list(skb
, dev
))
2137 goto nla_put_failure_rcu
;
2140 if (dev
->dev
.parent
&&
2141 nla_put_string(skb
, IFLA_PARENT_DEV_NAME
,
2142 dev_name(dev
->dev
.parent
)))
2143 goto nla_put_failure
;
2145 if (dev
->dev
.parent
&& dev
->dev
.parent
->bus
&&
2146 nla_put_string(skb
, IFLA_PARENT_DEV_BUS_NAME
,
2147 dev
->dev
.parent
->bus
->name
))
2148 goto nla_put_failure
;
2150 if (rtnl_fill_devlink_port(skb
, dev
))
2151 goto nla_put_failure
;
2153 if (rtnl_fill_dpll_pin(skb
, dev
))
2154 goto nla_put_failure
;
2156 nlmsg_end(skb
, nlh
);
2159 nla_put_failure_rcu
:
2162 nlmsg_cancel(skb
, nlh
);
2166 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
2167 [IFLA_UNSPEC
] = { .strict_start_type
= IFLA_DPLL_PIN
},
2168 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
2169 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
2170 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
2171 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
2172 [IFLA_MTU
] = { .type
= NLA_U32
},
2173 [IFLA_LINK
] = { .type
= NLA_U32
},
2174 [IFLA_MASTER
] = { .type
= NLA_U32
},
2175 [IFLA_CARRIER
] = { .type
= NLA_U8
},
2176 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
2177 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
2178 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
2179 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
2180 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
2181 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
2182 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
2183 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
2184 * allow 0-length string (needed to remove an alias).
2186 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
2187 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
2188 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
2189 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
2190 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
2191 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
2192 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
2193 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
2194 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
2195 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
2196 [IFLA_GSO_MAX_SIZE
] = NLA_POLICY_MIN(NLA_U32
, MAX_TCP_HEADER
+ 1),
2197 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
2198 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
2199 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
2200 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
2201 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
2202 [IFLA_XDP
] = { .type
= NLA_NESTED
},
2203 [IFLA_EVENT
] = { .type
= NLA_U32
},
2204 [IFLA_GROUP
] = { .type
= NLA_U32
},
2205 [IFLA_TARGET_NETNSID
] = { .type
= NLA_S32
},
2206 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
2207 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
2208 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
2209 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
2210 [IFLA_PROP_LIST
] = { .type
= NLA_NESTED
},
2211 [IFLA_ALT_IFNAME
] = { .type
= NLA_STRING
,
2212 .len
= ALTIFNAMSIZ
- 1 },
2213 [IFLA_PERM_ADDRESS
] = { .type
= NLA_REJECT
},
2214 [IFLA_PROTO_DOWN_REASON
] = { .type
= NLA_NESTED
},
2215 [IFLA_NEW_IFINDEX
] = NLA_POLICY_MIN(NLA_S32
, 1),
2216 [IFLA_PARENT_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
2217 [IFLA_GRO_MAX_SIZE
] = { .type
= NLA_U32
},
2218 [IFLA_TSO_MAX_SIZE
] = { .type
= NLA_REJECT
},
2219 [IFLA_TSO_MAX_SEGS
] = { .type
= NLA_REJECT
},
2220 [IFLA_ALLMULTI
] = { .type
= NLA_REJECT
},
2221 [IFLA_GSO_IPV4_MAX_SIZE
] = NLA_POLICY_MIN(NLA_U32
, MAX_TCP_HEADER
+ 1),
2222 [IFLA_GRO_IPV4_MAX_SIZE
] = { .type
= NLA_U32
},
2225 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
2226 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
2227 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
2228 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
2229 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
2232 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
2233 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
2234 [IFLA_VF_BROADCAST
] = { .type
= NLA_REJECT
},
2235 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
2236 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
2237 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
2238 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
2239 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
2240 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
2241 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
2242 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
2243 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
2244 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
2245 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
2248 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
2249 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
2250 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
2251 .len
= PORT_PROFILE_MAX
},
2252 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
2253 .len
= PORT_UUID_MAX
},
2254 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
2255 .len
= PORT_UUID_MAX
},
2256 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
2257 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
2259 /* Unused, but we need to keep it here since user space could
2260 * fill it. It's also broken with regard to NLA_BINARY use in
2261 * combination with structs.
2263 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
2264 .len
= sizeof(struct ifla_port_vsi
) },
2267 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
2268 [IFLA_XDP_UNSPEC
] = { .strict_start_type
= IFLA_XDP_EXPECTED_FD
},
2269 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
2270 [IFLA_XDP_EXPECTED_FD
] = { .type
= NLA_S32
},
2271 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
2272 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
2273 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
2276 static struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
,
2277 int *ops_srcu_index
)
2279 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
2280 struct rtnl_link_ops
*ops
= NULL
;
2282 if (nla_parse_nested_deprecated(linfo
, IFLA_INFO_MAX
, nla
, ifla_info_policy
, NULL
) < 0)
2285 if (linfo
[IFLA_INFO_KIND
]) {
2286 char kind
[MODULE_NAME_LEN
];
2288 nla_strscpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
2289 ops
= rtnl_link_ops_get(kind
, ops_srcu_index
);
2295 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
2297 struct net_device
*master
;
2302 master
= netdev_master_upper_dev_get(dev
);
2304 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2305 * another invalid value for ifindex to denote "no master".
2307 if (master_idx
== -1)
2310 if (!master
|| master
->ifindex
!= master_idx
)
2316 static bool link_kind_filtered(const struct net_device
*dev
,
2317 const struct rtnl_link_ops
*kind_ops
)
2319 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
2325 static bool link_dump_filtered(struct net_device
*dev
,
2327 const struct rtnl_link_ops
*kind_ops
)
2329 if (link_master_filtered(dev
, master_idx
) ||
2330 link_kind_filtered(dev
, kind_ops
))
2337 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2338 * @sk: netlink socket
2339 * @netnsid: network namespace identifier
2341 * Returns the network namespace identified by netnsid on success or an error
2342 * pointer on failure.
2344 struct net
*rtnl_get_net_ns_capable(struct sock
*sk
, int netnsid
)
2348 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
2350 return ERR_PTR(-EINVAL
);
2352 /* For now, the caller is required to have CAP_NET_ADMIN in
2353 * the user namespace owning the target net ns.
2355 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
2357 return ERR_PTR(-EACCES
);
2361 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable
);
2363 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr
*nlh
,
2364 bool strict_check
, struct nlattr
**tb
,
2365 struct netlink_ext_ack
*extack
)
2370 struct ifinfomsg
*ifm
;
2372 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
2373 NL_SET_ERR_MSG(extack
, "Invalid header for link dump");
2377 ifm
= nlmsg_data(nlh
);
2378 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
2380 NL_SET_ERR_MSG(extack
, "Invalid values in header for link dump request");
2383 if (ifm
->ifi_index
) {
2384 NL_SET_ERR_MSG(extack
, "Filter by device index not supported for link dumps");
2388 return nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
,
2389 IFLA_MAX
, ifla_policy
,
2393 /* A hack to preserve kernel<->userspace interface.
2394 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2395 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2396 * what iproute2 < v3.9.0 used.
2397 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2398 * attribute, its netlink message is shorter than struct ifinfomsg.
2400 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2401 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2403 return nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
,
2407 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2409 struct netlink_ext_ack
*extack
= cb
->extack
;
2410 struct rtnl_link_ops
*kind_ops
= NULL
;
2411 const struct nlmsghdr
*nlh
= cb
->nlh
;
2412 struct net
*net
= sock_net(skb
->sk
);
2413 unsigned int flags
= NLM_F_MULTI
;
2414 struct nlattr
*tb
[IFLA_MAX
+1];
2416 unsigned long ifindex
;
2417 } *ctx
= (void *)cb
->ctx
;
2418 struct net
*tgt_net
= net
;
2419 u32 ext_filter_mask
= 0;
2420 struct net_device
*dev
;
2426 err
= rtnl_valid_dump_ifinfo_req(nlh
, cb
->strict_check
, tb
, extack
);
2428 if (cb
->strict_check
)
2434 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
2438 /* new attributes should only be added with strict checking */
2440 case IFLA_TARGET_NETNSID
:
2441 netnsid
= nla_get_s32(tb
[i
]);
2442 tgt_net
= rtnl_get_net_ns_capable(skb
->sk
, netnsid
);
2443 if (IS_ERR(tgt_net
)) {
2444 NL_SET_ERR_MSG(extack
, "Invalid target network namespace id");
2445 err
= PTR_ERR(tgt_net
);
2451 ext_filter_mask
= nla_get_u32(tb
[i
]);
2454 master_idx
= nla_get_u32(tb
[i
]);
2457 kind_ops
= linkinfo_to_kind_ops(tb
[i
], &ops_srcu_index
);
2460 if (cb
->strict_check
) {
2461 NL_SET_ERR_MSG(extack
, "Unsupported attribute in link dump request");
2468 if (master_idx
|| kind_ops
)
2469 flags
|= NLM_F_DUMP_FILTERED
;
2473 for_each_netdev_dump(tgt_net
, dev
, ctx
->ifindex
) {
2474 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
2476 err
= rtnl_fill_ifinfo(skb
, dev
, net
, RTM_NEWLINK
,
2477 NETLINK_CB(cb
->skb
).portid
,
2478 nlh
->nlmsg_seq
, 0, flags
,
2479 ext_filter_mask
, 0, NULL
, 0,
2480 netnsid
, GFP_KERNEL
);
2486 cb
->seq
= tgt_net
->dev_base_seq
;
2487 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2492 rtnl_link_ops_put(kind_ops
, ops_srcu_index
);
2499 int rtnl_nla_parse_ifinfomsg(struct nlattr
**tb
, const struct nlattr
*nla_peer
,
2500 struct netlink_ext_ack
*exterr
)
2502 const struct ifinfomsg
*ifmp
;
2503 const struct nlattr
*attrs
;
2506 ifmp
= nla_data(nla_peer
);
2507 attrs
= nla_data(nla_peer
) + sizeof(struct ifinfomsg
);
2508 len
= nla_len(nla_peer
) - sizeof(struct ifinfomsg
);
2510 if (ifmp
->ifi_index
< 0) {
2511 NL_SET_ERR_MSG_ATTR(exterr
, nla_peer
,
2512 "ifindex can't be negative");
2516 return nla_parse_deprecated(tb
, IFLA_MAX
, attrs
, len
, ifla_policy
,
2519 EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg
);
2521 static struct net
*rtnl_link_get_net_ifla(struct nlattr
*tb
[])
2523 struct net
*net
= NULL
;
2525 /* Examine the link attributes and figure out which
2526 * network namespace we are talking about.
2528 if (tb
[IFLA_NET_NS_PID
])
2529 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
2530 else if (tb
[IFLA_NET_NS_FD
])
2531 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
2536 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
2538 struct net
*net
= rtnl_link_get_net_ifla(tb
);
2541 net
= get_net(src_net
);
2545 EXPORT_SYMBOL(rtnl_link_get_net
);
2547 /* Figure out which network namespace we are talking about by
2548 * examining the link attributes in the following order:
2550 * 1. IFLA_NET_NS_PID
2552 * 3. IFLA_TARGET_NETNSID
2554 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
2555 struct nlattr
*tb
[])
2559 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
2560 return rtnl_link_get_net(src_net
, tb
);
2562 if (!tb
[IFLA_TARGET_NETNSID
])
2563 return get_net(src_net
);
2565 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_TARGET_NETNSID
]));
2567 return ERR_PTR(-EINVAL
);
2572 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2573 struct net
*src_net
,
2574 struct nlattr
*tb
[], int cap
)
2578 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2582 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2584 return ERR_PTR(-EPERM
);
2590 /* Verify that rtnetlink requests do not pass additional properties
2591 * potentially referring to different network namespaces.
2593 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2594 struct netlink_ext_ack
*extack
,
2598 if (netns_id_only
) {
2599 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2602 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2606 if (tb
[IFLA_TARGET_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2609 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2612 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2618 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2622 static int rtnl_set_vf_rate(struct net_device
*dev
, int vf
, int min_tx_rate
,
2625 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2627 if (!ops
->ndo_set_vf_rate
)
2629 if (max_tx_rate
&& max_tx_rate
< min_tx_rate
)
2632 return ops
->ndo_set_vf_rate(dev
, vf
, min_tx_rate
, max_tx_rate
);
2635 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[],
2636 struct netlink_ext_ack
*extack
)
2638 if (tb
[IFLA_ADDRESS
] &&
2639 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2642 if (tb
[IFLA_BROADCAST
] &&
2643 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2646 if (tb
[IFLA_GSO_MAX_SIZE
] &&
2647 nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]) > dev
->tso_max_size
) {
2648 NL_SET_ERR_MSG(extack
, "too big gso_max_size");
2652 if (tb
[IFLA_GSO_MAX_SEGS
] &&
2653 (nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]) > GSO_MAX_SEGS
||
2654 nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]) > dev
->tso_max_segs
)) {
2655 NL_SET_ERR_MSG(extack
, "too big gso_max_segs");
2659 if (tb
[IFLA_GRO_MAX_SIZE
] &&
2660 nla_get_u32(tb
[IFLA_GRO_MAX_SIZE
]) > GRO_MAX_SIZE
) {
2661 NL_SET_ERR_MSG(extack
, "too big gro_max_size");
2665 if (tb
[IFLA_GSO_IPV4_MAX_SIZE
] &&
2666 nla_get_u32(tb
[IFLA_GSO_IPV4_MAX_SIZE
]) > dev
->tso_max_size
) {
2667 NL_SET_ERR_MSG(extack
, "too big gso_ipv4_max_size");
2671 if (tb
[IFLA_GRO_IPV4_MAX_SIZE
] &&
2672 nla_get_u32(tb
[IFLA_GRO_IPV4_MAX_SIZE
]) > GRO_MAX_SIZE
) {
2673 NL_SET_ERR_MSG(extack
, "too big gro_ipv4_max_size");
2677 if (tb
[IFLA_AF_SPEC
]) {
2681 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2682 struct rtnl_af_ops
*af_ops
;
2683 int af_ops_srcu_index
;
2685 af_ops
= rtnl_af_lookup(nla_type(af
), &af_ops_srcu_index
);
2687 return -EAFNOSUPPORT
;
2689 if (!af_ops
->set_link_af
)
2691 else if (af_ops
->validate_link_af
)
2692 err
= af_ops
->validate_link_af(dev
, af
, extack
);
2696 rtnl_af_put(af_ops
, af_ops_srcu_index
);
2706 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2709 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2711 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2714 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2716 if (dev
->type
!= ARPHRD_INFINIBAND
)
2719 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2722 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2724 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2727 if (tb
[IFLA_VF_MAC
]) {
2728 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2730 if (ivm
->vf
>= INT_MAX
)
2733 if (ops
->ndo_set_vf_mac
)
2734 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2740 if (tb
[IFLA_VF_VLAN
]) {
2741 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2743 if (ivv
->vf
>= INT_MAX
)
2746 if (ops
->ndo_set_vf_vlan
)
2747 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2749 htons(ETH_P_8021Q
));
2754 if (tb
[IFLA_VF_VLAN_LIST
]) {
2755 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2756 struct nlattr
*attr
;
2760 if (!ops
->ndo_set_vf_vlan
)
2763 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2764 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2765 nla_len(attr
) < sizeof(struct ifla_vf_vlan_info
)) {
2768 if (len
>= MAX_VLAN_LIST_LEN
)
2770 ivvl
[len
] = nla_data(attr
);
2777 if (ivvl
[0]->vf
>= INT_MAX
)
2779 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2780 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2785 if (tb
[IFLA_VF_TX_RATE
]) {
2786 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2787 struct ifla_vf_info ivf
;
2789 if (ivt
->vf
>= INT_MAX
)
2792 if (ops
->ndo_get_vf_config
)
2793 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2797 err
= rtnl_set_vf_rate(dev
, ivt
->vf
,
2798 ivf
.min_tx_rate
, ivt
->rate
);
2803 if (tb
[IFLA_VF_RATE
]) {
2804 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2806 if (ivt
->vf
>= INT_MAX
)
2809 err
= rtnl_set_vf_rate(dev
, ivt
->vf
,
2810 ivt
->min_tx_rate
, ivt
->max_tx_rate
);
2815 if (tb
[IFLA_VF_SPOOFCHK
]) {
2816 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2818 if (ivs
->vf
>= INT_MAX
)
2821 if (ops
->ndo_set_vf_spoofchk
)
2822 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2828 if (tb
[IFLA_VF_LINK_STATE
]) {
2829 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2831 if (ivl
->vf
>= INT_MAX
)
2834 if (ops
->ndo_set_vf_link_state
)
2835 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2841 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2842 struct ifla_vf_rss_query_en
*ivrssq_en
;
2845 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2846 if (ivrssq_en
->vf
>= INT_MAX
)
2848 if (ops
->ndo_set_vf_rss_query_en
)
2849 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2850 ivrssq_en
->setting
);
2855 if (tb
[IFLA_VF_TRUST
]) {
2856 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2858 if (ivt
->vf
>= INT_MAX
)
2861 if (ops
->ndo_set_vf_trust
)
2862 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2867 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2868 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2870 if (ivt
->vf
>= INT_MAX
)
2872 if (!ops
->ndo_set_vf_guid
)
2874 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2877 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2878 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2880 if (ivt
->vf
>= INT_MAX
)
2882 if (!ops
->ndo_set_vf_guid
)
2885 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2891 static int do_set_master(struct net_device
*dev
, int ifindex
,
2892 struct netlink_ext_ack
*extack
)
2894 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2895 const struct net_device_ops
*ops
;
2899 if (upper_dev
->ifindex
== ifindex
)
2901 ops
= upper_dev
->netdev_ops
;
2902 if (ops
->ndo_del_slave
) {
2903 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2912 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2915 ops
= upper_dev
->netdev_ops
;
2916 if (ops
->ndo_add_slave
) {
2917 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2927 static const struct nla_policy ifla_proto_down_reason_policy
[IFLA_PROTO_DOWN_REASON_VALUE
+ 1] = {
2928 [IFLA_PROTO_DOWN_REASON_MASK
] = { .type
= NLA_U32
},
2929 [IFLA_PROTO_DOWN_REASON_VALUE
] = { .type
= NLA_U32
},
2932 static int do_set_proto_down(struct net_device
*dev
,
2933 struct nlattr
*nl_proto_down
,
2934 struct nlattr
*nl_proto_down_reason
,
2935 struct netlink_ext_ack
*extack
)
2937 struct nlattr
*pdreason
[IFLA_PROTO_DOWN_REASON_MAX
+ 1];
2938 unsigned long mask
= 0;
2943 if (!dev
->change_proto_down
) {
2944 NL_SET_ERR_MSG(extack
, "Protodown not supported by device");
2948 if (nl_proto_down_reason
) {
2949 err
= nla_parse_nested_deprecated(pdreason
,
2950 IFLA_PROTO_DOWN_REASON_MAX
,
2951 nl_proto_down_reason
,
2952 ifla_proto_down_reason_policy
,
2957 if (!pdreason
[IFLA_PROTO_DOWN_REASON_VALUE
]) {
2958 NL_SET_ERR_MSG(extack
, "Invalid protodown reason value");
2962 value
= nla_get_u32(pdreason
[IFLA_PROTO_DOWN_REASON_VALUE
]);
2964 if (pdreason
[IFLA_PROTO_DOWN_REASON_MASK
])
2965 mask
= nla_get_u32(pdreason
[IFLA_PROTO_DOWN_REASON_MASK
]);
2967 dev_change_proto_down_reason(dev
, mask
, value
);
2970 if (nl_proto_down
) {
2971 proto_down
= nla_get_u8(nl_proto_down
);
2973 /* Don't turn off protodown if there are active reasons */
2974 if (!proto_down
&& dev
->proto_down_reason
) {
2975 NL_SET_ERR_MSG(extack
, "Cannot clear protodown, active reasons");
2978 err
= dev_change_proto_down(dev
,
2987 #define DO_SETLINK_MODIFIED 0x01
2988 /* notify flag means notify + modified. */
2989 #define DO_SETLINK_NOTIFY 0x03
2990 static int do_setlink(const struct sk_buff
*skb
, struct net_device
*dev
,
2991 struct net
*tgt_net
, struct ifinfomsg
*ifm
,
2992 struct netlink_ext_ack
*extack
,
2993 struct nlattr
**tb
, int status
)
2995 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2996 char ifname
[IFNAMSIZ
];
2999 err
= validate_linkmsg(dev
, tb
, extack
);
3003 if (tb
[IFLA_IFNAME
])
3004 nla_strscpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3008 if (!net_eq(tgt_net
, dev_net(dev
))) {
3009 const char *pat
= ifname
[0] ? ifname
: NULL
;
3012 new_ifindex
= nla_get_s32_default(tb
[IFLA_NEW_IFINDEX
], 0);
3014 err
= __dev_change_net_namespace(dev
, tgt_net
, pat
, new_ifindex
);
3018 status
|= DO_SETLINK_MODIFIED
;
3022 struct rtnl_link_ifmap
*u_map
;
3025 if (!ops
->ndo_set_config
) {
3030 if (!netif_device_present(dev
)) {
3035 u_map
= nla_data(tb
[IFLA_MAP
]);
3036 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
3037 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
3038 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
3039 k_map
.irq
= (unsigned char) u_map
->irq
;
3040 k_map
.dma
= (unsigned char) u_map
->dma
;
3041 k_map
.port
= (unsigned char) u_map
->port
;
3043 err
= ops
->ndo_set_config(dev
, &k_map
);
3047 status
|= DO_SETLINK_NOTIFY
;
3050 if (tb
[IFLA_ADDRESS
]) {
3051 struct sockaddr
*sa
;
3054 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
3056 sa
= kmalloc(len
, GFP_KERNEL
);
3061 sa
->sa_family
= dev
->type
;
3062 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
3064 err
= dev_set_mac_address_user(dev
, sa
, extack
);
3068 status
|= DO_SETLINK_MODIFIED
;
3072 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
3075 status
|= DO_SETLINK_MODIFIED
;
3078 if (tb
[IFLA_GROUP
]) {
3079 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
3080 status
|= DO_SETLINK_NOTIFY
;
3084 * Interface selected by interface index but interface
3085 * name provided implies that a name change has been
3088 if (ifm
->ifi_index
> 0 && ifname
[0]) {
3089 err
= dev_change_name(dev
, ifname
);
3092 status
|= DO_SETLINK_MODIFIED
;
3095 if (tb
[IFLA_IFALIAS
]) {
3096 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
3097 nla_len(tb
[IFLA_IFALIAS
]));
3100 status
|= DO_SETLINK_NOTIFY
;
3103 if (tb
[IFLA_BROADCAST
]) {
3104 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
3105 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
3108 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
3109 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
3115 if (tb
[IFLA_MASTER
]) {
3116 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
3119 status
|= DO_SETLINK_MODIFIED
;
3122 if (tb
[IFLA_CARRIER
]) {
3123 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
3126 status
|= DO_SETLINK_MODIFIED
;
3129 if (tb
[IFLA_TXQLEN
]) {
3130 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
3132 err
= dev_change_tx_queue_len(dev
, value
);
3135 status
|= DO_SETLINK_MODIFIED
;
3138 if (tb
[IFLA_GSO_MAX_SIZE
]) {
3139 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
3141 if (dev
->gso_max_size
^ max_size
) {
3142 netif_set_gso_max_size(dev
, max_size
);
3143 status
|= DO_SETLINK_MODIFIED
;
3147 if (tb
[IFLA_GSO_MAX_SEGS
]) {
3148 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
3150 if (dev
->gso_max_segs
^ max_segs
) {
3151 netif_set_gso_max_segs(dev
, max_segs
);
3152 status
|= DO_SETLINK_MODIFIED
;
3156 if (tb
[IFLA_GRO_MAX_SIZE
]) {
3157 u32 gro_max_size
= nla_get_u32(tb
[IFLA_GRO_MAX_SIZE
]);
3159 if (dev
->gro_max_size
^ gro_max_size
) {
3160 netif_set_gro_max_size(dev
, gro_max_size
);
3161 status
|= DO_SETLINK_MODIFIED
;
3165 if (tb
[IFLA_GSO_IPV4_MAX_SIZE
]) {
3166 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_IPV4_MAX_SIZE
]);
3168 if (dev
->gso_ipv4_max_size
^ max_size
) {
3169 netif_set_gso_ipv4_max_size(dev
, max_size
);
3170 status
|= DO_SETLINK_MODIFIED
;
3174 if (tb
[IFLA_GRO_IPV4_MAX_SIZE
]) {
3175 u32 gro_max_size
= nla_get_u32(tb
[IFLA_GRO_IPV4_MAX_SIZE
]);
3177 if (dev
->gro_ipv4_max_size
^ gro_max_size
) {
3178 netif_set_gro_ipv4_max_size(dev
, gro_max_size
);
3179 status
|= DO_SETLINK_MODIFIED
;
3183 if (tb
[IFLA_OPERSTATE
])
3184 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
3186 if (tb
[IFLA_LINKMODE
]) {
3187 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
3189 if (dev
->link_mode
^ value
)
3190 status
|= DO_SETLINK_NOTIFY
;
3191 WRITE_ONCE(dev
->link_mode
, value
);
3194 if (tb
[IFLA_VFINFO_LIST
]) {
3195 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
3196 struct nlattr
*attr
;
3199 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
3200 if (nla_type(attr
) != IFLA_VF_INFO
||
3201 nla_len(attr
) < NLA_HDRLEN
) {
3205 err
= nla_parse_nested_deprecated(vfinfo
, IFLA_VF_MAX
,
3211 err
= do_setvfinfo(dev
, vfinfo
);
3214 status
|= DO_SETLINK_NOTIFY
;
3219 if (tb
[IFLA_VF_PORTS
]) {
3220 struct nlattr
*port
[IFLA_PORT_MAX
+1];
3221 struct nlattr
*attr
;
3226 if (!ops
->ndo_set_vf_port
)
3229 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
3230 if (nla_type(attr
) != IFLA_VF_PORT
||
3231 nla_len(attr
) < NLA_HDRLEN
) {
3235 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
3241 if (!port
[IFLA_PORT_VF
]) {
3245 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
3246 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
3249 status
|= DO_SETLINK_NOTIFY
;
3254 if (tb
[IFLA_PORT_SELF
]) {
3255 struct nlattr
*port
[IFLA_PORT_MAX
+1];
3257 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
3259 ifla_port_policy
, NULL
);
3264 if (ops
->ndo_set_vf_port
)
3265 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
3268 status
|= DO_SETLINK_NOTIFY
;
3271 if (tb
[IFLA_AF_SPEC
]) {
3275 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
3276 struct rtnl_af_ops
*af_ops
;
3277 int af_ops_srcu_index
;
3279 af_ops
= rtnl_af_lookup(nla_type(af
), &af_ops_srcu_index
);
3281 err
= -EAFNOSUPPORT
;
3285 err
= af_ops
->set_link_af(dev
, af
, extack
);
3286 rtnl_af_put(af_ops
, af_ops_srcu_index
);
3291 status
|= DO_SETLINK_NOTIFY
;
3296 if (tb
[IFLA_PROTO_DOWN
] || tb
[IFLA_PROTO_DOWN_REASON
]) {
3297 err
= do_set_proto_down(dev
, tb
[IFLA_PROTO_DOWN
],
3298 tb
[IFLA_PROTO_DOWN_REASON
], extack
);
3301 status
|= DO_SETLINK_NOTIFY
;
3305 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
3308 err
= nla_parse_nested_deprecated(xdp
, IFLA_XDP_MAX
,
3310 ifla_xdp_policy
, NULL
);
3314 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
3319 if (xdp
[IFLA_XDP_FLAGS
]) {
3320 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
3321 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
3325 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
3331 if (xdp
[IFLA_XDP_FD
]) {
3332 int expected_fd
= -1;
3334 if (xdp_flags
& XDP_FLAGS_REPLACE
) {
3335 if (!xdp
[IFLA_XDP_EXPECTED_FD
]) {
3340 nla_get_s32(xdp
[IFLA_XDP_EXPECTED_FD
]);
3343 err
= dev_change_xdp_fd(dev
, extack
,
3344 nla_get_s32(xdp
[IFLA_XDP_FD
]),
3349 status
|= DO_SETLINK_NOTIFY
;
3354 if (status
& DO_SETLINK_MODIFIED
) {
3355 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
3356 netdev_state_change(dev
);
3359 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",
3366 static struct net_device
*rtnl_dev_get(struct net
*net
,
3367 struct nlattr
*tb
[])
3369 char ifname
[ALTIFNAMSIZ
];
3371 if (tb
[IFLA_IFNAME
])
3372 nla_strscpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3373 else if (tb
[IFLA_ALT_IFNAME
])
3374 nla_strscpy(ifname
, tb
[IFLA_ALT_IFNAME
], ALTIFNAMSIZ
);
3378 return __dev_get_by_name(net
, ifname
);
3381 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3382 struct netlink_ext_ack
*extack
)
3384 struct ifinfomsg
*ifm
= nlmsg_data(nlh
);
3385 struct net
*net
= sock_net(skb
->sk
);
3386 struct nlattr
*tb
[IFLA_MAX
+1];
3387 struct net_device
*dev
= NULL
;
3388 struct rtnl_nets rtnl_nets
;
3389 struct net
*tgt_net
;
3392 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3393 ifla_policy
, extack
);
3397 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
3401 tgt_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3402 if (IS_ERR(tgt_net
)) {
3403 err
= PTR_ERR(tgt_net
);
3407 rtnl_nets_init(&rtnl_nets
);
3408 rtnl_nets_add(&rtnl_nets
, get_net(net
));
3409 rtnl_nets_add(&rtnl_nets
, tgt_net
);
3411 rtnl_nets_lock(&rtnl_nets
);
3413 if (ifm
->ifi_index
> 0)
3414 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3415 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3416 dev
= rtnl_dev_get(net
, tb
);
3421 err
= do_setlink(skb
, dev
, tgt_net
, ifm
, extack
, tb
, 0);
3425 rtnl_nets_unlock(&rtnl_nets
);
3430 static int rtnl_group_dellink(const struct net
*net
, int group
)
3432 struct net_device
*dev
, *aux
;
3433 LIST_HEAD(list_kill
);
3439 for_each_netdev(net
, dev
) {
3440 if (dev
->group
== group
) {
3441 const struct rtnl_link_ops
*ops
;
3444 ops
= dev
->rtnl_link_ops
;
3445 if (!ops
|| !ops
->dellink
)
3453 for_each_netdev_safe(net
, dev
, aux
) {
3454 if (dev
->group
== group
) {
3455 const struct rtnl_link_ops
*ops
;
3457 ops
= dev
->rtnl_link_ops
;
3458 ops
->dellink(dev
, &list_kill
);
3461 unregister_netdevice_many(&list_kill
);
3466 int rtnl_delete_link(struct net_device
*dev
, u32 portid
, const struct nlmsghdr
*nlh
)
3468 const struct rtnl_link_ops
*ops
;
3469 LIST_HEAD(list_kill
);
3471 ops
= dev
->rtnl_link_ops
;
3472 if (!ops
|| !ops
->dellink
)
3475 ops
->dellink(dev
, &list_kill
);
3476 unregister_netdevice_many_notify(&list_kill
, portid
, nlh
);
3480 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
3482 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3483 struct netlink_ext_ack
*extack
)
3485 struct ifinfomsg
*ifm
= nlmsg_data(nlh
);
3486 struct net
*net
= sock_net(skb
->sk
);
3487 u32 portid
= NETLINK_CB(skb
).portid
;
3488 struct nlattr
*tb
[IFLA_MAX
+1];
3489 struct net_device
*dev
= NULL
;
3490 struct net
*tgt_net
= net
;
3494 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3495 ifla_policy
, extack
);
3499 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3503 if (tb
[IFLA_TARGET_NETNSID
]) {
3504 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3505 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3506 if (IS_ERR(tgt_net
))
3507 return PTR_ERR(tgt_net
);
3510 rtnl_net_lock(tgt_net
);
3512 if (ifm
->ifi_index
> 0)
3513 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3514 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3515 dev
= rtnl_dev_get(tgt_net
, tb
);
3518 err
= rtnl_delete_link(dev
, portid
, nlh
);
3519 else if (ifm
->ifi_index
> 0 || tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3521 else if (tb
[IFLA_GROUP
])
3522 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
3526 rtnl_net_unlock(tgt_net
);
3534 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
,
3535 u32 portid
, const struct nlmsghdr
*nlh
)
3537 unsigned int old_flags
;
3540 old_flags
= dev
->flags
;
3541 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
3542 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
3548 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
3549 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
), portid
, nlh
);
3551 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
3552 __dev_notify_flags(dev
, old_flags
, ~0U, portid
, nlh
);
3556 EXPORT_SYMBOL(rtnl_configure_link
);
3558 struct net_device
*rtnl_create_link(struct net
*net
, const char *ifname
,
3559 unsigned char name_assign_type
,
3560 const struct rtnl_link_ops
*ops
,
3561 struct nlattr
*tb
[],
3562 struct netlink_ext_ack
*extack
)
3564 struct net_device
*dev
;
3565 unsigned int num_tx_queues
= 1;
3566 unsigned int num_rx_queues
= 1;
3569 if (tb
[IFLA_NUM_TX_QUEUES
])
3570 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
3571 else if (ops
->get_num_tx_queues
)
3572 num_tx_queues
= ops
->get_num_tx_queues();
3574 if (tb
[IFLA_NUM_RX_QUEUES
])
3575 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
3576 else if (ops
->get_num_rx_queues
)
3577 num_rx_queues
= ops
->get_num_rx_queues();
3579 if (num_tx_queues
< 1 || num_tx_queues
> 4096) {
3580 NL_SET_ERR_MSG(extack
, "Invalid number of transmit queues");
3581 return ERR_PTR(-EINVAL
);
3584 if (num_rx_queues
< 1 || num_rx_queues
> 4096) {
3585 NL_SET_ERR_MSG(extack
, "Invalid number of receive queues");
3586 return ERR_PTR(-EINVAL
);
3590 dev
= ops
->alloc(tb
, ifname
, name_assign_type
,
3591 num_tx_queues
, num_rx_queues
);
3595 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
,
3596 name_assign_type
, ops
->setup
,
3597 num_tx_queues
, num_rx_queues
);
3601 return ERR_PTR(-ENOMEM
);
3603 err
= validate_linkmsg(dev
, tb
, extack
);
3606 return ERR_PTR(err
);
3609 dev_net_set(dev
, net
);
3610 dev
->rtnl_link_ops
= ops
;
3611 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
3614 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3616 err
= dev_validate_mtu(dev
, mtu
, extack
);
3619 return ERR_PTR(err
);
3623 if (tb
[IFLA_ADDRESS
]) {
3624 __dev_addr_set(dev
, nla_data(tb
[IFLA_ADDRESS
]),
3625 nla_len(tb
[IFLA_ADDRESS
]));
3626 dev
->addr_assign_type
= NET_ADDR_SET
;
3628 if (tb
[IFLA_BROADCAST
])
3629 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
3630 nla_len(tb
[IFLA_BROADCAST
]));
3631 if (tb
[IFLA_TXQLEN
])
3632 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
3633 if (tb
[IFLA_OPERSTATE
])
3634 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
3635 if (tb
[IFLA_LINKMODE
])
3636 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
3638 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
3639 if (tb
[IFLA_GSO_MAX_SIZE
])
3640 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
3641 if (tb
[IFLA_GSO_MAX_SEGS
])
3642 netif_set_gso_max_segs(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]));
3643 if (tb
[IFLA_GRO_MAX_SIZE
])
3644 netif_set_gro_max_size(dev
, nla_get_u32(tb
[IFLA_GRO_MAX_SIZE
]));
3645 if (tb
[IFLA_GSO_IPV4_MAX_SIZE
])
3646 netif_set_gso_ipv4_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_IPV4_MAX_SIZE
]));
3647 if (tb
[IFLA_GRO_IPV4_MAX_SIZE
])
3648 netif_set_gro_ipv4_max_size(dev
, nla_get_u32(tb
[IFLA_GRO_IPV4_MAX_SIZE
]));
3652 EXPORT_SYMBOL(rtnl_create_link
);
3654 struct rtnl_newlink_tbs
{
3655 struct nlattr
*tb
[IFLA_MAX
+ 1];
3656 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
3657 struct nlattr
*attr
[RTNL_MAX_TYPE
+ 1];
3658 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
3661 static int rtnl_changelink(const struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3662 const struct rtnl_link_ops
*ops
,
3663 struct net_device
*dev
, struct net
*tgt_net
,
3664 struct rtnl_newlink_tbs
*tbs
,
3665 struct nlattr
**data
,
3666 struct netlink_ext_ack
*extack
)
3668 struct nlattr
** const linkinfo
= tbs
->linkinfo
;
3669 struct nlattr
** const tb
= tbs
->tb
;
3673 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3676 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3679 if (linkinfo
[IFLA_INFO_DATA
]) {
3680 if (!ops
|| ops
!= dev
->rtnl_link_ops
|| !ops
->changelink
)
3683 err
= ops
->changelink(dev
, tb
, data
, extack
);
3687 status
|= DO_SETLINK_NOTIFY
;
3690 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3691 const struct rtnl_link_ops
*m_ops
= NULL
;
3692 struct nlattr
**slave_data
= NULL
;
3693 struct net_device
*master_dev
;
3695 master_dev
= netdev_master_upper_dev_get(dev
);
3697 m_ops
= master_dev
->rtnl_link_ops
;
3699 if (!m_ops
|| !m_ops
->slave_changelink
)
3702 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3705 if (m_ops
->slave_maxtype
) {
3706 err
= nla_parse_nested_deprecated(tbs
->slave_attr
,
3707 m_ops
->slave_maxtype
,
3708 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3709 m_ops
->slave_policy
, extack
);
3713 slave_data
= tbs
->slave_attr
;
3716 err
= m_ops
->slave_changelink(master_dev
, dev
, tb
, slave_data
, extack
);
3720 status
|= DO_SETLINK_NOTIFY
;
3723 return do_setlink(skb
, dev
, tgt_net
, nlmsg_data(nlh
), extack
, tb
, status
);
3726 static int rtnl_group_changelink(const struct sk_buff
*skb
,
3727 struct net
*net
, struct net
*tgt_net
,
3728 int group
, struct ifinfomsg
*ifm
,
3729 struct netlink_ext_ack
*extack
,
3732 struct net_device
*dev
, *aux
;
3735 for_each_netdev_safe(net
, dev
, aux
) {
3736 if (dev
->group
== group
) {
3737 err
= do_setlink(skb
, dev
, tgt_net
, ifm
, extack
, tb
, 0);
3746 static int rtnl_newlink_create(struct sk_buff
*skb
, struct ifinfomsg
*ifm
,
3747 const struct rtnl_link_ops
*ops
,
3748 struct net
*tgt_net
, struct net
*link_net
,
3749 struct net
*peer_net
,
3750 const struct nlmsghdr
*nlh
,
3751 struct nlattr
**tb
, struct nlattr
**data
,
3752 struct netlink_ext_ack
*extack
)
3754 unsigned char name_assign_type
= NET_NAME_USER
;
3755 struct net
*net
= sock_net(skb
->sk
);
3756 u32 portid
= NETLINK_CB(skb
).portid
;
3757 struct net_device
*dev
;
3758 char ifname
[IFNAMSIZ
];
3761 if (!ops
->alloc
&& !ops
->setup
)
3764 if (tb
[IFLA_IFNAME
]) {
3765 nla_strscpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3767 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3768 name_assign_type
= NET_NAME_ENUM
;
3771 dev
= rtnl_create_link(link_net
? : tgt_net
, ifname
,
3772 name_assign_type
, ops
, tb
, extack
);
3778 dev
->ifindex
= ifm
->ifi_index
;
3786 err
= ops
->newlink(net
, dev
, tb
, data
, extack
);
3788 err
= register_netdevice(dev
);
3794 err
= rtnl_configure_link(dev
, ifm
, portid
, nlh
);
3796 goto out_unregister
;
3798 err
= dev_change_net_namespace(dev
, tgt_net
, ifname
);
3800 goto out_unregister
;
3802 if (tb
[IFLA_MASTER
]) {
3803 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
3805 goto out_unregister
;
3811 LIST_HEAD(list_kill
);
3813 ops
->dellink(dev
, &list_kill
);
3814 unregister_netdevice_many(&list_kill
);
3816 unregister_netdevice(dev
);
3821 static struct net
*rtnl_get_peer_net(const struct rtnl_link_ops
*ops
,
3822 struct nlattr
*data
[],
3823 struct netlink_ext_ack
*extack
)
3825 struct nlattr
*tb
[IFLA_MAX
+ 1];
3828 if (!data
|| !data
[ops
->peer_type
])
3831 err
= rtnl_nla_parse_ifinfomsg(tb
, data
[ops
->peer_type
], extack
);
3833 return ERR_PTR(err
);
3835 if (ops
->validate
) {
3836 err
= ops
->validate(tb
, NULL
, extack
);
3838 return ERR_PTR(err
);
3841 return rtnl_link_get_net_ifla(tb
);
3844 static int __rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3845 const struct rtnl_link_ops
*ops
,
3846 struct net
*tgt_net
, struct net
*link_net
,
3847 struct net
*peer_net
,
3848 struct rtnl_newlink_tbs
*tbs
,
3849 struct nlattr
**data
,
3850 struct netlink_ext_ack
*extack
)
3852 struct nlattr
** const tb
= tbs
->tb
;
3853 struct net
*net
= sock_net(skb
->sk
);
3854 struct net_device
*dev
;
3855 struct ifinfomsg
*ifm
;
3856 bool link_specified
;
3858 ifm
= nlmsg_data(nlh
);
3859 if (ifm
->ifi_index
> 0) {
3860 link_specified
= true;
3861 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3862 } else if (ifm
->ifi_index
< 0) {
3863 NL_SET_ERR_MSG(extack
, "ifindex can't be negative");
3865 } else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
]) {
3866 link_specified
= true;
3867 dev
= rtnl_dev_get(net
, tb
);
3869 link_specified
= false;
3874 return rtnl_changelink(skb
, nlh
, ops
, dev
, tgt_net
, tbs
, data
, extack
);
3876 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3877 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3878 * or it's for a group
3880 if (link_specified
|| !tb
[IFLA_GROUP
])
3883 return rtnl_group_changelink(skb
, net
, tgt_net
,
3884 nla_get_u32(tb
[IFLA_GROUP
]),
3888 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3892 NL_SET_ERR_MSG(extack
, "Unknown device type");
3896 return rtnl_newlink_create(skb
, ifm
, ops
, tgt_net
, link_net
, peer_net
, nlh
,
3900 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3901 struct netlink_ext_ack
*extack
)
3903 struct net
*tgt_net
, *link_net
= NULL
, *peer_net
= NULL
;
3904 struct nlattr
**tb
, **linkinfo
, **data
= NULL
;
3905 struct rtnl_link_ops
*ops
= NULL
;
3906 struct rtnl_newlink_tbs
*tbs
;
3907 struct rtnl_nets rtnl_nets
;
3911 tbs
= kmalloc(sizeof(*tbs
), GFP_KERNEL
);
3916 ret
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
), tb
,
3917 IFLA_MAX
, ifla_policy
, extack
);
3921 ret
= rtnl_ensure_unique_netns(tb
, extack
, false);
3925 linkinfo
= tbs
->linkinfo
;
3926 if (tb
[IFLA_LINKINFO
]) {
3927 ret
= nla_parse_nested_deprecated(linkinfo
, IFLA_INFO_MAX
,
3929 ifla_info_policy
, NULL
);
3933 memset(linkinfo
, 0, sizeof(tbs
->linkinfo
));
3936 if (linkinfo
[IFLA_INFO_KIND
]) {
3937 char kind
[MODULE_NAME_LEN
];
3939 nla_strscpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
3940 ops
= rtnl_link_ops_get(kind
, &ops_srcu_index
);
3941 #ifdef CONFIG_MODULES
3943 request_module("rtnl-link-%s", kind
);
3944 ops
= rtnl_link_ops_get(kind
, &ops_srcu_index
);
3949 rtnl_nets_init(&rtnl_nets
);
3952 if (ops
->maxtype
> RTNL_MAX_TYPE
) {
3957 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3958 ret
= nla_parse_nested_deprecated(tbs
->attr
, ops
->maxtype
,
3959 linkinfo
[IFLA_INFO_DATA
],
3960 ops
->policy
, extack
);
3967 if (ops
->validate
) {
3968 ret
= ops
->validate(tb
, data
, extack
);
3973 if (ops
->peer_type
) {
3974 peer_net
= rtnl_get_peer_net(ops
, data
, extack
);
3975 if (IS_ERR(peer_net
))
3978 rtnl_nets_add(&rtnl_nets
, peer_net
);
3982 tgt_net
= rtnl_link_get_net_capable(skb
, sock_net(skb
->sk
), tb
, CAP_NET_ADMIN
);
3983 if (IS_ERR(tgt_net
)) {
3984 ret
= PTR_ERR(tgt_net
);
3988 rtnl_nets_add(&rtnl_nets
, tgt_net
);
3990 if (tb
[IFLA_LINK_NETNSID
]) {
3991 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3993 link_net
= get_net_ns_by_id(tgt_net
, id
);
3995 NL_SET_ERR_MSG(extack
, "Unknown network namespace id");
4000 rtnl_nets_add(&rtnl_nets
, link_net
);
4002 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
)) {
4008 rtnl_nets_lock(&rtnl_nets
);
4009 ret
= __rtnl_newlink(skb
, nlh
, ops
, tgt_net
, link_net
, peer_net
, tbs
, data
, extack
);
4010 rtnl_nets_unlock(&rtnl_nets
);
4013 rtnl_nets_destroy(&rtnl_nets
);
4016 rtnl_link_ops_put(ops
, ops_srcu_index
);
4022 static int rtnl_valid_getlink_req(struct sk_buff
*skb
,
4023 const struct nlmsghdr
*nlh
,
4025 struct netlink_ext_ack
*extack
)
4027 struct ifinfomsg
*ifm
;
4030 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
4031 NL_SET_ERR_MSG(extack
, "Invalid header for get link");
4035 if (!netlink_strict_get_check(skb
))
4036 return nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
4037 ifla_policy
, extack
);
4039 ifm
= nlmsg_data(nlh
);
4040 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
4042 NL_SET_ERR_MSG(extack
, "Invalid values in header for get link request");
4046 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
4047 ifla_policy
, extack
);
4051 for (i
= 0; i
<= IFLA_MAX
; i
++) {
4057 case IFLA_ALT_IFNAME
:
4059 case IFLA_TARGET_NETNSID
:
4062 NL_SET_ERR_MSG(extack
, "Unsupported attribute in get link request");
4070 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4071 struct netlink_ext_ack
*extack
)
4073 struct net
*net
= sock_net(skb
->sk
);
4074 struct net
*tgt_net
= net
;
4075 struct ifinfomsg
*ifm
;
4076 struct nlattr
*tb
[IFLA_MAX
+1];
4077 struct net_device
*dev
= NULL
;
4078 struct sk_buff
*nskb
;
4081 u32 ext_filter_mask
= 0;
4083 err
= rtnl_valid_getlink_req(skb
, nlh
, tb
, extack
);
4087 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
4091 if (tb
[IFLA_TARGET_NETNSID
]) {
4092 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
4093 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
4094 if (IS_ERR(tgt_net
))
4095 return PTR_ERR(tgt_net
);
4098 if (tb
[IFLA_EXT_MASK
])
4099 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
4102 ifm
= nlmsg_data(nlh
);
4103 if (ifm
->ifi_index
> 0)
4104 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
4105 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
4106 dev
= rtnl_dev_get(tgt_net
, tb
);
4115 nskb
= nlmsg_new_large(if_nlmsg_size(dev
, ext_filter_mask
));
4119 /* Synchronize the carrier state so we don't report a state
4120 * that we're not actually going to honour immediately; if
4121 * the driver just did a carrier off->on transition, we can
4122 * only TX if link watch work has run, but without this we'd
4123 * already report carrier on, even if it doesn't work yet.
4125 linkwatch_sync_dev(dev
);
4127 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
4128 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
4129 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
4130 0, NULL
, 0, netnsid
, GFP_KERNEL
);
4132 /* -EMSGSIZE implies BUG in if_nlmsg_size */
4133 WARN_ON(err
== -EMSGSIZE
);
4136 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4144 static int rtnl_alt_ifname(int cmd
, struct net_device
*dev
, struct nlattr
*attr
,
4145 bool *changed
, struct netlink_ext_ack
*extack
)
4151 err
= nla_validate(attr
, attr
->nla_len
, IFLA_MAX
, ifla_policy
, extack
);
4155 if (cmd
== RTM_NEWLINKPROP
) {
4156 size
= rtnl_prop_list_size(dev
);
4157 size
+= nla_total_size(ALTIFNAMSIZ
);
4158 if (size
>= U16_MAX
) {
4159 NL_SET_ERR_MSG(extack
,
4160 "effective property list too long");
4165 alt_ifname
= nla_strdup(attr
, GFP_KERNEL_ACCOUNT
);
4169 if (cmd
== RTM_NEWLINKPROP
) {
4170 err
= netdev_name_node_alt_create(dev
, alt_ifname
);
4173 } else if (cmd
== RTM_DELLINKPROP
) {
4174 err
= netdev_name_node_alt_destroy(dev
, alt_ifname
);
4186 static int rtnl_linkprop(int cmd
, struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4187 struct netlink_ext_ack
*extack
)
4189 struct net
*net
= sock_net(skb
->sk
);
4190 struct nlattr
*tb
[IFLA_MAX
+ 1];
4191 struct net_device
*dev
;
4192 struct ifinfomsg
*ifm
;
4193 bool changed
= false;
4194 struct nlattr
*attr
;
4197 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
4201 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
4205 ifm
= nlmsg_data(nlh
);
4206 if (ifm
->ifi_index
> 0)
4207 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4208 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
4209 dev
= rtnl_dev_get(net
, tb
);
4216 if (!tb
[IFLA_PROP_LIST
])
4219 nla_for_each_nested(attr
, tb
[IFLA_PROP_LIST
], rem
) {
4220 switch (nla_type(attr
)) {
4221 case IFLA_ALT_IFNAME
:
4222 err
= rtnl_alt_ifname(cmd
, dev
, attr
, &changed
, extack
);
4230 netdev_state_change(dev
);
4234 static int rtnl_newlinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4235 struct netlink_ext_ack
*extack
)
4237 return rtnl_linkprop(RTM_NEWLINKPROP
, skb
, nlh
, extack
);
4240 static int rtnl_dellinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4241 struct netlink_ext_ack
*extack
)
4243 return rtnl_linkprop(RTM_DELLINKPROP
, skb
, nlh
, extack
);
4246 static noinline_for_stack u32
rtnl_calcit(struct sk_buff
*skb
,
4247 struct nlmsghdr
*nlh
)
4249 struct net
*net
= sock_net(skb
->sk
);
4250 size_t min_ifinfo_dump_size
= 0;
4251 u32 ext_filter_mask
= 0;
4252 struct net_device
*dev
;
4256 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
4257 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
4258 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
4260 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
4261 return NLMSG_GOODSIZE
;
4263 nla_for_each_attr_type(nla
, IFLA_EXT_MASK
,
4264 nlmsg_attrdata(nlh
, hdrlen
),
4265 nlmsg_attrlen(nlh
, hdrlen
), rem
) {
4266 if (nla_len(nla
) == sizeof(u32
))
4267 ext_filter_mask
= nla_get_u32(nla
);
4270 if (!ext_filter_mask
)
4271 return NLMSG_GOODSIZE
;
4273 * traverse the list of net devices and compute the minimum
4274 * buffer size based upon the filter mask.
4277 for_each_netdev_rcu(net
, dev
) {
4278 min_ifinfo_dump_size
= max(min_ifinfo_dump_size
,
4279 if_nlmsg_size(dev
, ext_filter_mask
));
4283 return nlmsg_total_size(min_ifinfo_dump_size
);
4286 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4289 int s_idx
= cb
->family
;
4290 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
4296 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
4297 struct rtnl_link __rcu
**tab
;
4298 struct rtnl_link
*link
;
4299 rtnl_dumpit_func dumpit
;
4301 if (idx
< s_idx
|| idx
== PF_PACKET
)
4304 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
4307 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
4311 link
= rcu_dereference_rtnl(tab
[type
]);
4315 dumpit
= link
->dumpit
;
4320 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
4324 ret
= dumpit(skb
, cb
);
4330 return skb
->len
? : ret
;
4333 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
4334 unsigned int change
,
4335 u32 event
, gfp_t flags
, int *new_nsid
,
4336 int new_ifindex
, u32 portid
,
4337 const struct nlmsghdr
*nlh
)
4339 struct net
*net
= dev_net(dev
);
4340 struct sk_buff
*skb
;
4344 skb
= nlmsg_new(if_nlmsg_size(dev
, 0), flags
);
4348 if (nlmsg_report(nlh
))
4349 seq
= nlmsg_seq(nlh
);
4353 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
4354 type
, portid
, seq
, change
, 0, 0, event
,
4355 new_nsid
, new_ifindex
, -1, flags
);
4357 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
4358 WARN_ON(err
== -EMSGSIZE
);
4364 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4368 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
,
4369 u32 portid
, const struct nlmsghdr
*nlh
)
4371 struct net
*net
= dev_net(dev
);
4373 rtnl_notify(skb
, net
, portid
, RTNLGRP_LINK
, nlh
, flags
);
4376 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
4377 unsigned int change
, u32 event
,
4378 gfp_t flags
, int *new_nsid
, int new_ifindex
,
4379 u32 portid
, const struct nlmsghdr
*nlh
)
4381 struct sk_buff
*skb
;
4383 if (dev
->reg_state
!= NETREG_REGISTERED
)
4386 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
4387 new_ifindex
, portid
, nlh
);
4389 rtmsg_ifinfo_send(skb
, dev
, flags
, portid
, nlh
);
4392 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
4393 gfp_t flags
, u32 portid
, const struct nlmsghdr
*nlh
)
4395 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
4396 NULL
, 0, portid
, nlh
);
4399 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
4400 gfp_t flags
, int *new_nsid
, int new_ifindex
)
4402 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
4403 new_nsid
, new_ifindex
, 0, NULL
);
4406 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
4407 struct net_device
*dev
,
4408 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
4409 int type
, unsigned int flags
,
4410 int nlflags
, u16 ndm_state
)
4412 struct nlmsghdr
*nlh
;
4415 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
4419 ndm
= nlmsg_data(nlh
);
4420 ndm
->ndm_family
= AF_BRIDGE
;
4423 ndm
->ndm_flags
= flags
;
4425 ndm
->ndm_ifindex
= dev
->ifindex
;
4426 ndm
->ndm_state
= ndm_state
;
4428 if (nla_put(skb
, NDA_LLADDR
, dev
->addr_len
, addr
))
4429 goto nla_put_failure
;
4431 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
4432 goto nla_put_failure
;
4434 nlmsg_end(skb
, nlh
);
4438 nlmsg_cancel(skb
, nlh
);
4442 static inline size_t rtnl_fdb_nlmsg_size(const struct net_device
*dev
)
4444 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
4445 nla_total_size(dev
->addr_len
) + /* NDA_LLADDR */
4446 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
4450 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
4453 struct net
*net
= dev_net(dev
);
4454 struct sk_buff
*skb
;
4457 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(dev
), GFP_ATOMIC
);
4461 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
4462 0, 0, type
, NTF_SELF
, 0, ndm_state
);
4468 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
4471 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
4475 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4477 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
4478 struct nlattr
*tb
[],
4479 struct net_device
*dev
,
4480 const unsigned char *addr
, u16 vid
,
4485 /* If aging addresses are supported device will need to
4486 * implement its own handler for this.
4488 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
4489 netdev_info(dev
, "default FDB implementation only supports local addresses\n");
4493 if (tb
[NDA_FLAGS_EXT
]) {
4494 netdev_info(dev
, "invalid flags given to default FDB implementation\n");
4499 netdev_info(dev
, "vlans aren't supported yet for dev_uc|mc_add()\n");
4503 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
4504 err
= dev_uc_add_excl(dev
, addr
);
4505 else if (is_multicast_ether_addr(addr
))
4506 err
= dev_mc_add_excl(dev
, addr
);
4508 /* Only return duplicate errors if NLM_F_EXCL is set */
4509 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
4514 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
4516 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
4517 struct netlink_ext_ack
*extack
)
4522 if (nla_len(vlan_attr
) != sizeof(u16
)) {
4523 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
4527 vid
= nla_get_u16(vlan_attr
);
4529 if (!vid
|| vid
>= VLAN_VID_MASK
) {
4530 NL_SET_ERR_MSG(extack
, "invalid vlan id");
4538 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4539 struct netlink_ext_ack
*extack
)
4541 struct net
*net
= sock_net(skb
->sk
);
4543 struct nlattr
*tb
[NDA_MAX
+1];
4544 struct net_device
*dev
;
4549 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
4554 ndm
= nlmsg_data(nlh
);
4555 if (ndm
->ndm_ifindex
== 0) {
4556 NL_SET_ERR_MSG(extack
, "invalid ifindex");
4560 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
4562 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4566 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4567 NL_SET_ERR_MSG(extack
, "invalid address");
4571 if (dev
->type
!= ARPHRD_ETHER
) {
4572 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
4576 addr
= nla_data(tb
[NDA_LLADDR
]);
4578 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
4584 /* Support fdb on master device the net/bridge default case */
4585 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
4586 netif_is_bridge_port(dev
)) {
4587 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4588 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
4589 bool notified
= false;
4591 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
4592 nlh
->nlmsg_flags
, ¬ified
, extack
);
4596 ndm
->ndm_flags
&= ~NTF_MASTER
;
4599 /* Embedded bridge, macvlan, and any other device support */
4600 if ((ndm
->ndm_flags
& NTF_SELF
)) {
4601 bool notified
= false;
4603 if (dev
->netdev_ops
->ndo_fdb_add
)
4604 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
4609 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
4612 if (!err
&& !notified
) {
4613 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
4615 ndm
->ndm_flags
&= ~NTF_SELF
;
4623 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4625 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
4626 struct nlattr
*tb
[],
4627 struct net_device
*dev
,
4628 const unsigned char *addr
, u16 vid
)
4632 /* If aging addresses are supported device will need to
4633 * implement its own handler for this.
4635 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
4636 netdev_info(dev
, "default FDB implementation only supports local addresses\n");
4640 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
4641 err
= dev_uc_del(dev
, addr
);
4642 else if (is_multicast_ether_addr(addr
))
4643 err
= dev_mc_del(dev
, addr
);
4647 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
4649 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4650 struct netlink_ext_ack
*extack
)
4652 bool del_bulk
= !!(nlh
->nlmsg_flags
& NLM_F_BULK
);
4653 struct net
*net
= sock_net(skb
->sk
);
4654 const struct net_device_ops
*ops
;
4656 struct nlattr
*tb
[NDA_MAX
+1];
4657 struct net_device
*dev
;
4662 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
4666 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
,
4669 /* For bulk delete, the drivers will parse the message with
4672 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
4677 ndm
= nlmsg_data(nlh
);
4678 if (ndm
->ndm_ifindex
== 0) {
4679 NL_SET_ERR_MSG(extack
, "invalid ifindex");
4683 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
4685 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4690 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4691 NL_SET_ERR_MSG(extack
, "invalid address");
4694 addr
= nla_data(tb
[NDA_LLADDR
]);
4696 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
4701 if (dev
->type
!= ARPHRD_ETHER
) {
4702 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
4708 /* Support fdb on master device the net/bridge default case */
4709 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
4710 netif_is_bridge_port(dev
)) {
4711 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4712 bool notified
= false;
4714 ops
= br_dev
->netdev_ops
;
4716 if (ops
->ndo_fdb_del
)
4717 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
,
4720 if (ops
->ndo_fdb_del_bulk
)
4721 err
= ops
->ndo_fdb_del_bulk(nlh
, dev
, extack
);
4727 ndm
->ndm_flags
&= ~NTF_MASTER
;
4730 /* Embedded bridge, macvlan, and any other device support */
4731 if (ndm
->ndm_flags
& NTF_SELF
) {
4732 bool notified
= false;
4734 ops
= dev
->netdev_ops
;
4736 if (ops
->ndo_fdb_del
)
4737 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
,
4740 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
4742 /* in case err was cleared by NTF_MASTER call */
4744 if (ops
->ndo_fdb_del_bulk
)
4745 err
= ops
->ndo_fdb_del_bulk(nlh
, dev
, extack
);
4749 if (!del_bulk
&& !notified
)
4750 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
4752 ndm
->ndm_flags
&= ~NTF_SELF
;
4759 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
4760 struct netlink_callback
*cb
,
4761 struct net_device
*dev
,
4763 struct netdev_hw_addr_list
*list
)
4765 struct netdev_hw_addr
*ha
;
4769 portid
= NETLINK_CB(cb
->skb
).portid
;
4770 seq
= cb
->nlh
->nlmsg_seq
;
4772 list_for_each_entry(ha
, &list
->list
, list
) {
4773 if (*idx
< cb
->args
[2])
4776 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
4778 RTM_NEWNEIGH
, NTF_SELF
,
4779 NLM_F_MULTI
, NUD_PERMANENT
);
4789 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4790 * @skb: socket buffer to store message in
4791 * @cb: netlink callback
4793 * @filter_dev: ignored
4794 * @idx: the number of FDB table entries dumped is added to *@idx
4796 * Default netdevice operation to dump the existing unicast address list.
4797 * Returns number of addresses from list put in skb.
4799 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
4800 struct netlink_callback
*cb
,
4801 struct net_device
*dev
,
4802 struct net_device
*filter_dev
,
4807 if (dev
->type
!= ARPHRD_ETHER
)
4810 netif_addr_lock_bh(dev
);
4811 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
4814 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
4816 netif_addr_unlock_bh(dev
);
4819 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
4821 static int valid_fdb_dump_strict(const struct nlmsghdr
*nlh
,
4822 int *br_idx
, int *brport_idx
,
4823 struct netlink_ext_ack
*extack
)
4825 struct nlattr
*tb
[NDA_MAX
+ 1];
4829 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4830 NL_SET_ERR_MSG(extack
, "Invalid header for fdb dump request");
4834 ndm
= nlmsg_data(nlh
);
4835 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4836 ndm
->ndm_flags
|| ndm
->ndm_type
) {
4837 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb dump request");
4841 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4842 NDA_MAX
, NULL
, extack
);
4846 *brport_idx
= ndm
->ndm_ifindex
;
4847 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4853 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4854 NL_SET_ERR_MSG(extack
, "Invalid IFINDEX attribute in fdb dump request");
4857 *brport_idx
= nla_get_u32(tb
[NDA_IFINDEX
]);
4860 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4861 NL_SET_ERR_MSG(extack
, "Invalid MASTER attribute in fdb dump request");
4864 *br_idx
= nla_get_u32(tb
[NDA_MASTER
]);
4867 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb dump request");
4875 static int valid_fdb_dump_legacy(const struct nlmsghdr
*nlh
,
4876 int *br_idx
, int *brport_idx
,
4877 struct netlink_ext_ack
*extack
)
4879 struct nlattr
*tb
[IFLA_MAX
+1];
4882 /* A hack to preserve kernel<->userspace interface.
4883 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4884 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4885 * So, check for ndmsg with an optional u32 attribute (not used here).
4886 * Fortunately these sizes don't conflict with the size of ifinfomsg
4887 * with an optional attribute.
4889 if (nlmsg_len(nlh
) != sizeof(struct ndmsg
) &&
4890 (nlmsg_len(nlh
) != sizeof(struct ndmsg
) +
4891 nla_attr_size(sizeof(u32
)))) {
4892 struct ifinfomsg
*ifm
;
4894 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4895 tb
, IFLA_MAX
, ifla_policy
,
4899 } else if (err
== 0) {
4900 if (tb
[IFLA_MASTER
])
4901 *br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
4904 ifm
= nlmsg_data(nlh
);
4905 *brport_idx
= ifm
->ifi_index
;
4910 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4912 struct net_device
*dev
;
4913 struct net_device
*br_dev
= NULL
;
4914 const struct net_device_ops
*ops
= NULL
;
4915 const struct net_device_ops
*cops
= NULL
;
4916 struct net
*net
= sock_net(skb
->sk
);
4917 struct hlist_head
*head
;
4925 if (cb
->strict_check
)
4926 err
= valid_fdb_dump_strict(cb
->nlh
, &br_idx
, &brport_idx
,
4929 err
= valid_fdb_dump_legacy(cb
->nlh
, &br_idx
, &brport_idx
,
4935 br_dev
= __dev_get_by_index(net
, br_idx
);
4939 ops
= br_dev
->netdev_ops
;
4943 s_idx
= cb
->args
[1];
4945 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4947 head
= &net
->dev_index_head
[h
];
4948 hlist_for_each_entry(dev
, head
, index_hlist
) {
4950 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
4953 if (!br_idx
) { /* user did not specify a specific bridge */
4954 if (netif_is_bridge_port(dev
)) {
4955 br_dev
= netdev_master_upper_dev_get(dev
);
4956 cops
= br_dev
->netdev_ops
;
4959 if (dev
!= br_dev
&&
4960 !netif_is_bridge_port(dev
))
4963 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
4964 !netif_is_bridge_master(dev
))
4972 if (netif_is_bridge_port(dev
)) {
4973 if (cops
&& cops
->ndo_fdb_dump
) {
4974 err
= cops
->ndo_fdb_dump(skb
, cb
,
4977 if (err
== -EMSGSIZE
)
4982 if (dev
->netdev_ops
->ndo_fdb_dump
)
4983 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
4987 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
4989 if (err
== -EMSGSIZE
)
4994 /* reset fdb offset to 0 for rest of the interfaces */
5010 static int valid_fdb_get_strict(const struct nlmsghdr
*nlh
,
5011 struct nlattr
**tb
, u8
*ndm_flags
,
5012 int *br_idx
, int *brport_idx
, u8
**addr
,
5013 u16
*vid
, struct netlink_ext_ack
*extack
)
5018 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
5019 NL_SET_ERR_MSG(extack
, "Invalid header for fdb get request");
5023 ndm
= nlmsg_data(nlh
);
5024 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
5026 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb get request");
5030 if (ndm
->ndm_flags
& ~(NTF_MASTER
| NTF_SELF
)) {
5031 NL_SET_ERR_MSG(extack
, "Invalid flags in header for fdb get request");
5035 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
5036 NDA_MAX
, nda_policy
, extack
);
5040 *ndm_flags
= ndm
->ndm_flags
;
5041 *brport_idx
= ndm
->ndm_ifindex
;
5042 for (i
= 0; i
<= NDA_MAX
; ++i
) {
5048 *br_idx
= nla_get_u32(tb
[i
]);
5051 if (nla_len(tb
[i
]) != ETH_ALEN
) {
5052 NL_SET_ERR_MSG(extack
, "Invalid address in fdb get request");
5055 *addr
= nla_data(tb
[i
]);
5058 err
= fdb_vid_parse(tb
[i
], vid
, extack
);
5065 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb get request");
5073 static int rtnl_fdb_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
5074 struct netlink_ext_ack
*extack
)
5076 struct net_device
*dev
= NULL
, *br_dev
= NULL
;
5077 const struct net_device_ops
*ops
= NULL
;
5078 struct net
*net
= sock_net(in_skb
->sk
);
5079 struct nlattr
*tb
[NDA_MAX
+ 1];
5080 struct sk_buff
*skb
;
5088 err
= valid_fdb_get_strict(nlh
, tb
, &ndm_flags
, &br_idx
,
5089 &brport_idx
, &addr
, &vid
, extack
);
5094 NL_SET_ERR_MSG(extack
, "Missing lookup address for fdb get request");
5099 dev
= __dev_get_by_index(net
, brport_idx
);
5101 NL_SET_ERR_MSG(extack
, "Unknown device ifindex");
5108 NL_SET_ERR_MSG(extack
, "Master and device are mutually exclusive");
5112 br_dev
= __dev_get_by_index(net
, br_idx
);
5114 NL_SET_ERR_MSG(extack
, "Invalid master ifindex");
5117 ops
= br_dev
->netdev_ops
;
5121 if (!ndm_flags
|| (ndm_flags
& NTF_MASTER
)) {
5122 if (!netif_is_bridge_port(dev
)) {
5123 NL_SET_ERR_MSG(extack
, "Device is not a bridge port");
5126 br_dev
= netdev_master_upper_dev_get(dev
);
5128 NL_SET_ERR_MSG(extack
, "Master of device not found");
5131 ops
= br_dev
->netdev_ops
;
5133 if (!(ndm_flags
& NTF_SELF
)) {
5134 NL_SET_ERR_MSG(extack
, "Missing NTF_SELF");
5137 ops
= dev
->netdev_ops
;
5141 if (!br_dev
&& !dev
) {
5142 NL_SET_ERR_MSG(extack
, "No device specified");
5146 if (!ops
|| !ops
->ndo_fdb_get
) {
5147 NL_SET_ERR_MSG(extack
, "Fdb get operation not supported by device");
5151 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
5157 err
= ops
->ndo_fdb_get(skb
, tb
, dev
, addr
, vid
,
5158 NETLINK_CB(in_skb
).portid
,
5159 nlh
->nlmsg_seq
, extack
);
5163 return rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
5169 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
5170 unsigned int attrnum
, unsigned int flag
)
5173 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
5177 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
5178 struct net_device
*dev
, u16 mode
,
5179 u32 flags
, u32 mask
, int nlflags
,
5181 int (*vlan_fill
)(struct sk_buff
*skb
,
5182 struct net_device
*dev
,
5185 struct nlmsghdr
*nlh
;
5186 struct ifinfomsg
*ifm
;
5187 struct nlattr
*br_afspec
;
5188 struct nlattr
*protinfo
;
5189 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
5190 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
5193 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
5197 ifm
= nlmsg_data(nlh
);
5198 ifm
->ifi_family
= AF_BRIDGE
;
5200 ifm
->ifi_type
= dev
->type
;
5201 ifm
->ifi_index
= dev
->ifindex
;
5202 ifm
->ifi_flags
= dev_get_flags(dev
);
5203 ifm
->ifi_change
= 0;
5206 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
5207 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
5208 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
5210 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
5212 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
5213 (dev
->ifindex
!= dev_get_iflink(dev
) &&
5214 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
5215 goto nla_put_failure
;
5217 br_afspec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
5219 goto nla_put_failure
;
5221 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
5222 nla_nest_cancel(skb
, br_afspec
);
5223 goto nla_put_failure
;
5226 if (mode
!= BRIDGE_MODE_UNDEF
) {
5227 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
5228 nla_nest_cancel(skb
, br_afspec
);
5229 goto nla_put_failure
;
5233 err
= vlan_fill(skb
, dev
, filter_mask
);
5235 nla_nest_cancel(skb
, br_afspec
);
5236 goto nla_put_failure
;
5239 nla_nest_end(skb
, br_afspec
);
5241 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
5243 goto nla_put_failure
;
5245 if (brport_nla_put_flag(skb
, flags
, mask
,
5246 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
5247 brport_nla_put_flag(skb
, flags
, mask
,
5248 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
5249 brport_nla_put_flag(skb
, flags
, mask
,
5250 IFLA_BRPORT_FAST_LEAVE
,
5251 BR_MULTICAST_FAST_LEAVE
) ||
5252 brport_nla_put_flag(skb
, flags
, mask
,
5253 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
5254 brport_nla_put_flag(skb
, flags
, mask
,
5255 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
5256 brport_nla_put_flag(skb
, flags
, mask
,
5257 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
5258 brport_nla_put_flag(skb
, flags
, mask
,
5259 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
5260 brport_nla_put_flag(skb
, flags
, mask
,
5261 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
) ||
5262 brport_nla_put_flag(skb
, flags
, mask
,
5263 IFLA_BRPORT_MCAST_FLOOD
, BR_MCAST_FLOOD
) ||
5264 brport_nla_put_flag(skb
, flags
, mask
,
5265 IFLA_BRPORT_BCAST_FLOOD
, BR_BCAST_FLOOD
)) {
5266 nla_nest_cancel(skb
, protinfo
);
5267 goto nla_put_failure
;
5270 nla_nest_end(skb
, protinfo
);
5272 nlmsg_end(skb
, nlh
);
5275 nlmsg_cancel(skb
, nlh
);
5276 return err
? err
: -EMSGSIZE
;
5278 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
5280 static int valid_bridge_getlink_req(const struct nlmsghdr
*nlh
,
5281 bool strict_check
, u32
*filter_mask
,
5282 struct netlink_ext_ack
*extack
)
5284 struct nlattr
*tb
[IFLA_MAX
+1];
5288 struct ifinfomsg
*ifm
;
5290 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5291 NL_SET_ERR_MSG(extack
, "Invalid header for bridge link dump");
5295 ifm
= nlmsg_data(nlh
);
5296 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
5297 ifm
->ifi_change
|| ifm
->ifi_index
) {
5298 NL_SET_ERR_MSG(extack
, "Invalid values in header for bridge link dump request");
5302 err
= nlmsg_parse_deprecated_strict(nlh
,
5303 sizeof(struct ifinfomsg
),
5304 tb
, IFLA_MAX
, ifla_policy
,
5307 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
5308 tb
, IFLA_MAX
, ifla_policy
,
5314 /* new attributes should only be added with strict checking */
5315 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
5321 *filter_mask
= nla_get_u32(tb
[i
]);
5325 NL_SET_ERR_MSG(extack
, "Unsupported attribute in bridge link dump request");
5334 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5336 const struct nlmsghdr
*nlh
= cb
->nlh
;
5337 struct net
*net
= sock_net(skb
->sk
);
5338 struct net_device
*dev
;
5340 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
5341 u32 seq
= nlh
->nlmsg_seq
;
5342 u32 filter_mask
= 0;
5345 err
= valid_bridge_getlink_req(nlh
, cb
->strict_check
, &filter_mask
,
5347 if (err
< 0 && cb
->strict_check
)
5351 for_each_netdev_rcu(net
, dev
) {
5352 const struct net_device_ops
*ops
= dev
->netdev_ops
;
5353 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
5355 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
5356 if (idx
>= cb
->args
[0]) {
5357 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
5358 skb
, portid
, seq
, dev
,
5359 filter_mask
, NLM_F_MULTI
);
5360 if (err
< 0 && err
!= -EOPNOTSUPP
) {
5361 if (likely(skb
->len
))
5370 if (ops
->ndo_bridge_getlink
) {
5371 if (idx
>= cb
->args
[0]) {
5372 err
= ops
->ndo_bridge_getlink(skb
, portid
,
5376 if (err
< 0 && err
!= -EOPNOTSUPP
) {
5377 if (likely(skb
->len
))
5394 static inline size_t bridge_nlmsg_size(void)
5396 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
5397 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
5398 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
5399 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
5400 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
5401 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
5402 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
5403 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
5404 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
5405 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
5406 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
5409 static int rtnl_bridge_notify(struct net_device
*dev
)
5411 struct net
*net
= dev_net(dev
);
5412 struct sk_buff
*skb
;
5413 int err
= -EOPNOTSUPP
;
5415 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
5418 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
5424 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
5428 /* Notification info is only filled for bridge ports, not the bridge
5429 * device itself. Therefore, a zero notification length is valid and
5430 * should not result in an error.
5435 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
5438 WARN_ON(err
== -EMSGSIZE
);
5441 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
5445 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5446 struct netlink_ext_ack
*extack
)
5448 struct net
*net
= sock_net(skb
->sk
);
5449 struct ifinfomsg
*ifm
;
5450 struct net_device
*dev
;
5451 struct nlattr
*br_spec
, *attr
, *br_flags_attr
= NULL
;
5452 int rem
, err
= -EOPNOTSUPP
;
5455 if (nlmsg_len(nlh
) < sizeof(*ifm
))
5458 ifm
= nlmsg_data(nlh
);
5459 if (ifm
->ifi_family
!= AF_BRIDGE
)
5460 return -EPFNOSUPPORT
;
5462 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
5464 NL_SET_ERR_MSG(extack
, "unknown ifindex");
5468 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
5470 nla_for_each_nested(attr
, br_spec
, rem
) {
5471 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
&& !br_flags_attr
) {
5472 if (nla_len(attr
) < sizeof(flags
))
5475 br_flags_attr
= attr
;
5476 flags
= nla_get_u16(attr
);
5479 if (nla_type(attr
) == IFLA_BRIDGE_MODE
) {
5480 if (nla_len(attr
) < sizeof(u16
))
5486 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
5487 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
5489 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
5494 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
,
5499 flags
&= ~BRIDGE_FLAGS_MASTER
;
5502 if ((flags
& BRIDGE_FLAGS_SELF
)) {
5503 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
5506 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
5510 flags
&= ~BRIDGE_FLAGS_SELF
;
5512 /* Generate event to notify upper layer of bridge
5515 err
= rtnl_bridge_notify(dev
);
5520 memcpy(nla_data(br_flags_attr
), &flags
, sizeof(flags
));
5525 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5526 struct netlink_ext_ack
*extack
)
5528 struct net
*net
= sock_net(skb
->sk
);
5529 struct ifinfomsg
*ifm
;
5530 struct net_device
*dev
;
5531 struct nlattr
*br_spec
, *attr
= NULL
;
5532 int rem
, err
= -EOPNOTSUPP
;
5534 bool have_flags
= false;
5536 if (nlmsg_len(nlh
) < sizeof(*ifm
))
5539 ifm
= nlmsg_data(nlh
);
5540 if (ifm
->ifi_family
!= AF_BRIDGE
)
5541 return -EPFNOSUPPORT
;
5543 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
5545 NL_SET_ERR_MSG(extack
, "unknown ifindex");
5549 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
5551 nla_for_each_nested_type(attr
, IFLA_BRIDGE_FLAGS
, br_spec
,
5553 if (nla_len(attr
) < sizeof(flags
))
5557 flags
= nla_get_u16(attr
);
5562 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
5563 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
5565 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
5570 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
5574 flags
&= ~BRIDGE_FLAGS_MASTER
;
5577 if ((flags
& BRIDGE_FLAGS_SELF
)) {
5578 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
5581 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
5585 flags
&= ~BRIDGE_FLAGS_SELF
;
5587 /* Generate event to notify upper layer of bridge
5590 err
= rtnl_bridge_notify(dev
);
5595 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
5600 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
5602 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
5603 (!idxattr
|| idxattr
== attrid
);
5607 rtnl_offload_xstats_have_ndo(const struct net_device
*dev
, int attr_id
)
5609 return dev
->netdev_ops
&&
5610 dev
->netdev_ops
->ndo_has_offload_stats
&&
5611 dev
->netdev_ops
->ndo_get_offload_stats
&&
5612 dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
);
5616 rtnl_offload_xstats_get_size_ndo(const struct net_device
*dev
, int attr_id
)
5618 return rtnl_offload_xstats_have_ndo(dev
, attr_id
) ?
5619 sizeof(struct rtnl_link_stats64
) : 0;
5623 rtnl_offload_xstats_fill_ndo(struct net_device
*dev
, int attr_id
,
5624 struct sk_buff
*skb
)
5626 unsigned int size
= rtnl_offload_xstats_get_size_ndo(dev
, attr_id
);
5627 struct nlattr
*attr
= NULL
;
5634 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
5635 IFLA_OFFLOAD_XSTATS_UNSPEC
);
5639 attr_data
= nla_data(attr
);
5640 memset(attr_data
, 0, size
);
5642 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
, attr_data
);
5650 rtnl_offload_xstats_get_size_stats(const struct net_device
*dev
,
5651 enum netdev_offload_xstats_type type
)
5653 bool enabled
= netdev_offload_xstats_enabled(dev
, type
);
5655 return enabled
? sizeof(struct rtnl_hw_stats64
) : 0;
5658 struct rtnl_offload_xstats_request_used
{
5664 rtnl_offload_xstats_get_stats(struct net_device
*dev
,
5665 enum netdev_offload_xstats_type type
,
5666 struct rtnl_offload_xstats_request_used
*ru
,
5667 struct rtnl_hw_stats64
*stats
,
5668 struct netlink_ext_ack
*extack
)
5674 request
= netdev_offload_xstats_enabled(dev
, type
);
5680 err
= netdev_offload_xstats_get(dev
, type
, stats
, &used
, extack
);
5686 ru
->request
= request
;
5693 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff
*skb
, int attr_id
,
5694 struct rtnl_offload_xstats_request_used
*ru
)
5696 struct nlattr
*nest
;
5698 nest
= nla_nest_start(skb
, attr_id
);
5702 if (nla_put_u8(skb
, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST
, ru
->request
))
5703 goto nla_put_failure
;
5705 if (nla_put_u8(skb
, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED
, ru
->used
))
5706 goto nla_put_failure
;
5708 nla_nest_end(skb
, nest
);
5712 nla_nest_cancel(skb
, nest
);
5717 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff
*skb
, struct net_device
*dev
,
5718 struct netlink_ext_ack
*extack
)
5720 enum netdev_offload_xstats_type t_l3
= NETDEV_OFFLOAD_XSTATS_TYPE_L3
;
5721 struct rtnl_offload_xstats_request_used ru_l3
;
5722 struct nlattr
*nest
;
5725 err
= rtnl_offload_xstats_get_stats(dev
, t_l3
, &ru_l3
, NULL
, extack
);
5729 nest
= nla_nest_start(skb
, IFLA_OFFLOAD_XSTATS_HW_S_INFO
);
5733 if (rtnl_offload_xstats_fill_hw_s_info_one(skb
,
5734 IFLA_OFFLOAD_XSTATS_L3_STATS
,
5736 goto nla_put_failure
;
5738 nla_nest_end(skb
, nest
);
5742 nla_nest_cancel(skb
, nest
);
5746 static int rtnl_offload_xstats_fill(struct sk_buff
*skb
, struct net_device
*dev
,
5747 int *prividx
, u32 off_filter_mask
,
5748 struct netlink_ext_ack
*extack
)
5750 enum netdev_offload_xstats_type t_l3
= NETDEV_OFFLOAD_XSTATS_TYPE_L3
;
5751 int attr_id_hw_s_info
= IFLA_OFFLOAD_XSTATS_HW_S_INFO
;
5752 int attr_id_l3_stats
= IFLA_OFFLOAD_XSTATS_L3_STATS
;
5753 int attr_id_cpu_hit
= IFLA_OFFLOAD_XSTATS_CPU_HIT
;
5754 bool have_data
= false;
5757 if (*prividx
<= attr_id_cpu_hit
&&
5759 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit
))) {
5760 err
= rtnl_offload_xstats_fill_ndo(dev
, attr_id_cpu_hit
, skb
);
5763 } else if (err
!= -ENODATA
) {
5764 *prividx
= attr_id_cpu_hit
;
5769 if (*prividx
<= attr_id_hw_s_info
&&
5770 (off_filter_mask
& IFLA_STATS_FILTER_BIT(attr_id_hw_s_info
))) {
5771 *prividx
= attr_id_hw_s_info
;
5773 err
= rtnl_offload_xstats_fill_hw_s_info(skb
, dev
, extack
);
5781 if (*prividx
<= attr_id_l3_stats
&&
5782 (off_filter_mask
& IFLA_STATS_FILTER_BIT(attr_id_l3_stats
))) {
5783 unsigned int size_l3
;
5784 struct nlattr
*attr
;
5786 *prividx
= attr_id_l3_stats
;
5788 size_l3
= rtnl_offload_xstats_get_size_stats(dev
, t_l3
);
5791 attr
= nla_reserve_64bit(skb
, attr_id_l3_stats
, size_l3
,
5792 IFLA_OFFLOAD_XSTATS_UNSPEC
);
5796 err
= rtnl_offload_xstats_get_stats(dev
, t_l3
, NULL
,
5797 nla_data(attr
), extack
);
5814 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device
*dev
,
5815 enum netdev_offload_xstats_type type
)
5817 return nla_total_size(0) +
5818 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
5819 nla_total_size(sizeof(u8
)) +
5820 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5821 nla_total_size(sizeof(u8
)) +
5826 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device
*dev
)
5828 enum netdev_offload_xstats_type t_l3
= NETDEV_OFFLOAD_XSTATS_TYPE_L3
;
5830 return nla_total_size(0) +
5831 /* IFLA_OFFLOAD_XSTATS_L3_STATS */
5832 rtnl_offload_xstats_get_size_hw_s_info_one(dev
, t_l3
) +
5836 static int rtnl_offload_xstats_get_size(const struct net_device
*dev
,
5837 u32 off_filter_mask
)
5839 enum netdev_offload_xstats_type t_l3
= NETDEV_OFFLOAD_XSTATS_TYPE_L3
;
5840 int attr_id_cpu_hit
= IFLA_OFFLOAD_XSTATS_CPU_HIT
;
5844 if (off_filter_mask
&
5845 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit
)) {
5846 size
= rtnl_offload_xstats_get_size_ndo(dev
, attr_id_cpu_hit
);
5847 nla_size
+= nla_total_size_64bit(size
);
5850 if (off_filter_mask
&
5851 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO
))
5852 nla_size
+= rtnl_offload_xstats_get_size_hw_s_info(dev
);
5854 if (off_filter_mask
&
5855 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS
)) {
5856 size
= rtnl_offload_xstats_get_size_stats(dev
, t_l3
);
5857 nla_size
+= nla_total_size_64bit(size
);
5861 nla_size
+= nla_total_size(0);
5866 struct rtnl_stats_dump_filters
{
5867 /* mask[0] filters outer attributes. Then individual nests have their
5868 * filtering mask at the index of the nested attribute.
5870 u32 mask
[IFLA_STATS_MAX
+ 1];
5873 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
5874 int type
, u32 pid
, u32 seq
, u32 change
,
5876 const struct rtnl_stats_dump_filters
*filters
,
5877 int *idxattr
, int *prividx
,
5878 struct netlink_ext_ack
*extack
)
5880 unsigned int filter_mask
= filters
->mask
[0];
5881 struct if_stats_msg
*ifsm
;
5882 struct nlmsghdr
*nlh
;
5883 struct nlattr
*attr
;
5884 int s_prividx
= *prividx
;
5889 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
5893 ifsm
= nlmsg_data(nlh
);
5894 ifsm
->family
= PF_UNSPEC
;
5897 ifsm
->ifindex
= dev
->ifindex
;
5898 ifsm
->filter_mask
= filter_mask
;
5900 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
5901 struct rtnl_link_stats64
*sp
;
5903 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
5904 sizeof(struct rtnl_link_stats64
),
5908 goto nla_put_failure
;
5911 sp
= nla_data(attr
);
5912 dev_get_stats(dev
, sp
);
5915 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
5916 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
5918 if (ops
&& ops
->fill_linkxstats
) {
5919 *idxattr
= IFLA_STATS_LINK_XSTATS
;
5920 attr
= nla_nest_start_noflag(skb
,
5921 IFLA_STATS_LINK_XSTATS
);
5924 goto nla_put_failure
;
5927 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5928 nla_nest_end(skb
, attr
);
5930 goto nla_put_failure
;
5935 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
5937 const struct rtnl_link_ops
*ops
= NULL
;
5938 const struct net_device
*master
;
5940 master
= netdev_master_upper_dev_get(dev
);
5942 ops
= master
->rtnl_link_ops
;
5943 if (ops
&& ops
->fill_linkxstats
) {
5944 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
5945 attr
= nla_nest_start_noflag(skb
,
5946 IFLA_STATS_LINK_XSTATS_SLAVE
);
5949 goto nla_put_failure
;
5952 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5953 nla_nest_end(skb
, attr
);
5955 goto nla_put_failure
;
5960 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
5962 u32 off_filter_mask
;
5964 off_filter_mask
= filters
->mask
[IFLA_STATS_LINK_OFFLOAD_XSTATS
];
5965 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
5966 attr
= nla_nest_start_noflag(skb
,
5967 IFLA_STATS_LINK_OFFLOAD_XSTATS
);
5970 goto nla_put_failure
;
5973 err
= rtnl_offload_xstats_fill(skb
, dev
, prividx
,
5974 off_filter_mask
, extack
);
5975 if (err
== -ENODATA
)
5976 nla_nest_cancel(skb
, attr
);
5978 nla_nest_end(skb
, attr
);
5980 if (err
&& err
!= -ENODATA
)
5981 goto nla_put_failure
;
5985 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
5986 struct rtnl_af_ops
*af_ops
;
5988 *idxattr
= IFLA_STATS_AF_SPEC
;
5989 attr
= nla_nest_start_noflag(skb
, IFLA_STATS_AF_SPEC
);
5992 goto nla_put_failure
;
5996 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
5997 if (af_ops
->fill_stats_af
) {
6000 af
= nla_nest_start_noflag(skb
,
6005 goto nla_put_failure
;
6007 err
= af_ops
->fill_stats_af(skb
, dev
);
6009 if (err
== -ENODATA
) {
6010 nla_nest_cancel(skb
, af
);
6011 } else if (err
< 0) {
6013 goto nla_put_failure
;
6016 nla_nest_end(skb
, af
);
6021 nla_nest_end(skb
, attr
);
6026 nlmsg_end(skb
, nlh
);
6031 /* not a multi message or no progress mean a real error */
6032 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
6033 nlmsg_cancel(skb
, nlh
);
6035 nlmsg_end(skb
, nlh
);
6040 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
6041 const struct rtnl_stats_dump_filters
*filters
)
6043 size_t size
= NLMSG_ALIGN(sizeof(struct if_stats_msg
));
6044 unsigned int filter_mask
= filters
->mask
[0];
6046 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
6047 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
6049 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
6050 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
6051 int attr
= IFLA_STATS_LINK_XSTATS
;
6053 if (ops
&& ops
->get_linkxstats_size
) {
6054 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
6056 /* for IFLA_STATS_LINK_XSTATS */
6057 size
+= nla_total_size(0);
6061 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
6062 struct net_device
*_dev
= (struct net_device
*)dev
;
6063 const struct rtnl_link_ops
*ops
= NULL
;
6064 const struct net_device
*master
;
6066 /* netdev_master_upper_dev_get can't take const */
6067 master
= netdev_master_upper_dev_get(_dev
);
6069 ops
= master
->rtnl_link_ops
;
6070 if (ops
&& ops
->get_linkxstats_size
) {
6071 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
6073 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
6075 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
6076 size
+= nla_total_size(0);
6080 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0)) {
6081 u32 off_filter_mask
;
6083 off_filter_mask
= filters
->mask
[IFLA_STATS_LINK_OFFLOAD_XSTATS
];
6084 size
+= rtnl_offload_xstats_get_size(dev
, off_filter_mask
);
6087 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
6088 struct rtnl_af_ops
*af_ops
;
6090 /* for IFLA_STATS_AF_SPEC */
6091 size
+= nla_total_size(0);
6094 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
6095 if (af_ops
->get_stats_af_size
) {
6096 size
+= nla_total_size(
6097 af_ops
->get_stats_af_size(dev
));
6100 size
+= nla_total_size(0);
6109 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
6111 static const struct nla_policy
6112 rtnl_stats_get_policy_filters
[IFLA_STATS_MAX
+ 1] = {
6113 [IFLA_STATS_LINK_OFFLOAD_XSTATS
] =
6114 NLA_POLICY_MASK(NLA_U32
, RTNL_STATS_OFFLOAD_XSTATS_VALID
),
6117 static const struct nla_policy
6118 rtnl_stats_get_policy
[IFLA_STATS_GETSET_MAX
+ 1] = {
6119 [IFLA_STATS_GET_FILTERS
] =
6120 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters
),
6123 static const struct nla_policy
6124 ifla_stats_set_policy
[IFLA_STATS_GETSET_MAX
+ 1] = {
6125 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS
] = NLA_POLICY_MAX(NLA_U8
, 1),
6128 static int rtnl_stats_get_parse_filters(struct nlattr
*ifla_filters
,
6129 struct rtnl_stats_dump_filters
*filters
,
6130 struct netlink_ext_ack
*extack
)
6132 struct nlattr
*tb
[IFLA_STATS_MAX
+ 1];
6136 err
= nla_parse_nested(tb
, IFLA_STATS_MAX
, ifla_filters
,
6137 rtnl_stats_get_policy_filters
, extack
);
6141 for (at
= 1; at
<= IFLA_STATS_MAX
; at
++) {
6143 if (!(filters
->mask
[0] & IFLA_STATS_FILTER_BIT(at
))) {
6144 NL_SET_ERR_MSG(extack
, "Filtered attribute not enabled in filter_mask");
6147 filters
->mask
[at
] = nla_get_u32(tb
[at
]);
6154 static int rtnl_stats_get_parse(const struct nlmsghdr
*nlh
,
6156 struct rtnl_stats_dump_filters
*filters
,
6157 struct netlink_ext_ack
*extack
)
6159 struct nlattr
*tb
[IFLA_STATS_GETSET_MAX
+ 1];
6163 filters
->mask
[0] = filter_mask
;
6164 for (i
= 1; i
< ARRAY_SIZE(filters
->mask
); i
++)
6165 filters
->mask
[i
] = -1U;
6167 err
= nlmsg_parse(nlh
, sizeof(struct if_stats_msg
), tb
,
6168 IFLA_STATS_GETSET_MAX
, rtnl_stats_get_policy
, extack
);
6172 if (tb
[IFLA_STATS_GET_FILTERS
]) {
6173 err
= rtnl_stats_get_parse_filters(tb
[IFLA_STATS_GET_FILTERS
],
6182 static int rtnl_valid_stats_req(const struct nlmsghdr
*nlh
, bool strict_check
,
6183 bool is_dump
, struct netlink_ext_ack
*extack
)
6185 struct if_stats_msg
*ifsm
;
6187 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifsm
))) {
6188 NL_SET_ERR_MSG(extack
, "Invalid header for stats dump");
6195 ifsm
= nlmsg_data(nlh
);
6197 /* only requests using strict checks can pass data to influence
6198 * the dump. The legacy exception is filter_mask.
6200 if (ifsm
->pad1
|| ifsm
->pad2
|| (is_dump
&& ifsm
->ifindex
)) {
6201 NL_SET_ERR_MSG(extack
, "Invalid values in header for stats dump request");
6204 if (ifsm
->filter_mask
>= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX
+ 1)) {
6205 NL_SET_ERR_MSG(extack
, "Invalid stats requested through filter mask");
6212 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
6213 struct netlink_ext_ack
*extack
)
6215 struct rtnl_stats_dump_filters filters
;
6216 struct net
*net
= sock_net(skb
->sk
);
6217 struct net_device
*dev
= NULL
;
6218 int idxattr
= 0, prividx
= 0;
6219 struct if_stats_msg
*ifsm
;
6220 struct sk_buff
*nskb
;
6223 err
= rtnl_valid_stats_req(nlh
, netlink_strict_get_check(skb
),
6228 ifsm
= nlmsg_data(nlh
);
6229 if (ifsm
->ifindex
> 0)
6230 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
6237 if (!ifsm
->filter_mask
) {
6238 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats get");
6242 err
= rtnl_stats_get_parse(nlh
, ifsm
->filter_mask
, &filters
, extack
);
6246 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, &filters
), GFP_KERNEL
);
6250 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
6251 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
6252 0, &filters
, &idxattr
, &prividx
, extack
);
6254 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
6255 WARN_ON(err
== -EMSGSIZE
);
6258 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
6264 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
6266 struct netlink_ext_ack
*extack
= cb
->extack
;
6267 struct rtnl_stats_dump_filters filters
;
6268 struct net
*net
= sock_net(skb
->sk
);
6269 unsigned int flags
= NLM_F_MULTI
;
6270 struct if_stats_msg
*ifsm
;
6272 unsigned long ifindex
;
6275 } *ctx
= (void *)cb
->ctx
;
6276 struct net_device
*dev
;
6279 cb
->seq
= net
->dev_base_seq
;
6281 err
= rtnl_valid_stats_req(cb
->nlh
, cb
->strict_check
, true, extack
);
6285 ifsm
= nlmsg_data(cb
->nlh
);
6286 if (!ifsm
->filter_mask
) {
6287 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats dump");
6291 err
= rtnl_stats_get_parse(cb
->nlh
, ifsm
->filter_mask
, &filters
,
6296 for_each_netdev_dump(net
, dev
, ctx
->ifindex
) {
6297 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
6298 NETLINK_CB(cb
->skb
).portid
,
6299 cb
->nlh
->nlmsg_seq
, 0,
6301 &ctx
->idxattr
, &ctx
->prividx
,
6303 /* If we ran out of room on the first message,
6306 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
6312 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
6318 void rtnl_offload_xstats_notify(struct net_device
*dev
)
6320 struct rtnl_stats_dump_filters response_filters
= {};
6321 struct net
*net
= dev_net(dev
);
6322 int idxattr
= 0, prividx
= 0;
6323 struct sk_buff
*skb
;
6328 response_filters
.mask
[0] |=
6329 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS
);
6330 response_filters
.mask
[IFLA_STATS_LINK_OFFLOAD_XSTATS
] |=
6331 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO
);
6333 skb
= nlmsg_new(if_nlmsg_stats_size(dev
, &response_filters
),
6338 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
, 0, 0, 0, 0,
6339 &response_filters
, &idxattr
, &prividx
, NULL
);
6345 rtnl_notify(skb
, net
, 0, RTNLGRP_STATS
, NULL
, GFP_KERNEL
);
6349 rtnl_set_sk_err(net
, RTNLGRP_STATS
, err
);
6351 EXPORT_SYMBOL(rtnl_offload_xstats_notify
);
6353 static int rtnl_stats_set(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
6354 struct netlink_ext_ack
*extack
)
6356 enum netdev_offload_xstats_type t_l3
= NETDEV_OFFLOAD_XSTATS_TYPE_L3
;
6357 struct rtnl_stats_dump_filters response_filters
= {};
6358 struct nlattr
*tb
[IFLA_STATS_GETSET_MAX
+ 1];
6359 struct net
*net
= sock_net(skb
->sk
);
6360 struct net_device
*dev
= NULL
;
6361 struct if_stats_msg
*ifsm
;
6362 bool notify
= false;
6365 err
= rtnl_valid_stats_req(nlh
, netlink_strict_get_check(skb
),
6370 ifsm
= nlmsg_data(nlh
);
6371 if (ifsm
->family
!= AF_UNSPEC
) {
6372 NL_SET_ERR_MSG(extack
, "Address family should be AF_UNSPEC");
6376 if (ifsm
->ifindex
> 0)
6377 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
6384 if (ifsm
->filter_mask
) {
6385 NL_SET_ERR_MSG(extack
, "Filter mask must be 0 for stats set");
6389 err
= nlmsg_parse(nlh
, sizeof(*ifsm
), tb
, IFLA_STATS_GETSET_MAX
,
6390 ifla_stats_set_policy
, extack
);
6394 if (tb
[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS
]) {
6395 u8 req
= nla_get_u8(tb
[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS
]);
6398 err
= netdev_offload_xstats_enable(dev
, t_l3
, extack
);
6400 err
= netdev_offload_xstats_disable(dev
, t_l3
);
6404 else if (err
!= -EALREADY
)
6407 response_filters
.mask
[0] |=
6408 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS
);
6409 response_filters
.mask
[IFLA_STATS_LINK_OFFLOAD_XSTATS
] |=
6410 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO
);
6414 rtnl_offload_xstats_notify(dev
);
6419 static int rtnl_mdb_valid_dump_req(const struct nlmsghdr
*nlh
,
6420 struct netlink_ext_ack
*extack
)
6422 struct br_port_msg
*bpm
;
6424 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*bpm
))) {
6425 NL_SET_ERR_MSG(extack
, "Invalid header for mdb dump request");
6429 bpm
= nlmsg_data(nlh
);
6431 NL_SET_ERR_MSG(extack
, "Filtering by device index is not supported for mdb dump request");
6434 if (nlmsg_attrlen(nlh
, sizeof(*bpm
))) {
6435 NL_SET_ERR_MSG(extack
, "Invalid data after header in mdb dump request");
6442 struct rtnl_mdb_dump_ctx
{
6446 static int rtnl_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
6448 struct rtnl_mdb_dump_ctx
*ctx
= (void *)cb
->ctx
;
6449 struct net
*net
= sock_net(skb
->sk
);
6450 struct net_device
*dev
;
6454 NL_ASSERT_CTX_FITS(struct rtnl_mdb_dump_ctx
);
6456 if (cb
->strict_check
) {
6457 err
= rtnl_mdb_valid_dump_req(cb
->nlh
, cb
->extack
);
6465 for_each_netdev(net
, dev
) {
6468 if (!dev
->netdev_ops
->ndo_mdb_dump
)
6471 err
= dev
->netdev_ops
->ndo_mdb_dump(dev
, skb
, cb
);
6472 if (err
== -EMSGSIZE
)
6474 /* Moving on to next device, reset markers and sequence
6475 * counters since they are all maintained per-device.
6477 memset(cb
->ctx
, 0, sizeof(cb
->ctx
));
6489 static int rtnl_validate_mdb_entry_get(const struct nlattr
*attr
,
6490 struct netlink_ext_ack
*extack
)
6492 struct br_mdb_entry
*entry
= nla_data(attr
);
6494 if (nla_len(attr
) != sizeof(struct br_mdb_entry
)) {
6495 NL_SET_ERR_MSG_ATTR(extack
, attr
, "Invalid attribute length");
6499 if (entry
->ifindex
) {
6500 NL_SET_ERR_MSG(extack
, "Entry ifindex cannot be specified");
6505 NL_SET_ERR_MSG(extack
, "Entry state cannot be specified");
6510 NL_SET_ERR_MSG(extack
, "Entry flags cannot be specified");
6514 if (entry
->vid
>= VLAN_VID_MASK
) {
6515 NL_SET_ERR_MSG(extack
, "Invalid entry VLAN id");
6519 if (entry
->addr
.proto
!= htons(ETH_P_IP
) &&
6520 entry
->addr
.proto
!= htons(ETH_P_IPV6
) &&
6521 entry
->addr
.proto
!= 0) {
6522 NL_SET_ERR_MSG(extack
, "Unknown entry protocol");
6529 static const struct nla_policy mdba_get_policy
[MDBA_GET_ENTRY_MAX
+ 1] = {
6530 [MDBA_GET_ENTRY
] = NLA_POLICY_VALIDATE_FN(NLA_BINARY
,
6531 rtnl_validate_mdb_entry_get
,
6532 sizeof(struct br_mdb_entry
)),
6533 [MDBA_GET_ENTRY_ATTRS
] = { .type
= NLA_NESTED
},
6536 static int rtnl_mdb_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
6537 struct netlink_ext_ack
*extack
)
6539 struct nlattr
*tb
[MDBA_GET_ENTRY_MAX
+ 1];
6540 struct net
*net
= sock_net(in_skb
->sk
);
6541 struct br_port_msg
*bpm
;
6542 struct net_device
*dev
;
6545 err
= nlmsg_parse(nlh
, sizeof(struct br_port_msg
), tb
,
6546 MDBA_GET_ENTRY_MAX
, mdba_get_policy
, extack
);
6550 bpm
= nlmsg_data(nlh
);
6551 if (!bpm
->ifindex
) {
6552 NL_SET_ERR_MSG(extack
, "Invalid ifindex");
6556 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
6558 NL_SET_ERR_MSG(extack
, "Device doesn't exist");
6562 if (NL_REQ_ATTR_CHECK(extack
, NULL
, tb
, MDBA_GET_ENTRY
)) {
6563 NL_SET_ERR_MSG(extack
, "Missing MDBA_GET_ENTRY attribute");
6567 if (!dev
->netdev_ops
->ndo_mdb_get
) {
6568 NL_SET_ERR_MSG(extack
, "Device does not support MDB operations");
6572 return dev
->netdev_ops
->ndo_mdb_get(dev
, tb
, NETLINK_CB(in_skb
).portid
,
6573 nlh
->nlmsg_seq
, extack
);
6576 static int rtnl_validate_mdb_entry(const struct nlattr
*attr
,
6577 struct netlink_ext_ack
*extack
)
6579 struct br_mdb_entry
*entry
= nla_data(attr
);
6581 if (nla_len(attr
) != sizeof(struct br_mdb_entry
)) {
6582 NL_SET_ERR_MSG_ATTR(extack
, attr
, "Invalid attribute length");
6586 if (entry
->ifindex
== 0) {
6587 NL_SET_ERR_MSG(extack
, "Zero entry ifindex is not allowed");
6591 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
6592 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
) &&
6593 !ipv4_is_zeronet(entry
->addr
.u
.ip4
)) {
6594 NL_SET_ERR_MSG(extack
, "IPv4 entry group address is not multicast or 0.0.0.0");
6597 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
)) {
6598 NL_SET_ERR_MSG(extack
, "IPv4 entry group address is local multicast");
6601 #if IS_ENABLED(CONFIG_IPV6)
6602 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
6603 if (ipv6_addr_is_ll_all_nodes(&entry
->addr
.u
.ip6
)) {
6604 NL_SET_ERR_MSG(extack
, "IPv6 entry group address is link-local all nodes");
6608 } else if (entry
->addr
.proto
== 0) {
6610 if (!is_multicast_ether_addr(entry
->addr
.u
.mac_addr
)) {
6611 NL_SET_ERR_MSG(extack
, "L2 entry group is not multicast");
6615 NL_SET_ERR_MSG(extack
, "Unknown entry protocol");
6619 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
) {
6620 NL_SET_ERR_MSG(extack
, "Unknown entry state");
6623 if (entry
->vid
>= VLAN_VID_MASK
) {
6624 NL_SET_ERR_MSG(extack
, "Invalid entry VLAN id");
6631 static const struct nla_policy mdba_policy
[MDBA_SET_ENTRY_MAX
+ 1] = {
6632 [MDBA_SET_ENTRY_UNSPEC
] = { .strict_start_type
= MDBA_SET_ENTRY_ATTRS
+ 1 },
6633 [MDBA_SET_ENTRY
] = NLA_POLICY_VALIDATE_FN(NLA_BINARY
,
6634 rtnl_validate_mdb_entry
,
6635 sizeof(struct br_mdb_entry
)),
6636 [MDBA_SET_ENTRY_ATTRS
] = { .type
= NLA_NESTED
},
6639 static int rtnl_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
6640 struct netlink_ext_ack
*extack
)
6642 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+ 1];
6643 struct net
*net
= sock_net(skb
->sk
);
6644 struct br_port_msg
*bpm
;
6645 struct net_device
*dev
;
6648 err
= nlmsg_parse_deprecated(nlh
, sizeof(*bpm
), tb
,
6649 MDBA_SET_ENTRY_MAX
, mdba_policy
, extack
);
6653 bpm
= nlmsg_data(nlh
);
6654 if (!bpm
->ifindex
) {
6655 NL_SET_ERR_MSG(extack
, "Invalid ifindex");
6659 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
6661 NL_SET_ERR_MSG(extack
, "Device doesn't exist");
6665 if (NL_REQ_ATTR_CHECK(extack
, NULL
, tb
, MDBA_SET_ENTRY
)) {
6666 NL_SET_ERR_MSG(extack
, "Missing MDBA_SET_ENTRY attribute");
6670 if (!dev
->netdev_ops
->ndo_mdb_add
) {
6671 NL_SET_ERR_MSG(extack
, "Device does not support MDB operations");
6675 return dev
->netdev_ops
->ndo_mdb_add(dev
, tb
, nlh
->nlmsg_flags
, extack
);
6678 static int rtnl_validate_mdb_entry_del_bulk(const struct nlattr
*attr
,
6679 struct netlink_ext_ack
*extack
)
6681 struct br_mdb_entry
*entry
= nla_data(attr
);
6682 struct br_mdb_entry zero_entry
= {};
6684 if (nla_len(attr
) != sizeof(struct br_mdb_entry
)) {
6685 NL_SET_ERR_MSG_ATTR(extack
, attr
, "Invalid attribute length");
6689 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
) {
6690 NL_SET_ERR_MSG(extack
, "Unknown entry state");
6695 NL_SET_ERR_MSG(extack
, "Entry flags cannot be set");
6699 if (entry
->vid
>= VLAN_N_VID
- 1) {
6700 NL_SET_ERR_MSG(extack
, "Invalid entry VLAN id");
6704 if (memcmp(&entry
->addr
, &zero_entry
.addr
, sizeof(entry
->addr
))) {
6705 NL_SET_ERR_MSG(extack
, "Entry address cannot be set");
6712 static const struct nla_policy mdba_del_bulk_policy
[MDBA_SET_ENTRY_MAX
+ 1] = {
6713 [MDBA_SET_ENTRY
] = NLA_POLICY_VALIDATE_FN(NLA_BINARY
,
6714 rtnl_validate_mdb_entry_del_bulk
,
6715 sizeof(struct br_mdb_entry
)),
6716 [MDBA_SET_ENTRY_ATTRS
] = { .type
= NLA_NESTED
},
6719 static int rtnl_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
6720 struct netlink_ext_ack
*extack
)
6722 bool del_bulk
= !!(nlh
->nlmsg_flags
& NLM_F_BULK
);
6723 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+ 1];
6724 struct net
*net
= sock_net(skb
->sk
);
6725 struct br_port_msg
*bpm
;
6726 struct net_device
*dev
;
6730 err
= nlmsg_parse_deprecated(nlh
, sizeof(*bpm
), tb
,
6731 MDBA_SET_ENTRY_MAX
, mdba_policy
,
6734 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY_MAX
,
6735 mdba_del_bulk_policy
, extack
);
6739 bpm
= nlmsg_data(nlh
);
6740 if (!bpm
->ifindex
) {
6741 NL_SET_ERR_MSG(extack
, "Invalid ifindex");
6745 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
6747 NL_SET_ERR_MSG(extack
, "Device doesn't exist");
6751 if (NL_REQ_ATTR_CHECK(extack
, NULL
, tb
, MDBA_SET_ENTRY
)) {
6752 NL_SET_ERR_MSG(extack
, "Missing MDBA_SET_ENTRY attribute");
6757 if (!dev
->netdev_ops
->ndo_mdb_del_bulk
) {
6758 NL_SET_ERR_MSG(extack
, "Device does not support MDB bulk deletion");
6761 return dev
->netdev_ops
->ndo_mdb_del_bulk(dev
, tb
, extack
);
6764 if (!dev
->netdev_ops
->ndo_mdb_del
) {
6765 NL_SET_ERR_MSG(extack
, "Device does not support MDB operations");
6769 return dev
->netdev_ops
->ndo_mdb_del(dev
, tb
, extack
);
6772 /* Process one rtnetlink message. */
6774 static int rtnl_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
6776 const bool needs_lock
= !(cb
->flags
& RTNL_FLAG_DUMP_UNLOCKED
);
6777 rtnl_dumpit_func dumpit
= cb
->data
;
6780 /* Previous iteration have already finished, avoid calling->dumpit()
6781 * again, it may not expect to be called after it reached the end.
6788 err
= dumpit(skb
, cb
);
6792 /* Old dump handlers used to send NLM_DONE as in a separate recvmsg().
6793 * Some applications which parse netlink manually depend on this.
6795 if (cb
->flags
& RTNL_FLAG_DUMP_SPLIT_NLM_DONE
) {
6796 if (err
< 0 && err
!= -EMSGSIZE
)
6806 static int rtnetlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
6807 const struct nlmsghdr
*nlh
,
6808 struct netlink_dump_control
*control
)
6810 if (control
->flags
& RTNL_FLAG_DUMP_SPLIT_NLM_DONE
||
6811 !(control
->flags
& RTNL_FLAG_DUMP_UNLOCKED
)) {
6812 WARN_ON(control
->data
);
6813 control
->data
= control
->dump
;
6814 control
->dump
= rtnl_dumpit
;
6817 return netlink_dump_start(ssk
, skb
, nlh
, control
);
6820 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
6821 struct netlink_ext_ack
*extack
)
6823 struct net
*net
= sock_net(skb
->sk
);
6824 struct rtnl_link
*link
;
6825 enum rtnl_kinds kind
;
6826 struct module
*owner
;
6827 int err
= -EOPNOTSUPP
;
6828 rtnl_doit_func doit
;
6833 type
= nlh
->nlmsg_type
;
6839 /* All the messages must have at least 1 byte length */
6840 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
6843 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
6844 kind
= rtnl_msgtype_kind(type
);
6846 if (kind
!= RTNL_KIND_GET
&& !netlink_net_capable(skb
, CAP_NET_ADMIN
))
6850 if (kind
== RTNL_KIND_GET
&& (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
6852 rtnl_dumpit_func dumpit
;
6853 u32 min_dump_alloc
= 0;
6855 link
= rtnl_get_link(family
, type
);
6856 if (!link
|| !link
->dumpit
) {
6858 link
= rtnl_get_link(family
, type
);
6859 if (!link
|| !link
->dumpit
)
6862 owner
= link
->owner
;
6863 dumpit
= link
->dumpit
;
6864 flags
= link
->flags
;
6866 if (type
== RTM_GETLINK
- RTM_BASE
)
6867 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
6870 /* need to do this before rcu_read_unlock() */
6871 if (!try_module_get(owner
))
6872 err
= -EPROTONOSUPPORT
;
6878 struct netlink_dump_control c
= {
6880 .min_dump_alloc
= min_dump_alloc
,
6884 err
= rtnetlink_dump_start(rtnl
, skb
, nlh
, &c
);
6885 /* netlink_dump_start() will keep a reference on
6886 * module if dump is still in progress.
6893 link
= rtnl_get_link(family
, type
);
6894 if (!link
|| !link
->doit
) {
6896 link
= rtnl_get_link(PF_UNSPEC
, type
);
6897 if (!link
|| !link
->doit
)
6901 owner
= link
->owner
;
6902 if (!try_module_get(owner
)) {
6903 err
= -EPROTONOSUPPORT
;
6907 flags
= link
->flags
;
6908 if (kind
== RTNL_KIND_DEL
&& (nlh
->nlmsg_flags
& NLM_F_BULK
) &&
6909 !(flags
& RTNL_FLAG_BULK_DEL_SUPPORTED
)) {
6910 NL_SET_ERR_MSG(extack
, "Bulk delete is not supported");
6915 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
6919 err
= doit(skb
, nlh
, extack
);
6926 link
= rtnl_get_link(family
, type
);
6927 if (link
&& link
->doit
)
6928 err
= link
->doit(skb
, nlh
, extack
);
6944 static void rtnetlink_rcv(struct sk_buff
*skb
)
6946 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
6949 static int rtnetlink_bind(struct net
*net
, int group
)
6952 case RTNLGRP_IPV4_MROUTE_R
:
6953 case RTNLGRP_IPV6_MROUTE_R
:
6954 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
6961 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
6963 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
6967 case NETDEV_CHANGEMTU
:
6968 case NETDEV_CHANGEADDR
:
6969 case NETDEV_CHANGENAME
:
6970 case NETDEV_FEAT_CHANGE
:
6971 case NETDEV_BONDING_FAILOVER
:
6972 case NETDEV_POST_TYPE_CHANGE
:
6973 case NETDEV_NOTIFY_PEERS
:
6974 case NETDEV_CHANGEUPPER
:
6975 case NETDEV_RESEND_IGMP
:
6976 case NETDEV_CHANGEINFODATA
:
6977 case NETDEV_CHANGELOWERSTATE
:
6978 case NETDEV_CHANGE_TX_QUEUE_LEN
:
6979 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
6980 GFP_KERNEL
, NULL
, 0, 0, NULL
);
6988 static struct notifier_block rtnetlink_dev_notifier
= {
6989 .notifier_call
= rtnetlink_event
,
6993 static int __net_init
rtnetlink_net_init(struct net
*net
)
6996 struct netlink_kernel_cfg cfg
= {
6997 .groups
= RTNLGRP_MAX
,
6998 .input
= rtnetlink_rcv
,
6999 .flags
= NL_CFG_F_NONROOT_RECV
,
7000 .bind
= rtnetlink_bind
,
7003 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
7010 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
7012 netlink_kernel_release(net
->rtnl
);
7016 static struct pernet_operations rtnetlink_net_ops
= {
7017 .init
= rtnetlink_net_init
,
7018 .exit
= rtnetlink_net_exit
,
7021 static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers
[] __initconst
= {
7022 {.msgtype
= RTM_NEWLINK
, .doit
= rtnl_newlink
,
7023 .flags
= RTNL_FLAG_DOIT_PERNET
},
7024 {.msgtype
= RTM_DELLINK
, .doit
= rtnl_dellink
,
7025 .flags
= RTNL_FLAG_DOIT_PERNET_WIP
},
7026 {.msgtype
= RTM_GETLINK
, .doit
= rtnl_getlink
,
7027 .dumpit
= rtnl_dump_ifinfo
, .flags
= RTNL_FLAG_DUMP_SPLIT_NLM_DONE
},
7028 {.msgtype
= RTM_SETLINK
, .doit
= rtnl_setlink
,
7029 .flags
= RTNL_FLAG_DOIT_PERNET_WIP
},
7030 {.msgtype
= RTM_GETADDR
, .dumpit
= rtnl_dump_all
},
7031 {.msgtype
= RTM_GETROUTE
, .dumpit
= rtnl_dump_all
},
7032 {.msgtype
= RTM_GETNETCONF
, .dumpit
= rtnl_dump_all
},
7033 {.msgtype
= RTM_GETSTATS
, .doit
= rtnl_stats_get
,
7034 .dumpit
= rtnl_stats_dump
},
7035 {.msgtype
= RTM_SETSTATS
, .doit
= rtnl_stats_set
},
7036 {.msgtype
= RTM_NEWLINKPROP
, .doit
= rtnl_newlinkprop
},
7037 {.msgtype
= RTM_DELLINKPROP
, .doit
= rtnl_dellinkprop
},
7038 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_GETLINK
,
7039 .dumpit
= rtnl_bridge_getlink
},
7040 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_DELLINK
,
7041 .doit
= rtnl_bridge_dellink
},
7042 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_SETLINK
,
7043 .doit
= rtnl_bridge_setlink
},
7044 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_NEWNEIGH
, .doit
= rtnl_fdb_add
},
7045 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_DELNEIGH
, .doit
= rtnl_fdb_del
,
7046 .flags
= RTNL_FLAG_BULK_DEL_SUPPORTED
},
7047 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_GETNEIGH
, .doit
= rtnl_fdb_get
,
7048 .dumpit
= rtnl_fdb_dump
},
7049 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_NEWMDB
, .doit
= rtnl_mdb_add
},
7050 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_DELMDB
, .doit
= rtnl_mdb_del
,
7051 .flags
= RTNL_FLAG_BULK_DEL_SUPPORTED
},
7052 {.protocol
= PF_BRIDGE
, .msgtype
= RTM_GETMDB
, .doit
= rtnl_mdb_get
,
7053 .dumpit
= rtnl_mdb_dump
},
7056 void __init
rtnetlink_init(void)
7058 if (register_pernet_subsys(&rtnetlink_net_ops
))
7059 panic("rtnetlink_init: cannot initialize rtnetlink\n");
7061 register_netdevice_notifier(&rtnetlink_dev_notifier
);
7063 rtnl_register_many(rtnetlink_rtnl_msg_handlers
);