1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
62 rtnl_dumpit_func dumpit
;
68 static DEFINE_MUTEX(rtnl_mutex
);
72 mutex_lock(&rtnl_mutex
);
74 EXPORT_SYMBOL(rtnl_lock
);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex
);
80 EXPORT_SYMBOL(rtnl_lock_killable
);
82 static struct sk_buff
*defer_kfree_skb_list
;
83 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
86 tail
->next
= defer_kfree_skb_list
;
87 defer_kfree_skb_list
= head
;
90 EXPORT_SYMBOL(rtnl_kfree_skbs
);
92 void __rtnl_unlock(void)
94 struct sk_buff
*head
= defer_kfree_skb_list
;
96 defer_kfree_skb_list
= NULL
;
98 mutex_unlock(&rtnl_mutex
);
101 struct sk_buff
*next
= head
->next
;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock
);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex
);
120 EXPORT_SYMBOL(rtnl_trylock
);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex
);
126 EXPORT_SYMBOL(rtnl_is_locked
);
128 bool refcount_dec_and_rtnl_lock(refcount_t
*r
)
130 return refcount_dec_and_mutex_lock(r
, &rtnl_mutex
);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock
);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex
);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
144 static inline int rtm_msgindex(int msgtype
)
146 int msgindex
= msgtype
- RTM_BASE
;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
158 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
160 struct rtnl_link
**tab
;
162 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
163 protocol
= PF_UNSPEC
;
165 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
167 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
172 static int rtnl_register_internal(struct module
*owner
,
173 int protocol
, int msgtype
,
174 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
177 struct rtnl_link
*link
, *old
;
178 struct rtnl_link __rcu
**tab
;
182 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
183 msgindex
= rtm_msgindex(msgtype
);
186 tab
= rtnl_msg_handlers
[protocol
];
188 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
196 old
= rtnl_dereference(tab
[msgindex
]);
198 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
202 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
207 WARN_ON(link
->owner
&& link
->owner
!= owner
);
210 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
213 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
215 link
->dumpit
= dumpit
;
217 link
->flags
|= flags
;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab
[msgindex
], link
);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module
*owner
,
242 int protocol
, int msgtype
,
243 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
246 return rtnl_register_internal(owner
, protocol
, msgtype
,
247 doit
, dumpit
, flags
);
249 EXPORT_SYMBOL_GPL(rtnl_register_module
);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol
, int msgtype
,
268 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
273 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol
, msgtype
);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol
, int msgtype
)
289 struct rtnl_link
**tab
, *link
;
292 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
293 msgindex
= rtm_msgindex(msgtype
);
296 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
302 link
= tab
[msgindex
];
303 rcu_assign_pointer(tab
[msgindex
], NULL
);
306 kfree_rcu(link
, rcu
);
310 EXPORT_SYMBOL_GPL(rtnl_unregister
);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol
)
321 struct rtnl_link
**tab
, *link
;
324 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
327 tab
= rtnl_msg_handlers
[protocol
];
332 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
333 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
334 link
= tab
[msgindex
];
338 rcu_assign_pointer(tab
[msgindex
], NULL
);
339 kfree_rcu(link
, rcu
);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
349 static LIST_HEAD(link_ops
);
351 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
353 const struct rtnl_link_ops
*ops
;
355 list_for_each_entry(ops
, &link_ops
, list
) {
356 if (!strcmp(ops
->kind
, kind
))
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
374 if (rtnl_link_ops_get(ops
->kind
))
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops
->setup
&& !ops
->dellink
)
383 ops
->dellink
= unregister_netdevice_queue
;
385 list_add_tail(&ops
->list
, &link_ops
);
388 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops
*ops
)
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
402 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
406 err
= __rtnl_link_register(ops
);
410 EXPORT_SYMBOL_GPL(rtnl_link_register
);
412 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
414 struct net_device
*dev
;
415 LIST_HEAD(list_kill
);
417 for_each_netdev(net
, dev
) {
418 if (dev
->rtnl_link_ops
== ops
)
419 ops
->dellink(dev
, &list_kill
);
421 unregister_netdevice_many(&list_kill
);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
437 __rtnl_kill_links(net
, ops
);
439 list_del(&ops
->list
);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
450 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
452 add_wait_queue(&netdev_unregistering_wq
, &wait
);
454 unregistering
= false;
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
460 if (net
->dev_unreg_count
> 0) {
461 unregistering
= true;
469 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
471 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem
);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops
);
485 up_write(&pernet_ops_rwsem
);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
491 struct net_device
*master_dev
;
492 const struct rtnl_link_ops
*ops
;
497 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
501 ops
= master_dev
->rtnl_link_ops
;
502 if (!ops
|| !ops
->get_slave_size
)
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size
= nla_total_size(sizeof(struct nlattr
)) +
506 ops
->get_slave_size(master_dev
, dev
);
513 static size_t rtnl_link_get_size(const struct net_device
*dev
)
515 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
521 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
525 /* IFLA_INFO_DATA + nested data */
526 size
+= nla_total_size(sizeof(struct nlattr
)) +
529 if (ops
->get_xstats_size
)
530 /* IFLA_INFO_XSTATS */
531 size
+= nla_total_size(ops
->get_xstats_size(dev
));
533 size
+= rtnl_link_get_slave_info_data_size(dev
);
538 static LIST_HEAD(rtnl_af_ops
);
540 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
542 const struct rtnl_af_ops
*ops
;
544 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
545 if (ops
->family
== family
)
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops
*ops
)
561 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
564 EXPORT_SYMBOL_GPL(rtnl_af_register
);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
573 list_del_rcu(&ops
->list
);
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
580 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
583 struct rtnl_af_ops
*af_ops
;
587 size
= nla_total_size(sizeof(struct nlattr
));
590 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
591 if (af_ops
->get_link_af_size
) {
592 /* AF_* + nested data */
593 size
+= nla_total_size(sizeof(struct nlattr
)) +
594 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
602 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
604 struct net_device
*master_dev
;
609 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
610 if (master_dev
&& master_dev
->rtnl_link_ops
)
616 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
617 const struct net_device
*dev
)
619 struct net_device
*master_dev
;
620 const struct rtnl_link_ops
*ops
;
621 struct nlattr
*slave_data
;
624 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
627 ops
= master_dev
->rtnl_link_ops
;
630 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
632 if (ops
->fill_slave_info
) {
633 slave_data
= nla_nest_start_noflag(skb
, IFLA_INFO_SLAVE_DATA
);
636 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
638 goto err_cancel_slave_data
;
639 nla_nest_end(skb
, slave_data
);
643 err_cancel_slave_data
:
644 nla_nest_cancel(skb
, slave_data
);
648 static int rtnl_link_info_fill(struct sk_buff
*skb
,
649 const struct net_device
*dev
)
651 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
657 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
659 if (ops
->fill_xstats
) {
660 err
= ops
->fill_xstats(skb
, dev
);
664 if (ops
->fill_info
) {
665 data
= nla_nest_start_noflag(skb
, IFLA_INFO_DATA
);
668 err
= ops
->fill_info(skb
, dev
);
670 goto err_cancel_data
;
671 nla_nest_end(skb
, data
);
676 nla_nest_cancel(skb
, data
);
680 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
682 struct nlattr
*linkinfo
;
685 linkinfo
= nla_nest_start_noflag(skb
, IFLA_LINKINFO
);
686 if (linkinfo
== NULL
)
689 err
= rtnl_link_info_fill(skb
, dev
);
691 goto err_cancel_link
;
693 err
= rtnl_link_slave_info_fill(skb
, dev
);
695 goto err_cancel_link
;
697 nla_nest_end(skb
, linkinfo
);
701 nla_nest_cancel(skb
, linkinfo
);
706 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
708 struct sock
*rtnl
= net
->rtnl
;
711 NETLINK_CB(skb
).dst_group
= group
;
713 refcount_inc(&skb
->users
);
714 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
716 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
720 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
722 struct sock
*rtnl
= net
->rtnl
;
724 return nlmsg_unicast(rtnl
, skb
, pid
);
726 EXPORT_SYMBOL(rtnl_unicast
);
728 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
729 struct nlmsghdr
*nlh
, gfp_t flags
)
731 struct sock
*rtnl
= net
->rtnl
;
735 report
= nlmsg_report(nlh
);
737 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
739 EXPORT_SYMBOL(rtnl_notify
);
741 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
743 struct sock
*rtnl
= net
->rtnl
;
745 netlink_set_err(rtnl
, 0, group
, error
);
747 EXPORT_SYMBOL(rtnl_set_sk_err
);
749 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
754 mx
= nla_nest_start_noflag(skb
, RTA_METRICS
);
758 for (i
= 0; i
< RTAX_MAX
; i
++) {
760 if (i
== RTAX_CC_ALGO
- 1) {
761 char tmp
[TCP_CA_NAME_MAX
], *name
;
763 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
766 if (nla_put_string(skb
, i
+ 1, name
))
767 goto nla_put_failure
;
768 } else if (i
== RTAX_FEATURES
- 1) {
769 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
773 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
774 if (nla_put_u32(skb
, i
+ 1, user_features
))
775 goto nla_put_failure
;
777 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
778 goto nla_put_failure
;
785 nla_nest_cancel(skb
, mx
);
789 return nla_nest_end(skb
, mx
);
792 nla_nest_cancel(skb
, mx
);
795 EXPORT_SYMBOL(rtnetlink_put_metrics
);
797 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
798 long expires
, u32 error
)
800 struct rta_cacheinfo ci
= {
806 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
807 ci
.rta_used
= dst
->__use
;
808 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
813 clock
= jiffies_to_clock_t(abs(expires
));
814 clock
= min_t(unsigned long, clock
, INT_MAX
);
815 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
817 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
819 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
821 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
823 unsigned char operstate
= dev
->operstate
;
825 switch (transition
) {
827 if ((operstate
== IF_OPER_DORMANT
||
828 operstate
== IF_OPER_UNKNOWN
) &&
830 operstate
= IF_OPER_UP
;
833 case IF_OPER_DORMANT
:
834 if (operstate
== IF_OPER_UP
||
835 operstate
== IF_OPER_UNKNOWN
)
836 operstate
= IF_OPER_DORMANT
;
840 if (dev
->operstate
!= operstate
) {
841 write_lock_bh(&dev_base_lock
);
842 dev
->operstate
= operstate
;
843 write_unlock_bh(&dev_base_lock
);
844 netdev_state_change(dev
);
848 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
850 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
851 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
854 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
855 const struct ifinfomsg
*ifm
)
857 unsigned int flags
= ifm
->ifi_flags
;
859 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
861 flags
= (flags
& ifm
->ifi_change
) |
862 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
867 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
868 const struct rtnl_link_stats64
*b
)
870 a
->rx_packets
= b
->rx_packets
;
871 a
->tx_packets
= b
->tx_packets
;
872 a
->rx_bytes
= b
->rx_bytes
;
873 a
->tx_bytes
= b
->tx_bytes
;
874 a
->rx_errors
= b
->rx_errors
;
875 a
->tx_errors
= b
->tx_errors
;
876 a
->rx_dropped
= b
->rx_dropped
;
877 a
->tx_dropped
= b
->tx_dropped
;
879 a
->multicast
= b
->multicast
;
880 a
->collisions
= b
->collisions
;
882 a
->rx_length_errors
= b
->rx_length_errors
;
883 a
->rx_over_errors
= b
->rx_over_errors
;
884 a
->rx_crc_errors
= b
->rx_crc_errors
;
885 a
->rx_frame_errors
= b
->rx_frame_errors
;
886 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
887 a
->rx_missed_errors
= b
->rx_missed_errors
;
889 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
890 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
891 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
892 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
893 a
->tx_window_errors
= b
->tx_window_errors
;
895 a
->rx_compressed
= b
->rx_compressed
;
896 a
->tx_compressed
= b
->tx_compressed
;
898 a
->rx_nohandler
= b
->rx_nohandler
;
902 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
905 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
906 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
907 size_t size
= nla_total_size(0);
910 nla_total_size(sizeof(struct ifla_vf_mac
)) +
911 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
912 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
913 nla_total_size(MAX_VLAN_LIST_LEN
*
914 sizeof(struct ifla_vf_vlan_info
)) +
915 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
916 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
917 nla_total_size(sizeof(struct ifla_vf_rate
)) +
918 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
919 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
920 nla_total_size(0) + /* nest IFLA_VF_STATS */
921 /* IFLA_VF_STATS_RX_PACKETS */
922 nla_total_size_64bit(sizeof(__u64
)) +
923 /* IFLA_VF_STATS_TX_PACKETS */
924 nla_total_size_64bit(sizeof(__u64
)) +
925 /* IFLA_VF_STATS_RX_BYTES */
926 nla_total_size_64bit(sizeof(__u64
)) +
927 /* IFLA_VF_STATS_TX_BYTES */
928 nla_total_size_64bit(sizeof(__u64
)) +
929 /* IFLA_VF_STATS_BROADCAST */
930 nla_total_size_64bit(sizeof(__u64
)) +
931 /* IFLA_VF_STATS_MULTICAST */
932 nla_total_size_64bit(sizeof(__u64
)) +
933 /* IFLA_VF_STATS_RX_DROPPED */
934 nla_total_size_64bit(sizeof(__u64
)) +
935 /* IFLA_VF_STATS_TX_DROPPED */
936 nla_total_size_64bit(sizeof(__u64
)) +
937 nla_total_size(sizeof(struct ifla_vf_trust
)));
943 static size_t rtnl_port_size(const struct net_device
*dev
,
946 size_t port_size
= nla_total_size(4) /* PORT_VF */
947 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
948 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
949 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
950 + nla_total_size(1) /* PROT_VDP_REQUEST */
951 + nla_total_size(2); /* PORT_VDP_RESPONSE */
952 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
953 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
955 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
958 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
959 !(ext_filter_mask
& RTEXT_FILTER_VF
))
961 if (dev_num_vf(dev
->dev
.parent
))
962 return port_self_size
+ vf_ports_size
+
963 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
965 return port_self_size
;
968 static size_t rtnl_xdp_size(void)
970 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
971 nla_total_size(1) + /* XDP_ATTACHED */
972 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
973 nla_total_size(4); /* XDP_<mode>_PROG_ID */
978 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
981 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
982 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
983 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
984 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
985 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
986 + nla_total_size(sizeof(struct rtnl_link_stats
))
987 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
988 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
989 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
990 + nla_total_size(4) /* IFLA_TXQLEN */
991 + nla_total_size(4) /* IFLA_WEIGHT */
992 + nla_total_size(4) /* IFLA_MTU */
993 + nla_total_size(4) /* IFLA_LINK */
994 + nla_total_size(4) /* IFLA_MASTER */
995 + nla_total_size(1) /* IFLA_CARRIER */
996 + nla_total_size(4) /* IFLA_PROMISCUITY */
997 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
998 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1000 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1001 + nla_total_size(1) /* IFLA_OPERSTATE */
1002 + nla_total_size(1) /* IFLA_LINKMODE */
1003 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1004 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1005 + nla_total_size(4) /* IFLA_GROUP */
1006 + nla_total_size(ext_filter_mask
1007 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1008 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1009 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1010 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1011 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1013 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1014 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1015 + rtnl_xdp_size() /* IFLA_XDP */
1016 + nla_total_size(4) /* IFLA_EVENT */
1017 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1018 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1019 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1020 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1021 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1022 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1023 + nla_total_size(4) /* IFLA_MIN_MTU */
1024 + nla_total_size(4) /* IFLA_MAX_MTU */
1028 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1030 struct nlattr
*vf_ports
;
1031 struct nlattr
*vf_port
;
1035 vf_ports
= nla_nest_start_noflag(skb
, IFLA_VF_PORTS
);
1039 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1040 vf_port
= nla_nest_start_noflag(skb
, IFLA_VF_PORT
);
1042 goto nla_put_failure
;
1043 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1044 goto nla_put_failure
;
1045 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1046 if (err
== -EMSGSIZE
)
1047 goto nla_put_failure
;
1049 nla_nest_cancel(skb
, vf_port
);
1052 nla_nest_end(skb
, vf_port
);
1055 nla_nest_end(skb
, vf_ports
);
1060 nla_nest_cancel(skb
, vf_ports
);
1064 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1066 struct nlattr
*port_self
;
1069 port_self
= nla_nest_start_noflag(skb
, IFLA_PORT_SELF
);
1073 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1075 nla_nest_cancel(skb
, port_self
);
1076 return (err
== -EMSGSIZE
) ? err
: 0;
1079 nla_nest_end(skb
, port_self
);
1084 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1085 u32 ext_filter_mask
)
1089 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1090 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1093 err
= rtnl_port_self_fill(skb
, dev
);
1097 if (dev_num_vf(dev
->dev
.parent
)) {
1098 err
= rtnl_vf_ports_fill(skb
, dev
);
1106 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1109 struct netdev_phys_item_id ppid
;
1111 err
= dev_get_phys_port_id(dev
, &ppid
);
1113 if (err
== -EOPNOTSUPP
)
1118 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1124 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1126 char name
[IFNAMSIZ
];
1129 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1131 if (err
== -EOPNOTSUPP
)
1136 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1142 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1144 struct netdev_phys_item_id ppid
= { };
1147 err
= dev_get_port_parent_id(dev
, &ppid
, false);
1149 if (err
== -EOPNOTSUPP
)
1154 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, ppid
.id_len
, ppid
.id
))
1160 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1161 struct net_device
*dev
)
1163 struct rtnl_link_stats64
*sp
;
1164 struct nlattr
*attr
;
1166 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1167 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1171 sp
= nla_data(attr
);
1172 dev_get_stats(dev
, sp
);
1174 attr
= nla_reserve(skb
, IFLA_STATS
,
1175 sizeof(struct rtnl_link_stats
));
1179 copy_rtnl_link_stats(nla_data(attr
), sp
);
1184 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1185 struct net_device
*dev
,
1187 struct nlattr
*vfinfo
)
1189 struct ifla_vf_rss_query_en vf_rss_query_en
;
1190 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1191 struct ifla_vf_link_state vf_linkstate
;
1192 struct ifla_vf_vlan_info vf_vlan_info
;
1193 struct ifla_vf_spoofchk vf_spoofchk
;
1194 struct ifla_vf_tx_rate vf_tx_rate
;
1195 struct ifla_vf_stats vf_stats
;
1196 struct ifla_vf_trust vf_trust
;
1197 struct ifla_vf_vlan vf_vlan
;
1198 struct ifla_vf_rate vf_rate
;
1199 struct ifla_vf_mac vf_mac
;
1200 struct ifla_vf_info ivi
;
1202 memset(&ivi
, 0, sizeof(ivi
));
1204 /* Not all SR-IOV capable drivers support the
1205 * spoofcheck and "RSS query enable" query. Preset to
1206 * -1 so the user space tool can detect that the driver
1207 * didn't report anything.
1210 ivi
.rss_query_en
= -1;
1212 /* The default value for VF link state is "auto"
1213 * IFLA_VF_LINK_STATE_AUTO which equals zero
1216 /* VLAN Protocol by default is 802.1Q */
1217 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1218 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1221 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1230 vf_rss_query_en
.vf
=
1231 vf_trust
.vf
= ivi
.vf
;
1233 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1234 vf_vlan
.vlan
= ivi
.vlan
;
1235 vf_vlan
.qos
= ivi
.qos
;
1236 vf_vlan_info
.vlan
= ivi
.vlan
;
1237 vf_vlan_info
.qos
= ivi
.qos
;
1238 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1239 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1240 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1241 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1242 vf_spoofchk
.setting
= ivi
.spoofchk
;
1243 vf_linkstate
.link_state
= ivi
.linkstate
;
1244 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1245 vf_trust
.setting
= ivi
.trusted
;
1246 vf
= nla_nest_start_noflag(skb
, IFLA_VF_INFO
);
1248 goto nla_put_vfinfo_failure
;
1249 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1250 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1251 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1253 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1255 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1257 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1259 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1260 sizeof(vf_rss_query_en
),
1261 &vf_rss_query_en
) ||
1262 nla_put(skb
, IFLA_VF_TRUST
,
1263 sizeof(vf_trust
), &vf_trust
))
1264 goto nla_put_vf_failure
;
1265 vfvlanlist
= nla_nest_start_noflag(skb
, IFLA_VF_VLAN_LIST
);
1267 goto nla_put_vf_failure
;
1268 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1270 nla_nest_cancel(skb
, vfvlanlist
);
1271 goto nla_put_vf_failure
;
1273 nla_nest_end(skb
, vfvlanlist
);
1274 memset(&vf_stats
, 0, sizeof(vf_stats
));
1275 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1276 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1278 vfstats
= nla_nest_start_noflag(skb
, IFLA_VF_STATS
);
1280 goto nla_put_vf_failure
;
1281 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1282 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1283 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1284 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1285 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1286 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1287 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1288 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1289 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1290 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1291 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1292 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1293 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1294 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1295 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1296 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1297 nla_nest_cancel(skb
, vfstats
);
1298 goto nla_put_vf_failure
;
1300 nla_nest_end(skb
, vfstats
);
1301 nla_nest_end(skb
, vf
);
1305 nla_nest_cancel(skb
, vf
);
1306 nla_put_vfinfo_failure
:
1307 nla_nest_cancel(skb
, vfinfo
);
1311 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1312 struct net_device
*dev
,
1313 u32 ext_filter_mask
)
1315 struct nlattr
*vfinfo
;
1318 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1321 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1322 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1325 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1328 vfinfo
= nla_nest_start_noflag(skb
, IFLA_VFINFO_LIST
);
1332 for (i
= 0; i
< num_vfs
; i
++) {
1333 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1337 nla_nest_end(skb
, vfinfo
);
1341 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1343 struct rtnl_link_ifmap map
;
1345 memset(&map
, 0, sizeof(map
));
1346 map
.mem_start
= dev
->mem_start
;
1347 map
.mem_end
= dev
->mem_end
;
1348 map
.base_addr
= dev
->base_addr
;
1351 map
.port
= dev
->if_port
;
1353 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1359 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1361 const struct bpf_prog
*generic_xdp_prog
;
1365 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1366 if (!generic_xdp_prog
)
1368 return generic_xdp_prog
->aux
->id
;
1371 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1373 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
, XDP_QUERY_PROG
);
1376 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1378 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
,
1382 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1383 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1384 u32 (*get_prog_id
)(struct net_device
*dev
))
1389 curr_id
= get_prog_id(dev
);
1394 err
= nla_put_u32(skb
, attr
, curr_id
);
1398 if (*mode
!= XDP_ATTACHED_NONE
)
1399 *mode
= XDP_ATTACHED_MULTI
;
1406 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1413 xdp
= nla_nest_start_noflag(skb
, IFLA_XDP
);
1418 mode
= XDP_ATTACHED_NONE
;
1419 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1420 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1423 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1424 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1427 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1428 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1432 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1436 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1437 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1442 nla_nest_end(skb
, xdp
);
1446 nla_nest_cancel(skb
, xdp
);
1450 static u32
rtnl_get_event(unsigned long event
)
1452 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1456 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1458 case NETDEV_FEAT_CHANGE
:
1459 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1461 case NETDEV_BONDING_FAILOVER
:
1462 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1464 case NETDEV_NOTIFY_PEERS
:
1465 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1467 case NETDEV_RESEND_IGMP
:
1468 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1470 case NETDEV_CHANGEINFODATA
:
1471 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1477 return rtnl_event_type
;
1480 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1482 const struct net_device
*upper_dev
;
1487 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1489 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1495 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
,
1498 int ifindex
= dev_get_iflink(dev
);
1500 if (force
|| dev
->ifindex
!= ifindex
)
1501 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1506 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1507 struct net_device
*dev
)
1512 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1513 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1516 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1517 const struct net_device
*dev
,
1518 struct net
*src_net
)
1520 bool put_iflink
= false;
1522 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1523 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1525 if (!net_eq(dev_net(dev
), link_net
)) {
1526 int id
= peernet2id_alloc(src_net
, link_net
);
1528 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1535 return nla_put_iflink(skb
, dev
, put_iflink
);
1538 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1539 const struct net_device
*dev
,
1540 u32 ext_filter_mask
)
1542 const struct rtnl_af_ops
*af_ops
;
1543 struct nlattr
*af_spec
;
1545 af_spec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
1549 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1553 if (!af_ops
->fill_link_af
)
1556 af
= nla_nest_start_noflag(skb
, af_ops
->family
);
1560 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1562 * Caller may return ENODATA to indicate that there
1563 * was no data to be dumped. This is not an error, it
1564 * means we should trim the attribute header and
1567 if (err
== -ENODATA
)
1568 nla_nest_cancel(skb
, af
);
1572 nla_nest_end(skb
, af
);
1575 nla_nest_end(skb
, af_spec
);
1579 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1580 struct net_device
*dev
, struct net
*src_net
,
1581 int type
, u32 pid
, u32 seq
, u32 change
,
1582 unsigned int flags
, u32 ext_filter_mask
,
1583 u32 event
, int *new_nsid
, int new_ifindex
,
1586 struct ifinfomsg
*ifm
;
1587 struct nlmsghdr
*nlh
;
1590 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1594 ifm
= nlmsg_data(nlh
);
1595 ifm
->ifi_family
= AF_UNSPEC
;
1597 ifm
->ifi_type
= dev
->type
;
1598 ifm
->ifi_index
= dev
->ifindex
;
1599 ifm
->ifi_flags
= dev_get_flags(dev
);
1600 ifm
->ifi_change
= change
;
1602 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_TARGET_NETNSID
, tgt_netnsid
))
1603 goto nla_put_failure
;
1605 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1606 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1607 nla_put_u8(skb
, IFLA_OPERSTATE
,
1608 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1609 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1610 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1611 nla_put_u32(skb
, IFLA_MIN_MTU
, dev
->min_mtu
) ||
1612 nla_put_u32(skb
, IFLA_MAX_MTU
, dev
->max_mtu
) ||
1613 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1614 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1615 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1616 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1617 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1619 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1621 put_master_ifindex(skb
, dev
) ||
1622 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1624 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1625 nla_put_ifalias(skb
, dev
) ||
1626 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1627 atomic_read(&dev
->carrier_up_count
) +
1628 atomic_read(&dev
->carrier_down_count
)) ||
1629 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1630 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1631 atomic_read(&dev
->carrier_up_count
)) ||
1632 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1633 atomic_read(&dev
->carrier_down_count
)))
1634 goto nla_put_failure
;
1636 if (event
!= IFLA_EVENT_NONE
) {
1637 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1638 goto nla_put_failure
;
1641 if (rtnl_fill_link_ifmap(skb
, dev
))
1642 goto nla_put_failure
;
1644 if (dev
->addr_len
) {
1645 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1646 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1647 goto nla_put_failure
;
1650 if (rtnl_phys_port_id_fill(skb
, dev
))
1651 goto nla_put_failure
;
1653 if (rtnl_phys_port_name_fill(skb
, dev
))
1654 goto nla_put_failure
;
1656 if (rtnl_phys_switch_id_fill(skb
, dev
))
1657 goto nla_put_failure
;
1659 if (rtnl_fill_stats(skb
, dev
))
1660 goto nla_put_failure
;
1662 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1663 goto nla_put_failure
;
1665 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1666 goto nla_put_failure
;
1668 if (rtnl_xdp_fill(skb
, dev
))
1669 goto nla_put_failure
;
1671 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1672 if (rtnl_link_fill(skb
, dev
) < 0)
1673 goto nla_put_failure
;
1676 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
))
1677 goto nla_put_failure
;
1680 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1681 goto nla_put_failure
;
1683 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1684 goto nla_put_failure
;
1688 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1689 goto nla_put_failure_rcu
;
1692 nlmsg_end(skb
, nlh
);
1695 nla_put_failure_rcu
:
1698 nlmsg_cancel(skb
, nlh
);
1702 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1703 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1704 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1705 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1706 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1707 [IFLA_MTU
] = { .type
= NLA_U32
},
1708 [IFLA_LINK
] = { .type
= NLA_U32
},
1709 [IFLA_MASTER
] = { .type
= NLA_U32
},
1710 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1711 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1712 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1713 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1714 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1715 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1716 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1717 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1718 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1719 * allow 0-length string (needed to remove an alias).
1721 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1722 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1723 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1724 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1725 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1726 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1727 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1728 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1729 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1730 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1731 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1732 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1733 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1734 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1735 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1736 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1737 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1738 [IFLA_EVENT
] = { .type
= NLA_U32
},
1739 [IFLA_GROUP
] = { .type
= NLA_U32
},
1740 [IFLA_TARGET_NETNSID
] = { .type
= NLA_S32
},
1741 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1742 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1743 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
1744 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
1747 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1748 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1749 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1750 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1751 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1754 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1755 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1756 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1757 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1758 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1759 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1760 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1761 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1762 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1763 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1764 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1765 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1766 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1769 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1770 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1771 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1772 .len
= PORT_PROFILE_MAX
},
1773 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1774 .len
= PORT_UUID_MAX
},
1775 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1776 .len
= PORT_UUID_MAX
},
1777 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1778 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1780 /* Unused, but we need to keep it here since user space could
1781 * fill it. It's also broken with regard to NLA_BINARY use in
1782 * combination with structs.
1784 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1785 .len
= sizeof(struct ifla_port_vsi
) },
1788 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1789 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1790 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1791 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1792 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1795 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1797 const struct rtnl_link_ops
*ops
= NULL
;
1798 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1800 if (nla_parse_nested_deprecated(linfo
, IFLA_INFO_MAX
, nla
, ifla_info_policy
, NULL
) < 0)
1803 if (linfo
[IFLA_INFO_KIND
]) {
1804 char kind
[MODULE_NAME_LEN
];
1806 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1807 ops
= rtnl_link_ops_get(kind
);
1813 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1815 struct net_device
*master
;
1820 master
= netdev_master_upper_dev_get(dev
);
1821 if (!master
|| master
->ifindex
!= master_idx
)
1827 static bool link_kind_filtered(const struct net_device
*dev
,
1828 const struct rtnl_link_ops
*kind_ops
)
1830 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1836 static bool link_dump_filtered(struct net_device
*dev
,
1838 const struct rtnl_link_ops
*kind_ops
)
1840 if (link_master_filtered(dev
, master_idx
) ||
1841 link_kind_filtered(dev
, kind_ops
))
1848 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1849 * @sk: netlink socket
1850 * @netnsid: network namespace identifier
1852 * Returns the network namespace identified by netnsid on success or an error
1853 * pointer on failure.
1855 struct net
*rtnl_get_net_ns_capable(struct sock
*sk
, int netnsid
)
1859 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1861 return ERR_PTR(-EINVAL
);
1863 /* For now, the caller is required to have CAP_NET_ADMIN in
1864 * the user namespace owning the target net ns.
1866 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1868 return ERR_PTR(-EACCES
);
1872 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable
);
1874 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr
*nlh
,
1875 bool strict_check
, struct nlattr
**tb
,
1876 struct netlink_ext_ack
*extack
)
1881 struct ifinfomsg
*ifm
;
1883 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
1884 NL_SET_ERR_MSG(extack
, "Invalid header for link dump");
1888 ifm
= nlmsg_data(nlh
);
1889 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
1891 NL_SET_ERR_MSG(extack
, "Invalid values in header for link dump request");
1894 if (ifm
->ifi_index
) {
1895 NL_SET_ERR_MSG(extack
, "Filter by device index not supported for link dumps");
1899 return nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
,
1900 IFLA_MAX
, ifla_policy
,
1904 /* A hack to preserve kernel<->userspace interface.
1905 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1906 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1907 * what iproute2 < v3.9.0 used.
1908 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1909 * attribute, its netlink message is shorter than struct ifinfomsg.
1911 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
1912 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1914 return nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
,
1918 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1920 struct netlink_ext_ack
*extack
= cb
->extack
;
1921 const struct nlmsghdr
*nlh
= cb
->nlh
;
1922 struct net
*net
= sock_net(skb
->sk
);
1923 struct net
*tgt_net
= net
;
1926 struct net_device
*dev
;
1927 struct hlist_head
*head
;
1928 struct nlattr
*tb
[IFLA_MAX
+1];
1929 u32 ext_filter_mask
= 0;
1930 const struct rtnl_link_ops
*kind_ops
= NULL
;
1931 unsigned int flags
= NLM_F_MULTI
;
1937 s_idx
= cb
->args
[1];
1939 err
= rtnl_valid_dump_ifinfo_req(nlh
, cb
->strict_check
, tb
, extack
);
1941 if (cb
->strict_check
)
1947 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
1951 /* new attributes should only be added with strict checking */
1953 case IFLA_TARGET_NETNSID
:
1954 netnsid
= nla_get_s32(tb
[i
]);
1955 tgt_net
= rtnl_get_net_ns_capable(skb
->sk
, netnsid
);
1956 if (IS_ERR(tgt_net
)) {
1957 NL_SET_ERR_MSG(extack
, "Invalid target network namespace id");
1958 return PTR_ERR(tgt_net
);
1962 ext_filter_mask
= nla_get_u32(tb
[i
]);
1965 master_idx
= nla_get_u32(tb
[i
]);
1968 kind_ops
= linkinfo_to_kind_ops(tb
[i
]);
1971 if (cb
->strict_check
) {
1972 NL_SET_ERR_MSG(extack
, "Unsupported attribute in link dump request");
1978 if (master_idx
|| kind_ops
)
1979 flags
|= NLM_F_DUMP_FILTERED
;
1982 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1984 head
= &tgt_net
->dev_index_head
[h
];
1985 hlist_for_each_entry(dev
, head
, index_hlist
) {
1986 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1990 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1992 NETLINK_CB(cb
->skb
).portid
,
1993 nlh
->nlmsg_seq
, 0, flags
,
1994 ext_filter_mask
, 0, NULL
, 0,
1998 if (likely(skb
->len
))
2012 cb
->seq
= net
->dev_base_seq
;
2013 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2020 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
2021 struct netlink_ext_ack
*exterr
)
2023 return nla_parse_deprecated(tb
, IFLA_MAX
, head
, len
, ifla_policy
,
2026 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
2028 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
2031 /* Examine the link attributes and figure out which
2032 * network namespace we are talking about.
2034 if (tb
[IFLA_NET_NS_PID
])
2035 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
2036 else if (tb
[IFLA_NET_NS_FD
])
2037 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
2039 net
= get_net(src_net
);
2042 EXPORT_SYMBOL(rtnl_link_get_net
);
2044 /* Figure out which network namespace we are talking about by
2045 * examining the link attributes in the following order:
2047 * 1. IFLA_NET_NS_PID
2049 * 3. IFLA_TARGET_NETNSID
2051 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
2052 struct nlattr
*tb
[])
2056 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
2057 return rtnl_link_get_net(src_net
, tb
);
2059 if (!tb
[IFLA_TARGET_NETNSID
])
2060 return get_net(src_net
);
2062 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_TARGET_NETNSID
]));
2064 return ERR_PTR(-EINVAL
);
2069 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2070 struct net
*src_net
,
2071 struct nlattr
*tb
[], int cap
)
2075 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2079 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2081 return ERR_PTR(-EPERM
);
2087 /* Verify that rtnetlink requests do not pass additional properties
2088 * potentially referring to different network namespaces.
2090 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2091 struct netlink_ext_ack
*extack
,
2095 if (netns_id_only
) {
2096 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2099 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2103 if (tb
[IFLA_TARGET_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2106 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2109 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2115 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2119 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2122 if (tb
[IFLA_ADDRESS
] &&
2123 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2126 if (tb
[IFLA_BROADCAST
] &&
2127 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2131 if (tb
[IFLA_AF_SPEC
]) {
2135 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2136 const struct rtnl_af_ops
*af_ops
;
2139 af_ops
= rtnl_af_lookup(nla_type(af
));
2142 return -EAFNOSUPPORT
;
2145 if (!af_ops
->set_link_af
) {
2150 if (af_ops
->validate_link_af
) {
2151 err
= af_ops
->validate_link_af(dev
, af
);
2165 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2168 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2170 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2173 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2175 if (dev
->type
!= ARPHRD_INFINIBAND
)
2178 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2181 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2183 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2186 if (tb
[IFLA_VF_MAC
]) {
2187 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2190 if (ops
->ndo_set_vf_mac
)
2191 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2197 if (tb
[IFLA_VF_VLAN
]) {
2198 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2201 if (ops
->ndo_set_vf_vlan
)
2202 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2204 htons(ETH_P_8021Q
));
2209 if (tb
[IFLA_VF_VLAN_LIST
]) {
2210 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2211 struct nlattr
*attr
;
2215 if (!ops
->ndo_set_vf_vlan
)
2218 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2219 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2220 nla_len(attr
) < NLA_HDRLEN
) {
2223 if (len
>= MAX_VLAN_LIST_LEN
)
2225 ivvl
[len
] = nla_data(attr
);
2232 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2233 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2238 if (tb
[IFLA_VF_TX_RATE
]) {
2239 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2240 struct ifla_vf_info ivf
;
2243 if (ops
->ndo_get_vf_config
)
2244 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2249 if (ops
->ndo_set_vf_rate
)
2250 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2257 if (tb
[IFLA_VF_RATE
]) {
2258 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2261 if (ops
->ndo_set_vf_rate
)
2262 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2269 if (tb
[IFLA_VF_SPOOFCHK
]) {
2270 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2273 if (ops
->ndo_set_vf_spoofchk
)
2274 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2280 if (tb
[IFLA_VF_LINK_STATE
]) {
2281 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2284 if (ops
->ndo_set_vf_link_state
)
2285 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2291 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2292 struct ifla_vf_rss_query_en
*ivrssq_en
;
2295 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2296 if (ops
->ndo_set_vf_rss_query_en
)
2297 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2298 ivrssq_en
->setting
);
2303 if (tb
[IFLA_VF_TRUST
]) {
2304 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2307 if (ops
->ndo_set_vf_trust
)
2308 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2313 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2314 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2316 if (!ops
->ndo_set_vf_guid
)
2319 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2322 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2323 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2325 if (!ops
->ndo_set_vf_guid
)
2328 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2334 static int do_set_master(struct net_device
*dev
, int ifindex
,
2335 struct netlink_ext_ack
*extack
)
2337 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2338 const struct net_device_ops
*ops
;
2342 if (upper_dev
->ifindex
== ifindex
)
2344 ops
= upper_dev
->netdev_ops
;
2345 if (ops
->ndo_del_slave
) {
2346 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2355 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2358 ops
= upper_dev
->netdev_ops
;
2359 if (ops
->ndo_add_slave
) {
2360 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2370 #define DO_SETLINK_MODIFIED 0x01
2371 /* notify flag means notify + modified. */
2372 #define DO_SETLINK_NOTIFY 0x03
2373 static int do_setlink(const struct sk_buff
*skb
,
2374 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2375 struct netlink_ext_ack
*extack
,
2376 struct nlattr
**tb
, char *ifname
, int status
)
2378 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2381 err
= validate_linkmsg(dev
, tb
);
2385 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_TARGET_NETNSID
]) {
2386 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2393 err
= dev_change_net_namespace(dev
, net
, ifname
);
2397 status
|= DO_SETLINK_MODIFIED
;
2401 struct rtnl_link_ifmap
*u_map
;
2404 if (!ops
->ndo_set_config
) {
2409 if (!netif_device_present(dev
)) {
2414 u_map
= nla_data(tb
[IFLA_MAP
]);
2415 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2416 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2417 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2418 k_map
.irq
= (unsigned char) u_map
->irq
;
2419 k_map
.dma
= (unsigned char) u_map
->dma
;
2420 k_map
.port
= (unsigned char) u_map
->port
;
2422 err
= ops
->ndo_set_config(dev
, &k_map
);
2426 status
|= DO_SETLINK_NOTIFY
;
2429 if (tb
[IFLA_ADDRESS
]) {
2430 struct sockaddr
*sa
;
2433 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2435 sa
= kmalloc(len
, GFP_KERNEL
);
2440 sa
->sa_family
= dev
->type
;
2441 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2443 err
= dev_set_mac_address(dev
, sa
, extack
);
2447 status
|= DO_SETLINK_MODIFIED
;
2451 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
2454 status
|= DO_SETLINK_MODIFIED
;
2457 if (tb
[IFLA_GROUP
]) {
2458 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2459 status
|= DO_SETLINK_NOTIFY
;
2463 * Interface selected by interface index but interface
2464 * name provided implies that a name change has been
2467 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2468 err
= dev_change_name(dev
, ifname
);
2471 status
|= DO_SETLINK_MODIFIED
;
2474 if (tb
[IFLA_IFALIAS
]) {
2475 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2476 nla_len(tb
[IFLA_IFALIAS
]));
2479 status
|= DO_SETLINK_NOTIFY
;
2482 if (tb
[IFLA_BROADCAST
]) {
2483 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2484 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2487 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2488 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
2494 if (tb
[IFLA_MASTER
]) {
2495 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2498 status
|= DO_SETLINK_MODIFIED
;
2501 if (tb
[IFLA_CARRIER
]) {
2502 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2505 status
|= DO_SETLINK_MODIFIED
;
2508 if (tb
[IFLA_TXQLEN
]) {
2509 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2511 err
= dev_change_tx_queue_len(dev
, value
);
2514 status
|= DO_SETLINK_MODIFIED
;
2517 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2518 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2520 if (max_size
> GSO_MAX_SIZE
) {
2525 if (dev
->gso_max_size
^ max_size
) {
2526 netif_set_gso_max_size(dev
, max_size
);
2527 status
|= DO_SETLINK_MODIFIED
;
2531 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2532 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2534 if (max_segs
> GSO_MAX_SEGS
) {
2539 if (dev
->gso_max_segs
^ max_segs
) {
2540 dev
->gso_max_segs
= max_segs
;
2541 status
|= DO_SETLINK_MODIFIED
;
2545 if (tb
[IFLA_OPERSTATE
])
2546 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2548 if (tb
[IFLA_LINKMODE
]) {
2549 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2551 write_lock_bh(&dev_base_lock
);
2552 if (dev
->link_mode
^ value
)
2553 status
|= DO_SETLINK_NOTIFY
;
2554 dev
->link_mode
= value
;
2555 write_unlock_bh(&dev_base_lock
);
2558 if (tb
[IFLA_VFINFO_LIST
]) {
2559 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2560 struct nlattr
*attr
;
2563 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2564 if (nla_type(attr
) != IFLA_VF_INFO
||
2565 nla_len(attr
) < NLA_HDRLEN
) {
2569 err
= nla_parse_nested_deprecated(vfinfo
, IFLA_VF_MAX
,
2575 err
= do_setvfinfo(dev
, vfinfo
);
2578 status
|= DO_SETLINK_NOTIFY
;
2583 if (tb
[IFLA_VF_PORTS
]) {
2584 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2585 struct nlattr
*attr
;
2590 if (!ops
->ndo_set_vf_port
)
2593 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2594 if (nla_type(attr
) != IFLA_VF_PORT
||
2595 nla_len(attr
) < NLA_HDRLEN
) {
2599 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2605 if (!port
[IFLA_PORT_VF
]) {
2609 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2610 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2613 status
|= DO_SETLINK_NOTIFY
;
2618 if (tb
[IFLA_PORT_SELF
]) {
2619 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2621 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2623 ifla_port_policy
, NULL
);
2628 if (ops
->ndo_set_vf_port
)
2629 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2632 status
|= DO_SETLINK_NOTIFY
;
2635 if (tb
[IFLA_AF_SPEC
]) {
2639 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2640 const struct rtnl_af_ops
*af_ops
;
2644 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2646 err
= af_ops
->set_link_af(dev
, af
);
2653 status
|= DO_SETLINK_NOTIFY
;
2658 if (tb
[IFLA_PROTO_DOWN
]) {
2659 err
= dev_change_proto_down(dev
,
2660 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2663 status
|= DO_SETLINK_NOTIFY
;
2667 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2670 err
= nla_parse_nested_deprecated(xdp
, IFLA_XDP_MAX
,
2672 ifla_xdp_policy
, NULL
);
2676 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2681 if (xdp
[IFLA_XDP_FLAGS
]) {
2682 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2683 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2687 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2693 if (xdp
[IFLA_XDP_FD
]) {
2694 err
= dev_change_xdp_fd(dev
, extack
,
2695 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2699 status
|= DO_SETLINK_NOTIFY
;
2704 if (status
& DO_SETLINK_MODIFIED
) {
2705 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2706 netdev_state_change(dev
);
2709 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",
2716 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2717 struct netlink_ext_ack
*extack
)
2719 struct net
*net
= sock_net(skb
->sk
);
2720 struct ifinfomsg
*ifm
;
2721 struct net_device
*dev
;
2723 struct nlattr
*tb
[IFLA_MAX
+1];
2724 char ifname
[IFNAMSIZ
];
2726 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
2727 ifla_policy
, extack
);
2731 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2735 if (tb
[IFLA_IFNAME
])
2736 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2741 ifm
= nlmsg_data(nlh
);
2742 if (ifm
->ifi_index
> 0)
2743 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2744 else if (tb
[IFLA_IFNAME
])
2745 dev
= __dev_get_by_name(net
, ifname
);
2754 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2759 static int rtnl_group_dellink(const struct net
*net
, int group
)
2761 struct net_device
*dev
, *aux
;
2762 LIST_HEAD(list_kill
);
2768 for_each_netdev(net
, dev
) {
2769 if (dev
->group
== group
) {
2770 const struct rtnl_link_ops
*ops
;
2773 ops
= dev
->rtnl_link_ops
;
2774 if (!ops
|| !ops
->dellink
)
2782 for_each_netdev_safe(net
, dev
, aux
) {
2783 if (dev
->group
== group
) {
2784 const struct rtnl_link_ops
*ops
;
2786 ops
= dev
->rtnl_link_ops
;
2787 ops
->dellink(dev
, &list_kill
);
2790 unregister_netdevice_many(&list_kill
);
2795 int rtnl_delete_link(struct net_device
*dev
)
2797 const struct rtnl_link_ops
*ops
;
2798 LIST_HEAD(list_kill
);
2800 ops
= dev
->rtnl_link_ops
;
2801 if (!ops
|| !ops
->dellink
)
2804 ops
->dellink(dev
, &list_kill
);
2805 unregister_netdevice_many(&list_kill
);
2809 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2811 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2812 struct netlink_ext_ack
*extack
)
2814 struct net
*net
= sock_net(skb
->sk
);
2815 struct net
*tgt_net
= net
;
2816 struct net_device
*dev
= NULL
;
2817 struct ifinfomsg
*ifm
;
2818 char ifname
[IFNAMSIZ
];
2819 struct nlattr
*tb
[IFLA_MAX
+1];
2823 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
2824 ifla_policy
, extack
);
2828 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2832 if (tb
[IFLA_IFNAME
])
2833 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2835 if (tb
[IFLA_TARGET_NETNSID
]) {
2836 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
2837 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
2838 if (IS_ERR(tgt_net
))
2839 return PTR_ERR(tgt_net
);
2843 ifm
= nlmsg_data(nlh
);
2844 if (ifm
->ifi_index
> 0)
2845 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2846 else if (tb
[IFLA_IFNAME
])
2847 dev
= __dev_get_by_name(tgt_net
, ifname
);
2848 else if (tb
[IFLA_GROUP
])
2849 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2854 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2860 err
= rtnl_delete_link(dev
);
2869 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2871 unsigned int old_flags
;
2874 old_flags
= dev
->flags
;
2875 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2876 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
2882 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
2883 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
));
2885 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2886 __dev_notify_flags(dev
, old_flags
, ~0U);
2890 EXPORT_SYMBOL(rtnl_configure_link
);
2892 struct net_device
*rtnl_create_link(struct net
*net
, const char *ifname
,
2893 unsigned char name_assign_type
,
2894 const struct rtnl_link_ops
*ops
,
2895 struct nlattr
*tb
[],
2896 struct netlink_ext_ack
*extack
)
2898 struct net_device
*dev
;
2899 unsigned int num_tx_queues
= 1;
2900 unsigned int num_rx_queues
= 1;
2902 if (tb
[IFLA_NUM_TX_QUEUES
])
2903 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2904 else if (ops
->get_num_tx_queues
)
2905 num_tx_queues
= ops
->get_num_tx_queues();
2907 if (tb
[IFLA_NUM_RX_QUEUES
])
2908 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2909 else if (ops
->get_num_rx_queues
)
2910 num_rx_queues
= ops
->get_num_rx_queues();
2912 if (num_tx_queues
< 1 || num_tx_queues
> 4096) {
2913 NL_SET_ERR_MSG(extack
, "Invalid number of transmit queues");
2914 return ERR_PTR(-EINVAL
);
2917 if (num_rx_queues
< 1 || num_rx_queues
> 4096) {
2918 NL_SET_ERR_MSG(extack
, "Invalid number of receive queues");
2919 return ERR_PTR(-EINVAL
);
2922 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2923 ops
->setup
, num_tx_queues
, num_rx_queues
);
2925 return ERR_PTR(-ENOMEM
);
2927 dev_net_set(dev
, net
);
2928 dev
->rtnl_link_ops
= ops
;
2929 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2932 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2933 if (tb
[IFLA_ADDRESS
]) {
2934 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2935 nla_len(tb
[IFLA_ADDRESS
]));
2936 dev
->addr_assign_type
= NET_ADDR_SET
;
2938 if (tb
[IFLA_BROADCAST
])
2939 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2940 nla_len(tb
[IFLA_BROADCAST
]));
2941 if (tb
[IFLA_TXQLEN
])
2942 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2943 if (tb
[IFLA_OPERSTATE
])
2944 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2945 if (tb
[IFLA_LINKMODE
])
2946 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2948 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2949 if (tb
[IFLA_GSO_MAX_SIZE
])
2950 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
2951 if (tb
[IFLA_GSO_MAX_SEGS
])
2952 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2956 EXPORT_SYMBOL(rtnl_create_link
);
2958 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2959 struct net
*net
, int group
,
2960 struct ifinfomsg
*ifm
,
2961 struct netlink_ext_ack
*extack
,
2964 struct net_device
*dev
, *aux
;
2967 for_each_netdev_safe(net
, dev
, aux
) {
2968 if (dev
->group
== group
) {
2969 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2978 static int __rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2979 struct nlattr
**attr
, struct netlink_ext_ack
*extack
)
2981 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
2982 unsigned char name_assign_type
= NET_NAME_USER
;
2983 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
2984 const struct rtnl_link_ops
*m_ops
= NULL
;
2985 struct net_device
*master_dev
= NULL
;
2986 struct net
*net
= sock_net(skb
->sk
);
2987 const struct rtnl_link_ops
*ops
;
2988 struct nlattr
*tb
[IFLA_MAX
+ 1];
2989 struct net
*dest_net
, *link_net
;
2990 struct nlattr
**slave_data
;
2991 char kind
[MODULE_NAME_LEN
];
2992 struct net_device
*dev
;
2993 struct ifinfomsg
*ifm
;
2994 char ifname
[IFNAMSIZ
];
2995 struct nlattr
**data
;
2998 #ifdef CONFIG_MODULES
3001 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3002 ifla_policy
, extack
);
3006 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
3010 if (tb
[IFLA_IFNAME
])
3011 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3015 ifm
= nlmsg_data(nlh
);
3016 if (ifm
->ifi_index
> 0)
3017 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3020 dev
= __dev_get_by_name(net
, ifname
);
3026 master_dev
= netdev_master_upper_dev_get(dev
);
3028 m_ops
= master_dev
->rtnl_link_ops
;
3031 err
= validate_linkmsg(dev
, tb
);
3035 if (tb
[IFLA_LINKINFO
]) {
3036 err
= nla_parse_nested_deprecated(linkinfo
, IFLA_INFO_MAX
,
3038 ifla_info_policy
, NULL
);
3042 memset(linkinfo
, 0, sizeof(linkinfo
));
3044 if (linkinfo
[IFLA_INFO_KIND
]) {
3045 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
3046 ops
= rtnl_link_ops_get(kind
);
3054 if (ops
->maxtype
> RTNL_MAX_TYPE
)
3057 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3058 err
= nla_parse_nested_deprecated(attr
, ops
->maxtype
,
3059 linkinfo
[IFLA_INFO_DATA
],
3060 ops
->policy
, extack
);
3065 if (ops
->validate
) {
3066 err
= ops
->validate(tb
, data
, extack
);
3074 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3077 if (m_ops
->slave_maxtype
&&
3078 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3079 err
= nla_parse_nested_deprecated(slave_attr
,
3080 m_ops
->slave_maxtype
,
3081 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3082 m_ops
->slave_policy
,
3086 slave_data
= slave_attr
;
3093 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3095 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3098 if (linkinfo
[IFLA_INFO_DATA
]) {
3099 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
3103 err
= ops
->changelink(dev
, tb
, data
, extack
);
3106 status
|= DO_SETLINK_NOTIFY
;
3109 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3110 if (!m_ops
|| !m_ops
->slave_changelink
)
3113 err
= m_ops
->slave_changelink(master_dev
, dev
, tb
,
3114 slave_data
, extack
);
3117 status
|= DO_SETLINK_NOTIFY
;
3120 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, status
);
3123 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3124 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
3125 return rtnl_group_changelink(skb
, net
,
3126 nla_get_u32(tb
[IFLA_GROUP
]),
3131 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3135 #ifdef CONFIG_MODULES
3138 request_module("rtnl-link-%s", kind
);
3140 ops
= rtnl_link_ops_get(kind
);
3145 NL_SET_ERR_MSG(extack
, "Unknown device type");
3153 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3154 name_assign_type
= NET_NAME_ENUM
;
3157 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3158 if (IS_ERR(dest_net
))
3159 return PTR_ERR(dest_net
);
3161 if (tb
[IFLA_LINK_NETNSID
]) {
3162 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3164 link_net
= get_net_ns_by_id(dest_net
, id
);
3166 NL_SET_ERR_MSG(extack
, "Unknown network namespace id");
3171 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3177 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3178 name_assign_type
, ops
, tb
, extack
);
3184 dev
->ifindex
= ifm
->ifi_index
;
3187 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
, extack
);
3188 /* Drivers should call free_netdev() in ->destructor
3189 * and unregister it on failure after registration
3190 * so that device could be finally freed in rtnl_unlock.
3193 /* If device is not registered at all, free it now */
3194 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3199 err
= register_netdevice(dev
);
3205 err
= rtnl_configure_link(dev
, ifm
);
3207 goto out_unregister
;
3209 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3211 goto out_unregister
;
3213 if (tb
[IFLA_MASTER
]) {
3214 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
3216 goto out_unregister
;
3225 LIST_HEAD(list_kill
);
3227 ops
->dellink(dev
, &list_kill
);
3228 unregister_netdevice_many(&list_kill
);
3230 unregister_netdevice(dev
);
3235 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3236 struct netlink_ext_ack
*extack
)
3238 struct nlattr
**attr
;
3241 attr
= kmalloc_array(RTNL_MAX_TYPE
+ 1, sizeof(*attr
), GFP_KERNEL
);
3245 ret
= __rtnl_newlink(skb
, nlh
, attr
, extack
);
3250 static int rtnl_valid_getlink_req(struct sk_buff
*skb
,
3251 const struct nlmsghdr
*nlh
,
3253 struct netlink_ext_ack
*extack
)
3255 struct ifinfomsg
*ifm
;
3258 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
3259 NL_SET_ERR_MSG(extack
, "Invalid header for get link");
3263 if (!netlink_strict_get_check(skb
))
3264 return nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3265 ifla_policy
, extack
);
3267 ifm
= nlmsg_data(nlh
);
3268 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
3270 NL_SET_ERR_MSG(extack
, "Invalid values in header for get link request");
3274 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3275 ifla_policy
, extack
);
3279 for (i
= 0; i
<= IFLA_MAX
; i
++) {
3286 case IFLA_TARGET_NETNSID
:
3289 NL_SET_ERR_MSG(extack
, "Unsupported attribute in get link request");
3297 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3298 struct netlink_ext_ack
*extack
)
3300 struct net
*net
= sock_net(skb
->sk
);
3301 struct net
*tgt_net
= net
;
3302 struct ifinfomsg
*ifm
;
3303 char ifname
[IFNAMSIZ
];
3304 struct nlattr
*tb
[IFLA_MAX
+1];
3305 struct net_device
*dev
= NULL
;
3306 struct sk_buff
*nskb
;
3309 u32 ext_filter_mask
= 0;
3311 err
= rtnl_valid_getlink_req(skb
, nlh
, tb
, extack
);
3315 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3319 if (tb
[IFLA_TARGET_NETNSID
]) {
3320 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3321 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3322 if (IS_ERR(tgt_net
))
3323 return PTR_ERR(tgt_net
);
3326 if (tb
[IFLA_IFNAME
])
3327 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3329 if (tb
[IFLA_EXT_MASK
])
3330 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3333 ifm
= nlmsg_data(nlh
);
3334 if (ifm
->ifi_index
> 0)
3335 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3336 else if (tb
[IFLA_IFNAME
])
3337 dev
= __dev_get_by_name(tgt_net
, ifname
);
3346 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3350 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3351 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3352 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3353 0, NULL
, 0, netnsid
);
3355 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3356 WARN_ON(err
== -EMSGSIZE
);
3359 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3367 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3369 struct net
*net
= sock_net(skb
->sk
);
3370 struct net_device
*dev
;
3371 struct nlattr
*tb
[IFLA_MAX
+1];
3372 u32 ext_filter_mask
= 0;
3373 u16 min_ifinfo_dump_size
= 0;
3376 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3377 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3378 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3380 if (nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3381 if (tb
[IFLA_EXT_MASK
])
3382 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3385 if (!ext_filter_mask
)
3386 return NLMSG_GOODSIZE
;
3388 * traverse the list of net devices and compute the minimum
3389 * buffer size based upon the filter mask.
3392 for_each_netdev_rcu(net
, dev
) {
3393 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3399 return nlmsg_total_size(min_ifinfo_dump_size
);
3402 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3405 int s_idx
= cb
->family
;
3406 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
3412 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3413 struct rtnl_link
**tab
;
3414 struct rtnl_link
*link
;
3415 rtnl_dumpit_func dumpit
;
3417 if (idx
< s_idx
|| idx
== PF_PACKET
)
3420 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3423 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3431 dumpit
= link
->dumpit
;
3436 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3440 ret
= dumpit(skb
, cb
);
3446 return skb
->len
? : ret
;
3449 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3450 unsigned int change
,
3451 u32 event
, gfp_t flags
, int *new_nsid
,
3454 struct net
*net
= dev_net(dev
);
3455 struct sk_buff
*skb
;
3457 size_t if_info_size
;
3459 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3463 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3464 type
, 0, 0, change
, 0, 0, event
,
3465 new_nsid
, new_ifindex
, -1);
3467 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3468 WARN_ON(err
== -EMSGSIZE
);
3475 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3479 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3481 struct net
*net
= dev_net(dev
);
3483 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3486 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3487 unsigned int change
, u32 event
,
3488 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3490 struct sk_buff
*skb
;
3492 if (dev
->reg_state
!= NETREG_REGISTERED
)
3495 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3498 rtmsg_ifinfo_send(skb
, dev
, flags
);
3501 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3504 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3508 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3509 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3511 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3512 new_nsid
, new_ifindex
);
3515 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3516 struct net_device
*dev
,
3517 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3518 int type
, unsigned int flags
,
3519 int nlflags
, u16 ndm_state
)
3521 struct nlmsghdr
*nlh
;
3524 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3528 ndm
= nlmsg_data(nlh
);
3529 ndm
->ndm_family
= AF_BRIDGE
;
3532 ndm
->ndm_flags
= flags
;
3534 ndm
->ndm_ifindex
= dev
->ifindex
;
3535 ndm
->ndm_state
= ndm_state
;
3537 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3538 goto nla_put_failure
;
3540 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3541 goto nla_put_failure
;
3543 nlmsg_end(skb
, nlh
);
3547 nlmsg_cancel(skb
, nlh
);
3551 static inline size_t rtnl_fdb_nlmsg_size(void)
3553 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3554 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3555 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3559 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3562 struct net
*net
= dev_net(dev
);
3563 struct sk_buff
*skb
;
3566 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3570 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3571 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3577 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3580 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3584 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3586 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3587 struct nlattr
*tb
[],
3588 struct net_device
*dev
,
3589 const unsigned char *addr
, u16 vid
,
3594 /* If aging addresses are supported device will need to
3595 * implement its own handler for this.
3597 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3598 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3603 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3607 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3608 err
= dev_uc_add_excl(dev
, addr
);
3609 else if (is_multicast_ether_addr(addr
))
3610 err
= dev_mc_add_excl(dev
, addr
);
3612 /* Only return duplicate errors if NLM_F_EXCL is set */
3613 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3618 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3620 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3621 struct netlink_ext_ack
*extack
)
3626 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3627 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3631 vid
= nla_get_u16(vlan_attr
);
3633 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3634 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3642 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3643 struct netlink_ext_ack
*extack
)
3645 struct net
*net
= sock_net(skb
->sk
);
3647 struct nlattr
*tb
[NDA_MAX
+1];
3648 struct net_device
*dev
;
3653 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
3658 ndm
= nlmsg_data(nlh
);
3659 if (ndm
->ndm_ifindex
== 0) {
3660 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3664 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3666 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3670 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3671 NL_SET_ERR_MSG(extack
, "invalid address");
3675 if (dev
->type
!= ARPHRD_ETHER
) {
3676 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
3680 addr
= nla_data(tb
[NDA_LLADDR
]);
3682 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3688 /* Support fdb on master device the net/bridge default case */
3689 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3690 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3691 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3692 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3694 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3695 nlh
->nlmsg_flags
, extack
);
3699 ndm
->ndm_flags
&= ~NTF_MASTER
;
3702 /* Embedded bridge, macvlan, and any other device support */
3703 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3704 if (dev
->netdev_ops
->ndo_fdb_add
)
3705 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3710 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3714 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3716 ndm
->ndm_flags
&= ~NTF_SELF
;
3724 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3726 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3727 struct nlattr
*tb
[],
3728 struct net_device
*dev
,
3729 const unsigned char *addr
, u16 vid
)
3733 /* If aging addresses are supported device will need to
3734 * implement its own handler for this.
3736 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3737 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3741 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3742 err
= dev_uc_del(dev
, addr
);
3743 else if (is_multicast_ether_addr(addr
))
3744 err
= dev_mc_del(dev
, addr
);
3748 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3750 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3751 struct netlink_ext_ack
*extack
)
3753 struct net
*net
= sock_net(skb
->sk
);
3755 struct nlattr
*tb
[NDA_MAX
+1];
3756 struct net_device
*dev
;
3761 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3764 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
3769 ndm
= nlmsg_data(nlh
);
3770 if (ndm
->ndm_ifindex
== 0) {
3771 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3775 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3777 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3781 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3782 NL_SET_ERR_MSG(extack
, "invalid address");
3786 if (dev
->type
!= ARPHRD_ETHER
) {
3787 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
3791 addr
= nla_data(tb
[NDA_LLADDR
]);
3793 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3799 /* Support fdb on master device the net/bridge default case */
3800 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3801 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3802 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3803 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3805 if (ops
->ndo_fdb_del
)
3806 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3811 ndm
->ndm_flags
&= ~NTF_MASTER
;
3814 /* Embedded bridge, macvlan, and any other device support */
3815 if (ndm
->ndm_flags
& NTF_SELF
) {
3816 if (dev
->netdev_ops
->ndo_fdb_del
)
3817 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3820 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3823 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3825 ndm
->ndm_flags
&= ~NTF_SELF
;
3832 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3833 struct netlink_callback
*cb
,
3834 struct net_device
*dev
,
3836 struct netdev_hw_addr_list
*list
)
3838 struct netdev_hw_addr
*ha
;
3842 portid
= NETLINK_CB(cb
->skb
).portid
;
3843 seq
= cb
->nlh
->nlmsg_seq
;
3845 list_for_each_entry(ha
, &list
->list
, list
) {
3846 if (*idx
< cb
->args
[2])
3849 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3851 RTM_NEWNEIGH
, NTF_SELF
,
3852 NLM_F_MULTI
, NUD_PERMANENT
);
3862 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3863 * @skb: socket buffer to store message in
3864 * @cb: netlink callback
3866 * @filter_dev: ignored
3867 * @idx: the number of FDB table entries dumped is added to *@idx
3869 * Default netdevice operation to dump the existing unicast address list.
3870 * Returns number of addresses from list put in skb.
3872 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3873 struct netlink_callback
*cb
,
3874 struct net_device
*dev
,
3875 struct net_device
*filter_dev
,
3880 if (dev
->type
!= ARPHRD_ETHER
)
3883 netif_addr_lock_bh(dev
);
3884 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3887 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3889 netif_addr_unlock_bh(dev
);
3892 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3894 static int valid_fdb_dump_strict(const struct nlmsghdr
*nlh
,
3895 int *br_idx
, int *brport_idx
,
3896 struct netlink_ext_ack
*extack
)
3898 struct nlattr
*tb
[NDA_MAX
+ 1];
3902 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
3903 NL_SET_ERR_MSG(extack
, "Invalid header for fdb dump request");
3907 ndm
= nlmsg_data(nlh
);
3908 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
3909 ndm
->ndm_flags
|| ndm
->ndm_type
) {
3910 NL_SET_ERR_MSG(extack
, "Invalid values in header for fbd dump request");
3914 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
3915 NDA_MAX
, NULL
, extack
);
3919 *brport_idx
= ndm
->ndm_ifindex
;
3920 for (i
= 0; i
<= NDA_MAX
; ++i
) {
3926 if (nla_len(tb
[i
]) != sizeof(u32
)) {
3927 NL_SET_ERR_MSG(extack
, "Invalid IFINDEX attribute in fdb dump request");
3930 *brport_idx
= nla_get_u32(tb
[NDA_IFINDEX
]);
3933 if (nla_len(tb
[i
]) != sizeof(u32
)) {
3934 NL_SET_ERR_MSG(extack
, "Invalid MASTER attribute in fdb dump request");
3937 *br_idx
= nla_get_u32(tb
[NDA_MASTER
]);
3940 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb dump request");
3948 static int valid_fdb_dump_legacy(const struct nlmsghdr
*nlh
,
3949 int *br_idx
, int *brport_idx
,
3950 struct netlink_ext_ack
*extack
)
3952 struct nlattr
*tb
[IFLA_MAX
+1];
3955 /* A hack to preserve kernel<->userspace interface.
3956 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3957 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3958 * So, check for ndmsg with an optional u32 attribute (not used here).
3959 * Fortunately these sizes don't conflict with the size of ifinfomsg
3960 * with an optional attribute.
3962 if (nlmsg_len(nlh
) != sizeof(struct ndmsg
) &&
3963 (nlmsg_len(nlh
) != sizeof(struct ndmsg
) +
3964 nla_attr_size(sizeof(u32
)))) {
3965 struct ifinfomsg
*ifm
;
3967 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
3968 tb
, IFLA_MAX
, ifla_policy
,
3972 } else if (err
== 0) {
3973 if (tb
[IFLA_MASTER
])
3974 *br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3977 ifm
= nlmsg_data(nlh
);
3978 *brport_idx
= ifm
->ifi_index
;
3983 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3985 struct net_device
*dev
;
3986 struct net_device
*br_dev
= NULL
;
3987 const struct net_device_ops
*ops
= NULL
;
3988 const struct net_device_ops
*cops
= NULL
;
3989 struct net
*net
= sock_net(skb
->sk
);
3990 struct hlist_head
*head
;
3998 if (cb
->strict_check
)
3999 err
= valid_fdb_dump_strict(cb
->nlh
, &br_idx
, &brport_idx
,
4002 err
= valid_fdb_dump_legacy(cb
->nlh
, &br_idx
, &brport_idx
,
4008 br_dev
= __dev_get_by_index(net
, br_idx
);
4012 ops
= br_dev
->netdev_ops
;
4016 s_idx
= cb
->args
[1];
4018 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4020 head
= &net
->dev_index_head
[h
];
4021 hlist_for_each_entry(dev
, head
, index_hlist
) {
4023 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
4026 if (!br_idx
) { /* user did not specify a specific bridge */
4027 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
4028 br_dev
= netdev_master_upper_dev_get(dev
);
4029 cops
= br_dev
->netdev_ops
;
4032 if (dev
!= br_dev
&&
4033 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
4036 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
4037 !(dev
->priv_flags
& IFF_EBRIDGE
))
4045 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
4046 if (cops
&& cops
->ndo_fdb_dump
) {
4047 err
= cops
->ndo_fdb_dump(skb
, cb
,
4050 if (err
== -EMSGSIZE
)
4055 if (dev
->netdev_ops
->ndo_fdb_dump
)
4056 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
4060 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
4062 if (err
== -EMSGSIZE
)
4067 /* reset fdb offset to 0 for rest of the interfaces */
4083 static int valid_fdb_get_strict(const struct nlmsghdr
*nlh
,
4084 struct nlattr
**tb
, u8
*ndm_flags
,
4085 int *br_idx
, int *brport_idx
, u8
**addr
,
4086 u16
*vid
, struct netlink_ext_ack
*extack
)
4091 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4092 NL_SET_ERR_MSG(extack
, "Invalid header for fdb get request");
4096 ndm
= nlmsg_data(nlh
);
4097 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4099 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb get request");
4103 if (ndm
->ndm_flags
& ~(NTF_MASTER
| NTF_SELF
)) {
4104 NL_SET_ERR_MSG(extack
, "Invalid flags in header for fdb get request");
4108 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4109 NDA_MAX
, nda_policy
, extack
);
4113 *ndm_flags
= ndm
->ndm_flags
;
4114 *brport_idx
= ndm
->ndm_ifindex
;
4115 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4121 *br_idx
= nla_get_u32(tb
[i
]);
4124 if (nla_len(tb
[i
]) != ETH_ALEN
) {
4125 NL_SET_ERR_MSG(extack
, "Invalid address in fdb get request");
4128 *addr
= nla_data(tb
[i
]);
4131 err
= fdb_vid_parse(tb
[i
], vid
, extack
);
4138 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb get request");
4146 static int rtnl_fdb_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
4147 struct netlink_ext_ack
*extack
)
4149 struct net_device
*dev
= NULL
, *br_dev
= NULL
;
4150 const struct net_device_ops
*ops
= NULL
;
4151 struct net
*net
= sock_net(in_skb
->sk
);
4152 struct nlattr
*tb
[NDA_MAX
+ 1];
4153 struct sk_buff
*skb
;
4161 err
= valid_fdb_get_strict(nlh
, tb
, &ndm_flags
, &br_idx
,
4162 &brport_idx
, &addr
, &vid
, extack
);
4167 NL_SET_ERR_MSG(extack
, "Missing lookup address for fdb get request");
4172 dev
= __dev_get_by_index(net
, brport_idx
);
4174 NL_SET_ERR_MSG(extack
, "Unknown device ifindex");
4181 NL_SET_ERR_MSG(extack
, "Master and device are mutually exclusive");
4185 br_dev
= __dev_get_by_index(net
, br_idx
);
4187 NL_SET_ERR_MSG(extack
, "Invalid master ifindex");
4190 ops
= br_dev
->netdev_ops
;
4194 if (!ndm_flags
|| (ndm_flags
& NTF_MASTER
)) {
4195 if (!(dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
4196 NL_SET_ERR_MSG(extack
, "Device is not a bridge port");
4199 br_dev
= netdev_master_upper_dev_get(dev
);
4201 NL_SET_ERR_MSG(extack
, "Master of device not found");
4204 ops
= br_dev
->netdev_ops
;
4206 if (!(ndm_flags
& NTF_SELF
)) {
4207 NL_SET_ERR_MSG(extack
, "Missing NTF_SELF");
4210 ops
= dev
->netdev_ops
;
4214 if (!br_dev
&& !dev
) {
4215 NL_SET_ERR_MSG(extack
, "No device specified");
4219 if (!ops
|| !ops
->ndo_fdb_get
) {
4220 NL_SET_ERR_MSG(extack
, "Fdb get operation not supported by device");
4224 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
4230 err
= ops
->ndo_fdb_get(skb
, tb
, dev
, addr
, vid
,
4231 NETLINK_CB(in_skb
).portid
,
4232 nlh
->nlmsg_seq
, extack
);
4236 return rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4242 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
4243 unsigned int attrnum
, unsigned int flag
)
4246 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
4250 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
4251 struct net_device
*dev
, u16 mode
,
4252 u32 flags
, u32 mask
, int nlflags
,
4254 int (*vlan_fill
)(struct sk_buff
*skb
,
4255 struct net_device
*dev
,
4258 struct nlmsghdr
*nlh
;
4259 struct ifinfomsg
*ifm
;
4260 struct nlattr
*br_afspec
;
4261 struct nlattr
*protinfo
;
4262 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
4263 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4266 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
4270 ifm
= nlmsg_data(nlh
);
4271 ifm
->ifi_family
= AF_BRIDGE
;
4273 ifm
->ifi_type
= dev
->type
;
4274 ifm
->ifi_index
= dev
->ifindex
;
4275 ifm
->ifi_flags
= dev_get_flags(dev
);
4276 ifm
->ifi_change
= 0;
4279 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4280 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4281 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
4283 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
4285 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4286 (dev
->ifindex
!= dev_get_iflink(dev
) &&
4287 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
4288 goto nla_put_failure
;
4290 br_afspec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
4292 goto nla_put_failure
;
4294 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
4295 nla_nest_cancel(skb
, br_afspec
);
4296 goto nla_put_failure
;
4299 if (mode
!= BRIDGE_MODE_UNDEF
) {
4300 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
4301 nla_nest_cancel(skb
, br_afspec
);
4302 goto nla_put_failure
;
4306 err
= vlan_fill(skb
, dev
, filter_mask
);
4308 nla_nest_cancel(skb
, br_afspec
);
4309 goto nla_put_failure
;
4312 nla_nest_end(skb
, br_afspec
);
4314 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4316 goto nla_put_failure
;
4318 if (brport_nla_put_flag(skb
, flags
, mask
,
4319 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
4320 brport_nla_put_flag(skb
, flags
, mask
,
4321 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
4322 brport_nla_put_flag(skb
, flags
, mask
,
4323 IFLA_BRPORT_FAST_LEAVE
,
4324 BR_MULTICAST_FAST_LEAVE
) ||
4325 brport_nla_put_flag(skb
, flags
, mask
,
4326 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
4327 brport_nla_put_flag(skb
, flags
, mask
,
4328 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
4329 brport_nla_put_flag(skb
, flags
, mask
,
4330 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
4331 brport_nla_put_flag(skb
, flags
, mask
,
4332 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
4333 brport_nla_put_flag(skb
, flags
, mask
,
4334 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
4335 nla_nest_cancel(skb
, protinfo
);
4336 goto nla_put_failure
;
4339 nla_nest_end(skb
, protinfo
);
4341 nlmsg_end(skb
, nlh
);
4344 nlmsg_cancel(skb
, nlh
);
4345 return err
? err
: -EMSGSIZE
;
4347 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
4349 static int valid_bridge_getlink_req(const struct nlmsghdr
*nlh
,
4350 bool strict_check
, u32
*filter_mask
,
4351 struct netlink_ext_ack
*extack
)
4353 struct nlattr
*tb
[IFLA_MAX
+1];
4357 struct ifinfomsg
*ifm
;
4359 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
4360 NL_SET_ERR_MSG(extack
, "Invalid header for bridge link dump");
4364 ifm
= nlmsg_data(nlh
);
4365 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
4366 ifm
->ifi_change
|| ifm
->ifi_index
) {
4367 NL_SET_ERR_MSG(extack
, "Invalid values in header for bridge link dump request");
4371 err
= nlmsg_parse_deprecated_strict(nlh
,
4372 sizeof(struct ifinfomsg
),
4373 tb
, IFLA_MAX
, ifla_policy
,
4376 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4377 tb
, IFLA_MAX
, ifla_policy
,
4383 /* new attributes should only be added with strict checking */
4384 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
4390 *filter_mask
= nla_get_u32(tb
[i
]);
4394 NL_SET_ERR_MSG(extack
, "Unsupported attribute in bridge link dump request");
4403 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4405 const struct nlmsghdr
*nlh
= cb
->nlh
;
4406 struct net
*net
= sock_net(skb
->sk
);
4407 struct net_device
*dev
;
4409 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
4410 u32 seq
= nlh
->nlmsg_seq
;
4411 u32 filter_mask
= 0;
4414 err
= valid_bridge_getlink_req(nlh
, cb
->strict_check
, &filter_mask
,
4416 if (err
< 0 && cb
->strict_check
)
4420 for_each_netdev_rcu(net
, dev
) {
4421 const struct net_device_ops
*ops
= dev
->netdev_ops
;
4422 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4424 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
4425 if (idx
>= cb
->args
[0]) {
4426 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
4427 skb
, portid
, seq
, dev
,
4428 filter_mask
, NLM_F_MULTI
);
4429 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4430 if (likely(skb
->len
))
4439 if (ops
->ndo_bridge_getlink
) {
4440 if (idx
>= cb
->args
[0]) {
4441 err
= ops
->ndo_bridge_getlink(skb
, portid
,
4445 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4446 if (likely(skb
->len
))
4463 static inline size_t bridge_nlmsg_size(void)
4465 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4466 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4467 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4468 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
4469 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
4470 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
4471 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
4472 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
4473 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
4474 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
4475 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
4478 static int rtnl_bridge_notify(struct net_device
*dev
)
4480 struct net
*net
= dev_net(dev
);
4481 struct sk_buff
*skb
;
4482 int err
= -EOPNOTSUPP
;
4484 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
4487 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
4493 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
4500 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
4503 WARN_ON(err
== -EMSGSIZE
);
4506 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4510 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4511 struct netlink_ext_ack
*extack
)
4513 struct net
*net
= sock_net(skb
->sk
);
4514 struct ifinfomsg
*ifm
;
4515 struct net_device
*dev
;
4516 struct nlattr
*br_spec
, *attr
= NULL
;
4517 int rem
, err
= -EOPNOTSUPP
;
4519 bool have_flags
= false;
4521 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4524 ifm
= nlmsg_data(nlh
);
4525 if (ifm
->ifi_family
!= AF_BRIDGE
)
4526 return -EPFNOSUPPORT
;
4528 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4530 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4534 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4536 nla_for_each_nested(attr
, br_spec
, rem
) {
4537 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4538 if (nla_len(attr
) < sizeof(flags
))
4542 flags
= nla_get_u16(attr
);
4548 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4549 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4551 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4556 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
,
4561 flags
&= ~BRIDGE_FLAGS_MASTER
;
4564 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4565 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4568 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4572 flags
&= ~BRIDGE_FLAGS_SELF
;
4574 /* Generate event to notify upper layer of bridge
4577 err
= rtnl_bridge_notify(dev
);
4582 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4587 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4588 struct netlink_ext_ack
*extack
)
4590 struct net
*net
= sock_net(skb
->sk
);
4591 struct ifinfomsg
*ifm
;
4592 struct net_device
*dev
;
4593 struct nlattr
*br_spec
, *attr
= NULL
;
4594 int rem
, err
= -EOPNOTSUPP
;
4596 bool have_flags
= false;
4598 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4601 ifm
= nlmsg_data(nlh
);
4602 if (ifm
->ifi_family
!= AF_BRIDGE
)
4603 return -EPFNOSUPPORT
;
4605 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4607 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4611 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4613 nla_for_each_nested(attr
, br_spec
, rem
) {
4614 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4615 if (nla_len(attr
) < sizeof(flags
))
4619 flags
= nla_get_u16(attr
);
4625 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4626 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4628 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4633 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4637 flags
&= ~BRIDGE_FLAGS_MASTER
;
4640 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4641 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4644 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4648 flags
&= ~BRIDGE_FLAGS_SELF
;
4650 /* Generate event to notify upper layer of bridge
4653 err
= rtnl_bridge_notify(dev
);
4658 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4663 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4665 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4666 (!idxattr
|| idxattr
== attrid
);
4669 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4670 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4673 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4674 return sizeof(struct rtnl_link_stats64
);
4680 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4683 struct nlattr
*attr
= NULL
;
4688 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4689 dev
->netdev_ops
->ndo_get_offload_stats
))
4692 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4693 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4694 if (attr_id
< *prividx
)
4697 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4701 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4704 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4705 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4707 goto nla_put_failure
;
4709 attr_data
= nla_data(attr
);
4710 memset(attr_data
, 0, size
);
4711 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4714 goto get_offload_stats_failure
;
4725 get_offload_stats_failure
:
4730 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4736 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4737 dev
->netdev_ops
->ndo_get_offload_stats
))
4740 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4741 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4742 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4744 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4745 nla_size
+= nla_total_size_64bit(size
);
4749 nla_size
+= nla_total_size(0);
4754 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4755 int type
, u32 pid
, u32 seq
, u32 change
,
4756 unsigned int flags
, unsigned int filter_mask
,
4757 int *idxattr
, int *prividx
)
4759 struct if_stats_msg
*ifsm
;
4760 struct nlmsghdr
*nlh
;
4761 struct nlattr
*attr
;
4762 int s_prividx
= *prividx
;
4767 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4771 ifsm
= nlmsg_data(nlh
);
4772 ifsm
->family
= PF_UNSPEC
;
4775 ifsm
->ifindex
= dev
->ifindex
;
4776 ifsm
->filter_mask
= filter_mask
;
4778 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4779 struct rtnl_link_stats64
*sp
;
4781 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4782 sizeof(struct rtnl_link_stats64
),
4785 goto nla_put_failure
;
4787 sp
= nla_data(attr
);
4788 dev_get_stats(dev
, sp
);
4791 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4792 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4794 if (ops
&& ops
->fill_linkxstats
) {
4795 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4796 attr
= nla_nest_start_noflag(skb
,
4797 IFLA_STATS_LINK_XSTATS
);
4799 goto nla_put_failure
;
4801 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4802 nla_nest_end(skb
, attr
);
4804 goto nla_put_failure
;
4809 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4811 const struct rtnl_link_ops
*ops
= NULL
;
4812 const struct net_device
*master
;
4814 master
= netdev_master_upper_dev_get(dev
);
4816 ops
= master
->rtnl_link_ops
;
4817 if (ops
&& ops
->fill_linkxstats
) {
4818 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4819 attr
= nla_nest_start_noflag(skb
,
4820 IFLA_STATS_LINK_XSTATS_SLAVE
);
4822 goto nla_put_failure
;
4824 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4825 nla_nest_end(skb
, attr
);
4827 goto nla_put_failure
;
4832 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4834 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4835 attr
= nla_nest_start_noflag(skb
,
4836 IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4838 goto nla_put_failure
;
4840 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4841 if (err
== -ENODATA
)
4842 nla_nest_cancel(skb
, attr
);
4844 nla_nest_end(skb
, attr
);
4846 if (err
&& err
!= -ENODATA
)
4847 goto nla_put_failure
;
4851 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4852 struct rtnl_af_ops
*af_ops
;
4854 *idxattr
= IFLA_STATS_AF_SPEC
;
4855 attr
= nla_nest_start_noflag(skb
, IFLA_STATS_AF_SPEC
);
4857 goto nla_put_failure
;
4860 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4861 if (af_ops
->fill_stats_af
) {
4865 af
= nla_nest_start_noflag(skb
,
4869 goto nla_put_failure
;
4871 err
= af_ops
->fill_stats_af(skb
, dev
);
4873 if (err
== -ENODATA
) {
4874 nla_nest_cancel(skb
, af
);
4875 } else if (err
< 0) {
4877 goto nla_put_failure
;
4880 nla_nest_end(skb
, af
);
4885 nla_nest_end(skb
, attr
);
4890 nlmsg_end(skb
, nlh
);
4895 /* not a multi message or no progress mean a real error */
4896 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4897 nlmsg_cancel(skb
, nlh
);
4899 nlmsg_end(skb
, nlh
);
4904 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4909 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4910 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4912 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4913 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4914 int attr
= IFLA_STATS_LINK_XSTATS
;
4916 if (ops
&& ops
->get_linkxstats_size
) {
4917 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4919 /* for IFLA_STATS_LINK_XSTATS */
4920 size
+= nla_total_size(0);
4924 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4925 struct net_device
*_dev
= (struct net_device
*)dev
;
4926 const struct rtnl_link_ops
*ops
= NULL
;
4927 const struct net_device
*master
;
4929 /* netdev_master_upper_dev_get can't take const */
4930 master
= netdev_master_upper_dev_get(_dev
);
4932 ops
= master
->rtnl_link_ops
;
4933 if (ops
&& ops
->get_linkxstats_size
) {
4934 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4936 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4938 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4939 size
+= nla_total_size(0);
4943 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4944 size
+= rtnl_get_offload_stats_size(dev
);
4946 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4947 struct rtnl_af_ops
*af_ops
;
4949 /* for IFLA_STATS_AF_SPEC */
4950 size
+= nla_total_size(0);
4953 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4954 if (af_ops
->get_stats_af_size
) {
4955 size
+= nla_total_size(
4956 af_ops
->get_stats_af_size(dev
));
4959 size
+= nla_total_size(0);
4968 static int rtnl_valid_stats_req(const struct nlmsghdr
*nlh
, bool strict_check
,
4969 bool is_dump
, struct netlink_ext_ack
*extack
)
4971 struct if_stats_msg
*ifsm
;
4973 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifsm
))) {
4974 NL_SET_ERR_MSG(extack
, "Invalid header for stats dump");
4981 ifsm
= nlmsg_data(nlh
);
4983 /* only requests using strict checks can pass data to influence
4984 * the dump. The legacy exception is filter_mask.
4986 if (ifsm
->pad1
|| ifsm
->pad2
|| (is_dump
&& ifsm
->ifindex
)) {
4987 NL_SET_ERR_MSG(extack
, "Invalid values in header for stats dump request");
4990 if (nlmsg_attrlen(nlh
, sizeof(*ifsm
))) {
4991 NL_SET_ERR_MSG(extack
, "Invalid attributes after stats header");
4994 if (ifsm
->filter_mask
>= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX
+ 1)) {
4995 NL_SET_ERR_MSG(extack
, "Invalid stats requested through filter mask");
5002 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5003 struct netlink_ext_ack
*extack
)
5005 struct net
*net
= sock_net(skb
->sk
);
5006 struct net_device
*dev
= NULL
;
5007 int idxattr
= 0, prividx
= 0;
5008 struct if_stats_msg
*ifsm
;
5009 struct sk_buff
*nskb
;
5013 err
= rtnl_valid_stats_req(nlh
, netlink_strict_get_check(skb
),
5018 ifsm
= nlmsg_data(nlh
);
5019 if (ifsm
->ifindex
> 0)
5020 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
5027 filter_mask
= ifsm
->filter_mask
;
5031 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
5035 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
5036 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
5037 0, filter_mask
, &idxattr
, &prividx
);
5039 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5040 WARN_ON(err
== -EMSGSIZE
);
5043 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
5049 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5051 struct netlink_ext_ack
*extack
= cb
->extack
;
5052 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
5053 struct net
*net
= sock_net(skb
->sk
);
5054 unsigned int flags
= NLM_F_MULTI
;
5055 struct if_stats_msg
*ifsm
;
5056 struct hlist_head
*head
;
5057 struct net_device
*dev
;
5058 u32 filter_mask
= 0;
5062 s_idx
= cb
->args
[1];
5063 s_idxattr
= cb
->args
[2];
5064 s_prividx
= cb
->args
[3];
5066 cb
->seq
= net
->dev_base_seq
;
5068 err
= rtnl_valid_stats_req(cb
->nlh
, cb
->strict_check
, true, extack
);
5072 ifsm
= nlmsg_data(cb
->nlh
);
5073 filter_mask
= ifsm
->filter_mask
;
5075 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats dump");
5079 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5081 head
= &net
->dev_index_head
[h
];
5082 hlist_for_each_entry(dev
, head
, index_hlist
) {
5085 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
5086 NETLINK_CB(cb
->skb
).portid
,
5087 cb
->nlh
->nlmsg_seq
, 0,
5089 &s_idxattr
, &s_prividx
);
5090 /* If we ran out of room on the first message,
5093 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
5099 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5105 cb
->args
[3] = s_prividx
;
5106 cb
->args
[2] = s_idxattr
;
5113 /* Process one rtnetlink message. */
5115 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5116 struct netlink_ext_ack
*extack
)
5118 struct net
*net
= sock_net(skb
->sk
);
5119 struct rtnl_link
*link
;
5120 struct module
*owner
;
5121 int err
= -EOPNOTSUPP
;
5122 rtnl_doit_func doit
;
5128 type
= nlh
->nlmsg_type
;
5134 /* All the messages must have at least 1 byte length */
5135 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
5138 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
5141 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
5145 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
5147 rtnl_dumpit_func dumpit
;
5148 u16 min_dump_alloc
= 0;
5150 link
= rtnl_get_link(family
, type
);
5151 if (!link
|| !link
->dumpit
) {
5153 link
= rtnl_get_link(family
, type
);
5154 if (!link
|| !link
->dumpit
)
5157 owner
= link
->owner
;
5158 dumpit
= link
->dumpit
;
5160 if (type
== RTM_GETLINK
- RTM_BASE
)
5161 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
5164 /* need to do this before rcu_read_unlock() */
5165 if (!try_module_get(owner
))
5166 err
= -EPROTONOSUPPORT
;
5172 struct netlink_dump_control c
= {
5174 .min_dump_alloc
= min_dump_alloc
,
5177 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
5178 /* netlink_dump_start() will keep a reference on
5179 * module if dump is still in progress.
5186 link
= rtnl_get_link(family
, type
);
5187 if (!link
|| !link
->doit
) {
5189 link
= rtnl_get_link(PF_UNSPEC
, type
);
5190 if (!link
|| !link
->doit
)
5194 owner
= link
->owner
;
5195 if (!try_module_get(owner
)) {
5196 err
= -EPROTONOSUPPORT
;
5200 flags
= link
->flags
;
5201 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
5205 err
= doit(skb
, nlh
, extack
);
5212 link
= rtnl_get_link(family
, type
);
5213 if (link
&& link
->doit
)
5214 err
= link
->doit(skb
, nlh
, extack
);
5230 static void rtnetlink_rcv(struct sk_buff
*skb
)
5232 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
5235 static int rtnetlink_bind(struct net
*net
, int group
)
5238 case RTNLGRP_IPV4_MROUTE_R
:
5239 case RTNLGRP_IPV6_MROUTE_R
:
5240 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
5247 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
5249 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
5253 case NETDEV_CHANGEMTU
:
5254 case NETDEV_CHANGEADDR
:
5255 case NETDEV_CHANGENAME
:
5256 case NETDEV_FEAT_CHANGE
:
5257 case NETDEV_BONDING_FAILOVER
:
5258 case NETDEV_POST_TYPE_CHANGE
:
5259 case NETDEV_NOTIFY_PEERS
:
5260 case NETDEV_CHANGEUPPER
:
5261 case NETDEV_RESEND_IGMP
:
5262 case NETDEV_CHANGEINFODATA
:
5263 case NETDEV_CHANGELOWERSTATE
:
5264 case NETDEV_CHANGE_TX_QUEUE_LEN
:
5265 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
5266 GFP_KERNEL
, NULL
, 0);
5274 static struct notifier_block rtnetlink_dev_notifier
= {
5275 .notifier_call
= rtnetlink_event
,
5279 static int __net_init
rtnetlink_net_init(struct net
*net
)
5282 struct netlink_kernel_cfg cfg
= {
5283 .groups
= RTNLGRP_MAX
,
5284 .input
= rtnetlink_rcv
,
5285 .cb_mutex
= &rtnl_mutex
,
5286 .flags
= NL_CFG_F_NONROOT_RECV
,
5287 .bind
= rtnetlink_bind
,
5290 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
5297 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
5299 netlink_kernel_release(net
->rtnl
);
5303 static struct pernet_operations rtnetlink_net_ops
= {
5304 .init
= rtnetlink_net_init
,
5305 .exit
= rtnetlink_net_exit
,
5308 void __init
rtnetlink_init(void)
5310 if (register_pernet_subsys(&rtnetlink_net_ops
))
5311 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5313 register_netdevice_notifier(&rtnetlink_dev_notifier
);
5315 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
5316 rtnl_dump_ifinfo
, 0);
5317 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
5318 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
5319 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
5321 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
5322 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
5323 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
5325 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
5326 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
5327 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, rtnl_fdb_get
, rtnl_fdb_dump
, 0);
5329 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
5330 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
5331 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
5333 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,