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/pci.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
51 #include <net/pkt_sched.h>
52 #include <net/fib_rules.h>
53 #include <net/rtnetlink.h>
54 #include <net/net_namespace.h>
58 rtnl_dumpit_func dumpit;
59 rtnl_calcit_func calcit;
62 static DEFINE_MUTEX(rtnl_mutex);
66 mutex_lock(&rtnl_mutex);
68 EXPORT_SYMBOL(rtnl_lock);
70 void __rtnl_unlock(void)
72 mutex_unlock(&rtnl_mutex);
75 void rtnl_unlock(void)
77 /* This fellow will unlock it for us. */
80 EXPORT_SYMBOL(rtnl_unlock);
82 int rtnl_trylock(void)
84 return mutex_trylock(&rtnl_mutex);
86 EXPORT_SYMBOL(rtnl_trylock);
88 int rtnl_is_locked(void)
90 return mutex_is_locked(&rtnl_mutex);
92 EXPORT_SYMBOL(rtnl_is_locked);
94 #ifdef CONFIG_PROVE_LOCKING
95 int lockdep_rtnl_is_held(void)
97 return lockdep_is_held(&rtnl_mutex);
99 EXPORT_SYMBOL(lockdep_rtnl_is_held);
100 #endif /* #ifdef CONFIG_PROVE_LOCKING */
102 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104 static inline int rtm_msgindex(int msgtype)
106 int msgindex = msgtype - RTM_BASE;
109 * msgindex < 0 implies someone tried to register a netlink
110 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
111 * the message type has not been added to linux/rtnetlink.h
113 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120 struct rtnl_link *tab;
122 if (protocol <= RTNL_FAMILY_MAX)
123 tab = rtnl_msg_handlers[protocol];
127 if (tab == NULL || tab[msgindex].doit == NULL)
128 tab = rtnl_msg_handlers[PF_UNSPEC];
130 return tab ? tab[msgindex].doit : NULL;
133 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135 struct rtnl_link *tab;
137 if (protocol <= RTNL_FAMILY_MAX)
138 tab = rtnl_msg_handlers[protocol];
142 if (tab == NULL || tab[msgindex].dumpit == NULL)
143 tab = rtnl_msg_handlers[PF_UNSPEC];
145 return tab ? tab[msgindex].dumpit : NULL;
148 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150 struct rtnl_link *tab;
152 if (protocol <= RTNL_FAMILY_MAX)
153 tab = rtnl_msg_handlers[protocol];
157 if (tab == NULL || tab[msgindex].calcit == NULL)
158 tab = rtnl_msg_handlers[PF_UNSPEC];
160 return tab ? tab[msgindex].calcit : NULL;
164 * __rtnl_register - Register a rtnetlink message type
165 * @protocol: Protocol family or PF_UNSPEC
166 * @msgtype: rtnetlink message type
167 * @doit: Function pointer called for each request message
168 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
169 * @calcit: Function pointer to calc size of dump message
171 * Registers the specified function pointers (at least one of them has
172 * to be non-NULL) to be called whenever a request message for the
173 * specified protocol family and message type is received.
175 * The special protocol family PF_UNSPEC may be used to define fallback
176 * function pointers for the case when no entry for the specific protocol
179 * Returns 0 on success or a negative error code.
181 int __rtnl_register(int protocol, int msgtype,
182 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
183 rtnl_calcit_func calcit)
185 struct rtnl_link *tab;
188 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
189 msgindex = rtm_msgindex(msgtype);
191 tab = rtnl_msg_handlers[protocol];
193 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
197 rtnl_msg_handlers[protocol] = tab;
201 tab[msgindex].doit = doit;
204 tab[msgindex].dumpit = dumpit;
207 tab[msgindex].calcit = calcit;
211 EXPORT_SYMBOL_GPL(__rtnl_register);
214 * rtnl_register - Register a rtnetlink message type
216 * Identical to __rtnl_register() but panics on failure. This is useful
217 * as failure of this function is very unlikely, it can only happen due
218 * to lack of memory when allocating the chain to store all message
219 * handlers for a protocol. Meant for use in init functions where lack
220 * of memory implies no sense in continuing.
222 void rtnl_register(int protocol, int msgtype,
223 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
224 rtnl_calcit_func calcit)
226 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
227 panic("Unable to register rtnetlink message handler, "
228 "protocol = %d, message type = %d\n",
231 EXPORT_SYMBOL_GPL(rtnl_register);
234 * rtnl_unregister - Unregister a rtnetlink message type
235 * @protocol: Protocol family or PF_UNSPEC
236 * @msgtype: rtnetlink message type
238 * Returns 0 on success or a negative error code.
240 int rtnl_unregister(int protocol, int msgtype)
244 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
245 msgindex = rtm_msgindex(msgtype);
247 if (rtnl_msg_handlers[protocol] == NULL)
250 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
251 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
255 EXPORT_SYMBOL_GPL(rtnl_unregister);
258 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
259 * @protocol : Protocol family or PF_UNSPEC
261 * Identical to calling rtnl_unregster() for all registered message types
262 * of a certain protocol family.
264 void rtnl_unregister_all(int protocol)
266 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268 kfree(rtnl_msg_handlers[protocol]);
269 rtnl_msg_handlers[protocol] = NULL;
271 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
273 static LIST_HEAD(link_ops);
276 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
277 * @ops: struct rtnl_link_ops * to register
279 * The caller must hold the rtnl_mutex. This function should be used
280 * by drivers that create devices during module initialization. It
281 * must be called before registering the devices.
283 * Returns 0 on success or a negative error code.
285 int __rtnl_link_register(struct rtnl_link_ops *ops)
288 ops->dellink = unregister_netdevice_queue;
290 list_add_tail(&ops->list, &link_ops);
293 EXPORT_SYMBOL_GPL(__rtnl_link_register);
296 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
297 * @ops: struct rtnl_link_ops * to register
299 * Returns 0 on success or a negative error code.
301 int rtnl_link_register(struct rtnl_link_ops *ops)
306 err = __rtnl_link_register(ops);
310 EXPORT_SYMBOL_GPL(rtnl_link_register);
312 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
314 struct net_device *dev;
315 LIST_HEAD(list_kill);
317 for_each_netdev(net, dev) {
318 if (dev->rtnl_link_ops == ops)
319 ops->dellink(dev, &list_kill);
321 unregister_netdevice_many(&list_kill);
325 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
326 * @ops: struct rtnl_link_ops * to unregister
328 * The caller must hold the rtnl_mutex.
330 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
335 __rtnl_kill_links(net, ops);
337 list_del(&ops->list);
339 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
342 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
345 void rtnl_link_unregister(struct rtnl_link_ops *ops)
348 __rtnl_link_unregister(ops);
351 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
353 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
355 const struct rtnl_link_ops *ops;
357 list_for_each_entry(ops, &link_ops, list) {
358 if (!strcmp(ops->kind, kind))
364 static size_t rtnl_link_get_size(const struct net_device *dev)
366 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
372 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
373 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
376 /* IFLA_INFO_DATA + nested data */
377 size += nla_total_size(sizeof(struct nlattr)) +
380 if (ops->get_xstats_size)
381 /* IFLA_INFO_XSTATS */
382 size += nla_total_size(ops->get_xstats_size(dev));
387 static LIST_HEAD(rtnl_af_ops);
389 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
391 const struct rtnl_af_ops *ops;
393 list_for_each_entry(ops, &rtnl_af_ops, list) {
394 if (ops->family == family)
402 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
403 * @ops: struct rtnl_af_ops * to register
405 * The caller must hold the rtnl_mutex.
407 * Returns 0 on success or a negative error code.
409 int __rtnl_af_register(struct rtnl_af_ops *ops)
411 list_add_tail(&ops->list, &rtnl_af_ops);
414 EXPORT_SYMBOL_GPL(__rtnl_af_register);
417 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
418 * @ops: struct rtnl_af_ops * to register
420 * Returns 0 on success or a negative error code.
422 int rtnl_af_register(struct rtnl_af_ops *ops)
427 err = __rtnl_af_register(ops);
431 EXPORT_SYMBOL_GPL(rtnl_af_register);
434 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
435 * @ops: struct rtnl_af_ops * to unregister
437 * The caller must hold the rtnl_mutex.
439 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
441 list_del(&ops->list);
443 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
446 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
447 * @ops: struct rtnl_af_ops * to unregister
449 void rtnl_af_unregister(struct rtnl_af_ops *ops)
452 __rtnl_af_unregister(ops);
455 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
457 static size_t rtnl_link_get_af_size(const struct net_device *dev)
459 struct rtnl_af_ops *af_ops;
463 size = nla_total_size(sizeof(struct nlattr));
465 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
466 if (af_ops->get_link_af_size) {
467 /* AF_* + nested data */
468 size += nla_total_size(sizeof(struct nlattr)) +
469 af_ops->get_link_af_size(dev);
476 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
478 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
479 struct nlattr *linkinfo, *data;
482 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
483 if (linkinfo == NULL)
486 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
487 goto err_cancel_link;
488 if (ops->fill_xstats) {
489 err = ops->fill_xstats(skb, dev);
491 goto err_cancel_link;
493 if (ops->fill_info) {
494 data = nla_nest_start(skb, IFLA_INFO_DATA);
496 goto err_cancel_link;
497 err = ops->fill_info(skb, dev);
499 goto err_cancel_data;
500 nla_nest_end(skb, data);
503 nla_nest_end(skb, linkinfo);
507 nla_nest_cancel(skb, data);
509 nla_nest_cancel(skb, linkinfo);
514 static const int rtm_min[RTM_NR_FAMILIES] =
516 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
517 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
518 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
519 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
520 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
521 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
522 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
523 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
524 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
525 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
528 static const int rta_max[RTM_NR_FAMILIES] =
530 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
531 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
532 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
533 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
534 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
535 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
536 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
537 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
540 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
543 int size = RTA_LENGTH(attrlen);
545 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
546 rta->rta_type = attrtype;
548 memcpy(RTA_DATA(rta), data, attrlen);
549 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
551 EXPORT_SYMBOL(__rta_fill);
553 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
555 struct sock *rtnl = net->rtnl;
558 NETLINK_CB(skb).dst_group = group;
560 atomic_inc(&skb->users);
561 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
563 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
567 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
569 struct sock *rtnl = net->rtnl;
571 return nlmsg_unicast(rtnl, skb, pid);
573 EXPORT_SYMBOL(rtnl_unicast);
575 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
576 struct nlmsghdr *nlh, gfp_t flags)
578 struct sock *rtnl = net->rtnl;
582 report = nlmsg_report(nlh);
584 nlmsg_notify(rtnl, skb, pid, group, report, flags);
586 EXPORT_SYMBOL(rtnl_notify);
588 void rtnl_set_sk_err(struct net *net, u32 group, int error)
590 struct sock *rtnl = net->rtnl;
592 netlink_set_err(rtnl, 0, group, error);
594 EXPORT_SYMBOL(rtnl_set_sk_err);
596 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
601 mx = nla_nest_start(skb, RTA_METRICS);
605 for (i = 0; i < RTAX_MAX; i++) {
608 NLA_PUT_U32(skb, i+1, metrics[i]);
613 nla_nest_cancel(skb, mx);
617 return nla_nest_end(skb, mx);
620 nla_nest_cancel(skb, mx);
623 EXPORT_SYMBOL(rtnetlink_put_metrics);
625 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
626 u32 ts, u32 tsage, long expires, u32 error)
628 struct rta_cacheinfo ci = {
629 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
630 .rta_used = dst->__use,
631 .rta_clntref = atomic_read(&(dst->__refcnt)),
639 ci.rta_expires = jiffies_to_clock_t(expires);
641 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
643 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
645 static void set_operstate(struct net_device *dev, unsigned char transition)
647 unsigned char operstate = dev->operstate;
649 switch (transition) {
651 if ((operstate == IF_OPER_DORMANT ||
652 operstate == IF_OPER_UNKNOWN) &&
654 operstate = IF_OPER_UP;
657 case IF_OPER_DORMANT:
658 if (operstate == IF_OPER_UP ||
659 operstate == IF_OPER_UNKNOWN)
660 operstate = IF_OPER_DORMANT;
664 if (dev->operstate != operstate) {
665 write_lock_bh(&dev_base_lock);
666 dev->operstate = operstate;
667 write_unlock_bh(&dev_base_lock);
668 netdev_state_change(dev);
672 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
674 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
675 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
678 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
679 const struct ifinfomsg *ifm)
681 unsigned int flags = ifm->ifi_flags;
683 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
685 flags = (flags & ifm->ifi_change) |
686 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
691 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
692 const struct rtnl_link_stats64 *b)
694 a->rx_packets = b->rx_packets;
695 a->tx_packets = b->tx_packets;
696 a->rx_bytes = b->rx_bytes;
697 a->tx_bytes = b->tx_bytes;
698 a->rx_errors = b->rx_errors;
699 a->tx_errors = b->tx_errors;
700 a->rx_dropped = b->rx_dropped;
701 a->tx_dropped = b->tx_dropped;
703 a->multicast = b->multicast;
704 a->collisions = b->collisions;
706 a->rx_length_errors = b->rx_length_errors;
707 a->rx_over_errors = b->rx_over_errors;
708 a->rx_crc_errors = b->rx_crc_errors;
709 a->rx_frame_errors = b->rx_frame_errors;
710 a->rx_fifo_errors = b->rx_fifo_errors;
711 a->rx_missed_errors = b->rx_missed_errors;
713 a->tx_aborted_errors = b->tx_aborted_errors;
714 a->tx_carrier_errors = b->tx_carrier_errors;
715 a->tx_fifo_errors = b->tx_fifo_errors;
716 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
717 a->tx_window_errors = b->tx_window_errors;
719 a->rx_compressed = b->rx_compressed;
720 a->tx_compressed = b->tx_compressed;
723 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
725 memcpy(v, b, sizeof(*b));
729 static inline int rtnl_vfinfo_size(const struct net_device *dev,
732 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
733 (ext_filter_mask & RTEXT_FILTER_VF)) {
734 int num_vfs = dev_num_vf(dev->dev.parent);
735 size_t size = nla_total_size(sizeof(struct nlattr));
736 size += nla_total_size(num_vfs * sizeof(struct nlattr));
738 (nla_total_size(sizeof(struct ifla_vf_mac)) +
739 nla_total_size(sizeof(struct ifla_vf_vlan)) +
740 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
741 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
747 static size_t rtnl_port_size(const struct net_device *dev,
750 size_t port_size = nla_total_size(4) /* PORT_VF */
751 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
752 + nla_total_size(sizeof(struct ifla_port_vsi))
754 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
755 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
756 + nla_total_size(1) /* PROT_VDP_REQUEST */
757 + nla_total_size(2); /* PORT_VDP_RESPONSE */
758 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
759 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
761 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
764 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
765 !(ext_filter_mask & RTEXT_FILTER_VF))
767 if (dev_num_vf(dev->dev.parent))
768 return port_self_size + vf_ports_size +
769 vf_port_size * dev_num_vf(dev->dev.parent);
771 return port_self_size;
774 static noinline size_t if_nlmsg_size(const struct net_device *dev,
777 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
778 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
779 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
780 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
781 + nla_total_size(sizeof(struct rtnl_link_ifmap))
782 + nla_total_size(sizeof(struct rtnl_link_stats))
783 + nla_total_size(sizeof(struct rtnl_link_stats64))
784 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
785 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
786 + nla_total_size(4) /* IFLA_TXQLEN */
787 + nla_total_size(4) /* IFLA_WEIGHT */
788 + nla_total_size(4) /* IFLA_MTU */
789 + nla_total_size(4) /* IFLA_LINK */
790 + nla_total_size(4) /* IFLA_MASTER */
791 + nla_total_size(1) /* IFLA_OPERSTATE */
792 + nla_total_size(1) /* IFLA_LINKMODE */
793 + nla_total_size(ext_filter_mask
794 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
795 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
796 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
797 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
798 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
801 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
803 struct nlattr *vf_ports;
804 struct nlattr *vf_port;
808 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
812 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
813 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
815 goto nla_put_failure;
816 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
817 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
818 if (err == -EMSGSIZE)
819 goto nla_put_failure;
821 nla_nest_cancel(skb, vf_port);
824 nla_nest_end(skb, vf_port);
827 nla_nest_end(skb, vf_ports);
832 nla_nest_cancel(skb, vf_ports);
836 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
838 struct nlattr *port_self;
841 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
845 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
847 nla_nest_cancel(skb, port_self);
848 return (err == -EMSGSIZE) ? err : 0;
851 nla_nest_end(skb, port_self);
856 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
861 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
862 !(ext_filter_mask & RTEXT_FILTER_VF))
865 err = rtnl_port_self_fill(skb, dev);
869 if (dev_num_vf(dev->dev.parent)) {
870 err = rtnl_vf_ports_fill(skb, dev);
878 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
879 int type, u32 pid, u32 seq, u32 change,
880 unsigned int flags, u32 ext_filter_mask)
882 struct ifinfomsg *ifm;
883 struct nlmsghdr *nlh;
884 struct rtnl_link_stats64 temp;
885 const struct rtnl_link_stats64 *stats;
886 struct nlattr *attr, *af_spec;
887 struct rtnl_af_ops *af_ops;
890 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
894 ifm = nlmsg_data(nlh);
895 ifm->ifi_family = AF_UNSPEC;
897 ifm->ifi_type = dev->type;
898 ifm->ifi_index = dev->ifindex;
899 ifm->ifi_flags = dev_get_flags(dev);
900 ifm->ifi_change = change;
902 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
903 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
904 NLA_PUT_U8(skb, IFLA_OPERSTATE,
905 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
906 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
907 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
908 NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
910 if (dev->ifindex != dev->iflink)
911 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
914 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
917 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
920 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
923 struct rtnl_link_ifmap map = {
924 .mem_start = dev->mem_start,
925 .mem_end = dev->mem_end,
926 .base_addr = dev->base_addr,
929 .port = dev->if_port,
931 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
935 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
936 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
939 attr = nla_reserve(skb, IFLA_STATS,
940 sizeof(struct rtnl_link_stats));
942 goto nla_put_failure;
944 stats = dev_get_stats(dev, &temp);
945 copy_rtnl_link_stats(nla_data(attr), stats);
947 attr = nla_reserve(skb, IFLA_STATS64,
948 sizeof(struct rtnl_link_stats64));
950 goto nla_put_failure;
951 copy_rtnl_link_stats64(nla_data(attr), stats);
953 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
954 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
956 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
957 && (ext_filter_mask & RTEXT_FILTER_VF)) {
960 struct nlattr *vfinfo, *vf;
961 int num_vfs = dev_num_vf(dev->dev.parent);
963 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
965 goto nla_put_failure;
966 for (i = 0; i < num_vfs; i++) {
967 struct ifla_vf_info ivi;
968 struct ifla_vf_mac vf_mac;
969 struct ifla_vf_vlan vf_vlan;
970 struct ifla_vf_tx_rate vf_tx_rate;
971 struct ifla_vf_spoofchk vf_spoofchk;
974 * Not all SR-IOV capable drivers support the
975 * spoofcheck query. Preset to -1 so the user
976 * space tool can detect that the driver didn't
980 memset(ivi.mac, 0, sizeof(ivi.mac));
981 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
986 vf_spoofchk.vf = ivi.vf;
988 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
989 vf_vlan.vlan = ivi.vlan;
990 vf_vlan.qos = ivi.qos;
991 vf_tx_rate.rate = ivi.tx_rate;
992 vf_spoofchk.setting = ivi.spoofchk;
993 vf = nla_nest_start(skb, IFLA_VF_INFO);
995 nla_nest_cancel(skb, vfinfo);
996 goto nla_put_failure;
998 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
999 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
1000 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1002 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1004 nla_nest_end(skb, vf);
1006 nla_nest_end(skb, vfinfo);
1009 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1010 goto nla_put_failure;
1012 if (dev->rtnl_link_ops) {
1013 if (rtnl_link_fill(skb, dev) < 0)
1014 goto nla_put_failure;
1017 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1018 goto nla_put_failure;
1020 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1021 if (af_ops->fill_link_af) {
1025 if (!(af = nla_nest_start(skb, af_ops->family)))
1026 goto nla_put_failure;
1028 err = af_ops->fill_link_af(skb, dev);
1031 * Caller may return ENODATA to indicate that there
1032 * was no data to be dumped. This is not an error, it
1033 * means we should trim the attribute header and
1036 if (err == -ENODATA)
1037 nla_nest_cancel(skb, af);
1039 goto nla_put_failure;
1041 nla_nest_end(skb, af);
1045 nla_nest_end(skb, af_spec);
1047 return nlmsg_end(skb, nlh);
1050 nlmsg_cancel(skb, nlh);
1054 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1056 struct net *net = sock_net(skb->sk);
1059 struct net_device *dev;
1060 struct hlist_head *head;
1061 struct hlist_node *node;
1062 struct nlattr *tb[IFLA_MAX+1];
1063 u32 ext_filter_mask = 0;
1068 s_idx = cb->args[1];
1071 cb->seq = net->dev_base_seq;
1073 /* A hack to preserve kernel<->userspace interface.
1074 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1075 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1076 * what iproute2 < v3.9.0 used.
1077 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1078 * attribute, its netlink message is shorter than struct ifinfomsg.
1080 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1081 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1083 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1085 if (tb[IFLA_EXT_MASK])
1086 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1089 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1091 head = &net->dev_index_head[h];
1092 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1095 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1096 NETLINK_CB(cb->skb).pid,
1097 cb->nlh->nlmsg_seq, 0,
1100 /* If we ran out of room on the first message,
1103 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1108 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1121 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1122 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1123 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1124 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1125 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1126 [IFLA_MTU] = { .type = NLA_U32 },
1127 [IFLA_LINK] = { .type = NLA_U32 },
1128 [IFLA_MASTER] = { .type = NLA_U32 },
1129 [IFLA_TXQLEN] = { .type = NLA_U32 },
1130 [IFLA_WEIGHT] = { .type = NLA_U32 },
1131 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1132 [IFLA_LINKMODE] = { .type = NLA_U8 },
1133 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1134 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1135 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1136 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1137 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1138 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1139 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1140 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1141 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1143 EXPORT_SYMBOL(ifla_policy);
1145 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1146 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1147 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1150 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1151 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1154 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1155 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1156 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1157 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1158 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1161 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1162 [IFLA_PORT_VF] = { .type = NLA_U32 },
1163 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1164 .len = PORT_PROFILE_MAX },
1165 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1166 .len = sizeof(struct ifla_port_vsi)},
1167 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1168 .len = PORT_UUID_MAX },
1169 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1170 .len = PORT_UUID_MAX },
1171 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1172 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1175 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1178 /* Examine the link attributes and figure out which
1179 * network namespace we are talking about.
1181 if (tb[IFLA_NET_NS_PID])
1182 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1183 else if (tb[IFLA_NET_NS_FD])
1184 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1186 net = get_net(src_net);
1189 EXPORT_SYMBOL(rtnl_link_get_net);
1191 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1194 if (tb[IFLA_ADDRESS] &&
1195 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1198 if (tb[IFLA_BROADCAST] &&
1199 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1203 if (tb[IFLA_AF_SPEC]) {
1207 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1208 const struct rtnl_af_ops *af_ops;
1210 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1211 return -EAFNOSUPPORT;
1213 if (!af_ops->set_link_af)
1216 if (af_ops->validate_link_af) {
1217 err = af_ops->validate_link_af(dev, af);
1227 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1229 int rem, err = -EINVAL;
1231 const struct net_device_ops *ops = dev->netdev_ops;
1233 nla_for_each_nested(vf, attr, rem) {
1234 switch (nla_type(vf)) {
1236 struct ifla_vf_mac *ivm;
1239 if (ops->ndo_set_vf_mac)
1240 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1244 case IFLA_VF_VLAN: {
1245 struct ifla_vf_vlan *ivv;
1248 if (ops->ndo_set_vf_vlan)
1249 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1254 case IFLA_VF_TX_RATE: {
1255 struct ifla_vf_tx_rate *ivt;
1258 if (ops->ndo_set_vf_tx_rate)
1259 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1263 case IFLA_VF_SPOOFCHK: {
1264 struct ifla_vf_spoofchk *ivs;
1267 if (ops->ndo_set_vf_spoofchk)
1268 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1282 static int do_set_master(struct net_device *dev, int ifindex)
1284 struct net_device *master_dev;
1285 const struct net_device_ops *ops;
1289 if (dev->master->ifindex == ifindex)
1291 ops = dev->master->netdev_ops;
1292 if (ops->ndo_del_slave) {
1293 err = ops->ndo_del_slave(dev->master, dev);
1302 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1305 ops = master_dev->netdev_ops;
1306 if (ops->ndo_add_slave) {
1307 err = ops->ndo_add_slave(master_dev, dev);
1317 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1318 struct nlattr **tb, char *ifname, int modified)
1320 const struct net_device_ops *ops = dev->netdev_ops;
1321 int send_addr_notify = 0;
1324 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1325 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1330 err = dev_change_net_namespace(dev, net, ifname);
1338 struct rtnl_link_ifmap *u_map;
1341 if (!ops->ndo_set_config) {
1346 if (!netif_device_present(dev)) {
1351 u_map = nla_data(tb[IFLA_MAP]);
1352 k_map.mem_start = (unsigned long) u_map->mem_start;
1353 k_map.mem_end = (unsigned long) u_map->mem_end;
1354 k_map.base_addr = (unsigned short) u_map->base_addr;
1355 k_map.irq = (unsigned char) u_map->irq;
1356 k_map.dma = (unsigned char) u_map->dma;
1357 k_map.port = (unsigned char) u_map->port;
1359 err = ops->ndo_set_config(dev, &k_map);
1366 if (tb[IFLA_ADDRESS]) {
1367 struct sockaddr *sa;
1370 if (!ops->ndo_set_mac_address) {
1375 if (!netif_device_present(dev)) {
1380 len = sizeof(sa_family_t) + dev->addr_len;
1381 sa = kmalloc(len, GFP_KERNEL);
1386 sa->sa_family = dev->type;
1387 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1389 err = ops->ndo_set_mac_address(dev, sa);
1393 send_addr_notify = 1;
1395 add_device_randomness(dev->dev_addr, dev->addr_len);
1399 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1405 if (tb[IFLA_GROUP]) {
1406 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1411 * Interface selected by interface index but interface
1412 * name provided implies that a name change has been
1415 if (ifm->ifi_index > 0 && ifname[0]) {
1416 err = dev_change_name(dev, ifname);
1422 if (tb[IFLA_IFALIAS]) {
1423 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1424 nla_len(tb[IFLA_IFALIAS]));
1430 if (tb[IFLA_BROADCAST]) {
1431 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1432 send_addr_notify = 1;
1435 if (ifm->ifi_flags || ifm->ifi_change) {
1436 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1441 if (tb[IFLA_MASTER]) {
1442 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1448 if (tb[IFLA_TXQLEN])
1449 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1451 if (tb[IFLA_OPERSTATE])
1452 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1454 if (tb[IFLA_LINKMODE]) {
1455 write_lock_bh(&dev_base_lock);
1456 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1457 write_unlock_bh(&dev_base_lock);
1460 if (tb[IFLA_VFINFO_LIST]) {
1461 struct nlattr *attr;
1463 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1464 if (nla_type(attr) != IFLA_VF_INFO) {
1468 err = do_setvfinfo(dev, attr);
1476 if (tb[IFLA_VF_PORTS]) {
1477 struct nlattr *port[IFLA_PORT_MAX+1];
1478 struct nlattr *attr;
1483 if (!ops->ndo_set_vf_port)
1486 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1487 if (nla_type(attr) != IFLA_VF_PORT)
1489 err = nla_parse_nested(port, IFLA_PORT_MAX,
1490 attr, ifla_port_policy);
1493 if (!port[IFLA_PORT_VF]) {
1497 vf = nla_get_u32(port[IFLA_PORT_VF]);
1498 err = ops->ndo_set_vf_port(dev, vf, port);
1506 if (tb[IFLA_PORT_SELF]) {
1507 struct nlattr *port[IFLA_PORT_MAX+1];
1509 err = nla_parse_nested(port, IFLA_PORT_MAX,
1510 tb[IFLA_PORT_SELF], ifla_port_policy);
1515 if (ops->ndo_set_vf_port)
1516 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1522 if (tb[IFLA_AF_SPEC]) {
1526 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1527 const struct rtnl_af_ops *af_ops;
1529 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1532 err = af_ops->set_link_af(dev, af);
1542 if (err < 0 && modified && net_ratelimit())
1543 printk(KERN_WARNING "A link change request failed with "
1544 "some changes committed already. Interface %s may "
1545 "have been left with an inconsistent configuration, "
1546 "please check.\n", dev->name);
1548 if (send_addr_notify)
1549 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1553 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1555 struct net *net = sock_net(skb->sk);
1556 struct ifinfomsg *ifm;
1557 struct net_device *dev;
1559 struct nlattr *tb[IFLA_MAX+1];
1560 char ifname[IFNAMSIZ];
1562 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1566 if (tb[IFLA_IFNAME])
1567 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1572 ifm = nlmsg_data(nlh);
1573 if (ifm->ifi_index > 0)
1574 dev = __dev_get_by_index(net, ifm->ifi_index);
1575 else if (tb[IFLA_IFNAME])
1576 dev = __dev_get_by_name(net, ifname);
1585 err = validate_linkmsg(dev, tb);
1589 err = do_setlink(dev, ifm, tb, ifname, 0);
1594 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1596 struct net *net = sock_net(skb->sk);
1597 const struct rtnl_link_ops *ops;
1598 struct net_device *dev;
1599 struct ifinfomsg *ifm;
1600 char ifname[IFNAMSIZ];
1601 struct nlattr *tb[IFLA_MAX+1];
1603 LIST_HEAD(list_kill);
1605 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1609 if (tb[IFLA_IFNAME])
1610 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1612 ifm = nlmsg_data(nlh);
1613 if (ifm->ifi_index > 0)
1614 dev = __dev_get_by_index(net, ifm->ifi_index);
1615 else if (tb[IFLA_IFNAME])
1616 dev = __dev_get_by_name(net, ifname);
1623 ops = dev->rtnl_link_ops;
1627 ops->dellink(dev, &list_kill);
1628 unregister_netdevice_many(&list_kill);
1629 list_del(&list_kill);
1633 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1635 unsigned int old_flags;
1638 old_flags = dev->flags;
1639 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1640 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1645 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1646 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1648 __dev_notify_flags(dev, old_flags);
1651 EXPORT_SYMBOL(rtnl_configure_link);
1653 struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1654 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1657 struct net_device *dev;
1658 unsigned int num_queues = 1;
1659 unsigned int real_num_queues = 1;
1661 if (ops->get_tx_queues) {
1662 err = ops->get_tx_queues(src_net, tb, &num_queues,
1668 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1672 dev_net_set(dev, net);
1673 dev->rtnl_link_ops = ops;
1674 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1677 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1678 if (tb[IFLA_ADDRESS])
1679 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1680 nla_len(tb[IFLA_ADDRESS]));
1681 if (tb[IFLA_BROADCAST])
1682 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1683 nla_len(tb[IFLA_BROADCAST]));
1684 if (tb[IFLA_TXQLEN])
1685 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1686 if (tb[IFLA_OPERSTATE])
1687 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1688 if (tb[IFLA_LINKMODE])
1689 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1691 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1696 return ERR_PTR(err);
1698 EXPORT_SYMBOL(rtnl_create_link);
1700 static int rtnl_group_changelink(struct net *net, int group,
1701 struct ifinfomsg *ifm,
1704 struct net_device *dev, *aux;
1707 for_each_netdev_safe(net, dev, aux) {
1708 if (dev->group == group) {
1709 err = do_setlink(dev, ifm, tb, NULL, 0);
1718 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1720 struct net *net = sock_net(skb->sk);
1721 const struct rtnl_link_ops *ops;
1722 struct net_device *dev;
1723 struct ifinfomsg *ifm;
1724 char kind[MODULE_NAME_LEN];
1725 char ifname[IFNAMSIZ];
1726 struct nlattr *tb[IFLA_MAX+1];
1727 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1730 #ifdef CONFIG_MODULES
1733 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1737 if (tb[IFLA_IFNAME])
1738 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1742 ifm = nlmsg_data(nlh);
1743 if (ifm->ifi_index > 0)
1744 dev = __dev_get_by_index(net, ifm->ifi_index);
1747 dev = __dev_get_by_name(net, ifname);
1752 err = validate_linkmsg(dev, tb);
1756 if (tb[IFLA_LINKINFO]) {
1757 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1758 tb[IFLA_LINKINFO], ifla_info_policy);
1762 memset(linkinfo, 0, sizeof(linkinfo));
1764 if (linkinfo[IFLA_INFO_KIND]) {
1765 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1766 ops = rtnl_link_ops_get(kind);
1773 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1774 struct net *dest_net;
1777 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1778 err = nla_parse_nested(attr, ops->maxtype,
1779 linkinfo[IFLA_INFO_DATA],
1785 if (ops->validate) {
1786 err = ops->validate(tb, data);
1795 if (nlh->nlmsg_flags & NLM_F_EXCL)
1797 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1800 if (linkinfo[IFLA_INFO_DATA]) {
1801 if (!ops || ops != dev->rtnl_link_ops ||
1805 err = ops->changelink(dev, tb, data);
1811 return do_setlink(dev, ifm, tb, ifname, modified);
1814 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1815 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1816 return rtnl_group_changelink(net,
1817 nla_get_u32(tb[IFLA_GROUP]),
1824 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1828 #ifdef CONFIG_MODULES
1831 request_module("rtnl-link-%s", kind);
1833 ops = rtnl_link_ops_get(kind);
1842 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1844 dest_net = rtnl_link_get_net(net, tb);
1845 if (IS_ERR(dest_net))
1846 return PTR_ERR(dest_net);
1848 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
1852 else if (ops->newlink)
1853 err = ops->newlink(net, dev, tb, data);
1855 err = register_netdevice(dev);
1857 if (err < 0 && !IS_ERR(dev))
1862 err = rtnl_configure_link(dev, ifm);
1865 LIST_HEAD(list_kill);
1867 ops->dellink(dev, &list_kill);
1868 unregister_netdevice_many(&list_kill);
1870 unregister_netdevice(dev);
1879 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1881 struct net *net = sock_net(skb->sk);
1882 struct ifinfomsg *ifm;
1883 char ifname[IFNAMSIZ];
1884 struct nlattr *tb[IFLA_MAX+1];
1885 struct net_device *dev = NULL;
1886 struct sk_buff *nskb;
1888 u32 ext_filter_mask = 0;
1890 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1894 if (tb[IFLA_IFNAME])
1895 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1897 if (tb[IFLA_EXT_MASK])
1898 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1900 ifm = nlmsg_data(nlh);
1901 if (ifm->ifi_index > 0)
1902 dev = __dev_get_by_index(net, ifm->ifi_index);
1903 else if (tb[IFLA_IFNAME])
1904 dev = __dev_get_by_name(net, ifname);
1911 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1915 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1916 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1918 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1919 WARN_ON(err == -EMSGSIZE);
1922 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1927 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1929 struct net *net = sock_net(skb->sk);
1930 struct net_device *dev;
1931 struct nlattr *tb[IFLA_MAX+1];
1932 u32 ext_filter_mask = 0;
1933 u16 min_ifinfo_dump_size = 0;
1936 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
1937 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1938 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1940 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1941 if (tb[IFLA_EXT_MASK])
1942 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1945 if (!ext_filter_mask)
1946 return NLMSG_GOODSIZE;
1948 * traverse the list of net devices and compute the minimum
1949 * buffer size based upon the filter mask.
1951 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1952 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1957 return min_ifinfo_dump_size;
1960 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1963 int s_idx = cb->family;
1967 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1968 int type = cb->nlh->nlmsg_type-RTM_BASE;
1969 if (idx < s_idx || idx == PF_PACKET)
1971 if (rtnl_msg_handlers[idx] == NULL ||
1972 rtnl_msg_handlers[idx][type].dumpit == NULL)
1975 memset(&cb->args[0], 0, sizeof(cb->args));
1976 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1984 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1986 struct net *net = dev_net(dev);
1987 struct sk_buff *skb;
1989 size_t if_info_size;
1991 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1995 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1997 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1998 WARN_ON(err == -EMSGSIZE);
2002 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
2006 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2009 /* Protected by RTNL sempahore. */
2010 static struct rtattr **rta_buf;
2011 static int rtattr_max;
2013 /* Process one rtnetlink message. */
2015 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2017 struct net *net = sock_net(skb->sk);
2018 rtnl_doit_func doit;
2025 type = nlh->nlmsg_type;
2031 /* All the messages must have at least 1 byte length */
2032 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2035 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
2039 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
2042 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
2044 rtnl_dumpit_func dumpit;
2045 rtnl_calcit_func calcit;
2046 u16 min_dump_alloc = 0;
2048 dumpit = rtnl_get_dumpit(family, type);
2051 calcit = rtnl_get_calcit(family, type);
2053 min_dump_alloc = calcit(skb, nlh);
2057 err = netlink_dump_start(rtnl, skb, nlh, dumpit,
2058 NULL, min_dump_alloc);
2063 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2065 min_len = rtm_min[sz_idx];
2066 if (nlh->nlmsg_len < min_len)
2069 if (nlh->nlmsg_len > min_len) {
2070 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2071 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
2073 while (RTA_OK(attr, attrlen)) {
2074 unsigned int flavor = attr->rta_type & NLA_TYPE_MASK;
2076 if (flavor > rta_max[sz_idx])
2078 rta_buf[flavor-1] = attr;
2080 attr = RTA_NEXT(attr, attrlen);
2084 doit = rtnl_get_doit(family, type);
2088 return doit(skb, nlh, (void *)&rta_buf[0]);
2091 static void rtnetlink_rcv(struct sk_buff *skb)
2094 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2098 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2100 struct net_device *dev = ptr;
2106 case NETDEV_POST_INIT:
2107 case NETDEV_REGISTER:
2109 case NETDEV_PRE_TYPE_CHANGE:
2110 case NETDEV_GOING_DOWN:
2111 case NETDEV_UNREGISTER:
2112 case NETDEV_UNREGISTER_BATCH:
2113 case NETDEV_RELEASE:
2117 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2123 static struct notifier_block rtnetlink_dev_notifier = {
2124 .notifier_call = rtnetlink_event,
2128 static int __net_init rtnetlink_net_init(struct net *net)
2131 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
2132 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
2139 static void __net_exit rtnetlink_net_exit(struct net *net)
2141 netlink_kernel_release(net->rtnl);
2145 static struct pernet_operations rtnetlink_net_ops = {
2146 .init = rtnetlink_net_init,
2147 .exit = rtnetlink_net_exit,
2150 void __init rtnetlink_init(void)
2155 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2156 if (rta_max[i] > rtattr_max)
2157 rtattr_max = rta_max[i];
2158 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2160 panic("rtnetlink_init: cannot allocate rta_buf\n");
2162 if (register_pernet_subsys(&rtnetlink_net_ops))
2163 panic("rtnetlink_init: cannot initialize rtnetlink\n");
2165 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2166 register_netdevice_notifier(&rtnetlink_dev_notifier);
2168 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2169 rtnl_dump_ifinfo, rtnl_calcit);
2170 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2171 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2172 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2174 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2175 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);