Linux 4.1.16
[linux/fpc-iii.git] / net / core / rtnetlink.c
blobfe95cb704aaa0a08f358032ee88ca9c39b8e1331
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/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>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
43 #include <asm/uaccess.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <net/udp.h>
53 #include <net/tcp.h>
54 #include <net/sock.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
60 struct rtnl_link {
61 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
63 rtnl_calcit_func calcit;
66 static DEFINE_MUTEX(rtnl_mutex);
68 void rtnl_lock(void)
70 mutex_lock(&rtnl_mutex);
72 EXPORT_SYMBOL(rtnl_lock);
74 void __rtnl_unlock(void)
76 mutex_unlock(&rtnl_mutex);
79 void rtnl_unlock(void)
81 /* This fellow will unlock it for us. */
82 netdev_run_todo();
84 EXPORT_SYMBOL(rtnl_unlock);
86 int rtnl_trylock(void)
88 return mutex_trylock(&rtnl_mutex);
90 EXPORT_SYMBOL(rtnl_trylock);
92 int rtnl_is_locked(void)
94 return mutex_is_locked(&rtnl_mutex);
96 EXPORT_SYMBOL(rtnl_is_locked);
98 #ifdef CONFIG_PROVE_LOCKING
99 int lockdep_rtnl_is_held(void)
101 return lockdep_is_held(&rtnl_mutex);
103 EXPORT_SYMBOL(lockdep_rtnl_is_held);
104 #endif /* #ifdef CONFIG_PROVE_LOCKING */
106 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
108 static inline int rtm_msgindex(int msgtype)
110 int msgindex = msgtype - RTM_BASE;
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
119 return msgindex;
122 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
124 struct rtnl_link *tab;
126 if (protocol <= RTNL_FAMILY_MAX)
127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
131 if (tab == NULL || tab[msgindex].doit == NULL)
132 tab = rtnl_msg_handlers[PF_UNSPEC];
134 return tab[msgindex].doit;
137 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
139 struct rtnl_link *tab;
141 if (protocol <= RTNL_FAMILY_MAX)
142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
146 if (tab == NULL || tab[msgindex].dumpit == NULL)
147 tab = rtnl_msg_handlers[PF_UNSPEC];
149 return tab[msgindex].dumpit;
152 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
154 struct rtnl_link *tab;
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
164 return tab[msgindex].calcit;
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
173 * @calcit: Function pointer to calc size of dump message
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
183 * Returns 0 on success or a negative error code.
185 int __rtnl_register(int protocol, int msgtype,
186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
189 struct rtnl_link *tab;
190 int msgindex;
192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
193 msgindex = rtm_msgindex(msgtype);
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
201 rtnl_msg_handlers[protocol] = tab;
204 if (doit)
205 tab[msgindex].doit = doit;
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
210 if (calcit)
211 tab[msgindex].calcit = calcit;
213 return 0;
215 EXPORT_SYMBOL_GPL(__rtnl_register);
218 * rtnl_register - Register a rtnetlink message type
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
224 * of memory implies no sense in continuing.
226 void rtnl_register(int protocol, int msgtype,
227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
235 EXPORT_SYMBOL_GPL(rtnl_register);
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
242 * Returns 0 on success or a negative error code.
244 int rtnl_unregister(int protocol, int msgtype)
246 int msgindex;
248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
249 msgindex = rtm_msgindex(msgtype);
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
257 return 0;
259 EXPORT_SYMBOL_GPL(rtnl_unregister);
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
268 void rtnl_unregister_all(int protocol)
270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
275 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
277 static LIST_HEAD(link_ops);
279 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
281 const struct rtnl_link_ops *ops;
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
287 return NULL;
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
298 * Returns 0 on success or a negative error code.
300 int __rtnl_link_register(struct rtnl_link_ops *ops)
302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
310 if (ops->setup && !ops->dellink)
311 ops->dellink = unregister_netdevice_queue;
313 list_add_tail(&ops->list, &link_ops);
314 return 0;
316 EXPORT_SYMBOL_GPL(__rtnl_link_register);
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
322 * Returns 0 on success or a negative error code.
324 int rtnl_link_register(struct rtnl_link_ops *ops)
326 int err;
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
333 EXPORT_SYMBOL_GPL(rtnl_link_register);
335 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
337 struct net_device *dev;
338 LIST_HEAD(list_kill);
340 for_each_netdev(net, dev) {
341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
344 unregister_netdevice_many(&list_kill);
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
351 * The caller must hold the rtnl_mutex.
353 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
355 struct net *net;
357 for_each_net(net) {
358 __rtnl_kill_links(net, ops);
360 list_del(&ops->list);
362 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
364 /* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
367 static void rtnl_lock_unregistering_all(void)
369 struct net *net;
370 bool unregistering;
371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
373 add_wait_queue(&netdev_unregistering_wq, &wait);
374 for (;;) {
375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
389 remove_wait_queue(&netdev_unregistering_wq, &wait);
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
396 void rtnl_link_unregister(struct rtnl_link_ops *ops)
398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
403 mutex_unlock(&net_mutex);
405 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
407 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
416 if (!ops || !ops->get_slave_size)
417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
423 static size_t rtnl_link_get_size(const struct net_device *dev)
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
428 if (!ops)
429 return 0;
431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
436 size += nla_total_size(sizeof(struct nlattr)) +
437 ops->get_size(dev);
439 if (ops->get_xstats_size)
440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
443 size += rtnl_link_get_slave_info_data_size(dev);
445 return size;
448 static LIST_HEAD(rtnl_af_ops);
450 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
452 const struct rtnl_af_ops *ops;
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
459 return NULL;
463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
466 * Returns 0 on success or a negative error code.
468 void rtnl_af_register(struct rtnl_af_ops *ops)
470 rtnl_lock();
471 list_add_tail(&ops->list, &rtnl_af_ops);
472 rtnl_unlock();
474 EXPORT_SYMBOL_GPL(rtnl_af_register);
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
480 * The caller must hold the rtnl_mutex.
482 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
484 list_del(&ops->list);
486 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
492 void rtnl_af_unregister(struct rtnl_af_ops *ops)
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
498 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
500 static size_t rtnl_link_get_af_size(const struct net_device *dev)
502 struct rtnl_af_ops *af_ops;
503 size_t size;
505 /* IFLA_AF_SPEC */
506 size = nla_total_size(sizeof(struct nlattr));
508 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
509 if (af_ops->get_link_af_size) {
510 /* AF_* + nested data */
511 size += nla_total_size(sizeof(struct nlattr)) +
512 af_ops->get_link_af_size(dev);
516 return size;
519 static bool rtnl_have_link_slave_info(const struct net_device *dev)
521 struct net_device *master_dev;
523 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
524 if (master_dev && master_dev->rtnl_link_ops)
525 return true;
526 return false;
529 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
530 const struct net_device *dev)
532 struct net_device *master_dev;
533 const struct rtnl_link_ops *ops;
534 struct nlattr *slave_data;
535 int err;
537 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
538 if (!master_dev)
539 return 0;
540 ops = master_dev->rtnl_link_ops;
541 if (!ops)
542 return 0;
543 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
544 return -EMSGSIZE;
545 if (ops->fill_slave_info) {
546 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
547 if (!slave_data)
548 return -EMSGSIZE;
549 err = ops->fill_slave_info(skb, master_dev, dev);
550 if (err < 0)
551 goto err_cancel_slave_data;
552 nla_nest_end(skb, slave_data);
554 return 0;
556 err_cancel_slave_data:
557 nla_nest_cancel(skb, slave_data);
558 return err;
561 static int rtnl_link_info_fill(struct sk_buff *skb,
562 const struct net_device *dev)
564 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
565 struct nlattr *data;
566 int err;
568 if (!ops)
569 return 0;
570 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
571 return -EMSGSIZE;
572 if (ops->fill_xstats) {
573 err = ops->fill_xstats(skb, dev);
574 if (err < 0)
575 return err;
577 if (ops->fill_info) {
578 data = nla_nest_start(skb, IFLA_INFO_DATA);
579 if (data == NULL)
580 return -EMSGSIZE;
581 err = ops->fill_info(skb, dev);
582 if (err < 0)
583 goto err_cancel_data;
584 nla_nest_end(skb, data);
586 return 0;
588 err_cancel_data:
589 nla_nest_cancel(skb, data);
590 return err;
593 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
595 struct nlattr *linkinfo;
596 int err = -EMSGSIZE;
598 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
599 if (linkinfo == NULL)
600 goto out;
602 err = rtnl_link_info_fill(skb, dev);
603 if (err < 0)
604 goto err_cancel_link;
606 err = rtnl_link_slave_info_fill(skb, dev);
607 if (err < 0)
608 goto err_cancel_link;
610 nla_nest_end(skb, linkinfo);
611 return 0;
613 err_cancel_link:
614 nla_nest_cancel(skb, linkinfo);
615 out:
616 return err;
619 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
621 struct sock *rtnl = net->rtnl;
622 int err = 0;
624 NETLINK_CB(skb).dst_group = group;
625 if (echo)
626 atomic_inc(&skb->users);
627 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
628 if (echo)
629 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
630 return err;
633 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
635 struct sock *rtnl = net->rtnl;
637 return nlmsg_unicast(rtnl, skb, pid);
639 EXPORT_SYMBOL(rtnl_unicast);
641 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
642 struct nlmsghdr *nlh, gfp_t flags)
644 struct sock *rtnl = net->rtnl;
645 int report = 0;
647 if (nlh)
648 report = nlmsg_report(nlh);
650 nlmsg_notify(rtnl, skb, pid, group, report, flags);
652 EXPORT_SYMBOL(rtnl_notify);
654 void rtnl_set_sk_err(struct net *net, u32 group, int error)
656 struct sock *rtnl = net->rtnl;
658 netlink_set_err(rtnl, 0, group, error);
660 EXPORT_SYMBOL(rtnl_set_sk_err);
662 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
664 struct nlattr *mx;
665 int i, valid = 0;
667 mx = nla_nest_start(skb, RTA_METRICS);
668 if (mx == NULL)
669 return -ENOBUFS;
671 for (i = 0; i < RTAX_MAX; i++) {
672 if (metrics[i]) {
673 if (i == RTAX_CC_ALGO - 1) {
674 char tmp[TCP_CA_NAME_MAX], *name;
676 name = tcp_ca_get_name_by_key(metrics[i], tmp);
677 if (!name)
678 continue;
679 if (nla_put_string(skb, i + 1, name))
680 goto nla_put_failure;
681 } else {
682 if (nla_put_u32(skb, i + 1, metrics[i]))
683 goto nla_put_failure;
685 valid++;
689 if (!valid) {
690 nla_nest_cancel(skb, mx);
691 return 0;
694 return nla_nest_end(skb, mx);
696 nla_put_failure:
697 nla_nest_cancel(skb, mx);
698 return -EMSGSIZE;
700 EXPORT_SYMBOL(rtnetlink_put_metrics);
702 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
703 long expires, u32 error)
705 struct rta_cacheinfo ci = {
706 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
707 .rta_used = dst->__use,
708 .rta_clntref = atomic_read(&(dst->__refcnt)),
709 .rta_error = error,
710 .rta_id = id,
713 if (expires) {
714 unsigned long clock;
716 clock = jiffies_to_clock_t(abs(expires));
717 clock = min_t(unsigned long, clock, INT_MAX);
718 ci.rta_expires = (expires > 0) ? clock : -clock;
720 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
722 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
724 static void set_operstate(struct net_device *dev, unsigned char transition)
726 unsigned char operstate = dev->operstate;
728 switch (transition) {
729 case IF_OPER_UP:
730 if ((operstate == IF_OPER_DORMANT ||
731 operstate == IF_OPER_UNKNOWN) &&
732 !netif_dormant(dev))
733 operstate = IF_OPER_UP;
734 break;
736 case IF_OPER_DORMANT:
737 if (operstate == IF_OPER_UP ||
738 operstate == IF_OPER_UNKNOWN)
739 operstate = IF_OPER_DORMANT;
740 break;
743 if (dev->operstate != operstate) {
744 write_lock_bh(&dev_base_lock);
745 dev->operstate = operstate;
746 write_unlock_bh(&dev_base_lock);
747 netdev_state_change(dev);
751 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
753 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
754 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
757 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
758 const struct ifinfomsg *ifm)
760 unsigned int flags = ifm->ifi_flags;
762 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
763 if (ifm->ifi_change)
764 flags = (flags & ifm->ifi_change) |
765 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
767 return flags;
770 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
771 const struct rtnl_link_stats64 *b)
773 a->rx_packets = b->rx_packets;
774 a->tx_packets = b->tx_packets;
775 a->rx_bytes = b->rx_bytes;
776 a->tx_bytes = b->tx_bytes;
777 a->rx_errors = b->rx_errors;
778 a->tx_errors = b->tx_errors;
779 a->rx_dropped = b->rx_dropped;
780 a->tx_dropped = b->tx_dropped;
782 a->multicast = b->multicast;
783 a->collisions = b->collisions;
785 a->rx_length_errors = b->rx_length_errors;
786 a->rx_over_errors = b->rx_over_errors;
787 a->rx_crc_errors = b->rx_crc_errors;
788 a->rx_frame_errors = b->rx_frame_errors;
789 a->rx_fifo_errors = b->rx_fifo_errors;
790 a->rx_missed_errors = b->rx_missed_errors;
792 a->tx_aborted_errors = b->tx_aborted_errors;
793 a->tx_carrier_errors = b->tx_carrier_errors;
794 a->tx_fifo_errors = b->tx_fifo_errors;
795 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
796 a->tx_window_errors = b->tx_window_errors;
798 a->rx_compressed = b->rx_compressed;
799 a->tx_compressed = b->tx_compressed;
802 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
804 memcpy(v, b, sizeof(*b));
807 /* All VF info */
808 static inline int rtnl_vfinfo_size(const struct net_device *dev,
809 u32 ext_filter_mask)
811 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
812 (ext_filter_mask & RTEXT_FILTER_VF)) {
813 int num_vfs = dev_num_vf(dev->dev.parent);
814 size_t size = nla_total_size(sizeof(struct nlattr));
815 size += nla_total_size(num_vfs * sizeof(struct nlattr));
816 size += num_vfs *
817 (nla_total_size(sizeof(struct ifla_vf_mac)) +
818 nla_total_size(sizeof(struct ifla_vf_vlan)) +
819 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
820 nla_total_size(sizeof(struct ifla_vf_rate)) +
821 nla_total_size(sizeof(struct ifla_vf_link_state)) +
822 nla_total_size(sizeof(struct ifla_vf_rss_query_en)));
823 return size;
824 } else
825 return 0;
828 static size_t rtnl_port_size(const struct net_device *dev,
829 u32 ext_filter_mask)
831 size_t port_size = nla_total_size(4) /* PORT_VF */
832 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
833 + nla_total_size(sizeof(struct ifla_port_vsi))
834 /* PORT_VSI_TYPE */
835 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
836 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
837 + nla_total_size(1) /* PROT_VDP_REQUEST */
838 + nla_total_size(2); /* PORT_VDP_RESPONSE */
839 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
840 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
841 + port_size;
842 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
843 + port_size;
845 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
846 !(ext_filter_mask & RTEXT_FILTER_VF))
847 return 0;
848 if (dev_num_vf(dev->dev.parent))
849 return port_self_size + vf_ports_size +
850 vf_port_size * dev_num_vf(dev->dev.parent);
851 else
852 return port_self_size;
855 static noinline size_t if_nlmsg_size(const struct net_device *dev,
856 u32 ext_filter_mask)
858 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
859 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
860 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
861 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
862 + nla_total_size(sizeof(struct rtnl_link_ifmap))
863 + nla_total_size(sizeof(struct rtnl_link_stats))
864 + nla_total_size(sizeof(struct rtnl_link_stats64))
865 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
866 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
867 + nla_total_size(4) /* IFLA_TXQLEN */
868 + nla_total_size(4) /* IFLA_WEIGHT */
869 + nla_total_size(4) /* IFLA_MTU */
870 + nla_total_size(4) /* IFLA_LINK */
871 + nla_total_size(4) /* IFLA_MASTER */
872 + nla_total_size(1) /* IFLA_CARRIER */
873 + nla_total_size(4) /* IFLA_PROMISCUITY */
874 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
875 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
876 + nla_total_size(1) /* IFLA_OPERSTATE */
877 + nla_total_size(1) /* IFLA_LINKMODE */
878 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
879 + nla_total_size(4) /* IFLA_LINK_NETNSID */
880 + nla_total_size(ext_filter_mask
881 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
882 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
883 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
884 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
885 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
886 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
887 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
890 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
892 struct nlattr *vf_ports;
893 struct nlattr *vf_port;
894 int vf;
895 int err;
897 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
898 if (!vf_ports)
899 return -EMSGSIZE;
901 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
902 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
903 if (!vf_port)
904 goto nla_put_failure;
905 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
906 goto nla_put_failure;
907 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
908 if (err == -EMSGSIZE)
909 goto nla_put_failure;
910 if (err) {
911 nla_nest_cancel(skb, vf_port);
912 continue;
914 nla_nest_end(skb, vf_port);
917 nla_nest_end(skb, vf_ports);
919 return 0;
921 nla_put_failure:
922 nla_nest_cancel(skb, vf_ports);
923 return -EMSGSIZE;
926 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
928 struct nlattr *port_self;
929 int err;
931 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
932 if (!port_self)
933 return -EMSGSIZE;
935 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
936 if (err) {
937 nla_nest_cancel(skb, port_self);
938 return (err == -EMSGSIZE) ? err : 0;
941 nla_nest_end(skb, port_self);
943 return 0;
946 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
947 u32 ext_filter_mask)
949 int err;
951 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
952 !(ext_filter_mask & RTEXT_FILTER_VF))
953 return 0;
955 err = rtnl_port_self_fill(skb, dev);
956 if (err)
957 return err;
959 if (dev_num_vf(dev->dev.parent)) {
960 err = rtnl_vf_ports_fill(skb, dev);
961 if (err)
962 return err;
965 return 0;
968 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
970 int err;
971 struct netdev_phys_item_id ppid;
973 err = dev_get_phys_port_id(dev, &ppid);
974 if (err) {
975 if (err == -EOPNOTSUPP)
976 return 0;
977 return err;
980 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
981 return -EMSGSIZE;
983 return 0;
986 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
988 char name[IFNAMSIZ];
989 int err;
991 err = dev_get_phys_port_name(dev, name, sizeof(name));
992 if (err) {
993 if (err == -EOPNOTSUPP)
994 return 0;
995 return err;
998 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
999 return -EMSGSIZE;
1001 return 0;
1004 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1006 int err;
1007 struct netdev_phys_item_id psid;
1009 err = netdev_switch_parent_id_get(dev, &psid);
1010 if (err) {
1011 if (err == -EOPNOTSUPP)
1012 return 0;
1013 return err;
1016 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
1017 return -EMSGSIZE;
1019 return 0;
1022 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1023 int type, u32 pid, u32 seq, u32 change,
1024 unsigned int flags, u32 ext_filter_mask)
1026 struct ifinfomsg *ifm;
1027 struct nlmsghdr *nlh;
1028 struct rtnl_link_stats64 temp;
1029 const struct rtnl_link_stats64 *stats;
1030 struct nlattr *attr, *af_spec;
1031 struct rtnl_af_ops *af_ops;
1032 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1034 ASSERT_RTNL();
1035 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1036 if (nlh == NULL)
1037 return -EMSGSIZE;
1039 ifm = nlmsg_data(nlh);
1040 ifm->ifi_family = AF_UNSPEC;
1041 ifm->__ifi_pad = 0;
1042 ifm->ifi_type = dev->type;
1043 ifm->ifi_index = dev->ifindex;
1044 ifm->ifi_flags = dev_get_flags(dev);
1045 ifm->ifi_change = change;
1047 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1048 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1049 nla_put_u8(skb, IFLA_OPERSTATE,
1050 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1051 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1052 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1053 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1054 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1055 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1056 #ifdef CONFIG_RPS
1057 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1058 #endif
1059 (dev->ifindex != dev_get_iflink(dev) &&
1060 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1061 (upper_dev &&
1062 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1063 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1064 (dev->qdisc &&
1065 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1066 (dev->ifalias &&
1067 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1068 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1069 atomic_read(&dev->carrier_changes)))
1070 goto nla_put_failure;
1072 if (1) {
1073 struct rtnl_link_ifmap map = {
1074 .mem_start = dev->mem_start,
1075 .mem_end = dev->mem_end,
1076 .base_addr = dev->base_addr,
1077 .irq = dev->irq,
1078 .dma = dev->dma,
1079 .port = dev->if_port,
1081 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1082 goto nla_put_failure;
1085 if (dev->addr_len) {
1086 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1087 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1088 goto nla_put_failure;
1091 if (rtnl_phys_port_id_fill(skb, dev))
1092 goto nla_put_failure;
1094 if (rtnl_phys_port_name_fill(skb, dev))
1095 goto nla_put_failure;
1097 if (rtnl_phys_switch_id_fill(skb, dev))
1098 goto nla_put_failure;
1100 attr = nla_reserve(skb, IFLA_STATS,
1101 sizeof(struct rtnl_link_stats));
1102 if (attr == NULL)
1103 goto nla_put_failure;
1105 stats = dev_get_stats(dev, &temp);
1106 copy_rtnl_link_stats(nla_data(attr), stats);
1108 attr = nla_reserve(skb, IFLA_STATS64,
1109 sizeof(struct rtnl_link_stats64));
1110 if (attr == NULL)
1111 goto nla_put_failure;
1112 copy_rtnl_link_stats64(nla_data(attr), stats);
1114 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1115 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1116 goto nla_put_failure;
1118 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1119 && (ext_filter_mask & RTEXT_FILTER_VF)) {
1120 int i;
1122 struct nlattr *vfinfo, *vf;
1123 int num_vfs = dev_num_vf(dev->dev.parent);
1125 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1126 if (!vfinfo)
1127 goto nla_put_failure;
1128 for (i = 0; i < num_vfs; i++) {
1129 struct ifla_vf_info ivi;
1130 struct ifla_vf_mac vf_mac;
1131 struct ifla_vf_vlan vf_vlan;
1132 struct ifla_vf_rate vf_rate;
1133 struct ifla_vf_tx_rate vf_tx_rate;
1134 struct ifla_vf_spoofchk vf_spoofchk;
1135 struct ifla_vf_link_state vf_linkstate;
1136 struct ifla_vf_rss_query_en vf_rss_query_en;
1139 * Not all SR-IOV capable drivers support the
1140 * spoofcheck and "RSS query enable" query. Preset to
1141 * -1 so the user space tool can detect that the driver
1142 * didn't report anything.
1144 ivi.spoofchk = -1;
1145 ivi.rss_query_en = -1;
1146 memset(ivi.mac, 0, sizeof(ivi.mac));
1147 /* The default value for VF link state is "auto"
1148 * IFLA_VF_LINK_STATE_AUTO which equals zero
1150 ivi.linkstate = 0;
1151 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1152 break;
1153 vf_mac.vf =
1154 vf_vlan.vf =
1155 vf_rate.vf =
1156 vf_tx_rate.vf =
1157 vf_spoofchk.vf =
1158 vf_linkstate.vf =
1159 vf_rss_query_en.vf = ivi.vf;
1161 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1162 vf_vlan.vlan = ivi.vlan;
1163 vf_vlan.qos = ivi.qos;
1164 vf_tx_rate.rate = ivi.max_tx_rate;
1165 vf_rate.min_tx_rate = ivi.min_tx_rate;
1166 vf_rate.max_tx_rate = ivi.max_tx_rate;
1167 vf_spoofchk.setting = ivi.spoofchk;
1168 vf_linkstate.link_state = ivi.linkstate;
1169 vf_rss_query_en.setting = ivi.rss_query_en;
1170 vf = nla_nest_start(skb, IFLA_VF_INFO);
1171 if (!vf) {
1172 nla_nest_cancel(skb, vfinfo);
1173 goto nla_put_failure;
1175 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1176 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1177 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1178 &vf_rate) ||
1179 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1180 &vf_tx_rate) ||
1181 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1182 &vf_spoofchk) ||
1183 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1184 &vf_linkstate) ||
1185 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1186 sizeof(vf_rss_query_en),
1187 &vf_rss_query_en))
1188 goto nla_put_failure;
1189 nla_nest_end(skb, vf);
1191 nla_nest_end(skb, vfinfo);
1194 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1195 goto nla_put_failure;
1197 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1198 if (rtnl_link_fill(skb, dev) < 0)
1199 goto nla_put_failure;
1202 if (dev->rtnl_link_ops &&
1203 dev->rtnl_link_ops->get_link_net) {
1204 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1206 if (!net_eq(dev_net(dev), link_net)) {
1207 int id = peernet2id(dev_net(dev), link_net);
1209 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1210 goto nla_put_failure;
1214 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1215 goto nla_put_failure;
1217 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1218 if (af_ops->fill_link_af) {
1219 struct nlattr *af;
1220 int err;
1222 if (!(af = nla_nest_start(skb, af_ops->family)))
1223 goto nla_put_failure;
1225 err = af_ops->fill_link_af(skb, dev);
1228 * Caller may return ENODATA to indicate that there
1229 * was no data to be dumped. This is not an error, it
1230 * means we should trim the attribute header and
1231 * continue.
1233 if (err == -ENODATA)
1234 nla_nest_cancel(skb, af);
1235 else if (err < 0)
1236 goto nla_put_failure;
1238 nla_nest_end(skb, af);
1242 nla_nest_end(skb, af_spec);
1244 nlmsg_end(skb, nlh);
1245 return 0;
1247 nla_put_failure:
1248 nlmsg_cancel(skb, nlh);
1249 return -EMSGSIZE;
1252 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1253 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1254 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1255 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1256 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1257 [IFLA_MTU] = { .type = NLA_U32 },
1258 [IFLA_LINK] = { .type = NLA_U32 },
1259 [IFLA_MASTER] = { .type = NLA_U32 },
1260 [IFLA_CARRIER] = { .type = NLA_U8 },
1261 [IFLA_TXQLEN] = { .type = NLA_U32 },
1262 [IFLA_WEIGHT] = { .type = NLA_U32 },
1263 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1264 [IFLA_LINKMODE] = { .type = NLA_U8 },
1265 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1266 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1267 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1268 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1269 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1270 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1271 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1272 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1273 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1274 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1275 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1276 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1277 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1278 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1279 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1280 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1283 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1284 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1285 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1286 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1287 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1290 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1291 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1292 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1293 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1294 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1295 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1296 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1297 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1300 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1301 [IFLA_PORT_VF] = { .type = NLA_U32 },
1302 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1303 .len = PORT_PROFILE_MAX },
1304 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1305 .len = sizeof(struct ifla_port_vsi)},
1306 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1307 .len = PORT_UUID_MAX },
1308 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1309 .len = PORT_UUID_MAX },
1310 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1311 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1314 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1316 struct net *net = sock_net(skb->sk);
1317 int h, s_h;
1318 int idx = 0, s_idx;
1319 struct net_device *dev;
1320 struct hlist_head *head;
1321 struct nlattr *tb[IFLA_MAX+1];
1322 u32 ext_filter_mask = 0;
1323 int err;
1324 int hdrlen;
1326 s_h = cb->args[0];
1327 s_idx = cb->args[1];
1329 cb->seq = net->dev_base_seq;
1331 /* A hack to preserve kernel<->userspace interface.
1332 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1333 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1334 * what iproute2 < v3.9.0 used.
1335 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1336 * attribute, its netlink message is shorter than struct ifinfomsg.
1338 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1339 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1341 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1343 if (tb[IFLA_EXT_MASK])
1344 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1347 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1348 idx = 0;
1349 head = &net->dev_index_head[h];
1350 hlist_for_each_entry(dev, head, index_hlist) {
1351 if (idx < s_idx)
1352 goto cont;
1353 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1354 NETLINK_CB(cb->skb).portid,
1355 cb->nlh->nlmsg_seq, 0,
1356 NLM_F_MULTI,
1357 ext_filter_mask);
1358 /* If we ran out of room on the first message,
1359 * we're in trouble
1361 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1363 if (err < 0)
1364 goto out;
1366 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1367 cont:
1368 idx++;
1371 out:
1372 cb->args[1] = idx;
1373 cb->args[0] = h;
1375 return skb->len;
1378 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1380 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1382 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1384 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1386 struct net *net;
1387 /* Examine the link attributes and figure out which
1388 * network namespace we are talking about.
1390 if (tb[IFLA_NET_NS_PID])
1391 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1392 else if (tb[IFLA_NET_NS_FD])
1393 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1394 else
1395 net = get_net(src_net);
1396 return net;
1398 EXPORT_SYMBOL(rtnl_link_get_net);
1400 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1402 if (dev) {
1403 if (tb[IFLA_ADDRESS] &&
1404 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1405 return -EINVAL;
1407 if (tb[IFLA_BROADCAST] &&
1408 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1409 return -EINVAL;
1412 if (tb[IFLA_AF_SPEC]) {
1413 struct nlattr *af;
1414 int rem, err;
1416 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1417 const struct rtnl_af_ops *af_ops;
1419 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1420 return -EAFNOSUPPORT;
1422 if (!af_ops->set_link_af)
1423 return -EOPNOTSUPP;
1425 if (af_ops->validate_link_af) {
1426 err = af_ops->validate_link_af(dev, af);
1427 if (err < 0)
1428 return err;
1433 return 0;
1436 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1438 const struct net_device_ops *ops = dev->netdev_ops;
1439 int err = -EINVAL;
1441 if (tb[IFLA_VF_MAC]) {
1442 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1444 err = -EOPNOTSUPP;
1445 if (ops->ndo_set_vf_mac)
1446 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1447 ivm->mac);
1448 if (err < 0)
1449 return err;
1452 if (tb[IFLA_VF_VLAN]) {
1453 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1455 err = -EOPNOTSUPP;
1456 if (ops->ndo_set_vf_vlan)
1457 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1458 ivv->qos);
1459 if (err < 0)
1460 return err;
1463 if (tb[IFLA_VF_TX_RATE]) {
1464 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1465 struct ifla_vf_info ivf;
1467 err = -EOPNOTSUPP;
1468 if (ops->ndo_get_vf_config)
1469 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1470 if (err < 0)
1471 return err;
1473 err = -EOPNOTSUPP;
1474 if (ops->ndo_set_vf_rate)
1475 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1476 ivf.min_tx_rate,
1477 ivt->rate);
1478 if (err < 0)
1479 return err;
1482 if (tb[IFLA_VF_RATE]) {
1483 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1485 err = -EOPNOTSUPP;
1486 if (ops->ndo_set_vf_rate)
1487 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1488 ivt->min_tx_rate,
1489 ivt->max_tx_rate);
1490 if (err < 0)
1491 return err;
1494 if (tb[IFLA_VF_SPOOFCHK]) {
1495 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1497 err = -EOPNOTSUPP;
1498 if (ops->ndo_set_vf_spoofchk)
1499 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1500 ivs->setting);
1501 if (err < 0)
1502 return err;
1505 if (tb[IFLA_VF_LINK_STATE]) {
1506 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1508 err = -EOPNOTSUPP;
1509 if (ops->ndo_set_vf_link_state)
1510 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1511 ivl->link_state);
1512 if (err < 0)
1513 return err;
1516 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1517 struct ifla_vf_rss_query_en *ivrssq_en;
1519 err = -EOPNOTSUPP;
1520 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1521 if (ops->ndo_set_vf_rss_query_en)
1522 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1523 ivrssq_en->setting);
1524 if (err < 0)
1525 return err;
1528 return err;
1531 static int do_set_master(struct net_device *dev, int ifindex)
1533 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1534 const struct net_device_ops *ops;
1535 int err;
1537 if (upper_dev) {
1538 if (upper_dev->ifindex == ifindex)
1539 return 0;
1540 ops = upper_dev->netdev_ops;
1541 if (ops->ndo_del_slave) {
1542 err = ops->ndo_del_slave(upper_dev, dev);
1543 if (err)
1544 return err;
1545 } else {
1546 return -EOPNOTSUPP;
1550 if (ifindex) {
1551 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1552 if (!upper_dev)
1553 return -EINVAL;
1554 ops = upper_dev->netdev_ops;
1555 if (ops->ndo_add_slave) {
1556 err = ops->ndo_add_slave(upper_dev, dev);
1557 if (err)
1558 return err;
1559 } else {
1560 return -EOPNOTSUPP;
1563 return 0;
1566 #define DO_SETLINK_MODIFIED 0x01
1567 /* notify flag means notify + modified. */
1568 #define DO_SETLINK_NOTIFY 0x03
1569 static int do_setlink(const struct sk_buff *skb,
1570 struct net_device *dev, struct ifinfomsg *ifm,
1571 struct nlattr **tb, char *ifname, int status)
1573 const struct net_device_ops *ops = dev->netdev_ops;
1574 int err;
1576 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1577 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1578 if (IS_ERR(net)) {
1579 err = PTR_ERR(net);
1580 goto errout;
1582 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1583 put_net(net);
1584 err = -EPERM;
1585 goto errout;
1587 err = dev_change_net_namespace(dev, net, ifname);
1588 put_net(net);
1589 if (err)
1590 goto errout;
1591 status |= DO_SETLINK_MODIFIED;
1594 if (tb[IFLA_MAP]) {
1595 struct rtnl_link_ifmap *u_map;
1596 struct ifmap k_map;
1598 if (!ops->ndo_set_config) {
1599 err = -EOPNOTSUPP;
1600 goto errout;
1603 if (!netif_device_present(dev)) {
1604 err = -ENODEV;
1605 goto errout;
1608 u_map = nla_data(tb[IFLA_MAP]);
1609 k_map.mem_start = (unsigned long) u_map->mem_start;
1610 k_map.mem_end = (unsigned long) u_map->mem_end;
1611 k_map.base_addr = (unsigned short) u_map->base_addr;
1612 k_map.irq = (unsigned char) u_map->irq;
1613 k_map.dma = (unsigned char) u_map->dma;
1614 k_map.port = (unsigned char) u_map->port;
1616 err = ops->ndo_set_config(dev, &k_map);
1617 if (err < 0)
1618 goto errout;
1620 status |= DO_SETLINK_NOTIFY;
1623 if (tb[IFLA_ADDRESS]) {
1624 struct sockaddr *sa;
1625 int len;
1627 len = sizeof(sa_family_t) + dev->addr_len;
1628 sa = kmalloc(len, GFP_KERNEL);
1629 if (!sa) {
1630 err = -ENOMEM;
1631 goto errout;
1633 sa->sa_family = dev->type;
1634 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1635 dev->addr_len);
1636 err = dev_set_mac_address(dev, sa);
1637 kfree(sa);
1638 if (err)
1639 goto errout;
1640 status |= DO_SETLINK_MODIFIED;
1643 if (tb[IFLA_MTU]) {
1644 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1645 if (err < 0)
1646 goto errout;
1647 status |= DO_SETLINK_MODIFIED;
1650 if (tb[IFLA_GROUP]) {
1651 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1652 status |= DO_SETLINK_NOTIFY;
1656 * Interface selected by interface index but interface
1657 * name provided implies that a name change has been
1658 * requested.
1660 if (ifm->ifi_index > 0 && ifname[0]) {
1661 err = dev_change_name(dev, ifname);
1662 if (err < 0)
1663 goto errout;
1664 status |= DO_SETLINK_MODIFIED;
1667 if (tb[IFLA_IFALIAS]) {
1668 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1669 nla_len(tb[IFLA_IFALIAS]));
1670 if (err < 0)
1671 goto errout;
1672 status |= DO_SETLINK_NOTIFY;
1675 if (tb[IFLA_BROADCAST]) {
1676 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1677 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1680 if (ifm->ifi_flags || ifm->ifi_change) {
1681 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1682 if (err < 0)
1683 goto errout;
1686 if (tb[IFLA_MASTER]) {
1687 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1688 if (err)
1689 goto errout;
1690 status |= DO_SETLINK_MODIFIED;
1693 if (tb[IFLA_CARRIER]) {
1694 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1695 if (err)
1696 goto errout;
1697 status |= DO_SETLINK_MODIFIED;
1700 if (tb[IFLA_TXQLEN]) {
1701 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1703 if (dev->tx_queue_len ^ value)
1704 status |= DO_SETLINK_NOTIFY;
1706 dev->tx_queue_len = value;
1709 if (tb[IFLA_OPERSTATE])
1710 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1712 if (tb[IFLA_LINKMODE]) {
1713 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1715 write_lock_bh(&dev_base_lock);
1716 if (dev->link_mode ^ value)
1717 status |= DO_SETLINK_NOTIFY;
1718 dev->link_mode = value;
1719 write_unlock_bh(&dev_base_lock);
1722 if (tb[IFLA_VFINFO_LIST]) {
1723 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
1724 struct nlattr *attr;
1725 int rem;
1727 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1728 if (nla_type(attr) != IFLA_VF_INFO ||
1729 nla_len(attr) < NLA_HDRLEN) {
1730 err = -EINVAL;
1731 goto errout;
1733 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1734 ifla_vf_policy);
1735 if (err < 0)
1736 goto errout;
1737 err = do_setvfinfo(dev, vfinfo);
1738 if (err < 0)
1739 goto errout;
1740 status |= DO_SETLINK_NOTIFY;
1743 err = 0;
1745 if (tb[IFLA_VF_PORTS]) {
1746 struct nlattr *port[IFLA_PORT_MAX+1];
1747 struct nlattr *attr;
1748 int vf;
1749 int rem;
1751 err = -EOPNOTSUPP;
1752 if (!ops->ndo_set_vf_port)
1753 goto errout;
1755 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1756 if (nla_type(attr) != IFLA_VF_PORT)
1757 continue;
1758 err = nla_parse_nested(port, IFLA_PORT_MAX,
1759 attr, ifla_port_policy);
1760 if (err < 0)
1761 goto errout;
1762 if (!port[IFLA_PORT_VF]) {
1763 err = -EOPNOTSUPP;
1764 goto errout;
1766 vf = nla_get_u32(port[IFLA_PORT_VF]);
1767 err = ops->ndo_set_vf_port(dev, vf, port);
1768 if (err < 0)
1769 goto errout;
1770 status |= DO_SETLINK_NOTIFY;
1773 err = 0;
1775 if (tb[IFLA_PORT_SELF]) {
1776 struct nlattr *port[IFLA_PORT_MAX+1];
1778 err = nla_parse_nested(port, IFLA_PORT_MAX,
1779 tb[IFLA_PORT_SELF], ifla_port_policy);
1780 if (err < 0)
1781 goto errout;
1783 err = -EOPNOTSUPP;
1784 if (ops->ndo_set_vf_port)
1785 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1786 if (err < 0)
1787 goto errout;
1788 status |= DO_SETLINK_NOTIFY;
1791 if (tb[IFLA_AF_SPEC]) {
1792 struct nlattr *af;
1793 int rem;
1795 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1796 const struct rtnl_af_ops *af_ops;
1798 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1799 BUG();
1801 err = af_ops->set_link_af(dev, af);
1802 if (err < 0)
1803 goto errout;
1805 status |= DO_SETLINK_NOTIFY;
1808 err = 0;
1810 errout:
1811 if (status & DO_SETLINK_MODIFIED) {
1812 if (status & DO_SETLINK_NOTIFY)
1813 netdev_state_change(dev);
1815 if (err < 0)
1816 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",
1817 dev->name);
1820 return err;
1823 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1825 struct net *net = sock_net(skb->sk);
1826 struct ifinfomsg *ifm;
1827 struct net_device *dev;
1828 int err;
1829 struct nlattr *tb[IFLA_MAX+1];
1830 char ifname[IFNAMSIZ];
1832 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1833 if (err < 0)
1834 goto errout;
1836 if (tb[IFLA_IFNAME])
1837 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1838 else
1839 ifname[0] = '\0';
1841 err = -EINVAL;
1842 ifm = nlmsg_data(nlh);
1843 if (ifm->ifi_index > 0)
1844 dev = __dev_get_by_index(net, ifm->ifi_index);
1845 else if (tb[IFLA_IFNAME])
1846 dev = __dev_get_by_name(net, ifname);
1847 else
1848 goto errout;
1850 if (dev == NULL) {
1851 err = -ENODEV;
1852 goto errout;
1855 err = validate_linkmsg(dev, tb);
1856 if (err < 0)
1857 goto errout;
1859 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1860 errout:
1861 return err;
1864 static int rtnl_group_dellink(const struct net *net, int group)
1866 struct net_device *dev, *aux;
1867 LIST_HEAD(list_kill);
1868 bool found = false;
1870 if (!group)
1871 return -EPERM;
1873 for_each_netdev(net, dev) {
1874 if (dev->group == group) {
1875 const struct rtnl_link_ops *ops;
1877 found = true;
1878 ops = dev->rtnl_link_ops;
1879 if (!ops || !ops->dellink)
1880 return -EOPNOTSUPP;
1884 if (!found)
1885 return -ENODEV;
1887 for_each_netdev_safe(net, dev, aux) {
1888 if (dev->group == group) {
1889 const struct rtnl_link_ops *ops;
1891 ops = dev->rtnl_link_ops;
1892 ops->dellink(dev, &list_kill);
1895 unregister_netdevice_many(&list_kill);
1897 return 0;
1900 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
1902 struct net *net = sock_net(skb->sk);
1903 const struct rtnl_link_ops *ops;
1904 struct net_device *dev;
1905 struct ifinfomsg *ifm;
1906 char ifname[IFNAMSIZ];
1907 struct nlattr *tb[IFLA_MAX+1];
1908 int err;
1909 LIST_HEAD(list_kill);
1911 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1912 if (err < 0)
1913 return err;
1915 if (tb[IFLA_IFNAME])
1916 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1918 ifm = nlmsg_data(nlh);
1919 if (ifm->ifi_index > 0)
1920 dev = __dev_get_by_index(net, ifm->ifi_index);
1921 else if (tb[IFLA_IFNAME])
1922 dev = __dev_get_by_name(net, ifname);
1923 else if (tb[IFLA_GROUP])
1924 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
1925 else
1926 return -EINVAL;
1928 if (!dev)
1929 return -ENODEV;
1931 ops = dev->rtnl_link_ops;
1932 if (!ops || !ops->dellink)
1933 return -EOPNOTSUPP;
1935 ops->dellink(dev, &list_kill);
1936 unregister_netdevice_many(&list_kill);
1937 return 0;
1940 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1942 unsigned int old_flags;
1943 int err;
1945 old_flags = dev->flags;
1946 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1947 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1948 if (err < 0)
1949 return err;
1952 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1954 __dev_notify_flags(dev, old_flags, ~0U);
1955 return 0;
1957 EXPORT_SYMBOL(rtnl_configure_link);
1959 struct net_device *rtnl_create_link(struct net *net,
1960 const char *ifname, unsigned char name_assign_type,
1961 const struct rtnl_link_ops *ops, struct nlattr *tb[])
1963 int err;
1964 struct net_device *dev;
1965 unsigned int num_tx_queues = 1;
1966 unsigned int num_rx_queues = 1;
1968 if (tb[IFLA_NUM_TX_QUEUES])
1969 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1970 else if (ops->get_num_tx_queues)
1971 num_tx_queues = ops->get_num_tx_queues();
1973 if (tb[IFLA_NUM_RX_QUEUES])
1974 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1975 else if (ops->get_num_rx_queues)
1976 num_rx_queues = ops->get_num_rx_queues();
1978 err = -ENOMEM;
1979 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1980 ops->setup, num_tx_queues, num_rx_queues);
1981 if (!dev)
1982 goto err;
1984 dev_net_set(dev, net);
1985 dev->rtnl_link_ops = ops;
1986 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1988 if (tb[IFLA_MTU])
1989 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1990 if (tb[IFLA_ADDRESS]) {
1991 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1992 nla_len(tb[IFLA_ADDRESS]));
1993 dev->addr_assign_type = NET_ADDR_SET;
1995 if (tb[IFLA_BROADCAST])
1996 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1997 nla_len(tb[IFLA_BROADCAST]));
1998 if (tb[IFLA_TXQLEN])
1999 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2000 if (tb[IFLA_OPERSTATE])
2001 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2002 if (tb[IFLA_LINKMODE])
2003 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2004 if (tb[IFLA_GROUP])
2005 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2007 return dev;
2009 err:
2010 return ERR_PTR(err);
2012 EXPORT_SYMBOL(rtnl_create_link);
2014 static int rtnl_group_changelink(const struct sk_buff *skb,
2015 struct net *net, int group,
2016 struct ifinfomsg *ifm,
2017 struct nlattr **tb)
2019 struct net_device *dev, *aux;
2020 int err;
2022 for_each_netdev_safe(net, dev, aux) {
2023 if (dev->group == group) {
2024 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2025 if (err < 0)
2026 return err;
2030 return 0;
2033 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2035 struct net *net = sock_net(skb->sk);
2036 const struct rtnl_link_ops *ops;
2037 const struct rtnl_link_ops *m_ops = NULL;
2038 struct net_device *dev;
2039 struct net_device *master_dev = NULL;
2040 struct ifinfomsg *ifm;
2041 char kind[MODULE_NAME_LEN];
2042 char ifname[IFNAMSIZ];
2043 struct nlattr *tb[IFLA_MAX+1];
2044 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2045 unsigned char name_assign_type = NET_NAME_USER;
2046 int err;
2048 #ifdef CONFIG_MODULES
2049 replay:
2050 #endif
2051 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2052 if (err < 0)
2053 return err;
2055 if (tb[IFLA_IFNAME])
2056 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2057 else
2058 ifname[0] = '\0';
2060 ifm = nlmsg_data(nlh);
2061 if (ifm->ifi_index > 0)
2062 dev = __dev_get_by_index(net, ifm->ifi_index);
2063 else {
2064 if (ifname[0])
2065 dev = __dev_get_by_name(net, ifname);
2066 else
2067 dev = NULL;
2070 if (dev) {
2071 master_dev = netdev_master_upper_dev_get(dev);
2072 if (master_dev)
2073 m_ops = master_dev->rtnl_link_ops;
2076 err = validate_linkmsg(dev, tb);
2077 if (err < 0)
2078 return err;
2080 if (tb[IFLA_LINKINFO]) {
2081 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2082 tb[IFLA_LINKINFO], ifla_info_policy);
2083 if (err < 0)
2084 return err;
2085 } else
2086 memset(linkinfo, 0, sizeof(linkinfo));
2088 if (linkinfo[IFLA_INFO_KIND]) {
2089 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2090 ops = rtnl_link_ops_get(kind);
2091 } else {
2092 kind[0] = '\0';
2093 ops = NULL;
2096 if (1) {
2097 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2098 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2099 struct nlattr **data = NULL;
2100 struct nlattr **slave_data = NULL;
2101 struct net *dest_net, *link_net = NULL;
2103 if (ops) {
2104 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2105 err = nla_parse_nested(attr, ops->maxtype,
2106 linkinfo[IFLA_INFO_DATA],
2107 ops->policy);
2108 if (err < 0)
2109 return err;
2110 data = attr;
2112 if (ops->validate) {
2113 err = ops->validate(tb, data);
2114 if (err < 0)
2115 return err;
2119 if (m_ops) {
2120 if (m_ops->slave_maxtype &&
2121 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2122 err = nla_parse_nested(slave_attr,
2123 m_ops->slave_maxtype,
2124 linkinfo[IFLA_INFO_SLAVE_DATA],
2125 m_ops->slave_policy);
2126 if (err < 0)
2127 return err;
2128 slave_data = slave_attr;
2130 if (m_ops->slave_validate) {
2131 err = m_ops->slave_validate(tb, slave_data);
2132 if (err < 0)
2133 return err;
2137 if (dev) {
2138 int status = 0;
2140 if (nlh->nlmsg_flags & NLM_F_EXCL)
2141 return -EEXIST;
2142 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2143 return -EOPNOTSUPP;
2145 if (linkinfo[IFLA_INFO_DATA]) {
2146 if (!ops || ops != dev->rtnl_link_ops ||
2147 !ops->changelink)
2148 return -EOPNOTSUPP;
2150 err = ops->changelink(dev, tb, data);
2151 if (err < 0)
2152 return err;
2153 status |= DO_SETLINK_NOTIFY;
2156 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2157 if (!m_ops || !m_ops->slave_changelink)
2158 return -EOPNOTSUPP;
2160 err = m_ops->slave_changelink(master_dev, dev,
2161 tb, slave_data);
2162 if (err < 0)
2163 return err;
2164 status |= DO_SETLINK_NOTIFY;
2167 return do_setlink(skb, dev, ifm, tb, ifname, status);
2170 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2171 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2172 return rtnl_group_changelink(skb, net,
2173 nla_get_u32(tb[IFLA_GROUP]),
2174 ifm, tb);
2175 return -ENODEV;
2178 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2179 return -EOPNOTSUPP;
2181 if (!ops) {
2182 #ifdef CONFIG_MODULES
2183 if (kind[0]) {
2184 __rtnl_unlock();
2185 request_module("rtnl-link-%s", kind);
2186 rtnl_lock();
2187 ops = rtnl_link_ops_get(kind);
2188 if (ops)
2189 goto replay;
2191 #endif
2192 return -EOPNOTSUPP;
2195 if (!ops->setup)
2196 return -EOPNOTSUPP;
2198 if (!ifname[0]) {
2199 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2200 name_assign_type = NET_NAME_ENUM;
2203 dest_net = rtnl_link_get_net(net, tb);
2204 if (IS_ERR(dest_net))
2205 return PTR_ERR(dest_net);
2207 err = -EPERM;
2208 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2209 goto out;
2211 if (tb[IFLA_LINK_NETNSID]) {
2212 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2214 link_net = get_net_ns_by_id(dest_net, id);
2215 if (!link_net) {
2216 err = -EINVAL;
2217 goto out;
2219 err = -EPERM;
2220 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2221 goto out;
2224 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2225 name_assign_type, ops, tb);
2226 if (IS_ERR(dev)) {
2227 err = PTR_ERR(dev);
2228 goto out;
2231 dev->ifindex = ifm->ifi_index;
2233 if (ops->newlink) {
2234 err = ops->newlink(link_net ? : net, dev, tb, data);
2235 /* Drivers should call free_netdev() in ->destructor
2236 * and unregister it on failure after registration
2237 * so that device could be finally freed in rtnl_unlock.
2239 if (err < 0) {
2240 /* If device is not registered at all, free it now */
2241 if (dev->reg_state == NETREG_UNINITIALIZED)
2242 free_netdev(dev);
2243 goto out;
2245 } else {
2246 err = register_netdevice(dev);
2247 if (err < 0) {
2248 free_netdev(dev);
2249 goto out;
2252 err = rtnl_configure_link(dev, ifm);
2253 if (err < 0)
2254 goto out_unregister;
2255 if (link_net) {
2256 err = dev_change_net_namespace(dev, dest_net, ifname);
2257 if (err < 0)
2258 goto out_unregister;
2260 out:
2261 if (link_net)
2262 put_net(link_net);
2263 put_net(dest_net);
2264 return err;
2265 out_unregister:
2266 if (ops->newlink) {
2267 LIST_HEAD(list_kill);
2269 ops->dellink(dev, &list_kill);
2270 unregister_netdevice_many(&list_kill);
2271 } else {
2272 unregister_netdevice(dev);
2274 goto out;
2278 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2280 struct net *net = sock_net(skb->sk);
2281 struct ifinfomsg *ifm;
2282 char ifname[IFNAMSIZ];
2283 struct nlattr *tb[IFLA_MAX+1];
2284 struct net_device *dev = NULL;
2285 struct sk_buff *nskb;
2286 int err;
2287 u32 ext_filter_mask = 0;
2289 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2290 if (err < 0)
2291 return err;
2293 if (tb[IFLA_IFNAME])
2294 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2296 if (tb[IFLA_EXT_MASK])
2297 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2299 ifm = nlmsg_data(nlh);
2300 if (ifm->ifi_index > 0)
2301 dev = __dev_get_by_index(net, ifm->ifi_index);
2302 else if (tb[IFLA_IFNAME])
2303 dev = __dev_get_by_name(net, ifname);
2304 else
2305 return -EINVAL;
2307 if (dev == NULL)
2308 return -ENODEV;
2310 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2311 if (nskb == NULL)
2312 return -ENOBUFS;
2314 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2315 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2316 if (err < 0) {
2317 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2318 WARN_ON(err == -EMSGSIZE);
2319 kfree_skb(nskb);
2320 } else
2321 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2323 return err;
2326 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2328 struct net *net = sock_net(skb->sk);
2329 struct net_device *dev;
2330 struct nlattr *tb[IFLA_MAX+1];
2331 u32 ext_filter_mask = 0;
2332 u16 min_ifinfo_dump_size = 0;
2333 int hdrlen;
2335 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2336 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2337 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2339 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2340 if (tb[IFLA_EXT_MASK])
2341 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2344 if (!ext_filter_mask)
2345 return NLMSG_GOODSIZE;
2347 * traverse the list of net devices and compute the minimum
2348 * buffer size based upon the filter mask.
2350 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2351 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2352 if_nlmsg_size(dev,
2353 ext_filter_mask));
2356 return min_ifinfo_dump_size;
2359 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2361 int idx;
2362 int s_idx = cb->family;
2364 if (s_idx == 0)
2365 s_idx = 1;
2366 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2367 int type = cb->nlh->nlmsg_type-RTM_BASE;
2368 if (idx < s_idx || idx == PF_PACKET)
2369 continue;
2370 if (rtnl_msg_handlers[idx] == NULL ||
2371 rtnl_msg_handlers[idx][type].dumpit == NULL)
2372 continue;
2373 if (idx > s_idx) {
2374 memset(&cb->args[0], 0, sizeof(cb->args));
2375 cb->prev_seq = 0;
2376 cb->seq = 0;
2378 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2379 break;
2381 cb->family = idx;
2383 return skb->len;
2386 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2387 unsigned int change, gfp_t flags)
2389 struct net *net = dev_net(dev);
2390 struct sk_buff *skb;
2391 int err = -ENOBUFS;
2392 size_t if_info_size;
2394 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2395 if (skb == NULL)
2396 goto errout;
2398 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2399 if (err < 0) {
2400 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2401 WARN_ON(err == -EMSGSIZE);
2402 kfree_skb(skb);
2403 goto errout;
2405 return skb;
2406 errout:
2407 if (err < 0)
2408 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2409 return NULL;
2412 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2414 struct net *net = dev_net(dev);
2416 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2419 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2420 gfp_t flags)
2422 struct sk_buff *skb;
2424 if (dev->reg_state != NETREG_REGISTERED)
2425 return;
2427 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2428 if (skb)
2429 rtmsg_ifinfo_send(skb, dev, flags);
2431 EXPORT_SYMBOL(rtmsg_ifinfo);
2433 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2434 struct net_device *dev,
2435 u8 *addr, u16 vid, u32 pid, u32 seq,
2436 int type, unsigned int flags,
2437 int nlflags)
2439 struct nlmsghdr *nlh;
2440 struct ndmsg *ndm;
2442 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2443 if (!nlh)
2444 return -EMSGSIZE;
2446 ndm = nlmsg_data(nlh);
2447 ndm->ndm_family = AF_BRIDGE;
2448 ndm->ndm_pad1 = 0;
2449 ndm->ndm_pad2 = 0;
2450 ndm->ndm_flags = flags;
2451 ndm->ndm_type = 0;
2452 ndm->ndm_ifindex = dev->ifindex;
2453 ndm->ndm_state = NUD_PERMANENT;
2455 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2456 goto nla_put_failure;
2457 if (vid)
2458 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2459 goto nla_put_failure;
2461 nlmsg_end(skb, nlh);
2462 return 0;
2464 nla_put_failure:
2465 nlmsg_cancel(skb, nlh);
2466 return -EMSGSIZE;
2469 static inline size_t rtnl_fdb_nlmsg_size(void)
2471 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2474 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
2476 struct net *net = dev_net(dev);
2477 struct sk_buff *skb;
2478 int err = -ENOBUFS;
2480 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2481 if (!skb)
2482 goto errout;
2484 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2485 0, 0, type, NTF_SELF, 0);
2486 if (err < 0) {
2487 kfree_skb(skb);
2488 goto errout;
2491 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2492 return;
2493 errout:
2494 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2498 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2500 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2501 struct nlattr *tb[],
2502 struct net_device *dev,
2503 const unsigned char *addr, u16 vid,
2504 u16 flags)
2506 int err = -EINVAL;
2508 /* If aging addresses are supported device will need to
2509 * implement its own handler for this.
2511 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2512 pr_info("%s: FDB only supports static addresses\n", dev->name);
2513 return err;
2516 if (vid) {
2517 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2518 return err;
2521 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2522 err = dev_uc_add_excl(dev, addr);
2523 else if (is_multicast_ether_addr(addr))
2524 err = dev_mc_add_excl(dev, addr);
2526 /* Only return duplicate errors if NLM_F_EXCL is set */
2527 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2528 err = 0;
2530 return err;
2532 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2534 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2536 u16 vid = 0;
2538 if (vlan_attr) {
2539 if (nla_len(vlan_attr) != sizeof(u16)) {
2540 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2541 return -EINVAL;
2544 vid = nla_get_u16(vlan_attr);
2546 if (!vid || vid >= VLAN_VID_MASK) {
2547 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2548 vid);
2549 return -EINVAL;
2552 *p_vid = vid;
2553 return 0;
2556 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2558 struct net *net = sock_net(skb->sk);
2559 struct ndmsg *ndm;
2560 struct nlattr *tb[NDA_MAX+1];
2561 struct net_device *dev;
2562 u8 *addr;
2563 u16 vid;
2564 int err;
2566 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2567 if (err < 0)
2568 return err;
2570 ndm = nlmsg_data(nlh);
2571 if (ndm->ndm_ifindex == 0) {
2572 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2573 return -EINVAL;
2576 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2577 if (dev == NULL) {
2578 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2579 return -ENODEV;
2582 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2583 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2584 return -EINVAL;
2587 addr = nla_data(tb[NDA_LLADDR]);
2589 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2590 if (err)
2591 return err;
2593 err = -EOPNOTSUPP;
2595 /* Support fdb on master device the net/bridge default case */
2596 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2597 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2598 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2599 const struct net_device_ops *ops = br_dev->netdev_ops;
2601 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2602 nlh->nlmsg_flags);
2603 if (err)
2604 goto out;
2605 else
2606 ndm->ndm_flags &= ~NTF_MASTER;
2609 /* Embedded bridge, macvlan, and any other device support */
2610 if ((ndm->ndm_flags & NTF_SELF)) {
2611 if (dev->netdev_ops->ndo_fdb_add)
2612 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2613 vid,
2614 nlh->nlmsg_flags);
2615 else
2616 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2617 nlh->nlmsg_flags);
2619 if (!err) {
2620 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
2621 ndm->ndm_flags &= ~NTF_SELF;
2624 out:
2625 return err;
2629 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2631 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2632 struct nlattr *tb[],
2633 struct net_device *dev,
2634 const unsigned char *addr, u16 vid)
2636 int err = -EINVAL;
2638 /* If aging addresses are supported device will need to
2639 * implement its own handler for this.
2641 if (!(ndm->ndm_state & NUD_PERMANENT)) {
2642 pr_info("%s: FDB only supports static addresses\n", dev->name);
2643 return err;
2646 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2647 err = dev_uc_del(dev, addr);
2648 else if (is_multicast_ether_addr(addr))
2649 err = dev_mc_del(dev, addr);
2651 return err;
2653 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2655 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2657 struct net *net = sock_net(skb->sk);
2658 struct ndmsg *ndm;
2659 struct nlattr *tb[NDA_MAX+1];
2660 struct net_device *dev;
2661 int err = -EINVAL;
2662 __u8 *addr;
2663 u16 vid;
2665 if (!netlink_capable(skb, CAP_NET_ADMIN))
2666 return -EPERM;
2668 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2669 if (err < 0)
2670 return err;
2672 ndm = nlmsg_data(nlh);
2673 if (ndm->ndm_ifindex == 0) {
2674 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2675 return -EINVAL;
2678 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2679 if (dev == NULL) {
2680 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2681 return -ENODEV;
2684 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2685 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2686 return -EINVAL;
2689 addr = nla_data(tb[NDA_LLADDR]);
2691 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2692 if (err)
2693 return err;
2695 err = -EOPNOTSUPP;
2697 /* Support fdb on master device the net/bridge default case */
2698 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2699 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2700 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2701 const struct net_device_ops *ops = br_dev->netdev_ops;
2703 if (ops->ndo_fdb_del)
2704 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2706 if (err)
2707 goto out;
2708 else
2709 ndm->ndm_flags &= ~NTF_MASTER;
2712 /* Embedded bridge, macvlan, and any other device support */
2713 if (ndm->ndm_flags & NTF_SELF) {
2714 if (dev->netdev_ops->ndo_fdb_del)
2715 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2716 vid);
2717 else
2718 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2720 if (!err) {
2721 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
2722 ndm->ndm_flags &= ~NTF_SELF;
2725 out:
2726 return err;
2729 static int nlmsg_populate_fdb(struct sk_buff *skb,
2730 struct netlink_callback *cb,
2731 struct net_device *dev,
2732 int *idx,
2733 struct netdev_hw_addr_list *list)
2735 struct netdev_hw_addr *ha;
2736 int err;
2737 u32 portid, seq;
2739 portid = NETLINK_CB(cb->skb).portid;
2740 seq = cb->nlh->nlmsg_seq;
2742 list_for_each_entry(ha, &list->list, list) {
2743 if (*idx < cb->args[0])
2744 goto skip;
2746 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2747 portid, seq,
2748 RTM_NEWNEIGH, NTF_SELF,
2749 NLM_F_MULTI);
2750 if (err < 0)
2751 return err;
2752 skip:
2753 *idx += 1;
2755 return 0;
2759 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2760 * @nlh: netlink message header
2761 * @dev: netdevice
2763 * Default netdevice operation to dump the existing unicast address list.
2764 * Returns number of addresses from list put in skb.
2766 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2767 struct netlink_callback *cb,
2768 struct net_device *dev,
2769 struct net_device *filter_dev,
2770 int idx)
2772 int err;
2774 netif_addr_lock_bh(dev);
2775 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2776 if (err)
2777 goto out;
2778 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2779 out:
2780 netif_addr_unlock_bh(dev);
2781 return idx;
2783 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2785 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2787 struct net_device *dev;
2788 struct nlattr *tb[IFLA_MAX+1];
2789 struct net_device *br_dev = NULL;
2790 const struct net_device_ops *ops = NULL;
2791 const struct net_device_ops *cops = NULL;
2792 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2793 struct net *net = sock_net(skb->sk);
2794 int brport_idx = 0;
2795 int br_idx = 0;
2796 int idx = 0;
2798 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2799 ifla_policy) == 0) {
2800 if (tb[IFLA_MASTER])
2801 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2804 brport_idx = ifm->ifi_index;
2806 if (br_idx) {
2807 br_dev = __dev_get_by_index(net, br_idx);
2808 if (!br_dev)
2809 return -ENODEV;
2811 ops = br_dev->netdev_ops;
2814 for_each_netdev(net, dev) {
2815 if (brport_idx && (dev->ifindex != brport_idx))
2816 continue;
2818 if (!br_idx) { /* user did not specify a specific bridge */
2819 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2820 br_dev = netdev_master_upper_dev_get(dev);
2821 cops = br_dev->netdev_ops;
2824 } else {
2825 if (dev != br_dev &&
2826 !(dev->priv_flags & IFF_BRIDGE_PORT))
2827 continue;
2829 if (br_dev != netdev_master_upper_dev_get(dev) &&
2830 !(dev->priv_flags & IFF_EBRIDGE))
2831 continue;
2833 cops = ops;
2836 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2837 if (cops && cops->ndo_fdb_dump)
2838 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2839 idx);
2842 if (dev->netdev_ops->ndo_fdb_dump)
2843 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
2844 idx);
2845 else
2846 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
2848 cops = NULL;
2851 cb->args[0] = idx;
2852 return skb->len;
2855 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2856 unsigned int attrnum, unsigned int flag)
2858 if (mask & flag)
2859 return nla_put_u8(skb, attrnum, !!(flags & flag));
2860 return 0;
2863 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2864 struct net_device *dev, u16 mode,
2865 u32 flags, u32 mask, int nlflags)
2867 struct nlmsghdr *nlh;
2868 struct ifinfomsg *ifm;
2869 struct nlattr *br_afspec;
2870 struct nlattr *protinfo;
2871 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2872 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2874 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
2875 if (nlh == NULL)
2876 return -EMSGSIZE;
2878 ifm = nlmsg_data(nlh);
2879 ifm->ifi_family = AF_BRIDGE;
2880 ifm->__ifi_pad = 0;
2881 ifm->ifi_type = dev->type;
2882 ifm->ifi_index = dev->ifindex;
2883 ifm->ifi_flags = dev_get_flags(dev);
2884 ifm->ifi_change = 0;
2887 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2888 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2889 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2890 (br_dev &&
2891 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2892 (dev->addr_len &&
2893 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2894 (dev->ifindex != dev_get_iflink(dev) &&
2895 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
2896 goto nla_put_failure;
2898 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2899 if (!br_afspec)
2900 goto nla_put_failure;
2902 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
2903 nla_nest_cancel(skb, br_afspec);
2904 goto nla_put_failure;
2907 if (mode != BRIDGE_MODE_UNDEF) {
2908 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2909 nla_nest_cancel(skb, br_afspec);
2910 goto nla_put_failure;
2913 nla_nest_end(skb, br_afspec);
2915 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2916 if (!protinfo)
2917 goto nla_put_failure;
2919 if (brport_nla_put_flag(skb, flags, mask,
2920 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2921 brport_nla_put_flag(skb, flags, mask,
2922 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2923 brport_nla_put_flag(skb, flags, mask,
2924 IFLA_BRPORT_FAST_LEAVE,
2925 BR_MULTICAST_FAST_LEAVE) ||
2926 brport_nla_put_flag(skb, flags, mask,
2927 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2928 brport_nla_put_flag(skb, flags, mask,
2929 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2930 brport_nla_put_flag(skb, flags, mask,
2931 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2932 brport_nla_put_flag(skb, flags, mask,
2933 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2934 brport_nla_put_flag(skb, flags, mask,
2935 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2936 nla_nest_cancel(skb, protinfo);
2937 goto nla_put_failure;
2940 nla_nest_end(skb, protinfo);
2942 nlmsg_end(skb, nlh);
2943 return 0;
2944 nla_put_failure:
2945 nlmsg_cancel(skb, nlh);
2946 return -EMSGSIZE;
2948 EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2950 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2952 struct net *net = sock_net(skb->sk);
2953 struct net_device *dev;
2954 int idx = 0;
2955 u32 portid = NETLINK_CB(cb->skb).portid;
2956 u32 seq = cb->nlh->nlmsg_seq;
2957 u32 filter_mask = 0;
2959 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2960 struct nlattr *extfilt;
2962 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2963 IFLA_EXT_MASK);
2964 if (extfilt) {
2965 if (nla_len(extfilt) < sizeof(filter_mask))
2966 return -EINVAL;
2968 filter_mask = nla_get_u32(extfilt);
2972 rcu_read_lock();
2973 for_each_netdev_rcu(net, dev) {
2974 const struct net_device_ops *ops = dev->netdev_ops;
2975 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2977 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2978 if (idx >= cb->args[0] &&
2979 br_dev->netdev_ops->ndo_bridge_getlink(
2980 skb, portid, seq, dev, filter_mask,
2981 NLM_F_MULTI) < 0)
2982 break;
2983 idx++;
2986 if (ops->ndo_bridge_getlink) {
2987 if (idx >= cb->args[0] &&
2988 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2989 filter_mask,
2990 NLM_F_MULTI) < 0)
2991 break;
2992 idx++;
2995 rcu_read_unlock();
2996 cb->args[0] = idx;
2998 return skb->len;
3001 static inline size_t bridge_nlmsg_size(void)
3003 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3004 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3005 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3006 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3007 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3008 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3009 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3010 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3011 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3012 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3013 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3016 static int rtnl_bridge_notify(struct net_device *dev)
3018 struct net *net = dev_net(dev);
3019 struct sk_buff *skb;
3020 int err = -EOPNOTSUPP;
3022 if (!dev->netdev_ops->ndo_bridge_getlink)
3023 return 0;
3025 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3026 if (!skb) {
3027 err = -ENOMEM;
3028 goto errout;
3031 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3032 if (err < 0)
3033 goto errout;
3035 if (!skb->len)
3036 goto errout;
3038 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3039 return 0;
3040 errout:
3041 WARN_ON(err == -EMSGSIZE);
3042 kfree_skb(skb);
3043 if (err)
3044 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3045 return err;
3048 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3050 struct net *net = sock_net(skb->sk);
3051 struct ifinfomsg *ifm;
3052 struct net_device *dev;
3053 struct nlattr *br_spec, *attr = NULL;
3054 int rem, err = -EOPNOTSUPP;
3055 u16 flags = 0;
3056 bool have_flags = false;
3058 if (nlmsg_len(nlh) < sizeof(*ifm))
3059 return -EINVAL;
3061 ifm = nlmsg_data(nlh);
3062 if (ifm->ifi_family != AF_BRIDGE)
3063 return -EPFNOSUPPORT;
3065 dev = __dev_get_by_index(net, ifm->ifi_index);
3066 if (!dev) {
3067 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3068 return -ENODEV;
3071 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3072 if (br_spec) {
3073 nla_for_each_nested(attr, br_spec, rem) {
3074 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3075 if (nla_len(attr) < sizeof(flags))
3076 return -EINVAL;
3078 have_flags = true;
3079 flags = nla_get_u16(attr);
3080 break;
3085 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3086 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3088 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3089 err = -EOPNOTSUPP;
3090 goto out;
3093 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3094 if (err)
3095 goto out;
3097 flags &= ~BRIDGE_FLAGS_MASTER;
3100 if ((flags & BRIDGE_FLAGS_SELF)) {
3101 if (!dev->netdev_ops->ndo_bridge_setlink)
3102 err = -EOPNOTSUPP;
3103 else
3104 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3105 flags);
3106 if (!err) {
3107 flags &= ~BRIDGE_FLAGS_SELF;
3109 /* Generate event to notify upper layer of bridge
3110 * change
3112 err = rtnl_bridge_notify(dev);
3116 if (have_flags)
3117 memcpy(nla_data(attr), &flags, sizeof(flags));
3118 out:
3119 return err;
3122 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3124 struct net *net = sock_net(skb->sk);
3125 struct ifinfomsg *ifm;
3126 struct net_device *dev;
3127 struct nlattr *br_spec, *attr = NULL;
3128 int rem, err = -EOPNOTSUPP;
3129 u16 flags = 0;
3130 bool have_flags = false;
3132 if (nlmsg_len(nlh) < sizeof(*ifm))
3133 return -EINVAL;
3135 ifm = nlmsg_data(nlh);
3136 if (ifm->ifi_family != AF_BRIDGE)
3137 return -EPFNOSUPPORT;
3139 dev = __dev_get_by_index(net, ifm->ifi_index);
3140 if (!dev) {
3141 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3142 return -ENODEV;
3145 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3146 if (br_spec) {
3147 nla_for_each_nested(attr, br_spec, rem) {
3148 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3149 if (nla_len(attr) < sizeof(flags))
3150 return -EINVAL;
3152 have_flags = true;
3153 flags = nla_get_u16(attr);
3154 break;
3159 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3160 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3162 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3163 err = -EOPNOTSUPP;
3164 goto out;
3167 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3168 if (err)
3169 goto out;
3171 flags &= ~BRIDGE_FLAGS_MASTER;
3174 if ((flags & BRIDGE_FLAGS_SELF)) {
3175 if (!dev->netdev_ops->ndo_bridge_dellink)
3176 err = -EOPNOTSUPP;
3177 else
3178 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3179 flags);
3181 if (!err) {
3182 flags &= ~BRIDGE_FLAGS_SELF;
3184 /* Generate event to notify upper layer of bridge
3185 * change
3187 err = rtnl_bridge_notify(dev);
3191 if (have_flags)
3192 memcpy(nla_data(attr), &flags, sizeof(flags));
3193 out:
3194 return err;
3197 /* Process one rtnetlink message. */
3199 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3201 struct net *net = sock_net(skb->sk);
3202 rtnl_doit_func doit;
3203 int sz_idx, kind;
3204 int family;
3205 int type;
3206 int err;
3208 type = nlh->nlmsg_type;
3209 if (type > RTM_MAX)
3210 return -EOPNOTSUPP;
3212 type -= RTM_BASE;
3214 /* All the messages must have at least 1 byte length */
3215 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3216 return 0;
3218 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3219 sz_idx = type>>2;
3220 kind = type&3;
3222 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3223 return -EPERM;
3225 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3226 struct sock *rtnl;
3227 rtnl_dumpit_func dumpit;
3228 rtnl_calcit_func calcit;
3229 u16 min_dump_alloc = 0;
3231 dumpit = rtnl_get_dumpit(family, type);
3232 if (dumpit == NULL)
3233 return -EOPNOTSUPP;
3234 calcit = rtnl_get_calcit(family, type);
3235 if (calcit)
3236 min_dump_alloc = calcit(skb, nlh);
3238 __rtnl_unlock();
3239 rtnl = net->rtnl;
3241 struct netlink_dump_control c = {
3242 .dump = dumpit,
3243 .min_dump_alloc = min_dump_alloc,
3245 err = netlink_dump_start(rtnl, skb, nlh, &c);
3247 rtnl_lock();
3248 return err;
3251 doit = rtnl_get_doit(family, type);
3252 if (doit == NULL)
3253 return -EOPNOTSUPP;
3255 return doit(skb, nlh);
3258 static void rtnetlink_rcv(struct sk_buff *skb)
3260 rtnl_lock();
3261 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3262 rtnl_unlock();
3265 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3267 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3269 switch (event) {
3270 case NETDEV_UP:
3271 case NETDEV_DOWN:
3272 case NETDEV_PRE_UP:
3273 case NETDEV_POST_INIT:
3274 case NETDEV_REGISTER:
3275 case NETDEV_CHANGE:
3276 case NETDEV_PRE_TYPE_CHANGE:
3277 case NETDEV_GOING_DOWN:
3278 case NETDEV_UNREGISTER:
3279 case NETDEV_UNREGISTER_FINAL:
3280 case NETDEV_RELEASE:
3281 case NETDEV_JOIN:
3282 case NETDEV_BONDING_INFO:
3283 break;
3284 default:
3285 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3286 break;
3288 return NOTIFY_DONE;
3291 static struct notifier_block rtnetlink_dev_notifier = {
3292 .notifier_call = rtnetlink_event,
3296 static int __net_init rtnetlink_net_init(struct net *net)
3298 struct sock *sk;
3299 struct netlink_kernel_cfg cfg = {
3300 .groups = RTNLGRP_MAX,
3301 .input = rtnetlink_rcv,
3302 .cb_mutex = &rtnl_mutex,
3303 .flags = NL_CFG_F_NONROOT_RECV,
3306 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3307 if (!sk)
3308 return -ENOMEM;
3309 net->rtnl = sk;
3310 return 0;
3313 static void __net_exit rtnetlink_net_exit(struct net *net)
3315 netlink_kernel_release(net->rtnl);
3316 net->rtnl = NULL;
3319 static struct pernet_operations rtnetlink_net_ops = {
3320 .init = rtnetlink_net_init,
3321 .exit = rtnetlink_net_exit,
3324 void __init rtnetlink_init(void)
3326 if (register_pernet_subsys(&rtnetlink_net_ops))
3327 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3329 register_netdevice_notifier(&rtnetlink_dev_notifier);
3331 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3332 rtnl_dump_ifinfo, rtnl_calcit);
3333 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3334 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3335 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3337 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3338 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3340 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3341 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3342 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3344 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3345 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3346 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);