Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / core / rtnetlink.c
blobcec60583931fe62d457b91eb830f285a21e63d80
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Routing netlink socket interface: protocol independent part.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Fixes:
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
41 #include <linux/uaccess.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <net/ip.h>
46 #include <net/protocol.h>
47 #include <net/arp.h>
48 #include <net/route.h>
49 #include <net/udp.h>
50 #include <net/tcp.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
57 #define RTNL_MAX_TYPE 50
58 #define RTNL_SLAVE_MAX_TYPE 36
60 struct rtnl_link {
61 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
63 struct module *owner;
64 unsigned int flags;
65 struct rcu_head rcu;
68 static DEFINE_MUTEX(rtnl_mutex);
70 void rtnl_lock(void)
72 mutex_lock(&rtnl_mutex);
74 EXPORT_SYMBOL(rtnl_lock);
76 int rtnl_lock_killable(void)
78 return mutex_lock_killable(&rtnl_mutex);
80 EXPORT_SYMBOL(rtnl_lock_killable);
82 static struct sk_buff *defer_kfree_skb_list;
83 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
85 if (head && tail) {
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
90 EXPORT_SYMBOL(rtnl_kfree_skbs);
92 void __rtnl_unlock(void)
94 struct sk_buff *head = defer_kfree_skb_list;
96 defer_kfree_skb_list = NULL;
98 mutex_unlock(&rtnl_mutex);
100 while (head) {
101 struct sk_buff *next = head->next;
103 kfree_skb(head);
104 cond_resched();
105 head = next;
109 void rtnl_unlock(void)
111 /* This fellow will unlock it for us. */
112 netdev_run_todo();
114 EXPORT_SYMBOL(rtnl_unlock);
116 int rtnl_trylock(void)
118 return mutex_trylock(&rtnl_mutex);
120 EXPORT_SYMBOL(rtnl_trylock);
122 int rtnl_is_locked(void)
124 return mutex_is_locked(&rtnl_mutex);
126 EXPORT_SYMBOL(rtnl_is_locked);
128 bool refcount_dec_and_rtnl_lock(refcount_t *r)
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
132 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
134 #ifdef CONFIG_PROVE_LOCKING
135 bool lockdep_rtnl_is_held(void)
137 return lockdep_is_held(&rtnl_mutex);
139 EXPORT_SYMBOL(lockdep_rtnl_is_held);
140 #endif /* #ifdef CONFIG_PROVE_LOCKING */
142 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
144 static inline int rtm_msgindex(int msgtype)
146 int msgindex = msgtype - RTM_BASE;
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
155 return msgindex;
158 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
160 struct rtnl_link **tab;
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
166 if (!tab)
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
169 return tab[msgtype];
172 static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
175 unsigned int flags)
177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
179 int msgindex;
180 int ret = -ENOBUFS;
182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
183 msgindex = rtm_msgindex(msgtype);
185 rtnl_lock();
186 tab = rtnl_msg_handlers[protocol];
187 if (tab == NULL) {
188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
189 if (!tab)
190 goto unlock;
192 /* ensures we see the 0 stores */
193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
196 old = rtnl_dereference(tab[msgindex]);
197 if (old) {
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
199 if (!link)
200 goto unlock;
201 } else {
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
203 if (!link)
204 goto unlock;
207 WARN_ON(link->owner && link->owner != owner);
208 link->owner = owner;
210 WARN_ON(doit && link->doit && link->doit != doit);
211 if (doit)
212 link->doit = doit;
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
214 if (dumpit)
215 link->dumpit = dumpit;
217 link->flags |= flags;
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
221 ret = 0;
222 if (old)
223 kfree_rcu(old, rcu);
224 unlock:
225 rtnl_unlock();
226 return ret;
230 * rtnl_register_module - Register a rtnetlink message type
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
239 * Like rtnl_register, but for use by removable modules.
241 int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
244 unsigned int flags)
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
249 EXPORT_SYMBOL_GPL(rtnl_register_module);
252 * rtnl_register - Register a rtnetlink message type
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
265 * family exists.
267 void rtnl_register(int protocol, int msgtype,
268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
269 unsigned int flags)
271 int err;
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
274 flags);
275 if (err)
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
285 * Returns 0 on success or a negative error code.
287 int rtnl_unregister(int protocol, int msgtype)
289 struct rtnl_link **tab, *link;
290 int msgindex;
292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293 msgindex = rtm_msgindex(msgtype);
295 rtnl_lock();
296 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
297 if (!tab) {
298 rtnl_unlock();
299 return -ENOENT;
302 link = tab[msgindex];
303 rcu_assign_pointer(tab[msgindex], NULL);
304 rtnl_unlock();
306 kfree_rcu(link, rcu);
308 return 0;
310 EXPORT_SYMBOL_GPL(rtnl_unregister);
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
319 void rtnl_unregister_all(int protocol)
321 struct rtnl_link **tab, *link;
322 int msgindex;
324 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
326 rtnl_lock();
327 tab = rtnl_msg_handlers[protocol];
328 if (!tab) {
329 rtnl_unlock();
330 return;
332 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
333 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334 link = tab[msgindex];
335 if (!link)
336 continue;
338 rcu_assign_pointer(tab[msgindex], NULL);
339 kfree_rcu(link, rcu);
341 rtnl_unlock();
343 synchronize_net();
345 kfree(tab);
347 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
349 static LIST_HEAD(link_ops);
351 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
353 const struct rtnl_link_ops *ops;
355 list_for_each_entry(ops, &link_ops, list) {
356 if (!strcmp(ops->kind, kind))
357 return ops;
359 return NULL;
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
370 * Returns 0 on success or a negative error code.
372 int __rtnl_link_register(struct rtnl_link_ops *ops)
374 if (rtnl_link_ops_get(ops->kind))
375 return -EEXIST;
377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
382 if (ops->setup && !ops->dellink)
383 ops->dellink = unregister_netdevice_queue;
385 list_add_tail(&ops->list, &link_ops);
386 return 0;
388 EXPORT_SYMBOL_GPL(__rtnl_link_register);
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
394 * Returns 0 on success or a negative error code.
396 int rtnl_link_register(struct rtnl_link_ops *ops)
398 int err;
400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
403 return -EINVAL;
405 rtnl_lock();
406 err = __rtnl_link_register(ops);
407 rtnl_unlock();
408 return err;
410 EXPORT_SYMBOL_GPL(rtnl_link_register);
412 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
414 struct net_device *dev;
415 LIST_HEAD(list_kill);
417 for_each_netdev(net, dev) {
418 if (dev->rtnl_link_ops == ops)
419 ops->dellink(dev, &list_kill);
421 unregister_netdevice_many(&list_kill);
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
432 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
434 struct net *net;
436 for_each_net(net) {
437 __rtnl_kill_links(net, ops);
439 list_del(&ops->list);
441 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
443 /* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
446 static void rtnl_lock_unregistering_all(void)
448 struct net *net;
449 bool unregistering;
450 DEFINE_WAIT_FUNC(wait, woken_wake_function);
452 add_wait_queue(&netdev_unregistering_wq, &wait);
453 for (;;) {
454 unregistering = false;
455 rtnl_lock();
456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
459 for_each_net(net) {
460 if (net->dev_unreg_count > 0) {
461 unregistering = true;
462 break;
465 if (!unregistering)
466 break;
467 __rtnl_unlock();
469 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
471 remove_wait_queue(&netdev_unregistering_wq, &wait);
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
478 void rtnl_link_unregister(struct rtnl_link_ops *ops)
480 /* Close the race with setup_net() and cleanup_net() */
481 down_write(&pernet_ops_rwsem);
482 rtnl_lock_unregistering_all();
483 __rtnl_link_unregister(ops);
484 rtnl_unlock();
485 up_write(&pernet_ops_rwsem);
487 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
489 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
491 struct net_device *master_dev;
492 const struct rtnl_link_ops *ops;
493 size_t size = 0;
495 rcu_read_lock();
497 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
498 if (!master_dev)
499 goto out;
501 ops = master_dev->rtnl_link_ops;
502 if (!ops || !ops->get_slave_size)
503 goto out;
504 /* IFLA_INFO_SLAVE_DATA + nested data */
505 size = nla_total_size(sizeof(struct nlattr)) +
506 ops->get_slave_size(master_dev, dev);
508 out:
509 rcu_read_unlock();
510 return size;
513 static size_t rtnl_link_get_size(const struct net_device *dev)
515 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
516 size_t size;
518 if (!ops)
519 return 0;
521 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
524 if (ops->get_size)
525 /* IFLA_INFO_DATA + nested data */
526 size += nla_total_size(sizeof(struct nlattr)) +
527 ops->get_size(dev);
529 if (ops->get_xstats_size)
530 /* IFLA_INFO_XSTATS */
531 size += nla_total_size(ops->get_xstats_size(dev));
533 size += rtnl_link_get_slave_info_data_size(dev);
535 return size;
538 static LIST_HEAD(rtnl_af_ops);
540 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
542 const struct rtnl_af_ops *ops;
544 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
545 if (ops->family == family)
546 return ops;
549 return NULL;
553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
556 * Returns 0 on success or a negative error code.
558 void rtnl_af_register(struct rtnl_af_ops *ops)
560 rtnl_lock();
561 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
562 rtnl_unlock();
564 EXPORT_SYMBOL_GPL(rtnl_af_register);
567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
570 void rtnl_af_unregister(struct rtnl_af_ops *ops)
572 rtnl_lock();
573 list_del_rcu(&ops->list);
574 rtnl_unlock();
576 synchronize_rcu();
578 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
580 static size_t rtnl_link_get_af_size(const struct net_device *dev,
581 u32 ext_filter_mask)
583 struct rtnl_af_ops *af_ops;
584 size_t size;
586 /* IFLA_AF_SPEC */
587 size = nla_total_size(sizeof(struct nlattr));
589 rcu_read_lock();
590 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
591 if (af_ops->get_link_af_size) {
592 /* AF_* + nested data */
593 size += nla_total_size(sizeof(struct nlattr)) +
594 af_ops->get_link_af_size(dev, ext_filter_mask);
597 rcu_read_unlock();
599 return size;
602 static bool rtnl_have_link_slave_info(const struct net_device *dev)
604 struct net_device *master_dev;
605 bool ret = false;
607 rcu_read_lock();
609 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
610 if (master_dev && master_dev->rtnl_link_ops)
611 ret = true;
612 rcu_read_unlock();
613 return ret;
616 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617 const struct net_device *dev)
619 struct net_device *master_dev;
620 const struct rtnl_link_ops *ops;
621 struct nlattr *slave_data;
622 int err;
624 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
625 if (!master_dev)
626 return 0;
627 ops = master_dev->rtnl_link_ops;
628 if (!ops)
629 return 0;
630 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
631 return -EMSGSIZE;
632 if (ops->fill_slave_info) {
633 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
634 if (!slave_data)
635 return -EMSGSIZE;
636 err = ops->fill_slave_info(skb, master_dev, dev);
637 if (err < 0)
638 goto err_cancel_slave_data;
639 nla_nest_end(skb, slave_data);
641 return 0;
643 err_cancel_slave_data:
644 nla_nest_cancel(skb, slave_data);
645 return err;
648 static int rtnl_link_info_fill(struct sk_buff *skb,
649 const struct net_device *dev)
651 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
652 struct nlattr *data;
653 int err;
655 if (!ops)
656 return 0;
657 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
658 return -EMSGSIZE;
659 if (ops->fill_xstats) {
660 err = ops->fill_xstats(skb, dev);
661 if (err < 0)
662 return err;
664 if (ops->fill_info) {
665 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
666 if (data == NULL)
667 return -EMSGSIZE;
668 err = ops->fill_info(skb, dev);
669 if (err < 0)
670 goto err_cancel_data;
671 nla_nest_end(skb, data);
673 return 0;
675 err_cancel_data:
676 nla_nest_cancel(skb, data);
677 return err;
680 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
682 struct nlattr *linkinfo;
683 int err = -EMSGSIZE;
685 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
686 if (linkinfo == NULL)
687 goto out;
689 err = rtnl_link_info_fill(skb, dev);
690 if (err < 0)
691 goto err_cancel_link;
693 err = rtnl_link_slave_info_fill(skb, dev);
694 if (err < 0)
695 goto err_cancel_link;
697 nla_nest_end(skb, linkinfo);
698 return 0;
700 err_cancel_link:
701 nla_nest_cancel(skb, linkinfo);
702 out:
703 return err;
706 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
708 struct sock *rtnl = net->rtnl;
709 int err = 0;
711 NETLINK_CB(skb).dst_group = group;
712 if (echo)
713 refcount_inc(&skb->users);
714 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
715 if (echo)
716 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
717 return err;
720 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
722 struct sock *rtnl = net->rtnl;
724 return nlmsg_unicast(rtnl, skb, pid);
726 EXPORT_SYMBOL(rtnl_unicast);
728 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
729 struct nlmsghdr *nlh, gfp_t flags)
731 struct sock *rtnl = net->rtnl;
732 int report = 0;
734 if (nlh)
735 report = nlmsg_report(nlh);
737 nlmsg_notify(rtnl, skb, pid, group, report, flags);
739 EXPORT_SYMBOL(rtnl_notify);
741 void rtnl_set_sk_err(struct net *net, u32 group, int error)
743 struct sock *rtnl = net->rtnl;
745 netlink_set_err(rtnl, 0, group, error);
747 EXPORT_SYMBOL(rtnl_set_sk_err);
749 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
751 struct nlattr *mx;
752 int i, valid = 0;
754 mx = nla_nest_start_noflag(skb, RTA_METRICS);
755 if (mx == NULL)
756 return -ENOBUFS;
758 for (i = 0; i < RTAX_MAX; i++) {
759 if (metrics[i]) {
760 if (i == RTAX_CC_ALGO - 1) {
761 char tmp[TCP_CA_NAME_MAX], *name;
763 name = tcp_ca_get_name_by_key(metrics[i], tmp);
764 if (!name)
765 continue;
766 if (nla_put_string(skb, i + 1, name))
767 goto nla_put_failure;
768 } else if (i == RTAX_FEATURES - 1) {
769 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
771 if (!user_features)
772 continue;
773 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
774 if (nla_put_u32(skb, i + 1, user_features))
775 goto nla_put_failure;
776 } else {
777 if (nla_put_u32(skb, i + 1, metrics[i]))
778 goto nla_put_failure;
780 valid++;
784 if (!valid) {
785 nla_nest_cancel(skb, mx);
786 return 0;
789 return nla_nest_end(skb, mx);
791 nla_put_failure:
792 nla_nest_cancel(skb, mx);
793 return -EMSGSIZE;
795 EXPORT_SYMBOL(rtnetlink_put_metrics);
797 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
798 long expires, u32 error)
800 struct rta_cacheinfo ci = {
801 .rta_error = error,
802 .rta_id = id,
805 if (dst) {
806 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
807 ci.rta_used = dst->__use;
808 ci.rta_clntref = atomic_read(&dst->__refcnt);
810 if (expires) {
811 unsigned long clock;
813 clock = jiffies_to_clock_t(abs(expires));
814 clock = min_t(unsigned long, clock, INT_MAX);
815 ci.rta_expires = (expires > 0) ? clock : -clock;
817 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
819 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
821 static void set_operstate(struct net_device *dev, unsigned char transition)
823 unsigned char operstate = dev->operstate;
825 switch (transition) {
826 case IF_OPER_UP:
827 if ((operstate == IF_OPER_DORMANT ||
828 operstate == IF_OPER_UNKNOWN) &&
829 !netif_dormant(dev))
830 operstate = IF_OPER_UP;
831 break;
833 case IF_OPER_DORMANT:
834 if (operstate == IF_OPER_UP ||
835 operstate == IF_OPER_UNKNOWN)
836 operstate = IF_OPER_DORMANT;
837 break;
840 if (dev->operstate != operstate) {
841 write_lock_bh(&dev_base_lock);
842 dev->operstate = operstate;
843 write_unlock_bh(&dev_base_lock);
844 netdev_state_change(dev);
848 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
850 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
851 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
854 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
855 const struct ifinfomsg *ifm)
857 unsigned int flags = ifm->ifi_flags;
859 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
860 if (ifm->ifi_change)
861 flags = (flags & ifm->ifi_change) |
862 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
864 return flags;
867 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
868 const struct rtnl_link_stats64 *b)
870 a->rx_packets = b->rx_packets;
871 a->tx_packets = b->tx_packets;
872 a->rx_bytes = b->rx_bytes;
873 a->tx_bytes = b->tx_bytes;
874 a->rx_errors = b->rx_errors;
875 a->tx_errors = b->tx_errors;
876 a->rx_dropped = b->rx_dropped;
877 a->tx_dropped = b->tx_dropped;
879 a->multicast = b->multicast;
880 a->collisions = b->collisions;
882 a->rx_length_errors = b->rx_length_errors;
883 a->rx_over_errors = b->rx_over_errors;
884 a->rx_crc_errors = b->rx_crc_errors;
885 a->rx_frame_errors = b->rx_frame_errors;
886 a->rx_fifo_errors = b->rx_fifo_errors;
887 a->rx_missed_errors = b->rx_missed_errors;
889 a->tx_aborted_errors = b->tx_aborted_errors;
890 a->tx_carrier_errors = b->tx_carrier_errors;
891 a->tx_fifo_errors = b->tx_fifo_errors;
892 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
893 a->tx_window_errors = b->tx_window_errors;
895 a->rx_compressed = b->rx_compressed;
896 a->tx_compressed = b->tx_compressed;
898 a->rx_nohandler = b->rx_nohandler;
901 /* All VF info */
902 static inline int rtnl_vfinfo_size(const struct net_device *dev,
903 u32 ext_filter_mask)
905 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
906 int num_vfs = dev_num_vf(dev->dev.parent);
907 size_t size = nla_total_size(0);
908 size += num_vfs *
909 (nla_total_size(0) +
910 nla_total_size(sizeof(struct ifla_vf_mac)) +
911 nla_total_size(sizeof(struct ifla_vf_vlan)) +
912 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
913 nla_total_size(MAX_VLAN_LIST_LEN *
914 sizeof(struct ifla_vf_vlan_info)) +
915 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
916 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
917 nla_total_size(sizeof(struct ifla_vf_rate)) +
918 nla_total_size(sizeof(struct ifla_vf_link_state)) +
919 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
920 nla_total_size(0) + /* nest IFLA_VF_STATS */
921 /* IFLA_VF_STATS_RX_PACKETS */
922 nla_total_size_64bit(sizeof(__u64)) +
923 /* IFLA_VF_STATS_TX_PACKETS */
924 nla_total_size_64bit(sizeof(__u64)) +
925 /* IFLA_VF_STATS_RX_BYTES */
926 nla_total_size_64bit(sizeof(__u64)) +
927 /* IFLA_VF_STATS_TX_BYTES */
928 nla_total_size_64bit(sizeof(__u64)) +
929 /* IFLA_VF_STATS_BROADCAST */
930 nla_total_size_64bit(sizeof(__u64)) +
931 /* IFLA_VF_STATS_MULTICAST */
932 nla_total_size_64bit(sizeof(__u64)) +
933 /* IFLA_VF_STATS_RX_DROPPED */
934 nla_total_size_64bit(sizeof(__u64)) +
935 /* IFLA_VF_STATS_TX_DROPPED */
936 nla_total_size_64bit(sizeof(__u64)) +
937 nla_total_size(sizeof(struct ifla_vf_trust)));
938 return size;
939 } else
940 return 0;
943 static size_t rtnl_port_size(const struct net_device *dev,
944 u32 ext_filter_mask)
946 size_t port_size = nla_total_size(4) /* PORT_VF */
947 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
948 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
949 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
950 + nla_total_size(1) /* PROT_VDP_REQUEST */
951 + nla_total_size(2); /* PORT_VDP_RESPONSE */
952 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
953 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
954 + port_size;
955 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
956 + port_size;
958 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
959 !(ext_filter_mask & RTEXT_FILTER_VF))
960 return 0;
961 if (dev_num_vf(dev->dev.parent))
962 return port_self_size + vf_ports_size +
963 vf_port_size * dev_num_vf(dev->dev.parent);
964 else
965 return port_self_size;
968 static size_t rtnl_xdp_size(void)
970 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
971 nla_total_size(1) + /* XDP_ATTACHED */
972 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
973 nla_total_size(4); /* XDP_<mode>_PROG_ID */
975 return xdp_size;
978 static noinline size_t if_nlmsg_size(const struct net_device *dev,
979 u32 ext_filter_mask)
981 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
982 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
983 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
984 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
985 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
986 + nla_total_size(sizeof(struct rtnl_link_stats))
987 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
988 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
989 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
990 + nla_total_size(4) /* IFLA_TXQLEN */
991 + nla_total_size(4) /* IFLA_WEIGHT */
992 + nla_total_size(4) /* IFLA_MTU */
993 + nla_total_size(4) /* IFLA_LINK */
994 + nla_total_size(4) /* IFLA_MASTER */
995 + nla_total_size(1) /* IFLA_CARRIER */
996 + nla_total_size(4) /* IFLA_PROMISCUITY */
997 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
998 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1000 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1001 + nla_total_size(1) /* IFLA_OPERSTATE */
1002 + nla_total_size(1) /* IFLA_LINKMODE */
1003 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1004 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1005 + nla_total_size(4) /* IFLA_GROUP */
1006 + nla_total_size(ext_filter_mask
1007 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1008 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1009 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1010 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1011 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1013 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1014 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1015 + rtnl_xdp_size() /* IFLA_XDP */
1016 + nla_total_size(4) /* IFLA_EVENT */
1017 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1018 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1019 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1020 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1021 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1022 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1023 + nla_total_size(4) /* IFLA_MIN_MTU */
1024 + nla_total_size(4) /* IFLA_MAX_MTU */
1025 + 0;
1028 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1030 struct nlattr *vf_ports;
1031 struct nlattr *vf_port;
1032 int vf;
1033 int err;
1035 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1036 if (!vf_ports)
1037 return -EMSGSIZE;
1039 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1040 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1041 if (!vf_port)
1042 goto nla_put_failure;
1043 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1044 goto nla_put_failure;
1045 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1046 if (err == -EMSGSIZE)
1047 goto nla_put_failure;
1048 if (err) {
1049 nla_nest_cancel(skb, vf_port);
1050 continue;
1052 nla_nest_end(skb, vf_port);
1055 nla_nest_end(skb, vf_ports);
1057 return 0;
1059 nla_put_failure:
1060 nla_nest_cancel(skb, vf_ports);
1061 return -EMSGSIZE;
1064 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1066 struct nlattr *port_self;
1067 int err;
1069 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1070 if (!port_self)
1071 return -EMSGSIZE;
1073 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1074 if (err) {
1075 nla_nest_cancel(skb, port_self);
1076 return (err == -EMSGSIZE) ? err : 0;
1079 nla_nest_end(skb, port_self);
1081 return 0;
1084 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1085 u32 ext_filter_mask)
1087 int err;
1089 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1090 !(ext_filter_mask & RTEXT_FILTER_VF))
1091 return 0;
1093 err = rtnl_port_self_fill(skb, dev);
1094 if (err)
1095 return err;
1097 if (dev_num_vf(dev->dev.parent)) {
1098 err = rtnl_vf_ports_fill(skb, dev);
1099 if (err)
1100 return err;
1103 return 0;
1106 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1108 int err;
1109 struct netdev_phys_item_id ppid;
1111 err = dev_get_phys_port_id(dev, &ppid);
1112 if (err) {
1113 if (err == -EOPNOTSUPP)
1114 return 0;
1115 return err;
1118 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1119 return -EMSGSIZE;
1121 return 0;
1124 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1126 char name[IFNAMSIZ];
1127 int err;
1129 err = dev_get_phys_port_name(dev, name, sizeof(name));
1130 if (err) {
1131 if (err == -EOPNOTSUPP)
1132 return 0;
1133 return err;
1136 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1137 return -EMSGSIZE;
1139 return 0;
1142 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1144 struct netdev_phys_item_id ppid = { };
1145 int err;
1147 err = dev_get_port_parent_id(dev, &ppid, false);
1148 if (err) {
1149 if (err == -EOPNOTSUPP)
1150 return 0;
1151 return err;
1154 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1155 return -EMSGSIZE;
1157 return 0;
1160 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1161 struct net_device *dev)
1163 struct rtnl_link_stats64 *sp;
1164 struct nlattr *attr;
1166 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1167 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1168 if (!attr)
1169 return -EMSGSIZE;
1171 sp = nla_data(attr);
1172 dev_get_stats(dev, sp);
1174 attr = nla_reserve(skb, IFLA_STATS,
1175 sizeof(struct rtnl_link_stats));
1176 if (!attr)
1177 return -EMSGSIZE;
1179 copy_rtnl_link_stats(nla_data(attr), sp);
1181 return 0;
1184 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1185 struct net_device *dev,
1186 int vfs_num,
1187 struct nlattr *vfinfo)
1189 struct ifla_vf_rss_query_en vf_rss_query_en;
1190 struct nlattr *vf, *vfstats, *vfvlanlist;
1191 struct ifla_vf_link_state vf_linkstate;
1192 struct ifla_vf_vlan_info vf_vlan_info;
1193 struct ifla_vf_spoofchk vf_spoofchk;
1194 struct ifla_vf_tx_rate vf_tx_rate;
1195 struct ifla_vf_stats vf_stats;
1196 struct ifla_vf_trust vf_trust;
1197 struct ifla_vf_vlan vf_vlan;
1198 struct ifla_vf_rate vf_rate;
1199 struct ifla_vf_mac vf_mac;
1200 struct ifla_vf_info ivi;
1202 memset(&ivi, 0, sizeof(ivi));
1204 /* Not all SR-IOV capable drivers support the
1205 * spoofcheck and "RSS query enable" query. Preset to
1206 * -1 so the user space tool can detect that the driver
1207 * didn't report anything.
1209 ivi.spoofchk = -1;
1210 ivi.rss_query_en = -1;
1211 ivi.trusted = -1;
1212 /* The default value for VF link state is "auto"
1213 * IFLA_VF_LINK_STATE_AUTO which equals zero
1215 ivi.linkstate = 0;
1216 /* VLAN Protocol by default is 802.1Q */
1217 ivi.vlan_proto = htons(ETH_P_8021Q);
1218 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1219 return 0;
1221 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1223 vf_mac.vf =
1224 vf_vlan.vf =
1225 vf_vlan_info.vf =
1226 vf_rate.vf =
1227 vf_tx_rate.vf =
1228 vf_spoofchk.vf =
1229 vf_linkstate.vf =
1230 vf_rss_query_en.vf =
1231 vf_trust.vf = ivi.vf;
1233 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1234 vf_vlan.vlan = ivi.vlan;
1235 vf_vlan.qos = ivi.qos;
1236 vf_vlan_info.vlan = ivi.vlan;
1237 vf_vlan_info.qos = ivi.qos;
1238 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1239 vf_tx_rate.rate = ivi.max_tx_rate;
1240 vf_rate.min_tx_rate = ivi.min_tx_rate;
1241 vf_rate.max_tx_rate = ivi.max_tx_rate;
1242 vf_spoofchk.setting = ivi.spoofchk;
1243 vf_linkstate.link_state = ivi.linkstate;
1244 vf_rss_query_en.setting = ivi.rss_query_en;
1245 vf_trust.setting = ivi.trusted;
1246 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1247 if (!vf)
1248 goto nla_put_vfinfo_failure;
1249 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1250 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1251 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1252 &vf_rate) ||
1253 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1254 &vf_tx_rate) ||
1255 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1256 &vf_spoofchk) ||
1257 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1258 &vf_linkstate) ||
1259 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1260 sizeof(vf_rss_query_en),
1261 &vf_rss_query_en) ||
1262 nla_put(skb, IFLA_VF_TRUST,
1263 sizeof(vf_trust), &vf_trust))
1264 goto nla_put_vf_failure;
1265 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1266 if (!vfvlanlist)
1267 goto nla_put_vf_failure;
1268 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1269 &vf_vlan_info)) {
1270 nla_nest_cancel(skb, vfvlanlist);
1271 goto nla_put_vf_failure;
1273 nla_nest_end(skb, vfvlanlist);
1274 memset(&vf_stats, 0, sizeof(vf_stats));
1275 if (dev->netdev_ops->ndo_get_vf_stats)
1276 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1277 &vf_stats);
1278 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1279 if (!vfstats)
1280 goto nla_put_vf_failure;
1281 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1282 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1283 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1284 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1285 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1286 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1288 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1290 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1292 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1294 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1296 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1297 nla_nest_cancel(skb, vfstats);
1298 goto nla_put_vf_failure;
1300 nla_nest_end(skb, vfstats);
1301 nla_nest_end(skb, vf);
1302 return 0;
1304 nla_put_vf_failure:
1305 nla_nest_cancel(skb, vf);
1306 nla_put_vfinfo_failure:
1307 nla_nest_cancel(skb, vfinfo);
1308 return -EMSGSIZE;
1311 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1312 struct net_device *dev,
1313 u32 ext_filter_mask)
1315 struct nlattr *vfinfo;
1316 int i, num_vfs;
1318 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1319 return 0;
1321 num_vfs = dev_num_vf(dev->dev.parent);
1322 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1323 return -EMSGSIZE;
1325 if (!dev->netdev_ops->ndo_get_vf_config)
1326 return 0;
1328 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1329 if (!vfinfo)
1330 return -EMSGSIZE;
1332 for (i = 0; i < num_vfs; i++) {
1333 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1334 return -EMSGSIZE;
1337 nla_nest_end(skb, vfinfo);
1338 return 0;
1341 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1343 struct rtnl_link_ifmap map;
1345 memset(&map, 0, sizeof(map));
1346 map.mem_start = dev->mem_start;
1347 map.mem_end = dev->mem_end;
1348 map.base_addr = dev->base_addr;
1349 map.irq = dev->irq;
1350 map.dma = dev->dma;
1351 map.port = dev->if_port;
1353 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1354 return -EMSGSIZE;
1356 return 0;
1359 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1361 const struct bpf_prog *generic_xdp_prog;
1363 ASSERT_RTNL();
1365 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1366 if (!generic_xdp_prog)
1367 return 0;
1368 return generic_xdp_prog->aux->id;
1371 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1373 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1376 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1378 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1379 XDP_QUERY_PROG_HW);
1382 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1383 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1384 u32 (*get_prog_id)(struct net_device *dev))
1386 u32 curr_id;
1387 int err;
1389 curr_id = get_prog_id(dev);
1390 if (!curr_id)
1391 return 0;
1393 *prog_id = curr_id;
1394 err = nla_put_u32(skb, attr, curr_id);
1395 if (err)
1396 return err;
1398 if (*mode != XDP_ATTACHED_NONE)
1399 *mode = XDP_ATTACHED_MULTI;
1400 else
1401 *mode = tgt_mode;
1403 return 0;
1406 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1408 struct nlattr *xdp;
1409 u32 prog_id;
1410 int err;
1411 u8 mode;
1413 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1414 if (!xdp)
1415 return -EMSGSIZE;
1417 prog_id = 0;
1418 mode = XDP_ATTACHED_NONE;
1419 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1420 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1421 if (err)
1422 goto err_cancel;
1423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1424 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1425 if (err)
1426 goto err_cancel;
1427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1428 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1429 if (err)
1430 goto err_cancel;
1432 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1433 if (err)
1434 goto err_cancel;
1436 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1437 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1438 if (err)
1439 goto err_cancel;
1442 nla_nest_end(skb, xdp);
1443 return 0;
1445 err_cancel:
1446 nla_nest_cancel(skb, xdp);
1447 return err;
1450 static u32 rtnl_get_event(unsigned long event)
1452 u32 rtnl_event_type = IFLA_EVENT_NONE;
1454 switch (event) {
1455 case NETDEV_REBOOT:
1456 rtnl_event_type = IFLA_EVENT_REBOOT;
1457 break;
1458 case NETDEV_FEAT_CHANGE:
1459 rtnl_event_type = IFLA_EVENT_FEATURES;
1460 break;
1461 case NETDEV_BONDING_FAILOVER:
1462 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1463 break;
1464 case NETDEV_NOTIFY_PEERS:
1465 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1466 break;
1467 case NETDEV_RESEND_IGMP:
1468 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1469 break;
1470 case NETDEV_CHANGEINFODATA:
1471 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1472 break;
1473 default:
1474 break;
1477 return rtnl_event_type;
1480 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1482 const struct net_device *upper_dev;
1483 int ret = 0;
1485 rcu_read_lock();
1487 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1488 if (upper_dev)
1489 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1491 rcu_read_unlock();
1492 return ret;
1495 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1496 bool force)
1498 int ifindex = dev_get_iflink(dev);
1500 if (force || dev->ifindex != ifindex)
1501 return nla_put_u32(skb, IFLA_LINK, ifindex);
1503 return 0;
1506 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1507 struct net_device *dev)
1509 char buf[IFALIASZ];
1510 int ret;
1512 ret = dev_get_alias(dev, buf, sizeof(buf));
1513 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1516 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1517 const struct net_device *dev,
1518 struct net *src_net)
1520 bool put_iflink = false;
1522 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1523 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1525 if (!net_eq(dev_net(dev), link_net)) {
1526 int id = peernet2id_alloc(src_net, link_net);
1528 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1529 return -EMSGSIZE;
1531 put_iflink = true;
1535 return nla_put_iflink(skb, dev, put_iflink);
1538 static int rtnl_fill_link_af(struct sk_buff *skb,
1539 const struct net_device *dev,
1540 u32 ext_filter_mask)
1542 const struct rtnl_af_ops *af_ops;
1543 struct nlattr *af_spec;
1545 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1546 if (!af_spec)
1547 return -EMSGSIZE;
1549 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1550 struct nlattr *af;
1551 int err;
1553 if (!af_ops->fill_link_af)
1554 continue;
1556 af = nla_nest_start_noflag(skb, af_ops->family);
1557 if (!af)
1558 return -EMSGSIZE;
1560 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1562 * Caller may return ENODATA to indicate that there
1563 * was no data to be dumped. This is not an error, it
1564 * means we should trim the attribute header and
1565 * continue.
1567 if (err == -ENODATA)
1568 nla_nest_cancel(skb, af);
1569 else if (err < 0)
1570 return -EMSGSIZE;
1572 nla_nest_end(skb, af);
1575 nla_nest_end(skb, af_spec);
1576 return 0;
1579 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1580 struct net_device *dev, struct net *src_net,
1581 int type, u32 pid, u32 seq, u32 change,
1582 unsigned int flags, u32 ext_filter_mask,
1583 u32 event, int *new_nsid, int new_ifindex,
1584 int tgt_netnsid)
1586 struct ifinfomsg *ifm;
1587 struct nlmsghdr *nlh;
1589 ASSERT_RTNL();
1590 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1591 if (nlh == NULL)
1592 return -EMSGSIZE;
1594 ifm = nlmsg_data(nlh);
1595 ifm->ifi_family = AF_UNSPEC;
1596 ifm->__ifi_pad = 0;
1597 ifm->ifi_type = dev->type;
1598 ifm->ifi_index = dev->ifindex;
1599 ifm->ifi_flags = dev_get_flags(dev);
1600 ifm->ifi_change = change;
1602 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1603 goto nla_put_failure;
1605 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1606 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1607 nla_put_u8(skb, IFLA_OPERSTATE,
1608 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1609 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1610 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1611 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1612 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1613 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1614 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1615 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1616 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1617 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1618 #ifdef CONFIG_RPS
1619 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1620 #endif
1621 put_master_ifindex(skb, dev) ||
1622 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1623 (dev->qdisc &&
1624 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1625 nla_put_ifalias(skb, dev) ||
1626 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1627 atomic_read(&dev->carrier_up_count) +
1628 atomic_read(&dev->carrier_down_count)) ||
1629 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1630 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1631 atomic_read(&dev->carrier_up_count)) ||
1632 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1633 atomic_read(&dev->carrier_down_count)))
1634 goto nla_put_failure;
1636 if (event != IFLA_EVENT_NONE) {
1637 if (nla_put_u32(skb, IFLA_EVENT, event))
1638 goto nla_put_failure;
1641 if (rtnl_fill_link_ifmap(skb, dev))
1642 goto nla_put_failure;
1644 if (dev->addr_len) {
1645 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1646 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1647 goto nla_put_failure;
1650 if (rtnl_phys_port_id_fill(skb, dev))
1651 goto nla_put_failure;
1653 if (rtnl_phys_port_name_fill(skb, dev))
1654 goto nla_put_failure;
1656 if (rtnl_phys_switch_id_fill(skb, dev))
1657 goto nla_put_failure;
1659 if (rtnl_fill_stats(skb, dev))
1660 goto nla_put_failure;
1662 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1663 goto nla_put_failure;
1665 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1666 goto nla_put_failure;
1668 if (rtnl_xdp_fill(skb, dev))
1669 goto nla_put_failure;
1671 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1672 if (rtnl_link_fill(skb, dev) < 0)
1673 goto nla_put_failure;
1676 if (rtnl_fill_link_netnsid(skb, dev, src_net))
1677 goto nla_put_failure;
1679 if (new_nsid &&
1680 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1681 goto nla_put_failure;
1682 if (new_ifindex &&
1683 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1684 goto nla_put_failure;
1687 rcu_read_lock();
1688 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1689 goto nla_put_failure_rcu;
1690 rcu_read_unlock();
1692 nlmsg_end(skb, nlh);
1693 return 0;
1695 nla_put_failure_rcu:
1696 rcu_read_unlock();
1697 nla_put_failure:
1698 nlmsg_cancel(skb, nlh);
1699 return -EMSGSIZE;
1702 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1703 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1704 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1705 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1706 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1707 [IFLA_MTU] = { .type = NLA_U32 },
1708 [IFLA_LINK] = { .type = NLA_U32 },
1709 [IFLA_MASTER] = { .type = NLA_U32 },
1710 [IFLA_CARRIER] = { .type = NLA_U8 },
1711 [IFLA_TXQLEN] = { .type = NLA_U32 },
1712 [IFLA_WEIGHT] = { .type = NLA_U32 },
1713 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1714 [IFLA_LINKMODE] = { .type = NLA_U8 },
1715 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1716 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1717 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1718 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1719 * allow 0-length string (needed to remove an alias).
1721 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1722 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1723 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1724 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1725 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1726 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1727 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1728 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1729 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1730 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1731 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1732 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1733 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1734 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1735 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1736 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1737 [IFLA_XDP] = { .type = NLA_NESTED },
1738 [IFLA_EVENT] = { .type = NLA_U32 },
1739 [IFLA_GROUP] = { .type = NLA_U32 },
1740 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1741 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1742 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1743 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1744 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1747 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1748 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1749 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1750 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1751 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1754 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1755 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1756 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1757 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1758 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1759 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1760 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1761 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1762 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1763 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1764 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1765 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1766 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1769 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1770 [IFLA_PORT_VF] = { .type = NLA_U32 },
1771 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1772 .len = PORT_PROFILE_MAX },
1773 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1774 .len = PORT_UUID_MAX },
1775 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1776 .len = PORT_UUID_MAX },
1777 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1778 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1780 /* Unused, but we need to keep it here since user space could
1781 * fill it. It's also broken with regard to NLA_BINARY use in
1782 * combination with structs.
1784 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1785 .len = sizeof(struct ifla_port_vsi) },
1788 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1789 [IFLA_XDP_FD] = { .type = NLA_S32 },
1790 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1791 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1792 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1795 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1797 const struct rtnl_link_ops *ops = NULL;
1798 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1800 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1801 return NULL;
1803 if (linfo[IFLA_INFO_KIND]) {
1804 char kind[MODULE_NAME_LEN];
1806 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1807 ops = rtnl_link_ops_get(kind);
1810 return ops;
1813 static bool link_master_filtered(struct net_device *dev, int master_idx)
1815 struct net_device *master;
1817 if (!master_idx)
1818 return false;
1820 master = netdev_master_upper_dev_get(dev);
1821 if (!master || master->ifindex != master_idx)
1822 return true;
1824 return false;
1827 static bool link_kind_filtered(const struct net_device *dev,
1828 const struct rtnl_link_ops *kind_ops)
1830 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1831 return true;
1833 return false;
1836 static bool link_dump_filtered(struct net_device *dev,
1837 int master_idx,
1838 const struct rtnl_link_ops *kind_ops)
1840 if (link_master_filtered(dev, master_idx) ||
1841 link_kind_filtered(dev, kind_ops))
1842 return true;
1844 return false;
1848 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1849 * @sk: netlink socket
1850 * @netnsid: network namespace identifier
1852 * Returns the network namespace identified by netnsid on success or an error
1853 * pointer on failure.
1855 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1857 struct net *net;
1859 net = get_net_ns_by_id(sock_net(sk), netnsid);
1860 if (!net)
1861 return ERR_PTR(-EINVAL);
1863 /* For now, the caller is required to have CAP_NET_ADMIN in
1864 * the user namespace owning the target net ns.
1866 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1867 put_net(net);
1868 return ERR_PTR(-EACCES);
1870 return net;
1872 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1874 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1875 bool strict_check, struct nlattr **tb,
1876 struct netlink_ext_ack *extack)
1878 int hdrlen;
1880 if (strict_check) {
1881 struct ifinfomsg *ifm;
1883 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1884 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1885 return -EINVAL;
1888 ifm = nlmsg_data(nlh);
1889 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1890 ifm->ifi_change) {
1891 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1892 return -EINVAL;
1894 if (ifm->ifi_index) {
1895 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1896 return -EINVAL;
1899 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1900 IFLA_MAX, ifla_policy,
1901 extack);
1904 /* A hack to preserve kernel<->userspace interface.
1905 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1906 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1907 * what iproute2 < v3.9.0 used.
1908 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1909 * attribute, its netlink message is shorter than struct ifinfomsg.
1911 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1912 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1914 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
1915 extack);
1918 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1920 struct netlink_ext_ack *extack = cb->extack;
1921 const struct nlmsghdr *nlh = cb->nlh;
1922 struct net *net = sock_net(skb->sk);
1923 struct net *tgt_net = net;
1924 int h, s_h;
1925 int idx = 0, s_idx;
1926 struct net_device *dev;
1927 struct hlist_head *head;
1928 struct nlattr *tb[IFLA_MAX+1];
1929 u32 ext_filter_mask = 0;
1930 const struct rtnl_link_ops *kind_ops = NULL;
1931 unsigned int flags = NLM_F_MULTI;
1932 int master_idx = 0;
1933 int netnsid = -1;
1934 int err, i;
1936 s_h = cb->args[0];
1937 s_idx = cb->args[1];
1939 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1940 if (err < 0) {
1941 if (cb->strict_check)
1942 return err;
1944 goto walk_entries;
1947 for (i = 0; i <= IFLA_MAX; ++i) {
1948 if (!tb[i])
1949 continue;
1951 /* new attributes should only be added with strict checking */
1952 switch (i) {
1953 case IFLA_TARGET_NETNSID:
1954 netnsid = nla_get_s32(tb[i]);
1955 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1956 if (IS_ERR(tgt_net)) {
1957 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1958 return PTR_ERR(tgt_net);
1960 break;
1961 case IFLA_EXT_MASK:
1962 ext_filter_mask = nla_get_u32(tb[i]);
1963 break;
1964 case IFLA_MASTER:
1965 master_idx = nla_get_u32(tb[i]);
1966 break;
1967 case IFLA_LINKINFO:
1968 kind_ops = linkinfo_to_kind_ops(tb[i]);
1969 break;
1970 default:
1971 if (cb->strict_check) {
1972 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1973 return -EINVAL;
1978 if (master_idx || kind_ops)
1979 flags |= NLM_F_DUMP_FILTERED;
1981 walk_entries:
1982 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1983 idx = 0;
1984 head = &tgt_net->dev_index_head[h];
1985 hlist_for_each_entry(dev, head, index_hlist) {
1986 if (link_dump_filtered(dev, master_idx, kind_ops))
1987 goto cont;
1988 if (idx < s_idx)
1989 goto cont;
1990 err = rtnl_fill_ifinfo(skb, dev, net,
1991 RTM_NEWLINK,
1992 NETLINK_CB(cb->skb).portid,
1993 nlh->nlmsg_seq, 0, flags,
1994 ext_filter_mask, 0, NULL, 0,
1995 netnsid);
1997 if (err < 0) {
1998 if (likely(skb->len))
1999 goto out;
2001 goto out_err;
2003 cont:
2004 idx++;
2007 out:
2008 err = skb->len;
2009 out_err:
2010 cb->args[1] = idx;
2011 cb->args[0] = h;
2012 cb->seq = net->dev_base_seq;
2013 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2014 if (netnsid >= 0)
2015 put_net(tgt_net);
2017 return err;
2020 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2021 struct netlink_ext_ack *exterr)
2023 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2024 exterr);
2026 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2028 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2030 struct net *net;
2031 /* Examine the link attributes and figure out which
2032 * network namespace we are talking about.
2034 if (tb[IFLA_NET_NS_PID])
2035 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2036 else if (tb[IFLA_NET_NS_FD])
2037 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2038 else
2039 net = get_net(src_net);
2040 return net;
2042 EXPORT_SYMBOL(rtnl_link_get_net);
2044 /* Figure out which network namespace we are talking about by
2045 * examining the link attributes in the following order:
2047 * 1. IFLA_NET_NS_PID
2048 * 2. IFLA_NET_NS_FD
2049 * 3. IFLA_TARGET_NETNSID
2051 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2052 struct nlattr *tb[])
2054 struct net *net;
2056 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2057 return rtnl_link_get_net(src_net, tb);
2059 if (!tb[IFLA_TARGET_NETNSID])
2060 return get_net(src_net);
2062 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2063 if (!net)
2064 return ERR_PTR(-EINVAL);
2066 return net;
2069 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2070 struct net *src_net,
2071 struct nlattr *tb[], int cap)
2073 struct net *net;
2075 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2076 if (IS_ERR(net))
2077 return net;
2079 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2080 put_net(net);
2081 return ERR_PTR(-EPERM);
2084 return net;
2087 /* Verify that rtnetlink requests do not pass additional properties
2088 * potentially referring to different network namespaces.
2090 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2091 struct netlink_ext_ack *extack,
2092 bool netns_id_only)
2095 if (netns_id_only) {
2096 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2097 return 0;
2099 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2100 return -EOPNOTSUPP;
2103 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2104 goto invalid_attr;
2106 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2107 goto invalid_attr;
2109 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2110 goto invalid_attr;
2112 return 0;
2114 invalid_attr:
2115 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2116 return -EINVAL;
2119 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2121 if (dev) {
2122 if (tb[IFLA_ADDRESS] &&
2123 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2124 return -EINVAL;
2126 if (tb[IFLA_BROADCAST] &&
2127 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2128 return -EINVAL;
2131 if (tb[IFLA_AF_SPEC]) {
2132 struct nlattr *af;
2133 int rem, err;
2135 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2136 const struct rtnl_af_ops *af_ops;
2138 rcu_read_lock();
2139 af_ops = rtnl_af_lookup(nla_type(af));
2140 if (!af_ops) {
2141 rcu_read_unlock();
2142 return -EAFNOSUPPORT;
2145 if (!af_ops->set_link_af) {
2146 rcu_read_unlock();
2147 return -EOPNOTSUPP;
2150 if (af_ops->validate_link_af) {
2151 err = af_ops->validate_link_af(dev, af);
2152 if (err < 0) {
2153 rcu_read_unlock();
2154 return err;
2158 rcu_read_unlock();
2162 return 0;
2165 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2166 int guid_type)
2168 const struct net_device_ops *ops = dev->netdev_ops;
2170 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2173 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2175 if (dev->type != ARPHRD_INFINIBAND)
2176 return -EOPNOTSUPP;
2178 return handle_infiniband_guid(dev, ivt, guid_type);
2181 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2183 const struct net_device_ops *ops = dev->netdev_ops;
2184 int err = -EINVAL;
2186 if (tb[IFLA_VF_MAC]) {
2187 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2189 err = -EOPNOTSUPP;
2190 if (ops->ndo_set_vf_mac)
2191 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2192 ivm->mac);
2193 if (err < 0)
2194 return err;
2197 if (tb[IFLA_VF_VLAN]) {
2198 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2200 err = -EOPNOTSUPP;
2201 if (ops->ndo_set_vf_vlan)
2202 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2203 ivv->qos,
2204 htons(ETH_P_8021Q));
2205 if (err < 0)
2206 return err;
2209 if (tb[IFLA_VF_VLAN_LIST]) {
2210 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2211 struct nlattr *attr;
2212 int rem, len = 0;
2214 err = -EOPNOTSUPP;
2215 if (!ops->ndo_set_vf_vlan)
2216 return err;
2218 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2219 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2220 nla_len(attr) < NLA_HDRLEN) {
2221 return -EINVAL;
2223 if (len >= MAX_VLAN_LIST_LEN)
2224 return -EOPNOTSUPP;
2225 ivvl[len] = nla_data(attr);
2227 len++;
2229 if (len == 0)
2230 return -EINVAL;
2232 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2233 ivvl[0]->qos, ivvl[0]->vlan_proto);
2234 if (err < 0)
2235 return err;
2238 if (tb[IFLA_VF_TX_RATE]) {
2239 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2240 struct ifla_vf_info ivf;
2242 err = -EOPNOTSUPP;
2243 if (ops->ndo_get_vf_config)
2244 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2245 if (err < 0)
2246 return err;
2248 err = -EOPNOTSUPP;
2249 if (ops->ndo_set_vf_rate)
2250 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2251 ivf.min_tx_rate,
2252 ivt->rate);
2253 if (err < 0)
2254 return err;
2257 if (tb[IFLA_VF_RATE]) {
2258 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2260 err = -EOPNOTSUPP;
2261 if (ops->ndo_set_vf_rate)
2262 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2263 ivt->min_tx_rate,
2264 ivt->max_tx_rate);
2265 if (err < 0)
2266 return err;
2269 if (tb[IFLA_VF_SPOOFCHK]) {
2270 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2272 err = -EOPNOTSUPP;
2273 if (ops->ndo_set_vf_spoofchk)
2274 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2275 ivs->setting);
2276 if (err < 0)
2277 return err;
2280 if (tb[IFLA_VF_LINK_STATE]) {
2281 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2283 err = -EOPNOTSUPP;
2284 if (ops->ndo_set_vf_link_state)
2285 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2286 ivl->link_state);
2287 if (err < 0)
2288 return err;
2291 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2292 struct ifla_vf_rss_query_en *ivrssq_en;
2294 err = -EOPNOTSUPP;
2295 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2296 if (ops->ndo_set_vf_rss_query_en)
2297 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2298 ivrssq_en->setting);
2299 if (err < 0)
2300 return err;
2303 if (tb[IFLA_VF_TRUST]) {
2304 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2306 err = -EOPNOTSUPP;
2307 if (ops->ndo_set_vf_trust)
2308 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2309 if (err < 0)
2310 return err;
2313 if (tb[IFLA_VF_IB_NODE_GUID]) {
2314 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2316 if (!ops->ndo_set_vf_guid)
2317 return -EOPNOTSUPP;
2319 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2322 if (tb[IFLA_VF_IB_PORT_GUID]) {
2323 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2325 if (!ops->ndo_set_vf_guid)
2326 return -EOPNOTSUPP;
2328 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2331 return err;
2334 static int do_set_master(struct net_device *dev, int ifindex,
2335 struct netlink_ext_ack *extack)
2337 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2338 const struct net_device_ops *ops;
2339 int err;
2341 if (upper_dev) {
2342 if (upper_dev->ifindex == ifindex)
2343 return 0;
2344 ops = upper_dev->netdev_ops;
2345 if (ops->ndo_del_slave) {
2346 err = ops->ndo_del_slave(upper_dev, dev);
2347 if (err)
2348 return err;
2349 } else {
2350 return -EOPNOTSUPP;
2354 if (ifindex) {
2355 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2356 if (!upper_dev)
2357 return -EINVAL;
2358 ops = upper_dev->netdev_ops;
2359 if (ops->ndo_add_slave) {
2360 err = ops->ndo_add_slave(upper_dev, dev, extack);
2361 if (err)
2362 return err;
2363 } else {
2364 return -EOPNOTSUPP;
2367 return 0;
2370 #define DO_SETLINK_MODIFIED 0x01
2371 /* notify flag means notify + modified. */
2372 #define DO_SETLINK_NOTIFY 0x03
2373 static int do_setlink(const struct sk_buff *skb,
2374 struct net_device *dev, struct ifinfomsg *ifm,
2375 struct netlink_ext_ack *extack,
2376 struct nlattr **tb, char *ifname, int status)
2378 const struct net_device_ops *ops = dev->netdev_ops;
2379 int err;
2381 err = validate_linkmsg(dev, tb);
2382 if (err < 0)
2383 return err;
2385 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2386 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2387 tb, CAP_NET_ADMIN);
2388 if (IS_ERR(net)) {
2389 err = PTR_ERR(net);
2390 goto errout;
2393 err = dev_change_net_namespace(dev, net, ifname);
2394 put_net(net);
2395 if (err)
2396 goto errout;
2397 status |= DO_SETLINK_MODIFIED;
2400 if (tb[IFLA_MAP]) {
2401 struct rtnl_link_ifmap *u_map;
2402 struct ifmap k_map;
2404 if (!ops->ndo_set_config) {
2405 err = -EOPNOTSUPP;
2406 goto errout;
2409 if (!netif_device_present(dev)) {
2410 err = -ENODEV;
2411 goto errout;
2414 u_map = nla_data(tb[IFLA_MAP]);
2415 k_map.mem_start = (unsigned long) u_map->mem_start;
2416 k_map.mem_end = (unsigned long) u_map->mem_end;
2417 k_map.base_addr = (unsigned short) u_map->base_addr;
2418 k_map.irq = (unsigned char) u_map->irq;
2419 k_map.dma = (unsigned char) u_map->dma;
2420 k_map.port = (unsigned char) u_map->port;
2422 err = ops->ndo_set_config(dev, &k_map);
2423 if (err < 0)
2424 goto errout;
2426 status |= DO_SETLINK_NOTIFY;
2429 if (tb[IFLA_ADDRESS]) {
2430 struct sockaddr *sa;
2431 int len;
2433 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2434 sizeof(*sa));
2435 sa = kmalloc(len, GFP_KERNEL);
2436 if (!sa) {
2437 err = -ENOMEM;
2438 goto errout;
2440 sa->sa_family = dev->type;
2441 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2442 dev->addr_len);
2443 err = dev_set_mac_address(dev, sa, extack);
2444 kfree(sa);
2445 if (err)
2446 goto errout;
2447 status |= DO_SETLINK_MODIFIED;
2450 if (tb[IFLA_MTU]) {
2451 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2452 if (err < 0)
2453 goto errout;
2454 status |= DO_SETLINK_MODIFIED;
2457 if (tb[IFLA_GROUP]) {
2458 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2459 status |= DO_SETLINK_NOTIFY;
2463 * Interface selected by interface index but interface
2464 * name provided implies that a name change has been
2465 * requested.
2467 if (ifm->ifi_index > 0 && ifname[0]) {
2468 err = dev_change_name(dev, ifname);
2469 if (err < 0)
2470 goto errout;
2471 status |= DO_SETLINK_MODIFIED;
2474 if (tb[IFLA_IFALIAS]) {
2475 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2476 nla_len(tb[IFLA_IFALIAS]));
2477 if (err < 0)
2478 goto errout;
2479 status |= DO_SETLINK_NOTIFY;
2482 if (tb[IFLA_BROADCAST]) {
2483 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2484 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2487 if (ifm->ifi_flags || ifm->ifi_change) {
2488 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2489 extack);
2490 if (err < 0)
2491 goto errout;
2494 if (tb[IFLA_MASTER]) {
2495 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2496 if (err)
2497 goto errout;
2498 status |= DO_SETLINK_MODIFIED;
2501 if (tb[IFLA_CARRIER]) {
2502 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2503 if (err)
2504 goto errout;
2505 status |= DO_SETLINK_MODIFIED;
2508 if (tb[IFLA_TXQLEN]) {
2509 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2511 err = dev_change_tx_queue_len(dev, value);
2512 if (err)
2513 goto errout;
2514 status |= DO_SETLINK_MODIFIED;
2517 if (tb[IFLA_GSO_MAX_SIZE]) {
2518 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2520 if (max_size > GSO_MAX_SIZE) {
2521 err = -EINVAL;
2522 goto errout;
2525 if (dev->gso_max_size ^ max_size) {
2526 netif_set_gso_max_size(dev, max_size);
2527 status |= DO_SETLINK_MODIFIED;
2531 if (tb[IFLA_GSO_MAX_SEGS]) {
2532 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2534 if (max_segs > GSO_MAX_SEGS) {
2535 err = -EINVAL;
2536 goto errout;
2539 if (dev->gso_max_segs ^ max_segs) {
2540 dev->gso_max_segs = max_segs;
2541 status |= DO_SETLINK_MODIFIED;
2545 if (tb[IFLA_OPERSTATE])
2546 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2548 if (tb[IFLA_LINKMODE]) {
2549 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2551 write_lock_bh(&dev_base_lock);
2552 if (dev->link_mode ^ value)
2553 status |= DO_SETLINK_NOTIFY;
2554 dev->link_mode = value;
2555 write_unlock_bh(&dev_base_lock);
2558 if (tb[IFLA_VFINFO_LIST]) {
2559 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2560 struct nlattr *attr;
2561 int rem;
2563 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2564 if (nla_type(attr) != IFLA_VF_INFO ||
2565 nla_len(attr) < NLA_HDRLEN) {
2566 err = -EINVAL;
2567 goto errout;
2569 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2570 attr,
2571 ifla_vf_policy,
2572 NULL);
2573 if (err < 0)
2574 goto errout;
2575 err = do_setvfinfo(dev, vfinfo);
2576 if (err < 0)
2577 goto errout;
2578 status |= DO_SETLINK_NOTIFY;
2581 err = 0;
2583 if (tb[IFLA_VF_PORTS]) {
2584 struct nlattr *port[IFLA_PORT_MAX+1];
2585 struct nlattr *attr;
2586 int vf;
2587 int rem;
2589 err = -EOPNOTSUPP;
2590 if (!ops->ndo_set_vf_port)
2591 goto errout;
2593 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2594 if (nla_type(attr) != IFLA_VF_PORT ||
2595 nla_len(attr) < NLA_HDRLEN) {
2596 err = -EINVAL;
2597 goto errout;
2599 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2600 attr,
2601 ifla_port_policy,
2602 NULL);
2603 if (err < 0)
2604 goto errout;
2605 if (!port[IFLA_PORT_VF]) {
2606 err = -EOPNOTSUPP;
2607 goto errout;
2609 vf = nla_get_u32(port[IFLA_PORT_VF]);
2610 err = ops->ndo_set_vf_port(dev, vf, port);
2611 if (err < 0)
2612 goto errout;
2613 status |= DO_SETLINK_NOTIFY;
2616 err = 0;
2618 if (tb[IFLA_PORT_SELF]) {
2619 struct nlattr *port[IFLA_PORT_MAX+1];
2621 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2622 tb[IFLA_PORT_SELF],
2623 ifla_port_policy, NULL);
2624 if (err < 0)
2625 goto errout;
2627 err = -EOPNOTSUPP;
2628 if (ops->ndo_set_vf_port)
2629 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2630 if (err < 0)
2631 goto errout;
2632 status |= DO_SETLINK_NOTIFY;
2635 if (tb[IFLA_AF_SPEC]) {
2636 struct nlattr *af;
2637 int rem;
2639 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2640 const struct rtnl_af_ops *af_ops;
2642 rcu_read_lock();
2644 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2646 err = af_ops->set_link_af(dev, af);
2647 if (err < 0) {
2648 rcu_read_unlock();
2649 goto errout;
2652 rcu_read_unlock();
2653 status |= DO_SETLINK_NOTIFY;
2656 err = 0;
2658 if (tb[IFLA_PROTO_DOWN]) {
2659 err = dev_change_proto_down(dev,
2660 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2661 if (err)
2662 goto errout;
2663 status |= DO_SETLINK_NOTIFY;
2666 if (tb[IFLA_XDP]) {
2667 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2668 u32 xdp_flags = 0;
2670 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2671 tb[IFLA_XDP],
2672 ifla_xdp_policy, NULL);
2673 if (err < 0)
2674 goto errout;
2676 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2677 err = -EINVAL;
2678 goto errout;
2681 if (xdp[IFLA_XDP_FLAGS]) {
2682 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2683 if (xdp_flags & ~XDP_FLAGS_MASK) {
2684 err = -EINVAL;
2685 goto errout;
2687 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2688 err = -EINVAL;
2689 goto errout;
2693 if (xdp[IFLA_XDP_FD]) {
2694 err = dev_change_xdp_fd(dev, extack,
2695 nla_get_s32(xdp[IFLA_XDP_FD]),
2696 xdp_flags);
2697 if (err)
2698 goto errout;
2699 status |= DO_SETLINK_NOTIFY;
2703 errout:
2704 if (status & DO_SETLINK_MODIFIED) {
2705 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2706 netdev_state_change(dev);
2708 if (err < 0)
2709 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",
2710 dev->name);
2713 return err;
2716 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2717 struct netlink_ext_ack *extack)
2719 struct net *net = sock_net(skb->sk);
2720 struct ifinfomsg *ifm;
2721 struct net_device *dev;
2722 int err;
2723 struct nlattr *tb[IFLA_MAX+1];
2724 char ifname[IFNAMSIZ];
2726 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2727 ifla_policy, extack);
2728 if (err < 0)
2729 goto errout;
2731 err = rtnl_ensure_unique_netns(tb, extack, false);
2732 if (err < 0)
2733 goto errout;
2735 if (tb[IFLA_IFNAME])
2736 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2737 else
2738 ifname[0] = '\0';
2740 err = -EINVAL;
2741 ifm = nlmsg_data(nlh);
2742 if (ifm->ifi_index > 0)
2743 dev = __dev_get_by_index(net, ifm->ifi_index);
2744 else if (tb[IFLA_IFNAME])
2745 dev = __dev_get_by_name(net, ifname);
2746 else
2747 goto errout;
2749 if (dev == NULL) {
2750 err = -ENODEV;
2751 goto errout;
2754 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2755 errout:
2756 return err;
2759 static int rtnl_group_dellink(const struct net *net, int group)
2761 struct net_device *dev, *aux;
2762 LIST_HEAD(list_kill);
2763 bool found = false;
2765 if (!group)
2766 return -EPERM;
2768 for_each_netdev(net, dev) {
2769 if (dev->group == group) {
2770 const struct rtnl_link_ops *ops;
2772 found = true;
2773 ops = dev->rtnl_link_ops;
2774 if (!ops || !ops->dellink)
2775 return -EOPNOTSUPP;
2779 if (!found)
2780 return -ENODEV;
2782 for_each_netdev_safe(net, dev, aux) {
2783 if (dev->group == group) {
2784 const struct rtnl_link_ops *ops;
2786 ops = dev->rtnl_link_ops;
2787 ops->dellink(dev, &list_kill);
2790 unregister_netdevice_many(&list_kill);
2792 return 0;
2795 int rtnl_delete_link(struct net_device *dev)
2797 const struct rtnl_link_ops *ops;
2798 LIST_HEAD(list_kill);
2800 ops = dev->rtnl_link_ops;
2801 if (!ops || !ops->dellink)
2802 return -EOPNOTSUPP;
2804 ops->dellink(dev, &list_kill);
2805 unregister_netdevice_many(&list_kill);
2807 return 0;
2809 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2811 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2812 struct netlink_ext_ack *extack)
2814 struct net *net = sock_net(skb->sk);
2815 struct net *tgt_net = net;
2816 struct net_device *dev = NULL;
2817 struct ifinfomsg *ifm;
2818 char ifname[IFNAMSIZ];
2819 struct nlattr *tb[IFLA_MAX+1];
2820 int err;
2821 int netnsid = -1;
2823 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2824 ifla_policy, extack);
2825 if (err < 0)
2826 return err;
2828 err = rtnl_ensure_unique_netns(tb, extack, true);
2829 if (err < 0)
2830 return err;
2832 if (tb[IFLA_IFNAME])
2833 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2835 if (tb[IFLA_TARGET_NETNSID]) {
2836 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2837 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2838 if (IS_ERR(tgt_net))
2839 return PTR_ERR(tgt_net);
2842 err = -EINVAL;
2843 ifm = nlmsg_data(nlh);
2844 if (ifm->ifi_index > 0)
2845 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2846 else if (tb[IFLA_IFNAME])
2847 dev = __dev_get_by_name(tgt_net, ifname);
2848 else if (tb[IFLA_GROUP])
2849 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2850 else
2851 goto out;
2853 if (!dev) {
2854 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2855 err = -ENODEV;
2857 goto out;
2860 err = rtnl_delete_link(dev);
2862 out:
2863 if (netnsid >= 0)
2864 put_net(tgt_net);
2866 return err;
2869 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2871 unsigned int old_flags;
2872 int err;
2874 old_flags = dev->flags;
2875 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2876 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2877 NULL);
2878 if (err < 0)
2879 return err;
2882 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2883 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2884 } else {
2885 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2886 __dev_notify_flags(dev, old_flags, ~0U);
2888 return 0;
2890 EXPORT_SYMBOL(rtnl_configure_link);
2892 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2893 unsigned char name_assign_type,
2894 const struct rtnl_link_ops *ops,
2895 struct nlattr *tb[],
2896 struct netlink_ext_ack *extack)
2898 struct net_device *dev;
2899 unsigned int num_tx_queues = 1;
2900 unsigned int num_rx_queues = 1;
2902 if (tb[IFLA_NUM_TX_QUEUES])
2903 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2904 else if (ops->get_num_tx_queues)
2905 num_tx_queues = ops->get_num_tx_queues();
2907 if (tb[IFLA_NUM_RX_QUEUES])
2908 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2909 else if (ops->get_num_rx_queues)
2910 num_rx_queues = ops->get_num_rx_queues();
2912 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2913 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
2914 return ERR_PTR(-EINVAL);
2917 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2918 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
2919 return ERR_PTR(-EINVAL);
2922 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2923 ops->setup, num_tx_queues, num_rx_queues);
2924 if (!dev)
2925 return ERR_PTR(-ENOMEM);
2927 dev_net_set(dev, net);
2928 dev->rtnl_link_ops = ops;
2929 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2931 if (tb[IFLA_MTU])
2932 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2933 if (tb[IFLA_ADDRESS]) {
2934 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2935 nla_len(tb[IFLA_ADDRESS]));
2936 dev->addr_assign_type = NET_ADDR_SET;
2938 if (tb[IFLA_BROADCAST])
2939 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2940 nla_len(tb[IFLA_BROADCAST]));
2941 if (tb[IFLA_TXQLEN])
2942 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2943 if (tb[IFLA_OPERSTATE])
2944 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2945 if (tb[IFLA_LINKMODE])
2946 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2947 if (tb[IFLA_GROUP])
2948 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2949 if (tb[IFLA_GSO_MAX_SIZE])
2950 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2951 if (tb[IFLA_GSO_MAX_SEGS])
2952 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2954 return dev;
2956 EXPORT_SYMBOL(rtnl_create_link);
2958 static int rtnl_group_changelink(const struct sk_buff *skb,
2959 struct net *net, int group,
2960 struct ifinfomsg *ifm,
2961 struct netlink_ext_ack *extack,
2962 struct nlattr **tb)
2964 struct net_device *dev, *aux;
2965 int err;
2967 for_each_netdev_safe(net, dev, aux) {
2968 if (dev->group == group) {
2969 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2970 if (err < 0)
2971 return err;
2975 return 0;
2978 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2979 struct nlattr **attr, struct netlink_ext_ack *extack)
2981 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
2982 unsigned char name_assign_type = NET_NAME_USER;
2983 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
2984 const struct rtnl_link_ops *m_ops = NULL;
2985 struct net_device *master_dev = NULL;
2986 struct net *net = sock_net(skb->sk);
2987 const struct rtnl_link_ops *ops;
2988 struct nlattr *tb[IFLA_MAX + 1];
2989 struct net *dest_net, *link_net;
2990 struct nlattr **slave_data;
2991 char kind[MODULE_NAME_LEN];
2992 struct net_device *dev;
2993 struct ifinfomsg *ifm;
2994 char ifname[IFNAMSIZ];
2995 struct nlattr **data;
2996 int err;
2998 #ifdef CONFIG_MODULES
2999 replay:
3000 #endif
3001 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3002 ifla_policy, extack);
3003 if (err < 0)
3004 return err;
3006 err = rtnl_ensure_unique_netns(tb, extack, false);
3007 if (err < 0)
3008 return err;
3010 if (tb[IFLA_IFNAME])
3011 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3012 else
3013 ifname[0] = '\0';
3015 ifm = nlmsg_data(nlh);
3016 if (ifm->ifi_index > 0)
3017 dev = __dev_get_by_index(net, ifm->ifi_index);
3018 else {
3019 if (ifname[0])
3020 dev = __dev_get_by_name(net, ifname);
3021 else
3022 dev = NULL;
3025 if (dev) {
3026 master_dev = netdev_master_upper_dev_get(dev);
3027 if (master_dev)
3028 m_ops = master_dev->rtnl_link_ops;
3031 err = validate_linkmsg(dev, tb);
3032 if (err < 0)
3033 return err;
3035 if (tb[IFLA_LINKINFO]) {
3036 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3037 tb[IFLA_LINKINFO],
3038 ifla_info_policy, NULL);
3039 if (err < 0)
3040 return err;
3041 } else
3042 memset(linkinfo, 0, sizeof(linkinfo));
3044 if (linkinfo[IFLA_INFO_KIND]) {
3045 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3046 ops = rtnl_link_ops_get(kind);
3047 } else {
3048 kind[0] = '\0';
3049 ops = NULL;
3052 data = NULL;
3053 if (ops) {
3054 if (ops->maxtype > RTNL_MAX_TYPE)
3055 return -EINVAL;
3057 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3058 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3059 linkinfo[IFLA_INFO_DATA],
3060 ops->policy, extack);
3061 if (err < 0)
3062 return err;
3063 data = attr;
3065 if (ops->validate) {
3066 err = ops->validate(tb, data, extack);
3067 if (err < 0)
3068 return err;
3072 slave_data = NULL;
3073 if (m_ops) {
3074 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3075 return -EINVAL;
3077 if (m_ops->slave_maxtype &&
3078 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3079 err = nla_parse_nested_deprecated(slave_attr,
3080 m_ops->slave_maxtype,
3081 linkinfo[IFLA_INFO_SLAVE_DATA],
3082 m_ops->slave_policy,
3083 extack);
3084 if (err < 0)
3085 return err;
3086 slave_data = slave_attr;
3090 if (dev) {
3091 int status = 0;
3093 if (nlh->nlmsg_flags & NLM_F_EXCL)
3094 return -EEXIST;
3095 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3096 return -EOPNOTSUPP;
3098 if (linkinfo[IFLA_INFO_DATA]) {
3099 if (!ops || ops != dev->rtnl_link_ops ||
3100 !ops->changelink)
3101 return -EOPNOTSUPP;
3103 err = ops->changelink(dev, tb, data, extack);
3104 if (err < 0)
3105 return err;
3106 status |= DO_SETLINK_NOTIFY;
3109 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3110 if (!m_ops || !m_ops->slave_changelink)
3111 return -EOPNOTSUPP;
3113 err = m_ops->slave_changelink(master_dev, dev, tb,
3114 slave_data, extack);
3115 if (err < 0)
3116 return err;
3117 status |= DO_SETLINK_NOTIFY;
3120 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3123 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3124 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3125 return rtnl_group_changelink(skb, net,
3126 nla_get_u32(tb[IFLA_GROUP]),
3127 ifm, extack, tb);
3128 return -ENODEV;
3131 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3132 return -EOPNOTSUPP;
3134 if (!ops) {
3135 #ifdef CONFIG_MODULES
3136 if (kind[0]) {
3137 __rtnl_unlock();
3138 request_module("rtnl-link-%s", kind);
3139 rtnl_lock();
3140 ops = rtnl_link_ops_get(kind);
3141 if (ops)
3142 goto replay;
3144 #endif
3145 NL_SET_ERR_MSG(extack, "Unknown device type");
3146 return -EOPNOTSUPP;
3149 if (!ops->setup)
3150 return -EOPNOTSUPP;
3152 if (!ifname[0]) {
3153 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3154 name_assign_type = NET_NAME_ENUM;
3157 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3158 if (IS_ERR(dest_net))
3159 return PTR_ERR(dest_net);
3161 if (tb[IFLA_LINK_NETNSID]) {
3162 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3164 link_net = get_net_ns_by_id(dest_net, id);
3165 if (!link_net) {
3166 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3167 err = -EINVAL;
3168 goto out;
3170 err = -EPERM;
3171 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3172 goto out;
3173 } else {
3174 link_net = NULL;
3177 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3178 name_assign_type, ops, tb, extack);
3179 if (IS_ERR(dev)) {
3180 err = PTR_ERR(dev);
3181 goto out;
3184 dev->ifindex = ifm->ifi_index;
3186 if (ops->newlink) {
3187 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3188 /* Drivers should call free_netdev() in ->destructor
3189 * and unregister it on failure after registration
3190 * so that device could be finally freed in rtnl_unlock.
3192 if (err < 0) {
3193 /* If device is not registered at all, free it now */
3194 if (dev->reg_state == NETREG_UNINITIALIZED)
3195 free_netdev(dev);
3196 goto out;
3198 } else {
3199 err = register_netdevice(dev);
3200 if (err < 0) {
3201 free_netdev(dev);
3202 goto out;
3205 err = rtnl_configure_link(dev, ifm);
3206 if (err < 0)
3207 goto out_unregister;
3208 if (link_net) {
3209 err = dev_change_net_namespace(dev, dest_net, ifname);
3210 if (err < 0)
3211 goto out_unregister;
3213 if (tb[IFLA_MASTER]) {
3214 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3215 if (err)
3216 goto out_unregister;
3218 out:
3219 if (link_net)
3220 put_net(link_net);
3221 put_net(dest_net);
3222 return err;
3223 out_unregister:
3224 if (ops->newlink) {
3225 LIST_HEAD(list_kill);
3227 ops->dellink(dev, &list_kill);
3228 unregister_netdevice_many(&list_kill);
3229 } else {
3230 unregister_netdevice(dev);
3232 goto out;
3235 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3236 struct netlink_ext_ack *extack)
3238 struct nlattr **attr;
3239 int ret;
3241 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3242 if (!attr)
3243 return -ENOMEM;
3245 ret = __rtnl_newlink(skb, nlh, attr, extack);
3246 kfree(attr);
3247 return ret;
3250 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3251 const struct nlmsghdr *nlh,
3252 struct nlattr **tb,
3253 struct netlink_ext_ack *extack)
3255 struct ifinfomsg *ifm;
3256 int i, err;
3258 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3259 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3260 return -EINVAL;
3263 if (!netlink_strict_get_check(skb))
3264 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3265 ifla_policy, extack);
3267 ifm = nlmsg_data(nlh);
3268 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3269 ifm->ifi_change) {
3270 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3271 return -EINVAL;
3274 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3275 ifla_policy, extack);
3276 if (err)
3277 return err;
3279 for (i = 0; i <= IFLA_MAX; i++) {
3280 if (!tb[i])
3281 continue;
3283 switch (i) {
3284 case IFLA_IFNAME:
3285 case IFLA_EXT_MASK:
3286 case IFLA_TARGET_NETNSID:
3287 break;
3288 default:
3289 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3290 return -EINVAL;
3294 return 0;
3297 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3298 struct netlink_ext_ack *extack)
3300 struct net *net = sock_net(skb->sk);
3301 struct net *tgt_net = net;
3302 struct ifinfomsg *ifm;
3303 char ifname[IFNAMSIZ];
3304 struct nlattr *tb[IFLA_MAX+1];
3305 struct net_device *dev = NULL;
3306 struct sk_buff *nskb;
3307 int netnsid = -1;
3308 int err;
3309 u32 ext_filter_mask = 0;
3311 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3312 if (err < 0)
3313 return err;
3315 err = rtnl_ensure_unique_netns(tb, extack, true);
3316 if (err < 0)
3317 return err;
3319 if (tb[IFLA_TARGET_NETNSID]) {
3320 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3321 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3322 if (IS_ERR(tgt_net))
3323 return PTR_ERR(tgt_net);
3326 if (tb[IFLA_IFNAME])
3327 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3329 if (tb[IFLA_EXT_MASK])
3330 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3332 err = -EINVAL;
3333 ifm = nlmsg_data(nlh);
3334 if (ifm->ifi_index > 0)
3335 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3336 else if (tb[IFLA_IFNAME])
3337 dev = __dev_get_by_name(tgt_net, ifname);
3338 else
3339 goto out;
3341 err = -ENODEV;
3342 if (dev == NULL)
3343 goto out;
3345 err = -ENOBUFS;
3346 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3347 if (nskb == NULL)
3348 goto out;
3350 err = rtnl_fill_ifinfo(nskb, dev, net,
3351 RTM_NEWLINK, NETLINK_CB(skb).portid,
3352 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3353 0, NULL, 0, netnsid);
3354 if (err < 0) {
3355 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3356 WARN_ON(err == -EMSGSIZE);
3357 kfree_skb(nskb);
3358 } else
3359 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3360 out:
3361 if (netnsid >= 0)
3362 put_net(tgt_net);
3364 return err;
3367 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3369 struct net *net = sock_net(skb->sk);
3370 struct net_device *dev;
3371 struct nlattr *tb[IFLA_MAX+1];
3372 u32 ext_filter_mask = 0;
3373 u16 min_ifinfo_dump_size = 0;
3374 int hdrlen;
3376 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3377 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3378 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3380 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3381 if (tb[IFLA_EXT_MASK])
3382 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3385 if (!ext_filter_mask)
3386 return NLMSG_GOODSIZE;
3388 * traverse the list of net devices and compute the minimum
3389 * buffer size based upon the filter mask.
3391 rcu_read_lock();
3392 for_each_netdev_rcu(net, dev) {
3393 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3394 if_nlmsg_size(dev,
3395 ext_filter_mask));
3397 rcu_read_unlock();
3399 return nlmsg_total_size(min_ifinfo_dump_size);
3402 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3404 int idx;
3405 int s_idx = cb->family;
3406 int type = cb->nlh->nlmsg_type - RTM_BASE;
3407 int ret = 0;
3409 if (s_idx == 0)
3410 s_idx = 1;
3412 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3413 struct rtnl_link **tab;
3414 struct rtnl_link *link;
3415 rtnl_dumpit_func dumpit;
3417 if (idx < s_idx || idx == PF_PACKET)
3418 continue;
3420 if (type < 0 || type >= RTM_NR_MSGTYPES)
3421 continue;
3423 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3424 if (!tab)
3425 continue;
3427 link = tab[type];
3428 if (!link)
3429 continue;
3431 dumpit = link->dumpit;
3432 if (!dumpit)
3433 continue;
3435 if (idx > s_idx) {
3436 memset(&cb->args[0], 0, sizeof(cb->args));
3437 cb->prev_seq = 0;
3438 cb->seq = 0;
3440 ret = dumpit(skb, cb);
3441 if (ret)
3442 break;
3444 cb->family = idx;
3446 return skb->len ? : ret;
3449 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3450 unsigned int change,
3451 u32 event, gfp_t flags, int *new_nsid,
3452 int new_ifindex)
3454 struct net *net = dev_net(dev);
3455 struct sk_buff *skb;
3456 int err = -ENOBUFS;
3457 size_t if_info_size;
3459 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3460 if (skb == NULL)
3461 goto errout;
3463 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3464 type, 0, 0, change, 0, 0, event,
3465 new_nsid, new_ifindex, -1);
3466 if (err < 0) {
3467 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3468 WARN_ON(err == -EMSGSIZE);
3469 kfree_skb(skb);
3470 goto errout;
3472 return skb;
3473 errout:
3474 if (err < 0)
3475 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3476 return NULL;
3479 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3481 struct net *net = dev_net(dev);
3483 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3486 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3487 unsigned int change, u32 event,
3488 gfp_t flags, int *new_nsid, int new_ifindex)
3490 struct sk_buff *skb;
3492 if (dev->reg_state != NETREG_REGISTERED)
3493 return;
3495 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3496 new_ifindex);
3497 if (skb)
3498 rtmsg_ifinfo_send(skb, dev, flags);
3501 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3502 gfp_t flags)
3504 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3505 NULL, 0);
3508 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3509 gfp_t flags, int *new_nsid, int new_ifindex)
3511 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3512 new_nsid, new_ifindex);
3515 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3516 struct net_device *dev,
3517 u8 *addr, u16 vid, u32 pid, u32 seq,
3518 int type, unsigned int flags,
3519 int nlflags, u16 ndm_state)
3521 struct nlmsghdr *nlh;
3522 struct ndmsg *ndm;
3524 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3525 if (!nlh)
3526 return -EMSGSIZE;
3528 ndm = nlmsg_data(nlh);
3529 ndm->ndm_family = AF_BRIDGE;
3530 ndm->ndm_pad1 = 0;
3531 ndm->ndm_pad2 = 0;
3532 ndm->ndm_flags = flags;
3533 ndm->ndm_type = 0;
3534 ndm->ndm_ifindex = dev->ifindex;
3535 ndm->ndm_state = ndm_state;
3537 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3538 goto nla_put_failure;
3539 if (vid)
3540 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3541 goto nla_put_failure;
3543 nlmsg_end(skb, nlh);
3544 return 0;
3546 nla_put_failure:
3547 nlmsg_cancel(skb, nlh);
3548 return -EMSGSIZE;
3551 static inline size_t rtnl_fdb_nlmsg_size(void)
3553 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3554 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3555 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3559 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3560 u16 ndm_state)
3562 struct net *net = dev_net(dev);
3563 struct sk_buff *skb;
3564 int err = -ENOBUFS;
3566 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3567 if (!skb)
3568 goto errout;
3570 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3571 0, 0, type, NTF_SELF, 0, ndm_state);
3572 if (err < 0) {
3573 kfree_skb(skb);
3574 goto errout;
3577 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3578 return;
3579 errout:
3580 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3584 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3586 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3587 struct nlattr *tb[],
3588 struct net_device *dev,
3589 const unsigned char *addr, u16 vid,
3590 u16 flags)
3592 int err = -EINVAL;
3594 /* If aging addresses are supported device will need to
3595 * implement its own handler for this.
3597 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3598 pr_info("%s: FDB only supports static addresses\n", dev->name);
3599 return err;
3602 if (vid) {
3603 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3604 return err;
3607 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3608 err = dev_uc_add_excl(dev, addr);
3609 else if (is_multicast_ether_addr(addr))
3610 err = dev_mc_add_excl(dev, addr);
3612 /* Only return duplicate errors if NLM_F_EXCL is set */
3613 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3614 err = 0;
3616 return err;
3618 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3620 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3621 struct netlink_ext_ack *extack)
3623 u16 vid = 0;
3625 if (vlan_attr) {
3626 if (nla_len(vlan_attr) != sizeof(u16)) {
3627 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3628 return -EINVAL;
3631 vid = nla_get_u16(vlan_attr);
3633 if (!vid || vid >= VLAN_VID_MASK) {
3634 NL_SET_ERR_MSG(extack, "invalid vlan id");
3635 return -EINVAL;
3638 *p_vid = vid;
3639 return 0;
3642 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3643 struct netlink_ext_ack *extack)
3645 struct net *net = sock_net(skb->sk);
3646 struct ndmsg *ndm;
3647 struct nlattr *tb[NDA_MAX+1];
3648 struct net_device *dev;
3649 u8 *addr;
3650 u16 vid;
3651 int err;
3653 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3654 extack);
3655 if (err < 0)
3656 return err;
3658 ndm = nlmsg_data(nlh);
3659 if (ndm->ndm_ifindex == 0) {
3660 NL_SET_ERR_MSG(extack, "invalid ifindex");
3661 return -EINVAL;
3664 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3665 if (dev == NULL) {
3666 NL_SET_ERR_MSG(extack, "unknown ifindex");
3667 return -ENODEV;
3670 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3671 NL_SET_ERR_MSG(extack, "invalid address");
3672 return -EINVAL;
3675 if (dev->type != ARPHRD_ETHER) {
3676 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3677 return -EINVAL;
3680 addr = nla_data(tb[NDA_LLADDR]);
3682 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3683 if (err)
3684 return err;
3686 err = -EOPNOTSUPP;
3688 /* Support fdb on master device the net/bridge default case */
3689 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3690 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3691 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3692 const struct net_device_ops *ops = br_dev->netdev_ops;
3694 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3695 nlh->nlmsg_flags, extack);
3696 if (err)
3697 goto out;
3698 else
3699 ndm->ndm_flags &= ~NTF_MASTER;
3702 /* Embedded bridge, macvlan, and any other device support */
3703 if ((ndm->ndm_flags & NTF_SELF)) {
3704 if (dev->netdev_ops->ndo_fdb_add)
3705 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3706 vid,
3707 nlh->nlmsg_flags,
3708 extack);
3709 else
3710 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3711 nlh->nlmsg_flags);
3713 if (!err) {
3714 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3715 ndm->ndm_state);
3716 ndm->ndm_flags &= ~NTF_SELF;
3719 out:
3720 return err;
3724 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3726 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3727 struct nlattr *tb[],
3728 struct net_device *dev,
3729 const unsigned char *addr, u16 vid)
3731 int err = -EINVAL;
3733 /* If aging addresses are supported device will need to
3734 * implement its own handler for this.
3736 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3737 pr_info("%s: FDB only supports static addresses\n", dev->name);
3738 return err;
3741 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3742 err = dev_uc_del(dev, addr);
3743 else if (is_multicast_ether_addr(addr))
3744 err = dev_mc_del(dev, addr);
3746 return err;
3748 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3750 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3751 struct netlink_ext_ack *extack)
3753 struct net *net = sock_net(skb->sk);
3754 struct ndmsg *ndm;
3755 struct nlattr *tb[NDA_MAX+1];
3756 struct net_device *dev;
3757 int err = -EINVAL;
3758 __u8 *addr;
3759 u16 vid;
3761 if (!netlink_capable(skb, CAP_NET_ADMIN))
3762 return -EPERM;
3764 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3765 extack);
3766 if (err < 0)
3767 return err;
3769 ndm = nlmsg_data(nlh);
3770 if (ndm->ndm_ifindex == 0) {
3771 NL_SET_ERR_MSG(extack, "invalid ifindex");
3772 return -EINVAL;
3775 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3776 if (dev == NULL) {
3777 NL_SET_ERR_MSG(extack, "unknown ifindex");
3778 return -ENODEV;
3781 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3782 NL_SET_ERR_MSG(extack, "invalid address");
3783 return -EINVAL;
3786 if (dev->type != ARPHRD_ETHER) {
3787 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3788 return -EINVAL;
3791 addr = nla_data(tb[NDA_LLADDR]);
3793 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3794 if (err)
3795 return err;
3797 err = -EOPNOTSUPP;
3799 /* Support fdb on master device the net/bridge default case */
3800 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3801 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3802 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3803 const struct net_device_ops *ops = br_dev->netdev_ops;
3805 if (ops->ndo_fdb_del)
3806 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3808 if (err)
3809 goto out;
3810 else
3811 ndm->ndm_flags &= ~NTF_MASTER;
3814 /* Embedded bridge, macvlan, and any other device support */
3815 if (ndm->ndm_flags & NTF_SELF) {
3816 if (dev->netdev_ops->ndo_fdb_del)
3817 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3818 vid);
3819 else
3820 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3822 if (!err) {
3823 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3824 ndm->ndm_state);
3825 ndm->ndm_flags &= ~NTF_SELF;
3828 out:
3829 return err;
3832 static int nlmsg_populate_fdb(struct sk_buff *skb,
3833 struct netlink_callback *cb,
3834 struct net_device *dev,
3835 int *idx,
3836 struct netdev_hw_addr_list *list)
3838 struct netdev_hw_addr *ha;
3839 int err;
3840 u32 portid, seq;
3842 portid = NETLINK_CB(cb->skb).portid;
3843 seq = cb->nlh->nlmsg_seq;
3845 list_for_each_entry(ha, &list->list, list) {
3846 if (*idx < cb->args[2])
3847 goto skip;
3849 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3850 portid, seq,
3851 RTM_NEWNEIGH, NTF_SELF,
3852 NLM_F_MULTI, NUD_PERMANENT);
3853 if (err < 0)
3854 return err;
3855 skip:
3856 *idx += 1;
3858 return 0;
3862 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3863 * @skb: socket buffer to store message in
3864 * @cb: netlink callback
3865 * @dev: netdevice
3866 * @filter_dev: ignored
3867 * @idx: the number of FDB table entries dumped is added to *@idx
3869 * Default netdevice operation to dump the existing unicast address list.
3870 * Returns number of addresses from list put in skb.
3872 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3873 struct netlink_callback *cb,
3874 struct net_device *dev,
3875 struct net_device *filter_dev,
3876 int *idx)
3878 int err;
3880 if (dev->type != ARPHRD_ETHER)
3881 return -EINVAL;
3883 netif_addr_lock_bh(dev);
3884 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3885 if (err)
3886 goto out;
3887 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3888 out:
3889 netif_addr_unlock_bh(dev);
3890 return err;
3892 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3894 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3895 int *br_idx, int *brport_idx,
3896 struct netlink_ext_ack *extack)
3898 struct nlattr *tb[NDA_MAX + 1];
3899 struct ndmsg *ndm;
3900 int err, i;
3902 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3903 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3904 return -EINVAL;
3907 ndm = nlmsg_data(nlh);
3908 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3909 ndm->ndm_flags || ndm->ndm_type) {
3910 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3911 return -EINVAL;
3914 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
3915 NDA_MAX, NULL, extack);
3916 if (err < 0)
3917 return err;
3919 *brport_idx = ndm->ndm_ifindex;
3920 for (i = 0; i <= NDA_MAX; ++i) {
3921 if (!tb[i])
3922 continue;
3924 switch (i) {
3925 case NDA_IFINDEX:
3926 if (nla_len(tb[i]) != sizeof(u32)) {
3927 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3928 return -EINVAL;
3930 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3931 break;
3932 case NDA_MASTER:
3933 if (nla_len(tb[i]) != sizeof(u32)) {
3934 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3935 return -EINVAL;
3937 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3938 break;
3939 default:
3940 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3941 return -EINVAL;
3945 return 0;
3948 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3949 int *br_idx, int *brport_idx,
3950 struct netlink_ext_ack *extack)
3952 struct nlattr *tb[IFLA_MAX+1];
3953 int err;
3955 /* A hack to preserve kernel<->userspace interface.
3956 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3957 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3958 * So, check for ndmsg with an optional u32 attribute (not used here).
3959 * Fortunately these sizes don't conflict with the size of ifinfomsg
3960 * with an optional attribute.
3962 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3963 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3964 nla_attr_size(sizeof(u32)))) {
3965 struct ifinfomsg *ifm;
3967 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
3968 tb, IFLA_MAX, ifla_policy,
3969 extack);
3970 if (err < 0) {
3971 return -EINVAL;
3972 } else if (err == 0) {
3973 if (tb[IFLA_MASTER])
3974 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3977 ifm = nlmsg_data(nlh);
3978 *brport_idx = ifm->ifi_index;
3980 return 0;
3983 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3985 struct net_device *dev;
3986 struct net_device *br_dev = NULL;
3987 const struct net_device_ops *ops = NULL;
3988 const struct net_device_ops *cops = NULL;
3989 struct net *net = sock_net(skb->sk);
3990 struct hlist_head *head;
3991 int brport_idx = 0;
3992 int br_idx = 0;
3993 int h, s_h;
3994 int idx = 0, s_idx;
3995 int err = 0;
3996 int fidx = 0;
3998 if (cb->strict_check)
3999 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4000 cb->extack);
4001 else
4002 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4003 cb->extack);
4004 if (err < 0)
4005 return err;
4007 if (br_idx) {
4008 br_dev = __dev_get_by_index(net, br_idx);
4009 if (!br_dev)
4010 return -ENODEV;
4012 ops = br_dev->netdev_ops;
4015 s_h = cb->args[0];
4016 s_idx = cb->args[1];
4018 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4019 idx = 0;
4020 head = &net->dev_index_head[h];
4021 hlist_for_each_entry(dev, head, index_hlist) {
4023 if (brport_idx && (dev->ifindex != brport_idx))
4024 continue;
4026 if (!br_idx) { /* user did not specify a specific bridge */
4027 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4028 br_dev = netdev_master_upper_dev_get(dev);
4029 cops = br_dev->netdev_ops;
4031 } else {
4032 if (dev != br_dev &&
4033 !(dev->priv_flags & IFF_BRIDGE_PORT))
4034 continue;
4036 if (br_dev != netdev_master_upper_dev_get(dev) &&
4037 !(dev->priv_flags & IFF_EBRIDGE))
4038 continue;
4039 cops = ops;
4042 if (idx < s_idx)
4043 goto cont;
4045 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4046 if (cops && cops->ndo_fdb_dump) {
4047 err = cops->ndo_fdb_dump(skb, cb,
4048 br_dev, dev,
4049 &fidx);
4050 if (err == -EMSGSIZE)
4051 goto out;
4055 if (dev->netdev_ops->ndo_fdb_dump)
4056 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4057 dev, NULL,
4058 &fidx);
4059 else
4060 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4061 &fidx);
4062 if (err == -EMSGSIZE)
4063 goto out;
4065 cops = NULL;
4067 /* reset fdb offset to 0 for rest of the interfaces */
4068 cb->args[2] = 0;
4069 fidx = 0;
4070 cont:
4071 idx++;
4075 out:
4076 cb->args[0] = h;
4077 cb->args[1] = idx;
4078 cb->args[2] = fidx;
4080 return skb->len;
4083 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4084 struct nlattr **tb, u8 *ndm_flags,
4085 int *br_idx, int *brport_idx, u8 **addr,
4086 u16 *vid, struct netlink_ext_ack *extack)
4088 struct ndmsg *ndm;
4089 int err, i;
4091 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4092 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4093 return -EINVAL;
4096 ndm = nlmsg_data(nlh);
4097 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4098 ndm->ndm_type) {
4099 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4100 return -EINVAL;
4103 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4104 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4105 return -EINVAL;
4108 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4109 NDA_MAX, nda_policy, extack);
4110 if (err < 0)
4111 return err;
4113 *ndm_flags = ndm->ndm_flags;
4114 *brport_idx = ndm->ndm_ifindex;
4115 for (i = 0; i <= NDA_MAX; ++i) {
4116 if (!tb[i])
4117 continue;
4119 switch (i) {
4120 case NDA_MASTER:
4121 *br_idx = nla_get_u32(tb[i]);
4122 break;
4123 case NDA_LLADDR:
4124 if (nla_len(tb[i]) != ETH_ALEN) {
4125 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4126 return -EINVAL;
4128 *addr = nla_data(tb[i]);
4129 break;
4130 case NDA_VLAN:
4131 err = fdb_vid_parse(tb[i], vid, extack);
4132 if (err)
4133 return err;
4134 break;
4135 case NDA_VNI:
4136 break;
4137 default:
4138 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4139 return -EINVAL;
4143 return 0;
4146 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4147 struct netlink_ext_ack *extack)
4149 struct net_device *dev = NULL, *br_dev = NULL;
4150 const struct net_device_ops *ops = NULL;
4151 struct net *net = sock_net(in_skb->sk);
4152 struct nlattr *tb[NDA_MAX + 1];
4153 struct sk_buff *skb;
4154 int brport_idx = 0;
4155 u8 ndm_flags = 0;
4156 int br_idx = 0;
4157 u8 *addr = NULL;
4158 u16 vid = 0;
4159 int err;
4161 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4162 &brport_idx, &addr, &vid, extack);
4163 if (err < 0)
4164 return err;
4166 if (!addr) {
4167 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4168 return -EINVAL;
4171 if (brport_idx) {
4172 dev = __dev_get_by_index(net, brport_idx);
4173 if (!dev) {
4174 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4175 return -ENODEV;
4179 if (br_idx) {
4180 if (dev) {
4181 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4182 return -EINVAL;
4185 br_dev = __dev_get_by_index(net, br_idx);
4186 if (!br_dev) {
4187 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4188 return -EINVAL;
4190 ops = br_dev->netdev_ops;
4193 if (dev) {
4194 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4195 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4196 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4197 return -EINVAL;
4199 br_dev = netdev_master_upper_dev_get(dev);
4200 if (!br_dev) {
4201 NL_SET_ERR_MSG(extack, "Master of device not found");
4202 return -EINVAL;
4204 ops = br_dev->netdev_ops;
4205 } else {
4206 if (!(ndm_flags & NTF_SELF)) {
4207 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4208 return -EINVAL;
4210 ops = dev->netdev_ops;
4214 if (!br_dev && !dev) {
4215 NL_SET_ERR_MSG(extack, "No device specified");
4216 return -ENODEV;
4219 if (!ops || !ops->ndo_fdb_get) {
4220 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4221 return -EOPNOTSUPP;
4224 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4225 if (!skb)
4226 return -ENOBUFS;
4228 if (br_dev)
4229 dev = br_dev;
4230 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4231 NETLINK_CB(in_skb).portid,
4232 nlh->nlmsg_seq, extack);
4233 if (err)
4234 goto out;
4236 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4237 out:
4238 kfree_skb(skb);
4239 return err;
4242 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4243 unsigned int attrnum, unsigned int flag)
4245 if (mask & flag)
4246 return nla_put_u8(skb, attrnum, !!(flags & flag));
4247 return 0;
4250 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4251 struct net_device *dev, u16 mode,
4252 u32 flags, u32 mask, int nlflags,
4253 u32 filter_mask,
4254 int (*vlan_fill)(struct sk_buff *skb,
4255 struct net_device *dev,
4256 u32 filter_mask))
4258 struct nlmsghdr *nlh;
4259 struct ifinfomsg *ifm;
4260 struct nlattr *br_afspec;
4261 struct nlattr *protinfo;
4262 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4263 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4264 int err = 0;
4266 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4267 if (nlh == NULL)
4268 return -EMSGSIZE;
4270 ifm = nlmsg_data(nlh);
4271 ifm->ifi_family = AF_BRIDGE;
4272 ifm->__ifi_pad = 0;
4273 ifm->ifi_type = dev->type;
4274 ifm->ifi_index = dev->ifindex;
4275 ifm->ifi_flags = dev_get_flags(dev);
4276 ifm->ifi_change = 0;
4279 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4280 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4281 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4282 (br_dev &&
4283 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4284 (dev->addr_len &&
4285 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4286 (dev->ifindex != dev_get_iflink(dev) &&
4287 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4288 goto nla_put_failure;
4290 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4291 if (!br_afspec)
4292 goto nla_put_failure;
4294 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4295 nla_nest_cancel(skb, br_afspec);
4296 goto nla_put_failure;
4299 if (mode != BRIDGE_MODE_UNDEF) {
4300 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4301 nla_nest_cancel(skb, br_afspec);
4302 goto nla_put_failure;
4305 if (vlan_fill) {
4306 err = vlan_fill(skb, dev, filter_mask);
4307 if (err) {
4308 nla_nest_cancel(skb, br_afspec);
4309 goto nla_put_failure;
4312 nla_nest_end(skb, br_afspec);
4314 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4315 if (!protinfo)
4316 goto nla_put_failure;
4318 if (brport_nla_put_flag(skb, flags, mask,
4319 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4320 brport_nla_put_flag(skb, flags, mask,
4321 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4322 brport_nla_put_flag(skb, flags, mask,
4323 IFLA_BRPORT_FAST_LEAVE,
4324 BR_MULTICAST_FAST_LEAVE) ||
4325 brport_nla_put_flag(skb, flags, mask,
4326 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4327 brport_nla_put_flag(skb, flags, mask,
4328 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4329 brport_nla_put_flag(skb, flags, mask,
4330 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4331 brport_nla_put_flag(skb, flags, mask,
4332 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4333 brport_nla_put_flag(skb, flags, mask,
4334 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4335 nla_nest_cancel(skb, protinfo);
4336 goto nla_put_failure;
4339 nla_nest_end(skb, protinfo);
4341 nlmsg_end(skb, nlh);
4342 return 0;
4343 nla_put_failure:
4344 nlmsg_cancel(skb, nlh);
4345 return err ? err : -EMSGSIZE;
4347 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4349 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4350 bool strict_check, u32 *filter_mask,
4351 struct netlink_ext_ack *extack)
4353 struct nlattr *tb[IFLA_MAX+1];
4354 int err, i;
4356 if (strict_check) {
4357 struct ifinfomsg *ifm;
4359 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4360 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4361 return -EINVAL;
4364 ifm = nlmsg_data(nlh);
4365 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4366 ifm->ifi_change || ifm->ifi_index) {
4367 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4368 return -EINVAL;
4371 err = nlmsg_parse_deprecated_strict(nlh,
4372 sizeof(struct ifinfomsg),
4373 tb, IFLA_MAX, ifla_policy,
4374 extack);
4375 } else {
4376 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4377 tb, IFLA_MAX, ifla_policy,
4378 extack);
4380 if (err < 0)
4381 return err;
4383 /* new attributes should only be added with strict checking */
4384 for (i = 0; i <= IFLA_MAX; ++i) {
4385 if (!tb[i])
4386 continue;
4388 switch (i) {
4389 case IFLA_EXT_MASK:
4390 *filter_mask = nla_get_u32(tb[i]);
4391 break;
4392 default:
4393 if (strict_check) {
4394 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4395 return -EINVAL;
4400 return 0;
4403 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4405 const struct nlmsghdr *nlh = cb->nlh;
4406 struct net *net = sock_net(skb->sk);
4407 struct net_device *dev;
4408 int idx = 0;
4409 u32 portid = NETLINK_CB(cb->skb).portid;
4410 u32 seq = nlh->nlmsg_seq;
4411 u32 filter_mask = 0;
4412 int err;
4414 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4415 cb->extack);
4416 if (err < 0 && cb->strict_check)
4417 return err;
4419 rcu_read_lock();
4420 for_each_netdev_rcu(net, dev) {
4421 const struct net_device_ops *ops = dev->netdev_ops;
4422 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4424 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4425 if (idx >= cb->args[0]) {
4426 err = br_dev->netdev_ops->ndo_bridge_getlink(
4427 skb, portid, seq, dev,
4428 filter_mask, NLM_F_MULTI);
4429 if (err < 0 && err != -EOPNOTSUPP) {
4430 if (likely(skb->len))
4431 break;
4433 goto out_err;
4436 idx++;
4439 if (ops->ndo_bridge_getlink) {
4440 if (idx >= cb->args[0]) {
4441 err = ops->ndo_bridge_getlink(skb, portid,
4442 seq, dev,
4443 filter_mask,
4444 NLM_F_MULTI);
4445 if (err < 0 && err != -EOPNOTSUPP) {
4446 if (likely(skb->len))
4447 break;
4449 goto out_err;
4452 idx++;
4455 err = skb->len;
4456 out_err:
4457 rcu_read_unlock();
4458 cb->args[0] = idx;
4460 return err;
4463 static inline size_t bridge_nlmsg_size(void)
4465 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4466 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4467 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4468 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4469 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4470 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4471 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4472 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4473 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4474 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4475 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4478 static int rtnl_bridge_notify(struct net_device *dev)
4480 struct net *net = dev_net(dev);
4481 struct sk_buff *skb;
4482 int err = -EOPNOTSUPP;
4484 if (!dev->netdev_ops->ndo_bridge_getlink)
4485 return 0;
4487 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4488 if (!skb) {
4489 err = -ENOMEM;
4490 goto errout;
4493 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4494 if (err < 0)
4495 goto errout;
4497 if (!skb->len)
4498 goto errout;
4500 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4501 return 0;
4502 errout:
4503 WARN_ON(err == -EMSGSIZE);
4504 kfree_skb(skb);
4505 if (err)
4506 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4507 return err;
4510 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4511 struct netlink_ext_ack *extack)
4513 struct net *net = sock_net(skb->sk);
4514 struct ifinfomsg *ifm;
4515 struct net_device *dev;
4516 struct nlattr *br_spec, *attr = NULL;
4517 int rem, err = -EOPNOTSUPP;
4518 u16 flags = 0;
4519 bool have_flags = false;
4521 if (nlmsg_len(nlh) < sizeof(*ifm))
4522 return -EINVAL;
4524 ifm = nlmsg_data(nlh);
4525 if (ifm->ifi_family != AF_BRIDGE)
4526 return -EPFNOSUPPORT;
4528 dev = __dev_get_by_index(net, ifm->ifi_index);
4529 if (!dev) {
4530 NL_SET_ERR_MSG(extack, "unknown ifindex");
4531 return -ENODEV;
4534 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4535 if (br_spec) {
4536 nla_for_each_nested(attr, br_spec, rem) {
4537 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4538 if (nla_len(attr) < sizeof(flags))
4539 return -EINVAL;
4541 have_flags = true;
4542 flags = nla_get_u16(attr);
4543 break;
4548 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4549 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4551 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4552 err = -EOPNOTSUPP;
4553 goto out;
4556 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4557 extack);
4558 if (err)
4559 goto out;
4561 flags &= ~BRIDGE_FLAGS_MASTER;
4564 if ((flags & BRIDGE_FLAGS_SELF)) {
4565 if (!dev->netdev_ops->ndo_bridge_setlink)
4566 err = -EOPNOTSUPP;
4567 else
4568 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4569 flags,
4570 extack);
4571 if (!err) {
4572 flags &= ~BRIDGE_FLAGS_SELF;
4574 /* Generate event to notify upper layer of bridge
4575 * change
4577 err = rtnl_bridge_notify(dev);
4581 if (have_flags)
4582 memcpy(nla_data(attr), &flags, sizeof(flags));
4583 out:
4584 return err;
4587 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4588 struct netlink_ext_ack *extack)
4590 struct net *net = sock_net(skb->sk);
4591 struct ifinfomsg *ifm;
4592 struct net_device *dev;
4593 struct nlattr *br_spec, *attr = NULL;
4594 int rem, err = -EOPNOTSUPP;
4595 u16 flags = 0;
4596 bool have_flags = false;
4598 if (nlmsg_len(nlh) < sizeof(*ifm))
4599 return -EINVAL;
4601 ifm = nlmsg_data(nlh);
4602 if (ifm->ifi_family != AF_BRIDGE)
4603 return -EPFNOSUPPORT;
4605 dev = __dev_get_by_index(net, ifm->ifi_index);
4606 if (!dev) {
4607 NL_SET_ERR_MSG(extack, "unknown ifindex");
4608 return -ENODEV;
4611 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4612 if (br_spec) {
4613 nla_for_each_nested(attr, br_spec, rem) {
4614 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4615 if (nla_len(attr) < sizeof(flags))
4616 return -EINVAL;
4618 have_flags = true;
4619 flags = nla_get_u16(attr);
4620 break;
4625 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4626 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4628 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4629 err = -EOPNOTSUPP;
4630 goto out;
4633 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4634 if (err)
4635 goto out;
4637 flags &= ~BRIDGE_FLAGS_MASTER;
4640 if ((flags & BRIDGE_FLAGS_SELF)) {
4641 if (!dev->netdev_ops->ndo_bridge_dellink)
4642 err = -EOPNOTSUPP;
4643 else
4644 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4645 flags);
4647 if (!err) {
4648 flags &= ~BRIDGE_FLAGS_SELF;
4650 /* Generate event to notify upper layer of bridge
4651 * change
4653 err = rtnl_bridge_notify(dev);
4657 if (have_flags)
4658 memcpy(nla_data(attr), &flags, sizeof(flags));
4659 out:
4660 return err;
4663 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4665 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4666 (!idxattr || idxattr == attrid);
4669 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4670 static int rtnl_get_offload_stats_attr_size(int attr_id)
4672 switch (attr_id) {
4673 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4674 return sizeof(struct rtnl_link_stats64);
4677 return 0;
4680 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4681 int *prividx)
4683 struct nlattr *attr = NULL;
4684 int attr_id, size;
4685 void *attr_data;
4686 int err;
4688 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4689 dev->netdev_ops->ndo_get_offload_stats))
4690 return -ENODATA;
4692 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4693 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4694 if (attr_id < *prividx)
4695 continue;
4697 size = rtnl_get_offload_stats_attr_size(attr_id);
4698 if (!size)
4699 continue;
4701 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4702 continue;
4704 attr = nla_reserve_64bit(skb, attr_id, size,
4705 IFLA_OFFLOAD_XSTATS_UNSPEC);
4706 if (!attr)
4707 goto nla_put_failure;
4709 attr_data = nla_data(attr);
4710 memset(attr_data, 0, size);
4711 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4712 attr_data);
4713 if (err)
4714 goto get_offload_stats_failure;
4717 if (!attr)
4718 return -ENODATA;
4720 *prividx = 0;
4721 return 0;
4723 nla_put_failure:
4724 err = -EMSGSIZE;
4725 get_offload_stats_failure:
4726 *prividx = attr_id;
4727 return err;
4730 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4732 int nla_size = 0;
4733 int attr_id;
4734 int size;
4736 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4737 dev->netdev_ops->ndo_get_offload_stats))
4738 return 0;
4740 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4741 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4742 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4743 continue;
4744 size = rtnl_get_offload_stats_attr_size(attr_id);
4745 nla_size += nla_total_size_64bit(size);
4748 if (nla_size != 0)
4749 nla_size += nla_total_size(0);
4751 return nla_size;
4754 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4755 int type, u32 pid, u32 seq, u32 change,
4756 unsigned int flags, unsigned int filter_mask,
4757 int *idxattr, int *prividx)
4759 struct if_stats_msg *ifsm;
4760 struct nlmsghdr *nlh;
4761 struct nlattr *attr;
4762 int s_prividx = *prividx;
4763 int err;
4765 ASSERT_RTNL();
4767 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4768 if (!nlh)
4769 return -EMSGSIZE;
4771 ifsm = nlmsg_data(nlh);
4772 ifsm->family = PF_UNSPEC;
4773 ifsm->pad1 = 0;
4774 ifsm->pad2 = 0;
4775 ifsm->ifindex = dev->ifindex;
4776 ifsm->filter_mask = filter_mask;
4778 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4779 struct rtnl_link_stats64 *sp;
4781 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4782 sizeof(struct rtnl_link_stats64),
4783 IFLA_STATS_UNSPEC);
4784 if (!attr)
4785 goto nla_put_failure;
4787 sp = nla_data(attr);
4788 dev_get_stats(dev, sp);
4791 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4792 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4794 if (ops && ops->fill_linkxstats) {
4795 *idxattr = IFLA_STATS_LINK_XSTATS;
4796 attr = nla_nest_start_noflag(skb,
4797 IFLA_STATS_LINK_XSTATS);
4798 if (!attr)
4799 goto nla_put_failure;
4801 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4802 nla_nest_end(skb, attr);
4803 if (err)
4804 goto nla_put_failure;
4805 *idxattr = 0;
4809 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4810 *idxattr)) {
4811 const struct rtnl_link_ops *ops = NULL;
4812 const struct net_device *master;
4814 master = netdev_master_upper_dev_get(dev);
4815 if (master)
4816 ops = master->rtnl_link_ops;
4817 if (ops && ops->fill_linkxstats) {
4818 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4819 attr = nla_nest_start_noflag(skb,
4820 IFLA_STATS_LINK_XSTATS_SLAVE);
4821 if (!attr)
4822 goto nla_put_failure;
4824 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4825 nla_nest_end(skb, attr);
4826 if (err)
4827 goto nla_put_failure;
4828 *idxattr = 0;
4832 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4833 *idxattr)) {
4834 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4835 attr = nla_nest_start_noflag(skb,
4836 IFLA_STATS_LINK_OFFLOAD_XSTATS);
4837 if (!attr)
4838 goto nla_put_failure;
4840 err = rtnl_get_offload_stats(skb, dev, prividx);
4841 if (err == -ENODATA)
4842 nla_nest_cancel(skb, attr);
4843 else
4844 nla_nest_end(skb, attr);
4846 if (err && err != -ENODATA)
4847 goto nla_put_failure;
4848 *idxattr = 0;
4851 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4852 struct rtnl_af_ops *af_ops;
4854 *idxattr = IFLA_STATS_AF_SPEC;
4855 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
4856 if (!attr)
4857 goto nla_put_failure;
4859 rcu_read_lock();
4860 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4861 if (af_ops->fill_stats_af) {
4862 struct nlattr *af;
4863 int err;
4865 af = nla_nest_start_noflag(skb,
4866 af_ops->family);
4867 if (!af) {
4868 rcu_read_unlock();
4869 goto nla_put_failure;
4871 err = af_ops->fill_stats_af(skb, dev);
4873 if (err == -ENODATA) {
4874 nla_nest_cancel(skb, af);
4875 } else if (err < 0) {
4876 rcu_read_unlock();
4877 goto nla_put_failure;
4880 nla_nest_end(skb, af);
4883 rcu_read_unlock();
4885 nla_nest_end(skb, attr);
4887 *idxattr = 0;
4890 nlmsg_end(skb, nlh);
4892 return 0;
4894 nla_put_failure:
4895 /* not a multi message or no progress mean a real error */
4896 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4897 nlmsg_cancel(skb, nlh);
4898 else
4899 nlmsg_end(skb, nlh);
4901 return -EMSGSIZE;
4904 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4905 u32 filter_mask)
4907 size_t size = 0;
4909 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4910 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4912 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4913 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4914 int attr = IFLA_STATS_LINK_XSTATS;
4916 if (ops && ops->get_linkxstats_size) {
4917 size += nla_total_size(ops->get_linkxstats_size(dev,
4918 attr));
4919 /* for IFLA_STATS_LINK_XSTATS */
4920 size += nla_total_size(0);
4924 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4925 struct net_device *_dev = (struct net_device *)dev;
4926 const struct rtnl_link_ops *ops = NULL;
4927 const struct net_device *master;
4929 /* netdev_master_upper_dev_get can't take const */
4930 master = netdev_master_upper_dev_get(_dev);
4931 if (master)
4932 ops = master->rtnl_link_ops;
4933 if (ops && ops->get_linkxstats_size) {
4934 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4936 size += nla_total_size(ops->get_linkxstats_size(dev,
4937 attr));
4938 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4939 size += nla_total_size(0);
4943 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4944 size += rtnl_get_offload_stats_size(dev);
4946 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4947 struct rtnl_af_ops *af_ops;
4949 /* for IFLA_STATS_AF_SPEC */
4950 size += nla_total_size(0);
4952 rcu_read_lock();
4953 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4954 if (af_ops->get_stats_af_size) {
4955 size += nla_total_size(
4956 af_ops->get_stats_af_size(dev));
4958 /* for AF_* */
4959 size += nla_total_size(0);
4962 rcu_read_unlock();
4965 return size;
4968 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
4969 bool is_dump, struct netlink_ext_ack *extack)
4971 struct if_stats_msg *ifsm;
4973 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
4974 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
4975 return -EINVAL;
4978 if (!strict_check)
4979 return 0;
4981 ifsm = nlmsg_data(nlh);
4983 /* only requests using strict checks can pass data to influence
4984 * the dump. The legacy exception is filter_mask.
4986 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
4987 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4988 return -EINVAL;
4990 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
4991 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4992 return -EINVAL;
4994 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
4995 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
4996 return -EINVAL;
4999 return 0;
5002 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5003 struct netlink_ext_ack *extack)
5005 struct net *net = sock_net(skb->sk);
5006 struct net_device *dev = NULL;
5007 int idxattr = 0, prividx = 0;
5008 struct if_stats_msg *ifsm;
5009 struct sk_buff *nskb;
5010 u32 filter_mask;
5011 int err;
5013 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5014 false, extack);
5015 if (err)
5016 return err;
5018 ifsm = nlmsg_data(nlh);
5019 if (ifsm->ifindex > 0)
5020 dev = __dev_get_by_index(net, ifsm->ifindex);
5021 else
5022 return -EINVAL;
5024 if (!dev)
5025 return -ENODEV;
5027 filter_mask = ifsm->filter_mask;
5028 if (!filter_mask)
5029 return -EINVAL;
5031 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5032 if (!nskb)
5033 return -ENOBUFS;
5035 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5036 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5037 0, filter_mask, &idxattr, &prividx);
5038 if (err < 0) {
5039 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5040 WARN_ON(err == -EMSGSIZE);
5041 kfree_skb(nskb);
5042 } else {
5043 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5046 return err;
5049 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5051 struct netlink_ext_ack *extack = cb->extack;
5052 int h, s_h, err, s_idx, s_idxattr, s_prividx;
5053 struct net *net = sock_net(skb->sk);
5054 unsigned int flags = NLM_F_MULTI;
5055 struct if_stats_msg *ifsm;
5056 struct hlist_head *head;
5057 struct net_device *dev;
5058 u32 filter_mask = 0;
5059 int idx = 0;
5061 s_h = cb->args[0];
5062 s_idx = cb->args[1];
5063 s_idxattr = cb->args[2];
5064 s_prividx = cb->args[3];
5066 cb->seq = net->dev_base_seq;
5068 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5069 if (err)
5070 return err;
5072 ifsm = nlmsg_data(cb->nlh);
5073 filter_mask = ifsm->filter_mask;
5074 if (!filter_mask) {
5075 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5076 return -EINVAL;
5079 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5080 idx = 0;
5081 head = &net->dev_index_head[h];
5082 hlist_for_each_entry(dev, head, index_hlist) {
5083 if (idx < s_idx)
5084 goto cont;
5085 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5086 NETLINK_CB(cb->skb).portid,
5087 cb->nlh->nlmsg_seq, 0,
5088 flags, filter_mask,
5089 &s_idxattr, &s_prividx);
5090 /* If we ran out of room on the first message,
5091 * we're in trouble
5093 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5095 if (err < 0)
5096 goto out;
5097 s_prividx = 0;
5098 s_idxattr = 0;
5099 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5100 cont:
5101 idx++;
5104 out:
5105 cb->args[3] = s_prividx;
5106 cb->args[2] = s_idxattr;
5107 cb->args[1] = idx;
5108 cb->args[0] = h;
5110 return skb->len;
5113 /* Process one rtnetlink message. */
5115 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5116 struct netlink_ext_ack *extack)
5118 struct net *net = sock_net(skb->sk);
5119 struct rtnl_link *link;
5120 struct module *owner;
5121 int err = -EOPNOTSUPP;
5122 rtnl_doit_func doit;
5123 unsigned int flags;
5124 int kind;
5125 int family;
5126 int type;
5128 type = nlh->nlmsg_type;
5129 if (type > RTM_MAX)
5130 return -EOPNOTSUPP;
5132 type -= RTM_BASE;
5134 /* All the messages must have at least 1 byte length */
5135 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
5136 return 0;
5138 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
5139 kind = type&3;
5141 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
5142 return -EPERM;
5144 rcu_read_lock();
5145 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5146 struct sock *rtnl;
5147 rtnl_dumpit_func dumpit;
5148 u16 min_dump_alloc = 0;
5150 link = rtnl_get_link(family, type);
5151 if (!link || !link->dumpit) {
5152 family = PF_UNSPEC;
5153 link = rtnl_get_link(family, type);
5154 if (!link || !link->dumpit)
5155 goto err_unlock;
5157 owner = link->owner;
5158 dumpit = link->dumpit;
5160 if (type == RTM_GETLINK - RTM_BASE)
5161 min_dump_alloc = rtnl_calcit(skb, nlh);
5163 err = 0;
5164 /* need to do this before rcu_read_unlock() */
5165 if (!try_module_get(owner))
5166 err = -EPROTONOSUPPORT;
5168 rcu_read_unlock();
5170 rtnl = net->rtnl;
5171 if (err == 0) {
5172 struct netlink_dump_control c = {
5173 .dump = dumpit,
5174 .min_dump_alloc = min_dump_alloc,
5175 .module = owner,
5177 err = netlink_dump_start(rtnl, skb, nlh, &c);
5178 /* netlink_dump_start() will keep a reference on
5179 * module if dump is still in progress.
5181 module_put(owner);
5183 return err;
5186 link = rtnl_get_link(family, type);
5187 if (!link || !link->doit) {
5188 family = PF_UNSPEC;
5189 link = rtnl_get_link(PF_UNSPEC, type);
5190 if (!link || !link->doit)
5191 goto out_unlock;
5194 owner = link->owner;
5195 if (!try_module_get(owner)) {
5196 err = -EPROTONOSUPPORT;
5197 goto out_unlock;
5200 flags = link->flags;
5201 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5202 doit = link->doit;
5203 rcu_read_unlock();
5204 if (doit)
5205 err = doit(skb, nlh, extack);
5206 module_put(owner);
5207 return err;
5209 rcu_read_unlock();
5211 rtnl_lock();
5212 link = rtnl_get_link(family, type);
5213 if (link && link->doit)
5214 err = link->doit(skb, nlh, extack);
5215 rtnl_unlock();
5217 module_put(owner);
5219 return err;
5221 out_unlock:
5222 rcu_read_unlock();
5223 return err;
5225 err_unlock:
5226 rcu_read_unlock();
5227 return -EOPNOTSUPP;
5230 static void rtnetlink_rcv(struct sk_buff *skb)
5232 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
5235 static int rtnetlink_bind(struct net *net, int group)
5237 switch (group) {
5238 case RTNLGRP_IPV4_MROUTE_R:
5239 case RTNLGRP_IPV6_MROUTE_R:
5240 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5241 return -EPERM;
5242 break;
5244 return 0;
5247 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5249 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5251 switch (event) {
5252 case NETDEV_REBOOT:
5253 case NETDEV_CHANGEMTU:
5254 case NETDEV_CHANGEADDR:
5255 case NETDEV_CHANGENAME:
5256 case NETDEV_FEAT_CHANGE:
5257 case NETDEV_BONDING_FAILOVER:
5258 case NETDEV_POST_TYPE_CHANGE:
5259 case NETDEV_NOTIFY_PEERS:
5260 case NETDEV_CHANGEUPPER:
5261 case NETDEV_RESEND_IGMP:
5262 case NETDEV_CHANGEINFODATA:
5263 case NETDEV_CHANGELOWERSTATE:
5264 case NETDEV_CHANGE_TX_QUEUE_LEN:
5265 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5266 GFP_KERNEL, NULL, 0);
5267 break;
5268 default:
5269 break;
5271 return NOTIFY_DONE;
5274 static struct notifier_block rtnetlink_dev_notifier = {
5275 .notifier_call = rtnetlink_event,
5279 static int __net_init rtnetlink_net_init(struct net *net)
5281 struct sock *sk;
5282 struct netlink_kernel_cfg cfg = {
5283 .groups = RTNLGRP_MAX,
5284 .input = rtnetlink_rcv,
5285 .cb_mutex = &rtnl_mutex,
5286 .flags = NL_CFG_F_NONROOT_RECV,
5287 .bind = rtnetlink_bind,
5290 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5291 if (!sk)
5292 return -ENOMEM;
5293 net->rtnl = sk;
5294 return 0;
5297 static void __net_exit rtnetlink_net_exit(struct net *net)
5299 netlink_kernel_release(net->rtnl);
5300 net->rtnl = NULL;
5303 static struct pernet_operations rtnetlink_net_ops = {
5304 .init = rtnetlink_net_init,
5305 .exit = rtnetlink_net_exit,
5308 void __init rtnetlink_init(void)
5310 if (register_pernet_subsys(&rtnetlink_net_ops))
5311 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5313 register_netdevice_notifier(&rtnetlink_dev_notifier);
5315 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5316 rtnl_dump_ifinfo, 0);
5317 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5318 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5319 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5321 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5322 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5323 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5325 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5326 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5327 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5329 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5330 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5331 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5333 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,