1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
62 rtnl_dumpit_func dumpit
;
68 static DEFINE_MUTEX(rtnl_mutex
);
72 mutex_lock(&rtnl_mutex
);
74 EXPORT_SYMBOL(rtnl_lock
);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex
);
80 EXPORT_SYMBOL(rtnl_lock_killable
);
82 static struct sk_buff
*defer_kfree_skb_list
;
83 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
86 tail
->next
= defer_kfree_skb_list
;
87 defer_kfree_skb_list
= head
;
90 EXPORT_SYMBOL(rtnl_kfree_skbs
);
92 void __rtnl_unlock(void)
94 struct sk_buff
*head
= defer_kfree_skb_list
;
96 defer_kfree_skb_list
= NULL
;
98 mutex_unlock(&rtnl_mutex
);
101 struct sk_buff
*next
= head
->next
;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
114 EXPORT_SYMBOL(rtnl_unlock
);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex
);
120 EXPORT_SYMBOL(rtnl_trylock
);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex
);
126 EXPORT_SYMBOL(rtnl_is_locked
);
128 bool refcount_dec_and_rtnl_lock(refcount_t
*r
)
130 return refcount_dec_and_mutex_lock(r
, &rtnl_mutex
);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock
);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex
);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
144 static inline int rtm_msgindex(int msgtype
)
146 int msgindex
= msgtype
- RTM_BASE
;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
158 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
160 struct rtnl_link
**tab
;
162 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
163 protocol
= PF_UNSPEC
;
165 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
167 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
172 static int rtnl_register_internal(struct module
*owner
,
173 int protocol
, int msgtype
,
174 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
177 struct rtnl_link
*link
, *old
;
178 struct rtnl_link __rcu
**tab
;
182 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
183 msgindex
= rtm_msgindex(msgtype
);
186 tab
= rtnl_msg_handlers
[protocol
];
188 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
196 old
= rtnl_dereference(tab
[msgindex
]);
198 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
202 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
207 WARN_ON(link
->owner
&& link
->owner
!= owner
);
210 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
213 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
215 link
->dumpit
= dumpit
;
217 link
->flags
|= flags
;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab
[msgindex
], link
);
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module
*owner
,
242 int protocol
, int msgtype
,
243 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
246 return rtnl_register_internal(owner
, protocol
, msgtype
,
247 doit
, dumpit
, flags
);
249 EXPORT_SYMBOL_GPL(rtnl_register_module
);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
267 void rtnl_register(int protocol
, int msgtype
,
268 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
273 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol
, msgtype
);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol
, int msgtype
)
289 struct rtnl_link
**tab
, *link
;
292 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
293 msgindex
= rtm_msgindex(msgtype
);
296 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
302 link
= tab
[msgindex
];
303 rcu_assign_pointer(tab
[msgindex
], NULL
);
306 kfree_rcu(link
, rcu
);
310 EXPORT_SYMBOL_GPL(rtnl_unregister
);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol
)
321 struct rtnl_link
**tab
, *link
;
324 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
327 tab
= rtnl_msg_handlers
[protocol
];
332 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
333 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
334 link
= tab
[msgindex
];
338 rcu_assign_pointer(tab
[msgindex
], NULL
);
339 kfree_rcu(link
, rcu
);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
349 static LIST_HEAD(link_ops
);
351 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
353 const struct rtnl_link_ops
*ops
;
355 list_for_each_entry(ops
, &link_ops
, list
) {
356 if (!strcmp(ops
->kind
, kind
))
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
374 if (rtnl_link_ops_get(ops
->kind
))
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops
->setup
&& !ops
->dellink
)
383 ops
->dellink
= unregister_netdevice_queue
;
385 list_add_tail(&ops
->list
, &link_ops
);
388 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops
*ops
)
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
402 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
406 err
= __rtnl_link_register(ops
);
410 EXPORT_SYMBOL_GPL(rtnl_link_register
);
412 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
414 struct net_device
*dev
;
415 LIST_HEAD(list_kill
);
417 for_each_netdev(net
, dev
) {
418 if (dev
->rtnl_link_ops
== ops
)
419 ops
->dellink(dev
, &list_kill
);
421 unregister_netdevice_many(&list_kill
);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
437 __rtnl_kill_links(net
, ops
);
439 list_del(&ops
->list
);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
450 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
452 add_wait_queue(&netdev_unregistering_wq
, &wait
);
454 unregistering
= false;
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
460 if (net
->dev_unreg_count
> 0) {
461 unregistering
= true;
469 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
471 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem
);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops
);
485 up_write(&pernet_ops_rwsem
);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
491 struct net_device
*master_dev
;
492 const struct rtnl_link_ops
*ops
;
497 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
501 ops
= master_dev
->rtnl_link_ops
;
502 if (!ops
|| !ops
->get_slave_size
)
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size
= nla_total_size(sizeof(struct nlattr
)) +
506 ops
->get_slave_size(master_dev
, dev
);
513 static size_t rtnl_link_get_size(const struct net_device
*dev
)
515 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
521 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
525 /* IFLA_INFO_DATA + nested data */
526 size
+= nla_total_size(sizeof(struct nlattr
)) +
529 if (ops
->get_xstats_size
)
530 /* IFLA_INFO_XSTATS */
531 size
+= nla_total_size(ops
->get_xstats_size(dev
));
533 size
+= rtnl_link_get_slave_info_data_size(dev
);
538 static LIST_HEAD(rtnl_af_ops
);
540 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
542 const struct rtnl_af_ops
*ops
;
544 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
545 if (ops
->family
== family
)
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops
*ops
)
561 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
564 EXPORT_SYMBOL_GPL(rtnl_af_register
);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
573 list_del_rcu(&ops
->list
);
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
580 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
583 struct rtnl_af_ops
*af_ops
;
587 size
= nla_total_size(sizeof(struct nlattr
));
590 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
591 if (af_ops
->get_link_af_size
) {
592 /* AF_* + nested data */
593 size
+= nla_total_size(sizeof(struct nlattr
)) +
594 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
602 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
604 struct net_device
*master_dev
;
609 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
610 if (master_dev
&& master_dev
->rtnl_link_ops
)
616 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
617 const struct net_device
*dev
)
619 struct net_device
*master_dev
;
620 const struct rtnl_link_ops
*ops
;
621 struct nlattr
*slave_data
;
624 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
627 ops
= master_dev
->rtnl_link_ops
;
630 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
632 if (ops
->fill_slave_info
) {
633 slave_data
= nla_nest_start_noflag(skb
, IFLA_INFO_SLAVE_DATA
);
636 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
638 goto err_cancel_slave_data
;
639 nla_nest_end(skb
, slave_data
);
643 err_cancel_slave_data
:
644 nla_nest_cancel(skb
, slave_data
);
648 static int rtnl_link_info_fill(struct sk_buff
*skb
,
649 const struct net_device
*dev
)
651 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
657 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
659 if (ops
->fill_xstats
) {
660 err
= ops
->fill_xstats(skb
, dev
);
664 if (ops
->fill_info
) {
665 data
= nla_nest_start_noflag(skb
, IFLA_INFO_DATA
);
668 err
= ops
->fill_info(skb
, dev
);
670 goto err_cancel_data
;
671 nla_nest_end(skb
, data
);
676 nla_nest_cancel(skb
, data
);
680 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
682 struct nlattr
*linkinfo
;
685 linkinfo
= nla_nest_start_noflag(skb
, IFLA_LINKINFO
);
686 if (linkinfo
== NULL
)
689 err
= rtnl_link_info_fill(skb
, dev
);
691 goto err_cancel_link
;
693 err
= rtnl_link_slave_info_fill(skb
, dev
);
695 goto err_cancel_link
;
697 nla_nest_end(skb
, linkinfo
);
701 nla_nest_cancel(skb
, linkinfo
);
706 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
708 struct sock
*rtnl
= net
->rtnl
;
711 NETLINK_CB(skb
).dst_group
= group
;
713 refcount_inc(&skb
->users
);
714 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
716 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
720 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
722 struct sock
*rtnl
= net
->rtnl
;
724 return nlmsg_unicast(rtnl
, skb
, pid
);
726 EXPORT_SYMBOL(rtnl_unicast
);
728 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
729 struct nlmsghdr
*nlh
, gfp_t flags
)
731 struct sock
*rtnl
= net
->rtnl
;
735 report
= nlmsg_report(nlh
);
737 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
739 EXPORT_SYMBOL(rtnl_notify
);
741 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
743 struct sock
*rtnl
= net
->rtnl
;
745 netlink_set_err(rtnl
, 0, group
, error
);
747 EXPORT_SYMBOL(rtnl_set_sk_err
);
749 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
754 /* nothing is dumped for dst_default_metrics, so just skip the loop */
755 if (metrics
== dst_default_metrics
.metrics
)
758 mx
= nla_nest_start_noflag(skb
, RTA_METRICS
);
762 for (i
= 0; i
< RTAX_MAX
; i
++) {
764 if (i
== RTAX_CC_ALGO
- 1) {
765 char tmp
[TCP_CA_NAME_MAX
], *name
;
767 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
770 if (nla_put_string(skb
, i
+ 1, name
))
771 goto nla_put_failure
;
772 } else if (i
== RTAX_FEATURES
- 1) {
773 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
777 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
778 if (nla_put_u32(skb
, i
+ 1, user_features
))
779 goto nla_put_failure
;
781 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
782 goto nla_put_failure
;
789 nla_nest_cancel(skb
, mx
);
793 return nla_nest_end(skb
, mx
);
796 nla_nest_cancel(skb
, mx
);
799 EXPORT_SYMBOL(rtnetlink_put_metrics
);
801 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
802 long expires
, u32 error
)
804 struct rta_cacheinfo ci
= {
810 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
811 ci
.rta_used
= dst
->__use
;
812 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
817 clock
= jiffies_to_clock_t(abs(expires
));
818 clock
= min_t(unsigned long, clock
, INT_MAX
);
819 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
821 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
823 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
825 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
827 unsigned char operstate
= dev
->operstate
;
829 switch (transition
) {
831 if ((operstate
== IF_OPER_DORMANT
||
832 operstate
== IF_OPER_UNKNOWN
) &&
834 operstate
= IF_OPER_UP
;
837 case IF_OPER_DORMANT
:
838 if (operstate
== IF_OPER_UP
||
839 operstate
== IF_OPER_UNKNOWN
)
840 operstate
= IF_OPER_DORMANT
;
844 if (dev
->operstate
!= operstate
) {
845 write_lock_bh(&dev_base_lock
);
846 dev
->operstate
= operstate
;
847 write_unlock_bh(&dev_base_lock
);
848 netdev_state_change(dev
);
852 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
854 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
855 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
858 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
859 const struct ifinfomsg
*ifm
)
861 unsigned int flags
= ifm
->ifi_flags
;
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 flags
= (flags
& ifm
->ifi_change
) |
866 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
871 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
872 const struct rtnl_link_stats64
*b
)
874 a
->rx_packets
= b
->rx_packets
;
875 a
->tx_packets
= b
->tx_packets
;
876 a
->rx_bytes
= b
->rx_bytes
;
877 a
->tx_bytes
= b
->tx_bytes
;
878 a
->rx_errors
= b
->rx_errors
;
879 a
->tx_errors
= b
->tx_errors
;
880 a
->rx_dropped
= b
->rx_dropped
;
881 a
->tx_dropped
= b
->tx_dropped
;
883 a
->multicast
= b
->multicast
;
884 a
->collisions
= b
->collisions
;
886 a
->rx_length_errors
= b
->rx_length_errors
;
887 a
->rx_over_errors
= b
->rx_over_errors
;
888 a
->rx_crc_errors
= b
->rx_crc_errors
;
889 a
->rx_frame_errors
= b
->rx_frame_errors
;
890 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
891 a
->rx_missed_errors
= b
->rx_missed_errors
;
893 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
894 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
895 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
896 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
897 a
->tx_window_errors
= b
->tx_window_errors
;
899 a
->rx_compressed
= b
->rx_compressed
;
900 a
->tx_compressed
= b
->tx_compressed
;
902 a
->rx_nohandler
= b
->rx_nohandler
;
906 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
909 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
910 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
911 size_t size
= nla_total_size(0);
914 nla_total_size(sizeof(struct ifla_vf_mac
)) +
915 nla_total_size(sizeof(struct ifla_vf_broadcast
)) +
916 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
918 nla_total_size(MAX_VLAN_LIST_LEN
*
919 sizeof(struct ifla_vf_vlan_info
)) +
920 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
921 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
922 nla_total_size(sizeof(struct ifla_vf_rate
)) +
923 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
924 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
925 nla_total_size(0) + /* nest IFLA_VF_STATS */
926 /* IFLA_VF_STATS_RX_PACKETS */
927 nla_total_size_64bit(sizeof(__u64
)) +
928 /* IFLA_VF_STATS_TX_PACKETS */
929 nla_total_size_64bit(sizeof(__u64
)) +
930 /* IFLA_VF_STATS_RX_BYTES */
931 nla_total_size_64bit(sizeof(__u64
)) +
932 /* IFLA_VF_STATS_TX_BYTES */
933 nla_total_size_64bit(sizeof(__u64
)) +
934 /* IFLA_VF_STATS_BROADCAST */
935 nla_total_size_64bit(sizeof(__u64
)) +
936 /* IFLA_VF_STATS_MULTICAST */
937 nla_total_size_64bit(sizeof(__u64
)) +
938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64
)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64
)) +
942 nla_total_size(sizeof(struct ifla_vf_trust
)));
948 static size_t rtnl_port_size(const struct net_device
*dev
,
951 size_t port_size
= nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
953 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
958 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
960 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
963 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
964 !(ext_filter_mask
& RTEXT_FILTER_VF
))
966 if (dev_num_vf(dev
->dev
.parent
))
967 return port_self_size
+ vf_ports_size
+
968 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
970 return port_self_size
;
973 static size_t rtnl_xdp_size(void)
975 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
976 nla_total_size(1) + /* XDP_ATTACHED */
977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
983 static size_t rtnl_prop_list_size(const struct net_device
*dev
)
985 struct netdev_name_node
*name_node
;
988 if (list_empty(&dev
->name_node
->list
))
990 size
= nla_total_size(0);
991 list_for_each_entry(name_node
, &dev
->name_node
->list
, list
)
992 size
+= nla_total_size(ALTIFNAMSIZ
);
996 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
999 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
1000 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
1001 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
1002 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
1003 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
1004 + nla_total_size(sizeof(struct rtnl_link_stats
))
1005 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
1006 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
1007 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
1008 + nla_total_size(4) /* IFLA_TXQLEN */
1009 + nla_total_size(4) /* IFLA_WEIGHT */
1010 + nla_total_size(4) /* IFLA_MTU */
1011 + nla_total_size(4) /* IFLA_LINK */
1012 + nla_total_size(4) /* IFLA_MASTER */
1013 + nla_total_size(1) /* IFLA_CARRIER */
1014 + nla_total_size(4) /* IFLA_PROMISCUITY */
1015 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1016 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1017 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1018 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1019 + nla_total_size(1) /* IFLA_OPERSTATE */
1020 + nla_total_size(1) /* IFLA_LINKMODE */
1021 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1022 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1023 + nla_total_size(4) /* IFLA_GROUP */
1024 + nla_total_size(ext_filter_mask
1025 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1026 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1027 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1028 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1029 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1030 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1031 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1032 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1033 + rtnl_xdp_size() /* IFLA_XDP */
1034 + nla_total_size(4) /* IFLA_EVENT */
1035 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1036 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1037 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1038 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1039 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1040 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1041 + nla_total_size(4) /* IFLA_MIN_MTU */
1042 + nla_total_size(4) /* IFLA_MAX_MTU */
1043 + rtnl_prop_list_size(dev
)
1047 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1049 struct nlattr
*vf_ports
;
1050 struct nlattr
*vf_port
;
1054 vf_ports
= nla_nest_start_noflag(skb
, IFLA_VF_PORTS
);
1058 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1059 vf_port
= nla_nest_start_noflag(skb
, IFLA_VF_PORT
);
1061 goto nla_put_failure
;
1062 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1063 goto nla_put_failure
;
1064 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1065 if (err
== -EMSGSIZE
)
1066 goto nla_put_failure
;
1068 nla_nest_cancel(skb
, vf_port
);
1071 nla_nest_end(skb
, vf_port
);
1074 nla_nest_end(skb
, vf_ports
);
1079 nla_nest_cancel(skb
, vf_ports
);
1083 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1085 struct nlattr
*port_self
;
1088 port_self
= nla_nest_start_noflag(skb
, IFLA_PORT_SELF
);
1092 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1094 nla_nest_cancel(skb
, port_self
);
1095 return (err
== -EMSGSIZE
) ? err
: 0;
1098 nla_nest_end(skb
, port_self
);
1103 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1104 u32 ext_filter_mask
)
1108 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1109 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1112 err
= rtnl_port_self_fill(skb
, dev
);
1116 if (dev_num_vf(dev
->dev
.parent
)) {
1117 err
= rtnl_vf_ports_fill(skb
, dev
);
1125 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1128 struct netdev_phys_item_id ppid
;
1130 err
= dev_get_phys_port_id(dev
, &ppid
);
1132 if (err
== -EOPNOTSUPP
)
1137 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1143 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1145 char name
[IFNAMSIZ
];
1148 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1150 if (err
== -EOPNOTSUPP
)
1155 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1161 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1163 struct netdev_phys_item_id ppid
= { };
1166 err
= dev_get_port_parent_id(dev
, &ppid
, false);
1168 if (err
== -EOPNOTSUPP
)
1173 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, ppid
.id_len
, ppid
.id
))
1179 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1180 struct net_device
*dev
)
1182 struct rtnl_link_stats64
*sp
;
1183 struct nlattr
*attr
;
1185 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1186 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1190 sp
= nla_data(attr
);
1191 dev_get_stats(dev
, sp
);
1193 attr
= nla_reserve(skb
, IFLA_STATS
,
1194 sizeof(struct rtnl_link_stats
));
1198 copy_rtnl_link_stats(nla_data(attr
), sp
);
1203 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1204 struct net_device
*dev
,
1206 struct nlattr
*vfinfo
)
1208 struct ifla_vf_rss_query_en vf_rss_query_en
;
1209 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1210 struct ifla_vf_link_state vf_linkstate
;
1211 struct ifla_vf_vlan_info vf_vlan_info
;
1212 struct ifla_vf_spoofchk vf_spoofchk
;
1213 struct ifla_vf_tx_rate vf_tx_rate
;
1214 struct ifla_vf_stats vf_stats
;
1215 struct ifla_vf_trust vf_trust
;
1216 struct ifla_vf_vlan vf_vlan
;
1217 struct ifla_vf_rate vf_rate
;
1218 struct ifla_vf_mac vf_mac
;
1219 struct ifla_vf_broadcast vf_broadcast
;
1220 struct ifla_vf_info ivi
;
1221 struct ifla_vf_guid node_guid
;
1222 struct ifla_vf_guid port_guid
;
1224 memset(&ivi
, 0, sizeof(ivi
));
1226 /* Not all SR-IOV capable drivers support the
1227 * spoofcheck and "RSS query enable" query. Preset to
1228 * -1 so the user space tool can detect that the driver
1229 * didn't report anything.
1232 ivi
.rss_query_en
= -1;
1234 /* The default value for VF link state is "auto"
1235 * IFLA_VF_LINK_STATE_AUTO which equals zero
1238 /* VLAN Protocol by default is 802.1Q */
1239 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1240 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1243 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1252 vf_rss_query_en
.vf
=
1253 vf_trust
.vf
= ivi
.vf
;
1255 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1256 memcpy(vf_broadcast
.broadcast
, dev
->broadcast
, dev
->addr_len
);
1257 vf_vlan
.vlan
= ivi
.vlan
;
1258 vf_vlan
.qos
= ivi
.qos
;
1259 vf_vlan_info
.vlan
= ivi
.vlan
;
1260 vf_vlan_info
.qos
= ivi
.qos
;
1261 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1262 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1263 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1264 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1265 vf_spoofchk
.setting
= ivi
.spoofchk
;
1266 vf_linkstate
.link_state
= ivi
.linkstate
;
1267 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1268 vf_trust
.setting
= ivi
.trusted
;
1269 vf
= nla_nest_start_noflag(skb
, IFLA_VF_INFO
);
1271 goto nla_put_vfinfo_failure
;
1272 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1273 nla_put(skb
, IFLA_VF_BROADCAST
, sizeof(vf_broadcast
), &vf_broadcast
) ||
1274 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1275 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1277 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1279 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1281 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1283 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1284 sizeof(vf_rss_query_en
),
1285 &vf_rss_query_en
) ||
1286 nla_put(skb
, IFLA_VF_TRUST
,
1287 sizeof(vf_trust
), &vf_trust
))
1288 goto nla_put_vf_failure
;
1290 memset(&node_guid
, 0, sizeof(node_guid
));
1291 memset(&port_guid
, 0, sizeof(port_guid
));
1292 if (dev
->netdev_ops
->ndo_get_vf_guid
&&
1293 !dev
->netdev_ops
->ndo_get_vf_guid(dev
, vfs_num
, &node_guid
,
1295 if (nla_put(skb
, IFLA_VF_IB_NODE_GUID
, sizeof(node_guid
),
1297 nla_put(skb
, IFLA_VF_IB_PORT_GUID
, sizeof(port_guid
),
1299 goto nla_put_vf_failure
;
1301 vfvlanlist
= nla_nest_start_noflag(skb
, IFLA_VF_VLAN_LIST
);
1303 goto nla_put_vf_failure
;
1304 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1306 nla_nest_cancel(skb
, vfvlanlist
);
1307 goto nla_put_vf_failure
;
1309 nla_nest_end(skb
, vfvlanlist
);
1310 memset(&vf_stats
, 0, sizeof(vf_stats
));
1311 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1312 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1314 vfstats
= nla_nest_start_noflag(skb
, IFLA_VF_STATS
);
1316 goto nla_put_vf_failure
;
1317 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1318 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1319 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1320 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1321 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1322 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1323 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1324 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1325 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1326 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1327 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1328 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1329 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1330 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1331 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1332 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1333 nla_nest_cancel(skb
, vfstats
);
1334 goto nla_put_vf_failure
;
1336 nla_nest_end(skb
, vfstats
);
1337 nla_nest_end(skb
, vf
);
1341 nla_nest_cancel(skb
, vf
);
1342 nla_put_vfinfo_failure
:
1343 nla_nest_cancel(skb
, vfinfo
);
1347 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1348 struct net_device
*dev
,
1349 u32 ext_filter_mask
)
1351 struct nlattr
*vfinfo
;
1354 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1357 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1358 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1361 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1364 vfinfo
= nla_nest_start_noflag(skb
, IFLA_VFINFO_LIST
);
1368 for (i
= 0; i
< num_vfs
; i
++) {
1369 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1373 nla_nest_end(skb
, vfinfo
);
1377 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1379 struct rtnl_link_ifmap map
;
1381 memset(&map
, 0, sizeof(map
));
1382 map
.mem_start
= dev
->mem_start
;
1383 map
.mem_end
= dev
->mem_end
;
1384 map
.base_addr
= dev
->base_addr
;
1387 map
.port
= dev
->if_port
;
1389 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1395 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1397 const struct bpf_prog
*generic_xdp_prog
;
1401 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1402 if (!generic_xdp_prog
)
1404 return generic_xdp_prog
->aux
->id
;
1407 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1409 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
, XDP_QUERY_PROG
);
1412 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1414 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
,
1418 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1419 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1420 u32 (*get_prog_id
)(struct net_device
*dev
))
1425 curr_id
= get_prog_id(dev
);
1430 err
= nla_put_u32(skb
, attr
, curr_id
);
1434 if (*mode
!= XDP_ATTACHED_NONE
)
1435 *mode
= XDP_ATTACHED_MULTI
;
1442 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1449 xdp
= nla_nest_start_noflag(skb
, IFLA_XDP
);
1454 mode
= XDP_ATTACHED_NONE
;
1455 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1456 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1459 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1460 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1463 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1464 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1468 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1472 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1473 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1478 nla_nest_end(skb
, xdp
);
1482 nla_nest_cancel(skb
, xdp
);
1486 static u32
rtnl_get_event(unsigned long event
)
1488 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1492 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1494 case NETDEV_FEAT_CHANGE
:
1495 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1497 case NETDEV_BONDING_FAILOVER
:
1498 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1500 case NETDEV_NOTIFY_PEERS
:
1501 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1503 case NETDEV_RESEND_IGMP
:
1504 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1506 case NETDEV_CHANGEINFODATA
:
1507 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1513 return rtnl_event_type
;
1516 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1518 const struct net_device
*upper_dev
;
1523 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1525 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1531 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
,
1534 int ifindex
= dev_get_iflink(dev
);
1536 if (force
|| dev
->ifindex
!= ifindex
)
1537 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1542 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1543 struct net_device
*dev
)
1548 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1549 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1552 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1553 const struct net_device
*dev
,
1554 struct net
*src_net
, gfp_t gfp
)
1556 bool put_iflink
= false;
1558 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1559 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1561 if (!net_eq(dev_net(dev
), link_net
)) {
1562 int id
= peernet2id_alloc(src_net
, link_net
, gfp
);
1564 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1571 return nla_put_iflink(skb
, dev
, put_iflink
);
1574 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1575 const struct net_device
*dev
,
1576 u32 ext_filter_mask
)
1578 const struct rtnl_af_ops
*af_ops
;
1579 struct nlattr
*af_spec
;
1581 af_spec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
1585 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1589 if (!af_ops
->fill_link_af
)
1592 af
= nla_nest_start_noflag(skb
, af_ops
->family
);
1596 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1598 * Caller may return ENODATA to indicate that there
1599 * was no data to be dumped. This is not an error, it
1600 * means we should trim the attribute header and
1603 if (err
== -ENODATA
)
1604 nla_nest_cancel(skb
, af
);
1608 nla_nest_end(skb
, af
);
1611 nla_nest_end(skb
, af_spec
);
1615 static int rtnl_fill_alt_ifnames(struct sk_buff
*skb
,
1616 const struct net_device
*dev
)
1618 struct netdev_name_node
*name_node
;
1621 list_for_each_entry(name_node
, &dev
->name_node
->list
, list
) {
1622 if (nla_put_string(skb
, IFLA_ALT_IFNAME
, name_node
->name
))
1629 static int rtnl_fill_prop_list(struct sk_buff
*skb
,
1630 const struct net_device
*dev
)
1632 struct nlattr
*prop_list
;
1635 prop_list
= nla_nest_start(skb
, IFLA_PROP_LIST
);
1639 ret
= rtnl_fill_alt_ifnames(skb
, dev
);
1643 nla_nest_end(skb
, prop_list
);
1647 nla_nest_cancel(skb
, prop_list
);
1651 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1652 struct net_device
*dev
, struct net
*src_net
,
1653 int type
, u32 pid
, u32 seq
, u32 change
,
1654 unsigned int flags
, u32 ext_filter_mask
,
1655 u32 event
, int *new_nsid
, int new_ifindex
,
1656 int tgt_netnsid
, gfp_t gfp
)
1658 struct ifinfomsg
*ifm
;
1659 struct nlmsghdr
*nlh
;
1662 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1666 ifm
= nlmsg_data(nlh
);
1667 ifm
->ifi_family
= AF_UNSPEC
;
1669 ifm
->ifi_type
= dev
->type
;
1670 ifm
->ifi_index
= dev
->ifindex
;
1671 ifm
->ifi_flags
= dev_get_flags(dev
);
1672 ifm
->ifi_change
= change
;
1674 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_TARGET_NETNSID
, tgt_netnsid
))
1675 goto nla_put_failure
;
1677 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1678 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1679 nla_put_u8(skb
, IFLA_OPERSTATE
,
1680 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1681 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1682 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1683 nla_put_u32(skb
, IFLA_MIN_MTU
, dev
->min_mtu
) ||
1684 nla_put_u32(skb
, IFLA_MAX_MTU
, dev
->max_mtu
) ||
1685 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1686 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1687 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1688 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1689 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1691 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1693 put_master_ifindex(skb
, dev
) ||
1694 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1696 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1697 nla_put_ifalias(skb
, dev
) ||
1698 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1699 atomic_read(&dev
->carrier_up_count
) +
1700 atomic_read(&dev
->carrier_down_count
)) ||
1701 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1702 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1703 atomic_read(&dev
->carrier_up_count
)) ||
1704 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1705 atomic_read(&dev
->carrier_down_count
)))
1706 goto nla_put_failure
;
1708 if (event
!= IFLA_EVENT_NONE
) {
1709 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1710 goto nla_put_failure
;
1713 if (rtnl_fill_link_ifmap(skb
, dev
))
1714 goto nla_put_failure
;
1716 if (dev
->addr_len
) {
1717 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1718 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1719 goto nla_put_failure
;
1722 if (rtnl_phys_port_id_fill(skb
, dev
))
1723 goto nla_put_failure
;
1725 if (rtnl_phys_port_name_fill(skb
, dev
))
1726 goto nla_put_failure
;
1728 if (rtnl_phys_switch_id_fill(skb
, dev
))
1729 goto nla_put_failure
;
1731 if (rtnl_fill_stats(skb
, dev
))
1732 goto nla_put_failure
;
1734 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1735 goto nla_put_failure
;
1737 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1738 goto nla_put_failure
;
1740 if (rtnl_xdp_fill(skb
, dev
))
1741 goto nla_put_failure
;
1743 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1744 if (rtnl_link_fill(skb
, dev
) < 0)
1745 goto nla_put_failure
;
1748 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
, gfp
))
1749 goto nla_put_failure
;
1752 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1753 goto nla_put_failure
;
1755 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1756 goto nla_put_failure
;
1760 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1761 goto nla_put_failure_rcu
;
1764 if (rtnl_fill_prop_list(skb
, dev
))
1765 goto nla_put_failure
;
1767 nlmsg_end(skb
, nlh
);
1770 nla_put_failure_rcu
:
1773 nlmsg_cancel(skb
, nlh
);
1777 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1778 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1779 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1780 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1781 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1782 [IFLA_MTU
] = { .type
= NLA_U32
},
1783 [IFLA_LINK
] = { .type
= NLA_U32
},
1784 [IFLA_MASTER
] = { .type
= NLA_U32
},
1785 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1786 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1787 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1788 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1789 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1790 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1791 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1792 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1793 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1794 * allow 0-length string (needed to remove an alias).
1796 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1797 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1798 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1799 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1800 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1801 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1802 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1803 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1804 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1805 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1806 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1807 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1808 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1809 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1810 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1811 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1812 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1813 [IFLA_EVENT
] = { .type
= NLA_U32
},
1814 [IFLA_GROUP
] = { .type
= NLA_U32
},
1815 [IFLA_TARGET_NETNSID
] = { .type
= NLA_S32
},
1816 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1817 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1818 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
1819 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
1820 [IFLA_PROP_LIST
] = { .type
= NLA_NESTED
},
1821 [IFLA_ALT_IFNAME
] = { .type
= NLA_STRING
,
1822 .len
= ALTIFNAMSIZ
- 1 },
1825 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1826 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1827 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1828 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1829 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1832 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1833 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1834 [IFLA_VF_BROADCAST
] = { .type
= NLA_REJECT
},
1835 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1836 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1837 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1838 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1839 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1840 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1841 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1842 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1843 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1844 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1845 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1848 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1849 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1850 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1851 .len
= PORT_PROFILE_MAX
},
1852 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1853 .len
= PORT_UUID_MAX
},
1854 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1855 .len
= PORT_UUID_MAX
},
1856 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1857 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1859 /* Unused, but we need to keep it here since user space could
1860 * fill it. It's also broken with regard to NLA_BINARY use in
1861 * combination with structs.
1863 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1864 .len
= sizeof(struct ifla_port_vsi
) },
1867 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1868 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1869 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1870 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1871 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1874 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1876 const struct rtnl_link_ops
*ops
= NULL
;
1877 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1879 if (nla_parse_nested_deprecated(linfo
, IFLA_INFO_MAX
, nla
, ifla_info_policy
, NULL
) < 0)
1882 if (linfo
[IFLA_INFO_KIND
]) {
1883 char kind
[MODULE_NAME_LEN
];
1885 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1886 ops
= rtnl_link_ops_get(kind
);
1892 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1894 struct net_device
*master
;
1899 master
= netdev_master_upper_dev_get(dev
);
1900 if (!master
|| master
->ifindex
!= master_idx
)
1906 static bool link_kind_filtered(const struct net_device
*dev
,
1907 const struct rtnl_link_ops
*kind_ops
)
1909 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1915 static bool link_dump_filtered(struct net_device
*dev
,
1917 const struct rtnl_link_ops
*kind_ops
)
1919 if (link_master_filtered(dev
, master_idx
) ||
1920 link_kind_filtered(dev
, kind_ops
))
1927 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1928 * @sk: netlink socket
1929 * @netnsid: network namespace identifier
1931 * Returns the network namespace identified by netnsid on success or an error
1932 * pointer on failure.
1934 struct net
*rtnl_get_net_ns_capable(struct sock
*sk
, int netnsid
)
1938 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1940 return ERR_PTR(-EINVAL
);
1942 /* For now, the caller is required to have CAP_NET_ADMIN in
1943 * the user namespace owning the target net ns.
1945 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1947 return ERR_PTR(-EACCES
);
1951 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable
);
1953 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr
*nlh
,
1954 bool strict_check
, struct nlattr
**tb
,
1955 struct netlink_ext_ack
*extack
)
1960 struct ifinfomsg
*ifm
;
1962 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
1963 NL_SET_ERR_MSG(extack
, "Invalid header for link dump");
1967 ifm
= nlmsg_data(nlh
);
1968 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
1970 NL_SET_ERR_MSG(extack
, "Invalid values in header for link dump request");
1973 if (ifm
->ifi_index
) {
1974 NL_SET_ERR_MSG(extack
, "Filter by device index not supported for link dumps");
1978 return nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
,
1979 IFLA_MAX
, ifla_policy
,
1983 /* A hack to preserve kernel<->userspace interface.
1984 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1985 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1986 * what iproute2 < v3.9.0 used.
1987 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1988 * attribute, its netlink message is shorter than struct ifinfomsg.
1990 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
1991 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1993 return nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
,
1997 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1999 struct netlink_ext_ack
*extack
= cb
->extack
;
2000 const struct nlmsghdr
*nlh
= cb
->nlh
;
2001 struct net
*net
= sock_net(skb
->sk
);
2002 struct net
*tgt_net
= net
;
2005 struct net_device
*dev
;
2006 struct hlist_head
*head
;
2007 struct nlattr
*tb
[IFLA_MAX
+1];
2008 u32 ext_filter_mask
= 0;
2009 const struct rtnl_link_ops
*kind_ops
= NULL
;
2010 unsigned int flags
= NLM_F_MULTI
;
2016 s_idx
= cb
->args
[1];
2018 err
= rtnl_valid_dump_ifinfo_req(nlh
, cb
->strict_check
, tb
, extack
);
2020 if (cb
->strict_check
)
2026 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
2030 /* new attributes should only be added with strict checking */
2032 case IFLA_TARGET_NETNSID
:
2033 netnsid
= nla_get_s32(tb
[i
]);
2034 tgt_net
= rtnl_get_net_ns_capable(skb
->sk
, netnsid
);
2035 if (IS_ERR(tgt_net
)) {
2036 NL_SET_ERR_MSG(extack
, "Invalid target network namespace id");
2037 return PTR_ERR(tgt_net
);
2041 ext_filter_mask
= nla_get_u32(tb
[i
]);
2044 master_idx
= nla_get_u32(tb
[i
]);
2047 kind_ops
= linkinfo_to_kind_ops(tb
[i
]);
2050 if (cb
->strict_check
) {
2051 NL_SET_ERR_MSG(extack
, "Unsupported attribute in link dump request");
2057 if (master_idx
|| kind_ops
)
2058 flags
|= NLM_F_DUMP_FILTERED
;
2061 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
2063 head
= &tgt_net
->dev_index_head
[h
];
2064 hlist_for_each_entry(dev
, head
, index_hlist
) {
2065 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
2069 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
2071 NETLINK_CB(cb
->skb
).portid
,
2072 nlh
->nlmsg_seq
, 0, flags
,
2073 ext_filter_mask
, 0, NULL
, 0,
2074 netnsid
, GFP_KERNEL
);
2077 if (likely(skb
->len
))
2091 cb
->seq
= net
->dev_base_seq
;
2092 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2099 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
2100 struct netlink_ext_ack
*exterr
)
2102 return nla_parse_deprecated(tb
, IFLA_MAX
, head
, len
, ifla_policy
,
2105 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
2107 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
2110 /* Examine the link attributes and figure out which
2111 * network namespace we are talking about.
2113 if (tb
[IFLA_NET_NS_PID
])
2114 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
2115 else if (tb
[IFLA_NET_NS_FD
])
2116 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
2118 net
= get_net(src_net
);
2121 EXPORT_SYMBOL(rtnl_link_get_net
);
2123 /* Figure out which network namespace we are talking about by
2124 * examining the link attributes in the following order:
2126 * 1. IFLA_NET_NS_PID
2128 * 3. IFLA_TARGET_NETNSID
2130 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
2131 struct nlattr
*tb
[])
2135 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
2136 return rtnl_link_get_net(src_net
, tb
);
2138 if (!tb
[IFLA_TARGET_NETNSID
])
2139 return get_net(src_net
);
2141 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_TARGET_NETNSID
]));
2143 return ERR_PTR(-EINVAL
);
2148 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2149 struct net
*src_net
,
2150 struct nlattr
*tb
[], int cap
)
2154 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2158 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2160 return ERR_PTR(-EPERM
);
2166 /* Verify that rtnetlink requests do not pass additional properties
2167 * potentially referring to different network namespaces.
2169 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2170 struct netlink_ext_ack
*extack
,
2174 if (netns_id_only
) {
2175 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2178 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2182 if (tb
[IFLA_TARGET_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2185 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2188 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2194 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2198 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2201 if (tb
[IFLA_ADDRESS
] &&
2202 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2205 if (tb
[IFLA_BROADCAST
] &&
2206 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2210 if (tb
[IFLA_AF_SPEC
]) {
2214 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2215 const struct rtnl_af_ops
*af_ops
;
2218 af_ops
= rtnl_af_lookup(nla_type(af
));
2221 return -EAFNOSUPPORT
;
2224 if (!af_ops
->set_link_af
) {
2229 if (af_ops
->validate_link_af
) {
2230 err
= af_ops
->validate_link_af(dev
, af
);
2244 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2247 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2249 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2252 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2254 if (dev
->type
!= ARPHRD_INFINIBAND
)
2257 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2260 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2262 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2265 if (tb
[IFLA_VF_MAC
]) {
2266 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2268 if (ivm
->vf
>= INT_MAX
)
2271 if (ops
->ndo_set_vf_mac
)
2272 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2278 if (tb
[IFLA_VF_VLAN
]) {
2279 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2281 if (ivv
->vf
>= INT_MAX
)
2284 if (ops
->ndo_set_vf_vlan
)
2285 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2287 htons(ETH_P_8021Q
));
2292 if (tb
[IFLA_VF_VLAN_LIST
]) {
2293 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2294 struct nlattr
*attr
;
2298 if (!ops
->ndo_set_vf_vlan
)
2301 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2302 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2303 nla_len(attr
) < NLA_HDRLEN
) {
2306 if (len
>= MAX_VLAN_LIST_LEN
)
2308 ivvl
[len
] = nla_data(attr
);
2315 if (ivvl
[0]->vf
>= INT_MAX
)
2317 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2318 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2323 if (tb
[IFLA_VF_TX_RATE
]) {
2324 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2325 struct ifla_vf_info ivf
;
2327 if (ivt
->vf
>= INT_MAX
)
2330 if (ops
->ndo_get_vf_config
)
2331 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2336 if (ops
->ndo_set_vf_rate
)
2337 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2344 if (tb
[IFLA_VF_RATE
]) {
2345 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2347 if (ivt
->vf
>= INT_MAX
)
2350 if (ops
->ndo_set_vf_rate
)
2351 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2358 if (tb
[IFLA_VF_SPOOFCHK
]) {
2359 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2361 if (ivs
->vf
>= INT_MAX
)
2364 if (ops
->ndo_set_vf_spoofchk
)
2365 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2371 if (tb
[IFLA_VF_LINK_STATE
]) {
2372 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2374 if (ivl
->vf
>= INT_MAX
)
2377 if (ops
->ndo_set_vf_link_state
)
2378 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2384 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2385 struct ifla_vf_rss_query_en
*ivrssq_en
;
2388 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2389 if (ivrssq_en
->vf
>= INT_MAX
)
2391 if (ops
->ndo_set_vf_rss_query_en
)
2392 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2393 ivrssq_en
->setting
);
2398 if (tb
[IFLA_VF_TRUST
]) {
2399 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2401 if (ivt
->vf
>= INT_MAX
)
2404 if (ops
->ndo_set_vf_trust
)
2405 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2410 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2411 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2413 if (ivt
->vf
>= INT_MAX
)
2415 if (!ops
->ndo_set_vf_guid
)
2417 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2420 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2421 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2423 if (ivt
->vf
>= INT_MAX
)
2425 if (!ops
->ndo_set_vf_guid
)
2428 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2434 static int do_set_master(struct net_device
*dev
, int ifindex
,
2435 struct netlink_ext_ack
*extack
)
2437 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2438 const struct net_device_ops
*ops
;
2442 if (upper_dev
->ifindex
== ifindex
)
2444 ops
= upper_dev
->netdev_ops
;
2445 if (ops
->ndo_del_slave
) {
2446 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2449 netdev_update_lockdep_key(dev
);
2456 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2459 ops
= upper_dev
->netdev_ops
;
2460 if (ops
->ndo_add_slave
) {
2461 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2471 #define DO_SETLINK_MODIFIED 0x01
2472 /* notify flag means notify + modified. */
2473 #define DO_SETLINK_NOTIFY 0x03
2474 static int do_setlink(const struct sk_buff
*skb
,
2475 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2476 struct netlink_ext_ack
*extack
,
2477 struct nlattr
**tb
, char *ifname
, int status
)
2479 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2482 err
= validate_linkmsg(dev
, tb
);
2486 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_TARGET_NETNSID
]) {
2487 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2494 err
= dev_change_net_namespace(dev
, net
, ifname
);
2498 status
|= DO_SETLINK_MODIFIED
;
2502 struct rtnl_link_ifmap
*u_map
;
2505 if (!ops
->ndo_set_config
) {
2510 if (!netif_device_present(dev
)) {
2515 u_map
= nla_data(tb
[IFLA_MAP
]);
2516 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2517 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2518 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2519 k_map
.irq
= (unsigned char) u_map
->irq
;
2520 k_map
.dma
= (unsigned char) u_map
->dma
;
2521 k_map
.port
= (unsigned char) u_map
->port
;
2523 err
= ops
->ndo_set_config(dev
, &k_map
);
2527 status
|= DO_SETLINK_NOTIFY
;
2530 if (tb
[IFLA_ADDRESS
]) {
2531 struct sockaddr
*sa
;
2534 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2536 sa
= kmalloc(len
, GFP_KERNEL
);
2541 sa
->sa_family
= dev
->type
;
2542 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2544 err
= dev_set_mac_address(dev
, sa
, extack
);
2548 status
|= DO_SETLINK_MODIFIED
;
2552 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
2555 status
|= DO_SETLINK_MODIFIED
;
2558 if (tb
[IFLA_GROUP
]) {
2559 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2560 status
|= DO_SETLINK_NOTIFY
;
2564 * Interface selected by interface index but interface
2565 * name provided implies that a name change has been
2568 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2569 err
= dev_change_name(dev
, ifname
);
2572 status
|= DO_SETLINK_MODIFIED
;
2575 if (tb
[IFLA_IFALIAS
]) {
2576 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2577 nla_len(tb
[IFLA_IFALIAS
]));
2580 status
|= DO_SETLINK_NOTIFY
;
2583 if (tb
[IFLA_BROADCAST
]) {
2584 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2585 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2588 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2589 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
2595 if (tb
[IFLA_MASTER
]) {
2596 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2599 status
|= DO_SETLINK_MODIFIED
;
2602 if (tb
[IFLA_CARRIER
]) {
2603 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2606 status
|= DO_SETLINK_MODIFIED
;
2609 if (tb
[IFLA_TXQLEN
]) {
2610 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2612 err
= dev_change_tx_queue_len(dev
, value
);
2615 status
|= DO_SETLINK_MODIFIED
;
2618 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2619 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2621 if (max_size
> GSO_MAX_SIZE
) {
2626 if (dev
->gso_max_size
^ max_size
) {
2627 netif_set_gso_max_size(dev
, max_size
);
2628 status
|= DO_SETLINK_MODIFIED
;
2632 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2633 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2635 if (max_segs
> GSO_MAX_SEGS
) {
2640 if (dev
->gso_max_segs
^ max_segs
) {
2641 dev
->gso_max_segs
= max_segs
;
2642 status
|= DO_SETLINK_MODIFIED
;
2646 if (tb
[IFLA_OPERSTATE
])
2647 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2649 if (tb
[IFLA_LINKMODE
]) {
2650 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2652 write_lock_bh(&dev_base_lock
);
2653 if (dev
->link_mode
^ value
)
2654 status
|= DO_SETLINK_NOTIFY
;
2655 dev
->link_mode
= value
;
2656 write_unlock_bh(&dev_base_lock
);
2659 if (tb
[IFLA_VFINFO_LIST
]) {
2660 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2661 struct nlattr
*attr
;
2664 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2665 if (nla_type(attr
) != IFLA_VF_INFO
||
2666 nla_len(attr
) < NLA_HDRLEN
) {
2670 err
= nla_parse_nested_deprecated(vfinfo
, IFLA_VF_MAX
,
2676 err
= do_setvfinfo(dev
, vfinfo
);
2679 status
|= DO_SETLINK_NOTIFY
;
2684 if (tb
[IFLA_VF_PORTS
]) {
2685 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2686 struct nlattr
*attr
;
2691 if (!ops
->ndo_set_vf_port
)
2694 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2695 if (nla_type(attr
) != IFLA_VF_PORT
||
2696 nla_len(attr
) < NLA_HDRLEN
) {
2700 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2706 if (!port
[IFLA_PORT_VF
]) {
2710 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2711 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2714 status
|= DO_SETLINK_NOTIFY
;
2719 if (tb
[IFLA_PORT_SELF
]) {
2720 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2722 err
= nla_parse_nested_deprecated(port
, IFLA_PORT_MAX
,
2724 ifla_port_policy
, NULL
);
2729 if (ops
->ndo_set_vf_port
)
2730 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2733 status
|= DO_SETLINK_NOTIFY
;
2736 if (tb
[IFLA_AF_SPEC
]) {
2740 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2741 const struct rtnl_af_ops
*af_ops
;
2745 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2747 err
= af_ops
->set_link_af(dev
, af
);
2754 status
|= DO_SETLINK_NOTIFY
;
2759 if (tb
[IFLA_PROTO_DOWN
]) {
2760 err
= dev_change_proto_down(dev
,
2761 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2764 status
|= DO_SETLINK_NOTIFY
;
2768 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2771 err
= nla_parse_nested_deprecated(xdp
, IFLA_XDP_MAX
,
2773 ifla_xdp_policy
, NULL
);
2777 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2782 if (xdp
[IFLA_XDP_FLAGS
]) {
2783 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2784 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2788 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2794 if (xdp
[IFLA_XDP_FD
]) {
2795 err
= dev_change_xdp_fd(dev
, extack
,
2796 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2800 status
|= DO_SETLINK_NOTIFY
;
2805 if (status
& DO_SETLINK_MODIFIED
) {
2806 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2807 netdev_state_change(dev
);
2810 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",
2817 static struct net_device
*rtnl_dev_get(struct net
*net
,
2818 struct nlattr
*ifname_attr
,
2819 struct nlattr
*altifname_attr
,
2822 char buffer
[ALTIFNAMSIZ
];
2827 nla_strlcpy(ifname
, ifname_attr
, IFNAMSIZ
);
2828 else if (altifname_attr
)
2829 nla_strlcpy(ifname
, altifname_attr
, ALTIFNAMSIZ
);
2834 return __dev_get_by_name(net
, ifname
);
2837 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2838 struct netlink_ext_ack
*extack
)
2840 struct net
*net
= sock_net(skb
->sk
);
2841 struct ifinfomsg
*ifm
;
2842 struct net_device
*dev
;
2844 struct nlattr
*tb
[IFLA_MAX
+1];
2845 char ifname
[IFNAMSIZ
];
2847 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
2848 ifla_policy
, extack
);
2852 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2856 if (tb
[IFLA_IFNAME
])
2857 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2862 ifm
= nlmsg_data(nlh
);
2863 if (ifm
->ifi_index
> 0)
2864 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2865 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
2866 dev
= rtnl_dev_get(net
, NULL
, tb
[IFLA_ALT_IFNAME
], ifname
);
2875 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2880 static int rtnl_group_dellink(const struct net
*net
, int group
)
2882 struct net_device
*dev
, *aux
;
2883 LIST_HEAD(list_kill
);
2889 for_each_netdev(net
, dev
) {
2890 if (dev
->group
== group
) {
2891 const struct rtnl_link_ops
*ops
;
2894 ops
= dev
->rtnl_link_ops
;
2895 if (!ops
|| !ops
->dellink
)
2903 for_each_netdev_safe(net
, dev
, aux
) {
2904 if (dev
->group
== group
) {
2905 const struct rtnl_link_ops
*ops
;
2907 ops
= dev
->rtnl_link_ops
;
2908 ops
->dellink(dev
, &list_kill
);
2911 unregister_netdevice_many(&list_kill
);
2916 int rtnl_delete_link(struct net_device
*dev
)
2918 const struct rtnl_link_ops
*ops
;
2919 LIST_HEAD(list_kill
);
2921 ops
= dev
->rtnl_link_ops
;
2922 if (!ops
|| !ops
->dellink
)
2925 ops
->dellink(dev
, &list_kill
);
2926 unregister_netdevice_many(&list_kill
);
2930 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2932 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2933 struct netlink_ext_ack
*extack
)
2935 struct net
*net
= sock_net(skb
->sk
);
2936 struct net
*tgt_net
= net
;
2937 struct net_device
*dev
= NULL
;
2938 struct ifinfomsg
*ifm
;
2939 struct nlattr
*tb
[IFLA_MAX
+1];
2943 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
2944 ifla_policy
, extack
);
2948 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2952 if (tb
[IFLA_TARGET_NETNSID
]) {
2953 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
2954 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
2955 if (IS_ERR(tgt_net
))
2956 return PTR_ERR(tgt_net
);
2960 ifm
= nlmsg_data(nlh
);
2961 if (ifm
->ifi_index
> 0)
2962 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2963 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
2964 dev
= rtnl_dev_get(net
, tb
[IFLA_IFNAME
],
2965 tb
[IFLA_ALT_IFNAME
], NULL
);
2966 else if (tb
[IFLA_GROUP
])
2967 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2972 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2978 err
= rtnl_delete_link(dev
);
2987 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2989 unsigned int old_flags
;
2992 old_flags
= dev
->flags
;
2993 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2994 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
),
3000 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
3001 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
));
3003 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
3004 __dev_notify_flags(dev
, old_flags
, ~0U);
3008 EXPORT_SYMBOL(rtnl_configure_link
);
3010 struct net_device
*rtnl_create_link(struct net
*net
, const char *ifname
,
3011 unsigned char name_assign_type
,
3012 const struct rtnl_link_ops
*ops
,
3013 struct nlattr
*tb
[],
3014 struct netlink_ext_ack
*extack
)
3016 struct net_device
*dev
;
3017 unsigned int num_tx_queues
= 1;
3018 unsigned int num_rx_queues
= 1;
3020 if (tb
[IFLA_NUM_TX_QUEUES
])
3021 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
3022 else if (ops
->get_num_tx_queues
)
3023 num_tx_queues
= ops
->get_num_tx_queues();
3025 if (tb
[IFLA_NUM_RX_QUEUES
])
3026 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
3027 else if (ops
->get_num_rx_queues
)
3028 num_rx_queues
= ops
->get_num_rx_queues();
3030 if (num_tx_queues
< 1 || num_tx_queues
> 4096) {
3031 NL_SET_ERR_MSG(extack
, "Invalid number of transmit queues");
3032 return ERR_PTR(-EINVAL
);
3035 if (num_rx_queues
< 1 || num_rx_queues
> 4096) {
3036 NL_SET_ERR_MSG(extack
, "Invalid number of receive queues");
3037 return ERR_PTR(-EINVAL
);
3040 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
3041 ops
->setup
, num_tx_queues
, num_rx_queues
);
3043 return ERR_PTR(-ENOMEM
);
3045 dev_net_set(dev
, net
);
3046 dev
->rtnl_link_ops
= ops
;
3047 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
3050 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
3051 if (tb
[IFLA_ADDRESS
]) {
3052 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
3053 nla_len(tb
[IFLA_ADDRESS
]));
3054 dev
->addr_assign_type
= NET_ADDR_SET
;
3056 if (tb
[IFLA_BROADCAST
])
3057 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
3058 nla_len(tb
[IFLA_BROADCAST
]));
3059 if (tb
[IFLA_TXQLEN
])
3060 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
3061 if (tb
[IFLA_OPERSTATE
])
3062 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
3063 if (tb
[IFLA_LINKMODE
])
3064 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
3066 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
3067 if (tb
[IFLA_GSO_MAX_SIZE
])
3068 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
3069 if (tb
[IFLA_GSO_MAX_SEGS
])
3070 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
3074 EXPORT_SYMBOL(rtnl_create_link
);
3076 static int rtnl_group_changelink(const struct sk_buff
*skb
,
3077 struct net
*net
, int group
,
3078 struct ifinfomsg
*ifm
,
3079 struct netlink_ext_ack
*extack
,
3082 struct net_device
*dev
, *aux
;
3085 for_each_netdev_safe(net
, dev
, aux
) {
3086 if (dev
->group
== group
) {
3087 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
3096 static int __rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3097 struct nlattr
**attr
, struct netlink_ext_ack
*extack
)
3099 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
3100 unsigned char name_assign_type
= NET_NAME_USER
;
3101 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
3102 const struct rtnl_link_ops
*m_ops
= NULL
;
3103 struct net_device
*master_dev
= NULL
;
3104 struct net
*net
= sock_net(skb
->sk
);
3105 const struct rtnl_link_ops
*ops
;
3106 struct nlattr
*tb
[IFLA_MAX
+ 1];
3107 struct net
*dest_net
, *link_net
;
3108 struct nlattr
**slave_data
;
3109 char kind
[MODULE_NAME_LEN
];
3110 struct net_device
*dev
;
3111 struct ifinfomsg
*ifm
;
3112 char ifname
[IFNAMSIZ
];
3113 struct nlattr
**data
;
3116 #ifdef CONFIG_MODULES
3119 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3120 ifla_policy
, extack
);
3124 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
3128 if (tb
[IFLA_IFNAME
])
3129 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3133 ifm
= nlmsg_data(nlh
);
3134 if (ifm
->ifi_index
> 0)
3135 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3136 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3137 dev
= rtnl_dev_get(net
, NULL
, tb
[IFLA_ALT_IFNAME
], ifname
);
3142 master_dev
= netdev_master_upper_dev_get(dev
);
3144 m_ops
= master_dev
->rtnl_link_ops
;
3147 err
= validate_linkmsg(dev
, tb
);
3151 if (tb
[IFLA_LINKINFO
]) {
3152 err
= nla_parse_nested_deprecated(linkinfo
, IFLA_INFO_MAX
,
3154 ifla_info_policy
, NULL
);
3158 memset(linkinfo
, 0, sizeof(linkinfo
));
3160 if (linkinfo
[IFLA_INFO_KIND
]) {
3161 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
3162 ops
= rtnl_link_ops_get(kind
);
3170 if (ops
->maxtype
> RTNL_MAX_TYPE
)
3173 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3174 err
= nla_parse_nested_deprecated(attr
, ops
->maxtype
,
3175 linkinfo
[IFLA_INFO_DATA
],
3176 ops
->policy
, extack
);
3181 if (ops
->validate
) {
3182 err
= ops
->validate(tb
, data
, extack
);
3190 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3193 if (m_ops
->slave_maxtype
&&
3194 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3195 err
= nla_parse_nested_deprecated(slave_attr
,
3196 m_ops
->slave_maxtype
,
3197 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3198 m_ops
->slave_policy
,
3202 slave_data
= slave_attr
;
3209 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3211 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3214 if (linkinfo
[IFLA_INFO_DATA
]) {
3215 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
3219 err
= ops
->changelink(dev
, tb
, data
, extack
);
3222 status
|= DO_SETLINK_NOTIFY
;
3225 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3226 if (!m_ops
|| !m_ops
->slave_changelink
)
3229 err
= m_ops
->slave_changelink(master_dev
, dev
, tb
,
3230 slave_data
, extack
);
3233 status
|= DO_SETLINK_NOTIFY
;
3236 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, status
);
3239 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3240 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
3241 return rtnl_group_changelink(skb
, net
,
3242 nla_get_u32(tb
[IFLA_GROUP
]),
3247 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3251 #ifdef CONFIG_MODULES
3254 request_module("rtnl-link-%s", kind
);
3256 ops
= rtnl_link_ops_get(kind
);
3261 NL_SET_ERR_MSG(extack
, "Unknown device type");
3269 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3270 name_assign_type
= NET_NAME_ENUM
;
3273 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3274 if (IS_ERR(dest_net
))
3275 return PTR_ERR(dest_net
);
3277 if (tb
[IFLA_LINK_NETNSID
]) {
3278 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3280 link_net
= get_net_ns_by_id(dest_net
, id
);
3282 NL_SET_ERR_MSG(extack
, "Unknown network namespace id");
3287 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3293 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3294 name_assign_type
, ops
, tb
, extack
);
3300 dev
->ifindex
= ifm
->ifi_index
;
3303 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
, extack
);
3304 /* Drivers should call free_netdev() in ->destructor
3305 * and unregister it on failure after registration
3306 * so that device could be finally freed in rtnl_unlock.
3309 /* If device is not registered at all, free it now */
3310 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3315 err
= register_netdevice(dev
);
3321 err
= rtnl_configure_link(dev
, ifm
);
3323 goto out_unregister
;
3325 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3327 goto out_unregister
;
3329 if (tb
[IFLA_MASTER
]) {
3330 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
3332 goto out_unregister
;
3341 LIST_HEAD(list_kill
);
3343 ops
->dellink(dev
, &list_kill
);
3344 unregister_netdevice_many(&list_kill
);
3346 unregister_netdevice(dev
);
3351 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3352 struct netlink_ext_ack
*extack
)
3354 struct nlattr
**attr
;
3357 attr
= kmalloc_array(RTNL_MAX_TYPE
+ 1, sizeof(*attr
), GFP_KERNEL
);
3361 ret
= __rtnl_newlink(skb
, nlh
, attr
, extack
);
3366 static int rtnl_valid_getlink_req(struct sk_buff
*skb
,
3367 const struct nlmsghdr
*nlh
,
3369 struct netlink_ext_ack
*extack
)
3371 struct ifinfomsg
*ifm
;
3374 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
3375 NL_SET_ERR_MSG(extack
, "Invalid header for get link");
3379 if (!netlink_strict_get_check(skb
))
3380 return nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3381 ifla_policy
, extack
);
3383 ifm
= nlmsg_data(nlh
);
3384 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
3386 NL_SET_ERR_MSG(extack
, "Invalid values in header for get link request");
3390 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
3391 ifla_policy
, extack
);
3395 for (i
= 0; i
<= IFLA_MAX
; i
++) {
3401 case IFLA_ALT_IFNAME
:
3403 case IFLA_TARGET_NETNSID
:
3406 NL_SET_ERR_MSG(extack
, "Unsupported attribute in get link request");
3414 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3415 struct netlink_ext_ack
*extack
)
3417 struct net
*net
= sock_net(skb
->sk
);
3418 struct net
*tgt_net
= net
;
3419 struct ifinfomsg
*ifm
;
3420 struct nlattr
*tb
[IFLA_MAX
+1];
3421 struct net_device
*dev
= NULL
;
3422 struct sk_buff
*nskb
;
3425 u32 ext_filter_mask
= 0;
3427 err
= rtnl_valid_getlink_req(skb
, nlh
, tb
, extack
);
3431 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3435 if (tb
[IFLA_TARGET_NETNSID
]) {
3436 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3437 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3438 if (IS_ERR(tgt_net
))
3439 return PTR_ERR(tgt_net
);
3442 if (tb
[IFLA_EXT_MASK
])
3443 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3446 ifm
= nlmsg_data(nlh
);
3447 if (ifm
->ifi_index
> 0)
3448 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3449 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3450 dev
= rtnl_dev_get(tgt_net
, tb
[IFLA_IFNAME
],
3451 tb
[IFLA_ALT_IFNAME
], NULL
);
3460 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3464 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3465 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3466 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3467 0, NULL
, 0, netnsid
, GFP_KERNEL
);
3469 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3470 WARN_ON(err
== -EMSGSIZE
);
3473 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3481 static int rtnl_alt_ifname(int cmd
, struct net_device
*dev
, struct nlattr
*attr
,
3482 bool *changed
, struct netlink_ext_ack
*extack
)
3487 err
= nla_validate(attr
, attr
->nla_len
, IFLA_MAX
, ifla_policy
, extack
);
3491 alt_ifname
= nla_data(attr
);
3492 if (cmd
== RTM_NEWLINKPROP
) {
3493 alt_ifname
= kstrdup(alt_ifname
, GFP_KERNEL
);
3496 err
= netdev_name_node_alt_create(dev
, alt_ifname
);
3501 } else if (cmd
== RTM_DELLINKPROP
) {
3502 err
= netdev_name_node_alt_destroy(dev
, alt_ifname
);
3514 static int rtnl_linkprop(int cmd
, struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3515 struct netlink_ext_ack
*extack
)
3517 struct net
*net
= sock_net(skb
->sk
);
3518 struct nlattr
*tb
[IFLA_MAX
+ 1];
3519 struct net_device
*dev
;
3520 struct ifinfomsg
*ifm
;
3521 bool changed
= false;
3522 struct nlattr
*attr
;
3525 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3529 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3533 ifm
= nlmsg_data(nlh
);
3534 if (ifm
->ifi_index
> 0)
3535 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3536 else if (tb
[IFLA_IFNAME
] || tb
[IFLA_ALT_IFNAME
])
3537 dev
= rtnl_dev_get(net
, tb
[IFLA_IFNAME
],
3538 tb
[IFLA_ALT_IFNAME
], NULL
);
3545 if (!tb
[IFLA_PROP_LIST
])
3548 nla_for_each_nested(attr
, tb
[IFLA_PROP_LIST
], rem
) {
3549 switch (nla_type(attr
)) {
3550 case IFLA_ALT_IFNAME
:
3551 err
= rtnl_alt_ifname(cmd
, dev
, attr
, &changed
, extack
);
3559 netdev_state_change(dev
);
3563 static int rtnl_newlinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3564 struct netlink_ext_ack
*extack
)
3566 return rtnl_linkprop(RTM_NEWLINKPROP
, skb
, nlh
, extack
);
3569 static int rtnl_dellinkprop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3570 struct netlink_ext_ack
*extack
)
3572 return rtnl_linkprop(RTM_DELLINKPROP
, skb
, nlh
, extack
);
3575 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3577 struct net
*net
= sock_net(skb
->sk
);
3578 struct net_device
*dev
;
3579 struct nlattr
*tb
[IFLA_MAX
+1];
3580 u32 ext_filter_mask
= 0;
3581 u16 min_ifinfo_dump_size
= 0;
3584 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3585 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3586 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3588 if (nlmsg_parse_deprecated(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3589 if (tb
[IFLA_EXT_MASK
])
3590 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3593 if (!ext_filter_mask
)
3594 return NLMSG_GOODSIZE
;
3596 * traverse the list of net devices and compute the minimum
3597 * buffer size based upon the filter mask.
3600 for_each_netdev_rcu(net
, dev
) {
3601 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3607 return nlmsg_total_size(min_ifinfo_dump_size
);
3610 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3613 int s_idx
= cb
->family
;
3614 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
3620 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3621 struct rtnl_link
**tab
;
3622 struct rtnl_link
*link
;
3623 rtnl_dumpit_func dumpit
;
3625 if (idx
< s_idx
|| idx
== PF_PACKET
)
3628 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3631 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3639 dumpit
= link
->dumpit
;
3644 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3648 ret
= dumpit(skb
, cb
);
3654 return skb
->len
? : ret
;
3657 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3658 unsigned int change
,
3659 u32 event
, gfp_t flags
, int *new_nsid
,
3662 struct net
*net
= dev_net(dev
);
3663 struct sk_buff
*skb
;
3665 size_t if_info_size
;
3667 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3671 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3672 type
, 0, 0, change
, 0, 0, event
,
3673 new_nsid
, new_ifindex
, -1, flags
);
3675 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3676 WARN_ON(err
== -EMSGSIZE
);
3683 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3687 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3689 struct net
*net
= dev_net(dev
);
3691 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3694 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3695 unsigned int change
, u32 event
,
3696 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3698 struct sk_buff
*skb
;
3700 if (dev
->reg_state
!= NETREG_REGISTERED
)
3703 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3706 rtmsg_ifinfo_send(skb
, dev
, flags
);
3709 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3712 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3716 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3717 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3719 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3720 new_nsid
, new_ifindex
);
3723 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3724 struct net_device
*dev
,
3725 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3726 int type
, unsigned int flags
,
3727 int nlflags
, u16 ndm_state
)
3729 struct nlmsghdr
*nlh
;
3732 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3736 ndm
= nlmsg_data(nlh
);
3737 ndm
->ndm_family
= AF_BRIDGE
;
3740 ndm
->ndm_flags
= flags
;
3742 ndm
->ndm_ifindex
= dev
->ifindex
;
3743 ndm
->ndm_state
= ndm_state
;
3745 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3746 goto nla_put_failure
;
3748 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3749 goto nla_put_failure
;
3751 nlmsg_end(skb
, nlh
);
3755 nlmsg_cancel(skb
, nlh
);
3759 static inline size_t rtnl_fdb_nlmsg_size(void)
3761 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3762 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3763 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3767 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3770 struct net
*net
= dev_net(dev
);
3771 struct sk_buff
*skb
;
3774 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3778 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3779 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3785 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3788 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3792 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3794 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3795 struct nlattr
*tb
[],
3796 struct net_device
*dev
,
3797 const unsigned char *addr
, u16 vid
,
3802 /* If aging addresses are supported device will need to
3803 * implement its own handler for this.
3805 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3806 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3811 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3815 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3816 err
= dev_uc_add_excl(dev
, addr
);
3817 else if (is_multicast_ether_addr(addr
))
3818 err
= dev_mc_add_excl(dev
, addr
);
3820 /* Only return duplicate errors if NLM_F_EXCL is set */
3821 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3826 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3828 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3829 struct netlink_ext_ack
*extack
)
3834 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3835 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3839 vid
= nla_get_u16(vlan_attr
);
3841 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3842 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3850 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3851 struct netlink_ext_ack
*extack
)
3853 struct net
*net
= sock_net(skb
->sk
);
3855 struct nlattr
*tb
[NDA_MAX
+1];
3856 struct net_device
*dev
;
3861 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
3866 ndm
= nlmsg_data(nlh
);
3867 if (ndm
->ndm_ifindex
== 0) {
3868 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3872 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3874 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3878 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3879 NL_SET_ERR_MSG(extack
, "invalid address");
3883 if (dev
->type
!= ARPHRD_ETHER
) {
3884 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
3888 addr
= nla_data(tb
[NDA_LLADDR
]);
3890 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3896 /* Support fdb on master device the net/bridge default case */
3897 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3898 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3899 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3900 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3902 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3903 nlh
->nlmsg_flags
, extack
);
3907 ndm
->ndm_flags
&= ~NTF_MASTER
;
3910 /* Embedded bridge, macvlan, and any other device support */
3911 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3912 if (dev
->netdev_ops
->ndo_fdb_add
)
3913 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3918 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3922 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3924 ndm
->ndm_flags
&= ~NTF_SELF
;
3932 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3934 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3935 struct nlattr
*tb
[],
3936 struct net_device
*dev
,
3937 const unsigned char *addr
, u16 vid
)
3941 /* If aging addresses are supported device will need to
3942 * implement its own handler for this.
3944 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3945 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3949 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3950 err
= dev_uc_del(dev
, addr
);
3951 else if (is_multicast_ether_addr(addr
))
3952 err
= dev_mc_del(dev
, addr
);
3956 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3958 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3959 struct netlink_ext_ack
*extack
)
3961 struct net
*net
= sock_net(skb
->sk
);
3963 struct nlattr
*tb
[NDA_MAX
+1];
3964 struct net_device
*dev
;
3969 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3972 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
,
3977 ndm
= nlmsg_data(nlh
);
3978 if (ndm
->ndm_ifindex
== 0) {
3979 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3983 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3985 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3989 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3990 NL_SET_ERR_MSG(extack
, "invalid address");
3994 if (dev
->type
!= ARPHRD_ETHER
) {
3995 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
3999 addr
= nla_data(tb
[NDA_LLADDR
]);
4001 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
4007 /* Support fdb on master device the net/bridge default case */
4008 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
4009 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
4010 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4011 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
4013 if (ops
->ndo_fdb_del
)
4014 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
4019 ndm
->ndm_flags
&= ~NTF_MASTER
;
4022 /* Embedded bridge, macvlan, and any other device support */
4023 if (ndm
->ndm_flags
& NTF_SELF
) {
4024 if (dev
->netdev_ops
->ndo_fdb_del
)
4025 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
4028 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
4031 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
4033 ndm
->ndm_flags
&= ~NTF_SELF
;
4040 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
4041 struct netlink_callback
*cb
,
4042 struct net_device
*dev
,
4044 struct netdev_hw_addr_list
*list
)
4046 struct netdev_hw_addr
*ha
;
4050 portid
= NETLINK_CB(cb
->skb
).portid
;
4051 seq
= cb
->nlh
->nlmsg_seq
;
4053 list_for_each_entry(ha
, &list
->list
, list
) {
4054 if (*idx
< cb
->args
[2])
4057 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
4059 RTM_NEWNEIGH
, NTF_SELF
,
4060 NLM_F_MULTI
, NUD_PERMANENT
);
4070 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4071 * @skb: socket buffer to store message in
4072 * @cb: netlink callback
4074 * @filter_dev: ignored
4075 * @idx: the number of FDB table entries dumped is added to *@idx
4077 * Default netdevice operation to dump the existing unicast address list.
4078 * Returns number of addresses from list put in skb.
4080 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
4081 struct netlink_callback
*cb
,
4082 struct net_device
*dev
,
4083 struct net_device
*filter_dev
,
4088 if (dev
->type
!= ARPHRD_ETHER
)
4091 netif_addr_lock_bh(dev
);
4092 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
4095 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
4097 netif_addr_unlock_bh(dev
);
4100 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
4102 static int valid_fdb_dump_strict(const struct nlmsghdr
*nlh
,
4103 int *br_idx
, int *brport_idx
,
4104 struct netlink_ext_ack
*extack
)
4106 struct nlattr
*tb
[NDA_MAX
+ 1];
4110 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4111 NL_SET_ERR_MSG(extack
, "Invalid header for fdb dump request");
4115 ndm
= nlmsg_data(nlh
);
4116 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4117 ndm
->ndm_flags
|| ndm
->ndm_type
) {
4118 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb dump request");
4122 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4123 NDA_MAX
, NULL
, extack
);
4127 *brport_idx
= ndm
->ndm_ifindex
;
4128 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4134 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4135 NL_SET_ERR_MSG(extack
, "Invalid IFINDEX attribute in fdb dump request");
4138 *brport_idx
= nla_get_u32(tb
[NDA_IFINDEX
]);
4141 if (nla_len(tb
[i
]) != sizeof(u32
)) {
4142 NL_SET_ERR_MSG(extack
, "Invalid MASTER attribute in fdb dump request");
4145 *br_idx
= nla_get_u32(tb
[NDA_MASTER
]);
4148 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb dump request");
4156 static int valid_fdb_dump_legacy(const struct nlmsghdr
*nlh
,
4157 int *br_idx
, int *brport_idx
,
4158 struct netlink_ext_ack
*extack
)
4160 struct nlattr
*tb
[IFLA_MAX
+1];
4163 /* A hack to preserve kernel<->userspace interface.
4164 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4165 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4166 * So, check for ndmsg with an optional u32 attribute (not used here).
4167 * Fortunately these sizes don't conflict with the size of ifinfomsg
4168 * with an optional attribute.
4170 if (nlmsg_len(nlh
) != sizeof(struct ndmsg
) &&
4171 (nlmsg_len(nlh
) != sizeof(struct ndmsg
) +
4172 nla_attr_size(sizeof(u32
)))) {
4173 struct ifinfomsg
*ifm
;
4175 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4176 tb
, IFLA_MAX
, ifla_policy
,
4180 } else if (err
== 0) {
4181 if (tb
[IFLA_MASTER
])
4182 *br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
4185 ifm
= nlmsg_data(nlh
);
4186 *brport_idx
= ifm
->ifi_index
;
4191 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4193 struct net_device
*dev
;
4194 struct net_device
*br_dev
= NULL
;
4195 const struct net_device_ops
*ops
= NULL
;
4196 const struct net_device_ops
*cops
= NULL
;
4197 struct net
*net
= sock_net(skb
->sk
);
4198 struct hlist_head
*head
;
4206 if (cb
->strict_check
)
4207 err
= valid_fdb_dump_strict(cb
->nlh
, &br_idx
, &brport_idx
,
4210 err
= valid_fdb_dump_legacy(cb
->nlh
, &br_idx
, &brport_idx
,
4216 br_dev
= __dev_get_by_index(net
, br_idx
);
4220 ops
= br_dev
->netdev_ops
;
4224 s_idx
= cb
->args
[1];
4226 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4228 head
= &net
->dev_index_head
[h
];
4229 hlist_for_each_entry(dev
, head
, index_hlist
) {
4231 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
4234 if (!br_idx
) { /* user did not specify a specific bridge */
4235 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
4236 br_dev
= netdev_master_upper_dev_get(dev
);
4237 cops
= br_dev
->netdev_ops
;
4240 if (dev
!= br_dev
&&
4241 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
4244 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
4245 !(dev
->priv_flags
& IFF_EBRIDGE
))
4253 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
4254 if (cops
&& cops
->ndo_fdb_dump
) {
4255 err
= cops
->ndo_fdb_dump(skb
, cb
,
4258 if (err
== -EMSGSIZE
)
4263 if (dev
->netdev_ops
->ndo_fdb_dump
)
4264 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
4268 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
4270 if (err
== -EMSGSIZE
)
4275 /* reset fdb offset to 0 for rest of the interfaces */
4291 static int valid_fdb_get_strict(const struct nlmsghdr
*nlh
,
4292 struct nlattr
**tb
, u8
*ndm_flags
,
4293 int *br_idx
, int *brport_idx
, u8
**addr
,
4294 u16
*vid
, struct netlink_ext_ack
*extack
)
4299 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
4300 NL_SET_ERR_MSG(extack
, "Invalid header for fdb get request");
4304 ndm
= nlmsg_data(nlh
);
4305 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
4307 NL_SET_ERR_MSG(extack
, "Invalid values in header for fdb get request");
4311 if (ndm
->ndm_flags
& ~(NTF_MASTER
| NTF_SELF
)) {
4312 NL_SET_ERR_MSG(extack
, "Invalid flags in header for fdb get request");
4316 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct ndmsg
), tb
,
4317 NDA_MAX
, nda_policy
, extack
);
4321 *ndm_flags
= ndm
->ndm_flags
;
4322 *brport_idx
= ndm
->ndm_ifindex
;
4323 for (i
= 0; i
<= NDA_MAX
; ++i
) {
4329 *br_idx
= nla_get_u32(tb
[i
]);
4332 if (nla_len(tb
[i
]) != ETH_ALEN
) {
4333 NL_SET_ERR_MSG(extack
, "Invalid address in fdb get request");
4336 *addr
= nla_data(tb
[i
]);
4339 err
= fdb_vid_parse(tb
[i
], vid
, extack
);
4346 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb get request");
4354 static int rtnl_fdb_get(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
4355 struct netlink_ext_ack
*extack
)
4357 struct net_device
*dev
= NULL
, *br_dev
= NULL
;
4358 const struct net_device_ops
*ops
= NULL
;
4359 struct net
*net
= sock_net(in_skb
->sk
);
4360 struct nlattr
*tb
[NDA_MAX
+ 1];
4361 struct sk_buff
*skb
;
4369 err
= valid_fdb_get_strict(nlh
, tb
, &ndm_flags
, &br_idx
,
4370 &brport_idx
, &addr
, &vid
, extack
);
4375 NL_SET_ERR_MSG(extack
, "Missing lookup address for fdb get request");
4380 dev
= __dev_get_by_index(net
, brport_idx
);
4382 NL_SET_ERR_MSG(extack
, "Unknown device ifindex");
4389 NL_SET_ERR_MSG(extack
, "Master and device are mutually exclusive");
4393 br_dev
= __dev_get_by_index(net
, br_idx
);
4395 NL_SET_ERR_MSG(extack
, "Invalid master ifindex");
4398 ops
= br_dev
->netdev_ops
;
4402 if (!ndm_flags
|| (ndm_flags
& NTF_MASTER
)) {
4403 if (!(dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
4404 NL_SET_ERR_MSG(extack
, "Device is not a bridge port");
4407 br_dev
= netdev_master_upper_dev_get(dev
);
4409 NL_SET_ERR_MSG(extack
, "Master of device not found");
4412 ops
= br_dev
->netdev_ops
;
4414 if (!(ndm_flags
& NTF_SELF
)) {
4415 NL_SET_ERR_MSG(extack
, "Missing NTF_SELF");
4418 ops
= dev
->netdev_ops
;
4422 if (!br_dev
&& !dev
) {
4423 NL_SET_ERR_MSG(extack
, "No device specified");
4427 if (!ops
|| !ops
->ndo_fdb_get
) {
4428 NL_SET_ERR_MSG(extack
, "Fdb get operation not supported by device");
4432 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
4438 err
= ops
->ndo_fdb_get(skb
, tb
, dev
, addr
, vid
,
4439 NETLINK_CB(in_skb
).portid
,
4440 nlh
->nlmsg_seq
, extack
);
4444 return rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4450 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
4451 unsigned int attrnum
, unsigned int flag
)
4454 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
4458 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
4459 struct net_device
*dev
, u16 mode
,
4460 u32 flags
, u32 mask
, int nlflags
,
4462 int (*vlan_fill
)(struct sk_buff
*skb
,
4463 struct net_device
*dev
,
4466 struct nlmsghdr
*nlh
;
4467 struct ifinfomsg
*ifm
;
4468 struct nlattr
*br_afspec
;
4469 struct nlattr
*protinfo
;
4470 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
4471 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4474 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
4478 ifm
= nlmsg_data(nlh
);
4479 ifm
->ifi_family
= AF_BRIDGE
;
4481 ifm
->ifi_type
= dev
->type
;
4482 ifm
->ifi_index
= dev
->ifindex
;
4483 ifm
->ifi_flags
= dev_get_flags(dev
);
4484 ifm
->ifi_change
= 0;
4487 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4488 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4489 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
4491 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
4493 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4494 (dev
->ifindex
!= dev_get_iflink(dev
) &&
4495 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
4496 goto nla_put_failure
;
4498 br_afspec
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
4500 goto nla_put_failure
;
4502 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
4503 nla_nest_cancel(skb
, br_afspec
);
4504 goto nla_put_failure
;
4507 if (mode
!= BRIDGE_MODE_UNDEF
) {
4508 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
4509 nla_nest_cancel(skb
, br_afspec
);
4510 goto nla_put_failure
;
4514 err
= vlan_fill(skb
, dev
, filter_mask
);
4516 nla_nest_cancel(skb
, br_afspec
);
4517 goto nla_put_failure
;
4520 nla_nest_end(skb
, br_afspec
);
4522 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4524 goto nla_put_failure
;
4526 if (brport_nla_put_flag(skb
, flags
, mask
,
4527 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
4528 brport_nla_put_flag(skb
, flags
, mask
,
4529 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
4530 brport_nla_put_flag(skb
, flags
, mask
,
4531 IFLA_BRPORT_FAST_LEAVE
,
4532 BR_MULTICAST_FAST_LEAVE
) ||
4533 brport_nla_put_flag(skb
, flags
, mask
,
4534 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
4535 brport_nla_put_flag(skb
, flags
, mask
,
4536 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
4537 brport_nla_put_flag(skb
, flags
, mask
,
4538 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
4539 brport_nla_put_flag(skb
, flags
, mask
,
4540 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
4541 brport_nla_put_flag(skb
, flags
, mask
,
4542 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
4543 nla_nest_cancel(skb
, protinfo
);
4544 goto nla_put_failure
;
4547 nla_nest_end(skb
, protinfo
);
4549 nlmsg_end(skb
, nlh
);
4552 nlmsg_cancel(skb
, nlh
);
4553 return err
? err
: -EMSGSIZE
;
4555 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
4557 static int valid_bridge_getlink_req(const struct nlmsghdr
*nlh
,
4558 bool strict_check
, u32
*filter_mask
,
4559 struct netlink_ext_ack
*extack
)
4561 struct nlattr
*tb
[IFLA_MAX
+1];
4565 struct ifinfomsg
*ifm
;
4567 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
4568 NL_SET_ERR_MSG(extack
, "Invalid header for bridge link dump");
4572 ifm
= nlmsg_data(nlh
);
4573 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
4574 ifm
->ifi_change
|| ifm
->ifi_index
) {
4575 NL_SET_ERR_MSG(extack
, "Invalid values in header for bridge link dump request");
4579 err
= nlmsg_parse_deprecated_strict(nlh
,
4580 sizeof(struct ifinfomsg
),
4581 tb
, IFLA_MAX
, ifla_policy
,
4584 err
= nlmsg_parse_deprecated(nlh
, sizeof(struct ifinfomsg
),
4585 tb
, IFLA_MAX
, ifla_policy
,
4591 /* new attributes should only be added with strict checking */
4592 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
4598 *filter_mask
= nla_get_u32(tb
[i
]);
4602 NL_SET_ERR_MSG(extack
, "Unsupported attribute in bridge link dump request");
4611 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4613 const struct nlmsghdr
*nlh
= cb
->nlh
;
4614 struct net
*net
= sock_net(skb
->sk
);
4615 struct net_device
*dev
;
4617 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
4618 u32 seq
= nlh
->nlmsg_seq
;
4619 u32 filter_mask
= 0;
4622 err
= valid_bridge_getlink_req(nlh
, cb
->strict_check
, &filter_mask
,
4624 if (err
< 0 && cb
->strict_check
)
4628 for_each_netdev_rcu(net
, dev
) {
4629 const struct net_device_ops
*ops
= dev
->netdev_ops
;
4630 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4632 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
4633 if (idx
>= cb
->args
[0]) {
4634 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
4635 skb
, portid
, seq
, dev
,
4636 filter_mask
, NLM_F_MULTI
);
4637 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4638 if (likely(skb
->len
))
4647 if (ops
->ndo_bridge_getlink
) {
4648 if (idx
>= cb
->args
[0]) {
4649 err
= ops
->ndo_bridge_getlink(skb
, portid
,
4653 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4654 if (likely(skb
->len
))
4671 static inline size_t bridge_nlmsg_size(void)
4673 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4674 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4675 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4676 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
4677 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
4678 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
4679 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
4680 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
4681 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
4682 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
4683 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
4686 static int rtnl_bridge_notify(struct net_device
*dev
)
4688 struct net
*net
= dev_net(dev
);
4689 struct sk_buff
*skb
;
4690 int err
= -EOPNOTSUPP
;
4692 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
4695 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
4701 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
4708 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
4711 WARN_ON(err
== -EMSGSIZE
);
4714 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4718 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4719 struct netlink_ext_ack
*extack
)
4721 struct net
*net
= sock_net(skb
->sk
);
4722 struct ifinfomsg
*ifm
;
4723 struct net_device
*dev
;
4724 struct nlattr
*br_spec
, *attr
= NULL
;
4725 int rem
, err
= -EOPNOTSUPP
;
4727 bool have_flags
= false;
4729 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4732 ifm
= nlmsg_data(nlh
);
4733 if (ifm
->ifi_family
!= AF_BRIDGE
)
4734 return -EPFNOSUPPORT
;
4736 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4738 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4742 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4744 nla_for_each_nested(attr
, br_spec
, rem
) {
4745 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4746 if (nla_len(attr
) < sizeof(flags
))
4750 flags
= nla_get_u16(attr
);
4756 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4757 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4759 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4764 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
,
4769 flags
&= ~BRIDGE_FLAGS_MASTER
;
4772 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4773 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4776 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4780 flags
&= ~BRIDGE_FLAGS_SELF
;
4782 /* Generate event to notify upper layer of bridge
4785 err
= rtnl_bridge_notify(dev
);
4790 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4795 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4796 struct netlink_ext_ack
*extack
)
4798 struct net
*net
= sock_net(skb
->sk
);
4799 struct ifinfomsg
*ifm
;
4800 struct net_device
*dev
;
4801 struct nlattr
*br_spec
, *attr
= NULL
;
4802 int rem
, err
= -EOPNOTSUPP
;
4804 bool have_flags
= false;
4806 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4809 ifm
= nlmsg_data(nlh
);
4810 if (ifm
->ifi_family
!= AF_BRIDGE
)
4811 return -EPFNOSUPPORT
;
4813 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4815 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4819 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4821 nla_for_each_nested(attr
, br_spec
, rem
) {
4822 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4823 if (nla_len(attr
) < sizeof(flags
))
4827 flags
= nla_get_u16(attr
);
4833 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4834 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4836 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4841 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4845 flags
&= ~BRIDGE_FLAGS_MASTER
;
4848 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4849 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4852 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4856 flags
&= ~BRIDGE_FLAGS_SELF
;
4858 /* Generate event to notify upper layer of bridge
4861 err
= rtnl_bridge_notify(dev
);
4866 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4871 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4873 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4874 (!idxattr
|| idxattr
== attrid
);
4877 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4878 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4881 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4882 return sizeof(struct rtnl_link_stats64
);
4888 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4891 struct nlattr
*attr
= NULL
;
4896 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4897 dev
->netdev_ops
->ndo_get_offload_stats
))
4900 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4901 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4902 if (attr_id
< *prividx
)
4905 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4909 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4912 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4913 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4915 goto nla_put_failure
;
4917 attr_data
= nla_data(attr
);
4918 memset(attr_data
, 0, size
);
4919 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4922 goto get_offload_stats_failure
;
4933 get_offload_stats_failure
:
4938 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4944 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4945 dev
->netdev_ops
->ndo_get_offload_stats
))
4948 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4949 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4950 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4952 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4953 nla_size
+= nla_total_size_64bit(size
);
4957 nla_size
+= nla_total_size(0);
4962 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4963 int type
, u32 pid
, u32 seq
, u32 change
,
4964 unsigned int flags
, unsigned int filter_mask
,
4965 int *idxattr
, int *prividx
)
4967 struct if_stats_msg
*ifsm
;
4968 struct nlmsghdr
*nlh
;
4969 struct nlattr
*attr
;
4970 int s_prividx
= *prividx
;
4975 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4979 ifsm
= nlmsg_data(nlh
);
4980 ifsm
->family
= PF_UNSPEC
;
4983 ifsm
->ifindex
= dev
->ifindex
;
4984 ifsm
->filter_mask
= filter_mask
;
4986 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4987 struct rtnl_link_stats64
*sp
;
4989 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4990 sizeof(struct rtnl_link_stats64
),
4993 goto nla_put_failure
;
4995 sp
= nla_data(attr
);
4996 dev_get_stats(dev
, sp
);
4999 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
5000 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
5002 if (ops
&& ops
->fill_linkxstats
) {
5003 *idxattr
= IFLA_STATS_LINK_XSTATS
;
5004 attr
= nla_nest_start_noflag(skb
,
5005 IFLA_STATS_LINK_XSTATS
);
5007 goto nla_put_failure
;
5009 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5010 nla_nest_end(skb
, attr
);
5012 goto nla_put_failure
;
5017 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
5019 const struct rtnl_link_ops
*ops
= NULL
;
5020 const struct net_device
*master
;
5022 master
= netdev_master_upper_dev_get(dev
);
5024 ops
= master
->rtnl_link_ops
;
5025 if (ops
&& ops
->fill_linkxstats
) {
5026 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
5027 attr
= nla_nest_start_noflag(skb
,
5028 IFLA_STATS_LINK_XSTATS_SLAVE
);
5030 goto nla_put_failure
;
5032 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
5033 nla_nest_end(skb
, attr
);
5035 goto nla_put_failure
;
5040 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
5042 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
5043 attr
= nla_nest_start_noflag(skb
,
5044 IFLA_STATS_LINK_OFFLOAD_XSTATS
);
5046 goto nla_put_failure
;
5048 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
5049 if (err
== -ENODATA
)
5050 nla_nest_cancel(skb
, attr
);
5052 nla_nest_end(skb
, attr
);
5054 if (err
&& err
!= -ENODATA
)
5055 goto nla_put_failure
;
5059 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
5060 struct rtnl_af_ops
*af_ops
;
5062 *idxattr
= IFLA_STATS_AF_SPEC
;
5063 attr
= nla_nest_start_noflag(skb
, IFLA_STATS_AF_SPEC
);
5065 goto nla_put_failure
;
5068 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
5069 if (af_ops
->fill_stats_af
) {
5073 af
= nla_nest_start_noflag(skb
,
5077 goto nla_put_failure
;
5079 err
= af_ops
->fill_stats_af(skb
, dev
);
5081 if (err
== -ENODATA
) {
5082 nla_nest_cancel(skb
, af
);
5083 } else if (err
< 0) {
5085 goto nla_put_failure
;
5088 nla_nest_end(skb
, af
);
5093 nla_nest_end(skb
, attr
);
5098 nlmsg_end(skb
, nlh
);
5103 /* not a multi message or no progress mean a real error */
5104 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
5105 nlmsg_cancel(skb
, nlh
);
5107 nlmsg_end(skb
, nlh
);
5112 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
5117 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
5118 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
5120 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
5121 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
5122 int attr
= IFLA_STATS_LINK_XSTATS
;
5124 if (ops
&& ops
->get_linkxstats_size
) {
5125 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
5127 /* for IFLA_STATS_LINK_XSTATS */
5128 size
+= nla_total_size(0);
5132 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
5133 struct net_device
*_dev
= (struct net_device
*)dev
;
5134 const struct rtnl_link_ops
*ops
= NULL
;
5135 const struct net_device
*master
;
5137 /* netdev_master_upper_dev_get can't take const */
5138 master
= netdev_master_upper_dev_get(_dev
);
5140 ops
= master
->rtnl_link_ops
;
5141 if (ops
&& ops
->get_linkxstats_size
) {
5142 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
5144 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
5146 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5147 size
+= nla_total_size(0);
5151 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
5152 size
+= rtnl_get_offload_stats_size(dev
);
5154 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
5155 struct rtnl_af_ops
*af_ops
;
5157 /* for IFLA_STATS_AF_SPEC */
5158 size
+= nla_total_size(0);
5161 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
5162 if (af_ops
->get_stats_af_size
) {
5163 size
+= nla_total_size(
5164 af_ops
->get_stats_af_size(dev
));
5167 size
+= nla_total_size(0);
5176 static int rtnl_valid_stats_req(const struct nlmsghdr
*nlh
, bool strict_check
,
5177 bool is_dump
, struct netlink_ext_ack
*extack
)
5179 struct if_stats_msg
*ifsm
;
5181 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifsm
))) {
5182 NL_SET_ERR_MSG(extack
, "Invalid header for stats dump");
5189 ifsm
= nlmsg_data(nlh
);
5191 /* only requests using strict checks can pass data to influence
5192 * the dump. The legacy exception is filter_mask.
5194 if (ifsm
->pad1
|| ifsm
->pad2
|| (is_dump
&& ifsm
->ifindex
)) {
5195 NL_SET_ERR_MSG(extack
, "Invalid values in header for stats dump request");
5198 if (nlmsg_attrlen(nlh
, sizeof(*ifsm
))) {
5199 NL_SET_ERR_MSG(extack
, "Invalid attributes after stats header");
5202 if (ifsm
->filter_mask
>= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX
+ 1)) {
5203 NL_SET_ERR_MSG(extack
, "Invalid stats requested through filter mask");
5210 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5211 struct netlink_ext_ack
*extack
)
5213 struct net
*net
= sock_net(skb
->sk
);
5214 struct net_device
*dev
= NULL
;
5215 int idxattr
= 0, prividx
= 0;
5216 struct if_stats_msg
*ifsm
;
5217 struct sk_buff
*nskb
;
5221 err
= rtnl_valid_stats_req(nlh
, netlink_strict_get_check(skb
),
5226 ifsm
= nlmsg_data(nlh
);
5227 if (ifsm
->ifindex
> 0)
5228 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
5235 filter_mask
= ifsm
->filter_mask
;
5239 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
5243 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
5244 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
5245 0, filter_mask
, &idxattr
, &prividx
);
5247 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5248 WARN_ON(err
== -EMSGSIZE
);
5251 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
5257 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5259 struct netlink_ext_ack
*extack
= cb
->extack
;
5260 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
5261 struct net
*net
= sock_net(skb
->sk
);
5262 unsigned int flags
= NLM_F_MULTI
;
5263 struct if_stats_msg
*ifsm
;
5264 struct hlist_head
*head
;
5265 struct net_device
*dev
;
5266 u32 filter_mask
= 0;
5270 s_idx
= cb
->args
[1];
5271 s_idxattr
= cb
->args
[2];
5272 s_prividx
= cb
->args
[3];
5274 cb
->seq
= net
->dev_base_seq
;
5276 err
= rtnl_valid_stats_req(cb
->nlh
, cb
->strict_check
, true, extack
);
5280 ifsm
= nlmsg_data(cb
->nlh
);
5281 filter_mask
= ifsm
->filter_mask
;
5283 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats dump");
5287 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5289 head
= &net
->dev_index_head
[h
];
5290 hlist_for_each_entry(dev
, head
, index_hlist
) {
5293 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
5294 NETLINK_CB(cb
->skb
).portid
,
5295 cb
->nlh
->nlmsg_seq
, 0,
5297 &s_idxattr
, &s_prividx
);
5298 /* If we ran out of room on the first message,
5301 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
5307 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5313 cb
->args
[3] = s_prividx
;
5314 cb
->args
[2] = s_idxattr
;
5321 /* Process one rtnetlink message. */
5323 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5324 struct netlink_ext_ack
*extack
)
5326 struct net
*net
= sock_net(skb
->sk
);
5327 struct rtnl_link
*link
;
5328 struct module
*owner
;
5329 int err
= -EOPNOTSUPP
;
5330 rtnl_doit_func doit
;
5336 type
= nlh
->nlmsg_type
;
5342 /* All the messages must have at least 1 byte length */
5343 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
5346 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
5349 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
5353 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
5355 rtnl_dumpit_func dumpit
;
5356 u16 min_dump_alloc
= 0;
5358 link
= rtnl_get_link(family
, type
);
5359 if (!link
|| !link
->dumpit
) {
5361 link
= rtnl_get_link(family
, type
);
5362 if (!link
|| !link
->dumpit
)
5365 owner
= link
->owner
;
5366 dumpit
= link
->dumpit
;
5368 if (type
== RTM_GETLINK
- RTM_BASE
)
5369 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
5372 /* need to do this before rcu_read_unlock() */
5373 if (!try_module_get(owner
))
5374 err
= -EPROTONOSUPPORT
;
5380 struct netlink_dump_control c
= {
5382 .min_dump_alloc
= min_dump_alloc
,
5385 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
5386 /* netlink_dump_start() will keep a reference on
5387 * module if dump is still in progress.
5394 link
= rtnl_get_link(family
, type
);
5395 if (!link
|| !link
->doit
) {
5397 link
= rtnl_get_link(PF_UNSPEC
, type
);
5398 if (!link
|| !link
->doit
)
5402 owner
= link
->owner
;
5403 if (!try_module_get(owner
)) {
5404 err
= -EPROTONOSUPPORT
;
5408 flags
= link
->flags
;
5409 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
5413 err
= doit(skb
, nlh
, extack
);
5420 link
= rtnl_get_link(family
, type
);
5421 if (link
&& link
->doit
)
5422 err
= link
->doit(skb
, nlh
, extack
);
5438 static void rtnetlink_rcv(struct sk_buff
*skb
)
5440 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
5443 static int rtnetlink_bind(struct net
*net
, int group
)
5446 case RTNLGRP_IPV4_MROUTE_R
:
5447 case RTNLGRP_IPV6_MROUTE_R
:
5448 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
5455 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
5457 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
5461 case NETDEV_CHANGEMTU
:
5462 case NETDEV_CHANGEADDR
:
5463 case NETDEV_CHANGENAME
:
5464 case NETDEV_FEAT_CHANGE
:
5465 case NETDEV_BONDING_FAILOVER
:
5466 case NETDEV_POST_TYPE_CHANGE
:
5467 case NETDEV_NOTIFY_PEERS
:
5468 case NETDEV_CHANGEUPPER
:
5469 case NETDEV_RESEND_IGMP
:
5470 case NETDEV_CHANGEINFODATA
:
5471 case NETDEV_CHANGELOWERSTATE
:
5472 case NETDEV_CHANGE_TX_QUEUE_LEN
:
5473 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
5474 GFP_KERNEL
, NULL
, 0);
5482 static struct notifier_block rtnetlink_dev_notifier
= {
5483 .notifier_call
= rtnetlink_event
,
5487 static int __net_init
rtnetlink_net_init(struct net
*net
)
5490 struct netlink_kernel_cfg cfg
= {
5491 .groups
= RTNLGRP_MAX
,
5492 .input
= rtnetlink_rcv
,
5493 .cb_mutex
= &rtnl_mutex
,
5494 .flags
= NL_CFG_F_NONROOT_RECV
,
5495 .bind
= rtnetlink_bind
,
5498 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
5505 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
5507 netlink_kernel_release(net
->rtnl
);
5511 static struct pernet_operations rtnetlink_net_ops
= {
5512 .init
= rtnetlink_net_init
,
5513 .exit
= rtnetlink_net_exit
,
5516 void __init
rtnetlink_init(void)
5518 if (register_pernet_subsys(&rtnetlink_net_ops
))
5519 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5521 register_netdevice_notifier(&rtnetlink_dev_notifier
);
5523 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
5524 rtnl_dump_ifinfo
, 0);
5525 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
5526 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
5527 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
5529 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
5530 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
5531 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
5533 rtnl_register(PF_UNSPEC
, RTM_NEWLINKPROP
, rtnl_newlinkprop
, NULL
, 0);
5534 rtnl_register(PF_UNSPEC
, RTM_DELLINKPROP
, rtnl_dellinkprop
, NULL
, 0);
5536 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
5537 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
5538 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, rtnl_fdb_get
, rtnl_fdb_dump
, 0);
5540 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
5541 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
5542 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
5544 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,