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 static struct sk_buff
*defer_kfree_skb_list
;
75 void rtnl_kfree_skbs(struct sk_buff
*head
, struct sk_buff
*tail
)
78 tail
->next
= defer_kfree_skb_list
;
79 defer_kfree_skb_list
= head
;
82 EXPORT_SYMBOL(rtnl_kfree_skbs
);
84 void __rtnl_unlock(void)
86 struct sk_buff
*head
= defer_kfree_skb_list
;
88 defer_kfree_skb_list
= NULL
;
90 mutex_unlock(&rtnl_mutex
);
93 struct sk_buff
*next
= head
->next
;
101 void rtnl_unlock(void)
103 /* This fellow will unlock it for us. */
106 EXPORT_SYMBOL(rtnl_unlock
);
108 int rtnl_trylock(void)
110 return mutex_trylock(&rtnl_mutex
);
112 EXPORT_SYMBOL(rtnl_trylock
);
114 int rtnl_is_locked(void)
116 return mutex_is_locked(&rtnl_mutex
);
118 EXPORT_SYMBOL(rtnl_is_locked
);
120 #ifdef CONFIG_PROVE_LOCKING
121 bool lockdep_rtnl_is_held(void)
123 return lockdep_is_held(&rtnl_mutex
);
125 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
126 #endif /* #ifdef CONFIG_PROVE_LOCKING */
128 static struct rtnl_link
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
130 static inline int rtm_msgindex(int msgtype
)
132 int msgindex
= msgtype
- RTM_BASE
;
135 * msgindex < 0 implies someone tried to register a netlink
136 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
137 * the message type has not been added to linux/rtnetlink.h
139 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
144 static rtnl_doit_func
rtnl_get_doit(int protocol
, int msgindex
)
146 struct rtnl_link
*tab
;
148 if (protocol
<= RTNL_FAMILY_MAX
)
149 tab
= rtnl_msg_handlers
[protocol
];
153 if (tab
== NULL
|| tab
[msgindex
].doit
== NULL
)
154 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
156 return tab
[msgindex
].doit
;
159 static rtnl_dumpit_func
rtnl_get_dumpit(int protocol
, int msgindex
)
161 struct rtnl_link
*tab
;
163 if (protocol
<= RTNL_FAMILY_MAX
)
164 tab
= rtnl_msg_handlers
[protocol
];
168 if (tab
== NULL
|| tab
[msgindex
].dumpit
== NULL
)
169 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
171 return tab
[msgindex
].dumpit
;
174 static rtnl_calcit_func
rtnl_get_calcit(int protocol
, int msgindex
)
176 struct rtnl_link
*tab
;
178 if (protocol
<= RTNL_FAMILY_MAX
)
179 tab
= rtnl_msg_handlers
[protocol
];
183 if (tab
== NULL
|| tab
[msgindex
].calcit
== NULL
)
184 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
186 return tab
[msgindex
].calcit
;
190 * __rtnl_register - Register a rtnetlink message type
191 * @protocol: Protocol family or PF_UNSPEC
192 * @msgtype: rtnetlink message type
193 * @doit: Function pointer called for each request message
194 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
195 * @calcit: Function pointer to calc size of dump message
197 * Registers the specified function pointers (at least one of them has
198 * to be non-NULL) to be called whenever a request message for the
199 * specified protocol family and message type is received.
201 * The special protocol family PF_UNSPEC may be used to define fallback
202 * function pointers for the case when no entry for the specific protocol
205 * Returns 0 on success or a negative error code.
207 int __rtnl_register(int protocol
, int msgtype
,
208 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
209 rtnl_calcit_func calcit
)
211 struct rtnl_link
*tab
;
214 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
215 msgindex
= rtm_msgindex(msgtype
);
217 tab
= rtnl_msg_handlers
[protocol
];
219 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
223 rtnl_msg_handlers
[protocol
] = tab
;
227 tab
[msgindex
].doit
= doit
;
230 tab
[msgindex
].dumpit
= dumpit
;
233 tab
[msgindex
].calcit
= calcit
;
237 EXPORT_SYMBOL_GPL(__rtnl_register
);
240 * rtnl_register - Register a rtnetlink message type
242 * Identical to __rtnl_register() but panics on failure. This is useful
243 * as failure of this function is very unlikely, it can only happen due
244 * to lack of memory when allocating the chain to store all message
245 * handlers for a protocol. Meant for use in init functions where lack
246 * of memory implies no sense in continuing.
248 void rtnl_register(int protocol
, int msgtype
,
249 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
250 rtnl_calcit_func calcit
)
252 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
, calcit
) < 0)
253 panic("Unable to register rtnetlink message handler, "
254 "protocol = %d, message type = %d\n",
257 EXPORT_SYMBOL_GPL(rtnl_register
);
260 * rtnl_unregister - Unregister a rtnetlink message type
261 * @protocol: Protocol family or PF_UNSPEC
262 * @msgtype: rtnetlink message type
264 * Returns 0 on success or a negative error code.
266 int rtnl_unregister(int protocol
, int msgtype
)
270 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
271 msgindex
= rtm_msgindex(msgtype
);
273 if (rtnl_msg_handlers
[protocol
] == NULL
)
276 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
277 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
281 EXPORT_SYMBOL_GPL(rtnl_unregister
);
284 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
285 * @protocol : Protocol family or PF_UNSPEC
287 * Identical to calling rtnl_unregster() for all registered message types
288 * of a certain protocol family.
290 void rtnl_unregister_all(int protocol
)
292 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
294 kfree(rtnl_msg_handlers
[protocol
]);
295 rtnl_msg_handlers
[protocol
] = NULL
;
297 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
299 static LIST_HEAD(link_ops
);
301 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
303 const struct rtnl_link_ops
*ops
;
305 list_for_each_entry(ops
, &link_ops
, list
) {
306 if (!strcmp(ops
->kind
, kind
))
313 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
316 * The caller must hold the rtnl_mutex. This function should be used
317 * by drivers that create devices during module initialization. It
318 * must be called before registering the devices.
320 * Returns 0 on success or a negative error code.
322 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
324 if (rtnl_link_ops_get(ops
->kind
))
327 /* The check for setup is here because if ops
328 * does not have that filled up, it is not possible
329 * to use the ops for creating device. So do not
330 * fill up dellink as well. That disables rtnl_dellink.
332 if (ops
->setup
&& !ops
->dellink
)
333 ops
->dellink
= unregister_netdevice_queue
;
335 list_add_tail(&ops
->list
, &link_ops
);
338 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
341 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
342 * @ops: struct rtnl_link_ops * to register
344 * Returns 0 on success or a negative error code.
346 int rtnl_link_register(struct rtnl_link_ops
*ops
)
351 err
= __rtnl_link_register(ops
);
355 EXPORT_SYMBOL_GPL(rtnl_link_register
);
357 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
359 struct net_device
*dev
;
360 LIST_HEAD(list_kill
);
362 for_each_netdev(net
, dev
) {
363 if (dev
->rtnl_link_ops
== ops
)
364 ops
->dellink(dev
, &list_kill
);
366 unregister_netdevice_many(&list_kill
);
370 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
371 * @ops: struct rtnl_link_ops * to unregister
373 * The caller must hold the rtnl_mutex.
375 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
380 __rtnl_kill_links(net
, ops
);
382 list_del(&ops
->list
);
384 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
386 /* Return with the rtnl_lock held when there are no network
387 * devices unregistering in any network namespace.
389 static void rtnl_lock_unregistering_all(void)
393 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
395 add_wait_queue(&netdev_unregistering_wq
, &wait
);
397 unregistering
= false;
400 if (net
->dev_unreg_count
> 0) {
401 unregistering
= true;
409 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
411 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
415 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
416 * @ops: struct rtnl_link_ops * to unregister
418 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
420 /* Close the race with cleanup_net() */
421 mutex_lock(&net_mutex
);
422 rtnl_lock_unregistering_all();
423 __rtnl_link_unregister(ops
);
425 mutex_unlock(&net_mutex
);
427 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
429 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
431 struct net_device
*master_dev
;
432 const struct rtnl_link_ops
*ops
;
434 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
437 ops
= master_dev
->rtnl_link_ops
;
438 if (!ops
|| !ops
->get_slave_size
)
440 /* IFLA_INFO_SLAVE_DATA + nested data */
441 return nla_total_size(sizeof(struct nlattr
)) +
442 ops
->get_slave_size(master_dev
, dev
);
445 static size_t rtnl_link_get_size(const struct net_device
*dev
)
447 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
453 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
454 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
457 /* IFLA_INFO_DATA + nested data */
458 size
+= nla_total_size(sizeof(struct nlattr
)) +
461 if (ops
->get_xstats_size
)
462 /* IFLA_INFO_XSTATS */
463 size
+= nla_total_size(ops
->get_xstats_size(dev
));
465 size
+= rtnl_link_get_slave_info_data_size(dev
);
470 static LIST_HEAD(rtnl_af_ops
);
472 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
474 const struct rtnl_af_ops
*ops
;
476 list_for_each_entry(ops
, &rtnl_af_ops
, list
) {
477 if (ops
->family
== family
)
485 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
486 * @ops: struct rtnl_af_ops * to register
488 * Returns 0 on success or a negative error code.
490 void rtnl_af_register(struct rtnl_af_ops
*ops
)
493 list_add_tail(&ops
->list
, &rtnl_af_ops
);
496 EXPORT_SYMBOL_GPL(rtnl_af_register
);
499 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
500 * @ops: struct rtnl_af_ops * to unregister
502 * The caller must hold the rtnl_mutex.
504 void __rtnl_af_unregister(struct rtnl_af_ops
*ops
)
506 list_del(&ops
->list
);
508 EXPORT_SYMBOL_GPL(__rtnl_af_unregister
);
511 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
512 * @ops: struct rtnl_af_ops * to unregister
514 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
517 __rtnl_af_unregister(ops
);
520 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
522 static size_t rtnl_link_get_af_size(const struct net_device
*dev
,
525 struct rtnl_af_ops
*af_ops
;
529 size
= nla_total_size(sizeof(struct nlattr
));
531 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
532 if (af_ops
->get_link_af_size
) {
533 /* AF_* + nested data */
534 size
+= nla_total_size(sizeof(struct nlattr
)) +
535 af_ops
->get_link_af_size(dev
, ext_filter_mask
);
542 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
544 struct net_device
*master_dev
;
546 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
547 if (master_dev
&& master_dev
->rtnl_link_ops
)
552 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
553 const struct net_device
*dev
)
555 struct net_device
*master_dev
;
556 const struct rtnl_link_ops
*ops
;
557 struct nlattr
*slave_data
;
560 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
563 ops
= master_dev
->rtnl_link_ops
;
566 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
568 if (ops
->fill_slave_info
) {
569 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
572 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
574 goto err_cancel_slave_data
;
575 nla_nest_end(skb
, slave_data
);
579 err_cancel_slave_data
:
580 nla_nest_cancel(skb
, slave_data
);
584 static int rtnl_link_info_fill(struct sk_buff
*skb
,
585 const struct net_device
*dev
)
587 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
593 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
595 if (ops
->fill_xstats
) {
596 err
= ops
->fill_xstats(skb
, dev
);
600 if (ops
->fill_info
) {
601 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
604 err
= ops
->fill_info(skb
, dev
);
606 goto err_cancel_data
;
607 nla_nest_end(skb
, data
);
612 nla_nest_cancel(skb
, data
);
616 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
618 struct nlattr
*linkinfo
;
621 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
622 if (linkinfo
== NULL
)
625 err
= rtnl_link_info_fill(skb
, dev
);
627 goto err_cancel_link
;
629 err
= rtnl_link_slave_info_fill(skb
, dev
);
631 goto err_cancel_link
;
633 nla_nest_end(skb
, linkinfo
);
637 nla_nest_cancel(skb
, linkinfo
);
642 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
644 struct sock
*rtnl
= net
->rtnl
;
647 NETLINK_CB(skb
).dst_group
= group
;
649 atomic_inc(&skb
->users
);
650 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
652 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
656 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
658 struct sock
*rtnl
= net
->rtnl
;
660 return nlmsg_unicast(rtnl
, skb
, pid
);
662 EXPORT_SYMBOL(rtnl_unicast
);
664 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
665 struct nlmsghdr
*nlh
, gfp_t flags
)
667 struct sock
*rtnl
= net
->rtnl
;
671 report
= nlmsg_report(nlh
);
673 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
675 EXPORT_SYMBOL(rtnl_notify
);
677 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
679 struct sock
*rtnl
= net
->rtnl
;
681 netlink_set_err(rtnl
, 0, group
, error
);
683 EXPORT_SYMBOL(rtnl_set_sk_err
);
685 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
690 mx
= nla_nest_start(skb
, RTA_METRICS
);
694 for (i
= 0; i
< RTAX_MAX
; i
++) {
696 if (i
== RTAX_CC_ALGO
- 1) {
697 char tmp
[TCP_CA_NAME_MAX
], *name
;
699 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
702 if (nla_put_string(skb
, i
+ 1, name
))
703 goto nla_put_failure
;
704 } else if (i
== RTAX_FEATURES
- 1) {
705 u32 user_features
= metrics
[i
] & RTAX_FEATURE_MASK
;
707 BUILD_BUG_ON(RTAX_FEATURE_MASK
& DST_FEATURE_MASK
);
708 if (nla_put_u32(skb
, i
+ 1, user_features
))
709 goto nla_put_failure
;
711 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
712 goto nla_put_failure
;
719 nla_nest_cancel(skb
, mx
);
723 return nla_nest_end(skb
, mx
);
726 nla_nest_cancel(skb
, mx
);
729 EXPORT_SYMBOL(rtnetlink_put_metrics
);
731 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
732 long expires
, u32 error
)
734 struct rta_cacheinfo ci
= {
735 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
736 .rta_used
= dst
->__use
,
737 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
745 clock
= jiffies_to_clock_t(abs(expires
));
746 clock
= min_t(unsigned long, clock
, INT_MAX
);
747 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
749 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
751 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
753 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
755 unsigned char operstate
= dev
->operstate
;
757 switch (transition
) {
759 if ((operstate
== IF_OPER_DORMANT
||
760 operstate
== IF_OPER_UNKNOWN
) &&
762 operstate
= IF_OPER_UP
;
765 case IF_OPER_DORMANT
:
766 if (operstate
== IF_OPER_UP
||
767 operstate
== IF_OPER_UNKNOWN
)
768 operstate
= IF_OPER_DORMANT
;
772 if (dev
->operstate
!= operstate
) {
773 write_lock_bh(&dev_base_lock
);
774 dev
->operstate
= operstate
;
775 write_unlock_bh(&dev_base_lock
);
776 netdev_state_change(dev
);
780 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
782 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
783 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
786 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
787 const struct ifinfomsg
*ifm
)
789 unsigned int flags
= ifm
->ifi_flags
;
791 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
793 flags
= (flags
& ifm
->ifi_change
) |
794 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
799 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
800 const struct rtnl_link_stats64
*b
)
802 a
->rx_packets
= b
->rx_packets
;
803 a
->tx_packets
= b
->tx_packets
;
804 a
->rx_bytes
= b
->rx_bytes
;
805 a
->tx_bytes
= b
->tx_bytes
;
806 a
->rx_errors
= b
->rx_errors
;
807 a
->tx_errors
= b
->tx_errors
;
808 a
->rx_dropped
= b
->rx_dropped
;
809 a
->tx_dropped
= b
->tx_dropped
;
811 a
->multicast
= b
->multicast
;
812 a
->collisions
= b
->collisions
;
814 a
->rx_length_errors
= b
->rx_length_errors
;
815 a
->rx_over_errors
= b
->rx_over_errors
;
816 a
->rx_crc_errors
= b
->rx_crc_errors
;
817 a
->rx_frame_errors
= b
->rx_frame_errors
;
818 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
819 a
->rx_missed_errors
= b
->rx_missed_errors
;
821 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
822 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
823 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
824 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
825 a
->tx_window_errors
= b
->tx_window_errors
;
827 a
->rx_compressed
= b
->rx_compressed
;
828 a
->tx_compressed
= b
->tx_compressed
;
830 a
->rx_nohandler
= b
->rx_nohandler
;
834 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
837 if (dev
->dev
.parent
&& dev_is_pci(dev
->dev
.parent
) &&
838 (ext_filter_mask
& RTEXT_FILTER_VF
)) {
839 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
840 size_t size
= nla_total_size(sizeof(struct nlattr
));
841 size
+= nla_total_size(num_vfs
* sizeof(struct nlattr
));
843 (nla_total_size(sizeof(struct ifla_vf_mac
)) +
844 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
845 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
846 nla_total_size(sizeof(struct ifla_vf_rate
)) +
847 nla_total_size(sizeof(struct ifla_vf_link_state
)) +
848 nla_total_size(sizeof(struct ifla_vf_rss_query_en
)) +
849 /* IFLA_VF_STATS_RX_PACKETS */
850 nla_total_size_64bit(sizeof(__u64
)) +
851 /* IFLA_VF_STATS_TX_PACKETS */
852 nla_total_size_64bit(sizeof(__u64
)) +
853 /* IFLA_VF_STATS_RX_BYTES */
854 nla_total_size_64bit(sizeof(__u64
)) +
855 /* IFLA_VF_STATS_TX_BYTES */
856 nla_total_size_64bit(sizeof(__u64
)) +
857 /* IFLA_VF_STATS_BROADCAST */
858 nla_total_size_64bit(sizeof(__u64
)) +
859 /* IFLA_VF_STATS_MULTICAST */
860 nla_total_size_64bit(sizeof(__u64
)) +
861 nla_total_size(sizeof(struct ifla_vf_trust
)));
867 static size_t rtnl_port_size(const struct net_device
*dev
,
870 size_t port_size
= nla_total_size(4) /* PORT_VF */
871 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
872 + nla_total_size(sizeof(struct ifla_port_vsi
))
874 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
875 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
876 + nla_total_size(1) /* PROT_VDP_REQUEST */
877 + nla_total_size(2); /* PORT_VDP_RESPONSE */
878 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
879 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
881 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
884 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
885 !(ext_filter_mask
& RTEXT_FILTER_VF
))
887 if (dev_num_vf(dev
->dev
.parent
))
888 return port_self_size
+ vf_ports_size
+
889 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
891 return port_self_size
;
894 static size_t rtnl_xdp_size(const struct net_device
*dev
)
896 size_t xdp_size
= nla_total_size(1); /* XDP_ATTACHED */
898 if (!dev
->netdev_ops
->ndo_xdp
)
904 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
907 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
908 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
909 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
910 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
911 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap
))
912 + nla_total_size(sizeof(struct rtnl_link_stats
))
913 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64
))
914 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
915 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
916 + nla_total_size(4) /* IFLA_TXQLEN */
917 + nla_total_size(4) /* IFLA_WEIGHT */
918 + nla_total_size(4) /* IFLA_MTU */
919 + nla_total_size(4) /* IFLA_LINK */
920 + nla_total_size(4) /* IFLA_MASTER */
921 + nla_total_size(1) /* IFLA_CARRIER */
922 + nla_total_size(4) /* IFLA_PROMISCUITY */
923 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
924 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
925 + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
926 + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
927 + nla_total_size(1) /* IFLA_OPERSTATE */
928 + nla_total_size(1) /* IFLA_LINKMODE */
929 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
930 + nla_total_size(4) /* IFLA_LINK_NETNSID */
931 + nla_total_size(ext_filter_mask
932 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
933 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
934 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
935 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
936 + rtnl_link_get_af_size(dev
, ext_filter_mask
) /* IFLA_AF_SPEC */
937 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
938 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_SWITCH_ID */
939 + nla_total_size(IFNAMSIZ
) /* IFLA_PHYS_PORT_NAME */
940 + rtnl_xdp_size(dev
) /* IFLA_XDP */
941 + nla_total_size(1); /* IFLA_PROTO_DOWN */
945 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
947 struct nlattr
*vf_ports
;
948 struct nlattr
*vf_port
;
952 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
956 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
957 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
959 goto nla_put_failure
;
960 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
961 goto nla_put_failure
;
962 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
963 if (err
== -EMSGSIZE
)
964 goto nla_put_failure
;
966 nla_nest_cancel(skb
, vf_port
);
969 nla_nest_end(skb
, vf_port
);
972 nla_nest_end(skb
, vf_ports
);
977 nla_nest_cancel(skb
, vf_ports
);
981 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
983 struct nlattr
*port_self
;
986 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
990 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
992 nla_nest_cancel(skb
, port_self
);
993 return (err
== -EMSGSIZE
) ? err
: 0;
996 nla_nest_end(skb
, port_self
);
1001 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
1002 u32 ext_filter_mask
)
1006 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
1007 !(ext_filter_mask
& RTEXT_FILTER_VF
))
1010 err
= rtnl_port_self_fill(skb
, dev
);
1014 if (dev_num_vf(dev
->dev
.parent
)) {
1015 err
= rtnl_vf_ports_fill(skb
, dev
);
1023 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1026 struct netdev_phys_item_id ppid
;
1028 err
= dev_get_phys_port_id(dev
, &ppid
);
1030 if (err
== -EOPNOTSUPP
)
1035 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
1041 static int rtnl_phys_port_name_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1043 char name
[IFNAMSIZ
];
1046 err
= dev_get_phys_port_name(dev
, name
, sizeof(name
));
1048 if (err
== -EOPNOTSUPP
)
1053 if (nla_put(skb
, IFLA_PHYS_PORT_NAME
, strlen(name
), name
))
1059 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1062 struct switchdev_attr attr
= {
1064 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1065 .flags
= SWITCHDEV_F_NO_RECURSE
,
1068 err
= switchdev_port_attr_get(dev
, &attr
);
1070 if (err
== -EOPNOTSUPP
)
1075 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, attr
.u
.ppid
.id_len
,
1082 static noinline_for_stack
int rtnl_fill_stats(struct sk_buff
*skb
,
1083 struct net_device
*dev
)
1085 struct rtnl_link_stats64
*sp
;
1086 struct nlattr
*attr
;
1088 attr
= nla_reserve_64bit(skb
, IFLA_STATS64
,
1089 sizeof(struct rtnl_link_stats64
), IFLA_PAD
);
1093 sp
= nla_data(attr
);
1094 dev_get_stats(dev
, sp
);
1096 attr
= nla_reserve(skb
, IFLA_STATS
,
1097 sizeof(struct rtnl_link_stats
));
1101 copy_rtnl_link_stats(nla_data(attr
), sp
);
1106 static noinline_for_stack
int rtnl_fill_vfinfo(struct sk_buff
*skb
,
1107 struct net_device
*dev
,
1109 struct nlattr
*vfinfo
)
1111 struct ifla_vf_rss_query_en vf_rss_query_en
;
1112 struct ifla_vf_link_state vf_linkstate
;
1113 struct ifla_vf_spoofchk vf_spoofchk
;
1114 struct ifla_vf_tx_rate vf_tx_rate
;
1115 struct ifla_vf_stats vf_stats
;
1116 struct ifla_vf_trust vf_trust
;
1117 struct ifla_vf_vlan vf_vlan
;
1118 struct ifla_vf_rate vf_rate
;
1119 struct nlattr
*vf
, *vfstats
;
1120 struct ifla_vf_mac vf_mac
;
1121 struct ifla_vf_info ivi
;
1123 /* Not all SR-IOV capable drivers support the
1124 * spoofcheck and "RSS query enable" query. Preset to
1125 * -1 so the user space tool can detect that the driver
1126 * didn't report anything.
1129 ivi
.rss_query_en
= -1;
1131 memset(ivi
.mac
, 0, sizeof(ivi
.mac
));
1132 /* The default value for VF link state is "auto"
1133 * IFLA_VF_LINK_STATE_AUTO which equals zero
1136 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, vfs_num
, &ivi
))
1145 vf_rss_query_en
.vf
=
1146 vf_trust
.vf
= ivi
.vf
;
1148 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1149 vf_vlan
.vlan
= ivi
.vlan
;
1150 vf_vlan
.qos
= ivi
.qos
;
1151 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1152 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1153 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1154 vf_spoofchk
.setting
= ivi
.spoofchk
;
1155 vf_linkstate
.link_state
= ivi
.linkstate
;
1156 vf_rss_query_en
.setting
= ivi
.rss_query_en
;
1157 vf_trust
.setting
= ivi
.trusted
;
1158 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1160 nla_nest_cancel(skb
, vfinfo
);
1163 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1164 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1165 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1167 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1169 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1171 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1173 nla_put(skb
, IFLA_VF_RSS_QUERY_EN
,
1174 sizeof(vf_rss_query_en
),
1175 &vf_rss_query_en
) ||
1176 nla_put(skb
, IFLA_VF_TRUST
,
1177 sizeof(vf_trust
), &vf_trust
))
1179 memset(&vf_stats
, 0, sizeof(vf_stats
));
1180 if (dev
->netdev_ops
->ndo_get_vf_stats
)
1181 dev
->netdev_ops
->ndo_get_vf_stats(dev
, vfs_num
,
1183 vfstats
= nla_nest_start(skb
, IFLA_VF_STATS
);
1185 nla_nest_cancel(skb
, vf
);
1186 nla_nest_cancel(skb
, vfinfo
);
1189 if (nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_PACKETS
,
1190 vf_stats
.rx_packets
, IFLA_VF_STATS_PAD
) ||
1191 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_PACKETS
,
1192 vf_stats
.tx_packets
, IFLA_VF_STATS_PAD
) ||
1193 nla_put_u64_64bit(skb
, IFLA_VF_STATS_RX_BYTES
,
1194 vf_stats
.rx_bytes
, IFLA_VF_STATS_PAD
) ||
1195 nla_put_u64_64bit(skb
, IFLA_VF_STATS_TX_BYTES
,
1196 vf_stats
.tx_bytes
, IFLA_VF_STATS_PAD
) ||
1197 nla_put_u64_64bit(skb
, IFLA_VF_STATS_BROADCAST
,
1198 vf_stats
.broadcast
, IFLA_VF_STATS_PAD
) ||
1199 nla_put_u64_64bit(skb
, IFLA_VF_STATS_MULTICAST
,
1200 vf_stats
.multicast
, IFLA_VF_STATS_PAD
))
1202 nla_nest_end(skb
, vfstats
);
1203 nla_nest_end(skb
, vf
);
1207 static int rtnl_fill_link_ifmap(struct sk_buff
*skb
, struct net_device
*dev
)
1209 struct rtnl_link_ifmap map
;
1211 memset(&map
, 0, sizeof(map
));
1212 map
.mem_start
= dev
->mem_start
;
1213 map
.mem_end
= dev
->mem_end
;
1214 map
.base_addr
= dev
->base_addr
;
1217 map
.port
= dev
->if_port
;
1219 if (nla_put_64bit(skb
, IFLA_MAP
, sizeof(map
), &map
, IFLA_PAD
))
1225 static int rtnl_xdp_fill(struct sk_buff
*skb
, struct net_device
*dev
)
1227 struct netdev_xdp xdp_op
= {};
1231 if (!dev
->netdev_ops
->ndo_xdp
)
1233 xdp
= nla_nest_start(skb
, IFLA_XDP
);
1236 xdp_op
.command
= XDP_QUERY_PROG
;
1237 err
= dev
->netdev_ops
->ndo_xdp(dev
, &xdp_op
);
1240 err
= nla_put_u8(skb
, IFLA_XDP_ATTACHED
, xdp_op
.prog_attached
);
1244 nla_nest_end(skb
, xdp
);
1248 nla_nest_cancel(skb
, xdp
);
1252 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
1253 int type
, u32 pid
, u32 seq
, u32 change
,
1254 unsigned int flags
, u32 ext_filter_mask
)
1256 struct ifinfomsg
*ifm
;
1257 struct nlmsghdr
*nlh
;
1258 struct nlattr
*af_spec
;
1259 struct rtnl_af_ops
*af_ops
;
1260 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1263 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1267 ifm
= nlmsg_data(nlh
);
1268 ifm
->ifi_family
= AF_UNSPEC
;
1270 ifm
->ifi_type
= dev
->type
;
1271 ifm
->ifi_index
= dev
->ifindex
;
1272 ifm
->ifi_flags
= dev_get_flags(dev
);
1273 ifm
->ifi_change
= change
;
1275 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1276 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1277 nla_put_u8(skb
, IFLA_OPERSTATE
,
1278 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1279 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1280 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1281 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1282 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1283 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1284 nla_put_u32(skb
, IFLA_GSO_MAX_SEGS
, dev
->gso_max_segs
) ||
1285 nla_put_u32(skb
, IFLA_GSO_MAX_SIZE
, dev
->gso_max_size
) ||
1287 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1289 (dev
->ifindex
!= dev_get_iflink(dev
) &&
1290 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
1292 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1293 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1295 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1297 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)) ||
1298 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1299 atomic_read(&dev
->carrier_changes
)) ||
1300 nla_put_u8(skb
, IFLA_PROTO_DOWN
, dev
->proto_down
))
1301 goto nla_put_failure
;
1303 if (rtnl_fill_link_ifmap(skb
, dev
))
1304 goto nla_put_failure
;
1306 if (dev
->addr_len
) {
1307 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1308 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1309 goto nla_put_failure
;
1312 if (rtnl_phys_port_id_fill(skb
, dev
))
1313 goto nla_put_failure
;
1315 if (rtnl_phys_port_name_fill(skb
, dev
))
1316 goto nla_put_failure
;
1318 if (rtnl_phys_switch_id_fill(skb
, dev
))
1319 goto nla_put_failure
;
1321 if (rtnl_fill_stats(skb
, dev
))
1322 goto nla_put_failure
;
1324 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
1325 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
1326 goto nla_put_failure
;
1328 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
&&
1329 ext_filter_mask
& RTEXT_FILTER_VF
) {
1331 struct nlattr
*vfinfo
;
1332 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1334 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1336 goto nla_put_failure
;
1337 for (i
= 0; i
< num_vfs
; i
++) {
1338 if (rtnl_fill_vfinfo(skb
, dev
, i
, vfinfo
))
1339 goto nla_put_failure
;
1342 nla_nest_end(skb
, vfinfo
);
1345 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1346 goto nla_put_failure
;
1348 if (rtnl_xdp_fill(skb
, dev
))
1349 goto nla_put_failure
;
1351 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1352 if (rtnl_link_fill(skb
, dev
) < 0)
1353 goto nla_put_failure
;
1356 if (dev
->rtnl_link_ops
&&
1357 dev
->rtnl_link_ops
->get_link_net
) {
1358 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1360 if (!net_eq(dev_net(dev
), link_net
)) {
1361 int id
= peernet2id_alloc(dev_net(dev
), link_net
);
1363 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1364 goto nla_put_failure
;
1368 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1369 goto nla_put_failure
;
1371 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1372 if (af_ops
->fill_link_af
) {
1376 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1377 goto nla_put_failure
;
1379 err
= af_ops
->fill_link_af(skb
, dev
, ext_filter_mask
);
1382 * Caller may return ENODATA to indicate that there
1383 * was no data to be dumped. This is not an error, it
1384 * means we should trim the attribute header and
1387 if (err
== -ENODATA
)
1388 nla_nest_cancel(skb
, af
);
1390 goto nla_put_failure
;
1392 nla_nest_end(skb
, af
);
1396 nla_nest_end(skb
, af_spec
);
1398 nlmsg_end(skb
, nlh
);
1402 nlmsg_cancel(skb
, nlh
);
1406 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1407 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1408 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1409 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1410 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1411 [IFLA_MTU
] = { .type
= NLA_U32
},
1412 [IFLA_LINK
] = { .type
= NLA_U32
},
1413 [IFLA_MASTER
] = { .type
= NLA_U32
},
1414 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1415 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1416 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1417 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1418 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1419 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1420 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1421 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1422 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1423 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1424 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1425 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1426 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1427 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1428 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1429 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1430 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1431 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1432 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1433 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1434 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1435 [IFLA_PROTO_DOWN
] = { .type
= NLA_U8
},
1436 [IFLA_XDP
] = { .type
= NLA_NESTED
},
1439 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1440 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1441 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1442 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1443 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1446 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1447 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1448 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1449 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1450 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1451 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1452 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1453 [IFLA_VF_RSS_QUERY_EN
] = { .len
= sizeof(struct ifla_vf_rss_query_en
) },
1454 [IFLA_VF_STATS
] = { .type
= NLA_NESTED
},
1455 [IFLA_VF_TRUST
] = { .len
= sizeof(struct ifla_vf_trust
) },
1456 [IFLA_VF_IB_NODE_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1457 [IFLA_VF_IB_PORT_GUID
] = { .len
= sizeof(struct ifla_vf_guid
) },
1460 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1461 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1462 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1463 .len
= PORT_PROFILE_MAX
},
1464 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1465 .len
= sizeof(struct ifla_port_vsi
)},
1466 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1467 .len
= PORT_UUID_MAX
},
1468 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1469 .len
= PORT_UUID_MAX
},
1470 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1471 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1474 static const struct nla_policy ifla_xdp_policy
[IFLA_XDP_MAX
+ 1] = {
1475 [IFLA_XDP_FD
] = { .type
= NLA_S32
},
1476 [IFLA_XDP_ATTACHED
] = { .type
= NLA_U8
},
1479 static const struct rtnl_link_ops
*linkinfo_to_kind_ops(const struct nlattr
*nla
)
1481 const struct rtnl_link_ops
*ops
= NULL
;
1482 struct nlattr
*linfo
[IFLA_INFO_MAX
+ 1];
1484 if (nla_parse_nested(linfo
, IFLA_INFO_MAX
, nla
, ifla_info_policy
) < 0)
1487 if (linfo
[IFLA_INFO_KIND
]) {
1488 char kind
[MODULE_NAME_LEN
];
1490 nla_strlcpy(kind
, linfo
[IFLA_INFO_KIND
], sizeof(kind
));
1491 ops
= rtnl_link_ops_get(kind
);
1497 static bool link_master_filtered(struct net_device
*dev
, int master_idx
)
1499 struct net_device
*master
;
1504 master
= netdev_master_upper_dev_get(dev
);
1505 if (!master
|| master
->ifindex
!= master_idx
)
1511 static bool link_kind_filtered(const struct net_device
*dev
,
1512 const struct rtnl_link_ops
*kind_ops
)
1514 if (kind_ops
&& dev
->rtnl_link_ops
!= kind_ops
)
1520 static bool link_dump_filtered(struct net_device
*dev
,
1522 const struct rtnl_link_ops
*kind_ops
)
1524 if (link_master_filtered(dev
, master_idx
) ||
1525 link_kind_filtered(dev
, kind_ops
))
1531 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1533 struct net
*net
= sock_net(skb
->sk
);
1536 struct net_device
*dev
;
1537 struct hlist_head
*head
;
1538 struct nlattr
*tb
[IFLA_MAX
+1];
1539 u32 ext_filter_mask
= 0;
1540 const struct rtnl_link_ops
*kind_ops
= NULL
;
1541 unsigned int flags
= NLM_F_MULTI
;
1547 s_idx
= cb
->args
[1];
1549 cb
->seq
= net
->dev_base_seq
;
1551 /* A hack to preserve kernel<->userspace interface.
1552 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1553 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1554 * what iproute2 < v3.9.0 used.
1555 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1556 * attribute, its netlink message is shorter than struct ifinfomsg.
1558 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1559 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1561 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
1563 if (tb
[IFLA_EXT_MASK
])
1564 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1566 if (tb
[IFLA_MASTER
])
1567 master_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
1569 if (tb
[IFLA_LINKINFO
])
1570 kind_ops
= linkinfo_to_kind_ops(tb
[IFLA_LINKINFO
]);
1572 if (master_idx
|| kind_ops
)
1573 flags
|= NLM_F_DUMP_FILTERED
;
1576 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1578 head
= &net
->dev_index_head
[h
];
1579 hlist_for_each_entry(dev
, head
, index_hlist
) {
1580 if (link_dump_filtered(dev
, master_idx
, kind_ops
))
1584 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1585 NETLINK_CB(cb
->skb
).portid
,
1586 cb
->nlh
->nlmsg_seq
, 0,
1589 /* If we ran out of room on the first message,
1592 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
1597 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1609 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
)
1611 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
);
1613 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1615 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1618 /* Examine the link attributes and figure out which
1619 * network namespace we are talking about.
1621 if (tb
[IFLA_NET_NS_PID
])
1622 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1623 else if (tb
[IFLA_NET_NS_FD
])
1624 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1626 net
= get_net(src_net
);
1629 EXPORT_SYMBOL(rtnl_link_get_net
);
1631 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1634 if (tb
[IFLA_ADDRESS
] &&
1635 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1638 if (tb
[IFLA_BROADCAST
] &&
1639 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1643 if (tb
[IFLA_AF_SPEC
]) {
1647 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1648 const struct rtnl_af_ops
*af_ops
;
1650 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1651 return -EAFNOSUPPORT
;
1653 if (!af_ops
->set_link_af
)
1656 if (af_ops
->validate_link_af
) {
1657 err
= af_ops
->validate_link_af(dev
, af
);
1667 static int handle_infiniband_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
,
1670 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1672 return ops
->ndo_set_vf_guid(dev
, ivt
->vf
, ivt
->guid
, guid_type
);
1675 static int handle_vf_guid(struct net_device
*dev
, struct ifla_vf_guid
*ivt
, int guid_type
)
1677 if (dev
->type
!= ARPHRD_INFINIBAND
)
1680 return handle_infiniband_guid(dev
, ivt
, guid_type
);
1683 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
**tb
)
1685 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1688 if (tb
[IFLA_VF_MAC
]) {
1689 struct ifla_vf_mac
*ivm
= nla_data(tb
[IFLA_VF_MAC
]);
1692 if (ops
->ndo_set_vf_mac
)
1693 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1699 if (tb
[IFLA_VF_VLAN
]) {
1700 struct ifla_vf_vlan
*ivv
= nla_data(tb
[IFLA_VF_VLAN
]);
1703 if (ops
->ndo_set_vf_vlan
)
1704 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
, ivv
->vlan
,
1710 if (tb
[IFLA_VF_TX_RATE
]) {
1711 struct ifla_vf_tx_rate
*ivt
= nla_data(tb
[IFLA_VF_TX_RATE
]);
1712 struct ifla_vf_info ivf
;
1715 if (ops
->ndo_get_vf_config
)
1716 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
, &ivf
);
1721 if (ops
->ndo_set_vf_rate
)
1722 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1729 if (tb
[IFLA_VF_RATE
]) {
1730 struct ifla_vf_rate
*ivt
= nla_data(tb
[IFLA_VF_RATE
]);
1733 if (ops
->ndo_set_vf_rate
)
1734 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1741 if (tb
[IFLA_VF_SPOOFCHK
]) {
1742 struct ifla_vf_spoofchk
*ivs
= nla_data(tb
[IFLA_VF_SPOOFCHK
]);
1745 if (ops
->ndo_set_vf_spoofchk
)
1746 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1752 if (tb
[IFLA_VF_LINK_STATE
]) {
1753 struct ifla_vf_link_state
*ivl
= nla_data(tb
[IFLA_VF_LINK_STATE
]);
1756 if (ops
->ndo_set_vf_link_state
)
1757 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1763 if (tb
[IFLA_VF_RSS_QUERY_EN
]) {
1764 struct ifla_vf_rss_query_en
*ivrssq_en
;
1767 ivrssq_en
= nla_data(tb
[IFLA_VF_RSS_QUERY_EN
]);
1768 if (ops
->ndo_set_vf_rss_query_en
)
1769 err
= ops
->ndo_set_vf_rss_query_en(dev
, ivrssq_en
->vf
,
1770 ivrssq_en
->setting
);
1775 if (tb
[IFLA_VF_TRUST
]) {
1776 struct ifla_vf_trust
*ivt
= nla_data(tb
[IFLA_VF_TRUST
]);
1779 if (ops
->ndo_set_vf_trust
)
1780 err
= ops
->ndo_set_vf_trust(dev
, ivt
->vf
, ivt
->setting
);
1785 if (tb
[IFLA_VF_IB_NODE_GUID
]) {
1786 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_NODE_GUID
]);
1788 if (!ops
->ndo_set_vf_guid
)
1791 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_NODE_GUID
);
1794 if (tb
[IFLA_VF_IB_PORT_GUID
]) {
1795 struct ifla_vf_guid
*ivt
= nla_data(tb
[IFLA_VF_IB_PORT_GUID
]);
1797 if (!ops
->ndo_set_vf_guid
)
1800 return handle_vf_guid(dev
, ivt
, IFLA_VF_IB_PORT_GUID
);
1806 static int do_set_master(struct net_device
*dev
, int ifindex
)
1808 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1809 const struct net_device_ops
*ops
;
1813 if (upper_dev
->ifindex
== ifindex
)
1815 ops
= upper_dev
->netdev_ops
;
1816 if (ops
->ndo_del_slave
) {
1817 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1826 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1829 ops
= upper_dev
->netdev_ops
;
1830 if (ops
->ndo_add_slave
) {
1831 err
= ops
->ndo_add_slave(upper_dev
, dev
);
1841 #define DO_SETLINK_MODIFIED 0x01
1842 /* notify flag means notify + modified. */
1843 #define DO_SETLINK_NOTIFY 0x03
1844 static int do_setlink(const struct sk_buff
*skb
,
1845 struct net_device
*dev
, struct ifinfomsg
*ifm
,
1846 struct nlattr
**tb
, char *ifname
, int status
)
1848 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1851 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1852 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1857 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1862 err
= dev_change_net_namespace(dev
, net
, ifname
);
1866 status
|= DO_SETLINK_MODIFIED
;
1870 struct rtnl_link_ifmap
*u_map
;
1873 if (!ops
->ndo_set_config
) {
1878 if (!netif_device_present(dev
)) {
1883 u_map
= nla_data(tb
[IFLA_MAP
]);
1884 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1885 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1886 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1887 k_map
.irq
= (unsigned char) u_map
->irq
;
1888 k_map
.dma
= (unsigned char) u_map
->dma
;
1889 k_map
.port
= (unsigned char) u_map
->port
;
1891 err
= ops
->ndo_set_config(dev
, &k_map
);
1895 status
|= DO_SETLINK_NOTIFY
;
1898 if (tb
[IFLA_ADDRESS
]) {
1899 struct sockaddr
*sa
;
1902 len
= sizeof(sa_family_t
) + dev
->addr_len
;
1903 sa
= kmalloc(len
, GFP_KERNEL
);
1908 sa
->sa_family
= dev
->type
;
1909 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1911 err
= dev_set_mac_address(dev
, sa
);
1915 status
|= DO_SETLINK_MODIFIED
;
1919 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1922 status
|= DO_SETLINK_MODIFIED
;
1925 if (tb
[IFLA_GROUP
]) {
1926 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1927 status
|= DO_SETLINK_NOTIFY
;
1931 * Interface selected by interface index but interface
1932 * name provided implies that a name change has been
1935 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1936 err
= dev_change_name(dev
, ifname
);
1939 status
|= DO_SETLINK_MODIFIED
;
1942 if (tb
[IFLA_IFALIAS
]) {
1943 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
1944 nla_len(tb
[IFLA_IFALIAS
]));
1947 status
|= DO_SETLINK_NOTIFY
;
1950 if (tb
[IFLA_BROADCAST
]) {
1951 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
1952 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
1955 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
1956 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1961 if (tb
[IFLA_MASTER
]) {
1962 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1965 status
|= DO_SETLINK_MODIFIED
;
1968 if (tb
[IFLA_CARRIER
]) {
1969 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
1972 status
|= DO_SETLINK_MODIFIED
;
1975 if (tb
[IFLA_TXQLEN
]) {
1976 unsigned long value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1977 unsigned long orig_len
= dev
->tx_queue_len
;
1979 if (dev
->tx_queue_len
^ value
) {
1980 dev
->tx_queue_len
= value
;
1981 err
= call_netdevice_notifiers(
1982 NETDEV_CHANGE_TX_QUEUE_LEN
, dev
);
1983 err
= notifier_to_errno(err
);
1985 dev
->tx_queue_len
= orig_len
;
1988 status
|= DO_SETLINK_NOTIFY
;
1992 if (tb
[IFLA_OPERSTATE
])
1993 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1995 if (tb
[IFLA_LINKMODE
]) {
1996 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1998 write_lock_bh(&dev_base_lock
);
1999 if (dev
->link_mode
^ value
)
2000 status
|= DO_SETLINK_NOTIFY
;
2001 dev
->link_mode
= value
;
2002 write_unlock_bh(&dev_base_lock
);
2005 if (tb
[IFLA_VFINFO_LIST
]) {
2006 struct nlattr
*vfinfo
[IFLA_VF_MAX
+ 1];
2007 struct nlattr
*attr
;
2010 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
2011 if (nla_type(attr
) != IFLA_VF_INFO
||
2012 nla_len(attr
) < NLA_HDRLEN
) {
2016 err
= nla_parse_nested(vfinfo
, IFLA_VF_MAX
, attr
,
2020 err
= do_setvfinfo(dev
, vfinfo
);
2023 status
|= DO_SETLINK_NOTIFY
;
2028 if (tb
[IFLA_VF_PORTS
]) {
2029 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2030 struct nlattr
*attr
;
2035 if (!ops
->ndo_set_vf_port
)
2038 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
2039 if (nla_type(attr
) != IFLA_VF_PORT
||
2040 nla_len(attr
) < NLA_HDRLEN
) {
2044 err
= nla_parse_nested(port
, IFLA_PORT_MAX
, attr
,
2048 if (!port
[IFLA_PORT_VF
]) {
2052 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
2053 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
2056 status
|= DO_SETLINK_NOTIFY
;
2061 if (tb
[IFLA_PORT_SELF
]) {
2062 struct nlattr
*port
[IFLA_PORT_MAX
+1];
2064 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
2065 tb
[IFLA_PORT_SELF
], ifla_port_policy
);
2070 if (ops
->ndo_set_vf_port
)
2071 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
2074 status
|= DO_SETLINK_NOTIFY
;
2077 if (tb
[IFLA_AF_SPEC
]) {
2081 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
2082 const struct rtnl_af_ops
*af_ops
;
2084 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
2087 err
= af_ops
->set_link_af(dev
, af
);
2091 status
|= DO_SETLINK_NOTIFY
;
2096 if (tb
[IFLA_PROTO_DOWN
]) {
2097 err
= dev_change_proto_down(dev
,
2098 nla_get_u8(tb
[IFLA_PROTO_DOWN
]));
2101 status
|= DO_SETLINK_NOTIFY
;
2105 struct nlattr
*xdp
[IFLA_XDP_MAX
+ 1];
2107 err
= nla_parse_nested(xdp
, IFLA_XDP_MAX
, tb
[IFLA_XDP
],
2112 if (xdp
[IFLA_XDP_ATTACHED
]) {
2116 if (xdp
[IFLA_XDP_FD
]) {
2117 err
= dev_change_xdp_fd(dev
,
2118 nla_get_s32(xdp
[IFLA_XDP_FD
]));
2121 status
|= DO_SETLINK_NOTIFY
;
2126 if (status
& DO_SETLINK_MODIFIED
) {
2127 if (status
& DO_SETLINK_NOTIFY
)
2128 netdev_state_change(dev
);
2131 net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
2138 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2140 struct net
*net
= sock_net(skb
->sk
);
2141 struct ifinfomsg
*ifm
;
2142 struct net_device
*dev
;
2144 struct nlattr
*tb
[IFLA_MAX
+1];
2145 char ifname
[IFNAMSIZ
];
2147 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2151 if (tb
[IFLA_IFNAME
])
2152 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2157 ifm
= nlmsg_data(nlh
);
2158 if (ifm
->ifi_index
> 0)
2159 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2160 else if (tb
[IFLA_IFNAME
])
2161 dev
= __dev_get_by_name(net
, ifname
);
2170 err
= validate_linkmsg(dev
, tb
);
2174 err
= do_setlink(skb
, dev
, ifm
, tb
, ifname
, 0);
2179 static int rtnl_group_dellink(const struct net
*net
, int group
)
2181 struct net_device
*dev
, *aux
;
2182 LIST_HEAD(list_kill
);
2188 for_each_netdev(net
, dev
) {
2189 if (dev
->group
== group
) {
2190 const struct rtnl_link_ops
*ops
;
2193 ops
= dev
->rtnl_link_ops
;
2194 if (!ops
|| !ops
->dellink
)
2202 for_each_netdev_safe(net
, dev
, aux
) {
2203 if (dev
->group
== group
) {
2204 const struct rtnl_link_ops
*ops
;
2206 ops
= dev
->rtnl_link_ops
;
2207 ops
->dellink(dev
, &list_kill
);
2210 unregister_netdevice_many(&list_kill
);
2215 int rtnl_delete_link(struct net_device
*dev
)
2217 const struct rtnl_link_ops
*ops
;
2218 LIST_HEAD(list_kill
);
2220 ops
= dev
->rtnl_link_ops
;
2221 if (!ops
|| !ops
->dellink
)
2224 ops
->dellink(dev
, &list_kill
);
2225 unregister_netdevice_many(&list_kill
);
2229 EXPORT_SYMBOL_GPL(rtnl_delete_link
);
2231 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2233 struct net
*net
= sock_net(skb
->sk
);
2234 struct net_device
*dev
;
2235 struct ifinfomsg
*ifm
;
2236 char ifname
[IFNAMSIZ
];
2237 struct nlattr
*tb
[IFLA_MAX
+1];
2240 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2244 if (tb
[IFLA_IFNAME
])
2245 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2247 ifm
= nlmsg_data(nlh
);
2248 if (ifm
->ifi_index
> 0)
2249 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2250 else if (tb
[IFLA_IFNAME
])
2251 dev
= __dev_get_by_name(net
, ifname
);
2252 else if (tb
[IFLA_GROUP
])
2253 return rtnl_group_dellink(net
, nla_get_u32(tb
[IFLA_GROUP
]));
2260 return rtnl_delete_link(dev
);
2263 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
2265 unsigned int old_flags
;
2268 old_flags
= dev
->flags
;
2269 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
2270 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
2275 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
2277 __dev_notify_flags(dev
, old_flags
, ~0U);
2280 EXPORT_SYMBOL(rtnl_configure_link
);
2282 struct net_device
*rtnl_create_link(struct net
*net
,
2283 const char *ifname
, unsigned char name_assign_type
,
2284 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
2287 struct net_device
*dev
;
2288 unsigned int num_tx_queues
= 1;
2289 unsigned int num_rx_queues
= 1;
2291 if (tb
[IFLA_NUM_TX_QUEUES
])
2292 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
2293 else if (ops
->get_num_tx_queues
)
2294 num_tx_queues
= ops
->get_num_tx_queues();
2296 if (tb
[IFLA_NUM_RX_QUEUES
])
2297 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
2298 else if (ops
->get_num_rx_queues
)
2299 num_rx_queues
= ops
->get_num_rx_queues();
2302 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
2303 ops
->setup
, num_tx_queues
, num_rx_queues
);
2307 dev_net_set(dev
, net
);
2308 dev
->rtnl_link_ops
= ops
;
2309 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
2312 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
2313 if (tb
[IFLA_ADDRESS
]) {
2314 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
2315 nla_len(tb
[IFLA_ADDRESS
]));
2316 dev
->addr_assign_type
= NET_ADDR_SET
;
2318 if (tb
[IFLA_BROADCAST
])
2319 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
2320 nla_len(tb
[IFLA_BROADCAST
]));
2321 if (tb
[IFLA_TXQLEN
])
2322 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
2323 if (tb
[IFLA_OPERSTATE
])
2324 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
2325 if (tb
[IFLA_LINKMODE
])
2326 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
2328 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
2333 return ERR_PTR(err
);
2335 EXPORT_SYMBOL(rtnl_create_link
);
2337 static int rtnl_group_changelink(const struct sk_buff
*skb
,
2338 struct net
*net
, int group
,
2339 struct ifinfomsg
*ifm
,
2342 struct net_device
*dev
, *aux
;
2345 for_each_netdev_safe(net
, dev
, aux
) {
2346 if (dev
->group
== group
) {
2347 err
= do_setlink(skb
, dev
, ifm
, tb
, NULL
, 0);
2356 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2358 struct net
*net
= sock_net(skb
->sk
);
2359 const struct rtnl_link_ops
*ops
;
2360 const struct rtnl_link_ops
*m_ops
= NULL
;
2361 struct net_device
*dev
;
2362 struct net_device
*master_dev
= NULL
;
2363 struct ifinfomsg
*ifm
;
2364 char kind
[MODULE_NAME_LEN
];
2365 char ifname
[IFNAMSIZ
];
2366 struct nlattr
*tb
[IFLA_MAX
+1];
2367 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
2368 unsigned char name_assign_type
= NET_NAME_USER
;
2371 #ifdef CONFIG_MODULES
2374 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2378 if (tb
[IFLA_IFNAME
])
2379 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2383 ifm
= nlmsg_data(nlh
);
2384 if (ifm
->ifi_index
> 0)
2385 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2388 dev
= __dev_get_by_name(net
, ifname
);
2394 master_dev
= netdev_master_upper_dev_get(dev
);
2396 m_ops
= master_dev
->rtnl_link_ops
;
2399 err
= validate_linkmsg(dev
, tb
);
2403 if (tb
[IFLA_LINKINFO
]) {
2404 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
2405 tb
[IFLA_LINKINFO
], ifla_info_policy
);
2409 memset(linkinfo
, 0, sizeof(linkinfo
));
2411 if (linkinfo
[IFLA_INFO_KIND
]) {
2412 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2413 ops
= rtnl_link_ops_get(kind
);
2420 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2421 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2422 struct nlattr
**data
= NULL
;
2423 struct nlattr
**slave_data
= NULL
;
2424 struct net
*dest_net
, *link_net
= NULL
;
2427 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2428 err
= nla_parse_nested(attr
, ops
->maxtype
,
2429 linkinfo
[IFLA_INFO_DATA
],
2435 if (ops
->validate
) {
2436 err
= ops
->validate(tb
, data
);
2443 if (m_ops
->slave_maxtype
&&
2444 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2445 err
= nla_parse_nested(slave_attr
,
2446 m_ops
->slave_maxtype
,
2447 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2448 m_ops
->slave_policy
);
2451 slave_data
= slave_attr
;
2453 if (m_ops
->slave_validate
) {
2454 err
= m_ops
->slave_validate(tb
, slave_data
);
2463 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2465 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2468 if (linkinfo
[IFLA_INFO_DATA
]) {
2469 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2473 err
= ops
->changelink(dev
, tb
, data
);
2476 status
|= DO_SETLINK_NOTIFY
;
2479 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2480 if (!m_ops
|| !m_ops
->slave_changelink
)
2483 err
= m_ops
->slave_changelink(master_dev
, dev
,
2487 status
|= DO_SETLINK_NOTIFY
;
2490 return do_setlink(skb
, dev
, ifm
, tb
, ifname
, status
);
2493 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2494 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2495 return rtnl_group_changelink(skb
, net
,
2496 nla_get_u32(tb
[IFLA_GROUP
]),
2501 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
2505 #ifdef CONFIG_MODULES
2508 request_module("rtnl-link-%s", kind
);
2510 ops
= rtnl_link_ops_get(kind
);
2522 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2523 name_assign_type
= NET_NAME_ENUM
;
2526 dest_net
= rtnl_link_get_net(net
, tb
);
2527 if (IS_ERR(dest_net
))
2528 return PTR_ERR(dest_net
);
2531 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2534 if (tb
[IFLA_LINK_NETNSID
]) {
2535 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2537 link_net
= get_net_ns_by_id(dest_net
, id
);
2543 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2547 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2548 name_assign_type
, ops
, tb
);
2554 dev
->ifindex
= ifm
->ifi_index
;
2557 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
);
2558 /* Drivers should call free_netdev() in ->destructor
2559 * and unregister it on failure after registration
2560 * so that device could be finally freed in rtnl_unlock.
2563 /* If device is not registered at all, free it now */
2564 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2569 err
= register_netdevice(dev
);
2575 err
= rtnl_configure_link(dev
, ifm
);
2577 goto out_unregister
;
2579 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2581 goto out_unregister
;
2590 LIST_HEAD(list_kill
);
2592 ops
->dellink(dev
, &list_kill
);
2593 unregister_netdevice_many(&list_kill
);
2595 unregister_netdevice(dev
);
2601 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
)
2603 struct net
*net
= sock_net(skb
->sk
);
2604 struct ifinfomsg
*ifm
;
2605 char ifname
[IFNAMSIZ
];
2606 struct nlattr
*tb
[IFLA_MAX
+1];
2607 struct net_device
*dev
= NULL
;
2608 struct sk_buff
*nskb
;
2610 u32 ext_filter_mask
= 0;
2612 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2616 if (tb
[IFLA_IFNAME
])
2617 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2619 if (tb
[IFLA_EXT_MASK
])
2620 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2622 ifm
= nlmsg_data(nlh
);
2623 if (ifm
->ifi_index
> 0)
2624 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2625 else if (tb
[IFLA_IFNAME
])
2626 dev
= __dev_get_by_name(net
, ifname
);
2633 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2637 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2638 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
2640 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2641 WARN_ON(err
== -EMSGSIZE
);
2644 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2649 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2651 struct net
*net
= sock_net(skb
->sk
);
2652 struct net_device
*dev
;
2653 struct nlattr
*tb
[IFLA_MAX
+1];
2654 u32 ext_filter_mask
= 0;
2655 u16 min_ifinfo_dump_size
= 0;
2658 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2659 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2660 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2662 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
2663 if (tb
[IFLA_EXT_MASK
])
2664 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2667 if (!ext_filter_mask
)
2668 return NLMSG_GOODSIZE
;
2670 * traverse the list of net devices and compute the minimum
2671 * buffer size based upon the filter mask.
2673 list_for_each_entry(dev
, &net
->dev_base_head
, dev_list
) {
2674 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2679 return min_ifinfo_dump_size
;
2682 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2685 int s_idx
= cb
->family
;
2689 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2690 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2691 if (idx
< s_idx
|| idx
== PF_PACKET
)
2693 if (rtnl_msg_handlers
[idx
] == NULL
||
2694 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
2697 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2701 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
2709 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
2710 unsigned int change
, gfp_t flags
)
2712 struct net
*net
= dev_net(dev
);
2713 struct sk_buff
*skb
;
2715 size_t if_info_size
;
2717 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2721 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
2723 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2724 WARN_ON(err
== -EMSGSIZE
);
2731 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2735 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
2737 struct net
*net
= dev_net(dev
);
2739 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
2742 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
2745 struct sk_buff
*skb
;
2747 if (dev
->reg_state
!= NETREG_REGISTERED
)
2750 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, flags
);
2752 rtmsg_ifinfo_send(skb
, dev
, flags
);
2754 EXPORT_SYMBOL(rtmsg_ifinfo
);
2756 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
2757 struct net_device
*dev
,
2758 u8
*addr
, u16 vid
, u32 pid
, u32 seq
,
2759 int type
, unsigned int flags
,
2760 int nlflags
, u16 ndm_state
)
2762 struct nlmsghdr
*nlh
;
2765 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2769 ndm
= nlmsg_data(nlh
);
2770 ndm
->ndm_family
= AF_BRIDGE
;
2773 ndm
->ndm_flags
= flags
;
2775 ndm
->ndm_ifindex
= dev
->ifindex
;
2776 ndm
->ndm_state
= ndm_state
;
2778 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
2779 goto nla_put_failure
;
2781 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &vid
))
2782 goto nla_put_failure
;
2784 nlmsg_end(skb
, nlh
);
2788 nlmsg_cancel(skb
, nlh
);
2792 static inline size_t rtnl_fdb_nlmsg_size(void)
2794 return NLMSG_ALIGN(sizeof(struct ndmsg
)) + nla_total_size(ETH_ALEN
);
2797 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, u16 vid
, int type
,
2800 struct net
*net
= dev_net(dev
);
2801 struct sk_buff
*skb
;
2804 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2808 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, vid
,
2809 0, 0, type
, NTF_SELF
, 0, ndm_state
);
2815 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
2818 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
2822 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2824 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
2825 struct nlattr
*tb
[],
2826 struct net_device
*dev
,
2827 const unsigned char *addr
, u16 vid
,
2832 /* If aging addresses are supported device will need to
2833 * implement its own handler for this.
2835 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
2836 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2841 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
2845 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2846 err
= dev_uc_add_excl(dev
, addr
);
2847 else if (is_multicast_ether_addr(addr
))
2848 err
= dev_mc_add_excl(dev
, addr
);
2850 /* Only return duplicate errors if NLM_F_EXCL is set */
2851 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
2856 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
2858 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
2863 if (nla_len(vlan_attr
) != sizeof(u16
)) {
2864 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2868 vid
= nla_get_u16(vlan_attr
);
2870 if (!vid
|| vid
>= VLAN_VID_MASK
) {
2871 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2880 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2882 struct net
*net
= sock_net(skb
->sk
);
2884 struct nlattr
*tb
[NDA_MAX
+1];
2885 struct net_device
*dev
;
2890 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2894 ndm
= nlmsg_data(nlh
);
2895 if (ndm
->ndm_ifindex
== 0) {
2896 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2900 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2902 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2906 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2907 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2911 addr
= nla_data(tb
[NDA_LLADDR
]);
2913 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2919 /* Support fdb on master device the net/bridge default case */
2920 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2921 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2922 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2923 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2925 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2930 ndm
->ndm_flags
&= ~NTF_MASTER
;
2933 /* Embedded bridge, macvlan, and any other device support */
2934 if ((ndm
->ndm_flags
& NTF_SELF
)) {
2935 if (dev
->netdev_ops
->ndo_fdb_add
)
2936 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
2940 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2944 rtnl_fdb_notify(dev
, addr
, vid
, RTM_NEWNEIGH
,
2946 ndm
->ndm_flags
&= ~NTF_SELF
;
2954 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2956 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
2957 struct nlattr
*tb
[],
2958 struct net_device
*dev
,
2959 const unsigned char *addr
, u16 vid
)
2963 /* If aging addresses are supported device will need to
2964 * implement its own handler for this.
2966 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
2967 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2971 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2972 err
= dev_uc_del(dev
, addr
);
2973 else if (is_multicast_ether_addr(addr
))
2974 err
= dev_mc_del(dev
, addr
);
2978 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
2980 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2982 struct net
*net
= sock_net(skb
->sk
);
2984 struct nlattr
*tb
[NDA_MAX
+1];
2985 struct net_device
*dev
;
2990 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
2993 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2997 ndm
= nlmsg_data(nlh
);
2998 if (ndm
->ndm_ifindex
== 0) {
2999 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3003 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
3005 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3009 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3010 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
3014 addr
= nla_data(tb
[NDA_LLADDR
]);
3016 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
3022 /* Support fdb on master device the net/bridge default case */
3023 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
3024 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
3025 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3026 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
3028 if (ops
->ndo_fdb_del
)
3029 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3034 ndm
->ndm_flags
&= ~NTF_MASTER
;
3037 /* Embedded bridge, macvlan, and any other device support */
3038 if (ndm
->ndm_flags
& NTF_SELF
) {
3039 if (dev
->netdev_ops
->ndo_fdb_del
)
3040 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
3043 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
3046 rtnl_fdb_notify(dev
, addr
, vid
, RTM_DELNEIGH
,
3048 ndm
->ndm_flags
&= ~NTF_SELF
;
3055 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
3056 struct netlink_callback
*cb
,
3057 struct net_device
*dev
,
3059 struct netdev_hw_addr_list
*list
)
3061 struct netdev_hw_addr
*ha
;
3065 portid
= NETLINK_CB(cb
->skb
).portid
;
3066 seq
= cb
->nlh
->nlmsg_seq
;
3068 list_for_each_entry(ha
, &list
->list
, list
) {
3069 if (*idx
< cb
->args
[0])
3072 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
, 0,
3074 RTM_NEWNEIGH
, NTF_SELF
,
3075 NLM_F_MULTI
, NUD_PERMANENT
);
3085 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3086 * @nlh: netlink message header
3089 * Default netdevice operation to dump the existing unicast address list.
3090 * Returns number of addresses from list put in skb.
3092 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
3093 struct netlink_callback
*cb
,
3094 struct net_device
*dev
,
3095 struct net_device
*filter_dev
,
3100 netif_addr_lock_bh(dev
);
3101 err
= nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->uc
);
3104 nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->mc
);
3106 netif_addr_unlock_bh(dev
);
3110 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
3112 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3114 struct net_device
*dev
;
3115 struct nlattr
*tb
[IFLA_MAX
+1];
3116 struct net_device
*br_dev
= NULL
;
3117 const struct net_device_ops
*ops
= NULL
;
3118 const struct net_device_ops
*cops
= NULL
;
3119 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
3120 struct net
*net
= sock_net(skb
->sk
);
3125 if (nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
, IFLA_MAX
,
3126 ifla_policy
) == 0) {
3127 if (tb
[IFLA_MASTER
])
3128 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
3131 brport_idx
= ifm
->ifi_index
;
3134 br_dev
= __dev_get_by_index(net
, br_idx
);
3138 ops
= br_dev
->netdev_ops
;
3142 for_each_netdev(net
, dev
) {
3143 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
3146 if (!br_idx
) { /* user did not specify a specific bridge */
3147 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3148 br_dev
= netdev_master_upper_dev_get(dev
);
3149 cops
= br_dev
->netdev_ops
;
3153 if (dev
!= br_dev
&&
3154 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
3157 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
3158 !(dev
->priv_flags
& IFF_EBRIDGE
))
3164 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
3165 if (cops
&& cops
->ndo_fdb_dump
)
3166 idx
= cops
->ndo_fdb_dump(skb
, cb
, br_dev
, dev
,
3169 if (cb
->args
[1] == -EMSGSIZE
)
3172 if (dev
->netdev_ops
->ndo_fdb_dump
)
3173 idx
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
, dev
, NULL
,
3176 idx
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
3177 if (cb
->args
[1] == -EMSGSIZE
)
3187 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
3188 unsigned int attrnum
, unsigned int flag
)
3191 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
3195 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
3196 struct net_device
*dev
, u16 mode
,
3197 u32 flags
, u32 mask
, int nlflags
,
3199 int (*vlan_fill
)(struct sk_buff
*skb
,
3200 struct net_device
*dev
,
3203 struct nlmsghdr
*nlh
;
3204 struct ifinfomsg
*ifm
;
3205 struct nlattr
*br_afspec
;
3206 struct nlattr
*protinfo
;
3207 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
3208 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3211 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), nlflags
);
3215 ifm
= nlmsg_data(nlh
);
3216 ifm
->ifi_family
= AF_BRIDGE
;
3218 ifm
->ifi_type
= dev
->type
;
3219 ifm
->ifi_index
= dev
->ifindex
;
3220 ifm
->ifi_flags
= dev_get_flags(dev
);
3221 ifm
->ifi_change
= 0;
3224 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
3225 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
3226 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
3228 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
3230 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
3231 (dev
->ifindex
!= dev_get_iflink(dev
) &&
3232 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))))
3233 goto nla_put_failure
;
3235 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
3237 goto nla_put_failure
;
3239 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
3240 nla_nest_cancel(skb
, br_afspec
);
3241 goto nla_put_failure
;
3244 if (mode
!= BRIDGE_MODE_UNDEF
) {
3245 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
3246 nla_nest_cancel(skb
, br_afspec
);
3247 goto nla_put_failure
;
3251 err
= vlan_fill(skb
, dev
, filter_mask
);
3253 nla_nest_cancel(skb
, br_afspec
);
3254 goto nla_put_failure
;
3257 nla_nest_end(skb
, br_afspec
);
3259 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
3261 goto nla_put_failure
;
3263 if (brport_nla_put_flag(skb
, flags
, mask
,
3264 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
3265 brport_nla_put_flag(skb
, flags
, mask
,
3266 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
3267 brport_nla_put_flag(skb
, flags
, mask
,
3268 IFLA_BRPORT_FAST_LEAVE
,
3269 BR_MULTICAST_FAST_LEAVE
) ||
3270 brport_nla_put_flag(skb
, flags
, mask
,
3271 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
3272 brport_nla_put_flag(skb
, flags
, mask
,
3273 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
3274 brport_nla_put_flag(skb
, flags
, mask
,
3275 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
3276 brport_nla_put_flag(skb
, flags
, mask
,
3277 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
3278 brport_nla_put_flag(skb
, flags
, mask
,
3279 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
3280 nla_nest_cancel(skb
, protinfo
);
3281 goto nla_put_failure
;
3284 nla_nest_end(skb
, protinfo
);
3286 nlmsg_end(skb
, nlh
);
3289 nlmsg_cancel(skb
, nlh
);
3290 return err
? err
: -EMSGSIZE
;
3292 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink
);
3294 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3296 struct net
*net
= sock_net(skb
->sk
);
3297 struct net_device
*dev
;
3299 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
3300 u32 seq
= cb
->nlh
->nlmsg_seq
;
3301 u32 filter_mask
= 0;
3304 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
3305 struct nlattr
*extfilt
;
3307 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
3310 if (nla_len(extfilt
) < sizeof(filter_mask
))
3313 filter_mask
= nla_get_u32(extfilt
);
3318 for_each_netdev_rcu(net
, dev
) {
3319 const struct net_device_ops
*ops
= dev
->netdev_ops
;
3320 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3322 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
3323 if (idx
>= cb
->args
[0]) {
3324 err
= br_dev
->netdev_ops
->ndo_bridge_getlink(
3325 skb
, portid
, seq
, dev
,
3326 filter_mask
, NLM_F_MULTI
);
3327 if (err
< 0 && err
!= -EOPNOTSUPP
)
3333 if (ops
->ndo_bridge_getlink
) {
3334 if (idx
>= cb
->args
[0]) {
3335 err
= ops
->ndo_bridge_getlink(skb
, portid
,
3339 if (err
< 0 && err
!= -EOPNOTSUPP
)
3351 static inline size_t bridge_nlmsg_size(void)
3353 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3354 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3355 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3356 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
3357 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
3358 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
3359 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
3360 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
3361 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
3362 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
3363 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
3366 static int rtnl_bridge_notify(struct net_device
*dev
)
3368 struct net
*net
= dev_net(dev
);
3369 struct sk_buff
*skb
;
3370 int err
= -EOPNOTSUPP
;
3372 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
3375 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
3381 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0, 0);
3388 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
3391 WARN_ON(err
== -EMSGSIZE
);
3394 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
3398 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3400 struct net
*net
= sock_net(skb
->sk
);
3401 struct ifinfomsg
*ifm
;
3402 struct net_device
*dev
;
3403 struct nlattr
*br_spec
, *attr
= NULL
;
3404 int rem
, err
= -EOPNOTSUPP
;
3406 bool have_flags
= false;
3408 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3411 ifm
= nlmsg_data(nlh
);
3412 if (ifm
->ifi_family
!= AF_BRIDGE
)
3413 return -EPFNOSUPPORT
;
3415 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3417 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3421 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3423 nla_for_each_nested(attr
, br_spec
, rem
) {
3424 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3425 if (nla_len(attr
) < sizeof(flags
))
3429 flags
= nla_get_u16(attr
);
3435 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3436 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3438 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3443 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3447 flags
&= ~BRIDGE_FLAGS_MASTER
;
3450 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3451 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3454 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3457 flags
&= ~BRIDGE_FLAGS_SELF
;
3459 /* Generate event to notify upper layer of bridge
3462 err
= rtnl_bridge_notify(dev
);
3467 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3472 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3474 struct net
*net
= sock_net(skb
->sk
);
3475 struct ifinfomsg
*ifm
;
3476 struct net_device
*dev
;
3477 struct nlattr
*br_spec
, *attr
= NULL
;
3478 int rem
, err
= -EOPNOTSUPP
;
3480 bool have_flags
= false;
3482 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3485 ifm
= nlmsg_data(nlh
);
3486 if (ifm
->ifi_family
!= AF_BRIDGE
)
3487 return -EPFNOSUPPORT
;
3489 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3491 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3495 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3497 nla_for_each_nested(attr
, br_spec
, rem
) {
3498 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3499 if (nla_len(attr
) < sizeof(flags
))
3503 flags
= nla_get_u16(attr
);
3509 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3510 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3512 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3517 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3521 flags
&= ~BRIDGE_FLAGS_MASTER
;
3524 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3525 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3528 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3532 flags
&= ~BRIDGE_FLAGS_SELF
;
3534 /* Generate event to notify upper layer of bridge
3537 err
= rtnl_bridge_notify(dev
);
3542 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3547 static bool stats_attr_valid(unsigned int mask
, int attrid
, int idxattr
)
3549 return (mask
& IFLA_STATS_FILTER_BIT(attrid
)) &&
3550 (!idxattr
|| idxattr
== attrid
);
3553 static int rtnl_fill_statsinfo(struct sk_buff
*skb
, struct net_device
*dev
,
3554 int type
, u32 pid
, u32 seq
, u32 change
,
3555 unsigned int flags
, unsigned int filter_mask
,
3556 int *idxattr
, int *prividx
)
3558 struct if_stats_msg
*ifsm
;
3559 struct nlmsghdr
*nlh
;
3560 struct nlattr
*attr
;
3561 int s_prividx
= *prividx
;
3565 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifsm
), flags
);
3569 ifsm
= nlmsg_data(nlh
);
3570 ifsm
->ifindex
= dev
->ifindex
;
3571 ifsm
->filter_mask
= filter_mask
;
3573 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, *idxattr
)) {
3574 struct rtnl_link_stats64
*sp
;
3576 attr
= nla_reserve_64bit(skb
, IFLA_STATS_LINK_64
,
3577 sizeof(struct rtnl_link_stats64
),
3580 goto nla_put_failure
;
3582 sp
= nla_data(attr
);
3583 dev_get_stats(dev
, sp
);
3586 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, *idxattr
)) {
3587 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
3589 if (ops
&& ops
->fill_linkxstats
) {
3592 *idxattr
= IFLA_STATS_LINK_XSTATS
;
3593 attr
= nla_nest_start(skb
,
3594 IFLA_STATS_LINK_XSTATS
);
3596 goto nla_put_failure
;
3598 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
3599 nla_nest_end(skb
, attr
);
3601 goto nla_put_failure
;
3606 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
,
3608 const struct rtnl_link_ops
*ops
= NULL
;
3609 const struct net_device
*master
;
3611 master
= netdev_master_upper_dev_get(dev
);
3613 ops
= master
->rtnl_link_ops
;
3614 if (ops
&& ops
->fill_linkxstats
) {
3617 *idxattr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
3618 attr
= nla_nest_start(skb
,
3619 IFLA_STATS_LINK_XSTATS_SLAVE
);
3621 goto nla_put_failure
;
3623 err
= ops
->fill_linkxstats(skb
, dev
, prividx
, *idxattr
);
3624 nla_nest_end(skb
, attr
);
3626 goto nla_put_failure
;
3631 nlmsg_end(skb
, nlh
);
3636 /* not a multi message or no progress mean a real error */
3637 if (!(flags
& NLM_F_MULTI
) || s_prividx
== *prividx
)
3638 nlmsg_cancel(skb
, nlh
);
3640 nlmsg_end(skb
, nlh
);
3645 static const struct nla_policy ifla_stats_policy
[IFLA_STATS_MAX
+ 1] = {
3646 [IFLA_STATS_LINK_64
] = { .len
= sizeof(struct rtnl_link_stats64
) },
3649 static size_t if_nlmsg_stats_size(const struct net_device
*dev
,
3654 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_64
, 0))
3655 size
+= nla_total_size_64bit(sizeof(struct rtnl_link_stats64
));
3657 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS
, 0)) {
3658 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
3659 int attr
= IFLA_STATS_LINK_XSTATS
;
3661 if (ops
&& ops
->get_linkxstats_size
) {
3662 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
3664 /* for IFLA_STATS_LINK_XSTATS */
3665 size
+= nla_total_size(0);
3669 if (stats_attr_valid(filter_mask
, IFLA_STATS_LINK_XSTATS_SLAVE
, 0)) {
3670 struct net_device
*_dev
= (struct net_device
*)dev
;
3671 const struct rtnl_link_ops
*ops
= NULL
;
3672 const struct net_device
*master
;
3674 /* netdev_master_upper_dev_get can't take const */
3675 master
= netdev_master_upper_dev_get(_dev
);
3677 ops
= master
->rtnl_link_ops
;
3678 if (ops
&& ops
->get_linkxstats_size
) {
3679 int attr
= IFLA_STATS_LINK_XSTATS_SLAVE
;
3681 size
+= nla_total_size(ops
->get_linkxstats_size(dev
,
3683 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
3684 size
+= nla_total_size(0);
3691 static int rtnl_stats_get(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3693 struct net
*net
= sock_net(skb
->sk
);
3694 struct net_device
*dev
= NULL
;
3695 int idxattr
= 0, prividx
= 0;
3696 struct if_stats_msg
*ifsm
;
3697 struct sk_buff
*nskb
;
3701 ifsm
= nlmsg_data(nlh
);
3702 if (ifsm
->ifindex
> 0)
3703 dev
= __dev_get_by_index(net
, ifsm
->ifindex
);
3710 filter_mask
= ifsm
->filter_mask
;
3714 nskb
= nlmsg_new(if_nlmsg_stats_size(dev
, filter_mask
), GFP_KERNEL
);
3718 err
= rtnl_fill_statsinfo(nskb
, dev
, RTM_NEWSTATS
,
3719 NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
, 0,
3720 0, filter_mask
, &idxattr
, &prividx
);
3722 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3723 WARN_ON(err
== -EMSGSIZE
);
3726 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
3732 static int rtnl_stats_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3734 int h
, s_h
, err
, s_idx
, s_idxattr
, s_prividx
;
3735 struct net
*net
= sock_net(skb
->sk
);
3736 unsigned int flags
= NLM_F_MULTI
;
3737 struct if_stats_msg
*ifsm
;
3738 struct hlist_head
*head
;
3739 struct net_device
*dev
;
3740 u32 filter_mask
= 0;
3744 s_idx
= cb
->args
[1];
3745 s_idxattr
= cb
->args
[2];
3746 s_prividx
= cb
->args
[3];
3748 cb
->seq
= net
->dev_base_seq
;
3750 ifsm
= nlmsg_data(cb
->nlh
);
3751 filter_mask
= ifsm
->filter_mask
;
3755 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3757 head
= &net
->dev_index_head
[h
];
3758 hlist_for_each_entry(dev
, head
, index_hlist
) {
3761 err
= rtnl_fill_statsinfo(skb
, dev
, RTM_NEWSTATS
,
3762 NETLINK_CB(cb
->skb
).portid
,
3763 cb
->nlh
->nlmsg_seq
, 0,
3765 &s_idxattr
, &s_prividx
);
3766 /* If we ran out of room on the first message,
3769 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
3775 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
3781 cb
->args
[3] = s_prividx
;
3782 cb
->args
[2] = s_idxattr
;
3789 /* Process one rtnetlink message. */
3791 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3793 struct net
*net
= sock_net(skb
->sk
);
3794 rtnl_doit_func doit
;
3800 type
= nlh
->nlmsg_type
;
3806 /* All the messages must have at least 1 byte length */
3807 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
3810 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
3813 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
3816 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
3818 rtnl_dumpit_func dumpit
;
3819 rtnl_calcit_func calcit
;
3820 u16 min_dump_alloc
= 0;
3822 dumpit
= rtnl_get_dumpit(family
, type
);
3825 calcit
= rtnl_get_calcit(family
, type
);
3827 min_dump_alloc
= calcit(skb
, nlh
);
3832 struct netlink_dump_control c
= {
3834 .min_dump_alloc
= min_dump_alloc
,
3836 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
3842 doit
= rtnl_get_doit(family
, type
);
3846 return doit(skb
, nlh
);
3849 static void rtnetlink_rcv(struct sk_buff
*skb
)
3852 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
3856 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3858 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3864 case NETDEV_POST_INIT
:
3865 case NETDEV_REGISTER
:
3867 case NETDEV_PRE_TYPE_CHANGE
:
3868 case NETDEV_GOING_DOWN
:
3869 case NETDEV_UNREGISTER
:
3870 case NETDEV_UNREGISTER_FINAL
:
3871 case NETDEV_RELEASE
:
3873 case NETDEV_BONDING_INFO
:
3876 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
3882 static struct notifier_block rtnetlink_dev_notifier
= {
3883 .notifier_call
= rtnetlink_event
,
3887 static int __net_init
rtnetlink_net_init(struct net
*net
)
3890 struct netlink_kernel_cfg cfg
= {
3891 .groups
= RTNLGRP_MAX
,
3892 .input
= rtnetlink_rcv
,
3893 .cb_mutex
= &rtnl_mutex
,
3894 .flags
= NL_CFG_F_NONROOT_RECV
,
3897 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
3904 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
3906 netlink_kernel_release(net
->rtnl
);
3910 static struct pernet_operations rtnetlink_net_ops
= {
3911 .init
= rtnetlink_net_init
,
3912 .exit
= rtnetlink_net_exit
,
3915 void __init
rtnetlink_init(void)
3917 if (register_pernet_subsys(&rtnetlink_net_ops
))
3918 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3920 register_netdevice_notifier(&rtnetlink_dev_notifier
);
3922 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
3923 rtnl_dump_ifinfo
, rtnl_calcit
);
3924 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, NULL
);
3925 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, NULL
);
3926 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, NULL
);
3928 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, NULL
);
3929 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, NULL
);
3931 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, NULL
);
3932 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, NULL
);
3933 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, NULL
);
3935 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, NULL
);
3936 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, NULL
);
3937 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, NULL
);
3939 rtnl_register(PF_UNSPEC
, RTM_GETSTATS
, rtnl_stats_get
, rtnl_stats_dump
,