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
];
327 RCU_INIT_POINTER(rtnl_msg_handlers
[protocol
], NULL
);
328 for (msgindex
= 0; msgindex
< RTM_NR_MSGTYPES
; msgindex
++) {
329 link
= tab
[msgindex
];
333 rcu_assign_pointer(tab
[msgindex
], NULL
);
334 kfree_rcu(link
, rcu
);
342 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
344 static LIST_HEAD(link_ops
);
346 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
348 const struct rtnl_link_ops
*ops
;
350 list_for_each_entry(ops
, &link_ops
, list
) {
351 if (!strcmp(ops
->kind
, kind
))
358 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
359 * @ops: struct rtnl_link_ops * to register
361 * The caller must hold the rtnl_mutex. This function should be used
362 * by drivers that create devices during module initialization. It
363 * must be called before registering the devices.
365 * Returns 0 on success or a negative error code.
367 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
369 if (rtnl_link_ops_get(ops
->kind
))
372 /* The check for setup is here because if ops
373 * does not have that filled up, it is not possible
374 * to use the ops for creating device. So do not
375 * fill up dellink as well. That disables rtnl_dellink.
377 if (ops
->setup
&& !ops
->dellink
)
378 ops
->dellink
= unregister_netdevice_queue
;
380 list_add_tail(&ops
->list
, &link_ops
);
383 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
386 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
387 * @ops: struct rtnl_link_ops * to register
389 * Returns 0 on success or a negative error code.
391 int rtnl_link_register(struct rtnl_link_ops
*ops
)
395 /* Sanity-check max sizes to avoid stack buffer overflow. */
396 if (WARN_ON(ops
->maxtype
> RTNL_MAX_TYPE
||
397 ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
))
401 err
= __rtnl_link_register(ops
);
405 EXPORT_SYMBOL_GPL(rtnl_link_register
);
407 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
409 struct net_device
*dev
;
410 LIST_HEAD(list_kill
);
412 for_each_netdev(net
, dev
) {
413 if (dev
->rtnl_link_ops
== ops
)
414 ops
->dellink(dev
, &list_kill
);
416 unregister_netdevice_many(&list_kill
);
420 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
421 * @ops: struct rtnl_link_ops * to unregister
423 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
424 * integrity (hold pernet_ops_rwsem for writing to close the race
425 * with setup_net() and cleanup_net()).
427 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
432 __rtnl_kill_links(net
, ops
);
434 list_del(&ops
->list
);
436 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
438 /* Return with the rtnl_lock held when there are no network
439 * devices unregistering in any network namespace.
441 static void rtnl_lock_unregistering_all(void)
445 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
447 add_wait_queue(&netdev_unregistering_wq
, &wait
);
449 unregistering
= false;
451 /* We held write locked pernet_ops_rwsem, and parallel
452 * setup_net() and cleanup_net() are not possible.
455 if (net
->dev_unreg_count
> 0) {
456 unregistering
= true;
464 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
466 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
470 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
471 * @ops: struct rtnl_link_ops * to unregister
473 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
475 /* Close the race with setup_net() and cleanup_net() */
476 down_write(&pernet_ops_rwsem
);
477 rtnl_lock_unregistering_all();
478 __rtnl_link_unregister(ops
);
480 up_write(&pernet_ops_rwsem
);
482 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
484 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
486 struct net_device
*master_dev
;
487 const struct rtnl_link_ops
*ops
;
492 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
496 ops
= master_dev
->rtnl_link_ops
;
497 if (!ops
|| !ops
->get_slave_size
)
499 /* IFLA_INFO_SLAVE_DATA + nested data */
500 size
= nla_total_size(sizeof(struct nlattr
)) +
501 ops
->get_slave_size(master_dev
, dev
);
508 static size_t rtnl_link_get_size(const struct net_device
*dev
)
510 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
516 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
517 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
520 /* IFLA_INFO_DATA + nested data */
521 size
+= nla_total_size(sizeof(struct nlattr
)) +
524 if (ops
->get_xstats_size
)
525 /* IFLA_INFO_XSTATS */
526 size
+= nla_total_size(ops
->get_xstats_size(dev
));
528 size
+= rtnl_link_get_slave_info_data_size(dev
);
533 static LIST_HEAD(rtnl_af_ops
);
535 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
537 const struct rtnl_af_ops
*ops
;
539 list_for_each_entry_rcu(ops
, &rtnl_af_ops
, list
) {
540 if (ops
->family
== family
)
548 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
549 * @ops: struct rtnl_af_ops * to register
551 * Returns 0 on success or a negative error code.
553 void rtnl_af_register(struct rtnl_af_ops
*ops
)
556 list_add_tail_rcu(&ops
->list
, &rtnl_af_ops
);
559 EXPORT_SYMBOL_GPL(rtnl_af_register
);
562 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
563 * @ops: struct rtnl_af_ops * to unregister
565 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
568 list_del_rcu(&ops
->list
);
573 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
575 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
578 struct rtnl_af_ops
*af_ops
;
582 size
= nla_total_size(sizeof(struct nlattr
));
585 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
586 if (af_ops
->get_link_af_size
) {
587 /* AF_* + nested data */
588 size
+= nla_total_size(sizeof(struct nlattr
)) +
589 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
597 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
599 struct net_device
*master_dev
;
604 master_dev
= netdev_master_upper_dev_get_rcu((struct net_device
*)dev
);
605 if (master_dev
&& master_dev
->rtnl_link_ops
)
611 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
612 const struct net_device
*dev
)
614 struct net_device
*master_dev
;
615 const struct rtnl_link_ops
*ops
;
616 struct nlattr
*slave_data
;
619 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
622 ops
= master_dev
->rtnl_link_ops
;
625 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
627 if (ops
->fill_slave_info
) {
628 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
631 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
633 goto err_cancel_slave_data
;
634 nla_nest_end(skb
, slave_data
);
638 err_cancel_slave_data
:
639 nla_nest_cancel(skb
, slave_data
);
643 static int rtnl_link_info_fill(struct sk_buff
*skb
,
644 const struct net_device
*dev
)
646 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
652 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
654 if (ops
->fill_xstats
) {
655 err
= ops
->fill_xstats(skb
, dev
);
659 if (ops
->fill_info
) {
660 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
663 err
= ops
->fill_info(skb
, dev
);
665 goto err_cancel_data
;
666 nla_nest_end(skb
, data
);
671 nla_nest_cancel(skb
, data
);
675 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
677 struct nlattr
*linkinfo
;
680 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
681 if (linkinfo
== NULL
)
684 err
= rtnl_link_info_fill(skb
, dev
);
686 goto err_cancel_link
;
688 err
= rtnl_link_slave_info_fill(skb
, dev
);
690 goto err_cancel_link
;
692 nla_nest_end(skb
, linkinfo
);
696 nla_nest_cancel(skb
, linkinfo
);
701 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
703 struct sock
*rtnl
= net
->rtnl
;
706 NETLINK_CB(skb
).dst_group
= group
;
708 refcount_inc(&skb
->users
);
709 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
711 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
715 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
717 struct sock
*rtnl
= net
->rtnl
;
719 return nlmsg_unicast(rtnl
, skb
, pid
);
721 EXPORT_SYMBOL(rtnl_unicast
);
723 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
724 struct nlmsghdr
*nlh
, gfp_t flags
)
726 struct sock
*rtnl
= net
->rtnl
;
730 report
= nlmsg_report(nlh
);
732 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
734 EXPORT_SYMBOL(rtnl_notify
);
736 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
738 struct sock
*rtnl
= net
->rtnl
;
740 netlink_set_err(rtnl
, 0, group
, error
);
742 EXPORT_SYMBOL(rtnl_set_sk_err
);
744 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
749 mx
= nla_nest_start(skb
, RTA_METRICS
);
753 for (i
= 0; i
< RTAX_MAX
; i
++) {
755 if (i
== RTAX_CC_ALGO
- 1) {
756 char tmp
[TCP_CA_NAME_MAX
], *name
;
758 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
761 if (nla_put_string(skb
, i
+ 1, name
))
762 goto nla_put_failure
;
763 } else if (i
== RTAX_FEATURES
- 1) {
764 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
768 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
769 if (nla_put_u32(skb
, i
+ 1, user_features
))
770 goto nla_put_failure
;
772 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
773 goto nla_put_failure
;
780 nla_nest_cancel(skb
, mx
);
784 return nla_nest_end(skb
, mx
);
787 nla_nest_cancel(skb
, mx
);
790 EXPORT_SYMBOL(rtnetlink_put_metrics
);
792 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
793 long expires
, u32 error
)
795 struct rta_cacheinfo ci
= {
801 ci
.rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
);
802 ci
.rta_used
= dst
->__use
;
803 ci
.rta_clntref
= atomic_read(&dst
->__refcnt
);
808 clock
= jiffies_to_clock_t(abs(expires
));
809 clock
= min_t(unsigned long, clock
, INT_MAX
);
810 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
812 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
814 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
816 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
818 unsigned char operstate
= dev
->operstate
;
820 switch (transition
) {
822 if ((operstate
== IF_OPER_DORMANT
||
823 operstate
== IF_OPER_UNKNOWN
) &&
825 operstate
= IF_OPER_UP
;
828 case IF_OPER_DORMANT
:
829 if (operstate
== IF_OPER_UP
||
830 operstate
== IF_OPER_UNKNOWN
)
831 operstate
= IF_OPER_DORMANT
;
835 if (dev
->operstate
!= operstate
) {
836 write_lock_bh(&dev_base_lock
);
837 dev
->operstate
= operstate
;
838 write_unlock_bh(&dev_base_lock
);
839 netdev_state_change(dev
);
843 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
845 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
846 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
849 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
850 const struct ifinfomsg
*ifm
)
852 unsigned int flags
= ifm
->ifi_flags
;
854 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
856 flags
= (flags
& ifm
->ifi_change
) |
857 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
862 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
863 const struct rtnl_link_stats64
*b
)
865 a
->rx_packets
= b
->rx_packets
;
866 a
->tx_packets
= b
->tx_packets
;
867 a
->rx_bytes
= b
->rx_bytes
;
868 a
->tx_bytes
= b
->tx_bytes
;
869 a
->rx_errors
= b
->rx_errors
;
870 a
->tx_errors
= b
->tx_errors
;
871 a
->rx_dropped
= b
->rx_dropped
;
872 a
->tx_dropped
= b
->tx_dropped
;
874 a
->multicast
= b
->multicast
;
875 a
->collisions
= b
->collisions
;
877 a
->rx_length_errors
= b
->rx_length_errors
;
878 a
->rx_over_errors
= b
->rx_over_errors
;
879 a
->rx_crc_errors
= b
->rx_crc_errors
;
880 a
->rx_frame_errors
= b
->rx_frame_errors
;
881 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
882 a
->rx_missed_errors
= b
->rx_missed_errors
;
884 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
885 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
886 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
887 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
888 a
->tx_window_errors
= b
->tx_window_errors
;
890 a
->rx_compressed
= b
->rx_compressed
;
891 a
->tx_compressed
= b
->tx_compressed
;
893 a
->rx_nohandler
= b
->rx_nohandler
;
897 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
900 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
)) {
901 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
902 size_t size
= nla_total_size(0);
905 nla_total_size(sizeof(struct ifla_vf_mac
)) +
906 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
907 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
908 nla_total_size(MAX_VLAN_LIST_LEN
*
909 sizeof(struct ifla_vf_vlan_info
)) +
910 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
911 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
912 nla_total_size(sizeof(struct ifla_vf_rate
)) +
913 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
914 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
915 nla_total_size(0) + /* nest IFLA_VF_STATS */
916 /* IFLA_VF_STATS_RX_PACKETS */
917 nla_total_size_64bit(sizeof(__u64
)) +
918 /* IFLA_VF_STATS_TX_PACKETS */
919 nla_total_size_64bit(sizeof(__u64
)) +
920 /* IFLA_VF_STATS_RX_BYTES */
921 nla_total_size_64bit(sizeof(__u64
)) +
922 /* IFLA_VF_STATS_TX_BYTES */
923 nla_total_size_64bit(sizeof(__u64
)) +
924 /* IFLA_VF_STATS_BROADCAST */
925 nla_total_size_64bit(sizeof(__u64
)) +
926 /* IFLA_VF_STATS_MULTICAST */
927 nla_total_size_64bit(sizeof(__u64
)) +
928 /* IFLA_VF_STATS_RX_DROPPED */
929 nla_total_size_64bit(sizeof(__u64
)) +
930 /* IFLA_VF_STATS_TX_DROPPED */
931 nla_total_size_64bit(sizeof(__u64
)) +
932 nla_total_size(sizeof(struct ifla_vf_trust
)));
938 static size_t rtnl_port_size(const struct net_device
*dev
,
941 size_t port_size
= nla_total_size(4) /* PORT_VF */
942 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
943 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
944 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
945 + nla_total_size(1) /* PROT_VDP_REQUEST */
946 + nla_total_size(2); /* PORT_VDP_RESPONSE */
947 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
948 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
950 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
953 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
954 !(ext_filter_mask
& RTEXT_FILTER_VF
))
956 if (dev_num_vf(dev
->dev
.parent
))
957 return port_self_size
+ vf_ports_size
+
958 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
960 return port_self_size
;
963 static size_t rtnl_xdp_size(void)
965 size_t xdp_size
= nla_total_size(0) + /* nest IFLA_XDP */
966 nla_total_size(1) + /* XDP_ATTACHED */
967 nla_total_size(4); /* XDP_PROG_ID */
972 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
975 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
976 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
977 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
978 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
979 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
980 + nla_total_size(sizeof(struct rtnl_link_stats
))
981 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
982 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
983 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
984 + nla_total_size(4) /* IFLA_TXQLEN */
985 + nla_total_size(4) /* IFLA_WEIGHT */
986 + nla_total_size(4) /* IFLA_MTU */
987 + nla_total_size(4) /* IFLA_LINK */
988 + nla_total_size(4) /* IFLA_MASTER */
989 + nla_total_size(1) /* IFLA_CARRIER */
990 + nla_total_size(4) /* IFLA_PROMISCUITY */
991 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
992 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
993 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
994 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
995 + nla_total_size(1) /* IFLA_OPERSTATE */
996 + nla_total_size(1) /* IFLA_LINKMODE */
997 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
998 + nla_total_size(4) /* IFLA_LINK_NETNSID */
999 + nla_total_size(4) /* IFLA_GROUP */
1000 + nla_total_size(ext_filter_mask
1001 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
1002 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
1003 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1004 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
1005 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
1006 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
1007 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
1008 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
1009 + rtnl_xdp_size() /* IFLA_XDP */
1010 + nla_total_size(4) /* IFLA_EVENT */
1011 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1012 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1013 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1014 + nla_total_size(4) /* IFLA_IF_NETNSID */
1015 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1016 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1020 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1022 struct nlattr
*vf_ports
;
1023 struct nlattr
*vf_port
;
1027 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
1031 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
1032 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
1034 goto nla_put_failure
;
1035 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
1036 goto nla_put_failure
;
1037 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
1038 if (err
== -EMSGSIZE
)
1039 goto nla_put_failure
;
1041 nla_nest_cancel(skb
, vf_port
);
1044 nla_nest_end(skb
, vf_port
);
1047 nla_nest_end(skb
, vf_ports
);
1052 nla_nest_cancel(skb
, vf_ports
);
1056 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1058 struct nlattr
*port_self
;
1061 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
1065 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
1067 nla_nest_cancel(skb
, port_self
);
1068 return (err
== -EMSGSIZE
) ? err
: 0;
1071 nla_nest_end(skb
, port_self
);
1076 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1077 u32 ext_filter_mask
)
1081 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1082 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1085 err
= rtnl_port_self_fill(skb
, dev
);
1089 if (dev_num_vf(dev
->dev
.parent
)) {
1090 err
= rtnl_vf_ports_fill(skb
, dev
);
1098 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1101 struct netdev_phys_item_id ppid
;
1103 err
= dev_get_phys_port_id(dev
, &ppid
);
1105 if (err
== -EOPNOTSUPP
)
1110 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1116 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1118 char name
[IFNAMSIZ
];
1121 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1123 if (err
== -EOPNOTSUPP
)
1128 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1134 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1137 struct switchdev_attr attr
= {
1139 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1140 .flags
= SWITCHDEV_F_NO_RECURSE
,
1143 err
= switchdev_port_attr_get(dev
, &attr
);
1145 if (err
== -EOPNOTSUPP
)
1150 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1157 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1158 struct net_device
*dev
)
1160 struct rtnl_link_stats64
*sp
;
1161 struct nlattr
*attr
;
1163 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1164 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1168 sp
= nla_data(attr
);
1169 dev_get_stats(dev
, sp
);
1171 attr
= nla_reserve(skb
, IFLA_STATS
,
1172 sizeof(struct rtnl_link_stats
));
1176 copy_rtnl_link_stats(nla_data(attr
), sp
);
1181 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1182 struct net_device
*dev
,
1184 struct nlattr
*vfinfo
)
1186 struct ifla_vf_rss_query_en vf_rss_query_en
;
1187 struct nlattr
*vf
, *vfstats
, *vfvlanlist
;
1188 struct ifla_vf_link_state vf_linkstate
;
1189 struct ifla_vf_vlan_info vf_vlan_info
;
1190 struct ifla_vf_spoofchk vf_spoofchk
;
1191 struct ifla_vf_tx_rate vf_tx_rate
;
1192 struct ifla_vf_stats vf_stats
;
1193 struct ifla_vf_trust vf_trust
;
1194 struct ifla_vf_vlan vf_vlan
;
1195 struct ifla_vf_rate vf_rate
;
1196 struct ifla_vf_mac vf_mac
;
1197 struct ifla_vf_info ivi
;
1199 memset(&ivi
, 0, sizeof(ivi
));
1201 /* Not all SR-IOV capable drivers support the
1202 * spoofcheck and "RSS query enable" query. Preset to
1203 * -1 so the user space tool can detect that the driver
1204 * didn't report anything.
1207 ivi
.rss_query_en
= -1;
1209 /* The default value for VF link state is "auto"
1210 * IFLA_VF_LINK_STATE_AUTO which equals zero
1213 /* VLAN Protocol by default is 802.1Q */
1214 ivi
.vlan_proto
= htons(ETH_P_8021Q
);
1215 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1218 memset(&vf_vlan_info
, 0, sizeof(vf_vlan_info
));
1227 vf_rss_query_en
.vf
=
1228 vf_trust
.vf
= ivi
.vf
;
1230 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1231 vf_vlan
.vlan
= ivi
.vlan
;
1232 vf_vlan
.qos
= ivi
.qos
;
1233 vf_vlan_info
.vlan
= ivi
.vlan
;
1234 vf_vlan_info
.qos
= ivi
.qos
;
1235 vf_vlan_info
.vlan_proto
= ivi
.vlan_proto
;
1236 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1237 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1238 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1239 vf_spoofchk
.setting
= ivi
.spoofchk
;
1240 vf_linkstate
.link_state
= ivi
.linkstate
;
1241 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1242 vf_trust
.setting
= ivi
.trusted
;
1243 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1245 goto nla_put_vfinfo_failure
;
1246 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1247 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1248 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1250 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1252 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1254 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1256 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1257 sizeof(vf_rss_query_en
),
1258 &vf_rss_query_en
) ||
1259 nla_put(skb
, IFLA_VF_TRUST
,
1260 sizeof(vf_trust
), &vf_trust
))
1261 goto nla_put_vf_failure
;
1262 vfvlanlist
= nla_nest_start(skb
, IFLA_VF_VLAN_LIST
);
1264 goto nla_put_vf_failure
;
1265 if (nla_put(skb
, IFLA_VF_VLAN_INFO
, sizeof(vf_vlan_info
),
1267 nla_nest_cancel(skb
, vfvlanlist
);
1268 goto nla_put_vf_failure
;
1270 nla_nest_end(skb
, vfvlanlist
);
1271 memset(&vf_stats
, 0, sizeof(vf_stats
));
1272 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1273 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1275 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1277 goto nla_put_vf_failure
;
1278 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1279 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1280 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1281 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1282 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1283 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1284 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1285 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1286 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1287 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1288 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1289 vf_stats
.multicast
, IFLA_VF_STATS_PAD
) ||
1290 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_DROPPED
,
1291 vf_stats
.rx_dropped
, IFLA_VF_STATS_PAD
) ||
1292 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_DROPPED
,
1293 vf_stats
.tx_dropped
, IFLA_VF_STATS_PAD
)) {
1294 nla_nest_cancel(skb
, vfstats
);
1295 goto nla_put_vf_failure
;
1297 nla_nest_end(skb
, vfstats
);
1298 nla_nest_end(skb
, vf
);
1302 nla_nest_cancel(skb
, vf
);
1303 nla_put_vfinfo_failure
:
1304 nla_nest_cancel(skb
, vfinfo
);
1308 static noinline_for_stack
int rtnl_fill_vf(struct sk_buff
*skb
,
1309 struct net_device
*dev
,
1310 u32 ext_filter_mask
)
1312 struct nlattr
*vfinfo
;
1315 if (!dev
->dev
.parent
|| ((ext_filter_mask
& RTEXT_FILTER_VF
) == 0))
1318 num_vfs
= dev_num_vf(dev
->dev
.parent
);
1319 if (nla_put_u32(skb
, IFLA_NUM_VF
, num_vfs
))
1322 if (!dev
->netdev_ops
->ndo_get_vf_config
)
1325 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1329 for (i
= 0; i
< num_vfs
; i
++) {
1330 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1334 nla_nest_end(skb
, vfinfo
);
1338 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1340 struct rtnl_link_ifmap map
;
1342 memset(&map
, 0, sizeof(map
));
1343 map
.mem_start
= dev
->mem_start
;
1344 map
.mem_end
= dev
->mem_end
;
1345 map
.base_addr
= dev
->base_addr
;
1348 map
.port
= dev
->if_port
;
1350 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1356 static u8
rtnl_xdp_attached_mode(struct net_device
*dev
, u32
*prog_id
)
1358 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1359 const struct bpf_prog
*generic_xdp_prog
;
1360 struct netdev_bpf xdp
;
1365 generic_xdp_prog
= rtnl_dereference(dev
->xdp_prog
);
1366 if (generic_xdp_prog
) {
1367 *prog_id
= generic_xdp_prog
->aux
->id
;
1368 return XDP_ATTACHED_SKB
;
1371 return XDP_ATTACHED_NONE
;
1373 __dev_xdp_query(dev
, ops
->ndo_bpf
, &xdp
);
1374 *prog_id
= xdp
.prog_id
;
1376 return xdp
.prog_attached
;
1379 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1385 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1389 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
,
1390 rtnl_xdp_attached_mode(dev
, &prog_id
));
1395 err
= nla_put_u32(skb
, IFLA_XDP_PROG_ID
, prog_id
);
1400 nla_nest_end(skb
, xdp
);
1404 nla_nest_cancel(skb
, xdp
);
1408 static u32
rtnl_get_event(unsigned long event
)
1410 u32 rtnl_event_type
= IFLA_EVENT_NONE
;
1414 rtnl_event_type
= IFLA_EVENT_REBOOT
;
1416 case NETDEV_FEAT_CHANGE
:
1417 rtnl_event_type
= IFLA_EVENT_FEATURES
;
1419 case NETDEV_BONDING_FAILOVER
:
1420 rtnl_event_type
= IFLA_EVENT_BONDING_FAILOVER
;
1422 case NETDEV_NOTIFY_PEERS
:
1423 rtnl_event_type
= IFLA_EVENT_NOTIFY_PEERS
;
1425 case NETDEV_RESEND_IGMP
:
1426 rtnl_event_type
= IFLA_EVENT_IGMP_RESEND
;
1428 case NETDEV_CHANGEINFODATA
:
1429 rtnl_event_type
= IFLA_EVENT_BONDING_OPTIONS
;
1435 return rtnl_event_type
;
1438 static int put_master_ifindex(struct sk_buff
*skb
, struct net_device
*dev
)
1440 const struct net_device
*upper_dev
;
1445 upper_dev
= netdev_master_upper_dev_get_rcu(dev
);
1447 ret
= nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
);
1453 static int nla_put_iflink(struct sk_buff
*skb
, const struct net_device
*dev
)
1455 int ifindex
= dev_get_iflink(dev
);
1457 if (dev
->ifindex
== ifindex
)
1460 return nla_put_u32(skb
, IFLA_LINK
, ifindex
);
1463 static noinline_for_stack
int nla_put_ifalias(struct sk_buff
*skb
,
1464 struct net_device
*dev
)
1469 ret
= dev_get_alias(dev
, buf
, sizeof(buf
));
1470 return ret
> 0 ? nla_put_string(skb
, IFLA_IFALIAS
, buf
) : 0;
1473 static int rtnl_fill_link_netnsid(struct sk_buff
*skb
,
1474 const struct net_device
*dev
,
1475 struct net
*src_net
)
1477 if (dev
->rtnl_link_ops
&& dev
->rtnl_link_ops
->get_link_net
) {
1478 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1480 if (!net_eq(dev_net(dev
), link_net
)) {
1481 int id
= peernet2id_alloc(src_net
, link_net
);
1483 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1491 static int rtnl_fill_link_af(struct sk_buff
*skb
,
1492 const struct net_device
*dev
,
1493 u32 ext_filter_mask
)
1495 const struct rtnl_af_ops
*af_ops
;
1496 struct nlattr
*af_spec
;
1498 af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
1502 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
1506 if (!af_ops
->fill_link_af
)
1509 af
= nla_nest_start(skb
, af_ops
->family
);
1513 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1515 * Caller may return ENODATA to indicate that there
1516 * was no data to be dumped. This is not an error, it
1517 * means we should trim the attribute header and
1520 if (err
== -ENODATA
)
1521 nla_nest_cancel(skb
, af
);
1525 nla_nest_end(skb
, af
);
1528 nla_nest_end(skb
, af_spec
);
1532 static int rtnl_fill_ifinfo(struct sk_buff
*skb
,
1533 struct net_device
*dev
, struct net
*src_net
,
1534 int type
, u32 pid
, u32 seq
, u32 change
,
1535 unsigned int flags
, u32 ext_filter_mask
,
1536 u32 event
, int *new_nsid
, int new_ifindex
,
1539 struct ifinfomsg
*ifm
;
1540 struct nlmsghdr
*nlh
;
1543 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1547 ifm
= nlmsg_data(nlh
);
1548 ifm
->ifi_family
= AF_UNSPEC
;
1550 ifm
->ifi_type
= dev
->type
;
1551 ifm
->ifi_index
= dev
->ifindex
;
1552 ifm
->ifi_flags
= dev_get_flags(dev
);
1553 ifm
->ifi_change
= change
;
1555 if (tgt_netnsid
>= 0 && nla_put_s32(skb
, IFLA_IF_NETNSID
, tgt_netnsid
))
1556 goto nla_put_failure
;
1558 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1559 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1560 nla_put_u8(skb
, IFLA_OPERSTATE
,
1561 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1562 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1563 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1564 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1565 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1566 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1567 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1568 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1570 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1572 nla_put_iflink(skb
, dev
) ||
1573 put_master_ifindex(skb
, dev
) ||
1574 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1576 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1577 nla_put_ifalias(skb
, dev
) ||
1578 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1579 atomic_read(&dev
->carrier_up_count
) +
1580 atomic_read(&dev
->carrier_down_count
)) ||
1581 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
) ||
1582 nla_put_u32(skb
, IFLA_CARRIER_UP_COUNT
,
1583 atomic_read(&dev
->carrier_up_count
)) ||
1584 nla_put_u32(skb
, IFLA_CARRIER_DOWN_COUNT
,
1585 atomic_read(&dev
->carrier_down_count
)))
1586 goto nla_put_failure
;
1588 if (event
!= IFLA_EVENT_NONE
) {
1589 if (nla_put_u32(skb
, IFLA_EVENT
, event
))
1590 goto nla_put_failure
;
1593 if (rtnl_fill_link_ifmap(skb
, dev
))
1594 goto nla_put_failure
;
1596 if (dev
->addr_len
) {
1597 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1598 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1599 goto nla_put_failure
;
1602 if (rtnl_phys_port_id_fill(skb
, dev
))
1603 goto nla_put_failure
;
1605 if (rtnl_phys_port_name_fill(skb
, dev
))
1606 goto nla_put_failure
;
1608 if (rtnl_phys_switch_id_fill(skb
, dev
))
1609 goto nla_put_failure
;
1611 if (rtnl_fill_stats(skb
, dev
))
1612 goto nla_put_failure
;
1614 if (rtnl_fill_vf(skb
, dev
, ext_filter_mask
))
1615 goto nla_put_failure
;
1617 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1618 goto nla_put_failure
;
1620 if (rtnl_xdp_fill(skb
, dev
))
1621 goto nla_put_failure
;
1623 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1624 if (rtnl_link_fill(skb
, dev
) < 0)
1625 goto nla_put_failure
;
1628 if (rtnl_fill_link_netnsid(skb
, dev
, src_net
))
1629 goto nla_put_failure
;
1632 nla_put_s32(skb
, IFLA_NEW_NETNSID
, *new_nsid
) < 0)
1633 goto nla_put_failure
;
1635 nla_put_s32(skb
, IFLA_NEW_IFINDEX
, new_ifindex
) < 0)
1636 goto nla_put_failure
;
1640 if (rtnl_fill_link_af(skb
, dev
, ext_filter_mask
))
1641 goto nla_put_failure_rcu
;
1644 nlmsg_end(skb
, nlh
);
1647 nla_put_failure_rcu
:
1650 nlmsg_cancel(skb
, nlh
);
1654 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1655 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1656 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1657 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1658 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1659 [IFLA_MTU
] = { .type
= NLA_U32
},
1660 [IFLA_LINK
] = { .type
= NLA_U32
},
1661 [IFLA_MASTER
] = { .type
= NLA_U32
},
1662 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1663 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1664 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1665 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1666 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1667 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1668 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1669 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1670 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1671 * allow 0-length string (needed to remove an alias).
1673 [IFLA_IFALIAS
] = { .type
= NLA_BINARY
, .len
= IFALIASZ
- 1 },
1674 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1675 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1676 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1677 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1678 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1679 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1680 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1681 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1682 [IFLA_GSO_MAX_SEGS
] = { .type
= NLA_U32
},
1683 [IFLA_GSO_MAX_SIZE
] = { .type
= NLA_U32
},
1684 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1685 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1686 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1687 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1688 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1689 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1690 [IFLA_EVENT
] = { .type
= NLA_U32
},
1691 [IFLA_GROUP
] = { .type
= NLA_U32
},
1692 [IFLA_IF_NETNSID
] = { .type
= NLA_S32
},
1693 [IFLA_CARRIER_UP_COUNT
] = { .type
= NLA_U32
},
1694 [IFLA_CARRIER_DOWN_COUNT
] = { .type
= NLA_U32
},
1697 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1698 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1699 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1700 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1701 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1704 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1705 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1706 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1707 [IFLA_VF_VLAN_LIST
] = { .type
= NLA_NESTED
},
1708 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1709 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1710 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1711 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1712 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1713 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1714 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1715 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1716 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1719 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1720 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1721 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1722 .len
= PORT_PROFILE_MAX
},
1723 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1724 .len
= PORT_UUID_MAX
},
1725 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1726 .len
= PORT_UUID_MAX
},
1727 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1728 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1730 /* Unused, but we need to keep it here since user space could
1731 * fill it. It's also broken with regard to NLA_BINARY use in
1732 * combination with structs.
1734 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1735 .len
= sizeof(struct ifla_port_vsi
) },
1738 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1739 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1740 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1741 [IFLA_XDP_FLAGS
] = { .type
= NLA_U32
},
1742 [IFLA_XDP_PROG_ID
] = { .type
= NLA_U32
},
1745 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1747 const struct rtnl_link_ops
*ops
= NULL
;
1748 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1750 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
,
1751 ifla_info_policy
, NULL
) < 0)
1754 if (linfo
[IFLA_INFO_KIND
]) {
1755 char kind
[MODULE_NAME_LEN
];
1757 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1758 ops
= rtnl_link_ops_get(kind
);
1764 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1766 struct net_device
*master
;
1771 master
= netdev_master_upper_dev_get(dev
);
1772 if (!master
|| master
->ifindex
!= master_idx
)
1778 static bool link_kind_filtered(const struct net_device
*dev
,
1779 const struct rtnl_link_ops
*kind_ops
)
1781 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1787 static bool link_dump_filtered(struct net_device
*dev
,
1789 const struct rtnl_link_ops
*kind_ops
)
1791 if (link_master_filtered(dev
, master_idx
) ||
1792 link_kind_filtered(dev
, kind_ops
))
1798 static struct net
*get_target_net(struct sock
*sk
, int netnsid
)
1802 net
= get_net_ns_by_id(sock_net(sk
), netnsid
);
1804 return ERR_PTR(-EINVAL
);
1806 /* For now, the caller is required to have CAP_NET_ADMIN in
1807 * the user namespace owning the target net ns.
1809 if (!sk_ns_capable(sk
, net
->user_ns
, CAP_NET_ADMIN
)) {
1811 return ERR_PTR(-EACCES
);
1816 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1818 struct net
*net
= sock_net(skb
->sk
);
1819 struct net
*tgt_net
= net
;
1822 struct net_device
*dev
;
1823 struct hlist_head
*head
;
1824 struct nlattr
*tb
[IFLA_MAX
+1];
1825 u32 ext_filter_mask
= 0;
1826 const struct rtnl_link_ops
*kind_ops
= NULL
;
1827 unsigned int flags
= NLM_F_MULTI
;
1834 s_idx
= cb
->args
[1];
1836 /* A hack to preserve kernel<->userspace interface.
1837 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1838 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1839 * what iproute2 < v3.9.0 used.
1840 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1841 * attribute, its netlink message is shorter than struct ifinfomsg.
1843 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1844 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1846 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
,
1847 ifla_policy
, NULL
) >= 0) {
1848 if (tb
[IFLA_IF_NETNSID
]) {
1849 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
1850 tgt_net
= get_target_net(skb
->sk
, netnsid
);
1851 if (IS_ERR(tgt_net
)) {
1857 if (tb
[IFLA_EXT_MASK
])
1858 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1860 if (tb
[IFLA_MASTER
])
1861 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1863 if (tb
[IFLA_LINKINFO
])
1864 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1866 if (master_idx
|| kind_ops
)
1867 flags
|= NLM_F_DUMP_FILTERED
;
1870 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1872 head
= &tgt_net
->dev_index_head
[h
];
1873 hlist_for_each_entry(dev
, head
, index_hlist
) {
1874 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1878 err
= rtnl_fill_ifinfo(skb
, dev
, net
,
1880 NETLINK_CB(cb
->skb
).portid
,
1881 cb
->nlh
->nlmsg_seq
, 0,
1883 ext_filter_mask
, 0, NULL
, 0,
1887 if (likely(skb
->len
))
1901 cb
->seq
= net
->dev_base_seq
;
1902 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1909 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
,
1910 struct netlink_ext_ack
*exterr
)
1912 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
, exterr
);
1914 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1916 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1919 /* Examine the link attributes and figure out which
1920 * network namespace we are talking about.
1922 if (tb
[IFLA_NET_NS_PID
])
1923 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1924 else if (tb
[IFLA_NET_NS_FD
])
1925 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1927 net
= get_net(src_net
);
1930 EXPORT_SYMBOL(rtnl_link_get_net
);
1932 /* Figure out which network namespace we are talking about by
1933 * examining the link attributes in the following order:
1935 * 1. IFLA_NET_NS_PID
1937 * 3. IFLA_IF_NETNSID
1939 static struct net
*rtnl_link_get_net_by_nlattr(struct net
*src_net
,
1940 struct nlattr
*tb
[])
1944 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
])
1945 return rtnl_link_get_net(src_net
, tb
);
1947 if (!tb
[IFLA_IF_NETNSID
])
1948 return get_net(src_net
);
1950 net
= get_net_ns_by_id(src_net
, nla_get_u32(tb
[IFLA_IF_NETNSID
]));
1952 return ERR_PTR(-EINVAL
);
1957 static struct net
*rtnl_link_get_net_capable(const struct sk_buff
*skb
,
1958 struct net
*src_net
,
1959 struct nlattr
*tb
[], int cap
)
1963 net
= rtnl_link_get_net_by_nlattr(src_net
, tb
);
1967 if (!netlink_ns_capable(skb
, net
->user_ns
, cap
)) {
1969 return ERR_PTR(-EPERM
);
1975 /* Verify that rtnetlink requests do not pass additional properties
1976 * potentially referring to different network namespaces.
1978 static int rtnl_ensure_unique_netns(struct nlattr
*tb
[],
1979 struct netlink_ext_ack
*extack
,
1983 if (netns_id_only
) {
1984 if (!tb
[IFLA_NET_NS_PID
] && !tb
[IFLA_NET_NS_FD
])
1987 NL_SET_ERR_MSG(extack
, "specified netns attribute not supported");
1991 if (tb
[IFLA_IF_NETNSID
] && (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]))
1994 if (tb
[IFLA_NET_NS_PID
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_FD
]))
1997 if (tb
[IFLA_NET_NS_FD
] && (tb
[IFLA_IF_NETNSID
] || tb
[IFLA_NET_NS_PID
]))
2003 NL_SET_ERR_MSG(extack
, "multiple netns identifying attributes specified");
2007 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
2010 if (tb
[IFLA_ADDRESS
] &&
2011 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
2014 if (tb
[IFLA_BROADCAST
] &&
2015 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
2019 if (tb
[IFLA_AF_SPEC
]) {
2023 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2024 const struct rtnl_af_ops
*af_ops
;
2027 af_ops
= rtnl_af_lookup(nla_type(af
));
2030 return -EAFNOSUPPORT
;
2033 if (!af_ops
->set_link_af
) {
2038 if (af_ops
->validate_link_af
) {
2039 err
= af_ops
->validate_link_af(dev
, af
);
2053 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
2056 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2058 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
2061 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
2063 if (dev
->type
!= ARPHRD_INFINIBAND
)
2066 return handle_infiniband_guid(dev
, ivt
, guid_type
);
2069 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
2071 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2074 if (tb
[IFLA_VF_MAC
]) {
2075 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
2078 if (ops
->ndo_set_vf_mac
)
2079 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
2085 if (tb
[IFLA_VF_VLAN
]) {
2086 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
2089 if (ops
->ndo_set_vf_vlan
)
2090 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
2092 htons(ETH_P_8021Q
));
2097 if (tb
[IFLA_VF_VLAN_LIST
]) {
2098 struct ifla_vf_vlan_info
*ivvl
[MAX_VLAN_LIST_LEN
];
2099 struct nlattr
*attr
;
2103 if (!ops
->ndo_set_vf_vlan
)
2106 nla_for_each_nested(attr
, tb
[IFLA_VF_VLAN_LIST
], rem
) {
2107 if (nla_type(attr
) != IFLA_VF_VLAN_INFO
||
2108 nla_len(attr
) < NLA_HDRLEN
) {
2111 if (len
>= MAX_VLAN_LIST_LEN
)
2113 ivvl
[len
] = nla_data(attr
);
2120 err
= ops
->ndo_set_vf_vlan(dev
, ivvl
[0]->vf
, ivvl
[0]->vlan
,
2121 ivvl
[0]->qos
, ivvl
[0]->vlan_proto
);
2126 if (tb
[IFLA_VF_TX_RATE
]) {
2127 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
2128 struct ifla_vf_info ivf
;
2131 if (ops
->ndo_get_vf_config
)
2132 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
2137 if (ops
->ndo_set_vf_rate
)
2138 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2145 if (tb
[IFLA_VF_RATE
]) {
2146 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
2149 if (ops
->ndo_set_vf_rate
)
2150 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
2157 if (tb
[IFLA_VF_SPOOFCHK
]) {
2158 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
2161 if (ops
->ndo_set_vf_spoofchk
)
2162 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
2168 if (tb
[IFLA_VF_LINK_STATE
]) {
2169 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
2172 if (ops
->ndo_set_vf_link_state
)
2173 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
2179 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
2180 struct ifla_vf_rss_query_en
*ivrssq_en
;
2183 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
2184 if (ops
->ndo_set_vf_rss_query_en
)
2185 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
2186 ivrssq_en
->setting
);
2191 if (tb
[IFLA_VF_TRUST
]) {
2192 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
2195 if (ops
->ndo_set_vf_trust
)
2196 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
2201 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
2202 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
2204 if (!ops
->ndo_set_vf_guid
)
2207 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
2210 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
2211 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
2213 if (!ops
->ndo_set_vf_guid
)
2216 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
2222 static int do_set_master(struct net_device
*dev
, int ifindex
,
2223 struct netlink_ext_ack
*extack
)
2225 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
2226 const struct net_device_ops
*ops
;
2230 if (upper_dev
->ifindex
== ifindex
)
2232 ops
= upper_dev
->netdev_ops
;
2233 if (ops
->ndo_del_slave
) {
2234 err
= ops
->ndo_del_slave(upper_dev
, dev
);
2243 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
2246 ops
= upper_dev
->netdev_ops
;
2247 if (ops
->ndo_add_slave
) {
2248 err
= ops
->ndo_add_slave(upper_dev
, dev
, extack
);
2258 #define DO_SETLINK_MODIFIED 0x01
2259 /* notify flag means notify + modified. */
2260 #define DO_SETLINK_NOTIFY 0x03
2261 static int do_setlink(const struct sk_buff
*skb
,
2262 struct net_device
*dev
, struct ifinfomsg
*ifm
,
2263 struct netlink_ext_ack
*extack
,
2264 struct nlattr
**tb
, char *ifname
, int status
)
2266 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2269 err
= validate_linkmsg(dev
, tb
);
2273 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
] || tb
[IFLA_IF_NETNSID
]) {
2274 struct net
*net
= rtnl_link_get_net_capable(skb
, dev_net(dev
),
2281 err
= dev_change_net_namespace(dev
, net
, ifname
);
2285 status
|= DO_SETLINK_MODIFIED
;
2289 struct rtnl_link_ifmap
*u_map
;
2292 if (!ops
->ndo_set_config
) {
2297 if (!netif_device_present(dev
)) {
2302 u_map
= nla_data(tb
[IFLA_MAP
]);
2303 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
2304 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
2305 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
2306 k_map
.irq
= (unsigned char) u_map
->irq
;
2307 k_map
.dma
= (unsigned char) u_map
->dma
;
2308 k_map
.port
= (unsigned char) u_map
->port
;
2310 err
= ops
->ndo_set_config(dev
, &k_map
);
2314 status
|= DO_SETLINK_NOTIFY
;
2317 if (tb
[IFLA_ADDRESS
]) {
2318 struct sockaddr
*sa
;
2321 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
2323 sa
= kmalloc(len
, GFP_KERNEL
);
2328 sa
->sa_family
= dev
->type
;
2329 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
2331 err
= dev_set_mac_address(dev
, sa
);
2335 status
|= DO_SETLINK_MODIFIED
;
2339 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2342 status
|= DO_SETLINK_MODIFIED
;
2345 if (tb
[IFLA_GROUP
]) {
2346 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2347 status
|= DO_SETLINK_NOTIFY
;
2351 * Interface selected by interface index but interface
2352 * name provided implies that a name change has been
2355 if (ifm
->ifi_index
> 0 && ifname
[0]) {
2356 err
= dev_change_name(dev
, ifname
);
2359 status
|= DO_SETLINK_MODIFIED
;
2362 if (tb
[IFLA_IFALIAS
]) {
2363 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
2364 nla_len(tb
[IFLA_IFALIAS
]));
2367 status
|= DO_SETLINK_NOTIFY
;
2370 if (tb
[IFLA_BROADCAST
]) {
2371 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
2372 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
2375 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
2376 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2381 if (tb
[IFLA_MASTER
]) {
2382 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]), extack
);
2385 status
|= DO_SETLINK_MODIFIED
;
2388 if (tb
[IFLA_CARRIER
]) {
2389 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
2392 status
|= DO_SETLINK_MODIFIED
;
2395 if (tb
[IFLA_TXQLEN
]) {
2396 unsigned int value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2398 err
= dev_change_tx_queue_len(dev
, value
);
2401 status
|= DO_SETLINK_MODIFIED
;
2404 if (tb
[IFLA_GSO_MAX_SIZE
]) {
2405 u32 max_size
= nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]);
2407 if (max_size
> GSO_MAX_SIZE
) {
2412 if (dev
->gso_max_size
^ max_size
) {
2413 netif_set_gso_max_size(dev
, max_size
);
2414 status
|= DO_SETLINK_MODIFIED
;
2418 if (tb
[IFLA_GSO_MAX_SEGS
]) {
2419 u32 max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2421 if (max_segs
> GSO_MAX_SEGS
) {
2426 if (dev
->gso_max_segs
^ max_segs
) {
2427 dev
->gso_max_segs
= max_segs
;
2428 status
|= DO_SETLINK_MODIFIED
;
2432 if (tb
[IFLA_OPERSTATE
])
2433 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2435 if (tb
[IFLA_LINKMODE
]) {
2436 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2438 write_lock_bh(&dev_base_lock
);
2439 if (dev
->link_mode
^ value
)
2440 status
|= DO_SETLINK_NOTIFY
;
2441 dev
->link_mode
= value
;
2442 write_unlock_bh(&dev_base_lock
);
2445 if (tb
[IFLA_VFINFO_LIST
]) {
2446 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2447 struct nlattr
*attr
;
2450 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2451 if (nla_type(attr
) != IFLA_VF_INFO
||
2452 nla_len(attr
) < NLA_HDRLEN
) {
2456 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2457 ifla_vf_policy
, NULL
);
2460 err
= do_setvfinfo(dev
, vfinfo
);
2463 status
|= DO_SETLINK_NOTIFY
;
2468 if (tb
[IFLA_VF_PORTS
]) {
2469 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2470 struct nlattr
*attr
;
2475 if (!ops
->ndo_set_vf_port
)
2478 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2479 if (nla_type(attr
) != IFLA_VF_PORT
||
2480 nla_len(attr
) < NLA_HDRLEN
) {
2484 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2485 ifla_port_policy
, NULL
);
2488 if (!port
[IFLA_PORT_VF
]) {
2492 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2493 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2496 status
|= DO_SETLINK_NOTIFY
;
2501 if (tb
[IFLA_PORT_SELF
]) {
2502 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2504 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2505 tb
[IFLA_PORT_SELF
], ifla_port_policy
,
2511 if (ops
->ndo_set_vf_port
)
2512 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2515 status
|= DO_SETLINK_NOTIFY
;
2518 if (tb
[IFLA_AF_SPEC
]) {
2522 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2523 const struct rtnl_af_ops
*af_ops
;
2527 BUG_ON(!(af_ops
= rtnl_af_lookup(nla_type(af
))));
2529 err
= af_ops
->set_link_af(dev
, af
);
2536 status
|= DO_SETLINK_NOTIFY
;
2541 if (tb
[IFLA_PROTO_DOWN
]) {
2542 err
= dev_change_proto_down(dev
,
2543 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2546 status
|= DO_SETLINK_NOTIFY
;
2550 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2553 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2554 ifla_xdp_policy
, NULL
);
2558 if (xdp
[IFLA_XDP_ATTACHED
] || xdp
[IFLA_XDP_PROG_ID
]) {
2563 if (xdp
[IFLA_XDP_FLAGS
]) {
2564 xdp_flags
= nla_get_u32(xdp
[IFLA_XDP_FLAGS
]);
2565 if (xdp_flags
& ~XDP_FLAGS_MASK
) {
2569 if (hweight32(xdp_flags
& XDP_FLAGS_MODES
) > 1) {
2575 if (xdp
[IFLA_XDP_FD
]) {
2576 err
= dev_change_xdp_fd(dev
, extack
,
2577 nla_get_s32(xdp
[IFLA_XDP_FD
]),
2581 status
|= DO_SETLINK_NOTIFY
;
2586 if (status
& DO_SETLINK_MODIFIED
) {
2587 if ((status
& DO_SETLINK_NOTIFY
) == DO_SETLINK_NOTIFY
)
2588 netdev_state_change(dev
);
2591 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",
2598 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2599 struct netlink_ext_ack
*extack
)
2601 struct net
*net
= sock_net(skb
->sk
);
2602 struct ifinfomsg
*ifm
;
2603 struct net_device
*dev
;
2605 struct nlattr
*tb
[IFLA_MAX
+1];
2606 char ifname
[IFNAMSIZ
];
2608 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
,
2613 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2617 if (tb
[IFLA_IFNAME
])
2618 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2623 ifm
= nlmsg_data(nlh
);
2624 if (ifm
->ifi_index
> 0)
2625 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2626 else if (tb
[IFLA_IFNAME
])
2627 dev
= __dev_get_by_name(net
, ifname
);
2636 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
, 0);
2641 static int rtnl_group_dellink(const struct net
*net
, int group
)
2643 struct net_device
*dev
, *aux
;
2644 LIST_HEAD(list_kill
);
2650 for_each_netdev(net
, dev
) {
2651 if (dev
->group
== group
) {
2652 const struct rtnl_link_ops
*ops
;
2655 ops
= dev
->rtnl_link_ops
;
2656 if (!ops
|| !ops
->dellink
)
2664 for_each_netdev_safe(net
, dev
, aux
) {
2665 if (dev
->group
== group
) {
2666 const struct rtnl_link_ops
*ops
;
2668 ops
= dev
->rtnl_link_ops
;
2669 ops
->dellink(dev
, &list_kill
);
2672 unregister_netdevice_many(&list_kill
);
2677 int rtnl_delete_link(struct net_device
*dev
)
2679 const struct rtnl_link_ops
*ops
;
2680 LIST_HEAD(list_kill
);
2682 ops
= dev
->rtnl_link_ops
;
2683 if (!ops
|| !ops
->dellink
)
2686 ops
->dellink(dev
, &list_kill
);
2687 unregister_netdevice_many(&list_kill
);
2691 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2693 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2694 struct netlink_ext_ack
*extack
)
2696 struct net
*net
= sock_net(skb
->sk
);
2697 struct net
*tgt_net
= net
;
2698 struct net_device
*dev
= NULL
;
2699 struct ifinfomsg
*ifm
;
2700 char ifname
[IFNAMSIZ
];
2701 struct nlattr
*tb
[IFLA_MAX
+1];
2705 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2709 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
2713 if (tb
[IFLA_IFNAME
])
2714 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2716 if (tb
[IFLA_IF_NETNSID
]) {
2717 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
2718 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
2719 if (IS_ERR(tgt_net
))
2720 return PTR_ERR(tgt_net
);
2724 ifm
= nlmsg_data(nlh
);
2725 if (ifm
->ifi_index
> 0)
2726 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
2727 else if (tb
[IFLA_IFNAME
])
2728 dev
= __dev_get_by_name(tgt_net
, ifname
);
2729 else if (tb
[IFLA_GROUP
])
2730 err
= rtnl_group_dellink(tgt_net
, nla_get_u32(tb
[IFLA_GROUP
]));
2735 if (tb
[IFLA_IFNAME
] || ifm
->ifi_index
> 0)
2741 err
= rtnl_delete_link(dev
);
2750 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2752 unsigned int old_flags
;
2755 old_flags
= dev
->flags
;
2756 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2757 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2762 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2764 __dev_notify_flags(dev
, old_flags
, ~0U);
2767 EXPORT_SYMBOL(rtnl_configure_link
);
2769 struct net_device
*rtnl_create_link(struct net
*net
,
2770 const char *ifname
, unsigned char name_assign_type
,
2771 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2773 struct net_device
*dev
;
2774 unsigned int num_tx_queues
= 1;
2775 unsigned int num_rx_queues
= 1;
2777 if (tb
[IFLA_NUM_TX_QUEUES
])
2778 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2779 else if (ops
->get_num_tx_queues
)
2780 num_tx_queues
= ops
->get_num_tx_queues();
2782 if (tb
[IFLA_NUM_RX_QUEUES
])
2783 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2784 else if (ops
->get_num_rx_queues
)
2785 num_rx_queues
= ops
->get_num_rx_queues();
2787 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2788 ops
->setup
, num_tx_queues
, num_rx_queues
);
2790 return ERR_PTR(-ENOMEM
);
2792 dev_net_set(dev
, net
);
2793 dev
->rtnl_link_ops
= ops
;
2794 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2797 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2798 if (tb
[IFLA_ADDRESS
]) {
2799 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2800 nla_len(tb
[IFLA_ADDRESS
]));
2801 dev
->addr_assign_type
= NET_ADDR_SET
;
2803 if (tb
[IFLA_BROADCAST
])
2804 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2805 nla_len(tb
[IFLA_BROADCAST
]));
2806 if (tb
[IFLA_TXQLEN
])
2807 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2808 if (tb
[IFLA_OPERSTATE
])
2809 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2810 if (tb
[IFLA_LINKMODE
])
2811 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2813 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2814 if (tb
[IFLA_GSO_MAX_SIZE
])
2815 netif_set_gso_max_size(dev
, nla_get_u32(tb
[IFLA_GSO_MAX_SIZE
]));
2816 if (tb
[IFLA_GSO_MAX_SEGS
])
2817 dev
->gso_max_segs
= nla_get_u32(tb
[IFLA_GSO_MAX_SEGS
]);
2821 EXPORT_SYMBOL(rtnl_create_link
);
2823 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2824 struct net
*net
, int group
,
2825 struct ifinfomsg
*ifm
,
2826 struct netlink_ext_ack
*extack
,
2829 struct net_device
*dev
, *aux
;
2832 for_each_netdev_safe(net
, dev
, aux
) {
2833 if (dev
->group
== group
) {
2834 err
= do_setlink(skb
, dev
, ifm
, extack
, tb
, NULL
, 0);
2843 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2844 struct netlink_ext_ack
*extack
)
2846 struct net
*net
= sock_net(skb
->sk
);
2847 const struct rtnl_link_ops
*ops
;
2848 const struct rtnl_link_ops
*m_ops
= NULL
;
2849 struct net_device
*dev
;
2850 struct net_device
*master_dev
= NULL
;
2851 struct ifinfomsg
*ifm
;
2852 char kind
[MODULE_NAME_LEN
];
2853 char ifname
[IFNAMSIZ
];
2854 struct nlattr
*tb
[IFLA_MAX
+1];
2855 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2856 unsigned char name_assign_type
= NET_NAME_USER
;
2859 #ifdef CONFIG_MODULES
2862 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
2866 err
= rtnl_ensure_unique_netns(tb
, extack
, false);
2870 if (tb
[IFLA_IFNAME
])
2871 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2875 ifm
= nlmsg_data(nlh
);
2876 if (ifm
->ifi_index
> 0)
2877 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2880 dev
= __dev_get_by_name(net
, ifname
);
2886 master_dev
= netdev_master_upper_dev_get(dev
);
2888 m_ops
= master_dev
->rtnl_link_ops
;
2891 err
= validate_linkmsg(dev
, tb
);
2895 if (tb
[IFLA_LINKINFO
]) {
2896 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2897 tb
[IFLA_LINKINFO
], ifla_info_policy
,
2902 memset(linkinfo
, 0, sizeof(linkinfo
));
2904 if (linkinfo
[IFLA_INFO_KIND
]) {
2905 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2906 ops
= rtnl_link_ops_get(kind
);
2913 struct nlattr
*attr
[RTNL_MAX_TYPE
+ 1];
2914 struct nlattr
*slave_attr
[RTNL_SLAVE_MAX_TYPE
+ 1];
2915 struct nlattr
**data
= NULL
;
2916 struct nlattr
**slave_data
= NULL
;
2917 struct net
*dest_net
, *link_net
= NULL
;
2920 if (ops
->maxtype
> RTNL_MAX_TYPE
)
2923 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2924 err
= nla_parse_nested(attr
, ops
->maxtype
,
2925 linkinfo
[IFLA_INFO_DATA
],
2931 if (ops
->validate
) {
2932 err
= ops
->validate(tb
, data
, extack
);
2939 if (m_ops
->slave_maxtype
> RTNL_SLAVE_MAX_TYPE
)
2942 if (m_ops
->slave_maxtype
&&
2943 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2944 err
= nla_parse_nested(slave_attr
,
2945 m_ops
->slave_maxtype
,
2946 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2947 m_ops
->slave_policy
,
2951 slave_data
= slave_attr
;
2958 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2960 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2963 if (linkinfo
[IFLA_INFO_DATA
]) {
2964 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2968 err
= ops
->changelink(dev
, tb
, data
, extack
);
2971 status
|= DO_SETLINK_NOTIFY
;
2974 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2975 if (!m_ops
|| !m_ops
->slave_changelink
)
2978 err
= m_ops
->slave_changelink(master_dev
, dev
,
2983 status
|= DO_SETLINK_NOTIFY
;
2986 return do_setlink(skb
, dev
, ifm
, extack
, tb
, ifname
,
2990 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2991 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2992 return rtnl_group_changelink(skb
, net
,
2993 nla_get_u32(tb
[IFLA_GROUP
]),
2998 if (tb
[IFLA_MAP
] || tb
[IFLA_PROTINFO
])
3002 #ifdef CONFIG_MODULES
3005 request_module("rtnl-link-%s", kind
);
3007 ops
= rtnl_link_ops_get(kind
);
3019 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
3020 name_assign_type
= NET_NAME_ENUM
;
3023 dest_net
= rtnl_link_get_net_capable(skb
, net
, tb
, CAP_NET_ADMIN
);
3024 if (IS_ERR(dest_net
))
3025 return PTR_ERR(dest_net
);
3027 if (tb
[IFLA_LINK_NETNSID
]) {
3028 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
3030 link_net
= get_net_ns_by_id(dest_net
, id
);
3036 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
3040 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
3041 name_assign_type
, ops
, tb
);
3047 dev
->ifindex
= ifm
->ifi_index
;
3050 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
,
3052 /* Drivers should call free_netdev() in ->destructor
3053 * and unregister it on failure after registration
3054 * so that device could be finally freed in rtnl_unlock.
3057 /* If device is not registered at all, free it now */
3058 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
3063 err
= register_netdevice(dev
);
3069 err
= rtnl_configure_link(dev
, ifm
);
3071 goto out_unregister
;
3073 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
3075 goto out_unregister
;
3077 if (tb
[IFLA_MASTER
]) {
3078 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]),
3081 goto out_unregister
;
3090 LIST_HEAD(list_kill
);
3092 ops
->dellink(dev
, &list_kill
);
3093 unregister_netdevice_many(&list_kill
);
3095 unregister_netdevice(dev
);
3101 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3102 struct netlink_ext_ack
*extack
)
3104 struct net
*net
= sock_net(skb
->sk
);
3105 struct net
*tgt_net
= net
;
3106 struct ifinfomsg
*ifm
;
3107 char ifname
[IFNAMSIZ
];
3108 struct nlattr
*tb
[IFLA_MAX
+1];
3109 struct net_device
*dev
= NULL
;
3110 struct sk_buff
*nskb
;
3113 u32 ext_filter_mask
= 0;
3115 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
, extack
);
3119 err
= rtnl_ensure_unique_netns(tb
, extack
, true);
3123 if (tb
[IFLA_IF_NETNSID
]) {
3124 netnsid
= nla_get_s32(tb
[IFLA_IF_NETNSID
]);
3125 tgt_net
= get_target_net(NETLINK_CB(skb
).sk
, netnsid
);
3126 if (IS_ERR(tgt_net
))
3127 return PTR_ERR(tgt_net
);
3130 if (tb
[IFLA_IFNAME
])
3131 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
3133 if (tb
[IFLA_EXT_MASK
])
3134 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3137 ifm
= nlmsg_data(nlh
);
3138 if (ifm
->ifi_index
> 0)
3139 dev
= __dev_get_by_index(tgt_net
, ifm
->ifi_index
);
3140 else if (tb
[IFLA_IFNAME
])
3141 dev
= __dev_get_by_name(tgt_net
, ifname
);
3150 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
3154 err
= rtnl_fill_ifinfo(nskb
, dev
, net
,
3155 RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
3156 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
,
3157 0, NULL
, 0, netnsid
);
3159 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3160 WARN_ON(err
== -EMSGSIZE
);
3163 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3171 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3173 struct net
*net
= sock_net(skb
->sk
);
3174 struct net_device
*dev
;
3175 struct nlattr
*tb
[IFLA_MAX
+1];
3176 u32 ext_filter_mask
= 0;
3177 u16 min_ifinfo_dump_size
= 0;
3180 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3181 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
3182 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
3184 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
, NULL
) >= 0) {
3185 if (tb
[IFLA_EXT_MASK
])
3186 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
3189 if (!ext_filter_mask
)
3190 return NLMSG_GOODSIZE
;
3192 * traverse the list of net devices and compute the minimum
3193 * buffer size based upon the filter mask.
3196 for_each_netdev_rcu(net
, dev
) {
3197 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
3203 return nlmsg_total_size(min_ifinfo_dump_size
);
3206 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3209 int s_idx
= cb
->family
;
3214 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
3215 struct rtnl_link
**tab
;
3216 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
3217 struct rtnl_link
*link
;
3218 rtnl_dumpit_func dumpit
;
3220 if (idx
< s_idx
|| idx
== PF_PACKET
)
3223 if (type
< 0 || type
>= RTM_NR_MSGTYPES
)
3226 tab
= rcu_dereference_rtnl(rtnl_msg_handlers
[idx
]);
3234 dumpit
= link
->dumpit
;
3239 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
3243 if (dumpit(skb
, cb
))
3251 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
3252 unsigned int change
,
3253 u32 event
, gfp_t flags
, int *new_nsid
,
3256 struct net
*net
= dev_net(dev
);
3257 struct sk_buff
*skb
;
3259 size_t if_info_size
;
3261 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
3265 err
= rtnl_fill_ifinfo(skb
, dev
, dev_net(dev
),
3266 type
, 0, 0, change
, 0, 0, event
,
3267 new_nsid
, new_ifindex
, -1);
3269 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3270 WARN_ON(err
== -EMSGSIZE
);
3277 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3281 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
3283 struct net
*net
= dev_net(dev
);
3285 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
3288 static void rtmsg_ifinfo_event(int type
, struct net_device
*dev
,
3289 unsigned int change
, u32 event
,
3290 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3292 struct sk_buff
*skb
;
3294 if (dev
->reg_state
!= NETREG_REGISTERED
)
3297 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, event
, flags
, new_nsid
,
3300 rtmsg_ifinfo_send(skb
, dev
, flags
);
3303 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
3306 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3310 void rtmsg_ifinfo_newnet(int type
, struct net_device
*dev
, unsigned int change
,
3311 gfp_t flags
, int *new_nsid
, int new_ifindex
)
3313 rtmsg_ifinfo_event(type
, dev
, change
, rtnl_get_event(0), flags
,
3314 new_nsid
, new_ifindex
);
3317 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
3318 struct net_device
*dev
,
3319 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
3320 int type
, unsigned int flags
,
3321 int nlflags
, u16 ndm_state
)
3323 struct nlmsghdr
*nlh
;
3326 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
3330 ndm
= nlmsg_data(nlh
);
3331 ndm
->ndm_family
= AF_BRIDGE
;
3334 ndm
->ndm_flags
= flags
;
3336 ndm
->ndm_ifindex
= dev
->ifindex
;
3337 ndm
->ndm_state
= ndm_state
;
3339 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
3340 goto nla_put_failure
;
3342 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
3343 goto nla_put_failure
;
3345 nlmsg_end(skb
, nlh
);
3349 nlmsg_cancel(skb
, nlh
);
3353 static inline size_t rtnl_fdb_nlmsg_size(void)
3355 return NLMSG_ALIGN(sizeof(struct ndmsg
)) +
3356 nla_total_size(ETH_ALEN
) + /* NDA_LLADDR */
3357 nla_total_size(sizeof(u16
)) + /* NDA_VLAN */
3361 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
3364 struct net
*net
= dev_net(dev
);
3365 struct sk_buff
*skb
;
3368 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
3372 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
3373 0, 0, type
, NTF_SELF
, 0, ndm_state
);
3379 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
3382 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
3386 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3388 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
3389 struct nlattr
*tb
[],
3390 struct net_device
*dev
,
3391 const unsigned char *addr
, u16 vid
,
3396 /* If aging addresses are supported device will need to
3397 * implement its own handler for this.
3399 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
3400 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3405 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
3409 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3410 err
= dev_uc_add_excl(dev
, addr
);
3411 else if (is_multicast_ether_addr(addr
))
3412 err
= dev_mc_add_excl(dev
, addr
);
3414 /* Only return duplicate errors if NLM_F_EXCL is set */
3415 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
3420 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
3422 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
,
3423 struct netlink_ext_ack
*extack
)
3428 if (nla_len(vlan_attr
) != sizeof(u16
)) {
3429 NL_SET_ERR_MSG(extack
, "invalid vlan attribute size");
3433 vid
= nla_get_u16(vlan_attr
);
3435 if (!vid
|| vid
>= VLAN_VID_MASK
) {
3436 NL_SET_ERR_MSG(extack
, "invalid vlan id");
3444 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3445 struct netlink_ext_ack
*extack
)
3447 struct net
*net
= sock_net(skb
->sk
);
3449 struct nlattr
*tb
[NDA_MAX
+1];
3450 struct net_device
*dev
;
3455 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3459 ndm
= nlmsg_data(nlh
);
3460 if (ndm
->ndm_ifindex
== 0) {
3461 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3465 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3467 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3471 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3472 NL_SET_ERR_MSG(extack
, "invalid address");
3476 addr
= nla_data(tb
[NDA_LLADDR
]);
3478 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3484 /* Support fdb on master device the net/bridge default case */
3485 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3486 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3487 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3488 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3490 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3495 ndm
->ndm_flags
&= ~NTF_MASTER
;
3498 /* Embedded bridge, macvlan, and any other device support */
3499 if ((ndm
->ndm_flags
& NTF_SELF
)) {
3500 if (dev
->netdev_ops
->ndo_fdb_add
)
3501 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
3505 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
3509 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
3511 ndm
->ndm_flags
&= ~NTF_SELF
;
3519 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3521 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
3522 struct nlattr
*tb
[],
3523 struct net_device
*dev
,
3524 const unsigned char *addr
, u16 vid
)
3528 /* If aging addresses are supported device will need to
3529 * implement its own handler for this.
3531 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
3532 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
3536 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
3537 err
= dev_uc_del(dev
, addr
);
3538 else if (is_multicast_ether_addr(addr
))
3539 err
= dev_mc_del(dev
, addr
);
3543 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
3545 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
3546 struct netlink_ext_ack
*extack
)
3548 struct net
*net
= sock_net(skb
->sk
);
3550 struct nlattr
*tb
[NDA_MAX
+1];
3551 struct net_device
*dev
;
3556 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
3559 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
, extack
);
3563 ndm
= nlmsg_data(nlh
);
3564 if (ndm
->ndm_ifindex
== 0) {
3565 NL_SET_ERR_MSG(extack
, "invalid ifindex");
3569 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3571 NL_SET_ERR_MSG(extack
, "unknown ifindex");
3575 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3576 NL_SET_ERR_MSG(extack
, "invalid address");
3580 addr
= nla_data(tb
[NDA_LLADDR
]);
3582 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
, extack
);
3588 /* Support fdb on master device the net/bridge default case */
3589 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3590 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3591 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3592 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3594 if (ops
->ndo_fdb_del
)
3595 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3600 ndm
->ndm_flags
&= ~NTF_MASTER
;
3603 /* Embedded bridge, macvlan, and any other device support */
3604 if (ndm
->ndm_flags
& NTF_SELF
) {
3605 if (dev
->netdev_ops
->ndo_fdb_del
)
3606 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3609 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3612 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3614 ndm
->ndm_flags
&= ~NTF_SELF
;
3621 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3622 struct netlink_callback
*cb
,
3623 struct net_device
*dev
,
3625 struct netdev_hw_addr_list
*list
)
3627 struct netdev_hw_addr
*ha
;
3631 portid
= NETLINK_CB(cb
->skb
).portid
;
3632 seq
= cb
->nlh
->nlmsg_seq
;
3634 list_for_each_entry(ha
, &list
->list
, list
) {
3635 if (*idx
< cb
->args
[2])
3638 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3640 RTM_NEWNEIGH
, NTF_SELF
,
3641 NLM_F_MULTI
, NUD_PERMANENT
);
3651 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3652 * @nlh: netlink message header
3655 * Default netdevice operation to dump the existing unicast address list.
3656 * Returns number of addresses from list put in skb.
3658 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3659 struct netlink_callback
*cb
,
3660 struct net_device
*dev
,
3661 struct net_device
*filter_dev
,
3666 netif_addr_lock_bh(dev
);
3667 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->uc
);
3670 err
= nlmsg_populate_fdb(skb
, cb
, dev
, idx
, &dev
->mc
);
3672 netif_addr_unlock_bh(dev
);
3675 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3677 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3679 struct net_device
*dev
;
3680 struct nlattr
*tb
[IFLA_MAX
+1];
3681 struct net_device
*br_dev
= NULL
;
3682 const struct net_device_ops
*ops
= NULL
;
3683 const struct net_device_ops
*cops
= NULL
;
3684 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3685 struct net
*net
= sock_net(skb
->sk
);
3686 struct hlist_head
*head
;
3694 err
= nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
,
3695 IFLA_MAX
, ifla_policy
, NULL
);
3698 } else if (err
== 0) {
3699 if (tb
[IFLA_MASTER
])
3700 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3703 brport_idx
= ifm
->ifi_index
;
3706 br_dev
= __dev_get_by_index(net
, br_idx
);
3710 ops
= br_dev
->netdev_ops
;
3714 s_idx
= cb
->args
[1];
3716 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3718 head
= &net
->dev_index_head
[h
];
3719 hlist_for_each_entry(dev
, head
, index_hlist
) {
3721 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3724 if (!br_idx
) { /* user did not specify a specific bridge */
3725 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3726 br_dev
= netdev_master_upper_dev_get(dev
);
3727 cops
= br_dev
->netdev_ops
;
3730 if (dev
!= br_dev
&&
3731 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3734 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3735 !(dev
->priv_flags
& IFF_EBRIDGE
))
3743 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3744 if (cops
&& cops
->ndo_fdb_dump
) {
3745 err
= cops
->ndo_fdb_dump(skb
, cb
,
3748 if (err
== -EMSGSIZE
)
3753 if (dev
->netdev_ops
->ndo_fdb_dump
)
3754 err
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
,
3758 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
,
3760 if (err
== -EMSGSIZE
)
3765 /* reset fdb offset to 0 for rest of the interfaces */
3781 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3782 unsigned int attrnum
, unsigned int flag
)
3785 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3789 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3790 struct net_device
*dev
, u16 mode
,
3791 u32 flags
, u32 mask
, int nlflags
,
3793 int (*vlan_fill
)(struct sk_buff
*skb
,
3794 struct net_device
*dev
,
3797 struct nlmsghdr
*nlh
;
3798 struct ifinfomsg
*ifm
;
3799 struct nlattr
*br_afspec
;
3800 struct nlattr
*protinfo
;
3801 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3802 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3805 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3809 ifm
= nlmsg_data(nlh
);
3810 ifm
->ifi_family
= AF_BRIDGE
;
3812 ifm
->ifi_type
= dev
->type
;
3813 ifm
->ifi_index
= dev
->ifindex
;
3814 ifm
->ifi_flags
= dev_get_flags(dev
);
3815 ifm
->ifi_change
= 0;
3818 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3819 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3820 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3822 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3824 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3825 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3826 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3827 goto nla_put_failure
;
3829 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3831 goto nla_put_failure
;
3833 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3834 nla_nest_cancel(skb
, br_afspec
);
3835 goto nla_put_failure
;
3838 if (mode
!= BRIDGE_MODE_UNDEF
) {
3839 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3840 nla_nest_cancel(skb
, br_afspec
);
3841 goto nla_put_failure
;
3845 err
= vlan_fill(skb
, dev
, filter_mask
);
3847 nla_nest_cancel(skb
, br_afspec
);
3848 goto nla_put_failure
;
3851 nla_nest_end(skb
, br_afspec
);
3853 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3855 goto nla_put_failure
;
3857 if (brport_nla_put_flag(skb
, flags
, mask
,
3858 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3859 brport_nla_put_flag(skb
, flags
, mask
,
3860 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3861 brport_nla_put_flag(skb
, flags
, mask
,
3862 IFLA_BRPORT_FAST_LEAVE
,
3863 BR_MULTICAST_FAST_LEAVE
) ||
3864 brport_nla_put_flag(skb
, flags
, mask
,
3865 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3866 brport_nla_put_flag(skb
, flags
, mask
,
3867 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3868 brport_nla_put_flag(skb
, flags
, mask
,
3869 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3870 brport_nla_put_flag(skb
, flags
, mask
,
3871 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3872 brport_nla_put_flag(skb
, flags
, mask
,
3873 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3874 nla_nest_cancel(skb
, protinfo
);
3875 goto nla_put_failure
;
3878 nla_nest_end(skb
, protinfo
);
3880 nlmsg_end(skb
, nlh
);
3883 nlmsg_cancel(skb
, nlh
);
3884 return err
? err
: -EMSGSIZE
;
3886 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3888 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3890 struct net
*net
= sock_net(skb
->sk
);
3891 struct net_device
*dev
;
3893 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3894 u32 seq
= cb
->nlh
->nlmsg_seq
;
3895 u32 filter_mask
= 0;
3898 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3899 struct nlattr
*extfilt
;
3901 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3904 if (nla_len(extfilt
) < sizeof(filter_mask
))
3907 filter_mask
= nla_get_u32(extfilt
);
3912 for_each_netdev_rcu(net
, dev
) {
3913 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3914 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3916 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3917 if (idx
>= cb
->args
[0]) {
3918 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3919 skb
, portid
, seq
, dev
,
3920 filter_mask
, NLM_F_MULTI
);
3921 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3922 if (likely(skb
->len
))
3931 if (ops
->ndo_bridge_getlink
) {
3932 if (idx
>= cb
->args
[0]) {
3933 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3937 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3938 if (likely(skb
->len
))
3955 static inline size_t bridge_nlmsg_size(void)
3957 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3958 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3959 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3960 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3961 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3962 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3963 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3964 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3965 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3966 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3967 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3970 static int rtnl_bridge_notify(struct net_device
*dev
)
3972 struct net
*net
= dev_net(dev
);
3973 struct sk_buff
*skb
;
3974 int err
= -EOPNOTSUPP
;
3976 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3979 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3985 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3992 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3995 WARN_ON(err
== -EMSGSIZE
);
3998 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
4002 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4003 struct netlink_ext_ack
*extack
)
4005 struct net
*net
= sock_net(skb
->sk
);
4006 struct ifinfomsg
*ifm
;
4007 struct net_device
*dev
;
4008 struct nlattr
*br_spec
, *attr
= NULL
;
4009 int rem
, err
= -EOPNOTSUPP
;
4011 bool have_flags
= false;
4013 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4016 ifm
= nlmsg_data(nlh
);
4017 if (ifm
->ifi_family
!= AF_BRIDGE
)
4018 return -EPFNOSUPPORT
;
4020 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4022 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4026 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4028 nla_for_each_nested(attr
, br_spec
, rem
) {
4029 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4030 if (nla_len(attr
) < sizeof(flags
))
4034 flags
= nla_get_u16(attr
);
4040 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4041 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4043 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
4048 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
4052 flags
&= ~BRIDGE_FLAGS_MASTER
;
4055 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4056 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
4059 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
4062 flags
&= ~BRIDGE_FLAGS_SELF
;
4064 /* Generate event to notify upper layer of bridge
4067 err
= rtnl_bridge_notify(dev
);
4072 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4077 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4078 struct netlink_ext_ack
*extack
)
4080 struct net
*net
= sock_net(skb
->sk
);
4081 struct ifinfomsg
*ifm
;
4082 struct net_device
*dev
;
4083 struct nlattr
*br_spec
, *attr
= NULL
;
4084 int rem
, err
= -EOPNOTSUPP
;
4086 bool have_flags
= false;
4088 if (nlmsg_len(nlh
) < sizeof(*ifm
))
4091 ifm
= nlmsg_data(nlh
);
4092 if (ifm
->ifi_family
!= AF_BRIDGE
)
4093 return -EPFNOSUPPORT
;
4095 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
4097 NL_SET_ERR_MSG(extack
, "unknown ifindex");
4101 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
4103 nla_for_each_nested(attr
, br_spec
, rem
) {
4104 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
4105 if (nla_len(attr
) < sizeof(flags
))
4109 flags
= nla_get_u16(attr
);
4115 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
4116 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
4118 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
4123 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
4127 flags
&= ~BRIDGE_FLAGS_MASTER
;
4130 if ((flags
& BRIDGE_FLAGS_SELF
)) {
4131 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
4134 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
4138 flags
&= ~BRIDGE_FLAGS_SELF
;
4140 /* Generate event to notify upper layer of bridge
4143 err
= rtnl_bridge_notify(dev
);
4148 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
4153 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
4155 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
4156 (!idxattr
|| idxattr
== attrid
);
4159 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4160 static int rtnl_get_offload_stats_attr_size(int attr_id
)
4163 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
4164 return sizeof(struct rtnl_link_stats64
);
4170 static int rtnl_get_offload_stats(struct sk_buff
*skb
, struct net_device
*dev
,
4173 struct nlattr
*attr
= NULL
;
4178 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4179 dev
->netdev_ops
->ndo_get_offload_stats
))
4182 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4183 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4184 if (attr_id
< *prividx
)
4187 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4191 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4194 attr
= nla_reserve_64bit(skb
, attr_id
, size
,
4195 IFLA_OFFLOAD_XSTATS_UNSPEC
);
4197 goto nla_put_failure
;
4199 attr_data
= nla_data(attr
);
4200 memset(attr_data
, 0, size
);
4201 err
= dev
->netdev_ops
->ndo_get_offload_stats(attr_id
, dev
,
4204 goto get_offload_stats_failure
;
4215 get_offload_stats_failure
:
4220 static int rtnl_get_offload_stats_size(const struct net_device
*dev
)
4226 if (!(dev
->netdev_ops
&& dev
->netdev_ops
->ndo_has_offload_stats
&&
4227 dev
->netdev_ops
->ndo_get_offload_stats
))
4230 for (attr_id
= IFLA_OFFLOAD_XSTATS_FIRST
;
4231 attr_id
<= IFLA_OFFLOAD_XSTATS_MAX
; attr_id
++) {
4232 if (!dev
->netdev_ops
->ndo_has_offload_stats(dev
, attr_id
))
4234 size
= rtnl_get_offload_stats_attr_size(attr_id
);
4235 nla_size
+= nla_total_size_64bit(size
);
4239 nla_size
+= nla_total_size(0);
4244 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
4245 int type
, u32 pid
, u32 seq
, u32 change
,
4246 unsigned int flags
, unsigned int filter_mask
,
4247 int *idxattr
, int *prividx
)
4249 struct if_stats_msg
*ifsm
;
4250 struct nlmsghdr
*nlh
;
4251 struct nlattr
*attr
;
4252 int s_prividx
= *prividx
;
4257 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
4261 ifsm
= nlmsg_data(nlh
);
4262 ifsm
->family
= PF_UNSPEC
;
4265 ifsm
->ifindex
= dev
->ifindex
;
4266 ifsm
->filter_mask
= filter_mask
;
4268 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
4269 struct rtnl_link_stats64
*sp
;
4271 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
4272 sizeof(struct rtnl_link_stats64
),
4275 goto nla_put_failure
;
4277 sp
= nla_data(attr
);
4278 dev_get_stats(dev
, sp
);
4281 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
4282 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4284 if (ops
&& ops
->fill_linkxstats
) {
4285 *idxattr
= IFLA_STATS_LINK_XSTATS
;
4286 attr
= nla_nest_start(skb
,
4287 IFLA_STATS_LINK_XSTATS
);
4289 goto nla_put_failure
;
4291 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4292 nla_nest_end(skb
, attr
);
4294 goto nla_put_failure
;
4299 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
4301 const struct rtnl_link_ops
*ops
= NULL
;
4302 const struct net_device
*master
;
4304 master
= netdev_master_upper_dev_get(dev
);
4306 ops
= master
->rtnl_link_ops
;
4307 if (ops
&& ops
->fill_linkxstats
) {
4308 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4309 attr
= nla_nest_start(skb
,
4310 IFLA_STATS_LINK_XSTATS_SLAVE
);
4312 goto nla_put_failure
;
4314 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
4315 nla_nest_end(skb
, attr
);
4317 goto nla_put_failure
;
4322 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
,
4324 *idxattr
= IFLA_STATS_LINK_OFFLOAD_XSTATS
;
4325 attr
= nla_nest_start(skb
, IFLA_STATS_LINK_OFFLOAD_XSTATS
);
4327 goto nla_put_failure
;
4329 err
= rtnl_get_offload_stats(skb
, dev
, prividx
);
4330 if (err
== -ENODATA
)
4331 nla_nest_cancel(skb
, attr
);
4333 nla_nest_end(skb
, attr
);
4335 if (err
&& err
!= -ENODATA
)
4336 goto nla_put_failure
;
4340 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, *idxattr
)) {
4341 struct rtnl_af_ops
*af_ops
;
4343 *idxattr
= IFLA_STATS_AF_SPEC
;
4344 attr
= nla_nest_start(skb
, IFLA_STATS_AF_SPEC
);
4346 goto nla_put_failure
;
4349 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4350 if (af_ops
->fill_stats_af
) {
4354 af
= nla_nest_start(skb
, af_ops
->family
);
4357 goto nla_put_failure
;
4359 err
= af_ops
->fill_stats_af(skb
, dev
);
4361 if (err
== -ENODATA
) {
4362 nla_nest_cancel(skb
, af
);
4363 } else if (err
< 0) {
4365 goto nla_put_failure
;
4368 nla_nest_end(skb
, af
);
4373 nla_nest_end(skb
, attr
);
4378 nlmsg_end(skb
, nlh
);
4383 /* not a multi message or no progress mean a real error */
4384 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
4385 nlmsg_cancel(skb
, nlh
);
4387 nlmsg_end(skb
, nlh
);
4392 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
4397 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
4398 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
4400 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
4401 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
4402 int attr
= IFLA_STATS_LINK_XSTATS
;
4404 if (ops
&& ops
->get_linkxstats_size
) {
4405 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4407 /* for IFLA_STATS_LINK_XSTATS */
4408 size
+= nla_total_size(0);
4412 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
4413 struct net_device
*_dev
= (struct net_device
*)dev
;
4414 const struct rtnl_link_ops
*ops
= NULL
;
4415 const struct net_device
*master
;
4417 /* netdev_master_upper_dev_get can't take const */
4418 master
= netdev_master_upper_dev_get(_dev
);
4420 ops
= master
->rtnl_link_ops
;
4421 if (ops
&& ops
->get_linkxstats_size
) {
4422 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
4424 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
4426 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4427 size
+= nla_total_size(0);
4431 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_OFFLOAD_XSTATS
, 0))
4432 size
+= rtnl_get_offload_stats_size(dev
);
4434 if (stats_attr_valid(filter_mask
, IFLA_STATS_AF_SPEC
, 0)) {
4435 struct rtnl_af_ops
*af_ops
;
4437 /* for IFLA_STATS_AF_SPEC */
4438 size
+= nla_total_size(0);
4441 list_for_each_entry_rcu(af_ops
, &rtnl_af_ops
, list
) {
4442 if (af_ops
->get_stats_af_size
) {
4443 size
+= nla_total_size(
4444 af_ops
->get_stats_af_size(dev
));
4447 size
+= nla_total_size(0);
4456 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4457 struct netlink_ext_ack
*extack
)
4459 struct net
*net
= sock_net(skb
->sk
);
4460 struct net_device
*dev
= NULL
;
4461 int idxattr
= 0, prividx
= 0;
4462 struct if_stats_msg
*ifsm
;
4463 struct sk_buff
*nskb
;
4467 if (nlmsg_len(nlh
) < sizeof(*ifsm
))
4470 ifsm
= nlmsg_data(nlh
);
4471 if (ifsm
->ifindex
> 0)
4472 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
4479 filter_mask
= ifsm
->filter_mask
;
4483 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
4487 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
4488 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
4489 0, filter_mask
, &idxattr
, &prividx
);
4491 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4492 WARN_ON(err
== -EMSGSIZE
);
4495 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
4501 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4503 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
4504 struct net
*net
= sock_net(skb
->sk
);
4505 unsigned int flags
= NLM_F_MULTI
;
4506 struct if_stats_msg
*ifsm
;
4507 struct hlist_head
*head
;
4508 struct net_device
*dev
;
4509 u32 filter_mask
= 0;
4513 s_idx
= cb
->args
[1];
4514 s_idxattr
= cb
->args
[2];
4515 s_prividx
= cb
->args
[3];
4517 cb
->seq
= net
->dev_base_seq
;
4519 if (nlmsg_len(cb
->nlh
) < sizeof(*ifsm
))
4522 ifsm
= nlmsg_data(cb
->nlh
);
4523 filter_mask
= ifsm
->filter_mask
;
4527 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4529 head
= &net
->dev_index_head
[h
];
4530 hlist_for_each_entry(dev
, head
, index_hlist
) {
4533 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
4534 NETLINK_CB(cb
->skb
).portid
,
4535 cb
->nlh
->nlmsg_seq
, 0,
4537 &s_idxattr
, &s_prividx
);
4538 /* If we ran out of room on the first message,
4541 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
4547 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4553 cb
->args
[3] = s_prividx
;
4554 cb
->args
[2] = s_idxattr
;
4561 /* Process one rtnetlink message. */
4563 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4564 struct netlink_ext_ack
*extack
)
4566 struct net
*net
= sock_net(skb
->sk
);
4567 struct rtnl_link
*link
;
4568 struct module
*owner
;
4569 int err
= -EOPNOTSUPP
;
4570 rtnl_doit_func doit
;
4576 type
= nlh
->nlmsg_type
;
4582 /* All the messages must have at least 1 byte length */
4583 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
4586 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
4589 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
4593 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
4595 rtnl_dumpit_func dumpit
;
4596 u16 min_dump_alloc
= 0;
4598 link
= rtnl_get_link(family
, type
);
4599 if (!link
|| !link
->dumpit
) {
4601 link
= rtnl_get_link(family
, type
);
4602 if (!link
|| !link
->dumpit
)
4605 owner
= link
->owner
;
4606 dumpit
= link
->dumpit
;
4608 if (type
== RTM_GETLINK
- RTM_BASE
)
4609 min_dump_alloc
= rtnl_calcit(skb
, nlh
);
4612 /* need to do this before rcu_read_unlock() */
4613 if (!try_module_get(owner
))
4614 err
= -EPROTONOSUPPORT
;
4620 struct netlink_dump_control c
= {
4622 .min_dump_alloc
= min_dump_alloc
,
4625 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
4626 /* netlink_dump_start() will keep a reference on
4627 * module if dump is still in progress.
4634 link
= rtnl_get_link(family
, type
);
4635 if (!link
|| !link
->doit
) {
4637 link
= rtnl_get_link(PF_UNSPEC
, type
);
4638 if (!link
|| !link
->doit
)
4642 owner
= link
->owner
;
4643 if (!try_module_get(owner
)) {
4644 err
= -EPROTONOSUPPORT
;
4648 flags
= link
->flags
;
4649 if (flags
& RTNL_FLAG_DOIT_UNLOCKED
) {
4653 err
= doit(skb
, nlh
, extack
);
4660 link
= rtnl_get_link(family
, type
);
4661 if (link
&& link
->doit
)
4662 err
= link
->doit(skb
, nlh
, extack
);
4678 static void rtnetlink_rcv(struct sk_buff
*skb
)
4680 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
4683 static int rtnetlink_bind(struct net
*net
, int group
)
4686 case RTNLGRP_IPV4_MROUTE_R
:
4687 case RTNLGRP_IPV6_MROUTE_R
:
4688 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4695 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
4697 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4701 case NETDEV_CHANGEMTU
:
4702 case NETDEV_CHANGEADDR
:
4703 case NETDEV_CHANGENAME
:
4704 case NETDEV_FEAT_CHANGE
:
4705 case NETDEV_BONDING_FAILOVER
:
4706 case NETDEV_POST_TYPE_CHANGE
:
4707 case NETDEV_NOTIFY_PEERS
:
4708 case NETDEV_CHANGEUPPER
:
4709 case NETDEV_RESEND_IGMP
:
4710 case NETDEV_CHANGEINFODATA
:
4711 case NETDEV_CHANGELOWERSTATE
:
4712 case NETDEV_CHANGE_TX_QUEUE_LEN
:
4713 rtmsg_ifinfo_event(RTM_NEWLINK
, dev
, 0, rtnl_get_event(event
),
4714 GFP_KERNEL
, NULL
, 0);
4722 static struct notifier_block rtnetlink_dev_notifier
= {
4723 .notifier_call
= rtnetlink_event
,
4727 static int __net_init
rtnetlink_net_init(struct net
*net
)
4730 struct netlink_kernel_cfg cfg
= {
4731 .groups
= RTNLGRP_MAX
,
4732 .input
= rtnetlink_rcv
,
4733 .cb_mutex
= &rtnl_mutex
,
4734 .flags
= NL_CFG_F_NONROOT_RECV
,
4735 .bind
= rtnetlink_bind
,
4738 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
4745 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
4747 netlink_kernel_release(net
->rtnl
);
4751 static struct pernet_operations rtnetlink_net_ops
= {
4752 .init
= rtnetlink_net_init
,
4753 .exit
= rtnetlink_net_exit
,
4756 void __init
rtnetlink_init(void)
4758 if (register_pernet_subsys(&rtnetlink_net_ops
))
4759 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4761 register_netdevice_notifier(&rtnetlink_dev_notifier
);
4763 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
4764 rtnl_dump_ifinfo
, 0);
4765 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, 0);
4766 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, 0);
4767 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, 0);
4769 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, 0);
4770 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, 0);
4771 rtnl_register(PF_UNSPEC
, RTM_GETNETCONF
, NULL
, rtnl_dump_all
, 0);
4773 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, 0);
4774 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, 0);
4775 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, 0);
4777 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, 0);
4778 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, 0);
4779 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, 0);
4781 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,