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
;
70 static DEFINE_MUTEX(rtnl_mutex
);
74 mutex_lock(&rtnl_mutex
);
76 EXPORT_SYMBOL(rtnl_lock
);
78 static struct sk_buff
*defer_kfree_skb_list
;
79 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
82 tail
->next
= defer_kfree_skb_list
;
83 defer_kfree_skb_list
= head
;
86 EXPORT_SYMBOL(rtnl_kfree_skbs
);
88 void __rtnl_unlock(void)
90 struct sk_buff
*head
= defer_kfree_skb_list
;
92 defer_kfree_skb_list
= NULL
;
94 mutex_unlock(&rtnl_mutex
);
97 struct sk_buff
*next
= head
->next
;
105 void rtnl_unlock(void)
107 /* This fellow will unlock it for us. */
110 EXPORT_SYMBOL(rtnl_unlock
);
112 int rtnl_trylock(void)
114 return mutex_trylock(&rtnl_mutex
);
116 EXPORT_SYMBOL(rtnl_trylock
);
118 int rtnl_is_locked(void)
120 return mutex_is_locked(&rtnl_mutex
);
122 EXPORT_SYMBOL(rtnl_is_locked
);
124 #ifdef CONFIG_PROVE_LOCKING
125 bool lockdep_rtnl_is_held(void)
127 return lockdep_is_held(&rtnl_mutex
);
129 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
130 #endif /* #ifdef CONFIG_PROVE_LOCKING */
132 static struct rtnl_link
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
134 static inline int rtm_msgindex(int msgtype
)
136 int msgindex
= msgtype
- RTM_BASE
;
139 * msgindex < 0 implies someone tried to register a netlink
140 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
141 * the message type has not been added to linux/rtnetlink.h
143 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
148 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
150 struct rtnl_link
**tab
;
152 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
153 protocol
= PF_UNSPEC
;
155 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
157 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
162 static int rtnl_register_internal(struct module
*owner
,
163 int protocol
, int msgtype
,
164 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
167 struct rtnl_link
*link
, *old
;
168 struct rtnl_link __rcu
**tab
;
172 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
173 msgindex
= rtm_msgindex(msgtype
);
176 tab
= rtnl_msg_handlers
[protocol
];
178 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
182 /* ensures we see the 0 stores */
183 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
186 old
= rtnl_dereference(tab
[msgindex
]);
188 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
192 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
197 WARN_ON(link
->owner
&& link
->owner
!= owner
);
200 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
203 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
205 link
->dumpit
= dumpit
;
207 link
->flags
|= flags
;
209 /* publish protocol:msgtype */
210 rcu_assign_pointer(tab
[msgindex
], link
);
220 * rtnl_register_module - Register a rtnetlink message type
222 * @owner: module registering the hook (THIS_MODULE)
223 * @protocol: Protocol family or PF_UNSPEC
224 * @msgtype: rtnetlink message type
225 * @doit: Function pointer called for each request message
226 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
227 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
229 * Like rtnl_register, but for use by removable modules.
231 int rtnl_register_module(struct module
*owner
,
232 int protocol
, int msgtype
,
233 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
236 return rtnl_register_internal(owner
, protocol
, msgtype
,
237 doit
, dumpit
, flags
);
239 EXPORT_SYMBOL_GPL(rtnl_register_module
);
242 * rtnl_register - Register a rtnetlink message type
243 * @protocol: Protocol family or PF_UNSPEC
244 * @msgtype: rtnetlink message type
245 * @doit: Function pointer called for each request message
246 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
247 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
249 * Registers the specified function pointers (at least one of them has
250 * to be non-NULL) to be called whenever a request message for the
251 * specified protocol family and message type is received.
253 * The special protocol family PF_UNSPEC may be used to define fallback
254 * function pointers for the case when no entry for the specific protocol
257 void rtnl_register(int protocol
, int msgtype
,
258 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
263 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
266 pr_err("Unable to register rtnetlink message handler, "
267 "protocol = %d, message type = %d\n", protocol
, msgtype
);
271 * rtnl_unregister - Unregister a rtnetlink message type
272 * @protocol: Protocol family or PF_UNSPEC
273 * @msgtype: rtnetlink message type
275 * Returns 0 on success or a negative error code.
277 int rtnl_unregister(int protocol
, int msgtype
)
279 struct rtnl_link
**tab
, *link
;
282 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
283 msgindex
= rtm_msgindex(msgtype
);
286 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
292 link
= tab
[msgindex
];
293 rcu_assign_pointer(tab
[msgindex
], NULL
);
296 kfree_rcu(link
, rcu
);
300 EXPORT_SYMBOL_GPL(rtnl_unregister
);
303 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
304 * @protocol : Protocol family or PF_UNSPEC
306 * Identical to calling rtnl_unregster() for all registered message types
307 * of a certain protocol family.
309 void rtnl_unregister_all(int protocol
)
311 struct rtnl_link
**tab
, *link
;
314 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
317 tab
= rtnl_msg_handlers
[protocol
];
318 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
319 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
320 link
= tab
[msgindex
];
324 rcu_assign_pointer(tab
[msgindex
], NULL
);
325 kfree_rcu(link
, rcu
);
333 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
335 static LIST_HEAD(link_ops
);
337 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
339 const struct rtnl_link_ops
*ops
;
341 list_for_each_entry(ops
, &link_ops
, list
) {
342 if (!strcmp(ops
->kind
, kind
))
349 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
350 * @ops: struct rtnl_link_ops * to register
352 * The caller must hold the rtnl_mutex. This function should be used
353 * by drivers that create devices during module initialization. It
354 * must be called before registering the devices.
356 * Returns 0 on success or a negative error code.
358 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
360 if (rtnl_link_ops_get(ops
->kind
))
363 /* The check for setup is here because if ops
364 * does not have that filled up, it is not possible
365 * to use the ops for creating device. So do not
366 * fill up dellink as well. That disables rtnl_dellink.
368 if (ops
->setup
&& !ops
->dellink
)
369 ops
->dellink
= unregister_netdevice_queue
;
371 list_add_tail(&ops
->list
, &link_ops
);
374 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
377 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
378 * @ops: struct rtnl_link_ops * to register
380 * Returns 0 on success or a negative error code.
382 int rtnl_link_register(struct rtnl_link_ops
*ops
)
387 err
= __rtnl_link_register(ops
);
391 EXPORT_SYMBOL_GPL(rtnl_link_register
);
393 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
395 struct net_device
*dev
;
396 LIST_HEAD(list_kill
);
398 for_each_netdev(net
, dev
) {
399 if (dev
->rtnl_link_ops
== ops
)
400 ops
->dellink(dev
, &list_kill
);
402 unregister_netdevice_many(&list_kill
);
406 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
407 * @ops: struct rtnl_link_ops * to unregister
409 * The caller must hold the rtnl_mutex.
411 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
416 __rtnl_kill_links(net
, ops
);
418 list_del(&ops
->list
);
420 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
422 /* Return with the rtnl_lock held when there are no network
423 * devices unregistering in any network namespace.
425 static void rtnl_lock_unregistering_all(void)
429 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
431 add_wait_queue(&netdev_unregistering_wq
, &wait
);
433 unregistering
= false;
436 if (net
->dev_unreg_count
> 0) {
437 unregistering
= true;
445 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
447 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
451 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
452 * @ops: struct rtnl_link_ops * to unregister
454 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
456 /* Close the race with cleanup_net() */
457 mutex_lock(&net_mutex
);
458 rtnl_lock_unregistering_all();
459 __rtnl_link_unregister(ops
);
461 mutex_unlock(&net_mutex
);
463 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
465 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
467 struct net_device
*master_dev
;
468 const struct rtnl_link_ops
*ops
;
473 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
477 ops
= master_dev
->rtnl_link_ops
;
478 if (!ops
|| !ops
->get_slave_size
)
480 /* IFLA_INFO_SLAVE_DATA + nested data */
481 size
= nla_total_size(sizeof(struct nlattr
)) +
482 ops
->get_slave_size(master_dev
, dev
);
489 static size_t rtnl_link_get_size(const struct net_device
*dev
)
491 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
497 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
498 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
501 /* IFLA_INFO_DATA + nested data */
502 size
+= nla_total_size(sizeof(struct nlattr
)) +
505 if (ops
->get_xstats_size
)
506 /* IFLA_INFO_XSTATS */
507 size
+= nla_total_size(ops
->get_xstats_size(dev
));
509 size
+= rtnl_link_get_slave_info_data_size(dev
);
514 static LIST_HEAD(rtnl_af_ops
);
516 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
518 const struct rtnl_af_ops
*ops
;
520 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
521 if (ops
->family
== family
)
529 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
530 * @ops: struct rtnl_af_ops * to register
532 * Returns 0 on success or a negative error code.
534 void rtnl_af_register(struct rtnl_af_ops
*ops
)
537 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
540 EXPORT_SYMBOL_GPL(rtnl_af_register
);
543 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
544 * @ops: struct rtnl_af_ops * to unregister
546 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
549 list_del_rcu(&ops
->list
);
554 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
556 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
559 struct rtnl_af_ops
*af_ops
;
563 size
= nla_total_size(sizeof(struct nlattr
));
566 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
567 if (af_ops
->get_link_af_size
) {
568 /* AF_* + nested data */
569 size
+= nla_total_size(sizeof(struct nlattr
)) +
570 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
578 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
580 struct net_device
*master_dev
;
585 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
586 if (master_dev
&& master_dev
->rtnl_link_ops
)
592 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
593 const struct net_device
*dev
)
595 struct net_device
*master_dev
;
596 const struct rtnl_link_ops
*ops
;
597 struct nlattr
*slave_data
;
600 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
603 ops
= master_dev
->rtnl_link_ops
;
606 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
608 if (ops
->fill_slave_info
) {
609 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
612 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
614 goto err_cancel_slave_data
;
615 nla_nest_end(skb
, slave_data
);
619 err_cancel_slave_data
:
620 nla_nest_cancel(skb
, slave_data
);
624 static int rtnl_link_info_fill(struct sk_buff
*skb
,
625 const struct net_device
*dev
)
627 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
633 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
635 if (ops
->fill_xstats
) {
636 err
= ops
->fill_xstats(skb
, dev
);
640 if (ops
->fill_info
) {
641 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
644 err
= ops
->fill_info(skb
, dev
);
646 goto err_cancel_data
;
647 nla_nest_end(skb
, data
);
652 nla_nest_cancel(skb
, data
);
656 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
658 struct nlattr
*linkinfo
;
661 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
662 if (linkinfo
== NULL
)
665 err
= rtnl_link_info_fill(skb
, dev
);
667 goto err_cancel_link
;
669 err
= rtnl_link_slave_info_fill(skb
, dev
);
671 goto err_cancel_link
;
673 nla_nest_end(skb
, linkinfo
);
677 nla_nest_cancel(skb
, linkinfo
);
682 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
684 struct sock
*rtnl
= net
->rtnl
;
687 NETLINK_CB(skb
).dst_group
= group
;
689 refcount_inc(&skb
->users
);
690 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
692 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
696 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
698 struct sock
*rtnl
= net
->rtnl
;
700 return nlmsg_unicast(rtnl
, skb
, pid
);
702 EXPORT_SYMBOL(rtnl_unicast
);
704 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
705 struct nlmsghdr
*nlh
, gfp_t flags
)
707 struct sock
*rtnl
= net
->rtnl
;
711 report
= nlmsg_report(nlh
);
713 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
715 EXPORT_SYMBOL(rtnl_notify
);
717 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
719 struct sock
*rtnl
= net
->rtnl
;
721 netlink_set_err(rtnl
, 0, group
, error
);
723 EXPORT_SYMBOL(rtnl_set_sk_err
);
725 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
730 mx
= nla_nest_start(skb
, RTA_METRICS
);
734 for (i
= 0; i
< RTAX_MAX
; i
++) {
736 if (i
== RTAX_CC_ALGO
- 1) {
737 char tmp
[TCP_CA_NAME_MAX
], *name
;
739 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
742 if (nla_put_string(skb
, i
+ 1, name
))
743 goto nla_put_failure
;
744 } else if (i
== RTAX_FEATURES
- 1) {
745 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
749 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
750 if (nla_put_u32(skb
, i
+ 1, user_features
))
751 goto nla_put_failure
;
753 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
754 goto nla_put_failure
;
761 nla_nest_cancel(skb
, mx
);
765 return nla_nest_end(skb
, mx
);
768 nla_nest_cancel(skb
, mx
);
771 EXPORT_SYMBOL(rtnetlink_put_metrics
);
773 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
774 long expires
, u32 error
)
776 struct rta_cacheinfo ci
= {
777 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
778 .rta_used
= dst
->__use
,
779 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
787 clock
= jiffies_to_clock_t(abs(expires
));
788 clock
= min_t(unsigned long, clock
, INT_MAX
);
789 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
791 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
793 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
795 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
797 unsigned char operstate
= dev
->operstate
;
799 switch (transition
) {
801 if ((operstate
== IF_OPER_DORMANT
||
802 operstate
== IF_OPER_UNKNOWN
) &&
804 operstate
= IF_OPER_UP
;
807 case IF_OPER_DORMANT
:
808 if (operstate
== IF_OPER_UP
||
809 operstate
== IF_OPER_UNKNOWN
)
810 operstate
= IF_OPER_DORMANT
;
814 if (dev
->operstate
!= operstate
) {
815 write_lock_bh(&dev_base_lock
);
816 dev
->operstate
= operstate
;
817 write_unlock_bh(&dev_base_lock
);
818 netdev_state_change(dev
);
822 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
824 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
825 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
828 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
829 const struct ifinfomsg
*ifm
)
831 unsigned int flags
= ifm
->ifi_flags
;
833 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
835 flags
= (flags
& ifm
->ifi_change
) |
836 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
841 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
842 const struct rtnl_link_stats64
*b
)
844 a
->rx_packets
= b
->rx_packets
;
845 a
->tx_packets
= b
->tx_packets
;
846 a
->rx_bytes
= b
->rx_bytes
;
847 a
->tx_bytes
= b
->tx_bytes
;
848 a
->rx_errors
= b
->rx_errors
;
849 a
->tx_errors
= b
->tx_errors
;
850 a
->rx_dropped
= b
->rx_dropped
;
851 a
->tx_dropped
= b
->tx_dropped
;
853 a
->multicast
= b
->multicast
;
854 a
->collisions
= b
->collisions
;
856 a
->rx_length_errors
= b
->rx_length_errors
;
857 a
->rx_over_errors
= b
->rx_over_errors
;
858 a
->rx_crc_errors
= b
->rx_crc_errors
;
859 a
->rx_frame_errors
= b
->rx_frame_errors
;
860 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
861 a
->rx_missed_errors
= b
->rx_missed_errors
;
863 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
864 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
865 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
866 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
867 a
->tx_window_errors
= b
->tx_window_errors
;
869 a
->rx_compressed
= b
->rx_compressed
;
870 a
->tx_compressed
= b
->tx_compressed
;
872 a
->rx_nohandler
= b
->rx_nohandler
;
876 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
879 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
880 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
881 size_t size
= nla_total_size(0);
884 nla_total_size(sizeof(struct ifla_vf_mac
)) +
885 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
886 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
887 nla_total_size(MAX_VLAN_LIST_LEN
*
888 sizeof(struct ifla_vf_vlan_info
)) +
889 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
890 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
891 nla_total_size(sizeof(struct ifla_vf_rate
)) +
892 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
893 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
894 nla_total_size(0) + /* nest IFLA_VF_STATS */
895 /* IFLA_VF_STATS_RX_PACKETS */
896 nla_total_size_64bit(sizeof(__u64
)) +
897 /* IFLA_VF_STATS_TX_PACKETS */
898 nla_total_size_64bit(sizeof(__u64
)) +
899 /* IFLA_VF_STATS_RX_BYTES */
900 nla_total_size_64bit(sizeof(__u64
)) +
901 /* IFLA_VF_STATS_TX_BYTES */
902 nla_total_size_64bit(sizeof(__u64
)) +
903 /* IFLA_VF_STATS_BROADCAST */
904 nla_total_size_64bit(sizeof(__u64
)) +
905 /* IFLA_VF_STATS_MULTICAST */
906 nla_total_size_64bit(sizeof(__u64
)) +
907 /* IFLA_VF_STATS_RX_DROPPED */
908 nla_total_size_64bit(sizeof(__u64
)) +
909 /* IFLA_VF_STATS_TX_DROPPED */
910 nla_total_size_64bit(sizeof(__u64
)) +
911 nla_total_size(sizeof(struct ifla_vf_trust
)));
917 static size_t rtnl_port_size(const struct net_device
*dev
,
920 size_t port_size
= nla_total_size(4) /* PORT_VF */
921 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
922 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
923 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
924 + nla_total_size(1) /* PROT_VDP_REQUEST */
925 + nla_total_size(2); /* PORT_VDP_RESPONSE */
926 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
927 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
929 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
932 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
933 !(ext_filter_mask
& RTEXT_FILTER_VF
))
935 if (dev_num_vf(dev
->dev
.parent
))
936 return port_self_size
+ vf_ports_size
+
937 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
939 return port_self_size
;
942 static size_t rtnl_xdp_size(void)
944 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
945 nla_total_size(1) + /* XDP_ATTACHED */
946 nla_total_size(4); /* XDP_PROG_ID */
951 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
954 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
955 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
956 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
957 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
958 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
959 + nla_total_size(sizeof(struct rtnl_link_stats
))
960 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
961 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
962 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
963 + nla_total_size(4) /* IFLA_TXQLEN */
964 + nla_total_size(4) /* IFLA_WEIGHT */
965 + nla_total_size(4) /* IFLA_MTU */
966 + nla_total_size(4) /* IFLA_LINK */
967 + nla_total_size(4) /* IFLA_MASTER */
968 + nla_total_size(1) /* IFLA_CARRIER */
969 + nla_total_size(4) /* IFLA_PROMISCUITY */
970 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
971 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
972 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
973 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
974 + nla_total_size(1) /* IFLA_OPERSTATE */
975 + nla_total_size(1) /* IFLA_LINKMODE */
976 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
977 + nla_total_size(4) /* IFLA_LINK_NETNSID */
978 + nla_total_size(4) /* IFLA_GROUP */
979 + nla_total_size(ext_filter_mask
980 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
981 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
982 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
983 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
984 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
985 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
986 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
987 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
988 + rtnl_xdp_size() /* IFLA_XDP */
989 + nla_total_size(4) /* IFLA_EVENT */
990 + nla_total_size(4) /* IFLA_NEW_NETNSID */
991 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
992 + nla_total_size(1) /* IFLA_PROTO_DOWN */
993 + nla_total_size(4) /* IFLA_IF_NETNSID */
994 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
995 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
999 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1001 struct nlattr
*vf_ports
;
1002 struct nlattr
*vf_port
;
1006 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
1010 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1011 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
1013 goto nla_put_failure
;
1014 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1015 goto nla_put_failure
;
1016 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1017 if (err
== -EMSGSIZE
)
1018 goto nla_put_failure
;
1020 nla_nest_cancel(skb
, vf_port
);
1023 nla_nest_end(skb
, vf_port
);
1026 nla_nest_end(skb
, vf_ports
);
1031 nla_nest_cancel(skb
, vf_ports
);
1035 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1037 struct nlattr
*port_self
;
1040 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
1044 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1046 nla_nest_cancel(skb
, port_self
);
1047 return (err
== -EMSGSIZE
) ? err
: 0;
1050 nla_nest_end(skb
, port_self
);
1055 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1056 u32 ext_filter_mask
)
1060 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1061 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1064 err
= rtnl_port_self_fill(skb
, dev
);
1068 if (dev_num_vf(dev
->dev
.parent
)) {
1069 err
= rtnl_vf_ports_fill(skb
, dev
);
1077 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1080 struct netdev_phys_item_id ppid
;
1082 err
= dev_get_phys_port_id(dev
, &ppid
);
1084 if (err
== -EOPNOTSUPP
)
1089 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1095 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1097 char name
[IFNAMSIZ
];
1100 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1102 if (err
== -EOPNOTSUPP
)
1107 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1113 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1116 struct switchdev_attr attr
= {
1118 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1119 .flags
= SWITCHDEV_F_NO_RECURSE
,
1122 err
= switchdev_port_attr_get(dev
, &attr
);
1124 if (err
== -EOPNOTSUPP
)
1129 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1136 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1137 struct net_device
*dev
)
1139 struct rtnl_link_stats64
*sp
;
1140 struct nlattr
*attr
;
1142 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1143 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1147 sp
= nla_data(attr
);
1148 dev_get_stats(dev
, sp
);
1150 attr
= nla_reserve(skb
, IFLA_STATS
,
1151 sizeof(struct rtnl_link_stats
));
1155 copy_rtnl_link_stats(nla_data(attr
), sp
);
1160 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1161 struct net_device
*dev
,
1163 struct nlattr
*vfinfo
)
1165 struct ifla_vf_rss_query_en vf_rss_query_en
;
1166 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1167 struct ifla_vf_link_state vf_linkstate
;
1168 struct ifla_vf_vlan_info vf_vlan_info
;
1169 struct ifla_vf_spoofchk vf_spoofchk
;
1170 struct ifla_vf_tx_rate vf_tx_rate
;
1171 struct ifla_vf_stats vf_stats
;
1172 struct ifla_vf_trust vf_trust
;
1173 struct ifla_vf_vlan vf_vlan
;
1174 struct ifla_vf_rate vf_rate
;
1175 struct ifla_vf_mac vf_mac
;
1176 struct ifla_vf_info ivi
;
1178 memset(&ivi
, 0, sizeof(ivi
));
1180 /* Not all SR-IOV capable drivers support the
1181 * spoofcheck and "RSS query enable" query. Preset to
1182 * -1 so the user space tool can detect that the driver
1183 * didn't report anything.
1186 ivi
.rss_query_en
= -1;
1188 /* The default value for VF link state is "auto"
1189 * IFLA_VF_LINK_STATE_AUTO which equals zero
1192 /* VLAN Protocol by default is 802.1Q */
1193 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1194 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1197 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1206 vf_rss_query_en
.vf
=
1207 vf_trust
.vf
= ivi
.vf
;
1209 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1210 vf_vlan
.vlan
= ivi
.vlan
;
1211 vf_vlan
.qos
= ivi
.qos
;
1212 vf_vlan_info
.vlan
= ivi
.vlan
;
1213 vf_vlan_info
.qos
= ivi
.qos
;
1214 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1215 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1216 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1217 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1218 vf_spoofchk
.setting
= ivi
.spoofchk
;
1219 vf_linkstate
.link_state
= ivi
.linkstate
;
1220 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1221 vf_trust
.setting
= ivi
.trusted
;
1222 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1224 goto nla_put_vfinfo_failure
;
1225 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1226 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1227 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1229 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1231 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1233 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1235 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1236 sizeof(vf_rss_query_en
),
1237 &vf_rss_query_en
) ||
1238 nla_put(skb
, IFLA_VF_TRUST
,
1239 sizeof(vf_trust
), &vf_trust
))
1240 goto nla_put_vf_failure
;
1241 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1243 goto nla_put_vf_failure
;
1244 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1246 nla_nest_cancel(skb
, vfvlanlist
);
1247 goto nla_put_vf_failure
;
1249 nla_nest_end(skb
, vfvlanlist
);
1250 memset(&vf_stats
, 0, sizeof(vf_stats
));
1251 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1252 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1254 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1256 goto nla_put_vf_failure
;
1257 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1258 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1259 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1260 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1261 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1262 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1263 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1264 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1265 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1266 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1267 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1268 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1269 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1270 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1271 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1272 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1273 nla_nest_cancel(skb
, vfstats
);
1274 goto nla_put_vf_failure
;
1276 nla_nest_end(skb
, vfstats
);
1277 nla_nest_end(skb
, vf
);
1281 nla_nest_cancel(skb
, vf
);
1282 nla_put_vfinfo_failure
:
1283 nla_nest_cancel(skb
, vfinfo
);
1287 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1288 struct net_device
*dev
,
1289 u32 ext_filter_mask
)
1291 struct nlattr
*vfinfo
;
1294 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1297 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1298 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1301 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1304 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1308 for (i
= 0; i
< num_vfs
; i
++) {
1309 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1313 nla_nest_end(skb
, vfinfo
);
1317 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1319 struct rtnl_link_ifmap map
;
1321 memset(&map
, 0, sizeof(map
));
1322 map
.mem_start
= dev
->mem_start
;
1323 map
.mem_end
= dev
->mem_end
;
1324 map
.base_addr
= dev
->base_addr
;
1327 map
.port
= dev
->if_port
;
1329 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1335 static u8
rtnl_xdp_attached_mode(struct net_device
*dev
, u32
*prog_id
)
1337 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1338 const struct bpf_prog
*generic_xdp_prog
;
1339 struct netdev_bpf xdp
;
1344 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1345 if (generic_xdp_prog
) {
1346 *prog_id
= generic_xdp_prog
->aux
->id
;
1347 return XDP_ATTACHED_SKB
;
1350 return XDP_ATTACHED_NONE
;
1352 __dev_xdp_query(dev
, ops
->ndo_bpf
, &xdp
);
1353 *prog_id
= xdp
.prog_id
;
1355 return xdp
.prog_attached
;
1358 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1364 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1368 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
,
1369 rtnl_xdp_attached_mode(dev
, &prog_id
));
1374 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1379 nla_nest_end(skb
, xdp
);
1383 nla_nest_cancel(skb
, xdp
);
1387 static u32
rtnl_get_event(unsigned long event
)
1389 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1393 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1395 case NETDEV_FEAT_CHANGE
:
1396 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1398 case NETDEV_BONDING_FAILOVER
:
1399 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1401 case NETDEV_NOTIFY_PEERS
:
1402 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1404 case NETDEV_RESEND_IGMP
:
1405 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1407 case NETDEV_CHANGEINFODATA
:
1408 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1414 return rtnl_event_type
;
1417 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1419 const struct net_device
*upper_dev
;
1424 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1426 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1432 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
)
1434 int ifindex
= dev_get_iflink(dev
);
1436 if (dev
->ifindex
== ifindex
)
1439 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1442 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1443 struct net_device
*dev
)
1448 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1449 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1452 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1453 const struct net_device
*dev
,
1454 struct net
*src_net
)
1456 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1457 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1459 if (!net_eq(dev_net(dev
), link_net
)) {
1460 int id
= peernet2id_alloc(src_net
, link_net
);
1462 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1470 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1471 const struct net_device
*dev
,
1472 u32 ext_filter_mask
)
1474 const struct rtnl_af_ops
*af_ops
;
1475 struct nlattr
*af_spec
;
1477 af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
1481 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1485 if (!af_ops
->fill_link_af
)
1488 af
= nla_nest_start(skb
, af_ops
->family
);
1492 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1494 * Caller may return ENODATA to indicate that there
1495 * was no data to be dumped. This is not an error, it
1496 * means we should trim the attribute header and
1499 if (err
== -ENODATA
)
1500 nla_nest_cancel(skb
, af
);
1504 nla_nest_end(skb
, af
);
1507 nla_nest_end(skb
, af_spec
);
1511 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1512 struct net_device
*dev
, struct net
*src_net
,
1513 int type
, u32 pid
, u32 seq
, u32 change
,
1514 unsigned int flags
, u32 ext_filter_mask
,
1515 u32 event
, int *new_nsid
, int new_ifindex
,
1518 struct ifinfomsg
*ifm
;
1519 struct nlmsghdr
*nlh
;
1522 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1526 ifm
= nlmsg_data(nlh
);
1527 ifm
->ifi_family
= AF_UNSPEC
;
1529 ifm
->ifi_type
= dev
->type
;
1530 ifm
->ifi_index
= dev
->ifindex
;
1531 ifm
->ifi_flags
= dev_get_flags(dev
);
1532 ifm
->ifi_change
= change
;
1534 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_IF_NETNSID
, tgt_netnsid
))
1535 goto nla_put_failure
;
1537 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1538 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1539 nla_put_u8(skb
, IFLA_OPERSTATE
,
1540 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1541 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1542 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1543 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1544 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1545 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1546 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1547 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1549 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1551 nla_put_iflink(skb
, dev
) ||
1552 put_master_ifindex(skb
, dev
) ||
1553 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1555 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1556 nla_put_ifalias(skb
, dev
) ||
1557 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1558 atomic_read(&dev
->carrier_up_count
) +
1559 atomic_read(&dev
->carrier_down_count
)) ||
1560 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1561 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1562 atomic_read(&dev
->carrier_up_count
)) ||
1563 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1564 atomic_read(&dev
->carrier_down_count
)))
1565 goto nla_put_failure
;
1567 if (event
!= IFLA_EVENT_NONE
) {
1568 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1569 goto nla_put_failure
;
1572 if (rtnl_fill_link_ifmap(skb
, dev
))
1573 goto nla_put_failure
;
1575 if (dev
->addr_len
) {
1576 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1577 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1578 goto nla_put_failure
;
1581 if (rtnl_phys_port_id_fill(skb
, dev
))
1582 goto nla_put_failure
;
1584 if (rtnl_phys_port_name_fill(skb
, dev
))
1585 goto nla_put_failure
;
1587 if (rtnl_phys_switch_id_fill(skb
, dev
))
1588 goto nla_put_failure
;
1590 if (rtnl_fill_stats(skb
, dev
))
1591 goto nla_put_failure
;
1593 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1594 goto nla_put_failure
;
1596 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1597 goto nla_put_failure
;
1599 if (rtnl_xdp_fill(skb
, dev
))
1600 goto nla_put_failure
;
1602 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1603 if (rtnl_link_fill(skb
, dev
) < 0)
1604 goto nla_put_failure
;
1607 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
))
1608 goto nla_put_failure
;
1611 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1612 goto nla_put_failure
;
1614 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1615 goto nla_put_failure
;
1619 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1620 goto nla_put_failure_rcu
;
1623 nlmsg_end(skb
, nlh
);
1626 nla_put_failure_rcu
:
1629 nlmsg_cancel(skb
, nlh
);
1633 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1634 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1635 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1636 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1637 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1638 [IFLA_MTU
] = { .type
= NLA_U32
},
1639 [IFLA_LINK
] = { .type
= NLA_U32
},
1640 [IFLA_MASTER
] = { .type
= NLA_U32
},
1641 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1642 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1643 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1644 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1645 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1646 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1647 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1648 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1649 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1650 * allow 0-length string (needed to remove an alias).
1652 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1653 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1654 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1655 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1656 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1657 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1658 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1659 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1660 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1661 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1662 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1663 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1664 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1665 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1666 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1667 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1668 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1669 [IFLA_EVENT
] = { .type
= NLA_U32
},
1670 [IFLA_GROUP
] = { .type
= NLA_U32
},
1671 [IFLA_IF_NETNSID
] = { .type
= NLA_S32
},
1672 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1673 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1676 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1677 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1678 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1679 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1680 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1683 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1684 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1685 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1686 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1687 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1688 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1689 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1690 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1691 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1692 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1693 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1694 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1695 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1698 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1699 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1700 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1701 .len
= PORT_PROFILE_MAX
},
1702 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1703 .len
= PORT_UUID_MAX
},
1704 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1705 .len
= PORT_UUID_MAX
},
1706 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1707 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1709 /* Unused, but we need to keep it here since user space could
1710 * fill it. It's also broken with regard to NLA_BINARY use in
1711 * combination with structs.
1713 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1714 .len
= sizeof(struct ifla_port_vsi
) },
1717 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1718 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1719 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1720 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1721 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1724 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1726 const struct rtnl_link_ops
*ops
= NULL
;
1727 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1729 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1730 ifla_info_policy
, NULL
) < 0)
1733 if (linfo
[IFLA_INFO_KIND
]) {
1734 char kind
[MODULE_NAME_LEN
];
1736 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1737 ops
= rtnl_link_ops_get(kind
);
1743 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1745 struct net_device
*master
;
1750 master
= netdev_master_upper_dev_get(dev
);
1751 if (!master
|| master
->ifindex
!= master_idx
)
1757 static bool link_kind_filtered(const struct net_device
*dev
,
1758 const struct rtnl_link_ops
*kind_ops
)
1760 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1766 static bool link_dump_filtered(struct net_device
*dev
,
1768 const struct rtnl_link_ops
*kind_ops
)
1770 if (link_master_filtered(dev
, master_idx
) ||
1771 link_kind_filtered(dev
, kind_ops
))
1777 static struct net
*get_target_net(struct sock
*sk
, int netnsid
)
1781 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1783 return ERR_PTR(-EINVAL
);
1785 /* For now, the caller is required to have CAP_NET_ADMIN in
1786 * the user namespace owning the target net ns.
1788 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1790 return ERR_PTR(-EACCES
);
1795 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1797 struct net
*net
= sock_net(skb
->sk
);
1798 struct net
*tgt_net
= net
;
1801 struct net_device
*dev
;
1802 struct hlist_head
*head
;
1803 struct nlattr
*tb
[IFLA_MAX
+1];
1804 u32 ext_filter_mask
= 0;
1805 const struct rtnl_link_ops
*kind_ops
= NULL
;
1806 unsigned int flags
= NLM_F_MULTI
;
1813 s_idx
= cb
->args
[1];
1815 /* A hack to preserve kernel<->userspace interface.
1816 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1817 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1818 * what iproute2 < v3.9.0 used.
1819 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1820 * attribute, its netlink message is shorter than struct ifinfomsg.
1822 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1823 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1825 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
,
1826 ifla_policy
, NULL
) >= 0) {
1827 if (tb
[IFLA_IF_NETNSID
]) {
1828 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
1829 tgt_net
= get_target_net(skb
->sk
, netnsid
);
1830 if (IS_ERR(tgt_net
)) {
1836 if (tb
[IFLA_EXT_MASK
])
1837 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1839 if (tb
[IFLA_MASTER
])
1840 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1842 if (tb
[IFLA_LINKINFO
])
1843 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1845 if (master_idx
|| kind_ops
)
1846 flags
|= NLM_F_DUMP_FILTERED
;
1849 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1851 head
= &tgt_net
->dev_index_head
[h
];
1852 hlist_for_each_entry(dev
, head
, index_hlist
) {
1853 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1857 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1859 NETLINK_CB(cb
->skb
).portid
,
1860 cb
->nlh
->nlmsg_seq
, 0,
1862 ext_filter_mask
, 0, NULL
, 0,
1866 if (likely(skb
->len
))
1880 cb
->seq
= net
->dev_base_seq
;
1881 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1888 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
1889 struct netlink_ext_ack
*exterr
)
1891 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
1893 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1895 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1898 /* Examine the link attributes and figure out which
1899 * network namespace we are talking about.
1901 if (tb
[IFLA_NET_NS_PID
])
1902 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1903 else if (tb
[IFLA_NET_NS_FD
])
1904 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1906 net
= get_net(src_net
);
1909 EXPORT_SYMBOL(rtnl_link_get_net
);
1911 /* Figure out which network namespace we are talking about by
1912 * examining the link attributes in the following order:
1914 * 1. IFLA_NET_NS_PID
1916 * 3. IFLA_IF_NETNSID
1918 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
1919 struct nlattr
*tb
[])
1923 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
1924 return rtnl_link_get_net(src_net
, tb
);
1926 if (!tb
[IFLA_IF_NETNSID
])
1927 return get_net(src_net
);
1929 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_IF_NETNSID
]));
1931 return ERR_PTR(-EINVAL
);
1936 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
1937 struct net
*src_net
,
1938 struct nlattr
*tb
[], int cap
)
1942 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
1946 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
1948 return ERR_PTR(-EPERM
);
1954 /* Verify that rtnetlink requests do not pass additional properties
1955 * potentially referring to different network namespaces.
1957 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
1958 struct netlink_ext_ack
*extack
,
1962 if (netns_id_only
) {
1963 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
1966 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
1970 if (tb
[IFLA_IF_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
1973 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
1976 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
1982 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
1986 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1989 if (tb
[IFLA_ADDRESS
] &&
1990 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1993 if (tb
[IFLA_BROADCAST
] &&
1994 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1998 if (tb
[IFLA_AF_SPEC
]) {
2002 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2003 const struct rtnl_af_ops
*af_ops
;
2006 af_ops
= rtnl_af_lookup(nla_type(af
));
2009 return -EAFNOSUPPORT
;
2012 if (!af_ops
->set_link_af
) {
2017 if (af_ops
->validate_link_af
) {
2018 err
= af_ops
->validate_link_af(dev
, af
);
2032 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2035 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2037 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2040 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2042 if (dev
->type
!= ARPHRD_INFINIBAND
)
2045 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2048 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2050 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2053 if (tb
[IFLA_VF_MAC
]) {
2054 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2057 if (ops
->ndo_set_vf_mac
)
2058 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2064 if (tb
[IFLA_VF_VLAN
]) {
2065 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2068 if (ops
->ndo_set_vf_vlan
)
2069 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2071 htons(ETH_P_8021Q
));
2076 if (tb
[IFLA_VF_VLAN_LIST
]) {
2077 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2078 struct nlattr
*attr
;
2082 if (!ops
->ndo_set_vf_vlan
)
2085 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2086 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2087 nla_len(attr
) < NLA_HDRLEN
) {
2090 if (len
>= MAX_VLAN_LIST_LEN
)
2092 ivvl
[len
] = nla_data(attr
);
2099 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2100 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2105 if (tb
[IFLA_VF_TX_RATE
]) {
2106 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2107 struct ifla_vf_info ivf
;
2110 if (ops
->ndo_get_vf_config
)
2111 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2116 if (ops
->ndo_set_vf_rate
)
2117 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2124 if (tb
[IFLA_VF_RATE
]) {
2125 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2128 if (ops
->ndo_set_vf_rate
)
2129 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2136 if (tb
[IFLA_VF_SPOOFCHK
]) {
2137 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2140 if (ops
->ndo_set_vf_spoofchk
)
2141 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2147 if (tb
[IFLA_VF_LINK_STATE
]) {
2148 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2151 if (ops
->ndo_set_vf_link_state
)
2152 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2158 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2159 struct ifla_vf_rss_query_en
*ivrssq_en
;
2162 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2163 if (ops
->ndo_set_vf_rss_query_en
)
2164 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2165 ivrssq_en
->setting
);
2170 if (tb
[IFLA_VF_TRUST
]) {
2171 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2174 if (ops
->ndo_set_vf_trust
)
2175 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2180 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2181 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2183 if (!ops
->ndo_set_vf_guid
)
2186 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2189 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2190 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2192 if (!ops
->ndo_set_vf_guid
)
2195 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2201 static int do_set_master(struct net_device
*dev
, int ifindex
,
2202 struct netlink_ext_ack
*extack
)
2204 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2205 const struct net_device_ops
*ops
;
2209 if (upper_dev
->ifindex
== ifindex
)
2211 ops
= upper_dev
->netdev_ops
;
2212 if (ops
->ndo_del_slave
) {
2213 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2222 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2225 ops
= upper_dev
->netdev_ops
;
2226 if (ops
->ndo_add_slave
) {
2227 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2237 #define DO_SETLINK_MODIFIED 0x01
2238 /* notify flag means notify + modified. */
2239 #define DO_SETLINK_NOTIFY 0x03
2240 static int do_setlink(const struct sk_buff
*skb
,
2241 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2242 struct netlink_ext_ack
*extack
,
2243 struct nlattr
**tb
, char *ifname
, int status
)
2245 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2248 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_IF_NETNSID
]) {
2249 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2256 err
= dev_change_net_namespace(dev
, net
, ifname
);
2260 status
|= DO_SETLINK_MODIFIED
;
2264 struct rtnl_link_ifmap
*u_map
;
2267 if (!ops
->ndo_set_config
) {
2272 if (!netif_device_present(dev
)) {
2277 u_map
= nla_data(tb
[IFLA_MAP
]);
2278 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2279 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2280 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2281 k_map
.irq
= (unsigned char) u_map
->irq
;
2282 k_map
.dma
= (unsigned char) u_map
->dma
;
2283 k_map
.port
= (unsigned char) u_map
->port
;
2285 err
= ops
->ndo_set_config(dev
, &k_map
);
2289 status
|= DO_SETLINK_NOTIFY
;
2292 if (tb
[IFLA_ADDRESS
]) {
2293 struct sockaddr
*sa
;
2296 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2298 sa
= kmalloc(len
, GFP_KERNEL
);
2303 sa
->sa_family
= dev
->type
;
2304 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2306 err
= dev_set_mac_address(dev
, sa
);
2310 status
|= DO_SETLINK_MODIFIED
;
2314 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2317 status
|= DO_SETLINK_MODIFIED
;
2320 if (tb
[IFLA_GROUP
]) {
2321 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2322 status
|= DO_SETLINK_NOTIFY
;
2326 * Interface selected by interface index but interface
2327 * name provided implies that a name change has been
2330 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2331 err
= dev_change_name(dev
, ifname
);
2334 status
|= DO_SETLINK_MODIFIED
;
2337 if (tb
[IFLA_IFALIAS
]) {
2338 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2339 nla_len(tb
[IFLA_IFALIAS
]));
2342 status
|= DO_SETLINK_NOTIFY
;
2345 if (tb
[IFLA_BROADCAST
]) {
2346 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2347 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2350 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2351 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2356 if (tb
[IFLA_MASTER
]) {
2357 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2360 status
|= DO_SETLINK_MODIFIED
;
2363 if (tb
[IFLA_CARRIER
]) {
2364 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2367 status
|= DO_SETLINK_MODIFIED
;
2370 if (tb
[IFLA_TXQLEN
]) {
2371 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2373 err
= dev_change_tx_queue_len(dev
, value
);
2376 status
|= DO_SETLINK_MODIFIED
;
2379 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2380 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2382 if (max_size
> GSO_MAX_SIZE
) {
2387 if (dev
->gso_max_size
^ max_size
) {
2388 netif_set_gso_max_size(dev
, max_size
);
2389 status
|= DO_SETLINK_MODIFIED
;
2393 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2394 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2396 if (max_segs
> GSO_MAX_SEGS
) {
2401 if (dev
->gso_max_segs
^ max_segs
) {
2402 dev
->gso_max_segs
= max_segs
;
2403 status
|= DO_SETLINK_MODIFIED
;
2407 if (tb
[IFLA_OPERSTATE
])
2408 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2410 if (tb
[IFLA_LINKMODE
]) {
2411 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2413 write_lock_bh(&dev_base_lock
);
2414 if (dev
->link_mode
^ value
)
2415 status
|= DO_SETLINK_NOTIFY
;
2416 dev
->link_mode
= value
;
2417 write_unlock_bh(&dev_base_lock
);
2420 if (tb
[IFLA_VFINFO_LIST
]) {
2421 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2422 struct nlattr
*attr
;
2425 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2426 if (nla_type(attr
) != IFLA_VF_INFO
||
2427 nla_len(attr
) < NLA_HDRLEN
) {
2431 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2432 ifla_vf_policy
, NULL
);
2435 err
= do_setvfinfo(dev
, vfinfo
);
2438 status
|= DO_SETLINK_NOTIFY
;
2443 if (tb
[IFLA_VF_PORTS
]) {
2444 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2445 struct nlattr
*attr
;
2450 if (!ops
->ndo_set_vf_port
)
2453 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2454 if (nla_type(attr
) != IFLA_VF_PORT
||
2455 nla_len(attr
) < NLA_HDRLEN
) {
2459 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2460 ifla_port_policy
, NULL
);
2463 if (!port
[IFLA_PORT_VF
]) {
2467 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2468 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2471 status
|= DO_SETLINK_NOTIFY
;
2476 if (tb
[IFLA_PORT_SELF
]) {
2477 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2479 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2480 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2486 if (ops
->ndo_set_vf_port
)
2487 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2490 status
|= DO_SETLINK_NOTIFY
;
2493 if (tb
[IFLA_AF_SPEC
]) {
2497 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2498 const struct rtnl_af_ops
*af_ops
;
2502 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2504 err
= af_ops
->set_link_af(dev
, af
);
2511 status
|= DO_SETLINK_NOTIFY
;
2516 if (tb
[IFLA_PROTO_DOWN
]) {
2517 err
= dev_change_proto_down(dev
,
2518 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2521 status
|= DO_SETLINK_NOTIFY
;
2525 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2528 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2529 ifla_xdp_policy
, NULL
);
2533 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2538 if (xdp
[IFLA_XDP_FLAGS
]) {
2539 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2540 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2544 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2550 if (xdp
[IFLA_XDP_FD
]) {
2551 err
= dev_change_xdp_fd(dev
, extack
,
2552 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2556 status
|= DO_SETLINK_NOTIFY
;
2561 if (status
& DO_SETLINK_MODIFIED
) {
2562 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2563 netdev_state_change(dev
);
2566 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",
2573 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2574 struct netlink_ext_ack
*extack
)
2576 struct net
*net
= sock_net(skb
->sk
);
2577 struct ifinfomsg
*ifm
;
2578 struct net_device
*dev
;
2580 struct nlattr
*tb
[IFLA_MAX
+1];
2581 char ifname
[IFNAMSIZ
];
2583 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2588 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2592 if (tb
[IFLA_IFNAME
])
2593 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2598 ifm
= nlmsg_data(nlh
);
2599 if (ifm
->ifi_index
> 0)
2600 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2601 else if (tb
[IFLA_IFNAME
])
2602 dev
= __dev_get_by_name(net
, ifname
);
2611 err
= validate_linkmsg(dev
, tb
);
2615 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2620 static int rtnl_group_dellink(const struct net
*net
, int group
)
2622 struct net_device
*dev
, *aux
;
2623 LIST_HEAD(list_kill
);
2629 for_each_netdev(net
, dev
) {
2630 if (dev
->group
== group
) {
2631 const struct rtnl_link_ops
*ops
;
2634 ops
= dev
->rtnl_link_ops
;
2635 if (!ops
|| !ops
->dellink
)
2643 for_each_netdev_safe(net
, dev
, aux
) {
2644 if (dev
->group
== group
) {
2645 const struct rtnl_link_ops
*ops
;
2647 ops
= dev
->rtnl_link_ops
;
2648 ops
->dellink(dev
, &list_kill
);
2651 unregister_netdevice_many(&list_kill
);
2656 int rtnl_delete_link(struct net_device
*dev
)
2658 const struct rtnl_link_ops
*ops
;
2659 LIST_HEAD(list_kill
);
2661 ops
= dev
->rtnl_link_ops
;
2662 if (!ops
|| !ops
->dellink
)
2665 ops
->dellink(dev
, &list_kill
);
2666 unregister_netdevice_many(&list_kill
);
2670 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2672 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2673 struct netlink_ext_ack
*extack
)
2675 struct net
*net
= sock_net(skb
->sk
);
2676 struct net
*tgt_net
= net
;
2677 struct net_device
*dev
= NULL
;
2678 struct ifinfomsg
*ifm
;
2679 char ifname
[IFNAMSIZ
];
2680 struct nlattr
*tb
[IFLA_MAX
+1];
2684 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2688 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2692 if (tb
[IFLA_IFNAME
])
2693 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2695 if (tb
[IFLA_IF_NETNSID
]) {
2696 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
2697 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
2698 if (IS_ERR(tgt_net
))
2699 return PTR_ERR(tgt_net
);
2703 ifm
= nlmsg_data(nlh
);
2704 if (ifm
->ifi_index
> 0)
2705 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2706 else if (tb
[IFLA_IFNAME
])
2707 dev
= __dev_get_by_name(tgt_net
, ifname
);
2708 else if (tb
[IFLA_GROUP
])
2709 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2714 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2720 err
= rtnl_delete_link(dev
);
2729 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2731 unsigned int old_flags
;
2734 old_flags
= dev
->flags
;
2735 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2736 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2741 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2743 __dev_notify_flags(dev
, old_flags
, ~0U);
2746 EXPORT_SYMBOL(rtnl_configure_link
);
2748 struct net_device
*rtnl_create_link(struct net
*net
,
2749 const char *ifname
, unsigned char name_assign_type
,
2750 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2752 struct net_device
*dev
;
2753 unsigned int num_tx_queues
= 1;
2754 unsigned int num_rx_queues
= 1;
2756 if (tb
[IFLA_NUM_TX_QUEUES
])
2757 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2758 else if (ops
->get_num_tx_queues
)
2759 num_tx_queues
= ops
->get_num_tx_queues();
2761 if (tb
[IFLA_NUM_RX_QUEUES
])
2762 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2763 else if (ops
->get_num_rx_queues
)
2764 num_rx_queues
= ops
->get_num_rx_queues();
2766 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2767 ops
->setup
, num_tx_queues
, num_rx_queues
);
2769 return ERR_PTR(-ENOMEM
);
2771 dev_net_set(dev
, net
);
2772 dev
->rtnl_link_ops
= ops
;
2773 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2776 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2777 if (tb
[IFLA_ADDRESS
]) {
2778 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2779 nla_len(tb
[IFLA_ADDRESS
]));
2780 dev
->addr_assign_type
= NET_ADDR_SET
;
2782 if (tb
[IFLA_BROADCAST
])
2783 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2784 nla_len(tb
[IFLA_BROADCAST
]));
2785 if (tb
[IFLA_TXQLEN
])
2786 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2787 if (tb
[IFLA_OPERSTATE
])
2788 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2789 if (tb
[IFLA_LINKMODE
])
2790 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2792 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2793 if (tb
[IFLA_GSO_MAX_SIZE
])
2794 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
2795 if (tb
[IFLA_GSO_MAX_SEGS
])
2796 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2800 EXPORT_SYMBOL(rtnl_create_link
);
2802 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2803 struct net
*net
, int group
,
2804 struct ifinfomsg
*ifm
,
2805 struct netlink_ext_ack
*extack
,
2808 struct net_device
*dev
, *aux
;
2811 for_each_netdev_safe(net
, dev
, aux
) {
2812 if (dev
->group
== group
) {
2813 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2822 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2823 struct netlink_ext_ack
*extack
)
2825 struct net
*net
= sock_net(skb
->sk
);
2826 const struct rtnl_link_ops
*ops
;
2827 const struct rtnl_link_ops
*m_ops
= NULL
;
2828 struct net_device
*dev
;
2829 struct net_device
*master_dev
= NULL
;
2830 struct ifinfomsg
*ifm
;
2831 char kind
[MODULE_NAME_LEN
];
2832 char ifname
[IFNAMSIZ
];
2833 struct nlattr
*tb
[IFLA_MAX
+1];
2834 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2835 unsigned char name_assign_type
= NET_NAME_USER
;
2838 #ifdef CONFIG_MODULES
2841 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2845 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2849 if (tb
[IFLA_IFNAME
])
2850 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2854 ifm
= nlmsg_data(nlh
);
2855 if (ifm
->ifi_index
> 0)
2856 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2859 dev
= __dev_get_by_name(net
, ifname
);
2865 master_dev
= netdev_master_upper_dev_get(dev
);
2867 m_ops
= master_dev
->rtnl_link_ops
;
2870 err
= validate_linkmsg(dev
, tb
);
2874 if (tb
[IFLA_LINKINFO
]) {
2875 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2876 tb
[IFLA_LINKINFO
], ifla_info_policy
,
2881 memset(linkinfo
, 0, sizeof(linkinfo
));
2883 if (linkinfo
[IFLA_INFO_KIND
]) {
2884 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2885 ops
= rtnl_link_ops_get(kind
);
2892 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2893 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2894 struct nlattr
**data
= NULL
;
2895 struct nlattr
**slave_data
= NULL
;
2896 struct net
*dest_net
, *link_net
= NULL
;
2899 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2900 err
= nla_parse_nested(attr
, ops
->maxtype
,
2901 linkinfo
[IFLA_INFO_DATA
],
2907 if (ops
->validate
) {
2908 err
= ops
->validate(tb
, data
, extack
);
2915 if (m_ops
->slave_maxtype
&&
2916 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2917 err
= nla_parse_nested(slave_attr
,
2918 m_ops
->slave_maxtype
,
2919 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2920 m_ops
->slave_policy
,
2924 slave_data
= slave_attr
;
2931 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2933 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2936 if (linkinfo
[IFLA_INFO_DATA
]) {
2937 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2941 err
= ops
->changelink(dev
, tb
, data
, extack
);
2944 status
|= DO_SETLINK_NOTIFY
;
2947 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2948 if (!m_ops
|| !m_ops
->slave_changelink
)
2951 err
= m_ops
->slave_changelink(master_dev
, dev
,
2956 status
|= DO_SETLINK_NOTIFY
;
2959 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
2963 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2964 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2965 return rtnl_group_changelink(skb
, net
,
2966 nla_get_u32(tb
[IFLA_GROUP
]),
2971 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
2975 #ifdef CONFIG_MODULES
2978 request_module("rtnl-link-%s", kind
);
2980 ops
= rtnl_link_ops_get(kind
);
2992 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2993 name_assign_type
= NET_NAME_ENUM
;
2996 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
2997 if (IS_ERR(dest_net
))
2998 return PTR_ERR(dest_net
);
3000 if (tb
[IFLA_LINK_NETNSID
]) {
3001 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3003 link_net
= get_net_ns_by_id(dest_net
, id
);
3009 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3013 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3014 name_assign_type
, ops
, tb
);
3020 dev
->ifindex
= ifm
->ifi_index
;
3023 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
3025 /* Drivers should call free_netdev() in ->destructor
3026 * and unregister it on failure after registration
3027 * so that device could be finally freed in rtnl_unlock.
3030 /* If device is not registered at all, free it now */
3031 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3036 err
= register_netdevice(dev
);
3042 err
= rtnl_configure_link(dev
, ifm
);
3044 goto out_unregister
;
3046 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3048 goto out_unregister
;
3050 if (tb
[IFLA_MASTER
]) {
3051 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]),
3054 goto out_unregister
;
3063 LIST_HEAD(list_kill
);
3065 ops
->dellink(dev
, &list_kill
);
3066 unregister_netdevice_many(&list_kill
);
3068 unregister_netdevice(dev
);
3074 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3075 struct netlink_ext_ack
*extack
)
3077 struct net
*net
= sock_net(skb
->sk
);
3078 struct net
*tgt_net
= net
;
3079 struct ifinfomsg
*ifm
;
3080 char ifname
[IFNAMSIZ
];
3081 struct nlattr
*tb
[IFLA_MAX
+1];
3082 struct net_device
*dev
= NULL
;
3083 struct sk_buff
*nskb
;
3086 u32 ext_filter_mask
= 0;
3088 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3092 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3096 if (tb
[IFLA_IF_NETNSID
]) {
3097 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
3098 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
3099 if (IS_ERR(tgt_net
))
3100 return PTR_ERR(tgt_net
);
3103 if (tb
[IFLA_IFNAME
])
3104 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3106 if (tb
[IFLA_EXT_MASK
])
3107 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3110 ifm
= nlmsg_data(nlh
);
3111 if (ifm
->ifi_index
> 0)
3112 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3113 else if (tb
[IFLA_IFNAME
])
3114 dev
= __dev_get_by_name(tgt_net
, ifname
);
3123 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3127 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3128 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3129 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3130 0, NULL
, 0, netnsid
);
3132 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3133 WARN_ON(err
== -EMSGSIZE
);
3136 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3144 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3146 struct net
*net
= sock_net(skb
->sk
);
3147 struct net_device
*dev
;
3148 struct nlattr
*tb
[IFLA_MAX
+1];
3149 u32 ext_filter_mask
= 0;
3150 u16 min_ifinfo_dump_size
= 0;
3153 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3154 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3155 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3157 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3158 if (tb
[IFLA_EXT_MASK
])
3159 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3162 if (!ext_filter_mask
)
3163 return NLMSG_GOODSIZE
;
3165 * traverse the list of net devices and compute the minimum
3166 * buffer size based upon the filter mask.
3169 for_each_netdev_rcu(net
, dev
) {
3170 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3176 return nlmsg_total_size(min_ifinfo_dump_size
);
3179 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3182 int s_idx
= cb
->family
;
3187 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3188 struct rtnl_link
**tab
;
3189 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
3190 struct rtnl_link
*link
;
3191 rtnl_dumpit_func dumpit
;
3193 if (idx
< s_idx
|| idx
== PF_PACKET
)
3196 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3199 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3207 dumpit
= link
->dumpit
;
3212 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3216 if (dumpit(skb
, cb
))
3224 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3225 unsigned int change
,
3226 u32 event
, gfp_t flags
, int *new_nsid
,
3229 struct net
*net
= dev_net(dev
);
3230 struct sk_buff
*skb
;
3232 size_t if_info_size
;
3234 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3238 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3239 type
, 0, 0, change
, 0, 0, event
,
3240 new_nsid
, new_ifindex
, -1);
3242 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3243 WARN_ON(err
== -EMSGSIZE
);
3250 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3254 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3256 struct net
*net
= dev_net(dev
);
3258 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3261 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3262 unsigned int change
, u32 event
,
3263 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3265 struct sk_buff
*skb
;
3267 if (dev
->reg_state
!= NETREG_REGISTERED
)
3270 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3273 rtmsg_ifinfo_send(skb
, dev
, flags
);
3276 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3279 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3283 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3284 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3286 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3287 new_nsid
, new_ifindex
);
3290 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3291 struct net_device
*dev
,
3292 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3293 int type
, unsigned int flags
,
3294 int nlflags
, u16 ndm_state
)
3296 struct nlmsghdr
*nlh
;
3299 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3303 ndm
= nlmsg_data(nlh
);
3304 ndm
->ndm_family
= AF_BRIDGE
;
3307 ndm
->ndm_flags
= flags
;
3309 ndm
->ndm_ifindex
= dev
->ifindex
;
3310 ndm
->ndm_state
= ndm_state
;
3312 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3313 goto nla_put_failure
;
3315 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3316 goto nla_put_failure
;
3318 nlmsg_end(skb
, nlh
);
3322 nlmsg_cancel(skb
, nlh
);
3326 static inline size_t rtnl_fdb_nlmsg_size(void)
3328 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3329 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3330 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3334 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3337 struct net
*net
= dev_net(dev
);
3338 struct sk_buff
*skb
;
3341 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3345 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3346 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3352 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3355 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3359 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3361 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3362 struct nlattr
*tb
[],
3363 struct net_device
*dev
,
3364 const unsigned char *addr
, u16 vid
,
3369 /* If aging addresses are supported device will need to
3370 * implement its own handler for this.
3372 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3373 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3378 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3382 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3383 err
= dev_uc_add_excl(dev
, addr
);
3384 else if (is_multicast_ether_addr(addr
))
3385 err
= dev_mc_add_excl(dev
, addr
);
3387 /* Only return duplicate errors if NLM_F_EXCL is set */
3388 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3393 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3395 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3396 struct netlink_ext_ack
*extack
)
3401 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3402 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3406 vid
= nla_get_u16(vlan_attr
);
3408 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3409 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3417 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3418 struct netlink_ext_ack
*extack
)
3420 struct net
*net
= sock_net(skb
->sk
);
3422 struct nlattr
*tb
[NDA_MAX
+1];
3423 struct net_device
*dev
;
3428 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3432 ndm
= nlmsg_data(nlh
);
3433 if (ndm
->ndm_ifindex
== 0) {
3434 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3438 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3440 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3444 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3445 NL_SET_ERR_MSG(extack
, "invalid address");
3449 addr
= nla_data(tb
[NDA_LLADDR
]);
3451 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3457 /* Support fdb on master device the net/bridge default case */
3458 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3459 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3460 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3461 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3463 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3468 ndm
->ndm_flags
&= ~NTF_MASTER
;
3471 /* Embedded bridge, macvlan, and any other device support */
3472 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3473 if (dev
->netdev_ops
->ndo_fdb_add
)
3474 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3478 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3482 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3484 ndm
->ndm_flags
&= ~NTF_SELF
;
3492 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3494 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3495 struct nlattr
*tb
[],
3496 struct net_device
*dev
,
3497 const unsigned char *addr
, u16 vid
)
3501 /* If aging addresses are supported device will need to
3502 * implement its own handler for this.
3504 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3505 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3509 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3510 err
= dev_uc_del(dev
, addr
);
3511 else if (is_multicast_ether_addr(addr
))
3512 err
= dev_mc_del(dev
, addr
);
3516 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3518 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3519 struct netlink_ext_ack
*extack
)
3521 struct net
*net
= sock_net(skb
->sk
);
3523 struct nlattr
*tb
[NDA_MAX
+1];
3524 struct net_device
*dev
;
3529 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3532 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3536 ndm
= nlmsg_data(nlh
);
3537 if (ndm
->ndm_ifindex
== 0) {
3538 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3542 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3544 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3548 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3549 NL_SET_ERR_MSG(extack
, "invalid address");
3553 addr
= nla_data(tb
[NDA_LLADDR
]);
3555 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3561 /* Support fdb on master device the net/bridge default case */
3562 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3563 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3564 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3565 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3567 if (ops
->ndo_fdb_del
)
3568 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3573 ndm
->ndm_flags
&= ~NTF_MASTER
;
3576 /* Embedded bridge, macvlan, and any other device support */
3577 if (ndm
->ndm_flags
& NTF_SELF
) {
3578 if (dev
->netdev_ops
->ndo_fdb_del
)
3579 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3582 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3585 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3587 ndm
->ndm_flags
&= ~NTF_SELF
;
3594 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3595 struct netlink_callback
*cb
,
3596 struct net_device
*dev
,
3598 struct netdev_hw_addr_list
*list
)
3600 struct netdev_hw_addr
*ha
;
3604 portid
= NETLINK_CB(cb
->skb
).portid
;
3605 seq
= cb
->nlh
->nlmsg_seq
;
3607 list_for_each_entry(ha
, &list
->list
, list
) {
3608 if (*idx
< cb
->args
[2])
3611 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3613 RTM_NEWNEIGH
, NTF_SELF
,
3614 NLM_F_MULTI
, NUD_PERMANENT
);
3624 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3625 * @nlh: netlink message header
3628 * Default netdevice operation to dump the existing unicast address list.
3629 * Returns number of addresses from list put in skb.
3631 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3632 struct netlink_callback
*cb
,
3633 struct net_device
*dev
,
3634 struct net_device
*filter_dev
,
3639 netif_addr_lock_bh(dev
);
3640 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3643 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3645 netif_addr_unlock_bh(dev
);
3648 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3650 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3652 struct net_device
*dev
;
3653 struct nlattr
*tb
[IFLA_MAX
+1];
3654 struct net_device
*br_dev
= NULL
;
3655 const struct net_device_ops
*ops
= NULL
;
3656 const struct net_device_ops
*cops
= NULL
;
3657 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3658 struct net
*net
= sock_net(skb
->sk
);
3659 struct hlist_head
*head
;
3667 err
= nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
,
3668 IFLA_MAX
, ifla_policy
, NULL
);
3671 } else if (err
== 0) {
3672 if (tb
[IFLA_MASTER
])
3673 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3676 brport_idx
= ifm
->ifi_index
;
3679 br_dev
= __dev_get_by_index(net
, br_idx
);
3683 ops
= br_dev
->netdev_ops
;
3687 s_idx
= cb
->args
[1];
3689 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3691 head
= &net
->dev_index_head
[h
];
3692 hlist_for_each_entry(dev
, head
, index_hlist
) {
3694 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3697 if (!br_idx
) { /* user did not specify a specific bridge */
3698 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3699 br_dev
= netdev_master_upper_dev_get(dev
);
3700 cops
= br_dev
->netdev_ops
;
3703 if (dev
!= br_dev
&&
3704 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3707 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3708 !(dev
->priv_flags
& IFF_EBRIDGE
))
3716 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3717 if (cops
&& cops
->ndo_fdb_dump
) {
3718 err
= cops
->ndo_fdb_dump(skb
, cb
,
3721 if (err
== -EMSGSIZE
)
3726 if (dev
->netdev_ops
->ndo_fdb_dump
)
3727 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3731 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3733 if (err
== -EMSGSIZE
)
3738 /* reset fdb offset to 0 for rest of the interfaces */
3754 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3755 unsigned int attrnum
, unsigned int flag
)
3758 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3762 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3763 struct net_device
*dev
, u16 mode
,
3764 u32 flags
, u32 mask
, int nlflags
,
3766 int (*vlan_fill
)(struct sk_buff
*skb
,
3767 struct net_device
*dev
,
3770 struct nlmsghdr
*nlh
;
3771 struct ifinfomsg
*ifm
;
3772 struct nlattr
*br_afspec
;
3773 struct nlattr
*protinfo
;
3774 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3775 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3778 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3782 ifm
= nlmsg_data(nlh
);
3783 ifm
->ifi_family
= AF_BRIDGE
;
3785 ifm
->ifi_type
= dev
->type
;
3786 ifm
->ifi_index
= dev
->ifindex
;
3787 ifm
->ifi_flags
= dev_get_flags(dev
);
3788 ifm
->ifi_change
= 0;
3791 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3792 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3793 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3795 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3797 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3798 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3799 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3800 goto nla_put_failure
;
3802 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3804 goto nla_put_failure
;
3806 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3807 nla_nest_cancel(skb
, br_afspec
);
3808 goto nla_put_failure
;
3811 if (mode
!= BRIDGE_MODE_UNDEF
) {
3812 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3813 nla_nest_cancel(skb
, br_afspec
);
3814 goto nla_put_failure
;
3818 err
= vlan_fill(skb
, dev
, filter_mask
);
3820 nla_nest_cancel(skb
, br_afspec
);
3821 goto nla_put_failure
;
3824 nla_nest_end(skb
, br_afspec
);
3826 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3828 goto nla_put_failure
;
3830 if (brport_nla_put_flag(skb
, flags
, mask
,
3831 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3832 brport_nla_put_flag(skb
, flags
, mask
,
3833 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3834 brport_nla_put_flag(skb
, flags
, mask
,
3835 IFLA_BRPORT_FAST_LEAVE
,
3836 BR_MULTICAST_FAST_LEAVE
) ||
3837 brport_nla_put_flag(skb
, flags
, mask
,
3838 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3839 brport_nla_put_flag(skb
, flags
, mask
,
3840 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3841 brport_nla_put_flag(skb
, flags
, mask
,
3842 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3843 brport_nla_put_flag(skb
, flags
, mask
,
3844 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3845 brport_nla_put_flag(skb
, flags
, mask
,
3846 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3847 nla_nest_cancel(skb
, protinfo
);
3848 goto nla_put_failure
;
3851 nla_nest_end(skb
, protinfo
);
3853 nlmsg_end(skb
, nlh
);
3856 nlmsg_cancel(skb
, nlh
);
3857 return err
? err
: -EMSGSIZE
;
3859 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3861 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3863 struct net
*net
= sock_net(skb
->sk
);
3864 struct net_device
*dev
;
3866 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3867 u32 seq
= cb
->nlh
->nlmsg_seq
;
3868 u32 filter_mask
= 0;
3871 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3872 struct nlattr
*extfilt
;
3874 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3877 if (nla_len(extfilt
) < sizeof(filter_mask
))
3880 filter_mask
= nla_get_u32(extfilt
);
3885 for_each_netdev_rcu(net
, dev
) {
3886 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3887 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3889 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3890 if (idx
>= cb
->args
[0]) {
3891 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3892 skb
, portid
, seq
, dev
,
3893 filter_mask
, NLM_F_MULTI
);
3894 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3895 if (likely(skb
->len
))
3904 if (ops
->ndo_bridge_getlink
) {
3905 if (idx
>= cb
->args
[0]) {
3906 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3910 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3911 if (likely(skb
->len
))
3928 static inline size_t bridge_nlmsg_size(void)
3930 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3931 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3932 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3933 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3934 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3935 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3936 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3937 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3938 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3939 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3940 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3943 static int rtnl_bridge_notify(struct net_device
*dev
)
3945 struct net
*net
= dev_net(dev
);
3946 struct sk_buff
*skb
;
3947 int err
= -EOPNOTSUPP
;
3949 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3952 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3958 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3965 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3968 WARN_ON(err
== -EMSGSIZE
);
3971 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3975 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3976 struct netlink_ext_ack
*extack
)
3978 struct net
*net
= sock_net(skb
->sk
);
3979 struct ifinfomsg
*ifm
;
3980 struct net_device
*dev
;
3981 struct nlattr
*br_spec
, *attr
= NULL
;
3982 int rem
, err
= -EOPNOTSUPP
;
3984 bool have_flags
= false;
3986 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3989 ifm
= nlmsg_data(nlh
);
3990 if (ifm
->ifi_family
!= AF_BRIDGE
)
3991 return -EPFNOSUPPORT
;
3993 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3995 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3999 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4001 nla_for_each_nested(attr
, br_spec
, rem
) {
4002 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4003 if (nla_len(attr
) < sizeof(flags
))
4007 flags
= nla_get_u16(attr
);
4013 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4014 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4016 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4021 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
4025 flags
&= ~BRIDGE_FLAGS_MASTER
;
4028 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4029 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4032 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4035 flags
&= ~BRIDGE_FLAGS_SELF
;
4037 /* Generate event to notify upper layer of bridge
4040 err
= rtnl_bridge_notify(dev
);
4045 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4050 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4051 struct netlink_ext_ack
*extack
)
4053 struct net
*net
= sock_net(skb
->sk
);
4054 struct ifinfomsg
*ifm
;
4055 struct net_device
*dev
;
4056 struct nlattr
*br_spec
, *attr
= NULL
;
4057 int rem
, err
= -EOPNOTSUPP
;
4059 bool have_flags
= false;
4061 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4064 ifm
= nlmsg_data(nlh
);
4065 if (ifm
->ifi_family
!= AF_BRIDGE
)
4066 return -EPFNOSUPPORT
;
4068 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4070 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4074 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4076 nla_for_each_nested(attr
, br_spec
, rem
) {
4077 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4078 if (nla_len(attr
) < sizeof(flags
))
4082 flags
= nla_get_u16(attr
);
4088 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4089 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4091 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4096 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4100 flags
&= ~BRIDGE_FLAGS_MASTER
;
4103 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4104 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4107 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4111 flags
&= ~BRIDGE_FLAGS_SELF
;
4113 /* Generate event to notify upper layer of bridge
4116 err
= rtnl_bridge_notify(dev
);
4121 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4126 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4128 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4129 (!idxattr
|| idxattr
== attrid
);
4132 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4133 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4136 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4137 return sizeof(struct rtnl_link_stats64
);
4143 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4146 struct nlattr
*attr
= NULL
;
4151 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4152 dev
->netdev_ops
->ndo_get_offload_stats
))
4155 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4156 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4157 if (attr_id
< *prividx
)
4160 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4164 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4167 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4168 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4170 goto nla_put_failure
;
4172 attr_data
= nla_data(attr
);
4173 memset(attr_data
, 0, size
);
4174 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4177 goto get_offload_stats_failure
;
4188 get_offload_stats_failure
:
4193 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4199 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4200 dev
->netdev_ops
->ndo_get_offload_stats
))
4203 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4204 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4205 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4207 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4208 nla_size
+= nla_total_size_64bit(size
);
4212 nla_size
+= nla_total_size(0);
4217 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4218 int type
, u32 pid
, u32 seq
, u32 change
,
4219 unsigned int flags
, unsigned int filter_mask
,
4220 int *idxattr
, int *prividx
)
4222 struct if_stats_msg
*ifsm
;
4223 struct nlmsghdr
*nlh
;
4224 struct nlattr
*attr
;
4225 int s_prividx
= *prividx
;
4230 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4234 ifsm
= nlmsg_data(nlh
);
4235 ifsm
->family
= PF_UNSPEC
;
4238 ifsm
->ifindex
= dev
->ifindex
;
4239 ifsm
->filter_mask
= filter_mask
;
4241 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4242 struct rtnl_link_stats64
*sp
;
4244 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4245 sizeof(struct rtnl_link_stats64
),
4248 goto nla_put_failure
;
4250 sp
= nla_data(attr
);
4251 dev_get_stats(dev
, sp
);
4254 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4255 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4257 if (ops
&& ops
->fill_linkxstats
) {
4258 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4259 attr
= nla_nest_start(skb
,
4260 IFLA_STATS_LINK_XSTATS
);
4262 goto nla_put_failure
;
4264 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4265 nla_nest_end(skb
, attr
);
4267 goto nla_put_failure
;
4272 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4274 const struct rtnl_link_ops
*ops
= NULL
;
4275 const struct net_device
*master
;
4277 master
= netdev_master_upper_dev_get(dev
);
4279 ops
= master
->rtnl_link_ops
;
4280 if (ops
&& ops
->fill_linkxstats
) {
4281 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4282 attr
= nla_nest_start(skb
,
4283 IFLA_STATS_LINK_XSTATS_SLAVE
);
4285 goto nla_put_failure
;
4287 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4288 nla_nest_end(skb
, attr
);
4290 goto nla_put_failure
;
4295 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4297 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4298 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4300 goto nla_put_failure
;
4302 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4303 if (err
== -ENODATA
)
4304 nla_nest_cancel(skb
, attr
);
4306 nla_nest_end(skb
, attr
);
4308 if (err
&& err
!= -ENODATA
)
4309 goto nla_put_failure
;
4313 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4314 struct rtnl_af_ops
*af_ops
;
4316 *idxattr
= IFLA_STATS_AF_SPEC
;
4317 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
4319 goto nla_put_failure
;
4322 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4323 if (af_ops
->fill_stats_af
) {
4327 af
= nla_nest_start(skb
, af_ops
->family
);
4330 goto nla_put_failure
;
4332 err
= af_ops
->fill_stats_af(skb
, dev
);
4334 if (err
== -ENODATA
) {
4335 nla_nest_cancel(skb
, af
);
4336 } else if (err
< 0) {
4338 goto nla_put_failure
;
4341 nla_nest_end(skb
, af
);
4346 nla_nest_end(skb
, attr
);
4351 nlmsg_end(skb
, nlh
);
4356 /* not a multi message or no progress mean a real error */
4357 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4358 nlmsg_cancel(skb
, nlh
);
4360 nlmsg_end(skb
, nlh
);
4365 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4370 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4371 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4373 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4374 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4375 int attr
= IFLA_STATS_LINK_XSTATS
;
4377 if (ops
&& ops
->get_linkxstats_size
) {
4378 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4380 /* for IFLA_STATS_LINK_XSTATS */
4381 size
+= nla_total_size(0);
4385 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4386 struct net_device
*_dev
= (struct net_device
*)dev
;
4387 const struct rtnl_link_ops
*ops
= NULL
;
4388 const struct net_device
*master
;
4390 /* netdev_master_upper_dev_get can't take const */
4391 master
= netdev_master_upper_dev_get(_dev
);
4393 ops
= master
->rtnl_link_ops
;
4394 if (ops
&& ops
->get_linkxstats_size
) {
4395 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4397 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4399 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4400 size
+= nla_total_size(0);
4404 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4405 size
+= rtnl_get_offload_stats_size(dev
);
4407 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4408 struct rtnl_af_ops
*af_ops
;
4410 /* for IFLA_STATS_AF_SPEC */
4411 size
+= nla_total_size(0);
4414 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4415 if (af_ops
->get_stats_af_size
) {
4416 size
+= nla_total_size(
4417 af_ops
->get_stats_af_size(dev
));
4420 size
+= nla_total_size(0);
4429 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4430 struct netlink_ext_ack
*extack
)
4432 struct net
*net
= sock_net(skb
->sk
);
4433 struct net_device
*dev
= NULL
;
4434 int idxattr
= 0, prividx
= 0;
4435 struct if_stats_msg
*ifsm
;
4436 struct sk_buff
*nskb
;
4440 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4443 ifsm
= nlmsg_data(nlh
);
4444 if (ifsm
->ifindex
> 0)
4445 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4452 filter_mask
= ifsm
->filter_mask
;
4456 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4460 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4461 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4462 0, filter_mask
, &idxattr
, &prividx
);
4464 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4465 WARN_ON(err
== -EMSGSIZE
);
4468 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4474 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4476 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4477 struct net
*net
= sock_net(skb
->sk
);
4478 unsigned int flags
= NLM_F_MULTI
;
4479 struct if_stats_msg
*ifsm
;
4480 struct hlist_head
*head
;
4481 struct net_device
*dev
;
4482 u32 filter_mask
= 0;
4486 s_idx
= cb
->args
[1];
4487 s_idxattr
= cb
->args
[2];
4488 s_prividx
= cb
->args
[3];
4490 cb
->seq
= net
->dev_base_seq
;
4492 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
))
4495 ifsm
= nlmsg_data(cb
->nlh
);
4496 filter_mask
= ifsm
->filter_mask
;
4500 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4502 head
= &net
->dev_index_head
[h
];
4503 hlist_for_each_entry(dev
, head
, index_hlist
) {
4506 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4507 NETLINK_CB(cb
->skb
).portid
,
4508 cb
->nlh
->nlmsg_seq
, 0,
4510 &s_idxattr
, &s_prividx
);
4511 /* If we ran out of room on the first message,
4514 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4520 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4526 cb
->args
[3] = s_prividx
;
4527 cb
->args
[2] = s_idxattr
;
4534 /* Process one rtnetlink message. */
4536 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4537 struct netlink_ext_ack
*extack
)
4539 struct net
*net
= sock_net(skb
->sk
);
4540 struct rtnl_link
*link
;
4541 struct module
*owner
;
4542 int err
= -EOPNOTSUPP
;
4543 rtnl_doit_func doit
;
4549 type
= nlh
->nlmsg_type
;
4555 /* All the messages must have at least 1 byte length */
4556 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4559 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4562 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4566 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4568 rtnl_dumpit_func dumpit
;
4569 u16 min_dump_alloc
= 0;
4571 link
= rtnl_get_link(family
, type
);
4572 if (!link
|| !link
->dumpit
) {
4574 link
= rtnl_get_link(family
, type
);
4575 if (!link
|| !link
->dumpit
)
4578 owner
= link
->owner
;
4579 dumpit
= link
->dumpit
;
4581 if (type
== RTM_GETLINK
- RTM_BASE
)
4582 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4585 /* need to do this before rcu_read_unlock() */
4586 if (!try_module_get(owner
))
4587 err
= -EPROTONOSUPPORT
;
4593 struct netlink_dump_control c
= {
4595 .min_dump_alloc
= min_dump_alloc
,
4598 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4599 /* netlink_dump_start() will keep a reference on
4600 * module if dump is still in progress.
4607 link
= rtnl_get_link(family
, type
);
4608 if (!link
|| !link
->doit
) {
4610 link
= rtnl_get_link(PF_UNSPEC
, type
);
4611 if (!link
|| !link
->doit
)
4615 owner
= link
->owner
;
4616 if (!try_module_get(owner
)) {
4617 err
= -EPROTONOSUPPORT
;
4621 flags
= link
->flags
;
4622 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4626 err
= doit(skb
, nlh
, extack
);
4633 link
= rtnl_get_link(family
, type
);
4634 if (link
&& link
->doit
)
4635 err
= link
->doit(skb
, nlh
, extack
);
4651 static void rtnetlink_rcv(struct sk_buff
*skb
)
4653 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4656 static int rtnetlink_bind(struct net
*net
, int group
)
4659 case RTNLGRP_IPV4_MROUTE_R
:
4660 case RTNLGRP_IPV6_MROUTE_R
:
4661 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4668 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4670 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4674 case NETDEV_CHANGEMTU
:
4675 case NETDEV_CHANGEADDR
:
4676 case NETDEV_CHANGENAME
:
4677 case NETDEV_FEAT_CHANGE
:
4678 case NETDEV_BONDING_FAILOVER
:
4679 case NETDEV_POST_TYPE_CHANGE
:
4680 case NETDEV_NOTIFY_PEERS
:
4681 case NETDEV_CHANGEUPPER
:
4682 case NETDEV_RESEND_IGMP
:
4683 case NETDEV_CHANGEINFODATA
:
4684 case NETDEV_CHANGELOWERSTATE
:
4685 case NETDEV_CHANGE_TX_QUEUE_LEN
:
4686 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
4687 GFP_KERNEL
, NULL
, 0);
4695 static struct notifier_block rtnetlink_dev_notifier
= {
4696 .notifier_call
= rtnetlink_event
,
4700 static int __net_init
rtnetlink_net_init(struct net
*net
)
4703 struct netlink_kernel_cfg cfg
= {
4704 .groups
= RTNLGRP_MAX
,
4705 .input
= rtnetlink_rcv
,
4706 .cb_mutex
= &rtnl_mutex
,
4707 .flags
= NL_CFG_F_NONROOT_RECV
,
4708 .bind
= rtnetlink_bind
,
4711 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
4718 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
4720 netlink_kernel_release(net
->rtnl
);
4724 static struct pernet_operations rtnetlink_net_ops
= {
4725 .init
= rtnetlink_net_init
,
4726 .exit
= rtnetlink_net_exit
,
4729 void __init
rtnetlink_init(void)
4731 if (register_pernet_subsys(&rtnetlink_net_ops
))
4732 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4734 register_netdevice_notifier(&rtnetlink_dev_notifier
);
4736 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
4737 rtnl_dump_ifinfo
, 0);
4738 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
4739 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
4740 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
4742 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
4743 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
4744 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
4746 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
4747 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
4748 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
4750 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
4751 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
4752 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
4754 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,