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/pci.h>
40 #include <linux/etherdevice.h>
42 #include <asm/uaccess.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
47 #include <net/protocol.h>
49 #include <net/route.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
59 rtnl_dumpit_func dumpit;
60 rtnl_calcit_func calcit;
63 static DEFINE_MUTEX(rtnl_mutex);
67 mutex_lock(&rtnl_mutex);
69 EXPORT_SYMBOL(rtnl_lock);
71 void __rtnl_unlock(void)
73 mutex_unlock(&rtnl_mutex);
76 void rtnl_unlock(void)
78 /* This fellow will unlock it for us. */
81 EXPORT_SYMBOL(rtnl_unlock);
83 int rtnl_trylock(void)
85 return mutex_trylock(&rtnl_mutex);
87 EXPORT_SYMBOL(rtnl_trylock);
89 int rtnl_is_locked(void)
91 return mutex_is_locked(&rtnl_mutex);
93 EXPORT_SYMBOL(rtnl_is_locked);
95 #ifdef CONFIG_PROVE_LOCKING
96 int lockdep_rtnl_is_held(void)
98 return lockdep_is_held(&rtnl_mutex);
100 EXPORT_SYMBOL(lockdep_rtnl_is_held);
101 #endif /* #ifdef CONFIG_PROVE_LOCKING */
103 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
105 static inline int rtm_msgindex(int msgtype)
107 int msgindex = msgtype - RTM_BASE;
110 * msgindex < 0 implies someone tried to register a netlink
111 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112 * the message type has not been added to linux/rtnetlink.h
114 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
119 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
121 struct rtnl_link *tab;
123 if (protocol <= RTNL_FAMILY_MAX)
124 tab = rtnl_msg_handlers[protocol];
128 if (tab == NULL || tab[msgindex].doit == NULL)
129 tab = rtnl_msg_handlers[PF_UNSPEC];
131 return tab[msgindex].doit;
134 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
136 struct rtnl_link *tab;
138 if (protocol <= RTNL_FAMILY_MAX)
139 tab = rtnl_msg_handlers[protocol];
143 if (tab == NULL || tab[msgindex].dumpit == NULL)
144 tab = rtnl_msg_handlers[PF_UNSPEC];
146 return tab[msgindex].dumpit;
149 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
151 struct rtnl_link *tab;
153 if (protocol <= RTNL_FAMILY_MAX)
154 tab = rtnl_msg_handlers[protocol];
158 if (tab == NULL || tab[msgindex].calcit == NULL)
159 tab = rtnl_msg_handlers[PF_UNSPEC];
161 return tab[msgindex].calcit;
165 * __rtnl_register - Register a rtnetlink message type
166 * @protocol: Protocol family or PF_UNSPEC
167 * @msgtype: rtnetlink message type
168 * @doit: Function pointer called for each request message
169 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170 * @calcit: Function pointer to calc size of dump message
172 * Registers the specified function pointers (at least one of them has
173 * to be non-NULL) to be called whenever a request message for the
174 * specified protocol family and message type is received.
176 * The special protocol family PF_UNSPEC may be used to define fallback
177 * function pointers for the case when no entry for the specific protocol
180 * Returns 0 on success or a negative error code.
182 int __rtnl_register(int protocol, int msgtype,
183 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184 rtnl_calcit_func calcit)
186 struct rtnl_link *tab;
189 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190 msgindex = rtm_msgindex(msgtype);
192 tab = rtnl_msg_handlers[protocol];
194 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 rtnl_msg_handlers[protocol] = tab;
202 tab[msgindex].doit = doit;
205 tab[msgindex].dumpit = dumpit;
208 tab[msgindex].calcit = calcit;
212 EXPORT_SYMBOL_GPL(__rtnl_register);
215 * rtnl_register - Register a rtnetlink message type
217 * Identical to __rtnl_register() but panics on failure. This is useful
218 * as failure of this function is very unlikely, it can only happen due
219 * to lack of memory when allocating the chain to store all message
220 * handlers for a protocol. Meant for use in init functions where lack
221 * of memory implies no sense in continuing.
223 void rtnl_register(int protocol, int msgtype,
224 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225 rtnl_calcit_func calcit)
227 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228 panic("Unable to register rtnetlink message handler, "
229 "protocol = %d, message type = %d\n",
232 EXPORT_SYMBOL_GPL(rtnl_register);
235 * rtnl_unregister - Unregister a rtnetlink message type
236 * @protocol: Protocol family or PF_UNSPEC
237 * @msgtype: rtnetlink message type
239 * Returns 0 on success or a negative error code.
241 int rtnl_unregister(int protocol, int msgtype)
245 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246 msgindex = rtm_msgindex(msgtype);
248 if (rtnl_msg_handlers[protocol] == NULL)
251 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256 EXPORT_SYMBOL_GPL(rtnl_unregister);
259 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260 * @protocol : Protocol family or PF_UNSPEC
262 * Identical to calling rtnl_unregster() for all registered message types
263 * of a certain protocol family.
265 void rtnl_unregister_all(int protocol)
267 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
269 kfree(rtnl_msg_handlers[protocol]);
270 rtnl_msg_handlers[protocol] = NULL;
272 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
274 static LIST_HEAD(link_ops);
276 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
278 const struct rtnl_link_ops *ops;
280 list_for_each_entry(ops, &link_ops, list) {
281 if (!strcmp(ops->kind, kind))
288 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
289 * @ops: struct rtnl_link_ops * to register
291 * The caller must hold the rtnl_mutex. This function should be used
292 * by drivers that create devices during module initialization. It
293 * must be called before registering the devices.
295 * Returns 0 on success or a negative error code.
297 int __rtnl_link_register(struct rtnl_link_ops *ops)
299 if (rtnl_link_ops_get(ops->kind))
303 ops->dellink = unregister_netdevice_queue;
305 list_add_tail(&ops->list, &link_ops);
308 EXPORT_SYMBOL_GPL(__rtnl_link_register);
311 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
312 * @ops: struct rtnl_link_ops * to register
314 * Returns 0 on success or a negative error code.
316 int rtnl_link_register(struct rtnl_link_ops *ops)
321 err = __rtnl_link_register(ops);
325 EXPORT_SYMBOL_GPL(rtnl_link_register);
327 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
329 struct net_device *dev;
330 LIST_HEAD(list_kill);
332 for_each_netdev(net, dev) {
333 if (dev->rtnl_link_ops == ops)
334 ops->dellink(dev, &list_kill);
336 unregister_netdevice_many(&list_kill);
340 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
341 * @ops: struct rtnl_link_ops * to unregister
343 * The caller must hold the rtnl_mutex.
345 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
350 __rtnl_kill_links(net, ops);
352 list_del(&ops->list);
354 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
357 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
358 * @ops: struct rtnl_link_ops * to unregister
360 void rtnl_link_unregister(struct rtnl_link_ops *ops)
363 __rtnl_link_unregister(ops);
366 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
368 static size_t rtnl_link_get_size(const struct net_device *dev)
370 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
376 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
380 /* IFLA_INFO_DATA + nested data */
381 size += nla_total_size(sizeof(struct nlattr)) +
384 if (ops->get_xstats_size)
385 /* IFLA_INFO_XSTATS */
386 size += nla_total_size(ops->get_xstats_size(dev));
391 static LIST_HEAD(rtnl_af_ops);
393 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
395 const struct rtnl_af_ops *ops;
397 list_for_each_entry(ops, &rtnl_af_ops, list) {
398 if (ops->family == family)
406 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407 * @ops: struct rtnl_af_ops * to register
409 * The caller must hold the rtnl_mutex.
411 * Returns 0 on success or a negative error code.
413 int __rtnl_af_register(struct rtnl_af_ops *ops)
415 list_add_tail(&ops->list, &rtnl_af_ops);
418 EXPORT_SYMBOL_GPL(__rtnl_af_register);
421 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422 * @ops: struct rtnl_af_ops * to register
424 * Returns 0 on success or a negative error code.
426 int rtnl_af_register(struct rtnl_af_ops *ops)
431 err = __rtnl_af_register(ops);
435 EXPORT_SYMBOL_GPL(rtnl_af_register);
438 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439 * @ops: struct rtnl_af_ops * to unregister
441 * The caller must hold the rtnl_mutex.
443 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
445 list_del(&ops->list);
447 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
450 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451 * @ops: struct rtnl_af_ops * to unregister
453 void rtnl_af_unregister(struct rtnl_af_ops *ops)
456 __rtnl_af_unregister(ops);
459 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
461 static size_t rtnl_link_get_af_size(const struct net_device *dev)
463 struct rtnl_af_ops *af_ops;
467 size = nla_total_size(sizeof(struct nlattr));
469 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470 if (af_ops->get_link_af_size) {
471 /* AF_* + nested data */
472 size += nla_total_size(sizeof(struct nlattr)) +
473 af_ops->get_link_af_size(dev);
480 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
482 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
483 struct nlattr *linkinfo, *data;
486 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
487 if (linkinfo == NULL)
490 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
491 goto err_cancel_link;
492 if (ops->fill_xstats) {
493 err = ops->fill_xstats(skb, dev);
495 goto err_cancel_link;
497 if (ops->fill_info) {
498 data = nla_nest_start(skb, IFLA_INFO_DATA);
500 goto err_cancel_link;
501 err = ops->fill_info(skb, dev);
503 goto err_cancel_data;
504 nla_nest_end(skb, data);
507 nla_nest_end(skb, linkinfo);
511 nla_nest_cancel(skb, data);
513 nla_nest_cancel(skb, linkinfo);
518 static const int rtm_min[RTM_NR_FAMILIES] =
520 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
521 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
522 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
523 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
524 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
525 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
526 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
527 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
528 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
529 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
532 static const int rta_max[RTM_NR_FAMILIES] =
534 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
535 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
536 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
537 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
538 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
539 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
540 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
541 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
544 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
546 struct sock *rtnl = net->rtnl;
549 NETLINK_CB(skb).dst_group = group;
551 atomic_inc(&skb->users);
552 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
554 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
558 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
560 struct sock *rtnl = net->rtnl;
562 return nlmsg_unicast(rtnl, skb, pid);
564 EXPORT_SYMBOL(rtnl_unicast);
566 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
567 struct nlmsghdr *nlh, gfp_t flags)
569 struct sock *rtnl = net->rtnl;
573 report = nlmsg_report(nlh);
575 nlmsg_notify(rtnl, skb, pid, group, report, flags);
577 EXPORT_SYMBOL(rtnl_notify);
579 void rtnl_set_sk_err(struct net *net, u32 group, int error)
581 struct sock *rtnl = net->rtnl;
583 netlink_set_err(rtnl, 0, group, error);
585 EXPORT_SYMBOL(rtnl_set_sk_err);
587 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
592 mx = nla_nest_start(skb, RTA_METRICS);
596 for (i = 0; i < RTAX_MAX; i++) {
599 if (nla_put_u32(skb, i+1, metrics[i]))
600 goto nla_put_failure;
605 nla_nest_cancel(skb, mx);
609 return nla_nest_end(skb, mx);
612 nla_nest_cancel(skb, mx);
615 EXPORT_SYMBOL(rtnetlink_put_metrics);
617 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
618 long expires, u32 error)
620 struct rta_cacheinfo ci = {
621 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
622 .rta_used = dst->__use,
623 .rta_clntref = atomic_read(&(dst->__refcnt)),
631 clock = jiffies_to_clock_t(abs(expires));
632 clock = min_t(unsigned long, clock, INT_MAX);
633 ci.rta_expires = (expires > 0) ? clock : -clock;
635 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
637 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
639 static void set_operstate(struct net_device *dev, unsigned char transition)
641 unsigned char operstate = dev->operstate;
643 switch (transition) {
645 if ((operstate == IF_OPER_DORMANT ||
646 operstate == IF_OPER_UNKNOWN) &&
648 operstate = IF_OPER_UP;
651 case IF_OPER_DORMANT:
652 if (operstate == IF_OPER_UP ||
653 operstate == IF_OPER_UNKNOWN)
654 operstate = IF_OPER_DORMANT;
658 if (dev->operstate != operstate) {
659 write_lock_bh(&dev_base_lock);
660 dev->operstate = operstate;
661 write_unlock_bh(&dev_base_lock);
662 netdev_state_change(dev);
666 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
668 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
669 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
672 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
673 const struct ifinfomsg *ifm)
675 unsigned int flags = ifm->ifi_flags;
677 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
679 flags = (flags & ifm->ifi_change) |
680 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
685 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
686 const struct rtnl_link_stats64 *b)
688 a->rx_packets = b->rx_packets;
689 a->tx_packets = b->tx_packets;
690 a->rx_bytes = b->rx_bytes;
691 a->tx_bytes = b->tx_bytes;
692 a->rx_errors = b->rx_errors;
693 a->tx_errors = b->tx_errors;
694 a->rx_dropped = b->rx_dropped;
695 a->tx_dropped = b->tx_dropped;
697 a->multicast = b->multicast;
698 a->collisions = b->collisions;
700 a->rx_length_errors = b->rx_length_errors;
701 a->rx_over_errors = b->rx_over_errors;
702 a->rx_crc_errors = b->rx_crc_errors;
703 a->rx_frame_errors = b->rx_frame_errors;
704 a->rx_fifo_errors = b->rx_fifo_errors;
705 a->rx_missed_errors = b->rx_missed_errors;
707 a->tx_aborted_errors = b->tx_aborted_errors;
708 a->tx_carrier_errors = b->tx_carrier_errors;
709 a->tx_fifo_errors = b->tx_fifo_errors;
710 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
711 a->tx_window_errors = b->tx_window_errors;
713 a->rx_compressed = b->rx_compressed;
714 a->tx_compressed = b->tx_compressed;
717 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
719 memcpy(v, b, sizeof(*b));
723 static inline int rtnl_vfinfo_size(const struct net_device *dev,
726 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
727 (ext_filter_mask & RTEXT_FILTER_VF)) {
728 int num_vfs = dev_num_vf(dev->dev.parent);
729 size_t size = nla_total_size(sizeof(struct nlattr));
730 size += nla_total_size(num_vfs * sizeof(struct nlattr));
732 (nla_total_size(sizeof(struct ifla_vf_mac)) +
733 nla_total_size(sizeof(struct ifla_vf_vlan)) +
734 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
735 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
741 static size_t rtnl_port_size(const struct net_device *dev)
743 size_t port_size = nla_total_size(4) /* PORT_VF */
744 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
745 + nla_total_size(sizeof(struct ifla_port_vsi))
747 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
748 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
749 + nla_total_size(1) /* PROT_VDP_REQUEST */
750 + nla_total_size(2); /* PORT_VDP_RESPONSE */
751 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
752 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
754 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
757 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
759 if (dev_num_vf(dev->dev.parent))
760 return port_self_size + vf_ports_size +
761 vf_port_size * dev_num_vf(dev->dev.parent);
763 return port_self_size;
766 static noinline size_t if_nlmsg_size(const struct net_device *dev,
769 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
770 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
771 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
772 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
773 + nla_total_size(sizeof(struct rtnl_link_ifmap))
774 + nla_total_size(sizeof(struct rtnl_link_stats))
775 + nla_total_size(sizeof(struct rtnl_link_stats64))
776 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
777 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
778 + nla_total_size(4) /* IFLA_TXQLEN */
779 + nla_total_size(4) /* IFLA_WEIGHT */
780 + nla_total_size(4) /* IFLA_MTU */
781 + nla_total_size(4) /* IFLA_LINK */
782 + nla_total_size(4) /* IFLA_MASTER */
783 + nla_total_size(1) /* IFLA_CARRIER */
784 + nla_total_size(4) /* IFLA_PROMISCUITY */
785 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
786 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
787 + nla_total_size(1) /* IFLA_OPERSTATE */
788 + nla_total_size(1) /* IFLA_LINKMODE */
789 + nla_total_size(ext_filter_mask
790 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
791 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
792 + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
793 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
794 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
797 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
799 struct nlattr *vf_ports;
800 struct nlattr *vf_port;
804 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
808 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
809 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
811 goto nla_put_failure;
812 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
813 goto nla_put_failure;
814 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
815 if (err == -EMSGSIZE)
816 goto nla_put_failure;
818 nla_nest_cancel(skb, vf_port);
821 nla_nest_end(skb, vf_port);
824 nla_nest_end(skb, vf_ports);
829 nla_nest_cancel(skb, vf_ports);
833 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
835 struct nlattr *port_self;
838 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
842 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
844 nla_nest_cancel(skb, port_self);
845 return (err == -EMSGSIZE) ? err : 0;
848 nla_nest_end(skb, port_self);
853 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
857 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
860 err = rtnl_port_self_fill(skb, dev);
864 if (dev_num_vf(dev->dev.parent)) {
865 err = rtnl_vf_ports_fill(skb, dev);
873 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
874 int type, u32 pid, u32 seq, u32 change,
875 unsigned int flags, u32 ext_filter_mask)
877 struct ifinfomsg *ifm;
878 struct nlmsghdr *nlh;
879 struct rtnl_link_stats64 temp;
880 const struct rtnl_link_stats64 *stats;
881 struct nlattr *attr, *af_spec;
882 struct rtnl_af_ops *af_ops;
883 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
886 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
890 ifm = nlmsg_data(nlh);
891 ifm->ifi_family = AF_UNSPEC;
893 ifm->ifi_type = dev->type;
894 ifm->ifi_index = dev->ifindex;
895 ifm->ifi_flags = dev_get_flags(dev);
896 ifm->ifi_change = change;
898 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
899 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
900 nla_put_u8(skb, IFLA_OPERSTATE,
901 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
902 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
903 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
904 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
905 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
906 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
908 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
910 (dev->ifindex != dev->iflink &&
911 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
913 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
914 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
916 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
918 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
919 goto nla_put_failure;
922 struct rtnl_link_ifmap map = {
923 .mem_start = dev->mem_start,
924 .mem_end = dev->mem_end,
925 .base_addr = dev->base_addr,
928 .port = dev->if_port,
930 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
931 goto nla_put_failure;
935 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
936 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
937 goto nla_put_failure;
940 attr = nla_reserve(skb, IFLA_STATS,
941 sizeof(struct rtnl_link_stats));
943 goto nla_put_failure;
945 stats = dev_get_stats(dev, &temp);
946 copy_rtnl_link_stats(nla_data(attr), stats);
948 attr = nla_reserve(skb, IFLA_STATS64,
949 sizeof(struct rtnl_link_stats64));
951 goto nla_put_failure;
952 copy_rtnl_link_stats64(nla_data(attr), stats);
954 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
955 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
956 goto nla_put_failure;
958 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
959 && (ext_filter_mask & RTEXT_FILTER_VF)) {
962 struct nlattr *vfinfo, *vf;
963 int num_vfs = dev_num_vf(dev->dev.parent);
965 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
967 goto nla_put_failure;
968 for (i = 0; i < num_vfs; i++) {
969 struct ifla_vf_info ivi;
970 struct ifla_vf_mac vf_mac;
971 struct ifla_vf_vlan vf_vlan;
972 struct ifla_vf_tx_rate vf_tx_rate;
973 struct ifla_vf_spoofchk vf_spoofchk;
976 * Not all SR-IOV capable drivers support the
977 * spoofcheck query. Preset to -1 so the user
978 * space tool can detect that the driver didn't
982 memset(ivi.mac, 0, sizeof(ivi.mac));
983 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
988 vf_spoofchk.vf = ivi.vf;
990 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
991 vf_vlan.vlan = ivi.vlan;
992 vf_vlan.qos = ivi.qos;
993 vf_tx_rate.rate = ivi.tx_rate;
994 vf_spoofchk.setting = ivi.spoofchk;
995 vf = nla_nest_start(skb, IFLA_VF_INFO);
997 nla_nest_cancel(skb, vfinfo);
998 goto nla_put_failure;
1000 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1001 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1002 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1004 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1006 goto nla_put_failure;
1007 nla_nest_end(skb, vf);
1009 nla_nest_end(skb, vfinfo);
1012 if (rtnl_port_fill(skb, dev))
1013 goto nla_put_failure;
1015 if (dev->rtnl_link_ops) {
1016 if (rtnl_link_fill(skb, dev) < 0)
1017 goto nla_put_failure;
1020 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1021 goto nla_put_failure;
1023 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1024 if (af_ops->fill_link_af) {
1028 if (!(af = nla_nest_start(skb, af_ops->family)))
1029 goto nla_put_failure;
1031 err = af_ops->fill_link_af(skb, dev);
1034 * Caller may return ENODATA to indicate that there
1035 * was no data to be dumped. This is not an error, it
1036 * means we should trim the attribute header and
1039 if (err == -ENODATA)
1040 nla_nest_cancel(skb, af);
1042 goto nla_put_failure;
1044 nla_nest_end(skb, af);
1048 nla_nest_end(skb, af_spec);
1050 return nlmsg_end(skb, nlh);
1053 nlmsg_cancel(skb, nlh);
1057 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1059 struct net *net = sock_net(skb->sk);
1062 struct net_device *dev;
1063 struct hlist_head *head;
1064 struct nlattr *tb[IFLA_MAX+1];
1065 u32 ext_filter_mask = 0;
1068 s_idx = cb->args[1];
1071 cb->seq = net->dev_base_seq;
1073 if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1074 ifla_policy) >= 0) {
1076 if (tb[IFLA_EXT_MASK])
1077 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1080 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1082 head = &net->dev_index_head[h];
1083 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1086 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1087 NETLINK_CB(cb->skb).portid,
1088 cb->nlh->nlmsg_seq, 0,
1090 ext_filter_mask) <= 0)
1093 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1106 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1107 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1108 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1109 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1110 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1111 [IFLA_MTU] = { .type = NLA_U32 },
1112 [IFLA_LINK] = { .type = NLA_U32 },
1113 [IFLA_MASTER] = { .type = NLA_U32 },
1114 [IFLA_CARRIER] = { .type = NLA_U8 },
1115 [IFLA_TXQLEN] = { .type = NLA_U32 },
1116 [IFLA_WEIGHT] = { .type = NLA_U32 },
1117 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1118 [IFLA_LINKMODE] = { .type = NLA_U8 },
1119 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1120 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1121 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1122 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1123 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1124 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1125 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1126 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1127 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1128 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1129 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1130 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1132 EXPORT_SYMBOL(ifla_policy);
1134 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1135 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1136 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1139 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1140 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1143 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1144 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1145 .len = sizeof(struct ifla_vf_mac) },
1146 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1147 .len = sizeof(struct ifla_vf_vlan) },
1148 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1149 .len = sizeof(struct ifla_vf_tx_rate) },
1150 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1151 .len = sizeof(struct ifla_vf_spoofchk) },
1154 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1155 [IFLA_PORT_VF] = { .type = NLA_U32 },
1156 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1157 .len = PORT_PROFILE_MAX },
1158 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1159 .len = sizeof(struct ifla_port_vsi)},
1160 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1161 .len = PORT_UUID_MAX },
1162 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1163 .len = PORT_UUID_MAX },
1164 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1165 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1168 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1171 /* Examine the link attributes and figure out which
1172 * network namespace we are talking about.
1174 if (tb[IFLA_NET_NS_PID])
1175 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1176 else if (tb[IFLA_NET_NS_FD])
1177 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1179 net = get_net(src_net);
1182 EXPORT_SYMBOL(rtnl_link_get_net);
1184 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1187 if (tb[IFLA_ADDRESS] &&
1188 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1191 if (tb[IFLA_BROADCAST] &&
1192 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1196 if (tb[IFLA_AF_SPEC]) {
1200 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1201 const struct rtnl_af_ops *af_ops;
1203 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1204 return -EAFNOSUPPORT;
1206 if (!af_ops->set_link_af)
1209 if (af_ops->validate_link_af) {
1210 err = af_ops->validate_link_af(dev, af);
1220 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1222 int rem, err = -EINVAL;
1224 const struct net_device_ops *ops = dev->netdev_ops;
1226 nla_for_each_nested(vf, attr, rem) {
1227 switch (nla_type(vf)) {
1229 struct ifla_vf_mac *ivm;
1232 if (ops->ndo_set_vf_mac)
1233 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1237 case IFLA_VF_VLAN: {
1238 struct ifla_vf_vlan *ivv;
1241 if (ops->ndo_set_vf_vlan)
1242 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1247 case IFLA_VF_TX_RATE: {
1248 struct ifla_vf_tx_rate *ivt;
1251 if (ops->ndo_set_vf_tx_rate)
1252 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1256 case IFLA_VF_SPOOFCHK: {
1257 struct ifla_vf_spoofchk *ivs;
1260 if (ops->ndo_set_vf_spoofchk)
1261 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1275 static int do_set_master(struct net_device *dev, int ifindex)
1277 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1278 const struct net_device_ops *ops;
1282 if (upper_dev->ifindex == ifindex)
1284 ops = upper_dev->netdev_ops;
1285 if (ops->ndo_del_slave) {
1286 err = ops->ndo_del_slave(upper_dev, dev);
1295 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1298 ops = upper_dev->netdev_ops;
1299 if (ops->ndo_add_slave) {
1300 err = ops->ndo_add_slave(upper_dev, dev);
1310 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1311 struct nlattr **tb, char *ifname, int modified)
1313 const struct net_device_ops *ops = dev->netdev_ops;
1316 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1317 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1322 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1326 err = dev_change_net_namespace(dev, net, ifname);
1334 struct rtnl_link_ifmap *u_map;
1337 if (!ops->ndo_set_config) {
1342 if (!netif_device_present(dev)) {
1347 u_map = nla_data(tb[IFLA_MAP]);
1348 k_map.mem_start = (unsigned long) u_map->mem_start;
1349 k_map.mem_end = (unsigned long) u_map->mem_end;
1350 k_map.base_addr = (unsigned short) u_map->base_addr;
1351 k_map.irq = (unsigned char) u_map->irq;
1352 k_map.dma = (unsigned char) u_map->dma;
1353 k_map.port = (unsigned char) u_map->port;
1355 err = ops->ndo_set_config(dev, &k_map);
1362 if (tb[IFLA_ADDRESS]) {
1363 struct sockaddr *sa;
1366 len = sizeof(sa_family_t) + dev->addr_len;
1367 sa = kmalloc(len, GFP_KERNEL);
1372 sa->sa_family = dev->type;
1373 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1375 err = dev_set_mac_address(dev, sa);
1383 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1389 if (tb[IFLA_GROUP]) {
1390 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1395 * Interface selected by interface index but interface
1396 * name provided implies that a name change has been
1399 if (ifm->ifi_index > 0 && ifname[0]) {
1400 err = dev_change_name(dev, ifname);
1406 if (tb[IFLA_IFALIAS]) {
1407 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1408 nla_len(tb[IFLA_IFALIAS]));
1414 if (tb[IFLA_BROADCAST]) {
1415 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1416 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1419 if (ifm->ifi_flags || ifm->ifi_change) {
1420 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1425 if (tb[IFLA_MASTER]) {
1426 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1432 if (tb[IFLA_CARRIER]) {
1433 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1439 if (tb[IFLA_TXQLEN])
1440 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1442 if (tb[IFLA_OPERSTATE])
1443 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1445 if (tb[IFLA_LINKMODE]) {
1446 write_lock_bh(&dev_base_lock);
1447 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1448 write_unlock_bh(&dev_base_lock);
1451 if (tb[IFLA_VFINFO_LIST]) {
1452 struct nlattr *attr;
1454 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1455 if (nla_type(attr) != IFLA_VF_INFO) {
1459 err = do_setvfinfo(dev, attr);
1467 if (tb[IFLA_VF_PORTS]) {
1468 struct nlattr *port[IFLA_PORT_MAX+1];
1469 struct nlattr *attr;
1474 if (!ops->ndo_set_vf_port)
1477 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1478 if (nla_type(attr) != IFLA_VF_PORT)
1480 err = nla_parse_nested(port, IFLA_PORT_MAX,
1481 attr, ifla_port_policy);
1484 if (!port[IFLA_PORT_VF]) {
1488 vf = nla_get_u32(port[IFLA_PORT_VF]);
1489 err = ops->ndo_set_vf_port(dev, vf, port);
1497 if (tb[IFLA_PORT_SELF]) {
1498 struct nlattr *port[IFLA_PORT_MAX+1];
1500 err = nla_parse_nested(port, IFLA_PORT_MAX,
1501 tb[IFLA_PORT_SELF], ifla_port_policy);
1506 if (ops->ndo_set_vf_port)
1507 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1513 if (tb[IFLA_AF_SPEC]) {
1517 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1518 const struct rtnl_af_ops *af_ops;
1520 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1523 err = af_ops->set_link_af(dev, af);
1533 if (err < 0 && modified)
1534 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",
1540 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1542 struct net *net = sock_net(skb->sk);
1543 struct ifinfomsg *ifm;
1544 struct net_device *dev;
1546 struct nlattr *tb[IFLA_MAX+1];
1547 char ifname[IFNAMSIZ];
1549 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1553 if (tb[IFLA_IFNAME])
1554 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1559 ifm = nlmsg_data(nlh);
1560 if (ifm->ifi_index > 0)
1561 dev = __dev_get_by_index(net, ifm->ifi_index);
1562 else if (tb[IFLA_IFNAME])
1563 dev = __dev_get_by_name(net, ifname);
1572 err = validate_linkmsg(dev, tb);
1576 err = do_setlink(dev, ifm, tb, ifname, 0);
1581 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1583 struct net *net = sock_net(skb->sk);
1584 const struct rtnl_link_ops *ops;
1585 struct net_device *dev;
1586 struct ifinfomsg *ifm;
1587 char ifname[IFNAMSIZ];
1588 struct nlattr *tb[IFLA_MAX+1];
1590 LIST_HEAD(list_kill);
1592 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1596 if (tb[IFLA_IFNAME])
1597 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1599 ifm = nlmsg_data(nlh);
1600 if (ifm->ifi_index > 0)
1601 dev = __dev_get_by_index(net, ifm->ifi_index);
1602 else if (tb[IFLA_IFNAME])
1603 dev = __dev_get_by_name(net, ifname);
1610 ops = dev->rtnl_link_ops;
1614 ops->dellink(dev, &list_kill);
1615 unregister_netdevice_many(&list_kill);
1616 list_del(&list_kill);
1620 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1622 unsigned int old_flags;
1625 old_flags = dev->flags;
1626 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1627 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1632 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1633 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1635 __dev_notify_flags(dev, old_flags);
1638 EXPORT_SYMBOL(rtnl_configure_link);
1640 struct net_device *rtnl_create_link(struct net *net,
1641 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1644 struct net_device *dev;
1645 unsigned int num_tx_queues = 1;
1646 unsigned int num_rx_queues = 1;
1648 if (tb[IFLA_NUM_TX_QUEUES])
1649 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1650 else if (ops->get_num_tx_queues)
1651 num_tx_queues = ops->get_num_tx_queues();
1653 if (tb[IFLA_NUM_RX_QUEUES])
1654 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1655 else if (ops->get_num_rx_queues)
1656 num_rx_queues = ops->get_num_rx_queues();
1659 dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1660 num_tx_queues, num_rx_queues);
1664 dev_net_set(dev, net);
1665 dev->rtnl_link_ops = ops;
1666 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1669 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1670 if (tb[IFLA_ADDRESS]) {
1671 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1672 nla_len(tb[IFLA_ADDRESS]));
1673 dev->addr_assign_type = NET_ADDR_SET;
1675 if (tb[IFLA_BROADCAST])
1676 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1677 nla_len(tb[IFLA_BROADCAST]));
1678 if (tb[IFLA_TXQLEN])
1679 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1680 if (tb[IFLA_OPERSTATE])
1681 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1682 if (tb[IFLA_LINKMODE])
1683 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1685 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1690 return ERR_PTR(err);
1692 EXPORT_SYMBOL(rtnl_create_link);
1694 static int rtnl_group_changelink(struct net *net, int group,
1695 struct ifinfomsg *ifm,
1698 struct net_device *dev;
1701 for_each_netdev(net, dev) {
1702 if (dev->group == group) {
1703 err = do_setlink(dev, ifm, tb, NULL, 0);
1712 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1714 struct net *net = sock_net(skb->sk);
1715 const struct rtnl_link_ops *ops;
1716 struct net_device *dev;
1717 struct ifinfomsg *ifm;
1718 char kind[MODULE_NAME_LEN];
1719 char ifname[IFNAMSIZ];
1720 struct nlattr *tb[IFLA_MAX+1];
1721 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1724 #ifdef CONFIG_MODULES
1727 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1731 if (tb[IFLA_IFNAME])
1732 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1736 ifm = nlmsg_data(nlh);
1737 if (ifm->ifi_index > 0)
1738 dev = __dev_get_by_index(net, ifm->ifi_index);
1741 dev = __dev_get_by_name(net, ifname);
1746 err = validate_linkmsg(dev, tb);
1750 if (tb[IFLA_LINKINFO]) {
1751 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1752 tb[IFLA_LINKINFO], ifla_info_policy);
1756 memset(linkinfo, 0, sizeof(linkinfo));
1758 if (linkinfo[IFLA_INFO_KIND]) {
1759 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1760 ops = rtnl_link_ops_get(kind);
1767 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1768 struct net *dest_net;
1771 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1772 err = nla_parse_nested(attr, ops->maxtype,
1773 linkinfo[IFLA_INFO_DATA],
1779 if (ops->validate) {
1780 err = ops->validate(tb, data);
1789 if (nlh->nlmsg_flags & NLM_F_EXCL)
1791 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1794 if (linkinfo[IFLA_INFO_DATA]) {
1795 if (!ops || ops != dev->rtnl_link_ops ||
1799 err = ops->changelink(dev, tb, data);
1805 return do_setlink(dev, ifm, tb, ifname, modified);
1808 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1809 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1810 return rtnl_group_changelink(net,
1811 nla_get_u32(tb[IFLA_GROUP]),
1816 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1820 #ifdef CONFIG_MODULES
1823 request_module("rtnl-link-%s", kind);
1825 ops = rtnl_link_ops_get(kind);
1834 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1836 dest_net = rtnl_link_get_net(net, tb);
1837 if (IS_ERR(dest_net))
1838 return PTR_ERR(dest_net);
1840 dev = rtnl_create_link(dest_net, ifname, ops, tb);
1846 dev->ifindex = ifm->ifi_index;
1849 err = ops->newlink(net, dev, tb, data);
1851 err = register_netdevice(dev);
1853 if (err < 0 && !IS_ERR(dev))
1858 err = rtnl_configure_link(dev, ifm);
1860 unregister_netdevice(dev);
1867 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1869 struct net *net = sock_net(skb->sk);
1870 struct ifinfomsg *ifm;
1871 char ifname[IFNAMSIZ];
1872 struct nlattr *tb[IFLA_MAX+1];
1873 struct net_device *dev = NULL;
1874 struct sk_buff *nskb;
1876 u32 ext_filter_mask = 0;
1878 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1882 if (tb[IFLA_IFNAME])
1883 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1885 if (tb[IFLA_EXT_MASK])
1886 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1888 ifm = nlmsg_data(nlh);
1889 if (ifm->ifi_index > 0)
1890 dev = __dev_get_by_index(net, ifm->ifi_index);
1891 else if (tb[IFLA_IFNAME])
1892 dev = __dev_get_by_name(net, ifname);
1899 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1903 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
1904 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1906 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1907 WARN_ON(err == -EMSGSIZE);
1910 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
1915 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1917 struct net *net = sock_net(skb->sk);
1918 struct net_device *dev;
1919 struct nlattr *tb[IFLA_MAX+1];
1920 u32 ext_filter_mask = 0;
1921 u16 min_ifinfo_dump_size = 0;
1923 if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1924 ifla_policy) >= 0) {
1925 if (tb[IFLA_EXT_MASK])
1926 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1929 if (!ext_filter_mask)
1930 return NLMSG_GOODSIZE;
1932 * traverse the list of net devices and compute the minimum
1933 * buffer size based upon the filter mask.
1935 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1936 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1941 return min_ifinfo_dump_size;
1944 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1947 int s_idx = cb->family;
1951 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1952 int type = cb->nlh->nlmsg_type-RTM_BASE;
1953 if (idx < s_idx || idx == PF_PACKET)
1955 if (rtnl_msg_handlers[idx] == NULL ||
1956 rtnl_msg_handlers[idx][type].dumpit == NULL)
1959 memset(&cb->args[0], 0, sizeof(cb->args));
1960 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1968 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
1970 struct net *net = dev_net(dev);
1971 struct sk_buff *skb;
1973 size_t if_info_size;
1975 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1979 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1981 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1982 WARN_ON(err == -EMSGSIZE);
1986 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1990 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1992 EXPORT_SYMBOL(rtmsg_ifinfo);
1994 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
1995 struct net_device *dev,
1996 u8 *addr, u32 pid, u32 seq,
1997 int type, unsigned int flags)
1999 struct nlmsghdr *nlh;
2002 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
2006 ndm = nlmsg_data(nlh);
2007 ndm->ndm_family = AF_BRIDGE;
2010 ndm->ndm_flags = flags;
2012 ndm->ndm_ifindex = dev->ifindex;
2013 ndm->ndm_state = NUD_PERMANENT;
2015 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2016 goto nla_put_failure;
2018 return nlmsg_end(skb, nlh);
2021 nlmsg_cancel(skb, nlh);
2025 static inline size_t rtnl_fdb_nlmsg_size(void)
2027 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2030 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2032 struct net *net = dev_net(dev);
2033 struct sk_buff *skb;
2036 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2040 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
2046 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2049 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2053 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2055 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2056 struct nlattr *tb[],
2057 struct net_device *dev,
2058 const unsigned char *addr,
2063 /* If aging addresses are supported device will need to
2064 * implement its own handler for this.
2066 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2067 pr_info("%s: FDB only supports static addresses\n", dev->name);
2071 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2072 err = dev_uc_add_excl(dev, addr);
2073 else if (is_multicast_ether_addr(addr))
2074 err = dev_mc_add_excl(dev, addr);
2076 /* Only return duplicate errors if NLM_F_EXCL is set */
2077 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2082 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2084 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2086 struct net *net = sock_net(skb->sk);
2088 struct nlattr *tb[NDA_MAX+1];
2089 struct net_device *dev;
2093 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2097 ndm = nlmsg_data(nlh);
2098 if (ndm->ndm_ifindex == 0) {
2099 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2103 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2105 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2109 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2110 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2114 addr = nla_data(tb[NDA_LLADDR]);
2115 if (is_zero_ether_addr(addr)) {
2116 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
2122 /* Support fdb on master device the net/bridge default case */
2123 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2124 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2125 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2126 const struct net_device_ops *ops = br_dev->netdev_ops;
2128 err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
2132 ndm->ndm_flags &= ~NTF_MASTER;
2135 /* Embedded bridge, macvlan, and any other device support */
2136 if ((ndm->ndm_flags & NTF_SELF)) {
2137 if (dev->netdev_ops->ndo_fdb_add)
2138 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2141 err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
2145 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
2146 ndm->ndm_flags &= ~NTF_SELF;
2154 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2156 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2157 struct nlattr *tb[],
2158 struct net_device *dev,
2159 const unsigned char *addr)
2161 int err = -EOPNOTSUPP;
2163 /* If aging addresses are supported device will need to
2164 * implement its own handler for this.
2166 if (ndm->ndm_state & NUD_PERMANENT) {
2167 pr_info("%s: FDB only supports static addresses\n", dev->name);
2171 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2172 err = dev_uc_del(dev, addr);
2173 else if (is_multicast_ether_addr(addr))
2174 err = dev_mc_del(dev, addr);
2180 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2182 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2184 struct net *net = sock_net(skb->sk);
2186 struct nlattr *tb[NDA_MAX+1];
2187 struct net_device *dev;
2191 if (!capable(CAP_NET_ADMIN))
2194 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2198 ndm = nlmsg_data(nlh);
2199 if (ndm->ndm_ifindex == 0) {
2200 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2204 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2206 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2210 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2211 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2215 addr = nla_data(tb[NDA_LLADDR]);
2216 if (!is_valid_ether_addr(addr)) {
2217 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ether address\n");
2223 /* Support fdb on master device the net/bridge default case */
2224 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2225 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2226 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2227 const struct net_device_ops *ops = br_dev->netdev_ops;
2229 if (ops->ndo_fdb_del)
2230 err = ops->ndo_fdb_del(ndm, tb, dev, addr);
2235 ndm->ndm_flags &= ~NTF_MASTER;
2238 /* Embedded bridge, macvlan, and any other device support */
2239 if (ndm->ndm_flags & NTF_SELF) {
2240 if (dev->netdev_ops->ndo_fdb_del)
2241 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2243 err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
2246 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
2247 ndm->ndm_flags &= ~NTF_SELF;
2254 static int nlmsg_populate_fdb(struct sk_buff *skb,
2255 struct netlink_callback *cb,
2256 struct net_device *dev,
2258 struct netdev_hw_addr_list *list)
2260 struct netdev_hw_addr *ha;
2264 portid = NETLINK_CB(cb->skb).portid;
2265 seq = cb->nlh->nlmsg_seq;
2267 list_for_each_entry(ha, &list->list, list) {
2268 if (*idx < cb->args[0])
2271 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2273 RTM_NEWNEIGH, NTF_SELF);
2283 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2284 * @nlh: netlink message header
2287 * Default netdevice operation to dump the existing unicast address list.
2288 * Returns zero on success.
2290 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2291 struct netlink_callback *cb,
2292 struct net_device *dev,
2297 netif_addr_lock_bh(dev);
2298 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2301 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2303 netif_addr_unlock_bh(dev);
2306 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2308 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2311 struct net *net = sock_net(skb->sk);
2312 struct net_device *dev;
2315 for_each_netdev_rcu(net, dev) {
2316 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2317 struct net_device *br_dev;
2318 const struct net_device_ops *ops;
2320 br_dev = netdev_master_upper_dev_get(dev);
2321 ops = br_dev->netdev_ops;
2322 if (ops->ndo_fdb_dump)
2323 idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
2326 if (dev->netdev_ops->ndo_fdb_dump)
2327 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2329 ndo_dflt_fdb_dump(skb, cb, dev, idx);
2337 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2338 struct net_device *dev, u16 mode)
2340 struct nlmsghdr *nlh;
2341 struct ifinfomsg *ifm;
2342 struct nlattr *br_afspec;
2343 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2344 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2346 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2350 ifm = nlmsg_data(nlh);
2351 ifm->ifi_family = AF_BRIDGE;
2353 ifm->ifi_type = dev->type;
2354 ifm->ifi_index = dev->ifindex;
2355 ifm->ifi_flags = dev_get_flags(dev);
2356 ifm->ifi_change = 0;
2359 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2360 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2361 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2363 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2365 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2366 (dev->ifindex != dev->iflink &&
2367 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2368 goto nla_put_failure;
2370 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2372 goto nla_put_failure;
2374 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2375 nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2376 nla_nest_cancel(skb, br_afspec);
2377 goto nla_put_failure;
2379 nla_nest_end(skb, br_afspec);
2381 return nlmsg_end(skb, nlh);
2383 nlmsg_cancel(skb, nlh);
2386 EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2388 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2390 struct net *net = sock_net(skb->sk);
2391 struct net_device *dev;
2393 u32 portid = NETLINK_CB(cb->skb).portid;
2394 u32 seq = cb->nlh->nlmsg_seq;
2395 struct nlattr *extfilt;
2396 u32 filter_mask = 0;
2398 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct rtgenmsg),
2401 filter_mask = nla_get_u32(extfilt);
2404 for_each_netdev_rcu(net, dev) {
2405 const struct net_device_ops *ops = dev->netdev_ops;
2406 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2408 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2409 if (idx >= cb->args[0] &&
2410 br_dev->netdev_ops->ndo_bridge_getlink(
2411 skb, portid, seq, dev, filter_mask) < 0)
2416 if (ops->ndo_bridge_getlink) {
2417 if (idx >= cb->args[0] &&
2418 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2430 static inline size_t bridge_nlmsg_size(void)
2432 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2433 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2434 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2435 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2436 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2437 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2438 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2439 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2440 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2441 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2442 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2445 static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2447 struct net *net = dev_net(dev);
2448 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2449 struct sk_buff *skb;
2450 int err = -EOPNOTSUPP;
2452 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2458 if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2459 br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2460 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2465 if ((flags & BRIDGE_FLAGS_SELF) &&
2466 dev->netdev_ops->ndo_bridge_getlink) {
2467 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2472 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2475 WARN_ON(err == -EMSGSIZE);
2477 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2481 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2484 struct net *net = sock_net(skb->sk);
2485 struct ifinfomsg *ifm;
2486 struct net_device *dev;
2487 struct nlattr *br_spec, *attr = NULL;
2488 int rem, err = -EOPNOTSUPP;
2489 u16 oflags, flags = 0;
2490 bool have_flags = false;
2492 if (nlmsg_len(nlh) < sizeof(*ifm))
2495 ifm = nlmsg_data(nlh);
2496 if (ifm->ifi_family != AF_BRIDGE)
2497 return -EPFNOSUPPORT;
2499 dev = __dev_get_by_index(net, ifm->ifi_index);
2501 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2505 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2507 nla_for_each_nested(attr, br_spec, rem) {
2508 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2510 flags = nla_get_u16(attr);
2518 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2519 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2521 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2526 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2530 flags &= ~BRIDGE_FLAGS_MASTER;
2533 if ((flags & BRIDGE_FLAGS_SELF)) {
2534 if (!dev->netdev_ops->ndo_bridge_setlink)
2537 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2540 flags &= ~BRIDGE_FLAGS_SELF;
2544 memcpy(nla_data(attr), &flags, sizeof(flags));
2545 /* Generate event to notify upper layer of bridge change */
2547 err = rtnl_bridge_notify(dev, oflags);
2552 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2555 struct net *net = sock_net(skb->sk);
2556 struct ifinfomsg *ifm;
2557 struct net_device *dev;
2558 struct nlattr *br_spec, *attr = NULL;
2559 int rem, err = -EOPNOTSUPP;
2560 u16 oflags, flags = 0;
2561 bool have_flags = false;
2563 if (nlmsg_len(nlh) < sizeof(*ifm))
2566 ifm = nlmsg_data(nlh);
2567 if (ifm->ifi_family != AF_BRIDGE)
2568 return -EPFNOSUPPORT;
2570 dev = __dev_get_by_index(net, ifm->ifi_index);
2572 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2576 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2578 nla_for_each_nested(attr, br_spec, rem) {
2579 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2581 flags = nla_get_u16(attr);
2589 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2590 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2592 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2597 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2601 flags &= ~BRIDGE_FLAGS_MASTER;
2604 if ((flags & BRIDGE_FLAGS_SELF)) {
2605 if (!dev->netdev_ops->ndo_bridge_dellink)
2608 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2611 flags &= ~BRIDGE_FLAGS_SELF;
2615 memcpy(nla_data(attr), &flags, sizeof(flags));
2616 /* Generate event to notify upper layer of bridge change */
2618 err = rtnl_bridge_notify(dev, oflags);
2623 /* Protected by RTNL sempahore. */
2624 static struct rtattr **rta_buf;
2625 static int rtattr_max;
2627 /* Process one rtnetlink message. */
2629 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2631 struct net *net = sock_net(skb->sk);
2632 rtnl_doit_func doit;
2639 type = nlh->nlmsg_type;
2645 /* All the messages must have at least 1 byte length */
2646 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2649 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
2653 if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
2656 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
2658 rtnl_dumpit_func dumpit;
2659 rtnl_calcit_func calcit;
2660 u16 min_dump_alloc = 0;
2662 dumpit = rtnl_get_dumpit(family, type);
2665 calcit = rtnl_get_calcit(family, type);
2667 min_dump_alloc = calcit(skb, nlh);
2672 struct netlink_dump_control c = {
2674 .min_dump_alloc = min_dump_alloc,
2676 err = netlink_dump_start(rtnl, skb, nlh, &c);
2682 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2684 min_len = rtm_min[sz_idx];
2685 if (nlh->nlmsg_len < min_len)
2688 if (nlh->nlmsg_len > min_len) {
2689 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2690 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
2692 while (RTA_OK(attr, attrlen)) {
2693 unsigned int flavor = attr->rta_type;
2695 if (flavor > rta_max[sz_idx])
2697 rta_buf[flavor-1] = attr;
2699 attr = RTA_NEXT(attr, attrlen);
2703 doit = rtnl_get_doit(family, type);
2707 return doit(skb, nlh, (void *)&rta_buf[0]);
2710 static void rtnetlink_rcv(struct sk_buff *skb)
2713 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2717 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2719 struct net_device *dev = ptr;
2725 case NETDEV_POST_INIT:
2726 case NETDEV_REGISTER:
2728 case NETDEV_PRE_TYPE_CHANGE:
2729 case NETDEV_GOING_DOWN:
2730 case NETDEV_UNREGISTER:
2731 case NETDEV_UNREGISTER_FINAL:
2732 case NETDEV_RELEASE:
2736 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2742 static struct notifier_block rtnetlink_dev_notifier = {
2743 .notifier_call = rtnetlink_event,
2747 static int __net_init rtnetlink_net_init(struct net *net)
2750 struct netlink_kernel_cfg cfg = {
2751 .groups = RTNLGRP_MAX,
2752 .input = rtnetlink_rcv,
2753 .cb_mutex = &rtnl_mutex,
2754 .flags = NL_CFG_F_NONROOT_RECV,
2757 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
2764 static void __net_exit rtnetlink_net_exit(struct net *net)
2766 netlink_kernel_release(net->rtnl);
2770 static struct pernet_operations rtnetlink_net_ops = {
2771 .init = rtnetlink_net_init,
2772 .exit = rtnetlink_net_exit,
2775 void __init rtnetlink_init(void)
2780 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2781 if (rta_max[i] > rtattr_max)
2782 rtattr_max = rta_max[i];
2783 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2785 panic("rtnetlink_init: cannot allocate rta_buf\n");
2787 if (register_pernet_subsys(&rtnetlink_net_ops))
2788 panic("rtnetlink_init: cannot initialize rtnetlink\n");
2790 register_netdevice_notifier(&rtnetlink_dev_notifier);
2792 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2793 rtnl_dump_ifinfo, rtnl_calcit);
2794 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2795 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2796 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2798 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2799 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
2801 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
2802 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
2803 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2805 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2806 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2807 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);