2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
43 #include <asm/uaccess.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
49 #include <net/protocol.h>
51 #include <net/route.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
62 rtnl_dumpit_func dumpit
;
63 rtnl_calcit_func calcit
;
66 static DEFINE_MUTEX(rtnl_mutex
);
70 mutex_lock(&rtnl_mutex
);
72 EXPORT_SYMBOL(rtnl_lock
);
74 void __rtnl_unlock(void)
76 mutex_unlock(&rtnl_mutex
);
79 void rtnl_unlock(void)
81 /* This fellow will unlock it for us. */
84 EXPORT_SYMBOL(rtnl_unlock
);
86 int rtnl_trylock(void)
88 return mutex_trylock(&rtnl_mutex
);
90 EXPORT_SYMBOL(rtnl_trylock
);
92 int rtnl_is_locked(void)
94 return mutex_is_locked(&rtnl_mutex
);
96 EXPORT_SYMBOL(rtnl_is_locked
);
98 #ifdef CONFIG_PROVE_LOCKING
99 int lockdep_rtnl_is_held(void)
101 return lockdep_is_held(&rtnl_mutex
);
103 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
104 #endif /* #ifdef CONFIG_PROVE_LOCKING */
106 static struct rtnl_link
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
108 static inline int rtm_msgindex(int msgtype
)
110 int msgindex
= msgtype
- RTM_BASE
;
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
117 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
122 static rtnl_doit_func
rtnl_get_doit(int protocol
, int msgindex
)
124 struct rtnl_link
*tab
;
126 if (protocol
<= RTNL_FAMILY_MAX
)
127 tab
= rtnl_msg_handlers
[protocol
];
131 if (tab
== NULL
|| tab
[msgindex
].doit
== NULL
)
132 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
134 return tab
[msgindex
].doit
;
137 static rtnl_dumpit_func
rtnl_get_dumpit(int protocol
, int msgindex
)
139 struct rtnl_link
*tab
;
141 if (protocol
<= RTNL_FAMILY_MAX
)
142 tab
= rtnl_msg_handlers
[protocol
];
146 if (tab
== NULL
|| tab
[msgindex
].dumpit
== NULL
)
147 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
149 return tab
[msgindex
].dumpit
;
152 static rtnl_calcit_func
rtnl_get_calcit(int protocol
, int msgindex
)
154 struct rtnl_link
*tab
;
156 if (protocol
<= RTNL_FAMILY_MAX
)
157 tab
= rtnl_msg_handlers
[protocol
];
161 if (tab
== NULL
|| tab
[msgindex
].calcit
== NULL
)
162 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
164 return tab
[msgindex
].calcit
;
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
173 * @calcit: Function pointer to calc size of dump message
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
183 * Returns 0 on success or a negative error code.
185 int __rtnl_register(int protocol
, int msgtype
,
186 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
187 rtnl_calcit_func calcit
)
189 struct rtnl_link
*tab
;
192 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
193 msgindex
= rtm_msgindex(msgtype
);
195 tab
= rtnl_msg_handlers
[protocol
];
197 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
201 rtnl_msg_handlers
[protocol
] = tab
;
205 tab
[msgindex
].doit
= doit
;
208 tab
[msgindex
].dumpit
= dumpit
;
211 tab
[msgindex
].calcit
= calcit
;
215 EXPORT_SYMBOL_GPL(__rtnl_register
);
218 * rtnl_register - Register a rtnetlink message type
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
224 * of memory implies no sense in continuing.
226 void rtnl_register(int protocol
, int msgtype
,
227 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
228 rtnl_calcit_func calcit
)
230 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
, calcit
) < 0)
231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
235 EXPORT_SYMBOL_GPL(rtnl_register
);
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
242 * Returns 0 on success or a negative error code.
244 int rtnl_unregister(int protocol
, int msgtype
)
248 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
249 msgindex
= rtm_msgindex(msgtype
);
251 if (rtnl_msg_handlers
[protocol
] == NULL
)
254 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
255 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
259 EXPORT_SYMBOL_GPL(rtnl_unregister
);
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
268 void rtnl_unregister_all(int protocol
)
270 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
272 kfree(rtnl_msg_handlers
[protocol
]);
273 rtnl_msg_handlers
[protocol
] = NULL
;
275 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
277 static LIST_HEAD(link_ops
);
279 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
281 const struct rtnl_link_ops
*ops
;
283 list_for_each_entry(ops
, &link_ops
, list
) {
284 if (!strcmp(ops
->kind
, kind
))
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
298 * Returns 0 on success or a negative error code.
300 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
302 if (rtnl_link_ops_get(ops
->kind
))
305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
310 if (ops
->setup
&& !ops
->dellink
)
311 ops
->dellink
= unregister_netdevice_queue
;
313 list_add_tail(&ops
->list
, &link_ops
);
316 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
322 * Returns 0 on success or a negative error code.
324 int rtnl_link_register(struct rtnl_link_ops
*ops
)
329 err
= __rtnl_link_register(ops
);
333 EXPORT_SYMBOL_GPL(rtnl_link_register
);
335 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
337 struct net_device
*dev
;
338 LIST_HEAD(list_kill
);
340 for_each_netdev(net
, dev
) {
341 if (dev
->rtnl_link_ops
== ops
)
342 ops
->dellink(dev
, &list_kill
);
344 unregister_netdevice_many(&list_kill
);
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
351 * The caller must hold the rtnl_mutex.
353 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
358 __rtnl_kill_links(net
, ops
);
360 list_del(&ops
->list
);
362 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
364 /* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
367 static void rtnl_lock_unregistering_all(void)
371 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
373 add_wait_queue(&netdev_unregistering_wq
, &wait
);
375 unregistering
= false;
378 if (net
->dev_unreg_count
> 0) {
379 unregistering
= true;
387 wait_woken(&wait
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
389 remove_wait_queue(&netdev_unregistering_wq
, &wait
);
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
396 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex
);
400 rtnl_lock_unregistering_all();
401 __rtnl_link_unregister(ops
);
403 mutex_unlock(&net_mutex
);
405 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
407 static size_t rtnl_link_get_slave_info_data_size(const struct net_device
*dev
)
409 struct net_device
*master_dev
;
410 const struct rtnl_link_ops
*ops
;
412 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
415 ops
= master_dev
->rtnl_link_ops
;
416 if (!ops
|| !ops
->get_slave_size
)
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr
)) +
420 ops
->get_slave_size(master_dev
, dev
);
423 static size_t rtnl_link_get_size(const struct net_device
*dev
)
425 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
431 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
435 /* IFLA_INFO_DATA + nested data */
436 size
+= nla_total_size(sizeof(struct nlattr
)) +
439 if (ops
->get_xstats_size
)
440 /* IFLA_INFO_XSTATS */
441 size
+= nla_total_size(ops
->get_xstats_size(dev
));
443 size
+= rtnl_link_get_slave_info_data_size(dev
);
448 static LIST_HEAD(rtnl_af_ops
);
450 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
452 const struct rtnl_af_ops
*ops
;
454 list_for_each_entry(ops
, &rtnl_af_ops
, list
) {
455 if (ops
->family
== family
)
463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
466 * Returns 0 on success or a negative error code.
468 void rtnl_af_register(struct rtnl_af_ops
*ops
)
471 list_add_tail(&ops
->list
, &rtnl_af_ops
);
474 EXPORT_SYMBOL_GPL(rtnl_af_register
);
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
480 * The caller must hold the rtnl_mutex.
482 void __rtnl_af_unregister(struct rtnl_af_ops
*ops
)
484 list_del(&ops
->list
);
486 EXPORT_SYMBOL_GPL(__rtnl_af_unregister
);
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
492 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
495 __rtnl_af_unregister(ops
);
498 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
500 static size_t rtnl_link_get_af_size(const struct net_device
*dev
)
502 struct rtnl_af_ops
*af_ops
;
506 size
= nla_total_size(sizeof(struct nlattr
));
508 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
509 if (af_ops
->get_link_af_size
) {
510 /* AF_* + nested data */
511 size
+= nla_total_size(sizeof(struct nlattr
)) +
512 af_ops
->get_link_af_size(dev
);
519 static bool rtnl_have_link_slave_info(const struct net_device
*dev
)
521 struct net_device
*master_dev
;
523 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
524 if (master_dev
&& master_dev
->rtnl_link_ops
)
529 static int rtnl_link_slave_info_fill(struct sk_buff
*skb
,
530 const struct net_device
*dev
)
532 struct net_device
*master_dev
;
533 const struct rtnl_link_ops
*ops
;
534 struct nlattr
*slave_data
;
537 master_dev
= netdev_master_upper_dev_get((struct net_device
*) dev
);
540 ops
= master_dev
->rtnl_link_ops
;
543 if (nla_put_string(skb
, IFLA_INFO_SLAVE_KIND
, ops
->kind
) < 0)
545 if (ops
->fill_slave_info
) {
546 slave_data
= nla_nest_start(skb
, IFLA_INFO_SLAVE_DATA
);
549 err
= ops
->fill_slave_info(skb
, master_dev
, dev
);
551 goto err_cancel_slave_data
;
552 nla_nest_end(skb
, slave_data
);
556 err_cancel_slave_data
:
557 nla_nest_cancel(skb
, slave_data
);
561 static int rtnl_link_info_fill(struct sk_buff
*skb
,
562 const struct net_device
*dev
)
564 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
570 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
572 if (ops
->fill_xstats
) {
573 err
= ops
->fill_xstats(skb
, dev
);
577 if (ops
->fill_info
) {
578 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
581 err
= ops
->fill_info(skb
, dev
);
583 goto err_cancel_data
;
584 nla_nest_end(skb
, data
);
589 nla_nest_cancel(skb
, data
);
593 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
595 struct nlattr
*linkinfo
;
598 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
599 if (linkinfo
== NULL
)
602 err
= rtnl_link_info_fill(skb
, dev
);
604 goto err_cancel_link
;
606 err
= rtnl_link_slave_info_fill(skb
, dev
);
608 goto err_cancel_link
;
610 nla_nest_end(skb
, linkinfo
);
614 nla_nest_cancel(skb
, linkinfo
);
619 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned int group
, int echo
)
621 struct sock
*rtnl
= net
->rtnl
;
624 NETLINK_CB(skb
).dst_group
= group
;
626 atomic_inc(&skb
->users
);
627 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
629 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
633 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
635 struct sock
*rtnl
= net
->rtnl
;
637 return nlmsg_unicast(rtnl
, skb
, pid
);
639 EXPORT_SYMBOL(rtnl_unicast
);
641 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
642 struct nlmsghdr
*nlh
, gfp_t flags
)
644 struct sock
*rtnl
= net
->rtnl
;
648 report
= nlmsg_report(nlh
);
650 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
652 EXPORT_SYMBOL(rtnl_notify
);
654 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
656 struct sock
*rtnl
= net
->rtnl
;
658 netlink_set_err(rtnl
, 0, group
, error
);
660 EXPORT_SYMBOL(rtnl_set_sk_err
);
662 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
667 mx
= nla_nest_start(skb
, RTA_METRICS
);
671 for (i
= 0; i
< RTAX_MAX
; i
++) {
673 if (i
== RTAX_CC_ALGO
- 1) {
674 char tmp
[TCP_CA_NAME_MAX
], *name
;
676 name
= tcp_ca_get_name_by_key(metrics
[i
], tmp
);
679 if (nla_put_string(skb
, i
+ 1, name
))
680 goto nla_put_failure
;
682 if (nla_put_u32(skb
, i
+ 1, metrics
[i
]))
683 goto nla_put_failure
;
690 nla_nest_cancel(skb
, mx
);
694 return nla_nest_end(skb
, mx
);
697 nla_nest_cancel(skb
, mx
);
700 EXPORT_SYMBOL(rtnetlink_put_metrics
);
702 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
703 long expires
, u32 error
)
705 struct rta_cacheinfo ci
= {
706 .rta_lastuse
= jiffies_delta_to_clock_t(jiffies
- dst
->lastuse
),
707 .rta_used
= dst
->__use
,
708 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
716 clock
= jiffies_to_clock_t(abs(expires
));
717 clock
= min_t(unsigned long, clock
, INT_MAX
);
718 ci
.rta_expires
= (expires
> 0) ? clock
: -clock
;
720 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
722 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
724 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
726 unsigned char operstate
= dev
->operstate
;
728 switch (transition
) {
730 if ((operstate
== IF_OPER_DORMANT
||
731 operstate
== IF_OPER_UNKNOWN
) &&
733 operstate
= IF_OPER_UP
;
736 case IF_OPER_DORMANT
:
737 if (operstate
== IF_OPER_UP
||
738 operstate
== IF_OPER_UNKNOWN
)
739 operstate
= IF_OPER_DORMANT
;
743 if (dev
->operstate
!= operstate
) {
744 write_lock_bh(&dev_base_lock
);
745 dev
->operstate
= operstate
;
746 write_unlock_bh(&dev_base_lock
);
747 netdev_state_change(dev
);
751 static unsigned int rtnl_dev_get_flags(const struct net_device
*dev
)
753 return (dev
->flags
& ~(IFF_PROMISC
| IFF_ALLMULTI
)) |
754 (dev
->gflags
& (IFF_PROMISC
| IFF_ALLMULTI
));
757 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
758 const struct ifinfomsg
*ifm
)
760 unsigned int flags
= ifm
->ifi_flags
;
762 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
764 flags
= (flags
& ifm
->ifi_change
) |
765 (rtnl_dev_get_flags(dev
) & ~ifm
->ifi_change
);
770 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
771 const struct rtnl_link_stats64
*b
)
773 a
->rx_packets
= b
->rx_packets
;
774 a
->tx_packets
= b
->tx_packets
;
775 a
->rx_bytes
= b
->rx_bytes
;
776 a
->tx_bytes
= b
->tx_bytes
;
777 a
->rx_errors
= b
->rx_errors
;
778 a
->tx_errors
= b
->tx_errors
;
779 a
->rx_dropped
= b
->rx_dropped
;
780 a
->tx_dropped
= b
->tx_dropped
;
782 a
->multicast
= b
->multicast
;
783 a
->collisions
= b
->collisions
;
785 a
->rx_length_errors
= b
->rx_length_errors
;
786 a
->rx_over_errors
= b
->rx_over_errors
;
787 a
->rx_crc_errors
= b
->rx_crc_errors
;
788 a
->rx_frame_errors
= b
->rx_frame_errors
;
789 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
790 a
->rx_missed_errors
= b
->rx_missed_errors
;
792 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
793 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
794 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
795 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
796 a
->tx_window_errors
= b
->tx_window_errors
;
798 a
->rx_compressed
= b
->rx_compressed
;
799 a
->tx_compressed
= b
->tx_compressed
;
802 static void copy_rtnl_link_stats64(void *v
, const struct rtnl_link_stats64
*b
)
804 memcpy(v
, b
, sizeof(*b
));
808 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
811 if (dev
->dev
.parent
&& dev_is_pci(dev
->dev
.parent
) &&
812 (ext_filter_mask
& RTEXT_FILTER_VF
)) {
813 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
814 size_t size
= nla_total_size(sizeof(struct nlattr
));
815 size
+= nla_total_size(num_vfs
* sizeof(struct nlattr
));
817 (nla_total_size(sizeof(struct ifla_vf_mac
)) +
818 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
819 nla_total_size(sizeof(struct ifla_vf_spoofchk
)) +
820 nla_total_size(sizeof(struct ifla_vf_rate
)) +
821 nla_total_size(sizeof(struct ifla_vf_link_state
)));
827 static size_t rtnl_port_size(const struct net_device
*dev
,
830 size_t port_size
= nla_total_size(4) /* PORT_VF */
831 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
832 + nla_total_size(sizeof(struct ifla_port_vsi
))
834 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
835 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
836 + nla_total_size(1) /* PROT_VDP_REQUEST */
837 + nla_total_size(2); /* PORT_VDP_RESPONSE */
838 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
839 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
841 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
844 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
845 !(ext_filter_mask
& RTEXT_FILTER_VF
))
847 if (dev_num_vf(dev
->dev
.parent
))
848 return port_self_size
+ vf_ports_size
+
849 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
851 return port_self_size
;
854 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
857 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
858 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
859 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
860 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
861 + nla_total_size(sizeof(struct rtnl_link_ifmap
))
862 + nla_total_size(sizeof(struct rtnl_link_stats
))
863 + nla_total_size(sizeof(struct rtnl_link_stats64
))
864 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
865 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
866 + nla_total_size(4) /* IFLA_TXQLEN */
867 + nla_total_size(4) /* IFLA_WEIGHT */
868 + nla_total_size(4) /* IFLA_MTU */
869 + nla_total_size(4) /* IFLA_LINK */
870 + nla_total_size(4) /* IFLA_MASTER */
871 + nla_total_size(1) /* IFLA_CARRIER */
872 + nla_total_size(4) /* IFLA_PROMISCUITY */
873 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
874 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
875 + nla_total_size(1) /* IFLA_OPERSTATE */
876 + nla_total_size(1) /* IFLA_LINKMODE */
877 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
878 + nla_total_size(4) /* IFLA_LINK_NETNSID */
879 + nla_total_size(ext_filter_mask
880 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
881 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
882 + rtnl_port_size(dev
, ext_filter_mask
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
883 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
884 + rtnl_link_get_af_size(dev
) /* IFLA_AF_SPEC */
885 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
) /* IFLA_PHYS_PORT_ID */
886 + nla_total_size(MAX_PHYS_ITEM_ID_LEN
); /* IFLA_PHYS_SWITCH_ID */
889 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
891 struct nlattr
*vf_ports
;
892 struct nlattr
*vf_port
;
896 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
900 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
901 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
903 goto nla_put_failure
;
904 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
905 goto nla_put_failure
;
906 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
907 if (err
== -EMSGSIZE
)
908 goto nla_put_failure
;
910 nla_nest_cancel(skb
, vf_port
);
913 nla_nest_end(skb
, vf_port
);
916 nla_nest_end(skb
, vf_ports
);
921 nla_nest_cancel(skb
, vf_ports
);
925 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
927 struct nlattr
*port_self
;
930 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
934 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
936 nla_nest_cancel(skb
, port_self
);
937 return (err
== -EMSGSIZE
) ? err
: 0;
940 nla_nest_end(skb
, port_self
);
945 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
,
950 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
||
951 !(ext_filter_mask
& RTEXT_FILTER_VF
))
954 err
= rtnl_port_self_fill(skb
, dev
);
958 if (dev_num_vf(dev
->dev
.parent
)) {
959 err
= rtnl_vf_ports_fill(skb
, dev
);
967 static int rtnl_phys_port_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
970 struct netdev_phys_item_id ppid
;
972 err
= dev_get_phys_port_id(dev
, &ppid
);
974 if (err
== -EOPNOTSUPP
)
979 if (nla_put(skb
, IFLA_PHYS_PORT_ID
, ppid
.id_len
, ppid
.id
))
985 static int rtnl_phys_switch_id_fill(struct sk_buff
*skb
, struct net_device
*dev
)
988 struct netdev_phys_item_id psid
;
990 err
= netdev_switch_parent_id_get(dev
, &psid
);
992 if (err
== -EOPNOTSUPP
)
997 if (nla_put(skb
, IFLA_PHYS_SWITCH_ID
, psid
.id_len
, psid
.id
))
1003 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
1004 int type
, u32 pid
, u32 seq
, u32 change
,
1005 unsigned int flags
, u32 ext_filter_mask
)
1007 struct ifinfomsg
*ifm
;
1008 struct nlmsghdr
*nlh
;
1009 struct rtnl_link_stats64 temp
;
1010 const struct rtnl_link_stats64
*stats
;
1011 struct nlattr
*attr
, *af_spec
;
1012 struct rtnl_af_ops
*af_ops
;
1013 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1016 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
1020 ifm
= nlmsg_data(nlh
);
1021 ifm
->ifi_family
= AF_UNSPEC
;
1023 ifm
->ifi_type
= dev
->type
;
1024 ifm
->ifi_index
= dev
->ifindex
;
1025 ifm
->ifi_flags
= dev_get_flags(dev
);
1026 ifm
->ifi_change
= change
;
1028 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
1029 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
1030 nla_put_u8(skb
, IFLA_OPERSTATE
,
1031 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
1032 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
1033 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
1034 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
1035 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
1036 nla_put_u32(skb
, IFLA_NUM_TX_QUEUES
, dev
->num_tx_queues
) ||
1038 nla_put_u32(skb
, IFLA_NUM_RX_QUEUES
, dev
->num_rx_queues
) ||
1040 (dev
->ifindex
!= dev
->iflink
&&
1041 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)) ||
1043 nla_put_u32(skb
, IFLA_MASTER
, upper_dev
->ifindex
)) ||
1044 nla_put_u8(skb
, IFLA_CARRIER
, netif_carrier_ok(dev
)) ||
1046 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
1048 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)) ||
1049 nla_put_u32(skb
, IFLA_CARRIER_CHANGES
,
1050 atomic_read(&dev
->carrier_changes
)))
1051 goto nla_put_failure
;
1054 struct rtnl_link_ifmap map
= {
1055 .mem_start
= dev
->mem_start
,
1056 .mem_end
= dev
->mem_end
,
1057 .base_addr
= dev
->base_addr
,
1060 .port
= dev
->if_port
,
1062 if (nla_put(skb
, IFLA_MAP
, sizeof(map
), &map
))
1063 goto nla_put_failure
;
1066 if (dev
->addr_len
) {
1067 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
1068 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
1069 goto nla_put_failure
;
1072 if (rtnl_phys_port_id_fill(skb
, dev
))
1073 goto nla_put_failure
;
1075 if (rtnl_phys_switch_id_fill(skb
, dev
))
1076 goto nla_put_failure
;
1078 attr
= nla_reserve(skb
, IFLA_STATS
,
1079 sizeof(struct rtnl_link_stats
));
1081 goto nla_put_failure
;
1083 stats
= dev_get_stats(dev
, &temp
);
1084 copy_rtnl_link_stats(nla_data(attr
), stats
);
1086 attr
= nla_reserve(skb
, IFLA_STATS64
,
1087 sizeof(struct rtnl_link_stats64
));
1089 goto nla_put_failure
;
1090 copy_rtnl_link_stats64(nla_data(attr
), stats
);
1092 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
1093 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
1094 goto nla_put_failure
;
1096 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
1097 && (ext_filter_mask
& RTEXT_FILTER_VF
)) {
1100 struct nlattr
*vfinfo
, *vf
;
1101 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
1103 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
1105 goto nla_put_failure
;
1106 for (i
= 0; i
< num_vfs
; i
++) {
1107 struct ifla_vf_info ivi
;
1108 struct ifla_vf_mac vf_mac
;
1109 struct ifla_vf_vlan vf_vlan
;
1110 struct ifla_vf_rate vf_rate
;
1111 struct ifla_vf_tx_rate vf_tx_rate
;
1112 struct ifla_vf_spoofchk vf_spoofchk
;
1113 struct ifla_vf_link_state vf_linkstate
;
1116 * Not all SR-IOV capable drivers support the
1117 * spoofcheck query. Preset to -1 so the user
1118 * space tool can detect that the driver didn't
1122 memset(ivi
.mac
, 0, sizeof(ivi
.mac
));
1123 /* The default value for VF link state is "auto"
1124 * IFLA_VF_LINK_STATE_AUTO which equals zero
1127 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, i
, &ivi
))
1134 vf_linkstate
.vf
= ivi
.vf
;
1136 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
1137 vf_vlan
.vlan
= ivi
.vlan
;
1138 vf_vlan
.qos
= ivi
.qos
;
1139 vf_tx_rate
.rate
= ivi
.max_tx_rate
;
1140 vf_rate
.min_tx_rate
= ivi
.min_tx_rate
;
1141 vf_rate
.max_tx_rate
= ivi
.max_tx_rate
;
1142 vf_spoofchk
.setting
= ivi
.spoofchk
;
1143 vf_linkstate
.link_state
= ivi
.linkstate
;
1144 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
1146 nla_nest_cancel(skb
, vfinfo
);
1147 goto nla_put_failure
;
1149 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
1150 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
1151 nla_put(skb
, IFLA_VF_RATE
, sizeof(vf_rate
),
1153 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
1155 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
1157 nla_put(skb
, IFLA_VF_LINK_STATE
, sizeof(vf_linkstate
),
1159 goto nla_put_failure
;
1160 nla_nest_end(skb
, vf
);
1162 nla_nest_end(skb
, vfinfo
);
1165 if (rtnl_port_fill(skb
, dev
, ext_filter_mask
))
1166 goto nla_put_failure
;
1168 if (dev
->rtnl_link_ops
|| rtnl_have_link_slave_info(dev
)) {
1169 if (rtnl_link_fill(skb
, dev
) < 0)
1170 goto nla_put_failure
;
1173 if (dev
->rtnl_link_ops
&&
1174 dev
->rtnl_link_ops
->get_link_net
) {
1175 struct net
*link_net
= dev
->rtnl_link_ops
->get_link_net(dev
);
1177 if (!net_eq(dev_net(dev
), link_net
)) {
1178 int id
= peernet2id(dev_net(dev
), link_net
);
1180 if (nla_put_s32(skb
, IFLA_LINK_NETNSID
, id
))
1181 goto nla_put_failure
;
1185 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1186 goto nla_put_failure
;
1188 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1189 if (af_ops
->fill_link_af
) {
1193 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1194 goto nla_put_failure
;
1196 err
= af_ops
->fill_link_af(skb
, dev
);
1199 * Caller may return ENODATA to indicate that there
1200 * was no data to be dumped. This is not an error, it
1201 * means we should trim the attribute header and
1204 if (err
== -ENODATA
)
1205 nla_nest_cancel(skb
, af
);
1207 goto nla_put_failure
;
1209 nla_nest_end(skb
, af
);
1213 nla_nest_end(skb
, af_spec
);
1215 nlmsg_end(skb
, nlh
);
1219 nlmsg_cancel(skb
, nlh
);
1223 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1224 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1225 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1226 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1227 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1228 [IFLA_MTU
] = { .type
= NLA_U32
},
1229 [IFLA_LINK
] = { .type
= NLA_U32
},
1230 [IFLA_MASTER
] = { .type
= NLA_U32
},
1231 [IFLA_CARRIER
] = { .type
= NLA_U8
},
1232 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1233 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1234 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1235 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1236 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1237 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1238 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1239 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1240 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1241 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1242 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1243 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1244 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1245 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1246 [IFLA_NUM_TX_QUEUES
] = { .type
= NLA_U32
},
1247 [IFLA_NUM_RX_QUEUES
] = { .type
= NLA_U32
},
1248 [IFLA_PHYS_PORT_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1249 [IFLA_CARRIER_CHANGES
] = { .type
= NLA_U32
}, /* ignored */
1250 [IFLA_PHYS_SWITCH_ID
] = { .type
= NLA_BINARY
, .len
= MAX_PHYS_ITEM_ID_LEN
},
1251 [IFLA_LINK_NETNSID
] = { .type
= NLA_S32
},
1254 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1255 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1256 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1257 [IFLA_INFO_SLAVE_KIND
] = { .type
= NLA_STRING
},
1258 [IFLA_INFO_SLAVE_DATA
] = { .type
= NLA_NESTED
},
1261 static const struct nla_policy ifla_vfinfo_policy
[IFLA_VF_INFO_MAX
+1] = {
1262 [IFLA_VF_INFO
] = { .type
= NLA_NESTED
},
1265 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1266 [IFLA_VF_MAC
] = { .len
= sizeof(struct ifla_vf_mac
) },
1267 [IFLA_VF_VLAN
] = { .len
= sizeof(struct ifla_vf_vlan
) },
1268 [IFLA_VF_TX_RATE
] = { .len
= sizeof(struct ifla_vf_tx_rate
) },
1269 [IFLA_VF_SPOOFCHK
] = { .len
= sizeof(struct ifla_vf_spoofchk
) },
1270 [IFLA_VF_RATE
] = { .len
= sizeof(struct ifla_vf_rate
) },
1271 [IFLA_VF_LINK_STATE
] = { .len
= sizeof(struct ifla_vf_link_state
) },
1274 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1275 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1276 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1277 .len
= PORT_PROFILE_MAX
},
1278 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1279 .len
= sizeof(struct ifla_port_vsi
)},
1280 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1281 .len
= PORT_UUID_MAX
},
1282 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1283 .len
= PORT_UUID_MAX
},
1284 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1285 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1288 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1290 struct net
*net
= sock_net(skb
->sk
);
1293 struct net_device
*dev
;
1294 struct hlist_head
*head
;
1295 struct nlattr
*tb
[IFLA_MAX
+1];
1296 u32 ext_filter_mask
= 0;
1301 s_idx
= cb
->args
[1];
1303 cb
->seq
= net
->dev_base_seq
;
1305 /* A hack to preserve kernel<->userspace interface.
1306 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1307 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1308 * what iproute2 < v3.9.0 used.
1309 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1310 * attribute, its netlink message is shorter than struct ifinfomsg.
1312 hdrlen
= nlmsg_len(cb
->nlh
) < sizeof(struct ifinfomsg
) ?
1313 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
1315 if (nlmsg_parse(cb
->nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
1317 if (tb
[IFLA_EXT_MASK
])
1318 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1321 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1323 head
= &net
->dev_index_head
[h
];
1324 hlist_for_each_entry(dev
, head
, index_hlist
) {
1327 err
= rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1328 NETLINK_CB(cb
->skb
).portid
,
1329 cb
->nlh
->nlmsg_seq
, 0,
1332 /* If we ran out of room on the first message,
1335 WARN_ON((err
== -EMSGSIZE
) && (skb
->len
== 0));
1340 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1352 int rtnl_nla_parse_ifla(struct nlattr
**tb
, const struct nlattr
*head
, int len
)
1354 return nla_parse(tb
, IFLA_MAX
, head
, len
, ifla_policy
);
1356 EXPORT_SYMBOL(rtnl_nla_parse_ifla
);
1358 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1361 /* Examine the link attributes and figure out which
1362 * network namespace we are talking about.
1364 if (tb
[IFLA_NET_NS_PID
])
1365 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1366 else if (tb
[IFLA_NET_NS_FD
])
1367 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1369 net
= get_net(src_net
);
1372 EXPORT_SYMBOL(rtnl_link_get_net
);
1374 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1377 if (tb
[IFLA_ADDRESS
] &&
1378 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1381 if (tb
[IFLA_BROADCAST
] &&
1382 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1386 if (tb
[IFLA_AF_SPEC
]) {
1390 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1391 const struct rtnl_af_ops
*af_ops
;
1393 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1394 return -EAFNOSUPPORT
;
1396 if (!af_ops
->set_link_af
)
1399 if (af_ops
->validate_link_af
) {
1400 err
= af_ops
->validate_link_af(dev
, af
);
1410 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
*attr
)
1412 int rem
, err
= -EINVAL
;
1414 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1416 nla_for_each_nested(vf
, attr
, rem
) {
1417 switch (nla_type(vf
)) {
1419 struct ifla_vf_mac
*ivm
;
1422 if (ops
->ndo_set_vf_mac
)
1423 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1427 case IFLA_VF_VLAN
: {
1428 struct ifla_vf_vlan
*ivv
;
1431 if (ops
->ndo_set_vf_vlan
)
1432 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
,
1437 case IFLA_VF_TX_RATE
: {
1438 struct ifla_vf_tx_rate
*ivt
;
1439 struct ifla_vf_info ivf
;
1442 if (ops
->ndo_get_vf_config
)
1443 err
= ops
->ndo_get_vf_config(dev
, ivt
->vf
,
1448 if (ops
->ndo_set_vf_rate
)
1449 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1454 case IFLA_VF_RATE
: {
1455 struct ifla_vf_rate
*ivt
;
1458 if (ops
->ndo_set_vf_rate
)
1459 err
= ops
->ndo_set_vf_rate(dev
, ivt
->vf
,
1464 case IFLA_VF_SPOOFCHK
: {
1465 struct ifla_vf_spoofchk
*ivs
;
1468 if (ops
->ndo_set_vf_spoofchk
)
1469 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1473 case IFLA_VF_LINK_STATE
: {
1474 struct ifla_vf_link_state
*ivl
;
1477 if (ops
->ndo_set_vf_link_state
)
1478 err
= ops
->ndo_set_vf_link_state(dev
, ivl
->vf
,
1492 static int do_set_master(struct net_device
*dev
, int ifindex
)
1494 struct net_device
*upper_dev
= netdev_master_upper_dev_get(dev
);
1495 const struct net_device_ops
*ops
;
1499 if (upper_dev
->ifindex
== ifindex
)
1501 ops
= upper_dev
->netdev_ops
;
1502 if (ops
->ndo_del_slave
) {
1503 err
= ops
->ndo_del_slave(upper_dev
, dev
);
1512 upper_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1515 ops
= upper_dev
->netdev_ops
;
1516 if (ops
->ndo_add_slave
) {
1517 err
= ops
->ndo_add_slave(upper_dev
, dev
);
1527 #define DO_SETLINK_MODIFIED 0x01
1528 /* notify flag means notify + modified. */
1529 #define DO_SETLINK_NOTIFY 0x03
1530 static int do_setlink(const struct sk_buff
*skb
,
1531 struct net_device
*dev
, struct ifinfomsg
*ifm
,
1532 struct nlattr
**tb
, char *ifname
, int status
)
1534 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1537 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1538 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1543 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
1548 err
= dev_change_net_namespace(dev
, net
, ifname
);
1552 status
|= DO_SETLINK_MODIFIED
;
1556 struct rtnl_link_ifmap
*u_map
;
1559 if (!ops
->ndo_set_config
) {
1564 if (!netif_device_present(dev
)) {
1569 u_map
= nla_data(tb
[IFLA_MAP
]);
1570 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1571 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1572 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1573 k_map
.irq
= (unsigned char) u_map
->irq
;
1574 k_map
.dma
= (unsigned char) u_map
->dma
;
1575 k_map
.port
= (unsigned char) u_map
->port
;
1577 err
= ops
->ndo_set_config(dev
, &k_map
);
1581 status
|= DO_SETLINK_NOTIFY
;
1584 if (tb
[IFLA_ADDRESS
]) {
1585 struct sockaddr
*sa
;
1588 len
= sizeof(sa_family_t
) + dev
->addr_len
;
1589 sa
= kmalloc(len
, GFP_KERNEL
);
1594 sa
->sa_family
= dev
->type
;
1595 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1597 err
= dev_set_mac_address(dev
, sa
);
1601 status
|= DO_SETLINK_MODIFIED
;
1605 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1608 status
|= DO_SETLINK_MODIFIED
;
1611 if (tb
[IFLA_GROUP
]) {
1612 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1613 status
|= DO_SETLINK_NOTIFY
;
1617 * Interface selected by interface index but interface
1618 * name provided implies that a name change has been
1621 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1622 err
= dev_change_name(dev
, ifname
);
1625 status
|= DO_SETLINK_MODIFIED
;
1628 if (tb
[IFLA_IFALIAS
]) {
1629 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
1630 nla_len(tb
[IFLA_IFALIAS
]));
1633 status
|= DO_SETLINK_NOTIFY
;
1636 if (tb
[IFLA_BROADCAST
]) {
1637 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
1638 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
1641 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
1642 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1647 if (tb
[IFLA_MASTER
]) {
1648 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1651 status
|= DO_SETLINK_MODIFIED
;
1654 if (tb
[IFLA_CARRIER
]) {
1655 err
= dev_change_carrier(dev
, nla_get_u8(tb
[IFLA_CARRIER
]));
1658 status
|= DO_SETLINK_MODIFIED
;
1661 if (tb
[IFLA_TXQLEN
]) {
1662 unsigned long value
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1664 if (dev
->tx_queue_len
^ value
)
1665 status
|= DO_SETLINK_NOTIFY
;
1667 dev
->tx_queue_len
= value
;
1670 if (tb
[IFLA_OPERSTATE
])
1671 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1673 if (tb
[IFLA_LINKMODE
]) {
1674 unsigned char value
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1676 write_lock_bh(&dev_base_lock
);
1677 if (dev
->link_mode
^ value
)
1678 status
|= DO_SETLINK_NOTIFY
;
1679 dev
->link_mode
= value
;
1680 write_unlock_bh(&dev_base_lock
);
1683 if (tb
[IFLA_VFINFO_LIST
]) {
1684 struct nlattr
*attr
;
1686 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
1687 if (nla_type(attr
) != IFLA_VF_INFO
) {
1691 err
= do_setvfinfo(dev
, attr
);
1694 status
|= DO_SETLINK_NOTIFY
;
1699 if (tb
[IFLA_VF_PORTS
]) {
1700 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1701 struct nlattr
*attr
;
1706 if (!ops
->ndo_set_vf_port
)
1709 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
1710 if (nla_type(attr
) != IFLA_VF_PORT
)
1712 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1713 attr
, ifla_port_policy
);
1716 if (!port
[IFLA_PORT_VF
]) {
1720 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
1721 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
1724 status
|= DO_SETLINK_NOTIFY
;
1729 if (tb
[IFLA_PORT_SELF
]) {
1730 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1732 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1733 tb
[IFLA_PORT_SELF
], ifla_port_policy
);
1738 if (ops
->ndo_set_vf_port
)
1739 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
1742 status
|= DO_SETLINK_NOTIFY
;
1745 if (tb
[IFLA_AF_SPEC
]) {
1749 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1750 const struct rtnl_af_ops
*af_ops
;
1752 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1755 err
= af_ops
->set_link_af(dev
, af
);
1759 status
|= DO_SETLINK_NOTIFY
;
1765 if (status
& DO_SETLINK_MODIFIED
) {
1766 if (status
& DO_SETLINK_NOTIFY
)
1767 netdev_state_change(dev
);
1770 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",
1777 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1779 struct net
*net
= sock_net(skb
->sk
);
1780 struct ifinfomsg
*ifm
;
1781 struct net_device
*dev
;
1783 struct nlattr
*tb
[IFLA_MAX
+1];
1784 char ifname
[IFNAMSIZ
];
1786 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1790 if (tb
[IFLA_IFNAME
])
1791 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1796 ifm
= nlmsg_data(nlh
);
1797 if (ifm
->ifi_index
> 0)
1798 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1799 else if (tb
[IFLA_IFNAME
])
1800 dev
= __dev_get_by_name(net
, ifname
);
1809 err
= validate_linkmsg(dev
, tb
);
1813 err
= do_setlink(skb
, dev
, ifm
, tb
, ifname
, 0);
1818 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1820 struct net
*net
= sock_net(skb
->sk
);
1821 const struct rtnl_link_ops
*ops
;
1822 struct net_device
*dev
;
1823 struct ifinfomsg
*ifm
;
1824 char ifname
[IFNAMSIZ
];
1825 struct nlattr
*tb
[IFLA_MAX
+1];
1827 LIST_HEAD(list_kill
);
1829 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1833 if (tb
[IFLA_IFNAME
])
1834 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1836 ifm
= nlmsg_data(nlh
);
1837 if (ifm
->ifi_index
> 0)
1838 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1839 else if (tb
[IFLA_IFNAME
])
1840 dev
= __dev_get_by_name(net
, ifname
);
1847 ops
= dev
->rtnl_link_ops
;
1848 if (!ops
|| !ops
->dellink
)
1851 ops
->dellink(dev
, &list_kill
);
1852 unregister_netdevice_many(&list_kill
);
1856 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
1858 unsigned int old_flags
;
1861 old_flags
= dev
->flags
;
1862 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
1863 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1868 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
1870 __dev_notify_flags(dev
, old_flags
, ~0U);
1873 EXPORT_SYMBOL(rtnl_configure_link
);
1875 struct net_device
*rtnl_create_link(struct net
*net
,
1876 char *ifname
, unsigned char name_assign_type
,
1877 const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
1880 struct net_device
*dev
;
1881 unsigned int num_tx_queues
= 1;
1882 unsigned int num_rx_queues
= 1;
1884 if (tb
[IFLA_NUM_TX_QUEUES
])
1885 num_tx_queues
= nla_get_u32(tb
[IFLA_NUM_TX_QUEUES
]);
1886 else if (ops
->get_num_tx_queues
)
1887 num_tx_queues
= ops
->get_num_tx_queues();
1889 if (tb
[IFLA_NUM_RX_QUEUES
])
1890 num_rx_queues
= nla_get_u32(tb
[IFLA_NUM_RX_QUEUES
]);
1891 else if (ops
->get_num_rx_queues
)
1892 num_rx_queues
= ops
->get_num_rx_queues();
1895 dev
= alloc_netdev_mqs(ops
->priv_size
, ifname
, name_assign_type
,
1896 ops
->setup
, num_tx_queues
, num_rx_queues
);
1900 dev_net_set(dev
, net
);
1901 dev
->rtnl_link_ops
= ops
;
1902 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
1905 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
1906 if (tb
[IFLA_ADDRESS
]) {
1907 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
1908 nla_len(tb
[IFLA_ADDRESS
]));
1909 dev
->addr_assign_type
= NET_ADDR_SET
;
1911 if (tb
[IFLA_BROADCAST
])
1912 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
1913 nla_len(tb
[IFLA_BROADCAST
]));
1914 if (tb
[IFLA_TXQLEN
])
1915 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1916 if (tb
[IFLA_OPERSTATE
])
1917 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1918 if (tb
[IFLA_LINKMODE
])
1919 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1921 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1926 return ERR_PTR(err
);
1928 EXPORT_SYMBOL(rtnl_create_link
);
1930 static int rtnl_group_changelink(const struct sk_buff
*skb
,
1931 struct net
*net
, int group
,
1932 struct ifinfomsg
*ifm
,
1935 struct net_device
*dev
;
1938 for_each_netdev(net
, dev
) {
1939 if (dev
->group
== group
) {
1940 err
= do_setlink(skb
, dev
, ifm
, tb
, NULL
, 0);
1949 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1951 struct net
*net
= sock_net(skb
->sk
);
1952 const struct rtnl_link_ops
*ops
;
1953 const struct rtnl_link_ops
*m_ops
= NULL
;
1954 struct net_device
*dev
;
1955 struct net_device
*master_dev
= NULL
;
1956 struct ifinfomsg
*ifm
;
1957 char kind
[MODULE_NAME_LEN
];
1958 char ifname
[IFNAMSIZ
];
1959 struct nlattr
*tb
[IFLA_MAX
+1];
1960 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
1961 unsigned char name_assign_type
= NET_NAME_USER
;
1964 #ifdef CONFIG_MODULES
1967 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1971 if (tb
[IFLA_IFNAME
])
1972 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1976 ifm
= nlmsg_data(nlh
);
1977 if (ifm
->ifi_index
> 0)
1978 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1981 dev
= __dev_get_by_name(net
, ifname
);
1987 master_dev
= netdev_master_upper_dev_get(dev
);
1989 m_ops
= master_dev
->rtnl_link_ops
;
1992 err
= validate_linkmsg(dev
, tb
);
1996 if (tb
[IFLA_LINKINFO
]) {
1997 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
1998 tb
[IFLA_LINKINFO
], ifla_info_policy
);
2002 memset(linkinfo
, 0, sizeof(linkinfo
));
2004 if (linkinfo
[IFLA_INFO_KIND
]) {
2005 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
2006 ops
= rtnl_link_ops_get(kind
);
2013 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 1];
2014 struct nlattr
*slave_attr
[m_ops
? m_ops
->slave_maxtype
+ 1 : 1];
2015 struct nlattr
**data
= NULL
;
2016 struct nlattr
**slave_data
= NULL
;
2017 struct net
*dest_net
, *link_net
= NULL
;
2020 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
2021 err
= nla_parse_nested(attr
, ops
->maxtype
,
2022 linkinfo
[IFLA_INFO_DATA
],
2028 if (ops
->validate
) {
2029 err
= ops
->validate(tb
, data
);
2036 if (m_ops
->slave_maxtype
&&
2037 linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2038 err
= nla_parse_nested(slave_attr
,
2039 m_ops
->slave_maxtype
,
2040 linkinfo
[IFLA_INFO_SLAVE_DATA
],
2041 m_ops
->slave_policy
);
2044 slave_data
= slave_attr
;
2046 if (m_ops
->slave_validate
) {
2047 err
= m_ops
->slave_validate(tb
, slave_data
);
2056 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2058 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2061 if (linkinfo
[IFLA_INFO_DATA
]) {
2062 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
2066 err
= ops
->changelink(dev
, tb
, data
);
2069 status
|= DO_SETLINK_NOTIFY
;
2072 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
2073 if (!m_ops
|| !m_ops
->slave_changelink
)
2076 err
= m_ops
->slave_changelink(master_dev
, dev
,
2080 status
|= DO_SETLINK_NOTIFY
;
2083 return do_setlink(skb
, dev
, ifm
, tb
, ifname
, status
);
2086 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
2087 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
2088 return rtnl_group_changelink(skb
, net
,
2089 nla_get_u32(tb
[IFLA_GROUP
]),
2094 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
2098 #ifdef CONFIG_MODULES
2101 request_module("rtnl-link-%s", kind
);
2103 ops
= rtnl_link_ops_get(kind
);
2115 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
2116 name_assign_type
= NET_NAME_ENUM
;
2119 dest_net
= rtnl_link_get_net(net
, tb
);
2120 if (IS_ERR(dest_net
))
2121 return PTR_ERR(dest_net
);
2124 if (!netlink_ns_capable(skb
, dest_net
->user_ns
, CAP_NET_ADMIN
))
2127 if (tb
[IFLA_LINK_NETNSID
]) {
2128 int id
= nla_get_s32(tb
[IFLA_LINK_NETNSID
]);
2130 link_net
= get_net_ns_by_id(dest_net
, id
);
2136 if (!netlink_ns_capable(skb
, link_net
->user_ns
, CAP_NET_ADMIN
))
2140 dev
= rtnl_create_link(link_net
? : dest_net
, ifname
,
2141 name_assign_type
, ops
, tb
);
2147 dev
->ifindex
= ifm
->ifi_index
;
2150 err
= ops
->newlink(link_net
? : net
, dev
, tb
, data
);
2151 /* Drivers should call free_netdev() in ->destructor
2152 * and unregister it on failure after registration
2153 * so that device could be finally freed in rtnl_unlock.
2156 /* If device is not registered at all, free it now */
2157 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
2162 err
= register_netdevice(dev
);
2168 err
= rtnl_configure_link(dev
, ifm
);
2171 LIST_HEAD(list_kill
);
2173 ops
->dellink(dev
, &list_kill
);
2174 unregister_netdevice_many(&list_kill
);
2176 unregister_netdevice(dev
);
2182 err
= dev_change_net_namespace(dev
, dest_net
, ifname
);
2184 unregister_netdevice(dev
);
2194 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
)
2196 struct net
*net
= sock_net(skb
->sk
);
2197 struct ifinfomsg
*ifm
;
2198 char ifname
[IFNAMSIZ
];
2199 struct nlattr
*tb
[IFLA_MAX
+1];
2200 struct net_device
*dev
= NULL
;
2201 struct sk_buff
*nskb
;
2203 u32 ext_filter_mask
= 0;
2205 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
2209 if (tb
[IFLA_IFNAME
])
2210 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
2212 if (tb
[IFLA_EXT_MASK
])
2213 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2215 ifm
= nlmsg_data(nlh
);
2216 if (ifm
->ifi_index
> 0)
2217 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2218 else if (tb
[IFLA_IFNAME
])
2219 dev
= __dev_get_by_name(net
, ifname
);
2226 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
2230 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).portid
,
2231 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
2233 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2234 WARN_ON(err
== -EMSGSIZE
);
2237 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).portid
);
2242 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2244 struct net
*net
= sock_net(skb
->sk
);
2245 struct net_device
*dev
;
2246 struct nlattr
*tb
[IFLA_MAX
+1];
2247 u32 ext_filter_mask
= 0;
2248 u16 min_ifinfo_dump_size
= 0;
2251 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2252 hdrlen
= nlmsg_len(nlh
) < sizeof(struct ifinfomsg
) ?
2253 sizeof(struct rtgenmsg
) : sizeof(struct ifinfomsg
);
2255 if (nlmsg_parse(nlh
, hdrlen
, tb
, IFLA_MAX
, ifla_policy
) >= 0) {
2256 if (tb
[IFLA_EXT_MASK
])
2257 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
2260 if (!ext_filter_mask
)
2261 return NLMSG_GOODSIZE
;
2263 * traverse the list of net devices and compute the minimum
2264 * buffer size based upon the filter mask.
2266 list_for_each_entry(dev
, &net
->dev_base_head
, dev_list
) {
2267 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
2272 return min_ifinfo_dump_size
;
2275 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2278 int s_idx
= cb
->family
;
2282 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
2283 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
2284 if (idx
< s_idx
|| idx
== PF_PACKET
)
2286 if (rtnl_msg_handlers
[idx
] == NULL
||
2287 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
2290 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
2294 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
2302 struct sk_buff
*rtmsg_ifinfo_build_skb(int type
, struct net_device
*dev
,
2303 unsigned int change
, gfp_t flags
)
2305 struct net
*net
= dev_net(dev
);
2306 struct sk_buff
*skb
;
2308 size_t if_info_size
;
2310 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), flags
);
2314 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
2316 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2317 WARN_ON(err
== -EMSGSIZE
);
2324 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2328 void rtmsg_ifinfo_send(struct sk_buff
*skb
, struct net_device
*dev
, gfp_t flags
)
2330 struct net
*net
= dev_net(dev
);
2332 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, flags
);
2335 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned int change
,
2338 struct sk_buff
*skb
;
2340 skb
= rtmsg_ifinfo_build_skb(type
, dev
, change
, flags
);
2342 rtmsg_ifinfo_send(skb
, dev
, flags
);
2344 EXPORT_SYMBOL(rtmsg_ifinfo
);
2346 static int nlmsg_populate_fdb_fill(struct sk_buff
*skb
,
2347 struct net_device
*dev
,
2348 u8
*addr
, u32 pid
, u32 seq
,
2349 int type
, unsigned int flags
,
2352 struct nlmsghdr
*nlh
;
2355 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), nlflags
);
2359 ndm
= nlmsg_data(nlh
);
2360 ndm
->ndm_family
= AF_BRIDGE
;
2363 ndm
->ndm_flags
= flags
;
2365 ndm
->ndm_ifindex
= dev
->ifindex
;
2366 ndm
->ndm_state
= NUD_PERMANENT
;
2368 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
2369 goto nla_put_failure
;
2371 nlmsg_end(skb
, nlh
);
2375 nlmsg_cancel(skb
, nlh
);
2379 static inline size_t rtnl_fdb_nlmsg_size(void)
2381 return NLMSG_ALIGN(sizeof(struct ndmsg
)) + nla_total_size(ETH_ALEN
);
2384 static void rtnl_fdb_notify(struct net_device
*dev
, u8
*addr
, int type
)
2386 struct net
*net
= dev_net(dev
);
2387 struct sk_buff
*skb
;
2390 skb
= nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC
);
2394 err
= nlmsg_populate_fdb_fill(skb
, dev
, addr
, 0, 0, type
, NTF_SELF
, 0);
2400 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
2403 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
2407 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2409 int ndo_dflt_fdb_add(struct ndmsg
*ndm
,
2410 struct nlattr
*tb
[],
2411 struct net_device
*dev
,
2412 const unsigned char *addr
, u16 vid
,
2417 /* If aging addresses are supported device will need to
2418 * implement its own handler for this.
2420 if (ndm
->ndm_state
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
2421 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2426 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev
->name
);
2430 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2431 err
= dev_uc_add_excl(dev
, addr
);
2432 else if (is_multicast_ether_addr(addr
))
2433 err
= dev_mc_add_excl(dev
, addr
);
2435 /* Only return duplicate errors if NLM_F_EXCL is set */
2436 if (err
== -EEXIST
&& !(flags
& NLM_F_EXCL
))
2441 EXPORT_SYMBOL(ndo_dflt_fdb_add
);
2443 static int fdb_vid_parse(struct nlattr
*vlan_attr
, u16
*p_vid
)
2448 if (nla_len(vlan_attr
) != sizeof(u16
)) {
2449 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2453 vid
= nla_get_u16(vlan_attr
);
2455 if (!vid
|| vid
>= VLAN_VID_MASK
) {
2456 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2465 static int rtnl_fdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2467 struct net
*net
= sock_net(skb
->sk
);
2469 struct nlattr
*tb
[NDA_MAX
+1];
2470 struct net_device
*dev
;
2475 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2479 ndm
= nlmsg_data(nlh
);
2480 if (ndm
->ndm_ifindex
== 0) {
2481 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2485 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2487 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2491 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2492 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2496 addr
= nla_data(tb
[NDA_LLADDR
]);
2498 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2504 /* Support fdb on master device the net/bridge default case */
2505 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2506 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2507 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2508 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2510 err
= ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2515 ndm
->ndm_flags
&= ~NTF_MASTER
;
2518 /* Embedded bridge, macvlan, and any other device support */
2519 if ((ndm
->ndm_flags
& NTF_SELF
)) {
2520 if (dev
->netdev_ops
->ndo_fdb_add
)
2521 err
= dev
->netdev_ops
->ndo_fdb_add(ndm
, tb
, dev
, addr
,
2525 err
= ndo_dflt_fdb_add(ndm
, tb
, dev
, addr
, vid
,
2529 rtnl_fdb_notify(dev
, addr
, RTM_NEWNEIGH
);
2530 ndm
->ndm_flags
&= ~NTF_SELF
;
2538 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2540 int ndo_dflt_fdb_del(struct ndmsg
*ndm
,
2541 struct nlattr
*tb
[],
2542 struct net_device
*dev
,
2543 const unsigned char *addr
, u16 vid
)
2547 /* If aging addresses are supported device will need to
2548 * implement its own handler for this.
2550 if (!(ndm
->ndm_state
& NUD_PERMANENT
)) {
2551 pr_info("%s: FDB only supports static addresses\n", dev
->name
);
2555 if (is_unicast_ether_addr(addr
) || is_link_local_ether_addr(addr
))
2556 err
= dev_uc_del(dev
, addr
);
2557 else if (is_multicast_ether_addr(addr
))
2558 err
= dev_mc_del(dev
, addr
);
2562 EXPORT_SYMBOL(ndo_dflt_fdb_del
);
2564 static int rtnl_fdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2566 struct net
*net
= sock_net(skb
->sk
);
2568 struct nlattr
*tb
[NDA_MAX
+1];
2569 struct net_device
*dev
;
2574 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
2577 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
, NULL
);
2581 ndm
= nlmsg_data(nlh
);
2582 if (ndm
->ndm_ifindex
== 0) {
2583 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2587 dev
= __dev_get_by_index(net
, ndm
->ndm_ifindex
);
2589 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2593 if (!tb
[NDA_LLADDR
] || nla_len(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2594 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2598 addr
= nla_data(tb
[NDA_LLADDR
]);
2600 err
= fdb_vid_parse(tb
[NDA_VLAN
], &vid
);
2606 /* Support fdb on master device the net/bridge default case */
2607 if ((!ndm
->ndm_flags
|| ndm
->ndm_flags
& NTF_MASTER
) &&
2608 (dev
->priv_flags
& IFF_BRIDGE_PORT
)) {
2609 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2610 const struct net_device_ops
*ops
= br_dev
->netdev_ops
;
2612 if (ops
->ndo_fdb_del
)
2613 err
= ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2618 ndm
->ndm_flags
&= ~NTF_MASTER
;
2621 /* Embedded bridge, macvlan, and any other device support */
2622 if (ndm
->ndm_flags
& NTF_SELF
) {
2623 if (dev
->netdev_ops
->ndo_fdb_del
)
2624 err
= dev
->netdev_ops
->ndo_fdb_del(ndm
, tb
, dev
, addr
,
2627 err
= ndo_dflt_fdb_del(ndm
, tb
, dev
, addr
, vid
);
2630 rtnl_fdb_notify(dev
, addr
, RTM_DELNEIGH
);
2631 ndm
->ndm_flags
&= ~NTF_SELF
;
2638 static int nlmsg_populate_fdb(struct sk_buff
*skb
,
2639 struct netlink_callback
*cb
,
2640 struct net_device
*dev
,
2642 struct netdev_hw_addr_list
*list
)
2644 struct netdev_hw_addr
*ha
;
2648 portid
= NETLINK_CB(cb
->skb
).portid
;
2649 seq
= cb
->nlh
->nlmsg_seq
;
2651 list_for_each_entry(ha
, &list
->list
, list
) {
2652 if (*idx
< cb
->args
[0])
2655 err
= nlmsg_populate_fdb_fill(skb
, dev
, ha
->addr
,
2657 RTM_NEWNEIGH
, NTF_SELF
,
2668 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2669 * @nlh: netlink message header
2672 * Default netdevice operation to dump the existing unicast address list.
2673 * Returns number of addresses from list put in skb.
2675 int ndo_dflt_fdb_dump(struct sk_buff
*skb
,
2676 struct netlink_callback
*cb
,
2677 struct net_device
*dev
,
2678 struct net_device
*filter_dev
,
2683 netif_addr_lock_bh(dev
);
2684 err
= nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->uc
);
2687 nlmsg_populate_fdb(skb
, cb
, dev
, &idx
, &dev
->mc
);
2689 netif_addr_unlock_bh(dev
);
2692 EXPORT_SYMBOL(ndo_dflt_fdb_dump
);
2694 static int rtnl_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2696 struct net_device
*dev
;
2697 struct nlattr
*tb
[IFLA_MAX
+1];
2698 struct net_device
*bdev
= NULL
;
2699 struct net_device
*br_dev
= NULL
;
2700 const struct net_device_ops
*ops
= NULL
;
2701 const struct net_device_ops
*cops
= NULL
;
2702 struct ifinfomsg
*ifm
= nlmsg_data(cb
->nlh
);
2703 struct net
*net
= sock_net(skb
->sk
);
2708 if (nlmsg_parse(cb
->nlh
, sizeof(struct ifinfomsg
), tb
, IFLA_MAX
,
2709 ifla_policy
) == 0) {
2710 if (tb
[IFLA_MASTER
])
2711 br_idx
= nla_get_u32(tb
[IFLA_MASTER
]);
2714 brport_idx
= ifm
->ifi_index
;
2717 br_dev
= __dev_get_by_index(net
, br_idx
);
2721 ops
= br_dev
->netdev_ops
;
2725 for_each_netdev(net
, dev
) {
2726 if (brport_idx
&& (dev
->ifindex
!= brport_idx
))
2729 if (!br_idx
) { /* user did not specify a specific bridge */
2730 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2731 br_dev
= netdev_master_upper_dev_get(dev
);
2732 cops
= br_dev
->netdev_ops
;
2737 if (dev
!= br_dev
&&
2738 !(dev
->priv_flags
& IFF_BRIDGE_PORT
))
2741 if (br_dev
!= netdev_master_upper_dev_get(dev
) &&
2742 !(dev
->priv_flags
& IFF_EBRIDGE
))
2749 if (dev
->priv_flags
& IFF_BRIDGE_PORT
) {
2750 if (cops
&& cops
->ndo_fdb_dump
)
2751 idx
= cops
->ndo_fdb_dump(skb
, cb
, br_dev
, dev
,
2755 if (dev
->netdev_ops
->ndo_fdb_dump
)
2756 idx
= dev
->netdev_ops
->ndo_fdb_dump(skb
, cb
, dev
, NULL
,
2759 idx
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
2768 static int brport_nla_put_flag(struct sk_buff
*skb
, u32 flags
, u32 mask
,
2769 unsigned int attrnum
, unsigned int flag
)
2772 return nla_put_u8(skb
, attrnum
, !!(flags
& flag
));
2776 int ndo_dflt_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
2777 struct net_device
*dev
, u16 mode
,
2778 u32 flags
, u32 mask
)
2780 struct nlmsghdr
*nlh
;
2781 struct ifinfomsg
*ifm
;
2782 struct nlattr
*br_afspec
;
2783 struct nlattr
*protinfo
;
2784 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
2785 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2787 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWLINK
, sizeof(*ifm
), NLM_F_MULTI
);
2791 ifm
= nlmsg_data(nlh
);
2792 ifm
->ifi_family
= AF_BRIDGE
;
2794 ifm
->ifi_type
= dev
->type
;
2795 ifm
->ifi_index
= dev
->ifindex
;
2796 ifm
->ifi_flags
= dev_get_flags(dev
);
2797 ifm
->ifi_change
= 0;
2800 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
2801 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
2802 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
2804 nla_put_u32(skb
, IFLA_MASTER
, br_dev
->ifindex
)) ||
2806 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
2807 (dev
->ifindex
!= dev
->iflink
&&
2808 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)))
2809 goto nla_put_failure
;
2811 br_afspec
= nla_nest_start(skb
, IFLA_AF_SPEC
);
2813 goto nla_put_failure
;
2815 if (nla_put_u16(skb
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
)) {
2816 nla_nest_cancel(skb
, br_afspec
);
2817 goto nla_put_failure
;
2820 if (mode
!= BRIDGE_MODE_UNDEF
) {
2821 if (nla_put_u16(skb
, IFLA_BRIDGE_MODE
, mode
)) {
2822 nla_nest_cancel(skb
, br_afspec
);
2823 goto nla_put_failure
;
2826 nla_nest_end(skb
, br_afspec
);
2828 protinfo
= nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
2830 goto nla_put_failure
;
2832 if (brport_nla_put_flag(skb
, flags
, mask
,
2833 IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
) ||
2834 brport_nla_put_flag(skb
, flags
, mask
,
2835 IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
) ||
2836 brport_nla_put_flag(skb
, flags
, mask
,
2837 IFLA_BRPORT_FAST_LEAVE
,
2838 BR_MULTICAST_FAST_LEAVE
) ||
2839 brport_nla_put_flag(skb
, flags
, mask
,
2840 IFLA_BRPORT_PROTECT
, BR_ROOT_BLOCK
) ||
2841 brport_nla_put_flag(skb
, flags
, mask
,
2842 IFLA_BRPORT_LEARNING
, BR_LEARNING
) ||
2843 brport_nla_put_flag(skb
, flags
, mask
,
2844 IFLA_BRPORT_LEARNING_SYNC
, BR_LEARNING_SYNC
) ||
2845 brport_nla_put_flag(skb
, flags
, mask
,
2846 IFLA_BRPORT_UNICAST_FLOOD
, BR_FLOOD
) ||
2847 brport_nla_put_flag(skb
, flags
, mask
,
2848 IFLA_BRPORT_PROXYARP
, BR_PROXYARP
)) {
2849 nla_nest_cancel(skb
, protinfo
);
2850 goto nla_put_failure
;
2853 nla_nest_end(skb
, protinfo
);
2855 nlmsg_end(skb
, nlh
);
2858 nlmsg_cancel(skb
, nlh
);
2861 EXPORT_SYMBOL(ndo_dflt_bridge_getlink
);
2863 static int rtnl_bridge_getlink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2865 struct net
*net
= sock_net(skb
->sk
);
2866 struct net_device
*dev
;
2868 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
2869 u32 seq
= cb
->nlh
->nlmsg_seq
;
2870 u32 filter_mask
= 0;
2872 if (nlmsg_len(cb
->nlh
) > sizeof(struct ifinfomsg
)) {
2873 struct nlattr
*extfilt
;
2875 extfilt
= nlmsg_find_attr(cb
->nlh
, sizeof(struct ifinfomsg
),
2878 if (nla_len(extfilt
) < sizeof(filter_mask
))
2881 filter_mask
= nla_get_u32(extfilt
);
2886 for_each_netdev_rcu(net
, dev
) {
2887 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2888 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2890 if (br_dev
&& br_dev
->netdev_ops
->ndo_bridge_getlink
) {
2891 if (idx
>= cb
->args
[0] &&
2892 br_dev
->netdev_ops
->ndo_bridge_getlink(
2893 skb
, portid
, seq
, dev
, filter_mask
) < 0)
2898 if (ops
->ndo_bridge_getlink
) {
2899 if (idx
>= cb
->args
[0] &&
2900 ops
->ndo_bridge_getlink(skb
, portid
, seq
, dev
,
2912 static inline size_t bridge_nlmsg_size(void)
2914 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
2915 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
2916 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
2917 + nla_total_size(sizeof(u32
)) /* IFLA_MASTER */
2918 + nla_total_size(sizeof(u32
)) /* IFLA_MTU */
2919 + nla_total_size(sizeof(u32
)) /* IFLA_LINK */
2920 + nla_total_size(sizeof(u32
)) /* IFLA_OPERSTATE */
2921 + nla_total_size(sizeof(u8
)) /* IFLA_PROTINFO */
2922 + nla_total_size(sizeof(struct nlattr
)) /* IFLA_AF_SPEC */
2923 + nla_total_size(sizeof(u16
)) /* IFLA_BRIDGE_FLAGS */
2924 + nla_total_size(sizeof(u16
)); /* IFLA_BRIDGE_MODE */
2927 static int rtnl_bridge_notify(struct net_device
*dev
)
2929 struct net
*net
= dev_net(dev
);
2930 struct sk_buff
*skb
;
2931 int err
= -EOPNOTSUPP
;
2933 if (!dev
->netdev_ops
->ndo_bridge_getlink
)
2936 skb
= nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC
);
2942 err
= dev
->netdev_ops
->ndo_bridge_getlink(skb
, 0, 0, dev
, 0);
2949 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
2952 WARN_ON(err
== -EMSGSIZE
);
2955 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
2959 static int rtnl_bridge_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2961 struct net
*net
= sock_net(skb
->sk
);
2962 struct ifinfomsg
*ifm
;
2963 struct net_device
*dev
;
2964 struct nlattr
*br_spec
, *attr
= NULL
;
2965 int rem
, err
= -EOPNOTSUPP
;
2967 bool have_flags
= false;
2969 if (nlmsg_len(nlh
) < sizeof(*ifm
))
2972 ifm
= nlmsg_data(nlh
);
2973 if (ifm
->ifi_family
!= AF_BRIDGE
)
2974 return -EPFNOSUPPORT
;
2976 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
2978 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2982 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
2984 nla_for_each_nested(attr
, br_spec
, rem
) {
2985 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
2986 if (nla_len(attr
) < sizeof(flags
))
2990 flags
= nla_get_u16(attr
);
2996 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
2997 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
2999 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_setlink
) {
3004 err
= br_dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
3008 flags
&= ~BRIDGE_FLAGS_MASTER
;
3011 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3012 if (!dev
->netdev_ops
->ndo_bridge_setlink
)
3015 err
= dev
->netdev_ops
->ndo_bridge_setlink(dev
, nlh
,
3018 flags
&= ~BRIDGE_FLAGS_SELF
;
3020 /* Generate event to notify upper layer of bridge
3023 err
= rtnl_bridge_notify(dev
);
3028 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3033 static int rtnl_bridge_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3035 struct net
*net
= sock_net(skb
->sk
);
3036 struct ifinfomsg
*ifm
;
3037 struct net_device
*dev
;
3038 struct nlattr
*br_spec
, *attr
= NULL
;
3039 int rem
, err
= -EOPNOTSUPP
;
3041 bool have_flags
= false;
3043 if (nlmsg_len(nlh
) < sizeof(*ifm
))
3046 ifm
= nlmsg_data(nlh
);
3047 if (ifm
->ifi_family
!= AF_BRIDGE
)
3048 return -EPFNOSUPPORT
;
3050 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
3052 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3056 br_spec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
3058 nla_for_each_nested(attr
, br_spec
, rem
) {
3059 if (nla_type(attr
) == IFLA_BRIDGE_FLAGS
) {
3060 if (nla_len(attr
) < sizeof(flags
))
3064 flags
= nla_get_u16(attr
);
3070 if (!flags
|| (flags
& BRIDGE_FLAGS_MASTER
)) {
3071 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3073 if (!br_dev
|| !br_dev
->netdev_ops
->ndo_bridge_dellink
) {
3078 err
= br_dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
3082 flags
&= ~BRIDGE_FLAGS_MASTER
;
3085 if ((flags
& BRIDGE_FLAGS_SELF
)) {
3086 if (!dev
->netdev_ops
->ndo_bridge_dellink
)
3089 err
= dev
->netdev_ops
->ndo_bridge_dellink(dev
, nlh
,
3093 flags
&= ~BRIDGE_FLAGS_SELF
;
3095 /* Generate event to notify upper layer of bridge
3098 err
= rtnl_bridge_notify(dev
);
3103 memcpy(nla_data(attr
), &flags
, sizeof(flags
));
3108 /* Process one rtnetlink message. */
3110 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3112 struct net
*net
= sock_net(skb
->sk
);
3113 rtnl_doit_func doit
;
3119 type
= nlh
->nlmsg_type
;
3125 /* All the messages must have at least 1 byte length */
3126 if (nlmsg_len(nlh
) < sizeof(struct rtgenmsg
))
3129 family
= ((struct rtgenmsg
*)nlmsg_data(nlh
))->rtgen_family
;
3133 if (kind
!= 2 && !netlink_net_capable(skb
, CAP_NET_ADMIN
))
3136 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
3138 rtnl_dumpit_func dumpit
;
3139 rtnl_calcit_func calcit
;
3140 u16 min_dump_alloc
= 0;
3142 dumpit
= rtnl_get_dumpit(family
, type
);
3145 calcit
= rtnl_get_calcit(family
, type
);
3147 min_dump_alloc
= calcit(skb
, nlh
);
3152 struct netlink_dump_control c
= {
3154 .min_dump_alloc
= min_dump_alloc
,
3156 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
3162 doit
= rtnl_get_doit(family
, type
);
3166 return doit(skb
, nlh
);
3169 static void rtnetlink_rcv(struct sk_buff
*skb
)
3172 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
3176 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3178 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3184 case NETDEV_POST_INIT
:
3185 case NETDEV_REGISTER
:
3187 case NETDEV_PRE_TYPE_CHANGE
:
3188 case NETDEV_GOING_DOWN
:
3189 case NETDEV_UNREGISTER
:
3190 case NETDEV_UNREGISTER_FINAL
:
3191 case NETDEV_RELEASE
:
3193 case NETDEV_BONDING_INFO
:
3196 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0, GFP_KERNEL
);
3202 static struct notifier_block rtnetlink_dev_notifier
= {
3203 .notifier_call
= rtnetlink_event
,
3207 static int __net_init
rtnetlink_net_init(struct net
*net
)
3210 struct netlink_kernel_cfg cfg
= {
3211 .groups
= RTNLGRP_MAX
,
3212 .input
= rtnetlink_rcv
,
3213 .cb_mutex
= &rtnl_mutex
,
3214 .flags
= NL_CFG_F_NONROOT_RECV
,
3217 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, &cfg
);
3224 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
3226 netlink_kernel_release(net
->rtnl
);
3230 static struct pernet_operations rtnetlink_net_ops
= {
3231 .init
= rtnetlink_net_init
,
3232 .exit
= rtnetlink_net_exit
,
3235 void __init
rtnetlink_init(void)
3237 if (register_pernet_subsys(&rtnetlink_net_ops
))
3238 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3240 register_netdevice_notifier(&rtnetlink_dev_notifier
);
3242 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
3243 rtnl_dump_ifinfo
, rtnl_calcit
);
3244 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, NULL
);
3245 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, NULL
);
3246 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, NULL
);
3248 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, NULL
);
3249 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, NULL
);
3251 rtnl_register(PF_BRIDGE
, RTM_NEWNEIGH
, rtnl_fdb_add
, NULL
, NULL
);
3252 rtnl_register(PF_BRIDGE
, RTM_DELNEIGH
, rtnl_fdb_del
, NULL
, NULL
);
3253 rtnl_register(PF_BRIDGE
, RTM_GETNEIGH
, NULL
, rtnl_fdb_dump
, NULL
);
3255 rtnl_register(PF_BRIDGE
, RTM_GETLINK
, NULL
, rtnl_bridge_getlink
, NULL
);
3256 rtnl_register(PF_BRIDGE
, RTM_DELLINK
, rtnl_bridge_dellink
, NULL
, NULL
);
3257 rtnl_register(PF_BRIDGE
, RTM_SETLINK
, rtnl_bridge_setlink
, NULL
, NULL
);