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 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
);
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
,
503 struct rtnl_af_ops
*af_ops
;
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
);
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
)
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
;
538 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
541 ops
= master_dev
->rtnl_link_ops
;
544 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
546 if (ops
->fill_slave_info
) {
547 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
550 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
552 goto err_cancel_slave_data
;
553 nla_nest_end(skb
, slave_data
);
557 err_cancel_slave_data
:
558 nla_nest_cancel(skb
, slave_data
);
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
;
571 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
573 if (ops
->fill_xstats
) {
574 err
= ops
->fill_xstats(skb
, dev
);
578 if (ops
->fill_info
) {
579 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
582 err
= ops
->fill_info(skb
, dev
);
584 goto err_cancel_data
;
585 nla_nest_end(skb
, data
);
590 nla_nest_cancel(skb
, data
);
594 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
596 struct nlattr
*linkinfo
;
599 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
600 if (linkinfo
== NULL
)
603 err
= rtnl_link_info_fill(skb
, dev
);
605 goto err_cancel_link
;
607 err
= rtnl_link_slave_info_fill(skb
, dev
);
609 goto err_cancel_link
;
611 nla_nest_end(skb
, linkinfo
);
615 nla_nest_cancel(skb
, linkinfo
);
620 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
622 struct sock
*rtnl
= net
->rtnl
;
625 NETLINK_CB(skb
).dst_group
= group
;
627 atomic_inc(&skb
->users
);
628 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
630 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
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
;
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
)
668 mx
= nla_nest_start(skb
, RTA_METRICS
);
672 for (i
= 0; i
< RTAX_MAX
; 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
);
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
;
689 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
690 goto nla_put_failure
;
697 nla_nest_cancel(skb
, mx
);
701 return nla_nest_end(skb
, mx
);
704 nla_nest_cancel(skb
, mx
);
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
)),
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
) {
737 if ((operstate
== IF_OPER_DORMANT
||
738 operstate
== IF_OPER_UNKNOWN
) &&
740 operstate
= IF_OPER_UP
;
743 case IF_OPER_DORMANT
:
744 if (operstate
== IF_OPER_UP
||
745 operstate
== IF_OPER_UNKNOWN
)
746 operstate
= IF_OPER_DORMANT
;
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 */
771 flags
= (flags
& ifm
->ifi_change
) |
772 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
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
));
815 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
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
));
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
)));
848 static size_t rtnl_port_size(const struct net_device
*dev
,
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
))
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
))
862 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
865 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
866 !(ext_filter_mask
& RTEXT_FILTER_VF
))
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
);
872 return port_self_size
;
875 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
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
;
921 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
925 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
926 vf_port
= nla_nest_start(skb
, IFLA_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
;
935 nla_nest_cancel(skb
, vf_port
);
938 nla_nest_end(skb
, vf_port
);
941 nla_nest_end(skb
, vf_ports
);
946 nla_nest_cancel(skb
, vf_ports
);
950 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
952 struct nlattr
*port_self
;
955 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
959 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
961 nla_nest_cancel(skb
, port_self
);
962 return (err
== -EMSGSIZE
) ? err
: 0;
965 nla_nest_end(skb
, port_self
);
970 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
975 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
976 !(ext_filter_mask
& RTEXT_FILTER_VF
))
979 err
= rtnl_port_self_fill(skb
, dev
);
983 if (dev_num_vf(dev
->dev
.parent
)) {
984 err
= rtnl_vf_ports_fill(skb
, dev
);
992 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
995 struct netdev_phys_item_id ppid
;
997 err
= dev_get_phys_port_id(dev
, &ppid
);
999 if (err
== -EOPNOTSUPP
)
1004 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1010 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1012 char name
[IFNAMSIZ
];
1015 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1017 if (err
== -EOPNOTSUPP
)
1022 if (nla_put_string(skb
, IFLA_PHYS_PORT_NAME
, name
))
1028 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
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
);
1038 if (err
== -EOPNOTSUPP
)
1043 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
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
));
1064 copy_rtnl_link_stats(nla_data(attr
), stats
);
1066 attr
= nla_reserve(skb
, IFLA_STATS64
,
1067 sizeof(struct rtnl_link_stats64
));
1071 copy_rtnl_link_stats64(nla_data(attr
), stats
);
1076 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1077 struct net_device
*dev
,
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.
1101 ivi
.rss_query_en
= -1;
1103 /* The default value for VF link state is "auto"
1104 * IFLA_VF_LINK_STATE_AUTO which equals zero
1107 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
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
);
1131 nla_nest_cancel(skb
, vfinfo
);
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
),
1138 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1140 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1142 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(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
))
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
,
1154 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1156 nla_nest_cancel(skb
, vf
);
1157 nla_nest_cancel(skb
, vfinfo
);
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
))
1173 nla_nest_end(skb
, vfstats
);
1174 nla_nest_end(skb
, vf
);
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
;
1188 map
.port
= dev
->if_port
;
1190 if (nla_put(skb
, IFLA_MAP
, sizeof(map
), &map
))
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
);
1207 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1211 ifm
= nlmsg_data(nlh
);
1212 ifm
->ifi_family
= AF_UNSPEC
;
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
) ||
1229 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1231 (dev
->ifindex
!= dev_get_iflink(dev
) &&
1232 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
1234 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1235 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1237 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
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
) {
1273 struct nlattr
*vfinfo
;
1274 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1276 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
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
) {
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
1326 if (err
== -ENODATA
)
1327 nla_nest_cancel(skb
, af
);
1329 goto nla_put_failure
;
1331 nla_nest_end(skb
, af
);
1335 nla_nest_end(skb
, af_spec
);
1337 nlmsg_end(skb
, nlh
);
1341 nlmsg_cancel(skb
, nlh
);
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
);
1425 struct net_device
*dev
;
1426 struct hlist_head
*head
;
1427 struct nlattr
*tb
[IFLA_MAX
+1];
1428 u32 ext_filter_mask
= 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) {
1455 head
= &net
->dev_index_head
[h
];
1456 hlist_for_each_entry(dev
, head
, index_hlist
) {
1459 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1460 NETLINK_CB(cb
->skb
).portid
,
1461 cb
->nlh
->nlmsg_seq
, 0,
1466 if (likely(skb
->len
))
1472 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
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
[])
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
]));
1503 net
= get_net(src_net
);
1506 EXPORT_SYMBOL(rtnl_link_get_net
);
1508 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1511 if (tb
[IFLA_ADDRESS
] &&
1512 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1515 if (tb
[IFLA_BROADCAST
] &&
1516 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1520 if (tb
[IFLA_AF_SPEC
]) {
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
)
1533 if (af_ops
->validate_link_af
) {
1534 err
= af_ops
->validate_link_af(dev
, af
);
1544 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1546 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1549 if (tb
[IFLA_VF_MAC
]) {
1550 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1553 if (ops
->ndo_set_vf_mac
)
1554 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1560 if (tb
[IFLA_VF_VLAN
]) {
1561 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1564 if (ops
->ndo_set_vf_vlan
)
1565 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
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
;
1576 if (ops
->ndo_get_vf_config
)
1577 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1582 if (ops
->ndo_set_vf_rate
)
1583 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1590 if (tb
[IFLA_VF_RATE
]) {
1591 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1594 if (ops
->ndo_set_vf_rate
)
1595 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1602 if (tb
[IFLA_VF_SPOOFCHK
]) {
1603 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1606 if (ops
->ndo_set_vf_spoofchk
)
1607 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1613 if (tb
[IFLA_VF_LINK_STATE
]) {
1614 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1617 if (ops
->ndo_set_vf_link_state
)
1618 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1624 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1625 struct ifla_vf_rss_query_en
*ivrssq_en
;
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
);
1636 if (tb
[IFLA_VF_TRUST
]) {
1637 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
1640 if (ops
->ndo_set_vf_trust
)
1641 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
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
;
1656 if (upper_dev
->ifindex
== ifindex
)
1658 ops
= upper_dev
->netdev_ops
;
1659 if (ops
->ndo_del_slave
) {
1660 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1669 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1672 ops
= upper_dev
->netdev_ops
;
1673 if (ops
->ndo_add_slave
) {
1674 err
= ops
->ndo_add_slave(upper_dev
, dev
);
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
;
1694 err
= validate_linkmsg(dev
, tb
);
1698 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1699 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1704 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1709 err
= dev_change_net_namespace(dev
, net
, ifname
);
1713 status
|= DO_SETLINK_MODIFIED
;
1717 struct rtnl_link_ifmap
*u_map
;
1720 if (!ops
->ndo_set_config
) {
1725 if (!netif_device_present(dev
)) {
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
);
1742 status
|= DO_SETLINK_NOTIFY
;
1745 if (tb
[IFLA_ADDRESS
]) {
1746 struct sockaddr
*sa
;
1749 len
= sizeof(sa_family_t
) + max_t(size_t, dev
->addr_len
,
1751 sa
= kmalloc(len
, GFP_KERNEL
);
1756 sa
->sa_family
= dev
->type
;
1757 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1759 err
= dev_set_mac_address(dev
, sa
);
1763 status
|= DO_SETLINK_MODIFIED
;
1767 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
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
1783 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1784 err
= dev_change_name(dev
, ifname
);
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
]));
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
));
1809 if (tb
[IFLA_MASTER
]) {
1810 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1813 status
|= DO_SETLINK_MODIFIED
;
1816 if (tb
[IFLA_CARRIER
]) {
1817 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
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
;
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
) {
1856 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
1860 err
= do_setvfinfo(dev
, vfinfo
);
1863 status
|= DO_SETLINK_NOTIFY
;
1868 if (tb
[IFLA_VF_PORTS
]) {
1869 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1870 struct nlattr
*attr
;
1875 if (!ops
->ndo_set_vf_port
)
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
) {
1884 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
1888 if (!port
[IFLA_PORT_VF
]) {
1892 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
1893 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
1896 status
|= DO_SETLINK_NOTIFY
;
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
);
1910 if (ops
->ndo_set_vf_port
)
1911 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
1914 status
|= DO_SETLINK_NOTIFY
;
1917 if (tb
[IFLA_AF_SPEC
]) {
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
))))
1927 err
= af_ops
->set_link_af(dev
, af
);
1931 status
|= DO_SETLINK_NOTIFY
;
1936 if (tb
[IFLA_PROTO_DOWN
]) {
1937 err
= dev_change_proto_down(dev
,
1938 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
1941 status
|= DO_SETLINK_NOTIFY
;
1945 if (status
& DO_SETLINK_MODIFIED
) {
1946 if (status
& DO_SETLINK_NOTIFY
)
1947 netdev_state_change(dev
);
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",
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
;
1963 struct nlattr
*tb
[IFLA_MAX
+1];
1964 char ifname
[IFNAMSIZ
];
1966 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1970 if (tb
[IFLA_IFNAME
])
1971 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
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
);
1989 err
= do_setlink(skb
, dev
, ifm
, tb
, ifname
, 0);
1994 static int rtnl_group_dellink(const struct net
*net
, int group
)
1996 struct net_device
*dev
, *aux
;
1997 LIST_HEAD(list_kill
);
2003 for_each_netdev(net
, dev
) {
2004 if (dev
->group
== group
) {
2005 const struct rtnl_link_ops
*ops
;
2008 ops
= dev
->rtnl_link_ops
;
2009 if (!ops
|| !ops
->dellink
)
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
);
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
)
2039 ops
->dellink(dev
, &list_kill
);
2040 unregister_netdevice_many(&list_kill
);
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];
2055 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
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
]));
2075 return rtnl_delete_link(dev
);
2078 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2080 unsigned int old_flags
;
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
));
2090 if (dev
->rtnl_link_state
== RTNL_LINK_INITIALIZED
) {
2091 __dev_notify_flags(dev
, old_flags
, 0U);
2093 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2094 __dev_notify_flags(dev
, old_flags
, ~0U);
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
[])
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
);
2126 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2127 ops
->setup
, num_tx_queues
, num_rx_queues
);
2131 dev_net_set(dev
, net
);
2132 dev
->rtnl_link_ops
= ops
;
2133 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
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
]);
2152 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
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
,
2166 struct net_device
*dev
, *aux
;
2169 for_each_netdev_safe(net
, dev
, aux
) {
2170 if (dev
->group
== group
) {
2171 err
= do_setlink(skb
, dev
, ifm
, tb
, NULL
, 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
;
2195 #ifdef CONFIG_MODULES
2198 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2202 if (tb
[IFLA_IFNAME
])
2203 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2207 ifm
= nlmsg_data(nlh
);
2208 if (ifm
->ifi_index
> 0)
2209 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2212 dev
= __dev_get_by_name(net
, ifname
);
2218 master_dev
= netdev_master_upper_dev_get(dev
);
2220 m_ops
= master_dev
->rtnl_link_ops
;
2223 err
= validate_linkmsg(dev
, tb
);
2227 if (tb
[IFLA_LINKINFO
]) {
2228 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2229 tb
[IFLA_LINKINFO
], ifla_info_policy
);
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
);
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
;
2251 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2252 err
= nla_parse_nested(attr
, ops
->maxtype
,
2253 linkinfo
[IFLA_INFO_DATA
],
2259 if (ops
->validate
) {
2260 err
= ops
->validate(tb
, data
);
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
);
2275 slave_data
= slave_attr
;
2277 if (m_ops
->slave_validate
) {
2278 err
= m_ops
->slave_validate(tb
, slave_data
);
2287 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2289 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2292 if (linkinfo
[IFLA_INFO_DATA
]) {
2293 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2297 err
= ops
->changelink(dev
, tb
, data
);
2300 status
|= DO_SETLINK_NOTIFY
;
2303 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2304 if (!m_ops
|| !m_ops
->slave_changelink
)
2307 err
= m_ops
->slave_changelink(master_dev
, dev
,
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
]),
2325 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
2329 #ifdef CONFIG_MODULES
2332 request_module("rtnl-link-%s", kind
);
2334 ops
= rtnl_link_ops_get(kind
);
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
);
2355 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
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
);
2367 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2371 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2372 name_assign_type
, ops
, tb
);
2378 dev
->ifindex
= ifm
->ifi_index
;
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.
2387 /* If device is not registered at all, free it now */
2388 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2393 err
= register_netdevice(dev
);
2399 err
= rtnl_configure_link(dev
, ifm
);
2401 goto out_unregister
;
2403 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2405 goto out_unregister
;
2414 LIST_HEAD(list_kill
);
2416 ops
->dellink(dev
, &list_kill
);
2417 unregister_netdevice_many(&list_kill
);
2419 unregister_netdevice(dev
);
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
;
2434 u32 ext_filter_mask
= 0;
2436 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
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
);
2457 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2461 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2462 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
2464 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2465 WARN_ON(err
== -EMSGSIZE
);
2468 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
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;
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
,
2503 return min_ifinfo_dump_size
;
2506 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2509 int s_idx
= cb
->family
;
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
)
2517 if (rtnl_msg_handlers
[idx
] == NULL
||
2518 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
2521 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2525 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
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
;
2539 size_t if_info_size
;
2541 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2545 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
2547 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2548 WARN_ON(err
== -EMSGSIZE
);
2555 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
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
,
2569 struct sk_buff
*skb
;
2571 if (dev
->reg_state
!= NETREG_REGISTERED
)
2574 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, flags
);
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
,
2586 struct nlmsghdr
*nlh
;
2589 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2593 ndm
= nlmsg_data(nlh
);
2594 ndm
->ndm_family
= AF_BRIDGE
;
2597 ndm
->ndm_flags
= flags
;
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
;
2605 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
2606 goto nla_put_failure
;
2608 nlmsg_end(skb
, nlh
);
2612 nlmsg_cancel(skb
, nlh
);
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
;
2630 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2634 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
2635 0, 0, type
, NTF_SELF
, 0);
2641 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
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
,
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
);
2667 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
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
))
2682 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
2684 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
2689 if (nla_len(vlan_attr
) != sizeof(u16
)) {
2690 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
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",
2706 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2708 struct net
*net
= sock_net(skb
->sk
);
2710 struct nlattr
*tb
[NDA_MAX
+1];
2711 struct net_device
*dev
;
2716 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2720 ndm
= nlmsg_data(nlh
);
2721 if (ndm
->ndm_ifindex
== 0) {
2722 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2726 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2728 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2732 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2733 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2737 if (dev
->type
!= ARPHRD_ETHER
) {
2738 pr_info("PF_BRIDGE: FDB add only supported for Ethernet devices");
2742 addr
= nla_data(tb
[NDA_LLADDR
]);
2744 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
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
,
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
,
2771 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2775 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
);
2776 ndm
->ndm_flags
&= ~NTF_SELF
;
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
)
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
);
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
);
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
);
2814 struct nlattr
*tb
[NDA_MAX
+1];
2815 struct net_device
*dev
;
2820 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
2823 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2827 ndm
= nlmsg_data(nlh
);
2828 if (ndm
->ndm_ifindex
== 0) {
2829 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2833 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2835 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2839 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2840 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2844 if (dev
->type
!= ARPHRD_ETHER
) {
2845 pr_info("PF_BRIDGE: FDB delete only supported for Ethernet devices");
2849 addr
= nla_data(tb
[NDA_LLADDR
]);
2851 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
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
);
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
,
2878 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2881 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
);
2882 ndm
->ndm_flags
&= ~NTF_SELF
;
2889 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
2890 struct netlink_callback
*cb
,
2891 struct net_device
*dev
,
2893 struct netdev_hw_addr_list
*list
)
2895 struct netdev_hw_addr
*ha
;
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])
2906 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
2908 RTM_NEWNEIGH
, NTF_SELF
,
2919 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2920 * @nlh: netlink message header
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
,
2934 if (dev
->type
!= ARPHRD_ETHER
)
2937 netif_addr_lock_bh(dev
);
2938 err
= nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->uc
);
2941 nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->mc
);
2943 netif_addr_unlock_bh(dev
);
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
);
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
;
2970 br_dev
= __dev_get_by_index(net
, br_idx
);
2974 ops
= br_dev
->netdev_ops
;
2977 for_each_netdev(net
, dev
) {
2978 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
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
;
2988 if (dev
!= br_dev
&&
2989 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
2992 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
2993 !(dev
->priv_flags
& IFF_EBRIDGE
))
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
,
3005 if (dev
->netdev_ops
->ndo_fdb_dump
)
3006 idx
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
, dev
, NULL
,
3009 idx
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
3018 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3019 unsigned int attrnum
, unsigned int flag
)
3022 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
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
,
3030 int (*vlan_fill
)(struct sk_buff
*skb
,
3031 struct net_device
*dev
,
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
);
3042 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3046 ifm
= nlmsg_data(nlh
);
3047 ifm
->ifi_family
= AF_BRIDGE
;
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
) ||
3059 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
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
);
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
;
3082 err
= vlan_fill(skb
, dev
, filter_mask
);
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
);
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
);
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
;
3130 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3131 u32 seq
= cb
->nlh
->nlmsg_seq
;
3132 u32 filter_mask
= 0;
3135 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3136 struct nlattr
*extfilt
;
3138 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3141 if (nla_len(extfilt
) < sizeof(filter_mask
))
3144 filter_mask
= nla_get_u32(extfilt
);
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
))
3168 if (ops
->ndo_bridge_getlink
) {
3169 if (idx
>= cb
->args
[0]) {
3170 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3174 if (err
< 0 && err
!= -EOPNOTSUPP
) {
3175 if (likely(skb
->len
))
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
)
3216 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3222 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3229 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3232 WARN_ON(err
== -EMSGSIZE
);
3235 rtnl_set_sk_err(net
, RTNLGRP_LINK
, 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
;
3247 bool have_flags
= false;
3249 if (nlmsg_len(nlh
) < sizeof(*ifm
))
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
);
3258 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3262 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_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
))
3270 flags
= nla_get_u16(attr
);
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
) {
3284 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3288 flags
&= ~BRIDGE_FLAGS_MASTER
;
3291 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3292 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3295 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3298 flags
&= ~BRIDGE_FLAGS_SELF
;
3300 /* Generate event to notify upper layer of bridge
3303 err
= rtnl_bridge_notify(dev
);
3308 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
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
;
3321 bool have_flags
= false;
3323 if (nlmsg_len(nlh
) < sizeof(*ifm
))
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
);
3332 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3336 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_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
))
3344 flags
= nla_get_u16(attr
);
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
) {
3358 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3362 flags
&= ~BRIDGE_FLAGS_MASTER
;
3365 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3366 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3369 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3373 flags
&= ~BRIDGE_FLAGS_SELF
;
3375 /* Generate event to notify upper layer of bridge
3378 err
= rtnl_bridge_notify(dev
);
3383 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
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
;
3399 type
= nlh
->nlmsg_type
;
3405 /* All the messages must have at least 1 byte length */
3406 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
3409 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
3413 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
3416 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
3418 rtnl_dumpit_func dumpit
;
3419 rtnl_calcit_func calcit
;
3420 u16 min_dump_alloc
= 0;
3422 dumpit
= rtnl_get_dumpit(family
, type
);
3425 calcit
= rtnl_get_calcit(family
, type
);
3427 min_dump_alloc
= calcit(skb
, nlh
);
3432 struct netlink_dump_control c
= {
3434 .min_dump_alloc
= min_dump_alloc
,
3436 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
3442 doit
= rtnl_get_doit(family
, type
);
3446 return doit(skb
, nlh
);
3449 static void rtnetlink_rcv(struct sk_buff
*skb
)
3452 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
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
);
3464 case NETDEV_POST_INIT
:
3465 case NETDEV_REGISTER
:
3467 case NETDEV_PRE_TYPE_CHANGE
:
3468 case NETDEV_GOING_DOWN
:
3469 case NETDEV_UNREGISTER
:
3470 case NETDEV_UNREGISTER_FINAL
:
3471 case NETDEV_RELEASE
:
3473 case NETDEV_BONDING_INFO
:
3476 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
3482 static struct notifier_block rtnetlink_dev_notifier
= {
3483 .notifier_call
= rtnetlink_event
,
3487 static int __net_init
rtnetlink_net_init(struct net
*net
)
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
);
3504 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
3506 netlink_kernel_release(net
->rtnl
);
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
);