rtnetlink: check DO_SETLINK_NOTIFY correctly in do_setlink
[linux/fpc-iii.git] / net / core / rtnetlink.c
blob3e98fb5575981cce5b14ff7e738295c3b094ce91
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 struct rtnl_link {
63 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
65 unsigned int flags;
68 static DEFINE_MUTEX(rtnl_mutex);
70 void rtnl_lock(void)
72 mutex_lock(&rtnl_mutex);
74 EXPORT_SYMBOL(rtnl_lock);
76 static struct sk_buff *defer_kfree_skb_list;
77 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
79 if (head && tail) {
80 tail->next = defer_kfree_skb_list;
81 defer_kfree_skb_list = head;
84 EXPORT_SYMBOL(rtnl_kfree_skbs);
86 void __rtnl_unlock(void)
88 struct sk_buff *head = defer_kfree_skb_list;
90 defer_kfree_skb_list = NULL;
92 mutex_unlock(&rtnl_mutex);
94 while (head) {
95 struct sk_buff *next = head->next;
97 kfree_skb(head);
98 cond_resched();
99 head = next;
103 void rtnl_unlock(void)
105 /* This fellow will unlock it for us. */
106 netdev_run_todo();
108 EXPORT_SYMBOL(rtnl_unlock);
110 int rtnl_trylock(void)
112 return mutex_trylock(&rtnl_mutex);
114 EXPORT_SYMBOL(rtnl_trylock);
116 int rtnl_is_locked(void)
118 return mutex_is_locked(&rtnl_mutex);
120 EXPORT_SYMBOL(rtnl_is_locked);
122 #ifdef CONFIG_PROVE_LOCKING
123 bool lockdep_rtnl_is_held(void)
125 return lockdep_is_held(&rtnl_mutex);
127 EXPORT_SYMBOL(lockdep_rtnl_is_held);
128 #endif /* #ifdef CONFIG_PROVE_LOCKING */
130 static struct rtnl_link __rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
131 static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
133 static inline int rtm_msgindex(int msgtype)
135 int msgindex = msgtype - RTM_BASE;
138 * msgindex < 0 implies someone tried to register a netlink
139 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
140 * the message type has not been added to linux/rtnetlink.h
142 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
144 return msgindex;
148 * __rtnl_register - Register a rtnetlink message type
149 * @protocol: Protocol family or PF_UNSPEC
150 * @msgtype: rtnetlink message type
151 * @doit: Function pointer called for each request message
152 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
153 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
155 * Registers the specified function pointers (at least one of them has
156 * to be non-NULL) to be called whenever a request message for the
157 * specified protocol family and message type is received.
159 * The special protocol family PF_UNSPEC may be used to define fallback
160 * function pointers for the case when no entry for the specific protocol
161 * family exists.
163 * Returns 0 on success or a negative error code.
165 int __rtnl_register(int protocol, int msgtype,
166 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
167 unsigned int flags)
169 struct rtnl_link *tab;
170 int msgindex;
172 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
173 msgindex = rtm_msgindex(msgtype);
175 tab = rcu_dereference_raw(rtnl_msg_handlers[protocol]);
176 if (tab == NULL) {
177 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
178 if (tab == NULL)
179 return -ENOBUFS;
181 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
184 if (doit)
185 tab[msgindex].doit = doit;
186 if (dumpit)
187 tab[msgindex].dumpit = dumpit;
188 tab[msgindex].flags |= flags;
190 return 0;
192 EXPORT_SYMBOL_GPL(__rtnl_register);
195 * rtnl_register - Register a rtnetlink message type
197 * Identical to __rtnl_register() but panics on failure. This is useful
198 * as failure of this function is very unlikely, it can only happen due
199 * to lack of memory when allocating the chain to store all message
200 * handlers for a protocol. Meant for use in init functions where lack
201 * of memory implies no sense in continuing.
203 void rtnl_register(int protocol, int msgtype,
204 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
205 unsigned int flags)
207 if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
208 panic("Unable to register rtnetlink message handler, "
209 "protocol = %d, message type = %d\n",
210 protocol, msgtype);
212 EXPORT_SYMBOL_GPL(rtnl_register);
215 * rtnl_unregister - Unregister a rtnetlink message type
216 * @protocol: Protocol family or PF_UNSPEC
217 * @msgtype: rtnetlink message type
219 * Returns 0 on success or a negative error code.
221 int rtnl_unregister(int protocol, int msgtype)
223 struct rtnl_link *handlers;
224 int msgindex;
226 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
227 msgindex = rtm_msgindex(msgtype);
229 rtnl_lock();
230 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
231 if (!handlers) {
232 rtnl_unlock();
233 return -ENOENT;
236 handlers[msgindex].doit = NULL;
237 handlers[msgindex].dumpit = NULL;
238 handlers[msgindex].flags = 0;
239 rtnl_unlock();
241 return 0;
243 EXPORT_SYMBOL_GPL(rtnl_unregister);
246 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
247 * @protocol : Protocol family or PF_UNSPEC
249 * Identical to calling rtnl_unregster() for all registered message types
250 * of a certain protocol family.
252 void rtnl_unregister_all(int protocol)
254 struct rtnl_link *handlers;
256 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
258 rtnl_lock();
259 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
260 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
261 rtnl_unlock();
263 synchronize_net();
265 while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 1)
266 schedule();
267 kfree(handlers);
269 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
271 static LIST_HEAD(link_ops);
273 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
275 const struct rtnl_link_ops *ops;
277 list_for_each_entry(ops, &link_ops, list) {
278 if (!strcmp(ops->kind, kind))
279 return ops;
281 return NULL;
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
292 * Returns 0 on success or a negative error code.
294 int __rtnl_link_register(struct rtnl_link_ops *ops)
296 if (rtnl_link_ops_get(ops->kind))
297 return -EEXIST;
299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
304 if (ops->setup && !ops->dellink)
305 ops->dellink = unregister_netdevice_queue;
307 list_add_tail(&ops->list, &link_ops);
308 return 0;
310 EXPORT_SYMBOL_GPL(__rtnl_link_register);
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
316 * Returns 0 on success or a negative error code.
318 int rtnl_link_register(struct rtnl_link_ops *ops)
320 int err;
322 rtnl_lock();
323 err = __rtnl_link_register(ops);
324 rtnl_unlock();
325 return err;
327 EXPORT_SYMBOL_GPL(rtnl_link_register);
329 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
331 struct net_device *dev;
332 LIST_HEAD(list_kill);
334 for_each_netdev(net, dev) {
335 if (dev->rtnl_link_ops == ops)
336 ops->dellink(dev, &list_kill);
338 unregister_netdevice_many(&list_kill);
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
345 * The caller must hold the rtnl_mutex.
347 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
349 struct net *net;
351 for_each_net(net) {
352 __rtnl_kill_links(net, ops);
354 list_del(&ops->list);
356 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
358 /* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
361 static void rtnl_lock_unregistering_all(void)
363 struct net *net;
364 bool unregistering;
365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
367 add_wait_queue(&netdev_unregistering_wq, &wait);
368 for (;;) {
369 unregistering = false;
370 rtnl_lock();
371 for_each_net(net) {
372 if (net->dev_unreg_count > 0) {
373 unregistering = true;
374 break;
377 if (!unregistering)
378 break;
379 __rtnl_unlock();
381 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
383 remove_wait_queue(&netdev_unregistering_wq, &wait);
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
390 void rtnl_link_unregister(struct rtnl_link_ops *ops)
392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex);
394 rtnl_lock_unregistering_all();
395 __rtnl_link_unregister(ops);
396 rtnl_unlock();
397 mutex_unlock(&net_mutex);
399 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
401 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
403 struct net_device *master_dev;
404 const struct rtnl_link_ops *ops;
405 size_t size = 0;
407 rcu_read_lock();
409 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
410 if (!master_dev)
411 goto out;
413 ops = master_dev->rtnl_link_ops;
414 if (!ops || !ops->get_slave_size)
415 goto out;
416 /* IFLA_INFO_SLAVE_DATA + nested data */
417 size = nla_total_size(sizeof(struct nlattr)) +
418 ops->get_slave_size(master_dev, dev);
420 out:
421 rcu_read_unlock();
422 return size;
425 static size_t rtnl_link_get_size(const struct net_device *dev)
427 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
428 size_t size;
430 if (!ops)
431 return 0;
433 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
436 if (ops->get_size)
437 /* IFLA_INFO_DATA + nested data */
438 size += nla_total_size(sizeof(struct nlattr)) +
439 ops->get_size(dev);
441 if (ops->get_xstats_size)
442 /* IFLA_INFO_XSTATS */
443 size += nla_total_size(ops->get_xstats_size(dev));
445 size += rtnl_link_get_slave_info_data_size(dev);
447 return size;
450 static LIST_HEAD(rtnl_af_ops);
452 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
454 const struct rtnl_af_ops *ops;
456 list_for_each_entry(ops, &rtnl_af_ops, list) {
457 if (ops->family == family)
458 return ops;
461 return NULL;
465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
468 * Returns 0 on success or a negative error code.
470 void rtnl_af_register(struct rtnl_af_ops *ops)
472 rtnl_lock();
473 list_add_tail(&ops->list, &rtnl_af_ops);
474 rtnl_unlock();
476 EXPORT_SYMBOL_GPL(rtnl_af_register);
479 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
482 * The caller must hold the rtnl_mutex.
484 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
486 list_del(&ops->list);
488 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
491 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492 * @ops: struct rtnl_af_ops * to unregister
494 void rtnl_af_unregister(struct rtnl_af_ops *ops)
496 rtnl_lock();
497 __rtnl_af_unregister(ops);
498 rtnl_unlock();
500 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
502 static size_t rtnl_link_get_af_size(const struct net_device *dev,
503 u32 ext_filter_mask)
505 struct rtnl_af_ops *af_ops;
506 size_t size;
508 /* IFLA_AF_SPEC */
509 size = nla_total_size(sizeof(struct nlattr));
511 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
512 if (af_ops->get_link_af_size) {
513 /* AF_* + nested data */
514 size += nla_total_size(sizeof(struct nlattr)) +
515 af_ops->get_link_af_size(dev, ext_filter_mask);
519 return size;
522 static bool rtnl_have_link_slave_info(const struct net_device *dev)
524 struct net_device *master_dev;
526 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
527 if (master_dev && master_dev->rtnl_link_ops)
528 return true;
529 return false;
532 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
533 const struct net_device *dev)
535 struct net_device *master_dev;
536 const struct rtnl_link_ops *ops;
537 struct nlattr *slave_data;
538 int err;
540 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
541 if (!master_dev)
542 return 0;
543 ops = master_dev->rtnl_link_ops;
544 if (!ops)
545 return 0;
546 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
547 return -EMSGSIZE;
548 if (ops->fill_slave_info) {
549 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
550 if (!slave_data)
551 return -EMSGSIZE;
552 err = ops->fill_slave_info(skb, master_dev, dev);
553 if (err < 0)
554 goto err_cancel_slave_data;
555 nla_nest_end(skb, slave_data);
557 return 0;
559 err_cancel_slave_data:
560 nla_nest_cancel(skb, slave_data);
561 return err;
564 static int rtnl_link_info_fill(struct sk_buff *skb,
565 const struct net_device *dev)
567 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
568 struct nlattr *data;
569 int err;
571 if (!ops)
572 return 0;
573 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
574 return -EMSGSIZE;
575 if (ops->fill_xstats) {
576 err = ops->fill_xstats(skb, dev);
577 if (err < 0)
578 return err;
580 if (ops->fill_info) {
581 data = nla_nest_start(skb, IFLA_INFO_DATA);
582 if (data == NULL)
583 return -EMSGSIZE;
584 err = ops->fill_info(skb, dev);
585 if (err < 0)
586 goto err_cancel_data;
587 nla_nest_end(skb, data);
589 return 0;
591 err_cancel_data:
592 nla_nest_cancel(skb, data);
593 return err;
596 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
598 struct nlattr *linkinfo;
599 int err = -EMSGSIZE;
601 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
602 if (linkinfo == NULL)
603 goto out;
605 err = rtnl_link_info_fill(skb, dev);
606 if (err < 0)
607 goto err_cancel_link;
609 err = rtnl_link_slave_info_fill(skb, dev);
610 if (err < 0)
611 goto err_cancel_link;
613 nla_nest_end(skb, linkinfo);
614 return 0;
616 err_cancel_link:
617 nla_nest_cancel(skb, linkinfo);
618 out:
619 return err;
622 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
624 struct sock *rtnl = net->rtnl;
625 int err = 0;
627 NETLINK_CB(skb).dst_group = group;
628 if (echo)
629 refcount_inc(&skb->users);
630 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
631 if (echo)
632 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
633 return err;
636 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
638 struct sock *rtnl = net->rtnl;
640 return nlmsg_unicast(rtnl, skb, pid);
642 EXPORT_SYMBOL(rtnl_unicast);
644 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
645 struct nlmsghdr *nlh, gfp_t flags)
647 struct sock *rtnl = net->rtnl;
648 int report = 0;
650 if (nlh)
651 report = nlmsg_report(nlh);
653 nlmsg_notify(rtnl, skb, pid, group, report, flags);
655 EXPORT_SYMBOL(rtnl_notify);
657 void rtnl_set_sk_err(struct net *net, u32 group, int error)
659 struct sock *rtnl = net->rtnl;
661 netlink_set_err(rtnl, 0, group, error);
663 EXPORT_SYMBOL(rtnl_set_sk_err);
665 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
667 struct nlattr *mx;
668 int i, valid = 0;
670 mx = nla_nest_start(skb, RTA_METRICS);
671 if (mx == NULL)
672 return -ENOBUFS;
674 for (i = 0; i < RTAX_MAX; i++) {
675 if (metrics[i]) {
676 if (i == RTAX_CC_ALGO - 1) {
677 char tmp[TCP_CA_NAME_MAX], *name;
679 name = tcp_ca_get_name_by_key(metrics[i], tmp);
680 if (!name)
681 continue;
682 if (nla_put_string(skb, i + 1, name))
683 goto nla_put_failure;
684 } else if (i == RTAX_FEATURES - 1) {
685 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
687 if (!user_features)
688 continue;
689 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
690 if (nla_put_u32(skb, i + 1, user_features))
691 goto nla_put_failure;
692 } else {
693 if (nla_put_u32(skb, i + 1, metrics[i]))
694 goto nla_put_failure;
696 valid++;
700 if (!valid) {
701 nla_nest_cancel(skb, mx);
702 return 0;
705 return nla_nest_end(skb, mx);
707 nla_put_failure:
708 nla_nest_cancel(skb, mx);
709 return -EMSGSIZE;
711 EXPORT_SYMBOL(rtnetlink_put_metrics);
713 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
714 long expires, u32 error)
716 struct rta_cacheinfo ci = {
717 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
718 .rta_used = dst->__use,
719 .rta_clntref = atomic_read(&(dst->__refcnt)),
720 .rta_error = error,
721 .rta_id = id,
724 if (expires) {
725 unsigned long clock;
727 clock = jiffies_to_clock_t(abs(expires));
728 clock = min_t(unsigned long, clock, INT_MAX);
729 ci.rta_expires = (expires > 0) ? clock : -clock;
731 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
733 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
735 static void set_operstate(struct net_device *dev, unsigned char transition)
737 unsigned char operstate = dev->operstate;
739 switch (transition) {
740 case IF_OPER_UP:
741 if ((operstate == IF_OPER_DORMANT ||
742 operstate == IF_OPER_UNKNOWN) &&
743 !netif_dormant(dev))
744 operstate = IF_OPER_UP;
745 break;
747 case IF_OPER_DORMANT:
748 if (operstate == IF_OPER_UP ||
749 operstate == IF_OPER_UNKNOWN)
750 operstate = IF_OPER_DORMANT;
751 break;
754 if (dev->operstate != operstate) {
755 write_lock_bh(&dev_base_lock);
756 dev->operstate = operstate;
757 write_unlock_bh(&dev_base_lock);
758 netdev_state_change(dev);
762 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
764 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
765 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
768 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
769 const struct ifinfomsg *ifm)
771 unsigned int flags = ifm->ifi_flags;
773 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
774 if (ifm->ifi_change)
775 flags = (flags & ifm->ifi_change) |
776 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
778 return flags;
781 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
782 const struct rtnl_link_stats64 *b)
784 a->rx_packets = b->rx_packets;
785 a->tx_packets = b->tx_packets;
786 a->rx_bytes = b->rx_bytes;
787 a->tx_bytes = b->tx_bytes;
788 a->rx_errors = b->rx_errors;
789 a->tx_errors = b->tx_errors;
790 a->rx_dropped = b->rx_dropped;
791 a->tx_dropped = b->tx_dropped;
793 a->multicast = b->multicast;
794 a->collisions = b->collisions;
796 a->rx_length_errors = b->rx_length_errors;
797 a->rx_over_errors = b->rx_over_errors;
798 a->rx_crc_errors = b->rx_crc_errors;
799 a->rx_frame_errors = b->rx_frame_errors;
800 a->rx_fifo_errors = b->rx_fifo_errors;
801 a->rx_missed_errors = b->rx_missed_errors;
803 a->tx_aborted_errors = b->tx_aborted_errors;
804 a->tx_carrier_errors = b->tx_carrier_errors;
805 a->tx_fifo_errors = b->tx_fifo_errors;
806 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
807 a->tx_window_errors = b->tx_window_errors;
809 a->rx_compressed = b->rx_compressed;
810 a->tx_compressed = b->tx_compressed;
812 a->rx_nohandler = b->rx_nohandler;
815 /* All VF info */
816 static inline int rtnl_vfinfo_size(const struct net_device *dev,
817 u32 ext_filter_mask)
819 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
820 int num_vfs = dev_num_vf(dev->dev.parent);
821 size_t size = nla_total_size(0);
822 size += num_vfs *
823 (nla_total_size(0) +
824 nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
827 nla_total_size(MAX_VLAN_LIST_LEN *
828 sizeof(struct ifla_vf_vlan_info)) +
829 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
830 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
831 nla_total_size(sizeof(struct ifla_vf_rate)) +
832 nla_total_size(sizeof(struct ifla_vf_link_state)) +
833 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
834 nla_total_size(0) + /* nest IFLA_VF_STATS */
835 /* IFLA_VF_STATS_RX_PACKETS */
836 nla_total_size_64bit(sizeof(__u64)) +
837 /* IFLA_VF_STATS_TX_PACKETS */
838 nla_total_size_64bit(sizeof(__u64)) +
839 /* IFLA_VF_STATS_RX_BYTES */
840 nla_total_size_64bit(sizeof(__u64)) +
841 /* IFLA_VF_STATS_TX_BYTES */
842 nla_total_size_64bit(sizeof(__u64)) +
843 /* IFLA_VF_STATS_BROADCAST */
844 nla_total_size_64bit(sizeof(__u64)) +
845 /* IFLA_VF_STATS_MULTICAST */
846 nla_total_size_64bit(sizeof(__u64)) +
847 nla_total_size(sizeof(struct ifla_vf_trust)));
848 return size;
849 } else
850 return 0;
853 static size_t rtnl_port_size(const struct net_device *dev,
854 u32 ext_filter_mask)
856 size_t port_size = nla_total_size(4) /* PORT_VF */
857 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
858 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
859 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
860 + nla_total_size(1) /* PROT_VDP_REQUEST */
861 + nla_total_size(2); /* PORT_VDP_RESPONSE */
862 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
863 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
864 + port_size;
865 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
866 + port_size;
868 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
869 !(ext_filter_mask & RTEXT_FILTER_VF))
870 return 0;
871 if (dev_num_vf(dev->dev.parent))
872 return port_self_size + vf_ports_size +
873 vf_port_size * dev_num_vf(dev->dev.parent);
874 else
875 return port_self_size;
878 static size_t rtnl_xdp_size(void)
880 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
881 nla_total_size(1) + /* XDP_ATTACHED */
882 nla_total_size(4); /* XDP_PROG_ID */
884 return xdp_size;
887 static noinline size_t if_nlmsg_size(const struct net_device *dev,
888 u32 ext_filter_mask)
890 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
891 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
892 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
893 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
894 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
895 + nla_total_size(sizeof(struct rtnl_link_stats))
896 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
897 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
898 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
899 + nla_total_size(4) /* IFLA_TXQLEN */
900 + nla_total_size(4) /* IFLA_WEIGHT */
901 + nla_total_size(4) /* IFLA_MTU */
902 + nla_total_size(4) /* IFLA_LINK */
903 + nla_total_size(4) /* IFLA_MASTER */
904 + nla_total_size(1) /* IFLA_CARRIER */
905 + nla_total_size(4) /* IFLA_PROMISCUITY */
906 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
907 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
908 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
909 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
910 + nla_total_size(1) /* IFLA_OPERSTATE */
911 + nla_total_size(1) /* IFLA_LINKMODE */
912 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
913 + nla_total_size(4) /* IFLA_LINK_NETNSID */
914 + nla_total_size(4) /* IFLA_GROUP */
915 + nla_total_size(ext_filter_mask
916 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
917 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
918 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
919 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
920 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
921 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
922 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
923 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
924 + rtnl_xdp_size() /* IFLA_XDP */
925 + nla_total_size(4) /* IFLA_EVENT */
926 + nla_total_size(1); /* IFLA_PROTO_DOWN */
930 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
932 struct nlattr *vf_ports;
933 struct nlattr *vf_port;
934 int vf;
935 int err;
937 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
938 if (!vf_ports)
939 return -EMSGSIZE;
941 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
942 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
943 if (!vf_port)
944 goto nla_put_failure;
945 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
946 goto nla_put_failure;
947 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
948 if (err == -EMSGSIZE)
949 goto nla_put_failure;
950 if (err) {
951 nla_nest_cancel(skb, vf_port);
952 continue;
954 nla_nest_end(skb, vf_port);
957 nla_nest_end(skb, vf_ports);
959 return 0;
961 nla_put_failure:
962 nla_nest_cancel(skb, vf_ports);
963 return -EMSGSIZE;
966 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
968 struct nlattr *port_self;
969 int err;
971 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
972 if (!port_self)
973 return -EMSGSIZE;
975 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
976 if (err) {
977 nla_nest_cancel(skb, port_self);
978 return (err == -EMSGSIZE) ? err : 0;
981 nla_nest_end(skb, port_self);
983 return 0;
986 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
987 u32 ext_filter_mask)
989 int err;
991 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
992 !(ext_filter_mask & RTEXT_FILTER_VF))
993 return 0;
995 err = rtnl_port_self_fill(skb, dev);
996 if (err)
997 return err;
999 if (dev_num_vf(dev->dev.parent)) {
1000 err = rtnl_vf_ports_fill(skb, dev);
1001 if (err)
1002 return err;
1005 return 0;
1008 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1010 int err;
1011 struct netdev_phys_item_id ppid;
1013 err = dev_get_phys_port_id(dev, &ppid);
1014 if (err) {
1015 if (err == -EOPNOTSUPP)
1016 return 0;
1017 return err;
1020 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1021 return -EMSGSIZE;
1023 return 0;
1026 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1028 char name[IFNAMSIZ];
1029 int err;
1031 err = dev_get_phys_port_name(dev, name, sizeof(name));
1032 if (err) {
1033 if (err == -EOPNOTSUPP)
1034 return 0;
1035 return err;
1038 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1039 return -EMSGSIZE;
1041 return 0;
1044 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1046 int err;
1047 struct switchdev_attr attr = {
1048 .orig_dev = dev,
1049 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1050 .flags = SWITCHDEV_F_NO_RECURSE,
1053 err = switchdev_port_attr_get(dev, &attr);
1054 if (err) {
1055 if (err == -EOPNOTSUPP)
1056 return 0;
1057 return err;
1060 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1061 attr.u.ppid.id))
1062 return -EMSGSIZE;
1064 return 0;
1067 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1068 struct net_device *dev)
1070 struct rtnl_link_stats64 *sp;
1071 struct nlattr *attr;
1073 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1074 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1075 if (!attr)
1076 return -EMSGSIZE;
1078 sp = nla_data(attr);
1079 dev_get_stats(dev, sp);
1081 attr = nla_reserve(skb, IFLA_STATS,
1082 sizeof(struct rtnl_link_stats));
1083 if (!attr)
1084 return -EMSGSIZE;
1086 copy_rtnl_link_stats(nla_data(attr), sp);
1088 return 0;
1091 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1092 struct net_device *dev,
1093 int vfs_num,
1094 struct nlattr *vfinfo)
1096 struct ifla_vf_rss_query_en vf_rss_query_en;
1097 struct nlattr *vf, *vfstats, *vfvlanlist;
1098 struct ifla_vf_link_state vf_linkstate;
1099 struct ifla_vf_vlan_info vf_vlan_info;
1100 struct ifla_vf_spoofchk vf_spoofchk;
1101 struct ifla_vf_tx_rate vf_tx_rate;
1102 struct ifla_vf_stats vf_stats;
1103 struct ifla_vf_trust vf_trust;
1104 struct ifla_vf_vlan vf_vlan;
1105 struct ifla_vf_rate vf_rate;
1106 struct ifla_vf_mac vf_mac;
1107 struct ifla_vf_info ivi;
1109 memset(&ivi, 0, sizeof(ivi));
1111 /* Not all SR-IOV capable drivers support the
1112 * spoofcheck and "RSS query enable" query. Preset to
1113 * -1 so the user space tool can detect that the driver
1114 * didn't report anything.
1116 ivi.spoofchk = -1;
1117 ivi.rss_query_en = -1;
1118 ivi.trusted = -1;
1119 /* The default value for VF link state is "auto"
1120 * IFLA_VF_LINK_STATE_AUTO which equals zero
1122 ivi.linkstate = 0;
1123 /* VLAN Protocol by default is 802.1Q */
1124 ivi.vlan_proto = htons(ETH_P_8021Q);
1125 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1126 return 0;
1128 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1130 vf_mac.vf =
1131 vf_vlan.vf =
1132 vf_vlan_info.vf =
1133 vf_rate.vf =
1134 vf_tx_rate.vf =
1135 vf_spoofchk.vf =
1136 vf_linkstate.vf =
1137 vf_rss_query_en.vf =
1138 vf_trust.vf = ivi.vf;
1140 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1141 vf_vlan.vlan = ivi.vlan;
1142 vf_vlan.qos = ivi.qos;
1143 vf_vlan_info.vlan = ivi.vlan;
1144 vf_vlan_info.qos = ivi.qos;
1145 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1146 vf_tx_rate.rate = ivi.max_tx_rate;
1147 vf_rate.min_tx_rate = ivi.min_tx_rate;
1148 vf_rate.max_tx_rate = ivi.max_tx_rate;
1149 vf_spoofchk.setting = ivi.spoofchk;
1150 vf_linkstate.link_state = ivi.linkstate;
1151 vf_rss_query_en.setting = ivi.rss_query_en;
1152 vf_trust.setting = ivi.trusted;
1153 vf = nla_nest_start(skb, IFLA_VF_INFO);
1154 if (!vf)
1155 goto nla_put_vfinfo_failure;
1156 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1157 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1158 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1159 &vf_rate) ||
1160 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1161 &vf_tx_rate) ||
1162 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1163 &vf_spoofchk) ||
1164 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1165 &vf_linkstate) ||
1166 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1167 sizeof(vf_rss_query_en),
1168 &vf_rss_query_en) ||
1169 nla_put(skb, IFLA_VF_TRUST,
1170 sizeof(vf_trust), &vf_trust))
1171 goto nla_put_vf_failure;
1172 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1173 if (!vfvlanlist)
1174 goto nla_put_vf_failure;
1175 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1176 &vf_vlan_info)) {
1177 nla_nest_cancel(skb, vfvlanlist);
1178 goto nla_put_vf_failure;
1180 nla_nest_end(skb, vfvlanlist);
1181 memset(&vf_stats, 0, sizeof(vf_stats));
1182 if (dev->netdev_ops->ndo_get_vf_stats)
1183 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1184 &vf_stats);
1185 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1186 if (!vfstats)
1187 goto nla_put_vf_failure;
1188 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1189 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1191 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1193 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1195 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1197 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1198 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1199 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1200 nla_nest_cancel(skb, vfstats);
1201 goto nla_put_vf_failure;
1203 nla_nest_end(skb, vfstats);
1204 nla_nest_end(skb, vf);
1205 return 0;
1207 nla_put_vf_failure:
1208 nla_nest_cancel(skb, vf);
1209 nla_put_vfinfo_failure:
1210 nla_nest_cancel(skb, vfinfo);
1211 return -EMSGSIZE;
1214 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1216 struct rtnl_link_ifmap map;
1218 memset(&map, 0, sizeof(map));
1219 map.mem_start = dev->mem_start;
1220 map.mem_end = dev->mem_end;
1221 map.base_addr = dev->base_addr;
1222 map.irq = dev->irq;
1223 map.dma = dev->dma;
1224 map.port = dev->if_port;
1226 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1227 return -EMSGSIZE;
1229 return 0;
1232 static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
1234 const struct net_device_ops *ops = dev->netdev_ops;
1235 const struct bpf_prog *generic_xdp_prog;
1237 ASSERT_RTNL();
1239 *prog_id = 0;
1240 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1241 if (generic_xdp_prog) {
1242 *prog_id = generic_xdp_prog->aux->id;
1243 return XDP_ATTACHED_SKB;
1245 if (!ops->ndo_xdp)
1246 return XDP_ATTACHED_NONE;
1248 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
1251 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1253 struct nlattr *xdp;
1254 u32 prog_id;
1255 int err;
1257 xdp = nla_nest_start(skb, IFLA_XDP);
1258 if (!xdp)
1259 return -EMSGSIZE;
1261 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
1262 rtnl_xdp_attached_mode(dev, &prog_id));
1263 if (err)
1264 goto err_cancel;
1266 if (prog_id) {
1267 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1268 if (err)
1269 goto err_cancel;
1272 nla_nest_end(skb, xdp);
1273 return 0;
1275 err_cancel:
1276 nla_nest_cancel(skb, xdp);
1277 return err;
1280 static u32 rtnl_get_event(unsigned long event)
1282 u32 rtnl_event_type = IFLA_EVENT_NONE;
1284 switch (event) {
1285 case NETDEV_REBOOT:
1286 rtnl_event_type = IFLA_EVENT_REBOOT;
1287 break;
1288 case NETDEV_FEAT_CHANGE:
1289 rtnl_event_type = IFLA_EVENT_FEATURES;
1290 break;
1291 case NETDEV_BONDING_FAILOVER:
1292 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1293 break;
1294 case NETDEV_NOTIFY_PEERS:
1295 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1296 break;
1297 case NETDEV_RESEND_IGMP:
1298 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1299 break;
1300 case NETDEV_CHANGEINFODATA:
1301 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1302 break;
1303 default:
1304 break;
1307 return rtnl_event_type;
1310 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1311 int type, u32 pid, u32 seq, u32 change,
1312 unsigned int flags, u32 ext_filter_mask,
1313 u32 event)
1315 struct ifinfomsg *ifm;
1316 struct nlmsghdr *nlh;
1317 struct nlattr *af_spec;
1318 struct rtnl_af_ops *af_ops;
1319 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1321 ASSERT_RTNL();
1322 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1323 if (nlh == NULL)
1324 return -EMSGSIZE;
1326 ifm = nlmsg_data(nlh);
1327 ifm->ifi_family = AF_UNSPEC;
1328 ifm->__ifi_pad = 0;
1329 ifm->ifi_type = dev->type;
1330 ifm->ifi_index = dev->ifindex;
1331 ifm->ifi_flags = dev_get_flags(dev);
1332 ifm->ifi_change = change;
1334 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1335 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1336 nla_put_u8(skb, IFLA_OPERSTATE,
1337 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1338 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1339 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1340 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1341 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1342 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1343 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1344 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1345 #ifdef CONFIG_RPS
1346 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1347 #endif
1348 (dev->ifindex != dev_get_iflink(dev) &&
1349 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1350 (upper_dev &&
1351 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1352 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1353 (dev->qdisc &&
1354 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1355 (dev->ifalias &&
1356 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1357 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1358 atomic_read(&dev->carrier_changes)) ||
1359 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1360 goto nla_put_failure;
1362 if (event != IFLA_EVENT_NONE) {
1363 if (nla_put_u32(skb, IFLA_EVENT, event))
1364 goto nla_put_failure;
1367 if (rtnl_fill_link_ifmap(skb, dev))
1368 goto nla_put_failure;
1370 if (dev->addr_len) {
1371 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1372 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1373 goto nla_put_failure;
1376 if (rtnl_phys_port_id_fill(skb, dev))
1377 goto nla_put_failure;
1379 if (rtnl_phys_port_name_fill(skb, dev))
1380 goto nla_put_failure;
1382 if (rtnl_phys_switch_id_fill(skb, dev))
1383 goto nla_put_failure;
1385 if (rtnl_fill_stats(skb, dev))
1386 goto nla_put_failure;
1388 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1389 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1390 goto nla_put_failure;
1392 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1393 ext_filter_mask & RTEXT_FILTER_VF) {
1394 int i;
1395 struct nlattr *vfinfo;
1396 int num_vfs = dev_num_vf(dev->dev.parent);
1398 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1399 if (!vfinfo)
1400 goto nla_put_failure;
1401 for (i = 0; i < num_vfs; i++) {
1402 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1403 goto nla_put_failure;
1406 nla_nest_end(skb, vfinfo);
1409 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1410 goto nla_put_failure;
1412 if (rtnl_xdp_fill(skb, dev))
1413 goto nla_put_failure;
1415 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1416 if (rtnl_link_fill(skb, dev) < 0)
1417 goto nla_put_failure;
1420 if (dev->rtnl_link_ops &&
1421 dev->rtnl_link_ops->get_link_net) {
1422 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1424 if (!net_eq(dev_net(dev), link_net)) {
1425 int id = peernet2id_alloc(dev_net(dev), link_net);
1427 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1428 goto nla_put_failure;
1432 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1433 goto nla_put_failure;
1435 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1436 if (af_ops->fill_link_af) {
1437 struct nlattr *af;
1438 int err;
1440 if (!(af = nla_nest_start(skb, af_ops->family)))
1441 goto nla_put_failure;
1443 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1446 * Caller may return ENODATA to indicate that there
1447 * was no data to be dumped. This is not an error, it
1448 * means we should trim the attribute header and
1449 * continue.
1451 if (err == -ENODATA)
1452 nla_nest_cancel(skb, af);
1453 else if (err < 0)
1454 goto nla_put_failure;
1456 nla_nest_end(skb, af);
1460 nla_nest_end(skb, af_spec);
1462 nlmsg_end(skb, nlh);
1463 return 0;
1465 nla_put_failure:
1466 nlmsg_cancel(skb, nlh);
1467 return -EMSGSIZE;
1470 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1471 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1472 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1473 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1474 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1475 [IFLA_MTU] = { .type = NLA_U32 },
1476 [IFLA_LINK] = { .type = NLA_U32 },
1477 [IFLA_MASTER] = { .type = NLA_U32 },
1478 [IFLA_CARRIER] = { .type = NLA_U8 },
1479 [IFLA_TXQLEN] = { .type = NLA_U32 },
1480 [IFLA_WEIGHT] = { .type = NLA_U32 },
1481 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1482 [IFLA_LINKMODE] = { .type = NLA_U8 },
1483 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1484 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1485 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1486 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1487 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1488 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1489 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1490 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1491 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1492 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1493 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1494 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1495 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1496 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1497 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1498 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1499 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1500 [IFLA_XDP] = { .type = NLA_NESTED },
1501 [IFLA_EVENT] = { .type = NLA_U32 },
1502 [IFLA_GROUP] = { .type = NLA_U32 },
1505 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1506 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1507 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1508 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1509 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1512 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1513 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1514 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1515 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1516 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1517 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1518 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1519 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1520 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1521 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1522 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1523 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1524 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1527 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1528 [IFLA_PORT_VF] = { .type = NLA_U32 },
1529 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1530 .len = PORT_PROFILE_MAX },
1531 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1532 .len = PORT_UUID_MAX },
1533 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1534 .len = PORT_UUID_MAX },
1535 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1536 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1538 /* Unused, but we need to keep it here since user space could
1539 * fill it. It's also broken with regard to NLA_BINARY use in
1540 * combination with structs.
1542 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1543 .len = sizeof(struct ifla_port_vsi) },
1546 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1547 [IFLA_XDP_FD] = { .type = NLA_S32 },
1548 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1549 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1550 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1553 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1555 const struct rtnl_link_ops *ops = NULL;
1556 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1558 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1559 ifla_info_policy, NULL) < 0)
1560 return NULL;
1562 if (linfo[IFLA_INFO_KIND]) {
1563 char kind[MODULE_NAME_LEN];
1565 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1566 ops = rtnl_link_ops_get(kind);
1569 return ops;
1572 static bool link_master_filtered(struct net_device *dev, int master_idx)
1574 struct net_device *master;
1576 if (!master_idx)
1577 return false;
1579 master = netdev_master_upper_dev_get(dev);
1580 if (!master || master->ifindex != master_idx)
1581 return true;
1583 return false;
1586 static bool link_kind_filtered(const struct net_device *dev,
1587 const struct rtnl_link_ops *kind_ops)
1589 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1590 return true;
1592 return false;
1595 static bool link_dump_filtered(struct net_device *dev,
1596 int master_idx,
1597 const struct rtnl_link_ops *kind_ops)
1599 if (link_master_filtered(dev, master_idx) ||
1600 link_kind_filtered(dev, kind_ops))
1601 return true;
1603 return false;
1606 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1608 struct net *net = sock_net(skb->sk);
1609 int h, s_h;
1610 int idx = 0, s_idx;
1611 struct net_device *dev;
1612 struct hlist_head *head;
1613 struct nlattr *tb[IFLA_MAX+1];
1614 u32 ext_filter_mask = 0;
1615 const struct rtnl_link_ops *kind_ops = NULL;
1616 unsigned int flags = NLM_F_MULTI;
1617 int master_idx = 0;
1618 int err;
1619 int hdrlen;
1621 s_h = cb->args[0];
1622 s_idx = cb->args[1];
1624 /* A hack to preserve kernel<->userspace interface.
1625 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1626 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1627 * what iproute2 < v3.9.0 used.
1628 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1629 * attribute, its netlink message is shorter than struct ifinfomsg.
1631 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1632 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1634 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1635 ifla_policy, NULL) >= 0) {
1636 if (tb[IFLA_EXT_MASK])
1637 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1639 if (tb[IFLA_MASTER])
1640 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1642 if (tb[IFLA_LINKINFO])
1643 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1645 if (master_idx || kind_ops)
1646 flags |= NLM_F_DUMP_FILTERED;
1649 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1650 idx = 0;
1651 head = &net->dev_index_head[h];
1652 hlist_for_each_entry(dev, head, index_hlist) {
1653 if (link_dump_filtered(dev, master_idx, kind_ops))
1654 goto cont;
1655 if (idx < s_idx)
1656 goto cont;
1657 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1658 NETLINK_CB(cb->skb).portid,
1659 cb->nlh->nlmsg_seq, 0,
1660 flags,
1661 ext_filter_mask, 0);
1663 if (err < 0) {
1664 if (likely(skb->len))
1665 goto out;
1667 goto out_err;
1669 cont:
1670 idx++;
1673 out:
1674 err = skb->len;
1675 out_err:
1676 cb->args[1] = idx;
1677 cb->args[0] = h;
1678 cb->seq = net->dev_base_seq;
1679 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1681 return err;
1684 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1685 struct netlink_ext_ack *exterr)
1687 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1689 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1691 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1693 struct net *net;
1694 /* Examine the link attributes and figure out which
1695 * network namespace we are talking about.
1697 if (tb[IFLA_NET_NS_PID])
1698 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1699 else if (tb[IFLA_NET_NS_FD])
1700 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1701 else
1702 net = get_net(src_net);
1703 return net;
1705 EXPORT_SYMBOL(rtnl_link_get_net);
1707 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1709 if (dev) {
1710 if (tb[IFLA_ADDRESS] &&
1711 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1712 return -EINVAL;
1714 if (tb[IFLA_BROADCAST] &&
1715 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1716 return -EINVAL;
1719 if (tb[IFLA_AF_SPEC]) {
1720 struct nlattr *af;
1721 int rem, err;
1723 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1724 const struct rtnl_af_ops *af_ops;
1726 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1727 return -EAFNOSUPPORT;
1729 if (!af_ops->set_link_af)
1730 return -EOPNOTSUPP;
1732 if (af_ops->validate_link_af) {
1733 err = af_ops->validate_link_af(dev, af);
1734 if (err < 0)
1735 return err;
1740 return 0;
1743 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1744 int guid_type)
1746 const struct net_device_ops *ops = dev->netdev_ops;
1748 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1751 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1753 if (dev->type != ARPHRD_INFINIBAND)
1754 return -EOPNOTSUPP;
1756 return handle_infiniband_guid(dev, ivt, guid_type);
1759 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1761 const struct net_device_ops *ops = dev->netdev_ops;
1762 int err = -EINVAL;
1764 if (tb[IFLA_VF_MAC]) {
1765 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1767 err = -EOPNOTSUPP;
1768 if (ops->ndo_set_vf_mac)
1769 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1770 ivm->mac);
1771 if (err < 0)
1772 return err;
1775 if (tb[IFLA_VF_VLAN]) {
1776 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1778 err = -EOPNOTSUPP;
1779 if (ops->ndo_set_vf_vlan)
1780 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1781 ivv->qos,
1782 htons(ETH_P_8021Q));
1783 if (err < 0)
1784 return err;
1787 if (tb[IFLA_VF_VLAN_LIST]) {
1788 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1789 struct nlattr *attr;
1790 int rem, len = 0;
1792 err = -EOPNOTSUPP;
1793 if (!ops->ndo_set_vf_vlan)
1794 return err;
1796 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1797 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1798 nla_len(attr) < NLA_HDRLEN) {
1799 return -EINVAL;
1801 if (len >= MAX_VLAN_LIST_LEN)
1802 return -EOPNOTSUPP;
1803 ivvl[len] = nla_data(attr);
1805 len++;
1807 if (len == 0)
1808 return -EINVAL;
1810 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1811 ivvl[0]->qos, ivvl[0]->vlan_proto);
1812 if (err < 0)
1813 return err;
1816 if (tb[IFLA_VF_TX_RATE]) {
1817 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1818 struct ifla_vf_info ivf;
1820 err = -EOPNOTSUPP;
1821 if (ops->ndo_get_vf_config)
1822 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1823 if (err < 0)
1824 return err;
1826 err = -EOPNOTSUPP;
1827 if (ops->ndo_set_vf_rate)
1828 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1829 ivf.min_tx_rate,
1830 ivt->rate);
1831 if (err < 0)
1832 return err;
1835 if (tb[IFLA_VF_RATE]) {
1836 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1838 err = -EOPNOTSUPP;
1839 if (ops->ndo_set_vf_rate)
1840 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1841 ivt->min_tx_rate,
1842 ivt->max_tx_rate);
1843 if (err < 0)
1844 return err;
1847 if (tb[IFLA_VF_SPOOFCHK]) {
1848 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1850 err = -EOPNOTSUPP;
1851 if (ops->ndo_set_vf_spoofchk)
1852 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1853 ivs->setting);
1854 if (err < 0)
1855 return err;
1858 if (tb[IFLA_VF_LINK_STATE]) {
1859 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1861 err = -EOPNOTSUPP;
1862 if (ops->ndo_set_vf_link_state)
1863 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1864 ivl->link_state);
1865 if (err < 0)
1866 return err;
1869 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1870 struct ifla_vf_rss_query_en *ivrssq_en;
1872 err = -EOPNOTSUPP;
1873 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1874 if (ops->ndo_set_vf_rss_query_en)
1875 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1876 ivrssq_en->setting);
1877 if (err < 0)
1878 return err;
1881 if (tb[IFLA_VF_TRUST]) {
1882 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1884 err = -EOPNOTSUPP;
1885 if (ops->ndo_set_vf_trust)
1886 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1887 if (err < 0)
1888 return err;
1891 if (tb[IFLA_VF_IB_NODE_GUID]) {
1892 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1894 if (!ops->ndo_set_vf_guid)
1895 return -EOPNOTSUPP;
1897 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1900 if (tb[IFLA_VF_IB_PORT_GUID]) {
1901 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1903 if (!ops->ndo_set_vf_guid)
1904 return -EOPNOTSUPP;
1906 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1909 return err;
1912 static int do_set_master(struct net_device *dev, int ifindex)
1914 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1915 const struct net_device_ops *ops;
1916 int err;
1918 if (upper_dev) {
1919 if (upper_dev->ifindex == ifindex)
1920 return 0;
1921 ops = upper_dev->netdev_ops;
1922 if (ops->ndo_del_slave) {
1923 err = ops->ndo_del_slave(upper_dev, dev);
1924 if (err)
1925 return err;
1926 } else {
1927 return -EOPNOTSUPP;
1931 if (ifindex) {
1932 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1933 if (!upper_dev)
1934 return -EINVAL;
1935 ops = upper_dev->netdev_ops;
1936 if (ops->ndo_add_slave) {
1937 err = ops->ndo_add_slave(upper_dev, dev);
1938 if (err)
1939 return err;
1940 } else {
1941 return -EOPNOTSUPP;
1944 return 0;
1947 #define DO_SETLINK_MODIFIED 0x01
1948 /* notify flag means notify + modified. */
1949 #define DO_SETLINK_NOTIFY 0x03
1950 static int do_setlink(const struct sk_buff *skb,
1951 struct net_device *dev, struct ifinfomsg *ifm,
1952 struct netlink_ext_ack *extack,
1953 struct nlattr **tb, char *ifname, int status)
1955 const struct net_device_ops *ops = dev->netdev_ops;
1956 int err;
1958 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1959 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1960 if (IS_ERR(net)) {
1961 err = PTR_ERR(net);
1962 goto errout;
1964 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1965 put_net(net);
1966 err = -EPERM;
1967 goto errout;
1969 err = dev_change_net_namespace(dev, net, ifname);
1970 put_net(net);
1971 if (err)
1972 goto errout;
1973 status |= DO_SETLINK_MODIFIED;
1976 if (tb[IFLA_MAP]) {
1977 struct rtnl_link_ifmap *u_map;
1978 struct ifmap k_map;
1980 if (!ops->ndo_set_config) {
1981 err = -EOPNOTSUPP;
1982 goto errout;
1985 if (!netif_device_present(dev)) {
1986 err = -ENODEV;
1987 goto errout;
1990 u_map = nla_data(tb[IFLA_MAP]);
1991 k_map.mem_start = (unsigned long) u_map->mem_start;
1992 k_map.mem_end = (unsigned long) u_map->mem_end;
1993 k_map.base_addr = (unsigned short) u_map->base_addr;
1994 k_map.irq = (unsigned char) u_map->irq;
1995 k_map.dma = (unsigned char) u_map->dma;
1996 k_map.port = (unsigned char) u_map->port;
1998 err = ops->ndo_set_config(dev, &k_map);
1999 if (err < 0)
2000 goto errout;
2002 status |= DO_SETLINK_NOTIFY;
2005 if (tb[IFLA_ADDRESS]) {
2006 struct sockaddr *sa;
2007 int len;
2009 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2010 sizeof(*sa));
2011 sa = kmalloc(len, GFP_KERNEL);
2012 if (!sa) {
2013 err = -ENOMEM;
2014 goto errout;
2016 sa->sa_family = dev->type;
2017 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2018 dev->addr_len);
2019 err = dev_set_mac_address(dev, sa);
2020 kfree(sa);
2021 if (err)
2022 goto errout;
2023 status |= DO_SETLINK_MODIFIED;
2026 if (tb[IFLA_MTU]) {
2027 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2028 if (err < 0)
2029 goto errout;
2030 status |= DO_SETLINK_MODIFIED;
2033 if (tb[IFLA_GROUP]) {
2034 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2035 status |= DO_SETLINK_NOTIFY;
2039 * Interface selected by interface index but interface
2040 * name provided implies that a name change has been
2041 * requested.
2043 if (ifm->ifi_index > 0 && ifname[0]) {
2044 err = dev_change_name(dev, ifname);
2045 if (err < 0)
2046 goto errout;
2047 status |= DO_SETLINK_MODIFIED;
2050 if (tb[IFLA_IFALIAS]) {
2051 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2052 nla_len(tb[IFLA_IFALIAS]));
2053 if (err < 0)
2054 goto errout;
2055 status |= DO_SETLINK_NOTIFY;
2058 if (tb[IFLA_BROADCAST]) {
2059 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2060 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2063 if (ifm->ifi_flags || ifm->ifi_change) {
2064 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2065 if (err < 0)
2066 goto errout;
2069 if (tb[IFLA_MASTER]) {
2070 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2071 if (err)
2072 goto errout;
2073 status |= DO_SETLINK_MODIFIED;
2076 if (tb[IFLA_CARRIER]) {
2077 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2078 if (err)
2079 goto errout;
2080 status |= DO_SETLINK_MODIFIED;
2083 if (tb[IFLA_TXQLEN]) {
2084 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2085 unsigned int orig_len = dev->tx_queue_len;
2087 if (dev->tx_queue_len ^ value) {
2088 dev->tx_queue_len = value;
2089 err = call_netdevice_notifiers(
2090 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2091 err = notifier_to_errno(err);
2092 if (err) {
2093 dev->tx_queue_len = orig_len;
2094 goto errout;
2096 status |= DO_SETLINK_NOTIFY;
2100 if (tb[IFLA_OPERSTATE])
2101 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2103 if (tb[IFLA_LINKMODE]) {
2104 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2106 write_lock_bh(&dev_base_lock);
2107 if (dev->link_mode ^ value)
2108 status |= DO_SETLINK_NOTIFY;
2109 dev->link_mode = value;
2110 write_unlock_bh(&dev_base_lock);
2113 if (tb[IFLA_VFINFO_LIST]) {
2114 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2115 struct nlattr *attr;
2116 int rem;
2118 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2119 if (nla_type(attr) != IFLA_VF_INFO ||
2120 nla_len(attr) < NLA_HDRLEN) {
2121 err = -EINVAL;
2122 goto errout;
2124 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2125 ifla_vf_policy, NULL);
2126 if (err < 0)
2127 goto errout;
2128 err = do_setvfinfo(dev, vfinfo);
2129 if (err < 0)
2130 goto errout;
2131 status |= DO_SETLINK_NOTIFY;
2134 err = 0;
2136 if (tb[IFLA_VF_PORTS]) {
2137 struct nlattr *port[IFLA_PORT_MAX+1];
2138 struct nlattr *attr;
2139 int vf;
2140 int rem;
2142 err = -EOPNOTSUPP;
2143 if (!ops->ndo_set_vf_port)
2144 goto errout;
2146 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2147 if (nla_type(attr) != IFLA_VF_PORT ||
2148 nla_len(attr) < NLA_HDRLEN) {
2149 err = -EINVAL;
2150 goto errout;
2152 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2153 ifla_port_policy, NULL);
2154 if (err < 0)
2155 goto errout;
2156 if (!port[IFLA_PORT_VF]) {
2157 err = -EOPNOTSUPP;
2158 goto errout;
2160 vf = nla_get_u32(port[IFLA_PORT_VF]);
2161 err = ops->ndo_set_vf_port(dev, vf, port);
2162 if (err < 0)
2163 goto errout;
2164 status |= DO_SETLINK_NOTIFY;
2167 err = 0;
2169 if (tb[IFLA_PORT_SELF]) {
2170 struct nlattr *port[IFLA_PORT_MAX+1];
2172 err = nla_parse_nested(port, IFLA_PORT_MAX,
2173 tb[IFLA_PORT_SELF], ifla_port_policy,
2174 NULL);
2175 if (err < 0)
2176 goto errout;
2178 err = -EOPNOTSUPP;
2179 if (ops->ndo_set_vf_port)
2180 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2181 if (err < 0)
2182 goto errout;
2183 status |= DO_SETLINK_NOTIFY;
2186 if (tb[IFLA_AF_SPEC]) {
2187 struct nlattr *af;
2188 int rem;
2190 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2191 const struct rtnl_af_ops *af_ops;
2193 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2194 BUG();
2196 err = af_ops->set_link_af(dev, af);
2197 if (err < 0)
2198 goto errout;
2200 status |= DO_SETLINK_NOTIFY;
2203 err = 0;
2205 if (tb[IFLA_PROTO_DOWN]) {
2206 err = dev_change_proto_down(dev,
2207 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2208 if (err)
2209 goto errout;
2210 status |= DO_SETLINK_NOTIFY;
2213 if (tb[IFLA_XDP]) {
2214 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2215 u32 xdp_flags = 0;
2217 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2218 ifla_xdp_policy, NULL);
2219 if (err < 0)
2220 goto errout;
2222 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2223 err = -EINVAL;
2224 goto errout;
2227 if (xdp[IFLA_XDP_FLAGS]) {
2228 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2229 if (xdp_flags & ~XDP_FLAGS_MASK) {
2230 err = -EINVAL;
2231 goto errout;
2233 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2234 err = -EINVAL;
2235 goto errout;
2239 if (xdp[IFLA_XDP_FD]) {
2240 err = dev_change_xdp_fd(dev, extack,
2241 nla_get_s32(xdp[IFLA_XDP_FD]),
2242 xdp_flags);
2243 if (err)
2244 goto errout;
2245 status |= DO_SETLINK_NOTIFY;
2249 errout:
2250 if (status & DO_SETLINK_MODIFIED) {
2251 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2252 netdev_state_change(dev);
2254 if (err < 0)
2255 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",
2256 dev->name);
2259 return err;
2262 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2263 struct netlink_ext_ack *extack)
2265 struct net *net = sock_net(skb->sk);
2266 struct ifinfomsg *ifm;
2267 struct net_device *dev;
2268 int err;
2269 struct nlattr *tb[IFLA_MAX+1];
2270 char ifname[IFNAMSIZ];
2272 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2273 extack);
2274 if (err < 0)
2275 goto errout;
2277 if (tb[IFLA_IFNAME])
2278 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2279 else
2280 ifname[0] = '\0';
2282 err = -EINVAL;
2283 ifm = nlmsg_data(nlh);
2284 if (ifm->ifi_index > 0)
2285 dev = __dev_get_by_index(net, ifm->ifi_index);
2286 else if (tb[IFLA_IFNAME])
2287 dev = __dev_get_by_name(net, ifname);
2288 else
2289 goto errout;
2291 if (dev == NULL) {
2292 err = -ENODEV;
2293 goto errout;
2296 err = validate_linkmsg(dev, tb);
2297 if (err < 0)
2298 goto errout;
2300 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2301 errout:
2302 return err;
2305 static int rtnl_group_dellink(const struct net *net, int group)
2307 struct net_device *dev, *aux;
2308 LIST_HEAD(list_kill);
2309 bool found = false;
2311 if (!group)
2312 return -EPERM;
2314 for_each_netdev(net, dev) {
2315 if (dev->group == group) {
2316 const struct rtnl_link_ops *ops;
2318 found = true;
2319 ops = dev->rtnl_link_ops;
2320 if (!ops || !ops->dellink)
2321 return -EOPNOTSUPP;
2325 if (!found)
2326 return -ENODEV;
2328 for_each_netdev_safe(net, dev, aux) {
2329 if (dev->group == group) {
2330 const struct rtnl_link_ops *ops;
2332 ops = dev->rtnl_link_ops;
2333 ops->dellink(dev, &list_kill);
2336 unregister_netdevice_many(&list_kill);
2338 return 0;
2341 int rtnl_delete_link(struct net_device *dev)
2343 const struct rtnl_link_ops *ops;
2344 LIST_HEAD(list_kill);
2346 ops = dev->rtnl_link_ops;
2347 if (!ops || !ops->dellink)
2348 return -EOPNOTSUPP;
2350 ops->dellink(dev, &list_kill);
2351 unregister_netdevice_many(&list_kill);
2353 return 0;
2355 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2357 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2358 struct netlink_ext_ack *extack)
2360 struct net *net = sock_net(skb->sk);
2361 struct net_device *dev;
2362 struct ifinfomsg *ifm;
2363 char ifname[IFNAMSIZ];
2364 struct nlattr *tb[IFLA_MAX+1];
2365 int err;
2367 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2368 if (err < 0)
2369 return err;
2371 if (tb[IFLA_IFNAME])
2372 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2374 ifm = nlmsg_data(nlh);
2375 if (ifm->ifi_index > 0)
2376 dev = __dev_get_by_index(net, ifm->ifi_index);
2377 else if (tb[IFLA_IFNAME])
2378 dev = __dev_get_by_name(net, ifname);
2379 else if (tb[IFLA_GROUP])
2380 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2381 else
2382 return -EINVAL;
2384 if (!dev)
2385 return -ENODEV;
2387 return rtnl_delete_link(dev);
2390 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2392 unsigned int old_flags;
2393 int err;
2395 old_flags = dev->flags;
2396 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2397 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2398 if (err < 0)
2399 return err;
2402 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2404 __dev_notify_flags(dev, old_flags, ~0U);
2405 return 0;
2407 EXPORT_SYMBOL(rtnl_configure_link);
2409 struct net_device *rtnl_create_link(struct net *net,
2410 const char *ifname, unsigned char name_assign_type,
2411 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2413 struct net_device *dev;
2414 unsigned int num_tx_queues = 1;
2415 unsigned int num_rx_queues = 1;
2417 if (tb[IFLA_NUM_TX_QUEUES])
2418 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2419 else if (ops->get_num_tx_queues)
2420 num_tx_queues = ops->get_num_tx_queues();
2422 if (tb[IFLA_NUM_RX_QUEUES])
2423 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2424 else if (ops->get_num_rx_queues)
2425 num_rx_queues = ops->get_num_rx_queues();
2427 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2428 ops->setup, num_tx_queues, num_rx_queues);
2429 if (!dev)
2430 return ERR_PTR(-ENOMEM);
2432 dev_net_set(dev, net);
2433 dev->rtnl_link_ops = ops;
2434 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2436 if (tb[IFLA_MTU])
2437 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2438 if (tb[IFLA_ADDRESS]) {
2439 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2440 nla_len(tb[IFLA_ADDRESS]));
2441 dev->addr_assign_type = NET_ADDR_SET;
2443 if (tb[IFLA_BROADCAST])
2444 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2445 nla_len(tb[IFLA_BROADCAST]));
2446 if (tb[IFLA_TXQLEN])
2447 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2448 if (tb[IFLA_OPERSTATE])
2449 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2450 if (tb[IFLA_LINKMODE])
2451 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2452 if (tb[IFLA_GROUP])
2453 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2455 return dev;
2457 EXPORT_SYMBOL(rtnl_create_link);
2459 static int rtnl_group_changelink(const struct sk_buff *skb,
2460 struct net *net, int group,
2461 struct ifinfomsg *ifm,
2462 struct netlink_ext_ack *extack,
2463 struct nlattr **tb)
2465 struct net_device *dev, *aux;
2466 int err;
2468 for_each_netdev_safe(net, dev, aux) {
2469 if (dev->group == group) {
2470 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2471 if (err < 0)
2472 return err;
2476 return 0;
2479 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2480 struct netlink_ext_ack *extack)
2482 struct net *net = sock_net(skb->sk);
2483 const struct rtnl_link_ops *ops;
2484 const struct rtnl_link_ops *m_ops = NULL;
2485 struct net_device *dev;
2486 struct net_device *master_dev = NULL;
2487 struct ifinfomsg *ifm;
2488 char kind[MODULE_NAME_LEN];
2489 char ifname[IFNAMSIZ];
2490 struct nlattr *tb[IFLA_MAX+1];
2491 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2492 unsigned char name_assign_type = NET_NAME_USER;
2493 int err;
2495 #ifdef CONFIG_MODULES
2496 replay:
2497 #endif
2498 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2499 if (err < 0)
2500 return err;
2502 if (tb[IFLA_IFNAME])
2503 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2504 else
2505 ifname[0] = '\0';
2507 ifm = nlmsg_data(nlh);
2508 if (ifm->ifi_index > 0)
2509 dev = __dev_get_by_index(net, ifm->ifi_index);
2510 else {
2511 if (ifname[0])
2512 dev = __dev_get_by_name(net, ifname);
2513 else
2514 dev = NULL;
2517 if (dev) {
2518 master_dev = netdev_master_upper_dev_get(dev);
2519 if (master_dev)
2520 m_ops = master_dev->rtnl_link_ops;
2523 err = validate_linkmsg(dev, tb);
2524 if (err < 0)
2525 return err;
2527 if (tb[IFLA_LINKINFO]) {
2528 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2529 tb[IFLA_LINKINFO], ifla_info_policy,
2530 NULL);
2531 if (err < 0)
2532 return err;
2533 } else
2534 memset(linkinfo, 0, sizeof(linkinfo));
2536 if (linkinfo[IFLA_INFO_KIND]) {
2537 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2538 ops = rtnl_link_ops_get(kind);
2539 } else {
2540 kind[0] = '\0';
2541 ops = NULL;
2544 if (1) {
2545 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2546 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2547 struct nlattr **data = NULL;
2548 struct nlattr **slave_data = NULL;
2549 struct net *dest_net, *link_net = NULL;
2551 if (ops) {
2552 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2553 err = nla_parse_nested(attr, ops->maxtype,
2554 linkinfo[IFLA_INFO_DATA],
2555 ops->policy, NULL);
2556 if (err < 0)
2557 return err;
2558 data = attr;
2560 if (ops->validate) {
2561 err = ops->validate(tb, data, extack);
2562 if (err < 0)
2563 return err;
2567 if (m_ops) {
2568 if (m_ops->slave_maxtype &&
2569 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2570 err = nla_parse_nested(slave_attr,
2571 m_ops->slave_maxtype,
2572 linkinfo[IFLA_INFO_SLAVE_DATA],
2573 m_ops->slave_policy,
2574 NULL);
2575 if (err < 0)
2576 return err;
2577 slave_data = slave_attr;
2579 if (m_ops->slave_validate) {
2580 err = m_ops->slave_validate(tb, slave_data,
2581 extack);
2582 if (err < 0)
2583 return err;
2587 if (dev) {
2588 int status = 0;
2590 if (nlh->nlmsg_flags & NLM_F_EXCL)
2591 return -EEXIST;
2592 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2593 return -EOPNOTSUPP;
2595 if (linkinfo[IFLA_INFO_DATA]) {
2596 if (!ops || ops != dev->rtnl_link_ops ||
2597 !ops->changelink)
2598 return -EOPNOTSUPP;
2600 err = ops->changelink(dev, tb, data, extack);
2601 if (err < 0)
2602 return err;
2603 status |= DO_SETLINK_NOTIFY;
2606 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2607 if (!m_ops || !m_ops->slave_changelink)
2608 return -EOPNOTSUPP;
2610 err = m_ops->slave_changelink(master_dev, dev,
2611 tb, slave_data,
2612 extack);
2613 if (err < 0)
2614 return err;
2615 status |= DO_SETLINK_NOTIFY;
2618 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2619 status);
2622 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2623 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2624 return rtnl_group_changelink(skb, net,
2625 nla_get_u32(tb[IFLA_GROUP]),
2626 ifm, extack, tb);
2627 return -ENODEV;
2630 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
2631 return -EOPNOTSUPP;
2633 if (!ops) {
2634 #ifdef CONFIG_MODULES
2635 if (kind[0]) {
2636 __rtnl_unlock();
2637 request_module("rtnl-link-%s", kind);
2638 rtnl_lock();
2639 ops = rtnl_link_ops_get(kind);
2640 if (ops)
2641 goto replay;
2643 #endif
2644 return -EOPNOTSUPP;
2647 if (!ops->setup)
2648 return -EOPNOTSUPP;
2650 if (!ifname[0]) {
2651 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2652 name_assign_type = NET_NAME_ENUM;
2655 dest_net = rtnl_link_get_net(net, tb);
2656 if (IS_ERR(dest_net))
2657 return PTR_ERR(dest_net);
2659 err = -EPERM;
2660 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2661 goto out;
2663 if (tb[IFLA_LINK_NETNSID]) {
2664 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2666 link_net = get_net_ns_by_id(dest_net, id);
2667 if (!link_net) {
2668 err = -EINVAL;
2669 goto out;
2671 err = -EPERM;
2672 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2673 goto out;
2676 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2677 name_assign_type, ops, tb);
2678 if (IS_ERR(dev)) {
2679 err = PTR_ERR(dev);
2680 goto out;
2683 dev->ifindex = ifm->ifi_index;
2685 if (ops->newlink) {
2686 err = ops->newlink(link_net ? : net, dev, tb, data,
2687 extack);
2688 /* Drivers should call free_netdev() in ->destructor
2689 * and unregister it on failure after registration
2690 * so that device could be finally freed in rtnl_unlock.
2692 if (err < 0) {
2693 /* If device is not registered at all, free it now */
2694 if (dev->reg_state == NETREG_UNINITIALIZED)
2695 free_netdev(dev);
2696 goto out;
2698 } else {
2699 err = register_netdevice(dev);
2700 if (err < 0) {
2701 free_netdev(dev);
2702 goto out;
2705 err = rtnl_configure_link(dev, ifm);
2706 if (err < 0)
2707 goto out_unregister;
2708 if (link_net) {
2709 err = dev_change_net_namespace(dev, dest_net, ifname);
2710 if (err < 0)
2711 goto out_unregister;
2713 if (tb[IFLA_MASTER]) {
2714 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2715 if (err)
2716 goto out_unregister;
2718 out:
2719 if (link_net)
2720 put_net(link_net);
2721 put_net(dest_net);
2722 return err;
2723 out_unregister:
2724 if (ops->newlink) {
2725 LIST_HEAD(list_kill);
2727 ops->dellink(dev, &list_kill);
2728 unregister_netdevice_many(&list_kill);
2729 } else {
2730 unregister_netdevice(dev);
2732 goto out;
2736 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2737 struct netlink_ext_ack *extack)
2739 struct net *net = sock_net(skb->sk);
2740 struct ifinfomsg *ifm;
2741 char ifname[IFNAMSIZ];
2742 struct nlattr *tb[IFLA_MAX+1];
2743 struct net_device *dev = NULL;
2744 struct sk_buff *nskb;
2745 int err;
2746 u32 ext_filter_mask = 0;
2748 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2749 if (err < 0)
2750 return err;
2752 if (tb[IFLA_IFNAME])
2753 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2755 if (tb[IFLA_EXT_MASK])
2756 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2758 ifm = nlmsg_data(nlh);
2759 if (ifm->ifi_index > 0)
2760 dev = __dev_get_by_index(net, ifm->ifi_index);
2761 else if (tb[IFLA_IFNAME])
2762 dev = __dev_get_by_name(net, ifname);
2763 else
2764 return -EINVAL;
2766 if (dev == NULL)
2767 return -ENODEV;
2769 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2770 if (nskb == NULL)
2771 return -ENOBUFS;
2773 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2774 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
2775 if (err < 0) {
2776 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2777 WARN_ON(err == -EMSGSIZE);
2778 kfree_skb(nskb);
2779 } else
2780 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2782 return err;
2785 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2787 struct net *net = sock_net(skb->sk);
2788 struct net_device *dev;
2789 struct nlattr *tb[IFLA_MAX+1];
2790 u32 ext_filter_mask = 0;
2791 u16 min_ifinfo_dump_size = 0;
2792 int hdrlen;
2794 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2795 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2796 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2798 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
2799 if (tb[IFLA_EXT_MASK])
2800 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2803 if (!ext_filter_mask)
2804 return NLMSG_GOODSIZE;
2806 * traverse the list of net devices and compute the minimum
2807 * buffer size based upon the filter mask.
2809 rcu_read_lock();
2810 for_each_netdev_rcu(net, dev) {
2811 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2812 if_nlmsg_size(dev,
2813 ext_filter_mask));
2815 rcu_read_unlock();
2817 return nlmsg_total_size(min_ifinfo_dump_size);
2820 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2822 int idx;
2823 int s_idx = cb->family;
2825 if (s_idx == 0)
2826 s_idx = 1;
2828 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2829 int type = cb->nlh->nlmsg_type-RTM_BASE;
2830 struct rtnl_link *handlers;
2831 rtnl_dumpit_func dumpit;
2833 if (idx < s_idx || idx == PF_PACKET)
2834 continue;
2836 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2837 if (!handlers)
2838 continue;
2840 dumpit = READ_ONCE(handlers[type].dumpit);
2841 if (!dumpit)
2842 continue;
2844 if (idx > s_idx) {
2845 memset(&cb->args[0], 0, sizeof(cb->args));
2846 cb->prev_seq = 0;
2847 cb->seq = 0;
2849 if (dumpit(skb, cb))
2850 break;
2852 cb->family = idx;
2854 return skb->len;
2857 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2858 unsigned int change,
2859 u32 event, gfp_t flags)
2861 struct net *net = dev_net(dev);
2862 struct sk_buff *skb;
2863 int err = -ENOBUFS;
2864 size_t if_info_size;
2866 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2867 if (skb == NULL)
2868 goto errout;
2870 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
2871 if (err < 0) {
2872 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2873 WARN_ON(err == -EMSGSIZE);
2874 kfree_skb(skb);
2875 goto errout;
2877 return skb;
2878 errout:
2879 if (err < 0)
2880 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2881 return NULL;
2884 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2886 struct net *net = dev_net(dev);
2888 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2891 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2892 unsigned int change, u32 event,
2893 gfp_t flags)
2895 struct sk_buff *skb;
2897 if (dev->reg_state != NETREG_REGISTERED)
2898 return;
2900 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
2901 if (skb)
2902 rtmsg_ifinfo_send(skb, dev, flags);
2905 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2906 gfp_t flags)
2908 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
2910 EXPORT_SYMBOL(rtmsg_ifinfo);
2912 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2913 struct net_device *dev,
2914 u8 *addr, u16 vid, u32 pid, u32 seq,
2915 int type, unsigned int flags,
2916 int nlflags, u16 ndm_state)
2918 struct nlmsghdr *nlh;
2919 struct ndmsg *ndm;
2921 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2922 if (!nlh)
2923 return -EMSGSIZE;
2925 ndm = nlmsg_data(nlh);
2926 ndm->ndm_family = AF_BRIDGE;
2927 ndm->ndm_pad1 = 0;
2928 ndm->ndm_pad2 = 0;
2929 ndm->ndm_flags = flags;
2930 ndm->ndm_type = 0;
2931 ndm->ndm_ifindex = dev->ifindex;
2932 ndm->ndm_state = ndm_state;
2934 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2935 goto nla_put_failure;
2936 if (vid)
2937 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2938 goto nla_put_failure;
2940 nlmsg_end(skb, nlh);
2941 return 0;
2943 nla_put_failure:
2944 nlmsg_cancel(skb, nlh);
2945 return -EMSGSIZE;
2948 static inline size_t rtnl_fdb_nlmsg_size(void)
2950 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2951 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2952 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2956 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2957 u16 ndm_state)
2959 struct net *net = dev_net(dev);
2960 struct sk_buff *skb;
2961 int err = -ENOBUFS;
2963 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2964 if (!skb)
2965 goto errout;
2967 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2968 0, 0, type, NTF_SELF, 0, ndm_state);
2969 if (err < 0) {
2970 kfree_skb(skb);
2971 goto errout;
2974 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2975 return;
2976 errout:
2977 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2981 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2983 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2984 struct nlattr *tb[],
2985 struct net_device *dev,
2986 const unsigned char *addr, u16 vid,
2987 u16 flags)
2989 int err = -EINVAL;
2991 /* If aging addresses are supported device will need to
2992 * implement its own handler for this.
2994 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2995 pr_info("%s: FDB only supports static addresses\n", dev->name);
2996 return err;
2999 if (vid) {
3000 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3001 return err;
3004 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3005 err = dev_uc_add_excl(dev, addr);
3006 else if (is_multicast_ether_addr(addr))
3007 err = dev_mc_add_excl(dev, addr);
3009 /* Only return duplicate errors if NLM_F_EXCL is set */
3010 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3011 err = 0;
3013 return err;
3015 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3017 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3019 u16 vid = 0;
3021 if (vlan_attr) {
3022 if (nla_len(vlan_attr) != sizeof(u16)) {
3023 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3024 return -EINVAL;
3027 vid = nla_get_u16(vlan_attr);
3029 if (!vid || vid >= VLAN_VID_MASK) {
3030 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3031 vid);
3032 return -EINVAL;
3035 *p_vid = vid;
3036 return 0;
3039 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3040 struct netlink_ext_ack *extack)
3042 struct net *net = sock_net(skb->sk);
3043 struct ndmsg *ndm;
3044 struct nlattr *tb[NDA_MAX+1];
3045 struct net_device *dev;
3046 u8 *addr;
3047 u16 vid;
3048 int err;
3050 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3051 if (err < 0)
3052 return err;
3054 ndm = nlmsg_data(nlh);
3055 if (ndm->ndm_ifindex == 0) {
3056 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3057 return -EINVAL;
3060 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3061 if (dev == NULL) {
3062 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3063 return -ENODEV;
3066 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3067 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3068 return -EINVAL;
3071 addr = nla_data(tb[NDA_LLADDR]);
3073 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3074 if (err)
3075 return err;
3077 err = -EOPNOTSUPP;
3079 /* Support fdb on master device the net/bridge default case */
3080 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3081 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3082 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3083 const struct net_device_ops *ops = br_dev->netdev_ops;
3085 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3086 nlh->nlmsg_flags);
3087 if (err)
3088 goto out;
3089 else
3090 ndm->ndm_flags &= ~NTF_MASTER;
3093 /* Embedded bridge, macvlan, and any other device support */
3094 if ((ndm->ndm_flags & NTF_SELF)) {
3095 if (dev->netdev_ops->ndo_fdb_add)
3096 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3097 vid,
3098 nlh->nlmsg_flags);
3099 else
3100 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3101 nlh->nlmsg_flags);
3103 if (!err) {
3104 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3105 ndm->ndm_state);
3106 ndm->ndm_flags &= ~NTF_SELF;
3109 out:
3110 return err;
3114 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3116 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3117 struct nlattr *tb[],
3118 struct net_device *dev,
3119 const unsigned char *addr, u16 vid)
3121 int err = -EINVAL;
3123 /* If aging addresses are supported device will need to
3124 * implement its own handler for this.
3126 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3127 pr_info("%s: FDB only supports static addresses\n", dev->name);
3128 return err;
3131 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3132 err = dev_uc_del(dev, addr);
3133 else if (is_multicast_ether_addr(addr))
3134 err = dev_mc_del(dev, addr);
3136 return err;
3138 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3140 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3141 struct netlink_ext_ack *extack)
3143 struct net *net = sock_net(skb->sk);
3144 struct ndmsg *ndm;
3145 struct nlattr *tb[NDA_MAX+1];
3146 struct net_device *dev;
3147 int err = -EINVAL;
3148 __u8 *addr;
3149 u16 vid;
3151 if (!netlink_capable(skb, CAP_NET_ADMIN))
3152 return -EPERM;
3154 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3155 if (err < 0)
3156 return err;
3158 ndm = nlmsg_data(nlh);
3159 if (ndm->ndm_ifindex == 0) {
3160 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3161 return -EINVAL;
3164 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3165 if (dev == NULL) {
3166 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3167 return -ENODEV;
3170 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3171 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3172 return -EINVAL;
3175 addr = nla_data(tb[NDA_LLADDR]);
3177 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3178 if (err)
3179 return err;
3181 err = -EOPNOTSUPP;
3183 /* Support fdb on master device the net/bridge default case */
3184 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3185 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3186 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3187 const struct net_device_ops *ops = br_dev->netdev_ops;
3189 if (ops->ndo_fdb_del)
3190 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3192 if (err)
3193 goto out;
3194 else
3195 ndm->ndm_flags &= ~NTF_MASTER;
3198 /* Embedded bridge, macvlan, and any other device support */
3199 if (ndm->ndm_flags & NTF_SELF) {
3200 if (dev->netdev_ops->ndo_fdb_del)
3201 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3202 vid);
3203 else
3204 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3206 if (!err) {
3207 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3208 ndm->ndm_state);
3209 ndm->ndm_flags &= ~NTF_SELF;
3212 out:
3213 return err;
3216 static int nlmsg_populate_fdb(struct sk_buff *skb,
3217 struct netlink_callback *cb,
3218 struct net_device *dev,
3219 int *idx,
3220 struct netdev_hw_addr_list *list)
3222 struct netdev_hw_addr *ha;
3223 int err;
3224 u32 portid, seq;
3226 portid = NETLINK_CB(cb->skb).portid;
3227 seq = cb->nlh->nlmsg_seq;
3229 list_for_each_entry(ha, &list->list, list) {
3230 if (*idx < cb->args[2])
3231 goto skip;
3233 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3234 portid, seq,
3235 RTM_NEWNEIGH, NTF_SELF,
3236 NLM_F_MULTI, NUD_PERMANENT);
3237 if (err < 0)
3238 return err;
3239 skip:
3240 *idx += 1;
3242 return 0;
3246 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3247 * @nlh: netlink message header
3248 * @dev: netdevice
3250 * Default netdevice operation to dump the existing unicast address list.
3251 * Returns number of addresses from list put in skb.
3253 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3254 struct netlink_callback *cb,
3255 struct net_device *dev,
3256 struct net_device *filter_dev,
3257 int *idx)
3259 int err;
3261 netif_addr_lock_bh(dev);
3262 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3263 if (err)
3264 goto out;
3265 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3266 out:
3267 netif_addr_unlock_bh(dev);
3268 return err;
3270 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3272 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3274 struct net_device *dev;
3275 struct nlattr *tb[IFLA_MAX+1];
3276 struct net_device *br_dev = NULL;
3277 const struct net_device_ops *ops = NULL;
3278 const struct net_device_ops *cops = NULL;
3279 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3280 struct net *net = sock_net(skb->sk);
3281 struct hlist_head *head;
3282 int brport_idx = 0;
3283 int br_idx = 0;
3284 int h, s_h;
3285 int idx = 0, s_idx;
3286 int err = 0;
3287 int fidx = 0;
3289 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3290 IFLA_MAX, ifla_policy, NULL);
3291 if (err < 0) {
3292 return -EINVAL;
3293 } else if (err == 0) {
3294 if (tb[IFLA_MASTER])
3295 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3298 brport_idx = ifm->ifi_index;
3300 if (br_idx) {
3301 br_dev = __dev_get_by_index(net, br_idx);
3302 if (!br_dev)
3303 return -ENODEV;
3305 ops = br_dev->netdev_ops;
3308 s_h = cb->args[0];
3309 s_idx = cb->args[1];
3311 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3312 idx = 0;
3313 head = &net->dev_index_head[h];
3314 hlist_for_each_entry(dev, head, index_hlist) {
3316 if (brport_idx && (dev->ifindex != brport_idx))
3317 continue;
3319 if (!br_idx) { /* user did not specify a specific bridge */
3320 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3321 br_dev = netdev_master_upper_dev_get(dev);
3322 cops = br_dev->netdev_ops;
3324 } else {
3325 if (dev != br_dev &&
3326 !(dev->priv_flags & IFF_BRIDGE_PORT))
3327 continue;
3329 if (br_dev != netdev_master_upper_dev_get(dev) &&
3330 !(dev->priv_flags & IFF_EBRIDGE))
3331 continue;
3332 cops = ops;
3335 if (idx < s_idx)
3336 goto cont;
3338 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3339 if (cops && cops->ndo_fdb_dump) {
3340 err = cops->ndo_fdb_dump(skb, cb,
3341 br_dev, dev,
3342 &fidx);
3343 if (err == -EMSGSIZE)
3344 goto out;
3348 if (dev->netdev_ops->ndo_fdb_dump)
3349 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3350 dev, NULL,
3351 &fidx);
3352 else
3353 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3354 &fidx);
3355 if (err == -EMSGSIZE)
3356 goto out;
3358 cops = NULL;
3360 /* reset fdb offset to 0 for rest of the interfaces */
3361 cb->args[2] = 0;
3362 fidx = 0;
3363 cont:
3364 idx++;
3368 out:
3369 cb->args[0] = h;
3370 cb->args[1] = idx;
3371 cb->args[2] = fidx;
3373 return skb->len;
3376 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3377 unsigned int attrnum, unsigned int flag)
3379 if (mask & flag)
3380 return nla_put_u8(skb, attrnum, !!(flags & flag));
3381 return 0;
3384 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3385 struct net_device *dev, u16 mode,
3386 u32 flags, u32 mask, int nlflags,
3387 u32 filter_mask,
3388 int (*vlan_fill)(struct sk_buff *skb,
3389 struct net_device *dev,
3390 u32 filter_mask))
3392 struct nlmsghdr *nlh;
3393 struct ifinfomsg *ifm;
3394 struct nlattr *br_afspec;
3395 struct nlattr *protinfo;
3396 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3397 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3398 int err = 0;
3400 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3401 if (nlh == NULL)
3402 return -EMSGSIZE;
3404 ifm = nlmsg_data(nlh);
3405 ifm->ifi_family = AF_BRIDGE;
3406 ifm->__ifi_pad = 0;
3407 ifm->ifi_type = dev->type;
3408 ifm->ifi_index = dev->ifindex;
3409 ifm->ifi_flags = dev_get_flags(dev);
3410 ifm->ifi_change = 0;
3413 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3414 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3415 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3416 (br_dev &&
3417 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3418 (dev->addr_len &&
3419 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3420 (dev->ifindex != dev_get_iflink(dev) &&
3421 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3422 goto nla_put_failure;
3424 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3425 if (!br_afspec)
3426 goto nla_put_failure;
3428 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3429 nla_nest_cancel(skb, br_afspec);
3430 goto nla_put_failure;
3433 if (mode != BRIDGE_MODE_UNDEF) {
3434 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3435 nla_nest_cancel(skb, br_afspec);
3436 goto nla_put_failure;
3439 if (vlan_fill) {
3440 err = vlan_fill(skb, dev, filter_mask);
3441 if (err) {
3442 nla_nest_cancel(skb, br_afspec);
3443 goto nla_put_failure;
3446 nla_nest_end(skb, br_afspec);
3448 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3449 if (!protinfo)
3450 goto nla_put_failure;
3452 if (brport_nla_put_flag(skb, flags, mask,
3453 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3454 brport_nla_put_flag(skb, flags, mask,
3455 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3456 brport_nla_put_flag(skb, flags, mask,
3457 IFLA_BRPORT_FAST_LEAVE,
3458 BR_MULTICAST_FAST_LEAVE) ||
3459 brport_nla_put_flag(skb, flags, mask,
3460 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3461 brport_nla_put_flag(skb, flags, mask,
3462 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3463 brport_nla_put_flag(skb, flags, mask,
3464 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3465 brport_nla_put_flag(skb, flags, mask,
3466 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3467 brport_nla_put_flag(skb, flags, mask,
3468 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3469 nla_nest_cancel(skb, protinfo);
3470 goto nla_put_failure;
3473 nla_nest_end(skb, protinfo);
3475 nlmsg_end(skb, nlh);
3476 return 0;
3477 nla_put_failure:
3478 nlmsg_cancel(skb, nlh);
3479 return err ? err : -EMSGSIZE;
3481 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3483 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3485 struct net *net = sock_net(skb->sk);
3486 struct net_device *dev;
3487 int idx = 0;
3488 u32 portid = NETLINK_CB(cb->skb).portid;
3489 u32 seq = cb->nlh->nlmsg_seq;
3490 u32 filter_mask = 0;
3491 int err;
3493 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3494 struct nlattr *extfilt;
3496 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3497 IFLA_EXT_MASK);
3498 if (extfilt) {
3499 if (nla_len(extfilt) < sizeof(filter_mask))
3500 return -EINVAL;
3502 filter_mask = nla_get_u32(extfilt);
3506 rcu_read_lock();
3507 for_each_netdev_rcu(net, dev) {
3508 const struct net_device_ops *ops = dev->netdev_ops;
3509 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3511 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3512 if (idx >= cb->args[0]) {
3513 err = br_dev->netdev_ops->ndo_bridge_getlink(
3514 skb, portid, seq, dev,
3515 filter_mask, NLM_F_MULTI);
3516 if (err < 0 && err != -EOPNOTSUPP) {
3517 if (likely(skb->len))
3518 break;
3520 goto out_err;
3523 idx++;
3526 if (ops->ndo_bridge_getlink) {
3527 if (idx >= cb->args[0]) {
3528 err = ops->ndo_bridge_getlink(skb, portid,
3529 seq, dev,
3530 filter_mask,
3531 NLM_F_MULTI);
3532 if (err < 0 && err != -EOPNOTSUPP) {
3533 if (likely(skb->len))
3534 break;
3536 goto out_err;
3539 idx++;
3542 err = skb->len;
3543 out_err:
3544 rcu_read_unlock();
3545 cb->args[0] = idx;
3547 return err;
3550 static inline size_t bridge_nlmsg_size(void)
3552 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3553 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3554 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3555 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3556 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3557 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3558 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3559 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3560 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3561 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3562 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3565 static int rtnl_bridge_notify(struct net_device *dev)
3567 struct net *net = dev_net(dev);
3568 struct sk_buff *skb;
3569 int err = -EOPNOTSUPP;
3571 if (!dev->netdev_ops->ndo_bridge_getlink)
3572 return 0;
3574 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3575 if (!skb) {
3576 err = -ENOMEM;
3577 goto errout;
3580 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3581 if (err < 0)
3582 goto errout;
3584 if (!skb->len)
3585 goto errout;
3587 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3588 return 0;
3589 errout:
3590 WARN_ON(err == -EMSGSIZE);
3591 kfree_skb(skb);
3592 if (err)
3593 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3594 return err;
3597 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3598 struct netlink_ext_ack *extack)
3600 struct net *net = sock_net(skb->sk);
3601 struct ifinfomsg *ifm;
3602 struct net_device *dev;
3603 struct nlattr *br_spec, *attr = NULL;
3604 int rem, err = -EOPNOTSUPP;
3605 u16 flags = 0;
3606 bool have_flags = false;
3608 if (nlmsg_len(nlh) < sizeof(*ifm))
3609 return -EINVAL;
3611 ifm = nlmsg_data(nlh);
3612 if (ifm->ifi_family != AF_BRIDGE)
3613 return -EPFNOSUPPORT;
3615 dev = __dev_get_by_index(net, ifm->ifi_index);
3616 if (!dev) {
3617 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3618 return -ENODEV;
3621 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3622 if (br_spec) {
3623 nla_for_each_nested(attr, br_spec, rem) {
3624 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3625 if (nla_len(attr) < sizeof(flags))
3626 return -EINVAL;
3628 have_flags = true;
3629 flags = nla_get_u16(attr);
3630 break;
3635 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3636 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3638 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3639 err = -EOPNOTSUPP;
3640 goto out;
3643 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3644 if (err)
3645 goto out;
3647 flags &= ~BRIDGE_FLAGS_MASTER;
3650 if ((flags & BRIDGE_FLAGS_SELF)) {
3651 if (!dev->netdev_ops->ndo_bridge_setlink)
3652 err = -EOPNOTSUPP;
3653 else
3654 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3655 flags);
3656 if (!err) {
3657 flags &= ~BRIDGE_FLAGS_SELF;
3659 /* Generate event to notify upper layer of bridge
3660 * change
3662 err = rtnl_bridge_notify(dev);
3666 if (have_flags)
3667 memcpy(nla_data(attr), &flags, sizeof(flags));
3668 out:
3669 return err;
3672 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3673 struct netlink_ext_ack *extack)
3675 struct net *net = sock_net(skb->sk);
3676 struct ifinfomsg *ifm;
3677 struct net_device *dev;
3678 struct nlattr *br_spec, *attr = NULL;
3679 int rem, err = -EOPNOTSUPP;
3680 u16 flags = 0;
3681 bool have_flags = false;
3683 if (nlmsg_len(nlh) < sizeof(*ifm))
3684 return -EINVAL;
3686 ifm = nlmsg_data(nlh);
3687 if (ifm->ifi_family != AF_BRIDGE)
3688 return -EPFNOSUPPORT;
3690 dev = __dev_get_by_index(net, ifm->ifi_index);
3691 if (!dev) {
3692 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3693 return -ENODEV;
3696 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3697 if (br_spec) {
3698 nla_for_each_nested(attr, br_spec, rem) {
3699 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3700 if (nla_len(attr) < sizeof(flags))
3701 return -EINVAL;
3703 have_flags = true;
3704 flags = nla_get_u16(attr);
3705 break;
3710 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3711 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3713 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3714 err = -EOPNOTSUPP;
3715 goto out;
3718 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3719 if (err)
3720 goto out;
3722 flags &= ~BRIDGE_FLAGS_MASTER;
3725 if ((flags & BRIDGE_FLAGS_SELF)) {
3726 if (!dev->netdev_ops->ndo_bridge_dellink)
3727 err = -EOPNOTSUPP;
3728 else
3729 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3730 flags);
3732 if (!err) {
3733 flags &= ~BRIDGE_FLAGS_SELF;
3735 /* Generate event to notify upper layer of bridge
3736 * change
3738 err = rtnl_bridge_notify(dev);
3742 if (have_flags)
3743 memcpy(nla_data(attr), &flags, sizeof(flags));
3744 out:
3745 return err;
3748 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3750 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3751 (!idxattr || idxattr == attrid);
3754 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3755 static int rtnl_get_offload_stats_attr_size(int attr_id)
3757 switch (attr_id) {
3758 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3759 return sizeof(struct rtnl_link_stats64);
3762 return 0;
3765 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3766 int *prividx)
3768 struct nlattr *attr = NULL;
3769 int attr_id, size;
3770 void *attr_data;
3771 int err;
3773 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3774 dev->netdev_ops->ndo_get_offload_stats))
3775 return -ENODATA;
3777 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3778 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3779 if (attr_id < *prividx)
3780 continue;
3782 size = rtnl_get_offload_stats_attr_size(attr_id);
3783 if (!size)
3784 continue;
3786 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3787 continue;
3789 attr = nla_reserve_64bit(skb, attr_id, size,
3790 IFLA_OFFLOAD_XSTATS_UNSPEC);
3791 if (!attr)
3792 goto nla_put_failure;
3794 attr_data = nla_data(attr);
3795 memset(attr_data, 0, size);
3796 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3797 attr_data);
3798 if (err)
3799 goto get_offload_stats_failure;
3802 if (!attr)
3803 return -ENODATA;
3805 *prividx = 0;
3806 return 0;
3808 nla_put_failure:
3809 err = -EMSGSIZE;
3810 get_offload_stats_failure:
3811 *prividx = attr_id;
3812 return err;
3815 static int rtnl_get_offload_stats_size(const struct net_device *dev)
3817 int nla_size = 0;
3818 int attr_id;
3819 int size;
3821 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3822 dev->netdev_ops->ndo_get_offload_stats))
3823 return 0;
3825 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3826 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3827 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3828 continue;
3829 size = rtnl_get_offload_stats_attr_size(attr_id);
3830 nla_size += nla_total_size_64bit(size);
3833 if (nla_size != 0)
3834 nla_size += nla_total_size(0);
3836 return nla_size;
3839 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3840 int type, u32 pid, u32 seq, u32 change,
3841 unsigned int flags, unsigned int filter_mask,
3842 int *idxattr, int *prividx)
3844 struct if_stats_msg *ifsm;
3845 struct nlmsghdr *nlh;
3846 struct nlattr *attr;
3847 int s_prividx = *prividx;
3848 int err;
3850 ASSERT_RTNL();
3852 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3853 if (!nlh)
3854 return -EMSGSIZE;
3856 ifsm = nlmsg_data(nlh);
3857 ifsm->family = PF_UNSPEC;
3858 ifsm->pad1 = 0;
3859 ifsm->pad2 = 0;
3860 ifsm->ifindex = dev->ifindex;
3861 ifsm->filter_mask = filter_mask;
3863 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3864 struct rtnl_link_stats64 *sp;
3866 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3867 sizeof(struct rtnl_link_stats64),
3868 IFLA_STATS_UNSPEC);
3869 if (!attr)
3870 goto nla_put_failure;
3872 sp = nla_data(attr);
3873 dev_get_stats(dev, sp);
3876 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3877 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3879 if (ops && ops->fill_linkxstats) {
3880 *idxattr = IFLA_STATS_LINK_XSTATS;
3881 attr = nla_nest_start(skb,
3882 IFLA_STATS_LINK_XSTATS);
3883 if (!attr)
3884 goto nla_put_failure;
3886 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3887 nla_nest_end(skb, attr);
3888 if (err)
3889 goto nla_put_failure;
3890 *idxattr = 0;
3894 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3895 *idxattr)) {
3896 const struct rtnl_link_ops *ops = NULL;
3897 const struct net_device *master;
3899 master = netdev_master_upper_dev_get(dev);
3900 if (master)
3901 ops = master->rtnl_link_ops;
3902 if (ops && ops->fill_linkxstats) {
3903 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3904 attr = nla_nest_start(skb,
3905 IFLA_STATS_LINK_XSTATS_SLAVE);
3906 if (!attr)
3907 goto nla_put_failure;
3909 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3910 nla_nest_end(skb, attr);
3911 if (err)
3912 goto nla_put_failure;
3913 *idxattr = 0;
3917 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3918 *idxattr)) {
3919 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3920 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3921 if (!attr)
3922 goto nla_put_failure;
3924 err = rtnl_get_offload_stats(skb, dev, prividx);
3925 if (err == -ENODATA)
3926 nla_nest_cancel(skb, attr);
3927 else
3928 nla_nest_end(skb, attr);
3930 if (err && err != -ENODATA)
3931 goto nla_put_failure;
3932 *idxattr = 0;
3935 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3936 struct rtnl_af_ops *af_ops;
3938 *idxattr = IFLA_STATS_AF_SPEC;
3939 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3940 if (!attr)
3941 goto nla_put_failure;
3943 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3944 if (af_ops->fill_stats_af) {
3945 struct nlattr *af;
3946 int err;
3948 af = nla_nest_start(skb, af_ops->family);
3949 if (!af)
3950 goto nla_put_failure;
3952 err = af_ops->fill_stats_af(skb, dev);
3954 if (err == -ENODATA)
3955 nla_nest_cancel(skb, af);
3956 else if (err < 0)
3957 goto nla_put_failure;
3959 nla_nest_end(skb, af);
3963 nla_nest_end(skb, attr);
3965 *idxattr = 0;
3968 nlmsg_end(skb, nlh);
3970 return 0;
3972 nla_put_failure:
3973 /* not a multi message or no progress mean a real error */
3974 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3975 nlmsg_cancel(skb, nlh);
3976 else
3977 nlmsg_end(skb, nlh);
3979 return -EMSGSIZE;
3982 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3983 u32 filter_mask)
3985 size_t size = 0;
3987 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3988 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3990 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3991 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3992 int attr = IFLA_STATS_LINK_XSTATS;
3994 if (ops && ops->get_linkxstats_size) {
3995 size += nla_total_size(ops->get_linkxstats_size(dev,
3996 attr));
3997 /* for IFLA_STATS_LINK_XSTATS */
3998 size += nla_total_size(0);
4002 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4003 struct net_device *_dev = (struct net_device *)dev;
4004 const struct rtnl_link_ops *ops = NULL;
4005 const struct net_device *master;
4007 /* netdev_master_upper_dev_get can't take const */
4008 master = netdev_master_upper_dev_get(_dev);
4009 if (master)
4010 ops = master->rtnl_link_ops;
4011 if (ops && ops->get_linkxstats_size) {
4012 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4014 size += nla_total_size(ops->get_linkxstats_size(dev,
4015 attr));
4016 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4017 size += nla_total_size(0);
4021 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4022 size += rtnl_get_offload_stats_size(dev);
4024 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4025 struct rtnl_af_ops *af_ops;
4027 /* for IFLA_STATS_AF_SPEC */
4028 size += nla_total_size(0);
4030 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4031 if (af_ops->get_stats_af_size) {
4032 size += nla_total_size(
4033 af_ops->get_stats_af_size(dev));
4035 /* for AF_* */
4036 size += nla_total_size(0);
4041 return size;
4044 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4045 struct netlink_ext_ack *extack)
4047 struct net *net = sock_net(skb->sk);
4048 struct net_device *dev = NULL;
4049 int idxattr = 0, prividx = 0;
4050 struct if_stats_msg *ifsm;
4051 struct sk_buff *nskb;
4052 u32 filter_mask;
4053 int err;
4055 if (nlmsg_len(nlh) < sizeof(*ifsm))
4056 return -EINVAL;
4058 ifsm = nlmsg_data(nlh);
4059 if (ifsm->ifindex > 0)
4060 dev = __dev_get_by_index(net, ifsm->ifindex);
4061 else
4062 return -EINVAL;
4064 if (!dev)
4065 return -ENODEV;
4067 filter_mask = ifsm->filter_mask;
4068 if (!filter_mask)
4069 return -EINVAL;
4071 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4072 if (!nskb)
4073 return -ENOBUFS;
4075 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4076 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4077 0, filter_mask, &idxattr, &prividx);
4078 if (err < 0) {
4079 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4080 WARN_ON(err == -EMSGSIZE);
4081 kfree_skb(nskb);
4082 } else {
4083 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4086 return err;
4089 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4091 int h, s_h, err, s_idx, s_idxattr, s_prividx;
4092 struct net *net = sock_net(skb->sk);
4093 unsigned int flags = NLM_F_MULTI;
4094 struct if_stats_msg *ifsm;
4095 struct hlist_head *head;
4096 struct net_device *dev;
4097 u32 filter_mask = 0;
4098 int idx = 0;
4100 s_h = cb->args[0];
4101 s_idx = cb->args[1];
4102 s_idxattr = cb->args[2];
4103 s_prividx = cb->args[3];
4105 cb->seq = net->dev_base_seq;
4107 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4108 return -EINVAL;
4110 ifsm = nlmsg_data(cb->nlh);
4111 filter_mask = ifsm->filter_mask;
4112 if (!filter_mask)
4113 return -EINVAL;
4115 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4116 idx = 0;
4117 head = &net->dev_index_head[h];
4118 hlist_for_each_entry(dev, head, index_hlist) {
4119 if (idx < s_idx)
4120 goto cont;
4121 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4122 NETLINK_CB(cb->skb).portid,
4123 cb->nlh->nlmsg_seq, 0,
4124 flags, filter_mask,
4125 &s_idxattr, &s_prividx);
4126 /* If we ran out of room on the first message,
4127 * we're in trouble
4129 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4131 if (err < 0)
4132 goto out;
4133 s_prividx = 0;
4134 s_idxattr = 0;
4135 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4136 cont:
4137 idx++;
4140 out:
4141 cb->args[3] = s_prividx;
4142 cb->args[2] = s_idxattr;
4143 cb->args[1] = idx;
4144 cb->args[0] = h;
4146 return skb->len;
4149 /* Process one rtnetlink message. */
4151 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4152 struct netlink_ext_ack *extack)
4154 struct net *net = sock_net(skb->sk);
4155 struct rtnl_link *handlers;
4156 int err = -EOPNOTSUPP;
4157 rtnl_doit_func doit;
4158 unsigned int flags;
4159 int kind;
4160 int family;
4161 int type;
4163 type = nlh->nlmsg_type;
4164 if (type > RTM_MAX)
4165 return -EOPNOTSUPP;
4167 type -= RTM_BASE;
4169 /* All the messages must have at least 1 byte length */
4170 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4171 return 0;
4173 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4174 kind = type&3;
4176 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4177 return -EPERM;
4179 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
4180 family = PF_UNSPEC;
4182 rcu_read_lock();
4183 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4184 if (!handlers) {
4185 family = PF_UNSPEC;
4186 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4189 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4190 struct sock *rtnl;
4191 rtnl_dumpit_func dumpit;
4192 u16 min_dump_alloc = 0;
4194 dumpit = READ_ONCE(handlers[type].dumpit);
4195 if (!dumpit) {
4196 family = PF_UNSPEC;
4197 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4198 if (!handlers)
4199 goto err_unlock;
4201 dumpit = READ_ONCE(handlers[type].dumpit);
4202 if (!dumpit)
4203 goto err_unlock;
4206 refcount_inc(&rtnl_msg_handlers_ref[family]);
4208 if (type == RTM_GETLINK - RTM_BASE)
4209 min_dump_alloc = rtnl_calcit(skb, nlh);
4211 rcu_read_unlock();
4213 rtnl = net->rtnl;
4215 struct netlink_dump_control c = {
4216 .dump = dumpit,
4217 .min_dump_alloc = min_dump_alloc,
4219 err = netlink_dump_start(rtnl, skb, nlh, &c);
4221 refcount_dec(&rtnl_msg_handlers_ref[family]);
4222 return err;
4225 doit = READ_ONCE(handlers[type].doit);
4226 if (!doit) {
4227 family = PF_UNSPEC;
4228 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4231 flags = READ_ONCE(handlers[type].flags);
4232 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4233 refcount_inc(&rtnl_msg_handlers_ref[family]);
4234 doit = READ_ONCE(handlers[type].doit);
4235 rcu_read_unlock();
4236 if (doit)
4237 err = doit(skb, nlh, extack);
4238 refcount_dec(&rtnl_msg_handlers_ref[family]);
4239 return err;
4242 rcu_read_unlock();
4244 rtnl_lock();
4245 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4246 if (handlers) {
4247 doit = READ_ONCE(handlers[type].doit);
4248 if (doit)
4249 err = doit(skb, nlh, extack);
4251 rtnl_unlock();
4252 return err;
4254 err_unlock:
4255 rcu_read_unlock();
4256 return -EOPNOTSUPP;
4259 static void rtnetlink_rcv(struct sk_buff *skb)
4261 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4264 static int rtnetlink_bind(struct net *net, int group)
4266 switch (group) {
4267 case RTNLGRP_IPV4_MROUTE_R:
4268 case RTNLGRP_IPV6_MROUTE_R:
4269 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4270 return -EPERM;
4271 break;
4273 return 0;
4276 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4278 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4280 switch (event) {
4281 case NETDEV_REBOOT:
4282 case NETDEV_CHANGEMTU:
4283 case NETDEV_CHANGEADDR:
4284 case NETDEV_CHANGENAME:
4285 case NETDEV_FEAT_CHANGE:
4286 case NETDEV_BONDING_FAILOVER:
4287 case NETDEV_POST_TYPE_CHANGE:
4288 case NETDEV_NOTIFY_PEERS:
4289 case NETDEV_CHANGEUPPER:
4290 case NETDEV_RESEND_IGMP:
4291 case NETDEV_CHANGEINFODATA:
4292 case NETDEV_CHANGE_TX_QUEUE_LEN:
4293 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4294 GFP_KERNEL);
4295 break;
4296 default:
4297 break;
4299 return NOTIFY_DONE;
4302 static struct notifier_block rtnetlink_dev_notifier = {
4303 .notifier_call = rtnetlink_event,
4307 static int __net_init rtnetlink_net_init(struct net *net)
4309 struct sock *sk;
4310 struct netlink_kernel_cfg cfg = {
4311 .groups = RTNLGRP_MAX,
4312 .input = rtnetlink_rcv,
4313 .cb_mutex = &rtnl_mutex,
4314 .flags = NL_CFG_F_NONROOT_RECV,
4315 .bind = rtnetlink_bind,
4318 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4319 if (!sk)
4320 return -ENOMEM;
4321 net->rtnl = sk;
4322 return 0;
4325 static void __net_exit rtnetlink_net_exit(struct net *net)
4327 netlink_kernel_release(net->rtnl);
4328 net->rtnl = NULL;
4331 static struct pernet_operations rtnetlink_net_ops = {
4332 .init = rtnetlink_net_init,
4333 .exit = rtnetlink_net_exit,
4336 void __init rtnetlink_init(void)
4338 int i;
4340 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4341 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4343 if (register_pernet_subsys(&rtnetlink_net_ops))
4344 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4346 register_netdevice_notifier(&rtnetlink_dev_notifier);
4348 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4349 rtnl_dump_ifinfo, 0);
4350 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4351 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4352 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4354 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4355 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4356 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
4358 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4359 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4360 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4362 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4363 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4364 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
4366 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,