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 48
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 #ifdef CONFIG_PROVE_LOCKING
134 bool lockdep_rtnl_is_held(void)
136 return lockdep_is_held(&rtnl_mutex
);
138 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
139 #endif /* #ifdef CONFIG_PROVE_LOCKING */
141 static struct rtnl_link
*__rcu
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
143 static inline int rtm_msgindex(int msgtype
)
145 int msgindex
= msgtype
- RTM_BASE
;
148 * msgindex < 0 implies someone tried to register a netlink
149 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
150 * the message type has not been added to linux/rtnetlink.h
152 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
157 static struct rtnl_link
*rtnl_get_link(int protocol
, int msgtype
)
159 struct rtnl_link
**tab
;
161 if (protocol
>= ARRAY_SIZE(rtnl_msg_handlers
))
162 protocol
= PF_UNSPEC
;
164 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[protocol
]);
166 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[PF_UNSPEC
]);
171 static int rtnl_register_internal(struct module
*owner
,
172 int protocol
, int msgtype
,
173 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
176 struct rtnl_link
*link
, *old
;
177 struct rtnl_link __rcu
**tab
;
181 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
182 msgindex
= rtm_msgindex(msgtype
);
185 tab
= rtnl_msg_handlers
[protocol
];
187 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(void *), GFP_KERNEL
);
191 /* ensures we see the 0 stores */
192 rcu_assign_pointer(rtnl_msg_handlers
[protocol
], tab
);
195 old
= rtnl_dereference(tab
[msgindex
]);
197 link
= kmemdup(old
, sizeof(*old
), GFP_KERNEL
);
201 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
206 WARN_ON(link
->owner
&& link
->owner
!= owner
);
209 WARN_ON(doit
&& link
->doit
&& link
->doit
!= doit
);
212 WARN_ON(dumpit
&& link
->dumpit
&& link
->dumpit
!= dumpit
);
214 link
->dumpit
= dumpit
;
216 link
->flags
|= flags
;
218 /* publish protocol:msgtype */
219 rcu_assign_pointer(tab
[msgindex
], link
);
229 * rtnl_register_module - Register a rtnetlink message type
231 * @owner: module registering the hook (THIS_MODULE)
232 * @protocol: Protocol family or PF_UNSPEC
233 * @msgtype: rtnetlink message type
234 * @doit: Function pointer called for each request message
235 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
236 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
238 * Like rtnl_register, but for use by removable modules.
240 int rtnl_register_module(struct module
*owner
,
241 int protocol
, int msgtype
,
242 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
245 return rtnl_register_internal(owner
, protocol
, msgtype
,
246 doit
, dumpit
, flags
);
248 EXPORT_SYMBOL_GPL(rtnl_register_module
);
251 * rtnl_register - Register a rtnetlink message type
252 * @protocol: Protocol family or PF_UNSPEC
253 * @msgtype: rtnetlink message type
254 * @doit: Function pointer called for each request message
255 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
256 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
258 * Registers the specified function pointers (at least one of them has
259 * to be non-NULL) to be called whenever a request message for the
260 * specified protocol family and message type is received.
262 * The special protocol family PF_UNSPEC may be used to define fallback
263 * function pointers for the case when no entry for the specific protocol
266 void rtnl_register(int protocol
, int msgtype
,
267 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
272 err
= rtnl_register_internal(NULL
, protocol
, msgtype
, doit
, dumpit
,
275 pr_err("Unable to register rtnetlink message handler, "
276 "protocol = %d, message type = %d\n", protocol
, msgtype
);
280 * rtnl_unregister - Unregister a rtnetlink message type
281 * @protocol: Protocol family or PF_UNSPEC
282 * @msgtype: rtnetlink message type
284 * Returns 0 on success or a negative error code.
286 int rtnl_unregister(int protocol
, int msgtype
)
288 struct rtnl_link
**tab
, *link
;
291 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
292 msgindex
= rtm_msgindex(msgtype
);
295 tab
= rtnl_dereference(rtnl_msg_handlers
[protocol
]);
301 link
= tab
[msgindex
];
302 rcu_assign_pointer(tab
[msgindex
], NULL
);
305 kfree_rcu(link
, rcu
);
309 EXPORT_SYMBOL_GPL(rtnl_unregister
);
312 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
313 * @protocol : Protocol family or PF_UNSPEC
315 * Identical to calling rtnl_unregster() for all registered message types
316 * of a certain protocol family.
318 void rtnl_unregister_all(int protocol
)
320 struct rtnl_link
**tab
, *link
;
323 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
326 tab
= rtnl_msg_handlers
[protocol
];
331 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
332 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
333 link
= tab
[msgindex
];
337 rcu_assign_pointer(tab
[msgindex
], NULL
);
338 kfree_rcu(link
, rcu
);
346 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
348 static LIST_HEAD(link_ops
);
350 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
352 const struct rtnl_link_ops
*ops
;
354 list_for_each_entry(ops
, &link_ops
, list
) {
355 if (!strcmp(ops
->kind
, kind
))
362 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
363 * @ops: struct rtnl_link_ops * to register
365 * The caller must hold the rtnl_mutex. This function should be used
366 * by drivers that create devices during module initialization. It
367 * must be called before registering the devices.
369 * Returns 0 on success or a negative error code.
371 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
373 if (rtnl_link_ops_get(ops
->kind
))
376 /* The check for setup is here because if ops
377 * does not have that filled up, it is not possible
378 * to use the ops for creating device. So do not
379 * fill up dellink as well. That disables rtnl_dellink.
381 if (ops
->setup
&& !ops
->dellink
)
382 ops
->dellink
= unregister_netdevice_queue
;
384 list_add_tail(&ops
->list
, &link_ops
);
387 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
390 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
391 * @ops: struct rtnl_link_ops * to register
393 * Returns 0 on success or a negative error code.
395 int rtnl_link_register(struct rtnl_link_ops
*ops
)
399 /* Sanity-check max sizes to avoid stack buffer overflow. */
400 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
401 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
405 err
= __rtnl_link_register(ops
);
409 EXPORT_SYMBOL_GPL(rtnl_link_register
);
411 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
413 struct net_device
*dev
;
414 LIST_HEAD(list_kill
);
416 for_each_netdev(net
, dev
) {
417 if (dev
->rtnl_link_ops
== ops
)
418 ops
->dellink(dev
, &list_kill
);
420 unregister_netdevice_many(&list_kill
);
424 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
425 * @ops: struct rtnl_link_ops * to unregister
427 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
428 * integrity (hold pernet_ops_rwsem for writing to close the race
429 * with setup_net() and cleanup_net()).
431 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
436 __rtnl_kill_links(net
, ops
);
438 list_del(&ops
->list
);
440 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
442 /* Return with the rtnl_lock held when there are no network
443 * devices unregistering in any network namespace.
445 static void rtnl_lock_unregistering_all(void)
449 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
451 add_wait_queue(&netdev_unregistering_wq
, &wait
);
453 unregistering
= false;
455 /* We held write locked pernet_ops_rwsem, and parallel
456 * setup_net() and cleanup_net() are not possible.
459 if (net
->dev_unreg_count
> 0) {
460 unregistering
= true;
468 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
470 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
474 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
475 * @ops: struct rtnl_link_ops * to unregister
477 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
479 /* Close the race with setup_net() and cleanup_net() */
480 down_write(&pernet_ops_rwsem
);
481 rtnl_lock_unregistering_all();
482 __rtnl_link_unregister(ops
);
484 up_write(&pernet_ops_rwsem
);
486 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
488 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
490 struct net_device
*master_dev
;
491 const struct rtnl_link_ops
*ops
;
496 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
500 ops
= master_dev
->rtnl_link_ops
;
501 if (!ops
|| !ops
->get_slave_size
)
503 /* IFLA_INFO_SLAVE_DATA + nested data */
504 size
= nla_total_size(sizeof(struct nlattr
)) +
505 ops
->get_slave_size(master_dev
, dev
);
512 static size_t rtnl_link_get_size(const struct net_device
*dev
)
514 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
520 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
521 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
524 /* IFLA_INFO_DATA + nested data */
525 size
+= nla_total_size(sizeof(struct nlattr
)) +
528 if (ops
->get_xstats_size
)
529 /* IFLA_INFO_XSTATS */
530 size
+= nla_total_size(ops
->get_xstats_size(dev
));
532 size
+= rtnl_link_get_slave_info_data_size(dev
);
537 static LIST_HEAD(rtnl_af_ops
);
539 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
541 const struct rtnl_af_ops
*ops
;
543 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
544 if (ops
->family
== family
)
552 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
553 * @ops: struct rtnl_af_ops * to register
555 * Returns 0 on success or a negative error code.
557 void rtnl_af_register(struct rtnl_af_ops
*ops
)
560 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
563 EXPORT_SYMBOL_GPL(rtnl_af_register
);
566 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
567 * @ops: struct rtnl_af_ops * to unregister
569 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
572 list_del_rcu(&ops
->list
);
577 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
579 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
582 struct rtnl_af_ops
*af_ops
;
586 size
= nla_total_size(sizeof(struct nlattr
));
589 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
590 if (af_ops
->get_link_af_size
) {
591 /* AF_* + nested data */
592 size
+= nla_total_size(sizeof(struct nlattr
)) +
593 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
601 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
603 struct net_device
*master_dev
;
608 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
609 if (master_dev
&& master_dev
->rtnl_link_ops
)
615 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
616 const struct net_device
*dev
)
618 struct net_device
*master_dev
;
619 const struct rtnl_link_ops
*ops
;
620 struct nlattr
*slave_data
;
623 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
626 ops
= master_dev
->rtnl_link_ops
;
629 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
631 if (ops
->fill_slave_info
) {
632 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
635 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
637 goto err_cancel_slave_data
;
638 nla_nest_end(skb
, slave_data
);
642 err_cancel_slave_data
:
643 nla_nest_cancel(skb
, slave_data
);
647 static int rtnl_link_info_fill(struct sk_buff
*skb
,
648 const struct net_device
*dev
)
650 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
656 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
658 if (ops
->fill_xstats
) {
659 err
= ops
->fill_xstats(skb
, dev
);
663 if (ops
->fill_info
) {
664 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
667 err
= ops
->fill_info(skb
, dev
);
669 goto err_cancel_data
;
670 nla_nest_end(skb
, data
);
675 nla_nest_cancel(skb
, data
);
679 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
681 struct nlattr
*linkinfo
;
684 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
685 if (linkinfo
== NULL
)
688 err
= rtnl_link_info_fill(skb
, dev
);
690 goto err_cancel_link
;
692 err
= rtnl_link_slave_info_fill(skb
, dev
);
694 goto err_cancel_link
;
696 nla_nest_end(skb
, linkinfo
);
700 nla_nest_cancel(skb
, linkinfo
);
705 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
707 struct sock
*rtnl
= net
->rtnl
;
710 NETLINK_CB(skb
).dst_group
= group
;
712 refcount_inc(&skb
->users
);
713 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
715 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
719 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
721 struct sock
*rtnl
= net
->rtnl
;
723 return nlmsg_unicast(rtnl
, skb
, pid
);
725 EXPORT_SYMBOL(rtnl_unicast
);
727 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
728 struct nlmsghdr
*nlh
, gfp_t flags
)
730 struct sock
*rtnl
= net
->rtnl
;
734 report
= nlmsg_report(nlh
);
736 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
738 EXPORT_SYMBOL(rtnl_notify
);
740 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
742 struct sock
*rtnl
= net
->rtnl
;
744 netlink_set_err(rtnl
, 0, group
, error
);
746 EXPORT_SYMBOL(rtnl_set_sk_err
);
748 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
753 mx
= nla_nest_start(skb
, RTA_METRICS
);
757 for (i
= 0; i
< RTAX_MAX
; i
++) {
759 if (i
== RTAX_CC_ALGO
- 1) {
760 char tmp
[TCP_CA_NAME_MAX
], *name
;
762 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
765 if (nla_put_string(skb
, i
+ 1, name
))
766 goto nla_put_failure
;
767 } else if (i
== RTAX_FEATURES
- 1) {
768 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
772 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
773 if (nla_put_u32(skb
, i
+ 1, user_features
))
774 goto nla_put_failure
;
776 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
777 goto nla_put_failure
;
784 nla_nest_cancel(skb
, mx
);
788 return nla_nest_end(skb
, mx
);
791 nla_nest_cancel(skb
, mx
);
794 EXPORT_SYMBOL(rtnetlink_put_metrics
);
796 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
797 long expires
, u32 error
)
799 struct rta_cacheinfo ci
= {
805 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
806 ci
.rta_used
= dst
->__use
;
807 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
812 clock
= jiffies_to_clock_t(abs(expires
));
813 clock
= min_t(unsigned long, clock
, INT_MAX
);
814 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
816 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
818 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
820 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
822 unsigned char operstate
= dev
->operstate
;
824 switch (transition
) {
826 if ((operstate
== IF_OPER_DORMANT
||
827 operstate
== IF_OPER_UNKNOWN
) &&
829 operstate
= IF_OPER_UP
;
832 case IF_OPER_DORMANT
:
833 if (operstate
== IF_OPER_UP
||
834 operstate
== IF_OPER_UNKNOWN
)
835 operstate
= IF_OPER_DORMANT
;
839 if (dev
->operstate
!= operstate
) {
840 write_lock_bh(&dev_base_lock
);
841 dev
->operstate
= operstate
;
842 write_unlock_bh(&dev_base_lock
);
843 netdev_state_change(dev
);
847 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
849 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
850 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
853 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
854 const struct ifinfomsg
*ifm
)
856 unsigned int flags
= ifm
->ifi_flags
;
858 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
860 flags
= (flags
& ifm
->ifi_change
) |
861 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
866 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
867 const struct rtnl_link_stats64
*b
)
869 a
->rx_packets
= b
->rx_packets
;
870 a
->tx_packets
= b
->tx_packets
;
871 a
->rx_bytes
= b
->rx_bytes
;
872 a
->tx_bytes
= b
->tx_bytes
;
873 a
->rx_errors
= b
->rx_errors
;
874 a
->tx_errors
= b
->tx_errors
;
875 a
->rx_dropped
= b
->rx_dropped
;
876 a
->tx_dropped
= b
->tx_dropped
;
878 a
->multicast
= b
->multicast
;
879 a
->collisions
= b
->collisions
;
881 a
->rx_length_errors
= b
->rx_length_errors
;
882 a
->rx_over_errors
= b
->rx_over_errors
;
883 a
->rx_crc_errors
= b
->rx_crc_errors
;
884 a
->rx_frame_errors
= b
->rx_frame_errors
;
885 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
886 a
->rx_missed_errors
= b
->rx_missed_errors
;
888 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
889 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
890 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
891 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
892 a
->tx_window_errors
= b
->tx_window_errors
;
894 a
->rx_compressed
= b
->rx_compressed
;
895 a
->tx_compressed
= b
->tx_compressed
;
897 a
->rx_nohandler
= b
->rx_nohandler
;
901 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
904 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
905 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
906 size_t size
= nla_total_size(0);
909 nla_total_size(sizeof(struct ifla_vf_mac
)) +
910 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
911 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
912 nla_total_size(MAX_VLAN_LIST_LEN
*
913 sizeof(struct ifla_vf_vlan_info
)) +
914 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
915 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
916 nla_total_size(sizeof(struct ifla_vf_rate
)) +
917 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
918 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
919 nla_total_size(0) + /* nest IFLA_VF_STATS */
920 /* IFLA_VF_STATS_RX_PACKETS */
921 nla_total_size_64bit(sizeof(__u64
)) +
922 /* IFLA_VF_STATS_TX_PACKETS */
923 nla_total_size_64bit(sizeof(__u64
)) +
924 /* IFLA_VF_STATS_RX_BYTES */
925 nla_total_size_64bit(sizeof(__u64
)) +
926 /* IFLA_VF_STATS_TX_BYTES */
927 nla_total_size_64bit(sizeof(__u64
)) +
928 /* IFLA_VF_STATS_BROADCAST */
929 nla_total_size_64bit(sizeof(__u64
)) +
930 /* IFLA_VF_STATS_MULTICAST */
931 nla_total_size_64bit(sizeof(__u64
)) +
932 /* IFLA_VF_STATS_RX_DROPPED */
933 nla_total_size_64bit(sizeof(__u64
)) +
934 /* IFLA_VF_STATS_TX_DROPPED */
935 nla_total_size_64bit(sizeof(__u64
)) +
936 nla_total_size(sizeof(struct ifla_vf_trust
)));
942 static size_t rtnl_port_size(const struct net_device
*dev
,
945 size_t port_size
= nla_total_size(4) /* PORT_VF */
946 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
947 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
948 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
949 + nla_total_size(1) /* PROT_VDP_REQUEST */
950 + nla_total_size(2); /* PORT_VDP_RESPONSE */
951 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
952 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
954 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
957 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
958 !(ext_filter_mask
& RTEXT_FILTER_VF
))
960 if (dev_num_vf(dev
->dev
.parent
))
961 return port_self_size
+ vf_ports_size
+
962 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
964 return port_self_size
;
967 static size_t rtnl_xdp_size(void)
969 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
970 nla_total_size(1) + /* XDP_ATTACHED */
971 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
972 nla_total_size(4); /* XDP_<mode>_PROG_ID */
977 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
980 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
981 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
982 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
983 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
984 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
985 + nla_total_size(sizeof(struct rtnl_link_stats
))
986 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
987 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
988 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
989 + nla_total_size(4) /* IFLA_TXQLEN */
990 + nla_total_size(4) /* IFLA_WEIGHT */
991 + nla_total_size(4) /* IFLA_MTU */
992 + nla_total_size(4) /* IFLA_LINK */
993 + nla_total_size(4) /* IFLA_MASTER */
994 + nla_total_size(1) /* IFLA_CARRIER */
995 + nla_total_size(4) /* IFLA_PROMISCUITY */
996 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
997 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
998 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1000 + nla_total_size(1) /* IFLA_OPERSTATE */
1001 + nla_total_size(1) /* IFLA_LINKMODE */
1002 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1003 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1004 + nla_total_size(4) /* IFLA_GROUP */
1005 + nla_total_size(ext_filter_mask
1006 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1007 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1008 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1009 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1010 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1011 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1013 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1014 + rtnl_xdp_size() /* IFLA_XDP */
1015 + nla_total_size(4) /* IFLA_EVENT */
1016 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1017 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1018 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1019 + nla_total_size(4) /* IFLA_IF_NETNSID */
1020 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1021 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1022 + nla_total_size(4) /* IFLA_MIN_MTU */
1023 + nla_total_size(4) /* IFLA_MAX_MTU */
1027 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1029 struct nlattr
*vf_ports
;
1030 struct nlattr
*vf_port
;
1034 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
1038 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1039 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
1041 goto nla_put_failure
;
1042 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1043 goto nla_put_failure
;
1044 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1045 if (err
== -EMSGSIZE
)
1046 goto nla_put_failure
;
1048 nla_nest_cancel(skb
, vf_port
);
1051 nla_nest_end(skb
, vf_port
);
1054 nla_nest_end(skb
, vf_ports
);
1059 nla_nest_cancel(skb
, vf_ports
);
1063 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1065 struct nlattr
*port_self
;
1068 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
1072 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1074 nla_nest_cancel(skb
, port_self
);
1075 return (err
== -EMSGSIZE
) ? err
: 0;
1078 nla_nest_end(skb
, port_self
);
1083 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1084 u32 ext_filter_mask
)
1088 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1089 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1092 err
= rtnl_port_self_fill(skb
, dev
);
1096 if (dev_num_vf(dev
->dev
.parent
)) {
1097 err
= rtnl_vf_ports_fill(skb
, dev
);
1105 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1108 struct netdev_phys_item_id ppid
;
1110 err
= dev_get_phys_port_id(dev
, &ppid
);
1112 if (err
== -EOPNOTSUPP
)
1117 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1123 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1125 char name
[IFNAMSIZ
];
1128 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1130 if (err
== -EOPNOTSUPP
)
1135 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1141 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1144 struct switchdev_attr attr
= {
1146 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1147 .flags
= SWITCHDEV_F_NO_RECURSE
,
1150 err
= switchdev_port_attr_get(dev
, &attr
);
1152 if (err
== -EOPNOTSUPP
)
1157 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1164 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1165 struct net_device
*dev
)
1167 struct rtnl_link_stats64
*sp
;
1168 struct nlattr
*attr
;
1170 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1171 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1175 sp
= nla_data(attr
);
1176 dev_get_stats(dev
, sp
);
1178 attr
= nla_reserve(skb
, IFLA_STATS
,
1179 sizeof(struct rtnl_link_stats
));
1183 copy_rtnl_link_stats(nla_data(attr
), sp
);
1188 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1189 struct net_device
*dev
,
1191 struct nlattr
*vfinfo
)
1193 struct ifla_vf_rss_query_en vf_rss_query_en
;
1194 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1195 struct ifla_vf_link_state vf_linkstate
;
1196 struct ifla_vf_vlan_info vf_vlan_info
;
1197 struct ifla_vf_spoofchk vf_spoofchk
;
1198 struct ifla_vf_tx_rate vf_tx_rate
;
1199 struct ifla_vf_stats vf_stats
;
1200 struct ifla_vf_trust vf_trust
;
1201 struct ifla_vf_vlan vf_vlan
;
1202 struct ifla_vf_rate vf_rate
;
1203 struct ifla_vf_mac vf_mac
;
1204 struct ifla_vf_info ivi
;
1206 memset(&ivi
, 0, sizeof(ivi
));
1208 /* Not all SR-IOV capable drivers support the
1209 * spoofcheck and "RSS query enable" query. Preset to
1210 * -1 so the user space tool can detect that the driver
1211 * didn't report anything.
1214 ivi
.rss_query_en
= -1;
1216 /* The default value for VF link state is "auto"
1217 * IFLA_VF_LINK_STATE_AUTO which equals zero
1220 /* VLAN Protocol by default is 802.1Q */
1221 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1222 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1225 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1234 vf_rss_query_en
.vf
=
1235 vf_trust
.vf
= ivi
.vf
;
1237 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1238 vf_vlan
.vlan
= ivi
.vlan
;
1239 vf_vlan
.qos
= ivi
.qos
;
1240 vf_vlan_info
.vlan
= ivi
.vlan
;
1241 vf_vlan_info
.qos
= ivi
.qos
;
1242 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1243 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1244 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1245 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1246 vf_spoofchk
.setting
= ivi
.spoofchk
;
1247 vf_linkstate
.link_state
= ivi
.linkstate
;
1248 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1249 vf_trust
.setting
= ivi
.trusted
;
1250 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1252 goto nla_put_vfinfo_failure
;
1253 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1254 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1255 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1257 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1259 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1261 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1263 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1264 sizeof(vf_rss_query_en
),
1265 &vf_rss_query_en
) ||
1266 nla_put(skb
, IFLA_VF_TRUST
,
1267 sizeof(vf_trust
), &vf_trust
))
1268 goto nla_put_vf_failure
;
1269 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1271 goto nla_put_vf_failure
;
1272 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1274 nla_nest_cancel(skb
, vfvlanlist
);
1275 goto nla_put_vf_failure
;
1277 nla_nest_end(skb
, vfvlanlist
);
1278 memset(&vf_stats
, 0, sizeof(vf_stats
));
1279 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1280 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1282 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1284 goto nla_put_vf_failure
;
1285 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1286 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1287 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1288 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1289 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1290 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1291 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1292 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1293 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1294 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1295 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1296 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1297 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1298 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1299 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1300 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1301 nla_nest_cancel(skb
, vfstats
);
1302 goto nla_put_vf_failure
;
1304 nla_nest_end(skb
, vfstats
);
1305 nla_nest_end(skb
, vf
);
1309 nla_nest_cancel(skb
, vf
);
1310 nla_put_vfinfo_failure
:
1311 nla_nest_cancel(skb
, vfinfo
);
1315 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1316 struct net_device
*dev
,
1317 u32 ext_filter_mask
)
1319 struct nlattr
*vfinfo
;
1322 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1325 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1326 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1329 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1332 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1336 for (i
= 0; i
< num_vfs
; i
++) {
1337 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1341 nla_nest_end(skb
, vfinfo
);
1345 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1347 struct rtnl_link_ifmap map
;
1349 memset(&map
, 0, sizeof(map
));
1350 map
.mem_start
= dev
->mem_start
;
1351 map
.mem_end
= dev
->mem_end
;
1352 map
.base_addr
= dev
->base_addr
;
1355 map
.port
= dev
->if_port
;
1357 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1363 static u32
rtnl_xdp_prog_skb(struct net_device
*dev
)
1365 const struct bpf_prog
*generic_xdp_prog
;
1369 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1370 if (!generic_xdp_prog
)
1372 return generic_xdp_prog
->aux
->id
;
1375 static u32
rtnl_xdp_prog_drv(struct net_device
*dev
)
1377 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
, XDP_QUERY_PROG
);
1380 static u32
rtnl_xdp_prog_hw(struct net_device
*dev
)
1382 return __dev_xdp_query(dev
, dev
->netdev_ops
->ndo_bpf
,
1386 static int rtnl_xdp_report_one(struct sk_buff
*skb
, struct net_device
*dev
,
1387 u32
*prog_id
, u8
*mode
, u8 tgt_mode
, u32 attr
,
1388 u32 (*get_prog_id
)(struct net_device
*dev
))
1393 curr_id
= get_prog_id(dev
);
1398 err
= nla_put_u32(skb
, attr
, curr_id
);
1402 if (*mode
!= XDP_ATTACHED_NONE
)
1403 *mode
= XDP_ATTACHED_MULTI
;
1410 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1417 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1422 mode
= XDP_ATTACHED_NONE
;
1423 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_SKB
,
1424 IFLA_XDP_SKB_PROG_ID
, rtnl_xdp_prog_skb
);
1427 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_DRV
,
1428 IFLA_XDP_DRV_PROG_ID
, rtnl_xdp_prog_drv
);
1431 err
= rtnl_xdp_report_one(skb
, dev
, &prog_id
, &mode
, XDP_ATTACHED_HW
,
1432 IFLA_XDP_HW_PROG_ID
, rtnl_xdp_prog_hw
);
1436 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, mode
);
1440 if (prog_id
&& mode
!= XDP_ATTACHED_MULTI
) {
1441 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1446 nla_nest_end(skb
, xdp
);
1450 nla_nest_cancel(skb
, xdp
);
1454 static u32
rtnl_get_event(unsigned long event
)
1456 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1460 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1462 case NETDEV_FEAT_CHANGE
:
1463 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1465 case NETDEV_BONDING_FAILOVER
:
1466 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1468 case NETDEV_NOTIFY_PEERS
:
1469 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1471 case NETDEV_RESEND_IGMP
:
1472 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1474 case NETDEV_CHANGEINFODATA
:
1475 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1481 return rtnl_event_type
;
1484 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1486 const struct net_device
*upper_dev
;
1491 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1493 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1499 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
,
1502 int ifindex
= dev_get_iflink(dev
);
1504 if (force
|| dev
->ifindex
!= ifindex
)
1505 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1510 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1511 struct net_device
*dev
)
1516 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1517 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1520 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1521 const struct net_device
*dev
,
1522 struct net
*src_net
, gfp_t gfp
)
1524 bool put_iflink
= false;
1526 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1527 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1529 if (!net_eq(dev_net(dev
), link_net
)) {
1530 int id
= peernet2id_alloc(src_net
, link_net
, gfp
);
1532 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1539 return nla_put_iflink(skb
, dev
, put_iflink
);
1542 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1543 const struct net_device
*dev
,
1544 u32 ext_filter_mask
)
1546 const struct rtnl_af_ops
*af_ops
;
1547 struct nlattr
*af_spec
;
1549 af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
1553 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1557 if (!af_ops
->fill_link_af
)
1560 af
= nla_nest_start(skb
, af_ops
->family
);
1564 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1566 * Caller may return ENODATA to indicate that there
1567 * was no data to be dumped. This is not an error, it
1568 * means we should trim the attribute header and
1571 if (err
== -ENODATA
)
1572 nla_nest_cancel(skb
, af
);
1576 nla_nest_end(skb
, af
);
1579 nla_nest_end(skb
, af_spec
);
1583 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1584 struct net_device
*dev
, struct net
*src_net
,
1585 int type
, u32 pid
, u32 seq
, u32 change
,
1586 unsigned int flags
, u32 ext_filter_mask
,
1587 u32 event
, int *new_nsid
, int new_ifindex
,
1588 int tgt_netnsid
, gfp_t gfp
)
1590 struct ifinfomsg
*ifm
;
1591 struct nlmsghdr
*nlh
;
1594 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1598 ifm
= nlmsg_data(nlh
);
1599 ifm
->ifi_family
= AF_UNSPEC
;
1601 ifm
->ifi_type
= dev
->type
;
1602 ifm
->ifi_index
= dev
->ifindex
;
1603 ifm
->ifi_flags
= dev_get_flags(dev
);
1604 ifm
->ifi_change
= change
;
1606 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_IF_NETNSID
, tgt_netnsid
))
1607 goto nla_put_failure
;
1609 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1610 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1611 nla_put_u8(skb
, IFLA_OPERSTATE
,
1612 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1613 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1614 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1615 nla_put_u32(skb
, IFLA_MIN_MTU
, dev
->min_mtu
) ||
1616 nla_put_u32(skb
, IFLA_MAX_MTU
, dev
->max_mtu
) ||
1617 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1618 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1619 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1620 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1621 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1623 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1625 put_master_ifindex(skb
, dev
) ||
1626 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1628 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1629 nla_put_ifalias(skb
, dev
) ||
1630 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1631 atomic_read(&dev
->carrier_up_count
) +
1632 atomic_read(&dev
->carrier_down_count
)) ||
1633 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1634 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1635 atomic_read(&dev
->carrier_up_count
)) ||
1636 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1637 atomic_read(&dev
->carrier_down_count
)))
1638 goto nla_put_failure
;
1640 if (event
!= IFLA_EVENT_NONE
) {
1641 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1642 goto nla_put_failure
;
1645 if (rtnl_fill_link_ifmap(skb
, dev
))
1646 goto nla_put_failure
;
1648 if (dev
->addr_len
) {
1649 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1650 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1651 goto nla_put_failure
;
1654 if (rtnl_phys_port_id_fill(skb
, dev
))
1655 goto nla_put_failure
;
1657 if (rtnl_phys_port_name_fill(skb
, dev
))
1658 goto nla_put_failure
;
1660 if (rtnl_phys_switch_id_fill(skb
, dev
))
1661 goto nla_put_failure
;
1663 if (rtnl_fill_stats(skb
, dev
))
1664 goto nla_put_failure
;
1666 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1667 goto nla_put_failure
;
1669 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1670 goto nla_put_failure
;
1672 if (rtnl_xdp_fill(skb
, dev
))
1673 goto nla_put_failure
;
1675 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1676 if (rtnl_link_fill(skb
, dev
) < 0)
1677 goto nla_put_failure
;
1680 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
, gfp
))
1681 goto nla_put_failure
;
1684 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1685 goto nla_put_failure
;
1687 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1688 goto nla_put_failure
;
1692 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1693 goto nla_put_failure_rcu
;
1696 nlmsg_end(skb
, nlh
);
1699 nla_put_failure_rcu
:
1702 nlmsg_cancel(skb
, nlh
);
1706 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1707 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1708 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1709 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1710 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1711 [IFLA_MTU
] = { .type
= NLA_U32
},
1712 [IFLA_LINK
] = { .type
= NLA_U32
},
1713 [IFLA_MASTER
] = { .type
= NLA_U32
},
1714 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1715 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1716 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1717 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1718 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1719 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1720 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1721 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1722 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1723 * allow 0-length string (needed to remove an alias).
1725 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1726 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1727 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1728 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1729 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1730 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1731 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1732 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1733 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1734 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1735 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1736 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1737 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1738 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1739 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1740 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1741 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1742 [IFLA_EVENT
] = { .type
= NLA_U32
},
1743 [IFLA_GROUP
] = { .type
= NLA_U32
},
1744 [IFLA_IF_NETNSID
] = { .type
= NLA_S32
},
1745 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1746 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1747 [IFLA_MIN_MTU
] = { .type
= NLA_U32
},
1748 [IFLA_MAX_MTU
] = { .type
= NLA_U32
},
1751 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1752 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1753 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1754 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1755 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1758 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1759 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1760 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1761 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1762 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1763 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1764 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1765 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1766 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1767 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1768 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1769 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1770 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1773 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1774 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1775 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1776 .len
= PORT_PROFILE_MAX
},
1777 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1778 .len
= PORT_UUID_MAX
},
1779 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1780 .len
= PORT_UUID_MAX
},
1781 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1782 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1784 /* Unused, but we need to keep it here since user space could
1785 * fill it. It's also broken with regard to NLA_BINARY use in
1786 * combination with structs.
1788 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1789 .len
= sizeof(struct ifla_port_vsi
) },
1792 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1793 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1794 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1795 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1796 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1799 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1801 const struct rtnl_link_ops
*ops
= NULL
;
1802 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1804 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1805 ifla_info_policy
, NULL
) < 0)
1808 if (linfo
[IFLA_INFO_KIND
]) {
1809 char kind
[MODULE_NAME_LEN
];
1811 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1812 ops
= rtnl_link_ops_get(kind
);
1818 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1820 struct net_device
*master
;
1825 master
= netdev_master_upper_dev_get(dev
);
1826 if (!master
|| master
->ifindex
!= master_idx
)
1832 static bool link_kind_filtered(const struct net_device
*dev
,
1833 const struct rtnl_link_ops
*kind_ops
)
1835 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1841 static bool link_dump_filtered(struct net_device
*dev
,
1843 const struct rtnl_link_ops
*kind_ops
)
1845 if (link_master_filtered(dev
, master_idx
) ||
1846 link_kind_filtered(dev
, kind_ops
))
1852 static struct net
*get_target_net(struct sock
*sk
, int netnsid
)
1856 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1858 return ERR_PTR(-EINVAL
);
1860 /* For now, the caller is required to have CAP_NET_ADMIN in
1861 * the user namespace owning the target net ns.
1863 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1865 return ERR_PTR(-EACCES
);
1870 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1872 struct net
*net
= sock_net(skb
->sk
);
1873 struct net
*tgt_net
= net
;
1876 struct net_device
*dev
;
1877 struct hlist_head
*head
;
1878 struct nlattr
*tb
[IFLA_MAX
+1];
1879 u32 ext_filter_mask
= 0;
1880 const struct rtnl_link_ops
*kind_ops
= NULL
;
1881 unsigned int flags
= NLM_F_MULTI
;
1888 s_idx
= cb
->args
[1];
1890 /* A hack to preserve kernel<->userspace interface.
1891 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1892 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1893 * what iproute2 < v3.9.0 used.
1894 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1895 * attribute, its netlink message is shorter than struct ifinfomsg.
1897 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1898 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1900 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
,
1901 ifla_policy
, NULL
) >= 0) {
1902 if (tb
[IFLA_IF_NETNSID
]) {
1903 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
1904 tgt_net
= get_target_net(skb
->sk
, netnsid
);
1905 if (IS_ERR(tgt_net
))
1906 return PTR_ERR(tgt_net
);
1909 if (tb
[IFLA_EXT_MASK
])
1910 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1912 if (tb
[IFLA_MASTER
])
1913 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1915 if (tb
[IFLA_LINKINFO
])
1916 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1918 if (master_idx
|| kind_ops
)
1919 flags
|= NLM_F_DUMP_FILTERED
;
1922 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1924 head
= &tgt_net
->dev_index_head
[h
];
1925 hlist_for_each_entry(dev
, head
, index_hlist
) {
1926 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1930 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1932 NETLINK_CB(cb
->skb
).portid
,
1933 cb
->nlh
->nlmsg_seq
, 0,
1935 ext_filter_mask
, 0, NULL
, 0,
1936 netnsid
, GFP_KERNEL
);
1939 if (likely(skb
->len
))
1953 cb
->seq
= net
->dev_base_seq
;
1954 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1961 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
1962 struct netlink_ext_ack
*exterr
)
1964 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
1966 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1968 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1971 /* Examine the link attributes and figure out which
1972 * network namespace we are talking about.
1974 if (tb
[IFLA_NET_NS_PID
])
1975 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1976 else if (tb
[IFLA_NET_NS_FD
])
1977 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1979 net
= get_net(src_net
);
1982 EXPORT_SYMBOL(rtnl_link_get_net
);
1984 /* Figure out which network namespace we are talking about by
1985 * examining the link attributes in the following order:
1987 * 1. IFLA_NET_NS_PID
1989 * 3. IFLA_IF_NETNSID
1991 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
1992 struct nlattr
*tb
[])
1996 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
1997 return rtnl_link_get_net(src_net
, tb
);
1999 if (!tb
[IFLA_IF_NETNSID
])
2000 return get_net(src_net
);
2002 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_IF_NETNSID
]));
2004 return ERR_PTR(-EINVAL
);
2009 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
2010 struct net
*src_net
,
2011 struct nlattr
*tb
[], int cap
)
2015 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
2019 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
2021 return ERR_PTR(-EPERM
);
2027 /* Verify that rtnetlink requests do not pass additional properties
2028 * potentially referring to different network namespaces.
2030 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
2031 struct netlink_ext_ack
*extack
,
2035 if (netns_id_only
) {
2036 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
2039 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
2043 if (tb
[IFLA_IF_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
2046 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
2049 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2055 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2059 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2062 if (tb
[IFLA_ADDRESS
] &&
2063 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2066 if (tb
[IFLA_BROADCAST
] &&
2067 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2071 if (tb
[IFLA_AF_SPEC
]) {
2075 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2076 const struct rtnl_af_ops
*af_ops
;
2079 af_ops
= rtnl_af_lookup(nla_type(af
));
2082 return -EAFNOSUPPORT
;
2085 if (!af_ops
->set_link_af
) {
2090 if (af_ops
->validate_link_af
) {
2091 err
= af_ops
->validate_link_af(dev
, af
);
2105 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2108 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2110 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2113 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2115 if (dev
->type
!= ARPHRD_INFINIBAND
)
2118 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2121 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2123 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2126 if (tb
[IFLA_VF_MAC
]) {
2127 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2129 if (ivm
->vf
>= INT_MAX
)
2132 if (ops
->ndo_set_vf_mac
)
2133 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2139 if (tb
[IFLA_VF_VLAN
]) {
2140 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2142 if (ivv
->vf
>= INT_MAX
)
2145 if (ops
->ndo_set_vf_vlan
)
2146 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2148 htons(ETH_P_8021Q
));
2153 if (tb
[IFLA_VF_VLAN_LIST
]) {
2154 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2155 struct nlattr
*attr
;
2159 if (!ops
->ndo_set_vf_vlan
)
2162 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2163 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2164 nla_len(attr
) < NLA_HDRLEN
) {
2167 if (len
>= MAX_VLAN_LIST_LEN
)
2169 ivvl
[len
] = nla_data(attr
);
2176 if (ivvl
[0]->vf
>= INT_MAX
)
2178 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2179 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2184 if (tb
[IFLA_VF_TX_RATE
]) {
2185 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2186 struct ifla_vf_info ivf
;
2188 if (ivt
->vf
>= INT_MAX
)
2191 if (ops
->ndo_get_vf_config
)
2192 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2197 if (ops
->ndo_set_vf_rate
)
2198 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2205 if (tb
[IFLA_VF_RATE
]) {
2206 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2208 if (ivt
->vf
>= INT_MAX
)
2211 if (ops
->ndo_set_vf_rate
)
2212 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2219 if (tb
[IFLA_VF_SPOOFCHK
]) {
2220 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2222 if (ivs
->vf
>= INT_MAX
)
2225 if (ops
->ndo_set_vf_spoofchk
)
2226 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2232 if (tb
[IFLA_VF_LINK_STATE
]) {
2233 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2235 if (ivl
->vf
>= INT_MAX
)
2238 if (ops
->ndo_set_vf_link_state
)
2239 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2245 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2246 struct ifla_vf_rss_query_en
*ivrssq_en
;
2249 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2250 if (ivrssq_en
->vf
>= INT_MAX
)
2252 if (ops
->ndo_set_vf_rss_query_en
)
2253 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2254 ivrssq_en
->setting
);
2259 if (tb
[IFLA_VF_TRUST
]) {
2260 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2262 if (ivt
->vf
>= INT_MAX
)
2265 if (ops
->ndo_set_vf_trust
)
2266 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2271 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2272 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2274 if (ivt
->vf
>= INT_MAX
)
2276 if (!ops
->ndo_set_vf_guid
)
2278 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2281 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2282 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2284 if (ivt
->vf
>= INT_MAX
)
2286 if (!ops
->ndo_set_vf_guid
)
2289 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2295 static int do_set_master(struct net_device
*dev
, int ifindex
,
2296 struct netlink_ext_ack
*extack
)
2298 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2299 const struct net_device_ops
*ops
;
2303 if (upper_dev
->ifindex
== ifindex
)
2305 ops
= upper_dev
->netdev_ops
;
2306 if (ops
->ndo_del_slave
) {
2307 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2316 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2319 ops
= upper_dev
->netdev_ops
;
2320 if (ops
->ndo_add_slave
) {
2321 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2331 #define DO_SETLINK_MODIFIED 0x01
2332 /* notify flag means notify + modified. */
2333 #define DO_SETLINK_NOTIFY 0x03
2334 static int do_setlink(const struct sk_buff
*skb
,
2335 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2336 struct netlink_ext_ack
*extack
,
2337 struct nlattr
**tb
, char *ifname
, int status
)
2339 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2342 err
= validate_linkmsg(dev
, tb
);
2346 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_IF_NETNSID
]) {
2347 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2354 err
= dev_change_net_namespace(dev
, net
, ifname
);
2358 status
|= DO_SETLINK_MODIFIED
;
2362 struct rtnl_link_ifmap
*u_map
;
2365 if (!ops
->ndo_set_config
) {
2370 if (!netif_device_present(dev
)) {
2375 u_map
= nla_data(tb
[IFLA_MAP
]);
2376 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2377 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2378 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2379 k_map
.irq
= (unsigned char) u_map
->irq
;
2380 k_map
.dma
= (unsigned char) u_map
->dma
;
2381 k_map
.port
= (unsigned char) u_map
->port
;
2383 err
= ops
->ndo_set_config(dev
, &k_map
);
2387 status
|= DO_SETLINK_NOTIFY
;
2390 if (tb
[IFLA_ADDRESS
]) {
2391 struct sockaddr
*sa
;
2394 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2396 sa
= kmalloc(len
, GFP_KERNEL
);
2401 sa
->sa_family
= dev
->type
;
2402 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2404 err
= dev_set_mac_address(dev
, sa
);
2408 status
|= DO_SETLINK_MODIFIED
;
2412 err
= dev_set_mtu_ext(dev
, nla_get_u32(tb
[IFLA_MTU
]), extack
);
2415 status
|= DO_SETLINK_MODIFIED
;
2418 if (tb
[IFLA_GROUP
]) {
2419 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2420 status
|= DO_SETLINK_NOTIFY
;
2424 * Interface selected by interface index but interface
2425 * name provided implies that a name change has been
2428 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2429 err
= dev_change_name(dev
, ifname
);
2432 status
|= DO_SETLINK_MODIFIED
;
2435 if (tb
[IFLA_IFALIAS
]) {
2436 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2437 nla_len(tb
[IFLA_IFALIAS
]));
2440 status
|= DO_SETLINK_NOTIFY
;
2443 if (tb
[IFLA_BROADCAST
]) {
2444 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2445 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2448 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2449 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2454 if (tb
[IFLA_MASTER
]) {
2455 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2458 status
|= DO_SETLINK_MODIFIED
;
2461 if (tb
[IFLA_CARRIER
]) {
2462 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2465 status
|= DO_SETLINK_MODIFIED
;
2468 if (tb
[IFLA_TXQLEN
]) {
2469 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2471 err
= dev_change_tx_queue_len(dev
, value
);
2474 status
|= DO_SETLINK_MODIFIED
;
2477 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2478 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2480 if (max_size
> GSO_MAX_SIZE
) {
2485 if (dev
->gso_max_size
^ max_size
) {
2486 netif_set_gso_max_size(dev
, max_size
);
2487 status
|= DO_SETLINK_MODIFIED
;
2491 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2492 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2494 if (max_segs
> GSO_MAX_SEGS
) {
2499 if (dev
->gso_max_segs
^ max_segs
) {
2500 dev
->gso_max_segs
= max_segs
;
2501 status
|= DO_SETLINK_MODIFIED
;
2505 if (tb
[IFLA_OPERSTATE
])
2506 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2508 if (tb
[IFLA_LINKMODE
]) {
2509 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2511 write_lock_bh(&dev_base_lock
);
2512 if (dev
->link_mode
^ value
)
2513 status
|= DO_SETLINK_NOTIFY
;
2514 dev
->link_mode
= value
;
2515 write_unlock_bh(&dev_base_lock
);
2518 if (tb
[IFLA_VFINFO_LIST
]) {
2519 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2520 struct nlattr
*attr
;
2523 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2524 if (nla_type(attr
) != IFLA_VF_INFO
||
2525 nla_len(attr
) < NLA_HDRLEN
) {
2529 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2530 ifla_vf_policy
, NULL
);
2533 err
= do_setvfinfo(dev
, vfinfo
);
2536 status
|= DO_SETLINK_NOTIFY
;
2541 if (tb
[IFLA_VF_PORTS
]) {
2542 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2543 struct nlattr
*attr
;
2548 if (!ops
->ndo_set_vf_port
)
2551 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2552 if (nla_type(attr
) != IFLA_VF_PORT
||
2553 nla_len(attr
) < NLA_HDRLEN
) {
2557 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2558 ifla_port_policy
, NULL
);
2561 if (!port
[IFLA_PORT_VF
]) {
2565 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2566 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2569 status
|= DO_SETLINK_NOTIFY
;
2574 if (tb
[IFLA_PORT_SELF
]) {
2575 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2577 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2578 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2584 if (ops
->ndo_set_vf_port
)
2585 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2588 status
|= DO_SETLINK_NOTIFY
;
2591 if (tb
[IFLA_AF_SPEC
]) {
2595 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2596 const struct rtnl_af_ops
*af_ops
;
2600 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2602 err
= af_ops
->set_link_af(dev
, af
);
2609 status
|= DO_SETLINK_NOTIFY
;
2614 if (tb
[IFLA_PROTO_DOWN
]) {
2615 err
= dev_change_proto_down(dev
,
2616 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2619 status
|= DO_SETLINK_NOTIFY
;
2623 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2626 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2627 ifla_xdp_policy
, NULL
);
2631 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2636 if (xdp
[IFLA_XDP_FLAGS
]) {
2637 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2638 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2642 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2648 if (xdp
[IFLA_XDP_FD
]) {
2649 err
= dev_change_xdp_fd(dev
, extack
,
2650 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2654 status
|= DO_SETLINK_NOTIFY
;
2659 if (status
& DO_SETLINK_MODIFIED
) {
2660 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2661 netdev_state_change(dev
);
2664 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",
2671 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2672 struct netlink_ext_ack
*extack
)
2674 struct net
*net
= sock_net(skb
->sk
);
2675 struct ifinfomsg
*ifm
;
2676 struct net_device
*dev
;
2678 struct nlattr
*tb
[IFLA_MAX
+1];
2679 char ifname
[IFNAMSIZ
];
2681 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2686 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2690 if (tb
[IFLA_IFNAME
])
2691 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2696 ifm
= nlmsg_data(nlh
);
2697 if (ifm
->ifi_index
> 0)
2698 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2699 else if (tb
[IFLA_IFNAME
])
2700 dev
= __dev_get_by_name(net
, ifname
);
2709 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2714 static int rtnl_group_dellink(const struct net
*net
, int group
)
2716 struct net_device
*dev
, *aux
;
2717 LIST_HEAD(list_kill
);
2723 for_each_netdev(net
, dev
) {
2724 if (dev
->group
== group
) {
2725 const struct rtnl_link_ops
*ops
;
2728 ops
= dev
->rtnl_link_ops
;
2729 if (!ops
|| !ops
->dellink
)
2737 for_each_netdev_safe(net
, dev
, aux
) {
2738 if (dev
->group
== group
) {
2739 const struct rtnl_link_ops
*ops
;
2741 ops
= dev
->rtnl_link_ops
;
2742 ops
->dellink(dev
, &list_kill
);
2745 unregister_netdevice_many(&list_kill
);
2750 int rtnl_delete_link(struct net_device
*dev
)
2752 const struct rtnl_link_ops
*ops
;
2753 LIST_HEAD(list_kill
);
2755 ops
= dev
->rtnl_link_ops
;
2756 if (!ops
|| !ops
->dellink
)
2759 ops
->dellink(dev
, &list_kill
);
2760 unregister_netdevice_many(&list_kill
);
2764 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2766 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2767 struct netlink_ext_ack
*extack
)
2769 struct net
*net
= sock_net(skb
->sk
);
2770 struct net
*tgt_net
= net
;
2771 struct net_device
*dev
= NULL
;
2772 struct ifinfomsg
*ifm
;
2773 char ifname
[IFNAMSIZ
];
2774 struct nlattr
*tb
[IFLA_MAX
+1];
2778 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2782 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2786 if (tb
[IFLA_IFNAME
])
2787 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2789 if (tb
[IFLA_IF_NETNSID
]) {
2790 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
2791 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
2792 if (IS_ERR(tgt_net
))
2793 return PTR_ERR(tgt_net
);
2797 ifm
= nlmsg_data(nlh
);
2798 if (ifm
->ifi_index
> 0)
2799 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2800 else if (tb
[IFLA_IFNAME
])
2801 dev
= __dev_get_by_name(tgt_net
, ifname
);
2802 else if (tb
[IFLA_GROUP
])
2803 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2808 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2814 err
= rtnl_delete_link(dev
);
2823 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2825 unsigned int old_flags
;
2828 old_flags
= dev
->flags
;
2829 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2830 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2835 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
2836 __dev_notify_flags(dev
, old_flags
, (old_flags
^ dev
->flags
));
2838 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2839 __dev_notify_flags(dev
, old_flags
, ~0U);
2843 EXPORT_SYMBOL(rtnl_configure_link
);
2845 struct net_device
*rtnl_create_link(struct net
*net
,
2846 const char *ifname
, unsigned char name_assign_type
,
2847 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2849 struct net_device
*dev
;
2850 unsigned int num_tx_queues
= 1;
2851 unsigned int num_rx_queues
= 1;
2853 if (tb
[IFLA_NUM_TX_QUEUES
])
2854 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2855 else if (ops
->get_num_tx_queues
)
2856 num_tx_queues
= ops
->get_num_tx_queues();
2858 if (tb
[IFLA_NUM_RX_QUEUES
])
2859 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2860 else if (ops
->get_num_rx_queues
)
2861 num_rx_queues
= ops
->get_num_rx_queues();
2863 if (num_tx_queues
< 1 || num_tx_queues
> 4096)
2864 return ERR_PTR(-EINVAL
);
2866 if (num_rx_queues
< 1 || num_rx_queues
> 4096)
2867 return ERR_PTR(-EINVAL
);
2869 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2870 ops
->setup
, num_tx_queues
, num_rx_queues
);
2872 return ERR_PTR(-ENOMEM
);
2874 dev_net_set(dev
, net
);
2875 dev
->rtnl_link_ops
= ops
;
2876 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2879 u32 mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2882 err
= dev_validate_mtu(dev
, mtu
, NULL
);
2885 return ERR_PTR(err
);
2889 if (tb
[IFLA_ADDRESS
]) {
2890 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2891 nla_len(tb
[IFLA_ADDRESS
]));
2892 dev
->addr_assign_type
= NET_ADDR_SET
;
2894 if (tb
[IFLA_BROADCAST
])
2895 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2896 nla_len(tb
[IFLA_BROADCAST
]));
2897 if (tb
[IFLA_TXQLEN
])
2898 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2899 if (tb
[IFLA_OPERSTATE
])
2900 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2901 if (tb
[IFLA_LINKMODE
])
2902 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2904 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2905 if (tb
[IFLA_GSO_MAX_SIZE
])
2906 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
2907 if (tb
[IFLA_GSO_MAX_SEGS
])
2908 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2912 EXPORT_SYMBOL(rtnl_create_link
);
2914 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2915 struct net
*net
, int group
,
2916 struct ifinfomsg
*ifm
,
2917 struct netlink_ext_ack
*extack
,
2920 struct net_device
*dev
, *aux
;
2923 for_each_netdev_safe(net
, dev
, aux
) {
2924 if (dev
->group
== group
) {
2925 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2934 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2935 struct netlink_ext_ack
*extack
)
2937 struct net
*net
= sock_net(skb
->sk
);
2938 const struct rtnl_link_ops
*ops
;
2939 const struct rtnl_link_ops
*m_ops
= NULL
;
2940 struct net_device
*dev
;
2941 struct net_device
*master_dev
= NULL
;
2942 struct ifinfomsg
*ifm
;
2943 char kind
[MODULE_NAME_LEN
];
2944 char ifname
[IFNAMSIZ
];
2945 struct nlattr
*tb
[IFLA_MAX
+1];
2946 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2947 unsigned char name_assign_type
= NET_NAME_USER
;
2950 #ifdef CONFIG_MODULES
2953 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2957 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2961 if (tb
[IFLA_IFNAME
])
2962 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2966 ifm
= nlmsg_data(nlh
);
2967 if (ifm
->ifi_index
> 0)
2968 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2971 dev
= __dev_get_by_name(net
, ifname
);
2977 master_dev
= netdev_master_upper_dev_get(dev
);
2979 m_ops
= master_dev
->rtnl_link_ops
;
2982 err
= validate_linkmsg(dev
, tb
);
2986 if (tb
[IFLA_LINKINFO
]) {
2987 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2988 tb
[IFLA_LINKINFO
], ifla_info_policy
,
2993 memset(linkinfo
, 0, sizeof(linkinfo
));
2995 if (linkinfo
[IFLA_INFO_KIND
]) {
2996 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2997 ops
= rtnl_link_ops_get(kind
);
3004 struct nlattr
*attr
[RTNL_MAX_TYPE
+ 1];
3005 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
3006 struct nlattr
**data
= NULL
;
3007 struct nlattr
**slave_data
= NULL
;
3008 struct net
*dest_net
, *link_net
= NULL
;
3011 if (ops
->maxtype
> RTNL_MAX_TYPE
)
3014 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
3015 err
= nla_parse_nested(attr
, ops
->maxtype
,
3016 linkinfo
[IFLA_INFO_DATA
],
3022 if (ops
->validate
) {
3023 err
= ops
->validate(tb
, data
, extack
);
3030 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
3033 if (m_ops
->slave_maxtype
&&
3034 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3035 err
= nla_parse_nested(slave_attr
,
3036 m_ops
->slave_maxtype
,
3037 linkinfo
[IFLA_INFO_SLAVE_DATA
],
3038 m_ops
->slave_policy
,
3042 slave_data
= slave_attr
;
3049 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3051 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3054 if (linkinfo
[IFLA_INFO_DATA
]) {
3055 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
3059 err
= ops
->changelink(dev
, tb
, data
, extack
);
3062 status
|= DO_SETLINK_NOTIFY
;
3065 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
3066 if (!m_ops
|| !m_ops
->slave_changelink
)
3069 err
= m_ops
->slave_changelink(master_dev
, dev
,
3074 status
|= DO_SETLINK_NOTIFY
;
3077 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
3081 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3082 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
3083 return rtnl_group_changelink(skb
, net
,
3084 nla_get_u32(tb
[IFLA_GROUP
]),
3089 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3093 #ifdef CONFIG_MODULES
3096 request_module("rtnl-link-%s", kind
);
3098 ops
= rtnl_link_ops_get(kind
);
3110 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3111 name_assign_type
= NET_NAME_ENUM
;
3114 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3115 if (IS_ERR(dest_net
))
3116 return PTR_ERR(dest_net
);
3118 if (tb
[IFLA_LINK_NETNSID
]) {
3119 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3121 link_net
= get_net_ns_by_id(dest_net
, id
);
3127 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3131 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3132 name_assign_type
, ops
, tb
);
3138 dev
->ifindex
= ifm
->ifi_index
;
3141 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
3143 /* Drivers should call free_netdev() in ->destructor
3144 * and unregister it on failure after registration
3145 * so that device could be finally freed in rtnl_unlock.
3148 /* If device is not registered at all, free it now */
3149 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3154 err
= register_netdevice(dev
);
3160 err
= rtnl_configure_link(dev
, ifm
);
3162 goto out_unregister
;
3164 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3166 goto out_unregister
;
3168 if (tb
[IFLA_MASTER
]) {
3169 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]),
3172 goto out_unregister
;
3181 LIST_HEAD(list_kill
);
3183 ops
->dellink(dev
, &list_kill
);
3184 unregister_netdevice_many(&list_kill
);
3186 unregister_netdevice(dev
);
3192 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3193 struct netlink_ext_ack
*extack
)
3195 struct net
*net
= sock_net(skb
->sk
);
3196 struct net
*tgt_net
= net
;
3197 struct ifinfomsg
*ifm
;
3198 char ifname
[IFNAMSIZ
];
3199 struct nlattr
*tb
[IFLA_MAX
+1];
3200 struct net_device
*dev
= NULL
;
3201 struct sk_buff
*nskb
;
3204 u32 ext_filter_mask
= 0;
3206 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3210 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3214 if (tb
[IFLA_IF_NETNSID
]) {
3215 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
3216 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
3217 if (IS_ERR(tgt_net
))
3218 return PTR_ERR(tgt_net
);
3221 if (tb
[IFLA_IFNAME
])
3222 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3224 if (tb
[IFLA_EXT_MASK
])
3225 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3228 ifm
= nlmsg_data(nlh
);
3229 if (ifm
->ifi_index
> 0)
3230 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3231 else if (tb
[IFLA_IFNAME
])
3232 dev
= __dev_get_by_name(tgt_net
, ifname
);
3241 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3245 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3246 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3247 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3248 0, NULL
, 0, netnsid
, GFP_KERNEL
);
3250 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3251 WARN_ON(err
== -EMSGSIZE
);
3254 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3262 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3264 struct net
*net
= sock_net(skb
->sk
);
3265 struct net_device
*dev
;
3266 struct nlattr
*tb
[IFLA_MAX
+1];
3267 u32 ext_filter_mask
= 0;
3268 u16 min_ifinfo_dump_size
= 0;
3271 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3272 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3273 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3275 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3276 if (tb
[IFLA_EXT_MASK
])
3277 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3280 if (!ext_filter_mask
)
3281 return NLMSG_GOODSIZE
;
3283 * traverse the list of net devices and compute the minimum
3284 * buffer size based upon the filter mask.
3287 for_each_netdev_rcu(net
, dev
) {
3288 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3294 return nlmsg_total_size(min_ifinfo_dump_size
);
3297 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3300 int s_idx
= cb
->family
;
3301 int type
= cb
->nlh
->nlmsg_type
- RTM_BASE
;
3306 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3307 struct rtnl_link
**tab
;
3308 struct rtnl_link
*link
;
3309 rtnl_dumpit_func dumpit
;
3311 if (idx
< s_idx
|| idx
== PF_PACKET
)
3314 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3317 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3325 dumpit
= link
->dumpit
;
3330 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3334 if (dumpit(skb
, cb
))
3342 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3343 unsigned int change
,
3344 u32 event
, gfp_t flags
, int *new_nsid
,
3347 struct net
*net
= dev_net(dev
);
3348 struct sk_buff
*skb
;
3350 size_t if_info_size
;
3352 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3356 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3357 type
, 0, 0, change
, 0, 0, event
,
3358 new_nsid
, new_ifindex
, -1, flags
);
3360 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3361 WARN_ON(err
== -EMSGSIZE
);
3368 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3372 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3374 struct net
*net
= dev_net(dev
);
3376 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3379 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3380 unsigned int change
, u32 event
,
3381 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3383 struct sk_buff
*skb
;
3385 if (dev
->reg_state
!= NETREG_REGISTERED
)
3388 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3391 rtmsg_ifinfo_send(skb
, dev
, flags
);
3394 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3397 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3401 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3402 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3404 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3405 new_nsid
, new_ifindex
);
3408 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3409 struct net_device
*dev
,
3410 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3411 int type
, unsigned int flags
,
3412 int nlflags
, u16 ndm_state
)
3414 struct nlmsghdr
*nlh
;
3417 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3421 ndm
= nlmsg_data(nlh
);
3422 ndm
->ndm_family
= AF_BRIDGE
;
3425 ndm
->ndm_flags
= flags
;
3427 ndm
->ndm_ifindex
= dev
->ifindex
;
3428 ndm
->ndm_state
= ndm_state
;
3430 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3431 goto nla_put_failure
;
3433 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3434 goto nla_put_failure
;
3436 nlmsg_end(skb
, nlh
);
3440 nlmsg_cancel(skb
, nlh
);
3444 static inline size_t rtnl_fdb_nlmsg_size(void)
3446 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3447 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3448 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3452 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3455 struct net
*net
= dev_net(dev
);
3456 struct sk_buff
*skb
;
3459 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3463 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3464 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3470 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3473 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3477 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3479 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3480 struct nlattr
*tb
[],
3481 struct net_device
*dev
,
3482 const unsigned char *addr
, u16 vid
,
3487 /* If aging addresses are supported device will need to
3488 * implement its own handler for this.
3490 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3491 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3496 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3500 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3501 err
= dev_uc_add_excl(dev
, addr
);
3502 else if (is_multicast_ether_addr(addr
))
3503 err
= dev_mc_add_excl(dev
, addr
);
3505 /* Only return duplicate errors if NLM_F_EXCL is set */
3506 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3511 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3513 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3514 struct netlink_ext_ack
*extack
)
3519 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3520 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3524 vid
= nla_get_u16(vlan_attr
);
3526 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3527 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3535 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3536 struct netlink_ext_ack
*extack
)
3538 struct net
*net
= sock_net(skb
->sk
);
3540 struct nlattr
*tb
[NDA_MAX
+1];
3541 struct net_device
*dev
;
3546 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3550 ndm
= nlmsg_data(nlh
);
3551 if (ndm
->ndm_ifindex
== 0) {
3552 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3556 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3558 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3562 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3563 NL_SET_ERR_MSG(extack
, "invalid address");
3567 if (dev
->type
!= ARPHRD_ETHER
) {
3568 NL_SET_ERR_MSG(extack
, "FDB delete only supported for Ethernet devices");
3572 addr
= nla_data(tb
[NDA_LLADDR
]);
3574 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3580 /* Support fdb on master device the net/bridge default case */
3581 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3582 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3583 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3584 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3586 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3591 ndm
->ndm_flags
&= ~NTF_MASTER
;
3594 /* Embedded bridge, macvlan, and any other device support */
3595 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3596 if (dev
->netdev_ops
->ndo_fdb_add
)
3597 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3601 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3605 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3607 ndm
->ndm_flags
&= ~NTF_SELF
;
3615 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3617 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3618 struct nlattr
*tb
[],
3619 struct net_device
*dev
,
3620 const unsigned char *addr
, u16 vid
)
3624 /* If aging addresses are supported device will need to
3625 * implement its own handler for this.
3627 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3628 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3632 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3633 err
= dev_uc_del(dev
, addr
);
3634 else if (is_multicast_ether_addr(addr
))
3635 err
= dev_mc_del(dev
, addr
);
3639 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3641 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3642 struct netlink_ext_ack
*extack
)
3644 struct net
*net
= sock_net(skb
->sk
);
3646 struct nlattr
*tb
[NDA_MAX
+1];
3647 struct net_device
*dev
;
3652 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3655 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3659 ndm
= nlmsg_data(nlh
);
3660 if (ndm
->ndm_ifindex
== 0) {
3661 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3665 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3667 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3671 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3672 NL_SET_ERR_MSG(extack
, "invalid address");
3676 if (dev
->type
!= ARPHRD_ETHER
) {
3677 NL_SET_ERR_MSG(extack
, "FDB add only supported for Ethernet devices");
3681 addr
= nla_data(tb
[NDA_LLADDR
]);
3683 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3689 /* Support fdb on master device the net/bridge default case */
3690 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3691 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3692 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3693 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3695 if (ops
->ndo_fdb_del
)
3696 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3701 ndm
->ndm_flags
&= ~NTF_MASTER
;
3704 /* Embedded bridge, macvlan, and any other device support */
3705 if (ndm
->ndm_flags
& NTF_SELF
) {
3706 if (dev
->netdev_ops
->ndo_fdb_del
)
3707 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3710 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3713 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3715 ndm
->ndm_flags
&= ~NTF_SELF
;
3722 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3723 struct netlink_callback
*cb
,
3724 struct net_device
*dev
,
3726 struct netdev_hw_addr_list
*list
)
3728 struct netdev_hw_addr
*ha
;
3732 portid
= NETLINK_CB(cb
->skb
).portid
;
3733 seq
= cb
->nlh
->nlmsg_seq
;
3735 list_for_each_entry(ha
, &list
->list
, list
) {
3736 if (*idx
< cb
->args
[2])
3739 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3741 RTM_NEWNEIGH
, NTF_SELF
,
3742 NLM_F_MULTI
, NUD_PERMANENT
);
3752 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3753 * @nlh: netlink message header
3756 * Default netdevice operation to dump the existing unicast address list.
3757 * Returns number of addresses from list put in skb.
3759 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3760 struct netlink_callback
*cb
,
3761 struct net_device
*dev
,
3762 struct net_device
*filter_dev
,
3767 if (dev
->type
!= ARPHRD_ETHER
)
3770 netif_addr_lock_bh(dev
);
3771 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3774 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3776 netif_addr_unlock_bh(dev
);
3779 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3781 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3783 struct net_device
*dev
;
3784 struct nlattr
*tb
[IFLA_MAX
+1];
3785 struct net_device
*br_dev
= NULL
;
3786 const struct net_device_ops
*ops
= NULL
;
3787 const struct net_device_ops
*cops
= NULL
;
3788 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3789 struct net
*net
= sock_net(skb
->sk
);
3790 struct hlist_head
*head
;
3798 /* A hack to preserve kernel<->userspace interface.
3799 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3800 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3801 * So, check for ndmsg with an optional u32 attribute (not used here).
3802 * Fortunately these sizes don't conflict with the size of ifinfomsg
3803 * with an optional attribute.
3805 if (nlmsg_len(cb
->nlh
) != sizeof(struct ndmsg
) &&
3806 (nlmsg_len(cb
->nlh
) != sizeof(struct ndmsg
) +
3807 nla_attr_size(sizeof(u32
)))) {
3808 err
= nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
,
3809 IFLA_MAX
, ifla_policy
, NULL
);
3812 } else if (err
== 0) {
3813 if (tb
[IFLA_MASTER
])
3814 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3817 brport_idx
= ifm
->ifi_index
;
3821 br_dev
= __dev_get_by_index(net
, br_idx
);
3825 ops
= br_dev
->netdev_ops
;
3829 s_idx
= cb
->args
[1];
3831 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3833 head
= &net
->dev_index_head
[h
];
3834 hlist_for_each_entry(dev
, head
, index_hlist
) {
3836 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3839 if (!br_idx
) { /* user did not specify a specific bridge */
3840 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3841 br_dev
= netdev_master_upper_dev_get(dev
);
3842 cops
= br_dev
->netdev_ops
;
3845 if (dev
!= br_dev
&&
3846 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3849 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3850 !(dev
->priv_flags
& IFF_EBRIDGE
))
3858 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3859 if (cops
&& cops
->ndo_fdb_dump
) {
3860 err
= cops
->ndo_fdb_dump(skb
, cb
,
3863 if (err
== -EMSGSIZE
)
3868 if (dev
->netdev_ops
->ndo_fdb_dump
)
3869 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3873 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3875 if (err
== -EMSGSIZE
)
3880 /* reset fdb offset to 0 for rest of the interfaces */
3896 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3897 unsigned int attrnum
, unsigned int flag
)
3900 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3904 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3905 struct net_device
*dev
, u16 mode
,
3906 u32 flags
, u32 mask
, int nlflags
,
3908 int (*vlan_fill
)(struct sk_buff
*skb
,
3909 struct net_device
*dev
,
3912 struct nlmsghdr
*nlh
;
3913 struct ifinfomsg
*ifm
;
3914 struct nlattr
*br_afspec
;
3915 struct nlattr
*protinfo
;
3916 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3917 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3920 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3924 ifm
= nlmsg_data(nlh
);
3925 ifm
->ifi_family
= AF_BRIDGE
;
3927 ifm
->ifi_type
= dev
->type
;
3928 ifm
->ifi_index
= dev
->ifindex
;
3929 ifm
->ifi_flags
= dev_get_flags(dev
);
3930 ifm
->ifi_change
= 0;
3933 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3934 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3935 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3937 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3939 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3940 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3941 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3942 goto nla_put_failure
;
3944 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3946 goto nla_put_failure
;
3948 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3949 nla_nest_cancel(skb
, br_afspec
);
3950 goto nla_put_failure
;
3953 if (mode
!= BRIDGE_MODE_UNDEF
) {
3954 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3955 nla_nest_cancel(skb
, br_afspec
);
3956 goto nla_put_failure
;
3960 err
= vlan_fill(skb
, dev
, filter_mask
);
3962 nla_nest_cancel(skb
, br_afspec
);
3963 goto nla_put_failure
;
3966 nla_nest_end(skb
, br_afspec
);
3968 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3970 goto nla_put_failure
;
3972 if (brport_nla_put_flag(skb
, flags
, mask
,
3973 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3974 brport_nla_put_flag(skb
, flags
, mask
,
3975 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3976 brport_nla_put_flag(skb
, flags
, mask
,
3977 IFLA_BRPORT_FAST_LEAVE
,
3978 BR_MULTICAST_FAST_LEAVE
) ||
3979 brport_nla_put_flag(skb
, flags
, mask
,
3980 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3981 brport_nla_put_flag(skb
, flags
, mask
,
3982 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3983 brport_nla_put_flag(skb
, flags
, mask
,
3984 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3985 brport_nla_put_flag(skb
, flags
, mask
,
3986 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3987 brport_nla_put_flag(skb
, flags
, mask
,
3988 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3989 nla_nest_cancel(skb
, protinfo
);
3990 goto nla_put_failure
;
3993 nla_nest_end(skb
, protinfo
);
3995 nlmsg_end(skb
, nlh
);
3998 nlmsg_cancel(skb
, nlh
);
3999 return err
? err
: -EMSGSIZE
;
4001 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
4003 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4005 struct net
*net
= sock_net(skb
->sk
);
4006 struct net_device
*dev
;
4008 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
4009 u32 seq
= cb
->nlh
->nlmsg_seq
;
4010 u32 filter_mask
= 0;
4013 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
4014 struct nlattr
*extfilt
;
4016 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
4019 if (nla_len(extfilt
) < sizeof(filter_mask
))
4022 filter_mask
= nla_get_u32(extfilt
);
4027 for_each_netdev_rcu(net
, dev
) {
4028 const struct net_device_ops
*ops
= dev
->netdev_ops
;
4029 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4031 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
4032 if (idx
>= cb
->args
[0]) {
4033 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
4034 skb
, portid
, seq
, dev
,
4035 filter_mask
, NLM_F_MULTI
);
4036 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4037 if (likely(skb
->len
))
4046 if (ops
->ndo_bridge_getlink
) {
4047 if (idx
>= cb
->args
[0]) {
4048 err
= ops
->ndo_bridge_getlink(skb
, portid
,
4052 if (err
< 0 && err
!= -EOPNOTSUPP
) {
4053 if (likely(skb
->len
))
4070 static inline size_t bridge_nlmsg_size(void)
4072 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4073 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4074 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4075 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
4076 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
4077 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
4078 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
4079 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
4080 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
4081 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
4082 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
4085 static int rtnl_bridge_notify(struct net_device
*dev
)
4087 struct net
*net
= dev_net(dev
);
4088 struct sk_buff
*skb
;
4089 int err
= -EOPNOTSUPP
;
4091 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
4094 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
4100 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
4107 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
4110 WARN_ON(err
== -EMSGSIZE
);
4113 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4117 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4118 struct netlink_ext_ack
*extack
)
4120 struct net
*net
= sock_net(skb
->sk
);
4121 struct ifinfomsg
*ifm
;
4122 struct net_device
*dev
;
4123 struct nlattr
*br_spec
, *attr
= NULL
;
4124 int rem
, err
= -EOPNOTSUPP
;
4126 bool have_flags
= false;
4128 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4131 ifm
= nlmsg_data(nlh
);
4132 if (ifm
->ifi_family
!= AF_BRIDGE
)
4133 return -EPFNOSUPPORT
;
4135 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4137 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4141 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4143 nla_for_each_nested(attr
, br_spec
, rem
) {
4144 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4145 if (nla_len(attr
) < sizeof(flags
))
4149 flags
= nla_get_u16(attr
);
4155 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4156 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4158 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4163 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
4167 flags
&= ~BRIDGE_FLAGS_MASTER
;
4170 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4171 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4174 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4177 flags
&= ~BRIDGE_FLAGS_SELF
;
4179 /* Generate event to notify upper layer of bridge
4182 err
= rtnl_bridge_notify(dev
);
4187 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4192 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4193 struct netlink_ext_ack
*extack
)
4195 struct net
*net
= sock_net(skb
->sk
);
4196 struct ifinfomsg
*ifm
;
4197 struct net_device
*dev
;
4198 struct nlattr
*br_spec
, *attr
= NULL
;
4199 int rem
, err
= -EOPNOTSUPP
;
4201 bool have_flags
= false;
4203 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4206 ifm
= nlmsg_data(nlh
);
4207 if (ifm
->ifi_family
!= AF_BRIDGE
)
4208 return -EPFNOSUPPORT
;
4210 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4212 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4216 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4218 nla_for_each_nested(attr
, br_spec
, rem
) {
4219 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4220 if (nla_len(attr
) < sizeof(flags
))
4224 flags
= nla_get_u16(attr
);
4230 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4231 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4233 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4238 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4242 flags
&= ~BRIDGE_FLAGS_MASTER
;
4245 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4246 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4249 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4253 flags
&= ~BRIDGE_FLAGS_SELF
;
4255 /* Generate event to notify upper layer of bridge
4258 err
= rtnl_bridge_notify(dev
);
4263 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4268 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4270 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4271 (!idxattr
|| idxattr
== attrid
);
4274 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4275 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4278 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4279 return sizeof(struct rtnl_link_stats64
);
4285 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4288 struct nlattr
*attr
= NULL
;
4293 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4294 dev
->netdev_ops
->ndo_get_offload_stats
))
4297 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4298 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4299 if (attr_id
< *prividx
)
4302 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4306 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4309 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4310 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4312 goto nla_put_failure
;
4314 attr_data
= nla_data(attr
);
4315 memset(attr_data
, 0, size
);
4316 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4319 goto get_offload_stats_failure
;
4330 get_offload_stats_failure
:
4335 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4341 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4342 dev
->netdev_ops
->ndo_get_offload_stats
))
4345 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4346 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4347 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4349 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4350 nla_size
+= nla_total_size_64bit(size
);
4354 nla_size
+= nla_total_size(0);
4359 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4360 int type
, u32 pid
, u32 seq
, u32 change
,
4361 unsigned int flags
, unsigned int filter_mask
,
4362 int *idxattr
, int *prividx
)
4364 struct if_stats_msg
*ifsm
;
4365 struct nlmsghdr
*nlh
;
4366 struct nlattr
*attr
;
4367 int s_prividx
= *prividx
;
4372 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4376 ifsm
= nlmsg_data(nlh
);
4377 ifsm
->family
= PF_UNSPEC
;
4380 ifsm
->ifindex
= dev
->ifindex
;
4381 ifsm
->filter_mask
= filter_mask
;
4383 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4384 struct rtnl_link_stats64
*sp
;
4386 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4387 sizeof(struct rtnl_link_stats64
),
4390 goto nla_put_failure
;
4392 sp
= nla_data(attr
);
4393 dev_get_stats(dev
, sp
);
4396 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4397 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4399 if (ops
&& ops
->fill_linkxstats
) {
4400 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4401 attr
= nla_nest_start(skb
,
4402 IFLA_STATS_LINK_XSTATS
);
4404 goto nla_put_failure
;
4406 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4407 nla_nest_end(skb
, attr
);
4409 goto nla_put_failure
;
4414 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4416 const struct rtnl_link_ops
*ops
= NULL
;
4417 const struct net_device
*master
;
4419 master
= netdev_master_upper_dev_get(dev
);
4421 ops
= master
->rtnl_link_ops
;
4422 if (ops
&& ops
->fill_linkxstats
) {
4423 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4424 attr
= nla_nest_start(skb
,
4425 IFLA_STATS_LINK_XSTATS_SLAVE
);
4427 goto nla_put_failure
;
4429 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4430 nla_nest_end(skb
, attr
);
4432 goto nla_put_failure
;
4437 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4439 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4440 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4442 goto nla_put_failure
;
4444 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4445 if (err
== -ENODATA
)
4446 nla_nest_cancel(skb
, attr
);
4448 nla_nest_end(skb
, attr
);
4450 if (err
&& err
!= -ENODATA
)
4451 goto nla_put_failure
;
4455 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4456 struct rtnl_af_ops
*af_ops
;
4458 *idxattr
= IFLA_STATS_AF_SPEC
;
4459 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
4461 goto nla_put_failure
;
4464 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4465 if (af_ops
->fill_stats_af
) {
4469 af
= nla_nest_start(skb
, af_ops
->family
);
4472 goto nla_put_failure
;
4474 err
= af_ops
->fill_stats_af(skb
, dev
);
4476 if (err
== -ENODATA
) {
4477 nla_nest_cancel(skb
, af
);
4478 } else if (err
< 0) {
4480 goto nla_put_failure
;
4483 nla_nest_end(skb
, af
);
4488 nla_nest_end(skb
, attr
);
4493 nlmsg_end(skb
, nlh
);
4498 /* not a multi message or no progress mean a real error */
4499 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4500 nlmsg_cancel(skb
, nlh
);
4502 nlmsg_end(skb
, nlh
);
4507 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4512 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4513 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4515 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4516 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4517 int attr
= IFLA_STATS_LINK_XSTATS
;
4519 if (ops
&& ops
->get_linkxstats_size
) {
4520 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4522 /* for IFLA_STATS_LINK_XSTATS */
4523 size
+= nla_total_size(0);
4527 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4528 struct net_device
*_dev
= (struct net_device
*)dev
;
4529 const struct rtnl_link_ops
*ops
= NULL
;
4530 const struct net_device
*master
;
4532 /* netdev_master_upper_dev_get can't take const */
4533 master
= netdev_master_upper_dev_get(_dev
);
4535 ops
= master
->rtnl_link_ops
;
4536 if (ops
&& ops
->get_linkxstats_size
) {
4537 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4539 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4541 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4542 size
+= nla_total_size(0);
4546 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4547 size
+= rtnl_get_offload_stats_size(dev
);
4549 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4550 struct rtnl_af_ops
*af_ops
;
4552 /* for IFLA_STATS_AF_SPEC */
4553 size
+= nla_total_size(0);
4556 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4557 if (af_ops
->get_stats_af_size
) {
4558 size
+= nla_total_size(
4559 af_ops
->get_stats_af_size(dev
));
4562 size
+= nla_total_size(0);
4571 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4572 struct netlink_ext_ack
*extack
)
4574 struct net
*net
= sock_net(skb
->sk
);
4575 struct net_device
*dev
= NULL
;
4576 int idxattr
= 0, prividx
= 0;
4577 struct if_stats_msg
*ifsm
;
4578 struct sk_buff
*nskb
;
4582 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4585 ifsm
= nlmsg_data(nlh
);
4586 if (ifsm
->ifindex
> 0)
4587 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4594 filter_mask
= ifsm
->filter_mask
;
4598 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4602 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4603 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4604 0, filter_mask
, &idxattr
, &prividx
);
4606 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4607 WARN_ON(err
== -EMSGSIZE
);
4610 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4616 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4618 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4619 struct net
*net
= sock_net(skb
->sk
);
4620 unsigned int flags
= NLM_F_MULTI
;
4621 struct if_stats_msg
*ifsm
;
4622 struct hlist_head
*head
;
4623 struct net_device
*dev
;
4624 u32 filter_mask
= 0;
4628 s_idx
= cb
->args
[1];
4629 s_idxattr
= cb
->args
[2];
4630 s_prividx
= cb
->args
[3];
4632 cb
->seq
= net
->dev_base_seq
;
4634 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
))
4637 ifsm
= nlmsg_data(cb
->nlh
);
4638 filter_mask
= ifsm
->filter_mask
;
4642 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4644 head
= &net
->dev_index_head
[h
];
4645 hlist_for_each_entry(dev
, head
, index_hlist
) {
4648 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4649 NETLINK_CB(cb
->skb
).portid
,
4650 cb
->nlh
->nlmsg_seq
, 0,
4652 &s_idxattr
, &s_prividx
);
4653 /* If we ran out of room on the first message,
4656 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4662 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4668 cb
->args
[3] = s_prividx
;
4669 cb
->args
[2] = s_idxattr
;
4676 /* Process one rtnetlink message. */
4678 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4679 struct netlink_ext_ack
*extack
)
4681 struct net
*net
= sock_net(skb
->sk
);
4682 struct rtnl_link
*link
;
4683 struct module
*owner
;
4684 int err
= -EOPNOTSUPP
;
4685 rtnl_doit_func doit
;
4691 type
= nlh
->nlmsg_type
;
4697 /* All the messages must have at least 1 byte length */
4698 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4701 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4704 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4708 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4710 rtnl_dumpit_func dumpit
;
4711 u16 min_dump_alloc
= 0;
4713 link
= rtnl_get_link(family
, type
);
4714 if (!link
|| !link
->dumpit
) {
4716 link
= rtnl_get_link(family
, type
);
4717 if (!link
|| !link
->dumpit
)
4720 owner
= link
->owner
;
4721 dumpit
= link
->dumpit
;
4723 if (type
== RTM_GETLINK
- RTM_BASE
)
4724 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4727 /* need to do this before rcu_read_unlock() */
4728 if (!try_module_get(owner
))
4729 err
= -EPROTONOSUPPORT
;
4735 struct netlink_dump_control c
= {
4737 .min_dump_alloc
= min_dump_alloc
,
4740 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4741 /* netlink_dump_start() will keep a reference on
4742 * module if dump is still in progress.
4749 link
= rtnl_get_link(family
, type
);
4750 if (!link
|| !link
->doit
) {
4752 link
= rtnl_get_link(PF_UNSPEC
, type
);
4753 if (!link
|| !link
->doit
)
4757 owner
= link
->owner
;
4758 if (!try_module_get(owner
)) {
4759 err
= -EPROTONOSUPPORT
;
4763 flags
= link
->flags
;
4764 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4768 err
= doit(skb
, nlh
, extack
);
4775 link
= rtnl_get_link(family
, type
);
4776 if (link
&& link
->doit
)
4777 err
= link
->doit(skb
, nlh
, extack
);
4793 static void rtnetlink_rcv(struct sk_buff
*skb
)
4795 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4798 static int rtnetlink_bind(struct net
*net
, int group
)
4801 case RTNLGRP_IPV4_MROUTE_R
:
4802 case RTNLGRP_IPV6_MROUTE_R
:
4803 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4810 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4812 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4816 case NETDEV_CHANGEMTU
:
4817 case NETDEV_CHANGEADDR
:
4818 case NETDEV_CHANGENAME
:
4819 case NETDEV_FEAT_CHANGE
:
4820 case NETDEV_BONDING_FAILOVER
:
4821 case NETDEV_POST_TYPE_CHANGE
:
4822 case NETDEV_NOTIFY_PEERS
:
4823 case NETDEV_CHANGEUPPER
:
4824 case NETDEV_RESEND_IGMP
:
4825 case NETDEV_CHANGEINFODATA
:
4826 case NETDEV_CHANGELOWERSTATE
:
4827 case NETDEV_CHANGE_TX_QUEUE_LEN
:
4828 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
4829 GFP_KERNEL
, NULL
, 0);
4837 static struct notifier_block rtnetlink_dev_notifier
= {
4838 .notifier_call
= rtnetlink_event
,
4842 static int __net_init
rtnetlink_net_init(struct net
*net
)
4845 struct netlink_kernel_cfg cfg
= {
4846 .groups
= RTNLGRP_MAX
,
4847 .input
= rtnetlink_rcv
,
4848 .cb_mutex
= &rtnl_mutex
,
4849 .flags
= NL_CFG_F_NONROOT_RECV
,
4850 .bind
= rtnetlink_bind
,
4853 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
4860 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
4862 netlink_kernel_release(net
->rtnl
);
4866 static struct pernet_operations rtnetlink_net_ops
= {
4867 .init
= rtnetlink_net_init
,
4868 .exit
= rtnetlink_net_exit
,
4871 void __init
rtnetlink_init(void)
4873 if (register_pernet_subsys(&rtnetlink_net_ops
))
4874 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4876 register_netdevice_notifier(&rtnetlink_dev_notifier
);
4878 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
4879 rtnl_dump_ifinfo
, 0);
4880 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
4881 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
4882 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
4884 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
4885 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
4886 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
4888 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
4889 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
4890 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
4892 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
4893 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
4894 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
4896 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,