2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/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>
49 #include <net/protocol.h>
51 #include <net/route.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
62 rtnl_dumpit_func dumpit
;
63 rtnl_calcit_func calcit
;
66 static DEFINE_MUTEX(rtnl_mutex
);
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. */
84 EXPORT_SYMBOL(rtnl_unlock
);
86 int rtnl_trylock(void)
88 return mutex_trylock(&rtnl_mutex
);
90 EXPORT_SYMBOL(rtnl_trylock
);
92 int rtnl_is_locked(void)
94 return mutex_is_locked(&rtnl_mutex
);
96 EXPORT_SYMBOL(rtnl_is_locked
);
98 #ifdef CONFIG_PROVE_LOCKING
99 int lockdep_rtnl_is_held(void)
101 return lockdep_is_held(&rtnl_mutex
);
103 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
104 #endif /* #ifdef CONFIG_PROVE_LOCKING */
106 static struct rtnl_link
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
108 static inline int rtm_msgindex(int msgtype
)
110 int msgindex
= msgtype
- RTM_BASE
;
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
117 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
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
];
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
];
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
];
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
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
;
192 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
193 msgindex
= rtm_msgindex(msgtype
);
195 tab
= rtnl_msg_handlers
[protocol
];
197 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
201 rtnl_msg_handlers
[protocol
] = tab
;
205 tab
[msgindex
].doit
= doit
;
208 tab
[msgindex
].dumpit
= dumpit
;
211 tab
[msgindex
].calcit
= calcit
;
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",
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
)
248 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
249 msgindex
= rtm_msgindex(msgtype
);
251 if (rtnl_msg_handlers
[protocol
] == NULL
)
254 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
255 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
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
))
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
))
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
);
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
)
329 err
= __rtnl_link_register(ops
);
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
)
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)
371 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
373 add_wait_queue(&netdev_unregistering_wq
, &wait
);
375 unregistering
= false;
378 if (net
->dev_unreg_count
> 0) {
379 unregistering
= true;
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
);
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
);
415 ops
= master_dev
->rtnl_link_ops
;
416 if (!ops
|| !ops
->get_slave_size
)
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
;
431 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
435 /* IFLA_INFO_DATA + nested data */
436 size
+= nla_total_size(sizeof(struct nlattr
)) +
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
);
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
)
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
)
471 list_add_tail(&ops
->list
, &rtnl_af_ops
);
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
)
495 __rtnl_af_unregister(ops
);
498 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
500 static size_t rtnl_link_get_af_size(const struct net_device
*dev
)
502 struct rtnl_af_ops
*af_ops
;
506 size
= nla_total_size(sizeof(struct nlattr
));
508 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
509 if (af_ops
->get_link_af_size
) {
510 /* AF_* + nested data */
511 size
+= nla_total_size(sizeof(struct nlattr
)) +
512 af_ops
->get_link_af_size(dev
);
519 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
521 struct net_device
*master_dev
;
523 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
524 if (master_dev
&& master_dev
->rtnl_link_ops
)
529 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
530 const struct net_device
*dev
)
532 struct net_device
*master_dev
;
533 const struct rtnl_link_ops
*ops
;
534 struct nlattr
*slave_data
;
537 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
540 ops
= master_dev
->rtnl_link_ops
;
543 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
545 if (ops
->fill_slave_info
) {
546 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
549 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
551 goto err_cancel_slave_data
;
552 nla_nest_end(skb
, slave_data
);
556 err_cancel_slave_data
:
557 nla_nest_cancel(skb
, slave_data
);
561 static int rtnl_link_info_fill(struct sk_buff
*skb
,
562 const struct net_device
*dev
)
564 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
570 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
572 if (ops
->fill_xstats
) {
573 err
= ops
->fill_xstats(skb
, dev
);
577 if (ops
->fill_info
) {
578 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
581 err
= ops
->fill_info(skb
, dev
);
583 goto err_cancel_data
;
584 nla_nest_end(skb
, data
);
589 nla_nest_cancel(skb
, data
);
593 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
595 struct nlattr
*linkinfo
;
598 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
599 if (linkinfo
== NULL
)
602 err
= rtnl_link_info_fill(skb
, dev
);
604 goto err_cancel_link
;
606 err
= rtnl_link_slave_info_fill(skb
, dev
);
608 goto err_cancel_link
;
610 nla_nest_end(skb
, linkinfo
);
614 nla_nest_cancel(skb
, linkinfo
);
619 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
621 struct sock
*rtnl
= net
->rtnl
;
624 NETLINK_CB(skb
).dst_group
= group
;
626 atomic_inc(&skb
->users
);
627 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
629 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
633 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
635 struct sock
*rtnl
= net
->rtnl
;
637 return nlmsg_unicast(rtnl
, skb
, pid
);
639 EXPORT_SYMBOL(rtnl_unicast
);
641 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
642 struct nlmsghdr
*nlh
, gfp_t flags
)
644 struct sock
*rtnl
= net
->rtnl
;
648 report
= nlmsg_report(nlh
);
650 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
652 EXPORT_SYMBOL(rtnl_notify
);
654 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
656 struct sock
*rtnl
= net
->rtnl
;
658 netlink_set_err(rtnl
, 0, group
, error
);
660 EXPORT_SYMBOL(rtnl_set_sk_err
);
662 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
667 mx
= nla_nest_start(skb
, RTA_METRICS
);
671 for (i
= 0; i
< RTAX_MAX
; i
++) {
673 if (i
== RTAX_CC_ALGO
- 1) {
674 char tmp
[TCP_CA_NAME_MAX
], *name
;
676 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
679 if (nla_put_string(skb
, i
+ 1, name
))
680 goto nla_put_failure
;
682 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
683 goto nla_put_failure
;
690 nla_nest_cancel(skb
, mx
);
694 return nla_nest_end(skb
, mx
);
697 nla_nest_cancel(skb
, mx
);
700 EXPORT_SYMBOL(rtnetlink_put_metrics
);
702 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
703 long expires
, u32 error
)
705 struct rta_cacheinfo ci
= {
706 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
707 .rta_used
= dst
->__use
,
708 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
716 clock
= jiffies_to_clock_t(abs(expires
));
717 clock
= min_t(unsigned long, clock
, INT_MAX
);
718 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
720 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
722 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
724 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
726 unsigned char operstate
= dev
->operstate
;
728 switch (transition
) {
730 if ((operstate
== IF_OPER_DORMANT
||
731 operstate
== IF_OPER_UNKNOWN
) &&
733 operstate
= IF_OPER_UP
;
736 case IF_OPER_DORMANT
:
737 if (operstate
== IF_OPER_UP
||
738 operstate
== IF_OPER_UNKNOWN
)
739 operstate
= IF_OPER_DORMANT
;
743 if (dev
->operstate
!= operstate
) {
744 write_lock_bh(&dev_base_lock
);
745 dev
->operstate
= operstate
;
746 write_unlock_bh(&dev_base_lock
);
747 netdev_state_change(dev
);
751 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
753 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
754 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
757 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
758 const struct ifinfomsg
*ifm
)
760 unsigned int flags
= ifm
->ifi_flags
;
762 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
764 flags
= (flags
& ifm
->ifi_change
) |
765 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
770 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
771 const struct rtnl_link_stats64
*b
)
773 a
->rx_packets
= b
->rx_packets
;
774 a
->tx_packets
= b
->tx_packets
;
775 a
->rx_bytes
= b
->rx_bytes
;
776 a
->tx_bytes
= b
->tx_bytes
;
777 a
->rx_errors
= b
->rx_errors
;
778 a
->tx_errors
= b
->tx_errors
;
779 a
->rx_dropped
= b
->rx_dropped
;
780 a
->tx_dropped
= b
->tx_dropped
;
782 a
->multicast
= b
->multicast
;
783 a
->collisions
= b
->collisions
;
785 a
->rx_length_errors
= b
->rx_length_errors
;
786 a
->rx_over_errors
= b
->rx_over_errors
;
787 a
->rx_crc_errors
= b
->rx_crc_errors
;
788 a
->rx_frame_errors
= b
->rx_frame_errors
;
789 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
790 a
->rx_missed_errors
= b
->rx_missed_errors
;
792 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
793 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
794 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
795 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
796 a
->tx_window_errors
= b
->tx_window_errors
;
798 a
->rx_compressed
= b
->rx_compressed
;
799 a
->tx_compressed
= b
->tx_compressed
;
802 static void copy_rtnl_link_stats64(void *v
, const struct rtnl_link_stats64
*b
)
804 memcpy(v
, b
, sizeof(*b
));
808 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
811 if (dev
->dev
.parent
&& dev_is_pci(dev
->dev
.parent
) &&
812 (ext_filter_mask
& RTEXT_FILTER_VF
)) {
813 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
814 size_t size
= nla_total_size(sizeof(struct nlattr
));
815 size
+= nla_total_size(num_vfs
* sizeof(struct nlattr
));
817 (nla_total_size(sizeof(struct ifla_vf_mac
)) +
818 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
819 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
820 nla_total_size(sizeof(struct ifla_vf_rate
)) +
821 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
822 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
823 /* IFLA_VF_STATS_RX_PACKETS */
824 nla_total_size(sizeof(__u64
)) +
825 /* IFLA_VF_STATS_TX_PACKETS */
826 nla_total_size(sizeof(__u64
)) +
827 /* IFLA_VF_STATS_RX_BYTES */
828 nla_total_size(sizeof(__u64
)) +
829 /* IFLA_VF_STATS_TX_BYTES */
830 nla_total_size(sizeof(__u64
)) +
831 /* IFLA_VF_STATS_BROADCAST */
832 nla_total_size(sizeof(__u64
)) +
833 /* IFLA_VF_STATS_MULTICAST */
834 nla_total_size(sizeof(__u64
)));
840 static size_t rtnl_port_size(const struct net_device
*dev
,
843 size_t port_size
= nla_total_size(4) /* PORT_VF */
844 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
845 + nla_total_size(sizeof(struct ifla_port_vsi
))
847 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
848 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
849 + nla_total_size(1) /* PROT_VDP_REQUEST */
850 + nla_total_size(2); /* PORT_VDP_RESPONSE */
851 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
852 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
854 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
857 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
858 !(ext_filter_mask
& RTEXT_FILTER_VF
))
860 if (dev_num_vf(dev
->dev
.parent
))
861 return port_self_size
+ vf_ports_size
+
862 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
864 return port_self_size
;
867 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
870 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
871 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
872 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
873 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
874 + nla_total_size(sizeof(struct rtnl_link_ifmap
))
875 + nla_total_size(sizeof(struct rtnl_link_stats
))
876 + nla_total_size(sizeof(struct rtnl_link_stats64
))
877 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
878 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
879 + nla_total_size(4) /* IFLA_TXQLEN */
880 + nla_total_size(4) /* IFLA_WEIGHT */
881 + nla_total_size(4) /* IFLA_MTU */
882 + nla_total_size(4) /* IFLA_LINK */
883 + nla_total_size(4) /* IFLA_MASTER */
884 + nla_total_size(1) /* IFLA_CARRIER */
885 + nla_total_size(4) /* IFLA_PROMISCUITY */
886 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
887 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
888 + nla_total_size(1) /* IFLA_OPERSTATE */
889 + nla_total_size(1) /* IFLA_LINKMODE */
890 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
891 + nla_total_size(4) /* IFLA_LINK_NETNSID */
892 + nla_total_size(ext_filter_mask
893 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
894 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
895 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
896 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
897 + rtnl_link_get_af_size(dev
) /* IFLA_AF_SPEC */
898 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
899 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
); /* IFLA_PHYS_SWITCH_ID */
902 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
904 struct nlattr
*vf_ports
;
905 struct nlattr
*vf_port
;
909 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
913 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
914 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
916 goto nla_put_failure
;
917 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
918 goto nla_put_failure
;
919 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
920 if (err
== -EMSGSIZE
)
921 goto nla_put_failure
;
923 nla_nest_cancel(skb
, vf_port
);
926 nla_nest_end(skb
, vf_port
);
929 nla_nest_end(skb
, vf_ports
);
934 nla_nest_cancel(skb
, vf_ports
);
938 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
940 struct nlattr
*port_self
;
943 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
947 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
949 nla_nest_cancel(skb
, port_self
);
950 return (err
== -EMSGSIZE
) ? err
: 0;
953 nla_nest_end(skb
, port_self
);
958 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
963 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
964 !(ext_filter_mask
& RTEXT_FILTER_VF
))
967 err
= rtnl_port_self_fill(skb
, dev
);
971 if (dev_num_vf(dev
->dev
.parent
)) {
972 err
= rtnl_vf_ports_fill(skb
, dev
);
980 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
983 struct netdev_phys_item_id ppid
;
985 err
= dev_get_phys_port_id(dev
, &ppid
);
987 if (err
== -EOPNOTSUPP
)
992 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
998 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1000 char name
[IFNAMSIZ
];
1003 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1005 if (err
== -EOPNOTSUPP
)
1010 if (nla_put(skb
, IFLA_PHYS_PORT_NAME
, strlen(name
), name
))
1016 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1019 struct switchdev_attr attr
= {
1020 .id
= SWITCHDEV_ATTR_PORT_PARENT_ID
,
1021 .flags
= SWITCHDEV_F_NO_RECURSE
,
1024 err
= switchdev_port_attr_get(dev
, &attr
);
1026 if (err
== -EOPNOTSUPP
)
1031 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1038 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
1039 int type
, u32 pid
, u32 seq
, u32 change
,
1040 unsigned int flags
, u32 ext_filter_mask
)
1042 struct ifinfomsg
*ifm
;
1043 struct nlmsghdr
*nlh
;
1044 struct rtnl_link_stats64 temp
;
1045 const struct rtnl_link_stats64
*stats
;
1046 struct nlattr
*attr
, *af_spec
;
1047 struct rtnl_af_ops
*af_ops
;
1048 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1051 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1055 ifm
= nlmsg_data(nlh
);
1056 ifm
->ifi_family
= AF_UNSPEC
;
1058 ifm
->ifi_type
= dev
->type
;
1059 ifm
->ifi_index
= dev
->ifindex
;
1060 ifm
->ifi_flags
= dev_get_flags(dev
);
1061 ifm
->ifi_change
= change
;
1063 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1064 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1065 nla_put_u8(skb
, IFLA_OPERSTATE
,
1066 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1067 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1068 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1069 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1070 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1071 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1073 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1075 (dev
->ifindex
!= dev_get_iflink(dev
) &&
1076 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
1078 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1079 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1081 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1083 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)) ||
1084 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1085 atomic_read(&dev
->carrier_changes
)))
1086 goto nla_put_failure
;
1089 struct rtnl_link_ifmap map
= {
1090 .mem_start
= dev
->mem_start
,
1091 .mem_end
= dev
->mem_end
,
1092 .base_addr
= dev
->base_addr
,
1095 .port
= dev
->if_port
,
1097 if (nla_put(skb
, IFLA_MAP
, sizeof(map
), &map
))
1098 goto nla_put_failure
;
1101 if (dev
->addr_len
) {
1102 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1103 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1104 goto nla_put_failure
;
1107 if (rtnl_phys_port_id_fill(skb
, dev
))
1108 goto nla_put_failure
;
1110 if (rtnl_phys_port_name_fill(skb
, dev
))
1111 goto nla_put_failure
;
1113 if (rtnl_phys_switch_id_fill(skb
, dev
))
1114 goto nla_put_failure
;
1116 attr
= nla_reserve(skb
, IFLA_STATS
,
1117 sizeof(struct rtnl_link_stats
));
1119 goto nla_put_failure
;
1121 stats
= dev_get_stats(dev
, &temp
);
1122 copy_rtnl_link_stats(nla_data(attr
), stats
);
1124 attr
= nla_reserve(skb
, IFLA_STATS64
,
1125 sizeof(struct rtnl_link_stats64
));
1127 goto nla_put_failure
;
1128 copy_rtnl_link_stats64(nla_data(attr
), stats
);
1130 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
1131 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
1132 goto nla_put_failure
;
1134 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
1135 && (ext_filter_mask
& RTEXT_FILTER_VF
)) {
1138 struct nlattr
*vfinfo
, *vf
, *vfstats
;
1139 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1141 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1143 goto nla_put_failure
;
1144 for (i
= 0; i
< num_vfs
; i
++) {
1145 struct ifla_vf_info ivi
;
1146 struct ifla_vf_mac vf_mac
;
1147 struct ifla_vf_vlan vf_vlan
;
1148 struct ifla_vf_rate vf_rate
;
1149 struct ifla_vf_tx_rate vf_tx_rate
;
1150 struct ifla_vf_spoofchk vf_spoofchk
;
1151 struct ifla_vf_link_state vf_linkstate
;
1152 struct ifla_vf_rss_query_en vf_rss_query_en
;
1153 struct ifla_vf_stats vf_stats
;
1156 * Not all SR-IOV capable drivers support the
1157 * spoofcheck and "RSS query enable" query. Preset to
1158 * -1 so the user space tool can detect that the driver
1159 * didn't report anything.
1162 ivi
.rss_query_en
= -1;
1163 memset(ivi
.mac
, 0, sizeof(ivi
.mac
));
1164 /* The default value for VF link state is "auto"
1165 * IFLA_VF_LINK_STATE_AUTO which equals zero
1168 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, i
, &ivi
))
1176 vf_rss_query_en
.vf
= ivi
.vf
;
1178 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1179 vf_vlan
.vlan
= ivi
.vlan
;
1180 vf_vlan
.qos
= ivi
.qos
;
1181 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1182 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1183 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1184 vf_spoofchk
.setting
= ivi
.spoofchk
;
1185 vf_linkstate
.link_state
= ivi
.linkstate
;
1186 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1187 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1189 nla_nest_cancel(skb
, vfinfo
);
1190 goto nla_put_failure
;
1192 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1193 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1194 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1196 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1198 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1200 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1202 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1203 sizeof(vf_rss_query_en
),
1205 goto nla_put_failure
;
1206 memset(&vf_stats
, 0, sizeof(vf_stats
));
1207 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1208 dev
->netdev_ops
->ndo_get_vf_stats(dev
, i
,
1210 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1212 nla_nest_cancel(skb
, vf
);
1213 nla_nest_cancel(skb
, vfinfo
);
1214 goto nla_put_failure
;
1216 if (nla_put_u64(skb
, IFLA_VF_STATS_RX_PACKETS
,
1217 vf_stats
.rx_packets
) ||
1218 nla_put_u64(skb
, IFLA_VF_STATS_TX_PACKETS
,
1219 vf_stats
.tx_packets
) ||
1220 nla_put_u64(skb
, IFLA_VF_STATS_RX_BYTES
,
1221 vf_stats
.rx_bytes
) ||
1222 nla_put_u64(skb
, IFLA_VF_STATS_TX_BYTES
,
1223 vf_stats
.tx_bytes
) ||
1224 nla_put_u64(skb
, IFLA_VF_STATS_BROADCAST
,
1225 vf_stats
.broadcast
) ||
1226 nla_put_u64(skb
, IFLA_VF_STATS_MULTICAST
,
1227 vf_stats
.multicast
))
1228 goto nla_put_failure
;
1229 nla_nest_end(skb
, vfstats
);
1230 nla_nest_end(skb
, vf
);
1232 nla_nest_end(skb
, vfinfo
);
1235 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1236 goto nla_put_failure
;
1238 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1239 if (rtnl_link_fill(skb
, dev
) < 0)
1240 goto nla_put_failure
;
1243 if (dev
->rtnl_link_ops
&&
1244 dev
->rtnl_link_ops
->get_link_net
) {
1245 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1247 if (!net_eq(dev_net(dev
), link_net
)) {
1248 int id
= peernet2id_alloc(dev_net(dev
), link_net
);
1250 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1251 goto nla_put_failure
;
1255 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1256 goto nla_put_failure
;
1258 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1259 if (af_ops
->fill_link_af
) {
1263 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1264 goto nla_put_failure
;
1266 err
= af_ops
->fill_link_af(skb
, dev
);
1269 * Caller may return ENODATA to indicate that there
1270 * was no data to be dumped. This is not an error, it
1271 * means we should trim the attribute header and
1274 if (err
== -ENODATA
)
1275 nla_nest_cancel(skb
, af
);
1277 goto nla_put_failure
;
1279 nla_nest_end(skb
, af
);
1283 nla_nest_end(skb
, af_spec
);
1285 nlmsg_end(skb
, nlh
);
1289 nlmsg_cancel(skb
, nlh
);
1293 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1294 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1295 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1296 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1297 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1298 [IFLA_MTU
] = { .type
= NLA_U32
},
1299 [IFLA_LINK
] = { .type
= NLA_U32
},
1300 [IFLA_MASTER
] = { .type
= NLA_U32
},
1301 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1302 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1303 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1304 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1305 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1306 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1307 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1308 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1309 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1310 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1311 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1312 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1313 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1314 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1315 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1316 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1317 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1318 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1319 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1320 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1321 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1324 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1325 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1326 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1327 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1328 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1331 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1332 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1333 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1334 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1335 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1336 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1337 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1338 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1339 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1342 static const struct nla_policy ifla_vf_stats_policy
[IFLA_VF_STATS_MAX
+ 1] = {
1343 [IFLA_VF_STATS_RX_PACKETS
] = { .type
= NLA_U64
},
1344 [IFLA_VF_STATS_TX_PACKETS
] = { .type
= NLA_U64
},
1345 [IFLA_VF_STATS_RX_BYTES
] = { .type
= NLA_U64
},
1346 [IFLA_VF_STATS_TX_BYTES
] = { .type
= NLA_U64
},
1347 [IFLA_VF_STATS_BROADCAST
] = { .type
= NLA_U64
},
1348 [IFLA_VF_STATS_MULTICAST
] = { .type
= NLA_U64
},
1351 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1352 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1353 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1354 .len
= PORT_PROFILE_MAX
},
1355 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1356 .len
= sizeof(struct ifla_port_vsi
)},
1357 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1358 .len
= PORT_UUID_MAX
},
1359 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1360 .len
= PORT_UUID_MAX
},
1361 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1362 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1365 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1367 struct net
*net
= sock_net(skb
->sk
);
1370 struct net_device
*dev
;
1371 struct hlist_head
*head
;
1372 struct nlattr
*tb
[IFLA_MAX
+1];
1373 u32 ext_filter_mask
= 0;
1378 s_idx
= cb
->args
[1];
1380 cb
->seq
= net
->dev_base_seq
;
1382 /* A hack to preserve kernel<->userspace interface.
1383 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1384 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1385 * what iproute2 < v3.9.0 used.
1386 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1387 * attribute, its netlink message is shorter than struct ifinfomsg.
1389 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1390 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1392 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
1394 if (tb
[IFLA_EXT_MASK
])
1395 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1398 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1400 head
= &net
->dev_index_head
[h
];
1401 hlist_for_each_entry(dev
, head
, index_hlist
) {
1404 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1405 NETLINK_CB(cb
->skb
).portid
,
1406 cb
->nlh
->nlmsg_seq
, 0,
1409 /* If we ran out of room on the first message,
1412 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
1417 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1429 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
)
1431 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
);
1433 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1435 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1438 /* Examine the link attributes and figure out which
1439 * network namespace we are talking about.
1441 if (tb
[IFLA_NET_NS_PID
])
1442 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1443 else if (tb
[IFLA_NET_NS_FD
])
1444 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1446 net
= get_net(src_net
);
1449 EXPORT_SYMBOL(rtnl_link_get_net
);
1451 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1454 if (tb
[IFLA_ADDRESS
] &&
1455 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1458 if (tb
[IFLA_BROADCAST
] &&
1459 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1463 if (tb
[IFLA_AF_SPEC
]) {
1467 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1468 const struct rtnl_af_ops
*af_ops
;
1470 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1471 return -EAFNOSUPPORT
;
1473 if (!af_ops
->set_link_af
)
1476 if (af_ops
->validate_link_af
) {
1477 err
= af_ops
->validate_link_af(dev
, af
);
1487 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1489 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1492 if (tb
[IFLA_VF_MAC
]) {
1493 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1496 if (ops
->ndo_set_vf_mac
)
1497 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1503 if (tb
[IFLA_VF_VLAN
]) {
1504 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1507 if (ops
->ndo_set_vf_vlan
)
1508 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
1514 if (tb
[IFLA_VF_TX_RATE
]) {
1515 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
1516 struct ifla_vf_info ivf
;
1519 if (ops
->ndo_get_vf_config
)
1520 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1525 if (ops
->ndo_set_vf_rate
)
1526 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1533 if (tb
[IFLA_VF_RATE
]) {
1534 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1537 if (ops
->ndo_set_vf_rate
)
1538 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1545 if (tb
[IFLA_VF_SPOOFCHK
]) {
1546 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1549 if (ops
->ndo_set_vf_spoofchk
)
1550 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1556 if (tb
[IFLA_VF_LINK_STATE
]) {
1557 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1560 if (ops
->ndo_set_vf_link_state
)
1561 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1567 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1568 struct ifla_vf_rss_query_en
*ivrssq_en
;
1571 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
1572 if (ops
->ndo_set_vf_rss_query_en
)
1573 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
1574 ivrssq_en
->setting
);
1582 static int do_set_master(struct net_device
*dev
, int ifindex
)
1584 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1585 const struct net_device_ops
*ops
;
1589 if (upper_dev
->ifindex
== ifindex
)
1591 ops
= upper_dev
->netdev_ops
;
1592 if (ops
->ndo_del_slave
) {
1593 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1602 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1605 ops
= upper_dev
->netdev_ops
;
1606 if (ops
->ndo_add_slave
) {
1607 err
= ops
->ndo_add_slave(upper_dev
, dev
);
1617 #define DO_SETLINK_MODIFIED 0x01
1618 /* notify flag means notify + modified. */
1619 #define DO_SETLINK_NOTIFY 0x03
1620 static int do_setlink(const struct sk_buff
*skb
,
1621 struct net_device
*dev
, struct ifinfomsg
*ifm
,
1622 struct nlattr
**tb
, char *ifname
, int status
)
1624 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1627 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1628 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1633 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1638 err
= dev_change_net_namespace(dev
, net
, ifname
);
1642 status
|= DO_SETLINK_MODIFIED
;
1646 struct rtnl_link_ifmap
*u_map
;
1649 if (!ops
->ndo_set_config
) {
1654 if (!netif_device_present(dev
)) {
1659 u_map
= nla_data(tb
[IFLA_MAP
]);
1660 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1661 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1662 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1663 k_map
.irq
= (unsigned char) u_map
->irq
;
1664 k_map
.dma
= (unsigned char) u_map
->dma
;
1665 k_map
.port
= (unsigned char) u_map
->port
;
1667 err
= ops
->ndo_set_config(dev
, &k_map
);
1671 status
|= DO_SETLINK_NOTIFY
;
1674 if (tb
[IFLA_ADDRESS
]) {
1675 struct sockaddr
*sa
;
1678 len
= sizeof(sa_family_t
) + dev
->addr_len
;
1679 sa
= kmalloc(len
, GFP_KERNEL
);
1684 sa
->sa_family
= dev
->type
;
1685 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1687 err
= dev_set_mac_address(dev
, sa
);
1691 status
|= DO_SETLINK_MODIFIED
;
1695 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1698 status
|= DO_SETLINK_MODIFIED
;
1701 if (tb
[IFLA_GROUP
]) {
1702 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1703 status
|= DO_SETLINK_NOTIFY
;
1707 * Interface selected by interface index but interface
1708 * name provided implies that a name change has been
1711 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1712 err
= dev_change_name(dev
, ifname
);
1715 status
|= DO_SETLINK_MODIFIED
;
1718 if (tb
[IFLA_IFALIAS
]) {
1719 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
1720 nla_len(tb
[IFLA_IFALIAS
]));
1723 status
|= DO_SETLINK_NOTIFY
;
1726 if (tb
[IFLA_BROADCAST
]) {
1727 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
1728 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
1731 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
1732 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1737 if (tb
[IFLA_MASTER
]) {
1738 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1741 status
|= DO_SETLINK_MODIFIED
;
1744 if (tb
[IFLA_CARRIER
]) {
1745 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
1748 status
|= DO_SETLINK_MODIFIED
;
1751 if (tb
[IFLA_TXQLEN
]) {
1752 unsigned long value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1754 if (dev
->tx_queue_len
^ value
)
1755 status
|= DO_SETLINK_NOTIFY
;
1757 dev
->tx_queue_len
= value
;
1760 if (tb
[IFLA_OPERSTATE
])
1761 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1763 if (tb
[IFLA_LINKMODE
]) {
1764 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1766 write_lock_bh(&dev_base_lock
);
1767 if (dev
->link_mode
^ value
)
1768 status
|= DO_SETLINK_NOTIFY
;
1769 dev
->link_mode
= value
;
1770 write_unlock_bh(&dev_base_lock
);
1773 if (tb
[IFLA_VFINFO_LIST
]) {
1774 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
1775 struct nlattr
*attr
;
1778 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
1779 if (nla_type(attr
) != IFLA_VF_INFO
||
1780 nla_len(attr
) < NLA_HDRLEN
) {
1784 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
1788 err
= do_setvfinfo(dev
, vfinfo
);
1791 status
|= DO_SETLINK_NOTIFY
;
1796 if (tb
[IFLA_VF_PORTS
]) {
1797 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1798 struct nlattr
*attr
;
1803 if (!ops
->ndo_set_vf_port
)
1806 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
1807 if (nla_type(attr
) != IFLA_VF_PORT
||
1808 nla_len(attr
) < NLA_HDRLEN
) {
1812 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
1816 if (!port
[IFLA_PORT_VF
]) {
1820 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
1821 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
1824 status
|= DO_SETLINK_NOTIFY
;
1829 if (tb
[IFLA_PORT_SELF
]) {
1830 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1832 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1833 tb
[IFLA_PORT_SELF
], ifla_port_policy
);
1838 if (ops
->ndo_set_vf_port
)
1839 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
1842 status
|= DO_SETLINK_NOTIFY
;
1845 if (tb
[IFLA_AF_SPEC
]) {
1849 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1850 const struct rtnl_af_ops
*af_ops
;
1852 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1855 err
= af_ops
->set_link_af(dev
, af
);
1859 status
|= DO_SETLINK_NOTIFY
;
1865 if (status
& DO_SETLINK_MODIFIED
) {
1866 if (status
& DO_SETLINK_NOTIFY
)
1867 netdev_state_change(dev
);
1870 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",
1877 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1879 struct net
*net
= sock_net(skb
->sk
);
1880 struct ifinfomsg
*ifm
;
1881 struct net_device
*dev
;
1883 struct nlattr
*tb
[IFLA_MAX
+1];
1884 char ifname
[IFNAMSIZ
];
1886 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1890 if (tb
[IFLA_IFNAME
])
1891 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1896 ifm
= nlmsg_data(nlh
);
1897 if (ifm
->ifi_index
> 0)
1898 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1899 else if (tb
[IFLA_IFNAME
])
1900 dev
= __dev_get_by_name(net
, ifname
);
1909 err
= validate_linkmsg(dev
, tb
);
1913 err
= do_setlink(skb
, dev
, ifm
, tb
, ifname
, 0);
1918 static int rtnl_group_dellink(const struct net
*net
, int group
)
1920 struct net_device
*dev
, *aux
;
1921 LIST_HEAD(list_kill
);
1927 for_each_netdev(net
, dev
) {
1928 if (dev
->group
== group
) {
1929 const struct rtnl_link_ops
*ops
;
1932 ops
= dev
->rtnl_link_ops
;
1933 if (!ops
|| !ops
->dellink
)
1941 for_each_netdev_safe(net
, dev
, aux
) {
1942 if (dev
->group
== group
) {
1943 const struct rtnl_link_ops
*ops
;
1945 ops
= dev
->rtnl_link_ops
;
1946 ops
->dellink(dev
, &list_kill
);
1949 unregister_netdevice_many(&list_kill
);
1954 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1956 struct net
*net
= sock_net(skb
->sk
);
1957 const struct rtnl_link_ops
*ops
;
1958 struct net_device
*dev
;
1959 struct ifinfomsg
*ifm
;
1960 char ifname
[IFNAMSIZ
];
1961 struct nlattr
*tb
[IFLA_MAX
+1];
1963 LIST_HEAD(list_kill
);
1965 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1969 if (tb
[IFLA_IFNAME
])
1970 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1972 ifm
= nlmsg_data(nlh
);
1973 if (ifm
->ifi_index
> 0)
1974 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1975 else if (tb
[IFLA_IFNAME
])
1976 dev
= __dev_get_by_name(net
, ifname
);
1977 else if (tb
[IFLA_GROUP
])
1978 return rtnl_group_dellink(net
, nla_get_u32(tb
[IFLA_GROUP
]));
1985 ops
= dev
->rtnl_link_ops
;
1986 if (!ops
|| !ops
->dellink
)
1989 ops
->dellink(dev
, &list_kill
);
1990 unregister_netdevice_many(&list_kill
);
1994 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
1996 unsigned int old_flags
;
1999 old_flags
= dev
->flags
;
2000 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2001 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2006 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2008 __dev_notify_flags(dev
, old_flags
, ~0U);
2011 EXPORT_SYMBOL(rtnl_configure_link
);
2013 struct net_device
*rtnl_create_link(struct net
*net
,
2014 const char *ifname
, unsigned char name_assign_type
,
2015 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2018 struct net_device
*dev
;
2019 unsigned int num_tx_queues
= 1;
2020 unsigned int num_rx_queues
= 1;
2022 if (tb
[IFLA_NUM_TX_QUEUES
])
2023 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2024 else if (ops
->get_num_tx_queues
)
2025 num_tx_queues
= ops
->get_num_tx_queues();
2027 if (tb
[IFLA_NUM_RX_QUEUES
])
2028 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2029 else if (ops
->get_num_rx_queues
)
2030 num_rx_queues
= ops
->get_num_rx_queues();
2033 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2034 ops
->setup
, num_tx_queues
, num_rx_queues
);
2038 dev_net_set(dev
, net
);
2039 dev
->rtnl_link_ops
= ops
;
2040 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2043 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2044 if (tb
[IFLA_ADDRESS
]) {
2045 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2046 nla_len(tb
[IFLA_ADDRESS
]));
2047 dev
->addr_assign_type
= NET_ADDR_SET
;
2049 if (tb
[IFLA_BROADCAST
])
2050 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2051 nla_len(tb
[IFLA_BROADCAST
]));
2052 if (tb
[IFLA_TXQLEN
])
2053 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2054 if (tb
[IFLA_OPERSTATE
])
2055 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2056 if (tb
[IFLA_LINKMODE
])
2057 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2059 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2064 return ERR_PTR(err
);
2066 EXPORT_SYMBOL(rtnl_create_link
);
2068 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2069 struct net
*net
, int group
,
2070 struct ifinfomsg
*ifm
,
2073 struct net_device
*dev
, *aux
;
2076 for_each_netdev_safe(net
, dev
, aux
) {
2077 if (dev
->group
== group
) {
2078 err
= do_setlink(skb
, dev
, ifm
, tb
, NULL
, 0);
2087 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2089 struct net
*net
= sock_net(skb
->sk
);
2090 const struct rtnl_link_ops
*ops
;
2091 const struct rtnl_link_ops
*m_ops
= NULL
;
2092 struct net_device
*dev
;
2093 struct net_device
*master_dev
= NULL
;
2094 struct ifinfomsg
*ifm
;
2095 char kind
[MODULE_NAME_LEN
];
2096 char ifname
[IFNAMSIZ
];
2097 struct nlattr
*tb
[IFLA_MAX
+1];
2098 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2099 unsigned char name_assign_type
= NET_NAME_USER
;
2102 #ifdef CONFIG_MODULES
2105 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2109 if (tb
[IFLA_IFNAME
])
2110 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2114 ifm
= nlmsg_data(nlh
);
2115 if (ifm
->ifi_index
> 0)
2116 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2119 dev
= __dev_get_by_name(net
, ifname
);
2125 master_dev
= netdev_master_upper_dev_get(dev
);
2127 m_ops
= master_dev
->rtnl_link_ops
;
2130 err
= validate_linkmsg(dev
, tb
);
2134 if (tb
[IFLA_LINKINFO
]) {
2135 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2136 tb
[IFLA_LINKINFO
], ifla_info_policy
);
2140 memset(linkinfo
, 0, sizeof(linkinfo
));
2142 if (linkinfo
[IFLA_INFO_KIND
]) {
2143 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2144 ops
= rtnl_link_ops_get(kind
);
2151 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2152 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2153 struct nlattr
**data
= NULL
;
2154 struct nlattr
**slave_data
= NULL
;
2155 struct net
*dest_net
, *link_net
= NULL
;
2158 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2159 err
= nla_parse_nested(attr
, ops
->maxtype
,
2160 linkinfo
[IFLA_INFO_DATA
],
2166 if (ops
->validate
) {
2167 err
= ops
->validate(tb
, data
);
2174 if (m_ops
->slave_maxtype
&&
2175 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2176 err
= nla_parse_nested(slave_attr
,
2177 m_ops
->slave_maxtype
,
2178 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2179 m_ops
->slave_policy
);
2182 slave_data
= slave_attr
;
2184 if (m_ops
->slave_validate
) {
2185 err
= m_ops
->slave_validate(tb
, slave_data
);
2194 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2196 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2199 if (linkinfo
[IFLA_INFO_DATA
]) {
2200 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2204 err
= ops
->changelink(dev
, tb
, data
);
2207 status
|= DO_SETLINK_NOTIFY
;
2210 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2211 if (!m_ops
|| !m_ops
->slave_changelink
)
2214 err
= m_ops
->slave_changelink(master_dev
, dev
,
2218 status
|= DO_SETLINK_NOTIFY
;
2221 return do_setlink(skb
, dev
, ifm
, tb
, ifname
, status
);
2224 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2225 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2226 return rtnl_group_changelink(skb
, net
,
2227 nla_get_u32(tb
[IFLA_GROUP
]),
2232 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
2236 #ifdef CONFIG_MODULES
2239 request_module("rtnl-link-%s", kind
);
2241 ops
= rtnl_link_ops_get(kind
);
2253 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2254 name_assign_type
= NET_NAME_ENUM
;
2257 dest_net
= rtnl_link_get_net(net
, tb
);
2258 if (IS_ERR(dest_net
))
2259 return PTR_ERR(dest_net
);
2262 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2265 if (tb
[IFLA_LINK_NETNSID
]) {
2266 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2268 link_net
= get_net_ns_by_id(dest_net
, id
);
2274 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2278 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2279 name_assign_type
, ops
, tb
);
2285 dev
->ifindex
= ifm
->ifi_index
;
2288 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
);
2289 /* Drivers should call free_netdev() in ->destructor
2290 * and unregister it on failure after registration
2291 * so that device could be finally freed in rtnl_unlock.
2294 /* If device is not registered at all, free it now */
2295 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2300 err
= register_netdevice(dev
);
2306 err
= rtnl_configure_link(dev
, ifm
);
2308 goto out_unregister
;
2310 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2312 goto out_unregister
;
2321 LIST_HEAD(list_kill
);
2323 ops
->dellink(dev
, &list_kill
);
2324 unregister_netdevice_many(&list_kill
);
2326 unregister_netdevice(dev
);
2332 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
)
2334 struct net
*net
= sock_net(skb
->sk
);
2335 struct ifinfomsg
*ifm
;
2336 char ifname
[IFNAMSIZ
];
2337 struct nlattr
*tb
[IFLA_MAX
+1];
2338 struct net_device
*dev
= NULL
;
2339 struct sk_buff
*nskb
;
2341 u32 ext_filter_mask
= 0;
2343 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2347 if (tb
[IFLA_IFNAME
])
2348 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2350 if (tb
[IFLA_EXT_MASK
])
2351 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2353 ifm
= nlmsg_data(nlh
);
2354 if (ifm
->ifi_index
> 0)
2355 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2356 else if (tb
[IFLA_IFNAME
])
2357 dev
= __dev_get_by_name(net
, ifname
);
2364 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2368 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2369 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
2371 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2372 WARN_ON(err
== -EMSGSIZE
);
2375 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2380 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2382 struct net
*net
= sock_net(skb
->sk
);
2383 struct net_device
*dev
;
2384 struct nlattr
*tb
[IFLA_MAX
+1];
2385 u32 ext_filter_mask
= 0;
2386 u16 min_ifinfo_dump_size
= 0;
2389 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2390 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2391 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2393 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
2394 if (tb
[IFLA_EXT_MASK
])
2395 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2398 if (!ext_filter_mask
)
2399 return NLMSG_GOODSIZE
;
2401 * traverse the list of net devices and compute the minimum
2402 * buffer size based upon the filter mask.
2404 list_for_each_entry(dev
, &net
->dev_base_head
, dev_list
) {
2405 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2410 return min_ifinfo_dump_size
;
2413 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2416 int s_idx
= cb
->family
;
2420 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2421 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2422 if (idx
< s_idx
|| idx
== PF_PACKET
)
2424 if (rtnl_msg_handlers
[idx
] == NULL
||
2425 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
2428 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2432 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
2440 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
2441 unsigned int change
, gfp_t flags
)
2443 struct net
*net
= dev_net(dev
);
2444 struct sk_buff
*skb
;
2446 size_t if_info_size
;
2448 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2452 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
2454 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2455 WARN_ON(err
== -EMSGSIZE
);
2462 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2466 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
2468 struct net
*net
= dev_net(dev
);
2470 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
2473 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
2476 struct sk_buff
*skb
;
2478 if (dev
->reg_state
!= NETREG_REGISTERED
)
2481 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, flags
);
2483 rtmsg_ifinfo_send(skb
, dev
, flags
);
2485 EXPORT_SYMBOL(rtmsg_ifinfo
);
2487 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
2488 struct net_device
*dev
,
2489 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
2490 int type
, unsigned int flags
,
2493 struct nlmsghdr
*nlh
;
2496 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2500 ndm
= nlmsg_data(nlh
);
2501 ndm
->ndm_family
= AF_BRIDGE
;
2504 ndm
->ndm_flags
= flags
;
2506 ndm
->ndm_ifindex
= dev
->ifindex
;
2507 ndm
->ndm_state
= NUD_PERMANENT
;
2509 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
2510 goto nla_put_failure
;
2512 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
2513 goto nla_put_failure
;
2515 nlmsg_end(skb
, nlh
);
2519 nlmsg_cancel(skb
, nlh
);
2523 static inline size_t rtnl_fdb_nlmsg_size(void)
2525 return NLMSG_ALIGN(sizeof(struct ndmsg
)) + nla_total_size(ETH_ALEN
);
2528 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
)
2530 struct net
*net
= dev_net(dev
);
2531 struct sk_buff
*skb
;
2534 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2538 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
2539 0, 0, type
, NTF_SELF
, 0);
2545 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
2548 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
2552 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2554 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
2555 struct nlattr
*tb
[],
2556 struct net_device
*dev
,
2557 const unsigned char *addr
, u16 vid
,
2562 /* If aging addresses are supported device will need to
2563 * implement its own handler for this.
2565 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
2566 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2571 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
2575 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2576 err
= dev_uc_add_excl(dev
, addr
);
2577 else if (is_multicast_ether_addr(addr
))
2578 err
= dev_mc_add_excl(dev
, addr
);
2580 /* Only return duplicate errors if NLM_F_EXCL is set */
2581 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
2586 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
2588 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
2593 if (nla_len(vlan_attr
) != sizeof(u16
)) {
2594 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2598 vid
= nla_get_u16(vlan_attr
);
2600 if (!vid
|| vid
>= VLAN_VID_MASK
) {
2601 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2610 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2612 struct net
*net
= sock_net(skb
->sk
);
2614 struct nlattr
*tb
[NDA_MAX
+1];
2615 struct net_device
*dev
;
2620 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2624 ndm
= nlmsg_data(nlh
);
2625 if (ndm
->ndm_ifindex
== 0) {
2626 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2630 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2632 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2636 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2637 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2641 addr
= nla_data(tb
[NDA_LLADDR
]);
2643 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2649 /* Support fdb on master device the net/bridge default case */
2650 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2651 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2652 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2653 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2655 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2660 ndm
->ndm_flags
&= ~NTF_MASTER
;
2663 /* Embedded bridge, macvlan, and any other device support */
2664 if ((ndm
->ndm_flags
& NTF_SELF
)) {
2665 if (dev
->netdev_ops
->ndo_fdb_add
)
2666 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
2670 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2674 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
);
2675 ndm
->ndm_flags
&= ~NTF_SELF
;
2683 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2685 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
2686 struct nlattr
*tb
[],
2687 struct net_device
*dev
,
2688 const unsigned char *addr
, u16 vid
)
2692 /* If aging addresses are supported device will need to
2693 * implement its own handler for this.
2695 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
2696 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2700 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2701 err
= dev_uc_del(dev
, addr
);
2702 else if (is_multicast_ether_addr(addr
))
2703 err
= dev_mc_del(dev
, addr
);
2707 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
2709 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2711 struct net
*net
= sock_net(skb
->sk
);
2713 struct nlattr
*tb
[NDA_MAX
+1];
2714 struct net_device
*dev
;
2719 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
2722 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2726 ndm
= nlmsg_data(nlh
);
2727 if (ndm
->ndm_ifindex
== 0) {
2728 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2732 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2734 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2738 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2739 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2743 addr
= nla_data(tb
[NDA_LLADDR
]);
2745 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2751 /* Support fdb on master device the net/bridge default case */
2752 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2753 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2754 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2755 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2757 if (ops
->ndo_fdb_del
)
2758 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2763 ndm
->ndm_flags
&= ~NTF_MASTER
;
2766 /* Embedded bridge, macvlan, and any other device support */
2767 if (ndm
->ndm_flags
& NTF_SELF
) {
2768 if (dev
->netdev_ops
->ndo_fdb_del
)
2769 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
2772 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2775 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
);
2776 ndm
->ndm_flags
&= ~NTF_SELF
;
2783 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
2784 struct netlink_callback
*cb
,
2785 struct net_device
*dev
,
2787 struct netdev_hw_addr_list
*list
)
2789 struct netdev_hw_addr
*ha
;
2793 portid
= NETLINK_CB(cb
->skb
).portid
;
2794 seq
= cb
->nlh
->nlmsg_seq
;
2796 list_for_each_entry(ha
, &list
->list
, list
) {
2797 if (*idx
< cb
->args
[0])
2800 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
2802 RTM_NEWNEIGH
, NTF_SELF
,
2813 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2814 * @nlh: netlink message header
2817 * Default netdevice operation to dump the existing unicast address list.
2818 * Returns number of addresses from list put in skb.
2820 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
2821 struct netlink_callback
*cb
,
2822 struct net_device
*dev
,
2823 struct net_device
*filter_dev
,
2828 netif_addr_lock_bh(dev
);
2829 err
= nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->uc
);
2832 nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->mc
);
2834 netif_addr_unlock_bh(dev
);
2837 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
2839 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2841 struct net_device
*dev
;
2842 struct nlattr
*tb
[IFLA_MAX
+1];
2843 struct net_device
*br_dev
= NULL
;
2844 const struct net_device_ops
*ops
= NULL
;
2845 const struct net_device_ops
*cops
= NULL
;
2846 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
2847 struct net
*net
= sock_net(skb
->sk
);
2852 if (nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
, IFLA_MAX
,
2853 ifla_policy
) == 0) {
2854 if (tb
[IFLA_MASTER
])
2855 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
2858 brport_idx
= ifm
->ifi_index
;
2861 br_dev
= __dev_get_by_index(net
, br_idx
);
2865 ops
= br_dev
->netdev_ops
;
2868 for_each_netdev(net
, dev
) {
2869 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
2872 if (!br_idx
) { /* user did not specify a specific bridge */
2873 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2874 br_dev
= netdev_master_upper_dev_get(dev
);
2875 cops
= br_dev
->netdev_ops
;
2879 if (dev
!= br_dev
&&
2880 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
2883 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
2884 !(dev
->priv_flags
& IFF_EBRIDGE
))
2890 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2891 if (cops
&& cops
->ndo_fdb_dump
)
2892 idx
= cops
->ndo_fdb_dump(skb
, cb
, br_dev
, dev
,
2896 if (dev
->netdev_ops
->ndo_fdb_dump
)
2897 idx
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
, dev
, NULL
,
2900 idx
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
2909 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
2910 unsigned int attrnum
, unsigned int flag
)
2913 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
2917 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
2918 struct net_device
*dev
, u16 mode
,
2919 u32 flags
, u32 mask
, int nlflags
,
2921 int (*vlan_fill
)(struct sk_buff
*skb
,
2922 struct net_device
*dev
,
2925 struct nlmsghdr
*nlh
;
2926 struct ifinfomsg
*ifm
;
2927 struct nlattr
*br_afspec
;
2928 struct nlattr
*protinfo
;
2929 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
2930 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2933 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
2937 ifm
= nlmsg_data(nlh
);
2938 ifm
->ifi_family
= AF_BRIDGE
;
2940 ifm
->ifi_type
= dev
->type
;
2941 ifm
->ifi_index
= dev
->ifindex
;
2942 ifm
->ifi_flags
= dev_get_flags(dev
);
2943 ifm
->ifi_change
= 0;
2946 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
2947 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
2948 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
2950 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
2952 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
2953 (dev
->ifindex
!= dev_get_iflink(dev
) &&
2954 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
2955 goto nla_put_failure
;
2957 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
2959 goto nla_put_failure
;
2961 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
2962 nla_nest_cancel(skb
, br_afspec
);
2963 goto nla_put_failure
;
2966 if (mode
!= BRIDGE_MODE_UNDEF
) {
2967 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
2968 nla_nest_cancel(skb
, br_afspec
);
2969 goto nla_put_failure
;
2973 err
= vlan_fill(skb
, dev
, filter_mask
);
2975 nla_nest_cancel(skb
, br_afspec
);
2976 goto nla_put_failure
;
2979 nla_nest_end(skb
, br_afspec
);
2981 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
2983 goto nla_put_failure
;
2985 if (brport_nla_put_flag(skb
, flags
, mask
,
2986 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
2987 brport_nla_put_flag(skb
, flags
, mask
,
2988 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
2989 brport_nla_put_flag(skb
, flags
, mask
,
2990 IFLA_BRPORT_FAST_LEAVE
,
2991 BR_MULTICAST_FAST_LEAVE
) ||
2992 brport_nla_put_flag(skb
, flags
, mask
,
2993 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
2994 brport_nla_put_flag(skb
, flags
, mask
,
2995 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
2996 brport_nla_put_flag(skb
, flags
, mask
,
2997 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
2998 brport_nla_put_flag(skb
, flags
, mask
,
2999 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3000 brport_nla_put_flag(skb
, flags
, mask
,
3001 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3002 nla_nest_cancel(skb
, protinfo
);
3003 goto nla_put_failure
;
3006 nla_nest_end(skb
, protinfo
);
3008 nlmsg_end(skb
, nlh
);
3011 nlmsg_cancel(skb
, nlh
);
3012 return err
? err
: -EMSGSIZE
;
3014 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3016 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3018 struct net
*net
= sock_net(skb
->sk
);
3019 struct net_device
*dev
;
3021 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3022 u32 seq
= cb
->nlh
->nlmsg_seq
;
3023 u32 filter_mask
= 0;
3025 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3026 struct nlattr
*extfilt
;
3028 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3031 if (nla_len(extfilt
) < sizeof(filter_mask
))
3034 filter_mask
= nla_get_u32(extfilt
);
3039 for_each_netdev_rcu(net
, dev
) {
3040 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3041 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3043 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3044 if (idx
>= cb
->args
[0] &&
3045 br_dev
->netdev_ops
->ndo_bridge_getlink(
3046 skb
, portid
, seq
, dev
, filter_mask
,
3052 if (ops
->ndo_bridge_getlink
) {
3053 if (idx
>= cb
->args
[0] &&
3054 ops
->ndo_bridge_getlink(skb
, portid
, seq
, dev
,
3067 static inline size_t bridge_nlmsg_size(void)
3069 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3070 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3071 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3072 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3073 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3074 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3075 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3076 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3077 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3078 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3079 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3082 static int rtnl_bridge_notify(struct net_device
*dev
)
3084 struct net
*net
= dev_net(dev
);
3085 struct sk_buff
*skb
;
3086 int err
= -EOPNOTSUPP
;
3088 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3091 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3097 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3104 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3107 WARN_ON(err
== -EMSGSIZE
);
3110 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3114 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3116 struct net
*net
= sock_net(skb
->sk
);
3117 struct ifinfomsg
*ifm
;
3118 struct net_device
*dev
;
3119 struct nlattr
*br_spec
, *attr
= NULL
;
3120 int rem
, err
= -EOPNOTSUPP
;
3122 bool have_flags
= false;
3124 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3127 ifm
= nlmsg_data(nlh
);
3128 if (ifm
->ifi_family
!= AF_BRIDGE
)
3129 return -EPFNOSUPPORT
;
3131 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3133 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3137 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3139 nla_for_each_nested(attr
, br_spec
, rem
) {
3140 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3141 if (nla_len(attr
) < sizeof(flags
))
3145 flags
= nla_get_u16(attr
);
3151 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3152 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3154 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3159 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3163 flags
&= ~BRIDGE_FLAGS_MASTER
;
3166 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3167 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3170 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3173 flags
&= ~BRIDGE_FLAGS_SELF
;
3175 /* Generate event to notify upper layer of bridge
3178 err
= rtnl_bridge_notify(dev
);
3183 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3188 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3190 struct net
*net
= sock_net(skb
->sk
);
3191 struct ifinfomsg
*ifm
;
3192 struct net_device
*dev
;
3193 struct nlattr
*br_spec
, *attr
= NULL
;
3194 int rem
, err
= -EOPNOTSUPP
;
3196 bool have_flags
= false;
3198 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3201 ifm
= nlmsg_data(nlh
);
3202 if (ifm
->ifi_family
!= AF_BRIDGE
)
3203 return -EPFNOSUPPORT
;
3205 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3207 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3211 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3213 nla_for_each_nested(attr
, br_spec
, rem
) {
3214 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3215 if (nla_len(attr
) < sizeof(flags
))
3219 flags
= nla_get_u16(attr
);
3225 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3226 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3228 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3233 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3237 flags
&= ~BRIDGE_FLAGS_MASTER
;
3240 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3241 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3244 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3248 flags
&= ~BRIDGE_FLAGS_SELF
;
3250 /* Generate event to notify upper layer of bridge
3253 err
= rtnl_bridge_notify(dev
);
3258 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3263 /* Process one rtnetlink message. */
3265 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3267 struct net
*net
= sock_net(skb
->sk
);
3268 rtnl_doit_func doit
;
3274 type
= nlh
->nlmsg_type
;
3280 /* All the messages must have at least 1 byte length */
3281 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
3284 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
3288 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
3291 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
3293 rtnl_dumpit_func dumpit
;
3294 rtnl_calcit_func calcit
;
3295 u16 min_dump_alloc
= 0;
3297 dumpit
= rtnl_get_dumpit(family
, type
);
3300 calcit
= rtnl_get_calcit(family
, type
);
3302 min_dump_alloc
= calcit(skb
, nlh
);
3307 struct netlink_dump_control c
= {
3309 .min_dump_alloc
= min_dump_alloc
,
3311 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
3317 doit
= rtnl_get_doit(family
, type
);
3321 return doit(skb
, nlh
);
3324 static void rtnetlink_rcv(struct sk_buff
*skb
)
3327 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
3331 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3333 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3339 case NETDEV_POST_INIT
:
3340 case NETDEV_REGISTER
:
3342 case NETDEV_PRE_TYPE_CHANGE
:
3343 case NETDEV_GOING_DOWN
:
3344 case NETDEV_UNREGISTER
:
3345 case NETDEV_UNREGISTER_FINAL
:
3346 case NETDEV_RELEASE
:
3348 case NETDEV_BONDING_INFO
:
3351 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
3357 static struct notifier_block rtnetlink_dev_notifier
= {
3358 .notifier_call
= rtnetlink_event
,
3362 static int __net_init
rtnetlink_net_init(struct net
*net
)
3365 struct netlink_kernel_cfg cfg
= {
3366 .groups
= RTNLGRP_MAX
,
3367 .input
= rtnetlink_rcv
,
3368 .cb_mutex
= &rtnl_mutex
,
3369 .flags
= NL_CFG_F_NONROOT_RECV
,
3372 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
3379 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
3381 netlink_kernel_release(net
->rtnl
);
3385 static struct pernet_operations rtnetlink_net_ops
= {
3386 .init
= rtnetlink_net_init
,
3387 .exit
= rtnetlink_net_exit
,
3390 void __init
rtnetlink_init(void)
3392 if (register_pernet_subsys(&rtnetlink_net_ops
))
3393 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3395 register_netdevice_notifier(&rtnetlink_dev_notifier
);
3397 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
3398 rtnl_dump_ifinfo
, rtnl_calcit
);
3399 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, NULL
);
3400 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, NULL
);
3401 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, NULL
);
3403 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, NULL
);
3404 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, NULL
);
3406 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, NULL
);
3407 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, NULL
);
3408 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, NULL
);
3410 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, NULL
);
3411 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, NULL
);
3412 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, NULL
);