2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
45 #include <linux/uaccess.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
51 #include <net/protocol.h>
53 #include <net/route.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
64 rtnl_dumpit_func dumpit
;
68 static DEFINE_MUTEX(rtnl_mutex
);
72 mutex_lock(&rtnl_mutex
);
74 EXPORT_SYMBOL(rtnl_lock
);
76 static struct sk_buff
*defer_kfree_skb_list
;
77 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
80 tail
->next
= defer_kfree_skb_list
;
81 defer_kfree_skb_list
= head
;
84 EXPORT_SYMBOL(rtnl_kfree_skbs
);
86 void __rtnl_unlock(void)
88 struct sk_buff
*head
= defer_kfree_skb_list
;
90 defer_kfree_skb_list
= NULL
;
92 mutex_unlock(&rtnl_mutex
);
95 struct sk_buff
*next
= head
->next
;
103 void rtnl_unlock(void)
105 /* This fellow will unlock it for us. */
108 EXPORT_SYMBOL(rtnl_unlock
);
110 int rtnl_trylock(void)
112 return mutex_trylock(&rtnl_mutex
);
114 EXPORT_SYMBOL(rtnl_trylock
);
116 int rtnl_is_locked(void)
118 return mutex_is_locked(&rtnl_mutex
);
120 EXPORT_SYMBOL(rtnl_is_locked
);
122 #ifdef CONFIG_PROVE_LOCKING
123 bool lockdep_rtnl_is_held(void)
125 return lockdep_is_held(&rtnl_mutex
);
127 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
128 #endif /* #ifdef CONFIG_PROVE_LOCKING */
130 static struct rtnl_link __rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
131 static refcount_t rtnl_msg_handlers_ref
[RTNL_FAMILY_MAX
+ 1];
133 static inline int rtm_msgindex(int msgtype
)
135 int msgindex
= msgtype
- RTM_BASE
;
138 * msgindex < 0 implies someone tried to register a netlink
139 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
140 * the message type has not been added to linux/rtnetlink.h
142 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
148 * __rtnl_register - Register a rtnetlink message type
149 * @protocol: Protocol family or PF_UNSPEC
150 * @msgtype: rtnetlink message type
151 * @doit: Function pointer called for each request message
152 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
153 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
155 * Registers the specified function pointers (at least one of them has
156 * to be non-NULL) to be called whenever a request message for the
157 * specified protocol family and message type is received.
159 * The special protocol family PF_UNSPEC may be used to define fallback
160 * function pointers for the case when no entry for the specific protocol
163 * Returns 0 on success or a negative error code.
165 int __rtnl_register(int protocol
, int msgtype
,
166 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
169 struct rtnl_link
*tab
;
172 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
173 msgindex
= rtm_msgindex(msgtype
);
175 tab
= rcu_dereference_raw(rtnl_msg_handlers
[protocol
]);
177 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
181 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
185 tab
[msgindex
].doit
= doit
;
187 tab
[msgindex
].dumpit
= dumpit
;
188 tab
[msgindex
].flags
|= flags
;
192 EXPORT_SYMBOL_GPL(__rtnl_register
);
195 * rtnl_register - Register a rtnetlink message type
197 * Identical to __rtnl_register() but panics on failure. This is useful
198 * as failure of this function is very unlikely, it can only happen due
199 * to lack of memory when allocating the chain to store all message
200 * handlers for a protocol. Meant for use in init functions where lack
201 * of memory implies no sense in continuing.
203 void rtnl_register(int protocol
, int msgtype
,
204 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
207 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
, flags
) < 0)
208 panic("Unable to register rtnetlink message handler, "
209 "protocol = %d, message type = %d\n",
212 EXPORT_SYMBOL_GPL(rtnl_register
);
215 * rtnl_unregister - Unregister a rtnetlink message type
216 * @protocol: Protocol family or PF_UNSPEC
217 * @msgtype: rtnetlink message type
219 * Returns 0 on success or a negative error code.
221 int rtnl_unregister(int protocol
, int msgtype
)
223 struct rtnl_link
*handlers
;
226 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
227 msgindex
= rtm_msgindex(msgtype
);
230 handlers
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
236 handlers
[msgindex
].doit
= NULL
;
237 handlers
[msgindex
].dumpit
= NULL
;
238 handlers
[msgindex
].flags
= 0;
243 EXPORT_SYMBOL_GPL(rtnl_unregister
);
246 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
247 * @protocol : Protocol family or PF_UNSPEC
249 * Identical to calling rtnl_unregster() for all registered message types
250 * of a certain protocol family.
252 void rtnl_unregister_all(int protocol
)
254 struct rtnl_link
*handlers
;
256 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
259 handlers
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
260 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
265 while (refcount_read(&rtnl_msg_handlers_ref
[protocol
]) > 1)
269 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
271 static LIST_HEAD(link_ops
);
273 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
275 const struct rtnl_link_ops
*ops
;
277 list_for_each_entry(ops
, &link_ops
, list
) {
278 if (!strcmp(ops
->kind
, kind
))
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
292 * Returns 0 on success or a negative error code.
294 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
296 if (rtnl_link_ops_get(ops
->kind
))
299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
304 if (ops
->setup
&& !ops
->dellink
)
305 ops
->dellink
= unregister_netdevice_queue
;
307 list_add_tail(&ops
->list
, &link_ops
);
310 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
316 * Returns 0 on success or a negative error code.
318 int rtnl_link_register(struct rtnl_link_ops
*ops
)
323 err
= __rtnl_link_register(ops
);
327 EXPORT_SYMBOL_GPL(rtnl_link_register
);
329 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
331 struct net_device
*dev
;
332 LIST_HEAD(list_kill
);
334 for_each_netdev(net
, dev
) {
335 if (dev
->rtnl_link_ops
== ops
)
336 ops
->dellink(dev
, &list_kill
);
338 unregister_netdevice_many(&list_kill
);
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
345 * The caller must hold the rtnl_mutex.
347 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
352 __rtnl_kill_links(net
, ops
);
354 list_del(&ops
->list
);
356 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
358 /* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
361 static void rtnl_lock_unregistering_all(void)
365 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
367 add_wait_queue(&netdev_unregistering_wq
, &wait
);
369 unregistering
= false;
372 if (net
->dev_unreg_count
> 0) {
373 unregistering
= true;
381 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
383 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
390 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex
);
394 rtnl_lock_unregistering_all();
395 __rtnl_link_unregister(ops
);
397 mutex_unlock(&net_mutex
);
399 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
401 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
403 struct net_device
*master_dev
;
404 const struct rtnl_link_ops
*ops
;
409 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
413 ops
= master_dev
->rtnl_link_ops
;
414 if (!ops
|| !ops
->get_slave_size
)
416 /* IFLA_INFO_SLAVE_DATA + nested data */
417 size
= nla_total_size(sizeof(struct nlattr
)) +
418 ops
->get_slave_size(master_dev
, dev
);
425 static size_t rtnl_link_get_size(const struct net_device
*dev
)
427 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
433 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
437 /* IFLA_INFO_DATA + nested data */
438 size
+= nla_total_size(sizeof(struct nlattr
)) +
441 if (ops
->get_xstats_size
)
442 /* IFLA_INFO_XSTATS */
443 size
+= nla_total_size(ops
->get_xstats_size(dev
));
445 size
+= rtnl_link_get_slave_info_data_size(dev
);
450 static LIST_HEAD(rtnl_af_ops
);
452 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
454 const struct rtnl_af_ops
*ops
;
456 list_for_each_entry(ops
, &rtnl_af_ops
, list
) {
457 if (ops
->family
== family
)
465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
468 * Returns 0 on success or a negative error code.
470 void rtnl_af_register(struct rtnl_af_ops
*ops
)
473 list_add_tail(&ops
->list
, &rtnl_af_ops
);
476 EXPORT_SYMBOL_GPL(rtnl_af_register
);
479 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
482 * The caller must hold the rtnl_mutex.
484 void __rtnl_af_unregister(struct rtnl_af_ops
*ops
)
486 list_del(&ops
->list
);
488 EXPORT_SYMBOL_GPL(__rtnl_af_unregister
);
491 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492 * @ops: struct rtnl_af_ops * to unregister
494 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
497 __rtnl_af_unregister(ops
);
500 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
502 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
505 struct rtnl_af_ops
*af_ops
;
509 size
= nla_total_size(sizeof(struct nlattr
));
511 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
512 if (af_ops
->get_link_af_size
) {
513 /* AF_* + nested data */
514 size
+= nla_total_size(sizeof(struct nlattr
)) +
515 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
522 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
524 struct net_device
*master_dev
;
526 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
527 if (master_dev
&& master_dev
->rtnl_link_ops
)
532 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
533 const struct net_device
*dev
)
535 struct net_device
*master_dev
;
536 const struct rtnl_link_ops
*ops
;
537 struct nlattr
*slave_data
;
540 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
543 ops
= master_dev
->rtnl_link_ops
;
546 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
548 if (ops
->fill_slave_info
) {
549 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
552 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
554 goto err_cancel_slave_data
;
555 nla_nest_end(skb
, slave_data
);
559 err_cancel_slave_data
:
560 nla_nest_cancel(skb
, slave_data
);
564 static int rtnl_link_info_fill(struct sk_buff
*skb
,
565 const struct net_device
*dev
)
567 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
573 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
575 if (ops
->fill_xstats
) {
576 err
= ops
->fill_xstats(skb
, dev
);
580 if (ops
->fill_info
) {
581 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
584 err
= ops
->fill_info(skb
, dev
);
586 goto err_cancel_data
;
587 nla_nest_end(skb
, data
);
592 nla_nest_cancel(skb
, data
);
596 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
598 struct nlattr
*linkinfo
;
601 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
602 if (linkinfo
== NULL
)
605 err
= rtnl_link_info_fill(skb
, dev
);
607 goto err_cancel_link
;
609 err
= rtnl_link_slave_info_fill(skb
, dev
);
611 goto err_cancel_link
;
613 nla_nest_end(skb
, linkinfo
);
617 nla_nest_cancel(skb
, linkinfo
);
622 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
624 struct sock
*rtnl
= net
->rtnl
;
627 NETLINK_CB(skb
).dst_group
= group
;
629 refcount_inc(&skb
->users
);
630 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
632 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
636 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
638 struct sock
*rtnl
= net
->rtnl
;
640 return nlmsg_unicast(rtnl
, skb
, pid
);
642 EXPORT_SYMBOL(rtnl_unicast
);
644 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
645 struct nlmsghdr
*nlh
, gfp_t flags
)
647 struct sock
*rtnl
= net
->rtnl
;
651 report
= nlmsg_report(nlh
);
653 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
655 EXPORT_SYMBOL(rtnl_notify
);
657 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
659 struct sock
*rtnl
= net
->rtnl
;
661 netlink_set_err(rtnl
, 0, group
, error
);
663 EXPORT_SYMBOL(rtnl_set_sk_err
);
665 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
670 mx
= nla_nest_start(skb
, RTA_METRICS
);
674 for (i
= 0; i
< RTAX_MAX
; i
++) {
676 if (i
== RTAX_CC_ALGO
- 1) {
677 char tmp
[TCP_CA_NAME_MAX
], *name
;
679 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
682 if (nla_put_string(skb
, i
+ 1, name
))
683 goto nla_put_failure
;
684 } else if (i
== RTAX_FEATURES
- 1) {
685 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
689 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
690 if (nla_put_u32(skb
, i
+ 1, user_features
))
691 goto nla_put_failure
;
693 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
694 goto nla_put_failure
;
701 nla_nest_cancel(skb
, mx
);
705 return nla_nest_end(skb
, mx
);
708 nla_nest_cancel(skb
, mx
);
711 EXPORT_SYMBOL(rtnetlink_put_metrics
);
713 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
714 long expires
, u32 error
)
716 struct rta_cacheinfo ci
= {
717 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
718 .rta_used
= dst
->__use
,
719 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
727 clock
= jiffies_to_clock_t(abs(expires
));
728 clock
= min_t(unsigned long, clock
, INT_MAX
);
729 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
731 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
733 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
735 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
737 unsigned char operstate
= dev
->operstate
;
739 switch (transition
) {
741 if ((operstate
== IF_OPER_DORMANT
||
742 operstate
== IF_OPER_UNKNOWN
) &&
744 operstate
= IF_OPER_UP
;
747 case IF_OPER_DORMANT
:
748 if (operstate
== IF_OPER_UP
||
749 operstate
== IF_OPER_UNKNOWN
)
750 operstate
= IF_OPER_DORMANT
;
754 if (dev
->operstate
!= operstate
) {
755 write_lock_bh(&dev_base_lock
);
756 dev
->operstate
= operstate
;
757 write_unlock_bh(&dev_base_lock
);
758 netdev_state_change(dev
);
762 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
764 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
765 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
768 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
769 const struct ifinfomsg
*ifm
)
771 unsigned int flags
= ifm
->ifi_flags
;
773 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
775 flags
= (flags
& ifm
->ifi_change
) |
776 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
781 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
782 const struct rtnl_link_stats64
*b
)
784 a
->rx_packets
= b
->rx_packets
;
785 a
->tx_packets
= b
->tx_packets
;
786 a
->rx_bytes
= b
->rx_bytes
;
787 a
->tx_bytes
= b
->tx_bytes
;
788 a
->rx_errors
= b
->rx_errors
;
789 a
->tx_errors
= b
->tx_errors
;
790 a
->rx_dropped
= b
->rx_dropped
;
791 a
->tx_dropped
= b
->tx_dropped
;
793 a
->multicast
= b
->multicast
;
794 a
->collisions
= b
->collisions
;
796 a
->rx_length_errors
= b
->rx_length_errors
;
797 a
->rx_over_errors
= b
->rx_over_errors
;
798 a
->rx_crc_errors
= b
->rx_crc_errors
;
799 a
->rx_frame_errors
= b
->rx_frame_errors
;
800 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
801 a
->rx_missed_errors
= b
->rx_missed_errors
;
803 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
804 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
805 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
806 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
807 a
->tx_window_errors
= b
->tx_window_errors
;
809 a
->rx_compressed
= b
->rx_compressed
;
810 a
->tx_compressed
= b
->tx_compressed
;
812 a
->rx_nohandler
= b
->rx_nohandler
;
816 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
819 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
820 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
821 size_t size
= nla_total_size(0);
824 nla_total_size(sizeof(struct ifla_vf_mac
)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
826 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
827 nla_total_size(MAX_VLAN_LIST_LEN
*
828 sizeof(struct ifla_vf_vlan_info
)) +
829 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
830 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
831 nla_total_size(sizeof(struct ifla_vf_rate
)) +
832 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
833 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
834 nla_total_size(0) + /* nest IFLA_VF_STATS */
835 /* IFLA_VF_STATS_RX_PACKETS */
836 nla_total_size_64bit(sizeof(__u64
)) +
837 /* IFLA_VF_STATS_TX_PACKETS */
838 nla_total_size_64bit(sizeof(__u64
)) +
839 /* IFLA_VF_STATS_RX_BYTES */
840 nla_total_size_64bit(sizeof(__u64
)) +
841 /* IFLA_VF_STATS_TX_BYTES */
842 nla_total_size_64bit(sizeof(__u64
)) +
843 /* IFLA_VF_STATS_BROADCAST */
844 nla_total_size_64bit(sizeof(__u64
)) +
845 /* IFLA_VF_STATS_MULTICAST */
846 nla_total_size_64bit(sizeof(__u64
)) +
847 nla_total_size(sizeof(struct ifla_vf_trust
)));
853 static size_t rtnl_port_size(const struct net_device
*dev
,
856 size_t port_size
= nla_total_size(4) /* PORT_VF */
857 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
858 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
859 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
860 + nla_total_size(1) /* PROT_VDP_REQUEST */
861 + nla_total_size(2); /* PORT_VDP_RESPONSE */
862 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
863 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
865 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
868 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
869 !(ext_filter_mask
& RTEXT_FILTER_VF
))
871 if (dev_num_vf(dev
->dev
.parent
))
872 return port_self_size
+ vf_ports_size
+
873 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
875 return port_self_size
;
878 static size_t rtnl_xdp_size(void)
880 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
881 nla_total_size(1) + /* XDP_ATTACHED */
882 nla_total_size(4); /* XDP_PROG_ID */
887 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
890 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
891 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
892 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
893 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
894 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
895 + nla_total_size(sizeof(struct rtnl_link_stats
))
896 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
897 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
898 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
899 + nla_total_size(4) /* IFLA_TXQLEN */
900 + nla_total_size(4) /* IFLA_WEIGHT */
901 + nla_total_size(4) /* IFLA_MTU */
902 + nla_total_size(4) /* IFLA_LINK */
903 + nla_total_size(4) /* IFLA_MASTER */
904 + nla_total_size(1) /* IFLA_CARRIER */
905 + nla_total_size(4) /* IFLA_PROMISCUITY */
906 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
907 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
908 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
909 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
910 + nla_total_size(1) /* IFLA_OPERSTATE */
911 + nla_total_size(1) /* IFLA_LINKMODE */
912 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
913 + nla_total_size(4) /* IFLA_LINK_NETNSID */
914 + nla_total_size(4) /* IFLA_GROUP */
915 + nla_total_size(ext_filter_mask
916 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
917 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
918 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
919 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
920 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
921 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
922 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
923 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
924 + rtnl_xdp_size() /* IFLA_XDP */
925 + nla_total_size(4) /* IFLA_EVENT */
926 + nla_total_size(1); /* IFLA_PROTO_DOWN */
930 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
932 struct nlattr
*vf_ports
;
933 struct nlattr
*vf_port
;
937 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
941 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
942 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
944 goto nla_put_failure
;
945 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
946 goto nla_put_failure
;
947 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
948 if (err
== -EMSGSIZE
)
949 goto nla_put_failure
;
951 nla_nest_cancel(skb
, vf_port
);
954 nla_nest_end(skb
, vf_port
);
957 nla_nest_end(skb
, vf_ports
);
962 nla_nest_cancel(skb
, vf_ports
);
966 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
968 struct nlattr
*port_self
;
971 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
975 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
977 nla_nest_cancel(skb
, port_self
);
978 return (err
== -EMSGSIZE
) ? err
: 0;
981 nla_nest_end(skb
, port_self
);
986 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
991 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
992 !(ext_filter_mask
& RTEXT_FILTER_VF
))
995 err
= rtnl_port_self_fill(skb
, dev
);
999 if (dev_num_vf(dev
->dev
.parent
)) {
1000 err
= rtnl_vf_ports_fill(skb
, dev
);
1008 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1011 struct netdev_phys_item_id ppid
;
1013 err
= dev_get_phys_port_id(dev
, &ppid
);
1015 if (err
== -EOPNOTSUPP
)
1020 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1026 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1028 char name
[IFNAMSIZ
];
1031 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1033 if (err
== -EOPNOTSUPP
)
1038 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1044 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1047 struct switchdev_attr attr
= {
1049 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1050 .flags
= SWITCHDEV_F_NO_RECURSE
,
1053 err
= switchdev_port_attr_get(dev
, &attr
);
1055 if (err
== -EOPNOTSUPP
)
1060 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1067 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1068 struct net_device
*dev
)
1070 struct rtnl_link_stats64
*sp
;
1071 struct nlattr
*attr
;
1073 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1074 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1078 sp
= nla_data(attr
);
1079 dev_get_stats(dev
, sp
);
1081 attr
= nla_reserve(skb
, IFLA_STATS
,
1082 sizeof(struct rtnl_link_stats
));
1086 copy_rtnl_link_stats(nla_data(attr
), sp
);
1091 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1092 struct net_device
*dev
,
1094 struct nlattr
*vfinfo
)
1096 struct ifla_vf_rss_query_en vf_rss_query_en
;
1097 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1098 struct ifla_vf_link_state vf_linkstate
;
1099 struct ifla_vf_vlan_info vf_vlan_info
;
1100 struct ifla_vf_spoofchk vf_spoofchk
;
1101 struct ifla_vf_tx_rate vf_tx_rate
;
1102 struct ifla_vf_stats vf_stats
;
1103 struct ifla_vf_trust vf_trust
;
1104 struct ifla_vf_vlan vf_vlan
;
1105 struct ifla_vf_rate vf_rate
;
1106 struct ifla_vf_mac vf_mac
;
1107 struct ifla_vf_info ivi
;
1109 memset(&ivi
, 0, sizeof(ivi
));
1111 /* Not all SR-IOV capable drivers support the
1112 * spoofcheck and "RSS query enable" query. Preset to
1113 * -1 so the user space tool can detect that the driver
1114 * didn't report anything.
1117 ivi
.rss_query_en
= -1;
1119 /* The default value for VF link state is "auto"
1120 * IFLA_VF_LINK_STATE_AUTO which equals zero
1123 /* VLAN Protocol by default is 802.1Q */
1124 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1125 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1128 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1137 vf_rss_query_en
.vf
=
1138 vf_trust
.vf
= ivi
.vf
;
1140 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1141 vf_vlan
.vlan
= ivi
.vlan
;
1142 vf_vlan
.qos
= ivi
.qos
;
1143 vf_vlan_info
.vlan
= ivi
.vlan
;
1144 vf_vlan_info
.qos
= ivi
.qos
;
1145 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1146 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1147 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1148 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1149 vf_spoofchk
.setting
= ivi
.spoofchk
;
1150 vf_linkstate
.link_state
= ivi
.linkstate
;
1151 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1152 vf_trust
.setting
= ivi
.trusted
;
1153 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1155 goto nla_put_vfinfo_failure
;
1156 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1157 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1158 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1160 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1162 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1164 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1166 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1167 sizeof(vf_rss_query_en
),
1168 &vf_rss_query_en
) ||
1169 nla_put(skb
, IFLA_VF_TRUST
,
1170 sizeof(vf_trust
), &vf_trust
))
1171 goto nla_put_vf_failure
;
1172 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1174 goto nla_put_vf_failure
;
1175 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1177 nla_nest_cancel(skb
, vfvlanlist
);
1178 goto nla_put_vf_failure
;
1180 nla_nest_end(skb
, vfvlanlist
);
1181 memset(&vf_stats
, 0, sizeof(vf_stats
));
1182 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1183 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1185 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1187 goto nla_put_vf_failure
;
1188 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1189 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1190 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1191 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1192 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1193 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1194 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1195 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1196 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1197 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1198 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1199 vf_stats
.multicast
, IFLA_VF_STATS_PAD
)) {
1200 nla_nest_cancel(skb
, vfstats
);
1201 goto nla_put_vf_failure
;
1203 nla_nest_end(skb
, vfstats
);
1204 nla_nest_end(skb
, vf
);
1208 nla_nest_cancel(skb
, vf
);
1209 nla_put_vfinfo_failure
:
1210 nla_nest_cancel(skb
, vfinfo
);
1214 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1216 struct rtnl_link_ifmap map
;
1218 memset(&map
, 0, sizeof(map
));
1219 map
.mem_start
= dev
->mem_start
;
1220 map
.mem_end
= dev
->mem_end
;
1221 map
.base_addr
= dev
->base_addr
;
1224 map
.port
= dev
->if_port
;
1226 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1232 static u8
rtnl_xdp_attached_mode(struct net_device
*dev
, u32
*prog_id
)
1234 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1235 const struct bpf_prog
*generic_xdp_prog
;
1240 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1241 if (generic_xdp_prog
) {
1242 *prog_id
= generic_xdp_prog
->aux
->id
;
1243 return XDP_ATTACHED_SKB
;
1246 return XDP_ATTACHED_NONE
;
1248 return __dev_xdp_attached(dev
, ops
->ndo_xdp
, prog_id
);
1251 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1257 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1261 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
,
1262 rtnl_xdp_attached_mode(dev
, &prog_id
));
1267 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1272 nla_nest_end(skb
, xdp
);
1276 nla_nest_cancel(skb
, xdp
);
1280 static u32
rtnl_get_event(unsigned long event
)
1282 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1286 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1288 case NETDEV_FEAT_CHANGE
:
1289 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1291 case NETDEV_BONDING_FAILOVER
:
1292 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1294 case NETDEV_NOTIFY_PEERS
:
1295 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1297 case NETDEV_RESEND_IGMP
:
1298 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1300 case NETDEV_CHANGEINFODATA
:
1301 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1307 return rtnl_event_type
;
1310 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
1311 int type
, u32 pid
, u32 seq
, u32 change
,
1312 unsigned int flags
, u32 ext_filter_mask
,
1315 struct ifinfomsg
*ifm
;
1316 struct nlmsghdr
*nlh
;
1317 struct nlattr
*af_spec
;
1318 struct rtnl_af_ops
*af_ops
;
1319 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1322 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1326 ifm
= nlmsg_data(nlh
);
1327 ifm
->ifi_family
= AF_UNSPEC
;
1329 ifm
->ifi_type
= dev
->type
;
1330 ifm
->ifi_index
= dev
->ifindex
;
1331 ifm
->ifi_flags
= dev_get_flags(dev
);
1332 ifm
->ifi_change
= change
;
1334 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1335 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1336 nla_put_u8(skb
, IFLA_OPERSTATE
,
1337 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1338 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1339 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1340 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1341 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1342 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1343 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1344 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1346 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1348 (dev
->ifindex
!= dev_get_iflink(dev
) &&
1349 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
1351 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1352 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1354 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1356 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)) ||
1357 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1358 atomic_read(&dev
->carrier_changes
)) ||
1359 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
))
1360 goto nla_put_failure
;
1362 if (event
!= IFLA_EVENT_NONE
) {
1363 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1364 goto nla_put_failure
;
1367 if (rtnl_fill_link_ifmap(skb
, dev
))
1368 goto nla_put_failure
;
1370 if (dev
->addr_len
) {
1371 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1372 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1373 goto nla_put_failure
;
1376 if (rtnl_phys_port_id_fill(skb
, dev
))
1377 goto nla_put_failure
;
1379 if (rtnl_phys_port_name_fill(skb
, dev
))
1380 goto nla_put_failure
;
1382 if (rtnl_phys_switch_id_fill(skb
, dev
))
1383 goto nla_put_failure
;
1385 if (rtnl_fill_stats(skb
, dev
))
1386 goto nla_put_failure
;
1388 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
1389 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
1390 goto nla_put_failure
;
1392 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
&&
1393 ext_filter_mask
& RTEXT_FILTER_VF
) {
1395 struct nlattr
*vfinfo
;
1396 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1398 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1400 goto nla_put_failure
;
1401 for (i
= 0; i
< num_vfs
; i
++) {
1402 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1403 goto nla_put_failure
;
1406 nla_nest_end(skb
, vfinfo
);
1409 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1410 goto nla_put_failure
;
1412 if (rtnl_xdp_fill(skb
, dev
))
1413 goto nla_put_failure
;
1415 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1416 if (rtnl_link_fill(skb
, dev
) < 0)
1417 goto nla_put_failure
;
1420 if (dev
->rtnl_link_ops
&&
1421 dev
->rtnl_link_ops
->get_link_net
) {
1422 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1424 if (!net_eq(dev_net(dev
), link_net
)) {
1425 int id
= peernet2id_alloc(dev_net(dev
), link_net
);
1427 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1428 goto nla_put_failure
;
1432 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1433 goto nla_put_failure
;
1435 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1436 if (af_ops
->fill_link_af
) {
1440 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1441 goto nla_put_failure
;
1443 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1446 * Caller may return ENODATA to indicate that there
1447 * was no data to be dumped. This is not an error, it
1448 * means we should trim the attribute header and
1451 if (err
== -ENODATA
)
1452 nla_nest_cancel(skb
, af
);
1454 goto nla_put_failure
;
1456 nla_nest_end(skb
, af
);
1460 nla_nest_end(skb
, af_spec
);
1462 nlmsg_end(skb
, nlh
);
1466 nlmsg_cancel(skb
, nlh
);
1470 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1471 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1472 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1473 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1474 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1475 [IFLA_MTU
] = { .type
= NLA_U32
},
1476 [IFLA_LINK
] = { .type
= NLA_U32
},
1477 [IFLA_MASTER
] = { .type
= NLA_U32
},
1478 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1479 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1480 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1481 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1482 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1483 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1484 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1485 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1486 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1487 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1488 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1489 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1490 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1491 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1492 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1493 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1494 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1495 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1496 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1497 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1498 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1499 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1500 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1501 [IFLA_EVENT
] = { .type
= NLA_U32
},
1502 [IFLA_GROUP
] = { .type
= NLA_U32
},
1505 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1506 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1507 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1508 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1509 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1512 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1513 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1514 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1515 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1516 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1517 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1518 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1519 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1520 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1521 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1522 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1523 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1524 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1527 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1528 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1529 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1530 .len
= PORT_PROFILE_MAX
},
1531 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1532 .len
= PORT_UUID_MAX
},
1533 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1534 .len
= PORT_UUID_MAX
},
1535 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1536 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1538 /* Unused, but we need to keep it here since user space could
1539 * fill it. It's also broken with regard to NLA_BINARY use in
1540 * combination with structs.
1542 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1543 .len
= sizeof(struct ifla_port_vsi
) },
1546 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1547 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1548 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1549 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1550 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1553 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1555 const struct rtnl_link_ops
*ops
= NULL
;
1556 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1558 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1559 ifla_info_policy
, NULL
) < 0)
1562 if (linfo
[IFLA_INFO_KIND
]) {
1563 char kind
[MODULE_NAME_LEN
];
1565 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1566 ops
= rtnl_link_ops_get(kind
);
1572 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1574 struct net_device
*master
;
1579 master
= netdev_master_upper_dev_get(dev
);
1580 if (!master
|| master
->ifindex
!= master_idx
)
1586 static bool link_kind_filtered(const struct net_device
*dev
,
1587 const struct rtnl_link_ops
*kind_ops
)
1589 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1595 static bool link_dump_filtered(struct net_device
*dev
,
1597 const struct rtnl_link_ops
*kind_ops
)
1599 if (link_master_filtered(dev
, master_idx
) ||
1600 link_kind_filtered(dev
, kind_ops
))
1606 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1608 struct net
*net
= sock_net(skb
->sk
);
1611 struct net_device
*dev
;
1612 struct hlist_head
*head
;
1613 struct nlattr
*tb
[IFLA_MAX
+1];
1614 u32 ext_filter_mask
= 0;
1615 const struct rtnl_link_ops
*kind_ops
= NULL
;
1616 unsigned int flags
= NLM_F_MULTI
;
1622 s_idx
= cb
->args
[1];
1624 /* A hack to preserve kernel<->userspace interface.
1625 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1626 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1627 * what iproute2 < v3.9.0 used.
1628 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1629 * attribute, its netlink message is shorter than struct ifinfomsg.
1631 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1632 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1634 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
,
1635 ifla_policy
, NULL
) >= 0) {
1636 if (tb
[IFLA_EXT_MASK
])
1637 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1639 if (tb
[IFLA_MASTER
])
1640 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1642 if (tb
[IFLA_LINKINFO
])
1643 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1645 if (master_idx
|| kind_ops
)
1646 flags
|= NLM_F_DUMP_FILTERED
;
1649 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1651 head
= &net
->dev_index_head
[h
];
1652 hlist_for_each_entry(dev
, head
, index_hlist
) {
1653 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1657 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1658 NETLINK_CB(cb
->skb
).portid
,
1659 cb
->nlh
->nlmsg_seq
, 0,
1661 ext_filter_mask
, 0);
1664 if (likely(skb
->len
))
1678 cb
->seq
= net
->dev_base_seq
;
1679 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1684 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
1685 struct netlink_ext_ack
*exterr
)
1687 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
1689 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1691 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1694 /* Examine the link attributes and figure out which
1695 * network namespace we are talking about.
1697 if (tb
[IFLA_NET_NS_PID
])
1698 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1699 else if (tb
[IFLA_NET_NS_FD
])
1700 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1702 net
= get_net(src_net
);
1705 EXPORT_SYMBOL(rtnl_link_get_net
);
1707 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1710 if (tb
[IFLA_ADDRESS
] &&
1711 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1714 if (tb
[IFLA_BROADCAST
] &&
1715 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1719 if (tb
[IFLA_AF_SPEC
]) {
1723 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1724 const struct rtnl_af_ops
*af_ops
;
1726 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1727 return -EAFNOSUPPORT
;
1729 if (!af_ops
->set_link_af
)
1732 if (af_ops
->validate_link_af
) {
1733 err
= af_ops
->validate_link_af(dev
, af
);
1743 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
1746 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1748 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
1751 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
1753 if (dev
->type
!= ARPHRD_INFINIBAND
)
1756 return handle_infiniband_guid(dev
, ivt
, guid_type
);
1759 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1761 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1764 if (tb
[IFLA_VF_MAC
]) {
1765 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1768 if (ops
->ndo_set_vf_mac
)
1769 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1775 if (tb
[IFLA_VF_VLAN
]) {
1776 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1779 if (ops
->ndo_set_vf_vlan
)
1780 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
1782 htons(ETH_P_8021Q
));
1787 if (tb
[IFLA_VF_VLAN_LIST
]) {
1788 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
1789 struct nlattr
*attr
;
1793 if (!ops
->ndo_set_vf_vlan
)
1796 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
1797 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
1798 nla_len(attr
) < NLA_HDRLEN
) {
1801 if (len
>= MAX_VLAN_LIST_LEN
)
1803 ivvl
[len
] = nla_data(attr
);
1810 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
1811 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
1816 if (tb
[IFLA_VF_TX_RATE
]) {
1817 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
1818 struct ifla_vf_info ivf
;
1821 if (ops
->ndo_get_vf_config
)
1822 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1827 if (ops
->ndo_set_vf_rate
)
1828 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1835 if (tb
[IFLA_VF_RATE
]) {
1836 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1839 if (ops
->ndo_set_vf_rate
)
1840 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1847 if (tb
[IFLA_VF_SPOOFCHK
]) {
1848 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1851 if (ops
->ndo_set_vf_spoofchk
)
1852 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1858 if (tb
[IFLA_VF_LINK_STATE
]) {
1859 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1862 if (ops
->ndo_set_vf_link_state
)
1863 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1869 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1870 struct ifla_vf_rss_query_en
*ivrssq_en
;
1873 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
1874 if (ops
->ndo_set_vf_rss_query_en
)
1875 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
1876 ivrssq_en
->setting
);
1881 if (tb
[IFLA_VF_TRUST
]) {
1882 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
1885 if (ops
->ndo_set_vf_trust
)
1886 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
1891 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
1892 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
1894 if (!ops
->ndo_set_vf_guid
)
1897 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
1900 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
1901 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
1903 if (!ops
->ndo_set_vf_guid
)
1906 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
1912 static int do_set_master(struct net_device
*dev
, int ifindex
)
1914 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1915 const struct net_device_ops
*ops
;
1919 if (upper_dev
->ifindex
== ifindex
)
1921 ops
= upper_dev
->netdev_ops
;
1922 if (ops
->ndo_del_slave
) {
1923 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1932 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1935 ops
= upper_dev
->netdev_ops
;
1936 if (ops
->ndo_add_slave
) {
1937 err
= ops
->ndo_add_slave(upper_dev
, dev
);
1947 #define DO_SETLINK_MODIFIED 0x01
1948 /* notify flag means notify + modified. */
1949 #define DO_SETLINK_NOTIFY 0x03
1950 static int do_setlink(const struct sk_buff
*skb
,
1951 struct net_device
*dev
, struct ifinfomsg
*ifm
,
1952 struct netlink_ext_ack
*extack
,
1953 struct nlattr
**tb
, char *ifname
, int status
)
1955 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1958 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1959 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1964 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1969 err
= dev_change_net_namespace(dev
, net
, ifname
);
1973 status
|= DO_SETLINK_MODIFIED
;
1977 struct rtnl_link_ifmap
*u_map
;
1980 if (!ops
->ndo_set_config
) {
1985 if (!netif_device_present(dev
)) {
1990 u_map
= nla_data(tb
[IFLA_MAP
]);
1991 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1992 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1993 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1994 k_map
.irq
= (unsigned char) u_map
->irq
;
1995 k_map
.dma
= (unsigned char) u_map
->dma
;
1996 k_map
.port
= (unsigned char) u_map
->port
;
1998 err
= ops
->ndo_set_config(dev
, &k_map
);
2002 status
|= DO_SETLINK_NOTIFY
;
2005 if (tb
[IFLA_ADDRESS
]) {
2006 struct sockaddr
*sa
;
2009 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2011 sa
= kmalloc(len
, GFP_KERNEL
);
2016 sa
->sa_family
= dev
->type
;
2017 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2019 err
= dev_set_mac_address(dev
, sa
);
2023 status
|= DO_SETLINK_MODIFIED
;
2027 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2030 status
|= DO_SETLINK_MODIFIED
;
2033 if (tb
[IFLA_GROUP
]) {
2034 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2035 status
|= DO_SETLINK_NOTIFY
;
2039 * Interface selected by interface index but interface
2040 * name provided implies that a name change has been
2043 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2044 err
= dev_change_name(dev
, ifname
);
2047 status
|= DO_SETLINK_MODIFIED
;
2050 if (tb
[IFLA_IFALIAS
]) {
2051 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2052 nla_len(tb
[IFLA_IFALIAS
]));
2055 status
|= DO_SETLINK_NOTIFY
;
2058 if (tb
[IFLA_BROADCAST
]) {
2059 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2060 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2063 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2064 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2069 if (tb
[IFLA_MASTER
]) {
2070 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
2073 status
|= DO_SETLINK_MODIFIED
;
2076 if (tb
[IFLA_CARRIER
]) {
2077 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2080 status
|= DO_SETLINK_MODIFIED
;
2083 if (tb
[IFLA_TXQLEN
]) {
2084 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2085 unsigned int orig_len
= dev
->tx_queue_len
;
2087 if (dev
->tx_queue_len
^ value
) {
2088 dev
->tx_queue_len
= value
;
2089 err
= call_netdevice_notifiers(
2090 NETDEV_CHANGE_TX_QUEUE_LEN
, dev
);
2091 err
= notifier_to_errno(err
);
2093 dev
->tx_queue_len
= orig_len
;
2096 status
|= DO_SETLINK_NOTIFY
;
2100 if (tb
[IFLA_OPERSTATE
])
2101 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2103 if (tb
[IFLA_LINKMODE
]) {
2104 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2106 write_lock_bh(&dev_base_lock
);
2107 if (dev
->link_mode
^ value
)
2108 status
|= DO_SETLINK_NOTIFY
;
2109 dev
->link_mode
= value
;
2110 write_unlock_bh(&dev_base_lock
);
2113 if (tb
[IFLA_VFINFO_LIST
]) {
2114 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2115 struct nlattr
*attr
;
2118 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2119 if (nla_type(attr
) != IFLA_VF_INFO
||
2120 nla_len(attr
) < NLA_HDRLEN
) {
2124 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2125 ifla_vf_policy
, NULL
);
2128 err
= do_setvfinfo(dev
, vfinfo
);
2131 status
|= DO_SETLINK_NOTIFY
;
2136 if (tb
[IFLA_VF_PORTS
]) {
2137 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2138 struct nlattr
*attr
;
2143 if (!ops
->ndo_set_vf_port
)
2146 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2147 if (nla_type(attr
) != IFLA_VF_PORT
||
2148 nla_len(attr
) < NLA_HDRLEN
) {
2152 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2153 ifla_port_policy
, NULL
);
2156 if (!port
[IFLA_PORT_VF
]) {
2160 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2161 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2164 status
|= DO_SETLINK_NOTIFY
;
2169 if (tb
[IFLA_PORT_SELF
]) {
2170 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2172 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2173 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2179 if (ops
->ndo_set_vf_port
)
2180 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2183 status
|= DO_SETLINK_NOTIFY
;
2186 if (tb
[IFLA_AF_SPEC
]) {
2190 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2191 const struct rtnl_af_ops
*af_ops
;
2193 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
2196 err
= af_ops
->set_link_af(dev
, af
);
2200 status
|= DO_SETLINK_NOTIFY
;
2205 if (tb
[IFLA_PROTO_DOWN
]) {
2206 err
= dev_change_proto_down(dev
,
2207 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2210 status
|= DO_SETLINK_NOTIFY
;
2214 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2217 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2218 ifla_xdp_policy
, NULL
);
2222 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2227 if (xdp
[IFLA_XDP_FLAGS
]) {
2228 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2229 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2233 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2239 if (xdp
[IFLA_XDP_FD
]) {
2240 err
= dev_change_xdp_fd(dev
, extack
,
2241 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2245 status
|= DO_SETLINK_NOTIFY
;
2250 if (status
& DO_SETLINK_MODIFIED
) {
2251 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2252 netdev_state_change(dev
);
2255 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",
2262 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2263 struct netlink_ext_ack
*extack
)
2265 struct net
*net
= sock_net(skb
->sk
);
2266 struct ifinfomsg
*ifm
;
2267 struct net_device
*dev
;
2269 struct nlattr
*tb
[IFLA_MAX
+1];
2270 char ifname
[IFNAMSIZ
];
2272 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2277 if (tb
[IFLA_IFNAME
])
2278 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2283 ifm
= nlmsg_data(nlh
);
2284 if (ifm
->ifi_index
> 0)
2285 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2286 else if (tb
[IFLA_IFNAME
])
2287 dev
= __dev_get_by_name(net
, ifname
);
2296 err
= validate_linkmsg(dev
, tb
);
2300 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2305 static int rtnl_group_dellink(const struct net
*net
, int group
)
2307 struct net_device
*dev
, *aux
;
2308 LIST_HEAD(list_kill
);
2314 for_each_netdev(net
, dev
) {
2315 if (dev
->group
== group
) {
2316 const struct rtnl_link_ops
*ops
;
2319 ops
= dev
->rtnl_link_ops
;
2320 if (!ops
|| !ops
->dellink
)
2328 for_each_netdev_safe(net
, dev
, aux
) {
2329 if (dev
->group
== group
) {
2330 const struct rtnl_link_ops
*ops
;
2332 ops
= dev
->rtnl_link_ops
;
2333 ops
->dellink(dev
, &list_kill
);
2336 unregister_netdevice_many(&list_kill
);
2341 int rtnl_delete_link(struct net_device
*dev
)
2343 const struct rtnl_link_ops
*ops
;
2344 LIST_HEAD(list_kill
);
2346 ops
= dev
->rtnl_link_ops
;
2347 if (!ops
|| !ops
->dellink
)
2350 ops
->dellink(dev
, &list_kill
);
2351 unregister_netdevice_many(&list_kill
);
2355 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2357 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2358 struct netlink_ext_ack
*extack
)
2360 struct net
*net
= sock_net(skb
->sk
);
2361 struct net_device
*dev
;
2362 struct ifinfomsg
*ifm
;
2363 char ifname
[IFNAMSIZ
];
2364 struct nlattr
*tb
[IFLA_MAX
+1];
2367 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2371 if (tb
[IFLA_IFNAME
])
2372 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2374 ifm
= nlmsg_data(nlh
);
2375 if (ifm
->ifi_index
> 0)
2376 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2377 else if (tb
[IFLA_IFNAME
])
2378 dev
= __dev_get_by_name(net
, ifname
);
2379 else if (tb
[IFLA_GROUP
])
2380 return rtnl_group_dellink(net
, nla_get_u32(tb
[IFLA_GROUP
]));
2387 return rtnl_delete_link(dev
);
2390 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2392 unsigned int old_flags
;
2395 old_flags
= dev
->flags
;
2396 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2397 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2402 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2404 __dev_notify_flags(dev
, old_flags
, ~0U);
2407 EXPORT_SYMBOL(rtnl_configure_link
);
2409 struct net_device
*rtnl_create_link(struct net
*net
,
2410 const char *ifname
, unsigned char name_assign_type
,
2411 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2413 struct net_device
*dev
;
2414 unsigned int num_tx_queues
= 1;
2415 unsigned int num_rx_queues
= 1;
2417 if (tb
[IFLA_NUM_TX_QUEUES
])
2418 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2419 else if (ops
->get_num_tx_queues
)
2420 num_tx_queues
= ops
->get_num_tx_queues();
2422 if (tb
[IFLA_NUM_RX_QUEUES
])
2423 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2424 else if (ops
->get_num_rx_queues
)
2425 num_rx_queues
= ops
->get_num_rx_queues();
2427 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2428 ops
->setup
, num_tx_queues
, num_rx_queues
);
2430 return ERR_PTR(-ENOMEM
);
2432 dev_net_set(dev
, net
);
2433 dev
->rtnl_link_ops
= ops
;
2434 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2437 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2438 if (tb
[IFLA_ADDRESS
]) {
2439 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2440 nla_len(tb
[IFLA_ADDRESS
]));
2441 dev
->addr_assign_type
= NET_ADDR_SET
;
2443 if (tb
[IFLA_BROADCAST
])
2444 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2445 nla_len(tb
[IFLA_BROADCAST
]));
2446 if (tb
[IFLA_TXQLEN
])
2447 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2448 if (tb
[IFLA_OPERSTATE
])
2449 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2450 if (tb
[IFLA_LINKMODE
])
2451 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2453 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2457 EXPORT_SYMBOL(rtnl_create_link
);
2459 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2460 struct net
*net
, int group
,
2461 struct ifinfomsg
*ifm
,
2462 struct netlink_ext_ack
*extack
,
2465 struct net_device
*dev
, *aux
;
2468 for_each_netdev_safe(net
, dev
, aux
) {
2469 if (dev
->group
== group
) {
2470 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2479 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2480 struct netlink_ext_ack
*extack
)
2482 struct net
*net
= sock_net(skb
->sk
);
2483 const struct rtnl_link_ops
*ops
;
2484 const struct rtnl_link_ops
*m_ops
= NULL
;
2485 struct net_device
*dev
;
2486 struct net_device
*master_dev
= NULL
;
2487 struct ifinfomsg
*ifm
;
2488 char kind
[MODULE_NAME_LEN
];
2489 char ifname
[IFNAMSIZ
];
2490 struct nlattr
*tb
[IFLA_MAX
+1];
2491 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2492 unsigned char name_assign_type
= NET_NAME_USER
;
2495 #ifdef CONFIG_MODULES
2498 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2502 if (tb
[IFLA_IFNAME
])
2503 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2507 ifm
= nlmsg_data(nlh
);
2508 if (ifm
->ifi_index
> 0)
2509 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2512 dev
= __dev_get_by_name(net
, ifname
);
2518 master_dev
= netdev_master_upper_dev_get(dev
);
2520 m_ops
= master_dev
->rtnl_link_ops
;
2523 err
= validate_linkmsg(dev
, tb
);
2527 if (tb
[IFLA_LINKINFO
]) {
2528 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2529 tb
[IFLA_LINKINFO
], ifla_info_policy
,
2534 memset(linkinfo
, 0, sizeof(linkinfo
));
2536 if (linkinfo
[IFLA_INFO_KIND
]) {
2537 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2538 ops
= rtnl_link_ops_get(kind
);
2545 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2546 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2547 struct nlattr
**data
= NULL
;
2548 struct nlattr
**slave_data
= NULL
;
2549 struct net
*dest_net
, *link_net
= NULL
;
2552 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2553 err
= nla_parse_nested(attr
, ops
->maxtype
,
2554 linkinfo
[IFLA_INFO_DATA
],
2560 if (ops
->validate
) {
2561 err
= ops
->validate(tb
, data
, extack
);
2568 if (m_ops
->slave_maxtype
&&
2569 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2570 err
= nla_parse_nested(slave_attr
,
2571 m_ops
->slave_maxtype
,
2572 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2573 m_ops
->slave_policy
,
2577 slave_data
= slave_attr
;
2579 if (m_ops
->slave_validate
) {
2580 err
= m_ops
->slave_validate(tb
, slave_data
,
2590 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2592 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2595 if (linkinfo
[IFLA_INFO_DATA
]) {
2596 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2600 err
= ops
->changelink(dev
, tb
, data
, extack
);
2603 status
|= DO_SETLINK_NOTIFY
;
2606 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2607 if (!m_ops
|| !m_ops
->slave_changelink
)
2610 err
= m_ops
->slave_changelink(master_dev
, dev
,
2615 status
|= DO_SETLINK_NOTIFY
;
2618 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
2622 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2623 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2624 return rtnl_group_changelink(skb
, net
,
2625 nla_get_u32(tb
[IFLA_GROUP
]),
2630 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
2634 #ifdef CONFIG_MODULES
2637 request_module("rtnl-link-%s", kind
);
2639 ops
= rtnl_link_ops_get(kind
);
2651 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2652 name_assign_type
= NET_NAME_ENUM
;
2655 dest_net
= rtnl_link_get_net(net
, tb
);
2656 if (IS_ERR(dest_net
))
2657 return PTR_ERR(dest_net
);
2660 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2663 if (tb
[IFLA_LINK_NETNSID
]) {
2664 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2666 link_net
= get_net_ns_by_id(dest_net
, id
);
2672 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2676 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2677 name_assign_type
, ops
, tb
);
2683 dev
->ifindex
= ifm
->ifi_index
;
2686 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
2688 /* Drivers should call free_netdev() in ->destructor
2689 * and unregister it on failure after registration
2690 * so that device could be finally freed in rtnl_unlock.
2693 /* If device is not registered at all, free it now */
2694 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2699 err
= register_netdevice(dev
);
2705 err
= rtnl_configure_link(dev
, ifm
);
2707 goto out_unregister
;
2709 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2711 goto out_unregister
;
2713 if (tb
[IFLA_MASTER
]) {
2714 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
2716 goto out_unregister
;
2725 LIST_HEAD(list_kill
);
2727 ops
->dellink(dev
, &list_kill
);
2728 unregister_netdevice_many(&list_kill
);
2730 unregister_netdevice(dev
);
2736 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2737 struct netlink_ext_ack
*extack
)
2739 struct net
*net
= sock_net(skb
->sk
);
2740 struct ifinfomsg
*ifm
;
2741 char ifname
[IFNAMSIZ
];
2742 struct nlattr
*tb
[IFLA_MAX
+1];
2743 struct net_device
*dev
= NULL
;
2744 struct sk_buff
*nskb
;
2746 u32 ext_filter_mask
= 0;
2748 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2752 if (tb
[IFLA_IFNAME
])
2753 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2755 if (tb
[IFLA_EXT_MASK
])
2756 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2758 ifm
= nlmsg_data(nlh
);
2759 if (ifm
->ifi_index
> 0)
2760 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2761 else if (tb
[IFLA_IFNAME
])
2762 dev
= __dev_get_by_name(net
, ifname
);
2769 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2773 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2774 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
, 0);
2776 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2777 WARN_ON(err
== -EMSGSIZE
);
2780 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2785 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2787 struct net
*net
= sock_net(skb
->sk
);
2788 struct net_device
*dev
;
2789 struct nlattr
*tb
[IFLA_MAX
+1];
2790 u32 ext_filter_mask
= 0;
2791 u16 min_ifinfo_dump_size
= 0;
2794 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2795 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2796 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2798 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
2799 if (tb
[IFLA_EXT_MASK
])
2800 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2803 if (!ext_filter_mask
)
2804 return NLMSG_GOODSIZE
;
2806 * traverse the list of net devices and compute the minimum
2807 * buffer size based upon the filter mask.
2810 for_each_netdev_rcu(net
, dev
) {
2811 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2817 return nlmsg_total_size(min_ifinfo_dump_size
);
2820 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2823 int s_idx
= cb
->family
;
2828 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2829 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2830 struct rtnl_link
*handlers
;
2831 rtnl_dumpit_func dumpit
;
2833 if (idx
< s_idx
|| idx
== PF_PACKET
)
2836 handlers
= rtnl_dereference(rtnl_msg_handlers
[idx
]);
2840 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
2845 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2849 if (dumpit(skb
, cb
))
2857 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
2858 unsigned int change
,
2859 u32 event
, gfp_t flags
)
2861 struct net
*net
= dev_net(dev
);
2862 struct sk_buff
*skb
;
2864 size_t if_info_size
;
2866 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2870 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0, event
);
2872 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2873 WARN_ON(err
== -EMSGSIZE
);
2880 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2884 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
2886 struct net
*net
= dev_net(dev
);
2888 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
2891 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
2892 unsigned int change
, u32 event
,
2895 struct sk_buff
*skb
;
2897 if (dev
->reg_state
!= NETREG_REGISTERED
)
2900 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
);
2902 rtmsg_ifinfo_send(skb
, dev
, flags
);
2905 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
2908 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
);
2910 EXPORT_SYMBOL(rtmsg_ifinfo
);
2912 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
2913 struct net_device
*dev
,
2914 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
2915 int type
, unsigned int flags
,
2916 int nlflags
, u16 ndm_state
)
2918 struct nlmsghdr
*nlh
;
2921 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2925 ndm
= nlmsg_data(nlh
);
2926 ndm
->ndm_family
= AF_BRIDGE
;
2929 ndm
->ndm_flags
= flags
;
2931 ndm
->ndm_ifindex
= dev
->ifindex
;
2932 ndm
->ndm_state
= ndm_state
;
2934 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
2935 goto nla_put_failure
;
2937 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
2938 goto nla_put_failure
;
2940 nlmsg_end(skb
, nlh
);
2944 nlmsg_cancel(skb
, nlh
);
2948 static inline size_t rtnl_fdb_nlmsg_size(void)
2950 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
2951 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
2952 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
2956 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
2959 struct net
*net
= dev_net(dev
);
2960 struct sk_buff
*skb
;
2963 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2967 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
2968 0, 0, type
, NTF_SELF
, 0, ndm_state
);
2974 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
2977 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
2981 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2983 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
2984 struct nlattr
*tb
[],
2985 struct net_device
*dev
,
2986 const unsigned char *addr
, u16 vid
,
2991 /* If aging addresses are supported device will need to
2992 * implement its own handler for this.
2994 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
2995 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3000 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3004 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3005 err
= dev_uc_add_excl(dev
, addr
);
3006 else if (is_multicast_ether_addr(addr
))
3007 err
= dev_mc_add_excl(dev
, addr
);
3009 /* Only return duplicate errors if NLM_F_EXCL is set */
3010 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3015 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3017 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
3022 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3023 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3027 vid
= nla_get_u16(vlan_attr
);
3029 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3030 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3039 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3040 struct netlink_ext_ack
*extack
)
3042 struct net
*net
= sock_net(skb
->sk
);
3044 struct nlattr
*tb
[NDA_MAX
+1];
3045 struct net_device
*dev
;
3050 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3054 ndm
= nlmsg_data(nlh
);
3055 if (ndm
->ndm_ifindex
== 0) {
3056 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3060 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3062 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3066 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3067 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3071 addr
= nla_data(tb
[NDA_LLADDR
]);
3073 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
3079 /* Support fdb on master device the net/bridge default case */
3080 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3081 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3082 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3083 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3085 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3090 ndm
->ndm_flags
&= ~NTF_MASTER
;
3093 /* Embedded bridge, macvlan, and any other device support */
3094 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3095 if (dev
->netdev_ops
->ndo_fdb_add
)
3096 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3100 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3104 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3106 ndm
->ndm_flags
&= ~NTF_SELF
;
3114 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3116 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3117 struct nlattr
*tb
[],
3118 struct net_device
*dev
,
3119 const unsigned char *addr
, u16 vid
)
3123 /* If aging addresses are supported device will need to
3124 * implement its own handler for this.
3126 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3127 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3131 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3132 err
= dev_uc_del(dev
, addr
);
3133 else if (is_multicast_ether_addr(addr
))
3134 err
= dev_mc_del(dev
, addr
);
3138 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3140 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3141 struct netlink_ext_ack
*extack
)
3143 struct net
*net
= sock_net(skb
->sk
);
3145 struct nlattr
*tb
[NDA_MAX
+1];
3146 struct net_device
*dev
;
3151 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3154 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3158 ndm
= nlmsg_data(nlh
);
3159 if (ndm
->ndm_ifindex
== 0) {
3160 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3164 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3166 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3170 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3171 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3175 addr
= nla_data(tb
[NDA_LLADDR
]);
3177 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
3183 /* Support fdb on master device the net/bridge default case */
3184 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3185 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3186 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3187 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3189 if (ops
->ndo_fdb_del
)
3190 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3195 ndm
->ndm_flags
&= ~NTF_MASTER
;
3198 /* Embedded bridge, macvlan, and any other device support */
3199 if (ndm
->ndm_flags
& NTF_SELF
) {
3200 if (dev
->netdev_ops
->ndo_fdb_del
)
3201 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3204 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3207 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3209 ndm
->ndm_flags
&= ~NTF_SELF
;
3216 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3217 struct netlink_callback
*cb
,
3218 struct net_device
*dev
,
3220 struct netdev_hw_addr_list
*list
)
3222 struct netdev_hw_addr
*ha
;
3226 portid
= NETLINK_CB(cb
->skb
).portid
;
3227 seq
= cb
->nlh
->nlmsg_seq
;
3229 list_for_each_entry(ha
, &list
->list
, list
) {
3230 if (*idx
< cb
->args
[2])
3233 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3235 RTM_NEWNEIGH
, NTF_SELF
,
3236 NLM_F_MULTI
, NUD_PERMANENT
);
3246 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3247 * @nlh: netlink message header
3250 * Default netdevice operation to dump the existing unicast address list.
3251 * Returns number of addresses from list put in skb.
3253 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3254 struct netlink_callback
*cb
,
3255 struct net_device
*dev
,
3256 struct net_device
*filter_dev
,
3261 netif_addr_lock_bh(dev
);
3262 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3265 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3267 netif_addr_unlock_bh(dev
);
3270 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3272 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3274 struct net_device
*dev
;
3275 struct nlattr
*tb
[IFLA_MAX
+1];
3276 struct net_device
*br_dev
= NULL
;
3277 const struct net_device_ops
*ops
= NULL
;
3278 const struct net_device_ops
*cops
= NULL
;
3279 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3280 struct net
*net
= sock_net(skb
->sk
);
3281 struct hlist_head
*head
;
3289 err
= nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
,
3290 IFLA_MAX
, ifla_policy
, NULL
);
3293 } else if (err
== 0) {
3294 if (tb
[IFLA_MASTER
])
3295 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3298 brport_idx
= ifm
->ifi_index
;
3301 br_dev
= __dev_get_by_index(net
, br_idx
);
3305 ops
= br_dev
->netdev_ops
;
3309 s_idx
= cb
->args
[1];
3311 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3313 head
= &net
->dev_index_head
[h
];
3314 hlist_for_each_entry(dev
, head
, index_hlist
) {
3316 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3319 if (!br_idx
) { /* user did not specify a specific bridge */
3320 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3321 br_dev
= netdev_master_upper_dev_get(dev
);
3322 cops
= br_dev
->netdev_ops
;
3325 if (dev
!= br_dev
&&
3326 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3329 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3330 !(dev
->priv_flags
& IFF_EBRIDGE
))
3338 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3339 if (cops
&& cops
->ndo_fdb_dump
) {
3340 err
= cops
->ndo_fdb_dump(skb
, cb
,
3343 if (err
== -EMSGSIZE
)
3348 if (dev
->netdev_ops
->ndo_fdb_dump
)
3349 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3353 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3355 if (err
== -EMSGSIZE
)
3360 /* reset fdb offset to 0 for rest of the interfaces */
3376 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3377 unsigned int attrnum
, unsigned int flag
)
3380 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3384 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3385 struct net_device
*dev
, u16 mode
,
3386 u32 flags
, u32 mask
, int nlflags
,
3388 int (*vlan_fill
)(struct sk_buff
*skb
,
3389 struct net_device
*dev
,
3392 struct nlmsghdr
*nlh
;
3393 struct ifinfomsg
*ifm
;
3394 struct nlattr
*br_afspec
;
3395 struct nlattr
*protinfo
;
3396 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3397 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3400 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3404 ifm
= nlmsg_data(nlh
);
3405 ifm
->ifi_family
= AF_BRIDGE
;
3407 ifm
->ifi_type
= dev
->type
;
3408 ifm
->ifi_index
= dev
->ifindex
;
3409 ifm
->ifi_flags
= dev_get_flags(dev
);
3410 ifm
->ifi_change
= 0;
3413 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3414 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3415 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3417 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3419 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3420 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3421 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3422 goto nla_put_failure
;
3424 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3426 goto nla_put_failure
;
3428 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3429 nla_nest_cancel(skb
, br_afspec
);
3430 goto nla_put_failure
;
3433 if (mode
!= BRIDGE_MODE_UNDEF
) {
3434 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3435 nla_nest_cancel(skb
, br_afspec
);
3436 goto nla_put_failure
;
3440 err
= vlan_fill(skb
, dev
, filter_mask
);
3442 nla_nest_cancel(skb
, br_afspec
);
3443 goto nla_put_failure
;
3446 nla_nest_end(skb
, br_afspec
);
3448 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3450 goto nla_put_failure
;
3452 if (brport_nla_put_flag(skb
, flags
, mask
,
3453 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3454 brport_nla_put_flag(skb
, flags
, mask
,
3455 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3456 brport_nla_put_flag(skb
, flags
, mask
,
3457 IFLA_BRPORT_FAST_LEAVE
,
3458 BR_MULTICAST_FAST_LEAVE
) ||
3459 brport_nla_put_flag(skb
, flags
, mask
,
3460 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3461 brport_nla_put_flag(skb
, flags
, mask
,
3462 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3463 brport_nla_put_flag(skb
, flags
, mask
,
3464 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3465 brport_nla_put_flag(skb
, flags
, mask
,
3466 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3467 brport_nla_put_flag(skb
, flags
, mask
,
3468 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3469 nla_nest_cancel(skb
, protinfo
);
3470 goto nla_put_failure
;
3473 nla_nest_end(skb
, protinfo
);
3475 nlmsg_end(skb
, nlh
);
3478 nlmsg_cancel(skb
, nlh
);
3479 return err
? err
: -EMSGSIZE
;
3481 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3483 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3485 struct net
*net
= sock_net(skb
->sk
);
3486 struct net_device
*dev
;
3488 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3489 u32 seq
= cb
->nlh
->nlmsg_seq
;
3490 u32 filter_mask
= 0;
3493 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3494 struct nlattr
*extfilt
;
3496 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3499 if (nla_len(extfilt
) < sizeof(filter_mask
))
3502 filter_mask
= nla_get_u32(extfilt
);
3507 for_each_netdev_rcu(net
, dev
) {
3508 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3509 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3511 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3512 if (idx
>= cb
->args
[0]) {
3513 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3514 skb
, portid
, seq
, dev
,
3515 filter_mask
, NLM_F_MULTI
);
3516 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3517 if (likely(skb
->len
))
3526 if (ops
->ndo_bridge_getlink
) {
3527 if (idx
>= cb
->args
[0]) {
3528 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3532 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3533 if (likely(skb
->len
))
3550 static inline size_t bridge_nlmsg_size(void)
3552 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3553 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3554 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3555 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3556 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3557 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3558 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3559 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3560 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3561 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3562 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3565 static int rtnl_bridge_notify(struct net_device
*dev
)
3567 struct net
*net
= dev_net(dev
);
3568 struct sk_buff
*skb
;
3569 int err
= -EOPNOTSUPP
;
3571 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3574 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3580 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3587 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3590 WARN_ON(err
== -EMSGSIZE
);
3593 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3597 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3598 struct netlink_ext_ack
*extack
)
3600 struct net
*net
= sock_net(skb
->sk
);
3601 struct ifinfomsg
*ifm
;
3602 struct net_device
*dev
;
3603 struct nlattr
*br_spec
, *attr
= NULL
;
3604 int rem
, err
= -EOPNOTSUPP
;
3606 bool have_flags
= false;
3608 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3611 ifm
= nlmsg_data(nlh
);
3612 if (ifm
->ifi_family
!= AF_BRIDGE
)
3613 return -EPFNOSUPPORT
;
3615 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3617 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3621 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3623 nla_for_each_nested(attr
, br_spec
, rem
) {
3624 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3625 if (nla_len(attr
) < sizeof(flags
))
3629 flags
= nla_get_u16(attr
);
3635 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3636 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3638 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3643 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3647 flags
&= ~BRIDGE_FLAGS_MASTER
;
3650 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3651 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3654 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3657 flags
&= ~BRIDGE_FLAGS_SELF
;
3659 /* Generate event to notify upper layer of bridge
3662 err
= rtnl_bridge_notify(dev
);
3667 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3672 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3673 struct netlink_ext_ack
*extack
)
3675 struct net
*net
= sock_net(skb
->sk
);
3676 struct ifinfomsg
*ifm
;
3677 struct net_device
*dev
;
3678 struct nlattr
*br_spec
, *attr
= NULL
;
3679 int rem
, err
= -EOPNOTSUPP
;
3681 bool have_flags
= false;
3683 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3686 ifm
= nlmsg_data(nlh
);
3687 if (ifm
->ifi_family
!= AF_BRIDGE
)
3688 return -EPFNOSUPPORT
;
3690 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3692 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3696 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3698 nla_for_each_nested(attr
, br_spec
, rem
) {
3699 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3700 if (nla_len(attr
) < sizeof(flags
))
3704 flags
= nla_get_u16(attr
);
3710 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3711 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3713 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3718 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3722 flags
&= ~BRIDGE_FLAGS_MASTER
;
3725 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3726 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3729 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3733 flags
&= ~BRIDGE_FLAGS_SELF
;
3735 /* Generate event to notify upper layer of bridge
3738 err
= rtnl_bridge_notify(dev
);
3743 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3748 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
3750 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
3751 (!idxattr
|| idxattr
== attrid
);
3754 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3755 static int rtnl_get_offload_stats_attr_size(int attr_id
)
3758 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
3759 return sizeof(struct rtnl_link_stats64
);
3765 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
3768 struct nlattr
*attr
= NULL
;
3773 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
3774 dev
->netdev_ops
->ndo_get_offload_stats
))
3777 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
3778 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
3779 if (attr_id
< *prividx
)
3782 size
= rtnl_get_offload_stats_attr_size(attr_id
);
3786 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
3789 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
3790 IFLA_OFFLOAD_XSTATS_UNSPEC
);
3792 goto nla_put_failure
;
3794 attr_data
= nla_data(attr
);
3795 memset(attr_data
, 0, size
);
3796 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
3799 goto get_offload_stats_failure
;
3810 get_offload_stats_failure
:
3815 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
3821 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
3822 dev
->netdev_ops
->ndo_get_offload_stats
))
3825 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
3826 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
3827 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
3829 size
= rtnl_get_offload_stats_attr_size(attr_id
);
3830 nla_size
+= nla_total_size_64bit(size
);
3834 nla_size
+= nla_total_size(0);
3839 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
3840 int type
, u32 pid
, u32 seq
, u32 change
,
3841 unsigned int flags
, unsigned int filter_mask
,
3842 int *idxattr
, int *prividx
)
3844 struct if_stats_msg
*ifsm
;
3845 struct nlmsghdr
*nlh
;
3846 struct nlattr
*attr
;
3847 int s_prividx
= *prividx
;
3852 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
3856 ifsm
= nlmsg_data(nlh
);
3857 ifsm
->family
= PF_UNSPEC
;
3860 ifsm
->ifindex
= dev
->ifindex
;
3861 ifsm
->filter_mask
= filter_mask
;
3863 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
3864 struct rtnl_link_stats64
*sp
;
3866 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
3867 sizeof(struct rtnl_link_stats64
),
3870 goto nla_put_failure
;
3872 sp
= nla_data(attr
);
3873 dev_get_stats(dev
, sp
);
3876 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
3877 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
3879 if (ops
&& ops
->fill_linkxstats
) {
3880 *idxattr
= IFLA_STATS_LINK_XSTATS
;
3881 attr
= nla_nest_start(skb
,
3882 IFLA_STATS_LINK_XSTATS
);
3884 goto nla_put_failure
;
3886 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
3887 nla_nest_end(skb
, attr
);
3889 goto nla_put_failure
;
3894 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
3896 const struct rtnl_link_ops
*ops
= NULL
;
3897 const struct net_device
*master
;
3899 master
= netdev_master_upper_dev_get(dev
);
3901 ops
= master
->rtnl_link_ops
;
3902 if (ops
&& ops
->fill_linkxstats
) {
3903 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
3904 attr
= nla_nest_start(skb
,
3905 IFLA_STATS_LINK_XSTATS_SLAVE
);
3907 goto nla_put_failure
;
3909 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
3910 nla_nest_end(skb
, attr
);
3912 goto nla_put_failure
;
3917 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
3919 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
3920 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
3922 goto nla_put_failure
;
3924 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
3925 if (err
== -ENODATA
)
3926 nla_nest_cancel(skb
, attr
);
3928 nla_nest_end(skb
, attr
);
3930 if (err
&& err
!= -ENODATA
)
3931 goto nla_put_failure
;
3935 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
3936 struct rtnl_af_ops
*af_ops
;
3938 *idxattr
= IFLA_STATS_AF_SPEC
;
3939 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
3941 goto nla_put_failure
;
3943 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
3944 if (af_ops
->fill_stats_af
) {
3948 af
= nla_nest_start(skb
, af_ops
->family
);
3950 goto nla_put_failure
;
3952 err
= af_ops
->fill_stats_af(skb
, dev
);
3954 if (err
== -ENODATA
)
3955 nla_nest_cancel(skb
, af
);
3957 goto nla_put_failure
;
3959 nla_nest_end(skb
, af
);
3963 nla_nest_end(skb
, attr
);
3968 nlmsg_end(skb
, nlh
);
3973 /* not a multi message or no progress mean a real error */
3974 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
3975 nlmsg_cancel(skb
, nlh
);
3977 nlmsg_end(skb
, nlh
);
3982 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
3987 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
3988 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
3990 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
3991 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
3992 int attr
= IFLA_STATS_LINK_XSTATS
;
3994 if (ops
&& ops
->get_linkxstats_size
) {
3995 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
3997 /* for IFLA_STATS_LINK_XSTATS */
3998 size
+= nla_total_size(0);
4002 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4003 struct net_device
*_dev
= (struct net_device
*)dev
;
4004 const struct rtnl_link_ops
*ops
= NULL
;
4005 const struct net_device
*master
;
4007 /* netdev_master_upper_dev_get can't take const */
4008 master
= netdev_master_upper_dev_get(_dev
);
4010 ops
= master
->rtnl_link_ops
;
4011 if (ops
&& ops
->get_linkxstats_size
) {
4012 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4014 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4016 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4017 size
+= nla_total_size(0);
4021 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4022 size
+= rtnl_get_offload_stats_size(dev
);
4024 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4025 struct rtnl_af_ops
*af_ops
;
4027 /* for IFLA_STATS_AF_SPEC */
4028 size
+= nla_total_size(0);
4030 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
4031 if (af_ops
->get_stats_af_size
) {
4032 size
+= nla_total_size(
4033 af_ops
->get_stats_af_size(dev
));
4036 size
+= nla_total_size(0);
4044 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4045 struct netlink_ext_ack
*extack
)
4047 struct net
*net
= sock_net(skb
->sk
);
4048 struct net_device
*dev
= NULL
;
4049 int idxattr
= 0, prividx
= 0;
4050 struct if_stats_msg
*ifsm
;
4051 struct sk_buff
*nskb
;
4055 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4058 ifsm
= nlmsg_data(nlh
);
4059 if (ifsm
->ifindex
> 0)
4060 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4067 filter_mask
= ifsm
->filter_mask
;
4071 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4075 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4076 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4077 0, filter_mask
, &idxattr
, &prividx
);
4079 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4080 WARN_ON(err
== -EMSGSIZE
);
4083 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4089 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4091 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4092 struct net
*net
= sock_net(skb
->sk
);
4093 unsigned int flags
= NLM_F_MULTI
;
4094 struct if_stats_msg
*ifsm
;
4095 struct hlist_head
*head
;
4096 struct net_device
*dev
;
4097 u32 filter_mask
= 0;
4101 s_idx
= cb
->args
[1];
4102 s_idxattr
= cb
->args
[2];
4103 s_prividx
= cb
->args
[3];
4105 cb
->seq
= net
->dev_base_seq
;
4107 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
))
4110 ifsm
= nlmsg_data(cb
->nlh
);
4111 filter_mask
= ifsm
->filter_mask
;
4115 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4117 head
= &net
->dev_index_head
[h
];
4118 hlist_for_each_entry(dev
, head
, index_hlist
) {
4121 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4122 NETLINK_CB(cb
->skb
).portid
,
4123 cb
->nlh
->nlmsg_seq
, 0,
4125 &s_idxattr
, &s_prividx
);
4126 /* If we ran out of room on the first message,
4129 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4135 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4141 cb
->args
[3] = s_prividx
;
4142 cb
->args
[2] = s_idxattr
;
4149 /* Process one rtnetlink message. */
4151 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4152 struct netlink_ext_ack
*extack
)
4154 struct net
*net
= sock_net(skb
->sk
);
4155 struct rtnl_link
*handlers
;
4156 int err
= -EOPNOTSUPP
;
4157 rtnl_doit_func doit
;
4163 type
= nlh
->nlmsg_type
;
4169 /* All the messages must have at least 1 byte length */
4170 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4173 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4176 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4179 if (family
>= ARRAY_SIZE(rtnl_msg_handlers
))
4183 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4186 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4189 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4191 rtnl_dumpit_func dumpit
;
4192 u16 min_dump_alloc
= 0;
4194 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
4197 handlers
= rcu_dereference(rtnl_msg_handlers
[PF_UNSPEC
]);
4201 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
4206 refcount_inc(&rtnl_msg_handlers_ref
[family
]);
4208 if (type
== RTM_GETLINK
- RTM_BASE
)
4209 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4215 struct netlink_dump_control c
= {
4217 .min_dump_alloc
= min_dump_alloc
,
4219 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4221 refcount_dec(&rtnl_msg_handlers_ref
[family
]);
4225 doit
= READ_ONCE(handlers
[type
].doit
);
4228 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4231 flags
= READ_ONCE(handlers
[type
].flags
);
4232 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4233 refcount_inc(&rtnl_msg_handlers_ref
[family
]);
4234 doit
= READ_ONCE(handlers
[type
].doit
);
4237 err
= doit(skb
, nlh
, extack
);
4238 refcount_dec(&rtnl_msg_handlers_ref
[family
]);
4245 handlers
= rtnl_dereference(rtnl_msg_handlers
[family
]);
4247 doit
= READ_ONCE(handlers
[type
].doit
);
4249 err
= doit(skb
, nlh
, extack
);
4259 static void rtnetlink_rcv(struct sk_buff
*skb
)
4261 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4264 static int rtnetlink_bind(struct net
*net
, int group
)
4267 case RTNLGRP_IPV4_MROUTE_R
:
4268 case RTNLGRP_IPV6_MROUTE_R
:
4269 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4276 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4278 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4282 case NETDEV_CHANGEMTU
:
4283 case NETDEV_CHANGEADDR
:
4284 case NETDEV_CHANGENAME
:
4285 case NETDEV_FEAT_CHANGE
:
4286 case NETDEV_BONDING_FAILOVER
:
4287 case NETDEV_POST_TYPE_CHANGE
:
4288 case NETDEV_NOTIFY_PEERS
:
4289 case NETDEV_CHANGEUPPER
:
4290 case NETDEV_RESEND_IGMP
:
4291 case NETDEV_CHANGEINFODATA
:
4292 case NETDEV_CHANGE_TX_QUEUE_LEN
:
4293 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
4302 static struct notifier_block rtnetlink_dev_notifier
= {
4303 .notifier_call
= rtnetlink_event
,
4307 static int __net_init
rtnetlink_net_init(struct net
*net
)
4310 struct netlink_kernel_cfg cfg
= {
4311 .groups
= RTNLGRP_MAX
,
4312 .input
= rtnetlink_rcv
,
4313 .cb_mutex
= &rtnl_mutex
,
4314 .flags
= NL_CFG_F_NONROOT_RECV
,
4315 .bind
= rtnetlink_bind
,
4318 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
4325 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
4327 netlink_kernel_release(net
->rtnl
);
4331 static struct pernet_operations rtnetlink_net_ops
= {
4332 .init
= rtnetlink_net_init
,
4333 .exit
= rtnetlink_net_exit
,
4336 void __init
rtnetlink_init(void)
4340 for (i
= 0; i
< ARRAY_SIZE(rtnl_msg_handlers_ref
); i
++)
4341 refcount_set(&rtnl_msg_handlers_ref
[i
], 1);
4343 if (register_pernet_subsys(&rtnetlink_net_ops
))
4344 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4346 register_netdevice_notifier(&rtnetlink_dev_notifier
);
4348 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
4349 rtnl_dump_ifinfo
, 0);
4350 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
4351 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
4352 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
4354 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
4355 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
4356 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
4358 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
4359 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
4360 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
4362 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
4363 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
4364 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
4366 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,