ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / core / rtnetlink.c
blobd52b633164c917ed4c1f775091d87be75cee1f1f
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
43 #include <asm/uaccess.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <net/udp.h>
53 #include <net/tcp.h>
54 #include <net/sock.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
60 struct rtnl_link {
61 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
63 rtnl_calcit_func calcit;
66 static DEFINE_MUTEX(rtnl_mutex);
68 void rtnl_lock(void)
70 mutex_lock(&rtnl_mutex);
72 EXPORT_SYMBOL(rtnl_lock);
74 void __rtnl_unlock(void)
76 mutex_unlock(&rtnl_mutex);
79 void rtnl_unlock(void)
81 /* This fellow will unlock it for us. */
82 netdev_run_todo();
84 EXPORT_SYMBOL(rtnl_unlock);
86 int rtnl_trylock(void)
88 return mutex_trylock(&rtnl_mutex);
90 EXPORT_SYMBOL(rtnl_trylock);
92 int rtnl_is_locked(void)
94 return mutex_is_locked(&rtnl_mutex);
96 EXPORT_SYMBOL(rtnl_is_locked);
98 #ifdef CONFIG_PROVE_LOCKING
99 bool lockdep_rtnl_is_held(void)
101 return lockdep_is_held(&rtnl_mutex);
103 EXPORT_SYMBOL(lockdep_rtnl_is_held);
104 #endif /* #ifdef CONFIG_PROVE_LOCKING */
106 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
108 static inline int rtm_msgindex(int msgtype)
110 int msgindex = msgtype - RTM_BASE;
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
119 return msgindex;
122 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
124 struct rtnl_link *tab;
126 if (protocol <= RTNL_FAMILY_MAX)
127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
131 if (tab == NULL || tab[msgindex].doit == NULL)
132 tab = rtnl_msg_handlers[PF_UNSPEC];
134 return tab[msgindex].doit;
137 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
139 struct rtnl_link *tab;
141 if (protocol <= RTNL_FAMILY_MAX)
142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
146 if (tab == NULL || tab[msgindex].dumpit == NULL)
147 tab = rtnl_msg_handlers[PF_UNSPEC];
149 return tab[msgindex].dumpit;
152 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
154 struct rtnl_link *tab;
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
164 return tab[msgindex].calcit;
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
173 * @calcit: Function pointer to calc size of dump message
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
183 * Returns 0 on success or a negative error code.
185 int __rtnl_register(int protocol, int msgtype,
186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
189 struct rtnl_link *tab;
190 int msgindex;
192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
193 msgindex = rtm_msgindex(msgtype);
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
201 rtnl_msg_handlers[protocol] = tab;
204 if (doit)
205 tab[msgindex].doit = doit;
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
210 if (calcit)
211 tab[msgindex].calcit = calcit;
213 return 0;
215 EXPORT_SYMBOL_GPL(__rtnl_register);
218 * rtnl_register - Register a rtnetlink message type
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
224 * of memory implies no sense in continuing.
226 void rtnl_register(int protocol, int msgtype,
227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
235 EXPORT_SYMBOL_GPL(rtnl_register);
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
242 * Returns 0 on success or a negative error code.
244 int rtnl_unregister(int protocol, int msgtype)
246 int msgindex;
248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
249 msgindex = rtm_msgindex(msgtype);
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
257 return 0;
259 EXPORT_SYMBOL_GPL(rtnl_unregister);
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
268 void rtnl_unregister_all(int protocol)
270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
275 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
277 static LIST_HEAD(link_ops);
279 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
281 const struct rtnl_link_ops *ops;
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
287 return NULL;
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
298 * Returns 0 on success or a negative error code.
300 int __rtnl_link_register(struct rtnl_link_ops *ops)
302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
310 if (ops->setup && !ops->dellink)
311 ops->dellink = unregister_netdevice_queue;
313 list_add_tail(&ops->list, &link_ops);
314 return 0;
316 EXPORT_SYMBOL_GPL(__rtnl_link_register);
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
322 * Returns 0 on success or a negative error code.
324 int rtnl_link_register(struct rtnl_link_ops *ops)
326 int err;
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
333 EXPORT_SYMBOL_GPL(rtnl_link_register);
335 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
337 struct net_device *dev;
338 LIST_HEAD(list_kill);
340 for_each_netdev(net, dev) {
341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
344 unregister_netdevice_many(&list_kill);
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
351 * The caller must hold the rtnl_mutex.
353 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
355 struct net *net;
357 for_each_net(net) {
358 __rtnl_kill_links(net, ops);
360 list_del(&ops->list);
362 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
364 /* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
367 static void rtnl_lock_unregistering_all(void)
369 struct net *net;
370 bool unregistering;
371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
373 add_wait_queue(&netdev_unregistering_wq, &wait);
374 for (;;) {
375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
389 remove_wait_queue(&netdev_unregistering_wq, &wait);
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
396 void rtnl_link_unregister(struct rtnl_link_ops *ops)
398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
403 mutex_unlock(&net_mutex);
405 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
407 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
416 if (!ops || !ops->get_slave_size)
417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
423 static size_t rtnl_link_get_size(const struct net_device *dev)
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
428 if (!ops)
429 return 0;
431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
436 size += nla_total_size(sizeof(struct nlattr)) +
437 ops->get_size(dev);
439 if (ops->get_xstats_size)
440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
443 size += rtnl_link_get_slave_info_data_size(dev);
445 return size;
448 static LIST_HEAD(rtnl_af_ops);
450 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
452 const struct rtnl_af_ops *ops;
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
459 return NULL;
463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
466 * Returns 0 on success or a negative error code.
468 void rtnl_af_register(struct rtnl_af_ops *ops)
470 rtnl_lock();
471 list_add_tail(&ops->list, &rtnl_af_ops);
472 rtnl_unlock();
474 EXPORT_SYMBOL_GPL(rtnl_af_register);
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
480 * The caller must hold the rtnl_mutex.
482 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
484 list_del(&ops->list);
486 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
492 void rtnl_af_unregister(struct rtnl_af_ops *ops)
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
498 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
500 static size_t rtnl_link_get_af_size(const struct net_device *dev,
501 u32 ext_filter_mask)
503 struct rtnl_af_ops *af_ops;
504 size_t size;
506 /* IFLA_AF_SPEC */
507 size = nla_total_size(sizeof(struct nlattr));
509 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
510 if (af_ops->get_link_af_size) {
511 /* AF_* + nested data */
512 size += nla_total_size(sizeof(struct nlattr)) +
513 af_ops->get_link_af_size(dev, ext_filter_mask);
517 return size;
520 static bool rtnl_have_link_slave_info(const struct net_device *dev)
522 struct net_device *master_dev;
524 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
525 if (master_dev && master_dev->rtnl_link_ops)
526 return true;
527 return false;
530 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
531 const struct net_device *dev)
533 struct net_device *master_dev;
534 const struct rtnl_link_ops *ops;
535 struct nlattr *slave_data;
536 int err;
538 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
539 if (!master_dev)
540 return 0;
541 ops = master_dev->rtnl_link_ops;
542 if (!ops)
543 return 0;
544 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
545 return -EMSGSIZE;
546 if (ops->fill_slave_info) {
547 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
548 if (!slave_data)
549 return -EMSGSIZE;
550 err = ops->fill_slave_info(skb, master_dev, dev);
551 if (err < 0)
552 goto err_cancel_slave_data;
553 nla_nest_end(skb, slave_data);
555 return 0;
557 err_cancel_slave_data:
558 nla_nest_cancel(skb, slave_data);
559 return err;
562 static int rtnl_link_info_fill(struct sk_buff *skb,
563 const struct net_device *dev)
565 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
566 struct nlattr *data;
567 int err;
569 if (!ops)
570 return 0;
571 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
572 return -EMSGSIZE;
573 if (ops->fill_xstats) {
574 err = ops->fill_xstats(skb, dev);
575 if (err < 0)
576 return err;
578 if (ops->fill_info) {
579 data = nla_nest_start(skb, IFLA_INFO_DATA);
580 if (data == NULL)
581 return -EMSGSIZE;
582 err = ops->fill_info(skb, dev);
583 if (err < 0)
584 goto err_cancel_data;
585 nla_nest_end(skb, data);
587 return 0;
589 err_cancel_data:
590 nla_nest_cancel(skb, data);
591 return err;
594 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
596 struct nlattr *linkinfo;
597 int err = -EMSGSIZE;
599 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
600 if (linkinfo == NULL)
601 goto out;
603 err = rtnl_link_info_fill(skb, dev);
604 if (err < 0)
605 goto err_cancel_link;
607 err = rtnl_link_slave_info_fill(skb, dev);
608 if (err < 0)
609 goto err_cancel_link;
611 nla_nest_end(skb, linkinfo);
612 return 0;
614 err_cancel_link:
615 nla_nest_cancel(skb, linkinfo);
616 out:
617 return err;
620 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
622 struct sock *rtnl = net->rtnl;
623 int err = 0;
625 NETLINK_CB(skb).dst_group = group;
626 if (echo)
627 atomic_inc(&skb->users);
628 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
629 if (echo)
630 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
631 return err;
634 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
636 struct sock *rtnl = net->rtnl;
638 return nlmsg_unicast(rtnl, skb, pid);
640 EXPORT_SYMBOL(rtnl_unicast);
642 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
643 struct nlmsghdr *nlh, gfp_t flags)
645 struct sock *rtnl = net->rtnl;
646 int report = 0;
648 if (nlh)
649 report = nlmsg_report(nlh);
651 nlmsg_notify(rtnl, skb, pid, group, report, flags);
653 EXPORT_SYMBOL(rtnl_notify);
655 void rtnl_set_sk_err(struct net *net, u32 group, int error)
657 struct sock *rtnl = net->rtnl;
659 netlink_set_err(rtnl, 0, group, error);
661 EXPORT_SYMBOL(rtnl_set_sk_err);
663 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
665 struct nlattr *mx;
666 int i, valid = 0;
668 mx = nla_nest_start(skb, RTA_METRICS);
669 if (mx == NULL)
670 return -ENOBUFS;
672 for (i = 0; i < RTAX_MAX; i++) {
673 if (metrics[i]) {
674 if (i == RTAX_CC_ALGO - 1) {
675 char tmp[TCP_CA_NAME_MAX], *name;
677 name = tcp_ca_get_name_by_key(metrics[i], tmp);
678 if (!name)
679 continue;
680 if (nla_put_string(skb, i + 1, name))
681 goto nla_put_failure;
682 } else if (i == RTAX_FEATURES - 1) {
683 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
685 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 if (nla_put_u32(skb, i + 1, user_features))
687 goto nla_put_failure;
688 } else {
689 if (nla_put_u32(skb, i + 1, metrics[i]))
690 goto nla_put_failure;
692 valid++;
696 if (!valid) {
697 nla_nest_cancel(skb, mx);
698 return 0;
701 return nla_nest_end(skb, mx);
703 nla_put_failure:
704 nla_nest_cancel(skb, mx);
705 return -EMSGSIZE;
707 EXPORT_SYMBOL(rtnetlink_put_metrics);
709 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
710 long expires, u32 error)
712 struct rta_cacheinfo ci = {
713 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
714 .rta_used = dst->__use,
715 .rta_clntref = atomic_read(&(dst->__refcnt)),
716 .rta_error = error,
717 .rta_id = id,
720 if (expires) {
721 unsigned long clock;
723 clock = jiffies_to_clock_t(abs(expires));
724 clock = min_t(unsigned long, clock, INT_MAX);
725 ci.rta_expires = (expires > 0) ? clock : -clock;
727 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
729 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
731 static void set_operstate(struct net_device *dev, unsigned char transition)
733 unsigned char operstate = dev->operstate;
735 switch (transition) {
736 case IF_OPER_UP:
737 if ((operstate == IF_OPER_DORMANT ||
738 operstate == IF_OPER_UNKNOWN) &&
739 !netif_dormant(dev))
740 operstate = IF_OPER_UP;
741 break;
743 case IF_OPER_DORMANT:
744 if (operstate == IF_OPER_UP ||
745 operstate == IF_OPER_UNKNOWN)
746 operstate = IF_OPER_DORMANT;
747 break;
750 if (dev->operstate != operstate) {
751 write_lock_bh(&dev_base_lock);
752 dev->operstate = operstate;
753 write_unlock_bh(&dev_base_lock);
754 netdev_state_change(dev);
758 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
760 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
764 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 const struct ifinfomsg *ifm)
767 unsigned int flags = ifm->ifi_flags;
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 if (ifm->ifi_change)
771 flags = (flags & ifm->ifi_change) |
772 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
774 return flags;
777 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
778 const struct rtnl_link_stats64 *b)
780 a->rx_packets = b->rx_packets;
781 a->tx_packets = b->tx_packets;
782 a->rx_bytes = b->rx_bytes;
783 a->tx_bytes = b->tx_bytes;
784 a->rx_errors = b->rx_errors;
785 a->tx_errors = b->tx_errors;
786 a->rx_dropped = b->rx_dropped;
787 a->tx_dropped = b->tx_dropped;
789 a->multicast = b->multicast;
790 a->collisions = b->collisions;
792 a->rx_length_errors = b->rx_length_errors;
793 a->rx_over_errors = b->rx_over_errors;
794 a->rx_crc_errors = b->rx_crc_errors;
795 a->rx_frame_errors = b->rx_frame_errors;
796 a->rx_fifo_errors = b->rx_fifo_errors;
797 a->rx_missed_errors = b->rx_missed_errors;
799 a->tx_aborted_errors = b->tx_aborted_errors;
800 a->tx_carrier_errors = b->tx_carrier_errors;
801 a->tx_fifo_errors = b->tx_fifo_errors;
802 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 a->tx_window_errors = b->tx_window_errors;
805 a->rx_compressed = b->rx_compressed;
806 a->tx_compressed = b->tx_compressed;
809 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
811 memcpy(v, b, sizeof(*b));
814 /* All VF info */
815 static inline int rtnl_vfinfo_size(const struct net_device *dev,
816 u32 ext_filter_mask)
818 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
819 (ext_filter_mask & RTEXT_FILTER_VF)) {
820 int num_vfs = dev_num_vf(dev->dev.parent);
821 size_t size = nla_total_size(sizeof(struct nlattr));
822 size += nla_total_size(num_vfs * sizeof(struct nlattr));
823 size += num_vfs *
824 (nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
827 nla_total_size(sizeof(struct ifla_vf_rate)) +
828 nla_total_size(sizeof(struct ifla_vf_link_state)) +
829 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
830 /* IFLA_VF_STATS_RX_PACKETS */
831 nla_total_size(sizeof(__u64)) +
832 /* IFLA_VF_STATS_TX_PACKETS */
833 nla_total_size(sizeof(__u64)) +
834 /* IFLA_VF_STATS_RX_BYTES */
835 nla_total_size(sizeof(__u64)) +
836 /* IFLA_VF_STATS_TX_BYTES */
837 nla_total_size(sizeof(__u64)) +
838 /* IFLA_VF_STATS_BROADCAST */
839 nla_total_size(sizeof(__u64)) +
840 /* IFLA_VF_STATS_MULTICAST */
841 nla_total_size(sizeof(__u64)) +
842 nla_total_size(sizeof(struct ifla_vf_trust)));
843 return size;
844 } else
845 return 0;
848 static size_t rtnl_port_size(const struct net_device *dev,
849 u32 ext_filter_mask)
851 size_t port_size = nla_total_size(4) /* PORT_VF */
852 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
853 + nla_total_size(sizeof(struct ifla_port_vsi))
854 /* PORT_VSI_TYPE */
855 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
856 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
857 + nla_total_size(1) /* PROT_VDP_REQUEST */
858 + nla_total_size(2); /* PORT_VDP_RESPONSE */
859 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
860 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
861 + port_size;
862 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
863 + port_size;
865 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
866 !(ext_filter_mask & RTEXT_FILTER_VF))
867 return 0;
868 if (dev_num_vf(dev->dev.parent))
869 return port_self_size + vf_ports_size +
870 vf_port_size * dev_num_vf(dev->dev.parent);
871 else
872 return port_self_size;
875 static noinline size_t if_nlmsg_size(const struct net_device *dev,
876 u32 ext_filter_mask)
878 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
879 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
880 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
881 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
882 + nla_total_size(sizeof(struct rtnl_link_ifmap))
883 + nla_total_size(sizeof(struct rtnl_link_stats))
884 + nla_total_size(sizeof(struct rtnl_link_stats64))
885 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
886 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
887 + nla_total_size(4) /* IFLA_TXQLEN */
888 + nla_total_size(4) /* IFLA_WEIGHT */
889 + nla_total_size(4) /* IFLA_MTU */
890 + nla_total_size(4) /* IFLA_LINK */
891 + nla_total_size(4) /* IFLA_MASTER */
892 + nla_total_size(1) /* IFLA_CARRIER */
893 + nla_total_size(4) /* IFLA_PROMISCUITY */
894 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
895 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
896 + nla_total_size(1) /* IFLA_OPERSTATE */
897 + nla_total_size(1) /* IFLA_LINKMODE */
898 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
899 + nla_total_size(4) /* IFLA_LINK_NETNSID */
900 + nla_total_size(4) /* IFLA_GROUP */
901 + nla_total_size(ext_filter_mask
902 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
903 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
904 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
905 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
906 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
907 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
908 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
909 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
910 + nla_total_size(1); /* IFLA_PROTO_DOWN */
914 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
916 struct nlattr *vf_ports;
917 struct nlattr *vf_port;
918 int vf;
919 int err;
921 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
922 if (!vf_ports)
923 return -EMSGSIZE;
925 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
926 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
927 if (!vf_port)
928 goto nla_put_failure;
929 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
930 goto nla_put_failure;
931 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
932 if (err == -EMSGSIZE)
933 goto nla_put_failure;
934 if (err) {
935 nla_nest_cancel(skb, vf_port);
936 continue;
938 nla_nest_end(skb, vf_port);
941 nla_nest_end(skb, vf_ports);
943 return 0;
945 nla_put_failure:
946 nla_nest_cancel(skb, vf_ports);
947 return -EMSGSIZE;
950 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
952 struct nlattr *port_self;
953 int err;
955 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
956 if (!port_self)
957 return -EMSGSIZE;
959 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
960 if (err) {
961 nla_nest_cancel(skb, port_self);
962 return (err == -EMSGSIZE) ? err : 0;
965 nla_nest_end(skb, port_self);
967 return 0;
970 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
971 u32 ext_filter_mask)
973 int err;
975 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
976 !(ext_filter_mask & RTEXT_FILTER_VF))
977 return 0;
979 err = rtnl_port_self_fill(skb, dev);
980 if (err)
981 return err;
983 if (dev_num_vf(dev->dev.parent)) {
984 err = rtnl_vf_ports_fill(skb, dev);
985 if (err)
986 return err;
989 return 0;
992 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
994 int err;
995 struct netdev_phys_item_id ppid;
997 err = dev_get_phys_port_id(dev, &ppid);
998 if (err) {
999 if (err == -EOPNOTSUPP)
1000 return 0;
1001 return err;
1004 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1005 return -EMSGSIZE;
1007 return 0;
1010 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1012 char name[IFNAMSIZ];
1013 int err;
1015 err = dev_get_phys_port_name(dev, name, sizeof(name));
1016 if (err) {
1017 if (err == -EOPNOTSUPP)
1018 return 0;
1019 return err;
1022 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1023 return -EMSGSIZE;
1025 return 0;
1028 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1030 int err;
1031 struct switchdev_attr attr = {
1032 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1033 .flags = SWITCHDEV_F_NO_RECURSE,
1036 err = switchdev_port_attr_get(dev, &attr);
1037 if (err) {
1038 if (err == -EOPNOTSUPP)
1039 return 0;
1040 return err;
1043 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1044 attr.u.ppid.id))
1045 return -EMSGSIZE;
1047 return 0;
1050 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1051 struct net_device *dev)
1053 const struct rtnl_link_stats64 *stats;
1054 struct rtnl_link_stats64 temp;
1055 struct nlattr *attr;
1057 stats = dev_get_stats(dev, &temp);
1059 attr = nla_reserve(skb, IFLA_STATS,
1060 sizeof(struct rtnl_link_stats));
1061 if (!attr)
1062 return -EMSGSIZE;
1064 copy_rtnl_link_stats(nla_data(attr), stats);
1066 attr = nla_reserve(skb, IFLA_STATS64,
1067 sizeof(struct rtnl_link_stats64));
1068 if (!attr)
1069 return -EMSGSIZE;
1071 copy_rtnl_link_stats64(nla_data(attr), stats);
1073 return 0;
1076 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1077 struct net_device *dev,
1078 int vfs_num,
1079 struct nlattr *vfinfo)
1081 struct ifla_vf_rss_query_en vf_rss_query_en;
1082 struct ifla_vf_link_state vf_linkstate;
1083 struct ifla_vf_spoofchk vf_spoofchk;
1084 struct ifla_vf_tx_rate vf_tx_rate;
1085 struct ifla_vf_stats vf_stats;
1086 struct ifla_vf_trust vf_trust;
1087 struct ifla_vf_vlan vf_vlan;
1088 struct ifla_vf_rate vf_rate;
1089 struct nlattr *vf, *vfstats;
1090 struct ifla_vf_mac vf_mac;
1091 struct ifla_vf_info ivi;
1093 memset(&ivi, 0, sizeof(ivi));
1095 /* Not all SR-IOV capable drivers support the
1096 * spoofcheck and "RSS query enable" query. Preset to
1097 * -1 so the user space tool can detect that the driver
1098 * didn't report anything.
1100 ivi.spoofchk = -1;
1101 ivi.rss_query_en = -1;
1102 ivi.trusted = -1;
1103 /* The default value for VF link state is "auto"
1104 * IFLA_VF_LINK_STATE_AUTO which equals zero
1106 ivi.linkstate = 0;
1107 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1108 return 0;
1110 vf_mac.vf =
1111 vf_vlan.vf =
1112 vf_rate.vf =
1113 vf_tx_rate.vf =
1114 vf_spoofchk.vf =
1115 vf_linkstate.vf =
1116 vf_rss_query_en.vf =
1117 vf_trust.vf = ivi.vf;
1119 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1120 vf_vlan.vlan = ivi.vlan;
1121 vf_vlan.qos = ivi.qos;
1122 vf_tx_rate.rate = ivi.max_tx_rate;
1123 vf_rate.min_tx_rate = ivi.min_tx_rate;
1124 vf_rate.max_tx_rate = ivi.max_tx_rate;
1125 vf_spoofchk.setting = ivi.spoofchk;
1126 vf_linkstate.link_state = ivi.linkstate;
1127 vf_rss_query_en.setting = ivi.rss_query_en;
1128 vf_trust.setting = ivi.trusted;
1129 vf = nla_nest_start(skb, IFLA_VF_INFO);
1130 if (!vf) {
1131 nla_nest_cancel(skb, vfinfo);
1132 return -EMSGSIZE;
1134 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1135 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1136 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1137 &vf_rate) ||
1138 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1139 &vf_tx_rate) ||
1140 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1141 &vf_spoofchk) ||
1142 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1143 &vf_linkstate) ||
1144 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1145 sizeof(vf_rss_query_en),
1146 &vf_rss_query_en) ||
1147 nla_put(skb, IFLA_VF_TRUST,
1148 sizeof(vf_trust), &vf_trust))
1149 return -EMSGSIZE;
1150 memset(&vf_stats, 0, sizeof(vf_stats));
1151 if (dev->netdev_ops->ndo_get_vf_stats)
1152 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1153 &vf_stats);
1154 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1155 if (!vfstats) {
1156 nla_nest_cancel(skb, vf);
1157 nla_nest_cancel(skb, vfinfo);
1158 return -EMSGSIZE;
1160 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1161 vf_stats.rx_packets) ||
1162 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1163 vf_stats.tx_packets) ||
1164 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1165 vf_stats.rx_bytes) ||
1166 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1167 vf_stats.tx_bytes) ||
1168 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1169 vf_stats.broadcast) ||
1170 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1171 vf_stats.multicast))
1172 return -EMSGSIZE;
1173 nla_nest_end(skb, vfstats);
1174 nla_nest_end(skb, vf);
1175 return 0;
1178 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1180 struct rtnl_link_ifmap map;
1182 memset(&map, 0, sizeof(map));
1183 map.mem_start = dev->mem_start;
1184 map.mem_end = dev->mem_end;
1185 map.base_addr = dev->base_addr;
1186 map.irq = dev->irq;
1187 map.dma = dev->dma;
1188 map.port = dev->if_port;
1190 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1191 return -EMSGSIZE;
1193 return 0;
1196 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1197 int type, u32 pid, u32 seq, u32 change,
1198 unsigned int flags, u32 ext_filter_mask)
1200 struct ifinfomsg *ifm;
1201 struct nlmsghdr *nlh;
1202 struct nlattr *af_spec;
1203 struct rtnl_af_ops *af_ops;
1204 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1206 ASSERT_RTNL();
1207 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1208 if (nlh == NULL)
1209 return -EMSGSIZE;
1211 ifm = nlmsg_data(nlh);
1212 ifm->ifi_family = AF_UNSPEC;
1213 ifm->__ifi_pad = 0;
1214 ifm->ifi_type = dev->type;
1215 ifm->ifi_index = dev->ifindex;
1216 ifm->ifi_flags = dev_get_flags(dev);
1217 ifm->ifi_change = change;
1219 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1220 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1221 nla_put_u8(skb, IFLA_OPERSTATE,
1222 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1223 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1224 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1225 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1226 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1227 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1228 #ifdef CONFIG_RPS
1229 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1230 #endif
1231 (dev->ifindex != dev_get_iflink(dev) &&
1232 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1233 (upper_dev &&
1234 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1235 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1236 (dev->qdisc &&
1237 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1238 (dev->ifalias &&
1239 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1240 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1241 atomic_read(&dev->carrier_changes)) ||
1242 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1243 goto nla_put_failure;
1245 if (rtnl_fill_link_ifmap(skb, dev))
1246 goto nla_put_failure;
1248 if (dev->addr_len) {
1249 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1250 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1251 goto nla_put_failure;
1254 if (rtnl_phys_port_id_fill(skb, dev))
1255 goto nla_put_failure;
1257 if (rtnl_phys_port_name_fill(skb, dev))
1258 goto nla_put_failure;
1260 if (rtnl_phys_switch_id_fill(skb, dev))
1261 goto nla_put_failure;
1263 if (rtnl_fill_stats(skb, dev))
1264 goto nla_put_failure;
1266 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1267 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1268 goto nla_put_failure;
1270 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1271 ext_filter_mask & RTEXT_FILTER_VF) {
1272 int i;
1273 struct nlattr *vfinfo;
1274 int num_vfs = dev_num_vf(dev->dev.parent);
1276 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1277 if (!vfinfo)
1278 goto nla_put_failure;
1279 for (i = 0; i < num_vfs; i++) {
1280 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1281 goto nla_put_failure;
1284 nla_nest_end(skb, vfinfo);
1287 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1288 goto nla_put_failure;
1290 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1291 if (rtnl_link_fill(skb, dev) < 0)
1292 goto nla_put_failure;
1295 if (dev->rtnl_link_ops &&
1296 dev->rtnl_link_ops->get_link_net) {
1297 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1299 if (!net_eq(dev_net(dev), link_net)) {
1300 int id = peernet2id_alloc(dev_net(dev), link_net);
1302 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1303 goto nla_put_failure;
1307 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1308 goto nla_put_failure;
1310 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1311 if (af_ops->fill_link_af) {
1312 struct nlattr *af;
1313 int err;
1315 if (!(af = nla_nest_start(skb, af_ops->family)))
1316 goto nla_put_failure;
1318 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1321 * Caller may return ENODATA to indicate that there
1322 * was no data to be dumped. This is not an error, it
1323 * means we should trim the attribute header and
1324 * continue.
1326 if (err == -ENODATA)
1327 nla_nest_cancel(skb, af);
1328 else if (err < 0)
1329 goto nla_put_failure;
1331 nla_nest_end(skb, af);
1335 nla_nest_end(skb, af_spec);
1337 nlmsg_end(skb, nlh);
1338 return 0;
1340 nla_put_failure:
1341 nlmsg_cancel(skb, nlh);
1342 return -EMSGSIZE;
1345 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1346 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1347 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1348 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1349 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1350 [IFLA_MTU] = { .type = NLA_U32 },
1351 [IFLA_LINK] = { .type = NLA_U32 },
1352 [IFLA_MASTER] = { .type = NLA_U32 },
1353 [IFLA_CARRIER] = { .type = NLA_U8 },
1354 [IFLA_TXQLEN] = { .type = NLA_U32 },
1355 [IFLA_WEIGHT] = { .type = NLA_U32 },
1356 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1357 [IFLA_LINKMODE] = { .type = NLA_U8 },
1358 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1359 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1360 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1361 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1362 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1363 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1364 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1365 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1366 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1367 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1368 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1369 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1370 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1371 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1372 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1373 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1374 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1375 [IFLA_GROUP] = { .type = NLA_U32 },
1378 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1379 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1380 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1381 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1382 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1385 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1386 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1387 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1388 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1389 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1390 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1391 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1392 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1393 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1394 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1397 static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = {
1398 [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 },
1399 [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 },
1400 [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 },
1401 [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 },
1402 [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 },
1403 [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 },
1406 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1407 [IFLA_PORT_VF] = { .type = NLA_U32 },
1408 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1409 .len = PORT_PROFILE_MAX },
1410 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1411 .len = sizeof(struct ifla_port_vsi)},
1412 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1413 .len = PORT_UUID_MAX },
1414 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1415 .len = PORT_UUID_MAX },
1416 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1417 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1420 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1422 struct net *net = sock_net(skb->sk);
1423 int h, s_h;
1424 int idx = 0, s_idx;
1425 struct net_device *dev;
1426 struct hlist_head *head;
1427 struct nlattr *tb[IFLA_MAX+1];
1428 u32 ext_filter_mask = 0;
1429 int err;
1430 int hdrlen;
1432 s_h = cb->args[0];
1433 s_idx = cb->args[1];
1435 cb->seq = net->dev_base_seq;
1437 /* A hack to preserve kernel<->userspace interface.
1438 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1439 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1440 * what iproute2 < v3.9.0 used.
1441 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1442 * attribute, its netlink message is shorter than struct ifinfomsg.
1444 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1445 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1447 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1449 if (tb[IFLA_EXT_MASK])
1450 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1453 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1454 idx = 0;
1455 head = &net->dev_index_head[h];
1456 hlist_for_each_entry(dev, head, index_hlist) {
1457 if (idx < s_idx)
1458 goto cont;
1459 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1460 NETLINK_CB(cb->skb).portid,
1461 cb->nlh->nlmsg_seq, 0,
1462 NLM_F_MULTI,
1463 ext_filter_mask);
1465 if (err < 0) {
1466 if (likely(skb->len))
1467 goto out;
1469 goto out_err;
1472 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1473 cont:
1474 idx++;
1477 out:
1478 err = skb->len;
1479 out_err:
1480 cb->args[1] = idx;
1481 cb->args[0] = h;
1483 return err;
1486 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1488 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1490 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1492 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1494 struct net *net;
1495 /* Examine the link attributes and figure out which
1496 * network namespace we are talking about.
1498 if (tb[IFLA_NET_NS_PID])
1499 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1500 else if (tb[IFLA_NET_NS_FD])
1501 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1502 else
1503 net = get_net(src_net);
1504 return net;
1506 EXPORT_SYMBOL(rtnl_link_get_net);
1508 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1510 if (dev) {
1511 if (tb[IFLA_ADDRESS] &&
1512 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1513 return -EINVAL;
1515 if (tb[IFLA_BROADCAST] &&
1516 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1517 return -EINVAL;
1520 if (tb[IFLA_AF_SPEC]) {
1521 struct nlattr *af;
1522 int rem, err;
1524 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1525 const struct rtnl_af_ops *af_ops;
1527 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1528 return -EAFNOSUPPORT;
1530 if (!af_ops->set_link_af)
1531 return -EOPNOTSUPP;
1533 if (af_ops->validate_link_af) {
1534 err = af_ops->validate_link_af(dev, af);
1535 if (err < 0)
1536 return err;
1541 return 0;
1544 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1546 const struct net_device_ops *ops = dev->netdev_ops;
1547 int err = -EINVAL;
1549 if (tb[IFLA_VF_MAC]) {
1550 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1552 err = -EOPNOTSUPP;
1553 if (ops->ndo_set_vf_mac)
1554 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1555 ivm->mac);
1556 if (err < 0)
1557 return err;
1560 if (tb[IFLA_VF_VLAN]) {
1561 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1563 err = -EOPNOTSUPP;
1564 if (ops->ndo_set_vf_vlan)
1565 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1566 ivv->qos);
1567 if (err < 0)
1568 return err;
1571 if (tb[IFLA_VF_TX_RATE]) {
1572 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1573 struct ifla_vf_info ivf;
1575 err = -EOPNOTSUPP;
1576 if (ops->ndo_get_vf_config)
1577 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1578 if (err < 0)
1579 return err;
1581 err = -EOPNOTSUPP;
1582 if (ops->ndo_set_vf_rate)
1583 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1584 ivf.min_tx_rate,
1585 ivt->rate);
1586 if (err < 0)
1587 return err;
1590 if (tb[IFLA_VF_RATE]) {
1591 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1593 err = -EOPNOTSUPP;
1594 if (ops->ndo_set_vf_rate)
1595 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1596 ivt->min_tx_rate,
1597 ivt->max_tx_rate);
1598 if (err < 0)
1599 return err;
1602 if (tb[IFLA_VF_SPOOFCHK]) {
1603 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1605 err = -EOPNOTSUPP;
1606 if (ops->ndo_set_vf_spoofchk)
1607 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1608 ivs->setting);
1609 if (err < 0)
1610 return err;
1613 if (tb[IFLA_VF_LINK_STATE]) {
1614 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1616 err = -EOPNOTSUPP;
1617 if (ops->ndo_set_vf_link_state)
1618 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1619 ivl->link_state);
1620 if (err < 0)
1621 return err;
1624 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1625 struct ifla_vf_rss_query_en *ivrssq_en;
1627 err = -EOPNOTSUPP;
1628 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1629 if (ops->ndo_set_vf_rss_query_en)
1630 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1631 ivrssq_en->setting);
1632 if (err < 0)
1633 return err;
1636 if (tb[IFLA_VF_TRUST]) {
1637 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1639 err = -EOPNOTSUPP;
1640 if (ops->ndo_set_vf_trust)
1641 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1642 if (err < 0)
1643 return err;
1646 return err;
1649 static int do_set_master(struct net_device *dev, int ifindex)
1651 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1652 const struct net_device_ops *ops;
1653 int err;
1655 if (upper_dev) {
1656 if (upper_dev->ifindex == ifindex)
1657 return 0;
1658 ops = upper_dev->netdev_ops;
1659 if (ops->ndo_del_slave) {
1660 err = ops->ndo_del_slave(upper_dev, dev);
1661 if (err)
1662 return err;
1663 } else {
1664 return -EOPNOTSUPP;
1668 if (ifindex) {
1669 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1670 if (!upper_dev)
1671 return -EINVAL;
1672 ops = upper_dev->netdev_ops;
1673 if (ops->ndo_add_slave) {
1674 err = ops->ndo_add_slave(upper_dev, dev);
1675 if (err)
1676 return err;
1677 } else {
1678 return -EOPNOTSUPP;
1681 return 0;
1684 #define DO_SETLINK_MODIFIED 0x01
1685 /* notify flag means notify + modified. */
1686 #define DO_SETLINK_NOTIFY 0x03
1687 static int do_setlink(const struct sk_buff *skb,
1688 struct net_device *dev, struct ifinfomsg *ifm,
1689 struct nlattr **tb, char *ifname, int status)
1691 const struct net_device_ops *ops = dev->netdev_ops;
1692 int err;
1694 err = validate_linkmsg(dev, tb);
1695 if (err < 0)
1696 return err;
1698 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1699 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1700 if (IS_ERR(net)) {
1701 err = PTR_ERR(net);
1702 goto errout;
1704 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1705 put_net(net);
1706 err = -EPERM;
1707 goto errout;
1709 err = dev_change_net_namespace(dev, net, ifname);
1710 put_net(net);
1711 if (err)
1712 goto errout;
1713 status |= DO_SETLINK_MODIFIED;
1716 if (tb[IFLA_MAP]) {
1717 struct rtnl_link_ifmap *u_map;
1718 struct ifmap k_map;
1720 if (!ops->ndo_set_config) {
1721 err = -EOPNOTSUPP;
1722 goto errout;
1725 if (!netif_device_present(dev)) {
1726 err = -ENODEV;
1727 goto errout;
1730 u_map = nla_data(tb[IFLA_MAP]);
1731 k_map.mem_start = (unsigned long) u_map->mem_start;
1732 k_map.mem_end = (unsigned long) u_map->mem_end;
1733 k_map.base_addr = (unsigned short) u_map->base_addr;
1734 k_map.irq = (unsigned char) u_map->irq;
1735 k_map.dma = (unsigned char) u_map->dma;
1736 k_map.port = (unsigned char) u_map->port;
1738 err = ops->ndo_set_config(dev, &k_map);
1739 if (err < 0)
1740 goto errout;
1742 status |= DO_SETLINK_NOTIFY;
1745 if (tb[IFLA_ADDRESS]) {
1746 struct sockaddr *sa;
1747 int len;
1749 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
1750 sizeof(*sa));
1751 sa = kmalloc(len, GFP_KERNEL);
1752 if (!sa) {
1753 err = -ENOMEM;
1754 goto errout;
1756 sa->sa_family = dev->type;
1757 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1758 dev->addr_len);
1759 err = dev_set_mac_address(dev, sa);
1760 kfree(sa);
1761 if (err)
1762 goto errout;
1763 status |= DO_SETLINK_MODIFIED;
1766 if (tb[IFLA_MTU]) {
1767 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1768 if (err < 0)
1769 goto errout;
1770 status |= DO_SETLINK_MODIFIED;
1773 if (tb[IFLA_GROUP]) {
1774 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1775 status |= DO_SETLINK_NOTIFY;
1779 * Interface selected by interface index but interface
1780 * name provided implies that a name change has been
1781 * requested.
1783 if (ifm->ifi_index > 0 && ifname[0]) {
1784 err = dev_change_name(dev, ifname);
1785 if (err < 0)
1786 goto errout;
1787 status |= DO_SETLINK_MODIFIED;
1790 if (tb[IFLA_IFALIAS]) {
1791 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1792 nla_len(tb[IFLA_IFALIAS]));
1793 if (err < 0)
1794 goto errout;
1795 status |= DO_SETLINK_NOTIFY;
1798 if (tb[IFLA_BROADCAST]) {
1799 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1800 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1803 if (ifm->ifi_flags || ifm->ifi_change) {
1804 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1805 if (err < 0)
1806 goto errout;
1809 if (tb[IFLA_MASTER]) {
1810 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1811 if (err)
1812 goto errout;
1813 status |= DO_SETLINK_MODIFIED;
1816 if (tb[IFLA_CARRIER]) {
1817 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1818 if (err)
1819 goto errout;
1820 status |= DO_SETLINK_MODIFIED;
1823 if (tb[IFLA_TXQLEN]) {
1824 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1826 if (dev->tx_queue_len ^ value)
1827 status |= DO_SETLINK_NOTIFY;
1829 dev->tx_queue_len = value;
1832 if (tb[IFLA_OPERSTATE])
1833 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1835 if (tb[IFLA_LINKMODE]) {
1836 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1838 write_lock_bh(&dev_base_lock);
1839 if (dev->link_mode ^ value)
1840 status |= DO_SETLINK_NOTIFY;
1841 dev->link_mode = value;
1842 write_unlock_bh(&dev_base_lock);
1845 if (tb[IFLA_VFINFO_LIST]) {
1846 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
1847 struct nlattr *attr;
1848 int rem;
1850 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1851 if (nla_type(attr) != IFLA_VF_INFO ||
1852 nla_len(attr) < NLA_HDRLEN) {
1853 err = -EINVAL;
1854 goto errout;
1856 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1857 ifla_vf_policy);
1858 if (err < 0)
1859 goto errout;
1860 err = do_setvfinfo(dev, vfinfo);
1861 if (err < 0)
1862 goto errout;
1863 status |= DO_SETLINK_NOTIFY;
1866 err = 0;
1868 if (tb[IFLA_VF_PORTS]) {
1869 struct nlattr *port[IFLA_PORT_MAX+1];
1870 struct nlattr *attr;
1871 int vf;
1872 int rem;
1874 err = -EOPNOTSUPP;
1875 if (!ops->ndo_set_vf_port)
1876 goto errout;
1878 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1879 if (nla_type(attr) != IFLA_VF_PORT ||
1880 nla_len(attr) < NLA_HDRLEN) {
1881 err = -EINVAL;
1882 goto errout;
1884 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1885 ifla_port_policy);
1886 if (err < 0)
1887 goto errout;
1888 if (!port[IFLA_PORT_VF]) {
1889 err = -EOPNOTSUPP;
1890 goto errout;
1892 vf = nla_get_u32(port[IFLA_PORT_VF]);
1893 err = ops->ndo_set_vf_port(dev, vf, port);
1894 if (err < 0)
1895 goto errout;
1896 status |= DO_SETLINK_NOTIFY;
1899 err = 0;
1901 if (tb[IFLA_PORT_SELF]) {
1902 struct nlattr *port[IFLA_PORT_MAX+1];
1904 err = nla_parse_nested(port, IFLA_PORT_MAX,
1905 tb[IFLA_PORT_SELF], ifla_port_policy);
1906 if (err < 0)
1907 goto errout;
1909 err = -EOPNOTSUPP;
1910 if (ops->ndo_set_vf_port)
1911 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1912 if (err < 0)
1913 goto errout;
1914 status |= DO_SETLINK_NOTIFY;
1917 if (tb[IFLA_AF_SPEC]) {
1918 struct nlattr *af;
1919 int rem;
1921 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1922 const struct rtnl_af_ops *af_ops;
1924 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1925 BUG();
1927 err = af_ops->set_link_af(dev, af);
1928 if (err < 0)
1929 goto errout;
1931 status |= DO_SETLINK_NOTIFY;
1934 err = 0;
1936 if (tb[IFLA_PROTO_DOWN]) {
1937 err = dev_change_proto_down(dev,
1938 nla_get_u8(tb[IFLA_PROTO_DOWN]));
1939 if (err)
1940 goto errout;
1941 status |= DO_SETLINK_NOTIFY;
1944 errout:
1945 if (status & DO_SETLINK_MODIFIED) {
1946 if (status & DO_SETLINK_NOTIFY)
1947 netdev_state_change(dev);
1949 if (err < 0)
1950 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",
1951 dev->name);
1954 return err;
1957 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1959 struct net *net = sock_net(skb->sk);
1960 struct ifinfomsg *ifm;
1961 struct net_device *dev;
1962 int err;
1963 struct nlattr *tb[IFLA_MAX+1];
1964 char ifname[IFNAMSIZ];
1966 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1967 if (err < 0)
1968 goto errout;
1970 if (tb[IFLA_IFNAME])
1971 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1972 else
1973 ifname[0] = '\0';
1975 err = -EINVAL;
1976 ifm = nlmsg_data(nlh);
1977 if (ifm->ifi_index > 0)
1978 dev = __dev_get_by_index(net, ifm->ifi_index);
1979 else if (tb[IFLA_IFNAME])
1980 dev = __dev_get_by_name(net, ifname);
1981 else
1982 goto errout;
1984 if (dev == NULL) {
1985 err = -ENODEV;
1986 goto errout;
1989 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1990 errout:
1991 return err;
1994 static int rtnl_group_dellink(const struct net *net, int group)
1996 struct net_device *dev, *aux;
1997 LIST_HEAD(list_kill);
1998 bool found = false;
2000 if (!group)
2001 return -EPERM;
2003 for_each_netdev(net, dev) {
2004 if (dev->group == group) {
2005 const struct rtnl_link_ops *ops;
2007 found = true;
2008 ops = dev->rtnl_link_ops;
2009 if (!ops || !ops->dellink)
2010 return -EOPNOTSUPP;
2014 if (!found)
2015 return -ENODEV;
2017 for_each_netdev_safe(net, dev, aux) {
2018 if (dev->group == group) {
2019 const struct rtnl_link_ops *ops;
2021 ops = dev->rtnl_link_ops;
2022 ops->dellink(dev, &list_kill);
2025 unregister_netdevice_many(&list_kill);
2027 return 0;
2030 int rtnl_delete_link(struct net_device *dev)
2032 const struct rtnl_link_ops *ops;
2033 LIST_HEAD(list_kill);
2035 ops = dev->rtnl_link_ops;
2036 if (!ops || !ops->dellink)
2037 return -EOPNOTSUPP;
2039 ops->dellink(dev, &list_kill);
2040 unregister_netdevice_many(&list_kill);
2042 return 0;
2044 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2046 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2048 struct net *net = sock_net(skb->sk);
2049 struct net_device *dev;
2050 struct ifinfomsg *ifm;
2051 char ifname[IFNAMSIZ];
2052 struct nlattr *tb[IFLA_MAX+1];
2053 int err;
2055 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2056 if (err < 0)
2057 return err;
2059 if (tb[IFLA_IFNAME])
2060 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2062 ifm = nlmsg_data(nlh);
2063 if (ifm->ifi_index > 0)
2064 dev = __dev_get_by_index(net, ifm->ifi_index);
2065 else if (tb[IFLA_IFNAME])
2066 dev = __dev_get_by_name(net, ifname);
2067 else if (tb[IFLA_GROUP])
2068 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2069 else
2070 return -EINVAL;
2072 if (!dev)
2073 return -ENODEV;
2075 return rtnl_delete_link(dev);
2078 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2080 unsigned int old_flags;
2081 int err;
2083 old_flags = dev->flags;
2084 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2085 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2086 if (err < 0)
2087 return err;
2090 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2091 __dev_notify_flags(dev, old_flags, 0U);
2092 } else {
2093 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2094 __dev_notify_flags(dev, old_flags, ~0U);
2096 return 0;
2098 EXPORT_SYMBOL(rtnl_configure_link);
2100 struct net_device *rtnl_create_link(struct net *net,
2101 const char *ifname, unsigned char name_assign_type,
2102 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2104 int err;
2105 struct net_device *dev;
2106 unsigned int num_tx_queues = 1;
2107 unsigned int num_rx_queues = 1;
2109 if (tb[IFLA_NUM_TX_QUEUES])
2110 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2111 else if (ops->get_num_tx_queues)
2112 num_tx_queues = ops->get_num_tx_queues();
2114 if (tb[IFLA_NUM_RX_QUEUES])
2115 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2116 else if (ops->get_num_rx_queues)
2117 num_rx_queues = ops->get_num_rx_queues();
2119 if (num_tx_queues < 1 || num_tx_queues > 4096)
2120 return ERR_PTR(-EINVAL);
2122 if (num_rx_queues < 1 || num_rx_queues > 4096)
2123 return ERR_PTR(-EINVAL);
2125 err = -ENOMEM;
2126 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2127 ops->setup, num_tx_queues, num_rx_queues);
2128 if (!dev)
2129 goto err;
2131 dev_net_set(dev, net);
2132 dev->rtnl_link_ops = ops;
2133 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2135 if (tb[IFLA_MTU])
2136 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2137 if (tb[IFLA_ADDRESS]) {
2138 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2139 nla_len(tb[IFLA_ADDRESS]));
2140 dev->addr_assign_type = NET_ADDR_SET;
2142 if (tb[IFLA_BROADCAST])
2143 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2144 nla_len(tb[IFLA_BROADCAST]));
2145 if (tb[IFLA_TXQLEN])
2146 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2147 if (tb[IFLA_OPERSTATE])
2148 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2149 if (tb[IFLA_LINKMODE])
2150 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2151 if (tb[IFLA_GROUP])
2152 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2154 return dev;
2156 err:
2157 return ERR_PTR(err);
2159 EXPORT_SYMBOL(rtnl_create_link);
2161 static int rtnl_group_changelink(const struct sk_buff *skb,
2162 struct net *net, int group,
2163 struct ifinfomsg *ifm,
2164 struct nlattr **tb)
2166 struct net_device *dev, *aux;
2167 int err;
2169 for_each_netdev_safe(net, dev, aux) {
2170 if (dev->group == group) {
2171 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2172 if (err < 0)
2173 return err;
2177 return 0;
2180 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2182 struct net *net = sock_net(skb->sk);
2183 const struct rtnl_link_ops *ops;
2184 const struct rtnl_link_ops *m_ops = NULL;
2185 struct net_device *dev;
2186 struct net_device *master_dev = NULL;
2187 struct ifinfomsg *ifm;
2188 char kind[MODULE_NAME_LEN];
2189 char ifname[IFNAMSIZ];
2190 struct nlattr *tb[IFLA_MAX+1];
2191 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2192 unsigned char name_assign_type = NET_NAME_USER;
2193 int err;
2195 #ifdef CONFIG_MODULES
2196 replay:
2197 #endif
2198 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2199 if (err < 0)
2200 return err;
2202 if (tb[IFLA_IFNAME])
2203 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2204 else
2205 ifname[0] = '\0';
2207 ifm = nlmsg_data(nlh);
2208 if (ifm->ifi_index > 0)
2209 dev = __dev_get_by_index(net, ifm->ifi_index);
2210 else {
2211 if (ifname[0])
2212 dev = __dev_get_by_name(net, ifname);
2213 else
2214 dev = NULL;
2217 if (dev) {
2218 master_dev = netdev_master_upper_dev_get(dev);
2219 if (master_dev)
2220 m_ops = master_dev->rtnl_link_ops;
2223 err = validate_linkmsg(dev, tb);
2224 if (err < 0)
2225 return err;
2227 if (tb[IFLA_LINKINFO]) {
2228 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2229 tb[IFLA_LINKINFO], ifla_info_policy);
2230 if (err < 0)
2231 return err;
2232 } else
2233 memset(linkinfo, 0, sizeof(linkinfo));
2235 if (linkinfo[IFLA_INFO_KIND]) {
2236 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2237 ops = rtnl_link_ops_get(kind);
2238 } else {
2239 kind[0] = '\0';
2240 ops = NULL;
2243 if (1) {
2244 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2245 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2246 struct nlattr **data = NULL;
2247 struct nlattr **slave_data = NULL;
2248 struct net *dest_net, *link_net = NULL;
2250 if (ops) {
2251 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2252 err = nla_parse_nested(attr, ops->maxtype,
2253 linkinfo[IFLA_INFO_DATA],
2254 ops->policy);
2255 if (err < 0)
2256 return err;
2257 data = attr;
2259 if (ops->validate) {
2260 err = ops->validate(tb, data);
2261 if (err < 0)
2262 return err;
2266 if (m_ops) {
2267 if (m_ops->slave_maxtype &&
2268 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2269 err = nla_parse_nested(slave_attr,
2270 m_ops->slave_maxtype,
2271 linkinfo[IFLA_INFO_SLAVE_DATA],
2272 m_ops->slave_policy);
2273 if (err < 0)
2274 return err;
2275 slave_data = slave_attr;
2277 if (m_ops->slave_validate) {
2278 err = m_ops->slave_validate(tb, slave_data);
2279 if (err < 0)
2280 return err;
2284 if (dev) {
2285 int status = 0;
2287 if (nlh->nlmsg_flags & NLM_F_EXCL)
2288 return -EEXIST;
2289 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2290 return -EOPNOTSUPP;
2292 if (linkinfo[IFLA_INFO_DATA]) {
2293 if (!ops || ops != dev->rtnl_link_ops ||
2294 !ops->changelink)
2295 return -EOPNOTSUPP;
2297 err = ops->changelink(dev, tb, data);
2298 if (err < 0)
2299 return err;
2300 status |= DO_SETLINK_NOTIFY;
2303 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2304 if (!m_ops || !m_ops->slave_changelink)
2305 return -EOPNOTSUPP;
2307 err = m_ops->slave_changelink(master_dev, dev,
2308 tb, slave_data);
2309 if (err < 0)
2310 return err;
2311 status |= DO_SETLINK_NOTIFY;
2314 return do_setlink(skb, dev, ifm, tb, ifname, status);
2317 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2318 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2319 return rtnl_group_changelink(skb, net,
2320 nla_get_u32(tb[IFLA_GROUP]),
2321 ifm, tb);
2322 return -ENODEV;
2325 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2326 return -EOPNOTSUPP;
2328 if (!ops) {
2329 #ifdef CONFIG_MODULES
2330 if (kind[0]) {
2331 __rtnl_unlock();
2332 request_module("rtnl-link-%s", kind);
2333 rtnl_lock();
2334 ops = rtnl_link_ops_get(kind);
2335 if (ops)
2336 goto replay;
2338 #endif
2339 return -EOPNOTSUPP;
2342 if (!ops->setup)
2343 return -EOPNOTSUPP;
2345 if (!ifname[0]) {
2346 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2347 name_assign_type = NET_NAME_ENUM;
2350 dest_net = rtnl_link_get_net(net, tb);
2351 if (IS_ERR(dest_net))
2352 return PTR_ERR(dest_net);
2354 err = -EPERM;
2355 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2356 goto out;
2358 if (tb[IFLA_LINK_NETNSID]) {
2359 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2361 link_net = get_net_ns_by_id(dest_net, id);
2362 if (!link_net) {
2363 err = -EINVAL;
2364 goto out;
2366 err = -EPERM;
2367 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2368 goto out;
2371 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2372 name_assign_type, ops, tb);
2373 if (IS_ERR(dev)) {
2374 err = PTR_ERR(dev);
2375 goto out;
2378 dev->ifindex = ifm->ifi_index;
2380 if (ops->newlink) {
2381 err = ops->newlink(link_net ? : net, dev, tb, data);
2382 /* Drivers should call free_netdev() in ->destructor
2383 * and unregister it on failure after registration
2384 * so that device could be finally freed in rtnl_unlock.
2386 if (err < 0) {
2387 /* If device is not registered at all, free it now */
2388 if (dev->reg_state == NETREG_UNINITIALIZED)
2389 free_netdev(dev);
2390 goto out;
2392 } else {
2393 err = register_netdevice(dev);
2394 if (err < 0) {
2395 free_netdev(dev);
2396 goto out;
2399 err = rtnl_configure_link(dev, ifm);
2400 if (err < 0)
2401 goto out_unregister;
2402 if (link_net) {
2403 err = dev_change_net_namespace(dev, dest_net, ifname);
2404 if (err < 0)
2405 goto out_unregister;
2407 out:
2408 if (link_net)
2409 put_net(link_net);
2410 put_net(dest_net);
2411 return err;
2412 out_unregister:
2413 if (ops->newlink) {
2414 LIST_HEAD(list_kill);
2416 ops->dellink(dev, &list_kill);
2417 unregister_netdevice_many(&list_kill);
2418 } else {
2419 unregister_netdevice(dev);
2421 goto out;
2425 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2427 struct net *net = sock_net(skb->sk);
2428 struct ifinfomsg *ifm;
2429 char ifname[IFNAMSIZ];
2430 struct nlattr *tb[IFLA_MAX+1];
2431 struct net_device *dev = NULL;
2432 struct sk_buff *nskb;
2433 int err;
2434 u32 ext_filter_mask = 0;
2436 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2437 if (err < 0)
2438 return err;
2440 if (tb[IFLA_IFNAME])
2441 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2443 if (tb[IFLA_EXT_MASK])
2444 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2446 ifm = nlmsg_data(nlh);
2447 if (ifm->ifi_index > 0)
2448 dev = __dev_get_by_index(net, ifm->ifi_index);
2449 else if (tb[IFLA_IFNAME])
2450 dev = __dev_get_by_name(net, ifname);
2451 else
2452 return -EINVAL;
2454 if (dev == NULL)
2455 return -ENODEV;
2457 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2458 if (nskb == NULL)
2459 return -ENOBUFS;
2461 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2462 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2463 if (err < 0) {
2464 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2465 WARN_ON(err == -EMSGSIZE);
2466 kfree_skb(nskb);
2467 } else
2468 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2470 return err;
2473 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2475 struct net *net = sock_net(skb->sk);
2476 struct net_device *dev;
2477 struct nlattr *tb[IFLA_MAX+1];
2478 u32 ext_filter_mask = 0;
2479 u16 min_ifinfo_dump_size = 0;
2480 int hdrlen;
2482 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2483 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2484 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2486 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2487 if (tb[IFLA_EXT_MASK])
2488 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2491 if (!ext_filter_mask)
2492 return NLMSG_GOODSIZE;
2494 * traverse the list of net devices and compute the minimum
2495 * buffer size based upon the filter mask.
2497 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2498 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2499 if_nlmsg_size(dev,
2500 ext_filter_mask));
2503 return min_ifinfo_dump_size;
2506 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2508 int idx;
2509 int s_idx = cb->family;
2511 if (s_idx == 0)
2512 s_idx = 1;
2513 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2514 int type = cb->nlh->nlmsg_type-RTM_BASE;
2515 if (idx < s_idx || idx == PF_PACKET)
2516 continue;
2517 if (rtnl_msg_handlers[idx] == NULL ||
2518 rtnl_msg_handlers[idx][type].dumpit == NULL)
2519 continue;
2520 if (idx > s_idx) {
2521 memset(&cb->args[0], 0, sizeof(cb->args));
2522 cb->prev_seq = 0;
2523 cb->seq = 0;
2525 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2526 break;
2528 cb->family = idx;
2530 return skb->len;
2533 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2534 unsigned int change, gfp_t flags)
2536 struct net *net = dev_net(dev);
2537 struct sk_buff *skb;
2538 int err = -ENOBUFS;
2539 size_t if_info_size;
2541 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2542 if (skb == NULL)
2543 goto errout;
2545 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2546 if (err < 0) {
2547 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2548 WARN_ON(err == -EMSGSIZE);
2549 kfree_skb(skb);
2550 goto errout;
2552 return skb;
2553 errout:
2554 if (err < 0)
2555 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2556 return NULL;
2559 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2561 struct net *net = dev_net(dev);
2563 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2566 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2567 gfp_t flags)
2569 struct sk_buff *skb;
2571 if (dev->reg_state != NETREG_REGISTERED)
2572 return;
2574 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2575 if (skb)
2576 rtmsg_ifinfo_send(skb, dev, flags);
2578 EXPORT_SYMBOL(rtmsg_ifinfo);
2580 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2581 struct net_device *dev,
2582 u8 *addr, u16 vid, u32 pid, u32 seq,
2583 int type, unsigned int flags,
2584 int nlflags)
2586 struct nlmsghdr *nlh;
2587 struct ndmsg *ndm;
2589 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2590 if (!nlh)
2591 return -EMSGSIZE;
2593 ndm = nlmsg_data(nlh);
2594 ndm->ndm_family = AF_BRIDGE;
2595 ndm->ndm_pad1 = 0;
2596 ndm->ndm_pad2 = 0;
2597 ndm->ndm_flags = flags;
2598 ndm->ndm_type = 0;
2599 ndm->ndm_ifindex = dev->ifindex;
2600 ndm->ndm_state = NUD_PERMANENT;
2602 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2603 goto nla_put_failure;
2604 if (vid)
2605 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2606 goto nla_put_failure;
2608 nlmsg_end(skb, nlh);
2609 return 0;
2611 nla_put_failure:
2612 nlmsg_cancel(skb, nlh);
2613 return -EMSGSIZE;
2616 static inline size_t rtnl_fdb_nlmsg_size(void)
2618 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2619 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2620 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2624 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
2626 struct net *net = dev_net(dev);
2627 struct sk_buff *skb;
2628 int err = -ENOBUFS;
2630 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2631 if (!skb)
2632 goto errout;
2634 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2635 0, 0, type, NTF_SELF, 0);
2636 if (err < 0) {
2637 kfree_skb(skb);
2638 goto errout;
2641 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2642 return;
2643 errout:
2644 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2648 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2650 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2651 struct nlattr *tb[],
2652 struct net_device *dev,
2653 const unsigned char *addr, u16 vid,
2654 u16 flags)
2656 int err = -EINVAL;
2658 /* If aging addresses are supported device will need to
2659 * implement its own handler for this.
2661 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2662 pr_info("%s: FDB only supports static addresses\n", dev->name);
2663 return err;
2666 if (vid) {
2667 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2668 return err;
2671 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2672 err = dev_uc_add_excl(dev, addr);
2673 else if (is_multicast_ether_addr(addr))
2674 err = dev_mc_add_excl(dev, addr);
2676 /* Only return duplicate errors if NLM_F_EXCL is set */
2677 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2678 err = 0;
2680 return err;
2682 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2684 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2686 u16 vid = 0;
2688 if (vlan_attr) {
2689 if (nla_len(vlan_attr) != sizeof(u16)) {
2690 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2691 return -EINVAL;
2694 vid = nla_get_u16(vlan_attr);
2696 if (!vid || vid >= VLAN_VID_MASK) {
2697 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2698 vid);
2699 return -EINVAL;
2702 *p_vid = vid;
2703 return 0;
2706 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2708 struct net *net = sock_net(skb->sk);
2709 struct ndmsg *ndm;
2710 struct nlattr *tb[NDA_MAX+1];
2711 struct net_device *dev;
2712 u8 *addr;
2713 u16 vid;
2714 int err;
2716 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2717 if (err < 0)
2718 return err;
2720 ndm = nlmsg_data(nlh);
2721 if (ndm->ndm_ifindex == 0) {
2722 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2723 return -EINVAL;
2726 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2727 if (dev == NULL) {
2728 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2729 return -ENODEV;
2732 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2733 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2734 return -EINVAL;
2737 if (dev->type != ARPHRD_ETHER) {
2738 pr_info("PF_BRIDGE: FDB add only supported for Ethernet devices");
2739 return -EINVAL;
2742 addr = nla_data(tb[NDA_LLADDR]);
2744 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2745 if (err)
2746 return err;
2748 err = -EOPNOTSUPP;
2750 /* Support fdb on master device the net/bridge default case */
2751 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2752 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2753 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2754 const struct net_device_ops *ops = br_dev->netdev_ops;
2756 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2757 nlh->nlmsg_flags);
2758 if (err)
2759 goto out;
2760 else
2761 ndm->ndm_flags &= ~NTF_MASTER;
2764 /* Embedded bridge, macvlan, and any other device support */
2765 if ((ndm->ndm_flags & NTF_SELF)) {
2766 if (dev->netdev_ops->ndo_fdb_add)
2767 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2768 vid,
2769 nlh->nlmsg_flags);
2770 else
2771 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2772 nlh->nlmsg_flags);
2774 if (!err) {
2775 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
2776 ndm->ndm_flags &= ~NTF_SELF;
2779 out:
2780 return err;
2784 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2786 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2787 struct nlattr *tb[],
2788 struct net_device *dev,
2789 const unsigned char *addr, u16 vid)
2791 int err = -EINVAL;
2793 /* If aging addresses are supported device will need to
2794 * implement its own handler for this.
2796 if (!(ndm->ndm_state & NUD_PERMANENT)) {
2797 pr_info("%s: FDB only supports static addresses\n", dev->name);
2798 return err;
2801 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2802 err = dev_uc_del(dev, addr);
2803 else if (is_multicast_ether_addr(addr))
2804 err = dev_mc_del(dev, addr);
2806 return err;
2808 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2810 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2812 struct net *net = sock_net(skb->sk);
2813 struct ndmsg *ndm;
2814 struct nlattr *tb[NDA_MAX+1];
2815 struct net_device *dev;
2816 int err = -EINVAL;
2817 __u8 *addr;
2818 u16 vid;
2820 if (!netlink_capable(skb, CAP_NET_ADMIN))
2821 return -EPERM;
2823 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2824 if (err < 0)
2825 return err;
2827 ndm = nlmsg_data(nlh);
2828 if (ndm->ndm_ifindex == 0) {
2829 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2830 return -EINVAL;
2833 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2834 if (dev == NULL) {
2835 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2836 return -ENODEV;
2839 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2840 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2841 return -EINVAL;
2844 if (dev->type != ARPHRD_ETHER) {
2845 pr_info("PF_BRIDGE: FDB delete only supported for Ethernet devices");
2846 return -EINVAL;
2849 addr = nla_data(tb[NDA_LLADDR]);
2851 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2852 if (err)
2853 return err;
2855 err = -EOPNOTSUPP;
2857 /* Support fdb on master device the net/bridge default case */
2858 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2859 (dev->priv_flags & IFF_BRIDGE_PORT)) {
2860 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2861 const struct net_device_ops *ops = br_dev->netdev_ops;
2863 if (ops->ndo_fdb_del)
2864 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2866 if (err)
2867 goto out;
2868 else
2869 ndm->ndm_flags &= ~NTF_MASTER;
2872 /* Embedded bridge, macvlan, and any other device support */
2873 if (ndm->ndm_flags & NTF_SELF) {
2874 if (dev->netdev_ops->ndo_fdb_del)
2875 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2876 vid);
2877 else
2878 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2880 if (!err) {
2881 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
2882 ndm->ndm_flags &= ~NTF_SELF;
2885 out:
2886 return err;
2889 static int nlmsg_populate_fdb(struct sk_buff *skb,
2890 struct netlink_callback *cb,
2891 struct net_device *dev,
2892 int *idx,
2893 struct netdev_hw_addr_list *list)
2895 struct netdev_hw_addr *ha;
2896 int err;
2897 u32 portid, seq;
2899 portid = NETLINK_CB(cb->skb).portid;
2900 seq = cb->nlh->nlmsg_seq;
2902 list_for_each_entry(ha, &list->list, list) {
2903 if (*idx < cb->args[0])
2904 goto skip;
2906 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2907 portid, seq,
2908 RTM_NEWNEIGH, NTF_SELF,
2909 NLM_F_MULTI);
2910 if (err < 0)
2911 return err;
2912 skip:
2913 *idx += 1;
2915 return 0;
2919 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2920 * @nlh: netlink message header
2921 * @dev: netdevice
2923 * Default netdevice operation to dump the existing unicast address list.
2924 * Returns number of addresses from list put in skb.
2926 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2927 struct netlink_callback *cb,
2928 struct net_device *dev,
2929 struct net_device *filter_dev,
2930 int idx)
2932 int err;
2934 if (dev->type != ARPHRD_ETHER)
2935 return -EINVAL;
2937 netif_addr_lock_bh(dev);
2938 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2939 if (err)
2940 goto out;
2941 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2942 out:
2943 netif_addr_unlock_bh(dev);
2944 return idx;
2946 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2948 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2950 struct net_device *dev;
2951 struct nlattr *tb[IFLA_MAX+1];
2952 struct net_device *br_dev = NULL;
2953 const struct net_device_ops *ops = NULL;
2954 const struct net_device_ops *cops = NULL;
2955 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2956 struct net *net = sock_net(skb->sk);
2957 int brport_idx = 0;
2958 int br_idx = 0;
2959 int idx = 0;
2961 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2962 ifla_policy) == 0) {
2963 if (tb[IFLA_MASTER])
2964 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2967 brport_idx = ifm->ifi_index;
2969 if (br_idx) {
2970 br_dev = __dev_get_by_index(net, br_idx);
2971 if (!br_dev)
2972 return -ENODEV;
2974 ops = br_dev->netdev_ops;
2977 for_each_netdev(net, dev) {
2978 if (brport_idx && (dev->ifindex != brport_idx))
2979 continue;
2981 if (!br_idx) { /* user did not specify a specific bridge */
2982 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2983 br_dev = netdev_master_upper_dev_get(dev);
2984 cops = br_dev->netdev_ops;
2987 } else {
2988 if (dev != br_dev &&
2989 !(dev->priv_flags & IFF_BRIDGE_PORT))
2990 continue;
2992 if (br_dev != netdev_master_upper_dev_get(dev) &&
2993 !(dev->priv_flags & IFF_EBRIDGE))
2994 continue;
2996 cops = ops;
2999 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3000 if (cops && cops->ndo_fdb_dump)
3001 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3002 idx);
3005 if (dev->netdev_ops->ndo_fdb_dump)
3006 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
3007 idx);
3008 else
3009 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
3011 cops = NULL;
3014 cb->args[0] = idx;
3015 return skb->len;
3018 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3019 unsigned int attrnum, unsigned int flag)
3021 if (mask & flag)
3022 return nla_put_u8(skb, attrnum, !!(flags & flag));
3023 return 0;
3026 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3027 struct net_device *dev, u16 mode,
3028 u32 flags, u32 mask, int nlflags,
3029 u32 filter_mask,
3030 int (*vlan_fill)(struct sk_buff *skb,
3031 struct net_device *dev,
3032 u32 filter_mask))
3034 struct nlmsghdr *nlh;
3035 struct ifinfomsg *ifm;
3036 struct nlattr *br_afspec;
3037 struct nlattr *protinfo;
3038 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3039 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3040 int err = 0;
3042 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3043 if (nlh == NULL)
3044 return -EMSGSIZE;
3046 ifm = nlmsg_data(nlh);
3047 ifm->ifi_family = AF_BRIDGE;
3048 ifm->__ifi_pad = 0;
3049 ifm->ifi_type = dev->type;
3050 ifm->ifi_index = dev->ifindex;
3051 ifm->ifi_flags = dev_get_flags(dev);
3052 ifm->ifi_change = 0;
3055 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3056 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3057 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3058 (br_dev &&
3059 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3060 (dev->addr_len &&
3061 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3062 (dev->ifindex != dev_get_iflink(dev) &&
3063 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3064 goto nla_put_failure;
3066 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3067 if (!br_afspec)
3068 goto nla_put_failure;
3070 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3071 nla_nest_cancel(skb, br_afspec);
3072 goto nla_put_failure;
3075 if (mode != BRIDGE_MODE_UNDEF) {
3076 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3077 nla_nest_cancel(skb, br_afspec);
3078 goto nla_put_failure;
3081 if (vlan_fill) {
3082 err = vlan_fill(skb, dev, filter_mask);
3083 if (err) {
3084 nla_nest_cancel(skb, br_afspec);
3085 goto nla_put_failure;
3088 nla_nest_end(skb, br_afspec);
3090 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3091 if (!protinfo)
3092 goto nla_put_failure;
3094 if (brport_nla_put_flag(skb, flags, mask,
3095 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3096 brport_nla_put_flag(skb, flags, mask,
3097 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3098 brport_nla_put_flag(skb, flags, mask,
3099 IFLA_BRPORT_FAST_LEAVE,
3100 BR_MULTICAST_FAST_LEAVE) ||
3101 brport_nla_put_flag(skb, flags, mask,
3102 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3103 brport_nla_put_flag(skb, flags, mask,
3104 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3105 brport_nla_put_flag(skb, flags, mask,
3106 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3107 brport_nla_put_flag(skb, flags, mask,
3108 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3109 brport_nla_put_flag(skb, flags, mask,
3110 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3111 nla_nest_cancel(skb, protinfo);
3112 goto nla_put_failure;
3115 nla_nest_end(skb, protinfo);
3117 nlmsg_end(skb, nlh);
3118 return 0;
3119 nla_put_failure:
3120 nlmsg_cancel(skb, nlh);
3121 return err ? err : -EMSGSIZE;
3123 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3125 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3127 struct net *net = sock_net(skb->sk);
3128 struct net_device *dev;
3129 int idx = 0;
3130 u32 portid = NETLINK_CB(cb->skb).portid;
3131 u32 seq = cb->nlh->nlmsg_seq;
3132 u32 filter_mask = 0;
3133 int err;
3135 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3136 struct nlattr *extfilt;
3138 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3139 IFLA_EXT_MASK);
3140 if (extfilt) {
3141 if (nla_len(extfilt) < sizeof(filter_mask))
3142 return -EINVAL;
3144 filter_mask = nla_get_u32(extfilt);
3148 rcu_read_lock();
3149 for_each_netdev_rcu(net, dev) {
3150 const struct net_device_ops *ops = dev->netdev_ops;
3151 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3153 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3154 if (idx >= cb->args[0]) {
3155 err = br_dev->netdev_ops->ndo_bridge_getlink(
3156 skb, portid, seq, dev,
3157 filter_mask, NLM_F_MULTI);
3158 if (err < 0 && err != -EOPNOTSUPP) {
3159 if (likely(skb->len))
3160 break;
3162 goto out_err;
3165 idx++;
3168 if (ops->ndo_bridge_getlink) {
3169 if (idx >= cb->args[0]) {
3170 err = ops->ndo_bridge_getlink(skb, portid,
3171 seq, dev,
3172 filter_mask,
3173 NLM_F_MULTI);
3174 if (err < 0 && err != -EOPNOTSUPP) {
3175 if (likely(skb->len))
3176 break;
3178 goto out_err;
3181 idx++;
3184 err = skb->len;
3185 out_err:
3186 rcu_read_unlock();
3187 cb->args[0] = idx;
3189 return err;
3192 static inline size_t bridge_nlmsg_size(void)
3194 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3195 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3196 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3197 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3198 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3199 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3200 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3201 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3202 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3203 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3204 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3207 static int rtnl_bridge_notify(struct net_device *dev)
3209 struct net *net = dev_net(dev);
3210 struct sk_buff *skb;
3211 int err = -EOPNOTSUPP;
3213 if (!dev->netdev_ops->ndo_bridge_getlink)
3214 return 0;
3216 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3217 if (!skb) {
3218 err = -ENOMEM;
3219 goto errout;
3222 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3223 if (err < 0)
3224 goto errout;
3226 if (!skb->len)
3227 goto errout;
3229 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3230 return 0;
3231 errout:
3232 WARN_ON(err == -EMSGSIZE);
3233 kfree_skb(skb);
3234 if (err)
3235 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3236 return err;
3239 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3241 struct net *net = sock_net(skb->sk);
3242 struct ifinfomsg *ifm;
3243 struct net_device *dev;
3244 struct nlattr *br_spec, *attr = NULL;
3245 int rem, err = -EOPNOTSUPP;
3246 u16 flags = 0;
3247 bool have_flags = false;
3249 if (nlmsg_len(nlh) < sizeof(*ifm))
3250 return -EINVAL;
3252 ifm = nlmsg_data(nlh);
3253 if (ifm->ifi_family != AF_BRIDGE)
3254 return -EPFNOSUPPORT;
3256 dev = __dev_get_by_index(net, ifm->ifi_index);
3257 if (!dev) {
3258 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3259 return -ENODEV;
3262 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3263 if (br_spec) {
3264 nla_for_each_nested(attr, br_spec, rem) {
3265 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3266 if (nla_len(attr) < sizeof(flags))
3267 return -EINVAL;
3269 have_flags = true;
3270 flags = nla_get_u16(attr);
3271 break;
3276 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3277 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3279 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3280 err = -EOPNOTSUPP;
3281 goto out;
3284 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3285 if (err)
3286 goto out;
3288 flags &= ~BRIDGE_FLAGS_MASTER;
3291 if ((flags & BRIDGE_FLAGS_SELF)) {
3292 if (!dev->netdev_ops->ndo_bridge_setlink)
3293 err = -EOPNOTSUPP;
3294 else
3295 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3296 flags);
3297 if (!err) {
3298 flags &= ~BRIDGE_FLAGS_SELF;
3300 /* Generate event to notify upper layer of bridge
3301 * change
3303 err = rtnl_bridge_notify(dev);
3307 if (have_flags)
3308 memcpy(nla_data(attr), &flags, sizeof(flags));
3309 out:
3310 return err;
3313 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3315 struct net *net = sock_net(skb->sk);
3316 struct ifinfomsg *ifm;
3317 struct net_device *dev;
3318 struct nlattr *br_spec, *attr = NULL;
3319 int rem, err = -EOPNOTSUPP;
3320 u16 flags = 0;
3321 bool have_flags = false;
3323 if (nlmsg_len(nlh) < sizeof(*ifm))
3324 return -EINVAL;
3326 ifm = nlmsg_data(nlh);
3327 if (ifm->ifi_family != AF_BRIDGE)
3328 return -EPFNOSUPPORT;
3330 dev = __dev_get_by_index(net, ifm->ifi_index);
3331 if (!dev) {
3332 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3333 return -ENODEV;
3336 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3337 if (br_spec) {
3338 nla_for_each_nested(attr, br_spec, rem) {
3339 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3340 if (nla_len(attr) < sizeof(flags))
3341 return -EINVAL;
3343 have_flags = true;
3344 flags = nla_get_u16(attr);
3345 break;
3350 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3351 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3353 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3354 err = -EOPNOTSUPP;
3355 goto out;
3358 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3359 if (err)
3360 goto out;
3362 flags &= ~BRIDGE_FLAGS_MASTER;
3365 if ((flags & BRIDGE_FLAGS_SELF)) {
3366 if (!dev->netdev_ops->ndo_bridge_dellink)
3367 err = -EOPNOTSUPP;
3368 else
3369 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3370 flags);
3372 if (!err) {
3373 flags &= ~BRIDGE_FLAGS_SELF;
3375 /* Generate event to notify upper layer of bridge
3376 * change
3378 err = rtnl_bridge_notify(dev);
3382 if (have_flags)
3383 memcpy(nla_data(attr), &flags, sizeof(flags));
3384 out:
3385 return err;
3388 /* Process one rtnetlink message. */
3390 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3392 struct net *net = sock_net(skb->sk);
3393 rtnl_doit_func doit;
3394 int sz_idx, kind;
3395 int family;
3396 int type;
3397 int err;
3399 type = nlh->nlmsg_type;
3400 if (type > RTM_MAX)
3401 return -EOPNOTSUPP;
3403 type -= RTM_BASE;
3405 /* All the messages must have at least 1 byte length */
3406 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3407 return 0;
3409 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3410 sz_idx = type>>2;
3411 kind = type&3;
3413 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3414 return -EPERM;
3416 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3417 struct sock *rtnl;
3418 rtnl_dumpit_func dumpit;
3419 rtnl_calcit_func calcit;
3420 u16 min_dump_alloc = 0;
3422 dumpit = rtnl_get_dumpit(family, type);
3423 if (dumpit == NULL)
3424 return -EOPNOTSUPP;
3425 calcit = rtnl_get_calcit(family, type);
3426 if (calcit)
3427 min_dump_alloc = calcit(skb, nlh);
3429 __rtnl_unlock();
3430 rtnl = net->rtnl;
3432 struct netlink_dump_control c = {
3433 .dump = dumpit,
3434 .min_dump_alloc = min_dump_alloc,
3436 err = netlink_dump_start(rtnl, skb, nlh, &c);
3438 rtnl_lock();
3439 return err;
3442 doit = rtnl_get_doit(family, type);
3443 if (doit == NULL)
3444 return -EOPNOTSUPP;
3446 return doit(skb, nlh);
3449 static void rtnetlink_rcv(struct sk_buff *skb)
3451 rtnl_lock();
3452 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3453 rtnl_unlock();
3456 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3458 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3460 switch (event) {
3461 case NETDEV_UP:
3462 case NETDEV_DOWN:
3463 case NETDEV_PRE_UP:
3464 case NETDEV_POST_INIT:
3465 case NETDEV_REGISTER:
3466 case NETDEV_CHANGE:
3467 case NETDEV_PRE_TYPE_CHANGE:
3468 case NETDEV_GOING_DOWN:
3469 case NETDEV_UNREGISTER:
3470 case NETDEV_UNREGISTER_FINAL:
3471 case NETDEV_RELEASE:
3472 case NETDEV_JOIN:
3473 case NETDEV_BONDING_INFO:
3474 break;
3475 default:
3476 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3477 break;
3479 return NOTIFY_DONE;
3482 static struct notifier_block rtnetlink_dev_notifier = {
3483 .notifier_call = rtnetlink_event,
3487 static int __net_init rtnetlink_net_init(struct net *net)
3489 struct sock *sk;
3490 struct netlink_kernel_cfg cfg = {
3491 .groups = RTNLGRP_MAX,
3492 .input = rtnetlink_rcv,
3493 .cb_mutex = &rtnl_mutex,
3494 .flags = NL_CFG_F_NONROOT_RECV,
3497 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3498 if (!sk)
3499 return -ENOMEM;
3500 net->rtnl = sk;
3501 return 0;
3504 static void __net_exit rtnetlink_net_exit(struct net *net)
3506 netlink_kernel_release(net->rtnl);
3507 net->rtnl = NULL;
3510 static struct pernet_operations rtnetlink_net_ops = {
3511 .init = rtnetlink_net_init,
3512 .exit = rtnetlink_net_exit,
3515 void __init rtnetlink_init(void)
3517 if (register_pernet_subsys(&rtnetlink_net_ops))
3518 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3520 register_netdevice_notifier(&rtnetlink_dev_notifier);
3522 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3523 rtnl_dump_ifinfo, rtnl_calcit);
3524 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3525 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3526 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3528 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3529 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3531 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3532 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3533 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3535 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3536 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3537 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);