2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
42 #include <linux/bpf.h>
44 #include <linux/uaccess.h>
46 #include <linux/inet.h>
47 #include <linux/netdevice.h>
48 #include <net/switchdev.h>
50 #include <net/protocol.h>
52 #include <net/route.h>
56 #include <net/pkt_sched.h>
57 #include <net/fib_rules.h>
58 #include <net/rtnetlink.h>
59 #include <net/net_namespace.h>
63 rtnl_dumpit_func dumpit;
64 rtnl_calcit_func calcit;
67 static DEFINE_MUTEX(rtnl_mutex);
71 mutex_lock(&rtnl_mutex);
73 EXPORT_SYMBOL(rtnl_lock);
75 static struct sk_buff *defer_kfree_skb_list;
76 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
79 tail->next = defer_kfree_skb_list;
80 defer_kfree_skb_list = head;
83 EXPORT_SYMBOL(rtnl_kfree_skbs);
85 void __rtnl_unlock(void)
87 struct sk_buff *head = defer_kfree_skb_list;
89 defer_kfree_skb_list = NULL;
91 mutex_unlock(&rtnl_mutex);
94 struct sk_buff *next = head->next;
102 void rtnl_unlock(void)
104 /* This fellow will unlock it for us. */
107 EXPORT_SYMBOL(rtnl_unlock);
109 int rtnl_trylock(void)
111 return mutex_trylock(&rtnl_mutex);
113 EXPORT_SYMBOL(rtnl_trylock);
115 int rtnl_is_locked(void)
117 return mutex_is_locked(&rtnl_mutex);
119 EXPORT_SYMBOL(rtnl_is_locked);
121 #ifdef CONFIG_PROVE_LOCKING
122 bool lockdep_rtnl_is_held(void)
124 return lockdep_is_held(&rtnl_mutex);
126 EXPORT_SYMBOL(lockdep_rtnl_is_held);
127 #endif /* #ifdef CONFIG_PROVE_LOCKING */
129 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
131 static inline int rtm_msgindex(int msgtype)
133 int msgindex = msgtype - RTM_BASE;
136 * msgindex < 0 implies someone tried to register a netlink
137 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
138 * the message type has not been added to linux/rtnetlink.h
140 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
145 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
147 struct rtnl_link *tab;
149 if (protocol <= RTNL_FAMILY_MAX)
150 tab = rtnl_msg_handlers[protocol];
154 if (tab == NULL || tab[msgindex].doit == NULL)
155 tab = rtnl_msg_handlers[PF_UNSPEC];
157 return tab[msgindex].doit;
160 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
162 struct rtnl_link *tab;
164 if (protocol <= RTNL_FAMILY_MAX)
165 tab = rtnl_msg_handlers[protocol];
169 if (tab == NULL || tab[msgindex].dumpit == NULL)
170 tab = rtnl_msg_handlers[PF_UNSPEC];
172 return tab[msgindex].dumpit;
175 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
177 struct rtnl_link *tab;
179 if (protocol <= RTNL_FAMILY_MAX)
180 tab = rtnl_msg_handlers[protocol];
184 if (tab == NULL || tab[msgindex].calcit == NULL)
185 tab = rtnl_msg_handlers[PF_UNSPEC];
187 return tab[msgindex].calcit;
191 * __rtnl_register - Register a rtnetlink message type
192 * @protocol: Protocol family or PF_UNSPEC
193 * @msgtype: rtnetlink message type
194 * @doit: Function pointer called for each request message
195 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
196 * @calcit: Function pointer to calc size of dump message
198 * Registers the specified function pointers (at least one of them has
199 * to be non-NULL) to be called whenever a request message for the
200 * specified protocol family and message type is received.
202 * The special protocol family PF_UNSPEC may be used to define fallback
203 * function pointers for the case when no entry for the specific protocol
206 * Returns 0 on success or a negative error code.
208 int __rtnl_register(int protocol, int msgtype,
209 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
210 rtnl_calcit_func calcit)
212 struct rtnl_link *tab;
215 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
216 msgindex = rtm_msgindex(msgtype);
218 tab = rtnl_msg_handlers[protocol];
220 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
224 rtnl_msg_handlers[protocol] = tab;
228 tab[msgindex].doit = doit;
231 tab[msgindex].dumpit = dumpit;
234 tab[msgindex].calcit = calcit;
238 EXPORT_SYMBOL_GPL(__rtnl_register);
241 * rtnl_register - Register a rtnetlink message type
243 * Identical to __rtnl_register() but panics on failure. This is useful
244 * as failure of this function is very unlikely, it can only happen due
245 * to lack of memory when allocating the chain to store all message
246 * handlers for a protocol. Meant for use in init functions where lack
247 * of memory implies no sense in continuing.
249 void rtnl_register(int protocol, int msgtype,
250 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
251 rtnl_calcit_func calcit)
253 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
254 panic("Unable to register rtnetlink message handler, "
255 "protocol = %d, message type = %d\n",
258 EXPORT_SYMBOL_GPL(rtnl_register);
261 * rtnl_unregister - Unregister a rtnetlink message type
262 * @protocol: Protocol family or PF_UNSPEC
263 * @msgtype: rtnetlink message type
265 * Returns 0 on success or a negative error code.
267 int rtnl_unregister(int protocol, int msgtype)
271 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
272 msgindex = rtm_msgindex(msgtype);
274 if (rtnl_msg_handlers[protocol] == NULL)
277 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
278 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
279 rtnl_msg_handlers[protocol][msgindex].calcit = NULL;
283 EXPORT_SYMBOL_GPL(rtnl_unregister);
286 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
287 * @protocol : Protocol family or PF_UNSPEC
289 * Identical to calling rtnl_unregster() for all registered message types
290 * of a certain protocol family.
292 void rtnl_unregister_all(int protocol)
294 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
296 kfree(rtnl_msg_handlers[protocol]);
297 rtnl_msg_handlers[protocol] = NULL;
299 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
301 static LIST_HEAD(link_ops);
303 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
305 const struct rtnl_link_ops *ops;
307 list_for_each_entry(ops, &link_ops, list) {
308 if (!strcmp(ops->kind, kind))
315 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
316 * @ops: struct rtnl_link_ops * to register
318 * The caller must hold the rtnl_mutex. This function should be used
319 * by drivers that create devices during module initialization. It
320 * must be called before registering the devices.
322 * Returns 0 on success or a negative error code.
324 int __rtnl_link_register(struct rtnl_link_ops *ops)
326 if (rtnl_link_ops_get(ops->kind))
329 /* The check for setup is here because if ops
330 * does not have that filled up, it is not possible
331 * to use the ops for creating device. So do not
332 * fill up dellink as well. That disables rtnl_dellink.
334 if (ops->setup && !ops->dellink)
335 ops->dellink = unregister_netdevice_queue;
337 list_add_tail(&ops->list, &link_ops);
340 EXPORT_SYMBOL_GPL(__rtnl_link_register);
343 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
344 * @ops: struct rtnl_link_ops * to register
346 * Returns 0 on success or a negative error code.
348 int rtnl_link_register(struct rtnl_link_ops *ops)
353 err = __rtnl_link_register(ops);
357 EXPORT_SYMBOL_GPL(rtnl_link_register);
359 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
361 struct net_device *dev;
362 LIST_HEAD(list_kill);
364 for_each_netdev(net, dev) {
365 if (dev->rtnl_link_ops == ops)
366 ops->dellink(dev, &list_kill);
368 unregister_netdevice_many(&list_kill);
372 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
373 * @ops: struct rtnl_link_ops * to unregister
375 * The caller must hold the rtnl_mutex.
377 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
382 __rtnl_kill_links(net, ops);
384 list_del(&ops->list);
386 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
388 /* Return with the rtnl_lock held when there are no network
389 * devices unregistering in any network namespace.
391 static void rtnl_lock_unregistering_all(void)
395 DEFINE_WAIT_FUNC(wait, woken_wake_function);
397 add_wait_queue(&netdev_unregistering_wq, &wait);
399 unregistering = false;
402 if (net->dev_unreg_count > 0) {
403 unregistering = true;
411 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
413 remove_wait_queue(&netdev_unregistering_wq, &wait);
417 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
418 * @ops: struct rtnl_link_ops * to unregister
420 void rtnl_link_unregister(struct rtnl_link_ops *ops)
422 /* Close the race with cleanup_net() */
423 mutex_lock(&net_mutex);
424 rtnl_lock_unregistering_all();
425 __rtnl_link_unregister(ops);
427 mutex_unlock(&net_mutex);
429 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
431 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
433 struct net_device *master_dev;
434 const struct rtnl_link_ops *ops;
436 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
439 ops = master_dev->rtnl_link_ops;
440 if (!ops || !ops->get_slave_size)
442 /* IFLA_INFO_SLAVE_DATA + nested data */
443 return nla_total_size(sizeof(struct nlattr)) +
444 ops->get_slave_size(master_dev, dev);
447 static size_t rtnl_link_get_size(const struct net_device *dev)
449 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
455 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
456 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
459 /* IFLA_INFO_DATA + nested data */
460 size += nla_total_size(sizeof(struct nlattr)) +
463 if (ops->get_xstats_size)
464 /* IFLA_INFO_XSTATS */
465 size += nla_total_size(ops->get_xstats_size(dev));
467 size += rtnl_link_get_slave_info_data_size(dev);
472 static LIST_HEAD(rtnl_af_ops);
474 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
476 const struct rtnl_af_ops *ops;
478 list_for_each_entry(ops, &rtnl_af_ops, list) {
479 if (ops->family == family)
487 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
488 * @ops: struct rtnl_af_ops * to register
490 * Returns 0 on success or a negative error code.
492 void rtnl_af_register(struct rtnl_af_ops *ops)
495 list_add_tail(&ops->list, &rtnl_af_ops);
498 EXPORT_SYMBOL_GPL(rtnl_af_register);
501 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
502 * @ops: struct rtnl_af_ops * to unregister
504 * The caller must hold the rtnl_mutex.
506 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
508 list_del(&ops->list);
510 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
513 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
514 * @ops: struct rtnl_af_ops * to unregister
516 void rtnl_af_unregister(struct rtnl_af_ops *ops)
519 __rtnl_af_unregister(ops);
522 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
524 static size_t rtnl_link_get_af_size(const struct net_device *dev,
527 struct rtnl_af_ops *af_ops;
531 size = nla_total_size(sizeof(struct nlattr));
533 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
534 if (af_ops->get_link_af_size) {
535 /* AF_* + nested data */
536 size += nla_total_size(sizeof(struct nlattr)) +
537 af_ops->get_link_af_size(dev, ext_filter_mask);
544 static bool rtnl_have_link_slave_info(const struct net_device *dev)
546 struct net_device *master_dev;
548 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
549 if (master_dev && master_dev->rtnl_link_ops)
554 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
555 const struct net_device *dev)
557 struct net_device *master_dev;
558 const struct rtnl_link_ops *ops;
559 struct nlattr *slave_data;
562 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
565 ops = master_dev->rtnl_link_ops;
568 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
570 if (ops->fill_slave_info) {
571 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
574 err = ops->fill_slave_info(skb, master_dev, dev);
576 goto err_cancel_slave_data;
577 nla_nest_end(skb, slave_data);
581 err_cancel_slave_data:
582 nla_nest_cancel(skb, slave_data);
586 static int rtnl_link_info_fill(struct sk_buff *skb,
587 const struct net_device *dev)
589 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
595 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
597 if (ops->fill_xstats) {
598 err = ops->fill_xstats(skb, dev);
602 if (ops->fill_info) {
603 data = nla_nest_start(skb, IFLA_INFO_DATA);
606 err = ops->fill_info(skb, dev);
608 goto err_cancel_data;
609 nla_nest_end(skb, data);
614 nla_nest_cancel(skb, data);
618 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
620 struct nlattr *linkinfo;
623 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
624 if (linkinfo == NULL)
627 err = rtnl_link_info_fill(skb, dev);
629 goto err_cancel_link;
631 err = rtnl_link_slave_info_fill(skb, dev);
633 goto err_cancel_link;
635 nla_nest_end(skb, linkinfo);
639 nla_nest_cancel(skb, linkinfo);
644 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
646 struct sock *rtnl = net->rtnl;
649 NETLINK_CB(skb).dst_group = group;
651 atomic_inc(&skb->users);
652 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
654 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
658 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
660 struct sock *rtnl = net->rtnl;
662 return nlmsg_unicast(rtnl, skb, pid);
664 EXPORT_SYMBOL(rtnl_unicast);
666 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
667 struct nlmsghdr *nlh, gfp_t flags)
669 struct sock *rtnl = net->rtnl;
673 report = nlmsg_report(nlh);
675 nlmsg_notify(rtnl, skb, pid, group, report, flags);
677 EXPORT_SYMBOL(rtnl_notify);
679 void rtnl_set_sk_err(struct net *net, u32 group, int error)
681 struct sock *rtnl = net->rtnl;
683 netlink_set_err(rtnl, 0, group, error);
685 EXPORT_SYMBOL(rtnl_set_sk_err);
687 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
692 mx = nla_nest_start(skb, RTA_METRICS);
696 for (i = 0; i < RTAX_MAX; i++) {
698 if (i == RTAX_CC_ALGO - 1) {
699 char tmp[TCP_CA_NAME_MAX], *name;
701 name = tcp_ca_get_name_by_key(metrics[i], tmp);
704 if (nla_put_string(skb, i + 1, name))
705 goto nla_put_failure;
706 } else if (i == RTAX_FEATURES - 1) {
707 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
711 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
712 if (nla_put_u32(skb, i + 1, user_features))
713 goto nla_put_failure;
715 if (nla_put_u32(skb, i + 1, metrics[i]))
716 goto nla_put_failure;
723 nla_nest_cancel(skb, mx);
727 return nla_nest_end(skb, mx);
730 nla_nest_cancel(skb, mx);
733 EXPORT_SYMBOL(rtnetlink_put_metrics);
735 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
736 long expires, u32 error)
738 struct rta_cacheinfo ci = {
739 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
740 .rta_used = dst->__use,
741 .rta_clntref = atomic_read(&(dst->__refcnt)),
749 clock = jiffies_to_clock_t(abs(expires));
750 clock = min_t(unsigned long, clock, INT_MAX);
751 ci.rta_expires = (expires > 0) ? clock : -clock;
753 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
755 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
757 static void set_operstate(struct net_device *dev, unsigned char transition)
759 unsigned char operstate = dev->operstate;
761 switch (transition) {
763 if ((operstate == IF_OPER_DORMANT ||
764 operstate == IF_OPER_UNKNOWN) &&
766 operstate = IF_OPER_UP;
769 case IF_OPER_DORMANT:
770 if (operstate == IF_OPER_UP ||
771 operstate == IF_OPER_UNKNOWN)
772 operstate = IF_OPER_DORMANT;
776 if (dev->operstate != operstate) {
777 write_lock_bh(&dev_base_lock);
778 dev->operstate = operstate;
779 write_unlock_bh(&dev_base_lock);
780 netdev_state_change(dev);
784 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
786 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
787 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
790 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
791 const struct ifinfomsg *ifm)
793 unsigned int flags = ifm->ifi_flags;
795 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
797 flags = (flags & ifm->ifi_change) |
798 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
803 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
804 const struct rtnl_link_stats64 *b)
806 a->rx_packets = b->rx_packets;
807 a->tx_packets = b->tx_packets;
808 a->rx_bytes = b->rx_bytes;
809 a->tx_bytes = b->tx_bytes;
810 a->rx_errors = b->rx_errors;
811 a->tx_errors = b->tx_errors;
812 a->rx_dropped = b->rx_dropped;
813 a->tx_dropped = b->tx_dropped;
815 a->multicast = b->multicast;
816 a->collisions = b->collisions;
818 a->rx_length_errors = b->rx_length_errors;
819 a->rx_over_errors = b->rx_over_errors;
820 a->rx_crc_errors = b->rx_crc_errors;
821 a->rx_frame_errors = b->rx_frame_errors;
822 a->rx_fifo_errors = b->rx_fifo_errors;
823 a->rx_missed_errors = b->rx_missed_errors;
825 a->tx_aborted_errors = b->tx_aborted_errors;
826 a->tx_carrier_errors = b->tx_carrier_errors;
827 a->tx_fifo_errors = b->tx_fifo_errors;
828 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
829 a->tx_window_errors = b->tx_window_errors;
831 a->rx_compressed = b->rx_compressed;
832 a->tx_compressed = b->tx_compressed;
834 a->rx_nohandler = b->rx_nohandler;
838 static inline int rtnl_vfinfo_size(const struct net_device *dev,
841 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
842 int num_vfs = dev_num_vf(dev->dev.parent);
843 size_t size = nla_total_size(0);
846 nla_total_size(sizeof(struct ifla_vf_mac)) +
847 nla_total_size(sizeof(struct ifla_vf_vlan)) +
848 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
849 nla_total_size(MAX_VLAN_LIST_LEN *
850 sizeof(struct ifla_vf_vlan_info)) +
851 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
852 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
853 nla_total_size(sizeof(struct ifla_vf_rate)) +
854 nla_total_size(sizeof(struct ifla_vf_link_state)) +
855 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
856 nla_total_size(0) + /* nest IFLA_VF_STATS */
857 /* IFLA_VF_STATS_RX_PACKETS */
858 nla_total_size_64bit(sizeof(__u64)) +
859 /* IFLA_VF_STATS_TX_PACKETS */
860 nla_total_size_64bit(sizeof(__u64)) +
861 /* IFLA_VF_STATS_RX_BYTES */
862 nla_total_size_64bit(sizeof(__u64)) +
863 /* IFLA_VF_STATS_TX_BYTES */
864 nla_total_size_64bit(sizeof(__u64)) +
865 /* IFLA_VF_STATS_BROADCAST */
866 nla_total_size_64bit(sizeof(__u64)) +
867 /* IFLA_VF_STATS_MULTICAST */
868 nla_total_size_64bit(sizeof(__u64)) +
869 nla_total_size(sizeof(struct ifla_vf_trust)));
875 static size_t rtnl_port_size(const struct net_device *dev,
878 size_t port_size = nla_total_size(4) /* PORT_VF */
879 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
880 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
881 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
882 + nla_total_size(1) /* PROT_VDP_REQUEST */
883 + nla_total_size(2); /* PORT_VDP_RESPONSE */
884 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
885 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
887 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
890 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
891 !(ext_filter_mask & RTEXT_FILTER_VF))
893 if (dev_num_vf(dev->dev.parent))
894 return port_self_size + vf_ports_size +
895 vf_port_size * dev_num_vf(dev->dev.parent);
897 return port_self_size;
900 static size_t rtnl_xdp_size(void)
902 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
903 nla_total_size(1) + /* XDP_ATTACHED */
904 nla_total_size(4); /* XDP_PROG_ID */
909 static noinline size_t if_nlmsg_size(const struct net_device *dev,
912 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
913 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
914 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
915 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
916 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
917 + nla_total_size(sizeof(struct rtnl_link_stats))
918 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
919 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
920 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
921 + nla_total_size(4) /* IFLA_TXQLEN */
922 + nla_total_size(4) /* IFLA_WEIGHT */
923 + nla_total_size(4) /* IFLA_MTU */
924 + nla_total_size(4) /* IFLA_LINK */
925 + nla_total_size(4) /* IFLA_MASTER */
926 + nla_total_size(1) /* IFLA_CARRIER */
927 + nla_total_size(4) /* IFLA_PROMISCUITY */
928 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
929 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
930 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
931 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
932 + nla_total_size(1) /* IFLA_OPERSTATE */
933 + nla_total_size(1) /* IFLA_LINKMODE */
934 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
935 + nla_total_size(4) /* IFLA_LINK_NETNSID */
936 + nla_total_size(4) /* IFLA_GROUP */
937 + nla_total_size(ext_filter_mask
938 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
939 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
940 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
941 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
942 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
943 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
944 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
945 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
946 + rtnl_xdp_size() /* IFLA_XDP */
947 + nla_total_size(4) /* IFLA_EVENT */
948 + nla_total_size(1); /* IFLA_PROTO_DOWN */
952 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
954 struct nlattr *vf_ports;
955 struct nlattr *vf_port;
959 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
963 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
964 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
966 goto nla_put_failure;
967 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
968 goto nla_put_failure;
969 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
970 if (err == -EMSGSIZE)
971 goto nla_put_failure;
973 nla_nest_cancel(skb, vf_port);
976 nla_nest_end(skb, vf_port);
979 nla_nest_end(skb, vf_ports);
984 nla_nest_cancel(skb, vf_ports);
988 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
990 struct nlattr *port_self;
993 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
997 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
999 nla_nest_cancel(skb, port_self);
1000 return (err == -EMSGSIZE) ? err : 0;
1003 nla_nest_end(skb, port_self);
1008 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1009 u32 ext_filter_mask)
1013 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1014 !(ext_filter_mask & RTEXT_FILTER_VF))
1017 err = rtnl_port_self_fill(skb, dev);
1021 if (dev_num_vf(dev->dev.parent)) {
1022 err = rtnl_vf_ports_fill(skb, dev);
1030 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1033 struct netdev_phys_item_id ppid;
1035 err = dev_get_phys_port_id(dev, &ppid);
1037 if (err == -EOPNOTSUPP)
1042 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1048 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1050 char name[IFNAMSIZ];
1053 err = dev_get_phys_port_name(dev, name, sizeof(name));
1055 if (err == -EOPNOTSUPP)
1060 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1066 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1069 struct switchdev_attr attr = {
1071 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1072 .flags = SWITCHDEV_F_NO_RECURSE,
1075 err = switchdev_port_attr_get(dev, &attr);
1077 if (err == -EOPNOTSUPP)
1082 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1089 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1090 struct net_device *dev)
1092 struct rtnl_link_stats64 *sp;
1093 struct nlattr *attr;
1095 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1096 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1100 sp = nla_data(attr);
1101 dev_get_stats(dev, sp);
1103 attr = nla_reserve(skb, IFLA_STATS,
1104 sizeof(struct rtnl_link_stats));
1108 copy_rtnl_link_stats(nla_data(attr), sp);
1113 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1114 struct net_device *dev,
1116 struct nlattr *vfinfo)
1118 struct ifla_vf_rss_query_en vf_rss_query_en;
1119 struct nlattr *vf, *vfstats, *vfvlanlist;
1120 struct ifla_vf_link_state vf_linkstate;
1121 struct ifla_vf_vlan_info vf_vlan_info;
1122 struct ifla_vf_spoofchk vf_spoofchk;
1123 struct ifla_vf_tx_rate vf_tx_rate;
1124 struct ifla_vf_stats vf_stats;
1125 struct ifla_vf_trust vf_trust;
1126 struct ifla_vf_vlan vf_vlan;
1127 struct ifla_vf_rate vf_rate;
1128 struct ifla_vf_mac vf_mac;
1129 struct ifla_vf_info ivi;
1131 memset(&ivi, 0, sizeof(ivi));
1133 /* Not all SR-IOV capable drivers support the
1134 * spoofcheck and "RSS query enable" query. Preset to
1135 * -1 so the user space tool can detect that the driver
1136 * didn't report anything.
1139 ivi.rss_query_en = -1;
1141 /* The default value for VF link state is "auto"
1142 * IFLA_VF_LINK_STATE_AUTO which equals zero
1145 /* VLAN Protocol by default is 802.1Q */
1146 ivi.vlan_proto = htons(ETH_P_8021Q);
1147 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1150 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1159 vf_rss_query_en.vf =
1160 vf_trust.vf = ivi.vf;
1162 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1163 vf_vlan.vlan = ivi.vlan;
1164 vf_vlan.qos = ivi.qos;
1165 vf_vlan_info.vlan = ivi.vlan;
1166 vf_vlan_info.qos = ivi.qos;
1167 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1168 vf_tx_rate.rate = ivi.max_tx_rate;
1169 vf_rate.min_tx_rate = ivi.min_tx_rate;
1170 vf_rate.max_tx_rate = ivi.max_tx_rate;
1171 vf_spoofchk.setting = ivi.spoofchk;
1172 vf_linkstate.link_state = ivi.linkstate;
1173 vf_rss_query_en.setting = ivi.rss_query_en;
1174 vf_trust.setting = ivi.trusted;
1175 vf = nla_nest_start(skb, IFLA_VF_INFO);
1177 goto nla_put_vfinfo_failure;
1178 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1179 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1180 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1182 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1184 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1186 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1188 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1189 sizeof(vf_rss_query_en),
1190 &vf_rss_query_en) ||
1191 nla_put(skb, IFLA_VF_TRUST,
1192 sizeof(vf_trust), &vf_trust))
1193 goto nla_put_vf_failure;
1194 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1196 goto nla_put_vf_failure;
1197 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1199 nla_nest_cancel(skb, vfvlanlist);
1200 goto nla_put_vf_failure;
1202 nla_nest_end(skb, vfvlanlist);
1203 memset(&vf_stats, 0, sizeof(vf_stats));
1204 if (dev->netdev_ops->ndo_get_vf_stats)
1205 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1207 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1209 goto nla_put_vf_failure;
1210 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1211 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1212 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1213 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1214 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1215 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1216 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1217 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1218 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1219 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1220 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1221 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1222 nla_nest_cancel(skb, vfstats);
1223 goto nla_put_vf_failure;
1225 nla_nest_end(skb, vfstats);
1226 nla_nest_end(skb, vf);
1230 nla_nest_cancel(skb, vf);
1231 nla_put_vfinfo_failure:
1232 nla_nest_cancel(skb, vfinfo);
1236 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1238 struct rtnl_link_ifmap map;
1240 memset(&map, 0, sizeof(map));
1241 map.mem_start = dev->mem_start;
1242 map.mem_end = dev->mem_end;
1243 map.base_addr = dev->base_addr;
1246 map.port = dev->if_port;
1248 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1254 static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
1256 const struct net_device_ops *ops = dev->netdev_ops;
1257 const struct bpf_prog *generic_xdp_prog;
1262 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1263 if (generic_xdp_prog) {
1264 *prog_id = generic_xdp_prog->aux->id;
1265 return XDP_ATTACHED_SKB;
1267 if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp, prog_id))
1268 return XDP_ATTACHED_DRV;
1270 return XDP_ATTACHED_NONE;
1273 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1279 xdp = nla_nest_start(skb, IFLA_XDP);
1283 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
1284 rtnl_xdp_attached_mode(dev, &prog_id));
1289 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1294 nla_nest_end(skb, xdp);
1298 nla_nest_cancel(skb, xdp);
1302 static u32 rtnl_get_event(unsigned long event)
1304 u32 rtnl_event_type = IFLA_EVENT_NONE;
1308 rtnl_event_type = IFLA_EVENT_REBOOT;
1310 case NETDEV_FEAT_CHANGE:
1311 rtnl_event_type = IFLA_EVENT_FEATURES;
1313 case NETDEV_BONDING_FAILOVER:
1314 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1316 case NETDEV_NOTIFY_PEERS:
1317 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1319 case NETDEV_RESEND_IGMP:
1320 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1322 case NETDEV_CHANGEINFODATA:
1323 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1329 return rtnl_event_type;
1332 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1333 int type, u32 pid, u32 seq, u32 change,
1334 unsigned int flags, u32 ext_filter_mask,
1337 struct ifinfomsg *ifm;
1338 struct nlmsghdr *nlh;
1339 struct nlattr *af_spec;
1340 struct rtnl_af_ops *af_ops;
1341 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1344 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1348 ifm = nlmsg_data(nlh);
1349 ifm->ifi_family = AF_UNSPEC;
1351 ifm->ifi_type = dev->type;
1352 ifm->ifi_index = dev->ifindex;
1353 ifm->ifi_flags = dev_get_flags(dev);
1354 ifm->ifi_change = change;
1356 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1357 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1358 nla_put_u8(skb, IFLA_OPERSTATE,
1359 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1360 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1361 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1362 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1363 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1364 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1365 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1366 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1368 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1370 (dev->ifindex != dev_get_iflink(dev) &&
1371 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1373 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1374 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1376 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1378 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1379 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1380 atomic_read(&dev->carrier_changes)) ||
1381 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1382 goto nla_put_failure;
1384 if (event != IFLA_EVENT_NONE) {
1385 if (nla_put_u32(skb, IFLA_EVENT, event))
1386 goto nla_put_failure;
1389 if (rtnl_fill_link_ifmap(skb, dev))
1390 goto nla_put_failure;
1392 if (dev->addr_len) {
1393 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1394 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1395 goto nla_put_failure;
1398 if (rtnl_phys_port_id_fill(skb, dev))
1399 goto nla_put_failure;
1401 if (rtnl_phys_port_name_fill(skb, dev))
1402 goto nla_put_failure;
1404 if (rtnl_phys_switch_id_fill(skb, dev))
1405 goto nla_put_failure;
1407 if (rtnl_fill_stats(skb, dev))
1408 goto nla_put_failure;
1410 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1411 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1412 goto nla_put_failure;
1414 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1415 ext_filter_mask & RTEXT_FILTER_VF) {
1417 struct nlattr *vfinfo;
1418 int num_vfs = dev_num_vf(dev->dev.parent);
1420 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1422 goto nla_put_failure;
1423 for (i = 0; i < num_vfs; i++) {
1424 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1425 goto nla_put_failure;
1428 nla_nest_end(skb, vfinfo);
1431 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1432 goto nla_put_failure;
1434 if (rtnl_xdp_fill(skb, dev))
1435 goto nla_put_failure;
1437 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1438 if (rtnl_link_fill(skb, dev) < 0)
1439 goto nla_put_failure;
1442 if (dev->rtnl_link_ops &&
1443 dev->rtnl_link_ops->get_link_net) {
1444 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1446 if (!net_eq(dev_net(dev), link_net)) {
1447 int id = peernet2id_alloc(dev_net(dev), link_net);
1449 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1450 goto nla_put_failure;
1454 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1455 goto nla_put_failure;
1457 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1458 if (af_ops->fill_link_af) {
1462 if (!(af = nla_nest_start(skb, af_ops->family)))
1463 goto nla_put_failure;
1465 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1468 * Caller may return ENODATA to indicate that there
1469 * was no data to be dumped. This is not an error, it
1470 * means we should trim the attribute header and
1473 if (err == -ENODATA)
1474 nla_nest_cancel(skb, af);
1476 goto nla_put_failure;
1478 nla_nest_end(skb, af);
1482 nla_nest_end(skb, af_spec);
1484 nlmsg_end(skb, nlh);
1488 nlmsg_cancel(skb, nlh);
1492 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1493 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1494 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1495 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1496 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1497 [IFLA_MTU] = { .type = NLA_U32 },
1498 [IFLA_LINK] = { .type = NLA_U32 },
1499 [IFLA_MASTER] = { .type = NLA_U32 },
1500 [IFLA_CARRIER] = { .type = NLA_U8 },
1501 [IFLA_TXQLEN] = { .type = NLA_U32 },
1502 [IFLA_WEIGHT] = { .type = NLA_U32 },
1503 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1504 [IFLA_LINKMODE] = { .type = NLA_U8 },
1505 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1506 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1507 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1508 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1509 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1510 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1511 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1512 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1513 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1514 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1515 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1516 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1517 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1518 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1519 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1520 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1521 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1522 [IFLA_XDP] = { .type = NLA_NESTED },
1523 [IFLA_EVENT] = { .type = NLA_U32 },
1524 [IFLA_GROUP] = { .type = NLA_U32 },
1527 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1528 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1529 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1530 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1531 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1534 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1535 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1536 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1537 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1538 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1539 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1540 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1541 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1542 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1543 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1544 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1545 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1546 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1549 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1550 [IFLA_PORT_VF] = { .type = NLA_U32 },
1551 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1552 .len = PORT_PROFILE_MAX },
1553 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1554 .len = PORT_UUID_MAX },
1555 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1556 .len = PORT_UUID_MAX },
1557 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1558 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1560 /* Unused, but we need to keep it here since user space could
1561 * fill it. It's also broken with regard to NLA_BINARY use in
1562 * combination with structs.
1564 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1565 .len = sizeof(struct ifla_port_vsi) },
1568 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1569 [IFLA_XDP_FD] = { .type = NLA_S32 },
1570 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1571 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1572 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1575 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1577 const struct rtnl_link_ops *ops = NULL;
1578 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1580 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1581 ifla_info_policy, NULL) < 0)
1584 if (linfo[IFLA_INFO_KIND]) {
1585 char kind[MODULE_NAME_LEN];
1587 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1588 ops = rtnl_link_ops_get(kind);
1594 static bool link_master_filtered(struct net_device *dev, int master_idx)
1596 struct net_device *master;
1601 master = netdev_master_upper_dev_get(dev);
1602 if (!master || master->ifindex != master_idx)
1608 static bool link_kind_filtered(const struct net_device *dev,
1609 const struct rtnl_link_ops *kind_ops)
1611 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1617 static bool link_dump_filtered(struct net_device *dev,
1619 const struct rtnl_link_ops *kind_ops)
1621 if (link_master_filtered(dev, master_idx) ||
1622 link_kind_filtered(dev, kind_ops))
1628 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1630 struct net *net = sock_net(skb->sk);
1633 struct net_device *dev;
1634 struct hlist_head *head;
1635 struct nlattr *tb[IFLA_MAX+1];
1636 u32 ext_filter_mask = 0;
1637 const struct rtnl_link_ops *kind_ops = NULL;
1638 unsigned int flags = NLM_F_MULTI;
1644 s_idx = cb->args[1];
1646 cb->seq = net->dev_base_seq;
1648 /* A hack to preserve kernel<->userspace interface.
1649 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1650 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1651 * what iproute2 < v3.9.0 used.
1652 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1653 * attribute, its netlink message is shorter than struct ifinfomsg.
1655 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1656 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1658 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1659 ifla_policy, NULL) >= 0) {
1660 if (tb[IFLA_EXT_MASK])
1661 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1663 if (tb[IFLA_MASTER])
1664 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1666 if (tb[IFLA_LINKINFO])
1667 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1669 if (master_idx || kind_ops)
1670 flags |= NLM_F_DUMP_FILTERED;
1673 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1675 head = &net->dev_index_head[h];
1676 hlist_for_each_entry(dev, head, index_hlist) {
1677 if (link_dump_filtered(dev, master_idx, kind_ops))
1681 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1682 NETLINK_CB(cb->skb).portid,
1683 cb->nlh->nlmsg_seq, 0,
1685 ext_filter_mask, 0);
1688 if (likely(skb->len))
1694 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1708 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1709 struct netlink_ext_ack *exterr)
1711 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1713 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1715 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1718 /* Examine the link attributes and figure out which
1719 * network namespace we are talking about.
1721 if (tb[IFLA_NET_NS_PID])
1722 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1723 else if (tb[IFLA_NET_NS_FD])
1724 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1726 net = get_net(src_net);
1729 EXPORT_SYMBOL(rtnl_link_get_net);
1731 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1734 if (tb[IFLA_ADDRESS] &&
1735 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1738 if (tb[IFLA_BROADCAST] &&
1739 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1743 if (tb[IFLA_AF_SPEC]) {
1747 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1748 const struct rtnl_af_ops *af_ops;
1750 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1751 return -EAFNOSUPPORT;
1753 if (!af_ops->set_link_af)
1756 if (af_ops->validate_link_af) {
1757 err = af_ops->validate_link_af(dev, af);
1767 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1770 const struct net_device_ops *ops = dev->netdev_ops;
1772 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1775 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1777 if (dev->type != ARPHRD_INFINIBAND)
1780 return handle_infiniband_guid(dev, ivt, guid_type);
1783 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1785 const struct net_device_ops *ops = dev->netdev_ops;
1788 if (tb[IFLA_VF_MAC]) {
1789 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1792 if (ops->ndo_set_vf_mac)
1793 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1799 if (tb[IFLA_VF_VLAN]) {
1800 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1803 if (ops->ndo_set_vf_vlan)
1804 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1806 htons(ETH_P_8021Q));
1811 if (tb[IFLA_VF_VLAN_LIST]) {
1812 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1813 struct nlattr *attr;
1817 if (!ops->ndo_set_vf_vlan)
1820 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1821 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1822 nla_len(attr) < NLA_HDRLEN) {
1825 if (len >= MAX_VLAN_LIST_LEN)
1827 ivvl[len] = nla_data(attr);
1834 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1835 ivvl[0]->qos, ivvl[0]->vlan_proto);
1840 if (tb[IFLA_VF_TX_RATE]) {
1841 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1842 struct ifla_vf_info ivf;
1845 if (ops->ndo_get_vf_config)
1846 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1851 if (ops->ndo_set_vf_rate)
1852 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1859 if (tb[IFLA_VF_RATE]) {
1860 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1863 if (ops->ndo_set_vf_rate)
1864 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1871 if (tb[IFLA_VF_SPOOFCHK]) {
1872 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1875 if (ops->ndo_set_vf_spoofchk)
1876 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1882 if (tb[IFLA_VF_LINK_STATE]) {
1883 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1886 if (ops->ndo_set_vf_link_state)
1887 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1893 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1894 struct ifla_vf_rss_query_en *ivrssq_en;
1897 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1898 if (ops->ndo_set_vf_rss_query_en)
1899 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1900 ivrssq_en->setting);
1905 if (tb[IFLA_VF_TRUST]) {
1906 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1909 if (ops->ndo_set_vf_trust)
1910 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1915 if (tb[IFLA_VF_IB_NODE_GUID]) {
1916 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1918 if (!ops->ndo_set_vf_guid)
1921 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1924 if (tb[IFLA_VF_IB_PORT_GUID]) {
1925 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1927 if (!ops->ndo_set_vf_guid)
1930 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1936 static int do_set_master(struct net_device *dev, int ifindex)
1938 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1939 const struct net_device_ops *ops;
1943 if (upper_dev->ifindex == ifindex)
1945 ops = upper_dev->netdev_ops;
1946 if (ops->ndo_del_slave) {
1947 err = ops->ndo_del_slave(upper_dev, dev);
1956 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1959 ops = upper_dev->netdev_ops;
1960 if (ops->ndo_add_slave) {
1961 err = ops->ndo_add_slave(upper_dev, dev);
1971 #define DO_SETLINK_MODIFIED 0x01
1972 /* notify flag means notify + modified. */
1973 #define DO_SETLINK_NOTIFY 0x03
1974 static int do_setlink(const struct sk_buff *skb,
1975 struct net_device *dev, struct ifinfomsg *ifm,
1976 struct netlink_ext_ack *extack,
1977 struct nlattr **tb, char *ifname, int status)
1979 const struct net_device_ops *ops = dev->netdev_ops;
1982 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1983 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1988 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1993 err = dev_change_net_namespace(dev, net, ifname);
1997 status |= DO_SETLINK_MODIFIED;
2001 struct rtnl_link_ifmap *u_map;
2004 if (!ops->ndo_set_config) {
2009 if (!netif_device_present(dev)) {
2014 u_map = nla_data(tb[IFLA_MAP]);
2015 k_map.mem_start = (unsigned long) u_map->mem_start;
2016 k_map.mem_end = (unsigned long) u_map->mem_end;
2017 k_map.base_addr = (unsigned short) u_map->base_addr;
2018 k_map.irq = (unsigned char) u_map->irq;
2019 k_map.dma = (unsigned char) u_map->dma;
2020 k_map.port = (unsigned char) u_map->port;
2022 err = ops->ndo_set_config(dev, &k_map);
2026 status |= DO_SETLINK_NOTIFY;
2029 if (tb[IFLA_ADDRESS]) {
2030 struct sockaddr *sa;
2033 len = sizeof(sa_family_t) + dev->addr_len;
2034 sa = kmalloc(len, GFP_KERNEL);
2039 sa->sa_family = dev->type;
2040 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2042 err = dev_set_mac_address(dev, sa);
2046 status |= DO_SETLINK_MODIFIED;
2050 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2053 status |= DO_SETLINK_MODIFIED;
2056 if (tb[IFLA_GROUP]) {
2057 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2058 status |= DO_SETLINK_NOTIFY;
2062 * Interface selected by interface index but interface
2063 * name provided implies that a name change has been
2066 if (ifm->ifi_index > 0 && ifname[0]) {
2067 err = dev_change_name(dev, ifname);
2070 status |= DO_SETLINK_MODIFIED;
2073 if (tb[IFLA_IFALIAS]) {
2074 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2075 nla_len(tb[IFLA_IFALIAS]));
2078 status |= DO_SETLINK_NOTIFY;
2081 if (tb[IFLA_BROADCAST]) {
2082 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2083 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2086 if (ifm->ifi_flags || ifm->ifi_change) {
2087 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2092 if (tb[IFLA_MASTER]) {
2093 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2096 status |= DO_SETLINK_MODIFIED;
2099 if (tb[IFLA_CARRIER]) {
2100 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2103 status |= DO_SETLINK_MODIFIED;
2106 if (tb[IFLA_TXQLEN]) {
2107 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2108 unsigned int orig_len = dev->tx_queue_len;
2110 if (dev->tx_queue_len ^ value) {
2111 dev->tx_queue_len = value;
2112 err = call_netdevice_notifiers(
2113 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2114 err = notifier_to_errno(err);
2116 dev->tx_queue_len = orig_len;
2119 status |= DO_SETLINK_NOTIFY;
2123 if (tb[IFLA_OPERSTATE])
2124 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2126 if (tb[IFLA_LINKMODE]) {
2127 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2129 write_lock_bh(&dev_base_lock);
2130 if (dev->link_mode ^ value)
2131 status |= DO_SETLINK_NOTIFY;
2132 dev->link_mode = value;
2133 write_unlock_bh(&dev_base_lock);
2136 if (tb[IFLA_VFINFO_LIST]) {
2137 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2138 struct nlattr *attr;
2141 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2142 if (nla_type(attr) != IFLA_VF_INFO ||
2143 nla_len(attr) < NLA_HDRLEN) {
2147 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2148 ifla_vf_policy, NULL);
2151 err = do_setvfinfo(dev, vfinfo);
2154 status |= DO_SETLINK_NOTIFY;
2159 if (tb[IFLA_VF_PORTS]) {
2160 struct nlattr *port[IFLA_PORT_MAX+1];
2161 struct nlattr *attr;
2166 if (!ops->ndo_set_vf_port)
2169 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2170 if (nla_type(attr) != IFLA_VF_PORT ||
2171 nla_len(attr) < NLA_HDRLEN) {
2175 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2176 ifla_port_policy, NULL);
2179 if (!port[IFLA_PORT_VF]) {
2183 vf = nla_get_u32(port[IFLA_PORT_VF]);
2184 err = ops->ndo_set_vf_port(dev, vf, port);
2187 status |= DO_SETLINK_NOTIFY;
2192 if (tb[IFLA_PORT_SELF]) {
2193 struct nlattr *port[IFLA_PORT_MAX+1];
2195 err = nla_parse_nested(port, IFLA_PORT_MAX,
2196 tb[IFLA_PORT_SELF], ifla_port_policy,
2202 if (ops->ndo_set_vf_port)
2203 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2206 status |= DO_SETLINK_NOTIFY;
2209 if (tb[IFLA_AF_SPEC]) {
2213 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2214 const struct rtnl_af_ops *af_ops;
2216 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2219 err = af_ops->set_link_af(dev, af);
2223 status |= DO_SETLINK_NOTIFY;
2228 if (tb[IFLA_PROTO_DOWN]) {
2229 err = dev_change_proto_down(dev,
2230 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2233 status |= DO_SETLINK_NOTIFY;
2237 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2240 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2241 ifla_xdp_policy, NULL);
2245 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2250 if (xdp[IFLA_XDP_FLAGS]) {
2251 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2252 if (xdp_flags & ~XDP_FLAGS_MASK) {
2256 if ((xdp_flags & XDP_FLAGS_SKB_MODE) &&
2257 (xdp_flags & XDP_FLAGS_DRV_MODE)) {
2263 if (xdp[IFLA_XDP_FD]) {
2264 err = dev_change_xdp_fd(dev, extack,
2265 nla_get_s32(xdp[IFLA_XDP_FD]),
2269 status |= DO_SETLINK_NOTIFY;
2274 if (status & DO_SETLINK_MODIFIED) {
2275 if (status & DO_SETLINK_NOTIFY)
2276 netdev_state_change(dev);
2279 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",
2286 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2287 struct netlink_ext_ack *extack)
2289 struct net *net = sock_net(skb->sk);
2290 struct ifinfomsg *ifm;
2291 struct net_device *dev;
2293 struct nlattr *tb[IFLA_MAX+1];
2294 char ifname[IFNAMSIZ];
2296 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2301 if (tb[IFLA_IFNAME])
2302 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2307 ifm = nlmsg_data(nlh);
2308 if (ifm->ifi_index > 0)
2309 dev = __dev_get_by_index(net, ifm->ifi_index);
2310 else if (tb[IFLA_IFNAME])
2311 dev = __dev_get_by_name(net, ifname);
2320 err = validate_linkmsg(dev, tb);
2324 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2329 static int rtnl_group_dellink(const struct net *net, int group)
2331 struct net_device *dev, *aux;
2332 LIST_HEAD(list_kill);
2338 for_each_netdev(net, dev) {
2339 if (dev->group == group) {
2340 const struct rtnl_link_ops *ops;
2343 ops = dev->rtnl_link_ops;
2344 if (!ops || !ops->dellink)
2352 for_each_netdev_safe(net, dev, aux) {
2353 if (dev->group == group) {
2354 const struct rtnl_link_ops *ops;
2356 ops = dev->rtnl_link_ops;
2357 ops->dellink(dev, &list_kill);
2360 unregister_netdevice_many(&list_kill);
2365 int rtnl_delete_link(struct net_device *dev)
2367 const struct rtnl_link_ops *ops;
2368 LIST_HEAD(list_kill);
2370 ops = dev->rtnl_link_ops;
2371 if (!ops || !ops->dellink)
2374 ops->dellink(dev, &list_kill);
2375 unregister_netdevice_many(&list_kill);
2379 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2381 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2382 struct netlink_ext_ack *extack)
2384 struct net *net = sock_net(skb->sk);
2385 struct net_device *dev;
2386 struct ifinfomsg *ifm;
2387 char ifname[IFNAMSIZ];
2388 struct nlattr *tb[IFLA_MAX+1];
2391 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2395 if (tb[IFLA_IFNAME])
2396 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2398 ifm = nlmsg_data(nlh);
2399 if (ifm->ifi_index > 0)
2400 dev = __dev_get_by_index(net, ifm->ifi_index);
2401 else if (tb[IFLA_IFNAME])
2402 dev = __dev_get_by_name(net, ifname);
2403 else if (tb[IFLA_GROUP])
2404 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2411 return rtnl_delete_link(dev);
2414 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2416 unsigned int old_flags;
2419 old_flags = dev->flags;
2420 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2421 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2426 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2428 __dev_notify_flags(dev, old_flags, ~0U);
2431 EXPORT_SYMBOL(rtnl_configure_link);
2433 struct net_device *rtnl_create_link(struct net *net,
2434 const char *ifname, unsigned char name_assign_type,
2435 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2437 struct net_device *dev;
2438 unsigned int num_tx_queues = 1;
2439 unsigned int num_rx_queues = 1;
2441 if (tb[IFLA_NUM_TX_QUEUES])
2442 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2443 else if (ops->get_num_tx_queues)
2444 num_tx_queues = ops->get_num_tx_queues();
2446 if (tb[IFLA_NUM_RX_QUEUES])
2447 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2448 else if (ops->get_num_rx_queues)
2449 num_rx_queues = ops->get_num_rx_queues();
2451 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2452 ops->setup, num_tx_queues, num_rx_queues);
2454 return ERR_PTR(-ENOMEM);
2456 dev_net_set(dev, net);
2457 dev->rtnl_link_ops = ops;
2458 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2461 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2462 if (tb[IFLA_ADDRESS]) {
2463 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2464 nla_len(tb[IFLA_ADDRESS]));
2465 dev->addr_assign_type = NET_ADDR_SET;
2467 if (tb[IFLA_BROADCAST])
2468 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2469 nla_len(tb[IFLA_BROADCAST]));
2470 if (tb[IFLA_TXQLEN])
2471 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2472 if (tb[IFLA_OPERSTATE])
2473 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2474 if (tb[IFLA_LINKMODE])
2475 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2477 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2481 EXPORT_SYMBOL(rtnl_create_link);
2483 static int rtnl_group_changelink(const struct sk_buff *skb,
2484 struct net *net, int group,
2485 struct ifinfomsg *ifm,
2486 struct netlink_ext_ack *extack,
2489 struct net_device *dev, *aux;
2492 for_each_netdev_safe(net, dev, aux) {
2493 if (dev->group == group) {
2494 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2503 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2504 struct netlink_ext_ack *extack)
2506 struct net *net = sock_net(skb->sk);
2507 const struct rtnl_link_ops *ops;
2508 const struct rtnl_link_ops *m_ops = NULL;
2509 struct net_device *dev;
2510 struct net_device *master_dev = NULL;
2511 struct ifinfomsg *ifm;
2512 char kind[MODULE_NAME_LEN];
2513 char ifname[IFNAMSIZ];
2514 struct nlattr *tb[IFLA_MAX+1];
2515 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2516 unsigned char name_assign_type = NET_NAME_USER;
2519 #ifdef CONFIG_MODULES
2522 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2526 if (tb[IFLA_IFNAME])
2527 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2531 ifm = nlmsg_data(nlh);
2532 if (ifm->ifi_index > 0)
2533 dev = __dev_get_by_index(net, ifm->ifi_index);
2536 dev = __dev_get_by_name(net, ifname);
2542 master_dev = netdev_master_upper_dev_get(dev);
2544 m_ops = master_dev->rtnl_link_ops;
2547 err = validate_linkmsg(dev, tb);
2551 if (tb[IFLA_LINKINFO]) {
2552 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2553 tb[IFLA_LINKINFO], ifla_info_policy,
2558 memset(linkinfo, 0, sizeof(linkinfo));
2560 if (linkinfo[IFLA_INFO_KIND]) {
2561 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2562 ops = rtnl_link_ops_get(kind);
2569 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2570 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2571 struct nlattr **data = NULL;
2572 struct nlattr **slave_data = NULL;
2573 struct net *dest_net, *link_net = NULL;
2576 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2577 err = nla_parse_nested(attr, ops->maxtype,
2578 linkinfo[IFLA_INFO_DATA],
2584 if (ops->validate) {
2585 err = ops->validate(tb, data);
2592 if (m_ops->slave_maxtype &&
2593 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2594 err = nla_parse_nested(slave_attr,
2595 m_ops->slave_maxtype,
2596 linkinfo[IFLA_INFO_SLAVE_DATA],
2597 m_ops->slave_policy,
2601 slave_data = slave_attr;
2603 if (m_ops->slave_validate) {
2604 err = m_ops->slave_validate(tb, slave_data);
2613 if (nlh->nlmsg_flags & NLM_F_EXCL)
2615 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2618 if (linkinfo[IFLA_INFO_DATA]) {
2619 if (!ops || ops != dev->rtnl_link_ops ||
2623 err = ops->changelink(dev, tb, data);
2626 status |= DO_SETLINK_NOTIFY;
2629 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2630 if (!m_ops || !m_ops->slave_changelink)
2633 err = m_ops->slave_changelink(master_dev, dev,
2637 status |= DO_SETLINK_NOTIFY;
2640 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2644 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2645 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2646 return rtnl_group_changelink(skb, net,
2647 nla_get_u32(tb[IFLA_GROUP]),
2652 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
2656 #ifdef CONFIG_MODULES
2659 request_module("rtnl-link-%s", kind);
2661 ops = rtnl_link_ops_get(kind);
2673 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2674 name_assign_type = NET_NAME_ENUM;
2677 dest_net = rtnl_link_get_net(net, tb);
2678 if (IS_ERR(dest_net))
2679 return PTR_ERR(dest_net);
2682 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2685 if (tb[IFLA_LINK_NETNSID]) {
2686 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2688 link_net = get_net_ns_by_id(dest_net, id);
2694 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2698 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2699 name_assign_type, ops, tb);
2705 dev->ifindex = ifm->ifi_index;
2708 err = ops->newlink(link_net ? : net, dev, tb, data);
2709 /* Drivers should call free_netdev() in ->destructor
2710 * and unregister it on failure after registration
2711 * so that device could be finally freed in rtnl_unlock.
2714 /* If device is not registered at all, free it now */
2715 if (dev->reg_state == NETREG_UNINITIALIZED)
2720 err = register_netdevice(dev);
2726 err = rtnl_configure_link(dev, ifm);
2728 goto out_unregister;
2730 err = dev_change_net_namespace(dev, dest_net, ifname);
2732 goto out_unregister;
2734 if (tb[IFLA_MASTER]) {
2735 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2737 goto out_unregister;
2746 LIST_HEAD(list_kill);
2748 ops->dellink(dev, &list_kill);
2749 unregister_netdevice_many(&list_kill);
2751 unregister_netdevice(dev);
2757 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2758 struct netlink_ext_ack *extack)
2760 struct net *net = sock_net(skb->sk);
2761 struct ifinfomsg *ifm;
2762 char ifname[IFNAMSIZ];
2763 struct nlattr *tb[IFLA_MAX+1];
2764 struct net_device *dev = NULL;
2765 struct sk_buff *nskb;
2767 u32 ext_filter_mask = 0;
2769 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2773 if (tb[IFLA_IFNAME])
2774 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2776 if (tb[IFLA_EXT_MASK])
2777 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2779 ifm = nlmsg_data(nlh);
2780 if (ifm->ifi_index > 0)
2781 dev = __dev_get_by_index(net, ifm->ifi_index);
2782 else if (tb[IFLA_IFNAME])
2783 dev = __dev_get_by_name(net, ifname);
2790 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2794 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2795 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
2797 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2798 WARN_ON(err == -EMSGSIZE);
2801 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2806 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2808 struct net *net = sock_net(skb->sk);
2809 struct net_device *dev;
2810 struct nlattr *tb[IFLA_MAX+1];
2811 u32 ext_filter_mask = 0;
2812 u16 min_ifinfo_dump_size = 0;
2815 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2816 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2817 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2819 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
2820 if (tb[IFLA_EXT_MASK])
2821 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2824 if (!ext_filter_mask)
2825 return NLMSG_GOODSIZE;
2827 * traverse the list of net devices and compute the minimum
2828 * buffer size based upon the filter mask.
2830 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2831 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2836 return nlmsg_total_size(min_ifinfo_dump_size);
2839 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2842 int s_idx = cb->family;
2846 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2847 int type = cb->nlh->nlmsg_type-RTM_BASE;
2848 if (idx < s_idx || idx == PF_PACKET)
2850 if (rtnl_msg_handlers[idx] == NULL ||
2851 rtnl_msg_handlers[idx][type].dumpit == NULL)
2854 memset(&cb->args[0], 0, sizeof(cb->args));
2858 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2866 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2867 unsigned int change,
2868 u32 event, gfp_t flags)
2870 struct net *net = dev_net(dev);
2871 struct sk_buff *skb;
2873 size_t if_info_size;
2875 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2879 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
2881 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2882 WARN_ON(err == -EMSGSIZE);
2889 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2893 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2895 struct net *net = dev_net(dev);
2897 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2900 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2901 unsigned int change, u32 event,
2904 struct sk_buff *skb;
2906 if (dev->reg_state != NETREG_REGISTERED)
2909 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
2911 rtmsg_ifinfo_send(skb, dev, flags);
2914 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2917 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
2919 EXPORT_SYMBOL(rtmsg_ifinfo);
2921 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2922 struct net_device *dev,
2923 u8 *addr, u16 vid, u32 pid, u32 seq,
2924 int type, unsigned int flags,
2925 int nlflags, u16 ndm_state)
2927 struct nlmsghdr *nlh;
2930 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2934 ndm = nlmsg_data(nlh);
2935 ndm->ndm_family = AF_BRIDGE;
2938 ndm->ndm_flags = flags;
2940 ndm->ndm_ifindex = dev->ifindex;
2941 ndm->ndm_state = ndm_state;
2943 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2944 goto nla_put_failure;
2946 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2947 goto nla_put_failure;
2949 nlmsg_end(skb, nlh);
2953 nlmsg_cancel(skb, nlh);
2957 static inline size_t rtnl_fdb_nlmsg_size(void)
2959 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2960 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2961 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2965 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2968 struct net *net = dev_net(dev);
2969 struct sk_buff *skb;
2972 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2976 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2977 0, 0, type, NTF_SELF, 0, ndm_state);
2983 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2986 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2990 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2992 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2993 struct nlattr *tb[],
2994 struct net_device *dev,
2995 const unsigned char *addr, u16 vid,
3000 /* If aging addresses are supported device will need to
3001 * implement its own handler for this.
3003 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3004 pr_info("%s: FDB only supports static addresses\n", dev->name);
3009 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3013 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3014 err = dev_uc_add_excl(dev, addr);
3015 else if (is_multicast_ether_addr(addr))
3016 err = dev_mc_add_excl(dev, addr);
3018 /* Only return duplicate errors if NLM_F_EXCL is set */
3019 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3024 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3026 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3031 if (nla_len(vlan_attr) != sizeof(u16)) {
3032 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3036 vid = nla_get_u16(vlan_attr);
3038 if (!vid || vid >= VLAN_VID_MASK) {
3039 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3048 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3049 struct netlink_ext_ack *extack)
3051 struct net *net = sock_net(skb->sk);
3053 struct nlattr *tb[NDA_MAX+1];
3054 struct net_device *dev;
3059 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3063 ndm = nlmsg_data(nlh);
3064 if (ndm->ndm_ifindex == 0) {
3065 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3069 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3071 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3075 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3076 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3080 addr = nla_data(tb[NDA_LLADDR]);
3082 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3088 /* Support fdb on master device the net/bridge default case */
3089 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3090 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3091 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3092 const struct net_device_ops *ops = br_dev->netdev_ops;
3094 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3099 ndm->ndm_flags &= ~NTF_MASTER;
3102 /* Embedded bridge, macvlan, and any other device support */
3103 if ((ndm->ndm_flags & NTF_SELF)) {
3104 if (dev->netdev_ops->ndo_fdb_add)
3105 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3109 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3113 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3115 ndm->ndm_flags &= ~NTF_SELF;
3123 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3125 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3126 struct nlattr *tb[],
3127 struct net_device *dev,
3128 const unsigned char *addr, u16 vid)
3132 /* If aging addresses are supported device will need to
3133 * implement its own handler for this.
3135 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3136 pr_info("%s: FDB only supports static addresses\n", dev->name);
3140 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3141 err = dev_uc_del(dev, addr);
3142 else if (is_multicast_ether_addr(addr))
3143 err = dev_mc_del(dev, addr);
3147 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3149 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3150 struct netlink_ext_ack *extack)
3152 struct net *net = sock_net(skb->sk);
3154 struct nlattr *tb[NDA_MAX+1];
3155 struct net_device *dev;
3160 if (!netlink_capable(skb, CAP_NET_ADMIN))
3163 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3167 ndm = nlmsg_data(nlh);
3168 if (ndm->ndm_ifindex == 0) {
3169 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3173 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3175 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3179 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3180 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3184 addr = nla_data(tb[NDA_LLADDR]);
3186 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3192 /* Support fdb on master device the net/bridge default case */
3193 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3194 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3195 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3196 const struct net_device_ops *ops = br_dev->netdev_ops;
3198 if (ops->ndo_fdb_del)
3199 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3204 ndm->ndm_flags &= ~NTF_MASTER;
3207 /* Embedded bridge, macvlan, and any other device support */
3208 if (ndm->ndm_flags & NTF_SELF) {
3209 if (dev->netdev_ops->ndo_fdb_del)
3210 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3213 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3216 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3218 ndm->ndm_flags &= ~NTF_SELF;
3225 static int nlmsg_populate_fdb(struct sk_buff *skb,
3226 struct netlink_callback *cb,
3227 struct net_device *dev,
3229 struct netdev_hw_addr_list *list)
3231 struct netdev_hw_addr *ha;
3235 portid = NETLINK_CB(cb->skb).portid;
3236 seq = cb->nlh->nlmsg_seq;
3238 list_for_each_entry(ha, &list->list, list) {
3239 if (*idx < cb->args[2])
3242 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3244 RTM_NEWNEIGH, NTF_SELF,
3245 NLM_F_MULTI, NUD_PERMANENT);
3255 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3256 * @nlh: netlink message header
3259 * Default netdevice operation to dump the existing unicast address list.
3260 * Returns number of addresses from list put in skb.
3262 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3263 struct netlink_callback *cb,
3264 struct net_device *dev,
3265 struct net_device *filter_dev,
3270 netif_addr_lock_bh(dev);
3271 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3274 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3276 netif_addr_unlock_bh(dev);
3279 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3281 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3283 struct net_device *dev;
3284 struct nlattr *tb[IFLA_MAX+1];
3285 struct net_device *br_dev = NULL;
3286 const struct net_device_ops *ops = NULL;
3287 const struct net_device_ops *cops = NULL;
3288 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3289 struct net *net = sock_net(skb->sk);
3290 struct hlist_head *head;
3298 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3299 IFLA_MAX, ifla_policy, NULL);
3302 } else if (err == 0) {
3303 if (tb[IFLA_MASTER])
3304 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3307 brport_idx = ifm->ifi_index;
3310 br_dev = __dev_get_by_index(net, br_idx);
3314 ops = br_dev->netdev_ops;
3318 s_idx = cb->args[1];
3320 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3322 head = &net->dev_index_head[h];
3323 hlist_for_each_entry(dev, head, index_hlist) {
3325 if (brport_idx && (dev->ifindex != brport_idx))
3328 if (!br_idx) { /* user did not specify a specific bridge */
3329 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3330 br_dev = netdev_master_upper_dev_get(dev);
3331 cops = br_dev->netdev_ops;
3334 if (dev != br_dev &&
3335 !(dev->priv_flags & IFF_BRIDGE_PORT))
3338 if (br_dev != netdev_master_upper_dev_get(dev) &&
3339 !(dev->priv_flags & IFF_EBRIDGE))
3347 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3348 if (cops && cops->ndo_fdb_dump) {
3349 err = cops->ndo_fdb_dump(skb, cb,
3352 if (err == -EMSGSIZE)
3357 if (dev->netdev_ops->ndo_fdb_dump)
3358 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3362 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3364 if (err == -EMSGSIZE)
3369 /* reset fdb offset to 0 for rest of the interfaces */
3385 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3386 unsigned int attrnum, unsigned int flag)
3389 return nla_put_u8(skb, attrnum, !!(flags & flag));
3393 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3394 struct net_device *dev, u16 mode,
3395 u32 flags, u32 mask, int nlflags,
3397 int (*vlan_fill)(struct sk_buff *skb,
3398 struct net_device *dev,
3401 struct nlmsghdr *nlh;
3402 struct ifinfomsg *ifm;
3403 struct nlattr *br_afspec;
3404 struct nlattr *protinfo;
3405 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3406 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3409 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3413 ifm = nlmsg_data(nlh);
3414 ifm->ifi_family = AF_BRIDGE;
3416 ifm->ifi_type = dev->type;
3417 ifm->ifi_index = dev->ifindex;
3418 ifm->ifi_flags = dev_get_flags(dev);
3419 ifm->ifi_change = 0;
3422 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3423 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3424 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3426 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3428 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3429 (dev->ifindex != dev_get_iflink(dev) &&
3430 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3431 goto nla_put_failure;
3433 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3435 goto nla_put_failure;
3437 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3438 nla_nest_cancel(skb, br_afspec);
3439 goto nla_put_failure;
3442 if (mode != BRIDGE_MODE_UNDEF) {
3443 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3444 nla_nest_cancel(skb, br_afspec);
3445 goto nla_put_failure;
3449 err = vlan_fill(skb, dev, filter_mask);
3451 nla_nest_cancel(skb, br_afspec);
3452 goto nla_put_failure;
3455 nla_nest_end(skb, br_afspec);
3457 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3459 goto nla_put_failure;
3461 if (brport_nla_put_flag(skb, flags, mask,
3462 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3463 brport_nla_put_flag(skb, flags, mask,
3464 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3465 brport_nla_put_flag(skb, flags, mask,
3466 IFLA_BRPORT_FAST_LEAVE,
3467 BR_MULTICAST_FAST_LEAVE) ||
3468 brport_nla_put_flag(skb, flags, mask,
3469 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3470 brport_nla_put_flag(skb, flags, mask,
3471 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3472 brport_nla_put_flag(skb, flags, mask,
3473 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3474 brport_nla_put_flag(skb, flags, mask,
3475 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3476 brport_nla_put_flag(skb, flags, mask,
3477 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3478 nla_nest_cancel(skb, protinfo);
3479 goto nla_put_failure;
3482 nla_nest_end(skb, protinfo);
3484 nlmsg_end(skb, nlh);
3487 nlmsg_cancel(skb, nlh);
3488 return err ? err : -EMSGSIZE;
3490 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3492 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3494 struct net *net = sock_net(skb->sk);
3495 struct net_device *dev;
3497 u32 portid = NETLINK_CB(cb->skb).portid;
3498 u32 seq = cb->nlh->nlmsg_seq;
3499 u32 filter_mask = 0;
3502 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3503 struct nlattr *extfilt;
3505 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3508 if (nla_len(extfilt) < sizeof(filter_mask))
3511 filter_mask = nla_get_u32(extfilt);
3516 for_each_netdev_rcu(net, dev) {
3517 const struct net_device_ops *ops = dev->netdev_ops;
3518 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3520 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3521 if (idx >= cb->args[0]) {
3522 err = br_dev->netdev_ops->ndo_bridge_getlink(
3523 skb, portid, seq, dev,
3524 filter_mask, NLM_F_MULTI);
3525 if (err < 0 && err != -EOPNOTSUPP) {
3526 if (likely(skb->len))
3535 if (ops->ndo_bridge_getlink) {
3536 if (idx >= cb->args[0]) {
3537 err = ops->ndo_bridge_getlink(skb, portid,
3541 if (err < 0 && err != -EOPNOTSUPP) {
3542 if (likely(skb->len))
3559 static inline size_t bridge_nlmsg_size(void)
3561 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3562 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3563 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3564 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3565 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3566 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3567 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3568 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3569 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3570 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3571 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3574 static int rtnl_bridge_notify(struct net_device *dev)
3576 struct net *net = dev_net(dev);
3577 struct sk_buff *skb;
3578 int err = -EOPNOTSUPP;
3580 if (!dev->netdev_ops->ndo_bridge_getlink)
3583 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3589 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3596 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3599 WARN_ON(err == -EMSGSIZE);
3602 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3606 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3607 struct netlink_ext_ack *extack)
3609 struct net *net = sock_net(skb->sk);
3610 struct ifinfomsg *ifm;
3611 struct net_device *dev;
3612 struct nlattr *br_spec, *attr = NULL;
3613 int rem, err = -EOPNOTSUPP;
3615 bool have_flags = false;
3617 if (nlmsg_len(nlh) < sizeof(*ifm))
3620 ifm = nlmsg_data(nlh);
3621 if (ifm->ifi_family != AF_BRIDGE)
3622 return -EPFNOSUPPORT;
3624 dev = __dev_get_by_index(net, ifm->ifi_index);
3626 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3630 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3632 nla_for_each_nested(attr, br_spec, rem) {
3633 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3634 if (nla_len(attr) < sizeof(flags))
3638 flags = nla_get_u16(attr);
3644 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3645 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3647 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3652 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3656 flags &= ~BRIDGE_FLAGS_MASTER;
3659 if ((flags & BRIDGE_FLAGS_SELF)) {
3660 if (!dev->netdev_ops->ndo_bridge_setlink)
3663 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3666 flags &= ~BRIDGE_FLAGS_SELF;
3668 /* Generate event to notify upper layer of bridge
3671 err = rtnl_bridge_notify(dev);
3676 memcpy(nla_data(attr), &flags, sizeof(flags));
3681 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3682 struct netlink_ext_ack *extack)
3684 struct net *net = sock_net(skb->sk);
3685 struct ifinfomsg *ifm;
3686 struct net_device *dev;
3687 struct nlattr *br_spec, *attr = NULL;
3688 int rem, err = -EOPNOTSUPP;
3690 bool have_flags = false;
3692 if (nlmsg_len(nlh) < sizeof(*ifm))
3695 ifm = nlmsg_data(nlh);
3696 if (ifm->ifi_family != AF_BRIDGE)
3697 return -EPFNOSUPPORT;
3699 dev = __dev_get_by_index(net, ifm->ifi_index);
3701 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3705 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3707 nla_for_each_nested(attr, br_spec, rem) {
3708 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3709 if (nla_len(attr) < sizeof(flags))
3713 flags = nla_get_u16(attr);
3719 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3720 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3722 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3727 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3731 flags &= ~BRIDGE_FLAGS_MASTER;
3734 if ((flags & BRIDGE_FLAGS_SELF)) {
3735 if (!dev->netdev_ops->ndo_bridge_dellink)
3738 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3742 flags &= ~BRIDGE_FLAGS_SELF;
3744 /* Generate event to notify upper layer of bridge
3747 err = rtnl_bridge_notify(dev);
3752 memcpy(nla_data(attr), &flags, sizeof(flags));
3757 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3759 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3760 (!idxattr || idxattr == attrid);
3763 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3764 static int rtnl_get_offload_stats_attr_size(int attr_id)
3767 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3768 return sizeof(struct rtnl_link_stats64);
3774 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3777 struct nlattr *attr = NULL;
3782 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3783 dev->netdev_ops->ndo_get_offload_stats))
3786 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3787 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3788 if (attr_id < *prividx)
3791 size = rtnl_get_offload_stats_attr_size(attr_id);
3795 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3798 attr = nla_reserve_64bit(skb, attr_id, size,
3799 IFLA_OFFLOAD_XSTATS_UNSPEC);
3801 goto nla_put_failure;
3803 attr_data = nla_data(attr);
3804 memset(attr_data, 0, size);
3805 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3808 goto get_offload_stats_failure;
3819 get_offload_stats_failure:
3824 static int rtnl_get_offload_stats_size(const struct net_device *dev)
3830 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3831 dev->netdev_ops->ndo_get_offload_stats))
3834 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3835 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3836 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
3838 size = rtnl_get_offload_stats_attr_size(attr_id);
3839 nla_size += nla_total_size_64bit(size);
3843 nla_size += nla_total_size(0);
3848 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3849 int type, u32 pid, u32 seq, u32 change,
3850 unsigned int flags, unsigned int filter_mask,
3851 int *idxattr, int *prividx)
3853 struct if_stats_msg *ifsm;
3854 struct nlmsghdr *nlh;
3855 struct nlattr *attr;
3856 int s_prividx = *prividx;
3861 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3865 ifsm = nlmsg_data(nlh);
3866 ifsm->ifindex = dev->ifindex;
3867 ifsm->filter_mask = filter_mask;
3869 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3870 struct rtnl_link_stats64 *sp;
3872 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3873 sizeof(struct rtnl_link_stats64),
3876 goto nla_put_failure;
3878 sp = nla_data(attr);
3879 dev_get_stats(dev, sp);
3882 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3883 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3885 if (ops && ops->fill_linkxstats) {
3886 *idxattr = IFLA_STATS_LINK_XSTATS;
3887 attr = nla_nest_start(skb,
3888 IFLA_STATS_LINK_XSTATS);
3890 goto nla_put_failure;
3892 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3893 nla_nest_end(skb, attr);
3895 goto nla_put_failure;
3900 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3902 const struct rtnl_link_ops *ops = NULL;
3903 const struct net_device *master;
3905 master = netdev_master_upper_dev_get(dev);
3907 ops = master->rtnl_link_ops;
3908 if (ops && ops->fill_linkxstats) {
3909 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3910 attr = nla_nest_start(skb,
3911 IFLA_STATS_LINK_XSTATS_SLAVE);
3913 goto nla_put_failure;
3915 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3916 nla_nest_end(skb, attr);
3918 goto nla_put_failure;
3923 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3925 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3926 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3928 goto nla_put_failure;
3930 err = rtnl_get_offload_stats(skb, dev, prividx);
3931 if (err == -ENODATA)
3932 nla_nest_cancel(skb, attr);
3934 nla_nest_end(skb, attr);
3936 if (err && err != -ENODATA)
3937 goto nla_put_failure;
3941 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3942 struct rtnl_af_ops *af_ops;
3944 *idxattr = IFLA_STATS_AF_SPEC;
3945 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3947 goto nla_put_failure;
3949 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3950 if (af_ops->fill_stats_af) {
3954 af = nla_nest_start(skb, af_ops->family);
3956 goto nla_put_failure;
3958 err = af_ops->fill_stats_af(skb, dev);
3960 if (err == -ENODATA)
3961 nla_nest_cancel(skb, af);
3963 goto nla_put_failure;
3965 nla_nest_end(skb, af);
3969 nla_nest_end(skb, attr);
3974 nlmsg_end(skb, nlh);
3979 /* not a multi message or no progress mean a real error */
3980 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3981 nlmsg_cancel(skb, nlh);
3983 nlmsg_end(skb, nlh);
3988 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3993 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3994 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3996 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3997 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3998 int attr = IFLA_STATS_LINK_XSTATS;
4000 if (ops && ops->get_linkxstats_size) {
4001 size += nla_total_size(ops->get_linkxstats_size(dev,
4003 /* for IFLA_STATS_LINK_XSTATS */
4004 size += nla_total_size(0);
4008 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4009 struct net_device *_dev = (struct net_device *)dev;
4010 const struct rtnl_link_ops *ops = NULL;
4011 const struct net_device *master;
4013 /* netdev_master_upper_dev_get can't take const */
4014 master = netdev_master_upper_dev_get(_dev);
4016 ops = master->rtnl_link_ops;
4017 if (ops && ops->get_linkxstats_size) {
4018 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4020 size += nla_total_size(ops->get_linkxstats_size(dev,
4022 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4023 size += nla_total_size(0);
4027 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4028 size += rtnl_get_offload_stats_size(dev);
4030 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4031 struct rtnl_af_ops *af_ops;
4033 /* for IFLA_STATS_AF_SPEC */
4034 size += nla_total_size(0);
4036 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4037 if (af_ops->get_stats_af_size) {
4038 size += nla_total_size(
4039 af_ops->get_stats_af_size(dev));
4042 size += nla_total_size(0);
4050 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4051 struct netlink_ext_ack *extack)
4053 struct net *net = sock_net(skb->sk);
4054 struct net_device *dev = NULL;
4055 int idxattr = 0, prividx = 0;
4056 struct if_stats_msg *ifsm;
4057 struct sk_buff *nskb;
4061 if (nlmsg_len(nlh) < sizeof(*ifsm))
4064 ifsm = nlmsg_data(nlh);
4065 if (ifsm->ifindex > 0)
4066 dev = __dev_get_by_index(net, ifsm->ifindex);
4073 filter_mask = ifsm->filter_mask;
4077 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4081 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4082 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4083 0, filter_mask, &idxattr, &prividx);
4085 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4086 WARN_ON(err == -EMSGSIZE);
4089 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4095 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4097 int h, s_h, err, s_idx, s_idxattr, s_prividx;
4098 struct net *net = sock_net(skb->sk);
4099 unsigned int flags = NLM_F_MULTI;
4100 struct if_stats_msg *ifsm;
4101 struct hlist_head *head;
4102 struct net_device *dev;
4103 u32 filter_mask = 0;
4107 s_idx = cb->args[1];
4108 s_idxattr = cb->args[2];
4109 s_prividx = cb->args[3];
4111 cb->seq = net->dev_base_seq;
4113 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4116 ifsm = nlmsg_data(cb->nlh);
4117 filter_mask = ifsm->filter_mask;
4121 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4123 head = &net->dev_index_head[h];
4124 hlist_for_each_entry(dev, head, index_hlist) {
4127 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4128 NETLINK_CB(cb->skb).portid,
4129 cb->nlh->nlmsg_seq, 0,
4131 &s_idxattr, &s_prividx);
4132 /* If we ran out of room on the first message,
4135 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4141 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4147 cb->args[3] = s_prividx;
4148 cb->args[2] = s_idxattr;
4155 /* Process one rtnetlink message. */
4157 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4158 struct netlink_ext_ack *extack)
4160 struct net *net = sock_net(skb->sk);
4161 rtnl_doit_func doit;
4167 type = nlh->nlmsg_type;
4173 /* All the messages must have at least 1 byte length */
4174 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4177 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4180 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4183 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4185 rtnl_dumpit_func dumpit;
4186 rtnl_calcit_func calcit;
4187 u16 min_dump_alloc = 0;
4189 dumpit = rtnl_get_dumpit(family, type);
4192 calcit = rtnl_get_calcit(family, type);
4194 min_dump_alloc = calcit(skb, nlh);
4199 struct netlink_dump_control c = {
4201 .min_dump_alloc = min_dump_alloc,
4203 err = netlink_dump_start(rtnl, skb, nlh, &c);
4209 doit = rtnl_get_doit(family, type);
4213 return doit(skb, nlh, extack);
4216 static void rtnetlink_rcv(struct sk_buff *skb)
4219 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4223 static int rtnetlink_bind(struct net *net, int group)
4226 case RTNLGRP_IPV4_MROUTE_R:
4227 case RTNLGRP_IPV6_MROUTE_R:
4228 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4235 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4237 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4241 case NETDEV_CHANGENAME:
4242 case NETDEV_FEAT_CHANGE:
4243 case NETDEV_BONDING_FAILOVER:
4244 case NETDEV_NOTIFY_PEERS:
4245 case NETDEV_RESEND_IGMP:
4246 case NETDEV_CHANGEINFODATA:
4247 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4256 static struct notifier_block rtnetlink_dev_notifier = {
4257 .notifier_call = rtnetlink_event,
4261 static int __net_init rtnetlink_net_init(struct net *net)
4264 struct netlink_kernel_cfg cfg = {
4265 .groups = RTNLGRP_MAX,
4266 .input = rtnetlink_rcv,
4267 .cb_mutex = &rtnl_mutex,
4268 .flags = NL_CFG_F_NONROOT_RECV,
4269 .bind = rtnetlink_bind,
4272 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4279 static void __net_exit rtnetlink_net_exit(struct net *net)
4281 netlink_kernel_release(net->rtnl);
4285 static struct pernet_operations rtnetlink_net_ops = {
4286 .init = rtnetlink_net_init,
4287 .exit = rtnetlink_net_exit,
4290 void __init rtnetlink_init(void)
4292 if (register_pernet_subsys(&rtnetlink_net_ops))
4293 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4295 register_netdevice_notifier(&rtnetlink_dev_notifier);
4297 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4298 rtnl_dump_ifinfo, rtnl_calcit);
4299 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4300 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4301 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
4303 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4304 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
4305 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL);
4307 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4308 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4309 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
4311 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
4312 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
4313 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
4315 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,