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(ext_filter_mask
901 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
902 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
903 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
904 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
905 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
906 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
907 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
908 + nla_total_size(1); /* IFLA_PROTO_DOWN */
912 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
914 struct nlattr
*vf_ports
;
915 struct nlattr
*vf_port
;
919 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
923 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
924 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
926 goto nla_put_failure
;
927 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
928 goto nla_put_failure
;
929 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
930 if (err
== -EMSGSIZE
)
931 goto nla_put_failure
;
933 nla_nest_cancel(skb
, vf_port
);
936 nla_nest_end(skb
, vf_port
);
939 nla_nest_end(skb
, vf_ports
);
944 nla_nest_cancel(skb
, vf_ports
);
948 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
950 struct nlattr
*port_self
;
953 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
957 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
959 nla_nest_cancel(skb
, port_self
);
960 return (err
== -EMSGSIZE
) ? err
: 0;
963 nla_nest_end(skb
, port_self
);
968 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
973 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
974 !(ext_filter_mask
& RTEXT_FILTER_VF
))
977 err
= rtnl_port_self_fill(skb
, dev
);
981 if (dev_num_vf(dev
->dev
.parent
)) {
982 err
= rtnl_vf_ports_fill(skb
, dev
);
990 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
993 struct netdev_phys_item_id ppid
;
995 err
= dev_get_phys_port_id(dev
, &ppid
);
997 if (err
== -EOPNOTSUPP
)
1002 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1008 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1010 char name
[IFNAMSIZ
];
1013 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1015 if (err
== -EOPNOTSUPP
)
1020 if (nla_put(skb
, IFLA_PHYS_PORT_NAME
, strlen(name
), name
))
1026 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1029 struct switchdev_attr attr
= {
1030 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1031 .flags
= SWITCHDEV_F_NO_RECURSE
,
1034 err
= switchdev_port_attr_get(dev
, &attr
);
1036 if (err
== -EOPNOTSUPP
)
1041 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1048 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1049 struct net_device
*dev
)
1051 const struct rtnl_link_stats64
*stats
;
1052 struct rtnl_link_stats64 temp
;
1053 struct nlattr
*attr
;
1055 stats
= dev_get_stats(dev
, &temp
);
1057 attr
= nla_reserve(skb
, IFLA_STATS
,
1058 sizeof(struct rtnl_link_stats
));
1062 copy_rtnl_link_stats(nla_data(attr
), stats
);
1064 attr
= nla_reserve(skb
, IFLA_STATS64
,
1065 sizeof(struct rtnl_link_stats64
));
1069 copy_rtnl_link_stats64(nla_data(attr
), stats
);
1074 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1075 struct net_device
*dev
,
1077 struct nlattr
*vfinfo
)
1079 struct ifla_vf_rss_query_en vf_rss_query_en
;
1080 struct ifla_vf_link_state vf_linkstate
;
1081 struct ifla_vf_spoofchk vf_spoofchk
;
1082 struct ifla_vf_tx_rate vf_tx_rate
;
1083 struct ifla_vf_stats vf_stats
;
1084 struct ifla_vf_trust vf_trust
;
1085 struct ifla_vf_vlan vf_vlan
;
1086 struct ifla_vf_rate vf_rate
;
1087 struct nlattr
*vf
, *vfstats
;
1088 struct ifla_vf_mac vf_mac
;
1089 struct ifla_vf_info ivi
;
1091 /* Not all SR-IOV capable drivers support the
1092 * spoofcheck and "RSS query enable" query. Preset to
1093 * -1 so the user space tool can detect that the driver
1094 * didn't report anything.
1097 ivi
.rss_query_en
= -1;
1099 memset(ivi
.mac
, 0, sizeof(ivi
.mac
));
1100 /* The default value for VF link state is "auto"
1101 * IFLA_VF_LINK_STATE_AUTO which equals zero
1104 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1113 vf_rss_query_en
.vf
=
1114 vf_trust
.vf
= ivi
.vf
;
1116 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1117 vf_vlan
.vlan
= ivi
.vlan
;
1118 vf_vlan
.qos
= ivi
.qos
;
1119 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1120 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1121 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1122 vf_spoofchk
.setting
= ivi
.spoofchk
;
1123 vf_linkstate
.link_state
= ivi
.linkstate
;
1124 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1125 vf_trust
.setting
= ivi
.trusted
;
1126 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1128 nla_nest_cancel(skb
, vfinfo
);
1131 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1132 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1133 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1135 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1137 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1139 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1141 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1142 sizeof(vf_rss_query_en
),
1143 &vf_rss_query_en
) ||
1144 nla_put(skb
, IFLA_VF_TRUST
,
1145 sizeof(vf_trust
), &vf_trust
))
1147 memset(&vf_stats
, 0, sizeof(vf_stats
));
1148 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1149 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1151 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1153 nla_nest_cancel(skb
, vf
);
1154 nla_nest_cancel(skb
, vfinfo
);
1157 if (nla_put_u64(skb
, IFLA_VF_STATS_RX_PACKETS
,
1158 vf_stats
.rx_packets
) ||
1159 nla_put_u64(skb
, IFLA_VF_STATS_TX_PACKETS
,
1160 vf_stats
.tx_packets
) ||
1161 nla_put_u64(skb
, IFLA_VF_STATS_RX_BYTES
,
1162 vf_stats
.rx_bytes
) ||
1163 nla_put_u64(skb
, IFLA_VF_STATS_TX_BYTES
,
1164 vf_stats
.tx_bytes
) ||
1165 nla_put_u64(skb
, IFLA_VF_STATS_BROADCAST
,
1166 vf_stats
.broadcast
) ||
1167 nla_put_u64(skb
, IFLA_VF_STATS_MULTICAST
,
1168 vf_stats
.multicast
))
1170 nla_nest_end(skb
, vfstats
);
1171 nla_nest_end(skb
, vf
);
1175 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1177 struct rtnl_link_ifmap map
= {
1178 .mem_start
= dev
->mem_start
,
1179 .mem_end
= dev
->mem_end
,
1180 .base_addr
= dev
->base_addr
,
1183 .port
= dev
->if_port
,
1185 if (nla_put(skb
, IFLA_MAP
, sizeof(map
), &map
))
1191 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
1192 int type
, u32 pid
, u32 seq
, u32 change
,
1193 unsigned int flags
, u32 ext_filter_mask
)
1195 struct ifinfomsg
*ifm
;
1196 struct nlmsghdr
*nlh
;
1197 struct nlattr
*af_spec
;
1198 struct rtnl_af_ops
*af_ops
;
1199 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1202 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1206 ifm
= nlmsg_data(nlh
);
1207 ifm
->ifi_family
= AF_UNSPEC
;
1209 ifm
->ifi_type
= dev
->type
;
1210 ifm
->ifi_index
= dev
->ifindex
;
1211 ifm
->ifi_flags
= dev_get_flags(dev
);
1212 ifm
->ifi_change
= change
;
1214 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1215 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1216 nla_put_u8(skb
, IFLA_OPERSTATE
,
1217 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1218 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1219 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1220 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1221 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1222 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1224 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1226 (dev
->ifindex
!= dev_get_iflink(dev
) &&
1227 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
1229 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1230 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1232 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1234 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)) ||
1235 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1236 atomic_read(&dev
->carrier_changes
)) ||
1237 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
))
1238 goto nla_put_failure
;
1240 if (rtnl_fill_link_ifmap(skb
, dev
))
1241 goto nla_put_failure
;
1243 if (dev
->addr_len
) {
1244 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1245 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1246 goto nla_put_failure
;
1249 if (rtnl_phys_port_id_fill(skb
, dev
))
1250 goto nla_put_failure
;
1252 if (rtnl_phys_port_name_fill(skb
, dev
))
1253 goto nla_put_failure
;
1255 if (rtnl_phys_switch_id_fill(skb
, dev
))
1256 goto nla_put_failure
;
1258 if (rtnl_fill_stats(skb
, dev
))
1259 goto nla_put_failure
;
1261 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
1262 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
1263 goto nla_put_failure
;
1265 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
&&
1266 ext_filter_mask
& RTEXT_FILTER_VF
) {
1268 struct nlattr
*vfinfo
;
1269 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1271 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1273 goto nla_put_failure
;
1274 for (i
= 0; i
< num_vfs
; i
++) {
1275 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1276 goto nla_put_failure
;
1279 nla_nest_end(skb
, vfinfo
);
1282 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1283 goto nla_put_failure
;
1285 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1286 if (rtnl_link_fill(skb
, dev
) < 0)
1287 goto nla_put_failure
;
1290 if (dev
->rtnl_link_ops
&&
1291 dev
->rtnl_link_ops
->get_link_net
) {
1292 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1294 if (!net_eq(dev_net(dev
), link_net
)) {
1295 int id
= peernet2id_alloc(dev_net(dev
), link_net
);
1297 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1298 goto nla_put_failure
;
1302 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1303 goto nla_put_failure
;
1305 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1306 if (af_ops
->fill_link_af
) {
1310 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1311 goto nla_put_failure
;
1313 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1316 * Caller may return ENODATA to indicate that there
1317 * was no data to be dumped. This is not an error, it
1318 * means we should trim the attribute header and
1321 if (err
== -ENODATA
)
1322 nla_nest_cancel(skb
, af
);
1324 goto nla_put_failure
;
1326 nla_nest_end(skb
, af
);
1330 nla_nest_end(skb
, af_spec
);
1332 nlmsg_end(skb
, nlh
);
1336 nlmsg_cancel(skb
, nlh
);
1340 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1341 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1342 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1343 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1344 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1345 [IFLA_MTU
] = { .type
= NLA_U32
},
1346 [IFLA_LINK
] = { .type
= NLA_U32
},
1347 [IFLA_MASTER
] = { .type
= NLA_U32
},
1348 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1349 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1350 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1351 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1352 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1353 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1354 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1355 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1356 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1357 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1358 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1359 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1360 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1361 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1362 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1363 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1364 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1365 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1366 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1367 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1368 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1369 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1372 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1373 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1374 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1375 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1376 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1379 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1380 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1381 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1382 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1383 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1384 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1385 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1386 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1387 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1388 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1391 static const struct nla_policy ifla_vf_stats_policy
[IFLA_VF_STATS_MAX
+ 1] = {
1392 [IFLA_VF_STATS_RX_PACKETS
] = { .type
= NLA_U64
},
1393 [IFLA_VF_STATS_TX_PACKETS
] = { .type
= NLA_U64
},
1394 [IFLA_VF_STATS_RX_BYTES
] = { .type
= NLA_U64
},
1395 [IFLA_VF_STATS_TX_BYTES
] = { .type
= NLA_U64
},
1396 [IFLA_VF_STATS_BROADCAST
] = { .type
= NLA_U64
},
1397 [IFLA_VF_STATS_MULTICAST
] = { .type
= NLA_U64
},
1400 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1401 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1402 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1403 .len
= PORT_PROFILE_MAX
},
1404 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1405 .len
= sizeof(struct ifla_port_vsi
)},
1406 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1407 .len
= PORT_UUID_MAX
},
1408 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1409 .len
= PORT_UUID_MAX
},
1410 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1411 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1414 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1416 struct net
*net
= sock_net(skb
->sk
);
1419 struct net_device
*dev
;
1420 struct hlist_head
*head
;
1421 struct nlattr
*tb
[IFLA_MAX
+1];
1422 u32 ext_filter_mask
= 0;
1427 s_idx
= cb
->args
[1];
1429 cb
->seq
= net
->dev_base_seq
;
1431 /* A hack to preserve kernel<->userspace interface.
1432 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1433 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1434 * what iproute2 < v3.9.0 used.
1435 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1436 * attribute, its netlink message is shorter than struct ifinfomsg.
1438 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1439 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1441 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
1443 if (tb
[IFLA_EXT_MASK
])
1444 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1447 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1449 head
= &net
->dev_index_head
[h
];
1450 hlist_for_each_entry(dev
, head
, index_hlist
) {
1453 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1454 NETLINK_CB(cb
->skb
).portid
,
1455 cb
->nlh
->nlmsg_seq
, 0,
1458 /* If we ran out of room on the first message,
1461 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
1466 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1478 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
)
1480 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
);
1482 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1484 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1487 /* Examine the link attributes and figure out which
1488 * network namespace we are talking about.
1490 if (tb
[IFLA_NET_NS_PID
])
1491 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1492 else if (tb
[IFLA_NET_NS_FD
])
1493 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1495 net
= get_net(src_net
);
1498 EXPORT_SYMBOL(rtnl_link_get_net
);
1500 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1503 if (tb
[IFLA_ADDRESS
] &&
1504 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1507 if (tb
[IFLA_BROADCAST
] &&
1508 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1512 if (tb
[IFLA_AF_SPEC
]) {
1516 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1517 const struct rtnl_af_ops
*af_ops
;
1519 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1520 return -EAFNOSUPPORT
;
1522 if (!af_ops
->set_link_af
)
1525 if (af_ops
->validate_link_af
) {
1526 err
= af_ops
->validate_link_af(dev
, af
);
1536 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1538 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1541 if (tb
[IFLA_VF_MAC
]) {
1542 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1545 if (ops
->ndo_set_vf_mac
)
1546 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1552 if (tb
[IFLA_VF_VLAN
]) {
1553 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1556 if (ops
->ndo_set_vf_vlan
)
1557 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
1563 if (tb
[IFLA_VF_TX_RATE
]) {
1564 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
1565 struct ifla_vf_info ivf
;
1568 if (ops
->ndo_get_vf_config
)
1569 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1574 if (ops
->ndo_set_vf_rate
)
1575 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1582 if (tb
[IFLA_VF_RATE
]) {
1583 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1586 if (ops
->ndo_set_vf_rate
)
1587 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1594 if (tb
[IFLA_VF_SPOOFCHK
]) {
1595 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1598 if (ops
->ndo_set_vf_spoofchk
)
1599 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1605 if (tb
[IFLA_VF_LINK_STATE
]) {
1606 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1609 if (ops
->ndo_set_vf_link_state
)
1610 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1616 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1617 struct ifla_vf_rss_query_en
*ivrssq_en
;
1620 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
1621 if (ops
->ndo_set_vf_rss_query_en
)
1622 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
1623 ivrssq_en
->setting
);
1628 if (tb
[IFLA_VF_TRUST
]) {
1629 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
1632 if (ops
->ndo_set_vf_trust
)
1633 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
1641 static int do_set_master(struct net_device
*dev
, int ifindex
)
1643 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1644 const struct net_device_ops
*ops
;
1648 if (upper_dev
->ifindex
== ifindex
)
1650 ops
= upper_dev
->netdev_ops
;
1651 if (ops
->ndo_del_slave
) {
1652 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1661 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1664 ops
= upper_dev
->netdev_ops
;
1665 if (ops
->ndo_add_slave
) {
1666 err
= ops
->ndo_add_slave(upper_dev
, dev
);
1676 #define DO_SETLINK_MODIFIED 0x01
1677 /* notify flag means notify + modified. */
1678 #define DO_SETLINK_NOTIFY 0x03
1679 static int do_setlink(const struct sk_buff
*skb
,
1680 struct net_device
*dev
, struct ifinfomsg
*ifm
,
1681 struct nlattr
**tb
, char *ifname
, int status
)
1683 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1686 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1687 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1692 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1697 err
= dev_change_net_namespace(dev
, net
, ifname
);
1701 status
|= DO_SETLINK_MODIFIED
;
1705 struct rtnl_link_ifmap
*u_map
;
1708 if (!ops
->ndo_set_config
) {
1713 if (!netif_device_present(dev
)) {
1718 u_map
= nla_data(tb
[IFLA_MAP
]);
1719 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1720 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1721 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1722 k_map
.irq
= (unsigned char) u_map
->irq
;
1723 k_map
.dma
= (unsigned char) u_map
->dma
;
1724 k_map
.port
= (unsigned char) u_map
->port
;
1726 err
= ops
->ndo_set_config(dev
, &k_map
);
1730 status
|= DO_SETLINK_NOTIFY
;
1733 if (tb
[IFLA_ADDRESS
]) {
1734 struct sockaddr
*sa
;
1737 len
= sizeof(sa_family_t
) + dev
->addr_len
;
1738 sa
= kmalloc(len
, GFP_KERNEL
);
1743 sa
->sa_family
= dev
->type
;
1744 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1746 err
= dev_set_mac_address(dev
, sa
);
1750 status
|= DO_SETLINK_MODIFIED
;
1754 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1757 status
|= DO_SETLINK_MODIFIED
;
1760 if (tb
[IFLA_GROUP
]) {
1761 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1762 status
|= DO_SETLINK_NOTIFY
;
1766 * Interface selected by interface index but interface
1767 * name provided implies that a name change has been
1770 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1771 err
= dev_change_name(dev
, ifname
);
1774 status
|= DO_SETLINK_MODIFIED
;
1777 if (tb
[IFLA_IFALIAS
]) {
1778 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
1779 nla_len(tb
[IFLA_IFALIAS
]));
1782 status
|= DO_SETLINK_NOTIFY
;
1785 if (tb
[IFLA_BROADCAST
]) {
1786 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
1787 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
1790 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
1791 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1796 if (tb
[IFLA_MASTER
]) {
1797 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1800 status
|= DO_SETLINK_MODIFIED
;
1803 if (tb
[IFLA_CARRIER
]) {
1804 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
1807 status
|= DO_SETLINK_MODIFIED
;
1810 if (tb
[IFLA_TXQLEN
]) {
1811 unsigned long value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1813 if (dev
->tx_queue_len
^ value
)
1814 status
|= DO_SETLINK_NOTIFY
;
1816 dev
->tx_queue_len
= value
;
1819 if (tb
[IFLA_OPERSTATE
])
1820 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1822 if (tb
[IFLA_LINKMODE
]) {
1823 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1825 write_lock_bh(&dev_base_lock
);
1826 if (dev
->link_mode
^ value
)
1827 status
|= DO_SETLINK_NOTIFY
;
1828 dev
->link_mode
= value
;
1829 write_unlock_bh(&dev_base_lock
);
1832 if (tb
[IFLA_VFINFO_LIST
]) {
1833 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
1834 struct nlattr
*attr
;
1837 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
1838 if (nla_type(attr
) != IFLA_VF_INFO
||
1839 nla_len(attr
) < NLA_HDRLEN
) {
1843 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
1847 err
= do_setvfinfo(dev
, vfinfo
);
1850 status
|= DO_SETLINK_NOTIFY
;
1855 if (tb
[IFLA_VF_PORTS
]) {
1856 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1857 struct nlattr
*attr
;
1862 if (!ops
->ndo_set_vf_port
)
1865 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
1866 if (nla_type(attr
) != IFLA_VF_PORT
||
1867 nla_len(attr
) < NLA_HDRLEN
) {
1871 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
1875 if (!port
[IFLA_PORT_VF
]) {
1879 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
1880 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
1883 status
|= DO_SETLINK_NOTIFY
;
1888 if (tb
[IFLA_PORT_SELF
]) {
1889 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1891 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1892 tb
[IFLA_PORT_SELF
], ifla_port_policy
);
1897 if (ops
->ndo_set_vf_port
)
1898 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
1901 status
|= DO_SETLINK_NOTIFY
;
1904 if (tb
[IFLA_AF_SPEC
]) {
1908 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1909 const struct rtnl_af_ops
*af_ops
;
1911 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1914 err
= af_ops
->set_link_af(dev
, af
);
1918 status
|= DO_SETLINK_NOTIFY
;
1923 if (tb
[IFLA_PROTO_DOWN
]) {
1924 err
= dev_change_proto_down(dev
,
1925 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
1928 status
|= DO_SETLINK_NOTIFY
;
1932 if (status
& DO_SETLINK_MODIFIED
) {
1933 if (status
& DO_SETLINK_NOTIFY
)
1934 netdev_state_change(dev
);
1937 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",
1944 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1946 struct net
*net
= sock_net(skb
->sk
);
1947 struct ifinfomsg
*ifm
;
1948 struct net_device
*dev
;
1950 struct nlattr
*tb
[IFLA_MAX
+1];
1951 char ifname
[IFNAMSIZ
];
1953 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1957 if (tb
[IFLA_IFNAME
])
1958 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1963 ifm
= nlmsg_data(nlh
);
1964 if (ifm
->ifi_index
> 0)
1965 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1966 else if (tb
[IFLA_IFNAME
])
1967 dev
= __dev_get_by_name(net
, ifname
);
1976 err
= validate_linkmsg(dev
, tb
);
1980 err
= do_setlink(skb
, dev
, ifm
, tb
, ifname
, 0);
1985 static int rtnl_group_dellink(const struct net
*net
, int group
)
1987 struct net_device
*dev
, *aux
;
1988 LIST_HEAD(list_kill
);
1994 for_each_netdev(net
, dev
) {
1995 if (dev
->group
== group
) {
1996 const struct rtnl_link_ops
*ops
;
1999 ops
= dev
->rtnl_link_ops
;
2000 if (!ops
|| !ops
->dellink
)
2008 for_each_netdev_safe(net
, dev
, aux
) {
2009 if (dev
->group
== group
) {
2010 const struct rtnl_link_ops
*ops
;
2012 ops
= dev
->rtnl_link_ops
;
2013 ops
->dellink(dev
, &list_kill
);
2016 unregister_netdevice_many(&list_kill
);
2021 int rtnl_delete_link(struct net_device
*dev
)
2023 const struct rtnl_link_ops
*ops
;
2024 LIST_HEAD(list_kill
);
2026 ops
= dev
->rtnl_link_ops
;
2027 if (!ops
|| !ops
->dellink
)
2030 ops
->dellink(dev
, &list_kill
);
2031 unregister_netdevice_many(&list_kill
);
2035 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2037 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2039 struct net
*net
= sock_net(skb
->sk
);
2040 struct net_device
*dev
;
2041 struct ifinfomsg
*ifm
;
2042 char ifname
[IFNAMSIZ
];
2043 struct nlattr
*tb
[IFLA_MAX
+1];
2046 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2050 if (tb
[IFLA_IFNAME
])
2051 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2053 ifm
= nlmsg_data(nlh
);
2054 if (ifm
->ifi_index
> 0)
2055 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2056 else if (tb
[IFLA_IFNAME
])
2057 dev
= __dev_get_by_name(net
, ifname
);
2058 else if (tb
[IFLA_GROUP
])
2059 return rtnl_group_dellink(net
, nla_get_u32(tb
[IFLA_GROUP
]));
2066 return rtnl_delete_link(dev
);
2069 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2071 unsigned int old_flags
;
2074 old_flags
= dev
->flags
;
2075 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2076 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2081 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2083 __dev_notify_flags(dev
, old_flags
, ~0U);
2086 EXPORT_SYMBOL(rtnl_configure_link
);
2088 struct net_device
*rtnl_create_link(struct net
*net
,
2089 const char *ifname
, unsigned char name_assign_type
,
2090 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2093 struct net_device
*dev
;
2094 unsigned int num_tx_queues
= 1;
2095 unsigned int num_rx_queues
= 1;
2097 if (tb
[IFLA_NUM_TX_QUEUES
])
2098 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2099 else if (ops
->get_num_tx_queues
)
2100 num_tx_queues
= ops
->get_num_tx_queues();
2102 if (tb
[IFLA_NUM_RX_QUEUES
])
2103 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2104 else if (ops
->get_num_rx_queues
)
2105 num_rx_queues
= ops
->get_num_rx_queues();
2108 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2109 ops
->setup
, num_tx_queues
, num_rx_queues
);
2113 dev_net_set(dev
, net
);
2114 dev
->rtnl_link_ops
= ops
;
2115 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2118 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2119 if (tb
[IFLA_ADDRESS
]) {
2120 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2121 nla_len(tb
[IFLA_ADDRESS
]));
2122 dev
->addr_assign_type
= NET_ADDR_SET
;
2124 if (tb
[IFLA_BROADCAST
])
2125 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2126 nla_len(tb
[IFLA_BROADCAST
]));
2127 if (tb
[IFLA_TXQLEN
])
2128 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2129 if (tb
[IFLA_OPERSTATE
])
2130 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2131 if (tb
[IFLA_LINKMODE
])
2132 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2134 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2139 return ERR_PTR(err
);
2141 EXPORT_SYMBOL(rtnl_create_link
);
2143 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2144 struct net
*net
, int group
,
2145 struct ifinfomsg
*ifm
,
2148 struct net_device
*dev
, *aux
;
2151 for_each_netdev_safe(net
, dev
, aux
) {
2152 if (dev
->group
== group
) {
2153 err
= do_setlink(skb
, dev
, ifm
, tb
, NULL
, 0);
2162 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2164 struct net
*net
= sock_net(skb
->sk
);
2165 const struct rtnl_link_ops
*ops
;
2166 const struct rtnl_link_ops
*m_ops
= NULL
;
2167 struct net_device
*dev
;
2168 struct net_device
*master_dev
= NULL
;
2169 struct ifinfomsg
*ifm
;
2170 char kind
[MODULE_NAME_LEN
];
2171 char ifname
[IFNAMSIZ
];
2172 struct nlattr
*tb
[IFLA_MAX
+1];
2173 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2174 unsigned char name_assign_type
= NET_NAME_USER
;
2177 #ifdef CONFIG_MODULES
2180 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2184 if (tb
[IFLA_IFNAME
])
2185 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2189 ifm
= nlmsg_data(nlh
);
2190 if (ifm
->ifi_index
> 0)
2191 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2194 dev
= __dev_get_by_name(net
, ifname
);
2200 master_dev
= netdev_master_upper_dev_get(dev
);
2202 m_ops
= master_dev
->rtnl_link_ops
;
2205 err
= validate_linkmsg(dev
, tb
);
2209 if (tb
[IFLA_LINKINFO
]) {
2210 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2211 tb
[IFLA_LINKINFO
], ifla_info_policy
);
2215 memset(linkinfo
, 0, sizeof(linkinfo
));
2217 if (linkinfo
[IFLA_INFO_KIND
]) {
2218 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2219 ops
= rtnl_link_ops_get(kind
);
2226 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2227 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2228 struct nlattr
**data
= NULL
;
2229 struct nlattr
**slave_data
= NULL
;
2230 struct net
*dest_net
, *link_net
= NULL
;
2233 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2234 err
= nla_parse_nested(attr
, ops
->maxtype
,
2235 linkinfo
[IFLA_INFO_DATA
],
2241 if (ops
->validate
) {
2242 err
= ops
->validate(tb
, data
);
2249 if (m_ops
->slave_maxtype
&&
2250 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2251 err
= nla_parse_nested(slave_attr
,
2252 m_ops
->slave_maxtype
,
2253 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2254 m_ops
->slave_policy
);
2257 slave_data
= slave_attr
;
2259 if (m_ops
->slave_validate
) {
2260 err
= m_ops
->slave_validate(tb
, slave_data
);
2269 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2271 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2274 if (linkinfo
[IFLA_INFO_DATA
]) {
2275 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2279 err
= ops
->changelink(dev
, tb
, data
);
2282 status
|= DO_SETLINK_NOTIFY
;
2285 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2286 if (!m_ops
|| !m_ops
->slave_changelink
)
2289 err
= m_ops
->slave_changelink(master_dev
, dev
,
2293 status
|= DO_SETLINK_NOTIFY
;
2296 return do_setlink(skb
, dev
, ifm
, tb
, ifname
, status
);
2299 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2300 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2301 return rtnl_group_changelink(skb
, net
,
2302 nla_get_u32(tb
[IFLA_GROUP
]),
2307 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
2311 #ifdef CONFIG_MODULES
2314 request_module("rtnl-link-%s", kind
);
2316 ops
= rtnl_link_ops_get(kind
);
2328 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2329 name_assign_type
= NET_NAME_ENUM
;
2332 dest_net
= rtnl_link_get_net(net
, tb
);
2333 if (IS_ERR(dest_net
))
2334 return PTR_ERR(dest_net
);
2337 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2340 if (tb
[IFLA_LINK_NETNSID
]) {
2341 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2343 link_net
= get_net_ns_by_id(dest_net
, id
);
2349 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2353 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2354 name_assign_type
, ops
, tb
);
2360 dev
->ifindex
= ifm
->ifi_index
;
2363 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
);
2364 /* Drivers should call free_netdev() in ->destructor
2365 * and unregister it on failure after registration
2366 * so that device could be finally freed in rtnl_unlock.
2369 /* If device is not registered at all, free it now */
2370 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2375 err
= register_netdevice(dev
);
2381 err
= rtnl_configure_link(dev
, ifm
);
2383 goto out_unregister
;
2385 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2387 goto out_unregister
;
2396 LIST_HEAD(list_kill
);
2398 ops
->dellink(dev
, &list_kill
);
2399 unregister_netdevice_many(&list_kill
);
2401 unregister_netdevice(dev
);
2407 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
)
2409 struct net
*net
= sock_net(skb
->sk
);
2410 struct ifinfomsg
*ifm
;
2411 char ifname
[IFNAMSIZ
];
2412 struct nlattr
*tb
[IFLA_MAX
+1];
2413 struct net_device
*dev
= NULL
;
2414 struct sk_buff
*nskb
;
2416 u32 ext_filter_mask
= 0;
2418 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2422 if (tb
[IFLA_IFNAME
])
2423 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2425 if (tb
[IFLA_EXT_MASK
])
2426 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2428 ifm
= nlmsg_data(nlh
);
2429 if (ifm
->ifi_index
> 0)
2430 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2431 else if (tb
[IFLA_IFNAME
])
2432 dev
= __dev_get_by_name(net
, ifname
);
2439 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2443 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2444 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
2446 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2447 WARN_ON(err
== -EMSGSIZE
);
2450 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2455 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2457 struct net
*net
= sock_net(skb
->sk
);
2458 struct net_device
*dev
;
2459 struct nlattr
*tb
[IFLA_MAX
+1];
2460 u32 ext_filter_mask
= 0;
2461 u16 min_ifinfo_dump_size
= 0;
2464 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2465 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2466 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2468 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
2469 if (tb
[IFLA_EXT_MASK
])
2470 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2473 if (!ext_filter_mask
)
2474 return NLMSG_GOODSIZE
;
2476 * traverse the list of net devices and compute the minimum
2477 * buffer size based upon the filter mask.
2479 list_for_each_entry(dev
, &net
->dev_base_head
, dev_list
) {
2480 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2485 return min_ifinfo_dump_size
;
2488 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2491 int s_idx
= cb
->family
;
2495 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2496 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2497 if (idx
< s_idx
|| idx
== PF_PACKET
)
2499 if (rtnl_msg_handlers
[idx
] == NULL
||
2500 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
2503 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2507 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
2515 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
2516 unsigned int change
, gfp_t flags
)
2518 struct net
*net
= dev_net(dev
);
2519 struct sk_buff
*skb
;
2521 size_t if_info_size
;
2523 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2527 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
2529 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2530 WARN_ON(err
== -EMSGSIZE
);
2537 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2541 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
2543 struct net
*net
= dev_net(dev
);
2545 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
2548 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
2551 struct sk_buff
*skb
;
2553 if (dev
->reg_state
!= NETREG_REGISTERED
)
2556 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, flags
);
2558 rtmsg_ifinfo_send(skb
, dev
, flags
);
2560 EXPORT_SYMBOL(rtmsg_ifinfo
);
2562 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
2563 struct net_device
*dev
,
2564 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
2565 int type
, unsigned int flags
,
2568 struct nlmsghdr
*nlh
;
2571 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2575 ndm
= nlmsg_data(nlh
);
2576 ndm
->ndm_family
= AF_BRIDGE
;
2579 ndm
->ndm_flags
= flags
;
2581 ndm
->ndm_ifindex
= dev
->ifindex
;
2582 ndm
->ndm_state
= NUD_PERMANENT
;
2584 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
2585 goto nla_put_failure
;
2587 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
2588 goto nla_put_failure
;
2590 nlmsg_end(skb
, nlh
);
2594 nlmsg_cancel(skb
, nlh
);
2598 static inline size_t rtnl_fdb_nlmsg_size(void)
2600 return NLMSG_ALIGN(sizeof(struct ndmsg
)) + nla_total_size(ETH_ALEN
);
2603 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
)
2605 struct net
*net
= dev_net(dev
);
2606 struct sk_buff
*skb
;
2609 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2613 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
2614 0, 0, type
, NTF_SELF
, 0);
2620 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
2623 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
2627 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2629 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
2630 struct nlattr
*tb
[],
2631 struct net_device
*dev
,
2632 const unsigned char *addr
, u16 vid
,
2637 /* If aging addresses are supported device will need to
2638 * implement its own handler for this.
2640 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
2641 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2646 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
2650 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2651 err
= dev_uc_add_excl(dev
, addr
);
2652 else if (is_multicast_ether_addr(addr
))
2653 err
= dev_mc_add_excl(dev
, addr
);
2655 /* Only return duplicate errors if NLM_F_EXCL is set */
2656 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
2661 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
2663 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
2668 if (nla_len(vlan_attr
) != sizeof(u16
)) {
2669 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2673 vid
= nla_get_u16(vlan_attr
);
2675 if (!vid
|| vid
>= VLAN_VID_MASK
) {
2676 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2685 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2687 struct net
*net
= sock_net(skb
->sk
);
2689 struct nlattr
*tb
[NDA_MAX
+1];
2690 struct net_device
*dev
;
2695 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2699 ndm
= nlmsg_data(nlh
);
2700 if (ndm
->ndm_ifindex
== 0) {
2701 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2705 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2707 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2711 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2712 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2716 addr
= nla_data(tb
[NDA_LLADDR
]);
2718 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2724 /* Support fdb on master device the net/bridge default case */
2725 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2726 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2727 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2728 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2730 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2735 ndm
->ndm_flags
&= ~NTF_MASTER
;
2738 /* Embedded bridge, macvlan, and any other device support */
2739 if ((ndm
->ndm_flags
& NTF_SELF
)) {
2740 if (dev
->netdev_ops
->ndo_fdb_add
)
2741 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
2745 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2749 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
);
2750 ndm
->ndm_flags
&= ~NTF_SELF
;
2758 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2760 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
2761 struct nlattr
*tb
[],
2762 struct net_device
*dev
,
2763 const unsigned char *addr
, u16 vid
)
2767 /* If aging addresses are supported device will need to
2768 * implement its own handler for this.
2770 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
2771 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2775 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2776 err
= dev_uc_del(dev
, addr
);
2777 else if (is_multicast_ether_addr(addr
))
2778 err
= dev_mc_del(dev
, addr
);
2782 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
2784 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2786 struct net
*net
= sock_net(skb
->sk
);
2788 struct nlattr
*tb
[NDA_MAX
+1];
2789 struct net_device
*dev
;
2794 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
2797 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2801 ndm
= nlmsg_data(nlh
);
2802 if (ndm
->ndm_ifindex
== 0) {
2803 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2807 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2809 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2813 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2814 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2818 addr
= nla_data(tb
[NDA_LLADDR
]);
2820 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2826 /* Support fdb on master device the net/bridge default case */
2827 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2828 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2829 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2830 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2832 if (ops
->ndo_fdb_del
)
2833 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2838 ndm
->ndm_flags
&= ~NTF_MASTER
;
2841 /* Embedded bridge, macvlan, and any other device support */
2842 if (ndm
->ndm_flags
& NTF_SELF
) {
2843 if (dev
->netdev_ops
->ndo_fdb_del
)
2844 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
2847 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2850 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
);
2851 ndm
->ndm_flags
&= ~NTF_SELF
;
2858 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
2859 struct netlink_callback
*cb
,
2860 struct net_device
*dev
,
2862 struct netdev_hw_addr_list
*list
)
2864 struct netdev_hw_addr
*ha
;
2868 portid
= NETLINK_CB(cb
->skb
).portid
;
2869 seq
= cb
->nlh
->nlmsg_seq
;
2871 list_for_each_entry(ha
, &list
->list
, list
) {
2872 if (*idx
< cb
->args
[0])
2875 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
2877 RTM_NEWNEIGH
, NTF_SELF
,
2888 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2889 * @nlh: netlink message header
2892 * Default netdevice operation to dump the existing unicast address list.
2893 * Returns number of addresses from list put in skb.
2895 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
2896 struct netlink_callback
*cb
,
2897 struct net_device
*dev
,
2898 struct net_device
*filter_dev
,
2903 netif_addr_lock_bh(dev
);
2904 err
= nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->uc
);
2907 nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->mc
);
2909 netif_addr_unlock_bh(dev
);
2912 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
2914 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2916 struct net_device
*dev
;
2917 struct nlattr
*tb
[IFLA_MAX
+1];
2918 struct net_device
*br_dev
= NULL
;
2919 const struct net_device_ops
*ops
= NULL
;
2920 const struct net_device_ops
*cops
= NULL
;
2921 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
2922 struct net
*net
= sock_net(skb
->sk
);
2927 if (nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
, IFLA_MAX
,
2928 ifla_policy
) == 0) {
2929 if (tb
[IFLA_MASTER
])
2930 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
2933 brport_idx
= ifm
->ifi_index
;
2936 br_dev
= __dev_get_by_index(net
, br_idx
);
2940 ops
= br_dev
->netdev_ops
;
2943 for_each_netdev(net
, dev
) {
2944 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
2947 if (!br_idx
) { /* user did not specify a specific bridge */
2948 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2949 br_dev
= netdev_master_upper_dev_get(dev
);
2950 cops
= br_dev
->netdev_ops
;
2954 if (dev
!= br_dev
&&
2955 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
2958 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
2959 !(dev
->priv_flags
& IFF_EBRIDGE
))
2965 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2966 if (cops
&& cops
->ndo_fdb_dump
)
2967 idx
= cops
->ndo_fdb_dump(skb
, cb
, br_dev
, dev
,
2971 if (dev
->netdev_ops
->ndo_fdb_dump
)
2972 idx
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
, dev
, NULL
,
2975 idx
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
2984 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
2985 unsigned int attrnum
, unsigned int flag
)
2988 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
2992 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
2993 struct net_device
*dev
, u16 mode
,
2994 u32 flags
, u32 mask
, int nlflags
,
2996 int (*vlan_fill
)(struct sk_buff
*skb
,
2997 struct net_device
*dev
,
3000 struct nlmsghdr
*nlh
;
3001 struct ifinfomsg
*ifm
;
3002 struct nlattr
*br_afspec
;
3003 struct nlattr
*protinfo
;
3004 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3005 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3008 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3012 ifm
= nlmsg_data(nlh
);
3013 ifm
->ifi_family
= AF_BRIDGE
;
3015 ifm
->ifi_type
= dev
->type
;
3016 ifm
->ifi_index
= dev
->ifindex
;
3017 ifm
->ifi_flags
= dev_get_flags(dev
);
3018 ifm
->ifi_change
= 0;
3021 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3022 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3023 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3025 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3027 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3028 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3029 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3030 goto nla_put_failure
;
3032 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3034 goto nla_put_failure
;
3036 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3037 nla_nest_cancel(skb
, br_afspec
);
3038 goto nla_put_failure
;
3041 if (mode
!= BRIDGE_MODE_UNDEF
) {
3042 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3043 nla_nest_cancel(skb
, br_afspec
);
3044 goto nla_put_failure
;
3048 err
= vlan_fill(skb
, dev
, filter_mask
);
3050 nla_nest_cancel(skb
, br_afspec
);
3051 goto nla_put_failure
;
3054 nla_nest_end(skb
, br_afspec
);
3056 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3058 goto nla_put_failure
;
3060 if (brport_nla_put_flag(skb
, flags
, mask
,
3061 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3062 brport_nla_put_flag(skb
, flags
, mask
,
3063 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3064 brport_nla_put_flag(skb
, flags
, mask
,
3065 IFLA_BRPORT_FAST_LEAVE
,
3066 BR_MULTICAST_FAST_LEAVE
) ||
3067 brport_nla_put_flag(skb
, flags
, mask
,
3068 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3069 brport_nla_put_flag(skb
, flags
, mask
,
3070 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3071 brport_nla_put_flag(skb
, flags
, mask
,
3072 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3073 brport_nla_put_flag(skb
, flags
, mask
,
3074 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3075 brport_nla_put_flag(skb
, flags
, mask
,
3076 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3077 nla_nest_cancel(skb
, protinfo
);
3078 goto nla_put_failure
;
3081 nla_nest_end(skb
, protinfo
);
3083 nlmsg_end(skb
, nlh
);
3086 nlmsg_cancel(skb
, nlh
);
3087 return err
? err
: -EMSGSIZE
;
3089 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3091 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3093 struct net
*net
= sock_net(skb
->sk
);
3094 struct net_device
*dev
;
3096 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3097 u32 seq
= cb
->nlh
->nlmsg_seq
;
3098 u32 filter_mask
= 0;
3101 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3102 struct nlattr
*extfilt
;
3104 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3107 if (nla_len(extfilt
) < sizeof(filter_mask
))
3110 filter_mask
= nla_get_u32(extfilt
);
3115 for_each_netdev_rcu(net
, dev
) {
3116 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3117 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3119 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3120 if (idx
>= cb
->args
[0]) {
3121 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3122 skb
, portid
, seq
, dev
,
3123 filter_mask
, NLM_F_MULTI
);
3124 if (err
< 0 && err
!= -EOPNOTSUPP
)
3130 if (ops
->ndo_bridge_getlink
) {
3131 if (idx
>= cb
->args
[0]) {
3132 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3136 if (err
< 0 && err
!= -EOPNOTSUPP
)
3148 static inline size_t bridge_nlmsg_size(void)
3150 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3151 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3152 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3153 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3154 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3155 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3156 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3157 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3158 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3159 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3160 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3163 static int rtnl_bridge_notify(struct net_device
*dev
)
3165 struct net
*net
= dev_net(dev
);
3166 struct sk_buff
*skb
;
3167 int err
= -EOPNOTSUPP
;
3169 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3172 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3178 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3185 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3188 WARN_ON(err
== -EMSGSIZE
);
3191 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3195 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3197 struct net
*net
= sock_net(skb
->sk
);
3198 struct ifinfomsg
*ifm
;
3199 struct net_device
*dev
;
3200 struct nlattr
*br_spec
, *attr
= NULL
;
3201 int rem
, err
= -EOPNOTSUPP
;
3203 bool have_flags
= false;
3205 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3208 ifm
= nlmsg_data(nlh
);
3209 if (ifm
->ifi_family
!= AF_BRIDGE
)
3210 return -EPFNOSUPPORT
;
3212 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3214 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3218 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3220 nla_for_each_nested(attr
, br_spec
, rem
) {
3221 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3222 if (nla_len(attr
) < sizeof(flags
))
3226 flags
= nla_get_u16(attr
);
3232 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3233 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3235 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3240 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3244 flags
&= ~BRIDGE_FLAGS_MASTER
;
3247 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3248 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3251 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3254 flags
&= ~BRIDGE_FLAGS_SELF
;
3256 /* Generate event to notify upper layer of bridge
3259 err
= rtnl_bridge_notify(dev
);
3264 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3269 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3271 struct net
*net
= sock_net(skb
->sk
);
3272 struct ifinfomsg
*ifm
;
3273 struct net_device
*dev
;
3274 struct nlattr
*br_spec
, *attr
= NULL
;
3275 int rem
, err
= -EOPNOTSUPP
;
3277 bool have_flags
= false;
3279 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3282 ifm
= nlmsg_data(nlh
);
3283 if (ifm
->ifi_family
!= AF_BRIDGE
)
3284 return -EPFNOSUPPORT
;
3286 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3288 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3292 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3294 nla_for_each_nested(attr
, br_spec
, rem
) {
3295 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3296 if (nla_len(attr
) < sizeof(flags
))
3300 flags
= nla_get_u16(attr
);
3306 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3307 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3309 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3314 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3318 flags
&= ~BRIDGE_FLAGS_MASTER
;
3321 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3322 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3325 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3329 flags
&= ~BRIDGE_FLAGS_SELF
;
3331 /* Generate event to notify upper layer of bridge
3334 err
= rtnl_bridge_notify(dev
);
3339 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3344 /* Process one rtnetlink message. */
3346 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3348 struct net
*net
= sock_net(skb
->sk
);
3349 rtnl_doit_func doit
;
3355 type
= nlh
->nlmsg_type
;
3361 /* All the messages must have at least 1 byte length */
3362 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
3365 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
3369 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
3372 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
3374 rtnl_dumpit_func dumpit
;
3375 rtnl_calcit_func calcit
;
3376 u16 min_dump_alloc
= 0;
3378 dumpit
= rtnl_get_dumpit(family
, type
);
3381 calcit
= rtnl_get_calcit(family
, type
);
3383 min_dump_alloc
= calcit(skb
, nlh
);
3388 struct netlink_dump_control c
= {
3390 .min_dump_alloc
= min_dump_alloc
,
3392 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
3398 doit
= rtnl_get_doit(family
, type
);
3402 return doit(skb
, nlh
);
3405 static void rtnetlink_rcv(struct sk_buff
*skb
)
3408 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
3412 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3414 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3420 case NETDEV_POST_INIT
:
3421 case NETDEV_REGISTER
:
3423 case NETDEV_PRE_TYPE_CHANGE
:
3424 case NETDEV_GOING_DOWN
:
3425 case NETDEV_UNREGISTER
:
3426 case NETDEV_UNREGISTER_FINAL
:
3427 case NETDEV_RELEASE
:
3429 case NETDEV_BONDING_INFO
:
3432 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
3438 static struct notifier_block rtnetlink_dev_notifier
= {
3439 .notifier_call
= rtnetlink_event
,
3443 static int __net_init
rtnetlink_net_init(struct net
*net
)
3446 struct netlink_kernel_cfg cfg
= {
3447 .groups
= RTNLGRP_MAX
,
3448 .input
= rtnetlink_rcv
,
3449 .cb_mutex
= &rtnl_mutex
,
3450 .flags
= NL_CFG_F_NONROOT_RECV
,
3453 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
3460 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
3462 netlink_kernel_release(net
->rtnl
);
3466 static struct pernet_operations rtnetlink_net_ops
= {
3467 .init
= rtnetlink_net_init
,
3468 .exit
= rtnetlink_net_exit
,
3471 void __init
rtnetlink_init(void)
3473 if (register_pernet_subsys(&rtnetlink_net_ops
))
3474 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3476 register_netdevice_notifier(&rtnetlink_dev_notifier
);
3478 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
3479 rtnl_dump_ifinfo
, rtnl_calcit
);
3480 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, NULL
);
3481 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, NULL
);
3482 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, NULL
);
3484 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, NULL
);
3485 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, NULL
);
3487 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, NULL
);
3488 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, NULL
);
3489 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, NULL
);
3491 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, NULL
);
3492 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, NULL
);
3493 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, NULL
);