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_rcu(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_rcu(&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 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
485 list_del_rcu(&ops
->list
);
490 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
492 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
495 struct rtnl_af_ops
*af_ops
;
499 size
= nla_total_size(sizeof(struct nlattr
));
502 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
503 if (af_ops
->get_link_af_size
) {
504 /* AF_* + nested data */
505 size
+= nla_total_size(sizeof(struct nlattr
)) +
506 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
514 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
516 struct net_device
*master_dev
;
521 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
522 if (master_dev
&& master_dev
->rtnl_link_ops
)
528 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
529 const struct net_device
*dev
)
531 struct net_device
*master_dev
;
532 const struct rtnl_link_ops
*ops
;
533 struct nlattr
*slave_data
;
536 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
539 ops
= master_dev
->rtnl_link_ops
;
542 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
544 if (ops
->fill_slave_info
) {
545 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
548 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
550 goto err_cancel_slave_data
;
551 nla_nest_end(skb
, slave_data
);
555 err_cancel_slave_data
:
556 nla_nest_cancel(skb
, slave_data
);
560 static int rtnl_link_info_fill(struct sk_buff
*skb
,
561 const struct net_device
*dev
)
563 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
569 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
571 if (ops
->fill_xstats
) {
572 err
= ops
->fill_xstats(skb
, dev
);
576 if (ops
->fill_info
) {
577 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
580 err
= ops
->fill_info(skb
, dev
);
582 goto err_cancel_data
;
583 nla_nest_end(skb
, data
);
588 nla_nest_cancel(skb
, data
);
592 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
594 struct nlattr
*linkinfo
;
597 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
598 if (linkinfo
== NULL
)
601 err
= rtnl_link_info_fill(skb
, dev
);
603 goto err_cancel_link
;
605 err
= rtnl_link_slave_info_fill(skb
, dev
);
607 goto err_cancel_link
;
609 nla_nest_end(skb
, linkinfo
);
613 nla_nest_cancel(skb
, linkinfo
);
618 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
620 struct sock
*rtnl
= net
->rtnl
;
623 NETLINK_CB(skb
).dst_group
= group
;
625 refcount_inc(&skb
->users
);
626 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
628 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
632 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
634 struct sock
*rtnl
= net
->rtnl
;
636 return nlmsg_unicast(rtnl
, skb
, pid
);
638 EXPORT_SYMBOL(rtnl_unicast
);
640 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
641 struct nlmsghdr
*nlh
, gfp_t flags
)
643 struct sock
*rtnl
= net
->rtnl
;
647 report
= nlmsg_report(nlh
);
649 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
651 EXPORT_SYMBOL(rtnl_notify
);
653 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
655 struct sock
*rtnl
= net
->rtnl
;
657 netlink_set_err(rtnl
, 0, group
, error
);
659 EXPORT_SYMBOL(rtnl_set_sk_err
);
661 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
666 mx
= nla_nest_start(skb
, RTA_METRICS
);
670 for (i
= 0; i
< RTAX_MAX
; i
++) {
672 if (i
== RTAX_CC_ALGO
- 1) {
673 char tmp
[TCP_CA_NAME_MAX
], *name
;
675 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
678 if (nla_put_string(skb
, i
+ 1, name
))
679 goto nla_put_failure
;
680 } else if (i
== RTAX_FEATURES
- 1) {
681 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
685 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
686 if (nla_put_u32(skb
, i
+ 1, user_features
))
687 goto nla_put_failure
;
689 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
690 goto nla_put_failure
;
697 nla_nest_cancel(skb
, mx
);
701 return nla_nest_end(skb
, mx
);
704 nla_nest_cancel(skb
, mx
);
707 EXPORT_SYMBOL(rtnetlink_put_metrics
);
709 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
710 long expires
, u32 error
)
712 struct rta_cacheinfo ci
= {
713 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
714 .rta_used
= dst
->__use
,
715 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
723 clock
= jiffies_to_clock_t(abs(expires
));
724 clock
= min_t(unsigned long, clock
, INT_MAX
);
725 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
727 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
729 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
731 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
733 unsigned char operstate
= dev
->operstate
;
735 switch (transition
) {
737 if ((operstate
== IF_OPER_DORMANT
||
738 operstate
== IF_OPER_UNKNOWN
) &&
740 operstate
= IF_OPER_UP
;
743 case IF_OPER_DORMANT
:
744 if (operstate
== IF_OPER_UP
||
745 operstate
== IF_OPER_UNKNOWN
)
746 operstate
= IF_OPER_DORMANT
;
750 if (dev
->operstate
!= operstate
) {
751 write_lock_bh(&dev_base_lock
);
752 dev
->operstate
= operstate
;
753 write_unlock_bh(&dev_base_lock
);
754 netdev_state_change(dev
);
758 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
760 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
761 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
764 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
765 const struct ifinfomsg
*ifm
)
767 unsigned int flags
= ifm
->ifi_flags
;
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
771 flags
= (flags
& ifm
->ifi_change
) |
772 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
777 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
778 const struct rtnl_link_stats64
*b
)
780 a
->rx_packets
= b
->rx_packets
;
781 a
->tx_packets
= b
->tx_packets
;
782 a
->rx_bytes
= b
->rx_bytes
;
783 a
->tx_bytes
= b
->tx_bytes
;
784 a
->rx_errors
= b
->rx_errors
;
785 a
->tx_errors
= b
->tx_errors
;
786 a
->rx_dropped
= b
->rx_dropped
;
787 a
->tx_dropped
= b
->tx_dropped
;
789 a
->multicast
= b
->multicast
;
790 a
->collisions
= b
->collisions
;
792 a
->rx_length_errors
= b
->rx_length_errors
;
793 a
->rx_over_errors
= b
->rx_over_errors
;
794 a
->rx_crc_errors
= b
->rx_crc_errors
;
795 a
->rx_frame_errors
= b
->rx_frame_errors
;
796 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
797 a
->rx_missed_errors
= b
->rx_missed_errors
;
799 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
800 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
801 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
802 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
803 a
->tx_window_errors
= b
->tx_window_errors
;
805 a
->rx_compressed
= b
->rx_compressed
;
806 a
->tx_compressed
= b
->tx_compressed
;
808 a
->rx_nohandler
= b
->rx_nohandler
;
812 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
815 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
816 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
817 size_t size
= nla_total_size(0);
820 nla_total_size(sizeof(struct ifla_vf_mac
)) +
821 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
822 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
823 nla_total_size(MAX_VLAN_LIST_LEN
*
824 sizeof(struct ifla_vf_vlan_info
)) +
825 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
826 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
827 nla_total_size(sizeof(struct ifla_vf_rate
)) +
828 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
829 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
830 nla_total_size(0) + /* nest IFLA_VF_STATS */
831 /* IFLA_VF_STATS_RX_PACKETS */
832 nla_total_size_64bit(sizeof(__u64
)) +
833 /* IFLA_VF_STATS_TX_PACKETS */
834 nla_total_size_64bit(sizeof(__u64
)) +
835 /* IFLA_VF_STATS_RX_BYTES */
836 nla_total_size_64bit(sizeof(__u64
)) +
837 /* IFLA_VF_STATS_TX_BYTES */
838 nla_total_size_64bit(sizeof(__u64
)) +
839 /* IFLA_VF_STATS_BROADCAST */
840 nla_total_size_64bit(sizeof(__u64
)) +
841 /* IFLA_VF_STATS_MULTICAST */
842 nla_total_size_64bit(sizeof(__u64
)) +
843 nla_total_size(sizeof(struct ifla_vf_trust
)));
849 static size_t rtnl_port_size(const struct net_device
*dev
,
852 size_t port_size
= nla_total_size(4) /* PORT_VF */
853 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
854 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
855 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
856 + nla_total_size(1) /* PROT_VDP_REQUEST */
857 + nla_total_size(2); /* PORT_VDP_RESPONSE */
858 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
859 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
861 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
864 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
865 !(ext_filter_mask
& RTEXT_FILTER_VF
))
867 if (dev_num_vf(dev
->dev
.parent
))
868 return port_self_size
+ vf_ports_size
+
869 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
871 return port_self_size
;
874 static size_t rtnl_xdp_size(void)
876 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
877 nla_total_size(1) + /* XDP_ATTACHED */
878 nla_total_size(4); /* XDP_PROG_ID */
883 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
886 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
887 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
888 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
889 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
890 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
891 + nla_total_size(sizeof(struct rtnl_link_stats
))
892 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
893 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
894 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
895 + nla_total_size(4) /* IFLA_TXQLEN */
896 + nla_total_size(4) /* IFLA_WEIGHT */
897 + nla_total_size(4) /* IFLA_MTU */
898 + nla_total_size(4) /* IFLA_LINK */
899 + nla_total_size(4) /* IFLA_MASTER */
900 + nla_total_size(1) /* IFLA_CARRIER */
901 + nla_total_size(4) /* IFLA_PROMISCUITY */
902 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
903 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
904 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
905 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
906 + nla_total_size(1) /* IFLA_OPERSTATE */
907 + nla_total_size(1) /* IFLA_LINKMODE */
908 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
909 + nla_total_size(4) /* IFLA_LINK_NETNSID */
910 + nla_total_size(4) /* IFLA_GROUP */
911 + nla_total_size(ext_filter_mask
912 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
913 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
914 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
915 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
916 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
917 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
918 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
919 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
920 + rtnl_xdp_size() /* IFLA_XDP */
921 + nla_total_size(4) /* IFLA_EVENT */
922 + nla_total_size(4) /* IFLA_NEW_NETNSID */
923 + nla_total_size(1) /* IFLA_PROTO_DOWN */
924 + nla_total_size(4) /* IFLA_IF_NETNSID */
928 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
930 struct nlattr
*vf_ports
;
931 struct nlattr
*vf_port
;
935 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
939 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
940 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
942 goto nla_put_failure
;
943 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
944 goto nla_put_failure
;
945 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
946 if (err
== -EMSGSIZE
)
947 goto nla_put_failure
;
949 nla_nest_cancel(skb
, vf_port
);
952 nla_nest_end(skb
, vf_port
);
955 nla_nest_end(skb
, vf_ports
);
960 nla_nest_cancel(skb
, vf_ports
);
964 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
966 struct nlattr
*port_self
;
969 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
973 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
975 nla_nest_cancel(skb
, port_self
);
976 return (err
== -EMSGSIZE
) ? err
: 0;
979 nla_nest_end(skb
, port_self
);
984 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
989 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
990 !(ext_filter_mask
& RTEXT_FILTER_VF
))
993 err
= rtnl_port_self_fill(skb
, dev
);
997 if (dev_num_vf(dev
->dev
.parent
)) {
998 err
= rtnl_vf_ports_fill(skb
, dev
);
1006 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1009 struct netdev_phys_item_id ppid
;
1011 err
= dev_get_phys_port_id(dev
, &ppid
);
1013 if (err
== -EOPNOTSUPP
)
1018 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1024 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1026 char name
[IFNAMSIZ
];
1029 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1031 if (err
== -EOPNOTSUPP
)
1036 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1042 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1045 struct switchdev_attr attr
= {
1047 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1048 .flags
= SWITCHDEV_F_NO_RECURSE
,
1051 err
= switchdev_port_attr_get(dev
, &attr
);
1053 if (err
== -EOPNOTSUPP
)
1058 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1065 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1066 struct net_device
*dev
)
1068 struct rtnl_link_stats64
*sp
;
1069 struct nlattr
*attr
;
1071 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1072 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1076 sp
= nla_data(attr
);
1077 dev_get_stats(dev
, sp
);
1079 attr
= nla_reserve(skb
, IFLA_STATS
,
1080 sizeof(struct rtnl_link_stats
));
1084 copy_rtnl_link_stats(nla_data(attr
), sp
);
1089 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1090 struct net_device
*dev
,
1092 struct nlattr
*vfinfo
)
1094 struct ifla_vf_rss_query_en vf_rss_query_en
;
1095 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1096 struct ifla_vf_link_state vf_linkstate
;
1097 struct ifla_vf_vlan_info vf_vlan_info
;
1098 struct ifla_vf_spoofchk vf_spoofchk
;
1099 struct ifla_vf_tx_rate vf_tx_rate
;
1100 struct ifla_vf_stats vf_stats
;
1101 struct ifla_vf_trust vf_trust
;
1102 struct ifla_vf_vlan vf_vlan
;
1103 struct ifla_vf_rate vf_rate
;
1104 struct ifla_vf_mac vf_mac
;
1105 struct ifla_vf_info ivi
;
1107 memset(&ivi
, 0, sizeof(ivi
));
1109 /* Not all SR-IOV capable drivers support the
1110 * spoofcheck and "RSS query enable" query. Preset to
1111 * -1 so the user space tool can detect that the driver
1112 * didn't report anything.
1115 ivi
.rss_query_en
= -1;
1117 /* The default value for VF link state is "auto"
1118 * IFLA_VF_LINK_STATE_AUTO which equals zero
1121 /* VLAN Protocol by default is 802.1Q */
1122 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1123 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1126 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1135 vf_rss_query_en
.vf
=
1136 vf_trust
.vf
= ivi
.vf
;
1138 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1139 vf_vlan
.vlan
= ivi
.vlan
;
1140 vf_vlan
.qos
= ivi
.qos
;
1141 vf_vlan_info
.vlan
= ivi
.vlan
;
1142 vf_vlan_info
.qos
= ivi
.qos
;
1143 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1144 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1145 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1146 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1147 vf_spoofchk
.setting
= ivi
.spoofchk
;
1148 vf_linkstate
.link_state
= ivi
.linkstate
;
1149 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1150 vf_trust
.setting
= ivi
.trusted
;
1151 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1153 goto nla_put_vfinfo_failure
;
1154 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1155 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1156 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1158 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1160 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1162 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1164 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1165 sizeof(vf_rss_query_en
),
1166 &vf_rss_query_en
) ||
1167 nla_put(skb
, IFLA_VF_TRUST
,
1168 sizeof(vf_trust
), &vf_trust
))
1169 goto nla_put_vf_failure
;
1170 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1172 goto nla_put_vf_failure
;
1173 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1175 nla_nest_cancel(skb
, vfvlanlist
);
1176 goto nla_put_vf_failure
;
1178 nla_nest_end(skb
, vfvlanlist
);
1179 memset(&vf_stats
, 0, sizeof(vf_stats
));
1180 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1181 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1183 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1185 goto nla_put_vf_failure
;
1186 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1187 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1188 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1189 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1190 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1191 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1192 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1193 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1194 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1195 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1196 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1197 vf_stats
.multicast
, IFLA_VF_STATS_PAD
)) {
1198 nla_nest_cancel(skb
, vfstats
);
1199 goto nla_put_vf_failure
;
1201 nla_nest_end(skb
, vfstats
);
1202 nla_nest_end(skb
, vf
);
1206 nla_nest_cancel(skb
, vf
);
1207 nla_put_vfinfo_failure
:
1208 nla_nest_cancel(skb
, vfinfo
);
1212 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1213 struct net_device
*dev
,
1214 u32 ext_filter_mask
)
1216 struct nlattr
*vfinfo
;
1219 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1222 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1223 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1226 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1229 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1233 for (i
= 0; i
< num_vfs
; i
++) {
1234 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1238 nla_nest_end(skb
, vfinfo
);
1242 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1244 struct rtnl_link_ifmap map
;
1246 memset(&map
, 0, sizeof(map
));
1247 map
.mem_start
= dev
->mem_start
;
1248 map
.mem_end
= dev
->mem_end
;
1249 map
.base_addr
= dev
->base_addr
;
1252 map
.port
= dev
->if_port
;
1254 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1260 static u8
rtnl_xdp_attached_mode(struct net_device
*dev
, u32
*prog_id
)
1262 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1263 const struct bpf_prog
*generic_xdp_prog
;
1268 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1269 if (generic_xdp_prog
) {
1270 *prog_id
= generic_xdp_prog
->aux
->id
;
1271 return XDP_ATTACHED_SKB
;
1274 return XDP_ATTACHED_NONE
;
1276 return __dev_xdp_attached(dev
, ops
->ndo_bpf
, prog_id
);
1279 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1285 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1289 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
,
1290 rtnl_xdp_attached_mode(dev
, &prog_id
));
1295 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1300 nla_nest_end(skb
, xdp
);
1304 nla_nest_cancel(skb
, xdp
);
1308 static u32
rtnl_get_event(unsigned long event
)
1310 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1314 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1316 case NETDEV_FEAT_CHANGE
:
1317 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1319 case NETDEV_BONDING_FAILOVER
:
1320 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1322 case NETDEV_NOTIFY_PEERS
:
1323 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1325 case NETDEV_RESEND_IGMP
:
1326 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1328 case NETDEV_CHANGEINFODATA
:
1329 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1335 return rtnl_event_type
;
1338 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1340 const struct net_device
*upper_dev
;
1345 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1347 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1353 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
)
1355 int ifindex
= dev_get_iflink(dev
);
1357 if (dev
->ifindex
== ifindex
)
1360 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1363 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1364 struct net_device
*dev
)
1369 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1370 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1373 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1374 const struct net_device
*dev
,
1375 struct net
*src_net
)
1377 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1378 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1380 if (!net_eq(dev_net(dev
), link_net
)) {
1381 int id
= peernet2id_alloc(src_net
, link_net
);
1383 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1391 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1392 const struct net_device
*dev
,
1393 u32 ext_filter_mask
)
1395 const struct rtnl_af_ops
*af_ops
;
1396 struct nlattr
*af_spec
;
1398 af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
1402 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1406 if (!af_ops
->fill_link_af
)
1409 af
= nla_nest_start(skb
, af_ops
->family
);
1413 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1415 * Caller may return ENODATA to indicate that there
1416 * was no data to be dumped. This is not an error, it
1417 * means we should trim the attribute header and
1420 if (err
== -ENODATA
)
1421 nla_nest_cancel(skb
, af
);
1425 nla_nest_end(skb
, af
);
1428 nla_nest_end(skb
, af_spec
);
1432 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1433 struct net_device
*dev
, struct net
*src_net
,
1434 int type
, u32 pid
, u32 seq
, u32 change
,
1435 unsigned int flags
, u32 ext_filter_mask
,
1436 u32 event
, int *new_nsid
, int tgt_netnsid
)
1438 struct ifinfomsg
*ifm
;
1439 struct nlmsghdr
*nlh
;
1442 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1446 ifm
= nlmsg_data(nlh
);
1447 ifm
->ifi_family
= AF_UNSPEC
;
1449 ifm
->ifi_type
= dev
->type
;
1450 ifm
->ifi_index
= dev
->ifindex
;
1451 ifm
->ifi_flags
= dev_get_flags(dev
);
1452 ifm
->ifi_change
= change
;
1454 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_IF_NETNSID
, tgt_netnsid
))
1455 goto nla_put_failure
;
1457 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1458 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1459 nla_put_u8(skb
, IFLA_OPERSTATE
,
1460 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1461 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1462 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1463 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1464 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1465 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1466 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1467 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1469 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1471 nla_put_iflink(skb
, dev
) ||
1472 put_master_ifindex(skb
, dev
) ||
1473 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1475 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1476 nla_put_ifalias(skb
, dev
) ||
1477 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1478 atomic_read(&dev
->carrier_changes
)) ||
1479 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
))
1480 goto nla_put_failure
;
1482 if (event
!= IFLA_EVENT_NONE
) {
1483 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1484 goto nla_put_failure
;
1487 if (rtnl_fill_link_ifmap(skb
, dev
))
1488 goto nla_put_failure
;
1490 if (dev
->addr_len
) {
1491 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1492 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1493 goto nla_put_failure
;
1496 if (rtnl_phys_port_id_fill(skb
, dev
))
1497 goto nla_put_failure
;
1499 if (rtnl_phys_port_name_fill(skb
, dev
))
1500 goto nla_put_failure
;
1502 if (rtnl_phys_switch_id_fill(skb
, dev
))
1503 goto nla_put_failure
;
1505 if (rtnl_fill_stats(skb
, dev
))
1506 goto nla_put_failure
;
1508 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1509 goto nla_put_failure
;
1511 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1512 goto nla_put_failure
;
1514 if (rtnl_xdp_fill(skb
, dev
))
1515 goto nla_put_failure
;
1517 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1518 if (rtnl_link_fill(skb
, dev
) < 0)
1519 goto nla_put_failure
;
1522 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
))
1523 goto nla_put_failure
;
1526 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1527 goto nla_put_failure
;
1530 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1531 goto nla_put_failure_rcu
;
1534 nlmsg_end(skb
, nlh
);
1537 nla_put_failure_rcu
:
1540 nlmsg_cancel(skb
, nlh
);
1544 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1545 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1546 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1547 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1548 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1549 [IFLA_MTU
] = { .type
= NLA_U32
},
1550 [IFLA_LINK
] = { .type
= NLA_U32
},
1551 [IFLA_MASTER
] = { .type
= NLA_U32
},
1552 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1553 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1554 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1555 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1556 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1557 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1558 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1559 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1560 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1561 * allow 0-length string (needed to remove an alias).
1563 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1564 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1565 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1566 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1567 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1568 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1569 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1570 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1571 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1572 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1573 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1574 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1575 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1576 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1577 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1578 [IFLA_EVENT
] = { .type
= NLA_U32
},
1579 [IFLA_GROUP
] = { .type
= NLA_U32
},
1580 [IFLA_IF_NETNSID
] = { .type
= NLA_S32
},
1583 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1584 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1585 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1586 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1587 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1590 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1591 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1592 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1593 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1594 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1595 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1596 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1597 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1598 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1599 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1600 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1601 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1602 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1605 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1606 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1607 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1608 .len
= PORT_PROFILE_MAX
},
1609 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1610 .len
= PORT_UUID_MAX
},
1611 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1612 .len
= PORT_UUID_MAX
},
1613 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1614 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1616 /* Unused, but we need to keep it here since user space could
1617 * fill it. It's also broken with regard to NLA_BINARY use in
1618 * combination with structs.
1620 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1621 .len
= sizeof(struct ifla_port_vsi
) },
1624 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1625 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1626 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1627 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1628 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1631 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1633 const struct rtnl_link_ops
*ops
= NULL
;
1634 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1636 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1637 ifla_info_policy
, NULL
) < 0)
1640 if (linfo
[IFLA_INFO_KIND
]) {
1641 char kind
[MODULE_NAME_LEN
];
1643 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1644 ops
= rtnl_link_ops_get(kind
);
1650 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1652 struct net_device
*master
;
1657 master
= netdev_master_upper_dev_get(dev
);
1658 if (!master
|| master
->ifindex
!= master_idx
)
1664 static bool link_kind_filtered(const struct net_device
*dev
,
1665 const struct rtnl_link_ops
*kind_ops
)
1667 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1673 static bool link_dump_filtered(struct net_device
*dev
,
1675 const struct rtnl_link_ops
*kind_ops
)
1677 if (link_master_filtered(dev
, master_idx
) ||
1678 link_kind_filtered(dev
, kind_ops
))
1684 static struct net
*get_target_net(struct sk_buff
*skb
, int netnsid
)
1688 net
= get_net_ns_by_id(sock_net(skb
->sk
), netnsid
);
1690 return ERR_PTR(-EINVAL
);
1692 /* For now, the caller is required to have CAP_NET_ADMIN in
1693 * the user namespace owning the target net ns.
1695 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1697 return ERR_PTR(-EACCES
);
1702 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1704 struct net
*net
= sock_net(skb
->sk
);
1705 struct net
*tgt_net
= net
;
1708 struct net_device
*dev
;
1709 struct hlist_head
*head
;
1710 struct nlattr
*tb
[IFLA_MAX
+1];
1711 u32 ext_filter_mask
= 0;
1712 const struct rtnl_link_ops
*kind_ops
= NULL
;
1713 unsigned int flags
= NLM_F_MULTI
;
1720 s_idx
= cb
->args
[1];
1722 /* A hack to preserve kernel<->userspace interface.
1723 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1724 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1725 * what iproute2 < v3.9.0 used.
1726 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1727 * attribute, its netlink message is shorter than struct ifinfomsg.
1729 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1730 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1732 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
,
1733 ifla_policy
, NULL
) >= 0) {
1734 if (tb
[IFLA_IF_NETNSID
]) {
1735 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
1736 tgt_net
= get_target_net(skb
, netnsid
);
1737 if (IS_ERR(tgt_net
)) {
1743 if (tb
[IFLA_EXT_MASK
])
1744 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1746 if (tb
[IFLA_MASTER
])
1747 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1749 if (tb
[IFLA_LINKINFO
])
1750 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1752 if (master_idx
|| kind_ops
)
1753 flags
|= NLM_F_DUMP_FILTERED
;
1756 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1758 head
= &tgt_net
->dev_index_head
[h
];
1759 hlist_for_each_entry(dev
, head
, index_hlist
) {
1760 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1764 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1766 NETLINK_CB(cb
->skb
).portid
,
1767 cb
->nlh
->nlmsg_seq
, 0,
1769 ext_filter_mask
, 0, NULL
,
1773 if (likely(skb
->len
))
1787 cb
->seq
= net
->dev_base_seq
;
1788 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1795 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
1796 struct netlink_ext_ack
*exterr
)
1798 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
1800 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1802 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1805 /* Examine the link attributes and figure out which
1806 * network namespace we are talking about.
1808 if (tb
[IFLA_NET_NS_PID
])
1809 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1810 else if (tb
[IFLA_NET_NS_FD
])
1811 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1813 net
= get_net(src_net
);
1816 EXPORT_SYMBOL(rtnl_link_get_net
);
1818 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1821 if (tb
[IFLA_ADDRESS
] &&
1822 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1825 if (tb
[IFLA_BROADCAST
] &&
1826 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1830 if (tb
[IFLA_AF_SPEC
]) {
1834 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1835 const struct rtnl_af_ops
*af_ops
;
1838 af_ops
= rtnl_af_lookup(nla_type(af
));
1841 return -EAFNOSUPPORT
;
1844 if (!af_ops
->set_link_af
) {
1849 if (af_ops
->validate_link_af
) {
1850 err
= af_ops
->validate_link_af(dev
, af
);
1864 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
1867 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1869 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
1872 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
1874 if (dev
->type
!= ARPHRD_INFINIBAND
)
1877 return handle_infiniband_guid(dev
, ivt
, guid_type
);
1880 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1882 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1885 if (tb
[IFLA_VF_MAC
]) {
1886 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1889 if (ops
->ndo_set_vf_mac
)
1890 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1896 if (tb
[IFLA_VF_VLAN
]) {
1897 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1900 if (ops
->ndo_set_vf_vlan
)
1901 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
1903 htons(ETH_P_8021Q
));
1908 if (tb
[IFLA_VF_VLAN_LIST
]) {
1909 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
1910 struct nlattr
*attr
;
1914 if (!ops
->ndo_set_vf_vlan
)
1917 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
1918 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
1919 nla_len(attr
) < NLA_HDRLEN
) {
1922 if (len
>= MAX_VLAN_LIST_LEN
)
1924 ivvl
[len
] = nla_data(attr
);
1931 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
1932 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
1937 if (tb
[IFLA_VF_TX_RATE
]) {
1938 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
1939 struct ifla_vf_info ivf
;
1942 if (ops
->ndo_get_vf_config
)
1943 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1948 if (ops
->ndo_set_vf_rate
)
1949 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1956 if (tb
[IFLA_VF_RATE
]) {
1957 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1960 if (ops
->ndo_set_vf_rate
)
1961 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1968 if (tb
[IFLA_VF_SPOOFCHK
]) {
1969 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1972 if (ops
->ndo_set_vf_spoofchk
)
1973 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1979 if (tb
[IFLA_VF_LINK_STATE
]) {
1980 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1983 if (ops
->ndo_set_vf_link_state
)
1984 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1990 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1991 struct ifla_vf_rss_query_en
*ivrssq_en
;
1994 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
1995 if (ops
->ndo_set_vf_rss_query_en
)
1996 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
1997 ivrssq_en
->setting
);
2002 if (tb
[IFLA_VF_TRUST
]) {
2003 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2006 if (ops
->ndo_set_vf_trust
)
2007 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2012 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2013 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2015 if (!ops
->ndo_set_vf_guid
)
2018 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2021 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2022 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2024 if (!ops
->ndo_set_vf_guid
)
2027 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2033 static int do_set_master(struct net_device
*dev
, int ifindex
,
2034 struct netlink_ext_ack
*extack
)
2036 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2037 const struct net_device_ops
*ops
;
2041 if (upper_dev
->ifindex
== ifindex
)
2043 ops
= upper_dev
->netdev_ops
;
2044 if (ops
->ndo_del_slave
) {
2045 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2054 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2057 ops
= upper_dev
->netdev_ops
;
2058 if (ops
->ndo_add_slave
) {
2059 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2069 #define DO_SETLINK_MODIFIED 0x01
2070 /* notify flag means notify + modified. */
2071 #define DO_SETLINK_NOTIFY 0x03
2072 static int do_setlink(const struct sk_buff
*skb
,
2073 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2074 struct netlink_ext_ack
*extack
,
2075 struct nlattr
**tb
, char *ifname
, int status
)
2077 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2080 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
2081 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
2086 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
2091 err
= dev_change_net_namespace(dev
, net
, ifname
);
2095 status
|= DO_SETLINK_MODIFIED
;
2099 struct rtnl_link_ifmap
*u_map
;
2102 if (!ops
->ndo_set_config
) {
2107 if (!netif_device_present(dev
)) {
2112 u_map
= nla_data(tb
[IFLA_MAP
]);
2113 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2114 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2115 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2116 k_map
.irq
= (unsigned char) u_map
->irq
;
2117 k_map
.dma
= (unsigned char) u_map
->dma
;
2118 k_map
.port
= (unsigned char) u_map
->port
;
2120 err
= ops
->ndo_set_config(dev
, &k_map
);
2124 status
|= DO_SETLINK_NOTIFY
;
2127 if (tb
[IFLA_ADDRESS
]) {
2128 struct sockaddr
*sa
;
2131 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2133 sa
= kmalloc(len
, GFP_KERNEL
);
2138 sa
->sa_family
= dev
->type
;
2139 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2141 err
= dev_set_mac_address(dev
, sa
);
2145 status
|= DO_SETLINK_MODIFIED
;
2149 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2152 status
|= DO_SETLINK_MODIFIED
;
2155 if (tb
[IFLA_GROUP
]) {
2156 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2157 status
|= DO_SETLINK_NOTIFY
;
2161 * Interface selected by interface index but interface
2162 * name provided implies that a name change has been
2165 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2166 err
= dev_change_name(dev
, ifname
);
2169 status
|= DO_SETLINK_MODIFIED
;
2172 if (tb
[IFLA_IFALIAS
]) {
2173 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2174 nla_len(tb
[IFLA_IFALIAS
]));
2177 status
|= DO_SETLINK_NOTIFY
;
2180 if (tb
[IFLA_BROADCAST
]) {
2181 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2182 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2185 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2186 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2191 if (tb
[IFLA_MASTER
]) {
2192 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2195 status
|= DO_SETLINK_MODIFIED
;
2198 if (tb
[IFLA_CARRIER
]) {
2199 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2202 status
|= DO_SETLINK_MODIFIED
;
2205 if (tb
[IFLA_TXQLEN
]) {
2206 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2207 unsigned int orig_len
= dev
->tx_queue_len
;
2209 if (dev
->tx_queue_len
^ value
) {
2210 dev
->tx_queue_len
= value
;
2211 err
= call_netdevice_notifiers(
2212 NETDEV_CHANGE_TX_QUEUE_LEN
, dev
);
2213 err
= notifier_to_errno(err
);
2215 dev
->tx_queue_len
= orig_len
;
2218 status
|= DO_SETLINK_MODIFIED
;
2222 if (tb
[IFLA_OPERSTATE
])
2223 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2225 if (tb
[IFLA_LINKMODE
]) {
2226 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2228 write_lock_bh(&dev_base_lock
);
2229 if (dev
->link_mode
^ value
)
2230 status
|= DO_SETLINK_NOTIFY
;
2231 dev
->link_mode
= value
;
2232 write_unlock_bh(&dev_base_lock
);
2235 if (tb
[IFLA_VFINFO_LIST
]) {
2236 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2237 struct nlattr
*attr
;
2240 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2241 if (nla_type(attr
) != IFLA_VF_INFO
||
2242 nla_len(attr
) < NLA_HDRLEN
) {
2246 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2247 ifla_vf_policy
, NULL
);
2250 err
= do_setvfinfo(dev
, vfinfo
);
2253 status
|= DO_SETLINK_NOTIFY
;
2258 if (tb
[IFLA_VF_PORTS
]) {
2259 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2260 struct nlattr
*attr
;
2265 if (!ops
->ndo_set_vf_port
)
2268 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2269 if (nla_type(attr
) != IFLA_VF_PORT
||
2270 nla_len(attr
) < NLA_HDRLEN
) {
2274 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2275 ifla_port_policy
, NULL
);
2278 if (!port
[IFLA_PORT_VF
]) {
2282 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2283 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2286 status
|= DO_SETLINK_NOTIFY
;
2291 if (tb
[IFLA_PORT_SELF
]) {
2292 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2294 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2295 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2301 if (ops
->ndo_set_vf_port
)
2302 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2305 status
|= DO_SETLINK_NOTIFY
;
2308 if (tb
[IFLA_AF_SPEC
]) {
2312 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2313 const struct rtnl_af_ops
*af_ops
;
2317 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2319 err
= af_ops
->set_link_af(dev
, af
);
2326 status
|= DO_SETLINK_NOTIFY
;
2331 if (tb
[IFLA_PROTO_DOWN
]) {
2332 err
= dev_change_proto_down(dev
,
2333 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2336 status
|= DO_SETLINK_NOTIFY
;
2340 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2343 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2344 ifla_xdp_policy
, NULL
);
2348 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2353 if (xdp
[IFLA_XDP_FLAGS
]) {
2354 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2355 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2359 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2365 if (xdp
[IFLA_XDP_FD
]) {
2366 err
= dev_change_xdp_fd(dev
, extack
,
2367 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2371 status
|= DO_SETLINK_NOTIFY
;
2376 if (status
& DO_SETLINK_MODIFIED
) {
2377 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2378 netdev_state_change(dev
);
2381 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",
2388 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2389 struct netlink_ext_ack
*extack
)
2391 struct net
*net
= sock_net(skb
->sk
);
2392 struct ifinfomsg
*ifm
;
2393 struct net_device
*dev
;
2395 struct nlattr
*tb
[IFLA_MAX
+1];
2396 char ifname
[IFNAMSIZ
];
2398 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2403 if (tb
[IFLA_IF_NETNSID
])
2406 if (tb
[IFLA_IFNAME
])
2407 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2412 ifm
= nlmsg_data(nlh
);
2413 if (ifm
->ifi_index
> 0)
2414 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2415 else if (tb
[IFLA_IFNAME
])
2416 dev
= __dev_get_by_name(net
, ifname
);
2425 err
= validate_linkmsg(dev
, tb
);
2429 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2434 static int rtnl_group_dellink(const struct net
*net
, int group
)
2436 struct net_device
*dev
, *aux
;
2437 LIST_HEAD(list_kill
);
2443 for_each_netdev(net
, dev
) {
2444 if (dev
->group
== group
) {
2445 const struct rtnl_link_ops
*ops
;
2448 ops
= dev
->rtnl_link_ops
;
2449 if (!ops
|| !ops
->dellink
)
2457 for_each_netdev_safe(net
, dev
, aux
) {
2458 if (dev
->group
== group
) {
2459 const struct rtnl_link_ops
*ops
;
2461 ops
= dev
->rtnl_link_ops
;
2462 ops
->dellink(dev
, &list_kill
);
2465 unregister_netdevice_many(&list_kill
);
2470 int rtnl_delete_link(struct net_device
*dev
)
2472 const struct rtnl_link_ops
*ops
;
2473 LIST_HEAD(list_kill
);
2475 ops
= dev
->rtnl_link_ops
;
2476 if (!ops
|| !ops
->dellink
)
2479 ops
->dellink(dev
, &list_kill
);
2480 unregister_netdevice_many(&list_kill
);
2484 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2486 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2487 struct netlink_ext_ack
*extack
)
2489 struct net
*net
= sock_net(skb
->sk
);
2490 struct net_device
*dev
;
2491 struct ifinfomsg
*ifm
;
2492 char ifname
[IFNAMSIZ
];
2493 struct nlattr
*tb
[IFLA_MAX
+1];
2496 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2500 if (tb
[IFLA_IF_NETNSID
])
2503 if (tb
[IFLA_IFNAME
])
2504 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2506 ifm
= nlmsg_data(nlh
);
2507 if (ifm
->ifi_index
> 0)
2508 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2509 else if (tb
[IFLA_IFNAME
])
2510 dev
= __dev_get_by_name(net
, ifname
);
2511 else if (tb
[IFLA_GROUP
])
2512 return rtnl_group_dellink(net
, nla_get_u32(tb
[IFLA_GROUP
]));
2519 return rtnl_delete_link(dev
);
2522 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2524 unsigned int old_flags
;
2527 old_flags
= dev
->flags
;
2528 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2529 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2534 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2536 __dev_notify_flags(dev
, old_flags
, ~0U);
2539 EXPORT_SYMBOL(rtnl_configure_link
);
2541 struct net_device
*rtnl_create_link(struct net
*net
,
2542 const char *ifname
, unsigned char name_assign_type
,
2543 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2545 struct net_device
*dev
;
2546 unsigned int num_tx_queues
= 1;
2547 unsigned int num_rx_queues
= 1;
2549 if (tb
[IFLA_NUM_TX_QUEUES
])
2550 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2551 else if (ops
->get_num_tx_queues
)
2552 num_tx_queues
= ops
->get_num_tx_queues();
2554 if (tb
[IFLA_NUM_RX_QUEUES
])
2555 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2556 else if (ops
->get_num_rx_queues
)
2557 num_rx_queues
= ops
->get_num_rx_queues();
2559 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2560 ops
->setup
, num_tx_queues
, num_rx_queues
);
2562 return ERR_PTR(-ENOMEM
);
2564 dev_net_set(dev
, net
);
2565 dev
->rtnl_link_ops
= ops
;
2566 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2569 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2570 if (tb
[IFLA_ADDRESS
]) {
2571 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2572 nla_len(tb
[IFLA_ADDRESS
]));
2573 dev
->addr_assign_type
= NET_ADDR_SET
;
2575 if (tb
[IFLA_BROADCAST
])
2576 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2577 nla_len(tb
[IFLA_BROADCAST
]));
2578 if (tb
[IFLA_TXQLEN
])
2579 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2580 if (tb
[IFLA_OPERSTATE
])
2581 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2582 if (tb
[IFLA_LINKMODE
])
2583 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2585 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2589 EXPORT_SYMBOL(rtnl_create_link
);
2591 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2592 struct net
*net
, int group
,
2593 struct ifinfomsg
*ifm
,
2594 struct netlink_ext_ack
*extack
,
2597 struct net_device
*dev
, *aux
;
2600 for_each_netdev_safe(net
, dev
, aux
) {
2601 if (dev
->group
== group
) {
2602 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2611 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2612 struct netlink_ext_ack
*extack
)
2614 struct net
*net
= sock_net(skb
->sk
);
2615 const struct rtnl_link_ops
*ops
;
2616 const struct rtnl_link_ops
*m_ops
= NULL
;
2617 struct net_device
*dev
;
2618 struct net_device
*master_dev
= NULL
;
2619 struct ifinfomsg
*ifm
;
2620 char kind
[MODULE_NAME_LEN
];
2621 char ifname
[IFNAMSIZ
];
2622 struct nlattr
*tb
[IFLA_MAX
+1];
2623 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2624 unsigned char name_assign_type
= NET_NAME_USER
;
2627 #ifdef CONFIG_MODULES
2630 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2634 if (tb
[IFLA_IF_NETNSID
])
2637 if (tb
[IFLA_IFNAME
])
2638 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2642 ifm
= nlmsg_data(nlh
);
2643 if (ifm
->ifi_index
> 0)
2644 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2647 dev
= __dev_get_by_name(net
, ifname
);
2653 master_dev
= netdev_master_upper_dev_get(dev
);
2655 m_ops
= master_dev
->rtnl_link_ops
;
2658 err
= validate_linkmsg(dev
, tb
);
2662 if (tb
[IFLA_LINKINFO
]) {
2663 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2664 tb
[IFLA_LINKINFO
], ifla_info_policy
,
2669 memset(linkinfo
, 0, sizeof(linkinfo
));
2671 if (linkinfo
[IFLA_INFO_KIND
]) {
2672 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2673 ops
= rtnl_link_ops_get(kind
);
2680 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2681 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2682 struct nlattr
**data
= NULL
;
2683 struct nlattr
**slave_data
= NULL
;
2684 struct net
*dest_net
, *link_net
= NULL
;
2687 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2688 err
= nla_parse_nested(attr
, ops
->maxtype
,
2689 linkinfo
[IFLA_INFO_DATA
],
2695 if (ops
->validate
) {
2696 err
= ops
->validate(tb
, data
, extack
);
2703 if (m_ops
->slave_maxtype
&&
2704 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2705 err
= nla_parse_nested(slave_attr
,
2706 m_ops
->slave_maxtype
,
2707 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2708 m_ops
->slave_policy
,
2712 slave_data
= slave_attr
;
2719 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2721 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2724 if (linkinfo
[IFLA_INFO_DATA
]) {
2725 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2729 err
= ops
->changelink(dev
, tb
, data
, extack
);
2732 status
|= DO_SETLINK_NOTIFY
;
2735 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2736 if (!m_ops
|| !m_ops
->slave_changelink
)
2739 err
= m_ops
->slave_changelink(master_dev
, dev
,
2744 status
|= DO_SETLINK_NOTIFY
;
2747 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
2751 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2752 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2753 return rtnl_group_changelink(skb
, net
,
2754 nla_get_u32(tb
[IFLA_GROUP
]),
2759 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
2763 #ifdef CONFIG_MODULES
2766 request_module("rtnl-link-%s", kind
);
2768 ops
= rtnl_link_ops_get(kind
);
2780 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2781 name_assign_type
= NET_NAME_ENUM
;
2784 dest_net
= rtnl_link_get_net(net
, tb
);
2785 if (IS_ERR(dest_net
))
2786 return PTR_ERR(dest_net
);
2789 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2792 if (tb
[IFLA_LINK_NETNSID
]) {
2793 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2795 link_net
= get_net_ns_by_id(dest_net
, id
);
2801 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2805 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2806 name_assign_type
, ops
, tb
);
2812 dev
->ifindex
= ifm
->ifi_index
;
2815 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
2817 /* Drivers should call free_netdev() in ->destructor
2818 * and unregister it on failure after registration
2819 * so that device could be finally freed in rtnl_unlock.
2822 /* If device is not registered at all, free it now */
2823 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2828 err
= register_netdevice(dev
);
2834 err
= rtnl_configure_link(dev
, ifm
);
2836 goto out_unregister
;
2838 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2840 goto out_unregister
;
2842 if (tb
[IFLA_MASTER
]) {
2843 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]),
2846 goto out_unregister
;
2855 LIST_HEAD(list_kill
);
2857 ops
->dellink(dev
, &list_kill
);
2858 unregister_netdevice_many(&list_kill
);
2860 unregister_netdevice(dev
);
2866 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2867 struct netlink_ext_ack
*extack
)
2869 struct net
*net
= sock_net(skb
->sk
);
2870 struct net
*tgt_net
= net
;
2871 struct ifinfomsg
*ifm
;
2872 char ifname
[IFNAMSIZ
];
2873 struct nlattr
*tb
[IFLA_MAX
+1];
2874 struct net_device
*dev
= NULL
;
2875 struct sk_buff
*nskb
;
2878 u32 ext_filter_mask
= 0;
2880 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2884 if (tb
[IFLA_IF_NETNSID
]) {
2885 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
2886 tgt_net
= get_target_net(skb
, netnsid
);
2887 if (IS_ERR(tgt_net
))
2888 return PTR_ERR(tgt_net
);
2891 if (tb
[IFLA_IFNAME
])
2892 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2894 if (tb
[IFLA_EXT_MASK
])
2895 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2898 ifm
= nlmsg_data(nlh
);
2899 if (ifm
->ifi_index
> 0)
2900 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2901 else if (tb
[IFLA_IFNAME
])
2902 dev
= __dev_get_by_name(tgt_net
, ifname
);
2911 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2915 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
2916 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2917 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
2920 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2921 WARN_ON(err
== -EMSGSIZE
);
2924 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2932 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2934 struct net
*net
= sock_net(skb
->sk
);
2935 struct net_device
*dev
;
2936 struct nlattr
*tb
[IFLA_MAX
+1];
2937 u32 ext_filter_mask
= 0;
2938 u16 min_ifinfo_dump_size
= 0;
2941 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2942 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2943 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2945 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
2946 if (tb
[IFLA_EXT_MASK
])
2947 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2950 if (!ext_filter_mask
)
2951 return NLMSG_GOODSIZE
;
2953 * traverse the list of net devices and compute the minimum
2954 * buffer size based upon the filter mask.
2957 for_each_netdev_rcu(net
, dev
) {
2958 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2964 return nlmsg_total_size(min_ifinfo_dump_size
);
2967 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2970 int s_idx
= cb
->family
;
2975 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2976 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2977 struct rtnl_link
*handlers
;
2978 rtnl_dumpit_func dumpit
;
2980 if (idx
< s_idx
|| idx
== PF_PACKET
)
2983 handlers
= rtnl_dereference(rtnl_msg_handlers
[idx
]);
2987 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
2992 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2996 if (dumpit(skb
, cb
))
3004 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3005 unsigned int change
,
3006 u32 event
, gfp_t flags
, int *new_nsid
)
3008 struct net
*net
= dev_net(dev
);
3009 struct sk_buff
*skb
;
3011 size_t if_info_size
;
3013 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3017 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3018 type
, 0, 0, change
, 0, 0, event
,
3021 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3022 WARN_ON(err
== -EMSGSIZE
);
3029 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3033 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3035 struct net
*net
= dev_net(dev
);
3037 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3040 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3041 unsigned int change
, u32 event
,
3042 gfp_t flags
, int *new_nsid
)
3044 struct sk_buff
*skb
;
3046 if (dev
->reg_state
!= NETREG_REGISTERED
)
3049 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
);
3051 rtmsg_ifinfo_send(skb
, dev
, flags
);
3054 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3057 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
, NULL
);
3060 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3061 gfp_t flags
, int *new_nsid
)
3063 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3067 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3068 struct net_device
*dev
,
3069 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3070 int type
, unsigned int flags
,
3071 int nlflags
, u16 ndm_state
)
3073 struct nlmsghdr
*nlh
;
3076 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3080 ndm
= nlmsg_data(nlh
);
3081 ndm
->ndm_family
= AF_BRIDGE
;
3084 ndm
->ndm_flags
= flags
;
3086 ndm
->ndm_ifindex
= dev
->ifindex
;
3087 ndm
->ndm_state
= ndm_state
;
3089 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3090 goto nla_put_failure
;
3092 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3093 goto nla_put_failure
;
3095 nlmsg_end(skb
, nlh
);
3099 nlmsg_cancel(skb
, nlh
);
3103 static inline size_t rtnl_fdb_nlmsg_size(void)
3105 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3106 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3107 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3111 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3114 struct net
*net
= dev_net(dev
);
3115 struct sk_buff
*skb
;
3118 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3122 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3123 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3129 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3132 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3136 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3138 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3139 struct nlattr
*tb
[],
3140 struct net_device
*dev
,
3141 const unsigned char *addr
, u16 vid
,
3146 /* If aging addresses are supported device will need to
3147 * implement its own handler for this.
3149 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3150 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3155 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3159 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3160 err
= dev_uc_add_excl(dev
, addr
);
3161 else if (is_multicast_ether_addr(addr
))
3162 err
= dev_mc_add_excl(dev
, addr
);
3164 /* Only return duplicate errors if NLM_F_EXCL is set */
3165 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3170 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3172 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3173 struct netlink_ext_ack
*extack
)
3178 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3179 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3183 vid
= nla_get_u16(vlan_attr
);
3185 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3186 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3194 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3195 struct netlink_ext_ack
*extack
)
3197 struct net
*net
= sock_net(skb
->sk
);
3199 struct nlattr
*tb
[NDA_MAX
+1];
3200 struct net_device
*dev
;
3205 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3209 ndm
= nlmsg_data(nlh
);
3210 if (ndm
->ndm_ifindex
== 0) {
3211 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3215 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3217 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3221 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3222 NL_SET_ERR_MSG(extack
, "invalid address");
3226 addr
= nla_data(tb
[NDA_LLADDR
]);
3228 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3234 /* Support fdb on master device the net/bridge default case */
3235 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3236 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3237 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3238 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3240 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3245 ndm
->ndm_flags
&= ~NTF_MASTER
;
3248 /* Embedded bridge, macvlan, and any other device support */
3249 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3250 if (dev
->netdev_ops
->ndo_fdb_add
)
3251 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3255 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3259 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3261 ndm
->ndm_flags
&= ~NTF_SELF
;
3269 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3271 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3272 struct nlattr
*tb
[],
3273 struct net_device
*dev
,
3274 const unsigned char *addr
, u16 vid
)
3278 /* If aging addresses are supported device will need to
3279 * implement its own handler for this.
3281 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3282 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3286 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3287 err
= dev_uc_del(dev
, addr
);
3288 else if (is_multicast_ether_addr(addr
))
3289 err
= dev_mc_del(dev
, addr
);
3293 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3295 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3296 struct netlink_ext_ack
*extack
)
3298 struct net
*net
= sock_net(skb
->sk
);
3300 struct nlattr
*tb
[NDA_MAX
+1];
3301 struct net_device
*dev
;
3306 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3309 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3313 ndm
= nlmsg_data(nlh
);
3314 if (ndm
->ndm_ifindex
== 0) {
3315 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3319 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3321 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3325 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3326 NL_SET_ERR_MSG(extack
, "invalid address");
3330 addr
= nla_data(tb
[NDA_LLADDR
]);
3332 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3338 /* Support fdb on master device the net/bridge default case */
3339 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3340 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3341 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3342 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3344 if (ops
->ndo_fdb_del
)
3345 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3350 ndm
->ndm_flags
&= ~NTF_MASTER
;
3353 /* Embedded bridge, macvlan, and any other device support */
3354 if (ndm
->ndm_flags
& NTF_SELF
) {
3355 if (dev
->netdev_ops
->ndo_fdb_del
)
3356 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3359 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3362 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3364 ndm
->ndm_flags
&= ~NTF_SELF
;
3371 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3372 struct netlink_callback
*cb
,
3373 struct net_device
*dev
,
3375 struct netdev_hw_addr_list
*list
)
3377 struct netdev_hw_addr
*ha
;
3381 portid
= NETLINK_CB(cb
->skb
).portid
;
3382 seq
= cb
->nlh
->nlmsg_seq
;
3384 list_for_each_entry(ha
, &list
->list
, list
) {
3385 if (*idx
< cb
->args
[2])
3388 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3390 RTM_NEWNEIGH
, NTF_SELF
,
3391 NLM_F_MULTI
, NUD_PERMANENT
);
3401 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3402 * @nlh: netlink message header
3405 * Default netdevice operation to dump the existing unicast address list.
3406 * Returns number of addresses from list put in skb.
3408 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3409 struct netlink_callback
*cb
,
3410 struct net_device
*dev
,
3411 struct net_device
*filter_dev
,
3416 netif_addr_lock_bh(dev
);
3417 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3420 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3422 netif_addr_unlock_bh(dev
);
3425 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3427 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3429 struct net_device
*dev
;
3430 struct nlattr
*tb
[IFLA_MAX
+1];
3431 struct net_device
*br_dev
= NULL
;
3432 const struct net_device_ops
*ops
= NULL
;
3433 const struct net_device_ops
*cops
= NULL
;
3434 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3435 struct net
*net
= sock_net(skb
->sk
);
3436 struct hlist_head
*head
;
3444 err
= nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
,
3445 IFLA_MAX
, ifla_policy
, NULL
);
3448 } else if (err
== 0) {
3449 if (tb
[IFLA_MASTER
])
3450 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3453 brport_idx
= ifm
->ifi_index
;
3456 br_dev
= __dev_get_by_index(net
, br_idx
);
3460 ops
= br_dev
->netdev_ops
;
3464 s_idx
= cb
->args
[1];
3466 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3468 head
= &net
->dev_index_head
[h
];
3469 hlist_for_each_entry(dev
, head
, index_hlist
) {
3471 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3474 if (!br_idx
) { /* user did not specify a specific bridge */
3475 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3476 br_dev
= netdev_master_upper_dev_get(dev
);
3477 cops
= br_dev
->netdev_ops
;
3480 if (dev
!= br_dev
&&
3481 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3484 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3485 !(dev
->priv_flags
& IFF_EBRIDGE
))
3493 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3494 if (cops
&& cops
->ndo_fdb_dump
) {
3495 err
= cops
->ndo_fdb_dump(skb
, cb
,
3498 if (err
== -EMSGSIZE
)
3503 if (dev
->netdev_ops
->ndo_fdb_dump
)
3504 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3508 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3510 if (err
== -EMSGSIZE
)
3515 /* reset fdb offset to 0 for rest of the interfaces */
3531 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3532 unsigned int attrnum
, unsigned int flag
)
3535 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3539 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3540 struct net_device
*dev
, u16 mode
,
3541 u32 flags
, u32 mask
, int nlflags
,
3543 int (*vlan_fill
)(struct sk_buff
*skb
,
3544 struct net_device
*dev
,
3547 struct nlmsghdr
*nlh
;
3548 struct ifinfomsg
*ifm
;
3549 struct nlattr
*br_afspec
;
3550 struct nlattr
*protinfo
;
3551 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3552 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3555 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3559 ifm
= nlmsg_data(nlh
);
3560 ifm
->ifi_family
= AF_BRIDGE
;
3562 ifm
->ifi_type
= dev
->type
;
3563 ifm
->ifi_index
= dev
->ifindex
;
3564 ifm
->ifi_flags
= dev_get_flags(dev
);
3565 ifm
->ifi_change
= 0;
3568 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3569 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3570 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3572 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3574 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3575 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3576 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3577 goto nla_put_failure
;
3579 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3581 goto nla_put_failure
;
3583 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3584 nla_nest_cancel(skb
, br_afspec
);
3585 goto nla_put_failure
;
3588 if (mode
!= BRIDGE_MODE_UNDEF
) {
3589 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3590 nla_nest_cancel(skb
, br_afspec
);
3591 goto nla_put_failure
;
3595 err
= vlan_fill(skb
, dev
, filter_mask
);
3597 nla_nest_cancel(skb
, br_afspec
);
3598 goto nla_put_failure
;
3601 nla_nest_end(skb
, br_afspec
);
3603 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3605 goto nla_put_failure
;
3607 if (brport_nla_put_flag(skb
, flags
, mask
,
3608 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3609 brport_nla_put_flag(skb
, flags
, mask
,
3610 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3611 brport_nla_put_flag(skb
, flags
, mask
,
3612 IFLA_BRPORT_FAST_LEAVE
,
3613 BR_MULTICAST_FAST_LEAVE
) ||
3614 brport_nla_put_flag(skb
, flags
, mask
,
3615 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3616 brport_nla_put_flag(skb
, flags
, mask
,
3617 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3618 brport_nla_put_flag(skb
, flags
, mask
,
3619 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3620 brport_nla_put_flag(skb
, flags
, mask
,
3621 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3622 brport_nla_put_flag(skb
, flags
, mask
,
3623 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3624 nla_nest_cancel(skb
, protinfo
);
3625 goto nla_put_failure
;
3628 nla_nest_end(skb
, protinfo
);
3630 nlmsg_end(skb
, nlh
);
3633 nlmsg_cancel(skb
, nlh
);
3634 return err
? err
: -EMSGSIZE
;
3636 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3638 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3640 struct net
*net
= sock_net(skb
->sk
);
3641 struct net_device
*dev
;
3643 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3644 u32 seq
= cb
->nlh
->nlmsg_seq
;
3645 u32 filter_mask
= 0;
3648 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3649 struct nlattr
*extfilt
;
3651 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3654 if (nla_len(extfilt
) < sizeof(filter_mask
))
3657 filter_mask
= nla_get_u32(extfilt
);
3662 for_each_netdev_rcu(net
, dev
) {
3663 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3664 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3666 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3667 if (idx
>= cb
->args
[0]) {
3668 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3669 skb
, portid
, seq
, dev
,
3670 filter_mask
, NLM_F_MULTI
);
3671 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3672 if (likely(skb
->len
))
3681 if (ops
->ndo_bridge_getlink
) {
3682 if (idx
>= cb
->args
[0]) {
3683 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3687 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3688 if (likely(skb
->len
))
3705 static inline size_t bridge_nlmsg_size(void)
3707 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3708 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3709 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3710 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3711 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3712 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3713 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3714 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3715 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3716 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3717 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3720 static int rtnl_bridge_notify(struct net_device
*dev
)
3722 struct net
*net
= dev_net(dev
);
3723 struct sk_buff
*skb
;
3724 int err
= -EOPNOTSUPP
;
3726 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3729 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3735 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3742 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3745 WARN_ON(err
== -EMSGSIZE
);
3748 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3752 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3753 struct netlink_ext_ack
*extack
)
3755 struct net
*net
= sock_net(skb
->sk
);
3756 struct ifinfomsg
*ifm
;
3757 struct net_device
*dev
;
3758 struct nlattr
*br_spec
, *attr
= NULL
;
3759 int rem
, err
= -EOPNOTSUPP
;
3761 bool have_flags
= false;
3763 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3766 ifm
= nlmsg_data(nlh
);
3767 if (ifm
->ifi_family
!= AF_BRIDGE
)
3768 return -EPFNOSUPPORT
;
3770 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3772 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3776 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3778 nla_for_each_nested(attr
, br_spec
, rem
) {
3779 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3780 if (nla_len(attr
) < sizeof(flags
))
3784 flags
= nla_get_u16(attr
);
3790 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3791 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3793 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3798 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3802 flags
&= ~BRIDGE_FLAGS_MASTER
;
3805 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3806 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3809 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3812 flags
&= ~BRIDGE_FLAGS_SELF
;
3814 /* Generate event to notify upper layer of bridge
3817 err
= rtnl_bridge_notify(dev
);
3822 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3827 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3828 struct netlink_ext_ack
*extack
)
3830 struct net
*net
= sock_net(skb
->sk
);
3831 struct ifinfomsg
*ifm
;
3832 struct net_device
*dev
;
3833 struct nlattr
*br_spec
, *attr
= NULL
;
3834 int rem
, err
= -EOPNOTSUPP
;
3836 bool have_flags
= false;
3838 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3841 ifm
= nlmsg_data(nlh
);
3842 if (ifm
->ifi_family
!= AF_BRIDGE
)
3843 return -EPFNOSUPPORT
;
3845 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3847 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3851 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3853 nla_for_each_nested(attr
, br_spec
, rem
) {
3854 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3855 if (nla_len(attr
) < sizeof(flags
))
3859 flags
= nla_get_u16(attr
);
3865 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3866 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3868 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3873 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3877 flags
&= ~BRIDGE_FLAGS_MASTER
;
3880 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3881 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3884 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3888 flags
&= ~BRIDGE_FLAGS_SELF
;
3890 /* Generate event to notify upper layer of bridge
3893 err
= rtnl_bridge_notify(dev
);
3898 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3903 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
3905 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
3906 (!idxattr
|| idxattr
== attrid
);
3909 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3910 static int rtnl_get_offload_stats_attr_size(int attr_id
)
3913 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
3914 return sizeof(struct rtnl_link_stats64
);
3920 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
3923 struct nlattr
*attr
= NULL
;
3928 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
3929 dev
->netdev_ops
->ndo_get_offload_stats
))
3932 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
3933 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
3934 if (attr_id
< *prividx
)
3937 size
= rtnl_get_offload_stats_attr_size(attr_id
);
3941 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
3944 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
3945 IFLA_OFFLOAD_XSTATS_UNSPEC
);
3947 goto nla_put_failure
;
3949 attr_data
= nla_data(attr
);
3950 memset(attr_data
, 0, size
);
3951 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
3954 goto get_offload_stats_failure
;
3965 get_offload_stats_failure
:
3970 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
3976 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
3977 dev
->netdev_ops
->ndo_get_offload_stats
))
3980 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
3981 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
3982 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
3984 size
= rtnl_get_offload_stats_attr_size(attr_id
);
3985 nla_size
+= nla_total_size_64bit(size
);
3989 nla_size
+= nla_total_size(0);
3994 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
3995 int type
, u32 pid
, u32 seq
, u32 change
,
3996 unsigned int flags
, unsigned int filter_mask
,
3997 int *idxattr
, int *prividx
)
3999 struct if_stats_msg
*ifsm
;
4000 struct nlmsghdr
*nlh
;
4001 struct nlattr
*attr
;
4002 int s_prividx
= *prividx
;
4007 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4011 ifsm
= nlmsg_data(nlh
);
4012 ifsm
->family
= PF_UNSPEC
;
4015 ifsm
->ifindex
= dev
->ifindex
;
4016 ifsm
->filter_mask
= filter_mask
;
4018 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4019 struct rtnl_link_stats64
*sp
;
4021 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4022 sizeof(struct rtnl_link_stats64
),
4025 goto nla_put_failure
;
4027 sp
= nla_data(attr
);
4028 dev_get_stats(dev
, sp
);
4031 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4032 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4034 if (ops
&& ops
->fill_linkxstats
) {
4035 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4036 attr
= nla_nest_start(skb
,
4037 IFLA_STATS_LINK_XSTATS
);
4039 goto nla_put_failure
;
4041 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4042 nla_nest_end(skb
, attr
);
4044 goto nla_put_failure
;
4049 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4051 const struct rtnl_link_ops
*ops
= NULL
;
4052 const struct net_device
*master
;
4054 master
= netdev_master_upper_dev_get(dev
);
4056 ops
= master
->rtnl_link_ops
;
4057 if (ops
&& ops
->fill_linkxstats
) {
4058 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4059 attr
= nla_nest_start(skb
,
4060 IFLA_STATS_LINK_XSTATS_SLAVE
);
4062 goto nla_put_failure
;
4064 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4065 nla_nest_end(skb
, attr
);
4067 goto nla_put_failure
;
4072 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4074 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4075 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4077 goto nla_put_failure
;
4079 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4080 if (err
== -ENODATA
)
4081 nla_nest_cancel(skb
, attr
);
4083 nla_nest_end(skb
, attr
);
4085 if (err
&& err
!= -ENODATA
)
4086 goto nla_put_failure
;
4090 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4091 struct rtnl_af_ops
*af_ops
;
4093 *idxattr
= IFLA_STATS_AF_SPEC
;
4094 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
4096 goto nla_put_failure
;
4099 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4100 if (af_ops
->fill_stats_af
) {
4104 af
= nla_nest_start(skb
, af_ops
->family
);
4107 goto nla_put_failure
;
4109 err
= af_ops
->fill_stats_af(skb
, dev
);
4111 if (err
== -ENODATA
) {
4112 nla_nest_cancel(skb
, af
);
4113 } else if (err
< 0) {
4115 goto nla_put_failure
;
4118 nla_nest_end(skb
, af
);
4123 nla_nest_end(skb
, attr
);
4128 nlmsg_end(skb
, nlh
);
4133 /* not a multi message or no progress mean a real error */
4134 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4135 nlmsg_cancel(skb
, nlh
);
4137 nlmsg_end(skb
, nlh
);
4142 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4147 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4148 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4150 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4151 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4152 int attr
= IFLA_STATS_LINK_XSTATS
;
4154 if (ops
&& ops
->get_linkxstats_size
) {
4155 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4157 /* for IFLA_STATS_LINK_XSTATS */
4158 size
+= nla_total_size(0);
4162 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4163 struct net_device
*_dev
= (struct net_device
*)dev
;
4164 const struct rtnl_link_ops
*ops
= NULL
;
4165 const struct net_device
*master
;
4167 /* netdev_master_upper_dev_get can't take const */
4168 master
= netdev_master_upper_dev_get(_dev
);
4170 ops
= master
->rtnl_link_ops
;
4171 if (ops
&& ops
->get_linkxstats_size
) {
4172 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4174 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4176 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4177 size
+= nla_total_size(0);
4181 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4182 size
+= rtnl_get_offload_stats_size(dev
);
4184 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4185 struct rtnl_af_ops
*af_ops
;
4187 /* for IFLA_STATS_AF_SPEC */
4188 size
+= nla_total_size(0);
4191 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4192 if (af_ops
->get_stats_af_size
) {
4193 size
+= nla_total_size(
4194 af_ops
->get_stats_af_size(dev
));
4197 size
+= nla_total_size(0);
4206 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4207 struct netlink_ext_ack
*extack
)
4209 struct net
*net
= sock_net(skb
->sk
);
4210 struct net_device
*dev
= NULL
;
4211 int idxattr
= 0, prividx
= 0;
4212 struct if_stats_msg
*ifsm
;
4213 struct sk_buff
*nskb
;
4217 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4220 ifsm
= nlmsg_data(nlh
);
4221 if (ifsm
->ifindex
> 0)
4222 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4229 filter_mask
= ifsm
->filter_mask
;
4233 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4237 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4238 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4239 0, filter_mask
, &idxattr
, &prividx
);
4241 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4242 WARN_ON(err
== -EMSGSIZE
);
4245 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4251 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4253 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4254 struct net
*net
= sock_net(skb
->sk
);
4255 unsigned int flags
= NLM_F_MULTI
;
4256 struct if_stats_msg
*ifsm
;
4257 struct hlist_head
*head
;
4258 struct net_device
*dev
;
4259 u32 filter_mask
= 0;
4263 s_idx
= cb
->args
[1];
4264 s_idxattr
= cb
->args
[2];
4265 s_prividx
= cb
->args
[3];
4267 cb
->seq
= net
->dev_base_seq
;
4269 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
))
4272 ifsm
= nlmsg_data(cb
->nlh
);
4273 filter_mask
= ifsm
->filter_mask
;
4277 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4279 head
= &net
->dev_index_head
[h
];
4280 hlist_for_each_entry(dev
, head
, index_hlist
) {
4283 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4284 NETLINK_CB(cb
->skb
).portid
,
4285 cb
->nlh
->nlmsg_seq
, 0,
4287 &s_idxattr
, &s_prividx
);
4288 /* If we ran out of room on the first message,
4291 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4297 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4303 cb
->args
[3] = s_prividx
;
4304 cb
->args
[2] = s_idxattr
;
4311 /* Process one rtnetlink message. */
4313 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4314 struct netlink_ext_ack
*extack
)
4316 struct net
*net
= sock_net(skb
->sk
);
4317 struct rtnl_link
*handlers
;
4318 int err
= -EOPNOTSUPP
;
4319 rtnl_doit_func doit
;
4325 type
= nlh
->nlmsg_type
;
4331 /* All the messages must have at least 1 byte length */
4332 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4335 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4338 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4341 if (family
>= ARRAY_SIZE(rtnl_msg_handlers
))
4345 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4348 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4351 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4353 rtnl_dumpit_func dumpit
;
4354 u16 min_dump_alloc
= 0;
4356 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
4359 handlers
= rcu_dereference(rtnl_msg_handlers
[PF_UNSPEC
]);
4363 dumpit
= READ_ONCE(handlers
[type
].dumpit
);
4368 refcount_inc(&rtnl_msg_handlers_ref
[family
]);
4370 if (type
== RTM_GETLINK
- RTM_BASE
)
4371 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4377 struct netlink_dump_control c
= {
4379 .min_dump_alloc
= min_dump_alloc
,
4381 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4383 refcount_dec(&rtnl_msg_handlers_ref
[family
]);
4387 doit
= READ_ONCE(handlers
[type
].doit
);
4390 handlers
= rcu_dereference(rtnl_msg_handlers
[family
]);
4393 flags
= READ_ONCE(handlers
[type
].flags
);
4394 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4395 refcount_inc(&rtnl_msg_handlers_ref
[family
]);
4396 doit
= READ_ONCE(handlers
[type
].doit
);
4399 err
= doit(skb
, nlh
, extack
);
4400 refcount_dec(&rtnl_msg_handlers_ref
[family
]);
4407 handlers
= rtnl_dereference(rtnl_msg_handlers
[family
]);
4409 doit
= READ_ONCE(handlers
[type
].doit
);
4411 err
= doit(skb
, nlh
, extack
);
4421 static void rtnetlink_rcv(struct sk_buff
*skb
)
4423 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4426 static int rtnetlink_bind(struct net
*net
, int group
)
4429 case RTNLGRP_IPV4_MROUTE_R
:
4430 case RTNLGRP_IPV6_MROUTE_R
:
4431 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4438 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4440 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4444 case NETDEV_CHANGEMTU
:
4445 case NETDEV_CHANGEADDR
:
4446 case NETDEV_CHANGENAME
:
4447 case NETDEV_FEAT_CHANGE
:
4448 case NETDEV_BONDING_FAILOVER
:
4449 case NETDEV_POST_TYPE_CHANGE
:
4450 case NETDEV_NOTIFY_PEERS
:
4451 case NETDEV_CHANGEUPPER
:
4452 case NETDEV_RESEND_IGMP
:
4453 case NETDEV_CHANGEINFODATA
:
4454 case NETDEV_CHANGELOWERSTATE
:
4455 case NETDEV_CHANGE_TX_QUEUE_LEN
:
4456 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
4465 static struct notifier_block rtnetlink_dev_notifier
= {
4466 .notifier_call
= rtnetlink_event
,
4470 static int __net_init
rtnetlink_net_init(struct net
*net
)
4473 struct netlink_kernel_cfg cfg
= {
4474 .groups
= RTNLGRP_MAX
,
4475 .input
= rtnetlink_rcv
,
4476 .cb_mutex
= &rtnl_mutex
,
4477 .flags
= NL_CFG_F_NONROOT_RECV
,
4478 .bind
= rtnetlink_bind
,
4481 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
4488 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
4490 netlink_kernel_release(net
->rtnl
);
4494 static struct pernet_operations rtnetlink_net_ops
= {
4495 .init
= rtnetlink_net_init
,
4496 .exit
= rtnetlink_net_exit
,
4499 void __init
rtnetlink_init(void)
4503 for (i
= 0; i
< ARRAY_SIZE(rtnl_msg_handlers_ref
); i
++)
4504 refcount_set(&rtnl_msg_handlers_ref
[i
], 1);
4506 if (register_pernet_subsys(&rtnetlink_net_ops
))
4507 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4509 register_netdevice_notifier(&rtnetlink_dev_notifier
);
4511 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
4512 rtnl_dump_ifinfo
, 0);
4513 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
4514 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
4515 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
4517 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
4518 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
4519 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
4521 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
4522 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
4523 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
4525 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
4526 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
4527 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
4529 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,