debugfs: fix debugfs_rename parameter checking
[linux/fpc-iii.git] / net / core / rtnetlink.c
blob7819f7804eeb80fcac76f32ee1aacdc0a180d6ca
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
45 #include <linux/uaccess.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
50 #include <net/ip.h>
51 #include <net/protocol.h>
52 #include <net/arp.h>
53 #include <net/route.h>
54 #include <net/udp.h>
55 #include <net/tcp.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
62 #define RTNL_MAX_TYPE 49
63 #define RTNL_SLAVE_MAX_TYPE 36
65 struct rtnl_link {
66 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
68 struct module *owner;
69 unsigned int flags;
70 struct rcu_head rcu;
73 static DEFINE_MUTEX(rtnl_mutex);
75 void rtnl_lock(void)
77 mutex_lock(&rtnl_mutex);
79 EXPORT_SYMBOL(rtnl_lock);
81 int rtnl_lock_killable(void)
83 return mutex_lock_killable(&rtnl_mutex);
85 EXPORT_SYMBOL(rtnl_lock_killable);
87 static struct sk_buff *defer_kfree_skb_list;
88 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
95 EXPORT_SYMBOL(rtnl_kfree_skbs);
97 void __rtnl_unlock(void)
99 struct sk_buff *head = defer_kfree_skb_list;
101 defer_kfree_skb_list = NULL;
103 mutex_unlock(&rtnl_mutex);
105 while (head) {
106 struct sk_buff *next = head->next;
108 kfree_skb(head);
109 cond_resched();
110 head = next;
114 void rtnl_unlock(void)
116 /* This fellow will unlock it for us. */
117 netdev_run_todo();
119 EXPORT_SYMBOL(rtnl_unlock);
121 int rtnl_trylock(void)
123 return mutex_trylock(&rtnl_mutex);
125 EXPORT_SYMBOL(rtnl_trylock);
127 int rtnl_is_locked(void)
129 return mutex_is_locked(&rtnl_mutex);
131 EXPORT_SYMBOL(rtnl_is_locked);
133 bool refcount_dec_and_rtnl_lock(refcount_t *r)
135 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
137 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
139 #ifdef CONFIG_PROVE_LOCKING
140 bool lockdep_rtnl_is_held(void)
142 return lockdep_is_held(&rtnl_mutex);
144 EXPORT_SYMBOL(lockdep_rtnl_is_held);
145 #endif /* #ifdef CONFIG_PROVE_LOCKING */
147 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
149 static inline int rtm_msgindex(int msgtype)
151 int msgindex = msgtype - RTM_BASE;
154 * msgindex < 0 implies someone tried to register a netlink
155 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
156 * the message type has not been added to linux/rtnetlink.h
158 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
160 return msgindex;
163 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
165 struct rtnl_link **tab;
167 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
168 protocol = PF_UNSPEC;
170 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
171 if (!tab)
172 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
174 return tab[msgtype];
177 static int rtnl_register_internal(struct module *owner,
178 int protocol, int msgtype,
179 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
180 unsigned int flags)
182 struct rtnl_link *link, *old;
183 struct rtnl_link __rcu **tab;
184 int msgindex;
185 int ret = -ENOBUFS;
187 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
188 msgindex = rtm_msgindex(msgtype);
190 rtnl_lock();
191 tab = rtnl_msg_handlers[protocol];
192 if (tab == NULL) {
193 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
194 if (!tab)
195 goto unlock;
197 /* ensures we see the 0 stores */
198 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
201 old = rtnl_dereference(tab[msgindex]);
202 if (old) {
203 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
204 if (!link)
205 goto unlock;
206 } else {
207 link = kzalloc(sizeof(*link), GFP_KERNEL);
208 if (!link)
209 goto unlock;
212 WARN_ON(link->owner && link->owner != owner);
213 link->owner = owner;
215 WARN_ON(doit && link->doit && link->doit != doit);
216 if (doit)
217 link->doit = doit;
218 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
219 if (dumpit)
220 link->dumpit = dumpit;
222 link->flags |= flags;
224 /* publish protocol:msgtype */
225 rcu_assign_pointer(tab[msgindex], link);
226 ret = 0;
227 if (old)
228 kfree_rcu(old, rcu);
229 unlock:
230 rtnl_unlock();
231 return ret;
235 * rtnl_register_module - Register a rtnetlink message type
237 * @owner: module registering the hook (THIS_MODULE)
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 * @doit: Function pointer called for each request message
241 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
242 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
244 * Like rtnl_register, but for use by removable modules.
246 int rtnl_register_module(struct module *owner,
247 int protocol, int msgtype,
248 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
249 unsigned int flags)
251 return rtnl_register_internal(owner, protocol, msgtype,
252 doit, dumpit, flags);
254 EXPORT_SYMBOL_GPL(rtnl_register_module);
257 * rtnl_register - Register a rtnetlink message type
258 * @protocol: Protocol family or PF_UNSPEC
259 * @msgtype: rtnetlink message type
260 * @doit: Function pointer called for each request message
261 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
262 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
264 * Registers the specified function pointers (at least one of them has
265 * to be non-NULL) to be called whenever a request message for the
266 * specified protocol family and message type is received.
268 * The special protocol family PF_UNSPEC may be used to define fallback
269 * function pointers for the case when no entry for the specific protocol
270 * family exists.
272 void rtnl_register(int protocol, int msgtype,
273 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
274 unsigned int flags)
276 int err;
278 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
279 flags);
280 if (err)
281 pr_err("Unable to register rtnetlink message handler, "
282 "protocol = %d, message type = %d\n", protocol, msgtype);
286 * rtnl_unregister - Unregister a rtnetlink message type
287 * @protocol: Protocol family or PF_UNSPEC
288 * @msgtype: rtnetlink message type
290 * Returns 0 on success or a negative error code.
292 int rtnl_unregister(int protocol, int msgtype)
294 struct rtnl_link **tab, *link;
295 int msgindex;
297 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
298 msgindex = rtm_msgindex(msgtype);
300 rtnl_lock();
301 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 if (!tab) {
303 rtnl_unlock();
304 return -ENOENT;
307 link = tab[msgindex];
308 rcu_assign_pointer(tab[msgindex], NULL);
309 rtnl_unlock();
311 kfree_rcu(link, rcu);
313 return 0;
315 EXPORT_SYMBOL_GPL(rtnl_unregister);
318 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
319 * @protocol : Protocol family or PF_UNSPEC
321 * Identical to calling rtnl_unregster() for all registered message types
322 * of a certain protocol family.
324 void rtnl_unregister_all(int protocol)
326 struct rtnl_link **tab, *link;
327 int msgindex;
329 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
331 rtnl_lock();
332 tab = rtnl_msg_handlers[protocol];
333 if (!tab) {
334 rtnl_unlock();
335 return;
337 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
338 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
339 link = tab[msgindex];
340 if (!link)
341 continue;
343 rcu_assign_pointer(tab[msgindex], NULL);
344 kfree_rcu(link, rcu);
346 rtnl_unlock();
348 synchronize_net();
350 kfree(tab);
352 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
354 static LIST_HEAD(link_ops);
356 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
358 const struct rtnl_link_ops *ops;
360 list_for_each_entry(ops, &link_ops, list) {
361 if (!strcmp(ops->kind, kind))
362 return ops;
364 return NULL;
368 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
369 * @ops: struct rtnl_link_ops * to register
371 * The caller must hold the rtnl_mutex. This function should be used
372 * by drivers that create devices during module initialization. It
373 * must be called before registering the devices.
375 * Returns 0 on success or a negative error code.
377 int __rtnl_link_register(struct rtnl_link_ops *ops)
379 if (rtnl_link_ops_get(ops->kind))
380 return -EEXIST;
382 /* The check for setup is here because if ops
383 * does not have that filled up, it is not possible
384 * to use the ops for creating device. So do not
385 * fill up dellink as well. That disables rtnl_dellink.
387 if (ops->setup && !ops->dellink)
388 ops->dellink = unregister_netdevice_queue;
390 list_add_tail(&ops->list, &link_ops);
391 return 0;
393 EXPORT_SYMBOL_GPL(__rtnl_link_register);
396 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
397 * @ops: struct rtnl_link_ops * to register
399 * Returns 0 on success or a negative error code.
401 int rtnl_link_register(struct rtnl_link_ops *ops)
403 int err;
405 /* Sanity-check max sizes to avoid stack buffer overflow. */
406 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
407 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
408 return -EINVAL;
410 rtnl_lock();
411 err = __rtnl_link_register(ops);
412 rtnl_unlock();
413 return err;
415 EXPORT_SYMBOL_GPL(rtnl_link_register);
417 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
419 struct net_device *dev;
420 LIST_HEAD(list_kill);
422 for_each_netdev(net, dev) {
423 if (dev->rtnl_link_ops == ops)
424 ops->dellink(dev, &list_kill);
426 unregister_netdevice_many(&list_kill);
430 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
431 * @ops: struct rtnl_link_ops * to unregister
433 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
434 * integrity (hold pernet_ops_rwsem for writing to close the race
435 * with setup_net() and cleanup_net()).
437 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
439 struct net *net;
441 for_each_net(net) {
442 __rtnl_kill_links(net, ops);
444 list_del(&ops->list);
446 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
448 /* Return with the rtnl_lock held when there are no network
449 * devices unregistering in any network namespace.
451 static void rtnl_lock_unregistering_all(void)
453 struct net *net;
454 bool unregistering;
455 DEFINE_WAIT_FUNC(wait, woken_wake_function);
457 add_wait_queue(&netdev_unregistering_wq, &wait);
458 for (;;) {
459 unregistering = false;
460 rtnl_lock();
461 /* We held write locked pernet_ops_rwsem, and parallel
462 * setup_net() and cleanup_net() are not possible.
464 for_each_net(net) {
465 if (net->dev_unreg_count > 0) {
466 unregistering = true;
467 break;
470 if (!unregistering)
471 break;
472 __rtnl_unlock();
474 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
476 remove_wait_queue(&netdev_unregistering_wq, &wait);
480 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
481 * @ops: struct rtnl_link_ops * to unregister
483 void rtnl_link_unregister(struct rtnl_link_ops *ops)
485 /* Close the race with setup_net() and cleanup_net() */
486 down_write(&pernet_ops_rwsem);
487 rtnl_lock_unregistering_all();
488 __rtnl_link_unregister(ops);
489 rtnl_unlock();
490 up_write(&pernet_ops_rwsem);
492 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
494 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
496 struct net_device *master_dev;
497 const struct rtnl_link_ops *ops;
498 size_t size = 0;
500 rcu_read_lock();
502 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
503 if (!master_dev)
504 goto out;
506 ops = master_dev->rtnl_link_ops;
507 if (!ops || !ops->get_slave_size)
508 goto out;
509 /* IFLA_INFO_SLAVE_DATA + nested data */
510 size = nla_total_size(sizeof(struct nlattr)) +
511 ops->get_slave_size(master_dev, dev);
513 out:
514 rcu_read_unlock();
515 return size;
518 static size_t rtnl_link_get_size(const struct net_device *dev)
520 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size_t size;
523 if (!ops)
524 return 0;
526 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
527 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
529 if (ops->get_size)
530 /* IFLA_INFO_DATA + nested data */
531 size += nla_total_size(sizeof(struct nlattr)) +
532 ops->get_size(dev);
534 if (ops->get_xstats_size)
535 /* IFLA_INFO_XSTATS */
536 size += nla_total_size(ops->get_xstats_size(dev));
538 size += rtnl_link_get_slave_info_data_size(dev);
540 return size;
543 static LIST_HEAD(rtnl_af_ops);
545 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
547 const struct rtnl_af_ops *ops;
549 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
550 if (ops->family == family)
551 return ops;
554 return NULL;
558 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
559 * @ops: struct rtnl_af_ops * to register
561 * Returns 0 on success or a negative error code.
563 void rtnl_af_register(struct rtnl_af_ops *ops)
565 rtnl_lock();
566 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
567 rtnl_unlock();
569 EXPORT_SYMBOL_GPL(rtnl_af_register);
572 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
573 * @ops: struct rtnl_af_ops * to unregister
575 void rtnl_af_unregister(struct rtnl_af_ops *ops)
577 rtnl_lock();
578 list_del_rcu(&ops->list);
579 rtnl_unlock();
581 synchronize_rcu();
583 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
585 static size_t rtnl_link_get_af_size(const struct net_device *dev,
586 u32 ext_filter_mask)
588 struct rtnl_af_ops *af_ops;
589 size_t size;
591 /* IFLA_AF_SPEC */
592 size = nla_total_size(sizeof(struct nlattr));
594 rcu_read_lock();
595 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
596 if (af_ops->get_link_af_size) {
597 /* AF_* + nested data */
598 size += nla_total_size(sizeof(struct nlattr)) +
599 af_ops->get_link_af_size(dev, ext_filter_mask);
602 rcu_read_unlock();
604 return size;
607 static bool rtnl_have_link_slave_info(const struct net_device *dev)
609 struct net_device *master_dev;
610 bool ret = false;
612 rcu_read_lock();
614 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
615 if (master_dev && master_dev->rtnl_link_ops)
616 ret = true;
617 rcu_read_unlock();
618 return ret;
621 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
622 const struct net_device *dev)
624 struct net_device *master_dev;
625 const struct rtnl_link_ops *ops;
626 struct nlattr *slave_data;
627 int err;
629 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
630 if (!master_dev)
631 return 0;
632 ops = master_dev->rtnl_link_ops;
633 if (!ops)
634 return 0;
635 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
636 return -EMSGSIZE;
637 if (ops->fill_slave_info) {
638 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
639 if (!slave_data)
640 return -EMSGSIZE;
641 err = ops->fill_slave_info(skb, master_dev, dev);
642 if (err < 0)
643 goto err_cancel_slave_data;
644 nla_nest_end(skb, slave_data);
646 return 0;
648 err_cancel_slave_data:
649 nla_nest_cancel(skb, slave_data);
650 return err;
653 static int rtnl_link_info_fill(struct sk_buff *skb,
654 const struct net_device *dev)
656 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
657 struct nlattr *data;
658 int err;
660 if (!ops)
661 return 0;
662 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
663 return -EMSGSIZE;
664 if (ops->fill_xstats) {
665 err = ops->fill_xstats(skb, dev);
666 if (err < 0)
667 return err;
669 if (ops->fill_info) {
670 data = nla_nest_start(skb, IFLA_INFO_DATA);
671 if (data == NULL)
672 return -EMSGSIZE;
673 err = ops->fill_info(skb, dev);
674 if (err < 0)
675 goto err_cancel_data;
676 nla_nest_end(skb, data);
678 return 0;
680 err_cancel_data:
681 nla_nest_cancel(skb, data);
682 return err;
685 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
687 struct nlattr *linkinfo;
688 int err = -EMSGSIZE;
690 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
691 if (linkinfo == NULL)
692 goto out;
694 err = rtnl_link_info_fill(skb, dev);
695 if (err < 0)
696 goto err_cancel_link;
698 err = rtnl_link_slave_info_fill(skb, dev);
699 if (err < 0)
700 goto err_cancel_link;
702 nla_nest_end(skb, linkinfo);
703 return 0;
705 err_cancel_link:
706 nla_nest_cancel(skb, linkinfo);
707 out:
708 return err;
711 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
713 struct sock *rtnl = net->rtnl;
714 int err = 0;
716 NETLINK_CB(skb).dst_group = group;
717 if (echo)
718 refcount_inc(&skb->users);
719 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
720 if (echo)
721 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
722 return err;
725 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
727 struct sock *rtnl = net->rtnl;
729 return nlmsg_unicast(rtnl, skb, pid);
731 EXPORT_SYMBOL(rtnl_unicast);
733 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
734 struct nlmsghdr *nlh, gfp_t flags)
736 struct sock *rtnl = net->rtnl;
737 int report = 0;
739 if (nlh)
740 report = nlmsg_report(nlh);
742 nlmsg_notify(rtnl, skb, pid, group, report, flags);
744 EXPORT_SYMBOL(rtnl_notify);
746 void rtnl_set_sk_err(struct net *net, u32 group, int error)
748 struct sock *rtnl = net->rtnl;
750 netlink_set_err(rtnl, 0, group, error);
752 EXPORT_SYMBOL(rtnl_set_sk_err);
754 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
756 struct nlattr *mx;
757 int i, valid = 0;
759 mx = nla_nest_start(skb, RTA_METRICS);
760 if (mx == NULL)
761 return -ENOBUFS;
763 for (i = 0; i < RTAX_MAX; i++) {
764 if (metrics[i]) {
765 if (i == RTAX_CC_ALGO - 1) {
766 char tmp[TCP_CA_NAME_MAX], *name;
768 name = tcp_ca_get_name_by_key(metrics[i], tmp);
769 if (!name)
770 continue;
771 if (nla_put_string(skb, i + 1, name))
772 goto nla_put_failure;
773 } else if (i == RTAX_FEATURES - 1) {
774 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
776 if (!user_features)
777 continue;
778 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
779 if (nla_put_u32(skb, i + 1, user_features))
780 goto nla_put_failure;
781 } else {
782 if (nla_put_u32(skb, i + 1, metrics[i]))
783 goto nla_put_failure;
785 valid++;
789 if (!valid) {
790 nla_nest_cancel(skb, mx);
791 return 0;
794 return nla_nest_end(skb, mx);
796 nla_put_failure:
797 nla_nest_cancel(skb, mx);
798 return -EMSGSIZE;
800 EXPORT_SYMBOL(rtnetlink_put_metrics);
802 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
803 long expires, u32 error)
805 struct rta_cacheinfo ci = {
806 .rta_error = error,
807 .rta_id = id,
810 if (dst) {
811 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
812 ci.rta_used = dst->__use;
813 ci.rta_clntref = atomic_read(&dst->__refcnt);
815 if (expires) {
816 unsigned long clock;
818 clock = jiffies_to_clock_t(abs(expires));
819 clock = min_t(unsigned long, clock, INT_MAX);
820 ci.rta_expires = (expires > 0) ? clock : -clock;
822 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
824 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
826 static void set_operstate(struct net_device *dev, unsigned char transition)
828 unsigned char operstate = dev->operstate;
830 switch (transition) {
831 case IF_OPER_UP:
832 if ((operstate == IF_OPER_DORMANT ||
833 operstate == IF_OPER_UNKNOWN) &&
834 !netif_dormant(dev))
835 operstate = IF_OPER_UP;
836 break;
838 case IF_OPER_DORMANT:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_DORMANT;
842 break;
845 if (dev->operstate != operstate) {
846 write_lock_bh(&dev_base_lock);
847 dev->operstate = operstate;
848 write_unlock_bh(&dev_base_lock);
849 netdev_state_change(dev);
853 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
855 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
856 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
859 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
860 const struct ifinfomsg *ifm)
862 unsigned int flags = ifm->ifi_flags;
864 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 if (ifm->ifi_change)
866 flags = (flags & ifm->ifi_change) |
867 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
869 return flags;
872 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
873 const struct rtnl_link_stats64 *b)
875 a->rx_packets = b->rx_packets;
876 a->tx_packets = b->tx_packets;
877 a->rx_bytes = b->rx_bytes;
878 a->tx_bytes = b->tx_bytes;
879 a->rx_errors = b->rx_errors;
880 a->tx_errors = b->tx_errors;
881 a->rx_dropped = b->rx_dropped;
882 a->tx_dropped = b->tx_dropped;
884 a->multicast = b->multicast;
885 a->collisions = b->collisions;
887 a->rx_length_errors = b->rx_length_errors;
888 a->rx_over_errors = b->rx_over_errors;
889 a->rx_crc_errors = b->rx_crc_errors;
890 a->rx_frame_errors = b->rx_frame_errors;
891 a->rx_fifo_errors = b->rx_fifo_errors;
892 a->rx_missed_errors = b->rx_missed_errors;
894 a->tx_aborted_errors = b->tx_aborted_errors;
895 a->tx_carrier_errors = b->tx_carrier_errors;
896 a->tx_fifo_errors = b->tx_fifo_errors;
897 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
898 a->tx_window_errors = b->tx_window_errors;
900 a->rx_compressed = b->rx_compressed;
901 a->tx_compressed = b->tx_compressed;
903 a->rx_nohandler = b->rx_nohandler;
906 /* All VF info */
907 static inline int rtnl_vfinfo_size(const struct net_device *dev,
908 u32 ext_filter_mask)
910 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
911 int num_vfs = dev_num_vf(dev->dev.parent);
912 size_t size = nla_total_size(0);
913 size += num_vfs *
914 (nla_total_size(0) +
915 nla_total_size(sizeof(struct ifla_vf_mac)) +
916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
922 nla_total_size(sizeof(struct ifla_vf_rate)) +
923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
925 nla_total_size(0) + /* nest IFLA_VF_STATS */
926 /* IFLA_VF_STATS_RX_PACKETS */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_TX_PACKETS */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_RX_BYTES */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_TX_BYTES */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_BROADCAST */
935 nla_total_size_64bit(sizeof(__u64)) +
936 /* IFLA_VF_STATS_MULTICAST */
937 nla_total_size_64bit(sizeof(__u64)) +
938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
942 nla_total_size(sizeof(struct ifla_vf_trust)));
943 return size;
944 } else
945 return 0;
948 static size_t rtnl_port_size(const struct net_device *dev,
949 u32 ext_filter_mask)
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
959 + port_size;
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
961 + port_size;
963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
965 return 0;
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
969 else
970 return port_self_size;
973 static size_t rtnl_xdp_size(void)
975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
976 nla_total_size(1) + /* XDP_ATTACHED */
977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
980 return xdp_size;
983 static noinline size_t if_nlmsg_size(const struct net_device *dev,
984 u32 ext_filter_mask)
986 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
987 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
988 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
989 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
990 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
991 + nla_total_size(sizeof(struct rtnl_link_stats))
992 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
993 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
994 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
995 + nla_total_size(4) /* IFLA_TXQLEN */
996 + nla_total_size(4) /* IFLA_WEIGHT */
997 + nla_total_size(4) /* IFLA_MTU */
998 + nla_total_size(4) /* IFLA_LINK */
999 + nla_total_size(4) /* IFLA_MASTER */
1000 + nla_total_size(1) /* IFLA_CARRIER */
1001 + nla_total_size(4) /* IFLA_PROMISCUITY */
1002 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1003 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1004 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1005 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1006 + nla_total_size(1) /* IFLA_OPERSTATE */
1007 + nla_total_size(1) /* IFLA_LINKMODE */
1008 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1009 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1010 + nla_total_size(4) /* IFLA_GROUP */
1011 + nla_total_size(ext_filter_mask
1012 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1013 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1014 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1015 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1016 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1017 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1018 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1019 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1020 + rtnl_xdp_size() /* IFLA_XDP */
1021 + nla_total_size(4) /* IFLA_EVENT */
1022 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1023 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1024 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1025 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1026 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1027 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1028 + nla_total_size(4) /* IFLA_MIN_MTU */
1029 + nla_total_size(4) /* IFLA_MAX_MTU */
1030 + 0;
1033 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1035 struct nlattr *vf_ports;
1036 struct nlattr *vf_port;
1037 int vf;
1038 int err;
1040 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1041 if (!vf_ports)
1042 return -EMSGSIZE;
1044 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1045 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
1046 if (!vf_port)
1047 goto nla_put_failure;
1048 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1049 goto nla_put_failure;
1050 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1051 if (err == -EMSGSIZE)
1052 goto nla_put_failure;
1053 if (err) {
1054 nla_nest_cancel(skb, vf_port);
1055 continue;
1057 nla_nest_end(skb, vf_port);
1060 nla_nest_end(skb, vf_ports);
1062 return 0;
1064 nla_put_failure:
1065 nla_nest_cancel(skb, vf_ports);
1066 return -EMSGSIZE;
1069 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1071 struct nlattr *port_self;
1072 int err;
1074 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1075 if (!port_self)
1076 return -EMSGSIZE;
1078 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1079 if (err) {
1080 nla_nest_cancel(skb, port_self);
1081 return (err == -EMSGSIZE) ? err : 0;
1084 nla_nest_end(skb, port_self);
1086 return 0;
1089 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1090 u32 ext_filter_mask)
1092 int err;
1094 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1095 !(ext_filter_mask & RTEXT_FILTER_VF))
1096 return 0;
1098 err = rtnl_port_self_fill(skb, dev);
1099 if (err)
1100 return err;
1102 if (dev_num_vf(dev->dev.parent)) {
1103 err = rtnl_vf_ports_fill(skb, dev);
1104 if (err)
1105 return err;
1108 return 0;
1111 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1113 int err;
1114 struct netdev_phys_item_id ppid;
1116 err = dev_get_phys_port_id(dev, &ppid);
1117 if (err) {
1118 if (err == -EOPNOTSUPP)
1119 return 0;
1120 return err;
1123 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1124 return -EMSGSIZE;
1126 return 0;
1129 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1131 char name[IFNAMSIZ];
1132 int err;
1134 err = dev_get_phys_port_name(dev, name, sizeof(name));
1135 if (err) {
1136 if (err == -EOPNOTSUPP)
1137 return 0;
1138 return err;
1141 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1142 return -EMSGSIZE;
1144 return 0;
1147 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1149 int err;
1150 struct switchdev_attr attr = {
1151 .orig_dev = dev,
1152 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1153 .flags = SWITCHDEV_F_NO_RECURSE,
1156 err = switchdev_port_attr_get(dev, &attr);
1157 if (err) {
1158 if (err == -EOPNOTSUPP)
1159 return 0;
1160 return err;
1163 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1164 attr.u.ppid.id))
1165 return -EMSGSIZE;
1167 return 0;
1170 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1171 struct net_device *dev)
1173 struct rtnl_link_stats64 *sp;
1174 struct nlattr *attr;
1176 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1177 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1178 if (!attr)
1179 return -EMSGSIZE;
1181 sp = nla_data(attr);
1182 dev_get_stats(dev, sp);
1184 attr = nla_reserve(skb, IFLA_STATS,
1185 sizeof(struct rtnl_link_stats));
1186 if (!attr)
1187 return -EMSGSIZE;
1189 copy_rtnl_link_stats(nla_data(attr), sp);
1191 return 0;
1194 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1195 struct net_device *dev,
1196 int vfs_num,
1197 struct nlattr *vfinfo)
1199 struct ifla_vf_rss_query_en vf_rss_query_en;
1200 struct nlattr *vf, *vfstats, *vfvlanlist;
1201 struct ifla_vf_link_state vf_linkstate;
1202 struct ifla_vf_vlan_info vf_vlan_info;
1203 struct ifla_vf_spoofchk vf_spoofchk;
1204 struct ifla_vf_tx_rate vf_tx_rate;
1205 struct ifla_vf_stats vf_stats;
1206 struct ifla_vf_trust vf_trust;
1207 struct ifla_vf_vlan vf_vlan;
1208 struct ifla_vf_rate vf_rate;
1209 struct ifla_vf_mac vf_mac;
1210 struct ifla_vf_info ivi;
1212 memset(&ivi, 0, sizeof(ivi));
1214 /* Not all SR-IOV capable drivers support the
1215 * spoofcheck and "RSS query enable" query. Preset to
1216 * -1 so the user space tool can detect that the driver
1217 * didn't report anything.
1219 ivi.spoofchk = -1;
1220 ivi.rss_query_en = -1;
1221 ivi.trusted = -1;
1222 /* The default value for VF link state is "auto"
1223 * IFLA_VF_LINK_STATE_AUTO which equals zero
1225 ivi.linkstate = 0;
1226 /* VLAN Protocol by default is 802.1Q */
1227 ivi.vlan_proto = htons(ETH_P_8021Q);
1228 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1229 return 0;
1231 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1233 vf_mac.vf =
1234 vf_vlan.vf =
1235 vf_vlan_info.vf =
1236 vf_rate.vf =
1237 vf_tx_rate.vf =
1238 vf_spoofchk.vf =
1239 vf_linkstate.vf =
1240 vf_rss_query_en.vf =
1241 vf_trust.vf = ivi.vf;
1243 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1244 vf_vlan.vlan = ivi.vlan;
1245 vf_vlan.qos = ivi.qos;
1246 vf_vlan_info.vlan = ivi.vlan;
1247 vf_vlan_info.qos = ivi.qos;
1248 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1249 vf_tx_rate.rate = ivi.max_tx_rate;
1250 vf_rate.min_tx_rate = ivi.min_tx_rate;
1251 vf_rate.max_tx_rate = ivi.max_tx_rate;
1252 vf_spoofchk.setting = ivi.spoofchk;
1253 vf_linkstate.link_state = ivi.linkstate;
1254 vf_rss_query_en.setting = ivi.rss_query_en;
1255 vf_trust.setting = ivi.trusted;
1256 vf = nla_nest_start(skb, IFLA_VF_INFO);
1257 if (!vf)
1258 goto nla_put_vfinfo_failure;
1259 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1260 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1261 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1262 &vf_rate) ||
1263 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1264 &vf_tx_rate) ||
1265 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1266 &vf_spoofchk) ||
1267 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1268 &vf_linkstate) ||
1269 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1270 sizeof(vf_rss_query_en),
1271 &vf_rss_query_en) ||
1272 nla_put(skb, IFLA_VF_TRUST,
1273 sizeof(vf_trust), &vf_trust))
1274 goto nla_put_vf_failure;
1275 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1276 if (!vfvlanlist)
1277 goto nla_put_vf_failure;
1278 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1279 &vf_vlan_info)) {
1280 nla_nest_cancel(skb, vfvlanlist);
1281 goto nla_put_vf_failure;
1283 nla_nest_end(skb, vfvlanlist);
1284 memset(&vf_stats, 0, sizeof(vf_stats));
1285 if (dev->netdev_ops->ndo_get_vf_stats)
1286 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1287 &vf_stats);
1288 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1289 if (!vfstats)
1290 goto nla_put_vf_failure;
1291 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1292 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1294 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1296 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1298 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1300 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1301 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1302 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1303 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1304 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1305 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1306 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1307 nla_nest_cancel(skb, vfstats);
1308 goto nla_put_vf_failure;
1310 nla_nest_end(skb, vfstats);
1311 nla_nest_end(skb, vf);
1312 return 0;
1314 nla_put_vf_failure:
1315 nla_nest_cancel(skb, vf);
1316 nla_put_vfinfo_failure:
1317 nla_nest_cancel(skb, vfinfo);
1318 return -EMSGSIZE;
1321 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1322 struct net_device *dev,
1323 u32 ext_filter_mask)
1325 struct nlattr *vfinfo;
1326 int i, num_vfs;
1328 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1329 return 0;
1331 num_vfs = dev_num_vf(dev->dev.parent);
1332 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1333 return -EMSGSIZE;
1335 if (!dev->netdev_ops->ndo_get_vf_config)
1336 return 0;
1338 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1339 if (!vfinfo)
1340 return -EMSGSIZE;
1342 for (i = 0; i < num_vfs; i++) {
1343 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1344 return -EMSGSIZE;
1347 nla_nest_end(skb, vfinfo);
1348 return 0;
1351 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1353 struct rtnl_link_ifmap map;
1355 memset(&map, 0, sizeof(map));
1356 map.mem_start = dev->mem_start;
1357 map.mem_end = dev->mem_end;
1358 map.base_addr = dev->base_addr;
1359 map.irq = dev->irq;
1360 map.dma = dev->dma;
1361 map.port = dev->if_port;
1363 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1364 return -EMSGSIZE;
1366 return 0;
1369 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1371 const struct bpf_prog *generic_xdp_prog;
1373 ASSERT_RTNL();
1375 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1376 if (!generic_xdp_prog)
1377 return 0;
1378 return generic_xdp_prog->aux->id;
1381 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1383 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1386 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1388 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1389 XDP_QUERY_PROG_HW);
1392 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1393 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1394 u32 (*get_prog_id)(struct net_device *dev))
1396 u32 curr_id;
1397 int err;
1399 curr_id = get_prog_id(dev);
1400 if (!curr_id)
1401 return 0;
1403 *prog_id = curr_id;
1404 err = nla_put_u32(skb, attr, curr_id);
1405 if (err)
1406 return err;
1408 if (*mode != XDP_ATTACHED_NONE)
1409 *mode = XDP_ATTACHED_MULTI;
1410 else
1411 *mode = tgt_mode;
1413 return 0;
1416 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1418 struct nlattr *xdp;
1419 u32 prog_id;
1420 int err;
1421 u8 mode;
1423 xdp = nla_nest_start(skb, IFLA_XDP);
1424 if (!xdp)
1425 return -EMSGSIZE;
1427 prog_id = 0;
1428 mode = XDP_ATTACHED_NONE;
1429 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1430 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1431 if (err)
1432 goto err_cancel;
1433 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1434 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1435 if (err)
1436 goto err_cancel;
1437 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1438 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1439 if (err)
1440 goto err_cancel;
1442 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1443 if (err)
1444 goto err_cancel;
1446 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1447 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1448 if (err)
1449 goto err_cancel;
1452 nla_nest_end(skb, xdp);
1453 return 0;
1455 err_cancel:
1456 nla_nest_cancel(skb, xdp);
1457 return err;
1460 static u32 rtnl_get_event(unsigned long event)
1462 u32 rtnl_event_type = IFLA_EVENT_NONE;
1464 switch (event) {
1465 case NETDEV_REBOOT:
1466 rtnl_event_type = IFLA_EVENT_REBOOT;
1467 break;
1468 case NETDEV_FEAT_CHANGE:
1469 rtnl_event_type = IFLA_EVENT_FEATURES;
1470 break;
1471 case NETDEV_BONDING_FAILOVER:
1472 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1473 break;
1474 case NETDEV_NOTIFY_PEERS:
1475 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1476 break;
1477 case NETDEV_RESEND_IGMP:
1478 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1479 break;
1480 case NETDEV_CHANGEINFODATA:
1481 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1482 break;
1483 default:
1484 break;
1487 return rtnl_event_type;
1490 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1492 const struct net_device *upper_dev;
1493 int ret = 0;
1495 rcu_read_lock();
1497 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1498 if (upper_dev)
1499 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1501 rcu_read_unlock();
1502 return ret;
1505 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1507 int ifindex = dev_get_iflink(dev);
1509 if (dev->ifindex == ifindex)
1510 return 0;
1512 return nla_put_u32(skb, IFLA_LINK, ifindex);
1515 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1516 struct net_device *dev)
1518 char buf[IFALIASZ];
1519 int ret;
1521 ret = dev_get_alias(dev, buf, sizeof(buf));
1522 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1525 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1526 const struct net_device *dev,
1527 struct net *src_net)
1529 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1530 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1532 if (!net_eq(dev_net(dev), link_net)) {
1533 int id = peernet2id_alloc(src_net, link_net);
1535 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1536 return -EMSGSIZE;
1540 return 0;
1543 static int rtnl_fill_link_af(struct sk_buff *skb,
1544 const struct net_device *dev,
1545 u32 ext_filter_mask)
1547 const struct rtnl_af_ops *af_ops;
1548 struct nlattr *af_spec;
1550 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1551 if (!af_spec)
1552 return -EMSGSIZE;
1554 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1555 struct nlattr *af;
1556 int err;
1558 if (!af_ops->fill_link_af)
1559 continue;
1561 af = nla_nest_start(skb, af_ops->family);
1562 if (!af)
1563 return -EMSGSIZE;
1565 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1567 * Caller may return ENODATA to indicate that there
1568 * was no data to be dumped. This is not an error, it
1569 * means we should trim the attribute header and
1570 * continue.
1572 if (err == -ENODATA)
1573 nla_nest_cancel(skb, af);
1574 else if (err < 0)
1575 return -EMSGSIZE;
1577 nla_nest_end(skb, af);
1580 nla_nest_end(skb, af_spec);
1581 return 0;
1584 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1585 struct net_device *dev, struct net *src_net,
1586 int type, u32 pid, u32 seq, u32 change,
1587 unsigned int flags, u32 ext_filter_mask,
1588 u32 event, int *new_nsid, int new_ifindex,
1589 int tgt_netnsid)
1591 struct ifinfomsg *ifm;
1592 struct nlmsghdr *nlh;
1594 ASSERT_RTNL();
1595 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1596 if (nlh == NULL)
1597 return -EMSGSIZE;
1599 ifm = nlmsg_data(nlh);
1600 ifm->ifi_family = AF_UNSPEC;
1601 ifm->__ifi_pad = 0;
1602 ifm->ifi_type = dev->type;
1603 ifm->ifi_index = dev->ifindex;
1604 ifm->ifi_flags = dev_get_flags(dev);
1605 ifm->ifi_change = change;
1607 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1608 goto nla_put_failure;
1610 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1611 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1612 nla_put_u8(skb, IFLA_OPERSTATE,
1613 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1614 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1615 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1616 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1617 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1618 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1619 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1620 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1621 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1622 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1623 #ifdef CONFIG_RPS
1624 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1625 #endif
1626 nla_put_iflink(skb, dev) ||
1627 put_master_ifindex(skb, dev) ||
1628 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1629 (dev->qdisc &&
1630 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1631 nla_put_ifalias(skb, dev) ||
1632 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1633 atomic_read(&dev->carrier_up_count) +
1634 atomic_read(&dev->carrier_down_count)) ||
1635 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1636 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1637 atomic_read(&dev->carrier_up_count)) ||
1638 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1639 atomic_read(&dev->carrier_down_count)))
1640 goto nla_put_failure;
1642 if (event != IFLA_EVENT_NONE) {
1643 if (nla_put_u32(skb, IFLA_EVENT, event))
1644 goto nla_put_failure;
1647 if (rtnl_fill_link_ifmap(skb, dev))
1648 goto nla_put_failure;
1650 if (dev->addr_len) {
1651 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1652 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1653 goto nla_put_failure;
1656 if (rtnl_phys_port_id_fill(skb, dev))
1657 goto nla_put_failure;
1659 if (rtnl_phys_port_name_fill(skb, dev))
1660 goto nla_put_failure;
1662 if (rtnl_phys_switch_id_fill(skb, dev))
1663 goto nla_put_failure;
1665 if (rtnl_fill_stats(skb, dev))
1666 goto nla_put_failure;
1668 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1669 goto nla_put_failure;
1671 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1672 goto nla_put_failure;
1674 if (rtnl_xdp_fill(skb, dev))
1675 goto nla_put_failure;
1677 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1678 if (rtnl_link_fill(skb, dev) < 0)
1679 goto nla_put_failure;
1682 if (rtnl_fill_link_netnsid(skb, dev, src_net))
1683 goto nla_put_failure;
1685 if (new_nsid &&
1686 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1687 goto nla_put_failure;
1688 if (new_ifindex &&
1689 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1690 goto nla_put_failure;
1693 rcu_read_lock();
1694 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1695 goto nla_put_failure_rcu;
1696 rcu_read_unlock();
1698 nlmsg_end(skb, nlh);
1699 return 0;
1701 nla_put_failure_rcu:
1702 rcu_read_unlock();
1703 nla_put_failure:
1704 nlmsg_cancel(skb, nlh);
1705 return -EMSGSIZE;
1708 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1709 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1710 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1711 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1712 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1713 [IFLA_MTU] = { .type = NLA_U32 },
1714 [IFLA_LINK] = { .type = NLA_U32 },
1715 [IFLA_MASTER] = { .type = NLA_U32 },
1716 [IFLA_CARRIER] = { .type = NLA_U8 },
1717 [IFLA_TXQLEN] = { .type = NLA_U32 },
1718 [IFLA_WEIGHT] = { .type = NLA_U32 },
1719 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1720 [IFLA_LINKMODE] = { .type = NLA_U8 },
1721 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1722 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1723 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1724 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1725 * allow 0-length string (needed to remove an alias).
1727 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1728 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1729 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1730 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1731 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1732 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1733 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1734 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1735 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1736 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1737 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1738 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1739 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1740 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1741 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1742 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1743 [IFLA_XDP] = { .type = NLA_NESTED },
1744 [IFLA_EVENT] = { .type = NLA_U32 },
1745 [IFLA_GROUP] = { .type = NLA_U32 },
1746 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1747 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1748 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1749 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1750 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1753 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1754 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1756 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1757 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1760 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1761 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1762 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1763 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1764 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1765 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1766 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1767 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1768 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1769 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1770 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1771 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1772 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1775 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1776 [IFLA_PORT_VF] = { .type = NLA_U32 },
1777 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1778 .len = PORT_PROFILE_MAX },
1779 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1780 .len = PORT_UUID_MAX },
1781 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1782 .len = PORT_UUID_MAX },
1783 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1784 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1786 /* Unused, but we need to keep it here since user space could
1787 * fill it. It's also broken with regard to NLA_BINARY use in
1788 * combination with structs.
1790 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1791 .len = sizeof(struct ifla_port_vsi) },
1794 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1795 [IFLA_XDP_FD] = { .type = NLA_S32 },
1796 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1797 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1798 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1801 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1803 const struct rtnl_link_ops *ops = NULL;
1804 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1806 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1807 ifla_info_policy, NULL) < 0)
1808 return NULL;
1810 if (linfo[IFLA_INFO_KIND]) {
1811 char kind[MODULE_NAME_LEN];
1813 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1814 ops = rtnl_link_ops_get(kind);
1817 return ops;
1820 static bool link_master_filtered(struct net_device *dev, int master_idx)
1822 struct net_device *master;
1824 if (!master_idx)
1825 return false;
1827 master = netdev_master_upper_dev_get(dev);
1828 if (!master || master->ifindex != master_idx)
1829 return true;
1831 return false;
1834 static bool link_kind_filtered(const struct net_device *dev,
1835 const struct rtnl_link_ops *kind_ops)
1837 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1838 return true;
1840 return false;
1843 static bool link_dump_filtered(struct net_device *dev,
1844 int master_idx,
1845 const struct rtnl_link_ops *kind_ops)
1847 if (link_master_filtered(dev, master_idx) ||
1848 link_kind_filtered(dev, kind_ops))
1849 return true;
1851 return false;
1855 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1856 * @sk: netlink socket
1857 * @netnsid: network namespace identifier
1859 * Returns the network namespace identified by netnsid on success or an error
1860 * pointer on failure.
1862 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1864 struct net *net;
1866 net = get_net_ns_by_id(sock_net(sk), netnsid);
1867 if (!net)
1868 return ERR_PTR(-EINVAL);
1870 /* For now, the caller is required to have CAP_NET_ADMIN in
1871 * the user namespace owning the target net ns.
1873 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1874 put_net(net);
1875 return ERR_PTR(-EACCES);
1877 return net;
1879 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1881 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1882 bool strict_check, struct nlattr **tb,
1883 struct netlink_ext_ack *extack)
1885 int hdrlen;
1887 if (strict_check) {
1888 struct ifinfomsg *ifm;
1890 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1891 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1892 return -EINVAL;
1895 ifm = nlmsg_data(nlh);
1896 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1897 ifm->ifi_change) {
1898 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1899 return -EINVAL;
1901 if (ifm->ifi_index) {
1902 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1903 return -EINVAL;
1906 return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
1907 ifla_policy, extack);
1910 /* A hack to preserve kernel<->userspace interface.
1911 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1912 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1913 * what iproute2 < v3.9.0 used.
1914 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1915 * attribute, its netlink message is shorter than struct ifinfomsg.
1917 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1918 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1920 return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack);
1923 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1925 struct netlink_ext_ack *extack = cb->extack;
1926 const struct nlmsghdr *nlh = cb->nlh;
1927 struct net *net = sock_net(skb->sk);
1928 struct net *tgt_net = net;
1929 int h, s_h;
1930 int idx = 0, s_idx;
1931 struct net_device *dev;
1932 struct hlist_head *head;
1933 struct nlattr *tb[IFLA_MAX+1];
1934 u32 ext_filter_mask = 0;
1935 const struct rtnl_link_ops *kind_ops = NULL;
1936 unsigned int flags = NLM_F_MULTI;
1937 int master_idx = 0;
1938 int netnsid = -1;
1939 int err, i;
1941 s_h = cb->args[0];
1942 s_idx = cb->args[1];
1944 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1945 if (err < 0) {
1946 if (cb->strict_check)
1947 return err;
1949 goto walk_entries;
1952 for (i = 0; i <= IFLA_MAX; ++i) {
1953 if (!tb[i])
1954 continue;
1956 /* new attributes should only be added with strict checking */
1957 switch (i) {
1958 case IFLA_TARGET_NETNSID:
1959 netnsid = nla_get_s32(tb[i]);
1960 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1961 if (IS_ERR(tgt_net)) {
1962 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1963 return PTR_ERR(tgt_net);
1965 break;
1966 case IFLA_EXT_MASK:
1967 ext_filter_mask = nla_get_u32(tb[i]);
1968 break;
1969 case IFLA_MASTER:
1970 master_idx = nla_get_u32(tb[i]);
1971 break;
1972 case IFLA_LINKINFO:
1973 kind_ops = linkinfo_to_kind_ops(tb[i]);
1974 break;
1975 default:
1976 if (cb->strict_check) {
1977 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1978 return -EINVAL;
1983 if (master_idx || kind_ops)
1984 flags |= NLM_F_DUMP_FILTERED;
1986 walk_entries:
1987 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1988 idx = 0;
1989 head = &tgt_net->dev_index_head[h];
1990 hlist_for_each_entry(dev, head, index_hlist) {
1991 if (link_dump_filtered(dev, master_idx, kind_ops))
1992 goto cont;
1993 if (idx < s_idx)
1994 goto cont;
1995 err = rtnl_fill_ifinfo(skb, dev, net,
1996 RTM_NEWLINK,
1997 NETLINK_CB(cb->skb).portid,
1998 nlh->nlmsg_seq, 0, flags,
1999 ext_filter_mask, 0, NULL, 0,
2000 netnsid);
2002 if (err < 0) {
2003 if (likely(skb->len))
2004 goto out;
2006 goto out_err;
2008 cont:
2009 idx++;
2012 out:
2013 err = skb->len;
2014 out_err:
2015 cb->args[1] = idx;
2016 cb->args[0] = h;
2017 cb->seq = net->dev_base_seq;
2018 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2019 if (netnsid >= 0)
2020 put_net(tgt_net);
2022 return err;
2025 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2026 struct netlink_ext_ack *exterr)
2028 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
2030 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2032 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2034 struct net *net;
2035 /* Examine the link attributes and figure out which
2036 * network namespace we are talking about.
2038 if (tb[IFLA_NET_NS_PID])
2039 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2040 else if (tb[IFLA_NET_NS_FD])
2041 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2042 else
2043 net = get_net(src_net);
2044 return net;
2046 EXPORT_SYMBOL(rtnl_link_get_net);
2048 /* Figure out which network namespace we are talking about by
2049 * examining the link attributes in the following order:
2051 * 1. IFLA_NET_NS_PID
2052 * 2. IFLA_NET_NS_FD
2053 * 3. IFLA_TARGET_NETNSID
2055 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2056 struct nlattr *tb[])
2058 struct net *net;
2060 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2061 return rtnl_link_get_net(src_net, tb);
2063 if (!tb[IFLA_TARGET_NETNSID])
2064 return get_net(src_net);
2066 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2067 if (!net)
2068 return ERR_PTR(-EINVAL);
2070 return net;
2073 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2074 struct net *src_net,
2075 struct nlattr *tb[], int cap)
2077 struct net *net;
2079 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2080 if (IS_ERR(net))
2081 return net;
2083 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2084 put_net(net);
2085 return ERR_PTR(-EPERM);
2088 return net;
2091 /* Verify that rtnetlink requests do not pass additional properties
2092 * potentially referring to different network namespaces.
2094 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2095 struct netlink_ext_ack *extack,
2096 bool netns_id_only)
2099 if (netns_id_only) {
2100 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2101 return 0;
2103 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2104 return -EOPNOTSUPP;
2107 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2108 goto invalid_attr;
2110 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2111 goto invalid_attr;
2113 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2114 goto invalid_attr;
2116 return 0;
2118 invalid_attr:
2119 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2120 return -EINVAL;
2123 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2125 if (dev) {
2126 if (tb[IFLA_ADDRESS] &&
2127 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2128 return -EINVAL;
2130 if (tb[IFLA_BROADCAST] &&
2131 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2132 return -EINVAL;
2135 if (tb[IFLA_AF_SPEC]) {
2136 struct nlattr *af;
2137 int rem, err;
2139 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2140 const struct rtnl_af_ops *af_ops;
2142 rcu_read_lock();
2143 af_ops = rtnl_af_lookup(nla_type(af));
2144 if (!af_ops) {
2145 rcu_read_unlock();
2146 return -EAFNOSUPPORT;
2149 if (!af_ops->set_link_af) {
2150 rcu_read_unlock();
2151 return -EOPNOTSUPP;
2154 if (af_ops->validate_link_af) {
2155 err = af_ops->validate_link_af(dev, af);
2156 if (err < 0) {
2157 rcu_read_unlock();
2158 return err;
2162 rcu_read_unlock();
2166 return 0;
2169 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2170 int guid_type)
2172 const struct net_device_ops *ops = dev->netdev_ops;
2174 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2177 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2179 if (dev->type != ARPHRD_INFINIBAND)
2180 return -EOPNOTSUPP;
2182 return handle_infiniband_guid(dev, ivt, guid_type);
2185 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2187 const struct net_device_ops *ops = dev->netdev_ops;
2188 int err = -EINVAL;
2190 if (tb[IFLA_VF_MAC]) {
2191 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2193 err = -EOPNOTSUPP;
2194 if (ops->ndo_set_vf_mac)
2195 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2196 ivm->mac);
2197 if (err < 0)
2198 return err;
2201 if (tb[IFLA_VF_VLAN]) {
2202 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2204 err = -EOPNOTSUPP;
2205 if (ops->ndo_set_vf_vlan)
2206 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2207 ivv->qos,
2208 htons(ETH_P_8021Q));
2209 if (err < 0)
2210 return err;
2213 if (tb[IFLA_VF_VLAN_LIST]) {
2214 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2215 struct nlattr *attr;
2216 int rem, len = 0;
2218 err = -EOPNOTSUPP;
2219 if (!ops->ndo_set_vf_vlan)
2220 return err;
2222 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2223 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2224 nla_len(attr) < NLA_HDRLEN) {
2225 return -EINVAL;
2227 if (len >= MAX_VLAN_LIST_LEN)
2228 return -EOPNOTSUPP;
2229 ivvl[len] = nla_data(attr);
2231 len++;
2233 if (len == 0)
2234 return -EINVAL;
2236 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2237 ivvl[0]->qos, ivvl[0]->vlan_proto);
2238 if (err < 0)
2239 return err;
2242 if (tb[IFLA_VF_TX_RATE]) {
2243 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2244 struct ifla_vf_info ivf;
2246 err = -EOPNOTSUPP;
2247 if (ops->ndo_get_vf_config)
2248 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2249 if (err < 0)
2250 return err;
2252 err = -EOPNOTSUPP;
2253 if (ops->ndo_set_vf_rate)
2254 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2255 ivf.min_tx_rate,
2256 ivt->rate);
2257 if (err < 0)
2258 return err;
2261 if (tb[IFLA_VF_RATE]) {
2262 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2264 err = -EOPNOTSUPP;
2265 if (ops->ndo_set_vf_rate)
2266 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2267 ivt->min_tx_rate,
2268 ivt->max_tx_rate);
2269 if (err < 0)
2270 return err;
2273 if (tb[IFLA_VF_SPOOFCHK]) {
2274 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2276 err = -EOPNOTSUPP;
2277 if (ops->ndo_set_vf_spoofchk)
2278 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2279 ivs->setting);
2280 if (err < 0)
2281 return err;
2284 if (tb[IFLA_VF_LINK_STATE]) {
2285 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2287 err = -EOPNOTSUPP;
2288 if (ops->ndo_set_vf_link_state)
2289 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2290 ivl->link_state);
2291 if (err < 0)
2292 return err;
2295 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2296 struct ifla_vf_rss_query_en *ivrssq_en;
2298 err = -EOPNOTSUPP;
2299 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2300 if (ops->ndo_set_vf_rss_query_en)
2301 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2302 ivrssq_en->setting);
2303 if (err < 0)
2304 return err;
2307 if (tb[IFLA_VF_TRUST]) {
2308 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2310 err = -EOPNOTSUPP;
2311 if (ops->ndo_set_vf_trust)
2312 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2313 if (err < 0)
2314 return err;
2317 if (tb[IFLA_VF_IB_NODE_GUID]) {
2318 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2320 if (!ops->ndo_set_vf_guid)
2321 return -EOPNOTSUPP;
2323 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2326 if (tb[IFLA_VF_IB_PORT_GUID]) {
2327 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2329 if (!ops->ndo_set_vf_guid)
2330 return -EOPNOTSUPP;
2332 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2335 return err;
2338 static int do_set_master(struct net_device *dev, int ifindex,
2339 struct netlink_ext_ack *extack)
2341 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2342 const struct net_device_ops *ops;
2343 int err;
2345 if (upper_dev) {
2346 if (upper_dev->ifindex == ifindex)
2347 return 0;
2348 ops = upper_dev->netdev_ops;
2349 if (ops->ndo_del_slave) {
2350 err = ops->ndo_del_slave(upper_dev, dev);
2351 if (err)
2352 return err;
2353 } else {
2354 return -EOPNOTSUPP;
2358 if (ifindex) {
2359 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2360 if (!upper_dev)
2361 return -EINVAL;
2362 ops = upper_dev->netdev_ops;
2363 if (ops->ndo_add_slave) {
2364 err = ops->ndo_add_slave(upper_dev, dev, extack);
2365 if (err)
2366 return err;
2367 } else {
2368 return -EOPNOTSUPP;
2371 return 0;
2374 #define DO_SETLINK_MODIFIED 0x01
2375 /* notify flag means notify + modified. */
2376 #define DO_SETLINK_NOTIFY 0x03
2377 static int do_setlink(const struct sk_buff *skb,
2378 struct net_device *dev, struct ifinfomsg *ifm,
2379 struct netlink_ext_ack *extack,
2380 struct nlattr **tb, char *ifname, int status)
2382 const struct net_device_ops *ops = dev->netdev_ops;
2383 int err;
2385 err = validate_linkmsg(dev, tb);
2386 if (err < 0)
2387 return err;
2389 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2390 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2391 tb, CAP_NET_ADMIN);
2392 if (IS_ERR(net)) {
2393 err = PTR_ERR(net);
2394 goto errout;
2397 err = dev_change_net_namespace(dev, net, ifname);
2398 put_net(net);
2399 if (err)
2400 goto errout;
2401 status |= DO_SETLINK_MODIFIED;
2404 if (tb[IFLA_MAP]) {
2405 struct rtnl_link_ifmap *u_map;
2406 struct ifmap k_map;
2408 if (!ops->ndo_set_config) {
2409 err = -EOPNOTSUPP;
2410 goto errout;
2413 if (!netif_device_present(dev)) {
2414 err = -ENODEV;
2415 goto errout;
2418 u_map = nla_data(tb[IFLA_MAP]);
2419 k_map.mem_start = (unsigned long) u_map->mem_start;
2420 k_map.mem_end = (unsigned long) u_map->mem_end;
2421 k_map.base_addr = (unsigned short) u_map->base_addr;
2422 k_map.irq = (unsigned char) u_map->irq;
2423 k_map.dma = (unsigned char) u_map->dma;
2424 k_map.port = (unsigned char) u_map->port;
2426 err = ops->ndo_set_config(dev, &k_map);
2427 if (err < 0)
2428 goto errout;
2430 status |= DO_SETLINK_NOTIFY;
2433 if (tb[IFLA_ADDRESS]) {
2434 struct sockaddr *sa;
2435 int len;
2437 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2438 sizeof(*sa));
2439 sa = kmalloc(len, GFP_KERNEL);
2440 if (!sa) {
2441 err = -ENOMEM;
2442 goto errout;
2444 sa->sa_family = dev->type;
2445 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2446 dev->addr_len);
2447 err = dev_set_mac_address(dev, sa);
2448 kfree(sa);
2449 if (err)
2450 goto errout;
2451 status |= DO_SETLINK_MODIFIED;
2454 if (tb[IFLA_MTU]) {
2455 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2456 if (err < 0)
2457 goto errout;
2458 status |= DO_SETLINK_MODIFIED;
2461 if (tb[IFLA_GROUP]) {
2462 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2463 status |= DO_SETLINK_NOTIFY;
2467 * Interface selected by interface index but interface
2468 * name provided implies that a name change has been
2469 * requested.
2471 if (ifm->ifi_index > 0 && ifname[0]) {
2472 err = dev_change_name(dev, ifname);
2473 if (err < 0)
2474 goto errout;
2475 status |= DO_SETLINK_MODIFIED;
2478 if (tb[IFLA_IFALIAS]) {
2479 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2480 nla_len(tb[IFLA_IFALIAS]));
2481 if (err < 0)
2482 goto errout;
2483 status |= DO_SETLINK_NOTIFY;
2486 if (tb[IFLA_BROADCAST]) {
2487 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2488 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2491 if (ifm->ifi_flags || ifm->ifi_change) {
2492 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2493 if (err < 0)
2494 goto errout;
2497 if (tb[IFLA_MASTER]) {
2498 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2499 if (err)
2500 goto errout;
2501 status |= DO_SETLINK_MODIFIED;
2504 if (tb[IFLA_CARRIER]) {
2505 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2506 if (err)
2507 goto errout;
2508 status |= DO_SETLINK_MODIFIED;
2511 if (tb[IFLA_TXQLEN]) {
2512 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2514 err = dev_change_tx_queue_len(dev, value);
2515 if (err)
2516 goto errout;
2517 status |= DO_SETLINK_MODIFIED;
2520 if (tb[IFLA_GSO_MAX_SIZE]) {
2521 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2523 if (max_size > GSO_MAX_SIZE) {
2524 err = -EINVAL;
2525 goto errout;
2528 if (dev->gso_max_size ^ max_size) {
2529 netif_set_gso_max_size(dev, max_size);
2530 status |= DO_SETLINK_MODIFIED;
2534 if (tb[IFLA_GSO_MAX_SEGS]) {
2535 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2537 if (max_segs > GSO_MAX_SEGS) {
2538 err = -EINVAL;
2539 goto errout;
2542 if (dev->gso_max_segs ^ max_segs) {
2543 dev->gso_max_segs = max_segs;
2544 status |= DO_SETLINK_MODIFIED;
2548 if (tb[IFLA_OPERSTATE])
2549 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2551 if (tb[IFLA_LINKMODE]) {
2552 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2554 write_lock_bh(&dev_base_lock);
2555 if (dev->link_mode ^ value)
2556 status |= DO_SETLINK_NOTIFY;
2557 dev->link_mode = value;
2558 write_unlock_bh(&dev_base_lock);
2561 if (tb[IFLA_VFINFO_LIST]) {
2562 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2563 struct nlattr *attr;
2564 int rem;
2566 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2567 if (nla_type(attr) != IFLA_VF_INFO ||
2568 nla_len(attr) < NLA_HDRLEN) {
2569 err = -EINVAL;
2570 goto errout;
2572 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2573 ifla_vf_policy, NULL);
2574 if (err < 0)
2575 goto errout;
2576 err = do_setvfinfo(dev, vfinfo);
2577 if (err < 0)
2578 goto errout;
2579 status |= DO_SETLINK_NOTIFY;
2582 err = 0;
2584 if (tb[IFLA_VF_PORTS]) {
2585 struct nlattr *port[IFLA_PORT_MAX+1];
2586 struct nlattr *attr;
2587 int vf;
2588 int rem;
2590 err = -EOPNOTSUPP;
2591 if (!ops->ndo_set_vf_port)
2592 goto errout;
2594 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2595 if (nla_type(attr) != IFLA_VF_PORT ||
2596 nla_len(attr) < NLA_HDRLEN) {
2597 err = -EINVAL;
2598 goto errout;
2600 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2601 ifla_port_policy, NULL);
2602 if (err < 0)
2603 goto errout;
2604 if (!port[IFLA_PORT_VF]) {
2605 err = -EOPNOTSUPP;
2606 goto errout;
2608 vf = nla_get_u32(port[IFLA_PORT_VF]);
2609 err = ops->ndo_set_vf_port(dev, vf, port);
2610 if (err < 0)
2611 goto errout;
2612 status |= DO_SETLINK_NOTIFY;
2615 err = 0;
2617 if (tb[IFLA_PORT_SELF]) {
2618 struct nlattr *port[IFLA_PORT_MAX+1];
2620 err = nla_parse_nested(port, IFLA_PORT_MAX,
2621 tb[IFLA_PORT_SELF], ifla_port_policy,
2622 NULL);
2623 if (err < 0)
2624 goto errout;
2626 err = -EOPNOTSUPP;
2627 if (ops->ndo_set_vf_port)
2628 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2629 if (err < 0)
2630 goto errout;
2631 status |= DO_SETLINK_NOTIFY;
2634 if (tb[IFLA_AF_SPEC]) {
2635 struct nlattr *af;
2636 int rem;
2638 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2639 const struct rtnl_af_ops *af_ops;
2641 rcu_read_lock();
2643 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2645 err = af_ops->set_link_af(dev, af);
2646 if (err < 0) {
2647 rcu_read_unlock();
2648 goto errout;
2651 rcu_read_unlock();
2652 status |= DO_SETLINK_NOTIFY;
2655 err = 0;
2657 if (tb[IFLA_PROTO_DOWN]) {
2658 err = dev_change_proto_down(dev,
2659 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2660 if (err)
2661 goto errout;
2662 status |= DO_SETLINK_NOTIFY;
2665 if (tb[IFLA_XDP]) {
2666 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2667 u32 xdp_flags = 0;
2669 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2670 ifla_xdp_policy, NULL);
2671 if (err < 0)
2672 goto errout;
2674 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2675 err = -EINVAL;
2676 goto errout;
2679 if (xdp[IFLA_XDP_FLAGS]) {
2680 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2681 if (xdp_flags & ~XDP_FLAGS_MASK) {
2682 err = -EINVAL;
2683 goto errout;
2685 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2686 err = -EINVAL;
2687 goto errout;
2691 if (xdp[IFLA_XDP_FD]) {
2692 err = dev_change_xdp_fd(dev, extack,
2693 nla_get_s32(xdp[IFLA_XDP_FD]),
2694 xdp_flags);
2695 if (err)
2696 goto errout;
2697 status |= DO_SETLINK_NOTIFY;
2701 errout:
2702 if (status & DO_SETLINK_MODIFIED) {
2703 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2704 netdev_state_change(dev);
2706 if (err < 0)
2707 net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
2708 dev->name);
2711 return err;
2714 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2715 struct netlink_ext_ack *extack)
2717 struct net *net = sock_net(skb->sk);
2718 struct ifinfomsg *ifm;
2719 struct net_device *dev;
2720 int err;
2721 struct nlattr *tb[IFLA_MAX+1];
2722 char ifname[IFNAMSIZ];
2724 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2725 extack);
2726 if (err < 0)
2727 goto errout;
2729 err = rtnl_ensure_unique_netns(tb, extack, false);
2730 if (err < 0)
2731 goto errout;
2733 if (tb[IFLA_IFNAME])
2734 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2735 else
2736 ifname[0] = '\0';
2738 err = -EINVAL;
2739 ifm = nlmsg_data(nlh);
2740 if (ifm->ifi_index > 0)
2741 dev = __dev_get_by_index(net, ifm->ifi_index);
2742 else if (tb[IFLA_IFNAME])
2743 dev = __dev_get_by_name(net, ifname);
2744 else
2745 goto errout;
2747 if (dev == NULL) {
2748 err = -ENODEV;
2749 goto errout;
2752 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2753 errout:
2754 return err;
2757 static int rtnl_group_dellink(const struct net *net, int group)
2759 struct net_device *dev, *aux;
2760 LIST_HEAD(list_kill);
2761 bool found = false;
2763 if (!group)
2764 return -EPERM;
2766 for_each_netdev(net, dev) {
2767 if (dev->group == group) {
2768 const struct rtnl_link_ops *ops;
2770 found = true;
2771 ops = dev->rtnl_link_ops;
2772 if (!ops || !ops->dellink)
2773 return -EOPNOTSUPP;
2777 if (!found)
2778 return -ENODEV;
2780 for_each_netdev_safe(net, dev, aux) {
2781 if (dev->group == group) {
2782 const struct rtnl_link_ops *ops;
2784 ops = dev->rtnl_link_ops;
2785 ops->dellink(dev, &list_kill);
2788 unregister_netdevice_many(&list_kill);
2790 return 0;
2793 int rtnl_delete_link(struct net_device *dev)
2795 const struct rtnl_link_ops *ops;
2796 LIST_HEAD(list_kill);
2798 ops = dev->rtnl_link_ops;
2799 if (!ops || !ops->dellink)
2800 return -EOPNOTSUPP;
2802 ops->dellink(dev, &list_kill);
2803 unregister_netdevice_many(&list_kill);
2805 return 0;
2807 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2809 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2810 struct netlink_ext_ack *extack)
2812 struct net *net = sock_net(skb->sk);
2813 struct net *tgt_net = net;
2814 struct net_device *dev = NULL;
2815 struct ifinfomsg *ifm;
2816 char ifname[IFNAMSIZ];
2817 struct nlattr *tb[IFLA_MAX+1];
2818 int err;
2819 int netnsid = -1;
2821 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2822 if (err < 0)
2823 return err;
2825 err = rtnl_ensure_unique_netns(tb, extack, true);
2826 if (err < 0)
2827 return err;
2829 if (tb[IFLA_IFNAME])
2830 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2832 if (tb[IFLA_TARGET_NETNSID]) {
2833 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2834 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2835 if (IS_ERR(tgt_net))
2836 return PTR_ERR(tgt_net);
2839 err = -EINVAL;
2840 ifm = nlmsg_data(nlh);
2841 if (ifm->ifi_index > 0)
2842 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2843 else if (tb[IFLA_IFNAME])
2844 dev = __dev_get_by_name(tgt_net, ifname);
2845 else if (tb[IFLA_GROUP])
2846 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2847 else
2848 goto out;
2850 if (!dev) {
2851 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2852 err = -ENODEV;
2854 goto out;
2857 err = rtnl_delete_link(dev);
2859 out:
2860 if (netnsid >= 0)
2861 put_net(tgt_net);
2863 return err;
2866 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2868 unsigned int old_flags;
2869 int err;
2871 old_flags = dev->flags;
2872 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2873 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2874 if (err < 0)
2875 return err;
2878 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2879 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2880 } else {
2881 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2882 __dev_notify_flags(dev, old_flags, ~0U);
2884 return 0;
2886 EXPORT_SYMBOL(rtnl_configure_link);
2888 struct net_device *rtnl_create_link(struct net *net,
2889 const char *ifname, unsigned char name_assign_type,
2890 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2892 struct net_device *dev;
2893 unsigned int num_tx_queues = 1;
2894 unsigned int num_rx_queues = 1;
2896 if (tb[IFLA_NUM_TX_QUEUES])
2897 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2898 else if (ops->get_num_tx_queues)
2899 num_tx_queues = ops->get_num_tx_queues();
2901 if (tb[IFLA_NUM_RX_QUEUES])
2902 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2903 else if (ops->get_num_rx_queues)
2904 num_rx_queues = ops->get_num_rx_queues();
2906 if (num_tx_queues < 1 || num_tx_queues > 4096)
2907 return ERR_PTR(-EINVAL);
2909 if (num_rx_queues < 1 || num_rx_queues > 4096)
2910 return ERR_PTR(-EINVAL);
2912 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2913 ops->setup, num_tx_queues, num_rx_queues);
2914 if (!dev)
2915 return ERR_PTR(-ENOMEM);
2917 dev_net_set(dev, net);
2918 dev->rtnl_link_ops = ops;
2919 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2921 if (tb[IFLA_MTU])
2922 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2923 if (tb[IFLA_ADDRESS]) {
2924 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2925 nla_len(tb[IFLA_ADDRESS]));
2926 dev->addr_assign_type = NET_ADDR_SET;
2928 if (tb[IFLA_BROADCAST])
2929 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2930 nla_len(tb[IFLA_BROADCAST]));
2931 if (tb[IFLA_TXQLEN])
2932 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2933 if (tb[IFLA_OPERSTATE])
2934 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2935 if (tb[IFLA_LINKMODE])
2936 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2937 if (tb[IFLA_GROUP])
2938 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2939 if (tb[IFLA_GSO_MAX_SIZE])
2940 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2941 if (tb[IFLA_GSO_MAX_SEGS])
2942 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2944 return dev;
2946 EXPORT_SYMBOL(rtnl_create_link);
2948 static int rtnl_group_changelink(const struct sk_buff *skb,
2949 struct net *net, int group,
2950 struct ifinfomsg *ifm,
2951 struct netlink_ext_ack *extack,
2952 struct nlattr **tb)
2954 struct net_device *dev, *aux;
2955 int err;
2957 for_each_netdev_safe(net, dev, aux) {
2958 if (dev->group == group) {
2959 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2960 if (err < 0)
2961 return err;
2965 return 0;
2968 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2969 struct netlink_ext_ack *extack)
2971 struct net *net = sock_net(skb->sk);
2972 const struct rtnl_link_ops *ops;
2973 const struct rtnl_link_ops *m_ops = NULL;
2974 struct net_device *dev;
2975 struct net_device *master_dev = NULL;
2976 struct ifinfomsg *ifm;
2977 char kind[MODULE_NAME_LEN];
2978 char ifname[IFNAMSIZ];
2979 struct nlattr *tb[IFLA_MAX+1];
2980 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2981 unsigned char name_assign_type = NET_NAME_USER;
2982 int err;
2984 #ifdef CONFIG_MODULES
2985 replay:
2986 #endif
2987 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2988 if (err < 0)
2989 return err;
2991 err = rtnl_ensure_unique_netns(tb, extack, false);
2992 if (err < 0)
2993 return err;
2995 if (tb[IFLA_IFNAME])
2996 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2997 else
2998 ifname[0] = '\0';
3000 ifm = nlmsg_data(nlh);
3001 if (ifm->ifi_index > 0)
3002 dev = __dev_get_by_index(net, ifm->ifi_index);
3003 else {
3004 if (ifname[0])
3005 dev = __dev_get_by_name(net, ifname);
3006 else
3007 dev = NULL;
3010 if (dev) {
3011 master_dev = netdev_master_upper_dev_get(dev);
3012 if (master_dev)
3013 m_ops = master_dev->rtnl_link_ops;
3016 err = validate_linkmsg(dev, tb);
3017 if (err < 0)
3018 return err;
3020 if (tb[IFLA_LINKINFO]) {
3021 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
3022 tb[IFLA_LINKINFO], ifla_info_policy,
3023 NULL);
3024 if (err < 0)
3025 return err;
3026 } else
3027 memset(linkinfo, 0, sizeof(linkinfo));
3029 if (linkinfo[IFLA_INFO_KIND]) {
3030 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3031 ops = rtnl_link_ops_get(kind);
3032 } else {
3033 kind[0] = '\0';
3034 ops = NULL;
3037 if (1) {
3038 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3039 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3040 struct nlattr **data = NULL;
3041 struct nlattr **slave_data = NULL;
3042 struct net *dest_net, *link_net = NULL;
3044 if (ops) {
3045 if (ops->maxtype > RTNL_MAX_TYPE)
3046 return -EINVAL;
3048 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3049 err = nla_parse_nested(attr, ops->maxtype,
3050 linkinfo[IFLA_INFO_DATA],
3051 ops->policy, NULL);
3052 if (err < 0)
3053 return err;
3054 data = attr;
3056 if (ops->validate) {
3057 err = ops->validate(tb, data, extack);
3058 if (err < 0)
3059 return err;
3063 if (m_ops) {
3064 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3065 return -EINVAL;
3067 if (m_ops->slave_maxtype &&
3068 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3069 err = nla_parse_nested(slave_attr,
3070 m_ops->slave_maxtype,
3071 linkinfo[IFLA_INFO_SLAVE_DATA],
3072 m_ops->slave_policy,
3073 NULL);
3074 if (err < 0)
3075 return err;
3076 slave_data = slave_attr;
3080 if (dev) {
3081 int status = 0;
3083 if (nlh->nlmsg_flags & NLM_F_EXCL)
3084 return -EEXIST;
3085 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3086 return -EOPNOTSUPP;
3088 if (linkinfo[IFLA_INFO_DATA]) {
3089 if (!ops || ops != dev->rtnl_link_ops ||
3090 !ops->changelink)
3091 return -EOPNOTSUPP;
3093 err = ops->changelink(dev, tb, data, extack);
3094 if (err < 0)
3095 return err;
3096 status |= DO_SETLINK_NOTIFY;
3099 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3100 if (!m_ops || !m_ops->slave_changelink)
3101 return -EOPNOTSUPP;
3103 err = m_ops->slave_changelink(master_dev, dev,
3104 tb, slave_data,
3105 extack);
3106 if (err < 0)
3107 return err;
3108 status |= DO_SETLINK_NOTIFY;
3111 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3112 status);
3115 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3116 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3117 return rtnl_group_changelink(skb, net,
3118 nla_get_u32(tb[IFLA_GROUP]),
3119 ifm, extack, tb);
3120 return -ENODEV;
3123 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3124 return -EOPNOTSUPP;
3126 if (!ops) {
3127 #ifdef CONFIG_MODULES
3128 if (kind[0]) {
3129 __rtnl_unlock();
3130 request_module("rtnl-link-%s", kind);
3131 rtnl_lock();
3132 ops = rtnl_link_ops_get(kind);
3133 if (ops)
3134 goto replay;
3136 #endif
3137 return -EOPNOTSUPP;
3140 if (!ops->setup)
3141 return -EOPNOTSUPP;
3143 if (!ifname[0]) {
3144 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3145 name_assign_type = NET_NAME_ENUM;
3148 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3149 if (IS_ERR(dest_net))
3150 return PTR_ERR(dest_net);
3152 if (tb[IFLA_LINK_NETNSID]) {
3153 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3155 link_net = get_net_ns_by_id(dest_net, id);
3156 if (!link_net) {
3157 err = -EINVAL;
3158 goto out;
3160 err = -EPERM;
3161 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3162 goto out;
3165 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3166 name_assign_type, ops, tb);
3167 if (IS_ERR(dev)) {
3168 err = PTR_ERR(dev);
3169 goto out;
3172 dev->ifindex = ifm->ifi_index;
3174 if (ops->newlink) {
3175 err = ops->newlink(link_net ? : net, dev, tb, data,
3176 extack);
3177 /* Drivers should call free_netdev() in ->destructor
3178 * and unregister it on failure after registration
3179 * so that device could be finally freed in rtnl_unlock.
3181 if (err < 0) {
3182 /* If device is not registered at all, free it now */
3183 if (dev->reg_state == NETREG_UNINITIALIZED)
3184 free_netdev(dev);
3185 goto out;
3187 } else {
3188 err = register_netdevice(dev);
3189 if (err < 0) {
3190 free_netdev(dev);
3191 goto out;
3194 err = rtnl_configure_link(dev, ifm);
3195 if (err < 0)
3196 goto out_unregister;
3197 if (link_net) {
3198 err = dev_change_net_namespace(dev, dest_net, ifname);
3199 if (err < 0)
3200 goto out_unregister;
3202 if (tb[IFLA_MASTER]) {
3203 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3204 extack);
3205 if (err)
3206 goto out_unregister;
3208 out:
3209 if (link_net)
3210 put_net(link_net);
3211 put_net(dest_net);
3212 return err;
3213 out_unregister:
3214 if (ops->newlink) {
3215 LIST_HEAD(list_kill);
3217 ops->dellink(dev, &list_kill);
3218 unregister_netdevice_many(&list_kill);
3219 } else {
3220 unregister_netdevice(dev);
3222 goto out;
3226 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3227 struct netlink_ext_ack *extack)
3229 struct net *net = sock_net(skb->sk);
3230 struct net *tgt_net = net;
3231 struct ifinfomsg *ifm;
3232 char ifname[IFNAMSIZ];
3233 struct nlattr *tb[IFLA_MAX+1];
3234 struct net_device *dev = NULL;
3235 struct sk_buff *nskb;
3236 int netnsid = -1;
3237 int err;
3238 u32 ext_filter_mask = 0;
3240 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3241 if (err < 0)
3242 return err;
3244 err = rtnl_ensure_unique_netns(tb, extack, true);
3245 if (err < 0)
3246 return err;
3248 if (tb[IFLA_TARGET_NETNSID]) {
3249 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3250 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3251 if (IS_ERR(tgt_net))
3252 return PTR_ERR(tgt_net);
3255 if (tb[IFLA_IFNAME])
3256 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3258 if (tb[IFLA_EXT_MASK])
3259 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3261 err = -EINVAL;
3262 ifm = nlmsg_data(nlh);
3263 if (ifm->ifi_index > 0)
3264 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3265 else if (tb[IFLA_IFNAME])
3266 dev = __dev_get_by_name(tgt_net, ifname);
3267 else
3268 goto out;
3270 err = -ENODEV;
3271 if (dev == NULL)
3272 goto out;
3274 err = -ENOBUFS;
3275 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3276 if (nskb == NULL)
3277 goto out;
3279 err = rtnl_fill_ifinfo(nskb, dev, net,
3280 RTM_NEWLINK, NETLINK_CB(skb).portid,
3281 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3282 0, NULL, 0, netnsid);
3283 if (err < 0) {
3284 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3285 WARN_ON(err == -EMSGSIZE);
3286 kfree_skb(nskb);
3287 } else
3288 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3289 out:
3290 if (netnsid >= 0)
3291 put_net(tgt_net);
3293 return err;
3296 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3298 struct net *net = sock_net(skb->sk);
3299 struct net_device *dev;
3300 struct nlattr *tb[IFLA_MAX+1];
3301 u32 ext_filter_mask = 0;
3302 u16 min_ifinfo_dump_size = 0;
3303 int hdrlen;
3305 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3306 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3307 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3309 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3310 if (tb[IFLA_EXT_MASK])
3311 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3314 if (!ext_filter_mask)
3315 return NLMSG_GOODSIZE;
3317 * traverse the list of net devices and compute the minimum
3318 * buffer size based upon the filter mask.
3320 rcu_read_lock();
3321 for_each_netdev_rcu(net, dev) {
3322 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3323 if_nlmsg_size(dev,
3324 ext_filter_mask));
3326 rcu_read_unlock();
3328 return nlmsg_total_size(min_ifinfo_dump_size);
3331 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3333 int idx;
3334 int s_idx = cb->family;
3335 int type = cb->nlh->nlmsg_type - RTM_BASE;
3336 int ret = 0;
3338 if (s_idx == 0)
3339 s_idx = 1;
3341 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3342 struct rtnl_link **tab;
3343 struct rtnl_link *link;
3344 rtnl_dumpit_func dumpit;
3346 if (idx < s_idx || idx == PF_PACKET)
3347 continue;
3349 if (type < 0 || type >= RTM_NR_MSGTYPES)
3350 continue;
3352 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3353 if (!tab)
3354 continue;
3356 link = tab[type];
3357 if (!link)
3358 continue;
3360 dumpit = link->dumpit;
3361 if (!dumpit)
3362 continue;
3364 if (idx > s_idx) {
3365 memset(&cb->args[0], 0, sizeof(cb->args));
3366 cb->prev_seq = 0;
3367 cb->seq = 0;
3369 ret = dumpit(skb, cb);
3370 if (ret)
3371 break;
3373 cb->family = idx;
3375 return skb->len ? : ret;
3378 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3379 unsigned int change,
3380 u32 event, gfp_t flags, int *new_nsid,
3381 int new_ifindex)
3383 struct net *net = dev_net(dev);
3384 struct sk_buff *skb;
3385 int err = -ENOBUFS;
3386 size_t if_info_size;
3388 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3389 if (skb == NULL)
3390 goto errout;
3392 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3393 type, 0, 0, change, 0, 0, event,
3394 new_nsid, new_ifindex, -1);
3395 if (err < 0) {
3396 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3397 WARN_ON(err == -EMSGSIZE);
3398 kfree_skb(skb);
3399 goto errout;
3401 return skb;
3402 errout:
3403 if (err < 0)
3404 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3405 return NULL;
3408 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3410 struct net *net = dev_net(dev);
3412 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3415 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3416 unsigned int change, u32 event,
3417 gfp_t flags, int *new_nsid, int new_ifindex)
3419 struct sk_buff *skb;
3421 if (dev->reg_state != NETREG_REGISTERED)
3422 return;
3424 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3425 new_ifindex);
3426 if (skb)
3427 rtmsg_ifinfo_send(skb, dev, flags);
3430 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3431 gfp_t flags)
3433 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3434 NULL, 0);
3437 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3438 gfp_t flags, int *new_nsid, int new_ifindex)
3440 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3441 new_nsid, new_ifindex);
3444 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3445 struct net_device *dev,
3446 u8 *addr, u16 vid, u32 pid, u32 seq,
3447 int type, unsigned int flags,
3448 int nlflags, u16 ndm_state)
3450 struct nlmsghdr *nlh;
3451 struct ndmsg *ndm;
3453 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3454 if (!nlh)
3455 return -EMSGSIZE;
3457 ndm = nlmsg_data(nlh);
3458 ndm->ndm_family = AF_BRIDGE;
3459 ndm->ndm_pad1 = 0;
3460 ndm->ndm_pad2 = 0;
3461 ndm->ndm_flags = flags;
3462 ndm->ndm_type = 0;
3463 ndm->ndm_ifindex = dev->ifindex;
3464 ndm->ndm_state = ndm_state;
3466 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3467 goto nla_put_failure;
3468 if (vid)
3469 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3470 goto nla_put_failure;
3472 nlmsg_end(skb, nlh);
3473 return 0;
3475 nla_put_failure:
3476 nlmsg_cancel(skb, nlh);
3477 return -EMSGSIZE;
3480 static inline size_t rtnl_fdb_nlmsg_size(void)
3482 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3483 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3484 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3488 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3489 u16 ndm_state)
3491 struct net *net = dev_net(dev);
3492 struct sk_buff *skb;
3493 int err = -ENOBUFS;
3495 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3496 if (!skb)
3497 goto errout;
3499 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3500 0, 0, type, NTF_SELF, 0, ndm_state);
3501 if (err < 0) {
3502 kfree_skb(skb);
3503 goto errout;
3506 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3507 return;
3508 errout:
3509 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3513 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3515 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3516 struct nlattr *tb[],
3517 struct net_device *dev,
3518 const unsigned char *addr, u16 vid,
3519 u16 flags)
3521 int err = -EINVAL;
3523 /* If aging addresses are supported device will need to
3524 * implement its own handler for this.
3526 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3527 pr_info("%s: FDB only supports static addresses\n", dev->name);
3528 return err;
3531 if (vid) {
3532 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3533 return err;
3536 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3537 err = dev_uc_add_excl(dev, addr);
3538 else if (is_multicast_ether_addr(addr))
3539 err = dev_mc_add_excl(dev, addr);
3541 /* Only return duplicate errors if NLM_F_EXCL is set */
3542 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3543 err = 0;
3545 return err;
3547 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3549 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3550 struct netlink_ext_ack *extack)
3552 u16 vid = 0;
3554 if (vlan_attr) {
3555 if (nla_len(vlan_attr) != sizeof(u16)) {
3556 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3557 return -EINVAL;
3560 vid = nla_get_u16(vlan_attr);
3562 if (!vid || vid >= VLAN_VID_MASK) {
3563 NL_SET_ERR_MSG(extack, "invalid vlan id");
3564 return -EINVAL;
3567 *p_vid = vid;
3568 return 0;
3571 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3572 struct netlink_ext_ack *extack)
3574 struct net *net = sock_net(skb->sk);
3575 struct ndmsg *ndm;
3576 struct nlattr *tb[NDA_MAX+1];
3577 struct net_device *dev;
3578 u8 *addr;
3579 u16 vid;
3580 int err;
3582 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3583 if (err < 0)
3584 return err;
3586 ndm = nlmsg_data(nlh);
3587 if (ndm->ndm_ifindex == 0) {
3588 NL_SET_ERR_MSG(extack, "invalid ifindex");
3589 return -EINVAL;
3592 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3593 if (dev == NULL) {
3594 NL_SET_ERR_MSG(extack, "unknown ifindex");
3595 return -ENODEV;
3598 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3599 NL_SET_ERR_MSG(extack, "invalid address");
3600 return -EINVAL;
3603 if (dev->type != ARPHRD_ETHER) {
3604 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3605 return -EINVAL;
3608 addr = nla_data(tb[NDA_LLADDR]);
3610 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3611 if (err)
3612 return err;
3614 err = -EOPNOTSUPP;
3616 /* Support fdb on master device the net/bridge default case */
3617 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3618 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3619 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3620 const struct net_device_ops *ops = br_dev->netdev_ops;
3622 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3623 nlh->nlmsg_flags);
3624 if (err)
3625 goto out;
3626 else
3627 ndm->ndm_flags &= ~NTF_MASTER;
3630 /* Embedded bridge, macvlan, and any other device support */
3631 if ((ndm->ndm_flags & NTF_SELF)) {
3632 if (dev->netdev_ops->ndo_fdb_add)
3633 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3634 vid,
3635 nlh->nlmsg_flags);
3636 else
3637 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3638 nlh->nlmsg_flags);
3640 if (!err) {
3641 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3642 ndm->ndm_state);
3643 ndm->ndm_flags &= ~NTF_SELF;
3646 out:
3647 return err;
3651 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3653 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3654 struct nlattr *tb[],
3655 struct net_device *dev,
3656 const unsigned char *addr, u16 vid)
3658 int err = -EINVAL;
3660 /* If aging addresses are supported device will need to
3661 * implement its own handler for this.
3663 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3664 pr_info("%s: FDB only supports static addresses\n", dev->name);
3665 return err;
3668 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3669 err = dev_uc_del(dev, addr);
3670 else if (is_multicast_ether_addr(addr))
3671 err = dev_mc_del(dev, addr);
3673 return err;
3675 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3677 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3678 struct netlink_ext_ack *extack)
3680 struct net *net = sock_net(skb->sk);
3681 struct ndmsg *ndm;
3682 struct nlattr *tb[NDA_MAX+1];
3683 struct net_device *dev;
3684 int err = -EINVAL;
3685 __u8 *addr;
3686 u16 vid;
3688 if (!netlink_capable(skb, CAP_NET_ADMIN))
3689 return -EPERM;
3691 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3692 if (err < 0)
3693 return err;
3695 ndm = nlmsg_data(nlh);
3696 if (ndm->ndm_ifindex == 0) {
3697 NL_SET_ERR_MSG(extack, "invalid ifindex");
3698 return -EINVAL;
3701 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3702 if (dev == NULL) {
3703 NL_SET_ERR_MSG(extack, "unknown ifindex");
3704 return -ENODEV;
3707 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3708 NL_SET_ERR_MSG(extack, "invalid address");
3709 return -EINVAL;
3712 if (dev->type != ARPHRD_ETHER) {
3713 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3714 return -EINVAL;
3717 addr = nla_data(tb[NDA_LLADDR]);
3719 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3720 if (err)
3721 return err;
3723 err = -EOPNOTSUPP;
3725 /* Support fdb on master device the net/bridge default case */
3726 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3727 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3728 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3729 const struct net_device_ops *ops = br_dev->netdev_ops;
3731 if (ops->ndo_fdb_del)
3732 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3734 if (err)
3735 goto out;
3736 else
3737 ndm->ndm_flags &= ~NTF_MASTER;
3740 /* Embedded bridge, macvlan, and any other device support */
3741 if (ndm->ndm_flags & NTF_SELF) {
3742 if (dev->netdev_ops->ndo_fdb_del)
3743 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3744 vid);
3745 else
3746 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3748 if (!err) {
3749 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3750 ndm->ndm_state);
3751 ndm->ndm_flags &= ~NTF_SELF;
3754 out:
3755 return err;
3758 static int nlmsg_populate_fdb(struct sk_buff *skb,
3759 struct netlink_callback *cb,
3760 struct net_device *dev,
3761 int *idx,
3762 struct netdev_hw_addr_list *list)
3764 struct netdev_hw_addr *ha;
3765 int err;
3766 u32 portid, seq;
3768 portid = NETLINK_CB(cb->skb).portid;
3769 seq = cb->nlh->nlmsg_seq;
3771 list_for_each_entry(ha, &list->list, list) {
3772 if (*idx < cb->args[2])
3773 goto skip;
3775 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3776 portid, seq,
3777 RTM_NEWNEIGH, NTF_SELF,
3778 NLM_F_MULTI, NUD_PERMANENT);
3779 if (err < 0)
3780 return err;
3781 skip:
3782 *idx += 1;
3784 return 0;
3788 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3789 * @nlh: netlink message header
3790 * @dev: netdevice
3792 * Default netdevice operation to dump the existing unicast address list.
3793 * Returns number of addresses from list put in skb.
3795 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3796 struct netlink_callback *cb,
3797 struct net_device *dev,
3798 struct net_device *filter_dev,
3799 int *idx)
3801 int err;
3803 if (dev->type != ARPHRD_ETHER)
3804 return -EINVAL;
3806 netif_addr_lock_bh(dev);
3807 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3808 if (err)
3809 goto out;
3810 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3811 out:
3812 netif_addr_unlock_bh(dev);
3813 return err;
3815 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3817 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3818 int *br_idx, int *brport_idx,
3819 struct netlink_ext_ack *extack)
3821 struct nlattr *tb[NDA_MAX + 1];
3822 struct ndmsg *ndm;
3823 int err, i;
3825 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3826 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3827 return -EINVAL;
3830 ndm = nlmsg_data(nlh);
3831 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3832 ndm->ndm_flags || ndm->ndm_type) {
3833 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3834 return -EINVAL;
3837 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
3838 NULL, extack);
3839 if (err < 0)
3840 return err;
3842 *brport_idx = ndm->ndm_ifindex;
3843 for (i = 0; i <= NDA_MAX; ++i) {
3844 if (!tb[i])
3845 continue;
3847 switch (i) {
3848 case NDA_IFINDEX:
3849 if (nla_len(tb[i]) != sizeof(u32)) {
3850 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3851 return -EINVAL;
3853 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3854 break;
3855 case NDA_MASTER:
3856 if (nla_len(tb[i]) != sizeof(u32)) {
3857 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3858 return -EINVAL;
3860 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3861 break;
3862 default:
3863 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3864 return -EINVAL;
3868 return 0;
3871 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3872 int *br_idx, int *brport_idx,
3873 struct netlink_ext_ack *extack)
3875 struct nlattr *tb[IFLA_MAX+1];
3876 int err;
3878 /* A hack to preserve kernel<->userspace interface.
3879 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3880 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3881 * So, check for ndmsg with an optional u32 attribute (not used here).
3882 * Fortunately these sizes don't conflict with the size of ifinfomsg
3883 * with an optional attribute.
3885 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3886 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3887 nla_attr_size(sizeof(u32)))) {
3888 struct ifinfomsg *ifm;
3890 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3891 ifla_policy, extack);
3892 if (err < 0) {
3893 return -EINVAL;
3894 } else if (err == 0) {
3895 if (tb[IFLA_MASTER])
3896 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3899 ifm = nlmsg_data(nlh);
3900 *brport_idx = ifm->ifi_index;
3902 return 0;
3905 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3907 struct net_device *dev;
3908 struct net_device *br_dev = NULL;
3909 const struct net_device_ops *ops = NULL;
3910 const struct net_device_ops *cops = NULL;
3911 struct net *net = sock_net(skb->sk);
3912 struct hlist_head *head;
3913 int brport_idx = 0;
3914 int br_idx = 0;
3915 int h, s_h;
3916 int idx = 0, s_idx;
3917 int err = 0;
3918 int fidx = 0;
3920 if (cb->strict_check)
3921 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
3922 cb->extack);
3923 else
3924 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
3925 cb->extack);
3926 if (err < 0)
3927 return err;
3929 if (br_idx) {
3930 br_dev = __dev_get_by_index(net, br_idx);
3931 if (!br_dev)
3932 return -ENODEV;
3934 ops = br_dev->netdev_ops;
3937 s_h = cb->args[0];
3938 s_idx = cb->args[1];
3940 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3941 idx = 0;
3942 head = &net->dev_index_head[h];
3943 hlist_for_each_entry(dev, head, index_hlist) {
3945 if (brport_idx && (dev->ifindex != brport_idx))
3946 continue;
3948 if (!br_idx) { /* user did not specify a specific bridge */
3949 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3950 br_dev = netdev_master_upper_dev_get(dev);
3951 cops = br_dev->netdev_ops;
3953 } else {
3954 if (dev != br_dev &&
3955 !(dev->priv_flags & IFF_BRIDGE_PORT))
3956 continue;
3958 if (br_dev != netdev_master_upper_dev_get(dev) &&
3959 !(dev->priv_flags & IFF_EBRIDGE))
3960 continue;
3961 cops = ops;
3964 if (idx < s_idx)
3965 goto cont;
3967 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3968 if (cops && cops->ndo_fdb_dump) {
3969 err = cops->ndo_fdb_dump(skb, cb,
3970 br_dev, dev,
3971 &fidx);
3972 if (err == -EMSGSIZE)
3973 goto out;
3977 if (dev->netdev_ops->ndo_fdb_dump)
3978 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3979 dev, NULL,
3980 &fidx);
3981 else
3982 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3983 &fidx);
3984 if (err == -EMSGSIZE)
3985 goto out;
3987 cops = NULL;
3989 /* reset fdb offset to 0 for rest of the interfaces */
3990 cb->args[2] = 0;
3991 fidx = 0;
3992 cont:
3993 idx++;
3997 out:
3998 cb->args[0] = h;
3999 cb->args[1] = idx;
4000 cb->args[2] = fidx;
4002 return skb->len;
4005 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4006 unsigned int attrnum, unsigned int flag)
4008 if (mask & flag)
4009 return nla_put_u8(skb, attrnum, !!(flags & flag));
4010 return 0;
4013 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4014 struct net_device *dev, u16 mode,
4015 u32 flags, u32 mask, int nlflags,
4016 u32 filter_mask,
4017 int (*vlan_fill)(struct sk_buff *skb,
4018 struct net_device *dev,
4019 u32 filter_mask))
4021 struct nlmsghdr *nlh;
4022 struct ifinfomsg *ifm;
4023 struct nlattr *br_afspec;
4024 struct nlattr *protinfo;
4025 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4026 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4027 int err = 0;
4029 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4030 if (nlh == NULL)
4031 return -EMSGSIZE;
4033 ifm = nlmsg_data(nlh);
4034 ifm->ifi_family = AF_BRIDGE;
4035 ifm->__ifi_pad = 0;
4036 ifm->ifi_type = dev->type;
4037 ifm->ifi_index = dev->ifindex;
4038 ifm->ifi_flags = dev_get_flags(dev);
4039 ifm->ifi_change = 0;
4042 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4043 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4044 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4045 (br_dev &&
4046 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4047 (dev->addr_len &&
4048 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4049 (dev->ifindex != dev_get_iflink(dev) &&
4050 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4051 goto nla_put_failure;
4053 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
4054 if (!br_afspec)
4055 goto nla_put_failure;
4057 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4058 nla_nest_cancel(skb, br_afspec);
4059 goto nla_put_failure;
4062 if (mode != BRIDGE_MODE_UNDEF) {
4063 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4064 nla_nest_cancel(skb, br_afspec);
4065 goto nla_put_failure;
4068 if (vlan_fill) {
4069 err = vlan_fill(skb, dev, filter_mask);
4070 if (err) {
4071 nla_nest_cancel(skb, br_afspec);
4072 goto nla_put_failure;
4075 nla_nest_end(skb, br_afspec);
4077 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
4078 if (!protinfo)
4079 goto nla_put_failure;
4081 if (brport_nla_put_flag(skb, flags, mask,
4082 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4083 brport_nla_put_flag(skb, flags, mask,
4084 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4085 brport_nla_put_flag(skb, flags, mask,
4086 IFLA_BRPORT_FAST_LEAVE,
4087 BR_MULTICAST_FAST_LEAVE) ||
4088 brport_nla_put_flag(skb, flags, mask,
4089 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4090 brport_nla_put_flag(skb, flags, mask,
4091 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4092 brport_nla_put_flag(skb, flags, mask,
4093 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4094 brport_nla_put_flag(skb, flags, mask,
4095 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4096 brport_nla_put_flag(skb, flags, mask,
4097 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4098 nla_nest_cancel(skb, protinfo);
4099 goto nla_put_failure;
4102 nla_nest_end(skb, protinfo);
4104 nlmsg_end(skb, nlh);
4105 return 0;
4106 nla_put_failure:
4107 nlmsg_cancel(skb, nlh);
4108 return err ? err : -EMSGSIZE;
4110 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4112 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4113 bool strict_check, u32 *filter_mask,
4114 struct netlink_ext_ack *extack)
4116 struct nlattr *tb[IFLA_MAX+1];
4117 int err, i;
4119 if (strict_check) {
4120 struct ifinfomsg *ifm;
4122 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4123 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4124 return -EINVAL;
4127 ifm = nlmsg_data(nlh);
4128 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4129 ifm->ifi_change || ifm->ifi_index) {
4130 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4131 return -EINVAL;
4134 err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4135 IFLA_MAX, ifla_policy, extack);
4136 } else {
4137 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4138 IFLA_MAX, ifla_policy, extack);
4140 if (err < 0)
4141 return err;
4143 /* new attributes should only be added with strict checking */
4144 for (i = 0; i <= IFLA_MAX; ++i) {
4145 if (!tb[i])
4146 continue;
4148 switch (i) {
4149 case IFLA_EXT_MASK:
4150 *filter_mask = nla_get_u32(tb[i]);
4151 break;
4152 default:
4153 if (strict_check) {
4154 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4155 return -EINVAL;
4160 return 0;
4163 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4165 const struct nlmsghdr *nlh = cb->nlh;
4166 struct net *net = sock_net(skb->sk);
4167 struct net_device *dev;
4168 int idx = 0;
4169 u32 portid = NETLINK_CB(cb->skb).portid;
4170 u32 seq = nlh->nlmsg_seq;
4171 u32 filter_mask = 0;
4172 int err;
4174 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4175 cb->extack);
4176 if (err < 0 && cb->strict_check)
4177 return err;
4179 rcu_read_lock();
4180 for_each_netdev_rcu(net, dev) {
4181 const struct net_device_ops *ops = dev->netdev_ops;
4182 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4184 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4185 if (idx >= cb->args[0]) {
4186 err = br_dev->netdev_ops->ndo_bridge_getlink(
4187 skb, portid, seq, dev,
4188 filter_mask, NLM_F_MULTI);
4189 if (err < 0 && err != -EOPNOTSUPP) {
4190 if (likely(skb->len))
4191 break;
4193 goto out_err;
4196 idx++;
4199 if (ops->ndo_bridge_getlink) {
4200 if (idx >= cb->args[0]) {
4201 err = ops->ndo_bridge_getlink(skb, portid,
4202 seq, dev,
4203 filter_mask,
4204 NLM_F_MULTI);
4205 if (err < 0 && err != -EOPNOTSUPP) {
4206 if (likely(skb->len))
4207 break;
4209 goto out_err;
4212 idx++;
4215 err = skb->len;
4216 out_err:
4217 rcu_read_unlock();
4218 cb->args[0] = idx;
4220 return err;
4223 static inline size_t bridge_nlmsg_size(void)
4225 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4226 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4227 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4228 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4229 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4230 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4231 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4232 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4233 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4234 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4235 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4238 static int rtnl_bridge_notify(struct net_device *dev)
4240 struct net *net = dev_net(dev);
4241 struct sk_buff *skb;
4242 int err = -EOPNOTSUPP;
4244 if (!dev->netdev_ops->ndo_bridge_getlink)
4245 return 0;
4247 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4248 if (!skb) {
4249 err = -ENOMEM;
4250 goto errout;
4253 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4254 if (err < 0)
4255 goto errout;
4257 if (!skb->len)
4258 goto errout;
4260 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4261 return 0;
4262 errout:
4263 WARN_ON(err == -EMSGSIZE);
4264 kfree_skb(skb);
4265 if (err)
4266 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4267 return err;
4270 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4271 struct netlink_ext_ack *extack)
4273 struct net *net = sock_net(skb->sk);
4274 struct ifinfomsg *ifm;
4275 struct net_device *dev;
4276 struct nlattr *br_spec, *attr = NULL;
4277 int rem, err = -EOPNOTSUPP;
4278 u16 flags = 0;
4279 bool have_flags = false;
4281 if (nlmsg_len(nlh) < sizeof(*ifm))
4282 return -EINVAL;
4284 ifm = nlmsg_data(nlh);
4285 if (ifm->ifi_family != AF_BRIDGE)
4286 return -EPFNOSUPPORT;
4288 dev = __dev_get_by_index(net, ifm->ifi_index);
4289 if (!dev) {
4290 NL_SET_ERR_MSG(extack, "unknown ifindex");
4291 return -ENODEV;
4294 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4295 if (br_spec) {
4296 nla_for_each_nested(attr, br_spec, rem) {
4297 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4298 if (nla_len(attr) < sizeof(flags))
4299 return -EINVAL;
4301 have_flags = true;
4302 flags = nla_get_u16(attr);
4303 break;
4308 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4309 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4311 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4312 err = -EOPNOTSUPP;
4313 goto out;
4316 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
4317 if (err)
4318 goto out;
4320 flags &= ~BRIDGE_FLAGS_MASTER;
4323 if ((flags & BRIDGE_FLAGS_SELF)) {
4324 if (!dev->netdev_ops->ndo_bridge_setlink)
4325 err = -EOPNOTSUPP;
4326 else
4327 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4328 flags);
4329 if (!err) {
4330 flags &= ~BRIDGE_FLAGS_SELF;
4332 /* Generate event to notify upper layer of bridge
4333 * change
4335 err = rtnl_bridge_notify(dev);
4339 if (have_flags)
4340 memcpy(nla_data(attr), &flags, sizeof(flags));
4341 out:
4342 return err;
4345 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4346 struct netlink_ext_ack *extack)
4348 struct net *net = sock_net(skb->sk);
4349 struct ifinfomsg *ifm;
4350 struct net_device *dev;
4351 struct nlattr *br_spec, *attr = NULL;
4352 int rem, err = -EOPNOTSUPP;
4353 u16 flags = 0;
4354 bool have_flags = false;
4356 if (nlmsg_len(nlh) < sizeof(*ifm))
4357 return -EINVAL;
4359 ifm = nlmsg_data(nlh);
4360 if (ifm->ifi_family != AF_BRIDGE)
4361 return -EPFNOSUPPORT;
4363 dev = __dev_get_by_index(net, ifm->ifi_index);
4364 if (!dev) {
4365 NL_SET_ERR_MSG(extack, "unknown ifindex");
4366 return -ENODEV;
4369 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4370 if (br_spec) {
4371 nla_for_each_nested(attr, br_spec, rem) {
4372 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4373 if (nla_len(attr) < sizeof(flags))
4374 return -EINVAL;
4376 have_flags = true;
4377 flags = nla_get_u16(attr);
4378 break;
4383 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4384 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4386 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4387 err = -EOPNOTSUPP;
4388 goto out;
4391 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4392 if (err)
4393 goto out;
4395 flags &= ~BRIDGE_FLAGS_MASTER;
4398 if ((flags & BRIDGE_FLAGS_SELF)) {
4399 if (!dev->netdev_ops->ndo_bridge_dellink)
4400 err = -EOPNOTSUPP;
4401 else
4402 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4403 flags);
4405 if (!err) {
4406 flags &= ~BRIDGE_FLAGS_SELF;
4408 /* Generate event to notify upper layer of bridge
4409 * change
4411 err = rtnl_bridge_notify(dev);
4415 if (have_flags)
4416 memcpy(nla_data(attr), &flags, sizeof(flags));
4417 out:
4418 return err;
4421 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4423 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4424 (!idxattr || idxattr == attrid);
4427 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4428 static int rtnl_get_offload_stats_attr_size(int attr_id)
4430 switch (attr_id) {
4431 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4432 return sizeof(struct rtnl_link_stats64);
4435 return 0;
4438 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4439 int *prividx)
4441 struct nlattr *attr = NULL;
4442 int attr_id, size;
4443 void *attr_data;
4444 int err;
4446 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4447 dev->netdev_ops->ndo_get_offload_stats))
4448 return -ENODATA;
4450 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4451 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4452 if (attr_id < *prividx)
4453 continue;
4455 size = rtnl_get_offload_stats_attr_size(attr_id);
4456 if (!size)
4457 continue;
4459 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4460 continue;
4462 attr = nla_reserve_64bit(skb, attr_id, size,
4463 IFLA_OFFLOAD_XSTATS_UNSPEC);
4464 if (!attr)
4465 goto nla_put_failure;
4467 attr_data = nla_data(attr);
4468 memset(attr_data, 0, size);
4469 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4470 attr_data);
4471 if (err)
4472 goto get_offload_stats_failure;
4475 if (!attr)
4476 return -ENODATA;
4478 *prividx = 0;
4479 return 0;
4481 nla_put_failure:
4482 err = -EMSGSIZE;
4483 get_offload_stats_failure:
4484 *prividx = attr_id;
4485 return err;
4488 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4490 int nla_size = 0;
4491 int attr_id;
4492 int size;
4494 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4495 dev->netdev_ops->ndo_get_offload_stats))
4496 return 0;
4498 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4499 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4500 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4501 continue;
4502 size = rtnl_get_offload_stats_attr_size(attr_id);
4503 nla_size += nla_total_size_64bit(size);
4506 if (nla_size != 0)
4507 nla_size += nla_total_size(0);
4509 return nla_size;
4512 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4513 int type, u32 pid, u32 seq, u32 change,
4514 unsigned int flags, unsigned int filter_mask,
4515 int *idxattr, int *prividx)
4517 struct if_stats_msg *ifsm;
4518 struct nlmsghdr *nlh;
4519 struct nlattr *attr;
4520 int s_prividx = *prividx;
4521 int err;
4523 ASSERT_RTNL();
4525 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4526 if (!nlh)
4527 return -EMSGSIZE;
4529 ifsm = nlmsg_data(nlh);
4530 ifsm->family = PF_UNSPEC;
4531 ifsm->pad1 = 0;
4532 ifsm->pad2 = 0;
4533 ifsm->ifindex = dev->ifindex;
4534 ifsm->filter_mask = filter_mask;
4536 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4537 struct rtnl_link_stats64 *sp;
4539 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4540 sizeof(struct rtnl_link_stats64),
4541 IFLA_STATS_UNSPEC);
4542 if (!attr)
4543 goto nla_put_failure;
4545 sp = nla_data(attr);
4546 dev_get_stats(dev, sp);
4549 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4550 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4552 if (ops && ops->fill_linkxstats) {
4553 *idxattr = IFLA_STATS_LINK_XSTATS;
4554 attr = nla_nest_start(skb,
4555 IFLA_STATS_LINK_XSTATS);
4556 if (!attr)
4557 goto nla_put_failure;
4559 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4560 nla_nest_end(skb, attr);
4561 if (err)
4562 goto nla_put_failure;
4563 *idxattr = 0;
4567 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4568 *idxattr)) {
4569 const struct rtnl_link_ops *ops = NULL;
4570 const struct net_device *master;
4572 master = netdev_master_upper_dev_get(dev);
4573 if (master)
4574 ops = master->rtnl_link_ops;
4575 if (ops && ops->fill_linkxstats) {
4576 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4577 attr = nla_nest_start(skb,
4578 IFLA_STATS_LINK_XSTATS_SLAVE);
4579 if (!attr)
4580 goto nla_put_failure;
4582 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4583 nla_nest_end(skb, attr);
4584 if (err)
4585 goto nla_put_failure;
4586 *idxattr = 0;
4590 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4591 *idxattr)) {
4592 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4593 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4594 if (!attr)
4595 goto nla_put_failure;
4597 err = rtnl_get_offload_stats(skb, dev, prividx);
4598 if (err == -ENODATA)
4599 nla_nest_cancel(skb, attr);
4600 else
4601 nla_nest_end(skb, attr);
4603 if (err && err != -ENODATA)
4604 goto nla_put_failure;
4605 *idxattr = 0;
4608 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4609 struct rtnl_af_ops *af_ops;
4611 *idxattr = IFLA_STATS_AF_SPEC;
4612 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4613 if (!attr)
4614 goto nla_put_failure;
4616 rcu_read_lock();
4617 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4618 if (af_ops->fill_stats_af) {
4619 struct nlattr *af;
4620 int err;
4622 af = nla_nest_start(skb, af_ops->family);
4623 if (!af) {
4624 rcu_read_unlock();
4625 goto nla_put_failure;
4627 err = af_ops->fill_stats_af(skb, dev);
4629 if (err == -ENODATA) {
4630 nla_nest_cancel(skb, af);
4631 } else if (err < 0) {
4632 rcu_read_unlock();
4633 goto nla_put_failure;
4636 nla_nest_end(skb, af);
4639 rcu_read_unlock();
4641 nla_nest_end(skb, attr);
4643 *idxattr = 0;
4646 nlmsg_end(skb, nlh);
4648 return 0;
4650 nla_put_failure:
4651 /* not a multi message or no progress mean a real error */
4652 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4653 nlmsg_cancel(skb, nlh);
4654 else
4655 nlmsg_end(skb, nlh);
4657 return -EMSGSIZE;
4660 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4661 u32 filter_mask)
4663 size_t size = 0;
4665 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4666 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4668 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4669 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4670 int attr = IFLA_STATS_LINK_XSTATS;
4672 if (ops && ops->get_linkxstats_size) {
4673 size += nla_total_size(ops->get_linkxstats_size(dev,
4674 attr));
4675 /* for IFLA_STATS_LINK_XSTATS */
4676 size += nla_total_size(0);
4680 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4681 struct net_device *_dev = (struct net_device *)dev;
4682 const struct rtnl_link_ops *ops = NULL;
4683 const struct net_device *master;
4685 /* netdev_master_upper_dev_get can't take const */
4686 master = netdev_master_upper_dev_get(_dev);
4687 if (master)
4688 ops = master->rtnl_link_ops;
4689 if (ops && ops->get_linkxstats_size) {
4690 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4692 size += nla_total_size(ops->get_linkxstats_size(dev,
4693 attr));
4694 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4695 size += nla_total_size(0);
4699 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4700 size += rtnl_get_offload_stats_size(dev);
4702 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4703 struct rtnl_af_ops *af_ops;
4705 /* for IFLA_STATS_AF_SPEC */
4706 size += nla_total_size(0);
4708 rcu_read_lock();
4709 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4710 if (af_ops->get_stats_af_size) {
4711 size += nla_total_size(
4712 af_ops->get_stats_af_size(dev));
4714 /* for AF_* */
4715 size += nla_total_size(0);
4718 rcu_read_unlock();
4721 return size;
4724 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4725 struct netlink_ext_ack *extack)
4727 struct net *net = sock_net(skb->sk);
4728 struct net_device *dev = NULL;
4729 int idxattr = 0, prividx = 0;
4730 struct if_stats_msg *ifsm;
4731 struct sk_buff *nskb;
4732 u32 filter_mask;
4733 int err;
4735 if (nlmsg_len(nlh) < sizeof(*ifsm))
4736 return -EINVAL;
4738 ifsm = nlmsg_data(nlh);
4739 if (ifsm->ifindex > 0)
4740 dev = __dev_get_by_index(net, ifsm->ifindex);
4741 else
4742 return -EINVAL;
4744 if (!dev)
4745 return -ENODEV;
4747 filter_mask = ifsm->filter_mask;
4748 if (!filter_mask)
4749 return -EINVAL;
4751 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4752 if (!nskb)
4753 return -ENOBUFS;
4755 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4756 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4757 0, filter_mask, &idxattr, &prividx);
4758 if (err < 0) {
4759 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4760 WARN_ON(err == -EMSGSIZE);
4761 kfree_skb(nskb);
4762 } else {
4763 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4766 return err;
4769 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4771 struct netlink_ext_ack *extack = cb->extack;
4772 int h, s_h, err, s_idx, s_idxattr, s_prividx;
4773 struct net *net = sock_net(skb->sk);
4774 unsigned int flags = NLM_F_MULTI;
4775 struct if_stats_msg *ifsm;
4776 struct hlist_head *head;
4777 struct net_device *dev;
4778 u32 filter_mask = 0;
4779 int idx = 0;
4781 s_h = cb->args[0];
4782 s_idx = cb->args[1];
4783 s_idxattr = cb->args[2];
4784 s_prividx = cb->args[3];
4786 cb->seq = net->dev_base_seq;
4788 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) {
4789 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
4790 return -EINVAL;
4793 ifsm = nlmsg_data(cb->nlh);
4795 /* only requests using strict checks can pass data to influence
4796 * the dump. The legacy exception is filter_mask.
4798 if (cb->strict_check) {
4799 if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) {
4800 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4801 return -EINVAL;
4803 if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) {
4804 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4805 return -EINVAL;
4809 filter_mask = ifsm->filter_mask;
4810 if (!filter_mask) {
4811 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
4812 return -EINVAL;
4815 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4816 idx = 0;
4817 head = &net->dev_index_head[h];
4818 hlist_for_each_entry(dev, head, index_hlist) {
4819 if (idx < s_idx)
4820 goto cont;
4821 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4822 NETLINK_CB(cb->skb).portid,
4823 cb->nlh->nlmsg_seq, 0,
4824 flags, filter_mask,
4825 &s_idxattr, &s_prividx);
4826 /* If we ran out of room on the first message,
4827 * we're in trouble
4829 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4831 if (err < 0)
4832 goto out;
4833 s_prividx = 0;
4834 s_idxattr = 0;
4835 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4836 cont:
4837 idx++;
4840 out:
4841 cb->args[3] = s_prividx;
4842 cb->args[2] = s_idxattr;
4843 cb->args[1] = idx;
4844 cb->args[0] = h;
4846 return skb->len;
4849 /* Process one rtnetlink message. */
4851 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4852 struct netlink_ext_ack *extack)
4854 struct net *net = sock_net(skb->sk);
4855 struct rtnl_link *link;
4856 struct module *owner;
4857 int err = -EOPNOTSUPP;
4858 rtnl_doit_func doit;
4859 unsigned int flags;
4860 int kind;
4861 int family;
4862 int type;
4864 type = nlh->nlmsg_type;
4865 if (type > RTM_MAX)
4866 return -EOPNOTSUPP;
4868 type -= RTM_BASE;
4870 /* All the messages must have at least 1 byte length */
4871 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4872 return 0;
4874 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4875 kind = type&3;
4877 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4878 return -EPERM;
4880 rcu_read_lock();
4881 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4882 struct sock *rtnl;
4883 rtnl_dumpit_func dumpit;
4884 u16 min_dump_alloc = 0;
4886 link = rtnl_get_link(family, type);
4887 if (!link || !link->dumpit) {
4888 family = PF_UNSPEC;
4889 link = rtnl_get_link(family, type);
4890 if (!link || !link->dumpit)
4891 goto err_unlock;
4893 owner = link->owner;
4894 dumpit = link->dumpit;
4896 if (type == RTM_GETLINK - RTM_BASE)
4897 min_dump_alloc = rtnl_calcit(skb, nlh);
4899 err = 0;
4900 /* need to do this before rcu_read_unlock() */
4901 if (!try_module_get(owner))
4902 err = -EPROTONOSUPPORT;
4904 rcu_read_unlock();
4906 rtnl = net->rtnl;
4907 if (err == 0) {
4908 struct netlink_dump_control c = {
4909 .dump = dumpit,
4910 .min_dump_alloc = min_dump_alloc,
4911 .module = owner,
4913 err = netlink_dump_start(rtnl, skb, nlh, &c);
4914 /* netlink_dump_start() will keep a reference on
4915 * module if dump is still in progress.
4917 module_put(owner);
4919 return err;
4922 link = rtnl_get_link(family, type);
4923 if (!link || !link->doit) {
4924 family = PF_UNSPEC;
4925 link = rtnl_get_link(PF_UNSPEC, type);
4926 if (!link || !link->doit)
4927 goto out_unlock;
4930 owner = link->owner;
4931 if (!try_module_get(owner)) {
4932 err = -EPROTONOSUPPORT;
4933 goto out_unlock;
4936 flags = link->flags;
4937 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4938 doit = link->doit;
4939 rcu_read_unlock();
4940 if (doit)
4941 err = doit(skb, nlh, extack);
4942 module_put(owner);
4943 return err;
4945 rcu_read_unlock();
4947 rtnl_lock();
4948 link = rtnl_get_link(family, type);
4949 if (link && link->doit)
4950 err = link->doit(skb, nlh, extack);
4951 rtnl_unlock();
4953 module_put(owner);
4955 return err;
4957 out_unlock:
4958 rcu_read_unlock();
4959 return err;
4961 err_unlock:
4962 rcu_read_unlock();
4963 return -EOPNOTSUPP;
4966 static void rtnetlink_rcv(struct sk_buff *skb)
4968 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4971 static int rtnetlink_bind(struct net *net, int group)
4973 switch (group) {
4974 case RTNLGRP_IPV4_MROUTE_R:
4975 case RTNLGRP_IPV6_MROUTE_R:
4976 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4977 return -EPERM;
4978 break;
4980 return 0;
4983 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4985 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4987 switch (event) {
4988 case NETDEV_REBOOT:
4989 case NETDEV_CHANGEMTU:
4990 case NETDEV_CHANGEADDR:
4991 case NETDEV_CHANGENAME:
4992 case NETDEV_FEAT_CHANGE:
4993 case NETDEV_BONDING_FAILOVER:
4994 case NETDEV_POST_TYPE_CHANGE:
4995 case NETDEV_NOTIFY_PEERS:
4996 case NETDEV_CHANGEUPPER:
4997 case NETDEV_RESEND_IGMP:
4998 case NETDEV_CHANGEINFODATA:
4999 case NETDEV_CHANGELOWERSTATE:
5000 case NETDEV_CHANGE_TX_QUEUE_LEN:
5001 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
5002 GFP_KERNEL, NULL, 0);
5003 break;
5004 default:
5005 break;
5007 return NOTIFY_DONE;
5010 static struct notifier_block rtnetlink_dev_notifier = {
5011 .notifier_call = rtnetlink_event,
5015 static int __net_init rtnetlink_net_init(struct net *net)
5017 struct sock *sk;
5018 struct netlink_kernel_cfg cfg = {
5019 .groups = RTNLGRP_MAX,
5020 .input = rtnetlink_rcv,
5021 .cb_mutex = &rtnl_mutex,
5022 .flags = NL_CFG_F_NONROOT_RECV,
5023 .bind = rtnetlink_bind,
5026 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5027 if (!sk)
5028 return -ENOMEM;
5029 net->rtnl = sk;
5030 return 0;
5033 static void __net_exit rtnetlink_net_exit(struct net *net)
5035 netlink_kernel_release(net->rtnl);
5036 net->rtnl = NULL;
5039 static struct pernet_operations rtnetlink_net_ops = {
5040 .init = rtnetlink_net_init,
5041 .exit = rtnetlink_net_exit,
5044 void __init rtnetlink_init(void)
5046 if (register_pernet_subsys(&rtnetlink_net_ops))
5047 panic("rtnetlink_init: cannot initialize rtnetlink\n");
5049 register_netdevice_notifier(&rtnetlink_dev_notifier);
5051 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5052 rtnl_dump_ifinfo, 0);
5053 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5054 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5055 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5057 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5058 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5059 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5061 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5062 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5063 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
5065 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5066 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5067 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5069 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,