2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
45 #include <linux/uaccess.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
51 #include <net/protocol.h>
53 #include <net/route.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
62 #define RTNL_MAX_TYPE 49
63 #define RTNL_SLAVE_MAX_TYPE 36
67 rtnl_dumpit_func dumpit
;
73 static DEFINE_MUTEX(rtnl_mutex
);
77 mutex_lock(&rtnl_mutex
);
79 EXPORT_SYMBOL(rtnl_lock
);
81 int rtnl_lock_killable(void)
83 return mutex_lock_killable(&rtnl_mutex
);
85 EXPORT_SYMBOL(rtnl_lock_killable
);
87 static struct sk_buff
*defer_kfree_skb_list
;
88 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
91 tail
->next
= defer_kfree_skb_list
;
92 defer_kfree_skb_list
= head
;
95 EXPORT_SYMBOL(rtnl_kfree_skbs
);
97 void __rtnl_unlock(void)
99 struct sk_buff
*head
= defer_kfree_skb_list
;
101 defer_kfree_skb_list
= NULL
;
103 mutex_unlock(&rtnl_mutex
);
106 struct sk_buff
*next
= head
->next
;
114 void rtnl_unlock(void)
116 /* This fellow will unlock it for us. */
119 EXPORT_SYMBOL(rtnl_unlock
);
121 int rtnl_trylock(void)
123 return mutex_trylock(&rtnl_mutex
);
125 EXPORT_SYMBOL(rtnl_trylock
);
127 int rtnl_is_locked(void)
129 return mutex_is_locked(&rtnl_mutex
);
131 EXPORT_SYMBOL(rtnl_is_locked
);
133 bool refcount_dec_and_rtnl_lock(refcount_t
*r
)
135 return refcount_dec_and_mutex_lock(r
, &rtnl_mutex
);
137 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock
);
139 #ifdef CONFIG_PROVE_LOCKING
140 bool lockdep_rtnl_is_held(void)
142 return lockdep_is_held(&rtnl_mutex
);
144 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
145 #endif /* #ifdef CONFIG_PROVE_LOCKING */
147 static struct rtnl_link
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
149 static inline int rtm_msgindex(int msgtype
)
151 int msgindex
= msgtype
- RTM_BASE
;
154 * msgindex < 0 implies someone tried to register a netlink
155 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
156 * the message type has not been added to linux/rtnetlink.h
158 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
163 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
165 struct rtnl_link
**tab
;
167 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
168 protocol
= PF_UNSPEC
;
170 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
172 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
177 static int rtnl_register_internal(struct module
*owner
,
178 int protocol
, int msgtype
,
179 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
182 struct rtnl_link
*link
, *old
;
183 struct rtnl_link __rcu
**tab
;
187 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
188 msgindex
= rtm_msgindex(msgtype
);
191 tab
= rtnl_msg_handlers
[protocol
];
193 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
197 /* ensures we see the 0 stores */
198 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
201 old
= rtnl_dereference(tab
[msgindex
]);
203 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
207 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
212 WARN_ON(link
->owner
&& link
->owner
!= owner
);
215 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
218 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
220 link
->dumpit
= dumpit
;
222 link
->flags
|= flags
;
224 /* publish protocol:msgtype */
225 rcu_assign_pointer(tab
[msgindex
], link
);
235 * rtnl_register_module - Register a rtnetlink message type
237 * @owner: module registering the hook (THIS_MODULE)
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 * @doit: Function pointer called for each request message
241 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
242 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
244 * Like rtnl_register, but for use by removable modules.
246 int rtnl_register_module(struct module
*owner
,
247 int protocol
, int msgtype
,
248 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
251 return rtnl_register_internal(owner
, protocol
, msgtype
,
252 doit
, dumpit
, flags
);
254 EXPORT_SYMBOL_GPL(rtnl_register_module
);
257 * rtnl_register - Register a rtnetlink message type
258 * @protocol: Protocol family or PF_UNSPEC
259 * @msgtype: rtnetlink message type
260 * @doit: Function pointer called for each request message
261 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
262 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
264 * Registers the specified function pointers (at least one of them has
265 * to be non-NULL) to be called whenever a request message for the
266 * specified protocol family and message type is received.
268 * The special protocol family PF_UNSPEC may be used to define fallback
269 * function pointers for the case when no entry for the specific protocol
272 void rtnl_register(int protocol
, int msgtype
,
273 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
278 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
281 pr_err("Unable to register rtnetlink message handler, "
282 "protocol = %d, message type = %d\n", protocol
, msgtype
);
286 * rtnl_unregister - Unregister a rtnetlink message type
287 * @protocol: Protocol family or PF_UNSPEC
288 * @msgtype: rtnetlink message type
290 * Returns 0 on success or a negative error code.
292 int rtnl_unregister(int protocol
, int msgtype
)
294 struct rtnl_link
**tab
, *link
;
297 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
298 msgindex
= rtm_msgindex(msgtype
);
301 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
307 link
= tab
[msgindex
];
308 rcu_assign_pointer(tab
[msgindex
], NULL
);
311 kfree_rcu(link
, rcu
);
315 EXPORT_SYMBOL_GPL(rtnl_unregister
);
318 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
319 * @protocol : Protocol family or PF_UNSPEC
321 * Identical to calling rtnl_unregster() for all registered message types
322 * of a certain protocol family.
324 void rtnl_unregister_all(int protocol
)
326 struct rtnl_link
**tab
, *link
;
329 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
332 tab
= rtnl_msg_handlers
[protocol
];
337 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
338 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
339 link
= tab
[msgindex
];
343 rcu_assign_pointer(tab
[msgindex
], NULL
);
344 kfree_rcu(link
, rcu
);
352 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
354 static LIST_HEAD(link_ops
);
356 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
358 const struct rtnl_link_ops
*ops
;
360 list_for_each_entry(ops
, &link_ops
, list
) {
361 if (!strcmp(ops
->kind
, kind
))
368 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
369 * @ops: struct rtnl_link_ops * to register
371 * The caller must hold the rtnl_mutex. This function should be used
372 * by drivers that create devices during module initialization. It
373 * must be called before registering the devices.
375 * Returns 0 on success or a negative error code.
377 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
379 if (rtnl_link_ops_get(ops
->kind
))
382 /* The check for setup is here because if ops
383 * does not have that filled up, it is not possible
384 * to use the ops for creating device. So do not
385 * fill up dellink as well. That disables rtnl_dellink.
387 if (ops
->setup
&& !ops
->dellink
)
388 ops
->dellink
= unregister_netdevice_queue
;
390 list_add_tail(&ops
->list
, &link_ops
);
393 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
396 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
397 * @ops: struct rtnl_link_ops * to register
399 * Returns 0 on success or a negative error code.
401 int rtnl_link_register(struct rtnl_link_ops
*ops
)
405 /* Sanity-check max sizes to avoid stack buffer overflow. */
406 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
407 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
411 err
= __rtnl_link_register(ops
);
415 EXPORT_SYMBOL_GPL(rtnl_link_register
);
417 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
419 struct net_device
*dev
;
420 LIST_HEAD(list_kill
);
422 for_each_netdev(net
, dev
) {
423 if (dev
->rtnl_link_ops
== ops
)
424 ops
->dellink(dev
, &list_kill
);
426 unregister_netdevice_many(&list_kill
);
430 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
431 * @ops: struct rtnl_link_ops * to unregister
433 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
434 * integrity (hold pernet_ops_rwsem for writing to close the race
435 * with setup_net() and cleanup_net()).
437 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
442 __rtnl_kill_links(net
, ops
);
444 list_del(&ops
->list
);
446 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
448 /* Return with the rtnl_lock held when there are no network
449 * devices unregistering in any network namespace.
451 static void rtnl_lock_unregistering_all(void)
455 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
457 add_wait_queue(&netdev_unregistering_wq
, &wait
);
459 unregistering
= false;
461 /* We held write locked pernet_ops_rwsem, and parallel
462 * setup_net() and cleanup_net() are not possible.
465 if (net
->dev_unreg_count
> 0) {
466 unregistering
= true;
474 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
476 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
480 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
481 * @ops: struct rtnl_link_ops * to unregister
483 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
485 /* Close the race with setup_net() and cleanup_net() */
486 down_write(&pernet_ops_rwsem
);
487 rtnl_lock_unregistering_all();
488 __rtnl_link_unregister(ops
);
490 up_write(&pernet_ops_rwsem
);
492 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
494 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
496 struct net_device
*master_dev
;
497 const struct rtnl_link_ops
*ops
;
502 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
506 ops
= master_dev
->rtnl_link_ops
;
507 if (!ops
|| !ops
->get_slave_size
)
509 /* IFLA_INFO_SLAVE_DATA + nested data */
510 size
= nla_total_size(sizeof(struct nlattr
)) +
511 ops
->get_slave_size(master_dev
, dev
);
518 static size_t rtnl_link_get_size(const struct net_device
*dev
)
520 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
526 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
527 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
530 /* IFLA_INFO_DATA + nested data */
531 size
+= nla_total_size(sizeof(struct nlattr
)) +
534 if (ops
->get_xstats_size
)
535 /* IFLA_INFO_XSTATS */
536 size
+= nla_total_size(ops
->get_xstats_size(dev
));
538 size
+= rtnl_link_get_slave_info_data_size(dev
);
543 static LIST_HEAD(rtnl_af_ops
);
545 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
547 const struct rtnl_af_ops
*ops
;
549 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
550 if (ops
->family
== family
)
558 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
559 * @ops: struct rtnl_af_ops * to register
561 * Returns 0 on success or a negative error code.
563 void rtnl_af_register(struct rtnl_af_ops
*ops
)
566 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
569 EXPORT_SYMBOL_GPL(rtnl_af_register
);
572 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
573 * @ops: struct rtnl_af_ops * to unregister
575 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
578 list_del_rcu(&ops
->list
);
583 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
585 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
588 struct rtnl_af_ops
*af_ops
;
592 size
= nla_total_size(sizeof(struct nlattr
));
595 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
596 if (af_ops
->get_link_af_size
) {
597 /* AF_* + nested data */
598 size
+= nla_total_size(sizeof(struct nlattr
)) +
599 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
607 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
609 struct net_device
*master_dev
;
614 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
615 if (master_dev
&& master_dev
->rtnl_link_ops
)
621 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
622 const struct net_device
*dev
)
624 struct net_device
*master_dev
;
625 const struct rtnl_link_ops
*ops
;
626 struct nlattr
*slave_data
;
629 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
632 ops
= master_dev
->rtnl_link_ops
;
635 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
637 if (ops
->fill_slave_info
) {
638 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
641 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
643 goto err_cancel_slave_data
;
644 nla_nest_end(skb
, slave_data
);
648 err_cancel_slave_data
:
649 nla_nest_cancel(skb
, slave_data
);
653 static int rtnl_link_info_fill(struct sk_buff
*skb
,
654 const struct net_device
*dev
)
656 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
662 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
664 if (ops
->fill_xstats
) {
665 err
= ops
->fill_xstats(skb
, dev
);
669 if (ops
->fill_info
) {
670 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
673 err
= ops
->fill_info(skb
, dev
);
675 goto err_cancel_data
;
676 nla_nest_end(skb
, data
);
681 nla_nest_cancel(skb
, data
);
685 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
687 struct nlattr
*linkinfo
;
690 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
691 if (linkinfo
== NULL
)
694 err
= rtnl_link_info_fill(skb
, dev
);
696 goto err_cancel_link
;
698 err
= rtnl_link_slave_info_fill(skb
, dev
);
700 goto err_cancel_link
;
702 nla_nest_end(skb
, linkinfo
);
706 nla_nest_cancel(skb
, linkinfo
);
711 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
713 struct sock
*rtnl
= net
->rtnl
;
716 NETLINK_CB(skb
).dst_group
= group
;
718 refcount_inc(&skb
->users
);
719 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
721 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
725 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
727 struct sock
*rtnl
= net
->rtnl
;
729 return nlmsg_unicast(rtnl
, skb
, pid
);
731 EXPORT_SYMBOL(rtnl_unicast
);
733 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
734 struct nlmsghdr
*nlh
, gfp_t flags
)
736 struct sock
*rtnl
= net
->rtnl
;
740 report
= nlmsg_report(nlh
);
742 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
744 EXPORT_SYMBOL(rtnl_notify
);
746 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
748 struct sock
*rtnl
= net
->rtnl
;
750 netlink_set_err(rtnl
, 0, group
, error
);
752 EXPORT_SYMBOL(rtnl_set_sk_err
);
754 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
759 mx
= nla_nest_start(skb
, RTA_METRICS
);
763 for (i
= 0; i
< RTAX_MAX
; i
++) {
765 if (i
== RTAX_CC_ALGO
- 1) {
766 char tmp
[TCP_CA_NAME_MAX
], *name
;
768 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
771 if (nla_put_string(skb
, i
+ 1, name
))
772 goto nla_put_failure
;
773 } else if (i
== RTAX_FEATURES
- 1) {
774 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
778 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
779 if (nla_put_u32(skb
, i
+ 1, user_features
))
780 goto nla_put_failure
;
782 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
783 goto nla_put_failure
;
790 nla_nest_cancel(skb
, mx
);
794 return nla_nest_end(skb
, mx
);
797 nla_nest_cancel(skb
, mx
);
800 EXPORT_SYMBOL(rtnetlink_put_metrics
);
802 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
803 long expires
, u32 error
)
805 struct rta_cacheinfo ci
= {
811 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
812 ci
.rta_used
= dst
->__use
;
813 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
818 clock
= jiffies_to_clock_t(abs(expires
));
819 clock
= min_t(unsigned long, clock
, INT_MAX
);
820 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
822 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
824 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
826 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
828 unsigned char operstate
= dev
->operstate
;
830 switch (transition
) {
832 if ((operstate
== IF_OPER_DORMANT
||
833 operstate
== IF_OPER_UNKNOWN
) &&
835 operstate
= IF_OPER_UP
;
838 case IF_OPER_DORMANT
:
839 if (operstate
== IF_OPER_UP
||
840 operstate
== IF_OPER_UNKNOWN
)
841 operstate
= IF_OPER_DORMANT
;
845 if (dev
->operstate
!= operstate
) {
846 write_lock_bh(&dev_base_lock
);
847 dev
->operstate
= operstate
;
848 write_unlock_bh(&dev_base_lock
);
849 netdev_state_change(dev
);
853 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
855 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
856 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
859 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
860 const struct ifinfomsg
*ifm
)
862 unsigned int flags
= ifm
->ifi_flags
;
864 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
866 flags
= (flags
& ifm
->ifi_change
) |
867 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
872 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
873 const struct rtnl_link_stats64
*b
)
875 a
->rx_packets
= b
->rx_packets
;
876 a
->tx_packets
= b
->tx_packets
;
877 a
->rx_bytes
= b
->rx_bytes
;
878 a
->tx_bytes
= b
->tx_bytes
;
879 a
->rx_errors
= b
->rx_errors
;
880 a
->tx_errors
= b
->tx_errors
;
881 a
->rx_dropped
= b
->rx_dropped
;
882 a
->tx_dropped
= b
->tx_dropped
;
884 a
->multicast
= b
->multicast
;
885 a
->collisions
= b
->collisions
;
887 a
->rx_length_errors
= b
->rx_length_errors
;
888 a
->rx_over_errors
= b
->rx_over_errors
;
889 a
->rx_crc_errors
= b
->rx_crc_errors
;
890 a
->rx_frame_errors
= b
->rx_frame_errors
;
891 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
892 a
->rx_missed_errors
= b
->rx_missed_errors
;
894 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
895 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
896 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
897 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
898 a
->tx_window_errors
= b
->tx_window_errors
;
900 a
->rx_compressed
= b
->rx_compressed
;
901 a
->tx_compressed
= b
->tx_compressed
;
903 a
->rx_nohandler
= b
->rx_nohandler
;
907 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
910 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
911 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
912 size_t size
= nla_total_size(0);
915 nla_total_size(sizeof(struct ifla_vf_mac
)) +
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 noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
986 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
987 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
988 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
989 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
990 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
991 + nla_total_size(sizeof(struct rtnl_link_stats
))
992 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
993 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
994 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
995 + nla_total_size(4) /* IFLA_TXQLEN */
996 + nla_total_size(4) /* IFLA_WEIGHT */
997 + nla_total_size(4) /* IFLA_MTU */
998 + nla_total_size(4) /* IFLA_LINK */
999 + nla_total_size(4) /* IFLA_MASTER */
1000 + nla_total_size(1) /* IFLA_CARRIER */
1001 + nla_total_size(4) /* IFLA_PROMISCUITY */
1002 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1003 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1004 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1005 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1006 + nla_total_size(1) /* IFLA_OPERSTATE */
1007 + nla_total_size(1) /* IFLA_LINKMODE */
1008 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1009 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1010 + nla_total_size(4) /* IFLA_GROUP */
1011 + nla_total_size(ext_filter_mask
1012 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1013 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1014 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1015 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1016 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1017 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1018 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1019 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1020 + rtnl_xdp_size() /* IFLA_XDP */
1021 + nla_total_size(4) /* IFLA_EVENT */
1022 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1023 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1024 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1025 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1026 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1027 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1028 + nla_total_size(4) /* IFLA_MIN_MTU */
1029 + nla_total_size(4) /* IFLA_MAX_MTU */
1033 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1035 struct nlattr
*vf_ports
;
1036 struct nlattr
*vf_port
;
1040 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
1044 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1045 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
1047 goto nla_put_failure
;
1048 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1049 goto nla_put_failure
;
1050 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1051 if (err
== -EMSGSIZE
)
1052 goto nla_put_failure
;
1054 nla_nest_cancel(skb
, vf_port
);
1057 nla_nest_end(skb
, vf_port
);
1060 nla_nest_end(skb
, vf_ports
);
1065 nla_nest_cancel(skb
, vf_ports
);
1069 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1071 struct nlattr
*port_self
;
1074 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
1078 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1080 nla_nest_cancel(skb
, port_self
);
1081 return (err
== -EMSGSIZE
) ? err
: 0;
1084 nla_nest_end(skb
, port_self
);
1089 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1090 u32 ext_filter_mask
)
1094 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1095 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1098 err
= rtnl_port_self_fill(skb
, dev
);
1102 if (dev_num_vf(dev
->dev
.parent
)) {
1103 err
= rtnl_vf_ports_fill(skb
, dev
);
1111 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1114 struct netdev_phys_item_id ppid
;
1116 err
= dev_get_phys_port_id(dev
, &ppid
);
1118 if (err
== -EOPNOTSUPP
)
1123 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1129 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1131 char name
[IFNAMSIZ
];
1134 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1136 if (err
== -EOPNOTSUPP
)
1141 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1147 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1150 struct switchdev_attr attr
= {
1152 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1153 .flags
= SWITCHDEV_F_NO_RECURSE
,
1156 err
= switchdev_port_attr_get(dev
, &attr
);
1158 if (err
== -EOPNOTSUPP
)
1163 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1170 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1171 struct net_device
*dev
)
1173 struct rtnl_link_stats64
*sp
;
1174 struct nlattr
*attr
;
1176 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1177 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1181 sp
= nla_data(attr
);
1182 dev_get_stats(dev
, sp
);
1184 attr
= nla_reserve(skb
, IFLA_STATS
,
1185 sizeof(struct rtnl_link_stats
));
1189 copy_rtnl_link_stats(nla_data(attr
), sp
);
1194 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1195 struct net_device
*dev
,
1197 struct nlattr
*vfinfo
)
1199 struct ifla_vf_rss_query_en vf_rss_query_en
;
1200 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1201 struct ifla_vf_link_state vf_linkstate
;
1202 struct ifla_vf_vlan_info vf_vlan_info
;
1203 struct ifla_vf_spoofchk vf_spoofchk
;
1204 struct ifla_vf_tx_rate vf_tx_rate
;
1205 struct ifla_vf_stats vf_stats
;
1206 struct ifla_vf_trust vf_trust
;
1207 struct ifla_vf_vlan vf_vlan
;
1208 struct ifla_vf_rate vf_rate
;
1209 struct ifla_vf_mac vf_mac
;
1210 struct ifla_vf_info ivi
;
1212 memset(&ivi
, 0, sizeof(ivi
));
1214 /* Not all SR-IOV capable drivers support the
1215 * spoofcheck and "RSS query enable" query. Preset to
1216 * -1 so the user space tool can detect that the driver
1217 * didn't report anything.
1220 ivi
.rss_query_en
= -1;
1222 /* The default value for VF link state is "auto"
1223 * IFLA_VF_LINK_STATE_AUTO which equals zero
1226 /* VLAN Protocol by default is 802.1Q */
1227 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1228 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1231 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1240 vf_rss_query_en
.vf
=
1241 vf_trust
.vf
= ivi
.vf
;
1243 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1244 vf_vlan
.vlan
= ivi
.vlan
;
1245 vf_vlan
.qos
= ivi
.qos
;
1246 vf_vlan_info
.vlan
= ivi
.vlan
;
1247 vf_vlan_info
.qos
= ivi
.qos
;
1248 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1249 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1250 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1251 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1252 vf_spoofchk
.setting
= ivi
.spoofchk
;
1253 vf_linkstate
.link_state
= ivi
.linkstate
;
1254 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1255 vf_trust
.setting
= ivi
.trusted
;
1256 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1258 goto nla_put_vfinfo_failure
;
1259 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1260 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1261 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1263 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1265 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1267 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1269 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1270 sizeof(vf_rss_query_en
),
1271 &vf_rss_query_en
) ||
1272 nla_put(skb
, IFLA_VF_TRUST
,
1273 sizeof(vf_trust
), &vf_trust
))
1274 goto nla_put_vf_failure
;
1275 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1277 goto nla_put_vf_failure
;
1278 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1280 nla_nest_cancel(skb
, vfvlanlist
);
1281 goto nla_put_vf_failure
;
1283 nla_nest_end(skb
, vfvlanlist
);
1284 memset(&vf_stats
, 0, sizeof(vf_stats
));
1285 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1286 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1288 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1290 goto nla_put_vf_failure
;
1291 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1292 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1293 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1294 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1295 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1296 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1297 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1298 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1299 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1300 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1301 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1302 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1303 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1304 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1305 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1306 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1307 nla_nest_cancel(skb
, vfstats
);
1308 goto nla_put_vf_failure
;
1310 nla_nest_end(skb
, vfstats
);
1311 nla_nest_end(skb
, vf
);
1315 nla_nest_cancel(skb
, vf
);
1316 nla_put_vfinfo_failure
:
1317 nla_nest_cancel(skb
, vfinfo
);
1321 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1322 struct net_device
*dev
,
1323 u32 ext_filter_mask
)
1325 struct nlattr
*vfinfo
;
1328 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1331 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1332 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1335 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1338 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1342 for (i
= 0; i
< num_vfs
; i
++) {
1343 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1347 nla_nest_end(skb
, vfinfo
);
1351 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1353 struct rtnl_link_ifmap map
;
1355 memset(&map
, 0, sizeof(map
));
1356 map
.mem_start
= dev
->mem_start
;
1357 map
.mem_end
= dev
->mem_end
;
1358 map
.base_addr
= dev
->base_addr
;
1361 map
.port
= dev
->if_port
;
1363 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1369 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1371 const struct bpf_prog
*generic_xdp_prog
;
1375 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1376 if (!generic_xdp_prog
)
1378 return generic_xdp_prog
->aux
->id
;
1381 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1383 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
, XDP_QUERY_PROG
);
1386 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1388 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
,
1392 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1393 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1394 u32 (*get_prog_id
)(struct net_device
*dev
))
1399 curr_id
= get_prog_id(dev
);
1404 err
= nla_put_u32(skb
, attr
, curr_id
);
1408 if (*mode
!= XDP_ATTACHED_NONE
)
1409 *mode
= XDP_ATTACHED_MULTI
;
1416 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1423 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1428 mode
= XDP_ATTACHED_NONE
;
1429 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1430 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1433 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1434 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1437 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1438 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1442 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1446 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1447 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1452 nla_nest_end(skb
, xdp
);
1456 nla_nest_cancel(skb
, xdp
);
1460 static u32
rtnl_get_event(unsigned long event
)
1462 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1466 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1468 case NETDEV_FEAT_CHANGE
:
1469 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1471 case NETDEV_BONDING_FAILOVER
:
1472 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1474 case NETDEV_NOTIFY_PEERS
:
1475 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1477 case NETDEV_RESEND_IGMP
:
1478 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1480 case NETDEV_CHANGEINFODATA
:
1481 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1487 return rtnl_event_type
;
1490 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1492 const struct net_device
*upper_dev
;
1497 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1499 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1505 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
)
1507 int ifindex
= dev_get_iflink(dev
);
1509 if (dev
->ifindex
== ifindex
)
1512 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1515 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1516 struct net_device
*dev
)
1521 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1522 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1525 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1526 const struct net_device
*dev
,
1527 struct net
*src_net
)
1529 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1530 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1532 if (!net_eq(dev_net(dev
), link_net
)) {
1533 int id
= peernet2id_alloc(src_net
, link_net
);
1535 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1543 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1544 const struct net_device
*dev
,
1545 u32 ext_filter_mask
)
1547 const struct rtnl_af_ops
*af_ops
;
1548 struct nlattr
*af_spec
;
1550 af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
1554 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1558 if (!af_ops
->fill_link_af
)
1561 af
= nla_nest_start(skb
, af_ops
->family
);
1565 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1567 * Caller may return ENODATA to indicate that there
1568 * was no data to be dumped. This is not an error, it
1569 * means we should trim the attribute header and
1572 if (err
== -ENODATA
)
1573 nla_nest_cancel(skb
, af
);
1577 nla_nest_end(skb
, af
);
1580 nla_nest_end(skb
, af_spec
);
1584 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1585 struct net_device
*dev
, struct net
*src_net
,
1586 int type
, u32 pid
, u32 seq
, u32 change
,
1587 unsigned int flags
, u32 ext_filter_mask
,
1588 u32 event
, int *new_nsid
, int new_ifindex
,
1591 struct ifinfomsg
*ifm
;
1592 struct nlmsghdr
*nlh
;
1595 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1599 ifm
= nlmsg_data(nlh
);
1600 ifm
->ifi_family
= AF_UNSPEC
;
1602 ifm
->ifi_type
= dev
->type
;
1603 ifm
->ifi_index
= dev
->ifindex
;
1604 ifm
->ifi_flags
= dev_get_flags(dev
);
1605 ifm
->ifi_change
= change
;
1607 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_TARGET_NETNSID
, tgt_netnsid
))
1608 goto nla_put_failure
;
1610 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1611 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1612 nla_put_u8(skb
, IFLA_OPERSTATE
,
1613 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1614 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1615 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1616 nla_put_u32(skb
, IFLA_MIN_MTU
, dev
->min_mtu
) ||
1617 nla_put_u32(skb
, IFLA_MAX_MTU
, dev
->max_mtu
) ||
1618 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1619 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1620 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1621 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1622 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1624 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1626 nla_put_iflink(skb
, dev
) ||
1627 put_master_ifindex(skb
, dev
) ||
1628 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1630 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1631 nla_put_ifalias(skb
, dev
) ||
1632 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1633 atomic_read(&dev
->carrier_up_count
) +
1634 atomic_read(&dev
->carrier_down_count
)) ||
1635 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1636 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1637 atomic_read(&dev
->carrier_up_count
)) ||
1638 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1639 atomic_read(&dev
->carrier_down_count
)))
1640 goto nla_put_failure
;
1642 if (event
!= IFLA_EVENT_NONE
) {
1643 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1644 goto nla_put_failure
;
1647 if (rtnl_fill_link_ifmap(skb
, dev
))
1648 goto nla_put_failure
;
1650 if (dev
->addr_len
) {
1651 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1652 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1653 goto nla_put_failure
;
1656 if (rtnl_phys_port_id_fill(skb
, dev
))
1657 goto nla_put_failure
;
1659 if (rtnl_phys_port_name_fill(skb
, dev
))
1660 goto nla_put_failure
;
1662 if (rtnl_phys_switch_id_fill(skb
, dev
))
1663 goto nla_put_failure
;
1665 if (rtnl_fill_stats(skb
, dev
))
1666 goto nla_put_failure
;
1668 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1669 goto nla_put_failure
;
1671 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1672 goto nla_put_failure
;
1674 if (rtnl_xdp_fill(skb
, dev
))
1675 goto nla_put_failure
;
1677 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1678 if (rtnl_link_fill(skb
, dev
) < 0)
1679 goto nla_put_failure
;
1682 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
))
1683 goto nla_put_failure
;
1686 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1687 goto nla_put_failure
;
1689 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1690 goto nla_put_failure
;
1694 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1695 goto nla_put_failure_rcu
;
1698 nlmsg_end(skb
, nlh
);
1701 nla_put_failure_rcu
:
1704 nlmsg_cancel(skb
, nlh
);
1708 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1709 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1710 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1711 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1712 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1713 [IFLA_MTU
] = { .type
= NLA_U32
},
1714 [IFLA_LINK
] = { .type
= NLA_U32
},
1715 [IFLA_MASTER
] = { .type
= NLA_U32
},
1716 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1717 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1718 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1719 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1720 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1721 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1722 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1723 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1724 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1725 * allow 0-length string (needed to remove an alias).
1727 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1728 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1729 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1730 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1731 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1732 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1733 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1734 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1735 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1736 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1737 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1738 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1739 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1740 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1741 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1742 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1743 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1744 [IFLA_EVENT
] = { .type
= NLA_U32
},
1745 [IFLA_GROUP
] = { .type
= NLA_U32
},
1746 [IFLA_TARGET_NETNSID
] = { .type
= NLA_S32
},
1747 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1748 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1749 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
1750 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
1753 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1754 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1755 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1756 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1757 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1760 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1761 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1762 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1763 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1764 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1765 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1766 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1767 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1768 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1769 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1770 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1771 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1772 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1775 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1776 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1777 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1778 .len
= PORT_PROFILE_MAX
},
1779 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1780 .len
= PORT_UUID_MAX
},
1781 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1782 .len
= PORT_UUID_MAX
},
1783 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1784 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1786 /* Unused, but we need to keep it here since user space could
1787 * fill it. It's also broken with regard to NLA_BINARY use in
1788 * combination with structs.
1790 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1791 .len
= sizeof(struct ifla_port_vsi
) },
1794 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1795 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1796 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1797 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1798 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1801 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1803 const struct rtnl_link_ops
*ops
= NULL
;
1804 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1806 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1807 ifla_info_policy
, NULL
) < 0)
1810 if (linfo
[IFLA_INFO_KIND
]) {
1811 char kind
[MODULE_NAME_LEN
];
1813 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1814 ops
= rtnl_link_ops_get(kind
);
1820 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1822 struct net_device
*master
;
1827 master
= netdev_master_upper_dev_get(dev
);
1828 if (!master
|| master
->ifindex
!= master_idx
)
1834 static bool link_kind_filtered(const struct net_device
*dev
,
1835 const struct rtnl_link_ops
*kind_ops
)
1837 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1843 static bool link_dump_filtered(struct net_device
*dev
,
1845 const struct rtnl_link_ops
*kind_ops
)
1847 if (link_master_filtered(dev
, master_idx
) ||
1848 link_kind_filtered(dev
, kind_ops
))
1855 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1856 * @sk: netlink socket
1857 * @netnsid: network namespace identifier
1859 * Returns the network namespace identified by netnsid on success or an error
1860 * pointer on failure.
1862 struct net
*rtnl_get_net_ns_capable(struct sock
*sk
, int netnsid
)
1866 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1868 return ERR_PTR(-EINVAL
);
1870 /* For now, the caller is required to have CAP_NET_ADMIN in
1871 * the user namespace owning the target net ns.
1873 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1875 return ERR_PTR(-EACCES
);
1879 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable
);
1881 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr
*nlh
,
1882 bool strict_check
, struct nlattr
**tb
,
1883 struct netlink_ext_ack
*extack
)
1888 struct ifinfomsg
*ifm
;
1890 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
1891 NL_SET_ERR_MSG(extack
, "Invalid header for link dump");
1895 ifm
= nlmsg_data(nlh
);
1896 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
1898 NL_SET_ERR_MSG(extack
, "Invalid values in header for link dump request");
1901 if (ifm
->ifi_index
) {
1902 NL_SET_ERR_MSG(extack
, "Filter by device index not supported for link dumps");
1906 return nlmsg_parse_strict(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
,
1907 ifla_policy
, extack
);
1910 /* A hack to preserve kernel<->userspace interface.
1911 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1912 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1913 * what iproute2 < v3.9.0 used.
1914 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1915 * attribute, its netlink message is shorter than struct ifinfomsg.
1917 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
1918 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1920 return nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, extack
);
1923 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1925 struct netlink_ext_ack
*extack
= cb
->extack
;
1926 const struct nlmsghdr
*nlh
= cb
->nlh
;
1927 struct net
*net
= sock_net(skb
->sk
);
1928 struct net
*tgt_net
= net
;
1931 struct net_device
*dev
;
1932 struct hlist_head
*head
;
1933 struct nlattr
*tb
[IFLA_MAX
+1];
1934 u32 ext_filter_mask
= 0;
1935 const struct rtnl_link_ops
*kind_ops
= NULL
;
1936 unsigned int flags
= NLM_F_MULTI
;
1942 s_idx
= cb
->args
[1];
1944 err
= rtnl_valid_dump_ifinfo_req(nlh
, cb
->strict_check
, tb
, extack
);
1946 if (cb
->strict_check
)
1952 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
1956 /* new attributes should only be added with strict checking */
1958 case IFLA_TARGET_NETNSID
:
1959 netnsid
= nla_get_s32(tb
[i
]);
1960 tgt_net
= rtnl_get_net_ns_capable(skb
->sk
, netnsid
);
1961 if (IS_ERR(tgt_net
)) {
1962 NL_SET_ERR_MSG(extack
, "Invalid target network namespace id");
1963 return PTR_ERR(tgt_net
);
1967 ext_filter_mask
= nla_get_u32(tb
[i
]);
1970 master_idx
= nla_get_u32(tb
[i
]);
1973 kind_ops
= linkinfo_to_kind_ops(tb
[i
]);
1976 if (cb
->strict_check
) {
1977 NL_SET_ERR_MSG(extack
, "Unsupported attribute in link dump request");
1983 if (master_idx
|| kind_ops
)
1984 flags
|= NLM_F_DUMP_FILTERED
;
1987 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1989 head
= &tgt_net
->dev_index_head
[h
];
1990 hlist_for_each_entry(dev
, head
, index_hlist
) {
1991 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1995 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1997 NETLINK_CB(cb
->skb
).portid
,
1998 nlh
->nlmsg_seq
, 0, flags
,
1999 ext_filter_mask
, 0, NULL
, 0,
2003 if (likely(skb
->len
))
2017 cb
->seq
= net
->dev_base_seq
;
2018 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2025 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
2026 struct netlink_ext_ack
*exterr
)
2028 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
2030 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
2032 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
2035 /* Examine the link attributes and figure out which
2036 * network namespace we are talking about.
2038 if (tb
[IFLA_NET_NS_PID
])
2039 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
2040 else if (tb
[IFLA_NET_NS_FD
])
2041 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
2043 net
= get_net(src_net
);
2046 EXPORT_SYMBOL(rtnl_link_get_net
);
2048 /* Figure out which network namespace we are talking about by
2049 * examining the link attributes in the following order:
2051 * 1. IFLA_NET_NS_PID
2053 * 3. IFLA_TARGET_NETNSID
2055 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
2056 struct nlattr
*tb
[])
2060 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
2061 return rtnl_link_get_net(src_net
, tb
);
2063 if (!tb
[IFLA_TARGET_NETNSID
])
2064 return get_net(src_net
);
2066 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_TARGET_NETNSID
]));
2068 return ERR_PTR(-EINVAL
);
2073 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2074 struct net
*src_net
,
2075 struct nlattr
*tb
[], int cap
)
2079 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2083 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2085 return ERR_PTR(-EPERM
);
2091 /* Verify that rtnetlink requests do not pass additional properties
2092 * potentially referring to different network namespaces.
2094 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2095 struct netlink_ext_ack
*extack
,
2099 if (netns_id_only
) {
2100 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2103 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2107 if (tb
[IFLA_TARGET_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2110 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2113 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_TARGET_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2119 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2123 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2126 if (tb
[IFLA_ADDRESS
] &&
2127 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2130 if (tb
[IFLA_BROADCAST
] &&
2131 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2135 if (tb
[IFLA_AF_SPEC
]) {
2139 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2140 const struct rtnl_af_ops
*af_ops
;
2143 af_ops
= rtnl_af_lookup(nla_type(af
));
2146 return -EAFNOSUPPORT
;
2149 if (!af_ops
->set_link_af
) {
2154 if (af_ops
->validate_link_af
) {
2155 err
= af_ops
->validate_link_af(dev
, af
);
2169 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2172 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2174 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2177 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2179 if (dev
->type
!= ARPHRD_INFINIBAND
)
2182 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2185 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2187 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2190 if (tb
[IFLA_VF_MAC
]) {
2191 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2194 if (ops
->ndo_set_vf_mac
)
2195 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2201 if (tb
[IFLA_VF_VLAN
]) {
2202 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2205 if (ops
->ndo_set_vf_vlan
)
2206 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2208 htons(ETH_P_8021Q
));
2213 if (tb
[IFLA_VF_VLAN_LIST
]) {
2214 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2215 struct nlattr
*attr
;
2219 if (!ops
->ndo_set_vf_vlan
)
2222 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2223 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2224 nla_len(attr
) < NLA_HDRLEN
) {
2227 if (len
>= MAX_VLAN_LIST_LEN
)
2229 ivvl
[len
] = nla_data(attr
);
2236 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2237 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2242 if (tb
[IFLA_VF_TX_RATE
]) {
2243 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2244 struct ifla_vf_info ivf
;
2247 if (ops
->ndo_get_vf_config
)
2248 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2253 if (ops
->ndo_set_vf_rate
)
2254 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2261 if (tb
[IFLA_VF_RATE
]) {
2262 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2265 if (ops
->ndo_set_vf_rate
)
2266 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2273 if (tb
[IFLA_VF_SPOOFCHK
]) {
2274 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2277 if (ops
->ndo_set_vf_spoofchk
)
2278 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2284 if (tb
[IFLA_VF_LINK_STATE
]) {
2285 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2288 if (ops
->ndo_set_vf_link_state
)
2289 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2295 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2296 struct ifla_vf_rss_query_en
*ivrssq_en
;
2299 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2300 if (ops
->ndo_set_vf_rss_query_en
)
2301 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2302 ivrssq_en
->setting
);
2307 if (tb
[IFLA_VF_TRUST
]) {
2308 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2311 if (ops
->ndo_set_vf_trust
)
2312 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2317 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2318 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2320 if (!ops
->ndo_set_vf_guid
)
2323 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2326 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2327 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2329 if (!ops
->ndo_set_vf_guid
)
2332 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2338 static int do_set_master(struct net_device
*dev
, int ifindex
,
2339 struct netlink_ext_ack
*extack
)
2341 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2342 const struct net_device_ops
*ops
;
2346 if (upper_dev
->ifindex
== ifindex
)
2348 ops
= upper_dev
->netdev_ops
;
2349 if (ops
->ndo_del_slave
) {
2350 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2359 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2362 ops
= upper_dev
->netdev_ops
;
2363 if (ops
->ndo_add_slave
) {
2364 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2374 #define DO_SETLINK_MODIFIED 0x01
2375 /* notify flag means notify + modified. */
2376 #define DO_SETLINK_NOTIFY 0x03
2377 static int do_setlink(const struct sk_buff
*skb
,
2378 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2379 struct netlink_ext_ack
*extack
,
2380 struct nlattr
**tb
, char *ifname
, int status
)
2382 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2385 err
= validate_linkmsg(dev
, tb
);
2389 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_TARGET_NETNSID
]) {
2390 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2397 err
= dev_change_net_namespace(dev
, net
, ifname
);
2401 status
|= DO_SETLINK_MODIFIED
;
2405 struct rtnl_link_ifmap
*u_map
;
2408 if (!ops
->ndo_set_config
) {
2413 if (!netif_device_present(dev
)) {
2418 u_map
= nla_data(tb
[IFLA_MAP
]);
2419 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2420 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2421 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2422 k_map
.irq
= (unsigned char) u_map
->irq
;
2423 k_map
.dma
= (unsigned char) u_map
->dma
;
2424 k_map
.port
= (unsigned char) u_map
->port
;
2426 err
= ops
->ndo_set_config(dev
, &k_map
);
2430 status
|= DO_SETLINK_NOTIFY
;
2433 if (tb
[IFLA_ADDRESS
]) {
2434 struct sockaddr
*sa
;
2437 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2439 sa
= kmalloc(len
, GFP_KERNEL
);
2444 sa
->sa_family
= dev
->type
;
2445 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2447 err
= dev_set_mac_address(dev
, sa
);
2451 status
|= DO_SETLINK_MODIFIED
;
2455 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
2458 status
|= DO_SETLINK_MODIFIED
;
2461 if (tb
[IFLA_GROUP
]) {
2462 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2463 status
|= DO_SETLINK_NOTIFY
;
2467 * Interface selected by interface index but interface
2468 * name provided implies that a name change has been
2471 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2472 err
= dev_change_name(dev
, ifname
);
2475 status
|= DO_SETLINK_MODIFIED
;
2478 if (tb
[IFLA_IFALIAS
]) {
2479 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2480 nla_len(tb
[IFLA_IFALIAS
]));
2483 status
|= DO_SETLINK_NOTIFY
;
2486 if (tb
[IFLA_BROADCAST
]) {
2487 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2488 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2491 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2492 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2497 if (tb
[IFLA_MASTER
]) {
2498 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2501 status
|= DO_SETLINK_MODIFIED
;
2504 if (tb
[IFLA_CARRIER
]) {
2505 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2508 status
|= DO_SETLINK_MODIFIED
;
2511 if (tb
[IFLA_TXQLEN
]) {
2512 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2514 err
= dev_change_tx_queue_len(dev
, value
);
2517 status
|= DO_SETLINK_MODIFIED
;
2520 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2521 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2523 if (max_size
> GSO_MAX_SIZE
) {
2528 if (dev
->gso_max_size
^ max_size
) {
2529 netif_set_gso_max_size(dev
, max_size
);
2530 status
|= DO_SETLINK_MODIFIED
;
2534 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2535 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2537 if (max_segs
> GSO_MAX_SEGS
) {
2542 if (dev
->gso_max_segs
^ max_segs
) {
2543 dev
->gso_max_segs
= max_segs
;
2544 status
|= DO_SETLINK_MODIFIED
;
2548 if (tb
[IFLA_OPERSTATE
])
2549 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2551 if (tb
[IFLA_LINKMODE
]) {
2552 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2554 write_lock_bh(&dev_base_lock
);
2555 if (dev
->link_mode
^ value
)
2556 status
|= DO_SETLINK_NOTIFY
;
2557 dev
->link_mode
= value
;
2558 write_unlock_bh(&dev_base_lock
);
2561 if (tb
[IFLA_VFINFO_LIST
]) {
2562 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2563 struct nlattr
*attr
;
2566 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2567 if (nla_type(attr
) != IFLA_VF_INFO
||
2568 nla_len(attr
) < NLA_HDRLEN
) {
2572 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2573 ifla_vf_policy
, NULL
);
2576 err
= do_setvfinfo(dev
, vfinfo
);
2579 status
|= DO_SETLINK_NOTIFY
;
2584 if (tb
[IFLA_VF_PORTS
]) {
2585 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2586 struct nlattr
*attr
;
2591 if (!ops
->ndo_set_vf_port
)
2594 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2595 if (nla_type(attr
) != IFLA_VF_PORT
||
2596 nla_len(attr
) < NLA_HDRLEN
) {
2600 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2601 ifla_port_policy
, NULL
);
2604 if (!port
[IFLA_PORT_VF
]) {
2608 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2609 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2612 status
|= DO_SETLINK_NOTIFY
;
2617 if (tb
[IFLA_PORT_SELF
]) {
2618 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2620 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2621 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2627 if (ops
->ndo_set_vf_port
)
2628 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2631 status
|= DO_SETLINK_NOTIFY
;
2634 if (tb
[IFLA_AF_SPEC
]) {
2638 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2639 const struct rtnl_af_ops
*af_ops
;
2643 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2645 err
= af_ops
->set_link_af(dev
, af
);
2652 status
|= DO_SETLINK_NOTIFY
;
2657 if (tb
[IFLA_PROTO_DOWN
]) {
2658 err
= dev_change_proto_down(dev
,
2659 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2662 status
|= DO_SETLINK_NOTIFY
;
2666 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2669 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2670 ifla_xdp_policy
, NULL
);
2674 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2679 if (xdp
[IFLA_XDP_FLAGS
]) {
2680 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2681 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2685 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2691 if (xdp
[IFLA_XDP_FD
]) {
2692 err
= dev_change_xdp_fd(dev
, extack
,
2693 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2697 status
|= DO_SETLINK_NOTIFY
;
2702 if (status
& DO_SETLINK_MODIFIED
) {
2703 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2704 netdev_state_change(dev
);
2707 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",
2714 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2715 struct netlink_ext_ack
*extack
)
2717 struct net
*net
= sock_net(skb
->sk
);
2718 struct ifinfomsg
*ifm
;
2719 struct net_device
*dev
;
2721 struct nlattr
*tb
[IFLA_MAX
+1];
2722 char ifname
[IFNAMSIZ
];
2724 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2729 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2733 if (tb
[IFLA_IFNAME
])
2734 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2739 ifm
= nlmsg_data(nlh
);
2740 if (ifm
->ifi_index
> 0)
2741 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2742 else if (tb
[IFLA_IFNAME
])
2743 dev
= __dev_get_by_name(net
, ifname
);
2752 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2757 static int rtnl_group_dellink(const struct net
*net
, int group
)
2759 struct net_device
*dev
, *aux
;
2760 LIST_HEAD(list_kill
);
2766 for_each_netdev(net
, dev
) {
2767 if (dev
->group
== group
) {
2768 const struct rtnl_link_ops
*ops
;
2771 ops
= dev
->rtnl_link_ops
;
2772 if (!ops
|| !ops
->dellink
)
2780 for_each_netdev_safe(net
, dev
, aux
) {
2781 if (dev
->group
== group
) {
2782 const struct rtnl_link_ops
*ops
;
2784 ops
= dev
->rtnl_link_ops
;
2785 ops
->dellink(dev
, &list_kill
);
2788 unregister_netdevice_many(&list_kill
);
2793 int rtnl_delete_link(struct net_device
*dev
)
2795 const struct rtnl_link_ops
*ops
;
2796 LIST_HEAD(list_kill
);
2798 ops
= dev
->rtnl_link_ops
;
2799 if (!ops
|| !ops
->dellink
)
2802 ops
->dellink(dev
, &list_kill
);
2803 unregister_netdevice_many(&list_kill
);
2807 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2809 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2810 struct netlink_ext_ack
*extack
)
2812 struct net
*net
= sock_net(skb
->sk
);
2813 struct net
*tgt_net
= net
;
2814 struct net_device
*dev
= NULL
;
2815 struct ifinfomsg
*ifm
;
2816 char ifname
[IFNAMSIZ
];
2817 struct nlattr
*tb
[IFLA_MAX
+1];
2821 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2825 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2829 if (tb
[IFLA_IFNAME
])
2830 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2832 if (tb
[IFLA_TARGET_NETNSID
]) {
2833 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
2834 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
2835 if (IS_ERR(tgt_net
))
2836 return PTR_ERR(tgt_net
);
2840 ifm
= nlmsg_data(nlh
);
2841 if (ifm
->ifi_index
> 0)
2842 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2843 else if (tb
[IFLA_IFNAME
])
2844 dev
= __dev_get_by_name(tgt_net
, ifname
);
2845 else if (tb
[IFLA_GROUP
])
2846 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2851 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2857 err
= rtnl_delete_link(dev
);
2866 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2868 unsigned int old_flags
;
2871 old_flags
= dev
->flags
;
2872 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2873 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2878 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
2879 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
));
2881 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2882 __dev_notify_flags(dev
, old_flags
, ~0U);
2886 EXPORT_SYMBOL(rtnl_configure_link
);
2888 struct net_device
*rtnl_create_link(struct net
*net
,
2889 const char *ifname
, unsigned char name_assign_type
,
2890 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2892 struct net_device
*dev
;
2893 unsigned int num_tx_queues
= 1;
2894 unsigned int num_rx_queues
= 1;
2896 if (tb
[IFLA_NUM_TX_QUEUES
])
2897 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2898 else if (ops
->get_num_tx_queues
)
2899 num_tx_queues
= ops
->get_num_tx_queues();
2901 if (tb
[IFLA_NUM_RX_QUEUES
])
2902 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2903 else if (ops
->get_num_rx_queues
)
2904 num_rx_queues
= ops
->get_num_rx_queues();
2906 if (num_tx_queues
< 1 || num_tx_queues
> 4096)
2907 return ERR_PTR(-EINVAL
);
2909 if (num_rx_queues
< 1 || num_rx_queues
> 4096)
2910 return ERR_PTR(-EINVAL
);
2912 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2913 ops
->setup
, num_tx_queues
, num_rx_queues
);
2915 return ERR_PTR(-ENOMEM
);
2917 dev_net_set(dev
, net
);
2918 dev
->rtnl_link_ops
= ops
;
2919 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2922 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2923 if (tb
[IFLA_ADDRESS
]) {
2924 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2925 nla_len(tb
[IFLA_ADDRESS
]));
2926 dev
->addr_assign_type
= NET_ADDR_SET
;
2928 if (tb
[IFLA_BROADCAST
])
2929 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2930 nla_len(tb
[IFLA_BROADCAST
]));
2931 if (tb
[IFLA_TXQLEN
])
2932 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2933 if (tb
[IFLA_OPERSTATE
])
2934 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2935 if (tb
[IFLA_LINKMODE
])
2936 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2938 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2939 if (tb
[IFLA_GSO_MAX_SIZE
])
2940 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
2941 if (tb
[IFLA_GSO_MAX_SEGS
])
2942 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2946 EXPORT_SYMBOL(rtnl_create_link
);
2948 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2949 struct net
*net
, int group
,
2950 struct ifinfomsg
*ifm
,
2951 struct netlink_ext_ack
*extack
,
2954 struct net_device
*dev
, *aux
;
2957 for_each_netdev_safe(net
, dev
, aux
) {
2958 if (dev
->group
== group
) {
2959 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2968 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2969 struct netlink_ext_ack
*extack
)
2971 struct net
*net
= sock_net(skb
->sk
);
2972 const struct rtnl_link_ops
*ops
;
2973 const struct rtnl_link_ops
*m_ops
= NULL
;
2974 struct net_device
*dev
;
2975 struct net_device
*master_dev
= NULL
;
2976 struct ifinfomsg
*ifm
;
2977 char kind
[MODULE_NAME_LEN
];
2978 char ifname
[IFNAMSIZ
];
2979 struct nlattr
*tb
[IFLA_MAX
+1];
2980 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2981 unsigned char name_assign_type
= NET_NAME_USER
;
2984 #ifdef CONFIG_MODULES
2987 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2991 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2995 if (tb
[IFLA_IFNAME
])
2996 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3000 ifm
= nlmsg_data(nlh
);
3001 if (ifm
->ifi_index
> 0)
3002 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3005 dev
= __dev_get_by_name(net
, ifname
);
3011 master_dev
= netdev_master_upper_dev_get(dev
);
3013 m_ops
= master_dev
->rtnl_link_ops
;
3016 err
= validate_linkmsg(dev
, tb
);
3020 if (tb
[IFLA_LINKINFO
]) {
3021 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
3022 tb
[IFLA_LINKINFO
], ifla_info_policy
,
3027 memset(linkinfo
, 0, sizeof(linkinfo
));
3029 if (linkinfo
[IFLA_INFO_KIND
]) {
3030 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
3031 ops
= rtnl_link_ops_get(kind
);
3038 struct nlattr
*attr
[RTNL_MAX_TYPE
+ 1];
3039 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
3040 struct nlattr
**data
= NULL
;
3041 struct nlattr
**slave_data
= NULL
;
3042 struct net
*dest_net
, *link_net
= NULL
;
3045 if (ops
->maxtype
> RTNL_MAX_TYPE
)
3048 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3049 err
= nla_parse_nested(attr
, ops
->maxtype
,
3050 linkinfo
[IFLA_INFO_DATA
],
3056 if (ops
->validate
) {
3057 err
= ops
->validate(tb
, data
, extack
);
3064 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3067 if (m_ops
->slave_maxtype
&&
3068 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3069 err
= nla_parse_nested(slave_attr
,
3070 m_ops
->slave_maxtype
,
3071 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3072 m_ops
->slave_policy
,
3076 slave_data
= slave_attr
;
3083 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3085 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3088 if (linkinfo
[IFLA_INFO_DATA
]) {
3089 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
3093 err
= ops
->changelink(dev
, tb
, data
, extack
);
3096 status
|= DO_SETLINK_NOTIFY
;
3099 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3100 if (!m_ops
|| !m_ops
->slave_changelink
)
3103 err
= m_ops
->slave_changelink(master_dev
, dev
,
3108 status
|= DO_SETLINK_NOTIFY
;
3111 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
3115 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3116 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
3117 return rtnl_group_changelink(skb
, net
,
3118 nla_get_u32(tb
[IFLA_GROUP
]),
3123 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3127 #ifdef CONFIG_MODULES
3130 request_module("rtnl-link-%s", kind
);
3132 ops
= rtnl_link_ops_get(kind
);
3144 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3145 name_assign_type
= NET_NAME_ENUM
;
3148 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3149 if (IS_ERR(dest_net
))
3150 return PTR_ERR(dest_net
);
3152 if (tb
[IFLA_LINK_NETNSID
]) {
3153 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3155 link_net
= get_net_ns_by_id(dest_net
, id
);
3161 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3165 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3166 name_assign_type
, ops
, tb
);
3172 dev
->ifindex
= ifm
->ifi_index
;
3175 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
3177 /* Drivers should call free_netdev() in ->destructor
3178 * and unregister it on failure after registration
3179 * so that device could be finally freed in rtnl_unlock.
3182 /* If device is not registered at all, free it now */
3183 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3188 err
= register_netdevice(dev
);
3194 err
= rtnl_configure_link(dev
, ifm
);
3196 goto out_unregister
;
3198 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3200 goto out_unregister
;
3202 if (tb
[IFLA_MASTER
]) {
3203 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]),
3206 goto out_unregister
;
3215 LIST_HEAD(list_kill
);
3217 ops
->dellink(dev
, &list_kill
);
3218 unregister_netdevice_many(&list_kill
);
3220 unregister_netdevice(dev
);
3226 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3227 struct netlink_ext_ack
*extack
)
3229 struct net
*net
= sock_net(skb
->sk
);
3230 struct net
*tgt_net
= net
;
3231 struct ifinfomsg
*ifm
;
3232 char ifname
[IFNAMSIZ
];
3233 struct nlattr
*tb
[IFLA_MAX
+1];
3234 struct net_device
*dev
= NULL
;
3235 struct sk_buff
*nskb
;
3238 u32 ext_filter_mask
= 0;
3240 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3244 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3248 if (tb
[IFLA_TARGET_NETNSID
]) {
3249 netnsid
= nla_get_s32(tb
[IFLA_TARGET_NETNSID
]);
3250 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(skb
).sk
, netnsid
);
3251 if (IS_ERR(tgt_net
))
3252 return PTR_ERR(tgt_net
);
3255 if (tb
[IFLA_IFNAME
])
3256 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3258 if (tb
[IFLA_EXT_MASK
])
3259 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3262 ifm
= nlmsg_data(nlh
);
3263 if (ifm
->ifi_index
> 0)
3264 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3265 else if (tb
[IFLA_IFNAME
])
3266 dev
= __dev_get_by_name(tgt_net
, ifname
);
3275 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3279 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3280 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3281 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3282 0, NULL
, 0, netnsid
);
3284 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3285 WARN_ON(err
== -EMSGSIZE
);
3288 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3296 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3298 struct net
*net
= sock_net(skb
->sk
);
3299 struct net_device
*dev
;
3300 struct nlattr
*tb
[IFLA_MAX
+1];
3301 u32 ext_filter_mask
= 0;
3302 u16 min_ifinfo_dump_size
= 0;
3305 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3306 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3307 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3309 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3310 if (tb
[IFLA_EXT_MASK
])
3311 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3314 if (!ext_filter_mask
)
3315 return NLMSG_GOODSIZE
;
3317 * traverse the list of net devices and compute the minimum
3318 * buffer size based upon the filter mask.
3321 for_each_netdev_rcu(net
, dev
) {
3322 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3328 return nlmsg_total_size(min_ifinfo_dump_size
);
3331 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3334 int s_idx
= cb
->family
;
3335 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
3341 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3342 struct rtnl_link
**tab
;
3343 struct rtnl_link
*link
;
3344 rtnl_dumpit_func dumpit
;
3346 if (idx
< s_idx
|| idx
== PF_PACKET
)
3349 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3352 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3360 dumpit
= link
->dumpit
;
3365 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3369 ret
= dumpit(skb
, cb
);
3375 return skb
->len
? : ret
;
3378 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3379 unsigned int change
,
3380 u32 event
, gfp_t flags
, int *new_nsid
,
3383 struct net
*net
= dev_net(dev
);
3384 struct sk_buff
*skb
;
3386 size_t if_info_size
;
3388 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3392 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3393 type
, 0, 0, change
, 0, 0, event
,
3394 new_nsid
, new_ifindex
, -1);
3396 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3397 WARN_ON(err
== -EMSGSIZE
);
3404 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3408 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3410 struct net
*net
= dev_net(dev
);
3412 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3415 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3416 unsigned int change
, u32 event
,
3417 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3419 struct sk_buff
*skb
;
3421 if (dev
->reg_state
!= NETREG_REGISTERED
)
3424 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3427 rtmsg_ifinfo_send(skb
, dev
, flags
);
3430 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3433 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3437 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3438 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3440 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3441 new_nsid
, new_ifindex
);
3444 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3445 struct net_device
*dev
,
3446 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3447 int type
, unsigned int flags
,
3448 int nlflags
, u16 ndm_state
)
3450 struct nlmsghdr
*nlh
;
3453 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3457 ndm
= nlmsg_data(nlh
);
3458 ndm
->ndm_family
= AF_BRIDGE
;
3461 ndm
->ndm_flags
= flags
;
3463 ndm
->ndm_ifindex
= dev
->ifindex
;
3464 ndm
->ndm_state
= ndm_state
;
3466 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3467 goto nla_put_failure
;
3469 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3470 goto nla_put_failure
;
3472 nlmsg_end(skb
, nlh
);
3476 nlmsg_cancel(skb
, nlh
);
3480 static inline size_t rtnl_fdb_nlmsg_size(void)
3482 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3483 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3484 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3488 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3491 struct net
*net
= dev_net(dev
);
3492 struct sk_buff
*skb
;
3495 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3499 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3500 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3506 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3509 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3513 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3515 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3516 struct nlattr
*tb
[],
3517 struct net_device
*dev
,
3518 const unsigned char *addr
, u16 vid
,
3523 /* If aging addresses are supported device will need to
3524 * implement its own handler for this.
3526 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3527 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3532 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3536 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3537 err
= dev_uc_add_excl(dev
, addr
);
3538 else if (is_multicast_ether_addr(addr
))
3539 err
= dev_mc_add_excl(dev
, addr
);
3541 /* Only return duplicate errors if NLM_F_EXCL is set */
3542 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3547 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3549 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3550 struct netlink_ext_ack
*extack
)
3555 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3556 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3560 vid
= nla_get_u16(vlan_attr
);
3562 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3563 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3571 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3572 struct netlink_ext_ack
*extack
)
3574 struct net
*net
= sock_net(skb
->sk
);
3576 struct nlattr
*tb
[NDA_MAX
+1];
3577 struct net_device
*dev
;
3582 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3586 ndm
= nlmsg_data(nlh
);
3587 if (ndm
->ndm_ifindex
== 0) {
3588 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3592 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3594 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3598 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3599 NL_SET_ERR_MSG(extack
, "invalid address");
3603 if (dev
->type
!= ARPHRD_ETHER
) {
3604 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
3608 addr
= nla_data(tb
[NDA_LLADDR
]);
3610 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3616 /* Support fdb on master device the net/bridge default case */
3617 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3618 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3619 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3620 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3622 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3627 ndm
->ndm_flags
&= ~NTF_MASTER
;
3630 /* Embedded bridge, macvlan, and any other device support */
3631 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3632 if (dev
->netdev_ops
->ndo_fdb_add
)
3633 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3637 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3641 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3643 ndm
->ndm_flags
&= ~NTF_SELF
;
3651 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3653 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3654 struct nlattr
*tb
[],
3655 struct net_device
*dev
,
3656 const unsigned char *addr
, u16 vid
)
3660 /* If aging addresses are supported device will need to
3661 * implement its own handler for this.
3663 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3664 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3668 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3669 err
= dev_uc_del(dev
, addr
);
3670 else if (is_multicast_ether_addr(addr
))
3671 err
= dev_mc_del(dev
, addr
);
3675 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3677 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3678 struct netlink_ext_ack
*extack
)
3680 struct net
*net
= sock_net(skb
->sk
);
3682 struct nlattr
*tb
[NDA_MAX
+1];
3683 struct net_device
*dev
;
3688 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3691 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3695 ndm
= nlmsg_data(nlh
);
3696 if (ndm
->ndm_ifindex
== 0) {
3697 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3701 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3703 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3707 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3708 NL_SET_ERR_MSG(extack
, "invalid address");
3712 if (dev
->type
!= ARPHRD_ETHER
) {
3713 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
3717 addr
= nla_data(tb
[NDA_LLADDR
]);
3719 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3725 /* Support fdb on master device the net/bridge default case */
3726 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3727 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3728 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3729 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3731 if (ops
->ndo_fdb_del
)
3732 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3737 ndm
->ndm_flags
&= ~NTF_MASTER
;
3740 /* Embedded bridge, macvlan, and any other device support */
3741 if (ndm
->ndm_flags
& NTF_SELF
) {
3742 if (dev
->netdev_ops
->ndo_fdb_del
)
3743 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3746 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3749 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3751 ndm
->ndm_flags
&= ~NTF_SELF
;
3758 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3759 struct netlink_callback
*cb
,
3760 struct net_device
*dev
,
3762 struct netdev_hw_addr_list
*list
)
3764 struct netdev_hw_addr
*ha
;
3768 portid
= NETLINK_CB(cb
->skb
).portid
;
3769 seq
= cb
->nlh
->nlmsg_seq
;
3771 list_for_each_entry(ha
, &list
->list
, list
) {
3772 if (*idx
< cb
->args
[2])
3775 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3777 RTM_NEWNEIGH
, NTF_SELF
,
3778 NLM_F_MULTI
, NUD_PERMANENT
);
3788 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3789 * @nlh: netlink message header
3792 * Default netdevice operation to dump the existing unicast address list.
3793 * Returns number of addresses from list put in skb.
3795 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3796 struct netlink_callback
*cb
,
3797 struct net_device
*dev
,
3798 struct net_device
*filter_dev
,
3803 if (dev
->type
!= ARPHRD_ETHER
)
3806 netif_addr_lock_bh(dev
);
3807 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3810 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3812 netif_addr_unlock_bh(dev
);
3815 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3817 static int valid_fdb_dump_strict(const struct nlmsghdr
*nlh
,
3818 int *br_idx
, int *brport_idx
,
3819 struct netlink_ext_ack
*extack
)
3821 struct nlattr
*tb
[NDA_MAX
+ 1];
3825 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ndm
))) {
3826 NL_SET_ERR_MSG(extack
, "Invalid header for fdb dump request");
3830 ndm
= nlmsg_data(nlh
);
3831 if (ndm
->ndm_pad1
|| ndm
->ndm_pad2
|| ndm
->ndm_state
||
3832 ndm
->ndm_flags
|| ndm
->ndm_type
) {
3833 NL_SET_ERR_MSG(extack
, "Invalid values in header for fbd dump request");
3837 err
= nlmsg_parse_strict(nlh
, sizeof(struct ndmsg
), tb
, NDA_MAX
,
3842 *brport_idx
= ndm
->ndm_ifindex
;
3843 for (i
= 0; i
<= NDA_MAX
; ++i
) {
3849 if (nla_len(tb
[i
]) != sizeof(u32
)) {
3850 NL_SET_ERR_MSG(extack
, "Invalid IFINDEX attribute in fdb dump request");
3853 *brport_idx
= nla_get_u32(tb
[NDA_IFINDEX
]);
3856 if (nla_len(tb
[i
]) != sizeof(u32
)) {
3857 NL_SET_ERR_MSG(extack
, "Invalid MASTER attribute in fdb dump request");
3860 *br_idx
= nla_get_u32(tb
[NDA_MASTER
]);
3863 NL_SET_ERR_MSG(extack
, "Unsupported attribute in fdb dump request");
3871 static int valid_fdb_dump_legacy(const struct nlmsghdr
*nlh
,
3872 int *br_idx
, int *brport_idx
,
3873 struct netlink_ext_ack
*extack
)
3875 struct nlattr
*tb
[IFLA_MAX
+1];
3878 /* A hack to preserve kernel<->userspace interface.
3879 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3880 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3881 * So, check for ndmsg with an optional u32 attribute (not used here).
3882 * Fortunately these sizes don't conflict with the size of ifinfomsg
3883 * with an optional attribute.
3885 if (nlmsg_len(nlh
) != sizeof(struct ndmsg
) &&
3886 (nlmsg_len(nlh
) != sizeof(struct ndmsg
) +
3887 nla_attr_size(sizeof(u32
)))) {
3888 struct ifinfomsg
*ifm
;
3890 err
= nlmsg_parse(nlh
, sizeof(struct ifinfomsg
), tb
, IFLA_MAX
,
3891 ifla_policy
, extack
);
3894 } else if (err
== 0) {
3895 if (tb
[IFLA_MASTER
])
3896 *br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3899 ifm
= nlmsg_data(nlh
);
3900 *brport_idx
= ifm
->ifi_index
;
3905 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3907 struct net_device
*dev
;
3908 struct net_device
*br_dev
= NULL
;
3909 const struct net_device_ops
*ops
= NULL
;
3910 const struct net_device_ops
*cops
= NULL
;
3911 struct net
*net
= sock_net(skb
->sk
);
3912 struct hlist_head
*head
;
3920 if (cb
->strict_check
)
3921 err
= valid_fdb_dump_strict(cb
->nlh
, &br_idx
, &brport_idx
,
3924 err
= valid_fdb_dump_legacy(cb
->nlh
, &br_idx
, &brport_idx
,
3930 br_dev
= __dev_get_by_index(net
, br_idx
);
3934 ops
= br_dev
->netdev_ops
;
3938 s_idx
= cb
->args
[1];
3940 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3942 head
= &net
->dev_index_head
[h
];
3943 hlist_for_each_entry(dev
, head
, index_hlist
) {
3945 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3948 if (!br_idx
) { /* user did not specify a specific bridge */
3949 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3950 br_dev
= netdev_master_upper_dev_get(dev
);
3951 cops
= br_dev
->netdev_ops
;
3954 if (dev
!= br_dev
&&
3955 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3958 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3959 !(dev
->priv_flags
& IFF_EBRIDGE
))
3967 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3968 if (cops
&& cops
->ndo_fdb_dump
) {
3969 err
= cops
->ndo_fdb_dump(skb
, cb
,
3972 if (err
== -EMSGSIZE
)
3977 if (dev
->netdev_ops
->ndo_fdb_dump
)
3978 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3982 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3984 if (err
== -EMSGSIZE
)
3989 /* reset fdb offset to 0 for rest of the interfaces */
4005 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
4006 unsigned int attrnum
, unsigned int flag
)
4009 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
4013 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
4014 struct net_device
*dev
, u16 mode
,
4015 u32 flags
, u32 mask
, int nlflags
,
4017 int (*vlan_fill
)(struct sk_buff
*skb
,
4018 struct net_device
*dev
,
4021 struct nlmsghdr
*nlh
;
4022 struct ifinfomsg
*ifm
;
4023 struct nlattr
*br_afspec
;
4024 struct nlattr
*protinfo
;
4025 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
4026 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4029 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
4033 ifm
= nlmsg_data(nlh
);
4034 ifm
->ifi_family
= AF_BRIDGE
;
4036 ifm
->ifi_type
= dev
->type
;
4037 ifm
->ifi_index
= dev
->ifindex
;
4038 ifm
->ifi_flags
= dev_get_flags(dev
);
4039 ifm
->ifi_change
= 0;
4042 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4043 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4044 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
4046 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
4048 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4049 (dev
->ifindex
!= dev_get_iflink(dev
) &&
4050 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
4051 goto nla_put_failure
;
4053 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
4055 goto nla_put_failure
;
4057 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
4058 nla_nest_cancel(skb
, br_afspec
);
4059 goto nla_put_failure
;
4062 if (mode
!= BRIDGE_MODE_UNDEF
) {
4063 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
4064 nla_nest_cancel(skb
, br_afspec
);
4065 goto nla_put_failure
;
4069 err
= vlan_fill(skb
, dev
, filter_mask
);
4071 nla_nest_cancel(skb
, br_afspec
);
4072 goto nla_put_failure
;
4075 nla_nest_end(skb
, br_afspec
);
4077 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
4079 goto nla_put_failure
;
4081 if (brport_nla_put_flag(skb
, flags
, mask
,
4082 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
4083 brport_nla_put_flag(skb
, flags
, mask
,
4084 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
4085 brport_nla_put_flag(skb
, flags
, mask
,
4086 IFLA_BRPORT_FAST_LEAVE
,
4087 BR_MULTICAST_FAST_LEAVE
) ||
4088 brport_nla_put_flag(skb
, flags
, mask
,
4089 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
4090 brport_nla_put_flag(skb
, flags
, mask
,
4091 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
4092 brport_nla_put_flag(skb
, flags
, mask
,
4093 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
4094 brport_nla_put_flag(skb
, flags
, mask
,
4095 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
4096 brport_nla_put_flag(skb
, flags
, mask
,
4097 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
4098 nla_nest_cancel(skb
, protinfo
);
4099 goto nla_put_failure
;
4102 nla_nest_end(skb
, protinfo
);
4104 nlmsg_end(skb
, nlh
);
4107 nlmsg_cancel(skb
, nlh
);
4108 return err
? err
: -EMSGSIZE
;
4110 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
4112 static int valid_bridge_getlink_req(const struct nlmsghdr
*nlh
,
4113 bool strict_check
, u32
*filter_mask
,
4114 struct netlink_ext_ack
*extack
)
4116 struct nlattr
*tb
[IFLA_MAX
+1];
4120 struct ifinfomsg
*ifm
;
4122 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
4123 NL_SET_ERR_MSG(extack
, "Invalid header for bridge link dump");
4127 ifm
= nlmsg_data(nlh
);
4128 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
4129 ifm
->ifi_change
|| ifm
->ifi_index
) {
4130 NL_SET_ERR_MSG(extack
, "Invalid values in header for bridge link dump request");
4134 err
= nlmsg_parse_strict(nlh
, sizeof(struct ifinfomsg
), tb
,
4135 IFLA_MAX
, ifla_policy
, extack
);
4137 err
= nlmsg_parse(nlh
, sizeof(struct ifinfomsg
), tb
,
4138 IFLA_MAX
, ifla_policy
, extack
);
4143 /* new attributes should only be added with strict checking */
4144 for (i
= 0; i
<= IFLA_MAX
; ++i
) {
4150 *filter_mask
= nla_get_u32(tb
[i
]);
4154 NL_SET_ERR_MSG(extack
, "Unsupported attribute in bridge link dump request");
4163 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4165 const struct nlmsghdr
*nlh
= cb
->nlh
;
4166 struct net
*net
= sock_net(skb
->sk
);
4167 struct net_device
*dev
;
4169 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
4170 u32 seq
= nlh
->nlmsg_seq
;
4171 u32 filter_mask
= 0;
4174 err
= valid_bridge_getlink_req(nlh
, cb
->strict_check
, &filter_mask
,
4176 if (err
< 0 && cb
->strict_check
)
4180 for_each_netdev_rcu(net
, dev
) {
4181 const struct net_device_ops
*ops
= dev
->netdev_ops
;
4182 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4184 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
4185 if (idx
>= cb
->args
[0]) {
4186 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
4187 skb
, portid
, seq
, dev
,
4188 filter_mask
, NLM_F_MULTI
);
4189 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4190 if (likely(skb
->len
))
4199 if (ops
->ndo_bridge_getlink
) {
4200 if (idx
>= cb
->args
[0]) {
4201 err
= ops
->ndo_bridge_getlink(skb
, portid
,
4205 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4206 if (likely(skb
->len
))
4223 static inline size_t bridge_nlmsg_size(void)
4225 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4226 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4227 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4228 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
4229 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
4230 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
4231 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
4232 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
4233 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
4234 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
4235 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
4238 static int rtnl_bridge_notify(struct net_device
*dev
)
4240 struct net
*net
= dev_net(dev
);
4241 struct sk_buff
*skb
;
4242 int err
= -EOPNOTSUPP
;
4244 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
4247 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
4253 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
4260 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
4263 WARN_ON(err
== -EMSGSIZE
);
4266 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4270 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4271 struct netlink_ext_ack
*extack
)
4273 struct net
*net
= sock_net(skb
->sk
);
4274 struct ifinfomsg
*ifm
;
4275 struct net_device
*dev
;
4276 struct nlattr
*br_spec
, *attr
= NULL
;
4277 int rem
, err
= -EOPNOTSUPP
;
4279 bool have_flags
= false;
4281 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4284 ifm
= nlmsg_data(nlh
);
4285 if (ifm
->ifi_family
!= AF_BRIDGE
)
4286 return -EPFNOSUPPORT
;
4288 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4290 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4294 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4296 nla_for_each_nested(attr
, br_spec
, rem
) {
4297 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4298 if (nla_len(attr
) < sizeof(flags
))
4302 flags
= nla_get_u16(attr
);
4308 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4309 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4311 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4316 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
4320 flags
&= ~BRIDGE_FLAGS_MASTER
;
4323 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4324 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4327 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4330 flags
&= ~BRIDGE_FLAGS_SELF
;
4332 /* Generate event to notify upper layer of bridge
4335 err
= rtnl_bridge_notify(dev
);
4340 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4345 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4346 struct netlink_ext_ack
*extack
)
4348 struct net
*net
= sock_net(skb
->sk
);
4349 struct ifinfomsg
*ifm
;
4350 struct net_device
*dev
;
4351 struct nlattr
*br_spec
, *attr
= NULL
;
4352 int rem
, err
= -EOPNOTSUPP
;
4354 bool have_flags
= false;
4356 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4359 ifm
= nlmsg_data(nlh
);
4360 if (ifm
->ifi_family
!= AF_BRIDGE
)
4361 return -EPFNOSUPPORT
;
4363 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4365 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4369 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4371 nla_for_each_nested(attr
, br_spec
, rem
) {
4372 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4373 if (nla_len(attr
) < sizeof(flags
))
4377 flags
= nla_get_u16(attr
);
4383 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4384 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4386 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4391 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4395 flags
&= ~BRIDGE_FLAGS_MASTER
;
4398 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4399 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4402 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4406 flags
&= ~BRIDGE_FLAGS_SELF
;
4408 /* Generate event to notify upper layer of bridge
4411 err
= rtnl_bridge_notify(dev
);
4416 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4421 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4423 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4424 (!idxattr
|| idxattr
== attrid
);
4427 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4428 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4431 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4432 return sizeof(struct rtnl_link_stats64
);
4438 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4441 struct nlattr
*attr
= NULL
;
4446 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4447 dev
->netdev_ops
->ndo_get_offload_stats
))
4450 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4451 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4452 if (attr_id
< *prividx
)
4455 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4459 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4462 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4463 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4465 goto nla_put_failure
;
4467 attr_data
= nla_data(attr
);
4468 memset(attr_data
, 0, size
);
4469 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4472 goto get_offload_stats_failure
;
4483 get_offload_stats_failure
:
4488 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4494 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4495 dev
->netdev_ops
->ndo_get_offload_stats
))
4498 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4499 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4500 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4502 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4503 nla_size
+= nla_total_size_64bit(size
);
4507 nla_size
+= nla_total_size(0);
4512 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4513 int type
, u32 pid
, u32 seq
, u32 change
,
4514 unsigned int flags
, unsigned int filter_mask
,
4515 int *idxattr
, int *prividx
)
4517 struct if_stats_msg
*ifsm
;
4518 struct nlmsghdr
*nlh
;
4519 struct nlattr
*attr
;
4520 int s_prividx
= *prividx
;
4525 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4529 ifsm
= nlmsg_data(nlh
);
4530 ifsm
->family
= PF_UNSPEC
;
4533 ifsm
->ifindex
= dev
->ifindex
;
4534 ifsm
->filter_mask
= filter_mask
;
4536 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4537 struct rtnl_link_stats64
*sp
;
4539 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4540 sizeof(struct rtnl_link_stats64
),
4543 goto nla_put_failure
;
4545 sp
= nla_data(attr
);
4546 dev_get_stats(dev
, sp
);
4549 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4550 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4552 if (ops
&& ops
->fill_linkxstats
) {
4553 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4554 attr
= nla_nest_start(skb
,
4555 IFLA_STATS_LINK_XSTATS
);
4557 goto nla_put_failure
;
4559 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4560 nla_nest_end(skb
, attr
);
4562 goto nla_put_failure
;
4567 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4569 const struct rtnl_link_ops
*ops
= NULL
;
4570 const struct net_device
*master
;
4572 master
= netdev_master_upper_dev_get(dev
);
4574 ops
= master
->rtnl_link_ops
;
4575 if (ops
&& ops
->fill_linkxstats
) {
4576 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4577 attr
= nla_nest_start(skb
,
4578 IFLA_STATS_LINK_XSTATS_SLAVE
);
4580 goto nla_put_failure
;
4582 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4583 nla_nest_end(skb
, attr
);
4585 goto nla_put_failure
;
4590 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4592 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4593 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4595 goto nla_put_failure
;
4597 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4598 if (err
== -ENODATA
)
4599 nla_nest_cancel(skb
, attr
);
4601 nla_nest_end(skb
, attr
);
4603 if (err
&& err
!= -ENODATA
)
4604 goto nla_put_failure
;
4608 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4609 struct rtnl_af_ops
*af_ops
;
4611 *idxattr
= IFLA_STATS_AF_SPEC
;
4612 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
4614 goto nla_put_failure
;
4617 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4618 if (af_ops
->fill_stats_af
) {
4622 af
= nla_nest_start(skb
, af_ops
->family
);
4625 goto nla_put_failure
;
4627 err
= af_ops
->fill_stats_af(skb
, dev
);
4629 if (err
== -ENODATA
) {
4630 nla_nest_cancel(skb
, af
);
4631 } else if (err
< 0) {
4633 goto nla_put_failure
;
4636 nla_nest_end(skb
, af
);
4641 nla_nest_end(skb
, attr
);
4646 nlmsg_end(skb
, nlh
);
4651 /* not a multi message or no progress mean a real error */
4652 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4653 nlmsg_cancel(skb
, nlh
);
4655 nlmsg_end(skb
, nlh
);
4660 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4665 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4666 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4668 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4669 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4670 int attr
= IFLA_STATS_LINK_XSTATS
;
4672 if (ops
&& ops
->get_linkxstats_size
) {
4673 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4675 /* for IFLA_STATS_LINK_XSTATS */
4676 size
+= nla_total_size(0);
4680 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4681 struct net_device
*_dev
= (struct net_device
*)dev
;
4682 const struct rtnl_link_ops
*ops
= NULL
;
4683 const struct net_device
*master
;
4685 /* netdev_master_upper_dev_get can't take const */
4686 master
= netdev_master_upper_dev_get(_dev
);
4688 ops
= master
->rtnl_link_ops
;
4689 if (ops
&& ops
->get_linkxstats_size
) {
4690 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4692 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4694 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4695 size
+= nla_total_size(0);
4699 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4700 size
+= rtnl_get_offload_stats_size(dev
);
4702 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4703 struct rtnl_af_ops
*af_ops
;
4705 /* for IFLA_STATS_AF_SPEC */
4706 size
+= nla_total_size(0);
4709 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4710 if (af_ops
->get_stats_af_size
) {
4711 size
+= nla_total_size(
4712 af_ops
->get_stats_af_size(dev
));
4715 size
+= nla_total_size(0);
4724 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4725 struct netlink_ext_ack
*extack
)
4727 struct net
*net
= sock_net(skb
->sk
);
4728 struct net_device
*dev
= NULL
;
4729 int idxattr
= 0, prividx
= 0;
4730 struct if_stats_msg
*ifsm
;
4731 struct sk_buff
*nskb
;
4735 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4738 ifsm
= nlmsg_data(nlh
);
4739 if (ifsm
->ifindex
> 0)
4740 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4747 filter_mask
= ifsm
->filter_mask
;
4751 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4755 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4756 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4757 0, filter_mask
, &idxattr
, &prividx
);
4759 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4760 WARN_ON(err
== -EMSGSIZE
);
4763 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4769 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4771 struct netlink_ext_ack
*extack
= cb
->extack
;
4772 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4773 struct net
*net
= sock_net(skb
->sk
);
4774 unsigned int flags
= NLM_F_MULTI
;
4775 struct if_stats_msg
*ifsm
;
4776 struct hlist_head
*head
;
4777 struct net_device
*dev
;
4778 u32 filter_mask
= 0;
4782 s_idx
= cb
->args
[1];
4783 s_idxattr
= cb
->args
[2];
4784 s_prividx
= cb
->args
[3];
4786 cb
->seq
= net
->dev_base_seq
;
4788 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
)) {
4789 NL_SET_ERR_MSG(extack
, "Invalid header for stats dump");
4793 ifsm
= nlmsg_data(cb
->nlh
);
4795 /* only requests using strict checks can pass data to influence
4796 * the dump. The legacy exception is filter_mask.
4798 if (cb
->strict_check
) {
4799 if (ifsm
->pad1
|| ifsm
->pad2
|| ifsm
->ifindex
) {
4800 NL_SET_ERR_MSG(extack
, "Invalid values in header for stats dump request");
4803 if (nlmsg_attrlen(cb
->nlh
, sizeof(*ifsm
))) {
4804 NL_SET_ERR_MSG(extack
, "Invalid attributes after stats header");
4809 filter_mask
= ifsm
->filter_mask
;
4811 NL_SET_ERR_MSG(extack
, "Filter mask must be set for stats dump");
4815 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4817 head
= &net
->dev_index_head
[h
];
4818 hlist_for_each_entry(dev
, head
, index_hlist
) {
4821 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4822 NETLINK_CB(cb
->skb
).portid
,
4823 cb
->nlh
->nlmsg_seq
, 0,
4825 &s_idxattr
, &s_prividx
);
4826 /* If we ran out of room on the first message,
4829 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4835 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4841 cb
->args
[3] = s_prividx
;
4842 cb
->args
[2] = s_idxattr
;
4849 /* Process one rtnetlink message. */
4851 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4852 struct netlink_ext_ack
*extack
)
4854 struct net
*net
= sock_net(skb
->sk
);
4855 struct rtnl_link
*link
;
4856 struct module
*owner
;
4857 int err
= -EOPNOTSUPP
;
4858 rtnl_doit_func doit
;
4864 type
= nlh
->nlmsg_type
;
4870 /* All the messages must have at least 1 byte length */
4871 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4874 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4877 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4881 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4883 rtnl_dumpit_func dumpit
;
4884 u16 min_dump_alloc
= 0;
4886 link
= rtnl_get_link(family
, type
);
4887 if (!link
|| !link
->dumpit
) {
4889 link
= rtnl_get_link(family
, type
);
4890 if (!link
|| !link
->dumpit
)
4893 owner
= link
->owner
;
4894 dumpit
= link
->dumpit
;
4896 if (type
== RTM_GETLINK
- RTM_BASE
)
4897 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4900 /* need to do this before rcu_read_unlock() */
4901 if (!try_module_get(owner
))
4902 err
= -EPROTONOSUPPORT
;
4908 struct netlink_dump_control c
= {
4910 .min_dump_alloc
= min_dump_alloc
,
4913 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4914 /* netlink_dump_start() will keep a reference on
4915 * module if dump is still in progress.
4922 link
= rtnl_get_link(family
, type
);
4923 if (!link
|| !link
->doit
) {
4925 link
= rtnl_get_link(PF_UNSPEC
, type
);
4926 if (!link
|| !link
->doit
)
4930 owner
= link
->owner
;
4931 if (!try_module_get(owner
)) {
4932 err
= -EPROTONOSUPPORT
;
4936 flags
= link
->flags
;
4937 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4941 err
= doit(skb
, nlh
, extack
);
4948 link
= rtnl_get_link(family
, type
);
4949 if (link
&& link
->doit
)
4950 err
= link
->doit(skb
, nlh
, extack
);
4966 static void rtnetlink_rcv(struct sk_buff
*skb
)
4968 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4971 static int rtnetlink_bind(struct net
*net
, int group
)
4974 case RTNLGRP_IPV4_MROUTE_R
:
4975 case RTNLGRP_IPV6_MROUTE_R
:
4976 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4983 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4985 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4989 case NETDEV_CHANGEMTU
:
4990 case NETDEV_CHANGEADDR
:
4991 case NETDEV_CHANGENAME
:
4992 case NETDEV_FEAT_CHANGE
:
4993 case NETDEV_BONDING_FAILOVER
:
4994 case NETDEV_POST_TYPE_CHANGE
:
4995 case NETDEV_NOTIFY_PEERS
:
4996 case NETDEV_CHANGEUPPER
:
4997 case NETDEV_RESEND_IGMP
:
4998 case NETDEV_CHANGEINFODATA
:
4999 case NETDEV_CHANGELOWERSTATE
:
5000 case NETDEV_CHANGE_TX_QUEUE_LEN
:
5001 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
5002 GFP_KERNEL
, NULL
, 0);
5010 static struct notifier_block rtnetlink_dev_notifier
= {
5011 .notifier_call
= rtnetlink_event
,
5015 static int __net_init
rtnetlink_net_init(struct net
*net
)
5018 struct netlink_kernel_cfg cfg
= {
5019 .groups
= RTNLGRP_MAX
,
5020 .input
= rtnetlink_rcv
,
5021 .cb_mutex
= &rtnl_mutex
,
5022 .flags
= NL_CFG_F_NONROOT_RECV
,
5023 .bind
= rtnetlink_bind
,
5026 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
5033 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
5035 netlink_kernel_release(net
->rtnl
);
5039 static struct pernet_operations rtnetlink_net_ops
= {
5040 .init
= rtnetlink_net_init
,
5041 .exit
= rtnetlink_net_exit
,
5044 void __init
rtnetlink_init(void)
5046 if (register_pernet_subsys(&rtnetlink_net_ops
))
5047 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5049 register_netdevice_notifier(&rtnetlink_dev_notifier
);
5051 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
5052 rtnl_dump_ifinfo
, 0);
5053 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
5054 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
5055 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
5057 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
5058 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
5059 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
5061 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
5062 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
5063 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
5065 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
5066 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
5067 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
5069 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,