1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
62 rtnl_dumpit_func dumpit
;
68 static DEFINE_MUTEX(rtnl_mutex
);
72 mutex_lock(&rtnl_mutex
);
74 EXPORT_SYMBOL(rtnl_lock
);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex
);
80 EXPORT_SYMBOL(rtnl_lock_killable
);
82 static struct sk_buff
*defer_kfree_skb_list
;
83 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
86 tail
->next
= defer_kfree_skb_list
;
87 defer_kfree_skb_list
= head
;
90 EXPORT_SYMBOL(rtnl_kfree_skbs
);
92 void __rtnl_unlock(void)
94 struct sk_buff
*head
= defer_kfree_skb_list
;
96 defer_kfree_skb_list
= NULL
;
98 mutex_unlock(&rtnl_mutex
);
101 struct sk_buff
*next
= head
->next
;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock
);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex
);
120 EXPORT_SYMBOL(rtnl_trylock
);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex
);
126 EXPORT_SYMBOL(rtnl_is_locked
);
128 bool refcount_dec_and_rtnl_lock(refcount_t
*r
)
130 return refcount_dec_and_mutex_lock(r
, &rtnl_mutex
);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock
);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex
);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link __rcu
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
144 static inline int rtm_msgindex(int msgtype
)
146 int msgindex
= msgtype
- RTM_BASE
;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
158 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
160 struct rtnl_link __rcu
**tab
;
162 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
163 protocol
= PF_UNSPEC
;
165 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
167 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
169 return rcu_dereference_rtnl(tab
[msgtype
]);
172 static int rtnl_register_internal(struct module
*owner
,
173 int protocol
, int msgtype
,
174 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
177 struct rtnl_link
*link
, *old
;
178 struct rtnl_link __rcu
**tab
;
182 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
183 msgindex
= rtm_msgindex(msgtype
);
186 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
188 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
196 old
= rtnl_dereference(tab
[msgindex
]);
198 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
202 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
207 WARN_ON(link
->owner
&& link
->owner
!= owner
);
210 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
213 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
215 link
->dumpit
= dumpit
;
217 link
->flags
|= flags
;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab
[msgindex
], link
);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module
*owner
,
242 int protocol
, int msgtype
,
243 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
246 return rtnl_register_internal(owner
, protocol
, msgtype
,
247 doit
, dumpit
, flags
);
249 EXPORT_SYMBOL_GPL(rtnl_register_module
);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol
, int msgtype
,
268 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
273 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol
, msgtype
);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol
, int msgtype
)
289 struct rtnl_link __rcu
**tab
;
290 struct rtnl_link
*link
;
293 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
294 msgindex
= rtm_msgindex(msgtype
);
297 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
303 link
= rtnl_dereference(tab
[msgindex
]);
304 rcu_assign_pointer(tab
[msgindex
], NULL
);
307 kfree_rcu(link
, rcu
);
311 EXPORT_SYMBOL_GPL(rtnl_unregister
);
314 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
315 * @protocol : Protocol family or PF_UNSPEC
317 * Identical to calling rtnl_unregster() for all registered message types
318 * of a certain protocol family.
320 void rtnl_unregister_all(int protocol
)
322 struct rtnl_link __rcu
**tab
;
323 struct rtnl_link
*link
;
326 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
329 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
334 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
335 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
336 link
= rtnl_dereference(tab
[msgindex
]);
340 rcu_assign_pointer(tab
[msgindex
], NULL
);
341 kfree_rcu(link
, rcu
);
349 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
351 static LIST_HEAD(link_ops
);
353 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
355 const struct rtnl_link_ops
*ops
;
357 list_for_each_entry(ops
, &link_ops
, list
) {
358 if (!strcmp(ops
->kind
, kind
))
365 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
366 * @ops: struct rtnl_link_ops * to register
368 * The caller must hold the rtnl_mutex. This function should be used
369 * by drivers that create devices during module initialization. It
370 * must be called before registering the devices.
372 * Returns 0 on success or a negative error code.
374 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
376 if (rtnl_link_ops_get(ops
->kind
))
379 /* The check for setup is here because if ops
380 * does not have that filled up, it is not possible
381 * to use the ops for creating device. So do not
382 * fill up dellink as well. That disables rtnl_dellink.
384 if (ops
->setup
&& !ops
->dellink
)
385 ops
->dellink
= unregister_netdevice_queue
;
387 list_add_tail(&ops
->list
, &link_ops
);
390 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
393 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
394 * @ops: struct rtnl_link_ops * to register
396 * Returns 0 on success or a negative error code.
398 int rtnl_link_register(struct rtnl_link_ops
*ops
)
402 /* Sanity-check max sizes to avoid stack buffer overflow. */
403 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
404 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
408 err
= __rtnl_link_register(ops
);
412 EXPORT_SYMBOL_GPL(rtnl_link_register
);
414 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
416 struct net_device
*dev
;
417 LIST_HEAD(list_kill
);
419 for_each_netdev(net
, dev
) {
420 if (dev
->rtnl_link_ops
== ops
)
421 ops
->dellink(dev
, &list_kill
);
423 unregister_netdevice_many(&list_kill
);
427 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
428 * @ops: struct rtnl_link_ops * to unregister
430 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
431 * integrity (hold pernet_ops_rwsem for writing to close the race
432 * with setup_net() and cleanup_net()).
434 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
439 __rtnl_kill_links(net
, ops
);
441 list_del(&ops
->list
);
443 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
445 /* Return with the rtnl_lock held when there are no network
446 * devices unregistering in any network namespace.
448 static void rtnl_lock_unregistering_all(void)
452 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
454 add_wait_queue(&netdev_unregistering_wq
, &wait
);
456 unregistering
= false;
458 /* We held write locked pernet_ops_rwsem, and parallel
459 * setup_net() and cleanup_net() are not possible.
462 if (net
->dev_unreg_count
> 0) {
463 unregistering
= true;
471 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
473 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
477 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
478 * @ops: struct rtnl_link_ops * to unregister
480 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
482 /* Close the race with setup_net() and cleanup_net() */
483 down_write(&pernet_ops_rwsem
);
484 rtnl_lock_unregistering_all();
485 __rtnl_link_unregister(ops
);
487 up_write(&pernet_ops_rwsem
);
489 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
491 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
493 struct net_device
*master_dev
;
494 const struct rtnl_link_ops
*ops
;
499 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
503 ops
= master_dev
->rtnl_link_ops
;
504 if (!ops
|| !ops
->get_slave_size
)
506 /* IFLA_INFO_SLAVE_DATA + nested data */
507 size
= nla_total_size(sizeof(struct nlattr
)) +
508 ops
->get_slave_size(master_dev
, dev
);
515 static size_t rtnl_link_get_size(const struct net_device
*dev
)
517 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
523 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
524 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
527 /* IFLA_INFO_DATA + nested data */
528 size
+= nla_total_size(sizeof(struct nlattr
)) +
531 if (ops
->get_xstats_size
)
532 /* IFLA_INFO_XSTATS */
533 size
+= nla_total_size(ops
->get_xstats_size(dev
));
535 size
+= rtnl_link_get_slave_info_data_size(dev
);
540 static LIST_HEAD(rtnl_af_ops
);
542 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
544 const struct rtnl_af_ops
*ops
;
546 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
547 if (ops
->family
== family
)
555 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
556 * @ops: struct rtnl_af_ops * to register
558 * Returns 0 on success or a negative error code.
560 void rtnl_af_register(struct rtnl_af_ops
*ops
)
563 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
566 EXPORT_SYMBOL_GPL(rtnl_af_register
);
569 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
570 * @ops: struct rtnl_af_ops * to unregister
572 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
575 list_del_rcu(&ops
->list
);
580 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
582 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
585 struct rtnl_af_ops
*af_ops
;
589 size
= nla_total_size(sizeof(struct nlattr
));
592 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
593 if (af_ops
->get_link_af_size
) {
594 /* AF_* + nested data */
595 size
+= nla_total_size(sizeof(struct nlattr
)) +
596 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
604 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
606 struct net_device
*master_dev
;
611 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
612 if (master_dev
&& master_dev
->rtnl_link_ops
)
618 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
619 const struct net_device
*dev
)
621 struct net_device
*master_dev
;
622 const struct rtnl_link_ops
*ops
;
623 struct nlattr
*slave_data
;
626 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
629 ops
= master_dev
->rtnl_link_ops
;
632 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
634 if (ops
->fill_slave_info
) {
635 slave_data
= nla_nest_start_noflag(skb
, IFLA_INFO_SLAVE_DATA
);
638 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
640 goto err_cancel_slave_data
;
641 nla_nest_end(skb
, slave_data
);
645 err_cancel_slave_data
:
646 nla_nest_cancel(skb
, slave_data
);
650 static int rtnl_link_info_fill(struct sk_buff
*skb
,
651 const struct net_device
*dev
)
653 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
659 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
661 if (ops
->fill_xstats
) {
662 err
= ops
->fill_xstats(skb
, dev
);
666 if (ops
->fill_info
) {
667 data
= nla_nest_start_noflag(skb
, IFLA_INFO_DATA
);
670 err
= ops
->fill_info(skb
, dev
);
672 goto err_cancel_data
;
673 nla_nest_end(skb
, data
);
678 nla_nest_cancel(skb
, data
);
682 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
684 struct nlattr
*linkinfo
;
687 linkinfo
= nla_nest_start_noflag(skb
, IFLA_LINKINFO
);
688 if (linkinfo
== NULL
)
691 err
= rtnl_link_info_fill(skb
, dev
);
693 goto err_cancel_link
;
695 err
= rtnl_link_slave_info_fill(skb
, dev
);
697 goto err_cancel_link
;
699 nla_nest_end(skb
, linkinfo
);
703 nla_nest_cancel(skb
, linkinfo
);
708 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
710 struct sock
*rtnl
= net
->rtnl
;
713 NETLINK_CB(skb
).dst_group
= group
;
715 refcount_inc(&skb
->users
);
716 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
718 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
722 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
724 struct sock
*rtnl
= net
->rtnl
;
726 return nlmsg_unicast(rtnl
, skb
, pid
);
728 EXPORT_SYMBOL(rtnl_unicast
);
730 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
731 struct nlmsghdr
*nlh
, gfp_t flags
)
733 struct sock
*rtnl
= net
->rtnl
;
737 report
= nlmsg_report(nlh
);
739 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
741 EXPORT_SYMBOL(rtnl_notify
);
743 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
745 struct sock
*rtnl
= net
->rtnl
;
747 netlink_set_err(rtnl
, 0, group
, error
);
749 EXPORT_SYMBOL(rtnl_set_sk_err
);
751 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
756 /* nothing is dumped for dst_default_metrics, so just skip the loop */
757 if (metrics
== dst_default_metrics
.metrics
)
760 mx
= nla_nest_start_noflag(skb
, RTA_METRICS
);
764 for (i
= 0; i
< RTAX_MAX
; i
++) {
766 if (i
== RTAX_CC_ALGO
- 1) {
767 char tmp
[TCP_CA_NAME_MAX
], *name
;
769 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
772 if (nla_put_string(skb
, i
+ 1, name
))
773 goto nla_put_failure
;
774 } else if (i
== RTAX_FEATURES
- 1) {
775 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
779 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
780 if (nla_put_u32(skb
, i
+ 1, user_features
))
781 goto nla_put_failure
;
783 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
784 goto nla_put_failure
;
791 nla_nest_cancel(skb
, mx
);
795 return nla_nest_end(skb
, mx
);
798 nla_nest_cancel(skb
, mx
);
801 EXPORT_SYMBOL(rtnetlink_put_metrics
);
803 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
804 long expires
, u32 error
)
806 struct rta_cacheinfo ci
= {
812 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
813 ci
.rta_used
= dst
->__use
;
814 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
819 clock
= jiffies_to_clock_t(abs(expires
));
820 clock
= min_t(unsigned long, clock
, INT_MAX
);
821 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
823 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
825 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
827 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
829 unsigned char operstate
= dev
->operstate
;
831 switch (transition
) {
833 if ((operstate
== IF_OPER_DORMANT
||
834 operstate
== IF_OPER_TESTING
||
835 operstate
== IF_OPER_UNKNOWN
) &&
836 !netif_dormant(dev
) && !netif_testing(dev
))
837 operstate
= IF_OPER_UP
;
840 case IF_OPER_TESTING
:
841 if (operstate
== IF_OPER_UP
||
842 operstate
== IF_OPER_UNKNOWN
)
843 operstate
= IF_OPER_TESTING
;
846 case IF_OPER_DORMANT
:
847 if (operstate
== IF_OPER_UP
||
848 operstate
== IF_OPER_UNKNOWN
)
849 operstate
= IF_OPER_DORMANT
;
853 if (dev
->operstate
!= operstate
) {
854 write_lock_bh(&dev_base_lock
);
855 dev
->operstate
= operstate
;
856 write_unlock_bh(&dev_base_lock
);
857 netdev_state_change(dev
);
861 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
863 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
864 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
867 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
868 const struct ifinfomsg
*ifm
)
870 unsigned int flags
= ifm
->ifi_flags
;
872 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
874 flags
= (flags
& ifm
->ifi_change
) |
875 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
880 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
881 const struct rtnl_link_stats64
*b
)
883 a
->rx_packets
= b
->rx_packets
;
884 a
->tx_packets
= b
->tx_packets
;
885 a
->rx_bytes
= b
->rx_bytes
;
886 a
->tx_bytes
= b
->tx_bytes
;
887 a
->rx_errors
= b
->rx_errors
;
888 a
->tx_errors
= b
->tx_errors
;
889 a
->rx_dropped
= b
->rx_dropped
;
890 a
->tx_dropped
= b
->tx_dropped
;
892 a
->multicast
= b
->multicast
;
893 a
->collisions
= b
->collisions
;
895 a
->rx_length_errors
= b
->rx_length_errors
;
896 a
->rx_over_errors
= b
->rx_over_errors
;
897 a
->rx_crc_errors
= b
->rx_crc_errors
;
898 a
->rx_frame_errors
= b
->rx_frame_errors
;
899 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
900 a
->rx_missed_errors
= b
->rx_missed_errors
;
902 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
903 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
904 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
905 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
906 a
->tx_window_errors
= b
->tx_window_errors
;
908 a
->rx_compressed
= b
->rx_compressed
;
909 a
->tx_compressed
= b
->tx_compressed
;
911 a
->rx_nohandler
= b
->rx_nohandler
;
915 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
918 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
919 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
920 size_t size
= nla_total_size(0);
923 nla_total_size(sizeof(struct ifla_vf_mac
)) +
924 nla_total_size(sizeof(struct ifla_vf_broadcast
)) +
925 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
926 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
927 nla_total_size(MAX_VLAN_LIST_LEN
*
928 sizeof(struct ifla_vf_vlan_info
)) +
929 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
930 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
931 nla_total_size(sizeof(struct ifla_vf_rate
)) +
932 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
933 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
934 nla_total_size(0) + /* nest IFLA_VF_STATS */
935 /* IFLA_VF_STATS_RX_PACKETS */
936 nla_total_size_64bit(sizeof(__u64
)) +
937 /* IFLA_VF_STATS_TX_PACKETS */
938 nla_total_size_64bit(sizeof(__u64
)) +
939 /* IFLA_VF_STATS_RX_BYTES */
940 nla_total_size_64bit(sizeof(__u64
)) +
941 /* IFLA_VF_STATS_TX_BYTES */
942 nla_total_size_64bit(sizeof(__u64
)) +
943 /* IFLA_VF_STATS_BROADCAST */
944 nla_total_size_64bit(sizeof(__u64
)) +
945 /* IFLA_VF_STATS_MULTICAST */
946 nla_total_size_64bit(sizeof(__u64
)) +
947 /* IFLA_VF_STATS_RX_DROPPED */
948 nla_total_size_64bit(sizeof(__u64
)) +
949 /* IFLA_VF_STATS_TX_DROPPED */
950 nla_total_size_64bit(sizeof(__u64
)) +
951 nla_total_size(sizeof(struct ifla_vf_trust
)));
957 static size_t rtnl_port_size(const struct net_device
*dev
,
960 size_t port_size
= nla_total_size(4) /* PORT_VF */
961 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
962 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
963 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
964 + nla_total_size(1) /* PROT_VDP_REQUEST */
965 + nla_total_size(2); /* PORT_VDP_RESPONSE */
966 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
967 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
969 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
972 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
973 !(ext_filter_mask
& RTEXT_FILTER_VF
))
975 if (dev_num_vf(dev
->dev
.parent
))
976 return port_self_size
+ vf_ports_size
+
977 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
979 return port_self_size
;
982 static size_t rtnl_xdp_size(void)
984 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
985 nla_total_size(1) + /* XDP_ATTACHED */
986 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
987 nla_total_size(4); /* XDP_<mode>_PROG_ID */
992 static size_t rtnl_prop_list_size(const struct net_device
*dev
)
994 struct netdev_name_node
*name_node
;
997 if (list_empty(&dev
->name_node
->list
))
999 size
= nla_total_size(0);
1000 list_for_each_entry(name_node
, &dev
->name_node
->list
, list
)
1001 size
+= nla_total_size(ALTIFNAMSIZ
);
1005 static size_t rtnl_proto_down_size(const struct net_device
*dev
)
1007 size_t size
= nla_total_size(1);
1009 if (dev
->proto_down_reason
)
1010 size
+= nla_total_size(0) + nla_total_size(4);
1015 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
1016 u32 ext_filter_mask
)
1018 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
1019 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
1020 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
1021 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
1022 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
1023 + nla_total_size(sizeof(struct rtnl_link_stats
))
1024 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
1025 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
1026 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
1027 + nla_total_size(4) /* IFLA_TXQLEN */
1028 + nla_total_size(4) /* IFLA_WEIGHT */
1029 + nla_total_size(4) /* IFLA_MTU */
1030 + nla_total_size(4) /* IFLA_LINK */
1031 + nla_total_size(4) /* IFLA_MASTER */
1032 + nla_total_size(1) /* IFLA_CARRIER */
1033 + nla_total_size(4) /* IFLA_PROMISCUITY */
1034 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1035 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1036 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1037 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1038 + nla_total_size(1) /* IFLA_OPERSTATE */
1039 + nla_total_size(1) /* IFLA_LINKMODE */
1040 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1041 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1042 + nla_total_size(4) /* IFLA_GROUP */
1043 + nla_total_size(ext_filter_mask
1044 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1045 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1046 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1047 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1048 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1049 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1050 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1051 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1052 + rtnl_xdp_size() /* IFLA_XDP */
1053 + nla_total_size(4) /* IFLA_EVENT */
1054 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1055 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1056 + rtnl_proto_down_size(dev
) /* proto down */
1057 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1058 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1059 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1060 + nla_total_size(4) /* IFLA_MIN_MTU */
1061 + nla_total_size(4) /* IFLA_MAX_MTU */
1062 + rtnl_prop_list_size(dev
)
1063 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_PERM_ADDRESS */
1067 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1069 struct nlattr
*vf_ports
;
1070 struct nlattr
*vf_port
;
1074 vf_ports
= nla_nest_start_noflag(skb
, IFLA_VF_PORTS
);
1078 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1079 vf_port
= nla_nest_start_noflag(skb
, IFLA_VF_PORT
);
1081 goto nla_put_failure
;
1082 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1083 goto nla_put_failure
;
1084 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1085 if (err
== -EMSGSIZE
)
1086 goto nla_put_failure
;
1088 nla_nest_cancel(skb
, vf_port
);
1091 nla_nest_end(skb
, vf_port
);
1094 nla_nest_end(skb
, vf_ports
);
1099 nla_nest_cancel(skb
, vf_ports
);
1103 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1105 struct nlattr
*port_self
;
1108 port_self
= nla_nest_start_noflag(skb
, IFLA_PORT_SELF
);
1112 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1114 nla_nest_cancel(skb
, port_self
);
1115 return (err
== -EMSGSIZE
) ? err
: 0;
1118 nla_nest_end(skb
, port_self
);
1123 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1124 u32 ext_filter_mask
)
1128 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1129 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1132 err
= rtnl_port_self_fill(skb
, dev
);
1136 if (dev_num_vf(dev
->dev
.parent
)) {
1137 err
= rtnl_vf_ports_fill(skb
, dev
);
1145 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1148 struct netdev_phys_item_id ppid
;
1150 err
= dev_get_phys_port_id(dev
, &ppid
);
1152 if (err
== -EOPNOTSUPP
)
1157 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1163 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1165 char name
[IFNAMSIZ
];
1168 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1170 if (err
== -EOPNOTSUPP
)
1175 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1181 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1183 struct netdev_phys_item_id ppid
= { };
1186 err
= dev_get_port_parent_id(dev
, &ppid
, false);
1188 if (err
== -EOPNOTSUPP
)
1193 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, ppid
.id_len
, ppid
.id
))
1199 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1200 struct net_device
*dev
)
1202 struct rtnl_link_stats64
*sp
;
1203 struct nlattr
*attr
;
1205 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1206 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1210 sp
= nla_data(attr
);
1211 dev_get_stats(dev
, sp
);
1213 attr
= nla_reserve(skb
, IFLA_STATS
,
1214 sizeof(struct rtnl_link_stats
));
1218 copy_rtnl_link_stats(nla_data(attr
), sp
);
1223 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1224 struct net_device
*dev
,
1226 struct nlattr
*vfinfo
)
1228 struct ifla_vf_rss_query_en vf_rss_query_en
;
1229 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1230 struct ifla_vf_link_state vf_linkstate
;
1231 struct ifla_vf_vlan_info vf_vlan_info
;
1232 struct ifla_vf_spoofchk vf_spoofchk
;
1233 struct ifla_vf_tx_rate vf_tx_rate
;
1234 struct ifla_vf_stats vf_stats
;
1235 struct ifla_vf_trust vf_trust
;
1236 struct ifla_vf_vlan vf_vlan
;
1237 struct ifla_vf_rate vf_rate
;
1238 struct ifla_vf_mac vf_mac
;
1239 struct ifla_vf_broadcast vf_broadcast
;
1240 struct ifla_vf_info ivi
;
1241 struct ifla_vf_guid node_guid
;
1242 struct ifla_vf_guid port_guid
;
1244 memset(&ivi
, 0, sizeof(ivi
));
1246 /* Not all SR-IOV capable drivers support the
1247 * spoofcheck and "RSS query enable" query. Preset to
1248 * -1 so the user space tool can detect that the driver
1249 * didn't report anything.
1252 ivi
.rss_query_en
= -1;
1254 /* The default value for VF link state is "auto"
1255 * IFLA_VF_LINK_STATE_AUTO which equals zero
1258 /* VLAN Protocol by default is 802.1Q */
1259 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1260 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1263 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1264 memset(&node_guid
, 0, sizeof(node_guid
));
1265 memset(&port_guid
, 0, sizeof(port_guid
));
1274 vf_rss_query_en
.vf
=
1277 port_guid
.vf
= ivi
.vf
;
1279 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1280 memcpy(vf_broadcast
.broadcast
, dev
->broadcast
, dev
->addr_len
);
1281 vf_vlan
.vlan
= ivi
.vlan
;
1282 vf_vlan
.qos
= ivi
.qos
;
1283 vf_vlan_info
.vlan
= ivi
.vlan
;
1284 vf_vlan_info
.qos
= ivi
.qos
;
1285 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1286 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1287 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1288 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1289 vf_spoofchk
.setting
= ivi
.spoofchk
;
1290 vf_linkstate
.link_state
= ivi
.linkstate
;
1291 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1292 vf_trust
.setting
= ivi
.trusted
;
1293 vf
= nla_nest_start_noflag(skb
, IFLA_VF_INFO
);
1295 goto nla_put_vfinfo_failure
;
1296 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1297 nla_put(skb
, IFLA_VF_BROADCAST
, sizeof(vf_broadcast
), &vf_broadcast
) ||
1298 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1299 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1301 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1303 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1305 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1307 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1308 sizeof(vf_rss_query_en
),
1309 &vf_rss_query_en
) ||
1310 nla_put(skb
, IFLA_VF_TRUST
,
1311 sizeof(vf_trust
), &vf_trust
))
1312 goto nla_put_vf_failure
;
1314 if (dev
->netdev_ops
->ndo_get_vf_guid
&&
1315 !dev
->netdev_ops
->ndo_get_vf_guid(dev
, vfs_num
, &node_guid
,
1317 if (nla_put(skb
, IFLA_VF_IB_NODE_GUID
, sizeof(node_guid
),
1319 nla_put(skb
, IFLA_VF_IB_PORT_GUID
, sizeof(port_guid
),
1321 goto nla_put_vf_failure
;
1323 vfvlanlist
= nla_nest_start_noflag(skb
, IFLA_VF_VLAN_LIST
);
1325 goto nla_put_vf_failure
;
1326 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1328 nla_nest_cancel(skb
, vfvlanlist
);
1329 goto nla_put_vf_failure
;
1331 nla_nest_end(skb
, vfvlanlist
);
1332 memset(&vf_stats
, 0, sizeof(vf_stats
));
1333 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1334 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1336 vfstats
= nla_nest_start_noflag(skb
, IFLA_VF_STATS
);
1338 goto nla_put_vf_failure
;
1339 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1340 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1341 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1342 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1343 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1344 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1345 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1346 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1347 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1348 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1349 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1350 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1351 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1352 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1353 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1354 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1355 nla_nest_cancel(skb
, vfstats
);
1356 goto nla_put_vf_failure
;
1358 nla_nest_end(skb
, vfstats
);
1359 nla_nest_end(skb
, vf
);
1363 nla_nest_cancel(skb
, vf
);
1364 nla_put_vfinfo_failure
:
1365 nla_nest_cancel(skb
, vfinfo
);
1369 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1370 struct net_device
*dev
,
1371 u32 ext_filter_mask
)
1373 struct nlattr
*vfinfo
;
1376 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1379 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1380 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1383 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1386 vfinfo
= nla_nest_start_noflag(skb
, IFLA_VFINFO_LIST
);
1390 for (i
= 0; i
< num_vfs
; i
++) {
1391 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1395 nla_nest_end(skb
, vfinfo
);
1399 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1401 struct rtnl_link_ifmap map
;
1403 memset(&map
, 0, sizeof(map
));
1404 map
.mem_start
= dev
->mem_start
;
1405 map
.mem_end
= dev
->mem_end
;
1406 map
.base_addr
= dev
->base_addr
;
1409 map
.port
= dev
->if_port
;
1411 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1417 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1419 const struct bpf_prog
*generic_xdp_prog
;
1423 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1424 if (!generic_xdp_prog
)
1426 return generic_xdp_prog
->aux
->id
;
1429 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1431 return dev_xdp_prog_id(dev
, XDP_MODE_DRV
);
1434 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1436 return dev_xdp_prog_id(dev
, XDP_MODE_HW
);
1439 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1440 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1441 u32 (*get_prog_id
)(struct net_device
*dev
))
1446 curr_id
= get_prog_id(dev
);
1451 err
= nla_put_u32(skb
, attr
, curr_id
);
1455 if (*mode
!= XDP_ATTACHED_NONE
)
1456 *mode
= XDP_ATTACHED_MULTI
;
1463 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1470 xdp
= nla_nest_start_noflag(skb
, IFLA_XDP
);
1475 mode
= XDP_ATTACHED_NONE
;
1476 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1477 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1480 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1481 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1484 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1485 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1489 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1493 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1494 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1499 nla_nest_end(skb
, xdp
);
1503 nla_nest_cancel(skb
, xdp
);
1507 static u32
rtnl_get_event(unsigned long event
)
1509 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1513 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1515 case NETDEV_FEAT_CHANGE
:
1516 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1518 case NETDEV_BONDING_FAILOVER
:
1519 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1521 case NETDEV_NOTIFY_PEERS
:
1522 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1524 case NETDEV_RESEND_IGMP
:
1525 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1527 case NETDEV_CHANGEINFODATA
:
1528 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1534 return rtnl_event_type
;
1537 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1539 const struct net_device
*upper_dev
;
1544 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1546 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1552 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
,
1555 int ifindex
= dev_get_iflink(dev
);
1557 if (force
|| dev
->ifindex
!= ifindex
)
1558 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1563 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1564 struct net_device
*dev
)
1569 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1570 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1573 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1574 const struct net_device
*dev
,
1575 struct net
*src_net
, gfp_t gfp
)
1577 bool put_iflink
= false;
1579 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1580 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1582 if (!net_eq(dev_net(dev
), link_net
)) {
1583 int id
= peernet2id_alloc(src_net
, link_net
, gfp
);
1585 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1592 return nla_put_iflink(skb
, dev
, put_iflink
);
1595 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1596 const struct net_device
*dev
,
1597 u32 ext_filter_mask
)
1599 const struct rtnl_af_ops
*af_ops
;
1600 struct nlattr
*af_spec
;
1602 af_spec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
1606 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1610 if (!af_ops
->fill_link_af
)
1613 af
= nla_nest_start_noflag(skb
, af_ops
->family
);
1617 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1619 * Caller may return ENODATA to indicate that there
1620 * was no data to be dumped. This is not an error, it
1621 * means we should trim the attribute header and
1624 if (err
== -ENODATA
)
1625 nla_nest_cancel(skb
, af
);
1629 nla_nest_end(skb
, af
);
1632 nla_nest_end(skb
, af_spec
);
1636 static int rtnl_fill_alt_ifnames(struct sk_buff
*skb
,
1637 const struct net_device
*dev
)
1639 struct netdev_name_node
*name_node
;
1642 list_for_each_entry(name_node
, &dev
->name_node
->list
, list
) {
1643 if (nla_put_string(skb
, IFLA_ALT_IFNAME
, name_node
->name
))
1650 static int rtnl_fill_prop_list(struct sk_buff
*skb
,
1651 const struct net_device
*dev
)
1653 struct nlattr
*prop_list
;
1656 prop_list
= nla_nest_start(skb
, IFLA_PROP_LIST
);
1660 ret
= rtnl_fill_alt_ifnames(skb
, dev
);
1664 nla_nest_end(skb
, prop_list
);
1668 nla_nest_cancel(skb
, prop_list
);
1672 static int rtnl_fill_proto_down(struct sk_buff
*skb
,
1673 const struct net_device
*dev
)
1678 if (nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
))
1679 goto nla_put_failure
;
1681 preason
= dev
->proto_down_reason
;
1685 pr
= nla_nest_start(skb
, IFLA_PROTO_DOWN_REASON
);
1689 if (nla_put_u32(skb
, IFLA_PROTO_DOWN_REASON_VALUE
, preason
)) {
1690 nla_nest_cancel(skb
, pr
);
1691 goto nla_put_failure
;
1694 nla_nest_end(skb
, pr
);
1701 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1702 struct net_device
*dev
, struct net
*src_net
,
1703 int type
, u32 pid
, u32 seq
, u32 change
,
1704 unsigned int flags
, u32 ext_filter_mask
,
1705 u32 event
, int *new_nsid
, int new_ifindex
,
1706 int tgt_netnsid
, gfp_t gfp
)
1708 struct ifinfomsg
*ifm
;
1709 struct nlmsghdr
*nlh
;
1712 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1716 ifm
= nlmsg_data(nlh
);
1717 ifm
->ifi_family
= AF_UNSPEC
;
1719 ifm
->ifi_type
= dev
->type
;
1720 ifm
->ifi_index
= dev
->ifindex
;
1721 ifm
->ifi_flags
= dev_get_flags(dev
);
1722 ifm
->ifi_change
= change
;
1724 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_TARGET_NETNSID
, tgt_netnsid
))
1725 goto nla_put_failure
;
1727 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1728 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1729 nla_put_u8(skb
, IFLA_OPERSTATE
,
1730 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1731 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1732 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1733 nla_put_u32(skb
, IFLA_MIN_MTU
, dev
->min_mtu
) ||
1734 nla_put_u32(skb
, IFLA_MAX_MTU
, dev
->max_mtu
) ||
1735 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1736 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1737 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1738 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1739 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1741 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1743 put_master_ifindex(skb
, dev
) ||
1744 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1746 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1747 nla_put_ifalias(skb
, dev
) ||
1748 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1749 atomic_read(&dev
->carrier_up_count
) +
1750 atomic_read(&dev
->carrier_down_count
)) ||
1751 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1752 atomic_read(&dev
->carrier_up_count
)) ||
1753 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1754 atomic_read(&dev
->carrier_down_count
)))
1755 goto nla_put_failure
;
1757 if (rtnl_fill_proto_down(skb
, dev
))
1758 goto nla_put_failure
;
1760 if (event
!= IFLA_EVENT_NONE
) {
1761 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1762 goto nla_put_failure
;
1765 if (rtnl_fill_link_ifmap(skb
, dev
))
1766 goto nla_put_failure
;
1768 if (dev
->addr_len
) {
1769 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1770 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1771 goto nla_put_failure
;
1774 if (rtnl_phys_port_id_fill(skb
, dev
))
1775 goto nla_put_failure
;
1777 if (rtnl_phys_port_name_fill(skb
, dev
))
1778 goto nla_put_failure
;
1780 if (rtnl_phys_switch_id_fill(skb
, dev
))
1781 goto nla_put_failure
;
1783 if (rtnl_fill_stats(skb
, dev
))
1784 goto nla_put_failure
;
1786 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1787 goto nla_put_failure
;
1789 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1790 goto nla_put_failure
;
1792 if (rtnl_xdp_fill(skb
, dev
))
1793 goto nla_put_failure
;
1795 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1796 if (rtnl_link_fill(skb
, dev
) < 0)
1797 goto nla_put_failure
;
1800 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
, gfp
))
1801 goto nla_put_failure
;
1804 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1805 goto nla_put_failure
;
1807 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1808 goto nla_put_failure
;
1810 if (memchr_inv(dev
->perm_addr
, '\0', dev
->addr_len
) &&
1811 nla_put(skb
, IFLA_PERM_ADDRESS
, dev
->addr_len
, dev
->perm_addr
))
1812 goto nla_put_failure
;
1815 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1816 goto nla_put_failure_rcu
;
1819 if (rtnl_fill_prop_list(skb
, dev
))
1820 goto nla_put_failure
;
1822 nlmsg_end(skb
, nlh
);
1825 nla_put_failure_rcu
:
1828 nlmsg_cancel(skb
, nlh
);
1832 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1833 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1834 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1835 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1836 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1837 [IFLA_MTU
] = { .type
= NLA_U32
},
1838 [IFLA_LINK
] = { .type
= NLA_U32
},
1839 [IFLA_MASTER
] = { .type
= NLA_U32
},
1840 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1841 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1842 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1843 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1844 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1845 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1846 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1847 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1848 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1849 * allow 0-length string (needed to remove an alias).
1851 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1852 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1853 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1854 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1855 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1856 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1857 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1858 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1859 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1860 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1861 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1862 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1863 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1864 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1865 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1866 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1867 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1868 [IFLA_EVENT
] = { .type
= NLA_U32
},
1869 [IFLA_GROUP
] = { .type
= NLA_U32
},
1870 [IFLA_TARGET_NETNSID
] = { .type
= NLA_S32
},
1871 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1872 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1873 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
1874 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
1875 [IFLA_PROP_LIST
] = { .type
= NLA_NESTED
},
1876 [IFLA_ALT_IFNAME
] = { .type
= NLA_STRING
,
1877 .len
= ALTIFNAMSIZ
- 1 },
1878 [IFLA_PERM_ADDRESS
] = { .type
= NLA_REJECT
},
1879 [IFLA_PROTO_DOWN_REASON
] = { .type
= NLA_NESTED
},
1882 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1883 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1884 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1885 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1886 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1889 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1890 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1891 [IFLA_VF_BROADCAST
] = { .type
= NLA_REJECT
},
1892 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1893 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1894 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1895 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1896 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1897 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1898 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1899 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1900 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1901 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1902 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1905 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1906 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1907 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1908 .len
= PORT_PROFILE_MAX
},
1909 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1910 .len
= PORT_UUID_MAX
},
1911 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1912 .len
= PORT_UUID_MAX
},
1913 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1914 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1916 /* Unused, but we need to keep it here since user space could
1917 * fill it. It's also broken with regard to NLA_BINARY use in
1918 * combination with structs.
1920 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1921 .len
= sizeof(struct ifla_port_vsi
) },
1924 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1925 [IFLA_XDP_UNSPEC
] = { .strict_start_type
= IFLA_XDP_EXPECTED_FD
},
1926 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1927 [IFLA_XDP_EXPECTED_FD
] = { .type
= NLA_S32
},
1928 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1929 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1930 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1933 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1935 const struct rtnl_link_ops
*ops
= NULL
;
1936 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1938 if (nla_parse_nested_deprecated(linfo
, IFLA_INFO_MAX
, nla
, ifla_info_policy
, NULL
) < 0)
1941 if (linfo
[IFLA_INFO_KIND
]) {
1942 char kind
[MODULE_NAME_LEN
];
1944 nla_strscpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1945 ops
= rtnl_link_ops_get(kind
);
1951 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1953 struct net_device
*master
;
1958 master
= netdev_master_upper_dev_get(dev
);
1959 if (!master
|| master
->ifindex
!= master_idx
)
1965 static bool link_kind_filtered(const struct net_device
*dev
,
1966 const struct rtnl_link_ops
*kind_ops
)
1968 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1974 static bool link_dump_filtered(struct net_device
*dev
,
1976 const struct rtnl_link_ops
*kind_ops
)
1978 if (link_master_filtered(dev
, master_idx
) ||
1979 link_kind_filtered(dev
, kind_ops
))
1986 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1987 * @sk: netlink socket
1988 * @netnsid: network namespace identifier
1990 * Returns the network namespace identified by netnsid on success or an error
1991 * pointer on failure.
1993 struct net
*rtnl_get_net_ns_capable(struct sock
*sk
, int netnsid
)
1997 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1999 return ERR_PTR(-EINVAL
);
2001 /* For now, the caller is required to have CAP_NET_ADMIN in
2002 * the user namespace owning the target net ns.
2004 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
2006 return ERR_PTR(-EACCES
);
2010 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable
);
2012 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr
*nlh
,
2013 bool strict_check
, struct nlattr
**tb
,
2014 struct netlink_ext_ack
*extack
)
2019 struct ifinfomsg
*ifm
;
2021 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
2022 NL_SET_ERR_MSG(extack
, "Invalid header for link dump");
2026 ifm
= nlmsg_data(nlh
);
2027 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
2029 NL_SET_ERR_MSG(extack
, "Invalid values in header for link dump request");
2032 if (ifm
->ifi_index
) {
2033 NL_SET_ERR_MSG(extack
, "Filter by device index not supported for link dumps");
2037 return nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
,
2038 IFLA_MAX
, ifla_policy
,
2042 /* A hack to preserve kernel<->userspace interface.
2043 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2044 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2045 * what iproute2 < v3.9.0 used.
2046 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2047 * attribute, its netlink message is shorter than struct ifinfomsg.
2049 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2050 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2052 return nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
,
2056 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2058 struct netlink_ext_ack
*extack
= cb
->extack
;
2059 const struct nlmsghdr
*nlh
= cb
->nlh
;
2060 struct net
*net
= sock_net(skb
->sk
);
2061 struct net
*tgt_net
= net
;
2064 struct net_device
*dev
;
2065 struct hlist_head
*head
;
2066 struct nlattr
*tb
[IFLA_MAX
+1];
2067 u32 ext_filter_mask
= 0;
2068 const struct rtnl_link_ops
*kind_ops
= NULL
;
2069 unsigned int flags
= NLM_F_MULTI
;
2075 s_idx
= cb
->args
[1];
2077 err
= rtnl_valid_dump_ifinfo_req(nlh
, cb
->strict_check
, tb
, extack
);
2079 if (cb
->strict_check
)
2085 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
2089 /* new attributes should only be added with strict checking */
2091 case IFLA_TARGET_NETNSID
:
2092 netnsid
= nla_get_s32(tb
[i
]);
2093 tgt_net
= rtnl_get_net_ns_capable(skb
->sk
, netnsid
);
2094 if (IS_ERR(tgt_net
)) {
2095 NL_SET_ERR_MSG(extack
, "Invalid target network namespace id");
2096 return PTR_ERR(tgt_net
);
2100 ext_filter_mask
= nla_get_u32(tb
[i
]);
2103 master_idx
= nla_get_u32(tb
[i
]);
2106 kind_ops
= linkinfo_to_kind_ops(tb
[i
]);
2109 if (cb
->strict_check
) {
2110 NL_SET_ERR_MSG(extack
, "Unsupported attribute in link dump request");
2116 if (master_idx
|| kind_ops
)
2117 flags
|= NLM_F_DUMP_FILTERED
;
2120 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
2122 head
= &tgt_net
->dev_index_head
[h
];
2123 hlist_for_each_entry(dev
, head
, index_hlist
) {
2124 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
2128 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
2130 NETLINK_CB(cb
->skb
).portid
,
2131 nlh
->nlmsg_seq
, 0, flags
,
2132 ext_filter_mask
, 0, NULL
, 0,
2133 netnsid
, GFP_KERNEL
);
2136 if (likely(skb
->len
))
2150 cb
->seq
= net
->dev_base_seq
;
2151 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2158 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
2159 struct netlink_ext_ack
*exterr
)
2161 return nla_parse_deprecated(tb
, IFLA_MAX
, head
, len
, ifla_policy
,
2164 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
2166 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
2169 /* Examine the link attributes and figure out which
2170 * network namespace we are talking about.
2172 if (tb
[IFLA_NET_NS_PID
])
2173 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
2174 else if (tb
[IFLA_NET_NS_FD
])
2175 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
2177 net
= get_net(src_net
);
2180 EXPORT_SYMBOL(rtnl_link_get_net
);
2182 /* Figure out which network namespace we are talking about by
2183 * examining the link attributes in the following order:
2185 * 1. IFLA_NET_NS_PID
2187 * 3. IFLA_TARGET_NETNSID
2189 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
2190 struct nlattr
*tb
[])
2194 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
2195 return rtnl_link_get_net(src_net
, tb
);
2197 if (!tb
[IFLA_TARGET_NETNSID
])
2198 return get_net(src_net
);
2200 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_TARGET_NETNSID
]));
2202 return ERR_PTR(-EINVAL
);
2207 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2208 struct net
*src_net
,
2209 struct nlattr
*tb
[], int cap
)
2213 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2217 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2219 return ERR_PTR(-EPERM
);
2225 /* Verify that rtnetlink requests do not pass additional properties
2226 * potentially referring to different network namespaces.
2228 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2229 struct netlink_ext_ack
*extack
,
2233 if (netns_id_only
) {
2234 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2237 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2241 if (tb
[IFLA_TARGET_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2244 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2247 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2253 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2257 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2260 if (tb
[IFLA_ADDRESS
] &&
2261 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2264 if (tb
[IFLA_BROADCAST
] &&
2265 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2269 if (tb
[IFLA_AF_SPEC
]) {
2273 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2274 const struct rtnl_af_ops
*af_ops
;
2277 af_ops
= rtnl_af_lookup(nla_type(af
));
2280 return -EAFNOSUPPORT
;
2283 if (!af_ops
->set_link_af
) {
2288 if (af_ops
->validate_link_af
) {
2289 err
= af_ops
->validate_link_af(dev
, af
);
2303 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2306 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2308 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2311 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2313 if (dev
->type
!= ARPHRD_INFINIBAND
)
2316 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2319 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2321 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2324 if (tb
[IFLA_VF_MAC
]) {
2325 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2327 if (ivm
->vf
>= INT_MAX
)
2330 if (ops
->ndo_set_vf_mac
)
2331 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2337 if (tb
[IFLA_VF_VLAN
]) {
2338 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2340 if (ivv
->vf
>= INT_MAX
)
2343 if (ops
->ndo_set_vf_vlan
)
2344 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2346 htons(ETH_P_8021Q
));
2351 if (tb
[IFLA_VF_VLAN_LIST
]) {
2352 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2353 struct nlattr
*attr
;
2357 if (!ops
->ndo_set_vf_vlan
)
2360 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2361 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2362 nla_len(attr
) < NLA_HDRLEN
) {
2365 if (len
>= MAX_VLAN_LIST_LEN
)
2367 ivvl
[len
] = nla_data(attr
);
2374 if (ivvl
[0]->vf
>= INT_MAX
)
2376 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2377 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2382 if (tb
[IFLA_VF_TX_RATE
]) {
2383 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2384 struct ifla_vf_info ivf
;
2386 if (ivt
->vf
>= INT_MAX
)
2389 if (ops
->ndo_get_vf_config
)
2390 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2395 if (ops
->ndo_set_vf_rate
)
2396 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2403 if (tb
[IFLA_VF_RATE
]) {
2404 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2406 if (ivt
->vf
>= INT_MAX
)
2409 if (ops
->ndo_set_vf_rate
)
2410 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2417 if (tb
[IFLA_VF_SPOOFCHK
]) {
2418 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2420 if (ivs
->vf
>= INT_MAX
)
2423 if (ops
->ndo_set_vf_spoofchk
)
2424 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2430 if (tb
[IFLA_VF_LINK_STATE
]) {
2431 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2433 if (ivl
->vf
>= INT_MAX
)
2436 if (ops
->ndo_set_vf_link_state
)
2437 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2443 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2444 struct ifla_vf_rss_query_en
*ivrssq_en
;
2447 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2448 if (ivrssq_en
->vf
>= INT_MAX
)
2450 if (ops
->ndo_set_vf_rss_query_en
)
2451 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2452 ivrssq_en
->setting
);
2457 if (tb
[IFLA_VF_TRUST
]) {
2458 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2460 if (ivt
->vf
>= INT_MAX
)
2463 if (ops
->ndo_set_vf_trust
)
2464 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2469 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2470 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2472 if (ivt
->vf
>= INT_MAX
)
2474 if (!ops
->ndo_set_vf_guid
)
2476 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2479 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2480 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2482 if (ivt
->vf
>= INT_MAX
)
2484 if (!ops
->ndo_set_vf_guid
)
2487 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2493 static int do_set_master(struct net_device
*dev
, int ifindex
,
2494 struct netlink_ext_ack
*extack
)
2496 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2497 const struct net_device_ops
*ops
;
2501 if (upper_dev
->ifindex
== ifindex
)
2503 ops
= upper_dev
->netdev_ops
;
2504 if (ops
->ndo_del_slave
) {
2505 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2514 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2517 ops
= upper_dev
->netdev_ops
;
2518 if (ops
->ndo_add_slave
) {
2519 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2529 static const struct nla_policy ifla_proto_down_reason_policy
[IFLA_PROTO_DOWN_REASON_VALUE
+ 1] = {
2530 [IFLA_PROTO_DOWN_REASON_MASK
] = { .type
= NLA_U32
},
2531 [IFLA_PROTO_DOWN_REASON_VALUE
] = { .type
= NLA_U32
},
2534 static int do_set_proto_down(struct net_device
*dev
,
2535 struct nlattr
*nl_proto_down
,
2536 struct nlattr
*nl_proto_down_reason
,
2537 struct netlink_ext_ack
*extack
)
2539 struct nlattr
*pdreason
[IFLA_PROTO_DOWN_REASON_MAX
+ 1];
2540 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2541 unsigned long mask
= 0;
2546 if (!ops
->ndo_change_proto_down
) {
2547 NL_SET_ERR_MSG(extack
, "Protodown not supported by device");
2551 if (nl_proto_down_reason
) {
2552 err
= nla_parse_nested_deprecated(pdreason
,
2553 IFLA_PROTO_DOWN_REASON_MAX
,
2554 nl_proto_down_reason
,
2555 ifla_proto_down_reason_policy
,
2560 if (!pdreason
[IFLA_PROTO_DOWN_REASON_VALUE
]) {
2561 NL_SET_ERR_MSG(extack
, "Invalid protodown reason value");
2565 value
= nla_get_u32(pdreason
[IFLA_PROTO_DOWN_REASON_VALUE
]);
2567 if (pdreason
[IFLA_PROTO_DOWN_REASON_MASK
])
2568 mask
= nla_get_u32(pdreason
[IFLA_PROTO_DOWN_REASON_MASK
]);
2570 dev_change_proto_down_reason(dev
, mask
, value
);
2573 if (nl_proto_down
) {
2574 proto_down
= nla_get_u8(nl_proto_down
);
2576 /* Dont turn off protodown if there are active reasons */
2577 if (!proto_down
&& dev
->proto_down_reason
) {
2578 NL_SET_ERR_MSG(extack
, "Cannot clear protodown, active reasons");
2581 err
= dev_change_proto_down(dev
,
2590 #define DO_SETLINK_MODIFIED 0x01
2591 /* notify flag means notify + modified. */
2592 #define DO_SETLINK_NOTIFY 0x03
2593 static int do_setlink(const struct sk_buff
*skb
,
2594 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2595 struct netlink_ext_ack
*extack
,
2596 struct nlattr
**tb
, char *ifname
, int status
)
2598 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2601 err
= validate_linkmsg(dev
, tb
);
2605 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_TARGET_NETNSID
]) {
2606 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2613 err
= dev_change_net_namespace(dev
, net
, ifname
);
2617 status
|= DO_SETLINK_MODIFIED
;
2621 struct rtnl_link_ifmap
*u_map
;
2624 if (!ops
->ndo_set_config
) {
2629 if (!netif_device_present(dev
)) {
2634 u_map
= nla_data(tb
[IFLA_MAP
]);
2635 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2636 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2637 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2638 k_map
.irq
= (unsigned char) u_map
->irq
;
2639 k_map
.dma
= (unsigned char) u_map
->dma
;
2640 k_map
.port
= (unsigned char) u_map
->port
;
2642 err
= ops
->ndo_set_config(dev
, &k_map
);
2646 status
|= DO_SETLINK_NOTIFY
;
2649 if (tb
[IFLA_ADDRESS
]) {
2650 struct sockaddr
*sa
;
2653 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2655 sa
= kmalloc(len
, GFP_KERNEL
);
2660 sa
->sa_family
= dev
->type
;
2661 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2663 err
= dev_set_mac_address(dev
, sa
, extack
);
2667 status
|= DO_SETLINK_MODIFIED
;
2671 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
2674 status
|= DO_SETLINK_MODIFIED
;
2677 if (tb
[IFLA_GROUP
]) {
2678 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2679 status
|= DO_SETLINK_NOTIFY
;
2683 * Interface selected by interface index but interface
2684 * name provided implies that a name change has been
2687 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2688 err
= dev_change_name(dev
, ifname
);
2691 status
|= DO_SETLINK_MODIFIED
;
2694 if (tb
[IFLA_IFALIAS
]) {
2695 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2696 nla_len(tb
[IFLA_IFALIAS
]));
2699 status
|= DO_SETLINK_NOTIFY
;
2702 if (tb
[IFLA_BROADCAST
]) {
2703 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2704 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2707 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2708 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
2714 if (tb
[IFLA_MASTER
]) {
2715 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2718 status
|= DO_SETLINK_MODIFIED
;
2721 if (tb
[IFLA_CARRIER
]) {
2722 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2725 status
|= DO_SETLINK_MODIFIED
;
2728 if (tb
[IFLA_TXQLEN
]) {
2729 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2731 err
= dev_change_tx_queue_len(dev
, value
);
2734 status
|= DO_SETLINK_MODIFIED
;
2737 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2738 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2740 if (max_size
> GSO_MAX_SIZE
) {
2745 if (dev
->gso_max_size
^ max_size
) {
2746 netif_set_gso_max_size(dev
, max_size
);
2747 status
|= DO_SETLINK_MODIFIED
;
2751 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2752 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2754 if (max_segs
> GSO_MAX_SEGS
) {
2759 if (dev
->gso_max_segs
^ max_segs
) {
2760 dev
->gso_max_segs
= max_segs
;
2761 status
|= DO_SETLINK_MODIFIED
;
2765 if (tb
[IFLA_OPERSTATE
])
2766 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2768 if (tb
[IFLA_LINKMODE
]) {
2769 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2771 write_lock_bh(&dev_base_lock
);
2772 if (dev
->link_mode
^ value
)
2773 status
|= DO_SETLINK_NOTIFY
;
2774 dev
->link_mode
= value
;
2775 write_unlock_bh(&dev_base_lock
);
2778 if (tb
[IFLA_VFINFO_LIST
]) {
2779 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2780 struct nlattr
*attr
;
2783 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2784 if (nla_type(attr
) != IFLA_VF_INFO
||
2785 nla_len(attr
) < NLA_HDRLEN
) {
2789 err
= nla_parse_nested_deprecated(vfinfo
, IFLA_VF_MAX
,
2795 err
= do_setvfinfo(dev
, vfinfo
);
2798 status
|= DO_SETLINK_NOTIFY
;
2803 if (tb
[IFLA_VF_PORTS
]) {
2804 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2805 struct nlattr
*attr
;
2810 if (!ops
->ndo_set_vf_port
)
2813 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2814 if (nla_type(attr
) != IFLA_VF_PORT
||
2815 nla_len(attr
) < NLA_HDRLEN
) {
2819 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2825 if (!port
[IFLA_PORT_VF
]) {
2829 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2830 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2833 status
|= DO_SETLINK_NOTIFY
;
2838 if (tb
[IFLA_PORT_SELF
]) {
2839 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2841 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2843 ifla_port_policy
, NULL
);
2848 if (ops
->ndo_set_vf_port
)
2849 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2852 status
|= DO_SETLINK_NOTIFY
;
2855 if (tb
[IFLA_AF_SPEC
]) {
2859 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2860 const struct rtnl_af_ops
*af_ops
;
2864 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2866 err
= af_ops
->set_link_af(dev
, af
);
2873 status
|= DO_SETLINK_NOTIFY
;
2878 if (tb
[IFLA_PROTO_DOWN
] || tb
[IFLA_PROTO_DOWN_REASON
]) {
2879 err
= do_set_proto_down(dev
, tb
[IFLA_PROTO_DOWN
],
2880 tb
[IFLA_PROTO_DOWN_REASON
], extack
);
2883 status
|= DO_SETLINK_NOTIFY
;
2887 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2890 err
= nla_parse_nested_deprecated(xdp
, IFLA_XDP_MAX
,
2892 ifla_xdp_policy
, NULL
);
2896 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2901 if (xdp
[IFLA_XDP_FLAGS
]) {
2902 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2903 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2907 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2913 if (xdp
[IFLA_XDP_FD
]) {
2914 int expected_fd
= -1;
2916 if (xdp_flags
& XDP_FLAGS_REPLACE
) {
2917 if (!xdp
[IFLA_XDP_EXPECTED_FD
]) {
2922 nla_get_s32(xdp
[IFLA_XDP_EXPECTED_FD
]);
2925 err
= dev_change_xdp_fd(dev
, extack
,
2926 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2931 status
|= DO_SETLINK_NOTIFY
;
2936 if (status
& DO_SETLINK_MODIFIED
) {
2937 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2938 netdev_state_change(dev
);
2941 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",
2948 static struct net_device
*rtnl_dev_get(struct net
*net
,
2949 struct nlattr
*ifname_attr
,
2950 struct nlattr
*altifname_attr
,
2953 char buffer
[ALTIFNAMSIZ
];
2958 nla_strscpy(ifname
, ifname_attr
, IFNAMSIZ
);
2959 else if (altifname_attr
)
2960 nla_strscpy(ifname
, altifname_attr
, ALTIFNAMSIZ
);
2965 return __dev_get_by_name(net
, ifname
);
2968 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2969 struct netlink_ext_ack
*extack
)
2971 struct net
*net
= sock_net(skb
->sk
);
2972 struct ifinfomsg
*ifm
;
2973 struct net_device
*dev
;
2975 struct nlattr
*tb
[IFLA_MAX
+1];
2976 char ifname
[IFNAMSIZ
];
2978 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
2979 ifla_policy
, extack
);
2983 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2987 if (tb
[IFLA_IFNAME
])
2988 nla_strscpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2993 ifm
= nlmsg_data(nlh
);
2994 if (ifm
->ifi_index
> 0)
2995 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2996 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
2997 dev
= rtnl_dev_get(net
, NULL
, tb
[IFLA_ALT_IFNAME
], ifname
);
3006 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
3011 static int rtnl_group_dellink(const struct net
*net
, int group
)
3013 struct net_device
*dev
, *aux
;
3014 LIST_HEAD(list_kill
);
3020 for_each_netdev(net
, dev
) {
3021 if (dev
->group
== group
) {
3022 const struct rtnl_link_ops
*ops
;
3025 ops
= dev
->rtnl_link_ops
;
3026 if (!ops
|| !ops
->dellink
)
3034 for_each_netdev_safe(net
, dev
, aux
) {
3035 if (dev
->group
== group
) {
3036 const struct rtnl_link_ops
*ops
;
3038 ops
= dev
->rtnl_link_ops
;
3039 ops
->dellink(dev
, &list_kill
);
3042 unregister_netdevice_many(&list_kill
);
3047 int rtnl_delete_link(struct net_device
*dev
)
3049 const struct rtnl_link_ops
*ops
;
3050 LIST_HEAD(list_kill
);
3052 ops
= dev
->rtnl_link_ops
;
3053 if (!ops
|| !ops
->dellink
)
3056 ops
->dellink(dev
, &list_kill
);
3057 unregister_netdevice_many(&list_kill
);
3061 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
3063 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3064 struct netlink_ext_ack
*extack
)
3066 struct net
*net
= sock_net(skb
->sk
);
3067 struct net
*tgt_net
= net
;
3068 struct net_device
*dev
= NULL
;
3069 struct ifinfomsg
*ifm
;
3070 struct nlattr
*tb
[IFLA_MAX
+1];
3074 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3075 ifla_policy
, extack
);
3079 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3083 if (tb
[IFLA_TARGET_NETNSID
]) {
3084 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3085 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3086 if (IS_ERR(tgt_net
))
3087 return PTR_ERR(tgt_net
);
3091 ifm
= nlmsg_data(nlh
);
3092 if (ifm
->ifi_index
> 0)
3093 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3094 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3095 dev
= rtnl_dev_get(net
, tb
[IFLA_IFNAME
],
3096 tb
[IFLA_ALT_IFNAME
], NULL
);
3097 else if (tb
[IFLA_GROUP
])
3098 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
3103 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
3109 err
= rtnl_delete_link(dev
);
3118 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
3120 unsigned int old_flags
;
3123 old_flags
= dev
->flags
;
3124 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
3125 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
3131 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
3132 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
));
3134 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
3135 __dev_notify_flags(dev
, old_flags
, ~0U);
3139 EXPORT_SYMBOL(rtnl_configure_link
);
3141 struct net_device
*rtnl_create_link(struct net
*net
, const char *ifname
,
3142 unsigned char name_assign_type
,
3143 const struct rtnl_link_ops
*ops
,
3144 struct nlattr
*tb
[],
3145 struct netlink_ext_ack
*extack
)
3147 struct net_device
*dev
;
3148 unsigned int num_tx_queues
= 1;
3149 unsigned int num_rx_queues
= 1;
3151 if (tb
[IFLA_NUM_TX_QUEUES
])
3152 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
3153 else if (ops
->get_num_tx_queues
)
3154 num_tx_queues
= ops
->get_num_tx_queues();
3156 if (tb
[IFLA_NUM_RX_QUEUES
])
3157 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
3158 else if (ops
->get_num_rx_queues
)
3159 num_rx_queues
= ops
->get_num_rx_queues();
3161 if (num_tx_queues
< 1 || num_tx_queues
> 4096) {
3162 NL_SET_ERR_MSG(extack
, "Invalid number of transmit queues");
3163 return ERR_PTR(-EINVAL
);
3166 if (num_rx_queues
< 1 || num_rx_queues
> 4096) {
3167 NL_SET_ERR_MSG(extack
, "Invalid number of receive queues");
3168 return ERR_PTR(-EINVAL
);
3171 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
3172 ops
->setup
, num_tx_queues
, num_rx_queues
);
3174 return ERR_PTR(-ENOMEM
);
3176 dev_net_set(dev
, net
);
3177 dev
->rtnl_link_ops
= ops
;
3178 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
3181 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3184 err
= dev_validate_mtu(dev
, mtu
, extack
);
3187 return ERR_PTR(err
);
3191 if (tb
[IFLA_ADDRESS
]) {
3192 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
3193 nla_len(tb
[IFLA_ADDRESS
]));
3194 dev
->addr_assign_type
= NET_ADDR_SET
;
3196 if (tb
[IFLA_BROADCAST
])
3197 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
3198 nla_len(tb
[IFLA_BROADCAST
]));
3199 if (tb
[IFLA_TXQLEN
])
3200 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
3201 if (tb
[IFLA_OPERSTATE
])
3202 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
3203 if (tb
[IFLA_LINKMODE
])
3204 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
3206 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
3207 if (tb
[IFLA_GSO_MAX_SIZE
])
3208 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
3209 if (tb
[IFLA_GSO_MAX_SEGS
])
3210 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
3214 EXPORT_SYMBOL(rtnl_create_link
);
3216 static int rtnl_group_changelink(const struct sk_buff
*skb
,
3217 struct net
*net
, int group
,
3218 struct ifinfomsg
*ifm
,
3219 struct netlink_ext_ack
*extack
,
3222 struct net_device
*dev
, *aux
;
3225 for_each_netdev_safe(net
, dev
, aux
) {
3226 if (dev
->group
== group
) {
3227 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
3236 static int __rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3237 struct nlattr
**attr
, struct netlink_ext_ack
*extack
)
3239 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
3240 unsigned char name_assign_type
= NET_NAME_USER
;
3241 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
3242 const struct rtnl_link_ops
*m_ops
= NULL
;
3243 struct net_device
*master_dev
= NULL
;
3244 struct net
*net
= sock_net(skb
->sk
);
3245 const struct rtnl_link_ops
*ops
;
3246 struct nlattr
*tb
[IFLA_MAX
+ 1];
3247 struct net
*dest_net
, *link_net
;
3248 struct nlattr
**slave_data
;
3249 char kind
[MODULE_NAME_LEN
];
3250 struct net_device
*dev
;
3251 struct ifinfomsg
*ifm
;
3252 char ifname
[IFNAMSIZ
];
3253 struct nlattr
**data
;
3256 #ifdef CONFIG_MODULES
3259 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3260 ifla_policy
, extack
);
3264 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
3268 if (tb
[IFLA_IFNAME
])
3269 nla_strscpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3273 ifm
= nlmsg_data(nlh
);
3274 if (ifm
->ifi_index
> 0)
3275 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3276 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3277 dev
= rtnl_dev_get(net
, NULL
, tb
[IFLA_ALT_IFNAME
], ifname
);
3282 master_dev
= netdev_master_upper_dev_get(dev
);
3284 m_ops
= master_dev
->rtnl_link_ops
;
3287 err
= validate_linkmsg(dev
, tb
);
3291 if (tb
[IFLA_LINKINFO
]) {
3292 err
= nla_parse_nested_deprecated(linkinfo
, IFLA_INFO_MAX
,
3294 ifla_info_policy
, NULL
);
3298 memset(linkinfo
, 0, sizeof(linkinfo
));
3300 if (linkinfo
[IFLA_INFO_KIND
]) {
3301 nla_strscpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
3302 ops
= rtnl_link_ops_get(kind
);
3310 if (ops
->maxtype
> RTNL_MAX_TYPE
)
3313 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3314 err
= nla_parse_nested_deprecated(attr
, ops
->maxtype
,
3315 linkinfo
[IFLA_INFO_DATA
],
3316 ops
->policy
, extack
);
3321 if (ops
->validate
) {
3322 err
= ops
->validate(tb
, data
, extack
);
3330 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3333 if (m_ops
->slave_maxtype
&&
3334 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3335 err
= nla_parse_nested_deprecated(slave_attr
,
3336 m_ops
->slave_maxtype
,
3337 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3338 m_ops
->slave_policy
,
3342 slave_data
= slave_attr
;
3349 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3351 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3354 if (linkinfo
[IFLA_INFO_DATA
]) {
3355 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
3359 err
= ops
->changelink(dev
, tb
, data
, extack
);
3362 status
|= DO_SETLINK_NOTIFY
;
3365 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3366 if (!m_ops
|| !m_ops
->slave_changelink
)
3369 err
= m_ops
->slave_changelink(master_dev
, dev
, tb
,
3370 slave_data
, extack
);
3373 status
|= DO_SETLINK_NOTIFY
;
3376 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, status
);
3379 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3380 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
3381 return rtnl_group_changelink(skb
, net
,
3382 nla_get_u32(tb
[IFLA_GROUP
]),
3387 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3391 #ifdef CONFIG_MODULES
3394 request_module("rtnl-link-%s", kind
);
3396 ops
= rtnl_link_ops_get(kind
);
3401 NL_SET_ERR_MSG(extack
, "Unknown device type");
3409 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3410 name_assign_type
= NET_NAME_ENUM
;
3413 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3414 if (IS_ERR(dest_net
))
3415 return PTR_ERR(dest_net
);
3417 if (tb
[IFLA_LINK_NETNSID
]) {
3418 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3420 link_net
= get_net_ns_by_id(dest_net
, id
);
3422 NL_SET_ERR_MSG(extack
, "Unknown network namespace id");
3427 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3433 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3434 name_assign_type
, ops
, tb
, extack
);
3440 dev
->ifindex
= ifm
->ifi_index
;
3443 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
, extack
);
3444 /* Drivers should call free_netdev() in ->destructor
3445 * and unregister it on failure after registration
3446 * so that device could be finally freed in rtnl_unlock.
3449 /* If device is not registered at all, free it now */
3450 if (dev
->reg_state
== NETREG_UNINITIALIZED
||
3451 dev
->reg_state
== NETREG_UNREGISTERED
)
3456 err
= register_netdevice(dev
);
3462 err
= rtnl_configure_link(dev
, ifm
);
3464 goto out_unregister
;
3466 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3468 goto out_unregister
;
3470 if (tb
[IFLA_MASTER
]) {
3471 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
3473 goto out_unregister
;
3482 LIST_HEAD(list_kill
);
3484 ops
->dellink(dev
, &list_kill
);
3485 unregister_netdevice_many(&list_kill
);
3487 unregister_netdevice(dev
);
3492 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3493 struct netlink_ext_ack
*extack
)
3495 struct nlattr
**attr
;
3498 attr
= kmalloc_array(RTNL_MAX_TYPE
+ 1, sizeof(*attr
), GFP_KERNEL
);
3502 ret
= __rtnl_newlink(skb
, nlh
, attr
, extack
);
3507 static int rtnl_valid_getlink_req(struct sk_buff
*skb
,
3508 const struct nlmsghdr
*nlh
,
3510 struct netlink_ext_ack
*extack
)
3512 struct ifinfomsg
*ifm
;
3515 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
3516 NL_SET_ERR_MSG(extack
, "Invalid header for get link");
3520 if (!netlink_strict_get_check(skb
))
3521 return nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3522 ifla_policy
, extack
);
3524 ifm
= nlmsg_data(nlh
);
3525 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
3527 NL_SET_ERR_MSG(extack
, "Invalid values in header for get link request");
3531 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3532 ifla_policy
, extack
);
3536 for (i
= 0; i
<= IFLA_MAX
; i
++) {
3542 case IFLA_ALT_IFNAME
:
3544 case IFLA_TARGET_NETNSID
:
3547 NL_SET_ERR_MSG(extack
, "Unsupported attribute in get link request");
3555 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3556 struct netlink_ext_ack
*extack
)
3558 struct net
*net
= sock_net(skb
->sk
);
3559 struct net
*tgt_net
= net
;
3560 struct ifinfomsg
*ifm
;
3561 struct nlattr
*tb
[IFLA_MAX
+1];
3562 struct net_device
*dev
= NULL
;
3563 struct sk_buff
*nskb
;
3566 u32 ext_filter_mask
= 0;
3568 err
= rtnl_valid_getlink_req(skb
, nlh
, tb
, extack
);
3572 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3576 if (tb
[IFLA_TARGET_NETNSID
]) {
3577 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3578 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3579 if (IS_ERR(tgt_net
))
3580 return PTR_ERR(tgt_net
);
3583 if (tb
[IFLA_EXT_MASK
])
3584 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3587 ifm
= nlmsg_data(nlh
);
3588 if (ifm
->ifi_index
> 0)
3589 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3590 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3591 dev
= rtnl_dev_get(tgt_net
, tb
[IFLA_IFNAME
],
3592 tb
[IFLA_ALT_IFNAME
], NULL
);
3601 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3605 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3606 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3607 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3608 0, NULL
, 0, netnsid
, GFP_KERNEL
);
3610 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3611 WARN_ON(err
== -EMSGSIZE
);
3614 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3622 static int rtnl_alt_ifname(int cmd
, struct net_device
*dev
, struct nlattr
*attr
,
3623 bool *changed
, struct netlink_ext_ack
*extack
)
3628 err
= nla_validate(attr
, attr
->nla_len
, IFLA_MAX
, ifla_policy
, extack
);
3632 alt_ifname
= nla_strdup(attr
, GFP_KERNEL
);
3636 if (cmd
== RTM_NEWLINKPROP
) {
3637 err
= netdev_name_node_alt_create(dev
, alt_ifname
);
3640 } else if (cmd
== RTM_DELLINKPROP
) {
3641 err
= netdev_name_node_alt_destroy(dev
, alt_ifname
);
3653 static int rtnl_linkprop(int cmd
, struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3654 struct netlink_ext_ack
*extack
)
3656 struct net
*net
= sock_net(skb
->sk
);
3657 struct nlattr
*tb
[IFLA_MAX
+ 1];
3658 struct net_device
*dev
;
3659 struct ifinfomsg
*ifm
;
3660 bool changed
= false;
3661 struct nlattr
*attr
;
3664 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3668 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3672 ifm
= nlmsg_data(nlh
);
3673 if (ifm
->ifi_index
> 0)
3674 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3675 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3676 dev
= rtnl_dev_get(net
, tb
[IFLA_IFNAME
],
3677 tb
[IFLA_ALT_IFNAME
], NULL
);
3684 if (!tb
[IFLA_PROP_LIST
])
3687 nla_for_each_nested(attr
, tb
[IFLA_PROP_LIST
], rem
) {
3688 switch (nla_type(attr
)) {
3689 case IFLA_ALT_IFNAME
:
3690 err
= rtnl_alt_ifname(cmd
, dev
, attr
, &changed
, extack
);
3698 netdev_state_change(dev
);
3702 static int rtnl_newlinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3703 struct netlink_ext_ack
*extack
)
3705 return rtnl_linkprop(RTM_NEWLINKPROP
, skb
, nlh
, extack
);
3708 static int rtnl_dellinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3709 struct netlink_ext_ack
*extack
)
3711 return rtnl_linkprop(RTM_DELLINKPROP
, skb
, nlh
, extack
);
3714 static u32
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3716 struct net
*net
= sock_net(skb
->sk
);
3717 size_t min_ifinfo_dump_size
= 0;
3718 struct nlattr
*tb
[IFLA_MAX
+1];
3719 u32 ext_filter_mask
= 0;
3720 struct net_device
*dev
;
3723 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3724 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3725 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3727 if (nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3728 if (tb
[IFLA_EXT_MASK
])
3729 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3732 if (!ext_filter_mask
)
3733 return NLMSG_GOODSIZE
;
3735 * traverse the list of net devices and compute the minimum
3736 * buffer size based upon the filter mask.
3739 for_each_netdev_rcu(net
, dev
) {
3740 min_ifinfo_dump_size
= max(min_ifinfo_dump_size
,
3741 if_nlmsg_size(dev
, ext_filter_mask
));
3745 return nlmsg_total_size(min_ifinfo_dump_size
);
3748 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3751 int s_idx
= cb
->family
;
3752 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
3758 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3759 struct rtnl_link __rcu
**tab
;
3760 struct rtnl_link
*link
;
3761 rtnl_dumpit_func dumpit
;
3763 if (idx
< s_idx
|| idx
== PF_PACKET
)
3766 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3769 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3773 link
= rcu_dereference_rtnl(tab
[type
]);
3777 dumpit
= link
->dumpit
;
3782 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3786 ret
= dumpit(skb
, cb
);
3792 return skb
->len
? : ret
;
3795 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3796 unsigned int change
,
3797 u32 event
, gfp_t flags
, int *new_nsid
,
3800 struct net
*net
= dev_net(dev
);
3801 struct sk_buff
*skb
;
3803 size_t if_info_size
;
3805 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3809 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3810 type
, 0, 0, change
, 0, 0, event
,
3811 new_nsid
, new_ifindex
, -1, flags
);
3813 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3814 WARN_ON(err
== -EMSGSIZE
);
3821 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3825 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3827 struct net
*net
= dev_net(dev
);
3829 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3832 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3833 unsigned int change
, u32 event
,
3834 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3836 struct sk_buff
*skb
;
3838 if (dev
->reg_state
!= NETREG_REGISTERED
)
3841 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3844 rtmsg_ifinfo_send(skb
, dev
, flags
);
3847 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3850 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3854 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3855 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3857 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3858 new_nsid
, new_ifindex
);
3861 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3862 struct net_device
*dev
,
3863 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3864 int type
, unsigned int flags
,
3865 int nlflags
, u16 ndm_state
)
3867 struct nlmsghdr
*nlh
;
3870 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3874 ndm
= nlmsg_data(nlh
);
3875 ndm
->ndm_family
= AF_BRIDGE
;
3878 ndm
->ndm_flags
= flags
;
3880 ndm
->ndm_ifindex
= dev
->ifindex
;
3881 ndm
->ndm_state
= ndm_state
;
3883 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3884 goto nla_put_failure
;
3886 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3887 goto nla_put_failure
;
3889 nlmsg_end(skb
, nlh
);
3893 nlmsg_cancel(skb
, nlh
);
3897 static inline size_t rtnl_fdb_nlmsg_size(void)
3899 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3900 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3901 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3905 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3908 struct net
*net
= dev_net(dev
);
3909 struct sk_buff
*skb
;
3912 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3916 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3917 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3923 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3926 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3930 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3932 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3933 struct nlattr
*tb
[],
3934 struct net_device
*dev
,
3935 const unsigned char *addr
, u16 vid
,
3940 /* If aging addresses are supported device will need to
3941 * implement its own handler for this.
3943 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3944 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3949 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3953 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3954 err
= dev_uc_add_excl(dev
, addr
);
3955 else if (is_multicast_ether_addr(addr
))
3956 err
= dev_mc_add_excl(dev
, addr
);
3958 /* Only return duplicate errors if NLM_F_EXCL is set */
3959 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3964 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3966 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3967 struct netlink_ext_ack
*extack
)
3972 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3973 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3977 vid
= nla_get_u16(vlan_attr
);
3979 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3980 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3988 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3989 struct netlink_ext_ack
*extack
)
3991 struct net
*net
= sock_net(skb
->sk
);
3993 struct nlattr
*tb
[NDA_MAX
+1];
3994 struct net_device
*dev
;
3999 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
4004 ndm
= nlmsg_data(nlh
);
4005 if (ndm
->ndm_ifindex
== 0) {
4006 NL_SET_ERR_MSG(extack
, "invalid ifindex");
4010 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
4012 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4016 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4017 NL_SET_ERR_MSG(extack
, "invalid address");
4021 if (dev
->type
!= ARPHRD_ETHER
) {
4022 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
4026 addr
= nla_data(tb
[NDA_LLADDR
]);
4028 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
4034 /* Support fdb on master device the net/bridge default case */
4035 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
4036 netif_is_bridge_port(dev
)) {
4037 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4038 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
4040 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
4041 nlh
->nlmsg_flags
, extack
);
4045 ndm
->ndm_flags
&= ~NTF_MASTER
;
4048 /* Embedded bridge, macvlan, and any other device support */
4049 if ((ndm
->ndm_flags
& NTF_SELF
)) {
4050 if (dev
->netdev_ops
->ndo_fdb_add
)
4051 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
4056 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
4060 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
4062 ndm
->ndm_flags
&= ~NTF_SELF
;
4070 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4072 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
4073 struct nlattr
*tb
[],
4074 struct net_device
*dev
,
4075 const unsigned char *addr
, u16 vid
)
4079 /* If aging addresses are supported device will need to
4080 * implement its own handler for this.
4082 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
4083 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
4087 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
4088 err
= dev_uc_del(dev
, addr
);
4089 else if (is_multicast_ether_addr(addr
))
4090 err
= dev_mc_del(dev
, addr
);
4094 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
4096 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4097 struct netlink_ext_ack
*extack
)
4099 struct net
*net
= sock_net(skb
->sk
);
4101 struct nlattr
*tb
[NDA_MAX
+1];
4102 struct net_device
*dev
;
4107 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
4110 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
4115 ndm
= nlmsg_data(nlh
);
4116 if (ndm
->ndm_ifindex
== 0) {
4117 NL_SET_ERR_MSG(extack
, "invalid ifindex");
4121 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
4123 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4127 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4128 NL_SET_ERR_MSG(extack
, "invalid address");
4132 if (dev
->type
!= ARPHRD_ETHER
) {
4133 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
4137 addr
= nla_data(tb
[NDA_LLADDR
]);
4139 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
4145 /* Support fdb on master device the net/bridge default case */
4146 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
4147 netif_is_bridge_port(dev
)) {
4148 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4149 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
4151 if (ops
->ndo_fdb_del
)
4152 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
4157 ndm
->ndm_flags
&= ~NTF_MASTER
;
4160 /* Embedded bridge, macvlan, and any other device support */
4161 if (ndm
->ndm_flags
& NTF_SELF
) {
4162 if (dev
->netdev_ops
->ndo_fdb_del
)
4163 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
4166 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
4169 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
4171 ndm
->ndm_flags
&= ~NTF_SELF
;
4178 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
4179 struct netlink_callback
*cb
,
4180 struct net_device
*dev
,
4182 struct netdev_hw_addr_list
*list
)
4184 struct netdev_hw_addr
*ha
;
4188 portid
= NETLINK_CB(cb
->skb
).portid
;
4189 seq
= cb
->nlh
->nlmsg_seq
;
4191 list_for_each_entry(ha
, &list
->list
, list
) {
4192 if (*idx
< cb
->args
[2])
4195 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
4197 RTM_NEWNEIGH
, NTF_SELF
,
4198 NLM_F_MULTI
, NUD_PERMANENT
);
4208 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4209 * @skb: socket buffer to store message in
4210 * @cb: netlink callback
4212 * @filter_dev: ignored
4213 * @idx: the number of FDB table entries dumped is added to *@idx
4215 * Default netdevice operation to dump the existing unicast address list.
4216 * Returns number of addresses from list put in skb.
4218 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
4219 struct netlink_callback
*cb
,
4220 struct net_device
*dev
,
4221 struct net_device
*filter_dev
,
4226 if (dev
->type
!= ARPHRD_ETHER
)
4229 netif_addr_lock_bh(dev
);
4230 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
4233 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
4235 netif_addr_unlock_bh(dev
);
4238 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
4240 static int valid_fdb_dump_strict(const struct nlmsghdr
*nlh
,
4241 int *br_idx
, int *brport_idx
,
4242 struct netlink_ext_ack
*extack
)
4244 struct nlattr
*tb
[NDA_MAX
+ 1];
4248 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4249 NL_SET_ERR_MSG(extack
, "Invalid header for fdb dump request");
4253 ndm
= nlmsg_data(nlh
);
4254 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4255 ndm
->ndm_flags
|| ndm
->ndm_type
) {
4256 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb dump request");
4260 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4261 NDA_MAX
, NULL
, extack
);
4265 *brport_idx
= ndm
->ndm_ifindex
;
4266 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4272 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4273 NL_SET_ERR_MSG(extack
, "Invalid IFINDEX attribute in fdb dump request");
4276 *brport_idx
= nla_get_u32(tb
[NDA_IFINDEX
]);
4279 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4280 NL_SET_ERR_MSG(extack
, "Invalid MASTER attribute in fdb dump request");
4283 *br_idx
= nla_get_u32(tb
[NDA_MASTER
]);
4286 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb dump request");
4294 static int valid_fdb_dump_legacy(const struct nlmsghdr
*nlh
,
4295 int *br_idx
, int *brport_idx
,
4296 struct netlink_ext_ack
*extack
)
4298 struct nlattr
*tb
[IFLA_MAX
+1];
4301 /* A hack to preserve kernel<->userspace interface.
4302 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4303 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4304 * So, check for ndmsg with an optional u32 attribute (not used here).
4305 * Fortunately these sizes don't conflict with the size of ifinfomsg
4306 * with an optional attribute.
4308 if (nlmsg_len(nlh
) != sizeof(struct ndmsg
) &&
4309 (nlmsg_len(nlh
) != sizeof(struct ndmsg
) +
4310 nla_attr_size(sizeof(u32
)))) {
4311 struct ifinfomsg
*ifm
;
4313 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4314 tb
, IFLA_MAX
, ifla_policy
,
4318 } else if (err
== 0) {
4319 if (tb
[IFLA_MASTER
])
4320 *br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
4323 ifm
= nlmsg_data(nlh
);
4324 *brport_idx
= ifm
->ifi_index
;
4329 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4331 struct net_device
*dev
;
4332 struct net_device
*br_dev
= NULL
;
4333 const struct net_device_ops
*ops
= NULL
;
4334 const struct net_device_ops
*cops
= NULL
;
4335 struct net
*net
= sock_net(skb
->sk
);
4336 struct hlist_head
*head
;
4344 if (cb
->strict_check
)
4345 err
= valid_fdb_dump_strict(cb
->nlh
, &br_idx
, &brport_idx
,
4348 err
= valid_fdb_dump_legacy(cb
->nlh
, &br_idx
, &brport_idx
,
4354 br_dev
= __dev_get_by_index(net
, br_idx
);
4358 ops
= br_dev
->netdev_ops
;
4362 s_idx
= cb
->args
[1];
4364 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4366 head
= &net
->dev_index_head
[h
];
4367 hlist_for_each_entry(dev
, head
, index_hlist
) {
4369 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
4372 if (!br_idx
) { /* user did not specify a specific bridge */
4373 if (netif_is_bridge_port(dev
)) {
4374 br_dev
= netdev_master_upper_dev_get(dev
);
4375 cops
= br_dev
->netdev_ops
;
4378 if (dev
!= br_dev
&&
4379 !netif_is_bridge_port(dev
))
4382 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
4383 !(dev
->priv_flags
& IFF_EBRIDGE
))
4391 if (netif_is_bridge_port(dev
)) {
4392 if (cops
&& cops
->ndo_fdb_dump
) {
4393 err
= cops
->ndo_fdb_dump(skb
, cb
,
4396 if (err
== -EMSGSIZE
)
4401 if (dev
->netdev_ops
->ndo_fdb_dump
)
4402 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
4406 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
4408 if (err
== -EMSGSIZE
)
4413 /* reset fdb offset to 0 for rest of the interfaces */
4429 static int valid_fdb_get_strict(const struct nlmsghdr
*nlh
,
4430 struct nlattr
**tb
, u8
*ndm_flags
,
4431 int *br_idx
, int *brport_idx
, u8
**addr
,
4432 u16
*vid
, struct netlink_ext_ack
*extack
)
4437 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4438 NL_SET_ERR_MSG(extack
, "Invalid header for fdb get request");
4442 ndm
= nlmsg_data(nlh
);
4443 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4445 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb get request");
4449 if (ndm
->ndm_flags
& ~(NTF_MASTER
| NTF_SELF
)) {
4450 NL_SET_ERR_MSG(extack
, "Invalid flags in header for fdb get request");
4454 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4455 NDA_MAX
, nda_policy
, extack
);
4459 *ndm_flags
= ndm
->ndm_flags
;
4460 *brport_idx
= ndm
->ndm_ifindex
;
4461 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4467 *br_idx
= nla_get_u32(tb
[i
]);
4470 if (nla_len(tb
[i
]) != ETH_ALEN
) {
4471 NL_SET_ERR_MSG(extack
, "Invalid address in fdb get request");
4474 *addr
= nla_data(tb
[i
]);
4477 err
= fdb_vid_parse(tb
[i
], vid
, extack
);
4484 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb get request");
4492 static int rtnl_fdb_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
4493 struct netlink_ext_ack
*extack
)
4495 struct net_device
*dev
= NULL
, *br_dev
= NULL
;
4496 const struct net_device_ops
*ops
= NULL
;
4497 struct net
*net
= sock_net(in_skb
->sk
);
4498 struct nlattr
*tb
[NDA_MAX
+ 1];
4499 struct sk_buff
*skb
;
4507 err
= valid_fdb_get_strict(nlh
, tb
, &ndm_flags
, &br_idx
,
4508 &brport_idx
, &addr
, &vid
, extack
);
4513 NL_SET_ERR_MSG(extack
, "Missing lookup address for fdb get request");
4518 dev
= __dev_get_by_index(net
, brport_idx
);
4520 NL_SET_ERR_MSG(extack
, "Unknown device ifindex");
4527 NL_SET_ERR_MSG(extack
, "Master and device are mutually exclusive");
4531 br_dev
= __dev_get_by_index(net
, br_idx
);
4533 NL_SET_ERR_MSG(extack
, "Invalid master ifindex");
4536 ops
= br_dev
->netdev_ops
;
4540 if (!ndm_flags
|| (ndm_flags
& NTF_MASTER
)) {
4541 if (!netif_is_bridge_port(dev
)) {
4542 NL_SET_ERR_MSG(extack
, "Device is not a bridge port");
4545 br_dev
= netdev_master_upper_dev_get(dev
);
4547 NL_SET_ERR_MSG(extack
, "Master of device not found");
4550 ops
= br_dev
->netdev_ops
;
4552 if (!(ndm_flags
& NTF_SELF
)) {
4553 NL_SET_ERR_MSG(extack
, "Missing NTF_SELF");
4556 ops
= dev
->netdev_ops
;
4560 if (!br_dev
&& !dev
) {
4561 NL_SET_ERR_MSG(extack
, "No device specified");
4565 if (!ops
|| !ops
->ndo_fdb_get
) {
4566 NL_SET_ERR_MSG(extack
, "Fdb get operation not supported by device");
4570 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
4576 err
= ops
->ndo_fdb_get(skb
, tb
, dev
, addr
, vid
,
4577 NETLINK_CB(in_skb
).portid
,
4578 nlh
->nlmsg_seq
, extack
);
4582 return rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4588 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
4589 unsigned int attrnum
, unsigned int flag
)
4592 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
4596 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
4597 struct net_device
*dev
, u16 mode
,
4598 u32 flags
, u32 mask
, int nlflags
,
4600 int (*vlan_fill
)(struct sk_buff
*skb
,
4601 struct net_device
*dev
,
4604 struct nlmsghdr
*nlh
;
4605 struct ifinfomsg
*ifm
;
4606 struct nlattr
*br_afspec
;
4607 struct nlattr
*protinfo
;
4608 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
4609 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4612 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
4616 ifm
= nlmsg_data(nlh
);
4617 ifm
->ifi_family
= AF_BRIDGE
;
4619 ifm
->ifi_type
= dev
->type
;
4620 ifm
->ifi_index
= dev
->ifindex
;
4621 ifm
->ifi_flags
= dev_get_flags(dev
);
4622 ifm
->ifi_change
= 0;
4625 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4626 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4627 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
4629 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
4631 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4632 (dev
->ifindex
!= dev_get_iflink(dev
) &&
4633 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
4634 goto nla_put_failure
;
4636 br_afspec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
4638 goto nla_put_failure
;
4640 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
4641 nla_nest_cancel(skb
, br_afspec
);
4642 goto nla_put_failure
;
4645 if (mode
!= BRIDGE_MODE_UNDEF
) {
4646 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
4647 nla_nest_cancel(skb
, br_afspec
);
4648 goto nla_put_failure
;
4652 err
= vlan_fill(skb
, dev
, filter_mask
);
4654 nla_nest_cancel(skb
, br_afspec
);
4655 goto nla_put_failure
;
4658 nla_nest_end(skb
, br_afspec
);
4660 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4662 goto nla_put_failure
;
4664 if (brport_nla_put_flag(skb
, flags
, mask
,
4665 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
4666 brport_nla_put_flag(skb
, flags
, mask
,
4667 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
4668 brport_nla_put_flag(skb
, flags
, mask
,
4669 IFLA_BRPORT_FAST_LEAVE
,
4670 BR_MULTICAST_FAST_LEAVE
) ||
4671 brport_nla_put_flag(skb
, flags
, mask
,
4672 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
4673 brport_nla_put_flag(skb
, flags
, mask
,
4674 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
4675 brport_nla_put_flag(skb
, flags
, mask
,
4676 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
4677 brport_nla_put_flag(skb
, flags
, mask
,
4678 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
4679 brport_nla_put_flag(skb
, flags
, mask
,
4680 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
) ||
4681 brport_nla_put_flag(skb
, flags
, mask
,
4682 IFLA_BRPORT_MCAST_FLOOD
, BR_MCAST_FLOOD
) ||
4683 brport_nla_put_flag(skb
, flags
, mask
,
4684 IFLA_BRPORT_BCAST_FLOOD
, BR_BCAST_FLOOD
)) {
4685 nla_nest_cancel(skb
, protinfo
);
4686 goto nla_put_failure
;
4689 nla_nest_end(skb
, protinfo
);
4691 nlmsg_end(skb
, nlh
);
4694 nlmsg_cancel(skb
, nlh
);
4695 return err
? err
: -EMSGSIZE
;
4697 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
4699 static int valid_bridge_getlink_req(const struct nlmsghdr
*nlh
,
4700 bool strict_check
, u32
*filter_mask
,
4701 struct netlink_ext_ack
*extack
)
4703 struct nlattr
*tb
[IFLA_MAX
+1];
4707 struct ifinfomsg
*ifm
;
4709 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
4710 NL_SET_ERR_MSG(extack
, "Invalid header for bridge link dump");
4714 ifm
= nlmsg_data(nlh
);
4715 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
4716 ifm
->ifi_change
|| ifm
->ifi_index
) {
4717 NL_SET_ERR_MSG(extack
, "Invalid values in header for bridge link dump request");
4721 err
= nlmsg_parse_deprecated_strict(nlh
,
4722 sizeof(struct ifinfomsg
),
4723 tb
, IFLA_MAX
, ifla_policy
,
4726 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4727 tb
, IFLA_MAX
, ifla_policy
,
4733 /* new attributes should only be added with strict checking */
4734 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
4740 *filter_mask
= nla_get_u32(tb
[i
]);
4744 NL_SET_ERR_MSG(extack
, "Unsupported attribute in bridge link dump request");
4753 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4755 const struct nlmsghdr
*nlh
= cb
->nlh
;
4756 struct net
*net
= sock_net(skb
->sk
);
4757 struct net_device
*dev
;
4759 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
4760 u32 seq
= nlh
->nlmsg_seq
;
4761 u32 filter_mask
= 0;
4764 err
= valid_bridge_getlink_req(nlh
, cb
->strict_check
, &filter_mask
,
4766 if (err
< 0 && cb
->strict_check
)
4770 for_each_netdev_rcu(net
, dev
) {
4771 const struct net_device_ops
*ops
= dev
->netdev_ops
;
4772 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4774 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
4775 if (idx
>= cb
->args
[0]) {
4776 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
4777 skb
, portid
, seq
, dev
,
4778 filter_mask
, NLM_F_MULTI
);
4779 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4780 if (likely(skb
->len
))
4789 if (ops
->ndo_bridge_getlink
) {
4790 if (idx
>= cb
->args
[0]) {
4791 err
= ops
->ndo_bridge_getlink(skb
, portid
,
4795 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4796 if (likely(skb
->len
))
4813 static inline size_t bridge_nlmsg_size(void)
4815 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4816 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4817 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4818 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
4819 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
4820 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
4821 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
4822 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
4823 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
4824 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
4825 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
4828 static int rtnl_bridge_notify(struct net_device
*dev
)
4830 struct net
*net
= dev_net(dev
);
4831 struct sk_buff
*skb
;
4832 int err
= -EOPNOTSUPP
;
4834 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
4837 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
4843 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
4850 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
4853 WARN_ON(err
== -EMSGSIZE
);
4856 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4860 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4861 struct netlink_ext_ack
*extack
)
4863 struct net
*net
= sock_net(skb
->sk
);
4864 struct ifinfomsg
*ifm
;
4865 struct net_device
*dev
;
4866 struct nlattr
*br_spec
, *attr
= NULL
;
4867 int rem
, err
= -EOPNOTSUPP
;
4869 bool have_flags
= false;
4871 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4874 ifm
= nlmsg_data(nlh
);
4875 if (ifm
->ifi_family
!= AF_BRIDGE
)
4876 return -EPFNOSUPPORT
;
4878 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4880 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4884 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4886 nla_for_each_nested(attr
, br_spec
, rem
) {
4887 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4888 if (nla_len(attr
) < sizeof(flags
))
4892 flags
= nla_get_u16(attr
);
4898 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4899 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4901 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4906 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
,
4911 flags
&= ~BRIDGE_FLAGS_MASTER
;
4914 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4915 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4918 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4922 flags
&= ~BRIDGE_FLAGS_SELF
;
4924 /* Generate event to notify upper layer of bridge
4927 err
= rtnl_bridge_notify(dev
);
4932 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4937 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4938 struct netlink_ext_ack
*extack
)
4940 struct net
*net
= sock_net(skb
->sk
);
4941 struct ifinfomsg
*ifm
;
4942 struct net_device
*dev
;
4943 struct nlattr
*br_spec
, *attr
= NULL
;
4944 int rem
, err
= -EOPNOTSUPP
;
4946 bool have_flags
= false;
4948 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4951 ifm
= nlmsg_data(nlh
);
4952 if (ifm
->ifi_family
!= AF_BRIDGE
)
4953 return -EPFNOSUPPORT
;
4955 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4957 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4961 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4963 nla_for_each_nested(attr
, br_spec
, rem
) {
4964 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4965 if (nla_len(attr
) < sizeof(flags
))
4969 flags
= nla_get_u16(attr
);
4975 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4976 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4978 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4983 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4987 flags
&= ~BRIDGE_FLAGS_MASTER
;
4990 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4991 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4994 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4998 flags
&= ~BRIDGE_FLAGS_SELF
;
5000 /* Generate event to notify upper layer of bridge
5003 err
= rtnl_bridge_notify(dev
);
5008 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
5013 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
5015 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
5016 (!idxattr
|| idxattr
== attrid
);
5019 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5020 static int rtnl_get_offload_stats_attr_size(int attr_id
)
5023 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
5024 return sizeof(struct rtnl_link_stats64
);
5030 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
5033 struct nlattr
*attr
= NULL
;
5038 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
5039 dev
->netdev_ops
->ndo_get_offload_stats
))
5042 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
5043 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
5044 if (attr_id
< *prividx
)
5047 size
= rtnl_get_offload_stats_attr_size(attr_id
);
5051 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
5054 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
5055 IFLA_OFFLOAD_XSTATS_UNSPEC
);
5057 goto nla_put_failure
;
5059 attr_data
= nla_data(attr
);
5060 memset(attr_data
, 0, size
);
5061 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
5064 goto get_offload_stats_failure
;
5075 get_offload_stats_failure
:
5080 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
5086 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
5087 dev
->netdev_ops
->ndo_get_offload_stats
))
5090 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
5091 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
5092 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
5094 size
= rtnl_get_offload_stats_attr_size(attr_id
);
5095 nla_size
+= nla_total_size_64bit(size
);
5099 nla_size
+= nla_total_size(0);
5104 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
5105 int type
, u32 pid
, u32 seq
, u32 change
,
5106 unsigned int flags
, unsigned int filter_mask
,
5107 int *idxattr
, int *prividx
)
5109 struct if_stats_msg
*ifsm
;
5110 struct nlmsghdr
*nlh
;
5111 struct nlattr
*attr
;
5112 int s_prividx
= *prividx
;
5117 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
5121 ifsm
= nlmsg_data(nlh
);
5122 ifsm
->family
= PF_UNSPEC
;
5125 ifsm
->ifindex
= dev
->ifindex
;
5126 ifsm
->filter_mask
= filter_mask
;
5128 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
5129 struct rtnl_link_stats64
*sp
;
5131 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
5132 sizeof(struct rtnl_link_stats64
),
5135 goto nla_put_failure
;
5137 sp
= nla_data(attr
);
5138 dev_get_stats(dev
, sp
);
5141 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
5142 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
5144 if (ops
&& ops
->fill_linkxstats
) {
5145 *idxattr
= IFLA_STATS_LINK_XSTATS
;
5146 attr
= nla_nest_start_noflag(skb
,
5147 IFLA_STATS_LINK_XSTATS
);
5149 goto nla_put_failure
;
5151 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5152 nla_nest_end(skb
, attr
);
5154 goto nla_put_failure
;
5159 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
5161 const struct rtnl_link_ops
*ops
= NULL
;
5162 const struct net_device
*master
;
5164 master
= netdev_master_upper_dev_get(dev
);
5166 ops
= master
->rtnl_link_ops
;
5167 if (ops
&& ops
->fill_linkxstats
) {
5168 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
5169 attr
= nla_nest_start_noflag(skb
,
5170 IFLA_STATS_LINK_XSTATS_SLAVE
);
5172 goto nla_put_failure
;
5174 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5175 nla_nest_end(skb
, attr
);
5177 goto nla_put_failure
;
5182 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
5184 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
5185 attr
= nla_nest_start_noflag(skb
,
5186 IFLA_STATS_LINK_OFFLOAD_XSTATS
);
5188 goto nla_put_failure
;
5190 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
5191 if (err
== -ENODATA
)
5192 nla_nest_cancel(skb
, attr
);
5194 nla_nest_end(skb
, attr
);
5196 if (err
&& err
!= -ENODATA
)
5197 goto nla_put_failure
;
5201 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
5202 struct rtnl_af_ops
*af_ops
;
5204 *idxattr
= IFLA_STATS_AF_SPEC
;
5205 attr
= nla_nest_start_noflag(skb
, IFLA_STATS_AF_SPEC
);
5207 goto nla_put_failure
;
5210 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
5211 if (af_ops
->fill_stats_af
) {
5215 af
= nla_nest_start_noflag(skb
,
5219 goto nla_put_failure
;
5221 err
= af_ops
->fill_stats_af(skb
, dev
);
5223 if (err
== -ENODATA
) {
5224 nla_nest_cancel(skb
, af
);
5225 } else if (err
< 0) {
5227 goto nla_put_failure
;
5230 nla_nest_end(skb
, af
);
5235 nla_nest_end(skb
, attr
);
5240 nlmsg_end(skb
, nlh
);
5245 /* not a multi message or no progress mean a real error */
5246 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
5247 nlmsg_cancel(skb
, nlh
);
5249 nlmsg_end(skb
, nlh
);
5254 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
5259 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
5260 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
5262 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
5263 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
5264 int attr
= IFLA_STATS_LINK_XSTATS
;
5266 if (ops
&& ops
->get_linkxstats_size
) {
5267 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
5269 /* for IFLA_STATS_LINK_XSTATS */
5270 size
+= nla_total_size(0);
5274 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
5275 struct net_device
*_dev
= (struct net_device
*)dev
;
5276 const struct rtnl_link_ops
*ops
= NULL
;
5277 const struct net_device
*master
;
5279 /* netdev_master_upper_dev_get can't take const */
5280 master
= netdev_master_upper_dev_get(_dev
);
5282 ops
= master
->rtnl_link_ops
;
5283 if (ops
&& ops
->get_linkxstats_size
) {
5284 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
5286 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
5288 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5289 size
+= nla_total_size(0);
5293 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
5294 size
+= rtnl_get_offload_stats_size(dev
);
5296 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
5297 struct rtnl_af_ops
*af_ops
;
5299 /* for IFLA_STATS_AF_SPEC */
5300 size
+= nla_total_size(0);
5303 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
5304 if (af_ops
->get_stats_af_size
) {
5305 size
+= nla_total_size(
5306 af_ops
->get_stats_af_size(dev
));
5309 size
+= nla_total_size(0);
5318 static int rtnl_valid_stats_req(const struct nlmsghdr
*nlh
, bool strict_check
,
5319 bool is_dump
, struct netlink_ext_ack
*extack
)
5321 struct if_stats_msg
*ifsm
;
5323 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifsm
))) {
5324 NL_SET_ERR_MSG(extack
, "Invalid header for stats dump");
5331 ifsm
= nlmsg_data(nlh
);
5333 /* only requests using strict checks can pass data to influence
5334 * the dump. The legacy exception is filter_mask.
5336 if (ifsm
->pad1
|| ifsm
->pad2
|| (is_dump
&& ifsm
->ifindex
)) {
5337 NL_SET_ERR_MSG(extack
, "Invalid values in header for stats dump request");
5340 if (nlmsg_attrlen(nlh
, sizeof(*ifsm
))) {
5341 NL_SET_ERR_MSG(extack
, "Invalid attributes after stats header");
5344 if (ifsm
->filter_mask
>= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX
+ 1)) {
5345 NL_SET_ERR_MSG(extack
, "Invalid stats requested through filter mask");
5352 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5353 struct netlink_ext_ack
*extack
)
5355 struct net
*net
= sock_net(skb
->sk
);
5356 struct net_device
*dev
= NULL
;
5357 int idxattr
= 0, prividx
= 0;
5358 struct if_stats_msg
*ifsm
;
5359 struct sk_buff
*nskb
;
5363 err
= rtnl_valid_stats_req(nlh
, netlink_strict_get_check(skb
),
5368 ifsm
= nlmsg_data(nlh
);
5369 if (ifsm
->ifindex
> 0)
5370 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
5377 filter_mask
= ifsm
->filter_mask
;
5381 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
5385 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
5386 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
5387 0, filter_mask
, &idxattr
, &prividx
);
5389 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5390 WARN_ON(err
== -EMSGSIZE
);
5393 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
5399 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5401 struct netlink_ext_ack
*extack
= cb
->extack
;
5402 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
5403 struct net
*net
= sock_net(skb
->sk
);
5404 unsigned int flags
= NLM_F_MULTI
;
5405 struct if_stats_msg
*ifsm
;
5406 struct hlist_head
*head
;
5407 struct net_device
*dev
;
5408 u32 filter_mask
= 0;
5412 s_idx
= cb
->args
[1];
5413 s_idxattr
= cb
->args
[2];
5414 s_prividx
= cb
->args
[3];
5416 cb
->seq
= net
->dev_base_seq
;
5418 err
= rtnl_valid_stats_req(cb
->nlh
, cb
->strict_check
, true, extack
);
5422 ifsm
= nlmsg_data(cb
->nlh
);
5423 filter_mask
= ifsm
->filter_mask
;
5425 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats dump");
5429 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5431 head
= &net
->dev_index_head
[h
];
5432 hlist_for_each_entry(dev
, head
, index_hlist
) {
5435 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
5436 NETLINK_CB(cb
->skb
).portid
,
5437 cb
->nlh
->nlmsg_seq
, 0,
5439 &s_idxattr
, &s_prividx
);
5440 /* If we ran out of room on the first message,
5443 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
5449 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5455 cb
->args
[3] = s_prividx
;
5456 cb
->args
[2] = s_idxattr
;
5463 /* Process one rtnetlink message. */
5465 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5466 struct netlink_ext_ack
*extack
)
5468 struct net
*net
= sock_net(skb
->sk
);
5469 struct rtnl_link
*link
;
5470 struct module
*owner
;
5471 int err
= -EOPNOTSUPP
;
5472 rtnl_doit_func doit
;
5478 type
= nlh
->nlmsg_type
;
5484 /* All the messages must have at least 1 byte length */
5485 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
5488 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
5491 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
5495 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
5497 rtnl_dumpit_func dumpit
;
5498 u32 min_dump_alloc
= 0;
5500 link
= rtnl_get_link(family
, type
);
5501 if (!link
|| !link
->dumpit
) {
5503 link
= rtnl_get_link(family
, type
);
5504 if (!link
|| !link
->dumpit
)
5507 owner
= link
->owner
;
5508 dumpit
= link
->dumpit
;
5510 if (type
== RTM_GETLINK
- RTM_BASE
)
5511 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
5514 /* need to do this before rcu_read_unlock() */
5515 if (!try_module_get(owner
))
5516 err
= -EPROTONOSUPPORT
;
5522 struct netlink_dump_control c
= {
5524 .min_dump_alloc
= min_dump_alloc
,
5527 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
5528 /* netlink_dump_start() will keep a reference on
5529 * module if dump is still in progress.
5536 link
= rtnl_get_link(family
, type
);
5537 if (!link
|| !link
->doit
) {
5539 link
= rtnl_get_link(PF_UNSPEC
, type
);
5540 if (!link
|| !link
->doit
)
5544 owner
= link
->owner
;
5545 if (!try_module_get(owner
)) {
5546 err
= -EPROTONOSUPPORT
;
5550 flags
= link
->flags
;
5551 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
5555 err
= doit(skb
, nlh
, extack
);
5562 link
= rtnl_get_link(family
, type
);
5563 if (link
&& link
->doit
)
5564 err
= link
->doit(skb
, nlh
, extack
);
5580 static void rtnetlink_rcv(struct sk_buff
*skb
)
5582 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
5585 static int rtnetlink_bind(struct net
*net
, int group
)
5588 case RTNLGRP_IPV4_MROUTE_R
:
5589 case RTNLGRP_IPV6_MROUTE_R
:
5590 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
5597 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
5599 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
5603 case NETDEV_CHANGEMTU
:
5604 case NETDEV_CHANGEADDR
:
5605 case NETDEV_CHANGENAME
:
5606 case NETDEV_FEAT_CHANGE
:
5607 case NETDEV_BONDING_FAILOVER
:
5608 case NETDEV_POST_TYPE_CHANGE
:
5609 case NETDEV_NOTIFY_PEERS
:
5610 case NETDEV_CHANGEUPPER
:
5611 case NETDEV_RESEND_IGMP
:
5612 case NETDEV_CHANGEINFODATA
:
5613 case NETDEV_CHANGELOWERSTATE
:
5614 case NETDEV_CHANGE_TX_QUEUE_LEN
:
5615 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
5616 GFP_KERNEL
, NULL
, 0);
5624 static struct notifier_block rtnetlink_dev_notifier
= {
5625 .notifier_call
= rtnetlink_event
,
5629 static int __net_init
rtnetlink_net_init(struct net
*net
)
5632 struct netlink_kernel_cfg cfg
= {
5633 .groups
= RTNLGRP_MAX
,
5634 .input
= rtnetlink_rcv
,
5635 .cb_mutex
= &rtnl_mutex
,
5636 .flags
= NL_CFG_F_NONROOT_RECV
,
5637 .bind
= rtnetlink_bind
,
5640 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
5647 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
5649 netlink_kernel_release(net
->rtnl
);
5653 static struct pernet_operations rtnetlink_net_ops
= {
5654 .init
= rtnetlink_net_init
,
5655 .exit
= rtnetlink_net_exit
,
5658 void __init
rtnetlink_init(void)
5660 if (register_pernet_subsys(&rtnetlink_net_ops
))
5661 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5663 register_netdevice_notifier(&rtnetlink_dev_notifier
);
5665 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
5666 rtnl_dump_ifinfo
, 0);
5667 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
5668 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
5669 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
5671 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
5672 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
5673 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
5675 rtnl_register(PF_UNSPEC
, RTM_NEWLINKPROP
, rtnl_newlinkprop
, NULL
, 0);
5676 rtnl_register(PF_UNSPEC
, RTM_DELLINKPROP
, rtnl_dellinkprop
, NULL
, 0);
5678 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
5679 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
5680 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, rtnl_fdb_get
, rtnl_fdb_dump
, 0);
5682 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
5683 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
5684 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
5686 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,