Revert "ocfs2: bump up o2cb network protocol version"
[linux-2.6/btrfs-unstable.git] / net / core / rtnetlink.c
blob189cc78c77eba22b0023cc9598e719a308c77547
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 static struct sk_buff *defer_kfree_skb_list;
75 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
77 if (head && tail) {
78 tail->next = defer_kfree_skb_list;
79 defer_kfree_skb_list = head;
82 EXPORT_SYMBOL(rtnl_kfree_skbs);
84 void __rtnl_unlock(void)
86 struct sk_buff *head = defer_kfree_skb_list;
88 defer_kfree_skb_list = NULL;
90 mutex_unlock(&rtnl_mutex);
92 while (head) {
93 struct sk_buff *next = head->next;
95 kfree_skb(head);
96 cond_resched();
97 head = next;
101 void rtnl_unlock(void)
103 /* This fellow will unlock it for us. */
104 netdev_run_todo();
106 EXPORT_SYMBOL(rtnl_unlock);
108 int rtnl_trylock(void)
110 return mutex_trylock(&rtnl_mutex);
112 EXPORT_SYMBOL(rtnl_trylock);
114 int rtnl_is_locked(void)
116 return mutex_is_locked(&rtnl_mutex);
118 EXPORT_SYMBOL(rtnl_is_locked);
120 #ifdef CONFIG_PROVE_LOCKING
121 bool lockdep_rtnl_is_held(void)
123 return lockdep_is_held(&rtnl_mutex);
125 EXPORT_SYMBOL(lockdep_rtnl_is_held);
126 #endif /* #ifdef CONFIG_PROVE_LOCKING */
128 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
130 static inline int rtm_msgindex(int msgtype)
132 int msgindex = msgtype - RTM_BASE;
135 * msgindex < 0 implies someone tried to register a netlink
136 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
137 * the message type has not been added to linux/rtnetlink.h
139 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
141 return msgindex;
144 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
146 struct rtnl_link *tab;
148 if (protocol <= RTNL_FAMILY_MAX)
149 tab = rtnl_msg_handlers[protocol];
150 else
151 tab = NULL;
153 if (tab == NULL || tab[msgindex].doit == NULL)
154 tab = rtnl_msg_handlers[PF_UNSPEC];
156 return tab[msgindex].doit;
159 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
161 struct rtnl_link *tab;
163 if (protocol <= RTNL_FAMILY_MAX)
164 tab = rtnl_msg_handlers[protocol];
165 else
166 tab = NULL;
168 if (tab == NULL || tab[msgindex].dumpit == NULL)
169 tab = rtnl_msg_handlers[PF_UNSPEC];
171 return tab[msgindex].dumpit;
174 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
176 struct rtnl_link *tab;
178 if (protocol <= RTNL_FAMILY_MAX)
179 tab = rtnl_msg_handlers[protocol];
180 else
181 tab = NULL;
183 if (tab == NULL || tab[msgindex].calcit == NULL)
184 tab = rtnl_msg_handlers[PF_UNSPEC];
186 return tab[msgindex].calcit;
190 * __rtnl_register - Register a rtnetlink message type
191 * @protocol: Protocol family or PF_UNSPEC
192 * @msgtype: rtnetlink message type
193 * @doit: Function pointer called for each request message
194 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
195 * @calcit: Function pointer to calc size of dump message
197 * Registers the specified function pointers (at least one of them has
198 * to be non-NULL) to be called whenever a request message for the
199 * specified protocol family and message type is received.
201 * The special protocol family PF_UNSPEC may be used to define fallback
202 * function pointers for the case when no entry for the specific protocol
203 * family exists.
205 * Returns 0 on success or a negative error code.
207 int __rtnl_register(int protocol, int msgtype,
208 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
209 rtnl_calcit_func calcit)
211 struct rtnl_link *tab;
212 int msgindex;
214 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
215 msgindex = rtm_msgindex(msgtype);
217 tab = rtnl_msg_handlers[protocol];
218 if (tab == NULL) {
219 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
220 if (tab == NULL)
221 return -ENOBUFS;
223 rtnl_msg_handlers[protocol] = tab;
226 if (doit)
227 tab[msgindex].doit = doit;
229 if (dumpit)
230 tab[msgindex].dumpit = dumpit;
232 if (calcit)
233 tab[msgindex].calcit = calcit;
235 return 0;
237 EXPORT_SYMBOL_GPL(__rtnl_register);
240 * rtnl_register - Register a rtnetlink message type
242 * Identical to __rtnl_register() but panics on failure. This is useful
243 * as failure of this function is very unlikely, it can only happen due
244 * to lack of memory when allocating the chain to store all message
245 * handlers for a protocol. Meant for use in init functions where lack
246 * of memory implies no sense in continuing.
248 void rtnl_register(int protocol, int msgtype,
249 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
250 rtnl_calcit_func calcit)
252 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
253 panic("Unable to register rtnetlink message handler, "
254 "protocol = %d, message type = %d\n",
255 protocol, msgtype);
257 EXPORT_SYMBOL_GPL(rtnl_register);
260 * rtnl_unregister - Unregister a rtnetlink message type
261 * @protocol: Protocol family or PF_UNSPEC
262 * @msgtype: rtnetlink message type
264 * Returns 0 on success or a negative error code.
266 int rtnl_unregister(int protocol, int msgtype)
268 int msgindex;
270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
271 msgindex = rtm_msgindex(msgtype);
273 if (rtnl_msg_handlers[protocol] == NULL)
274 return -ENOENT;
276 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
277 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
279 return 0;
281 EXPORT_SYMBOL_GPL(rtnl_unregister);
284 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
285 * @protocol : Protocol family or PF_UNSPEC
287 * Identical to calling rtnl_unregster() for all registered message types
288 * of a certain protocol family.
290 void rtnl_unregister_all(int protocol)
292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
294 kfree(rtnl_msg_handlers[protocol]);
295 rtnl_msg_handlers[protocol] = NULL;
297 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
299 static LIST_HEAD(link_ops);
301 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
303 const struct rtnl_link_ops *ops;
305 list_for_each_entry(ops, &link_ops, list) {
306 if (!strcmp(ops->kind, kind))
307 return ops;
309 return NULL;
313 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
316 * The caller must hold the rtnl_mutex. This function should be used
317 * by drivers that create devices during module initialization. It
318 * must be called before registering the devices.
320 * Returns 0 on success or a negative error code.
322 int __rtnl_link_register(struct rtnl_link_ops *ops)
324 if (rtnl_link_ops_get(ops->kind))
325 return -EEXIST;
327 /* The check for setup is here because if ops
328 * does not have that filled up, it is not possible
329 * to use the ops for creating device. So do not
330 * fill up dellink as well. That disables rtnl_dellink.
332 if (ops->setup && !ops->dellink)
333 ops->dellink = unregister_netdevice_queue;
335 list_add_tail(&ops->list, &link_ops);
336 return 0;
338 EXPORT_SYMBOL_GPL(__rtnl_link_register);
341 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
342 * @ops: struct rtnl_link_ops * to register
344 * Returns 0 on success or a negative error code.
346 int rtnl_link_register(struct rtnl_link_ops *ops)
348 int err;
350 rtnl_lock();
351 err = __rtnl_link_register(ops);
352 rtnl_unlock();
353 return err;
355 EXPORT_SYMBOL_GPL(rtnl_link_register);
357 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
359 struct net_device *dev;
360 LIST_HEAD(list_kill);
362 for_each_netdev(net, dev) {
363 if (dev->rtnl_link_ops == ops)
364 ops->dellink(dev, &list_kill);
366 unregister_netdevice_many(&list_kill);
370 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
371 * @ops: struct rtnl_link_ops * to unregister
373 * The caller must hold the rtnl_mutex.
375 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
377 struct net *net;
379 for_each_net(net) {
380 __rtnl_kill_links(net, ops);
382 list_del(&ops->list);
384 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
386 /* Return with the rtnl_lock held when there are no network
387 * devices unregistering in any network namespace.
389 static void rtnl_lock_unregistering_all(void)
391 struct net *net;
392 bool unregistering;
393 DEFINE_WAIT_FUNC(wait, woken_wake_function);
395 add_wait_queue(&netdev_unregistering_wq, &wait);
396 for (;;) {
397 unregistering = false;
398 rtnl_lock();
399 for_each_net(net) {
400 if (net->dev_unreg_count > 0) {
401 unregistering = true;
402 break;
405 if (!unregistering)
406 break;
407 __rtnl_unlock();
409 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
411 remove_wait_queue(&netdev_unregistering_wq, &wait);
415 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
416 * @ops: struct rtnl_link_ops * to unregister
418 void rtnl_link_unregister(struct rtnl_link_ops *ops)
420 /* Close the race with cleanup_net() */
421 mutex_lock(&net_mutex);
422 rtnl_lock_unregistering_all();
423 __rtnl_link_unregister(ops);
424 rtnl_unlock();
425 mutex_unlock(&net_mutex);
427 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
429 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
431 struct net_device *master_dev;
432 const struct rtnl_link_ops *ops;
434 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
435 if (!master_dev)
436 return 0;
437 ops = master_dev->rtnl_link_ops;
438 if (!ops || !ops->get_slave_size)
439 return 0;
440 /* IFLA_INFO_SLAVE_DATA + nested data */
441 return nla_total_size(sizeof(struct nlattr)) +
442 ops->get_slave_size(master_dev, dev);
445 static size_t rtnl_link_get_size(const struct net_device *dev)
447 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
448 size_t size;
450 if (!ops)
451 return 0;
453 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
454 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
456 if (ops->get_size)
457 /* IFLA_INFO_DATA + nested data */
458 size += nla_total_size(sizeof(struct nlattr)) +
459 ops->get_size(dev);
461 if (ops->get_xstats_size)
462 /* IFLA_INFO_XSTATS */
463 size += nla_total_size(ops->get_xstats_size(dev));
465 size += rtnl_link_get_slave_info_data_size(dev);
467 return size;
470 static LIST_HEAD(rtnl_af_ops);
472 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
474 const struct rtnl_af_ops *ops;
476 list_for_each_entry(ops, &rtnl_af_ops, list) {
477 if (ops->family == family)
478 return ops;
481 return NULL;
485 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
486 * @ops: struct rtnl_af_ops * to register
488 * Returns 0 on success or a negative error code.
490 void rtnl_af_register(struct rtnl_af_ops *ops)
492 rtnl_lock();
493 list_add_tail(&ops->list, &rtnl_af_ops);
494 rtnl_unlock();
496 EXPORT_SYMBOL_GPL(rtnl_af_register);
499 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
500 * @ops: struct rtnl_af_ops * to unregister
502 * The caller must hold the rtnl_mutex.
504 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
506 list_del(&ops->list);
508 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
511 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
512 * @ops: struct rtnl_af_ops * to unregister
514 void rtnl_af_unregister(struct rtnl_af_ops *ops)
516 rtnl_lock();
517 __rtnl_af_unregister(ops);
518 rtnl_unlock();
520 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
522 static size_t rtnl_link_get_af_size(const struct net_device *dev,
523 u32 ext_filter_mask)
525 struct rtnl_af_ops *af_ops;
526 size_t size;
528 /* IFLA_AF_SPEC */
529 size = nla_total_size(sizeof(struct nlattr));
531 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
532 if (af_ops->get_link_af_size) {
533 /* AF_* + nested data */
534 size += nla_total_size(sizeof(struct nlattr)) +
535 af_ops->get_link_af_size(dev, ext_filter_mask);
539 return size;
542 static bool rtnl_have_link_slave_info(const struct net_device *dev)
544 struct net_device *master_dev;
546 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
547 if (master_dev && master_dev->rtnl_link_ops)
548 return true;
549 return false;
552 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
553 const struct net_device *dev)
555 struct net_device *master_dev;
556 const struct rtnl_link_ops *ops;
557 struct nlattr *slave_data;
558 int err;
560 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
561 if (!master_dev)
562 return 0;
563 ops = master_dev->rtnl_link_ops;
564 if (!ops)
565 return 0;
566 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
567 return -EMSGSIZE;
568 if (ops->fill_slave_info) {
569 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
570 if (!slave_data)
571 return -EMSGSIZE;
572 err = ops->fill_slave_info(skb, master_dev, dev);
573 if (err < 0)
574 goto err_cancel_slave_data;
575 nla_nest_end(skb, slave_data);
577 return 0;
579 err_cancel_slave_data:
580 nla_nest_cancel(skb, slave_data);
581 return err;
584 static int rtnl_link_info_fill(struct sk_buff *skb,
585 const struct net_device *dev)
587 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
588 struct nlattr *data;
589 int err;
591 if (!ops)
592 return 0;
593 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
594 return -EMSGSIZE;
595 if (ops->fill_xstats) {
596 err = ops->fill_xstats(skb, dev);
597 if (err < 0)
598 return err;
600 if (ops->fill_info) {
601 data = nla_nest_start(skb, IFLA_INFO_DATA);
602 if (data == NULL)
603 return -EMSGSIZE;
604 err = ops->fill_info(skb, dev);
605 if (err < 0)
606 goto err_cancel_data;
607 nla_nest_end(skb, data);
609 return 0;
611 err_cancel_data:
612 nla_nest_cancel(skb, data);
613 return err;
616 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
618 struct nlattr *linkinfo;
619 int err = -EMSGSIZE;
621 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
622 if (linkinfo == NULL)
623 goto out;
625 err = rtnl_link_info_fill(skb, dev);
626 if (err < 0)
627 goto err_cancel_link;
629 err = rtnl_link_slave_info_fill(skb, dev);
630 if (err < 0)
631 goto err_cancel_link;
633 nla_nest_end(skb, linkinfo);
634 return 0;
636 err_cancel_link:
637 nla_nest_cancel(skb, linkinfo);
638 out:
639 return err;
642 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
644 struct sock *rtnl = net->rtnl;
645 int err = 0;
647 NETLINK_CB(skb).dst_group = group;
648 if (echo)
649 atomic_inc(&skb->users);
650 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
651 if (echo)
652 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
653 return err;
656 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
658 struct sock *rtnl = net->rtnl;
660 return nlmsg_unicast(rtnl, skb, pid);
662 EXPORT_SYMBOL(rtnl_unicast);
664 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
665 struct nlmsghdr *nlh, gfp_t flags)
667 struct sock *rtnl = net->rtnl;
668 int report = 0;
670 if (nlh)
671 report = nlmsg_report(nlh);
673 nlmsg_notify(rtnl, skb, pid, group, report, flags);
675 EXPORT_SYMBOL(rtnl_notify);
677 void rtnl_set_sk_err(struct net *net, u32 group, int error)
679 struct sock *rtnl = net->rtnl;
681 netlink_set_err(rtnl, 0, group, error);
683 EXPORT_SYMBOL(rtnl_set_sk_err);
685 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
687 struct nlattr *mx;
688 int i, valid = 0;
690 mx = nla_nest_start(skb, RTA_METRICS);
691 if (mx == NULL)
692 return -ENOBUFS;
694 for (i = 0; i < RTAX_MAX; i++) {
695 if (metrics[i]) {
696 if (i == RTAX_CC_ALGO - 1) {
697 char tmp[TCP_CA_NAME_MAX], *name;
699 name = tcp_ca_get_name_by_key(metrics[i], tmp);
700 if (!name)
701 continue;
702 if (nla_put_string(skb, i + 1, name))
703 goto nla_put_failure;
704 } else if (i == RTAX_FEATURES - 1) {
705 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
707 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
708 if (nla_put_u32(skb, i + 1, user_features))
709 goto nla_put_failure;
710 } else {
711 if (nla_put_u32(skb, i + 1, metrics[i]))
712 goto nla_put_failure;
714 valid++;
718 if (!valid) {
719 nla_nest_cancel(skb, mx);
720 return 0;
723 return nla_nest_end(skb, mx);
725 nla_put_failure:
726 nla_nest_cancel(skb, mx);
727 return -EMSGSIZE;
729 EXPORT_SYMBOL(rtnetlink_put_metrics);
731 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
732 long expires, u32 error)
734 struct rta_cacheinfo ci = {
735 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
736 .rta_used = dst->__use,
737 .rta_clntref = atomic_read(&(dst->__refcnt)),
738 .rta_error = error,
739 .rta_id = id,
742 if (expires) {
743 unsigned long clock;
745 clock = jiffies_to_clock_t(abs(expires));
746 clock = min_t(unsigned long, clock, INT_MAX);
747 ci.rta_expires = (expires > 0) ? clock : -clock;
749 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
751 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
753 static void set_operstate(struct net_device *dev, unsigned char transition)
755 unsigned char operstate = dev->operstate;
757 switch (transition) {
758 case IF_OPER_UP:
759 if ((operstate == IF_OPER_DORMANT ||
760 operstate == IF_OPER_UNKNOWN) &&
761 !netif_dormant(dev))
762 operstate = IF_OPER_UP;
763 break;
765 case IF_OPER_DORMANT:
766 if (operstate == IF_OPER_UP ||
767 operstate == IF_OPER_UNKNOWN)
768 operstate = IF_OPER_DORMANT;
769 break;
772 if (dev->operstate != operstate) {
773 write_lock_bh(&dev_base_lock);
774 dev->operstate = operstate;
775 write_unlock_bh(&dev_base_lock);
776 netdev_state_change(dev);
780 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
782 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
783 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
786 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
787 const struct ifinfomsg *ifm)
789 unsigned int flags = ifm->ifi_flags;
791 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
792 if (ifm->ifi_change)
793 flags = (flags & ifm->ifi_change) |
794 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
796 return flags;
799 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
800 const struct rtnl_link_stats64 *b)
802 a->rx_packets = b->rx_packets;
803 a->tx_packets = b->tx_packets;
804 a->rx_bytes = b->rx_bytes;
805 a->tx_bytes = b->tx_bytes;
806 a->rx_errors = b->rx_errors;
807 a->tx_errors = b->tx_errors;
808 a->rx_dropped = b->rx_dropped;
809 a->tx_dropped = b->tx_dropped;
811 a->multicast = b->multicast;
812 a->collisions = b->collisions;
814 a->rx_length_errors = b->rx_length_errors;
815 a->rx_over_errors = b->rx_over_errors;
816 a->rx_crc_errors = b->rx_crc_errors;
817 a->rx_frame_errors = b->rx_frame_errors;
818 a->rx_fifo_errors = b->rx_fifo_errors;
819 a->rx_missed_errors = b->rx_missed_errors;
821 a->tx_aborted_errors = b->tx_aborted_errors;
822 a->tx_carrier_errors = b->tx_carrier_errors;
823 a->tx_fifo_errors = b->tx_fifo_errors;
824 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
825 a->tx_window_errors = b->tx_window_errors;
827 a->rx_compressed = b->rx_compressed;
828 a->tx_compressed = b->tx_compressed;
830 a->rx_nohandler = b->rx_nohandler;
833 /* All VF info */
834 static inline int rtnl_vfinfo_size(const struct net_device *dev,
835 u32 ext_filter_mask)
837 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
838 (ext_filter_mask & RTEXT_FILTER_VF)) {
839 int num_vfs = dev_num_vf(dev->dev.parent);
840 size_t size = nla_total_size(sizeof(struct nlattr));
841 size += nla_total_size(num_vfs * sizeof(struct nlattr));
842 size += num_vfs *
843 (nla_total_size(sizeof(struct ifla_vf_mac)) +
844 nla_total_size(sizeof(struct ifla_vf_vlan)) +
845 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
846 nla_total_size(sizeof(struct ifla_vf_rate)) +
847 nla_total_size(sizeof(struct ifla_vf_link_state)) +
848 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
849 /* IFLA_VF_STATS_RX_PACKETS */
850 nla_total_size_64bit(sizeof(__u64)) +
851 /* IFLA_VF_STATS_TX_PACKETS */
852 nla_total_size_64bit(sizeof(__u64)) +
853 /* IFLA_VF_STATS_RX_BYTES */
854 nla_total_size_64bit(sizeof(__u64)) +
855 /* IFLA_VF_STATS_TX_BYTES */
856 nla_total_size_64bit(sizeof(__u64)) +
857 /* IFLA_VF_STATS_BROADCAST */
858 nla_total_size_64bit(sizeof(__u64)) +
859 /* IFLA_VF_STATS_MULTICAST */
860 nla_total_size_64bit(sizeof(__u64)) +
861 nla_total_size(sizeof(struct ifla_vf_trust)));
862 return size;
863 } else
864 return 0;
867 static size_t rtnl_port_size(const struct net_device *dev,
868 u32 ext_filter_mask)
870 size_t port_size = nla_total_size(4) /* PORT_VF */
871 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
872 + nla_total_size(sizeof(struct ifla_port_vsi))
873 /* PORT_VSI_TYPE */
874 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
875 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
876 + nla_total_size(1) /* PROT_VDP_REQUEST */
877 + nla_total_size(2); /* PORT_VDP_RESPONSE */
878 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
879 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
880 + port_size;
881 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
882 + port_size;
884 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
885 !(ext_filter_mask & RTEXT_FILTER_VF))
886 return 0;
887 if (dev_num_vf(dev->dev.parent))
888 return port_self_size + vf_ports_size +
889 vf_port_size * dev_num_vf(dev->dev.parent);
890 else
891 return port_self_size;
894 static size_t rtnl_xdp_size(const struct net_device *dev)
896 size_t xdp_size = nla_total_size(1); /* XDP_ATTACHED */
898 if (!dev->netdev_ops->ndo_xdp)
899 return 0;
900 else
901 return xdp_size;
904 static noinline size_t if_nlmsg_size(const struct net_device *dev,
905 u32 ext_filter_mask)
907 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
908 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
909 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
910 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
911 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
912 + nla_total_size(sizeof(struct rtnl_link_stats))
913 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
914 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
915 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
916 + nla_total_size(4) /* IFLA_TXQLEN */
917 + nla_total_size(4) /* IFLA_WEIGHT */
918 + nla_total_size(4) /* IFLA_MTU */
919 + nla_total_size(4) /* IFLA_LINK */
920 + nla_total_size(4) /* IFLA_MASTER */
921 + nla_total_size(1) /* IFLA_CARRIER */
922 + nla_total_size(4) /* IFLA_PROMISCUITY */
923 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
924 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
925 + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
926 + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
927 + nla_total_size(1) /* IFLA_OPERSTATE */
928 + nla_total_size(1) /* IFLA_LINKMODE */
929 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
930 + nla_total_size(4) /* IFLA_LINK_NETNSID */
931 + nla_total_size(ext_filter_mask
932 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
933 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
934 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
935 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
936 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
937 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
938 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
939 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
940 + rtnl_xdp_size(dev) /* IFLA_XDP */
941 + nla_total_size(1); /* IFLA_PROTO_DOWN */
945 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
947 struct nlattr *vf_ports;
948 struct nlattr *vf_port;
949 int vf;
950 int err;
952 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
953 if (!vf_ports)
954 return -EMSGSIZE;
956 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
957 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
958 if (!vf_port)
959 goto nla_put_failure;
960 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
961 goto nla_put_failure;
962 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
963 if (err == -EMSGSIZE)
964 goto nla_put_failure;
965 if (err) {
966 nla_nest_cancel(skb, vf_port);
967 continue;
969 nla_nest_end(skb, vf_port);
972 nla_nest_end(skb, vf_ports);
974 return 0;
976 nla_put_failure:
977 nla_nest_cancel(skb, vf_ports);
978 return -EMSGSIZE;
981 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
983 struct nlattr *port_self;
984 int err;
986 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
987 if (!port_self)
988 return -EMSGSIZE;
990 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
991 if (err) {
992 nla_nest_cancel(skb, port_self);
993 return (err == -EMSGSIZE) ? err : 0;
996 nla_nest_end(skb, port_self);
998 return 0;
1001 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1002 u32 ext_filter_mask)
1004 int err;
1006 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1007 !(ext_filter_mask & RTEXT_FILTER_VF))
1008 return 0;
1010 err = rtnl_port_self_fill(skb, dev);
1011 if (err)
1012 return err;
1014 if (dev_num_vf(dev->dev.parent)) {
1015 err = rtnl_vf_ports_fill(skb, dev);
1016 if (err)
1017 return err;
1020 return 0;
1023 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1025 int err;
1026 struct netdev_phys_item_id ppid;
1028 err = dev_get_phys_port_id(dev, &ppid);
1029 if (err) {
1030 if (err == -EOPNOTSUPP)
1031 return 0;
1032 return err;
1035 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1036 return -EMSGSIZE;
1038 return 0;
1041 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1043 char name[IFNAMSIZ];
1044 int err;
1046 err = dev_get_phys_port_name(dev, name, sizeof(name));
1047 if (err) {
1048 if (err == -EOPNOTSUPP)
1049 return 0;
1050 return err;
1053 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1054 return -EMSGSIZE;
1056 return 0;
1059 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1061 int err;
1062 struct switchdev_attr attr = {
1063 .orig_dev = dev,
1064 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1065 .flags = SWITCHDEV_F_NO_RECURSE,
1068 err = switchdev_port_attr_get(dev, &attr);
1069 if (err) {
1070 if (err == -EOPNOTSUPP)
1071 return 0;
1072 return err;
1075 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1076 attr.u.ppid.id))
1077 return -EMSGSIZE;
1079 return 0;
1082 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1083 struct net_device *dev)
1085 struct rtnl_link_stats64 *sp;
1086 struct nlattr *attr;
1088 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1089 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1090 if (!attr)
1091 return -EMSGSIZE;
1093 sp = nla_data(attr);
1094 dev_get_stats(dev, sp);
1096 attr = nla_reserve(skb, IFLA_STATS,
1097 sizeof(struct rtnl_link_stats));
1098 if (!attr)
1099 return -EMSGSIZE;
1101 copy_rtnl_link_stats(nla_data(attr), sp);
1103 return 0;
1106 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1107 struct net_device *dev,
1108 int vfs_num,
1109 struct nlattr *vfinfo)
1111 struct ifla_vf_rss_query_en vf_rss_query_en;
1112 struct ifla_vf_link_state vf_linkstate;
1113 struct ifla_vf_spoofchk vf_spoofchk;
1114 struct ifla_vf_tx_rate vf_tx_rate;
1115 struct ifla_vf_stats vf_stats;
1116 struct ifla_vf_trust vf_trust;
1117 struct ifla_vf_vlan vf_vlan;
1118 struct ifla_vf_rate vf_rate;
1119 struct nlattr *vf, *vfstats;
1120 struct ifla_vf_mac vf_mac;
1121 struct ifla_vf_info ivi;
1123 /* Not all SR-IOV capable drivers support the
1124 * spoofcheck and "RSS query enable" query. Preset to
1125 * -1 so the user space tool can detect that the driver
1126 * didn't report anything.
1128 ivi.spoofchk = -1;
1129 ivi.rss_query_en = -1;
1130 ivi.trusted = -1;
1131 memset(ivi.mac, 0, sizeof(ivi.mac));
1132 /* The default value for VF link state is "auto"
1133 * IFLA_VF_LINK_STATE_AUTO which equals zero
1135 ivi.linkstate = 0;
1136 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1137 return 0;
1139 vf_mac.vf =
1140 vf_vlan.vf =
1141 vf_rate.vf =
1142 vf_tx_rate.vf =
1143 vf_spoofchk.vf =
1144 vf_linkstate.vf =
1145 vf_rss_query_en.vf =
1146 vf_trust.vf = ivi.vf;
1148 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1149 vf_vlan.vlan = ivi.vlan;
1150 vf_vlan.qos = ivi.qos;
1151 vf_tx_rate.rate = ivi.max_tx_rate;
1152 vf_rate.min_tx_rate = ivi.min_tx_rate;
1153 vf_rate.max_tx_rate = ivi.max_tx_rate;
1154 vf_spoofchk.setting = ivi.spoofchk;
1155 vf_linkstate.link_state = ivi.linkstate;
1156 vf_rss_query_en.setting = ivi.rss_query_en;
1157 vf_trust.setting = ivi.trusted;
1158 vf = nla_nest_start(skb, IFLA_VF_INFO);
1159 if (!vf) {
1160 nla_nest_cancel(skb, vfinfo);
1161 return -EMSGSIZE;
1163 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1164 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1165 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1166 &vf_rate) ||
1167 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1168 &vf_tx_rate) ||
1169 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1170 &vf_spoofchk) ||
1171 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1172 &vf_linkstate) ||
1173 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1174 sizeof(vf_rss_query_en),
1175 &vf_rss_query_en) ||
1176 nla_put(skb, IFLA_VF_TRUST,
1177 sizeof(vf_trust), &vf_trust))
1178 return -EMSGSIZE;
1179 memset(&vf_stats, 0, sizeof(vf_stats));
1180 if (dev->netdev_ops->ndo_get_vf_stats)
1181 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1182 &vf_stats);
1183 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1184 if (!vfstats) {
1185 nla_nest_cancel(skb, vf);
1186 nla_nest_cancel(skb, vfinfo);
1187 return -EMSGSIZE;
1189 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1190 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1191 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1192 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1193 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1194 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1195 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1196 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1197 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1198 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1199 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1200 vf_stats.multicast, IFLA_VF_STATS_PAD))
1201 return -EMSGSIZE;
1202 nla_nest_end(skb, vfstats);
1203 nla_nest_end(skb, vf);
1204 return 0;
1207 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1209 struct rtnl_link_ifmap map;
1211 memset(&map, 0, sizeof(map));
1212 map.mem_start = dev->mem_start;
1213 map.mem_end = dev->mem_end;
1214 map.base_addr = dev->base_addr;
1215 map.irq = dev->irq;
1216 map.dma = dev->dma;
1217 map.port = dev->if_port;
1219 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1220 return -EMSGSIZE;
1222 return 0;
1225 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1227 struct netdev_xdp xdp_op = {};
1228 struct nlattr *xdp;
1229 int err;
1231 if (!dev->netdev_ops->ndo_xdp)
1232 return 0;
1233 xdp = nla_nest_start(skb, IFLA_XDP);
1234 if (!xdp)
1235 return -EMSGSIZE;
1236 xdp_op.command = XDP_QUERY_PROG;
1237 err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
1238 if (err)
1239 goto err_cancel;
1240 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
1241 if (err)
1242 goto err_cancel;
1244 nla_nest_end(skb, xdp);
1245 return 0;
1247 err_cancel:
1248 nla_nest_cancel(skb, xdp);
1249 return err;
1252 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1253 int type, u32 pid, u32 seq, u32 change,
1254 unsigned int flags, u32 ext_filter_mask)
1256 struct ifinfomsg *ifm;
1257 struct nlmsghdr *nlh;
1258 struct nlattr *af_spec;
1259 struct rtnl_af_ops *af_ops;
1260 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1262 ASSERT_RTNL();
1263 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1264 if (nlh == NULL)
1265 return -EMSGSIZE;
1267 ifm = nlmsg_data(nlh);
1268 ifm->ifi_family = AF_UNSPEC;
1269 ifm->__ifi_pad = 0;
1270 ifm->ifi_type = dev->type;
1271 ifm->ifi_index = dev->ifindex;
1272 ifm->ifi_flags = dev_get_flags(dev);
1273 ifm->ifi_change = change;
1275 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1276 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1277 nla_put_u8(skb, IFLA_OPERSTATE,
1278 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1279 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1280 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1281 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1282 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1283 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1284 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1285 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1286 #ifdef CONFIG_RPS
1287 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1288 #endif
1289 (dev->ifindex != dev_get_iflink(dev) &&
1290 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1291 (upper_dev &&
1292 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1293 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1294 (dev->qdisc &&
1295 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1296 (dev->ifalias &&
1297 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1298 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1299 atomic_read(&dev->carrier_changes)) ||
1300 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1301 goto nla_put_failure;
1303 if (rtnl_fill_link_ifmap(skb, dev))
1304 goto nla_put_failure;
1306 if (dev->addr_len) {
1307 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1308 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1309 goto nla_put_failure;
1312 if (rtnl_phys_port_id_fill(skb, dev))
1313 goto nla_put_failure;
1315 if (rtnl_phys_port_name_fill(skb, dev))
1316 goto nla_put_failure;
1318 if (rtnl_phys_switch_id_fill(skb, dev))
1319 goto nla_put_failure;
1321 if (rtnl_fill_stats(skb, dev))
1322 goto nla_put_failure;
1324 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1325 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1326 goto nla_put_failure;
1328 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1329 ext_filter_mask & RTEXT_FILTER_VF) {
1330 int i;
1331 struct nlattr *vfinfo;
1332 int num_vfs = dev_num_vf(dev->dev.parent);
1334 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1335 if (!vfinfo)
1336 goto nla_put_failure;
1337 for (i = 0; i < num_vfs; i++) {
1338 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1339 goto nla_put_failure;
1342 nla_nest_end(skb, vfinfo);
1345 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1346 goto nla_put_failure;
1348 if (rtnl_xdp_fill(skb, dev))
1349 goto nla_put_failure;
1351 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1352 if (rtnl_link_fill(skb, dev) < 0)
1353 goto nla_put_failure;
1356 if (dev->rtnl_link_ops &&
1357 dev->rtnl_link_ops->get_link_net) {
1358 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1360 if (!net_eq(dev_net(dev), link_net)) {
1361 int id = peernet2id_alloc(dev_net(dev), link_net);
1363 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1364 goto nla_put_failure;
1368 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1369 goto nla_put_failure;
1371 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1372 if (af_ops->fill_link_af) {
1373 struct nlattr *af;
1374 int err;
1376 if (!(af = nla_nest_start(skb, af_ops->family)))
1377 goto nla_put_failure;
1379 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1382 * Caller may return ENODATA to indicate that there
1383 * was no data to be dumped. This is not an error, it
1384 * means we should trim the attribute header and
1385 * continue.
1387 if (err == -ENODATA)
1388 nla_nest_cancel(skb, af);
1389 else if (err < 0)
1390 goto nla_put_failure;
1392 nla_nest_end(skb, af);
1396 nla_nest_end(skb, af_spec);
1398 nlmsg_end(skb, nlh);
1399 return 0;
1401 nla_put_failure:
1402 nlmsg_cancel(skb, nlh);
1403 return -EMSGSIZE;
1406 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1407 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1408 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1409 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1410 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1411 [IFLA_MTU] = { .type = NLA_U32 },
1412 [IFLA_LINK] = { .type = NLA_U32 },
1413 [IFLA_MASTER] = { .type = NLA_U32 },
1414 [IFLA_CARRIER] = { .type = NLA_U8 },
1415 [IFLA_TXQLEN] = { .type = NLA_U32 },
1416 [IFLA_WEIGHT] = { .type = NLA_U32 },
1417 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1418 [IFLA_LINKMODE] = { .type = NLA_U8 },
1419 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1420 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1421 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1422 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1423 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1424 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1425 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1426 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1427 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1428 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1429 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1430 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1431 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1432 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1433 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1434 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1435 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1436 [IFLA_XDP] = { .type = NLA_NESTED },
1439 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1440 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1441 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1442 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1443 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1446 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1447 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1448 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1449 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1450 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1451 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1452 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1453 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1454 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1455 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1456 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1457 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1460 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1461 [IFLA_PORT_VF] = { .type = NLA_U32 },
1462 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1463 .len = PORT_PROFILE_MAX },
1464 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1465 .len = sizeof(struct ifla_port_vsi)},
1466 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1467 .len = PORT_UUID_MAX },
1468 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1469 .len = PORT_UUID_MAX },
1470 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1471 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1474 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1475 [IFLA_XDP_FD] = { .type = NLA_S32 },
1476 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1479 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1481 const struct rtnl_link_ops *ops = NULL;
1482 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1484 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1485 return NULL;
1487 if (linfo[IFLA_INFO_KIND]) {
1488 char kind[MODULE_NAME_LEN];
1490 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1491 ops = rtnl_link_ops_get(kind);
1494 return ops;
1497 static bool link_master_filtered(struct net_device *dev, int master_idx)
1499 struct net_device *master;
1501 if (!master_idx)
1502 return false;
1504 master = netdev_master_upper_dev_get(dev);
1505 if (!master || master->ifindex != master_idx)
1506 return true;
1508 return false;
1511 static bool link_kind_filtered(const struct net_device *dev,
1512 const struct rtnl_link_ops *kind_ops)
1514 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1515 return true;
1517 return false;
1520 static bool link_dump_filtered(struct net_device *dev,
1521 int master_idx,
1522 const struct rtnl_link_ops *kind_ops)
1524 if (link_master_filtered(dev, master_idx) ||
1525 link_kind_filtered(dev, kind_ops))
1526 return true;
1528 return false;
1531 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1533 struct net *net = sock_net(skb->sk);
1534 int h, s_h;
1535 int idx = 0, s_idx;
1536 struct net_device *dev;
1537 struct hlist_head *head;
1538 struct nlattr *tb[IFLA_MAX+1];
1539 u32 ext_filter_mask = 0;
1540 const struct rtnl_link_ops *kind_ops = NULL;
1541 unsigned int flags = NLM_F_MULTI;
1542 int master_idx = 0;
1543 int err;
1544 int hdrlen;
1546 s_h = cb->args[0];
1547 s_idx = cb->args[1];
1549 cb->seq = net->dev_base_seq;
1551 /* A hack to preserve kernel<->userspace interface.
1552 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1553 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1554 * what iproute2 < v3.9.0 used.
1555 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1556 * attribute, its netlink message is shorter than struct ifinfomsg.
1558 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1559 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1561 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1563 if (tb[IFLA_EXT_MASK])
1564 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1566 if (tb[IFLA_MASTER])
1567 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1569 if (tb[IFLA_LINKINFO])
1570 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1572 if (master_idx || kind_ops)
1573 flags |= NLM_F_DUMP_FILTERED;
1576 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1577 idx = 0;
1578 head = &net->dev_index_head[h];
1579 hlist_for_each_entry(dev, head, index_hlist) {
1580 if (link_dump_filtered(dev, master_idx, kind_ops))
1581 continue;
1582 if (idx < s_idx)
1583 goto cont;
1584 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1585 NETLINK_CB(cb->skb).portid,
1586 cb->nlh->nlmsg_seq, 0,
1587 flags,
1588 ext_filter_mask);
1589 /* If we ran out of room on the first message,
1590 * we're in trouble
1592 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1594 if (err < 0)
1595 goto out;
1597 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1598 cont:
1599 idx++;
1602 out:
1603 cb->args[1] = idx;
1604 cb->args[0] = h;
1606 return skb->len;
1609 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1611 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1613 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1615 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1617 struct net *net;
1618 /* Examine the link attributes and figure out which
1619 * network namespace we are talking about.
1621 if (tb[IFLA_NET_NS_PID])
1622 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1623 else if (tb[IFLA_NET_NS_FD])
1624 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1625 else
1626 net = get_net(src_net);
1627 return net;
1629 EXPORT_SYMBOL(rtnl_link_get_net);
1631 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1633 if (dev) {
1634 if (tb[IFLA_ADDRESS] &&
1635 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1636 return -EINVAL;
1638 if (tb[IFLA_BROADCAST] &&
1639 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1640 return -EINVAL;
1643 if (tb[IFLA_AF_SPEC]) {
1644 struct nlattr *af;
1645 int rem, err;
1647 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1648 const struct rtnl_af_ops *af_ops;
1650 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1651 return -EAFNOSUPPORT;
1653 if (!af_ops->set_link_af)
1654 return -EOPNOTSUPP;
1656 if (af_ops->validate_link_af) {
1657 err = af_ops->validate_link_af(dev, af);
1658 if (err < 0)
1659 return err;
1664 return 0;
1667 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1668 int guid_type)
1670 const struct net_device_ops *ops = dev->netdev_ops;
1672 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1675 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1677 if (dev->type != ARPHRD_INFINIBAND)
1678 return -EOPNOTSUPP;
1680 return handle_infiniband_guid(dev, ivt, guid_type);
1683 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1685 const struct net_device_ops *ops = dev->netdev_ops;
1686 int err = -EINVAL;
1688 if (tb[IFLA_VF_MAC]) {
1689 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1691 err = -EOPNOTSUPP;
1692 if (ops->ndo_set_vf_mac)
1693 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1694 ivm->mac);
1695 if (err < 0)
1696 return err;
1699 if (tb[IFLA_VF_VLAN]) {
1700 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1702 err = -EOPNOTSUPP;
1703 if (ops->ndo_set_vf_vlan)
1704 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1705 ivv->qos);
1706 if (err < 0)
1707 return err;
1710 if (tb[IFLA_VF_TX_RATE]) {
1711 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1712 struct ifla_vf_info ivf;
1714 err = -EOPNOTSUPP;
1715 if (ops->ndo_get_vf_config)
1716 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1717 if (err < 0)
1718 return err;
1720 err = -EOPNOTSUPP;
1721 if (ops->ndo_set_vf_rate)
1722 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1723 ivf.min_tx_rate,
1724 ivt->rate);
1725 if (err < 0)
1726 return err;
1729 if (tb[IFLA_VF_RATE]) {
1730 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1732 err = -EOPNOTSUPP;
1733 if (ops->ndo_set_vf_rate)
1734 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1735 ivt->min_tx_rate,
1736 ivt->max_tx_rate);
1737 if (err < 0)
1738 return err;
1741 if (tb[IFLA_VF_SPOOFCHK]) {
1742 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1744 err = -EOPNOTSUPP;
1745 if (ops->ndo_set_vf_spoofchk)
1746 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1747 ivs->setting);
1748 if (err < 0)
1749 return err;
1752 if (tb[IFLA_VF_LINK_STATE]) {
1753 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1755 err = -EOPNOTSUPP;
1756 if (ops->ndo_set_vf_link_state)
1757 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1758 ivl->link_state);
1759 if (err < 0)
1760 return err;
1763 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1764 struct ifla_vf_rss_query_en *ivrssq_en;
1766 err = -EOPNOTSUPP;
1767 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1768 if (ops->ndo_set_vf_rss_query_en)
1769 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1770 ivrssq_en->setting);
1771 if (err < 0)
1772 return err;
1775 if (tb[IFLA_VF_TRUST]) {
1776 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1778 err = -EOPNOTSUPP;
1779 if (ops->ndo_set_vf_trust)
1780 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1781 if (err < 0)
1782 return err;
1785 if (tb[IFLA_VF_IB_NODE_GUID]) {
1786 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1788 if (!ops->ndo_set_vf_guid)
1789 return -EOPNOTSUPP;
1791 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1794 if (tb[IFLA_VF_IB_PORT_GUID]) {
1795 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1797 if (!ops->ndo_set_vf_guid)
1798 return -EOPNOTSUPP;
1800 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1803 return err;
1806 static int do_set_master(struct net_device *dev, int ifindex)
1808 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1809 const struct net_device_ops *ops;
1810 int err;
1812 if (upper_dev) {
1813 if (upper_dev->ifindex == ifindex)
1814 return 0;
1815 ops = upper_dev->netdev_ops;
1816 if (ops->ndo_del_slave) {
1817 err = ops->ndo_del_slave(upper_dev, dev);
1818 if (err)
1819 return err;
1820 } else {
1821 return -EOPNOTSUPP;
1825 if (ifindex) {
1826 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1827 if (!upper_dev)
1828 return -EINVAL;
1829 ops = upper_dev->netdev_ops;
1830 if (ops->ndo_add_slave) {
1831 err = ops->ndo_add_slave(upper_dev, dev);
1832 if (err)
1833 return err;
1834 } else {
1835 return -EOPNOTSUPP;
1838 return 0;
1841 #define DO_SETLINK_MODIFIED 0x01
1842 /* notify flag means notify + modified. */
1843 #define DO_SETLINK_NOTIFY 0x03
1844 static int do_setlink(const struct sk_buff *skb,
1845 struct net_device *dev, struct ifinfomsg *ifm,
1846 struct nlattr **tb, char *ifname, int status)
1848 const struct net_device_ops *ops = dev->netdev_ops;
1849 int err;
1851 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1852 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1853 if (IS_ERR(net)) {
1854 err = PTR_ERR(net);
1855 goto errout;
1857 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1858 put_net(net);
1859 err = -EPERM;
1860 goto errout;
1862 err = dev_change_net_namespace(dev, net, ifname);
1863 put_net(net);
1864 if (err)
1865 goto errout;
1866 status |= DO_SETLINK_MODIFIED;
1869 if (tb[IFLA_MAP]) {
1870 struct rtnl_link_ifmap *u_map;
1871 struct ifmap k_map;
1873 if (!ops->ndo_set_config) {
1874 err = -EOPNOTSUPP;
1875 goto errout;
1878 if (!netif_device_present(dev)) {
1879 err = -ENODEV;
1880 goto errout;
1883 u_map = nla_data(tb[IFLA_MAP]);
1884 k_map.mem_start = (unsigned long) u_map->mem_start;
1885 k_map.mem_end = (unsigned long) u_map->mem_end;
1886 k_map.base_addr = (unsigned short) u_map->base_addr;
1887 k_map.irq = (unsigned char) u_map->irq;
1888 k_map.dma = (unsigned char) u_map->dma;
1889 k_map.port = (unsigned char) u_map->port;
1891 err = ops->ndo_set_config(dev, &k_map);
1892 if (err < 0)
1893 goto errout;
1895 status |= DO_SETLINK_NOTIFY;
1898 if (tb[IFLA_ADDRESS]) {
1899 struct sockaddr *sa;
1900 int len;
1902 len = sizeof(sa_family_t) + dev->addr_len;
1903 sa = kmalloc(len, GFP_KERNEL);
1904 if (!sa) {
1905 err = -ENOMEM;
1906 goto errout;
1908 sa->sa_family = dev->type;
1909 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1910 dev->addr_len);
1911 err = dev_set_mac_address(dev, sa);
1912 kfree(sa);
1913 if (err)
1914 goto errout;
1915 status |= DO_SETLINK_MODIFIED;
1918 if (tb[IFLA_MTU]) {
1919 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1920 if (err < 0)
1921 goto errout;
1922 status |= DO_SETLINK_MODIFIED;
1925 if (tb[IFLA_GROUP]) {
1926 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1927 status |= DO_SETLINK_NOTIFY;
1931 * Interface selected by interface index but interface
1932 * name provided implies that a name change has been
1933 * requested.
1935 if (ifm->ifi_index > 0 && ifname[0]) {
1936 err = dev_change_name(dev, ifname);
1937 if (err < 0)
1938 goto errout;
1939 status |= DO_SETLINK_MODIFIED;
1942 if (tb[IFLA_IFALIAS]) {
1943 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1944 nla_len(tb[IFLA_IFALIAS]));
1945 if (err < 0)
1946 goto errout;
1947 status |= DO_SETLINK_NOTIFY;
1950 if (tb[IFLA_BROADCAST]) {
1951 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1952 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1955 if (ifm->ifi_flags || ifm->ifi_change) {
1956 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1957 if (err < 0)
1958 goto errout;
1961 if (tb[IFLA_MASTER]) {
1962 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1963 if (err)
1964 goto errout;
1965 status |= DO_SETLINK_MODIFIED;
1968 if (tb[IFLA_CARRIER]) {
1969 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1970 if (err)
1971 goto errout;
1972 status |= DO_SETLINK_MODIFIED;
1975 if (tb[IFLA_TXQLEN]) {
1976 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1977 unsigned long orig_len = dev->tx_queue_len;
1979 if (dev->tx_queue_len ^ value) {
1980 dev->tx_queue_len = value;
1981 err = call_netdevice_notifiers(
1982 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
1983 err = notifier_to_errno(err);
1984 if (err) {
1985 dev->tx_queue_len = orig_len;
1986 goto errout;
1988 status |= DO_SETLINK_NOTIFY;
1992 if (tb[IFLA_OPERSTATE])
1993 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1995 if (tb[IFLA_LINKMODE]) {
1996 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1998 write_lock_bh(&dev_base_lock);
1999 if (dev->link_mode ^ value)
2000 status |= DO_SETLINK_NOTIFY;
2001 dev->link_mode = value;
2002 write_unlock_bh(&dev_base_lock);
2005 if (tb[IFLA_VFINFO_LIST]) {
2006 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2007 struct nlattr *attr;
2008 int rem;
2010 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2011 if (nla_type(attr) != IFLA_VF_INFO ||
2012 nla_len(attr) < NLA_HDRLEN) {
2013 err = -EINVAL;
2014 goto errout;
2016 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2017 ifla_vf_policy);
2018 if (err < 0)
2019 goto errout;
2020 err = do_setvfinfo(dev, vfinfo);
2021 if (err < 0)
2022 goto errout;
2023 status |= DO_SETLINK_NOTIFY;
2026 err = 0;
2028 if (tb[IFLA_VF_PORTS]) {
2029 struct nlattr *port[IFLA_PORT_MAX+1];
2030 struct nlattr *attr;
2031 int vf;
2032 int rem;
2034 err = -EOPNOTSUPP;
2035 if (!ops->ndo_set_vf_port)
2036 goto errout;
2038 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2039 if (nla_type(attr) != IFLA_VF_PORT ||
2040 nla_len(attr) < NLA_HDRLEN) {
2041 err = -EINVAL;
2042 goto errout;
2044 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2045 ifla_port_policy);
2046 if (err < 0)
2047 goto errout;
2048 if (!port[IFLA_PORT_VF]) {
2049 err = -EOPNOTSUPP;
2050 goto errout;
2052 vf = nla_get_u32(port[IFLA_PORT_VF]);
2053 err = ops->ndo_set_vf_port(dev, vf, port);
2054 if (err < 0)
2055 goto errout;
2056 status |= DO_SETLINK_NOTIFY;
2059 err = 0;
2061 if (tb[IFLA_PORT_SELF]) {
2062 struct nlattr *port[IFLA_PORT_MAX+1];
2064 err = nla_parse_nested(port, IFLA_PORT_MAX,
2065 tb[IFLA_PORT_SELF], ifla_port_policy);
2066 if (err < 0)
2067 goto errout;
2069 err = -EOPNOTSUPP;
2070 if (ops->ndo_set_vf_port)
2071 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2072 if (err < 0)
2073 goto errout;
2074 status |= DO_SETLINK_NOTIFY;
2077 if (tb[IFLA_AF_SPEC]) {
2078 struct nlattr *af;
2079 int rem;
2081 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2082 const struct rtnl_af_ops *af_ops;
2084 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2085 BUG();
2087 err = af_ops->set_link_af(dev, af);
2088 if (err < 0)
2089 goto errout;
2091 status |= DO_SETLINK_NOTIFY;
2094 err = 0;
2096 if (tb[IFLA_PROTO_DOWN]) {
2097 err = dev_change_proto_down(dev,
2098 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2099 if (err)
2100 goto errout;
2101 status |= DO_SETLINK_NOTIFY;
2104 if (tb[IFLA_XDP]) {
2105 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2107 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2108 ifla_xdp_policy);
2109 if (err < 0)
2110 goto errout;
2112 if (xdp[IFLA_XDP_ATTACHED]) {
2113 err = -EINVAL;
2114 goto errout;
2116 if (xdp[IFLA_XDP_FD]) {
2117 err = dev_change_xdp_fd(dev,
2118 nla_get_s32(xdp[IFLA_XDP_FD]));
2119 if (err)
2120 goto errout;
2121 status |= DO_SETLINK_NOTIFY;
2125 errout:
2126 if (status & DO_SETLINK_MODIFIED) {
2127 if (status & DO_SETLINK_NOTIFY)
2128 netdev_state_change(dev);
2130 if (err < 0)
2131 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",
2132 dev->name);
2135 return err;
2138 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2140 struct net *net = sock_net(skb->sk);
2141 struct ifinfomsg *ifm;
2142 struct net_device *dev;
2143 int err;
2144 struct nlattr *tb[IFLA_MAX+1];
2145 char ifname[IFNAMSIZ];
2147 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2148 if (err < 0)
2149 goto errout;
2151 if (tb[IFLA_IFNAME])
2152 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2153 else
2154 ifname[0] = '\0';
2156 err = -EINVAL;
2157 ifm = nlmsg_data(nlh);
2158 if (ifm->ifi_index > 0)
2159 dev = __dev_get_by_index(net, ifm->ifi_index);
2160 else if (tb[IFLA_IFNAME])
2161 dev = __dev_get_by_name(net, ifname);
2162 else
2163 goto errout;
2165 if (dev == NULL) {
2166 err = -ENODEV;
2167 goto errout;
2170 err = validate_linkmsg(dev, tb);
2171 if (err < 0)
2172 goto errout;
2174 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
2175 errout:
2176 return err;
2179 static int rtnl_group_dellink(const struct net *net, int group)
2181 struct net_device *dev, *aux;
2182 LIST_HEAD(list_kill);
2183 bool found = false;
2185 if (!group)
2186 return -EPERM;
2188 for_each_netdev(net, dev) {
2189 if (dev->group == group) {
2190 const struct rtnl_link_ops *ops;
2192 found = true;
2193 ops = dev->rtnl_link_ops;
2194 if (!ops || !ops->dellink)
2195 return -EOPNOTSUPP;
2199 if (!found)
2200 return -ENODEV;
2202 for_each_netdev_safe(net, dev, aux) {
2203 if (dev->group == group) {
2204 const struct rtnl_link_ops *ops;
2206 ops = dev->rtnl_link_ops;
2207 ops->dellink(dev, &list_kill);
2210 unregister_netdevice_many(&list_kill);
2212 return 0;
2215 int rtnl_delete_link(struct net_device *dev)
2217 const struct rtnl_link_ops *ops;
2218 LIST_HEAD(list_kill);
2220 ops = dev->rtnl_link_ops;
2221 if (!ops || !ops->dellink)
2222 return -EOPNOTSUPP;
2224 ops->dellink(dev, &list_kill);
2225 unregister_netdevice_many(&list_kill);
2227 return 0;
2229 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2231 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2233 struct net *net = sock_net(skb->sk);
2234 struct net_device *dev;
2235 struct ifinfomsg *ifm;
2236 char ifname[IFNAMSIZ];
2237 struct nlattr *tb[IFLA_MAX+1];
2238 int err;
2240 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2241 if (err < 0)
2242 return err;
2244 if (tb[IFLA_IFNAME])
2245 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2247 ifm = nlmsg_data(nlh);
2248 if (ifm->ifi_index > 0)
2249 dev = __dev_get_by_index(net, ifm->ifi_index);
2250 else if (tb[IFLA_IFNAME])
2251 dev = __dev_get_by_name(net, ifname);
2252 else if (tb[IFLA_GROUP])
2253 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2254 else
2255 return -EINVAL;
2257 if (!dev)
2258 return -ENODEV;
2260 return rtnl_delete_link(dev);
2263 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2265 unsigned int old_flags;
2266 int err;
2268 old_flags = dev->flags;
2269 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2270 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2271 if (err < 0)
2272 return err;
2275 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2277 __dev_notify_flags(dev, old_flags, ~0U);
2278 return 0;
2280 EXPORT_SYMBOL(rtnl_configure_link);
2282 struct net_device *rtnl_create_link(struct net *net,
2283 const char *ifname, unsigned char name_assign_type,
2284 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2286 int err;
2287 struct net_device *dev;
2288 unsigned int num_tx_queues = 1;
2289 unsigned int num_rx_queues = 1;
2291 if (tb[IFLA_NUM_TX_QUEUES])
2292 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2293 else if (ops->get_num_tx_queues)
2294 num_tx_queues = ops->get_num_tx_queues();
2296 if (tb[IFLA_NUM_RX_QUEUES])
2297 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2298 else if (ops->get_num_rx_queues)
2299 num_rx_queues = ops->get_num_rx_queues();
2301 err = -ENOMEM;
2302 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2303 ops->setup, num_tx_queues, num_rx_queues);
2304 if (!dev)
2305 goto err;
2307 dev_net_set(dev, net);
2308 dev->rtnl_link_ops = ops;
2309 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2311 if (tb[IFLA_MTU])
2312 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2313 if (tb[IFLA_ADDRESS]) {
2314 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2315 nla_len(tb[IFLA_ADDRESS]));
2316 dev->addr_assign_type = NET_ADDR_SET;
2318 if (tb[IFLA_BROADCAST])
2319 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2320 nla_len(tb[IFLA_BROADCAST]));
2321 if (tb[IFLA_TXQLEN])
2322 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2323 if (tb[IFLA_OPERSTATE])
2324 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2325 if (tb[IFLA_LINKMODE])
2326 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2327 if (tb[IFLA_GROUP])
2328 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2330 return dev;
2332 err:
2333 return ERR_PTR(err);
2335 EXPORT_SYMBOL(rtnl_create_link);
2337 static int rtnl_group_changelink(const struct sk_buff *skb,
2338 struct net *net, int group,
2339 struct ifinfomsg *ifm,
2340 struct nlattr **tb)
2342 struct net_device *dev, *aux;
2343 int err;
2345 for_each_netdev_safe(net, dev, aux) {
2346 if (dev->group == group) {
2347 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2348 if (err < 0)
2349 return err;
2353 return 0;
2356 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2358 struct net *net = sock_net(skb->sk);
2359 const struct rtnl_link_ops *ops;
2360 const struct rtnl_link_ops *m_ops = NULL;
2361 struct net_device *dev;
2362 struct net_device *master_dev = NULL;
2363 struct ifinfomsg *ifm;
2364 char kind[MODULE_NAME_LEN];
2365 char ifname[IFNAMSIZ];
2366 struct nlattr *tb[IFLA_MAX+1];
2367 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2368 unsigned char name_assign_type = NET_NAME_USER;
2369 int err;
2371 #ifdef CONFIG_MODULES
2372 replay:
2373 #endif
2374 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2375 if (err < 0)
2376 return err;
2378 if (tb[IFLA_IFNAME])
2379 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2380 else
2381 ifname[0] = '\0';
2383 ifm = nlmsg_data(nlh);
2384 if (ifm->ifi_index > 0)
2385 dev = __dev_get_by_index(net, ifm->ifi_index);
2386 else {
2387 if (ifname[0])
2388 dev = __dev_get_by_name(net, ifname);
2389 else
2390 dev = NULL;
2393 if (dev) {
2394 master_dev = netdev_master_upper_dev_get(dev);
2395 if (master_dev)
2396 m_ops = master_dev->rtnl_link_ops;
2399 err = validate_linkmsg(dev, tb);
2400 if (err < 0)
2401 return err;
2403 if (tb[IFLA_LINKINFO]) {
2404 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2405 tb[IFLA_LINKINFO], ifla_info_policy);
2406 if (err < 0)
2407 return err;
2408 } else
2409 memset(linkinfo, 0, sizeof(linkinfo));
2411 if (linkinfo[IFLA_INFO_KIND]) {
2412 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2413 ops = rtnl_link_ops_get(kind);
2414 } else {
2415 kind[0] = '\0';
2416 ops = NULL;
2419 if (1) {
2420 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2421 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2422 struct nlattr **data = NULL;
2423 struct nlattr **slave_data = NULL;
2424 struct net *dest_net, *link_net = NULL;
2426 if (ops) {
2427 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2428 err = nla_parse_nested(attr, ops->maxtype,
2429 linkinfo[IFLA_INFO_DATA],
2430 ops->policy);
2431 if (err < 0)
2432 return err;
2433 data = attr;
2435 if (ops->validate) {
2436 err = ops->validate(tb, data);
2437 if (err < 0)
2438 return err;
2442 if (m_ops) {
2443 if (m_ops->slave_maxtype &&
2444 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2445 err = nla_parse_nested(slave_attr,
2446 m_ops->slave_maxtype,
2447 linkinfo[IFLA_INFO_SLAVE_DATA],
2448 m_ops->slave_policy);
2449 if (err < 0)
2450 return err;
2451 slave_data = slave_attr;
2453 if (m_ops->slave_validate) {
2454 err = m_ops->slave_validate(tb, slave_data);
2455 if (err < 0)
2456 return err;
2460 if (dev) {
2461 int status = 0;
2463 if (nlh->nlmsg_flags & NLM_F_EXCL)
2464 return -EEXIST;
2465 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2466 return -EOPNOTSUPP;
2468 if (linkinfo[IFLA_INFO_DATA]) {
2469 if (!ops || ops != dev->rtnl_link_ops ||
2470 !ops->changelink)
2471 return -EOPNOTSUPP;
2473 err = ops->changelink(dev, tb, data);
2474 if (err < 0)
2475 return err;
2476 status |= DO_SETLINK_NOTIFY;
2479 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2480 if (!m_ops || !m_ops->slave_changelink)
2481 return -EOPNOTSUPP;
2483 err = m_ops->slave_changelink(master_dev, dev,
2484 tb, slave_data);
2485 if (err < 0)
2486 return err;
2487 status |= DO_SETLINK_NOTIFY;
2490 return do_setlink(skb, dev, ifm, tb, ifname, status);
2493 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2494 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2495 return rtnl_group_changelink(skb, net,
2496 nla_get_u32(tb[IFLA_GROUP]),
2497 ifm, tb);
2498 return -ENODEV;
2501 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2502 return -EOPNOTSUPP;
2504 if (!ops) {
2505 #ifdef CONFIG_MODULES
2506 if (kind[0]) {
2507 __rtnl_unlock();
2508 request_module("rtnl-link-%s", kind);
2509 rtnl_lock();
2510 ops = rtnl_link_ops_get(kind);
2511 if (ops)
2512 goto replay;
2514 #endif
2515 return -EOPNOTSUPP;
2518 if (!ops->setup)
2519 return -EOPNOTSUPP;
2521 if (!ifname[0]) {
2522 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2523 name_assign_type = NET_NAME_ENUM;
2526 dest_net = rtnl_link_get_net(net, tb);
2527 if (IS_ERR(dest_net))
2528 return PTR_ERR(dest_net);
2530 err = -EPERM;
2531 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2532 goto out;
2534 if (tb[IFLA_LINK_NETNSID]) {
2535 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2537 link_net = get_net_ns_by_id(dest_net, id);
2538 if (!link_net) {
2539 err = -EINVAL;
2540 goto out;
2542 err = -EPERM;
2543 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2544 goto out;
2547 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2548 name_assign_type, ops, tb);
2549 if (IS_ERR(dev)) {
2550 err = PTR_ERR(dev);
2551 goto out;
2554 dev->ifindex = ifm->ifi_index;
2556 if (ops->newlink) {
2557 err = ops->newlink(link_net ? : net, dev, tb, data);
2558 /* Drivers should call free_netdev() in ->destructor
2559 * and unregister it on failure after registration
2560 * so that device could be finally freed in rtnl_unlock.
2562 if (err < 0) {
2563 /* If device is not registered at all, free it now */
2564 if (dev->reg_state == NETREG_UNINITIALIZED)
2565 free_netdev(dev);
2566 goto out;
2568 } else {
2569 err = register_netdevice(dev);
2570 if (err < 0) {
2571 free_netdev(dev);
2572 goto out;
2575 err = rtnl_configure_link(dev, ifm);
2576 if (err < 0)
2577 goto out_unregister;
2578 if (link_net) {
2579 err = dev_change_net_namespace(dev, dest_net, ifname);
2580 if (err < 0)
2581 goto out_unregister;
2583 out:
2584 if (link_net)
2585 put_net(link_net);
2586 put_net(dest_net);
2587 return err;
2588 out_unregister:
2589 if (ops->newlink) {
2590 LIST_HEAD(list_kill);
2592 ops->dellink(dev, &list_kill);
2593 unregister_netdevice_many(&list_kill);
2594 } else {
2595 unregister_netdevice(dev);
2597 goto out;
2601 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2603 struct net *net = sock_net(skb->sk);
2604 struct ifinfomsg *ifm;
2605 char ifname[IFNAMSIZ];
2606 struct nlattr *tb[IFLA_MAX+1];
2607 struct net_device *dev = NULL;
2608 struct sk_buff *nskb;
2609 int err;
2610 u32 ext_filter_mask = 0;
2612 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2613 if (err < 0)
2614 return err;
2616 if (tb[IFLA_IFNAME])
2617 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2619 if (tb[IFLA_EXT_MASK])
2620 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2622 ifm = nlmsg_data(nlh);
2623 if (ifm->ifi_index > 0)
2624 dev = __dev_get_by_index(net, ifm->ifi_index);
2625 else if (tb[IFLA_IFNAME])
2626 dev = __dev_get_by_name(net, ifname);
2627 else
2628 return -EINVAL;
2630 if (dev == NULL)
2631 return -ENODEV;
2633 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2634 if (nskb == NULL)
2635 return -ENOBUFS;
2637 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2638 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2639 if (err < 0) {
2640 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2641 WARN_ON(err == -EMSGSIZE);
2642 kfree_skb(nskb);
2643 } else
2644 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2646 return err;
2649 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2651 struct net *net = sock_net(skb->sk);
2652 struct net_device *dev;
2653 struct nlattr *tb[IFLA_MAX+1];
2654 u32 ext_filter_mask = 0;
2655 u16 min_ifinfo_dump_size = 0;
2656 int hdrlen;
2658 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2659 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2660 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2662 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2663 if (tb[IFLA_EXT_MASK])
2664 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2667 if (!ext_filter_mask)
2668 return NLMSG_GOODSIZE;
2670 * traverse the list of net devices and compute the minimum
2671 * buffer size based upon the filter mask.
2673 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2674 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2675 if_nlmsg_size(dev,
2676 ext_filter_mask));
2679 return min_ifinfo_dump_size;
2682 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2684 int idx;
2685 int s_idx = cb->family;
2687 if (s_idx == 0)
2688 s_idx = 1;
2689 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2690 int type = cb->nlh->nlmsg_type-RTM_BASE;
2691 if (idx < s_idx || idx == PF_PACKET)
2692 continue;
2693 if (rtnl_msg_handlers[idx] == NULL ||
2694 rtnl_msg_handlers[idx][type].dumpit == NULL)
2695 continue;
2696 if (idx > s_idx) {
2697 memset(&cb->args[0], 0, sizeof(cb->args));
2698 cb->prev_seq = 0;
2699 cb->seq = 0;
2701 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2702 break;
2704 cb->family = idx;
2706 return skb->len;
2709 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2710 unsigned int change, gfp_t flags)
2712 struct net *net = dev_net(dev);
2713 struct sk_buff *skb;
2714 int err = -ENOBUFS;
2715 size_t if_info_size;
2717 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2718 if (skb == NULL)
2719 goto errout;
2721 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2722 if (err < 0) {
2723 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2724 WARN_ON(err == -EMSGSIZE);
2725 kfree_skb(skb);
2726 goto errout;
2728 return skb;
2729 errout:
2730 if (err < 0)
2731 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2732 return NULL;
2735 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2737 struct net *net = dev_net(dev);
2739 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2742 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2743 gfp_t flags)
2745 struct sk_buff *skb;
2747 if (dev->reg_state != NETREG_REGISTERED)
2748 return;
2750 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2751 if (skb)
2752 rtmsg_ifinfo_send(skb, dev, flags);
2754 EXPORT_SYMBOL(rtmsg_ifinfo);
2756 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2757 struct net_device *dev,
2758 u8 *addr, u16 vid, u32 pid, u32 seq,
2759 int type, unsigned int flags,
2760 int nlflags, u16 ndm_state)
2762 struct nlmsghdr *nlh;
2763 struct ndmsg *ndm;
2765 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2766 if (!nlh)
2767 return -EMSGSIZE;
2769 ndm = nlmsg_data(nlh);
2770 ndm->ndm_family = AF_BRIDGE;
2771 ndm->ndm_pad1 = 0;
2772 ndm->ndm_pad2 = 0;
2773 ndm->ndm_flags = flags;
2774 ndm->ndm_type = 0;
2775 ndm->ndm_ifindex = dev->ifindex;
2776 ndm->ndm_state = ndm_state;
2778 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2779 goto nla_put_failure;
2780 if (vid)
2781 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2782 goto nla_put_failure;
2784 nlmsg_end(skb, nlh);
2785 return 0;
2787 nla_put_failure:
2788 nlmsg_cancel(skb, nlh);
2789 return -EMSGSIZE;
2792 static inline size_t rtnl_fdb_nlmsg_size(void)
2794 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2797 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2798 u16 ndm_state)
2800 struct net *net = dev_net(dev);
2801 struct sk_buff *skb;
2802 int err = -ENOBUFS;
2804 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2805 if (!skb)
2806 goto errout;
2808 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2809 0, 0, type, NTF_SELF, 0, ndm_state);
2810 if (err < 0) {
2811 kfree_skb(skb);
2812 goto errout;
2815 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2816 return;
2817 errout:
2818 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2822 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2824 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2825 struct nlattr *tb[],
2826 struct net_device *dev,
2827 const unsigned char *addr, u16 vid,
2828 u16 flags)
2830 int err = -EINVAL;
2832 /* If aging addresses are supported device will need to
2833 * implement its own handler for this.
2835 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2836 pr_info("%s: FDB only supports static addresses\n", dev->name);
2837 return err;
2840 if (vid) {
2841 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2842 return err;
2845 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2846 err = dev_uc_add_excl(dev, addr);
2847 else if (is_multicast_ether_addr(addr))
2848 err = dev_mc_add_excl(dev, addr);
2850 /* Only return duplicate errors if NLM_F_EXCL is set */
2851 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2852 err = 0;
2854 return err;
2856 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2858 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2860 u16 vid = 0;
2862 if (vlan_attr) {
2863 if (nla_len(vlan_attr) != sizeof(u16)) {
2864 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2865 return -EINVAL;
2868 vid = nla_get_u16(vlan_attr);
2870 if (!vid || vid >= VLAN_VID_MASK) {
2871 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2872 vid);
2873 return -EINVAL;
2876 *p_vid = vid;
2877 return 0;
2880 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2882 struct net *net = sock_net(skb->sk);
2883 struct ndmsg *ndm;
2884 struct nlattr *tb[NDA_MAX+1];
2885 struct net_device *dev;
2886 u8 *addr;
2887 u16 vid;
2888 int err;
2890 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2891 if (err < 0)
2892 return err;
2894 ndm = nlmsg_data(nlh);
2895 if (ndm->ndm_ifindex == 0) {
2896 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2897 return -EINVAL;
2900 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2901 if (dev == NULL) {
2902 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2903 return -ENODEV;
2906 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2907 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2908 return -EINVAL;
2911 addr = nla_data(tb[NDA_LLADDR]);
2913 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2914 if (err)
2915 return err;
2917 err = -EOPNOTSUPP;
2919 /* Support fdb on master device the net/bridge default case */
2920 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2921 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2922 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2923 const struct net_device_ops *ops = br_dev->netdev_ops;
2925 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2926 nlh->nlmsg_flags);
2927 if (err)
2928 goto out;
2929 else
2930 ndm->ndm_flags &= ~NTF_MASTER;
2933 /* Embedded bridge, macvlan, and any other device support */
2934 if ((ndm->ndm_flags & NTF_SELF)) {
2935 if (dev->netdev_ops->ndo_fdb_add)
2936 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2937 vid,
2938 nlh->nlmsg_flags);
2939 else
2940 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2941 nlh->nlmsg_flags);
2943 if (!err) {
2944 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2945 ndm->ndm_state);
2946 ndm->ndm_flags &= ~NTF_SELF;
2949 out:
2950 return err;
2954 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2956 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2957 struct nlattr *tb[],
2958 struct net_device *dev,
2959 const unsigned char *addr, u16 vid)
2961 int err = -EINVAL;
2963 /* If aging addresses are supported device will need to
2964 * implement its own handler for this.
2966 if (!(ndm->ndm_state & NUD_PERMANENT)) {
2967 pr_info("%s: FDB only supports static addresses\n", dev->name);
2968 return err;
2971 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2972 err = dev_uc_del(dev, addr);
2973 else if (is_multicast_ether_addr(addr))
2974 err = dev_mc_del(dev, addr);
2976 return err;
2978 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2980 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2982 struct net *net = sock_net(skb->sk);
2983 struct ndmsg *ndm;
2984 struct nlattr *tb[NDA_MAX+1];
2985 struct net_device *dev;
2986 int err = -EINVAL;
2987 __u8 *addr;
2988 u16 vid;
2990 if (!netlink_capable(skb, CAP_NET_ADMIN))
2991 return -EPERM;
2993 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2994 if (err < 0)
2995 return err;
2997 ndm = nlmsg_data(nlh);
2998 if (ndm->ndm_ifindex == 0) {
2999 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3000 return -EINVAL;
3003 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3004 if (dev == NULL) {
3005 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3006 return -ENODEV;
3009 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3010 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3011 return -EINVAL;
3014 addr = nla_data(tb[NDA_LLADDR]);
3016 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3017 if (err)
3018 return err;
3020 err = -EOPNOTSUPP;
3022 /* Support fdb on master device the net/bridge default case */
3023 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3024 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3025 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3026 const struct net_device_ops *ops = br_dev->netdev_ops;
3028 if (ops->ndo_fdb_del)
3029 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3031 if (err)
3032 goto out;
3033 else
3034 ndm->ndm_flags &= ~NTF_MASTER;
3037 /* Embedded bridge, macvlan, and any other device support */
3038 if (ndm->ndm_flags & NTF_SELF) {
3039 if (dev->netdev_ops->ndo_fdb_del)
3040 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3041 vid);
3042 else
3043 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3045 if (!err) {
3046 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3047 ndm->ndm_state);
3048 ndm->ndm_flags &= ~NTF_SELF;
3051 out:
3052 return err;
3055 static int nlmsg_populate_fdb(struct sk_buff *skb,
3056 struct netlink_callback *cb,
3057 struct net_device *dev,
3058 int *idx,
3059 struct netdev_hw_addr_list *list)
3061 struct netdev_hw_addr *ha;
3062 int err;
3063 u32 portid, seq;
3065 portid = NETLINK_CB(cb->skb).portid;
3066 seq = cb->nlh->nlmsg_seq;
3068 list_for_each_entry(ha, &list->list, list) {
3069 if (*idx < cb->args[0])
3070 goto skip;
3072 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3073 portid, seq,
3074 RTM_NEWNEIGH, NTF_SELF,
3075 NLM_F_MULTI, NUD_PERMANENT);
3076 if (err < 0)
3077 return err;
3078 skip:
3079 *idx += 1;
3081 return 0;
3085 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3086 * @nlh: netlink message header
3087 * @dev: netdevice
3089 * Default netdevice operation to dump the existing unicast address list.
3090 * Returns number of addresses from list put in skb.
3092 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3093 struct netlink_callback *cb,
3094 struct net_device *dev,
3095 struct net_device *filter_dev,
3096 int idx)
3098 int err;
3100 netif_addr_lock_bh(dev);
3101 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3102 if (err)
3103 goto out;
3104 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3105 out:
3106 netif_addr_unlock_bh(dev);
3107 cb->args[1] = err;
3108 return idx;
3110 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3112 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3114 struct net_device *dev;
3115 struct nlattr *tb[IFLA_MAX+1];
3116 struct net_device *br_dev = NULL;
3117 const struct net_device_ops *ops = NULL;
3118 const struct net_device_ops *cops = NULL;
3119 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3120 struct net *net = sock_net(skb->sk);
3121 int brport_idx = 0;
3122 int br_idx = 0;
3123 int idx = 0;
3125 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3126 ifla_policy) == 0) {
3127 if (tb[IFLA_MASTER])
3128 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3131 brport_idx = ifm->ifi_index;
3133 if (br_idx) {
3134 br_dev = __dev_get_by_index(net, br_idx);
3135 if (!br_dev)
3136 return -ENODEV;
3138 ops = br_dev->netdev_ops;
3141 cb->args[1] = 0;
3142 for_each_netdev(net, dev) {
3143 if (brport_idx && (dev->ifindex != brport_idx))
3144 continue;
3146 if (!br_idx) { /* user did not specify a specific bridge */
3147 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3148 br_dev = netdev_master_upper_dev_get(dev);
3149 cops = br_dev->netdev_ops;
3152 } else {
3153 if (dev != br_dev &&
3154 !(dev->priv_flags & IFF_BRIDGE_PORT))
3155 continue;
3157 if (br_dev != netdev_master_upper_dev_get(dev) &&
3158 !(dev->priv_flags & IFF_EBRIDGE))
3159 continue;
3161 cops = ops;
3164 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3165 if (cops && cops->ndo_fdb_dump)
3166 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3167 idx);
3169 if (cb->args[1] == -EMSGSIZE)
3170 break;
3172 if (dev->netdev_ops->ndo_fdb_dump)
3173 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
3174 idx);
3175 else
3176 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
3177 if (cb->args[1] == -EMSGSIZE)
3178 break;
3180 cops = NULL;
3183 cb->args[0] = idx;
3184 return skb->len;
3187 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3188 unsigned int attrnum, unsigned int flag)
3190 if (mask & flag)
3191 return nla_put_u8(skb, attrnum, !!(flags & flag));
3192 return 0;
3195 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3196 struct net_device *dev, u16 mode,
3197 u32 flags, u32 mask, int nlflags,
3198 u32 filter_mask,
3199 int (*vlan_fill)(struct sk_buff *skb,
3200 struct net_device *dev,
3201 u32 filter_mask))
3203 struct nlmsghdr *nlh;
3204 struct ifinfomsg *ifm;
3205 struct nlattr *br_afspec;
3206 struct nlattr *protinfo;
3207 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3208 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3209 int err = 0;
3211 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3212 if (nlh == NULL)
3213 return -EMSGSIZE;
3215 ifm = nlmsg_data(nlh);
3216 ifm->ifi_family = AF_BRIDGE;
3217 ifm->__ifi_pad = 0;
3218 ifm->ifi_type = dev->type;
3219 ifm->ifi_index = dev->ifindex;
3220 ifm->ifi_flags = dev_get_flags(dev);
3221 ifm->ifi_change = 0;
3224 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3225 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3226 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3227 (br_dev &&
3228 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3229 (dev->addr_len &&
3230 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3231 (dev->ifindex != dev_get_iflink(dev) &&
3232 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3233 goto nla_put_failure;
3235 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3236 if (!br_afspec)
3237 goto nla_put_failure;
3239 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3240 nla_nest_cancel(skb, br_afspec);
3241 goto nla_put_failure;
3244 if (mode != BRIDGE_MODE_UNDEF) {
3245 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3246 nla_nest_cancel(skb, br_afspec);
3247 goto nla_put_failure;
3250 if (vlan_fill) {
3251 err = vlan_fill(skb, dev, filter_mask);
3252 if (err) {
3253 nla_nest_cancel(skb, br_afspec);
3254 goto nla_put_failure;
3257 nla_nest_end(skb, br_afspec);
3259 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3260 if (!protinfo)
3261 goto nla_put_failure;
3263 if (brport_nla_put_flag(skb, flags, mask,
3264 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3265 brport_nla_put_flag(skb, flags, mask,
3266 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3267 brport_nla_put_flag(skb, flags, mask,
3268 IFLA_BRPORT_FAST_LEAVE,
3269 BR_MULTICAST_FAST_LEAVE) ||
3270 brport_nla_put_flag(skb, flags, mask,
3271 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3272 brport_nla_put_flag(skb, flags, mask,
3273 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3274 brport_nla_put_flag(skb, flags, mask,
3275 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3276 brport_nla_put_flag(skb, flags, mask,
3277 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3278 brport_nla_put_flag(skb, flags, mask,
3279 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3280 nla_nest_cancel(skb, protinfo);
3281 goto nla_put_failure;
3284 nla_nest_end(skb, protinfo);
3286 nlmsg_end(skb, nlh);
3287 return 0;
3288 nla_put_failure:
3289 nlmsg_cancel(skb, nlh);
3290 return err ? err : -EMSGSIZE;
3292 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3294 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3296 struct net *net = sock_net(skb->sk);
3297 struct net_device *dev;
3298 int idx = 0;
3299 u32 portid = NETLINK_CB(cb->skb).portid;
3300 u32 seq = cb->nlh->nlmsg_seq;
3301 u32 filter_mask = 0;
3302 int err;
3304 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3305 struct nlattr *extfilt;
3307 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3308 IFLA_EXT_MASK);
3309 if (extfilt) {
3310 if (nla_len(extfilt) < sizeof(filter_mask))
3311 return -EINVAL;
3313 filter_mask = nla_get_u32(extfilt);
3317 rcu_read_lock();
3318 for_each_netdev_rcu(net, dev) {
3319 const struct net_device_ops *ops = dev->netdev_ops;
3320 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3322 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3323 if (idx >= cb->args[0]) {
3324 err = br_dev->netdev_ops->ndo_bridge_getlink(
3325 skb, portid, seq, dev,
3326 filter_mask, NLM_F_MULTI);
3327 if (err < 0 && err != -EOPNOTSUPP)
3328 break;
3330 idx++;
3333 if (ops->ndo_bridge_getlink) {
3334 if (idx >= cb->args[0]) {
3335 err = ops->ndo_bridge_getlink(skb, portid,
3336 seq, dev,
3337 filter_mask,
3338 NLM_F_MULTI);
3339 if (err < 0 && err != -EOPNOTSUPP)
3340 break;
3342 idx++;
3345 rcu_read_unlock();
3346 cb->args[0] = idx;
3348 return skb->len;
3351 static inline size_t bridge_nlmsg_size(void)
3353 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3354 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3355 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3356 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3357 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3358 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3359 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3360 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3361 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3362 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3363 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3366 static int rtnl_bridge_notify(struct net_device *dev)
3368 struct net *net = dev_net(dev);
3369 struct sk_buff *skb;
3370 int err = -EOPNOTSUPP;
3372 if (!dev->netdev_ops->ndo_bridge_getlink)
3373 return 0;
3375 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3376 if (!skb) {
3377 err = -ENOMEM;
3378 goto errout;
3381 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3382 if (err < 0)
3383 goto errout;
3385 if (!skb->len)
3386 goto errout;
3388 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3389 return 0;
3390 errout:
3391 WARN_ON(err == -EMSGSIZE);
3392 kfree_skb(skb);
3393 if (err)
3394 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3395 return err;
3398 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3400 struct net *net = sock_net(skb->sk);
3401 struct ifinfomsg *ifm;
3402 struct net_device *dev;
3403 struct nlattr *br_spec, *attr = NULL;
3404 int rem, err = -EOPNOTSUPP;
3405 u16 flags = 0;
3406 bool have_flags = false;
3408 if (nlmsg_len(nlh) < sizeof(*ifm))
3409 return -EINVAL;
3411 ifm = nlmsg_data(nlh);
3412 if (ifm->ifi_family != AF_BRIDGE)
3413 return -EPFNOSUPPORT;
3415 dev = __dev_get_by_index(net, ifm->ifi_index);
3416 if (!dev) {
3417 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3418 return -ENODEV;
3421 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3422 if (br_spec) {
3423 nla_for_each_nested(attr, br_spec, rem) {
3424 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3425 if (nla_len(attr) < sizeof(flags))
3426 return -EINVAL;
3428 have_flags = true;
3429 flags = nla_get_u16(attr);
3430 break;
3435 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3436 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3438 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3439 err = -EOPNOTSUPP;
3440 goto out;
3443 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3444 if (err)
3445 goto out;
3447 flags &= ~BRIDGE_FLAGS_MASTER;
3450 if ((flags & BRIDGE_FLAGS_SELF)) {
3451 if (!dev->netdev_ops->ndo_bridge_setlink)
3452 err = -EOPNOTSUPP;
3453 else
3454 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3455 flags);
3456 if (!err) {
3457 flags &= ~BRIDGE_FLAGS_SELF;
3459 /* Generate event to notify upper layer of bridge
3460 * change
3462 err = rtnl_bridge_notify(dev);
3466 if (have_flags)
3467 memcpy(nla_data(attr), &flags, sizeof(flags));
3468 out:
3469 return err;
3472 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3474 struct net *net = sock_net(skb->sk);
3475 struct ifinfomsg *ifm;
3476 struct net_device *dev;
3477 struct nlattr *br_spec, *attr = NULL;
3478 int rem, err = -EOPNOTSUPP;
3479 u16 flags = 0;
3480 bool have_flags = false;
3482 if (nlmsg_len(nlh) < sizeof(*ifm))
3483 return -EINVAL;
3485 ifm = nlmsg_data(nlh);
3486 if (ifm->ifi_family != AF_BRIDGE)
3487 return -EPFNOSUPPORT;
3489 dev = __dev_get_by_index(net, ifm->ifi_index);
3490 if (!dev) {
3491 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3492 return -ENODEV;
3495 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3496 if (br_spec) {
3497 nla_for_each_nested(attr, br_spec, rem) {
3498 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3499 if (nla_len(attr) < sizeof(flags))
3500 return -EINVAL;
3502 have_flags = true;
3503 flags = nla_get_u16(attr);
3504 break;
3509 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3510 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3512 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3513 err = -EOPNOTSUPP;
3514 goto out;
3517 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3518 if (err)
3519 goto out;
3521 flags &= ~BRIDGE_FLAGS_MASTER;
3524 if ((flags & BRIDGE_FLAGS_SELF)) {
3525 if (!dev->netdev_ops->ndo_bridge_dellink)
3526 err = -EOPNOTSUPP;
3527 else
3528 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3529 flags);
3531 if (!err) {
3532 flags &= ~BRIDGE_FLAGS_SELF;
3534 /* Generate event to notify upper layer of bridge
3535 * change
3537 err = rtnl_bridge_notify(dev);
3541 if (have_flags)
3542 memcpy(nla_data(attr), &flags, sizeof(flags));
3543 out:
3544 return err;
3547 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3549 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3550 (!idxattr || idxattr == attrid);
3553 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3554 int type, u32 pid, u32 seq, u32 change,
3555 unsigned int flags, unsigned int filter_mask,
3556 int *idxattr, int *prividx)
3558 struct if_stats_msg *ifsm;
3559 struct nlmsghdr *nlh;
3560 struct nlattr *attr;
3561 int s_prividx = *prividx;
3563 ASSERT_RTNL();
3565 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3566 if (!nlh)
3567 return -EMSGSIZE;
3569 ifsm = nlmsg_data(nlh);
3570 ifsm->ifindex = dev->ifindex;
3571 ifsm->filter_mask = filter_mask;
3573 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3574 struct rtnl_link_stats64 *sp;
3576 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3577 sizeof(struct rtnl_link_stats64),
3578 IFLA_STATS_UNSPEC);
3579 if (!attr)
3580 goto nla_put_failure;
3582 sp = nla_data(attr);
3583 dev_get_stats(dev, sp);
3586 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3587 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3589 if (ops && ops->fill_linkxstats) {
3590 int err;
3592 *idxattr = IFLA_STATS_LINK_XSTATS;
3593 attr = nla_nest_start(skb,
3594 IFLA_STATS_LINK_XSTATS);
3595 if (!attr)
3596 goto nla_put_failure;
3598 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3599 nla_nest_end(skb, attr);
3600 if (err)
3601 goto nla_put_failure;
3602 *idxattr = 0;
3606 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3607 *idxattr)) {
3608 const struct rtnl_link_ops *ops = NULL;
3609 const struct net_device *master;
3611 master = netdev_master_upper_dev_get(dev);
3612 if (master)
3613 ops = master->rtnl_link_ops;
3614 if (ops && ops->fill_linkxstats) {
3615 int err;
3617 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3618 attr = nla_nest_start(skb,
3619 IFLA_STATS_LINK_XSTATS_SLAVE);
3620 if (!attr)
3621 goto nla_put_failure;
3623 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3624 nla_nest_end(skb, attr);
3625 if (err)
3626 goto nla_put_failure;
3627 *idxattr = 0;
3631 nlmsg_end(skb, nlh);
3633 return 0;
3635 nla_put_failure:
3636 /* not a multi message or no progress mean a real error */
3637 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3638 nlmsg_cancel(skb, nlh);
3639 else
3640 nlmsg_end(skb, nlh);
3642 return -EMSGSIZE;
3645 static const struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
3646 [IFLA_STATS_LINK_64] = { .len = sizeof(struct rtnl_link_stats64) },
3649 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3650 u32 filter_mask)
3652 size_t size = 0;
3654 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3655 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3657 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3658 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3659 int attr = IFLA_STATS_LINK_XSTATS;
3661 if (ops && ops->get_linkxstats_size) {
3662 size += nla_total_size(ops->get_linkxstats_size(dev,
3663 attr));
3664 /* for IFLA_STATS_LINK_XSTATS */
3665 size += nla_total_size(0);
3669 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
3670 struct net_device *_dev = (struct net_device *)dev;
3671 const struct rtnl_link_ops *ops = NULL;
3672 const struct net_device *master;
3674 /* netdev_master_upper_dev_get can't take const */
3675 master = netdev_master_upper_dev_get(_dev);
3676 if (master)
3677 ops = master->rtnl_link_ops;
3678 if (ops && ops->get_linkxstats_size) {
3679 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
3681 size += nla_total_size(ops->get_linkxstats_size(dev,
3682 attr));
3683 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
3684 size += nla_total_size(0);
3688 return size;
3691 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
3693 struct net *net = sock_net(skb->sk);
3694 struct net_device *dev = NULL;
3695 int idxattr = 0, prividx = 0;
3696 struct if_stats_msg *ifsm;
3697 struct sk_buff *nskb;
3698 u32 filter_mask;
3699 int err;
3701 ifsm = nlmsg_data(nlh);
3702 if (ifsm->ifindex > 0)
3703 dev = __dev_get_by_index(net, ifsm->ifindex);
3704 else
3705 return -EINVAL;
3707 if (!dev)
3708 return -ENODEV;
3710 filter_mask = ifsm->filter_mask;
3711 if (!filter_mask)
3712 return -EINVAL;
3714 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
3715 if (!nskb)
3716 return -ENOBUFS;
3718 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
3719 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
3720 0, filter_mask, &idxattr, &prividx);
3721 if (err < 0) {
3722 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3723 WARN_ON(err == -EMSGSIZE);
3724 kfree_skb(nskb);
3725 } else {
3726 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3729 return err;
3732 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
3734 int h, s_h, err, s_idx, s_idxattr, s_prividx;
3735 struct net *net = sock_net(skb->sk);
3736 unsigned int flags = NLM_F_MULTI;
3737 struct if_stats_msg *ifsm;
3738 struct hlist_head *head;
3739 struct net_device *dev;
3740 u32 filter_mask = 0;
3741 int idx = 0;
3743 s_h = cb->args[0];
3744 s_idx = cb->args[1];
3745 s_idxattr = cb->args[2];
3746 s_prividx = cb->args[3];
3748 cb->seq = net->dev_base_seq;
3750 ifsm = nlmsg_data(cb->nlh);
3751 filter_mask = ifsm->filter_mask;
3752 if (!filter_mask)
3753 return -EINVAL;
3755 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3756 idx = 0;
3757 head = &net->dev_index_head[h];
3758 hlist_for_each_entry(dev, head, index_hlist) {
3759 if (idx < s_idx)
3760 goto cont;
3761 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
3762 NETLINK_CB(cb->skb).portid,
3763 cb->nlh->nlmsg_seq, 0,
3764 flags, filter_mask,
3765 &s_idxattr, &s_prividx);
3766 /* If we ran out of room on the first message,
3767 * we're in trouble
3769 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
3771 if (err < 0)
3772 goto out;
3773 s_prividx = 0;
3774 s_idxattr = 0;
3775 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3776 cont:
3777 idx++;
3780 out:
3781 cb->args[3] = s_prividx;
3782 cb->args[2] = s_idxattr;
3783 cb->args[1] = idx;
3784 cb->args[0] = h;
3786 return skb->len;
3789 /* Process one rtnetlink message. */
3791 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3793 struct net *net = sock_net(skb->sk);
3794 rtnl_doit_func doit;
3795 int kind;
3796 int family;
3797 int type;
3798 int err;
3800 type = nlh->nlmsg_type;
3801 if (type > RTM_MAX)
3802 return -EOPNOTSUPP;
3804 type -= RTM_BASE;
3806 /* All the messages must have at least 1 byte length */
3807 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3808 return 0;
3810 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3811 kind = type&3;
3813 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3814 return -EPERM;
3816 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3817 struct sock *rtnl;
3818 rtnl_dumpit_func dumpit;
3819 rtnl_calcit_func calcit;
3820 u16 min_dump_alloc = 0;
3822 dumpit = rtnl_get_dumpit(family, type);
3823 if (dumpit == NULL)
3824 return -EOPNOTSUPP;
3825 calcit = rtnl_get_calcit(family, type);
3826 if (calcit)
3827 min_dump_alloc = calcit(skb, nlh);
3829 __rtnl_unlock();
3830 rtnl = net->rtnl;
3832 struct netlink_dump_control c = {
3833 .dump = dumpit,
3834 .min_dump_alloc = min_dump_alloc,
3836 err = netlink_dump_start(rtnl, skb, nlh, &c);
3838 rtnl_lock();
3839 return err;
3842 doit = rtnl_get_doit(family, type);
3843 if (doit == NULL)
3844 return -EOPNOTSUPP;
3846 return doit(skb, nlh);
3849 static void rtnetlink_rcv(struct sk_buff *skb)
3851 rtnl_lock();
3852 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3853 rtnl_unlock();
3856 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3858 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3860 switch (event) {
3861 case NETDEV_UP:
3862 case NETDEV_DOWN:
3863 case NETDEV_PRE_UP:
3864 case NETDEV_POST_INIT:
3865 case NETDEV_REGISTER:
3866 case NETDEV_CHANGE:
3867 case NETDEV_PRE_TYPE_CHANGE:
3868 case NETDEV_GOING_DOWN:
3869 case NETDEV_UNREGISTER:
3870 case NETDEV_UNREGISTER_FINAL:
3871 case NETDEV_RELEASE:
3872 case NETDEV_JOIN:
3873 case NETDEV_BONDING_INFO:
3874 break;
3875 default:
3876 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3877 break;
3879 return NOTIFY_DONE;
3882 static struct notifier_block rtnetlink_dev_notifier = {
3883 .notifier_call = rtnetlink_event,
3887 static int __net_init rtnetlink_net_init(struct net *net)
3889 struct sock *sk;
3890 struct netlink_kernel_cfg cfg = {
3891 .groups = RTNLGRP_MAX,
3892 .input = rtnetlink_rcv,
3893 .cb_mutex = &rtnl_mutex,
3894 .flags = NL_CFG_F_NONROOT_RECV,
3897 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3898 if (!sk)
3899 return -ENOMEM;
3900 net->rtnl = sk;
3901 return 0;
3904 static void __net_exit rtnetlink_net_exit(struct net *net)
3906 netlink_kernel_release(net->rtnl);
3907 net->rtnl = NULL;
3910 static struct pernet_operations rtnetlink_net_ops = {
3911 .init = rtnetlink_net_init,
3912 .exit = rtnetlink_net_exit,
3915 void __init rtnetlink_init(void)
3917 if (register_pernet_subsys(&rtnetlink_net_ops))
3918 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3920 register_netdevice_notifier(&rtnetlink_dev_notifier);
3922 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3923 rtnl_dump_ifinfo, rtnl_calcit);
3924 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3925 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3926 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3928 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3929 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3931 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3932 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3933 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3935 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3936 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3937 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
3939 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
3940 NULL);